vpxdec

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 #include <assert.h>
00012 #include <limits.h>
00013 #include <stdarg.h>
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 
00018 #include "./vpx_config.h"
00019 
00020 #if CONFIG_LIBYUV
00021 #include "third_party/libyuv/include/libyuv/scale.h"
00022 #endif
00023 
00024 #include "./args.h"
00025 #include "./ivfdec.h"
00026 
00027 #include "vpx/vpx_decoder.h"
00028 #include "vpx_ports/mem_ops.h"
00029 #include "vpx_ports/vpx_timer.h"
00030 
00031 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
00032 #include "vpx/vp8dx.h"
00033 #endif
00034 
00035 #include "./md5_utils.h"
00036 
00037 #include "./tools_common.h"
00038 #if CONFIG_WEBM_IO
00039 #include "./webmdec.h"
00040 #endif
00041 #include "./y4menc.h"
00042 
00043 static const char *exec_name;
00044 
00045 struct VpxDecInputContext {
00046   struct VpxInputContext *vpx_input_ctx;
00047   struct WebmInputContext *webm_ctx;
00048 };
00049 
00050 static const arg_def_t looparg =
00051     ARG_DEF(NULL, "loops", 1, "Number of times to decode the file");
00052 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
00053 static const arg_def_t use_yv12 =
00054     ARG_DEF(NULL, "yv12", 0, "Output raw YV12 frames");
00055 static const arg_def_t use_i420 =
00056     ARG_DEF(NULL, "i420", 0, "Output raw I420 frames");
00057 static const arg_def_t flipuvarg =
00058     ARG_DEF(NULL, "flipuv", 0, "Flip the chroma planes in the output");
00059 static const arg_def_t rawvideo =
00060     ARG_DEF(NULL, "rawvideo", 0, "Output raw YUV frames");
00061 static const arg_def_t noblitarg =
00062     ARG_DEF(NULL, "noblit", 0, "Don't process the decoded frames");
00063 static const arg_def_t progressarg =
00064     ARG_DEF(NULL, "progress", 0, "Show progress after each frame decodes");
00065 static const arg_def_t limitarg =
00066     ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
00067 static const arg_def_t skiparg =
00068     ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
00069 static const arg_def_t postprocarg =
00070     ARG_DEF(NULL, "postproc", 0, "Postprocess decoded frames");
00071 static const arg_def_t summaryarg =
00072     ARG_DEF(NULL, "summary", 0, "Show timing summary");
00073 static const arg_def_t outputfile =
00074     ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
00075 static const arg_def_t threadsarg =
00076     ARG_DEF("t", "threads", 1, "Max threads to use");
00077 static const arg_def_t frameparallelarg =
00078     ARG_DEF(NULL, "frame-parallel", 0, "Frame parallel decode");
00079 static const arg_def_t verbosearg =
00080     ARG_DEF("v", "verbose", 0, "Show version string");
00081 static const arg_def_t error_concealment =
00082     ARG_DEF(NULL, "error-concealment", 0, "Enable decoder error-concealment");
00083 static const arg_def_t scalearg =
00084     ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
00085 static const arg_def_t continuearg =
00086     ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
00087 static const arg_def_t fb_arg =
00088     ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
00089 static const arg_def_t md5arg =
00090     ARG_DEF(NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
00091 #if CONFIG_VP9_HIGHBITDEPTH
00092 static const arg_def_t outbitdeptharg =
00093     ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
00094 #endif
00095 static const arg_def_t svcdecodingarg = ARG_DEF(
00096     NULL, "svc-decode-layer", 1, "Decode SVC stream up to given spatial layer");
00097 
00098 static const arg_def_t *all_args[] = {
00099   &codecarg,       &use_yv12,    &use_i420,   &flipuvarg,         &rawvideo,
00100   &noblitarg,      &progressarg, &limitarg,   &skiparg,           &postprocarg,
00101   &summaryarg,     &outputfile,  &threadsarg, &frameparallelarg,  &verbosearg,
00102   &scalearg,       &fb_arg,      &md5arg,     &error_concealment, &continuearg,
00103 #if CONFIG_VP9_HIGHBITDEPTH
00104   &outbitdeptharg,
00105 #endif
00106   &svcdecodingarg, NULL
00107 };
00108 
00109 #if CONFIG_VP8_DECODER
00110 static const arg_def_t addnoise_level =
00111     ARG_DEF(NULL, "noise-level", 1, "Enable VP8 postproc add noise");
00112 static const arg_def_t deblock =
00113     ARG_DEF(NULL, "deblock", 0, "Enable VP8 deblocking");
00114 static const arg_def_t demacroblock_level = ARG_DEF(
00115     NULL, "demacroblock-level", 1, "Enable VP8 demacroblocking, w/ level");
00116 static const arg_def_t mfqe =
00117     ARG_DEF(NULL, "mfqe", 0, "Enable multiframe quality enhancement");
00118 
00119 static const arg_def_t *vp8_pp_args[] = { &addnoise_level, &deblock,
00120                                           &demacroblock_level, &mfqe, NULL };
00121 #endif
00122 
00123 #if CONFIG_LIBYUV
00124 static INLINE int libyuv_scale(vpx_image_t *src, vpx_image_t *dst,
00125                                FilterModeEnum mode) {
00126 #if CONFIG_VP9_HIGHBITDEPTH
00127   if (src->fmt == VPX_IMG_FMT_I42016) {
00128     assert(dst->fmt == VPX_IMG_FMT_I42016);
00129     return I420Scale_16(
00130         (uint16_t *)src->planes[VPX_PLANE_Y], src->stride[VPX_PLANE_Y] / 2,
00131         (uint16_t *)src->planes[VPX_PLANE_U], src->stride[VPX_PLANE_U] / 2,
00132         (uint16_t *)src->planes[VPX_PLANE_V], src->stride[VPX_PLANE_V] / 2,
00133         src->d_w, src->d_h, (uint16_t *)dst->planes[VPX_PLANE_Y],
00134         dst->stride[VPX_PLANE_Y] / 2, (uint16_t *)dst->planes[VPX_PLANE_U],
00135         dst->stride[VPX_PLANE_U] / 2, (uint16_t *)dst->planes[VPX_PLANE_V],
00136         dst->stride[VPX_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
00137   }
00138 #endif
00139   assert(src->fmt == VPX_IMG_FMT_I420);
00140   assert(dst->fmt == VPX_IMG_FMT_I420);
00141   return I420Scale(src->planes[VPX_PLANE_Y], src->stride[VPX_PLANE_Y],
00142                    src->planes[VPX_PLANE_U], src->stride[VPX_PLANE_U],
00143                    src->planes[VPX_PLANE_V], src->stride[VPX_PLANE_V], src->d_w,
00144                    src->d_h, dst->planes[VPX_PLANE_Y], dst->stride[VPX_PLANE_Y],
00145                    dst->planes[VPX_PLANE_U], dst->stride[VPX_PLANE_U],
00146                    dst->planes[VPX_PLANE_V], dst->stride[VPX_PLANE_V], dst->d_w,
00147                    dst->d_h, mode);
00148 }
00149 #endif
00150 
00151 void usage_exit(void) {
00152   int i;
00153 
00154   fprintf(stderr,
00155           "Usage: %s <options> filename\n\n"
00156           "Options:\n",
00157           exec_name);
00158   arg_show_usage(stderr, all_args);
00159 #if CONFIG_VP8_DECODER
00160   fprintf(stderr, "\nVP8 Postprocessing Options:\n");
00161   arg_show_usage(stderr, vp8_pp_args);
00162 #endif
00163   fprintf(stderr,
00164           "\nOutput File Patterns:\n\n"
00165           "  The -o argument specifies the name of the file(s) to "
00166           "write to. If the\n  argument does not include any escape "
00167           "characters, the output will be\n  written to a single file. "
00168           "Otherwise, the filename will be calculated by\n  expanding "
00169           "the following escape characters:\n");
00170   fprintf(stderr,
00171           "\n\t%%w   - Frame width"
00172           "\n\t%%h   - Frame height"
00173           "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
00174           "\n\n  Pattern arguments are only supported in conjunction "
00175           "with the --yv12 and\n  --i420 options. If the -o option is "
00176           "not specified, the output will be\n  directed to stdout.\n");
00177   fprintf(stderr, "\nIncluded decoders:\n\n");
00178 
00179   for (i = 0; i < get_vpx_decoder_count(); ++i) {
00180     const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
00181     fprintf(stderr, "    %-6s - %s\n", decoder->name,
00182             vpx_codec_iface_name(decoder->codec_interface()));
00183   }
00184 
00185   exit(EXIT_FAILURE);
00186 }
00187 
00188 static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
00189                           size_t *buffer_size) {
00190   char raw_hdr[RAW_FRAME_HDR_SZ];
00191   size_t frame_size = 0;
00192 
00193   if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
00194     if (!feof(infile)) warn("Failed to read RAW frame size\n");
00195   } else {
00196     const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
00197     const size_t kFrameTooSmallThreshold = 256 * 1024;
00198     frame_size = mem_get_le32(raw_hdr);
00199 
00200     if (frame_size > kCorruptFrameThreshold) {
00201       warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
00202       frame_size = 0;
00203     }
00204 
00205     if (frame_size < kFrameTooSmallThreshold) {
00206       warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
00207            (unsigned int)frame_size);
00208     }
00209 
00210     if (frame_size > *buffer_size) {
00211       uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
00212       if (new_buf) {
00213         *buffer = new_buf;
00214         *buffer_size = 2 * frame_size;
00215       } else {
00216         warn("Failed to allocate compressed data buffer\n");
00217         frame_size = 0;
00218       }
00219     }
00220   }
00221 
00222   if (!feof(infile)) {
00223     if (fread(*buffer, 1, frame_size, infile) != frame_size) {
00224       warn("Failed to read full frame\n");
00225       return 1;
00226     }
00227     *bytes_read = frame_size;
00228   }
00229 
00230   return 0;
00231 }
00232 
00233 static int read_frame(struct VpxDecInputContext *input, uint8_t **buf,
00234                       size_t *bytes_in_buffer, size_t *buffer_size) {
00235   switch (input->vpx_input_ctx->file_type) {
00236 #if CONFIG_WEBM_IO
00237     case FILE_TYPE_WEBM:
00238       return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer);
00239 #endif
00240     case FILE_TYPE_RAW:
00241       return raw_read_frame(input->vpx_input_ctx->file, buf, bytes_in_buffer,
00242                             buffer_size);
00243     case FILE_TYPE_IVF:
00244       return ivf_read_frame(input->vpx_input_ctx->file, buf, bytes_in_buffer,
00245                             buffer_size);
00246     default: return 1;
00247   }
00248 }
00249 
00250 static void update_image_md5(const vpx_image_t *img, const int planes[3],
00251                              MD5Context *md5) {
00252   int i, y;
00253 
00254   for (i = 0; i < 3; ++i) {
00255     const int plane = planes[i];
00256     const unsigned char *buf = img->planes[plane];
00257     const int stride = img->stride[plane];
00258     const int w = vpx_img_plane_width(img, plane) *
00259                   ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
00260     const int h = vpx_img_plane_height(img, plane);
00261 
00262     for (y = 0; y < h; ++y) {
00263       MD5Update(md5, buf, w);
00264       buf += stride;
00265     }
00266   }
00267 }
00268 
00269 static void write_image_file(const vpx_image_t *img, const int planes[3],
00270                              FILE *file) {
00271   int i, y;
00272 #if CONFIG_VP9_HIGHBITDEPTH
00273   const int bytes_per_sample = ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
00274 #else
00275   const int bytes_per_sample = 1;
00276 #endif
00277 
00278   for (i = 0; i < 3; ++i) {
00279     const int plane = planes[i];
00280     const unsigned char *buf = img->planes[plane];
00281     const int stride = img->stride[plane];
00282     const int w = vpx_img_plane_width(img, plane);
00283     const int h = vpx_img_plane_height(img, plane);
00284 
00285     for (y = 0; y < h; ++y) {
00286       fwrite(buf, bytes_per_sample, w, file);
00287       buf += stride;
00288     }
00289   }
00290 }
00291 
00292 static int file_is_raw(struct VpxInputContext *input) {
00293   uint8_t buf[32];
00294   int is_raw = 0;
00295   vpx_codec_stream_info_t si;
00296 
00297   si.sz = sizeof(si);
00298 
00299   if (fread(buf, 1, 32, input->file) == 32) {
00300     int i;
00301 
00302     if (mem_get_le32(buf) < 256 * 1024 * 1024) {
00303       for (i = 0; i < get_vpx_decoder_count(); ++i) {
00304         const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
00305         if (!vpx_codec_peek_stream_info(decoder->codec_interface(), buf + 4,
00306                                         32 - 4, &si)) {
00307           is_raw = 1;
00308           input->fourcc = decoder->fourcc;
00309           input->width = si.w;
00310           input->height = si.h;
00311           input->framerate.numerator = 30;
00312           input->framerate.denominator = 1;
00313           break;
00314         }
00315       }
00316     }
00317   }
00318 
00319   rewind(input->file);
00320   return is_raw;
00321 }
00322 
00323 static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
00324   fprintf(stderr,
00325           "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
00326           frame_in, frame_out, dx_time,
00327           (double)frame_out * 1000000.0 / (double)dx_time);
00328 }
00329 
00330 struct ExternalFrameBuffer {
00331   uint8_t *data;
00332   size_t size;
00333   int in_use;
00334 };
00335 
00336 struct ExternalFrameBufferList {
00337   int num_external_frame_buffers;
00338   struct ExternalFrameBuffer *ext_fb;
00339 };
00340 
00341 // Callback used by libvpx to request an external frame buffer. |cb_priv|
00342 // Application private data passed into the set function. |min_size| is the
00343 // minimum size in bytes needed to decode the next frame. |fb| pointer to the
00344 // frame buffer.
00345 static int get_vp9_frame_buffer(void *cb_priv, size_t min_size,
00346                                 vpx_codec_frame_buffer_t *fb) {
00347   int i;
00348   struct ExternalFrameBufferList *const ext_fb_list =
00349       (struct ExternalFrameBufferList *)cb_priv;
00350   if (ext_fb_list == NULL) return -1;
00351 
00352   // Find a free frame buffer.
00353   for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
00354     if (!ext_fb_list->ext_fb[i].in_use) break;
00355   }
00356 
00357   if (i == ext_fb_list->num_external_frame_buffers) return -1;
00358 
00359   if (ext_fb_list->ext_fb[i].size < min_size) {
00360     free(ext_fb_list->ext_fb[i].data);
00361     ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
00362     if (!ext_fb_list->ext_fb[i].data) return -1;
00363 
00364     ext_fb_list->ext_fb[i].size = min_size;
00365   }
00366 
00367   fb->data = ext_fb_list->ext_fb[i].data;
00368   fb->size = ext_fb_list->ext_fb[i].size;
00369   ext_fb_list->ext_fb[i].in_use = 1;
00370 
00371   // Set the frame buffer's private data to point at the external frame buffer.
00372   fb->priv = &ext_fb_list->ext_fb[i];
00373   return 0;
00374 }
00375 
00376 // Callback used by libvpx when there are no references to the frame buffer.
00377 // |cb_priv| user private data passed into the set function. |fb| pointer
00378 // to the frame buffer.
00379 static int release_vp9_frame_buffer(void *cb_priv,
00380                                     vpx_codec_frame_buffer_t *fb) {
00381   struct ExternalFrameBuffer *const ext_fb =
00382       (struct ExternalFrameBuffer *)fb->priv;
00383   (void)cb_priv;
00384   ext_fb->in_use = 0;
00385   return 0;
00386 }
00387 
00388 static void generate_filename(const char *pattern, char *out, size_t q_len,
00389                               unsigned int d_w, unsigned int d_h,
00390                               unsigned int frame_in) {
00391   const char *p = pattern;
00392   char *q = out;
00393 
00394   do {
00395     char *next_pat = strchr(p, '%');
00396 
00397     if (p == next_pat) {
00398       size_t pat_len;
00399 
00400       /* parse the pattern */
00401       q[q_len - 1] = '\0';
00402       switch (p[1]) {
00403         case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
00404         case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
00405         case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
00406         case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
00407         case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
00408         case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
00409         case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
00410         case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
00411         case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
00412         case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
00413         case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
00414         default: die("Unrecognized pattern %%%c\n", p[1]); break;
00415       }
00416 
00417       pat_len = strlen(q);
00418       if (pat_len >= q_len - 1) die("Output filename too long.\n");
00419       q += pat_len;
00420       p += 2;
00421       q_len -= pat_len;
00422     } else {
00423       size_t copy_len;
00424 
00425       /* copy the next segment */
00426       if (!next_pat)
00427         copy_len = strlen(p);
00428       else
00429         copy_len = next_pat - p;
00430 
00431       if (copy_len >= q_len - 1) die("Output filename too long.\n");
00432 
00433       memcpy(q, p, copy_len);
00434       q[copy_len] = '\0';
00435       q += copy_len;
00436       p += copy_len;
00437       q_len -= copy_len;
00438     }
00439   } while (*p);
00440 }
00441 
00442 static int is_single_file(const char *outfile_pattern) {
00443   const char *p = outfile_pattern;
00444 
00445   do {
00446     p = strchr(p, '%');
00447     if (p && p[1] >= '1' && p[1] <= '9')
00448       return 0;  // pattern contains sequence number, so it's not unique
00449     if (p) p++;
00450   } while (p);
00451 
00452   return 1;
00453 }
00454 
00455 static void print_md5(unsigned char digest[16], const char *filename) {
00456   int i;
00457 
00458   for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
00459   printf("  %s\n", filename);
00460 }
00461 
00462 static FILE *open_outfile(const char *name) {
00463   if (strcmp("-", name) == 0) {
00464     set_binary_mode(stdout);
00465     return stdout;
00466   } else {
00467     FILE *file = fopen(name, "wb");
00468     if (!file) fatal("Failed to open output file '%s'", name);
00469     return file;
00470   }
00471 }
00472 
00473 #if CONFIG_VP9_HIGHBITDEPTH
00474 static int img_shifted_realloc_required(const vpx_image_t *img,
00475                                         const vpx_image_t *shifted,
00476                                         vpx_img_fmt_t required_fmt) {
00477   return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
00478          required_fmt != shifted->fmt;
00479 }
00480 #endif
00481 
00482 static int main_loop(int argc, const char **argv_) {
00483   vpx_codec_ctx_t decoder;
00484   char *fn = NULL;
00485   int i;
00486   int ret = EXIT_FAILURE;
00487   uint8_t *buf = NULL;
00488   size_t bytes_in_buffer = 0, buffer_size = 0;
00489   FILE *infile;
00490   int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
00491   int do_md5 = 0, progress = 0, frame_parallel = 0;
00492   int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
00493   int arg_skip = 0;
00494   int ec_enabled = 0;
00495   int keep_going = 0;
00496   const VpxInterface *interface = NULL;
00497   const VpxInterface *fourcc_interface = NULL;
00498   uint64_t dx_time = 0;
00499   struct arg arg;
00500   char **argv, **argi, **argj;
00501 
00502   int single_file;
00503   int use_y4m = 1;
00504   int opt_yv12 = 0;
00505   int opt_i420 = 0;
00506   vpx_codec_dec_cfg_t cfg = { 0, 0, 0 };
00507 #if CONFIG_VP9_HIGHBITDEPTH
00508   unsigned int output_bit_depth = 0;
00509 #endif
00510   int svc_decoding = 0;
00511   int svc_spatial_layer = 0;
00512 #if CONFIG_VP8_DECODER
00513   vp8_postproc_cfg_t vp8_pp_cfg = { 0, 0, 0 };
00514 #endif
00515   int frames_corrupted = 0;
00516   int dec_flags = 0;
00517   int do_scale = 0;
00518   vpx_image_t *scaled_img = NULL;
00519 #if CONFIG_VP9_HIGHBITDEPTH
00520   vpx_image_t *img_shifted = NULL;
00521 #endif
00522   int frame_avail, got_data, flush_decoder = 0;
00523   int num_external_frame_buffers = 0;
00524   struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
00525 
00526   const char *outfile_pattern = NULL;
00527   char outfile_name[PATH_MAX] = { 0 };
00528   FILE *outfile = NULL;
00529 
00530   MD5Context md5_ctx;
00531   unsigned char md5_digest[16];
00532 
00533   struct VpxDecInputContext input = { NULL, NULL };
00534   struct VpxInputContext vpx_input_ctx;
00535 #if CONFIG_WEBM_IO
00536   struct WebmInputContext webm_ctx;
00537   memset(&(webm_ctx), 0, sizeof(webm_ctx));
00538   input.webm_ctx = &webm_ctx;
00539 #endif
00540   input.vpx_input_ctx = &vpx_input_ctx;
00541 
00542   /* Parse command line */
00543   exec_name = argv_[0];
00544   argv = argv_dup(argc - 1, argv_ + 1);
00545 
00546   for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
00547     memset(&arg, 0, sizeof(arg));
00548     arg.argv_step = 1;
00549 
00550     if (arg_match(&arg, &codecarg, argi)) {
00551       interface = get_vpx_decoder_by_name(arg.val);
00552       if (!interface)
00553         die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
00554     } else if (arg_match(&arg, &looparg, argi)) {
00555       // no-op
00556     } else if (arg_match(&arg, &outputfile, argi))
00557       outfile_pattern = arg.val;
00558     else if (arg_match(&arg, &use_yv12, argi)) {
00559       use_y4m = 0;
00560       flipuv = 1;
00561       opt_yv12 = 1;
00562     } else if (arg_match(&arg, &use_i420, argi)) {
00563       use_y4m = 0;
00564       flipuv = 0;
00565       opt_i420 = 1;
00566     } else if (arg_match(&arg, &rawvideo, argi)) {
00567       use_y4m = 0;
00568     } else if (arg_match(&arg, &flipuvarg, argi))
00569       flipuv = 1;
00570     else if (arg_match(&arg, &noblitarg, argi))
00571       noblit = 1;
00572     else if (arg_match(&arg, &progressarg, argi))
00573       progress = 1;
00574     else if (arg_match(&arg, &limitarg, argi))
00575       stop_after = arg_parse_uint(&arg);
00576     else if (arg_match(&arg, &skiparg, argi))
00577       arg_skip = arg_parse_uint(&arg);
00578     else if (arg_match(&arg, &postprocarg, argi))
00579       postproc = 1;
00580     else if (arg_match(&arg, &md5arg, argi))
00581       do_md5 = 1;
00582     else if (arg_match(&arg, &summaryarg, argi))
00583       summary = 1;
00584     else if (arg_match(&arg, &threadsarg, argi))
00585       cfg.threads = arg_parse_uint(&arg);
00586 #if CONFIG_VP9_DECODER
00587     else if (arg_match(&arg, &frameparallelarg, argi))
00588       frame_parallel = 1;
00589 #endif
00590     else if (arg_match(&arg, &verbosearg, argi))
00591       quiet = 0;
00592     else if (arg_match(&arg, &scalearg, argi))
00593       do_scale = 1;
00594     else if (arg_match(&arg, &fb_arg, argi))
00595       num_external_frame_buffers = arg_parse_uint(&arg);
00596     else if (arg_match(&arg, &continuearg, argi))
00597       keep_going = 1;
00598 #if CONFIG_VP9_HIGHBITDEPTH
00599     else if (arg_match(&arg, &outbitdeptharg, argi)) {
00600       output_bit_depth = arg_parse_uint(&arg);
00601     }
00602 #endif
00603     else if (arg_match(&arg, &svcdecodingarg, argi)) {
00604       svc_decoding = 1;
00605       svc_spatial_layer = arg_parse_uint(&arg);
00606     }
00607 #if CONFIG_VP8_DECODER
00608     else if (arg_match(&arg, &addnoise_level, argi)) {
00609       postproc = 1;
00610       vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE;
00611       vp8_pp_cfg.noise_level = arg_parse_uint(&arg);
00612     } else if (arg_match(&arg, &demacroblock_level, argi)) {
00613       postproc = 1;
00614       vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK;
00615       vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg);
00616     } else if (arg_match(&arg, &deblock, argi)) {
00617       postproc = 1;
00618       vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK;
00619     } else if (arg_match(&arg, &mfqe, argi)) {
00620       postproc = 1;
00621       vp8_pp_cfg.post_proc_flag |= VP8_MFQE;
00622     } else if (arg_match(&arg, &error_concealment, argi)) {
00623       ec_enabled = 1;
00624     }
00625 #endif  // CONFIG_VP8_DECODER
00626     else
00627       argj++;
00628   }
00629 
00630   /* Check for unrecognized options */
00631   for (argi = argv; *argi; argi++)
00632     if (argi[0][0] == '-' && strlen(argi[0]) > 1)
00633       die("Error: Unrecognized option %s\n", *argi);
00634 
00635   /* Handle non-option arguments */
00636   fn = argv[0];
00637 
00638   if (!fn) {
00639     free(argv);
00640     usage_exit();
00641   }
00642   /* Open file */
00643   infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
00644 
00645   if (!infile) {
00646     fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
00647   }
00648 #if CONFIG_OS_SUPPORT
00649   /* Make sure we don't dump to the terminal, unless forced to with -o - */
00650   if (!outfile_pattern && isatty(fileno(stdout)) && !do_md5 && !noblit) {
00651     fprintf(stderr,
00652             "Not dumping raw video to your terminal. Use '-o -' to "
00653             "override.\n");
00654     return EXIT_FAILURE;
00655   }
00656 #endif
00657   input.vpx_input_ctx->file = infile;
00658   if (file_is_ivf(input.vpx_input_ctx))
00659     input.vpx_input_ctx->file_type = FILE_TYPE_IVF;
00660 #if CONFIG_WEBM_IO
00661   else if (file_is_webm(input.webm_ctx, input.vpx_input_ctx))
00662     input.vpx_input_ctx->file_type = FILE_TYPE_WEBM;
00663 #endif
00664   else if (file_is_raw(input.vpx_input_ctx))
00665     input.vpx_input_ctx->file_type = FILE_TYPE_RAW;
00666   else {
00667     fprintf(stderr, "Unrecognized input file type.\n");
00668 #if !CONFIG_WEBM_IO
00669     fprintf(stderr, "vpxdec was built without WebM container support.\n");
00670 #endif
00671     return EXIT_FAILURE;
00672   }
00673 
00674   outfile_pattern = outfile_pattern ? outfile_pattern : "-";
00675   single_file = is_single_file(outfile_pattern);
00676 
00677   if (!noblit && single_file) {
00678     generate_filename(outfile_pattern, outfile_name, PATH_MAX,
00679                       vpx_input_ctx.width, vpx_input_ctx.height, 0);
00680     if (do_md5)
00681       MD5Init(&md5_ctx);
00682     else
00683       outfile = open_outfile(outfile_name);
00684   }
00685 
00686   if (use_y4m && !noblit) {
00687     if (!single_file) {
00688       fprintf(stderr,
00689               "YUV4MPEG2 not supported with output patterns,"
00690               " try --i420 or --yv12 or --rawvideo.\n");
00691       return EXIT_FAILURE;
00692     }
00693 
00694 #if CONFIG_WEBM_IO
00695     if (vpx_input_ctx.file_type == FILE_TYPE_WEBM) {
00696       if (webm_guess_framerate(input.webm_ctx, input.vpx_input_ctx)) {
00697         fprintf(stderr,
00698                 "Failed to guess framerate -- error parsing "
00699                 "webm file?\n");
00700         return EXIT_FAILURE;
00701       }
00702     }
00703 #endif
00704   }
00705 
00706   fourcc_interface = get_vpx_decoder_by_fourcc(vpx_input_ctx.fourcc);
00707   if (interface && fourcc_interface && interface != fourcc_interface)
00708     warn("Header indicates codec: %s\n", fourcc_interface->name);
00709   else
00710     interface = fourcc_interface;
00711 
00712   if (!interface) interface = get_vpx_decoder_by_index(0);
00713 
00714   dec_flags = (postproc ? VPX_CODEC_USE_POSTPROC : 0) |
00715               (ec_enabled ? VPX_CODEC_USE_ERROR_CONCEALMENT : 0) |
00716               (frame_parallel ? VPX_CODEC_USE_FRAME_THREADING : 0);
00717   if (vpx_codec_dec_init(&decoder, interface->codec_interface(), &cfg,
00718                          dec_flags)) {
00719     fprintf(stderr, "Failed to initialize decoder: %s\n",
00720             vpx_codec_error(&decoder));
00721     goto fail2;
00722   }
00723   if (svc_decoding) {
00724     if (vpx_codec_control(&decoder, VP9_DECODE_SVC_SPATIAL_LAYER,
00725                           svc_spatial_layer)) {
00726       fprintf(stderr, "Failed to set spatial layer for svc decode: %s\n",
00727               vpx_codec_error(&decoder));
00728       goto fail;
00729     }
00730   }
00731   if (!quiet) fprintf(stderr, "%s\n", decoder.name);
00732 
00733 #if CONFIG_VP8_DECODER
00734   if (vp8_pp_cfg.post_proc_flag &&
00735       vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg)) {
00736     fprintf(stderr, "Failed to configure postproc: %s\n",
00737             vpx_codec_error(&decoder));
00738     goto fail;
00739   }
00740 #endif
00741 
00742   if (arg_skip) fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
00743   while (arg_skip) {
00744     if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) break;
00745     arg_skip--;
00746   }
00747 
00748   if (num_external_frame_buffers > 0) {
00749     ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
00750     ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
00751         num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
00752     if (vpx_codec_set_frame_buffer_functions(&decoder, get_vp9_frame_buffer,
00753                                              release_vp9_frame_buffer,
00754                                              &ext_fb_list)) {
00755       fprintf(stderr, "Failed to configure external frame buffers: %s\n",
00756               vpx_codec_error(&decoder));
00757       goto fail;
00758     }
00759   }
00760 
00761   frame_avail = 1;
00762   got_data = 0;
00763 
00764   /* Decode file */
00765   while (frame_avail || got_data) {
00766     vpx_codec_iter_t iter = NULL;
00767     vpx_image_t *img;
00768     struct vpx_usec_timer timer;
00769     int corrupted = 0;
00770 
00771     frame_avail = 0;
00772     if (!stop_after || frame_in < stop_after) {
00773       if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
00774         frame_avail = 1;
00775         frame_in++;
00776 
00777         vpx_usec_timer_start(&timer);
00778 
00779         if (vpx_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer, NULL,
00780                              0)) {
00781           const char *detail = vpx_codec_error_detail(&decoder);
00782           warn("Failed to decode frame %d: %s", frame_in,
00783                vpx_codec_error(&decoder));
00784           if (detail) warn("Additional information: %s", detail);
00785           corrupted = 1;
00786           if (!keep_going) goto fail;
00787         }
00788 
00789         vpx_usec_timer_mark(&timer);
00790         dx_time += vpx_usec_timer_elapsed(&timer);
00791       } else {
00792         flush_decoder = 1;
00793       }
00794     } else {
00795       flush_decoder = 1;
00796     }
00797 
00798     vpx_usec_timer_start(&timer);
00799 
00800     if (flush_decoder) {
00801       // Flush the decoder in frame parallel decode.
00802       if (vpx_codec_decode(&decoder, NULL, 0, NULL, 0)) {
00803         warn("Failed to flush decoder: %s", vpx_codec_error(&decoder));
00804         corrupted = 1;
00805         if (!keep_going) goto fail;
00806       }
00807     }
00808 
00809     got_data = 0;
00810     if ((img = vpx_codec_get_frame(&decoder, &iter))) {
00811       ++frame_out;
00812       got_data = 1;
00813     }
00814 
00815     vpx_usec_timer_mark(&timer);
00816     dx_time += (unsigned int)vpx_usec_timer_elapsed(&timer);
00817 
00818     if (!frame_parallel && !corrupted &&
00819         vpx_codec_control(&decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted)) {
00820       warn("Failed VP8_GET_FRAME_CORRUPTED: %s", vpx_codec_error(&decoder));
00821       if (!keep_going) goto fail;
00822     }
00823     frames_corrupted += corrupted;
00824 
00825     if (progress) show_progress(frame_in, frame_out, dx_time);
00826 
00827     if (!noblit && img) {
00828       const int PLANES_YUV[] = { VPX_PLANE_Y, VPX_PLANE_U, VPX_PLANE_V };
00829       const int PLANES_YVU[] = { VPX_PLANE_Y, VPX_PLANE_V, VPX_PLANE_U };
00830       const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
00831 
00832       if (do_scale) {
00833         if (frame_out == 1) {
00834           // If the output frames are to be scaled to a fixed display size then
00835           // use the width and height specified in the container. If either of
00836           // these is set to 0, use the display size set in the first frame
00837           // header. If that is unavailable, use the raw decoded size of the
00838           // first decoded frame.
00839           int render_width = vpx_input_ctx.width;
00840           int render_height = vpx_input_ctx.height;
00841           if (!render_width || !render_height) {
00842             int render_size[2];
00843             if (vpx_codec_control(&decoder, VP9D_GET_DISPLAY_SIZE,
00844                                   render_size)) {
00845               // As last resort use size of first frame as display size.
00846               render_width = img->d_w;
00847               render_height = img->d_h;
00848             } else {
00849               render_width = render_size[0];
00850               render_height = render_size[1];
00851             }
00852           }
00853           scaled_img =
00854               vpx_img_alloc(NULL, img->fmt, render_width, render_height, 16);
00855           scaled_img->bit_depth = img->bit_depth;
00856         }
00857 
00858         if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
00859 #if CONFIG_LIBYUV
00860           libyuv_scale(img, scaled_img, kFilterBox);
00861           img = scaled_img;
00862 #else
00863           fprintf(stderr,
00864                   "Failed  to scale output frame: %s.\n"
00865                   "Scaling is disabled in this configuration. "
00866                   "To enable scaling, configure with --enable-libyuv\n",
00867                   vpx_codec_error(&decoder));
00868           goto fail;
00869 #endif
00870         }
00871       }
00872 #if CONFIG_VP9_HIGHBITDEPTH
00873       // Default to codec bit depth if output bit depth not set
00874       if (!output_bit_depth && single_file && !do_md5) {
00875         output_bit_depth = img->bit_depth;
00876       }
00877       // Shift up or down if necessary
00878       if (output_bit_depth != 0 && output_bit_depth != img->bit_depth) {
00879         const vpx_img_fmt_t shifted_fmt =
00880             output_bit_depth == 8
00881                 ? img->fmt ^ (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH)
00882                 : img->fmt | VPX_IMG_FMT_HIGHBITDEPTH;
00883         if (img_shifted &&
00884             img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
00885           vpx_img_free(img_shifted);
00886           img_shifted = NULL;
00887         }
00888         if (!img_shifted) {
00889           img_shifted =
00890               vpx_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
00891           img_shifted->bit_depth = output_bit_depth;
00892         }
00893         if (output_bit_depth > img->bit_depth) {
00894           vpx_img_upshift(img_shifted, img, output_bit_depth - img->bit_depth);
00895         } else {
00896           vpx_img_downshift(img_shifted, img,
00897                             img->bit_depth - output_bit_depth);
00898         }
00899         img = img_shifted;
00900       }
00901 #endif
00902 
00903       if (single_file) {
00904         if (use_y4m) {
00905           char buf[Y4M_BUFFER_SIZE] = { 0 };
00906           size_t len = 0;
00907           if (img->fmt == VPX_IMG_FMT_I440 || img->fmt == VPX_IMG_FMT_I44016) {
00908             fprintf(stderr, "Cannot produce y4m output for 440 sampling.\n");
00909             goto fail;
00910           }
00911           if (frame_out == 1) {
00912             // Y4M file header
00913             len = y4m_write_file_header(
00914                 buf, sizeof(buf), vpx_input_ctx.width, vpx_input_ctx.height,
00915                 &vpx_input_ctx.framerate, img->fmt, img->bit_depth);
00916             if (do_md5) {
00917               MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
00918             } else {
00919               fputs(buf, outfile);
00920             }
00921           }
00922 
00923           // Y4M frame header
00924           len = y4m_write_frame_header(buf, sizeof(buf));
00925           if (do_md5) {
00926             MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
00927           } else {
00928             fputs(buf, outfile);
00929           }
00930         } else {
00931           if (frame_out == 1) {
00932             // Check if --yv12 or --i420 options are consistent with the
00933             // bit-stream decoded
00934             if (opt_i420) {
00935               if (img->fmt != VPX_IMG_FMT_I420 &&
00936                   img->fmt != VPX_IMG_FMT_I42016) {
00937                 fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
00938                 goto fail;
00939               }
00940             }
00941             if (opt_yv12) {
00942               if ((img->fmt != VPX_IMG_FMT_I420 &&
00943                    img->fmt != VPX_IMG_FMT_YV12) ||
00944                   img->bit_depth != 8) {
00945                 fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
00946                 goto fail;
00947               }
00948             }
00949           }
00950         }
00951 
00952         if (do_md5) {
00953           update_image_md5(img, planes, &md5_ctx);
00954         } else {
00955           write_image_file(img, planes, outfile);
00956         }
00957       } else {
00958         generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
00959                           img->d_h, frame_in);
00960         if (do_md5) {
00961           MD5Init(&md5_ctx);
00962           update_image_md5(img, planes, &md5_ctx);
00963           MD5Final(md5_digest, &md5_ctx);
00964           print_md5(md5_digest, outfile_name);
00965         } else {
00966           outfile = open_outfile(outfile_name);
00967           write_image_file(img, planes, outfile);
00968           fclose(outfile);
00969         }
00970       }
00971     }
00972   }
00973 
00974   if (summary || progress) {
00975     show_progress(frame_in, frame_out, dx_time);
00976     fprintf(stderr, "\n");
00977   }
00978 
00979   if (frames_corrupted) {
00980     fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
00981   } else {
00982     ret = EXIT_SUCCESS;
00983   }
00984 
00985 fail:
00986 
00987   if (vpx_codec_destroy(&decoder)) {
00988     fprintf(stderr, "Failed to destroy decoder: %s\n",
00989             vpx_codec_error(&decoder));
00990   }
00991 
00992 fail2:
00993 
00994   if (!noblit && single_file) {
00995     if (do_md5) {
00996       MD5Final(md5_digest, &md5_ctx);
00997       print_md5(md5_digest, outfile_name);
00998     } else {
00999       fclose(outfile);
01000     }
01001   }
01002 
01003 #if CONFIG_WEBM_IO
01004   if (input.vpx_input_ctx->file_type == FILE_TYPE_WEBM)
01005     webm_free(input.webm_ctx);
01006 #endif
01007 
01008   if (input.vpx_input_ctx->file_type != FILE_TYPE_WEBM) free(buf);
01009 
01010   if (scaled_img) vpx_img_free(scaled_img);
01011 #if CONFIG_VP9_HIGHBITDEPTH
01012   if (img_shifted) vpx_img_free(img_shifted);
01013 #endif
01014 
01015   for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
01016     free(ext_fb_list.ext_fb[i].data);
01017   }
01018   free(ext_fb_list.ext_fb);
01019 
01020   fclose(infile);
01021   free(argv);
01022 
01023   return ret;
01024 }
01025 
01026 int main(int argc, const char **argv_) {
01027   unsigned int loops = 1, i;
01028   char **argv, **argi, **argj;
01029   struct arg arg;
01030   int error = 0;
01031 
01032   argv = argv_dup(argc - 1, argv_ + 1);
01033   for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
01034     memset(&arg, 0, sizeof(arg));
01035     arg.argv_step = 1;
01036 
01037     if (arg_match(&arg, &looparg, argi)) {
01038       loops = arg_parse_uint(&arg);
01039       break;
01040     }
01041   }
01042   free(argv);
01043   for (i = 0; !error && i < loops; i++) error = main_loop(argc, argv_);
01044   return error;
01045 }

Generated on 16 Jun 2017 for WebM Codec SDK by  doxygen 1.6.1