00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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 "vpx/vpx_encoder.h"
00027 #if USE_POSIX_MMAP
00028 #include <sys/types.h>
00029 #include <sys/stat.h>
00030 #include <sys/mman.h>
00031 #include <fcntl.h>
00032 #include <unistd.h>
00033 #endif
00034 #include "vpx_version.h"
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
00044 #if defined(_MSC_VER)
00045
00046 typedef __int64 off_t;
00047 #define fseeko _fseeki64
00048 #define ftello _ftelli64
00049 #elif defined(_WIN32)
00050
00051 #define fseeko fseeko64
00052 #define ftello ftello64
00053 #endif
00054
00055 #if defined(_MSC_VER)
00056 #define LITERALU64(n) n
00057 #else
00058 #define LITERALU64(n) n##LLU
00059 #endif
00060
00061
00062
00063 #if !CONFIG_OS_SUPPORT
00064 typedef long off_t;
00065 #define fseeko fseek
00066 #define ftello ftell
00067 #endif
00068
00069 static const char *exec_name;
00070
00071 static const struct codec_item
00072 {
00073 char const *name;
00074 const vpx_codec_iface_t *iface;
00075 unsigned int fourcc;
00076 } codecs[] =
00077 {
00078 #if CONFIG_VP8_ENCODER
00079 {"vp8", &vpx_codec_vp8_cx_algo, 0x30385056},
00080 #endif
00081 };
00082
00083 static void usage_exit();
00084
00085 void die(const char *fmt, ...)
00086 {
00087 va_list ap;
00088 va_start(ap, fmt);
00089 vfprintf(stderr, fmt, ap);
00090 fprintf(stderr, "\n");
00091 usage_exit();
00092 }
00093
00094 static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s)
00095 {
00096 if (ctx->err)
00097 {
00098 const char *detail = vpx_codec_error_detail(ctx);
00099
00100 fprintf(stderr, "%s: %s\n", s, vpx_codec_error(ctx));
00101
00102 if (detail)
00103 fprintf(stderr, " %s\n", detail);
00104
00105 exit(EXIT_FAILURE);
00106 }
00107 }
00108
00109
00110
00111
00112 typedef struct
00113 {
00114 vpx_fixed_buf_t buf;
00115 int pass;
00116 FILE *file;
00117 char *buf_ptr;
00118 size_t buf_alloc_sz;
00119 } stats_io_t;
00120
00121 int stats_open_file(stats_io_t *stats, const char *fpf, int pass)
00122 {
00123 int res;
00124
00125 stats->pass = pass;
00126
00127 if (pass == 0)
00128 {
00129 stats->file = fopen(fpf, "wb");
00130 stats->buf.sz = 0;
00131 stats->buf.buf = NULL,
00132 res = (stats->file != NULL);
00133 }
00134 else
00135 {
00136 #if 0
00137 #elif USE_POSIX_MMAP
00138 struct stat stat_buf;
00139 int fd;
00140
00141 fd = open(fpf, O_RDONLY);
00142 stats->file = fdopen(fd, "rb");
00143 fstat(fd, &stat_buf);
00144 stats->buf.sz = stat_buf.st_size;
00145 stats->buf.buf = mmap(NULL, stats->buf.sz, PROT_READ, MAP_PRIVATE,
00146 fd, 0);
00147 res = (stats->buf.buf != NULL);
00148 #else
00149 size_t nbytes;
00150
00151 stats->file = fopen(fpf, "rb");
00152
00153 if (fseek(stats->file, 0, SEEK_END))
00154 {
00155 fprintf(stderr, "First-pass stats file must be seekable!\n");
00156 exit(EXIT_FAILURE);
00157 }
00158
00159 stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file);
00160 rewind(stats->file);
00161
00162 stats->buf.buf = malloc(stats->buf_alloc_sz);
00163
00164 if (!stats->buf.buf)
00165 {
00166 fprintf(stderr, "Failed to allocate first-pass stats buffer (%d bytes)\n",
00167 stats->buf_alloc_sz);
00168 exit(EXIT_FAILURE);
00169 }
00170
00171 nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
00172 res = (nbytes == stats->buf.sz);
00173 #endif
00174 }
00175
00176 return res;
00177 }
00178
00179 int stats_open_mem(stats_io_t *stats, int pass)
00180 {
00181 int res;
00182 stats->pass = pass;
00183
00184 if (!pass)
00185 {
00186 stats->buf.sz = 0;
00187 stats->buf_alloc_sz = 64 * 1024;
00188 stats->buf.buf = malloc(stats->buf_alloc_sz);
00189 }
00190
00191 stats->buf_ptr = stats->buf.buf;
00192 res = (stats->buf.buf != NULL);
00193 return res;
00194 }
00195
00196
00197 void stats_close(stats_io_t *stats, int last_pass)
00198 {
00199 if (stats->file)
00200 {
00201 if (stats->pass == last_pass)
00202 {
00203 #if 0
00204 #elif USE_POSIX_MMAP
00205 munmap(stats->buf.buf, stats->buf.sz);
00206 #else
00207 free(stats->buf.buf);
00208 #endif
00209 }
00210
00211 fclose(stats->file);
00212 stats->file = NULL;
00213 }
00214 else
00215 {
00216 if (stats->pass == last_pass)
00217 free(stats->buf.buf);
00218 }
00219 }
00220
00221 void stats_write(stats_io_t *stats, const void *pkt, size_t len)
00222 {
00223 if (stats->file)
00224 {
00225 if(fwrite(pkt, 1, len, stats->file));
00226 }
00227 else
00228 {
00229 if (stats->buf.sz + len > stats->buf_alloc_sz)
00230 {
00231 size_t new_sz = stats->buf_alloc_sz + 64 * 1024;
00232 char *new_ptr = realloc(stats->buf.buf, new_sz);
00233
00234 if (new_ptr)
00235 {
00236 stats->buf_ptr = new_ptr + (stats->buf_ptr - (char *)stats->buf.buf);
00237 stats->buf.buf = new_ptr;
00238 stats->buf_alloc_sz = new_sz;
00239 }
00240 }
00241
00242 memcpy(stats->buf_ptr, pkt, len);
00243 stats->buf.sz += len;
00244 stats->buf_ptr += len;
00245 }
00246 }
00247
00248 vpx_fixed_buf_t stats_get(stats_io_t *stats)
00249 {
00250 return stats->buf;
00251 }
00252
00253 enum video_file_type
00254 {
00255 FILE_TYPE_RAW,
00256 FILE_TYPE_IVF,
00257 FILE_TYPE_Y4M
00258 };
00259
00260 struct detect_buffer {
00261 char buf[4];
00262 size_t buf_read;
00263 size_t position;
00264 };
00265
00266
00267 #define IVF_FRAME_HDR_SZ (4+8)
00268 static int read_frame(FILE *f, vpx_image_t *img, unsigned int file_type,
00269 y4m_input *y4m, struct detect_buffer *detect)
00270 {
00271 int plane = 0;
00272 int shortread = 0;
00273
00274 if (file_type == FILE_TYPE_Y4M)
00275 {
00276 if (y4m_input_fetch_frame(y4m, f, img) < 1)
00277 return 0;
00278 }
00279 else
00280 {
00281 if (file_type == FILE_TYPE_IVF)
00282 {
00283 char junk[IVF_FRAME_HDR_SZ];
00284
00285
00286
00287
00288
00289 if(fread(junk, 1, IVF_FRAME_HDR_SZ, f));
00290 }
00291
00292 for (plane = 0; plane < 3; plane++)
00293 {
00294 unsigned char *ptr;
00295 int w = (plane ? (1 + img->d_w) / 2 : img->d_w);
00296 int h = (plane ? (1 + img->d_h) / 2 : img->d_h);
00297 int r;
00298
00299
00300
00301
00302
00303 switch (plane)
00304 {
00305 case 1:
00306 ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12? VPX_PLANE_V : VPX_PLANE_U];
00307 break;
00308 case 2:
00309 ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12?VPX_PLANE_U : VPX_PLANE_V];
00310 break;
00311 default:
00312 ptr = img->planes[plane];
00313 }
00314
00315 for (r = 0; r < h; r++)
00316 {
00317 size_t needed = w;
00318 size_t buf_position = 0;
00319 const size_t left = detect->buf_read - detect->position;
00320 if (left > 0)
00321 {
00322 const size_t more = (left < needed) ? left : needed;
00323 memcpy(ptr, detect->buf + detect->position, more);
00324 buf_position = more;
00325 needed -= more;
00326 detect->position += more;
00327 }
00328 if (needed > 0)
00329 {
00330 shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
00331 }
00332
00333 ptr += img->stride[plane];
00334 }
00335 }
00336 }
00337
00338 return !shortread;
00339 }
00340
00341
00342 unsigned int file_is_y4m(FILE *infile,
00343 y4m_input *y4m,
00344 char detect[4])
00345 {
00346 if(memcmp(detect, "YUV4", 4) == 0)
00347 {
00348 return 1;
00349 }
00350 return 0;
00351 }
00352
00353 #define IVF_FILE_HDR_SZ (32)
00354 unsigned int file_is_ivf(FILE *infile,
00355 unsigned int *fourcc,
00356 unsigned int *width,
00357 unsigned int *height,
00358 struct detect_buffer *detect)
00359 {
00360 char raw_hdr[IVF_FILE_HDR_SZ];
00361 int is_ivf = 0;
00362
00363 if(memcmp(detect->buf, "DKIF", 4) != 0)
00364 return 0;
00365
00366
00367
00368
00369 if (fread(raw_hdr + 4, 1, IVF_FILE_HDR_SZ - 4, infile)
00370 == IVF_FILE_HDR_SZ - 4)
00371 {
00372 {
00373 is_ivf = 1;
00374
00375 if (mem_get_le16(raw_hdr + 4) != 0)
00376 fprintf(stderr, "Error: Unrecognized IVF version! This file may not"
00377 " decode properly.");
00378
00379 *fourcc = mem_get_le32(raw_hdr + 8);
00380 }
00381 }
00382
00383 if (is_ivf)
00384 {
00385 *width = mem_get_le16(raw_hdr + 12);
00386 *height = mem_get_le16(raw_hdr + 14);
00387 detect->position = 4;
00388 }
00389
00390 return is_ivf;
00391 }
00392
00393
00394 static void write_ivf_file_header(FILE *outfile,
00395 const vpx_codec_enc_cfg_t *cfg,
00396 unsigned int fourcc,
00397 int frame_cnt)
00398 {
00399 char header[32];
00400
00401 if (cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS)
00402 return;
00403
00404 header[0] = 'D';
00405 header[1] = 'K';
00406 header[2] = 'I';
00407 header[3] = 'F';
00408 mem_put_le16(header + 4, 0);
00409 mem_put_le16(header + 6, 32);
00410 mem_put_le32(header + 8, fourcc);
00411 mem_put_le16(header + 12, cfg->g_w);
00412 mem_put_le16(header + 14, cfg->g_h);
00413 mem_put_le32(header + 16, cfg->g_timebase.den);
00414 mem_put_le32(header + 20, cfg->g_timebase.num);
00415 mem_put_le32(header + 24, frame_cnt);
00416 mem_put_le32(header + 28, 0);
00417
00418 if(fwrite(header, 1, 32, outfile));
00419 }
00420
00421
00422 static void write_ivf_frame_header(FILE *outfile,
00423 const vpx_codec_cx_pkt_t *pkt)
00424 {
00425 char header[12];
00426 vpx_codec_pts_t pts;
00427
00428 if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
00429 return;
00430
00431 pts = pkt->data.frame.pts;
00432 mem_put_le32(header, pkt->data.frame.sz);
00433 mem_put_le32(header + 4, pts & 0xFFFFFFFF);
00434 mem_put_le32(header + 8, pts >> 32);
00435
00436 if(fwrite(header, 1, 12, outfile));
00437 }
00438
00439
00440 typedef off_t EbmlLoc;
00441
00442
00443 struct cue_entry
00444 {
00445 unsigned int time;
00446 uint64_t loc;
00447 };
00448
00449
00450 struct EbmlGlobal
00451 {
00452 int debug;
00453
00454 FILE *stream;
00455 int64_t last_pts_ms;
00456 vpx_rational_t framerate;
00457
00458
00459 off_t position_reference;
00460 off_t seek_info_pos;
00461 off_t segment_info_pos;
00462 off_t track_pos;
00463 off_t cue_pos;
00464 off_t cluster_pos;
00465
00466
00467 off_t track_id_pos;
00468
00469
00470 EbmlLoc startSegment;
00471 EbmlLoc startCluster;
00472
00473 uint32_t cluster_timecode;
00474 int cluster_open;
00475
00476 struct cue_entry *cue_list;
00477 unsigned int cues;
00478
00479 };
00480
00481
00482 void Ebml_Write(EbmlGlobal *glob, const void *buffer_in, unsigned long len)
00483 {
00484 if(fwrite(buffer_in, 1, len, glob->stream));
00485 }
00486
00487
00488 void Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, unsigned long len)
00489 {
00490 const unsigned char *q = (const unsigned char *)buffer_in + len - 1;
00491
00492 for(; len; len--)
00493 Ebml_Write(glob, q--, 1);
00494 }
00495
00496
00497
00498
00499
00500 static void Ebml_SerializeUnsigned32(EbmlGlobal *glob, unsigned long class_id, uint64_t ui)
00501 {
00502 unsigned char sizeSerialized = 4 | 0x80;
00503 Ebml_WriteID(glob, class_id);
00504 Ebml_Serialize(glob, &sizeSerialized, 1);
00505 Ebml_Serialize(glob, &ui, 4);
00506 }
00507
00508
00509 static void
00510 Ebml_StartSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc,
00511 unsigned long class_id)
00512 {
00513
00514
00515 unsigned long long unknownLen = LITERALU64(0x01FFFFFFFFFFFFFF);
00516
00517 Ebml_WriteID(glob, class_id);
00518 *ebmlLoc = ftello(glob->stream);
00519 Ebml_Serialize(glob, &unknownLen, 8);
00520 }
00521
00522 static void
00523 Ebml_EndSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc)
00524 {
00525 off_t pos;
00526 uint64_t size;
00527
00528
00529 pos = ftello(glob->stream);
00530
00531
00532 size = pos - *ebmlLoc - 8;
00533 size |= LITERALU64(0x0100000000000000);
00534
00535
00536 fseeko(glob->stream, *ebmlLoc, SEEK_SET);
00537 Ebml_Serialize(glob, &size, 8);
00538
00539
00540 fseeko(glob->stream, pos, SEEK_SET);
00541 }
00542
00543
00544 static void
00545 write_webm_seek_element(EbmlGlobal *ebml, unsigned long id, off_t pos)
00546 {
00547 uint64_t offset = pos - ebml->position_reference;
00548 EbmlLoc start;
00549 Ebml_StartSubElement(ebml, &start, Seek);
00550 Ebml_SerializeBinary(ebml, SeekID, id);
00551 Ebml_SerializeUnsigned64(ebml, SeekPosition, offset);
00552 Ebml_EndSubElement(ebml, &start);
00553 }
00554
00555
00556 static void
00557 write_webm_seek_info(EbmlGlobal *ebml)
00558 {
00559
00560 off_t pos;
00561
00562
00563 pos = ftello(ebml->stream);
00564
00565 if(ebml->seek_info_pos)
00566 fseeko(ebml->stream, ebml->seek_info_pos, SEEK_SET);
00567 else
00568 ebml->seek_info_pos = pos;
00569
00570 {
00571 EbmlLoc start;
00572
00573 Ebml_StartSubElement(ebml, &start, SeekHead);
00574 write_webm_seek_element(ebml, Tracks, ebml->track_pos);
00575 write_webm_seek_element(ebml, Cues, ebml->cue_pos);
00576 write_webm_seek_element(ebml, Info, ebml->segment_info_pos);
00577 Ebml_EndSubElement(ebml, &start);
00578 }
00579 {
00580
00581 EbmlLoc startInfo;
00582 uint64_t frame_time;
00583
00584 frame_time = (uint64_t)1000 * ebml->framerate.den
00585 / ebml->framerate.num;
00586 ebml->segment_info_pos = ftello(ebml->stream);
00587 Ebml_StartSubElement(ebml, &startInfo, Info);
00588 Ebml_SerializeUnsigned(ebml, TimecodeScale, 1000000);
00589 Ebml_SerializeFloat(ebml, Segment_Duration,
00590 ebml->last_pts_ms + frame_time);
00591 Ebml_SerializeString(ebml, 0x4D80,
00592 ebml->debug ? "vpxenc" : "vpxenc" VERSION_STRING);
00593 Ebml_SerializeString(ebml, 0x5741,
00594 ebml->debug ? "vpxenc" : "vpxenc" VERSION_STRING);
00595 Ebml_EndSubElement(ebml, &startInfo);
00596 }
00597 }
00598
00599
00600 static void
00601 write_webm_file_header(EbmlGlobal *glob,
00602 const vpx_codec_enc_cfg_t *cfg,
00603 const struct vpx_rational *fps)
00604 {
00605 {
00606 EbmlLoc start;
00607 Ebml_StartSubElement(glob, &start, EBML);
00608 Ebml_SerializeUnsigned(glob, EBMLVersion, 1);
00609 Ebml_SerializeUnsigned(glob, EBMLReadVersion, 1);
00610 Ebml_SerializeUnsigned(glob, EBMLMaxIDLength, 4);
00611 Ebml_SerializeUnsigned(glob, EBMLMaxSizeLength, 8);
00612 Ebml_SerializeString(glob, DocType, "webm");
00613 Ebml_SerializeUnsigned(glob, DocTypeVersion, 2);
00614 Ebml_SerializeUnsigned(glob, DocTypeReadVersion, 2);
00615 Ebml_EndSubElement(glob, &start);
00616 }
00617 {
00618 Ebml_StartSubElement(glob, &glob->startSegment, Segment);
00619 glob->position_reference = ftello(glob->stream);
00620 glob->framerate = *fps;
00621 write_webm_seek_info(glob);
00622
00623 {
00624 EbmlLoc trackStart;
00625 glob->track_pos = ftello(glob->stream);
00626 Ebml_StartSubElement(glob, &trackStart, Tracks);
00627 {
00628 unsigned int trackNumber = 1;
00629 uint64_t trackID = 0;
00630
00631 EbmlLoc start;
00632 Ebml_StartSubElement(glob, &start, TrackEntry);
00633 Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
00634 glob->track_id_pos = ftello(glob->stream);
00635 Ebml_SerializeUnsigned32(glob, TrackUID, trackID);
00636 Ebml_SerializeUnsigned(glob, TrackType, 1);
00637 Ebml_SerializeString(glob, CodecID, "V_VP8");
00638 {
00639 unsigned int pixelWidth = cfg->g_w;
00640 unsigned int pixelHeight = cfg->g_h;
00641 float frameRate = (float)fps->num/(float)fps->den;
00642
00643 EbmlLoc videoStart;
00644 Ebml_StartSubElement(glob, &videoStart, Video);
00645 Ebml_SerializeUnsigned(glob, PixelWidth, pixelWidth);
00646 Ebml_SerializeUnsigned(glob, PixelHeight, pixelHeight);
00647 Ebml_SerializeFloat(glob, FrameRate, frameRate);
00648 Ebml_EndSubElement(glob, &videoStart);
00649 }
00650 Ebml_EndSubElement(glob, &start);
00651 }
00652 Ebml_EndSubElement(glob, &trackStart);
00653 }
00654
00655 }
00656 }
00657
00658
00659 static void
00660 write_webm_block(EbmlGlobal *glob,
00661 const vpx_codec_enc_cfg_t *cfg,
00662 const vpx_codec_cx_pkt_t *pkt)
00663 {
00664 unsigned long block_length;
00665 unsigned char track_number;
00666 unsigned short block_timecode = 0;
00667 unsigned char flags;
00668 int64_t pts_ms;
00669 int start_cluster = 0, is_keyframe;
00670
00671
00672 pts_ms = pkt->data.frame.pts * 1000
00673 * (uint64_t)cfg->g_timebase.num / (uint64_t)cfg->g_timebase.den;
00674 if(pts_ms <= glob->last_pts_ms)
00675 pts_ms = glob->last_pts_ms + 1;
00676 glob->last_pts_ms = pts_ms;
00677
00678
00679 if(pts_ms - glob->cluster_timecode > SHRT_MAX)
00680 start_cluster = 1;
00681 else
00682 block_timecode = pts_ms - glob->cluster_timecode;
00683
00684 is_keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY);
00685 if(start_cluster || is_keyframe)
00686 {
00687 if(glob->cluster_open)
00688 Ebml_EndSubElement(glob, &glob->startCluster);
00689
00690
00691 block_timecode = 0;
00692 glob->cluster_open = 1;
00693 glob->cluster_timecode = pts_ms;
00694 glob->cluster_pos = ftello(glob->stream);
00695 Ebml_StartSubElement(glob, &glob->startCluster, Cluster);
00696 Ebml_SerializeUnsigned(glob, Timecode, glob->cluster_timecode);
00697
00698
00699 if(is_keyframe)
00700 {
00701 struct cue_entry *cue;
00702
00703 glob->cue_list = realloc(glob->cue_list,
00704 (glob->cues+1) * sizeof(struct cue_entry));
00705 cue = &glob->cue_list[glob->cues];
00706 cue->time = glob->cluster_timecode;
00707 cue->loc = glob->cluster_pos;
00708 glob->cues++;
00709 }
00710 }
00711
00712
00713 Ebml_WriteID(glob, SimpleBlock);
00714
00715 block_length = pkt->data.frame.sz + 4;
00716 block_length |= 0x10000000;
00717 Ebml_Serialize(glob, &block_length, 4);
00718
00719 track_number = 1;
00720 track_number |= 0x80;
00721 Ebml_Write(glob, &track_number, 1);
00722
00723 Ebml_Serialize(glob, &block_timecode, 2);
00724
00725 flags = 0;
00726 if(is_keyframe)
00727 flags |= 0x80;
00728 if(pkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE)
00729 flags |= 0x08;
00730 Ebml_Write(glob, &flags, 1);
00731
00732 Ebml_Write(glob, pkt->data.frame.buf, pkt->data.frame.sz);
00733 }
00734
00735
00736 static void
00737 write_webm_file_footer(EbmlGlobal *glob, long hash)
00738 {
00739
00740 if(glob->cluster_open)
00741 Ebml_EndSubElement(glob, &glob->startCluster);
00742
00743 {
00744 EbmlLoc start;
00745 int i;
00746
00747 glob->cue_pos = ftello(glob->stream);
00748 Ebml_StartSubElement(glob, &start, Cues);
00749 for(i=0; i<glob->cues; i++)
00750 {
00751 struct cue_entry *cue = &glob->cue_list[i];
00752 EbmlLoc start;
00753
00754 Ebml_StartSubElement(glob, &start, CuePoint);
00755 {
00756 EbmlLoc start;
00757
00758 Ebml_SerializeUnsigned(glob, CueTime, cue->time);
00759
00760 Ebml_StartSubElement(glob, &start, CueTrackPositions);
00761 Ebml_SerializeUnsigned(glob, CueTrack, 1);
00762 Ebml_SerializeUnsigned64(glob, CueClusterPosition,
00763 cue->loc - glob->position_reference);
00764
00765 Ebml_EndSubElement(glob, &start);
00766 }
00767 Ebml_EndSubElement(glob, &start);
00768 }
00769 Ebml_EndSubElement(glob, &start);
00770 }
00771
00772 Ebml_EndSubElement(glob, &glob->startSegment);
00773
00774
00775 write_webm_seek_info(glob);
00776
00777
00778 fseeko(glob->stream, glob->track_id_pos, SEEK_SET);
00779 Ebml_SerializeUnsigned32(glob, TrackUID, glob->debug ? 0xDEADBEEF : hash);
00780
00781 fseeko(glob->stream, 0, SEEK_END);
00782 }
00783
00784
00785
00786
00787
00788 static unsigned int murmur ( const void * key, int len, unsigned int seed )
00789 {
00790 const unsigned int m = 0x5bd1e995;
00791 const int r = 24;
00792
00793 unsigned int h = seed ^ len;
00794
00795 const unsigned char * data = (const unsigned char *)key;
00796
00797 while(len >= 4)
00798 {
00799 unsigned int k;
00800
00801 k = data[0];
00802 k |= data[1] << 8;
00803 k |= data[2] << 16;
00804 k |= data[3] << 24;
00805
00806 k *= m;
00807 k ^= k >> r;
00808 k *= m;
00809
00810 h *= m;
00811 h ^= k;
00812
00813 data += 4;
00814 len -= 4;
00815 }
00816
00817 switch(len)
00818 {
00819 case 3: h ^= data[2] << 16;
00820 case 2: h ^= data[1] << 8;
00821 case 1: h ^= data[0];
00822 h *= m;
00823 };
00824
00825 h ^= h >> 13;
00826 h *= m;
00827 h ^= h >> 15;
00828
00829 return h;
00830 }
00831
00832 #include "math.h"
00833
00834 static double vp8_mse2psnr(double Samples, double Peak, double Mse)
00835 {
00836 double psnr;
00837
00838 if ((double)Mse > 0.0)
00839 psnr = 10.0 * log10(Peak * Peak * Samples / Mse);
00840 else
00841 psnr = 60;
00842
00843 if (psnr > 60)
00844 psnr = 60;
00845
00846 return psnr;
00847 }
00848
00849
00850 #include "args.h"
00851
00852 static const arg_def_t debugmode = ARG_DEF("D", "debug", 0,
00853 "Debug mode (makes output deterministic)");
00854 static const arg_def_t outputfile = ARG_DEF("o", "output", 1,
00855 "Output filename");
00856 static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0,
00857 "Input file is YV12 ");
00858 static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0,
00859 "Input file is I420 (default)");
00860 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1,
00861 "Codec to use");
00862 static const arg_def_t passes = ARG_DEF("p", "passes", 1,
00863 "Number of passes (1/2)");
00864 static const arg_def_t pass_arg = ARG_DEF(NULL, "pass", 1,
00865 "Pass to execute (1/2)");
00866 static const arg_def_t fpf_name = ARG_DEF(NULL, "fpf", 1,
00867 "First pass statistics file name");
00868 static const arg_def_t limit = ARG_DEF(NULL, "limit", 1,
00869 "Stop encoding after n input frames");
00870 static const arg_def_t deadline = ARG_DEF("d", "deadline", 1,
00871 "Deadline per frame (usec)");
00872 static const arg_def_t best_dl = ARG_DEF(NULL, "best", 0,
00873 "Use Best Quality Deadline");
00874 static const arg_def_t good_dl = ARG_DEF(NULL, "good", 0,
00875 "Use Good Quality Deadline");
00876 static const arg_def_t rt_dl = ARG_DEF(NULL, "rt", 0,
00877 "Use Realtime Quality Deadline");
00878 static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0,
00879 "Show encoder parameters");
00880 static const arg_def_t psnrarg = ARG_DEF(NULL, "psnr", 0,
00881 "Show PSNR in status line");
00882 static const arg_def_t framerate = ARG_DEF(NULL, "fps", 1,
00883 "Stream frame rate (rate/scale)");
00884 static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0,
00885 "Output IVF (default is WebM)");
00886 static const arg_def_t *main_args[] =
00887 {
00888 &debugmode,
00889 &outputfile, &codecarg, &passes, &pass_arg, &fpf_name, &limit, &deadline,
00890 &best_dl, &good_dl, &rt_dl,
00891 &verbosearg, &psnrarg, &use_ivf, &framerate,
00892 NULL
00893 };
00894
00895 static const arg_def_t usage = ARG_DEF("u", "usage", 1,
00896 "Usage profile number to use");
00897 static const arg_def_t threads = ARG_DEF("t", "threads", 1,
00898 "Max number of threads to use");
00899 static const arg_def_t profile = ARG_DEF(NULL, "profile", 1,
00900 "Bitstream profile number to use");
00901 static const arg_def_t width = ARG_DEF("w", "width", 1,
00902 "Frame width");
00903 static const arg_def_t height = ARG_DEF("h", "height", 1,
00904 "Frame height");
00905 static const arg_def_t timebase = ARG_DEF(NULL, "timebase", 1,
00906 "Stream timebase (frame duration)");
00907 static const arg_def_t error_resilient = ARG_DEF(NULL, "error-resilient", 1,
00908 "Enable error resiliency features");
00909 static const arg_def_t lag_in_frames = ARG_DEF(NULL, "lag-in-frames", 1,
00910 "Max number of frames to lag");
00911
00912 static const arg_def_t *global_args[] =
00913 {
00914 &use_yv12, &use_i420, &usage, &threads, &profile,
00915 &width, &height, &timebase, &framerate, &error_resilient,
00916 &lag_in_frames, NULL
00917 };
00918
00919 static const arg_def_t dropframe_thresh = ARG_DEF(NULL, "drop-frame", 1,
00920 "Temporal resampling threshold (buf %)");
00921 static const arg_def_t resize_allowed = ARG_DEF(NULL, "resize-allowed", 1,
00922 "Spatial resampling enabled (bool)");
00923 static const arg_def_t resize_up_thresh = ARG_DEF(NULL, "resize-up", 1,
00924 "Upscale threshold (buf %)");
00925 static const arg_def_t resize_down_thresh = ARG_DEF(NULL, "resize-down", 1,
00926 "Downscale threshold (buf %)");
00927 static const arg_def_t end_usage = ARG_DEF(NULL, "end-usage", 1,
00928 "VBR=0 | CBR=1 | CQ=2");
00929 static const arg_def_t target_bitrate = ARG_DEF(NULL, "target-bitrate", 1,
00930 "Bitrate (kbps)");
00931 static const arg_def_t min_quantizer = ARG_DEF(NULL, "min-q", 1,
00932 "Minimum (best) quantizer");
00933 static const arg_def_t max_quantizer = ARG_DEF(NULL, "max-q", 1,
00934 "Maximum (worst) quantizer");
00935 static const arg_def_t undershoot_pct = ARG_DEF(NULL, "undershoot-pct", 1,
00936 "Datarate undershoot (min) target (%)");
00937 static const arg_def_t overshoot_pct = ARG_DEF(NULL, "overshoot-pct", 1,
00938 "Datarate overshoot (max) target (%)");
00939 static const arg_def_t buf_sz = ARG_DEF(NULL, "buf-sz", 1,
00940 "Client buffer size (ms)");
00941 static const arg_def_t buf_initial_sz = ARG_DEF(NULL, "buf-initial-sz", 1,
00942 "Client initial buffer size (ms)");
00943 static const arg_def_t buf_optimal_sz = ARG_DEF(NULL, "buf-optimal-sz", 1,
00944 "Client optimal buffer size (ms)");
00945 static const arg_def_t *rc_args[] =
00946 {
00947 &dropframe_thresh, &resize_allowed, &resize_up_thresh, &resize_down_thresh,
00948 &end_usage, &target_bitrate, &min_quantizer, &max_quantizer,
00949 &undershoot_pct, &overshoot_pct, &buf_sz, &buf_initial_sz, &buf_optimal_sz,
00950 NULL
00951 };
00952
00953
00954 static const arg_def_t bias_pct = ARG_DEF(NULL, "bias-pct", 1,
00955 "CBR/VBR bias (0=CBR, 100=VBR)");
00956 static const arg_def_t minsection_pct = ARG_DEF(NULL, "minsection-pct", 1,
00957 "GOP min bitrate (% of target)");
00958 static const arg_def_t maxsection_pct = ARG_DEF(NULL, "maxsection-pct", 1,
00959 "GOP max bitrate (% of target)");
00960 static const arg_def_t *rc_twopass_args[] =
00961 {
00962 &bias_pct, &minsection_pct, &maxsection_pct, NULL
00963 };
00964
00965
00966 static const arg_def_t kf_min_dist = ARG_DEF(NULL, "kf-min-dist", 1,
00967 "Minimum keyframe interval (frames)");
00968 static const arg_def_t kf_max_dist = ARG_DEF(NULL, "kf-max-dist", 1,
00969 "Maximum keyframe interval (frames)");
00970 static const arg_def_t kf_disabled = ARG_DEF(NULL, "disable-kf", 0,
00971 "Disable keyframe placement");
00972 static const arg_def_t *kf_args[] =
00973 {
00974 &kf_min_dist, &kf_max_dist, &kf_disabled, NULL
00975 };
00976
00977
00978 #if CONFIG_VP8_ENCODER
00979 static const arg_def_t noise_sens = ARG_DEF(NULL, "noise-sensitivity", 1,
00980 "Noise sensitivity (frames to blur)");
00981 static const arg_def_t sharpness = ARG_DEF(NULL, "sharpness", 1,
00982 "Filter sharpness (0-7)");
00983 static const arg_def_t static_thresh = ARG_DEF(NULL, "static-thresh", 1,
00984 "Motion detection threshold");
00985 #endif
00986
00987 #if CONFIG_VP8_ENCODER
00988 static const arg_def_t cpu_used = ARG_DEF(NULL, "cpu-used", 1,
00989 "CPU Used (-16..16)");
00990 #endif
00991
00992
00993 #if CONFIG_VP8_ENCODER
00994 static const arg_def_t token_parts = ARG_DEF(NULL, "token-parts", 1,
00995 "Number of token partitions to use, log2");
00996 static const arg_def_t auto_altref = ARG_DEF(NULL, "auto-alt-ref", 1,
00997 "Enable automatic alt reference frames");
00998 static const arg_def_t arnr_maxframes = ARG_DEF(NULL, "arnr-maxframes", 1,
00999 "AltRef Max Frames");
01000 static const arg_def_t arnr_strength = ARG_DEF(NULL, "arnr-strength", 1,
01001 "AltRef Strength");
01002 static const arg_def_t arnr_type = ARG_DEF(NULL, "arnr-type", 1,
01003 "AltRef Type");
01004 static const struct arg_enum_list tuning_enum[] = {
01005 {"psnr", VP8_TUNE_PSNR},
01006 {"ssim", VP8_TUNE_SSIM},
01007 {NULL, 0}
01008 };
01009 static const arg_def_t tune_ssim = ARG_DEF_ENUM(NULL, "tune", 1,
01010 "Material to favor", tuning_enum);
01011 static const arg_def_t cq_level = ARG_DEF(NULL, "cq-level", 1,
01012 "Constrained Quality Level");
01013
01014 static const arg_def_t *vp8_args[] =
01015 {
01016 &cpu_used, &auto_altref, &noise_sens, &sharpness, &static_thresh,
01017 &token_parts, &arnr_maxframes, &arnr_strength, &arnr_type,
01018 &tune_ssim, &cq_level, NULL
01019 };
01020 static const int vp8_arg_ctrl_map[] =
01021 {
01022 VP8E_SET_CPUUSED, VP8E_SET_ENABLEAUTOALTREF,
01023 VP8E_SET_NOISE_SENSITIVITY, VP8E_SET_SHARPNESS, VP8E_SET_STATIC_THRESHOLD,
01024 VP8E_SET_TOKEN_PARTITIONS,
01025 VP8E_SET_ARNR_MAXFRAMES, VP8E_SET_ARNR_STRENGTH , VP8E_SET_ARNR_TYPE,
01026 VP8E_SET_TUNING, VP8E_SET_CQ_LEVEL, 0
01027 };
01028 #endif
01029
01030 static const arg_def_t *no_args[] = { NULL };
01031
01032 static void usage_exit()
01033 {
01034 int i;
01035
01036 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
01037 exec_name);
01038
01039 fprintf(stderr, "\nOptions:\n");
01040 arg_show_usage(stdout, main_args);
01041 fprintf(stderr, "\nEncoder Global Options:\n");
01042 arg_show_usage(stdout, global_args);
01043 fprintf(stderr, "\nRate Control Options:\n");
01044 arg_show_usage(stdout, rc_args);
01045 fprintf(stderr, "\nTwopass Rate Control Options:\n");
01046 arg_show_usage(stdout, rc_twopass_args);
01047 fprintf(stderr, "\nKeyframe Placement Options:\n");
01048 arg_show_usage(stdout, kf_args);
01049 #if CONFIG_VP8_ENCODER
01050 fprintf(stderr, "\nVP8 Specific Options:\n");
01051 arg_show_usage(stdout, vp8_args);
01052 #endif
01053 fprintf(stderr, "\n"
01054 "Included encoders:\n"
01055 "\n");
01056
01057 for (i = 0; i < sizeof(codecs) / sizeof(codecs[0]); i++)
01058 fprintf(stderr, " %-6s - %s\n",
01059 codecs[i].name,
01060 vpx_codec_iface_name(codecs[i].iface));
01061
01062 exit(EXIT_FAILURE);
01063 }
01064
01065 #define ARG_CTRL_CNT_MAX 10
01066
01067
01068 int main(int argc, const char **argv_)
01069 {
01070 vpx_codec_ctx_t encoder;
01071 const char *in_fn = NULL, *out_fn = NULL, *stats_fn = NULL;
01072 int i;
01073 FILE *infile, *outfile;
01074 vpx_codec_enc_cfg_t cfg;
01075 vpx_codec_err_t res;
01076 int pass, one_pass_only = 0;
01077 stats_io_t stats;
01078 vpx_image_t raw;
01079 const struct codec_item *codec = codecs;
01080 int frame_avail, got_data;
01081
01082 struct arg arg;
01083 char **argv, **argi, **argj;
01084 int arg_usage = 0, arg_passes = 1, arg_deadline = 0;
01085 int arg_ctrls[ARG_CTRL_CNT_MAX][2], arg_ctrl_cnt = 0;
01086 int arg_limit = 0;
01087 static const arg_def_t **ctrl_args = no_args;
01088 static const int *ctrl_args_map = NULL;
01089 int verbose = 0, show_psnr = 0;
01090 int arg_use_i420 = 1;
01091 unsigned long cx_time = 0;
01092 unsigned int file_type, fourcc;
01093 y4m_input y4m;
01094 struct vpx_rational arg_framerate = {30, 1};
01095 int arg_have_framerate = 0;
01096 int write_webm = 1;
01097 EbmlGlobal ebml = {0};
01098 uint32_t hash = 0;
01099 uint64_t psnr_sse_total = 0;
01100 uint64_t psnr_samples_total = 0;
01101 double psnr_totals[4] = {0, 0, 0, 0};
01102 int psnr_count = 0;
01103
01104 exec_name = argv_[0];
01105 ebml.last_pts_ms = -1;
01106
01107 if (argc < 3)
01108 usage_exit();
01109
01110
01111
01112
01113
01114 argv = argv_dup(argc - 1, argv_ + 1);
01115
01116 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
01117 {
01118 arg.argv_step = 1;
01119
01120 if (arg_match(&arg, &codecarg, argi))
01121 {
01122 int j, k = -1;
01123
01124 for (j = 0; j < sizeof(codecs) / sizeof(codecs[0]); j++)
01125 if (!strcmp(codecs[j].name, arg.val))
01126 k = j;
01127
01128 if (k >= 0)
01129 codec = codecs + k;
01130 else
01131 die("Error: Unrecognized argument (%s) to --codec\n",
01132 arg.val);
01133
01134 }
01135 else if (arg_match(&arg, &passes, argi))
01136 {
01137 arg_passes = arg_parse_uint(&arg);
01138
01139 if (arg_passes < 1 || arg_passes > 2)
01140 die("Error: Invalid number of passes (%d)\n", arg_passes);
01141 }
01142 else if (arg_match(&arg, &pass_arg, argi))
01143 {
01144 one_pass_only = arg_parse_uint(&arg);
01145
01146 if (one_pass_only < 1 || one_pass_only > 2)
01147 die("Error: Invalid pass selected (%d)\n", one_pass_only);
01148 }
01149 else if (arg_match(&arg, &fpf_name, argi))
01150 stats_fn = arg.val;
01151 else if (arg_match(&arg, &usage, argi))
01152 arg_usage = arg_parse_uint(&arg);
01153 else if (arg_match(&arg, &deadline, argi))
01154 arg_deadline = arg_parse_uint(&arg);
01155 else if (arg_match(&arg, &best_dl, argi))
01156 arg_deadline = VPX_DL_BEST_QUALITY;
01157 else if (arg_match(&arg, &good_dl, argi))
01158 arg_deadline = VPX_DL_GOOD_QUALITY;
01159 else if (arg_match(&arg, &rt_dl, argi))
01160 arg_deadline = VPX_DL_REALTIME;
01161 else if (arg_match(&arg, &use_yv12, argi))
01162 {
01163 arg_use_i420 = 0;
01164 }
01165 else if (arg_match(&arg, &use_i420, argi))
01166 {
01167 arg_use_i420 = 1;
01168 }
01169 else if (arg_match(&arg, &verbosearg, argi))
01170 verbose = 1;
01171 else if (arg_match(&arg, &limit, argi))
01172 arg_limit = arg_parse_uint(&arg);
01173 else if (arg_match(&arg, &psnrarg, argi))
01174 show_psnr = 1;
01175 else if (arg_match(&arg, &framerate, argi))
01176 {
01177 arg_framerate = arg_parse_rational(&arg);
01178 arg_have_framerate = 1;
01179 }
01180 else if (arg_match(&arg, &use_ivf, argi))
01181 write_webm = 0;
01182 else if (arg_match(&arg, &outputfile, argi))
01183 out_fn = arg.val;
01184 else if (arg_match(&arg, &debugmode, argi))
01185 ebml.debug = 1;
01186 else
01187 argj++;
01188 }
01189
01190
01191
01192
01193 if (one_pass_only)
01194 {
01195
01196 if (one_pass_only > arg_passes)
01197 {
01198 fprintf(stderr, "Warning: Assuming --pass=%d implies --passes=%d\n",
01199 one_pass_only, one_pass_only);
01200 arg_passes = one_pass_only;
01201 }
01202
01203 if (arg_passes == 2 && !stats_fn)
01204 die("Must specify --fpf when --pass=%d and --passes=2\n", one_pass_only);
01205 }
01206
01207
01208 res = vpx_codec_enc_config_default(codec->iface, &cfg, arg_usage);
01209
01210 if (res)
01211 {
01212 fprintf(stderr, "Failed to get config: %s\n",
01213 vpx_codec_err_to_string(res));
01214 return EXIT_FAILURE;
01215 }
01216
01217
01218
01219
01220 cfg.g_timebase.den = 1000;
01221
01222
01223
01224
01225 cfg.g_w = 0;
01226 cfg.g_h = 0;
01227
01228
01229 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
01230 {
01231 arg.argv_step = 1;
01232
01233 if (0);
01234 else if (arg_match(&arg, &threads, argi))
01235 cfg.g_threads = arg_parse_uint(&arg);
01236 else if (arg_match(&arg, &profile, argi))
01237 cfg.g_profile = arg_parse_uint(&arg);
01238 else if (arg_match(&arg, &width, argi))
01239 cfg.g_w = arg_parse_uint(&arg);
01240 else if (arg_match(&arg, &height, argi))
01241 cfg.g_h = arg_parse_uint(&arg);
01242 else if (arg_match(&arg, &timebase, argi))
01243 cfg.g_timebase = arg_parse_rational(&arg);
01244 else if (arg_match(&arg, &error_resilient, argi))
01245 cfg.g_error_resilient = arg_parse_uint(&arg);
01246 else if (arg_match(&arg, &lag_in_frames, argi))
01247 cfg.g_lag_in_frames = arg_parse_uint(&arg);
01248 else if (arg_match(&arg, &dropframe_thresh, argi))
01249 cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
01250 else if (arg_match(&arg, &resize_allowed, argi))
01251 cfg.rc_resize_allowed = arg_parse_uint(&arg);
01252 else if (arg_match(&arg, &resize_up_thresh, argi))
01253 cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
01254 else if (arg_match(&arg, &resize_down_thresh, argi))
01255 cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
01256 else if (arg_match(&arg, &resize_down_thresh, argi))
01257 cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
01258 else if (arg_match(&arg, &end_usage, argi))
01259 cfg.rc_end_usage = arg_parse_uint(&arg);
01260 else if (arg_match(&arg, &target_bitrate, argi))
01261 cfg.rc_target_bitrate = arg_parse_uint(&arg);
01262 else if (arg_match(&arg, &min_quantizer, argi))
01263 cfg.rc_min_quantizer = arg_parse_uint(&arg);
01264 else if (arg_match(&arg, &max_quantizer, argi))
01265 cfg.rc_max_quantizer = arg_parse_uint(&arg);
01266 else if (arg_match(&arg, &undershoot_pct, argi))
01267 cfg.rc_undershoot_pct = arg_parse_uint(&arg);
01268 else if (arg_match(&arg, &overshoot_pct, argi))
01269 cfg.rc_overshoot_pct = arg_parse_uint(&arg);
01270 else if (arg_match(&arg, &buf_sz, argi))
01271 cfg.rc_buf_sz = arg_parse_uint(&arg);
01272 else if (arg_match(&arg, &buf_initial_sz, argi))
01273 cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
01274 else if (arg_match(&arg, &buf_optimal_sz, argi))
01275 cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
01276 else if (arg_match(&arg, &bias_pct, argi))
01277 {
01278 cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
01279
01280 if (arg_passes < 2)
01281 fprintf(stderr,
01282 "Warning: option %s ignored in one-pass mode.\n",
01283 arg.name);
01284 }
01285 else if (arg_match(&arg, &minsection_pct, argi))
01286 {
01287 cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
01288
01289 if (arg_passes < 2)
01290 fprintf(stderr,
01291 "Warning: option %s ignored in one-pass mode.\n",
01292 arg.name);
01293 }
01294 else if (arg_match(&arg, &maxsection_pct, argi))
01295 {
01296 cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
01297
01298 if (arg_passes < 2)
01299 fprintf(stderr,
01300 "Warning: option %s ignored in one-pass mode.\n",
01301 arg.name);
01302 }
01303 else if (arg_match(&arg, &kf_min_dist, argi))
01304 cfg.kf_min_dist = arg_parse_uint(&arg);
01305 else if (arg_match(&arg, &kf_max_dist, argi))
01306 cfg.kf_max_dist = arg_parse_uint(&arg);
01307 else if (arg_match(&arg, &kf_disabled, argi))
01308 cfg.kf_mode = VPX_KF_DISABLED;
01309 else
01310 argj++;
01311 }
01312
01313
01314 #if CONFIG_VP8_ENCODER
01315
01316 if (codec->iface == &vpx_codec_vp8_cx_algo)
01317 {
01318 ctrl_args = vp8_args;
01319 ctrl_args_map = vp8_arg_ctrl_map;
01320 }
01321
01322 #endif
01323
01324 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
01325 {
01326 int match = 0;
01327
01328 arg.argv_step = 1;
01329
01330 for (i = 0; ctrl_args[i]; i++)
01331 {
01332 if (arg_match(&arg, ctrl_args[i], argi))
01333 {
01334 match = 1;
01335
01336 if (arg_ctrl_cnt < ARG_CTRL_CNT_MAX)
01337 {
01338 arg_ctrls[arg_ctrl_cnt][0] = ctrl_args_map[i];
01339 arg_ctrls[arg_ctrl_cnt][1] = arg_parse_enum_or_int(&arg);
01340 arg_ctrl_cnt++;
01341 }
01342 }
01343 }
01344
01345 if (!match)
01346 argj++;
01347 }
01348
01349
01350 for (argi = argv; *argi; argi++)
01351 if (argi[0][0] == '-' && argi[0][1])
01352 die("Error: Unrecognized option %s\n", *argi);
01353
01354
01355 in_fn = argv[0];
01356
01357 if (!in_fn)
01358 usage_exit();
01359
01360 if(!out_fn)
01361 die("Error: Output file is required (specify with -o)\n");
01362
01363 memset(&stats, 0, sizeof(stats));
01364
01365 for (pass = one_pass_only ? one_pass_only - 1 : 0; pass < arg_passes; pass++)
01366 {
01367 int frames_in = 0, frames_out = 0;
01368 unsigned long nbytes = 0;
01369 struct detect_buffer detect;
01370
01371
01372 infile = strcmp(in_fn, "-") ? fopen(in_fn, "rb")
01373 : set_binary_mode(stdin);
01374
01375 if (!infile)
01376 {
01377 fprintf(stderr, "Failed to open input file\n");
01378 return EXIT_FAILURE;
01379 }
01380
01381
01382
01383
01384 detect.buf_read = fread(detect.buf, 1, 4, infile);
01385 detect.position = 0;
01386
01387 if (detect.buf_read == 4 && file_is_y4m(infile, &y4m, detect.buf))
01388 {
01389 if (y4m_input_open(&y4m, infile, detect.buf, 4) >= 0)
01390 {
01391 file_type = FILE_TYPE_Y4M;
01392 cfg.g_w = y4m.pic_w;
01393 cfg.g_h = y4m.pic_h;
01394
01395
01396
01397
01398 if (!arg_have_framerate)
01399 {
01400 arg_framerate.num = y4m.fps_n;
01401 arg_framerate.den = y4m.fps_d;
01402 }
01403
01404 arg_use_i420 = 0;
01405 }
01406 else
01407 {
01408 fprintf(stderr, "Unsupported Y4M stream.\n");
01409 return EXIT_FAILURE;
01410 }
01411 }
01412 else if (detect.buf_read == 4 &&
01413 file_is_ivf(infile, &fourcc, &cfg.g_w, &cfg.g_h, &detect))
01414 {
01415 file_type = FILE_TYPE_IVF;
01416 switch (fourcc)
01417 {
01418 case 0x32315659:
01419 arg_use_i420 = 0;
01420 break;
01421 case 0x30323449:
01422 arg_use_i420 = 1;
01423 break;
01424 default:
01425 fprintf(stderr, "Unsupported fourcc (%08x) in IVF\n", fourcc);
01426 return EXIT_FAILURE;
01427 }
01428 }
01429 else
01430 {
01431 file_type = FILE_TYPE_RAW;
01432 }
01433
01434 if(!cfg.g_w || !cfg.g_h)
01435 {
01436 fprintf(stderr, "Specify stream dimensions with --width (-w) "
01437 " and --height (-h).\n");
01438 return EXIT_FAILURE;
01439 }
01440
01441 #define SHOW(field) fprintf(stderr, " %-28s = %d\n", #field, cfg.field)
01442
01443 if (verbose && pass == 0)
01444 {
01445 fprintf(stderr, "Codec: %s\n", vpx_codec_iface_name(codec->iface));
01446 fprintf(stderr, "Source file: %s Format: %s\n", in_fn,
01447 arg_use_i420 ? "I420" : "YV12");
01448 fprintf(stderr, "Destination file: %s\n", out_fn);
01449 fprintf(stderr, "Encoder parameters:\n");
01450
01451 SHOW(g_usage);
01452 SHOW(g_threads);
01453 SHOW(g_profile);
01454 SHOW(g_w);
01455 SHOW(g_h);
01456 SHOW(g_timebase.num);
01457 SHOW(g_timebase.den);
01458 SHOW(g_error_resilient);
01459 SHOW(g_pass);
01460 SHOW(g_lag_in_frames);
01461 SHOW(rc_dropframe_thresh);
01462 SHOW(rc_resize_allowed);
01463 SHOW(rc_resize_up_thresh);
01464 SHOW(rc_resize_down_thresh);
01465 SHOW(rc_end_usage);
01466 SHOW(rc_target_bitrate);
01467 SHOW(rc_min_quantizer);
01468 SHOW(rc_max_quantizer);
01469 SHOW(rc_undershoot_pct);
01470 SHOW(rc_overshoot_pct);
01471 SHOW(rc_buf_sz);
01472 SHOW(rc_buf_initial_sz);
01473 SHOW(rc_buf_optimal_sz);
01474 SHOW(rc_2pass_vbr_bias_pct);
01475 SHOW(rc_2pass_vbr_minsection_pct);
01476 SHOW(rc_2pass_vbr_maxsection_pct);
01477 SHOW(kf_mode);
01478 SHOW(kf_min_dist);
01479 SHOW(kf_max_dist);
01480 }
01481
01482 if(pass == (one_pass_only ? one_pass_only - 1 : 0)) {
01483 if (file_type == FILE_TYPE_Y4M)
01484
01485
01486
01487 memset(&raw, 0, sizeof(raw));
01488 else
01489 vpx_img_alloc(&raw, arg_use_i420 ? VPX_IMG_FMT_I420 : VPX_IMG_FMT_YV12,
01490 cfg.g_w, cfg.g_h, 1);
01491 }
01492
01493 outfile = strcmp(out_fn, "-") ? fopen(out_fn, "wb")
01494 : set_binary_mode(stdout);
01495
01496 if (!outfile)
01497 {
01498 fprintf(stderr, "Failed to open output file\n");
01499 return EXIT_FAILURE;
01500 }
01501
01502 if(write_webm && fseek(outfile, 0, SEEK_CUR))
01503 {
01504 fprintf(stderr, "WebM output to pipes not supported.\n");
01505 return EXIT_FAILURE;
01506 }
01507
01508 if (stats_fn)
01509 {
01510 if (!stats_open_file(&stats, stats_fn, pass))
01511 {
01512 fprintf(stderr, "Failed to open statistics store\n");
01513 return EXIT_FAILURE;
01514 }
01515 }
01516 else
01517 {
01518 if (!stats_open_mem(&stats, pass))
01519 {
01520 fprintf(stderr, "Failed to open statistics store\n");
01521 return EXIT_FAILURE;
01522 }
01523 }
01524
01525 cfg.g_pass = arg_passes == 2
01526 ? pass ? VPX_RC_LAST_PASS : VPX_RC_FIRST_PASS
01527 : VPX_RC_ONE_PASS;
01528 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
01529
01530 if (pass)
01531 {
01532 cfg.rc_twopass_stats_in = stats_get(&stats);
01533 }
01534
01535 #endif
01536
01537 if(write_webm)
01538 {
01539 ebml.stream = outfile;
01540 write_webm_file_header(&ebml, &cfg, &arg_framerate);
01541 }
01542 else
01543 write_ivf_file_header(outfile, &cfg, codec->fourcc, 0);
01544
01545
01546
01547 vpx_codec_enc_init(&encoder, codec->iface, &cfg,
01548 show_psnr ? VPX_CODEC_USE_PSNR : 0);
01549 ctx_exit_on_error(&encoder, "Failed to initialize encoder");
01550
01551
01552
01553
01554
01555 for (i = 0; i < arg_ctrl_cnt; i++)
01556 {
01557 if (vpx_codec_control_(&encoder, arg_ctrls[i][0], arg_ctrls[i][1]))
01558 fprintf(stderr, "Error: Tried to set control %d = %d\n",
01559 arg_ctrls[i][0], arg_ctrls[i][1]);
01560
01561 ctx_exit_on_error(&encoder, "Failed to control codec");
01562 }
01563
01564 frame_avail = 1;
01565 got_data = 0;
01566
01567 while (frame_avail || got_data)
01568 {
01569 vpx_codec_iter_t iter = NULL;
01570 const vpx_codec_cx_pkt_t *pkt;
01571 struct vpx_usec_timer timer;
01572 int64_t frame_start, next_frame_start;
01573
01574 if (!arg_limit || frames_in < arg_limit)
01575 {
01576 frame_avail = read_frame(infile, &raw, file_type, &y4m,
01577 &detect);
01578
01579 if (frame_avail)
01580 frames_in++;
01581
01582 fprintf(stderr,
01583 "\rPass %d/%d frame %4d/%-4d %7ldB \033[K", pass + 1,
01584 arg_passes, frames_in, frames_out, nbytes);
01585 }
01586 else
01587 frame_avail = 0;
01588
01589 vpx_usec_timer_start(&timer);
01590
01591 frame_start = (cfg.g_timebase.den * (int64_t)(frames_in - 1)
01592 * arg_framerate.den) / cfg.g_timebase.num / arg_framerate.num;
01593 next_frame_start = (cfg.g_timebase.den * (int64_t)(frames_in)
01594 * arg_framerate.den)
01595 / cfg.g_timebase.num / arg_framerate.num;
01596 vpx_codec_encode(&encoder, frame_avail ? &raw : NULL, frame_start,
01597 next_frame_start - frame_start,
01598 0, arg_deadline);
01599 vpx_usec_timer_mark(&timer);
01600 cx_time += vpx_usec_timer_elapsed(&timer);
01601 ctx_exit_on_error(&encoder, "Failed to encode frame");
01602 got_data = 0;
01603
01604 while ((pkt = vpx_codec_get_cx_data(&encoder, &iter)))
01605 {
01606 got_data = 1;
01607
01608 switch (pkt->kind)
01609 {
01610 case VPX_CODEC_CX_FRAME_PKT:
01611 frames_out++;
01612 fprintf(stderr, " %6luF",
01613 (unsigned long)pkt->data.frame.sz);
01614
01615 if(write_webm)
01616 {
01617
01618 if(!ebml.debug)
01619 hash = murmur(pkt->data.frame.buf,
01620 pkt->data.frame.sz, hash);
01621
01622 write_webm_block(&ebml, &cfg, pkt);
01623 }
01624 else
01625 {
01626 write_ivf_frame_header(outfile, pkt);
01627 if(fwrite(pkt->data.frame.buf, 1,
01628 pkt->data.frame.sz, outfile));
01629 }
01630 nbytes += pkt->data.raw.sz;
01631 break;
01632 case VPX_CODEC_STATS_PKT:
01633 frames_out++;
01634 fprintf(stderr, " %6luS",
01635 (unsigned long)pkt->data.twopass_stats.sz);
01636 stats_write(&stats,
01637 pkt->data.twopass_stats.buf,
01638 pkt->data.twopass_stats.sz);
01639 nbytes += pkt->data.raw.sz;
01640 break;
01641 case VPX_CODEC_PSNR_PKT:
01642
01643 if (show_psnr)
01644 {
01645 int i;
01646
01647 psnr_sse_total += pkt->data.psnr.sse[0];
01648 psnr_samples_total += pkt->data.psnr.samples[0];
01649 for (i = 0; i < 4; i++)
01650 {
01651 fprintf(stderr, "%.3lf ", pkt->data.psnr.psnr[i]);
01652 psnr_totals[i] += pkt->data.psnr.psnr[i];
01653 }
01654 psnr_count++;
01655 }
01656
01657 break;
01658 default:
01659 break;
01660 }
01661 }
01662
01663 fflush(stdout);
01664 }
01665
01666 fprintf(stderr,
01667 "\rPass %d/%d frame %4d/%-4d %7ldB %7ldb/f %7"PRId64"b/s"
01668 " %7lu %s (%.2f fps)\033[K", pass + 1,
01669 arg_passes, frames_in, frames_out, nbytes, nbytes * 8 / frames_in,
01670 nbytes * 8 *(int64_t)arg_framerate.num / arg_framerate.den / frames_in,
01671 cx_time > 9999999 ? cx_time / 1000 : cx_time,
01672 cx_time > 9999999 ? "ms" : "us",
01673 (float)frames_in * 1000000.0 / (float)cx_time);
01674
01675 if ( (show_psnr) && (psnr_count>0) )
01676 {
01677 int i;
01678 double ovpsnr = vp8_mse2psnr(psnr_samples_total, 255.0,
01679 psnr_sse_total);
01680
01681 fprintf(stderr, "\nPSNR (Overall/Avg/Y/U/V)");
01682
01683 fprintf(stderr, " %.3lf", ovpsnr);
01684 for (i = 0; i < 4; i++)
01685 {
01686 fprintf(stderr, " %.3lf", psnr_totals[i]/psnr_count);
01687 }
01688 }
01689
01690 vpx_codec_destroy(&encoder);
01691
01692 fclose(infile);
01693
01694 if(write_webm)
01695 {
01696 write_webm_file_footer(&ebml, hash);
01697 }
01698 else
01699 {
01700 if (!fseek(outfile, 0, SEEK_SET))
01701 write_ivf_file_header(outfile, &cfg, codec->fourcc, frames_out);
01702 }
01703
01704 fclose(outfile);
01705 stats_close(&stats, arg_passes-1);
01706 fprintf(stderr, "\n");
01707
01708 if (one_pass_only)
01709 break;
01710 }
01711
01712 vpx_img_free(&raw);
01713 free(argv);
01714 return EXIT_SUCCESS;
01715 }