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
244 static void set_roi_skip_map(vpx_codec_enc_cfg_t *cfg, vpx_roi_map_t *roi,
245 int *skip_map, int *prev_mask_map, int frame_num) {
246 const int block_size = 8;
247 unsigned int i, j;
248 roi->rows = (cfg->g_h + block_size - 1) / block_size;
249 roi->cols = (cfg->g_w + block_size - 1) / block_size;
250 zero(roi->skip);
251 zero(roi->delta_q);
252 zero(roi->delta_lf);
253 memset(roi->ref_frame, -1, sizeof(roi->ref_frame));
254 roi->ref_frame[1] = 1;
255 // Use segment 3 for skip.
256 roi->skip[3] = 1;
257 roi->roi_map =
258 (uint8_t *)calloc(roi->rows * roi->cols, sizeof(*roi->roi_map));
259 for (i = 0; i < roi->rows; ++i) {
260 for (j = 0; j < roi->cols; ++j) {
261 const int idx = i * roi->cols + j;
262 // Use segment 3 for skip.
263 // prev_mask_map keeps track of blocks that have been stably on segment 3
264 // for the past 10 frames. Only skip when the block is on segment 3 in
265 // both current map and prev_mask_map.
266 if (skip_map[idx] == 1 && prev_mask_map[idx] == 1) roi->roi_map[idx] = 3;
267 // Reset it every 10 frames so it doesn't propagate for too many frames.
268 if (frame_num % 10 == 0)
269 prev_mask_map[idx] = skip_map[idx];
270 else if (prev_mask_map[idx] == 1 && skip_map[idx] == 0)
271 prev_mask_map[idx] = 0;
272 }
273 }
274}
275#endif
276
277// Temporal scaling parameters:
278// NOTE: The 3 prediction frames cannot be used interchangeably due to
279// differences in the way they are handled throughout the code. The
280// frames should be allocated to layers in the order LAST, GF, ARF.
281// Other combinations work, but may produce slightly inferior results.
282 static void set_temporal_layer_pattern(int layering_mode,
283 vpx_codec_enc_cfg_t *cfg,
284 int *layer_flags,
285 int *flag_periodicity) {
286 switch (layering_mode) {
287 case 0: {
288 // 1-layer.
289 int ids[1] = { 0 };
290 cfg->ts_periodicity = 1;
291 *flag_periodicity = 1;
292 cfg->ts_number_layers = 1;
293 cfg->ts_rate_decimator[0] = 1;
294 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
295 // Update L only.
296 layer_flags[0] =
297 VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
298 break;
299 }
300 case 1: {
301 // 2-layers, 2-frame period.
302 int ids[2] = { 0, 1 };
303 cfg->ts_periodicity = 2;
304 *flag_periodicity = 2;
305 cfg->ts_number_layers = 2;
306 cfg->ts_rate_decimator[0] = 2;
307 cfg->ts_rate_decimator[1] = 1;
308 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
309#if 1
310 // 0=L, 1=GF, Intra-layer prediction enabled.
311 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF |
312 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF |
313 VP8_EFLAG_NO_REF_ARF;
314 layer_flags[1] =
315 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_REF_ARF;
316#else
317 // 0=L, 1=GF, Intra-layer prediction disabled.
318 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_GF |
319 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF |
320 VP8_EFLAG_NO_REF_ARF;
321 layer_flags[1] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
322 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_LAST;
323#endif
324 break;
325 }
326 case 2: {
327 // 2-layers, 3-frame period.
328 int ids[3] = { 0, 1, 1 };
329 cfg->ts_periodicity = 3;
330 *flag_periodicity = 3;
331 cfg->ts_number_layers = 2;
332 cfg->ts_rate_decimator[0] = 3;
333 cfg->ts_rate_decimator[1] = 1;
334 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
335 // 0=L, 1=GF, Intra-layer prediction enabled.
336 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
337 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
338 VP8_EFLAG_NO_UPD_ARF;
339 layer_flags[1] = layer_flags[2] =
340 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_ARF |
341 VP8_EFLAG_NO_UPD_LAST;
342 break;
343 }
344 case 3: {
345 // 3-layers, 6-frame period.
346 int ids[6] = { 0, 2, 2, 1, 2, 2 };
347 cfg->ts_periodicity = 6;
348 *flag_periodicity = 6;
349 cfg->ts_number_layers = 3;
350 cfg->ts_rate_decimator[0] = 6;
351 cfg->ts_rate_decimator[1] = 3;
352 cfg->ts_rate_decimator[2] = 1;
353 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
354 // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
355 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
356 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
357 VP8_EFLAG_NO_UPD_ARF;
358 layer_flags[3] =
359 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
360 layer_flags[1] = layer_flags[2] = layer_flags[4] = layer_flags[5] =
361 VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_LAST;
362 break;
363 }
364 case 4: {
365 // 3-layers, 4-frame period.
366 int ids[4] = { 0, 2, 1, 2 };
367 cfg->ts_periodicity = 4;
368 *flag_periodicity = 4;
369 cfg->ts_number_layers = 3;
370 cfg->ts_rate_decimator[0] = 4;
371 cfg->ts_rate_decimator[1] = 2;
372 cfg->ts_rate_decimator[2] = 1;
373 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
374 // 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
375 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
376 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
377 VP8_EFLAG_NO_UPD_ARF;
378 layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
379 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
380 layer_flags[1] = layer_flags[3] =
381 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
382 VP8_EFLAG_NO_UPD_ARF;
383 break;
384 }
385 case 5: {
386 // 3-layers, 4-frame period.
387 int ids[4] = { 0, 2, 1, 2 };
388 cfg->ts_periodicity = 4;
389 *flag_periodicity = 4;
390 cfg->ts_number_layers = 3;
391 cfg->ts_rate_decimator[0] = 4;
392 cfg->ts_rate_decimator[1] = 2;
393 cfg->ts_rate_decimator[2] = 1;
394 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
395 // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled in layer 1, disabled
396 // in layer 2.
397 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
398 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
399 VP8_EFLAG_NO_UPD_ARF;
400 layer_flags[2] =
401 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
402 layer_flags[1] = layer_flags[3] =
403 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
404 VP8_EFLAG_NO_UPD_ARF;
405 break;
406 }
407 case 6: {
408 // 3-layers, 4-frame period.
409 int ids[4] = { 0, 2, 1, 2 };
410 cfg->ts_periodicity = 4;
411 *flag_periodicity = 4;
412 cfg->ts_number_layers = 3;
413 cfg->ts_rate_decimator[0] = 4;
414 cfg->ts_rate_decimator[1] = 2;
415 cfg->ts_rate_decimator[2] = 1;
416 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
417 // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
418 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
419 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
420 VP8_EFLAG_NO_UPD_ARF;
421 layer_flags[2] =
422 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
423 layer_flags[1] = layer_flags[3] =
424 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
425 break;
426 }
427 case 7: {
428 // NOTE: Probably of academic interest only.
429 // 5-layers, 16-frame period.
430 int ids[16] = { 0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4 };
431 cfg->ts_periodicity = 16;
432 *flag_periodicity = 16;
433 cfg->ts_number_layers = 5;
434 cfg->ts_rate_decimator[0] = 16;
435 cfg->ts_rate_decimator[1] = 8;
436 cfg->ts_rate_decimator[2] = 4;
437 cfg->ts_rate_decimator[3] = 2;
438 cfg->ts_rate_decimator[4] = 1;
439 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
440 layer_flags[0] = VPX_EFLAG_FORCE_KF;
441 layer_flags[1] = layer_flags[3] = layer_flags[5] = layer_flags[7] =
442 layer_flags[9] = layer_flags[11] = layer_flags[13] = layer_flags[15] =
443 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
444 VP8_EFLAG_NO_UPD_ARF;
445 layer_flags[2] = layer_flags[6] = layer_flags[10] = layer_flags[14] =
446 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF;
447 layer_flags[4] = layer_flags[12] =
448 VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_UPD_ARF;
449 layer_flags[8] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF;
450 break;
451 }
452 case 8: {
453 // 2-layers, with sync point at first frame of layer 1.
454 int ids[2] = { 0, 1 };
455 cfg->ts_periodicity = 2;
456 *flag_periodicity = 8;
457 cfg->ts_number_layers = 2;
458 cfg->ts_rate_decimator[0] = 2;
459 cfg->ts_rate_decimator[1] = 1;
460 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
461 // 0=L, 1=GF.
462 // ARF is used as predictor for all frames, and is only updated on
463 // key frame. Sync point every 8 frames.
464
465 // Layer 0: predict from L and ARF, update L and G.
466 layer_flags[0] =
467 VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_ARF;
468 // Layer 1: sync point: predict from L and ARF, and update G.
469 layer_flags[1] =
470 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
471 // Layer 0, predict from L and ARF, update L.
472 layer_flags[2] =
473 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
474 // Layer 1: predict from L, G and ARF, and update G.
475 layer_flags[3] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
476 VP8_EFLAG_NO_UPD_ENTROPY;
477 // Layer 0.
478 layer_flags[4] = layer_flags[2];
479 // Layer 1.
480 layer_flags[5] = layer_flags[3];
481 // Layer 0.
482 layer_flags[6] = layer_flags[4];
483 // Layer 1.
484 layer_flags[7] = layer_flags[5];
485 break;
486 }
487 case 9: {
488 // 3-layers: Sync points for layer 1 and 2 every 8 frames.
489 int ids[4] = { 0, 2, 1, 2 };
490 cfg->ts_periodicity = 4;
491 *flag_periodicity = 8;
492 cfg->ts_number_layers = 3;
493 cfg->ts_rate_decimator[0] = 4;
494 cfg->ts_rate_decimator[1] = 2;
495 cfg->ts_rate_decimator[2] = 1;
496 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
497 // 0=L, 1=GF, 2=ARF.
498 layer_flags[0] = VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_REF_GF |
499 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
500 VP8_EFLAG_NO_UPD_ARF;
501 layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
502 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
503 layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
504 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
505 layer_flags[3] = layer_flags[5] =
506 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
507 layer_flags[4] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
508 VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
509 layer_flags[6] =
510 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF;
511 layer_flags[7] = VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
512 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_ENTROPY;
513 break;
514 }
515 case 10: {
516 // 3-layers structure where ARF is used as predictor for all frames,
517 // and is only updated on key frame.
518 // Sync points for layer 1 and 2 every 8 frames.
519
520 int ids[4] = { 0, 2, 1, 2 };
521 cfg->ts_periodicity = 4;
522 *flag_periodicity = 8;
523 cfg->ts_number_layers = 3;
524 cfg->ts_rate_decimator[0] = 4;
525 cfg->ts_rate_decimator[1] = 2;
526 cfg->ts_rate_decimator[2] = 1;
527 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
528 // 0=L, 1=GF, 2=ARF.
529 // Layer 0: predict from L and ARF; update L and G.
530 layer_flags[0] =
531 VPX_EFLAG_FORCE_KF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF;
532 // Layer 2: sync point: predict from L and ARF; update none.
533 layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF |
534 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST |
535 VP8_EFLAG_NO_UPD_ENTROPY;
536 // Layer 1: sync point: predict from L and ARF; update G.
537 layer_flags[2] =
538 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
539 // Layer 2: predict from L, G, ARF; update none.
540 layer_flags[3] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
541 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ENTROPY;
542 // Layer 0: predict from L and ARF; update L.
543 layer_flags[4] =
544 VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF;
545 // Layer 2: predict from L, G, ARF; update none.
546 layer_flags[5] = layer_flags[3];
547 // Layer 1: predict from L, G, ARF; update G.
548 layer_flags[6] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
549 // Layer 2: predict from L, G, ARF; update none.
550 layer_flags[7] = layer_flags[3];
551 break;
552 }
553 case 11: {
554 // 3-layers structure with one reference frame.
555 // This works same as temporal_layering_mode 3.
556 // This was added to compare with vp9_spatial_svc_encoder.
557
558 // 3-layers, 4-frame period.
559 int ids[4] = { 0, 2, 1, 2 };
560 cfg->ts_periodicity = 4;
561 *flag_periodicity = 4;
562 cfg->ts_number_layers = 3;
563 cfg->ts_rate_decimator[0] = 4;
564 cfg->ts_rate_decimator[1] = 2;
565 cfg->ts_rate_decimator[2] = 1;
566 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
567 // 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
568 layer_flags[0] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
569 VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
570 layer_flags[2] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
571 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
572 layer_flags[1] = VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
573 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
574 layer_flags[3] = VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_ARF |
575 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
576 break;
577 }
578 case 12:
579 default: {
580 // 3-layers structure as in case 10, but no sync/refresh points for
581 // layer 1 and 2.
582 int ids[4] = { 0, 2, 1, 2 };
583 cfg->ts_periodicity = 4;
584 *flag_periodicity = 8;
585 cfg->ts_number_layers = 3;
586 cfg->ts_rate_decimator[0] = 4;
587 cfg->ts_rate_decimator[1] = 2;
588 cfg->ts_rate_decimator[2] = 1;
589 memcpy(cfg->ts_layer_id, ids, sizeof(ids));
590 // 0=L, 1=GF, 2=ARF.
591 // Layer 0: predict from L and ARF; update L.
592 layer_flags[0] =
593 VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF;
594 layer_flags[4] = layer_flags[0];
595 // Layer 1: predict from L, G, ARF; update G.
596 layer_flags[2] = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
597 layer_flags[6] = layer_flags[2];
598 // Layer 2: predict from L, G, ARF; update none.
599 layer_flags[1] = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
600 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ENTROPY;
601 layer_flags[3] = layer_flags[1];
602 layer_flags[5] = layer_flags[1];
603 layer_flags[7] = layer_flags[1];
604 break;
605 }
606 }
607}
608
609#if ROI_MAP
610 static int read_mask(FILE *mask_file, int *seg_map, int allowed_mask_rows,
611 int allowed_mask_cols) {
612 int mask_rows, mask_cols, i, j;
613 int *map_start = seg_map;
614 if (fscanf(mask_file, "%d %d\n", &mask_cols, &mask_rows) != 2) return 0;
615 if (mask_rows != allowed_mask_rows || mask_cols != allowed_mask_cols) {
616 return 0;
617 }
618 for (i = 0; i < mask_rows; i++) {
619 for (j = 0; j < mask_cols; j++) {
620 if (fscanf(mask_file, "%d ", &seg_map[j]) != 1) return 0;
621 // reverse the bit
622 seg_map[j] = 1 - seg_map[j];
623 }
624 seg_map += mask_cols;
625 }
626 seg_map = map_start;
627 return 1;
628}
629#endif
630
631 int main(int argc, char **argv) {
632 VpxVideoWriter *outfile[VPX_TS_MAX_LAYERS] = { NULL };
633 vpx_codec_ctx_t codec;
634 vpx_codec_enc_cfg_t cfg;
635 int frame_cnt = 0;
636 vpx_image_t raw;
637 vpx_codec_err_t res;
638 unsigned int width;
639 unsigned int height;
640 uint32_t error_resilient = 0;
641 int speed;
642 int frame_avail;
643 int got_data;
644 int flags = 0;
645 unsigned int i;
646 int pts = 0; // PTS starts at 0.
647 int frame_duration = 1; // 1 timebase tick per frame.
648 int layering_mode = 0;
649 int layer_flags[VPX_TS_MAX_PERIODICITY] = { 0 };
650 int flag_periodicity = 1;
651#if ROI_MAP
652 vpx_roi_map_t roi;
653#endif
654 vpx_svc_layer_id_t layer_id;
655 const VpxInterface *encoder = NULL;
656 struct VpxInputContext input_ctx;
657 struct RateControlMetrics rc;
658 int64_t cx_time = 0;
659 const int min_args_base = 13;
660#if CONFIG_VP9_HIGHBITDEPTH
661 vpx_bit_depth_t bit_depth = VPX_BITS_8;
662 int input_bit_depth = 8;
663 const int min_args = min_args_base + 1;
664#else
665 const int min_args = min_args_base;
666#endif // CONFIG_VP9_HIGHBITDEPTH
667 double sum_bitrate = 0.0;
668 double sum_bitrate2 = 0.0;
669 double framerate = 30.0;
670#if ROI_MAP
671 FILE *mask_file = NULL;
672 int block_size = 8;
673 int mask_rows = 0;
674 int mask_cols = 0;
675 int *mask_map;
676 int *prev_mask_map;
677#endif
678 zero(rc.layer_target_bitrate);
679 memset(&layer_id, 0, sizeof(vpx_svc_layer_id_t));
680 memset(&input_ctx, 0, sizeof(input_ctx));
681 /* Setup default input stream settings */
682 input_ctx.framerate.numerator = 30;
683 input_ctx.framerate.denominator = 1;
684 input_ctx.only_i420 = 1;
685 input_ctx.bit_depth = 0;
686
687 exec_name = argv[0];
688 // Check usage and arguments.
689 if (argc < min_args) {
690#if CONFIG_VP9_HIGHBITDEPTH
691 die("Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> "
692 "<rate_num> <rate_den> <speed> <frame_drop_threshold> "
693 "<error_resilient> <threads> <mode> "
694 "<Rate_0> ... <Rate_nlayers-1> <bit-depth> \n",
695 argv[0]);
696#else
697 die("Usage: %s <infile> <outfile> <codec_type(vp8/vp9)> <width> <height> "
698 "<rate_num> <rate_den> <speed> <frame_drop_threshold> "
699 "<error_resilient> <threads> <mode> "
700 "<Rate_0> ... <Rate_nlayers-1> \n",
701 argv[0]);
702#endif // CONFIG_VP9_HIGHBITDEPTH
703 }
704
705 encoder = get_vpx_encoder_by_name(argv[3]);
706 if (!encoder) die("Unsupported codec.");
707
708 printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
709
710 width = (unsigned int)strtoul(argv[4], NULL, 0);
711 height = (unsigned int)strtoul(argv[5], NULL, 0);
712 if (width < 16 || width % 2 || height < 16 || height % 2) {
713 die("Invalid resolution: %d x %d", width, height);
714 }
715
716 layering_mode = (int)strtol(argv[12], NULL, 0);
717 if (layering_mode < 0 || layering_mode > 13) {
718 die("Invalid layering mode (0..12) %s", argv[12]);
719 }
720
721#if ROI_MAP
722 if (argc != min_args + mode_to_num_layers[layering_mode] + 1) {
723 die("Invalid number of arguments");
724 }
725#else
726 if (argc != min_args + mode_to_num_layers[layering_mode]) {
727 die("Invalid number of arguments");
728 }
729#endif
730
731 input_ctx.filename = argv[1];
732 open_input_file(&input_ctx);
733
734#if CONFIG_VP9_HIGHBITDEPTH
735 switch (strtol(argv[argc - 1], NULL, 0)) {
736 case 8:
737 bit_depth = VPX_BITS_8;
738 input_bit_depth = 8;
739 break;
740 case 10:
741 bit_depth = VPX_BITS_10;
742 input_bit_depth = 10;
743 break;
744 case 12:
745 bit_depth = VPX_BITS_12;
746 input_bit_depth = 12;
747 break;
748 default: die("Invalid bit depth (8, 10, 12) %s", argv[argc - 1]);
749 }
750
751 // Y4M reader has its own allocation.
752 if (input_ctx.file_type != FILE_TYPE_Y4M) {
753 if (!vpx_img_alloc(
754 &raw,
755 bit_depth == VPX_BITS_8 ? VPX_IMG_FMT_I420 : VPX_IMG_FMT_I42016,
756 width, height, 32)) {
757 die("Failed to allocate image (%dx%d)", width, height);
758 }
759 }
760#else
761 // Y4M reader has its own allocation.
762 if (input_ctx.file_type != FILE_TYPE_Y4M) {
763 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 32)) {
764 die("Failed to allocate image (%dx%d)", width, height);
765 }
766 }
767#endif // CONFIG_VP9_HIGHBITDEPTH
768
769 // Populate encoder configuration.
770 res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
771 if (res) {
772 printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
773 return EXIT_FAILURE;
774 }
775
776 // Update the default configuration with our settings.
777 cfg.g_w = width;
778 cfg.g_h = height;
779
780#if CONFIG_VP9_HIGHBITDEPTH
781 if (bit_depth != VPX_BITS_8) {
782 cfg.g_bit_depth = bit_depth;
783 cfg.g_input_bit_depth = input_bit_depth;
784 cfg.g_profile = 2;
785 }
786#endif // CONFIG_VP9_HIGHBITDEPTH
787
788 // Timebase format e.g. 30fps: numerator=1, demoninator = 30.
789 cfg.g_timebase.num = (int)strtol(argv[6], NULL, 0);
790 cfg.g_timebase.den = (int)strtol(argv[7], NULL, 0);
791
792 speed = (int)strtol(argv[8], NULL, 0);
793 if (speed < 0) {
794 die("Invalid speed setting: must be positive");
795 }
796 if (strncmp(encoder->name, "vp9", 3) == 0 && speed > 9) {
797 warn("Mapping speed %d to speed 9.\n", speed);
798 }
799
800 for (i = min_args_base;
801 (int)i < min_args_base + mode_to_num_layers[layering_mode]; ++i) {
802 rc.layer_target_bitrate[i - 13] = (int)strtol(argv[i], NULL, 0);
803 if (strncmp(encoder->name, "vp8", 3) == 0)
804 cfg.ts_target_bitrate[i - 13] = rc.layer_target_bitrate[i - 13];
805 else if (strncmp(encoder->name, "vp9", 3) == 0)
806 cfg.layer_target_bitrate[i - 13] = rc.layer_target_bitrate[i - 13];
807 }
808
809 // Real time parameters.
810 cfg.rc_dropframe_thresh = (unsigned int)strtoul(argv[9], NULL, 0);
811 cfg.rc_end_usage = VPX_CBR;
812 cfg.rc_min_quantizer = 2;
813 cfg.rc_max_quantizer = 56;
814 if (strncmp(encoder->name, "vp9", 3) == 0) cfg.rc_max_quantizer = 52;
815 cfg.rc_undershoot_pct = 50;
816 cfg.rc_overshoot_pct = 50;
817 cfg.rc_buf_initial_sz = 600;
818 cfg.rc_buf_optimal_sz = 600;
819 cfg.rc_buf_sz = 1000;
820
821 // Disable dynamic resizing by default.
822 cfg.rc_resize_allowed = 0;
823
824 // Use 1 thread as default.
825 cfg.g_threads = (unsigned int)strtoul(argv[11], NULL, 0);
826
827 error_resilient = (uint32_t)strtoul(argv[10], NULL, 0);
828 if (error_resilient != 0 && error_resilient != 1) {
829 die("Invalid value for error resilient (0, 1): %d.", error_resilient);
830 }
831 // Enable error resilient mode.
832 cfg.g_error_resilient = error_resilient;
833 cfg.g_lag_in_frames = 0;
834 cfg.kf_mode = VPX_KF_AUTO;
835
836 // Disable automatic keyframe placement.
837 cfg.kf_min_dist = cfg.kf_max_dist = 3000;
838
839 cfg.temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_BYPASS;
840
841 set_temporal_layer_pattern(layering_mode, &cfg, layer_flags,
842 &flag_periodicity);
843
844 set_rate_control_metrics(&rc, &cfg);
845
846 if (input_ctx.file_type == FILE_TYPE_Y4M) {
847 if (input_ctx.width != cfg.g_w || input_ctx.height != cfg.g_h) {
848 die("Incorrect width or height: %d x %d", cfg.g_w, cfg.g_h);
849 }
850 if (input_ctx.framerate.numerator != cfg.g_timebase.den ||
851 input_ctx.framerate.denominator != cfg.g_timebase.num) {
852 die("Incorrect framerate: numerator %d denominator %d",
853 cfg.g_timebase.num, cfg.g_timebase.den);
854 }
855 }
856
857 framerate = cfg.g_timebase.den / cfg.g_timebase.num;
858 // Open an output file for each stream.
859 for (i = 0; i < cfg.ts_number_layers; ++i) {
860 char file_name[PATH_MAX];
861 VpxVideoInfo info;
862 info.codec_fourcc = encoder->fourcc;
863 info.frame_width = cfg.g_w;
864 info.frame_height = cfg.g_h;
865 info.time_base.numerator = cfg.g_timebase.num;
866 info.time_base.denominator = cfg.g_timebase.den;
867
868 snprintf(file_name, sizeof(file_name), "%s_%d.ivf", argv[2], i);
869 outfile[i] = vpx_video_writer_open(file_name, kContainerIVF, &info);
870 if (!outfile[i]) die("Failed to open %s for writing", file_name);
871
872 assert(outfile[i] != NULL);
873 }
874 // No spatial layers in this encoder.
875 cfg.ss_number_layers = 1;
876
877// Initialize codec.
878#if CONFIG_VP9_HIGHBITDEPTH
879 if (vpx_codec_enc_init(
880 &codec, encoder->codec_interface(), &cfg,
881 bit_depth == VPX_BITS_8 ? 0 : VPX_CODEC_USE_HIGHBITDEPTH))
882#else
883 if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
884#endif // CONFIG_VP9_HIGHBITDEPTH
885 die("Failed to initialize encoder");
886
887#if ROI_MAP
888 mask_rows = (cfg.g_h + block_size - 1) / block_size;
889 mask_cols = (cfg.g_w + block_size - 1) / block_size;
890 mask_map = (int *)calloc(mask_rows * mask_cols, sizeof(*mask_map));
891 prev_mask_map = (int *)calloc(mask_rows * mask_cols, sizeof(*mask_map));
892#endif
893
894 if (strncmp(encoder->name, "vp8", 3) == 0) {
895 vpx_codec_control(&codec, VP8E_SET_CPUUSED, -speed);
896 vpx_codec_control(&codec, VP8E_SET_NOISE_SENSITIVITY, kVp8DenoiserOff);
897 vpx_codec_control(&codec, VP8E_SET_STATIC_THRESHOLD, 1);
898 vpx_codec_control(&codec, VP8E_SET_GF_CBR_BOOST_PCT, 0);
899#if ROI_MAP
900 set_roi_map(encoder->name, &cfg, &roi);
901 if (vpx_codec_control(&codec, VP8E_SET_ROI_MAP, &roi))
902 die_codec(&codec, "Failed to set ROI map");
903#endif
904 } else if (strncmp(encoder->name, "vp9", 3) == 0) {
905 vpx_svc_extra_cfg_t svc_params;
906 memset(&svc_params, 0, sizeof(svc_params));
907 vpx_codec_control(&codec, VP9E_SET_POSTENCODE_DROP, 0);
908 vpx_codec_control(&codec, VP9E_SET_DISABLE_OVERSHOOT_MAXQ_CBR, 0);
909 vpx_codec_control(&codec, VP8E_SET_CPUUSED, speed);
910 vpx_codec_control(&codec, VP9E_SET_AQ_MODE, 3);
911 vpx_codec_control(&codec, VP9E_SET_GF_CBR_BOOST_PCT, 0);
912 vpx_codec_control(&codec, VP9E_SET_FRAME_PARALLEL_DECODING, 0);
913 vpx_codec_control(&codec, VP9E_SET_FRAME_PERIODIC_BOOST, 0);
914 vpx_codec_control(&codec, VP9E_SET_NOISE_SENSITIVITY, kVp9DenoiserOff);
915 vpx_codec_control(&codec, VP8E_SET_STATIC_THRESHOLD, 1);
916 vpx_codec_control(&codec, VP9E_SET_TUNE_CONTENT, 0);
917 vpx_codec_control(&codec, VP9E_SET_TILE_COLUMNS, get_msb(cfg.g_threads));
918 vpx_codec_control(&codec, VP9E_SET_DISABLE_LOOPFILTER, 0);
919
920 if (cfg.g_threads > 1)
921 vpx_codec_control(&codec, VP9E_SET_ROW_MT, 1);
922 else
923 vpx_codec_control(&codec, VP9E_SET_ROW_MT, 0);
924 if (vpx_codec_control(&codec, VP9E_SET_SVC, layering_mode > 0 ? 1 : 0))
925 die_codec(&codec, "Failed to set SVC");
926 for (i = 0; i < cfg.ts_number_layers; ++i) {
927 svc_params.max_quantizers[i] = cfg.rc_max_quantizer;
928 svc_params.min_quantizers[i] = cfg.rc_min_quantizer;
929 }
930 svc_params.scaling_factor_num[0] = cfg.g_h;
931 svc_params.scaling_factor_den[0] = cfg.g_h;
932 vpx_codec_control(&codec, VP9E_SET_SVC_PARAMETERS, &svc_params);
933 }
934 if (strncmp(encoder->name, "vp8", 3) == 0) {
935 vpx_codec_control(&codec, VP8E_SET_SCREEN_CONTENT_MODE, 0);
936 }
937 vpx_codec_control(&codec, VP8E_SET_TOKEN_PARTITIONS, 1);
938 // This controls the maximum target size of the key frame.
939 // For generating smaller key frames, use a smaller max_intra_size_pct
940 // value, like 100 or 200.
941 {
942 const int max_intra_size_pct = 1000;
943 vpx_codec_control(&codec, VP8E_SET_MAX_INTRA_BITRATE_PCT,
944 max_intra_size_pct);
945 }
946
947 frame_avail = 1;
948 while (frame_avail || got_data) {
949 struct vpx_usec_timer timer;
950 vpx_codec_iter_t iter = NULL;
951 const vpx_codec_cx_pkt_t *pkt;
952#if ROI_MAP
953 char mask_file_name[255];
954#endif
955 // Update the temporal layer_id. No spatial layers in this test.
956 layer_id.spatial_layer_id = 0;
957 layer_id.temporal_layer_id =
958 cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
959 layer_id.temporal_layer_id_per_spatial[0] = layer_id.temporal_layer_id;
960 if (strncmp(encoder->name, "vp9", 3) == 0) {
961 vpx_codec_control(&codec, VP9E_SET_SVC_LAYER_ID, &layer_id);
962 } else if (strncmp(encoder->name, "vp8", 3) == 0) {
963 vpx_codec_control(&codec, VP8E_SET_TEMPORAL_LAYER_ID,
964 layer_id.temporal_layer_id);
965 }
966 flags = layer_flags[frame_cnt % flag_periodicity];
967 if (layering_mode == 0) flags = 0;
968#if ROI_MAP
969 snprintf(mask_file_name, sizeof(mask_file_name), "%s%05d.txt",
970 argv[argc - 1], frame_cnt);
971 mask_file = fopen(mask_file_name, "r");
972 if (mask_file != NULL) {
973 int mask_is_valid = read_mask(mask_file, mask_map, mask_rows, mask_cols);
974 fclose(mask_file);
975 if (mask_is_valid) {
976 // set_roi_map(encoder->name, &cfg, &roi);
977 set_roi_skip_map(&cfg, &roi, mask_map, prev_mask_map, frame_cnt);
978 if (vpx_codec_control(&codec, VP9E_SET_ROI_MAP, &roi))
979 die_codec(&codec, "Failed to set ROI map");
980 } else {
981 die_codec(&codec, "Mask input is invalid for ROI map");
982 }
983 }
984#endif
985 frame_avail = read_frame(&input_ctx, &raw);
986 if (frame_avail) ++rc.layer_input_frames[layer_id.temporal_layer_id];
987 vpx_usec_timer_start(&timer);
988 if (vpx_codec_encode(&codec, frame_avail ? &raw : NULL, pts, 1, flags,
989 VPX_DL_REALTIME)) {
990 die_codec(&codec, "Failed to encode frame");
991 }
992 vpx_usec_timer_mark(&timer);
993 cx_time += vpx_usec_timer_elapsed(&timer);
994 // Reset KF flag.
995 if (layering_mode != 7) {
996 layer_flags[0] &= ~VPX_EFLAG_FORCE_KF;
997 }
998 got_data = 0;
999 while ((pkt = vpx_codec_get_cx_data(&codec, &iter))) {
1000 got_data = 1;
1001 switch (pkt->kind) {
1002 case VPX_CODEC_CX_FRAME_PKT:
1003 for (i = cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
1004 i < cfg.ts_number_layers; ++i) {
1005 vpx_video_writer_write_frame(outfile[i], pkt->data.frame.buf,
1006 pkt->data.frame.sz, pts);
1007 ++rc.layer_tot_enc_frames[i];
1008 rc.layer_encoding_bitrate[i] += 8.0 * pkt->data.frame.sz;
1009 // Keep count of rate control stats per layer (for non-key frames).
1010 if (i == cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity] &&
1011 !(pkt->data.frame.flags & VPX_FRAME_IS_KEY)) {
1012 rc.layer_avg_frame_size[i] += 8.0 * pkt->data.frame.sz;
1013 rc.layer_avg_rate_mismatch[i] +=
1014 fabs(8.0 * pkt->data.frame.sz - rc.layer_pfb[i]) /
1015 rc.layer_pfb[i];
1016 ++rc.layer_enc_frames[i];
1017 }
1018 }
1019 // Update for short-time encoding bitrate states, for moving window
1020 // of size rc->window, shifted by rc->window / 2.
1021 // Ignore first window segment, due to key frame.
1022 if (rc.window_size == 0) rc.window_size = 15;
1023 if (frame_cnt > rc.window_size) {
1024 sum_bitrate += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
1025 if (frame_cnt % rc.window_size == 0) {
1026 rc.window_count += 1;
1027 rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
1028 rc.variance_st_encoding_bitrate +=
1029 (sum_bitrate / rc.window_size) *
1030 (sum_bitrate / rc.window_size);
1031 sum_bitrate = 0.0;
1032 }
1033 }
1034 // Second shifted window.
1035 if (frame_cnt > rc.window_size + rc.window_size / 2) {
1036 sum_bitrate2 += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
1037 if (frame_cnt > 2 * rc.window_size &&
1038 frame_cnt % rc.window_size == 0) {
1039 rc.window_count += 1;
1040 rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
1041 rc.variance_st_encoding_bitrate +=
1042 (sum_bitrate2 / rc.window_size) *
1043 (sum_bitrate2 / rc.window_size);
1044 sum_bitrate2 = 0.0;
1045 }
1046 }
1047 break;
1048 default: break;
1049 }
1050 }
1051 ++frame_cnt;
1052 pts += frame_duration;
1053 }
1054#if ROI_MAP
1055 free(mask_map);
1056 free(prev_mask_map);
1057#endif
1058 close_input_file(&input_ctx);
1059 printout_rate_control_summary(&rc, &cfg, frame_cnt);
1060 printf("\n");
1061 printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
1062 frame_cnt, 1000 * (float)cx_time / (double)(frame_cnt * 1000000),
1063 1000000 * (double)frame_cnt / (double)cx_time);
1064
1065 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
1066
1067 // Try to rewrite the output file headers with the actual frame count.
1068 for (i = 0; i < cfg.ts_number_layers; ++i) vpx_video_writer_close(outfile[i]);
1069
1070 if (input_ctx.file_type != FILE_TYPE_Y4M) {
1071 vpx_img_free(&raw);
1072 }
1073
1074#if ROI_MAP
1075 free(roi.roi_map);
1076#endif
1077 return EXIT_SUCCESS;
1078}
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:1012
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:903
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 temporal layering. This will only work when the n...
Definition vp8cx.h:808
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:411
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:706
vpx_codec_enc_cfg::kf_min_dist
unsigned int kf_min_dist
Keyframe minimum interval.
Definition vpx_encoder.h:618
vpx_codec_enc_cfg::rc_min_quantizer
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition vpx_encoder.h:486
vpx_codec_enc_cfg::ts_number_layers
unsigned int ts_number_layers
Number of temporal coding layers.
Definition vpx_encoder.h:657
vpx_codec_enc_cfg::ss_number_layers
unsigned int ss_number_layers
Number of spatial coding layers.
Definition vpx_encoder.h:637
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:697
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:609
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:689
vpx_codec_enc_cfg::g_error_resilient
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition vpx_encoder.h:362
vpx_codec_enc_cfg::ts_periodicity
unsigned int ts_periodicity
Length of the sequence defining frame temporal layer membership.
Definition vpx_encoder.h:680
vpx_codec_enc_cfg::rc_overshoot_pct
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition vpx_encoder.h:529
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:544
vpx_codec_enc_cfg::rc_dropframe_thresh
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition vpx_encoder.h:402
vpx_codec_enc_cfg::g_timebase
struct vpx_rational g_timebase
Stream timebase units.
Definition vpx_encoder.h:354
vpx_codec_enc_cfg::rc_max_quantizer
unsigned int rc_max_quantizer
Maximum (Worst Quality) Quantizer.
Definition vpx_encoder.h:495
vpx_codec_enc_cfg::g_lag_in_frames
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition vpx_encoder.h:383
vpx_codec_enc_cfg::rc_end_usage
enum vpx_rc_mode rc_end_usage
Rate control algorithm to use.
Definition vpx_encoder.h:451
vpx_codec_enc_cfg::rc_buf_initial_sz
unsigned int rc_buf_initial_sz
Decoder Buffer Initial Size.
Definition vpx_encoder.h:553
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:562
vpx_codec_enc_cfg::rc_target_bitrate
unsigned int rc_target_bitrate
Target data rate.
Definition vpx_encoder.h:473
vpx_codec_enc_cfg::ts_target_bitrate
unsigned int ts_target_bitrate[5]
Target bitrate for each temporal layer.
Definition vpx_encoder.h:664
vpx_codec_enc_cfg::g_input_bit_depth
unsigned int g_input_bit_depth
Bit-depth of the input frames.
Definition vpx_encoder.h:340
vpx_codec_enc_cfg::rc_undershoot_pct
unsigned int rc_undershoot_pct
Rate control adaptation undershoot control.
Definition vpx_encoder.h:514
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:671
vpx_codec_enc_cfg::kf_max_dist
unsigned int kf_max_dist
Keyframe maximum interval.
Definition vpx_encoder.h:627
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:837
vpx_roi_map::static_threshold
unsigned int static_threshold[4]
Definition vp8cx.h:840
vpx_roi_map::rows
unsigned int rows
Definition vp8cx.h:831
vpx_roi_map::cols
unsigned int cols
Definition vp8cx.h:832
vpx_roi_map::ref_frame
int ref_frame[8]
Definition vp8cx.h:838
vpx_roi_map::delta_q
int delta_q[8]
Definition vp8cx.h:834
vpx_roi_map::roi_map
unsigned char * roi_map
Definition vp8cx.h:830
vpx_roi_map::delta_lf
int delta_lf[8]
Definition vp8cx.h:835
vpx_svc_layer_id::temporal_layer_id
int temporal_layer_id
Definition vp8cx.h:905
vpx_svc_layer_id::temporal_layer_id_per_spatial
int temporal_layer_id_per_spatial[5]
Definition vp8cx.h:906
vpx_svc_layer_id::spatial_layer_id
int spatial_layer_id
Definition vp8cx.h:903
vpx_svc_parameters::min_quantizers
int min_quantizers[12]
Definition vpx_encoder.h:861
vpx_svc_parameters::scaling_factor_num
int scaling_factor_num[12]
Definition vpx_encoder.h:862
vpx_svc_parameters::max_quantizers
int max_quantizers[12]
Definition vpx_encoder.h:860
vpx_svc_parameters::scaling_factor_den
int scaling_factor_den[12]
Definition vpx_encoder.h:863
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.