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) {
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[13] = {1, 2, 2, 3, 3, 3, 3, 5, 2, 3, 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  int layer_target_bitrate[VPX_MAX_LAYERS];
74 };
75 
76 // Note: these rate control metrics assume only 1 key frame in the
77 // sequence (i.e., first frame only). So for temporal pattern# 7
78 // (which has key frame for every frame on base layer), the metrics
79 // computation will be off/wrong.
80 // TODO(marpan): Update these metrics to account for multiple key frames
81 // in the stream.
82 static void set_rate_control_metrics(struct RateControlMetrics *rc,
83  vpx_codec_enc_cfg_t *cfg) {
84  unsigned int i = 0;
85  // Set the layer (cumulative) framerate and the target layer (non-cumulative)
86  // per-frame-bandwidth, for the rate control encoding stats below.
87  const double framerate = cfg->g_timebase.den / cfg->g_timebase.num;
88  rc->layer_framerate[0] = framerate / cfg->ts_rate_decimator[0];
89  rc->layer_pfb[0] = 1000.0 * rc->layer_target_bitrate[0] /
90  rc->layer_framerate[0];
91  for (i = 0; i < cfg->ts_number_layers; ++i) {
92  if (i > 0) {
93  rc->layer_framerate[i] = framerate / cfg->ts_rate_decimator[i];
94  rc->layer_pfb[i] = 1000.0 *
95  (rc->layer_target_bitrate[i] - rc->layer_target_bitrate[i - 1]) /
96  (rc->layer_framerate[i] - rc->layer_framerate[i - 1]);
97  }
98  rc->layer_input_frames[i] = 0;
99  rc->layer_enc_frames[i] = 0;
100  rc->layer_tot_enc_frames[i] = 0;
101  rc->layer_encoding_bitrate[i] = 0.0;
102  rc->layer_avg_frame_size[i] = 0.0;
103  rc->layer_avg_rate_mismatch[i] = 0.0;
104  }
105  rc->window_count = 0;
106  rc->window_size = 15;
107  rc->avg_st_encoding_bitrate = 0.0;
108  rc->variance_st_encoding_bitrate = 0.0;
109 }
110 
111 static void printout_rate_control_summary(struct RateControlMetrics *rc,
112  vpx_codec_enc_cfg_t *cfg,
113  int frame_cnt) {
114  unsigned int i = 0;
115  int tot_num_frames = 0;
116  double perc_fluctuation = 0.0;
117  printf("Total number of processed frames: %d\n\n", frame_cnt -1);
118  printf("Rate control layer stats for %d layer(s):\n\n",
119  cfg->ts_number_layers);
120  for (i = 0; i < cfg->ts_number_layers; ++i) {
121  const int num_dropped = (i > 0) ?
122  (rc->layer_input_frames[i] - rc->layer_enc_frames[i]) :
123  (rc->layer_input_frames[i] - rc->layer_enc_frames[i] - 1);
124  tot_num_frames += rc->layer_input_frames[i];
125  rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[i] *
126  rc->layer_encoding_bitrate[i] / tot_num_frames;
127  rc->layer_avg_frame_size[i] = rc->layer_avg_frame_size[i] /
128  rc->layer_enc_frames[i];
129  rc->layer_avg_rate_mismatch[i] = 100.0 * rc->layer_avg_rate_mismatch[i] /
130  rc->layer_enc_frames[i];
131  printf("For layer#: %d \n", i);
132  printf("Bitrate (target vs actual): %d %f \n", rc->layer_target_bitrate[i],
133  rc->layer_encoding_bitrate[i]);
134  printf("Average frame size (target vs actual): %f %f \n", rc->layer_pfb[i],
135  rc->layer_avg_frame_size[i]);
136  printf("Average rate_mismatch: %f \n", rc->layer_avg_rate_mismatch[i]);
137  printf("Number of input frames, encoded (non-key) frames, "
138  "and perc dropped frames: %d %d %f \n", rc->layer_input_frames[i],
139  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,
152  sqrt(rc->variance_st_encoding_bitrate),
153  perc_fluctuation);
154  if ((frame_cnt - 1) != tot_num_frames)
155  die("Error: Number of input frames not equal to output! \n");
156 }
157 
158 // Temporal scaling parameters:
159 // NOTE: The 3 prediction frames cannot be used interchangeably due to
160 // differences in the way they are handled throughout the code. The
161 // frames should be allocated to layers in the order LAST, GF, ARF.
162 // Other combinations work, but may produce slightly inferior results.
163 static void set_temporal_layer_pattern(int layering_mode,
164  vpx_codec_enc_cfg_t *cfg,
165  int *layer_flags,
166  int *flag_periodicity) {
167  switch (layering_mode) {
168  case 0: {
169  // 1-layer.
170  int ids[1] = {0};
171  cfg->ts_periodicity = 1;
172  *flag_periodicity = 1;
173  cfg->ts_number_layers = 1;
174  cfg->ts_rate_decimator[0] = 1;
175  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
176  // Update L only.
177  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF |
179  break;
180  }
181  case 1: {
182  // 2-layers, 2-frame period.
183  int ids[2] = {0, 1};
184  cfg->ts_periodicity = 2;
185  *flag_periodicity = 2;
186  cfg->ts_number_layers = 2;
187  cfg->ts_rate_decimator[0] = 2;
188  cfg->ts_rate_decimator[1] = 1;
189  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
190 #if 1
191  // 0=L, 1=GF, Intra-layer prediction enabled.
192  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF |
194  layer_flags[1] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
196 #else
197  // 0=L, 1=GF, Intra-layer prediction disabled.
198  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF |
200  layer_flags[1] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
202 #endif
203  break;
204  }
205  case 2: {
206  // 2-layers, 3-frame period.
207  int ids[3] = {0, 1, 1};
208  cfg->ts_periodicity = 3;
209  *flag_periodicity = 3;
210  cfg->ts_number_layers = 2;
211  cfg->ts_rate_decimator[0] = 3;
212  cfg->ts_rate_decimator[1] = 1;
213  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
214  // 0=L, 1=GF, Intra-layer prediction enabled.
215  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
217  layer_flags[1] =
218  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
220  break;
221  }
222  case 3: {
223  // 3-layers, 6-frame period.
224  int ids[6] = {0, 2, 2, 1, 2, 2};
225  cfg->ts_periodicity = 6;
226  *flag_periodicity = 6;
227  cfg->ts_number_layers = 3;
228  cfg->ts_rate_decimator[0] = 6;
229  cfg->ts_rate_decimator[1] = 3;
230  cfg->ts_rate_decimator[2] = 1;
231  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
232  // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
233  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
235  layer_flags[3] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_ARF |
237  layer_flags[1] =
238  layer_flags[2] =
239  layer_flags[4] =
240  layer_flags[5] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_LAST;
241  break;
242  }
243  case 4: {
244  // 3-layers, 4-frame period.
245  int ids[4] = {0, 2, 1, 2};
246  cfg->ts_periodicity = 4;
247  *flag_periodicity = 4;
248  cfg->ts_number_layers = 3;
249  cfg->ts_rate_decimator[0] = 4;
250  cfg->ts_rate_decimator[1] = 2;
251  cfg->ts_rate_decimator[2] = 1;
252  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
253  // 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
254  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
256  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
258  layer_flags[1] =
259  layer_flags[3] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
261  break;
262  }
263  case 5: {
264  // 3-layers, 4-frame period.
265  int ids[4] = {0, 2, 1, 2};
266  cfg->ts_periodicity = 4;
267  *flag_periodicity = 4;
268  cfg->ts_number_layers = 3;
269  cfg->ts_rate_decimator[0] = 4;
270  cfg->ts_rate_decimator[1] = 2;
271  cfg->ts_rate_decimator[2] = 1;
272  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
273  // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled in layer 1, disabled
274  // in layer 2.
275  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
277  layer_flags[2] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
279  layer_flags[1] =
280  layer_flags[3] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
282  break;
283  }
284  case 6: {
285  // 3-layers, 4-frame period.
286  int ids[4] = {0, 2, 1, 2};
287  cfg->ts_periodicity = 4;
288  *flag_periodicity = 4;
289  cfg->ts_number_layers = 3;
290  cfg->ts_rate_decimator[0] = 4;
291  cfg->ts_rate_decimator[1] = 2;
292  cfg->ts_rate_decimator[2] = 1;
293  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
294  // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
295  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
297  layer_flags[2] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
299  layer_flags[1] =
300  layer_flags[3] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
301  break;
302  }
303  case 7: {
304  // NOTE: Probably of academic interest only.
305  // 5-layers, 16-frame period.
306  int ids[16] = {0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4};
307  cfg->ts_periodicity = 16;
308  *flag_periodicity = 16;
309  cfg->ts_number_layers = 5;
310  cfg->ts_rate_decimator[0] = 16;
311  cfg->ts_rate_decimator[1] = 8;
312  cfg->ts_rate_decimator[2] = 4;
313  cfg->ts_rate_decimator[3] = 2;
314  cfg->ts_rate_decimator[4] = 1;
315  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
316  layer_flags[0] = VPX_EFLAG_FORCE_KF;
317  layer_flags[1] =
318  layer_flags[3] =
319  layer_flags[5] =
320  layer_flags[7] =
321  layer_flags[9] =
322  layer_flags[11] =
323  layer_flags[13] =
324  layer_flags[15] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
326  layer_flags[2] =
327  layer_flags[6] =
328  layer_flags[10] =
329  layer_flags[14] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF;
330  layer_flags[4] =
331  layer_flags[12] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_UPD_ARF;
332  layer_flags[8] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF;
333  break;
334  }
335  case 8: {
336  // 2-layers, with sync point at first frame of layer 1.
337  int ids[2] = {0, 1};
338  cfg->ts_periodicity = 2;
339  *flag_periodicity = 8;
340  cfg->ts_number_layers = 2;
341  cfg->ts_rate_decimator[0] = 2;
342  cfg->ts_rate_decimator[1] = 1;
343  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
344  // 0=L, 1=GF.
345  // ARF is used as predictor for all frames, and is only updated on
346  // key frame. Sync point every 8 frames.
347 
348  // Layer 0: predict from L and ARF, update L and G.
349  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
351  // Layer 1: sync point: predict from L and ARF, and update G.
352  layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_LAST |
354  // Layer 0, predict from L and ARF, update L.
355  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF |
357  // Layer 1: predict from L, G and ARF, and update G.
358  layer_flags[3] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
360  // Layer 0.
361  layer_flags[4] = layer_flags[2];
362  // Layer 1.
363  layer_flags[5] = layer_flags[3];
364  // Layer 0.
365  layer_flags[6] = layer_flags[4];
366  // Layer 1.
367  layer_flags[7] = layer_flags[5];
368  break;
369  }
370  case 9: {
371  // 3-layers: Sync points for layer 1 and 2 every 8 frames.
372  int ids[4] = {0, 2, 1, 2};
373  cfg->ts_periodicity = 4;
374  *flag_periodicity = 8;
375  cfg->ts_number_layers = 3;
376  cfg->ts_rate_decimator[0] = 4;
377  cfg->ts_rate_decimator[1] = 2;
378  cfg->ts_rate_decimator[2] = 1;
379  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
380  // 0=L, 1=GF, 2=ARF.
381  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
383  layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
385  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
387  layer_flags[3] =
388  layer_flags[5] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
389  layer_flags[4] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
391  layer_flags[6] = VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
393  layer_flags[7] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
395  break;
396  }
397  case 10: {
398  // 3-layers structure where ARF is used as predictor for all frames,
399  // and is only updated on key frame.
400  // Sync points for layer 1 and 2 every 8 frames.
401 
402  int ids[4] = {0, 2, 1, 2};
403  cfg->ts_periodicity = 4;
404  *flag_periodicity = 8;
405  cfg->ts_number_layers = 3;
406  cfg->ts_rate_decimator[0] = 4;
407  cfg->ts_rate_decimator[1] = 2;
408  cfg->ts_rate_decimator[2] = 1;
409  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
410  // 0=L, 1=GF, 2=ARF.
411  // Layer 0: predict from L and ARF; update L and G.
412  layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_ARF |
414  // Layer 2: sync point: predict from L and ARF; update none.
415  layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF |
418  // Layer 1: sync point: predict from L and ARF; update G.
419  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_ARF |
421  // Layer 2: predict from L, G, ARF; update none.
422  layer_flags[3] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
424  // Layer 0: predict from L and ARF; update L.
425  layer_flags[4] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
427  // Layer 2: predict from L, G, ARF; update none.
428  layer_flags[5] = layer_flags[3];
429  // Layer 1: predict from L, G, ARF; update G.
430  layer_flags[6] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
431  // Layer 2: predict from L, G, ARF; update none.
432  layer_flags[7] = layer_flags[3];
433  break;
434  }
435  case 11: {
436  // 3-layers structure with one reference frame.
437  // This works same as temporal_layering_mode 3.
438  // This was added to compare with vp9_spatial_svc_encoder.
439 
440  // 3-layers, 4-frame period.
441  int ids[4] = {0, 2, 1, 2};
442  cfg->ts_periodicity = 4;
443  *flag_periodicity = 4;
444  cfg->ts_number_layers = 3;
445  cfg->ts_rate_decimator[0] = 4;
446  cfg->ts_rate_decimator[1] = 2;
447  cfg->ts_rate_decimator[2] = 1;
448  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
449  // 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
450  layer_flags[0] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
452  layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
454  layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
456  layer_flags[3] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_ARF |
458  break;
459  }
460  case 12:
461  default: {
462  // 3-layers structure as in case 10, but no sync/refresh points for
463  // layer 1 and 2.
464  int ids[4] = {0, 2, 1, 2};
465  cfg->ts_periodicity = 4;
466  *flag_periodicity = 8;
467  cfg->ts_number_layers = 3;
468  cfg->ts_rate_decimator[0] = 4;
469  cfg->ts_rate_decimator[1] = 2;
470  cfg->ts_rate_decimator[2] = 1;
471  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
472  // 0=L, 1=GF, 2=ARF.
473  // Layer 0: predict from L and ARF; update L.
474  layer_flags[0] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
476  layer_flags[4] = layer_flags[0];
477  // Layer 1: predict from L, G, ARF; update G.
478  layer_flags[2] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
479  layer_flags[6] = layer_flags[2];
480  // Layer 2: predict from L, G, ARF; update none.
481  layer_flags[1] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
483  layer_flags[3] = layer_flags[1];
484  layer_flags[5] = layer_flags[1];
485  layer_flags[7] = layer_flags[1];
486  break;
487  }
488  }
489 }
490 
491 int main(int argc, char **argv) {
492  VpxVideoWriter *outfile[VPX_TS_MAX_LAYERS] = {NULL};
493  vpx_codec_ctx_t codec;
495  int frame_cnt = 0;
496  vpx_image_t raw;
497  vpx_codec_err_t res;
498  unsigned int width;
499  unsigned int height;
500  int speed;
501  int frame_avail;
502  int got_data;
503  int flags = 0;
504  unsigned int i;
505  int pts = 0; // PTS starts at 0.
506  int frame_duration = 1; // 1 timebase tick per frame.
507  int layering_mode = 0;
508  int layer_flags[VPX_TS_MAX_PERIODICITY] = {0};
509  int flag_periodicity = 1;
510 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION)
511  vpx_svc_layer_id_t layer_id = {0, 0};
512 #else
513  vpx_svc_layer_id_t layer_id = {0};
514 #endif
515  const VpxInterface *encoder = NULL;
516  FILE *infile = NULL;
517  struct RateControlMetrics rc;
518  int64_t cx_time = 0;
519  const int min_args_base = 11;
520 #if CONFIG_VP9_HIGHBITDEPTH
521  vpx_bit_depth_t bit_depth = VPX_BITS_8;
522  int input_bit_depth = 8;
523  const int min_args = min_args_base + 1;
524 #else
525  const int min_args = min_args_base;
526 #endif // CONFIG_VP9_HIGHBITDEPTH
527  double sum_bitrate = 0.0;
528  double sum_bitrate2 = 0.0;
529  double framerate = 30.0;
530 
531  exec_name = argv[0];
532  // Check usage and arguments.
533  if (argc < min_args) {
534 #if CONFIG_VP9_HIGHBITDEPTH
535  die("Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> "
536  "<rate_num> <rate_den> <speed> <frame_drop_threshold> <mode> "
537  "<Rate_0> ... <Rate_nlayers-1> <bit-depth> \n", argv[0]);
538 #else
539  die("Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> "
540  "<rate_num> <rate_den> <speed> <frame_drop_threshold> <mode> "
541  "<Rate_0> ... <Rate_nlayers-1> \n", argv[0]);
542 #endif // CONFIG_VP9_HIGHBITDEPTH
543  }
544 
545  encoder = get_vpx_encoder_by_name(argv[3]);
546  if (!encoder)
547  die("Unsupported codec.");
548 
549  printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
550 
551  width = strtol(argv[4], NULL, 0);
552  height = strtol(argv[5], NULL, 0);
553  if (width < 16 || width % 2 || height < 16 || height % 2) {
554  die("Invalid resolution: %d x %d", width, height);
555  }
556 
557  layering_mode = strtol(argv[10], NULL, 0);
558  if (layering_mode < 0 || layering_mode > 13) {
559  die("Invalid layering mode (0..12) %s", argv[10]);
560  }
561 
562  if (argc != min_args + mode_to_num_layers[layering_mode]) {
563  die("Invalid number of arguments");
564  }
565 
566 #if CONFIG_VP9_HIGHBITDEPTH
567  switch (strtol(argv[argc-1], NULL, 0)) {
568  case 8:
569  bit_depth = VPX_BITS_8;
570  input_bit_depth = 8;
571  break;
572  case 10:
573  bit_depth = VPX_BITS_10;
574  input_bit_depth = 10;
575  break;
576  case 12:
577  bit_depth = VPX_BITS_12;
578  input_bit_depth = 12;
579  break;
580  default:
581  die("Invalid bit depth (8, 10, 12) %s", argv[argc-1]);
582  }
583  if (!vpx_img_alloc(&raw,
584  bit_depth == VPX_BITS_8 ? VPX_IMG_FMT_I420 :
585  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 = strtol(argv[6], NULL, 0);
616  cfg.g_timebase.den = strtol(argv[7], NULL, 0);
617 
618  speed = 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];
625  ++i) {
626  rc.layer_target_bitrate[i - 11] = strtol(argv[i], NULL, 0);
627  if (strncmp(encoder->name, "vp8", 3) == 0)
628  cfg.ts_target_bitrate[i - 11] = rc.layer_target_bitrate[i - 11];
629  else if (strncmp(encoder->name, "vp9", 3) == 0)
630  cfg.layer_target_bitrate[i - 11] = rc.layer_target_bitrate[i - 11];
631  }
632 
633  // Real time parameters.
634  cfg.rc_dropframe_thresh = strtol(argv[9], NULL, 0);
635  cfg.rc_end_usage = VPX_CBR;
636  cfg.rc_min_quantizer = 2;
637  cfg.rc_max_quantizer = 56;
638  if (strncmp(encoder->name, "vp9", 3) == 0)
639  cfg.rc_max_quantizer = 52;
640  cfg.rc_undershoot_pct = 50;
641  cfg.rc_overshoot_pct = 50;
642  cfg.rc_buf_initial_sz = 500;
643  cfg.rc_buf_optimal_sz = 600;
644  cfg.rc_buf_sz = 1000;
645 
646  // Disable dynamic resizing by default.
647  cfg.rc_resize_allowed = 0;
648 
649  // Use 1 thread as default.
650  cfg.g_threads = 1;
651 
652  // Enable error resilient mode.
653  cfg.g_error_resilient = 1;
654  cfg.g_lag_in_frames = 0;
655  cfg.kf_mode = VPX_KF_AUTO;
656 
657  // Disable automatic keyframe placement.
658  cfg.kf_min_dist = cfg.kf_max_dist = 3000;
659 
661 
662  set_temporal_layer_pattern(layering_mode,
663  &cfg,
664  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])
692  die("Failed to open %s for writing", file_name);
693 
694  assert(outfile[i] != NULL);
695  }
696  // No spatial layers in this encoder.
697  cfg.ss_number_layers = 1;
698 
699  // Initialize codec.
700 #if CONFIG_VP9_HIGHBITDEPTH
701  if (vpx_codec_enc_init(
702  &codec, encoder->codec_interface(), &cfg,
703  bit_depth == VPX_BITS_8 ? 0 : VPX_CODEC_USE_HIGHBITDEPTH))
704 #else
705  if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
706 #endif // CONFIG_VP9_HIGHBITDEPTH
707  die_codec(&codec, "Failed to initialize encoder");
708 
709  if (strncmp(encoder->name, "vp8", 3) == 0) {
710  vpx_codec_control(&codec, VP8E_SET_CPUUSED, -speed);
711  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  vpx_codec_control(&codec, VP8E_SET_CPUUSED, speed);
718  vpx_codec_control(&codec, VP9E_SET_NOISE_SENSITIVITY, kDenoiserOff);
722  if (vpx_codec_control(&codec, VP9E_SET_SVC, layering_mode > 0 ? 1: 0))
723  die_codec(&codec, "Failed to set SVC");
724  for (i = 0; i < cfg.ts_number_layers; ++i) {
725  svc_params.max_quantizers[i] = cfg.rc_max_quantizer;
726  svc_params.min_quantizers[i] = cfg.rc_min_quantizer;
727  }
728  svc_params.scaling_factor_num[0] = cfg.g_h;
729  svc_params.scaling_factor_den[0] = cfg.g_h;
730  vpx_codec_control(&codec, VP9E_SET_SVC_PARAMETERS, &svc_params);
731  }
732  if (strncmp(encoder->name, "vp8", 3) == 0) {
734  }
736  // This controls the maximum target size of the key frame.
737  // For generating smaller key frames, use a smaller max_intra_size_pct
738  // value, like 100 or 200.
739  {
740  const int max_intra_size_pct = 900;
742  max_intra_size_pct);
743  }
744 
745  frame_avail = 1;
746  while (frame_avail || got_data) {
747  struct vpx_usec_timer timer;
748  vpx_codec_iter_t iter = NULL;
749  const vpx_codec_cx_pkt_t *pkt;
750 #if VPX_ENCODER_ABI_VERSION > (4 + VPX_CODEC_ABI_VERSION)
751  // Update the temporal layer_id. No spatial layers in this test.
752  layer_id.spatial_layer_id = 0;
753 #endif
754  layer_id.temporal_layer_id =
755  cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
756  if (strncmp(encoder->name, "vp9", 3) == 0) {
757  vpx_codec_control(&codec, VP9E_SET_SVC_LAYER_ID, &layer_id);
758  } else if (strncmp(encoder->name, "vp8", 3) == 0) {
760  layer_id.temporal_layer_id);
761  }
762  flags = layer_flags[frame_cnt % flag_periodicity];
763  if (layering_mode == 0)
764  flags = 0;
765  frame_avail = vpx_img_read(&raw, infile);
766  if (frame_avail)
767  ++rc.layer_input_frames[layer_id.temporal_layer_id];
768  vpx_usec_timer_start(&timer);
769  if (vpx_codec_encode(&codec, frame_avail? &raw : NULL, pts, 1, flags,
770  VPX_DL_REALTIME)) {
771  die_codec(&codec, "Failed to encode frame");
772  }
773  vpx_usec_timer_mark(&timer);
774  cx_time += vpx_usec_timer_elapsed(&timer);
775  // Reset KF flag.
776  if (layering_mode != 7) {
777  layer_flags[0] &= ~VPX_EFLAG_FORCE_KF;
778  }
779  got_data = 0;
780  while ( (pkt = vpx_codec_get_cx_data(&codec, &iter)) ) {
781  got_data = 1;
782  switch (pkt->kind) {
784  for (i = cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
785  i < cfg.ts_number_layers; ++i) {
786  vpx_video_writer_write_frame(outfile[i], pkt->data.frame.buf,
787  pkt->data.frame.sz, pts);
788  ++rc.layer_tot_enc_frames[i];
789  rc.layer_encoding_bitrate[i] += 8.0 * pkt->data.frame.sz;
790  // Keep count of rate control stats per layer (for non-key frames).
791  if (i == cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity] &&
792  !(pkt->data.frame.flags & VPX_FRAME_IS_KEY)) {
793  rc.layer_avg_frame_size[i] += 8.0 * pkt->data.frame.sz;
794  rc.layer_avg_rate_mismatch[i] +=
795  fabs(8.0 * pkt->data.frame.sz - rc.layer_pfb[i]) /
796  rc.layer_pfb[i];
797  ++rc.layer_enc_frames[i];
798  }
799  }
800  // Update for short-time encoding bitrate states, for moving window
801  // of size rc->window, shifted by rc->window / 2.
802  // Ignore first window segment, due to key frame.
803  if (frame_cnt > rc.window_size) {
804  sum_bitrate += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
805  if (frame_cnt % rc.window_size == 0) {
806  rc.window_count += 1;
807  rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
808  rc.variance_st_encoding_bitrate +=
809  (sum_bitrate / rc.window_size) *
810  (sum_bitrate / rc.window_size);
811  sum_bitrate = 0.0;
812  }
813  }
814  // Second shifted window.
815  if (frame_cnt > rc.window_size + rc.window_size / 2) {
816  sum_bitrate2 += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
817  if (frame_cnt > 2 * rc.window_size &&
818  frame_cnt % rc.window_size == 0) {
819  rc.window_count += 1;
820  rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
821  rc.variance_st_encoding_bitrate +=
822  (sum_bitrate2 / rc.window_size) *
823  (sum_bitrate2 / rc.window_size);
824  sum_bitrate2 = 0.0;
825  }
826  }
827  break;
828  default:
829  break;
830  }
831  }
832  ++frame_cnt;
833  pts += frame_duration;
834  }
835  fclose(infile);
836  printout_rate_control_summary(&rc, &cfg, frame_cnt);
837  printf("\n");
838  printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
839  frame_cnt,
840  1000 * (float)cx_time / (double)(frame_cnt * 1000000),
841  1000000 * (double)frame_cnt / (double)cx_time);
842 
843  if (vpx_codec_destroy(&codec))
844  die_codec(&codec, "Failed to destroy codec");
845 
846  // Try to rewrite the output file headers with the actual frame count.
847  for (i = 0; i < cfg.ts_number_layers; ++i)
848  vpx_video_writer_close(outfile[i]);
849 
850  vpx_img_free(&raw);
851  return EXIT_SUCCESS;
852 }
unsigned int rc_buf_initial_sz
Decoder Buffer Initial Size.
Definition: vpx_encoder.h:610
int min_quantizers[12]
Definition: vpx_encoder.h:773
unsigned int ts_number_layers
Number of temporal coding layers.
Definition: vpx_encoder.h:715
Codec control function to set encoder internal speed settings.
Definition: vp8cx.h:164
#define VPX_MAX_LAYERS
Definition: vpx_encoder.h:46
#define VP8_EFLAG_NO_REF_LAST
Don't reference the last frame.
Definition: vp8cx.h:58
#define VP8_EFLAG_NO_UPD_GF
Don't update the golden frame.
Definition: vp8cx.h:92
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:458
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:397
Definition: vpx_encoder.h:276
Codec control function to set noise sensitivity.
Definition: vp8cx.h:423
unsigned int layer_target_bitrate[12]
Target bitrate for each spatial/temporal layer.
Definition: vpx_encoder.h:755
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition: vpx_encoder.h:600
#define VP8_EFLAG_NO_REF_GF
Don't reference the golden frame.
Definition: vp8cx.h:67
unsigned int g_input_bit_depth
Bit-depth of the input frames.
Definition: vpx_encoder.h:383
enum vpx_kf_mode kf_mode
Keyframe placement mode.
Definition: vpx_encoder.h:665
int den
Definition: vpx_encoder.h:261
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:552
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition: vpx_encoder.h:541
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: vpx_encoder.h:685
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: vpx_encoder.h:429
Encoder configuration structure.
Definition: vpx_encoder.h:314
Definition: vpx_encoder.h:292
int spatial_layer_id
Definition: vp8cx.h:685
Codec control function to set Max data rate for Intra frames.
Definition: vp8cx.h:260
#define VPX_CODEC_USE_HIGHBITDEPTH
Definition: vpx_encoder.h:101
Encoder output packet.
Definition: vpx_encoder.h:195
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition: vpx_encoder.h:583
Codec control function to set parameters for SVC.
Definition: vp8cx.h:440
unsigned int ts_rate_decimator[5]
Frame rate decimation factor for each temporal layer.
Definition: vpx_encoder.h:729
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition: vpx_encoder.h:620
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: vpx_encoder.h:675
unsigned int g_profile
Bitstream profile to use.
Definition: vpx_encoder.h:346
Codec control function to set number of tile columns.
Definition: vp8cx.h:353
unsigned int ts_layer_id[16]
Template defining the membership of frames to temporal layers.
Definition: vpx_encoder.h:747
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
int scaling_factor_num[12]
Definition: vpx_encoder.h:774
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:357
unsigned int ts_target_bitrate[5]
Target bitrate for each temporal layer.
Definition: vpx_encoder.h:722
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:570
Codec control function to set adaptive quantization mode.
Definition: vp8cx.h:400
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:367
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:196
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: vpx_encoder.h:452
vp9 svc layer parameters
Definition: vp8cx.h:684
Codec control function to set the temporal layer id.
Definition: vp8cx.h:307
#define VP8_EFLAG_NO_UPD_LAST
Don't update the last frame.
Definition: vp8cx.h:84
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:197
unsigned int rc_target_bitrate
Target data rate.
Definition: vpx_encoder.h:525
#define VPX_DL_REALTIME
Definition: vpx_encoder.h:911
int num
Definition: vpx_encoder.h:260
control function to set noise sensitivity
Definition: vp8cx.h:179
Definition: vpx_codec.h:222
unsigned int g_threads
Maximum number of threads to use.
Definition: vpx_encoder.h:335
unsigned int ss_number_layers
Number of spatial coding layers.
Definition: vpx_encoder.h:695
vpx_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition: vpx_encoder.h:375
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:813
Codec control function to set encoder screen content mode.
Definition: vp8cx.h:315
unsigned int rc_resize_allowed
Enable/disable spatial resampling, if supported by the codec.
Definition: vpx_encoder.h:462
Bypass mode. Used when application needs to control temporal layering. This will only work when the n...
Definition: vp8cx.h:592
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:763
int temporal_layer_id
Definition: vp8cx.h:686
Codec control function to enable/disable periodic Q boost.
Definition: vp8cx.h:415
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:432
#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:738
#define VP8_EFLAG_NO_REF_ARF
Don't reference the alternate reference frame.
Definition: vp8cx.h:76
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
Definition: vpx_codec.h:220
int scaling_factor_den[12]
Definition: vpx_encoder.h:775
Codec control function to set the threshold for MBs treated static.
Definition: vp8cx.h:191
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:130
Definition: vpx_codec.h:221
#define VPX_EFLAG_FORCE_KF
Definition: vpx_encoder.h:305
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:188
Definition: vpx_encoder.h:176
int max_quantizers[12]
Definition: vpx_encoder.h:772
vp9 svc extra configure parameters
Definition: vpx_encoder.h:771
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: vpx_encoder.h:406
#define VP8_EFLAG_NO_UPD_ARF
Don't update the alternate reference frame.
Definition: vp8cx.h:100
#define VP8_EFLAG_NO_UPD_ENTROPY
Disable entropy update.
Definition: vp8cx.h:124
Codec control function to set svc layer for spatial and temporal.
Definition: vp8cx.h:449
enum vpx_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: vpx_encoder.h:504
Codec context structure.
Definition: vpx_codec.h:199