blob: 0de072e8bf120c3853bc8c475b2a6d4987f0114a [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar94c52e42010-06-18 12:39:21 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * 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
John Koleszar94c52e42010-06-18 12:39:21 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
10
11
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.
14 */
15#include <stdio.h>
16#include <stdlib.h>
17#include <stdarg.h>
18#include <string.h>
19#define VPX_CODEC_DISABLE_COMPAT 1
20#include "vpx_config.h"
John Koleszarb7492342010-05-24 11:39:59 -040021#include "vpx/vpx_decoder.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040022#include "vpx_ports/vpx_timer.h"
23#if CONFIG_VP8_DECODER
John Koleszarb7492342010-05-24 11:39:59 -040024#include "vpx/vp8dx.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040025#endif
26#if CONFIG_MD5
27#include "md5_utils.h"
28#endif
John Koleszarad252da2010-10-19 17:20:17 -040029#include "nestegg/include/nestegg/nestegg.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040030
31static const char *exec_name;
32
John Koleszarad252da2010-10-19 17:20:17 -040033#define VP8_FOURCC (0x00385056)
John Koleszar0ea50ce2010-05-18 11:58:33 -040034static const struct
35{
36 char const *name;
37 const vpx_codec_iface_t *iface;
38 unsigned int fourcc;
39 unsigned int fourcc_mask;
40} ifaces[] =
41{
42#if CONFIG_VP8_DECODER
John Koleszarad252da2010-10-19 17:20:17 -040043 {"vp8", &vpx_codec_vp8_dx_algo, VP8_FOURCC, 0x00FFFFFF},
John Koleszar0ea50ce2010-05-18 11:58:33 -040044#endif
45};
46
47#include "args.h"
48static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1,
49 "Codec to use");
50static const arg_def_t prefixarg = ARG_DEF("p", "prefix", 1,
51 "Prefix to use when saving frames");
52static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0,
53 "Output file is YV12 ");
54static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0,
55 "Output file is I420 (default)");
56static const arg_def_t flipuvarg = ARG_DEF(NULL, "flipuv", 0,
57 "Synonym for --yv12");
58static const arg_def_t noblitarg = ARG_DEF(NULL, "noblit", 0,
59 "Don't process the decoded frames");
60static const arg_def_t progressarg = ARG_DEF(NULL, "progress", 0,
61 "Show progress after each frame decodes");
62static const arg_def_t limitarg = ARG_DEF(NULL, "limit", 1,
63 "Stop decoding after n frames");
64static const arg_def_t postprocarg = ARG_DEF(NULL, "postproc", 0,
65 "Postprocess decoded frames");
66static const arg_def_t summaryarg = ARG_DEF(NULL, "summary", 0,
67 "Show timing summary");
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -040068static const arg_def_t outputfile = ARG_DEF("o", "output", 1,
John Koleszar0ea50ce2010-05-18 11:58:33 -040069 "Output raw yv12 file instead of images");
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -040070static const arg_def_t usey4marg = ARG_DEF("y", "y4m", 0,
71 "Output file is YUV4MPEG2");
John Koleszar0ea50ce2010-05-18 11:58:33 -040072static const arg_def_t threadsarg = ARG_DEF("t", "threads", 1,
73 "Max threads to use");
74static const arg_def_t quietarg = ARG_DEF("q", "quiet", 0,
75 "Suppress version string");
76
77#if CONFIG_MD5
78static const arg_def_t md5arg = ARG_DEF(NULL, "md5", 0,
79 "Compute the MD5 sum of the decoded frame");
80#endif
81static const arg_def_t *all_args[] =
82{
83 &codecarg, &prefixarg, &use_yv12, &use_i420, &flipuvarg, &noblitarg,
84 &progressarg, &limitarg, &postprocarg, &summaryarg, &outputfile,
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -040085 &usey4marg, &threadsarg, &quietarg,
John Koleszar0ea50ce2010-05-18 11:58:33 -040086#if CONFIG_MD5
87 &md5arg,
88#endif
89 NULL
90};
91
92#if CONFIG_VP8_DECODER
93static const arg_def_t addnoise_level = ARG_DEF(NULL, "noise-level", 1,
94 "Enable VP8 postproc add noise");
95static const arg_def_t deblock = ARG_DEF(NULL, "deblock", 0,
96 "Enable VP8 deblocking");
97static const arg_def_t demacroblock_level = ARG_DEF(NULL, "demacroblock-level", 1,
98 "Enable VP8 demacroblocking, w/ level");
99static const arg_def_t pp_debug_info = ARG_DEF(NULL, "pp-debug-info", 1,
100 "Enable VP8 visible debug info");
101
102
103static const arg_def_t *vp8_pp_args[] =
104{
105 &addnoise_level, &deblock, &demacroblock_level, &pp_debug_info,
106 NULL
107};
108#endif
109
110static void usage_exit()
111{
112 int i;
113
114 fprintf(stderr, "Usage: %s <options> filename\n\n"
115 "Options:\n", exec_name);
116 arg_show_usage(stderr, all_args);
117#if CONFIG_VP8_DECODER
John Koleszar78f2d3e2010-10-11 09:41:14 -0400118 fprintf(stderr, "\nVP8 Postprocessing Options:\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400119 arg_show_usage(stderr, vp8_pp_args);
120#endif
121 fprintf(stderr, "\nIncluded decoders:\n\n");
122
123 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
124 fprintf(stderr, " %-6s - %s\n",
125 ifaces[i].name,
126 vpx_codec_iface_name(ifaces[i].iface));
127
128 exit(EXIT_FAILURE);
129}
130
131void die(const char *fmt, ...)
132{
133 va_list ap;
134 va_start(ap, fmt);
135 vfprintf(stderr, fmt, ap);
136 fprintf(stderr, "\n");
137 usage_exit();
138}
139
140static unsigned int mem_get_le16(const void *vmem)
141{
142 unsigned int val;
143 const unsigned char *mem = (const unsigned char *)vmem;
144
145 val = mem[1] << 8;
146 val |= mem[0];
147 return val;
148}
149
150static unsigned int mem_get_le32(const void *vmem)
151{
152 unsigned int val;
153 const unsigned char *mem = (const unsigned char *)vmem;
154
155 val = mem[3] << 24;
156 val |= mem[2] << 16;
157 val |= mem[1] << 8;
158 val |= mem[0];
159 return val;
160}
161
John Koleszarad252da2010-10-19 17:20:17 -0400162enum file_kind
163{
164 RAW_FILE,
165 IVF_FILE,
166 WEBM_FILE
167};
168
169struct input_ctx
170{
171 enum file_kind kind;
172 FILE *infile;
173 nestegg *nestegg_ctx;
174 nestegg_packet *pkt;
175 unsigned int chunk;
176 unsigned int chunks;
177 unsigned int video_track;
178};
179
John Koleszar0ea50ce2010-05-18 11:58:33 -0400180#define IVF_FRAME_HDR_SZ (sizeof(uint32_t) + sizeof(uint64_t))
181#define RAW_FRAME_HDR_SZ (sizeof(uint32_t))
John Koleszarad252da2010-10-19 17:20:17 -0400182static int read_frame(struct input_ctx *input,
John Koleszar0ea50ce2010-05-18 11:58:33 -0400183 uint8_t **buf,
John Koleszarad252da2010-10-19 17:20:17 -0400184 size_t *buf_sz,
185 size_t *buf_alloc_sz)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400186{
John Koleszarad252da2010-10-19 17:20:17 -0400187 char raw_hdr[IVF_FRAME_HDR_SZ];
188 size_t new_buf_sz;
189 FILE *infile = input->infile;
190 enum file_kind kind = input->kind;
191 if(kind == WEBM_FILE)
192 {
193 if(input->chunk >= input->chunks)
194 {
195 unsigned int track;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400196
John Koleszarad252da2010-10-19 17:20:17 -0400197 do
198 {
199 /* End of this packet, get another. */
200 if(input->pkt)
201 nestegg_free_packet(input->pkt);
202
203 if(nestegg_read_packet(input->nestegg_ctx, &input->pkt) <= 0
204 || nestegg_packet_track(input->pkt, &track))
205 return 1;
206
207 } while(track != input->video_track);
208
209 if(nestegg_packet_count(input->pkt, &input->chunks))
210 return 1;
211 input->chunk = 0;
212 }
213
214 if(nestegg_packet_data(input->pkt, input->chunk, buf, buf_sz))
215 return 1;
216 input->chunk++;
217
218 return 0;
219 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400220 /* For both the raw and ivf formats, the frame size is the first 4 bytes
221 * of the frame header. We just need to special case on the header
222 * size.
223 */
John Koleszarad252da2010-10-19 17:20:17 -0400224 else if (fread(raw_hdr, kind==IVF_FILE
225 ? IVF_FRAME_HDR_SZ : RAW_FRAME_HDR_SZ, 1, infile) != 1)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400226 {
227 if (!feof(infile))
228 fprintf(stderr, "Failed to read frame size\n");
229
230 new_buf_sz = 0;
231 }
232 else
233 {
234 new_buf_sz = mem_get_le32(raw_hdr);
235
236 if (new_buf_sz > 256 * 1024 * 1024)
237 {
238 fprintf(stderr, "Error: Read invalid frame size (%u)\n",
John Koleszarad252da2010-10-19 17:20:17 -0400239 (unsigned int)new_buf_sz);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400240 new_buf_sz = 0;
241 }
242
John Koleszarad252da2010-10-19 17:20:17 -0400243 if (kind == RAW_FILE && new_buf_sz > 256 * 1024)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400244 fprintf(stderr, "Warning: Read invalid frame size (%u)"
John Koleszarad252da2010-10-19 17:20:17 -0400245 " - not a raw file?\n", (unsigned int)new_buf_sz);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400246
247 if (new_buf_sz > *buf_alloc_sz)
248 {
249 uint8_t *new_buf = realloc(*buf, 2 * new_buf_sz);
250
251 if (new_buf)
252 {
253 *buf = new_buf;
254 *buf_alloc_sz = 2 * new_buf_sz;
255 }
256 else
257 {
258 fprintf(stderr, "Failed to allocate compressed data buffer\n");
259 new_buf_sz = 0;
260 }
261 }
262 }
263
264 *buf_sz = new_buf_sz;
265
266 if (*buf_sz)
267 {
268 if (fread(*buf, 1, *buf_sz, infile) != *buf_sz)
269 {
270 fprintf(stderr, "Failed to read full frame\n");
271 return 1;
272 }
273
274 return 0;
275 }
276
277 return 1;
278}
279
280void *out_open(const char *out_fn, int do_md5)
281{
282 void *out = NULL;
283
284 if (do_md5)
285 {
286#if CONFIG_MD5
Andres Mejia1856f222010-06-14 01:27:33 -0400287 MD5Context *md5_ctx = out = malloc(sizeof(MD5Context));
John Koleszar0ea50ce2010-05-18 11:58:33 -0400288 (void)out_fn;
Andres Mejia1856f222010-06-14 01:27:33 -0400289 MD5Init(md5_ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400290#endif
291 }
292 else
293 {
294 FILE *outfile = out = strcmp("-", out_fn) ? fopen(out_fn, "wb") : stdout;
295
296 if (!outfile)
297 {
298 fprintf(stderr, "Failed to output file");
299 exit(EXIT_FAILURE);
300 }
301 }
302
303 return out;
304}
305
306void out_put(void *out, const uint8_t *buf, unsigned int len, int do_md5)
307{
308 if (do_md5)
309 {
310#if CONFIG_MD5
Andres Mejia1856f222010-06-14 01:27:33 -0400311 MD5Update(out, buf, len);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400312#endif
313 }
314 else
315 {
316 fwrite(buf, 1, len, out);
317 }
318}
319
320void out_close(void *out, const char *out_fn, int do_md5)
321{
322 if (do_md5)
323 {
324#if CONFIG_MD5
325 uint8_t md5[16];
326 int i;
327
Andres Mejia1856f222010-06-14 01:27:33 -0400328 MD5Final(md5, out);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400329 free(out);
330
331 for (i = 0; i < 16; i++)
332 printf("%02x", md5[i]);
333
334 printf(" %s\n", out_fn);
335#endif
336 }
337 else
338 {
339 fclose(out);
340 }
341}
342
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -0400343unsigned int file_is_ivf(FILE *infile,
344 unsigned int *fourcc,
345 unsigned int *width,
346 unsigned int *height,
John Koleszarad252da2010-10-19 17:20:17 -0400347 unsigned int *fps_den,
348 unsigned int *fps_num)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400349{
350 char raw_hdr[32];
351 int is_ivf = 0;
352
353 if (fread(raw_hdr, 1, 32, infile) == 32)
354 {
355 if (raw_hdr[0] == 'D' && raw_hdr[1] == 'K'
356 && raw_hdr[2] == 'I' && raw_hdr[3] == 'F')
357 {
358 is_ivf = 1;
359
360 if (mem_get_le16(raw_hdr + 4) != 0)
361 fprintf(stderr, "Error: Unrecognized IVF version! This file may not"
362 " decode properly.");
363
364 *fourcc = mem_get_le32(raw_hdr + 8);
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -0400365 *width = mem_get_le16(raw_hdr + 12);
366 *height = mem_get_le16(raw_hdr + 14);
John Koleszarad252da2010-10-19 17:20:17 -0400367 *fps_num = mem_get_le32(raw_hdr + 16);
368 *fps_den = mem_get_le32(raw_hdr + 20);
369
John Koleszarea68ee02010-10-21 15:02:10 -0400370 /* Some versions of vpxenc used 1/(2*fps) for the timebase, so
John Koleszarad252da2010-10-19 17:20:17 -0400371 * we can guess the framerate using only the timebase in this
372 * case. Other files would require reading ahead to guess the
373 * timebase, like we do for webm.
374 */
375 if(*fps_num < 1000)
376 {
377 /* Correct for the factor of 2 applied to the timebase in the
378 * encoder.
379 */
380 if(*fps_num&1)*fps_den<<=1;
381 else *fps_num>>=1;
382 }
383 else
384 {
385 /* Don't know FPS for sure, and don't have readahead code
386 * (yet?), so just default to 30fps.
387 */
388 *fps_num = 30;
389 *fps_den = 1;
390 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400391 }
392 }
393
394 if (!is_ivf)
395 rewind(infile);
396
397 return is_ivf;
398}
399
John Koleszarad252da2010-10-19 17:20:17 -0400400
John Koleszarcfe3f912010-10-20 10:49:12 -0400401unsigned int file_is_raw(FILE *infile,
402 unsigned int *fourcc,
403 unsigned int *width,
404 unsigned int *height,
405 unsigned int *fps_den,
406 unsigned int *fps_num)
407{
408 unsigned char buf[32];
409 int is_raw = 0;
410 vpx_codec_stream_info_t si;
411
412 if (fread(buf, 1, 32, infile) == 32)
413 {
414 int i;
415
416 if(mem_get_le32(buf) < 256 * 1024 * 1024)
417 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
418 if(!vpx_codec_peek_stream_info(ifaces[i].iface,
419 buf + 4, 32 - 4, &si))
420 {
421 is_raw = 1;
422 *fourcc = ifaces[i].fourcc;
423 *width = si.w;
424 *height = si.h;
425 *fps_num = 30;
426 *fps_den = 1;
427 break;
428 }
429 }
430
431 rewind(infile);
432 return is_raw;
433}
434
435
John Koleszarad252da2010-10-19 17:20:17 -0400436static int
437nestegg_read_cb(void *buffer, size_t length, void *userdata)
438{
439 FILE *f = userdata;
440
441 fread(buffer, 1, length, f);
442 if (ferror(f))
443 return -1;
444 if (feof(f))
445 return 0;
446 return 1;
447}
448
449
450static int
451nestegg_seek_cb(int64_t offset, int whence, void * userdata)
452{
453 switch(whence) {
454 case NESTEGG_SEEK_SET: whence = SEEK_SET; break;
455 case NESTEGG_SEEK_CUR: whence = SEEK_CUR; break;
456 case NESTEGG_SEEK_END: whence = SEEK_END; break;
457 };
458 return fseek(userdata, offset, whence)? -1 : 0;
459}
460
461
462static int64_t
463nestegg_tell_cb(void * userdata)
464{
465 return ftell(userdata);
466}
467
468
469static void
470nestegg_log_cb(nestegg * context, unsigned int severity, char const * format,
471 ...)
472{
473 va_list ap;
474
475 va_start(ap, format);
476 vfprintf(stderr, format, ap);
477 fprintf(stderr, "\n");
478 va_end(ap);
479}
480
481
482static int
483webm_guess_framerate(struct input_ctx *input,
484 unsigned int *fps_den,
485 unsigned int *fps_num)
486{
487 unsigned int i;
488 uint64_t tstamp=0;
489
490 /* Guess the framerate. Read up to 1 second, or 50 video packets,
491 * whichever comes first.
492 */
493 for(i=0; tstamp < 1000000000 && i < 50;)
494 {
495 nestegg_packet * pkt;
496 unsigned int track;
497
498 if(nestegg_read_packet(input->nestegg_ctx, &pkt) <= 0)
499 break;
500
501 nestegg_packet_track(pkt, &track);
502 if(track == input->video_track)
503 {
504 nestegg_packet_tstamp(pkt, &tstamp);
505 i++;
506 }
507
508 nestegg_free_packet(pkt);
509 }
510
511 if(nestegg_track_seek(input->nestegg_ctx, input->video_track, 0))
512 goto fail;
513
514 *fps_num = (i - 1) * 1000000;
515 *fps_den = tstamp / 1000;
516 return 0;
517fail:
518 input->nestegg_ctx = NULL;
519 rewind(input->infile);
520 return 1;
521}
522
523
524static int
525file_is_webm(struct input_ctx *input,
526 unsigned int *fourcc,
527 unsigned int *width,
528 unsigned int *height,
529 unsigned int *fps_den,
530 unsigned int *fps_num)
531{
532 unsigned int i, n;
533 int track_type = -1;
534 uint64_t tstamp=0;
535
536 nestegg_io io = {nestegg_read_cb, nestegg_seek_cb, nestegg_tell_cb,
537 input->infile};
538 nestegg_video_params params;
539 nestegg_packet * pkt;
540
541 if(nestegg_init(&input->nestegg_ctx, io, NULL))
542 goto fail;
543
544 if(nestegg_track_count(input->nestegg_ctx, &n))
545 goto fail;
546
547 for(i=0; i<n; i++)
548 {
549 track_type = nestegg_track_type(input->nestegg_ctx, i);
550
551 if(track_type == NESTEGG_TRACK_VIDEO)
552 break;
553 else if(track_type < 0)
554 goto fail;
555 }
556
557 if(nestegg_track_codec_id(input->nestegg_ctx, i) != NESTEGG_CODEC_VP8)
558 {
559 fprintf(stderr, "Not VP8 video, quitting.\n");
560 exit(1);
561 }
562
563 input->video_track = i;
564
565 if(nestegg_track_video_params(input->nestegg_ctx, i, &params))
566 goto fail;
567
568 *fps_den = 0;
569 *fps_num = 0;
570 *fourcc = VP8_FOURCC;
571 *width = params.width;
572 *height = params.height;
573 return 1;
574fail:
575 input->nestegg_ctx = NULL;
576 rewind(input->infile);
577 return 0;
578}
579
John Koleszar0ea50ce2010-05-18 11:58:33 -0400580int main(int argc, const char **argv_)
581{
582 vpx_codec_ctx_t decoder;
583 char *prefix = NULL, *fn = NULL;
584 int i;
585 uint8_t *buf = NULL;
John Koleszarad252da2010-10-19 17:20:17 -0400586 size_t buf_sz = 0, buf_alloc_sz = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400587 FILE *infile;
588 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0, do_md5 = 0, progress = 0;
589 int stop_after = 0, postproc = 0, summary = 0, quiet = 0;
590 vpx_codec_iface_t *iface = NULL;
John Koleszarad252da2010-10-19 17:20:17 -0400591 unsigned int fourcc;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400592 unsigned long dx_time = 0;
593 struct arg arg;
594 char **argv, **argi, **argj;
595 const char *fn2 = 0;
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -0400596 int use_y4m = 0;
597 unsigned int width;
598 unsigned int height;
John Koleszarad252da2010-10-19 17:20:17 -0400599 unsigned int fps_den;
600 unsigned int fps_num;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400601 void *out = NULL;
602 vpx_codec_dec_cfg_t cfg = {0};
603#if CONFIG_VP8_DECODER
604 vp8_postproc_cfg_t vp8_pp_cfg = {0};
605#endif
John Koleszarad252da2010-10-19 17:20:17 -0400606 struct input_ctx input = {0};
John Koleszar0ea50ce2010-05-18 11:58:33 -0400607
608 /* Parse command line */
609 exec_name = argv_[0];
610 argv = argv_dup(argc - 1, argv_ + 1);
611
612 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
613 {
614 memset(&arg, 0, sizeof(arg));
615 arg.argv_step = 1;
616
617 if (arg_match(&arg, &codecarg, argi))
618 {
619 int j, k = -1;
620
621 for (j = 0; j < sizeof(ifaces) / sizeof(ifaces[0]); j++)
622 if (!strcmp(ifaces[j].name, arg.val))
623 k = j;
624
625 if (k >= 0)
626 iface = ifaces[k].iface;
627 else
628 die("Error: Unrecognized argument (%s) to --codec\n",
629 arg.val);
630 }
631 else if (arg_match(&arg, &outputfile, argi))
632 fn2 = arg.val;
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -0400633 else if (arg_match(&arg, &usey4marg, argi))
634 use_y4m = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400635 else if (arg_match(&arg, &prefixarg, argi))
636 prefix = strdup(arg.val);
637 else if (arg_match(&arg, &use_yv12, argi))
638 flipuv = 1;
639 else if (arg_match(&arg, &use_i420, argi))
640 flipuv = 0;
641 else if (arg_match(&arg, &flipuvarg, argi))
642 flipuv = 1;
643 else if (arg_match(&arg, &noblitarg, argi))
644 noblit = 1;
645 else if (arg_match(&arg, &progressarg, argi))
646 progress = 1;
647 else if (arg_match(&arg, &limitarg, argi))
648 stop_after = arg_parse_uint(&arg);
649 else if (arg_match(&arg, &postprocarg, argi))
650 postproc = 1;
651 else if (arg_match(&arg, &md5arg, argi))
652 do_md5 = 1;
653 else if (arg_match(&arg, &summaryarg, argi))
654 summary = 1;
655 else if (arg_match(&arg, &threadsarg, argi))
656 cfg.threads = arg_parse_uint(&arg);
657 else if (arg_match(&arg, &quietarg, argi))
658 quiet = 1;
659
660#if CONFIG_VP8_DECODER
661 else if (arg_match(&arg, &addnoise_level, argi))
662 {
663 postproc = 1;
664 vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE;
665 vp8_pp_cfg.noise_level = arg_parse_uint(&arg);
666 }
667 else if (arg_match(&arg, &demacroblock_level, argi))
668 {
669 postproc = 1;
670 vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK;
671 vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg);
672 }
673 else if (arg_match(&arg, &deblock, argi))
674 {
675 postproc = 1;
676 vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK;
677 }
678 else if (arg_match(&arg, &pp_debug_info, argi))
679 {
680 unsigned int level = arg_parse_uint(&arg);
681
682 postproc = 1;
683 vp8_pp_cfg.post_proc_flag &= ~0x7;
684
685 if (level)
686 vp8_pp_cfg.post_proc_flag |= 8 << (level - 1);
687 }
688
689#endif
690 else
691 argj++;
692 }
693
694 /* Check for unrecognized options */
695 for (argi = argv; *argi; argi++)
696 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
697 die("Error: Unrecognized option %s\n", *argi);
698
699 /* Handle non-option arguments */
700 fn = argv[0];
701
702 if (!fn)
703 usage_exit();
704
705 if (!prefix)
706 prefix = strdup("img");
707
708 /* Open file */
709 infile = strcmp(fn, "-") ? fopen(fn, "rb") : stdin;
710
711 if (!infile)
712 {
713 fprintf(stderr, "Failed to open file");
714 return EXIT_FAILURE;
715 }
716
717 if (fn2)
718 out = out_open(fn2, do_md5);
719
John Koleszarad252da2010-10-19 17:20:17 -0400720 input.infile = infile;
721 if(file_is_ivf(infile, &fourcc, &width, &height, &fps_den,
722 &fps_num))
723 input.kind = IVF_FILE;
John Koleszarcfe3f912010-10-20 10:49:12 -0400724 else if(file_is_webm(&input, &fourcc, &width, &height, &fps_den, &fps_num))
John Koleszarad252da2010-10-19 17:20:17 -0400725 input.kind = WEBM_FILE;
John Koleszarcfe3f912010-10-20 10:49:12 -0400726 else if(file_is_raw(infile, &fourcc, &width, &height, &fps_den, &fps_num))
John Koleszarad252da2010-10-19 17:20:17 -0400727 input.kind = RAW_FILE;
John Koleszarcfe3f912010-10-20 10:49:12 -0400728 else
John Koleszar0ea50ce2010-05-18 11:58:33 -0400729 {
John Koleszarcfe3f912010-10-20 10:49:12 -0400730 fprintf(stderr, "Unrecognized input file type.\n");
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -0400731 return EXIT_FAILURE;
732 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400733
John Koleszarcfe3f912010-10-20 10:49:12 -0400734 if (use_y4m)
735 {
736 char buffer[128];
737 if (!fn2)
738 {
739 fprintf(stderr, "YUV4MPEG2 output only supported with -o.\n");
740 return EXIT_FAILURE;
741 }
742
743 if(input.kind == WEBM_FILE)
744 webm_guess_framerate(&input, &fps_den, &fps_num);
745
746 /*Note: We can't output an aspect ratio here because IVF doesn't
747 store one, and neither does VP8.
748 That will have to wait until these tools support WebM natively.*/
749 sprintf(buffer, "YUV4MPEG2 C%s W%u H%u F%u:%u I%c\n",
750 "420jpeg", width, height, fps_num, fps_den, 'p');
751 out_put(out, (unsigned char *)buffer, strlen(buffer), do_md5);
752 }
753
754 /* Try to determine the codec from the fourcc. */
755 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
756 if ((fourcc & ifaces[i].fourcc_mask) == ifaces[i].fourcc)
757 {
758 vpx_codec_iface_t *ivf_iface = ifaces[i].iface;
759
760 if (iface && iface != ivf_iface)
761 fprintf(stderr, "Notice -- IVF header indicates codec: %s\n",
762 ifaces[i].name);
763 else
764 iface = ivf_iface;
765
766 break;
767 }
768
John Koleszar0ea50ce2010-05-18 11:58:33 -0400769 if (vpx_codec_dec_init(&decoder, iface ? iface : ifaces[0].iface, &cfg,
770 postproc ? VPX_CODEC_USE_POSTPROC : 0))
771 {
772 fprintf(stderr, "Failed to initialize decoder: %s\n", vpx_codec_error(&decoder));
773 return EXIT_FAILURE;
774 }
775
776 if (!quiet)
777 fprintf(stderr, "%s\n", decoder.name);
778
779#if CONFIG_VP8_DECODER
780
781 if (vp8_pp_cfg.post_proc_flag
782 && vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg))
783 {
784 fprintf(stderr, "Failed to configure postproc: %s\n", vpx_codec_error(&decoder));
785 return EXIT_FAILURE;
786 }
787
788#endif
789
790 /* Decode file */
John Koleszarad252da2010-10-19 17:20:17 -0400791 while (!read_frame(&input, &buf, &buf_sz, &buf_alloc_sz))
John Koleszar0ea50ce2010-05-18 11:58:33 -0400792 {
793 vpx_codec_iter_t iter = NULL;
794 vpx_image_t *img;
795 struct vpx_usec_timer timer;
796
797 vpx_usec_timer_start(&timer);
798
799 if (vpx_codec_decode(&decoder, buf, buf_sz, NULL, 0))
800 {
801 const char *detail = vpx_codec_error_detail(&decoder);
802 fprintf(stderr, "Failed to decode frame: %s\n", vpx_codec_error(&decoder));
803
804 if (detail)
805 fprintf(stderr, " Additional information: %s\n", detail);
806
807 goto fail;
808 }
809
810 vpx_usec_timer_mark(&timer);
811 dx_time += vpx_usec_timer_elapsed(&timer);
812
813 ++frame_in;
814
815 if (progress)
816 fprintf(stderr, "decoded frame %d.\n", frame_in);
817
818 if ((img = vpx_codec_get_frame(&decoder, &iter)))
819 ++frame_out;
820
821 if (!noblit)
822 {
823 if (img)
824 {
825 unsigned int y;
826 char out_fn[128+24];
827 uint8_t *buf;
828 const char *sfx = flipuv ? "yv12" : "i420";
829
830 if (!fn2)
831 {
832 sprintf(out_fn, "%s-%dx%d-%04d.%s",
833 prefix, img->d_w, img->d_h, frame_in, sfx);
834 out = out_open(out_fn, do_md5);
835 }
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -0400836 else if(use_y4m)
837 out_put(out, (unsigned char *)"FRAME\n", 6, do_md5);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400838
John Koleszarb6c71912010-05-24 21:45:05 -0400839 buf = img->planes[VPX_PLANE_Y];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400840
841 for (y = 0; y < img->d_h; y++)
842 {
843 out_put(out, buf, img->d_w, do_md5);
John Koleszarb6c71912010-05-24 21:45:05 -0400844 buf += img->stride[VPX_PLANE_Y];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400845 }
846
John Koleszarb6c71912010-05-24 21:45:05 -0400847 buf = img->planes[flipuv?VPX_PLANE_V:VPX_PLANE_U];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400848
849 for (y = 0; y < (1 + img->d_h) / 2; y++)
850 {
851 out_put(out, buf, (1 + img->d_w) / 2, do_md5);
John Koleszarb6c71912010-05-24 21:45:05 -0400852 buf += img->stride[VPX_PLANE_U];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400853 }
854
John Koleszarb6c71912010-05-24 21:45:05 -0400855 buf = img->planes[flipuv?VPX_PLANE_U:VPX_PLANE_V];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400856
857 for (y = 0; y < (1 + img->d_h) / 2; y++)
858 {
859 out_put(out, buf, (1 + img->d_w) / 2, do_md5);
John Koleszarb6c71912010-05-24 21:45:05 -0400860 buf += img->stride[VPX_PLANE_V];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400861 }
862
863 if (!fn2)
864 out_close(out, out_fn, do_md5);
865 }
866 }
867
868 if (stop_after && frame_in >= stop_after)
869 break;
870 }
871
872 if (summary)
873 {
874 fprintf(stderr, "%d decoded frames/%d showed frames in %lu us (%.2f fps)\n",
875 frame_in, frame_out, dx_time, (float)frame_out * 1000000.0 / (float)dx_time);
876 }
877
878fail:
879
880 if (vpx_codec_destroy(&decoder))
881 {
882 fprintf(stderr, "Failed to destroy decoder: %s\n", vpx_codec_error(&decoder));
883 return EXIT_FAILURE;
884 }
885
886 if (fn2)
887 out_close(out, fn2, do_md5);
888
John Koleszarad252da2010-10-19 17:20:17 -0400889 if(input.nestegg_ctx)
890 nestegg_destroy(input.nestegg_ctx);
891 if(input.kind != WEBM_FILE)
892 free(buf);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400893 fclose(infile);
894 free(prefix);
895 free(argv);
896
897 return EXIT_SUCCESS;
898}