WebM Codec SDK: vpx_temporal_svc_encoder

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