michael@0: /* michael@0: * Copyright (c) 2010 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: #include "vp8_rtcd.h" michael@0: #include "vpx/vpx_codec.h" michael@0: #include "vpx/internal/vpx_codec_internal.h" michael@0: #include "vpx_version.h" michael@0: #include "vp8/encoder/onyx_int.h" michael@0: #include "vpx/vp8cx.h" michael@0: #include "vp8/encoder/firstpass.h" michael@0: #include "vp8/common/onyx.h" michael@0: #include michael@0: #include michael@0: michael@0: struct vp8_extracfg michael@0: { michael@0: struct vpx_codec_pkt_list *pkt_list; michael@0: int cpu_used; /** available cpu percentage in 1/16*/ michael@0: unsigned int enable_auto_alt_ref; /** if encoder decides to uses alternate reference frame */ michael@0: unsigned int noise_sensitivity; michael@0: unsigned int Sharpness; michael@0: unsigned int static_thresh; michael@0: unsigned int token_partitions; michael@0: unsigned int arnr_max_frames; /* alt_ref Noise Reduction Max Frame Count */ michael@0: unsigned int arnr_strength; /* alt_ref Noise Reduction Strength */ michael@0: unsigned int arnr_type; /* alt_ref filter type */ michael@0: vp8e_tuning tuning; michael@0: unsigned int cq_level; /* constrained quality level */ michael@0: unsigned int rc_max_intra_bitrate_pct; michael@0: michael@0: }; michael@0: michael@0: struct extraconfig_map michael@0: { michael@0: int usage; michael@0: struct vp8_extracfg cfg; michael@0: }; michael@0: michael@0: static const struct extraconfig_map extracfg_map[] = michael@0: { michael@0: { michael@0: 0, michael@0: { michael@0: NULL, michael@0: #if !(CONFIG_REALTIME_ONLY) michael@0: 0, /* cpu_used */ michael@0: #else michael@0: 4, /* cpu_used */ michael@0: #endif michael@0: 0, /* enable_auto_alt_ref */ michael@0: 0, /* noise_sensitivity */ michael@0: 0, /* Sharpness */ michael@0: 0, /* static_thresh */ michael@0: #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) michael@0: VP8_EIGHT_TOKENPARTITION, michael@0: #else michael@0: VP8_ONE_TOKENPARTITION, /* token_partitions */ michael@0: #endif michael@0: 0, /* arnr_max_frames */ michael@0: 3, /* arnr_strength */ michael@0: 3, /* arnr_type*/ michael@0: 0, /* tuning*/ michael@0: 10, /* cq_level */ michael@0: 0, /* rc_max_intra_bitrate_pct */ michael@0: } michael@0: } michael@0: }; michael@0: michael@0: struct vpx_codec_alg_priv michael@0: { michael@0: vpx_codec_priv_t base; michael@0: vpx_codec_enc_cfg_t cfg; michael@0: struct vp8_extracfg vp8_cfg; michael@0: VP8_CONFIG oxcf; michael@0: struct VP8_COMP *cpi; michael@0: unsigned char *cx_data; michael@0: unsigned int cx_data_sz; michael@0: vpx_image_t preview_img; michael@0: unsigned int next_frame_flag; michael@0: vp8_postproc_cfg_t preview_ppcfg; michael@0: /* pkt_list size depends on the maximum number of lagged frames allowed. */ michael@0: vpx_codec_pkt_list_decl(64) pkt_list; michael@0: unsigned int fixed_kf_cntr; michael@0: }; michael@0: michael@0: michael@0: static vpx_codec_err_t michael@0: update_error_state(vpx_codec_alg_priv_t *ctx, michael@0: const struct vpx_internal_error_info *error) michael@0: { michael@0: vpx_codec_err_t res; michael@0: michael@0: if ((res = error->error_code)) michael@0: ctx->base.err_detail = error->has_detail michael@0: ? error->detail michael@0: : NULL; michael@0: michael@0: return res; michael@0: } michael@0: michael@0: michael@0: #undef ERROR michael@0: #define ERROR(str) do {\ michael@0: ctx->base.err_detail = str;\ michael@0: return VPX_CODEC_INVALID_PARAM;\ michael@0: } while(0) michael@0: michael@0: #define RANGE_CHECK(p,memb,lo,hi) do {\ michael@0: if(!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \ michael@0: ERROR(#memb " out of range ["#lo".."#hi"]");\ michael@0: } while(0) michael@0: michael@0: #define RANGE_CHECK_HI(p,memb,hi) do {\ michael@0: if(!((p)->memb <= (hi))) \ michael@0: ERROR(#memb " out of range [.."#hi"]");\ michael@0: } while(0) michael@0: michael@0: #define RANGE_CHECK_LO(p,memb,lo) do {\ michael@0: if(!((p)->memb >= (lo))) \ michael@0: ERROR(#memb " out of range ["#lo"..]");\ michael@0: } while(0) michael@0: michael@0: #define RANGE_CHECK_BOOL(p,memb) do {\ michael@0: if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\ michael@0: } while(0) michael@0: michael@0: static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx, michael@0: const vpx_codec_enc_cfg_t *cfg, michael@0: const struct vp8_extracfg *vp8_cfg, michael@0: int finalize) michael@0: { michael@0: RANGE_CHECK(cfg, g_w, 1, 16383); /* 14 bits available */ michael@0: RANGE_CHECK(cfg, g_h, 1, 16383); /* 14 bits available */ michael@0: RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000); michael@0: RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den); michael@0: RANGE_CHECK_HI(cfg, g_profile, 3); michael@0: RANGE_CHECK_HI(cfg, rc_max_quantizer, 63); michael@0: RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer); michael@0: RANGE_CHECK_HI(cfg, g_threads, 64); michael@0: #if CONFIG_REALTIME_ONLY michael@0: RANGE_CHECK_HI(cfg, g_lag_in_frames, 0); michael@0: #elif CONFIG_MULTI_RES_ENCODING michael@0: if (ctx->base.enc.total_encoders > 1) michael@0: RANGE_CHECK_HI(cfg, g_lag_in_frames, 0); michael@0: #else michael@0: RANGE_CHECK_HI(cfg, g_lag_in_frames, 25); michael@0: #endif michael@0: RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q); michael@0: RANGE_CHECK_HI(cfg, rc_undershoot_pct, 1000); michael@0: RANGE_CHECK_HI(cfg, rc_overshoot_pct, 1000); michael@0: RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100); michael@0: RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO); michael@0: michael@0: /* TODO: add spatial re-sampling support and frame dropping in michael@0: * multi-res-encoder.*/ michael@0: #if CONFIG_MULTI_RES_ENCODING michael@0: if (ctx->base.enc.total_encoders > 1) michael@0: RANGE_CHECK_HI(cfg, rc_resize_allowed, 0); michael@0: #else michael@0: RANGE_CHECK_BOOL(cfg, rc_resize_allowed); michael@0: #endif michael@0: RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100); michael@0: RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100); michael@0: RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100); michael@0: michael@0: #if CONFIG_REALTIME_ONLY michael@0: RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS); michael@0: #elif CONFIG_MULTI_RES_ENCODING michael@0: if (ctx->base.enc.total_encoders > 1) michael@0: RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS); michael@0: #else michael@0: RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS); michael@0: #endif michael@0: michael@0: /* VP8 does not support a lower bound on the keyframe interval in michael@0: * automatic keyframe placement mode. michael@0: */ michael@0: if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist michael@0: && cfg->kf_min_dist > 0) michael@0: ERROR("kf_min_dist not supported in auto mode, use 0 " michael@0: "or kf_max_dist instead."); michael@0: michael@0: RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref); michael@0: RANGE_CHECK(vp8_cfg, cpu_used, -16, 16); michael@0: michael@0: #if CONFIG_REALTIME_ONLY && !CONFIG_TEMPORAL_DENOISING michael@0: RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0); michael@0: #else michael@0: RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6); michael@0: #endif michael@0: michael@0: RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION, michael@0: VP8_EIGHT_TOKENPARTITION); michael@0: RANGE_CHECK_HI(vp8_cfg, Sharpness, 7); michael@0: RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15); michael@0: RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6); michael@0: RANGE_CHECK(vp8_cfg, arnr_type, 1, 3); michael@0: RANGE_CHECK(vp8_cfg, cq_level, 0, 63); michael@0: if (finalize && (cfg->rc_end_usage == VPX_CQ || cfg->rc_end_usage == VPX_Q)) michael@0: RANGE_CHECK(vp8_cfg, cq_level, michael@0: cfg->rc_min_quantizer, cfg->rc_max_quantizer); michael@0: michael@0: #if !(CONFIG_REALTIME_ONLY) michael@0: if (cfg->g_pass == VPX_RC_LAST_PASS) michael@0: { michael@0: size_t packet_sz = sizeof(FIRSTPASS_STATS); michael@0: int n_packets = (int)(cfg->rc_twopass_stats_in.sz / michael@0: packet_sz); michael@0: FIRSTPASS_STATS *stats; michael@0: michael@0: if (!cfg->rc_twopass_stats_in.buf) michael@0: ERROR("rc_twopass_stats_in.buf not set."); michael@0: michael@0: if (cfg->rc_twopass_stats_in.sz % packet_sz) michael@0: ERROR("rc_twopass_stats_in.sz indicates truncated packet."); michael@0: michael@0: if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz) michael@0: ERROR("rc_twopass_stats_in requires at least two packets."); michael@0: michael@0: stats = (void*)((char *)cfg->rc_twopass_stats_in.buf michael@0: + (n_packets - 1) * packet_sz); michael@0: michael@0: if ((int)(stats->count + 0.5) != n_packets - 1) michael@0: ERROR("rc_twopass_stats_in missing EOS stats packet"); michael@0: } michael@0: #endif michael@0: michael@0: RANGE_CHECK(cfg, ts_number_layers, 1, 5); michael@0: michael@0: if (cfg->ts_number_layers > 1) michael@0: { michael@0: unsigned int i; michael@0: RANGE_CHECK_HI(cfg, ts_periodicity, 16); michael@0: michael@0: for (i=1; its_number_layers; i++) michael@0: if (cfg->ts_target_bitrate[i] <= cfg->ts_target_bitrate[i-1]) michael@0: ERROR("ts_target_bitrate entries are not strictly increasing"); michael@0: michael@0: RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers-1], 1, 1); michael@0: for (i=cfg->ts_number_layers-2; i>0; i--) michael@0: if (cfg->ts_rate_decimator[i-1] != 2*cfg->ts_rate_decimator[i]) michael@0: ERROR("ts_rate_decimator factors are not powers of 2"); michael@0: michael@0: RANGE_CHECK_HI(cfg, ts_layer_id[i], cfg->ts_number_layers-1); michael@0: } michael@0: michael@0: #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) michael@0: if(cfg->g_threads > (1 << vp8_cfg->token_partitions)) michael@0: ERROR("g_threads cannot be bigger than number of token partitions"); michael@0: #endif michael@0: michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: michael@0: static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx, michael@0: const vpx_image_t *img) michael@0: { michael@0: switch (img->fmt) michael@0: { michael@0: case VPX_IMG_FMT_YV12: michael@0: case VPX_IMG_FMT_I420: michael@0: case VPX_IMG_FMT_VPXI420: michael@0: case VPX_IMG_FMT_VPXYV12: michael@0: break; michael@0: default: michael@0: ERROR("Invalid image format. Only YV12 and I420 images are supported"); michael@0: } michael@0: michael@0: if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h)) michael@0: ERROR("Image size must match encoder init configuration size"); michael@0: michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: michael@0: static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf, michael@0: vpx_codec_enc_cfg_t cfg, michael@0: struct vp8_extracfg vp8_cfg, michael@0: vpx_codec_priv_enc_mr_cfg_t *mr_cfg) michael@0: { michael@0: oxcf->multi_threaded = cfg.g_threads; michael@0: oxcf->Version = cfg.g_profile; michael@0: michael@0: oxcf->Width = cfg.g_w; michael@0: oxcf->Height = cfg.g_h; michael@0: oxcf->timebase = cfg.g_timebase; michael@0: michael@0: oxcf->error_resilient_mode = cfg.g_error_resilient; michael@0: michael@0: switch (cfg.g_pass) michael@0: { michael@0: case VPX_RC_ONE_PASS: michael@0: oxcf->Mode = MODE_BESTQUALITY; michael@0: break; michael@0: case VPX_RC_FIRST_PASS: michael@0: oxcf->Mode = MODE_FIRSTPASS; michael@0: break; michael@0: case VPX_RC_LAST_PASS: michael@0: oxcf->Mode = MODE_SECONDPASS_BEST; michael@0: break; michael@0: } michael@0: michael@0: if (cfg.g_pass == VPX_RC_FIRST_PASS || cfg.g_pass == VPX_RC_ONE_PASS) michael@0: { michael@0: oxcf->allow_lag = 0; michael@0: oxcf->lag_in_frames = 0; michael@0: } michael@0: else michael@0: { michael@0: oxcf->allow_lag = (cfg.g_lag_in_frames) > 0; michael@0: oxcf->lag_in_frames = cfg.g_lag_in_frames; michael@0: } michael@0: michael@0: oxcf->allow_df = (cfg.rc_dropframe_thresh > 0); michael@0: oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh; michael@0: michael@0: oxcf->allow_spatial_resampling = cfg.rc_resize_allowed; michael@0: oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh; michael@0: oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh; michael@0: michael@0: if (cfg.rc_end_usage == VPX_VBR) { michael@0: oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK; michael@0: } else if (cfg.rc_end_usage == VPX_CBR) { michael@0: oxcf->end_usage = USAGE_STREAM_FROM_SERVER; michael@0: } else if (cfg.rc_end_usage == VPX_CQ) { michael@0: oxcf->end_usage = USAGE_CONSTRAINED_QUALITY; michael@0: } else if (cfg.rc_end_usage == VPX_Q) { michael@0: oxcf->end_usage = USAGE_CONSTANT_QUALITY; michael@0: } michael@0: michael@0: oxcf->target_bandwidth = cfg.rc_target_bitrate; michael@0: oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct; michael@0: michael@0: oxcf->best_allowed_q = cfg.rc_min_quantizer; michael@0: oxcf->worst_allowed_q = cfg.rc_max_quantizer; michael@0: oxcf->cq_level = vp8_cfg.cq_level; michael@0: oxcf->fixed_q = -1; michael@0: michael@0: oxcf->under_shoot_pct = cfg.rc_undershoot_pct; michael@0: oxcf->over_shoot_pct = cfg.rc_overshoot_pct; michael@0: michael@0: oxcf->maximum_buffer_size_in_ms = cfg.rc_buf_sz; michael@0: oxcf->starting_buffer_level_in_ms = cfg.rc_buf_initial_sz; michael@0: oxcf->optimal_buffer_level_in_ms = cfg.rc_buf_optimal_sz; michael@0: michael@0: oxcf->maximum_buffer_size = cfg.rc_buf_sz; michael@0: oxcf->starting_buffer_level = cfg.rc_buf_initial_sz; michael@0: oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz; michael@0: michael@0: oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct; michael@0: oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct; michael@0: oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct; michael@0: michael@0: oxcf->auto_key = cfg.kf_mode == VPX_KF_AUTO michael@0: && cfg.kf_min_dist != cfg.kf_max_dist; michael@0: oxcf->key_freq = cfg.kf_max_dist; michael@0: michael@0: oxcf->number_of_layers = cfg.ts_number_layers; michael@0: oxcf->periodicity = cfg.ts_periodicity; michael@0: michael@0: if (oxcf->number_of_layers > 1) michael@0: { michael@0: memcpy (oxcf->target_bitrate, cfg.ts_target_bitrate, michael@0: sizeof(cfg.ts_target_bitrate)); michael@0: memcpy (oxcf->rate_decimator, cfg.ts_rate_decimator, michael@0: sizeof(cfg.ts_rate_decimator)); michael@0: memcpy (oxcf->layer_id, cfg.ts_layer_id, sizeof(cfg.ts_layer_id)); michael@0: } michael@0: michael@0: #if CONFIG_MULTI_RES_ENCODING michael@0: /* When mr_cfg is NULL, oxcf->mr_total_resolutions and oxcf->mr_encoder_id michael@0: * are both memset to 0, which ensures the correct logic under this michael@0: * situation. michael@0: */ michael@0: if(mr_cfg) michael@0: { michael@0: oxcf->mr_total_resolutions = mr_cfg->mr_total_resolutions; michael@0: oxcf->mr_encoder_id = mr_cfg->mr_encoder_id; michael@0: oxcf->mr_down_sampling_factor.num = mr_cfg->mr_down_sampling_factor.num; michael@0: oxcf->mr_down_sampling_factor.den = mr_cfg->mr_down_sampling_factor.den; michael@0: oxcf->mr_low_res_mode_info = mr_cfg->mr_low_res_mode_info; michael@0: } michael@0: #endif michael@0: michael@0: oxcf->cpu_used = vp8_cfg.cpu_used; michael@0: oxcf->encode_breakout = vp8_cfg.static_thresh; michael@0: oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref; michael@0: oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity; michael@0: oxcf->Sharpness = vp8_cfg.Sharpness; michael@0: oxcf->token_partitions = vp8_cfg.token_partitions; michael@0: michael@0: oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in; michael@0: oxcf->output_pkt_list = vp8_cfg.pkt_list; michael@0: michael@0: oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames; michael@0: oxcf->arnr_strength = vp8_cfg.arnr_strength; michael@0: oxcf->arnr_type = vp8_cfg.arnr_type; michael@0: michael@0: oxcf->tuning = vp8_cfg.tuning; michael@0: michael@0: /* michael@0: printf("Current VP8 Settings: \n"); michael@0: printf("target_bandwidth: %d\n", oxcf->target_bandwidth); michael@0: printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity); michael@0: printf("Sharpness: %d\n", oxcf->Sharpness); michael@0: printf("cpu_used: %d\n", oxcf->cpu_used); michael@0: printf("Mode: %d\n", oxcf->Mode); michael@0: printf("delete_first_pass_file: %d\n", oxcf->delete_first_pass_file); michael@0: printf("auto_key: %d\n", oxcf->auto_key); michael@0: printf("key_freq: %d\n", oxcf->key_freq); michael@0: printf("end_usage: %d\n", oxcf->end_usage); michael@0: printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct); michael@0: printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct); michael@0: printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level); michael@0: printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level); michael@0: printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size); michael@0: printf("fixed_q: %d\n", oxcf->fixed_q); michael@0: printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q); michael@0: printf("best_allowed_q: %d\n", oxcf->best_allowed_q); michael@0: printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling); michael@0: printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark); michael@0: printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark); michael@0: printf("allow_df: %d\n", oxcf->allow_df); michael@0: printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark); michael@0: printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias); michael@0: printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section); michael@0: printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section); michael@0: printf("allow_lag: %d\n", oxcf->allow_lag); michael@0: printf("lag_in_frames: %d\n", oxcf->lag_in_frames); michael@0: printf("play_alternate: %d\n", oxcf->play_alternate); michael@0: printf("Version: %d\n", oxcf->Version); michael@0: printf("multi_threaded: %d\n", oxcf->multi_threaded); michael@0: printf("encode_breakout: %d\n", oxcf->encode_breakout); michael@0: */ michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx, michael@0: const vpx_codec_enc_cfg_t *cfg) michael@0: { michael@0: vpx_codec_err_t res; michael@0: michael@0: if (((cfg->g_w != ctx->cfg.g_w) || (cfg->g_h != ctx->cfg.g_h)) michael@0: && (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)) michael@0: ERROR("Cannot change width or height after initialization"); michael@0: michael@0: /* Prevent increasing lag_in_frames. This check is stricter than it needs michael@0: * to be -- the limit is not increasing past the first lag_in_frames michael@0: * value, but we don't track the initial config, only the last successful michael@0: * config. michael@0: */ michael@0: if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)) michael@0: ERROR("Cannot increase lag_in_frames"); michael@0: michael@0: res = validate_config(ctx, cfg, &ctx->vp8_cfg, 0); michael@0: michael@0: if (!res) michael@0: { michael@0: ctx->cfg = *cfg; michael@0: set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL); michael@0: vp8_change_config(ctx->cpi, &ctx->oxcf); michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: michael@0: michael@0: int vp8_reverse_trans(int); michael@0: michael@0: michael@0: static vpx_codec_err_t get_param(vpx_codec_alg_priv_t *ctx, michael@0: int ctrl_id, michael@0: va_list args) michael@0: { michael@0: void *arg = va_arg(args, void *); michael@0: michael@0: #define MAP(id, var) case id: *(RECAST(id, arg)) = var; break michael@0: michael@0: if (!arg) michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: michael@0: switch (ctrl_id) michael@0: { michael@0: MAP(VP8E_GET_LAST_QUANTIZER, vp8_get_quantizer(ctx->cpi)); michael@0: MAP(VP8E_GET_LAST_QUANTIZER_64, vp8_reverse_trans(vp8_get_quantizer(ctx->cpi))); michael@0: } michael@0: michael@0: return VPX_CODEC_OK; michael@0: #undef MAP michael@0: } michael@0: michael@0: michael@0: static vpx_codec_err_t set_param(vpx_codec_alg_priv_t *ctx, michael@0: int ctrl_id, michael@0: va_list args) michael@0: { michael@0: vpx_codec_err_t res = VPX_CODEC_OK; michael@0: struct vp8_extracfg xcfg = ctx->vp8_cfg; michael@0: michael@0: #define MAP(id, var) case id: var = CAST(id, args); break; michael@0: michael@0: switch (ctrl_id) michael@0: { michael@0: MAP(VP8E_SET_CPUUSED, xcfg.cpu_used); michael@0: MAP(VP8E_SET_ENABLEAUTOALTREF, xcfg.enable_auto_alt_ref); michael@0: MAP(VP8E_SET_NOISE_SENSITIVITY, xcfg.noise_sensitivity); michael@0: MAP(VP8E_SET_SHARPNESS, xcfg.Sharpness); michael@0: MAP(VP8E_SET_STATIC_THRESHOLD, xcfg.static_thresh); michael@0: MAP(VP8E_SET_TOKEN_PARTITIONS, xcfg.token_partitions); michael@0: michael@0: MAP(VP8E_SET_ARNR_MAXFRAMES, xcfg.arnr_max_frames); michael@0: MAP(VP8E_SET_ARNR_STRENGTH , xcfg.arnr_strength); michael@0: MAP(VP8E_SET_ARNR_TYPE , xcfg.arnr_type); michael@0: MAP(VP8E_SET_TUNING, xcfg.tuning); michael@0: MAP(VP8E_SET_CQ_LEVEL, xcfg.cq_level); michael@0: MAP(VP8E_SET_MAX_INTRA_BITRATE_PCT, xcfg.rc_max_intra_bitrate_pct); michael@0: michael@0: } michael@0: michael@0: res = validate_config(ctx, &ctx->cfg, &xcfg, 0); michael@0: michael@0: if (!res) michael@0: { michael@0: ctx->vp8_cfg = xcfg; michael@0: set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL); michael@0: vp8_change_config(ctx->cpi, &ctx->oxcf); michael@0: } michael@0: michael@0: return res; michael@0: #undef MAP michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t *cfg, michael@0: void **mem_loc) michael@0: { michael@0: vpx_codec_err_t res = 0; michael@0: michael@0: #if CONFIG_MULTI_RES_ENCODING michael@0: LOWER_RES_FRAME_INFO *shared_mem_loc; michael@0: int mb_rows = ((cfg->g_w + 15) >>4); michael@0: int mb_cols = ((cfg->g_h + 15) >>4); michael@0: michael@0: shared_mem_loc = calloc(1, sizeof(LOWER_RES_FRAME_INFO)); michael@0: if(!shared_mem_loc) michael@0: { michael@0: res = VPX_CODEC_MEM_ERROR; michael@0: } michael@0: michael@0: shared_mem_loc->mb_info = calloc(mb_rows*mb_cols, sizeof(LOWER_RES_MB_INFO)); michael@0: if(!(shared_mem_loc->mb_info)) michael@0: { michael@0: res = VPX_CODEC_MEM_ERROR; michael@0: } michael@0: else michael@0: { michael@0: *mem_loc = (void *)shared_mem_loc; michael@0: res = VPX_CODEC_OK; michael@0: } michael@0: #endif michael@0: return res; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx, michael@0: vpx_codec_priv_enc_mr_cfg_t *mr_cfg) michael@0: { michael@0: vpx_codec_err_t res = VPX_CODEC_OK; michael@0: struct vpx_codec_alg_priv *priv; michael@0: vpx_codec_enc_cfg_t *cfg; michael@0: unsigned int i; michael@0: michael@0: struct VP8_COMP *optr; michael@0: michael@0: vp8_rtcd(); michael@0: michael@0: if (!ctx->priv) michael@0: { michael@0: priv = calloc(1, sizeof(struct vpx_codec_alg_priv)); michael@0: michael@0: if (!priv) michael@0: { michael@0: return VPX_CODEC_MEM_ERROR; michael@0: } michael@0: michael@0: ctx->priv = &priv->base; michael@0: ctx->priv->sz = sizeof(*ctx->priv); michael@0: ctx->priv->iface = ctx->iface; michael@0: ctx->priv->alg_priv = priv; michael@0: ctx->priv->init_flags = ctx->init_flags; michael@0: michael@0: if (ctx->config.enc) michael@0: { michael@0: /* Update the reference to the config structure to an michael@0: * internal copy. michael@0: */ michael@0: ctx->priv->alg_priv->cfg = *ctx->config.enc; michael@0: ctx->config.enc = &ctx->priv->alg_priv->cfg; michael@0: } michael@0: michael@0: cfg = &ctx->priv->alg_priv->cfg; michael@0: michael@0: /* Select the extra vp8 configuration table based on the current michael@0: * usage value. If the current usage value isn't found, use the michael@0: * values for usage case 0. michael@0: */ michael@0: for (i = 0; michael@0: extracfg_map[i].usage && extracfg_map[i].usage != cfg->g_usage; michael@0: i++); michael@0: michael@0: priv->vp8_cfg = extracfg_map[i].cfg; michael@0: priv->vp8_cfg.pkt_list = &priv->pkt_list.head; michael@0: michael@0: priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2; michael@0: michael@0: if (priv->cx_data_sz < 32768) priv->cx_data_sz = 32768; michael@0: michael@0: priv->cx_data = malloc(priv->cx_data_sz); michael@0: michael@0: if (!priv->cx_data) michael@0: { michael@0: return VPX_CODEC_MEM_ERROR; michael@0: } michael@0: michael@0: if(mr_cfg) michael@0: ctx->priv->enc.total_encoders = mr_cfg->mr_total_resolutions; michael@0: else michael@0: ctx->priv->enc.total_encoders = 1; michael@0: michael@0: res = validate_config(priv, &priv->cfg, &priv->vp8_cfg, 0); michael@0: michael@0: if (!res) michael@0: { michael@0: set_vp8e_config(&ctx->priv->alg_priv->oxcf, michael@0: ctx->priv->alg_priv->cfg, michael@0: ctx->priv->alg_priv->vp8_cfg, michael@0: mr_cfg); michael@0: michael@0: optr = vp8_create_compressor(&ctx->priv->alg_priv->oxcf); michael@0: michael@0: if (!optr) michael@0: res = VPX_CODEC_MEM_ERROR; michael@0: else michael@0: ctx->priv->alg_priv->cpi = optr; michael@0: } michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx) michael@0: { michael@0: #if CONFIG_MULTI_RES_ENCODING michael@0: /* Free multi-encoder shared memory */ michael@0: if (ctx->oxcf.mr_total_resolutions > 0 && (ctx->oxcf.mr_encoder_id == ctx->oxcf.mr_total_resolutions-1)) michael@0: { michael@0: LOWER_RES_FRAME_INFO *shared_mem_loc = (LOWER_RES_FRAME_INFO *)ctx->oxcf.mr_low_res_mode_info; michael@0: free(shared_mem_loc->mb_info); michael@0: free(ctx->oxcf.mr_low_res_mode_info); michael@0: } michael@0: #endif michael@0: michael@0: free(ctx->cx_data); michael@0: vp8_remove_compressor(&ctx->cpi); michael@0: free(ctx); michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img, michael@0: YV12_BUFFER_CONFIG *yv12) michael@0: { michael@0: vpx_codec_err_t res = VPX_CODEC_OK; michael@0: yv12->y_buffer = img->planes[VPX_PLANE_Y]; michael@0: yv12->u_buffer = img->planes[VPX_PLANE_U]; michael@0: yv12->v_buffer = img->planes[VPX_PLANE_V]; michael@0: michael@0: yv12->y_crop_width = img->d_w; michael@0: yv12->y_crop_height = img->d_h; michael@0: yv12->y_width = img->d_w; michael@0: yv12->y_height = img->d_h; michael@0: yv12->uv_width = (1 + yv12->y_width) / 2; michael@0: yv12->uv_height = (1 + yv12->y_height) / 2; michael@0: michael@0: yv12->y_stride = img->stride[VPX_PLANE_Y]; michael@0: yv12->uv_stride = img->stride[VPX_PLANE_U]; michael@0: michael@0: yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2; michael@0: return res; michael@0: } michael@0: michael@0: static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx, michael@0: unsigned long duration, michael@0: unsigned long deadline) michael@0: { michael@0: unsigned int new_qc; michael@0: michael@0: #if !(CONFIG_REALTIME_ONLY) michael@0: /* Use best quality mode if no deadline is given. */ michael@0: new_qc = MODE_BESTQUALITY; michael@0: michael@0: if (deadline) michael@0: { michael@0: uint64_t duration_us; michael@0: michael@0: /* Convert duration parameter from stream timebase to microseconds */ michael@0: duration_us = (uint64_t)duration * 1000000 michael@0: * (uint64_t)ctx->cfg.g_timebase.num michael@0: / (uint64_t)ctx->cfg.g_timebase.den; michael@0: michael@0: /* If the deadline is more that the duration this frame is to be shown, michael@0: * use good quality mode. Otherwise use realtime mode. michael@0: */ michael@0: new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME; michael@0: } michael@0: michael@0: #else michael@0: new_qc = MODE_REALTIME; michael@0: #endif michael@0: michael@0: if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS) michael@0: new_qc = MODE_FIRSTPASS; michael@0: else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS) michael@0: new_qc = (new_qc == MODE_BESTQUALITY) michael@0: ? MODE_SECONDPASS_BEST michael@0: : MODE_SECONDPASS; michael@0: michael@0: if (ctx->oxcf.Mode != new_qc) michael@0: { michael@0: ctx->oxcf.Mode = new_qc; michael@0: vp8_change_config(ctx->cpi, &ctx->oxcf); michael@0: } michael@0: } michael@0: michael@0: michael@0: static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx, michael@0: const vpx_image_t *img, michael@0: vpx_codec_pts_t pts, michael@0: unsigned long duration, michael@0: vpx_enc_frame_flags_t flags, michael@0: unsigned long deadline) michael@0: { michael@0: vpx_codec_err_t res = VPX_CODEC_OK; michael@0: michael@0: if (!ctx->cfg.rc_target_bitrate) michael@0: return res; michael@0: michael@0: if (!ctx->cfg.rc_target_bitrate) michael@0: return res; michael@0: michael@0: if (img) michael@0: res = validate_img(ctx, img); michael@0: michael@0: if (!res) michael@0: res = validate_config(ctx, &ctx->cfg, &ctx->vp8_cfg, 1); michael@0: michael@0: pick_quickcompress_mode(ctx, duration, deadline); michael@0: vpx_codec_pkt_list_init(&ctx->pkt_list); michael@0: michael@0: /* Handle Flags */ michael@0: if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF)) michael@0: || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) michael@0: { michael@0: ctx->base.err_detail = "Conflicting flags."; michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: michael@0: if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF michael@0: | VP8_EFLAG_NO_REF_ARF)) michael@0: { michael@0: int ref = 7; michael@0: michael@0: if (flags & VP8_EFLAG_NO_REF_LAST) michael@0: ref ^= VP8_LAST_FRAME; michael@0: michael@0: if (flags & VP8_EFLAG_NO_REF_GF) michael@0: ref ^= VP8_GOLD_FRAME; michael@0: michael@0: if (flags & VP8_EFLAG_NO_REF_ARF) michael@0: ref ^= VP8_ALTR_FRAME; michael@0: michael@0: vp8_use_as_reference(ctx->cpi, ref); michael@0: } michael@0: michael@0: if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF michael@0: | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF michael@0: | VP8_EFLAG_FORCE_ARF)) michael@0: { michael@0: int upd = 7; michael@0: michael@0: if (flags & VP8_EFLAG_NO_UPD_LAST) michael@0: upd ^= VP8_LAST_FRAME; michael@0: michael@0: if (flags & VP8_EFLAG_NO_UPD_GF) michael@0: upd ^= VP8_GOLD_FRAME; michael@0: michael@0: if (flags & VP8_EFLAG_NO_UPD_ARF) michael@0: upd ^= VP8_ALTR_FRAME; michael@0: michael@0: vp8_update_reference(ctx->cpi, upd); michael@0: } michael@0: michael@0: if (flags & VP8_EFLAG_NO_UPD_ENTROPY) michael@0: { michael@0: vp8_update_entropy(ctx->cpi, 0); michael@0: } michael@0: michael@0: /* Handle fixed keyframe intervals */ michael@0: if (ctx->cfg.kf_mode == VPX_KF_AUTO michael@0: && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) michael@0: { michael@0: if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) michael@0: { michael@0: flags |= VPX_EFLAG_FORCE_KF; michael@0: ctx->fixed_kf_cntr = 1; michael@0: } michael@0: } michael@0: michael@0: /* Initialize the encoder instance on the first frame*/ michael@0: if (!res && ctx->cpi) michael@0: { michael@0: unsigned int lib_flags; michael@0: YV12_BUFFER_CONFIG sd; michael@0: int64_t dst_time_stamp, dst_end_time_stamp; michael@0: unsigned long size, cx_data_sz; michael@0: unsigned char *cx_data; michael@0: unsigned char *cx_data_end; michael@0: int comp_data_state = 0; michael@0: michael@0: /* Set up internal flags */ michael@0: if (ctx->base.init_flags & VPX_CODEC_USE_PSNR) michael@0: ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1; michael@0: michael@0: if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION) michael@0: ((VP8_COMP *)ctx->cpi)->output_partition = 1; michael@0: michael@0: /* Convert API flags to internal codec lib flags */ michael@0: lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0; michael@0: michael@0: /* vp8 use 10,000,000 ticks/second as time stamp */ michael@0: dst_time_stamp = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den; michael@0: dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den; michael@0: michael@0: if (img != NULL) michael@0: { michael@0: res = image2yuvconfig(img, &sd); michael@0: michael@0: if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags, michael@0: &sd, dst_time_stamp, dst_end_time_stamp)) michael@0: { michael@0: VP8_COMP *cpi = (VP8_COMP *)ctx->cpi; michael@0: res = update_error_state(ctx, &cpi->common.error); michael@0: } michael@0: michael@0: /* reset for next frame */ michael@0: ctx->next_frame_flag = 0; michael@0: } michael@0: michael@0: cx_data = ctx->cx_data; michael@0: cx_data_sz = ctx->cx_data_sz; michael@0: cx_data_end = ctx->cx_data + cx_data_sz; michael@0: lib_flags = 0; michael@0: michael@0: while (cx_data_sz >= ctx->cx_data_sz / 2) michael@0: { michael@0: comp_data_state = vp8_get_compressed_data(ctx->cpi, michael@0: &lib_flags, michael@0: &size, michael@0: cx_data, michael@0: cx_data_end, michael@0: &dst_time_stamp, michael@0: &dst_end_time_stamp, michael@0: !img); michael@0: michael@0: if(comp_data_state == VPX_CODEC_CORRUPT_FRAME) michael@0: return VPX_CODEC_CORRUPT_FRAME; michael@0: else if(comp_data_state == -1) michael@0: break; michael@0: michael@0: if (size) michael@0: { michael@0: vpx_codec_pts_t round, delta; michael@0: vpx_codec_cx_pkt_t pkt; michael@0: VP8_COMP *cpi = (VP8_COMP *)ctx->cpi; michael@0: michael@0: /* Add the frame packet to the list of returned packets. */ michael@0: round = (vpx_codec_pts_t)1000000 michael@0: * ctx->cfg.g_timebase.num / 2 - 1; michael@0: delta = (dst_end_time_stamp - dst_time_stamp); michael@0: pkt.kind = VPX_CODEC_CX_FRAME_PKT; michael@0: pkt.data.frame.pts = michael@0: (dst_time_stamp * ctx->cfg.g_timebase.den + round) michael@0: / ctx->cfg.g_timebase.num / 10000000; michael@0: pkt.data.frame.duration = (unsigned long) michael@0: ((delta * ctx->cfg.g_timebase.den + round) michael@0: / ctx->cfg.g_timebase.num / 10000000); michael@0: pkt.data.frame.flags = lib_flags << 16; michael@0: michael@0: if (lib_flags & FRAMEFLAGS_KEY) michael@0: pkt.data.frame.flags |= VPX_FRAME_IS_KEY; michael@0: michael@0: if (!cpi->common.show_frame) michael@0: { michael@0: pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE; michael@0: michael@0: /* This timestamp should be as close as possible to the michael@0: * prior PTS so that if a decoder uses pts to schedule when michael@0: * to do this, we start right after last frame was decoded. michael@0: * Invisible frames have no duration. michael@0: */ michael@0: pkt.data.frame.pts = ((cpi->last_time_stamp_seen michael@0: * ctx->cfg.g_timebase.den + round) michael@0: / ctx->cfg.g_timebase.num / 10000000) + 1; michael@0: pkt.data.frame.duration = 0; michael@0: } michael@0: michael@0: if (cpi->droppable) michael@0: pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE; michael@0: michael@0: if (cpi->output_partition) michael@0: { michael@0: int i; michael@0: const int num_partitions = michael@0: (1 << cpi->common.multi_token_partition) + 1; michael@0: michael@0: pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT; michael@0: michael@0: for (i = 0; i < num_partitions; ++i) michael@0: { michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: pkt.data.frame.buf = cpi->partition_d[i]; michael@0: #else michael@0: pkt.data.frame.buf = cx_data; michael@0: cx_data += cpi->partition_sz[i]; michael@0: cx_data_sz -= cpi->partition_sz[i]; michael@0: #endif michael@0: pkt.data.frame.sz = cpi->partition_sz[i]; michael@0: pkt.data.frame.partition_id = i; michael@0: /* don't set the fragment bit for the last partition */ michael@0: if (i == (num_partitions - 1)) michael@0: pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT; michael@0: vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt); michael@0: } michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: /* In lagged mode the encoder can buffer multiple frames. michael@0: * We don't want this in partitioned output because michael@0: * partitions are spread all over the output buffer. michael@0: * So, force an exit! michael@0: */ michael@0: cx_data_sz -= ctx->cx_data_sz / 2; michael@0: #endif michael@0: } michael@0: else michael@0: { michael@0: pkt.data.frame.buf = cx_data; michael@0: pkt.data.frame.sz = size; michael@0: pkt.data.frame.partition_id = -1; michael@0: vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt); michael@0: cx_data += size; michael@0: cx_data_sz -= size; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: michael@0: michael@0: static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx, michael@0: vpx_codec_iter_t *iter) michael@0: { michael@0: return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter); michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx, michael@0: int ctr_id, michael@0: va_list args) michael@0: { michael@0: vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *); michael@0: michael@0: if (data) michael@0: { michael@0: vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data; michael@0: YV12_BUFFER_CONFIG sd; michael@0: michael@0: image2yuvconfig(&frame->img, &sd); michael@0: vp8_set_reference(ctx->cpi, frame->frame_type, &sd); michael@0: return VPX_CODEC_OK; michael@0: } michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx, michael@0: int ctr_id, michael@0: va_list args) michael@0: { michael@0: michael@0: vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *); michael@0: michael@0: if (data) michael@0: { michael@0: vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data; michael@0: YV12_BUFFER_CONFIG sd; michael@0: michael@0: image2yuvconfig(&frame->img, &sd); michael@0: vp8_get_reference(ctx->cpi, frame->frame_type, &sd); michael@0: return VPX_CODEC_OK; michael@0: } michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx, michael@0: int ctr_id, michael@0: va_list args) michael@0: { michael@0: #if CONFIG_POSTPROC michael@0: vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *); michael@0: (void)ctr_id; michael@0: michael@0: if (data) michael@0: { michael@0: ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data); michael@0: return VPX_CODEC_OK; michael@0: } michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: #else michael@0: (void)ctx; michael@0: (void)ctr_id; michael@0: (void)args; michael@0: return VPX_CODEC_INCAPABLE; michael@0: #endif michael@0: } michael@0: michael@0: michael@0: static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx) michael@0: { michael@0: michael@0: YV12_BUFFER_CONFIG sd; michael@0: vp8_ppflags_t flags = {0}; michael@0: michael@0: if (ctx->preview_ppcfg.post_proc_flag) michael@0: { michael@0: flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag; michael@0: flags.deblocking_level = ctx->preview_ppcfg.deblocking_level; michael@0: flags.noise_level = ctx->preview_ppcfg.noise_level; michael@0: } michael@0: michael@0: if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags)) michael@0: { michael@0: michael@0: /* michael@0: vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12, michael@0: sd.y_width + 2*VP8BORDERINPIXELS, michael@0: sd.y_height + 2*VP8BORDERINPIXELS, michael@0: 1, michael@0: sd.buffer_alloc); michael@0: vpx_img_set_rect(&ctx->preview_img, michael@0: VP8BORDERINPIXELS, VP8BORDERINPIXELS, michael@0: sd.y_width, sd.y_height); michael@0: */ michael@0: michael@0: ctx->preview_img.bps = 12; michael@0: ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer; michael@0: ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer; michael@0: ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer; michael@0: michael@0: ctx->preview_img.fmt = VPX_IMG_FMT_I420; michael@0: ctx->preview_img.x_chroma_shift = 1; michael@0: ctx->preview_img.y_chroma_shift = 1; michael@0: michael@0: ctx->preview_img.d_w = sd.y_width; michael@0: ctx->preview_img.d_h = sd.y_height; michael@0: ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride; michael@0: ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride; michael@0: ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride; michael@0: ctx->preview_img.w = sd.y_width; michael@0: ctx->preview_img.h = sd.y_height; michael@0: michael@0: return &ctx->preview_img; michael@0: } michael@0: else michael@0: return NULL; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8e_update_entropy(vpx_codec_alg_priv_t *ctx, michael@0: int ctr_id, michael@0: va_list args) michael@0: { michael@0: int update = va_arg(args, int); michael@0: vp8_update_entropy(ctx->cpi, update); michael@0: return VPX_CODEC_OK; michael@0: michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8e_update_reference(vpx_codec_alg_priv_t *ctx, michael@0: int ctr_id, michael@0: va_list args) michael@0: { michael@0: int update = va_arg(args, int); michael@0: vp8_update_reference(ctx->cpi, update); michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8e_use_reference(vpx_codec_alg_priv_t *ctx, michael@0: int ctr_id, michael@0: va_list args) michael@0: { michael@0: int reference_flag = va_arg(args, int); michael@0: vp8_use_as_reference(ctx->cpi, reference_flag); michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx, michael@0: int ctr_id, michael@0: va_list args) michael@0: { michael@0: vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *); michael@0: michael@0: if (data) michael@0: { michael@0: vpx_roi_map_t *roi = (vpx_roi_map_t *)data; michael@0: michael@0: if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols, roi->delta_q, roi->delta_lf, roi->static_threshold)) michael@0: return VPX_CODEC_OK; michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: michael@0: michael@0: static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx, michael@0: int ctr_id, michael@0: va_list args) michael@0: { michael@0: vpx_active_map_t *data = va_arg(args, vpx_active_map_t *); michael@0: michael@0: if (data) michael@0: { michael@0: michael@0: vpx_active_map_t *map = (vpx_active_map_t *)data; michael@0: michael@0: if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols)) michael@0: return VPX_CODEC_OK; michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx, michael@0: int ctr_id, michael@0: va_list args) michael@0: { michael@0: michael@0: vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *); michael@0: michael@0: if (data) michael@0: { michael@0: int res; michael@0: vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data ; michael@0: res = vp8_set_internal_size(ctx->cpi, michael@0: (VPX_SCALING)scalemode.h_scaling_mode, michael@0: (VPX_SCALING)scalemode.v_scaling_mode); michael@0: michael@0: if (!res) michael@0: { michael@0: /*force next frame a key frame to effect scaling mode */ michael@0: ctx->next_frame_flag |= FRAMEFLAGS_KEY; michael@0: return VPX_CODEC_OK; michael@0: } michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: michael@0: michael@0: static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] = michael@0: { michael@0: {VP8_SET_REFERENCE, vp8e_set_reference}, michael@0: {VP8_COPY_REFERENCE, vp8e_get_reference}, michael@0: {VP8_SET_POSTPROC, vp8e_set_previewpp}, michael@0: {VP8E_UPD_ENTROPY, vp8e_update_entropy}, michael@0: {VP8E_UPD_REFERENCE, vp8e_update_reference}, michael@0: {VP8E_USE_REFERENCE, vp8e_use_reference}, michael@0: {VP8E_SET_ROI_MAP, vp8e_set_roi_map}, michael@0: {VP8E_SET_ACTIVEMAP, vp8e_set_activemap}, michael@0: {VP8E_SET_SCALEMODE, vp8e_set_scalemode}, michael@0: {VP8E_SET_CPUUSED, set_param}, michael@0: {VP8E_SET_NOISE_SENSITIVITY, set_param}, michael@0: {VP8E_SET_ENABLEAUTOALTREF, set_param}, michael@0: {VP8E_SET_SHARPNESS, set_param}, michael@0: {VP8E_SET_STATIC_THRESHOLD, set_param}, michael@0: {VP8E_SET_TOKEN_PARTITIONS, set_param}, michael@0: {VP8E_GET_LAST_QUANTIZER, get_param}, michael@0: {VP8E_GET_LAST_QUANTIZER_64, get_param}, michael@0: {VP8E_SET_ARNR_MAXFRAMES, set_param}, michael@0: {VP8E_SET_ARNR_STRENGTH , set_param}, michael@0: {VP8E_SET_ARNR_TYPE , set_param}, michael@0: {VP8E_SET_TUNING, set_param}, michael@0: {VP8E_SET_CQ_LEVEL, set_param}, michael@0: {VP8E_SET_MAX_INTRA_BITRATE_PCT, set_param}, michael@0: { -1, NULL}, michael@0: }; michael@0: michael@0: static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] = michael@0: { michael@0: { michael@0: 0, michael@0: { michael@0: 0, /* g_usage */ michael@0: 0, /* g_threads */ michael@0: 0, /* g_profile */ michael@0: michael@0: 320, /* g_width */ michael@0: 240, /* g_height */ michael@0: {1, 30}, /* g_timebase */ michael@0: michael@0: 0, /* g_error_resilient */ michael@0: michael@0: VPX_RC_ONE_PASS, /* g_pass */ michael@0: michael@0: 0, /* g_lag_in_frames */ michael@0: michael@0: 0, /* rc_dropframe_thresh */ michael@0: 0, /* rc_resize_allowed */ michael@0: 60, /* rc_resize_down_thresold */ michael@0: 30, /* rc_resize_up_thresold */ michael@0: michael@0: VPX_VBR, /* rc_end_usage */ michael@0: #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION) michael@0: {0}, /* rc_twopass_stats_in */ michael@0: #endif michael@0: 256, /* rc_target_bandwidth */ michael@0: 4, /* rc_min_quantizer */ michael@0: 63, /* rc_max_quantizer */ michael@0: 100, /* rc_undershoot_pct */ michael@0: 100, /* rc_overshoot_pct */ michael@0: michael@0: 6000, /* rc_max_buffer_size */ michael@0: 4000, /* rc_buffer_initial_size; */ michael@0: 5000, /* rc_buffer_optimal_size; */ michael@0: michael@0: 50, /* rc_two_pass_vbrbias */ michael@0: 0, /* rc_two_pass_vbrmin_section */ michael@0: 400, /* rc_two_pass_vbrmax_section */ michael@0: michael@0: /* keyframing settings (kf) */ michael@0: VPX_KF_AUTO, /* g_kfmode*/ michael@0: 0, /* kf_min_dist */ michael@0: 128, /* kf_max_dist */ michael@0: michael@0: #if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION) michael@0: 1, /* g_delete_first_pass_file */ michael@0: "vp8.fpf" /* first pass filename */ michael@0: #endif michael@0: VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */ michael@0: 1, /* ts_number_layers */ michael@0: {0}, /* ts_target_bitrate */ michael@0: {0}, /* ts_rate_decimator */ michael@0: 0, /* ts_periodicity */ michael@0: {0}, /* ts_layer_id */ michael@0: }}, michael@0: { -1, {NOT_IMPLEMENTED}} michael@0: }; michael@0: michael@0: michael@0: #ifndef VERSION_STRING michael@0: #define VERSION_STRING michael@0: #endif michael@0: CODEC_INTERFACE(vpx_codec_vp8_cx) = michael@0: { michael@0: "WebM Project VP8 Encoder" VERSION_STRING, michael@0: VPX_CODEC_INTERNAL_ABI_VERSION, michael@0: VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR | michael@0: VPX_CODEC_CAP_OUTPUT_PARTITION, michael@0: /* vpx_codec_caps_t caps; */ michael@0: vp8e_init, /* vpx_codec_init_fn_t init; */ michael@0: vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */ michael@0: vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */ michael@0: NOT_IMPLEMENTED, /* vpx_codec_get_mmap_fn_t get_mmap; */ michael@0: NOT_IMPLEMENTED, /* vpx_codec_set_mmap_fn_t set_mmap; */ michael@0: { michael@0: NOT_IMPLEMENTED, /* vpx_codec_peek_si_fn_t peek_si; */ michael@0: NOT_IMPLEMENTED, /* vpx_codec_get_si_fn_t get_si; */ michael@0: NOT_IMPLEMENTED, /* vpx_codec_decode_fn_t decode; */ michael@0: NOT_IMPLEMENTED, /* vpx_codec_frame_get_fn_t frame_get; */ michael@0: }, michael@0: { michael@0: vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t peek_si; */ michael@0: vp8e_encode, /* vpx_codec_encode_fn_t encode; */ michael@0: vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t frame_get; */ michael@0: vp8e_set_config, michael@0: NOT_IMPLEMENTED, michael@0: vp8e_get_preview, michael@0: vp8e_mr_alloc_mem, michael@0: } /* encoder functions */ michael@0: };