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");
94 static const arg_def_t *all_args[] = {
95 &codecarg, &use_yv12, &use_i420, &flipuvarg, &rawvideo, &noblitarg,
96 &progressarg, &limitarg, &skiparg, &postprocarg, &summaryarg, &outputfile,
97 &threadsarg, &verbosearg, &scalearg, &fb_arg,
98 &md5arg, &error_concealment, &continuearg,
102 #if CONFIG_VP8_DECODER
103 static const arg_def_t addnoise_level = ARG_DEF(NULL, "noise-level", 1,
104 "Enable VP8 postproc add noise");
105 static const arg_def_t deblock = ARG_DEF(NULL, "deblock", 0,
106 "Enable VP8 deblocking");
107 static const arg_def_t demacroblock_level = ARG_DEF(NULL, "demacroblock-level", 1,
108 "Enable VP8 demacroblocking, w/ level");
109 static const arg_def_t pp_debug_info = ARG_DEF(NULL, "pp-debug-info", 1,
110 "Enable VP8 visible debug info");
111 static const arg_def_t pp_disp_ref_frame = ARG_DEF(NULL, "pp-dbg-ref-frame", 1,
112 "Display only selected reference frame per macro block");
113 static const arg_def_t pp_disp_mb_modes = ARG_DEF(NULL, "pp-dbg-mb-modes", 1,
114 "Display only selected macro block modes");
115 static const arg_def_t pp_disp_b_modes = ARG_DEF(NULL, "pp-dbg-b-modes", 1,
116 "Display only selected block modes");
117 static const arg_def_t pp_disp_mvs = ARG_DEF(NULL, "pp-dbg-mvs", 1,
118 "Draw only selected motion vectors");
119 static const arg_def_t mfqe = ARG_DEF(NULL, "mfqe", 0,
120 "Enable multiframe quality enhancement");
122 static const arg_def_t *vp8_pp_args[] = {
123 &addnoise_level, &deblock, &demacroblock_level, &pp_debug_info,
124 &pp_disp_ref_frame, &pp_disp_mb_modes, &pp_disp_b_modes, &pp_disp_mvs, &mfqe,
130 static INLINE int vpx_image_scale(vpx_image_t *src, vpx_image_t *dst,
131 FilterModeEnum mode) {
132 assert(src->fmt == VPX_IMG_FMT_I420);
133 assert(dst->fmt == VPX_IMG_FMT_I420);
134 return I420Scale(src->planes[VPX_PLANE_Y], src->stride[VPX_PLANE_Y],
135 src->planes[VPX_PLANE_U], src->stride[VPX_PLANE_U],
136 src->planes[VPX_PLANE_V], src->stride[VPX_PLANE_V],
138 dst->planes[VPX_PLANE_Y], dst->stride[VPX_PLANE_Y],
139 dst->planes[VPX_PLANE_U], dst->stride[VPX_PLANE_U],
140 dst->planes[VPX_PLANE_V], dst->stride[VPX_PLANE_V],
149 fprintf(stderr, "Usage: %s <options> filename\n\n"
150 "Options:\n", exec_name);
151 arg_show_usage(stderr, all_args);
152 #if CONFIG_VP8_DECODER
153 fprintf(stderr, "\nVP8 Postprocessing Options:\n");
154 arg_show_usage(stderr, vp8_pp_args);
157 "\nOutput File Patterns:\n\n"
158 " The -o argument specifies the name of the file(s) to "
159 "write to. If the\n argument does not include any escape "
160 "characters, the output will be\n written to a single file. "
161 "Otherwise, the filename will be calculated by\n expanding "
162 "the following escape characters:\n");
164 "\n\t%%w - Frame width"
165 "\n\t%%h - Frame height"
166 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
167 "\n\n Pattern arguments are only supported in conjunction "
168 "with the --yv12 and\n --i420 options. If the -o option is "
169 "not specified, the output will be\n directed to stdout.\n"
171 fprintf(stderr, "\nIncluded decoders:\n\n");
173 for (i = 0; i < get_vpx_decoder_count(); ++i) {
174 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
175 fprintf(stderr, " %-6s - %s\n",
176 decoder->name, vpx_codec_iface_name(decoder->codec_interface()));
182 static int raw_read_frame(FILE *infile, uint8_t **buffer,
183 size_t *bytes_read, size_t *buffer_size) {
184 char raw_hdr[RAW_FRAME_HDR_SZ];
185 size_t frame_size = 0;
187 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
189 warn("Failed to read RAW frame size\n");
191 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
192 const size_t kFrameTooSmallThreshold = 256 * 1024;
193 frame_size = mem_get_le32(raw_hdr);
195 if (frame_size > kCorruptFrameThreshold) {
196 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
200 if (frame_size < kFrameTooSmallThreshold) {
201 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
202 (unsigned int)frame_size);
205 if (frame_size > *buffer_size) {
206 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
209 *buffer_size = 2 * frame_size;
211 warn("Failed to allocate compressed data buffer\n");
218 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
219 warn("Failed to read full frame\n");
222 *bytes_read = frame_size;
228 static int read_frame(struct VpxDecInputContext *input, uint8_t **buf,
229 size_t *bytes_in_buffer, size_t *buffer_size) {
230 switch (input->vpx_input_ctx->file_type) {
233 return webm_read_frame(input->webm_ctx,
234 buf, bytes_in_buffer, buffer_size);
237 return raw_read_frame(input->vpx_input_ctx->file,
238 buf, bytes_in_buffer, buffer_size);
240 return ivf_read_frame(input->vpx_input_ctx->file,
241 buf, bytes_in_buffer, buffer_size);
247 static void update_image_md5(const vpx_image_t *img, const int planes[3],
251 for (i = 0; i < 3; ++i) {
252 const int plane = planes[i];
253 const unsigned char *buf = img->planes[plane];
254 const int stride = img->stride[plane];
255 const int w = vpx_img_plane_width(img, plane);
256 const int h = vpx_img_plane_height(img, plane);
258 for (y = 0; y < h; ++y) {
259 MD5Update(md5, buf, w);
265 static void write_image_file(const vpx_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 = vpx_img_plane_width(img, plane);
274 const int h = vpx_img_plane_height(img, plane);
276 for (y = 0; y < h; ++y) {
277 fwrite(buf, 1, w, file);
283 int file_is_raw(struct VpxInputContext *input) {
286 vpx_codec_stream_info_t si;
290 if (fread(buf, 1, 32, input->file) == 32) {
293 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
294 for (i = 0; i < get_vpx_decoder_count(); ++i) {
295 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
296 if (!vpx_codec_peek_stream_info(decoder->codec_interface(),
297 buf + 4, 32 - 4, &si)) {
299 input->fourcc = decoder->fourcc;
301 input->height = si.h;
302 input->framerate.numerator = 30;
303 input->framerate.denominator = 1;
314 void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
316 "%d decoded frames/%d showed frames in %"PRId64" us (%.2f fps)\r",
317 frame_in, frame_out, dx_time,
318 (double)frame_out * 1000000.0 / (double)dx_time);
321 struct ExternalFrameBuffer {
327 struct ExternalFrameBufferList {
328 int num_external_frame_buffers;
329 struct ExternalFrameBuffer *ext_fb;
332 // Callback used by libvpx to request an external frame buffer. |cb_priv|
333 // Application private data passed into the set function. |min_size| is the
334 // minimum size in bytes needed to decode the next frame. |fb| pointer to the
336 int get_vp9_frame_buffer(void *cb_priv, size_t min_size,
337 vpx_codec_frame_buffer_t *fb) {
339 struct ExternalFrameBufferList *const ext_fb_list =
340 (struct ExternalFrameBufferList *)cb_priv;
341 if (ext_fb_list == NULL)
344 // Find a free frame buffer.
345 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
346 if (!ext_fb_list->ext_fb[i].in_use)
350 if (i == ext_fb_list->num_external_frame_buffers)
353 if (ext_fb_list->ext_fb[i].size < min_size) {
354 free(ext_fb_list->ext_fb[i].data);
355 ext_fb_list->ext_fb[i].data = (uint8_t *)malloc(min_size);
356 if (!ext_fb_list->ext_fb[i].data)
359 ext_fb_list->ext_fb[i].size = min_size;
362 fb->data = ext_fb_list->ext_fb[i].data;
363 fb->size = ext_fb_list->ext_fb[i].size;
364 ext_fb_list->ext_fb[i].in_use = 1;
366 // Set the frame buffer's private data to point at the external frame buffer.
367 fb->priv = &ext_fb_list->ext_fb[i];
371 // Callback used by libvpx when there are no references to the frame buffer.
372 // |cb_priv| user private data passed into the set function. |fb| pointer
373 // to the frame buffer.
374 int release_vp9_frame_buffer(void *cb_priv,
375 vpx_codec_frame_buffer_t *fb) {
376 struct ExternalFrameBuffer *const ext_fb =
377 (struct ExternalFrameBuffer *)fb->priv;
383 void generate_filename(const char *pattern, char *out, size_t q_len,
384 unsigned int d_w, unsigned int d_h,
385 unsigned int frame_in) {
386 const char *p = pattern;
390 char *next_pat = strchr(p, '%');
395 /* parse the pattern */
399 snprintf(q, q_len - 1, "%d", d_w);
402 snprintf(q, q_len - 1, "%d", d_h);
405 snprintf(q, q_len - 1, "%d", frame_in);
408 snprintf(q, q_len - 1, "%02d", frame_in);
411 snprintf(q, q_len - 1, "%03d", frame_in);
414 snprintf(q, q_len - 1, "%04d", frame_in);
417 snprintf(q, q_len - 1, "%05d", frame_in);
420 snprintf(q, q_len - 1, "%06d", frame_in);
423 snprintf(q, q_len - 1, "%07d", frame_in);
426 snprintf(q, q_len - 1, "%08d", frame_in);
429 snprintf(q, q_len - 1, "%09d", frame_in);
432 die("Unrecognized pattern %%%c\n", p[1]);
437 if (pat_len >= q_len - 1)
438 die("Output filename too long.\n");
445 /* copy the next segment */
447 copy_len = strlen(p);
449 copy_len = next_pat - p;
451 if (copy_len >= q_len - 1)
452 die("Output filename too long.\n");
454 memcpy(q, p, copy_len);
463 static int is_single_file(const char *outfile_pattern) {
464 const char *p = outfile_pattern;
468 if (p && p[1] >= '1' && p[1] <= '9')
469 return 0; // pattern contains sequence number, so it's not unique
477 static void print_md5(unsigned char digest[16], const char *filename) {
480 for (i = 0; i < 16; ++i)
481 printf("%02x", digest[i]);
482 printf(" %s\n", filename);
485 static FILE *open_outfile(const char *name) {
486 if (strcmp("-", name) == 0) {
487 set_binary_mode(stdout);
490 FILE *file = fopen(name, "wb");
492 fatal("Failed to output file %s", name);
497 int main_loop(int argc, const char **argv_) {
498 vpx_codec_ctx_t decoder;
502 size_t bytes_in_buffer = 0, buffer_size = 0;
504 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
505 int do_md5 = 0, progress = 0;
506 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
510 const VpxInterface *interface = NULL;
511 const VpxInterface *fourcc_interface = NULL;
512 uint64_t dx_time = 0;
514 char **argv, **argi, **argj;
520 vpx_codec_dec_cfg_t cfg = {0, 0, 0};
521 #if CONFIG_VP8_DECODER
522 vp8_postproc_cfg_t vp8_pp_cfg = {0};
523 int vp8_dbg_color_ref_frame = 0;
524 int vp8_dbg_color_mb_modes = 0;
525 int vp8_dbg_color_b_modes = 0;
526 int vp8_dbg_display_mv = 0;
528 int frames_corrupted = 0;
531 vpx_image_t *scaled_img = NULL;
532 int frame_avail, got_data;
533 int num_external_frame_buffers = 0;
534 struct ExternalFrameBufferList ext_fb_list = {0, NULL};
536 const char *outfile_pattern = NULL;
537 char outfile_name[PATH_MAX] = {0};
538 FILE *outfile = NULL;
541 unsigned char md5_digest[16];
543 struct VpxDecInputContext input = {NULL, NULL};
544 struct VpxInputContext vpx_input_ctx;
546 struct WebmInputContext webm_ctx;
547 memset(&(webm_ctx), 0, sizeof(webm_ctx));
548 input.webm_ctx = &webm_ctx;
550 input.vpx_input_ctx = &vpx_input_ctx;
552 /* Parse command line */
553 exec_name = argv_[0];
554 argv = argv_dup(argc - 1, argv_ + 1);
556 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
557 memset(&arg, 0, sizeof(arg));
560 if (arg_match(&arg, &codecarg, argi)) {
561 interface = get_vpx_decoder_by_name(arg.val);
563 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
564 } else if (arg_match(&arg, &looparg, argi)) {
566 } else if (arg_match(&arg, &outputfile, argi))
567 outfile_pattern = arg.val;
568 else if (arg_match(&arg, &use_yv12, argi)) {
572 } else if (arg_match(&arg, &use_i420, argi)) {
576 } else if (arg_match(&arg, &rawvideo, argi)) {
578 } else if (arg_match(&arg, &flipuvarg, argi))
580 else if (arg_match(&arg, &noblitarg, argi))
582 else if (arg_match(&arg, &progressarg, argi))
584 else if (arg_match(&arg, &limitarg, argi))
585 stop_after = arg_parse_uint(&arg);
586 else if (arg_match(&arg, &skiparg, argi))
587 arg_skip = arg_parse_uint(&arg);
588 else if (arg_match(&arg, &postprocarg, argi))
590 else if (arg_match(&arg, &md5arg, argi))
592 else if (arg_match(&arg, &summaryarg, argi))
594 else if (arg_match(&arg, &threadsarg, argi))
595 cfg.threads = arg_parse_uint(&arg);
596 else if (arg_match(&arg, &verbosearg, argi))
598 else if (arg_match(&arg, &scalearg, argi))
600 else if (arg_match(&arg, &fb_arg, argi))
601 num_external_frame_buffers = arg_parse_uint(&arg);
603 #if CONFIG_VP8_DECODER
604 else if (arg_match(&arg, &addnoise_level, argi)) {
606 vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE;
607 vp8_pp_cfg.noise_level = arg_parse_uint(&arg);
608 } else if (arg_match(&arg, &demacroblock_level, argi)) {
610 vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK;
611 vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg);
612 } else if (arg_match(&arg, &deblock, argi)) {
614 vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK;
615 } else if (arg_match(&arg, &mfqe, argi)) {
617 vp8_pp_cfg.post_proc_flag |= VP8_MFQE;
618 } else if (arg_match(&arg, &pp_debug_info, argi)) {
619 unsigned int level = arg_parse_uint(&arg);
622 vp8_pp_cfg.post_proc_flag &= ~0x7;
625 vp8_pp_cfg.post_proc_flag |= level;
626 } else if (arg_match(&arg, &pp_disp_ref_frame, argi)) {
627 unsigned int flags = arg_parse_int(&arg);
630 vp8_dbg_color_ref_frame = flags;
632 } else if (arg_match(&arg, &pp_disp_mb_modes, argi)) {
633 unsigned int flags = arg_parse_int(&arg);
636 vp8_dbg_color_mb_modes = flags;
638 } else if (arg_match(&arg, &pp_disp_b_modes, argi)) {
639 unsigned int flags = arg_parse_int(&arg);
642 vp8_dbg_color_b_modes = flags;
644 } else if (arg_match(&arg, &pp_disp_mvs, argi)) {
645 unsigned int flags = arg_parse_int(&arg);
648 vp8_dbg_display_mv = flags;
650 } else if (arg_match(&arg, &error_concealment, argi)) {
652 } else if (arg_match(&arg, &continuearg, argi)) {
661 /* Check for unrecognized options */
662 for (argi = argv; *argi; argi++)
663 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
664 die("Error: Unrecognized option %s\n", *argi);
666 /* Handle non-option arguments */
673 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
676 fprintf(stderr, "Failed to open file '%s'", strcmp(fn, "-") ? fn : "stdin");
679 #if CONFIG_OS_SUPPORT
680 /* Make sure we don't dump to the terminal, unless forced to with -o - */
681 if (!outfile_pattern && isatty(fileno(stdout)) && !do_md5 && !noblit) {
683 "Not dumping raw video to your terminal. Use '-o -' to "
688 input.vpx_input_ctx->file = infile;
689 if (file_is_ivf(input.vpx_input_ctx))
690 input.vpx_input_ctx->file_type = FILE_TYPE_IVF;
692 else if (file_is_webm(input.webm_ctx, input.vpx_input_ctx))
693 input.vpx_input_ctx->file_type = FILE_TYPE_WEBM;
695 else if (file_is_raw(input.vpx_input_ctx))
696 input.vpx_input_ctx->file_type = FILE_TYPE_RAW;
698 fprintf(stderr, "Unrecognized input file type.\n");
700 fprintf(stderr, "vpxdec was built without WebM container support.\n");
705 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
706 single_file = is_single_file(outfile_pattern);
708 if (!noblit && single_file) {
709 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
710 vpx_input_ctx.width, vpx_input_ctx.height, 0);
714 outfile = open_outfile(outfile_name);
717 if (use_y4m && !noblit) {
719 fprintf(stderr, "YUV4MPEG2 not supported with output patterns,"
720 " try --i420 or --yv12.\n");
725 if (vpx_input_ctx.file_type == FILE_TYPE_WEBM) {
726 if (webm_guess_framerate(input.webm_ctx, input.vpx_input_ctx)) {
727 fprintf(stderr, "Failed to guess framerate -- error parsing "
735 fourcc_interface = get_vpx_decoder_by_fourcc(vpx_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;
742 interface = get_vpx_decoder_by_index(0);
744 dec_flags = (postproc ? VPX_CODEC_USE_POSTPROC : 0) |
745 (ec_enabled ? VPX_CODEC_USE_ERROR_CONCEALMENT : 0);
746 if (vpx_codec_dec_init(&decoder, interface->codec_interface(),
748 fprintf(stderr, "Failed to initialize decoder: %s\n",
749 vpx_codec_error(&decoder));
754 fprintf(stderr, "%s\n", decoder.name);
756 #if CONFIG_VP8_DECODER
758 if (vp8_pp_cfg.post_proc_flag
759 && vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg)) {
760 fprintf(stderr, "Failed to configure postproc: %s\n", vpx_codec_error(&decoder));
764 if (vp8_dbg_color_ref_frame
765 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_REF_FRAME, vp8_dbg_color_ref_frame)) {
766 fprintf(stderr, "Failed to configure reference block visualizer: %s\n", vpx_codec_error(&decoder));
770 if (vp8_dbg_color_mb_modes
771 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_MB_MODES, vp8_dbg_color_mb_modes)) {
772 fprintf(stderr, "Failed to configure macro block visualizer: %s\n", vpx_codec_error(&decoder));
776 if (vp8_dbg_color_b_modes
777 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_B_MODES, vp8_dbg_color_b_modes)) {
778 fprintf(stderr, "Failed to configure block visualizer: %s\n", vpx_codec_error(&decoder));
782 if (vp8_dbg_display_mv
783 && vpx_codec_control(&decoder, VP8_SET_DBG_DISPLAY_MV, vp8_dbg_display_mv)) {
784 fprintf(stderr, "Failed to configure motion vector visualizer: %s\n", vpx_codec_error(&decoder));
791 fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
793 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size))
798 if (num_external_frame_buffers > 0) {
799 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
800 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
801 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
802 if (vpx_codec_set_frame_buffer_functions(
803 &decoder, get_vp9_frame_buffer, release_vp9_frame_buffer,
805 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
806 vpx_codec_error(&decoder));
815 while (frame_avail || got_data) {
816 vpx_codec_iter_t iter = NULL;
818 struct vpx_usec_timer timer;
822 if (!stop_after || frame_in < stop_after) {
823 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
827 vpx_usec_timer_start(&timer);
829 if (vpx_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer,
831 const char *detail = vpx_codec_error_detail(&decoder);
832 warn("Failed to decode frame %d: %s",
833 frame_in, vpx_codec_error(&decoder));
836 warn("Additional information: %s", detail);
841 vpx_usec_timer_mark(&timer);
842 dx_time += vpx_usec_timer_elapsed(&timer);
846 vpx_usec_timer_start(&timer);
849 if ((img = vpx_codec_get_frame(&decoder, &iter))) {
854 vpx_usec_timer_mark(&timer);
855 dx_time += (unsigned int)vpx_usec_timer_elapsed(&timer);
857 if (vpx_codec_control(&decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted)) {
858 warn("Failed VP8_GET_FRAME_CORRUPTED: %s", vpx_codec_error(&decoder));
861 frames_corrupted += corrupted;
864 show_progress(frame_in, frame_out, dx_time);
866 if (!noblit && img) {
867 const int PLANES_YUV[] = {VPX_PLANE_Y, VPX_PLANE_U, VPX_PLANE_V};
868 const int PLANES_YVU[] = {VPX_PLANE_Y, VPX_PLANE_V, VPX_PLANE_U};
869 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
872 if (frame_out == 1) {
873 // If the output frames are to be scaled to a fixed display size then
874 // use the width and height specified in the container. If either of
875 // these is set to 0, use the display size set in the first frame
876 // header. If that is unavailable, use the raw decoded size of the
877 // first decoded frame.
878 int display_width = vpx_input_ctx.width;
879 int display_height = vpx_input_ctx.height;
880 if (!display_width || !display_height) {
882 if (vpx_codec_control(&decoder, VP9D_GET_DISPLAY_SIZE,
884 // As last resort use size of first frame as display size.
885 display_width = img->d_w;
886 display_height = img->d_h;
888 display_width = display_size[0];
889 display_height = display_size[1];
892 scaled_img = vpx_img_alloc(NULL, VPX_IMG_FMT_I420, display_width,
894 scaled_img->bit_depth = img->bit_depth;
897 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
899 vpx_image_scale(img, scaled_img, kFilterBox);
902 fprintf(stderr, "Failed to scale output frame: %s.\n"
903 "Scaling is disabled in this configuration. "
904 "To enable scaling, configure with --enable-libyuv\n",
905 vpx_codec_error(&decoder));
913 char buf[Y4M_BUFFER_SIZE] = {0};
915 if (frame_out == 1) {
917 len = y4m_write_file_header(buf, sizeof(buf),
919 vpx_input_ctx.height,
920 &vpx_input_ctx.framerate,
921 img->fmt, img->bit_depth);
923 MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
930 len = y4m_write_frame_header(buf, sizeof(buf));
932 MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
937 if (frame_out == 1) {
938 // Check if --yv12 or --i420 options are consistent with the
939 // bit-stream decoded
941 if (img->fmt != VPX_IMG_FMT_I420 &&
942 img->fmt != VPX_IMG_FMT_I42016) {
943 fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
948 if ((img->fmt != VPX_IMG_FMT_I420 &&
949 img->fmt != VPX_IMG_FMT_YV12) || img->bit_depth != 8) {
950 fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
958 update_image_md5(img, planes, &md5_ctx);
960 write_image_file(img, planes, outfile);
963 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
964 img->d_w, img->d_h, frame_in);
967 update_image_md5(img, planes, &md5_ctx);
968 MD5Final(md5_digest, &md5_ctx);
969 print_md5(md5_digest, outfile_name);
971 outfile = open_outfile(outfile_name);
972 write_image_file(img, planes, outfile);
978 if (stop_after && frame_in >= stop_after)
982 if (summary || progress) {
983 show_progress(frame_in, frame_out, dx_time);
984 fprintf(stderr, "\n");
987 if (frames_corrupted)
988 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
992 if (vpx_codec_destroy(&decoder)) {
993 fprintf(stderr, "Failed to destroy decoder: %s\n",
994 vpx_codec_error(&decoder));
998 if (!noblit && single_file) {
1000 MD5Final(md5_digest, &md5_ctx);
1001 print_md5(md5_digest, outfile_name);
1008 if (input.vpx_input_ctx->file_type == FILE_TYPE_WEBM)
1009 webm_free(input.webm_ctx);
1012 if (input.vpx_input_ctx->file_type != FILE_TYPE_WEBM)
1015 if (scaled_img) vpx_img_free(scaled_img);
1017 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
1018 free(ext_fb_list.ext_fb[i].data);
1020 free(ext_fb_list.ext_fb);
1025 return frames_corrupted ? EXIT_FAILURE : EXIT_SUCCESS;
1028 int main(int argc, const char **argv_) {
1029 unsigned int loops = 1, i;
1030 char **argv, **argi, **argj;
1034 argv = argv_dup(argc - 1, argv_ + 1);
1035 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1036 memset(&arg, 0, sizeof(arg));
1039 if (arg_match(&arg, &looparg, argi)) {
1040 loops = arg_parse_uint(&arg);
1045 for (i = 0; !error && i < loops; i++)
1046 error = main_loop(argc, argv_);