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"
46 #include "./tools_common.h"
48 #include "./webmdec.h"
52 static const char *exec_name
;
54 struct AvxDecInputContext
{
55 struct AvxInputContext
*aom_input_ctx
;
56 struct WebmInputContext
*webm_ctx
;
59 static const arg_def_t looparg
=
60 ARG_DEF(NULL
, "loops", 1, "Number of times to decode the file");
61 static const arg_def_t codecarg
= ARG_DEF(NULL
, "codec", 1, "Codec to use");
62 static const arg_def_t use_yv12
=
63 ARG_DEF(NULL
, "yv12", 0, "Output raw YV12 frames");
64 static const arg_def_t use_i420
=
65 ARG_DEF(NULL
, "i420", 0, "Output raw I420 frames");
66 static const arg_def_t flipuvarg
=
67 ARG_DEF(NULL
, "flipuv", 0, "Flip the chroma planes in the output");
68 static const arg_def_t rawvideo
=
69 ARG_DEF(NULL
, "rawvideo", 0, "Output raw YUV frames");
70 static const arg_def_t noblitarg
=
71 ARG_DEF(NULL
, "noblit", 0, "Don't process the decoded frames");
72 static const arg_def_t progressarg
=
73 ARG_DEF(NULL
, "progress", 0, "Show progress after each frame decodes");
74 static const arg_def_t limitarg
=
75 ARG_DEF(NULL
, "limit", 1, "Stop decoding after n frames");
76 static const arg_def_t skiparg
=
77 ARG_DEF(NULL
, "skip", 1, "Skip the first n input frames");
78 static const arg_def_t postprocarg
=
79 ARG_DEF(NULL
, "postproc", 0, "Postprocess decoded frames");
80 static const arg_def_t summaryarg
=
81 ARG_DEF(NULL
, "summary", 0, "Show timing summary");
82 static const arg_def_t outputfile
=
83 ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
84 static const arg_def_t threadsarg
=
85 ARG_DEF("t", "threads", 1, "Max threads to use");
86 static const arg_def_t frameparallelarg
=
87 ARG_DEF(NULL
, "frame-parallel", 0, "Frame parallel decode");
88 static const arg_def_t verbosearg
=
89 ARG_DEF("v", "verbose", 0, "Show version string");
90 static const arg_def_t error_concealment
=
91 ARG_DEF(NULL
, "error-concealment", 0, "Enable decoder error-concealment");
92 static const arg_def_t scalearg
=
93 ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
94 static const arg_def_t continuearg
=
95 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
96 static const arg_def_t fb_arg
=
97 ARG_DEF(NULL
, "frame-buffers", 1, "Number of frame buffers to use");
98 static const arg_def_t md5arg
=
99 ARG_DEF(NULL
, "md5", 0, "Compute the MD5 sum of the decoded frame");
100 #if CONFIG_AOM_HIGHBITDEPTH
101 static const arg_def_t outbitdeptharg
=
102 ARG_DEF(NULL
, "output-bit-depth", 1, "Output bit-depth for decoded frames");
105 static const arg_def_t tiler
= ARG_DEF(NULL
, "tile-row", 1,
106 "Row index of tile to decode "
107 "(-1 for all rows)");
108 static const arg_def_t tilec
= ARG_DEF(NULL
, "tile-column", 1,
109 "Column index of tile to decode "
110 "(-1 for all columns)");
111 #endif // CONFIG_EXT_TILE
113 static const arg_def_t
*all_args
[] = { &codecarg
,
133 #if CONFIG_AOM_HIGHBITDEPTH
139 #endif // CONFIG_EXT_TILE
143 static INLINE
int libyuv_scale(aom_image_t
*src
, aom_image_t
*dst
,
144 FilterModeEnum mode
) {
145 #if CONFIG_AOM_HIGHBITDEPTH
146 if (src
->fmt
== AOM_IMG_FMT_I42016
) {
147 assert(dst
->fmt
== AOM_IMG_FMT_I42016
);
149 (uint16_t *)src
->planes
[AOM_PLANE_Y
], src
->stride
[AOM_PLANE_Y
] / 2,
150 (uint16_t *)src
->planes
[AOM_PLANE_U
], src
->stride
[AOM_PLANE_U
] / 2,
151 (uint16_t *)src
->planes
[AOM_PLANE_V
], src
->stride
[AOM_PLANE_V
] / 2,
152 src
->d_w
, src
->d_h
, (uint16_t *)dst
->planes
[AOM_PLANE_Y
],
153 dst
->stride
[AOM_PLANE_Y
] / 2, (uint16_t *)dst
->planes
[AOM_PLANE_U
],
154 dst
->stride
[AOM_PLANE_U
] / 2, (uint16_t *)dst
->planes
[AOM_PLANE_V
],
155 dst
->stride
[AOM_PLANE_V
] / 2, dst
->d_w
, dst
->d_h
, mode
);
158 assert(src
->fmt
== AOM_IMG_FMT_I420
);
159 assert(dst
->fmt
== AOM_IMG_FMT_I420
);
160 return I420Scale(src
->planes
[AOM_PLANE_Y
], src
->stride
[AOM_PLANE_Y
],
161 src
->planes
[AOM_PLANE_U
], src
->stride
[AOM_PLANE_U
],
162 src
->planes
[AOM_PLANE_V
], src
->stride
[AOM_PLANE_V
], src
->d_w
,
163 src
->d_h
, dst
->planes
[AOM_PLANE_Y
], dst
->stride
[AOM_PLANE_Y
],
164 dst
->planes
[AOM_PLANE_U
], dst
->stride
[AOM_PLANE_U
],
165 dst
->planes
[AOM_PLANE_V
], dst
->stride
[AOM_PLANE_V
], dst
->d_w
,
170 void usage_exit(void) {
174 "Usage: %s <options> filename\n\n"
177 arg_show_usage(stderr
, all_args
);
179 "\nOutput File Patterns:\n\n"
180 " The -o argument specifies the name of the file(s) to "
181 "write to. If the\n argument does not include any escape "
182 "characters, the output will be\n written to a single file. "
183 "Otherwise, the filename will be calculated by\n expanding "
184 "the following escape characters:\n");
186 "\n\t%%w - Frame width"
187 "\n\t%%h - Frame height"
188 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
189 "\n\n Pattern arguments are only supported in conjunction "
190 "with the --yv12 and\n --i420 options. If the -o option is "
191 "not specified, the output will be\n directed to stdout.\n");
192 fprintf(stderr
, "\nIncluded decoders:\n\n");
194 for (i
= 0; i
< get_aom_decoder_count(); ++i
) {
195 const AvxInterface
*const decoder
= get_aom_decoder_by_index(i
);
196 fprintf(stderr
, " %-6s - %s\n", decoder
->name
,
197 aom_codec_iface_name(decoder
->codec_interface()));
203 static int raw_read_frame(FILE *infile
, uint8_t **buffer
, size_t *bytes_read
,
204 size_t *buffer_size
) {
205 char raw_hdr
[RAW_FRAME_HDR_SZ
];
206 size_t frame_size
= 0;
208 if (fread(raw_hdr
, RAW_FRAME_HDR_SZ
, 1, infile
) != 1) {
209 if (!feof(infile
)) warn("Failed to read RAW frame size\n");
211 const size_t kCorruptFrameThreshold
= 256 * 1024 * 1024;
212 const size_t kFrameTooSmallThreshold
= 256 * 1024;
213 frame_size
= mem_get_le32(raw_hdr
);
215 if (frame_size
> kCorruptFrameThreshold
) {
216 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size
);
220 if (frame_size
< kFrameTooSmallThreshold
) {
221 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
222 (unsigned int)frame_size
);
225 if (frame_size
> *buffer_size
) {
226 uint8_t *new_buf
= realloc(*buffer
, 2 * frame_size
);
229 *buffer_size
= 2 * frame_size
;
231 warn("Failed to allocate compressed data buffer\n");
238 if (fread(*buffer
, 1, frame_size
, infile
) != frame_size
) {
239 warn("Failed to read full frame\n");
242 *bytes_read
= frame_size
;
248 static int read_frame(struct AvxDecInputContext
*input
, uint8_t **buf
,
249 size_t *bytes_in_buffer
, size_t *buffer_size
) {
250 switch (input
->aom_input_ctx
->file_type
) {
253 return webm_read_frame(input
->webm_ctx
, buf
, bytes_in_buffer
);
256 return raw_read_frame(input
->aom_input_ctx
->file
, buf
, bytes_in_buffer
,
259 return ivf_read_frame(input
->aom_input_ctx
->file
, buf
, bytes_in_buffer
,
265 static void update_image_md5(const aom_image_t
*img
, const int planes
[3],
269 for (i
= 0; i
< 3; ++i
) {
270 const int plane
= planes
[i
];
271 const unsigned char *buf
= img
->planes
[plane
];
272 const int stride
= img
->stride
[plane
];
273 const int w
= aom_img_plane_width(img
, plane
) *
274 ((img
->fmt
& AOM_IMG_FMT_HIGHBITDEPTH
) ? 2 : 1);
275 const int h
= aom_img_plane_height(img
, plane
);
277 for (y
= 0; y
< h
; ++y
) {
278 MD5Update(md5
, buf
, w
);
284 static void write_image_file(const aom_image_t
*img
, const int planes
[3],
287 #if CONFIG_AOM_HIGHBITDEPTH
288 const int bytes_per_sample
= ((img
->fmt
& AOM_IMG_FMT_HIGHBITDEPTH
) ? 2 : 1);
290 const int bytes_per_sample
= 1;
293 for (i
= 0; i
< 3; ++i
) {
294 const int plane
= planes
[i
];
295 const unsigned char *buf
= img
->planes
[plane
];
296 const int stride
= img
->stride
[plane
];
297 const int w
= aom_img_plane_width(img
, plane
);
298 const int h
= aom_img_plane_height(img
, plane
);
300 for (y
= 0; y
< h
; ++y
) {
301 fwrite(buf
, bytes_per_sample
, w
, file
);
307 static int file_is_raw(struct AvxInputContext
*input
) {
310 aom_codec_stream_info_t si
;
314 if (fread(buf
, 1, 32, input
->file
) == 32) {
317 if (mem_get_le32(buf
) < 256 * 1024 * 1024) {
318 for (i
= 0; i
< get_aom_decoder_count(); ++i
) {
319 const AvxInterface
*const decoder
= get_aom_decoder_by_index(i
);
320 if (!aom_codec_peek_stream_info(decoder
->codec_interface(), buf
+ 4,
323 input
->fourcc
= decoder
->fourcc
;
325 input
->height
= si
.h
;
326 input
->framerate
.numerator
= 30;
327 input
->framerate
.denominator
= 1;
338 static void show_progress(int frame_in
, int frame_out
, uint64_t dx_time
) {
340 "%d decoded frames/%d showed frames in %" PRId64
" us (%.2f fps)\r",
341 frame_in
, frame_out
, dx_time
,
342 (double)frame_out
* 1000000.0 / (double)dx_time
);
345 struct ExternalFrameBuffer
{
351 struct ExternalFrameBufferList
{
352 int num_external_frame_buffers
;
353 struct ExternalFrameBuffer
*ext_fb
;
356 // Callback used by libaom to request an external frame buffer. |cb_priv|
357 // Application private data passed into the set function. |min_size| is the
358 // minimum size in bytes needed to decode the next frame. |fb| pointer to the
360 static int get_av1_frame_buffer(void *cb_priv
, size_t min_size
,
361 aom_codec_frame_buffer_t
*fb
) {
363 struct ExternalFrameBufferList
*const ext_fb_list
=
364 (struct ExternalFrameBufferList
*)cb_priv
;
365 if (ext_fb_list
== NULL
) return -1;
367 // Find a free frame buffer.
368 for (i
= 0; i
< ext_fb_list
->num_external_frame_buffers
; ++i
) {
369 if (!ext_fb_list
->ext_fb
[i
].in_use
) break;
372 if (i
== ext_fb_list
->num_external_frame_buffers
) return -1;
374 if (ext_fb_list
->ext_fb
[i
].size
< min_size
) {
375 free(ext_fb_list
->ext_fb
[i
].data
);
376 ext_fb_list
->ext_fb
[i
].data
= (uint8_t *)calloc(min_size
, sizeof(uint8_t));
377 if (!ext_fb_list
->ext_fb
[i
].data
) return -1;
379 ext_fb_list
->ext_fb
[i
].size
= min_size
;
382 fb
->data
= ext_fb_list
->ext_fb
[i
].data
;
383 fb
->size
= ext_fb_list
->ext_fb
[i
].size
;
384 ext_fb_list
->ext_fb
[i
].in_use
= 1;
386 // Set the frame buffer's private data to point at the external frame buffer.
387 fb
->priv
= &ext_fb_list
->ext_fb
[i
];
391 // Callback used by libaom when there are no references to the frame buffer.
392 // |cb_priv| user private data passed into the set function. |fb| pointer
393 // to the frame buffer.
394 static int release_av1_frame_buffer(void *cb_priv
,
395 aom_codec_frame_buffer_t
*fb
) {
396 struct ExternalFrameBuffer
*const ext_fb
=
397 (struct ExternalFrameBuffer
*)fb
->priv
;
403 static void generate_filename(const char *pattern
, char *out
, size_t q_len
,
404 unsigned int d_w
, unsigned int d_h
,
405 unsigned int frame_in
) {
406 const char *p
= pattern
;
410 char *next_pat
= strchr(p
, '%');
415 /* parse the pattern */
418 case 'w': snprintf(q
, q_len
- 1, "%d", d_w
); break;
419 case 'h': snprintf(q
, q_len
- 1, "%d", d_h
); break;
420 case '1': snprintf(q
, q_len
- 1, "%d", frame_in
); break;
421 case '2': snprintf(q
, q_len
- 1, "%02d", frame_in
); break;
422 case '3': snprintf(q
, q_len
- 1, "%03d", frame_in
); break;
423 case '4': snprintf(q
, q_len
- 1, "%04d", frame_in
); break;
424 case '5': snprintf(q
, q_len
- 1, "%05d", frame_in
); break;
425 case '6': snprintf(q
, q_len
- 1, "%06d", frame_in
); break;
426 case '7': snprintf(q
, q_len
- 1, "%07d", frame_in
); break;
427 case '8': snprintf(q
, q_len
- 1, "%08d", frame_in
); break;
428 case '9': snprintf(q
, q_len
- 1, "%09d", frame_in
); break;
429 default: die("Unrecognized pattern %%%c\n", p
[1]); break;
433 if (pat_len
>= q_len
- 1) die("Output filename too long.\n");
440 /* copy the next segment */
442 copy_len
= strlen(p
);
444 copy_len
= next_pat
- p
;
446 if (copy_len
>= q_len
- 1) die("Output filename too long.\n");
448 memcpy(q
, p
, copy_len
);
457 static int is_single_file(const char *outfile_pattern
) {
458 const char *p
= outfile_pattern
;
462 if (p
&& p
[1] >= '1' && p
[1] <= '9')
463 return 0; // pattern contains sequence number, so it's not unique
470 static void print_md5(unsigned char digest
[16], const char *filename
) {
473 for (i
= 0; i
< 16; ++i
) printf("%02x", digest
[i
]);
474 printf(" %s\n", filename
);
477 static FILE *open_outfile(const char *name
) {
478 if (strcmp("-", name
) == 0) {
479 set_binary_mode(stdout
);
482 FILE *file
= fopen(name
, "wb");
483 if (!file
) fatal("Failed to open output file '%s'", name
);
488 #if CONFIG_AOM_HIGHBITDEPTH
489 static int img_shifted_realloc_required(const aom_image_t
*img
,
490 const aom_image_t
*shifted
,
491 aom_img_fmt_t required_fmt
) {
492 return img
->d_w
!= shifted
->d_w
|| img
->d_h
!= shifted
->d_h
||
493 required_fmt
!= shifted
->fmt
;
497 static int main_loop(int argc
, const char **argv_
) {
498 aom_codec_ctx_t decoder
;
501 int ret
= EXIT_FAILURE
;
503 size_t bytes_in_buffer
= 0, buffer_size
= 0;
505 int frame_in
= 0, frame_out
= 0, flipuv
= 0, noblit
= 0;
506 int do_md5
= 0, progress
= 0, frame_parallel
= 0;
507 int stop_after
= 0, postproc
= 0, summary
= 0, quiet
= 1;
511 const AvxInterface
*interface
= NULL
;
512 const AvxInterface
*fourcc_interface
= NULL
;
513 uint64_t dx_time
= 0;
515 char **argv
, **argi
, **argj
;
521 aom_codec_dec_cfg_t cfg
= { 0, 0, 0 };
522 #if CONFIG_AOM_HIGHBITDEPTH
523 unsigned int output_bit_depth
= 0;
528 #endif // CONFIG_EXT_TILE
529 int frames_corrupted
= 0;
532 aom_image_t
*scaled_img
= NULL
;
533 #if CONFIG_AOM_HIGHBITDEPTH
534 aom_image_t
*img_shifted
= NULL
;
536 int frame_avail
, got_data
, flush_decoder
= 0;
537 int num_external_frame_buffers
= 0;
538 struct ExternalFrameBufferList ext_fb_list
= { 0, NULL
};
540 const char *outfile_pattern
= NULL
;
541 char outfile_name
[PATH_MAX
] = { 0 };
542 FILE *outfile
= NULL
;
545 unsigned char md5_digest
[16];
547 struct AvxDecInputContext input
= { NULL
, NULL
};
548 struct AvxInputContext aom_input_ctx
;
550 struct WebmInputContext webm_ctx
;
551 memset(&(webm_ctx
), 0, sizeof(webm_ctx
));
552 input
.webm_ctx
= &webm_ctx
;
554 input
.aom_input_ctx
= &aom_input_ctx
;
556 /* Parse command line */
557 exec_name
= argv_
[0];
558 argv
= argv_dup(argc
- 1, argv_
+ 1);
560 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
) {
561 memset(&arg
, 0, sizeof(arg
));
564 if (arg_match(&arg
, &codecarg
, argi
)) {
565 interface
= get_aom_decoder_by_name(arg
.val
);
567 die("Error: Unrecognized argument (%s) to --codec\n", arg
.val
);
568 } else if (arg_match(&arg
, &looparg
, argi
)) {
570 } else if (arg_match(&arg
, &outputfile
, argi
))
571 outfile_pattern
= arg
.val
;
572 else if (arg_match(&arg
, &use_yv12
, argi
)) {
576 } else if (arg_match(&arg
, &use_i420
, argi
)) {
580 } else if (arg_match(&arg
, &rawvideo
, argi
)) {
582 } else if (arg_match(&arg
, &flipuvarg
, argi
))
584 else if (arg_match(&arg
, &noblitarg
, argi
))
586 else if (arg_match(&arg
, &progressarg
, argi
))
588 else if (arg_match(&arg
, &limitarg
, argi
))
589 stop_after
= arg_parse_uint(&arg
);
590 else if (arg_match(&arg
, &skiparg
, argi
))
591 arg_skip
= arg_parse_uint(&arg
);
592 else if (arg_match(&arg
, &postprocarg
, argi
))
594 else if (arg_match(&arg
, &md5arg
, argi
))
596 else if (arg_match(&arg
, &summaryarg
, argi
))
598 else if (arg_match(&arg
, &threadsarg
, argi
))
599 cfg
.threads
= arg_parse_uint(&arg
);
600 #if CONFIG_AV1_DECODER
601 else if (arg_match(&arg
, &frameparallelarg
, argi
))
604 else if (arg_match(&arg
, &verbosearg
, argi
))
606 else if (arg_match(&arg
, &scalearg
, argi
))
608 else if (arg_match(&arg
, &fb_arg
, argi
))
609 num_external_frame_buffers
= arg_parse_uint(&arg
);
610 else if (arg_match(&arg
, &continuearg
, argi
))
612 #if CONFIG_AOM_HIGHBITDEPTH
613 else if (arg_match(&arg
, &outbitdeptharg
, argi
)) {
614 output_bit_depth
= arg_parse_uint(&arg
);
618 else if (arg_match(&arg
, &tiler
, argi
))
619 tile_row
= arg_parse_int(&arg
);
620 else if (arg_match(&arg
, &tilec
, argi
))
621 tile_col
= arg_parse_int(&arg
);
622 #endif // CONFIG_EXT_TILE
627 /* Check for unrecognized options */
628 for (argi
= argv
; *argi
; argi
++)
629 if (argi
[0][0] == '-' && strlen(argi
[0]) > 1)
630 die("Error: Unrecognized option %s\n", *argi
);
632 /* Handle non-option arguments */
640 infile
= strcmp(fn
, "-") ? fopen(fn
, "rb") : set_binary_mode(stdin
);
643 fatal("Failed to open input file '%s'", strcmp(fn
, "-") ? fn
: "stdin");
645 #if CONFIG_OS_SUPPORT
646 /* Make sure we don't dump to the terminal, unless forced to with -o - */
647 if (!outfile_pattern
&& isatty(STDOUT_FILENO
) && !do_md5
&& !noblit
) {
649 "Not dumping raw video to your terminal. Use '-o -' to "
654 input
.aom_input_ctx
->file
= infile
;
655 if (file_is_ivf(input
.aom_input_ctx
))
656 input
.aom_input_ctx
->file_type
= FILE_TYPE_IVF
;
658 else if (file_is_webm(input
.webm_ctx
, input
.aom_input_ctx
))
659 input
.aom_input_ctx
->file_type
= FILE_TYPE_WEBM
;
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 (ec_enabled
? AOM_CODEC_USE_ERROR_CONCEALMENT
: 0) |
713 (frame_parallel
? AOM_CODEC_USE_FRAME_THREADING
: 0);
714 if (aom_codec_dec_init(&decoder
, interface
->codec_interface(), &cfg
,
716 fprintf(stderr
, "Failed to initialize decoder: %s\n",
717 aom_codec_error(&decoder
));
721 if (!quiet
) fprintf(stderr
, "%s\n", decoder
.name
);
723 #if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
724 if (strncmp(decoder
.name
, "WebM Project AV1", 17) == 0) {
725 if (aom_codec_control(&decoder
, AV1_SET_DECODE_TILE_ROW
, tile_row
)) {
726 fprintf(stderr
, "Failed to set decode_tile_row: %s\n",
727 aom_codec_error(&decoder
));
731 if (aom_codec_control(&decoder
, AV1_SET_DECODE_TILE_COL
, tile_col
)) {
732 fprintf(stderr
, "Failed to set decode_tile_col: %s\n",
733 aom_codec_error(&decoder
));
739 if (arg_skip
) fprintf(stderr
, "Skipping first %d frames.\n", arg_skip
);
741 if (read_frame(&input
, &buf
, &bytes_in_buffer
, &buffer_size
)) break;
745 if (num_external_frame_buffers
> 0) {
746 ext_fb_list
.num_external_frame_buffers
= num_external_frame_buffers
;
747 ext_fb_list
.ext_fb
= (struct ExternalFrameBuffer
*)calloc(
748 num_external_frame_buffers
, sizeof(*ext_fb_list
.ext_fb
));
749 if (aom_codec_set_frame_buffer_functions(&decoder
, get_av1_frame_buffer
,
750 release_av1_frame_buffer
,
752 fprintf(stderr
, "Failed to configure external frame buffers: %s\n",
753 aom_codec_error(&decoder
));
762 while (frame_avail
|| got_data
) {
763 aom_codec_iter_t iter
= NULL
;
765 struct aom_usec_timer timer
;
769 if (!stop_after
|| frame_in
< stop_after
) {
770 if (!read_frame(&input
, &buf
, &bytes_in_buffer
, &buffer_size
)) {
774 aom_usec_timer_start(&timer
);
776 if (aom_codec_decode(&decoder
, buf
, (unsigned int)bytes_in_buffer
, NULL
,
778 const char *detail
= aom_codec_error_detail(&decoder
);
779 warn("Failed to decode frame %d: %s", frame_in
,
780 aom_codec_error(&decoder
));
782 if (detail
) warn("Additional information: %s", detail
);
783 if (!keep_going
) goto fail
;
786 aom_usec_timer_mark(&timer
);
787 dx_time
+= aom_usec_timer_elapsed(&timer
);
795 aom_usec_timer_start(&timer
);
798 // Flush the decoder in frame parallel decode.
799 if (aom_codec_decode(&decoder
, NULL
, 0, NULL
, 0)) {
800 warn("Failed to flush decoder: %s", aom_codec_error(&decoder
));
805 if ((img
= aom_codec_get_frame(&decoder
, &iter
))) {
810 aom_usec_timer_mark(&timer
);
811 dx_time
+= (unsigned int)aom_usec_timer_elapsed(&timer
);
813 if (!frame_parallel
&&
814 aom_codec_control(&decoder
, AOMD_GET_FRAME_CORRUPTED
, &corrupted
)) {
815 warn("Failed AOM_GET_FRAME_CORRUPTED: %s", aom_codec_error(&decoder
));
816 if (!keep_going
) goto fail
;
818 frames_corrupted
+= corrupted
;
820 if (progress
) show_progress(frame_in
, frame_out
, dx_time
);
822 if (!noblit
&& img
) {
823 const int PLANES_YUV
[] = { AOM_PLANE_Y
, AOM_PLANE_U
, AOM_PLANE_V
};
824 const int PLANES_YVU
[] = { AOM_PLANE_Y
, AOM_PLANE_V
, AOM_PLANE_U
};
825 const int *planes
= flipuv
? PLANES_YVU
: PLANES_YUV
;
828 if (frame_out
== 1) {
829 // If the output frames are to be scaled to a fixed display size then
830 // use the width and height specified in the container. If either of
831 // these is set to 0, use the display size set in the first frame
832 // header. If that is unavailable, use the raw decoded size of the
833 // first decoded frame.
834 int render_width
= aom_input_ctx
.width
;
835 int render_height
= aom_input_ctx
.height
;
836 if (!render_width
|| !render_height
) {
838 if (aom_codec_control(&decoder
, AV1D_GET_DISPLAY_SIZE
,
840 // As last resort use size of first frame as display size.
841 render_width
= img
->d_w
;
842 render_height
= img
->d_h
;
844 render_width
= render_size
[0];
845 render_height
= render_size
[1];
849 aom_img_alloc(NULL
, img
->fmt
, render_width
, render_height
, 16);
850 scaled_img
->bit_depth
= img
->bit_depth
;
853 if (img
->d_w
!= scaled_img
->d_w
|| img
->d_h
!= scaled_img
->d_h
) {
855 libyuv_scale(img
, scaled_img
, kFilterBox
);
859 "Failed to scale output frame: %s.\n"
860 "Scaling is disabled in this configuration. "
861 "To enable scaling, configure with --enable-libyuv\n",
862 aom_codec_error(&decoder
));
867 #if CONFIG_AOM_HIGHBITDEPTH
868 // Default to codec bit depth if output bit depth not set
869 if (!output_bit_depth
&& single_file
&& !do_md5
) {
870 output_bit_depth
= img
->bit_depth
;
872 // Shift up or down if necessary
873 if (output_bit_depth
!= 0 && output_bit_depth
!= img
->bit_depth
) {
874 const aom_img_fmt_t shifted_fmt
=
875 output_bit_depth
== 8
876 ? img
->fmt
^ (img
->fmt
& AOM_IMG_FMT_HIGHBITDEPTH
)
877 : img
->fmt
| AOM_IMG_FMT_HIGHBITDEPTH
;
879 img_shifted_realloc_required(img
, img_shifted
, shifted_fmt
)) {
880 aom_img_free(img_shifted
);
885 aom_img_alloc(NULL
, shifted_fmt
, img
->d_w
, img
->d_h
, 16);
886 img_shifted
->bit_depth
= output_bit_depth
;
888 if (output_bit_depth
> img
->bit_depth
) {
889 aom_img_upshift(img_shifted
, img
, output_bit_depth
- img
->bit_depth
);
891 aom_img_downshift(img_shifted
, img
,
892 img
->bit_depth
- output_bit_depth
);
899 aom_input_ctx
.width
= img
->d_w
;
900 aom_input_ctx
.height
= img
->d_h
;
901 #endif // CONFIG_EXT_TILE
905 char y4m_buf
[Y4M_BUFFER_SIZE
] = { 0 };
907 if (img
->fmt
== AOM_IMG_FMT_I440
|| img
->fmt
== AOM_IMG_FMT_I44016
) {
908 fprintf(stderr
, "Cannot produce y4m output for 440 sampling.\n");
911 if (frame_out
== 1) {
913 len
= y4m_write_file_header(
914 y4m_buf
, sizeof(y4m_buf
), aom_input_ctx
.width
,
915 aom_input_ctx
.height
, &aom_input_ctx
.framerate
, img
->fmt
,
918 MD5Update(&md5_ctx
, (md5byte
*)y4m_buf
, (unsigned int)len
);
920 fputs(y4m_buf
, outfile
);
925 len
= y4m_write_frame_header(y4m_buf
, sizeof(y4m_buf
));
927 MD5Update(&md5_ctx
, (md5byte
*)y4m_buf
, (unsigned int)len
);
929 fputs(y4m_buf
, outfile
);
932 if (frame_out
== 1) {
933 // Check if --yv12 or --i420 options are consistent with the
934 // bit-stream decoded
936 if (img
->fmt
!= AOM_IMG_FMT_I420
&&
937 img
->fmt
!= AOM_IMG_FMT_I42016
) {
938 fprintf(stderr
, "Cannot produce i420 output for bit-stream.\n");
943 if ((img
->fmt
!= AOM_IMG_FMT_I420
&&
944 img
->fmt
!= AOM_IMG_FMT_YV12
) ||
945 img
->bit_depth
!= 8) {
946 fprintf(stderr
, "Cannot produce yv12 output for bit-stream.\n");
954 update_image_md5(img
, planes
, &md5_ctx
);
956 write_image_file(img
, planes
, outfile
);
959 generate_filename(outfile_pattern
, outfile_name
, PATH_MAX
, img
->d_w
,
963 update_image_md5(img
, planes
, &md5_ctx
);
964 MD5Final(md5_digest
, &md5_ctx
);
965 print_md5(md5_digest
, outfile_name
);
967 outfile
= open_outfile(outfile_name
);
968 write_image_file(img
, planes
, outfile
);
975 if (summary
|| progress
) {
976 show_progress(frame_in
, frame_out
, dx_time
);
977 fprintf(stderr
, "\n");
980 if (frames_corrupted
) {
981 fprintf(stderr
, "WARNING: %d frames corrupted.\n", frames_corrupted
);
988 if (aom_codec_destroy(&decoder
)) {
989 fprintf(stderr
, "Failed to destroy decoder: %s\n",
990 aom_codec_error(&decoder
));
995 if (!noblit
&& single_file
) {
997 MD5Final(md5_digest
, &md5_ctx
);
998 print_md5(md5_digest
, outfile_name
);
1005 if (input
.aom_input_ctx
->file_type
== FILE_TYPE_WEBM
)
1006 webm_free(input
.webm_ctx
);
1009 if (input
.aom_input_ctx
->file_type
!= FILE_TYPE_WEBM
) free(buf
);
1011 if (scaled_img
) aom_img_free(scaled_img
);
1012 #if CONFIG_AOM_HIGHBITDEPTH
1013 if (img_shifted
) aom_img_free(img_shifted
);
1016 for (i
= 0; i
< ext_fb_list
.num_external_frame_buffers
; ++i
) {
1017 free(ext_fb_list
.ext_fb
[i
].data
);
1019 free(ext_fb_list
.ext_fb
);
1027 int main(int argc
, const char **argv_
) {
1028 unsigned int loops
= 1, i
;
1029 char **argv
, **argi
, **argj
;
1033 argv
= argv_dup(argc
- 1, argv_
+ 1);
1034 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
) {
1035 memset(&arg
, 0, sizeof(arg
));
1038 if (arg_match(&arg
, &looparg
, argi
)) {
1039 loops
= arg_parse_uint(&arg
);
1044 for (i
= 0; !error
&& i
< loops
; i
++) error
= main_loop(argc
, argv_
);