2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
12 /* This is a simple program that encodes YV12 files and generates ivf
13 * files using the new interface.
15 #if defined(_WIN32) || !CONFIG_OS_SUPPORT
16 #define USE_POSIX_MMAP 0
18 #define USE_POSIX_MMAP 1
27 #include "vpx/vpx_encoder.h"
29 #include <sys/types.h>
35 #include "vpx/vp8cx.h"
36 #include "vpx_ports/mem_ops.h"
37 #include "vpx_ports/vpx_timer.h"
38 #include "tools_common.h"
40 #include "libmkv/EbmlWriter.h"
41 #include "libmkv/EbmlIDs.h"
43 /* Need special handling of these functions on Windows */
45 /* MSVS doesn't define off_t, and uses _f{seek,tell}i64 */
46 typedef __int64 off_t
;
47 #define fseeko _fseeki64
48 #define ftello _ftelli64
50 /* MinGW defines off_t as long
51 and uses f{seek,tell}o64/off64_t for large files */
52 #define fseeko fseeko64
53 #define ftello ftello64
57 #define LITERALU64(hi,lo) ((((uint64_t)hi)<<32)|lo)
59 /* We should use 32-bit file operations in WebM file format
60 * when building ARM executable file (.axf) with RVCT */
61 #if !CONFIG_OS_SUPPORT
67 /* Swallow warnings about unused results of fread/fwrite */
68 static size_t wrap_fread(void *ptr
, size_t size
, size_t nmemb
,
71 return fread(ptr
, size
, nmemb
, stream
);
73 #define fread wrap_fread
75 static size_t wrap_fwrite(const void *ptr
, size_t size
, size_t nmemb
,
78 return fwrite(ptr
, size
, nmemb
, stream
);
80 #define fwrite wrap_fwrite
83 static const char *exec_name
;
85 static const struct codec_item
88 vpx_codec_iface_t
*iface
;
92 #if CONFIG_VP8_ENCODER
93 {"vp8", &vpx_codec_vp8_cx_algo
, 0x30385056},
97 static void usage_exit();
99 #define LOG_ERROR(label) do \
101 const char *l=label;\
105 fprintf(stderr, "%s: ", l);\
106 vfprintf(stderr, fmt, ap);\
107 fprintf(stderr, "\n");\
111 void die(const char *fmt
, ...)
118 void fatal(const char *fmt
, ...)
125 void warn(const char *fmt
, ...)
127 LOG_ERROR("Warning");
131 static void ctx_exit_on_error(vpx_codec_ctx_t
*ctx
, const char *s
, ...)
138 const char *detail
= vpx_codec_error_detail(ctx
);
140 vfprintf(stderr
, s
, ap
);
141 fprintf(stderr
, ": %s\n", vpx_codec_error(ctx
));
144 fprintf(stderr
, " %s\n", detail
);
150 /* This structure is used to abstract the different ways of handling
151 * first pass statistics.
162 int stats_open_file(stats_io_t
*stats
, const char *fpf
, int pass
)
170 stats
->file
= fopen(fpf
, "wb");
172 stats
->buf
.buf
= NULL
,
173 res
= (stats
->file
!= NULL
);
179 struct stat stat_buf
;
182 fd
= open(fpf
, O_RDONLY
);
183 stats
->file
= fdopen(fd
, "rb");
184 fstat(fd
, &stat_buf
);
185 stats
->buf
.sz
= stat_buf
.st_size
;
186 stats
->buf
.buf
= mmap(NULL
, stats
->buf
.sz
, PROT_READ
, MAP_PRIVATE
,
188 res
= (stats
->buf
.buf
!= NULL
);
192 stats
->file
= fopen(fpf
, "rb");
194 if (fseek(stats
->file
, 0, SEEK_END
))
195 fatal("First-pass stats file must be seekable!");
197 stats
->buf
.sz
= stats
->buf_alloc_sz
= ftell(stats
->file
);
200 stats
->buf
.buf
= malloc(stats
->buf_alloc_sz
);
203 fatal("Failed to allocate first-pass stats buffer (%lu bytes)",
204 (unsigned long)stats
->buf_alloc_sz
);
206 nbytes
= fread(stats
->buf
.buf
, 1, stats
->buf
.sz
, stats
->file
);
207 res
= (nbytes
== stats
->buf
.sz
);
214 int stats_open_mem(stats_io_t
*stats
, int pass
)
222 stats
->buf_alloc_sz
= 64 * 1024;
223 stats
->buf
.buf
= malloc(stats
->buf_alloc_sz
);
226 stats
->buf_ptr
= stats
->buf
.buf
;
227 res
= (stats
->buf
.buf
!= NULL
);
232 void stats_close(stats_io_t
*stats
, int last_pass
)
236 if (stats
->pass
== last_pass
)
240 munmap(stats
->buf
.buf
, stats
->buf
.sz
);
242 free(stats
->buf
.buf
);
251 if (stats
->pass
== last_pass
)
252 free(stats
->buf
.buf
);
256 void stats_write(stats_io_t
*stats
, const void *pkt
, size_t len
)
260 (void) fwrite(pkt
, 1, len
, stats
->file
);
264 if (stats
->buf
.sz
+ len
> stats
->buf_alloc_sz
)
266 size_t new_sz
= stats
->buf_alloc_sz
+ 64 * 1024;
267 char *new_ptr
= realloc(stats
->buf
.buf
, new_sz
);
271 stats
->buf_ptr
= new_ptr
+ (stats
->buf_ptr
- (char *)stats
->buf
.buf
);
272 stats
->buf
.buf
= new_ptr
;
273 stats
->buf_alloc_sz
= new_sz
;
276 fatal("Failed to realloc firstpass stats buffer.");
279 memcpy(stats
->buf_ptr
, pkt
, len
);
280 stats
->buf
.sz
+= len
;
281 stats
->buf_ptr
+= len
;
285 vpx_fixed_buf_t
stats_get(stats_io_t
*stats
)
290 /* Stereo 3D packed frame format */
291 typedef enum stereo_format
293 STEREO_FORMAT_MONO
= 0,
294 STEREO_FORMAT_LEFT_RIGHT
= 1,
295 STEREO_FORMAT_BOTTOM_TOP
= 2,
296 STEREO_FORMAT_TOP_BOTTOM
= 3,
297 STEREO_FORMAT_RIGHT_LEFT
= 11
307 struct detect_buffer
{
319 struct detect_buffer detect
;
320 enum video_file_type file_type
;
323 struct vpx_rational framerate
;
328 #define IVF_FRAME_HDR_SZ (4+8) /* 4 byte size + 8 byte timestamp */
329 static int read_frame(struct input_state
*input
, vpx_image_t
*img
)
331 FILE *f
= input
->file
;
332 enum video_file_type file_type
= input
->file_type
;
333 y4m_input
*y4m
= &input
->y4m
;
334 struct detect_buffer
*detect
= &input
->detect
;
338 if (file_type
== FILE_TYPE_Y4M
)
340 if (y4m_input_fetch_frame(y4m
, f
, img
) < 1)
345 if (file_type
== FILE_TYPE_IVF
)
347 char junk
[IVF_FRAME_HDR_SZ
];
349 /* Skip the frame header. We know how big the frame should be. See
350 * write_ivf_frame_header() for documentation on the frame header
353 (void) fread(junk
, 1, IVF_FRAME_HDR_SZ
, f
);
356 for (plane
= 0; plane
< 3; plane
++)
359 int w
= (plane
? (1 + img
->d_w
) / 2 : img
->d_w
);
360 int h
= (plane
? (1 + img
->d_h
) / 2 : img
->d_h
);
363 /* Determine the correct plane based on the image format. The for-loop
364 * always counts in Y,U,V order, but this may not match the order of
370 ptr
= img
->planes
[img
->fmt
==VPX_IMG_FMT_YV12
? VPX_PLANE_V
: VPX_PLANE_U
];
373 ptr
= img
->planes
[img
->fmt
==VPX_IMG_FMT_YV12
?VPX_PLANE_U
: VPX_PLANE_V
];
376 ptr
= img
->planes
[plane
];
379 for (r
= 0; r
< h
; r
++)
382 size_t buf_position
= 0;
383 const size_t left
= detect
->buf_read
- detect
->position
;
386 const size_t more
= (left
< needed
) ? left
: needed
;
387 memcpy(ptr
, detect
->buf
+ detect
->position
, more
);
390 detect
->position
+= more
;
394 shortread
|= (fread(ptr
+ buf_position
, 1, needed
, f
) < needed
);
397 ptr
+= img
->stride
[plane
];
406 unsigned int file_is_y4m(FILE *infile
,
410 if(memcmp(detect
, "YUV4", 4) == 0)
417 #define IVF_FILE_HDR_SZ (32)
418 unsigned int file_is_ivf(struct input_state
*input
,
419 unsigned int *fourcc
)
421 char raw_hdr
[IVF_FILE_HDR_SZ
];
423 FILE *infile
= input
->file
;
424 unsigned int *width
= &input
->w
;
425 unsigned int *height
= &input
->h
;
426 struct detect_buffer
*detect
= &input
->detect
;
428 if(memcmp(detect
->buf
, "DKIF", 4) != 0)
431 /* See write_ivf_file_header() for more documentation on the file header
434 if (fread(raw_hdr
+ 4, 1, IVF_FILE_HDR_SZ
- 4, infile
)
435 == IVF_FILE_HDR_SZ
- 4)
440 if (mem_get_le16(raw_hdr
+ 4) != 0)
441 warn("Unrecognized IVF version! This file may not decode "
444 *fourcc
= mem_get_le32(raw_hdr
+ 8);
450 *width
= mem_get_le16(raw_hdr
+ 12);
451 *height
= mem_get_le16(raw_hdr
+ 14);
452 detect
->position
= 4;
459 static void write_ivf_file_header(FILE *outfile
,
460 const vpx_codec_enc_cfg_t
*cfg
,
466 if (cfg
->g_pass
!= VPX_RC_ONE_PASS
&& cfg
->g_pass
!= VPX_RC_LAST_PASS
)
473 mem_put_le16(header
+ 4, 0); /* version */
474 mem_put_le16(header
+ 6, 32); /* headersize */
475 mem_put_le32(header
+ 8, fourcc
); /* headersize */
476 mem_put_le16(header
+ 12, cfg
->g_w
); /* width */
477 mem_put_le16(header
+ 14, cfg
->g_h
); /* height */
478 mem_put_le32(header
+ 16, cfg
->g_timebase
.den
); /* rate */
479 mem_put_le32(header
+ 20, cfg
->g_timebase
.num
); /* scale */
480 mem_put_le32(header
+ 24, frame_cnt
); /* length */
481 mem_put_le32(header
+ 28, 0); /* unused */
483 (void) fwrite(header
, 1, 32, outfile
);
487 static void write_ivf_frame_header(FILE *outfile
,
488 const vpx_codec_cx_pkt_t
*pkt
)
493 if (pkt
->kind
!= VPX_CODEC_CX_FRAME_PKT
)
496 pts
= pkt
->data
.frame
.pts
;
497 mem_put_le32(header
, (int)pkt
->data
.frame
.sz
);
498 mem_put_le32(header
+ 4, pts
& 0xFFFFFFFF);
499 mem_put_le32(header
+ 8, pts
>> 32);
501 (void) fwrite(header
, 1, 12, outfile
);
504 static void write_ivf_frame_size(FILE *outfile
, size_t size
)
507 mem_put_le32(header
, (int)size
);
508 (void) fwrite(header
, 1, 4, outfile
);
512 typedef off_t EbmlLoc
;
528 vpx_rational_t framerate
;
530 /* These pointers are to the start of an element */
531 off_t position_reference
;
533 off_t segment_info_pos
;
538 /* This pointer is to a specific element to be serialized */
541 /* These pointers are to the size field of the element */
542 EbmlLoc startSegment
;
543 EbmlLoc startCluster
;
545 uint32_t cluster_timecode
;
548 struct cue_entry
*cue_list
;
554 void Ebml_Write(EbmlGlobal
*glob
, const void *buffer_in
, unsigned long len
)
556 (void) fwrite(buffer_in
, 1, len
, glob
->stream
);
559 #define WRITE_BUFFER(s) \
560 for(i = len-1; i>=0; i--)\
562 x = (char)(*(const s *)buffer_in >> (i * CHAR_BIT)); \
563 Ebml_Write(glob, &x, 1); \
565 void Ebml_Serialize(EbmlGlobal
*glob
, const void *buffer_in
, int buffer_size
, unsigned long len
)
582 WRITE_BUFFER(int16_t)
585 WRITE_BUFFER(int32_t)
588 WRITE_BUFFER(int64_t)
596 /* Need a fixed size serializer for the track ID. libmkv provides a 64 bit
597 * one, but not a 32 bit one.
599 static void Ebml_SerializeUnsigned32(EbmlGlobal
*glob
, unsigned long class_id
, uint64_t ui
)
601 unsigned char sizeSerialized
= 4 | 0x80;
602 Ebml_WriteID(glob
, class_id
);
603 Ebml_Serialize(glob
, &sizeSerialized
, sizeof(sizeSerialized
), 1);
604 Ebml_Serialize(glob
, &ui
, sizeof(ui
), 4);
609 Ebml_StartSubElement(EbmlGlobal
*glob
, EbmlLoc
*ebmlLoc
,
610 unsigned long class_id
)
612 /* todo this is always taking 8 bytes, this may need later optimization */
613 /* this is a key that says length unknown */
614 uint64_t unknownLen
= LITERALU64(0x01FFFFFF, 0xFFFFFFFF);
616 Ebml_WriteID(glob
, class_id
);
617 *ebmlLoc
= ftello(glob
->stream
);
618 Ebml_Serialize(glob
, &unknownLen
, sizeof(unknownLen
), 8);
622 Ebml_EndSubElement(EbmlGlobal
*glob
, EbmlLoc
*ebmlLoc
)
627 /* Save the current stream pointer */
628 pos
= ftello(glob
->stream
);
630 /* Calculate the size of this element */
631 size
= pos
- *ebmlLoc
- 8;
632 size
|= LITERALU64(0x01000000,0x00000000);
634 /* Seek back to the beginning of the element and write the new size */
635 fseeko(glob
->stream
, *ebmlLoc
, SEEK_SET
);
636 Ebml_Serialize(glob
, &size
, sizeof(size
), 8);
638 /* Reset the stream pointer */
639 fseeko(glob
->stream
, pos
, SEEK_SET
);
644 write_webm_seek_element(EbmlGlobal
*ebml
, unsigned long id
, off_t pos
)
646 uint64_t offset
= pos
- ebml
->position_reference
;
648 Ebml_StartSubElement(ebml
, &start
, Seek
);
649 Ebml_SerializeBinary(ebml
, SeekID
, id
);
650 Ebml_SerializeUnsigned64(ebml
, SeekPosition
, offset
);
651 Ebml_EndSubElement(ebml
, &start
);
656 write_webm_seek_info(EbmlGlobal
*ebml
)
661 /* Save the current stream pointer */
662 pos
= ftello(ebml
->stream
);
664 if(ebml
->seek_info_pos
)
665 fseeko(ebml
->stream
, ebml
->seek_info_pos
, SEEK_SET
);
667 ebml
->seek_info_pos
= pos
;
672 Ebml_StartSubElement(ebml
, &start
, SeekHead
);
673 write_webm_seek_element(ebml
, Tracks
, ebml
->track_pos
);
674 write_webm_seek_element(ebml
, Cues
, ebml
->cue_pos
);
675 write_webm_seek_element(ebml
, Info
, ebml
->segment_info_pos
);
676 Ebml_EndSubElement(ebml
, &start
);
682 char version_string
[64];
684 /* Assemble version string */
686 strcpy(version_string
, "vpxenc");
689 strcpy(version_string
, "vpxenc ");
690 strncat(version_string
,
691 vpx_codec_version_str(),
692 sizeof(version_string
) - 1 - strlen(version_string
));
695 frame_time
= (uint64_t)1000 * ebml
->framerate
.den
696 / ebml
->framerate
.num
;
697 ebml
->segment_info_pos
= ftello(ebml
->stream
);
698 Ebml_StartSubElement(ebml
, &startInfo
, Info
);
699 Ebml_SerializeUnsigned(ebml
, TimecodeScale
, 1000000);
700 Ebml_SerializeFloat(ebml
, Segment_Duration
,
701 (double)(ebml
->last_pts_ms
+ frame_time
));
702 Ebml_SerializeString(ebml
, 0x4D80, version_string
);
703 Ebml_SerializeString(ebml
, 0x5741, version_string
);
704 Ebml_EndSubElement(ebml
, &startInfo
);
710 write_webm_file_header(EbmlGlobal
*glob
,
711 const vpx_codec_enc_cfg_t
*cfg
,
712 const struct vpx_rational
*fps
,
713 stereo_format_t stereo_fmt
)
717 Ebml_StartSubElement(glob
, &start
, EBML
);
718 Ebml_SerializeUnsigned(glob
, EBMLVersion
, 1);
719 Ebml_SerializeUnsigned(glob
, EBMLReadVersion
, 1);
720 Ebml_SerializeUnsigned(glob
, EBMLMaxIDLength
, 4);
721 Ebml_SerializeUnsigned(glob
, EBMLMaxSizeLength
, 8);
722 Ebml_SerializeString(glob
, DocType
, "webm");
723 Ebml_SerializeUnsigned(glob
, DocTypeVersion
, 2);
724 Ebml_SerializeUnsigned(glob
, DocTypeReadVersion
, 2);
725 Ebml_EndSubElement(glob
, &start
);
728 Ebml_StartSubElement(glob
, &glob
->startSegment
, Segment
);
729 glob
->position_reference
= ftello(glob
->stream
);
730 glob
->framerate
= *fps
;
731 write_webm_seek_info(glob
);
735 glob
->track_pos
= ftello(glob
->stream
);
736 Ebml_StartSubElement(glob
, &trackStart
, Tracks
);
738 unsigned int trackNumber
= 1;
739 uint64_t trackID
= 0;
742 Ebml_StartSubElement(glob
, &start
, TrackEntry
);
743 Ebml_SerializeUnsigned(glob
, TrackNumber
, trackNumber
);
744 glob
->track_id_pos
= ftello(glob
->stream
);
745 Ebml_SerializeUnsigned32(glob
, TrackUID
, trackID
);
746 Ebml_SerializeUnsigned(glob
, TrackType
, 1);
747 Ebml_SerializeString(glob
, CodecID
, "V_VP8");
749 unsigned int pixelWidth
= cfg
->g_w
;
750 unsigned int pixelHeight
= cfg
->g_h
;
751 float frameRate
= (float)fps
->num
/(float)fps
->den
;
754 Ebml_StartSubElement(glob
, &videoStart
, Video
);
755 Ebml_SerializeUnsigned(glob
, PixelWidth
, pixelWidth
);
756 Ebml_SerializeUnsigned(glob
, PixelHeight
, pixelHeight
);
757 Ebml_SerializeUnsigned(glob
, StereoMode
, stereo_fmt
);
758 Ebml_SerializeFloat(glob
, FrameRate
, frameRate
);
759 Ebml_EndSubElement(glob
, &videoStart
);
761 Ebml_EndSubElement(glob
, &start
); /* Track Entry */
763 Ebml_EndSubElement(glob
, &trackStart
);
765 /* segment element is open */
771 write_webm_block(EbmlGlobal
*glob
,
772 const vpx_codec_enc_cfg_t
*cfg
,
773 const vpx_codec_cx_pkt_t
*pkt
)
775 unsigned long block_length
;
776 unsigned char track_number
;
777 unsigned short block_timecode
= 0;
780 int start_cluster
= 0, is_keyframe
;
782 /* Calculate the PTS of this frame in milliseconds */
783 pts_ms
= pkt
->data
.frame
.pts
* 1000
784 * (uint64_t)cfg
->g_timebase
.num
/ (uint64_t)cfg
->g_timebase
.den
;
785 if(pts_ms
<= glob
->last_pts_ms
)
786 pts_ms
= glob
->last_pts_ms
+ 1;
787 glob
->last_pts_ms
= pts_ms
;
789 /* Calculate the relative time of this block */
790 if(pts_ms
- glob
->cluster_timecode
> SHRT_MAX
)
793 block_timecode
= (unsigned short)pts_ms
- glob
->cluster_timecode
;
795 is_keyframe
= (pkt
->data
.frame
.flags
& VPX_FRAME_IS_KEY
);
796 if(start_cluster
|| is_keyframe
)
798 if(glob
->cluster_open
)
799 Ebml_EndSubElement(glob
, &glob
->startCluster
);
801 /* Open the new cluster */
803 glob
->cluster_open
= 1;
804 glob
->cluster_timecode
= (uint32_t)pts_ms
;
805 glob
->cluster_pos
= ftello(glob
->stream
);
806 Ebml_StartSubElement(glob
, &glob
->startCluster
, Cluster
); /* cluster */
807 Ebml_SerializeUnsigned(glob
, Timecode
, glob
->cluster_timecode
);
809 /* Save a cue point if this is a keyframe. */
812 struct cue_entry
*cue
, *new_cue_list
;
814 new_cue_list
= realloc(glob
->cue_list
,
815 (glob
->cues
+1) * sizeof(struct cue_entry
));
817 glob
->cue_list
= new_cue_list
;
819 fatal("Failed to realloc cue list.");
821 cue
= &glob
->cue_list
[glob
->cues
];
822 cue
->time
= glob
->cluster_timecode
;
823 cue
->loc
= glob
->cluster_pos
;
828 /* Write the Simple Block */
829 Ebml_WriteID(glob
, SimpleBlock
);
831 block_length
= (unsigned long)pkt
->data
.frame
.sz
+ 4;
832 block_length
|= 0x10000000;
833 Ebml_Serialize(glob
, &block_length
, sizeof(block_length
), 4);
836 track_number
|= 0x80;
837 Ebml_Write(glob
, &track_number
, 1);
839 Ebml_Serialize(glob
, &block_timecode
, sizeof(block_timecode
), 2);
844 if(pkt
->data
.frame
.flags
& VPX_FRAME_IS_INVISIBLE
)
846 Ebml_Write(glob
, &flags
, 1);
848 Ebml_Write(glob
, pkt
->data
.frame
.buf
, (unsigned long)pkt
->data
.frame
.sz
);
853 write_webm_file_footer(EbmlGlobal
*glob
, long hash
)
856 if(glob
->cluster_open
)
857 Ebml_EndSubElement(glob
, &glob
->startCluster
);
863 glob
->cue_pos
= ftello(glob
->stream
);
864 Ebml_StartSubElement(glob
, &start
, Cues
);
865 for(i
=0; i
<glob
->cues
; i
++)
867 struct cue_entry
*cue
= &glob
->cue_list
[i
];
870 Ebml_StartSubElement(glob
, &start
, CuePoint
);
874 Ebml_SerializeUnsigned(glob
, CueTime
, cue
->time
);
876 Ebml_StartSubElement(glob
, &start
, CueTrackPositions
);
877 Ebml_SerializeUnsigned(glob
, CueTrack
, 1);
878 Ebml_SerializeUnsigned64(glob
, CueClusterPosition
,
879 cue
->loc
- glob
->position_reference
);
880 Ebml_EndSubElement(glob
, &start
);
882 Ebml_EndSubElement(glob
, &start
);
884 Ebml_EndSubElement(glob
, &start
);
887 Ebml_EndSubElement(glob
, &glob
->startSegment
);
889 /* Patch up the seek info block */
890 write_webm_seek_info(glob
);
892 /* Patch up the track id */
893 fseeko(glob
->stream
, glob
->track_id_pos
, SEEK_SET
);
894 Ebml_SerializeUnsigned32(glob
, TrackUID
, glob
->debug
? 0xDEADBEEF : hash
);
896 fseeko(glob
->stream
, 0, SEEK_END
);
900 /* Murmur hash derived from public domain reference implementation at
901 * http://sites.google.com/site/murmurhash/
903 static unsigned int murmur ( const void * key
, int len
, unsigned int seed
)
905 const unsigned int m
= 0x5bd1e995;
908 unsigned int h
= seed
^ len
;
910 const unsigned char * data
= (const unsigned char *)key
;
934 case 3: h
^= data
[2] << 16;
935 case 2: h
^= data
[1] << 8;
936 case 1: h
^= data
[0];
949 static double vp8_mse2psnr(double Samples
, double Peak
, double Mse
)
953 if ((double)Mse
> 0.0)
954 psnr
= 10.0 * log10(Peak
* Peak
* Samples
/ Mse
);
956 psnr
= 60; /* Limit to prevent / 0 */
966 static const arg_def_t debugmode
= ARG_DEF("D", "debug", 0,
967 "Debug mode (makes output deterministic)");
968 static const arg_def_t outputfile
= ARG_DEF("o", "output", 1,
970 static const arg_def_t use_yv12
= ARG_DEF(NULL
, "yv12", 0,
971 "Input file is YV12 ");
972 static const arg_def_t use_i420
= ARG_DEF(NULL
, "i420", 0,
973 "Input file is I420 (default)");
974 static const arg_def_t codecarg
= ARG_DEF(NULL
, "codec", 1,
976 static const arg_def_t passes
= ARG_DEF("p", "passes", 1,
977 "Number of passes (1/2)");
978 static const arg_def_t pass_arg
= ARG_DEF(NULL
, "pass", 1,
979 "Pass to execute (1/2)");
980 static const arg_def_t fpf_name
= ARG_DEF(NULL
, "fpf", 1,
981 "First pass statistics file name");
982 static const arg_def_t limit
= ARG_DEF(NULL
, "limit", 1,
983 "Stop encoding after n input frames");
984 static const arg_def_t deadline
= ARG_DEF("d", "deadline", 1,
985 "Deadline per frame (usec)");
986 static const arg_def_t best_dl
= ARG_DEF(NULL
, "best", 0,
987 "Use Best Quality Deadline");
988 static const arg_def_t good_dl
= ARG_DEF(NULL
, "good", 0,
989 "Use Good Quality Deadline");
990 static const arg_def_t rt_dl
= ARG_DEF(NULL
, "rt", 0,
991 "Use Realtime Quality Deadline");
992 static const arg_def_t quietarg
= ARG_DEF("q", "quiet", 0,
993 "Do not print encode progress");
994 static const arg_def_t verbosearg
= ARG_DEF("v", "verbose", 0,
995 "Show encoder parameters");
996 static const arg_def_t psnrarg
= ARG_DEF(NULL
, "psnr", 0,
997 "Show PSNR in status line");
998 static const arg_def_t framerate
= ARG_DEF(NULL
, "fps", 1,
999 "Stream frame rate (rate/scale)");
1000 static const arg_def_t use_ivf
= ARG_DEF(NULL
, "ivf", 0,
1001 "Output IVF (default is WebM)");
1002 static const arg_def_t out_part
= ARG_DEF("P", "output-partitions", 0,
1003 "Makes encoder output partitions. Requires IVF output!");
1004 static const arg_def_t q_hist_n
= ARG_DEF(NULL
, "q-hist", 1,
1005 "Show quantizer histogram (n-buckets)");
1006 static const arg_def_t rate_hist_n
= ARG_DEF(NULL
, "rate-hist", 1,
1007 "Show rate histogram (n-buckets)");
1008 static const arg_def_t
*main_args
[] =
1011 &outputfile
, &codecarg
, &passes
, &pass_arg
, &fpf_name
, &limit
, &deadline
,
1012 &best_dl
, &good_dl
, &rt_dl
,
1013 &quietarg
, &verbosearg
, &psnrarg
, &use_ivf
, &out_part
, &q_hist_n
, &rate_hist_n
,
1017 static const arg_def_t usage
= ARG_DEF("u", "usage", 1,
1018 "Usage profile number to use");
1019 static const arg_def_t threads
= ARG_DEF("t", "threads", 1,
1020 "Max number of threads to use");
1021 static const arg_def_t profile
= ARG_DEF(NULL
, "profile", 1,
1022 "Bitstream profile number to use");
1023 static const arg_def_t width
= ARG_DEF("w", "width", 1,
1025 static const arg_def_t height
= ARG_DEF("h", "height", 1,
1027 static const struct arg_enum_list stereo_mode_enum
[] = {
1028 {"mono" , STEREO_FORMAT_MONO
},
1029 {"left-right", STEREO_FORMAT_LEFT_RIGHT
},
1030 {"bottom-top", STEREO_FORMAT_BOTTOM_TOP
},
1031 {"top-bottom", STEREO_FORMAT_TOP_BOTTOM
},
1032 {"right-left", STEREO_FORMAT_RIGHT_LEFT
},
1035 static const arg_def_t stereo_mode
= ARG_DEF_ENUM(NULL
, "stereo-mode", 1,
1036 "Stereo 3D video format", stereo_mode_enum
);
1037 static const arg_def_t timebase
= ARG_DEF(NULL
, "timebase", 1,
1038 "Output timestamp precision (fractional seconds)");
1039 static const arg_def_t error_resilient
= ARG_DEF(NULL
, "error-resilient", 1,
1040 "Enable error resiliency features");
1041 static const arg_def_t lag_in_frames
= ARG_DEF(NULL
, "lag-in-frames", 1,
1042 "Max number of frames to lag");
1044 static const arg_def_t
*global_args
[] =
1046 &use_yv12
, &use_i420
, &usage
, &threads
, &profile
,
1047 &width
, &height
, &stereo_mode
, &timebase
, &framerate
, &error_resilient
,
1048 &lag_in_frames
, NULL
1051 static const arg_def_t dropframe_thresh
= ARG_DEF(NULL
, "drop-frame", 1,
1052 "Temporal resampling threshold (buf %)");
1053 static const arg_def_t resize_allowed
= ARG_DEF(NULL
, "resize-allowed", 1,
1054 "Spatial resampling enabled (bool)");
1055 static const arg_def_t resize_up_thresh
= ARG_DEF(NULL
, "resize-up", 1,
1056 "Upscale threshold (buf %)");
1057 static const arg_def_t resize_down_thresh
= ARG_DEF(NULL
, "resize-down", 1,
1058 "Downscale threshold (buf %)");
1059 static const struct arg_enum_list end_usage_enum
[] = {
1065 static const arg_def_t end_usage
= ARG_DEF_ENUM(NULL
, "end-usage", 1,
1066 "Rate control mode", end_usage_enum
);
1067 static const arg_def_t target_bitrate
= ARG_DEF(NULL
, "target-bitrate", 1,
1069 static const arg_def_t min_quantizer
= ARG_DEF(NULL
, "min-q", 1,
1070 "Minimum (best) quantizer");
1071 static const arg_def_t max_quantizer
= ARG_DEF(NULL
, "max-q", 1,
1072 "Maximum (worst) quantizer");
1073 static const arg_def_t undershoot_pct
= ARG_DEF(NULL
, "undershoot-pct", 1,
1074 "Datarate undershoot (min) target (%)");
1075 static const arg_def_t overshoot_pct
= ARG_DEF(NULL
, "overshoot-pct", 1,
1076 "Datarate overshoot (max) target (%)");
1077 static const arg_def_t buf_sz
= ARG_DEF(NULL
, "buf-sz", 1,
1078 "Client buffer size (ms)");
1079 static const arg_def_t buf_initial_sz
= ARG_DEF(NULL
, "buf-initial-sz", 1,
1080 "Client initial buffer size (ms)");
1081 static const arg_def_t buf_optimal_sz
= ARG_DEF(NULL
, "buf-optimal-sz", 1,
1082 "Client optimal buffer size (ms)");
1083 static const arg_def_t
*rc_args
[] =
1085 &dropframe_thresh
, &resize_allowed
, &resize_up_thresh
, &resize_down_thresh
,
1086 &end_usage
, &target_bitrate
, &min_quantizer
, &max_quantizer
,
1087 &undershoot_pct
, &overshoot_pct
, &buf_sz
, &buf_initial_sz
, &buf_optimal_sz
,
1092 static const arg_def_t bias_pct
= ARG_DEF(NULL
, "bias-pct", 1,
1093 "CBR/VBR bias (0=CBR, 100=VBR)");
1094 static const arg_def_t minsection_pct
= ARG_DEF(NULL
, "minsection-pct", 1,
1095 "GOP min bitrate (% of target)");
1096 static const arg_def_t maxsection_pct
= ARG_DEF(NULL
, "maxsection-pct", 1,
1097 "GOP max bitrate (% of target)");
1098 static const arg_def_t
*rc_twopass_args
[] =
1100 &bias_pct
, &minsection_pct
, &maxsection_pct
, NULL
1104 static const arg_def_t kf_min_dist
= ARG_DEF(NULL
, "kf-min-dist", 1,
1105 "Minimum keyframe interval (frames)");
1106 static const arg_def_t kf_max_dist
= ARG_DEF(NULL
, "kf-max-dist", 1,
1107 "Maximum keyframe interval (frames)");
1108 static const arg_def_t kf_disabled
= ARG_DEF(NULL
, "disable-kf", 0,
1109 "Disable keyframe placement");
1110 static const arg_def_t
*kf_args
[] =
1112 &kf_min_dist
, &kf_max_dist
, &kf_disabled
, NULL
1116 #if CONFIG_VP8_ENCODER
1117 static const arg_def_t noise_sens
= ARG_DEF(NULL
, "noise-sensitivity", 1,
1118 "Noise sensitivity (frames to blur)");
1119 static const arg_def_t sharpness
= ARG_DEF(NULL
, "sharpness", 1,
1120 "Filter sharpness (0-7)");
1121 static const arg_def_t static_thresh
= ARG_DEF(NULL
, "static-thresh", 1,
1122 "Motion detection threshold");
1125 #if CONFIG_VP8_ENCODER
1126 static const arg_def_t cpu_used
= ARG_DEF(NULL
, "cpu-used", 1,
1127 "CPU Used (-16..16)");
1131 #if CONFIG_VP8_ENCODER
1132 static const arg_def_t token_parts
= ARG_DEF(NULL
, "token-parts", 1,
1133 "Number of token partitions to use, log2");
1134 static const arg_def_t auto_altref
= ARG_DEF(NULL
, "auto-alt-ref", 1,
1135 "Enable automatic alt reference frames");
1136 static const arg_def_t arnr_maxframes
= ARG_DEF(NULL
, "arnr-maxframes", 1,
1137 "AltRef Max Frames");
1138 static const arg_def_t arnr_strength
= ARG_DEF(NULL
, "arnr-strength", 1,
1140 static const arg_def_t arnr_type
= ARG_DEF(NULL
, "arnr-type", 1,
1142 static const struct arg_enum_list tuning_enum
[] = {
1143 {"psnr", VP8_TUNE_PSNR
},
1144 {"ssim", VP8_TUNE_SSIM
},
1147 static const arg_def_t tune_ssim
= ARG_DEF_ENUM(NULL
, "tune", 1,
1148 "Material to favor", tuning_enum
);
1149 static const arg_def_t cq_level
= ARG_DEF(NULL
, "cq-level", 1,
1150 "Constrained Quality Level");
1151 static const arg_def_t max_intra_rate_pct
= ARG_DEF(NULL
, "max-intra-rate", 1,
1152 "Max I-frame bitrate (pct)");
1154 static const arg_def_t
*vp8_args
[] =
1156 &cpu_used
, &auto_altref
, &noise_sens
, &sharpness
, &static_thresh
,
1157 &token_parts
, &arnr_maxframes
, &arnr_strength
, &arnr_type
,
1158 &tune_ssim
, &cq_level
, &max_intra_rate_pct
, NULL
1160 static const int vp8_arg_ctrl_map
[] =
1162 VP8E_SET_CPUUSED
, VP8E_SET_ENABLEAUTOALTREF
,
1163 VP8E_SET_NOISE_SENSITIVITY
, VP8E_SET_SHARPNESS
, VP8E_SET_STATIC_THRESHOLD
,
1164 VP8E_SET_TOKEN_PARTITIONS
,
1165 VP8E_SET_ARNR_MAXFRAMES
, VP8E_SET_ARNR_STRENGTH
, VP8E_SET_ARNR_TYPE
,
1166 VP8E_SET_TUNING
, VP8E_SET_CQ_LEVEL
, VP8E_SET_MAX_INTRA_BITRATE_PCT
, 0
1170 static const arg_def_t
*no_args
[] = { NULL
};
1172 static void usage_exit()
1176 fprintf(stderr
, "Usage: %s <options> -o dst_filename src_filename \n",
1179 fprintf(stderr
, "\nOptions:\n");
1180 arg_show_usage(stdout
, main_args
);
1181 fprintf(stderr
, "\nEncoder Global Options:\n");
1182 arg_show_usage(stdout
, global_args
);
1183 fprintf(stderr
, "\nRate Control Options:\n");
1184 arg_show_usage(stdout
, rc_args
);
1185 fprintf(stderr
, "\nTwopass Rate Control Options:\n");
1186 arg_show_usage(stdout
, rc_twopass_args
);
1187 fprintf(stderr
, "\nKeyframe Placement Options:\n");
1188 arg_show_usage(stdout
, kf_args
);
1189 #if CONFIG_VP8_ENCODER
1190 fprintf(stderr
, "\nVP8 Specific Options:\n");
1191 arg_show_usage(stdout
, vp8_args
);
1193 fprintf(stderr
, "\nStream timebase (--timebase):\n"
1194 " The desired precision of timestamps in the output, expressed\n"
1195 " in fractional seconds. Default is 1/1000.\n");
1196 fprintf(stderr
, "\n"
1197 "Included encoders:\n"
1200 for (i
= 0; i
< sizeof(codecs
) / sizeof(codecs
[0]); i
++)
1201 fprintf(stderr
, " %-6s - %s\n",
1203 vpx_codec_iface_name(codecs
[i
].iface
));
1209 #define HIST_BAR_MAX 40
1212 int low
, high
, count
;
1216 static int merge_hist_buckets(struct hist_bucket
*bucket
,
1220 int small_bucket
= 0, merge_bucket
= INT_MAX
, big_bucket
=0;
1221 int buckets
= *buckets_
;
1224 /* Find the extrema for this list of buckets */
1225 big_bucket
= small_bucket
= 0;
1226 for(i
=0; i
< buckets
; i
++)
1228 if(bucket
[i
].count
< bucket
[small_bucket
].count
)
1230 if(bucket
[i
].count
> bucket
[big_bucket
].count
)
1234 /* If we have too many buckets, merge the smallest with an adjacent
1237 while(buckets
> max_buckets
)
1239 int last_bucket
= buckets
- 1;
1241 /* merge the small bucket with an adjacent one. */
1242 if(small_bucket
== 0)
1244 else if(small_bucket
== last_bucket
)
1245 merge_bucket
= last_bucket
- 1;
1246 else if(bucket
[small_bucket
- 1].count
< bucket
[small_bucket
+ 1].count
)
1247 merge_bucket
= small_bucket
- 1;
1249 merge_bucket
= small_bucket
+ 1;
1251 assert(abs(merge_bucket
- small_bucket
) <= 1);
1252 assert(small_bucket
< buckets
);
1253 assert(big_bucket
< buckets
);
1254 assert(merge_bucket
< buckets
);
1256 if(merge_bucket
< small_bucket
)
1258 bucket
[merge_bucket
].high
= bucket
[small_bucket
].high
;
1259 bucket
[merge_bucket
].count
+= bucket
[small_bucket
].count
;
1263 bucket
[small_bucket
].high
= bucket
[merge_bucket
].high
;
1264 bucket
[small_bucket
].count
+= bucket
[merge_bucket
].count
;
1265 merge_bucket
= small_bucket
;
1268 assert(bucket
[merge_bucket
].low
!= bucket
[merge_bucket
].high
);
1272 /* Remove the merge_bucket from the list, and find the new small
1273 * and big buckets while we're at it
1275 big_bucket
= small_bucket
= 0;
1276 for(i
=0; i
< buckets
; i
++)
1278 if(i
> merge_bucket
)
1279 bucket
[i
] = bucket
[i
+1];
1281 if(bucket
[i
].count
< bucket
[small_bucket
].count
)
1283 if(bucket
[i
].count
> bucket
[big_bucket
].count
)
1289 *buckets_
= buckets
;
1290 return bucket
[big_bucket
].count
;
1294 static void show_histogram(const struct hist_bucket
*bucket
,
1299 const char *pat1
, *pat2
;
1302 switch((int)(log(bucket
[buckets
-1].high
)/log(10))+1)
1330 pat1
= "%12d %10s: ";
1331 pat2
= "%12d-%10d: ";
1335 for(i
=0; i
<buckets
; i
++)
1341 pct
= (float)(100.0 * bucket
[i
].count
/ total
);
1342 len
= HIST_BAR_MAX
* bucket
[i
].count
/ scale
;
1345 assert(len
<= HIST_BAR_MAX
);
1347 if(bucket
[i
].low
== bucket
[i
].high
)
1348 fprintf(stderr
, pat1
, bucket
[i
].low
, "");
1350 fprintf(stderr
, pat2
, bucket
[i
].low
, bucket
[i
].high
);
1352 for(j
=0; j
<HIST_BAR_MAX
; j
++)
1353 fprintf(stderr
, j
<len
?"=":" ");
1354 fprintf(stderr
, "\t%5d (%6.2f%%)\n",bucket
[i
].count
,pct
);
1359 static void show_q_histogram(const int counts
[64], int max_buckets
)
1361 struct hist_bucket bucket
[64];
1372 bucket
[buckets
].low
= bucket
[buckets
].high
= i
;
1373 bucket
[buckets
].count
= counts
[i
];
1379 fprintf(stderr
, "\nQuantizer Selection:\n");
1380 scale
= merge_hist_buckets(bucket
, &buckets
, max_buckets
);
1381 show_histogram(bucket
, buckets
, total
, scale
);
1385 #define RATE_BINS (100)
1392 struct hist_bucket bucket
[RATE_BINS
];
1397 static void init_rate_histogram(struct rate_hist
*hist
,
1398 const vpx_codec_enc_cfg_t
*cfg
,
1399 const vpx_rational_t
*fps
)
1403 /* Determine the number of samples in the buffer. Use the file's framerate
1404 * to determine the number of frames in rc_buf_sz milliseconds, with an
1405 * adjustment (5/4) to account for alt-refs
1407 hist
->samples
= cfg
->rc_buf_sz
* 5 / 4 * fps
->num
/ fps
->den
/ 1000;
1409 /* prevent division by zero */
1410 if (hist
->samples
== 0)
1413 hist
->pts
= calloc(hist
->samples
, sizeof(*hist
->pts
));
1414 hist
->sz
= calloc(hist
->samples
, sizeof(*hist
->sz
));
1415 for(i
=0; i
<RATE_BINS
; i
++)
1417 hist
->bucket
[i
].low
= INT_MAX
;
1418 hist
->bucket
[i
].high
= 0;
1419 hist
->bucket
[i
].count
= 0;
1424 static void destroy_rate_histogram(struct rate_hist
*hist
)
1431 static void update_rate_histogram(struct rate_hist
*hist
,
1432 const vpx_codec_enc_cfg_t
*cfg
,
1433 const vpx_codec_cx_pkt_t
*pkt
)
1436 int64_t now
, then
, sum_sz
= 0, avg_bitrate
;
1438 now
= pkt
->data
.frame
.pts
* 1000
1439 * (uint64_t)cfg
->g_timebase
.num
/ (uint64_t)cfg
->g_timebase
.den
;
1441 idx
= hist
->frames
++ % hist
->samples
;
1442 hist
->pts
[idx
] = now
;
1443 hist
->sz
[idx
] = (int)pkt
->data
.frame
.sz
;
1445 if(now
< cfg
->rc_buf_initial_sz
)
1450 /* Sum the size over the past rc_buf_sz ms */
1451 for(i
= hist
->frames
; i
> 0 && hist
->frames
- i
< hist
->samples
; i
--)
1453 int i_idx
= (i
-1) % hist
->samples
;
1455 then
= hist
->pts
[i_idx
];
1456 if(now
- then
> cfg
->rc_buf_sz
)
1458 sum_sz
+= hist
->sz
[i_idx
];
1464 avg_bitrate
= sum_sz
* 8 * 1000 / (now
- then
);
1465 idx
= (int)(avg_bitrate
* (RATE_BINS
/2) / (cfg
->rc_target_bitrate
* 1000));
1468 if(idx
> RATE_BINS
-1)
1470 if(hist
->bucket
[idx
].low
> avg_bitrate
)
1471 hist
->bucket
[idx
].low
= (int)avg_bitrate
;
1472 if(hist
->bucket
[idx
].high
< avg_bitrate
)
1473 hist
->bucket
[idx
].high
= (int)avg_bitrate
;
1474 hist
->bucket
[idx
].count
++;
1479 static void show_rate_histogram(struct rate_hist
*hist
,
1480 const vpx_codec_enc_cfg_t
*cfg
,
1486 for(i
= 0; i
< RATE_BINS
; i
++)
1488 if(hist
->bucket
[i
].low
== INT_MAX
)
1490 hist
->bucket
[buckets
++] = hist
->bucket
[i
];
1493 fprintf(stderr
, "\nRate (over %dms window):\n", cfg
->rc_buf_sz
);
1494 scale
= merge_hist_buckets(hist
->bucket
, &buckets
, max_buckets
);
1495 show_histogram(hist
->bucket
, buckets
, hist
->total
, scale
);
1498 #define NELEMENTS(x) (sizeof(x)/sizeof(x[0]))
1499 #define ARG_CTRL_CNT_MAX NELEMENTS(vp8_arg_ctrl_map)
1502 /* Configuration elements common to all streams */
1503 struct global_config
1505 const struct codec_item
*codec
;
1516 struct vpx_rational framerate
;
1519 int show_q_hist_buckets
;
1520 int show_rate_hist_buckets
;
1524 /* Per-stream configuration */
1525 struct stream_config
1527 struct vpx_codec_enc_cfg cfg
;
1529 const char *stats_fn
;
1530 stereo_format_t stereo_fmt
;
1531 int arg_ctrls
[ARG_CTRL_CNT_MAX
][2];
1534 int have_kf_max_dist
;
1541 struct stream_state
*next
;
1542 struct stream_config config
;
1544 struct rate_hist rate_hist
;
1547 uint64_t psnr_sse_total
;
1548 uint64_t psnr_samples_total
;
1549 double psnr_totals
[4];
1552 vpx_codec_ctx_t encoder
;
1553 unsigned int frames_out
;
1560 void validate_positive_rational(const char *msg
,
1561 struct vpx_rational
*rat
)
1570 die("Error: %s must be positive\n", msg
);
1573 die("Error: %s has zero denominator\n", msg
);
1577 static void parse_global_config(struct global_config
*global
, char **argv
)
1579 char **argi
, **argj
;
1582 /* Initialize default parameters */
1583 memset(global
, 0, sizeof(*global
));
1584 global
->codec
= codecs
;
1586 global
->use_i420
= 1;
1588 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
)
1592 if (arg_match(&arg
, &codecarg
, argi
))
1596 for (j
= 0; j
< sizeof(codecs
) / sizeof(codecs
[0]); j
++)
1597 if (!strcmp(codecs
[j
].name
, arg
.val
))
1601 global
->codec
= codecs
+ k
;
1603 die("Error: Unrecognized argument (%s) to --codec\n",
1607 else if (arg_match(&arg
, &passes
, argi
))
1609 global
->passes
= arg_parse_uint(&arg
);
1611 if (global
->passes
< 1 || global
->passes
> 2)
1612 die("Error: Invalid number of passes (%d)\n", global
->passes
);
1614 else if (arg_match(&arg
, &pass_arg
, argi
))
1616 global
->pass
= arg_parse_uint(&arg
);
1618 if (global
->pass
< 1 || global
->pass
> 2)
1619 die("Error: Invalid pass selected (%d)\n",
1622 else if (arg_match(&arg
, &usage
, argi
))
1623 global
->usage
= arg_parse_uint(&arg
);
1624 else if (arg_match(&arg
, &deadline
, argi
))
1625 global
->deadline
= arg_parse_uint(&arg
);
1626 else if (arg_match(&arg
, &best_dl
, argi
))
1627 global
->deadline
= VPX_DL_BEST_QUALITY
;
1628 else if (arg_match(&arg
, &good_dl
, argi
))
1629 global
->deadline
= VPX_DL_GOOD_QUALITY
;
1630 else if (arg_match(&arg
, &rt_dl
, argi
))
1631 global
->deadline
= VPX_DL_REALTIME
;
1632 else if (arg_match(&arg
, &use_yv12
, argi
))
1633 global
->use_i420
= 0;
1634 else if (arg_match(&arg
, &use_i420
, argi
))
1635 global
->use_i420
= 1;
1636 else if (arg_match(&arg
, &quietarg
, argi
))
1638 else if (arg_match(&arg
, &verbosearg
, argi
))
1639 global
->verbose
= 1;
1640 else if (arg_match(&arg
, &limit
, argi
))
1641 global
->limit
= arg_parse_uint(&arg
);
1642 else if (arg_match(&arg
, &psnrarg
, argi
))
1643 global
->show_psnr
= 1;
1644 else if (arg_match(&arg
, &framerate
, argi
))
1646 global
->framerate
= arg_parse_rational(&arg
);
1647 validate_positive_rational(arg
.name
, &global
->framerate
);
1648 global
->have_framerate
= 1;
1650 else if (arg_match(&arg
,&out_part
, argi
))
1651 global
->out_part
= 1;
1652 else if (arg_match(&arg
, &debugmode
, argi
))
1654 else if (arg_match(&arg
, &q_hist_n
, argi
))
1655 global
->show_q_hist_buckets
= arg_parse_uint(&arg
);
1656 else if (arg_match(&arg
, &rate_hist_n
, argi
))
1657 global
->show_rate_hist_buckets
= arg_parse_uint(&arg
);
1662 /* Validate global config */
1666 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
1667 if (global
->pass
> global
->passes
)
1669 warn("Assuming --pass=%d implies --passes=%d\n",
1670 global
->pass
, global
->pass
);
1671 global
->passes
= global
->pass
;
1677 void open_input_file(struct input_state
*input
)
1679 unsigned int fourcc
;
1681 /* Parse certain options from the input file, if possible */
1682 input
->file
= strcmp(input
->fn
, "-") ? fopen(input
->fn
, "rb")
1683 : set_binary_mode(stdin
);
1686 fatal("Failed to open input file");
1688 /* For RAW input sources, these bytes will applied on the first frame
1691 input
->detect
.buf_read
= fread(input
->detect
.buf
, 1, 4, input
->file
);
1692 input
->detect
.position
= 0;
1694 if (input
->detect
.buf_read
== 4
1695 && file_is_y4m(input
->file
, &input
->y4m
, input
->detect
.buf
))
1697 if (y4m_input_open(&input
->y4m
, input
->file
, input
->detect
.buf
, 4) >= 0)
1699 input
->file_type
= FILE_TYPE_Y4M
;
1700 input
->w
= input
->y4m
.pic_w
;
1701 input
->h
= input
->y4m
.pic_h
;
1702 input
->framerate
.num
= input
->y4m
.fps_n
;
1703 input
->framerate
.den
= input
->y4m
.fps_d
;
1704 input
->use_i420
= 0;
1707 fatal("Unsupported Y4M stream.");
1709 else if (input
->detect
.buf_read
== 4 && file_is_ivf(input
, &fourcc
))
1711 input
->file_type
= FILE_TYPE_IVF
;
1715 input
->use_i420
= 0;
1718 input
->use_i420
= 1;
1721 fatal("Unsupported fourcc (%08x) in IVF", fourcc
);
1726 input
->file_type
= FILE_TYPE_RAW
;
1731 static void close_input_file(struct input_state
*input
)
1733 fclose(input
->file
);
1734 if (input
->file_type
== FILE_TYPE_Y4M
)
1735 y4m_input_close(&input
->y4m
);
1738 static struct stream_state
*new_stream(struct global_config
*global
,
1739 struct stream_state
*prev
)
1741 struct stream_state
*stream
;
1743 stream
= calloc(1, sizeof(*stream
));
1745 fatal("Failed to allocate new stream.");
1748 memcpy(stream
, prev
, sizeof(*stream
));
1750 prev
->next
= stream
;
1754 vpx_codec_err_t res
;
1756 /* Populate encoder configuration */
1757 res
= vpx_codec_enc_config_default(global
->codec
->iface
,
1758 &stream
->config
.cfg
,
1761 fatal("Failed to get config: %s\n", vpx_codec_err_to_string(res
));
1763 /* Change the default timebase to a high enough value so that the
1764 * encoder will always create strictly increasing timestamps.
1766 stream
->config
.cfg
.g_timebase
.den
= 1000;
1768 /* Never use the library's default resolution, require it be parsed
1769 * from the file or set on the command line.
1771 stream
->config
.cfg
.g_w
= 0;
1772 stream
->config
.cfg
.g_h
= 0;
1774 /* Initialize remaining stream parameters */
1775 stream
->config
.stereo_fmt
= STEREO_FORMAT_MONO
;
1776 stream
->config
.write_webm
= 1;
1777 stream
->ebml
.last_pts_ms
= -1;
1779 /* Allows removal of the application version from the EBML tags */
1780 stream
->ebml
.debug
= global
->debug
;
1783 /* Output files must be specified for each stream */
1784 stream
->config
.out_fn
= NULL
;
1786 stream
->next
= NULL
;
1791 static int parse_stream_params(struct global_config
*global
,
1792 struct stream_state
*stream
,
1795 char **argi
, **argj
;
1797 static const arg_def_t
**ctrl_args
= no_args
;
1798 static const int *ctrl_args_map
= NULL
;
1799 struct stream_config
*config
= &stream
->config
;
1800 int eos_mark_found
= 0;
1802 /* Handle codec specific options */
1803 if (global
->codec
->iface
== &vpx_codec_vp8_cx_algo
)
1805 ctrl_args
= vp8_args
;
1806 ctrl_args_map
= vp8_arg_ctrl_map
;
1809 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
)
1813 /* Once we've found an end-of-stream marker (--) we want to continue
1814 * shifting arguments but not consuming them.
1821 else if (!strcmp(*argj
, "--"))
1828 else if (arg_match(&arg
, &outputfile
, argi
))
1829 config
->out_fn
= arg
.val
;
1830 else if (arg_match(&arg
, &fpf_name
, argi
))
1831 config
->stats_fn
= arg
.val
;
1832 else if (arg_match(&arg
, &use_ivf
, argi
))
1833 config
->write_webm
= 0;
1834 else if (arg_match(&arg
, &threads
, argi
))
1835 config
->cfg
.g_threads
= arg_parse_uint(&arg
);
1836 else if (arg_match(&arg
, &profile
, argi
))
1837 config
->cfg
.g_profile
= arg_parse_uint(&arg
);
1838 else if (arg_match(&arg
, &width
, argi
))
1839 config
->cfg
.g_w
= arg_parse_uint(&arg
);
1840 else if (arg_match(&arg
, &height
, argi
))
1841 config
->cfg
.g_h
= arg_parse_uint(&arg
);
1842 else if (arg_match(&arg
, &stereo_mode
, argi
))
1843 config
->stereo_fmt
= arg_parse_enum_or_int(&arg
);
1844 else if (arg_match(&arg
, &timebase
, argi
))
1846 config
->cfg
.g_timebase
= arg_parse_rational(&arg
);
1847 validate_positive_rational(arg
.name
, &config
->cfg
.g_timebase
);
1849 else if (arg_match(&arg
, &error_resilient
, argi
))
1850 config
->cfg
.g_error_resilient
= arg_parse_uint(&arg
);
1851 else if (arg_match(&arg
, &lag_in_frames
, argi
))
1852 config
->cfg
.g_lag_in_frames
= arg_parse_uint(&arg
);
1853 else if (arg_match(&arg
, &dropframe_thresh
, argi
))
1854 config
->cfg
.rc_dropframe_thresh
= arg_parse_uint(&arg
);
1855 else if (arg_match(&arg
, &resize_allowed
, argi
))
1856 config
->cfg
.rc_resize_allowed
= arg_parse_uint(&arg
);
1857 else if (arg_match(&arg
, &resize_up_thresh
, argi
))
1858 config
->cfg
.rc_resize_up_thresh
= arg_parse_uint(&arg
);
1859 else if (arg_match(&arg
, &resize_down_thresh
, argi
))
1860 config
->cfg
.rc_resize_down_thresh
= arg_parse_uint(&arg
);
1861 else if (arg_match(&arg
, &end_usage
, argi
))
1862 config
->cfg
.rc_end_usage
= arg_parse_enum_or_int(&arg
);
1863 else if (arg_match(&arg
, &target_bitrate
, argi
))
1864 config
->cfg
.rc_target_bitrate
= arg_parse_uint(&arg
);
1865 else if (arg_match(&arg
, &min_quantizer
, argi
))
1866 config
->cfg
.rc_min_quantizer
= arg_parse_uint(&arg
);
1867 else if (arg_match(&arg
, &max_quantizer
, argi
))
1868 config
->cfg
.rc_max_quantizer
= arg_parse_uint(&arg
);
1869 else if (arg_match(&arg
, &undershoot_pct
, argi
))
1870 config
->cfg
.rc_undershoot_pct
= arg_parse_uint(&arg
);
1871 else if (arg_match(&arg
, &overshoot_pct
, argi
))
1872 config
->cfg
.rc_overshoot_pct
= arg_parse_uint(&arg
);
1873 else if (arg_match(&arg
, &buf_sz
, argi
))
1874 config
->cfg
.rc_buf_sz
= arg_parse_uint(&arg
);
1875 else if (arg_match(&arg
, &buf_initial_sz
, argi
))
1876 config
->cfg
.rc_buf_initial_sz
= arg_parse_uint(&arg
);
1877 else if (arg_match(&arg
, &buf_optimal_sz
, argi
))
1878 config
->cfg
.rc_buf_optimal_sz
= arg_parse_uint(&arg
);
1879 else if (arg_match(&arg
, &bias_pct
, argi
))
1881 config
->cfg
.rc_2pass_vbr_bias_pct
= arg_parse_uint(&arg
);
1883 if (global
->passes
< 2)
1884 warn("option %s ignored in one-pass mode.\n", arg
.name
);
1886 else if (arg_match(&arg
, &minsection_pct
, argi
))
1888 config
->cfg
.rc_2pass_vbr_minsection_pct
= arg_parse_uint(&arg
);
1890 if (global
->passes
< 2)
1891 warn("option %s ignored in one-pass mode.\n", arg
.name
);
1893 else if (arg_match(&arg
, &maxsection_pct
, argi
))
1895 config
->cfg
.rc_2pass_vbr_maxsection_pct
= arg_parse_uint(&arg
);
1897 if (global
->passes
< 2)
1898 warn("option %s ignored in one-pass mode.\n", arg
.name
);
1900 else if (arg_match(&arg
, &kf_min_dist
, argi
))
1901 config
->cfg
.kf_min_dist
= arg_parse_uint(&arg
);
1902 else if (arg_match(&arg
, &kf_max_dist
, argi
))
1904 config
->cfg
.kf_max_dist
= arg_parse_uint(&arg
);
1905 config
->have_kf_max_dist
= 1;
1907 else if (arg_match(&arg
, &kf_disabled
, argi
))
1908 config
->cfg
.kf_mode
= VPX_KF_DISABLED
;
1913 for (i
= 0; ctrl_args
[i
]; i
++)
1915 if (arg_match(&arg
, ctrl_args
[i
], argi
))
1920 /* Point either to the next free element or the first
1921 * instance of this control.
1923 for(j
=0; j
<config
->arg_ctrl_cnt
; j
++)
1924 if(config
->arg_ctrls
[j
][0] == ctrl_args_map
[i
])
1928 assert(j
< ARG_CTRL_CNT_MAX
);
1929 if (j
< ARG_CTRL_CNT_MAX
)
1931 config
->arg_ctrls
[j
][0] = ctrl_args_map
[i
];
1932 config
->arg_ctrls
[j
][1] = arg_parse_enum_or_int(&arg
);
1933 if(j
== config
->arg_ctrl_cnt
)
1934 config
->arg_ctrl_cnt
++;
1945 return eos_mark_found
;
1949 #define FOREACH_STREAM(func)\
1952 struct stream_state *stream;\
1954 for(stream = streams; stream; stream = stream->next)\
1959 static void validate_stream_config(struct stream_state
*stream
)
1961 struct stream_state
*streami
;
1963 if(!stream
->config
.cfg
.g_w
|| !stream
->config
.cfg
.g_h
)
1964 fatal("Stream %d: Specify stream dimensions with --width (-w) "
1965 " and --height (-h)", stream
->index
);
1967 for(streami
= stream
; streami
; streami
= streami
->next
)
1969 /* All streams require output files */
1970 if(!streami
->config
.out_fn
)
1971 fatal("Stream %d: Output file is required (specify with -o)",
1974 /* Check for two streams outputting to the same file */
1975 if(streami
!= stream
)
1977 const char *a
= stream
->config
.out_fn
;
1978 const char *b
= streami
->config
.out_fn
;
1979 if(!strcmp(a
,b
) && strcmp(a
, "/dev/null") && strcmp(a
, ":nul"))
1980 fatal("Stream %d: duplicate output file (from stream %d)",
1981 streami
->index
, stream
->index
);
1984 /* Check for two streams sharing a stats file. */
1985 if(streami
!= stream
)
1987 const char *a
= stream
->config
.stats_fn
;
1988 const char *b
= streami
->config
.stats_fn
;
1989 if(a
&& b
&& !strcmp(a
,b
))
1990 fatal("Stream %d: duplicate stats file (from stream %d)",
1991 streami
->index
, stream
->index
);
1997 static void set_stream_dimensions(struct stream_state
*stream
,
2001 if ((stream
->config
.cfg
.g_w
&& stream
->config
.cfg
.g_w
!= w
)
2002 ||(stream
->config
.cfg
.g_h
&& stream
->config
.cfg
.g_h
!= h
))
2003 fatal("Stream %d: Resizing not yet supported", stream
->index
);
2004 stream
->config
.cfg
.g_w
= w
;
2005 stream
->config
.cfg
.g_h
= h
;
2009 static void set_default_kf_interval(struct stream_state
*stream
,
2010 struct global_config
*global
)
2012 /* Use a max keyframe interval of 5 seconds, if none was
2013 * specified on the command line.
2015 if (!stream
->config
.have_kf_max_dist
)
2017 double framerate
= (double)global
->framerate
.num
/global
->framerate
.den
;
2018 if (framerate
> 0.0)
2019 stream
->config
.cfg
.kf_max_dist
= (unsigned int)(5.0*framerate
);
2024 static void show_stream_config(struct stream_state
*stream
,
2025 struct global_config
*global
,
2026 struct input_state
*input
)
2029 #define SHOW(field) \
2030 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
2032 if(stream
->index
== 0)
2034 fprintf(stderr
, "Codec: %s\n",
2035 vpx_codec_iface_name(global
->codec
->iface
));
2036 fprintf(stderr
, "Source file: %s Format: %s\n", input
->fn
,
2037 input
->use_i420
? "I420" : "YV12");
2039 if(stream
->next
|| stream
->index
)
2040 fprintf(stderr
, "\nStream Index: %d\n", stream
->index
);
2041 fprintf(stderr
, "Destination file: %s\n", stream
->config
.out_fn
);
2042 fprintf(stderr
, "Encoder parameters:\n");
2049 SHOW(g_timebase
.num
);
2050 SHOW(g_timebase
.den
);
2051 SHOW(g_error_resilient
);
2053 SHOW(g_lag_in_frames
);
2054 SHOW(rc_dropframe_thresh
);
2055 SHOW(rc_resize_allowed
);
2056 SHOW(rc_resize_up_thresh
);
2057 SHOW(rc_resize_down_thresh
);
2059 SHOW(rc_target_bitrate
);
2060 SHOW(rc_min_quantizer
);
2061 SHOW(rc_max_quantizer
);
2062 SHOW(rc_undershoot_pct
);
2063 SHOW(rc_overshoot_pct
);
2065 SHOW(rc_buf_initial_sz
);
2066 SHOW(rc_buf_optimal_sz
);
2067 SHOW(rc_2pass_vbr_bias_pct
);
2068 SHOW(rc_2pass_vbr_minsection_pct
);
2069 SHOW(rc_2pass_vbr_maxsection_pct
);
2076 static void open_output_file(struct stream_state
*stream
,
2077 struct global_config
*global
)
2079 const char *fn
= stream
->config
.out_fn
;
2081 stream
->file
= strcmp(fn
, "-") ? fopen(fn
, "wb") : set_binary_mode(stdout
);
2084 fatal("Failed to open output file");
2086 if(stream
->config
.write_webm
&& fseek(stream
->file
, 0, SEEK_CUR
))
2087 fatal("WebM output to pipes not supported.");
2089 if(stream
->config
.write_webm
)
2091 stream
->ebml
.stream
= stream
->file
;
2092 write_webm_file_header(&stream
->ebml
, &stream
->config
.cfg
,
2094 stream
->config
.stereo_fmt
);
2097 write_ivf_file_header(stream
->file
, &stream
->config
.cfg
,
2098 global
->codec
->fourcc
, 0);
2102 static void close_output_file(struct stream_state
*stream
,
2103 unsigned int fourcc
)
2105 if(stream
->config
.write_webm
)
2107 write_webm_file_footer(&stream
->ebml
, stream
->hash
);
2108 free(stream
->ebml
.cue_list
);
2109 stream
->ebml
.cue_list
= NULL
;
2113 if (!fseek(stream
->file
, 0, SEEK_SET
))
2114 write_ivf_file_header(stream
->file
, &stream
->config
.cfg
,
2116 stream
->frames_out
);
2119 fclose(stream
->file
);
2123 static void setup_pass(struct stream_state
*stream
,
2124 struct global_config
*global
,
2127 if (stream
->config
.stats_fn
)
2129 if (!stats_open_file(&stream
->stats
, stream
->config
.stats_fn
,
2131 fatal("Failed to open statistics store");
2135 if (!stats_open_mem(&stream
->stats
, pass
))
2136 fatal("Failed to open statistics store");
2139 stream
->config
.cfg
.g_pass
= global
->passes
== 2
2140 ? pass
? VPX_RC_LAST_PASS
: VPX_RC_FIRST_PASS
2143 stream
->config
.cfg
.rc_twopass_stats_in
= stats_get(&stream
->stats
);
2145 stream
->cx_time
= 0;
2147 stream
->frames_out
= 0;
2151 static void initialize_encoder(struct stream_state
*stream
,
2152 struct global_config
*global
)
2157 flags
|= global
->show_psnr
? VPX_CODEC_USE_PSNR
: 0;
2158 flags
|= global
->out_part
? VPX_CODEC_USE_OUTPUT_PARTITION
: 0;
2160 /* Construct Encoder Context */
2161 vpx_codec_enc_init(&stream
->encoder
, global
->codec
->iface
,
2162 &stream
->config
.cfg
, flags
);
2163 ctx_exit_on_error(&stream
->encoder
, "Failed to initialize encoder");
2165 /* Note that we bypass the vpx_codec_control wrapper macro because
2166 * we're being clever to store the control IDs in an array. Real
2167 * applications will want to make use of the enumerations directly
2169 for (i
= 0; i
< stream
->config
.arg_ctrl_cnt
; i
++)
2171 int ctrl
= stream
->config
.arg_ctrls
[i
][0];
2172 int value
= stream
->config
.arg_ctrls
[i
][1];
2173 if (vpx_codec_control_(&stream
->encoder
, ctrl
, value
))
2174 fprintf(stderr
, "Error: Tried to set control %d = %d\n",
2177 ctx_exit_on_error(&stream
->encoder
, "Failed to control codec");
2182 static void encode_frame(struct stream_state
*stream
,
2183 struct global_config
*global
,
2184 struct vpx_image
*img
,
2185 unsigned int frames_in
)
2187 vpx_codec_pts_t frame_start
, next_frame_start
;
2188 struct vpx_codec_enc_cfg
*cfg
= &stream
->config
.cfg
;
2189 struct vpx_usec_timer timer
;
2191 frame_start
= (cfg
->g_timebase
.den
* (int64_t)(frames_in
- 1)
2192 * global
->framerate
.den
)
2193 / cfg
->g_timebase
.num
/ global
->framerate
.num
;
2194 next_frame_start
= (cfg
->g_timebase
.den
* (int64_t)(frames_in
)
2195 * global
->framerate
.den
)
2196 / cfg
->g_timebase
.num
/ global
->framerate
.num
;
2197 vpx_usec_timer_start(&timer
);
2198 vpx_codec_encode(&stream
->encoder
, img
, frame_start
,
2199 (unsigned long)(next_frame_start
- frame_start
),
2200 0, global
->deadline
);
2201 vpx_usec_timer_mark(&timer
);
2202 stream
->cx_time
+= vpx_usec_timer_elapsed(&timer
);
2203 ctx_exit_on_error(&stream
->encoder
, "Stream %d: Failed to encode frame",
2208 static void update_quantizer_histogram(struct stream_state
*stream
)
2210 if(stream
->config
.cfg
.g_pass
!= VPX_RC_FIRST_PASS
)
2214 vpx_codec_control(&stream
->encoder
, VP8E_GET_LAST_QUANTIZER_64
, &q
);
2215 ctx_exit_on_error(&stream
->encoder
, "Failed to read quantizer");
2216 stream
->counts
[q
]++;
2221 static void get_cx_data(struct stream_state
*stream
,
2222 struct global_config
*global
,
2225 const vpx_codec_cx_pkt_t
*pkt
;
2226 const struct vpx_codec_enc_cfg
*cfg
= &stream
->config
.cfg
;
2227 vpx_codec_iter_t iter
= NULL
;
2229 while ((pkt
= vpx_codec_get_cx_data(&stream
->encoder
, &iter
)))
2231 static size_t fsize
= 0;
2232 static off_t ivf_header_pos
= 0;
2238 case VPX_CODEC_CX_FRAME_PKT
:
2239 if (!(pkt
->data
.frame
.flags
& VPX_FRAME_IS_FRAGMENT
))
2241 stream
->frames_out
++;
2244 fprintf(stderr
, " %6luF",
2245 (unsigned long)pkt
->data
.frame
.sz
);
2247 update_rate_histogram(&stream
->rate_hist
, cfg
, pkt
);
2248 if(stream
->config
.write_webm
)
2250 /* Update the hash */
2251 if(!stream
->ebml
.debug
)
2252 stream
->hash
= murmur(pkt
->data
.frame
.buf
,
2253 (int)pkt
->data
.frame
.sz
,
2256 write_webm_block(&stream
->ebml
, cfg
, pkt
);
2260 if (pkt
->data
.frame
.partition_id
<= 0)
2262 ivf_header_pos
= ftello(stream
->file
);
2263 fsize
= pkt
->data
.frame
.sz
;
2265 write_ivf_frame_header(stream
->file
, pkt
);
2269 fsize
+= pkt
->data
.frame
.sz
;
2271 if (!(pkt
->data
.frame
.flags
& VPX_FRAME_IS_FRAGMENT
))
2273 off_t currpos
= ftello(stream
->file
);
2274 fseeko(stream
->file
, ivf_header_pos
, SEEK_SET
);
2275 write_ivf_frame_size(stream
->file
, fsize
);
2276 fseeko(stream
->file
, currpos
, SEEK_SET
);
2280 (void) fwrite(pkt
->data
.frame
.buf
, 1, pkt
->data
.frame
.sz
,
2283 stream
->nbytes
+= pkt
->data
.raw
.sz
;
2285 case VPX_CODEC_STATS_PKT
:
2286 stream
->frames_out
++;
2287 fprintf(stderr
, " %6luS",
2288 (unsigned long)pkt
->data
.twopass_stats
.sz
);
2289 stats_write(&stream
->stats
,
2290 pkt
->data
.twopass_stats
.buf
,
2291 pkt
->data
.twopass_stats
.sz
);
2292 stream
->nbytes
+= pkt
->data
.raw
.sz
;
2294 case VPX_CODEC_PSNR_PKT
:
2296 if (global
->show_psnr
)
2300 stream
->psnr_sse_total
+= pkt
->data
.psnr
.sse
[0];
2301 stream
->psnr_samples_total
+= pkt
->data
.psnr
.samples
[0];
2302 for (i
= 0; i
< 4; i
++)
2305 fprintf(stderr
, "%.3f ", pkt
->data
.psnr
.psnr
[i
]);
2306 stream
->psnr_totals
[i
] += pkt
->data
.psnr
.psnr
[i
];
2308 stream
->psnr_count
++;
2319 static void show_psnr(struct stream_state
*stream
)
2324 if (!stream
->psnr_count
)
2327 fprintf(stderr
, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream
->index
);
2328 ovpsnr
= vp8_mse2psnr((double)stream
->psnr_samples_total
, 255.0,
2329 (double)stream
->psnr_sse_total
);
2330 fprintf(stderr
, " %.3f", ovpsnr
);
2332 for (i
= 0; i
< 4; i
++)
2334 fprintf(stderr
, " %.3f", stream
->psnr_totals
[i
]/stream
->psnr_count
);
2336 fprintf(stderr
, "\n");
2340 float usec_to_fps(uint64_t usec
, unsigned int frames
)
2342 return (float)(usec
> 0 ? frames
* 1000000.0 / (float)usec
: 0);
2346 int main(int argc
, const char **argv_
)
2350 int frame_avail
, got_data
;
2352 struct input_state input
= {0};
2353 struct global_config global
;
2354 struct stream_state
*streams
= NULL
;
2355 char **argv
, **argi
;
2356 unsigned long cx_time
= 0;
2359 exec_name
= argv_
[0];
2364 /* Setup default input stream settings */
2365 input
.framerate
.num
= 30;
2366 input
.framerate
.den
= 1;
2369 /* First parse the global configuration values, because we want to apply
2370 * other parameters on top of the default configuration provided by the
2373 argv
= argv_dup(argc
- 1, argv_
+ 1);
2374 parse_global_config(&global
, argv
);
2377 /* Now parse each stream's parameters. Using a local scope here
2378 * due to the use of 'stream' as loop variable in FOREACH_STREAM
2381 struct stream_state
*stream
= NULL
;
2385 stream
= new_stream(&global
, stream
);
2389 } while(parse_stream_params(&global
, stream
, argv
));
2392 /* Check for unrecognized options */
2393 for (argi
= argv
; *argi
; argi
++)
2394 if (argi
[0][0] == '-' && argi
[0][1])
2395 die("Error: Unrecognized option %s\n", *argi
);
2397 /* Handle non-option arguments */
2403 for (pass
= global
.pass
? global
.pass
- 1 : 0; pass
< global
.passes
; pass
++)
2407 open_input_file(&input
);
2409 /* If the input file doesn't specify its w/h (raw files), try to get
2410 * the data from the first stream's configuration.
2412 if(!input
.w
|| !input
.h
)
2414 if(stream
->config
.cfg
.g_w
&& stream
->config
.cfg
.g_h
)
2416 input
.w
= stream
->config
.cfg
.g_w
;
2417 input
.h
= stream
->config
.cfg
.g_h
;
2422 /* Update stream configurations from the input file's parameters */
2423 FOREACH_STREAM(set_stream_dimensions(stream
, input
.w
, input
.h
));
2424 FOREACH_STREAM(validate_stream_config(stream
));
2426 /* Ensure that --passes and --pass are consistent. If --pass is set and
2427 * --passes=2, ensure --fpf was set.
2429 if (global
.pass
&& global
.passes
== 2)
2431 if(!stream
->config
.stats_fn
)
2432 die("Stream %d: Must specify --fpf when --pass=%d"
2433 " and --passes=2\n", stream
->index
, global
.pass
);
2437 /* Use the frame rate from the file only if none was specified
2438 * on the command-line.
2440 if (!global
.have_framerate
)
2441 global
.framerate
= input
.framerate
;
2443 FOREACH_STREAM(set_default_kf_interval(stream
, &global
));
2445 /* Show configuration */
2446 if (global
.verbose
&& pass
== 0)
2447 FOREACH_STREAM(show_stream_config(stream
, &global
, &input
));
2449 if(pass
== (global
.pass
? global
.pass
- 1 : 0)) {
2450 if (input
.file_type
== FILE_TYPE_Y4M
)
2451 /*The Y4M reader does its own allocation.
2452 Just initialize this here to avoid problems if we never read any
2454 memset(&raw
, 0, sizeof(raw
));
2457 input
.use_i420
? VPX_IMG_FMT_I420
2459 input
.w
, input
.h
, 32);
2461 FOREACH_STREAM(init_rate_histogram(&stream
->rate_hist
,
2462 &stream
->config
.cfg
,
2463 &global
.framerate
));
2466 FOREACH_STREAM(open_output_file(stream
, &global
));
2467 FOREACH_STREAM(setup_pass(stream
, &global
, pass
));
2468 FOREACH_STREAM(initialize_encoder(stream
, &global
));
2473 while (frame_avail
|| got_data
)
2475 struct vpx_usec_timer timer
;
2477 if (!global
.limit
|| frames_in
< global
.limit
)
2479 frame_avail
= read_frame(&input
, &raw
);
2488 "\rPass %d/%d frame %4d/%-4d %7"PRId64
"B \033[K",
2489 pass
+ 1, global
.passes
, frames_in
,
2490 streams
->frames_out
, (int64_t)streams
->nbytes
);
2493 "\rPass %d/%d frame %4d %7lu %s (%.2f fps)\033[K",
2494 pass
+ 1, global
.passes
, frames_in
,
2495 cx_time
> 9999999 ? cx_time
/ 1000 : cx_time
,
2496 cx_time
> 9999999 ? "ms" : "us",
2497 usec_to_fps(cx_time
, frames_in
));
2504 vpx_usec_timer_start(&timer
);
2505 FOREACH_STREAM(encode_frame(stream
, &global
,
2506 frame_avail
? &raw
: NULL
,
2508 vpx_usec_timer_mark(&timer
);
2509 cx_time
+= (unsigned long)vpx_usec_timer_elapsed(&timer
);
2511 FOREACH_STREAM(update_quantizer_histogram(stream
));
2514 FOREACH_STREAM(get_cx_data(stream
, &global
, &got_data
));
2520 fprintf(stderr
, "\n");
2523 FOREACH_STREAM(fprintf(
2525 "\rPass %d/%d frame %4d/%-4d %7"PRId64
"B %7lub/f %7"PRId64
"b/s"
2526 " %7"PRId64
" %s (%.2f fps)\033[K\n", pass
+ 1,
2527 global
.passes
, frames_in
, stream
->frames_out
, (int64_t)stream
->nbytes
,
2528 frames_in
? (unsigned long)(stream
->nbytes
* 8 / frames_in
) : 0,
2529 frames_in
? (int64_t)stream
->nbytes
* 8
2530 * (int64_t)global
.framerate
.num
/ global
.framerate
.den
2533 stream
->cx_time
> 9999999 ? stream
->cx_time
/ 1000 : stream
->cx_time
,
2534 stream
->cx_time
> 9999999 ? "ms" : "us",
2535 usec_to_fps(stream
->cx_time
, frames_in
));
2538 if (global
.show_psnr
)
2539 FOREACH_STREAM(show_psnr(stream
));
2541 FOREACH_STREAM(vpx_codec_destroy(&stream
->encoder
));
2543 close_input_file(&input
);
2545 FOREACH_STREAM(close_output_file(stream
, global
.codec
->fourcc
));
2547 FOREACH_STREAM(stats_close(&stream
->stats
, global
.passes
-1));
2553 if (global
.show_q_hist_buckets
)
2554 FOREACH_STREAM(show_q_histogram(stream
->counts
,
2555 global
.show_q_hist_buckets
));
2557 if (global
.show_rate_hist_buckets
)
2558 FOREACH_STREAM(show_rate_histogram(&stream
->rate_hist
,
2559 &stream
->config
.cfg
,
2560 global
.show_rate_hist_buckets
));
2561 FOREACH_STREAM(destroy_rate_histogram(&stream
->rate_hist
));
2566 return EXIT_SUCCESS
;