vpxenc

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 encodes YV12 files and generates ivf
00013  * files using the new interface.
00014  */
00015 #if defined(_WIN32) || !CONFIG_OS_SUPPORT
00016 #define USE_POSIX_MMAP 0
00017 #else
00018 #define USE_POSIX_MMAP 1
00019 #endif
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <stdarg.h>
00024 #include <string.h>
00025 #include <limits.h>
00026 #include <assert.h>
00027 #include "vpx/vpx_encoder.h"
00028 #if USE_POSIX_MMAP
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #include <sys/mman.h>
00032 #include <fcntl.h>
00033 #include <unistd.h>
00034 #endif
00035 #include "vpx/vp8cx.h"
00036 #include "vpx_ports/mem_ops.h"
00037 #include "vpx_ports/vpx_timer.h"
00038 #include "tools_common.h"
00039 #include "y4minput.h"
00040 #include "libmkv/EbmlWriter.h"
00041 #include "libmkv/EbmlIDs.h"
00042 
00043 /* Need special handling of these functions on Windows */
00044 #if defined(_MSC_VER)
00045 /* MSVS doesn't define off_t, and uses _f{seek,tell}i64 */
00046 typedef __int64 off_t;
00047 #define fseeko _fseeki64
00048 #define ftello _ftelli64
00049 #elif defined(_WIN32)
00050 /* MinGW defines off_t as long
00051    and uses f{seek,tell}o64/off64_t for large files */
00052 #define fseeko fseeko64
00053 #define ftello ftello64
00054 #define off_t off64_t
00055 #endif
00056 
00057 #if defined(_MSC_VER)
00058 #define LITERALU64(n) n
00059 #else
00060 #define LITERALU64(n) n##LLU
00061 #endif
00062 
00063 /* We should use 32-bit file operations in WebM file format
00064  * when building ARM executable file (.axf) with RVCT */
00065 #if !CONFIG_OS_SUPPORT
00066 typedef long off_t;
00067 #define fseeko fseek
00068 #define ftello ftell
00069 #endif
00070 
00071 static const char *exec_name;
00072 
00073 static const struct codec_item
00074 {
00075     char const              *name;
00076     const vpx_codec_iface_t *iface;
00077     unsigned int             fourcc;
00078 } codecs[] =
00079 {
00080 #if CONFIG_VP8_ENCODER
00081     {"vp8",  &vpx_codec_vp8_cx_algo, 0x30385056},
00082 #endif
00083 };
00084 
00085 static void usage_exit();
00086 
00087 #define LOG_ERROR(label) do \
00088 {\
00089     const char *l=label;\
00090     va_list ap;\
00091     va_start(ap, fmt);\
00092     if(l)\
00093         fprintf(stderr, "%s: ", l);\
00094     vfprintf(stderr, fmt, ap);\
00095     fprintf(stderr, "\n");\
00096     va_end(ap);\
00097 } while(0)
00098 
00099 void die(const char *fmt, ...)
00100 {
00101     LOG_ERROR(NULL);
00102     usage_exit();
00103 }
00104 
00105 
00106 void fatal(const char *fmt, ...)
00107 {
00108     LOG_ERROR("Fatal");
00109     exit(EXIT_FAILURE);
00110 }
00111 
00112 
00113 void warn(const char *fmt, ...)
00114 {
00115     LOG_ERROR("Warning");
00116 }
00117 
00118 
00119 static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s, ...)
00120 {
00121     va_list ap;
00122 
00123     va_start(ap, s);
00124     if (ctx->err)
00125     {
00126         const char *detail = vpx_codec_error_detail(ctx);
00127 
00128         vfprintf(stderr, s, ap);
00129         fprintf(stderr, ": %s\n", vpx_codec_error(ctx));
00130 
00131         if (detail)
00132             fprintf(stderr, "    %s\n", detail);
00133 
00134         exit(EXIT_FAILURE);
00135     }
00136 }
00137 
00138 /* This structure is used to abstract the different ways of handling
00139  * first pass statistics.
00140  */
00141 typedef struct
00142 {
00143     vpx_fixed_buf_t buf;
00144     int             pass;
00145     FILE           *file;
00146     char           *buf_ptr;
00147     size_t          buf_alloc_sz;
00148 } stats_io_t;
00149 
00150 int stats_open_file(stats_io_t *stats, const char *fpf, int pass)
00151 {
00152     int res;
00153 
00154     stats->pass = pass;
00155 
00156     if (pass == 0)
00157     {
00158         stats->file = fopen(fpf, "wb");
00159         stats->buf.sz = 0;
00160         stats->buf.buf = NULL,
00161                    res = (stats->file != NULL);
00162     }
00163     else
00164     {
00165 #if 0
00166 #elif USE_POSIX_MMAP
00167         struct stat stat_buf;
00168         int fd;
00169 
00170         fd = open(fpf, O_RDONLY);
00171         stats->file = fdopen(fd, "rb");
00172         fstat(fd, &stat_buf);
00173         stats->buf.sz = stat_buf.st_size;
00174         stats->buf.buf = mmap(NULL, stats->buf.sz, PROT_READ, MAP_PRIVATE,
00175                               fd, 0);
00176         res = (stats->buf.buf != NULL);
00177 #else
00178         size_t nbytes;
00179 
00180         stats->file = fopen(fpf, "rb");
00181 
00182         if (fseek(stats->file, 0, SEEK_END))
00183             fatal("First-pass stats file must be seekable!");
00184 
00185         stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file);
00186         rewind(stats->file);
00187 
00188         stats->buf.buf = malloc(stats->buf_alloc_sz);
00189 
00190         if (!stats->buf.buf)
00191             fatal("Failed to allocate first-pass stats buffer (%lu bytes)",
00192                   (unsigned long)stats->buf_alloc_sz);
00193 
00194         nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
00195         res = (nbytes == stats->buf.sz);
00196 #endif
00197     }
00198 
00199     return res;
00200 }
00201 
00202 int stats_open_mem(stats_io_t *stats, int pass)
00203 {
00204     int res;
00205     stats->pass = pass;
00206 
00207     if (!pass)
00208     {
00209         stats->buf.sz = 0;
00210         stats->buf_alloc_sz = 64 * 1024;
00211         stats->buf.buf = malloc(stats->buf_alloc_sz);
00212     }
00213 
00214     stats->buf_ptr = stats->buf.buf;
00215     res = (stats->buf.buf != NULL);
00216     return res;
00217 }
00218 
00219 
00220 void stats_close(stats_io_t *stats, int last_pass)
00221 {
00222     if (stats->file)
00223     {
00224         if (stats->pass == last_pass)
00225         {
00226 #if 0
00227 #elif USE_POSIX_MMAP
00228             munmap(stats->buf.buf, stats->buf.sz);
00229 #else
00230             free(stats->buf.buf);
00231 #endif
00232         }
00233 
00234         fclose(stats->file);
00235         stats->file = NULL;
00236     }
00237     else
00238     {
00239         if (stats->pass == last_pass)
00240             free(stats->buf.buf);
00241     }
00242 }
00243 
00244 void stats_write(stats_io_t *stats, const void *pkt, size_t len)
00245 {
00246     if (stats->file)
00247     {
00248         if(fwrite(pkt, 1, len, stats->file));
00249     }
00250     else
00251     {
00252         if (stats->buf.sz + len > stats->buf_alloc_sz)
00253         {
00254             size_t  new_sz = stats->buf_alloc_sz + 64 * 1024;
00255             char   *new_ptr = realloc(stats->buf.buf, new_sz);
00256 
00257             if (new_ptr)
00258             {
00259                 stats->buf_ptr = new_ptr + (stats->buf_ptr - (char *)stats->buf.buf);
00260                 stats->buf.buf = new_ptr;
00261                 stats->buf_alloc_sz = new_sz;
00262             }
00263             else
00264                 fatal("Failed to realloc firstpass stats buffer.");
00265         }
00266 
00267         memcpy(stats->buf_ptr, pkt, len);
00268         stats->buf.sz += len;
00269         stats->buf_ptr += len;
00270     }
00271 }
00272 
00273 vpx_fixed_buf_t stats_get(stats_io_t *stats)
00274 {
00275     return stats->buf;
00276 }
00277 
00278 /* Stereo 3D packed frame format */
00279 typedef enum stereo_format
00280 {
00281     STEREO_FORMAT_MONO       = 0,
00282     STEREO_FORMAT_LEFT_RIGHT = 1,
00283     STEREO_FORMAT_BOTTOM_TOP = 2,
00284     STEREO_FORMAT_TOP_BOTTOM = 3,
00285     STEREO_FORMAT_RIGHT_LEFT = 11
00286 } stereo_format_t;
00287 
00288 enum video_file_type
00289 {
00290     FILE_TYPE_RAW,
00291     FILE_TYPE_IVF,
00292     FILE_TYPE_Y4M
00293 };
00294 
00295 struct detect_buffer {
00296     char buf[4];
00297     size_t buf_read;
00298     size_t position;
00299 };
00300 
00301 
00302 struct input_state
00303 {
00304     char                 *fn;
00305     FILE                 *file;
00306     y4m_input             y4m;
00307     struct detect_buffer  detect;
00308     enum video_file_type  file_type;
00309     unsigned int          w;
00310     unsigned int          h;
00311     struct vpx_rational   framerate;
00312     int                   use_i420;
00313 };
00314 
00315 
00316 #define IVF_FRAME_HDR_SZ (4+8) /* 4 byte size + 8 byte timestamp */
00317 static int read_frame(struct input_state *input, vpx_image_t *img)
00318 {
00319     FILE *f = input->file;
00320     enum video_file_type file_type = input->file_type;
00321     y4m_input *y4m = &input->y4m;
00322     struct detect_buffer *detect = &input->detect;
00323     int plane = 0;
00324     int shortread = 0;
00325 
00326     if (file_type == FILE_TYPE_Y4M)
00327     {
00328         if (y4m_input_fetch_frame(y4m, f, img) < 1)
00329            return 0;
00330     }
00331     else
00332     {
00333         if (file_type == FILE_TYPE_IVF)
00334         {
00335             char junk[IVF_FRAME_HDR_SZ];
00336 
00337             /* Skip the frame header. We know how big the frame should be. See
00338              * write_ivf_frame_header() for documentation on the frame header
00339              * layout.
00340              */
00341             if(fread(junk, 1, IVF_FRAME_HDR_SZ, f));
00342         }
00343 
00344         for (plane = 0; plane < 3; plane++)
00345         {
00346             unsigned char *ptr;
00347             int w = (plane ? (1 + img->d_w) / 2 : img->d_w);
00348             int h = (plane ? (1 + img->d_h) / 2 : img->d_h);
00349             int r;
00350 
00351             /* Determine the correct plane based on the image format. The for-loop
00352              * always counts in Y,U,V order, but this may not match the order of
00353              * the data on disk.
00354              */
00355             switch (plane)
00356             {
00357             case 1:
00358                 ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12? VPX_PLANE_V : VPX_PLANE_U];
00359                 break;
00360             case 2:
00361                 ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12?VPX_PLANE_U : VPX_PLANE_V];
00362                 break;
00363             default:
00364                 ptr = img->planes[plane];
00365             }
00366 
00367             for (r = 0; r < h; r++)
00368             {
00369                 size_t needed = w;
00370                 size_t buf_position = 0;
00371                 const size_t left = detect->buf_read - detect->position;
00372                 if (left > 0)
00373                 {
00374                     const size_t more = (left < needed) ? left : needed;
00375                     memcpy(ptr, detect->buf + detect->position, more);
00376                     buf_position = more;
00377                     needed -= more;
00378                     detect->position += more;
00379                 }
00380                 if (needed > 0)
00381                 {
00382                     shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
00383                 }
00384 
00385                 ptr += img->stride[plane];
00386             }
00387         }
00388     }
00389 
00390     return !shortread;
00391 }
00392 
00393 
00394 unsigned int file_is_y4m(FILE      *infile,
00395                          y4m_input *y4m,
00396                          char       detect[4])
00397 {
00398     if(memcmp(detect, "YUV4", 4) == 0)
00399     {
00400         return 1;
00401     }
00402     return 0;
00403 }
00404 
00405 #define IVF_FILE_HDR_SZ (32)
00406 unsigned int file_is_ivf(struct input_state *input,
00407                          unsigned int *fourcc)
00408 {
00409     char raw_hdr[IVF_FILE_HDR_SZ];
00410     int is_ivf = 0;
00411     FILE *infile = input->file;
00412     unsigned int *width = &input->w;
00413     unsigned int *height = &input->h;
00414     struct detect_buffer *detect = &input->detect;
00415 
00416     if(memcmp(detect->buf, "DKIF", 4) != 0)
00417         return 0;
00418 
00419     /* See write_ivf_file_header() for more documentation on the file header
00420      * layout.
00421      */
00422     if (fread(raw_hdr + 4, 1, IVF_FILE_HDR_SZ - 4, infile)
00423         == IVF_FILE_HDR_SZ - 4)
00424     {
00425         {
00426             is_ivf = 1;
00427 
00428             if (mem_get_le16(raw_hdr + 4) != 0)
00429                 warn("Unrecognized IVF version! This file may not decode "
00430                      "properly.");
00431 
00432             *fourcc = mem_get_le32(raw_hdr + 8);
00433         }
00434     }
00435 
00436     if (is_ivf)
00437     {
00438         *width = mem_get_le16(raw_hdr + 12);
00439         *height = mem_get_le16(raw_hdr + 14);
00440         detect->position = 4;
00441     }
00442 
00443     return is_ivf;
00444 }
00445 
00446 
00447 static void write_ivf_file_header(FILE *outfile,
00448                                   const vpx_codec_enc_cfg_t *cfg,
00449                                   unsigned int fourcc,
00450                                   int frame_cnt)
00451 {
00452     char header[32];
00453 
00454     if (cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS)
00455         return;
00456 
00457     header[0] = 'D';
00458     header[1] = 'K';
00459     header[2] = 'I';
00460     header[3] = 'F';
00461     mem_put_le16(header + 4,  0);                 /* version */
00462     mem_put_le16(header + 6,  32);                /* headersize */
00463     mem_put_le32(header + 8,  fourcc);            /* headersize */
00464     mem_put_le16(header + 12, cfg->g_w);          /* width */
00465     mem_put_le16(header + 14, cfg->g_h);          /* height */
00466     mem_put_le32(header + 16, cfg->g_timebase.den); /* rate */
00467     mem_put_le32(header + 20, cfg->g_timebase.num); /* scale */
00468     mem_put_le32(header + 24, frame_cnt);         /* length */
00469     mem_put_le32(header + 28, 0);                 /* unused */
00470 
00471     if(fwrite(header, 1, 32, outfile));
00472 }
00473 
00474 
00475 static void write_ivf_frame_header(FILE *outfile,
00476                                    const vpx_codec_cx_pkt_t *pkt)
00477 {
00478     char             header[12];
00479     vpx_codec_pts_t  pts;
00480 
00481     if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
00482         return;
00483 
00484     pts = pkt->data.frame.pts;
00485     mem_put_le32(header, pkt->data.frame.sz);
00486     mem_put_le32(header + 4, pts & 0xFFFFFFFF);
00487     mem_put_le32(header + 8, pts >> 32);
00488 
00489     if(fwrite(header, 1, 12, outfile));
00490 }
00491 
00492 static void write_ivf_frame_size(FILE *outfile, size_t size)
00493 {
00494     char             header[4];
00495     mem_put_le32(header, size);
00496     fwrite(header, 1, 4, outfile);
00497 }
00498 
00499 
00500 typedef off_t EbmlLoc;
00501 
00502 
00503 struct cue_entry
00504 {
00505     unsigned int time;
00506     uint64_t     loc;
00507 };
00508 
00509 
00510 struct EbmlGlobal
00511 {
00512     int debug;
00513 
00514     FILE    *stream;
00515     int64_t last_pts_ms;
00516     vpx_rational_t  framerate;
00517 
00518     /* These pointers are to the start of an element */
00519     off_t    position_reference;
00520     off_t    seek_info_pos;
00521     off_t    segment_info_pos;
00522     off_t    track_pos;
00523     off_t    cue_pos;
00524     off_t    cluster_pos;
00525 
00526     /* This pointer is to a specific element to be serialized */
00527     off_t    track_id_pos;
00528 
00529     /* These pointers are to the size field of the element */
00530     EbmlLoc  startSegment;
00531     EbmlLoc  startCluster;
00532 
00533     uint32_t cluster_timecode;
00534     int      cluster_open;
00535 
00536     struct cue_entry *cue_list;
00537     unsigned int      cues;
00538 
00539 };
00540 
00541 
00542 void Ebml_Write(EbmlGlobal *glob, const void *buffer_in, unsigned long len)
00543 {
00544     if(fwrite(buffer_in, 1, len, glob->stream));
00545 }
00546 
00547 #define WRITE_BUFFER(s) \
00548 for(i = len-1; i>=0; i--)\
00549 { \
00550     x = *(const s *)buffer_in >> (i * CHAR_BIT); \
00551     Ebml_Write(glob, &x, 1); \
00552 }
00553 void Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, int buffer_size, unsigned long len)
00554 {
00555     char x;
00556     int i;
00557 
00558     /* buffer_size:
00559      * 1 - int8_t;
00560      * 2 - int16_t;
00561      * 3 - int32_t;
00562      * 4 - int64_t;
00563      */
00564     switch (buffer_size)
00565     {
00566         case 1:
00567             WRITE_BUFFER(int8_t)
00568             break;
00569         case 2:
00570             WRITE_BUFFER(int16_t)
00571             break;
00572         case 4:
00573             WRITE_BUFFER(int32_t)
00574             break;
00575         case 8:
00576             WRITE_BUFFER(int64_t)
00577             break;
00578         default:
00579             break;
00580     }
00581 }
00582 #undef WRITE_BUFFER
00583 
00584 /* Need a fixed size serializer for the track ID. libmkv provides a 64 bit
00585  * one, but not a 32 bit one.
00586  */
00587 static void Ebml_SerializeUnsigned32(EbmlGlobal *glob, unsigned long class_id, uint64_t ui)
00588 {
00589     unsigned char sizeSerialized = 4 | 0x80;
00590     Ebml_WriteID(glob, class_id);
00591     Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1);
00592     Ebml_Serialize(glob, &ui, sizeof(ui), 4);
00593 }
00594 
00595 
00596 static void
00597 Ebml_StartSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc,
00598                           unsigned long class_id)
00599 {
00600     //todo this is always taking 8 bytes, this may need later optimization
00601     //this is a key that says length unknown
00602     uint64_t unknownLen =  LITERALU64(0x01FFFFFFFFFFFFFF);
00603 
00604     Ebml_WriteID(glob, class_id);
00605     *ebmlLoc = ftello(glob->stream);
00606     Ebml_Serialize(glob, &unknownLen, sizeof(unknownLen), 8);
00607 }
00608 
00609 static void
00610 Ebml_EndSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc)
00611 {
00612     off_t pos;
00613     uint64_t size;
00614 
00615     /* Save the current stream pointer */
00616     pos = ftello(glob->stream);
00617 
00618     /* Calculate the size of this element */
00619     size = pos - *ebmlLoc - 8;
00620     size |=  LITERALU64(0x0100000000000000);
00621 
00622     /* Seek back to the beginning of the element and write the new size */
00623     fseeko(glob->stream, *ebmlLoc, SEEK_SET);
00624     Ebml_Serialize(glob, &size, sizeof(size), 8);
00625 
00626     /* Reset the stream pointer */
00627     fseeko(glob->stream, pos, SEEK_SET);
00628 }
00629 
00630 
00631 static void
00632 write_webm_seek_element(EbmlGlobal *ebml, unsigned long id, off_t pos)
00633 {
00634     uint64_t offset = pos - ebml->position_reference;
00635     EbmlLoc start;
00636     Ebml_StartSubElement(ebml, &start, Seek);
00637     Ebml_SerializeBinary(ebml, SeekID, id);
00638     Ebml_SerializeUnsigned64(ebml, SeekPosition, offset);
00639     Ebml_EndSubElement(ebml, &start);
00640 }
00641 
00642 
00643 static void
00644 write_webm_seek_info(EbmlGlobal *ebml)
00645 {
00646 
00647     off_t pos;
00648 
00649     /* Save the current stream pointer */
00650     pos = ftello(ebml->stream);
00651 
00652     if(ebml->seek_info_pos)
00653         fseeko(ebml->stream, ebml->seek_info_pos, SEEK_SET);
00654     else
00655         ebml->seek_info_pos = pos;
00656 
00657     {
00658         EbmlLoc start;
00659 
00660         Ebml_StartSubElement(ebml, &start, SeekHead);
00661         write_webm_seek_element(ebml, Tracks, ebml->track_pos);
00662         write_webm_seek_element(ebml, Cues,   ebml->cue_pos);
00663         write_webm_seek_element(ebml, Info,   ebml->segment_info_pos);
00664         Ebml_EndSubElement(ebml, &start);
00665     }
00666     {
00667         //segment info
00668         EbmlLoc startInfo;
00669         uint64_t frame_time;
00670         char version_string[64];
00671 
00672         /* Assemble version string */
00673         if(ebml->debug)
00674             strcpy(version_string, "vpxenc");
00675         else
00676         {
00677             strcpy(version_string, "vpxenc ");
00678             strncat(version_string,
00679                     vpx_codec_version_str(),
00680                     sizeof(version_string) - 1 - strlen(version_string));
00681         }
00682 
00683         frame_time = (uint64_t)1000 * ebml->framerate.den
00684                      / ebml->framerate.num;
00685         ebml->segment_info_pos = ftello(ebml->stream);
00686         Ebml_StartSubElement(ebml, &startInfo, Info);
00687         Ebml_SerializeUnsigned(ebml, TimecodeScale, 1000000);
00688         Ebml_SerializeFloat(ebml, Segment_Duration,
00689                             ebml->last_pts_ms + frame_time);
00690         Ebml_SerializeString(ebml, 0x4D80, version_string);
00691         Ebml_SerializeString(ebml, 0x5741, version_string);
00692         Ebml_EndSubElement(ebml, &startInfo);
00693     }
00694 }
00695 
00696 
00697 static void
00698 write_webm_file_header(EbmlGlobal                *glob,
00699                        const vpx_codec_enc_cfg_t *cfg,
00700                        const struct vpx_rational *fps,
00701                        stereo_format_t            stereo_fmt)
00702 {
00703     {
00704         EbmlLoc start;
00705         Ebml_StartSubElement(glob, &start, EBML);
00706         Ebml_SerializeUnsigned(glob, EBMLVersion, 1);
00707         Ebml_SerializeUnsigned(glob, EBMLReadVersion, 1); //EBML Read Version
00708         Ebml_SerializeUnsigned(glob, EBMLMaxIDLength, 4); //EBML Max ID Length
00709         Ebml_SerializeUnsigned(glob, EBMLMaxSizeLength, 8); //EBML Max Size Length
00710         Ebml_SerializeString(glob, DocType, "webm"); //Doc Type
00711         Ebml_SerializeUnsigned(glob, DocTypeVersion, 2); //Doc Type Version
00712         Ebml_SerializeUnsigned(glob, DocTypeReadVersion, 2); //Doc Type Read Version
00713         Ebml_EndSubElement(glob, &start);
00714     }
00715     {
00716         Ebml_StartSubElement(glob, &glob->startSegment, Segment); //segment
00717         glob->position_reference = ftello(glob->stream);
00718         glob->framerate = *fps;
00719         write_webm_seek_info(glob);
00720 
00721         {
00722             EbmlLoc trackStart;
00723             glob->track_pos = ftello(glob->stream);
00724             Ebml_StartSubElement(glob, &trackStart, Tracks);
00725             {
00726                 unsigned int trackNumber = 1;
00727                 uint64_t     trackID = 0;
00728 
00729                 EbmlLoc start;
00730                 Ebml_StartSubElement(glob, &start, TrackEntry);
00731                 Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
00732                 glob->track_id_pos = ftello(glob->stream);
00733                 Ebml_SerializeUnsigned32(glob, TrackUID, trackID);
00734                 Ebml_SerializeUnsigned(glob, TrackType, 1); //video is always 1
00735                 Ebml_SerializeString(glob, CodecID, "V_VP8");
00736                 {
00737                     unsigned int pixelWidth = cfg->g_w;
00738                     unsigned int pixelHeight = cfg->g_h;
00739                     float        frameRate   = (float)fps->num/(float)fps->den;
00740 
00741                     EbmlLoc videoStart;
00742                     Ebml_StartSubElement(glob, &videoStart, Video);
00743                     Ebml_SerializeUnsigned(glob, PixelWidth, pixelWidth);
00744                     Ebml_SerializeUnsigned(glob, PixelHeight, pixelHeight);
00745                     Ebml_SerializeUnsigned(glob, StereoMode, stereo_fmt);
00746                     Ebml_SerializeFloat(glob, FrameRate, frameRate);
00747                     Ebml_EndSubElement(glob, &videoStart); //Video
00748                 }
00749                 Ebml_EndSubElement(glob, &start); //Track Entry
00750             }
00751             Ebml_EndSubElement(glob, &trackStart);
00752         }
00753         // segment element is open
00754     }
00755 }
00756 
00757 
00758 static void
00759 write_webm_block(EbmlGlobal                *glob,
00760                  const vpx_codec_enc_cfg_t *cfg,
00761                  const vpx_codec_cx_pkt_t  *pkt)
00762 {
00763     unsigned long  block_length;
00764     unsigned char  track_number;
00765     unsigned short block_timecode = 0;
00766     unsigned char  flags;
00767     int64_t        pts_ms;
00768     int            start_cluster = 0, is_keyframe;
00769 
00770     /* Calculate the PTS of this frame in milliseconds */
00771     pts_ms = pkt->data.frame.pts * 1000
00772              * (uint64_t)cfg->g_timebase.num / (uint64_t)cfg->g_timebase.den;
00773     if(pts_ms <= glob->last_pts_ms)
00774         pts_ms = glob->last_pts_ms + 1;
00775     glob->last_pts_ms = pts_ms;
00776 
00777     /* Calculate the relative time of this block */
00778     if(pts_ms - glob->cluster_timecode > SHRT_MAX)
00779         start_cluster = 1;
00780     else
00781         block_timecode = pts_ms - glob->cluster_timecode;
00782 
00783     is_keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY);
00784     if(start_cluster || is_keyframe)
00785     {
00786         if(glob->cluster_open)
00787             Ebml_EndSubElement(glob, &glob->startCluster);
00788 
00789         /* Open the new cluster */
00790         block_timecode = 0;
00791         glob->cluster_open = 1;
00792         glob->cluster_timecode = pts_ms;
00793         glob->cluster_pos = ftello(glob->stream);
00794         Ebml_StartSubElement(glob, &glob->startCluster, Cluster); //cluster
00795         Ebml_SerializeUnsigned(glob, Timecode, glob->cluster_timecode);
00796 
00797         /* Save a cue point if this is a keyframe. */
00798         if(is_keyframe)
00799         {
00800             struct cue_entry *cue, *new_cue_list;
00801 
00802             new_cue_list = realloc(glob->cue_list,
00803                                    (glob->cues+1) * sizeof(struct cue_entry));
00804             if(new_cue_list)
00805                 glob->cue_list = new_cue_list;
00806             else
00807                 fatal("Failed to realloc cue list.");
00808 
00809             cue = &glob->cue_list[glob->cues];
00810             cue->time = glob->cluster_timecode;
00811             cue->loc = glob->cluster_pos;
00812             glob->cues++;
00813         }
00814     }
00815 
00816     /* Write the Simple Block */
00817     Ebml_WriteID(glob, SimpleBlock);
00818 
00819     block_length = pkt->data.frame.sz + 4;
00820     block_length |= 0x10000000;
00821     Ebml_Serialize(glob, &block_length, sizeof(block_length), 4);
00822 
00823     track_number = 1;
00824     track_number |= 0x80;
00825     Ebml_Write(glob, &track_number, 1);
00826 
00827     Ebml_Serialize(glob, &block_timecode, sizeof(block_timecode), 2);
00828 
00829     flags = 0;
00830     if(is_keyframe)
00831         flags |= 0x80;
00832     if(pkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE)
00833         flags |= 0x08;
00834     Ebml_Write(glob, &flags, 1);
00835 
00836     Ebml_Write(glob, pkt->data.frame.buf, pkt->data.frame.sz);
00837 }
00838 
00839 
00840 static void
00841 write_webm_file_footer(EbmlGlobal *glob, long hash)
00842 {
00843 
00844     if(glob->cluster_open)
00845         Ebml_EndSubElement(glob, &glob->startCluster);
00846 
00847     {
00848         EbmlLoc start;
00849         unsigned int i;
00850 
00851         glob->cue_pos = ftello(glob->stream);
00852         Ebml_StartSubElement(glob, &start, Cues);
00853         for(i=0; i<glob->cues; i++)
00854         {
00855             struct cue_entry *cue = &glob->cue_list[i];
00856             EbmlLoc start;
00857 
00858             Ebml_StartSubElement(glob, &start, CuePoint);
00859             {
00860                 EbmlLoc start;
00861 
00862                 Ebml_SerializeUnsigned(glob, CueTime, cue->time);
00863 
00864                 Ebml_StartSubElement(glob, &start, CueTrackPositions);
00865                 Ebml_SerializeUnsigned(glob, CueTrack, 1);
00866                 Ebml_SerializeUnsigned64(glob, CueClusterPosition,
00867                                          cue->loc - glob->position_reference);
00868                 //Ebml_SerializeUnsigned(glob, CueBlockNumber, cue->blockNumber);
00869                 Ebml_EndSubElement(glob, &start);
00870             }
00871             Ebml_EndSubElement(glob, &start);
00872         }
00873         Ebml_EndSubElement(glob, &start);
00874     }
00875 
00876     Ebml_EndSubElement(glob, &glob->startSegment);
00877 
00878     /* Patch up the seek info block */
00879     write_webm_seek_info(glob);
00880 
00881     /* Patch up the track id */
00882     fseeko(glob->stream, glob->track_id_pos, SEEK_SET);
00883     Ebml_SerializeUnsigned32(glob, TrackUID, glob->debug ? 0xDEADBEEF : hash);
00884 
00885     fseeko(glob->stream, 0, SEEK_END);
00886 }
00887 
00888 
00889 /* Murmur hash derived from public domain reference implementation at
00890  *   http://sites.google.com/site/murmurhash/
00891  */
00892 static unsigned int murmur ( const void * key, int len, unsigned int seed )
00893 {
00894     const unsigned int m = 0x5bd1e995;
00895     const int r = 24;
00896 
00897     unsigned int h = seed ^ len;
00898 
00899     const unsigned char * data = (const unsigned char *)key;
00900 
00901     while(len >= 4)
00902     {
00903         unsigned int k;
00904 
00905         k  = data[0];
00906         k |= data[1] << 8;
00907         k |= data[2] << 16;
00908         k |= data[3] << 24;
00909 
00910         k *= m;
00911         k ^= k >> r;
00912         k *= m;
00913 
00914         h *= m;
00915         h ^= k;
00916 
00917         data += 4;
00918         len -= 4;
00919     }
00920 
00921     switch(len)
00922     {
00923     case 3: h ^= data[2] << 16;
00924     case 2: h ^= data[1] << 8;
00925     case 1: h ^= data[0];
00926             h *= m;
00927     };
00928 
00929     h ^= h >> 13;
00930     h *= m;
00931     h ^= h >> 15;
00932 
00933     return h;
00934 }
00935 
00936 #include "math.h"
00937 
00938 static double vp8_mse2psnr(double Samples, double Peak, double Mse)
00939 {
00940     double psnr;
00941 
00942     if ((double)Mse > 0.0)
00943         psnr = 10.0 * log10(Peak * Peak * Samples / Mse);
00944     else
00945         psnr = 60;      // Limit to prevent / 0
00946 
00947     if (psnr > 60)
00948         psnr = 60;
00949 
00950     return psnr;
00951 }
00952 
00953 
00954 #include "args.h"
00955 static const arg_def_t debugmode = ARG_DEF("D", "debug", 0,
00956         "Debug mode (makes output deterministic)");
00957 static const arg_def_t outputfile = ARG_DEF("o", "output", 1,
00958         "Output filename");
00959 static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0,
00960                                   "Input file is YV12 ");
00961 static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0,
00962                                   "Input file is I420 (default)");
00963 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1,
00964                                   "Codec to use");
00965 static const arg_def_t passes           = ARG_DEF("p", "passes", 1,
00966         "Number of passes (1/2)");
00967 static const arg_def_t pass_arg         = ARG_DEF(NULL, "pass", 1,
00968         "Pass to execute (1/2)");
00969 static const arg_def_t fpf_name         = ARG_DEF(NULL, "fpf", 1,
00970         "First pass statistics file name");
00971 static const arg_def_t limit = ARG_DEF(NULL, "limit", 1,
00972                                        "Stop encoding after n input frames");
00973 static const arg_def_t deadline         = ARG_DEF("d", "deadline", 1,
00974         "Deadline per frame (usec)");
00975 static const arg_def_t best_dl          = ARG_DEF(NULL, "best", 0,
00976         "Use Best Quality Deadline");
00977 static const arg_def_t good_dl          = ARG_DEF(NULL, "good", 0,
00978         "Use Good Quality Deadline");
00979 static const arg_def_t rt_dl            = ARG_DEF(NULL, "rt", 0,
00980         "Use Realtime Quality Deadline");
00981 static const arg_def_t verbosearg       = ARG_DEF("v", "verbose", 0,
00982         "Show encoder parameters");
00983 static const arg_def_t psnrarg          = ARG_DEF(NULL, "psnr", 0,
00984         "Show PSNR in status line");
00985 static const arg_def_t framerate        = ARG_DEF(NULL, "fps", 1,
00986         "Stream frame rate (rate/scale)");
00987 static const arg_def_t use_ivf          = ARG_DEF(NULL, "ivf", 0,
00988         "Output IVF (default is WebM)");
00989 static const arg_def_t out_part = ARG_DEF("P", "output-partitions", 0,
00990         "Makes encoder output partitions. Requires IVF output!");
00991 static const arg_def_t q_hist_n         = ARG_DEF(NULL, "q-hist", 1,
00992         "Show quantizer histogram (n-buckets)");
00993 static const arg_def_t rate_hist_n         = ARG_DEF(NULL, "rate-hist", 1,
00994         "Show rate histogram (n-buckets)");
00995 static const arg_def_t *main_args[] =
00996 {
00997     &debugmode,
00998     &outputfile, &codecarg, &passes, &pass_arg, &fpf_name, &limit, &deadline,
00999     &best_dl, &good_dl, &rt_dl,
01000     &verbosearg, &psnrarg, &use_ivf, &out_part, &q_hist_n, &rate_hist_n,
01001     NULL
01002 };
01003 
01004 static const arg_def_t usage            = ARG_DEF("u", "usage", 1,
01005         "Usage profile number to use");
01006 static const arg_def_t threads          = ARG_DEF("t", "threads", 1,
01007         "Max number of threads to use");
01008 static const arg_def_t profile          = ARG_DEF(NULL, "profile", 1,
01009         "Bitstream profile number to use");
01010 static const arg_def_t width            = ARG_DEF("w", "width", 1,
01011         "Frame width");
01012 static const arg_def_t height           = ARG_DEF("h", "height", 1,
01013         "Frame height");
01014 static const struct arg_enum_list stereo_mode_enum[] = {
01015     {"mono"      , STEREO_FORMAT_MONO},
01016     {"left-right", STEREO_FORMAT_LEFT_RIGHT},
01017     {"bottom-top", STEREO_FORMAT_BOTTOM_TOP},
01018     {"top-bottom", STEREO_FORMAT_TOP_BOTTOM},
01019     {"right-left", STEREO_FORMAT_RIGHT_LEFT},
01020     {NULL, 0}
01021 };
01022 static const arg_def_t stereo_mode      = ARG_DEF_ENUM(NULL, "stereo-mode", 1,
01023         "Stereo 3D video format", stereo_mode_enum);
01024 static const arg_def_t timebase         = ARG_DEF(NULL, "timebase", 1,
01025         "Output timestamp precision (fractional seconds)");
01026 static const arg_def_t error_resilient  = ARG_DEF(NULL, "error-resilient", 1,
01027         "Enable error resiliency features");
01028 static const arg_def_t lag_in_frames    = ARG_DEF(NULL, "lag-in-frames", 1,
01029         "Max number of frames to lag");
01030 
01031 static const arg_def_t *global_args[] =
01032 {
01033     &use_yv12, &use_i420, &usage, &threads, &profile,
01034     &width, &height, &stereo_mode, &timebase, &framerate, &error_resilient,
01035     &lag_in_frames, NULL
01036 };
01037 
01038 static const arg_def_t dropframe_thresh   = ARG_DEF(NULL, "drop-frame", 1,
01039         "Temporal resampling threshold (buf %)");
01040 static const arg_def_t resize_allowed     = ARG_DEF(NULL, "resize-allowed", 1,
01041         "Spatial resampling enabled (bool)");
01042 static const arg_def_t resize_up_thresh   = ARG_DEF(NULL, "resize-up", 1,
01043         "Upscale threshold (buf %)");
01044 static const arg_def_t resize_down_thresh = ARG_DEF(NULL, "resize-down", 1,
01045         "Downscale threshold (buf %)");
01046 static const struct arg_enum_list end_usage_enum[] = {
01047     {"vbr", VPX_VBR},
01048     {"cbr", VPX_CBR},
01049     {"cq",  VPX_CQ},
01050     {NULL, 0}
01051 };
01052 static const arg_def_t end_usage          = ARG_DEF_ENUM(NULL, "end-usage", 1,
01053         "Rate control mode", end_usage_enum);
01054 static const arg_def_t target_bitrate     = ARG_DEF(NULL, "target-bitrate", 1,
01055         "Bitrate (kbps)");
01056 static const arg_def_t min_quantizer      = ARG_DEF(NULL, "min-q", 1,
01057         "Minimum (best) quantizer");
01058 static const arg_def_t max_quantizer      = ARG_DEF(NULL, "max-q", 1,
01059         "Maximum (worst) quantizer");
01060 static const arg_def_t undershoot_pct     = ARG_DEF(NULL, "undershoot-pct", 1,
01061         "Datarate undershoot (min) target (%)");
01062 static const arg_def_t overshoot_pct      = ARG_DEF(NULL, "overshoot-pct", 1,
01063         "Datarate overshoot (max) target (%)");
01064 static const arg_def_t buf_sz             = ARG_DEF(NULL, "buf-sz", 1,
01065         "Client buffer size (ms)");
01066 static const arg_def_t buf_initial_sz     = ARG_DEF(NULL, "buf-initial-sz", 1,
01067         "Client initial buffer size (ms)");
01068 static const arg_def_t buf_optimal_sz     = ARG_DEF(NULL, "buf-optimal-sz", 1,
01069         "Client optimal buffer size (ms)");
01070 static const arg_def_t *rc_args[] =
01071 {
01072     &dropframe_thresh, &resize_allowed, &resize_up_thresh, &resize_down_thresh,
01073     &end_usage, &target_bitrate, &min_quantizer, &max_quantizer,
01074     &undershoot_pct, &overshoot_pct, &buf_sz, &buf_initial_sz, &buf_optimal_sz,
01075     NULL
01076 };
01077 
01078 
01079 static const arg_def_t bias_pct = ARG_DEF(NULL, "bias-pct", 1,
01080                                   "CBR/VBR bias (0=CBR, 100=VBR)");
01081 static const arg_def_t minsection_pct = ARG_DEF(NULL, "minsection-pct", 1,
01082                                         "GOP min bitrate (% of target)");
01083 static const arg_def_t maxsection_pct = ARG_DEF(NULL, "maxsection-pct", 1,
01084                                         "GOP max bitrate (% of target)");
01085 static const arg_def_t *rc_twopass_args[] =
01086 {
01087     &bias_pct, &minsection_pct, &maxsection_pct, NULL
01088 };
01089 
01090 
01091 static const arg_def_t kf_min_dist = ARG_DEF(NULL, "kf-min-dist", 1,
01092                                      "Minimum keyframe interval (frames)");
01093 static const arg_def_t kf_max_dist = ARG_DEF(NULL, "kf-max-dist", 1,
01094                                      "Maximum keyframe interval (frames)");
01095 static const arg_def_t kf_disabled = ARG_DEF(NULL, "disable-kf", 0,
01096                                      "Disable keyframe placement");
01097 static const arg_def_t *kf_args[] =
01098 {
01099     &kf_min_dist, &kf_max_dist, &kf_disabled, NULL
01100 };
01101 
01102 
01103 #if CONFIG_VP8_ENCODER
01104 static const arg_def_t noise_sens = ARG_DEF(NULL, "noise-sensitivity", 1,
01105                                     "Noise sensitivity (frames to blur)");
01106 static const arg_def_t sharpness = ARG_DEF(NULL, "sharpness", 1,
01107                                    "Filter sharpness (0-7)");
01108 static const arg_def_t static_thresh = ARG_DEF(NULL, "static-thresh", 1,
01109                                        "Motion detection threshold");
01110 #endif
01111 
01112 #if CONFIG_VP8_ENCODER
01113 static const arg_def_t cpu_used = ARG_DEF(NULL, "cpu-used", 1,
01114                                   "CPU Used (-16..16)");
01115 #endif
01116 
01117 
01118 #if CONFIG_VP8_ENCODER
01119 static const arg_def_t token_parts = ARG_DEF(NULL, "token-parts", 1,
01120                                      "Number of token partitions to use, log2");
01121 static const arg_def_t auto_altref = ARG_DEF(NULL, "auto-alt-ref", 1,
01122                                      "Enable automatic alt reference frames");
01123 static const arg_def_t arnr_maxframes = ARG_DEF(NULL, "arnr-maxframes", 1,
01124                                         "AltRef Max Frames");
01125 static const arg_def_t arnr_strength = ARG_DEF(NULL, "arnr-strength", 1,
01126                                        "AltRef Strength");
01127 static const arg_def_t arnr_type = ARG_DEF(NULL, "arnr-type", 1,
01128                                    "AltRef Type");
01129 static const struct arg_enum_list tuning_enum[] = {
01130     {"psnr", VP8_TUNE_PSNR},
01131     {"ssim", VP8_TUNE_SSIM},
01132     {NULL, 0}
01133 };
01134 static const arg_def_t tune_ssim = ARG_DEF_ENUM(NULL, "tune", 1,
01135                                    "Material to favor", tuning_enum);
01136 static const arg_def_t cq_level = ARG_DEF(NULL, "cq-level", 1,
01137                                    "Constrained Quality Level");
01138 static const arg_def_t max_intra_rate_pct = ARG_DEF(NULL, "max-intra-rate", 1,
01139         "Max I-frame bitrate (pct)");
01140 
01141 static const arg_def_t *vp8_args[] =
01142 {
01143     &cpu_used, &auto_altref, &noise_sens, &sharpness, &static_thresh,
01144     &token_parts, &arnr_maxframes, &arnr_strength, &arnr_type,
01145     &tune_ssim, &cq_level, &max_intra_rate_pct, NULL
01146 };
01147 static const int vp8_arg_ctrl_map[] =
01148 {
01149     VP8E_SET_CPUUSED, VP8E_SET_ENABLEAUTOALTREF,
01150     VP8E_SET_NOISE_SENSITIVITY, VP8E_SET_SHARPNESS, VP8E_SET_STATIC_THRESHOLD,
01151     VP8E_SET_TOKEN_PARTITIONS,
01152     VP8E_SET_ARNR_MAXFRAMES, VP8E_SET_ARNR_STRENGTH , VP8E_SET_ARNR_TYPE,
01153     VP8E_SET_TUNING, VP8E_SET_CQ_LEVEL, VP8E_SET_MAX_INTRA_BITRATE_PCT, 0
01154 };
01155 #endif
01156 
01157 static const arg_def_t *no_args[] = { NULL };
01158 
01159 static void usage_exit()
01160 {
01161     int i;
01162 
01163     fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
01164             exec_name);
01165 
01166     fprintf(stderr, "\nOptions:\n");
01167     arg_show_usage(stdout, main_args);
01168     fprintf(stderr, "\nEncoder Global Options:\n");
01169     arg_show_usage(stdout, global_args);
01170     fprintf(stderr, "\nRate Control Options:\n");
01171     arg_show_usage(stdout, rc_args);
01172     fprintf(stderr, "\nTwopass Rate Control Options:\n");
01173     arg_show_usage(stdout, rc_twopass_args);
01174     fprintf(stderr, "\nKeyframe Placement Options:\n");
01175     arg_show_usage(stdout, kf_args);
01176 #if CONFIG_VP8_ENCODER
01177     fprintf(stderr, "\nVP8 Specific Options:\n");
01178     arg_show_usage(stdout, vp8_args);
01179 #endif
01180     fprintf(stderr, "\nStream timebase (--timebase):\n"
01181             "  The desired precision of timestamps in the output, expressed\n"
01182             "  in fractional seconds. Default is 1/1000.\n");
01183     fprintf(stderr, "\n"
01184            "Included encoders:\n"
01185            "\n");
01186 
01187     for (i = 0; i < sizeof(codecs) / sizeof(codecs[0]); i++)
01188         fprintf(stderr, "    %-6s - %s\n",
01189                codecs[i].name,
01190                vpx_codec_iface_name(codecs[i].iface));
01191 
01192     exit(EXIT_FAILURE);
01193 }
01194 
01195 
01196 #define HIST_BAR_MAX 40
01197 struct hist_bucket
01198 {
01199     int low, high, count;
01200 };
01201 
01202 
01203 static int merge_hist_buckets(struct hist_bucket *bucket,
01204                               int *buckets_,
01205                               int max_buckets)
01206 {
01207     int small_bucket = 0, merge_bucket = INT_MAX, big_bucket=0;
01208     int buckets = *buckets_;
01209     int i;
01210 
01211     /* Find the extrema for this list of buckets */
01212     big_bucket = small_bucket = 0;
01213     for(i=0; i < buckets; i++)
01214     {
01215         if(bucket[i].count < bucket[small_bucket].count)
01216             small_bucket = i;
01217         if(bucket[i].count > bucket[big_bucket].count)
01218             big_bucket = i;
01219     }
01220 
01221     /* If we have too many buckets, merge the smallest with an adjacent
01222      * bucket.
01223      */
01224     while(buckets > max_buckets)
01225     {
01226         int last_bucket = buckets - 1;
01227 
01228         // merge the small bucket with an adjacent one.
01229         if(small_bucket == 0)
01230             merge_bucket = 1;
01231         else if(small_bucket == last_bucket)
01232             merge_bucket = last_bucket - 1;
01233         else if(bucket[small_bucket - 1].count < bucket[small_bucket + 1].count)
01234             merge_bucket = small_bucket - 1;
01235         else
01236             merge_bucket = small_bucket + 1;
01237 
01238         assert(abs(merge_bucket - small_bucket) <= 1);
01239         assert(small_bucket < buckets);
01240         assert(big_bucket < buckets);
01241         assert(merge_bucket < buckets);
01242 
01243         if(merge_bucket < small_bucket)
01244         {
01245             bucket[merge_bucket].high = bucket[small_bucket].high;
01246             bucket[merge_bucket].count += bucket[small_bucket].count;
01247         }
01248         else
01249         {
01250             bucket[small_bucket].high = bucket[merge_bucket].high;
01251             bucket[small_bucket].count += bucket[merge_bucket].count;
01252             merge_bucket = small_bucket;
01253         }
01254 
01255         assert(bucket[merge_bucket].low != bucket[merge_bucket].high);
01256 
01257         buckets--;
01258 
01259         /* Remove the merge_bucket from the list, and find the new small
01260          * and big buckets while we're at it
01261          */
01262         big_bucket = small_bucket = 0;
01263         for(i=0; i < buckets; i++)
01264         {
01265             if(i > merge_bucket)
01266                 bucket[i] = bucket[i+1];
01267 
01268             if(bucket[i].count < bucket[small_bucket].count)
01269                 small_bucket = i;
01270             if(bucket[i].count > bucket[big_bucket].count)
01271                 big_bucket = i;
01272         }
01273 
01274     }
01275 
01276     *buckets_ = buckets;
01277     return bucket[big_bucket].count;
01278 }
01279 
01280 
01281 static void show_histogram(const struct hist_bucket *bucket,
01282                            int                       buckets,
01283                            int                       total,
01284                            int                       scale)
01285 {
01286     const char *pat1, *pat2;
01287     int i;
01288 
01289     switch((int)(log(bucket[buckets-1].high)/log(10))+1)
01290     {
01291         case 1:
01292         case 2:
01293             pat1 = "%4d %2s: ";
01294             pat2 = "%4d-%2d: ";
01295             break;
01296         case 3:
01297             pat1 = "%5d %3s: ";
01298             pat2 = "%5d-%3d: ";
01299             break;
01300         case 4:
01301             pat1 = "%6d %4s: ";
01302             pat2 = "%6d-%4d: ";
01303             break;
01304         case 5:
01305             pat1 = "%7d %5s: ";
01306             pat2 = "%7d-%5d: ";
01307             break;
01308         case 6:
01309             pat1 = "%8d %6s: ";
01310             pat2 = "%8d-%6d: ";
01311             break;
01312         case 7:
01313             pat1 = "%9d %7s: ";
01314             pat2 = "%9d-%7d: ";
01315             break;
01316         default:
01317             pat1 = "%12d %10s: ";
01318             pat2 = "%12d-%10d: ";
01319             break;
01320     }
01321 
01322     for(i=0; i<buckets; i++)
01323     {
01324         int len;
01325         int j;
01326         float pct;
01327 
01328         pct = 100.0 * (float)bucket[i].count / (float)total;
01329         len = HIST_BAR_MAX * bucket[i].count / scale;
01330         if(len < 1)
01331             len = 1;
01332         assert(len <= HIST_BAR_MAX);
01333 
01334         if(bucket[i].low == bucket[i].high)
01335             fprintf(stderr, pat1, bucket[i].low, "");
01336         else
01337             fprintf(stderr, pat2, bucket[i].low, bucket[i].high);
01338 
01339         for(j=0; j<HIST_BAR_MAX; j++)
01340             fprintf(stderr, j<len?"=":" ");
01341         fprintf(stderr, "\t%5d (%6.2f%%)\n",bucket[i].count,pct);
01342     }
01343 }
01344 
01345 
01346 static void show_q_histogram(const int counts[64], int max_buckets)
01347 {
01348     struct hist_bucket bucket[64];
01349     int buckets = 0;
01350     int total = 0;
01351     int scale;
01352     int i;
01353 
01354 
01355     for(i=0; i<64; i++)
01356     {
01357         if(counts[i])
01358         {
01359             bucket[buckets].low = bucket[buckets].high = i;
01360             bucket[buckets].count = counts[i];
01361             buckets++;
01362             total += counts[i];
01363         }
01364     }
01365 
01366     fprintf(stderr, "\nQuantizer Selection:\n");
01367     scale = merge_hist_buckets(bucket, &buckets, max_buckets);
01368     show_histogram(bucket, buckets, total, scale);
01369 }
01370 
01371 
01372 #define RATE_BINS (100)
01373 struct rate_hist
01374 {
01375     int64_t            *pts;
01376     int                *sz;
01377     int                 samples;
01378     int                 frames;
01379     struct hist_bucket  bucket[RATE_BINS];
01380     int                 total;
01381 };
01382 
01383 
01384 static void init_rate_histogram(struct rate_hist          *hist,
01385                                 const vpx_codec_enc_cfg_t *cfg,
01386                                 const vpx_rational_t      *fps)
01387 {
01388     int i;
01389 
01390     /* Determine the number of samples in the buffer. Use the file's framerate
01391      * to determine the number of frames in rc_buf_sz milliseconds, with an
01392      * adjustment (5/4) to account for alt-refs
01393      */
01394     hist->samples = cfg->rc_buf_sz * 5 / 4 * fps->num / fps->den / 1000;
01395 
01396     // prevent division by zero
01397     if (hist->samples == 0)
01398       hist->samples=1;
01399 
01400     hist->pts = calloc(hist->samples, sizeof(*hist->pts));
01401     hist->sz = calloc(hist->samples, sizeof(*hist->sz));
01402     for(i=0; i<RATE_BINS; i++)
01403     {
01404         hist->bucket[i].low = INT_MAX;
01405         hist->bucket[i].high = 0;
01406         hist->bucket[i].count = 0;
01407     }
01408 }
01409 
01410 
01411 static void destroy_rate_histogram(struct rate_hist *hist)
01412 {
01413     free(hist->pts);
01414     free(hist->sz);
01415 }
01416 
01417 
01418 static void update_rate_histogram(struct rate_hist          *hist,
01419                                   const vpx_codec_enc_cfg_t *cfg,
01420                                   const vpx_codec_cx_pkt_t  *pkt)
01421 {
01422     int i, idx;
01423     int64_t now, then, sum_sz = 0, avg_bitrate;
01424 
01425     now = pkt->data.frame.pts * 1000
01426           * (uint64_t)cfg->g_timebase.num / (uint64_t)cfg->g_timebase.den;
01427 
01428     idx = hist->frames++ % hist->samples;
01429     hist->pts[idx] = now;
01430     hist->sz[idx] = pkt->data.frame.sz;
01431 
01432     if(now < cfg->rc_buf_initial_sz)
01433         return;
01434 
01435     then = now;
01436 
01437     /* Sum the size over the past rc_buf_sz ms */
01438     for(i = hist->frames; i > 0 && hist->frames - i < hist->samples; i--)
01439     {
01440         int i_idx = (i-1) % hist->samples;
01441 
01442         then = hist->pts[i_idx];
01443         if(now - then > cfg->rc_buf_sz)
01444             break;
01445         sum_sz += hist->sz[i_idx];
01446     }
01447 
01448     if (now == then)
01449         return;
01450 
01451     avg_bitrate = sum_sz * 8 * 1000 / (now - then);
01452     idx = avg_bitrate * (RATE_BINS/2) / (cfg->rc_target_bitrate * 1000);
01453     if(idx < 0)
01454         idx = 0;
01455     if(idx > RATE_BINS-1)
01456         idx = RATE_BINS-1;
01457     if(hist->bucket[idx].low > avg_bitrate)
01458         hist->bucket[idx].low = avg_bitrate;
01459     if(hist->bucket[idx].high < avg_bitrate)
01460         hist->bucket[idx].high = avg_bitrate;
01461     hist->bucket[idx].count++;
01462     hist->total++;
01463 }
01464 
01465 
01466 static void show_rate_histogram(struct rate_hist          *hist,
01467                                 const vpx_codec_enc_cfg_t *cfg,
01468                                 int                        max_buckets)
01469 {
01470     int i, scale;
01471     int buckets = 0;
01472 
01473     for(i = 0; i < RATE_BINS; i++)
01474     {
01475         if(hist->bucket[i].low == INT_MAX)
01476             continue;
01477         hist->bucket[buckets++] = hist->bucket[i];
01478     }
01479 
01480     fprintf(stderr, "\nRate (over %dms window):\n", cfg->rc_buf_sz);
01481     scale = merge_hist_buckets(hist->bucket, &buckets, max_buckets);
01482     show_histogram(hist->bucket, buckets, hist->total, scale);
01483 }
01484 
01485 #define NELEMENTS(x) (sizeof(x)/sizeof(x[0]))
01486 #define ARG_CTRL_CNT_MAX NELEMENTS(vp8_arg_ctrl_map)
01487 
01488 
01489 /* Configuration elements common to all streams */
01490 struct global_config
01491 {
01492     const struct codec_item  *codec;
01493     int                       passes;
01494     int                       pass;
01495     int                       usage;
01496     int                       deadline;
01497     int                       use_i420;
01498     int                       verbose;
01499     int                       limit;
01500     int                       show_psnr;
01501     int                       have_framerate;
01502     struct vpx_rational       framerate;
01503     int                       out_part;
01504     int                       debug;
01505     int                       show_q_hist_buckets;
01506     int                       show_rate_hist_buckets;
01507 };
01508 
01509 
01510 /* Per-stream configuration */
01511 struct stream_config
01512 {
01513     struct vpx_codec_enc_cfg  cfg;
01514     const char               *out_fn;
01515     const char               *stats_fn;
01516     stereo_format_t           stereo_fmt;
01517     int                       arg_ctrls[ARG_CTRL_CNT_MAX][2];
01518     int                       arg_ctrl_cnt;
01519     int                       write_webm;
01520     int                       have_kf_max_dist;
01521 };
01522 
01523 
01524 struct stream_state
01525 {
01526     int                       index;
01527     struct stream_state      *next;
01528     struct stream_config      config;
01529     FILE                     *file;
01530     struct rate_hist          rate_hist;
01531     EbmlGlobal                ebml;
01532     uint32_t                  hash;
01533     uint64_t                  psnr_sse_total;
01534     uint64_t                  psnr_samples_total;
01535     double                    psnr_totals[4];
01536     int                       psnr_count;
01537     int                       counts[64];
01538     vpx_codec_ctx_t           encoder;
01539     unsigned int              frames_out;
01540     uint64_t                  cx_time;
01541     size_t                    nbytes;
01542     stats_io_t                stats;
01543 };
01544 
01545 
01546 void validate_positive_rational(const char          *msg,
01547                                 struct vpx_rational *rat)
01548 {
01549     if (rat->den < 0)
01550     {
01551         rat->num *= -1;
01552         rat->den *= -1;
01553     }
01554 
01555     if (rat->num < 0)
01556         die("Error: %s must be positive\n", msg);
01557 
01558     if (!rat->den)
01559         die("Error: %s has zero denominator\n", msg);
01560 }
01561 
01562 
01563 static void parse_global_config(struct global_config *global, char **argv)
01564 {
01565     char       **argi, **argj;
01566     struct arg   arg;
01567 
01568     /* Initialize default parameters */
01569     memset(global, 0, sizeof(*global));
01570     global->codec = codecs;
01571     global->passes = 1;
01572     global->use_i420 = 1;
01573 
01574     for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
01575     {
01576         arg.argv_step = 1;
01577 
01578         if (arg_match(&arg, &codecarg, argi))
01579         {
01580             int j, k = -1;
01581 
01582             for (j = 0; j < sizeof(codecs) / sizeof(codecs[0]); j++)
01583                 if (!strcmp(codecs[j].name, arg.val))
01584                     k = j;
01585 
01586             if (k >= 0)
01587                 global->codec = codecs + k;
01588             else
01589                 die("Error: Unrecognized argument (%s) to --codec\n",
01590                     arg.val);
01591 
01592         }
01593         else if (arg_match(&arg, &passes, argi))
01594         {
01595             global->passes = arg_parse_uint(&arg);
01596 
01597             if (global->passes < 1 || global->passes > 2)
01598                 die("Error: Invalid number of passes (%d)\n", global->passes);
01599         }
01600         else if (arg_match(&arg, &pass_arg, argi))
01601         {
01602             global->pass = arg_parse_uint(&arg);
01603 
01604             if (global->pass < 1 || global->pass > 2)
01605                 die("Error: Invalid pass selected (%d)\n",
01606                     global->pass);
01607         }
01608         else if (arg_match(&arg, &usage, argi))
01609             global->usage = arg_parse_uint(&arg);
01610         else if (arg_match(&arg, &deadline, argi))
01611             global->deadline = arg_parse_uint(&arg);
01612         else if (arg_match(&arg, &best_dl, argi))
01613             global->deadline = VPX_DL_BEST_QUALITY;
01614         else if (arg_match(&arg, &good_dl, argi))
01615             global->deadline = VPX_DL_GOOD_QUALITY;
01616         else if (arg_match(&arg, &rt_dl, argi))
01617             global->deadline = VPX_DL_REALTIME;
01618         else if (arg_match(&arg, &use_yv12, argi))
01619             global->use_i420 = 0;
01620         else if (arg_match(&arg, &use_i420, argi))
01621             global->use_i420 = 1;
01622         else if (arg_match(&arg, &verbosearg, argi))
01623             global->verbose = 1;
01624         else if (arg_match(&arg, &limit, argi))
01625             global->limit = arg_parse_uint(&arg);
01626         else if (arg_match(&arg, &psnrarg, argi))
01627             global->show_psnr = 1;
01628         else if (arg_match(&arg, &framerate, argi))
01629         {
01630             global->framerate = arg_parse_rational(&arg);
01631             validate_positive_rational(arg.name, &global->framerate);
01632             global->have_framerate = 1;
01633         }
01634         else if (arg_match(&arg,&out_part, argi))
01635             global->out_part = 1;
01636         else if (arg_match(&arg, &debugmode, argi))
01637             global->debug = 1;
01638         else if (arg_match(&arg, &q_hist_n, argi))
01639             global->show_q_hist_buckets = arg_parse_uint(&arg);
01640         else if (arg_match(&arg, &rate_hist_n, argi))
01641             global->show_rate_hist_buckets = arg_parse_uint(&arg);
01642         else
01643             argj++;
01644     }
01645 
01646     /* Validate global config */
01647 
01648     if (global->pass)
01649     {
01650         /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
01651         if (global->pass > global->passes)
01652         {
01653             warn("Assuming --pass=%d implies --passes=%d\n",
01654                  global->pass, global->pass);
01655             global->passes = global->pass;
01656         }
01657     }
01658 }
01659 
01660 
01661 void open_input_file(struct input_state *input)
01662 {
01663     unsigned int fourcc;
01664 
01665     /* Parse certain options from the input file, if possible */
01666     input->file = strcmp(input->fn, "-") ? fopen(input->fn, "rb")
01667                                          : set_binary_mode(stdin);
01668 
01669     if (!input->file)
01670         fatal("Failed to open input file");
01671 
01672     /* For RAW input sources, these bytes will applied on the first frame
01673      *  in read_frame().
01674      */
01675     input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
01676     input->detect.position = 0;
01677 
01678     if (input->detect.buf_read == 4
01679         && file_is_y4m(input->file, &input->y4m, input->detect.buf))
01680     {
01681         if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4) >= 0)
01682         {
01683             input->file_type = FILE_TYPE_Y4M;
01684             input->w = input->y4m.pic_w;
01685             input->h = input->y4m.pic_h;
01686             input->framerate.num = input->y4m.fps_n;
01687             input->framerate.den = input->y4m.fps_d;
01688             input->use_i420 = 0;
01689         }
01690         else
01691             fatal("Unsupported Y4M stream.");
01692     }
01693     else if (input->detect.buf_read == 4 && file_is_ivf(input, &fourcc))
01694     {
01695         input->file_type = FILE_TYPE_IVF;
01696         switch (fourcc)
01697         {
01698         case 0x32315659:
01699             input->use_i420 = 0;
01700             break;
01701         case 0x30323449:
01702             input->use_i420 = 1;
01703             break;
01704         default:
01705             fatal("Unsupported fourcc (%08x) in IVF", fourcc);
01706         }
01707     }
01708     else
01709     {
01710         input->file_type = FILE_TYPE_RAW;
01711     }
01712 }
01713 
01714 
01715 static void close_input_file(struct input_state *input)
01716 {
01717     fclose(input->file);
01718     if (input->file_type == FILE_TYPE_Y4M)
01719         y4m_input_close(&input->y4m);
01720 }
01721 
01722 static struct stream_state *new_stream(struct global_config *global,
01723                                        struct stream_state  *prev)
01724 {
01725     struct stream_state *stream;
01726 
01727     stream = calloc(1, sizeof(*stream));
01728     if(!stream)
01729         fatal("Failed to allocate new stream.");
01730     if(prev)
01731     {
01732         memcpy(stream, prev, sizeof(*stream));
01733         stream->index++;
01734         prev->next = stream;
01735     }
01736     else
01737     {
01738         vpx_codec_err_t  res;
01739 
01740         /* Populate encoder configuration */
01741         res = vpx_codec_enc_config_default(global->codec->iface,
01742                                            &stream->config.cfg,
01743                                            global->usage);
01744         if (res)
01745             fatal("Failed to get config: %s\n", vpx_codec_err_to_string(res));
01746 
01747         /* Change the default timebase to a high enough value so that the
01748          * encoder will always create strictly increasing timestamps.
01749          */
01750         stream->config.cfg.g_timebase.den = 1000;
01751 
01752         /* Never use the library's default resolution, require it be parsed
01753          * from the file or set on the command line.
01754          */
01755         stream->config.cfg.g_w = 0;
01756         stream->config.cfg.g_h = 0;
01757 
01758         /* Initialize remaining stream parameters */
01759         stream->config.stereo_fmt = STEREO_FORMAT_MONO;
01760         stream->config.write_webm = 1;
01761         stream->ebml.last_pts_ms = -1;
01762 
01763         /* Allows removal of the application version from the EBML tags */
01764         stream->ebml.debug = global->debug;
01765     }
01766 
01767     /* Output files must be specified for each stream */
01768     stream->config.out_fn = NULL;
01769 
01770     stream->next = NULL;
01771     return stream;
01772 }
01773 
01774 
01775 static int parse_stream_params(struct global_config *global,
01776                                struct stream_state  *stream,
01777                                char **argv)
01778 {
01779     char                   **argi, **argj;
01780     struct arg               arg;
01781     static const arg_def_t **ctrl_args = no_args;
01782     static const int        *ctrl_args_map = NULL;
01783     struct stream_config    *config = &stream->config;
01784     int                      eos_mark_found = 0;
01785 
01786     /* Handle codec specific options */
01787     if (global->codec->iface == &vpx_codec_vp8_cx_algo)
01788     {
01789         ctrl_args = vp8_args;
01790         ctrl_args_map = vp8_arg_ctrl_map;
01791     }
01792 
01793     for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
01794     {
01795         arg.argv_step = 1;
01796 
01797         /* Once we've found an end-of-stream marker (--) we want to continue
01798          * shifting arguments but not consuming them.
01799          */
01800         if (eos_mark_found)
01801         {
01802             argj++;
01803             continue;
01804         }
01805         else if (!strcmp(*argj, "--"))
01806         {
01807             eos_mark_found = 1;
01808             continue;
01809         }
01810 
01811         if (0);
01812         else if (arg_match(&arg, &outputfile, argi))
01813             config->out_fn = arg.val;
01814         else if (arg_match(&arg, &fpf_name, argi))
01815             config->stats_fn = arg.val;
01816         else if (arg_match(&arg, &use_ivf, argi))
01817             config->write_webm = 0;
01818         else if (arg_match(&arg, &threads, argi))
01819             config->cfg.g_threads = arg_parse_uint(&arg);
01820         else if (arg_match(&arg, &profile, argi))
01821             config->cfg.g_profile = arg_parse_uint(&arg);
01822         else if (arg_match(&arg, &width, argi))
01823             config->cfg.g_w = arg_parse_uint(&arg);
01824         else if (arg_match(&arg, &height, argi))
01825             config->cfg.g_h = arg_parse_uint(&arg);
01826         else if (arg_match(&arg, &stereo_mode, argi))
01827             config->stereo_fmt = arg_parse_enum_or_int(&arg);
01828         else if (arg_match(&arg, &timebase, argi))
01829         {
01830             config->cfg.g_timebase = arg_parse_rational(&arg);
01831             validate_positive_rational(arg.name, &config->cfg.g_timebase);
01832         }
01833         else if (arg_match(&arg, &error_resilient, argi))
01834             config->cfg.g_error_resilient = arg_parse_uint(&arg);
01835         else if (arg_match(&arg, &lag_in_frames, argi))
01836             config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
01837         else if (arg_match(&arg, &dropframe_thresh, argi))
01838             config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
01839         else if (arg_match(&arg, &resize_allowed, argi))
01840             config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
01841         else if (arg_match(&arg, &resize_up_thresh, argi))
01842             config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
01843         else if (arg_match(&arg, &resize_down_thresh, argi))
01844             config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
01845         else if (arg_match(&arg, &end_usage, argi))
01846             config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
01847         else if (arg_match(&arg, &target_bitrate, argi))
01848             config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
01849         else if (arg_match(&arg, &min_quantizer, argi))
01850             config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
01851         else if (arg_match(&arg, &max_quantizer, argi))
01852             config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
01853         else if (arg_match(&arg, &undershoot_pct, argi))
01854             config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
01855         else if (arg_match(&arg, &overshoot_pct, argi))
01856             config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
01857         else if (arg_match(&arg, &buf_sz, argi))
01858             config->cfg.rc_buf_sz = arg_parse_uint(&arg);
01859         else if (arg_match(&arg, &buf_initial_sz, argi))
01860             config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
01861         else if (arg_match(&arg, &buf_optimal_sz, argi))
01862             config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
01863         else if (arg_match(&arg, &bias_pct, argi))
01864         {
01865             config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
01866 
01867             if (global->passes < 2)
01868                 warn("option %s ignored in one-pass mode.\n", arg.name);
01869         }
01870         else if (arg_match(&arg, &minsection_pct, argi))
01871         {
01872             config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
01873 
01874             if (global->passes < 2)
01875                 warn("option %s ignored in one-pass mode.\n", arg.name);
01876         }
01877         else if (arg_match(&arg, &maxsection_pct, argi))
01878         {
01879             config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
01880 
01881             if (global->passes < 2)
01882                 warn("option %s ignored in one-pass mode.\n", arg.name);
01883         }
01884         else if (arg_match(&arg, &kf_min_dist, argi))
01885             config->cfg.kf_min_dist = arg_parse_uint(&arg);
01886         else if (arg_match(&arg, &kf_max_dist, argi))
01887         {
01888             config->cfg.kf_max_dist = arg_parse_uint(&arg);
01889             config->have_kf_max_dist = 1;
01890         }
01891         else if (arg_match(&arg, &kf_disabled, argi))
01892             config->cfg.kf_mode = VPX_KF_DISABLED;
01893         else
01894         {
01895             int i, match = 0;
01896 
01897             for (i = 0; ctrl_args[i]; i++)
01898             {
01899                 if (arg_match(&arg, ctrl_args[i], argi))
01900                 {
01901                     int j;
01902                     match = 1;
01903 
01904                     /* Point either to the next free element or the first
01905                     * instance of this control.
01906                     */
01907                     for(j=0; j<config->arg_ctrl_cnt; j++)
01908                         if(config->arg_ctrls[j][0] == ctrl_args_map[i])
01909                             break;
01910 
01911                     /* Update/insert */
01912                     assert(j < ARG_CTRL_CNT_MAX);
01913                     if (j < ARG_CTRL_CNT_MAX)
01914                     {
01915                         config->arg_ctrls[j][0] = ctrl_args_map[i];
01916                         config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
01917                         if(j == config->arg_ctrl_cnt)
01918                             config->arg_ctrl_cnt++;
01919                     }
01920 
01921                 }
01922             }
01923 
01924             if (!match)
01925                 argj++;
01926         }
01927     }
01928 
01929     return eos_mark_found;
01930 }
01931 
01932 
01933 #define FOREACH_STREAM(func)\
01934 do\
01935 {\
01936     struct stream_state  *stream;\
01937 \
01938     for(stream = streams; stream; stream = stream->next)\
01939         func;\
01940 }while(0)
01941 
01942 
01943 static void validate_stream_config(struct stream_state *stream)
01944 {
01945     struct stream_state *streami;
01946 
01947     if(!stream->config.cfg.g_w || !stream->config.cfg.g_h)
01948         fatal("Stream %d: Specify stream dimensions with --width (-w) "
01949               " and --height (-h)", stream->index);
01950 
01951     for(streami = stream; streami; streami = streami->next)
01952     {
01953         /* All streams require output files */
01954         if(!streami->config.out_fn)
01955             fatal("Stream %d: Output file is required (specify with -o)",
01956                   streami->index);
01957 
01958         /* Check for two streams outputting to the same file */
01959         if(streami != stream)
01960         {
01961             const char *a = stream->config.out_fn;
01962             const char *b = streami->config.out_fn;
01963             if(!strcmp(a,b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
01964                 fatal("Stream %d: duplicate output file (from stream %d)",
01965                       streami->index, stream->index);
01966         }
01967 
01968         /* Check for two streams sharing a stats file. */
01969         if(streami != stream)
01970         {
01971             const char *a = stream->config.stats_fn;
01972             const char *b = streami->config.stats_fn;
01973             if(a && b && !strcmp(a,b))
01974                 fatal("Stream %d: duplicate stats file (from stream %d)",
01975                       streami->index, stream->index);
01976         }
01977     }
01978 }
01979 
01980 
01981 static void set_stream_dimensions(struct stream_state *stream,
01982                                   unsigned int w,
01983                                   unsigned int h)
01984 {
01985     if ((stream->config.cfg.g_w && stream->config.cfg.g_w != w)
01986         ||(stream->config.cfg.g_h && stream->config.cfg.g_h != h))
01987         fatal("Stream %d: Resizing not yet supported", stream->index);
01988     stream->config.cfg.g_w = w;
01989     stream->config.cfg.g_h = h;
01990 }
01991 
01992 
01993 static void set_default_kf_interval(struct stream_state  *stream,
01994                                     struct global_config *global)
01995 {
01996     /* Use a max keyframe interval of 5 seconds, if none was
01997      * specified on the command line.
01998      */
01999     if (!stream->config.have_kf_max_dist)
02000     {
02001         double framerate = (double)global->framerate.num/global->framerate.den;
02002         if (framerate > 0.0)
02003             stream->config.cfg.kf_max_dist = 5.0*framerate;
02004     }
02005 }
02006 
02007 
02008 static void show_stream_config(struct stream_state  *stream,
02009                                struct global_config *global,
02010                                struct input_state   *input)
02011 {
02012 
02013 #define SHOW(field) \
02014     fprintf(stderr, "    %-28s = %d\n", #field, stream->config.cfg.field)
02015 
02016     if(stream->index == 0)
02017     {
02018         fprintf(stderr, "Codec: %s\n",
02019                 vpx_codec_iface_name(global->codec->iface));
02020         fprintf(stderr, "Source file: %s Format: %s\n", input->fn,
02021                 input->use_i420 ? "I420" : "YV12");
02022     }
02023     if(stream->next || stream->index)
02024         fprintf(stderr, "\nStream Index: %d\n", stream->index);
02025     fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
02026     fprintf(stderr, "Encoder parameters:\n");
02027 
02028     SHOW(g_usage);
02029     SHOW(g_threads);
02030     SHOW(g_profile);
02031     SHOW(g_w);
02032     SHOW(g_h);
02033     SHOW(g_timebase.num);
02034     SHOW(g_timebase.den);
02035     SHOW(g_error_resilient);
02036     SHOW(g_pass);
02037     SHOW(g_lag_in_frames);
02038     SHOW(rc_dropframe_thresh);
02039     SHOW(rc_resize_allowed);
02040     SHOW(rc_resize_up_thresh);
02041     SHOW(rc_resize_down_thresh);
02042     SHOW(rc_end_usage);
02043     SHOW(rc_target_bitrate);
02044     SHOW(rc_min_quantizer);
02045     SHOW(rc_max_quantizer);
02046     SHOW(rc_undershoot_pct);
02047     SHOW(rc_overshoot_pct);
02048     SHOW(rc_buf_sz);
02049     SHOW(rc_buf_initial_sz);
02050     SHOW(rc_buf_optimal_sz);
02051     SHOW(rc_2pass_vbr_bias_pct);
02052     SHOW(rc_2pass_vbr_minsection_pct);
02053     SHOW(rc_2pass_vbr_maxsection_pct);
02054     SHOW(kf_mode);
02055     SHOW(kf_min_dist);
02056     SHOW(kf_max_dist);
02057 }
02058 
02059 
02060 static void open_output_file(struct stream_state *stream,
02061                              struct global_config *global)
02062 {
02063     const char *fn = stream->config.out_fn;
02064 
02065     stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
02066 
02067     if (!stream->file)
02068         fatal("Failed to open output file");
02069 
02070     if(stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
02071         fatal("WebM output to pipes not supported.");
02072 
02073     if(stream->config.write_webm)
02074     {
02075         stream->ebml.stream = stream->file;
02076         write_webm_file_header(&stream->ebml, &stream->config.cfg,
02077                                &global->framerate,
02078                                stream->config.stereo_fmt);
02079     }
02080     else
02081         write_ivf_file_header(stream->file, &stream->config.cfg,
02082                               global->codec->fourcc, 0);
02083 }
02084 
02085 
02086 static void close_output_file(struct stream_state *stream,
02087                               unsigned int         fourcc)
02088 {
02089     if(stream->config.write_webm)
02090     {
02091         write_webm_file_footer(&stream->ebml, stream->hash);
02092         free(stream->ebml.cue_list);
02093         stream->ebml.cue_list = NULL;
02094     }
02095     else
02096     {
02097         if (!fseek(stream->file, 0, SEEK_SET))
02098             write_ivf_file_header(stream->file, &stream->config.cfg,
02099                                   fourcc,
02100                                   stream->frames_out);
02101     }
02102 
02103     fclose(stream->file);
02104 }
02105 
02106 
02107 static void setup_pass(struct stream_state  *stream,
02108                        struct global_config *global,
02109                        int                   pass)
02110 {
02111     if (stream->config.stats_fn)
02112     {
02113         if (!stats_open_file(&stream->stats, stream->config.stats_fn,
02114                              pass))
02115             fatal("Failed to open statistics store");
02116     }
02117     else
02118     {
02119         if (!stats_open_mem(&stream->stats, pass))
02120             fatal("Failed to open statistics store");
02121     }
02122 
02123     stream->config.cfg.g_pass = global->passes == 2
02124         ? pass ? VPX_RC_LAST_PASS : VPX_RC_FIRST_PASS
02125         : VPX_RC_ONE_PASS;
02126     if (pass)
02127         stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
02128 
02129     stream->cx_time = 0;
02130     stream->nbytes = 0;
02131     stream->frames_out = 0;
02132 }
02133 
02134 
02135 static void initialize_encoder(struct stream_state  *stream,
02136                                struct global_config *global)
02137 {
02138     int i;
02139     int flags = 0;
02140 
02141     flags |= global->show_psnr ? VPX_CODEC_USE_PSNR : 0;
02142     flags |= global->out_part ? VPX_CODEC_USE_OUTPUT_PARTITION : 0;
02143 
02144     /* Construct Encoder Context */
02145     vpx_codec_enc_init(&stream->encoder, global->codec->iface,
02146                         &stream->config.cfg, flags);
02147     ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
02148 
02149     /* Note that we bypass the vpx_codec_control wrapper macro because
02150      * we're being clever to store the control IDs in an array. Real
02151      * applications will want to make use of the enumerations directly
02152      */
02153     for (i = 0; i < stream->config.arg_ctrl_cnt; i++)
02154     {
02155         int ctrl = stream->config.arg_ctrls[i][0];
02156         int value = stream->config.arg_ctrls[i][1];
02157         if (vpx_codec_control_(&stream->encoder, ctrl, value))
02158             fprintf(stderr, "Error: Tried to set control %d = %d\n",
02159                     ctrl, value);
02160 
02161         ctx_exit_on_error(&stream->encoder, "Failed to control codec");
02162     }
02163 }
02164 
02165 
02166 static void encode_frame(struct stream_state  *stream,
02167                          struct global_config *global,
02168                          struct vpx_image     *img,
02169                          unsigned int          frames_in)
02170 {
02171     vpx_codec_pts_t frame_start, next_frame_start;
02172     struct vpx_codec_enc_cfg *cfg = &stream->config.cfg;
02173     struct vpx_usec_timer timer;
02174 
02175     frame_start = (cfg->g_timebase.den * (int64_t)(frames_in - 1)
02176                   * global->framerate.den)
02177                   / cfg->g_timebase.num / global->framerate.num;
02178     next_frame_start = (cfg->g_timebase.den * (int64_t)(frames_in)
02179                         * global->framerate.den)
02180                         / cfg->g_timebase.num / global->framerate.num;
02181     vpx_usec_timer_start(&timer);
02182     vpx_codec_encode(&stream->encoder, img, frame_start,
02183                      next_frame_start - frame_start,
02184                      0, global->deadline);
02185     vpx_usec_timer_mark(&timer);
02186     stream->cx_time += vpx_usec_timer_elapsed(&timer);
02187     ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
02188                       stream->index);
02189 }
02190 
02191 
02192 static void update_quantizer_histogram(struct stream_state *stream)
02193 {
02194     if(stream->config.cfg.g_pass != VPX_RC_FIRST_PASS)
02195     {
02196         int q;
02197 
02198         vpx_codec_control(&stream->encoder, VP8E_GET_LAST_QUANTIZER_64, &q);
02199         ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
02200         stream->counts[q]++;
02201     }
02202 }
02203 
02204 
02205 static void get_cx_data(struct stream_state  *stream,
02206                         struct global_config *global,
02207                         int                  *got_data)
02208 {
02209     const vpx_codec_cx_pkt_t *pkt;
02210     const struct vpx_codec_enc_cfg *cfg = &stream->config.cfg;
02211     vpx_codec_iter_t iter = NULL;
02212 
02213     while ((pkt = vpx_codec_get_cx_data(&stream->encoder, &iter)))
02214     {
02215         static size_t fsize = 0;
02216         static off_t ivf_header_pos = 0;
02217 
02218         *got_data = 1;
02219 
02220         switch (pkt->kind)
02221         {
02222         case VPX_CODEC_CX_FRAME_PKT:
02223             if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT))
02224             {
02225                 stream->frames_out++;
02226             }
02227             fprintf(stderr, " %6luF",
02228                     (unsigned long)pkt->data.frame.sz);
02229 
02230             update_rate_histogram(&stream->rate_hist, cfg, pkt);
02231             if(stream->config.write_webm)
02232             {
02233                 /* Update the hash */
02234                 if(!stream->ebml.debug)
02235                     stream->hash = murmur(pkt->data.frame.buf,
02236                                           pkt->data.frame.sz, stream->hash);
02237 
02238                 write_webm_block(&stream->ebml, cfg, pkt);
02239             }
02240             else
02241             {
02242                 if (pkt->data.frame.partition_id <= 0)
02243                 {
02244                     ivf_header_pos = ftello(stream->file);
02245                     fsize = pkt->data.frame.sz;
02246 
02247                     write_ivf_frame_header(stream->file, pkt);
02248                 }
02249                 else
02250                 {
02251                     fsize += pkt->data.frame.sz;
02252 
02253                     if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT))
02254                     {
02255                         off_t currpos = ftello(stream->file);
02256                         fseeko(stream->file, ivf_header_pos, SEEK_SET);
02257                         write_ivf_frame_size(stream->file, fsize);
02258                         fseeko(stream->file, currpos, SEEK_SET);
02259                     }
02260                 }
02261 
02262                 fwrite(pkt->data.frame.buf, 1,
02263                        pkt->data.frame.sz, stream->file);
02264             }
02265             stream->nbytes += pkt->data.raw.sz;
02266             break;
02267         case VPX_CODEC_STATS_PKT:
02268             stream->frames_out++;
02269             fprintf(stderr, " %6luS",
02270                    (unsigned long)pkt->data.twopass_stats.sz);
02271             stats_write(&stream->stats,
02272                         pkt->data.twopass_stats.buf,
02273                         pkt->data.twopass_stats.sz);
02274             stream->nbytes += pkt->data.raw.sz;
02275             break;
02276         case VPX_CODEC_PSNR_PKT:
02277 
02278             if (global->show_psnr)
02279             {
02280                 int i;
02281 
02282                 stream->psnr_sse_total += pkt->data.psnr.sse[0];
02283                 stream->psnr_samples_total += pkt->data.psnr.samples[0];
02284                 for (i = 0; i < 4; i++)
02285                 {
02286                     fprintf(stderr, "%.3lf ", pkt->data.psnr.psnr[i]);
02287                     stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
02288                 }
02289                 stream->psnr_count++;
02290             }
02291 
02292             break;
02293         default:
02294             break;
02295         }
02296     }
02297 }
02298 
02299 
02300 static void show_psnr(struct stream_state  *stream)
02301 {
02302     int i;
02303     double ovpsnr;
02304 
02305     if (!stream->psnr_count)
02306         return;
02307 
02308     fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
02309     ovpsnr = vp8_mse2psnr(stream->psnr_samples_total, 255.0,
02310                           stream->psnr_sse_total);
02311     fprintf(stderr, " %.3lf", ovpsnr);
02312 
02313     for (i = 0; i < 4; i++)
02314     {
02315         fprintf(stderr, " %.3lf", stream->psnr_totals[i]/stream->psnr_count);
02316     }
02317     fprintf(stderr, "\n");
02318 }
02319 
02320 
02321 float usec_to_fps(uint64_t usec, unsigned int frames)
02322 {
02323     return usec > 0 ? (float)frames * 1000000.0 / (float)usec : 0;
02324 }
02325 
02326 
02327 int main(int argc, const char **argv_)
02328 {
02329     int                    pass;
02330     vpx_image_t            raw;
02331     int                    frame_avail, got_data;
02332 
02333     struct input_state       input = {0};
02334     struct global_config     global;
02335     struct stream_state     *streams = NULL;
02336     char                   **argv, **argi;
02337     unsigned long            cx_time = 0;
02338     int                      stream_cnt = 0;
02339 
02340     exec_name = argv_[0];
02341 
02342     if (argc < 3)
02343         usage_exit();
02344 
02345     /* Setup default input stream settings */
02346     input.framerate.num = 30;
02347     input.framerate.den = 1;
02348     input.use_i420 = 1;
02349 
02350     /* First parse the global configuration values, because we want to apply
02351      * other parameters on top of the default configuration provided by the
02352      * codec.
02353      */
02354     argv = argv_dup(argc - 1, argv_ + 1);
02355     parse_global_config(&global, argv);
02356 
02357     {
02358         /* Now parse each stream's parameters. Using a local scope here
02359          * due to the use of 'stream' as loop variable in FOREACH_STREAM
02360          * loops
02361          */
02362         struct stream_state *stream = NULL;
02363 
02364         do
02365         {
02366             stream = new_stream(&global, stream);
02367             stream_cnt++;
02368             if(!streams)
02369                 streams = stream;
02370         } while(parse_stream_params(&global, stream, argv));
02371     }
02372 
02373     /* Check for unrecognized options */
02374     for (argi = argv; *argi; argi++)
02375         if (argi[0][0] == '-' && argi[0][1])
02376             die("Error: Unrecognized option %s\n", *argi);
02377 
02378     /* Handle non-option arguments */
02379     input.fn = argv[0];
02380 
02381     if (!input.fn)
02382         usage_exit();
02383 
02384     for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++)
02385     {
02386         int frames_in = 0;
02387 
02388         open_input_file(&input);
02389 
02390         /* If the input file doesn't specify its w/h (raw files), try to get
02391          * the data from the first stream's configuration.
02392          */
02393         if(!input.w || !input.h)
02394             FOREACH_STREAM({
02395                 if(stream->config.cfg.g_w && stream->config.cfg.g_h)
02396                 {
02397                     input.w = stream->config.cfg.g_w;
02398                     input.h = stream->config.cfg.g_h;
02399                     break;
02400                 }
02401             });
02402 
02403         /* Update stream configurations from the input file's parameters */
02404         FOREACH_STREAM(set_stream_dimensions(stream, input.w, input.h));
02405         FOREACH_STREAM(validate_stream_config(stream));
02406 
02407         /* Ensure that --passes and --pass are consistent. If --pass is set and
02408          * --passes=2, ensure --fpf was set.
02409          */
02410         if (global.pass && global.passes == 2)
02411             FOREACH_STREAM({
02412                 if(!stream->config.stats_fn)
02413                     die("Stream %d: Must specify --fpf when --pass=%d"
02414                         " and --passes=2\n", stream->index, global.pass);
02415             });
02416 
02417 
02418         /* Use the frame rate from the file only if none was specified
02419          * on the command-line.
02420          */
02421         if (!global.have_framerate)
02422             global.framerate = input.framerate;
02423 
02424         FOREACH_STREAM(set_default_kf_interval(stream, &global));
02425 
02426         /* Show configuration */
02427         if (global.verbose && pass == 0)
02428             FOREACH_STREAM(show_stream_config(stream, &global, &input));
02429 
02430         if(pass == (global.pass ? global.pass - 1 : 0)) {
02431             if (input.file_type == FILE_TYPE_Y4M)
02432                 /*The Y4M reader does its own allocation.
02433                   Just initialize this here to avoid problems if we never read any
02434                    frames.*/
02435                 memset(&raw, 0, sizeof(raw));
02436             else
02437                 vpx_img_alloc(&raw,
02438                               input.use_i420 ? VPX_IMG_FMT_I420
02439                                              : VPX_IMG_FMT_YV12,
02440                               input.w, input.h, 1);
02441 
02442             FOREACH_STREAM(init_rate_histogram(&stream->rate_hist,
02443                                                &stream->config.cfg,
02444                                                &global.framerate));
02445         }
02446 
02447         FOREACH_STREAM(open_output_file(stream, &global));
02448         FOREACH_STREAM(setup_pass(stream, &global, pass));
02449         FOREACH_STREAM(initialize_encoder(stream, &global));
02450 
02451         frame_avail = 1;
02452         got_data = 0;
02453 
02454         while (frame_avail || got_data)
02455         {
02456             struct vpx_usec_timer timer;
02457 
02458             if (!global.limit || frames_in < global.limit)
02459             {
02460                 frame_avail = read_frame(&input, &raw);
02461 
02462                 if (frame_avail)
02463                     frames_in++;
02464 
02465                 if(stream_cnt == 1)
02466                     fprintf(stderr,
02467                             "\rPass %d/%d frame %4d/%-4d %7"PRId64"B \033[K",
02468                             pass + 1, global.passes, frames_in,
02469                             streams->frames_out, (int64_t)streams->nbytes);
02470                 else
02471                     fprintf(stderr,
02472                             "\rPass %d/%d frame %4d %7lu %s (%.2f fps)\033[K",
02473                             pass + 1, global.passes, frames_in,
02474                             cx_time > 9999999 ? cx_time / 1000 : cx_time,
02475                             cx_time > 9999999 ? "ms" : "us",
02476                             usec_to_fps(cx_time, frames_in));
02477 
02478             }
02479             else
02480                 frame_avail = 0;
02481 
02482             vpx_usec_timer_start(&timer);
02483             FOREACH_STREAM(encode_frame(stream, &global,
02484                                         frame_avail ? &raw : NULL,
02485                                         frames_in));
02486             vpx_usec_timer_mark(&timer);
02487             cx_time += vpx_usec_timer_elapsed(&timer);
02488 
02489             FOREACH_STREAM(update_quantizer_histogram(stream));
02490 
02491             got_data = 0;
02492             FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
02493 
02494             fflush(stdout);
02495         }
02496 
02497         if(stream_cnt > 1)
02498             fprintf(stderr, "\n");
02499 
02500         FOREACH_STREAM(fprintf(
02501             stderr,
02502             "\rPass %d/%d frame %4d/%-4d %7"PRId64"B %7lub/f %7"PRId64"b/s"
02503             " %7"PRId64" %s (%.2f fps)\033[K\n", pass + 1,
02504             global.passes, frames_in, stream->frames_out, (int64_t)stream->nbytes,
02505             frames_in ? (unsigned long)(stream->nbytes * 8 / frames_in) : 0,
02506             frames_in ? (int64_t)stream->nbytes * 8
02507                         * (int64_t)global.framerate.num / global.framerate.den
02508                         / frames_in
02509                       : 0,
02510             stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
02511             stream->cx_time > 9999999 ? "ms" : "us",
02512             usec_to_fps(stream->cx_time, frames_in));
02513         );
02514 
02515         if (global.show_psnr)
02516             FOREACH_STREAM(show_psnr(stream));
02517 
02518         FOREACH_STREAM(vpx_codec_destroy(&stream->encoder));
02519 
02520         close_input_file(&input);
02521 
02522         FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
02523 
02524         FOREACH_STREAM(stats_close(&stream->stats, global.passes-1));
02525 
02526         if (global.pass)
02527             break;
02528     }
02529 
02530     if (global.show_q_hist_buckets)
02531         FOREACH_STREAM(show_q_histogram(stream->counts,
02532                                         global.show_q_hist_buckets));
02533 
02534     if (global.show_rate_hist_buckets)
02535         FOREACH_STREAM(show_rate_histogram(&stream->rate_hist,
02536                                            &stream->config.cfg,
02537                                            global.show_rate_hist_buckets));
02538     FOREACH_STREAM(destroy_rate_histogram(&stream->rate_hist));
02539 
02540     vpx_img_free(&raw);
02541     free(argv);
02542     free(streams);
02543     return EXIT_SUCCESS;
02544 }