21 #include "./vpx_config.h" 22 #include "../vpx_ports/vpx_timer.h" 26 #include "../tools_common.h" 27 #include "../video_writer.h" 29 static const char *exec_name;
31 void usage_exit(
void) { exit(EXIT_FAILURE); }
38 kDenoiserOnYUVAggressive,
42 static int mode_to_num_layers[13] = { 1, 2, 2, 3, 3, 3, 3, 5, 2, 3, 3, 3, 3 };
45 struct RateControlMetrics {
64 double avg_st_encoding_bitrate;
66 double variance_st_encoding_bitrate;
80 static void set_rate_control_metrics(
struct RateControlMetrics *rc,
88 1000.0 * rc->layer_target_bitrate[0] / rc->layer_framerate[0];
92 rc->layer_pfb[i] = 1000.0 * (rc->layer_target_bitrate[i] -
93 rc->layer_target_bitrate[i - 1]) /
94 (rc->layer_framerate[i] - rc->layer_framerate[i - 1]);
96 rc->layer_input_frames[i] = 0;
97 rc->layer_enc_frames[i] = 0;
98 rc->layer_tot_enc_frames[i] = 0;
99 rc->layer_encoding_bitrate[i] = 0.0;
100 rc->layer_avg_frame_size[i] = 0.0;
101 rc->layer_avg_rate_mismatch[i] = 0.0;
103 rc->window_count = 0;
104 rc->window_size = 15;
105 rc->avg_st_encoding_bitrate = 0.0;
106 rc->variance_st_encoding_bitrate = 0.0;
109 static void printout_rate_control_summary(
struct RateControlMetrics *rc,
113 int tot_num_frames = 0;
114 double perc_fluctuation = 0.0;
115 printf(
"Total number of processed frames: %d\n\n", frame_cnt - 1);
116 printf(
"Rate control layer stats for %d layer(s):\n\n",
119 const int num_dropped =
120 (i > 0) ? (rc->layer_input_frames[i] - rc->layer_enc_frames[i])
121 : (rc->layer_input_frames[i] - rc->layer_enc_frames[i] - 1);
122 tot_num_frames += rc->layer_input_frames[i];
123 rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[i] *
124 rc->layer_encoding_bitrate[i] /
126 rc->layer_avg_frame_size[i] =
127 rc->layer_avg_frame_size[i] / rc->layer_enc_frames[i];
128 rc->layer_avg_rate_mismatch[i] =
129 100.0 * rc->layer_avg_rate_mismatch[i] / rc->layer_enc_frames[i];
130 printf(
"For layer#: %d \n", i);
131 printf(
"Bitrate (target vs actual): %d %f \n", rc->layer_target_bitrate[i],
132 rc->layer_encoding_bitrate[i]);
133 printf(
"Average frame size (target vs actual): %f %f \n", rc->layer_pfb[i],
134 rc->layer_avg_frame_size[i]);
135 printf(
"Average rate_mismatch: %f \n", rc->layer_avg_rate_mismatch[i]);
137 "Number of input frames, encoded (non-key) frames, " 138 "and perc dropped frames: %d %d %f \n",
139 rc->layer_input_frames[i], rc->layer_enc_frames[i],
140 100.0 * num_dropped / rc->layer_input_frames[i]);
143 rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
144 rc->variance_st_encoding_bitrate =
145 rc->variance_st_encoding_bitrate / rc->window_count -
146 (rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
147 perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
148 rc->avg_st_encoding_bitrate;
149 printf(
"Short-time stats, for window of %d frames: \n", rc->window_size);
150 printf(
"Average, rms-variance, and percent-fluct: %f %f %f \n",
151 rc->avg_st_encoding_bitrate, sqrt(rc->variance_st_encoding_bitrate),
153 if ((frame_cnt - 1) != tot_num_frames)
154 die(
"Error: Number of input frames not equal to output! \n");
162 static void set_temporal_layer_pattern(
int layering_mode,
165 int *flag_periodicity) {
166 switch (layering_mode) {
171 *flag_periodicity = 1;
182 int ids[2] = { 0, 1 };
184 *flag_periodicity = 2;
208 int ids[3] = { 0, 1, 1 };
210 *flag_periodicity = 3;
219 layer_flags[1] = layer_flags[2] =
226 int ids[6] = { 0, 2, 2, 1, 2, 2 };
228 *flag_periodicity = 6;
240 layer_flags[1] = layer_flags[2] = layer_flags[4] = layer_flags[5] =
246 int ids[4] = { 0, 2, 1, 2 };
248 *flag_periodicity = 4;
260 layer_flags[1] = layer_flags[3] =
267 int ids[4] = { 0, 2, 1, 2 };
269 *flag_periodicity = 4;
282 layer_flags[1] = layer_flags[3] =
289 int ids[4] = { 0, 2, 1, 2 };
291 *flag_periodicity = 4;
303 layer_flags[1] = layer_flags[3] =
310 int ids[16] = { 0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4 };
312 *flag_periodicity = 16;
321 layer_flags[1] = layer_flags[3] = layer_flags[5] = layer_flags[7] =
322 layer_flags[9] = layer_flags[11] = layer_flags[13] = layer_flags[15] =
325 layer_flags[2] = layer_flags[6] = layer_flags[10] = layer_flags[14] =
327 layer_flags[4] = layer_flags[12] =
334 int ids[2] = { 0, 1 };
336 *flag_periodicity = 8;
358 layer_flags[4] = layer_flags[2];
360 layer_flags[5] = layer_flags[3];
362 layer_flags[6] = layer_flags[4];
364 layer_flags[7] = layer_flags[5];
369 int ids[4] = { 0, 2, 1, 2 };
371 *flag_periodicity = 8;
385 layer_flags[3] = layer_flags[5] =
400 int ids[4] = { 0, 2, 1, 2 };
402 *flag_periodicity = 8;
426 layer_flags[5] = layer_flags[3];
430 layer_flags[7] = layer_flags[3];
439 int ids[4] = { 0, 2, 1, 2 };
441 *flag_periodicity = 4;
462 int ids[4] = { 0, 2, 1, 2 };
464 *flag_periodicity = 8;
474 layer_flags[4] = layer_flags[0];
477 layer_flags[6] = layer_flags[2];
481 layer_flags[3] = layer_flags[1];
482 layer_flags[5] = layer_flags[1];
483 layer_flags[7] = layer_flags[1];
489 int main(
int argc,
char **argv) {
504 int frame_duration = 1;
505 int layering_mode = 0;
507 int flag_periodicity = 1;
508 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION) 513 const VpxInterface *encoder = NULL;
515 struct RateControlMetrics rc;
517 const int min_args_base = 12;
518 #if CONFIG_VP9_HIGHBITDEPTH 520 int input_bit_depth = 8;
521 const int min_args = min_args_base + 1;
523 const int min_args = min_args_base;
524 #endif // CONFIG_VP9_HIGHBITDEPTH 525 double sum_bitrate = 0.0;
526 double sum_bitrate2 = 0.0;
527 double framerate = 30.0;
531 if (argc < min_args) {
532 #if CONFIG_VP9_HIGHBITDEPTH 533 die(
"Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> " 534 "<rate_num> <rate_den> <speed> <frame_drop_threshold> <threads> <mode> " 535 "<Rate_0> ... <Rate_nlayers-1> <bit-depth> \n",
538 die(
"Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> " 539 "<rate_num> <rate_den> <speed> <frame_drop_threshold> <threads> <mode> " 540 "<Rate_0> ... <Rate_nlayers-1> \n",
542 #endif // CONFIG_VP9_HIGHBITDEPTH 545 encoder = get_vpx_encoder_by_name(argv[3]);
546 if (!encoder) die(
"Unsupported codec.");
550 width = (
unsigned int)strtoul(argv[4], NULL, 0);
551 height = (
unsigned int)strtoul(argv[5], NULL, 0);
552 if (width < 16 || width % 2 || height < 16 || height % 2) {
553 die(
"Invalid resolution: %d x %d", width, height);
556 layering_mode = (int)strtol(argv[11], NULL, 0);
557 if (layering_mode < 0 || layering_mode > 13) {
558 die(
"Invalid layering mode (0..12) %s", argv[11]);
561 if (argc != min_args + mode_to_num_layers[layering_mode]) {
562 die(
"Invalid number of arguments");
565 #if CONFIG_VP9_HIGHBITDEPTH 566 switch (strtol(argv[argc - 1], NULL, 0)) {
573 input_bit_depth = 10;
577 input_bit_depth = 12;
579 default: die(
"Invalid bit depth (8, 10, 12) %s", argv[argc - 1]);
583 width, height, 32)) {
584 die(
"Failed to allocate image", width, height);
588 die(
"Failed to allocate image", width, height);
590 #endif // CONFIG_VP9_HIGHBITDEPTH 603 #if CONFIG_VP9_HIGHBITDEPTH 609 #endif // CONFIG_VP9_HIGHBITDEPTH 615 speed = (int)strtol(argv[8], NULL, 0);
617 die(
"Invalid speed setting: must be positive");
620 for (i = min_args_base;
621 (int)i < min_args_base + mode_to_num_layers[layering_mode]; ++i) {
622 rc.layer_target_bitrate[i - 12] = (int)strtol(argv[i], NULL, 0);
623 if (strncmp(encoder->name,
"vp8", 3) == 0)
625 else if (strncmp(encoder->name,
"vp9", 3) == 0)
645 cfg.
g_threads = (
unsigned int)strtoul(argv[10], NULL, 0);
657 set_temporal_layer_pattern(layering_mode, &cfg, layer_flags,
660 set_rate_control_metrics(&rc, &cfg);
667 if (!(infile = fopen(argv[1],
"rb"))) {
668 die(
"Failed to open %s for reading", argv[1]);
674 char file_name[PATH_MAX];
676 info.codec_fourcc = encoder->fourcc;
677 info.frame_width = cfg.
g_w;
678 info.frame_height = cfg.
g_h;
682 snprintf(file_name,
sizeof(file_name),
"%s_%d.ivf", argv[2], i);
683 outfile[i] = vpx_video_writer_open(file_name, kContainerIVF, &info);
684 if (!outfile[i]) die(
"Failed to open %s for writing", file_name);
686 assert(outfile[i] != NULL);
692 #if CONFIG_VP9_HIGHBITDEPTH 694 &codec, encoder->codec_interface(), &cfg,
698 #endif // CONFIG_VP9_HIGHBITDEPTH 699 die_codec(&codec,
"Failed to initialize encoder");
701 if (strncmp(encoder->name,
"vp8", 3) == 0) {
706 }
else if (strncmp(encoder->name,
"vp9", 3) == 0) {
708 memset(&svc_params, 0,
sizeof(svc_params));
719 die_codec(&codec,
"Failed to set SVC");
728 if (strncmp(encoder->name,
"vp8", 3) == 0) {
736 const int max_intra_size_pct = 900;
742 while (frame_avail || got_data) {
743 struct vpx_usec_timer timer;
746 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION) 752 if (strncmp(encoder->name,
"vp9", 3) == 0) {
754 }
else if (strncmp(encoder->name,
"vp8", 3) == 0) {
758 flags = layer_flags[frame_cnt % flag_periodicity];
759 if (layering_mode == 0) flags = 0;
760 frame_avail = vpx_img_read(&raw, infile);
762 vpx_usec_timer_start(&timer);
765 die_codec(&codec,
"Failed to encode frame");
767 vpx_usec_timer_mark(&timer);
768 cx_time += vpx_usec_timer_elapsed(&timer);
770 if (layering_mode != 7) {
780 vpx_video_writer_write_frame(outfile[i], pkt->
data.
frame.buf,
782 ++rc.layer_tot_enc_frames[i];
783 rc.layer_encoding_bitrate[i] += 8.0 * pkt->
data.
frame.sz;
787 rc.layer_avg_frame_size[i] += 8.0 * pkt->
data.
frame.sz;
788 rc.layer_avg_rate_mismatch[i] +=
789 fabs(8.0 * pkt->
data.
frame.sz - rc.layer_pfb[i]) /
791 ++rc.layer_enc_frames[i];
797 if (frame_cnt > rc.window_size) {
798 sum_bitrate += 0.001 * 8.0 * pkt->
data.
frame.sz * framerate;
799 if (frame_cnt % rc.window_size == 0) {
800 rc.window_count += 1;
801 rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
802 rc.variance_st_encoding_bitrate +=
803 (sum_bitrate / rc.window_size) *
804 (sum_bitrate / rc.window_size);
809 if (frame_cnt > rc.window_size + rc.window_size / 2) {
810 sum_bitrate2 += 0.001 * 8.0 * pkt->
data.
frame.sz * framerate;
811 if (frame_cnt > 2 * rc.window_size &&
812 frame_cnt % rc.window_size == 0) {
813 rc.window_count += 1;
814 rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
815 rc.variance_st_encoding_bitrate +=
816 (sum_bitrate2 / rc.window_size) *
817 (sum_bitrate2 / rc.window_size);
826 pts += frame_duration;
829 printout_rate_control_summary(&rc, &cfg, frame_cnt);
831 printf(
"Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
832 frame_cnt, 1000 * (
float)cx_time / (
double)(frame_cnt * 1000000),
833 1000000 * (
double)frame_cnt / (
double)cx_time);
838 for (i = 0; i < cfg.
ts_number_layers; ++i) vpx_video_writer_close(outfile[i]);
unsigned int rc_buf_initial_sz
Decoder Buffer Initial Size.
Definition: vpx_encoder.h:559
int min_quantizers[12]
Definition: vpx_encoder.h:715
unsigned int ts_number_layers
Number of temporal coding layers.
Definition: vpx_encoder.h:656
Codec control function to set encoder internal speed settings.
Definition: vp8cx.h:155
#define VPX_MAX_LAYERS
Definition: vpx_encoder.h:46
#define VP8_EFLAG_NO_REF_LAST
Don't reference the last frame.
Definition: vp8cx.h:58
#define VP8_EFLAG_NO_UPD_GF
Don't update the golden frame.
Definition: vp8cx.h:88
Image Descriptor.
Definition: vpx_image.h:88
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.
const char * vpx_codec_err_to_string(vpx_codec_err_t err)
Convert error number to printable string.
#define VPX_TS_MAX_LAYERS
Definition: vpx_encoder.h:40
Codec control function to set content type.
Definition: vp8cx.h:449
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:363
Definition: vpx_encoder.h:250
Codec control function to set noise sensitivity.
Definition: vp8cx.h:414
unsigned int layer_target_bitrate[12]
Target bitrate for each spatial/temporal layer.
Definition: vpx_encoder.h:696
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition: vpx_encoder.h:550
#define VP8_EFLAG_NO_REF_GF
Don't reference the golden frame.
Definition: vp8cx.h:66
unsigned int g_input_bit_depth
Bit-depth of the input frames.
Definition: vpx_encoder.h:349
enum vpx_kf_mode kf_mode
Keyframe placement mode.
Definition: vpx_encoder.h:608
int den
Definition: vpx_encoder.h:237
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:507
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition: vpx_encoder.h:497
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: vpx_encoder.h:626
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: vpx_encoder.h:392
Encoder configuration structure.
Definition: vpx_encoder.h:285
Definition: vpx_encoder.h:265
int spatial_layer_id
Definition: vp8cx.h:699
Codec control function to set Max data rate for Intra frames.
Definition: vp8cx.h:251
#define VPX_CODEC_USE_HIGHBITDEPTH
Definition: vpx_encoder.h:100
Encoder output packet.
Definition: vpx_encoder.h:175
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition: vpx_encoder.h:535
Codec control function to set parameters for SVC.
Definition: vp8cx.h:431
unsigned int ts_rate_decimator[5]
Frame rate decimation factor for each temporal layer.
Definition: vpx_encoder.h:670
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition: vpx_encoder.h:568
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: vpx_encoder.h:617
unsigned int g_profile
Bitstream profile to use.
Definition: vpx_encoder.h:315
Codec control function to set number of tile columns.
Definition: vp8cx.h:344
unsigned int ts_layer_id[16]
Template defining the membership of frames to temporal layers.
Definition: vpx_encoder.h:688
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:55
int scaling_factor_num[12]
Definition: vpx_encoder.h:716
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:324
unsigned int ts_target_bitrate[5]
Target bitrate for each temporal layer.
Definition: vpx_encoder.h:663
enum vpx_bit_depth vpx_bit_depth_t
Bit depth for codecThis enumeration determines the bit depth of the codec.
unsigned int rc_undershoot_pct
Rate control adaptation undershoot control.
Definition: vpx_encoder.h:523
Codec control function to set adaptive quantization mode.
Definition: vp8cx.h:391
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:333
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:176
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: vpx_encoder.h:414
Boost percentage for Golden Frame in CBR mode.
Definition: vp8cx.h:579
vp9 svc layer parameters
Definition: vp8cx.h:698
Codec control function to set the temporal layer id.
Definition: vp8cx.h:298
#define VP8_EFLAG_NO_UPD_LAST
Don't update the last frame.
Definition: vp8cx.h:81
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.
Codec control function to set the number of token partitions.
Definition: vp8cx.h:188
unsigned int rc_target_bitrate
Target data rate.
Definition: vpx_encoder.h:483
#define VPX_DL_REALTIME
deadline parameter analogous to VPx REALTIME mode.
Definition: vpx_encoder.h:842
int num
Definition: vpx_encoder.h:236
control function to set noise sensitivity
Definition: vp8cx.h:170
Definition: vpx_codec.h:215
Boost percentage for Golden Frame in CBR mode.
Definition: vp8cx.h:287
unsigned int g_threads
Maximum number of threads to use.
Definition: vpx_encoder.h:305
unsigned int ss_number_layers
Number of spatial coding layers.
Definition: vpx_encoder.h:636
vpx_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition: vpx_encoder.h:341
Provides definitions for using VP8 or VP9 encoder algorithm within the vpx Codec Interface.
#define vpx_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_enc_init_ver()
Definition: vpx_encoder.h:753
Codec control function to set encoder screen content mode.
Definition: vp8cx.h:306
unsigned int rc_resize_allowed
Enable/disable spatial resampling, if supported by the codec.
Definition: vpx_encoder.h:423
Bypass mode. Used when application needs to control temporal layering. This will only work when the n...
Definition: vp8cx.h:609
vpx_codec_err_t
Algorithm return codes.
Definition: vpx_codec.h:89
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
int temporal_layering_mode
Temporal layering mode indicating which temporal layering scheme to use.
Definition: vpx_encoder.h:705
int temporal_layer_id
Definition: vp8cx.h:700
Codec control function to enable/disable periodic Q boost.
Definition: vp8cx.h:406
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, unsigned int reserved)
Get a default configuration.
#define VPX_TS_MAX_PERIODICITY
Definition: vpx_encoder.h:37
Codec control function to turn on/off SVC in encoder.
Definition: vp8cx.h:423
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition: vpx_codec.h:399
unsigned int ts_periodicity
Length of the sequence defining frame temporal layer membership.
Definition: vpx_encoder.h:679
#define VP8_EFLAG_NO_REF_ARF
Don't reference the alternate reference frame.
Definition: vp8cx.h:74
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
Codec control function to enable frame parallel decoding feature.
Definition: vp8cx.h:378
Definition: vpx_codec.h:213
int scaling_factor_den[12]
Definition: vpx_encoder.h:717
Codec control function to set the threshold for MBs treated static.
Definition: vp8cx.h:182
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:126
Definition: vpx_codec.h:214
#define VPX_EFLAG_FORCE_KF
Definition: vpx_encoder.h:277
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:182
Definition: vpx_encoder.h:157
int max_quantizers[12]
Definition: vpx_encoder.h:714
vp9 svc extra configure parameters
Definition: vpx_encoder.h:713
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: vpx_encoder.h:371
#define VP8_EFLAG_NO_UPD_ARF
Don't update the alternate reference frame.
Definition: vp8cx.h:95
#define VP8_EFLAG_NO_UPD_ENTROPY
Disable entropy update.
Definition: vp8cx.h:116
Codec control function to set svc layer for spatial and temporal.
Definition: vp8cx.h:440
enum vpx_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: vpx_encoder.h:463
Codec context structure.
Definition: vpx_codec.h:192