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) {
498 uint32_t error_resilient = 0;
505 int frame_duration = 1;
506 int layering_mode = 0;
508 int flag_periodicity = 1;
509 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION) 514 const VpxInterface *encoder = NULL;
516 struct RateControlMetrics rc;
518 const int min_args_base = 13;
519 #if CONFIG_VP9_HIGHBITDEPTH 521 int input_bit_depth = 8;
522 const int min_args = min_args_base + 1;
524 const int min_args = min_args_base;
525 #endif // CONFIG_VP9_HIGHBITDEPTH 526 double sum_bitrate = 0.0;
527 double sum_bitrate2 = 0.0;
528 double framerate = 30.0;
532 if (argc < min_args) {
533 #if CONFIG_VP9_HIGHBITDEPTH 534 die(
"Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> " 535 "<rate_num> <rate_den> <speed> <frame_drop_threshold> " 536 "<error_resilient> <threads> <mode> " 537 "<Rate_0> ... <Rate_nlayers-1> <bit-depth> \n",
540 die(
"Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> " 541 "<rate_num> <rate_den> <speed> <frame_drop_threshold> " 542 "<error_resilient> <threads> <mode> " 543 "<Rate_0> ... <Rate_nlayers-1> \n",
545 #endif // CONFIG_VP9_HIGHBITDEPTH 548 encoder = get_vpx_encoder_by_name(argv[3]);
549 if (!encoder) die(
"Unsupported codec.");
553 width = (
unsigned int)strtoul(argv[4], NULL, 0);
554 height = (
unsigned int)strtoul(argv[5], NULL, 0);
555 if (width < 16 || width % 2 || height < 16 || height % 2) {
556 die(
"Invalid resolution: %d x %d", width, height);
559 layering_mode = (int)strtol(argv[12], NULL, 0);
560 if (layering_mode < 0 || layering_mode > 13) {
561 die(
"Invalid layering mode (0..12) %s", argv[12]);
564 if (argc != min_args + mode_to_num_layers[layering_mode]) {
565 die(
"Invalid number of arguments");
568 #if CONFIG_VP9_HIGHBITDEPTH 569 switch (strtol(argv[argc - 1], NULL, 0)) {
576 input_bit_depth = 10;
580 input_bit_depth = 12;
582 default: 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 = (int)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]; ++i) {
625 rc.layer_target_bitrate[i - 13] = (int)strtol(argv[i], NULL, 0);
626 if (strncmp(encoder->name,
"vp8", 3) == 0)
628 else if (strncmp(encoder->name,
"vp9", 3) == 0)
648 cfg.
g_threads = (
unsigned int)strtoul(argv[11], NULL, 0);
650 error_resilient = (uint32_t)strtoul(argv[10], NULL, 0);
651 if (error_resilient != 0 && error_resilient != 1) {
652 die(
"Invalid value for error resilient (0, 1): %d.", error_resilient);
664 set_temporal_layer_pattern(layering_mode, &cfg, layer_flags,
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);
691 if (!outfile[i]) die(
"Failed to open %s for writing", file_name);
693 assert(outfile[i] != NULL);
699 #if CONFIG_VP9_HIGHBITDEPTH 701 &codec, encoder->codec_interface(), &cfg,
705 #endif // CONFIG_VP9_HIGHBITDEPTH 706 die_codec(&codec,
"Failed to initialize encoder");
708 if (strncmp(encoder->name,
"vp8", 3) == 0) {
713 }
else if (strncmp(encoder->name,
"vp9", 3) == 0) {
715 memset(&svc_params, 0,
sizeof(svc_params));
727 if (cfg.
g_threads > 1 && ((cfg.
g_w > 320 && cfg.
g_h > 240) || speed < 7))
732 die_codec(&codec,
"Failed to set SVC");
741 if (strncmp(encoder->name,
"vp8", 3) == 0) {
749 const int max_intra_size_pct = 900;
755 while (frame_avail || got_data) {
756 struct vpx_usec_timer timer;
759 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION) 765 if (strncmp(encoder->name,
"vp9", 3) == 0) {
767 }
else if (strncmp(encoder->name,
"vp8", 3) == 0) {
771 flags = layer_flags[frame_cnt % flag_periodicity];
772 if (layering_mode == 0) flags = 0;
773 frame_avail = vpx_img_read(&raw, infile);
775 vpx_usec_timer_start(&timer);
778 die_codec(&codec,
"Failed to encode frame");
780 vpx_usec_timer_mark(&timer);
781 cx_time += vpx_usec_timer_elapsed(&timer);
783 if (layering_mode != 7) {
793 vpx_video_writer_write_frame(outfile[i], pkt->
data.
frame.buf,
795 ++rc.layer_tot_enc_frames[i];
796 rc.layer_encoding_bitrate[i] += 8.0 * pkt->
data.
frame.sz;
800 rc.layer_avg_frame_size[i] += 8.0 * pkt->
data.
frame.sz;
801 rc.layer_avg_rate_mismatch[i] +=
802 fabs(8.0 * pkt->
data.
frame.sz - rc.layer_pfb[i]) /
804 ++rc.layer_enc_frames[i];
810 if (frame_cnt > rc.window_size) {
811 sum_bitrate += 0.001 * 8.0 * pkt->
data.
frame.sz * framerate;
812 if (frame_cnt % rc.window_size == 0) {
813 rc.window_count += 1;
814 rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
815 rc.variance_st_encoding_bitrate +=
816 (sum_bitrate / rc.window_size) *
817 (sum_bitrate / rc.window_size);
822 if (frame_cnt > rc.window_size + rc.window_size / 2) {
823 sum_bitrate2 += 0.001 * 8.0 * pkt->
data.
frame.sz * framerate;
824 if (frame_cnt > 2 * rc.window_size &&
825 frame_cnt % rc.window_size == 0) {
826 rc.window_count += 1;
827 rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
828 rc.variance_st_encoding_bitrate +=
829 (sum_bitrate2 / rc.window_size) *
830 (sum_bitrate2 / rc.window_size);
839 pts += frame_duration;
842 printout_rate_control_summary(&rc, &cfg, frame_cnt);
844 printf(
"Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
845 frame_cnt, 1000 * (
float)cx_time / (
double)(frame_cnt * 1000000),
846 1000000 * (
double)frame_cnt / (
double)cx_time);
851 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:555
int min_quantizers[12]
Definition: vpx_encoder.h:711
unsigned int ts_number_layers
Number of temporal coding layers.
Definition: vpx_encoder.h:652
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:359
Definition: vpx_encoder.h:246
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:692
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition: vpx_encoder.h:546
#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:345
enum vpx_kf_mode kf_mode
Keyframe placement mode.
Definition: vpx_encoder.h:604
int den
Definition: vpx_encoder.h:233
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:503
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition: vpx_encoder.h:493
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: vpx_encoder.h:622
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: vpx_encoder.h:388
Encoder configuration structure.
Definition: vpx_encoder.h:281
Definition: vpx_encoder.h:261
Codec control function to set row level multi-threading.
Definition: vp8cx.h:556
int spatial_layer_id
Definition: vp8cx.h:716
Codec control function to set Max data rate for Intra frames.
Definition: vp8cx.h:251
#define VPX_CODEC_USE_HIGHBITDEPTH
Definition: vpx_encoder.h:96
Encoder output packet.
Definition: vpx_encoder.h:171
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition: vpx_encoder.h:531
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:666
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition: vpx_encoder.h:564
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: vpx_encoder.h:613
unsigned int g_profile
Bitstream profile to use.
Definition: vpx_encoder.h:311
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:684
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:712
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:320
unsigned int ts_target_bitrate[5]
Target bitrate for each temporal layer.
Definition: vpx_encoder.h:659
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:519
Codec control function to set adaptive quantization mode.
Definition: vp8cx.h:391
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:329
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:172
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: vpx_encoder.h:410
Boost percentage for Golden Frame in CBR mode.
Definition: vp8cx.h:587
vp9 svc layer parameters
Definition: vp8cx.h:715
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:479
#define VPX_DL_REALTIME
deadline parameter analogous to VPx REALTIME mode.
Definition: vpx_encoder.h:838
int num
Definition: vpx_encoder.h:232
control function to set noise sensitivity
Definition: vp8cx.h:170
Definition: vpx_codec.h:219
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:301
unsigned int ss_number_layers
Number of spatial coding layers.
Definition: vpx_encoder.h:632
vpx_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition: vpx_encoder.h:337
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:749
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:419
Bypass mode. Used when application needs to control temporal layering. This will only work when the n...
Definition: vp8cx.h:626
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:701
int temporal_layer_id
Definition: vp8cx.h:717
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:403
unsigned int ts_periodicity
Length of the sequence defining frame temporal layer membership.
Definition: vpx_encoder.h:675
#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:217
int scaling_factor_den[12]
Definition: vpx_encoder.h:713
Codec control function to set the threshold for MBs treated static.
Definition: vp8cx.h:182
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:122
Definition: vpx_codec.h:218
#define VPX_EFLAG_FORCE_KF
Definition: vpx_encoder.h:273
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:186
Definition: vpx_encoder.h:153
int max_quantizers[12]
Definition: vpx_encoder.h:710
vp9 svc extra configure parameters
Definition: vpx_encoder.h:709
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: vpx_encoder.h:367
#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:459
Codec context structure.
Definition: vpx_codec.h:196