WebM VP8 Codec SDK
|
00001 /* 00002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 00003 * 00004 * Use of this source code is governed by a BSD-style license 00005 * that can be found in the LICENSE file in the root of the source 00006 * tree. An additional intellectual property rights grant can be found 00007 * in the file PATENTS. All contributing project authors may 00008 * be found in the AUTHORS file in the root of the source tree. 00009 */ 00010 00011 00012 /* This is a simple program that reads ivf files and decodes them 00013 * using the new interface. Decoded frames are output as YV12 raw. 00014 */ 00015 #include <stdio.h> 00016 #include <stdlib.h> 00017 #include <stdarg.h> 00018 #include <string.h> 00019 #include <limits.h> 00020 00021 #define VPX_CODEC_DISABLE_COMPAT 1 00022 #include "vpx_config.h" 00023 #include "vpx/vpx_decoder.h" 00024 #include "vpx_ports/vpx_timer.h" 00025 #if CONFIG_VP8_DECODER 00026 #include "vpx/vp8dx.h" 00027 #endif 00028 #if CONFIG_MD5 00029 #include "md5_utils.h" 00030 #endif 00031 #include "tools_common.h" 00032 #include "nestegg/include/nestegg/nestegg.h" 00033 00034 #if CONFIG_OS_SUPPORT 00035 #if defined(_WIN32) 00036 #include <io.h> 00037 #define snprintf _snprintf 00038 #define isatty _isatty 00039 #define fileno _fileno 00040 #else 00041 #include <unistd.h> 00042 #endif 00043 #endif 00044 00045 #ifndef PATH_MAX 00046 #define PATH_MAX 256 00047 #endif 00048 00049 static const char *exec_name; 00050 00051 #define VP8_FOURCC (0x00385056) 00052 static const struct 00053 { 00054 char const *name; 00055 const vpx_codec_iface_t *iface; 00056 unsigned int fourcc; 00057 unsigned int fourcc_mask; 00058 } ifaces[] = 00059 { 00060 #if CONFIG_VP8_DECODER 00061 {"vp8", &vpx_codec_vp8_dx_algo, VP8_FOURCC, 0x00FFFFFF}, 00062 #endif 00063 }; 00064 00065 #include "args.h" 00066 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, 00067 "Codec to use"); 00068 static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0, 00069 "Output raw YV12 frames"); 00070 static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0, 00071 "Output raw I420 frames"); 00072 static const arg_def_t flipuvarg = ARG_DEF(NULL, "flipuv", 0, 00073 "Flip the chroma planes in the output"); 00074 static const arg_def_t noblitarg = ARG_DEF(NULL, "noblit", 0, 00075 "Don't process the decoded frames"); 00076 static const arg_def_t progressarg = ARG_DEF(NULL, "progress", 0, 00077 "Show progress after each frame decodes"); 00078 static const arg_def_t limitarg = ARG_DEF(NULL, "limit", 1, 00079 "Stop decoding after n frames"); 00080 static const arg_def_t postprocarg = ARG_DEF(NULL, "postproc", 0, 00081 "Postprocess decoded frames"); 00082 static const arg_def_t summaryarg = ARG_DEF(NULL, "summary", 0, 00083 "Show timing summary"); 00084 static const arg_def_t outputfile = ARG_DEF("o", "output", 1, 00085 "Output file name pattern (see below)"); 00086 static const arg_def_t threadsarg = ARG_DEF("t", "threads", 1, 00087 "Max threads to use"); 00088 static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0, 00089 "Show version string"); 00090 static const arg_def_t error_concealment = ARG_DEF(NULL, "error-concealment", 0, 00091 "Enable decoder error-concealment"); 00092 00093 00094 #if CONFIG_MD5 00095 static const arg_def_t md5arg = ARG_DEF(NULL, "md5", 0, 00096 "Compute the MD5 sum of the decoded frame"); 00097 #endif 00098 static const arg_def_t *all_args[] = 00099 { 00100 &codecarg, &use_yv12, &use_i420, &flipuvarg, &noblitarg, 00101 &progressarg, &limitarg, &postprocarg, &summaryarg, &outputfile, 00102 &threadsarg, &verbosearg, 00103 #if CONFIG_MD5 00104 &md5arg, 00105 #endif 00106 &error_concealment, 00107 NULL 00108 }; 00109 00110 #if CONFIG_VP8_DECODER 00111 static const arg_def_t addnoise_level = ARG_DEF(NULL, "noise-level", 1, 00112 "Enable VP8 postproc add noise"); 00113 static const arg_def_t deblock = ARG_DEF(NULL, "deblock", 0, 00114 "Enable VP8 deblocking"); 00115 static const arg_def_t demacroblock_level = ARG_DEF(NULL, "demacroblock-level", 1, 00116 "Enable VP8 demacroblocking, w/ level"); 00117 static const arg_def_t pp_debug_info = ARG_DEF(NULL, "pp-debug-info", 1, 00118 "Enable VP8 visible debug info"); 00119 static const arg_def_t pp_disp_ref_frame = ARG_DEF(NULL, "pp-dbg-ref-frame", 1, 00120 "Display only selected reference frame per macro block"); 00121 static const arg_def_t pp_disp_mb_modes = ARG_DEF(NULL, "pp-dbg-mb-modes", 1, 00122 "Display only selected macro block modes"); 00123 static const arg_def_t pp_disp_b_modes = ARG_DEF(NULL, "pp-dbg-b-modes", 1, 00124 "Display only selected block modes"); 00125 static const arg_def_t pp_disp_mvs = ARG_DEF(NULL, "pp-dbg-mvs", 1, 00126 "Draw only selected motion vectors"); 00127 00128 static const arg_def_t *vp8_pp_args[] = 00129 { 00130 &addnoise_level, &deblock, &demacroblock_level, &pp_debug_info, 00131 &pp_disp_ref_frame, &pp_disp_mb_modes, &pp_disp_b_modes, &pp_disp_mvs, 00132 NULL 00133 }; 00134 #endif 00135 00136 static void usage_exit() 00137 { 00138 int i; 00139 00140 fprintf(stderr, "Usage: %s <options> filename\n\n" 00141 "Options:\n", exec_name); 00142 arg_show_usage(stderr, all_args); 00143 #if CONFIG_VP8_DECODER 00144 fprintf(stderr, "\nVP8 Postprocessing Options:\n"); 00145 arg_show_usage(stderr, vp8_pp_args); 00146 #endif 00147 fprintf(stderr, 00148 "\nOutput File Patterns:\n\n" 00149 " The -o argument specifies the name of the file(s) to " 00150 "write to. If the\n argument does not include any escape " 00151 "characters, the output will be\n written to a single file. " 00152 "Otherwise, the filename will be calculated by\n expanding " 00153 "the following escape characters:\n" 00154 "\n\t%%w - Frame width" 00155 "\n\t%%h - Frame height" 00156 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)" 00157 "\n\n Pattern arguments are only supported in conjunction " 00158 "with the --yv12 and\n --i420 options. If the -o option is " 00159 "not specified, the output will be\n directed to stdout.\n" 00160 ); 00161 fprintf(stderr, "\nIncluded decoders:\n\n"); 00162 00163 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++) 00164 fprintf(stderr, " %-6s - %s\n", 00165 ifaces[i].name, 00166 vpx_codec_iface_name(ifaces[i].iface)); 00167 00168 exit(EXIT_FAILURE); 00169 } 00170 00171 void die(const char *fmt, ...) 00172 { 00173 va_list ap; 00174 va_start(ap, fmt); 00175 vfprintf(stderr, fmt, ap); 00176 fprintf(stderr, "\n"); 00177 usage_exit(); 00178 } 00179 00180 static unsigned int mem_get_le16(const void *vmem) 00181 { 00182 unsigned int val; 00183 const unsigned char *mem = (const unsigned char *)vmem; 00184 00185 val = mem[1] << 8; 00186 val |= mem[0]; 00187 return val; 00188 } 00189 00190 static unsigned int mem_get_le32(const void *vmem) 00191 { 00192 unsigned int val; 00193 const unsigned char *mem = (const unsigned char *)vmem; 00194 00195 val = mem[3] << 24; 00196 val |= mem[2] << 16; 00197 val |= mem[1] << 8; 00198 val |= mem[0]; 00199 return val; 00200 } 00201 00202 enum file_kind 00203 { 00204 RAW_FILE, 00205 IVF_FILE, 00206 WEBM_FILE 00207 }; 00208 00209 struct input_ctx 00210 { 00211 enum file_kind kind; 00212 FILE *infile; 00213 nestegg *nestegg_ctx; 00214 nestegg_packet *pkt; 00215 unsigned int chunk; 00216 unsigned int chunks; 00217 unsigned int video_track; 00218 }; 00219 00220 #define IVF_FRAME_HDR_SZ (sizeof(uint32_t) + sizeof(uint64_t)) 00221 #define RAW_FRAME_HDR_SZ (sizeof(uint32_t)) 00222 static int read_frame(struct input_ctx *input, 00223 uint8_t **buf, 00224 size_t *buf_sz, 00225 size_t *buf_alloc_sz) 00226 { 00227 char raw_hdr[IVF_FRAME_HDR_SZ]; 00228 size_t new_buf_sz; 00229 FILE *infile = input->infile; 00230 enum file_kind kind = input->kind; 00231 if(kind == WEBM_FILE) 00232 { 00233 if(input->chunk >= input->chunks) 00234 { 00235 unsigned int track; 00236 00237 do 00238 { 00239 /* End of this packet, get another. */ 00240 if(input->pkt) 00241 nestegg_free_packet(input->pkt); 00242 00243 if(nestegg_read_packet(input->nestegg_ctx, &input->pkt) <= 0 00244 || nestegg_packet_track(input->pkt, &track)) 00245 return 1; 00246 00247 } while(track != input->video_track); 00248 00249 if(nestegg_packet_count(input->pkt, &input->chunks)) 00250 return 1; 00251 input->chunk = 0; 00252 } 00253 00254 if(nestegg_packet_data(input->pkt, input->chunk, buf, buf_sz)) 00255 return 1; 00256 input->chunk++; 00257 00258 return 0; 00259 } 00260 /* For both the raw and ivf formats, the frame size is the first 4 bytes 00261 * of the frame header. We just need to special case on the header 00262 * size. 00263 */ 00264 else if (fread(raw_hdr, kind==IVF_FILE 00265 ? IVF_FRAME_HDR_SZ : RAW_FRAME_HDR_SZ, 1, infile) != 1) 00266 { 00267 if (!feof(infile)) 00268 fprintf(stderr, "Failed to read frame size\n"); 00269 00270 new_buf_sz = 0; 00271 } 00272 else 00273 { 00274 new_buf_sz = mem_get_le32(raw_hdr); 00275 00276 if (new_buf_sz > 256 * 1024 * 1024) 00277 { 00278 fprintf(stderr, "Error: Read invalid frame size (%u)\n", 00279 (unsigned int)new_buf_sz); 00280 new_buf_sz = 0; 00281 } 00282 00283 if (kind == RAW_FILE && new_buf_sz > 256 * 1024) 00284 fprintf(stderr, "Warning: Read invalid frame size (%u)" 00285 " - not a raw file?\n", (unsigned int)new_buf_sz); 00286 00287 if (new_buf_sz > *buf_alloc_sz) 00288 { 00289 uint8_t *new_buf = realloc(*buf, 2 * new_buf_sz); 00290 00291 if (new_buf) 00292 { 00293 *buf = new_buf; 00294 *buf_alloc_sz = 2 * new_buf_sz; 00295 } 00296 else 00297 { 00298 fprintf(stderr, "Failed to allocate compressed data buffer\n"); 00299 new_buf_sz = 0; 00300 } 00301 } 00302 } 00303 00304 *buf_sz = new_buf_sz; 00305 00306 if (*buf_sz) 00307 { 00308 if (fread(*buf, 1, *buf_sz, infile) != *buf_sz) 00309 { 00310 fprintf(stderr, "Failed to read full frame\n"); 00311 return 1; 00312 } 00313 00314 return 0; 00315 } 00316 00317 return 1; 00318 } 00319 00320 void *out_open(const char *out_fn, int do_md5) 00321 { 00322 void *out = NULL; 00323 00324 if (do_md5) 00325 { 00326 #if CONFIG_MD5 00327 MD5Context *md5_ctx = out = malloc(sizeof(MD5Context)); 00328 (void)out_fn; 00329 MD5Init(md5_ctx); 00330 #endif 00331 } 00332 else 00333 { 00334 FILE *outfile = out = strcmp("-", out_fn) ? fopen(out_fn, "wb") 00335 : set_binary_mode(stdout); 00336 00337 if (!outfile) 00338 { 00339 fprintf(stderr, "Failed to output file"); 00340 exit(EXIT_FAILURE); 00341 } 00342 } 00343 00344 return out; 00345 } 00346 00347 void out_put(void *out, const uint8_t *buf, unsigned int len, int do_md5) 00348 { 00349 if (do_md5) 00350 { 00351 #if CONFIG_MD5 00352 MD5Update(out, buf, len); 00353 #endif 00354 } 00355 else 00356 { 00357 if(fwrite(buf, 1, len, out)); 00358 } 00359 } 00360 00361 void out_close(void *out, const char *out_fn, int do_md5) 00362 { 00363 if (do_md5) 00364 { 00365 #if CONFIG_MD5 00366 uint8_t md5[16]; 00367 int i; 00368 00369 MD5Final(md5, out); 00370 free(out); 00371 00372 for (i = 0; i < 16; i++) 00373 printf("%02x", md5[i]); 00374 00375 printf(" %s\n", out_fn); 00376 #endif 00377 } 00378 else 00379 { 00380 fclose(out); 00381 } 00382 } 00383 00384 unsigned int file_is_ivf(FILE *infile, 00385 unsigned int *fourcc, 00386 unsigned int *width, 00387 unsigned int *height, 00388 unsigned int *fps_den, 00389 unsigned int *fps_num) 00390 { 00391 char raw_hdr[32]; 00392 int is_ivf = 0; 00393 00394 if (fread(raw_hdr, 1, 32, infile) == 32) 00395 { 00396 if (raw_hdr[0] == 'D' && raw_hdr[1] == 'K' 00397 && raw_hdr[2] == 'I' && raw_hdr[3] == 'F') 00398 { 00399 is_ivf = 1; 00400 00401 if (mem_get_le16(raw_hdr + 4) != 0) 00402 fprintf(stderr, "Error: Unrecognized IVF version! This file may not" 00403 " decode properly."); 00404 00405 *fourcc = mem_get_le32(raw_hdr + 8); 00406 *width = mem_get_le16(raw_hdr + 12); 00407 *height = mem_get_le16(raw_hdr + 14); 00408 *fps_num = mem_get_le32(raw_hdr + 16); 00409 *fps_den = mem_get_le32(raw_hdr + 20); 00410 00411 /* Some versions of vpxenc used 1/(2*fps) for the timebase, so 00412 * we can guess the framerate using only the timebase in this 00413 * case. Other files would require reading ahead to guess the 00414 * timebase, like we do for webm. 00415 */ 00416 if(*fps_num < 1000) 00417 { 00418 /* Correct for the factor of 2 applied to the timebase in the 00419 * encoder. 00420 */ 00421 if(*fps_num&1)*fps_den<<=1; 00422 else *fps_num>>=1; 00423 } 00424 else 00425 { 00426 /* Don't know FPS for sure, and don't have readahead code 00427 * (yet?), so just default to 30fps. 00428 */ 00429 *fps_num = 30; 00430 *fps_den = 1; 00431 } 00432 } 00433 } 00434 00435 if (!is_ivf) 00436 rewind(infile); 00437 00438 return is_ivf; 00439 } 00440 00441 00442 unsigned int file_is_raw(FILE *infile, 00443 unsigned int *fourcc, 00444 unsigned int *width, 00445 unsigned int *height, 00446 unsigned int *fps_den, 00447 unsigned int *fps_num) 00448 { 00449 unsigned char buf[32]; 00450 int is_raw = 0; 00451 vpx_codec_stream_info_t si; 00452 00453 si.sz = sizeof(si); 00454 00455 if (fread(buf, 1, 32, infile) == 32) 00456 { 00457 int i; 00458 00459 if(mem_get_le32(buf) < 256 * 1024 * 1024) 00460 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++) 00461 if(!vpx_codec_peek_stream_info(ifaces[i].iface, 00462 buf + 4, 32 - 4, &si)) 00463 { 00464 is_raw = 1; 00465 *fourcc = ifaces[i].fourcc; 00466 *width = si.w; 00467 *height = si.h; 00468 *fps_num = 30; 00469 *fps_den = 1; 00470 break; 00471 } 00472 } 00473 00474 rewind(infile); 00475 return is_raw; 00476 } 00477 00478 00479 static int 00480 nestegg_read_cb(void *buffer, size_t length, void *userdata) 00481 { 00482 FILE *f = userdata; 00483 00484 if(fread(buffer, 1, length, f) < length) 00485 { 00486 if (ferror(f)) 00487 return -1; 00488 if (feof(f)) 00489 return 0; 00490 } 00491 return 1; 00492 } 00493 00494 00495 static int 00496 nestegg_seek_cb(int64_t offset, int whence, void * userdata) 00497 { 00498 switch(whence) { 00499 case NESTEGG_SEEK_SET: whence = SEEK_SET; break; 00500 case NESTEGG_SEEK_CUR: whence = SEEK_CUR; break; 00501 case NESTEGG_SEEK_END: whence = SEEK_END; break; 00502 }; 00503 return fseek(userdata, offset, whence)? -1 : 0; 00504 } 00505 00506 00507 static int64_t 00508 nestegg_tell_cb(void * userdata) 00509 { 00510 return ftell(userdata); 00511 } 00512 00513 00514 static void 00515 nestegg_log_cb(nestegg * context, unsigned int severity, char const * format, 00516 ...) 00517 { 00518 va_list ap; 00519 00520 va_start(ap, format); 00521 vfprintf(stderr, format, ap); 00522 fprintf(stderr, "\n"); 00523 va_end(ap); 00524 } 00525 00526 00527 static int 00528 webm_guess_framerate(struct input_ctx *input, 00529 unsigned int *fps_den, 00530 unsigned int *fps_num) 00531 { 00532 unsigned int i; 00533 uint64_t tstamp=0; 00534 00535 /* Guess the framerate. Read up to 1 second, or 50 video packets, 00536 * whichever comes first. 00537 */ 00538 for(i=0; tstamp < 1000000000 && i < 50;) 00539 { 00540 nestegg_packet * pkt; 00541 unsigned int track; 00542 00543 if(nestegg_read_packet(input->nestegg_ctx, &pkt) <= 0) 00544 break; 00545 00546 nestegg_packet_track(pkt, &track); 00547 if(track == input->video_track) 00548 { 00549 nestegg_packet_tstamp(pkt, &tstamp); 00550 i++; 00551 } 00552 00553 nestegg_free_packet(pkt); 00554 } 00555 00556 if(nestegg_track_seek(input->nestegg_ctx, input->video_track, 0)) 00557 goto fail; 00558 00559 *fps_num = (i - 1) * 1000000; 00560 *fps_den = tstamp / 1000; 00561 return 0; 00562 fail: 00563 nestegg_destroy(input->nestegg_ctx); 00564 input->nestegg_ctx = NULL; 00565 rewind(input->infile); 00566 return 1; 00567 } 00568 00569 00570 static int 00571 file_is_webm(struct input_ctx *input, 00572 unsigned int *fourcc, 00573 unsigned int *width, 00574 unsigned int *height, 00575 unsigned int *fps_den, 00576 unsigned int *fps_num) 00577 { 00578 unsigned int i, n; 00579 int track_type = -1; 00580 00581 nestegg_io io = {nestegg_read_cb, nestegg_seek_cb, nestegg_tell_cb, 00582 input->infile}; 00583 nestegg_video_params params; 00584 00585 if(nestegg_init(&input->nestegg_ctx, io, NULL)) 00586 goto fail; 00587 00588 if(nestegg_track_count(input->nestegg_ctx, &n)) 00589 goto fail; 00590 00591 for(i=0; i<n; i++) 00592 { 00593 track_type = nestegg_track_type(input->nestegg_ctx, i); 00594 00595 if(track_type == NESTEGG_TRACK_VIDEO) 00596 break; 00597 else if(track_type < 0) 00598 goto fail; 00599 } 00600 00601 if(nestegg_track_codec_id(input->nestegg_ctx, i) != NESTEGG_CODEC_VP8) 00602 { 00603 fprintf(stderr, "Not VP8 video, quitting.\n"); 00604 exit(1); 00605 } 00606 00607 input->video_track = i; 00608 00609 if(nestegg_track_video_params(input->nestegg_ctx, i, ¶ms)) 00610 goto fail; 00611 00612 *fps_den = 0; 00613 *fps_num = 0; 00614 *fourcc = VP8_FOURCC; 00615 *width = params.width; 00616 *height = params.height; 00617 return 1; 00618 fail: 00619 input->nestegg_ctx = NULL; 00620 rewind(input->infile); 00621 return 0; 00622 } 00623 00624 00625 void show_progress(int frame_in, int frame_out, unsigned long dx_time) 00626 { 00627 fprintf(stderr, "%d decoded frames/%d showed frames in %lu us (%.2f fps)\r", 00628 frame_in, frame_out, dx_time, 00629 (float)frame_out * 1000000.0 / (float)dx_time); 00630 } 00631 00632 00633 void generate_filename(const char *pattern, char *out, size_t q_len, 00634 unsigned int d_w, unsigned int d_h, 00635 unsigned int frame_in) 00636 { 00637 const char *p = pattern; 00638 char *q = out; 00639 00640 do 00641 { 00642 char *next_pat = strchr(p, '%'); 00643 00644 if(p == next_pat) 00645 { 00646 size_t pat_len; 00647 00648 // parse the pattern 00649 q[q_len - 1] = '\0'; 00650 switch(p[1]) 00651 { 00652 case 'w': snprintf(q, q_len - 1, "%d", d_w); break; 00653 case 'h': snprintf(q, q_len - 1, "%d", d_h); break; 00654 case '1': snprintf(q, q_len - 1, "%d", frame_in); break; 00655 case '2': snprintf(q, q_len - 1, "%02d", frame_in); break; 00656 case '3': snprintf(q, q_len - 1, "%03d", frame_in); break; 00657 case '4': snprintf(q, q_len - 1, "%04d", frame_in); break; 00658 case '5': snprintf(q, q_len - 1, "%05d", frame_in); break; 00659 case '6': snprintf(q, q_len - 1, "%06d", frame_in); break; 00660 case '7': snprintf(q, q_len - 1, "%07d", frame_in); break; 00661 case '8': snprintf(q, q_len - 1, "%08d", frame_in); break; 00662 case '9': snprintf(q, q_len - 1, "%09d", frame_in); break; 00663 default: 00664 die("Unrecognized pattern %%%c\n", p[1]); 00665 } 00666 00667 pat_len = strlen(q); 00668 if(pat_len >= q_len - 1) 00669 die("Output filename too long.\n"); 00670 q += pat_len; 00671 p += 2; 00672 q_len -= pat_len; 00673 } 00674 else 00675 { 00676 size_t copy_len; 00677 00678 // copy the next segment 00679 if(!next_pat) 00680 copy_len = strlen(p); 00681 else 00682 copy_len = next_pat - p; 00683 00684 if(copy_len >= q_len - 1) 00685 die("Output filename too long.\n"); 00686 00687 memcpy(q, p, copy_len); 00688 q[copy_len] = '\0'; 00689 q += copy_len; 00690 p += copy_len; 00691 q_len -= copy_len; 00692 } 00693 } while(*p); 00694 } 00695 00696 00697 int main(int argc, const char **argv_) 00698 { 00699 vpx_codec_ctx_t decoder; 00700 char *fn = NULL; 00701 int i; 00702 uint8_t *buf = NULL; 00703 size_t buf_sz = 0, buf_alloc_sz = 0; 00704 FILE *infile; 00705 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0, do_md5 = 0, progress = 0; 00706 int stop_after = 0, postproc = 0, summary = 0, quiet = 1; 00707 int ec_enabled = 0; 00708 vpx_codec_iface_t *iface = NULL; 00709 unsigned int fourcc; 00710 unsigned long dx_time = 0; 00711 struct arg arg; 00712 char **argv, **argi, **argj; 00713 const char *outfile_pattern = 0; 00714 char outfile[PATH_MAX]; 00715 int single_file; 00716 int use_y4m = 1; 00717 unsigned int width; 00718 unsigned int height; 00719 unsigned int fps_den; 00720 unsigned int fps_num; 00721 void *out = NULL; 00722 vpx_codec_dec_cfg_t cfg = {0}; 00723 #if CONFIG_VP8_DECODER 00724 vp8_postproc_cfg_t vp8_pp_cfg = {0}; 00725 int vp8_dbg_color_ref_frame = 0; 00726 int vp8_dbg_color_mb_modes = 0; 00727 int vp8_dbg_color_b_modes = 0; 00728 int vp8_dbg_display_mv = 0; 00729 #endif 00730 struct input_ctx input = {0}; 00731 int frames_corrupted = 0; 00732 int dec_flags = 0; 00733 00734 /* Parse command line */ 00735 exec_name = argv_[0]; 00736 argv = argv_dup(argc - 1, argv_ + 1); 00737 00738 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) 00739 { 00740 memset(&arg, 0, sizeof(arg)); 00741 arg.argv_step = 1; 00742 00743 if (arg_match(&arg, &codecarg, argi)) 00744 { 00745 int j, k = -1; 00746 00747 for (j = 0; j < sizeof(ifaces) / sizeof(ifaces[0]); j++) 00748 if (!strcmp(ifaces[j].name, arg.val)) 00749 k = j; 00750 00751 if (k >= 0) 00752 iface = ifaces[k].iface; 00753 else 00754 die("Error: Unrecognized argument (%s) to --codec\n", 00755 arg.val); 00756 } 00757 else if (arg_match(&arg, &outputfile, argi)) 00758 outfile_pattern = arg.val; 00759 else if (arg_match(&arg, &use_yv12, argi)) 00760 { 00761 use_y4m = 0; 00762 flipuv = 1; 00763 } 00764 else if (arg_match(&arg, &use_i420, argi)) 00765 { 00766 use_y4m = 0; 00767 flipuv = 0; 00768 } 00769 else if (arg_match(&arg, &flipuvarg, argi)) 00770 flipuv = 1; 00771 else if (arg_match(&arg, &noblitarg, argi)) 00772 noblit = 1; 00773 else if (arg_match(&arg, &progressarg, argi)) 00774 progress = 1; 00775 else if (arg_match(&arg, &limitarg, argi)) 00776 stop_after = arg_parse_uint(&arg); 00777 else if (arg_match(&arg, &postprocarg, argi)) 00778 postproc = 1; 00779 else if (arg_match(&arg, &md5arg, argi)) 00780 do_md5 = 1; 00781 else if (arg_match(&arg, &summaryarg, argi)) 00782 summary = 1; 00783 else if (arg_match(&arg, &threadsarg, argi)) 00784 cfg.threads = arg_parse_uint(&arg); 00785 else if (arg_match(&arg, &verbosearg, argi)) 00786 quiet = 0; 00787 00788 #if CONFIG_VP8_DECODER 00789 else if (arg_match(&arg, &addnoise_level, argi)) 00790 { 00791 postproc = 1; 00792 vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE; 00793 vp8_pp_cfg.noise_level = arg_parse_uint(&arg); 00794 } 00795 else if (arg_match(&arg, &demacroblock_level, argi)) 00796 { 00797 postproc = 1; 00798 vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK; 00799 vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg); 00800 } 00801 else if (arg_match(&arg, &deblock, argi)) 00802 { 00803 postproc = 1; 00804 vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK; 00805 } 00806 else if (arg_match(&arg, &pp_debug_info, argi)) 00807 { 00808 unsigned int level = arg_parse_uint(&arg); 00809 00810 postproc = 1; 00811 vp8_pp_cfg.post_proc_flag &= ~0x7; 00812 00813 if (level) 00814 vp8_pp_cfg.post_proc_flag |= level; 00815 } 00816 else if (arg_match(&arg, &pp_disp_ref_frame, argi)) 00817 { 00818 unsigned int flags = arg_parse_int(&arg); 00819 if (flags) 00820 { 00821 postproc = 1; 00822 vp8_dbg_color_ref_frame = flags; 00823 } 00824 } 00825 else if (arg_match(&arg, &pp_disp_mb_modes, argi)) 00826 { 00827 unsigned int flags = arg_parse_int(&arg); 00828 if (flags) 00829 { 00830 postproc = 1; 00831 vp8_dbg_color_mb_modes = flags; 00832 } 00833 } 00834 else if (arg_match(&arg, &pp_disp_b_modes, argi)) 00835 { 00836 unsigned int flags = arg_parse_int(&arg); 00837 if (flags) 00838 { 00839 postproc = 1; 00840 vp8_dbg_color_b_modes = flags; 00841 } 00842 } 00843 else if (arg_match(&arg, &pp_disp_mvs, argi)) 00844 { 00845 unsigned int flags = arg_parse_int(&arg); 00846 if (flags) 00847 { 00848 postproc = 1; 00849 vp8_dbg_display_mv = flags; 00850 } 00851 } 00852 else if (arg_match(&arg, &error_concealment, argi)) 00853 { 00854 ec_enabled = 1; 00855 } 00856 00857 #endif 00858 else 00859 argj++; 00860 } 00861 00862 /* Check for unrecognized options */ 00863 for (argi = argv; *argi; argi++) 00864 if (argi[0][0] == '-' && strlen(argi[0]) > 1) 00865 die("Error: Unrecognized option %s\n", *argi); 00866 00867 /* Handle non-option arguments */ 00868 fn = argv[0]; 00869 00870 if (!fn) 00871 usage_exit(); 00872 00873 /* Open file */ 00874 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin); 00875 00876 if (!infile) 00877 { 00878 fprintf(stderr, "Failed to open file '%s'", 00879 strcmp(fn, "-") ? fn : "stdin"); 00880 return EXIT_FAILURE; 00881 } 00882 #if CONFIG_OS_SUPPORT 00883 /* Make sure we don't dump to the terminal, unless forced to with -o - */ 00884 if(!outfile_pattern && isatty(fileno(stdout)) && !do_md5 && !noblit) 00885 { 00886 fprintf(stderr, 00887 "Not dumping raw video to your terminal. Use '-o -' to " 00888 "override.\n"); 00889 return EXIT_FAILURE; 00890 } 00891 #endif 00892 input.infile = infile; 00893 if(file_is_ivf(infile, &fourcc, &width, &height, &fps_den, 00894 &fps_num)) 00895 input.kind = IVF_FILE; 00896 else if(file_is_webm(&input, &fourcc, &width, &height, &fps_den, &fps_num)) 00897 input.kind = WEBM_FILE; 00898 else if(file_is_raw(infile, &fourcc, &width, &height, &fps_den, &fps_num)) 00899 input.kind = RAW_FILE; 00900 else 00901 { 00902 fprintf(stderr, "Unrecognized input file type.\n"); 00903 return EXIT_FAILURE; 00904 } 00905 00906 /* If the output file is not set or doesn't have a sequence number in 00907 * it, then we only open it once. 00908 */ 00909 outfile_pattern = outfile_pattern ? outfile_pattern : "-"; 00910 single_file = 1; 00911 { 00912 const char *p = outfile_pattern; 00913 do 00914 { 00915 p = strchr(p, '%'); 00916 if(p && p[1] >= '1' && p[1] <= '9') 00917 { 00918 // pattern contains sequence number, so it's not unique. 00919 single_file = 0; 00920 break; 00921 } 00922 if(p) 00923 p++; 00924 } while(p); 00925 } 00926 00927 if(single_file && !noblit) 00928 { 00929 generate_filename(outfile_pattern, outfile, sizeof(outfile)-1, 00930 width, height, 0); 00931 out = out_open(outfile, do_md5); 00932 } 00933 00934 if (use_y4m && !noblit) 00935 { 00936 char buffer[128]; 00937 if (!single_file) 00938 { 00939 fprintf(stderr, "YUV4MPEG2 not supported with output patterns," 00940 " try --i420 or --yv12.\n"); 00941 return EXIT_FAILURE; 00942 } 00943 00944 if(input.kind == WEBM_FILE) 00945 if(webm_guess_framerate(&input, &fps_den, &fps_num)) 00946 { 00947 fprintf(stderr, "Failed to guess framerate -- error parsing " 00948 "webm file?\n"); 00949 return EXIT_FAILURE; 00950 } 00951 00952 00953 /*Note: We can't output an aspect ratio here because IVF doesn't 00954 store one, and neither does VP8. 00955 That will have to wait until these tools support WebM natively.*/ 00956 sprintf(buffer, "YUV4MPEG2 C%s W%u H%u F%u:%u I%c\n", 00957 "420jpeg", width, height, fps_num, fps_den, 'p'); 00958 out_put(out, (unsigned char *)buffer, strlen(buffer), do_md5); 00959 } 00960 00961 /* Try to determine the codec from the fourcc. */ 00962 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++) 00963 if ((fourcc & ifaces[i].fourcc_mask) == ifaces[i].fourcc) 00964 { 00965 vpx_codec_iface_t *ivf_iface = ifaces[i].iface; 00966 00967 if (iface && iface != ivf_iface) 00968 fprintf(stderr, "Notice -- IVF header indicates codec: %s\n", 00969 ifaces[i].name); 00970 else 00971 iface = ivf_iface; 00972 00973 break; 00974 } 00975 00976 dec_flags = (postproc ? VPX_CODEC_USE_POSTPROC : 0) | 00977 (ec_enabled ? VPX_CODEC_USE_ERROR_CONCEALMENT : 0); 00978 if (vpx_codec_dec_init(&decoder, iface ? iface : ifaces[0].iface, &cfg, 00979 dec_flags)) 00980 { 00981 fprintf(stderr, "Failed to initialize decoder: %s\n", vpx_codec_error(&decoder)); 00982 return EXIT_FAILURE; 00983 } 00984 00985 if (!quiet) 00986 fprintf(stderr, "%s\n", decoder.name); 00987 00988 #if CONFIG_VP8_DECODER 00989 00990 if (vp8_pp_cfg.post_proc_flag 00991 && vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg)) 00992 { 00993 fprintf(stderr, "Failed to configure postproc: %s\n", vpx_codec_error(&decoder)); 00994 return EXIT_FAILURE; 00995 } 00996 00997 if (vp8_dbg_color_ref_frame 00998 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_REF_FRAME, vp8_dbg_color_ref_frame)) 00999 { 01000 fprintf(stderr, "Failed to configure reference block visualizer: %s\n", vpx_codec_error(&decoder)); 01001 return EXIT_FAILURE; 01002 } 01003 01004 if (vp8_dbg_color_mb_modes 01005 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_MB_MODES, vp8_dbg_color_mb_modes)) 01006 { 01007 fprintf(stderr, "Failed to configure macro block visualizer: %s\n", vpx_codec_error(&decoder)); 01008 return EXIT_FAILURE; 01009 } 01010 01011 if (vp8_dbg_color_b_modes 01012 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_B_MODES, vp8_dbg_color_b_modes)) 01013 { 01014 fprintf(stderr, "Failed to configure block visualizer: %s\n", vpx_codec_error(&decoder)); 01015 return EXIT_FAILURE; 01016 } 01017 01018 if (vp8_dbg_display_mv 01019 && vpx_codec_control(&decoder, VP8_SET_DBG_DISPLAY_MV, vp8_dbg_display_mv)) 01020 { 01021 fprintf(stderr, "Failed to configure motion vector visualizer: %s\n", vpx_codec_error(&decoder)); 01022 return EXIT_FAILURE; 01023 } 01024 #endif 01025 01026 /* Decode file */ 01027 while (!read_frame(&input, &buf, &buf_sz, &buf_alloc_sz)) 01028 { 01029 vpx_codec_iter_t iter = NULL; 01030 vpx_image_t *img; 01031 struct vpx_usec_timer timer; 01032 int corrupted; 01033 01034 vpx_usec_timer_start(&timer); 01035 01036 if (vpx_codec_decode(&decoder, buf, buf_sz, NULL, 0)) 01037 { 01038 const char *detail = vpx_codec_error_detail(&decoder); 01039 fprintf(stderr, "Failed to decode frame: %s\n", vpx_codec_error(&decoder)); 01040 01041 if (detail) 01042 fprintf(stderr, " Additional information: %s\n", detail); 01043 01044 goto fail; 01045 } 01046 01047 vpx_usec_timer_mark(&timer); 01048 dx_time += vpx_usec_timer_elapsed(&timer); 01049 01050 ++frame_in; 01051 01052 if (vpx_codec_control(&decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted)) 01053 { 01054 fprintf(stderr, "Failed VP8_GET_FRAME_CORRUPTED: %s\n", 01055 vpx_codec_error(&decoder)); 01056 goto fail; 01057 } 01058 frames_corrupted += corrupted; 01059 01060 if ((img = vpx_codec_get_frame(&decoder, &iter))) 01061 ++frame_out; 01062 01063 if (progress) 01064 show_progress(frame_in, frame_out, dx_time); 01065 01066 if (!noblit) 01067 { 01068 if (img) 01069 { 01070 unsigned int y; 01071 char out_fn[PATH_MAX]; 01072 uint8_t *buf; 01073 01074 if (!single_file) 01075 { 01076 size_t len = sizeof(out_fn)-1; 01077 01078 out_fn[len] = '\0'; 01079 generate_filename(outfile_pattern, out_fn, len-1, 01080 img->d_w, img->d_h, frame_in); 01081 out = out_open(out_fn, do_md5); 01082 } 01083 else if(use_y4m) 01084 out_put(out, (unsigned char *)"FRAME\n", 6, do_md5); 01085 01086 buf = img->planes[VPX_PLANE_Y]; 01087 01088 for (y = 0; y < img->d_h; y++) 01089 { 01090 out_put(out, buf, img->d_w, do_md5); 01091 buf += img->stride[VPX_PLANE_Y]; 01092 } 01093 01094 buf = img->planes[flipuv?VPX_PLANE_V:VPX_PLANE_U]; 01095 01096 for (y = 0; y < (1 + img->d_h) / 2; y++) 01097 { 01098 out_put(out, buf, (1 + img->d_w) / 2, do_md5); 01099 buf += img->stride[VPX_PLANE_U]; 01100 } 01101 01102 buf = img->planes[flipuv?VPX_PLANE_U:VPX_PLANE_V]; 01103 01104 for (y = 0; y < (1 + img->d_h) / 2; y++) 01105 { 01106 out_put(out, buf, (1 + img->d_w) / 2, do_md5); 01107 buf += img->stride[VPX_PLANE_V]; 01108 } 01109 01110 if (!single_file) 01111 out_close(out, out_fn, do_md5); 01112 } 01113 } 01114 01115 if (stop_after && frame_in >= stop_after) 01116 break; 01117 } 01118 01119 if (summary || progress) 01120 { 01121 show_progress(frame_in, frame_out, dx_time); 01122 fprintf(stderr, "\n"); 01123 } 01124 01125 if (frames_corrupted) 01126 fprintf(stderr, "WARNING: %d frames corrupted.\n",frames_corrupted); 01127 01128 fail: 01129 01130 if (vpx_codec_destroy(&decoder)) 01131 { 01132 fprintf(stderr, "Failed to destroy decoder: %s\n", vpx_codec_error(&decoder)); 01133 return EXIT_FAILURE; 01134 } 01135 01136 if (single_file && !noblit) 01137 out_close(out, outfile, do_md5); 01138 01139 if(input.nestegg_ctx) 01140 nestegg_destroy(input.nestegg_ctx); 01141 if(input.kind != WEBM_FILE) 01142 free(buf); 01143 fclose(infile); 01144 free(argv); 01145 01146 return frames_corrupted ? EXIT_FAILURE : EXIT_SUCCESS; 01147 }