1 /* 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 3 * 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 */ 10 11 #include <assert.h> 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <stdarg.h> 15 #include <string.h> 16 #include <limits.h> 17 18 #include "third_party/libyuv/include/libyuv/scale.h" 19 20 #include "./args.h" 21 #include "./ivfdec.h" 22 23 #define VPX_CODEC_DISABLE_COMPAT 1 24 #include "./vpx_config.h" 25 #include "vpx/vpx_decoder.h" 26 #include "vpx_ports/mem_ops.h" 27 #include "vpx_ports/vpx_timer.h" 28 29 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER 30 #include "vpx/vp8dx.h" 31 #endif 32 33 #include "./md5_utils.h" 34 35 #include "./tools_common.h" 36 #include "./webmdec.h" 37 #include "./y4menc.h" 38 39 static const char *exec_name; 40 41 struct VpxDecInputContext { 42 struct VpxInputContext *vpx_input_ctx; 43 struct WebmInputContext *webm_ctx; 44 }; 45 46 static const arg_def_t looparg = ARG_DEF(NULL, "loops", 1, 47 "Number of times to decode the file"); 48 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, 49 "Codec to use"); 50 static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0, 51 "Output raw YV12 frames"); 52 static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0, 53 "Output raw I420 frames"); 54 static const arg_def_t flipuvarg = ARG_DEF(NULL, "flipuv", 0, 55 "Flip the chroma planes in the output"); 56 static const arg_def_t noblitarg = ARG_DEF(NULL, "noblit", 0, 57 "Don't process the decoded frames"); 58 static const arg_def_t progressarg = ARG_DEF(NULL, "progress", 0, 59 "Show progress after each frame decodes"); 60 static const arg_def_t limitarg = ARG_DEF(NULL, "limit", 1, 61 "Stop decoding after n frames"); 62 static const arg_def_t skiparg = ARG_DEF(NULL, "skip", 1, 63 "Skip the first n input frames"); 64 static const arg_def_t postprocarg = ARG_DEF(NULL, "postproc", 0, 65 "Postprocess decoded frames"); 66 static const arg_def_t summaryarg = ARG_DEF(NULL, "summary", 0, 67 "Show timing summary"); 68 static const arg_def_t outputfile = ARG_DEF("o", "output", 1, 69 "Output file name pattern (see below)"); 70 static const arg_def_t threadsarg = ARG_DEF("t", "threads", 1, 71 "Max threads to use"); 72 static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0, 73 "Show version string"); 74 static const arg_def_t error_concealment = ARG_DEF(NULL, "error-concealment", 0, 75 "Enable decoder error-concealment"); 76 static const arg_def_t scalearg = ARG_DEF("S", "scale", 0, 77 "Scale output frames uniformly"); 78 79 static const arg_def_t fb_arg = 80 ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use"); 81 82 static const arg_def_t md5arg = ARG_DEF(NULL, "md5", 0, 83 "Compute the MD5 sum of the decoded frame"); 84 85 static const arg_def_t *all_args[] = { 86 &codecarg, &use_yv12, &use_i420, &flipuvarg, &noblitarg, 87 &progressarg, &limitarg, &skiparg, &postprocarg, &summaryarg, &outputfile, 88 &threadsarg, &verbosearg, &scalearg, &fb_arg, 89 &md5arg, 90 &error_concealment, 91 NULL 92 }; 93 94 #if CONFIG_VP8_DECODER 95 static const arg_def_t addnoise_level = ARG_DEF(NULL, "noise-level", 1, 96 "Enable VP8 postproc add noise"); 97 static const arg_def_t deblock = ARG_DEF(NULL, "deblock", 0, 98 "Enable VP8 deblocking"); 99 static const arg_def_t demacroblock_level = ARG_DEF(NULL, "demacroblock-level", 1, 100 "Enable VP8 demacroblocking, w/ level"); 101 static const arg_def_t pp_debug_info = ARG_DEF(NULL, "pp-debug-info", 1, 102 "Enable VP8 visible debug info"); 103 static const arg_def_t pp_disp_ref_frame = ARG_DEF(NULL, "pp-dbg-ref-frame", 1, 104 "Display only selected reference frame per macro block"); 105 static const arg_def_t pp_disp_mb_modes = ARG_DEF(NULL, "pp-dbg-mb-modes", 1, 106 "Display only selected macro block modes"); 107 static const arg_def_t pp_disp_b_modes = ARG_DEF(NULL, "pp-dbg-b-modes", 1, 108 "Display only selected block modes"); 109 static const arg_def_t pp_disp_mvs = ARG_DEF(NULL, "pp-dbg-mvs", 1, 110 "Draw only selected motion vectors"); 111 static const arg_def_t mfqe = ARG_DEF(NULL, "mfqe", 0, 112 "Enable multiframe quality enhancement"); 113 114 static const arg_def_t *vp8_pp_args[] = { 115 &addnoise_level, &deblock, &demacroblock_level, &pp_debug_info, 116 &pp_disp_ref_frame, &pp_disp_mb_modes, &pp_disp_b_modes, &pp_disp_mvs, &mfqe, 117 NULL 118 }; 119 #endif 120 121 static int vpx_image_scale(vpx_image_t *src, vpx_image_t *dst, 122 FilterMode mode) { 123 assert(src->fmt == VPX_IMG_FMT_I420); 124 assert(dst->fmt == VPX_IMG_FMT_I420); 125 return I420Scale(src->planes[VPX_PLANE_Y], src->stride[VPX_PLANE_Y], 126 src->planes[VPX_PLANE_U], src->stride[VPX_PLANE_U], 127 src->planes[VPX_PLANE_V], src->stride[VPX_PLANE_V], 128 src->d_w, src->d_h, 129 dst->planes[VPX_PLANE_Y], dst->stride[VPX_PLANE_Y], 130 dst->planes[VPX_PLANE_U], dst->stride[VPX_PLANE_U], 131 dst->planes[VPX_PLANE_V], dst->stride[VPX_PLANE_V], 132 dst->d_w, dst->d_h, 133 mode); 134 } 135 136 void usage_exit() { 137 int i; 138 139 fprintf(stderr, "Usage: %s <options> filename\n\n" 140 "Options:\n", exec_name); 141 arg_show_usage(stderr, all_args); 142 #if CONFIG_VP8_DECODER 143 fprintf(stderr, "\nVP8 Postprocessing Options:\n"); 144 arg_show_usage(stderr, vp8_pp_args); 145 #endif 146 fprintf(stderr, 147 "\nOutput File Patterns:\n\n" 148 " The -o argument specifies the name of the file(s) to " 149 "write to. If the\n argument does not include any escape " 150 "characters, the output will be\n written to a single file. " 151 "Otherwise, the filename will be calculated by\n expanding " 152 "the following escape characters:\n"); 153 fprintf(stderr, 154 "\n\t%%w - Frame width" 155 "\n\t%%h - Frame height" 156 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)" 157 "\n\n Pattern arguments are only supported in conjunction " 158 "with the --yv12 and\n --i420 options. If the -o option is " 159 "not specified, the output will be\n directed to stdout.\n" 160 ); 161 fprintf(stderr, "\nIncluded decoders:\n\n"); 162 163 for (i = 0; i < get_vpx_decoder_count(); ++i) { 164 const VpxInterface *const decoder = get_vpx_decoder_by_index(i); 165 fprintf(stderr, " %-6s - %s\n", 166 decoder->name, vpx_codec_iface_name(decoder->interface())); 167 } 168 169 exit(EXIT_FAILURE); 170 } 171 172 static int raw_read_frame(FILE *infile, uint8_t **buffer, 173 size_t *bytes_read, size_t *buffer_size) { 174 char raw_hdr[RAW_FRAME_HDR_SZ]; 175 size_t frame_size = 0; 176 177 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) { 178 if (!feof(infile)) 179 warn("Failed to read RAW frame size\n"); 180 } else { 181 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024; 182 const size_t kFrameTooSmallThreshold = 256 * 1024; 183 frame_size = mem_get_le32(raw_hdr); 184 185 if (frame_size > kCorruptFrameThreshold) { 186 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size); 187 frame_size = 0; 188 } 189 190 if (frame_size < kFrameTooSmallThreshold) { 191 warn("Warning: Read invalid frame size (%u) - not a raw file?\n", 192 (unsigned int)frame_size); 193 } 194 195 if (frame_size > *buffer_size) { 196 uint8_t *new_buf = realloc(*buffer, 2 * frame_size); 197 if (new_buf) { 198 *buffer = new_buf; 199 *buffer_size = 2 * frame_size; 200 } else { 201 warn("Failed to allocate compressed data buffer\n"); 202 frame_size = 0; 203 } 204 } 205 } 206 207 if (!feof(infile)) { 208 if (fread(*buffer, 1, frame_size, infile) != frame_size) { 209 warn("Failed to read full frame\n"); 210 return 1; 211 } 212 *bytes_read = frame_size; 213 } 214 215 return 0; 216 } 217 218 static int read_frame(struct VpxDecInputContext *input, uint8_t **buf, 219 size_t *bytes_in_buffer, size_t *buffer_size) { 220 switch (input->vpx_input_ctx->file_type) { 221 #if CONFIG_WEBM_IO 222 case FILE_TYPE_WEBM: 223 return webm_read_frame(input->webm_ctx, 224 buf, bytes_in_buffer, buffer_size); 225 #endif 226 case FILE_TYPE_RAW: 227 return raw_read_frame(input->vpx_input_ctx->file, 228 buf, bytes_in_buffer, buffer_size); 229 case FILE_TYPE_IVF: 230 return ivf_read_frame(input->vpx_input_ctx->file, 231 buf, bytes_in_buffer, buffer_size); 232 default: 233 return 1; 234 } 235 } 236 237 static void update_image_md5(const vpx_image_t *img, const int planes[3], 238 MD5Context *md5) { 239 int i, y; 240 241 for (i = 0; i < 3; ++i) { 242 const int plane = planes[i]; 243 const unsigned char *buf = img->planes[plane]; 244 const int stride = img->stride[plane]; 245 const int w = vpx_img_plane_width(img, plane); 246 const int h = vpx_img_plane_height(img, plane); 247 248 for (y = 0; y < h; ++y) { 249 MD5Update(md5, buf, w); 250 buf += stride; 251 } 252 } 253 } 254 255 static void write_image_file(const vpx_image_t *img, const int planes[3], 256 FILE *file) { 257 int i, y; 258 259 for (i = 0; i < 3; ++i) { 260 const int plane = planes[i]; 261 const unsigned char *buf = img->planes[plane]; 262 const int stride = img->stride[plane]; 263 const int w = vpx_img_plane_width(img, plane); 264 const int h = vpx_img_plane_height(img, plane); 265 266 for (y = 0; y < h; ++y) { 267 fwrite(buf, 1, w, file); 268 buf += stride; 269 } 270 } 271 } 272 273 int file_is_raw(struct VpxInputContext *input) { 274 uint8_t buf[32]; 275 int is_raw = 0; 276 vpx_codec_stream_info_t si; 277 278 si.sz = sizeof(si); 279 280 if (fread(buf, 1, 32, input->file) == 32) { 281 int i; 282 283 if (mem_get_le32(buf) < 256 * 1024 * 1024) { 284 for (i = 0; i < get_vpx_decoder_count(); ++i) { 285 const VpxInterface *const decoder = get_vpx_decoder_by_index(i); 286 if (!vpx_codec_peek_stream_info(decoder->interface(), 287 buf + 4, 32 - 4, &si)) { 288 is_raw = 1; 289 input->fourcc = decoder->fourcc; 290 input->width = si.w; 291 input->height = si.h; 292 input->framerate.numerator = 30; 293 input->framerate.denominator = 1; 294 break; 295 } 296 } 297 } 298 } 299 300 rewind(input->file); 301 return is_raw; 302 } 303 304 void show_progress(int frame_in, int frame_out, uint64_t dx_time) { 305 fprintf(stderr, 306 "%d decoded frames/%d showed frames in %"PRId64" us (%.2f fps)\r", 307 frame_in, frame_out, dx_time, 308 (double)frame_out * 1000000.0 / (double)dx_time); 309 } 310 311 struct ExternalFrameBuffer { 312 uint8_t* data; 313 size_t size; 314 int in_use; 315 }; 316 317 struct ExternalFrameBufferList { 318 int num_external_frame_buffers; 319 struct ExternalFrameBuffer *ext_fb; 320 }; 321 322 // Callback used by libvpx to request an external frame buffer. |cb_priv| 323 // Application private data passed into the set function. |min_size| is the 324 // minimum size in bytes needed to decode the next frame. |fb| pointer to the 325 // frame buffer. 326 int get_vp9_frame_buffer(void *cb_priv, size_t min_size, 327 vpx_codec_frame_buffer_t *fb) { 328 int i; 329 struct ExternalFrameBufferList *const ext_fb_list = 330 (struct ExternalFrameBufferList *)cb_priv; 331 if (ext_fb_list == NULL) 332 return -1; 333 334 // Find a free frame buffer. 335 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) { 336 if (!ext_fb_list->ext_fb[i].in_use) 337 break; 338 } 339 340 if (i == ext_fb_list->num_external_frame_buffers) 341 return -1; 342 343 if (ext_fb_list->ext_fb[i].size < min_size) { 344 free(ext_fb_list->ext_fb[i].data); 345 ext_fb_list->ext_fb[i].data = (uint8_t *)malloc(min_size); 346 if (!ext_fb_list->ext_fb[i].data) 347 return -1; 348 349 ext_fb_list->ext_fb[i].size = min_size; 350 } 351 352 fb->data = ext_fb_list->ext_fb[i].data; 353 fb->size = ext_fb_list->ext_fb[i].size; 354 ext_fb_list->ext_fb[i].in_use = 1; 355 356 // Set the frame buffer's private data to point at the external frame buffer. 357 fb->priv = &ext_fb_list->ext_fb[i]; 358 return 0; 359 } 360 361 // Callback used by libvpx when there are no references to the frame buffer. 362 // |cb_priv| user private data passed into the set function. |fb| pointer 363 // to the frame buffer. 364 int release_vp9_frame_buffer(void *cb_priv, 365 vpx_codec_frame_buffer_t *fb) { 366 struct ExternalFrameBuffer *const ext_fb = 367 (struct ExternalFrameBuffer *)fb->priv; 368 (void)cb_priv; 369 ext_fb->in_use = 0; 370 return 0; 371 } 372 373 void generate_filename(const char *pattern, char *out, size_t q_len, 374 unsigned int d_w, unsigned int d_h, 375 unsigned int frame_in) { 376 const char *p = pattern; 377 char *q = out; 378 379 do { 380 char *next_pat = strchr(p, '%'); 381 382 if (p == next_pat) { 383 size_t pat_len; 384 385 /* parse the pattern */ 386 q[q_len - 1] = '\0'; 387 switch (p[1]) { 388 case 'w': 389 snprintf(q, q_len - 1, "%d", d_w); 390 break; 391 case 'h': 392 snprintf(q, q_len - 1, "%d", d_h); 393 break; 394 case '1': 395 snprintf(q, q_len - 1, "%d", frame_in); 396 break; 397 case '2': 398 snprintf(q, q_len - 1, "%02d", frame_in); 399 break; 400 case '3': 401 snprintf(q, q_len - 1, "%03d", frame_in); 402 break; 403 case '4': 404 snprintf(q, q_len - 1, "%04d", frame_in); 405 break; 406 case '5': 407 snprintf(q, q_len - 1, "%05d", frame_in); 408 break; 409 case '6': 410 snprintf(q, q_len - 1, "%06d", frame_in); 411 break; 412 case '7': 413 snprintf(q, q_len - 1, "%07d", frame_in); 414 break; 415 case '8': 416 snprintf(q, q_len - 1, "%08d", frame_in); 417 break; 418 case '9': 419 snprintf(q, q_len - 1, "%09d", frame_in); 420 break; 421 default: 422 die("Unrecognized pattern %%%c\n", p[1]); 423 } 424 425 pat_len = strlen(q); 426 if (pat_len >= q_len - 1) 427 die("Output filename too long.\n"); 428 q += pat_len; 429 p += 2; 430 q_len -= pat_len; 431 } else { 432 size_t copy_len; 433 434 /* copy the next segment */ 435 if (!next_pat) 436 copy_len = strlen(p); 437 else 438 copy_len = next_pat - p; 439 440 if (copy_len >= q_len - 1) 441 die("Output filename too long.\n"); 442 443 memcpy(q, p, copy_len); 444 q[copy_len] = '\0'; 445 q += copy_len; 446 p += copy_len; 447 q_len -= copy_len; 448 } 449 } while (*p); 450 } 451 452 static int is_single_file(const char *outfile_pattern) { 453 const char *p = outfile_pattern; 454 455 do { 456 p = strchr(p, '%'); 457 if (p && p[1] >= '1' && p[1] <= '9') 458 return 0; // pattern contains sequence number, so it's not unique 459 if (p) 460 p++; 461 } while (p); 462 463 return 1; 464 } 465 466 static void print_md5(unsigned char digest[16], const char *filename) { 467 int i; 468 469 for (i = 0; i < 16; ++i) 470 printf("%02x", digest[i]); 471 printf(" %s\n", filename); 472 } 473 474 static FILE *open_outfile(const char *name) { 475 if (strcmp("-", name) == 0) { 476 set_binary_mode(stdout); 477 return stdout; 478 } else { 479 FILE *file = fopen(name, "wb"); 480 if (!file) 481 fatal("Failed to output file %s", name); 482 return file; 483 } 484 } 485 486 int main_loop(int argc, const char **argv_) { 487 vpx_codec_ctx_t decoder; 488 char *fn = NULL; 489 int i; 490 uint8_t *buf = NULL; 491 size_t bytes_in_buffer = 0, buffer_size = 0; 492 FILE *infile; 493 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0; 494 int do_md5 = 0, progress = 0; 495 int stop_after = 0, postproc = 0, summary = 0, quiet = 1; 496 int arg_skip = 0; 497 int ec_enabled = 0; 498 const VpxInterface *interface = NULL; 499 const VpxInterface *fourcc_interface = NULL; 500 uint64_t dx_time = 0; 501 struct arg arg; 502 char **argv, **argi, **argj; 503 504 int single_file; 505 int use_y4m = 1; 506 vpx_codec_dec_cfg_t cfg = {0}; 507 #if CONFIG_VP8_DECODER 508 vp8_postproc_cfg_t vp8_pp_cfg = {0}; 509 int vp8_dbg_color_ref_frame = 0; 510 int vp8_dbg_color_mb_modes = 0; 511 int vp8_dbg_color_b_modes = 0; 512 int vp8_dbg_display_mv = 0; 513 #endif 514 int frames_corrupted = 0; 515 int dec_flags = 0; 516 int do_scale = 0; 517 vpx_image_t *scaled_img = NULL; 518 int frame_avail, got_data; 519 int num_external_frame_buffers = 0; 520 struct ExternalFrameBufferList ext_fb_list = {0}; 521 522 const char *outfile_pattern = NULL; 523 char outfile_name[PATH_MAX] = {0}; 524 FILE *outfile = NULL; 525 526 MD5Context md5_ctx; 527 unsigned char md5_digest[16]; 528 529 struct VpxDecInputContext input = {0}; 530 struct VpxInputContext vpx_input_ctx = {0}; 531 struct WebmInputContext webm_ctx = {0}; 532 input.vpx_input_ctx = &vpx_input_ctx; 533 input.webm_ctx = &webm_ctx; 534 535 /* Parse command line */ 536 exec_name = argv_[0]; 537 argv = argv_dup(argc - 1, argv_ + 1); 538 539 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) { 540 memset(&arg, 0, sizeof(arg)); 541 arg.argv_step = 1; 542 543 if (arg_match(&arg, &codecarg, argi)) { 544 interface = get_vpx_decoder_by_name(arg.val); 545 if (!interface) 546 die("Error: Unrecognized argument (%s) to --codec\n", arg.val); 547 } else if (arg_match(&arg, &looparg, argi)) { 548 // no-op 549 } else if (arg_match(&arg, &outputfile, argi)) 550 outfile_pattern = arg.val; 551 else if (arg_match(&arg, &use_yv12, argi)) { 552 use_y4m = 0; 553 flipuv = 1; 554 } else if (arg_match(&arg, &use_i420, argi)) { 555 use_y4m = 0; 556 flipuv = 0; 557 } else if (arg_match(&arg, &flipuvarg, argi)) 558 flipuv = 1; 559 else if (arg_match(&arg, &noblitarg, argi)) 560 noblit = 1; 561 else if (arg_match(&arg, &progressarg, argi)) 562 progress = 1; 563 else if (arg_match(&arg, &limitarg, argi)) 564 stop_after = arg_parse_uint(&arg); 565 else if (arg_match(&arg, &skiparg, argi)) 566 arg_skip = arg_parse_uint(&arg); 567 else if (arg_match(&arg, &postprocarg, argi)) 568 postproc = 1; 569 else if (arg_match(&arg, &md5arg, argi)) 570 do_md5 = 1; 571 else if (arg_match(&arg, &summaryarg, argi)) 572 summary = 1; 573 else if (arg_match(&arg, &threadsarg, argi)) 574 cfg.threads = arg_parse_uint(&arg); 575 else if (arg_match(&arg, &verbosearg, argi)) 576 quiet = 0; 577 else if (arg_match(&arg, &scalearg, argi)) 578 do_scale = 1; 579 else if (arg_match(&arg, &fb_arg, argi)) 580 num_external_frame_buffers = arg_parse_uint(&arg); 581 582 #if CONFIG_VP8_DECODER 583 else if (arg_match(&arg, &addnoise_level, argi)) { 584 postproc = 1; 585 vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE; 586 vp8_pp_cfg.noise_level = arg_parse_uint(&arg); 587 } else if (arg_match(&arg, &demacroblock_level, argi)) { 588 postproc = 1; 589 vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK; 590 vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg); 591 } else if (arg_match(&arg, &deblock, argi)) { 592 postproc = 1; 593 vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK; 594 } else if (arg_match(&arg, &mfqe, argi)) { 595 postproc = 1; 596 vp8_pp_cfg.post_proc_flag |= VP8_MFQE; 597 } else if (arg_match(&arg, &pp_debug_info, argi)) { 598 unsigned int level = arg_parse_uint(&arg); 599 600 postproc = 1; 601 vp8_pp_cfg.post_proc_flag &= ~0x7; 602 603 if (level) 604 vp8_pp_cfg.post_proc_flag |= level; 605 } else if (arg_match(&arg, &pp_disp_ref_frame, argi)) { 606 unsigned int flags = arg_parse_int(&arg); 607 if (flags) { 608 postproc = 1; 609 vp8_dbg_color_ref_frame = flags; 610 } 611 } else if (arg_match(&arg, &pp_disp_mb_modes, argi)) { 612 unsigned int flags = arg_parse_int(&arg); 613 if (flags) { 614 postproc = 1; 615 vp8_dbg_color_mb_modes = flags; 616 } 617 } else if (arg_match(&arg, &pp_disp_b_modes, argi)) { 618 unsigned int flags = arg_parse_int(&arg); 619 if (flags) { 620 postproc = 1; 621 vp8_dbg_color_b_modes = flags; 622 } 623 } else if (arg_match(&arg, &pp_disp_mvs, argi)) { 624 unsigned int flags = arg_parse_int(&arg); 625 if (flags) { 626 postproc = 1; 627 vp8_dbg_display_mv = flags; 628 } 629 } else if (arg_match(&arg, &error_concealment, argi)) { 630 ec_enabled = 1; 631 } 632 633 #endif 634 else 635 argj++; 636 } 637 638 /* Check for unrecognized options */ 639 for (argi = argv; *argi; argi++) 640 if (argi[0][0] == '-' && strlen(argi[0]) > 1) 641 die("Error: Unrecognized option %s\n", *argi); 642 643 /* Handle non-option arguments */ 644 fn = argv[0]; 645 646 if (!fn) 647 usage_exit(); 648 649 /* Open file */ 650 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin); 651 652 if (!infile) { 653 fprintf(stderr, "Failed to open file '%s'", strcmp(fn, "-") ? fn : "stdin"); 654 return EXIT_FAILURE; 655 } 656 #if CONFIG_OS_SUPPORT 657 /* Make sure we don't dump to the terminal, unless forced to with -o - */ 658 if (!outfile_pattern && isatty(fileno(stdout)) && !do_md5 && !noblit) { 659 fprintf(stderr, 660 "Not dumping raw video to your terminal. Use '-o -' to " 661 "override.\n"); 662 return EXIT_FAILURE; 663 } 664 #endif 665 input.vpx_input_ctx->file = infile; 666 if (file_is_ivf(input.vpx_input_ctx)) 667 input.vpx_input_ctx->file_type = FILE_TYPE_IVF; 668 #if CONFIG_WEBM_IO 669 else if (file_is_webm(input.webm_ctx, input.vpx_input_ctx)) 670 input.vpx_input_ctx->file_type = FILE_TYPE_WEBM; 671 #endif 672 else if (file_is_raw(input.vpx_input_ctx)) 673 input.vpx_input_ctx->file_type = FILE_TYPE_RAW; 674 else { 675 fprintf(stderr, "Unrecognized input file type.\n"); 676 #if !CONFIG_WEBM_IO 677 fprintf(stderr, "vpxdec was built without WebM container support.\n"); 678 #endif 679 return EXIT_FAILURE; 680 } 681 682 outfile_pattern = outfile_pattern ? outfile_pattern : "-"; 683 single_file = is_single_file(outfile_pattern); 684 685 if (!noblit && single_file) { 686 generate_filename(outfile_pattern, outfile_name, PATH_MAX, 687 vpx_input_ctx.width, vpx_input_ctx.height, 0); 688 if (do_md5) 689 MD5Init(&md5_ctx); 690 else 691 outfile = open_outfile(outfile_name); 692 } 693 694 if (use_y4m && !noblit) { 695 if (!single_file) { 696 fprintf(stderr, "YUV4MPEG2 not supported with output patterns," 697 " try --i420 or --yv12.\n"); 698 return EXIT_FAILURE; 699 } 700 701 #if CONFIG_WEBM_IO 702 if (vpx_input_ctx.file_type == FILE_TYPE_WEBM) { 703 if (webm_guess_framerate(input.webm_ctx, input.vpx_input_ctx)) { 704 fprintf(stderr, "Failed to guess framerate -- error parsing " 705 "webm file?\n"); 706 return EXIT_FAILURE; 707 } 708 } 709 #endif 710 } 711 712 fourcc_interface = get_vpx_decoder_by_fourcc(vpx_input_ctx.fourcc); 713 if (interface && fourcc_interface && interface != fourcc_interface) 714 warn("Header indicates codec: %s\n", fourcc_interface->name); 715 else 716 interface = fourcc_interface; 717 718 if (!interface) 719 interface = get_vpx_decoder_by_index(0); 720 721 dec_flags = (postproc ? VPX_CODEC_USE_POSTPROC : 0) | 722 (ec_enabled ? VPX_CODEC_USE_ERROR_CONCEALMENT : 0); 723 if (vpx_codec_dec_init(&decoder, interface->interface(), &cfg, dec_flags)) { 724 fprintf(stderr, "Failed to initialize decoder: %s\n", 725 vpx_codec_error(&decoder)); 726 return EXIT_FAILURE; 727 } 728 729 if (!quiet) 730 fprintf(stderr, "%s\n", decoder.name); 731 732 #if CONFIG_VP8_DECODER 733 734 if (vp8_pp_cfg.post_proc_flag 735 && vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg)) { 736 fprintf(stderr, "Failed to configure postproc: %s\n", vpx_codec_error(&decoder)); 737 return EXIT_FAILURE; 738 } 739 740 if (vp8_dbg_color_ref_frame 741 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_REF_FRAME, vp8_dbg_color_ref_frame)) { 742 fprintf(stderr, "Failed to configure reference block visualizer: %s\n", vpx_codec_error(&decoder)); 743 return EXIT_FAILURE; 744 } 745 746 if (vp8_dbg_color_mb_modes 747 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_MB_MODES, vp8_dbg_color_mb_modes)) { 748 fprintf(stderr, "Failed to configure macro block visualizer: %s\n", vpx_codec_error(&decoder)); 749 return EXIT_FAILURE; 750 } 751 752 if (vp8_dbg_color_b_modes 753 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_B_MODES, vp8_dbg_color_b_modes)) { 754 fprintf(stderr, "Failed to configure block visualizer: %s\n", vpx_codec_error(&decoder)); 755 return EXIT_FAILURE; 756 } 757 758 if (vp8_dbg_display_mv 759 && vpx_codec_control(&decoder, VP8_SET_DBG_DISPLAY_MV, vp8_dbg_display_mv)) { 760 fprintf(stderr, "Failed to configure motion vector visualizer: %s\n", vpx_codec_error(&decoder)); 761 return EXIT_FAILURE; 762 } 763 #endif 764 765 766 if (arg_skip) 767 fprintf(stderr, "Skipping first %d frames.\n", arg_skip); 768 while (arg_skip) { 769 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) 770 break; 771 arg_skip--; 772 } 773 774 if (num_external_frame_buffers > 0) { 775 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers; 776 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc( 777 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb)); 778 if (vpx_codec_set_frame_buffer_functions( 779 &decoder, get_vp9_frame_buffer, release_vp9_frame_buffer, 780 &ext_fb_list)) { 781 fprintf(stderr, "Failed to configure external frame buffers: %s\n", 782 vpx_codec_error(&decoder)); 783 return EXIT_FAILURE; 784 } 785 } 786 787 frame_avail = 1; 788 got_data = 0; 789 790 /* Decode file */ 791 while (frame_avail || got_data) { 792 vpx_codec_iter_t iter = NULL; 793 vpx_image_t *img; 794 struct vpx_usec_timer timer; 795 int corrupted; 796 797 frame_avail = 0; 798 if (!stop_after || frame_in < stop_after) { 799 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) { 800 frame_avail = 1; 801 frame_in++; 802 803 vpx_usec_timer_start(&timer); 804 805 if (vpx_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer, 806 NULL, 0)) { 807 const char *detail = vpx_codec_error_detail(&decoder); 808 warn("Failed to decode frame %d: %s", 809 frame_in, vpx_codec_error(&decoder)); 810 811 if (detail) 812 warn("Additional information: %s", detail); 813 goto fail; 814 } 815 816 vpx_usec_timer_mark(&timer); 817 dx_time += vpx_usec_timer_elapsed(&timer); 818 } 819 } 820 821 vpx_usec_timer_start(&timer); 822 823 got_data = 0; 824 if ((img = vpx_codec_get_frame(&decoder, &iter))) { 825 ++frame_out; 826 got_data = 1; 827 } 828 829 vpx_usec_timer_mark(&timer); 830 dx_time += (unsigned int)vpx_usec_timer_elapsed(&timer); 831 832 if (vpx_codec_control(&decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted)) { 833 warn("Failed VP8_GET_FRAME_CORRUPTED: %s", vpx_codec_error(&decoder)); 834 goto fail; 835 } 836 frames_corrupted += corrupted; 837 838 if (progress) 839 show_progress(frame_in, frame_out, dx_time); 840 841 if (!noblit && img) { 842 const int PLANES_YUV[] = {VPX_PLANE_Y, VPX_PLANE_U, VPX_PLANE_V}; 843 const int PLANES_YVU[] = {VPX_PLANE_Y, VPX_PLANE_V, VPX_PLANE_U}; 844 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV; 845 846 if (do_scale) { 847 if (frame_out == 1) { 848 // If the output frames are to be scaled to a fixed display size then 849 // use the width and height specified in the container. If either of 850 // these is set to 0, use the display size set in the first frame 851 // header. If that is unavailable, use the raw decoded size of the 852 // first decoded frame. 853 int display_width = vpx_input_ctx.width; 854 int display_height = vpx_input_ctx.height; 855 if (!display_width || !display_height) { 856 int display_size[2]; 857 if (vpx_codec_control(&decoder, VP9D_GET_DISPLAY_SIZE, 858 display_size)) { 859 // As last resort use size of first frame as display size. 860 display_width = img->d_w; 861 display_height = img->d_h; 862 } else { 863 display_width = display_size[0]; 864 display_height = display_size[1]; 865 } 866 } 867 scaled_img = vpx_img_alloc(NULL, VPX_IMG_FMT_I420, display_width, 868 display_height, 16); 869 } 870 871 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) { 872 vpx_image_scale(img, scaled_img, kFilterBox); 873 img = scaled_img; 874 } 875 } 876 877 if (single_file) { 878 if (use_y4m) { 879 char buf[Y4M_BUFFER_SIZE] = {0}; 880 size_t len = 0; 881 if (frame_out == 1) { 882 // Y4M file header 883 len = y4m_write_file_header(buf, sizeof(buf), 884 vpx_input_ctx.width, 885 vpx_input_ctx.height, 886 &vpx_input_ctx.framerate, img->fmt); 887 if (do_md5) { 888 MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len); 889 } else { 890 fputs(buf, outfile); 891 } 892 } 893 894 // Y4M frame header 895 len = y4m_write_frame_header(buf, sizeof(buf)); 896 if (do_md5) { 897 MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len); 898 } else { 899 fputs(buf, outfile); 900 } 901 } 902 903 if (do_md5) { 904 update_image_md5(img, planes, &md5_ctx); 905 } else { 906 write_image_file(img, planes, outfile); 907 } 908 } else { 909 generate_filename(outfile_pattern, outfile_name, PATH_MAX, 910 img->d_w, img->d_h, frame_in); 911 if (do_md5) { 912 MD5Init(&md5_ctx); 913 update_image_md5(img, planes, &md5_ctx); 914 MD5Final(md5_digest, &md5_ctx); 915 print_md5(md5_digest, outfile_name); 916 } else { 917 outfile = open_outfile(outfile_name); 918 write_image_file(img, planes, outfile); 919 fclose(outfile); 920 } 921 } 922 } 923 924 if (stop_after && frame_in >= stop_after) 925 break; 926 } 927 928 if (summary || progress) { 929 show_progress(frame_in, frame_out, dx_time); 930 fprintf(stderr, "\n"); 931 } 932 933 if (frames_corrupted) 934 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted); 935 936 fail: 937 938 if (vpx_codec_destroy(&decoder)) { 939 fprintf(stderr, "Failed to destroy decoder: %s\n", 940 vpx_codec_error(&decoder)); 941 return EXIT_FAILURE; 942 } 943 944 if (!noblit && single_file) { 945 if (do_md5) { 946 MD5Final(md5_digest, &md5_ctx); 947 print_md5(md5_digest, outfile_name); 948 } else { 949 fclose(outfile); 950 } 951 } 952 953 #if CONFIG_WEBM_IO 954 if (input.vpx_input_ctx->file_type == FILE_TYPE_WEBM) 955 webm_free(input.webm_ctx); 956 #endif 957 958 if (input.vpx_input_ctx->file_type != FILE_TYPE_WEBM) 959 free(buf); 960 961 if (scaled_img) vpx_img_free(scaled_img); 962 963 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) { 964 free(ext_fb_list.ext_fb[i].data); 965 } 966 free(ext_fb_list.ext_fb); 967 968 fclose(infile); 969 free(argv); 970 971 return frames_corrupted ? EXIT_FAILURE : EXIT_SUCCESS; 972 } 973 974 int main(int argc, const char **argv_) { 975 unsigned int loops = 1, i; 976 char **argv, **argi, **argj; 977 struct arg arg; 978 int error = 0; 979 980 argv = argv_dup(argc - 1, argv_ + 1); 981 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) { 982 memset(&arg, 0, sizeof(arg)); 983 arg.argv_step = 1; 984 985 if (arg_match(&arg, &looparg, argi)) { 986 loops = arg_parse_uint(&arg); 987 break; 988 } 989 } 990 free(argv); 991 for (i = 0; !error && i < loops; i++) 992 error = main_loop(argc, argv_); 993 return error; 994 } 995