]> granicus.if.org Git - libvpx/blob - vpxdec.c
Merge "Improve vp9_fdct4x4_sse2 (x1.2)"
[libvpx] / vpxdec.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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  */
10
11 #include <assert.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <limits.h>
17
18 #include "third_party/libyuv/include/libyuv/scale.h"
19
20 #include "./args.h"
21 #include "./ivfdec.h"
22
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"
27
28 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
29 #include "vpx/vp8dx.h"
30 #endif
31
32 #if CONFIG_MD5
33 #include "./md5_utils.h"
34 #endif
35
36 #include "./tools_common.h"
37 #include "./webmdec.h"
38
39 static const char *exec_name;
40
41 static const struct {
42   char const *name;
43   const vpx_codec_iface_t *(*iface)(void);
44   uint32_t fourcc;
45   uint32_t fourcc_mask;
46 } ifaces[] = {
47 #if CONFIG_VP8_DECODER
48   {"vp8",  vpx_codec_vp8_dx,   VP8_FOURCC_MASK, 0x00FFFFFF},
49 #endif
50 #if CONFIG_VP9_DECODER
51   {"vp9",  vpx_codec_vp9_dx,   VP9_FOURCC_MASK, 0x00FFFFFF},
52 #endif
53 };
54
55 struct VpxDecInputContext {
56   struct VpxInputContext *vpx_input_ctx;
57   struct WebmInputContext *webm_ctx;
58 };
59
60 static const arg_def_t looparg = ARG_DEF(NULL, "loops", 1,
61                                           "Number of times to decode the file");
62 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1,
63                                           "Codec to use");
64 static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0,
65                                           "Output raw YV12 frames");
66 static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0,
67                                           "Output raw I420 frames");
68 static const arg_def_t flipuvarg = ARG_DEF(NULL, "flipuv", 0,
69                                            "Flip the chroma planes in the output");
70 static const arg_def_t noblitarg = ARG_DEF(NULL, "noblit", 0,
71                                            "Don't process the decoded frames");
72 static const arg_def_t progressarg = ARG_DEF(NULL, "progress", 0,
73                                              "Show progress after each frame decodes");
74 static const arg_def_t limitarg = ARG_DEF(NULL, "limit", 1,
75                                           "Stop decoding after n frames");
76 static const arg_def_t skiparg = ARG_DEF(NULL, "skip", 1,
77                                          "Skip the first n input frames");
78 static const arg_def_t postprocarg = ARG_DEF(NULL, "postproc", 0,
79                                              "Postprocess decoded frames");
80 static const arg_def_t summaryarg = ARG_DEF(NULL, "summary", 0,
81                                             "Show timing summary");
82 static const arg_def_t outputfile = ARG_DEF("o", "output", 1,
83                                             "Output file name pattern (see below)");
84 static const arg_def_t threadsarg = ARG_DEF("t", "threads", 1,
85                                             "Max threads to use");
86 static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0,
87                                             "Show version string");
88 static const arg_def_t error_concealment = ARG_DEF(NULL, "error-concealment", 0,
89                                                    "Enable decoder error-concealment");
90 static const arg_def_t scalearg = ARG_DEF("S", "scale", 0,
91                                             "Scale output frames uniformly");
92
93
94 #if CONFIG_MD5
95 static const arg_def_t md5arg = ARG_DEF(NULL, "md5", 0,
96                                         "Compute the MD5 sum of the decoded frame");
97 #endif
98 static const arg_def_t *all_args[] = {
99   &codecarg, &use_yv12, &use_i420, &flipuvarg, &noblitarg,
100   &progressarg, &limitarg, &skiparg, &postprocarg, &summaryarg, &outputfile,
101   &threadsarg, &verbosearg, &scalearg,
102 #if CONFIG_MD5
103   &md5arg,
104 #endif
105   &error_concealment,
106   NULL
107 };
108
109 #if CONFIG_VP8_DECODER
110 static const arg_def_t addnoise_level = ARG_DEF(NULL, "noise-level", 1,
111                                                 "Enable VP8 postproc add noise");
112 static const arg_def_t deblock = ARG_DEF(NULL, "deblock", 0,
113                                          "Enable VP8 deblocking");
114 static const arg_def_t demacroblock_level = ARG_DEF(NULL, "demacroblock-level", 1,
115                                                     "Enable VP8 demacroblocking, w/ level");
116 static const arg_def_t pp_debug_info = ARG_DEF(NULL, "pp-debug-info", 1,
117                                                "Enable VP8 visible debug info");
118 static const arg_def_t pp_disp_ref_frame = ARG_DEF(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(NULL, "pp-dbg-mb-modes", 1,
121                                                   "Display only selected macro block modes");
122 static const arg_def_t pp_disp_b_modes = ARG_DEF(NULL, "pp-dbg-b-modes", 1,
123                                                  "Display only selected block modes");
124 static const arg_def_t pp_disp_mvs = ARG_DEF(NULL, "pp-dbg-mvs", 1,
125                                              "Draw only selected motion vectors");
126 static const arg_def_t mfqe = ARG_DEF(NULL, "mfqe", 0,
127                                       "Enable multiframe quality enhancement");
128
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
133 };
134 #endif
135
136 void usage_exit() {
137   int i;
138
139   fprintf(stderr, "Usage: %s <options> filename\n\n"
140           "Options:\n", exec_name);
141   arg_show_usage(stderr, all_args);
142 #if CONFIG_VP8_DECODER
143   fprintf(stderr, "\nVP8 Postprocessing Options:\n");
144   arg_show_usage(stderr, vp8_pp_args);
145 #endif
146   fprintf(stderr,
147           "\nOutput File Patterns:\n\n"
148           "  The -o argument specifies the name of the file(s) to "
149           "write to. If the\n  argument does not include any escape "
150           "characters, the output will be\n  written to a single file. "
151           "Otherwise, the filename will be calculated by\n  expanding "
152           "the following escape characters:\n");
153   fprintf(stderr,
154           "\n\t%%w   - Frame width"
155           "\n\t%%h   - Frame height"
156           "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
157           "\n\n  Pattern arguments are only supported in conjunction "
158           "with the --yv12 and\n  --i420 options. If the -o option is "
159           "not specified, the output will be\n  directed to stdout.\n"
160          );
161   fprintf(stderr, "\nIncluded decoders:\n\n");
162
163   for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
164     fprintf(stderr, "    %-6s - %s\n",
165             ifaces[i].name,
166             vpx_codec_iface_name(ifaces[i].iface()));
167
168   exit(EXIT_FAILURE);
169 }
170
171 static int read_frame(struct VpxDecInputContext *input,
172                       uint8_t **buf,
173                       size_t *bytes_in_buffer,
174                       size_t *buffer_size) {
175   char raw_hdr[RAW_FRAME_HDR_SZ];
176   size_t bytes_to_read = 0;
177   FILE *infile = input->vpx_input_ctx->file;
178   enum VideoFileType kind = input->vpx_input_ctx->file_type;
179   if (kind == FILE_TYPE_WEBM) {
180     return webm_read_frame(input->webm_ctx,
181                            buf, bytes_in_buffer, buffer_size);
182   } else if (kind == FILE_TYPE_RAW) {
183     if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
184       if (!feof(infile))
185         warn("Failed to read RAW frame size\n");
186     } else {
187       const int kCorruptFrameThreshold = 256 * 1024 * 1024;
188       const int kFrameTooSmallThreshold = 256 * 1024;
189       bytes_to_read = mem_get_le32(raw_hdr);
190
191       if (bytes_to_read > kCorruptFrameThreshold) {
192         warn("Read invalid frame size (%u)\n", (unsigned int)bytes_to_read);
193         bytes_to_read = 0;
194       }
195
196       if (kind == FILE_TYPE_RAW && bytes_to_read < kFrameTooSmallThreshold) {
197         warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
198              (unsigned int)bytes_to_read);
199       }
200
201       if (bytes_to_read > *buffer_size) {
202         uint8_t *new_buf = realloc(*buf, 2 * bytes_to_read);
203
204         if (new_buf) {
205           *buf = new_buf;
206           *buffer_size = 2 * bytes_to_read;
207         } else {
208           warn("Failed to allocate compressed data buffer\n");
209           bytes_to_read = 0;
210         }
211       }
212     }
213
214     if (!feof(infile)) {
215       if (fread(*buf, 1, bytes_to_read, infile) != bytes_to_read) {
216         warn("Failed to read full frame\n");
217         return 1;
218       }
219       *bytes_in_buffer = bytes_to_read;
220     }
221
222     return 0;
223   } else if (kind == FILE_TYPE_IVF) {
224     return ivf_read_frame(input->vpx_input_ctx,
225                           buf, bytes_in_buffer, buffer_size);
226   }
227
228   return 1;
229 }
230
231 void *out_open(const char *out_fn, int do_md5) {
232   void *out = NULL;
233
234   if (do_md5) {
235 #if CONFIG_MD5
236     MD5Context *md5_ctx = out = malloc(sizeof(MD5Context));
237     (void)out_fn;
238     MD5Init(md5_ctx);
239 #endif
240   } else {
241     FILE *outfile = out = strcmp("-", out_fn) ? fopen(out_fn, "wb")
242                           : set_binary_mode(stdout);
243
244     if (!outfile) {
245       fatal("Failed to output file");
246     }
247   }
248
249   return out;
250 }
251
252 void out_put(void *out, const uint8_t *buf, unsigned int len, int do_md5) {
253   if (do_md5) {
254 #if CONFIG_MD5
255     MD5Update(out, buf, len);
256 #endif
257   } else {
258     (void) fwrite(buf, 1, len, out);
259   }
260 }
261
262 void out_close(void *out, const char *out_fn, int do_md5) {
263   if (do_md5) {
264 #if CONFIG_MD5
265     uint8_t md5[16];
266     int i;
267
268     MD5Final(md5, out);
269     free(out);
270
271     for (i = 0; i < 16; i++)
272       printf("%02x", md5[i]);
273
274     printf("  %s\n", out_fn);
275 #endif
276   } else {
277     fclose(out);
278   }
279 }
280
281 int file_is_raw(struct VpxInputContext *input) {
282   uint8_t buf[32];
283   int is_raw = 0;
284   vpx_codec_stream_info_t si;
285
286   si.sz = sizeof(si);
287
288   if (fread(buf, 1, 32, input->file) == 32) {
289     int i;
290
291     if (mem_get_le32(buf) < 256 * 1024 * 1024) {
292       for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++) {
293         if (!vpx_codec_peek_stream_info(ifaces[i].iface(),
294                                         buf + 4, 32 - 4, &si)) {
295           is_raw = 1;
296           input->fourcc = ifaces[i].fourcc;
297           input->width = si.w;
298           input->height = si.h;
299           input->framerate.numerator = 30;
300           input->framerate.denominator = 1;
301           break;
302         }
303       }
304     }
305   }
306
307   rewind(input->file);
308   return is_raw;
309 }
310
311 void show_progress(int frame_in, int frame_out, unsigned long dx_time) {
312   fprintf(stderr, "%d decoded frames/%d showed frames in %lu us (%.2f fps)\r",
313           frame_in, frame_out, dx_time,
314           (float)frame_out * 1000000.0 / (float)dx_time);
315 }
316
317 void generate_filename(const char *pattern, char *out, size_t q_len,
318                        unsigned int d_w, unsigned int d_h,
319                        unsigned int frame_in) {
320   const char *p = pattern;
321   char *q = out;
322
323   do {
324     char *next_pat = strchr(p, '%');
325
326     if (p == next_pat) {
327       size_t pat_len;
328
329       /* parse the pattern */
330       q[q_len - 1] = '\0';
331       switch (p[1]) {
332         case 'w':
333           snprintf(q, q_len - 1, "%d", d_w);
334           break;
335         case 'h':
336           snprintf(q, q_len - 1, "%d", d_h);
337           break;
338         case '1':
339           snprintf(q, q_len - 1, "%d", frame_in);
340           break;
341         case '2':
342           snprintf(q, q_len - 1, "%02d", frame_in);
343           break;
344         case '3':
345           snprintf(q, q_len - 1, "%03d", frame_in);
346           break;
347         case '4':
348           snprintf(q, q_len - 1, "%04d", frame_in);
349           break;
350         case '5':
351           snprintf(q, q_len - 1, "%05d", frame_in);
352           break;
353         case '6':
354           snprintf(q, q_len - 1, "%06d", frame_in);
355           break;
356         case '7':
357           snprintf(q, q_len - 1, "%07d", frame_in);
358           break;
359         case '8':
360           snprintf(q, q_len - 1, "%08d", frame_in);
361           break;
362         case '9':
363           snprintf(q, q_len - 1, "%09d", frame_in);
364           break;
365         default:
366           die("Unrecognized pattern %%%c\n", p[1]);
367       }
368
369       pat_len = strlen(q);
370       if (pat_len >= q_len - 1)
371         die("Output filename too long.\n");
372       q += pat_len;
373       p += 2;
374       q_len -= pat_len;
375     } else {
376       size_t copy_len;
377
378       /* copy the next segment */
379       if (!next_pat)
380         copy_len = strlen(p);
381       else
382         copy_len = next_pat - p;
383
384       if (copy_len >= q_len - 1)
385         die("Output filename too long.\n");
386
387       memcpy(q, p, copy_len);
388       q[copy_len] = '\0';
389       q += copy_len;
390       p += copy_len;
391       q_len -= copy_len;
392     }
393   } while (*p);
394 }
395
396
397 int main_loop(int argc, const char **argv_) {
398   vpx_codec_ctx_t       decoder;
399   char                  *fn = NULL;
400   int                    i;
401   uint8_t               *buf = NULL;
402   size_t                 bytes_in_buffer = 0, buffer_size = 0;
403   FILE                  *infile;
404   int                    frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
405   int                    do_md5 = 0, progress = 0;
406   int                    stop_after = 0, postproc = 0, summary = 0, quiet = 1;
407   int                    arg_skip = 0;
408   int                    ec_enabled = 0;
409   vpx_codec_iface_t       *iface = NULL;
410   unsigned long          dx_time = 0;
411   struct arg               arg;
412   char                   **argv, **argi, **argj;
413   const char             *outfile_pattern = 0;
414   char                    outfile[PATH_MAX];
415   int                     single_file;
416   int                     use_y4m = 1;
417   void                   *out = NULL;
418   vpx_codec_dec_cfg_t     cfg = {0};
419 #if CONFIG_VP8_DECODER
420   vp8_postproc_cfg_t      vp8_pp_cfg = {0};
421   int                     vp8_dbg_color_ref_frame = 0;
422   int                     vp8_dbg_color_mb_modes = 0;
423   int                     vp8_dbg_color_b_modes = 0;
424   int                     vp8_dbg_display_mv = 0;
425 #endif
426   int                     frames_corrupted = 0;
427   int                     dec_flags = 0;
428   int                     do_scale = 0;
429   int                     stream_w = 0, stream_h = 0;
430   vpx_image_t             *scaled_img = NULL;
431   int                     frame_avail, got_data;
432
433   struct VpxDecInputContext input = {0};
434   struct VpxInputContext vpx_input_ctx = {0};
435   struct WebmInputContext webm_ctx = {0};
436   input.vpx_input_ctx = &vpx_input_ctx;
437   input.webm_ctx = &webm_ctx;
438
439   /* Parse command line */
440   exec_name = argv_[0];
441   argv = argv_dup(argc - 1, argv_ + 1);
442
443   for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
444     memset(&arg, 0, sizeof(arg));
445     arg.argv_step = 1;
446
447     if (arg_match(&arg, &codecarg, argi)) {
448       int j, k = -1;
449
450       for (j = 0; j < sizeof(ifaces) / sizeof(ifaces[0]); j++)
451         if (!strcmp(ifaces[j].name, arg.val))
452           k = j;
453
454       if (k >= 0)
455         iface = ifaces[k].iface();
456       else
457         die("Error: Unrecognized argument (%s) to --codec\n",
458             arg.val);
459     } else if (arg_match(&arg, &looparg, argi)) {
460       // no-op
461     } else if (arg_match(&arg, &outputfile, argi))
462       outfile_pattern = arg.val;
463     else if (arg_match(&arg, &use_yv12, argi)) {
464       use_y4m = 0;
465       flipuv = 1;
466     } else if (arg_match(&arg, &use_i420, argi)) {
467       use_y4m = 0;
468       flipuv = 0;
469     } else if (arg_match(&arg, &flipuvarg, argi))
470       flipuv = 1;
471     else if (arg_match(&arg, &noblitarg, argi))
472       noblit = 1;
473     else if (arg_match(&arg, &progressarg, argi))
474       progress = 1;
475     else if (arg_match(&arg, &limitarg, argi))
476       stop_after = arg_parse_uint(&arg);
477     else if (arg_match(&arg, &skiparg, argi))
478       arg_skip = arg_parse_uint(&arg);
479     else if (arg_match(&arg, &postprocarg, argi))
480       postproc = 1;
481     else if (arg_match(&arg, &md5arg, argi))
482       do_md5 = 1;
483     else if (arg_match(&arg, &summaryarg, argi))
484       summary = 1;
485     else if (arg_match(&arg, &threadsarg, argi))
486       cfg.threads = arg_parse_uint(&arg);
487     else if (arg_match(&arg, &verbosearg, argi))
488       quiet = 0;
489     else if (arg_match(&arg, &scalearg, argi))
490       do_scale = 1;
491
492 #if CONFIG_VP8_DECODER
493     else if (arg_match(&arg, &addnoise_level, argi)) {
494       postproc = 1;
495       vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE;
496       vp8_pp_cfg.noise_level = arg_parse_uint(&arg);
497     } else if (arg_match(&arg, &demacroblock_level, argi)) {
498       postproc = 1;
499       vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK;
500       vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg);
501     } else if (arg_match(&arg, &deblock, argi)) {
502       postproc = 1;
503       vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK;
504     } else if (arg_match(&arg, &mfqe, argi)) {
505       postproc = 1;
506       vp8_pp_cfg.post_proc_flag |= VP8_MFQE;
507     } else if (arg_match(&arg, &pp_debug_info, argi)) {
508       unsigned int level = arg_parse_uint(&arg);
509
510       postproc = 1;
511       vp8_pp_cfg.post_proc_flag &= ~0x7;
512
513       if (level)
514         vp8_pp_cfg.post_proc_flag |= level;
515     } else if (arg_match(&arg, &pp_disp_ref_frame, argi)) {
516       unsigned int flags = arg_parse_int(&arg);
517       if (flags) {
518         postproc = 1;
519         vp8_dbg_color_ref_frame = flags;
520       }
521     } else if (arg_match(&arg, &pp_disp_mb_modes, argi)) {
522       unsigned int flags = arg_parse_int(&arg);
523       if (flags) {
524         postproc = 1;
525         vp8_dbg_color_mb_modes = flags;
526       }
527     } else if (arg_match(&arg, &pp_disp_b_modes, argi)) {
528       unsigned int flags = arg_parse_int(&arg);
529       if (flags) {
530         postproc = 1;
531         vp8_dbg_color_b_modes = flags;
532       }
533     } else if (arg_match(&arg, &pp_disp_mvs, argi)) {
534       unsigned int flags = arg_parse_int(&arg);
535       if (flags) {
536         postproc = 1;
537         vp8_dbg_display_mv = flags;
538       }
539     } else if (arg_match(&arg, &error_concealment, argi)) {
540       ec_enabled = 1;
541     }
542
543 #endif
544     else
545       argj++;
546   }
547
548   /* Check for unrecognized options */
549   for (argi = argv; *argi; argi++)
550     if (argi[0][0] == '-' && strlen(argi[0]) > 1)
551       die("Error: Unrecognized option %s\n", *argi);
552
553   /* Handle non-option arguments */
554   fn = argv[0];
555
556   if (!fn)
557     usage_exit();
558
559   /* Open file */
560   infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
561
562   if (!infile) {
563     fprintf(stderr, "Failed to open file '%s'",
564             strcmp(fn, "-") ? fn : "stdin");
565     return EXIT_FAILURE;
566   }
567 #if CONFIG_OS_SUPPORT
568   /* Make sure we don't dump to the terminal, unless forced to with -o - */
569   if (!outfile_pattern && isatty(fileno(stdout)) && !do_md5 && !noblit) {
570     fprintf(stderr,
571             "Not dumping raw video to your terminal. Use '-o -' to "
572             "override.\n");
573     return EXIT_FAILURE;
574   }
575 #endif
576   input.vpx_input_ctx->file = infile;
577   if (file_is_ivf(input.vpx_input_ctx))
578     input.vpx_input_ctx->file_type = FILE_TYPE_IVF;
579   else if (file_is_webm(input.webm_ctx, input.vpx_input_ctx))
580     input.vpx_input_ctx->file_type = FILE_TYPE_WEBM;
581   else if (file_is_raw(input.vpx_input_ctx))
582     input.vpx_input_ctx->file_type = FILE_TYPE_RAW;
583   else {
584     fprintf(stderr, "Unrecognized input file type.\n");
585     return EXIT_FAILURE;
586   }
587
588   /* If the output file is not set or doesn't have a sequence number in
589    * it, then we only open it once.
590    */
591   outfile_pattern = outfile_pattern ? outfile_pattern : "-";
592   single_file = 1;
593   {
594     const char *p = outfile_pattern;
595     do {
596       p = strchr(p, '%');
597       if (p && p[1] >= '1' && p[1] <= '9') {
598         /* pattern contains sequence number, so it's not unique. */
599         single_file = 0;
600         break;
601       }
602       if (p)
603         p++;
604     } while (p);
605   }
606
607   if (single_file && !noblit) {
608     generate_filename(outfile_pattern, outfile, sizeof(outfile) - 1,
609                       vpx_input_ctx.width, vpx_input_ctx.height, 0);
610     out = out_open(outfile, do_md5);
611   }
612
613   if (use_y4m && !noblit) {
614     char buffer[128];
615
616     if (!single_file) {
617       fprintf(stderr, "YUV4MPEG2 not supported with output patterns,"
618               " try --i420 or --yv12.\n");
619       return EXIT_FAILURE;
620     }
621
622     if (vpx_input_ctx.file_type == FILE_TYPE_WEBM)
623       if (webm_guess_framerate(input.webm_ctx, input.vpx_input_ctx)) {
624         fprintf(stderr, "Failed to guess framerate -- error parsing "
625                 "webm file?\n");
626         return EXIT_FAILURE;
627       }
628
629
630     /*Note: We can't output an aspect ratio here because IVF doesn't
631        store one, and neither does VP8.
632       That will have to wait until these tools support WebM natively.*/
633     snprintf(buffer, sizeof(buffer), "YUV4MPEG2 W%u H%u F%u:%u I%c ",
634              vpx_input_ctx.width, vpx_input_ctx.height,
635              vpx_input_ctx.framerate.numerator,
636              vpx_input_ctx.framerate.denominator,
637              'p');
638     out_put(out, (unsigned char *)buffer,
639             (unsigned int)strlen(buffer), do_md5);
640   }
641
642   /* Try to determine the codec from the fourcc. */
643   for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
644     if ((vpx_input_ctx.fourcc & ifaces[i].fourcc_mask) == ifaces[i].fourcc) {
645       vpx_codec_iface_t *vpx_iface = ifaces[i].iface();
646
647       if (iface && iface != vpx_iface)
648         warn("Header indicates codec: %s\n", ifaces[i].name);
649       else
650         iface = vpx_iface;
651
652       break;
653     }
654
655   dec_flags = (postproc ? VPX_CODEC_USE_POSTPROC : 0) |
656               (ec_enabled ? VPX_CODEC_USE_ERROR_CONCEALMENT : 0);
657   if (vpx_codec_dec_init(&decoder, iface ? iface :  ifaces[0].iface(), &cfg,
658                          dec_flags)) {
659     fprintf(stderr, "Failed to initialize decoder: %s\n", vpx_codec_error(&decoder));
660     return EXIT_FAILURE;
661   }
662
663   if (!quiet)
664     fprintf(stderr, "%s\n", decoder.name);
665
666 #if CONFIG_VP8_DECODER
667
668   if (vp8_pp_cfg.post_proc_flag
669       && vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg)) {
670     fprintf(stderr, "Failed to configure postproc: %s\n", vpx_codec_error(&decoder));
671     return EXIT_FAILURE;
672   }
673
674   if (vp8_dbg_color_ref_frame
675       && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_REF_FRAME, vp8_dbg_color_ref_frame)) {
676     fprintf(stderr, "Failed to configure reference block visualizer: %s\n", vpx_codec_error(&decoder));
677     return EXIT_FAILURE;
678   }
679
680   if (vp8_dbg_color_mb_modes
681       && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_MB_MODES, vp8_dbg_color_mb_modes)) {
682     fprintf(stderr, "Failed to configure macro block visualizer: %s\n", vpx_codec_error(&decoder));
683     return EXIT_FAILURE;
684   }
685
686   if (vp8_dbg_color_b_modes
687       && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_B_MODES, vp8_dbg_color_b_modes)) {
688     fprintf(stderr, "Failed to configure block visualizer: %s\n", vpx_codec_error(&decoder));
689     return EXIT_FAILURE;
690   }
691
692   if (vp8_dbg_display_mv
693       && vpx_codec_control(&decoder, VP8_SET_DBG_DISPLAY_MV, vp8_dbg_display_mv)) {
694     fprintf(stderr, "Failed to configure motion vector visualizer: %s\n", vpx_codec_error(&decoder));
695     return EXIT_FAILURE;
696   }
697 #endif
698
699
700   if (arg_skip)
701     fprintf(stderr, "Skiping first %d frames.\n", arg_skip);
702   while (arg_skip) {
703     if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size))
704       break;
705     arg_skip--;
706   }
707
708   frame_avail = 1;
709   got_data = 0;
710
711   /* Decode file */
712   while (frame_avail || got_data) {
713     vpx_codec_iter_t  iter = NULL;
714     vpx_image_t    *img;
715     struct vpx_usec_timer timer;
716     int                   corrupted;
717
718     frame_avail = 0;
719     if (!stop_after || frame_in < stop_after) {
720       if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
721         frame_avail = 1;
722         frame_in++;
723
724         vpx_usec_timer_start(&timer);
725
726         if (vpx_codec_decode(&decoder, buf, bytes_in_buffer, NULL, 0)) {
727           const char *detail = vpx_codec_error_detail(&decoder);
728           warn("Failed to decode frame %d: %s",
729                frame_in, vpx_codec_error(&decoder));
730
731           if (detail)
732             warn("Additional information: %s", detail);
733           goto fail;
734         }
735
736         vpx_usec_timer_mark(&timer);
737         dx_time += (unsigned int)vpx_usec_timer_elapsed(&timer);
738       }
739     }
740
741     vpx_usec_timer_start(&timer);
742
743     got_data = 0;
744     if ((img = vpx_codec_get_frame(&decoder, &iter))) {
745       ++frame_out;
746       got_data = 1;
747     }
748
749     vpx_usec_timer_mark(&timer);
750     dx_time += (unsigned int)vpx_usec_timer_elapsed(&timer);
751
752     if (vpx_codec_control(&decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted)) {
753       warn("Failed VP8_GET_FRAME_CORRUPTED: %s", vpx_codec_error(&decoder));
754       goto fail;
755     }
756     frames_corrupted += corrupted;
757
758     if (progress)
759       show_progress(frame_in, frame_out, dx_time);
760
761     if (!noblit) {
762       if (frame_out == 1 && img && use_y4m) {
763         /* Write out the color format to terminate the header line */
764         const char *color =
765             img->fmt == VPX_IMG_FMT_444A ? "C444alpha\n" :
766             img->fmt == VPX_IMG_FMT_I444 ? "C444\n" :
767             img->fmt == VPX_IMG_FMT_I422 ? "C422\n" :
768             "C420jpeg\n";
769
770         out_put(out, (const unsigned char*)color, strlen(color), do_md5);
771       }
772
773       if (do_scale) {
774         if (img && frame_out == 1) {
775           stream_w = img->d_w;
776           stream_h = img->d_h;
777           scaled_img = vpx_img_alloc(NULL, VPX_IMG_FMT_I420,
778                                      stream_w, stream_h, 16);
779         }
780         if (img && (img->d_w != stream_w || img->d_h != stream_h)) {
781           assert(img->fmt == VPX_IMG_FMT_I420);
782           I420Scale(img->planes[VPX_PLANE_Y], img->stride[VPX_PLANE_Y],
783                     img->planes[VPX_PLANE_U], img->stride[VPX_PLANE_U],
784                     img->planes[VPX_PLANE_V], img->stride[VPX_PLANE_V],
785                     img->d_w, img->d_h,
786                     scaled_img->planes[VPX_PLANE_Y],
787                     scaled_img->stride[VPX_PLANE_Y],
788                     scaled_img->planes[VPX_PLANE_U],
789                     scaled_img->stride[VPX_PLANE_U],
790                     scaled_img->planes[VPX_PLANE_V],
791                     scaled_img->stride[VPX_PLANE_V],
792                     stream_w, stream_h,
793                     kFilterBox);
794           img = scaled_img;
795         }
796       }
797
798       if (img) {
799         unsigned int y;
800         char out_fn[PATH_MAX];
801         uint8_t *buf;
802         unsigned int c_w =
803             img->x_chroma_shift ? (1 + img->d_w) >> img->x_chroma_shift
804                                 : img->d_w;
805         unsigned int c_h =
806             img->y_chroma_shift ? (1 + img->d_h) >> img->y_chroma_shift
807                                 : img->d_h;
808
809         if (!single_file) {
810           size_t len = sizeof(out_fn) - 1;
811
812           out_fn[len] = '\0';
813           generate_filename(outfile_pattern, out_fn, len - 1,
814                             img->d_w, img->d_h, frame_in);
815           out = out_open(out_fn, do_md5);
816         } else if (use_y4m)
817           out_put(out, (unsigned char *)"FRAME\n", 6, do_md5);
818
819         buf = img->planes[VPX_PLANE_Y];
820
821         for (y = 0; y < img->d_h; y++) {
822           out_put(out, buf, img->d_w, do_md5);
823           buf += img->stride[VPX_PLANE_Y];
824         }
825
826         buf = img->planes[flipuv ? VPX_PLANE_V : VPX_PLANE_U];
827
828         for (y = 0; y < c_h; y++) {
829           out_put(out, buf, c_w, do_md5);
830           buf += img->stride[VPX_PLANE_U];
831         }
832
833         buf = img->planes[flipuv ? VPX_PLANE_U : VPX_PLANE_V];
834
835         for (y = 0; y < c_h; y++) {
836           out_put(out, buf, c_w, do_md5);
837           buf += img->stride[VPX_PLANE_V];
838         }
839
840         if (!single_file)
841           out_close(out, out_fn, do_md5);
842       }
843     }
844
845     if (stop_after && frame_in >= stop_after)
846       break;
847   }
848
849   if (summary || progress) {
850     show_progress(frame_in, frame_out, dx_time);
851     fprintf(stderr, "\n");
852   }
853
854   if (frames_corrupted)
855     fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
856
857 fail:
858
859   if (vpx_codec_destroy(&decoder)) {
860     fprintf(stderr, "Failed to destroy decoder: %s\n", vpx_codec_error(&decoder));
861     return EXIT_FAILURE;
862   }
863
864   if (single_file && !noblit)
865     out_close(out, outfile, do_md5);
866
867   if (input.vpx_input_ctx->file_type == FILE_TYPE_WEBM)
868     webm_free(input.webm_ctx);
869   else
870     free(buf);
871
872   fclose(infile);
873   free(argv);
874
875   return frames_corrupted ? EXIT_FAILURE : EXIT_SUCCESS;
876 }
877
878 int main(int argc, const char **argv_) {
879   unsigned int loops = 1, i;
880   char **argv, **argi, **argj;
881   struct arg arg;
882   int error = 0;
883
884   argv = argv_dup(argc - 1, argv_ + 1);
885   for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
886     memset(&arg, 0, sizeof(arg));
887     arg.argv_step = 1;
888
889     if (arg_match(&arg, &looparg, argi)) {
890       loops = arg_parse_uint(&arg);
891       break;
892     }
893   }
894   free(argv);
895   for (i = 0; !error && i < loops; i++)
896     error = main_loop(argc, argv_);
897   return error;
898 }