blob: 8ee60b6face89b36fb5baa2416bdf5478fd730c3 [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license and patent
5 * grant that can be found in the LICENSE file in the root of the source
6 * tree. All contributing project authors may be found in the AUTHORS
7 * file in the root of the source tree.
8 */
9
10
11/* This is a simple program that reads ivf files and decodes them
12 * using the new interface. Decoded frames are output as YV12 raw.
13 */
14#include <stdio.h>
15#include <stdlib.h>
16#include <stdarg.h>
17#include <string.h>
18#define VPX_CODEC_DISABLE_COMPAT 1
19#include "vpx_config.h"
John Koleszarb7492342010-05-24 11:39:59 -040020#include "vpx/vpx_decoder.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040021#include "vpx_ports/vpx_timer.h"
22#if CONFIG_VP8_DECODER
John Koleszarb7492342010-05-24 11:39:59 -040023#include "vpx/vp8dx.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040024#endif
25#if CONFIG_MD5
26#include "md5_utils.h"
27#endif
28
29static const char *exec_name;
30
31static const struct
32{
33 char const *name;
34 const vpx_codec_iface_t *iface;
35 unsigned int fourcc;
36 unsigned int fourcc_mask;
37} ifaces[] =
38{
39#if CONFIG_VP8_DECODER
40 {"vp8", &vpx_codec_vp8_dx_algo, 0x00385056, 0x00FFFFFF},
41#endif
42};
43
44#include "args.h"
45static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1,
46 "Codec to use");
47static const arg_def_t prefixarg = ARG_DEF("p", "prefix", 1,
48 "Prefix to use when saving frames");
49static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0,
50 "Output file is YV12 ");
51static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0,
52 "Output file is I420 (default)");
53static const arg_def_t flipuvarg = ARG_DEF(NULL, "flipuv", 0,
54 "Synonym for --yv12");
55static const arg_def_t noblitarg = ARG_DEF(NULL, "noblit", 0,
56 "Don't process the decoded frames");
57static const arg_def_t progressarg = ARG_DEF(NULL, "progress", 0,
58 "Show progress after each frame decodes");
59static const arg_def_t limitarg = ARG_DEF(NULL, "limit", 1,
60 "Stop decoding after n frames");
61static const arg_def_t postprocarg = ARG_DEF(NULL, "postproc", 0,
62 "Postprocess decoded frames");
63static const arg_def_t summaryarg = ARG_DEF(NULL, "summary", 0,
64 "Show timing summary");
65static const arg_def_t outputfile = ARG_DEF("o", "output-raw-file", 1,
66 "Output raw yv12 file instead of images");
67static const arg_def_t threadsarg = ARG_DEF("t", "threads", 1,
68 "Max threads to use");
69static const arg_def_t quietarg = ARG_DEF("q", "quiet", 0,
70 "Suppress version string");
71
72#if CONFIG_MD5
73static const arg_def_t md5arg = ARG_DEF(NULL, "md5", 0,
74 "Compute the MD5 sum of the decoded frame");
75#endif
76static const arg_def_t *all_args[] =
77{
78 &codecarg, &prefixarg, &use_yv12, &use_i420, &flipuvarg, &noblitarg,
79 &progressarg, &limitarg, &postprocarg, &summaryarg, &outputfile,
80 &threadsarg, &quietarg,
81#if CONFIG_MD5
82 &md5arg,
83#endif
84 NULL
85};
86
87#if CONFIG_VP8_DECODER
88static const arg_def_t addnoise_level = ARG_DEF(NULL, "noise-level", 1,
89 "Enable VP8 postproc add noise");
90static const arg_def_t deblock = ARG_DEF(NULL, "deblock", 0,
91 "Enable VP8 deblocking");
92static const arg_def_t demacroblock_level = ARG_DEF(NULL, "demacroblock-level", 1,
93 "Enable VP8 demacroblocking, w/ level");
94static const arg_def_t pp_debug_info = ARG_DEF(NULL, "pp-debug-info", 1,
95 "Enable VP8 visible debug info");
96
97
98static const arg_def_t *vp8_pp_args[] =
99{
100 &addnoise_level, &deblock, &demacroblock_level, &pp_debug_info,
101 NULL
102};
103#endif
104
105static void usage_exit()
106{
107 int i;
108
109 fprintf(stderr, "Usage: %s <options> filename\n\n"
110 "Options:\n", exec_name);
111 arg_show_usage(stderr, all_args);
112#if CONFIG_VP8_DECODER
113 fprintf(stderr, "\nvp8 Postprocessing Options:\n");
114 arg_show_usage(stderr, vp8_pp_args);
115#endif
116 fprintf(stderr, "\nIncluded decoders:\n\n");
117
118 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
119 fprintf(stderr, " %-6s - %s\n",
120 ifaces[i].name,
121 vpx_codec_iface_name(ifaces[i].iface));
122
123 exit(EXIT_FAILURE);
124}
125
126void die(const char *fmt, ...)
127{
128 va_list ap;
129 va_start(ap, fmt);
130 vfprintf(stderr, fmt, ap);
131 fprintf(stderr, "\n");
132 usage_exit();
133}
134
135static unsigned int mem_get_le16(const void *vmem)
136{
137 unsigned int val;
138 const unsigned char *mem = (const unsigned char *)vmem;
139
140 val = mem[1] << 8;
141 val |= mem[0];
142 return val;
143}
144
145static unsigned int mem_get_le32(const void *vmem)
146{
147 unsigned int val;
148 const unsigned char *mem = (const unsigned char *)vmem;
149
150 val = mem[3] << 24;
151 val |= mem[2] << 16;
152 val |= mem[1] << 8;
153 val |= mem[0];
154 return val;
155}
156
157#define IVF_FRAME_HDR_SZ (sizeof(uint32_t) + sizeof(uint64_t))
158#define RAW_FRAME_HDR_SZ (sizeof(uint32_t))
159static int read_frame(FILE *infile,
160 uint8_t **buf,
161 uint32_t *buf_sz,
162 uint32_t *buf_alloc_sz,
163 int is_ivf)
164{
165 char raw_hdr[IVF_FRAME_HDR_SZ];
166 uint32_t new_buf_sz;
167
168 /* For both the raw and ivf formats, the frame size is the first 4 bytes
169 * of the frame header. We just need to special case on the header
170 * size.
171 */
172 if (fread(raw_hdr, is_ivf ? IVF_FRAME_HDR_SZ : RAW_FRAME_HDR_SZ, 1,
173 infile) != 1)
174 {
175 if (!feof(infile))
176 fprintf(stderr, "Failed to read frame size\n");
177
178 new_buf_sz = 0;
179 }
180 else
181 {
182 new_buf_sz = mem_get_le32(raw_hdr);
183
184 if (new_buf_sz > 256 * 1024 * 1024)
185 {
186 fprintf(stderr, "Error: Read invalid frame size (%u)\n",
187 new_buf_sz);
188 new_buf_sz = 0;
189 }
190
191 if (!is_ivf && new_buf_sz > 256 * 1024)
192 fprintf(stderr, "Warning: Read invalid frame size (%u)"
193 " - not a raw file?\n", new_buf_sz);
194
195 if (new_buf_sz > *buf_alloc_sz)
196 {
197 uint8_t *new_buf = realloc(*buf, 2 * new_buf_sz);
198
199 if (new_buf)
200 {
201 *buf = new_buf;
202 *buf_alloc_sz = 2 * new_buf_sz;
203 }
204 else
205 {
206 fprintf(stderr, "Failed to allocate compressed data buffer\n");
207 new_buf_sz = 0;
208 }
209 }
210 }
211
212 *buf_sz = new_buf_sz;
213
214 if (*buf_sz)
215 {
216 if (fread(*buf, 1, *buf_sz, infile) != *buf_sz)
217 {
218 fprintf(stderr, "Failed to read full frame\n");
219 return 1;
220 }
221
222 return 0;
223 }
224
225 return 1;
226}
227
228void *out_open(const char *out_fn, int do_md5)
229{
230 void *out = NULL;
231
232 if (do_md5)
233 {
234#if CONFIG_MD5
235 md5_ctx_t *md5_ctx = out = malloc(sizeof(md5_ctx_t));
236 (void)out_fn;
237 md5_init(md5_ctx);
238#endif
239 }
240 else
241 {
242 FILE *outfile = out = strcmp("-", out_fn) ? fopen(out_fn, "wb") : stdout;
243
244 if (!outfile)
245 {
246 fprintf(stderr, "Failed to output file");
247 exit(EXIT_FAILURE);
248 }
249 }
250
251 return out;
252}
253
254void out_put(void *out, const uint8_t *buf, unsigned int len, int do_md5)
255{
256 if (do_md5)
257 {
258#if CONFIG_MD5
259 md5_update(out, buf, len);
260#endif
261 }
262 else
263 {
264 fwrite(buf, 1, len, out);
265 }
266}
267
268void out_close(void *out, const char *out_fn, int do_md5)
269{
270 if (do_md5)
271 {
272#if CONFIG_MD5
273 uint8_t md5[16];
274 int i;
275
276 md5_finalize(out, md5);
277 free(out);
278
279 for (i = 0; i < 16; i++)
280 printf("%02x", md5[i]);
281
282 printf(" %s\n", out_fn);
283#endif
284 }
285 else
286 {
287 fclose(out);
288 }
289}
290
291unsigned int file_is_ivf(FILE *infile, unsigned int *fourcc)
292{
293 char raw_hdr[32];
294 int is_ivf = 0;
295
296 if (fread(raw_hdr, 1, 32, infile) == 32)
297 {
298 if (raw_hdr[0] == 'D' && raw_hdr[1] == 'K'
299 && raw_hdr[2] == 'I' && raw_hdr[3] == 'F')
300 {
301 is_ivf = 1;
302
303 if (mem_get_le16(raw_hdr + 4) != 0)
304 fprintf(stderr, "Error: Unrecognized IVF version! This file may not"
305 " decode properly.");
306
307 *fourcc = mem_get_le32(raw_hdr + 8);
308 }
309 }
310
311 if (!is_ivf)
312 rewind(infile);
313
314 return is_ivf;
315}
316
317int main(int argc, const char **argv_)
318{
319 vpx_codec_ctx_t decoder;
320 char *prefix = NULL, *fn = NULL;
321 int i;
322 uint8_t *buf = NULL;
323 uint32_t buf_sz = 0, buf_alloc_sz = 0;
324 FILE *infile;
325 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0, do_md5 = 0, progress = 0;
326 int stop_after = 0, postproc = 0, summary = 0, quiet = 0;
327 vpx_codec_iface_t *iface = NULL;
328 unsigned int is_ivf, fourcc;
329 unsigned long dx_time = 0;
330 struct arg arg;
331 char **argv, **argi, **argj;
332 const char *fn2 = 0;
333 void *out = NULL;
334 vpx_codec_dec_cfg_t cfg = {0};
335#if CONFIG_VP8_DECODER
336 vp8_postproc_cfg_t vp8_pp_cfg = {0};
337#endif
338
339 /* Parse command line */
340 exec_name = argv_[0];
341 argv = argv_dup(argc - 1, argv_ + 1);
342
343 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
344 {
345 memset(&arg, 0, sizeof(arg));
346 arg.argv_step = 1;
347
348 if (arg_match(&arg, &codecarg, argi))
349 {
350 int j, k = -1;
351
352 for (j = 0; j < sizeof(ifaces) / sizeof(ifaces[0]); j++)
353 if (!strcmp(ifaces[j].name, arg.val))
354 k = j;
355
356 if (k >= 0)
357 iface = ifaces[k].iface;
358 else
359 die("Error: Unrecognized argument (%s) to --codec\n",
360 arg.val);
361 }
362 else if (arg_match(&arg, &outputfile, argi))
363 fn2 = arg.val;
364 else if (arg_match(&arg, &prefixarg, argi))
365 prefix = strdup(arg.val);
366 else if (arg_match(&arg, &use_yv12, argi))
367 flipuv = 1;
368 else if (arg_match(&arg, &use_i420, argi))
369 flipuv = 0;
370 else if (arg_match(&arg, &flipuvarg, argi))
371 flipuv = 1;
372 else if (arg_match(&arg, &noblitarg, argi))
373 noblit = 1;
374 else if (arg_match(&arg, &progressarg, argi))
375 progress = 1;
376 else if (arg_match(&arg, &limitarg, argi))
377 stop_after = arg_parse_uint(&arg);
378 else if (arg_match(&arg, &postprocarg, argi))
379 postproc = 1;
380 else if (arg_match(&arg, &md5arg, argi))
381 do_md5 = 1;
382 else if (arg_match(&arg, &summaryarg, argi))
383 summary = 1;
384 else if (arg_match(&arg, &threadsarg, argi))
385 cfg.threads = arg_parse_uint(&arg);
386 else if (arg_match(&arg, &quietarg, argi))
387 quiet = 1;
388
389#if CONFIG_VP8_DECODER
390 else if (arg_match(&arg, &addnoise_level, argi))
391 {
392 postproc = 1;
393 vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE;
394 vp8_pp_cfg.noise_level = arg_parse_uint(&arg);
395 }
396 else if (arg_match(&arg, &demacroblock_level, argi))
397 {
398 postproc = 1;
399 vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK;
400 vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg);
401 }
402 else if (arg_match(&arg, &deblock, argi))
403 {
404 postproc = 1;
405 vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK;
406 }
407 else if (arg_match(&arg, &pp_debug_info, argi))
408 {
409 unsigned int level = arg_parse_uint(&arg);
410
411 postproc = 1;
412 vp8_pp_cfg.post_proc_flag &= ~0x7;
413
414 if (level)
415 vp8_pp_cfg.post_proc_flag |= 8 << (level - 1);
416 }
417
418#endif
419 else
420 argj++;
421 }
422
423 /* Check for unrecognized options */
424 for (argi = argv; *argi; argi++)
425 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
426 die("Error: Unrecognized option %s\n", *argi);
427
428 /* Handle non-option arguments */
429 fn = argv[0];
430
431 if (!fn)
432 usage_exit();
433
434 if (!prefix)
435 prefix = strdup("img");
436
437 /* Open file */
438 infile = strcmp(fn, "-") ? fopen(fn, "rb") : stdin;
439
440 if (!infile)
441 {
442 fprintf(stderr, "Failed to open file");
443 return EXIT_FAILURE;
444 }
445
446 if (fn2)
447 out = out_open(fn2, do_md5);
448
449 is_ivf = file_is_ivf(infile, &fourcc);
450
451 if (is_ivf)
452 {
453 /* Try to determine the codec from the fourcc. */
454 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
455 if ((fourcc & ifaces[i].fourcc_mask) == ifaces[i].fourcc)
456 {
457 vpx_codec_iface_t *ivf_iface = ifaces[i].iface;
458
459 if (iface && iface != ivf_iface)
460 fprintf(stderr, "Notice -- IVF header indicates codec: %s\n",
461 ifaces[i].name);
462 else
463 iface = ivf_iface;
464
465 break;
466 }
467 }
468
469 if (vpx_codec_dec_init(&decoder, iface ? iface : ifaces[0].iface, &cfg,
470 postproc ? VPX_CODEC_USE_POSTPROC : 0))
471 {
472 fprintf(stderr, "Failed to initialize decoder: %s\n", vpx_codec_error(&decoder));
473 return EXIT_FAILURE;
474 }
475
476 if (!quiet)
477 fprintf(stderr, "%s\n", decoder.name);
478
479#if CONFIG_VP8_DECODER
480
481 if (vp8_pp_cfg.post_proc_flag
482 && vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg))
483 {
484 fprintf(stderr, "Failed to configure postproc: %s\n", vpx_codec_error(&decoder));
485 return EXIT_FAILURE;
486 }
487
488#endif
489
490 /* Decode file */
491 while (!read_frame(infile, &buf, &buf_sz, &buf_alloc_sz, is_ivf))
492 {
493 vpx_codec_iter_t iter = NULL;
494 vpx_image_t *img;
495 struct vpx_usec_timer timer;
496
497 vpx_usec_timer_start(&timer);
498
499 if (vpx_codec_decode(&decoder, buf, buf_sz, NULL, 0))
500 {
501 const char *detail = vpx_codec_error_detail(&decoder);
502 fprintf(stderr, "Failed to decode frame: %s\n", vpx_codec_error(&decoder));
503
504 if (detail)
505 fprintf(stderr, " Additional information: %s\n", detail);
506
507 goto fail;
508 }
509
510 vpx_usec_timer_mark(&timer);
511 dx_time += vpx_usec_timer_elapsed(&timer);
512
513 ++frame_in;
514
515 if (progress)
516 fprintf(stderr, "decoded frame %d.\n", frame_in);
517
518 if ((img = vpx_codec_get_frame(&decoder, &iter)))
519 ++frame_out;
520
521 if (!noblit)
522 {
523 if (img)
524 {
525 unsigned int y;
526 char out_fn[128+24];
527 uint8_t *buf;
528 const char *sfx = flipuv ? "yv12" : "i420";
529
530 if (!fn2)
531 {
532 sprintf(out_fn, "%s-%dx%d-%04d.%s",
533 prefix, img->d_w, img->d_h, frame_in, sfx);
534 out = out_open(out_fn, do_md5);
535 }
536
John Koleszarb6c71912010-05-24 21:45:05 -0400537 buf = img->planes[VPX_PLANE_Y];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400538
539 for (y = 0; y < img->d_h; y++)
540 {
541 out_put(out, buf, img->d_w, do_md5);
John Koleszarb6c71912010-05-24 21:45:05 -0400542 buf += img->stride[VPX_PLANE_Y];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400543 }
544
John Koleszarb6c71912010-05-24 21:45:05 -0400545 buf = img->planes[flipuv?VPX_PLANE_V:VPX_PLANE_U];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400546
547 for (y = 0; y < (1 + img->d_h) / 2; y++)
548 {
549 out_put(out, buf, (1 + img->d_w) / 2, do_md5);
John Koleszarb6c71912010-05-24 21:45:05 -0400550 buf += img->stride[VPX_PLANE_U];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400551 }
552
John Koleszarb6c71912010-05-24 21:45:05 -0400553 buf = img->planes[flipuv?VPX_PLANE_U:VPX_PLANE_V];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400554
555 for (y = 0; y < (1 + img->d_h) / 2; y++)
556 {
557 out_put(out, buf, (1 + img->d_w) / 2, do_md5);
John Koleszarb6c71912010-05-24 21:45:05 -0400558 buf += img->stride[VPX_PLANE_V];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400559 }
560
561 if (!fn2)
562 out_close(out, out_fn, do_md5);
563 }
564 }
565
566 if (stop_after && frame_in >= stop_after)
567 break;
568 }
569
570 if (summary)
571 {
572 fprintf(stderr, "%d decoded frames/%d showed frames in %lu us (%.2f fps)\n",
573 frame_in, frame_out, dx_time, (float)frame_out * 1000000.0 / (float)dx_time);
574 }
575
576fail:
577
578 if (vpx_codec_destroy(&decoder))
579 {
580 fprintf(stderr, "Failed to destroy decoder: %s\n", vpx_codec_error(&decoder));
581 return EXIT_FAILURE;
582 }
583
584 if (fn2)
585 out_close(out, fn2, do_md5);
586
587 free(buf);
588 fclose(infile);
589 free(prefix);
590 free(argv);
591
592 return EXIT_SUCCESS;
593}