27 #include "../tools_common.h" 28 #include "../video_writer.h" 30 #include "../vpx_ports/bitops.h" 31 #include "../vpx_ports/vpx_timer.h" 32 #include "./svc_context.h" 36 #include "../vpxstats.h" 37 #include "./y4minput.h" 39 #define OUTPUT_FRAME_STATS 0 40 #define OUTPUT_RC_STATS 1 42 #define SIMULCAST_MODE 0 44 static const arg_def_t outputfile =
45 ARG_DEF(
"o",
"output", 1,
"Output filename");
46 static const arg_def_t skip_frames_arg =
47 ARG_DEF(
"s",
"skip-frames", 1,
"input frames to skip");
48 static const arg_def_t frames_arg =
49 ARG_DEF(
"f",
"frames", 1,
"number of frames to encode");
50 static const arg_def_t threads_arg =
51 ARG_DEF(
"th",
"threads", 1,
"number of threads to use");
53 static const arg_def_t output_rc_stats_arg =
54 ARG_DEF(
"rcstat",
"output_rc_stats", 1,
"output rc stats");
56 static const arg_def_t width_arg = ARG_DEF(
"w",
"width", 1,
"source width");
57 static const arg_def_t height_arg = ARG_DEF(
"h",
"height", 1,
"source height");
58 static const arg_def_t timebase_arg =
59 ARG_DEF(
"t",
"timebase", 1,
"timebase (num/den)");
60 static const arg_def_t bitrate_arg = ARG_DEF(
61 "b",
"target-bitrate", 1,
"encoding bitrate, in kilobits per second");
62 static const arg_def_t spatial_layers_arg =
63 ARG_DEF(
"sl",
"spatial-layers", 1,
"number of spatial SVC layers");
64 static const arg_def_t temporal_layers_arg =
65 ARG_DEF(
"tl",
"temporal-layers", 1,
"number of temporal SVC layers");
66 static const arg_def_t temporal_layering_mode_arg =
67 ARG_DEF(
"tlm",
"temporal-layering-mode", 1,
68 "temporal layering scheme." 69 "VP9E_TEMPORAL_LAYERING_MODE");
70 static const arg_def_t kf_dist_arg =
71 ARG_DEF(
"k",
"kf-dist", 1,
"number of frames between keyframes");
72 static const arg_def_t scale_factors_arg =
73 ARG_DEF(
"r",
"scale-factors", 1,
"scale factors (lowest to highest layer)");
74 static const arg_def_t min_q_arg =
75 ARG_DEF(NULL,
"min-q", 1,
"Minimum quantizer");
76 static const arg_def_t max_q_arg =
77 ARG_DEF(NULL,
"max-q", 1,
"Maximum quantizer");
78 static const arg_def_t min_bitrate_arg =
79 ARG_DEF(NULL,
"min-bitrate", 1,
"Minimum bitrate");
80 static const arg_def_t max_bitrate_arg =
81 ARG_DEF(NULL,
"max-bitrate", 1,
"Maximum bitrate");
82 static const arg_def_t lag_in_frame_arg =
83 ARG_DEF(NULL,
"lag-in-frames", 1,
84 "Number of frame to input before " 85 "generating any outputs");
86 static const arg_def_t rc_end_usage_arg =
87 ARG_DEF(NULL,
"rc-end-usage", 1,
"0 - 3: VBR, CBR, CQ, Q");
88 static const arg_def_t speed_arg =
89 ARG_DEF(
"sp",
"speed", 1,
"speed configuration");
90 static const arg_def_t aqmode_arg =
91 ARG_DEF(
"aq",
"aqmode", 1,
"aq-mode off/on");
92 static const arg_def_t bitrates_arg =
93 ARG_DEF(
"bl",
"bitrates", 1,
"bitrates[sl * num_tl + tl]");
94 static const arg_def_t dropframe_thresh_arg =
95 ARG_DEF(NULL,
"drop-frame", 1,
"Temporal resampling threshold (buf %)");
96 static const arg_def_t psnr_arg =
97 ARG_DEF(NULL,
"psnr", 1,
"Enable PSNR computation and statistics");
98 static const struct arg_enum_list tune_content_enum[] = {
99 {
"default", VP9E_CONTENT_DEFAULT },
100 {
"screen", VP9E_CONTENT_SCREEN },
101 {
"film", VP9E_CONTENT_FILM },
105 static const arg_def_t tune_content_arg = ARG_DEF_ENUM(
106 NULL,
"tune-content", 1,
"Tune content type", tune_content_enum);
107 static const arg_def_t inter_layer_pred_arg = ARG_DEF(
108 NULL,
"inter-layer-pred", 1,
"0 - 3: On, Off, Key-frames, Constrained");
110 #if CONFIG_VP9_HIGHBITDEPTH 111 static const struct arg_enum_list bitdepth_enum[] = {
115 static const arg_def_t bitdepth_arg = ARG_DEF_ENUM(
116 "d",
"bit-depth", 1,
"Bit depth for codec 8, 10 or 12. ", bitdepth_enum);
117 #endif // CONFIG_VP9_HIGHBITDEPTH 119 static const arg_def_t *svc_args[] = { &frames_arg,
133 &temporal_layers_arg,
134 &temporal_layering_mode_arg,
139 &output_rc_stats_arg,
142 #if CONFIG_VP9_HIGHBITDEPTH 148 &dropframe_thresh_arg,
150 &inter_layer_pred_arg,
154 static const uint32_t default_frames_to_skip = 0;
155 static const uint32_t default_frames_to_code = 60 * 60;
156 static const uint32_t default_width = 1920;
157 static const uint32_t default_height = 1080;
158 static const uint32_t default_timebase_num = 1;
159 static const uint32_t default_timebase_den = 60;
160 static const uint32_t default_bitrate = 1000;
161 static const uint32_t default_spatial_layers = 5;
162 static const uint32_t default_temporal_layers = 1;
163 static const uint32_t default_kf_dist = 100;
164 static const uint32_t default_temporal_layering_mode = 0;
165 static const uint32_t default_output_rc_stats = 0;
166 static const int32_t default_speed = -1;
167 static const uint32_t default_threads = 0;
170 const char *output_filename;
171 uint32_t frames_to_code;
172 uint32_t frames_to_skip;
173 struct VpxInputContext input_ctx;
176 int inter_layer_pred;
179 static const char *exec_name;
181 void usage_exit(
void) {
182 fprintf(stderr,
"Usage: %s <options> input_filename -o output_filename\n",
184 fprintf(stderr,
"Options:\n");
185 arg_show_usage(stderr, svc_args);
189 static void parse_command_line(
int argc,
const char **argv_,
190 AppInput *app_input, SvcContext *svc_ctx,
197 unsigned int min_bitrate = 0;
198 unsigned int max_bitrate = 0;
199 char string_options[1024] = { 0 };
202 svc_ctx->log_level = SVC_LOG_DEBUG;
203 svc_ctx->spatial_layers = default_spatial_layers;
204 svc_ctx->temporal_layers = default_temporal_layers;
205 svc_ctx->temporal_layering_mode = default_temporal_layering_mode;
207 svc_ctx->output_rc_stat = default_output_rc_stats;
209 svc_ctx->speed = default_speed;
210 svc_ctx->threads = default_threads;
211 svc_ctx->use_psnr = 0;
219 enc_cfg->
g_w = default_width;
220 enc_cfg->
g_h = default_height;
229 app_input->frames_to_code = default_frames_to_code;
230 app_input->frames_to_skip = default_frames_to_skip;
233 argv = argv_dup(argc - 1, argv_ + 1);
235 fprintf(stderr,
"Error allocating argument list\n");
238 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
241 if (arg_match(&arg, &frames_arg, argi)) {
242 app_input->frames_to_code = arg_parse_uint(&arg);
243 }
else if (arg_match(&arg, &outputfile, argi)) {
244 app_input->output_filename = arg.val;
245 }
else if (arg_match(&arg, &width_arg, argi)) {
246 enc_cfg->
g_w = arg_parse_uint(&arg);
247 }
else if (arg_match(&arg, &height_arg, argi)) {
248 enc_cfg->
g_h = arg_parse_uint(&arg);
249 }
else if (arg_match(&arg, &timebase_arg, argi)) {
250 enc_cfg->
g_timebase = arg_parse_rational(&arg);
251 }
else if (arg_match(&arg, &bitrate_arg, argi)) {
253 }
else if (arg_match(&arg, &skip_frames_arg, argi)) {
254 app_input->frames_to_skip = arg_parse_uint(&arg);
255 }
else if (arg_match(&arg, &spatial_layers_arg, argi)) {
256 svc_ctx->spatial_layers = arg_parse_uint(&arg);
257 }
else if (arg_match(&arg, &temporal_layers_arg, argi)) {
258 svc_ctx->temporal_layers = arg_parse_uint(&arg);
260 }
else if (arg_match(&arg, &output_rc_stats_arg, argi)) {
261 svc_ctx->output_rc_stat = arg_parse_uint(&arg);
263 }
else if (arg_match(&arg, &speed_arg, argi)) {
264 svc_ctx->speed = arg_parse_uint(&arg);
265 if (svc_ctx->speed > 9) {
266 warn(
"Mapping speed %d to speed 9.\n", svc_ctx->speed);
268 }
else if (arg_match(&arg, &aqmode_arg, argi)) {
269 svc_ctx->aqmode = arg_parse_uint(&arg);
270 }
else if (arg_match(&arg, &threads_arg, argi)) {
271 svc_ctx->threads = arg_parse_uint(&arg);
272 }
else if (arg_match(&arg, &temporal_layering_mode_arg, argi)) {
275 if (svc_ctx->temporal_layering_mode) {
278 }
else if (arg_match(&arg, &kf_dist_arg, argi)) {
281 }
else if (arg_match(&arg, &scale_factors_arg, argi)) {
282 strncat(string_options,
" scale-factors=",
283 sizeof(string_options) - strlen(string_options) - 1);
284 strncat(string_options, arg.val,
285 sizeof(string_options) - strlen(string_options) - 1);
286 }
else if (arg_match(&arg, &bitrates_arg, argi)) {
287 strncat(string_options,
" bitrates=",
288 sizeof(string_options) - strlen(string_options) - 1);
289 strncat(string_options, arg.val,
290 sizeof(string_options) - strlen(string_options) - 1);
291 }
else if (arg_match(&arg, &min_q_arg, argi)) {
292 strncat(string_options,
" min-quantizers=",
293 sizeof(string_options) - strlen(string_options) - 1);
294 strncat(string_options, arg.val,
295 sizeof(string_options) - strlen(string_options) - 1);
296 }
else if (arg_match(&arg, &max_q_arg, argi)) {
297 strncat(string_options,
" max-quantizers=",
298 sizeof(string_options) - strlen(string_options) - 1);
299 strncat(string_options, arg.val,
300 sizeof(string_options) - strlen(string_options) - 1);
301 }
else if (arg_match(&arg, &min_bitrate_arg, argi)) {
302 min_bitrate = arg_parse_uint(&arg);
303 }
else if (arg_match(&arg, &max_bitrate_arg, argi)) {
304 max_bitrate = arg_parse_uint(&arg);
305 }
else if (arg_match(&arg, &lag_in_frame_arg, argi)) {
307 }
else if (arg_match(&arg, &rc_end_usage_arg, argi)) {
309 #if CONFIG_VP9_HIGHBITDEPTH 310 }
else if (arg_match(&arg, &bitdepth_arg, argi)) {
311 enc_cfg->
g_bit_depth = arg_parse_enum_or_int(&arg);
326 die(
"Error: Invalid bit depth selected (%d)\n", enc_cfg->
g_bit_depth);
328 #endif // CONFIG_VP9_HIGHBITDEPTH 329 }
else if (arg_match(&arg, &dropframe_thresh_arg, argi)) {
331 }
else if (arg_match(&arg, &tune_content_arg, argi)) {
332 app_input->tune_content = arg_parse_uint(&arg);
333 }
else if (arg_match(&arg, &inter_layer_pred_arg, argi)) {
334 app_input->inter_layer_pred = arg_parse_uint(&arg);
335 }
else if (arg_match(&arg, &psnr_arg, argi)) {
336 svc_ctx->use_psnr = arg_parse_uint(&arg);
343 if (strlen(string_options) > 0)
344 vpx_svc_set_options(svc_ctx, string_options + 1);
349 if (min_bitrate > 0) {
353 if (max_bitrate > 0) {
360 for (argi = argv; *argi; ++argi)
361 if (argi[0][0] ==
'-' && strlen(argi[0]) > 1)
362 die(
"Error: Unrecognized option %s\n", *argi);
364 if (argv[0] == NULL) {
367 app_input->input_ctx.filename = argv[0];
370 open_input_file(&app_input->input_ctx);
371 if (app_input->input_ctx.file_type == FILE_TYPE_Y4M) {
372 enc_cfg->
g_w = app_input->input_ctx.width;
373 enc_cfg->
g_h = app_input->input_ctx.height;
374 enc_cfg->
g_timebase.
den = app_input->input_ctx.framerate.numerator;
375 enc_cfg->
g_timebase.
num = app_input->input_ctx.framerate.denominator;
378 if (enc_cfg->
g_w < 16 || enc_cfg->
g_w % 2 || enc_cfg->
g_h < 16 ||
380 die(
"Invalid resolution: %d x %d\n", enc_cfg->
g_w, enc_cfg->
g_h);
383 "Codec %s\nframes: %d, skip: %d\n" 385 "width %d, height: %d,\n" 386 "num: %d, den: %d, bitrate: %d,\n" 389 app_input->frames_to_skip, svc_ctx->spatial_layers, enc_cfg->
g_w,
396 struct RateControlStats {
415 double avg_st_encoding_bitrate;
417 double variance_st_encoding_bitrate;
426 static void set_rate_control_stats(
struct RateControlStats *rc,
437 rc->layer_framerate[layer] = framerate;
441 rc->layer_pfb[layer] =
445 (rc->layer_framerate[layer] - rc->layer_framerate[layer - 1]);
448 rc->layer_framerate[layer];
450 rc->layer_input_frames[layer] = 0;
451 rc->layer_enc_frames[layer] = 0;
452 rc->layer_tot_enc_frames[layer] = 0;
453 rc->layer_encoding_bitrate[layer] = 0.0;
454 rc->layer_avg_frame_size[layer] = 0.0;
455 rc->layer_avg_rate_mismatch[layer] = 0.0;
458 rc->window_count = 0;
459 rc->window_size = 15;
460 rc->avg_st_encoding_bitrate = 0.0;
461 rc->variance_st_encoding_bitrate = 0.0;
464 static void printout_rate_control_summary(
struct RateControlStats *rc,
468 double perc_fluctuation = 0.0;
469 int tot_num_frames = 0;
470 printf(
"Total number of processed frames: %d\n\n", frame_cnt - 1);
471 printf(
"Rate control layer stats for sl%d tl%d layer(s):\n\n",
477 const int num_dropped =
479 ? (rc->layer_input_frames[layer] - rc->layer_enc_frames[layer])
480 : (rc->layer_input_frames[layer] - rc->layer_enc_frames[layer] -
482 tot_num_frames += rc->layer_input_frames[layer];
483 rc->layer_encoding_bitrate[layer] = 0.001 * rc->layer_framerate[layer] *
484 rc->layer_encoding_bitrate[layer] /
486 rc->layer_avg_frame_size[layer] =
487 rc->layer_avg_frame_size[layer] / rc->layer_enc_frames[layer];
488 rc->layer_avg_rate_mismatch[layer] = 100.0 *
489 rc->layer_avg_rate_mismatch[layer] /
490 rc->layer_enc_frames[layer];
491 printf(
"For layer#: sl%d tl%d \n", sl, tl);
492 printf(
"Bitrate (target vs actual): %d %f.0 kbps\n",
494 rc->layer_encoding_bitrate[layer]);
495 printf(
"Average frame size (target vs actual): %f %f bits\n",
496 rc->layer_pfb[layer], rc->layer_avg_frame_size[layer]);
497 printf(
"Average rate_mismatch: %f\n", rc->layer_avg_rate_mismatch[layer]);
499 "Number of input frames, encoded (non-key) frames, " 500 "and percent dropped frames: %d %d %f.0 \n",
501 rc->layer_input_frames[layer], rc->layer_enc_frames[layer],
502 100.0 * num_dropped / rc->layer_input_frames[layer]);
506 rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
507 rc->variance_st_encoding_bitrate =
508 rc->variance_st_encoding_bitrate / rc->window_count -
509 (rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
510 perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
511 rc->avg_st_encoding_bitrate;
512 printf(
"Short-time stats, for window of %d frames: \n", rc->window_size);
513 printf(
"Average, rms-variance, and percent-fluct: %f %f %f \n",
514 rc->avg_st_encoding_bitrate, sqrt(rc->variance_st_encoding_bitrate),
516 printf(
"Num of input, num of encoded (super) frames: %d %d \n", frame_cnt,
521 size_t data_sz, uint64_t sizes[8],
531 marker = *(data + data_sz - 1);
534 if ((marker & 0xe0) == 0xc0) {
535 const uint32_t frames = (marker & 0x7) + 1;
536 const uint32_t mag = ((marker >> 3) & 0x3) + 1;
537 const size_t index_sz = 2 + mag * frames;
544 const uint8_t marker2 = *(data + data_sz - index_sz);
555 const uint8_t *x = &data[data_sz - index_sz + 1];
557 for (i = 0; i < frames; ++i) {
558 uint32_t this_sz = 0;
560 for (j = 0; j < mag; ++j) this_sz |= (*x++) << (j * 8);
574 static void set_frame_flags_bypass_mode_ex0(
575 int tl,
int num_spatial_layers,
int is_key_frame,
578 for (sl = 0; sl < num_spatial_layers; ++sl)
581 for (sl = 0; sl < num_spatial_layers; ++sl) {
596 }
else if (tl == 1) {
599 (sl == 0) ? 0 : num_spatial_layers + sl - 1;
600 ref_frame_config->
alt_fb_idx[sl] = num_spatial_layers + sl;
627 }
else if (tl == 1) {
637 if (sl < num_spatial_layers - 1) {
643 }
else if (sl == num_spatial_layers - 1) {
657 static void set_frame_flags_bypass_mode_ex1(
658 int tl,
int num_spatial_layers,
int is_key_frame,
661 for (sl = 0; sl < num_spatial_layers; ++sl)
720 #if CONFIG_VP9_DECODER && !SIMULCAST_MODE 722 const int frames_out,
int *mismatch_seen) {
725 if (*mismatch_seen)
return;
730 enc_img = ref_enc.img;
732 dec_img = ref_dec.img;
733 #if CONFIG_VP9_HIGHBITDEPTH 738 enc_img.
d_w, enc_img.
d_h, 16);
739 vpx_img_truncate_16_to_8(&enc_img, &ref_enc.img);
743 dec_img.
d_w, dec_img.
d_h, 16);
744 vpx_img_truncate_16_to_8(&dec_img, &ref_dec.img);
749 if (!compare_img(&enc_img, &dec_img)) {
750 int y[4], u[4], v[4];
751 #if CONFIG_VP9_HIGHBITDEPTH 753 find_mismatch_high(&enc_img, &dec_img, y, u, v);
755 find_mismatch(&enc_img, &dec_img, y, u, v);
758 find_mismatch(&enc_img, &dec_img, y, u, v);
762 "Encode/decode mismatch on frame %d at" 763 " Y[%d, %d] {%d/%d}," 764 " U[%d, %d] {%d/%d}," 765 " V[%d, %d] {%d/%d}\n",
766 frames_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
768 *mismatch_seen = frames_out;
777 static void svc_output_rc_stats(
780 struct RateControlStats *rc, VpxVideoWriter **outfile,
781 const uint32_t frame_cnt,
const double framerate) {
782 int num_layers_encoded = 0;
785 uint64_t sizes_parsed[8];
787 double sum_bitrate = 0.0;
788 double sum_bitrate2 = 0.0;
789 memset(sizes, 0,
sizeof(sizes));
790 memset(sizes_parsed, 0,
sizeof(sizes_parsed));
793 sizes_parsed, &count);
799 if (cx_pkt->
data.
frame.spatial_layer_encoded[sl]) {
800 sizes[sl] = sizes_parsed[num_layers_encoded];
801 num_layers_encoded++;
807 uint64_t tot_size = 0;
809 for (sl2 = 0; sl2 < sl; ++sl2) {
810 if (cx_pkt->
data.
frame.spatial_layer_encoded[sl2]) tot_size += sizes[sl2];
812 vpx_video_writer_write_frame(outfile[sl],
813 (uint8_t *)(cx_pkt->
data.
frame.buf) + tot_size,
814 (
size_t)(sizes[sl]), cx_pkt->
data.
frame.pts);
816 for (sl2 = 0; sl2 <= sl; ++sl2) {
817 if (cx_pkt->
data.
frame.spatial_layer_encoded[sl2]) tot_size += sizes[sl2];
820 vpx_video_writer_write_frame(outfile[sl], cx_pkt->
data.
frame.buf,
821 (
size_t)(tot_size), cx_pkt->
data.
frame.pts);
822 #endif // SIMULCAST_MODE 825 if (cx_pkt->
data.
frame.spatial_layer_encoded[sl]) {
829 ++rc->layer_tot_enc_frames[layer];
830 rc->layer_encoding_bitrate[layer] += 8.0 * sizes[sl];
835 rc->layer_avg_frame_size[layer] += 8.0 * sizes[sl];
836 rc->layer_avg_rate_mismatch[layer] +=
837 fabs(8.0 * sizes[sl] - rc->layer_pfb[layer]) /
838 rc->layer_pfb[layer];
839 ++rc->layer_enc_frames[layer];
848 if (frame_cnt > (
unsigned int)rc->window_size) {
850 if (cx_pkt->
data.
frame.spatial_layer_encoded[sl])
851 sum_bitrate += 0.001 * 8.0 * sizes[sl] * framerate;
853 if (frame_cnt % rc->window_size == 0) {
854 rc->window_count += 1;
855 rc->avg_st_encoding_bitrate += sum_bitrate / rc->window_size;
856 rc->variance_st_encoding_bitrate +=
857 (sum_bitrate / rc->window_size) * (sum_bitrate / rc->window_size);
862 if (frame_cnt > (
unsigned int)(rc->window_size + rc->window_size / 2)) {
864 sum_bitrate2 += 0.001 * 8.0 * sizes[sl] * framerate;
867 if (frame_cnt > (
unsigned int)(2 * rc->window_size) &&
868 frame_cnt % rc->window_size == 0) {
869 rc->window_count += 1;
870 rc->avg_st_encoding_bitrate += sum_bitrate2 / rc->window_size;
871 rc->variance_st_encoding_bitrate +=
872 (sum_bitrate2 / rc->window_size) * (sum_bitrate2 / rc->window_size);
878 int main(
int argc,
const char **argv) {
880 VpxVideoWriter *writer = NULL;
887 uint32_t frame_cnt = 0;
891 int frame_duration = 1;
892 int end_of_stream = 0;
893 #if OUTPUT_FRAME_STATS 894 int frames_received = 0;
898 struct RateControlStats rc;
902 double framerate = 30.0;
904 struct vpx_usec_timer timer;
906 #if CONFIG_INTERNAL_STATS 907 FILE *f = fopen(
"opsnr.stt",
"a");
909 #if CONFIG_VP9_DECODER && !SIMULCAST_MODE 910 int mismatch_seen = 0;
913 memset(&svc_ctx, 0,
sizeof(svc_ctx));
914 memset(&app_input, 0,
sizeof(AppInput));
915 memset(&info, 0,
sizeof(VpxVideoInfo));
917 memset(&rc, 0,
sizeof(
struct RateControlStats));
921 app_input.input_ctx.framerate.numerator = 30;
922 app_input.input_ctx.framerate.denominator = 1;
923 app_input.input_ctx.only_i420 = 1;
924 app_input.input_ctx.bit_depth = 0;
926 parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg);
929 if (app_input.input_ctx.file_type != FILE_TYPE_Y4M) {
931 #if CONFIG_VP9_HIGHBITDEPTH 935 enc_cfg.
g_w, enc_cfg.
g_h, 32)) {
936 die(
"Failed to allocate image %dx%d\n", enc_cfg.
g_w, enc_cfg.
g_h);
940 die(
"Failed to allocate image %dx%d\n", enc_cfg.
g_w, enc_cfg.
g_h);
942 #endif // CONFIG_VP9_HIGHBITDEPTH 948 die(
"Failed to initialize encoder\n");
949 #if CONFIG_VP9_DECODER && !SIMULCAST_MODE 951 &decoder, get_vpx_decoder_by_name(
"vp9")->codec_interface(), NULL, 0))
952 die(
"Failed to initialize decoder\n");
958 rc.avg_st_encoding_bitrate = 0.0;
959 rc.variance_st_encoding_bitrate = 0.0;
960 if (svc_ctx.output_rc_stat) {
961 set_rate_control_stats(&rc, &enc_cfg);
966 info.codec_fourcc = VP9_FOURCC;
967 info.frame_width = enc_cfg.
g_w;
968 info.frame_height = enc_cfg.
g_h;
973 vpx_video_writer_open(app_input.output_filename, kContainerIVF, &info);
975 die(
"Failed to open %s for writing\n", app_input.output_filename);
980 if (svc_ctx.output_rc_stat) {
982 char file_name[PATH_MAX];
984 snprintf(file_name,
sizeof(file_name),
"%s_s%d.ivf",
985 app_input.output_filename, sl);
986 outfile[sl] = vpx_video_writer_open(file_name, kContainerIVF, &info);
987 if (!outfile[sl]) die(
"Failed to open %s for writing", file_name);
993 for (i = 0; i < app_input.frames_to_skip; ++i)
994 read_frame(&app_input.input_ctx, &raw);
996 if (svc_ctx.speed != -1)
998 if (svc_ctx.threads) {
1000 get_msb(svc_ctx.threads));
1001 if (svc_ctx.threads > 1)
1006 if (svc_ctx.speed >= 5 && svc_ctx.aqmode == 1)
1008 if (svc_ctx.speed >= 5)
1013 app_input.inter_layer_pred);
1023 for (sl = 0; sl < (
unsigned int)svc_ctx.spatial_layers; ++sl)
1029 while (!end_of_stream) {
1037 int example_pattern = 0;
1038 if (frame_cnt >= app_input.frames_to_code ||
1039 !read_frame(&app_input.input_ctx, &raw)) {
1054 if (frame_cnt % 2 == 0) {
1055 layer_id.temporal_layer_id = 0;
1057 layer_id.temporal_layer_id_per_spatial[i] = 0;
1059 layer_id.temporal_layer_id = 1;
1061 layer_id.temporal_layer_id_per_spatial[i] = 1;
1063 if (example_pattern == 1) {
1065 assert(svc_ctx.spatial_layers == 2);
1066 assert(svc_ctx.temporal_layers == 2);
1067 if (frame_cnt % 2 == 0) {
1069 layer_id.temporal_layer_id_per_spatial[0] = 0;
1070 layer_id.temporal_layer_id_per_spatial[1] = 0;
1071 layer_id.spatial_layer_id = 0;
1074 layer_id.temporal_layer_id_per_spatial[1] = 1;
1075 layer_id.spatial_layer_id = 1;
1081 if (example_pattern == 0) {
1082 set_frame_flags_bypass_mode_ex0(layer_id.temporal_layer_id,
1083 svc_ctx.spatial_layers, frame_cnt == 0,
1085 }
else if (example_pattern == 1) {
1086 set_frame_flags_bypass_mode_ex1(layer_id.temporal_layer_id,
1087 svc_ctx.spatial_layers, frame_cnt == 0,
1090 ref_frame_config.
duration[0] = frame_duration * 1;
1091 ref_frame_config.
duration[1] = frame_duration * 1;
1099 layer_id.temporal_layer_id];
1103 unsigned int tl = 0;
1105 tl = (frame_cnt % 2 != 0);
1107 if (frame_cnt % 2 != 0) tl = 2;
1108 if ((frame_cnt > 1) && ((frame_cnt - 2) % 4 == 0)) tl = 1;
1114 vpx_usec_timer_start(&timer);
1115 res = vpx_svc_encode(
1116 &svc_ctx, &encoder, (end_of_stream ? NULL : &raw), pts, frame_duration,
1118 vpx_usec_timer_mark(&timer);
1119 cx_time += vpx_usec_timer_elapsed(&timer);
1123 die_codec(&encoder,
"Failed to encode frame");
1127 switch (cx_pkt->
kind) {
1129 SvcInternal_t *
const si = (SvcInternal_t *)svc_ctx.internal;
1131 vpx_video_writer_write_frame(writer, cx_pkt->
data.
frame.buf,
1135 if (svc_ctx.output_rc_stat) {
1136 svc_output_rc_stats(&encoder, &enc_cfg, &layer_id, cx_pkt, &rc,
1137 outfile, frame_cnt, framerate);
1141 #if OUTPUT_FRAME_STATS 1142 printf(
"SVC frame: %d, kf: %d, size: %d, pts: %d\n", frames_received,
1148 uint64_t sizes[8] = { 0 };
1150 int num_layers_encoded = 0;
1151 parse_superframe_index(cx_pkt->
data.
frame.buf,
1154 if (cx_pkt->
data.
frame.spatial_layer_encoded[sl]) {
1155 si->bytes_sum[sl] += (int)sizes[num_layers_encoded];
1156 num_layers_encoded++;
1160 si->bytes_sum[0] += (int)cx_pkt->
data.
frame.sz;
1162 #if CONFIG_VP9_DECODER && !SIMULCAST_MODE 1164 (
unsigned int)cx_pkt->
data.
frame.sz, NULL, 0))
1165 die_codec(&decoder,
"Failed to decode frame.");
1173 !(layer_id.temporal_layer_id > 0 &&
1174 layer_id.temporal_layer_id ==
1176 test_decode(&encoder, &decoder, frame_cnt, &mismatch_seen);
1182 SvcInternal_t *
const si = (SvcInternal_t *)svc_ctx.internal;
1183 sl = cx_pkt->
data.
psnr.spatial_layer_id;
1184 si->number_of_frames[sl]++;
1185 for (
int j = 0; j < 4; ++j) {
1186 si->psnr_sum[sl][j] += cx_pkt->
data.
psnr.psnr[j];
1187 si->sse_sum[sl][j] += cx_pkt->
data.
psnr.sse[j];
1202 if (!end_of_stream) {
1204 pts += frame_duration;
1208 printf(
"Processed %d frames\n", frame_cnt);
1210 close_input_file(&app_input.input_ctx);
1213 if (svc_ctx.output_rc_stat) {
1214 printout_rate_control_summary(&rc, &enc_cfg, frame_cnt);
1219 die_codec(&encoder,
"Failed to destroy codec");
1221 vpx_video_writer_close(writer);
1224 if (svc_ctx.output_rc_stat) {
1226 vpx_video_writer_close(outfile[sl]);
1230 #if CONFIG_INTERNAL_STATS 1231 if (mismatch_seen) {
1232 fprintf(f,
"First mismatch occurred in frame %d\n", mismatch_seen);
1234 fprintf(f,
"No mismatch detected in recon buffers\n");
1238 printf(
"Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
1239 frame_cnt, 1000 * (
float)cx_time / (
double)(frame_cnt * 1000000),
1240 1000000 * (
double)frame_cnt / (
double)cx_time);
1241 if (app_input.input_ctx.file_type != FILE_TYPE_Y4M) {
1245 vpx_svc_dump_statistics(&svc_ctx);
1246 vpx_svc_release(&svc_ctx);
1247 return EXIT_SUCCESS;
vpx_fixed_buf_t twopass_stats
Definition: vpx_encoder.h:190
unsigned int ts_number_layers
Number of temporal coding layers.
Definition: vpx_encoder.h:657
Codec control function to disable increase Q on overshoot in CBR.
Definition: vp8cx.h:700
Codec control function to set encoder internal speed settings.
Definition: vp8cx.h:173
#define VPX_MAX_LAYERS
Definition: vpx_encoder.h:44
int reference_alt_ref[5]
Definition: vp8cx.h:928
Image Descriptor.
Definition: vpx_image.h:76
Describes the decoder algorithm interface to applications.
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.
Codec control function to constrain the inter-layer prediction (prediction of lower spatial resolutio...
Definition: vp8cx.h:625
const char * vpx_codec_err_to_string(vpx_codec_err_t err)
Convert error number to printable string.
int lst_fb_idx[5]
Definition: vp8cx.h:918
Codec control function to set content type.
Definition: vp8cx.h:481
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:354
Codec control function to set noise sensitivity.
Definition: vp8cx.h:439
unsigned int layer_target_bitrate[12]
Target bitrate for each spatial/temporal layer.
Definition: vpx_encoder.h:697
SVC_LAYER_DROP_MODE framedrop_mode
Definition: vp8cx.h:956
int spatial_layer_id
Definition: vpx_encoder.h:196
unsigned int g_input_bit_depth
Bit-depth of the input frames.
Definition: vpx_encoder.h:340
int den
Definition: vpx_encoder.h:229
Definition: vpx_encoder.h:156
int framedrop_thresh[5]
Definition: vp8cx.h:954
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: vpx_encoder.h:627
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: vpx_encoder.h:383
Encoder configuration structure.
Definition: vpx_encoder.h:279
int reference_golden[5]
Definition: vp8cx.h:927
Definition: vpx_encoder.h:158
The coded data for this stream is corrupt or incomplete.
Definition: vpx_codec.h:133
Codec control function to set row level multi-threading.
Definition: vp8cx.h:576
Codec control function to disable loopfilter.
Definition: vp8cx.h:709
Codec control function to set Max data rate for Intra frames.
Definition: vp8cx.h:275
Encoder output packet.
Definition: vpx_encoder.h:167
void * buf
Definition: vpx_encoder.h:104
unsigned int ts_rate_decimator[5]
Frame rate decimation factor for each temporal layer.
Definition: vpx_encoder.h:671
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: vpx_encoder.h:618
vp9 svc frame dropping parameters.
Definition: vp8cx.h:953
unsigned int g_profile
Bitstream profile to use.
Definition: vpx_encoder.h:306
Codec control function to set number of tile columns.
Definition: vp8cx.h:369
#define VPX_IMG_FMT_HIGHBITDEPTH
Definition: vpx_image.h:35
struct vpx_codec_cx_pkt::@1::@2 frame
#define VPX_SS_MAX_LAYERS
Definition: vpx_encoder.h:47
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:42
unsigned int d_w
Definition: vpx_image.h:87
#define vpx_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_dec_init_ver()
Definition: vpx_decoder.h:143
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:315
int reference_last[5]
Definition: vp8cx.h:926
int update_buffer_slot[5]
Definition: vp8cx.h:921
Codec control function to set adaptive quantization mode.
Definition: vp8cx.h:416
vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, const uint8_t *data, unsigned int data_sz, void *user_priv, long deadline)
Decode data.
Codec control function to get svc layer ID.
Definition: vp8cx.h:489
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:324
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:168
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: vpx_encoder.h:402
vp9 svc layer parameters
Definition: vp8cx.h:902
Operation completed without error.
Definition: vpx_codec.h:95
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.
vpx_img_fmt_t fmt
Definition: vpx_image.h:77
unsigned int rc_target_bitrate
Target data rate.
Definition: vpx_encoder.h:473
#define VPX_DL_REALTIME
deadline parameter analogous to VPx REALTIME mode.
Definition: vpx_encoder.h:1012
int num
Definition: vpx_encoder.h:228
Definition: vpx_codec.h:223
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, unsigned int usage)
Get a default configuration.
Codec control function to set the frame flags and buffer indices for spatial layers. The frame flags and buffer indices are set using the struct vpx_svc_ref_frame_config defined below.
Definition: vp8cx.h:551
enum vpx_enc_pass g_pass
Multi-pass Encoding Mode.
Definition: vpx_encoder.h:369
double psnr[4]
Definition: vpx_encoder.h:195
Codec control function to set mode and thresholds for frame dropping in SVC. Drop frame thresholds ar...
Definition: vp8cx.h:634
#define VPX_DL_GOOD_QUALITY
deadline parameter analogous to VPx GOOD QUALITY mode.
Definition: vpx_encoder.h:1014
unsigned int ss_number_layers
Number of spatial coding layers.
Definition: vpx_encoder.h:637
vpx_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition: vpx_encoder.h:332
Provides definitions for using VP8 or VP9 encoder algorithm within the vpx Codec Interface.
Bypass mode. Used when application needs to control temporal layering. This will only work when the n...
Definition: vp8cx.h:808
vpx_codec_err_t
Algorithm return codes.
Definition: vpx_codec.h:93
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:706
VP9 specific reference frame data struct.
Definition: vp8.h:110
int temporal_layer_id
Definition: vp8cx.h:905
Definition: vpx_image.h:47
vpx_codec_iface_t * vpx_codec_vp9_cx(void)
The interface to the VP9 encoder.
int max_consec_drop
Definition: vp8cx.h:957
Definition: vpx_encoder.h:243
int idx
Definition: vp8.h:111
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition: vpx_codec.h:411
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
unsigned int d_h
Definition: vpx_image.h:88
size_t sz
Definition: vpx_encoder.h:105
Definition: vpx_codec.h:221
vp9 svc frame flag parameters.
Definition: vp8cx.h:917
vpx_codec_err_t err
Definition: vpx_codec.h:203
Codec control function to set the threshold for MBs treated static.
Definition: vp8cx.h:206
int64_t duration[5]
Definition: vp8cx.h:929
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:123
Definition: vpx_codec.h:222
int alt_fb_idx[5]
Definition: vp8cx.h:920
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:190
Definition: vpx_encoder.h:155
unsigned int rc_2pass_vbr_maxsection_pct
Two-pass mode per-GOP maximum bitrate.
Definition: vpx_encoder.h:590
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: vpx_encoder.h:362
unsigned int rc_2pass_vbr_minsection_pct
Two-pass mode per-GOP minimum bitrate.
Definition: vpx_encoder.h:583
int gld_fb_idx[5]
Definition: vp8cx.h:919
Codec control function to set svc layer for spatial and temporal.
Definition: vp8cx.h:471
enum vpx_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: vpx_encoder.h:451
Definition: vpx_encoder.h:234
Codec context structure.
Definition: vpx_codec.h:200