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/mem_ops.h"
27 #include "vpx_ports/vpx_timer.h"
29 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
30 #include "vpx/vp8dx.h"
33 #include "./md5_utils.h"
35 #include "./tools_common.h"
37 #include "./webmdec.h"
41 static const char *exec_name;
43 struct VpxDecInputContext {
44 struct VpxInputContext *vpx_input_ctx;
45 struct WebmInputContext *webm_ctx;
48 static const arg_def_t looparg = ARG_DEF(NULL, "loops", 1,
49 "Number of times to decode the file");
50 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1,
52 static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0,
53 "Output raw YV12 frames");
54 static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0,
55 "Output raw I420 frames");
56 static const arg_def_t flipuvarg = ARG_DEF(NULL, "flipuv", 0,
57 "Flip the chroma planes in the output");
58 static const arg_def_t noblitarg = ARG_DEF(NULL, "noblit", 0,
59 "Don't process the decoded frames");
60 static const arg_def_t progressarg = ARG_DEF(NULL, "progress", 0,
61 "Show progress after each frame decodes");
62 static const arg_def_t limitarg = ARG_DEF(NULL, "limit", 1,
63 "Stop decoding after n frames");
64 static const arg_def_t skiparg = ARG_DEF(NULL, "skip", 1,
65 "Skip the first n input frames");
66 static const arg_def_t postprocarg = ARG_DEF(NULL, "postproc", 0,
67 "Postprocess decoded frames");
68 static const arg_def_t summaryarg = ARG_DEF(NULL, "summary", 0,
69 "Show timing summary");
70 static const arg_def_t outputfile = ARG_DEF("o", "output", 1,
71 "Output file name pattern (see below)");
72 static const arg_def_t threadsarg = ARG_DEF("t", "threads", 1,
73 "Max threads to use");
74 static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0,
75 "Show version string");
76 static const arg_def_t error_concealment = ARG_DEF(NULL, "error-concealment", 0,
77 "Enable decoder error-concealment");
78 static const arg_def_t scalearg = ARG_DEF("S", "scale", 0,
79 "Scale output frames uniformly");
81 static const arg_def_t fb_arg =
82 ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
84 static const arg_def_t md5arg = ARG_DEF(NULL, "md5", 0,
85 "Compute the MD5 sum of the decoded frame");
87 static const arg_def_t *all_args[] = {
88 &codecarg, &use_yv12, &use_i420, &flipuvarg, &noblitarg,
89 &progressarg, &limitarg, &skiparg, &postprocarg, &summaryarg, &outputfile,
90 &threadsarg, &verbosearg, &scalearg, &fb_arg,
96 #if CONFIG_VP8_DECODER
97 static const arg_def_t addnoise_level = ARG_DEF(NULL, "noise-level", 1,
98 "Enable VP8 postproc add noise");
99 static const arg_def_t deblock = ARG_DEF(NULL, "deblock", 0,
100 "Enable VP8 deblocking");
101 static const arg_def_t demacroblock_level = ARG_DEF(NULL, "demacroblock-level", 1,
102 "Enable VP8 demacroblocking, w/ level");
103 static const arg_def_t pp_debug_info = ARG_DEF(NULL, "pp-debug-info", 1,
104 "Enable VP8 visible debug info");
105 static const arg_def_t pp_disp_ref_frame = ARG_DEF(NULL, "pp-dbg-ref-frame", 1,
106 "Display only selected reference frame per macro block");
107 static const arg_def_t pp_disp_mb_modes = ARG_DEF(NULL, "pp-dbg-mb-modes", 1,
108 "Display only selected macro block modes");
109 static const arg_def_t pp_disp_b_modes = ARG_DEF(NULL, "pp-dbg-b-modes", 1,
110 "Display only selected block modes");
111 static const arg_def_t pp_disp_mvs = ARG_DEF(NULL, "pp-dbg-mvs", 1,
112 "Draw only selected motion vectors");
113 static const arg_def_t mfqe = ARG_DEF(NULL, "mfqe", 0,
114 "Enable multiframe quality enhancement");
116 static const arg_def_t *vp8_pp_args[] = {
117 &addnoise_level, &deblock, &demacroblock_level, &pp_debug_info,
118 &pp_disp_ref_frame, &pp_disp_mb_modes, &pp_disp_b_modes, &pp_disp_mvs, &mfqe,
123 static int vpx_image_scale(vpx_image_t *src, vpx_image_t *dst,
124 FilterModeEnum mode) {
125 assert(src->fmt == VPX_IMG_FMT_I420);
126 assert(dst->fmt == VPX_IMG_FMT_I420);
127 return I420Scale(src->planes[VPX_PLANE_Y], src->stride[VPX_PLANE_Y],
128 src->planes[VPX_PLANE_U], src->stride[VPX_PLANE_U],
129 src->planes[VPX_PLANE_V], src->stride[VPX_PLANE_V],
131 dst->planes[VPX_PLANE_Y], dst->stride[VPX_PLANE_Y],
132 dst->planes[VPX_PLANE_U], dst->stride[VPX_PLANE_U],
133 dst->planes[VPX_PLANE_V], dst->stride[VPX_PLANE_V],
141 fprintf(stderr, "Usage: %s <options> filename\n\n"
142 "Options:\n", exec_name);
143 arg_show_usage(stderr, all_args);
144 #if CONFIG_VP8_DECODER
145 fprintf(stderr, "\nVP8 Postprocessing Options:\n");
146 arg_show_usage(stderr, vp8_pp_args);
149 "\nOutput File Patterns:\n\n"
150 " The -o argument specifies the name of the file(s) to "
151 "write to. If the\n argument does not include any escape "
152 "characters, the output will be\n written to a single file. "
153 "Otherwise, the filename will be calculated by\n expanding "
154 "the following escape characters:\n");
156 "\n\t%%w - Frame width"
157 "\n\t%%h - Frame height"
158 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
159 "\n\n Pattern arguments are only supported in conjunction "
160 "with the --yv12 and\n --i420 options. If the -o option is "
161 "not specified, the output will be\n directed to stdout.\n"
163 fprintf(stderr, "\nIncluded decoders:\n\n");
165 for (i = 0; i < get_vpx_decoder_count(); ++i) {
166 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
167 fprintf(stderr, " %-6s - %s\n",
168 decoder->name, vpx_codec_iface_name(decoder->interface()));
174 static int raw_read_frame(FILE *infile, uint8_t **buffer,
175 size_t *bytes_read, size_t *buffer_size) {
176 char raw_hdr[RAW_FRAME_HDR_SZ];
177 size_t frame_size = 0;
179 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
181 warn("Failed to read RAW frame size\n");
183 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
184 const size_t kFrameTooSmallThreshold = 256 * 1024;
185 frame_size = mem_get_le32(raw_hdr);
187 if (frame_size > kCorruptFrameThreshold) {
188 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
192 if (frame_size < kFrameTooSmallThreshold) {
193 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
194 (unsigned int)frame_size);
197 if (frame_size > *buffer_size) {
198 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
201 *buffer_size = 2 * frame_size;
203 warn("Failed to allocate compressed data buffer\n");
210 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
211 warn("Failed to read full frame\n");
214 *bytes_read = frame_size;
220 static int read_frame(struct VpxDecInputContext *input, uint8_t **buf,
221 size_t *bytes_in_buffer, size_t *buffer_size) {
222 switch (input->vpx_input_ctx->file_type) {
225 return webm_read_frame(input->webm_ctx,
226 buf, bytes_in_buffer, buffer_size);
229 return raw_read_frame(input->vpx_input_ctx->file,
230 buf, bytes_in_buffer, buffer_size);
232 return ivf_read_frame(input->vpx_input_ctx->file,
233 buf, bytes_in_buffer, buffer_size);
239 static void update_image_md5(const vpx_image_t *img, const int planes[3],
243 for (i = 0; i < 3; ++i) {
244 const int plane = planes[i];
245 const unsigned char *buf = img->planes[plane];
246 const int stride = img->stride[plane];
247 const int w = vpx_img_plane_width(img, plane);
248 const int h = vpx_img_plane_height(img, plane);
250 for (y = 0; y < h; ++y) {
251 MD5Update(md5, buf, w);
257 static void write_image_file(const vpx_image_t *img, const int planes[3],
261 for (i = 0; i < 3; ++i) {
262 const int plane = planes[i];
263 const unsigned char *buf = img->planes[plane];
264 const int stride = img->stride[plane];
265 const int w = vpx_img_plane_width(img, plane);
266 const int h = vpx_img_plane_height(img, plane);
268 for (y = 0; y < h; ++y) {
269 fwrite(buf, 1, w, file);
275 int file_is_raw(struct VpxInputContext *input) {
278 vpx_codec_stream_info_t si;
282 if (fread(buf, 1, 32, input->file) == 32) {
285 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
286 for (i = 0; i < get_vpx_decoder_count(); ++i) {
287 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
288 if (!vpx_codec_peek_stream_info(decoder->interface(),
289 buf + 4, 32 - 4, &si)) {
291 input->fourcc = decoder->fourcc;
293 input->height = si.h;
294 input->framerate.numerator = 30;
295 input->framerate.denominator = 1;
306 void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
308 "%d decoded frames/%d showed frames in %"PRId64" us (%.2f fps)\r",
309 frame_in, frame_out, dx_time,
310 (double)frame_out * 1000000.0 / (double)dx_time);
313 struct ExternalFrameBuffer {
319 struct ExternalFrameBufferList {
320 int num_external_frame_buffers;
321 struct ExternalFrameBuffer *ext_fb;
324 // Callback used by libvpx to request an external frame buffer. |cb_priv|
325 // Application private data passed into the set function. |min_size| is the
326 // minimum size in bytes needed to decode the next frame. |fb| pointer to the
328 int get_vp9_frame_buffer(void *cb_priv, size_t min_size,
329 vpx_codec_frame_buffer_t *fb) {
331 struct ExternalFrameBufferList *const ext_fb_list =
332 (struct ExternalFrameBufferList *)cb_priv;
333 if (ext_fb_list == NULL)
336 // Find a free frame buffer.
337 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
338 if (!ext_fb_list->ext_fb[i].in_use)
342 if (i == ext_fb_list->num_external_frame_buffers)
345 if (ext_fb_list->ext_fb[i].size < min_size) {
346 free(ext_fb_list->ext_fb[i].data);
347 ext_fb_list->ext_fb[i].data = (uint8_t *)malloc(min_size);
348 if (!ext_fb_list->ext_fb[i].data)
351 ext_fb_list->ext_fb[i].size = min_size;
354 fb->data = ext_fb_list->ext_fb[i].data;
355 fb->size = ext_fb_list->ext_fb[i].size;
356 ext_fb_list->ext_fb[i].in_use = 1;
358 // Set the frame buffer's private data to point at the external frame buffer.
359 fb->priv = &ext_fb_list->ext_fb[i];
363 // Callback used by libvpx when there are no references to the frame buffer.
364 // |cb_priv| user private data passed into the set function. |fb| pointer
365 // to the frame buffer.
366 int release_vp9_frame_buffer(void *cb_priv,
367 vpx_codec_frame_buffer_t *fb) {
368 struct ExternalFrameBuffer *const ext_fb =
369 (struct ExternalFrameBuffer *)fb->priv;
375 void generate_filename(const char *pattern, char *out, size_t q_len,
376 unsigned int d_w, unsigned int d_h,
377 unsigned int frame_in) {
378 const char *p = pattern;
382 char *next_pat = strchr(p, '%');
387 /* parse the pattern */
391 snprintf(q, q_len - 1, "%d", d_w);
394 snprintf(q, q_len - 1, "%d", d_h);
397 snprintf(q, q_len - 1, "%d", frame_in);
400 snprintf(q, q_len - 1, "%02d", frame_in);
403 snprintf(q, q_len - 1, "%03d", frame_in);
406 snprintf(q, q_len - 1, "%04d", frame_in);
409 snprintf(q, q_len - 1, "%05d", frame_in);
412 snprintf(q, q_len - 1, "%06d", frame_in);
415 snprintf(q, q_len - 1, "%07d", frame_in);
418 snprintf(q, q_len - 1, "%08d", frame_in);
421 snprintf(q, q_len - 1, "%09d", frame_in);
424 die("Unrecognized pattern %%%c\n", p[1]);
428 if (pat_len >= q_len - 1)
429 die("Output filename too long.\n");
436 /* copy the next segment */
438 copy_len = strlen(p);
440 copy_len = next_pat - p;
442 if (copy_len >= q_len - 1)
443 die("Output filename too long.\n");
445 memcpy(q, p, copy_len);
454 static int is_single_file(const char *outfile_pattern) {
455 const char *p = outfile_pattern;
459 if (p && p[1] >= '1' && p[1] <= '9')
460 return 0; // pattern contains sequence number, so it's not unique
468 static void print_md5(unsigned char digest[16], const char *filename) {
471 for (i = 0; i < 16; ++i)
472 printf("%02x", digest[i]);
473 printf(" %s\n", filename);
476 static FILE *open_outfile(const char *name) {
477 if (strcmp("-", name) == 0) {
478 set_binary_mode(stdout);
481 FILE *file = fopen(name, "wb");
483 fatal("Failed to output file %s", name);
488 int main_loop(int argc, const char **argv_) {
489 vpx_codec_ctx_t decoder;
493 size_t bytes_in_buffer = 0, buffer_size = 0;
495 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
496 int do_md5 = 0, progress = 0;
497 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
500 const VpxInterface *interface = NULL;
501 const VpxInterface *fourcc_interface = NULL;
502 uint64_t dx_time = 0;
504 char **argv, **argi, **argj;
508 vpx_codec_dec_cfg_t cfg = {0};
509 #if CONFIG_VP8_DECODER
510 vp8_postproc_cfg_t vp8_pp_cfg = {0};
511 int vp8_dbg_color_ref_frame = 0;
512 int vp8_dbg_color_mb_modes = 0;
513 int vp8_dbg_color_b_modes = 0;
514 int vp8_dbg_display_mv = 0;
516 int frames_corrupted = 0;
519 vpx_image_t *scaled_img = NULL;
520 int frame_avail, got_data;
521 int num_external_frame_buffers = 0;
522 struct ExternalFrameBufferList ext_fb_list = {0};
524 const char *outfile_pattern = NULL;
525 char outfile_name[PATH_MAX] = {0};
526 FILE *outfile = NULL;
529 unsigned char md5_digest[16];
531 struct VpxDecInputContext input = {0};
532 struct VpxInputContext vpx_input_ctx = {0};
534 struct WebmInputContext webm_ctx = {0};
535 input.webm_ctx = &webm_ctx;
537 input.vpx_input_ctx = &vpx_input_ctx;
539 /* Parse command line */
540 exec_name = argv_[0];
541 argv = argv_dup(argc - 1, argv_ + 1);
543 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
544 memset(&arg, 0, sizeof(arg));
547 if (arg_match(&arg, &codecarg, argi)) {
548 interface = get_vpx_decoder_by_name(arg.val);
550 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
551 } else if (arg_match(&arg, &looparg, argi)) {
553 } else if (arg_match(&arg, &outputfile, argi))
554 outfile_pattern = arg.val;
555 else if (arg_match(&arg, &use_yv12, argi)) {
558 } else if (arg_match(&arg, &use_i420, argi)) {
561 } else if (arg_match(&arg, &flipuvarg, argi))
563 else if (arg_match(&arg, &noblitarg, argi))
565 else if (arg_match(&arg, &progressarg, argi))
567 else if (arg_match(&arg, &limitarg, argi))
568 stop_after = arg_parse_uint(&arg);
569 else if (arg_match(&arg, &skiparg, argi))
570 arg_skip = arg_parse_uint(&arg);
571 else if (arg_match(&arg, &postprocarg, argi))
573 else if (arg_match(&arg, &md5arg, argi))
575 else if (arg_match(&arg, &summaryarg, argi))
577 else if (arg_match(&arg, &threadsarg, argi))
578 cfg.threads = arg_parse_uint(&arg);
579 else if (arg_match(&arg, &verbosearg, argi))
581 else if (arg_match(&arg, &scalearg, argi))
583 else if (arg_match(&arg, &fb_arg, argi))
584 num_external_frame_buffers = arg_parse_uint(&arg);
586 #if CONFIG_VP8_DECODER
587 else if (arg_match(&arg, &addnoise_level, argi)) {
589 vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE;
590 vp8_pp_cfg.noise_level = arg_parse_uint(&arg);
591 } else if (arg_match(&arg, &demacroblock_level, argi)) {
593 vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK;
594 vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg);
595 } else if (arg_match(&arg, &deblock, argi)) {
597 vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK;
598 } else if (arg_match(&arg, &mfqe, argi)) {
600 vp8_pp_cfg.post_proc_flag |= VP8_MFQE;
601 } else if (arg_match(&arg, &pp_debug_info, argi)) {
602 unsigned int level = arg_parse_uint(&arg);
605 vp8_pp_cfg.post_proc_flag &= ~0x7;
608 vp8_pp_cfg.post_proc_flag |= level;
609 } else if (arg_match(&arg, &pp_disp_ref_frame, argi)) {
610 unsigned int flags = arg_parse_int(&arg);
613 vp8_dbg_color_ref_frame = flags;
615 } else if (arg_match(&arg, &pp_disp_mb_modes, argi)) {
616 unsigned int flags = arg_parse_int(&arg);
619 vp8_dbg_color_mb_modes = flags;
621 } else if (arg_match(&arg, &pp_disp_b_modes, argi)) {
622 unsigned int flags = arg_parse_int(&arg);
625 vp8_dbg_color_b_modes = flags;
627 } else if (arg_match(&arg, &pp_disp_mvs, argi)) {
628 unsigned int flags = arg_parse_int(&arg);
631 vp8_dbg_display_mv = flags;
633 } else if (arg_match(&arg, &error_concealment, argi)) {
642 /* Check for unrecognized options */
643 for (argi = argv; *argi; argi++)
644 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
645 die("Error: Unrecognized option %s\n", *argi);
647 /* Handle non-option arguments */
654 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
657 fprintf(stderr, "Failed to open file '%s'", strcmp(fn, "-") ? fn : "stdin");
660 #if CONFIG_OS_SUPPORT
661 /* Make sure we don't dump to the terminal, unless forced to with -o - */
662 if (!outfile_pattern && isatty(fileno(stdout)) && !do_md5 && !noblit) {
664 "Not dumping raw video to your terminal. Use '-o -' to "
669 input.vpx_input_ctx->file = infile;
670 if (file_is_ivf(input.vpx_input_ctx))
671 input.vpx_input_ctx->file_type = FILE_TYPE_IVF;
673 else if (file_is_webm(input.webm_ctx, input.vpx_input_ctx))
674 input.vpx_input_ctx->file_type = FILE_TYPE_WEBM;
676 else if (file_is_raw(input.vpx_input_ctx))
677 input.vpx_input_ctx->file_type = FILE_TYPE_RAW;
679 fprintf(stderr, "Unrecognized input file type.\n");
681 fprintf(stderr, "vpxdec was built without WebM container support.\n");
686 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
687 single_file = is_single_file(outfile_pattern);
689 if (!noblit && single_file) {
690 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
691 vpx_input_ctx.width, vpx_input_ctx.height, 0);
695 outfile = open_outfile(outfile_name);
698 if (use_y4m && !noblit) {
700 fprintf(stderr, "YUV4MPEG2 not supported with output patterns,"
701 " try --i420 or --yv12.\n");
706 if (vpx_input_ctx.file_type == FILE_TYPE_WEBM) {
707 if (webm_guess_framerate(input.webm_ctx, input.vpx_input_ctx)) {
708 fprintf(stderr, "Failed to guess framerate -- error parsing "
716 fourcc_interface = get_vpx_decoder_by_fourcc(vpx_input_ctx.fourcc);
717 if (interface && fourcc_interface && interface != fourcc_interface)
718 warn("Header indicates codec: %s\n", fourcc_interface->name);
720 interface = fourcc_interface;
723 interface = get_vpx_decoder_by_index(0);
725 dec_flags = (postproc ? VPX_CODEC_USE_POSTPROC : 0) |
726 (ec_enabled ? VPX_CODEC_USE_ERROR_CONCEALMENT : 0);
727 if (vpx_codec_dec_init(&decoder, interface->interface(), &cfg, dec_flags)) {
728 fprintf(stderr, "Failed to initialize decoder: %s\n",
729 vpx_codec_error(&decoder));
734 fprintf(stderr, "%s\n", decoder.name);
736 #if CONFIG_VP8_DECODER
738 if (vp8_pp_cfg.post_proc_flag
739 && vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg)) {
740 fprintf(stderr, "Failed to configure postproc: %s\n", vpx_codec_error(&decoder));
744 if (vp8_dbg_color_ref_frame
745 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_REF_FRAME, vp8_dbg_color_ref_frame)) {
746 fprintf(stderr, "Failed to configure reference block visualizer: %s\n", vpx_codec_error(&decoder));
750 if (vp8_dbg_color_mb_modes
751 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_MB_MODES, vp8_dbg_color_mb_modes)) {
752 fprintf(stderr, "Failed to configure macro block visualizer: %s\n", vpx_codec_error(&decoder));
756 if (vp8_dbg_color_b_modes
757 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_B_MODES, vp8_dbg_color_b_modes)) {
758 fprintf(stderr, "Failed to configure block visualizer: %s\n", vpx_codec_error(&decoder));
762 if (vp8_dbg_display_mv
763 && vpx_codec_control(&decoder, VP8_SET_DBG_DISPLAY_MV, vp8_dbg_display_mv)) {
764 fprintf(stderr, "Failed to configure motion vector visualizer: %s\n", vpx_codec_error(&decoder));
771 fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
773 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size))
778 if (num_external_frame_buffers > 0) {
779 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
780 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
781 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
782 if (vpx_codec_set_frame_buffer_functions(
783 &decoder, get_vp9_frame_buffer, release_vp9_frame_buffer,
785 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
786 vpx_codec_error(&decoder));
795 while (frame_avail || got_data) {
796 vpx_codec_iter_t iter = NULL;
798 struct vpx_usec_timer timer;
802 if (!stop_after || frame_in < stop_after) {
803 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
807 vpx_usec_timer_start(&timer);
809 if (vpx_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer,
811 const char *detail = vpx_codec_error_detail(&decoder);
812 warn("Failed to decode frame %d: %s",
813 frame_in, vpx_codec_error(&decoder));
816 warn("Additional information: %s", detail);
820 vpx_usec_timer_mark(&timer);
821 dx_time += vpx_usec_timer_elapsed(&timer);
825 vpx_usec_timer_start(&timer);
828 if ((img = vpx_codec_get_frame(&decoder, &iter))) {
833 vpx_usec_timer_mark(&timer);
834 dx_time += (unsigned int)vpx_usec_timer_elapsed(&timer);
836 if (vpx_codec_control(&decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted)) {
837 warn("Failed VP8_GET_FRAME_CORRUPTED: %s", vpx_codec_error(&decoder));
840 frames_corrupted += corrupted;
843 show_progress(frame_in, frame_out, dx_time);
845 if (!noblit && img) {
846 const int PLANES_YUV[] = {VPX_PLANE_Y, VPX_PLANE_U, VPX_PLANE_V};
847 const int PLANES_YVU[] = {VPX_PLANE_Y, VPX_PLANE_V, VPX_PLANE_U};
848 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
851 if (frame_out == 1) {
852 // If the output frames are to be scaled to a fixed display size then
853 // use the width and height specified in the container. If either of
854 // these is set to 0, use the display size set in the first frame
855 // header. If that is unavailable, use the raw decoded size of the
856 // first decoded frame.
857 int display_width = vpx_input_ctx.width;
858 int display_height = vpx_input_ctx.height;
859 if (!display_width || !display_height) {
861 if (vpx_codec_control(&decoder, VP9D_GET_DISPLAY_SIZE,
863 // As last resort use size of first frame as display size.
864 display_width = img->d_w;
865 display_height = img->d_h;
867 display_width = display_size[0];
868 display_height = display_size[1];
871 scaled_img = vpx_img_alloc(NULL, VPX_IMG_FMT_I420, display_width,
875 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
877 vpx_image_scale(img, scaled_img, kFilterBox);
880 fprintf(stderr, "Failed to scale output frame: %s.\n"
881 "Scaling is disabled in this configuration. "
882 "To enable scaling, configure with --enable-libyuv\n",
883 vpx_codec_error(&decoder));
891 char buf[Y4M_BUFFER_SIZE] = {0};
893 if (frame_out == 1) {
895 len = y4m_write_file_header(buf, sizeof(buf),
897 vpx_input_ctx.height,
898 &vpx_input_ctx.framerate,
901 MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
908 len = y4m_write_frame_header(buf, sizeof(buf));
910 MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
917 update_image_md5(img, planes, &md5_ctx);
919 write_image_file(img, planes, outfile);
922 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
923 img->d_w, img->d_h, frame_in);
926 update_image_md5(img, planes, &md5_ctx);
927 MD5Final(md5_digest, &md5_ctx);
928 print_md5(md5_digest, outfile_name);
930 outfile = open_outfile(outfile_name);
931 write_image_file(img, planes, outfile);
937 if (stop_after && frame_in >= stop_after)
941 if (summary || progress) {
942 show_progress(frame_in, frame_out, dx_time);
943 fprintf(stderr, "\n");
946 if (frames_corrupted)
947 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
951 if (vpx_codec_destroy(&decoder)) {
952 fprintf(stderr, "Failed to destroy decoder: %s\n",
953 vpx_codec_error(&decoder));
957 if (!noblit && single_file) {
959 MD5Final(md5_digest, &md5_ctx);
960 print_md5(md5_digest, outfile_name);
967 if (input.vpx_input_ctx->file_type == FILE_TYPE_WEBM)
968 webm_free(input.webm_ctx);
971 if (input.vpx_input_ctx->file_type != FILE_TYPE_WEBM)
974 if (scaled_img) vpx_img_free(scaled_img);
976 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
977 free(ext_fb_list.ext_fb[i].data);
979 free(ext_fb_list.ext_fb);
984 return frames_corrupted ? EXIT_FAILURE : EXIT_SUCCESS;
987 int main(int argc, const char **argv_) {
988 unsigned int loops = 1, i;
989 char **argv, **argi, **argj;
993 argv = argv_dup(argc - 1, argv_ + 1);
994 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
995 memset(&arg, 0, sizeof(arg));
998 if (arg_match(&arg, &looparg, argi)) {
999 loops = arg_parse_uint(&arg);
1004 for (i = 0; !error && i < loops; i++)
1005 error = main_loop(argc, argv_);