michael@0: /* michael@0: * Copyright (c) 2013 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: /** michael@0: * @file michael@0: * VP9 SVC encoding support via libvpx michael@0: */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #define VPX_DISABLE_CTRL_TYPECHECKS 1 michael@0: #define VPX_CODEC_DISABLE_COMPAT 1 michael@0: #include "vpx/svc_context.h" michael@0: #include "vpx/vp8cx.h" michael@0: #include "vpx/vpx_encoder.h" michael@0: michael@0: #ifdef __MINGW32__ michael@0: #define strtok_r strtok_s michael@0: #ifndef MINGW_HAS_SECURE_API michael@0: // proto from /usr/x86_64-w64-mingw32/include/sec_api/string_s.h michael@0: _CRTIMP char *__cdecl strtok_s(char *str, const char *delim, char **context); michael@0: #endif /* MINGW_HAS_SECURE_API */ michael@0: #endif /* __MINGW32__ */ michael@0: michael@0: #ifdef _MSC_VER michael@0: #define strdup _strdup michael@0: #define strtok_r strtok_s michael@0: #endif michael@0: michael@0: #define SVC_REFERENCE_FRAMES 8 michael@0: #define SUPERFRAME_SLOTS (8) michael@0: #define SUPERFRAME_BUFFER_SIZE (SUPERFRAME_SLOTS * sizeof(uint32_t) + 2) michael@0: #define OPTION_BUFFER_SIZE 256 michael@0: michael@0: static const char *DEFAULT_QUANTIZER_VALUES = "60,53,39,33,27"; michael@0: static const char *DEFAULT_SCALE_FACTORS = "4/16,5/16,7/16,11/16,16/16"; michael@0: michael@0: typedef struct SvcInternal { michael@0: char options[OPTION_BUFFER_SIZE]; // set by vpx_svc_set_options michael@0: char quantizers[OPTION_BUFFER_SIZE]; // set by vpx_svc_set_quantizers michael@0: char scale_factors[OPTION_BUFFER_SIZE]; // set by vpx_svc_set_scale_factors michael@0: michael@0: // values extracted from option, quantizers michael@0: int scaling_factor_num[VPX_SS_MAX_LAYERS]; michael@0: int scaling_factor_den[VPX_SS_MAX_LAYERS]; michael@0: int quantizer[VPX_SS_MAX_LAYERS]; michael@0: michael@0: // accumulated statistics michael@0: double psnr_in_layer[VPX_SS_MAX_LAYERS]; michael@0: uint32_t bytes_in_layer[VPX_SS_MAX_LAYERS]; michael@0: michael@0: // codec encoding values michael@0: int width; // width of highest layer michael@0: int height; // height of highest layer michael@0: int kf_dist; // distance between keyframes michael@0: michael@0: // state variables michael@0: int encode_frame_count; michael@0: int frame_within_gop; michael@0: vpx_enc_frame_flags_t enc_frame_flags; michael@0: int layers; michael@0: int layer; michael@0: int is_keyframe; michael@0: michael@0: size_t frame_size; michael@0: size_t buffer_size; michael@0: void *buffer; michael@0: michael@0: char message_buffer[2048]; michael@0: vpx_codec_ctx_t *codec_ctx; michael@0: } SvcInternal; michael@0: michael@0: // Superframe is used to generate an index of individual frames (i.e., layers) michael@0: struct Superframe { michael@0: int count; michael@0: uint32_t sizes[SUPERFRAME_SLOTS]; michael@0: uint32_t magnitude; michael@0: uint8_t buffer[SUPERFRAME_BUFFER_SIZE]; michael@0: size_t index_size; michael@0: }; michael@0: michael@0: // One encoded frame layer michael@0: struct LayerData { michael@0: void *buf; // compressed data buffer michael@0: size_t size; // length of compressed data michael@0: struct LayerData *next; michael@0: }; michael@0: michael@0: // create LayerData from encoder output michael@0: static struct LayerData *ld_create(void *buf, size_t size) { michael@0: struct LayerData *const layer_data = malloc(sizeof(*layer_data)); michael@0: if (layer_data == NULL) { michael@0: return NULL; michael@0: } michael@0: layer_data->buf = malloc(size); michael@0: if (layer_data->buf == NULL) { michael@0: free(layer_data); michael@0: return NULL; michael@0: } michael@0: memcpy(layer_data->buf, buf, size); michael@0: layer_data->size = size; michael@0: return layer_data; michael@0: } michael@0: michael@0: // free LayerData michael@0: static void ld_free(struct LayerData *layer_data) { michael@0: if (layer_data) { michael@0: if (layer_data->buf) { michael@0: free(layer_data->buf); michael@0: layer_data->buf = NULL; michael@0: } michael@0: free(layer_data); michael@0: } michael@0: } michael@0: michael@0: // add layer data to list michael@0: static void ld_list_add(struct LayerData **list, struct LayerData *layer_data) { michael@0: struct LayerData **p = list; michael@0: michael@0: while (*p != NULL) p = &(*p)->next; michael@0: *p = layer_data; michael@0: layer_data->next = NULL; michael@0: } michael@0: michael@0: // get accumulated size of layer data michael@0: static size_t ld_list_get_buffer_size(struct LayerData *list) { michael@0: struct LayerData *p; michael@0: size_t size = 0; michael@0: michael@0: for (p = list; p != NULL; p = p->next) { michael@0: size += p->size; michael@0: } michael@0: return size; michael@0: } michael@0: michael@0: // copy layer data to buffer michael@0: static void ld_list_copy_to_buffer(struct LayerData *list, uint8_t *buffer) { michael@0: struct LayerData *p; michael@0: michael@0: for (p = list; p != NULL; p = p->next) { michael@0: buffer[0] = 1; michael@0: memcpy(buffer, p->buf, p->size); michael@0: buffer += p->size; michael@0: } michael@0: } michael@0: michael@0: // free layer data list michael@0: static void ld_list_free(struct LayerData *list) { michael@0: struct LayerData *p = list; michael@0: michael@0: while (p) { michael@0: list = list->next; michael@0: ld_free(p); michael@0: p = list; michael@0: } michael@0: } michael@0: michael@0: static void sf_create_index(struct Superframe *sf) { michael@0: uint8_t marker = 0xc0; michael@0: int i; michael@0: uint32_t mag, mask; michael@0: uint8_t *bufp; michael@0: michael@0: if (sf->count == 0 || sf->count >= 8) return; michael@0: michael@0: // Add the number of frames to the marker byte michael@0: marker |= sf->count - 1; michael@0: michael@0: // Choose the magnitude michael@0: for (mag = 0, mask = 0xff; mag < 4; ++mag) { michael@0: if (sf->magnitude < mask) break; michael@0: mask <<= 8; michael@0: mask |= 0xff; michael@0: } michael@0: marker |= mag << 3; michael@0: michael@0: // Write the index michael@0: sf->index_size = 2 + (mag + 1) * sf->count; michael@0: bufp = sf->buffer; michael@0: michael@0: *bufp++ = marker; michael@0: for (i = 0; i < sf->count; ++i) { michael@0: int this_sz = sf->sizes[i]; michael@0: uint32_t j; michael@0: michael@0: for (j = 0; j <= mag; ++j) { michael@0: *bufp++ = this_sz & 0xff; michael@0: this_sz >>= 8; michael@0: } michael@0: } michael@0: *bufp++ = marker; michael@0: } michael@0: michael@0: static SvcInternal *get_svc_internal(SvcContext *svc_ctx) { michael@0: if (svc_ctx == NULL) return NULL; michael@0: if (svc_ctx->internal == NULL) { michael@0: SvcInternal *const si = malloc(sizeof(*si)); michael@0: if (si != NULL) { michael@0: memset(si, 0, sizeof(*si)); michael@0: } michael@0: svc_ctx->internal = si; michael@0: } michael@0: return svc_ctx->internal; michael@0: } michael@0: michael@0: static const SvcInternal *get_const_svc_internal(const SvcContext *svc_ctx) { michael@0: if (svc_ctx == NULL) return NULL; michael@0: return svc_ctx->internal; michael@0: } michael@0: michael@0: static void svc_log_reset(SvcContext *svc_ctx) { michael@0: SvcInternal *const si = (SvcInternal *)svc_ctx->internal; michael@0: si->message_buffer[0] = '\0'; michael@0: } michael@0: michael@0: static int svc_log(SvcContext *svc_ctx, int level, const char *fmt, ...) { michael@0: char buf[512]; michael@0: int retval = 0; michael@0: va_list ap; michael@0: SvcInternal *const si = get_svc_internal(svc_ctx); michael@0: michael@0: if (level > svc_ctx->log_level) { michael@0: return retval; michael@0: } michael@0: michael@0: va_start(ap, fmt); michael@0: retval = vsnprintf(buf, sizeof(buf), fmt, ap); michael@0: va_end(ap); michael@0: michael@0: if (svc_ctx->log_print) { michael@0: printf("%s", buf); michael@0: } else { michael@0: strncat(si->message_buffer, buf, michael@0: sizeof(si->message_buffer) - strlen(si->message_buffer) - 1); michael@0: } michael@0: michael@0: if (level == SVC_LOG_ERROR) { michael@0: si->codec_ctx->err_detail = si->message_buffer; michael@0: } michael@0: return retval; michael@0: } michael@0: michael@0: static vpx_codec_err_t set_option_encoding_mode(SvcContext *svc_ctx, michael@0: const char *value_str) { michael@0: if (strcmp(value_str, "i") == 0) { michael@0: svc_ctx->encoding_mode = INTER_LAYER_PREDICTION_I; michael@0: } else if (strcmp(value_str, "alt-ip") == 0) { michael@0: svc_ctx->encoding_mode = ALT_INTER_LAYER_PREDICTION_IP; michael@0: } else if (strcmp(value_str, "ip") == 0) { michael@0: svc_ctx->encoding_mode = INTER_LAYER_PREDICTION_IP; michael@0: } else if (strcmp(value_str, "gf") == 0) { michael@0: svc_ctx->encoding_mode = USE_GOLDEN_FRAME; michael@0: } else { michael@0: svc_log(svc_ctx, SVC_LOG_ERROR, "invalid encoding mode: %s", value_str); michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: static vpx_codec_err_t parse_quantizer_values(SvcContext *svc_ctx, michael@0: const char *quantizer_values) { michael@0: char *input_string; michael@0: char *token; michael@0: const char *delim = ","; michael@0: char *save_ptr; michael@0: int found = 0; michael@0: int i, q; michael@0: int res = VPX_CODEC_OK; michael@0: SvcInternal *const si = get_svc_internal(svc_ctx); michael@0: michael@0: if (quantizer_values == NULL || strlen(quantizer_values) == 0) { michael@0: input_string = strdup(DEFAULT_QUANTIZER_VALUES); michael@0: } else { michael@0: input_string = strdup(quantizer_values); michael@0: } michael@0: michael@0: token = strtok_r(input_string, delim, &save_ptr); michael@0: for (i = 0; i < svc_ctx->spatial_layers; ++i) { michael@0: if (token != NULL) { michael@0: q = atoi(token); michael@0: if (q <= 0 || q > 100) { michael@0: svc_log(svc_ctx, SVC_LOG_ERROR, michael@0: "svc-quantizer-values: invalid value %s\n", token); michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: break; michael@0: } michael@0: token = strtok_r(NULL, delim, &save_ptr); michael@0: found = i + 1; michael@0: } else { michael@0: q = 0; michael@0: } michael@0: si->quantizer[i + VPX_SS_MAX_LAYERS - svc_ctx->spatial_layers] = q; michael@0: } michael@0: if (res == VPX_CODEC_OK && found != svc_ctx->spatial_layers) { michael@0: svc_log(svc_ctx, SVC_LOG_ERROR, michael@0: "svc: quantizers: %d values required, but only %d specified\n", michael@0: svc_ctx->spatial_layers, found); michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: free(input_string); michael@0: return res; michael@0: } michael@0: michael@0: static void log_invalid_scale_factor(SvcContext *svc_ctx, const char *value) { michael@0: svc_log(svc_ctx, SVC_LOG_ERROR, "svc scale-factors: invalid value %s\n", michael@0: value); michael@0: } michael@0: michael@0: static vpx_codec_err_t parse_scale_factors(SvcContext *svc_ctx, michael@0: const char *scale_factors) { michael@0: char *input_string; michael@0: char *token; michael@0: const char *delim = ","; michael@0: char *save_ptr; michael@0: int found = 0; michael@0: int i; michael@0: int64_t num, den; michael@0: int res = VPX_CODEC_OK; michael@0: SvcInternal *const si = get_svc_internal(svc_ctx); michael@0: michael@0: if (scale_factors == NULL || strlen(scale_factors) == 0) { michael@0: input_string = strdup(DEFAULT_SCALE_FACTORS); michael@0: } else { michael@0: input_string = strdup(scale_factors); michael@0: } michael@0: token = strtok_r(input_string, delim, &save_ptr); michael@0: for (i = 0; i < svc_ctx->spatial_layers; ++i) { michael@0: num = den = 0; michael@0: if (token != NULL) { michael@0: num = strtol(token, &token, 10); michael@0: if (num <= 0) { michael@0: log_invalid_scale_factor(svc_ctx, token); michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: break; michael@0: } michael@0: if (*token++ != '/') { michael@0: log_invalid_scale_factor(svc_ctx, token); michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: break; michael@0: } michael@0: den = strtol(token, &token, 10); michael@0: if (den <= 0) { michael@0: log_invalid_scale_factor(svc_ctx, token); michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: break; michael@0: } michael@0: token = strtok_r(NULL, delim, &save_ptr); michael@0: found = i + 1; michael@0: } michael@0: si->scaling_factor_num[i + VPX_SS_MAX_LAYERS - svc_ctx->spatial_layers] = michael@0: (int)num; michael@0: si->scaling_factor_den[i + VPX_SS_MAX_LAYERS - svc_ctx->spatial_layers] = michael@0: (int)den; michael@0: } michael@0: if (res == VPX_CODEC_OK && found != svc_ctx->spatial_layers) { michael@0: svc_log(svc_ctx, SVC_LOG_ERROR, michael@0: "svc: scale-factors: %d values required, but only %d specified\n", michael@0: svc_ctx->spatial_layers, found); michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: free(input_string); michael@0: return res; michael@0: } michael@0: michael@0: /** michael@0: * Parse SVC encoding options michael@0: * Format: encoding-mode=,layers= michael@0: * scale-factors=/,/,... michael@0: * quantizers=,,... michael@0: * svc_mode = [i|ip|alt_ip|gf] michael@0: */ michael@0: static vpx_codec_err_t parse_options(SvcContext *svc_ctx, const char *options) { michael@0: char *input_string; michael@0: char *option_name; michael@0: char *option_value; michael@0: char *input_ptr; michael@0: int res = VPX_CODEC_OK; michael@0: michael@0: if (options == NULL) return VPX_CODEC_OK; michael@0: input_string = strdup(options); michael@0: michael@0: // parse option name michael@0: option_name = strtok_r(input_string, "=", &input_ptr); michael@0: while (option_name != NULL) { michael@0: // parse option value michael@0: option_value = strtok_r(NULL, " ", &input_ptr); michael@0: if (option_value == NULL) { michael@0: svc_log(svc_ctx, SVC_LOG_ERROR, "option missing value: %s\n", michael@0: option_name); michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: break; michael@0: } michael@0: if (strcmp("encoding-mode", option_name) == 0) { michael@0: res = set_option_encoding_mode(svc_ctx, option_value); michael@0: if (res != VPX_CODEC_OK) break; michael@0: } else if (strcmp("layers", option_name) == 0) { michael@0: svc_ctx->spatial_layers = atoi(option_value); michael@0: } else if (strcmp("scale-factors", option_name) == 0) { michael@0: res = parse_scale_factors(svc_ctx, option_value); michael@0: if (res != VPX_CODEC_OK) break; michael@0: } else if (strcmp("quantizers", option_name) == 0) { michael@0: res = parse_quantizer_values(svc_ctx, option_value); michael@0: if (res != VPX_CODEC_OK) break; michael@0: } else { michael@0: svc_log(svc_ctx, SVC_LOG_ERROR, "invalid option: %s\n", option_name); michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: break; michael@0: } michael@0: option_name = strtok_r(NULL, "=", &input_ptr); michael@0: } michael@0: free(input_string); michael@0: return res; michael@0: } michael@0: michael@0: vpx_codec_err_t vpx_svc_set_options(SvcContext *svc_ctx, const char *options) { michael@0: SvcInternal *const si = get_svc_internal(svc_ctx); michael@0: if (svc_ctx == NULL || options == NULL || si == NULL) { michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: strncpy(si->options, options, sizeof(si->options)); michael@0: si->options[sizeof(si->options) - 1] = '\0'; michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: vpx_codec_err_t vpx_svc_set_quantizers(SvcContext *svc_ctx, michael@0: const char *quantizers) { michael@0: SvcInternal *const si = get_svc_internal(svc_ctx); michael@0: if (svc_ctx == NULL || quantizers == NULL || si == NULL) { michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: strncpy(si->quantizers, quantizers, sizeof(si->quantizers)); michael@0: si->quantizers[sizeof(si->quantizers) - 1] = '\0'; michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: vpx_codec_err_t vpx_svc_set_scale_factors(SvcContext *svc_ctx, michael@0: const char *scale_factors) { michael@0: SvcInternal *const si = get_svc_internal(svc_ctx); michael@0: if (svc_ctx == NULL || scale_factors == NULL || si == NULL) { michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: strncpy(si->scale_factors, scale_factors, sizeof(si->scale_factors)); michael@0: si->scale_factors[sizeof(si->scale_factors) - 1] = '\0'; michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: vpx_codec_err_t vpx_svc_init(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx, michael@0: vpx_codec_iface_t *iface, michael@0: vpx_codec_enc_cfg_t *enc_cfg) { michael@0: int max_intra_size_pct; michael@0: vpx_codec_err_t res; michael@0: SvcInternal *const si = get_svc_internal(svc_ctx); michael@0: if (svc_ctx == NULL || codec_ctx == NULL || iface == NULL || michael@0: enc_cfg == NULL) { michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: if (si == NULL) return VPX_CODEC_MEM_ERROR; michael@0: michael@0: si->codec_ctx = codec_ctx; michael@0: michael@0: si->width = enc_cfg->g_w; michael@0: si->height = enc_cfg->g_h; michael@0: michael@0: if (enc_cfg->kf_max_dist < 2) { michael@0: svc_log(svc_ctx, SVC_LOG_ERROR, "key frame distance too small: %d\n", michael@0: enc_cfg->kf_max_dist); michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: si->kf_dist = enc_cfg->kf_max_dist; michael@0: michael@0: if (svc_ctx->spatial_layers == 0) michael@0: svc_ctx->spatial_layers = VPX_SS_DEFAULT_LAYERS; michael@0: if (svc_ctx->spatial_layers < 1 || michael@0: svc_ctx->spatial_layers > VPX_SS_MAX_LAYERS) { michael@0: svc_log(svc_ctx, SVC_LOG_ERROR, "spatial layers: invalid value: %d\n", michael@0: svc_ctx->spatial_layers); michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: // use SvcInternal value for number of layers to enable forcing single layer michael@0: // for first frame michael@0: si->layers = svc_ctx->spatial_layers; michael@0: michael@0: res = parse_quantizer_values(svc_ctx, si->quantizers); michael@0: if (res != VPX_CODEC_OK) return res; michael@0: michael@0: res = parse_scale_factors(svc_ctx, si->scale_factors); michael@0: if (res != VPX_CODEC_OK) return res; michael@0: michael@0: // parse aggregate command line options michael@0: res = parse_options(svc_ctx, si->options); michael@0: if (res != VPX_CODEC_OK) return res; michael@0: michael@0: // modify encoder configuration michael@0: enc_cfg->ss_number_layers = si->layers; michael@0: enc_cfg->kf_mode = VPX_KF_DISABLED; michael@0: enc_cfg->g_pass = VPX_RC_ONE_PASS; michael@0: // Lag in frames not currently supported michael@0: enc_cfg->g_lag_in_frames = 0; michael@0: michael@0: // TODO(ivanmaltz): determine if these values need to be set explicitly for michael@0: // svc, or if the normal default/override mechanism can be used michael@0: enc_cfg->rc_dropframe_thresh = 0; michael@0: enc_cfg->rc_end_usage = VPX_CBR; michael@0: enc_cfg->rc_resize_allowed = 0; michael@0: enc_cfg->rc_min_quantizer = 33; michael@0: enc_cfg->rc_max_quantizer = 33; michael@0: enc_cfg->rc_undershoot_pct = 100; michael@0: enc_cfg->rc_overshoot_pct = 15; michael@0: enc_cfg->rc_buf_initial_sz = 500; michael@0: enc_cfg->rc_buf_optimal_sz = 600; michael@0: enc_cfg->rc_buf_sz = 1000; michael@0: enc_cfg->g_error_resilient = 1; michael@0: michael@0: // Initialize codec michael@0: res = vpx_codec_enc_init(codec_ctx, iface, enc_cfg, VPX_CODEC_USE_PSNR); michael@0: if (res != VPX_CODEC_OK) { michael@0: svc_log(svc_ctx, SVC_LOG_ERROR, "svc_enc_init error\n"); michael@0: return res; michael@0: } michael@0: michael@0: vpx_codec_control(codec_ctx, VP9E_SET_SVC, 1); michael@0: vpx_codec_control(codec_ctx, VP8E_SET_CPUUSED, 1); michael@0: vpx_codec_control(codec_ctx, VP8E_SET_STATIC_THRESHOLD, 1); michael@0: vpx_codec_control(codec_ctx, VP8E_SET_NOISE_SENSITIVITY, 1); michael@0: vpx_codec_control(codec_ctx, VP8E_SET_TOKEN_PARTITIONS, 1); michael@0: michael@0: max_intra_size_pct = michael@0: (int)(((double)enc_cfg->rc_buf_optimal_sz * 0.5) * michael@0: ((double)enc_cfg->g_timebase.den / enc_cfg->g_timebase.num) / 10.0); michael@0: vpx_codec_control(codec_ctx, VP8E_SET_MAX_INTRA_BITRATE_PCT, michael@0: max_intra_size_pct); michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: // SVC Algorithm flags - these get mapped to VP8_EFLAG_* defined in vp8cx.h michael@0: michael@0: // encoder should reference the last frame michael@0: #define USE_LAST (1 << 0) michael@0: michael@0: // encoder should reference the alt ref frame michael@0: #define USE_ARF (1 << 1) michael@0: michael@0: // encoder should reference the golden frame michael@0: #define USE_GF (1 << 2) michael@0: michael@0: // encoder should copy current frame to the last frame buffer michael@0: #define UPDATE_LAST (1 << 3) michael@0: michael@0: // encoder should copy current frame to the alt ref frame buffer michael@0: #define UPDATE_ARF (1 << 4) michael@0: michael@0: // encoder should copy current frame to the golden frame michael@0: #define UPDATE_GF (1 << 5) michael@0: michael@0: static int map_vp8_flags(int svc_flags) { michael@0: int flags = 0; michael@0: michael@0: if (!(svc_flags & USE_LAST)) flags |= VP8_EFLAG_NO_REF_LAST; michael@0: if (!(svc_flags & USE_ARF)) flags |= VP8_EFLAG_NO_REF_ARF; michael@0: if (!(svc_flags & USE_GF)) flags |= VP8_EFLAG_NO_REF_GF; michael@0: michael@0: if (svc_flags & UPDATE_LAST) { michael@0: // last is updated automatically michael@0: } else { michael@0: flags |= VP8_EFLAG_NO_UPD_LAST; michael@0: } michael@0: if (svc_flags & UPDATE_ARF) { michael@0: flags |= VP8_EFLAG_FORCE_ARF; michael@0: } else { michael@0: flags |= VP8_EFLAG_NO_UPD_ARF; michael@0: } michael@0: if (svc_flags & UPDATE_GF) { michael@0: flags |= VP8_EFLAG_FORCE_GF; michael@0: } else { michael@0: flags |= VP8_EFLAG_NO_UPD_GF; michael@0: } michael@0: return flags; michael@0: } michael@0: michael@0: /** michael@0: * Helper to check if the current frame is the first, full resolution dummy. michael@0: */ michael@0: static int vpx_svc_dummy_frame(SvcContext *svc_ctx) { michael@0: SvcInternal *const si = get_svc_internal(svc_ctx); michael@0: return svc_ctx->first_frame_full_size == 1 && si->encode_frame_count == 0; michael@0: } michael@0: michael@0: static void calculate_enc_frame_flags(SvcContext *svc_ctx) { michael@0: vpx_enc_frame_flags_t flags = VPX_EFLAG_FORCE_KF; michael@0: SvcInternal *const si = get_svc_internal(svc_ctx); michael@0: const int is_keyframe = (si->frame_within_gop == 0); michael@0: michael@0: // keyframe layer zero is identical for all modes michael@0: if ((is_keyframe && si->layer == 0) || vpx_svc_dummy_frame(svc_ctx)) { michael@0: si->enc_frame_flags = VPX_EFLAG_FORCE_KF; michael@0: return; michael@0: } michael@0: michael@0: switch (svc_ctx->encoding_mode) { michael@0: case ALT_INTER_LAYER_PREDICTION_IP: michael@0: if (si->layer == 0) { michael@0: flags = map_vp8_flags(USE_LAST | UPDATE_LAST); michael@0: } else if (is_keyframe) { michael@0: if (si->layer == si->layers - 1) { michael@0: flags = map_vp8_flags(USE_ARF | UPDATE_LAST); michael@0: } else { michael@0: flags = map_vp8_flags(USE_ARF | UPDATE_LAST | UPDATE_GF); michael@0: } michael@0: } else { michael@0: flags = map_vp8_flags(USE_LAST | USE_ARF | UPDATE_LAST); michael@0: } michael@0: break; michael@0: case INTER_LAYER_PREDICTION_I: michael@0: if (si->layer == 0) { michael@0: flags = map_vp8_flags(USE_LAST | UPDATE_LAST); michael@0: } else if (is_keyframe) { michael@0: flags = map_vp8_flags(USE_ARF | UPDATE_LAST); michael@0: } else { michael@0: flags = map_vp8_flags(USE_LAST | UPDATE_LAST); michael@0: } michael@0: break; michael@0: case INTER_LAYER_PREDICTION_IP: michael@0: if (si->layer == 0) { michael@0: flags = map_vp8_flags(USE_LAST | UPDATE_LAST); michael@0: } else if (is_keyframe) { michael@0: flags = map_vp8_flags(USE_ARF | UPDATE_LAST); michael@0: } else { michael@0: flags = map_vp8_flags(USE_LAST | USE_ARF | UPDATE_LAST); michael@0: } michael@0: break; michael@0: case USE_GOLDEN_FRAME: michael@0: if (2 * si->layers - SVC_REFERENCE_FRAMES <= si->layer) { michael@0: if (si->layer == 0) { michael@0: flags = map_vp8_flags(USE_LAST | USE_GF | UPDATE_LAST); michael@0: } else if (is_keyframe) { michael@0: flags = map_vp8_flags(USE_ARF | UPDATE_LAST | UPDATE_GF); michael@0: } else { michael@0: flags = map_vp8_flags(USE_LAST | USE_ARF | USE_GF | UPDATE_LAST); michael@0: } michael@0: } else { michael@0: if (si->layer == 0) { michael@0: flags = map_vp8_flags(USE_LAST | UPDATE_LAST); michael@0: } else if (is_keyframe) { michael@0: flags = map_vp8_flags(USE_ARF | UPDATE_LAST); michael@0: } else { michael@0: flags = map_vp8_flags(USE_LAST | UPDATE_LAST); michael@0: } michael@0: } michael@0: break; michael@0: default: michael@0: svc_log(svc_ctx, SVC_LOG_ERROR, "unexpected encoding mode: %d\n", michael@0: svc_ctx->encoding_mode); michael@0: break; michael@0: } michael@0: si->enc_frame_flags = flags; michael@0: } michael@0: michael@0: vpx_codec_err_t vpx_svc_get_layer_resolution(const SvcContext *svc_ctx, michael@0: int layer, michael@0: unsigned int *width, michael@0: unsigned int *height) { michael@0: int w, h, index, num, den; michael@0: const SvcInternal *const si = get_const_svc_internal(svc_ctx); michael@0: michael@0: if (svc_ctx == NULL || si == NULL || width == NULL || height == NULL) { michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: if (layer < 0 || layer >= si->layers) return VPX_CODEC_INVALID_PARAM; michael@0: michael@0: index = layer + VPX_SS_MAX_LAYERS - si->layers; michael@0: num = si->scaling_factor_num[index]; michael@0: den = si->scaling_factor_den[index]; michael@0: if (num == 0 || den == 0) return VPX_CODEC_INVALID_PARAM; michael@0: michael@0: w = si->width * num / den; michael@0: h = si->height * num / den; michael@0: michael@0: // make height and width even to make chrome player happy michael@0: w += w % 2; michael@0: h += h % 2; michael@0: michael@0: *width = w; michael@0: *height = h; michael@0: michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: static void set_svc_parameters(SvcContext *svc_ctx, michael@0: vpx_codec_ctx_t *codec_ctx) { michael@0: int layer, layer_index; michael@0: vpx_svc_parameters_t svc_params; michael@0: SvcInternal *const si = get_svc_internal(svc_ctx); michael@0: michael@0: memset(&svc_params, 0, sizeof(svc_params)); michael@0: svc_params.layer = si->layer; michael@0: svc_params.flags = si->enc_frame_flags; michael@0: michael@0: layer = si->layer; michael@0: if (svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP && michael@0: si->frame_within_gop == 0) { michael@0: // layers 1 & 3 don't exist in this mode, use the higher one michael@0: if (layer == 0 || layer == 2) { michael@0: layer += 1; michael@0: } michael@0: } michael@0: if (VPX_CODEC_OK != vpx_svc_get_layer_resolution(svc_ctx, layer, michael@0: &svc_params.width, michael@0: &svc_params.height)) { michael@0: svc_log(svc_ctx, SVC_LOG_ERROR, "vpx_svc_get_layer_resolution failed\n"); michael@0: } michael@0: layer_index = layer + VPX_SS_MAX_LAYERS - si->layers; michael@0: svc_params.min_quantizer = si->quantizer[layer_index]; michael@0: svc_params.max_quantizer = si->quantizer[layer_index]; michael@0: svc_params.distance_from_i_frame = si->frame_within_gop; michael@0: michael@0: // Use buffer i for layer i LST michael@0: svc_params.lst_fb_idx = si->layer; michael@0: michael@0: // Use buffer i-1 for layer i Alt (Inter-layer prediction) michael@0: if (si->layer != 0) { michael@0: const int use_higher_layer = michael@0: svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP && michael@0: si->frame_within_gop == 0; michael@0: svc_params.alt_fb_idx = use_higher_layer ? si->layer - 2 : si->layer - 1; michael@0: } michael@0: michael@0: if (svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP) { michael@0: svc_params.gld_fb_idx = si->layer + 1; michael@0: } else { michael@0: if (si->layer < 2 * si->layers - SVC_REFERENCE_FRAMES) michael@0: svc_params.gld_fb_idx = svc_params.lst_fb_idx; michael@0: else michael@0: svc_params.gld_fb_idx = 2 * si->layers - 1 - si->layer; michael@0: } michael@0: michael@0: svc_log(svc_ctx, SVC_LOG_DEBUG, "SVC frame: %d, layer: %d, %dx%d, q: %d\n", michael@0: si->encode_frame_count, si->layer, svc_params.width, michael@0: svc_params.height, svc_params.min_quantizer); michael@0: michael@0: if (svc_params.flags == VPX_EFLAG_FORCE_KF) { michael@0: svc_log(svc_ctx, SVC_LOG_DEBUG, "flags == VPX_EFLAG_FORCE_KF\n"); michael@0: } else { michael@0: svc_log( michael@0: svc_ctx, SVC_LOG_DEBUG, "Using: LST/GLD/ALT [%2d|%2d|%2d]\n", michael@0: svc_params.flags & VP8_EFLAG_NO_REF_LAST ? -1 : svc_params.lst_fb_idx, michael@0: svc_params.flags & VP8_EFLAG_NO_REF_GF ? -1 : svc_params.gld_fb_idx, michael@0: svc_params.flags & VP8_EFLAG_NO_REF_ARF ? -1 : svc_params.alt_fb_idx); michael@0: svc_log( michael@0: svc_ctx, SVC_LOG_DEBUG, "Updating: LST/GLD/ALT [%2d|%2d|%2d]\n", michael@0: svc_params.flags & VP8_EFLAG_NO_UPD_LAST ? -1 : svc_params.lst_fb_idx, michael@0: svc_params.flags & VP8_EFLAG_NO_UPD_GF ? -1 : svc_params.gld_fb_idx, michael@0: svc_params.flags & VP8_EFLAG_NO_UPD_ARF ? -1 : svc_params.alt_fb_idx); michael@0: } michael@0: michael@0: vpx_codec_control(codec_ctx, VP9E_SET_SVC_PARAMETERS, &svc_params); michael@0: } michael@0: michael@0: /** michael@0: * Encode a frame into multiple layers michael@0: * Create a superframe containing the individual layers michael@0: */ michael@0: vpx_codec_err_t vpx_svc_encode(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx, michael@0: struct vpx_image *rawimg, vpx_codec_pts_t pts, michael@0: int64_t duration, int deadline) { michael@0: vpx_codec_err_t res; michael@0: vpx_codec_iter_t iter; michael@0: const vpx_codec_cx_pkt_t *cx_pkt; michael@0: struct LayerData *cx_layer_list = NULL; michael@0: struct LayerData *layer_data; michael@0: struct Superframe superframe; michael@0: SvcInternal *const si = get_svc_internal(svc_ctx); michael@0: if (svc_ctx == NULL || codec_ctx == NULL || rawimg == NULL || si == NULL) { michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: michael@0: memset(&superframe, 0, sizeof(superframe)); michael@0: svc_log_reset(svc_ctx); michael@0: michael@0: si->layers = vpx_svc_dummy_frame(svc_ctx) ? 1 : svc_ctx->spatial_layers; michael@0: if (si->frame_within_gop >= si->kf_dist || michael@0: si->encode_frame_count == 0 || michael@0: (si->encode_frame_count == 1 && svc_ctx->first_frame_full_size == 1)) { michael@0: si->frame_within_gop = 0; michael@0: } michael@0: si->is_keyframe = (si->frame_within_gop == 0); michael@0: si->frame_size = 0; michael@0: michael@0: svc_log(svc_ctx, SVC_LOG_DEBUG, michael@0: "vpx_svc_encode layers: %d, frame_count: %d, frame_within_gop: %d\n", michael@0: si->layers, si->encode_frame_count, si->frame_within_gop); michael@0: michael@0: // encode each layer michael@0: for (si->layer = 0; si->layer < si->layers; ++si->layer) { michael@0: if (svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP && michael@0: si->is_keyframe && (si->layer == 1 || si->layer == 3)) { michael@0: svc_log(svc_ctx, SVC_LOG_DEBUG, "Skip encoding layer %d\n", si->layer); michael@0: continue; michael@0: } michael@0: calculate_enc_frame_flags(svc_ctx); michael@0: michael@0: if (vpx_svc_dummy_frame(svc_ctx)) { michael@0: // do not set svc parameters, use normal encode michael@0: svc_log(svc_ctx, SVC_LOG_DEBUG, "encoding full size first frame\n"); michael@0: } else { michael@0: set_svc_parameters(svc_ctx, codec_ctx); michael@0: } michael@0: res = vpx_codec_encode(codec_ctx, rawimg, pts, (uint32_t)duration, michael@0: si->enc_frame_flags, deadline); michael@0: if (res != VPX_CODEC_OK) { michael@0: return res; michael@0: } michael@0: // save compressed data michael@0: iter = NULL; michael@0: while ((cx_pkt = vpx_codec_get_cx_data(codec_ctx, &iter))) { michael@0: switch (cx_pkt->kind) { michael@0: case VPX_CODEC_CX_FRAME_PKT: { michael@0: const uint32_t frame_pkt_size = (uint32_t)(cx_pkt->data.frame.sz); michael@0: if (!vpx_svc_dummy_frame(svc_ctx)) { michael@0: si->bytes_in_layer[si->layer] += frame_pkt_size; michael@0: svc_log(svc_ctx, SVC_LOG_DEBUG, michael@0: "SVC frame: %d, layer: %d, size: %u\n", michael@0: si->encode_frame_count, si->layer, frame_pkt_size); michael@0: } michael@0: layer_data = michael@0: ld_create(cx_pkt->data.frame.buf, (size_t)frame_pkt_size); michael@0: if (layer_data == NULL) { michael@0: svc_log(svc_ctx, SVC_LOG_ERROR, "Error allocating LayerData\n"); michael@0: return 0; michael@0: } michael@0: ld_list_add(&cx_layer_list, layer_data); michael@0: michael@0: // save layer size in superframe index michael@0: superframe.sizes[superframe.count++] = frame_pkt_size; michael@0: superframe.magnitude |= frame_pkt_size; michael@0: break; michael@0: } michael@0: case VPX_CODEC_PSNR_PKT: { michael@0: if (!vpx_svc_dummy_frame(svc_ctx)) { michael@0: svc_log(svc_ctx, SVC_LOG_DEBUG, michael@0: "SVC frame: %d, layer: %d, PSNR(Total/Y/U/V): " michael@0: "%2.3f %2.3f %2.3f %2.3f \n", michael@0: si->encode_frame_count, si->layer, michael@0: cx_pkt->data.psnr.psnr[0], cx_pkt->data.psnr.psnr[1], michael@0: cx_pkt->data.psnr.psnr[2], cx_pkt->data.psnr.psnr[3]); michael@0: si->psnr_in_layer[si->layer] += cx_pkt->data.psnr.psnr[0]; michael@0: } michael@0: break; michael@0: } michael@0: default: { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: // add superframe index to layer data list michael@0: if (!vpx_svc_dummy_frame(svc_ctx)) { michael@0: sf_create_index(&superframe); michael@0: layer_data = ld_create(superframe.buffer, superframe.index_size); michael@0: ld_list_add(&cx_layer_list, layer_data); michael@0: } michael@0: // get accumulated size of layer data michael@0: si->frame_size = ld_list_get_buffer_size(cx_layer_list); michael@0: if (si->frame_size == 0) return VPX_CODEC_ERROR; michael@0: michael@0: // all layers encoded, create single buffer with concatenated layers michael@0: if (si->frame_size > si->buffer_size) { michael@0: free(si->buffer); michael@0: si->buffer = malloc(si->frame_size); michael@0: if (si->buffer == NULL) { michael@0: ld_list_free(cx_layer_list); michael@0: return VPX_CODEC_MEM_ERROR; michael@0: } michael@0: si->buffer_size = si->frame_size; michael@0: } michael@0: // copy layer data into packet michael@0: ld_list_copy_to_buffer(cx_layer_list, si->buffer); michael@0: michael@0: ld_list_free(cx_layer_list); michael@0: michael@0: svc_log(svc_ctx, SVC_LOG_DEBUG, "SVC frame: %d, kf: %d, size: %d, pts: %d\n", michael@0: si->encode_frame_count, si->is_keyframe, (int)si->frame_size, michael@0: (int)pts); michael@0: ++si->frame_within_gop; michael@0: ++si->encode_frame_count; michael@0: michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: const char *vpx_svc_get_message(const SvcContext *svc_ctx) { michael@0: const SvcInternal *const si = get_const_svc_internal(svc_ctx); michael@0: if (svc_ctx == NULL || si == NULL) return NULL; michael@0: return si->message_buffer; michael@0: } michael@0: michael@0: void *vpx_svc_get_buffer(const SvcContext *svc_ctx) { michael@0: const SvcInternal *const si = get_const_svc_internal(svc_ctx); michael@0: if (svc_ctx == NULL || si == NULL) return NULL; michael@0: return si->buffer; michael@0: } michael@0: michael@0: size_t vpx_svc_get_frame_size(const SvcContext *svc_ctx) { michael@0: const SvcInternal *const si = get_const_svc_internal(svc_ctx); michael@0: if (svc_ctx == NULL || si == NULL) return 0; michael@0: return si->frame_size; michael@0: } michael@0: michael@0: int vpx_svc_get_encode_frame_count(const SvcContext *svc_ctx) { michael@0: const SvcInternal *const si = get_const_svc_internal(svc_ctx); michael@0: if (svc_ctx == NULL || si == NULL) return 0; michael@0: return si->encode_frame_count; michael@0: } michael@0: michael@0: int vpx_svc_is_keyframe(const SvcContext *svc_ctx) { michael@0: const SvcInternal *const si = get_const_svc_internal(svc_ctx); michael@0: if (svc_ctx == NULL || si == NULL) return 0; michael@0: return si->is_keyframe; michael@0: } michael@0: michael@0: void vpx_svc_set_keyframe(SvcContext *svc_ctx) { michael@0: SvcInternal *const si = get_svc_internal(svc_ctx); michael@0: if (svc_ctx == NULL || si == NULL) return; michael@0: si->frame_within_gop = 0; michael@0: } michael@0: michael@0: // dump accumulated statistics and reset accumulated values michael@0: const char *vpx_svc_dump_statistics(SvcContext *svc_ctx) { michael@0: int number_of_frames, number_of_keyframes, encode_frame_count; michael@0: int i; michael@0: uint32_t bytes_total = 0; michael@0: SvcInternal *const si = get_svc_internal(svc_ctx); michael@0: if (svc_ctx == NULL || si == NULL) return NULL; michael@0: michael@0: svc_log_reset(svc_ctx); michael@0: michael@0: encode_frame_count = si->encode_frame_count; michael@0: if (svc_ctx->first_frame_full_size) encode_frame_count--; michael@0: if (si->encode_frame_count <= 0) return vpx_svc_get_message(svc_ctx); michael@0: michael@0: svc_log(svc_ctx, SVC_LOG_INFO, "\n"); michael@0: number_of_keyframes = encode_frame_count / si->kf_dist + 1; michael@0: for (i = 0; i < si->layers; ++i) { michael@0: number_of_frames = encode_frame_count; michael@0: michael@0: if (svc_ctx->encoding_mode == ALT_INTER_LAYER_PREDICTION_IP && michael@0: (i == 1 || i == 3)) { michael@0: number_of_frames -= number_of_keyframes; michael@0: } michael@0: svc_log(svc_ctx, SVC_LOG_INFO, "Layer %d PSNR=[%2.3f], Bytes=[%u]\n", i, michael@0: (double)si->psnr_in_layer[i] / number_of_frames, michael@0: si->bytes_in_layer[i]); michael@0: bytes_total += si->bytes_in_layer[i]; michael@0: si->psnr_in_layer[i] = 0; michael@0: si->bytes_in_layer[i] = 0; michael@0: } michael@0: michael@0: // only display statistics once michael@0: si->encode_frame_count = 0; michael@0: michael@0: svc_log(svc_ctx, SVC_LOG_INFO, "Total Bytes=[%u]\n", bytes_total); michael@0: return vpx_svc_get_message(svc_ctx); michael@0: } michael@0: michael@0: void vpx_svc_release(SvcContext *svc_ctx) { michael@0: SvcInternal *si; michael@0: if (svc_ctx == NULL) return; michael@0: // do not use get_svc_internal as it will unnecessarily allocate an michael@0: // SvcInternal if it was not already allocated michael@0: si = (SvcInternal *)svc_ctx->internal; michael@0: if (si != NULL) { michael@0: free(si->buffer); michael@0: free(si); michael@0: svc_ctx->internal = NULL; michael@0: } michael@0: }