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 token = strtok_r(input_string, delim, &save_ptr);
159 for (i = 0; i < num_layers; ++i) {
160 if (token != NULL) {
161 res = extract_option(type, token, option0 + i, option1 + i);
162 if (res != VPX_CODEC_OK) break;
163 token = strtok_r(NULL, delim, &save_ptr);
164 } else {
165 break;
166 }
167 }
168 if (res == VPX_CODEC_OK && i != num_layers) {
169 svc_log(svc_ctx, SVC_LOG_ERROR,
170 "svc: layer params type: %d %d values required, "
171 "but only %d specified\n",
172 type, num_layers, i);
173 res = VPX_CODEC_INVALID_PARAM;
174 }
175 free(input_string);
176 return res;
177 }
178
179 /**
180 * Parse SVC encoding options
181 * Format: encoding-mode=<svc_mode>,layers=<layer_count>
182 * scale-factors=<n1>/<d1>,<n2>/<d2>,...
183 * quantizers=<q1>,<q2>,...
184 * svc_mode = [i|ip|alt_ip|gf]
185 */
parse_options(SvcContext * svc_ctx,const char * options)186 static vpx_codec_err_t parse_options(SvcContext *svc_ctx, const char *options) {
187 char *input_string;
188 char *option_name;
189 char *option_value;
190 char *input_ptr = NULL;
191 SvcInternal_t *const si = get_svc_internal(svc_ctx);
192 vpx_codec_err_t res = VPX_CODEC_OK;
193 int i, alt_ref_enabled = 0;
194
195 if (options == NULL) return VPX_CODEC_OK;
196 input_string = strdup(options);
197
198 // parse option name
199 option_name = strtok_r(input_string, "=", &input_ptr);
200 while (option_name != NULL) {
201 // parse option value
202 option_value = strtok_r(NULL, " ", &input_ptr);
203 if (option_value == NULL) {
204 svc_log(svc_ctx, SVC_LOG_ERROR, "option missing value: %s\n",
205 option_name);
206 res = VPX_CODEC_INVALID_PARAM;
207 break;
208 }
209 if (strcmp("spatial-layers", option_name) == 0) {
210 svc_ctx->spatial_layers = atoi(option_value);
211 } else if (strcmp("temporal-layers", option_name) == 0) {
212 svc_ctx->temporal_layers = atoi(option_value);
213 } else if (strcmp("scale-factors", option_name) == 0) {
214 res = parse_layer_options_from_string(svc_ctx, SCALE_FACTOR, option_value,
215 si->svc_params.scaling_factor_num,
216 si->svc_params.scaling_factor_den);
217 if (res != VPX_CODEC_OK) break;
218 } else if (strcmp("max-quantizers", option_name) == 0) {
219 res =
220 parse_layer_options_from_string(svc_ctx, QUANTIZER, option_value,
221 si->svc_params.max_quantizers, NULL);
222 if (res != VPX_CODEC_OK) break;
223 } else if (strcmp("min-quantizers", option_name) == 0) {
224 res =
225 parse_layer_options_from_string(svc_ctx, QUANTIZER, option_value,
226 si->svc_params.min_quantizers, NULL);
227 if (res != VPX_CODEC_OK) break;
228 } else if (strcmp("auto-alt-refs", option_name) == 0) {
229 res = parse_layer_options_from_string(svc_ctx, AUTO_ALT_REF, option_value,
230 si->enable_auto_alt_ref, NULL);
231 if (res != VPX_CODEC_OK) break;
232 } else if (strcmp("bitrates", option_name) == 0) {
233 res = parse_layer_options_from_string(svc_ctx, BITRATE, option_value,
234 si->bitrates, NULL);
235 if (res != VPX_CODEC_OK) break;
236 } else if (strcmp("multi-frame-contexts", option_name) == 0) {
237 si->use_multiple_frame_contexts = atoi(option_value);
238 } else {
239 svc_log(svc_ctx, SVC_LOG_ERROR, "invalid option: %s\n", option_name);
240 res = VPX_CODEC_INVALID_PARAM;
241 break;
242 }
243 option_name = strtok_r(NULL, "=", &input_ptr);
244 }
245 free(input_string);
246
247 for (i = 0; i < svc_ctx->spatial_layers; ++i) {
248 if (si->svc_params.max_quantizers[i] > MAX_QUANTIZER ||
249 si->svc_params.max_quantizers[i] < 0 ||
250 si->svc_params.min_quantizers[i] > si->svc_params.max_quantizers[i] ||
251 si->svc_params.min_quantizers[i] < 0)
252 res = VPX_CODEC_INVALID_PARAM;
253 }
254
255 if (si->use_multiple_frame_contexts &&
256 (svc_ctx->spatial_layers > 3 ||
257 svc_ctx->spatial_layers * svc_ctx->temporal_layers > 4))
258 res = VPX_CODEC_INVALID_PARAM;
259
260 for (i = 0; i < svc_ctx->spatial_layers; ++i)
261 alt_ref_enabled += si->enable_auto_alt_ref[i];
262 if (alt_ref_enabled > REF_FRAMES - svc_ctx->spatial_layers) {
263 svc_log(svc_ctx, SVC_LOG_ERROR,
264 "svc: auto alt ref: Maxinum %d(REF_FRAMES - layers) layers could"
265 "enabled auto alt reference frame, but % layers are enabled\n",
266 REF_FRAMES - svc_ctx->spatial_layers, alt_ref_enabled);
267 res = VPX_CODEC_INVALID_PARAM;
268 }
269
270 return res;
271 }
272
vpx_svc_set_options(SvcContext * svc_ctx,const char * options)273 vpx_codec_err_t vpx_svc_set_options(SvcContext *svc_ctx, const char *options) {
274 SvcInternal_t *const si = get_svc_internal(svc_ctx);
275 if (svc_ctx == NULL || options == NULL || si == NULL) {
276 return VPX_CODEC_INVALID_PARAM;
277 }
278 strncpy(si->options, options, sizeof(si->options));
279 si->options[sizeof(si->options) - 1] = '\0';
280 return VPX_CODEC_OK;
281 }
282
assign_layer_bitrates(const SvcContext * svc_ctx,vpx_codec_enc_cfg_t * const enc_cfg)283 static vpx_codec_err_t assign_layer_bitrates(
284 const SvcContext *svc_ctx, vpx_codec_enc_cfg_t *const enc_cfg) {
285 int i;
286 const SvcInternal_t *const si = get_const_svc_internal(svc_ctx);
287 int sl, tl, spatial_layer_target;
288
289 if (svc_ctx->temporal_layering_mode != 0) {
290 if (si->bitrates[0] != 0) {
291 unsigned int total_bitrate = 0;
292 for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
293 total_bitrate += si->bitrates[sl * svc_ctx->temporal_layers +
294 svc_ctx->temporal_layers - 1];
295 for (tl = 0; tl < svc_ctx->temporal_layers; ++tl) {
296 enc_cfg->ss_target_bitrate[sl * svc_ctx->temporal_layers] +=
297 (unsigned int)si->bitrates[sl * svc_ctx->temporal_layers + tl];
298 enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers + tl] =
299 si->bitrates[sl * svc_ctx->temporal_layers + tl];
300 if (tl > 0 && (si->bitrates[sl * svc_ctx->temporal_layers + tl] <=
301 si->bitrates[sl * svc_ctx->temporal_layers + tl - 1]))
302 return VPX_CODEC_INVALID_PARAM;
303 }
304 }
305 if (total_bitrate != enc_cfg->rc_target_bitrate)
306 return VPX_CODEC_INVALID_PARAM;
307 } else {
308 float total = 0;
309 float alloc_ratio[VPX_MAX_LAYERS] = { 0 };
310
311 for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
312 if (si->svc_params.scaling_factor_den[sl] > 0) {
313 alloc_ratio[sl] = (float)(pow(2, sl));
314 total += alloc_ratio[sl];
315 }
316 }
317
318 for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
319 enc_cfg->ss_target_bitrate[sl] = spatial_layer_target =
320 (unsigned int)(enc_cfg->rc_target_bitrate * alloc_ratio[sl] /
321 total);
322 if (svc_ctx->temporal_layering_mode == 3) {
323 enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers] =
324 (spatial_layer_target * 6) / 10; // 60%
325 enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers + 1] =
326 (spatial_layer_target * 8) / 10; // 80%
327 enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers + 2] =
328 spatial_layer_target;
329 } else if (svc_ctx->temporal_layering_mode == 2 ||
330 svc_ctx->temporal_layering_mode == 1) {
331 enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers] =
332 spatial_layer_target * 2 / 3;
333 enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers + 1] =
334 spatial_layer_target;
335 } else {
336 // User should explicitly assign bitrates in this case.
337 assert(0);
338 }
339 }
340 }
341 } else {
342 if (si->bitrates[0] != 0) {
343 unsigned int total_bitrate = 0;
344 for (i = 0; i < svc_ctx->spatial_layers; ++i) {
345 enc_cfg->ss_target_bitrate[i] = (unsigned int)si->bitrates[i];
346 enc_cfg->layer_target_bitrate[i] = (unsigned int)si->bitrates[i];
347 total_bitrate += si->bitrates[i];
348 }
349 if (total_bitrate != enc_cfg->rc_target_bitrate)
350 return VPX_CODEC_INVALID_PARAM;
351 } else {
352 float total = 0;
353 float alloc_ratio[VPX_MAX_LAYERS] = { 0 };
354
355 for (i = 0; i < svc_ctx->spatial_layers; ++i) {
356 if (si->svc_params.scaling_factor_den[i] > 0) {
357 alloc_ratio[i] = (float)(si->svc_params.scaling_factor_num[i] * 1.0 /
358 si->svc_params.scaling_factor_den[i]);
359
360 alloc_ratio[i] *= alloc_ratio[i];
361 total += alloc_ratio[i];
362 }
363 }
364 for (i = 0; i < VPX_SS_MAX_LAYERS; ++i) {
365 if (total > 0) {
366 enc_cfg->layer_target_bitrate[i] =
367 (unsigned int)(enc_cfg->rc_target_bitrate * alloc_ratio[i] /
368 total);
369 }
370 }
371 }
372 }
373 return VPX_CODEC_OK;
374 }
375
vpx_svc_init(SvcContext * svc_ctx,vpx_codec_ctx_t * codec_ctx,vpx_codec_iface_t * iface,vpx_codec_enc_cfg_t * enc_cfg)376 vpx_codec_err_t vpx_svc_init(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
377 vpx_codec_iface_t *iface,
378 vpx_codec_enc_cfg_t *enc_cfg) {
379 vpx_codec_err_t res;
380 int i, sl, tl;
381 SvcInternal_t *const si = get_svc_internal(svc_ctx);
382 if (svc_ctx == NULL || codec_ctx == NULL || iface == NULL ||
383 enc_cfg == NULL) {
384 return VPX_CODEC_INVALID_PARAM;
385 }
386 if (si == NULL) return VPX_CODEC_MEM_ERROR;
387
388 si->codec_ctx = codec_ctx;
389
390 si->width = enc_cfg->g_w;
391 si->height = enc_cfg->g_h;
392
393 si->kf_dist = enc_cfg->kf_max_dist;
394
395 if (svc_ctx->spatial_layers == 0)
396 svc_ctx->spatial_layers = VPX_SS_DEFAULT_LAYERS;
397 if (svc_ctx->spatial_layers < 1 ||
398 svc_ctx->spatial_layers > VPX_SS_MAX_LAYERS) {
399 svc_log(svc_ctx, SVC_LOG_ERROR, "spatial layers: invalid value: %d\n",
400 svc_ctx->spatial_layers);
401 return VPX_CODEC_INVALID_PARAM;
402 }
403
404 // Note: temporal_layering_mode only applies to one-pass CBR
405 // si->svc_params.temporal_layering_mode = svc_ctx->temporal_layering_mode;
406 if (svc_ctx->temporal_layering_mode == 3) {
407 svc_ctx->temporal_layers = 3;
408 } else if (svc_ctx->temporal_layering_mode == 2 ||
409 svc_ctx->temporal_layering_mode == 1) {
410 svc_ctx->temporal_layers = 2;
411 }
412
413 for (sl = 0; sl < VPX_SS_MAX_LAYERS; ++sl) {
414 si->svc_params.scaling_factor_num[sl] = DEFAULT_SCALE_FACTORS_NUM[sl];
415 si->svc_params.scaling_factor_den[sl] = DEFAULT_SCALE_FACTORS_DEN[sl];
416 si->svc_params.speed_per_layer[sl] = svc_ctx->speed;
417 }
418 if (enc_cfg->rc_end_usage == VPX_CBR && enc_cfg->g_pass == VPX_RC_ONE_PASS &&
419 svc_ctx->spatial_layers <= 3) {
420 for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
421 int sl2 = (svc_ctx->spatial_layers == 2) ? sl + 1 : sl;
422 si->svc_params.scaling_factor_num[sl] = DEFAULT_SCALE_FACTORS_NUM_2x[sl2];
423 si->svc_params.scaling_factor_den[sl] = DEFAULT_SCALE_FACTORS_DEN_2x[sl2];
424 }
425 if (svc_ctx->spatial_layers == 1) {
426 si->svc_params.scaling_factor_num[0] = 1;
427 si->svc_params.scaling_factor_den[0] = 1;
428 }
429 }
430 for (tl = 0; tl < svc_ctx->temporal_layers; ++tl) {
431 for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
432 i = sl * svc_ctx->temporal_layers + tl;
433 si->svc_params.max_quantizers[i] = MAX_QUANTIZER;
434 si->svc_params.min_quantizers[i] = 0;
435 if (enc_cfg->rc_end_usage == VPX_CBR &&
436 enc_cfg->g_pass == VPX_RC_ONE_PASS) {
437 si->svc_params.max_quantizers[i] = 56;
438 si->svc_params.min_quantizers[i] = 2;
439 }
440 }
441 }
442
443 // Parse aggregate command line options. Options must start with
444 // "layers=xx" then followed by other options
445 res = parse_options(svc_ctx, si->options);
446 if (res != VPX_CODEC_OK) return res;
447
448 if (svc_ctx->spatial_layers < 1) svc_ctx->spatial_layers = 1;
449 if (svc_ctx->spatial_layers > VPX_SS_MAX_LAYERS)
450 svc_ctx->spatial_layers = VPX_SS_MAX_LAYERS;
451
452 if (svc_ctx->temporal_layers < 1) svc_ctx->temporal_layers = 1;
453 if (svc_ctx->temporal_layers > VPX_TS_MAX_LAYERS)
454 svc_ctx->temporal_layers = VPX_TS_MAX_LAYERS;
455
456 if (svc_ctx->temporal_layers * svc_ctx->spatial_layers > VPX_MAX_LAYERS) {
457 svc_log(svc_ctx, SVC_LOG_ERROR,
458 "spatial layers * temporal layers exceeds the maximum number of "
459 "allowed layers of %d\n",
460 svc_ctx->spatial_layers * svc_ctx->temporal_layers,
461 (int)VPX_MAX_LAYERS);
462 return VPX_CODEC_INVALID_PARAM;
463 }
464 res = assign_layer_bitrates(svc_ctx, enc_cfg);
465 if (res != VPX_CODEC_OK) {
466 svc_log(svc_ctx, SVC_LOG_ERROR,
467 "layer bitrates incorrect: \n"
468 "1) spatial layer bitrates should sum up to target \n"
469 "2) temporal layer bitrates should be increasing within \n"
470 "a spatial layer \n");
471 return VPX_CODEC_INVALID_PARAM;
472 }
473
474 if (svc_ctx->temporal_layers > 1) {
475 int i;
476 for (i = 0; i < svc_ctx->temporal_layers; ++i) {
477 enc_cfg->ts_target_bitrate[i] =
478 enc_cfg->rc_target_bitrate / svc_ctx->temporal_layers;
479 enc_cfg->ts_rate_decimator[i] = 1 << (svc_ctx->temporal_layers - 1 - i);
480 }
481 }
482
483 if (svc_ctx->threads) enc_cfg->g_threads = svc_ctx->threads;
484
485 // Modify encoder configuration
486 enc_cfg->ss_number_layers = svc_ctx->spatial_layers;
487 enc_cfg->ts_number_layers = svc_ctx->temporal_layers;
488
489 if (enc_cfg->rc_end_usage == VPX_CBR) {
490 enc_cfg->rc_resize_allowed = 0;
491 enc_cfg->rc_min_quantizer = 2;
492 enc_cfg->rc_max_quantizer = 56;
493 enc_cfg->rc_undershoot_pct = 50;
494 enc_cfg->rc_overshoot_pct = 50;
495 enc_cfg->rc_buf_initial_sz = 500;
496 enc_cfg->rc_buf_optimal_sz = 600;
497 enc_cfg->rc_buf_sz = 1000;
498 }
499
500 for (tl = 0; tl < svc_ctx->temporal_layers; ++tl) {
501 for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
502 i = sl * svc_ctx->temporal_layers + tl;
503 if (enc_cfg->rc_end_usage == VPX_CBR &&
504 enc_cfg->g_pass == VPX_RC_ONE_PASS) {
505 si->svc_params.max_quantizers[i] = enc_cfg->rc_max_quantizer;
506 si->svc_params.min_quantizers[i] = enc_cfg->rc_min_quantizer;
507 }
508 }
509 }
510
511 if (enc_cfg->g_error_resilient == 0 && si->use_multiple_frame_contexts == 0)
512 enc_cfg->g_error_resilient = 1;
513
514 // Initialize codec
515 res = vpx_codec_enc_init(codec_ctx, iface, enc_cfg, VPX_CODEC_USE_PSNR);
516 if (res != VPX_CODEC_OK) {
517 svc_log(svc_ctx, SVC_LOG_ERROR, "svc_enc_init error\n");
518 return res;
519 }
520 if (svc_ctx->spatial_layers > 1 || svc_ctx->temporal_layers > 1) {
521 vpx_codec_control(codec_ctx, VP9E_SET_SVC, 1);
522 vpx_codec_control(codec_ctx, VP9E_SET_SVC_PARAMETERS, &si->svc_params);
523 }
524 return VPX_CODEC_OK;
525 }
526
527 /**
528 * Encode a frame into multiple layers
529 * Create a superframe containing the individual layers
530 */
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)531 vpx_codec_err_t vpx_svc_encode(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
532 struct vpx_image *rawimg, vpx_codec_pts_t pts,
533 int64_t duration, int deadline) {
534 vpx_codec_err_t res;
535 vpx_codec_iter_t iter;
536 const vpx_codec_cx_pkt_t *cx_pkt;
537 SvcInternal_t *const si = get_svc_internal(svc_ctx);
538 if (svc_ctx == NULL || codec_ctx == NULL || si == NULL) {
539 return VPX_CODEC_INVALID_PARAM;
540 }
541
542 res =
543 vpx_codec_encode(codec_ctx, rawimg, pts, (uint32_t)duration, 0, deadline);
544 if (res != VPX_CODEC_OK) {
545 return res;
546 }
547 // save compressed data
548 iter = NULL;
549 while ((cx_pkt = vpx_codec_get_cx_data(codec_ctx, &iter))) {
550 switch (cx_pkt->kind) {
551 case VPX_CODEC_PSNR_PKT: {
552 }
553 ++si->psnr_pkt_received;
554 break;
555 default: { break; }
556 }
557 }
558
559 return VPX_CODEC_OK;
560 }
561
calc_psnr(double d)562 static double calc_psnr(double d) {
563 if (d == 0) return 100;
564 return -10.0 * log(d) / log(10.0);
565 }
566
567 // dump accumulated statistics and reset accumulated values
vpx_svc_dump_statistics(SvcContext * svc_ctx)568 void vpx_svc_dump_statistics(SvcContext *svc_ctx) {
569 int number_of_frames;
570 int i, j;
571 uint32_t bytes_total = 0;
572 double scale[COMPONENTS];
573 double psnr[COMPONENTS];
574 double mse[COMPONENTS];
575 double y_scale;
576
577 SvcInternal_t *const si = get_svc_internal(svc_ctx);
578 if (svc_ctx == NULL || si == NULL) return;
579
580 number_of_frames = si->psnr_pkt_received;
581 if (number_of_frames <= 0) return;
582
583 svc_log(svc_ctx, SVC_LOG_INFO, "\n");
584 for (i = 0; i < svc_ctx->spatial_layers; ++i) {
585 svc_log(svc_ctx, SVC_LOG_INFO,
586 "Layer %d Average PSNR=[%2.3f, %2.3f, %2.3f, %2.3f], Bytes=[%u]\n",
587 i, (double)si->psnr_sum[i][0] / number_of_frames,
588 (double)si->psnr_sum[i][1] / number_of_frames,
589 (double)si->psnr_sum[i][2] / number_of_frames,
590 (double)si->psnr_sum[i][3] / number_of_frames, si->bytes_sum[i]);
591 // the following psnr calculation is deduced from ffmpeg.c#print_report
592 y_scale = si->width * si->height * 255.0 * 255.0 * number_of_frames;
593 scale[1] = y_scale;
594 scale[2] = scale[3] = y_scale / 4; // U or V
595 scale[0] = y_scale * 1.5; // total
596
597 for (j = 0; j < COMPONENTS; j++) {
598 psnr[j] = calc_psnr(si->sse_sum[i][j] / scale[j]);
599 mse[j] = si->sse_sum[i][j] * 255.0 * 255.0 / scale[j];
600 }
601 svc_log(svc_ctx, SVC_LOG_INFO,
602 "Layer %d Overall PSNR=[%2.3f, %2.3f, %2.3f, %2.3f]\n", i, psnr[0],
603 psnr[1], psnr[2], psnr[3]);
604 svc_log(svc_ctx, SVC_LOG_INFO,
605 "Layer %d Overall MSE=[%2.3f, %2.3f, %2.3f, %2.3f]\n", i, mse[0],
606 mse[1], mse[2], mse[3]);
607
608 bytes_total += si->bytes_sum[i];
609 // Clear sums for next time.
610 si->bytes_sum[i] = 0;
611 for (j = 0; j < COMPONENTS; ++j) {
612 si->psnr_sum[i][j] = 0;
613 si->sse_sum[i][j] = 0;
614 }
615 }
616
617 // only display statistics once
618 si->psnr_pkt_received = 0;
619
620 svc_log(svc_ctx, SVC_LOG_INFO, "Total Bytes=[%u]\n", bytes_total);
621 }
622
vpx_svc_release(SvcContext * svc_ctx)623 void vpx_svc_release(SvcContext *svc_ctx) {
624 SvcInternal_t *si;
625 if (svc_ctx == NULL) return;
626 // do not use get_svc_internal as it will unnecessarily allocate an
627 // SvcInternal_t if it was not already allocated
628 si = (SvcInternal_t *)svc_ctx->internal;
629 if (si != NULL) {
630 free(si);
631 svc_ctx->internal = NULL;
632 }
633 }
634