Move VP9 SSIM metrics to vpx_dsp.
[aom.git] / vpxdec.c
blobbaa61151ca89b5acc4756ead14a4efdcc1d5e55e
1 /*
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.
9 */
11 #include <assert.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <limits.h>
18 #include "./vpx_config.h"
20 #if CONFIG_LIBYUV
21 #include "third_party/libyuv/include/libyuv/scale.h"
22 #endif
24 #include "./args.h"
25 #include "./ivfdec.h"
27 #include "vpx/vpx_decoder.h"
28 #include "vpx_ports/mem_ops.h"
29 #include "vpx_ports/vpx_timer.h"
31 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
32 #include "vpx/vp8dx.h"
33 #endif
35 #include "./md5_utils.h"
37 #include "./tools_common.h"
38 #if CONFIG_WEBM_IO
39 #include "./webmdec.h"
40 #endif
41 #include "./y4menc.h"
43 static const char *exec_name;
45 struct VpxDecInputContext {
46 struct VpxInputContext *vpx_input_ctx;
47 struct WebmInputContext *webm_ctx;
50 static const arg_def_t looparg = ARG_DEF(
51 NULL, "loops", 1, "Number of times to decode the file");
52 static const arg_def_t codecarg = ARG_DEF(
53 NULL, "codec", 1, "Codec to use");
54 static const arg_def_t use_yv12 = ARG_DEF(
55 NULL, "yv12", 0, "Output raw YV12 frames");
56 static const arg_def_t use_i420 = ARG_DEF(
57 NULL, "i420", 0, "Output raw I420 frames");
58 static const arg_def_t flipuvarg = ARG_DEF(
59 NULL, "flipuv", 0, "Flip the chroma planes in the output");
60 static const arg_def_t rawvideo = ARG_DEF(
61 NULL, "rawvideo", 0, "Output raw YUV frames");
62 static const arg_def_t noblitarg = ARG_DEF(
63 NULL, "noblit", 0, "Don't process the decoded frames");
64 static const arg_def_t progressarg = ARG_DEF(
65 NULL, "progress", 0, "Show progress after each frame decodes");
66 static const arg_def_t limitarg = ARG_DEF(
67 NULL, "limit", 1, "Stop decoding after n frames");
68 static const arg_def_t skiparg = ARG_DEF(
69 NULL, "skip", 1, "Skip the first n input frames");
70 static const arg_def_t postprocarg = ARG_DEF(
71 NULL, "postproc", 0, "Postprocess decoded frames");
72 static const arg_def_t summaryarg = ARG_DEF(
73 NULL, "summary", 0, "Show timing summary");
74 static const arg_def_t outputfile = ARG_DEF(
75 "o", "output", 1, "Output file name pattern (see below)");
76 static const arg_def_t threadsarg = ARG_DEF(
77 "t", "threads", 1, "Max threads to use");
78 static const arg_def_t frameparallelarg = ARG_DEF(
79 NULL, "frame-parallel", 0, "Frame parallel decode");
80 static const arg_def_t verbosearg = ARG_DEF(
81 "v", "verbose", 0, "Show version string");
82 static const arg_def_t error_concealment = ARG_DEF(
83 NULL, "error-concealment", 0, "Enable decoder error-concealment");
84 static const arg_def_t scalearg = ARG_DEF(
85 "S", "scale", 0, "Scale output frames uniformly");
86 static const arg_def_t continuearg = ARG_DEF(
87 "k", "keep-going", 0, "(debug) Continue decoding after error");
88 static const arg_def_t fb_arg = ARG_DEF(
89 NULL, "frame-buffers", 1, "Number of frame buffers to use");
90 static const arg_def_t md5arg = ARG_DEF(
91 NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
92 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
93 static const arg_def_t outbitdeptharg = ARG_DEF(
94 NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
95 #endif
97 static const arg_def_t *all_args[] = {
98 &codecarg, &use_yv12, &use_i420, &flipuvarg, &rawvideo, &noblitarg,
99 &progressarg, &limitarg, &skiparg, &postprocarg, &summaryarg, &outputfile,
100 &threadsarg, &frameparallelarg, &verbosearg, &scalearg, &fb_arg,
101 &md5arg, &error_concealment, &continuearg,
102 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
103 &outbitdeptharg,
104 #endif
105 NULL
108 #if CONFIG_VP8_DECODER
109 static const arg_def_t addnoise_level = ARG_DEF(
110 NULL, "noise-level", 1, "Enable VP8 postproc add noise");
111 static const arg_def_t deblock = ARG_DEF(
112 NULL, "deblock", 0, "Enable VP8 deblocking");
113 static const arg_def_t demacroblock_level = ARG_DEF(
114 NULL, "demacroblock-level", 1, "Enable VP8 demacroblocking, w/ level");
115 static const arg_def_t pp_debug_info = ARG_DEF(
116 NULL, "pp-debug-info", 1, "Enable VP8 visible debug info");
117 static const arg_def_t pp_disp_ref_frame = ARG_DEF(
118 NULL, "pp-dbg-ref-frame", 1,
119 "Display only selected reference frame per macro block");
120 static const arg_def_t pp_disp_mb_modes = ARG_DEF(
121 NULL, "pp-dbg-mb-modes", 1, "Display only selected macro block modes");
122 static const arg_def_t pp_disp_b_modes = ARG_DEF(
123 NULL, "pp-dbg-b-modes", 1, "Display only selected block modes");
124 static const arg_def_t pp_disp_mvs = ARG_DEF(
125 NULL, "pp-dbg-mvs", 1, "Draw only selected motion vectors");
126 static const arg_def_t mfqe = ARG_DEF(
127 NULL, "mfqe", 0, "Enable multiframe quality enhancement");
129 static const arg_def_t *vp8_pp_args[] = {
130 &addnoise_level, &deblock, &demacroblock_level, &pp_debug_info,
131 &pp_disp_ref_frame, &pp_disp_mb_modes, &pp_disp_b_modes, &pp_disp_mvs, &mfqe,
132 NULL
134 #endif
136 #if CONFIG_LIBYUV
137 static INLINE int libyuv_scale(vpx_image_t *src, vpx_image_t *dst,
138 FilterModeEnum mode) {
139 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
140 if (src->fmt == VPX_IMG_FMT_I42016) {
141 assert(dst->fmt == VPX_IMG_FMT_I42016);
142 return I420Scale_16((uint16_t*)src->planes[VPX_PLANE_Y],
143 src->stride[VPX_PLANE_Y]/2,
144 (uint16_t*)src->planes[VPX_PLANE_U],
145 src->stride[VPX_PLANE_U]/2,
146 (uint16_t*)src->planes[VPX_PLANE_V],
147 src->stride[VPX_PLANE_V]/2,
148 src->d_w, src->d_h,
149 (uint16_t*)dst->planes[VPX_PLANE_Y],
150 dst->stride[VPX_PLANE_Y]/2,
151 (uint16_t*)dst->planes[VPX_PLANE_U],
152 dst->stride[VPX_PLANE_U]/2,
153 (uint16_t*)dst->planes[VPX_PLANE_V],
154 dst->stride[VPX_PLANE_V]/2,
155 dst->d_w, dst->d_h,
156 mode);
158 #endif
159 assert(src->fmt == VPX_IMG_FMT_I420);
160 assert(dst->fmt == VPX_IMG_FMT_I420);
161 return I420Scale(src->planes[VPX_PLANE_Y], src->stride[VPX_PLANE_Y],
162 src->planes[VPX_PLANE_U], src->stride[VPX_PLANE_U],
163 src->planes[VPX_PLANE_V], src->stride[VPX_PLANE_V],
164 src->d_w, src->d_h,
165 dst->planes[VPX_PLANE_Y], dst->stride[VPX_PLANE_Y],
166 dst->planes[VPX_PLANE_U], dst->stride[VPX_PLANE_U],
167 dst->planes[VPX_PLANE_V], dst->stride[VPX_PLANE_V],
168 dst->d_w, dst->d_h,
169 mode);
171 #endif
173 void usage_exit(void) {
174 int i;
176 fprintf(stderr, "Usage: %s <options> filename\n\n"
177 "Options:\n", exec_name);
178 arg_show_usage(stderr, all_args);
179 #if CONFIG_VP8_DECODER
180 fprintf(stderr, "\nVP8 Postprocessing Options:\n");
181 arg_show_usage(stderr, vp8_pp_args);
182 #endif
183 fprintf(stderr,
184 "\nOutput File Patterns:\n\n"
185 " The -o argument specifies the name of the file(s) to "
186 "write to. If the\n argument does not include any escape "
187 "characters, the output will be\n written to a single file. "
188 "Otherwise, the filename will be calculated by\n expanding "
189 "the following escape characters:\n");
190 fprintf(stderr,
191 "\n\t%%w - Frame width"
192 "\n\t%%h - Frame height"
193 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
194 "\n\n Pattern arguments are only supported in conjunction "
195 "with the --yv12 and\n --i420 options. If the -o option is "
196 "not specified, the output will be\n directed to stdout.\n"
198 fprintf(stderr, "\nIncluded decoders:\n\n");
200 for (i = 0; i < get_vpx_decoder_count(); ++i) {
201 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
202 fprintf(stderr, " %-6s - %s\n",
203 decoder->name, vpx_codec_iface_name(decoder->codec_interface()));
206 exit(EXIT_FAILURE);
209 static int raw_read_frame(FILE *infile, uint8_t **buffer,
210 size_t *bytes_read, size_t *buffer_size) {
211 char raw_hdr[RAW_FRAME_HDR_SZ];
212 size_t frame_size = 0;
214 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
215 if (!feof(infile))
216 warn("Failed to read RAW frame size\n");
217 } else {
218 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
219 const size_t kFrameTooSmallThreshold = 256 * 1024;
220 frame_size = mem_get_le32(raw_hdr);
222 if (frame_size > kCorruptFrameThreshold) {
223 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
224 frame_size = 0;
227 if (frame_size < kFrameTooSmallThreshold) {
228 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
229 (unsigned int)frame_size);
232 if (frame_size > *buffer_size) {
233 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
234 if (new_buf) {
235 *buffer = new_buf;
236 *buffer_size = 2 * frame_size;
237 } else {
238 warn("Failed to allocate compressed data buffer\n");
239 frame_size = 0;
244 if (!feof(infile)) {
245 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
246 warn("Failed to read full frame\n");
247 return 1;
249 *bytes_read = frame_size;
252 return 0;
255 static int read_frame(struct VpxDecInputContext *input, uint8_t **buf,
256 size_t *bytes_in_buffer, size_t *buffer_size) {
257 switch (input->vpx_input_ctx->file_type) {
258 #if CONFIG_WEBM_IO
259 case FILE_TYPE_WEBM:
260 return webm_read_frame(input->webm_ctx,
261 buf, bytes_in_buffer, buffer_size);
262 #endif
263 case FILE_TYPE_RAW:
264 return raw_read_frame(input->vpx_input_ctx->file,
265 buf, bytes_in_buffer, buffer_size);
266 case FILE_TYPE_IVF:
267 return ivf_read_frame(input->vpx_input_ctx->file,
268 buf, bytes_in_buffer, buffer_size);
269 default:
270 return 1;
274 static void update_image_md5(const vpx_image_t *img, const int planes[3],
275 MD5Context *md5) {
276 int i, y;
278 for (i = 0; i < 3; ++i) {
279 const int plane = planes[i];
280 const unsigned char *buf = img->planes[plane];
281 const int stride = img->stride[plane];
282 const int w = vpx_img_plane_width(img, plane) *
283 ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
284 const int h = vpx_img_plane_height(img, plane);
286 for (y = 0; y < h; ++y) {
287 MD5Update(md5, buf, w);
288 buf += stride;
293 static void write_image_file(const vpx_image_t *img, const int planes[3],
294 FILE *file) {
295 int i, y;
296 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
297 const int bytes_per_sample = ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
298 #else
299 const int bytes_per_sample = 1;
300 #endif
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);
311 buf += stride;
316 static int file_is_raw(struct VpxInputContext *input) {
317 uint8_t buf[32];
318 int is_raw = 0;
319 vpx_codec_stream_info_t si;
321 si.sz = sizeof(si);
323 if (fread(buf, 1, 32, input->file) == 32) {
324 int i;
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)) {
331 is_raw = 1;
332 input->fourcc = decoder->fourcc;
333 input->width = si.w;
334 input->height = si.h;
335 input->framerate.numerator = 30;
336 input->framerate.denominator = 1;
337 break;
343 rewind(input->file);
344 return is_raw;
347 static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
348 fprintf(stderr,
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 {
355 uint8_t* data;
356 size_t size;
357 int in_use;
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
368 // frame buffer.
369 static int get_vp9_frame_buffer(void *cb_priv, size_t min_size,
370 vpx_codec_frame_buffer_t *fb) {
371 int i;
372 struct ExternalFrameBufferList *const ext_fb_list =
373 (struct ExternalFrameBufferList *)cb_priv;
374 if (ext_fb_list == NULL)
375 return -1;
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)
380 break;
383 if (i == ext_fb_list->num_external_frame_buffers)
384 return -1;
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 *)calloc(min_size, sizeof(uint8_t));
389 if (!ext_fb_list->ext_fb[i].data)
390 return -1;
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];
401 return 0;
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 static 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;
411 (void)cb_priv;
412 ext_fb->in_use = 0;
413 return 0;
416 static 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;
420 char *q = out;
422 do {
423 char *next_pat = strchr(p, '%');
425 if (p == next_pat) {
426 size_t pat_len;
428 /* parse the pattern */
429 q[q_len - 1] = '\0';
430 switch (p[1]) {
431 case 'w':
432 snprintf(q, q_len - 1, "%d", d_w);
433 break;
434 case 'h':
435 snprintf(q, q_len - 1, "%d", d_h);
436 break;
437 case '1':
438 snprintf(q, q_len - 1, "%d", frame_in);
439 break;
440 case '2':
441 snprintf(q, q_len - 1, "%02d", frame_in);
442 break;
443 case '3':
444 snprintf(q, q_len - 1, "%03d", frame_in);
445 break;
446 case '4':
447 snprintf(q, q_len - 1, "%04d", frame_in);
448 break;
449 case '5':
450 snprintf(q, q_len - 1, "%05d", frame_in);
451 break;
452 case '6':
453 snprintf(q, q_len - 1, "%06d", frame_in);
454 break;
455 case '7':
456 snprintf(q, q_len - 1, "%07d", frame_in);
457 break;
458 case '8':
459 snprintf(q, q_len - 1, "%08d", frame_in);
460 break;
461 case '9':
462 snprintf(q, q_len - 1, "%09d", frame_in);
463 break;
464 default:
465 die("Unrecognized pattern %%%c\n", p[1]);
466 break;
469 pat_len = strlen(q);
470 if (pat_len >= q_len - 1)
471 die("Output filename too long.\n");
472 q += pat_len;
473 p += 2;
474 q_len -= pat_len;
475 } else {
476 size_t copy_len;
478 /* copy the next segment */
479 if (!next_pat)
480 copy_len = strlen(p);
481 else
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);
488 q[copy_len] = '\0';
489 q += copy_len;
490 p += copy_len;
491 q_len -= copy_len;
493 } while (*p);
496 static int is_single_file(const char *outfile_pattern) {
497 const char *p = outfile_pattern;
499 do {
500 p = strchr(p, '%');
501 if (p && p[1] >= '1' && p[1] <= '9')
502 return 0; // pattern contains sequence number, so it's not unique
503 if (p)
504 p++;
505 } while (p);
507 return 1;
510 static void print_md5(unsigned char digest[16], const char *filename) {
511 int i;
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);
521 return stdout;
522 } else {
523 FILE *file = fopen(name, "wb");
524 if (!file)
525 fatal("Failed to open output file '%s'", name);
526 return file;
530 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
531 static int img_shifted_realloc_required(const vpx_image_t *img,
532 const vpx_image_t *shifted,
533 vpx_img_fmt_t required_fmt) {
534 return img->d_w != shifted->d_w ||
535 img->d_h != shifted->d_h ||
536 required_fmt != shifted->fmt;
538 #endif
540 static int main_loop(int argc, const char **argv_) {
541 vpx_codec_ctx_t decoder;
542 char *fn = NULL;
543 int i;
544 uint8_t *buf = NULL;
545 size_t bytes_in_buffer = 0, buffer_size = 0;
546 FILE *infile;
547 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
548 int do_md5 = 0, progress = 0, frame_parallel = 0;
549 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
550 int arg_skip = 0;
551 int ec_enabled = 0;
552 int keep_going = 0;
553 const VpxInterface *interface = NULL;
554 const VpxInterface *fourcc_interface = NULL;
555 uint64_t dx_time = 0;
556 struct arg arg;
557 char **argv, **argi, **argj;
559 int single_file;
560 int use_y4m = 1;
561 int opt_yv12 = 0;
562 int opt_i420 = 0;
563 vpx_codec_dec_cfg_t cfg = {0, 0, 0};
564 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
565 int output_bit_depth = 0;
566 #endif
567 #if CONFIG_VP8_DECODER
568 vp8_postproc_cfg_t vp8_pp_cfg = {0};
569 int vp8_dbg_color_ref_frame = 0;
570 int vp8_dbg_color_mb_modes = 0;
571 int vp8_dbg_color_b_modes = 0;
572 int vp8_dbg_display_mv = 0;
573 #endif
574 int frames_corrupted = 0;
575 int dec_flags = 0;
576 int do_scale = 0;
577 vpx_image_t *scaled_img = NULL;
578 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
579 vpx_image_t *img_shifted = NULL;
580 #endif
581 int frame_avail, got_data, flush_decoder = 0;
582 int num_external_frame_buffers = 0;
583 struct ExternalFrameBufferList ext_fb_list = {0, NULL};
585 const char *outfile_pattern = NULL;
586 char outfile_name[PATH_MAX] = {0};
587 FILE *outfile = NULL;
589 MD5Context md5_ctx;
590 unsigned char md5_digest[16];
592 struct VpxDecInputContext input = {NULL, NULL};
593 struct VpxInputContext vpx_input_ctx;
594 #if CONFIG_WEBM_IO
595 struct WebmInputContext webm_ctx;
596 memset(&(webm_ctx), 0, sizeof(webm_ctx));
597 input.webm_ctx = &webm_ctx;
598 #endif
599 input.vpx_input_ctx = &vpx_input_ctx;
601 /* Parse command line */
602 exec_name = argv_[0];
603 argv = argv_dup(argc - 1, argv_ + 1);
605 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
606 memset(&arg, 0, sizeof(arg));
607 arg.argv_step = 1;
609 if (arg_match(&arg, &codecarg, argi)) {
610 interface = get_vpx_decoder_by_name(arg.val);
611 if (!interface)
612 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
613 } else if (arg_match(&arg, &looparg, argi)) {
614 // no-op
615 } else if (arg_match(&arg, &outputfile, argi))
616 outfile_pattern = arg.val;
617 else if (arg_match(&arg, &use_yv12, argi)) {
618 use_y4m = 0;
619 flipuv = 1;
620 opt_yv12 = 1;
621 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
622 output_bit_depth = 8; // For yv12 8-bit depth output is assumed
623 #endif
624 } else if (arg_match(&arg, &use_i420, argi)) {
625 use_y4m = 0;
626 flipuv = 0;
627 opt_i420 = 1;
628 } else if (arg_match(&arg, &rawvideo, argi)) {
629 use_y4m = 0;
630 } else if (arg_match(&arg, &flipuvarg, argi))
631 flipuv = 1;
632 else if (arg_match(&arg, &noblitarg, argi))
633 noblit = 1;
634 else if (arg_match(&arg, &progressarg, argi))
635 progress = 1;
636 else if (arg_match(&arg, &limitarg, argi))
637 stop_after = arg_parse_uint(&arg);
638 else if (arg_match(&arg, &skiparg, argi))
639 arg_skip = arg_parse_uint(&arg);
640 else if (arg_match(&arg, &postprocarg, argi))
641 postproc = 1;
642 else if (arg_match(&arg, &md5arg, argi))
643 do_md5 = 1;
644 else if (arg_match(&arg, &summaryarg, argi))
645 summary = 1;
646 else if (arg_match(&arg, &threadsarg, argi))
647 cfg.threads = arg_parse_uint(&arg);
648 #if CONFIG_VP9_DECODER
649 else if (arg_match(&arg, &frameparallelarg, argi))
650 frame_parallel = 1;
651 #endif
652 else if (arg_match(&arg, &verbosearg, argi))
653 quiet = 0;
654 else if (arg_match(&arg, &scalearg, argi))
655 do_scale = 1;
656 else if (arg_match(&arg, &fb_arg, argi))
657 num_external_frame_buffers = arg_parse_uint(&arg);
658 else if (arg_match(&arg, &continuearg, argi))
659 keep_going = 1;
660 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
661 else if (arg_match(&arg, &outbitdeptharg, argi)) {
662 output_bit_depth = arg_parse_uint(&arg);
664 #endif
665 #if CONFIG_VP8_DECODER
666 else if (arg_match(&arg, &addnoise_level, argi)) {
667 postproc = 1;
668 vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE;
669 vp8_pp_cfg.noise_level = arg_parse_uint(&arg);
670 } else if (arg_match(&arg, &demacroblock_level, argi)) {
671 postproc = 1;
672 vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK;
673 vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg);
674 } else if (arg_match(&arg, &deblock, argi)) {
675 postproc = 1;
676 vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK;
677 } else if (arg_match(&arg, &mfqe, argi)) {
678 postproc = 1;
679 vp8_pp_cfg.post_proc_flag |= VP8_MFQE;
680 } else if (arg_match(&arg, &pp_debug_info, argi)) {
681 unsigned int level = arg_parse_uint(&arg);
683 postproc = 1;
684 vp8_pp_cfg.post_proc_flag &= ~0x7;
686 if (level)
687 vp8_pp_cfg.post_proc_flag |= level;
688 } else if (arg_match(&arg, &pp_disp_ref_frame, argi)) {
689 unsigned int flags = arg_parse_int(&arg);
690 if (flags) {
691 postproc = 1;
692 vp8_dbg_color_ref_frame = flags;
694 } else if (arg_match(&arg, &pp_disp_mb_modes, argi)) {
695 unsigned int flags = arg_parse_int(&arg);
696 if (flags) {
697 postproc = 1;
698 vp8_dbg_color_mb_modes = flags;
700 } else if (arg_match(&arg, &pp_disp_b_modes, argi)) {
701 unsigned int flags = arg_parse_int(&arg);
702 if (flags) {
703 postproc = 1;
704 vp8_dbg_color_b_modes = flags;
706 } else if (arg_match(&arg, &pp_disp_mvs, argi)) {
707 unsigned int flags = arg_parse_int(&arg);
708 if (flags) {
709 postproc = 1;
710 vp8_dbg_display_mv = flags;
712 } else if (arg_match(&arg, &error_concealment, argi)) {
713 ec_enabled = 1;
715 #endif // CONFIG_VP8_DECODER
716 else
717 argj++;
720 /* Check for unrecognized options */
721 for (argi = argv; *argi; argi++)
722 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
723 die("Error: Unrecognized option %s\n", *argi);
725 /* Handle non-option arguments */
726 fn = argv[0];
728 if (!fn) {
729 free(argv);
730 usage_exit();
732 /* Open file */
733 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
735 if (!infile) {
736 fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
738 #if CONFIG_OS_SUPPORT
739 /* Make sure we don't dump to the terminal, unless forced to with -o - */
740 if (!outfile_pattern && isatty(fileno(stdout)) && !do_md5 && !noblit) {
741 fprintf(stderr,
742 "Not dumping raw video to your terminal. Use '-o -' to "
743 "override.\n");
744 return EXIT_FAILURE;
746 #endif
747 input.vpx_input_ctx->file = infile;
748 if (file_is_ivf(input.vpx_input_ctx))
749 input.vpx_input_ctx->file_type = FILE_TYPE_IVF;
750 #if CONFIG_WEBM_IO
751 else if (file_is_webm(input.webm_ctx, input.vpx_input_ctx))
752 input.vpx_input_ctx->file_type = FILE_TYPE_WEBM;
753 #endif
754 else if (file_is_raw(input.vpx_input_ctx))
755 input.vpx_input_ctx->file_type = FILE_TYPE_RAW;
756 else {
757 fprintf(stderr, "Unrecognized input file type.\n");
758 #if !CONFIG_WEBM_IO
759 fprintf(stderr, "vpxdec was built without WebM container support.\n");
760 #endif
761 return EXIT_FAILURE;
764 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
765 single_file = is_single_file(outfile_pattern);
767 if (!noblit && single_file) {
768 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
769 vpx_input_ctx.width, vpx_input_ctx.height, 0);
770 if (do_md5)
771 MD5Init(&md5_ctx);
772 else
773 outfile = open_outfile(outfile_name);
776 if (use_y4m && !noblit) {
777 if (!single_file) {
778 fprintf(stderr, "YUV4MPEG2 not supported with output patterns,"
779 " try --i420 or --yv12 or --rawvideo.\n");
780 return EXIT_FAILURE;
783 #if CONFIG_WEBM_IO
784 if (vpx_input_ctx.file_type == FILE_TYPE_WEBM) {
785 if (webm_guess_framerate(input.webm_ctx, input.vpx_input_ctx)) {
786 fprintf(stderr, "Failed to guess framerate -- error parsing "
787 "webm file?\n");
788 return EXIT_FAILURE;
791 #endif
794 fourcc_interface = get_vpx_decoder_by_fourcc(vpx_input_ctx.fourcc);
795 if (interface && fourcc_interface && interface != fourcc_interface)
796 warn("Header indicates codec: %s\n", fourcc_interface->name);
797 else
798 interface = fourcc_interface;
800 if (!interface)
801 interface = get_vpx_decoder_by_index(0);
803 dec_flags = (postproc ? VPX_CODEC_USE_POSTPROC : 0) |
804 (ec_enabled ? VPX_CODEC_USE_ERROR_CONCEALMENT : 0) |
805 (frame_parallel ? VPX_CODEC_USE_FRAME_THREADING : 0);
806 if (vpx_codec_dec_init(&decoder, interface->codec_interface(),
807 &cfg, dec_flags)) {
808 fprintf(stderr, "Failed to initialize decoder: %s\n",
809 vpx_codec_error(&decoder));
810 return EXIT_FAILURE;
813 if (!quiet)
814 fprintf(stderr, "%s\n", decoder.name);
816 #if CONFIG_VP8_DECODER
817 if (vp8_pp_cfg.post_proc_flag
818 && vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg)) {
819 fprintf(stderr, "Failed to configure postproc: %s\n",
820 vpx_codec_error(&decoder));
821 return EXIT_FAILURE;
824 if (vp8_dbg_color_ref_frame
825 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_REF_FRAME,
826 vp8_dbg_color_ref_frame)) {
827 fprintf(stderr, "Failed to configure reference block visualizer: %s\n",
828 vpx_codec_error(&decoder));
829 return EXIT_FAILURE;
832 if (vp8_dbg_color_mb_modes
833 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_MB_MODES,
834 vp8_dbg_color_mb_modes)) {
835 fprintf(stderr, "Failed to configure macro block visualizer: %s\n",
836 vpx_codec_error(&decoder));
837 return EXIT_FAILURE;
840 if (vp8_dbg_color_b_modes
841 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_B_MODES,
842 vp8_dbg_color_b_modes)) {
843 fprintf(stderr, "Failed to configure block visualizer: %s\n",
844 vpx_codec_error(&decoder));
845 return EXIT_FAILURE;
848 if (vp8_dbg_display_mv
849 && vpx_codec_control(&decoder, VP8_SET_DBG_DISPLAY_MV,
850 vp8_dbg_display_mv)) {
851 fprintf(stderr, "Failed to configure motion vector visualizer: %s\n",
852 vpx_codec_error(&decoder));
853 return EXIT_FAILURE;
855 #endif
858 if (arg_skip)
859 fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
860 while (arg_skip) {
861 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size))
862 break;
863 arg_skip--;
866 if (num_external_frame_buffers > 0) {
867 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
868 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
869 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
870 if (vpx_codec_set_frame_buffer_functions(
871 &decoder, get_vp9_frame_buffer, release_vp9_frame_buffer,
872 &ext_fb_list)) {
873 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
874 vpx_codec_error(&decoder));
875 return EXIT_FAILURE;
879 frame_avail = 1;
880 got_data = 0;
882 /* Decode file */
883 while (frame_avail || got_data) {
884 vpx_codec_iter_t iter = NULL;
885 vpx_image_t *img;
886 struct vpx_usec_timer timer;
887 int corrupted = 0;
889 frame_avail = 0;
890 if (!stop_after || frame_in < stop_after) {
891 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
892 frame_avail = 1;
893 frame_in++;
895 vpx_usec_timer_start(&timer);
897 if (vpx_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer,
898 NULL, 0)) {
899 const char *detail = vpx_codec_error_detail(&decoder);
900 warn("Failed to decode frame %d: %s",
901 frame_in, vpx_codec_error(&decoder));
903 if (detail)
904 warn("Additional information: %s", detail);
905 if (!keep_going)
906 goto fail;
909 vpx_usec_timer_mark(&timer);
910 dx_time += vpx_usec_timer_elapsed(&timer);
911 } else {
912 flush_decoder = 1;
914 } else {
915 flush_decoder = 1;
918 vpx_usec_timer_start(&timer);
920 if (flush_decoder) {
921 // Flush the decoder in frame parallel decode.
922 if (vpx_codec_decode(&decoder, NULL, 0, NULL, 0)) {
923 warn("Failed to flush decoder: %s", vpx_codec_error(&decoder));
927 got_data = 0;
928 if ((img = vpx_codec_get_frame(&decoder, &iter))) {
929 ++frame_out;
930 got_data = 1;
933 vpx_usec_timer_mark(&timer);
934 dx_time += (unsigned int)vpx_usec_timer_elapsed(&timer);
936 if (!frame_parallel &&
937 vpx_codec_control(&decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted)) {
938 warn("Failed VP8_GET_FRAME_CORRUPTED: %s", vpx_codec_error(&decoder));
939 if (!keep_going)
940 goto fail;
942 frames_corrupted += corrupted;
944 if (progress)
945 show_progress(frame_in, frame_out, dx_time);
947 if (!noblit && img) {
948 const int PLANES_YUV[] = {VPX_PLANE_Y, VPX_PLANE_U, VPX_PLANE_V};
949 const int PLANES_YVU[] = {VPX_PLANE_Y, VPX_PLANE_V, VPX_PLANE_U};
950 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
952 if (do_scale) {
953 if (frame_out == 1) {
954 // If the output frames are to be scaled to a fixed display size then
955 // use the width and height specified in the container. If either of
956 // these is set to 0, use the display size set in the first frame
957 // header. If that is unavailable, use the raw decoded size of the
958 // first decoded frame.
959 int display_width = vpx_input_ctx.width;
960 int display_height = vpx_input_ctx.height;
961 if (!display_width || !display_height) {
962 int display_size[2];
963 if (vpx_codec_control(&decoder, VP9D_GET_DISPLAY_SIZE,
964 display_size)) {
965 // As last resort use size of first frame as display size.
966 display_width = img->d_w;
967 display_height = img->d_h;
968 } else {
969 display_width = display_size[0];
970 display_height = display_size[1];
973 scaled_img = vpx_img_alloc(NULL, img->fmt, display_width,
974 display_height, 16);
975 scaled_img->bit_depth = img->bit_depth;
978 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
979 #if CONFIG_LIBYUV
980 libyuv_scale(img, scaled_img, kFilterBox);
981 img = scaled_img;
982 #else
983 fprintf(stderr, "Failed to scale output frame: %s.\n"
984 "Scaling is disabled in this configuration. "
985 "To enable scaling, configure with --enable-libyuv\n",
986 vpx_codec_error(&decoder));
987 return EXIT_FAILURE;
988 #endif
991 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
992 // Default to codec bit depth if output bit depth not set
993 if (!output_bit_depth) {
994 output_bit_depth = img->bit_depth;
996 // Shift up or down if necessary
997 if (output_bit_depth != img->bit_depth) {
998 const vpx_img_fmt_t shifted_fmt = output_bit_depth == 8 ?
999 img->fmt ^ (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) :
1000 img->fmt | VPX_IMG_FMT_HIGHBITDEPTH;
1001 if (img_shifted &&
1002 img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
1003 vpx_img_free(img_shifted);
1004 img_shifted = NULL;
1006 if (!img_shifted) {
1007 img_shifted = vpx_img_alloc(NULL, shifted_fmt,
1008 img->d_w, img->d_h, 16);
1009 img_shifted->bit_depth = output_bit_depth;
1011 if (output_bit_depth > img->bit_depth) {
1012 vpx_img_upshift(img_shifted, img,
1013 output_bit_depth - img->bit_depth);
1014 } else {
1015 vpx_img_downshift(img_shifted, img,
1016 img->bit_depth - output_bit_depth);
1018 img = img_shifted;
1020 #endif
1022 if (single_file) {
1023 if (use_y4m) {
1024 char buf[Y4M_BUFFER_SIZE] = {0};
1025 size_t len = 0;
1026 if (img->fmt == VPX_IMG_FMT_I440 || img->fmt == VPX_IMG_FMT_I44016) {
1027 fprintf(stderr, "Cannot produce y4m output for 440 sampling.\n");
1028 goto fail;
1030 if (frame_out == 1) {
1031 // Y4M file header
1032 len = y4m_write_file_header(buf, sizeof(buf),
1033 vpx_input_ctx.width,
1034 vpx_input_ctx.height,
1035 &vpx_input_ctx.framerate,
1036 img->fmt, img->bit_depth);
1037 if (do_md5) {
1038 MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
1039 } else {
1040 fputs(buf, outfile);
1044 // Y4M frame header
1045 len = y4m_write_frame_header(buf, sizeof(buf));
1046 if (do_md5) {
1047 MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
1048 } else {
1049 fputs(buf, outfile);
1051 } else {
1052 if (frame_out == 1) {
1053 // Check if --yv12 or --i420 options are consistent with the
1054 // bit-stream decoded
1055 if (opt_i420) {
1056 if (img->fmt != VPX_IMG_FMT_I420 &&
1057 img->fmt != VPX_IMG_FMT_I42016) {
1058 fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
1059 goto fail;
1062 if (opt_yv12) {
1063 if ((img->fmt != VPX_IMG_FMT_I420 &&
1064 img->fmt != VPX_IMG_FMT_YV12) || img->bit_depth != 8) {
1065 fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
1066 goto fail;
1072 if (do_md5) {
1073 update_image_md5(img, planes, &md5_ctx);
1074 } else {
1075 write_image_file(img, planes, outfile);
1077 } else {
1078 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
1079 img->d_w, img->d_h, frame_in);
1080 if (do_md5) {
1081 MD5Init(&md5_ctx);
1082 update_image_md5(img, planes, &md5_ctx);
1083 MD5Final(md5_digest, &md5_ctx);
1084 print_md5(md5_digest, outfile_name);
1085 } else {
1086 outfile = open_outfile(outfile_name);
1087 write_image_file(img, planes, outfile);
1088 fclose(outfile);
1094 if (summary || progress) {
1095 show_progress(frame_in, frame_out, dx_time);
1096 fprintf(stderr, "\n");
1099 if (frames_corrupted)
1100 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
1102 fail:
1104 if (vpx_codec_destroy(&decoder)) {
1105 fprintf(stderr, "Failed to destroy decoder: %s\n",
1106 vpx_codec_error(&decoder));
1107 return EXIT_FAILURE;
1110 if (!noblit && single_file) {
1111 if (do_md5) {
1112 MD5Final(md5_digest, &md5_ctx);
1113 print_md5(md5_digest, outfile_name);
1114 } else {
1115 fclose(outfile);
1119 #if CONFIG_WEBM_IO
1120 if (input.vpx_input_ctx->file_type == FILE_TYPE_WEBM)
1121 webm_free(input.webm_ctx);
1122 #endif
1124 if (input.vpx_input_ctx->file_type != FILE_TYPE_WEBM)
1125 free(buf);
1127 if (scaled_img) vpx_img_free(scaled_img);
1128 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
1129 if (img_shifted) vpx_img_free(img_shifted);
1130 #endif
1132 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
1133 free(ext_fb_list.ext_fb[i].data);
1135 free(ext_fb_list.ext_fb);
1137 fclose(infile);
1138 free(argv);
1140 return frames_corrupted ? EXIT_FAILURE : EXIT_SUCCESS;
1143 int main(int argc, const char **argv_) {
1144 unsigned int loops = 1, i;
1145 char **argv, **argi, **argj;
1146 struct arg arg;
1147 int error = 0;
1149 argv = argv_dup(argc - 1, argv_ + 1);
1150 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1151 memset(&arg, 0, sizeof(arg));
1152 arg.argv_step = 1;
1154 if (arg_match(&arg, &looparg, argi)) {
1155 loops = arg_parse_uint(&arg);
1156 break;
1159 free(argv);
1160 for (i = 0; !error && i < loops; i++)
1161 error = main_loop(argc, argv_);
1162 return error;