AOMedia Codec SDK: aomdec

AOMedia Codec SDK

aomdec
1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include <assert.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdarg.h>
16 #include <string.h>
17 #include <limits.h>
18
19 #include "config/aom_config.h"
20
21 #if CONFIG_OS_SUPPORT
22 #if HAVE_UNISTD_H
23 #include <unistd.h> // NOLINT
24 #elif !defined(STDOUT_FILENO)
25 #define STDOUT_FILENO 1
26 #endif
27 #endif
28
29 #include "aom/aom_decoder.h"
30 #include "aom/aomdx.h"
31 #include "aom_ports/aom_timer.h"
32 #include "aom_ports/mem_ops.h"
33 #include "common/args.h"
34 #include "common/ivfdec.h"
35 #include "common/md5_utils.h"
36 #include "common/obudec.h"
37 #include "common/tools_common.h"
38
39 #if CONFIG_WEBM_IO
40 #include "common/webmdec.h"
41 #endif
42
43 #include "common/rawenc.h"
44 #include "common/y4menc.h"
45
46 #if CONFIG_LIBYUV
47 #include "third_party/libyuv/include/libyuv/scale.h"
48 #endif
49
50 static const char *exec_name;
51
52 struct AvxDecInputContext {
53 struct AvxInputContext *aom_input_ctx;
54 struct ObuDecInputContext *obu_ctx;
55 struct WebmInputContext *webm_ctx;
56 };
57
58 static const arg_def_t help =
59 ARG_DEF(NULL, "help", 0, "Show usage options and exit");
60 static const arg_def_t looparg =
61 ARG_DEF(NULL, "loops", 1, "Number of times to decode the file");
62 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
63 static const arg_def_t use_yv12 =
64 ARG_DEF(NULL, "yv12", 0, "Output raw YV12 frames");
65 static const arg_def_t use_i420 =
66 ARG_DEF(NULL, "i420", 0, "Output raw I420 frames");
67 static const arg_def_t flipuvarg =
68 ARG_DEF(NULL, "flipuv", 0, "Flip the chroma planes in the output");
69 static const arg_def_t rawvideo =
70 ARG_DEF(NULL, "rawvideo", 0, "Output raw YUV frames");
71 static const arg_def_t noblitarg =
72 ARG_DEF(NULL, "noblit", 0, "Don't process the decoded frames");
73 static const arg_def_t progressarg =
74 ARG_DEF(NULL, "progress", 0, "Show progress after each frame decodes");
75 static const arg_def_t limitarg =
76 ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
77 static const arg_def_t skiparg =
78 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
79 static const arg_def_t postprocarg =
80 ARG_DEF(NULL, "postproc", 0, "Postprocess decoded frames");
81 static const arg_def_t summaryarg =
82 ARG_DEF(NULL, "summary", 0, "Show timing summary");
83 static const arg_def_t outputfile =
84 ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
85 static const arg_def_t threadsarg =
86 ARG_DEF("t", "threads", 1, "Max threads to use");
87 static const arg_def_t verbosearg =
88 ARG_DEF("v", "verbose", 0, "Show version string");
89 static const arg_def_t scalearg =
90 ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
91 static const arg_def_t continuearg =
92 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
93 static const arg_def_t fb_arg =
94 ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
95 static const arg_def_t md5arg =
96 ARG_DEF(NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
97 static const arg_def_t framestatsarg =
98 ARG_DEF(NULL, "framestats", 1, "Output per-frame stats (.csv format)");
99 static const arg_def_t outbitdeptharg =
100 ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
101 static const arg_def_t tilem = ARG_DEF(NULL, "tile-mode", 1,
102 "Tile coding mode "
103 "(0 for normal tile coding mode)");
104 static const arg_def_t tiler = ARG_DEF(NULL, "tile-row", 1,
105 "Row index of tile to decode "
106 "(-1 for all rows)");
107 static const arg_def_t tilec = ARG_DEF(NULL, "tile-column", 1,
108 "Column index of tile to decode "
109 "(-1 for all columns)");
110 static const arg_def_t isannexb =
111 ARG_DEF(NULL, "annexb", 0, "Bitstream is in Annex-B format");
112 static const arg_def_t oppointarg = ARG_DEF(
113 NULL, "oppoint", 1, "Select an operating point of a scalable bitstream");
114 static const arg_def_t outallarg = ARG_DEF(
115 NULL, "all-layers", 0, "Output all decoded frames of a scalable bitstream");
116 static const arg_def_t skipfilmgrain =
117 ARG_DEF(NULL, "skip-film-grain", 0, "Skip film grain application");
118
119 static const arg_def_t *all_args[] = {
120 &help, &codecarg, &use_yv12, &use_i420,
121 &flipuvarg, &rawvideo, &noblitarg, &progressarg,
122 &limitarg, &skiparg, &postprocarg, &summaryarg,
123 &outputfile, &threadsarg, &verbosearg, &scalearg,
124 &fb_arg, &md5arg, &framestatsarg, &continuearg,
125 &outbitdeptharg, &isannexb, &oppointarg, &outallarg,
126 &skipfilmgrain, NULL
127 };
128
129 #if CONFIG_LIBYUV
130 static INLINE int libyuv_scale(aom_image_t *src, aom_image_t *dst,
131 FilterModeEnum mode) {
132 if (src->fmt == AOM_IMG_FMT_I42016) {
133 assert(dst->fmt == AOM_IMG_FMT_I42016);
134 return I420Scale_16(
135 (uint16_t *)src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y] / 2,
136 (uint16_t *)src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U] / 2,
137 (uint16_t *)src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V] / 2,
138 src->d_w, src->d_h, (uint16_t *)dst->planes[AOM_PLANE_Y],
139 dst->stride[AOM_PLANE_Y] / 2, (uint16_t *)dst->planes[AOM_PLANE_U],
140 dst->stride[AOM_PLANE_U] / 2, (uint16_t *)dst->planes[AOM_PLANE_V],
141 dst->stride[AOM_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
142 }
143 assert(src->fmt == AOM_IMG_FMT_I420);
144 assert(dst->fmt == AOM_IMG_FMT_I420);
145 return I420Scale(src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y],
146 src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U],
147 src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V], src->d_w,
148 src->d_h, dst->planes[AOM_PLANE_Y], dst->stride[AOM_PLANE_Y],
149 dst->planes[AOM_PLANE_U], dst->stride[AOM_PLANE_U],
150 dst->planes[AOM_PLANE_V], dst->stride[AOM_PLANE_V], dst->d_w,
151 dst->d_h, mode);
152 }
153 #endif
154
155 void show_help(FILE *fout, int shorthelp) {
156 fprintf(fout, "Usage: %s <options> filename\n\n", exec_name);
157
158 if (shorthelp) {
159 fprintf(fout, "Use --help to see the full list of options.\n");
160 return;
161 }
162
163 fprintf(fout, "Options:\n");
164 arg_show_usage(fout, all_args);
165 fprintf(fout,
166 "\nOutput File Patterns:\n\n"
167 " The -o argument specifies the name of the file(s) to "
168 "write to. If the\n argument does not include any escape "
169 "characters, the output will be\n written to a single file. "
170 "Otherwise, the filename will be calculated by\n expanding "
171 "the following escape characters:\n");
172 fprintf(fout,
173 "\n\t%%w - Frame width"
174 "\n\t%%h - Frame height"
175 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
176 "\n\n Pattern arguments are only supported in conjunction "
177 "with the --yv12 and\n --i420 options. If the -o option is "
178 "not specified, the output will be\n directed to stdout.\n");
179 fprintf(fout, "\nIncluded decoders:\n\n");
180
181 for (int i = 0; i < get_aom_decoder_count(); ++i) {
182 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
183 fprintf(fout, " %-6s - %s\n", decoder->name,
184 aom_codec_iface_name(decoder->codec_interface()));
185 }
186 }
187
188 void usage_exit(void) {
189 show_help(stderr, 1);
190 exit(EXIT_FAILURE);
191 }
192
193 static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
194 size_t *buffer_size) {
195 char raw_hdr[RAW_FRAME_HDR_SZ];
196 size_t frame_size = 0;
197
198 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
199 if (!feof(infile)) warn("Failed to read RAW frame size\n");
200 } else {
201 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
202 const size_t kFrameTooSmallThreshold = 256 * 1024;
203 frame_size = mem_get_le32(raw_hdr);
204
205 if (frame_size > kCorruptFrameThreshold) {
206 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
207 frame_size = 0;
208 }
209
210 if (frame_size < kFrameTooSmallThreshold) {
211 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
212 (unsigned int)frame_size);
213 }
214
215 if (frame_size > *buffer_size) {
216 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
217 if (new_buf) {
218 *buffer = new_buf;
219 *buffer_size = 2 * frame_size;
220 } else {
221 warn("Failed to allocate compressed data buffer\n");
222 frame_size = 0;
223 }
224 }
225 }
226
227 if (!feof(infile)) {
228 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
229 warn("Failed to read full frame\n");
230 return 1;
231 }
232 *bytes_read = frame_size;
233 }
234
235 return 0;
236 }
237
238 static int read_frame(struct AvxDecInputContext *input, uint8_t **buf,
239 size_t *bytes_in_buffer, size_t *buffer_size) {
240 switch (input->aom_input_ctx->file_type) {
241 #if CONFIG_WEBM_IO
242 case FILE_TYPE_WEBM:
243 return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer,
244 buffer_size);
245 #endif
246 case FILE_TYPE_RAW:
247 return raw_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
248 buffer_size);
249 case FILE_TYPE_IVF:
250 return ivf_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
251 buffer_size, NULL);
252 case FILE_TYPE_OBU:
253 return obudec_read_temporal_unit(input->obu_ctx, buf, bytes_in_buffer,
254 buffer_size);
255 default: return 1;
256 }
257 }
258
259 static int file_is_raw(struct AvxInputContext *input) {
260 uint8_t buf[32];
261 int is_raw = 0;
262 aom_codec_stream_info_t si;
263 memset(&si, 0, sizeof(si));
264
265 if (fread(buf, 1, 32, input->file) == 32) {
266 int i;
267
268 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
269 for (i = 0; i < get_aom_decoder_count(); ++i) {
270 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
271 if (!aom_codec_peek_stream_info(decoder->codec_interface(), buf + 4,
272 32 - 4, &si)) {
273 is_raw = 1;
274 input->fourcc = decoder->fourcc;
275 input->width = si.w;
276 input->height = si.h;
277 input->framerate.numerator = 30;
278 input->framerate.denominator = 1;
279 break;
280 }
281 }
282 }
283 }
284
285 rewind(input->file);
286 return is_raw;
287 }
288
289 static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
290 fprintf(stderr,
291 "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
292 frame_in, frame_out, dx_time,
293 (double)frame_out * 1000000.0 / (double)dx_time);
294 }
295
296 struct ExternalFrameBuffer {
297 uint8_t *data;
298 size_t size;
299 int in_use;
300 };
301
302 struct ExternalFrameBufferList {
303 int num_external_frame_buffers;
304 struct ExternalFrameBuffer *ext_fb;
305 };
306
307 // Callback used by libaom to request an external frame buffer. |cb_priv|
308 // Application private data passed into the set function. |min_size| is the
309 // minimum size in bytes needed to decode the next frame. |fb| pointer to the
310 // frame buffer.
311 static int get_av1_frame_buffer(void *cb_priv, size_t min_size,
312 aom_codec_frame_buffer_t *fb) {
313 int i;
314 struct ExternalFrameBufferList *const ext_fb_list =
315 (struct ExternalFrameBufferList *)cb_priv;
316 if (ext_fb_list == NULL) return -1;
317
318 // Find a free frame buffer.
319 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
320 if (!ext_fb_list->ext_fb[i].in_use) break;
321 }
322
323 if (i == ext_fb_list->num_external_frame_buffers) return -1;
324
325 if (ext_fb_list->ext_fb[i].size < min_size) {
326 free(ext_fb_list->ext_fb[i].data);
327 ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
328 if (!ext_fb_list->ext_fb[i].data) return -1;
329
330 ext_fb_list->ext_fb[i].size = min_size;
331 }
332
333 fb->data = ext_fb_list->ext_fb[i].data;
334 fb->size = ext_fb_list->ext_fb[i].size;
335 ext_fb_list->ext_fb[i].in_use = 1;
336
337 // Set the frame buffer's private data to point at the external frame buffer.
338 fb->priv = &ext_fb_list->ext_fb[i];
339 return 0;
340 }
341
342 // Callback used by libaom when there are no references to the frame buffer.
343 // |cb_priv| user private data passed into the set function. |fb| pointer
344 // to the frame buffer.
345 static int release_av1_frame_buffer(void *cb_priv,
346 aom_codec_frame_buffer_t *fb) {
347 struct ExternalFrameBuffer *const ext_fb =
348 (struct ExternalFrameBuffer *)fb->priv;
349 (void)cb_priv;
350 ext_fb->in_use = 0;
351 return 0;
352 }
353
354 static void generate_filename(const char *pattern, char *out, size_t q_len,
355 unsigned int d_w, unsigned int d_h,
356 unsigned int frame_in) {
357 const char *p = pattern;
358 char *q = out;
359
360 do {
361 char *next_pat = strchr(p, '%');
362
363 if (p == next_pat) {
364 size_t pat_len;
365
366 /* parse the pattern */
367 q[q_len - 1] = '\0';
368 switch (p[1]) {
369 case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
370 case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
371 case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
372 case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
373 case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
374 case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
375 case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
376 case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
377 case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
378 case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
379 case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
380 default: die("Unrecognized pattern %%%c\n", p[1]); break;
381 }
382
383 pat_len = strlen(q);
384 if (pat_len >= q_len - 1) die("Output filename too long.\n");
385 q += pat_len;
386 p += 2;
387 q_len -= pat_len;
388 } else {
389 size_t copy_len;
390
391 /* copy the next segment */
392 if (!next_pat)
393 copy_len = strlen(p);
394 else
395 copy_len = next_pat - p;
396
397 if (copy_len >= q_len - 1) die("Output filename too long.\n");
398
399 memcpy(q, p, copy_len);
400 q[copy_len] = '\0';
401 q += copy_len;
402 p += copy_len;
403 q_len -= copy_len;
404 }
405 } while (*p);
406 }
407
408 static int is_single_file(const char *outfile_pattern) {
409 const char *p = outfile_pattern;
410
411 do {
412 p = strchr(p, '%');
413 if (p && p[1] >= '1' && p[1] <= '9')
414 return 0; // pattern contains sequence number, so it's not unique
415 if (p) p++;
416 } while (p);
417
418 return 1;
419 }
420
421 static void print_md5(unsigned char digest[16], const char *filename) {
422 int i;
423
424 for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
425 printf(" %s\n", filename);
426 }
427
428 static FILE *open_outfile(const char *name) {
429 if (strcmp("-", name) == 0) {
430 set_binary_mode(stdout);
431 return stdout;
432 } else {
433 FILE *file = fopen(name, "wb");
434 if (!file) fatal("Failed to open output file '%s'", name);
435 return file;
436 }
437 }
438
439 static int main_loop(int argc, const char **argv_) {
440 aom_codec_ctx_t decoder;
441 char *fn = NULL;
442 int i;
443 int ret = EXIT_FAILURE;
444 uint8_t *buf = NULL;
445 size_t bytes_in_buffer = 0, buffer_size = 0;
446 FILE *infile;
447 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
448 int do_md5 = 0, progress = 0;
449 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
450 int arg_skip = 0;
451 int keep_going = 0;
452 const AvxInterface *interface = NULL;
453 const AvxInterface *fourcc_interface = NULL;
454 uint64_t dx_time = 0;
455 struct arg arg;
456 char **argv, **argi, **argj;
457
458 int single_file;
459 int use_y4m = 1;
460 int opt_yv12 = 0;
461 int opt_i420 = 0;
462 int opt_raw = 0;
463 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH, { 1 } };
464 unsigned int fixed_output_bit_depth = 0;
465 unsigned int tile_mode = 0;
466 unsigned int is_annexb = 0;
467 int tile_row = -1;
468 int tile_col = -1;
469 int frames_corrupted = 0;
470 int dec_flags = 0;
471 int do_scale = 0;
472 int operating_point = 0;
473 int output_all_layers = 0;
474 int skip_film_grain = 0;
475 aom_image_t *scaled_img = NULL;
476 aom_image_t *img_shifted = NULL;
477 int frame_avail, got_data, flush_decoder = 0;
478 int num_external_frame_buffers = 0;
479 struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
480
481 const char *outfile_pattern = NULL;
482 char outfile_name[PATH_MAX] = { 0 };
483 FILE *outfile = NULL;
484
485 FILE *framestats_file = NULL;
486
487 MD5Context md5_ctx;
488 unsigned char md5_digest[16];
489
490 struct AvxDecInputContext input = { NULL, NULL, NULL };
491 struct AvxInputContext aom_input_ctx;
492 memset(&aom_input_ctx, 0, sizeof(aom_input_ctx));
493 #if CONFIG_WEBM_IO
494 struct WebmInputContext webm_ctx;
495 memset(&webm_ctx, 0, sizeof(webm_ctx));
496 input.webm_ctx = &webm_ctx;
497 #endif
498 struct ObuDecInputContext obu_ctx = { NULL, NULL, 0, 0, 0 };
499
500 obu_ctx.avx_ctx = &aom_input_ctx;
501 input.obu_ctx = &obu_ctx;
502 input.aom_input_ctx = &aom_input_ctx;
503
504 /* Parse command line */
505 exec_name = argv_[0];
506 argv = argv_dup(argc - 1, argv_ + 1);
507
508 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
509 memset(&arg, 0, sizeof(arg));
510 arg.argv_step = 1;
511
512 if (arg_match(&arg, &help, argi)) {
513 show_help(stdout, 0);
514 exit(EXIT_SUCCESS);
515 } else if (arg_match(&arg, &codecarg, argi)) {
516 interface = get_aom_decoder_by_name(arg.val);
517 if (!interface)
518 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
519 } else if (arg_match(&arg, &looparg, argi)) {
520 // no-op
521 } else if (arg_match(&arg, &outputfile, argi)) {
522 outfile_pattern = arg.val;
523 } else if (arg_match(&arg, &use_yv12, argi)) {
524 use_y4m = 0;
525 flipuv = 1;
526 opt_yv12 = 1;
527 opt_i420 = 0;
528 opt_raw = 0;
529 } else if (arg_match(&arg, &use_i420, argi)) {
530 use_y4m = 0;
531 flipuv = 0;
532 opt_yv12 = 0;
533 opt_i420 = 1;
534 opt_raw = 0;
535 } else if (arg_match(&arg, &rawvideo, argi)) {
536 use_y4m = 0;
537 opt_yv12 = 0;
538 opt_i420 = 0;
539 opt_raw = 1;
540 } else if (arg_match(&arg, &flipuvarg, argi)) {
541 flipuv = 1;
542 } else if (arg_match(&arg, &noblitarg, argi)) {
543 noblit = 1;
544 } else if (arg_match(&arg, &progressarg, argi)) {
545 progress = 1;
546 } else if (arg_match(&arg, &limitarg, argi)) {
547 stop_after = arg_parse_uint(&arg);
548 } else if (arg_match(&arg, &skiparg, argi)) {
549 arg_skip = arg_parse_uint(&arg);
550 } else if (arg_match(&arg, &postprocarg, argi)) {
551 postproc = 1;
552 } else if (arg_match(&arg, &md5arg, argi)) {
553 do_md5 = 1;
554 } else if (arg_match(&arg, &framestatsarg, argi)) {
555 framestats_file = fopen(arg.val, "w");
556 if (!framestats_file) {
557 die("Error: Could not open --framestats file (%s) for writing.\n",
558 arg.val);
559 }
560 } else if (arg_match(&arg, &summaryarg, argi)) {
561 summary = 1;
562 } else if (arg_match(&arg, &threadsarg, argi)) {
563 cfg.threads = arg_parse_uint(&arg);
564 } else if (arg_match(&arg, &verbosearg, argi)) {
565 quiet = 0;
566 } else if (arg_match(&arg, &scalearg, argi)) {
567 do_scale = 1;
568 } else if (arg_match(&arg, &fb_arg, argi)) {
569 num_external_frame_buffers = arg_parse_uint(&arg);
570 } else if (arg_match(&arg, &continuearg, argi)) {
571 keep_going = 1;
572 } else if (arg_match(&arg, &outbitdeptharg, argi)) {
573 fixed_output_bit_depth = arg_parse_uint(&arg);
574 } else if (arg_match(&arg, &tilem, argi)) {
575 tile_mode = arg_parse_int(&arg);
576 } else if (arg_match(&arg, &isannexb, argi)) {
577 is_annexb = 1;
578 input.obu_ctx->is_annexb = 1;
579 } else if (arg_match(&arg, &tiler, argi)) {
580 tile_row = arg_parse_int(&arg);
581 } else if (arg_match(&arg, &tilec, argi)) {
582 tile_col = arg_parse_int(&arg);
583 } else if (arg_match(&arg, &oppointarg, argi)) {
584 operating_point = arg_parse_int(&arg);
585 } else if (arg_match(&arg, &outallarg, argi)) {
586 output_all_layers = 1;
587 } else if (arg_match(&arg, &skipfilmgrain, argi)) {
588 skip_film_grain = 1;
589 } else {
590 argj++;
591 }
592 }
593
594 /* Check for unrecognized options */
595 for (argi = argv; *argi; argi++)
596 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
597 die("Error: Unrecognized option %s\n", *argi);
598
599 /* Handle non-option arguments */
600 fn = argv[0];
601
602 if (!fn) {
603 free(argv);
604 fprintf(stderr, "No input file specified!\n");
605 usage_exit();
606 }
607 /* Open file */
608 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
609
610 if (!infile) {
611 fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
612 }
613 #if CONFIG_OS_SUPPORT
614 /* Make sure we don't dump to the terminal, unless forced to with -o - */
615 if (!outfile_pattern && isatty(STDOUT_FILENO) && !do_md5 && !noblit) {
616 fprintf(stderr,
617 "Not dumping raw video to your terminal. Use '-o -' to "
618 "override.\n");
619 return EXIT_FAILURE;
620 }
621 #endif
622 input.aom_input_ctx->filename = fn;
623 input.aom_input_ctx->file = infile;
624 if (file_is_ivf(input.aom_input_ctx))
625 input.aom_input_ctx->file_type = FILE_TYPE_IVF;
626 #if CONFIG_WEBM_IO
627 else if (file_is_webm(input.webm_ctx, input.aom_input_ctx))
628 input.aom_input_ctx->file_type = FILE_TYPE_WEBM;
629 #endif
630 else if (file_is_obu(&obu_ctx))
631 input.aom_input_ctx->file_type = FILE_TYPE_OBU;
632 else if (file_is_raw(input.aom_input_ctx))
633 input.aom_input_ctx->file_type = FILE_TYPE_RAW;
634 else {
635 fprintf(stderr, "Unrecognized input file type.\n");
636 #if !CONFIG_WEBM_IO
637 fprintf(stderr, "aomdec was built without WebM container support.\n");
638 #endif
639 return EXIT_FAILURE;
640 }
641
642 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
643 single_file = is_single_file(outfile_pattern);
644
645 if (!noblit && single_file) {
646 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
647 aom_input_ctx.width, aom_input_ctx.height, 0);
648 if (do_md5)
649 MD5Init(&md5_ctx);
650 else
651 outfile = open_outfile(outfile_name);
652 }
653
654 if (use_y4m && !noblit) {
655 if (!single_file) {
656 fprintf(stderr,
657 "YUV4MPEG2 not supported with output patterns,"
658 " try --i420 or --yv12 or --rawvideo.\n");
659 return EXIT_FAILURE;
660 }
661
662 #if CONFIG_WEBM_IO
663 if (aom_input_ctx.file_type == FILE_TYPE_WEBM) {
664 if (webm_guess_framerate(input.webm_ctx, input.aom_input_ctx)) {
665 fprintf(stderr,
666 "Failed to guess framerate -- error parsing "
667 "webm file?\n");
668 return EXIT_FAILURE;
669 }
670 }
671 #endif
672 }
673
674 fourcc_interface = get_aom_decoder_by_fourcc(aom_input_ctx.fourcc);
675 if (interface && fourcc_interface && interface != fourcc_interface)
676 warn("Header indicates codec: %s\n", fourcc_interface->name);
677 else
678 interface = fourcc_interface;
679
680 if (!interface) interface = get_aom_decoder_by_index(0);
681
682 dec_flags = (postproc ? AOM_CODEC_USE_POSTPROC : 0);
683 if (aom_codec_dec_init(&decoder, interface->codec_interface(), &cfg,
684 dec_flags)) {
685 fprintf(stderr, "Failed to initialize decoder: %s\n",
686 aom_codec_error(&decoder));
687 goto fail2;
688 }
689
690 if (!quiet) fprintf(stderr, "%s\n", decoder.name);
691
692 #if CONFIG_AV1_DECODER
693 if (aom_codec_control(&decoder, AV1_SET_TILE_MODE, tile_mode)) {
694 fprintf(stderr, "Failed to set decode_tile_mode: %s\n",
695 aom_codec_error(&decoder));
696 goto fail;
697 }
698
699 if (aom_codec_control(&decoder, AV1D_SET_IS_ANNEXB, is_annexb)) {
700 fprintf(stderr, "Failed to set is_annexb: %s\n", aom_codec_error(&decoder));
701 goto fail;
702 }
703
704 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_ROW, tile_row)) {
705 fprintf(stderr, "Failed to set decode_tile_row: %s\n",
706 aom_codec_error(&decoder));
707 goto fail;
708 }
709
710 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_COL, tile_col)) {
711 fprintf(stderr, "Failed to set decode_tile_col: %s\n",
712 aom_codec_error(&decoder));
713 goto fail;
714 }
715
716 if (aom_codec_control(&decoder, AV1D_SET_OPERATING_POINT, operating_point)) {
717 fprintf(stderr, "Failed to set operating_point: %s\n",
718 aom_codec_error(&decoder));
719 goto fail;
720 }
721
722 if (aom_codec_control(&decoder, AV1D_SET_OUTPUT_ALL_LAYERS,
723 output_all_layers)) {
724 fprintf(stderr, "Failed to set output_all_layers: %s\n",
725 aom_codec_error(&decoder));
726 goto fail;
727 }
728 #endif
729
730 if (aom_codec_control(&decoder, AV1D_SET_SKIP_FILM_GRAIN, skip_film_grain)) {
731 fprintf(stderr, "Failed to set skip_film_grain: %s\n",
732 aom_codec_error(&decoder));
733 goto fail;
734 }
735
736 if (arg_skip) fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
737 while (arg_skip) {
738 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) break;
739 arg_skip--;
740 }
741
742 if (num_external_frame_buffers > 0) {
743 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
744 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
745 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
746 if (aom_codec_set_frame_buffer_functions(&decoder, get_av1_frame_buffer,
747 release_av1_frame_buffer,
748 &ext_fb_list)) {
749 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
750 aom_codec_error(&decoder));
751 goto fail;
752 }
753 }
754
755 frame_avail = 1;
756 got_data = 0;
757
758 if (framestats_file) fprintf(framestats_file, "bytes,qp\r\n");
759
760 /* Decode file */
761 while (frame_avail || got_data) {
762 aom_codec_iter_t iter = NULL;
763 aom_image_t *img;
764 struct aom_usec_timer timer;
765 int corrupted = 0;
766
767 frame_avail = 0;
768 if (!stop_after || frame_in < stop_after) {
769 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
770 frame_avail = 1;
771 frame_in++;
772
773 aom_usec_timer_start(&timer);
774
775 if (aom_codec_decode(&decoder, buf, bytes_in_buffer, NULL)) {
776 const char *detail = aom_codec_error_detail(&decoder);
777 warn("Failed to decode frame %d: %s", frame_in,
778 aom_codec_error(&decoder));
779
780 if (detail) warn("Additional information: %s", detail);
781 if (!keep_going) goto fail;
782 }
783
784 if (framestats_file) {
785 int qp;
786 if (aom_codec_control(&decoder, AOMD_GET_LAST_QUANTIZER, &qp)) {
787 warn("Failed AOMD_GET_LAST_QUANTIZER: %s",
788 aom_codec_error(&decoder));
789 if (!keep_going) goto fail;
790 }
791 fprintf(framestats_file, "%d,%d\r\n", (int)bytes_in_buffer, qp);
792 }
793
794 aom_usec_timer_mark(&timer);
795 dx_time += aom_usec_timer_elapsed(&timer);
796 } else {
797 flush_decoder = 1;
798 }
799 } else {
800 flush_decoder = 1;
801 }
802
803 aom_usec_timer_start(&timer);
804
805 if (flush_decoder) {
806 // Flush the decoder in frame parallel decode.
807 if (aom_codec_decode(&decoder, NULL, 0, NULL)) {
808 warn("Failed to flush decoder: %s", aom_codec_error(&decoder));
809 }
810 }
811
812 aom_usec_timer_mark(&timer);
813 dx_time += aom_usec_timer_elapsed(&timer);
814
815 got_data = 0;
816 while ((img = aom_codec_get_frame(&decoder, &iter))) {
817 ++frame_out;
818 got_data = 1;
819
820 if (aom_codec_control(&decoder, AOMD_GET_FRAME_CORRUPTED, &corrupted)) {
821 warn("Failed AOM_GET_FRAME_CORRUPTED: %s", aom_codec_error(&decoder));
822 if (!keep_going) goto fail;
823 }
824 frames_corrupted += corrupted;
825
826 if (progress) show_progress(frame_in, frame_out, dx_time);
827
828 if (!noblit) {
829 const int PLANES_YUV[] = { AOM_PLANE_Y, AOM_PLANE_U, AOM_PLANE_V };
830 const int PLANES_YVU[] = { AOM_PLANE_Y, AOM_PLANE_V, AOM_PLANE_U };
831 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
832
833 if (do_scale) {
834 if (frame_out == 1) {
835 // If the output frames are to be scaled to a fixed display size
836 // then use the width and height specified in the container. If
837 // either of these is set to 0, use the display size set in the
838 // first frame header. If that is unavailable, use the raw decoded
839 // size of the first decoded frame.
840 int render_width = aom_input_ctx.width;
841 int render_height = aom_input_ctx.height;
842 if (!render_width || !render_height) {
843 int render_size[2];
844 if (aom_codec_control(&decoder, AV1D_GET_DISPLAY_SIZE,
845 render_size)) {
846 // As last resort use size of first frame as display size.
847 render_width = img->d_w;
848 render_height = img->d_h;
849 } else {
850 render_width = render_size[0];
851 render_height = render_size[1];
852 }
853 }
854 scaled_img =
855 aom_img_alloc(NULL, img->fmt, render_width, render_height, 16);
856 scaled_img->bit_depth = img->bit_depth;
857 scaled_img->monochrome = img->monochrome;
858 }
859
860 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
861 #if CONFIG_LIBYUV
862 libyuv_scale(img, scaled_img, kFilterBox);
863 img = scaled_img;
864 #else
865 fprintf(
866 stderr,
867 "Failed to scale output frame: %s.\n"
868 "libyuv is required for scaling but is currently disabled.\n"
869 "Be sure to specify -DCONFIG_LIBYUV=1 when running cmake.\n",
870 aom_codec_error(&decoder));
871 goto fail;
872 #endif
873 }
874 }
875 // Default to codec bit depth if output bit depth not set
876 unsigned int output_bit_depth;
877 if (!fixed_output_bit_depth && single_file && !do_md5) {
878 output_bit_depth = img->bit_depth;
879 } else {
880 output_bit_depth = fixed_output_bit_depth;
881 }
882 // Shift up or down if necessary
883 if (output_bit_depth != 0)
884 aom_shift_img(output_bit_depth, &img, &img_shifted);
885
886 aom_input_ctx.width = img->d_w;
887 aom_input_ctx.height = img->d_h;
888
889 int num_planes = (opt_raw && img->monochrome) ? 1 : 3;
890 if (single_file) {
891 if (use_y4m) {
892 char y4m_buf[Y4M_BUFFER_SIZE] = { 0 };
893 size_t len = 0;
894 if (frame_out == 1) {
895 // Y4M file header
896 len = y4m_write_file_header(
897 y4m_buf, sizeof(y4m_buf), aom_input_ctx.width,
898 aom_input_ctx.height, &aom_input_ctx.framerate,
899 img->monochrome, img->fmt, img->bit_depth);
900 if (do_md5) {
901 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
902 } else {
903 fputs(y4m_buf, outfile);
904 }
905 }
906
907 // Y4M frame header
908 len = y4m_write_frame_header(y4m_buf, sizeof(y4m_buf));
909 if (do_md5) {
910 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
911 y4m_update_image_md5(img, planes, &md5_ctx);
912 } else {
913 fputs(y4m_buf, outfile);
914 y4m_write_image_file(img, planes, outfile);
915 }
916 } else {
917 if (frame_out == 1) {
918 // Check if --yv12 or --i420 options are consistent with the
919 // bit-stream decoded
920 if (opt_i420) {
921 if (img->fmt != AOM_IMG_FMT_I420 &&
922 img->fmt != AOM_IMG_FMT_I42016) {
923 fprintf(stderr,
924 "Cannot produce i420 output for bit-stream.\n");
925 goto fail;
926 }
927 }
928 if (opt_yv12) {
929 if ((img->fmt != AOM_IMG_FMT_I420 &&
930 img->fmt != AOM_IMG_FMT_YV12) ||
931 img->bit_depth != 8) {
932 fprintf(stderr,
933 "Cannot produce yv12 output for bit-stream.\n");
934 goto fail;
935 }
936 }
937 }
938 if (do_md5) {
939 raw_update_image_md5(img, planes, num_planes, &md5_ctx);
940 } else {
941 raw_write_image_file(img, planes, num_planes, outfile);
942 }
943 }
944 } else {
945 generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
946 img->d_h, frame_in);
947 if (do_md5) {
948 MD5Init(&md5_ctx);
949 if (use_y4m) {
950 y4m_update_image_md5(img, planes, &md5_ctx);
951 } else {
952 raw_update_image_md5(img, planes, num_planes, &md5_ctx);
953 }
954 MD5Final(md5_digest, &md5_ctx);
955 print_md5(md5_digest, outfile_name);
956 } else {
957 outfile = open_outfile(outfile_name);
958 if (use_y4m) {
959 y4m_write_image_file(img, planes, outfile);
960 } else {
961 raw_write_image_file(img, planes, num_planes, outfile);
962 }
963 fclose(outfile);
964 }
965 }
966 }
967 }
968 }
969
970 if (summary || progress) {
971 show_progress(frame_in, frame_out, dx_time);
972 fprintf(stderr, "\n");
973 }
974
975 if (frames_corrupted) {
976 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
977 } else {
978 ret = EXIT_SUCCESS;
979 }
980
981 fail:
982
983 if (aom_codec_destroy(&decoder)) {
984 fprintf(stderr, "Failed to destroy decoder: %s\n",
985 aom_codec_error(&decoder));
986 }
987
988 fail2:
989
990 if (!noblit && single_file) {
991 if (do_md5) {
992 MD5Final(md5_digest, &md5_ctx);
993 print_md5(md5_digest, outfile_name);
994 } else {
995 fclose(outfile);
996 }
997 }
998
999 #if CONFIG_WEBM_IO
1000 if (input.aom_input_ctx->file_type == FILE_TYPE_WEBM)
1001 webm_free(input.webm_ctx);
1002 #endif
1003 if (input.aom_input_ctx->file_type == FILE_TYPE_OBU)
1004 obudec_free(input.obu_ctx);
1005
1006 if (input.aom_input_ctx->file_type != FILE_TYPE_WEBM) free(buf);
1007
1008 if (scaled_img) aom_img_free(scaled_img);
1009 if (img_shifted) aom_img_free(img_shifted);
1010
1011 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
1012 free(ext_fb_list.ext_fb[i].data);
1013 }
1014 free(ext_fb_list.ext_fb);
1015
1016 fclose(infile);
1017 if (framestats_file) fclose(framestats_file);
1018
1019 free(argv);
1020
1021 return ret;
1022 }
1023
1024 int main(int argc, const char **argv_) {
1025 unsigned int loops = 1, i;
1026 char **argv, **argi, **argj;
1027 struct arg arg;
1028 int error = 0;
1029
1030 argv = argv_dup(argc - 1, argv_ + 1);
1031 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1032 memset(&arg, 0, sizeof(arg));
1033 arg.argv_step = 1;
1034
1035 if (arg_match(&arg, &looparg, argi)) {
1036 loops = arg_parse_uint(&arg);
1037 break;
1038 }
1039 }
1040 free(argv);
1041 for (i = 0; !error && i < loops; i++) error = main_loop(argc, argv_);
1042 return error;
1043 }
AOM_PLANE_U
#define AOM_PLANE_U
Definition: aom_image.h:170
aom_image::d_h
unsigned int d_h
Definition: aom_image.h:157
AOM_PLANE_V
#define AOM_PLANE_V
Definition: aom_image.h:171
AV1D_SET_SKIP_FILM_GRAIN
Definition: aomdx.h:231
aom_codec_peek_stream_info
aom_codec_err_t aom_codec_peek_stream_info(aom_codec_iface_t *iface, const uint8_t *data, size_t data_sz, aom_codec_stream_info_t *si)
Parse stream info from a buffer.
AV1D_SET_OPERATING_POINT
Definition: aomdx.h:208
aom_codec_get_frame
aom_image_t * aom_codec_get_frame(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Decoded frames iterator.
aom_codec_ctx::name
const char * name
Definition: aom_codec.h:205
AV1D_SET_IS_ANNEXB
Definition: aomdx.h:200
aom_codec_frame_buffer::data
uint8_t * data
Definition: aom_frame_buffer.h:41
aom_codec_ctx
Codec context structure.
Definition: aom_codec.h:204
aom_image::stride
int stride[4]
Definition: aom_image.h:174
aom_codec_frame_buffer
External frame buffer.
Definition: aom_frame_buffer.h:40
aom_decoder.h
Describes the decoder algorithm interface to applications.
aom_image::monochrome
int monochrome
Definition: aom_image.h:146
aom_image
Image Descriptor.
Definition: aom_image.h:141
aom_img_alloc
aom_image_t * aom_img_alloc(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
aom_codec_decode
aom_codec_err_t aom_codec_decode(aom_codec_ctx_t *ctx, const uint8_t *data, size_t data_sz, void *user_priv)
Decode data.
AOMD_GET_FRAME_CORRUPTED
Definition: aomdx.h:102
aom_codec_dec_init
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition: aom_decoder.h:142
aom_codec_iface_name
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
aom_codec_destroy
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
aom_codec_frame_buffer::priv
void * priv
Definition: aom_frame_buffer.h:43
aom_img_free
void aom_img_free(aom_image_t *img)
Close an image descriptor.
aom_codec_stream_info::h
unsigned int h
Definition: aom_decoder.h:85
aom_codec_dec_cfg
Initialization Configurations.
Definition: aom_decoder.h:103
aom_codec_control
#define aom_codec_control(ctx, id, data)
aom_codec_control wrapper macro
Definition: aom_codec.h:423
AOMD_GET_LAST_QUANTIZER
Definition: aomdx.h:166
aom_codec_stream_info
Stream properties.
Definition: aom_decoder.h:83
aom_codec_stream_info::w
unsigned int w
Definition: aom_decoder.h:84
AV1_SET_DECODE_TILE_ROW
Definition: aomdx.h:174
aom_codec_iter_t
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
aom_codec_frame_buffer::size
size_t size
Definition: aom_frame_buffer.h:42
AV1_SET_TILE_MODE
Definition: aomdx.h:180
AV1D_SET_OUTPUT_ALL_LAYERS
Definition: aomdx.h:219
aom_codec_error
const char * aom_codec_error(aom_codec_ctx_t *ctx)
Retrieve error synopsis for codec context.
AOM_IMG_FMT_I42016
Definition: aom_image.h:52
aomdx.h
Provides definitions for using AOM or AV1 within the aom Decoder interface.
aom_codec_set_frame_buffer_functions
aom_codec_err_t aom_codec_set_frame_buffer_functions(aom_codec_ctx_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get, aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv)
Pass in external frame buffers for the decoder to use.
AOM_IMG_FMT_I420
Definition: aom_image.h:45
aom_codec_error_detail
const char * aom_codec_error_detail(aom_codec_ctx_t *ctx)
Retrieve detailed error information for codec context.
aom_codec_dec_cfg::threads
unsigned int threads
Definition: aom_decoder.h:104
aom_image::bit_depth
unsigned int bit_depth
Definition: aom_image.h:153
aom_image::planes
unsigned char * planes[4]
Definition: aom_image.h:173
aom_image::d_w
unsigned int d_w
Definition: aom_image.h:156
AV1D_GET_DISPLAY_SIZE
Definition: aomdx.h:117
AOM_IMG_FMT_YV12
Definition: aom_image.h:43
aom_image::fmt
aom_img_fmt_t fmt
Definition: aom_image.h:142
AOM_CODEC_USE_POSTPROC
#define AOM_CODEC_USE_POSTPROC
Definition: aom_decoder.h:73
AOM_PLANE_Y
#define AOM_PLANE_Y
Definition: aom_image.h:169