Modify test.mk
[aom.git] / aomdec.c
blob402c4e2b8a84b25cc051b49269f4da526531f9f7
1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
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.
12 #include <assert.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdarg.h>
16 #include <string.h>
17 #include <limits.h>
19 #include "./aom_config.h"
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
29 #if CONFIG_LIBYUV
30 #include "third_party/libyuv/include/libyuv/scale.h"
31 #endif
33 #include "./args.h"
34 #include "./ivfdec.h"
36 #include "aom/aom_decoder.h"
37 #include "aom_ports/mem_ops.h"
38 #include "aom_ports/aom_timer.h"
40 #if CONFIG_AV1_DECODER
41 #include "aom/aomdx.h"
42 #endif
44 #include "./md5_utils.h"
46 #include "./tools_common.h"
47 #if CONFIG_WEBM_IO
48 #include "./webmdec.h"
49 #endif
50 #include "./y4menc.h"
52 static const char *exec_name;
54 struct AvxDecInputContext {
55 struct AvxInputContext *aom_input_ctx;
56 struct WebmInputContext *webm_ctx;
59 static const arg_def_t looparg =
60 ARG_DEF(NULL, "loops", 1, "Number of times to decode the file");
61 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
62 static const arg_def_t use_yv12 =
63 ARG_DEF(NULL, "yv12", 0, "Output raw YV12 frames");
64 static const arg_def_t use_i420 =
65 ARG_DEF(NULL, "i420", 0, "Output raw I420 frames");
66 static const arg_def_t flipuvarg =
67 ARG_DEF(NULL, "flipuv", 0, "Flip the chroma planes in the output");
68 static const arg_def_t rawvideo =
69 ARG_DEF(NULL, "rawvideo", 0, "Output raw YUV frames");
70 static const arg_def_t noblitarg =
71 ARG_DEF(NULL, "noblit", 0, "Don't process the decoded frames");
72 static const arg_def_t progressarg =
73 ARG_DEF(NULL, "progress", 0, "Show progress after each frame decodes");
74 static const arg_def_t limitarg =
75 ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
76 static const arg_def_t skiparg =
77 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
78 static const arg_def_t postprocarg =
79 ARG_DEF(NULL, "postproc", 0, "Postprocess decoded frames");
80 static const arg_def_t summaryarg =
81 ARG_DEF(NULL, "summary", 0, "Show timing summary");
82 static const arg_def_t outputfile =
83 ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
84 static const arg_def_t threadsarg =
85 ARG_DEF("t", "threads", 1, "Max threads to use");
86 static const arg_def_t frameparallelarg =
87 ARG_DEF(NULL, "frame-parallel", 0, "Frame parallel decode");
88 static const arg_def_t verbosearg =
89 ARG_DEF("v", "verbose", 0, "Show version string");
90 static const arg_def_t error_concealment =
91 ARG_DEF(NULL, "error-concealment", 0, "Enable decoder error-concealment");
92 static const arg_def_t scalearg =
93 ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
94 static const arg_def_t continuearg =
95 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
96 static const arg_def_t fb_arg =
97 ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
98 static const arg_def_t md5arg =
99 ARG_DEF(NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
100 #if CONFIG_AOM_HIGHBITDEPTH
101 static const arg_def_t outbitdeptharg =
102 ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
103 #endif
104 #if CONFIG_EXT_TILE
105 static const arg_def_t tiler = ARG_DEF(NULL, "tile-row", 1,
106 "Row index of tile to decode "
107 "(-1 for all rows)");
108 static const arg_def_t tilec = ARG_DEF(NULL, "tile-column", 1,
109 "Column index of tile to decode "
110 "(-1 for all columns)");
111 #endif // CONFIG_EXT_TILE
113 static const arg_def_t *all_args[] = { &codecarg,
114 &use_yv12,
115 &use_i420,
116 &flipuvarg,
117 &rawvideo,
118 &noblitarg,
119 &progressarg,
120 &limitarg,
121 &skiparg,
122 &postprocarg,
123 &summaryarg,
124 &outputfile,
125 &threadsarg,
126 &frameparallelarg,
127 &verbosearg,
128 &scalearg,
129 &fb_arg,
130 &md5arg,
131 &error_concealment,
132 &continuearg,
133 #if CONFIG_AOM_HIGHBITDEPTH
134 &outbitdeptharg,
135 #endif
136 #if CONFIG_EXT_TILE
137 &tiler,
138 &tilec,
139 #endif // CONFIG_EXT_TILE
140 NULL };
142 #if CONFIG_LIBYUV
143 static INLINE int libyuv_scale(aom_image_t *src, aom_image_t *dst,
144 FilterModeEnum mode) {
145 #if CONFIG_AOM_HIGHBITDEPTH
146 if (src->fmt == AOM_IMG_FMT_I42016) {
147 assert(dst->fmt == AOM_IMG_FMT_I42016);
148 return I420Scale_16(
149 (uint16_t *)src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y] / 2,
150 (uint16_t *)src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U] / 2,
151 (uint16_t *)src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V] / 2,
152 src->d_w, src->d_h, (uint16_t *)dst->planes[AOM_PLANE_Y],
153 dst->stride[AOM_PLANE_Y] / 2, (uint16_t *)dst->planes[AOM_PLANE_U],
154 dst->stride[AOM_PLANE_U] / 2, (uint16_t *)dst->planes[AOM_PLANE_V],
155 dst->stride[AOM_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
157 #endif
158 assert(src->fmt == AOM_IMG_FMT_I420);
159 assert(dst->fmt == AOM_IMG_FMT_I420);
160 return I420Scale(src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y],
161 src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U],
162 src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V], src->d_w,
163 src->d_h, dst->planes[AOM_PLANE_Y], dst->stride[AOM_PLANE_Y],
164 dst->planes[AOM_PLANE_U], dst->stride[AOM_PLANE_U],
165 dst->planes[AOM_PLANE_V], dst->stride[AOM_PLANE_V], dst->d_w,
166 dst->d_h, mode);
168 #endif
170 void usage_exit(void) {
171 int i;
173 fprintf(stderr,
174 "Usage: %s <options> filename\n\n"
175 "Options:\n",
176 exec_name);
177 arg_show_usage(stderr, all_args);
178 fprintf(stderr,
179 "\nOutput File Patterns:\n\n"
180 " The -o argument specifies the name of the file(s) to "
181 "write to. If the\n argument does not include any escape "
182 "characters, the output will be\n written to a single file. "
183 "Otherwise, the filename will be calculated by\n expanding "
184 "the following escape characters:\n");
185 fprintf(stderr,
186 "\n\t%%w - Frame width"
187 "\n\t%%h - Frame height"
188 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
189 "\n\n Pattern arguments are only supported in conjunction "
190 "with the --yv12 and\n --i420 options. If the -o option is "
191 "not specified, the output will be\n directed to stdout.\n");
192 fprintf(stderr, "\nIncluded decoders:\n\n");
194 for (i = 0; i < get_aom_decoder_count(); ++i) {
195 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
196 fprintf(stderr, " %-6s - %s\n", decoder->name,
197 aom_codec_iface_name(decoder->codec_interface()));
200 exit(EXIT_FAILURE);
203 static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
204 size_t *buffer_size) {
205 char raw_hdr[RAW_FRAME_HDR_SZ];
206 size_t frame_size = 0;
208 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
209 if (!feof(infile)) warn("Failed to read RAW frame size\n");
210 } else {
211 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
212 const size_t kFrameTooSmallThreshold = 256 * 1024;
213 frame_size = mem_get_le32(raw_hdr);
215 if (frame_size > kCorruptFrameThreshold) {
216 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
217 frame_size = 0;
220 if (frame_size < kFrameTooSmallThreshold) {
221 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
222 (unsigned int)frame_size);
225 if (frame_size > *buffer_size) {
226 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
227 if (new_buf) {
228 *buffer = new_buf;
229 *buffer_size = 2 * frame_size;
230 } else {
231 warn("Failed to allocate compressed data buffer\n");
232 frame_size = 0;
237 if (!feof(infile)) {
238 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
239 warn("Failed to read full frame\n");
240 return 1;
242 *bytes_read = frame_size;
245 return 0;
248 static int read_frame(struct AvxDecInputContext *input, uint8_t **buf,
249 size_t *bytes_in_buffer, size_t *buffer_size) {
250 switch (input->aom_input_ctx->file_type) {
251 #if CONFIG_WEBM_IO
252 case FILE_TYPE_WEBM:
253 return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer);
254 #endif
255 case FILE_TYPE_RAW:
256 return raw_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
257 buffer_size);
258 case FILE_TYPE_IVF:
259 return ivf_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
260 buffer_size);
261 default: return 1;
265 static void update_image_md5(const aom_image_t *img, const int planes[3],
266 MD5Context *md5) {
267 int i, y;
269 for (i = 0; i < 3; ++i) {
270 const int plane = planes[i];
271 const unsigned char *buf = img->planes[plane];
272 const int stride = img->stride[plane];
273 const int w = aom_img_plane_width(img, plane) *
274 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
275 const int h = aom_img_plane_height(img, plane);
277 for (y = 0; y < h; ++y) {
278 MD5Update(md5, buf, w);
279 buf += stride;
284 static void write_image_file(const aom_image_t *img, const int planes[3],
285 FILE *file) {
286 int i, y;
287 #if CONFIG_AOM_HIGHBITDEPTH
288 const int bytes_per_sample = ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
289 #else
290 const int bytes_per_sample = 1;
291 #endif
293 for (i = 0; i < 3; ++i) {
294 const int plane = planes[i];
295 const unsigned char *buf = img->planes[plane];
296 const int stride = img->stride[plane];
297 const int w = aom_img_plane_width(img, plane);
298 const int h = aom_img_plane_height(img, plane);
300 for (y = 0; y < h; ++y) {
301 fwrite(buf, bytes_per_sample, w, file);
302 buf += stride;
307 static int file_is_raw(struct AvxInputContext *input) {
308 uint8_t buf[32];
309 int is_raw = 0;
310 aom_codec_stream_info_t si;
312 si.sz = sizeof(si);
314 if (fread(buf, 1, 32, input->file) == 32) {
315 int i;
317 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
318 for (i = 0; i < get_aom_decoder_count(); ++i) {
319 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
320 if (!aom_codec_peek_stream_info(decoder->codec_interface(), buf + 4,
321 32 - 4, &si)) {
322 is_raw = 1;
323 input->fourcc = decoder->fourcc;
324 input->width = si.w;
325 input->height = si.h;
326 input->framerate.numerator = 30;
327 input->framerate.denominator = 1;
328 break;
334 rewind(input->file);
335 return is_raw;
338 static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
339 fprintf(stderr,
340 "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
341 frame_in, frame_out, dx_time,
342 (double)frame_out * 1000000.0 / (double)dx_time);
345 struct ExternalFrameBuffer {
346 uint8_t *data;
347 size_t size;
348 int in_use;
351 struct ExternalFrameBufferList {
352 int num_external_frame_buffers;
353 struct ExternalFrameBuffer *ext_fb;
356 // Callback used by libaom to request an external frame buffer. |cb_priv|
357 // Application private data passed into the set function. |min_size| is the
358 // minimum size in bytes needed to decode the next frame. |fb| pointer to the
359 // frame buffer.
360 static int get_av1_frame_buffer(void *cb_priv, size_t min_size,
361 aom_codec_frame_buffer_t *fb) {
362 int i;
363 struct ExternalFrameBufferList *const ext_fb_list =
364 (struct ExternalFrameBufferList *)cb_priv;
365 if (ext_fb_list == NULL) return -1;
367 // Find a free frame buffer.
368 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
369 if (!ext_fb_list->ext_fb[i].in_use) break;
372 if (i == ext_fb_list->num_external_frame_buffers) return -1;
374 if (ext_fb_list->ext_fb[i].size < min_size) {
375 free(ext_fb_list->ext_fb[i].data);
376 ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
377 if (!ext_fb_list->ext_fb[i].data) return -1;
379 ext_fb_list->ext_fb[i].size = min_size;
382 fb->data = ext_fb_list->ext_fb[i].data;
383 fb->size = ext_fb_list->ext_fb[i].size;
384 ext_fb_list->ext_fb[i].in_use = 1;
386 // Set the frame buffer's private data to point at the external frame buffer.
387 fb->priv = &ext_fb_list->ext_fb[i];
388 return 0;
391 // Callback used by libaom when there are no references to the frame buffer.
392 // |cb_priv| user private data passed into the set function. |fb| pointer
393 // to the frame buffer.
394 static int release_av1_frame_buffer(void *cb_priv,
395 aom_codec_frame_buffer_t *fb) {
396 struct ExternalFrameBuffer *const ext_fb =
397 (struct ExternalFrameBuffer *)fb->priv;
398 (void)cb_priv;
399 ext_fb->in_use = 0;
400 return 0;
403 static void generate_filename(const char *pattern, char *out, size_t q_len,
404 unsigned int d_w, unsigned int d_h,
405 unsigned int frame_in) {
406 const char *p = pattern;
407 char *q = out;
409 do {
410 char *next_pat = strchr(p, '%');
412 if (p == next_pat) {
413 size_t pat_len;
415 /* parse the pattern */
416 q[q_len - 1] = '\0';
417 switch (p[1]) {
418 case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
419 case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
420 case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
421 case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
422 case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
423 case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
424 case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
425 case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
426 case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
427 case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
428 case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
429 default: die("Unrecognized pattern %%%c\n", p[1]); break;
432 pat_len = strlen(q);
433 if (pat_len >= q_len - 1) die("Output filename too long.\n");
434 q += pat_len;
435 p += 2;
436 q_len -= pat_len;
437 } else {
438 size_t copy_len;
440 /* copy the next segment */
441 if (!next_pat)
442 copy_len = strlen(p);
443 else
444 copy_len = next_pat - p;
446 if (copy_len >= q_len - 1) die("Output filename too long.\n");
448 memcpy(q, p, copy_len);
449 q[copy_len] = '\0';
450 q += copy_len;
451 p += copy_len;
452 q_len -= copy_len;
454 } while (*p);
457 static int is_single_file(const char *outfile_pattern) {
458 const char *p = outfile_pattern;
460 do {
461 p = strchr(p, '%');
462 if (p && p[1] >= '1' && p[1] <= '9')
463 return 0; // pattern contains sequence number, so it's not unique
464 if (p) p++;
465 } while (p);
467 return 1;
470 static void print_md5(unsigned char digest[16], const char *filename) {
471 int i;
473 for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
474 printf(" %s\n", filename);
477 static FILE *open_outfile(const char *name) {
478 if (strcmp("-", name) == 0) {
479 set_binary_mode(stdout);
480 return stdout;
481 } else {
482 FILE *file = fopen(name, "wb");
483 if (!file) fatal("Failed to open output file '%s'", name);
484 return file;
488 #if CONFIG_AOM_HIGHBITDEPTH
489 static int img_shifted_realloc_required(const aom_image_t *img,
490 const aom_image_t *shifted,
491 aom_img_fmt_t required_fmt) {
492 return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
493 required_fmt != shifted->fmt;
495 #endif
497 static int main_loop(int argc, const char **argv_) {
498 aom_codec_ctx_t decoder;
499 char *fn = NULL;
500 int i;
501 int ret = EXIT_FAILURE;
502 uint8_t *buf = NULL;
503 size_t bytes_in_buffer = 0, buffer_size = 0;
504 FILE *infile;
505 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
506 int do_md5 = 0, progress = 0, frame_parallel = 0;
507 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
508 int arg_skip = 0;
509 int ec_enabled = 0;
510 int keep_going = 0;
511 const AvxInterface *interface = NULL;
512 const AvxInterface *fourcc_interface = NULL;
513 uint64_t dx_time = 0;
514 struct arg arg;
515 char **argv, **argi, **argj;
517 int single_file;
518 int use_y4m = 1;
519 int opt_yv12 = 0;
520 int opt_i420 = 0;
521 aom_codec_dec_cfg_t cfg = { 0, 0, 0 };
522 #if CONFIG_AOM_HIGHBITDEPTH
523 unsigned int output_bit_depth = 0;
524 #endif
525 #if CONFIG_EXT_TILE
526 int tile_row = -1;
527 int tile_col = -1;
528 #endif // CONFIG_EXT_TILE
529 int frames_corrupted = 0;
530 int dec_flags = 0;
531 int do_scale = 0;
532 aom_image_t *scaled_img = NULL;
533 #if CONFIG_AOM_HIGHBITDEPTH
534 aom_image_t *img_shifted = NULL;
535 #endif
536 int frame_avail, got_data, flush_decoder = 0;
537 int num_external_frame_buffers = 0;
538 struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
540 const char *outfile_pattern = NULL;
541 char outfile_name[PATH_MAX] = { 0 };
542 FILE *outfile = NULL;
544 MD5Context md5_ctx;
545 unsigned char md5_digest[16];
547 struct AvxDecInputContext input = { NULL, NULL };
548 struct AvxInputContext aom_input_ctx;
549 #if CONFIG_WEBM_IO
550 struct WebmInputContext webm_ctx;
551 memset(&(webm_ctx), 0, sizeof(webm_ctx));
552 input.webm_ctx = &webm_ctx;
553 #endif
554 input.aom_input_ctx = &aom_input_ctx;
556 /* Parse command line */
557 exec_name = argv_[0];
558 argv = argv_dup(argc - 1, argv_ + 1);
560 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
561 memset(&arg, 0, sizeof(arg));
562 arg.argv_step = 1;
564 if (arg_match(&arg, &codecarg, argi)) {
565 interface = get_aom_decoder_by_name(arg.val);
566 if (!interface)
567 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
568 } else if (arg_match(&arg, &looparg, argi)) {
569 // no-op
570 } else if (arg_match(&arg, &outputfile, argi))
571 outfile_pattern = arg.val;
572 else if (arg_match(&arg, &use_yv12, argi)) {
573 use_y4m = 0;
574 flipuv = 1;
575 opt_yv12 = 1;
576 } else if (arg_match(&arg, &use_i420, argi)) {
577 use_y4m = 0;
578 flipuv = 0;
579 opt_i420 = 1;
580 } else if (arg_match(&arg, &rawvideo, argi)) {
581 use_y4m = 0;
582 } else if (arg_match(&arg, &flipuvarg, argi))
583 flipuv = 1;
584 else if (arg_match(&arg, &noblitarg, argi))
585 noblit = 1;
586 else if (arg_match(&arg, &progressarg, argi))
587 progress = 1;
588 else if (arg_match(&arg, &limitarg, argi))
589 stop_after = arg_parse_uint(&arg);
590 else if (arg_match(&arg, &skiparg, argi))
591 arg_skip = arg_parse_uint(&arg);
592 else if (arg_match(&arg, &postprocarg, argi))
593 postproc = 1;
594 else if (arg_match(&arg, &md5arg, argi))
595 do_md5 = 1;
596 else if (arg_match(&arg, &summaryarg, argi))
597 summary = 1;
598 else if (arg_match(&arg, &threadsarg, argi))
599 cfg.threads = arg_parse_uint(&arg);
600 #if CONFIG_AV1_DECODER
601 else if (arg_match(&arg, &frameparallelarg, argi))
602 frame_parallel = 1;
603 #endif
604 else if (arg_match(&arg, &verbosearg, argi))
605 quiet = 0;
606 else if (arg_match(&arg, &scalearg, argi))
607 do_scale = 1;
608 else if (arg_match(&arg, &fb_arg, argi))
609 num_external_frame_buffers = arg_parse_uint(&arg);
610 else if (arg_match(&arg, &continuearg, argi))
611 keep_going = 1;
612 #if CONFIG_AOM_HIGHBITDEPTH
613 else if (arg_match(&arg, &outbitdeptharg, argi)) {
614 output_bit_depth = arg_parse_uint(&arg);
616 #endif
617 #if CONFIG_EXT_TILE
618 else if (arg_match(&arg, &tiler, argi))
619 tile_row = arg_parse_int(&arg);
620 else if (arg_match(&arg, &tilec, argi))
621 tile_col = arg_parse_int(&arg);
622 #endif // CONFIG_EXT_TILE
623 else
624 argj++;
627 /* Check for unrecognized options */
628 for (argi = argv; *argi; argi++)
629 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
630 die("Error: Unrecognized option %s\n", *argi);
632 /* Handle non-option arguments */
633 fn = argv[0];
635 if (!fn) {
636 free(argv);
637 usage_exit();
639 /* Open file */
640 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
642 if (!infile) {
643 fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
645 #if CONFIG_OS_SUPPORT
646 /* Make sure we don't dump to the terminal, unless forced to with -o - */
647 if (!outfile_pattern && isatty(STDOUT_FILENO) && !do_md5 && !noblit) {
648 fprintf(stderr,
649 "Not dumping raw video to your terminal. Use '-o -' to "
650 "override.\n");
651 return EXIT_FAILURE;
653 #endif
654 input.aom_input_ctx->file = infile;
655 if (file_is_ivf(input.aom_input_ctx))
656 input.aom_input_ctx->file_type = FILE_TYPE_IVF;
657 #if CONFIG_WEBM_IO
658 else if (file_is_webm(input.webm_ctx, input.aom_input_ctx))
659 input.aom_input_ctx->file_type = FILE_TYPE_WEBM;
660 #endif
661 else if (file_is_raw(input.aom_input_ctx))
662 input.aom_input_ctx->file_type = FILE_TYPE_RAW;
663 else {
664 fprintf(stderr, "Unrecognized input file type.\n");
665 #if !CONFIG_WEBM_IO
666 fprintf(stderr, "aomdec was built without WebM container support.\n");
667 #endif
668 return EXIT_FAILURE;
671 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
672 single_file = is_single_file(outfile_pattern);
674 if (!noblit && single_file) {
675 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
676 aom_input_ctx.width, aom_input_ctx.height, 0);
677 if (do_md5)
678 MD5Init(&md5_ctx);
679 else
680 outfile = open_outfile(outfile_name);
683 if (use_y4m && !noblit) {
684 if (!single_file) {
685 fprintf(stderr,
686 "YUV4MPEG2 not supported with output patterns,"
687 " try --i420 or --yv12 or --rawvideo.\n");
688 return EXIT_FAILURE;
691 #if CONFIG_WEBM_IO
692 if (aom_input_ctx.file_type == FILE_TYPE_WEBM) {
693 if (webm_guess_framerate(input.webm_ctx, input.aom_input_ctx)) {
694 fprintf(stderr,
695 "Failed to guess framerate -- error parsing "
696 "webm file?\n");
697 return EXIT_FAILURE;
700 #endif
703 fourcc_interface = get_aom_decoder_by_fourcc(aom_input_ctx.fourcc);
704 if (interface && fourcc_interface && interface != fourcc_interface)
705 warn("Header indicates codec: %s\n", fourcc_interface->name);
706 else
707 interface = fourcc_interface;
709 if (!interface) interface = get_aom_decoder_by_index(0);
711 dec_flags = (postproc ? AOM_CODEC_USE_POSTPROC : 0) |
712 (ec_enabled ? AOM_CODEC_USE_ERROR_CONCEALMENT : 0) |
713 (frame_parallel ? AOM_CODEC_USE_FRAME_THREADING : 0);
714 if (aom_codec_dec_init(&decoder, interface->codec_interface(), &cfg,
715 dec_flags)) {
716 fprintf(stderr, "Failed to initialize decoder: %s\n",
717 aom_codec_error(&decoder));
718 goto fail2;
721 if (!quiet) fprintf(stderr, "%s\n", decoder.name);
723 #if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
724 if (strncmp(decoder.name, "WebM Project AV1", 17) == 0) {
725 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_ROW, tile_row)) {
726 fprintf(stderr, "Failed to set decode_tile_row: %s\n",
727 aom_codec_error(&decoder));
728 goto fail;
731 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_COL, tile_col)) {
732 fprintf(stderr, "Failed to set decode_tile_col: %s\n",
733 aom_codec_error(&decoder));
734 goto fail;
737 #endif
739 if (arg_skip) fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
740 while (arg_skip) {
741 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) break;
742 arg_skip--;
745 if (num_external_frame_buffers > 0) {
746 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
747 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
748 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
749 if (aom_codec_set_frame_buffer_functions(&decoder, get_av1_frame_buffer,
750 release_av1_frame_buffer,
751 &ext_fb_list)) {
752 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
753 aom_codec_error(&decoder));
754 goto fail;
758 frame_avail = 1;
759 got_data = 0;
761 /* Decode file */
762 while (frame_avail || got_data) {
763 aom_codec_iter_t iter = NULL;
764 aom_image_t *img;
765 struct aom_usec_timer timer;
766 int corrupted = 0;
768 frame_avail = 0;
769 if (!stop_after || frame_in < stop_after) {
770 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
771 frame_avail = 1;
772 frame_in++;
774 aom_usec_timer_start(&timer);
776 if (aom_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer, NULL,
777 0)) {
778 const char *detail = aom_codec_error_detail(&decoder);
779 warn("Failed to decode frame %d: %s", frame_in,
780 aom_codec_error(&decoder));
782 if (detail) warn("Additional information: %s", detail);
783 if (!keep_going) goto fail;
786 aom_usec_timer_mark(&timer);
787 dx_time += aom_usec_timer_elapsed(&timer);
788 } else {
789 flush_decoder = 1;
791 } else {
792 flush_decoder = 1;
795 aom_usec_timer_start(&timer);
797 if (flush_decoder) {
798 // Flush the decoder in frame parallel decode.
799 if (aom_codec_decode(&decoder, NULL, 0, NULL, 0)) {
800 warn("Failed to flush decoder: %s", aom_codec_error(&decoder));
804 got_data = 0;
805 if ((img = aom_codec_get_frame(&decoder, &iter))) {
806 ++frame_out;
807 got_data = 1;
810 aom_usec_timer_mark(&timer);
811 dx_time += (unsigned int)aom_usec_timer_elapsed(&timer);
813 if (!frame_parallel &&
814 aom_codec_control(&decoder, AOMD_GET_FRAME_CORRUPTED, &corrupted)) {
815 warn("Failed AOM_GET_FRAME_CORRUPTED: %s", aom_codec_error(&decoder));
816 if (!keep_going) goto fail;
818 frames_corrupted += corrupted;
820 if (progress) show_progress(frame_in, frame_out, dx_time);
822 if (!noblit && img) {
823 const int PLANES_YUV[] = { AOM_PLANE_Y, AOM_PLANE_U, AOM_PLANE_V };
824 const int PLANES_YVU[] = { AOM_PLANE_Y, AOM_PLANE_V, AOM_PLANE_U };
825 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
827 if (do_scale) {
828 if (frame_out == 1) {
829 // If the output frames are to be scaled to a fixed display size then
830 // use the width and height specified in the container. If either of
831 // these is set to 0, use the display size set in the first frame
832 // header. If that is unavailable, use the raw decoded size of the
833 // first decoded frame.
834 int render_width = aom_input_ctx.width;
835 int render_height = aom_input_ctx.height;
836 if (!render_width || !render_height) {
837 int render_size[2];
838 if (aom_codec_control(&decoder, AV1D_GET_DISPLAY_SIZE,
839 render_size)) {
840 // As last resort use size of first frame as display size.
841 render_width = img->d_w;
842 render_height = img->d_h;
843 } else {
844 render_width = render_size[0];
845 render_height = render_size[1];
848 scaled_img =
849 aom_img_alloc(NULL, img->fmt, render_width, render_height, 16);
850 scaled_img->bit_depth = img->bit_depth;
853 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
854 #if CONFIG_LIBYUV
855 libyuv_scale(img, scaled_img, kFilterBox);
856 img = scaled_img;
857 #else
858 fprintf(stderr,
859 "Failed to scale output frame: %s.\n"
860 "Scaling is disabled in this configuration. "
861 "To enable scaling, configure with --enable-libyuv\n",
862 aom_codec_error(&decoder));
863 goto fail;
864 #endif
867 #if CONFIG_AOM_HIGHBITDEPTH
868 // Default to codec bit depth if output bit depth not set
869 if (!output_bit_depth && single_file && !do_md5) {
870 output_bit_depth = img->bit_depth;
872 // Shift up or down if necessary
873 if (output_bit_depth != 0 && output_bit_depth != img->bit_depth) {
874 const aom_img_fmt_t shifted_fmt =
875 output_bit_depth == 8
876 ? img->fmt ^ (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
877 : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
878 if (img_shifted &&
879 img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
880 aom_img_free(img_shifted);
881 img_shifted = NULL;
883 if (!img_shifted) {
884 img_shifted =
885 aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
886 img_shifted->bit_depth = output_bit_depth;
888 if (output_bit_depth > img->bit_depth) {
889 aom_img_upshift(img_shifted, img, output_bit_depth - img->bit_depth);
890 } else {
891 aom_img_downshift(img_shifted, img,
892 img->bit_depth - output_bit_depth);
894 img = img_shifted;
896 #endif
898 #if CONFIG_EXT_TILE
899 aom_input_ctx.width = img->d_w;
900 aom_input_ctx.height = img->d_h;
901 #endif // CONFIG_EXT_TILE
903 if (single_file) {
904 if (use_y4m) {
905 char y4m_buf[Y4M_BUFFER_SIZE] = { 0 };
906 size_t len = 0;
907 if (img->fmt == AOM_IMG_FMT_I440 || img->fmt == AOM_IMG_FMT_I44016) {
908 fprintf(stderr, "Cannot produce y4m output for 440 sampling.\n");
909 goto fail;
911 if (frame_out == 1) {
912 // Y4M file header
913 len = y4m_write_file_header(
914 y4m_buf, sizeof(y4m_buf), aom_input_ctx.width,
915 aom_input_ctx.height, &aom_input_ctx.framerate, img->fmt,
916 img->bit_depth);
917 if (do_md5) {
918 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
919 } else {
920 fputs(y4m_buf, outfile);
924 // Y4M frame header
925 len = y4m_write_frame_header(y4m_buf, sizeof(y4m_buf));
926 if (do_md5) {
927 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
928 } else {
929 fputs(y4m_buf, outfile);
931 } else {
932 if (frame_out == 1) {
933 // Check if --yv12 or --i420 options are consistent with the
934 // bit-stream decoded
935 if (opt_i420) {
936 if (img->fmt != AOM_IMG_FMT_I420 &&
937 img->fmt != AOM_IMG_FMT_I42016) {
938 fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
939 goto fail;
942 if (opt_yv12) {
943 if ((img->fmt != AOM_IMG_FMT_I420 &&
944 img->fmt != AOM_IMG_FMT_YV12) ||
945 img->bit_depth != 8) {
946 fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
947 goto fail;
953 if (do_md5) {
954 update_image_md5(img, planes, &md5_ctx);
955 } else {
956 write_image_file(img, planes, outfile);
958 } else {
959 generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
960 img->d_h, frame_in);
961 if (do_md5) {
962 MD5Init(&md5_ctx);
963 update_image_md5(img, planes, &md5_ctx);
964 MD5Final(md5_digest, &md5_ctx);
965 print_md5(md5_digest, outfile_name);
966 } else {
967 outfile = open_outfile(outfile_name);
968 write_image_file(img, planes, outfile);
969 fclose(outfile);
975 if (summary || progress) {
976 show_progress(frame_in, frame_out, dx_time);
977 fprintf(stderr, "\n");
980 if (frames_corrupted) {
981 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
982 } else {
983 ret = EXIT_SUCCESS;
986 fail:
988 if (aom_codec_destroy(&decoder)) {
989 fprintf(stderr, "Failed to destroy decoder: %s\n",
990 aom_codec_error(&decoder));
993 fail2:
995 if (!noblit && single_file) {
996 if (do_md5) {
997 MD5Final(md5_digest, &md5_ctx);
998 print_md5(md5_digest, outfile_name);
999 } else {
1000 fclose(outfile);
1004 #if CONFIG_WEBM_IO
1005 if (input.aom_input_ctx->file_type == FILE_TYPE_WEBM)
1006 webm_free(input.webm_ctx);
1007 #endif
1009 if (input.aom_input_ctx->file_type != FILE_TYPE_WEBM) free(buf);
1011 if (scaled_img) aom_img_free(scaled_img);
1012 #if CONFIG_AOM_HIGHBITDEPTH
1013 if (img_shifted) aom_img_free(img_shifted);
1014 #endif
1016 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
1017 free(ext_fb_list.ext_fb[i].data);
1019 free(ext_fb_list.ext_fb);
1021 fclose(infile);
1022 free(argv);
1024 return ret;
1027 int main(int argc, const char **argv_) {
1028 unsigned int loops = 1, i;
1029 char **argv, **argi, **argj;
1030 struct arg arg;
1031 int error = 0;
1033 argv = argv_dup(argc - 1, argv_ + 1);
1034 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1035 memset(&arg, 0, sizeof(arg));
1036 arg.argv_step = 1;
1038 if (arg_match(&arg, &looparg, argi)) {
1039 loops = arg_parse_uint(&arg);
1040 break;
1043 free(argv);
1044 for (i = 0; !error && i < loops; i++) error = main_loop(argc, argv_);
1045 return error;