AOMedia Codec SDK: aomdec

AOMedia Codec SDK

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