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.
19 #include "./aom_config.h"
23 #include <unistd.h> // NOLINT
24 #elif !defined(STDOUT_FILENO)
25 #define STDOUT_FILENO 1
30 #include "third_party/libyuv/include/libyuv/scale.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"
44 #include "./md5_utils.h"
50 #include "./tools_common.h"
52 #include "./webmdec.h"
57 static const char *exec_name
;
59 struct AvxDecInputContext
{
60 struct AvxInputContext
*aom_input_ctx
;
61 struct WebmInputContext
*webm_ctx
;
64 static const arg_def_t help
=
65 ARG_DEF(NULL
, "help", 0, "Show usage options and exit");
66 static const arg_def_t looparg
=
67 ARG_DEF(NULL
, "loops", 1, "Number of times to decode the file");
68 static const arg_def_t codecarg
= ARG_DEF(NULL
, "codec", 1, "Codec to use");
69 static const arg_def_t use_yv12
=
70 ARG_DEF(NULL
, "yv12", 0, "Output raw YV12 frames");
71 static const arg_def_t use_i420
=
72 ARG_DEF(NULL
, "i420", 0, "Output raw I420 frames");
73 static const arg_def_t flipuvarg
=
74 ARG_DEF(NULL
, "flipuv", 0, "Flip the chroma planes in the output");
75 static const arg_def_t rawvideo
=
76 ARG_DEF(NULL
, "rawvideo", 0, "Output raw YUV frames");
77 static const arg_def_t noblitarg
=
78 ARG_DEF(NULL
, "noblit", 0, "Don't process the decoded frames");
79 static const arg_def_t progressarg
=
80 ARG_DEF(NULL
, "progress", 0, "Show progress after each frame decodes");
81 static const arg_def_t limitarg
=
82 ARG_DEF(NULL
, "limit", 1, "Stop decoding after n frames");
83 static const arg_def_t skiparg
=
84 ARG_DEF(NULL
, "skip", 1, "Skip the first n input frames");
85 static const arg_def_t postprocarg
=
86 ARG_DEF(NULL
, "postproc", 0, "Postprocess decoded frames");
87 static const arg_def_t summaryarg
=
88 ARG_DEF(NULL
, "summary", 0, "Show timing summary");
89 static const arg_def_t outputfile
=
90 ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
91 static const arg_def_t threadsarg
=
92 ARG_DEF("t", "threads", 1, "Max threads to use");
93 static const arg_def_t frameparallelarg
=
94 ARG_DEF(NULL
, "frame-parallel", 0, "Frame parallel decode");
95 static const arg_def_t verbosearg
=
96 ARG_DEF("v", "verbose", 0, "Show version string");
97 static const arg_def_t scalearg
=
98 ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
99 static const arg_def_t continuearg
=
100 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
101 static const arg_def_t fb_arg
=
102 ARG_DEF(NULL
, "frame-buffers", 1, "Number of frame buffers to use");
103 static const arg_def_t md5arg
=
104 ARG_DEF(NULL
, "md5", 0, "Compute the MD5 sum of the decoded frame");
105 static const arg_def_t framestatsarg
=
106 ARG_DEF(NULL
, "framestats", 1, "Output per-frame stats (.csv format)");
107 #if CONFIG_HIGHBITDEPTH
108 static const arg_def_t outbitdeptharg
=
109 ARG_DEF(NULL
, "output-bit-depth", 1, "Output bit-depth for decoded frames");
112 static const arg_def_t tiler
= ARG_DEF(NULL
, "tile-row", 1,
113 "Row index of tile to decode "
114 "(-1 for all rows)");
115 static const arg_def_t tilec
= ARG_DEF(NULL
, "tile-column", 1,
116 "Column index of tile to decode "
117 "(-1 for all columns)");
118 #endif // CONFIG_EXT_TILE
120 static const arg_def_t
*all_args
[] = { &help
,
141 #if CONFIG_HIGHBITDEPTH
147 #endif // CONFIG_EXT_TILE
151 static INLINE
int libyuv_scale(aom_image_t
*src
, aom_image_t
*dst
,
152 FilterModeEnum mode
) {
153 #if CONFIG_HIGHBITDEPTH
154 if (src
->fmt
== AOM_IMG_FMT_I42016
) {
155 assert(dst
->fmt
== AOM_IMG_FMT_I42016
);
157 (uint16_t *)src
->planes
[AOM_PLANE_Y
], src
->stride
[AOM_PLANE_Y
] / 2,
158 (uint16_t *)src
->planes
[AOM_PLANE_U
], src
->stride
[AOM_PLANE_U
] / 2,
159 (uint16_t *)src
->planes
[AOM_PLANE_V
], src
->stride
[AOM_PLANE_V
] / 2,
160 src
->d_w
, src
->d_h
, (uint16_t *)dst
->planes
[AOM_PLANE_Y
],
161 dst
->stride
[AOM_PLANE_Y
] / 2, (uint16_t *)dst
->planes
[AOM_PLANE_U
],
162 dst
->stride
[AOM_PLANE_U
] / 2, (uint16_t *)dst
->planes
[AOM_PLANE_V
],
163 dst
->stride
[AOM_PLANE_V
] / 2, dst
->d_w
, dst
->d_h
, mode
);
166 assert(src
->fmt
== AOM_IMG_FMT_I420
);
167 assert(dst
->fmt
== AOM_IMG_FMT_I420
);
168 return I420Scale(src
->planes
[AOM_PLANE_Y
], src
->stride
[AOM_PLANE_Y
],
169 src
->planes
[AOM_PLANE_U
], src
->stride
[AOM_PLANE_U
],
170 src
->planes
[AOM_PLANE_V
], src
->stride
[AOM_PLANE_V
], src
->d_w
,
171 src
->d_h
, dst
->planes
[AOM_PLANE_Y
], dst
->stride
[AOM_PLANE_Y
],
172 dst
->planes
[AOM_PLANE_U
], dst
->stride
[AOM_PLANE_U
],
173 dst
->planes
[AOM_PLANE_V
], dst
->stride
[AOM_PLANE_V
], dst
->d_w
,
178 void show_help(FILE *fout
, int shorthelp
) {
179 fprintf(fout
, "Usage: %s <options> filename\n\n", exec_name
);
182 fprintf(fout
, "Use --help to see the full list of options.\n");
186 fprintf(fout
, "Options:\n");
187 arg_show_usage(fout
, all_args
);
189 "\nOutput File Patterns:\n\n"
190 " The -o argument specifies the name of the file(s) to "
191 "write to. If the\n argument does not include any escape "
192 "characters, the output will be\n written to a single file. "
193 "Otherwise, the filename will be calculated by\n expanding "
194 "the following escape characters:\n");
196 "\n\t%%w - Frame width"
197 "\n\t%%h - Frame height"
198 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
199 "\n\n Pattern arguments are only supported in conjunction "
200 "with the --yv12 and\n --i420 options. If the -o option is "
201 "not specified, the output will be\n directed to stdout.\n");
202 fprintf(fout
, "\nIncluded decoders:\n\n");
204 for (int i
= 0; i
< get_aom_decoder_count(); ++i
) {
205 const AvxInterface
*const decoder
= get_aom_decoder_by_index(i
);
206 fprintf(fout
, " %-6s - %s\n", decoder
->name
,
207 aom_codec_iface_name(decoder
->codec_interface()));
211 void usage_exit(void) {
212 show_help(stderr
, 1);
216 static int raw_read_frame(FILE *infile
, uint8_t **buffer
, size_t *bytes_read
,
217 size_t *buffer_size
) {
218 char raw_hdr
[RAW_FRAME_HDR_SZ
];
219 size_t frame_size
= 0;
221 if (fread(raw_hdr
, RAW_FRAME_HDR_SZ
, 1, infile
) != 1) {
222 if (!feof(infile
)) warn("Failed to read RAW frame size\n");
224 const size_t kCorruptFrameThreshold
= 256 * 1024 * 1024;
225 const size_t kFrameTooSmallThreshold
= 256 * 1024;
226 frame_size
= mem_get_le32(raw_hdr
);
228 if (frame_size
> kCorruptFrameThreshold
) {
229 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size
);
233 if (frame_size
< kFrameTooSmallThreshold
) {
234 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
235 (unsigned int)frame_size
);
238 if (frame_size
> *buffer_size
) {
239 uint8_t *new_buf
= realloc(*buffer
, 2 * frame_size
);
242 *buffer_size
= 2 * frame_size
;
244 warn("Failed to allocate compressed data buffer\n");
251 if (fread(*buffer
, 1, frame_size
, infile
) != frame_size
) {
252 warn("Failed to read full frame\n");
255 *bytes_read
= frame_size
;
261 static int read_frame(struct AvxDecInputContext
*input
, uint8_t **buf
,
262 size_t *bytes_in_buffer
, size_t *buffer_size
) {
263 switch (input
->aom_input_ctx
->file_type
) {
266 return webm_read_frame(input
->webm_ctx
, buf
, bytes_in_buffer
);
269 return raw_read_frame(input
->aom_input_ctx
->file
, buf
, bytes_in_buffer
,
272 return ivf_read_frame(input
->aom_input_ctx
->file
, buf
, bytes_in_buffer
,
274 #if CONFIG_OBU_NO_IVF
276 return obu_read_temporal_unit(input
->aom_input_ctx
->file
, buf
,
277 bytes_in_buffer
, buffer_size
);
283 static void update_image_md5(const aom_image_t
*img
, const int planes
[3],
287 for (i
= 0; i
< 3; ++i
) {
288 const int plane
= planes
[i
];
289 const unsigned char *buf
= img
->planes
[plane
];
290 const int stride
= img
->stride
[plane
];
291 const int w
= aom_img_plane_width(img
, plane
) *
292 ((img
->fmt
& AOM_IMG_FMT_HIGHBITDEPTH
) ? 2 : 1);
293 const int h
= aom_img_plane_height(img
, plane
);
295 for (y
= 0; y
< h
; ++y
) {
296 MD5Update(md5
, buf
, w
);
302 static void write_image_file(const aom_image_t
*img
, const int *planes
,
303 const int num_planes
, FILE *file
) {
305 #if CONFIG_HIGHBITDEPTH
306 const int bytes_per_sample
= ((img
->fmt
& AOM_IMG_FMT_HIGHBITDEPTH
) ? 2 : 1);
308 const int bytes_per_sample
= 1;
311 for (i
= 0; i
< num_planes
; ++i
) {
312 const int plane
= planes
[i
];
313 const unsigned char *buf
= img
->planes
[plane
];
314 const int stride
= img
->stride
[plane
];
315 const int w
= aom_img_plane_width(img
, plane
);
316 const int h
= aom_img_plane_height(img
, plane
);
318 for (y
= 0; y
< h
; ++y
) {
319 fwrite(buf
, bytes_per_sample
, w
, file
);
325 static int file_is_raw(struct AvxInputContext
*input
) {
328 aom_codec_stream_info_t si
;
330 if (fread(buf
, 1, 32, input
->file
) == 32) {
333 if (mem_get_le32(buf
) < 256 * 1024 * 1024) {
334 for (i
= 0; i
< get_aom_decoder_count(); ++i
) {
335 const AvxInterface
*const decoder
= get_aom_decoder_by_index(i
);
336 if (!aom_codec_peek_stream_info(decoder
->codec_interface(), buf
+ 4,
339 input
->fourcc
= decoder
->fourcc
;
341 input
->height
= si
.h
;
342 input
->framerate
.numerator
= 30;
343 input
->framerate
.denominator
= 1;
354 static void show_progress(int frame_in
, int frame_out
, uint64_t dx_time
) {
356 "%d decoded frames/%d showed frames in %" PRId64
" us (%.2f fps)\r",
357 frame_in
, frame_out
, dx_time
,
358 (double)frame_out
* 1000000.0 / (double)dx_time
);
361 struct ExternalFrameBuffer
{
367 struct ExternalFrameBufferList
{
368 int num_external_frame_buffers
;
369 struct ExternalFrameBuffer
*ext_fb
;
372 // Callback used by libaom to request an external frame buffer. |cb_priv|
373 // Application private data passed into the set function. |min_size| is the
374 // minimum size in bytes needed to decode the next frame. |fb| pointer to the
376 static int get_av1_frame_buffer(void *cb_priv
, size_t min_size
,
377 aom_codec_frame_buffer_t
*fb
) {
379 struct ExternalFrameBufferList
*const ext_fb_list
=
380 (struct ExternalFrameBufferList
*)cb_priv
;
381 if (ext_fb_list
== NULL
) return -1;
383 // Find a free frame buffer.
384 for (i
= 0; i
< ext_fb_list
->num_external_frame_buffers
; ++i
) {
385 if (!ext_fb_list
->ext_fb
[i
].in_use
) break;
388 if (i
== ext_fb_list
->num_external_frame_buffers
) return -1;
390 if (ext_fb_list
->ext_fb
[i
].size
< min_size
) {
391 free(ext_fb_list
->ext_fb
[i
].data
);
392 ext_fb_list
->ext_fb
[i
].data
= (uint8_t *)calloc(min_size
, sizeof(uint8_t));
393 if (!ext_fb_list
->ext_fb
[i
].data
) return -1;
395 ext_fb_list
->ext_fb
[i
].size
= min_size
;
398 fb
->data
= ext_fb_list
->ext_fb
[i
].data
;
399 fb
->size
= ext_fb_list
->ext_fb
[i
].size
;
400 ext_fb_list
->ext_fb
[i
].in_use
= 1;
402 // Set the frame buffer's private data to point at the external frame buffer.
403 fb
->priv
= &ext_fb_list
->ext_fb
[i
];
407 // Callback used by libaom when there are no references to the frame buffer.
408 // |cb_priv| user private data passed into the set function. |fb| pointer
409 // to the frame buffer.
410 static int release_av1_frame_buffer(void *cb_priv
,
411 aom_codec_frame_buffer_t
*fb
) {
412 struct ExternalFrameBuffer
*const ext_fb
=
413 (struct ExternalFrameBuffer
*)fb
->priv
;
419 static void generate_filename(const char *pattern
, char *out
, size_t q_len
,
420 unsigned int d_w
, unsigned int d_h
,
421 unsigned int frame_in
) {
422 const char *p
= pattern
;
426 char *next_pat
= strchr(p
, '%');
431 /* parse the pattern */
434 case 'w': snprintf(q
, q_len
- 1, "%d", d_w
); break;
435 case 'h': snprintf(q
, q_len
- 1, "%d", d_h
); break;
436 case '1': snprintf(q
, q_len
- 1, "%d", frame_in
); break;
437 case '2': snprintf(q
, q_len
- 1, "%02d", frame_in
); break;
438 case '3': snprintf(q
, q_len
- 1, "%03d", frame_in
); break;
439 case '4': snprintf(q
, q_len
- 1, "%04d", frame_in
); break;
440 case '5': snprintf(q
, q_len
- 1, "%05d", frame_in
); break;
441 case '6': snprintf(q
, q_len
- 1, "%06d", frame_in
); break;
442 case '7': snprintf(q
, q_len
- 1, "%07d", frame_in
); break;
443 case '8': snprintf(q
, q_len
- 1, "%08d", frame_in
); break;
444 case '9': snprintf(q
, q_len
- 1, "%09d", frame_in
); break;
445 default: die("Unrecognized pattern %%%c\n", p
[1]); break;
449 if (pat_len
>= q_len
- 1) die("Output filename too long.\n");
456 /* copy the next segment */
458 copy_len
= strlen(p
);
460 copy_len
= next_pat
- p
;
462 if (copy_len
>= q_len
- 1) die("Output filename too long.\n");
464 memcpy(q
, p
, copy_len
);
473 static int is_single_file(const char *outfile_pattern
) {
474 const char *p
= outfile_pattern
;
478 if (p
&& p
[1] >= '1' && p
[1] <= '9')
479 return 0; // pattern contains sequence number, so it's not unique
486 static void print_md5(unsigned char digest
[16], const char *filename
) {
489 for (i
= 0; i
< 16; ++i
) printf("%02x", digest
[i
]);
490 printf(" %s\n", filename
);
493 static FILE *open_outfile(const char *name
) {
494 if (strcmp("-", name
) == 0) {
495 set_binary_mode(stdout
);
498 FILE *file
= fopen(name
, "wb");
499 if (!file
) fatal("Failed to open output file '%s'", name
);
504 #if CONFIG_HIGHBITDEPTH
505 static int img_shifted_realloc_required(const aom_image_t
*img
,
506 const aom_image_t
*shifted
,
507 aom_img_fmt_t required_fmt
) {
508 return img
->d_w
!= shifted
->d_w
|| img
->d_h
!= shifted
->d_h
||
509 required_fmt
!= shifted
->fmt
;
513 static int main_loop(int argc
, const char **argv_
) {
514 aom_codec_ctx_t decoder
;
517 int ret
= EXIT_FAILURE
;
519 size_t bytes_in_buffer
= 0, buffer_size
= 0;
521 int frame_in
= 0, frame_out
= 0, flipuv
= 0, noblit
= 0;
522 int do_md5
= 0, progress
= 0, frame_parallel
= 0;
523 int stop_after
= 0, postproc
= 0, summary
= 0, quiet
= 1;
526 const AvxInterface
*interface
= NULL
;
527 const AvxInterface
*fourcc_interface
= NULL
;
528 uint64_t dx_time
= 0;
530 char **argv
, **argi
, **argj
;
536 aom_codec_dec_cfg_t cfg
= { 0, 0, 0, CONFIG_LOWBITDEPTH
, 0 };
537 #if CONFIG_HIGHBITDEPTH
538 unsigned int output_bit_depth
= 0;
543 #endif // CONFIG_EXT_TILE
544 int frames_corrupted
= 0;
547 aom_image_t
*scaled_img
= NULL
;
548 #if CONFIG_HIGHBITDEPTH
549 aom_image_t
*img_shifted
= NULL
;
551 int frame_avail
, got_data
, flush_decoder
= 0;
552 int num_external_frame_buffers
= 0;
553 struct ExternalFrameBufferList ext_fb_list
= { 0, NULL
};
555 const char *outfile_pattern
= NULL
;
556 char outfile_name
[PATH_MAX
] = { 0 };
557 FILE *outfile
= NULL
;
559 FILE *framestats_file
= NULL
;
562 unsigned char md5_digest
[16];
564 struct AvxDecInputContext input
= { NULL
, NULL
};
565 struct AvxInputContext aom_input_ctx
;
567 struct WebmInputContext webm_ctx
;
568 memset(&(webm_ctx
), 0, sizeof(webm_ctx
));
569 input
.webm_ctx
= &webm_ctx
;
571 input
.aom_input_ctx
= &aom_input_ctx
;
573 /* Parse command line */
574 exec_name
= argv_
[0];
575 argv
= argv_dup(argc
- 1, argv_
+ 1);
577 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
) {
578 memset(&arg
, 0, sizeof(arg
));
581 if (arg_match(&arg
, &help
, argi
)) {
582 show_help(stdout
, 0);
584 } else if (arg_match(&arg
, &codecarg
, argi
)) {
585 interface
= get_aom_decoder_by_name(arg
.val
);
587 die("Error: Unrecognized argument (%s) to --codec\n", arg
.val
);
588 } else if (arg_match(&arg
, &looparg
, argi
)) {
590 } else if (arg_match(&arg
, &outputfile
, argi
)) {
591 outfile_pattern
= arg
.val
;
592 } else if (arg_match(&arg
, &use_yv12
, argi
)) {
596 } else if (arg_match(&arg
, &use_i420
, argi
)) {
600 } else if (arg_match(&arg
, &rawvideo
, argi
)) {
602 } else if (arg_match(&arg
, &flipuvarg
, argi
)) {
604 } else if (arg_match(&arg
, &noblitarg
, argi
)) {
606 } else if (arg_match(&arg
, &progressarg
, argi
)) {
608 } else if (arg_match(&arg
, &limitarg
, argi
)) {
609 stop_after
= arg_parse_uint(&arg
);
610 } else if (arg_match(&arg
, &skiparg
, argi
)) {
611 arg_skip
= arg_parse_uint(&arg
);
612 } else if (arg_match(&arg
, &postprocarg
, argi
)) {
614 } else if (arg_match(&arg
, &md5arg
, argi
)) {
616 } else if (arg_match(&arg
, &framestatsarg
, argi
)) {
617 framestats_file
= fopen(arg
.val
, "w");
618 if (!framestats_file
) {
619 die("Error: Could not open --framestats file (%s) for writing.\n",
622 } else if (arg_match(&arg
, &summaryarg
, argi
)) {
624 } else if (arg_match(&arg
, &threadsarg
, argi
)) {
625 cfg
.threads
= arg_parse_uint(&arg
);
627 #if CONFIG_AV1_DECODER
628 else if (arg_match(&arg
, &frameparallelarg
, argi
))
631 else if (arg_match(&arg
, &verbosearg
, argi
))
633 else if (arg_match(&arg
, &scalearg
, argi
))
635 else if (arg_match(&arg
, &fb_arg
, argi
))
636 num_external_frame_buffers
= arg_parse_uint(&arg
);
637 else if (arg_match(&arg
, &continuearg
, argi
))
639 #if CONFIG_HIGHBITDEPTH
640 else if (arg_match(&arg
, &outbitdeptharg
, argi
)) {
641 output_bit_depth
= arg_parse_uint(&arg
);
645 else if (arg_match(&arg
, &tiler
, argi
))
646 tile_row
= arg_parse_int(&arg
);
647 else if (arg_match(&arg
, &tilec
, argi
))
648 tile_col
= arg_parse_int(&arg
);
649 #endif // CONFIG_EXT_TILE
654 /* Check for unrecognized options */
655 for (argi
= argv
; *argi
; argi
++)
656 if (argi
[0][0] == '-' && strlen(argi
[0]) > 1)
657 die("Error: Unrecognized option %s\n", *argi
);
659 /* Handle non-option arguments */
664 fprintf(stderr
, "No input file specified!\n");
668 infile
= strcmp(fn
, "-") ? fopen(fn
, "rb") : set_binary_mode(stdin
);
671 fatal("Failed to open input file '%s'", strcmp(fn
, "-") ? fn
: "stdin");
673 #if CONFIG_OS_SUPPORT
674 /* Make sure we don't dump to the terminal, unless forced to with -o - */
675 if (!outfile_pattern
&& isatty(STDOUT_FILENO
) && !do_md5
&& !noblit
) {
677 "Not dumping raw video to your terminal. Use '-o -' to "
682 input
.aom_input_ctx
->file
= infile
;
683 if (file_is_ivf(input
.aom_input_ctx
))
684 input
.aom_input_ctx
->file_type
= FILE_TYPE_IVF
;
686 else if (file_is_webm(input
.webm_ctx
, input
.aom_input_ctx
))
687 input
.aom_input_ctx
->file_type
= FILE_TYPE_WEBM
;
689 #if CONFIG_OBU_NO_IVF
690 else if (file_is_obu(input
.aom_input_ctx
))
691 input
.aom_input_ctx
->file_type
= FILE_TYPE_OBU
;
693 else if (file_is_raw(input
.aom_input_ctx
))
694 input
.aom_input_ctx
->file_type
= FILE_TYPE_RAW
;
696 fprintf(stderr
, "Unrecognized input file type.\n");
698 fprintf(stderr
, "aomdec was built without WebM container support.\n");
703 outfile_pattern
= outfile_pattern
? outfile_pattern
: "-";
704 single_file
= is_single_file(outfile_pattern
);
706 if (!noblit
&& single_file
) {
707 generate_filename(outfile_pattern
, outfile_name
, PATH_MAX
,
708 aom_input_ctx
.width
, aom_input_ctx
.height
, 0);
712 outfile
= open_outfile(outfile_name
);
715 if (use_y4m
&& !noblit
) {
718 "YUV4MPEG2 not supported with output patterns,"
719 " try --i420 or --yv12 or --rawvideo.\n");
724 if (aom_input_ctx
.file_type
== FILE_TYPE_WEBM
) {
725 if (webm_guess_framerate(input
.webm_ctx
, input
.aom_input_ctx
)) {
727 "Failed to guess framerate -- error parsing "
735 fourcc_interface
= get_aom_decoder_by_fourcc(aom_input_ctx
.fourcc
);
736 if (interface
&& fourcc_interface
&& interface
!= fourcc_interface
)
737 warn("Header indicates codec: %s\n", fourcc_interface
->name
);
739 interface
= fourcc_interface
;
741 if (!interface
) interface
= get_aom_decoder_by_index(0);
743 dec_flags
= (postproc
? AOM_CODEC_USE_POSTPROC
: 0) |
744 (frame_parallel
? AOM_CODEC_USE_FRAME_THREADING
: 0);
745 if (aom_codec_dec_init(&decoder
, interface
->codec_interface(), &cfg
,
747 fprintf(stderr
, "Failed to initialize decoder: %s\n",
748 aom_codec_error(&decoder
));
752 if (!quiet
) fprintf(stderr
, "%s\n", decoder
.name
);
754 #if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
755 if (aom_codec_control(&decoder
, AV1_SET_DECODE_TILE_ROW
, tile_row
)) {
756 fprintf(stderr
, "Failed to set decode_tile_row: %s\n",
757 aom_codec_error(&decoder
));
761 if (aom_codec_control(&decoder
, AV1_SET_DECODE_TILE_COL
, tile_col
)) {
762 fprintf(stderr
, "Failed to set decode_tile_col: %s\n",
763 aom_codec_error(&decoder
));
768 if (arg_skip
) fprintf(stderr
, "Skipping first %d frames.\n", arg_skip
);
770 if (read_frame(&input
, &buf
, &bytes_in_buffer
, &buffer_size
)) break;
774 if (num_external_frame_buffers
> 0) {
775 ext_fb_list
.num_external_frame_buffers
= num_external_frame_buffers
;
776 ext_fb_list
.ext_fb
= (struct ExternalFrameBuffer
*)calloc(
777 num_external_frame_buffers
, sizeof(*ext_fb_list
.ext_fb
));
778 if (aom_codec_set_frame_buffer_functions(&decoder
, get_av1_frame_buffer
,
779 release_av1_frame_buffer
,
781 fprintf(stderr
, "Failed to configure external frame buffers: %s\n",
782 aom_codec_error(&decoder
));
790 if (framestats_file
) fprintf(framestats_file
, "bytes,qp\r\n");
793 while (frame_avail
|| got_data
) {
794 aom_codec_iter_t iter
= NULL
;
796 struct aom_usec_timer timer
;
800 if (!stop_after
|| frame_in
< stop_after
) {
801 if (!read_frame(&input
, &buf
, &bytes_in_buffer
, &buffer_size
)) {
805 aom_usec_timer_start(&timer
);
807 if (aom_codec_decode(&decoder
, buf
, (unsigned int)bytes_in_buffer
, NULL
,
809 const char *detail
= aom_codec_error_detail(&decoder
);
810 warn("Failed to decode frame %d: %s", frame_in
,
811 aom_codec_error(&decoder
));
813 if (detail
) warn("Additional information: %s", detail
);
814 if (!keep_going
) goto fail
;
817 if (framestats_file
) {
819 if (aom_codec_control(&decoder
, AOMD_GET_LAST_QUANTIZER
, &qp
)) {
820 warn("Failed AOMD_GET_LAST_QUANTIZER: %s",
821 aom_codec_error(&decoder
));
822 if (!keep_going
) goto fail
;
824 fprintf(framestats_file
, "%d,%d\r\n", (int)bytes_in_buffer
, qp
);
827 aom_usec_timer_mark(&timer
);
828 dx_time
+= aom_usec_timer_elapsed(&timer
);
836 aom_usec_timer_start(&timer
);
839 // Flush the decoder in frame parallel decode.
840 if (aom_codec_decode(&decoder
, NULL
, 0, NULL
, 0)) {
841 warn("Failed to flush decoder: %s", aom_codec_error(&decoder
));
846 if ((img
= aom_codec_get_frame(&decoder
, &iter
))) {
851 aom_usec_timer_mark(&timer
);
852 dx_time
+= (unsigned int)aom_usec_timer_elapsed(&timer
);
854 if (!frame_parallel
&&
855 aom_codec_control(&decoder
, AOMD_GET_FRAME_CORRUPTED
, &corrupted
)) {
856 warn("Failed AOM_GET_FRAME_CORRUPTED: %s", aom_codec_error(&decoder
));
857 if (!keep_going
) goto fail
;
859 frames_corrupted
+= corrupted
;
861 if (progress
) show_progress(frame_in
, frame_out
, dx_time
);
863 if (!noblit
&& img
) {
864 const int PLANES_YUV
[] = { AOM_PLANE_Y
, AOM_PLANE_U
, AOM_PLANE_V
};
865 const int PLANES_YVU
[] = { AOM_PLANE_Y
, AOM_PLANE_V
, AOM_PLANE_U
};
866 const int *planes
= flipuv
? PLANES_YVU
: PLANES_YUV
;
869 if (frame_out
== 1) {
870 // If the output frames are to be scaled to a fixed display size then
871 // use the width and height specified in the container. If either of
872 // these is set to 0, use the display size set in the first frame
873 // header. If that is unavailable, use the raw decoded size of the
874 // first decoded frame.
875 int render_width
= aom_input_ctx
.width
;
876 int render_height
= aom_input_ctx
.height
;
877 if (!render_width
|| !render_height
) {
879 if (aom_codec_control(&decoder
, AV1D_GET_DISPLAY_SIZE
,
881 // As last resort use size of first frame as display size.
882 render_width
= img
->d_w
;
883 render_height
= img
->d_h
;
885 render_width
= render_size
[0];
886 render_height
= render_size
[1];
890 aom_img_alloc(NULL
, img
->fmt
, render_width
, render_height
, 16);
891 scaled_img
->bit_depth
= img
->bit_depth
;
894 if (img
->d_w
!= scaled_img
->d_w
|| img
->d_h
!= scaled_img
->d_h
) {
896 libyuv_scale(img
, scaled_img
, kFilterBox
);
900 "Failed to scale output frame: %s.\n"
901 "Scaling is disabled in this configuration. "
902 "To enable scaling, configure with --enable-libyuv\n",
903 aom_codec_error(&decoder
));
908 #if CONFIG_HIGHBITDEPTH
909 // Default to codec bit depth if output bit depth not set
910 if (!output_bit_depth
&& single_file
&& !do_md5
) {
911 output_bit_depth
= img
->bit_depth
;
913 // Shift up or down if necessary
914 if (output_bit_depth
!= 0) {
915 const aom_img_fmt_t shifted_fmt
=
916 output_bit_depth
== 8
917 ? img
->fmt
^ (img
->fmt
& AOM_IMG_FMT_HIGHBITDEPTH
)
918 : img
->fmt
| AOM_IMG_FMT_HIGHBITDEPTH
;
920 if (shifted_fmt
!= img
->fmt
|| output_bit_depth
!= img
->bit_depth
) {
922 img_shifted_realloc_required(img
, img_shifted
, shifted_fmt
)) {
923 aom_img_free(img_shifted
);
928 aom_img_alloc(NULL
, shifted_fmt
, img
->d_w
, img
->d_h
, 16);
929 img_shifted
->bit_depth
= output_bit_depth
;
931 if (output_bit_depth
> img
->bit_depth
) {
932 aom_img_upshift(img_shifted
, img
,
933 output_bit_depth
- img
->bit_depth
);
935 aom_img_downshift(img_shifted
, img
,
936 img
->bit_depth
- output_bit_depth
);
944 aom_input_ctx
.width
= img
->d_w
;
945 aom_input_ctx
.height
= img
->d_h
;
946 #endif // CONFIG_EXT_TILE
948 #if CONFIG_MONO_VIDEO
949 int num_planes
= (!use_y4m
&& img
->cs
== AOM_CS_MONOCHROME
) ? 1 : 3;
956 char y4m_buf
[Y4M_BUFFER_SIZE
] = { 0 };
958 if (img
->fmt
== AOM_IMG_FMT_I440
|| img
->fmt
== AOM_IMG_FMT_I44016
) {
959 fprintf(stderr
, "Cannot produce y4m output for 440 sampling.\n");
962 if (frame_out
== 1) {
964 len
= y4m_write_file_header(
965 y4m_buf
, sizeof(y4m_buf
), aom_input_ctx
.width
,
966 aom_input_ctx
.height
, &aom_input_ctx
.framerate
, img
->fmt
,
969 MD5Update(&md5_ctx
, (md5byte
*)y4m_buf
, (unsigned int)len
);
971 fputs(y4m_buf
, outfile
);
976 len
= y4m_write_frame_header(y4m_buf
, sizeof(y4m_buf
));
978 MD5Update(&md5_ctx
, (md5byte
*)y4m_buf
, (unsigned int)len
);
980 fputs(y4m_buf
, outfile
);
983 if (frame_out
== 1) {
984 // Check if --yv12 or --i420 options are consistent with the
985 // bit-stream decoded
987 if (img
->fmt
!= AOM_IMG_FMT_I420
&&
988 img
->fmt
!= AOM_IMG_FMT_I42016
) {
989 fprintf(stderr
, "Cannot produce i420 output for bit-stream.\n");
994 if ((img
->fmt
!= AOM_IMG_FMT_I420
&&
995 img
->fmt
!= AOM_IMG_FMT_YV12
) ||
996 img
->bit_depth
!= 8) {
997 fprintf(stderr
, "Cannot produce yv12 output for bit-stream.\n");
1005 update_image_md5(img
, planes
, &md5_ctx
);
1007 write_image_file(img
, planes
, num_planes
, outfile
);
1010 generate_filename(outfile_pattern
, outfile_name
, PATH_MAX
, img
->d_w
,
1011 img
->d_h
, frame_in
);
1014 update_image_md5(img
, planes
, &md5_ctx
);
1015 MD5Final(md5_digest
, &md5_ctx
);
1016 print_md5(md5_digest
, outfile_name
);
1018 outfile
= open_outfile(outfile_name
);
1019 write_image_file(img
, planes
, num_planes
, outfile
);
1026 if (summary
|| progress
) {
1027 show_progress(frame_in
, frame_out
, dx_time
);
1028 fprintf(stderr
, "\n");
1031 if (frames_corrupted
) {
1032 fprintf(stderr
, "WARNING: %d frames corrupted.\n", frames_corrupted
);
1039 if (aom_codec_destroy(&decoder
)) {
1040 fprintf(stderr
, "Failed to destroy decoder: %s\n",
1041 aom_codec_error(&decoder
));
1046 if (!noblit
&& single_file
) {
1048 MD5Final(md5_digest
, &md5_ctx
);
1049 print_md5(md5_digest
, outfile_name
);
1056 if (input
.aom_input_ctx
->file_type
== FILE_TYPE_WEBM
)
1057 webm_free(input
.webm_ctx
);
1060 if (input
.aom_input_ctx
->file_type
!= FILE_TYPE_WEBM
) free(buf
);
1062 if (scaled_img
) aom_img_free(scaled_img
);
1063 #if CONFIG_HIGHBITDEPTH
1064 if (img_shifted
) aom_img_free(img_shifted
);
1067 for (i
= 0; i
< ext_fb_list
.num_external_frame_buffers
; ++i
) {
1068 free(ext_fb_list
.ext_fb
[i
].data
);
1070 free(ext_fb_list
.ext_fb
);
1073 if (framestats_file
) fclose(framestats_file
);
1080 int main(int argc
, const char **argv_
) {
1081 unsigned int loops
= 1, i
;
1082 char **argv
, **argi
, **argj
;
1086 argv
= argv_dup(argc
- 1, argv_
+ 1);
1087 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
) {
1088 memset(&arg
, 0, sizeof(arg
));
1091 if (arg_match(&arg
, &looparg
, argi
)) {
1092 loops
= arg_parse_uint(&arg
);
1097 for (i
= 0; !error
&& i
< loops
; i
++) error
= main_loop(argc
, argv_
);