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 "third_party/libyuv/include/libyuv/scale.h"
23 #define VPX_CODEC_DISABLE_COMPAT 1
24 #include "./vpx_config.h"
25 #include "vpx/vpx_decoder.h"
26 #include "vpx_ports/vpx_timer.h"
28 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
29 #include "vpx/vp8dx.h"
32 #include "./md5_utils.h"
34 #include "./tools_common.h"
35 #include "./webmdec.h"
38 static const char *exec_name;
40 struct VpxDecInputContext {
41 struct VpxInputContext *vpx_input_ctx;
42 struct WebmInputContext *webm_ctx;
45 static const arg_def_t looparg = ARG_DEF(NULL, "loops", 1,
46 "Number of times to decode the file");
47 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1,
49 static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0,
50 "Output raw YV12 frames");
51 static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0,
52 "Output raw I420 frames");
53 static const arg_def_t flipuvarg = ARG_DEF(NULL, "flipuv", 0,
54 "Flip the chroma planes in the output");
55 static const arg_def_t noblitarg = ARG_DEF(NULL, "noblit", 0,
56 "Don't process the decoded frames");
57 static const arg_def_t progressarg = ARG_DEF(NULL, "progress", 0,
58 "Show progress after each frame decodes");
59 static const arg_def_t limitarg = ARG_DEF(NULL, "limit", 1,
60 "Stop decoding after n frames");
61 static const arg_def_t skiparg = ARG_DEF(NULL, "skip", 1,
62 "Skip the first n input frames");
63 static const arg_def_t postprocarg = ARG_DEF(NULL, "postproc", 0,
64 "Postprocess decoded frames");
65 static const arg_def_t summaryarg = ARG_DEF(NULL, "summary", 0,
66 "Show timing summary");
67 static const arg_def_t outputfile = ARG_DEF("o", "output", 1,
68 "Output file name pattern (see below)");
69 static const arg_def_t threadsarg = ARG_DEF("t", "threads", 1,
70 "Max threads to use");
71 static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0,
72 "Show version string");
73 static const arg_def_t error_concealment = ARG_DEF(NULL, "error-concealment", 0,
74 "Enable decoder error-concealment");
75 static const arg_def_t scalearg = ARG_DEF("S", "scale", 0,
76 "Scale output frames uniformly");
78 static const arg_def_t fb_arg =
79 ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
81 static const arg_def_t md5arg = ARG_DEF(NULL, "md5", 0,
82 "Compute the MD5 sum of the decoded frame");
84 static const arg_def_t *all_args[] = {
85 &codecarg, &use_yv12, &use_i420, &flipuvarg, &noblitarg,
86 &progressarg, &limitarg, &skiparg, &postprocarg, &summaryarg, &outputfile,
87 &threadsarg, &verbosearg, &scalearg, &fb_arg,
93 #if CONFIG_VP8_DECODER
94 static const arg_def_t addnoise_level = ARG_DEF(NULL, "noise-level", 1,
95 "Enable VP8 postproc add noise");
96 static const arg_def_t deblock = ARG_DEF(NULL, "deblock", 0,
97 "Enable VP8 deblocking");
98 static const arg_def_t demacroblock_level = ARG_DEF(NULL, "demacroblock-level", 1,
99 "Enable VP8 demacroblocking, w/ level");
100 static const arg_def_t pp_debug_info = ARG_DEF(NULL, "pp-debug-info", 1,
101 "Enable VP8 visible debug info");
102 static const arg_def_t pp_disp_ref_frame = ARG_DEF(NULL, "pp-dbg-ref-frame", 1,
103 "Display only selected reference frame per macro block");
104 static const arg_def_t pp_disp_mb_modes = ARG_DEF(NULL, "pp-dbg-mb-modes", 1,
105 "Display only selected macro block modes");
106 static const arg_def_t pp_disp_b_modes = ARG_DEF(NULL, "pp-dbg-b-modes", 1,
107 "Display only selected block modes");
108 static const arg_def_t pp_disp_mvs = ARG_DEF(NULL, "pp-dbg-mvs", 1,
109 "Draw only selected motion vectors");
110 static const arg_def_t mfqe = ARG_DEF(NULL, "mfqe", 0,
111 "Enable multiframe quality enhancement");
113 static const arg_def_t *vp8_pp_args[] = {
114 &addnoise_level, &deblock, &demacroblock_level, &pp_debug_info,
115 &pp_disp_ref_frame, &pp_disp_mb_modes, &pp_disp_b_modes, &pp_disp_mvs, &mfqe,
120 static int vpx_image_scale(vpx_image_t *src, vpx_image_t *dst,
122 assert(src->fmt == VPX_IMG_FMT_I420);
123 assert(dst->fmt == VPX_IMG_FMT_I420);
124 return I420Scale(src->planes[VPX_PLANE_Y], src->stride[VPX_PLANE_Y],
125 src->planes[VPX_PLANE_U], src->stride[VPX_PLANE_U],
126 src->planes[VPX_PLANE_V], src->stride[VPX_PLANE_V],
128 dst->planes[VPX_PLANE_Y], dst->stride[VPX_PLANE_Y],
129 dst->planes[VPX_PLANE_U], dst->stride[VPX_PLANE_U],
130 dst->planes[VPX_PLANE_V], dst->stride[VPX_PLANE_V],
138 fprintf(stderr, "Usage: %s <options> filename\n\n"
139 "Options:\n", exec_name);
140 arg_show_usage(stderr, all_args);
141 #if CONFIG_VP8_DECODER
142 fprintf(stderr, "\nVP8 Postprocessing Options:\n");
143 arg_show_usage(stderr, vp8_pp_args);
146 "\nOutput File Patterns:\n\n"
147 " The -o argument specifies the name of the file(s) to "
148 "write to. If the\n argument does not include any escape "
149 "characters, the output will be\n written to a single file. "
150 "Otherwise, the filename will be calculated by\n expanding "
151 "the following escape characters:\n");
153 "\n\t%%w - Frame width"
154 "\n\t%%h - Frame height"
155 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
156 "\n\n Pattern arguments are only supported in conjunction "
157 "with the --yv12 and\n --i420 options. If the -o option is "
158 "not specified, the output will be\n directed to stdout.\n"
160 fprintf(stderr, "\nIncluded decoders:\n\n");
162 for (i = 0; i < get_vpx_decoder_count(); ++i) {
163 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
164 fprintf(stderr, " %-6s - %s\n",
165 decoder->name, vpx_codec_iface_name(decoder->interface()));
171 static int raw_read_frame(FILE *infile, uint8_t **buffer,
172 size_t *bytes_read, size_t *buffer_size) {
173 char raw_hdr[RAW_FRAME_HDR_SZ];
174 size_t frame_size = 0;
176 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
178 warn("Failed to read RAW frame size\n");
180 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
181 const size_t kFrameTooSmallThreshold = 256 * 1024;
182 frame_size = mem_get_le32(raw_hdr);
184 if (frame_size > kCorruptFrameThreshold) {
185 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
189 if (frame_size < kFrameTooSmallThreshold) {
190 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
191 (unsigned int)frame_size);
194 if (frame_size > *buffer_size) {
195 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
198 *buffer_size = 2 * frame_size;
200 warn("Failed to allocate compressed data buffer\n");
207 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
208 warn("Failed to read full frame\n");
211 *bytes_read = frame_size;
217 static int read_frame(struct VpxDecInputContext *input, uint8_t **buf,
218 size_t *bytes_in_buffer, size_t *buffer_size) {
219 switch (input->vpx_input_ctx->file_type) {
221 return webm_read_frame(input->webm_ctx,
222 buf, bytes_in_buffer, buffer_size);
224 return raw_read_frame(input->vpx_input_ctx->file,
225 buf, bytes_in_buffer, buffer_size);
227 return ivf_read_frame(input->vpx_input_ctx->file,
228 buf, bytes_in_buffer, buffer_size);
234 static void update_image_md5(const vpx_image_t *img, const int planes[3],
238 for (i = 0; i < 3; ++i) {
239 const int plane = planes[i];
240 const unsigned char *buf = img->planes[plane];
241 const int stride = img->stride[plane];
242 const int w = vpx_img_plane_width(img, plane);
243 const int h = vpx_img_plane_height(img, plane);
245 for (y = 0; y < h; ++y) {
246 MD5Update(md5, buf, w);
252 static void write_image_file(const vpx_image_t *img, const int planes[3],
256 for (i = 0; i < 3; ++i) {
257 const int plane = planes[i];
258 const unsigned char *buf = img->planes[plane];
259 const int stride = img->stride[plane];
260 const int w = vpx_img_plane_width(img, plane);
261 const int h = vpx_img_plane_height(img, plane);
263 for (y = 0; y < h; ++y) {
264 fwrite(buf, 1, w, file);
270 int file_is_raw(struct VpxInputContext *input) {
273 vpx_codec_stream_info_t si;
277 if (fread(buf, 1, 32, input->file) == 32) {
280 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
281 for (i = 0; i < get_vpx_decoder_count(); ++i) {
282 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
283 if (!vpx_codec_peek_stream_info(decoder->interface(),
284 buf + 4, 32 - 4, &si)) {
286 input->fourcc = decoder->fourcc;
288 input->height = si.h;
289 input->framerate.numerator = 30;
290 input->framerate.denominator = 1;
301 void show_progress(int frame_in, int frame_out, unsigned long dx_time) {
302 fprintf(stderr, "%d decoded frames/%d showed frames in %lu us (%.2f fps)\r",
303 frame_in, frame_out, dx_time,
304 (float)frame_out * 1000000.0 / (float)dx_time);
307 struct ExternalFrameBuffer {
313 struct ExternalFrameBufferList {
314 int num_external_frame_buffers;
315 struct ExternalFrameBuffer *ext_fb;
318 // Callback used by libvpx to request an external frame buffer. |cb_priv|
319 // Application private data passed into the set function. |min_size| is the
320 // minimum size in bytes needed to decode the next frame. |fb| pointer to the
322 int get_vp9_frame_buffer(void *cb_priv, size_t min_size,
323 vpx_codec_frame_buffer_t *fb) {
325 struct ExternalFrameBufferList *const ext_fb_list =
326 (struct ExternalFrameBufferList *)cb_priv;
327 if (ext_fb_list == NULL)
330 // Find a free frame buffer.
331 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
332 if (!ext_fb_list->ext_fb[i].in_use)
336 if (i == ext_fb_list->num_external_frame_buffers)
339 if (ext_fb_list->ext_fb[i].size < min_size) {
340 free(ext_fb_list->ext_fb[i].data);
341 ext_fb_list->ext_fb[i].data = (uint8_t *)malloc(min_size);
342 if (!ext_fb_list->ext_fb[i].data)
345 ext_fb_list->ext_fb[i].size = min_size;
348 fb->data = ext_fb_list->ext_fb[i].data;
349 fb->size = ext_fb_list->ext_fb[i].size;
350 ext_fb_list->ext_fb[i].in_use = 1;
352 // Set the frame buffer's private data to point at the external frame buffer.
353 fb->priv = &ext_fb_list->ext_fb[i];
357 // Callback used by libvpx when there are no references to the frame buffer.
358 // |cb_priv| user private data passed into the set function. |fb| pointer
359 // to the frame buffer.
360 int release_vp9_frame_buffer(void *cb_priv,
361 vpx_codec_frame_buffer_t *fb) {
362 struct ExternalFrameBuffer *const ext_fb =
363 (struct ExternalFrameBuffer *)fb->priv;
369 void generate_filename(const char *pattern, char *out, size_t q_len,
370 unsigned int d_w, unsigned int d_h,
371 unsigned int frame_in) {
372 const char *p = pattern;
376 char *next_pat = strchr(p, '%');
381 /* parse the pattern */
385 snprintf(q, q_len - 1, "%d", d_w);
388 snprintf(q, q_len - 1, "%d", d_h);
391 snprintf(q, q_len - 1, "%d", frame_in);
394 snprintf(q, q_len - 1, "%02d", frame_in);
397 snprintf(q, q_len - 1, "%03d", frame_in);
400 snprintf(q, q_len - 1, "%04d", frame_in);
403 snprintf(q, q_len - 1, "%05d", frame_in);
406 snprintf(q, q_len - 1, "%06d", frame_in);
409 snprintf(q, q_len - 1, "%07d", frame_in);
412 snprintf(q, q_len - 1, "%08d", frame_in);
415 snprintf(q, q_len - 1, "%09d", frame_in);
418 die("Unrecognized pattern %%%c\n", p[1]);
422 if (pat_len >= q_len - 1)
423 die("Output filename too long.\n");
430 /* copy the next segment */
432 copy_len = strlen(p);
434 copy_len = next_pat - p;
436 if (copy_len >= q_len - 1)
437 die("Output filename too long.\n");
439 memcpy(q, p, copy_len);
448 static int is_single_file(const char *outfile_pattern) {
449 const char *p = outfile_pattern;
453 if (p && p[1] >= '1' && p[1] <= '9')
454 return 0; // pattern contains sequence number, so it's not unique
462 static void print_md5(unsigned char digest[16], const char *filename) {
465 for (i = 0; i < 16; ++i)
466 printf("%02x", digest[i]);
467 printf(" %s\n", filename);
470 static FILE *open_outfile(const char *name) {
471 if (strcmp("-", name) == 0) {
472 set_binary_mode(stdout);
475 FILE *file = fopen(name, "wb");
477 fatal("Failed to output file %s", name);
482 int main_loop(int argc, const char **argv_) {
483 vpx_codec_ctx_t decoder;
487 size_t bytes_in_buffer = 0, buffer_size = 0;
489 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
490 int do_md5 = 0, progress = 0;
491 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
494 const VpxInterface *interface = NULL;
495 const VpxInterface *fourcc_interface = NULL;
496 unsigned long dx_time = 0;
498 char **argv, **argi, **argj;
502 vpx_codec_dec_cfg_t cfg = {0};
503 #if CONFIG_VP8_DECODER
504 vp8_postproc_cfg_t vp8_pp_cfg = {0};
505 int vp8_dbg_color_ref_frame = 0;
506 int vp8_dbg_color_mb_modes = 0;
507 int vp8_dbg_color_b_modes = 0;
508 int vp8_dbg_display_mv = 0;
510 int frames_corrupted = 0;
513 vpx_image_t *scaled_img = NULL;
514 int frame_avail, got_data;
515 int num_external_frame_buffers = 0;
516 struct ExternalFrameBufferList ext_fb_list = {0};
518 const char *outfile_pattern = NULL;
519 char outfile_name[PATH_MAX] = {0};
520 FILE *outfile = NULL;
523 unsigned char md5_digest[16];
525 struct VpxDecInputContext input = {0};
526 struct VpxInputContext vpx_input_ctx = {0};
527 struct WebmInputContext webm_ctx = {0};
528 input.vpx_input_ctx = &vpx_input_ctx;
529 input.webm_ctx = &webm_ctx;
531 /* Parse command line */
532 exec_name = argv_[0];
533 argv = argv_dup(argc - 1, argv_ + 1);
535 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
536 memset(&arg, 0, sizeof(arg));
539 if (arg_match(&arg, &codecarg, argi)) {
540 interface = get_vpx_decoder_by_name(arg.val);
542 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
543 } else if (arg_match(&arg, &looparg, argi)) {
545 } else if (arg_match(&arg, &outputfile, argi))
546 outfile_pattern = arg.val;
547 else if (arg_match(&arg, &use_yv12, argi)) {
550 } else if (arg_match(&arg, &use_i420, argi)) {
553 } else if (arg_match(&arg, &flipuvarg, argi))
555 else if (arg_match(&arg, &noblitarg, argi))
557 else if (arg_match(&arg, &progressarg, argi))
559 else if (arg_match(&arg, &limitarg, argi))
560 stop_after = arg_parse_uint(&arg);
561 else if (arg_match(&arg, &skiparg, argi))
562 arg_skip = arg_parse_uint(&arg);
563 else if (arg_match(&arg, &postprocarg, argi))
565 else if (arg_match(&arg, &md5arg, argi))
567 else if (arg_match(&arg, &summaryarg, argi))
569 else if (arg_match(&arg, &threadsarg, argi))
570 cfg.threads = arg_parse_uint(&arg);
571 else if (arg_match(&arg, &verbosearg, argi))
573 else if (arg_match(&arg, &scalearg, argi))
575 else if (arg_match(&arg, &fb_arg, argi))
576 num_external_frame_buffers = arg_parse_uint(&arg);
578 #if CONFIG_VP8_DECODER
579 else if (arg_match(&arg, &addnoise_level, argi)) {
581 vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE;
582 vp8_pp_cfg.noise_level = arg_parse_uint(&arg);
583 } else if (arg_match(&arg, &demacroblock_level, argi)) {
585 vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK;
586 vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg);
587 } else if (arg_match(&arg, &deblock, argi)) {
589 vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK;
590 } else if (arg_match(&arg, &mfqe, argi)) {
592 vp8_pp_cfg.post_proc_flag |= VP8_MFQE;
593 } else if (arg_match(&arg, &pp_debug_info, argi)) {
594 unsigned int level = arg_parse_uint(&arg);
597 vp8_pp_cfg.post_proc_flag &= ~0x7;
600 vp8_pp_cfg.post_proc_flag |= level;
601 } else if (arg_match(&arg, &pp_disp_ref_frame, argi)) {
602 unsigned int flags = arg_parse_int(&arg);
605 vp8_dbg_color_ref_frame = flags;
607 } else if (arg_match(&arg, &pp_disp_mb_modes, argi)) {
608 unsigned int flags = arg_parse_int(&arg);
611 vp8_dbg_color_mb_modes = flags;
613 } else if (arg_match(&arg, &pp_disp_b_modes, argi)) {
614 unsigned int flags = arg_parse_int(&arg);
617 vp8_dbg_color_b_modes = flags;
619 } else if (arg_match(&arg, &pp_disp_mvs, argi)) {
620 unsigned int flags = arg_parse_int(&arg);
623 vp8_dbg_display_mv = flags;
625 } else if (arg_match(&arg, &error_concealment, argi)) {
634 /* Check for unrecognized options */
635 for (argi = argv; *argi; argi++)
636 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
637 die("Error: Unrecognized option %s\n", *argi);
639 /* Handle non-option arguments */
646 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
649 fprintf(stderr, "Failed to open file '%s'", strcmp(fn, "-") ? fn : "stdin");
652 #if CONFIG_OS_SUPPORT
653 /* Make sure we don't dump to the terminal, unless forced to with -o - */
654 if (!outfile_pattern && isatty(fileno(stdout)) && !do_md5 && !noblit) {
656 "Not dumping raw video to your terminal. Use '-o -' to "
661 input.vpx_input_ctx->file = infile;
662 if (file_is_ivf(input.vpx_input_ctx))
663 input.vpx_input_ctx->file_type = FILE_TYPE_IVF;
664 else if (file_is_webm(input.webm_ctx, input.vpx_input_ctx))
665 input.vpx_input_ctx->file_type = FILE_TYPE_WEBM;
666 else if (file_is_raw(input.vpx_input_ctx))
667 input.vpx_input_ctx->file_type = FILE_TYPE_RAW;
669 fprintf(stderr, "Unrecognized input file type.\n");
673 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
674 single_file = is_single_file(outfile_pattern);
676 if (!noblit && single_file) {
677 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
678 vpx_input_ctx.width, vpx_input_ctx.height, 0);
682 outfile = open_outfile(outfile_name);
685 if (use_y4m && !noblit) {
687 fprintf(stderr, "YUV4MPEG2 not supported with output patterns,"
688 " try --i420 or --yv12.\n");
692 if (vpx_input_ctx.file_type == FILE_TYPE_WEBM) {
693 if (webm_guess_framerate(input.webm_ctx, input.vpx_input_ctx)) {
694 fprintf(stderr, "Failed to guess framerate -- error parsing "
701 fourcc_interface = get_vpx_decoder_by_fourcc(vpx_input_ctx.fourcc);
702 if (interface && fourcc_interface && interface != fourcc_interface)
703 warn("Header indicates codec: %s\n", fourcc_interface->name);
705 interface = fourcc_interface;
708 interface = get_vpx_decoder_by_index(0);
710 dec_flags = (postproc ? VPX_CODEC_USE_POSTPROC : 0) |
711 (ec_enabled ? VPX_CODEC_USE_ERROR_CONCEALMENT : 0);
712 if (vpx_codec_dec_init(&decoder, interface->interface(), &cfg, dec_flags)) {
713 fprintf(stderr, "Failed to initialize decoder: %s\n",
714 vpx_codec_error(&decoder));
719 fprintf(stderr, "%s\n", decoder.name);
721 #if CONFIG_VP8_DECODER
723 if (vp8_pp_cfg.post_proc_flag
724 && vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg)) {
725 fprintf(stderr, "Failed to configure postproc: %s\n", vpx_codec_error(&decoder));
729 if (vp8_dbg_color_ref_frame
730 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_REF_FRAME, vp8_dbg_color_ref_frame)) {
731 fprintf(stderr, "Failed to configure reference block visualizer: %s\n", vpx_codec_error(&decoder));
735 if (vp8_dbg_color_mb_modes
736 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_MB_MODES, vp8_dbg_color_mb_modes)) {
737 fprintf(stderr, "Failed to configure macro block visualizer: %s\n", vpx_codec_error(&decoder));
741 if (vp8_dbg_color_b_modes
742 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_B_MODES, vp8_dbg_color_b_modes)) {
743 fprintf(stderr, "Failed to configure block visualizer: %s\n", vpx_codec_error(&decoder));
747 if (vp8_dbg_display_mv
748 && vpx_codec_control(&decoder, VP8_SET_DBG_DISPLAY_MV, vp8_dbg_display_mv)) {
749 fprintf(stderr, "Failed to configure motion vector visualizer: %s\n", vpx_codec_error(&decoder));
756 fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
758 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size))
763 if (num_external_frame_buffers > 0) {
764 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
765 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
766 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
767 if (vpx_codec_set_frame_buffer_functions(
768 &decoder, get_vp9_frame_buffer, release_vp9_frame_buffer,
770 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
771 vpx_codec_error(&decoder));
780 while (frame_avail || got_data) {
781 vpx_codec_iter_t iter = NULL;
783 struct vpx_usec_timer timer;
787 if (!stop_after || frame_in < stop_after) {
788 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
792 vpx_usec_timer_start(&timer);
794 if (vpx_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer,
796 const char *detail = vpx_codec_error_detail(&decoder);
797 warn("Failed to decode frame %d: %s",
798 frame_in, vpx_codec_error(&decoder));
801 warn("Additional information: %s", detail);
805 vpx_usec_timer_mark(&timer);
806 dx_time += (unsigned int)vpx_usec_timer_elapsed(&timer);
810 vpx_usec_timer_start(&timer);
813 if ((img = vpx_codec_get_frame(&decoder, &iter))) {
818 vpx_usec_timer_mark(&timer);
819 dx_time += (unsigned int)vpx_usec_timer_elapsed(&timer);
821 if (vpx_codec_control(&decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted)) {
822 warn("Failed VP8_GET_FRAME_CORRUPTED: %s", vpx_codec_error(&decoder));
825 frames_corrupted += corrupted;
828 show_progress(frame_in, frame_out, dx_time);
830 if (!noblit && img) {
831 const int PLANES_YUV[] = {VPX_PLANE_Y, VPX_PLANE_U, VPX_PLANE_V};
832 const int PLANES_YVU[] = {VPX_PLANE_Y, VPX_PLANE_V, VPX_PLANE_U};
833 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
836 if (frame_out == 1) {
837 // If the output frames are to be scaled to a fixed display size then
838 // use the width and height specified in the container. If either of
839 // these is set to 0, use the display size set in the first frame
840 // header. If that is unavailable, use the raw decoded size of the
841 // first decoded frame.
842 int display_width = vpx_input_ctx.width;
843 int display_height = vpx_input_ctx.height;
844 if (!display_width || !display_height) {
846 if (vpx_codec_control(&decoder, VP9D_GET_DISPLAY_SIZE,
848 // As last resort use size of first frame as display size.
849 display_width = img->d_w;
850 display_height = img->d_h;
852 display_width = display_size[0];
853 display_height = display_size[1];
856 scaled_img = vpx_img_alloc(NULL, VPX_IMG_FMT_I420, display_width,
860 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
861 vpx_image_scale(img, scaled_img, kFilterBox);
868 char buf[Y4M_BUFFER_SIZE] = {0};
870 if (frame_out == 1) {
872 len = y4m_write_file_header(buf, sizeof(buf),
874 vpx_input_ctx.height,
875 &vpx_input_ctx.framerate, img->fmt);
877 MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
884 len = y4m_write_frame_header(buf, sizeof(buf));
886 MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
893 update_image_md5(img, planes, &md5_ctx);
895 write_image_file(img, planes, outfile);
898 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
899 img->d_w, img->d_h, frame_in);
902 update_image_md5(img, planes, &md5_ctx);
903 MD5Final(md5_digest, &md5_ctx);
904 print_md5(md5_digest, outfile_name);
906 outfile = open_outfile(outfile_name);
907 write_image_file(img, planes, outfile);
913 if (stop_after && frame_in >= stop_after)
917 if (summary || progress) {
918 show_progress(frame_in, frame_out, dx_time);
919 fprintf(stderr, "\n");
922 if (frames_corrupted)
923 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
927 if (vpx_codec_destroy(&decoder)) {
928 fprintf(stderr, "Failed to destroy decoder: %s\n",
929 vpx_codec_error(&decoder));
933 if (!noblit && single_file) {
935 MD5Final(md5_digest, &md5_ctx);
936 print_md5(md5_digest, outfile_name);
942 if (input.vpx_input_ctx->file_type == FILE_TYPE_WEBM)
943 webm_free(input.webm_ctx);
947 if (scaled_img) vpx_img_free(scaled_img);
949 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
950 free(ext_fb_list.ext_fb[i].data);
952 free(ext_fb_list.ext_fb);
957 return frames_corrupted ? EXIT_FAILURE : EXIT_SUCCESS;
960 int main(int argc, const char **argv_) {
961 unsigned int loops = 1, i;
962 char **argv, **argi, **argj;
966 argv = argv_dup(argc - 1, argv_ + 1);
967 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
968 memset(&arg, 0, sizeof(arg));
971 if (arg_match(&arg, &looparg, argi)) {
972 loops = arg_parse_uint(&arg);
977 for (i = 0; !error && i < loops; i++)
978 error = main_loop(argc, argv_);