1 /*
2 * Copyright (c) 2013 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 /**
12 * @file
13 * VP9 SVC encoding support via libvpx
14 */
15
16 #include <assert.h>
17 #include <math.h>
18 #include <limits.h>
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #define VPX_DISABLE_CTRL_TYPECHECKS 1
24 #include "./vpx_config.h"
25 #include "./svc_context.h"
26 #include "vpx/vp8cx.h"
27 #include "vpx/vpx_encoder.h"
28 #include "vpx_mem/vpx_mem.h"
29 #include "vp9/common/vp9_onyxc_int.h"
30
31 #ifdef __MINGW32__
32 #define strtok_r strtok_s
33 #ifndef MINGW_HAS_SECURE_API
34 // proto from /usr/x86_64-w64-mingw32/include/sec_api/string_s.h
35 _CRTIMP char *__cdecl strtok_s(char *str, const char *delim, char **context);
36 #endif /* MINGW_HAS_SECURE_API */
37 #endif /* __MINGW32__ */
38
39 #ifdef _MSC_VER
40 #define strdup _strdup
41 #define strtok_r strtok_s
42 #endif
43
44 #define SVC_REFERENCE_FRAMES 8
45 #define SUPERFRAME_SLOTS (8)
46 #define SUPERFRAME_BUFFER_SIZE (SUPERFRAME_SLOTS * sizeof(uint32_t) + 2)
47
48 #define MAX_QUANTIZER 63
49
50 static const int DEFAULT_SCALE_FACTORS_NUM[VPX_SS_MAX_LAYERS] = { 4, 5, 7, 11,
51 16 };
52
53 static const int DEFAULT_SCALE_FACTORS_DEN[VPX_SS_MAX_LAYERS] = { 16, 16, 16,
54 16, 16 };
55
56 static const int DEFAULT_SCALE_FACTORS_NUM_2x[VPX_SS_MAX_LAYERS] = { 1, 2, 4 };
57
58 static const int DEFAULT_SCALE_FACTORS_DEN_2x[VPX_SS_MAX_LAYERS] = { 4, 4, 4 };
59
60 typedef enum {
61 QUANTIZER = 0,
62 BITRATE,
63 SCALE_FACTOR,
64 AUTO_ALT_REF,
65 ALL_OPTION_TYPES
66 } LAYER_OPTION_TYPE;
67
68 static const int option_max_values[ALL_OPTION_TYPES] = { 63, INT_MAX, INT_MAX,
69 1 };
70
71 static const int option_min_values[ALL_OPTION_TYPES] = { 0, 0, 1, 0 };
72
73 // One encoded frame
74 typedef struct FrameData {
75 void *buf; // compressed data buffer
76 size_t size; // length of compressed data
77 vpx_codec_frame_flags_t flags; /**< flags for this frame */
78 struct FrameData *next;
79 } FrameData;
80
get_svc_internal(SvcContext * svc_ctx)81 static SvcInternal_t *get_svc_internal(SvcContext *svc_ctx) {
82 if (svc_ctx == NULL) return NULL;
83 if (svc_ctx->internal == NULL) {
84 SvcInternal_t *const si = (SvcInternal_t *)malloc(sizeof(*si));
85 if (si != NULL) {
86 memset(si, 0, sizeof(*si));
87 }
88 svc_ctx->internal = si;
89 }
90 return (SvcInternal_t *)svc_ctx->internal;
91 }
92
get_const_svc_internal(const SvcContext * svc_ctx)93 static const SvcInternal_t *get_const_svc_internal(const SvcContext *svc_ctx) {
94 if (svc_ctx == NULL) return NULL;
95 return (const SvcInternal_t *)svc_ctx->internal;
96 }
97
svc_log(SvcContext * svc_ctx,SVC_LOG_LEVEL level,const char * fmt,...)98 static int svc_log(SvcContext *svc_ctx, SVC_LOG_LEVEL level, const char *fmt,
99 ...) {
100 char buf[512];
101 int retval = 0;
102 va_list ap;
103
104 if (level > svc_ctx->log_level) {
105 return retval;
106 }
107
108 va_start(ap, fmt);
109 retval = vsnprintf(buf, sizeof(buf), fmt, ap);
110 va_end(ap);
111
112 printf("%s", buf);
113
114 return retval;
115 }
116
extract_option(LAYER_OPTION_TYPE type,char * input,int * value0,int * value1)117 static vpx_codec_err_t extract_option(LAYER_OPTION_TYPE type, char *input,
118 int *value0, int *value1) {
119 if (type == SCALE_FACTOR) {
120 *value0 = (int)strtol(input, &input, 10);
121 if (*input++ != '/') return VPX_CODEC_INVALID_PARAM;
122 *value1 = (int)strtol(input, &input, 10);
123
124 if (*value0 < option_min_values[SCALE_FACTOR] ||
125 *value1 < option_min_values[SCALE_FACTOR] ||
126 *value0 > option_max_values[SCALE_FACTOR] ||
127 *value1 > option_max_values[SCALE_FACTOR] ||
128 *value0 > *value1) // num shouldn't be greater than den
129 return VPX_CODEC_INVALID_PARAM;
130 } else {
131 *value0 = atoi(input);
132 if (*value0 < option_min_values[type] || *value0 > option_max_values[type])
133 return VPX_CODEC_INVALID_PARAM;
134 }
135 return VPX_CODEC_OK;
136 }
137
parse_layer_options_from_string(SvcContext * svc_ctx,LAYER_OPTION_TYPE type,const char * input,int * option0,int * option1)138 static vpx_codec_err_t parse_layer_options_from_string(SvcContext *svc_ctx,
139 LAYER_OPTION_TYPE type,
140 const char *input,
141 int *option0,
142 int *option1) {
143 int i;
144 vpx_codec_err_t res = VPX_CODEC_OK;
145 char *input_string;
146 char *token;
147 const char *delim = ",";
148 char *save_ptr;
149 int num_layers = svc_ctx->spatial_layers;
150 if (type == BITRATE)
151 num_layers = svc_ctx->spatial_layers * svc_ctx->temporal_layers;
152
153 if (input == NULL || option0 == NULL ||
154 (option1 == NULL && type == SCALE_FACTOR))
155 return VPX_CODEC_INVALID_PARAM;
156
157 input_string = strdup(input);
158 if (input_string == NULL) return VPX_CODEC_MEM_ERROR;
159 token = strtok_r(input_string, delim, &save_ptr);
160 for (i = 0; i < num_layers; ++i) {
161 if (token != NULL) {
162 res = extract_option(type, token, option0 + i, option1 + i);
163 if (res != VPX_CODEC_OK) break;
164 token = strtok_r(NULL, delim, &save_ptr);
165 } else {
166 break;
167 }
168 }
169 if (res == VPX_CODEC_OK && i != num_layers) {
170 svc_log(svc_ctx, SVC_LOG_ERROR,
171 "svc: layer params type: %d %d values required, "
172 "but only %d specified\n",
173 type, num_layers, i);
174 res = VPX_CODEC_INVALID_PARAM;
175 }
176 free(input_string);
177 return res;
178 }
179
180 /**
181 * Parse SVC encoding options
182 * Format: encoding-mode=<svc_mode>,layers=<layer_count>
183 * scale-factors=<n1>/<d1>,<n2>/<d2>,...
184 * quantizers=<q1>,<q2>,...
185 * svc_mode = [i|ip|alt_ip|gf]
186 */
parse_options(SvcContext * svc_ctx,const char * options)187 static vpx_codec_err_t parse_options(SvcContext *svc_ctx, const char *options) {
188 char *input_string;
189 char *option_name;
190 char *option_value;
191 char *input_ptr = NULL;
192 SvcInternal_t *const si = get_svc_internal(svc_ctx);
193 vpx_codec_err_t res = VPX_CODEC_OK;
194 int i, alt_ref_enabled = 0;
195
196 if (options == NULL) return VPX_CODEC_OK;
197 input_string = strdup(options);
198 if (input_string == NULL) return VPX_CODEC_MEM_ERROR;
199
200 // parse option name
201 option_name = strtok_r(input_string, "=", &input_ptr);
202 while (option_name != NULL) {
203 // parse option value
204 option_value = strtok_r(NULL, " ", &input_ptr);
205 if (option_value == NULL) {
206 svc_log(svc_ctx, SVC_LOG_ERROR, "option missing value: %s\n",
207 option_name);
208 res = VPX_CODEC_INVALID_PARAM;
209 break;
210 }
211 if (strcmp("spatial-layers", option_name) == 0) {
212 svc_ctx->spatial_layers = atoi(option_value);
213 } else if (strcmp("temporal-layers", option_name) == 0) {
214 svc_ctx->temporal_layers = atoi(option_value);
215 } else if (strcmp("scale-factors", option_name) == 0) {
216 res = parse_layer_options_from_string(svc_ctx, SCALE_FACTOR, option_value,
217 si->svc_params.scaling_factor_num,
218 si->svc_params.scaling_factor_den);
219 if (res != VPX_CODEC_OK) break;
220 } else if (strcmp("max-quantizers", option_name) == 0) {
221 res =
222 parse_layer_options_from_string(svc_ctx, QUANTIZER, option_value,
223 si->svc_params.max_quantizers, NULL);
224 if (res != VPX_CODEC_OK) break;
225 } else if (strcmp("min-quantizers", option_name) == 0) {
226 res =
227 parse_layer_options_from_string(svc_ctx, QUANTIZER, option_value,
228 si->svc_params.min_quantizers, NULL);
229 if (res != VPX_CODEC_OK) break;
230 } else if (strcmp("auto-alt-refs", option_name) == 0) {
231 res = parse_layer_options_from_string(svc_ctx, AUTO_ALT_REF, option_value,
232 si->enable_auto_alt_ref, NULL);
233 if (res != VPX_CODEC_OK) break;
234 } else if (strcmp("bitrates", option_name) == 0) {
235 res = parse_layer_options_from_string(svc_ctx, BITRATE, option_value,
236 si->bitrates, NULL);
237 if (res != VPX_CODEC_OK) break;
238 } else if (strcmp("multi-frame-contexts", option_name) == 0) {
239 si->use_multiple_frame_contexts = atoi(option_value);
240 } else {
241 svc_log(svc_ctx, SVC_LOG_ERROR, "invalid option: %s\n", option_name);
242 res = VPX_CODEC_INVALID_PARAM;
243 break;
244 }
245 option_name = strtok_r(NULL, "=", &input_ptr);
246 }
247 free(input_string);
248
249 for (i = 0; i < svc_ctx->spatial_layers; ++i) {
250 if (si->svc_params.max_quantizers[i] > MAX_QUANTIZER ||
251 si->svc_params.max_quantizers[i] < 0 ||
252 si->svc_params.min_quantizers[i] > si->svc_params.max_quantizers[i] ||
253 si->svc_params.min_quantizers[i] < 0)
254 res = VPX_CODEC_INVALID_PARAM;
255 }
256
257 if (si->use_multiple_frame_contexts &&
258 (svc_ctx->spatial_layers > 3 ||
259 svc_ctx->spatial_layers * svc_ctx->temporal_layers > 4))
260 res = VPX_CODEC_INVALID_PARAM;
261
262 for (i = 0; i < svc_ctx->spatial_layers; ++i)
263 alt_ref_enabled += si->enable_auto_alt_ref[i];
264 if (alt_ref_enabled > REF_FRAMES - svc_ctx->spatial_layers) {
265 svc_log(svc_ctx, SVC_LOG_ERROR,
266 "svc: auto alt ref: Maxinum %d(REF_FRAMES - layers) layers could"
267 "enabled auto alt reference frame, but % layers are enabled\n",
268 REF_FRAMES - svc_ctx->spatial_layers, alt_ref_enabled);
269 res = VPX_CODEC_INVALID_PARAM;
270 }
271
272 return res;
273 }
274
vpx_svc_set_options(SvcContext * svc_ctx,const char * options)275 vpx_codec_err_t vpx_svc_set_options(SvcContext *svc_ctx, const char *options) {
276 SvcInternal_t *const si = get_svc_internal(svc_ctx);
277 if (svc_ctx == NULL || options == NULL || si == NULL) {
278 return VPX_CODEC_INVALID_PARAM;
279 }
280 strncpy(si->options, options, sizeof(si->options));
281 si->options[sizeof(si->options) - 1] = '\0';
282 return VPX_CODEC_OK;
283 }
284
assign_layer_bitrates(const SvcContext * svc_ctx,vpx_codec_enc_cfg_t * const enc_cfg)285 static vpx_codec_err_t assign_layer_bitrates(
286 const SvcContext *svc_ctx, vpx_codec_enc_cfg_t *const enc_cfg) {
287 int i;
288 const SvcInternal_t *const si = get_const_svc_internal(svc_ctx);
289 int sl, tl, spatial_layer_target;
290
291 if (svc_ctx->temporal_layering_mode != 0) {
292 if (si->bitrates[0] != 0) {
293 unsigned int total_bitrate = 0;
294 for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
295 total_bitrate += si->bitrates[sl * svc_ctx->temporal_layers +
296 svc_ctx->temporal_layers - 1];
297 for (tl = 0; tl < svc_ctx->temporal_layers; ++tl) {
298 enc_cfg->ss_target_bitrate[sl * svc_ctx->temporal_layers] +=
299 (unsigned int)si->bitrates[sl * svc_ctx->temporal_layers + tl];
300 enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers + tl] =
301 si->bitrates[sl * svc_ctx->temporal_layers + tl];
302 if (tl > 0 && (si->bitrates[sl * svc_ctx->temporal_layers + tl] <=
303 si->bitrates[sl * svc_ctx->temporal_layers + tl - 1]))
304 return VPX_CODEC_INVALID_PARAM;
305 }
306 }
307 if (total_bitrate != enc_cfg->rc_target_bitrate)
308 return VPX_CODEC_INVALID_PARAM;
309 } else {
310 float total = 0;
311 float alloc_ratio[VPX_MAX_LAYERS] = { 0 };
312
313 for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
314 if (si->svc_params.scaling_factor_den[sl] > 0) {
315 alloc_ratio[sl] = (float)(pow(2, sl));
316 total += alloc_ratio[sl];
317 }
318 }
319
320 for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
321 enc_cfg->ss_target_bitrate[sl] = spatial_layer_target =
322 (unsigned int)(enc_cfg->rc_target_bitrate * alloc_ratio[sl] /
323 total);
324 if (svc_ctx->temporal_layering_mode == 3) {
325 enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers] =
326 (spatial_layer_target * 6) / 10; // 60%
327 enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers + 1] =
328 (spatial_layer_target * 8) / 10; // 80%
329 enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers + 2] =
330 spatial_layer_target;
331 } else if (svc_ctx->temporal_layering_mode == 2 ||
332 svc_ctx->temporal_layering_mode == 1) {
333 enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers] =
334 spatial_layer_target * 2 / 3;
335 enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers + 1] =
336 spatial_layer_target;
337 } else {
338 // User should explicitly assign bitrates in this case.
339 assert(0);
340 }
341 }
342 }
343 } else {
344 if (si->bitrates[0] != 0) {
345 unsigned int total_bitrate = 0;
346 for (i = 0; i < svc_ctx->spatial_layers; ++i) {
347 enc_cfg->ss_target_bitrate[i] = (unsigned int)si->bitrates[i];
348 enc_cfg->layer_target_bitrate[i] = (unsigned int)si->bitrates[i];
349 total_bitrate += si->bitrates[i];
350 }
351 if (total_bitrate != enc_cfg->rc_target_bitrate)
352 return VPX_CODEC_INVALID_PARAM;
353 } else {
354 float total = 0;
355 float alloc_ratio[VPX_MAX_LAYERS] = { 0 };
356
357 for (i = 0; i < svc_ctx->spatial_layers; ++i) {
358 if (si->svc_params.scaling_factor_den[i] > 0) {
359 alloc_ratio[i] = (float)(si->svc_params.scaling_factor_num[i] * 1.0 /
360 si->svc_params.scaling_factor_den[i]);
361
362 alloc_ratio[i] *= alloc_ratio[i];
363 total += alloc_ratio[i];
364 }
365 }
366 for (i = 0; i < VPX_SS_MAX_LAYERS; ++i) {
367 if (total > 0) {
368 enc_cfg->layer_target_bitrate[i] =
369 (unsigned int)(enc_cfg->rc_target_bitrate * alloc_ratio[i] /
370 total);
371 }
372 }
373 }
374 }
375 return VPX_CODEC_OK;
376 }
377
vpx_svc_init(SvcContext * svc_ctx,vpx_codec_ctx_t * codec_ctx,vpx_codec_iface_t * iface,vpx_codec_enc_cfg_t * enc_cfg)378 vpx_codec_err_t vpx_svc_init(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
379 vpx_codec_iface_t *iface,
380 vpx_codec_enc_cfg_t *enc_cfg) {
381 vpx_codec_err_t res;
382 int i, sl, tl;
383 SvcInternal_t *const si = get_svc_internal(svc_ctx);
384 if (svc_ctx == NULL || codec_ctx == NULL || iface == NULL ||
385 enc_cfg == NULL) {
386 return VPX_CODEC_INVALID_PARAM;
387 }
388 if (si == NULL) return VPX_CODEC_MEM_ERROR;
389
390 si->codec_ctx = codec_ctx;
391
392 si->width = enc_cfg->g_w;
393 si->height = enc_cfg->g_h;
394
395 si->kf_dist = enc_cfg->kf_max_dist;
396
397 if (svc_ctx->spatial_layers == 0)
398 svc_ctx->spatial_layers = VPX_SS_DEFAULT_LAYERS;
399 if (svc_ctx->spatial_layers < 1 ||
400 svc_ctx->spatial_layers > VPX_SS_MAX_LAYERS) {
401 svc_log(svc_ctx, SVC_LOG_ERROR, "spatial layers: invalid value: %d\n",
402 svc_ctx->spatial_layers);
403 return VPX_CODEC_INVALID_PARAM;
404 }
405
406 // Note: temporal_layering_mode only applies to one-pass CBR
407 // si->svc_params.temporal_layering_mode = svc_ctx->temporal_layering_mode;
408 if (svc_ctx->temporal_layering_mode == 3) {
409 svc_ctx->temporal_layers = 3;
410 } else if (svc_ctx->temporal_layering_mode == 2 ||
411 svc_ctx->temporal_layering_mode == 1) {
412 svc_ctx->temporal_layers = 2;
413 }
414
415 for (sl = 0; sl < VPX_SS_MAX_LAYERS; ++sl) {
416 si->svc_params.scaling_factor_num[sl] = DEFAULT_SCALE_FACTORS_NUM[sl];
417 si->svc_params.scaling_factor_den[sl] = DEFAULT_SCALE_FACTORS_DEN[sl];
418 si->svc_params.speed_per_layer[sl] = svc_ctx->speed;
419 }
420 if (enc_cfg->rc_end_usage == VPX_CBR && enc_cfg->g_pass == VPX_RC_ONE_PASS &&
421 svc_ctx->spatial_layers <= 3) {
422 for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
423 int sl2 = (svc_ctx->spatial_layers == 2) ? sl + 1 : sl;
424 si->svc_params.scaling_factor_num[sl] = DEFAULT_SCALE_FACTORS_NUM_2x[sl2];
425 si->svc_params.scaling_factor_den[sl] = DEFAULT_SCALE_FACTORS_DEN_2x[sl2];
426 }
427 if (svc_ctx->spatial_layers == 1) {
428 si->svc_params.scaling_factor_num[0] = 1;
429 si->svc_params.scaling_factor_den[0] = 1;
430 }
431 }
432 for (tl = 0; tl < svc_ctx->temporal_layers; ++tl) {
433 for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
434 i = sl * svc_ctx->temporal_layers + tl;
435 si->svc_params.max_quantizers[i] = MAX_QUANTIZER;
436 si->svc_params.min_quantizers[i] = 0;
437 if (enc_cfg->rc_end_usage == VPX_CBR &&
438 enc_cfg->g_pass == VPX_RC_ONE_PASS) {
439 si->svc_params.max_quantizers[i] = 56;
440 si->svc_params.min_quantizers[i] = 2;
441 }
442 }
443 }
444
445 // Parse aggregate command line options. Options must start with
446 // "layers=xx" then followed by other options
447 res = parse_options(svc_ctx, si->options);
448 if (res != VPX_CODEC_OK) return res;
449
450 if (svc_ctx->spatial_layers < 1) svc_ctx->spatial_layers = 1;
451 if (svc_ctx->spatial_layers > VPX_SS_MAX_LAYERS)
452 svc_ctx->spatial_layers = VPX_SS_MAX_LAYERS;
453
454 if (svc_ctx->temporal_layers < 1) svc_ctx->temporal_layers = 1;
455 if (svc_ctx->temporal_layers > VPX_TS_MAX_LAYERS)
456 svc_ctx->temporal_layers = VPX_TS_MAX_LAYERS;
457
458 if (svc_ctx->temporal_layers * svc_ctx->spatial_layers > VPX_MAX_LAYERS) {
459 svc_log(svc_ctx, SVC_LOG_ERROR,
460 "spatial layers * temporal layers exceeds the maximum number of "
461 "allowed layers of %d\n",
462 svc_ctx->spatial_layers * svc_ctx->temporal_layers, VPX_MAX_LAYERS);
463 return VPX_CODEC_INVALID_PARAM;
464 }
465 res = assign_layer_bitrates(svc_ctx, enc_cfg);
466 if (res != VPX_CODEC_OK) {
467 svc_log(svc_ctx, SVC_LOG_ERROR,
468 "layer bitrates incorrect: \n"
469 "1) spatial layer bitrates should sum up to target \n"
470 "2) temporal layer bitrates should be increasing within \n"
471 "a spatial layer \n");
472 return VPX_CODEC_INVALID_PARAM;
473 }
474
475 if (svc_ctx->temporal_layers > 1) {
476 int i;
477 for (i = 0; i < svc_ctx->temporal_layers; ++i) {
478 enc_cfg->ts_target_bitrate[i] =
479 enc_cfg->rc_target_bitrate / svc_ctx->temporal_layers;
480 enc_cfg->ts_rate_decimator[i] = 1 << (svc_ctx->temporal_layers - 1 - i);
481 }
482 }
483
484 if (svc_ctx->threads) enc_cfg->g_threads = svc_ctx->threads;
485
486 // Modify encoder configuration
487 enc_cfg->ss_number_layers = svc_ctx->spatial_layers;
488 enc_cfg->ts_number_layers = svc_ctx->temporal_layers;
489
490 if (enc_cfg->rc_end_usage == VPX_CBR) {
491 enc_cfg->rc_resize_allowed = 0;
492 enc_cfg->rc_min_quantizer = 2;
493 enc_cfg->rc_max_quantizer = 56;
494 enc_cfg->rc_undershoot_pct = 50;
495 enc_cfg->rc_overshoot_pct = 50;
496 enc_cfg->rc_buf_initial_sz = 500;
497 enc_cfg->rc_buf_optimal_sz = 600;
498 enc_cfg->rc_buf_sz = 1000;
499 }
500
501 for (tl = 0; tl < svc_ctx->temporal_layers; ++tl) {
502 for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
503 i = sl * svc_ctx->temporal_layers + tl;
504 if (enc_cfg->rc_end_usage == VPX_CBR &&
505 enc_cfg->g_pass == VPX_RC_ONE_PASS) {
506 si->svc_params.max_quantizers[i] = enc_cfg->rc_max_quantizer;
507 si->svc_params.min_quantizers[i] = enc_cfg->rc_min_quantizer;
508 }
509 }
510 }
511
512 if (enc_cfg->g_error_resilient == 0 && si->use_multiple_frame_contexts == 0)
513 enc_cfg->g_error_resilient = 1;
514
515 // Initialize codec
516 res = vpx_codec_enc_init(codec_ctx, iface, enc_cfg, VPX_CODEC_USE_PSNR);
517 if (res != VPX_CODEC_OK) {
518 svc_log(svc_ctx, SVC_LOG_ERROR, "svc_enc_init error\n");
519 return res;
520 }
521 if (svc_ctx->spatial_layers > 1 || svc_ctx->temporal_layers > 1) {
522 vpx_codec_control(codec_ctx, VP9E_SET_SVC, 1);
523 vpx_codec_control(codec_ctx, VP9E_SET_SVC_PARAMETERS, &si->svc_params);
524 }
525 return VPX_CODEC_OK;
526 }
527
528 /**
529 * Encode a frame into multiple layers
530 * Create a superframe containing the individual layers
531 */
vpx_svc_encode(SvcContext * svc_ctx,vpx_codec_ctx_t * codec_ctx,struct vpx_image * rawimg,vpx_codec_pts_t pts,int64_t duration,int deadline)532 vpx_codec_err_t vpx_svc_encode(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
533 struct vpx_image *rawimg, vpx_codec_pts_t pts,
534 int64_t duration, int deadline) {
535 vpx_codec_err_t res;
536 vpx_codec_iter_t iter;
537 const vpx_codec_cx_pkt_t *cx_pkt;
538 SvcInternal_t *const si = get_svc_internal(svc_ctx);
539 if (svc_ctx == NULL || codec_ctx == NULL || si == NULL) {
540 return VPX_CODEC_INVALID_PARAM;
541 }
542
543 res =
544 vpx_codec_encode(codec_ctx, rawimg, pts, (uint32_t)duration, 0, deadline);
545 if (res != VPX_CODEC_OK) {
546 return res;
547 }
548 // save compressed data
549 iter = NULL;
550 while ((cx_pkt = vpx_codec_get_cx_data(codec_ctx, &iter))) {
551 switch (cx_pkt->kind) {
552 case VPX_CODEC_PSNR_PKT: {
553 }
554 ++si->psnr_pkt_received;
555 break;
556 default: { break; }
557 }
558 }
559
560 return VPX_CODEC_OK;
561 }
562
calc_psnr(double d)563 static double calc_psnr(double d) {
564 if (d == 0) return 100;
565 return -10.0 * log(d) / log(10.0);
566 }
567
568 // dump accumulated statistics and reset accumulated values
vpx_svc_dump_statistics(SvcContext * svc_ctx)569 void vpx_svc_dump_statistics(SvcContext *svc_ctx) {
570 int number_of_frames;
571 int i, j;
572 uint32_t bytes_total = 0;
573 double scale[COMPONENTS];
574 double psnr[COMPONENTS];
575 double mse[COMPONENTS];
576 double y_scale;
577
578 SvcInternal_t *const si = get_svc_internal(svc_ctx);
579 if (svc_ctx == NULL || si == NULL) return;
580
581 number_of_frames = si->psnr_pkt_received;
582 if (number_of_frames <= 0) return;
583
584 svc_log(svc_ctx, SVC_LOG_INFO, "\n");
585 for (i = 0; i < svc_ctx->spatial_layers; ++i) {
586 svc_log(svc_ctx, SVC_LOG_INFO,
587 "Layer %d Average PSNR=[%2.3f, %2.3f, %2.3f, %2.3f], Bytes=[%u]\n",
588 i, si->psnr_sum[i][0] / number_of_frames,
589 si->psnr_sum[i][1] / number_of_frames,
590 si->psnr_sum[i][2] / number_of_frames,
591 si->psnr_sum[i][3] / number_of_frames, si->bytes_sum[i]);
592 // the following psnr calculation is deduced from ffmpeg.c#print_report
593 y_scale = si->width * si->height * 255.0 * 255.0 * number_of_frames;
594 scale[1] = y_scale;
595 scale[2] = scale[3] = y_scale / 4; // U or V
596 scale[0] = y_scale * 1.5; // total
597
598 for (j = 0; j < COMPONENTS; j++) {
599 psnr[j] = calc_psnr(si->sse_sum[i][j] / scale[j]);
600 mse[j] = si->sse_sum[i][j] * 255.0 * 255.0 / scale[j];
601 }
602 svc_log(svc_ctx, SVC_LOG_INFO,
603 "Layer %d Overall PSNR=[%2.3f, %2.3f, %2.3f, %2.3f]\n", i, psnr[0],
604 psnr[1], psnr[2], psnr[3]);
605 svc_log(svc_ctx, SVC_LOG_INFO,
606 "Layer %d Overall MSE=[%2.3f, %2.3f, %2.3f, %2.3f]\n", i, mse[0],
607 mse[1], mse[2], mse[3]);
608
609 bytes_total += si->bytes_sum[i];
610 // Clear sums for next time.
611 si->bytes_sum[i] = 0;
612 for (j = 0; j < COMPONENTS; ++j) {
613 si->psnr_sum[i][j] = 0;
614 si->sse_sum[i][j] = 0;
615 }
616 }
617
618 // only display statistics once
619 si->psnr_pkt_received = 0;
620
621 svc_log(svc_ctx, SVC_LOG_INFO, "Total Bytes=[%u]\n", bytes_total);
622 }
623
vpx_svc_release(SvcContext * svc_ctx)624 void vpx_svc_release(SvcContext *svc_ctx) {
625 SvcInternal_t *si;
626 if (svc_ctx == NULL) return;
627 // do not use get_svc_internal as it will unnecessarily allocate an
628 // SvcInternal_t if it was not already allocated
629 si = (SvcInternal_t *)svc_ctx->internal;
630 if (si != NULL) {
631 free(si);
632 svc_ctx->internal = NULL;
633 }
634 }
635