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) {
40 kDenoiserOnYUVAggressive,
44 static int mode_to_num_layers[13] = {1, 2, 2, 3, 3, 3, 3, 5, 2, 3, 3, 3, 3};
47 struct RateControlMetrics {
66 double avg_st_encoding_bitrate;
68 double variance_st_encoding_bitrate;
82 static void set_rate_control_metrics(
struct RateControlMetrics *rc,
89 rc->layer_pfb[0] = 1000.0 * rc->layer_target_bitrate[0] /
90 rc->layer_framerate[0];
94 rc->layer_pfb[i] = 1000.0 *
95 (rc->layer_target_bitrate[i] - rc->layer_target_bitrate[i - 1]) /
96 (rc->layer_framerate[i] - rc->layer_framerate[i - 1]);
98 rc->layer_input_frames[i] = 0;
99 rc->layer_enc_frames[i] = 0;
100 rc->layer_tot_enc_frames[i] = 0;
101 rc->layer_encoding_bitrate[i] = 0.0;
102 rc->layer_avg_frame_size[i] = 0.0;
103 rc->layer_avg_rate_mismatch[i] = 0.0;
105 rc->window_count = 0;
106 rc->window_size = 15;
107 rc->avg_st_encoding_bitrate = 0.0;
108 rc->variance_st_encoding_bitrate = 0.0;
111 static void printout_rate_control_summary(
struct RateControlMetrics *rc,
115 int tot_num_frames = 0;
116 double perc_fluctuation = 0.0;
117 printf(
"Total number of processed frames: %d\n\n", frame_cnt -1);
118 printf(
"Rate control layer stats for %d layer(s):\n\n",
121 const int num_dropped = (i > 0) ?
122 (rc->layer_input_frames[i] - rc->layer_enc_frames[i]) :
123 (rc->layer_input_frames[i] - rc->layer_enc_frames[i] - 1);
124 tot_num_frames += rc->layer_input_frames[i];
125 rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[i] *
126 rc->layer_encoding_bitrate[i] / tot_num_frames;
127 rc->layer_avg_frame_size[i] = rc->layer_avg_frame_size[i] /
128 rc->layer_enc_frames[i];
129 rc->layer_avg_rate_mismatch[i] = 100.0 * rc->layer_avg_rate_mismatch[i] /
130 rc->layer_enc_frames[i];
131 printf(
"For layer#: %d \n", i);
132 printf(
"Bitrate (target vs actual): %d %f \n", rc->layer_target_bitrate[i],
133 rc->layer_encoding_bitrate[i]);
134 printf(
"Average frame size (target vs actual): %f %f \n", rc->layer_pfb[i],
135 rc->layer_avg_frame_size[i]);
136 printf(
"Average rate_mismatch: %f \n", rc->layer_avg_rate_mismatch[i]);
137 printf(
"Number of input frames, encoded (non-key) frames, " 138 "and perc dropped frames: %d %d %f \n", rc->layer_input_frames[i],
139 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,
152 sqrt(rc->variance_st_encoding_bitrate),
154 if ((frame_cnt - 1) != tot_num_frames)
155 die(
"Error: Number of input frames not equal to output! \n");
163 static void set_temporal_layer_pattern(
int layering_mode,
166 int *flag_periodicity) {
167 switch (layering_mode) {
172 *flag_periodicity = 1;
185 *flag_periodicity = 2;
207 int ids[3] = {0, 1, 1};
209 *flag_periodicity = 3;
224 int ids[6] = {0, 2, 2, 1, 2, 2};
226 *flag_periodicity = 6;
245 int ids[4] = {0, 2, 1, 2};
247 *flag_periodicity = 4;
265 int ids[4] = {0, 2, 1, 2};
267 *flag_periodicity = 4;
286 int ids[4] = {0, 2, 1, 2};
288 *flag_periodicity = 4;
306 int ids[16] = {0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4};
308 *flag_periodicity = 16;
339 *flag_periodicity = 8;
361 layer_flags[4] = layer_flags[2];
363 layer_flags[5] = layer_flags[3];
365 layer_flags[6] = layer_flags[4];
367 layer_flags[7] = layer_flags[5];
372 int ids[4] = {0, 2, 1, 2};
374 *flag_periodicity = 8;
402 int ids[4] = {0, 2, 1, 2};
404 *flag_periodicity = 8;
428 layer_flags[5] = layer_flags[3];
432 layer_flags[7] = layer_flags[3];
441 int ids[4] = {0, 2, 1, 2};
443 *flag_periodicity = 4;
464 int ids[4] = {0, 2, 1, 2};
466 *flag_periodicity = 8;
476 layer_flags[4] = layer_flags[0];
479 layer_flags[6] = layer_flags[2];
483 layer_flags[3] = layer_flags[1];
484 layer_flags[5] = layer_flags[1];
485 layer_flags[7] = layer_flags[1];
491 int main(
int argc,
char **argv) {
506 int frame_duration = 1;
507 int layering_mode = 0;
509 int flag_periodicity = 1;
510 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION) 515 const VpxInterface *encoder = NULL;
517 struct RateControlMetrics rc;
519 const int min_args_base = 11;
520 #if CONFIG_VP9_HIGHBITDEPTH 522 int input_bit_depth = 8;
523 const int min_args = min_args_base + 1;
525 const int min_args = min_args_base;
526 #endif // CONFIG_VP9_HIGHBITDEPTH 527 double sum_bitrate = 0.0;
528 double sum_bitrate2 = 0.0;
529 double framerate = 30.0;
533 if (argc < min_args) {
534 #if CONFIG_VP9_HIGHBITDEPTH 535 die(
"Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> " 536 "<rate_num> <rate_den> <speed> <frame_drop_threshold> <mode> " 537 "<Rate_0> ... <Rate_nlayers-1> <bit-depth> \n", argv[0]);
539 die(
"Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> " 540 "<rate_num> <rate_den> <speed> <frame_drop_threshold> <mode> " 541 "<Rate_0> ... <Rate_nlayers-1> \n", argv[0]);
542 #endif // CONFIG_VP9_HIGHBITDEPTH 545 encoder = get_vpx_encoder_by_name(argv[3]);
547 die(
"Unsupported codec.");
551 width = strtol(argv[4], NULL, 0);
552 height = strtol(argv[5], NULL, 0);
553 if (width < 16 || width % 2 || height < 16 || height % 2) {
554 die(
"Invalid resolution: %d x %d", width, height);
557 layering_mode = strtol(argv[10], NULL, 0);
558 if (layering_mode < 0 || layering_mode > 13) {
559 die(
"Invalid layering mode (0..12) %s", argv[10]);
562 if (argc != min_args + mode_to_num_layers[layering_mode]) {
563 die(
"Invalid number of arguments");
566 #if CONFIG_VP9_HIGHBITDEPTH 567 switch (strtol(argv[argc-1], NULL, 0)) {
574 input_bit_depth = 10;
578 input_bit_depth = 12;
581 die(
"Invalid bit depth (8, 10, 12) %s", argv[argc-1]);
586 width, height, 32)) {
587 die(
"Failed to allocate image", width, height);
591 die(
"Failed to allocate image", width, height);
593 #endif // CONFIG_VP9_HIGHBITDEPTH 606 #if CONFIG_VP9_HIGHBITDEPTH 612 #endif // CONFIG_VP9_HIGHBITDEPTH 618 speed = strtol(argv[8], NULL, 0);
620 die(
"Invalid speed setting: must be positive");
623 for (i = min_args_base;
624 (int)i < min_args_base + mode_to_num_layers[layering_mode];
626 rc.layer_target_bitrate[i - 11] = strtol(argv[i], NULL, 0);
627 if (strncmp(encoder->name,
"vp8", 3) == 0)
629 else if (strncmp(encoder->name,
"vp9", 3) == 0)
638 if (strncmp(encoder->name,
"vp9", 3) == 0)
662 set_temporal_layer_pattern(layering_mode,
667 set_rate_control_metrics(&rc, &cfg);
674 if (!(infile = fopen(argv[1],
"rb"))) {
675 die(
"Failed to open %s for reading", argv[1]);
681 char file_name[PATH_MAX];
683 info.codec_fourcc = encoder->fourcc;
684 info.frame_width = cfg.
g_w;
685 info.frame_height = cfg.
g_h;
689 snprintf(file_name,
sizeof(file_name),
"%s_%d.ivf", argv[2], i);
690 outfile[i] = vpx_video_writer_open(file_name, kContainerIVF, &info);
692 die(
"Failed to open %s for writing", file_name);
694 assert(outfile[i] != NULL);
700 #if CONFIG_VP9_HIGHBITDEPTH 702 &codec, encoder->codec_interface(), &cfg,
706 #endif // CONFIG_VP9_HIGHBITDEPTH 707 die_codec(&codec,
"Failed to initialize encoder");
709 if (strncmp(encoder->name,
"vp8", 3) == 0) {
713 }
else if (strncmp(encoder->name,
"vp9", 3) == 0) {
723 die_codec(&codec,
"Failed to set SVC");
732 if (strncmp(encoder->name,
"vp8", 3) == 0) {
740 const int max_intra_size_pct = 900;
746 while (frame_avail || got_data) {
747 struct vpx_usec_timer timer;
750 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION) 756 if (strncmp(encoder->name,
"vp9", 3) == 0) {
758 }
else if (strncmp(encoder->name,
"vp8", 3) == 0) {
762 flags = layer_flags[frame_cnt % flag_periodicity];
763 if (layering_mode == 0)
765 frame_avail = vpx_img_read(&raw, infile);
768 vpx_usec_timer_start(&timer);
771 die_codec(&codec,
"Failed to encode frame");
773 vpx_usec_timer_mark(&timer);
774 cx_time += vpx_usec_timer_elapsed(&timer);
776 if (layering_mode != 7) {
786 vpx_video_writer_write_frame(outfile[i], pkt->
data.
frame.buf,
788 ++rc.layer_tot_enc_frames[i];
789 rc.layer_encoding_bitrate[i] += 8.0 * pkt->
data.
frame.sz;
793 rc.layer_avg_frame_size[i] += 8.0 * pkt->
data.
frame.sz;
794 rc.layer_avg_rate_mismatch[i] +=
795 fabs(8.0 * pkt->
data.
frame.sz - rc.layer_pfb[i]) /
797 ++rc.layer_enc_frames[i];
803 if (frame_cnt > rc.window_size) {
804 sum_bitrate += 0.001 * 8.0 * pkt->
data.
frame.sz * framerate;
805 if (frame_cnt % rc.window_size == 0) {
806 rc.window_count += 1;
807 rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
808 rc.variance_st_encoding_bitrate +=
809 (sum_bitrate / rc.window_size) *
810 (sum_bitrate / rc.window_size);
815 if (frame_cnt > rc.window_size + rc.window_size / 2) {
816 sum_bitrate2 += 0.001 * 8.0 * pkt->
data.
frame.sz * framerate;
817 if (frame_cnt > 2 * rc.window_size &&
818 frame_cnt % rc.window_size == 0) {
819 rc.window_count += 1;
820 rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
821 rc.variance_st_encoding_bitrate +=
822 (sum_bitrate2 / rc.window_size) *
823 (sum_bitrate2 / rc.window_size);
833 pts += frame_duration;
836 printout_rate_control_summary(&rc, &cfg, frame_cnt);
838 printf(
"Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
840 1000 * (
float)cx_time / (
double)(frame_cnt * 1000000),
841 1000000 * (
double)frame_cnt / (
double)cx_time);
844 die_codec(&codec,
"Failed to destroy codec");
848 vpx_video_writer_close(outfile[i]);
unsigned int rc_buf_initial_sz
Decoder Buffer Initial Size.
Definition: vpx_encoder.h:610
int min_quantizers[12]
Definition: vpx_encoder.h:773
unsigned int ts_number_layers
Number of temporal coding layers.
Definition: vpx_encoder.h:715
Codec control function to set encoder internal speed settings.
Definition: vp8cx.h:164
#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:92
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:458
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:397
Definition: vpx_encoder.h:276
Codec control function to set noise sensitivity.
Definition: vp8cx.h:423
unsigned int layer_target_bitrate[12]
Target bitrate for each spatial/temporal layer.
Definition: vpx_encoder.h:755
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition: vpx_encoder.h:600
#define VP8_EFLAG_NO_REF_GF
Don't reference the golden frame.
Definition: vp8cx.h:67
unsigned int g_input_bit_depth
Bit-depth of the input frames.
Definition: vpx_encoder.h:383
enum vpx_kf_mode kf_mode
Keyframe placement mode.
Definition: vpx_encoder.h:665
int den
Definition: vpx_encoder.h:261
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:552
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition: vpx_encoder.h:541
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: vpx_encoder.h:685
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: vpx_encoder.h:429
Encoder configuration structure.
Definition: vpx_encoder.h:314
Definition: vpx_encoder.h:292
int spatial_layer_id
Definition: vp8cx.h:685
Codec control function to set Max data rate for Intra frames.
Definition: vp8cx.h:260
#define VPX_CODEC_USE_HIGHBITDEPTH
Definition: vpx_encoder.h:101
Encoder output packet.
Definition: vpx_encoder.h:195
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition: vpx_encoder.h:583
Codec control function to set parameters for SVC.
Definition: vp8cx.h:440
unsigned int ts_rate_decimator[5]
Frame rate decimation factor for each temporal layer.
Definition: vpx_encoder.h:729
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition: vpx_encoder.h:620
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: vpx_encoder.h:675
unsigned int g_profile
Bitstream profile to use.
Definition: vpx_encoder.h:346
Codec control function to set number of tile columns.
Definition: vp8cx.h:353
unsigned int ts_layer_id[16]
Template defining the membership of frames to temporal layers.
Definition: vpx_encoder.h:747
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
int scaling_factor_num[12]
Definition: vpx_encoder.h:774
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:357
unsigned int ts_target_bitrate[5]
Target bitrate for each temporal layer.
Definition: vpx_encoder.h:722
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:570
Codec control function to set adaptive quantization mode.
Definition: vp8cx.h:400
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:367
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:196
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: vpx_encoder.h:452
vp9 svc layer parameters
Definition: vp8cx.h:684
Codec control function to set the temporal layer id.
Definition: vp8cx.h:307
#define VP8_EFLAG_NO_UPD_LAST
Don't update the last frame.
Definition: vp8cx.h:84
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:197
unsigned int rc_target_bitrate
Target data rate.
Definition: vpx_encoder.h:525
#define VPX_DL_REALTIME
Definition: vpx_encoder.h:911
int num
Definition: vpx_encoder.h:260
control function to set noise sensitivity
Definition: vp8cx.h:179
Definition: vpx_codec.h:222
unsigned int g_threads
Maximum number of threads to use.
Definition: vpx_encoder.h:335
unsigned int ss_number_layers
Number of spatial coding layers.
Definition: vpx_encoder.h:695
vpx_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition: vpx_encoder.h:375
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:813
Codec control function to set encoder screen content mode.
Definition: vp8cx.h:315
unsigned int rc_resize_allowed
Enable/disable spatial resampling, if supported by the codec.
Definition: vpx_encoder.h:462
Bypass mode. Used when application needs to control temporal layering. This will only work when the n...
Definition: vp8cx.h:592
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:763
int temporal_layer_id
Definition: vp8cx.h:686
Codec control function to enable/disable periodic Q boost.
Definition: vp8cx.h:415
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:432
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition: vpx_codec.h:407
unsigned int ts_periodicity
Length of the sequence defining frame temporal layer membership.
Definition: vpx_encoder.h:738
#define VP8_EFLAG_NO_REF_ARF
Don't reference the alternate reference frame.
Definition: vp8cx.h:76
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
Definition: vpx_codec.h:220
int scaling_factor_den[12]
Definition: vpx_encoder.h:775
Codec control function to set the threshold for MBs treated static.
Definition: vp8cx.h:191
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:130
Definition: vpx_codec.h:221
#define VPX_EFLAG_FORCE_KF
Definition: vpx_encoder.h:305
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:188
Definition: vpx_encoder.h:176
int max_quantizers[12]
Definition: vpx_encoder.h:772
vp9 svc extra configure parameters
Definition: vpx_encoder.h:771
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: vpx_encoder.h:406
#define VP8_EFLAG_NO_UPD_ARF
Don't update the alternate reference frame.
Definition: vp8cx.h:100
#define VP8_EFLAG_NO_UPD_ENTROPY
Disable entropy update.
Definition: vp8cx.h:124
Codec control function to set svc layer for spatial and temporal.
Definition: vp8cx.h:449
enum vpx_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: vpx_encoder.h:504
Codec context structure.
Definition: vpx_codec.h:199