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