AV1InvTxfm2d.CfgTest: Add 1:2 and 1:4 transforms.
[aom.git] / aomdec.c
blob8aaffd6097db479e1cdb80c6007efab58993dff1
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 #if CONFIG_OBU_NO_IVF
47 #include "./obudec.h"
48 #endif
50 #include "./tools_common.h"
51 #if CONFIG_WEBM_IO
52 #include "./webmdec.h"
53 #endif
55 #include "./y4menc.h"
57 static const char *exec_name;
59 struct AvxDecInputContext {
60 struct AvxInputContext *aom_input_ctx;
61 struct WebmInputContext *webm_ctx;
64 static const arg_def_t help =
65 ARG_DEF(NULL, "help", 0, "Show usage options and exit");
66 static const arg_def_t looparg =
67 ARG_DEF(NULL, "loops", 1, "Number of times to decode the file");
68 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
69 static const arg_def_t use_yv12 =
70 ARG_DEF(NULL, "yv12", 0, "Output raw YV12 frames");
71 static const arg_def_t use_i420 =
72 ARG_DEF(NULL, "i420", 0, "Output raw I420 frames");
73 static const arg_def_t flipuvarg =
74 ARG_DEF(NULL, "flipuv", 0, "Flip the chroma planes in the output");
75 static const arg_def_t rawvideo =
76 ARG_DEF(NULL, "rawvideo", 0, "Output raw YUV frames");
77 static const arg_def_t noblitarg =
78 ARG_DEF(NULL, "noblit", 0, "Don't process the decoded frames");
79 static const arg_def_t progressarg =
80 ARG_DEF(NULL, "progress", 0, "Show progress after each frame decodes");
81 static const arg_def_t limitarg =
82 ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
83 static const arg_def_t skiparg =
84 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
85 static const arg_def_t postprocarg =
86 ARG_DEF(NULL, "postproc", 0, "Postprocess decoded frames");
87 static const arg_def_t summaryarg =
88 ARG_DEF(NULL, "summary", 0, "Show timing summary");
89 static const arg_def_t outputfile =
90 ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
91 static const arg_def_t threadsarg =
92 ARG_DEF("t", "threads", 1, "Max threads to use");
93 static const arg_def_t frameparallelarg =
94 ARG_DEF(NULL, "frame-parallel", 0, "Frame parallel decode");
95 static const arg_def_t verbosearg =
96 ARG_DEF("v", "verbose", 0, "Show version string");
97 static const arg_def_t scalearg =
98 ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
99 static const arg_def_t continuearg =
100 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
101 static const arg_def_t fb_arg =
102 ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
103 static const arg_def_t md5arg =
104 ARG_DEF(NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
105 static const arg_def_t framestatsarg =
106 ARG_DEF(NULL, "framestats", 1, "Output per-frame stats (.csv format)");
107 #if CONFIG_HIGHBITDEPTH
108 static const arg_def_t outbitdeptharg =
109 ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
110 #endif
111 #if CONFIG_EXT_TILE
112 static const arg_def_t tiler = ARG_DEF(NULL, "tile-row", 1,
113 "Row index of tile to decode "
114 "(-1 for all rows)");
115 static const arg_def_t tilec = ARG_DEF(NULL, "tile-column", 1,
116 "Column index of tile to decode "
117 "(-1 for all columns)");
118 #endif // CONFIG_EXT_TILE
120 static const arg_def_t *all_args[] = { &help,
121 &codecarg,
122 &use_yv12,
123 &use_i420,
124 &flipuvarg,
125 &rawvideo,
126 &noblitarg,
127 &progressarg,
128 &limitarg,
129 &skiparg,
130 &postprocarg,
131 &summaryarg,
132 &outputfile,
133 &threadsarg,
134 &frameparallelarg,
135 &verbosearg,
136 &scalearg,
137 &fb_arg,
138 &md5arg,
139 &framestatsarg,
140 &continuearg,
141 #if CONFIG_HIGHBITDEPTH
142 &outbitdeptharg,
143 #endif
144 #if CONFIG_EXT_TILE
145 &tiler,
146 &tilec,
147 #endif // CONFIG_EXT_TILE
148 NULL };
150 #if CONFIG_LIBYUV
151 static INLINE int libyuv_scale(aom_image_t *src, aom_image_t *dst,
152 FilterModeEnum mode) {
153 #if CONFIG_HIGHBITDEPTH
154 if (src->fmt == AOM_IMG_FMT_I42016) {
155 assert(dst->fmt == AOM_IMG_FMT_I42016);
156 return I420Scale_16(
157 (uint16_t *)src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y] / 2,
158 (uint16_t *)src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U] / 2,
159 (uint16_t *)src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V] / 2,
160 src->d_w, src->d_h, (uint16_t *)dst->planes[AOM_PLANE_Y],
161 dst->stride[AOM_PLANE_Y] / 2, (uint16_t *)dst->planes[AOM_PLANE_U],
162 dst->stride[AOM_PLANE_U] / 2, (uint16_t *)dst->planes[AOM_PLANE_V],
163 dst->stride[AOM_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
165 #endif
166 assert(src->fmt == AOM_IMG_FMT_I420);
167 assert(dst->fmt == AOM_IMG_FMT_I420);
168 return I420Scale(src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y],
169 src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U],
170 src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V], src->d_w,
171 src->d_h, dst->planes[AOM_PLANE_Y], dst->stride[AOM_PLANE_Y],
172 dst->planes[AOM_PLANE_U], dst->stride[AOM_PLANE_U],
173 dst->planes[AOM_PLANE_V], dst->stride[AOM_PLANE_V], dst->d_w,
174 dst->d_h, mode);
176 #endif
178 void show_help(FILE *fout, int shorthelp) {
179 fprintf(fout, "Usage: %s <options> filename\n\n", exec_name);
181 if (shorthelp) {
182 fprintf(fout, "Use --help to see the full list of options.\n");
183 return;
186 fprintf(fout, "Options:\n");
187 arg_show_usage(fout, all_args);
188 fprintf(fout,
189 "\nOutput File Patterns:\n\n"
190 " The -o argument specifies the name of the file(s) to "
191 "write to. If the\n argument does not include any escape "
192 "characters, the output will be\n written to a single file. "
193 "Otherwise, the filename will be calculated by\n expanding "
194 "the following escape characters:\n");
195 fprintf(fout,
196 "\n\t%%w - Frame width"
197 "\n\t%%h - Frame height"
198 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
199 "\n\n Pattern arguments are only supported in conjunction "
200 "with the --yv12 and\n --i420 options. If the -o option is "
201 "not specified, the output will be\n directed to stdout.\n");
202 fprintf(fout, "\nIncluded decoders:\n\n");
204 for (int i = 0; i < get_aom_decoder_count(); ++i) {
205 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
206 fprintf(fout, " %-6s - %s\n", decoder->name,
207 aom_codec_iface_name(decoder->codec_interface()));
211 void usage_exit(void) {
212 show_help(stderr, 1);
213 exit(EXIT_FAILURE);
216 static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
217 size_t *buffer_size) {
218 char raw_hdr[RAW_FRAME_HDR_SZ];
219 size_t frame_size = 0;
221 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
222 if (!feof(infile)) warn("Failed to read RAW frame size\n");
223 } else {
224 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
225 const size_t kFrameTooSmallThreshold = 256 * 1024;
226 frame_size = mem_get_le32(raw_hdr);
228 if (frame_size > kCorruptFrameThreshold) {
229 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
230 frame_size = 0;
233 if (frame_size < kFrameTooSmallThreshold) {
234 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
235 (unsigned int)frame_size);
238 if (frame_size > *buffer_size) {
239 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
240 if (new_buf) {
241 *buffer = new_buf;
242 *buffer_size = 2 * frame_size;
243 } else {
244 warn("Failed to allocate compressed data buffer\n");
245 frame_size = 0;
250 if (!feof(infile)) {
251 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
252 warn("Failed to read full frame\n");
253 return 1;
255 *bytes_read = frame_size;
258 return 0;
261 static int read_frame(struct AvxDecInputContext *input, uint8_t **buf,
262 size_t *bytes_in_buffer, size_t *buffer_size) {
263 switch (input->aom_input_ctx->file_type) {
264 #if CONFIG_WEBM_IO
265 case FILE_TYPE_WEBM:
266 return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer);
267 #endif
268 case FILE_TYPE_RAW:
269 return raw_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
270 buffer_size);
271 case FILE_TYPE_IVF:
272 return ivf_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
273 buffer_size);
274 #if CONFIG_OBU_NO_IVF
275 case FILE_TYPE_OBU:
276 return obu_read_temporal_unit(input->aom_input_ctx->file, buf,
277 bytes_in_buffer, buffer_size);
278 #endif
279 default: return 1;
283 static void update_image_md5(const aom_image_t *img, const int planes[3],
284 MD5Context *md5) {
285 int i, y;
287 for (i = 0; i < 3; ++i) {
288 const int plane = planes[i];
289 const unsigned char *buf = img->planes[plane];
290 const int stride = img->stride[plane];
291 const int w = aom_img_plane_width(img, plane) *
292 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
293 const int h = aom_img_plane_height(img, plane);
295 for (y = 0; y < h; ++y) {
296 MD5Update(md5, buf, w);
297 buf += stride;
302 static void write_image_file(const aom_image_t *img, const int *planes,
303 const int num_planes, FILE *file) {
304 int i, y;
305 #if CONFIG_HIGHBITDEPTH
306 const int bytes_per_sample = ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
307 #else
308 const int bytes_per_sample = 1;
309 #endif
311 for (i = 0; i < num_planes; ++i) {
312 const int plane = planes[i];
313 const unsigned char *buf = img->planes[plane];
314 const int stride = img->stride[plane];
315 const int w = aom_img_plane_width(img, plane);
316 const int h = aom_img_plane_height(img, plane);
318 for (y = 0; y < h; ++y) {
319 fwrite(buf, bytes_per_sample, w, file);
320 buf += stride;
325 static int file_is_raw(struct AvxInputContext *input) {
326 uint8_t buf[32];
327 int is_raw = 0;
328 aom_codec_stream_info_t si;
330 if (fread(buf, 1, 32, input->file) == 32) {
331 int i;
333 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
334 for (i = 0; i < get_aom_decoder_count(); ++i) {
335 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
336 if (!aom_codec_peek_stream_info(decoder->codec_interface(), buf + 4,
337 32 - 4, &si)) {
338 is_raw = 1;
339 input->fourcc = decoder->fourcc;
340 input->width = si.w;
341 input->height = si.h;
342 input->framerate.numerator = 30;
343 input->framerate.denominator = 1;
344 break;
350 rewind(input->file);
351 return is_raw;
354 static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
355 fprintf(stderr,
356 "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
357 frame_in, frame_out, dx_time,
358 (double)frame_out * 1000000.0 / (double)dx_time);
361 struct ExternalFrameBuffer {
362 uint8_t *data;
363 size_t size;
364 int in_use;
367 struct ExternalFrameBufferList {
368 int num_external_frame_buffers;
369 struct ExternalFrameBuffer *ext_fb;
372 // Callback used by libaom to request an external frame buffer. |cb_priv|
373 // Application private data passed into the set function. |min_size| is the
374 // minimum size in bytes needed to decode the next frame. |fb| pointer to the
375 // frame buffer.
376 static int get_av1_frame_buffer(void *cb_priv, size_t min_size,
377 aom_codec_frame_buffer_t *fb) {
378 int i;
379 struct ExternalFrameBufferList *const ext_fb_list =
380 (struct ExternalFrameBufferList *)cb_priv;
381 if (ext_fb_list == NULL) return -1;
383 // Find a free frame buffer.
384 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
385 if (!ext_fb_list->ext_fb[i].in_use) break;
388 if (i == ext_fb_list->num_external_frame_buffers) return -1;
390 if (ext_fb_list->ext_fb[i].size < min_size) {
391 free(ext_fb_list->ext_fb[i].data);
392 ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
393 if (!ext_fb_list->ext_fb[i].data) return -1;
395 ext_fb_list->ext_fb[i].size = min_size;
398 fb->data = ext_fb_list->ext_fb[i].data;
399 fb->size = ext_fb_list->ext_fb[i].size;
400 ext_fb_list->ext_fb[i].in_use = 1;
402 // Set the frame buffer's private data to point at the external frame buffer.
403 fb->priv = &ext_fb_list->ext_fb[i];
404 return 0;
407 // Callback used by libaom when there are no references to the frame buffer.
408 // |cb_priv| user private data passed into the set function. |fb| pointer
409 // to the frame buffer.
410 static int release_av1_frame_buffer(void *cb_priv,
411 aom_codec_frame_buffer_t *fb) {
412 struct ExternalFrameBuffer *const ext_fb =
413 (struct ExternalFrameBuffer *)fb->priv;
414 (void)cb_priv;
415 ext_fb->in_use = 0;
416 return 0;
419 static void generate_filename(const char *pattern, char *out, size_t q_len,
420 unsigned int d_w, unsigned int d_h,
421 unsigned int frame_in) {
422 const char *p = pattern;
423 char *q = out;
425 do {
426 char *next_pat = strchr(p, '%');
428 if (p == next_pat) {
429 size_t pat_len;
431 /* parse the pattern */
432 q[q_len - 1] = '\0';
433 switch (p[1]) {
434 case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
435 case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
436 case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
437 case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
438 case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
439 case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
440 case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
441 case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
442 case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
443 case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
444 case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
445 default: die("Unrecognized pattern %%%c\n", p[1]); break;
448 pat_len = strlen(q);
449 if (pat_len >= q_len - 1) die("Output filename too long.\n");
450 q += pat_len;
451 p += 2;
452 q_len -= pat_len;
453 } else {
454 size_t copy_len;
456 /* copy the next segment */
457 if (!next_pat)
458 copy_len = strlen(p);
459 else
460 copy_len = next_pat - p;
462 if (copy_len >= q_len - 1) die("Output filename too long.\n");
464 memcpy(q, p, copy_len);
465 q[copy_len] = '\0';
466 q += copy_len;
467 p += copy_len;
468 q_len -= copy_len;
470 } while (*p);
473 static int is_single_file(const char *outfile_pattern) {
474 const char *p = outfile_pattern;
476 do {
477 p = strchr(p, '%');
478 if (p && p[1] >= '1' && p[1] <= '9')
479 return 0; // pattern contains sequence number, so it's not unique
480 if (p) p++;
481 } while (p);
483 return 1;
486 static void print_md5(unsigned char digest[16], const char *filename) {
487 int i;
489 for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
490 printf(" %s\n", filename);
493 static FILE *open_outfile(const char *name) {
494 if (strcmp("-", name) == 0) {
495 set_binary_mode(stdout);
496 return stdout;
497 } else {
498 FILE *file = fopen(name, "wb");
499 if (!file) fatal("Failed to open output file '%s'", name);
500 return file;
504 #if CONFIG_HIGHBITDEPTH
505 static int img_shifted_realloc_required(const aom_image_t *img,
506 const aom_image_t *shifted,
507 aom_img_fmt_t required_fmt) {
508 return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
509 required_fmt != shifted->fmt;
511 #endif
513 static int main_loop(int argc, const char **argv_) {
514 aom_codec_ctx_t decoder;
515 char *fn = NULL;
516 int i;
517 int ret = EXIT_FAILURE;
518 uint8_t *buf = NULL;
519 size_t bytes_in_buffer = 0, buffer_size = 0;
520 FILE *infile;
521 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
522 int do_md5 = 0, progress = 0, frame_parallel = 0;
523 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
524 int arg_skip = 0;
525 int keep_going = 0;
526 const AvxInterface *interface = NULL;
527 const AvxInterface *fourcc_interface = NULL;
528 uint64_t dx_time = 0;
529 struct arg arg;
530 char **argv, **argi, **argj;
532 int single_file;
533 int use_y4m = 1;
534 int opt_yv12 = 0;
535 int opt_i420 = 0;
536 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH, 0 };
537 #if CONFIG_HIGHBITDEPTH
538 unsigned int output_bit_depth = 0;
539 #endif
540 #if CONFIG_EXT_TILE
541 int tile_row = -1;
542 int tile_col = -1;
543 #endif // CONFIG_EXT_TILE
544 int frames_corrupted = 0;
545 int dec_flags = 0;
546 int do_scale = 0;
547 aom_image_t *scaled_img = NULL;
548 #if CONFIG_HIGHBITDEPTH
549 aom_image_t *img_shifted = NULL;
550 #endif
551 int frame_avail, got_data, flush_decoder = 0;
552 int num_external_frame_buffers = 0;
553 struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
555 const char *outfile_pattern = NULL;
556 char outfile_name[PATH_MAX] = { 0 };
557 FILE *outfile = NULL;
559 FILE *framestats_file = NULL;
561 MD5Context md5_ctx;
562 unsigned char md5_digest[16];
564 struct AvxDecInputContext input = { NULL, NULL };
565 struct AvxInputContext aom_input_ctx;
566 #if CONFIG_WEBM_IO
567 struct WebmInputContext webm_ctx;
568 memset(&(webm_ctx), 0, sizeof(webm_ctx));
569 input.webm_ctx = &webm_ctx;
570 #endif
571 input.aom_input_ctx = &aom_input_ctx;
573 /* Parse command line */
574 exec_name = argv_[0];
575 argv = argv_dup(argc - 1, argv_ + 1);
577 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
578 memset(&arg, 0, sizeof(arg));
579 arg.argv_step = 1;
581 if (arg_match(&arg, &help, argi)) {
582 show_help(stdout, 0);
583 exit(EXIT_SUCCESS);
584 } else if (arg_match(&arg, &codecarg, argi)) {
585 interface = get_aom_decoder_by_name(arg.val);
586 if (!interface)
587 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
588 } else if (arg_match(&arg, &looparg, argi)) {
589 // no-op
590 } else if (arg_match(&arg, &outputfile, argi)) {
591 outfile_pattern = arg.val;
592 } else if (arg_match(&arg, &use_yv12, argi)) {
593 use_y4m = 0;
594 flipuv = 1;
595 opt_yv12 = 1;
596 } else if (arg_match(&arg, &use_i420, argi)) {
597 use_y4m = 0;
598 flipuv = 0;
599 opt_i420 = 1;
600 } else if (arg_match(&arg, &rawvideo, argi)) {
601 use_y4m = 0;
602 } else if (arg_match(&arg, &flipuvarg, argi)) {
603 flipuv = 1;
604 } else if (arg_match(&arg, &noblitarg, argi)) {
605 noblit = 1;
606 } else if (arg_match(&arg, &progressarg, argi)) {
607 progress = 1;
608 } else if (arg_match(&arg, &limitarg, argi)) {
609 stop_after = arg_parse_uint(&arg);
610 } else if (arg_match(&arg, &skiparg, argi)) {
611 arg_skip = arg_parse_uint(&arg);
612 } else if (arg_match(&arg, &postprocarg, argi)) {
613 postproc = 1;
614 } else if (arg_match(&arg, &md5arg, argi)) {
615 do_md5 = 1;
616 } else if (arg_match(&arg, &framestatsarg, argi)) {
617 framestats_file = fopen(arg.val, "w");
618 if (!framestats_file) {
619 die("Error: Could not open --framestats file (%s) for writing.\n",
620 arg.val);
622 } else if (arg_match(&arg, &summaryarg, argi)) {
623 summary = 1;
624 } else if (arg_match(&arg, &threadsarg, argi)) {
625 cfg.threads = arg_parse_uint(&arg);
627 #if CONFIG_AV1_DECODER
628 else if (arg_match(&arg, &frameparallelarg, argi))
629 frame_parallel = 1;
630 #endif
631 else if (arg_match(&arg, &verbosearg, argi))
632 quiet = 0;
633 else if (arg_match(&arg, &scalearg, argi))
634 do_scale = 1;
635 else if (arg_match(&arg, &fb_arg, argi))
636 num_external_frame_buffers = arg_parse_uint(&arg);
637 else if (arg_match(&arg, &continuearg, argi))
638 keep_going = 1;
639 #if CONFIG_HIGHBITDEPTH
640 else if (arg_match(&arg, &outbitdeptharg, argi)) {
641 output_bit_depth = arg_parse_uint(&arg);
643 #endif
644 #if CONFIG_EXT_TILE
645 else if (arg_match(&arg, &tiler, argi))
646 tile_row = arg_parse_int(&arg);
647 else if (arg_match(&arg, &tilec, argi))
648 tile_col = arg_parse_int(&arg);
649 #endif // CONFIG_EXT_TILE
650 else
651 argj++;
654 /* Check for unrecognized options */
655 for (argi = argv; *argi; argi++)
656 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
657 die("Error: Unrecognized option %s\n", *argi);
659 /* Handle non-option arguments */
660 fn = argv[0];
662 if (!fn) {
663 free(argv);
664 fprintf(stderr, "No input file specified!\n");
665 usage_exit();
667 /* Open file */
668 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
670 if (!infile) {
671 fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
673 #if CONFIG_OS_SUPPORT
674 /* Make sure we don't dump to the terminal, unless forced to with -o - */
675 if (!outfile_pattern && isatty(STDOUT_FILENO) && !do_md5 && !noblit) {
676 fprintf(stderr,
677 "Not dumping raw video to your terminal. Use '-o -' to "
678 "override.\n");
679 return EXIT_FAILURE;
681 #endif
682 input.aom_input_ctx->file = infile;
683 if (file_is_ivf(input.aom_input_ctx))
684 input.aom_input_ctx->file_type = FILE_TYPE_IVF;
685 #if CONFIG_WEBM_IO
686 else if (file_is_webm(input.webm_ctx, input.aom_input_ctx))
687 input.aom_input_ctx->file_type = FILE_TYPE_WEBM;
688 #endif
689 #if CONFIG_OBU_NO_IVF
690 else if (file_is_obu(input.aom_input_ctx))
691 input.aom_input_ctx->file_type = FILE_TYPE_OBU;
692 #endif
693 else if (file_is_raw(input.aom_input_ctx))
694 input.aom_input_ctx->file_type = FILE_TYPE_RAW;
695 else {
696 fprintf(stderr, "Unrecognized input file type.\n");
697 #if !CONFIG_WEBM_IO
698 fprintf(stderr, "aomdec was built without WebM container support.\n");
699 #endif
700 return EXIT_FAILURE;
703 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
704 single_file = is_single_file(outfile_pattern);
706 if (!noblit && single_file) {
707 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
708 aom_input_ctx.width, aom_input_ctx.height, 0);
709 if (do_md5)
710 MD5Init(&md5_ctx);
711 else
712 outfile = open_outfile(outfile_name);
715 if (use_y4m && !noblit) {
716 if (!single_file) {
717 fprintf(stderr,
718 "YUV4MPEG2 not supported with output patterns,"
719 " try --i420 or --yv12 or --rawvideo.\n");
720 return EXIT_FAILURE;
723 #if CONFIG_WEBM_IO
724 if (aom_input_ctx.file_type == FILE_TYPE_WEBM) {
725 if (webm_guess_framerate(input.webm_ctx, input.aom_input_ctx)) {
726 fprintf(stderr,
727 "Failed to guess framerate -- error parsing "
728 "webm file?\n");
729 return EXIT_FAILURE;
732 #endif
735 fourcc_interface = get_aom_decoder_by_fourcc(aom_input_ctx.fourcc);
736 if (interface && fourcc_interface && interface != fourcc_interface)
737 warn("Header indicates codec: %s\n", fourcc_interface->name);
738 else
739 interface = fourcc_interface;
741 if (!interface) interface = get_aom_decoder_by_index(0);
743 dec_flags = (postproc ? AOM_CODEC_USE_POSTPROC : 0) |
744 (frame_parallel ? AOM_CODEC_USE_FRAME_THREADING : 0);
745 if (aom_codec_dec_init(&decoder, interface->codec_interface(), &cfg,
746 dec_flags)) {
747 fprintf(stderr, "Failed to initialize decoder: %s\n",
748 aom_codec_error(&decoder));
749 goto fail2;
752 if (!quiet) fprintf(stderr, "%s\n", decoder.name);
754 #if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
755 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_ROW, tile_row)) {
756 fprintf(stderr, "Failed to set decode_tile_row: %s\n",
757 aom_codec_error(&decoder));
758 goto fail;
761 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_COL, tile_col)) {
762 fprintf(stderr, "Failed to set decode_tile_col: %s\n",
763 aom_codec_error(&decoder));
764 goto fail;
766 #endif
768 if (arg_skip) fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
769 while (arg_skip) {
770 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) break;
771 arg_skip--;
774 if (num_external_frame_buffers > 0) {
775 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
776 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
777 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
778 if (aom_codec_set_frame_buffer_functions(&decoder, get_av1_frame_buffer,
779 release_av1_frame_buffer,
780 &ext_fb_list)) {
781 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
782 aom_codec_error(&decoder));
783 goto fail;
787 frame_avail = 1;
788 got_data = 0;
790 if (framestats_file) fprintf(framestats_file, "bytes,qp\r\n");
792 /* Decode file */
793 while (frame_avail || got_data) {
794 aom_codec_iter_t iter = NULL;
795 aom_image_t *img;
796 struct aom_usec_timer timer;
797 int corrupted = 0;
799 frame_avail = 0;
800 if (!stop_after || frame_in < stop_after) {
801 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
802 frame_avail = 1;
803 frame_in++;
805 aom_usec_timer_start(&timer);
807 if (aom_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer, NULL,
808 0)) {
809 const char *detail = aom_codec_error_detail(&decoder);
810 warn("Failed to decode frame %d: %s", frame_in,
811 aom_codec_error(&decoder));
813 if (detail) warn("Additional information: %s", detail);
814 if (!keep_going) goto fail;
817 if (framestats_file) {
818 int qp;
819 if (aom_codec_control(&decoder, AOMD_GET_LAST_QUANTIZER, &qp)) {
820 warn("Failed AOMD_GET_LAST_QUANTIZER: %s",
821 aom_codec_error(&decoder));
822 if (!keep_going) goto fail;
824 fprintf(framestats_file, "%d,%d\r\n", (int)bytes_in_buffer, qp);
827 aom_usec_timer_mark(&timer);
828 dx_time += aom_usec_timer_elapsed(&timer);
829 } else {
830 flush_decoder = 1;
832 } else {
833 flush_decoder = 1;
836 aom_usec_timer_start(&timer);
838 if (flush_decoder) {
839 // Flush the decoder in frame parallel decode.
840 if (aom_codec_decode(&decoder, NULL, 0, NULL, 0)) {
841 warn("Failed to flush decoder: %s", aom_codec_error(&decoder));
845 got_data = 0;
846 if ((img = aom_codec_get_frame(&decoder, &iter))) {
847 ++frame_out;
848 got_data = 1;
851 aom_usec_timer_mark(&timer);
852 dx_time += (unsigned int)aom_usec_timer_elapsed(&timer);
854 if (!frame_parallel &&
855 aom_codec_control(&decoder, AOMD_GET_FRAME_CORRUPTED, &corrupted)) {
856 warn("Failed AOM_GET_FRAME_CORRUPTED: %s", aom_codec_error(&decoder));
857 if (!keep_going) goto fail;
859 frames_corrupted += corrupted;
861 if (progress) show_progress(frame_in, frame_out, dx_time);
863 if (!noblit && img) {
864 const int PLANES_YUV[] = { AOM_PLANE_Y, AOM_PLANE_U, AOM_PLANE_V };
865 const int PLANES_YVU[] = { AOM_PLANE_Y, AOM_PLANE_V, AOM_PLANE_U };
866 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
868 if (do_scale) {
869 if (frame_out == 1) {
870 // If the output frames are to be scaled to a fixed display size then
871 // use the width and height specified in the container. If either of
872 // these is set to 0, use the display size set in the first frame
873 // header. If that is unavailable, use the raw decoded size of the
874 // first decoded frame.
875 int render_width = aom_input_ctx.width;
876 int render_height = aom_input_ctx.height;
877 if (!render_width || !render_height) {
878 int render_size[2];
879 if (aom_codec_control(&decoder, AV1D_GET_DISPLAY_SIZE,
880 render_size)) {
881 // As last resort use size of first frame as display size.
882 render_width = img->d_w;
883 render_height = img->d_h;
884 } else {
885 render_width = render_size[0];
886 render_height = render_size[1];
889 scaled_img =
890 aom_img_alloc(NULL, img->fmt, render_width, render_height, 16);
891 scaled_img->bit_depth = img->bit_depth;
894 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
895 #if CONFIG_LIBYUV
896 libyuv_scale(img, scaled_img, kFilterBox);
897 img = scaled_img;
898 #else
899 fprintf(stderr,
900 "Failed to scale output frame: %s.\n"
901 "Scaling is disabled in this configuration. "
902 "To enable scaling, configure with --enable-libyuv\n",
903 aom_codec_error(&decoder));
904 goto fail;
905 #endif
908 #if CONFIG_HIGHBITDEPTH
909 // Default to codec bit depth if output bit depth not set
910 if (!output_bit_depth && single_file && !do_md5) {
911 output_bit_depth = img->bit_depth;
913 // Shift up or down if necessary
914 if (output_bit_depth != 0) {
915 const aom_img_fmt_t shifted_fmt =
916 output_bit_depth == 8
917 ? img->fmt ^ (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
918 : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
920 if (shifted_fmt != img->fmt || output_bit_depth != img->bit_depth) {
921 if (img_shifted &&
922 img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
923 aom_img_free(img_shifted);
924 img_shifted = NULL;
926 if (!img_shifted) {
927 img_shifted =
928 aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
929 img_shifted->bit_depth = output_bit_depth;
931 if (output_bit_depth > img->bit_depth) {
932 aom_img_upshift(img_shifted, img,
933 output_bit_depth - img->bit_depth);
934 } else {
935 aom_img_downshift(img_shifted, img,
936 img->bit_depth - output_bit_depth);
938 img = img_shifted;
941 #endif
943 #if CONFIG_EXT_TILE
944 aom_input_ctx.width = img->d_w;
945 aom_input_ctx.height = img->d_h;
946 #endif // CONFIG_EXT_TILE
948 #if CONFIG_MONO_VIDEO
949 int num_planes = (!use_y4m && img->cs == AOM_CS_MONOCHROME) ? 1 : 3;
950 #else
951 int num_planes = 3;
952 #endif
954 if (single_file) {
955 if (use_y4m) {
956 char y4m_buf[Y4M_BUFFER_SIZE] = { 0 };
957 size_t len = 0;
958 if (img->fmt == AOM_IMG_FMT_I440 || img->fmt == AOM_IMG_FMT_I44016) {
959 fprintf(stderr, "Cannot produce y4m output for 440 sampling.\n");
960 goto fail;
962 if (frame_out == 1) {
963 // Y4M file header
964 len = y4m_write_file_header(
965 y4m_buf, sizeof(y4m_buf), aom_input_ctx.width,
966 aom_input_ctx.height, &aom_input_ctx.framerate, img->fmt,
967 img->bit_depth);
968 if (do_md5) {
969 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
970 } else {
971 fputs(y4m_buf, outfile);
975 // Y4M frame header
976 len = y4m_write_frame_header(y4m_buf, sizeof(y4m_buf));
977 if (do_md5) {
978 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
979 } else {
980 fputs(y4m_buf, outfile);
982 } else {
983 if (frame_out == 1) {
984 // Check if --yv12 or --i420 options are consistent with the
985 // bit-stream decoded
986 if (opt_i420) {
987 if (img->fmt != AOM_IMG_FMT_I420 &&
988 img->fmt != AOM_IMG_FMT_I42016) {
989 fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
990 goto fail;
993 if (opt_yv12) {
994 if ((img->fmt != AOM_IMG_FMT_I420 &&
995 img->fmt != AOM_IMG_FMT_YV12) ||
996 img->bit_depth != 8) {
997 fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
998 goto fail;
1004 if (do_md5) {
1005 update_image_md5(img, planes, &md5_ctx);
1006 } else {
1007 write_image_file(img, planes, num_planes, outfile);
1009 } else {
1010 generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
1011 img->d_h, frame_in);
1012 if (do_md5) {
1013 MD5Init(&md5_ctx);
1014 update_image_md5(img, planes, &md5_ctx);
1015 MD5Final(md5_digest, &md5_ctx);
1016 print_md5(md5_digest, outfile_name);
1017 } else {
1018 outfile = open_outfile(outfile_name);
1019 write_image_file(img, planes, num_planes, outfile);
1020 fclose(outfile);
1026 if (summary || progress) {
1027 show_progress(frame_in, frame_out, dx_time);
1028 fprintf(stderr, "\n");
1031 if (frames_corrupted) {
1032 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
1033 } else {
1034 ret = EXIT_SUCCESS;
1037 fail:
1039 if (aom_codec_destroy(&decoder)) {
1040 fprintf(stderr, "Failed to destroy decoder: %s\n",
1041 aom_codec_error(&decoder));
1044 fail2:
1046 if (!noblit && single_file) {
1047 if (do_md5) {
1048 MD5Final(md5_digest, &md5_ctx);
1049 print_md5(md5_digest, outfile_name);
1050 } else {
1051 fclose(outfile);
1055 #if CONFIG_WEBM_IO
1056 if (input.aom_input_ctx->file_type == FILE_TYPE_WEBM)
1057 webm_free(input.webm_ctx);
1058 #endif
1060 if (input.aom_input_ctx->file_type != FILE_TYPE_WEBM) free(buf);
1062 if (scaled_img) aom_img_free(scaled_img);
1063 #if CONFIG_HIGHBITDEPTH
1064 if (img_shifted) aom_img_free(img_shifted);
1065 #endif
1067 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
1068 free(ext_fb_list.ext_fb[i].data);
1070 free(ext_fb_list.ext_fb);
1072 fclose(infile);
1073 if (framestats_file) fclose(framestats_file);
1075 free(argv);
1077 return ret;
1080 int main(int argc, const char **argv_) {
1081 unsigned int loops = 1, i;
1082 char **argv, **argi, **argj;
1083 struct arg arg;
1084 int error = 0;
1086 argv = argv_dup(argc - 1, argv_ + 1);
1087 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1088 memset(&arg, 0, sizeof(arg));
1089 arg.argv_step = 1;
1091 if (arg_match(&arg, &looparg, argi)) {
1092 loops = arg_parse_uint(&arg);
1093 break;
1096 free(argv);
1097 for (i = 0; !error && i < loops; i++) error = main_loop(argc, argv_);
1098 return error;