Merge "Remove stack shadowing for x86-64"
[libvpx.git] / vpxdec.c
blob5eb0bebde659e655c954aa2d913d98b5c8c74765
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 */
12 /* This is a simple program that reads ivf files and decodes them
13 * using the new interface. Decoded frames are output as YV12 raw.
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stdarg.h>
18 #include <string.h>
19 #include <limits.h>
20 #if defined(_WIN32)
21 #include <io.h>
22 #define snprintf _snprintf
23 #define isatty _isatty
24 #define fileno _fileno
25 #else
26 #include <unistd.h>
27 #endif
28 #define VPX_CODEC_DISABLE_COMPAT 1
29 #include "vpx_config.h"
30 #include "vpx/vpx_decoder.h"
31 #include "vpx_ports/vpx_timer.h"
32 #if CONFIG_VP8_DECODER
33 #include "vpx/vp8dx.h"
34 #endif
35 #if CONFIG_MD5
36 #include "md5_utils.h"
37 #endif
38 #include "tools_common.h"
39 #include "nestegg/include/nestegg/nestegg.h"
41 #ifndef PATH_MAX
42 #define PATH_MAX 256
43 #endif
45 static const char *exec_name;
47 #define VP8_FOURCC (0x00385056)
48 static const struct
50 char const *name;
51 const vpx_codec_iface_t *iface;
52 unsigned int fourcc;
53 unsigned int fourcc_mask;
54 } ifaces[] =
56 #if CONFIG_VP8_DECODER
57 {"vp8", &vpx_codec_vp8_dx_algo, VP8_FOURCC, 0x00FFFFFF},
58 #endif
61 #include "args.h"
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 postprocarg = ARG_DEF(NULL, "postproc", 0,
77 "Postprocess decoded frames");
78 static const arg_def_t summaryarg = ARG_DEF(NULL, "summary", 0,
79 "Show timing summary");
80 static const arg_def_t outputfile = ARG_DEF("o", "output", 1,
81 "Output file name pattern (see below)");
82 static const arg_def_t threadsarg = ARG_DEF("t", "threads", 1,
83 "Max threads to use");
84 static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0,
85 "Show version string");
87 #if CONFIG_MD5
88 static const arg_def_t md5arg = ARG_DEF(NULL, "md5", 0,
89 "Compute the MD5 sum of the decoded frame");
90 #endif
91 static const arg_def_t *all_args[] =
93 &codecarg, &use_yv12, &use_i420, &flipuvarg, &noblitarg,
94 &progressarg, &limitarg, &postprocarg, &summaryarg, &outputfile,
95 &threadsarg, &verbosearg,
96 #if CONFIG_MD5
97 &md5arg,
98 #endif
99 NULL
102 #if CONFIG_VP8_DECODER
103 static const arg_def_t addnoise_level = ARG_DEF(NULL, "noise-level", 1,
104 "Enable VP8 postproc add noise");
105 static const arg_def_t deblock = ARG_DEF(NULL, "deblock", 0,
106 "Enable VP8 deblocking");
107 static const arg_def_t demacroblock_level = ARG_DEF(NULL, "demacroblock-level", 1,
108 "Enable VP8 demacroblocking, w/ level");
109 static const arg_def_t pp_debug_info = ARG_DEF(NULL, "pp-debug-info", 1,
110 "Enable VP8 visible debug info");
111 static const arg_def_t pp_disp_ref_frame = ARG_DEF(NULL, "pp-dbg-ref-frame", 1,
112 "Display only selected reference frame per macro block");
113 static const arg_def_t pp_disp_mb_modes = ARG_DEF(NULL, "pp-dbg-mb-modes", 1,
114 "Display only selected macro block modes");
115 static const arg_def_t pp_disp_b_modes = ARG_DEF(NULL, "pp-dbg-b-modes", 1,
116 "Display only selected block modes");
117 static const arg_def_t pp_disp_mvs = ARG_DEF(NULL, "pp-dbg-mvs", 1,
118 "Draw only selected motion vectors");
120 static const arg_def_t *vp8_pp_args[] =
122 &addnoise_level, &deblock, &demacroblock_level, &pp_debug_info,
123 &pp_disp_ref_frame, &pp_disp_mb_modes, &pp_disp_b_modes, &pp_disp_mvs,
124 NULL
126 #endif
128 static void usage_exit()
130 int i;
132 fprintf(stderr, "Usage: %s <options> filename\n\n"
133 "Options:\n", exec_name);
134 arg_show_usage(stderr, all_args);
135 #if CONFIG_VP8_DECODER
136 fprintf(stderr, "\nVP8 Postprocessing Options:\n");
137 arg_show_usage(stderr, vp8_pp_args);
138 #endif
139 fprintf(stderr,
140 "\nOutput File Patterns:\n\n"
141 " The -o argument specifies the name of the file(s) to "
142 "write to. If the\n argument does not include any escape "
143 "characters, the output will be\n written to a single file. "
144 "Otherwise, the filename will be calculated by\n expanding "
145 "the following escape characters:\n"
146 "\n\t%%w - Frame width"
147 "\n\t%%h - Frame height"
148 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
149 "\n\n Pattern arguments are only supported in conjunction "
150 "with the --yv12 and\n --i420 options. If the -o option is "
151 "not specified, the output will be\n directed to stdout.\n"
153 fprintf(stderr, "\nIncluded decoders:\n\n");
155 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
156 fprintf(stderr, " %-6s - %s\n",
157 ifaces[i].name,
158 vpx_codec_iface_name(ifaces[i].iface));
160 exit(EXIT_FAILURE);
163 void die(const char *fmt, ...)
165 va_list ap;
166 va_start(ap, fmt);
167 vfprintf(stderr, fmt, ap);
168 fprintf(stderr, "\n");
169 usage_exit();
172 static unsigned int mem_get_le16(const void *vmem)
174 unsigned int val;
175 const unsigned char *mem = (const unsigned char *)vmem;
177 val = mem[1] << 8;
178 val |= mem[0];
179 return val;
182 static unsigned int mem_get_le32(const void *vmem)
184 unsigned int val;
185 const unsigned char *mem = (const unsigned char *)vmem;
187 val = mem[3] << 24;
188 val |= mem[2] << 16;
189 val |= mem[1] << 8;
190 val |= mem[0];
191 return val;
194 enum file_kind
196 RAW_FILE,
197 IVF_FILE,
198 WEBM_FILE
201 struct input_ctx
203 enum file_kind kind;
204 FILE *infile;
205 nestegg *nestegg_ctx;
206 nestegg_packet *pkt;
207 unsigned int chunk;
208 unsigned int chunks;
209 unsigned int video_track;
212 #define IVF_FRAME_HDR_SZ (sizeof(uint32_t) + sizeof(uint64_t))
213 #define RAW_FRAME_HDR_SZ (sizeof(uint32_t))
214 static int read_frame(struct input_ctx *input,
215 uint8_t **buf,
216 size_t *buf_sz,
217 size_t *buf_alloc_sz)
219 char raw_hdr[IVF_FRAME_HDR_SZ];
220 size_t new_buf_sz;
221 FILE *infile = input->infile;
222 enum file_kind kind = input->kind;
223 if(kind == WEBM_FILE)
225 if(input->chunk >= input->chunks)
227 unsigned int track;
231 /* End of this packet, get another. */
232 if(input->pkt)
233 nestegg_free_packet(input->pkt);
235 if(nestegg_read_packet(input->nestegg_ctx, &input->pkt) <= 0
236 || nestegg_packet_track(input->pkt, &track))
237 return 1;
239 } while(track != input->video_track);
241 if(nestegg_packet_count(input->pkt, &input->chunks))
242 return 1;
243 input->chunk = 0;
246 if(nestegg_packet_data(input->pkt, input->chunk, buf, buf_sz))
247 return 1;
248 input->chunk++;
250 return 0;
252 /* For both the raw and ivf formats, the frame size is the first 4 bytes
253 * of the frame header. We just need to special case on the header
254 * size.
256 else if (fread(raw_hdr, kind==IVF_FILE
257 ? IVF_FRAME_HDR_SZ : RAW_FRAME_HDR_SZ, 1, infile) != 1)
259 if (!feof(infile))
260 fprintf(stderr, "Failed to read frame size\n");
262 new_buf_sz = 0;
264 else
266 new_buf_sz = mem_get_le32(raw_hdr);
268 if (new_buf_sz > 256 * 1024 * 1024)
270 fprintf(stderr, "Error: Read invalid frame size (%u)\n",
271 (unsigned int)new_buf_sz);
272 new_buf_sz = 0;
275 if (kind == RAW_FILE && new_buf_sz > 256 * 1024)
276 fprintf(stderr, "Warning: Read invalid frame size (%u)"
277 " - not a raw file?\n", (unsigned int)new_buf_sz);
279 if (new_buf_sz > *buf_alloc_sz)
281 uint8_t *new_buf = realloc(*buf, 2 * new_buf_sz);
283 if (new_buf)
285 *buf = new_buf;
286 *buf_alloc_sz = 2 * new_buf_sz;
288 else
290 fprintf(stderr, "Failed to allocate compressed data buffer\n");
291 new_buf_sz = 0;
296 *buf_sz = new_buf_sz;
298 if (*buf_sz)
300 if (fread(*buf, 1, *buf_sz, infile) != *buf_sz)
302 fprintf(stderr, "Failed to read full frame\n");
303 return 1;
306 return 0;
309 return 1;
312 void *out_open(const char *out_fn, int do_md5)
314 void *out = NULL;
316 if (do_md5)
318 #if CONFIG_MD5
319 MD5Context *md5_ctx = out = malloc(sizeof(MD5Context));
320 (void)out_fn;
321 MD5Init(md5_ctx);
322 #endif
324 else
326 FILE *outfile = out = strcmp("-", out_fn) ? fopen(out_fn, "wb")
327 : set_binary_mode(stdout);
329 if (!outfile)
331 fprintf(stderr, "Failed to output file");
332 exit(EXIT_FAILURE);
336 return out;
339 void out_put(void *out, const uint8_t *buf, unsigned int len, int do_md5)
341 if (do_md5)
343 #if CONFIG_MD5
344 MD5Update(out, buf, len);
345 #endif
347 else
349 if(fwrite(buf, 1, len, out));
353 void out_close(void *out, const char *out_fn, int do_md5)
355 if (do_md5)
357 #if CONFIG_MD5
358 uint8_t md5[16];
359 int i;
361 MD5Final(md5, out);
362 free(out);
364 for (i = 0; i < 16; i++)
365 printf("%02x", md5[i]);
367 printf(" %s\n", out_fn);
368 #endif
370 else
372 fclose(out);
376 unsigned int file_is_ivf(FILE *infile,
377 unsigned int *fourcc,
378 unsigned int *width,
379 unsigned int *height,
380 unsigned int *fps_den,
381 unsigned int *fps_num)
383 char raw_hdr[32];
384 int is_ivf = 0;
386 if (fread(raw_hdr, 1, 32, infile) == 32)
388 if (raw_hdr[0] == 'D' && raw_hdr[1] == 'K'
389 && raw_hdr[2] == 'I' && raw_hdr[3] == 'F')
391 is_ivf = 1;
393 if (mem_get_le16(raw_hdr + 4) != 0)
394 fprintf(stderr, "Error: Unrecognized IVF version! This file may not"
395 " decode properly.");
397 *fourcc = mem_get_le32(raw_hdr + 8);
398 *width = mem_get_le16(raw_hdr + 12);
399 *height = mem_get_le16(raw_hdr + 14);
400 *fps_num = mem_get_le32(raw_hdr + 16);
401 *fps_den = mem_get_le32(raw_hdr + 20);
403 /* Some versions of vpxenc used 1/(2*fps) for the timebase, so
404 * we can guess the framerate using only the timebase in this
405 * case. Other files would require reading ahead to guess the
406 * timebase, like we do for webm.
408 if(*fps_num < 1000)
410 /* Correct for the factor of 2 applied to the timebase in the
411 * encoder.
413 if(*fps_num&1)*fps_den<<=1;
414 else *fps_num>>=1;
416 else
418 /* Don't know FPS for sure, and don't have readahead code
419 * (yet?), so just default to 30fps.
421 *fps_num = 30;
422 *fps_den = 1;
427 if (!is_ivf)
428 rewind(infile);
430 return is_ivf;
434 unsigned int file_is_raw(FILE *infile,
435 unsigned int *fourcc,
436 unsigned int *width,
437 unsigned int *height,
438 unsigned int *fps_den,
439 unsigned int *fps_num)
441 unsigned char buf[32];
442 int is_raw = 0;
443 vpx_codec_stream_info_t si;
445 if (fread(buf, 1, 32, infile) == 32)
447 int i;
449 if(mem_get_le32(buf) < 256 * 1024 * 1024)
450 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
451 if(!vpx_codec_peek_stream_info(ifaces[i].iface,
452 buf + 4, 32 - 4, &si))
454 is_raw = 1;
455 *fourcc = ifaces[i].fourcc;
456 *width = si.w;
457 *height = si.h;
458 *fps_num = 30;
459 *fps_den = 1;
460 break;
464 rewind(infile);
465 return is_raw;
469 static int
470 nestegg_read_cb(void *buffer, size_t length, void *userdata)
472 FILE *f = userdata;
474 if(fread(buffer, 1, length, f) < length)
476 if (ferror(f))
477 return -1;
478 if (feof(f))
479 return 0;
481 return 1;
485 static int
486 nestegg_seek_cb(int64_t offset, int whence, void * userdata)
488 switch(whence) {
489 case NESTEGG_SEEK_SET: whence = SEEK_SET; break;
490 case NESTEGG_SEEK_CUR: whence = SEEK_CUR; break;
491 case NESTEGG_SEEK_END: whence = SEEK_END; break;
493 return fseek(userdata, offset, whence)? -1 : 0;
497 static int64_t
498 nestegg_tell_cb(void * userdata)
500 return ftell(userdata);
504 static void
505 nestegg_log_cb(nestegg * context, unsigned int severity, char const * format,
506 ...)
508 va_list ap;
510 va_start(ap, format);
511 vfprintf(stderr, format, ap);
512 fprintf(stderr, "\n");
513 va_end(ap);
517 static int
518 webm_guess_framerate(struct input_ctx *input,
519 unsigned int *fps_den,
520 unsigned int *fps_num)
522 unsigned int i;
523 uint64_t tstamp=0;
525 /* Guess the framerate. Read up to 1 second, or 50 video packets,
526 * whichever comes first.
528 for(i=0; tstamp < 1000000000 && i < 50;)
530 nestegg_packet * pkt;
531 unsigned int track;
533 if(nestegg_read_packet(input->nestegg_ctx, &pkt) <= 0)
534 break;
536 nestegg_packet_track(pkt, &track);
537 if(track == input->video_track)
539 nestegg_packet_tstamp(pkt, &tstamp);
540 i++;
543 nestegg_free_packet(pkt);
546 if(nestegg_track_seek(input->nestegg_ctx, input->video_track, 0))
547 goto fail;
549 *fps_num = (i - 1) * 1000000;
550 *fps_den = tstamp / 1000;
551 return 0;
552 fail:
553 nestegg_destroy(input->nestegg_ctx);
554 input->nestegg_ctx = NULL;
555 rewind(input->infile);
556 return 1;
560 static int
561 file_is_webm(struct input_ctx *input,
562 unsigned int *fourcc,
563 unsigned int *width,
564 unsigned int *height,
565 unsigned int *fps_den,
566 unsigned int *fps_num)
568 unsigned int i, n;
569 int track_type = -1;
570 uint64_t tstamp=0;
572 nestegg_io io = {nestegg_read_cb, nestegg_seek_cb, nestegg_tell_cb,
573 input->infile};
574 nestegg_video_params params;
575 nestegg_packet * pkt;
577 if(nestegg_init(&input->nestegg_ctx, io, NULL))
578 goto fail;
580 if(nestegg_track_count(input->nestegg_ctx, &n))
581 goto fail;
583 for(i=0; i<n; i++)
585 track_type = nestegg_track_type(input->nestegg_ctx, i);
587 if(track_type == NESTEGG_TRACK_VIDEO)
588 break;
589 else if(track_type < 0)
590 goto fail;
593 if(nestegg_track_codec_id(input->nestegg_ctx, i) != NESTEGG_CODEC_VP8)
595 fprintf(stderr, "Not VP8 video, quitting.\n");
596 exit(1);
599 input->video_track = i;
601 if(nestegg_track_video_params(input->nestegg_ctx, i, &params))
602 goto fail;
604 *fps_den = 0;
605 *fps_num = 0;
606 *fourcc = VP8_FOURCC;
607 *width = params.width;
608 *height = params.height;
609 return 1;
610 fail:
611 input->nestegg_ctx = NULL;
612 rewind(input->infile);
613 return 0;
617 void show_progress(int frame_in, int frame_out, unsigned long dx_time)
619 fprintf(stderr, "%d decoded frames/%d showed frames in %lu us (%.2f fps)\r",
620 frame_in, frame_out, dx_time,
621 (float)frame_out * 1000000.0 / (float)dx_time);
625 void generate_filename(const char *pattern, char *out, size_t q_len,
626 unsigned int d_w, unsigned int d_h,
627 unsigned int frame_in)
629 const char *p = pattern;
630 char *q = out;
634 char *next_pat = strchr(p, '%');
636 if(p == next_pat)
638 size_t pat_len;
640 // parse the pattern
641 q[q_len - 1] = '\0';
642 switch(p[1])
644 case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
645 case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
646 case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
647 case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
648 case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
649 case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
650 case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
651 case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
652 case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
653 case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
654 case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
655 default:
656 die("Unrecognized pattern %%%c\n", p[1]);
659 pat_len = strlen(q);
660 if(pat_len >= q_len - 1)
661 die("Output filename too long.\n");
662 q += pat_len;
663 p += 2;
664 q_len -= pat_len;
666 else
668 size_t copy_len;
670 // copy the next segment
671 if(!next_pat)
672 copy_len = strlen(p);
673 else
674 copy_len = next_pat - p;
676 if(copy_len >= q_len - 1)
677 die("Output filename too long.\n");
679 memcpy(q, p, copy_len);
680 q[copy_len] = '\0';
681 q += copy_len;
682 p += copy_len;
683 q_len -= copy_len;
685 } while(*p);
689 int main(int argc, const char **argv_)
691 vpx_codec_ctx_t decoder;
692 char *fn = NULL;
693 int i;
694 uint8_t *buf = NULL;
695 size_t buf_sz = 0, buf_alloc_sz = 0;
696 FILE *infile;
697 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0, do_md5 = 0, progress = 0;
698 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
699 vpx_codec_iface_t *iface = NULL;
700 unsigned int fourcc;
701 unsigned long dx_time = 0;
702 struct arg arg;
703 char **argv, **argi, **argj;
704 const char *outfile_pattern = 0;
705 char outfile[PATH_MAX];
706 int single_file;
707 int use_y4m = 1;
708 unsigned int width;
709 unsigned int height;
710 unsigned int fps_den;
711 unsigned int fps_num;
712 void *out = NULL;
713 vpx_codec_dec_cfg_t cfg = {0};
714 #if CONFIG_VP8_DECODER
715 vp8_postproc_cfg_t vp8_pp_cfg = {0};
716 int vp8_dbg_color_ref_frame = 0;
717 int vp8_dbg_color_mb_modes = 0;
718 int vp8_dbg_color_b_modes = 0;
719 int vp8_dbg_display_mv = 0;
720 #endif
721 struct input_ctx input = {0};
723 /* Parse command line */
724 exec_name = argv_[0];
725 argv = argv_dup(argc - 1, argv_ + 1);
727 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
729 memset(&arg, 0, sizeof(arg));
730 arg.argv_step = 1;
732 if (arg_match(&arg, &codecarg, argi))
734 int j, k = -1;
736 for (j = 0; j < sizeof(ifaces) / sizeof(ifaces[0]); j++)
737 if (!strcmp(ifaces[j].name, arg.val))
738 k = j;
740 if (k >= 0)
741 iface = ifaces[k].iface;
742 else
743 die("Error: Unrecognized argument (%s) to --codec\n",
744 arg.val);
746 else if (arg_match(&arg, &outputfile, argi))
747 outfile_pattern = arg.val;
748 else if (arg_match(&arg, &use_yv12, argi))
750 use_y4m = 0;
751 flipuv = 1;
753 else if (arg_match(&arg, &use_i420, argi))
755 use_y4m = 0;
756 flipuv = 0;
758 else if (arg_match(&arg, &flipuvarg, argi))
759 flipuv = 1;
760 else if (arg_match(&arg, &noblitarg, argi))
761 noblit = 1;
762 else if (arg_match(&arg, &progressarg, argi))
763 progress = 1;
764 else if (arg_match(&arg, &limitarg, argi))
765 stop_after = arg_parse_uint(&arg);
766 else if (arg_match(&arg, &postprocarg, argi))
767 postproc = 1;
768 else if (arg_match(&arg, &md5arg, argi))
769 do_md5 = 1;
770 else if (arg_match(&arg, &summaryarg, argi))
771 summary = 1;
772 else if (arg_match(&arg, &threadsarg, argi))
773 cfg.threads = arg_parse_uint(&arg);
774 else if (arg_match(&arg, &verbosearg, argi))
775 quiet = 0;
777 #if CONFIG_VP8_DECODER
778 else if (arg_match(&arg, &addnoise_level, argi))
780 postproc = 1;
781 vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE;
782 vp8_pp_cfg.noise_level = arg_parse_uint(&arg);
784 else if (arg_match(&arg, &demacroblock_level, argi))
786 postproc = 1;
787 vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK;
788 vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg);
790 else if (arg_match(&arg, &deblock, argi))
792 postproc = 1;
793 vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK;
795 else if (arg_match(&arg, &pp_debug_info, argi))
797 unsigned int level = arg_parse_uint(&arg);
799 postproc = 1;
800 vp8_pp_cfg.post_proc_flag &= ~0x7;
802 if (level)
803 vp8_pp_cfg.post_proc_flag |= level;
805 else if (arg_match(&arg, &pp_disp_ref_frame, argi))
807 unsigned int flags = arg_parse_int(&arg);
808 if (flags)
810 postproc = 1;
811 vp8_dbg_color_ref_frame = flags;
814 else if (arg_match(&arg, &pp_disp_mb_modes, argi))
816 unsigned int flags = arg_parse_int(&arg);
817 if (flags)
819 postproc = 1;
820 vp8_dbg_color_mb_modes = flags;
823 else if (arg_match(&arg, &pp_disp_b_modes, argi))
825 unsigned int flags = arg_parse_int(&arg);
826 if (flags)
828 postproc = 1;
829 vp8_dbg_color_b_modes = flags;
832 else if (arg_match(&arg, &pp_disp_mvs, argi))
834 unsigned int flags = arg_parse_int(&arg);
835 if (flags)
837 postproc = 1;
838 vp8_dbg_display_mv = flags;
842 #endif
843 else
844 argj++;
847 /* Check for unrecognized options */
848 for (argi = argv; *argi; argi++)
849 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
850 die("Error: Unrecognized option %s\n", *argi);
852 /* Handle non-option arguments */
853 fn = argv[0];
855 if (!fn)
856 usage_exit();
858 /* Open file */
859 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
861 if (!infile)
863 fprintf(stderr, "Failed to open file '%s'",
864 strcmp(fn, "-") ? fn : "stdin");
865 return EXIT_FAILURE;
868 /* Make sure we don't dump to the terminal, unless forced to with -o - */
869 if(!outfile_pattern && isatty(fileno(stdout)) && !do_md5 && !noblit)
871 fprintf(stderr,
872 "Not dumping raw video to your terminal. Use '-o -' to "
873 "override.\n");
874 return EXIT_FAILURE;
877 input.infile = infile;
878 if(file_is_ivf(infile, &fourcc, &width, &height, &fps_den,
879 &fps_num))
880 input.kind = IVF_FILE;
881 else if(file_is_webm(&input, &fourcc, &width, &height, &fps_den, &fps_num))
882 input.kind = WEBM_FILE;
883 else if(file_is_raw(infile, &fourcc, &width, &height, &fps_den, &fps_num))
884 input.kind = RAW_FILE;
885 else
887 fprintf(stderr, "Unrecognized input file type.\n");
888 return EXIT_FAILURE;
891 /* If the output file is not set or doesn't have a sequence number in
892 * it, then we only open it once.
894 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
895 single_file = 1;
897 const char *p = outfile_pattern;
900 p = strchr(p, '%');
901 if(p && p[1] >= '1' && p[1] <= '9')
903 // pattern contains sequence number, so it's not unique.
904 single_file = 0;
905 break;
907 if(p)
908 p++;
909 } while(p);
912 if(single_file && !noblit)
914 generate_filename(outfile_pattern, outfile, sizeof(outfile)-1,
915 width, height, 0);
916 out = out_open(outfile, do_md5);
919 if (use_y4m && !noblit)
921 char buffer[128];
922 if (!single_file)
924 fprintf(stderr, "YUV4MPEG2 not supported with output patterns,"
925 " try --i420 or --yv12.\n");
926 return EXIT_FAILURE;
929 if(input.kind == WEBM_FILE)
930 if(webm_guess_framerate(&input, &fps_den, &fps_num))
932 fprintf(stderr, "Failed to guess framerate -- error parsing "
933 "webm file?\n");
934 return EXIT_FAILURE;
938 /*Note: We can't output an aspect ratio here because IVF doesn't
939 store one, and neither does VP8.
940 That will have to wait until these tools support WebM natively.*/
941 sprintf(buffer, "YUV4MPEG2 C%s W%u H%u F%u:%u I%c\n",
942 "420jpeg", width, height, fps_num, fps_den, 'p');
943 out_put(out, (unsigned char *)buffer, strlen(buffer), do_md5);
946 /* Try to determine the codec from the fourcc. */
947 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
948 if ((fourcc & ifaces[i].fourcc_mask) == ifaces[i].fourcc)
950 vpx_codec_iface_t *ivf_iface = ifaces[i].iface;
952 if (iface && iface != ivf_iface)
953 fprintf(stderr, "Notice -- IVF header indicates codec: %s\n",
954 ifaces[i].name);
955 else
956 iface = ivf_iface;
958 break;
961 if (vpx_codec_dec_init(&decoder, iface ? iface : ifaces[0].iface, &cfg,
962 postproc ? VPX_CODEC_USE_POSTPROC : 0))
964 fprintf(stderr, "Failed to initialize decoder: %s\n", vpx_codec_error(&decoder));
965 return EXIT_FAILURE;
968 if (!quiet)
969 fprintf(stderr, "%s\n", decoder.name);
971 #if CONFIG_VP8_DECODER
973 if (vp8_pp_cfg.post_proc_flag
974 && vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg))
976 fprintf(stderr, "Failed to configure postproc: %s\n", vpx_codec_error(&decoder));
977 return EXIT_FAILURE;
980 if (vp8_dbg_color_ref_frame
981 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_REF_FRAME, vp8_dbg_color_ref_frame))
983 fprintf(stderr, "Failed to configure reference block visualizer: %s\n", vpx_codec_error(&decoder));
984 return EXIT_FAILURE;
987 if (vp8_dbg_color_mb_modes
988 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_MB_MODES, vp8_dbg_color_mb_modes))
990 fprintf(stderr, "Failed to configure macro block visualizer: %s\n", vpx_codec_error(&decoder));
991 return EXIT_FAILURE;
994 if (vp8_dbg_color_b_modes
995 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_B_MODES, vp8_dbg_color_b_modes))
997 fprintf(stderr, "Failed to configure block visualizer: %s\n", vpx_codec_error(&decoder));
998 return EXIT_FAILURE;
1001 if (vp8_dbg_display_mv
1002 && vpx_codec_control(&decoder, VP8_SET_DBG_DISPLAY_MV, vp8_dbg_display_mv))
1004 fprintf(stderr, "Failed to configure motion vector visualizer: %s\n", vpx_codec_error(&decoder));
1005 return EXIT_FAILURE;
1007 #endif
1009 /* Decode file */
1010 while (!read_frame(&input, &buf, &buf_sz, &buf_alloc_sz))
1012 vpx_codec_iter_t iter = NULL;
1013 vpx_image_t *img;
1014 struct vpx_usec_timer timer;
1016 vpx_usec_timer_start(&timer);
1018 if (vpx_codec_decode(&decoder, buf, buf_sz, NULL, 0))
1020 const char *detail = vpx_codec_error_detail(&decoder);
1021 fprintf(stderr, "Failed to decode frame: %s\n", vpx_codec_error(&decoder));
1023 if (detail)
1024 fprintf(stderr, " Additional information: %s\n", detail);
1026 goto fail;
1029 vpx_usec_timer_mark(&timer);
1030 dx_time += vpx_usec_timer_elapsed(&timer);
1032 ++frame_in;
1034 if ((img = vpx_codec_get_frame(&decoder, &iter)))
1035 ++frame_out;
1037 if (progress)
1038 show_progress(frame_in, frame_out, dx_time);
1040 if (!noblit)
1042 if (img)
1044 unsigned int y;
1045 char out_fn[PATH_MAX];
1046 uint8_t *buf;
1048 if (!single_file)
1050 size_t len = sizeof(out_fn)-1;
1052 out_fn[len] = '\0';
1053 generate_filename(outfile_pattern, out_fn, len-1,
1054 img->d_w, img->d_h, frame_in);
1055 out = out_open(out_fn, do_md5);
1057 else if(use_y4m)
1058 out_put(out, (unsigned char *)"FRAME\n", 6, do_md5);
1060 buf = img->planes[VPX_PLANE_Y];
1062 for (y = 0; y < img->d_h; y++)
1064 out_put(out, buf, img->d_w, do_md5);
1065 buf += img->stride[VPX_PLANE_Y];
1068 buf = img->planes[flipuv?VPX_PLANE_V:VPX_PLANE_U];
1070 for (y = 0; y < (1 + img->d_h) / 2; y++)
1072 out_put(out, buf, (1 + img->d_w) / 2, do_md5);
1073 buf += img->stride[VPX_PLANE_U];
1076 buf = img->planes[flipuv?VPX_PLANE_U:VPX_PLANE_V];
1078 for (y = 0; y < (1 + img->d_h) / 2; y++)
1080 out_put(out, buf, (1 + img->d_w) / 2, do_md5);
1081 buf += img->stride[VPX_PLANE_V];
1084 if (!single_file)
1085 out_close(out, out_fn, do_md5);
1089 if (stop_after && frame_in >= stop_after)
1090 break;
1093 if (summary || progress)
1095 show_progress(frame_in, frame_out, dx_time);
1096 fprintf(stderr, "\n");
1099 fail:
1101 if (vpx_codec_destroy(&decoder))
1103 fprintf(stderr, "Failed to destroy decoder: %s\n", vpx_codec_error(&decoder));
1104 return EXIT_FAILURE;
1107 if (single_file && !noblit)
1108 out_close(out, outfile, do_md5);
1110 if(input.nestegg_ctx)
1111 nestegg_destroy(input.nestegg_ctx);
1112 if(input.kind != WEBM_FILE)
1113 free(buf);
1114 fclose(infile);
1115 free(argv);
1117 return EXIT_SUCCESS;