WebM Codec SDK
vpx_temporal_svc_encoder
1 /*
2  * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3  *
4  * Use of this source code is governed by a BSD-style license
5  * that can be found in the LICENSE file in the root of the source
6  * tree. An additional intellectual property rights grant can be found
7  * in the file PATENTS. All contributing project authors may
8  * be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 // This is an example demonstrating how to implement a multi-layer VPx
12 // encoding scheme based on temporal scalability for video applications
13 // that benefit from a scalable bitstream.
14 
15 #include <assert.h>
16 #include <math.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 
21 #include "./vpx_config.h"
22 #include "../vpx_ports/vpx_timer.h"
23 #include "vpx/vp8cx.h"
24 #include "vpx/vpx_encoder.h"
25 
26 #include "../tools_common.h"
27 #include "../video_writer.h"
28 
29 static const char *exec_name;
30 
31 void usage_exit(void) { exit(EXIT_FAILURE); }
32 
33 // Denoiser states, for temporal denoising.
34 enum denoiserState {
35  kDenoiserOff,
36  kDenoiserOnYOnly,
37  kDenoiserOnYUV,
38  kDenoiserOnYUVAggressive,
39  kDenoiserOnAdaptive
40 };
41 
42 static int mode_to_num_layers[13] = { 1, 2, 2, 3, 3, 3, 3, 5, 2, 3, 3, 3, 3 };
43 
44 // For rate control encoding stats.
45 struct RateControlMetrics {
46  // Number of input frames per layer.
47  int layer_input_frames[VPX_TS_MAX_LAYERS];
48  // Total (cumulative) number of encoded frames per layer.
49  int layer_tot_enc_frames[VPX_TS_MAX_LAYERS];
50  // Number of encoded non-key frames per layer.
51  int layer_enc_frames[VPX_TS_MAX_LAYERS];
52  // Framerate per layer layer (cumulative).
53  double layer_framerate[VPX_TS_MAX_LAYERS];
54  // Target average frame size per layer (per-frame-bandwidth per layer).
55  double layer_pfb[VPX_TS_MAX_LAYERS];
56  // Actual average frame size per layer.
57  double layer_avg_frame_size[VPX_TS_MAX_LAYERS];
58  // Average rate mismatch per layer (|target - actual| / target).
59  double layer_avg_rate_mismatch[VPX_TS_MAX_LAYERS];
60  // Actual encoding bitrate per layer (cumulative).
61  double layer_encoding_bitrate[VPX_TS_MAX_LAYERS];
62  // Average of the short-time encoder actual bitrate.
63  // TODO(marpan): Should we add these short-time stats for each layer?
64  double avg_st_encoding_bitrate;
65  // Variance of the short-time encoder actual bitrate.
66  double variance_st_encoding_bitrate;
67  // Window (number of frames) for computing short-timee encoding bitrate.
68  int window_size;
69  // Number of window measurements.
70  int window_count;
71  int layer_target_bitrate[VPX_MAX_LAYERS];
72 };
73 
74 // Note: these rate control metrics assume only 1 key frame in the
75 // sequence (i.e., first frame only). So for temporal pattern# 7
76 // (which has key frame for every frame on base layer), the metrics
77 // computation will be off/wrong.
78 // TODO(marpan): Update these metrics to account for multiple key frames
79 // in the stream.
80 static void set_rate_control_metrics(struct RateControlMetrics *rc,
81  vpx_codec_enc_cfg_t *cfg) {
82  unsigned int i = 0;
83  // Set the layer (cumulative) framerate and the target layer (non-cumulative)
84  // per-frame-bandwidth, for the rate control encoding stats below.
85  const double framerate = cfg->g_timebase.den / cfg->g_timebase.num;
86  rc->layer_framerate[0] = framerate / cfg->ts_rate_decimator[0];
87  rc->layer_pfb[0] =
88  1000.0 * rc->layer_target_bitrate[0] / rc->layer_framerate[0];
89  for (i = 0; i < cfg->ts_number_layers; ++i) {
90  if (i > 0) {
91  rc->layer_framerate[i] = framerate / cfg->ts_rate_decimator[i];
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]);
95  }
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;
102  }
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;
107 }
108 
109 static void printout_rate_control_summary(struct RateControlMetrics *rc,
110  vpx_codec_enc_cfg_t *cfg,
111  int frame_cnt) {
112  unsigned int i = 0;
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",
117  cfg->ts_number_layers);
118  for (i = 0; i < cfg->ts_number_layers; ++i) {
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] /
125  tot_num_frames;
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]);
136  printf(
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]);
141  printf("\n");
142  }
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),
152  perc_fluctuation);
153  if ((frame_cnt - 1) != tot_num_frames)
154  die("Error: Number of input frames not equal to output! \n");
155 }
156 
157 // Temporal scaling parameters:
158 // NOTE: The 3 prediction frames cannot be used interchangeably due to
159 // differences in the way they are handled throughout the code. The
160 // frames should be allocated to layers in the order LAST, GF, ARF.
161 // Other combinations work, but may produce slightly inferior results.
162 static void set_temporal_layer_pattern(int layering_mode,
163  vpx_codec_enc_cfg_t *cfg,
164  int *layer_flags,
165  int *flag_periodicity) {
166  switch (layering_mode) {
167  case 0: {
168  // 1-layer.
169  int ids[1] = { 0 };
170  cfg->ts_periodicity = 1;
171  *flag_periodicity = 1;
172  cfg->ts_number_layers = 1;
173  cfg->ts_rate_decimator[0] = 1;
174  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
175  // Update L only.
176  layer_flags[0] =
178  break;
179  }
180  case 1: {
181  // 2-layers, 2-frame period.
182  int ids[2] = { 0, 1 };
183  cfg->ts_periodicity = 2;
184  *flag_periodicity = 2;
185  cfg->ts_number_layers = 2;
186  cfg->ts_rate_decimator[0] = 2;
187  cfg->ts_rate_decimator[1] = 1;
188  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
189 #if 1
190  // 0=L, 1=GF, Intra-layer prediction enabled.
191  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF |
194  layer_flags[1] =
196 #else
197  // 0=L, 1=GF, Intra-layer prediction disabled.
198  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF |
201  layer_flags[1] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
203 #endif
204  break;
205  }
206  case 2: {
207  // 2-layers, 3-frame period.
208  int ids[3] = { 0, 1, 1 };
209  cfg->ts_periodicity = 3;
210  *flag_periodicity = 3;
211  cfg->ts_number_layers = 2;
212  cfg->ts_rate_decimator[0] = 3;
213  cfg->ts_rate_decimator[1] = 1;
214  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
215  // 0=L, 1=GF, Intra-layer prediction enabled.
216  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
219  layer_flags[1] = layer_flags[2] =
222  break;
223  }
224  case 3: {
225  // 3-layers, 6-frame period.
226  int ids[6] = { 0, 2, 2, 1, 2, 2 };
227  cfg->ts_periodicity = 6;
228  *flag_periodicity = 6;
229  cfg->ts_number_layers = 3;
230  cfg->ts_rate_decimator[0] = 6;
231  cfg->ts_rate_decimator[1] = 3;
232  cfg->ts_rate_decimator[2] = 1;
233  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
234  // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
235  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
238  layer_flags[3] =
240  layer_flags[1] = layer_flags[2] = layer_flags[4] = layer_flags[5] =
242  break;
243  }
244  case 4: {
245  // 3-layers, 4-frame period.
246  int ids[4] = { 0, 2, 1, 2 };
247  cfg->ts_periodicity = 4;
248  *flag_periodicity = 4;
249  cfg->ts_number_layers = 3;
250  cfg->ts_rate_decimator[0] = 4;
251  cfg->ts_rate_decimator[1] = 2;
252  cfg->ts_rate_decimator[2] = 1;
253  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
254  // 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
255  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
258  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
260  layer_flags[1] = layer_flags[3] =
263  break;
264  }
265  case 5: {
266  // 3-layers, 4-frame period.
267  int ids[4] = { 0, 2, 1, 2 };
268  cfg->ts_periodicity = 4;
269  *flag_periodicity = 4;
270  cfg->ts_number_layers = 3;
271  cfg->ts_rate_decimator[0] = 4;
272  cfg->ts_rate_decimator[1] = 2;
273  cfg->ts_rate_decimator[2] = 1;
274  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
275  // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled in layer 1, disabled
276  // in layer 2.
277  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
280  layer_flags[2] =
282  layer_flags[1] = layer_flags[3] =
285  break;
286  }
287  case 6: {
288  // 3-layers, 4-frame period.
289  int ids[4] = { 0, 2, 1, 2 };
290  cfg->ts_periodicity = 4;
291  *flag_periodicity = 4;
292  cfg->ts_number_layers = 3;
293  cfg->ts_rate_decimator[0] = 4;
294  cfg->ts_rate_decimator[1] = 2;
295  cfg->ts_rate_decimator[2] = 1;
296  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
297  // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
298  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
301  layer_flags[2] =
303  layer_flags[1] = layer_flags[3] =
305  break;
306  }
307  case 7: {
308  // NOTE: Probably of academic interest only.
309  // 5-layers, 16-frame period.
310  int ids[16] = { 0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4 };
311  cfg->ts_periodicity = 16;
312  *flag_periodicity = 16;
313  cfg->ts_number_layers = 5;
314  cfg->ts_rate_decimator[0] = 16;
315  cfg->ts_rate_decimator[1] = 8;
316  cfg->ts_rate_decimator[2] = 4;
317  cfg->ts_rate_decimator[3] = 2;
318  cfg->ts_rate_decimator[4] = 1;
319  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
320  layer_flags[0] = VPX_EFLAG_FORCE_KF;
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] =
329  layer_flags[8] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF;
330  break;
331  }
332  case 8: {
333  // 2-layers, with sync point at first frame of layer 1.
334  int ids[2] = { 0, 1 };
335  cfg->ts_periodicity = 2;
336  *flag_periodicity = 8;
337  cfg->ts_number_layers = 2;
338  cfg->ts_rate_decimator[0] = 2;
339  cfg->ts_rate_decimator[1] = 1;
340  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
341  // 0=L, 1=GF.
342  // ARF is used as predictor for all frames, and is only updated on
343  // key frame. Sync point every 8 frames.
344 
345  // Layer 0: predict from L and ARF, update L and G.
346  layer_flags[0] =
348  // Layer 1: sync point: predict from L and ARF, and update G.
349  layer_flags[1] =
351  // Layer 0, predict from L and ARF, update L.
352  layer_flags[2] =
354  // Layer 1: predict from L, G and ARF, and update G.
355  layer_flags[3] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
357  // Layer 0.
358  layer_flags[4] = layer_flags[2];
359  // Layer 1.
360  layer_flags[5] = layer_flags[3];
361  // Layer 0.
362  layer_flags[6] = layer_flags[4];
363  // Layer 1.
364  layer_flags[7] = layer_flags[5];
365  break;
366  }
367  case 9: {
368  // 3-layers: Sync points for layer 1 and 2 every 8 frames.
369  int ids[4] = { 0, 2, 1, 2 };
370  cfg->ts_periodicity = 4;
371  *flag_periodicity = 8;
372  cfg->ts_number_layers = 3;
373  cfg->ts_rate_decimator[0] = 4;
374  cfg->ts_rate_decimator[1] = 2;
375  cfg->ts_rate_decimator[2] = 1;
376  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
377  // 0=L, 1=GF, 2=ARF.
378  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
381  layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
383  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
385  layer_flags[3] = layer_flags[5] =
387  layer_flags[4] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
389  layer_flags[6] =
391  layer_flags[7] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
393  break;
394  }
395  case 10: {
396  // 3-layers structure where ARF is used as predictor for all frames,
397  // and is only updated on key frame.
398  // Sync points for layer 1 and 2 every 8 frames.
399 
400  int ids[4] = { 0, 2, 1, 2 };
401  cfg->ts_periodicity = 4;
402  *flag_periodicity = 8;
403  cfg->ts_number_layers = 3;
404  cfg->ts_rate_decimator[0] = 4;
405  cfg->ts_rate_decimator[1] = 2;
406  cfg->ts_rate_decimator[2] = 1;
407  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
408  // 0=L, 1=GF, 2=ARF.
409  // Layer 0: predict from L and ARF; update L and G.
410  layer_flags[0] =
412  // Layer 2: sync point: predict from L and ARF; update none.
413  layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF |
416  // Layer 1: sync point: predict from L and ARF; update G.
417  layer_flags[2] =
419  // Layer 2: predict from L, G, ARF; update none.
420  layer_flags[3] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
422  // Layer 0: predict from L and ARF; update L.
423  layer_flags[4] =
425  // Layer 2: predict from L, G, ARF; update none.
426  layer_flags[5] = layer_flags[3];
427  // Layer 1: predict from L, G, ARF; update G.
428  layer_flags[6] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
429  // Layer 2: predict from L, G, ARF; update none.
430  layer_flags[7] = layer_flags[3];
431  break;
432  }
433  case 11: {
434  // 3-layers structure with one reference frame.
435  // This works same as temporal_layering_mode 3.
436  // This was added to compare with vp9_spatial_svc_encoder.
437 
438  // 3-layers, 4-frame period.
439  int ids[4] = { 0, 2, 1, 2 };
440  cfg->ts_periodicity = 4;
441  *flag_periodicity = 4;
442  cfg->ts_number_layers = 3;
443  cfg->ts_rate_decimator[0] = 4;
444  cfg->ts_rate_decimator[1] = 2;
445  cfg->ts_rate_decimator[2] = 1;
446  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
447  // 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
448  layer_flags[0] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
450  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
452  layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
454  layer_flags[3] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_ARF |
456  break;
457  }
458  case 12:
459  default: {
460  // 3-layers structure as in case 10, but no sync/refresh points for
461  // layer 1 and 2.
462  int ids[4] = { 0, 2, 1, 2 };
463  cfg->ts_periodicity = 4;
464  *flag_periodicity = 8;
465  cfg->ts_number_layers = 3;
466  cfg->ts_rate_decimator[0] = 4;
467  cfg->ts_rate_decimator[1] = 2;
468  cfg->ts_rate_decimator[2] = 1;
469  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
470  // 0=L, 1=GF, 2=ARF.
471  // Layer 0: predict from L and ARF; update L.
472  layer_flags[0] =
474  layer_flags[4] = layer_flags[0];
475  // Layer 1: predict from L, G, ARF; update G.
476  layer_flags[2] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
477  layer_flags[6] = layer_flags[2];
478  // Layer 2: predict from L, G, ARF; update none.
479  layer_flags[1] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
481  layer_flags[3] = layer_flags[1];
482  layer_flags[5] = layer_flags[1];
483  layer_flags[7] = layer_flags[1];
484  break;
485  }
486  }
487 }
488 
489 int main(int argc, char **argv) {
490  VpxVideoWriter *outfile[VPX_TS_MAX_LAYERS] = { NULL };
491  vpx_codec_ctx_t codec;
493  int frame_cnt = 0;
494  vpx_image_t raw;
495  vpx_codec_err_t res;
496  unsigned int width;
497  unsigned int height;
498  uint32_t error_resilient = 0;
499  int speed;
500  int frame_avail;
501  int got_data;
502  int flags = 0;
503  unsigned int i;
504  int pts = 0; // PTS starts at 0.
505  int frame_duration = 1; // 1 timebase tick per frame.
506  int layering_mode = 0;
507  int layer_flags[VPX_TS_MAX_PERIODICITY] = { 0 };
508  int flag_periodicity = 1;
509 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION)
510  vpx_svc_layer_id_t layer_id = { 0, 0 };
511 #else
512  vpx_svc_layer_id_t layer_id = { 0 };
513 #endif
514  const VpxInterface *encoder = NULL;
515  FILE *infile = NULL;
516  struct RateControlMetrics rc;
517  int64_t cx_time = 0;
518  const int min_args_base = 13;
519 #if CONFIG_VP9_HIGHBITDEPTH
520  vpx_bit_depth_t bit_depth = VPX_BITS_8;
521  int input_bit_depth = 8;
522  const int min_args = min_args_base + 1;
523 #else
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;
529 
530  exec_name = argv[0];
531  // Check usage and arguments.
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",
538  argv[0]);
539 #else
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",
544  argv[0]);
545 #endif // CONFIG_VP9_HIGHBITDEPTH
546  }
547 
548  encoder = get_vpx_encoder_by_name(argv[3]);
549  if (!encoder) die("Unsupported codec.");
550 
551  printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
552 
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);
557  }
558 
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]);
562  }
563 
564  if (argc != min_args + mode_to_num_layers[layering_mode]) {
565  die("Invalid number of arguments");
566  }
567 
568 #if CONFIG_VP9_HIGHBITDEPTH
569  switch (strtol(argv[argc - 1], NULL, 0)) {
570  case 8:
571  bit_depth = VPX_BITS_8;
572  input_bit_depth = 8;
573  break;
574  case 10:
575  bit_depth = VPX_BITS_10;
576  input_bit_depth = 10;
577  break;
578  case 12:
579  bit_depth = VPX_BITS_12;
580  input_bit_depth = 12;
581  break;
582  default: die("Invalid bit depth (8, 10, 12) %s", argv[argc - 1]);
583  }
584  if (!vpx_img_alloc(
585  &raw, bit_depth == VPX_BITS_8 ? VPX_IMG_FMT_I420 : VPX_IMG_FMT_I42016,
586  width, height, 32)) {
587  die("Failed to allocate image", width, height);
588  }
589 #else
590  if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 32)) {
591  die("Failed to allocate image", width, height);
592  }
593 #endif // CONFIG_VP9_HIGHBITDEPTH
594 
595  // Populate encoder configuration.
596  res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
597  if (res) {
598  printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
599  return EXIT_FAILURE;
600  }
601 
602  // Update the default configuration with our settings.
603  cfg.g_w = width;
604  cfg.g_h = height;
605 
606 #if CONFIG_VP9_HIGHBITDEPTH
607  if (bit_depth != VPX_BITS_8) {
608  cfg.g_bit_depth = bit_depth;
609  cfg.g_input_bit_depth = input_bit_depth;
610  cfg.g_profile = 2;
611  }
612 #endif // CONFIG_VP9_HIGHBITDEPTH
613 
614  // Timebase format e.g. 30fps: numerator=1, demoninator = 30.
615  cfg.g_timebase.num = (int)strtol(argv[6], NULL, 0);
616  cfg.g_timebase.den = (int)strtol(argv[7], NULL, 0);
617 
618  speed = (int)strtol(argv[8], NULL, 0);
619  if (speed < 0) {
620  die("Invalid speed setting: must be positive");
621  }
622 
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)
627  cfg.ts_target_bitrate[i - 13] = rc.layer_target_bitrate[i - 13];
628  else if (strncmp(encoder->name, "vp9", 3) == 0)
629  cfg.layer_target_bitrate[i - 13] = rc.layer_target_bitrate[i - 13];
630  }
631 
632  // Real time parameters.
633  cfg.rc_dropframe_thresh = (unsigned int)strtoul(argv[9], NULL, 0);
634  cfg.rc_end_usage = VPX_CBR;
635  cfg.rc_min_quantizer = 2;
636  cfg.rc_max_quantizer = 56;
637  if (strncmp(encoder->name, "vp9", 3) == 0) cfg.rc_max_quantizer = 52;
638  cfg.rc_undershoot_pct = 50;
639  cfg.rc_overshoot_pct = 50;
640  cfg.rc_buf_initial_sz = 500;
641  cfg.rc_buf_optimal_sz = 600;
642  cfg.rc_buf_sz = 1000;
643 
644  // Disable dynamic resizing by default.
645  cfg.rc_resize_allowed = 0;
646 
647  // Use 1 thread as default.
648  cfg.g_threads = (unsigned int)strtoul(argv[11], NULL, 0);
649 
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);
653  }
654  // Enable error resilient mode.
655  cfg.g_error_resilient = error_resilient;
656  cfg.g_lag_in_frames = 0;
657  cfg.kf_mode = VPX_KF_AUTO;
658 
659  // Disable automatic keyframe placement.
660  cfg.kf_min_dist = cfg.kf_max_dist = 3000;
661 
663 
664  set_temporal_layer_pattern(layering_mode, &cfg, layer_flags,
665  &flag_periodicity);
666 
667  set_rate_control_metrics(&rc, &cfg);
668 
669  // Target bandwidth for the whole stream.
670  // Set to layer_target_bitrate for highest layer (total bitrate).
671  cfg.rc_target_bitrate = rc.layer_target_bitrate[cfg.ts_number_layers - 1];
672 
673  // Open input file.
674  if (!(infile = fopen(argv[1], "rb"))) {
675  die("Failed to open %s for reading", argv[1]);
676  }
677 
678  framerate = cfg.g_timebase.den / cfg.g_timebase.num;
679  // Open an output file for each stream.
680  for (i = 0; i < cfg.ts_number_layers; ++i) {
681  char file_name[PATH_MAX];
682  VpxVideoInfo info;
683  info.codec_fourcc = encoder->fourcc;
684  info.frame_width = cfg.g_w;
685  info.frame_height = cfg.g_h;
686  info.time_base.numerator = cfg.g_timebase.num;
687  info.time_base.denominator = cfg.g_timebase.den;
688 
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);
692 
693  assert(outfile[i] != NULL);
694  }
695  // No spatial layers in this encoder.
696  cfg.ss_number_layers = 1;
697 
698 // Initialize codec.
699 #if CONFIG_VP9_HIGHBITDEPTH
700  if (vpx_codec_enc_init(
701  &codec, encoder->codec_interface(), &cfg,
702  bit_depth == VPX_BITS_8 ? 0 : VPX_CODEC_USE_HIGHBITDEPTH))
703 #else
704  if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
705 #endif // CONFIG_VP9_HIGHBITDEPTH
706  die_codec(&codec, "Failed to initialize encoder");
707 
708  if (strncmp(encoder->name, "vp8", 3) == 0) {
709  vpx_codec_control(&codec, VP8E_SET_CPUUSED, -speed);
710  vpx_codec_control(&codec, VP8E_SET_NOISE_SENSITIVITY, kDenoiserOff);
713  } else if (strncmp(encoder->name, "vp9", 3) == 0) {
714  vpx_svc_extra_cfg_t svc_params;
715  memset(&svc_params, 0, sizeof(svc_params));
716  vpx_codec_control(&codec, VP8E_SET_CPUUSED, speed);
721  vpx_codec_control(&codec, VP9E_SET_NOISE_SENSITIVITY, kDenoiserOff);
725  // TODO(marpan/jianj): There is an issue with row-mt for low resolutons at
726  // high speed settings, disable its use for those cases for now.
727  if (cfg.g_threads > 1 && ((cfg.g_w > 320 && cfg.g_h > 240) || speed < 7))
729  else
731  if (vpx_codec_control(&codec, VP9E_SET_SVC, layering_mode > 0 ? 1 : 0))
732  die_codec(&codec, "Failed to set SVC");
733  for (i = 0; i < cfg.ts_number_layers; ++i) {
734  svc_params.max_quantizers[i] = cfg.rc_max_quantizer;
735  svc_params.min_quantizers[i] = cfg.rc_min_quantizer;
736  }
737  svc_params.scaling_factor_num[0] = cfg.g_h;
738  svc_params.scaling_factor_den[0] = cfg.g_h;
739  vpx_codec_control(&codec, VP9E_SET_SVC_PARAMETERS, &svc_params);
740  }
741  if (strncmp(encoder->name, "vp8", 3) == 0) {
743  }
745  // This controls the maximum target size of the key frame.
746  // For generating smaller key frames, use a smaller max_intra_size_pct
747  // value, like 100 or 200.
748  {
749  const int max_intra_size_pct = 900;
751  max_intra_size_pct);
752  }
753 
754  frame_avail = 1;
755  while (frame_avail || got_data) {
756  struct vpx_usec_timer timer;
757  vpx_codec_iter_t iter = NULL;
758  const vpx_codec_cx_pkt_t *pkt;
759 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION)
760  // Update the temporal layer_id. No spatial layers in this test.
761  layer_id.spatial_layer_id = 0;
762 #endif
763  layer_id.temporal_layer_id =
764  cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
765  if (strncmp(encoder->name, "vp9", 3) == 0) {
766  vpx_codec_control(&codec, VP9E_SET_SVC_LAYER_ID, &layer_id);
767  } else if (strncmp(encoder->name, "vp8", 3) == 0) {
769  layer_id.temporal_layer_id);
770  }
771  flags = layer_flags[frame_cnt % flag_periodicity];
772  if (layering_mode == 0) flags = 0;
773  frame_avail = vpx_img_read(&raw, infile);
774  if (frame_avail) ++rc.layer_input_frames[layer_id.temporal_layer_id];
775  vpx_usec_timer_start(&timer);
776  if (vpx_codec_encode(&codec, frame_avail ? &raw : NULL, pts, 1, flags,
777  VPX_DL_REALTIME)) {
778  die_codec(&codec, "Failed to encode frame");
779  }
780  vpx_usec_timer_mark(&timer);
781  cx_time += vpx_usec_timer_elapsed(&timer);
782  // Reset KF flag.
783  if (layering_mode != 7) {
784  layer_flags[0] &= ~VPX_EFLAG_FORCE_KF;
785  }
786  got_data = 0;
787  while ((pkt = vpx_codec_get_cx_data(&codec, &iter))) {
788  got_data = 1;
789  switch (pkt->kind) {
791  for (i = cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
792  i < cfg.ts_number_layers; ++i) {
793  vpx_video_writer_write_frame(outfile[i], pkt->data.frame.buf,
794  pkt->data.frame.sz, pts);
795  ++rc.layer_tot_enc_frames[i];
796  rc.layer_encoding_bitrate[i] += 8.0 * pkt->data.frame.sz;
797  // Keep count of rate control stats per layer (for non-key frames).
798  if (i == cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity] &&
799  !(pkt->data.frame.flags & VPX_FRAME_IS_KEY)) {
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]) /
803  rc.layer_pfb[i];
804  ++rc.layer_enc_frames[i];
805  }
806  }
807  // Update for short-time encoding bitrate states, for moving window
808  // of size rc->window, shifted by rc->window / 2.
809  // Ignore first window segment, due to key frame.
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);
818  sum_bitrate = 0.0;
819  }
820  }
821  // Second shifted window.
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);
831  sum_bitrate2 = 0.0;
832  }
833  }
834  break;
835  default: break;
836  }
837  }
838  ++frame_cnt;
839  pts += frame_duration;
840  }
841  fclose(infile);
842  printout_rate_control_summary(&rc, &cfg, frame_cnt);
843  printf("\n");
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);
847 
848  if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
849 
850  // Try to rewrite the output file headers with the actual frame count.
851  for (i = 0; i < cfg.ts_number_layers; ++i) vpx_video_writer_close(outfile[i]);
852 
853  vpx_img_free(&raw);
854  return EXIT_SUCCESS;
855 }
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&#39;t reference the last frame.
Definition: vp8cx.h:58
#define VP8_EFLAG_NO_UPD_GF
Don&#39;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&#39;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&#39;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&#39;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&#39;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