2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
18 #include "./vpx_config.h"
21 #include "third_party/libyuv/include/libyuv/scale.h"
27 #define VPX_CODEC_DISABLE_COMPAT 1
28 #include "vpx/vpx_decoder.h"
29 #include "vpx_ports/mem_ops.h"
30 #include "vpx_ports/vpx_timer.h"
32 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
33 #include "vpx/vp8dx.h"
36 #include "./md5_utils.h"
38 #include "./tools_common.h"
40 #include "./webmdec.h"
44 static const char *exec_name
;
46 struct VpxDecInputContext
{
47 struct VpxInputContext
*vpx_input_ctx
;
48 struct WebmInputContext
*webm_ctx
;
51 static const arg_def_t looparg
= ARG_DEF(NULL
, "loops", 1,
52 "Number of times to decode the file");
53 static const arg_def_t codecarg
= ARG_DEF(NULL
, "codec", 1,
55 static const arg_def_t use_yv12
= ARG_DEF(NULL
, "yv12", 0,
56 "Output raw YV12 frames");
57 static const arg_def_t use_i420
= ARG_DEF(NULL
, "i420", 0,
58 "Output raw I420 frames");
59 static const arg_def_t flipuvarg
= ARG_DEF(NULL
, "flipuv", 0,
60 "Flip the chroma planes in the output");
61 static const arg_def_t rawvideo
= ARG_DEF(NULL
, "rawvideo", 0,
62 "Output raw YUV frames");
63 static const arg_def_t noblitarg
= ARG_DEF(NULL
, "noblit", 0,
64 "Don't process the decoded frames");
65 static const arg_def_t progressarg
= ARG_DEF(NULL
, "progress", 0,
66 "Show progress after each frame decodes");
67 static const arg_def_t limitarg
= ARG_DEF(NULL
, "limit", 1,
68 "Stop decoding after n frames");
69 static const arg_def_t skiparg
= ARG_DEF(NULL
, "skip", 1,
70 "Skip the first n input frames");
71 static const arg_def_t postprocarg
= ARG_DEF(NULL
, "postproc", 0,
72 "Postprocess decoded frames");
73 static const arg_def_t summaryarg
= ARG_DEF(NULL
, "summary", 0,
74 "Show timing summary");
75 static const arg_def_t outputfile
= ARG_DEF("o", "output", 1,
76 "Output file name pattern (see below)");
77 static const arg_def_t threadsarg
= ARG_DEF("t", "threads", 1,
78 "Max threads to use");
79 static const arg_def_t verbosearg
= ARG_DEF("v", "verbose", 0,
80 "Show version string");
81 static const arg_def_t error_concealment
= ARG_DEF(NULL
, "error-concealment", 0,
82 "Enable decoder error-concealment");
83 static const arg_def_t scalearg
= ARG_DEF("S", "scale", 0,
84 "Scale output frames uniformly");
85 static const arg_def_t continuearg
=
86 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
88 static const arg_def_t fb_arg
=
89 ARG_DEF(NULL
, "frame-buffers", 1, "Number of frame buffers to use");
91 static const arg_def_t md5arg
= ARG_DEF(NULL
, "md5", 0,
92 "Compute the MD5 sum of the decoded frame");
93 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
94 static const arg_def_t outbitdeptharg
= ARG_DEF(
95 NULL
, "output-bit-depth", 1,
96 "Output bit-depth for decoded frames");
99 static const arg_def_t
*all_args
[] = {
100 &codecarg
, &use_yv12
, &use_i420
, &flipuvarg
, &rawvideo
, &noblitarg
,
101 &progressarg
, &limitarg
, &skiparg
, &postprocarg
, &summaryarg
, &outputfile
,
102 &threadsarg
, &verbosearg
, &scalearg
, &fb_arg
,
103 &md5arg
, &error_concealment
, &continuearg
,
104 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
110 #if CONFIG_VP8_DECODER
111 static const arg_def_t addnoise_level
= ARG_DEF(NULL
, "noise-level", 1,
112 "Enable VP8 postproc add noise");
113 static const arg_def_t deblock
= ARG_DEF(NULL
, "deblock", 0,
114 "Enable VP8 deblocking");
115 static const arg_def_t demacroblock_level
= ARG_DEF(NULL
, "demacroblock-level", 1,
116 "Enable VP8 demacroblocking, w/ level");
117 static const arg_def_t pp_debug_info
= ARG_DEF(NULL
, "pp-debug-info", 1,
118 "Enable VP8 visible debug info");
119 static const arg_def_t pp_disp_ref_frame
= ARG_DEF(NULL
, "pp-dbg-ref-frame", 1,
120 "Display only selected reference frame per macro block");
121 static const arg_def_t pp_disp_mb_modes
= ARG_DEF(NULL
, "pp-dbg-mb-modes", 1,
122 "Display only selected macro block modes");
123 static const arg_def_t pp_disp_b_modes
= ARG_DEF(NULL
, "pp-dbg-b-modes", 1,
124 "Display only selected block modes");
125 static const arg_def_t pp_disp_mvs
= ARG_DEF(NULL
, "pp-dbg-mvs", 1,
126 "Draw only selected motion vectors");
127 static const arg_def_t mfqe
= ARG_DEF(NULL
, "mfqe", 0,
128 "Enable multiframe quality enhancement");
130 static const arg_def_t
*vp8_pp_args
[] = {
131 &addnoise_level
, &deblock
, &demacroblock_level
, &pp_debug_info
,
132 &pp_disp_ref_frame
, &pp_disp_mb_modes
, &pp_disp_b_modes
, &pp_disp_mvs
, &mfqe
,
138 static INLINE
int vpx_image_scale(vpx_image_t
*src
, vpx_image_t
*dst
,
139 FilterModeEnum mode
) {
140 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
141 if (src
->fmt
== VPX_IMG_FMT_I42016
) {
142 assert(dst
->fmt
== VPX_IMG_FMT_I42016
);
143 return I420Scale_16((uint16_t*)src
->planes
[VPX_PLANE_Y
],
144 src
->stride
[VPX_PLANE_Y
]/2,
145 (uint16_t*)src
->planes
[VPX_PLANE_U
],
146 src
->stride
[VPX_PLANE_U
]/2,
147 (uint16_t*)src
->planes
[VPX_PLANE_V
],
148 src
->stride
[VPX_PLANE_V
]/2,
150 (uint16_t*)dst
->planes
[VPX_PLANE_Y
],
151 dst
->stride
[VPX_PLANE_Y
]/2,
152 (uint16_t*)dst
->planes
[VPX_PLANE_U
],
153 dst
->stride
[VPX_PLANE_U
]/2,
154 (uint16_t*)dst
->planes
[VPX_PLANE_V
],
155 dst
->stride
[VPX_PLANE_V
]/2,
160 assert(src
->fmt
== VPX_IMG_FMT_I420
);
161 assert(dst
->fmt
== VPX_IMG_FMT_I420
);
162 return I420Scale(src
->planes
[VPX_PLANE_Y
], src
->stride
[VPX_PLANE_Y
],
163 src
->planes
[VPX_PLANE_U
], src
->stride
[VPX_PLANE_U
],
164 src
->planes
[VPX_PLANE_V
], src
->stride
[VPX_PLANE_V
],
166 dst
->planes
[VPX_PLANE_Y
], dst
->stride
[VPX_PLANE_Y
],
167 dst
->planes
[VPX_PLANE_U
], dst
->stride
[VPX_PLANE_U
],
168 dst
->planes
[VPX_PLANE_V
], dst
->stride
[VPX_PLANE_V
],
177 fprintf(stderr
, "Usage: %s <options> filename\n\n"
178 "Options:\n", exec_name
);
179 arg_show_usage(stderr
, all_args
);
180 #if CONFIG_VP8_DECODER
181 fprintf(stderr
, "\nVP8 Postprocessing Options:\n");
182 arg_show_usage(stderr
, vp8_pp_args
);
185 "\nOutput File Patterns:\n\n"
186 " The -o argument specifies the name of the file(s) to "
187 "write to. If the\n argument does not include any escape "
188 "characters, the output will be\n written to a single file. "
189 "Otherwise, the filename will be calculated by\n expanding "
190 "the following escape characters:\n");
192 "\n\t%%w - Frame width"
193 "\n\t%%h - Frame height"
194 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
195 "\n\n Pattern arguments are only supported in conjunction "
196 "with the --yv12 and\n --i420 options. If the -o option is "
197 "not specified, the output will be\n directed to stdout.\n"
199 fprintf(stderr
, "\nIncluded decoders:\n\n");
201 for (i
= 0; i
< get_vpx_decoder_count(); ++i
) {
202 const VpxInterface
*const decoder
= get_vpx_decoder_by_index(i
);
203 fprintf(stderr
, " %-6s - %s\n",
204 decoder
->name
, vpx_codec_iface_name(decoder
->codec_interface()));
210 static int raw_read_frame(FILE *infile
, uint8_t **buffer
,
211 size_t *bytes_read
, size_t *buffer_size
) {
212 char raw_hdr
[RAW_FRAME_HDR_SZ
];
213 size_t frame_size
= 0;
215 if (fread(raw_hdr
, RAW_FRAME_HDR_SZ
, 1, infile
) != 1) {
217 warn("Failed to read RAW frame size\n");
219 const size_t kCorruptFrameThreshold
= 256 * 1024 * 1024;
220 const size_t kFrameTooSmallThreshold
= 256 * 1024;
221 frame_size
= mem_get_le32(raw_hdr
);
223 if (frame_size
> kCorruptFrameThreshold
) {
224 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size
);
228 if (frame_size
< kFrameTooSmallThreshold
) {
229 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
230 (unsigned int)frame_size
);
233 if (frame_size
> *buffer_size
) {
234 uint8_t *new_buf
= realloc(*buffer
, 2 * frame_size
);
237 *buffer_size
= 2 * frame_size
;
239 warn("Failed to allocate compressed data buffer\n");
246 if (fread(*buffer
, 1, frame_size
, infile
) != frame_size
) {
247 warn("Failed to read full frame\n");
250 *bytes_read
= frame_size
;
256 static int read_frame(struct VpxDecInputContext
*input
, uint8_t **buf
,
257 size_t *bytes_in_buffer
, size_t *buffer_size
) {
258 switch (input
->vpx_input_ctx
->file_type
) {
261 return webm_read_frame(input
->webm_ctx
,
262 buf
, bytes_in_buffer
, buffer_size
);
265 return raw_read_frame(input
->vpx_input_ctx
->file
,
266 buf
, bytes_in_buffer
, buffer_size
);
268 return ivf_read_frame(input
->vpx_input_ctx
->file
,
269 buf
, bytes_in_buffer
, buffer_size
);
275 static void update_image_md5(const vpx_image_t
*img
, const int planes
[3],
279 for (i
= 0; i
< 3; ++i
) {
280 const int plane
= planes
[i
];
281 const unsigned char *buf
= img
->planes
[plane
];
282 const int stride
= img
->stride
[plane
];
283 const int w
= vpx_img_plane_width(img
, plane
);
284 const int h
= vpx_img_plane_height(img
, plane
);
286 for (y
= 0; y
< h
; ++y
) {
287 MD5Update(md5
, buf
, w
);
293 static void write_image_file(const vpx_image_t
*img
, const int planes
[3],
296 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
297 const int bytes_per_sample
= ((img
->fmt
& VPX_IMG_FMT_HIGHBITDEPTH
) ? 2 : 1);
299 const int bytes_per_sample
= 1;
302 for (i
= 0; i
< 3; ++i
) {
303 const int plane
= planes
[i
];
304 const unsigned char *buf
= img
->planes
[plane
];
305 const int stride
= img
->stride
[plane
];
306 const int w
= vpx_img_plane_width(img
, plane
);
307 const int h
= vpx_img_plane_height(img
, plane
);
309 for (y
= 0; y
< h
; ++y
) {
310 fwrite(buf
, bytes_per_sample
, w
, file
);
316 int file_is_raw(struct VpxInputContext
*input
) {
319 vpx_codec_stream_info_t si
;
323 if (fread(buf
, 1, 32, input
->file
) == 32) {
326 if (mem_get_le32(buf
) < 256 * 1024 * 1024) {
327 for (i
= 0; i
< get_vpx_decoder_count(); ++i
) {
328 const VpxInterface
*const decoder
= get_vpx_decoder_by_index(i
);
329 if (!vpx_codec_peek_stream_info(decoder
->codec_interface(),
330 buf
+ 4, 32 - 4, &si
)) {
332 input
->fourcc
= decoder
->fourcc
;
334 input
->height
= si
.h
;
335 input
->framerate
.numerator
= 30;
336 input
->framerate
.denominator
= 1;
347 void show_progress(int frame_in
, int frame_out
, uint64_t dx_time
) {
349 "%d decoded frames/%d showed frames in %"PRId64
" us (%.2f fps)\r",
350 frame_in
, frame_out
, dx_time
,
351 (double)frame_out
* 1000000.0 / (double)dx_time
);
354 struct ExternalFrameBuffer
{
360 struct ExternalFrameBufferList
{
361 int num_external_frame_buffers
;
362 struct ExternalFrameBuffer
*ext_fb
;
365 // Callback used by libvpx to request an external frame buffer. |cb_priv|
366 // Application private data passed into the set function. |min_size| is the
367 // minimum size in bytes needed to decode the next frame. |fb| pointer to the
369 int get_vp9_frame_buffer(void *cb_priv
, size_t min_size
,
370 vpx_codec_frame_buffer_t
*fb
) {
372 struct ExternalFrameBufferList
*const ext_fb_list
=
373 (struct ExternalFrameBufferList
*)cb_priv
;
374 if (ext_fb_list
== NULL
)
377 // Find a free frame buffer.
378 for (i
= 0; i
< ext_fb_list
->num_external_frame_buffers
; ++i
) {
379 if (!ext_fb_list
->ext_fb
[i
].in_use
)
383 if (i
== ext_fb_list
->num_external_frame_buffers
)
386 if (ext_fb_list
->ext_fb
[i
].size
< min_size
) {
387 free(ext_fb_list
->ext_fb
[i
].data
);
388 ext_fb_list
->ext_fb
[i
].data
= (uint8_t *)malloc(min_size
);
389 if (!ext_fb_list
->ext_fb
[i
].data
)
392 ext_fb_list
->ext_fb
[i
].size
= min_size
;
395 fb
->data
= ext_fb_list
->ext_fb
[i
].data
;
396 fb
->size
= ext_fb_list
->ext_fb
[i
].size
;
397 ext_fb_list
->ext_fb
[i
].in_use
= 1;
399 // Set the frame buffer's private data to point at the external frame buffer.
400 fb
->priv
= &ext_fb_list
->ext_fb
[i
];
404 // Callback used by libvpx when there are no references to the frame buffer.
405 // |cb_priv| user private data passed into the set function. |fb| pointer
406 // to the frame buffer.
407 int release_vp9_frame_buffer(void *cb_priv
,
408 vpx_codec_frame_buffer_t
*fb
) {
409 struct ExternalFrameBuffer
*const ext_fb
=
410 (struct ExternalFrameBuffer
*)fb
->priv
;
416 void generate_filename(const char *pattern
, char *out
, size_t q_len
,
417 unsigned int d_w
, unsigned int d_h
,
418 unsigned int frame_in
) {
419 const char *p
= pattern
;
423 char *next_pat
= strchr(p
, '%');
428 /* parse the pattern */
432 snprintf(q
, q_len
- 1, "%d", d_w
);
435 snprintf(q
, q_len
- 1, "%d", d_h
);
438 snprintf(q
, q_len
- 1, "%d", frame_in
);
441 snprintf(q
, q_len
- 1, "%02d", frame_in
);
444 snprintf(q
, q_len
- 1, "%03d", frame_in
);
447 snprintf(q
, q_len
- 1, "%04d", frame_in
);
450 snprintf(q
, q_len
- 1, "%05d", frame_in
);
453 snprintf(q
, q_len
- 1, "%06d", frame_in
);
456 snprintf(q
, q_len
- 1, "%07d", frame_in
);
459 snprintf(q
, q_len
- 1, "%08d", frame_in
);
462 snprintf(q
, q_len
- 1, "%09d", frame_in
);
465 die("Unrecognized pattern %%%c\n", p
[1]);
470 if (pat_len
>= q_len
- 1)
471 die("Output filename too long.\n");
478 /* copy the next segment */
480 copy_len
= strlen(p
);
482 copy_len
= next_pat
- p
;
484 if (copy_len
>= q_len
- 1)
485 die("Output filename too long.\n");
487 memcpy(q
, p
, copy_len
);
496 static int is_single_file(const char *outfile_pattern
) {
497 const char *p
= outfile_pattern
;
501 if (p
&& p
[1] >= '1' && p
[1] <= '9')
502 return 0; // pattern contains sequence number, so it's not unique
510 static void print_md5(unsigned char digest
[16], const char *filename
) {
513 for (i
= 0; i
< 16; ++i
)
514 printf("%02x", digest
[i
]);
515 printf(" %s\n", filename
);
518 static FILE *open_outfile(const char *name
) {
519 if (strcmp("-", name
) == 0) {
520 set_binary_mode(stdout
);
523 FILE *file
= fopen(name
, "wb");
525 fatal("Failed to output file %s", name
);
530 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
531 static void high_img_upshift(vpx_image_t
*dst
, vpx_image_t
*src
,
533 const int offset
= input_shift
> 0 ? (1 << (input_shift
- 1)) : 0;
535 if (dst
->d_w
!= src
->d_w
|| dst
->d_h
!= src
->d_h
||
536 dst
->x_chroma_shift
!= src
->x_chroma_shift
||
537 dst
->y_chroma_shift
!= src
->y_chroma_shift
||
538 dst
->fmt
!= src
->fmt
|| input_shift
< 0) {
539 fatal("Unsupported image conversion");
542 case VPX_IMG_FMT_I42016
:
543 case VPX_IMG_FMT_I42216
:
544 case VPX_IMG_FMT_I44416
:
547 fatal("Unsupported image conversion");
550 for (plane
= 0; plane
< 3; plane
++) {
555 w
>>= src
->x_chroma_shift
;
556 h
>>= src
->y_chroma_shift
;
558 for (y
= 0; y
< h
; y
++) {
559 uint16_t *p_src
= (uint16_t *)(src
->planes
[plane
] +
560 y
* src
->stride
[plane
]);
561 uint16_t *p_dst
= (uint16_t *)(dst
->planes
[plane
] +
562 y
* dst
->stride
[plane
]);
563 for (x
= 0; x
< w
; x
++)
564 *p_dst
++ = (*p_src
++ << input_shift
) + offset
;
569 static void low_img_upshift(vpx_image_t
*dst
, vpx_image_t
*src
,
571 const int offset
= input_shift
> 0 ? (1 << (input_shift
- 1)) : 0;
573 if (dst
->d_w
!= src
->d_w
|| dst
->d_h
!= src
->d_h
||
574 dst
->x_chroma_shift
!= src
->x_chroma_shift
||
575 dst
->y_chroma_shift
!= src
->y_chroma_shift
||
576 dst
->fmt
!= src
->fmt
+ VPX_IMG_FMT_HIGHBITDEPTH
||
578 fatal("Unsupported image conversion");
581 case VPX_IMG_FMT_I420
:
582 case VPX_IMG_FMT_I422
:
583 case VPX_IMG_FMT_I444
:
586 fatal("Unsupported image conversion");
589 for (plane
= 0; plane
< 3; plane
++) {
594 w
>>= src
->x_chroma_shift
;
595 h
>>= src
->y_chroma_shift
;
597 for (y
= 0; y
< h
; y
++) {
598 uint8_t *p_src
= src
->planes
[plane
] + y
* src
->stride
[plane
];
599 uint16_t *p_dst
= (uint16_t *)(dst
->planes
[plane
] +
600 y
* dst
->stride
[plane
]);
601 for (x
= 0; x
< w
; x
++) {
602 *p_dst
++ = (*p_src
++ << input_shift
) + offset
;
608 static void img_upshift(vpx_image_t
*dst
, vpx_image_t
*src
,
610 if (src
->fmt
& VPX_IMG_FMT_HIGHBITDEPTH
) {
611 high_img_upshift(dst
, src
, input_shift
);
613 low_img_upshift(dst
, src
, input_shift
);
617 static void high_img_downshift(vpx_image_t
*dst
, vpx_image_t
*src
,
620 if (dst
->d_w
!= src
->d_w
|| dst
->d_h
!= src
->d_h
||
621 dst
->x_chroma_shift
!= src
->x_chroma_shift
||
622 dst
->y_chroma_shift
!= src
->y_chroma_shift
||
623 dst
->fmt
!= src
->fmt
|| down_shift
< 0) {
624 fatal("Unsupported image conversion");
627 case VPX_IMG_FMT_I42016
:
628 case VPX_IMG_FMT_I42216
:
629 case VPX_IMG_FMT_I44416
:
632 fatal("Unsupported image conversion");
635 for (plane
= 0; plane
< 3; plane
++) {
640 w
>>= src
->x_chroma_shift
;
641 h
>>= src
->y_chroma_shift
;
643 for (y
= 0; y
< h
; y
++) {
644 uint16_t *p_src
= (uint16_t *)(src
->planes
[plane
] +
645 y
* src
->stride
[plane
]);
646 uint16_t *p_dst
= (uint16_t *)(dst
->planes
[plane
] +
647 y
* dst
->stride
[plane
]);
648 for (x
= 0; x
< w
; x
++)
649 *p_dst
++ = *p_src
++ >> down_shift
;
654 static void low_img_downshift(vpx_image_t
*dst
, vpx_image_t
*src
,
657 if (dst
->d_w
!= src
->d_w
|| dst
->d_h
!= src
->d_h
||
658 dst
->x_chroma_shift
!= src
->x_chroma_shift
||
659 dst
->y_chroma_shift
!= src
->y_chroma_shift
||
660 src
->fmt
!= dst
->fmt
+ VPX_IMG_FMT_HIGHBITDEPTH
||
662 fatal("Unsupported image conversion");
665 case VPX_IMG_FMT_I420
:
666 case VPX_IMG_FMT_I422
:
667 case VPX_IMG_FMT_I444
:
670 fatal("Unsupported image conversion");
673 for (plane
= 0; plane
< 3; plane
++) {
678 w
>>= src
->x_chroma_shift
;
679 h
>>= src
->y_chroma_shift
;
681 for (y
= 0; y
< h
; y
++) {
682 uint16_t *p_src
= (uint16_t *)(src
->planes
[plane
] +
683 y
* src
->stride
[plane
]);
684 uint8_t *p_dst
= dst
->planes
[plane
] + y
* dst
->stride
[plane
];
685 for (x
= 0; x
< w
; x
++) {
686 *p_dst
++ = *p_src
++ >> down_shift
;
692 static void img_downshift(vpx_image_t
*dst
, vpx_image_t
*src
,
694 if (dst
->fmt
& VPX_IMG_FMT_HIGHBITDEPTH
) {
695 high_img_downshift(dst
, src
, down_shift
);
697 low_img_downshift(dst
, src
, down_shift
);
702 int main_loop(int argc
, const char **argv_
) {
703 vpx_codec_ctx_t decoder
;
707 size_t bytes_in_buffer
= 0, buffer_size
= 0;
709 int frame_in
= 0, frame_out
= 0, flipuv
= 0, noblit
= 0;
710 int do_md5
= 0, progress
= 0;
711 int stop_after
= 0, postproc
= 0, summary
= 0, quiet
= 1;
715 const VpxInterface
*interface
= NULL
;
716 const VpxInterface
*fourcc_interface
= NULL
;
717 uint64_t dx_time
= 0;
719 char **argv
, **argi
, **argj
;
725 vpx_codec_dec_cfg_t cfg
= {0, 0, 0};
726 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
727 int output_bit_depth
= 0;
729 #if CONFIG_VP8_DECODER
730 vp8_postproc_cfg_t vp8_pp_cfg
= {0};
731 int vp8_dbg_color_ref_frame
= 0;
732 int vp8_dbg_color_mb_modes
= 0;
733 int vp8_dbg_color_b_modes
= 0;
734 int vp8_dbg_display_mv
= 0;
736 int frames_corrupted
= 0;
739 vpx_image_t
*scaled_img
= NULL
;
740 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
741 vpx_image_t
*img_shifted
= NULL
;
743 int frame_avail
, got_data
;
744 int num_external_frame_buffers
= 0;
745 struct ExternalFrameBufferList ext_fb_list
= {0, NULL
};
747 const char *outfile_pattern
= NULL
;
748 char outfile_name
[PATH_MAX
] = {0};
749 FILE *outfile
= NULL
;
752 unsigned char md5_digest
[16];
754 struct VpxDecInputContext input
= {NULL
, NULL
};
755 struct VpxInputContext vpx_input_ctx
;
757 struct WebmInputContext webm_ctx
;
758 memset(&(webm_ctx
), 0, sizeof(webm_ctx
));
759 input
.webm_ctx
= &webm_ctx
;
761 input
.vpx_input_ctx
= &vpx_input_ctx
;
763 /* Parse command line */
764 exec_name
= argv_
[0];
765 argv
= argv_dup(argc
- 1, argv_
+ 1);
767 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
) {
768 memset(&arg
, 0, sizeof(arg
));
771 if (arg_match(&arg
, &codecarg
, argi
)) {
772 interface
= get_vpx_decoder_by_name(arg
.val
);
774 die("Error: Unrecognized argument (%s) to --codec\n", arg
.val
);
775 } else if (arg_match(&arg
, &looparg
, argi
)) {
777 } else if (arg_match(&arg
, &outputfile
, argi
))
778 outfile_pattern
= arg
.val
;
779 else if (arg_match(&arg
, &use_yv12
, argi
)) {
783 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
784 output_bit_depth
= 8; // For yv12 8-bit depth output is assumed
786 } else if (arg_match(&arg
, &use_i420
, argi
)) {
790 } else if (arg_match(&arg
, &rawvideo
, argi
)) {
792 } else if (arg_match(&arg
, &flipuvarg
, argi
))
794 else if (arg_match(&arg
, &noblitarg
, argi
))
796 else if (arg_match(&arg
, &progressarg
, argi
))
798 else if (arg_match(&arg
, &limitarg
, argi
))
799 stop_after
= arg_parse_uint(&arg
);
800 else if (arg_match(&arg
, &skiparg
, argi
))
801 arg_skip
= arg_parse_uint(&arg
);
802 else if (arg_match(&arg
, &postprocarg
, argi
))
804 else if (arg_match(&arg
, &md5arg
, argi
))
806 else if (arg_match(&arg
, &summaryarg
, argi
))
808 else if (arg_match(&arg
, &threadsarg
, argi
))
809 cfg
.threads
= arg_parse_uint(&arg
);
810 else if (arg_match(&arg
, &verbosearg
, argi
))
812 else if (arg_match(&arg
, &scalearg
, argi
))
814 else if (arg_match(&arg
, &fb_arg
, argi
))
815 num_external_frame_buffers
= arg_parse_uint(&arg
);
816 else if (arg_match(&arg
, &continuearg
, argi
))
818 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
819 else if (arg_match(&arg
, &outbitdeptharg
, argi
)) {
820 output_bit_depth
= arg_parse_uint(&arg
);
823 #if CONFIG_VP8_DECODER
824 else if (arg_match(&arg
, &addnoise_level
, argi
)) {
826 vp8_pp_cfg
.post_proc_flag
|= VP8_ADDNOISE
;
827 vp8_pp_cfg
.noise_level
= arg_parse_uint(&arg
);
828 } else if (arg_match(&arg
, &demacroblock_level
, argi
)) {
830 vp8_pp_cfg
.post_proc_flag
|= VP8_DEMACROBLOCK
;
831 vp8_pp_cfg
.deblocking_level
= arg_parse_uint(&arg
);
832 } else if (arg_match(&arg
, &deblock
, argi
)) {
834 vp8_pp_cfg
.post_proc_flag
|= VP8_DEBLOCK
;
835 } else if (arg_match(&arg
, &mfqe
, argi
)) {
837 vp8_pp_cfg
.post_proc_flag
|= VP8_MFQE
;
838 } else if (arg_match(&arg
, &pp_debug_info
, argi
)) {
839 unsigned int level
= arg_parse_uint(&arg
);
842 vp8_pp_cfg
.post_proc_flag
&= ~0x7;
845 vp8_pp_cfg
.post_proc_flag
|= level
;
846 } else if (arg_match(&arg
, &pp_disp_ref_frame
, argi
)) {
847 unsigned int flags
= arg_parse_int(&arg
);
850 vp8_dbg_color_ref_frame
= flags
;
852 } else if (arg_match(&arg
, &pp_disp_mb_modes
, argi
)) {
853 unsigned int flags
= arg_parse_int(&arg
);
856 vp8_dbg_color_mb_modes
= flags
;
858 } else if (arg_match(&arg
, &pp_disp_b_modes
, argi
)) {
859 unsigned int flags
= arg_parse_int(&arg
);
862 vp8_dbg_color_b_modes
= flags
;
864 } else if (arg_match(&arg
, &pp_disp_mvs
, argi
)) {
865 unsigned int flags
= arg_parse_int(&arg
);
868 vp8_dbg_display_mv
= flags
;
870 } else if (arg_match(&arg
, &error_concealment
, argi
)) {
873 #endif // CONFIG_VP8_DECODER
878 /* Check for unrecognized options */
879 for (argi
= argv
; *argi
; argi
++)
880 if (argi
[0][0] == '-' && strlen(argi
[0]) > 1)
881 die("Error: Unrecognized option %s\n", *argi
);
883 /* Handle non-option arguments */
890 infile
= strcmp(fn
, "-") ? fopen(fn
, "rb") : set_binary_mode(stdin
);
893 fprintf(stderr
, "Failed to open file '%s'", strcmp(fn
, "-") ? fn
: "stdin");
896 #if CONFIG_OS_SUPPORT
897 /* Make sure we don't dump to the terminal, unless forced to with -o - */
898 if (!outfile_pattern
&& isatty(fileno(stdout
)) && !do_md5
&& !noblit
) {
900 "Not dumping raw video to your terminal. Use '-o -' to "
905 input
.vpx_input_ctx
->file
= infile
;
906 if (file_is_ivf(input
.vpx_input_ctx
))
907 input
.vpx_input_ctx
->file_type
= FILE_TYPE_IVF
;
909 else if (file_is_webm(input
.webm_ctx
, input
.vpx_input_ctx
))
910 input
.vpx_input_ctx
->file_type
= FILE_TYPE_WEBM
;
912 else if (file_is_raw(input
.vpx_input_ctx
))
913 input
.vpx_input_ctx
->file_type
= FILE_TYPE_RAW
;
915 fprintf(stderr
, "Unrecognized input file type.\n");
917 fprintf(stderr
, "vpxdec was built without WebM container support.\n");
922 outfile_pattern
= outfile_pattern
? outfile_pattern
: "-";
923 single_file
= is_single_file(outfile_pattern
);
925 if (!noblit
&& single_file
) {
926 generate_filename(outfile_pattern
, outfile_name
, PATH_MAX
,
927 vpx_input_ctx
.width
, vpx_input_ctx
.height
, 0);
931 outfile
= open_outfile(outfile_name
);
934 if (use_y4m
&& !noblit
) {
936 fprintf(stderr
, "YUV4MPEG2 not supported with output patterns,"
937 " try --i420 or --yv12.\n");
942 if (vpx_input_ctx
.file_type
== FILE_TYPE_WEBM
) {
943 if (webm_guess_framerate(input
.webm_ctx
, input
.vpx_input_ctx
)) {
944 fprintf(stderr
, "Failed to guess framerate -- error parsing "
952 fourcc_interface
= get_vpx_decoder_by_fourcc(vpx_input_ctx
.fourcc
);
953 if (interface
&& fourcc_interface
&& interface
!= fourcc_interface
)
954 warn("Header indicates codec: %s\n", fourcc_interface
->name
);
956 interface
= fourcc_interface
;
959 interface
= get_vpx_decoder_by_index(0);
961 dec_flags
= (postproc
? VPX_CODEC_USE_POSTPROC
: 0) |
962 (ec_enabled
? VPX_CODEC_USE_ERROR_CONCEALMENT
: 0);
963 if (vpx_codec_dec_init(&decoder
, interface
->codec_interface(),
965 fprintf(stderr
, "Failed to initialize decoder: %s\n",
966 vpx_codec_error(&decoder
));
971 fprintf(stderr
, "%s\n", decoder
.name
);
973 #if CONFIG_VP8_DECODER
975 if (vp8_pp_cfg
.post_proc_flag
976 && vpx_codec_control(&decoder
, VP8_SET_POSTPROC
, &vp8_pp_cfg
)) {
977 fprintf(stderr
, "Failed to configure postproc: %s\n", vpx_codec_error(&decoder
));
981 if (vp8_dbg_color_ref_frame
982 && vpx_codec_control(&decoder
, VP8_SET_DBG_COLOR_REF_FRAME
, vp8_dbg_color_ref_frame
)) {
983 fprintf(stderr
, "Failed to configure reference block visualizer: %s\n", vpx_codec_error(&decoder
));
987 if (vp8_dbg_color_mb_modes
988 && vpx_codec_control(&decoder
, VP8_SET_DBG_COLOR_MB_MODES
, vp8_dbg_color_mb_modes
)) {
989 fprintf(stderr
, "Failed to configure macro block visualizer: %s\n", vpx_codec_error(&decoder
));
993 if (vp8_dbg_color_b_modes
994 && vpx_codec_control(&decoder
, VP8_SET_DBG_COLOR_B_MODES
, vp8_dbg_color_b_modes
)) {
995 fprintf(stderr
, "Failed to configure block visualizer: %s\n", vpx_codec_error(&decoder
));
999 if (vp8_dbg_display_mv
1000 && vpx_codec_control(&decoder
, VP8_SET_DBG_DISPLAY_MV
, vp8_dbg_display_mv
)) {
1001 fprintf(stderr
, "Failed to configure motion vector visualizer: %s\n", vpx_codec_error(&decoder
));
1002 return EXIT_FAILURE
;
1008 fprintf(stderr
, "Skipping first %d frames.\n", arg_skip
);
1010 if (read_frame(&input
, &buf
, &bytes_in_buffer
, &buffer_size
))
1015 if (num_external_frame_buffers
> 0) {
1016 ext_fb_list
.num_external_frame_buffers
= num_external_frame_buffers
;
1017 ext_fb_list
.ext_fb
= (struct ExternalFrameBuffer
*)calloc(
1018 num_external_frame_buffers
, sizeof(*ext_fb_list
.ext_fb
));
1019 if (vpx_codec_set_frame_buffer_functions(
1020 &decoder
, get_vp9_frame_buffer
, release_vp9_frame_buffer
,
1022 fprintf(stderr
, "Failed to configure external frame buffers: %s\n",
1023 vpx_codec_error(&decoder
));
1024 return EXIT_FAILURE
;
1032 while (frame_avail
|| got_data
) {
1033 vpx_codec_iter_t iter
= NULL
;
1035 struct vpx_usec_timer timer
;
1039 if (!stop_after
|| frame_in
< stop_after
) {
1040 if (!read_frame(&input
, &buf
, &bytes_in_buffer
, &buffer_size
)) {
1044 vpx_usec_timer_start(&timer
);
1046 if (vpx_codec_decode(&decoder
, buf
, (unsigned int)bytes_in_buffer
,
1048 const char *detail
= vpx_codec_error_detail(&decoder
);
1049 warn("Failed to decode frame %d: %s",
1050 frame_in
, vpx_codec_error(&decoder
));
1053 warn("Additional information: %s", detail
);
1058 vpx_usec_timer_mark(&timer
);
1059 dx_time
+= vpx_usec_timer_elapsed(&timer
);
1063 vpx_usec_timer_start(&timer
);
1066 if ((img
= vpx_codec_get_frame(&decoder
, &iter
))) {
1071 vpx_usec_timer_mark(&timer
);
1072 dx_time
+= (unsigned int)vpx_usec_timer_elapsed(&timer
);
1074 if (vpx_codec_control(&decoder
, VP8D_GET_FRAME_CORRUPTED
, &corrupted
)) {
1075 warn("Failed VP8_GET_FRAME_CORRUPTED: %s", vpx_codec_error(&decoder
));
1078 frames_corrupted
+= corrupted
;
1081 show_progress(frame_in
, frame_out
, dx_time
);
1083 if (!noblit
&& img
) {
1084 const int PLANES_YUV
[] = {VPX_PLANE_Y
, VPX_PLANE_U
, VPX_PLANE_V
};
1085 const int PLANES_YVU
[] = {VPX_PLANE_Y
, VPX_PLANE_V
, VPX_PLANE_U
};
1086 const int *planes
= flipuv
? PLANES_YVU
: PLANES_YUV
;
1089 if (frame_out
== 1) {
1090 // If the output frames are to be scaled to a fixed display size then
1091 // use the width and height specified in the container. If either of
1092 // these is set to 0, use the display size set in the first frame
1093 // header. If that is unavailable, use the raw decoded size of the
1094 // first decoded frame.
1095 int display_width
= vpx_input_ctx
.width
;
1096 int display_height
= vpx_input_ctx
.height
;
1097 if (!display_width
|| !display_height
) {
1098 int display_size
[2];
1099 if (vpx_codec_control(&decoder
, VP9D_GET_DISPLAY_SIZE
,
1101 // As last resort use size of first frame as display size.
1102 display_width
= img
->d_w
;
1103 display_height
= img
->d_h
;
1105 display_width
= display_size
[0];
1106 display_height
= display_size
[1];
1109 scaled_img
= vpx_img_alloc(NULL
, img
->fmt
, display_width
,
1110 display_height
, 16);
1111 scaled_img
->bit_depth
= img
->bit_depth
;
1114 if (img
->d_w
!= scaled_img
->d_w
|| img
->d_h
!= scaled_img
->d_h
) {
1116 vpx_image_scale(img
, scaled_img
, kFilterBox
);
1119 fprintf(stderr
, "Failed to scale output frame: %s.\n"
1120 "Scaling is disabled in this configuration. "
1121 "To enable scaling, configure with --enable-libyuv\n",
1122 vpx_codec_error(&decoder
));
1123 return EXIT_FAILURE
;
1127 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
1128 // Default to codec bit depth if output bit depth not set
1129 if (!output_bit_depth
) {
1130 output_bit_depth
= img
->bit_depth
;
1132 // Shift up or down if necessary
1133 if (output_bit_depth
!= img
->bit_depth
) {
1135 if (output_bit_depth
== 8) {
1136 img_shifted
= vpx_img_alloc(
1137 NULL
, img
->fmt
- VPX_IMG_FMT_HIGHBITDEPTH
,
1138 img
->d_w
, img
->d_h
, 16);
1140 img_shifted
= vpx_img_alloc(
1141 NULL
, img
->fmt
| VPX_IMG_FMT_HIGHBITDEPTH
,
1142 img
->d_w
, img
->d_h
, 16);
1144 img_shifted
->bit_depth
= output_bit_depth
;
1146 if (output_bit_depth
> img
->bit_depth
) {
1147 img_upshift(img_shifted
, img
, output_bit_depth
- img
->bit_depth
);
1149 img_downshift(img_shifted
, img
, img
->bit_depth
- output_bit_depth
);
1157 char buf
[Y4M_BUFFER_SIZE
] = {0};
1159 if (frame_out
== 1) {
1161 len
= y4m_write_file_header(buf
, sizeof(buf
),
1162 vpx_input_ctx
.width
,
1163 vpx_input_ctx
.height
,
1164 &vpx_input_ctx
.framerate
,
1165 img
->fmt
, img
->bit_depth
);
1167 MD5Update(&md5_ctx
, (md5byte
*)buf
, (unsigned int)len
);
1169 fputs(buf
, outfile
);
1174 len
= y4m_write_frame_header(buf
, sizeof(buf
));
1176 MD5Update(&md5_ctx
, (md5byte
*)buf
, (unsigned int)len
);
1178 fputs(buf
, outfile
);
1181 if (frame_out
== 1) {
1182 // Check if --yv12 or --i420 options are consistent with the
1183 // bit-stream decoded
1185 if (img
->fmt
!= VPX_IMG_FMT_I420
&&
1186 img
->fmt
!= VPX_IMG_FMT_I42016
) {
1187 fprintf(stderr
, "Cannot produce i420 output for bit-stream.\n");
1192 if ((img
->fmt
!= VPX_IMG_FMT_I420
&&
1193 img
->fmt
!= VPX_IMG_FMT_YV12
) || img
->bit_depth
!= 8) {
1194 fprintf(stderr
, "Cannot produce yv12 output for bit-stream.\n");
1202 update_image_md5(img
, planes
, &md5_ctx
);
1204 write_image_file(img
, planes
, outfile
);
1207 generate_filename(outfile_pattern
, outfile_name
, PATH_MAX
,
1208 img
->d_w
, img
->d_h
, frame_in
);
1211 update_image_md5(img
, planes
, &md5_ctx
);
1212 MD5Final(md5_digest
, &md5_ctx
);
1213 print_md5(md5_digest
, outfile_name
);
1215 outfile
= open_outfile(outfile_name
);
1216 write_image_file(img
, planes
, outfile
);
1222 if (stop_after
&& frame_in
>= stop_after
)
1226 if (summary
|| progress
) {
1227 show_progress(frame_in
, frame_out
, dx_time
);
1228 fprintf(stderr
, "\n");
1231 if (frames_corrupted
)
1232 fprintf(stderr
, "WARNING: %d frames corrupted.\n", frames_corrupted
);
1236 if (vpx_codec_destroy(&decoder
)) {
1237 fprintf(stderr
, "Failed to destroy decoder: %s\n",
1238 vpx_codec_error(&decoder
));
1239 return EXIT_FAILURE
;
1242 if (!noblit
&& single_file
) {
1244 MD5Final(md5_digest
, &md5_ctx
);
1245 print_md5(md5_digest
, outfile_name
);
1252 if (input
.vpx_input_ctx
->file_type
== FILE_TYPE_WEBM
)
1253 webm_free(input
.webm_ctx
);
1256 if (input
.vpx_input_ctx
->file_type
!= FILE_TYPE_WEBM
)
1259 if (scaled_img
) vpx_img_free(scaled_img
);
1260 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
1261 if (img_shifted
) vpx_img_free(img_shifted
);
1264 for (i
= 0; i
< ext_fb_list
.num_external_frame_buffers
; ++i
) {
1265 free(ext_fb_list
.ext_fb
[i
].data
);
1267 free(ext_fb_list
.ext_fb
);
1272 return frames_corrupted
? EXIT_FAILURE
: EXIT_SUCCESS
;
1275 int main(int argc
, const char **argv_
) {
1276 unsigned int loops
= 1, i
;
1277 char **argv
, **argi
, **argj
;
1281 argv
= argv_dup(argc
- 1, argv_
+ 1);
1282 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
) {
1283 memset(&arg
, 0, sizeof(arg
));
1286 if (arg_match(&arg
, &looparg
, argi
)) {
1287 loops
= arg_parse_uint(&arg
);
1292 for (i
= 0; !error
&& i
< loops
; i
++)
1293 error
= main_loop(argc
, argv_
);