WebM VP8 Codec SDK
vpxenc
1 /*
2  * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  * Use of this source code is governed by a BSD-style license
5  * that can be found in the LICENSE file in the root of the source
6  * tree. An additional intellectual property rights grant can be found
7  * in the file PATENTS. All contributing project authors may
8  * be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 
12 /* This is a simple program that encodes YV12 files and generates ivf
13  * files using the new interface.
14  */
15 #if defined(_WIN32) || !CONFIG_OS_SUPPORT
16 #define USE_POSIX_MMAP 0
17 #else
18 #define USE_POSIX_MMAP 1
19 #endif
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <assert.h>
27 #include "vpx/vpx_encoder.h"
28 #if USE_POSIX_MMAP
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/mman.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #endif
35 #include "vpx_version.h"
36 #include "vpx/vp8cx.h"
37 #include "vpx_ports/mem_ops.h"
38 #include "vpx_ports/vpx_timer.h"
39 #include "tools_common.h"
40 #include "y4minput.h"
41 #include "libmkv/EbmlWriter.h"
42 #include "libmkv/EbmlIDs.h"
43 
44 /* Need special handling of these functions on Windows */
45 #if defined(_MSC_VER)
46 /* MSVS doesn't define off_t, and uses _f{seek,tell}i64 */
47 typedef __int64 off_t;
48 #define fseeko _fseeki64
49 #define ftello _ftelli64
50 #elif defined(_WIN32)
51 /* MinGW defines off_t, and uses f{seek,tell}o64 */
52 #define fseeko fseeko64
53 #define ftello ftello64
54 #endif
55 
56 #if defined(_MSC_VER)
57 #define LITERALU64(n) n
58 #else
59 #define LITERALU64(n) n##LLU
60 #endif
61 
62 /* We should use 32-bit file operations in WebM file format
63  * when building ARM executable file (.axf) with RVCT */
64 #if !CONFIG_OS_SUPPORT
65 typedef long off_t;
66 #define fseeko fseek
67 #define ftello ftell
68 #endif
69 
70 static const char *exec_name;
71 
72 static const struct codec_item
73 {
74  char const *name;
75  const vpx_codec_iface_t *iface;
76  unsigned int fourcc;
77 } codecs[] =
78 {
79 #if CONFIG_VP8_ENCODER
80  {"vp8", &vpx_codec_vp8_cx_algo, 0x30385056},
81 #endif
82 };
83 
84 static void usage_exit();
85 
86 void die(const char *fmt, ...)
87 {
88  va_list ap;
89  va_start(ap, fmt);
90  vfprintf(stderr, fmt, ap);
91  fprintf(stderr, "\n");
92  usage_exit();
93 }
94 
95 static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s)
96 {
97  if (ctx->err)
98  {
99  const char *detail = vpx_codec_error_detail(ctx);
100 
101  fprintf(stderr, "%s: %s\n", s, vpx_codec_error(ctx));
102 
103  if (detail)
104  fprintf(stderr, " %s\n", detail);
105 
106  exit(EXIT_FAILURE);
107  }
108 }
109 
110 /* This structure is used to abstract the different ways of handling
111  * first pass statistics.
112  */
113 typedef struct
114 {
115  vpx_fixed_buf_t buf;
116  int pass;
117  FILE *file;
118  char *buf_ptr;
119  size_t buf_alloc_sz;
120 } stats_io_t;
121 
122 int stats_open_file(stats_io_t *stats, const char *fpf, int pass)
123 {
124  int res;
125 
126  stats->pass = pass;
127 
128  if (pass == 0)
129  {
130  stats->file = fopen(fpf, "wb");
131  stats->buf.sz = 0;
132  stats->buf.buf = NULL,
133  res = (stats->file != NULL);
134  }
135  else
136  {
137 #if 0
138 #elif USE_POSIX_MMAP
139  struct stat stat_buf;
140  int fd;
141 
142  fd = open(fpf, O_RDONLY);
143  stats->file = fdopen(fd, "rb");
144  fstat(fd, &stat_buf);
145  stats->buf.sz = stat_buf.st_size;
146  stats->buf.buf = mmap(NULL, stats->buf.sz, PROT_READ, MAP_PRIVATE,
147  fd, 0);
148  res = (stats->buf.buf != NULL);
149 #else
150  size_t nbytes;
151 
152  stats->file = fopen(fpf, "rb");
153 
154  if (fseek(stats->file, 0, SEEK_END))
155  {
156  fprintf(stderr, "First-pass stats file must be seekable!\n");
157  exit(EXIT_FAILURE);
158  }
159 
160  stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file);
161  rewind(stats->file);
162 
163  stats->buf.buf = malloc(stats->buf_alloc_sz);
164 
165  if (!stats->buf.buf)
166  {
167  fprintf(stderr, "Failed to allocate first-pass stats buffer (%lu bytes)\n",
168  (unsigned long)stats->buf_alloc_sz);
169  exit(EXIT_FAILURE);
170  }
171 
172  nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
173  res = (nbytes == stats->buf.sz);
174 #endif
175  }
176 
177  return res;
178 }
179 
180 int stats_open_mem(stats_io_t *stats, int pass)
181 {
182  int res;
183  stats->pass = pass;
184 
185  if (!pass)
186  {
187  stats->buf.sz = 0;
188  stats->buf_alloc_sz = 64 * 1024;
189  stats->buf.buf = malloc(stats->buf_alloc_sz);
190  }
191 
192  stats->buf_ptr = stats->buf.buf;
193  res = (stats->buf.buf != NULL);
194  return res;
195 }
196 
197 
198 void stats_close(stats_io_t *stats, int last_pass)
199 {
200  if (stats->file)
201  {
202  if (stats->pass == last_pass)
203  {
204 #if 0
205 #elif USE_POSIX_MMAP
206  munmap(stats->buf.buf, stats->buf.sz);
207 #else
208  free(stats->buf.buf);
209 #endif
210  }
211 
212  fclose(stats->file);
213  stats->file = NULL;
214  }
215  else
216  {
217  if (stats->pass == last_pass)
218  free(stats->buf.buf);
219  }
220 }
221 
222 void stats_write(stats_io_t *stats, const void *pkt, size_t len)
223 {
224  if (stats->file)
225  {
226  if(fwrite(pkt, 1, len, stats->file));
227  }
228  else
229  {
230  if (stats->buf.sz + len > stats->buf_alloc_sz)
231  {
232  size_t new_sz = stats->buf_alloc_sz + 64 * 1024;
233  char *new_ptr = realloc(stats->buf.buf, new_sz);
234 
235  if (new_ptr)
236  {
237  stats->buf_ptr = new_ptr + (stats->buf_ptr - (char *)stats->buf.buf);
238  stats->buf.buf = new_ptr;
239  stats->buf_alloc_sz = new_sz;
240  }
241  else
242  {
243  fprintf(stderr,
244  "\nFailed to realloc firstpass stats buffer.\n");
245  exit(EXIT_FAILURE);
246  }
247  }
248 
249  memcpy(stats->buf_ptr, pkt, len);
250  stats->buf.sz += len;
251  stats->buf_ptr += len;
252  }
253 }
254 
255 vpx_fixed_buf_t stats_get(stats_io_t *stats)
256 {
257  return stats->buf;
258 }
259 
260 /* Stereo 3D packed frame format */
261 typedef enum stereo_format
262 {
263  STEREO_FORMAT_MONO = 0,
264  STEREO_FORMAT_LEFT_RIGHT = 1,
265  STEREO_FORMAT_BOTTOM_TOP = 2,
266  STEREO_FORMAT_TOP_BOTTOM = 3,
267  STEREO_FORMAT_RIGHT_LEFT = 11
268 } stereo_format_t;
269 
270 enum video_file_type
271 {
272  FILE_TYPE_RAW,
273  FILE_TYPE_IVF,
274  FILE_TYPE_Y4M
275 };
276 
277 struct detect_buffer {
278  char buf[4];
279  size_t buf_read;
280  size_t position;
281 };
282 
283 
284 #define IVF_FRAME_HDR_SZ (4+8) /* 4 byte size + 8 byte timestamp */
285 static int read_frame(FILE *f, vpx_image_t *img, unsigned int file_type,
286  y4m_input *y4m, struct detect_buffer *detect)
287 {
288  int plane = 0;
289  int shortread = 0;
290 
291  if (file_type == FILE_TYPE_Y4M)
292  {
293  if (y4m_input_fetch_frame(y4m, f, img) < 1)
294  return 0;
295  }
296  else
297  {
298  if (file_type == FILE_TYPE_IVF)
299  {
300  char junk[IVF_FRAME_HDR_SZ];
301 
302  /* Skip the frame header. We know how big the frame should be. See
303  * write_ivf_frame_header() for documentation on the frame header
304  * layout.
305  */
306  if(fread(junk, 1, IVF_FRAME_HDR_SZ, f));
307  }
308 
309  for (plane = 0; plane < 3; plane++)
310  {
311  unsigned char *ptr;
312  int w = (plane ? (1 + img->d_w) / 2 : img->d_w);
313  int h = (plane ? (1 + img->d_h) / 2 : img->d_h);
314  int r;
315 
316  /* Determine the correct plane based on the image format. The for-loop
317  * always counts in Y,U,V order, but this may not match the order of
318  * the data on disk.
319  */
320  switch (plane)
321  {
322  case 1:
323  ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12? VPX_PLANE_V : VPX_PLANE_U];
324  break;
325  case 2:
326  ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12?VPX_PLANE_U : VPX_PLANE_V];
327  break;
328  default:
329  ptr = img->planes[plane];
330  }
331 
332  for (r = 0; r < h; r++)
333  {
334  size_t needed = w;
335  size_t buf_position = 0;
336  const size_t left = detect->buf_read - detect->position;
337  if (left > 0)
338  {
339  const size_t more = (left < needed) ? left : needed;
340  memcpy(ptr, detect->buf + detect->position, more);
341  buf_position = more;
342  needed -= more;
343  detect->position += more;
344  }
345  if (needed > 0)
346  {
347  shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
348  }
349 
350  ptr += img->stride[plane];
351  }
352  }
353  }
354 
355  return !shortread;
356 }
357 
358 
359 unsigned int file_is_y4m(FILE *infile,
360  y4m_input *y4m,
361  char detect[4])
362 {
363  if(memcmp(detect, "YUV4", 4) == 0)
364  {
365  return 1;
366  }
367  return 0;
368 }
369 
370 #define IVF_FILE_HDR_SZ (32)
371 unsigned int file_is_ivf(FILE *infile,
372  unsigned int *fourcc,
373  unsigned int *width,
374  unsigned int *height,
375  struct detect_buffer *detect)
376 {
377  char raw_hdr[IVF_FILE_HDR_SZ];
378  int is_ivf = 0;
379 
380  if(memcmp(detect->buf, "DKIF", 4) != 0)
381  return 0;
382 
383  /* See write_ivf_file_header() for more documentation on the file header
384  * layout.
385  */
386  if (fread(raw_hdr + 4, 1, IVF_FILE_HDR_SZ - 4, infile)
387  == IVF_FILE_HDR_SZ - 4)
388  {
389  {
390  is_ivf = 1;
391 
392  if (mem_get_le16(raw_hdr + 4) != 0)
393  fprintf(stderr, "Error: Unrecognized IVF version! This file may not"
394  " decode properly.");
395 
396  *fourcc = mem_get_le32(raw_hdr + 8);
397  }
398  }
399 
400  if (is_ivf)
401  {
402  *width = mem_get_le16(raw_hdr + 12);
403  *height = mem_get_le16(raw_hdr + 14);
404  detect->position = 4;
405  }
406 
407  return is_ivf;
408 }
409 
410 
411 static void write_ivf_file_header(FILE *outfile,
412  const vpx_codec_enc_cfg_t *cfg,
413  unsigned int fourcc,
414  int frame_cnt)
415 {
416  char header[32];
417 
418  if (cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS)
419  return;
420 
421  header[0] = 'D';
422  header[1] = 'K';
423  header[2] = 'I';
424  header[3] = 'F';
425  mem_put_le16(header + 4, 0); /* version */
426  mem_put_le16(header + 6, 32); /* headersize */
427  mem_put_le32(header + 8, fourcc); /* headersize */
428  mem_put_le16(header + 12, cfg->g_w); /* width */
429  mem_put_le16(header + 14, cfg->g_h); /* height */
430  mem_put_le32(header + 16, cfg->g_timebase.den); /* rate */
431  mem_put_le32(header + 20, cfg->g_timebase.num); /* scale */
432  mem_put_le32(header + 24, frame_cnt); /* length */
433  mem_put_le32(header + 28, 0); /* unused */
434 
435  if(fwrite(header, 1, 32, outfile));
436 }
437 
438 
439 static void write_ivf_frame_header(FILE *outfile,
440  const vpx_codec_cx_pkt_t *pkt)
441 {
442  char header[12];
443  vpx_codec_pts_t pts;
444 
445  if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
446  return;
447 
448  pts = pkt->data.frame.pts;
449  mem_put_le32(header, pkt->data.frame.sz);
450  mem_put_le32(header + 4, pts & 0xFFFFFFFF);
451  mem_put_le32(header + 8, pts >> 32);
452 
453  if(fwrite(header, 1, 12, outfile));
454 }
455 
456 
457 typedef off_t EbmlLoc;
458 
459 
460 struct cue_entry
461 {
462  unsigned int time;
463  uint64_t loc;
464 };
465 
466 
467 struct EbmlGlobal
468 {
469  int debug;
470 
471  FILE *stream;
472  int64_t last_pts_ms;
473  vpx_rational_t framerate;
474 
475  /* These pointers are to the start of an element */
476  off_t position_reference;
477  off_t seek_info_pos;
478  off_t segment_info_pos;
479  off_t track_pos;
480  off_t cue_pos;
481  off_t cluster_pos;
482 
483  /* This pointer is to a specific element to be serialized */
484  off_t track_id_pos;
485 
486  /* These pointers are to the size field of the element */
487  EbmlLoc startSegment;
488  EbmlLoc startCluster;
489 
490  uint32_t cluster_timecode;
491  int cluster_open;
492 
493  struct cue_entry *cue_list;
494  unsigned int cues;
495 
496 };
497 
498 
499 void Ebml_Write(EbmlGlobal *glob, const void *buffer_in, unsigned long len)
500 {
501  if(fwrite(buffer_in, 1, len, glob->stream));
502 }
503 
504 #define WRITE_BUFFER(s) \
505 for(i = len-1; i>=0; i--)\
506 { \
507  x = *(const s *)buffer_in >> (i * CHAR_BIT); \
508  Ebml_Write(glob, &x, 1); \
509 }
510 void Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, int buffer_size, unsigned long len)
511 {
512  char x;
513  int i;
514 
515  /* buffer_size:
516  * 1 - int8_t;
517  * 2 - int16_t;
518  * 3 - int32_t;
519  * 4 - int64_t;
520  */
521  switch (buffer_size)
522  {
523  case 1:
524  WRITE_BUFFER(int8_t)
525  break;
526  case 2:
527  WRITE_BUFFER(int16_t)
528  break;
529  case 4:
530  WRITE_BUFFER(int32_t)
531  break;
532  case 8:
533  WRITE_BUFFER(int64_t)
534  break;
535  default:
536  break;
537  }
538 }
539 #undef WRITE_BUFFER
540 
541 /* Need a fixed size serializer for the track ID. libmkv provides a 64 bit
542  * one, but not a 32 bit one.
543  */
544 static void Ebml_SerializeUnsigned32(EbmlGlobal *glob, unsigned long class_id, uint64_t ui)
545 {
546  unsigned char sizeSerialized = 4 | 0x80;
547  Ebml_WriteID(glob, class_id);
548  Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1);
549  Ebml_Serialize(glob, &ui, sizeof(ui), 4);
550 }
551 
552 
553 static void
554 Ebml_StartSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc,
555  unsigned long class_id)
556 {
557  //todo this is always taking 8 bytes, this may need later optimization
558  //this is a key that says length unknown
559  uint64_t unknownLen = LITERALU64(0x01FFFFFFFFFFFFFF);
560 
561  Ebml_WriteID(glob, class_id);
562  *ebmlLoc = ftello(glob->stream);
563  Ebml_Serialize(glob, &unknownLen, sizeof(unknownLen), 8);
564 }
565 
566 static void
567 Ebml_EndSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc)
568 {
569  off_t pos;
570  uint64_t size;
571 
572  /* Save the current stream pointer */
573  pos = ftello(glob->stream);
574 
575  /* Calculate the size of this element */
576  size = pos - *ebmlLoc - 8;
577  size |= LITERALU64(0x0100000000000000);
578 
579  /* Seek back to the beginning of the element and write the new size */
580  fseeko(glob->stream, *ebmlLoc, SEEK_SET);
581  Ebml_Serialize(glob, &size, sizeof(size), 8);
582 
583  /* Reset the stream pointer */
584  fseeko(glob->stream, pos, SEEK_SET);
585 }
586 
587 
588 static void
589 write_webm_seek_element(EbmlGlobal *ebml, unsigned long id, off_t pos)
590 {
591  uint64_t offset = pos - ebml->position_reference;
592  EbmlLoc start;
593  Ebml_StartSubElement(ebml, &start, Seek);
594  Ebml_SerializeBinary(ebml, SeekID, id);
595  Ebml_SerializeUnsigned64(ebml, SeekPosition, offset);
596  Ebml_EndSubElement(ebml, &start);
597 }
598 
599 
600 static void
601 write_webm_seek_info(EbmlGlobal *ebml)
602 {
603 
604  off_t pos;
605 
606  /* Save the current stream pointer */
607  pos = ftello(ebml->stream);
608 
609  if(ebml->seek_info_pos)
610  fseeko(ebml->stream, ebml->seek_info_pos, SEEK_SET);
611  else
612  ebml->seek_info_pos = pos;
613 
614  {
615  EbmlLoc start;
616 
617  Ebml_StartSubElement(ebml, &start, SeekHead);
618  write_webm_seek_element(ebml, Tracks, ebml->track_pos);
619  write_webm_seek_element(ebml, Cues, ebml->cue_pos);
620  write_webm_seek_element(ebml, Info, ebml->segment_info_pos);
621  Ebml_EndSubElement(ebml, &start);
622  }
623  {
624  //segment info
625  EbmlLoc startInfo;
626  uint64_t frame_time;
627 
628  frame_time = (uint64_t)1000 * ebml->framerate.den
629  / ebml->framerate.num;
630  ebml->segment_info_pos = ftello(ebml->stream);
631  Ebml_StartSubElement(ebml, &startInfo, Info);
632  Ebml_SerializeUnsigned(ebml, TimecodeScale, 1000000);
633  Ebml_SerializeFloat(ebml, Segment_Duration,
634  ebml->last_pts_ms + frame_time);
635  Ebml_SerializeString(ebml, 0x4D80,
636  ebml->debug ? "vpxenc" : "vpxenc" VERSION_STRING);
637  Ebml_SerializeString(ebml, 0x5741,
638  ebml->debug ? "vpxenc" : "vpxenc" VERSION_STRING);
639  Ebml_EndSubElement(ebml, &startInfo);
640  }
641 }
642 
643 
644 static void
645 write_webm_file_header(EbmlGlobal *glob,
646  const vpx_codec_enc_cfg_t *cfg,
647  const struct vpx_rational *fps,
648  stereo_format_t stereo_fmt)
649 {
650  {
651  EbmlLoc start;
652  Ebml_StartSubElement(glob, &start, EBML);
653  Ebml_SerializeUnsigned(glob, EBMLVersion, 1);
654  Ebml_SerializeUnsigned(glob, EBMLReadVersion, 1); //EBML Read Version
655  Ebml_SerializeUnsigned(glob, EBMLMaxIDLength, 4); //EBML Max ID Length
656  Ebml_SerializeUnsigned(glob, EBMLMaxSizeLength, 8); //EBML Max Size Length
657  Ebml_SerializeString(glob, DocType, "webm"); //Doc Type
658  Ebml_SerializeUnsigned(glob, DocTypeVersion, 2); //Doc Type Version
659  Ebml_SerializeUnsigned(glob, DocTypeReadVersion, 2); //Doc Type Read Version
660  Ebml_EndSubElement(glob, &start);
661  }
662  {
663  Ebml_StartSubElement(glob, &glob->startSegment, Segment); //segment
664  glob->position_reference = ftello(glob->stream);
665  glob->framerate = *fps;
666  write_webm_seek_info(glob);
667 
668  {
669  EbmlLoc trackStart;
670  glob->track_pos = ftello(glob->stream);
671  Ebml_StartSubElement(glob, &trackStart, Tracks);
672  {
673  unsigned int trackNumber = 1;
674  uint64_t trackID = 0;
675 
676  EbmlLoc start;
677  Ebml_StartSubElement(glob, &start, TrackEntry);
678  Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
679  glob->track_id_pos = ftello(glob->stream);
680  Ebml_SerializeUnsigned32(glob, TrackUID, trackID);
681  Ebml_SerializeUnsigned(glob, TrackType, 1); //video is always 1
682  Ebml_SerializeString(glob, CodecID, "V_VP8");
683  {
684  unsigned int pixelWidth = cfg->g_w;
685  unsigned int pixelHeight = cfg->g_h;
686  float frameRate = (float)fps->num/(float)fps->den;
687 
688  EbmlLoc videoStart;
689  Ebml_StartSubElement(glob, &videoStart, Video);
690  Ebml_SerializeUnsigned(glob, PixelWidth, pixelWidth);
691  Ebml_SerializeUnsigned(glob, PixelHeight, pixelHeight);
692  Ebml_SerializeUnsigned(glob, StereoMode, stereo_fmt);
693  Ebml_SerializeFloat(glob, FrameRate, frameRate);
694  Ebml_EndSubElement(glob, &videoStart); //Video
695  }
696  Ebml_EndSubElement(glob, &start); //Track Entry
697  }
698  Ebml_EndSubElement(glob, &trackStart);
699  }
700  // segment element is open
701  }
702 }
703 
704 
705 static void
706 write_webm_block(EbmlGlobal *glob,
707  const vpx_codec_enc_cfg_t *cfg,
708  const vpx_codec_cx_pkt_t *pkt)
709 {
710  unsigned long block_length;
711  unsigned char track_number;
712  unsigned short block_timecode = 0;
713  unsigned char flags;
714  int64_t pts_ms;
715  int start_cluster = 0, is_keyframe;
716 
717  /* Calculate the PTS of this frame in milliseconds */
718  pts_ms = pkt->data.frame.pts * 1000
719  * (uint64_t)cfg->g_timebase.num / (uint64_t)cfg->g_timebase.den;
720  if(pts_ms <= glob->last_pts_ms)
721  pts_ms = glob->last_pts_ms + 1;
722  glob->last_pts_ms = pts_ms;
723 
724  /* Calculate the relative time of this block */
725  if(pts_ms - glob->cluster_timecode > SHRT_MAX)
726  start_cluster = 1;
727  else
728  block_timecode = pts_ms - glob->cluster_timecode;
729 
730  is_keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY);
731  if(start_cluster || is_keyframe)
732  {
733  if(glob->cluster_open)
734  Ebml_EndSubElement(glob, &glob->startCluster);
735 
736  /* Open the new cluster */
737  block_timecode = 0;
738  glob->cluster_open = 1;
739  glob->cluster_timecode = pts_ms;
740  glob->cluster_pos = ftello(glob->stream);
741  Ebml_StartSubElement(glob, &glob->startCluster, Cluster); //cluster
742  Ebml_SerializeUnsigned(glob, Timecode, glob->cluster_timecode);
743 
744  /* Save a cue point if this is a keyframe. */
745  if(is_keyframe)
746  {
747  struct cue_entry *cue, *new_cue_list;
748 
749  new_cue_list = realloc(glob->cue_list,
750  (glob->cues+1) * sizeof(struct cue_entry));
751  if(new_cue_list)
752  glob->cue_list = new_cue_list;
753  else
754  {
755  fprintf(stderr, "\nFailed to realloc cue list.\n");
756  exit(EXIT_FAILURE);
757  }
758 
759  cue = &glob->cue_list[glob->cues];
760  cue->time = glob->cluster_timecode;
761  cue->loc = glob->cluster_pos;
762  glob->cues++;
763  }
764  }
765 
766  /* Write the Simple Block */
767  Ebml_WriteID(glob, SimpleBlock);
768 
769  block_length = pkt->data.frame.sz + 4;
770  block_length |= 0x10000000;
771  Ebml_Serialize(glob, &block_length, sizeof(block_length), 4);
772 
773  track_number = 1;
774  track_number |= 0x80;
775  Ebml_Write(glob, &track_number, 1);
776 
777  Ebml_Serialize(glob, &block_timecode, sizeof(block_timecode), 2);
778 
779  flags = 0;
780  if(is_keyframe)
781  flags |= 0x80;
782  if(pkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE)
783  flags |= 0x08;
784  Ebml_Write(glob, &flags, 1);
785 
786  Ebml_Write(glob, pkt->data.frame.buf, pkt->data.frame.sz);
787 }
788 
789 
790 static void
791 write_webm_file_footer(EbmlGlobal *glob, long hash)
792 {
793 
794  if(glob->cluster_open)
795  Ebml_EndSubElement(glob, &glob->startCluster);
796 
797  {
798  EbmlLoc start;
799  int i;
800 
801  glob->cue_pos = ftello(glob->stream);
802  Ebml_StartSubElement(glob, &start, Cues);
803  for(i=0; i<glob->cues; i++)
804  {
805  struct cue_entry *cue = &glob->cue_list[i];
806  EbmlLoc start;
807 
808  Ebml_StartSubElement(glob, &start, CuePoint);
809  {
810  EbmlLoc start;
811 
812  Ebml_SerializeUnsigned(glob, CueTime, cue->time);
813 
814  Ebml_StartSubElement(glob, &start, CueTrackPositions);
815  Ebml_SerializeUnsigned(glob, CueTrack, 1);
816  Ebml_SerializeUnsigned64(glob, CueClusterPosition,
817  cue->loc - glob->position_reference);
818  //Ebml_SerializeUnsigned(glob, CueBlockNumber, cue->blockNumber);
819  Ebml_EndSubElement(glob, &start);
820  }
821  Ebml_EndSubElement(glob, &start);
822  }
823  Ebml_EndSubElement(glob, &start);
824  }
825 
826  Ebml_EndSubElement(glob, &glob->startSegment);
827 
828  /* Patch up the seek info block */
829  write_webm_seek_info(glob);
830 
831  /* Patch up the track id */
832  fseeko(glob->stream, glob->track_id_pos, SEEK_SET);
833  Ebml_SerializeUnsigned32(glob, TrackUID, glob->debug ? 0xDEADBEEF : hash);
834 
835  fseeko(glob->stream, 0, SEEK_END);
836 }
837 
838 
839 /* Murmur hash derived from public domain reference implementation at
840  * http://sites.google.com/site/murmurhash/
841  */
842 static unsigned int murmur ( const void * key, int len, unsigned int seed )
843 {
844  const unsigned int m = 0x5bd1e995;
845  const int r = 24;
846 
847  unsigned int h = seed ^ len;
848 
849  const unsigned char * data = (const unsigned char *)key;
850 
851  while(len >= 4)
852  {
853  unsigned int k;
854 
855  k = data[0];
856  k |= data[1] << 8;
857  k |= data[2] << 16;
858  k |= data[3] << 24;
859 
860  k *= m;
861  k ^= k >> r;
862  k *= m;
863 
864  h *= m;
865  h ^= k;
866 
867  data += 4;
868  len -= 4;
869  }
870 
871  switch(len)
872  {
873  case 3: h ^= data[2] << 16;
874  case 2: h ^= data[1] << 8;
875  case 1: h ^= data[0];
876  h *= m;
877  };
878 
879  h ^= h >> 13;
880  h *= m;
881  h ^= h >> 15;
882 
883  return h;
884 }
885 
886 #include "math.h"
887 
888 static double vp8_mse2psnr(double Samples, double Peak, double Mse)
889 {
890  double psnr;
891 
892  if ((double)Mse > 0.0)
893  psnr = 10.0 * log10(Peak * Peak * Samples / Mse);
894  else
895  psnr = 60; // Limit to prevent / 0
896 
897  if (psnr > 60)
898  psnr = 60;
899 
900  return psnr;
901 }
902 
903 
904 #include "args.h"
905 
906 static const arg_def_t debugmode = ARG_DEF("D", "debug", 0,
907  "Debug mode (makes output deterministic)");
908 static const arg_def_t outputfile = ARG_DEF("o", "output", 1,
909  "Output filename");
910 static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0,
911  "Input file is YV12 ");
912 static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0,
913  "Input file is I420 (default)");
914 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1,
915  "Codec to use");
916 static const arg_def_t passes = ARG_DEF("p", "passes", 1,
917  "Number of passes (1/2)");
918 static const arg_def_t pass_arg = ARG_DEF(NULL, "pass", 1,
919  "Pass to execute (1/2)");
920 static const arg_def_t fpf_name = ARG_DEF(NULL, "fpf", 1,
921  "First pass statistics file name");
922 static const arg_def_t limit = ARG_DEF(NULL, "limit", 1,
923  "Stop encoding after n input frames");
924 static const arg_def_t deadline = ARG_DEF("d", "deadline", 1,
925  "Deadline per frame (usec)");
926 static const arg_def_t best_dl = ARG_DEF(NULL, "best", 0,
927  "Use Best Quality Deadline");
928 static const arg_def_t good_dl = ARG_DEF(NULL, "good", 0,
929  "Use Good Quality Deadline");
930 static const arg_def_t rt_dl = ARG_DEF(NULL, "rt", 0,
931  "Use Realtime Quality Deadline");
932 static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0,
933  "Show encoder parameters");
934 static const arg_def_t psnrarg = ARG_DEF(NULL, "psnr", 0,
935  "Show PSNR in status line");
936 static const arg_def_t framerate = ARG_DEF(NULL, "fps", 1,
937  "Stream frame rate (rate/scale)");
938 static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0,
939  "Output IVF (default is WebM)");
940 static const arg_def_t q_hist_n = ARG_DEF(NULL, "q-hist", 1,
941  "Show quantizer histogram (n-buckets)");
942 static const arg_def_t rate_hist_n = ARG_DEF(NULL, "rate-hist", 1,
943  "Show rate histogram (n-buckets)");
944 static const arg_def_t *main_args[] =
945 {
946  &debugmode,
947  &outputfile, &codecarg, &passes, &pass_arg, &fpf_name, &limit, &deadline,
948  &best_dl, &good_dl, &rt_dl,
949  &verbosearg, &psnrarg, &use_ivf, &q_hist_n, &rate_hist_n,
950  NULL
951 };
952 
953 static const arg_def_t usage = ARG_DEF("u", "usage", 1,
954  "Usage profile number to use");
955 static const arg_def_t threads = ARG_DEF("t", "threads", 1,
956  "Max number of threads to use");
957 static const arg_def_t profile = ARG_DEF(NULL, "profile", 1,
958  "Bitstream profile number to use");
959 static const arg_def_t width = ARG_DEF("w", "width", 1,
960  "Frame width");
961 static const arg_def_t height = ARG_DEF("h", "height", 1,
962  "Frame height");
963 static const struct arg_enum_list stereo_mode_enum[] = {
964  {"mono" , STEREO_FORMAT_MONO},
965  {"left-right", STEREO_FORMAT_LEFT_RIGHT},
966  {"bottom-top", STEREO_FORMAT_BOTTOM_TOP},
967  {"top-bottom", STEREO_FORMAT_TOP_BOTTOM},
968  {"right-left", STEREO_FORMAT_RIGHT_LEFT},
969  {NULL, 0}
970 };
971 static const arg_def_t stereo_mode = ARG_DEF_ENUM(NULL, "stereo-mode", 1,
972  "Stereo 3D video format", stereo_mode_enum);
973 static const arg_def_t timebase = ARG_DEF(NULL, "timebase", 1,
974  "Output timestamp precision (fractional seconds)");
975 static const arg_def_t error_resilient = ARG_DEF(NULL, "error-resilient", 1,
976  "Enable error resiliency features");
977 static const arg_def_t lag_in_frames = ARG_DEF(NULL, "lag-in-frames", 1,
978  "Max number of frames to lag");
979 
980 static const arg_def_t *global_args[] =
981 {
982  &use_yv12, &use_i420, &usage, &threads, &profile,
983  &width, &height, &stereo_mode, &timebase, &framerate, &error_resilient,
984  &lag_in_frames, NULL
985 };
986 
987 static const arg_def_t dropframe_thresh = ARG_DEF(NULL, "drop-frame", 1,
988  "Temporal resampling threshold (buf %)");
989 static const arg_def_t resize_allowed = ARG_DEF(NULL, "resize-allowed", 1,
990  "Spatial resampling enabled (bool)");
991 static const arg_def_t resize_up_thresh = ARG_DEF(NULL, "resize-up", 1,
992  "Upscale threshold (buf %)");
993 static const arg_def_t resize_down_thresh = ARG_DEF(NULL, "resize-down", 1,
994  "Downscale threshold (buf %)");
995 static const struct arg_enum_list end_usage_enum[] = {
996  {"vbr", VPX_VBR},
997  {"cbr", VPX_CBR},
998  {"cq", VPX_CQ},
999  {NULL, 0}
1000 };
1001 static const arg_def_t end_usage = ARG_DEF_ENUM(NULL, "end-usage", 1,
1002  "Rate control mode", end_usage_enum);
1003 static const arg_def_t target_bitrate = ARG_DEF(NULL, "target-bitrate", 1,
1004  "Bitrate (kbps)");
1005 static const arg_def_t min_quantizer = ARG_DEF(NULL, "min-q", 1,
1006  "Minimum (best) quantizer");
1007 static const arg_def_t max_quantizer = ARG_DEF(NULL, "max-q", 1,
1008  "Maximum (worst) quantizer");
1009 static const arg_def_t undershoot_pct = ARG_DEF(NULL, "undershoot-pct", 1,
1010  "Datarate undershoot (min) target (%)");
1011 static const arg_def_t overshoot_pct = ARG_DEF(NULL, "overshoot-pct", 1,
1012  "Datarate overshoot (max) target (%)");
1013 static const arg_def_t buf_sz = ARG_DEF(NULL, "buf-sz", 1,
1014  "Client buffer size (ms)");
1015 static const arg_def_t buf_initial_sz = ARG_DEF(NULL, "buf-initial-sz", 1,
1016  "Client initial buffer size (ms)");
1017 static const arg_def_t buf_optimal_sz = ARG_DEF(NULL, "buf-optimal-sz", 1,
1018  "Client optimal buffer size (ms)");
1019 static const arg_def_t *rc_args[] =
1020 {
1021  &dropframe_thresh, &resize_allowed, &resize_up_thresh, &resize_down_thresh,
1022  &end_usage, &target_bitrate, &min_quantizer, &max_quantizer,
1023  &undershoot_pct, &overshoot_pct, &buf_sz, &buf_initial_sz, &buf_optimal_sz,
1024  NULL
1025 };
1026 
1027 
1028 static const arg_def_t bias_pct = ARG_DEF(NULL, "bias-pct", 1,
1029  "CBR/VBR bias (0=CBR, 100=VBR)");
1030 static const arg_def_t minsection_pct = ARG_DEF(NULL, "minsection-pct", 1,
1031  "GOP min bitrate (% of target)");
1032 static const arg_def_t maxsection_pct = ARG_DEF(NULL, "maxsection-pct", 1,
1033  "GOP max bitrate (% of target)");
1034 static const arg_def_t *rc_twopass_args[] =
1035 {
1036  &bias_pct, &minsection_pct, &maxsection_pct, NULL
1037 };
1038 
1039 
1040 static const arg_def_t kf_min_dist = ARG_DEF(NULL, "kf-min-dist", 1,
1041  "Minimum keyframe interval (frames)");
1042 static const arg_def_t kf_max_dist = ARG_DEF(NULL, "kf-max-dist", 1,
1043  "Maximum keyframe interval (frames)");
1044 static const arg_def_t kf_disabled = ARG_DEF(NULL, "disable-kf", 0,
1045  "Disable keyframe placement");
1046 static const arg_def_t *kf_args[] =
1047 {
1048  &kf_min_dist, &kf_max_dist, &kf_disabled, NULL
1049 };
1050 
1051 
1052 #if CONFIG_VP8_ENCODER
1053 static const arg_def_t noise_sens = ARG_DEF(NULL, "noise-sensitivity", 1,
1054  "Noise sensitivity (frames to blur)");
1055 static const arg_def_t sharpness = ARG_DEF(NULL, "sharpness", 1,
1056  "Filter sharpness (0-7)");
1057 static const arg_def_t static_thresh = ARG_DEF(NULL, "static-thresh", 1,
1058  "Motion detection threshold");
1059 #endif
1060 
1061 #if CONFIG_VP8_ENCODER
1062 static const arg_def_t cpu_used = ARG_DEF(NULL, "cpu-used", 1,
1063  "CPU Used (-16..16)");
1064 #endif
1065 
1066 
1067 #if CONFIG_VP8_ENCODER
1068 static const arg_def_t token_parts = ARG_DEF(NULL, "token-parts", 1,
1069  "Number of token partitions to use, log2");
1070 static const arg_def_t auto_altref = ARG_DEF(NULL, "auto-alt-ref", 1,
1071  "Enable automatic alt reference frames");
1072 static const arg_def_t arnr_maxframes = ARG_DEF(NULL, "arnr-maxframes", 1,
1073  "AltRef Max Frames");
1074 static const arg_def_t arnr_strength = ARG_DEF(NULL, "arnr-strength", 1,
1075  "AltRef Strength");
1076 static const arg_def_t arnr_type = ARG_DEF(NULL, "arnr-type", 1,
1077  "AltRef Type");
1078 static const struct arg_enum_list tuning_enum[] = {
1079  {"psnr", VP8_TUNE_PSNR},
1080  {"ssim", VP8_TUNE_SSIM},
1081  {NULL, 0}
1082 };
1083 static const arg_def_t tune_ssim = ARG_DEF_ENUM(NULL, "tune", 1,
1084  "Material to favor", tuning_enum);
1085 static const arg_def_t cq_level = ARG_DEF(NULL, "cq-level", 1,
1086  "Constrained Quality Level");
1087 static const arg_def_t max_intra_rate_pct = ARG_DEF(NULL, "max-intra-rate", 1,
1088  "Max I-frame bitrate (pct)");
1089 
1090 static const arg_def_t *vp8_args[] =
1091 {
1092  &cpu_used, &auto_altref, &noise_sens, &sharpness, &static_thresh,
1093  &token_parts, &arnr_maxframes, &arnr_strength, &arnr_type,
1094  &tune_ssim, &cq_level, &max_intra_rate_pct, NULL
1095 };
1096 static const int vp8_arg_ctrl_map[] =
1097 {
1103 };
1104 #endif
1105 
1106 static const arg_def_t *no_args[] = { NULL };
1107 
1108 static void usage_exit()
1109 {
1110  int i;
1111 
1112  fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
1113  exec_name);
1114 
1115  fprintf(stderr, "\nOptions:\n");
1116  arg_show_usage(stdout, main_args);
1117  fprintf(stderr, "\nEncoder Global Options:\n");
1118  arg_show_usage(stdout, global_args);
1119  fprintf(stderr, "\nRate Control Options:\n");
1120  arg_show_usage(stdout, rc_args);
1121  fprintf(stderr, "\nTwopass Rate Control Options:\n");
1122  arg_show_usage(stdout, rc_twopass_args);
1123  fprintf(stderr, "\nKeyframe Placement Options:\n");
1124  arg_show_usage(stdout, kf_args);
1125 #if CONFIG_VP8_ENCODER
1126  fprintf(stderr, "\nVP8 Specific Options:\n");
1127  arg_show_usage(stdout, vp8_args);
1128 #endif
1129  fprintf(stderr, "\nStream timebase (--timebase):\n"
1130  " The desired precision of timestamps in the output, expressed\n"
1131  " in fractional seconds. Default is 1/1000.\n");
1132  fprintf(stderr, "\n"
1133  "Included encoders:\n"
1134  "\n");
1135 
1136  for (i = 0; i < sizeof(codecs) / sizeof(codecs[0]); i++)
1137  fprintf(stderr, " %-6s - %s\n",
1138  codecs[i].name,
1139  vpx_codec_iface_name(codecs[i].iface));
1140 
1141  exit(EXIT_FAILURE);
1142 }
1143 
1144 
1145 #define HIST_BAR_MAX 40
1146 struct hist_bucket
1147 {
1148  int low, high, count;
1149 };
1150 
1151 
1152 static int merge_hist_buckets(struct hist_bucket *bucket,
1153  int *buckets_,
1154  int max_buckets)
1155 {
1156  int small_bucket = 0, merge_bucket = INT_MAX, big_bucket=0;
1157  int buckets = *buckets_;
1158  int i;
1159 
1160  /* Find the extrema for this list of buckets */
1161  big_bucket = small_bucket = 0;
1162  for(i=0; i < buckets; i++)
1163  {
1164  if(bucket[i].count < bucket[small_bucket].count)
1165  small_bucket = i;
1166  if(bucket[i].count > bucket[big_bucket].count)
1167  big_bucket = i;
1168  }
1169 
1170  /* If we have too many buckets, merge the smallest with an adjacent
1171  * bucket.
1172  */
1173  while(buckets > max_buckets)
1174  {
1175  int last_bucket = buckets - 1;
1176 
1177  // merge the small bucket with an adjacent one.
1178  if(small_bucket == 0)
1179  merge_bucket = 1;
1180  else if(small_bucket == last_bucket)
1181  merge_bucket = last_bucket - 1;
1182  else if(bucket[small_bucket - 1].count < bucket[small_bucket + 1].count)
1183  merge_bucket = small_bucket - 1;
1184  else
1185  merge_bucket = small_bucket + 1;
1186 
1187  assert(abs(merge_bucket - small_bucket) <= 1);
1188  assert(small_bucket < buckets);
1189  assert(big_bucket < buckets);
1190  assert(merge_bucket < buckets);
1191 
1192  if(merge_bucket < small_bucket)
1193  {
1194  bucket[merge_bucket].high = bucket[small_bucket].high;
1195  bucket[merge_bucket].count += bucket[small_bucket].count;
1196  }
1197  else
1198  {
1199  bucket[small_bucket].high = bucket[merge_bucket].high;
1200  bucket[small_bucket].count += bucket[merge_bucket].count;
1201  merge_bucket = small_bucket;
1202  }
1203 
1204  assert(bucket[merge_bucket].low != bucket[merge_bucket].high);
1205 
1206  buckets--;
1207 
1208  /* Remove the merge_bucket from the list, and find the new small
1209  * and big buckets while we're at it
1210  */
1211  big_bucket = small_bucket = 0;
1212  for(i=0; i < buckets; i++)
1213  {
1214  if(i > merge_bucket)
1215  bucket[i] = bucket[i+1];
1216 
1217  if(bucket[i].count < bucket[small_bucket].count)
1218  small_bucket = i;
1219  if(bucket[i].count > bucket[big_bucket].count)
1220  big_bucket = i;
1221  }
1222 
1223  }
1224 
1225  *buckets_ = buckets;
1226  return bucket[big_bucket].count;
1227 }
1228 
1229 
1230 static void show_histogram(const struct hist_bucket *bucket,
1231  int buckets,
1232  int total,
1233  int scale)
1234 {
1235  const char *pat1, *pat2;
1236  int i;
1237 
1238  switch((int)(log(bucket[buckets-1].high)/log(10))+1)
1239  {
1240  case 1:
1241  case 2:
1242  pat1 = "%4d %2s: ";
1243  pat2 = "%4d-%2d: ";
1244  break;
1245  case 3:
1246  pat1 = "%5d %3s: ";
1247  pat2 = "%5d-%3d: ";
1248  break;
1249  case 4:
1250  pat1 = "%6d %4s: ";
1251  pat2 = "%6d-%4d: ";
1252  break;
1253  case 5:
1254  pat1 = "%7d %5s: ";
1255  pat2 = "%7d-%5d: ";
1256  break;
1257  case 6:
1258  pat1 = "%8d %6s: ";
1259  pat2 = "%8d-%6d: ";
1260  break;
1261  case 7:
1262  pat1 = "%9d %7s: ";
1263  pat2 = "%9d-%7d: ";
1264  break;
1265  default:
1266  pat1 = "%12d %10s: ";
1267  pat2 = "%12d-%10d: ";
1268  break;
1269  }
1270 
1271  for(i=0; i<buckets; i++)
1272  {
1273  int len;
1274  int j;
1275  float pct;
1276 
1277  pct = 100.0 * (float)bucket[i].count / (float)total;
1278  len = HIST_BAR_MAX * bucket[i].count / scale;
1279  if(len < 1)
1280  len = 1;
1281  assert(len <= HIST_BAR_MAX);
1282 
1283  if(bucket[i].low == bucket[i].high)
1284  fprintf(stderr, pat1, bucket[i].low, "");
1285  else
1286  fprintf(stderr, pat2, bucket[i].low, bucket[i].high);
1287 
1288  for(j=0; j<HIST_BAR_MAX; j++)
1289  fprintf(stderr, j<len?"=":" ");
1290  fprintf(stderr, "\t%5d (%6.2f%%)\n",bucket[i].count,pct);
1291  }
1292 }
1293 
1294 
1295 static void show_q_histogram(const int counts[64], int max_buckets)
1296 {
1297  struct hist_bucket bucket[64];
1298  int buckets = 0;
1299  int total = 0;
1300  int scale;
1301  int i;
1302 
1303 
1304  for(i=0; i<64; i++)
1305  {
1306  if(counts[i])
1307  {
1308  bucket[buckets].low = bucket[buckets].high = i;
1309  bucket[buckets].count = counts[i];
1310  buckets++;
1311  total += counts[i];
1312  }
1313  }
1314 
1315  fprintf(stderr, "\nQuantizer Selection:\n");
1316  scale = merge_hist_buckets(bucket, &buckets, max_buckets);
1317  show_histogram(bucket, buckets, total, scale);
1318 }
1319 
1320 
1321 #define RATE_BINS (100)
1322 struct rate_hist
1323 {
1324  int64_t *pts;
1325  int *sz;
1326  int samples;
1327  int frames;
1328  struct hist_bucket bucket[RATE_BINS];
1329  int total;
1330 };
1331 
1332 
1333 static void init_rate_histogram(struct rate_hist *hist,
1334  const vpx_codec_enc_cfg_t *cfg,
1335  const vpx_rational_t *fps)
1336 {
1337  int i;
1338 
1339  /* Determine the number of samples in the buffer. Use the file's framerate
1340  * to determine the number of frames in rc_buf_sz milliseconds, with an
1341  * adjustment (5/4) to account for alt-refs
1342  */
1343  hist->samples = cfg->rc_buf_sz * 5 / 4 * fps->num / fps->den / 1000;
1344 
1345  // prevent division by zero
1346  if (hist->samples == 0)
1347  hist->samples=1;
1348 
1349  hist->pts = calloc(hist->samples, sizeof(*hist->pts));
1350  hist->sz = calloc(hist->samples, sizeof(*hist->sz));
1351  for(i=0; i<RATE_BINS; i++)
1352  {
1353  hist->bucket[i].low = INT_MAX;
1354  hist->bucket[i].high = 0;
1355  hist->bucket[i].count = 0;
1356  }
1357 }
1358 
1359 
1360 static void destroy_rate_histogram(struct rate_hist *hist)
1361 {
1362  free(hist->pts);
1363  free(hist->sz);
1364 }
1365 
1366 
1367 static void update_rate_histogram(struct rate_hist *hist,
1368  const vpx_codec_enc_cfg_t *cfg,
1369  const vpx_codec_cx_pkt_t *pkt)
1370 {
1371  int i, idx;
1372  int64_t now, then, sum_sz = 0, avg_bitrate;
1373 
1374  now = pkt->data.frame.pts * 1000
1375  * (uint64_t)cfg->g_timebase.num / (uint64_t)cfg->g_timebase.den;
1376 
1377  idx = hist->frames++ % hist->samples;
1378  hist->pts[idx] = now;
1379  hist->sz[idx] = pkt->data.frame.sz;
1380 
1381  if(now < cfg->rc_buf_initial_sz)
1382  return;
1383 
1384  then = now;
1385 
1386  /* Sum the size over the past rc_buf_sz ms */
1387  for(i = hist->frames; i > 0 && hist->frames - i < hist->samples; i--)
1388  {
1389  int i_idx = (i-1) % hist->samples;
1390 
1391  then = hist->pts[i_idx];
1392  if(now - then > cfg->rc_buf_sz)
1393  break;
1394  sum_sz += hist->sz[i_idx];
1395  }
1396 
1397  if (now == then)
1398  return;
1399 
1400  avg_bitrate = sum_sz * 8 * 1000 / (now - then);
1401  idx = avg_bitrate * (RATE_BINS/2) / (cfg->rc_target_bitrate * 1000);
1402  if(idx < 0)
1403  idx = 0;
1404  if(idx > RATE_BINS-1)
1405  idx = RATE_BINS-1;
1406  if(hist->bucket[idx].low > avg_bitrate)
1407  hist->bucket[idx].low = avg_bitrate;
1408  if(hist->bucket[idx].high < avg_bitrate)
1409  hist->bucket[idx].high = avg_bitrate;
1410  hist->bucket[idx].count++;
1411  hist->total++;
1412 }
1413 
1414 
1415 static void show_rate_histogram(struct rate_hist *hist,
1416  const vpx_codec_enc_cfg_t *cfg,
1417  int max_buckets)
1418 {
1419  int i, scale;
1420  int buckets = 0;
1421 
1422  for(i = 0; i < RATE_BINS; i++)
1423  {
1424  if(hist->bucket[i].low == INT_MAX)
1425  continue;
1426  hist->bucket[buckets++] = hist->bucket[i];
1427  }
1428 
1429  fprintf(stderr, "\nRate (over %dms window):\n", cfg->rc_buf_sz);
1430  scale = merge_hist_buckets(hist->bucket, &buckets, max_buckets);
1431  show_histogram(hist->bucket, buckets, hist->total, scale);
1432 }
1433 
1434 #define ARG_CTRL_CNT_MAX 10
1435 
1436 int main(int argc, const char **argv_)
1437 {
1438  vpx_codec_ctx_t encoder;
1439  const char *in_fn = NULL, *out_fn = NULL, *stats_fn = NULL;
1440  int i;
1441  FILE *infile, *outfile;
1442  vpx_codec_enc_cfg_t cfg;
1443  vpx_codec_err_t res;
1444  int pass, one_pass_only = 0;
1445  stats_io_t stats;
1446  vpx_image_t raw;
1447  const struct codec_item *codec = codecs;
1448  int frame_avail, got_data;
1449 
1450  struct arg arg;
1451  char **argv, **argi, **argj;
1452  int arg_usage = 0, arg_passes = 1, arg_deadline = 0;
1453  int arg_ctrls[ARG_CTRL_CNT_MAX][2], arg_ctrl_cnt = 0;
1454  int arg_limit = 0;
1455  static const arg_def_t **ctrl_args = no_args;
1456  static const int *ctrl_args_map = NULL;
1457  int verbose = 0, show_psnr = 0;
1458  int arg_use_i420 = 1;
1459  unsigned long cx_time = 0;
1460  unsigned int file_type, fourcc;
1461  y4m_input y4m;
1462  struct vpx_rational arg_framerate = {30, 1};
1463  int arg_have_framerate = 0;
1464  int write_webm = 1;
1465  EbmlGlobal ebml = {0};
1466  uint32_t hash = 0;
1467  uint64_t psnr_sse_total = 0;
1468  uint64_t psnr_samples_total = 0;
1469  double psnr_totals[4] = {0, 0, 0, 0};
1470  int psnr_count = 0;
1471  stereo_format_t stereo_fmt = STEREO_FORMAT_MONO;
1472  int counts[64]={0};
1473  int show_q_hist_buckets=0;
1474  int show_rate_hist_buckets=0;
1475  struct rate_hist rate_hist={0};
1476 
1477  exec_name = argv_[0];
1478  ebml.last_pts_ms = -1;
1479 
1480  if (argc < 3)
1481  usage_exit();
1482 
1483 
1484  /* First parse the codec and usage values, because we want to apply other
1485  * parameters on top of the default configuration provided by the codec.
1486  */
1487  argv = argv_dup(argc - 1, argv_ + 1);
1488 
1489  for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
1490  {
1491  arg.argv_step = 1;
1492 
1493  if (arg_match(&arg, &codecarg, argi))
1494  {
1495  int j, k = -1;
1496 
1497  for (j = 0; j < sizeof(codecs) / sizeof(codecs[0]); j++)
1498  if (!strcmp(codecs[j].name, arg.val))
1499  k = j;
1500 
1501  if (k >= 0)
1502  codec = codecs + k;
1503  else
1504  die("Error: Unrecognized argument (%s) to --codec\n",
1505  arg.val);
1506 
1507  }
1508  else if (arg_match(&arg, &passes, argi))
1509  {
1510  arg_passes = arg_parse_uint(&arg);
1511 
1512  if (arg_passes < 1 || arg_passes > 2)
1513  die("Error: Invalid number of passes (%d)\n", arg_passes);
1514  }
1515  else if (arg_match(&arg, &pass_arg, argi))
1516  {
1517  one_pass_only = arg_parse_uint(&arg);
1518 
1519  if (one_pass_only < 1 || one_pass_only > 2)
1520  die("Error: Invalid pass selected (%d)\n", one_pass_only);
1521  }
1522  else if (arg_match(&arg, &fpf_name, argi))
1523  stats_fn = arg.val;
1524  else if (arg_match(&arg, &usage, argi))
1525  arg_usage = arg_parse_uint(&arg);
1526  else if (arg_match(&arg, &deadline, argi))
1527  arg_deadline = arg_parse_uint(&arg);
1528  else if (arg_match(&arg, &best_dl, argi))
1529  arg_deadline = VPX_DL_BEST_QUALITY;
1530  else if (arg_match(&arg, &good_dl, argi))
1531  arg_deadline = VPX_DL_GOOD_QUALITY;
1532  else if (arg_match(&arg, &rt_dl, argi))
1533  arg_deadline = VPX_DL_REALTIME;
1534  else if (arg_match(&arg, &use_yv12, argi))
1535  {
1536  arg_use_i420 = 0;
1537  }
1538  else if (arg_match(&arg, &use_i420, argi))
1539  {
1540  arg_use_i420 = 1;
1541  }
1542  else if (arg_match(&arg, &verbosearg, argi))
1543  verbose = 1;
1544  else if (arg_match(&arg, &limit, argi))
1545  arg_limit = arg_parse_uint(&arg);
1546  else if (arg_match(&arg, &psnrarg, argi))
1547  show_psnr = 1;
1548  else if (arg_match(&arg, &framerate, argi))
1549  {
1550  arg_framerate = arg_parse_rational(&arg);
1551  arg_have_framerate = 1;
1552  }
1553  else if (arg_match(&arg, &use_ivf, argi))
1554  write_webm = 0;
1555  else if (arg_match(&arg, &outputfile, argi))
1556  out_fn = arg.val;
1557  else if (arg_match(&arg, &debugmode, argi))
1558  ebml.debug = 1;
1559  else if (arg_match(&arg, &q_hist_n, argi))
1560  show_q_hist_buckets = arg_parse_uint(&arg);
1561  else if (arg_match(&arg, &rate_hist_n, argi))
1562  show_rate_hist_buckets = arg_parse_uint(&arg);
1563  else
1564  argj++;
1565  }
1566 
1567  /* Ensure that --passes and --pass are consistent. If --pass is set and --passes=2,
1568  * ensure --fpf was set.
1569  */
1570  if (one_pass_only)
1571  {
1572  /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
1573  if (one_pass_only > arg_passes)
1574  {
1575  fprintf(stderr, "Warning: Assuming --pass=%d implies --passes=%d\n",
1576  one_pass_only, one_pass_only);
1577  arg_passes = one_pass_only;
1578  }
1579 
1580  if (arg_passes == 2 && !stats_fn)
1581  die("Must specify --fpf when --pass=%d and --passes=2\n", one_pass_only);
1582  }
1583 
1584  /* Populate encoder configuration */
1585  res = vpx_codec_enc_config_default(codec->iface, &cfg, arg_usage);
1586 
1587  if (res)
1588  {
1589  fprintf(stderr, "Failed to get config: %s\n",
1591  return EXIT_FAILURE;
1592  }
1593 
1594  /* Change the default timebase to a high enough value so that the encoder
1595  * will always create strictly increasing timestamps.
1596  */
1597  cfg.g_timebase.den = 1000;
1598 
1599  /* Never use the library's default resolution, require it be parsed
1600  * from the file or set on the command line.
1601  */
1602  cfg.g_w = 0;
1603  cfg.g_h = 0;
1604 
1605  /* Now parse the remainder of the parameters. */
1606  for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
1607  {
1608  arg.argv_step = 1;
1609 
1610  if (0);
1611  else if (arg_match(&arg, &threads, argi))
1612  cfg.g_threads = arg_parse_uint(&arg);
1613  else if (arg_match(&arg, &profile, argi))
1614  cfg.g_profile = arg_parse_uint(&arg);
1615  else if (arg_match(&arg, &width, argi))
1616  cfg.g_w = arg_parse_uint(&arg);
1617  else if (arg_match(&arg, &height, argi))
1618  cfg.g_h = arg_parse_uint(&arg);
1619  else if (arg_match(&arg, &stereo_mode, argi))
1620  stereo_fmt = arg_parse_enum_or_int(&arg);
1621  else if (arg_match(&arg, &timebase, argi))
1622  cfg.g_timebase = arg_parse_rational(&arg);
1623  else if (arg_match(&arg, &error_resilient, argi))
1624  cfg.g_error_resilient = arg_parse_uint(&arg);
1625  else if (arg_match(&arg, &lag_in_frames, argi))
1626  cfg.g_lag_in_frames = arg_parse_uint(&arg);
1627  else if (arg_match(&arg, &dropframe_thresh, argi))
1628  cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
1629  else if (arg_match(&arg, &resize_allowed, argi))
1630  cfg.rc_resize_allowed = arg_parse_uint(&arg);
1631  else if (arg_match(&arg, &resize_up_thresh, argi))
1632  cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
1633  else if (arg_match(&arg, &resize_down_thresh, argi))
1634  cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
1635  else if (arg_match(&arg, &resize_down_thresh, argi))
1636  cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
1637  else if (arg_match(&arg, &end_usage, argi))
1638  cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
1639  else if (arg_match(&arg, &target_bitrate, argi))
1640  cfg.rc_target_bitrate = arg_parse_uint(&arg);
1641  else if (arg_match(&arg, &min_quantizer, argi))
1642  cfg.rc_min_quantizer = arg_parse_uint(&arg);
1643  else if (arg_match(&arg, &max_quantizer, argi))
1644  cfg.rc_max_quantizer = arg_parse_uint(&arg);
1645  else if (arg_match(&arg, &undershoot_pct, argi))
1646  cfg.rc_undershoot_pct = arg_parse_uint(&arg);
1647  else if (arg_match(&arg, &overshoot_pct, argi))
1648  cfg.rc_overshoot_pct = arg_parse_uint(&arg);
1649  else if (arg_match(&arg, &buf_sz, argi))
1650  cfg.rc_buf_sz = arg_parse_uint(&arg);
1651  else if (arg_match(&arg, &buf_initial_sz, argi))
1652  cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
1653  else if (arg_match(&arg, &buf_optimal_sz, argi))
1654  cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
1655  else if (arg_match(&arg, &bias_pct, argi))
1656  {
1657  cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
1658 
1659  if (arg_passes < 2)
1660  fprintf(stderr,
1661  "Warning: option %s ignored in one-pass mode.\n",
1662  arg.name);
1663  }
1664  else if (arg_match(&arg, &minsection_pct, argi))
1665  {
1666  cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1667 
1668  if (arg_passes < 2)
1669  fprintf(stderr,
1670  "Warning: option %s ignored in one-pass mode.\n",
1671  arg.name);
1672  }
1673  else if (arg_match(&arg, &maxsection_pct, argi))
1674  {
1675  cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1676 
1677  if (arg_passes < 2)
1678  fprintf(stderr,
1679  "Warning: option %s ignored in one-pass mode.\n",
1680  arg.name);
1681  }
1682  else if (arg_match(&arg, &kf_min_dist, argi))
1683  cfg.kf_min_dist = arg_parse_uint(&arg);
1684  else if (arg_match(&arg, &kf_max_dist, argi))
1685  cfg.kf_max_dist = arg_parse_uint(&arg);
1686  else if (arg_match(&arg, &kf_disabled, argi))
1687  cfg.kf_mode = VPX_KF_DISABLED;
1688  else
1689  argj++;
1690  }
1691 
1692  /* Handle codec specific options */
1693 #if CONFIG_VP8_ENCODER
1694 
1695  if (codec->iface == &vpx_codec_vp8_cx_algo)
1696  {
1697  ctrl_args = vp8_args;
1698  ctrl_args_map = vp8_arg_ctrl_map;
1699  }
1700 
1701 #endif
1702 
1703  for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
1704  {
1705  int match = 0;
1706 
1707  arg.argv_step = 1;
1708 
1709  for (i = 0; ctrl_args[i]; i++)
1710  {
1711  if (arg_match(&arg, ctrl_args[i], argi))
1712  {
1713  match = 1;
1714 
1715  if (arg_ctrl_cnt < ARG_CTRL_CNT_MAX)
1716  {
1717  arg_ctrls[arg_ctrl_cnt][0] = ctrl_args_map[i];
1718  arg_ctrls[arg_ctrl_cnt][1] = arg_parse_enum_or_int(&arg);
1719  arg_ctrl_cnt++;
1720  }
1721  }
1722  }
1723 
1724  if (!match)
1725  argj++;
1726  }
1727 
1728  /* Check for unrecognized options */
1729  for (argi = argv; *argi; argi++)
1730  if (argi[0][0] == '-' && argi[0][1])
1731  die("Error: Unrecognized option %s\n", *argi);
1732 
1733  /* Handle non-option arguments */
1734  in_fn = argv[0];
1735 
1736  if (!in_fn)
1737  usage_exit();
1738 
1739  if(!out_fn)
1740  die("Error: Output file is required (specify with -o)\n");
1741 
1742  memset(&stats, 0, sizeof(stats));
1743 
1744  for (pass = one_pass_only ? one_pass_only - 1 : 0; pass < arg_passes; pass++)
1745  {
1746  int frames_in = 0, frames_out = 0;
1747  unsigned long nbytes = 0;
1748  struct detect_buffer detect;
1749 
1750  /* Parse certain options from the input file, if possible */
1751  infile = strcmp(in_fn, "-") ? fopen(in_fn, "rb")
1752  : set_binary_mode(stdin);
1753 
1754  if (!infile)
1755  {
1756  fprintf(stderr, "Failed to open input file\n");
1757  return EXIT_FAILURE;
1758  }
1759 
1760  /* For RAW input sources, these bytes will applied on the first frame
1761  * in read_frame().
1762  */
1763  detect.buf_read = fread(detect.buf, 1, 4, infile);
1764  detect.position = 0;
1765 
1766  if (detect.buf_read == 4 && file_is_y4m(infile, &y4m, detect.buf))
1767  {
1768  if (y4m_input_open(&y4m, infile, detect.buf, 4) >= 0)
1769  {
1770  file_type = FILE_TYPE_Y4M;
1771  cfg.g_w = y4m.pic_w;
1772  cfg.g_h = y4m.pic_h;
1773 
1774  /* Use the frame rate from the file only if none was specified
1775  * on the command-line.
1776  */
1777  if (!arg_have_framerate)
1778  {
1779  arg_framerate.num = y4m.fps_n;
1780  arg_framerate.den = y4m.fps_d;
1781  }
1782 
1783  arg_use_i420 = 0;
1784  }
1785  else
1786  {
1787  fprintf(stderr, "Unsupported Y4M stream.\n");
1788  return EXIT_FAILURE;
1789  }
1790  }
1791  else if (detect.buf_read == 4 &&
1792  file_is_ivf(infile, &fourcc, &cfg.g_w, &cfg.g_h, &detect))
1793  {
1794  file_type = FILE_TYPE_IVF;
1795  switch (fourcc)
1796  {
1797  case 0x32315659:
1798  arg_use_i420 = 0;
1799  break;
1800  case 0x30323449:
1801  arg_use_i420 = 1;
1802  break;
1803  default:
1804  fprintf(stderr, "Unsupported fourcc (%08x) in IVF\n", fourcc);
1805  return EXIT_FAILURE;
1806  }
1807  }
1808  else
1809  {
1810  file_type = FILE_TYPE_RAW;
1811  }
1812 
1813  if(!cfg.g_w || !cfg.g_h)
1814  {
1815  fprintf(stderr, "Specify stream dimensions with --width (-w) "
1816  " and --height (-h).\n");
1817  return EXIT_FAILURE;
1818  }
1819 
1820 #define SHOW(field) fprintf(stderr, " %-28s = %d\n", #field, cfg.field)
1821 
1822  if (verbose && pass == 0)
1823  {
1824  fprintf(stderr, "Codec: %s\n", vpx_codec_iface_name(codec->iface));
1825  fprintf(stderr, "Source file: %s Format: %s\n", in_fn,
1826  arg_use_i420 ? "I420" : "YV12");
1827  fprintf(stderr, "Destination file: %s\n", out_fn);
1828  fprintf(stderr, "Encoder parameters:\n");
1829 
1830  SHOW(g_usage);
1831  SHOW(g_threads);
1832  SHOW(g_profile);
1833  SHOW(g_w);
1834  SHOW(g_h);
1835  SHOW(g_timebase.num);
1836  SHOW(g_timebase.den);
1837  SHOW(g_error_resilient);
1838  SHOW(g_pass);
1839  SHOW(g_lag_in_frames);
1840  SHOW(rc_dropframe_thresh);
1841  SHOW(rc_resize_allowed);
1842  SHOW(rc_resize_up_thresh);
1843  SHOW(rc_resize_down_thresh);
1844  SHOW(rc_end_usage);
1845  SHOW(rc_target_bitrate);
1846  SHOW(rc_min_quantizer);
1847  SHOW(rc_max_quantizer);
1848  SHOW(rc_undershoot_pct);
1849  SHOW(rc_overshoot_pct);
1850  SHOW(rc_buf_sz);
1851  SHOW(rc_buf_initial_sz);
1852  SHOW(rc_buf_optimal_sz);
1853  SHOW(rc_2pass_vbr_bias_pct);
1854  SHOW(rc_2pass_vbr_minsection_pct);
1855  SHOW(rc_2pass_vbr_maxsection_pct);
1856  SHOW(kf_mode);
1857  SHOW(kf_min_dist);
1858  SHOW(kf_max_dist);
1859  }
1860 
1861  if(pass == (one_pass_only ? one_pass_only - 1 : 0)) {
1862  if (file_type == FILE_TYPE_Y4M)
1863  /*The Y4M reader does its own allocation.
1864  Just initialize this here to avoid problems if we never read any
1865  frames.*/
1866  memset(&raw, 0, sizeof(raw));
1867  else
1868  vpx_img_alloc(&raw, arg_use_i420 ? VPX_IMG_FMT_I420 : VPX_IMG_FMT_YV12,
1869  cfg.g_w, cfg.g_h, 1);
1870 
1871  init_rate_histogram(&rate_hist, &cfg, &arg_framerate);
1872  }
1873 
1874  outfile = strcmp(out_fn, "-") ? fopen(out_fn, "wb")
1875  : set_binary_mode(stdout);
1876 
1877  if (!outfile)
1878  {
1879  fprintf(stderr, "Failed to open output file\n");
1880  return EXIT_FAILURE;
1881  }
1882 
1883  if(write_webm && fseek(outfile, 0, SEEK_CUR))
1884  {
1885  fprintf(stderr, "WebM output to pipes not supported.\n");
1886  return EXIT_FAILURE;
1887  }
1888 
1889  if (stats_fn)
1890  {
1891  if (!stats_open_file(&stats, stats_fn, pass))
1892  {
1893  fprintf(stderr, "Failed to open statistics store\n");
1894  return EXIT_FAILURE;
1895  }
1896  }
1897  else
1898  {
1899  if (!stats_open_mem(&stats, pass))
1900  {
1901  fprintf(stderr, "Failed to open statistics store\n");
1902  return EXIT_FAILURE;
1903  }
1904  }
1905 
1906  cfg.g_pass = arg_passes == 2
1908  : VPX_RC_ONE_PASS;
1909 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
1910 
1911  if (pass)
1912  {
1913  cfg.rc_twopass_stats_in = stats_get(&stats);
1914  }
1915 
1916 #endif
1917 
1918  if(write_webm)
1919  {
1920  ebml.stream = outfile;
1921  write_webm_file_header(&ebml, &cfg, &arg_framerate, stereo_fmt);
1922  }
1923  else
1924  write_ivf_file_header(outfile, &cfg, codec->fourcc, 0);
1925 
1926 
1927  /* Construct Encoder Context */
1928  vpx_codec_enc_init(&encoder, codec->iface, &cfg,
1929  show_psnr ? VPX_CODEC_USE_PSNR : 0);
1930  ctx_exit_on_error(&encoder, "Failed to initialize encoder");
1931 
1932  /* Note that we bypass the vpx_codec_control wrapper macro because
1933  * we're being clever to store the control IDs in an array. Real
1934  * applications will want to make use of the enumerations directly
1935  */
1936  for (i = 0; i < arg_ctrl_cnt; i++)
1937  {
1938  if (vpx_codec_control_(&encoder, arg_ctrls[i][0], arg_ctrls[i][1]))
1939  fprintf(stderr, "Error: Tried to set control %d = %d\n",
1940  arg_ctrls[i][0], arg_ctrls[i][1]);
1941 
1942  ctx_exit_on_error(&encoder, "Failed to control codec");
1943  }
1944 
1945  frame_avail = 1;
1946  got_data = 0;
1947 
1948  while (frame_avail || got_data)
1949  {
1950  vpx_codec_iter_t iter = NULL;
1951  const vpx_codec_cx_pkt_t *pkt;
1952  struct vpx_usec_timer timer;
1953  int64_t frame_start, next_frame_start;
1954 
1955  if (!arg_limit || frames_in < arg_limit)
1956  {
1957  frame_avail = read_frame(infile, &raw, file_type, &y4m,
1958  &detect);
1959 
1960  if (frame_avail)
1961  frames_in++;
1962 
1963  fprintf(stderr,
1964  "\rPass %d/%d frame %4d/%-4d %7ldB \033[K", pass + 1,
1965  arg_passes, frames_in, frames_out, nbytes);
1966  }
1967  else
1968  frame_avail = 0;
1969 
1970  vpx_usec_timer_start(&timer);
1971 
1972  frame_start = (cfg.g_timebase.den * (int64_t)(frames_in - 1)
1973  * arg_framerate.den) / cfg.g_timebase.num / arg_framerate.num;
1974  next_frame_start = (cfg.g_timebase.den * (int64_t)(frames_in)
1975  * arg_framerate.den)
1976  / cfg.g_timebase.num / arg_framerate.num;
1977  vpx_codec_encode(&encoder, frame_avail ? &raw : NULL, frame_start,
1978  next_frame_start - frame_start,
1979  0, arg_deadline);
1980  vpx_usec_timer_mark(&timer);
1981  cx_time += vpx_usec_timer_elapsed(&timer);
1982  ctx_exit_on_error(&encoder, "Failed to encode frame");
1983 
1984  if(cfg.g_pass != VPX_RC_FIRST_PASS)
1985  {
1986  int q;
1987 
1989  ctx_exit_on_error(&encoder, "Failed to read quantizer");
1990  counts[q]++;
1991  }
1992 
1993  got_data = 0;
1994 
1995  while ((pkt = vpx_codec_get_cx_data(&encoder, &iter)))
1996  {
1997  got_data = 1;
1998 
1999  switch (pkt->kind)
2000  {
2002  frames_out++;
2003  fprintf(stderr, " %6luF",
2004  (unsigned long)pkt->data.frame.sz);
2005 
2006  update_rate_histogram(&rate_hist, &cfg, pkt);
2007  if(write_webm)
2008  {
2009  /* Update the hash */
2010  if(!ebml.debug)
2011  hash = murmur(pkt->data.frame.buf,
2012  pkt->data.frame.sz, hash);
2013 
2014  write_webm_block(&ebml, &cfg, pkt);
2015  }
2016  else
2017  {
2018  write_ivf_frame_header(outfile, pkt);
2019  if(fwrite(pkt->data.frame.buf, 1,
2020  pkt->data.frame.sz, outfile));
2021  }
2022  nbytes += pkt->data.raw.sz;
2023  break;
2024  case VPX_CODEC_STATS_PKT:
2025  frames_out++;
2026  fprintf(stderr, " %6luS",
2027  (unsigned long)pkt->data.twopass_stats.sz);
2028  stats_write(&stats,
2029  pkt->data.twopass_stats.buf,
2030  pkt->data.twopass_stats.sz);
2031  nbytes += pkt->data.raw.sz;
2032  break;
2033  case VPX_CODEC_PSNR_PKT:
2034 
2035  if (show_psnr)
2036  {
2037  int i;
2038 
2039  psnr_sse_total += pkt->data.psnr.sse[0];
2040  psnr_samples_total += pkt->data.psnr.samples[0];
2041  for (i = 0; i < 4; i++)
2042  {
2043  fprintf(stderr, "%.3lf ", pkt->data.psnr.psnr[i]);
2044  psnr_totals[i] += pkt->data.psnr.psnr[i];
2045  }
2046  psnr_count++;
2047  }
2048 
2049  break;
2050  default:
2051  break;
2052  }
2053  }
2054 
2055  fflush(stdout);
2056  }
2057 
2058  fprintf(stderr,
2059  "\rPass %d/%d frame %4d/%-4d %7ldB %7ldb/f %7"PRId64"b/s"
2060  " %7lu %s (%.2f fps)\033[K", pass + 1,
2061  arg_passes, frames_in, frames_out, nbytes, nbytes * 8 / frames_in,
2062  nbytes * 8 *(int64_t)arg_framerate.num / arg_framerate.den / frames_in,
2063  cx_time > 9999999 ? cx_time / 1000 : cx_time,
2064  cx_time > 9999999 ? "ms" : "us",
2065  (float)frames_in * 1000000.0 / (float)cx_time);
2066 
2067  if ( (show_psnr) && (psnr_count>0) )
2068  {
2069  int i;
2070  double ovpsnr = vp8_mse2psnr(psnr_samples_total, 255.0,
2071  psnr_sse_total);
2072 
2073  fprintf(stderr, "\nPSNR (Overall/Avg/Y/U/V)");
2074 
2075  fprintf(stderr, " %.3lf", ovpsnr);
2076  for (i = 0; i < 4; i++)
2077  {
2078  fprintf(stderr, " %.3lf", psnr_totals[i]/psnr_count);
2079  }
2080  }
2081 
2082  vpx_codec_destroy(&encoder);
2083 
2084  fclose(infile);
2085  if (file_type == FILE_TYPE_Y4M)
2086  y4m_input_close(&y4m);
2087 
2088  if(write_webm)
2089  {
2090  write_webm_file_footer(&ebml, hash);
2091  free(ebml.cue_list);
2092  ebml.cue_list = NULL;
2093  }
2094  else
2095  {
2096  if (!fseek(outfile, 0, SEEK_SET))
2097  write_ivf_file_header(outfile, &cfg, codec->fourcc, frames_out);
2098  }
2099 
2100  fclose(outfile);
2101  stats_close(&stats, arg_passes-1);
2102  fprintf(stderr, "\n");
2103 
2104  if (one_pass_only)
2105  break;
2106  }
2107 
2108  if (show_q_hist_buckets)
2109  show_q_histogram(counts, show_q_hist_buckets);
2110 
2111  if (show_rate_hist_buckets)
2112  show_rate_histogram(&rate_hist, &cfg, show_rate_hist_buckets);
2113  destroy_rate_histogram(&rate_hist);
2114 
2115  vpx_img_free(&raw);
2116  free(argv);
2117  return EXIT_SUCCESS;
2118 }
Rational Number.
Definition: vpx_encoder.h:210
unsigned int rc_buf_initial_sz
Decoder Buffer Initial Size.
Definition: vpx_encoder.h:531
struct vpx_fixed_buf twopass_stats
Definition: vpx_encoder.h:187
struct vpx_codec_iface vpx_codec_iface_t
Codec interface structure.
Definition: vpx_codec.h:167
control function to set vp8 encoder cpuused
Definition: vp8cx.h:143
Image Descriptor.
Definition: vpx_image.h:97
Describes the encoder algorithm interface to applications.
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
Definition: vpx_image.h:55
struct vpx_fixed_buf rc_twopass_stats_in
Two-pass stats buffer.
Definition: vpx_encoder.h:439
const char * vpx_codec_err_to_string(vpx_codec_err_t err)
Convert error number to printable string.
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:337
Definition: vpx_encoder.h:230
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition: vpx_encoder.h:521
struct vpx_fixed_buf raw
Definition: vpx_encoder.h:194
enum vpx_kf_mode kf_mode
Keyframe placement mode.
Definition: vpx_encoder.h:586
int den
Definition: vpx_encoder.h:213
vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx, const vpx_image_t *img, vpx_codec_pts_t pts, unsigned long duration, vpx_enc_frame_flags_t flags, unsigned long deadline)
Encode a frame.
unsigned int rc_max_quantizer
Maximum (Worst Quality) Quantizer.
Definition: vpx_encoder.h:473
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition: vpx_encoder.h:462
Definition: vpx_encoder.h:154
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: vpx_encoder.h:606
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: vpx_encoder.h:369
Encoder configuration structure.
Definition: vpx_encoder.h:268
Definition: vp8cx.h:159
control function to set constrained quality level
Definition: vp8cx.h:166
Definition: vp8cx.h:158
Definition: vpx_encoder.h:155
Max data rate for Intra frames.
Definition: vp8cx.h:180
Encoder output packet.
Definition: vpx_encoder.h:165
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition: vpx_encoder.h:504
void * buf
Definition: vpx_encoder.h:85
unsigned int rc_2pass_vbr_bias_pct
Two-pass mode CBR/VBR bias.
Definition: vpx_encoder.h:557
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition: vpx_encoder.h:541
#define VPX_PLANE_V
Definition: vpx_image.h:117
Generic fixed size buffer structure.
Definition: vpx_encoder.h:83
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: vpx_encoder.h:596
Definition: vpx_encoder.h:221
unsigned int g_profile
Bitstream profile to use.
Definition: vpx_encoder.h:301
Definition: vpx_encoder.h:222
struct vpx_codec_cx_pkt::@1::@2 frame
vpx_image_t * vpx_img_alloc(vpx_image_t *img, vpx_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
Definition: vpx_image.h:56
unsigned int d_w
Definition: vpx_image.h:106
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:312
unsigned int rc_undershoot_pct
Rate control adaptation undershoot control.
Definition: vpx_encoder.h:491
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:322
int stride[4]
Definition: vpx_image.h:127
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:167
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: vpx_encoder.h:392
Definition: vp8cx.h:152
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.
vpx_img_fmt_t fmt
Definition: vpx_image.h:99
unsigned char * planes[4]
Definition: vpx_image.h:126
Definition: vp8cx.h:148
unsigned int rc_target_bitrate
Target data rate.
Definition: vpx_encoder.h:446
#define VPX_DL_REALTIME
Definition: vpx_encoder.h:704
int num
Definition: vpx_encoder.h:212
Definition: vp8cx.h:145
#define VPX_DL_BEST_QUALITY
Definition: vpx_encoder.h:710
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, unsigned int usage)
Get a default configuration.
Definition: vpx_encoder.h:229
enum vpx_enc_pass g_pass
Multi-pass Encoding Mode.
Definition: vpx_encoder.h:354
double psnr[4]
Definition: vpx_encoder.h:192
#define VPX_CODEC_USE_PSNR
Initialization-time Feature Enabling.
Definition: vpx_encoder.h:73
unsigned int g_threads
Maximum number of threads to use.
Definition: vpx_encoder.h:290
#define VPX_DL_GOOD_QUALITY
Definition: vpx_encoder.h:707
const char * vpx_codec_error_detail(vpx_codec_ctx_t *ctx)
Retrieve detailed error information for codec context.
Provides definitions for using the VP8 encoder algorithm within the vpx Codec Interface.
#define VPX_PLANE_U
Definition: vpx_image.h:116
#define vpx_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_enc_init_ver()
Definition: vpx_encoder.h:644
unsigned int rc_resize_allowed
Enable/disable spatial resampling, if supported by the codec.
Definition: vpx_encoder.h:402
vpx_codec_err_t
Algorithm return codes.
Definition: vpx_codec.h:81
const vpx_codec_cx_pkt_t * vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Encoded data iterator.
union vpx_codec_cx_pkt::@1 data
Definition: vp8cx.h:156
Definition: vpx_encoder.h:247
Definition: vp8cx.h:157
int64_t vpx_codec_pts_t
Time Stamp Type.
Definition: vpx_encoder.h:95
Definition: vp8cx.h:144
vpx_codec_err_t vpx_codec_control_(vpx_codec_ctx_t *ctx, int ctrl_id,...)
Control algorithm.
Definition: vpx_encoder.h:231
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition: vpx_codec.h:392
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
unsigned int d_h
Definition: vpx_image.h:107
size_t sz
Definition: vpx_encoder.h:86
unsigned int rc_resize_down_thresh
Spatial resampling down watermark.
Definition: vpx_encoder.h:420
vpx_codec_err_t err
Definition: vpx_codec.h:197
Definition: vp8cx.h:147
const char * vpx_codec_error(vpx_codec_ctx_t *ctx)
Retrieve error synopsis for codec context.
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:106
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:182
Definition: vp8cx.h:146
Definition: vpx_encoder.h:153
unsigned int rc_2pass_vbr_maxsection_pct
Two-pass mode per-GOP maximum bitrate.
Definition: vpx_encoder.h:573
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: vpx_encoder.h:346
unsigned int rc_2pass_vbr_minsection_pct
Two-pass mode per-GOP minimum bitrate.
Definition: vpx_encoder.h:565
#define VPX_FRAME_IS_INVISIBLE
Definition: vpx_encoder.h:112
unsigned int rc_resize_up_thresh
Spatial resampling up watermark.
Definition: vpx_encoder.h:411
enum vpx_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: vpx_encoder.h:431
Definition: vpx_encoder.h:220
Codec context structure.
Definition: vpx_codec.h:193