1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <math.h>
18#include <stdarg.h>
19#include <stdlib.h>
20#include <string.h>
21#include <time.h>
22
23#include "../args.h"
24#include "../tools_common.h"
25#include "../video_writer.h"
26
27#include "../vpx_ports/vpx_timer.h"
28#include "./svc_context.h"
31#include "../vpxstats.h"
32#include "vp9/encoder/vp9_encoder.h"
33#include "./y4minput.h"
34
35#define OUTPUT_FRAME_STATS 0
36#define OUTPUT_RC_STATS 1
37
38#define SIMULCAST_MODE 0
39
40static const arg_def_t outputfile =
41 ARG_DEF("o", "output", 1, "Output filename");
42static const arg_def_t skip_frames_arg =
43 ARG_DEF("s", "skip-frames", 1, "input frames to skip");
44static const arg_def_t frames_arg =
45 ARG_DEF("f", "frames", 1, "number of frames to encode");
46static const arg_def_t threads_arg =
47 ARG_DEF("th", "threads", 1, "number of threads to use");
48#if OUTPUT_RC_STATS
49static const arg_def_t output_rc_stats_arg =
50 ARG_DEF("rcstat", "output_rc_stats", 1, "output rc stats");
51#endif
52static const arg_def_t width_arg = ARG_DEF("w", "width", 1, "source width");
53static const arg_def_t height_arg = ARG_DEF("h", "height", 1, "source height");
54static const arg_def_t timebase_arg =
55 ARG_DEF("t", "timebase", 1, "timebase (num/den)");
56static const arg_def_t bitrate_arg = ARG_DEF(
57 "b", "target-bitrate", 1, "encoding bitrate, in kilobits per second");
58static const arg_def_t spatial_layers_arg =
59 ARG_DEF("sl", "spatial-layers", 1, "number of spatial SVC layers");
60static const arg_def_t temporal_layers_arg =
61 ARG_DEF("tl", "temporal-layers", 1, "number of temporal SVC layers");
62static const arg_def_t temporal_layering_mode_arg =
63 ARG_DEF("tlm", "temporal-layering-mode", 1,
64 "temporal layering scheme."
65 "VP9E_TEMPORAL_LAYERING_MODE");
66static const arg_def_t kf_dist_arg =
67 ARG_DEF("k", "kf-dist", 1, "number of frames between keyframes");
68static const arg_def_t scale_factors_arg =
69 ARG_DEF("r", "scale-factors", 1, "scale factors (lowest to highest layer)");
70static const arg_def_t min_q_arg =
71 ARG_DEF(NULL, "min-q", 1, "Minimum quantizer");
72static const arg_def_t max_q_arg =
73 ARG_DEF(NULL, "max-q", 1, "Maximum quantizer");
74static const arg_def_t min_bitrate_arg =
75 ARG_DEF(NULL, "min-bitrate", 1, "Minimum bitrate");
76static const arg_def_t max_bitrate_arg =
77 ARG_DEF(NULL, "max-bitrate", 1, "Maximum bitrate");
78static const arg_def_t lag_in_frame_arg =
79 ARG_DEF(NULL, "lag-in-frames", 1,
80 "Number of frame to input before "
81 "generating any outputs");
82static const arg_def_t rc_end_usage_arg =
83 ARG_DEF(NULL, "rc-end-usage", 1, "0 - 3: VBR, CBR, CQ, Q");
84static const arg_def_t speed_arg =
85 ARG_DEF("sp", "speed", 1, "speed configuration");
86static const arg_def_t aqmode_arg =
87 ARG_DEF("aq", "aqmode", 1, "aq-mode off/on");
88static const arg_def_t bitrates_arg =
89 ARG_DEF("bl", "bitrates", 1, "bitrates[sl * num_tl + tl]");
90static const arg_def_t dropframe_thresh_arg =
91 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
92static const struct arg_enum_list tune_content_enum[] = {
93 { "default", VP9E_CONTENT_DEFAULT },
94 { "screen", VP9E_CONTENT_SCREEN },
95 { "film", VP9E_CONTENT_FILM },
96 { NULL, 0 }
97};
98
99static const arg_def_t tune_content_arg = ARG_DEF_ENUM(
100 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
101static const arg_def_t inter_layer_pred_arg = ARG_DEF(
102 NULL, "inter-layer-pred", 1, "0 - 3: On, Off, Key-frames, Constrained");
103
104#if CONFIG_VP9_HIGHBITDEPTH
105static const struct arg_enum_list bitdepth_enum[] = {
107};
108
109static const arg_def_t bitdepth_arg = ARG_DEF_ENUM(
110 "d", "bit-depth", 1, "Bit depth for codec 8, 10 or 12. ", bitdepth_enum);
111#endif
112
113static const arg_def_t *svc_args[] = { &frames_arg,
114 &outputfile,
115 &width_arg,
116 &height_arg,
117 &timebase_arg,
118 &bitrate_arg,
119 &skip_frames_arg,
120 &spatial_layers_arg,
121 &kf_dist_arg,
122 &scale_factors_arg,
123 &min_q_arg,
124 &max_q_arg,
125 &min_bitrate_arg,
126 &max_bitrate_arg,
127 &temporal_layers_arg,
128 &temporal_layering_mode_arg,
129 &lag_in_frame_arg,
130 &threads_arg,
131 &aqmode_arg,
132#if OUTPUT_RC_STATS
133 &output_rc_stats_arg,
134#endif
135
136#if CONFIG_VP9_HIGHBITDEPTH
137 &bitdepth_arg,
138#endif
139 &speed_arg,
140 &rc_end_usage_arg,
141 &bitrates_arg,
142 &dropframe_thresh_arg,
143 &tune_content_arg,
144 &inter_layer_pred_arg,
145 NULL };
146
147static const uint32_t default_frames_to_skip = 0;
148static const uint32_t default_frames_to_code = 60 * 60;
149static const uint32_t default_width = 1920;
150static const uint32_t default_height = 1080;
151static const uint32_t default_timebase_num = 1;
152static const uint32_t default_timebase_den = 60;
153static const uint32_t default_bitrate = 1000;
154static const uint32_t default_spatial_layers = 5;
155static const uint32_t default_temporal_layers = 1;
156static const uint32_t default_kf_dist = 100;
157static const uint32_t default_temporal_layering_mode = 0;
158static const uint32_t default_output_rc_stats = 0;
159static const int32_t default_speed = -1;
160static const uint32_t default_threads = 0;
161
162typedef struct {
163 const char *output_filename;
164 uint32_t frames_to_code;
165 uint32_t frames_to_skip;
166 struct VpxInputContext input_ctx;
167 stats_io_t rc_stats;
168 int tune_content;
169 int inter_layer_pred;
170} AppInput;
171
172static const char *exec_name;
173
174void usage_exit(void) {
175 fprintf(stderr, "Usage: %s <options> input_filename -o output_filename\n",
176 exec_name);
177 fprintf(stderr, "Options:\n");
178 arg_show_usage(stderr, svc_args);
179 exit(EXIT_FAILURE);
180}
181
182static void parse_command_line(int argc, const char **argv_,
183 AppInput *app_input, SvcContext *svc_ctx,
185 struct arg arg;
186 char **argv = NULL;
187 char **argi = NULL;
188 char **argj = NULL;
190 unsigned int min_bitrate = 0;
191 unsigned int max_bitrate = 0;
192 char string_options[1024] = { 0 };
193
194
195 svc_ctx->log_level = SVC_LOG_DEBUG;
196 svc_ctx->spatial_layers = default_spatial_layers;
197 svc_ctx->temporal_layers = default_temporal_layers;
198 svc_ctx->temporal_layering_mode = default_temporal_layering_mode;
199#if OUTPUT_RC_STATS
200 svc_ctx->output_rc_stat = default_output_rc_stats;
201#endif
202 svc_ctx->speed = default_speed;
203 svc_ctx->threads = default_threads;
204
205
207 if (res) {
209 }
210
211 enc_cfg->
g_w = default_width;
212 enc_cfg->
g_h = default_height;
219
220
221 app_input->frames_to_code = default_frames_to_code;
222 app_input->frames_to_skip = default_frames_to_skip;
223
224
225 argv = argv_dup(argc - 1, argv_ + 1);
226 if (!argv) {
227 fprintf(stderr, "Error allocating argument list\n");
228 exit(EXIT_FAILURE);
229 }
230 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
231 arg.argv_step = 1;
232
233 if (arg_match(&arg, &frames_arg, argi)) {
234 app_input->frames_to_code = arg_parse_uint(&arg);
235 } else if (arg_match(&arg, &outputfile, argi)) {
236 app_input->output_filename = arg.val;
237 } else if (arg_match(&arg, &width_arg, argi)) {
238 enc_cfg->
g_w = arg_parse_uint(&arg);
239 } else if (arg_match(&arg, &height_arg, argi)) {
240 enc_cfg->
g_h = arg_parse_uint(&arg);
241 } else if (arg_match(&arg, &timebase_arg, argi)) {
242 enc_cfg->
g_timebase = arg_parse_rational(&arg);
243 } else if (arg_match(&arg, &bitrate_arg, argi)) {
245 } else if (arg_match(&arg, &skip_frames_arg, argi)) {
246 app_input->frames_to_skip = arg_parse_uint(&arg);
247 } else if (arg_match(&arg, &spatial_layers_arg, argi)) {
248 svc_ctx->spatial_layers = arg_parse_uint(&arg);
249 } else if (arg_match(&arg, &temporal_layers_arg, argi)) {
250 svc_ctx->temporal_layers = arg_parse_uint(&arg);
251#if OUTPUT_RC_STATS
252 } else if (arg_match(&arg, &output_rc_stats_arg, argi)) {
253 svc_ctx->output_rc_stat = arg_parse_uint(&arg);
254#endif
255 } else if (arg_match(&arg, &speed_arg, argi)) {
256 svc_ctx->speed = arg_parse_uint(&arg);
257 if (svc_ctx->speed > 9) {
258 warn("Mapping speed %d to speed 9.\n", svc_ctx->speed);
259 }
260 } else if (arg_match(&arg, &aqmode_arg, argi)) {
261 svc_ctx->aqmode = arg_parse_uint(&arg);
262 } else if (arg_match(&arg, &threads_arg, argi)) {
263 svc_ctx->threads = arg_parse_uint(&arg);
264 } else if (arg_match(&arg, &temporal_layering_mode_arg, argi)) {
266 arg_parse_int(&arg);
267 if (svc_ctx->temporal_layering_mode) {
269 }
270 } else if (arg_match(&arg, &kf_dist_arg, argi)) {
273 } else if (arg_match(&arg, &scale_factors_arg, argi)) {
274 strncat(string_options, " scale-factors=",
275 sizeof(string_options) - strlen(string_options) - 1);
276 strncat(string_options, arg.val,
277 sizeof(string_options) - strlen(string_options) - 1);
278 } else if (arg_match(&arg, &bitrates_arg, argi)) {
279 strncat(string_options, " bitrates=",
280 sizeof(string_options) - strlen(string_options) - 1);
281 strncat(string_options, arg.val,
282 sizeof(string_options) - strlen(string_options) - 1);
283 } else if (arg_match(&arg, &min_q_arg, argi)) {
284 strncat(string_options, " min-quantizers=",
285 sizeof(string_options) - strlen(string_options) - 1);
286 strncat(string_options, arg.val,
287 sizeof(string_options) - strlen(string_options) - 1);
288 } else if (arg_match(&arg, &max_q_arg, argi)) {
289 strncat(string_options, " max-quantizers=",
290 sizeof(string_options) - strlen(string_options) - 1);
291 strncat(string_options, arg.val,
292 sizeof(string_options) - strlen(string_options) - 1);
293 } else if (arg_match(&arg, &min_bitrate_arg, argi)) {
294 min_bitrate = arg_parse_uint(&arg);
295 } else if (arg_match(&arg, &max_bitrate_arg, argi)) {
296 max_bitrate = arg_parse_uint(&arg);
297 } else if (arg_match(&arg, &lag_in_frame_arg, argi)) {
299 } else if (arg_match(&arg, &rc_end_usage_arg, argi)) {
301#if CONFIG_VP9_HIGHBITDEPTH
302 } else if (arg_match(&arg, &bitdepth_arg, argi)) {
303 enc_cfg->
g_bit_depth = arg_parse_enum_or_int(&arg);
308 break;
312 break;
316 break;
317 default:
318 die(
"Error: Invalid bit depth selected (%d)\n", enc_cfg->
g_bit_depth);
319 }
320#endif
321 } else if (arg_match(&arg, &dropframe_thresh_arg, argi)) {
323 } else if (arg_match(&arg, &tune_content_arg, argi)) {
324 app_input->tune_content = arg_parse_uint(&arg);
325 } else if (arg_match(&arg, &inter_layer_pred_arg, argi)) {
326 app_input->inter_layer_pred = arg_parse_uint(&arg);
327 } else {
328 ++argj;
329 }
330 }
331
332
333 if (strlen(string_options) > 0)
334 vpx_svc_set_options(svc_ctx, string_options + 1);
335
337
339 if (min_bitrate > 0) {
342 }
343 if (max_bitrate > 0) {
346 }
347 }
348
349
350 for (argi = argv; *argi; ++argi)
351 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
352 die("Error: Unrecognized option %s\n", *argi);
353
354 if (argv[0] == NULL) {
355 usage_exit();
356 }
357 app_input->input_ctx.filename = argv[0];
358 free(argv);
359
360 open_input_file(&app_input->input_ctx);
361 if (app_input->input_ctx.file_type == FILE_TYPE_Y4M) {
362 enc_cfg->
g_w = app_input->input_ctx.width;
363 enc_cfg->
g_h = app_input->input_ctx.height;
364 enc_cfg->
g_timebase.
den = app_input->input_ctx.framerate.numerator;
365 enc_cfg->
g_timebase.
num = app_input->input_ctx.framerate.denominator;
366 }
367
368 if (enc_cfg->
g_w < 16 || enc_cfg->
g_w % 2 || enc_cfg->
g_h < 16 ||
370 die(
"Invalid resolution: %d x %d\n", enc_cfg->
g_w, enc_cfg->
g_h);
371
372 printf(
373 "Codec %s\nframes: %d, skip: %d\n"
374 "layers: %d\n"
375 "width %d, height: %d,\n"
376 "num: %d, den: %d, bitrate: %d,\n"
377 "gop size: %d\n",
379 app_input->frames_to_skip, svc_ctx->spatial_layers, enc_cfg->
g_w,
382}
383
384#if OUTPUT_RC_STATS
385
386struct RateControlStats {
387
389
391
393
395
397
399
401
403
404
405 double avg_st_encoding_bitrate;
406
407 double variance_st_encoding_bitrate;
408
409 int window_size;
410
411 int window_count;
412};
413
414
415
416static void set_rate_control_stats(struct RateControlStats *rc,
418 unsigned int sl, tl;
419
420
422
427 rc->layer_framerate[layer] = framerate;
428 else
430 if (tl > 0) {
431 rc->layer_pfb[layer] =
432 1000.0 *
435 (rc->layer_framerate[layer] - rc->layer_framerate[layer - 1]);
436 } else {
438 rc->layer_framerate[layer];
439 }
440 rc->layer_input_frames[layer] = 0;
441 rc->layer_enc_frames[layer] = 0;
442 rc->layer_tot_enc_frames[layer] = 0;
443 rc->layer_encoding_bitrate[layer] = 0.0;
444 rc->layer_avg_frame_size[layer] = 0.0;
445 rc->layer_avg_rate_mismatch[layer] = 0.0;
446 }
447 }
448 rc->window_count = 0;
449 rc->window_size = 15;
450 rc->avg_st_encoding_bitrate = 0.0;
451 rc->variance_st_encoding_bitrate = 0.0;
452}
453
454static void printout_rate_control_summary(struct RateControlStats *rc,
456 int frame_cnt) {
457 unsigned int sl, tl;
458 double perc_fluctuation = 0.0;
459 int tot_num_frames = 0;
460 printf("Total number of processed frames: %d\n\n", frame_cnt - 1);
461 printf("Rate control layer stats for sl%d tl%d layer(s):\n\n",
464 tot_num_frames = 0;
467 const int num_dropped =
468 (tl > 0)
469 ? (rc->layer_input_frames[layer] - rc->layer_enc_frames[layer])
470 : (rc->layer_input_frames[layer] - rc->layer_enc_frames[layer] -
471 1);
472 tot_num_frames += rc->layer_input_frames[layer];
473 rc->layer_encoding_bitrate[layer] = 0.001 * rc->layer_framerate[layer] *
474 rc->layer_encoding_bitrate[layer] /
475 tot_num_frames;
476 rc->layer_avg_frame_size[layer] =
477 rc->layer_avg_frame_size[layer] / rc->layer_enc_frames[layer];
478 rc->layer_avg_rate_mismatch[layer] = 100.0 *
479 rc->layer_avg_rate_mismatch[layer] /
480 rc->layer_enc_frames[layer];
481 printf("For layer#: sl%d tl%d \n", sl, tl);
482 printf("Bitrate (target vs actual): %d %f.0 kbps\n",
484 rc->layer_encoding_bitrate[layer]);
485 printf("Average frame size (target vs actual): %f %f bits\n",
486 rc->layer_pfb[layer], rc->layer_avg_frame_size[layer]);
487 printf("Average rate_mismatch: %f\n", rc->layer_avg_rate_mismatch[layer]);
488 printf(
489 "Number of input frames, encoded (non-key) frames, "
490 "and percent dropped frames: %d %d %f.0 \n",
491 rc->layer_input_frames[layer], rc->layer_enc_frames[layer],
492 100.0 * num_dropped / rc->layer_input_frames[layer]);
493 printf("\n");
494 }
495 }
496 rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
497 rc->variance_st_encoding_bitrate =
498 rc->variance_st_encoding_bitrate / rc->window_count -
499 (rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
500 perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
501 rc->avg_st_encoding_bitrate;
502 printf("Short-time stats, for window of %d frames: \n", rc->window_size);
503 printf("Average, rms-variance, and percent-fluct: %f %f %f \n",
504 rc->avg_st_encoding_bitrate, sqrt(rc->variance_st_encoding_bitrate),
505 perc_fluctuation);
506 printf("Num of input, num of encoded (super) frames: %d %d \n", frame_cnt,
507 tot_num_frames);
508}
509
511 size_t data_sz, uint64_t sizes[8],
512 int *count) {
513
514
515
516
517
518
519 uint8_t marker;
520
521 marker = *(data + data_sz - 1);
522 *count = 0;
523
524 if ((marker & 0xe0) == 0xc0) {
525 const uint32_t frames = (marker & 0x7) + 1;
526 const uint32_t mag = ((marker >> 3) & 0x3) + 1;
527 const size_t index_sz = 2 + mag * frames;
528
529
530
532
533 {
534 const uint8_t marker2 = *(data + data_sz - index_sz);
535
536
537
538
540 }
541
542 {
543
544 uint32_t i, j;
545 const uint8_t *x = &data[data_sz - index_sz + 1];
546
547 for (i = 0; i < frames; ++i) {
548 uint32_t this_sz = 0;
549
550 for (j = 0; j < mag; ++j) this_sz |= (*x++) << (j * 8);
551 sizes[i] = this_sz;
552 }
553 *count = frames;
554 }
555 }
557}
558#endif
559
560
561
562
563
564static void set_frame_flags_bypass_mode_ex0(
565 int tl, int num_spatial_layers, int is_key_frame,
567 int sl;
568 for (sl = 0; sl < num_spatial_layers; ++sl)
570
571 for (sl = 0; sl < num_spatial_layers; ++sl) {
572
573 if (tl == 0) {
575 if (sl) {
576 if (is_key_frame) {
579 } else {
581 }
582 } else {
584 }
586 } else if (tl == 1) {
589 (sl == 0) ? 0 : num_spatial_layers + sl - 1;
590 ref_frame_config->
alt_fb_idx[sl] = num_spatial_layers + sl;
591 }
592
593 if (!tl) {
594 if (!sl) {
595
601 } else {
602 if (is_key_frame) {
608 } else {
609
615 }
616 }
617 } else if (tl == 1) {
618 if (!sl) {
619
625 } else {
626
627 if (sl < num_spatial_layers - 1) {
633 } else if (sl == num_spatial_layers - 1) {
634
635
639 }
640 }
641 }
642 }
643}
644
645
646
647static void set_frame_flags_bypass_mode_ex1(
648 int tl, int num_spatial_layers, int is_key_frame,
650 int sl;
651 for (sl = 0; sl < num_spatial_layers; ++sl)
653
654 if (tl == 0) {
655 if (is_key_frame) {
658 } else {
661 }
663
667 }
668 if (tl == 1) {
672
676 }
677
678 if (tl == 0) {
679
685
686 if (is_key_frame) {
692 } else {
693
699 }
700 }
701 if (tl == 1) {
702
703
707 }
708}
709
710#if CONFIG_VP9_DECODER && !SIMULCAST_MODE
712 const int frames_out, int *mismatch_seen) {
715 if (*mismatch_seen) return;
716
718 ref_dec.idx = 0;
720 enc_img = ref_enc.img;
722 dec_img = ref_dec.img;
723#if CONFIG_VP9_HIGHBITDEPTH
728 enc_img.
d_w, enc_img.
d_h, 16);
729 vpx_img_truncate_16_to_8(&enc_img, &ref_enc.img);
730 }
733 dec_img.
d_w, dec_img.
d_h, 16);
734 vpx_img_truncate_16_to_8(&dec_img, &ref_dec.img);
735 }
736 }
737#endif
738
739 if (!compare_img(&enc_img, &dec_img)) {
740 int y[4], u[4], v[4];
741#if CONFIG_VP9_HIGHBITDEPTH
743 find_mismatch_high(&enc_img, &dec_img, y, u, v);
744 } else {
745 find_mismatch(&enc_img, &dec_img, y, u, v);
746 }
747#else
748 find_mismatch(&enc_img, &dec_img, y, u, v);
749#endif
751 printf(
752 "Encode/decode mismatch on frame %d at"
753 " Y[%d, %d] {%d/%d},"
754 " U[%d, %d] {%d/%d},"
755 " V[%d, %d] {%d/%d}\n",
756 frames_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
757 v[2], v[3]);
758 *mismatch_seen = frames_out;
759 }
760
763}
764#endif
765
766#if OUTPUT_RC_STATS
767static void svc_output_rc_stats(
770 struct RateControlStats *rc, VpxVideoWriter **outfile,
771 const uint32_t frame_cnt, const double framerate) {
772 int num_layers_encoded = 0;
773 unsigned int sl, tl;
774 uint64_t sizes[8];
775 uint64_t sizes_parsed[8];
776 int count = 0;
777 double sum_bitrate = 0.0;
778 double sum_bitrate2 = 0.0;
779 vp9_zero(sizes);
780 vp9_zero(sizes_parsed);
783 sizes_parsed, &count);
786 } else {
788 sizes[sl] = 0;
790 sizes[sl] = sizes_parsed[num_layers_encoded];
791 num_layers_encoded++;
792 }
793 }
794 }
796 unsigned int sl2;
797 uint64_t tot_size = 0;
798#if SIMULCAST_MODE
799 for (sl2 = 0; sl2 < sl; ++sl2) {
801 }
802 vpx_video_writer_write_frame(outfile[sl],
805#else
806 for (sl2 = 0; sl2 <= sl; ++sl2) {
808 }
809 if (tot_size > 0)
810 vpx_video_writer_write_frame(outfile[sl], cx_pkt->
data.
frame.
buf,
812#endif
813 }
817 ++tl) {
819 ++rc->layer_tot_enc_frames[layer];
820 rc->layer_encoding_bitrate[layer] += 8.0 * sizes[sl];
821
822
825 rc->layer_avg_frame_size[layer] += 8.0 * sizes[sl];
826 rc->layer_avg_rate_mismatch[layer] +=
827 fabs(8.0 * sizes[sl] - rc->layer_pfb[layer]) /
828 rc->layer_pfb[layer];
829 ++rc->layer_enc_frames[layer];
830 }
831 }
832 }
833 }
834
835
836
837
838 if (frame_cnt > (unsigned int)rc->window_size) {
841 sum_bitrate += 0.001 * 8.0 * sizes[sl] * framerate;
842 }
843 if (frame_cnt % rc->window_size == 0) {
844 rc->window_count += 1;
845 rc->avg_st_encoding_bitrate += sum_bitrate / rc->window_size;
846 rc->variance_st_encoding_bitrate +=
847 (sum_bitrate / rc->window_size) * (sum_bitrate / rc->window_size);
848 }
849 }
850
851
852 if (frame_cnt > (unsigned int)(rc->window_size + rc->window_size / 2)) {
854 sum_bitrate2 += 0.001 * 8.0 * sizes[sl] * framerate;
855 }
856
857 if (frame_cnt > (unsigned int)(2 * rc->window_size) &&
858 frame_cnt % rc->window_size == 0) {
859 rc->window_count += 1;
860 rc->avg_st_encoding_bitrate += sum_bitrate2 / rc->window_size;
861 rc->variance_st_encoding_bitrate +=
862 (sum_bitrate2 / rc->window_size) * (sum_bitrate2 / rc->window_size);
863 }
864 }
865}
866#endif
867
868int main(int argc, const char **argv) {
869 AppInput app_input;
870 VpxVideoWriter *writer = NULL;
871 VpxVideoInfo info;
874 SvcContext svc_ctx;
876 uint32_t i;
877 uint32_t frame_cnt = 0;
880 int pts = 0;
881 int frame_duration = 1;
882 int end_of_stream = 0;
883#if OUTPUT_FRAME_STATS
884 int frames_received = 0;
885#endif
886#if OUTPUT_RC_STATS
888 struct RateControlStats rc;
891 unsigned int sl;
892 double framerate = 30.0;
893#endif
894 struct vpx_usec_timer timer;
895 int64_t cx_time = 0;
896#if CONFIG_INTERNAL_STATS
897 FILE *f = fopen("opsnr.stt", "a");
898#endif
899#if CONFIG_VP9_DECODER && !SIMULCAST_MODE
900 int mismatch_seen = 0;
902#endif
903 memset(&svc_ctx, 0, sizeof(svc_ctx));
904 memset(&app_input, 0, sizeof(AppInput));
905 memset(&info, 0, sizeof(VpxVideoInfo));
907 memset(&rc, 0, sizeof(struct RateControlStats));
908 exec_name = argv[0];
909
910
911 app_input.input_ctx.framerate.numerator = 30;
912 app_input.input_ctx.framerate.denominator = 1;
913 app_input.input_ctx.only_i420 = 1;
914 app_input.input_ctx.bit_depth = 0;
915
916 parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg);
917
918
919 if (app_input.input_ctx.file_type != FILE_TYPE_Y4M) {
920
921#if CONFIG_VP9_HIGHBITDEPTH
925 enc_cfg.
g_w, enc_cfg.
g_h, 32)) {
926 die(
"Failed to allocate image %dx%d\n", enc_cfg.
g_w, enc_cfg.
g_h);
927 }
928#else
930 die(
"Failed to allocate image %dx%d\n", enc_cfg.
g_w, enc_cfg.
g_h);
931 }
932#endif
933 }
934
935
938 die("Failed to initialize encoder\n");
939#if CONFIG_VP9_DECODER && !SIMULCAST_MODE
941 &decoder, get_vpx_decoder_by_name("vp9")->codec_interface(), NULL, 0))
942 die("Failed to initialize decoder\n");
943#endif
944
945#if OUTPUT_RC_STATS
946 rc.window_count = 1;
947 rc.window_size = 15;
948 rc.avg_st_encoding_bitrate = 0.0;
949 rc.variance_st_encoding_bitrate = 0.0;
950 if (svc_ctx.output_rc_stat) {
951 set_rate_control_stats(&rc, &enc_cfg);
953 }
954#endif
955
956 info.codec_fourcc = VP9_FOURCC;
957 info.frame_width = enc_cfg.
g_w;
958 info.frame_height = enc_cfg.
g_h;
961
962 writer =
963 vpx_video_writer_open(app_input.output_filename, kContainerIVF, &info);
964 if (!writer)
965 die("Failed to open %s for writing\n", app_input.output_filename);
966
967#if OUTPUT_RC_STATS
968
969
970 if (svc_ctx.output_rc_stat) {
972 char file_name[PATH_MAX];
973
974 snprintf(file_name, sizeof(file_name), "%s_s%d.ivf",
975 app_input.output_filename, sl);
976 outfile[sl] = vpx_video_writer_open(file_name, kContainerIVF, &info);
977 if (!outfile[sl]) die("Failed to open %s for writing", file_name);
978 }
979 }
980#endif
981
982
983 for (i = 0; i < app_input.frames_to_skip; ++i)
984 read_frame(&app_input.input_ctx, &raw);
985
986 if (svc_ctx.speed != -1)
988 if (svc_ctx.threads) {
990 get_msb(svc_ctx.threads));
991 if (svc_ctx.threads > 1)
993 else
995 }
996 if (svc_ctx.speed >= 5 && svc_ctx.aqmode == 1)
998 if (svc_ctx.speed >= 5)
1001
1003 app_input.inter_layer_pred);
1004
1006
1008
1011
1013 for (sl = 0; sl < (unsigned int)svc_ctx.spatial_layers; ++sl)
1017
1018
1019 while (!end_of_stream) {
1022
1023
1024
1025
1026
1027 int example_pattern = 0;
1028 if (frame_cnt >= app_input.frames_to_code ||
1029 !read_frame(&app_input.input_ctx, &raw)) {
1030
1031
1032 end_of_stream = 1;
1033 }
1034
1035
1036
1037
1038
1039
1040
1042 layer_id.spatial_layer_id = 0;
1043
1044 if (frame_cnt % 2 == 0) {
1045 layer_id.temporal_layer_id = 0;
1047 layer_id.temporal_layer_id_per_spatial[i] = 0;
1048 } else {
1049 layer_id.temporal_layer_id = 1;
1051 layer_id.temporal_layer_id_per_spatial[i] = 1;
1052 }
1053 if (example_pattern == 1) {
1054
1055 assert(svc_ctx.spatial_layers == 2);
1056 assert(svc_ctx.temporal_layers == 2);
1057 if (frame_cnt % 2 == 0) {
1058
1059 layer_id.temporal_layer_id_per_spatial[0] = 0;
1060 layer_id.temporal_layer_id_per_spatial[1] = 0;
1061 layer_id.spatial_layer_id = 0;
1062 } else {
1063
1064 layer_id.temporal_layer_id_per_spatial[1] = 1;
1065 layer_id.spatial_layer_id = 1;
1066 }
1067 }
1069
1070
1071 if (example_pattern == 0) {
1072 set_frame_flags_bypass_mode_ex0(layer_id.temporal_layer_id,
1073 svc_ctx.spatial_layers, frame_cnt == 0,
1074 &ref_frame_config);
1075 } else if (example_pattern == 1) {
1076 set_frame_flags_bypass_mode_ex1(layer_id.temporal_layer_id,
1077 svc_ctx.spatial_layers, frame_cnt == 0,
1078 &ref_frame_config);
1079 }
1080 ref_frame_config.
duration[0] = frame_duration * 1;
1081 ref_frame_config.
duration[1] = frame_duration * 1;
1082
1084 &ref_frame_config);
1085
1086
1089 layer_id.temporal_layer_id];
1090 }
1091 } else {
1092
1093 unsigned int tl = 0;
1095 tl = (frame_cnt % 2 != 0);
1097 if (frame_cnt % 2 != 0) tl = 2;
1098 if ((frame_cnt > 1) && ((frame_cnt - 2) % 4 == 0)) tl = 1;
1099 }
1102 }
1103
1104 vpx_usec_timer_start(&timer);
1105 res = vpx_svc_encode(
1106 &svc_ctx, &encoder, (end_of_stream ? NULL : &raw), pts, frame_duration,
1108 vpx_usec_timer_mark(&timer);
1109 cx_time += vpx_usec_timer_elapsed(&timer);
1110
1111 fflush(stdout);
1113 die_codec(&encoder, "Failed to encode frame");
1114 }
1115
1117 switch (cx_pkt->
kind) {
1119 SvcInternal_t *const si = (SvcInternal_t *)svc_ctx.internal;
1121 vpx_video_writer_write_frame(writer, cx_pkt->
data.
frame.
buf,
1124#if OUTPUT_RC_STATS
1125 if (svc_ctx.output_rc_stat) {
1126 svc_output_rc_stats(&encoder, &enc_cfg, &layer_id, cx_pkt, &rc,
1127 outfile, frame_cnt, framerate);
1128 }
1129#endif
1130 }
1131#if OUTPUT_FRAME_STATS
1132 printf("SVC frame: %d, kf: %d, size: %d, pts: %d\n", frames_received,
1135 ++frames_received;
1136#endif
1139#if CONFIG_VP9_DECODER && !SIMULCAST_MODE
1142 die_codec(&decoder, "Failed to decode frame.");
1143#endif
1144 break;
1145 }
1149 break;
1150 }
1151 default: {
1152 break;
1153 }
1154 }
1155
1156#if CONFIG_VP9_DECODER && !SIMULCAST_MODE
1158
1159
1161 !(layer_id.temporal_layer_id > 0 &&
1165 test_decode(&encoder, &decoder, frame_cnt, &mismatch_seen);
1166 }
1167#endif
1168 }
1169
1170 if (!end_of_stream) {
1171 ++frame_cnt;
1172 pts += frame_duration;
1173 }
1174 }
1175
1176 printf("Processed %d frames\n", frame_cnt);
1177
1178 close_input_file(&app_input.input_ctx);
1179
1180#if OUTPUT_RC_STATS
1181 if (svc_ctx.output_rc_stat) {
1182 printout_rate_control_summary(&rc, &enc_cfg, frame_cnt);
1183 printf("\n");
1184 }
1185#endif
1187 die_codec(&encoder, "Failed to destroy codec");
1188 if (writer) {
1189 vpx_video_writer_close(writer);
1190 }
1191#if OUTPUT_RC_STATS
1192 if (svc_ctx.output_rc_stat) {
1194 vpx_video_writer_close(outfile[sl]);
1195 }
1196 }
1197#endif
1198#if CONFIG_INTERNAL_STATS
1199 if (mismatch_seen) {
1200 fprintf(f, "First mismatch occurred in frame %d\n", mismatch_seen);
1201 } else {
1202 fprintf(f, "No mismatch detected in recon buffers\n");
1203 }
1204 fclose(f);
1205#endif
1206 printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
1207 frame_cnt, 1000 * (float)cx_time / (double)(frame_cnt * 1000000),
1208 1000000 * (double)frame_cnt / (double)cx_time);
1209 if (app_input.input_ctx.file_type != FILE_TYPE_Y4M) {
1211 }
1212
1213 vpx_svc_dump_statistics(&svc_ctx);
1214 vpx_svc_release(&svc_ctx);
1215 return EXIT_SUCCESS;
1216}
const char * vpx_codec_err_to_string(vpx_codec_err_t err)
Convert error number to printable string.
struct vpx_codec_ctx vpx_codec_ctx_t
Codec context structure.
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
const void * vpx_codec_iter_t
Iterator.
Definition vpx_codec.h:190
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition vpx_codec.h:408
vpx_codec_err_t
Algorithm return codes.
Definition vpx_codec.h:93
@ VPX_CODEC_CORRUPT_FRAME
The coded data for this stream is corrupt or incomplete.
Definition vpx_codec.h:133
@ VPX_CODEC_OK
Operation completed without error.
Definition vpx_codec.h:95
@ VPX_BITS_8
Definition vpx_codec.h:221
@ VPX_BITS_12
Definition vpx_codec.h:223
@ VPX_BITS_10
Definition vpx_codec.h:222
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.
#define vpx_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_dec_init_ver()
Definition vpx_decoder.h:143
#define VPX_DL_REALTIME
deadline parameter analogous to VPx REALTIME mode.
Definition vpx_encoder.h:992
struct vpx_codec_enc_cfg vpx_codec_enc_cfg_t
Encoder configuration structure.
#define VPX_DL_GOOD_QUALITY
deadline parameter analogous to VPx GOOD QUALITY mode.
Definition vpx_encoder.h:994
const vpx_codec_cx_pkt_t * vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Encoded data iterator.
#define VPX_MAX_LAYERS
Definition vpx_encoder.h:45
#define VPX_FRAME_IS_KEY
Definition vpx_encoder.h:119
#define VPX_SS_MAX_LAYERS
Definition vpx_encoder.h:48
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.
struct vpx_codec_cx_pkt vpx_codec_cx_pkt_t
Encoder output packet.
@ VPX_CODEC_CX_FRAME_PKT
Definition vpx_encoder.h:151
@ VPX_CODEC_STATS_PKT
Definition vpx_encoder.h:152
@ VPX_RC_ONE_PASS
Definition vpx_encoder.h:229
@ VPX_CQ
Definition vpx_encoder.h:238
vpx_codec_iface_t * vpx_codec_vp9_cx(void)
The interface to the VP9 encoder.
struct vpx_svc_layer_id vpx_svc_layer_id_t
vp9 svc layer parameters
struct vpx_svc_ref_frame_config vpx_svc_ref_frame_config_t
vp9 svc frame flag parameters.
struct vpx_svc_frame_drop vpx_svc_frame_drop_t
vp9 svc frame dropping parameters.
@ FULL_SUPERFRAME_DROP
Definition vp8cx.h:933
@ VP9E_SET_SVC_LAYER_ID
Codec control function to set svc layer for spatial and temporal.
Definition vp8cx.h:471
@ VP8E_SET_MAX_INTRA_BITRATE_PCT
Codec control function to set Max data rate for Intra frames.
Definition vp8cx.h:275
@ VP9E_SET_SVC_INTER_LAYER_PRED
Codec control function to constrain the inter-layer prediction (prediction of lower spatial resolutio...
Definition vp8cx.h:625
@ VP9E_SET_AQ_MODE
Codec control function to set adaptive quantization mode.
Definition vp8cx.h:416
@ VP9E_SET_DISABLE_OVERSHOOT_MAXQ_CBR
Codec control function to disable increase Q on overshoot in CBR.
Definition vp8cx.h:700
@ VP9E_SET_TUNE_CONTENT
Codec control function to set content type.
Definition vp8cx.h:481
@ VP9E_SET_ROW_MT
Codec control function to set row level multi-threading.
Definition vp8cx.h:576
@ VP8E_SET_CPUUSED
Codec control function to set encoder internal speed settings.
Definition vp8cx.h:173
@ VP9E_SET_TILE_COLUMNS
Codec control function to set number of tile columns.
Definition vp8cx.h:369
@ VP9E_SET_SVC_FRAME_DROP_LAYER
Codec control function to set mode and thresholds for frame dropping in SVC. Drop frame thresholds ar...
Definition vp8cx.h:634
@ VP9E_SET_SVC_REF_FRAME_CONFIG
Codec control function to set the frame flags and buffer indices for spatial layers....
Definition vp8cx.h:551
@ VP8E_SET_STATIC_THRESHOLD
Codec control function to set the threshold for MBs treated static.
Definition vp8cx.h:206
@ VP9E_SET_DISABLE_LOOPFILTER
Codec control function to disable loopfilter.
Definition vp8cx.h:709
@ VP9E_SET_NOISE_SENSITIVITY
Codec control function to set noise sensitivity.
Definition vp8cx.h:439
@ VP9E_GET_SVC_LAYER_ID
Codec control function to get svc layer ID.
Definition vp8cx.h:489
@ VP9E_TEMPORAL_LAYERING_MODE_BYPASS
Bypass mode. Used when application needs to control temporal layering. This will only work when the n...
Definition vp8cx.h:800
@ VP9_GET_REFERENCE
Definition vp8.h:55
VP9 specific reference frame data struct.
Definition vp8.h:110
int idx
Definition vp8.h:111
vpx_codec_err_t err
Definition vpx_codec.h:203
union vpx_codec_cx_pkt::@105367030154200007005241002351245163342006201240 data
struct vpx_codec_cx_pkt::@105367030154200007005241002351245163342006201240::@337301343345304110063267327113124066016321050157 frame
vpx_codec_frame_flags_t flags
Definition vpx_encoder.h:173
vpx_fixed_buf_t twopass_stats
Definition vpx_encoder.h:186
enum vpx_codec_cx_pkt_kind kind
Definition vpx_encoder.h:164
uint8_t spatial_layer_encoded[5]
Flag to indicate if spatial layer frame in this packet is encoded or dropped. VP8 will always be set ...
Definition vpx_encoder.h:184
size_t sz
Definition vpx_encoder.h:168
void * buf
Definition vpx_encoder.h:167
vpx_codec_pts_t pts
time stamp to show frame (in timebase units)
Definition vpx_encoder.h:170
int temporal_layering_mode
Temporal layering mode indicating which temporal layering scheme to use.
Definition vpx_encoder.h:697
unsigned int kf_min_dist
Keyframe minimum interval.
Definition vpx_encoder.h:609
unsigned int ts_number_layers
Number of temporal coding layers.
Definition vpx_encoder.h:648
unsigned int ss_number_layers
Number of spatial coding layers.
Definition vpx_encoder.h:628
unsigned int rc_2pass_vbr_minsection_pct
Two-pass mode per-GOP minimum bitrate.
Definition vpx_encoder.h:574
unsigned int g_profile
Bitstream profile to use.
Definition vpx_encoder.h:299
unsigned int layer_target_bitrate[12]
Target bitrate for each spatial/temporal layer.
Definition vpx_encoder.h:688
unsigned int g_h
Height of the frame.
Definition vpx_encoder.h:317
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition vpx_encoder.h:355
unsigned int g_w
Width of the frame.
Definition vpx_encoder.h:308
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition vpx_encoder.h:395
struct vpx_rational g_timebase
Stream timebase units.
Definition vpx_encoder.h:347
enum vpx_enc_pass g_pass
Multi-pass Encoding Mode.
Definition vpx_encoder.h:362
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition vpx_encoder.h:376
enum vpx_rc_mode rc_end_usage
Rate control algorithm to use.
Definition vpx_encoder.h:444
vpx_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition vpx_encoder.h:325
unsigned int rc_2pass_vbr_maxsection_pct
Two-pass mode per-GOP maximum bitrate.
Definition vpx_encoder.h:581
unsigned int rc_target_bitrate
Target data rate.
Definition vpx_encoder.h:464
unsigned int g_input_bit_depth
Bit-depth of the input frames.
Definition vpx_encoder.h:333
unsigned int ts_rate_decimator[5]
Frame rate decimation factor for each temporal layer.
Definition vpx_encoder.h:662
unsigned int kf_max_dist
Keyframe maximum interval.
Definition vpx_encoder.h:618
size_t sz
Definition vpx_encoder.h:101
void * buf
Definition vpx_encoder.h:100
vpx_img_fmt_t fmt
Definition vpx_image.h:73
unsigned int d_h
Definition vpx_image.h:84
unsigned int d_w
Definition vpx_image.h:83
int den
Definition vpx_encoder.h:224
int num
Definition vpx_encoder.h:223
int framedrop_thresh[5]
Definition vp8cx.h:946
SVC_LAYER_DROP_MODE framedrop_mode
Definition vp8cx.h:948
int max_consec_drop
Definition vp8cx.h:949
int temporal_layer_id
Definition vp8cx.h:897
int lst_fb_idx[5]
Definition vp8cx.h:910
int update_buffer_slot[5]
Definition vp8cx.h:913
int gld_fb_idx[5]
Definition vp8cx.h:911
int reference_last[5]
Definition vp8cx.h:918
int reference_golden[5]
Definition vp8cx.h:919
int reference_alt_ref[5]
Definition vp8cx.h:920
int64_t duration[5]
Definition vp8cx.h:921
int alt_fb_idx[5]
Definition vp8cx.h:912
Provides definitions for using VP8 or VP9 encoder algorithm within the vpx Codec Interface.
Describes the encoder algorithm interface to applications.
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.
#define VPX_IMG_FMT_HIGHBITDEPTH
Definition vpx_image.h:35
@ VPX_IMG_FMT_I42016
Definition vpx_image.h:47
@ VPX_IMG_FMT_I420
Definition vpx_image.h:42
struct vpx_image vpx_image_t
Image Descriptor.
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.