Add sse2/avx2 version of aom_dc_predictor_wxh()
[aom.git] / aomdec.c
blob9534febf97b1e9219e3a878f07c7ce0d1dfbd40e
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 static const arg_def_t outbitdeptharg =
108 ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
109 static const arg_def_t tilem = ARG_DEF(NULL, "tile-mode", 1,
110 "Tile coding mode "
111 "(0 for normal tile coding mode)");
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)");
119 static const arg_def_t *all_args[] = {
120 &help, &codecarg, &use_yv12, &use_i420, &flipuvarg,
121 &rawvideo, &noblitarg, &progressarg, &limitarg, &skiparg,
122 &postprocarg, &summaryarg, &outputfile, &threadsarg, &frameparallelarg,
123 &verbosearg, &scalearg, &fb_arg, &md5arg, &framestatsarg,
124 &continuearg, &outbitdeptharg, &tilem, &tiler, &tilec,
125 NULL
128 #if CONFIG_LIBYUV
129 static INLINE int libyuv_scale(aom_image_t *src, aom_image_t *dst,
130 FilterModeEnum mode) {
131 if (src->fmt == AOM_IMG_FMT_I42016) {
132 assert(dst->fmt == AOM_IMG_FMT_I42016);
133 return I420Scale_16(
134 (uint16_t *)src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y] / 2,
135 (uint16_t *)src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U] / 2,
136 (uint16_t *)src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V] / 2,
137 src->d_w, src->d_h, (uint16_t *)dst->planes[AOM_PLANE_Y],
138 dst->stride[AOM_PLANE_Y] / 2, (uint16_t *)dst->planes[AOM_PLANE_U],
139 dst->stride[AOM_PLANE_U] / 2, (uint16_t *)dst->planes[AOM_PLANE_V],
140 dst->stride[AOM_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
142 assert(src->fmt == AOM_IMG_FMT_I420);
143 assert(dst->fmt == AOM_IMG_FMT_I420);
144 return I420Scale(src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y],
145 src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U],
146 src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V], src->d_w,
147 src->d_h, dst->planes[AOM_PLANE_Y], dst->stride[AOM_PLANE_Y],
148 dst->planes[AOM_PLANE_U], dst->stride[AOM_PLANE_U],
149 dst->planes[AOM_PLANE_V], dst->stride[AOM_PLANE_V], dst->d_w,
150 dst->d_h, mode);
152 #endif
154 void show_help(FILE *fout, int shorthelp) {
155 fprintf(fout, "Usage: %s <options> filename\n\n", exec_name);
157 if (shorthelp) {
158 fprintf(fout, "Use --help to see the full list of options.\n");
159 return;
162 fprintf(fout, "Options:\n");
163 arg_show_usage(fout, all_args);
164 fprintf(fout,
165 "\nOutput File Patterns:\n\n"
166 " The -o argument specifies the name of the file(s) to "
167 "write to. If the\n argument does not include any escape "
168 "characters, the output will be\n written to a single file. "
169 "Otherwise, the filename will be calculated by\n expanding "
170 "the following escape characters:\n");
171 fprintf(fout,
172 "\n\t%%w - Frame width"
173 "\n\t%%h - Frame height"
174 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
175 "\n\n Pattern arguments are only supported in conjunction "
176 "with the --yv12 and\n --i420 options. If the -o option is "
177 "not specified, the output will be\n directed to stdout.\n");
178 fprintf(fout, "\nIncluded decoders:\n\n");
180 for (int i = 0; i < get_aom_decoder_count(); ++i) {
181 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
182 fprintf(fout, " %-6s - %s\n", decoder->name,
183 aom_codec_iface_name(decoder->codec_interface()));
187 void usage_exit(void) {
188 show_help(stderr, 1);
189 exit(EXIT_FAILURE);
192 static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
193 size_t *buffer_size) {
194 char raw_hdr[RAW_FRAME_HDR_SZ];
195 size_t frame_size = 0;
197 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
198 if (!feof(infile)) warn("Failed to read RAW frame size\n");
199 } else {
200 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
201 const size_t kFrameTooSmallThreshold = 256 * 1024;
202 frame_size = mem_get_le32(raw_hdr);
204 if (frame_size > kCorruptFrameThreshold) {
205 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
206 frame_size = 0;
209 if (frame_size < kFrameTooSmallThreshold) {
210 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
211 (unsigned int)frame_size);
214 if (frame_size > *buffer_size) {
215 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
216 if (new_buf) {
217 *buffer = new_buf;
218 *buffer_size = 2 * frame_size;
219 } else {
220 warn("Failed to allocate compressed data buffer\n");
221 frame_size = 0;
226 if (!feof(infile)) {
227 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
228 warn("Failed to read full frame\n");
229 return 1;
231 *bytes_read = frame_size;
234 return 0;
237 static int read_frame(struct AvxDecInputContext *input, uint8_t **buf,
238 size_t *bytes_in_buffer, size_t *buffer_size) {
239 switch (input->aom_input_ctx->file_type) {
240 #if CONFIG_WEBM_IO
241 case FILE_TYPE_WEBM:
242 return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer);
243 #endif
244 case FILE_TYPE_RAW:
245 return raw_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
246 buffer_size);
247 case FILE_TYPE_IVF:
248 return ivf_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
249 buffer_size);
250 #if CONFIG_OBU_NO_IVF
251 case FILE_TYPE_OBU:
252 return obu_read_temporal_unit(input->aom_input_ctx->file, buf,
253 #if CONFIG_SCALABILITY
254 bytes_in_buffer, buffer_size, 0);
255 #else
256 bytes_in_buffer, buffer_size);
257 #endif
258 #endif
259 default: return 1;
263 static void update_image_md5(const aom_image_t *img, const int planes[3],
264 MD5Context *md5) {
265 int i, y;
267 for (i = 0; i < 3; ++i) {
268 const int plane = planes[i];
269 const unsigned char *buf = img->planes[plane];
270 const int stride = img->stride[plane];
271 const int w = aom_img_plane_width(img, plane) *
272 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
273 const int h = aom_img_plane_height(img, plane);
275 for (y = 0; y < h; ++y) {
276 MD5Update(md5, buf, w);
277 buf += stride;
282 static void write_image_file(const aom_image_t *img, const int *planes,
283 const int num_planes, FILE *file) {
284 int i, y;
285 const int bytes_per_sample = ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
287 for (i = 0; i < num_planes; ++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 const int h = aom_img_plane_height(img, plane);
294 for (y = 0; y < h; ++y) {
295 fwrite(buf, bytes_per_sample, w, file);
296 buf += stride;
301 static int file_is_raw(struct AvxInputContext *input) {
302 uint8_t buf[32];
303 int is_raw = 0;
304 aom_codec_stream_info_t si;
306 if (fread(buf, 1, 32, input->file) == 32) {
307 int i;
309 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
310 for (i = 0; i < get_aom_decoder_count(); ++i) {
311 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
312 if (!aom_codec_peek_stream_info(decoder->codec_interface(), buf + 4,
313 32 - 4, &si)) {
314 is_raw = 1;
315 input->fourcc = decoder->fourcc;
316 input->width = si.w;
317 input->height = si.h;
318 input->framerate.numerator = 30;
319 input->framerate.denominator = 1;
320 break;
326 rewind(input->file);
327 return is_raw;
330 static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
331 fprintf(stderr,
332 "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
333 frame_in, frame_out, dx_time,
334 (double)frame_out * 1000000.0 / (double)dx_time);
337 struct ExternalFrameBuffer {
338 uint8_t *data;
339 size_t size;
340 int in_use;
343 struct ExternalFrameBufferList {
344 int num_external_frame_buffers;
345 struct ExternalFrameBuffer *ext_fb;
348 // Callback used by libaom to request an external frame buffer. |cb_priv|
349 // Application private data passed into the set function. |min_size| is the
350 // minimum size in bytes needed to decode the next frame. |fb| pointer to the
351 // frame buffer.
352 static int get_av1_frame_buffer(void *cb_priv, size_t min_size,
353 aom_codec_frame_buffer_t *fb) {
354 int i;
355 struct ExternalFrameBufferList *const ext_fb_list =
356 (struct ExternalFrameBufferList *)cb_priv;
357 if (ext_fb_list == NULL) return -1;
359 // Find a free frame buffer.
360 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
361 if (!ext_fb_list->ext_fb[i].in_use) break;
364 if (i == ext_fb_list->num_external_frame_buffers) return -1;
366 if (ext_fb_list->ext_fb[i].size < min_size) {
367 free(ext_fb_list->ext_fb[i].data);
368 ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
369 if (!ext_fb_list->ext_fb[i].data) return -1;
371 ext_fb_list->ext_fb[i].size = min_size;
374 fb->data = ext_fb_list->ext_fb[i].data;
375 fb->size = ext_fb_list->ext_fb[i].size;
376 ext_fb_list->ext_fb[i].in_use = 1;
378 // Set the frame buffer's private data to point at the external frame buffer.
379 fb->priv = &ext_fb_list->ext_fb[i];
380 return 0;
383 // Callback used by libaom when there are no references to the frame buffer.
384 // |cb_priv| user private data passed into the set function. |fb| pointer
385 // to the frame buffer.
386 static int release_av1_frame_buffer(void *cb_priv,
387 aom_codec_frame_buffer_t *fb) {
388 struct ExternalFrameBuffer *const ext_fb =
389 (struct ExternalFrameBuffer *)fb->priv;
390 (void)cb_priv;
391 ext_fb->in_use = 0;
392 return 0;
395 static void generate_filename(const char *pattern, char *out, size_t q_len,
396 unsigned int d_w, unsigned int d_h,
397 unsigned int frame_in) {
398 const char *p = pattern;
399 char *q = out;
401 do {
402 char *next_pat = strchr(p, '%');
404 if (p == next_pat) {
405 size_t pat_len;
407 /* parse the pattern */
408 q[q_len - 1] = '\0';
409 switch (p[1]) {
410 case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
411 case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
412 case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
413 case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
414 case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
415 case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
416 case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
417 case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
418 case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
419 case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
420 case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
421 default: die("Unrecognized pattern %%%c\n", p[1]); break;
424 pat_len = strlen(q);
425 if (pat_len >= q_len - 1) die("Output filename too long.\n");
426 q += pat_len;
427 p += 2;
428 q_len -= pat_len;
429 } else {
430 size_t copy_len;
432 /* copy the next segment */
433 if (!next_pat)
434 copy_len = strlen(p);
435 else
436 copy_len = next_pat - p;
438 if (copy_len >= q_len - 1) die("Output filename too long.\n");
440 memcpy(q, p, copy_len);
441 q[copy_len] = '\0';
442 q += copy_len;
443 p += copy_len;
444 q_len -= copy_len;
446 } while (*p);
449 static int is_single_file(const char *outfile_pattern) {
450 const char *p = outfile_pattern;
452 do {
453 p = strchr(p, '%');
454 if (p && p[1] >= '1' && p[1] <= '9')
455 return 0; // pattern contains sequence number, so it's not unique
456 if (p) p++;
457 } while (p);
459 return 1;
462 static void print_md5(unsigned char digest[16], const char *filename) {
463 int i;
465 for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
466 printf(" %s\n", filename);
469 static FILE *open_outfile(const char *name) {
470 if (strcmp("-", name) == 0) {
471 set_binary_mode(stdout);
472 return stdout;
473 } else {
474 FILE *file = fopen(name, "wb");
475 if (!file) fatal("Failed to open output file '%s'", name);
476 return file;
480 static int img_shifted_realloc_required(const aom_image_t *img,
481 const aom_image_t *shifted,
482 aom_img_fmt_t required_fmt) {
483 return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
484 required_fmt != shifted->fmt;
487 static int main_loop(int argc, const char **argv_) {
488 aom_codec_ctx_t decoder;
489 char *fn = NULL;
490 int i;
491 int ret = EXIT_FAILURE;
492 uint8_t *buf = NULL;
493 size_t bytes_in_buffer = 0, buffer_size = 0;
494 FILE *infile;
495 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
496 int do_md5 = 0, progress = 0, frame_parallel = 0;
497 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
498 int arg_skip = 0;
499 int keep_going = 0;
500 const AvxInterface *interface = NULL;
501 const AvxInterface *fourcc_interface = NULL;
502 uint64_t dx_time = 0;
503 struct arg arg;
504 char **argv, **argi, **argj;
506 int single_file;
507 int use_y4m = 1;
508 int opt_yv12 = 0;
509 int opt_i420 = 0;
510 int opt_raw = 0;
511 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH, { 1 } };
512 unsigned int output_bit_depth = 0;
513 unsigned int tile_mode = 0;
514 int tile_row = -1;
515 int tile_col = -1;
516 int frames_corrupted = 0;
517 int dec_flags = 0;
518 int do_scale = 0;
519 aom_image_t *scaled_img = NULL;
520 aom_image_t *img_shifted = NULL;
521 int frame_avail, got_data, flush_decoder = 0;
522 int num_external_frame_buffers = 0;
523 struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
525 const char *outfile_pattern = NULL;
526 char outfile_name[PATH_MAX] = { 0 };
527 FILE *outfile = NULL;
529 FILE *framestats_file = NULL;
531 MD5Context md5_ctx;
532 unsigned char md5_digest[16];
534 struct AvxDecInputContext input = { NULL, NULL };
535 struct AvxInputContext aom_input_ctx;
536 #if CONFIG_WEBM_IO
537 struct WebmInputContext webm_ctx;
538 memset(&(webm_ctx), 0, sizeof(webm_ctx));
539 input.webm_ctx = &webm_ctx;
540 #endif
541 input.aom_input_ctx = &aom_input_ctx;
543 /* Parse command line */
544 exec_name = argv_[0];
545 argv = argv_dup(argc - 1, argv_ + 1);
547 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
548 memset(&arg, 0, sizeof(arg));
549 arg.argv_step = 1;
551 if (arg_match(&arg, &help, argi)) {
552 show_help(stdout, 0);
553 exit(EXIT_SUCCESS);
554 } else if (arg_match(&arg, &codecarg, argi)) {
555 interface = get_aom_decoder_by_name(arg.val);
556 if (!interface)
557 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
558 } else if (arg_match(&arg, &looparg, argi)) {
559 // no-op
560 } else if (arg_match(&arg, &outputfile, argi)) {
561 outfile_pattern = arg.val;
562 } else if (arg_match(&arg, &use_yv12, argi)) {
563 use_y4m = 0;
564 flipuv = 1;
565 opt_yv12 = 1;
566 opt_i420 = 0;
567 opt_raw = 0;
568 } else if (arg_match(&arg, &use_i420, argi)) {
569 use_y4m = 0;
570 flipuv = 0;
571 opt_yv12 = 0;
572 opt_i420 = 1;
573 opt_raw = 0;
574 } else if (arg_match(&arg, &rawvideo, argi)) {
575 use_y4m = 0;
576 opt_yv12 = 0;
577 opt_i420 = 0;
578 opt_raw = 1;
579 } else if (arg_match(&arg, &flipuvarg, argi)) {
580 flipuv = 1;
581 } else if (arg_match(&arg, &noblitarg, argi)) {
582 noblit = 1;
583 } else if (arg_match(&arg, &progressarg, argi)) {
584 progress = 1;
585 } else if (arg_match(&arg, &limitarg, argi)) {
586 stop_after = arg_parse_uint(&arg);
587 } else if (arg_match(&arg, &skiparg, argi)) {
588 arg_skip = arg_parse_uint(&arg);
589 } else if (arg_match(&arg, &postprocarg, argi)) {
590 postproc = 1;
591 } else if (arg_match(&arg, &md5arg, argi)) {
592 do_md5 = 1;
593 } else if (arg_match(&arg, &framestatsarg, argi)) {
594 framestats_file = fopen(arg.val, "w");
595 if (!framestats_file) {
596 die("Error: Could not open --framestats file (%s) for writing.\n",
597 arg.val);
599 } else if (arg_match(&arg, &summaryarg, argi)) {
600 summary = 1;
601 } else if (arg_match(&arg, &threadsarg, argi)) {
602 cfg.threads = arg_parse_uint(&arg);
604 #if CONFIG_AV1_DECODER
605 else if (arg_match(&arg, &frameparallelarg, argi))
606 frame_parallel = 1;
607 #endif
608 else if (arg_match(&arg, &verbosearg, argi))
609 quiet = 0;
610 else if (arg_match(&arg, &scalearg, argi))
611 do_scale = 1;
612 else if (arg_match(&arg, &fb_arg, argi))
613 num_external_frame_buffers = arg_parse_uint(&arg);
614 else if (arg_match(&arg, &continuearg, argi))
615 keep_going = 1;
616 else if (arg_match(&arg, &outbitdeptharg, argi)) {
617 output_bit_depth = arg_parse_uint(&arg);
618 } else if (arg_match(&arg, &tilem, argi))
619 tile_mode = arg_parse_int(&arg);
620 else if (arg_match(&arg, &tiler, argi))
621 tile_row = arg_parse_int(&arg);
622 else if (arg_match(&arg, &tilec, argi))
623 tile_col = arg_parse_int(&arg);
624 else
625 argj++;
628 /* Check for unrecognized options */
629 for (argi = argv; *argi; argi++)
630 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
631 die("Error: Unrecognized option %s\n", *argi);
633 /* Handle non-option arguments */
634 fn = argv[0];
636 if (!fn) {
637 free(argv);
638 fprintf(stderr, "No input file specified!\n");
639 usage_exit();
641 /* Open file */
642 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
644 if (!infile) {
645 fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
647 #if CONFIG_OS_SUPPORT
648 /* Make sure we don't dump to the terminal, unless forced to with -o - */
649 if (!outfile_pattern && isatty(STDOUT_FILENO) && !do_md5 && !noblit) {
650 fprintf(stderr,
651 "Not dumping raw video to your terminal. Use '-o -' to "
652 "override.\n");
653 return EXIT_FAILURE;
655 #endif
656 input.aom_input_ctx->filename = fn;
657 input.aom_input_ctx->file = infile;
658 if (file_is_ivf(input.aom_input_ctx))
659 input.aom_input_ctx->file_type = FILE_TYPE_IVF;
660 #if CONFIG_WEBM_IO
661 else if (file_is_webm(input.webm_ctx, input.aom_input_ctx))
662 input.aom_input_ctx->file_type = FILE_TYPE_WEBM;
663 #endif
664 #if CONFIG_OBU_NO_IVF
665 else if (file_is_obu(input.aom_input_ctx))
666 input.aom_input_ctx->file_type = FILE_TYPE_OBU;
667 #endif
668 else if (file_is_raw(input.aom_input_ctx))
669 input.aom_input_ctx->file_type = FILE_TYPE_RAW;
670 else {
671 fprintf(stderr, "Unrecognized input file type.\n");
672 #if !CONFIG_WEBM_IO
673 fprintf(stderr, "aomdec was built without WebM container support.\n");
674 #endif
675 return EXIT_FAILURE;
678 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
679 single_file = is_single_file(outfile_pattern);
681 if (!noblit && single_file) {
682 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
683 aom_input_ctx.width, aom_input_ctx.height, 0);
684 if (do_md5)
685 MD5Init(&md5_ctx);
686 else
687 outfile = open_outfile(outfile_name);
690 if (use_y4m && !noblit) {
691 if (!single_file) {
692 fprintf(stderr,
693 "YUV4MPEG2 not supported with output patterns,"
694 " try --i420 or --yv12 or --rawvideo.\n");
695 return EXIT_FAILURE;
698 #if CONFIG_WEBM_IO
699 if (aom_input_ctx.file_type == FILE_TYPE_WEBM) {
700 if (webm_guess_framerate(input.webm_ctx, input.aom_input_ctx)) {
701 fprintf(stderr,
702 "Failed to guess framerate -- error parsing "
703 "webm file?\n");
704 return EXIT_FAILURE;
707 #endif
710 fourcc_interface = get_aom_decoder_by_fourcc(aom_input_ctx.fourcc);
711 if (interface && fourcc_interface && interface != fourcc_interface)
712 warn("Header indicates codec: %s\n", fourcc_interface->name);
713 else
714 interface = fourcc_interface;
716 if (!interface) interface = get_aom_decoder_by_index(0);
718 dec_flags = (postproc ? AOM_CODEC_USE_POSTPROC : 0) |
719 (frame_parallel ? AOM_CODEC_USE_FRAME_THREADING : 0);
720 if (aom_codec_dec_init(&decoder, interface->codec_interface(), &cfg,
721 dec_flags)) {
722 fprintf(stderr, "Failed to initialize decoder: %s\n",
723 aom_codec_error(&decoder));
724 goto fail2;
727 if (!quiet) fprintf(stderr, "%s\n", decoder.name);
729 #if CONFIG_AV1_DECODER
730 if (aom_codec_control(&decoder, AV1_SET_TILE_MODE, tile_mode)) {
731 fprintf(stderr, "Failed to set decode_tile_mode: %s\n",
732 aom_codec_error(&decoder));
733 goto fail;
736 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_ROW, tile_row)) {
737 fprintf(stderr, "Failed to set decode_tile_row: %s\n",
738 aom_codec_error(&decoder));
739 goto fail;
742 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_COL, tile_col)) {
743 fprintf(stderr, "Failed to set decode_tile_col: %s\n",
744 aom_codec_error(&decoder));
745 goto fail;
747 #endif
749 if (arg_skip) fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
750 while (arg_skip) {
751 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) break;
752 arg_skip--;
755 if (num_external_frame_buffers > 0) {
756 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
757 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
758 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
759 if (aom_codec_set_frame_buffer_functions(&decoder, get_av1_frame_buffer,
760 release_av1_frame_buffer,
761 &ext_fb_list)) {
762 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
763 aom_codec_error(&decoder));
764 goto fail;
768 frame_avail = 1;
769 got_data = 0;
771 if (framestats_file) fprintf(framestats_file, "bytes,qp\r\n");
773 /* Decode file */
774 while (frame_avail || got_data) {
775 aom_codec_iter_t iter = NULL;
776 aom_image_t *img;
777 struct aom_usec_timer timer;
778 int corrupted = 0;
780 frame_avail = 0;
781 if (!stop_after || frame_in < stop_after) {
782 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
783 frame_avail = 1;
784 frame_in++;
786 aom_usec_timer_start(&timer);
788 if (aom_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer,
789 NULL)) {
790 const char *detail = aom_codec_error_detail(&decoder);
791 warn("Failed to decode frame %d: %s", frame_in,
792 aom_codec_error(&decoder));
794 if (detail) warn("Additional information: %s", detail);
795 if (!keep_going) goto fail;
798 if (framestats_file) {
799 int qp;
800 if (aom_codec_control(&decoder, AOMD_GET_LAST_QUANTIZER, &qp)) {
801 warn("Failed AOMD_GET_LAST_QUANTIZER: %s",
802 aom_codec_error(&decoder));
803 if (!keep_going) goto fail;
805 fprintf(framestats_file, "%d,%d\r\n", (int)bytes_in_buffer, qp);
808 aom_usec_timer_mark(&timer);
809 dx_time += aom_usec_timer_elapsed(&timer);
810 } else {
811 flush_decoder = 1;
813 } else {
814 flush_decoder = 1;
817 aom_usec_timer_start(&timer);
819 if (flush_decoder) {
820 // Flush the decoder in frame parallel decode.
821 if (aom_codec_decode(&decoder, NULL, 0, NULL)) {
822 warn("Failed to flush decoder: %s", aom_codec_error(&decoder));
826 got_data = 0;
827 if ((img = aom_codec_get_frame(&decoder, &iter))) {
828 ++frame_out;
829 got_data = 1;
832 aom_usec_timer_mark(&timer);
833 dx_time += (unsigned int)aom_usec_timer_elapsed(&timer);
835 if (!frame_parallel &&
836 aom_codec_control(&decoder, AOMD_GET_FRAME_CORRUPTED, &corrupted)) {
837 warn("Failed AOM_GET_FRAME_CORRUPTED: %s", aom_codec_error(&decoder));
838 if (!keep_going) goto fail;
840 frames_corrupted += corrupted;
842 if (progress) show_progress(frame_in, frame_out, dx_time);
844 if (!noblit && img) {
845 const int PLANES_YUV[] = { AOM_PLANE_Y, AOM_PLANE_U, AOM_PLANE_V };
846 const int PLANES_YVU[] = { AOM_PLANE_Y, AOM_PLANE_V, AOM_PLANE_U };
847 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
849 if (do_scale) {
850 if (frame_out == 1) {
851 // If the output frames are to be scaled to a fixed display size then
852 // use the width and height specified in the container. If either of
853 // these is set to 0, use the display size set in the first frame
854 // header. If that is unavailable, use the raw decoded size of the
855 // first decoded frame.
856 int render_width = aom_input_ctx.width;
857 int render_height = aom_input_ctx.height;
858 if (!render_width || !render_height) {
859 int render_size[2];
860 if (aom_codec_control(&decoder, AV1D_GET_DISPLAY_SIZE,
861 render_size)) {
862 // As last resort use size of first frame as display size.
863 render_width = img->d_w;
864 render_height = img->d_h;
865 } else {
866 render_width = render_size[0];
867 render_height = render_size[1];
870 scaled_img =
871 aom_img_alloc(NULL, img->fmt, render_width, render_height, 16);
872 scaled_img->bit_depth = img->bit_depth;
875 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
876 #if CONFIG_LIBYUV
877 libyuv_scale(img, scaled_img, kFilterBox);
878 img = scaled_img;
879 #else
880 fprintf(stderr,
881 "Failed to scale output frame: %s.\n"
882 "libyuv is required for scaling but is currently disabled.\n"
883 "Be sure to specify -DCONFIG_LIBYUV=1 when running cmake.\n",
884 aom_codec_error(&decoder));
885 goto fail;
886 #endif
889 // Default to codec bit depth if output bit depth not set
890 if (!output_bit_depth && single_file && !do_md5) {
891 output_bit_depth = img->bit_depth;
893 // Shift up or down if necessary
894 if (output_bit_depth != 0) {
895 const aom_img_fmt_t shifted_fmt =
896 output_bit_depth == 8
897 ? img->fmt ^ (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
898 : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
900 if (shifted_fmt != img->fmt || output_bit_depth != img->bit_depth) {
901 if (img_shifted &&
902 img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
903 aom_img_free(img_shifted);
904 img_shifted = NULL;
906 if (!img_shifted) {
907 img_shifted =
908 aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
909 img_shifted->bit_depth = output_bit_depth;
911 if (output_bit_depth > img->bit_depth) {
912 aom_img_upshift(img_shifted, img,
913 output_bit_depth - img->bit_depth);
914 } else {
915 aom_img_downshift(img_shifted, img,
916 img->bit_depth - output_bit_depth);
918 img = img_shifted;
922 aom_input_ctx.width = img->d_w;
923 aom_input_ctx.height = img->d_h;
925 #if CONFIG_MONO_VIDEO
926 int num_planes = (!use_y4m && opt_raw && img->monochrome) ? 1 : 3;
927 #else
928 int num_planes = 3;
929 (void)opt_raw;
930 #endif
932 if (single_file) {
933 if (use_y4m) {
934 char y4m_buf[Y4M_BUFFER_SIZE] = { 0 };
935 size_t len = 0;
936 if (img->fmt == AOM_IMG_FMT_I440 || img->fmt == AOM_IMG_FMT_I44016) {
937 fprintf(stderr, "Cannot produce y4m output for 440 sampling.\n");
938 goto fail;
940 if (frame_out == 1) {
941 // Y4M file header
942 len = y4m_write_file_header(
943 y4m_buf, sizeof(y4m_buf), aom_input_ctx.width,
944 aom_input_ctx.height, &aom_input_ctx.framerate, img->fmt,
945 img->bit_depth);
946 if (do_md5) {
947 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
948 } else {
949 fputs(y4m_buf, outfile);
953 // Y4M frame header
954 len = y4m_write_frame_header(y4m_buf, sizeof(y4m_buf));
955 if (do_md5) {
956 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
957 } else {
958 fputs(y4m_buf, outfile);
960 } else {
961 if (frame_out == 1) {
962 // Check if --yv12 or --i420 options are consistent with the
963 // bit-stream decoded
964 if (opt_i420) {
965 if (img->fmt != AOM_IMG_FMT_I420 &&
966 img->fmt != AOM_IMG_FMT_I42016) {
967 fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
968 goto fail;
971 if (opt_yv12) {
972 if ((img->fmt != AOM_IMG_FMT_I420 &&
973 img->fmt != AOM_IMG_FMT_YV12) ||
974 img->bit_depth != 8) {
975 fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
976 goto fail;
982 if (do_md5) {
983 update_image_md5(img, planes, &md5_ctx);
984 } else {
985 write_image_file(img, planes, num_planes, outfile);
987 } else {
988 generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
989 img->d_h, frame_in);
990 if (do_md5) {
991 MD5Init(&md5_ctx);
992 update_image_md5(img, planes, &md5_ctx);
993 MD5Final(md5_digest, &md5_ctx);
994 print_md5(md5_digest, outfile_name);
995 } else {
996 outfile = open_outfile(outfile_name);
997 write_image_file(img, planes, num_planes, outfile);
998 fclose(outfile);
1004 if (summary || progress) {
1005 show_progress(frame_in, frame_out, dx_time);
1006 fprintf(stderr, "\n");
1009 if (frames_corrupted) {
1010 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
1011 } else {
1012 ret = EXIT_SUCCESS;
1015 fail:
1017 if (aom_codec_destroy(&decoder)) {
1018 fprintf(stderr, "Failed to destroy decoder: %s\n",
1019 aom_codec_error(&decoder));
1022 fail2:
1024 if (!noblit && single_file) {
1025 if (do_md5) {
1026 MD5Final(md5_digest, &md5_ctx);
1027 print_md5(md5_digest, outfile_name);
1028 } else {
1029 fclose(outfile);
1033 #if CONFIG_WEBM_IO
1034 if (input.aom_input_ctx->file_type == FILE_TYPE_WEBM)
1035 webm_free(input.webm_ctx);
1036 #endif
1038 if (input.aom_input_ctx->file_type != FILE_TYPE_WEBM) free(buf);
1040 if (scaled_img) aom_img_free(scaled_img);
1041 if (img_shifted) aom_img_free(img_shifted);
1043 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
1044 free(ext_fb_list.ext_fb[i].data);
1046 free(ext_fb_list.ext_fb);
1048 fclose(infile);
1049 if (framestats_file) fclose(framestats_file);
1051 free(argv);
1053 return ret;
1056 int main(int argc, const char **argv_) {
1057 unsigned int loops = 1, i;
1058 char **argv, **argi, **argj;
1059 struct arg arg;
1060 int error = 0;
1062 argv = argv_dup(argc - 1, argv_ + 1);
1063 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1064 memset(&arg, 0, sizeof(arg));
1065 arg.argv_step = 1;
1067 if (arg_match(&arg, &looparg, argi)) {
1068 loops = arg_parse_uint(&arg);
1069 break;
1072 free(argv);
1073 for (i = 0; !error && i < loops; i++) error = main_loop(argc, argv_);
1074 return error;