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() {
32  exit(EXIT_FAILURE);
33 }
34 
35 // Denoiser states, for temporal denoising.
36 enum denoiserState {
37  kDenoiserOff,
38  kDenoiserOnYOnly,
39  kDenoiserOnYUV,
40  kDenoiserOnYUVAggressive,
41  kDenoiserOnAdaptive
42 };
43 
44 static int mode_to_num_layers[12] = {1, 2, 2, 3, 3, 3, 3, 5, 2, 3, 3, 3};
45 
46 // For rate control encoding stats.
47 struct RateControlMetrics {
48  // Number of input frames per layer.
49  int layer_input_frames[VPX_TS_MAX_LAYERS];
50  // Total (cumulative) number of encoded frames per layer.
51  int layer_tot_enc_frames[VPX_TS_MAX_LAYERS];
52  // Number of encoded non-key frames per layer.
53  int layer_enc_frames[VPX_TS_MAX_LAYERS];
54  // Framerate per layer layer (cumulative).
55  double layer_framerate[VPX_TS_MAX_LAYERS];
56  // Target average frame size per layer (per-frame-bandwidth per layer).
57  double layer_pfb[VPX_TS_MAX_LAYERS];
58  // Actual average frame size per layer.
59  double layer_avg_frame_size[VPX_TS_MAX_LAYERS];
60  // Average rate mismatch per layer (|target - actual| / target).
61  double layer_avg_rate_mismatch[VPX_TS_MAX_LAYERS];
62  // Actual encoding bitrate per layer (cumulative).
63  double layer_encoding_bitrate[VPX_TS_MAX_LAYERS];
64  // Average of the short-time encoder actual bitrate.
65  // TODO(marpan): Should we add these short-time stats for each layer?
66  double avg_st_encoding_bitrate;
67  // Variance of the short-time encoder actual bitrate.
68  double variance_st_encoding_bitrate;
69  // Window (number of frames) for computing short-timee encoding bitrate.
70  int window_size;
71  // Number of window measurements.
72  int window_count;
73 };
74 
75 // Note: these rate control metrics assume only 1 key frame in the
76 // sequence (i.e., first frame only). So for temporal pattern# 7
77 // (which has key frame for every frame on base layer), the metrics
78 // computation will be off/wrong.
79 // TODO(marpan): Update these metrics to account for multiple key frames
80 // in the stream.
81 static void set_rate_control_metrics(struct RateControlMetrics *rc,
82  vpx_codec_enc_cfg_t *cfg) {
83  unsigned int i = 0;
84  // Set the layer (cumulative) framerate and the target layer (non-cumulative)
85  // per-frame-bandwidth, for the rate control encoding stats below.
86  const double framerate = cfg->g_timebase.den / cfg->g_timebase.num;
87  rc->layer_framerate[0] = framerate / cfg->ts_rate_decimator[0];
88  rc->layer_pfb[0] = 1000.0 * cfg->ts_target_bitrate[0] /
89  rc->layer_framerate[0];
90  for (i = 0; i < cfg->ts_number_layers; ++i) {
91  if (i > 0) {
92  rc->layer_framerate[i] = framerate / cfg->ts_rate_decimator[i];
93  rc->layer_pfb[i] = 1000.0 *
94  (cfg->ts_target_bitrate[i] - cfg->ts_target_bitrate[i - 1]) /
95  (rc->layer_framerate[i] - rc->layer_framerate[i - 1]);
96  }
97  rc->layer_input_frames[i] = 0;
98  rc->layer_enc_frames[i] = 0;
99  rc->layer_tot_enc_frames[i] = 0;
100  rc->layer_encoding_bitrate[i] = 0.0;
101  rc->layer_avg_frame_size[i] = 0.0;
102  rc->layer_avg_rate_mismatch[i] = 0.0;
103  }
104  rc->window_count = 0;
105  rc->window_size = 15;
106  rc->avg_st_encoding_bitrate = 0.0;
107  rc->variance_st_encoding_bitrate = 0.0;
108 }
109 
110 static void printout_rate_control_summary(struct RateControlMetrics *rc,
111  vpx_codec_enc_cfg_t *cfg,
112  int frame_cnt) {
113  unsigned int i = 0;
114  int tot_num_frames = 0;
115  double perc_fluctuation = 0.0;
116  printf("Total number of processed frames: %d\n\n", frame_cnt -1);
117  printf("Rate control layer stats for %d layer(s):\n\n",
118  cfg->ts_number_layers);
119  for (i = 0; i < cfg->ts_number_layers; ++i) {
120  const int num_dropped = (i > 0) ?
121  (rc->layer_input_frames[i] - rc->layer_enc_frames[i]) :
122  (rc->layer_input_frames[i] - rc->layer_enc_frames[i] - 1);
123  tot_num_frames += rc->layer_input_frames[i];
124  rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[i] *
125  rc->layer_encoding_bitrate[i] / tot_num_frames;
126  rc->layer_avg_frame_size[i] = rc->layer_avg_frame_size[i] /
127  rc->layer_enc_frames[i];
128  rc->layer_avg_rate_mismatch[i] = 100.0 * rc->layer_avg_rate_mismatch[i] /
129  rc->layer_enc_frames[i];
130  printf("For layer#: %d \n", i);
131  printf("Bitrate (target vs actual): %d %f \n", cfg->ts_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("Number of input frames, encoded (non-key) frames, "
137  "and perc dropped frames: %d %d %f \n", rc->layer_input_frames[i],
138  rc->layer_enc_frames[i],
139  100.0 * num_dropped / rc->layer_input_frames[i]);
140  printf("\n");
141  }
142  rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
143  rc->variance_st_encoding_bitrate =
144  rc->variance_st_encoding_bitrate / rc->window_count -
145  (rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
146  perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
147  rc->avg_st_encoding_bitrate;
148  printf("Short-time stats, for window of %d frames: \n",rc->window_size);
149  printf("Average, rms-variance, and percent-fluct: %f %f %f \n",
150  rc->avg_st_encoding_bitrate,
151  sqrt(rc->variance_st_encoding_bitrate),
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] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF |
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 |
193  layer_flags[1] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
195 #else
196  // 0=L, 1=GF, Intra-layer prediction disabled.
197  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF |
199  layer_flags[1] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
201 #endif
202  break;
203  }
204  case 2: {
205  // 2-layers, 3-frame period.
206  int ids[3] = {0, 1, 1};
207  cfg->ts_periodicity = 3;
208  *flag_periodicity = 3;
209  cfg->ts_number_layers = 2;
210  cfg->ts_rate_decimator[0] = 3;
211  cfg->ts_rate_decimator[1] = 1;
212  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
213  // 0=L, 1=GF, Intra-layer prediction enabled.
214  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
216  layer_flags[1] =
217  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
219  break;
220  }
221  case 3: {
222  // 3-layers, 6-frame period.
223  int ids[6] = {0, 2, 2, 1, 2, 2};
224  cfg->ts_periodicity = 6;
225  *flag_periodicity = 6;
226  cfg->ts_number_layers = 3;
227  cfg->ts_rate_decimator[0] = 6;
228  cfg->ts_rate_decimator[1] = 3;
229  cfg->ts_rate_decimator[2] = 1;
230  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
231  // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
232  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
234  layer_flags[3] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_ARF |
236  layer_flags[1] =
237  layer_flags[2] =
238  layer_flags[4] =
239  layer_flags[5] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_LAST;
240  break;
241  }
242  case 4: {
243  // 3-layers, 4-frame period.
244  int ids[4] = {0, 2, 1, 2};
245  cfg->ts_periodicity = 4;
246  *flag_periodicity = 4;
247  cfg->ts_number_layers = 3;
248  cfg->ts_rate_decimator[0] = 4;
249  cfg->ts_rate_decimator[1] = 2;
250  cfg->ts_rate_decimator[2] = 1;
251  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
252  // 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
253  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
255  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
257  layer_flags[1] =
258  layer_flags[3] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
260  break;
261  }
262  case 5: {
263  // 3-layers, 4-frame period.
264  int ids[4] = {0, 2, 1, 2};
265  cfg->ts_periodicity = 4;
266  *flag_periodicity = 4;
267  cfg->ts_number_layers = 3;
268  cfg->ts_rate_decimator[0] = 4;
269  cfg->ts_rate_decimator[1] = 2;
270  cfg->ts_rate_decimator[2] = 1;
271  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
272  // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled in layer 1, disabled
273  // in layer 2.
274  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
276  layer_flags[2] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
278  layer_flags[1] =
279  layer_flags[3] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
281  break;
282  }
283  case 6: {
284  // 3-layers, 4-frame period.
285  int ids[4] = {0, 2, 1, 2};
286  cfg->ts_periodicity = 4;
287  *flag_periodicity = 4;
288  cfg->ts_number_layers = 3;
289  cfg->ts_rate_decimator[0] = 4;
290  cfg->ts_rate_decimator[1] = 2;
291  cfg->ts_rate_decimator[2] = 1;
292  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
293  // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
294  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
296  layer_flags[2] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
298  layer_flags[1] =
299  layer_flags[3] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
300  break;
301  }
302  case 7: {
303  // NOTE: Probably of academic interest only.
304  // 5-layers, 16-frame period.
305  int ids[16] = {0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4};
306  cfg->ts_periodicity = 16;
307  *flag_periodicity = 16;
308  cfg->ts_number_layers = 5;
309  cfg->ts_rate_decimator[0] = 16;
310  cfg->ts_rate_decimator[1] = 8;
311  cfg->ts_rate_decimator[2] = 4;
312  cfg->ts_rate_decimator[3] = 2;
313  cfg->ts_rate_decimator[4] = 1;
314  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
315  layer_flags[0] = VPX_EFLAG_FORCE_KF;
316  layer_flags[1] =
317  layer_flags[3] =
318  layer_flags[5] =
319  layer_flags[7] =
320  layer_flags[9] =
321  layer_flags[11] =
322  layer_flags[13] =
323  layer_flags[15] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
325  layer_flags[2] =
326  layer_flags[6] =
327  layer_flags[10] =
328  layer_flags[14] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF;
329  layer_flags[4] =
330  layer_flags[12] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_UPD_ARF;
331  layer_flags[8] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF;
332  break;
333  }
334  case 8: {
335  // 2-layers, with sync point at first frame of layer 1.
336  int ids[2] = {0, 1};
337  cfg->ts_periodicity = 2;
338  *flag_periodicity = 8;
339  cfg->ts_number_layers = 2;
340  cfg->ts_rate_decimator[0] = 2;
341  cfg->ts_rate_decimator[1] = 1;
342  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
343  // 0=L, 1=GF.
344  // ARF is used as predictor for all frames, and is only updated on
345  // key frame. Sync point every 8 frames.
346 
347  // Layer 0: predict from L and ARF, update L and G.
348  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
350  // Layer 1: sync point: predict from L and ARF, and update G.
351  layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_LAST |
353  // Layer 0, predict from L and ARF, update L.
354  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF |
356  // Layer 1: predict from L, G and ARF, and update G.
357  layer_flags[3] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
359  // Layer 0.
360  layer_flags[4] = layer_flags[2];
361  // Layer 1.
362  layer_flags[5] = layer_flags[3];
363  // Layer 0.
364  layer_flags[6] = layer_flags[4];
365  // Layer 1.
366  layer_flags[7] = layer_flags[5];
367  break;
368  }
369  case 9: {
370  // 3-layers: Sync points for layer 1 and 2 every 8 frames.
371  int ids[4] = {0, 2, 1, 2};
372  cfg->ts_periodicity = 4;
373  *flag_periodicity = 8;
374  cfg->ts_number_layers = 3;
375  cfg->ts_rate_decimator[0] = 4;
376  cfg->ts_rate_decimator[1] = 2;
377  cfg->ts_rate_decimator[2] = 1;
378  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
379  // 0=L, 1=GF, 2=ARF.
380  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
382  layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
384  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
386  layer_flags[3] =
387  layer_flags[5] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
388  layer_flags[4] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
390  layer_flags[6] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
392  layer_flags[7] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
394  break;
395  }
396  case 10: {
397  // 3-layers structure where ARF is used as predictor for all frames,
398  // and is only updated on key frame.
399  // Sync points for layer 1 and 2 every 8 frames.
400 
401  int ids[4] = {0, 2, 1, 2};
402  cfg->ts_periodicity = 4;
403  *flag_periodicity = 8;
404  cfg->ts_number_layers = 3;
405  cfg->ts_rate_decimator[0] = 4;
406  cfg->ts_rate_decimator[1] = 2;
407  cfg->ts_rate_decimator[2] = 1;
408  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
409  // 0=L, 1=GF, 2=ARF.
410  // Layer 0: predict from L and ARF; update L and G.
411  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_ARF |
413  // Layer 2: sync point: predict from L and ARF; update none.
414  layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF |
417  // Layer 1: sync point: predict from L and ARF; update G.
418  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_ARF |
420  // Layer 2: predict from L, G, ARF; update none.
421  layer_flags[3] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
423  // Layer 0: predict from L and ARF; update L.
424  layer_flags[4] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
426  // Layer 2: predict from L, G, ARF; update none.
427  layer_flags[5] = layer_flags[3];
428  // Layer 1: predict from L, G, ARF; update G.
429  layer_flags[6] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
430  // Layer 2: predict from L, G, ARF; update none.
431  layer_flags[7] = layer_flags[3];
432  break;
433  }
434  case 11:
435  default: {
436  // 3-layers structure as in case 10, but no sync/refresh points for
437  // layer 1 and 2.
438  int ids[4] = {0, 2, 1, 2};
439  cfg->ts_periodicity = 4;
440  *flag_periodicity = 8;
441  cfg->ts_number_layers = 3;
442  cfg->ts_rate_decimator[0] = 4;
443  cfg->ts_rate_decimator[1] = 2;
444  cfg->ts_rate_decimator[2] = 1;
445  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
446  // 0=L, 1=GF, 2=ARF.
447  // Layer 0: predict from L and ARF; update L.
448  layer_flags[0] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
450  layer_flags[4] = layer_flags[0];
451  // Layer 1: predict from L, G, ARF; update G.
452  layer_flags[2] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
453  layer_flags[6] = layer_flags[2];
454  // Layer 2: predict from L, G, ARF; update none.
455  layer_flags[1] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
457  layer_flags[3] = layer_flags[1];
458  layer_flags[5] = layer_flags[1];
459  layer_flags[7] = layer_flags[1];
460  break;
461  }
462  }
463 }
464 
465 int main(int argc, char **argv) {
466  VpxVideoWriter *outfile[VPX_TS_MAX_LAYERS] = {NULL};
467  vpx_codec_ctx_t codec;
469  int frame_cnt = 0;
470  vpx_image_t raw;
471  vpx_codec_err_t res;
472  unsigned int width;
473  unsigned int height;
474  int speed;
475  int frame_avail;
476  int got_data;
477  int flags = 0;
478  unsigned int i;
479  int pts = 0; // PTS starts at 0.
480  int frame_duration = 1; // 1 timebase tick per frame.
481  int layering_mode = 0;
482  int layer_flags[VPX_TS_MAX_PERIODICITY] = {0};
483  int flag_periodicity = 1;
484 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION)
485  vpx_svc_layer_id_t layer_id = {0, 0};
486 #else
487  vpx_svc_layer_id_t layer_id = {0};
488 #endif
489  const VpxInterface *encoder = NULL;
490  FILE *infile = NULL;
491  struct RateControlMetrics rc;
492  int64_t cx_time = 0;
493  const int min_args_base = 11;
494 #if CONFIG_VP9_HIGHBITDEPTH
495  vpx_bit_depth_t bit_depth = VPX_BITS_8;
496  int input_bit_depth = 8;
497  const int min_args = min_args_base + 1;
498 #else
499  const int min_args = min_args_base;
500 #endif // CONFIG_VP9_HIGHBITDEPTH
501  double sum_bitrate = 0.0;
502  double sum_bitrate2 = 0.0;
503  double framerate = 30.0;
504 
505  exec_name = argv[0];
506  // Check usage and arguments.
507  if (argc < min_args) {
508 #if CONFIG_VP9_HIGHBITDEPTH
509  die("Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> "
510  "<rate_num> <rate_den> <speed> <frame_drop_threshold> <mode> "
511  "<Rate_0> ... <Rate_nlayers-1> <bit-depth> \n", argv[0]);
512 #else
513  die("Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> "
514  "<rate_num> <rate_den> <speed> <frame_drop_threshold> <mode> "
515  "<Rate_0> ... <Rate_nlayers-1> \n", argv[0]);
516 #endif // CONFIG_VP9_HIGHBITDEPTH
517  }
518 
519  encoder = get_vpx_encoder_by_name(argv[3]);
520  if (!encoder)
521  die("Unsupported codec.");
522 
523  printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
524 
525  width = strtol(argv[4], NULL, 0);
526  height = strtol(argv[5], NULL, 0);
527  if (width < 16 || width % 2 || height < 16 || height % 2) {
528  die("Invalid resolution: %d x %d", width, height);
529  }
530 
531  layering_mode = strtol(argv[10], NULL, 0);
532  if (layering_mode < 0 || layering_mode > 12) {
533  die("Invalid layering mode (0..12) %s", argv[10]);
534  }
535 
536  if (argc != min_args + mode_to_num_layers[layering_mode]) {
537  die("Invalid number of arguments");
538  }
539 
540 #if CONFIG_VP9_HIGHBITDEPTH
541  switch (strtol(argv[argc-1], NULL, 0)) {
542  case 8:
543  bit_depth = VPX_BITS_8;
544  input_bit_depth = 8;
545  break;
546  case 10:
547  bit_depth = VPX_BITS_10;
548  input_bit_depth = 10;
549  break;
550  case 12:
551  bit_depth = VPX_BITS_12;
552  input_bit_depth = 12;
553  break;
554  default:
555  die("Invalid bit depth (8, 10, 12) %s", argv[argc-1]);
556  }
557  if (!vpx_img_alloc(&raw,
558  bit_depth == VPX_BITS_8 ? VPX_IMG_FMT_I420 :
559  VPX_IMG_FMT_I42016,
560  width, height, 32)) {
561  die("Failed to allocate image", width, height);
562  }
563 #else
564  if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 32)) {
565  die("Failed to allocate image", width, height);
566  }
567 #endif // CONFIG_VP9_HIGHBITDEPTH
568 
569  // Populate encoder configuration.
570  res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
571  if (res) {
572  printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
573  return EXIT_FAILURE;
574  }
575 
576  // Update the default configuration with our settings.
577  cfg.g_w = width;
578  cfg.g_h = height;
579 
580 #if CONFIG_VP9_HIGHBITDEPTH
581  if (bit_depth != VPX_BITS_8) {
582  cfg.g_bit_depth = bit_depth;
583  cfg.g_input_bit_depth = input_bit_depth;
584  cfg.g_profile = 2;
585  }
586 #endif // CONFIG_VP9_HIGHBITDEPTH
587 
588  // Timebase format e.g. 30fps: numerator=1, demoninator = 30.
589  cfg.g_timebase.num = strtol(argv[6], NULL, 0);
590  cfg.g_timebase.den = strtol(argv[7], NULL, 0);
591 
592  speed = strtol(argv[8], NULL, 0);
593  if (speed < 0) {
594  die("Invalid speed setting: must be positive");
595  }
596 
597  for (i = min_args_base;
598  (int)i < min_args_base + mode_to_num_layers[layering_mode];
599  ++i) {
600  cfg.ts_target_bitrate[i - 11] = strtol(argv[i], NULL, 0);
601  }
602 
603  // Real time parameters.
604  cfg.rc_dropframe_thresh = strtol(argv[9], NULL, 0);
605  cfg.rc_end_usage = VPX_CBR;
606  cfg.rc_resize_allowed = 0;
607  cfg.rc_min_quantizer = 2;
608  cfg.rc_max_quantizer = 56;
609  if (strncmp(encoder->name, "vp9", 3) == 0)
610  cfg.rc_max_quantizer = 52;
611  cfg.rc_undershoot_pct = 50;
612  cfg.rc_overshoot_pct = 50;
613  cfg.rc_buf_initial_sz = 500;
614  cfg.rc_buf_optimal_sz = 600;
615  cfg.rc_buf_sz = 1000;
616 
617  // Use 1 thread as default.
618  cfg.g_threads = 1;
619 
620  // Enable error resilient mode.
621  cfg.g_error_resilient = 1;
622  cfg.g_lag_in_frames = 0;
623  cfg.kf_mode = VPX_KF_AUTO;
624 
625  // Disable automatic keyframe placement.
626  cfg.kf_min_dist = cfg.kf_max_dist = 3000;
627 
628  set_temporal_layer_pattern(layering_mode,
629  &cfg,
630  layer_flags,
631  &flag_periodicity);
632 
633  set_rate_control_metrics(&rc, &cfg);
634 
635  // Target bandwidth for the whole stream.
636  // Set to ts_target_bitrate for highest layer (total bitrate).
638 
639  // Open input file.
640  if (!(infile = fopen(argv[1], "rb"))) {
641  die("Failed to open %s for reading", argv[1]);
642  }
643 
644  framerate = cfg.g_timebase.den / cfg.g_timebase.num;
645  // Open an output file for each stream.
646  for (i = 0; i < cfg.ts_number_layers; ++i) {
647  char file_name[PATH_MAX];
648  VpxVideoInfo info;
649  info.codec_fourcc = encoder->fourcc;
650  info.frame_width = cfg.g_w;
651  info.frame_height = cfg.g_h;
652  info.time_base.numerator = cfg.g_timebase.num;
653  info.time_base.denominator = cfg.g_timebase.den;
654 
655  snprintf(file_name, sizeof(file_name), "%s_%d.ivf", argv[2], i);
656  outfile[i] = vpx_video_writer_open(file_name, kContainerIVF, &info);
657  if (!outfile[i])
658  die("Failed to open %s for writing", file_name);
659 
660  assert(outfile[i] != NULL);
661  }
662  // No spatial layers in this encoder.
663  cfg.ss_number_layers = 1;
664 
665  // Initialize codec.
666 #if CONFIG_VP9_HIGHBITDEPTH
667  if (vpx_codec_enc_init(
668  &codec, encoder->codec_interface(), &cfg,
669  bit_depth == VPX_BITS_8 ? 0 : VPX_CODEC_USE_HIGHBITDEPTH))
670 #else
671  if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
672 #endif // CONFIG_VP9_HIGHBITDEPTH
673  die_codec(&codec, "Failed to initialize encoder");
674 
675  if (strncmp(encoder->name, "vp8", 3) == 0) {
676  vpx_codec_control(&codec, VP8E_SET_CPUUSED, -speed);
677  vpx_codec_control(&codec, VP8E_SET_NOISE_SENSITIVITY, kDenoiserOnYOnly);
679  } else if (strncmp(encoder->name, "vp9", 3) == 0) {
680  vpx_codec_control(&codec, VP8E_SET_CPUUSED, speed);
686  if (vpx_codec_control(&codec, VP9E_SET_SVC, layering_mode > 0 ? 1: 0)) {
687  die_codec(&codec, "Failed to set SVC");
688  }
689  }
690  if (strncmp(encoder->name, "vp8", 3) == 0) {
692  }
694  // This controls the maximum target size of the key frame.
695  // For generating smaller key frames, use a smaller max_intra_size_pct
696  // value, like 100 or 200.
697  {
698  const int max_intra_size_pct = 900;
700  max_intra_size_pct);
701  }
702 
703  frame_avail = 1;
704  while (frame_avail || got_data) {
705  struct vpx_usec_timer timer;
706  vpx_codec_iter_t iter = NULL;
707  const vpx_codec_cx_pkt_t *pkt;
708 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION)
709  // Update the temporal layer_id. No spatial layers in this test.
710  layer_id.spatial_layer_id = 0;
711 #endif
712  layer_id.temporal_layer_id =
713  cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
714  if (strncmp(encoder->name, "vp9", 3) == 0) {
715  vpx_codec_control(&codec, VP9E_SET_SVC_LAYER_ID, &layer_id);
716  } else if (strncmp(encoder->name, "vp8", 3) == 0) {
718  layer_id.temporal_layer_id);
719  }
720  flags = layer_flags[frame_cnt % flag_periodicity];
721  if (layering_mode == 0)
722  flags = 0;
723  frame_avail = vpx_img_read(&raw, infile);
724  if (frame_avail)
725  ++rc.layer_input_frames[layer_id.temporal_layer_id];
726  vpx_usec_timer_start(&timer);
727  if (vpx_codec_encode(&codec, frame_avail? &raw : NULL, pts, 1, flags,
728  VPX_DL_REALTIME)) {
729  die_codec(&codec, "Failed to encode frame");
730  }
731  vpx_usec_timer_mark(&timer);
732  cx_time += vpx_usec_timer_elapsed(&timer);
733  // Reset KF flag.
734  if (layering_mode != 7) {
735  layer_flags[0] &= ~VPX_EFLAG_FORCE_KF;
736  }
737  got_data = 0;
738  while ( (pkt = vpx_codec_get_cx_data(&codec, &iter)) ) {
739  got_data = 1;
740  switch (pkt->kind) {
742  for (i = cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
743  i < cfg.ts_number_layers; ++i) {
744  vpx_video_writer_write_frame(outfile[i], pkt->data.frame.buf,
745  pkt->data.frame.sz, pts);
746  ++rc.layer_tot_enc_frames[i];
747  rc.layer_encoding_bitrate[i] += 8.0 * pkt->data.frame.sz;
748  // Keep count of rate control stats per layer (for non-key frames).
749  if (i == cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity] &&
750  !(pkt->data.frame.flags & VPX_FRAME_IS_KEY)) {
751  rc.layer_avg_frame_size[i] += 8.0 * pkt->data.frame.sz;
752  rc.layer_avg_rate_mismatch[i] +=
753  fabs(8.0 * pkt->data.frame.sz - rc.layer_pfb[i]) /
754  rc.layer_pfb[i];
755  ++rc.layer_enc_frames[i];
756  }
757  }
758  // Update for short-time encoding bitrate states, for moving window
759  // of size rc->window, shifted by rc->window / 2.
760  // Ignore first window segment, due to key frame.
761  if (frame_cnt > rc.window_size) {
762  sum_bitrate += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
763  if (frame_cnt % rc.window_size == 0) {
764  rc.window_count += 1;
765  rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
766  rc.variance_st_encoding_bitrate +=
767  (sum_bitrate / rc.window_size) *
768  (sum_bitrate / rc.window_size);
769  sum_bitrate = 0.0;
770  }
771  }
772  // Second shifted window.
773  if (frame_cnt > rc.window_size + rc.window_size / 2) {
774  sum_bitrate2 += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
775  if (frame_cnt > 2 * rc.window_size &&
776  frame_cnt % rc.window_size == 0) {
777  rc.window_count += 1;
778  rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
779  rc.variance_st_encoding_bitrate +=
780  (sum_bitrate2 / rc.window_size) *
781  (sum_bitrate2 / rc.window_size);
782  sum_bitrate2 = 0.0;
783  }
784  }
785  break;
786  default:
787  break;
788  }
789  }
790  ++frame_cnt;
791  pts += frame_duration;
792  }
793  fclose(infile);
794  printout_rate_control_summary(&rc, &cfg, frame_cnt);
795  printf("\n");
796  printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
797  frame_cnt,
798  1000 * (float)cx_time / (double)(frame_cnt * 1000000),
799  1000000 * (double)frame_cnt / (double)cx_time);
800 
801  if (vpx_codec_destroy(&codec))
802  die_codec(&codec, "Failed to destroy codec");
803 
804  // Try to rewrite the output file headers with the actual frame count.
805  for (i = 0; i < cfg.ts_number_layers; ++i)
806  vpx_video_writer_close(outfile[i]);
807 
808  vpx_img_free(&raw);
809  return EXIT_SUCCESS;
810 }
unsigned int rc_buf_initial_sz
Decoder Buffer Initial Size.
Definition: vpx_encoder.h:607
unsigned int ts_number_layers
Number of temporal coding layers.
Definition: vpx_encoder.h:712
Codec control function to set encoder internal speed settings.
Definition: vp8cx.h:183
#define VP8_EFLAG_NO_REF_LAST
Don&#39;t reference the last frame.
Definition: vp8cx.h:59
#define VP8_EFLAG_NO_UPD_GF
Don&#39;t update the golden frame.
Definition: vp8cx.h:93
Image Descriptor.
Definition: vpx_image.h:82
Describes the encoder algorithm interface to applications.
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
const char * vpx_codec_err_to_string(vpx_codec_err_t err)
Convert error number to printable string.
#define VPX_TS_MAX_LAYERS
Definition: vpx_encoder.h:40
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:394
Definition: vpx_encoder.h:273
Codec control function to set noise sensitivity.
Definition: vp8cx.h:440
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition: vpx_encoder.h:597
#define VP8_EFLAG_NO_REF_GF
Don&#39;t reference the golden frame.
Definition: vp8cx.h:68
unsigned int g_input_bit_depth
Bit-depth of the input frames.
Definition: vpx_encoder.h:380
enum vpx_kf_mode kf_mode
Keyframe placement mode.
Definition: vpx_encoder.h:662
int den
Definition: vpx_encoder.h:258
vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx, const vpx_image_t *img, vpx_codec_pts_t pts, unsigned long duration, vpx_enc_frame_flags_t flags, unsigned long deadline)
Encode a frame.
unsigned int rc_max_quantizer
Maximum (Worst Quality) Quantizer.
Definition: vpx_encoder.h:549
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition: vpx_encoder.h:538
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: vpx_encoder.h:682
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: vpx_encoder.h:426
Encoder configuration structure.
Definition: vpx_encoder.h:311
Definition: vpx_encoder.h:289
Codec control function to set Max data rate for Intra frames.
Definition: vp8cx.h:279
#define VPX_CODEC_USE_HIGHBITDEPTH
Definition: vpx_encoder.h:98
Encoder output packet.
Definition: vpx_encoder.h:192
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition: vpx_encoder.h:580
unsigned int ts_rate_decimator[5]
Frame rate decimation factor for each temporal layer.
Definition: vpx_encoder.h:726
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition: vpx_encoder.h:617
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: vpx_encoder.h:672
unsigned int g_profile
Bitstream profile to use.
Definition: vpx_encoder.h:343
Codec control function to set number of tile columns.
Definition: vp8cx.h:370
unsigned int ts_layer_id[16]
Template defining the membership of frames to temporal layers.
Definition: vpx_encoder.h:744
struct vpx_codec_cx_pkt::@1::@2 frame
vpx_image_t * vpx_img_alloc(vpx_image_t *img, vpx_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
Definition: vpx_image.h:56
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:354
unsigned int ts_target_bitrate[5]
Target bitrate for each temporal layer.
Definition: vpx_encoder.h:719
enum vpx_bit_depth vpx_bit_depth_t
Bit depth for codecThis enumeration determines the bit depth of the codec.
unsigned int rc_undershoot_pct
Rate control adaptation undershoot control.
Definition: vpx_encoder.h:567
Codec control function to set adaptive quantization mode.
Definition: vp8cx.h:417
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:364
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:193
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: vpx_encoder.h:449
vp9 svc layer parameters
Definition: vp8cx.h:619
Codec control function to set the temporal layer id.
Definition: vp8cx.h:326
#define VP8_EFLAG_NO_UPD_LAST
Don&#39;t update the last frame.
Definition: vp8cx.h:85
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.
Codec control function to set the number of token partitions.
Definition: vp8cx.h:216
unsigned int rc_target_bitrate
Target data rate.
Definition: vpx_encoder.h:522
#define VPX_DL_REALTIME
Definition: vpx_encoder.h:891
int num
Definition: vpx_encoder.h:257
control function to set noise sensitivity
Definition: vp8cx.h:198
Definition: vpx_codec.h:222
unsigned int g_threads
Maximum number of threads to use.
Definition: vpx_encoder.h:332
unsigned int ss_number_layers
Number of spatial coding layers.
Definition: vpx_encoder.h:692
vpx_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition: vpx_encoder.h:372
Provides definitions for using VP8 or VP9 encoder algorithm within the vpx Codec Interface.
#define vpx_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_enc_init_ver()
Definition: vpx_encoder.h:793
Codec control function to set encoder screen content mode.
Definition: vp8cx.h:332
unsigned int rc_resize_allowed
Enable/disable spatial resampling, if supported by the codec.
Definition: vpx_encoder.h:459
vpx_codec_err_t
Algorithm return codes.
Definition: vpx_codec.h:89
const vpx_codec_cx_pkt_t * vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Encoded data iterator.
union vpx_codec_cx_pkt::@1 data
int temporal_layer_id
Definition: vp8cx.h:620
Codec control function to enable/disable periodic Q boost.
Definition: vp8cx.h:432
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, unsigned int reserved)
Get a default configuration.
#define VPX_TS_MAX_PERIODICITY
Definition: vpx_encoder.h:37
Codec control function to turn on/off SVC in encoder.
Definition: vp8cx.h:449
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition: vpx_codec.h:407
unsigned int ts_periodicity
Length of the sequence defining frame temporal layer membership.
Definition: vpx_encoder.h:735
#define VP8_EFLAG_NO_REF_ARF
Don&#39;t reference the alternate reference frame.
Definition: vp8cx.h:77
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
Definition: vpx_codec.h:220
Codec control function to set the threshold for MBs treated static.
Definition: vp8cx.h:210
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:127
Definition: vpx_codec.h:221
#define VPX_EFLAG_FORCE_KF
Definition: vpx_encoder.h:302
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:188
Definition: vpx_encoder.h:173
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: vpx_encoder.h:403
#define VP8_EFLAG_NO_UPD_ARF
Don&#39;t update the alternate reference frame.
Definition: vp8cx.h:101
#define VP8_EFLAG_NO_UPD_ENTROPY
Disable entropy update.
Definition: vp8cx.h:125
Codec control function to set svc layer for spatial and temporal.
Definition: vp8cx.h:468
enum vpx_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: vpx_encoder.h:501
Codec context structure.
Definition: vpx_codec.h:199