sse2 intrinsic version of vp8_mbloop_filter_vertical_edge()
[aom.git] / vpxdec.c
blob04d1ac7c5581939c24ef1e2d185ae572fb22f167
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>
21 #define VPX_CODEC_DISABLE_COMPAT 1
22 #include "vpx_config.h"
23 #include "vpx/vpx_decoder.h"
24 #include "vpx_ports/vpx_timer.h"
25 #if CONFIG_VP8_DECODER
26 #include "vpx/vp8dx.h"
27 #endif
28 #if CONFIG_MD5
29 #include "md5_utils.h"
30 #endif
31 #include "tools_common.h"
32 #include "nestegg/include/nestegg/nestegg.h"
34 #if CONFIG_OS_SUPPORT
35 #if defined(_WIN32)
36 #include <io.h>
37 #define snprintf _snprintf
38 #define isatty _isatty
39 #define fileno _fileno
40 #else
41 #include <unistd.h>
42 #endif
43 #endif
45 #ifndef PATH_MAX
46 #define PATH_MAX 256
47 #endif
49 static const char *exec_name;
51 #define VP8_FOURCC (0x00385056)
53 static const struct {
54 char const *name;
55 const vpx_codec_iface_t *(*iface)(void);
56 unsigned int fourcc;
57 unsigned int fourcc_mask;
58 } ifaces[] = {
59 #if CONFIG_VP8_DECODER
60 {"vp8", vpx_codec_vp8_dx, VP8_FOURCC, 0x00FFFFFF},
61 #endif
65 #include "args.h"
66 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1,
67 "Codec to use");
68 static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0,
69 "Output raw YV12 frames");
70 static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0,
71 "Output raw I420 frames");
72 static const arg_def_t flipuvarg = ARG_DEF(NULL, "flipuv", 0,
73 "Flip the chroma planes in the output");
74 static const arg_def_t noblitarg = ARG_DEF(NULL, "noblit", 0,
75 "Don't process the decoded frames");
76 static const arg_def_t progressarg = ARG_DEF(NULL, "progress", 0,
77 "Show progress after each frame decodes");
78 static const arg_def_t limitarg = ARG_DEF(NULL, "limit", 1,
79 "Stop decoding after n frames");
80 static const arg_def_t skiparg = ARG_DEF(NULL, "skip", 1,
81 "Skip the first n input frames");
82 static const arg_def_t postprocarg = ARG_DEF(NULL, "postproc", 0,
83 "Postprocess decoded frames");
84 static const arg_def_t summaryarg = ARG_DEF(NULL, "summary", 0,
85 "Show timing summary");
86 static const arg_def_t outputfile = ARG_DEF("o", "output", 1,
87 "Output file name pattern (see below)");
88 static const arg_def_t threadsarg = ARG_DEF("t", "threads", 1,
89 "Max threads to use");
90 static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0,
91 "Show version string");
93 #if CONFIG_MD5
94 static const arg_def_t md5arg = ARG_DEF(NULL, "md5", 0,
95 "Compute the MD5 sum of the decoded frame");
96 #endif
97 static const arg_def_t *all_args[] = {
98 &codecarg, &use_yv12, &use_i420, &flipuvarg, &noblitarg,
99 &progressarg, &limitarg, &skiparg, &postprocarg, &summaryarg, &outputfile,
100 &threadsarg, &verbosearg,
101 #if CONFIG_MD5
102 &md5arg,
103 #endif
104 NULL
107 #if CONFIG_VP8_DECODER
108 static const arg_def_t addnoise_level = ARG_DEF(NULL, "noise-level", 1,
109 "Enable VP8 postproc add noise");
110 static const arg_def_t deblock = ARG_DEF(NULL, "deblock", 0,
111 "Enable VP8 deblocking");
112 static const arg_def_t demacroblock_level = ARG_DEF(NULL, "demacroblock-level", 1,
113 "Enable VP8 demacroblocking, w/ level");
114 static const arg_def_t pp_debug_info = ARG_DEF(NULL, "pp-debug-info", 1,
115 "Enable VP8 visible debug info");
116 static const arg_def_t pp_disp_ref_frame = ARG_DEF(NULL, "pp-dbg-ref-frame", 1,
117 "Display only selected reference frame per macro block");
118 static const arg_def_t pp_disp_mb_modes = ARG_DEF(NULL, "pp-dbg-mb-modes", 1,
119 "Display only selected macro block modes");
120 static const arg_def_t pp_disp_b_modes = ARG_DEF(NULL, "pp-dbg-b-modes", 1,
121 "Display only selected block modes");
122 static const arg_def_t pp_disp_mvs = ARG_DEF(NULL, "pp-dbg-mvs", 1,
123 "Draw only selected motion vectors");
125 static const arg_def_t *vp8_pp_args[] = {
126 &addnoise_level, &deblock, &demacroblock_level, &pp_debug_info,
127 &pp_disp_ref_frame, &pp_disp_mb_modes, &pp_disp_b_modes, &pp_disp_mvs,
128 NULL
130 #endif
132 static void usage_exit() {
133 int i;
135 fprintf(stderr, "Usage: %s <options> filename\n\n"
136 "Options:\n", exec_name);
137 arg_show_usage(stderr, all_args);
138 #if CONFIG_VP8_DECODER
139 fprintf(stderr, "\nVP8 Postprocessing Options:\n");
140 arg_show_usage(stderr, vp8_pp_args);
141 #endif
142 fprintf(stderr,
143 "\nOutput File Patterns:\n\n"
144 " The -o argument specifies the name of the file(s) to "
145 "write to. If the\n argument does not include any escape "
146 "characters, the output will be\n written to a single file. "
147 "Otherwise, the filename will be calculated by\n expanding "
148 "the following escape characters:\n"
149 "\n\t%%w - Frame width"
150 "\n\t%%h - Frame height"
151 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
152 "\n\n Pattern arguments are only supported in conjunction "
153 "with the --yv12 and\n --i420 options. If the -o option is "
154 "not specified, the output will be\n directed to stdout.\n"
156 fprintf(stderr, "\nIncluded decoders:\n\n");
158 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
159 fprintf(stderr, " %-6s - %s\n",
160 ifaces[i].name,
161 vpx_codec_iface_name(ifaces[i].iface()));
163 exit(EXIT_FAILURE);
166 void die(const char *fmt, ...) {
167 va_list ap;
168 va_start(ap, fmt);
169 vfprintf(stderr, fmt, ap);
170 fprintf(stderr, "\n");
171 usage_exit();
174 static unsigned int mem_get_le16(const void *vmem) {
175 unsigned int val;
176 const unsigned char *mem = (const unsigned char *)vmem;
178 val = mem[1] << 8;
179 val |= mem[0];
180 return val;
183 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 {
195 RAW_FILE,
196 IVF_FILE,
197 WEBM_FILE
200 struct input_ctx {
201 enum file_kind kind;
202 FILE *infile;
203 nestegg *nestegg_ctx;
204 nestegg_packet *pkt;
205 unsigned int chunk;
206 unsigned int chunks;
207 unsigned int video_track;
210 #define IVF_FRAME_HDR_SZ (sizeof(uint32_t) + sizeof(uint64_t))
211 #define RAW_FRAME_HDR_SZ (sizeof(uint32_t))
212 static int read_frame(struct input_ctx *input,
213 uint8_t **buf,
214 size_t *buf_sz,
215 size_t *buf_alloc_sz) {
216 char raw_hdr[IVF_FRAME_HDR_SZ];
217 size_t new_buf_sz;
218 FILE *infile = input->infile;
219 enum file_kind kind = input->kind;
220 if (kind == WEBM_FILE) {
221 if (input->chunk >= input->chunks) {
222 unsigned int track;
224 do {
225 /* End of this packet, get another. */
226 if (input->pkt)
227 nestegg_free_packet(input->pkt);
229 if (nestegg_read_packet(input->nestegg_ctx, &input->pkt) <= 0
230 || nestegg_packet_track(input->pkt, &track))
231 return 1;
233 } while (track != input->video_track);
235 if (nestegg_packet_count(input->pkt, &input->chunks))
236 return 1;
237 input->chunk = 0;
240 if (nestegg_packet_data(input->pkt, input->chunk, buf, buf_sz))
241 return 1;
242 input->chunk++;
244 return 0;
246 /* For both the raw and ivf formats, the frame size is the first 4 bytes
247 * of the frame header. We just need to special case on the header
248 * size.
250 else if (fread(raw_hdr, kind == IVF_FILE
251 ? IVF_FRAME_HDR_SZ : RAW_FRAME_HDR_SZ, 1, infile) != 1) {
252 if (!feof(infile))
253 fprintf(stderr, "Failed to read frame size\n");
255 new_buf_sz = 0;
256 } else {
257 new_buf_sz = mem_get_le32(raw_hdr);
259 if (new_buf_sz > 256 * 1024 * 1024) {
260 fprintf(stderr, "Error: Read invalid frame size (%u)\n",
261 (unsigned int)new_buf_sz);
262 new_buf_sz = 0;
265 if (kind == RAW_FILE && new_buf_sz > 256 * 1024)
266 fprintf(stderr, "Warning: Read invalid frame size (%u)"
267 " - not a raw file?\n", (unsigned int)new_buf_sz);
269 if (new_buf_sz > *buf_alloc_sz) {
270 uint8_t *new_buf = realloc(*buf, 2 * new_buf_sz);
272 if (new_buf) {
273 *buf = new_buf;
274 *buf_alloc_sz = 2 * new_buf_sz;
275 } else {
276 fprintf(stderr, "Failed to allocate compressed data buffer\n");
277 new_buf_sz = 0;
282 *buf_sz = new_buf_sz;
284 if (*buf_sz) {
285 if (fread(*buf, 1, *buf_sz, infile) != *buf_sz) {
286 fprintf(stderr, "Failed to read full frame\n");
287 return 1;
290 return 0;
293 return 1;
296 void *out_open(const char *out_fn, int do_md5) {
297 void *out = NULL;
299 if (do_md5) {
300 #if CONFIG_MD5
301 MD5Context *md5_ctx = out = malloc(sizeof(MD5Context));
302 (void)out_fn;
303 MD5Init(md5_ctx);
304 #endif
305 } else {
306 FILE *outfile = out = strcmp("-", out_fn) ? fopen(out_fn, "wb")
307 : set_binary_mode(stdout);
309 if (!outfile) {
310 fprintf(stderr, "Failed to output file");
311 exit(EXIT_FAILURE);
315 return out;
318 void out_put(void *out, const uint8_t *buf, unsigned int len, int do_md5) {
319 if (do_md5) {
320 #if CONFIG_MD5
321 MD5Update(out, buf, len);
322 #endif
323 } else {
324 if (fwrite(buf, 1, len, out));
328 void out_close(void *out, const char *out_fn, int do_md5) {
329 if (do_md5) {
330 #if CONFIG_MD5
331 uint8_t md5[16];
332 int i;
334 MD5Final(md5, out);
335 free(out);
337 for (i = 0; i < 16; i++)
338 printf("%02x", md5[i]);
340 printf(" %s\n", out_fn);
341 #endif
342 } else {
343 fclose(out);
347 unsigned int file_is_ivf(FILE *infile,
348 unsigned int *fourcc,
349 unsigned int *width,
350 unsigned int *height,
351 unsigned int *fps_den,
352 unsigned int *fps_num) {
353 char raw_hdr[32];
354 int is_ivf = 0;
356 if (fread(raw_hdr, 1, 32, infile) == 32) {
357 if (raw_hdr[0] == 'D' && raw_hdr[1] == 'K'
358 && raw_hdr[2] == 'I' && raw_hdr[3] == 'F') {
359 is_ivf = 1;
361 if (mem_get_le16(raw_hdr + 4) != 0)
362 fprintf(stderr, "Error: Unrecognized IVF version! This file may not"
363 " decode properly.");
365 *fourcc = mem_get_le32(raw_hdr + 8);
366 *width = mem_get_le16(raw_hdr + 12);
367 *height = mem_get_le16(raw_hdr + 14);
368 *fps_num = mem_get_le32(raw_hdr + 16);
369 *fps_den = mem_get_le32(raw_hdr + 20);
371 /* Some versions of vpxenc used 1/(2*fps) for the timebase, so
372 * we can guess the framerate using only the timebase in this
373 * case. Other files would require reading ahead to guess the
374 * timebase, like we do for webm.
376 if (*fps_num < 1000) {
377 /* Correct for the factor of 2 applied to the timebase in the
378 * encoder.
380 if (*fps_num & 1)*fps_den <<= 1;
381 else *fps_num >>= 1;
382 } else {
383 /* Don't know FPS for sure, and don't have readahead code
384 * (yet?), so just default to 30fps.
386 *fps_num = 30;
387 *fps_den = 1;
392 if (!is_ivf)
393 rewind(infile);
395 return is_ivf;
399 unsigned int file_is_raw(FILE *infile,
400 unsigned int *fourcc,
401 unsigned int *width,
402 unsigned int *height,
403 unsigned int *fps_den,
404 unsigned int *fps_num) {
405 unsigned char buf[32];
406 int is_raw = 0;
407 vpx_codec_stream_info_t si;
409 si.sz = sizeof(si);
411 if (fread(buf, 1, 32, infile) == 32) {
412 int i;
414 if (mem_get_le32(buf) < 256 * 1024 * 1024)
415 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
416 if (!vpx_codec_peek_stream_info(ifaces[i].iface(),
417 buf + 4, 32 - 4, &si)) {
418 is_raw = 1;
419 *fourcc = ifaces[i].fourcc;
420 *width = si.w;
421 *height = si.h;
422 *fps_num = 30;
423 *fps_den = 1;
424 break;
428 rewind(infile);
429 return is_raw;
433 static int
434 nestegg_read_cb(void *buffer, size_t length, void *userdata) {
435 FILE *f = userdata;
437 if (fread(buffer, 1, length, f) < length) {
438 if (ferror(f))
439 return -1;
440 if (feof(f))
441 return 0;
443 return 1;
447 static int
448 nestegg_seek_cb(int64_t offset, int whence, void *userdata) {
449 switch (whence) {
450 case NESTEGG_SEEK_SET:
451 whence = SEEK_SET;
452 break;
453 case NESTEGG_SEEK_CUR:
454 whence = SEEK_CUR;
455 break;
456 case NESTEGG_SEEK_END:
457 whence = SEEK_END;
458 break;
460 return fseek(userdata, offset, whence) ? -1 : 0;
464 static int64_t
465 nestegg_tell_cb(void *userdata) {
466 return ftell(userdata);
470 static void
471 nestegg_log_cb(nestegg *context, unsigned int severity, char const *format,
472 ...) {
473 va_list ap;
475 va_start(ap, format);
476 vfprintf(stderr, format, ap);
477 fprintf(stderr, "\n");
478 va_end(ap);
482 static int
483 webm_guess_framerate(struct input_ctx *input,
484 unsigned int *fps_den,
485 unsigned int *fps_num) {
486 unsigned int i;
487 uint64_t tstamp = 0;
489 /* Guess the framerate. Read up to 1 second, or 50 video packets,
490 * whichever comes first.
492 for (i = 0; tstamp < 1000000000 && i < 50;) {
493 nestegg_packet *pkt;
494 unsigned int track;
496 if (nestegg_read_packet(input->nestegg_ctx, &pkt) <= 0)
497 break;
499 nestegg_packet_track(pkt, &track);
500 if (track == input->video_track) {
501 nestegg_packet_tstamp(pkt, &tstamp);
502 i++;
505 nestegg_free_packet(pkt);
508 if (nestegg_track_seek(input->nestegg_ctx, input->video_track, 0))
509 goto fail;
511 *fps_num = (i - 1) * 1000000;
512 *fps_den = tstamp / 1000;
513 return 0;
514 fail:
515 nestegg_destroy(input->nestegg_ctx);
516 input->nestegg_ctx = NULL;
517 rewind(input->infile);
518 return 1;
522 static int
523 file_is_webm(struct input_ctx *input,
524 unsigned int *fourcc,
525 unsigned int *width,
526 unsigned int *height,
527 unsigned int *fps_den,
528 unsigned int *fps_num) {
529 unsigned int i, n;
530 int track_type = -1;
532 nestegg_io io = {nestegg_read_cb, nestegg_seek_cb, nestegg_tell_cb,
533 input->infile
535 nestegg_video_params params;
537 if (nestegg_init(&input->nestegg_ctx, io, NULL))
538 goto fail;
540 if (nestegg_track_count(input->nestegg_ctx, &n))
541 goto fail;
543 for (i = 0; i < n; i++) {
544 track_type = nestegg_track_type(input->nestegg_ctx, i);
546 if (track_type == NESTEGG_TRACK_VIDEO)
547 break;
548 else if (track_type < 0)
549 goto fail;
552 if (nestegg_track_codec_id(input->nestegg_ctx, i) != NESTEGG_CODEC_VP8) {
553 fprintf(stderr, "Not VP8 video, quitting.\n");
554 exit(1);
557 input->video_track = i;
559 if (nestegg_track_video_params(input->nestegg_ctx, i, &params))
560 goto fail;
562 *fps_den = 0;
563 *fps_num = 0;
564 *fourcc = VP8_FOURCC;
565 *width = params.width;
566 *height = params.height;
567 return 1;
568 fail:
569 input->nestegg_ctx = NULL;
570 rewind(input->infile);
571 return 0;
575 void show_progress(int frame_in, int frame_out, unsigned long dx_time) {
576 fprintf(stderr, "%d decoded frames/%d showed frames in %lu us (%.2f fps)\r",
577 frame_in, frame_out, dx_time,
578 (float)frame_out * 1000000.0 / (float)dx_time);
582 void generate_filename(const char *pattern, char *out, size_t q_len,
583 unsigned int d_w, unsigned int d_h,
584 unsigned int frame_in) {
585 const char *p = pattern;
586 char *q = out;
588 do {
589 char *next_pat = strchr(p, '%');
591 if (p == next_pat) {
592 size_t pat_len;
594 // parse the pattern
595 q[q_len - 1] = '\0';
596 switch (p[1]) {
597 case 'w':
598 snprintf(q, q_len - 1, "%d", d_w);
599 break;
600 case 'h':
601 snprintf(q, q_len - 1, "%d", d_h);
602 break;
603 case '1':
604 snprintf(q, q_len - 1, "%d", frame_in);
605 break;
606 case '2':
607 snprintf(q, q_len - 1, "%02d", frame_in);
608 break;
609 case '3':
610 snprintf(q, q_len - 1, "%03d", frame_in);
611 break;
612 case '4':
613 snprintf(q, q_len - 1, "%04d", frame_in);
614 break;
615 case '5':
616 snprintf(q, q_len - 1, "%05d", frame_in);
617 break;
618 case '6':
619 snprintf(q, q_len - 1, "%06d", frame_in);
620 break;
621 case '7':
622 snprintf(q, q_len - 1, "%07d", frame_in);
623 break;
624 case '8':
625 snprintf(q, q_len - 1, "%08d", frame_in);
626 break;
627 case '9':
628 snprintf(q, q_len - 1, "%09d", frame_in);
629 break;
630 default:
631 die("Unrecognized pattern %%%c\n", p[1]);
634 pat_len = strlen(q);
635 if (pat_len >= q_len - 1)
636 die("Output filename too long.\n");
637 q += pat_len;
638 p += 2;
639 q_len -= pat_len;
640 } else {
641 size_t copy_len;
643 // copy the next segment
644 if (!next_pat)
645 copy_len = strlen(p);
646 else
647 copy_len = next_pat - p;
649 if (copy_len >= q_len - 1)
650 die("Output filename too long.\n");
652 memcpy(q, p, copy_len);
653 q[copy_len] = '\0';
654 q += copy_len;
655 p += copy_len;
656 q_len -= copy_len;
658 } while (*p);
662 int main(int argc, const char **argv_) {
663 vpx_codec_ctx_t decoder;
664 char *fn = NULL;
665 int i;
666 uint8_t *buf = NULL;
667 size_t buf_sz = 0, buf_alloc_sz = 0;
668 FILE *infile;
669 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0, do_md5 = 0, progress = 0;
670 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
671 int arg_skip = 0;
672 vpx_codec_iface_t *iface = NULL;
673 unsigned int fourcc;
674 unsigned long dx_time = 0;
675 struct arg arg;
676 char **argv, **argi, **argj;
677 const char *outfile_pattern = 0;
678 char outfile[PATH_MAX];
679 int single_file;
680 int use_y4m = 1;
681 unsigned int width;
682 unsigned int height;
683 unsigned int fps_den;
684 unsigned int fps_num;
685 void *out = NULL;
686 vpx_codec_dec_cfg_t cfg = {0};
687 #if CONFIG_VP8_DECODER
688 vp8_postproc_cfg_t vp8_pp_cfg = {0};
689 int vp8_dbg_color_ref_frame = 0;
690 int vp8_dbg_color_mb_modes = 0;
691 int vp8_dbg_color_b_modes = 0;
692 int vp8_dbg_display_mv = 0;
693 #endif
694 struct input_ctx input = {0};
695 int frames_corrupted = 0;
696 int dec_flags = 0;
698 /* Parse command line */
699 exec_name = argv_[0];
700 argv = argv_dup(argc - 1, argv_ + 1);
702 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
703 memset(&arg, 0, sizeof(arg));
704 arg.argv_step = 1;
706 if (arg_match(&arg, &codecarg, argi)) {
707 int j, k = -1;
709 for (j = 0; j < sizeof(ifaces) / sizeof(ifaces[0]); j++)
710 if (!strcmp(ifaces[j].name, arg.val))
711 k = j;
713 if (k >= 0)
714 iface = ifaces[k].iface();
715 else
716 die("Error: Unrecognized argument (%s) to --codec\n",
717 arg.val);
718 } else if (arg_match(&arg, &outputfile, argi))
719 outfile_pattern = arg.val;
720 else if (arg_match(&arg, &use_yv12, argi)) {
721 use_y4m = 0;
722 flipuv = 1;
723 } else if (arg_match(&arg, &use_i420, argi)) {
724 use_y4m = 0;
725 flipuv = 0;
726 } else if (arg_match(&arg, &flipuvarg, argi))
727 flipuv = 1;
728 else if (arg_match(&arg, &noblitarg, argi))
729 noblit = 1;
730 else if (arg_match(&arg, &progressarg, argi))
731 progress = 1;
732 else if (arg_match(&arg, &limitarg, argi))
733 stop_after = arg_parse_uint(&arg);
734 else if (arg_match(&arg, &skiparg, argi))
735 arg_skip = arg_parse_uint(&arg);
736 else if (arg_match(&arg, &postprocarg, argi))
737 postproc = 1;
738 else if (arg_match(&arg, &md5arg, argi))
739 do_md5 = 1;
740 else if (arg_match(&arg, &summaryarg, argi))
741 summary = 1;
742 else if (arg_match(&arg, &threadsarg, argi))
743 cfg.threads = arg_parse_uint(&arg);
744 else if (arg_match(&arg, &verbosearg, argi))
745 quiet = 0;
747 #if CONFIG_VP8_DECODER
748 else if (arg_match(&arg, &addnoise_level, argi)) {
749 postproc = 1;
750 vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE;
751 vp8_pp_cfg.noise_level = arg_parse_uint(&arg);
752 } else if (arg_match(&arg, &demacroblock_level, argi)) {
753 postproc = 1;
754 vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK;
755 vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg);
756 } else if (arg_match(&arg, &deblock, argi)) {
757 postproc = 1;
758 vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK;
759 } else if (arg_match(&arg, &pp_debug_info, argi)) {
760 unsigned int level = arg_parse_uint(&arg);
762 postproc = 1;
763 vp8_pp_cfg.post_proc_flag &= ~0x7;
765 if (level)
766 vp8_pp_cfg.post_proc_flag |= level;
767 } else if (arg_match(&arg, &pp_disp_ref_frame, argi)) {
768 unsigned int flags = arg_parse_int(&arg);
769 if (flags) {
770 postproc = 1;
771 vp8_dbg_color_ref_frame = flags;
773 } else if (arg_match(&arg, &pp_disp_mb_modes, argi)) {
774 unsigned int flags = arg_parse_int(&arg);
775 if (flags) {
776 postproc = 1;
777 vp8_dbg_color_mb_modes = flags;
779 } else if (arg_match(&arg, &pp_disp_b_modes, argi)) {
780 unsigned int flags = arg_parse_int(&arg);
781 if (flags) {
782 postproc = 1;
783 vp8_dbg_color_b_modes = flags;
785 } else if (arg_match(&arg, &pp_disp_mvs, argi)) {
786 unsigned int flags = arg_parse_int(&arg);
787 if (flags) {
788 postproc = 1;
789 vp8_dbg_display_mv = flags;
793 #endif
794 else
795 argj++;
798 /* Check for unrecognized options */
799 for (argi = argv; *argi; argi++)
800 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
801 die("Error: Unrecognized option %s\n", *argi);
803 /* Handle non-option arguments */
804 fn = argv[0];
806 if (!fn)
807 usage_exit();
809 /* Open file */
810 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
812 if (!infile) {
813 fprintf(stderr, "Failed to open file '%s'",
814 strcmp(fn, "-") ? fn : "stdin");
815 return EXIT_FAILURE;
817 #if CONFIG_OS_SUPPORT
818 /* Make sure we don't dump to the terminal, unless forced to with -o - */
819 if (!outfile_pattern && isatty(fileno(stdout)) && !do_md5 && !noblit) {
820 fprintf(stderr,
821 "Not dumping raw video to your terminal. Use '-o -' to "
822 "override.\n");
823 return EXIT_FAILURE;
825 #endif
826 input.infile = infile;
827 if (file_is_ivf(infile, &fourcc, &width, &height, &fps_den,
828 &fps_num))
829 input.kind = IVF_FILE;
830 else if (file_is_webm(&input, &fourcc, &width, &height, &fps_den, &fps_num))
831 input.kind = WEBM_FILE;
832 else if (file_is_raw(infile, &fourcc, &width, &height, &fps_den, &fps_num))
833 input.kind = RAW_FILE;
834 else {
835 fprintf(stderr, "Unrecognized input file type.\n");
836 return EXIT_FAILURE;
839 /* If the output file is not set or doesn't have a sequence number in
840 * it, then we only open it once.
842 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
843 single_file = 1;
845 const char *p = outfile_pattern;
846 do {
847 p = strchr(p, '%');
848 if (p && p[1] >= '1' && p[1] <= '9') {
849 // pattern contains sequence number, so it's not unique.
850 single_file = 0;
851 break;
853 if (p)
854 p++;
855 } while (p);
858 if (single_file && !noblit) {
859 generate_filename(outfile_pattern, outfile, sizeof(outfile) - 1,
860 width, height, 0);
861 out = out_open(outfile, do_md5);
864 if (use_y4m && !noblit) {
865 char buffer[128];
866 if (!single_file) {
867 fprintf(stderr, "YUV4MPEG2 not supported with output patterns,"
868 " try --i420 or --yv12.\n");
869 return EXIT_FAILURE;
872 if (input.kind == WEBM_FILE)
873 if (webm_guess_framerate(&input, &fps_den, &fps_num)) {
874 fprintf(stderr, "Failed to guess framerate -- error parsing "
875 "webm file?\n");
876 return EXIT_FAILURE;
880 /*Note: We can't output an aspect ratio here because IVF doesn't
881 store one, and neither does VP8.
882 That will have to wait until these tools support WebM natively.*/
883 sprintf(buffer, "YUV4MPEG2 C%s W%u H%u F%u:%u I%c\n",
884 "420jpeg", width, height, fps_num, fps_den, 'p');
885 out_put(out, (unsigned char *)buffer, strlen(buffer), do_md5);
888 /* Try to determine the codec from the fourcc. */
889 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
890 if ((fourcc & ifaces[i].fourcc_mask) == ifaces[i].fourcc) {
891 vpx_codec_iface_t *ivf_iface = ifaces[i].iface();
893 if (iface && iface != ivf_iface)
894 fprintf(stderr, "Notice -- IVF header indicates codec: %s\n",
895 ifaces[i].name);
896 else
897 iface = ivf_iface;
899 break;
902 dec_flags = (postproc ? VPX_CODEC_USE_POSTPROC : 0);
903 if (vpx_codec_dec_init(&decoder, iface ? iface : ifaces[0].iface(), &cfg,
904 dec_flags)) {
905 fprintf(stderr, "Failed to initialize decoder: %s\n", vpx_codec_error(&decoder));
906 return EXIT_FAILURE;
909 if (!quiet)
910 fprintf(stderr, "%s\n", decoder.name);
912 #if CONFIG_VP8_DECODER
914 if (vp8_pp_cfg.post_proc_flag
915 && vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg)) {
916 fprintf(stderr, "Failed to configure postproc: %s\n", vpx_codec_error(&decoder));
917 return EXIT_FAILURE;
920 if (vp8_dbg_color_ref_frame
921 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_REF_FRAME, vp8_dbg_color_ref_frame)) {
922 fprintf(stderr, "Failed to configure reference block visualizer: %s\n", vpx_codec_error(&decoder));
923 return EXIT_FAILURE;
926 if (vp8_dbg_color_mb_modes
927 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_MB_MODES, vp8_dbg_color_mb_modes)) {
928 fprintf(stderr, "Failed to configure macro block visualizer: %s\n", vpx_codec_error(&decoder));
929 return EXIT_FAILURE;
932 if (vp8_dbg_color_b_modes
933 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_B_MODES, vp8_dbg_color_b_modes)) {
934 fprintf(stderr, "Failed to configure block visualizer: %s\n", vpx_codec_error(&decoder));
935 return EXIT_FAILURE;
938 if (vp8_dbg_display_mv
939 && vpx_codec_control(&decoder, VP8_SET_DBG_DISPLAY_MV, vp8_dbg_display_mv)) {
940 fprintf(stderr, "Failed to configure motion vector visualizer: %s\n", vpx_codec_error(&decoder));
941 return EXIT_FAILURE;
943 #endif
946 if(arg_skip)
947 fprintf(stderr, "Skiping first %d frames.\n", arg_skip);
948 while (arg_skip) {
949 if (read_frame(&input, &buf, &buf_sz, &buf_alloc_sz))
950 break;
951 arg_skip--;
954 /* Decode file */
955 while (!read_frame(&input, &buf, &buf_sz, &buf_alloc_sz)) {
956 vpx_codec_iter_t iter = NULL;
957 vpx_image_t *img;
958 struct vpx_usec_timer timer;
959 int corrupted;
961 vpx_usec_timer_start(&timer);
963 if (vpx_codec_decode(&decoder, buf, buf_sz, NULL, 0)) {
964 const char *detail = vpx_codec_error_detail(&decoder);
965 fprintf(stderr, "Failed to decode frame: %s\n", vpx_codec_error(&decoder));
967 if (detail)
968 fprintf(stderr, " Additional information: %s\n", detail);
970 goto fail;
973 vpx_usec_timer_mark(&timer);
974 dx_time += vpx_usec_timer_elapsed(&timer);
976 ++frame_in;
978 if (vpx_codec_control(&decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted)) {
979 fprintf(stderr, "Failed VP8_GET_FRAME_CORRUPTED: %s\n",
980 vpx_codec_error(&decoder));
981 goto fail;
983 frames_corrupted += corrupted;
985 if ((img = vpx_codec_get_frame(&decoder, &iter)))
986 ++frame_out;
988 if (progress)
989 show_progress(frame_in, frame_out, dx_time);
991 if (!noblit) {
992 if (img) {
993 unsigned int y;
994 char out_fn[PATH_MAX];
995 uint8_t *buf;
997 if (!single_file) {
998 size_t len = sizeof(out_fn) - 1;
1000 out_fn[len] = '\0';
1001 generate_filename(outfile_pattern, out_fn, len - 1,
1002 img->d_w, img->d_h, frame_in);
1003 out = out_open(out_fn, do_md5);
1004 } else if (use_y4m)
1005 out_put(out, (unsigned char *)"FRAME\n", 6, do_md5);
1007 buf = img->planes[VPX_PLANE_Y];
1009 for (y = 0; y < img->d_h; y++) {
1010 out_put(out, buf, img->d_w, do_md5);
1011 buf += img->stride[VPX_PLANE_Y];
1014 buf = img->planes[flipuv ? VPX_PLANE_V : VPX_PLANE_U];
1016 for (y = 0; y < (1 + img->d_h) / 2; y++) {
1017 out_put(out, buf, (1 + img->d_w) / 2, do_md5);
1018 buf += img->stride[VPX_PLANE_U];
1021 buf = img->planes[flipuv ? VPX_PLANE_U : VPX_PLANE_V];
1023 for (y = 0; y < (1 + img->d_h) / 2; y++) {
1024 out_put(out, buf, (1 + img->d_w) / 2, do_md5);
1025 buf += img->stride[VPX_PLANE_V];
1028 if (!single_file)
1029 out_close(out, out_fn, do_md5);
1033 if (stop_after && frame_in >= stop_after)
1034 break;
1037 if (summary || progress) {
1038 show_progress(frame_in, frame_out, dx_time);
1039 fprintf(stderr, "\n");
1042 if (frames_corrupted)
1043 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
1045 fail:
1047 if (vpx_codec_destroy(&decoder)) {
1048 fprintf(stderr, "Failed to destroy decoder: %s\n", vpx_codec_error(&decoder));
1049 return EXIT_FAILURE;
1052 if (single_file && !noblit)
1053 out_close(out, outfile, do_md5);
1055 if (input.nestegg_ctx)
1056 nestegg_destroy(input.nestegg_ctx);
1057 if (input.kind != WEBM_FILE)
1058 free(buf);
1059 fclose(infile);
1060 free(argv);
1062 return frames_corrupted ? EXIT_FAILURE : EXIT_SUCCESS;