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 verbosearg
=
94 ARG_DEF("v", "verbose", 0, "Show version string");
95 static const arg_def_t scalearg
=
96 ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
97 static const arg_def_t continuearg
=
98 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
99 static const arg_def_t fb_arg
=
100 ARG_DEF(NULL
, "frame-buffers", 1, "Number of frame buffers to use");
101 static const arg_def_t md5arg
=
102 ARG_DEF(NULL
, "md5", 0, "Compute the MD5 sum of the decoded frame");
103 static const arg_def_t framestatsarg
=
104 ARG_DEF(NULL
, "framestats", 1, "Output per-frame stats (.csv format)");
105 static const arg_def_t outbitdeptharg
=
106 ARG_DEF(NULL
, "output-bit-depth", 1, "Output bit-depth for decoded frames");
107 static const arg_def_t tilem
= ARG_DEF(NULL
, "tile-mode", 1,
109 "(0 for normal tile coding mode)");
110 static const arg_def_t tiler
= ARG_DEF(NULL
, "tile-row", 1,
111 "Row index of tile to decode "
112 "(-1 for all rows)");
113 static const arg_def_t tilec
= ARG_DEF(NULL
, "tile-column", 1,
114 "Column index of tile to decode "
115 "(-1 for all columns)");
117 static const arg_def_t
*all_args
[] = {
118 &help
, &codecarg
, &use_yv12
, &use_i420
, &flipuvarg
,
119 &rawvideo
, &noblitarg
, &progressarg
, &limitarg
, &skiparg
,
120 &postprocarg
, &summaryarg
, &outputfile
, &threadsarg
, &verbosearg
,
121 &scalearg
, &fb_arg
, &md5arg
, &framestatsarg
, &continuearg
,
122 &outbitdeptharg
, &tilem
, &tiler
, &tilec
, NULL
126 static INLINE
int libyuv_scale(aom_image_t
*src
, aom_image_t
*dst
,
127 FilterModeEnum mode
) {
128 if (src
->fmt
== AOM_IMG_FMT_I42016
) {
129 assert(dst
->fmt
== AOM_IMG_FMT_I42016
);
131 (uint16_t *)src
->planes
[AOM_PLANE_Y
], src
->stride
[AOM_PLANE_Y
] / 2,
132 (uint16_t *)src
->planes
[AOM_PLANE_U
], src
->stride
[AOM_PLANE_U
] / 2,
133 (uint16_t *)src
->planes
[AOM_PLANE_V
], src
->stride
[AOM_PLANE_V
] / 2,
134 src
->d_w
, src
->d_h
, (uint16_t *)dst
->planes
[AOM_PLANE_Y
],
135 dst
->stride
[AOM_PLANE_Y
] / 2, (uint16_t *)dst
->planes
[AOM_PLANE_U
],
136 dst
->stride
[AOM_PLANE_U
] / 2, (uint16_t *)dst
->planes
[AOM_PLANE_V
],
137 dst
->stride
[AOM_PLANE_V
] / 2, dst
->d_w
, dst
->d_h
, mode
);
139 assert(src
->fmt
== AOM_IMG_FMT_I420
);
140 assert(dst
->fmt
== AOM_IMG_FMT_I420
);
141 return I420Scale(src
->planes
[AOM_PLANE_Y
], src
->stride
[AOM_PLANE_Y
],
142 src
->planes
[AOM_PLANE_U
], src
->stride
[AOM_PLANE_U
],
143 src
->planes
[AOM_PLANE_V
], src
->stride
[AOM_PLANE_V
], src
->d_w
,
144 src
->d_h
, dst
->planes
[AOM_PLANE_Y
], dst
->stride
[AOM_PLANE_Y
],
145 dst
->planes
[AOM_PLANE_U
], dst
->stride
[AOM_PLANE_U
],
146 dst
->planes
[AOM_PLANE_V
], dst
->stride
[AOM_PLANE_V
], dst
->d_w
,
151 void show_help(FILE *fout
, int shorthelp
) {
152 fprintf(fout
, "Usage: %s <options> filename\n\n", exec_name
);
155 fprintf(fout
, "Use --help to see the full list of options.\n");
159 fprintf(fout
, "Options:\n");
160 arg_show_usage(fout
, all_args
);
162 "\nOutput File Patterns:\n\n"
163 " The -o argument specifies the name of the file(s) to "
164 "write to. If the\n argument does not include any escape "
165 "characters, the output will be\n written to a single file. "
166 "Otherwise, the filename will be calculated by\n expanding "
167 "the following escape characters:\n");
169 "\n\t%%w - Frame width"
170 "\n\t%%h - Frame height"
171 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
172 "\n\n Pattern arguments are only supported in conjunction "
173 "with the --yv12 and\n --i420 options. If the -o option is "
174 "not specified, the output will be\n directed to stdout.\n");
175 fprintf(fout
, "\nIncluded decoders:\n\n");
177 for (int i
= 0; i
< get_aom_decoder_count(); ++i
) {
178 const AvxInterface
*const decoder
= get_aom_decoder_by_index(i
);
179 fprintf(fout
, " %-6s - %s\n", decoder
->name
,
180 aom_codec_iface_name(decoder
->codec_interface()));
184 void usage_exit(void) {
185 show_help(stderr
, 1);
189 static int raw_read_frame(FILE *infile
, uint8_t **buffer
, size_t *bytes_read
,
190 size_t *buffer_size
) {
191 char raw_hdr
[RAW_FRAME_HDR_SZ
];
192 size_t frame_size
= 0;
194 if (fread(raw_hdr
, RAW_FRAME_HDR_SZ
, 1, infile
) != 1) {
195 if (!feof(infile
)) warn("Failed to read RAW frame size\n");
197 const size_t kCorruptFrameThreshold
= 256 * 1024 * 1024;
198 const size_t kFrameTooSmallThreshold
= 256 * 1024;
199 frame_size
= mem_get_le32(raw_hdr
);
201 if (frame_size
> kCorruptFrameThreshold
) {
202 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size
);
206 if (frame_size
< kFrameTooSmallThreshold
) {
207 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
208 (unsigned int)frame_size
);
211 if (frame_size
> *buffer_size
) {
212 uint8_t *new_buf
= realloc(*buffer
, 2 * frame_size
);
215 *buffer_size
= 2 * frame_size
;
217 warn("Failed to allocate compressed data buffer\n");
224 if (fread(*buffer
, 1, frame_size
, infile
) != frame_size
) {
225 warn("Failed to read full frame\n");
228 *bytes_read
= frame_size
;
234 static int read_frame(struct AvxDecInputContext
*input
, uint8_t **buf
,
235 size_t *bytes_in_buffer
, size_t *buffer_size
) {
236 switch (input
->aom_input_ctx
->file_type
) {
239 return webm_read_frame(input
->webm_ctx
, buf
, bytes_in_buffer
);
242 return raw_read_frame(input
->aom_input_ctx
->file
, buf
, bytes_in_buffer
,
245 return ivf_read_frame(input
->aom_input_ctx
->file
, buf
, bytes_in_buffer
,
247 #if CONFIG_OBU_NO_IVF
249 return obu_read_temporal_unit(input
->aom_input_ctx
->file
, buf
,
250 #if CONFIG_SCALABILITY
251 bytes_in_buffer
, buffer_size
, 0);
253 bytes_in_buffer
, buffer_size
);
260 static void update_image_md5(const aom_image_t
*img
, const int planes
[3],
264 for (i
= 0; i
< 3; ++i
) {
265 const int plane
= planes
[i
];
266 const unsigned char *buf
= img
->planes
[plane
];
267 const int stride
= img
->stride
[plane
];
268 const int w
= aom_img_plane_width(img
, plane
) *
269 ((img
->fmt
& AOM_IMG_FMT_HIGHBITDEPTH
) ? 2 : 1);
270 const int h
= aom_img_plane_height(img
, plane
);
272 for (y
= 0; y
< h
; ++y
) {
273 MD5Update(md5
, buf
, w
);
279 static void write_image_file(const aom_image_t
*img
, const int *planes
,
280 const int num_planes
, FILE *file
) {
282 const int bytes_per_sample
= ((img
->fmt
& AOM_IMG_FMT_HIGHBITDEPTH
) ? 2 : 1);
284 for (i
= 0; i
< num_planes
; ++i
) {
285 const int plane
= planes
[i
];
286 const unsigned char *buf
= img
->planes
[plane
];
287 const int stride
= img
->stride
[plane
];
288 const int w
= aom_img_plane_width(img
, plane
);
289 const int h
= aom_img_plane_height(img
, plane
);
291 for (y
= 0; y
< h
; ++y
) {
292 fwrite(buf
, bytes_per_sample
, w
, file
);
298 static int file_is_raw(struct AvxInputContext
*input
) {
301 aom_codec_stream_info_t si
;
303 if (fread(buf
, 1, 32, input
->file
) == 32) {
306 if (mem_get_le32(buf
) < 256 * 1024 * 1024) {
307 for (i
= 0; i
< get_aom_decoder_count(); ++i
) {
308 const AvxInterface
*const decoder
= get_aom_decoder_by_index(i
);
309 if (!aom_codec_peek_stream_info(decoder
->codec_interface(), buf
+ 4,
312 input
->fourcc
= decoder
->fourcc
;
314 input
->height
= si
.h
;
315 input
->framerate
.numerator
= 30;
316 input
->framerate
.denominator
= 1;
327 static void show_progress(int frame_in
, int frame_out
, uint64_t dx_time
) {
329 "%d decoded frames/%d showed frames in %" PRId64
" us (%.2f fps)\r",
330 frame_in
, frame_out
, dx_time
,
331 (double)frame_out
* 1000000.0 / (double)dx_time
);
334 struct ExternalFrameBuffer
{
340 struct ExternalFrameBufferList
{
341 int num_external_frame_buffers
;
342 struct ExternalFrameBuffer
*ext_fb
;
345 // Callback used by libaom to request an external frame buffer. |cb_priv|
346 // Application private data passed into the set function. |min_size| is the
347 // minimum size in bytes needed to decode the next frame. |fb| pointer to the
349 static int get_av1_frame_buffer(void *cb_priv
, size_t min_size
,
350 aom_codec_frame_buffer_t
*fb
) {
352 struct ExternalFrameBufferList
*const ext_fb_list
=
353 (struct ExternalFrameBufferList
*)cb_priv
;
354 if (ext_fb_list
== NULL
) return -1;
356 // Find a free frame buffer.
357 for (i
= 0; i
< ext_fb_list
->num_external_frame_buffers
; ++i
) {
358 if (!ext_fb_list
->ext_fb
[i
].in_use
) break;
361 if (i
== ext_fb_list
->num_external_frame_buffers
) return -1;
363 if (ext_fb_list
->ext_fb
[i
].size
< min_size
) {
364 free(ext_fb_list
->ext_fb
[i
].data
);
365 ext_fb_list
->ext_fb
[i
].data
= (uint8_t *)calloc(min_size
, sizeof(uint8_t));
366 if (!ext_fb_list
->ext_fb
[i
].data
) return -1;
368 ext_fb_list
->ext_fb
[i
].size
= min_size
;
371 fb
->data
= ext_fb_list
->ext_fb
[i
].data
;
372 fb
->size
= ext_fb_list
->ext_fb
[i
].size
;
373 ext_fb_list
->ext_fb
[i
].in_use
= 1;
375 // Set the frame buffer's private data to point at the external frame buffer.
376 fb
->priv
= &ext_fb_list
->ext_fb
[i
];
380 // Callback used by libaom when there are no references to the frame buffer.
381 // |cb_priv| user private data passed into the set function. |fb| pointer
382 // to the frame buffer.
383 static int release_av1_frame_buffer(void *cb_priv
,
384 aom_codec_frame_buffer_t
*fb
) {
385 struct ExternalFrameBuffer
*const ext_fb
=
386 (struct ExternalFrameBuffer
*)fb
->priv
;
392 static void generate_filename(const char *pattern
, char *out
, size_t q_len
,
393 unsigned int d_w
, unsigned int d_h
,
394 unsigned int frame_in
) {
395 const char *p
= pattern
;
399 char *next_pat
= strchr(p
, '%');
404 /* parse the pattern */
407 case 'w': snprintf(q
, q_len
- 1, "%d", d_w
); break;
408 case 'h': snprintf(q
, q_len
- 1, "%d", d_h
); break;
409 case '1': snprintf(q
, q_len
- 1, "%d", frame_in
); break;
410 case '2': snprintf(q
, q_len
- 1, "%02d", frame_in
); break;
411 case '3': snprintf(q
, q_len
- 1, "%03d", frame_in
); break;
412 case '4': snprintf(q
, q_len
- 1, "%04d", frame_in
); break;
413 case '5': snprintf(q
, q_len
- 1, "%05d", frame_in
); break;
414 case '6': snprintf(q
, q_len
- 1, "%06d", frame_in
); break;
415 case '7': snprintf(q
, q_len
- 1, "%07d", frame_in
); break;
416 case '8': snprintf(q
, q_len
- 1, "%08d", frame_in
); break;
417 case '9': snprintf(q
, q_len
- 1, "%09d", frame_in
); break;
418 default: die("Unrecognized pattern %%%c\n", p
[1]); break;
422 if (pat_len
>= q_len
- 1) die("Output filename too long.\n");
429 /* copy the next segment */
431 copy_len
= strlen(p
);
433 copy_len
= next_pat
- p
;
435 if (copy_len
>= q_len
- 1) die("Output filename too long.\n");
437 memcpy(q
, p
, copy_len
);
446 static int is_single_file(const char *outfile_pattern
) {
447 const char *p
= outfile_pattern
;
451 if (p
&& p
[1] >= '1' && p
[1] <= '9')
452 return 0; // pattern contains sequence number, so it's not unique
459 static void print_md5(unsigned char digest
[16], const char *filename
) {
462 for (i
= 0; i
< 16; ++i
) printf("%02x", digest
[i
]);
463 printf(" %s\n", filename
);
466 static FILE *open_outfile(const char *name
) {
467 if (strcmp("-", name
) == 0) {
468 set_binary_mode(stdout
);
471 FILE *file
= fopen(name
, "wb");
472 if (!file
) fatal("Failed to open output file '%s'", name
);
477 static int img_shifted_realloc_required(const aom_image_t
*img
,
478 const aom_image_t
*shifted
,
479 aom_img_fmt_t required_fmt
) {
480 return img
->d_w
!= shifted
->d_w
|| img
->d_h
!= shifted
->d_h
||
481 required_fmt
!= shifted
->fmt
;
484 static int main_loop(int argc
, const char **argv_
) {
485 aom_codec_ctx_t decoder
;
488 int ret
= EXIT_FAILURE
;
490 size_t bytes_in_buffer
= 0, buffer_size
= 0;
492 int frame_in
= 0, frame_out
= 0, flipuv
= 0, noblit
= 0;
493 int do_md5
= 0, progress
= 0;
494 int stop_after
= 0, postproc
= 0, summary
= 0, quiet
= 1;
497 const AvxInterface
*interface
= NULL
;
498 const AvxInterface
*fourcc_interface
= NULL
;
499 uint64_t dx_time
= 0;
501 char **argv
, **argi
, **argj
;
508 aom_codec_dec_cfg_t cfg
= { 0, 0, 0, CONFIG_LOWBITDEPTH
, { 1 } };
509 unsigned int output_bit_depth
= 0;
510 unsigned int tile_mode
= 0;
513 int frames_corrupted
= 0;
516 aom_image_t
*scaled_img
= NULL
;
517 aom_image_t
*img_shifted
= NULL
;
518 int frame_avail
, got_data
, flush_decoder
= 0;
519 int num_external_frame_buffers
= 0;
520 struct ExternalFrameBufferList ext_fb_list
= { 0, NULL
};
522 const char *outfile_pattern
= NULL
;
523 char outfile_name
[PATH_MAX
] = { 0 };
524 FILE *outfile
= NULL
;
526 FILE *framestats_file
= NULL
;
529 unsigned char md5_digest
[16];
531 struct AvxDecInputContext input
= { NULL
, NULL
};
532 struct AvxInputContext aom_input_ctx
;
534 struct WebmInputContext webm_ctx
;
535 memset(&(webm_ctx
), 0, sizeof(webm_ctx
));
536 input
.webm_ctx
= &webm_ctx
;
538 input
.aom_input_ctx
= &aom_input_ctx
;
540 /* Parse command line */
541 exec_name
= argv_
[0];
542 argv
= argv_dup(argc
- 1, argv_
+ 1);
544 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
) {
545 memset(&arg
, 0, sizeof(arg
));
548 if (arg_match(&arg
, &help
, argi
)) {
549 show_help(stdout
, 0);
551 } else if (arg_match(&arg
, &codecarg
, argi
)) {
552 interface
= get_aom_decoder_by_name(arg
.val
);
554 die("Error: Unrecognized argument (%s) to --codec\n", arg
.val
);
555 } else if (arg_match(&arg
, &looparg
, argi
)) {
557 } else if (arg_match(&arg
, &outputfile
, argi
)) {
558 outfile_pattern
= arg
.val
;
559 } else if (arg_match(&arg
, &use_yv12
, argi
)) {
565 } else if (arg_match(&arg
, &use_i420
, argi
)) {
571 } else if (arg_match(&arg
, &rawvideo
, argi
)) {
576 } else if (arg_match(&arg
, &flipuvarg
, argi
)) {
578 } else if (arg_match(&arg
, &noblitarg
, argi
)) {
580 } else if (arg_match(&arg
, &progressarg
, argi
)) {
582 } else if (arg_match(&arg
, &limitarg
, argi
)) {
583 stop_after
= arg_parse_uint(&arg
);
584 } else if (arg_match(&arg
, &skiparg
, argi
)) {
585 arg_skip
= arg_parse_uint(&arg
);
586 } else if (arg_match(&arg
, &postprocarg
, argi
)) {
588 } else if (arg_match(&arg
, &md5arg
, argi
)) {
590 } else if (arg_match(&arg
, &framestatsarg
, argi
)) {
591 framestats_file
= fopen(arg
.val
, "w");
592 if (!framestats_file
) {
593 die("Error: Could not open --framestats file (%s) for writing.\n",
596 } else if (arg_match(&arg
, &summaryarg
, argi
)) {
598 } else if (arg_match(&arg
, &threadsarg
, argi
)) {
599 cfg
.threads
= arg_parse_uint(&arg
);
600 } else if (arg_match(&arg
, &verbosearg
, argi
)) {
602 } else if (arg_match(&arg
, &scalearg
, argi
)) {
604 } else if (arg_match(&arg
, &fb_arg
, argi
)) {
605 num_external_frame_buffers
= arg_parse_uint(&arg
);
606 } else if (arg_match(&arg
, &continuearg
, argi
)) {
608 } else if (arg_match(&arg
, &outbitdeptharg
, argi
)) {
609 output_bit_depth
= arg_parse_uint(&arg
);
610 } else if (arg_match(&arg
, &tilem
, argi
)) {
611 tile_mode
= arg_parse_int(&arg
);
612 } else if (arg_match(&arg
, &tiler
, argi
)) {
613 tile_row
= arg_parse_int(&arg
);
614 } else if (arg_match(&arg
, &tilec
, argi
)) {
615 tile_col
= arg_parse_int(&arg
);
621 /* Check for unrecognized options */
622 for (argi
= argv
; *argi
; argi
++)
623 if (argi
[0][0] == '-' && strlen(argi
[0]) > 1)
624 die("Error: Unrecognized option %s\n", *argi
);
626 /* Handle non-option arguments */
631 fprintf(stderr
, "No input file specified!\n");
635 infile
= strcmp(fn
, "-") ? fopen(fn
, "rb") : set_binary_mode(stdin
);
638 fatal("Failed to open input file '%s'", strcmp(fn
, "-") ? fn
: "stdin");
640 #if CONFIG_OS_SUPPORT
641 /* Make sure we don't dump to the terminal, unless forced to with -o - */
642 if (!outfile_pattern
&& isatty(STDOUT_FILENO
) && !do_md5
&& !noblit
) {
644 "Not dumping raw video to your terminal. Use '-o -' to "
649 input
.aom_input_ctx
->filename
= fn
;
650 input
.aom_input_ctx
->file
= infile
;
651 if (file_is_ivf(input
.aom_input_ctx
))
652 input
.aom_input_ctx
->file_type
= FILE_TYPE_IVF
;
654 else if (file_is_webm(input
.webm_ctx
, input
.aom_input_ctx
))
655 input
.aom_input_ctx
->file_type
= FILE_TYPE_WEBM
;
657 #if CONFIG_OBU_NO_IVF
658 else if (file_is_obu(input
.aom_input_ctx
))
659 input
.aom_input_ctx
->file_type
= FILE_TYPE_OBU
;
661 else if (file_is_raw(input
.aom_input_ctx
))
662 input
.aom_input_ctx
->file_type
= FILE_TYPE_RAW
;
664 fprintf(stderr
, "Unrecognized input file type.\n");
666 fprintf(stderr
, "aomdec was built without WebM container support.\n");
671 outfile_pattern
= outfile_pattern
? outfile_pattern
: "-";
672 single_file
= is_single_file(outfile_pattern
);
674 if (!noblit
&& single_file
) {
675 generate_filename(outfile_pattern
, outfile_name
, PATH_MAX
,
676 aom_input_ctx
.width
, aom_input_ctx
.height
, 0);
680 outfile
= open_outfile(outfile_name
);
683 if (use_y4m
&& !noblit
) {
686 "YUV4MPEG2 not supported with output patterns,"
687 " try --i420 or --yv12 or --rawvideo.\n");
692 if (aom_input_ctx
.file_type
== FILE_TYPE_WEBM
) {
693 if (webm_guess_framerate(input
.webm_ctx
, input
.aom_input_ctx
)) {
695 "Failed to guess framerate -- error parsing "
703 fourcc_interface
= get_aom_decoder_by_fourcc(aom_input_ctx
.fourcc
);
704 if (interface
&& fourcc_interface
&& interface
!= fourcc_interface
)
705 warn("Header indicates codec: %s\n", fourcc_interface
->name
);
707 interface
= fourcc_interface
;
709 if (!interface
) interface
= get_aom_decoder_by_index(0);
711 dec_flags
= (postproc
? AOM_CODEC_USE_POSTPROC
: 0);
712 if (aom_codec_dec_init(&decoder
, interface
->codec_interface(), &cfg
,
714 fprintf(stderr
, "Failed to initialize decoder: %s\n",
715 aom_codec_error(&decoder
));
719 if (!quiet
) fprintf(stderr
, "%s\n", decoder
.name
);
721 #if CONFIG_AV1_DECODER
722 if (aom_codec_control(&decoder
, AV1_SET_TILE_MODE
, tile_mode
)) {
723 fprintf(stderr
, "Failed to set decode_tile_mode: %s\n",
724 aom_codec_error(&decoder
));
728 if (aom_codec_control(&decoder
, AV1_SET_DECODE_TILE_ROW
, tile_row
)) {
729 fprintf(stderr
, "Failed to set decode_tile_row: %s\n",
730 aom_codec_error(&decoder
));
734 if (aom_codec_control(&decoder
, AV1_SET_DECODE_TILE_COL
, tile_col
)) {
735 fprintf(stderr
, "Failed to set decode_tile_col: %s\n",
736 aom_codec_error(&decoder
));
741 if (arg_skip
) fprintf(stderr
, "Skipping first %d frames.\n", arg_skip
);
743 if (read_frame(&input
, &buf
, &bytes_in_buffer
, &buffer_size
)) break;
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 (aom_codec_set_frame_buffer_functions(&decoder
, get_av1_frame_buffer
,
752 release_av1_frame_buffer
,
754 fprintf(stderr
, "Failed to configure external frame buffers: %s\n",
755 aom_codec_error(&decoder
));
763 if (framestats_file
) fprintf(framestats_file
, "bytes,qp\r\n");
766 while (frame_avail
|| got_data
) {
767 aom_codec_iter_t iter
= NULL
;
769 struct aom_usec_timer timer
;
773 if (!stop_after
|| frame_in
< stop_after
) {
774 if (!read_frame(&input
, &buf
, &bytes_in_buffer
, &buffer_size
)) {
778 aom_usec_timer_start(&timer
);
780 if (aom_codec_decode(&decoder
, buf
, (unsigned int)bytes_in_buffer
,
782 const char *detail
= aom_codec_error_detail(&decoder
);
783 warn("Failed to decode frame %d: %s", frame_in
,
784 aom_codec_error(&decoder
));
786 if (detail
) warn("Additional information: %s", detail
);
787 if (!keep_going
) goto fail
;
790 if (framestats_file
) {
792 if (aom_codec_control(&decoder
, AOMD_GET_LAST_QUANTIZER
, &qp
)) {
793 warn("Failed AOMD_GET_LAST_QUANTIZER: %s",
794 aom_codec_error(&decoder
));
795 if (!keep_going
) goto fail
;
797 fprintf(framestats_file
, "%d,%d\r\n", (int)bytes_in_buffer
, qp
);
800 aom_usec_timer_mark(&timer
);
801 dx_time
+= aom_usec_timer_elapsed(&timer
);
809 aom_usec_timer_start(&timer
);
812 // Flush the decoder in frame parallel decode.
813 if (aom_codec_decode(&decoder
, NULL
, 0, NULL
)) {
814 warn("Failed to flush decoder: %s", aom_codec_error(&decoder
));
819 if ((img
= aom_codec_get_frame(&decoder
, &iter
))) {
824 aom_usec_timer_mark(&timer
);
825 dx_time
+= (unsigned int)aom_usec_timer_elapsed(&timer
);
827 if (aom_codec_control(&decoder
, AOMD_GET_FRAME_CORRUPTED
, &corrupted
)) {
828 warn("Failed AOM_GET_FRAME_CORRUPTED: %s", aom_codec_error(&decoder
));
829 if (!keep_going
) goto fail
;
831 frames_corrupted
+= corrupted
;
833 if (progress
) show_progress(frame_in
, frame_out
, dx_time
);
835 if (!noblit
&& img
) {
836 const int PLANES_YUV
[] = { AOM_PLANE_Y
, AOM_PLANE_U
, AOM_PLANE_V
};
837 const int PLANES_YVU
[] = { AOM_PLANE_Y
, AOM_PLANE_V
, AOM_PLANE_U
};
838 const int *planes
= flipuv
? PLANES_YVU
: PLANES_YUV
;
841 if (frame_out
== 1) {
842 // If the output frames are to be scaled to a fixed display size then
843 // use the width and height specified in the container. If either of
844 // these is set to 0, use the display size set in the first frame
845 // header. If that is unavailable, use the raw decoded size of the
846 // first decoded frame.
847 int render_width
= aom_input_ctx
.width
;
848 int render_height
= aom_input_ctx
.height
;
849 if (!render_width
|| !render_height
) {
851 if (aom_codec_control(&decoder
, AV1D_GET_DISPLAY_SIZE
,
853 // As last resort use size of first frame as display size.
854 render_width
= img
->d_w
;
855 render_height
= img
->d_h
;
857 render_width
= render_size
[0];
858 render_height
= render_size
[1];
862 aom_img_alloc(NULL
, img
->fmt
, render_width
, render_height
, 16);
863 scaled_img
->bit_depth
= img
->bit_depth
;
866 if (img
->d_w
!= scaled_img
->d_w
|| img
->d_h
!= scaled_img
->d_h
) {
868 libyuv_scale(img
, scaled_img
, kFilterBox
);
872 "Failed to scale output frame: %s.\n"
873 "libyuv is required for scaling but is currently disabled.\n"
874 "Be sure to specify -DCONFIG_LIBYUV=1 when running cmake.\n",
875 aom_codec_error(&decoder
));
880 // Default to codec bit depth if output bit depth not set
881 if (!output_bit_depth
&& single_file
&& !do_md5
) {
882 output_bit_depth
= img
->bit_depth
;
884 // Shift up or down if necessary
885 if (output_bit_depth
!= 0) {
886 const aom_img_fmt_t shifted_fmt
=
887 output_bit_depth
== 8
888 ? img
->fmt
^ (img
->fmt
& AOM_IMG_FMT_HIGHBITDEPTH
)
889 : img
->fmt
| AOM_IMG_FMT_HIGHBITDEPTH
;
891 if (shifted_fmt
!= img
->fmt
|| output_bit_depth
!= img
->bit_depth
) {
893 img_shifted_realloc_required(img
, img_shifted
, shifted_fmt
)) {
894 aom_img_free(img_shifted
);
899 aom_img_alloc(NULL
, shifted_fmt
, img
->d_w
, img
->d_h
, 16);
900 img_shifted
->bit_depth
= output_bit_depth
;
902 if (output_bit_depth
> img
->bit_depth
) {
903 aom_img_upshift(img_shifted
, img
,
904 output_bit_depth
- img
->bit_depth
);
906 aom_img_downshift(img_shifted
, img
,
907 img
->bit_depth
- output_bit_depth
);
913 aom_input_ctx
.width
= img
->d_w
;
914 aom_input_ctx
.height
= img
->d_h
;
916 #if CONFIG_MONO_VIDEO
917 int num_planes
= (!use_y4m
&& opt_raw
&& img
->monochrome
) ? 1 : 3;
925 char y4m_buf
[Y4M_BUFFER_SIZE
] = { 0 };
927 if (img
->fmt
== AOM_IMG_FMT_I440
|| img
->fmt
== AOM_IMG_FMT_I44016
) {
928 fprintf(stderr
, "Cannot produce y4m output for 440 sampling.\n");
931 if (frame_out
== 1) {
933 len
= y4m_write_file_header(
934 y4m_buf
, sizeof(y4m_buf
), aom_input_ctx
.width
,
935 aom_input_ctx
.height
, &aom_input_ctx
.framerate
, img
->fmt
,
938 MD5Update(&md5_ctx
, (md5byte
*)y4m_buf
, (unsigned int)len
);
940 fputs(y4m_buf
, outfile
);
945 len
= y4m_write_frame_header(y4m_buf
, sizeof(y4m_buf
));
947 MD5Update(&md5_ctx
, (md5byte
*)y4m_buf
, (unsigned int)len
);
949 fputs(y4m_buf
, outfile
);
952 if (frame_out
== 1) {
953 // Check if --yv12 or --i420 options are consistent with the
954 // bit-stream decoded
956 if (img
->fmt
!= AOM_IMG_FMT_I420
&&
957 img
->fmt
!= AOM_IMG_FMT_I42016
) {
958 fprintf(stderr
, "Cannot produce i420 output for bit-stream.\n");
963 if ((img
->fmt
!= AOM_IMG_FMT_I420
&&
964 img
->fmt
!= AOM_IMG_FMT_YV12
) ||
965 img
->bit_depth
!= 8) {
966 fprintf(stderr
, "Cannot produce yv12 output for bit-stream.\n");
974 update_image_md5(img
, planes
, &md5_ctx
);
976 write_image_file(img
, planes
, num_planes
, outfile
);
979 generate_filename(outfile_pattern
, outfile_name
, PATH_MAX
, img
->d_w
,
983 update_image_md5(img
, planes
, &md5_ctx
);
984 MD5Final(md5_digest
, &md5_ctx
);
985 print_md5(md5_digest
, outfile_name
);
987 outfile
= open_outfile(outfile_name
);
988 write_image_file(img
, planes
, num_planes
, outfile
);
995 if (summary
|| progress
) {
996 show_progress(frame_in
, frame_out
, dx_time
);
997 fprintf(stderr
, "\n");
1000 if (frames_corrupted
) {
1001 fprintf(stderr
, "WARNING: %d frames corrupted.\n", frames_corrupted
);
1008 if (aom_codec_destroy(&decoder
)) {
1009 fprintf(stderr
, "Failed to destroy decoder: %s\n",
1010 aom_codec_error(&decoder
));
1015 if (!noblit
&& single_file
) {
1017 MD5Final(md5_digest
, &md5_ctx
);
1018 print_md5(md5_digest
, outfile_name
);
1025 if (input
.aom_input_ctx
->file_type
== FILE_TYPE_WEBM
)
1026 webm_free(input
.webm_ctx
);
1029 if (input
.aom_input_ctx
->file_type
!= FILE_TYPE_WEBM
) free(buf
);
1031 if (scaled_img
) aom_img_free(scaled_img
);
1032 if (img_shifted
) aom_img_free(img_shifted
);
1034 for (i
= 0; i
< ext_fb_list
.num_external_frame_buffers
; ++i
) {
1035 free(ext_fb_list
.ext_fb
[i
].data
);
1037 free(ext_fb_list
.ext_fb
);
1040 if (framestats_file
) fclose(framestats_file
);
1047 int main(int argc
, const char **argv_
) {
1048 unsigned int loops
= 1, i
;
1049 char **argv
, **argi
, **argj
;
1053 argv
= argv_dup(argc
- 1, argv_
+ 1);
1054 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
) {
1055 memset(&arg
, 0, sizeof(arg
));
1058 if (arg_match(&arg
, &looparg
, argi
)) {
1059 loops
= arg_parse_uint(&arg
);
1064 for (i
= 0; !error
&& i
< loops
; i
++) error
= main_loop(argc
, argv_
);