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;
40 kDenoiserOnYUVAggressive,
44 static int mode_to_num_layers[12] = {1, 2, 2, 3, 3, 3, 3, 5, 2, 3, 3, 3};
47 struct RateControlMetrics {
66 double avg_st_encoding_bitrate;
68 double variance_st_encoding_bitrate;
81 static void set_rate_control_metrics(
struct RateControlMetrics *rc,
89 rc->layer_framerate[0];
93 rc->layer_pfb[i] = 1000.0 *
95 (rc->layer_framerate[i] - rc->layer_framerate[i - 1]);
97 rc->layer_input_frames[i] = 0;
98 rc->layer_enc_frames[i] = 0;
99 rc->layer_tot_enc_frames[i] = 0;
100 rc->layer_encoding_bitrate[i] = 0.0;
101 rc->layer_avg_frame_size[i] = 0.0;
102 rc->layer_avg_rate_mismatch[i] = 0.0;
104 rc->window_count = 0;
105 rc->window_size = 15;
106 rc->avg_st_encoding_bitrate = 0.0;
107 rc->variance_st_encoding_bitrate = 0.0;
110 static void printout_rate_control_summary(
struct RateControlMetrics *rc,
114 int tot_num_frames = 0;
115 double perc_fluctuation = 0.0;
116 printf(
"Total number of processed frames: %d\n\n", frame_cnt -1);
117 printf(
"Rate control layer stats for %d layer(s):\n\n",
120 const int num_dropped = (i > 0) ?
121 (rc->layer_input_frames[i] - rc->layer_enc_frames[i]) :
122 (rc->layer_input_frames[i] - rc->layer_enc_frames[i] - 1);
123 tot_num_frames += rc->layer_input_frames[i];
124 rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[i] *
125 rc->layer_encoding_bitrate[i] / tot_num_frames;
126 rc->layer_avg_frame_size[i] = rc->layer_avg_frame_size[i] /
127 rc->layer_enc_frames[i];
128 rc->layer_avg_rate_mismatch[i] = 100.0 * rc->layer_avg_rate_mismatch[i] /
129 rc->layer_enc_frames[i];
130 printf(
"For layer#: %d \n", 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]);
136 printf(
"Number of input frames, encoded (non-key) frames, "
137 "and perc dropped frames: %d %d %f \n", rc->layer_input_frames[i],
138 rc->layer_enc_frames[i],
139 100.0 * num_dropped / rc->layer_input_frames[i]);
142 rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
143 rc->variance_st_encoding_bitrate =
144 rc->variance_st_encoding_bitrate / rc->window_count -
145 (rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
146 perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
147 rc->avg_st_encoding_bitrate;
148 printf(
"Short-time stats, for window of %d frames: \n",rc->window_size);
149 printf(
"Average, rms-variance, and percent-fluct: %f %f %f \n",
150 rc->avg_st_encoding_bitrate,
151 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;
184 *flag_periodicity = 2;
206 int ids[3] = {0, 1, 1};
208 *flag_periodicity = 3;
223 int ids[6] = {0, 2, 2, 1, 2, 2};
225 *flag_periodicity = 6;
244 int ids[4] = {0, 2, 1, 2};
246 *flag_periodicity = 4;
264 int ids[4] = {0, 2, 1, 2};
266 *flag_periodicity = 4;
285 int ids[4] = {0, 2, 1, 2};
287 *flag_periodicity = 4;
305 int ids[16] = {0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4};
307 *flag_periodicity = 16;
338 *flag_periodicity = 8;
360 layer_flags[4] = layer_flags[2];
362 layer_flags[5] = layer_flags[3];
364 layer_flags[6] = layer_flags[4];
366 layer_flags[7] = layer_flags[5];
371 int ids[4] = {0, 2, 1, 2};
373 *flag_periodicity = 8;
401 int ids[4] = {0, 2, 1, 2};
403 *flag_periodicity = 8;
427 layer_flags[5] = layer_flags[3];
431 layer_flags[7] = layer_flags[3];
438 int ids[4] = {0, 2, 1, 2};
440 *flag_periodicity = 8;
450 layer_flags[4] = layer_flags[0];
453 layer_flags[6] = layer_flags[2];
457 layer_flags[3] = layer_flags[1];
458 layer_flags[5] = layer_flags[1];
459 layer_flags[7] = layer_flags[1];
465 int main(
int argc,
char **argv) {
480 int frame_duration = 1;
481 int layering_mode = 0;
483 int flag_periodicity = 1;
484 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION)
489 const VpxInterface *encoder = NULL;
491 struct RateControlMetrics rc;
493 const int min_args_base = 11;
494 #if CONFIG_VP9_HIGHBITDEPTH
496 int input_bit_depth = 8;
497 const int min_args = min_args_base + 1;
499 const int min_args = min_args_base;
500 #endif // CONFIG_VP9_HIGHBITDEPTH
501 double sum_bitrate = 0.0;
502 double sum_bitrate2 = 0.0;
503 double framerate = 30.0;
507 if (argc < min_args) {
508 #if CONFIG_VP9_HIGHBITDEPTH
509 die(
"Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> "
510 "<rate_num> <rate_den> <speed> <frame_drop_threshold> <mode> "
511 "<Rate_0> ... <Rate_nlayers-1> <bit-depth> \n", argv[0]);
513 die(
"Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> "
514 "<rate_num> <rate_den> <speed> <frame_drop_threshold> <mode> "
515 "<Rate_0> ... <Rate_nlayers-1> \n", argv[0]);
516 #endif // CONFIG_VP9_HIGHBITDEPTH
519 encoder = get_vpx_encoder_by_name(argv[3]);
521 die(
"Unsupported codec.");
525 width = strtol(argv[4], NULL, 0);
526 height = strtol(argv[5], NULL, 0);
527 if (width < 16 || width % 2 || height < 16 || height % 2) {
528 die(
"Invalid resolution: %d x %d", width, height);
531 layering_mode = strtol(argv[10], NULL, 0);
532 if (layering_mode < 0 || layering_mode > 12) {
533 die(
"Invalid layering mode (0..12) %s", argv[10]);
536 if (argc != min_args + mode_to_num_layers[layering_mode]) {
537 die(
"Invalid number of arguments");
540 #if CONFIG_VP9_HIGHBITDEPTH
541 switch (strtol(argv[argc-1], NULL, 0)) {
548 input_bit_depth = 10;
552 input_bit_depth = 12;
555 die(
"Invalid bit depth (8, 10, 12) %s", argv[argc-1]);
560 width, height, 32)) {
561 die(
"Failed to allocate image", width, height);
565 die(
"Failed to allocate image", width, height);
567 #endif // CONFIG_VP9_HIGHBITDEPTH
580 #if CONFIG_VP9_HIGHBITDEPTH
586 #endif // CONFIG_VP9_HIGHBITDEPTH
592 speed = strtol(argv[8], NULL, 0);
594 die(
"Invalid speed setting: must be positive");
597 for (i = min_args_base;
598 (int)i < min_args_base + mode_to_num_layers[layering_mode];
609 if (strncmp(encoder->name,
"vp9", 3) == 0)
628 set_temporal_layer_pattern(layering_mode,
633 set_rate_control_metrics(&rc, &cfg);
640 if (!(infile = fopen(argv[1],
"rb"))) {
641 die(
"Failed to open %s for reading", argv[1]);
647 char file_name[PATH_MAX];
649 info.codec_fourcc = encoder->fourcc;
650 info.frame_width = cfg.
g_w;
651 info.frame_height = cfg.
g_h;
655 snprintf(file_name,
sizeof(file_name),
"%s_%d.ivf", argv[2], i);
656 outfile[i] = vpx_video_writer_open(file_name, kContainerIVF, &info);
658 die(
"Failed to open %s for writing", file_name);
660 assert(outfile[i] != NULL);
666 #if CONFIG_VP9_HIGHBITDEPTH
668 &codec, encoder->codec_interface(), &cfg,
672 #endif // CONFIG_VP9_HIGHBITDEPTH
673 die_codec(&codec,
"Failed to initialize encoder");
675 if (strncmp(encoder->name,
"vp8", 3) == 0) {
679 }
else if (strncmp(encoder->name,
"vp9", 3) == 0) {
687 die_codec(&codec,
"Failed to set SVC");
690 if (strncmp(encoder->name,
"vp8", 3) == 0) {
698 const int max_intra_size_pct = 900;
704 while (frame_avail || got_data) {
705 struct vpx_usec_timer timer;
708 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION)
710 layer_id.spatial_layer_id = 0;
714 if (strncmp(encoder->name,
"vp9", 3) == 0) {
716 }
else if (strncmp(encoder->name,
"vp8", 3) == 0) {
720 flags = layer_flags[frame_cnt % flag_periodicity];
721 if (layering_mode == 0)
723 frame_avail = vpx_img_read(&raw, infile);
726 vpx_usec_timer_start(&timer);
729 die_codec(&codec,
"Failed to encode frame");
731 vpx_usec_timer_mark(&timer);
732 cx_time += vpx_usec_timer_elapsed(&timer);
734 if (layering_mode != 7) {
744 vpx_video_writer_write_frame(outfile[i], pkt->
data.
frame.buf,
746 ++rc.layer_tot_enc_frames[i];
747 rc.layer_encoding_bitrate[i] += 8.0 * pkt->
data.
frame.sz;
751 rc.layer_avg_frame_size[i] += 8.0 * pkt->
data.
frame.sz;
752 rc.layer_avg_rate_mismatch[i] +=
753 fabs(8.0 * pkt->
data.
frame.sz - rc.layer_pfb[i]) /
755 ++rc.layer_enc_frames[i];
761 if (frame_cnt > rc.window_size) {
762 sum_bitrate += 0.001 * 8.0 * pkt->
data.
frame.sz * framerate;
763 if (frame_cnt % rc.window_size == 0) {
764 rc.window_count += 1;
765 rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
766 rc.variance_st_encoding_bitrate +=
767 (sum_bitrate / rc.window_size) *
768 (sum_bitrate / rc.window_size);
773 if (frame_cnt > rc.window_size + rc.window_size / 2) {
774 sum_bitrate2 += 0.001 * 8.0 * pkt->
data.
frame.sz * framerate;
775 if (frame_cnt > 2 * rc.window_size &&
776 frame_cnt % rc.window_size == 0) {
777 rc.window_count += 1;
778 rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
779 rc.variance_st_encoding_bitrate +=
780 (sum_bitrate2 / rc.window_size) *
781 (sum_bitrate2 / rc.window_size);
791 pts += frame_duration;
794 printout_rate_control_summary(&rc, &cfg, frame_cnt);
796 printf(
"Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
798 1000 * (
float)cx_time / (
double)(frame_cnt * 1000000),
799 1000000 * (
double)frame_cnt / (
double)cx_time);
802 die_codec(&codec,
"Failed to destroy codec");
806 vpx_video_writer_close(outfile[i]);
unsigned int rc_buf_initial_sz
Decoder Buffer Initial Size.
Definition: vpx_encoder.h:607
unsigned int ts_number_layers
Number of temporal coding layers.
Definition: vpx_encoder.h:712
Codec control function to set encoder internal speed settings.
Definition: vp8cx.h:183
#define VP8_EFLAG_NO_REF_LAST
Don't reference the last frame.
Definition: vp8cx.h:59
#define VP8_EFLAG_NO_UPD_GF
Don't update the golden frame.
Definition: vp8cx.h:93
Image Descriptor.
Definition: vpx_image.h:82
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
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:394
Definition: vpx_encoder.h:273
Codec control function to set noise sensitivity.
Definition: vp8cx.h:440
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition: vpx_encoder.h:597
#define VP8_EFLAG_NO_REF_GF
Don't reference the golden frame.
Definition: vp8cx.h:68
unsigned int g_input_bit_depth
Bit-depth of the input frames.
Definition: vpx_encoder.h:380
enum vpx_kf_mode kf_mode
Keyframe placement mode.
Definition: vpx_encoder.h:662
int den
Definition: vpx_encoder.h:258
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:549
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition: vpx_encoder.h:538
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: vpx_encoder.h:682
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: vpx_encoder.h:426
Encoder configuration structure.
Definition: vpx_encoder.h:311
Definition: vpx_encoder.h:289
Codec control function to set Max data rate for Intra frames.
Definition: vp8cx.h:279
#define VPX_CODEC_USE_HIGHBITDEPTH
Definition: vpx_encoder.h:98
Encoder output packet.
Definition: vpx_encoder.h:192
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition: vpx_encoder.h:580
unsigned int ts_rate_decimator[5]
Frame rate decimation factor for each temporal layer.
Definition: vpx_encoder.h:726
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition: vpx_encoder.h:617
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: vpx_encoder.h:672
unsigned int g_profile
Bitstream profile to use.
Definition: vpx_encoder.h:343
Codec control function to set number of tile columns.
Definition: vp8cx.h:370
unsigned int ts_layer_id[16]
Template defining the membership of frames to temporal layers.
Definition: vpx_encoder.h:744
struct vpx_codec_cx_pkt::@1::@2 frame
vpx_image_t * vpx_img_alloc(vpx_image_t *img, vpx_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
Definition: vpx_image.h:56
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:354
unsigned int ts_target_bitrate[5]
Target bitrate for each temporal layer.
Definition: vpx_encoder.h:719
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:567
Codec control function to set adaptive quantization mode.
Definition: vp8cx.h:417
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:364
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:193
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: vpx_encoder.h:449
vp9 svc layer parameters
Definition: vp8cx.h:619
Codec control function to set the temporal layer id.
Definition: vp8cx.h:326
#define VP8_EFLAG_NO_UPD_LAST
Don't update the last frame.
Definition: vp8cx.h:85
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:216
unsigned int rc_target_bitrate
Target data rate.
Definition: vpx_encoder.h:522
#define VPX_DL_REALTIME
Definition: vpx_encoder.h:891
int num
Definition: vpx_encoder.h:257
control function to set noise sensitivity
Definition: vp8cx.h:198
Definition: vpx_codec.h:222
unsigned int g_threads
Maximum number of threads to use.
Definition: vpx_encoder.h:332
unsigned int ss_number_layers
Number of spatial coding layers.
Definition: vpx_encoder.h:692
vpx_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition: vpx_encoder.h:372
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:793
Codec control function to set encoder screen content mode.
Definition: vp8cx.h:332
unsigned int rc_resize_allowed
Enable/disable spatial resampling, if supported by the codec.
Definition: vpx_encoder.h:459
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_layer_id
Definition: vp8cx.h:620
Codec control function to enable/disable periodic Q boost.
Definition: vp8cx.h:432
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:449
#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:735
#define VP8_EFLAG_NO_REF_ARF
Don't reference the alternate reference frame.
Definition: vp8cx.h:77
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
Definition: vpx_codec.h:220
Codec control function to set the threshold for MBs treated static.
Definition: vp8cx.h:210
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:127
Definition: vpx_codec.h:221
#define VPX_EFLAG_FORCE_KF
Definition: vpx_encoder.h:302
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:188
Definition: vpx_encoder.h:173
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: vpx_encoder.h:403
#define VP8_EFLAG_NO_UPD_ARF
Don't update the alternate reference frame.
Definition: vp8cx.h:101
#define VP8_EFLAG_NO_UPD_ENTROPY
Disable entropy update.
Definition: vp8cx.h:125
Codec control function to set svc layer for spatial and temporal.
Definition: vp8cx.h:468
enum vpx_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: vpx_encoder.h:501
Codec context structure.
Definition: vpx_codec.h:199