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