2 * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
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.
13 * VP9 SVC encoding support via libvpx
20 #define VPX_DISABLE_CTRL_TYPECHECKS 1
21 #define VPX_CODEC_DISABLE_COMPAT 1
22 #include "vpx/svc_context.h"
23 #include "vpx/vp8cx.h"
24 #include "vpx/vpx_encoder.h"
27 #define strtok_r strtok_s
28 #ifndef MINGW_HAS_SECURE_API
29 // proto from /usr/x86_64-w64-mingw32/include/sec_api/string_s.h
30 _CRTIMP char *__cdecl strtok_s(char *str, const char *delim, char **context);
31 #endif /* MINGW_HAS_SECURE_API */
32 #endif /* __MINGW32__ */
35 #define strdup _strdup
36 #define strtok_r strtok_s
39 #define SVC_REFERENCE_FRAMES 8
40 #define SUPERFRAME_SLOTS (8)
41 #define SUPERFRAME_BUFFER_SIZE (SUPERFRAME_SLOTS * sizeof(uint32_t) + 2)
42 #define OPTION_BUFFER_SIZE 256
44 static const char *DEFAULT_QUANTIZER_VALUES = "60,53,39,33,27";
45 static const char *DEFAULT_SCALE_FACTORS = "4/16,5/16,7/16,11/16,16/16";
47 typedef struct SvcInternal {
48 char options[OPTION_BUFFER_SIZE]; // set by vpx_svc_set_options
49 char quantizers[OPTION_BUFFER_SIZE]; // set by vpx_svc_set_quantizers
50 char scale_factors[OPTION_BUFFER_SIZE]; // set by vpx_svc_set_scale_factors
52 // values extracted from option, quantizers
53 int scaling_factor_num[VPX_SS_MAX_LAYERS];
54 int scaling_factor_den[VPX_SS_MAX_LAYERS];
55 int quantizer[VPX_SS_MAX_LAYERS];
57 // accumulated statistics
58 double psnr_in_layer[VPX_SS_MAX_LAYERS];
59 uint32_t bytes_in_layer[VPX_SS_MAX_LAYERS];
61 // codec encoding values
62 int width; // width of highest layer
63 int height; // height of highest layer
64 int kf_dist; // distance between keyframes
67 int encode_frame_count;
69 vpx_enc_frame_flags_t enc_frame_flags;
78 char message_buffer[2048];
79 vpx_codec_ctx_t *codec_ctx;
82 // Superframe is used to generate an index of individual frames (i.e., layers)
85 uint32_t sizes[SUPERFRAME_SLOTS];
87 uint8_t buffer[SUPERFRAME_BUFFER_SIZE];
91 // One encoded frame layer
93 void *buf; // compressed data buffer
94 size_t size; // length of compressed data
95 struct LayerData *next;
98 // create LayerData from encoder output
99 static struct LayerData *ld_create(void *buf, size_t size) {
100 struct LayerData *const layer_data =
101 (struct LayerData *)malloc(sizeof(*layer_data));
102 if (layer_data == NULL) {
105 layer_data->buf = malloc(size);
106 if (layer_data->buf == NULL) {
110 memcpy(layer_data->buf, buf, size);
111 layer_data->size = size;
116 static void ld_free(struct LayerData *layer_data) {
118 if (layer_data->buf) {
119 free(layer_data->buf);
120 layer_data->buf = NULL;
126 // add layer data to list
127 static void ld_list_add(struct LayerData **list, struct LayerData *layer_data) {
128 struct LayerData **p = list;
130 while (*p != NULL) p = &(*p)->next;
132 layer_data->next = NULL;
135 // get accumulated size of layer data
136 static size_t ld_list_get_buffer_size(struct LayerData *list) {
140 for (p = list; p != NULL; p = p->next) {
146 // copy layer data to buffer
147 static void ld_list_copy_to_buffer(struct LayerData *list, uint8_t *buffer) {
150 for (p = list; p != NULL; p = p->next) {
152 memcpy(buffer, p->buf, p->size);
157 // free layer data list
158 static void ld_list_free(struct LayerData *list) {
159 struct LayerData *p = list;
168 static void sf_create_index(struct Superframe *sf) {
169 uint8_t marker = 0xc0;
174 if (sf->count == 0 || sf->count >= 8) return;
176 // Add the number of frames to the marker byte
177 marker |= sf->count - 1;
179 // Choose the magnitude
180 for (mag = 0, mask = 0xff; mag < 4; ++mag) {
181 if (sf->magnitude < mask) break;
188 sf->index_size = 2 + (mag + 1) * sf->count;
192 for (i = 0; i < sf->count; ++i) {
193 int this_sz = sf->sizes[i];
196 for (j = 0; j <= mag; ++j) {
197 *bufp++ = this_sz & 0xff;
204 static SvcInternal *get_svc_internal(SvcContext *svc_ctx) {
205 if (svc_ctx == NULL) return NULL;
206 if (svc_ctx->internal == NULL) {
207 SvcInternal *const si = (SvcInternal *)malloc(sizeof(*si));
209 memset(si, 0, sizeof(*si));
211 svc_ctx->internal = si;
213 return (SvcInternal *)svc_ctx->internal;
216 static const SvcInternal *get_const_svc_internal(const SvcContext *svc_ctx) {
217 if (svc_ctx == NULL) return NULL;
218 return (const SvcInternal *)svc_ctx->internal;
221 static void svc_log_reset(SvcContext *svc_ctx) {
222 SvcInternal *const si = (SvcInternal *)svc_ctx->internal;
223 si->message_buffer[0] = '\0';
226 static int svc_log(SvcContext *svc_ctx, int level, const char *fmt, ...) {
230 SvcInternal *const si = get_svc_internal(svc_ctx);
232 if (level > svc_ctx->log_level) {
237 retval = vsnprintf(buf, sizeof(buf), fmt, ap);
240 if (svc_ctx->log_print) {
243 strncat(si->message_buffer, buf,
244 sizeof(si->message_buffer) - strlen(si->message_buffer) - 1);
247 if (level == SVC_LOG_ERROR) {
248 si->codec_ctx->err_detail = si->message_buffer;
253 static vpx_codec_err_t set_option_encoding_mode(SvcContext *svc_ctx,
254 const char *value_str) {
255 if (strcmp(value_str, "i") == 0) {
256 svc_ctx->encoding_mode = INTER_LAYER_PREDICTION_I;
257 } else if (strcmp(value_str, "alt-ip") == 0) {
258 svc_ctx->encoding_mode = ALT_INTER_LAYER_PREDICTION_IP;
259 } else if (strcmp(value_str, "ip") == 0) {
260 svc_ctx->encoding_mode = INTER_LAYER_PREDICTION_IP;
261 } else if (strcmp(value_str, "gf") == 0) {
262 svc_ctx->encoding_mode = USE_GOLDEN_FRAME;
264 svc_log(svc_ctx, SVC_LOG_ERROR, "invalid encoding mode: %s", value_str);
265 return VPX_CODEC_INVALID_PARAM;
270 static vpx_codec_err_t parse_quantizer_values(SvcContext *svc_ctx,
271 const char *quantizer_values) {
274 const char *delim = ",";
278 vpx_codec_err_t res = VPX_CODEC_OK;
279 SvcInternal *const si = get_svc_internal(svc_ctx);
281 if (quantizer_values == NULL || strlen(quantizer_values) == 0) {
282 input_string = strdup(DEFAULT_QUANTIZER_VALUES);
284 input_string = strdup(quantizer_values);
287 token = strtok_r(input_string, delim, &save_ptr);
288 for (i = 0; i < svc_ctx->spatial_layers; ++i) {
291 if (q <= 0 || q > 100) {
292 svc_log(svc_ctx, SVC_LOG_ERROR,
293 "svc-quantizer-values: invalid value %s\n", token);
294 res = VPX_CODEC_INVALID_PARAM;
297 token = strtok_r(NULL, delim, &save_ptr);
302 si->quantizer[i + VPX_SS_MAX_LAYERS - svc_ctx->spatial_layers] = q;
304 if (res == VPX_CODEC_OK && found != svc_ctx->spatial_layers) {
305 svc_log(svc_ctx, SVC_LOG_ERROR,
306 "svc: quantizers: %d values required, but only %d specified\n",
307 svc_ctx->spatial_layers, found);
308 res = VPX_CODEC_INVALID_PARAM;
314 static void log_invalid_scale_factor(SvcContext *svc_ctx, const char *value) {
315 svc_log(svc_ctx, SVC_LOG_ERROR, "svc scale-factors: invalid value %s\n",
319 static vpx_codec_err_t parse_scale_factors(SvcContext *svc_ctx,
320 const char *scale_factors) {
323 const char *delim = ",";
328 vpx_codec_err_t res = VPX_CODEC_OK;
329 SvcInternal *const si = get_svc_internal(svc_ctx);
331 if (scale_factors == NULL || strlen(scale_factors) == 0) {
332 input_string = strdup(DEFAULT_SCALE_FACTORS);
334 input_string = strdup(scale_factors);
336 token = strtok_r(input_string, delim, &save_ptr);
337 for (i = 0; i < svc_ctx->spatial_layers; ++i) {
340 num = strtol(token, &token, 10);
342 log_invalid_scale_factor(svc_ctx, token);
343 res = VPX_CODEC_INVALID_PARAM;
346 if (*token++ != '/') {
347 log_invalid_scale_factor(svc_ctx, token);
348 res = VPX_CODEC_INVALID_PARAM;
351 den = strtol(token, &token, 10);
353 log_invalid_scale_factor(svc_ctx, token);
354 res = VPX_CODEC_INVALID_PARAM;
357 token = strtok_r(NULL, delim, &save_ptr);
360 si->scaling_factor_num[i + VPX_SS_MAX_LAYERS - svc_ctx->spatial_layers] =
362 si->scaling_factor_den[i + VPX_SS_MAX_LAYERS - svc_ctx->spatial_layers] =
365 if (res == VPX_CODEC_OK && found != svc_ctx->spatial_layers) {
366 svc_log(svc_ctx, SVC_LOG_ERROR,
367 "svc: scale-factors: %d values required, but only %d specified\n",
368 svc_ctx->spatial_layers, found);
369 res = VPX_CODEC_INVALID_PARAM;
376 * Parse SVC encoding options
377 * Format: encoding-mode=<svc_mode>,layers=<layer_count>
378 * scale-factors=<n1>/<d1>,<n2>/<d2>,...
379 * quantizers=<q1>,<q2>,...
380 * svc_mode = [i|ip|alt_ip|gf]
382 static vpx_codec_err_t parse_options(SvcContext *svc_ctx, const char *options) {
387 vpx_codec_err_t res = VPX_CODEC_OK;
389 if (options == NULL) return VPX_CODEC_OK;
390 input_string = strdup(options);
393 option_name = strtok_r(input_string, "=", &input_ptr);
394 while (option_name != NULL) {
395 // parse option value
396 option_value = strtok_r(NULL, " ", &input_ptr);
397 if (option_value == NULL) {
398 svc_log(svc_ctx, SVC_LOG_ERROR, "option missing value: %s\n",
400 res = VPX_CODEC_INVALID_PARAM;
403 if (strcmp("encoding-mode", option_name) == 0) {
404 res = set_option_encoding_mode(svc_ctx, option_value);
405 if (res != VPX_CODEC_OK) break;
406 } else if (strcmp("layers", option_name) == 0) {
407 svc_ctx->spatial_layers = atoi(option_value);
408 } else if (strcmp("scale-factors", option_name) == 0) {
409 res = parse_scale_factors(svc_ctx, option_value);
410 if (res != VPX_CODEC_OK) break;
411 } else if (strcmp("quantizers", option_name) == 0) {
412 res = parse_quantizer_values(svc_ctx, option_value);
413 if (res != VPX_CODEC_OK) break;
415 svc_log(svc_ctx, SVC_LOG_ERROR, "invalid option: %s\n", option_name);
416 res = VPX_CODEC_INVALID_PARAM;
419 option_name = strtok_r(NULL, "=", &input_ptr);
425 vpx_codec_err_t vpx_svc_set_options(SvcContext *svc_ctx, const char *options) {
426 SvcInternal *const si = get_svc_internal(svc_ctx);
427 if (svc_ctx == NULL || options == NULL || si == NULL) {
428 return VPX_CODEC_INVALID_PARAM;
430 strncpy(si->options, options, sizeof(si->options));
431 si->options[sizeof(si->options) - 1] = '\0';
435 vpx_codec_err_t vpx_svc_set_quantizers(SvcContext *svc_ctx,
436 const char *quantizers) {
437 SvcInternal *const si = get_svc_internal(svc_ctx);
438 if (svc_ctx == NULL || quantizers == NULL || si == NULL) {
439 return VPX_CODEC_INVALID_PARAM;
441 strncpy(si->quantizers, quantizers, sizeof(si->quantizers));
442 si->quantizers[sizeof(si->quantizers) - 1] = '\0';
446 vpx_codec_err_t vpx_svc_set_scale_factors(SvcContext *svc_ctx,
447 const char *scale_factors) {
448 SvcInternal *const si = get_svc_internal(svc_ctx);
449 if (svc_ctx == NULL || scale_factors == NULL || si == NULL) {
450 return VPX_CODEC_INVALID_PARAM;
452 strncpy(si->scale_factors, scale_factors, sizeof(si->scale_factors));
453 si->scale_factors[sizeof(si->scale_factors) - 1] = '\0';
457 vpx_codec_err_t vpx_svc_init(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
458 vpx_codec_iface_t *iface,
459 vpx_codec_enc_cfg_t *enc_cfg) {
460 int max_intra_size_pct;
462 SvcInternal *const si = get_svc_internal(svc_ctx);
463 if (svc_ctx == NULL || codec_ctx == NULL || iface == NULL ||
465 return VPX_CODEC_INVALID_PARAM;
467 if (si == NULL) return VPX_CODEC_MEM_ERROR;
469 si->codec_ctx = codec_ctx;
471 si->width = enc_cfg->g_w;
472 si->height = enc_cfg->g_h;
474 if (enc_cfg->kf_max_dist < 2) {
475 svc_log(svc_ctx, SVC_LOG_ERROR, "key frame distance too small: %d\n",
476 enc_cfg->kf_max_dist);
477 return VPX_CODEC_INVALID_PARAM;
479 si->kf_dist = enc_cfg->kf_max_dist;
481 if (svc_ctx->spatial_layers == 0)
482 svc_ctx->spatial_layers = VPX_SS_DEFAULT_LAYERS;
483 if (svc_ctx->spatial_layers < 1 ||
484 svc_ctx->spatial_layers > VPX_SS_MAX_LAYERS) {
485 svc_log(svc_ctx, SVC_LOG_ERROR, "spatial layers: invalid value: %d\n",
486 svc_ctx->spatial_layers);
487 return VPX_CODEC_INVALID_PARAM;
489 // use SvcInternal value for number of layers to enable forcing single layer
491 si->layers = svc_ctx->spatial_layers;
493 res = parse_quantizer_values(svc_ctx, si->quantizers);
494 if (res != VPX_CODEC_OK) return res;
496 res = parse_scale_factors(svc_ctx, si->scale_factors);
497 if (res != VPX_CODEC_OK) return res;
499 // parse aggregate command line options
500 res = parse_options(svc_ctx, si->options);
501 if (res != VPX_CODEC_OK) return res;
503 // modify encoder configuration
504 enc_cfg->ss_number_layers = si->layers;
505 enc_cfg->ts_number_layers = 1; // Temporal layers not used in this encoder.
506 enc_cfg->kf_mode = VPX_KF_DISABLED;
507 enc_cfg->g_pass = VPX_RC_ONE_PASS;
508 // Lag in frames not currently supported
509 enc_cfg->g_lag_in_frames = 0;
511 // TODO(ivanmaltz): determine if these values need to be set explicitly for
512 // svc, or if the normal default/override mechanism can be used
513 enc_cfg->rc_dropframe_thresh = 0;
514 enc_cfg->rc_end_usage = VPX_CBR;
515 enc_cfg->rc_resize_allowed = 0;
516 enc_cfg->rc_min_quantizer = 33;
517 enc_cfg->rc_max_quantizer = 33;
518 enc_cfg->rc_undershoot_pct = 100;
519 enc_cfg->rc_overshoot_pct = 15;
520 enc_cfg->rc_buf_initial_sz = 500;
521 enc_cfg->rc_buf_optimal_sz = 600;
522 enc_cfg->rc_buf_sz = 1000;
523 enc_cfg->g_error_resilient = 1;
526 res = vpx_codec_enc_init(codec_ctx, iface, enc_cfg, VPX_CODEC_USE_PSNR);
527 if (res != VPX_CODEC_OK) {
528 svc_log(svc_ctx, SVC_LOG_ERROR, "svc_enc_init error\n");
532 vpx_codec_control(codec_ctx, VP9E_SET_SVC, 1);
533 vpx_codec_control(codec_ctx, VP8E_SET_CPUUSED, 1);
534 vpx_codec_control(codec_ctx, VP8E_SET_STATIC_THRESHOLD, 1);
535 vpx_codec_control(codec_ctx, VP8E_SET_NOISE_SENSITIVITY, 1);
536 vpx_codec_control(codec_ctx, VP8E_SET_TOKEN_PARTITIONS, 1);
539 (int)(((double)enc_cfg->rc_buf_optimal_sz * 0.5) *
540 ((double)enc_cfg->g_timebase.den / enc_cfg->g_timebase.num) / 10.0);
541 vpx_codec_control(codec_ctx, VP8E_SET_MAX_INTRA_BITRATE_PCT,
546 // SVC Algorithm flags - these get mapped to VP8_EFLAG_* defined in vp8cx.h
548 // encoder should reference the last frame
549 #define USE_LAST (1 << 0)
551 // encoder should reference the alt ref frame
552 #define USE_ARF (1 << 1)
554 // encoder should reference the golden frame
555 #define USE_GF (1 << 2)
557 // encoder should copy current frame to the last frame buffer
558 #define UPDATE_LAST (1 << 3)
560 // encoder should copy current frame to the alt ref frame buffer
561 #define UPDATE_ARF (1 << 4)
563 // encoder should copy current frame to the golden frame
564 #define UPDATE_GF (1 << 5)
566 static int map_vp8_flags(int svc_flags) {
569 if (!(svc_flags & USE_LAST)) flags |= VP8_EFLAG_NO_REF_LAST;
570 if (!(svc_flags & USE_ARF)) flags |= VP8_EFLAG_NO_REF_ARF;
571 if (!(svc_flags & USE_GF)) flags |= VP8_EFLAG_NO_REF_GF;
573 if (svc_flags & UPDATE_LAST) {
574 // last is updated automatically
576 flags |= VP8_EFLAG_NO_UPD_LAST;
578 if (svc_flags & UPDATE_ARF) {
579 flags |= VP8_EFLAG_FORCE_ARF;
581 flags |= VP8_EFLAG_NO_UPD_ARF;
583 if (svc_flags & UPDATE_GF) {
584 flags |= VP8_EFLAG_FORCE_GF;
586 flags |= VP8_EFLAG_NO_UPD_GF;
591 static void calculate_enc_frame_flags(SvcContext *svc_ctx) {
592 vpx_enc_frame_flags_t flags = VPX_EFLAG_FORCE_KF;
593 SvcInternal *const si = get_svc_internal(svc_ctx);
594 const int is_keyframe = (si->frame_within_gop == 0);
596 // keyframe layer zero is identical for all modes
597 if (is_keyframe && si->layer == 0) {
598 si->enc_frame_flags = VPX_EFLAG_FORCE_KF;
602 switch (svc_ctx->encoding_mode) {
603 case ALT_INTER_LAYER_PREDICTION_IP:
604 if (si->layer == 0) {
605 flags = map_vp8_flags(USE_LAST | UPDATE_LAST);
606 } else if (is_keyframe) {
607 if (si->layer == si->layers - 1) {
608 flags = map_vp8_flags(USE_ARF | UPDATE_LAST);
610 flags = map_vp8_flags(USE_ARF | UPDATE_LAST | UPDATE_GF);
613 flags = map_vp8_flags(USE_LAST | USE_ARF | UPDATE_LAST);
616 case INTER_LAYER_PREDICTION_I:
617 if (si->layer == 0) {
618 flags = map_vp8_flags(USE_LAST | UPDATE_LAST);
619 } else if (is_keyframe) {
620 flags = map_vp8_flags(USE_ARF | UPDATE_LAST);
622 flags = map_vp8_flags(USE_LAST | UPDATE_LAST);
625 case INTER_LAYER_PREDICTION_IP:
626 if (si->layer == 0) {
627 flags = map_vp8_flags(USE_LAST | UPDATE_LAST);
628 } else if (is_keyframe) {
629 flags = map_vp8_flags(USE_ARF | UPDATE_LAST);
631 flags = map_vp8_flags(USE_LAST | USE_ARF | UPDATE_LAST);
634 case USE_GOLDEN_FRAME:
635 if (2 * si->layers - SVC_REFERENCE_FRAMES <= si->layer) {
636 if (si->layer == 0) {
637 flags = map_vp8_flags(USE_LAST | USE_GF | UPDATE_LAST);
638 } else if (is_keyframe) {
639 flags = map_vp8_flags(USE_ARF | UPDATE_LAST | UPDATE_GF);
641 flags = map_vp8_flags(USE_LAST | USE_ARF | USE_GF | UPDATE_LAST);
644 if (si->layer == 0) {
645 flags = map_vp8_flags(USE_LAST | UPDATE_LAST);
646 } else if (is_keyframe) {
647 flags = map_vp8_flags(USE_ARF | UPDATE_LAST);
649 flags = map_vp8_flags(USE_LAST | UPDATE_LAST);
654 svc_log(svc_ctx, SVC_LOG_ERROR, "unexpected encoding mode: %d\n",
655 svc_ctx->encoding_mode);
658 si->enc_frame_flags = flags;
661 vpx_codec_err_t vpx_svc_get_layer_resolution(const SvcContext *svc_ctx,
664 unsigned int *height) {
665 int w, h, index, num, den;
666 const SvcInternal *const si = get_const_svc_internal(svc_ctx);
668 if (svc_ctx == NULL || si == NULL || width == NULL || height == NULL) {
669 return VPX_CODEC_INVALID_PARAM;
671 if (layer < 0 || layer >= si->layers) return VPX_CODEC_INVALID_PARAM;
673 index = layer + VPX_SS_MAX_LAYERS - si->layers;
674 num = si->scaling_factor_num[index];
675 den = si->scaling_factor_den[index];
676 if (num == 0 || den == 0) return VPX_CODEC_INVALID_PARAM;
678 w = si->width * num / den;
679 h = si->height * num / den;
681 // make height and width even to make chrome player happy
691 static void set_svc_parameters(SvcContext *svc_ctx,
692 vpx_codec_ctx_t *codec_ctx) {
693 int layer, layer_index;
694 vpx_svc_parameters_t svc_params;
695 SvcInternal *const si = get_svc_internal(svc_ctx);
697 memset(&svc_params, 0, sizeof(svc_params));
698 svc_params.temporal_layer = 0;
699 svc_params.spatial_layer = si->layer;
700 svc_params.flags = si->enc_frame_flags;
703 if (svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP &&
704 si->frame_within_gop == 0) {
705 // layers 1 & 3 don't exist in this mode, use the higher one
706 if (layer == 0 || layer == 2) {
710 if (VPX_CODEC_OK != vpx_svc_get_layer_resolution(svc_ctx, layer,
712 &svc_params.height)) {
713 svc_log(svc_ctx, SVC_LOG_ERROR, "vpx_svc_get_layer_resolution failed\n");
715 layer_index = layer + VPX_SS_MAX_LAYERS - si->layers;
716 svc_params.min_quantizer = si->quantizer[layer_index];
717 svc_params.max_quantizer = si->quantizer[layer_index];
718 svc_params.distance_from_i_frame = si->frame_within_gop;
720 // Use buffer i for layer i LST
721 svc_params.lst_fb_idx = si->layer;
723 // Use buffer i-1 for layer i Alt (Inter-layer prediction)
724 if (si->layer != 0) {
725 const int use_higher_layer =
726 svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP &&
727 si->frame_within_gop == 0;
728 svc_params.alt_fb_idx = use_higher_layer ? si->layer - 2 : si->layer - 1;
731 if (svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP) {
732 svc_params.gld_fb_idx = si->layer + 1;
734 if (si->layer < 2 * si->layers - SVC_REFERENCE_FRAMES)
735 svc_params.gld_fb_idx = svc_params.lst_fb_idx;
737 svc_params.gld_fb_idx = 2 * si->layers - 1 - si->layer;
740 svc_log(svc_ctx, SVC_LOG_DEBUG, "SVC frame: %d, layer: %d, %dx%d, q: %d\n",
741 si->encode_frame_count, si->layer, svc_params.width,
742 svc_params.height, svc_params.min_quantizer);
744 if (svc_params.flags == VPX_EFLAG_FORCE_KF) {
745 svc_log(svc_ctx, SVC_LOG_DEBUG, "flags == VPX_EFLAG_FORCE_KF\n");
748 svc_ctx, SVC_LOG_DEBUG, "Using: LST/GLD/ALT [%2d|%2d|%2d]\n",
749 svc_params.flags & VP8_EFLAG_NO_REF_LAST ? -1 : svc_params.lst_fb_idx,
750 svc_params.flags & VP8_EFLAG_NO_REF_GF ? -1 : svc_params.gld_fb_idx,
751 svc_params.flags & VP8_EFLAG_NO_REF_ARF ? -1 : svc_params.alt_fb_idx);
753 svc_ctx, SVC_LOG_DEBUG, "Updating: LST/GLD/ALT [%2d|%2d|%2d]\n",
754 svc_params.flags & VP8_EFLAG_NO_UPD_LAST ? -1 : svc_params.lst_fb_idx,
755 svc_params.flags & VP8_EFLAG_NO_UPD_GF ? -1 : svc_params.gld_fb_idx,
756 svc_params.flags & VP8_EFLAG_NO_UPD_ARF ? -1 : svc_params.alt_fb_idx);
759 vpx_codec_control(codec_ctx, VP9E_SET_SVC_PARAMETERS, &svc_params);
763 * Encode a frame into multiple layers
764 * Create a superframe containing the individual layers
766 vpx_codec_err_t vpx_svc_encode(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
767 struct vpx_image *rawimg, vpx_codec_pts_t pts,
768 int64_t duration, int deadline) {
770 vpx_codec_iter_t iter;
771 const vpx_codec_cx_pkt_t *cx_pkt;
772 struct LayerData *cx_layer_list = NULL;
773 struct LayerData *layer_data;
774 struct Superframe superframe;
775 SvcInternal *const si = get_svc_internal(svc_ctx);
776 if (svc_ctx == NULL || codec_ctx == NULL || rawimg == NULL || si == NULL) {
777 return VPX_CODEC_INVALID_PARAM;
780 memset(&superframe, 0, sizeof(superframe));
781 svc_log_reset(svc_ctx);
783 si->layers = svc_ctx->spatial_layers;
784 if (si->frame_within_gop >= si->kf_dist ||
785 si->encode_frame_count == 0) {
786 si->frame_within_gop = 0;
788 si->is_keyframe = (si->frame_within_gop == 0);
791 svc_log(svc_ctx, SVC_LOG_DEBUG,
792 "vpx_svc_encode layers: %d, frame_count: %d, frame_within_gop: %d\n",
793 si->layers, si->encode_frame_count, si->frame_within_gop);
796 for (si->layer = 0; si->layer < si->layers; ++si->layer) {
797 if (svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP &&
798 si->is_keyframe && (si->layer == 1 || si->layer == 3)) {
799 svc_log(svc_ctx, SVC_LOG_DEBUG, "Skip encoding layer %d\n", si->layer);
802 calculate_enc_frame_flags(svc_ctx);
804 set_svc_parameters(svc_ctx, codec_ctx);
806 res = vpx_codec_encode(codec_ctx, rawimg, pts, (uint32_t)duration,
807 si->enc_frame_flags, deadline);
808 if (res != VPX_CODEC_OK) {
811 // save compressed data
813 while ((cx_pkt = vpx_codec_get_cx_data(codec_ctx, &iter))) {
814 switch (cx_pkt->kind) {
815 case VPX_CODEC_CX_FRAME_PKT: {
816 const uint32_t frame_pkt_size = (uint32_t)(cx_pkt->data.frame.sz);
817 si->bytes_in_layer[si->layer] += frame_pkt_size;
818 svc_log(svc_ctx, SVC_LOG_DEBUG,
819 "SVC frame: %d, layer: %d, size: %u\n",
820 si->encode_frame_count, si->layer, frame_pkt_size);
822 ld_create(cx_pkt->data.frame.buf, (size_t)frame_pkt_size);
823 if (layer_data == NULL) {
824 svc_log(svc_ctx, SVC_LOG_ERROR, "Error allocating LayerData\n");
827 ld_list_add(&cx_layer_list, layer_data);
829 // save layer size in superframe index
830 superframe.sizes[superframe.count++] = frame_pkt_size;
831 superframe.magnitude |= frame_pkt_size;
834 case VPX_CODEC_PSNR_PKT: {
835 svc_log(svc_ctx, SVC_LOG_DEBUG,
836 "SVC frame: %d, layer: %d, PSNR(Total/Y/U/V): "
837 "%2.3f %2.3f %2.3f %2.3f \n",
838 si->encode_frame_count, si->layer,
839 cx_pkt->data.psnr.psnr[0], cx_pkt->data.psnr.psnr[1],
840 cx_pkt->data.psnr.psnr[2], cx_pkt->data.psnr.psnr[3]);
841 si->psnr_in_layer[si->layer] += cx_pkt->data.psnr.psnr[0];
850 // add superframe index to layer data list
851 sf_create_index(&superframe);
852 layer_data = ld_create(superframe.buffer, superframe.index_size);
853 ld_list_add(&cx_layer_list, layer_data);
855 // get accumulated size of layer data
856 si->frame_size = ld_list_get_buffer_size(cx_layer_list);
857 if (si->frame_size == 0) return VPX_CODEC_ERROR;
859 // all layers encoded, create single buffer with concatenated layers
860 if (si->frame_size > si->buffer_size) {
862 si->buffer = malloc(si->frame_size);
863 if (si->buffer == NULL) {
864 ld_list_free(cx_layer_list);
865 return VPX_CODEC_MEM_ERROR;
867 si->buffer_size = si->frame_size;
869 // copy layer data into packet
870 ld_list_copy_to_buffer(cx_layer_list, (uint8_t *)si->buffer);
872 ld_list_free(cx_layer_list);
874 svc_log(svc_ctx, SVC_LOG_DEBUG, "SVC frame: %d, kf: %d, size: %d, pts: %d\n",
875 si->encode_frame_count, si->is_keyframe, (int)si->frame_size,
877 ++si->frame_within_gop;
878 ++si->encode_frame_count;
883 const char *vpx_svc_get_message(const SvcContext *svc_ctx) {
884 const SvcInternal *const si = get_const_svc_internal(svc_ctx);
885 if (svc_ctx == NULL || si == NULL) return NULL;
886 return si->message_buffer;
889 void *vpx_svc_get_buffer(const SvcContext *svc_ctx) {
890 const SvcInternal *const si = get_const_svc_internal(svc_ctx);
891 if (svc_ctx == NULL || si == NULL) return NULL;
895 size_t vpx_svc_get_frame_size(const SvcContext *svc_ctx) {
896 const SvcInternal *const si = get_const_svc_internal(svc_ctx);
897 if (svc_ctx == NULL || si == NULL) return 0;
898 return si->frame_size;
901 int vpx_svc_get_encode_frame_count(const SvcContext *svc_ctx) {
902 const SvcInternal *const si = get_const_svc_internal(svc_ctx);
903 if (svc_ctx == NULL || si == NULL) return 0;
904 return si->encode_frame_count;
907 int vpx_svc_is_keyframe(const SvcContext *svc_ctx) {
908 const SvcInternal *const si = get_const_svc_internal(svc_ctx);
909 if (svc_ctx == NULL || si == NULL) return 0;
910 return si->is_keyframe;
913 void vpx_svc_set_keyframe(SvcContext *svc_ctx) {
914 SvcInternal *const si = get_svc_internal(svc_ctx);
915 if (svc_ctx == NULL || si == NULL) return;
916 si->frame_within_gop = 0;
919 // dump accumulated statistics and reset accumulated values
920 const char *vpx_svc_dump_statistics(SvcContext *svc_ctx) {
921 int number_of_frames, number_of_keyframes, encode_frame_count;
923 uint32_t bytes_total = 0;
924 SvcInternal *const si = get_svc_internal(svc_ctx);
925 if (svc_ctx == NULL || si == NULL) return NULL;
927 svc_log_reset(svc_ctx);
929 encode_frame_count = si->encode_frame_count;
930 if (si->encode_frame_count <= 0) return vpx_svc_get_message(svc_ctx);
932 svc_log(svc_ctx, SVC_LOG_INFO, "\n");
933 number_of_keyframes = encode_frame_count / si->kf_dist + 1;
934 for (i = 0; i < si->layers; ++i) {
935 number_of_frames = encode_frame_count;
937 if (svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP &&
938 (i == 1 || i == 3)) {
939 number_of_frames -= number_of_keyframes;
941 svc_log(svc_ctx, SVC_LOG_INFO, "Layer %d PSNR=[%2.3f], Bytes=[%u]\n", i,
942 (double)si->psnr_in_layer[i] / number_of_frames,
943 si->bytes_in_layer[i]);
944 bytes_total += si->bytes_in_layer[i];
945 si->psnr_in_layer[i] = 0;
946 si->bytes_in_layer[i] = 0;
949 // only display statistics once
950 si->encode_frame_count = 0;
952 svc_log(svc_ctx, SVC_LOG_INFO, "Total Bytes=[%u]\n", bytes_total);
953 return vpx_svc_get_message(svc_ctx);
956 void vpx_svc_release(SvcContext *svc_ctx) {
958 if (svc_ctx == NULL) return;
959 // do not use get_svc_internal as it will unnecessarily allocate an
960 // SvcInternal if it was not already allocated
961 si = (SvcInternal *)svc_ctx->internal;
965 svc_ctx->internal = NULL;