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 "./y4minput.h"
23#include "../vpx_ports/vpx_timer.h"
24#include "vpx/vp8cx.h"
25#include "vpx/vpx_encoder.h"
26#include "vpx_ports/bitops.h"
27
28#include "../tools_common.h"
29#include "../video_writer.h"
30
31#define ROI_MAP 0
32
33#define zero(Dest) memset(&(Dest), 0, sizeof(Dest));
34
35 static const char *exec_name;
36
37 void usage_exit(void) { exit(EXIT_FAILURE); }
38
39// Denoiser states for vp8, for temporal denoising.
40 enum denoiserStateVp8 {
41 kVp8DenoiserOff,
42 kVp8DenoiserOnYOnly,
43 kVp8DenoiserOnYUV,
44 kVp8DenoiserOnYUVAggressive,
45 kVp8DenoiserOnAdaptive
46};
47
48// Denoiser states for vp9, for temporal denoising.
49 enum denoiserStateVp9 {
50 kVp9DenoiserOff,
51 kVp9DenoiserOnYOnly,
52 // For SVC: denoise the top two spatial layers.
53 kVp9DenoiserOnYTwoSpatialLayers
54};
55
56 static int mode_to_num_layers[13] = { 1, 2, 2, 3, 3, 3, 3, 5, 2, 3, 3, 3, 3 };
57
58// For rate control encoding stats.
59 struct RateControlMetrics {
60 // Number of input frames per layer.
61 int layer_input_frames[VPX_TS_MAX_LAYERS];
62 // Total (cumulative) number of encoded frames per layer.
63 int layer_tot_enc_frames[VPX_TS_MAX_LAYERS];
64 // Number of encoded non-key frames per layer.
65 int layer_enc_frames[VPX_TS_MAX_LAYERS];
66 // Framerate per layer layer (cumulative).
67 double layer_framerate[VPX_TS_MAX_LAYERS];
68 // Target average frame size per layer (per-frame-bandwidth per layer).
69 double layer_pfb[VPX_TS_MAX_LAYERS];
70 // Actual average frame size per layer.
71 double layer_avg_frame_size[VPX_TS_MAX_LAYERS];
72 // Average rate mismatch per layer (|target - actual| / target).
73 double layer_avg_rate_mismatch[VPX_TS_MAX_LAYERS];
74 // Actual encoding bitrate per layer (cumulative).
75 double layer_encoding_bitrate[VPX_TS_MAX_LAYERS];
76 // Average of the short-time encoder actual bitrate.
77 // TODO(marpan): Should we add these short-time stats for each layer?
78 double avg_st_encoding_bitrate;
79 // Variance of the short-time encoder actual bitrate.
80 double variance_st_encoding_bitrate;
81 // Window (number of frames) for computing short-timee encoding bitrate.
82 int window_size;
83 // Number of window measurements.
84 int window_count;
85 int layer_target_bitrate[VPX_MAX_LAYERS];
86};
87
88// Note: these rate control metrics assume only 1 key frame in the
89// sequence (i.e., first frame only). So for temporal pattern# 7
90// (which has key frame for every frame on base layer), the metrics
91// computation will be off/wrong.
92// TODO(marpan): Update these metrics to account for multiple key frames
93// in the stream.
94 static void set_rate_control_metrics(struct RateControlMetrics *rc,
95 vpx_codec_enc_cfg_t *cfg) {
96 int i = 0;
97 // Set the layer (cumulative) framerate and the target layer (non-cumulative)
98 // per-frame-bandwidth, for the rate control encoding stats below.
99 const double framerate = cfg->g_timebase.den / cfg->g_timebase.num;
100 const int ts_number_layers = cfg->ts_number_layers;
101 rc->layer_framerate[0] = framerate / cfg->ts_rate_decimator[0];
102 rc->layer_pfb[0] =
103 1000.0 * rc->layer_target_bitrate[0] / rc->layer_framerate[0];
104 for (i = 0; i < ts_number_layers; ++i) {
105 if (i > 0) {
106 rc->layer_framerate[i] = framerate / cfg->ts_rate_decimator[i];
107 rc->layer_pfb[i] =
108 1000.0 *
109 (rc->layer_target_bitrate[i] - rc->layer_target_bitrate[i - 1]) /
110 (rc->layer_framerate[i] - rc->layer_framerate[i - 1]);
111 }
112 rc->layer_input_frames[i] = 0;
113 rc->layer_enc_frames[i] = 0;
114 rc->layer_tot_enc_frames[i] = 0;
115 rc->layer_encoding_bitrate[i] = 0.0;
116 rc->layer_avg_frame_size[i] = 0.0;
117 rc->layer_avg_rate_mismatch[i] = 0.0;
118 }
119 rc->window_count = 0;
120 rc->window_size = 15;
121 rc->avg_st_encoding_bitrate = 0.0;
122 rc->variance_st_encoding_bitrate = 0.0;
123 // Target bandwidth for the whole stream.
124 // Set to layer_target_bitrate for highest layer (total bitrate).
125 cfg->rc_target_bitrate = rc->layer_target_bitrate[ts_number_layers - 1];
126}
127
128 static void printout_rate_control_summary(struct RateControlMetrics *rc,
129 vpx_codec_enc_cfg_t *cfg,
130 int frame_cnt) {
131 unsigned int i = 0;
132 int tot_num_frames = 0;
133 double perc_fluctuation = 0.0;
134 printf("Total number of processed frames: %d\n\n", frame_cnt - 1);
135 printf("Rate control layer stats for %d layer(s):\n\n",
136 cfg->ts_number_layers);
137 for (i = 0; i < cfg->ts_number_layers; ++i) {
138 const int num_dropped =
139 (i > 0) ? (rc->layer_input_frames[i] - rc->layer_enc_frames[i])
140 : (rc->layer_input_frames[i] - rc->layer_enc_frames[i] - 1);
141 tot_num_frames += rc->layer_input_frames[i];
142 rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[i] *
143 rc->layer_encoding_bitrate[i] /
144 tot_num_frames;
145 rc->layer_avg_frame_size[i] =
146 rc->layer_avg_frame_size[i] / rc->layer_enc_frames[i];
147 rc->layer_avg_rate_mismatch[i] =
148 100.0 * rc->layer_avg_rate_mismatch[i] / rc->layer_enc_frames[i];
149 printf("For layer#: %d \n", i);
150 printf("Bitrate (target vs actual): %d %f \n", rc->layer_target_bitrate[i],
151 rc->layer_encoding_bitrate[i]);
152 printf("Average frame size (target vs actual): %f %f \n", rc->layer_pfb[i],
153 rc->layer_avg_frame_size[i]);
154 printf("Average rate_mismatch: %f \n", rc->layer_avg_rate_mismatch[i]);
155 printf(
156 "Number of input frames, encoded (non-key) frames, "
157 "and perc dropped frames: %d %d %f \n",
158 rc->layer_input_frames[i], rc->layer_enc_frames[i],
159 100.0 * num_dropped / rc->layer_input_frames[i]);
160 printf("\n");
161 }
162 rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
163 rc->variance_st_encoding_bitrate =
164 rc->variance_st_encoding_bitrate / rc->window_count -
165 (rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
166 perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
167 rc->avg_st_encoding_bitrate;
168 printf("Short-time stats, for window of %d frames: \n", rc->window_size);
169 printf("Average, rms-variance, and percent-fluct: %f %f %f \n",
170 rc->avg_st_encoding_bitrate, sqrt(rc->variance_st_encoding_bitrate),
171 perc_fluctuation);
172 if ((frame_cnt - 1) != tot_num_frames)
173 die("Error: Number of input frames not equal to output! \n");
174}
175
176#if ROI_MAP
177 static void set_roi_map(const char *enc_name, vpx_codec_enc_cfg_t *cfg,
178 vpx_roi_map_t *roi) {
179 unsigned int i, j;
180 int block_size = 0;
181 uint8_t is_vp8 = strncmp(enc_name, "vp8", 3) == 0 ? 1 : 0;
182 uint8_t is_vp9 = strncmp(enc_name, "vp9", 3) == 0 ? 1 : 0;
183 if (!is_vp8 && !is_vp9) {
184 die("unsupported codec.");
185 }
186 zero(*roi);
187
188 block_size = is_vp9 && !is_vp8 ? 8 : 16;
189
190 // ROI is based on the segments (4 for vp8, 8 for vp9), smallest unit for
191 // segment is 16x16 for vp8, 8x8 for vp9.
192 roi->rows = (cfg->g_h + block_size - 1) / block_size;
193 roi->cols = (cfg->g_w + block_size - 1) / block_size;
194
195 // Applies delta QP on the segment blocks, varies from -63 to 63.
196 // Setting to negative means lower QP (better quality).
197 // Below we set delta_q to the extreme (-63) to show strong effect.
198 // VP8 uses the first 4 segments. VP9 uses all 8 segments.
199 zero(roi->delta_q);
200 roi->delta_q[1] = -63;
201
202 // Applies delta loopfilter strength on the segment blocks, varies from -63 to
203 // 63. Setting to positive means stronger loopfilter. VP8 uses the first 4
204 // segments. VP9 uses all 8 segments.
205 zero(roi->delta_lf);
206
207 if (is_vp8) {
208 // Applies skip encoding threshold on the segment blocks, varies from 0 to
209 // UINT_MAX. Larger value means more skipping of encoding is possible.
210 // This skip threshold only applies on delta frames.
211 zero(roi->static_threshold);
212 }
213
214 if (is_vp9) {
215 // Apply skip segment. Setting to 1 means this block will be copied from
216 // previous frame.
217 zero(roi->skip);
218 }
219
220 if (is_vp9) {
221 // Apply ref frame segment.
222 // -1 : Do not apply this segment.
223 // 0 : Froce using intra.
224 // 1 : Force using last.
225 // 2 : Force using golden.
226 // 3 : Force using alfref but not used in non-rd pickmode for 0 lag.
227 memset(roi->ref_frame, -1, sizeof(roi->ref_frame));
228 roi->ref_frame[1] = 1;
229 }
230
231 // Use 2 states: 1 is center square, 0 is the rest.
232 roi->roi_map =
233 (uint8_t *)calloc(roi->rows * roi->cols, sizeof(*roi->roi_map));
234 for (i = 0; i < roi->rows; ++i) {
235 for (j = 0; j < roi->cols; ++j) {
236 if (i > (roi->rows >> 2) && i < ((roi->rows * 3) >> 2) &&
237 j > (roi->cols >> 2) && j < ((roi->cols * 3) >> 2)) {
238 roi->roi_map[i * roi->cols + j] = 1;
239 }
240 }
241 }
242}
243#endif
244
245// Temporal scaling parameters:
246// NOTE: The 3 prediction frames cannot be used interchangeably due to
247// differences in the way they are handled throughout the code. The
248// frames should be allocated to layers in the order LAST, GF, ARF.
249// Other combinations work, but may produce slightly inferior results.
250 static void set_temporal_layer_pattern(int layering_mode,
251 vpx_codec_enc_cfg_t *cfg,
252 int *layer_flags,
253 int *flag_periodicity) {
254 switch (layering_mode) {
255 case 0: {
256 // 1-layer.
257 int ids[1] = { 0 };
258 cfg->ts_periodicity = 1;
259 *flag_periodicity = 1;
260 cfg->ts_number_layers = 1;
261 cfg->ts_rate_decimator[0] = 1;
262 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
263 // Update L only.
264 layer_flags[0] =
265 VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
266 break;
267 }
268 case 1: {
269 // 2-layers, 2-frame period.
270 int ids[2] = { 0, 1 };
271 cfg->ts_periodicity = 2;
272 *flag_periodicity = 2;
273 cfg->ts_number_layers = 2;
274 cfg->ts_rate_decimator[0] = 2;
275 cfg->ts_rate_decimator[1] = 1;
276 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
277#if 1
278 // 0=L, 1=GF, Intra-layer prediction enabled.
279 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF |
280 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF |
281 VP8_EFLAG_NO_REF_ARF;
282 layer_flags[1] =
283 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_REF_ARF;
284#else
285 // 0=L, 1=GF, Intra-layer prediction disabled.
286 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF |
287 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF |
288 VP8_EFLAG_NO_REF_ARF;
289 layer_flags[1] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
290 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_LAST;
291#endif
292 break;
293 }
294 case 2: {
295 // 2-layers, 3-frame period.
296 int ids[3] = { 0, 1, 1 };
297 cfg->ts_periodicity = 3;
298 *flag_periodicity = 3;
299 cfg->ts_number_layers = 2;
300 cfg->ts_rate_decimator[0] = 3;
301 cfg->ts_rate_decimator[1] = 1;
302 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
303 // 0=L, 1=GF, Intra-layer prediction enabled.
304 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
305 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
306 VP8_EFLAG_NO_UPD_ARF;
307 layer_flags[1] = layer_flags[2] =
308 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_ARF |
309 VP8_EFLAG_NO_UPD_LAST;
310 break;
311 }
312 case 3: {
313 // 3-layers, 6-frame period.
314 int ids[6] = { 0, 2, 2, 1, 2, 2 };
315 cfg->ts_periodicity = 6;
316 *flag_periodicity = 6;
317 cfg->ts_number_layers = 3;
318 cfg->ts_rate_decimator[0] = 6;
319 cfg->ts_rate_decimator[1] = 3;
320 cfg->ts_rate_decimator[2] = 1;
321 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
322 // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
323 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
324 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
325 VP8_EFLAG_NO_UPD_ARF;
326 layer_flags[3] =
327 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
328 layer_flags[1] = layer_flags[2] = layer_flags[4] = layer_flags[5] =
329 VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_LAST;
330 break;
331 }
332 case 4: {
333 // 3-layers, 4-frame period.
334 int ids[4] = { 0, 2, 1, 2 };
335 cfg->ts_periodicity = 4;
336 *flag_periodicity = 4;
337 cfg->ts_number_layers = 3;
338 cfg->ts_rate_decimator[0] = 4;
339 cfg->ts_rate_decimator[1] = 2;
340 cfg->ts_rate_decimator[2] = 1;
341 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
342 // 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
343 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
344 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
345 VP8_EFLAG_NO_UPD_ARF;
346 layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
347 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
348 layer_flags[1] = layer_flags[3] =
349 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
350 VP8_EFLAG_NO_UPD_ARF;
351 break;
352 }
353 case 5: {
354 // 3-layers, 4-frame period.
355 int ids[4] = { 0, 2, 1, 2 };
356 cfg->ts_periodicity = 4;
357 *flag_periodicity = 4;
358 cfg->ts_number_layers = 3;
359 cfg->ts_rate_decimator[0] = 4;
360 cfg->ts_rate_decimator[1] = 2;
361 cfg->ts_rate_decimator[2] = 1;
362 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
363 // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled in layer 1, disabled
364 // in layer 2.
365 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
366 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
367 VP8_EFLAG_NO_UPD_ARF;
368 layer_flags[2] =
369 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
370 layer_flags[1] = layer_flags[3] =
371 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
372 VP8_EFLAG_NO_UPD_ARF;
373 break;
374 }
375 case 6: {
376 // 3-layers, 4-frame period.
377 int ids[4] = { 0, 2, 1, 2 };
378 cfg->ts_periodicity = 4;
379 *flag_periodicity = 4;
380 cfg->ts_number_layers = 3;
381 cfg->ts_rate_decimator[0] = 4;
382 cfg->ts_rate_decimator[1] = 2;
383 cfg->ts_rate_decimator[2] = 1;
384 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
385 // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
386 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
387 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
388 VP8_EFLAG_NO_UPD_ARF;
389 layer_flags[2] =
390 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
391 layer_flags[1] = layer_flags[3] =
392 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
393 break;
394 }
395 case 7: {
396 // NOTE: Probably of academic interest only.
397 // 5-layers, 16-frame period.
398 int ids[16] = { 0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4 };
399 cfg->ts_periodicity = 16;
400 *flag_periodicity = 16;
401 cfg->ts_number_layers = 5;
402 cfg->ts_rate_decimator[0] = 16;
403 cfg->ts_rate_decimator[1] = 8;
404 cfg->ts_rate_decimator[2] = 4;
405 cfg->ts_rate_decimator[3] = 2;
406 cfg->ts_rate_decimator[4] = 1;
407 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
408 layer_flags[0] = VPX_EFLAG_FORCE_KF;
409 layer_flags[1] = layer_flags[3] = layer_flags[5] = layer_flags[7] =
410 layer_flags[9] = layer_flags[11] = layer_flags[13] = layer_flags[15] =
411 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
412 VP8_EFLAG_NO_UPD_ARF;
413 layer_flags[2] = layer_flags[6] = layer_flags[10] = layer_flags[14] =
414 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF;
415 layer_flags[4] = layer_flags[12] =
416 VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_UPD_ARF;
417 layer_flags[8] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF;
418 break;
419 }
420 case 8: {
421 // 2-layers, with sync point at first frame of layer 1.
422 int ids[2] = { 0, 1 };
423 cfg->ts_periodicity = 2;
424 *flag_periodicity = 8;
425 cfg->ts_number_layers = 2;
426 cfg->ts_rate_decimator[0] = 2;
427 cfg->ts_rate_decimator[1] = 1;
428 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
429 // 0=L, 1=GF.
430 // ARF is used as predictor for all frames, and is only updated on
431 // key frame. Sync point every 8 frames.
432
433 // Layer 0: predict from L and ARF, update L and G.
434 layer_flags[0] =
435 VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_ARF;
436 // Layer 1: sync point: predict from L and ARF, and update G.
437 layer_flags[1] =
438 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
439 // Layer 0, predict from L and ARF, update L.
440 layer_flags[2] =
441 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
442 // Layer 1: predict from L, G and ARF, and update G.
443 layer_flags[3] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
444 VP8_EFLAG_NO_UPD_ENTROPY;
445 // Layer 0.
446 layer_flags[4] = layer_flags[2];
447 // Layer 1.
448 layer_flags[5] = layer_flags[3];
449 // Layer 0.
450 layer_flags[6] = layer_flags[4];
451 // Layer 1.
452 layer_flags[7] = layer_flags[5];
453 break;
454 }
455 case 9: {
456 // 3-layers: Sync points for layer 1 and 2 every 8 frames.
457 int ids[4] = { 0, 2, 1, 2 };
458 cfg->ts_periodicity = 4;
459 *flag_periodicity = 8;
460 cfg->ts_number_layers = 3;
461 cfg->ts_rate_decimator[0] = 4;
462 cfg->ts_rate_decimator[1] = 2;
463 cfg->ts_rate_decimator[2] = 1;
464 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
465 // 0=L, 1=GF, 2=ARF.
466 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
467 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
468 VP8_EFLAG_NO_UPD_ARF;
469 layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
470 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
471 layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
472 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
473 layer_flags[3] = layer_flags[5] =
474 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
475 layer_flags[4] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
476 VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
477 layer_flags[6] =
478 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
479 layer_flags[7] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
480 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_ENTROPY;
481 break;
482 }
483 case 10: {
484 // 3-layers structure where ARF is used as predictor for all frames,
485 // and is only updated on key frame.
486 // Sync points for layer 1 and 2 every 8 frames.
487
488 int ids[4] = { 0, 2, 1, 2 };
489 cfg->ts_periodicity = 4;
490 *flag_periodicity = 8;
491 cfg->ts_number_layers = 3;
492 cfg->ts_rate_decimator[0] = 4;
493 cfg->ts_rate_decimator[1] = 2;
494 cfg->ts_rate_decimator[2] = 1;
495 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
496 // 0=L, 1=GF, 2=ARF.
497 // Layer 0: predict from L and ARF; update L and G.
498 layer_flags[0] =
499 VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF;
500 // Layer 2: sync point: predict from L and ARF; update none.
501 layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF |
502 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
503 VP8_EFLAG_NO_UPD_ENTROPY;
504 // Layer 1: sync point: predict from L and ARF; update G.
505 layer_flags[2] =
506 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
507 // Layer 2: predict from L, G, ARF; update none.
508 layer_flags[3] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
509 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ENTROPY;
510 // Layer 0: predict from L and ARF; update L.
511 layer_flags[4] =
512 VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF;
513 // Layer 2: predict from L, G, ARF; update none.
514 layer_flags[5] = layer_flags[3];
515 // Layer 1: predict from L, G, ARF; update G.
516 layer_flags[6] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
517 // Layer 2: predict from L, G, ARF; update none.
518 layer_flags[7] = layer_flags[3];
519 break;
520 }
521 case 11: {
522 // 3-layers structure with one reference frame.
523 // This works same as temporal_layering_mode 3.
524 // This was added to compare with vp9_spatial_svc_encoder.
525
526 // 3-layers, 4-frame period.
527 int ids[4] = { 0, 2, 1, 2 };
528 cfg->ts_periodicity = 4;
529 *flag_periodicity = 4;
530 cfg->ts_number_layers = 3;
531 cfg->ts_rate_decimator[0] = 4;
532 cfg->ts_rate_decimator[1] = 2;
533 cfg->ts_rate_decimator[2] = 1;
534 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
535 // 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
536 layer_flags[0] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
537 VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
538 layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
539 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
540 layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
541 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
542 layer_flags[3] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_ARF |
543 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
544 break;
545 }
546 case 12:
547 default: {
548 // 3-layers structure as in case 10, but no sync/refresh points for
549 // layer 1 and 2.
550 int ids[4] = { 0, 2, 1, 2 };
551 cfg->ts_periodicity = 4;
552 *flag_periodicity = 8;
553 cfg->ts_number_layers = 3;
554 cfg->ts_rate_decimator[0] = 4;
555 cfg->ts_rate_decimator[1] = 2;
556 cfg->ts_rate_decimator[2] = 1;
557 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
558 // 0=L, 1=GF, 2=ARF.
559 // Layer 0: predict from L and ARF; update L.
560 layer_flags[0] =
561 VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF;
562 layer_flags[4] = layer_flags[0];
563 // Layer 1: predict from L, G, ARF; update G.
564 layer_flags[2] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
565 layer_flags[6] = layer_flags[2];
566 // Layer 2: predict from L, G, ARF; update none.
567 layer_flags[1] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
568 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ENTROPY;
569 layer_flags[3] = layer_flags[1];
570 layer_flags[5] = layer_flags[1];
571 layer_flags[7] = layer_flags[1];
572 break;
573 }
574 }
575}
576
577 int main(int argc, char **argv) {
578 VpxVideoWriter *outfile[VPX_TS_MAX_LAYERS] = { NULL };
579 vpx_codec_ctx_t codec;
580 vpx_codec_enc_cfg_t cfg;
581 int frame_cnt = 0;
582 vpx_image_t raw;
583 vpx_codec_err_t res;
584 unsigned int width;
585 unsigned int height;
586 uint32_t error_resilient = 0;
587 int speed;
588 int frame_avail;
589 int got_data;
590 int flags = 0;
591 unsigned int i;
592 int pts = 0; // PTS starts at 0.
593 int frame_duration = 1; // 1 timebase tick per frame.
594 int layering_mode = 0;
595 int layer_flags[VPX_TS_MAX_PERIODICITY] = { 0 };
596 int flag_periodicity = 1;
597#if ROI_MAP
598 vpx_roi_map_t roi;
599#endif
600 vpx_svc_layer_id_t layer_id;
601 const VpxInterface *encoder = NULL;
602 struct VpxInputContext input_ctx;
603 struct RateControlMetrics rc;
604 int64_t cx_time = 0;
605 const int min_args_base = 13;
606#if CONFIG_VP9_HIGHBITDEPTH
607 vpx_bit_depth_t bit_depth = VPX_BITS_8;
608 int input_bit_depth = 8;
609 const int min_args = min_args_base + 1;
610#else
611 const int min_args = min_args_base;
612#endif // CONFIG_VP9_HIGHBITDEPTH
613 double sum_bitrate = 0.0;
614 double sum_bitrate2 = 0.0;
615 double framerate = 30.0;
616
617 zero(rc.layer_target_bitrate);
618 memset(&layer_id, 0, sizeof(vpx_svc_layer_id_t));
619 memset(&input_ctx, 0, sizeof(input_ctx));
620 /* Setup default input stream settings */
621 input_ctx.framerate.numerator = 30;
622 input_ctx.framerate.denominator = 1;
623 input_ctx.only_i420 = 1;
624 input_ctx.bit_depth = 0;
625
626 exec_name = argv[0];
627 // Check usage and arguments.
628 if (argc < min_args) {
629#if CONFIG_VP9_HIGHBITDEPTH
630 die("Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> "
631 "<rate_num> <rate_den> <speed> <frame_drop_threshold> "
632 "<error_resilient> <threads> <mode> "
633 "<Rate_0> ... <Rate_nlayers-1> <bit-depth> \n",
634 argv[0]);
635#else
636 die("Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> "
637 "<rate_num> <rate_den> <speed> <frame_drop_threshold> "
638 "<error_resilient> <threads> <mode> "
639 "<Rate_0> ... <Rate_nlayers-1> \n",
640 argv[0]);
641#endif // CONFIG_VP9_HIGHBITDEPTH
642 }
643
644 encoder = get_vpx_encoder_by_name(argv[3]);
645 if (!encoder) die("Unsupported codec.");
646
647 printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
648
649 width = (unsigned int)strtoul(argv[4], NULL, 0);
650 height = (unsigned int)strtoul(argv[5], NULL, 0);
651 if (width < 16 || width % 2 || height < 16 || height % 2) {
652 die("Invalid resolution: %d x %d", width, height);
653 }
654
655 layering_mode = (int)strtol(argv[12], NULL, 0);
656 if (layering_mode < 0 || layering_mode > 13) {
657 die("Invalid layering mode (0..12) %s", argv[12]);
658 }
659
660 if (argc != min_args + mode_to_num_layers[layering_mode]) {
661 die("Invalid number of arguments");
662 }
663
664 input_ctx.filename = argv[1];
665 open_input_file(&input_ctx);
666
667#if CONFIG_VP9_HIGHBITDEPTH
668 switch (strtol(argv[argc - 1], NULL, 0)) {
669 case 8:
670 bit_depth = VPX_BITS_8;
671 input_bit_depth = 8;
672 break;
673 case 10:
674 bit_depth = VPX_BITS_10;
675 input_bit_depth = 10;
676 break;
677 case 12:
678 bit_depth = VPX_BITS_12;
679 input_bit_depth = 12;
680 break;
681 default: die("Invalid bit depth (8, 10, 12) %s", argv[argc - 1]);
682 }
683
684 // Y4M reader has its own allocation.
685 if (input_ctx.file_type != FILE_TYPE_Y4M) {
686 if (!vpx_img_alloc(
687 &raw,
688 bit_depth == VPX_BITS_8 ? VPX_IMG_FMT_I420 : VPX_IMG_FMT_I42016,
689 width, height, 32)) {
690 die("Failed to allocate image", width, height);
691 }
692 }
693#else
694 // Y4M reader has its own allocation.
695 if (input_ctx.file_type != FILE_TYPE_Y4M) {
696 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 32)) {
697 die("Failed to allocate image", width, height);
698 }
699 }
700#endif // CONFIG_VP9_HIGHBITDEPTH
701
702 // Populate encoder configuration.
703 res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
704 if (res) {
705 printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
706 return EXIT_FAILURE;
707 }
708
709 // Update the default configuration with our settings.
710 cfg.g_w = width;
711 cfg.g_h = height;
712
713#if CONFIG_VP9_HIGHBITDEPTH
714 if (bit_depth != VPX_BITS_8) {
715 cfg.g_bit_depth = bit_depth;
716 cfg.g_input_bit_depth = input_bit_depth;
717 cfg.g_profile = 2;
718 }
719#endif // CONFIG_VP9_HIGHBITDEPTH
720
721 // Timebase format e.g. 30fps: numerator=1, demoninator = 30.
722 cfg.g_timebase.num = (int)strtol(argv[6], NULL, 0);
723 cfg.g_timebase.den = (int)strtol(argv[7], NULL, 0);
724
725 speed = (int)strtol(argv[8], NULL, 0);
726 if (speed < 0) {
727 die("Invalid speed setting: must be positive");
728 }
729 if (strncmp(encoder->name, "vp9", 3) == 0 && speed > 9) {
730 warn("Mapping speed %d to speed 9.\n", speed);
731 }
732
733 for (i = min_args_base;
734 (int)i < min_args_base + mode_to_num_layers[layering_mode]; ++i) {
735 rc.layer_target_bitrate[i - 13] = (int)strtol(argv[i], NULL, 0);
736 if (strncmp(encoder->name, "vp8", 3) == 0)
737 cfg.ts_target_bitrate[i - 13] = rc.layer_target_bitrate[i - 13];
738 else if (strncmp(encoder->name, "vp9", 3) == 0)
739 cfg.layer_target_bitrate[i - 13] = rc.layer_target_bitrate[i - 13];
740 }
741
742 // Real time parameters.
743 cfg.rc_dropframe_thresh = (unsigned int)strtoul(argv[9], NULL, 0);
744 cfg.rc_end_usage = VPX_CBR;
745 cfg.rc_min_quantizer = 2;
746 cfg.rc_max_quantizer = 56;
747 if (strncmp(encoder->name, "vp9", 3) == 0) cfg.rc_max_quantizer = 52;
748 cfg.rc_undershoot_pct = 50;
749 cfg.rc_overshoot_pct = 50;
750 cfg.rc_buf_initial_sz = 600;
751 cfg.rc_buf_optimal_sz = 600;
752 cfg.rc_buf_sz = 1000;
753
754 // Disable dynamic resizing by default.
755 cfg.rc_resize_allowed = 0;
756
757 // Use 1 thread as default.
758 cfg.g_threads = (unsigned int)strtoul(argv[11], NULL, 0);
759
760 error_resilient = (uint32_t)strtoul(argv[10], NULL, 0);
761 if (error_resilient != 0 && error_resilient != 1) {
762 die("Invalid value for error resilient (0, 1): %d.", error_resilient);
763 }
764 // Enable error resilient mode.
765 cfg.g_error_resilient = error_resilient;
766 cfg.g_lag_in_frames = 0;
767 cfg.kf_mode = VPX_KF_AUTO;
768
769 // Disable automatic keyframe placement.
770 cfg.kf_min_dist = cfg.kf_max_dist = 3000;
771
772 cfg.temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_BYPASS;
773
774 set_temporal_layer_pattern(layering_mode, &cfg, layer_flags,
775 &flag_periodicity);
776
777 set_rate_control_metrics(&rc, &cfg);
778
779 if (input_ctx.file_type == FILE_TYPE_Y4M) {
780 if (input_ctx.width != cfg.g_w || input_ctx.height != cfg.g_h) {
781 die("Incorrect width or height: %d x %d", cfg.g_w, cfg.g_h);
782 }
783 if (input_ctx.framerate.numerator != cfg.g_timebase.den ||
784 input_ctx.framerate.denominator != cfg.g_timebase.num) {
785 die("Incorrect framerate: numerator %d denominator %d",
786 cfg.g_timebase.num, cfg.g_timebase.den);
787 }
788 }
789
790 framerate = cfg.g_timebase.den / cfg.g_timebase.num;
791 // Open an output file for each stream.
792 for (i = 0; i < cfg.ts_number_layers; ++i) {
793 char file_name[PATH_MAX];
794 VpxVideoInfo info;
795 info.codec_fourcc = encoder->fourcc;
796 info.frame_width = cfg.g_w;
797 info.frame_height = cfg.g_h;
798 info.time_base.numerator = cfg.g_timebase.num;
799 info.time_base.denominator = cfg.g_timebase.den;
800
801 snprintf(file_name, sizeof(file_name), "%s_%d.ivf", argv[2], i);
802 outfile[i] = vpx_video_writer_open(file_name, kContainerIVF, &info);
803 if (!outfile[i]) die("Failed to open %s for writing", file_name);
804
805 assert(outfile[i] != NULL);
806 }
807 // No spatial layers in this encoder.
808 cfg.ss_number_layers = 1;
809
810// Initialize codec.
811#if CONFIG_VP9_HIGHBITDEPTH
812 if (vpx_codec_enc_init(
813 &codec, encoder->codec_interface(), &cfg,
814 bit_depth == VPX_BITS_8 ? 0 : VPX_CODEC_USE_HIGHBITDEPTH))
815#else
816 if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
817#endif // CONFIG_VP9_HIGHBITDEPTH
818 die("Failed to initialize encoder");
819
820 if (strncmp(encoder->name, "vp8", 3) == 0) {
821 vpx_codec_control(&codec, VP8E_SET_CPUUSED, -speed);
822 vpx_codec_control(&codec, VP8E_SET_NOISE_SENSITIVITY, kVp8DenoiserOff);
823 vpx_codec_control(&codec, VP8E_SET_STATIC_THRESHOLD, 1);
824 vpx_codec_control(&codec, VP8E_SET_GF_CBR_BOOST_PCT, 0);
825#if ROI_MAP
826 set_roi_map(encoder->name, &cfg, &roi);
827 if (vpx_codec_control(&codec, VP8E_SET_ROI_MAP, &roi))
828 die_codec(&codec, "Failed to set ROI map");
829#endif
830
831 } else if (strncmp(encoder->name, "vp9", 3) == 0) {
832 vpx_svc_extra_cfg_t svc_params;
833 memset(&svc_params, 0, sizeof(svc_params));
834 vpx_codec_control(&codec, VP9E_SET_POSTENCODE_DROP, 0);
835 vpx_codec_control(&codec, VP9E_SET_DISABLE_OVERSHOOT_MAXQ_CBR, 0);
836 vpx_codec_control(&codec, VP8E_SET_CPUUSED, speed);
837 vpx_codec_control(&codec, VP9E_SET_AQ_MODE, 3);
838 vpx_codec_control(&codec, VP9E_SET_GF_CBR_BOOST_PCT, 0);
839 vpx_codec_control(&codec, VP9E_SET_FRAME_PARALLEL_DECODING, 0);
840 vpx_codec_control(&codec, VP9E_SET_FRAME_PERIODIC_BOOST, 0);
841 vpx_codec_control(&codec, VP9E_SET_NOISE_SENSITIVITY, kVp9DenoiserOff);
842 vpx_codec_control(&codec, VP8E_SET_STATIC_THRESHOLD, 1);
843 vpx_codec_control(&codec, VP9E_SET_TUNE_CONTENT, 0);
844 vpx_codec_control(&codec, VP9E_SET_TILE_COLUMNS, get_msb(cfg.g_threads));
845 vpx_codec_control(&codec, VP9E_SET_DISABLE_LOOPFILTER, 0);
846#if ROI_MAP
847 set_roi_map(encoder->name, &cfg, &roi);
848 if (vpx_codec_control(&codec, VP9E_SET_ROI_MAP, &roi))
849 die_codec(&codec, "Failed to set ROI map");
850 vpx_codec_control(&codec, VP9E_SET_AQ_MODE, 0);
851#endif
852 if (cfg.g_threads > 1)
853 vpx_codec_control(&codec, VP9E_SET_ROW_MT, 1);
854 else
855 vpx_codec_control(&codec, VP9E_SET_ROW_MT, 0);
856 if (vpx_codec_control(&codec, VP9E_SET_SVC, layering_mode > 0 ? 1 : 0))
857 die_codec(&codec, "Failed to set SVC");
858 for (i = 0; i < cfg.ts_number_layers; ++i) {
859 svc_params.max_quantizers[i] = cfg.rc_max_quantizer;
860 svc_params.min_quantizers[i] = cfg.rc_min_quantizer;
861 }
862 svc_params.scaling_factor_num[0] = cfg.g_h;
863 svc_params.scaling_factor_den[0] = cfg.g_h;
864 vpx_codec_control(&codec, VP9E_SET_SVC_PARAMETERS, &svc_params);
865 }
866 if (strncmp(encoder->name, "vp8", 3) == 0) {
867 vpx_codec_control(&codec, VP8E_SET_SCREEN_CONTENT_MODE, 0);
868 }
869 vpx_codec_control(&codec, VP8E_SET_TOKEN_PARTITIONS, 1);
870 // This controls the maximum target size of the key frame.
871 // For generating smaller key frames, use a smaller max_intra_size_pct
872 // value, like 100 or 200.
873 {
874 const int max_intra_size_pct = 1000;
875 vpx_codec_control(&codec, VP8E_SET_MAX_INTRA_BITRATE_PCT,
876 max_intra_size_pct);
877 }
878
879 frame_avail = 1;
880 while (frame_avail || got_data) {
881 struct vpx_usec_timer timer;
882 vpx_codec_iter_t iter = NULL;
883 const vpx_codec_cx_pkt_t *pkt;
884 // Update the temporal layer_id. No spatial layers in this test.
885 layer_id.spatial_layer_id = 0;
886 layer_id.temporal_layer_id =
887 cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
888 layer_id.temporal_layer_id_per_spatial[0] = layer_id.temporal_layer_id;
889 if (strncmp(encoder->name, "vp9", 3) == 0) {
890 vpx_codec_control(&codec, VP9E_SET_SVC_LAYER_ID, &layer_id);
891 } else if (strncmp(encoder->name, "vp8", 3) == 0) {
892 vpx_codec_control(&codec, VP8E_SET_TEMPORAL_LAYER_ID,
893 layer_id.temporal_layer_id);
894 }
895 flags = layer_flags[frame_cnt % flag_periodicity];
896 if (layering_mode == 0) flags = 0;
897 frame_avail = read_frame(&input_ctx, &raw);
898 if (frame_avail) ++rc.layer_input_frames[layer_id.temporal_layer_id];
899 vpx_usec_timer_start(&timer);
900 if (vpx_codec_encode(&codec, frame_avail ? &raw : NULL, pts, 1, flags,
901 VPX_DL_REALTIME)) {
902 die_codec(&codec, "Failed to encode frame");
903 }
904 vpx_usec_timer_mark(&timer);
905 cx_time += vpx_usec_timer_elapsed(&timer);
906 // Reset KF flag.
907 if (layering_mode != 7) {
908 layer_flags[0] &= ~VPX_EFLAG_FORCE_KF;
909 }
910 got_data = 0;
911 while ((pkt = vpx_codec_get_cx_data(&codec, &iter))) {
912 got_data = 1;
913 switch (pkt->kind) {
914 case VPX_CODEC_CX_FRAME_PKT:
915 for (i = cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
916 i < cfg.ts_number_layers; ++i) {
917 vpx_video_writer_write_frame(outfile[i], pkt->data.frame.buf,
918 pkt->data.frame.sz, pts);
919 ++rc.layer_tot_enc_frames[i];
920 rc.layer_encoding_bitrate[i] += 8.0 * pkt->data.frame.sz;
921 // Keep count of rate control stats per layer (for non-key frames).
922 if (i == cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity] &&
923 !(pkt->data.frame.flags & VPX_FRAME_IS_KEY)) {
924 rc.layer_avg_frame_size[i] += 8.0 * pkt->data.frame.sz;
925 rc.layer_avg_rate_mismatch[i] +=
926 fabs(8.0 * pkt->data.frame.sz - rc.layer_pfb[i]) /
927 rc.layer_pfb[i];
928 ++rc.layer_enc_frames[i];
929 }
930 }
931 // Update for short-time encoding bitrate states, for moving window
932 // of size rc->window, shifted by rc->window / 2.
933 // Ignore first window segment, due to key frame.
934 if (rc.window_size == 0) rc.window_size = 15;
935 if (frame_cnt > rc.window_size) {
936 sum_bitrate += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
937 if (frame_cnt % rc.window_size == 0) {
938 rc.window_count += 1;
939 rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
940 rc.variance_st_encoding_bitrate +=
941 (sum_bitrate / rc.window_size) *
942 (sum_bitrate / rc.window_size);
943 sum_bitrate = 0.0;
944 }
945 }
946 // Second shifted window.
947 if (frame_cnt > rc.window_size + rc.window_size / 2) {
948 sum_bitrate2 += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
949 if (frame_cnt > 2 * rc.window_size &&
950 frame_cnt % rc.window_size == 0) {
951 rc.window_count += 1;
952 rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
953 rc.variance_st_encoding_bitrate +=
954 (sum_bitrate2 / rc.window_size) *
955 (sum_bitrate2 / rc.window_size);
956 sum_bitrate2 = 0.0;
957 }
958 }
959 break;
960 default: break;
961 }
962 }
963 ++frame_cnt;
964 pts += frame_duration;
965 }
966 close_input_file(&input_ctx);
967 printout_rate_control_summary(&rc, &cfg, frame_cnt);
968 printf("\n");
969 printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
970 frame_cnt, 1000 * (float)cx_time / (double)(frame_cnt * 1000000),
971 1000000 * (double)frame_cnt / (double)cx_time);
972
973 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
974
975 // Try to rewrite the output file headers with the actual frame count.
976 for (i = 0; i < cfg.ts_number_layers; ++i) vpx_video_writer_close(outfile[i]);
977
978 if (input_ctx.file_type != FILE_TYPE_Y4M) {
979 vpx_img_free(&raw);
980 }
981
982#if ROI_MAP
983 free(roi.roi_map);
984#endif
985 return EXIT_SUCCESS;
986}
vpx_codec_err_to_string
const char * vpx_codec_err_to_string(vpx_codec_err_t err)
Convert error number to printable string.
vpx_codec_destroy
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
vpx_codec_iter_t
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:190
vpx_codec_iface_name
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
vpx_bit_depth_t
enum vpx_bit_depth vpx_bit_depth_t
Bit depth for codecThis enumeration determines the bit depth of the codec.
vpx_codec_control
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition: vpx_codec.h:407
vpx_codec_err_t
vpx_codec_err_t
Algorithm return codes.
Definition: vpx_codec.h:93
VPX_BITS_8
@ VPX_BITS_8
Definition: vpx_codec.h:221
VPX_BITS_12
@ VPX_BITS_12
Definition: vpx_codec.h:223
VPX_BITS_10
@ VPX_BITS_10
Definition: vpx_codec.h:222
VPX_DL_REALTIME
#define VPX_DL_REALTIME
deadline parameter analogous to VPx REALTIME mode.
Definition: vpx_encoder.h:978
VPX_TS_MAX_LAYERS
#define VPX_TS_MAX_LAYERS
Definition: vpx_encoder.h:41
vpx_codec_enc_init
#define vpx_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_enc_init_ver()
Definition: vpx_encoder.h:889
VPX_EFLAG_FORCE_KF
#define VPX_EFLAG_FORCE_KF
Definition: vpx_encoder.h:262
vpx_codec_get_cx_data
const vpx_codec_cx_pkt_t * vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Encoded data iterator.
VPX_TS_MAX_PERIODICITY
#define VPX_TS_MAX_PERIODICITY
Definition: vpx_encoder.h:38
VPX_CODEC_USE_HIGHBITDEPTH
#define VPX_CODEC_USE_HIGHBITDEPTH
Definition: vpx_encoder.h:92
VPX_MAX_LAYERS
#define VPX_MAX_LAYERS
Definition: vpx_encoder.h:44
VPX_FRAME_IS_KEY
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:118
vpx_codec_enc_config_default
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, unsigned int usage)
Get a default configuration.
vpx_codec_encode
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.
VPX_CODEC_CX_FRAME_PKT
@ VPX_CODEC_CX_FRAME_PKT
Definition: vpx_encoder.h:149
VPX_KF_AUTO
@ VPX_KF_AUTO
Definition: vpx_encoder.h:250
VPX_CBR
@ VPX_CBR
Definition: vpx_encoder.h:235
VP8_EFLAG_NO_UPD_ARF
#define VP8_EFLAG_NO_UPD_ARF
Don't update the alternate reference frame.
Definition: vp8cx.h:96
VP8_EFLAG_NO_UPD_ENTROPY
#define VP8_EFLAG_NO_UPD_ENTROPY
Disable entropy update.
Definition: vp8cx.h:117
VP8_EFLAG_NO_UPD_LAST
#define VP8_EFLAG_NO_UPD_LAST
Don't update the last frame.
Definition: vp8cx.h:82
VP8_EFLAG_NO_REF_ARF
#define VP8_EFLAG_NO_REF_ARF
Don't reference the alternate reference frame.
Definition: vp8cx.h:75
VP8_EFLAG_NO_UPD_GF
#define VP8_EFLAG_NO_UPD_GF
Don't update the golden frame.
Definition: vp8cx.h:89
VP8_EFLAG_NO_REF_GF
#define VP8_EFLAG_NO_REF_GF
Don't reference the golden frame.
Definition: vp8cx.h:67
VP8_EFLAG_NO_REF_LAST
#define VP8_EFLAG_NO_REF_LAST
Don't reference the last frame.
Definition: vp8cx.h:59
VP9E_SET_FRAME_PERIODIC_BOOST
@ VP9E_SET_FRAME_PERIODIC_BOOST
Codec control function to enable/disable periodic Q boost.
Definition: vp8cx.h:414
VP9E_SET_SVC_LAYER_ID
@ VP9E_SET_SVC_LAYER_ID
Codec control function to set svc layer for spatial and temporal.
Definition: vp8cx.h:454
VP8E_SET_MAX_INTRA_BITRATE_PCT
@ VP8E_SET_MAX_INTRA_BITRATE_PCT
Codec control function to set Max data rate for Intra frames.
Definition: vp8cx.h:258
VP9E_SET_ROI_MAP
@ VP9E_SET_ROI_MAP
Codec control function to pass an ROI map to encoder.
Definition: vp8cx.h:437
VP8E_SET_ROI_MAP
@ VP8E_SET_ROI_MAP
Codec control function to pass an ROI map to encoder.
Definition: vp8cx.h:131
VP9E_SET_AQ_MODE
@ VP9E_SET_AQ_MODE
Codec control function to set adaptive quantization mode.
Definition: vp8cx.h:399
VP8E_SET_NOISE_SENSITIVITY
@ VP8E_SET_NOISE_SENSITIVITY
control function to set noise sensitivity
Definition: vp8cx.h:174
VP8E_SET_TOKEN_PARTITIONS
@ VP8E_SET_TOKEN_PARTITIONS
Codec control function to set the number of token partitions.
Definition: vp8cx.h:195
VP9E_SET_POSTENCODE_DROP
@ VP9E_SET_POSTENCODE_DROP
Codec control function to enable postencode frame drop.
Definition: vp8cx.h:679
VP9E_SET_DISABLE_OVERSHOOT_MAXQ_CBR
@ VP9E_SET_DISABLE_OVERSHOOT_MAXQ_CBR
Codec control function to disable increase Q on overshoot in CBR.
Definition: vp8cx.h:695
VP9E_SET_SVC_PARAMETERS
@ VP9E_SET_SVC_PARAMETERS
Codec control function to set parameters for SVC.
Definition: vp8cx.h:445
VP9E_SET_FRAME_PARALLEL_DECODING
@ VP9E_SET_FRAME_PARALLEL_DECODING
Codec control function to enable frame parallel decoding feature.
Definition: vp8cx.h:386
VP8E_SET_GF_CBR_BOOST_PCT
@ VP8E_SET_GF_CBR_BOOST_PCT
Boost percentage for Golden Frame in CBR mode.
Definition: vp8cx.h:602
VP9E_SET_TUNE_CONTENT
@ VP9E_SET_TUNE_CONTENT
Codec control function to set content type.
Definition: vp8cx.h:464
VP9E_SET_SVC
@ VP9E_SET_SVC
Codec control function to turn on/off SVC in encoder.
Definition: vp8cx.h:431
VP9E_SET_ROW_MT
@ VP9E_SET_ROW_MT
Codec control function to set row level multi-threading.
Definition: vp8cx.h:571
VP8E_SET_CPUUSED
@ VP8E_SET_CPUUSED
Codec control function to set encoder internal speed settings.
Definition: vp8cx.h:156
VP8E_SET_TEMPORAL_LAYER_ID
@ VP8E_SET_TEMPORAL_LAYER_ID
Codec control function to set the temporal layer id.
Definition: vp8cx.h:305
VP9E_SET_TILE_COLUMNS
@ VP9E_SET_TILE_COLUMNS
Codec control function to set number of tile columns.
Definition: vp8cx.h:352
VP8E_SET_STATIC_THRESHOLD
@ VP8E_SET_STATIC_THRESHOLD
Codec control function to set the threshold for MBs treated static.
Definition: vp8cx.h:189
VP8E_SET_SCREEN_CONTENT_MODE
@ VP8E_SET_SCREEN_CONTENT_MODE
Codec control function to set encoder screen content mode.
Definition: vp8cx.h:313
VP9E_SET_DISABLE_LOOPFILTER
@ VP9E_SET_DISABLE_LOOPFILTER
Codec control function to disable loopfilter.
Definition: vp8cx.h:704
VP9E_SET_NOISE_SENSITIVITY
@ VP9E_SET_NOISE_SENSITIVITY
Codec control function to set noise sensitivity.
Definition: vp8cx.h:422
VP9E_SET_GF_CBR_BOOST_PCT
@ VP9E_SET_GF_CBR_BOOST_PCT
Boost percentage for Golden Frame in CBR mode.
Definition: vp8cx.h:294
VP9E_TEMPORAL_LAYERING_MODE_BYPASS
@ VP9E_TEMPORAL_LAYERING_MODE_BYPASS
Bypass mode. Used when application needs to control temporal layering. This will only work when the n...
Definition: vp8cx.h:774
vpx_codec_ctx
Codec context structure.
Definition: vpx_codec.h:200
vpx_codec_cx_pkt
Encoder output packet.
Definition: vpx_encoder.h:161
vpx_codec_cx_pkt::flags
vpx_codec_frame_flags_t flags
Definition: vpx_encoder.h:171
vpx_codec_cx_pkt::kind
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:162
vpx_codec_cx_pkt::frame
struct vpx_codec_cx_pkt::@1::@2 frame
vpx_codec_cx_pkt::sz
size_t sz
Definition: vpx_encoder.h:166
vpx_codec_cx_pkt::buf
void * buf
Definition: vpx_encoder.h:165
vpx_codec_cx_pkt::data
union vpx_codec_cx_pkt::@1 data
vpx_codec_enc_cfg
Encoder configuration structure.
Definition: vpx_encoder.h:270
vpx_codec_enc_cfg::rc_resize_allowed
unsigned int rc_resize_allowed
Enable/disable spatial resampling, if supported by the codec.
Definition: vpx_encoder.h:402
vpx_codec_enc_cfg::temporal_layering_mode
int temporal_layering_mode
Temporal layering mode indicating which temporal layering scheme to use.
Definition: vpx_encoder.h:695
vpx_codec_enc_cfg::kf_min_dist
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: vpx_encoder.h:607
vpx_codec_enc_cfg::rc_min_quantizer
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition: vpx_encoder.h:475
vpx_codec_enc_cfg::ts_number_layers
unsigned int ts_number_layers
Number of temporal coding layers.
Definition: vpx_encoder.h:646
vpx_codec_enc_cfg::ss_number_layers
unsigned int ss_number_layers
Number of spatial coding layers.
Definition: vpx_encoder.h:626
vpx_codec_enc_cfg::g_profile
unsigned int g_profile
Bitstream profile to use.
Definition: vpx_encoder.h:297
vpx_codec_enc_cfg::layer_target_bitrate
unsigned int layer_target_bitrate[12]
Target bitrate for each spatial/temporal layer.
Definition: vpx_encoder.h:686
vpx_codec_enc_cfg::g_h
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:315
vpx_codec_enc_cfg::kf_mode
enum vpx_kf_mode kf_mode
Keyframe placement mode.
Definition: vpx_encoder.h:598
vpx_codec_enc_cfg::ts_layer_id
unsigned int ts_layer_id[16]
Template defining the membership of frames to temporal layers.
Definition: vpx_encoder.h:678
vpx_codec_enc_cfg::g_error_resilient
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: vpx_encoder.h:353
vpx_codec_enc_cfg::ts_periodicity
unsigned int ts_periodicity
Length of the sequence defining frame temporal layer membership.
Definition: vpx_encoder.h:669
vpx_codec_enc_cfg::rc_overshoot_pct
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition: vpx_encoder.h:518
vpx_codec_enc_cfg::g_w
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:306
vpx_codec_enc_cfg::rc_buf_sz
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition: vpx_encoder.h:533
vpx_codec_enc_cfg::rc_dropframe_thresh
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: vpx_encoder.h:393
vpx_codec_enc_cfg::g_timebase
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:345
vpx_codec_enc_cfg::rc_max_quantizer
unsigned int rc_max_quantizer
Maximum (Worst Quality) Quantizer.
Definition: vpx_encoder.h:484
vpx_codec_enc_cfg::g_lag_in_frames
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: vpx_encoder.h:374
vpx_codec_enc_cfg::rc_end_usage
enum vpx_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: vpx_encoder.h:442
vpx_codec_enc_cfg::rc_buf_initial_sz
unsigned int rc_buf_initial_sz
Decoder Buffer Initial Size.
Definition: vpx_encoder.h:542
vpx_codec_enc_cfg::g_bit_depth
vpx_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition: vpx_encoder.h:323
vpx_codec_enc_cfg::rc_buf_optimal_sz
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition: vpx_encoder.h:551
vpx_codec_enc_cfg::rc_target_bitrate
unsigned int rc_target_bitrate
Target data rate.
Definition: vpx_encoder.h:462
vpx_codec_enc_cfg::ts_target_bitrate
unsigned int ts_target_bitrate[5]
Target bitrate for each temporal layer.
Definition: vpx_encoder.h:653
vpx_codec_enc_cfg::g_input_bit_depth
unsigned int g_input_bit_depth
Bit-depth of the input frames.
Definition: vpx_encoder.h:331
vpx_codec_enc_cfg::rc_undershoot_pct
unsigned int rc_undershoot_pct
Rate control adaptation undershoot control.
Definition: vpx_encoder.h:503
vpx_codec_enc_cfg::ts_rate_decimator
unsigned int ts_rate_decimator[5]
Frame rate decimation factor for each temporal layer.
Definition: vpx_encoder.h:660
vpx_codec_enc_cfg::kf_max_dist
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: vpx_encoder.h:616
vpx_codec_enc_cfg::g_threads
unsigned int g_threads
Maximum number of threads to use.
Definition: vpx_encoder.h:287
vpx_image
Image Descriptor.
Definition: vpx_image.h:72
vpx_rational::den
int den
Definition: vpx_encoder.h:222
vpx_rational::num
int num
Definition: vpx_encoder.h:221
vpx_roi_map
vpx region of interest map
Definition: vp8cx.h:791
vpx_roi_map::skip
int skip[8]
Definition: vp8cx.h:803
vpx_roi_map::static_threshold
unsigned int static_threshold[4]
Definition: vp8cx.h:806
vpx_roi_map::rows
unsigned int rows
Definition: vp8cx.h:797
vpx_roi_map::cols
unsigned int cols
Definition: vp8cx.h:798
vpx_roi_map::ref_frame
int ref_frame[8]
Definition: vp8cx.h:804
vpx_roi_map::delta_q
int delta_q[8]
Definition: vp8cx.h:800
vpx_roi_map::roi_map
unsigned char * roi_map
Definition: vp8cx.h:796
vpx_roi_map::delta_lf
int delta_lf[8]
Definition: vp8cx.h:801
vpx_svc_layer_id
vp9 svc layer parameters
Definition: vp8cx.h:868
vpx_svc_layer_id::temporal_layer_id
int temporal_layer_id
Definition: vp8cx.h:871
vpx_svc_layer_id::temporal_layer_id_per_spatial
int temporal_layer_id_per_spatial[5]
Definition: vp8cx.h:872
vpx_svc_layer_id::spatial_layer_id
int spatial_layer_id
Definition: vp8cx.h:869
vpx_svc_parameters
vp9 svc extra configure parameters
Definition: vpx_encoder.h:848
vpx_svc_parameters::min_quantizers
int min_quantizers[12]
Definition: vpx_encoder.h:850
vpx_svc_parameters::scaling_factor_num
int scaling_factor_num[12]
Definition: vpx_encoder.h:851
vpx_svc_parameters::max_quantizers
int max_quantizers[12]
Definition: vpx_encoder.h:849
vpx_svc_parameters::scaling_factor_den
int scaling_factor_den[12]
Definition: vpx_encoder.h:852
vp8cx.h
Provides definitions for using VP8 or VP9 encoder algorithm within the vpx Codec Interface.
vpx_encoder.h
Describes the encoder algorithm interface to applications.
vpx_img_alloc
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.
VPX_IMG_FMT_I42016
@ VPX_IMG_FMT_I42016
Definition: vpx_image.h:47
VPX_IMG_FMT_I420
@ VPX_IMG_FMT_I420
Definition: vpx_image.h:42
vpx_img_free
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.