media/libvpx/vp8/vp8_cx_iface.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license
michael@0 5 * that can be found in the LICENSE file in the root of the source
michael@0 6 * tree. An additional intellectual property rights grant can be found
michael@0 7 * in the file PATENTS. All contributing project authors may
michael@0 8 * be found in the AUTHORS file in the root of the source tree.
michael@0 9 */
michael@0 10
michael@0 11
michael@0 12 #include "vp8_rtcd.h"
michael@0 13 #include "vpx/vpx_codec.h"
michael@0 14 #include "vpx/internal/vpx_codec_internal.h"
michael@0 15 #include "vpx_version.h"
michael@0 16 #include "vp8/encoder/onyx_int.h"
michael@0 17 #include "vpx/vp8cx.h"
michael@0 18 #include "vp8/encoder/firstpass.h"
michael@0 19 #include "vp8/common/onyx.h"
michael@0 20 #include <stdlib.h>
michael@0 21 #include <string.h>
michael@0 22
michael@0 23 struct vp8_extracfg
michael@0 24 {
michael@0 25 struct vpx_codec_pkt_list *pkt_list;
michael@0 26 int cpu_used; /** available cpu percentage in 1/16*/
michael@0 27 unsigned int enable_auto_alt_ref; /** if encoder decides to uses alternate reference frame */
michael@0 28 unsigned int noise_sensitivity;
michael@0 29 unsigned int Sharpness;
michael@0 30 unsigned int static_thresh;
michael@0 31 unsigned int token_partitions;
michael@0 32 unsigned int arnr_max_frames; /* alt_ref Noise Reduction Max Frame Count */
michael@0 33 unsigned int arnr_strength; /* alt_ref Noise Reduction Strength */
michael@0 34 unsigned int arnr_type; /* alt_ref filter type */
michael@0 35 vp8e_tuning tuning;
michael@0 36 unsigned int cq_level; /* constrained quality level */
michael@0 37 unsigned int rc_max_intra_bitrate_pct;
michael@0 38
michael@0 39 };
michael@0 40
michael@0 41 struct extraconfig_map
michael@0 42 {
michael@0 43 int usage;
michael@0 44 struct vp8_extracfg cfg;
michael@0 45 };
michael@0 46
michael@0 47 static const struct extraconfig_map extracfg_map[] =
michael@0 48 {
michael@0 49 {
michael@0 50 0,
michael@0 51 {
michael@0 52 NULL,
michael@0 53 #if !(CONFIG_REALTIME_ONLY)
michael@0 54 0, /* cpu_used */
michael@0 55 #else
michael@0 56 4, /* cpu_used */
michael@0 57 #endif
michael@0 58 0, /* enable_auto_alt_ref */
michael@0 59 0, /* noise_sensitivity */
michael@0 60 0, /* Sharpness */
michael@0 61 0, /* static_thresh */
michael@0 62 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
michael@0 63 VP8_EIGHT_TOKENPARTITION,
michael@0 64 #else
michael@0 65 VP8_ONE_TOKENPARTITION, /* token_partitions */
michael@0 66 #endif
michael@0 67 0, /* arnr_max_frames */
michael@0 68 3, /* arnr_strength */
michael@0 69 3, /* arnr_type*/
michael@0 70 0, /* tuning*/
michael@0 71 10, /* cq_level */
michael@0 72 0, /* rc_max_intra_bitrate_pct */
michael@0 73 }
michael@0 74 }
michael@0 75 };
michael@0 76
michael@0 77 struct vpx_codec_alg_priv
michael@0 78 {
michael@0 79 vpx_codec_priv_t base;
michael@0 80 vpx_codec_enc_cfg_t cfg;
michael@0 81 struct vp8_extracfg vp8_cfg;
michael@0 82 VP8_CONFIG oxcf;
michael@0 83 struct VP8_COMP *cpi;
michael@0 84 unsigned char *cx_data;
michael@0 85 unsigned int cx_data_sz;
michael@0 86 vpx_image_t preview_img;
michael@0 87 unsigned int next_frame_flag;
michael@0 88 vp8_postproc_cfg_t preview_ppcfg;
michael@0 89 /* pkt_list size depends on the maximum number of lagged frames allowed. */
michael@0 90 vpx_codec_pkt_list_decl(64) pkt_list;
michael@0 91 unsigned int fixed_kf_cntr;
michael@0 92 };
michael@0 93
michael@0 94
michael@0 95 static vpx_codec_err_t
michael@0 96 update_error_state(vpx_codec_alg_priv_t *ctx,
michael@0 97 const struct vpx_internal_error_info *error)
michael@0 98 {
michael@0 99 vpx_codec_err_t res;
michael@0 100
michael@0 101 if ((res = error->error_code))
michael@0 102 ctx->base.err_detail = error->has_detail
michael@0 103 ? error->detail
michael@0 104 : NULL;
michael@0 105
michael@0 106 return res;
michael@0 107 }
michael@0 108
michael@0 109
michael@0 110 #undef ERROR
michael@0 111 #define ERROR(str) do {\
michael@0 112 ctx->base.err_detail = str;\
michael@0 113 return VPX_CODEC_INVALID_PARAM;\
michael@0 114 } while(0)
michael@0 115
michael@0 116 #define RANGE_CHECK(p,memb,lo,hi) do {\
michael@0 117 if(!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
michael@0 118 ERROR(#memb " out of range ["#lo".."#hi"]");\
michael@0 119 } while(0)
michael@0 120
michael@0 121 #define RANGE_CHECK_HI(p,memb,hi) do {\
michael@0 122 if(!((p)->memb <= (hi))) \
michael@0 123 ERROR(#memb " out of range [.."#hi"]");\
michael@0 124 } while(0)
michael@0 125
michael@0 126 #define RANGE_CHECK_LO(p,memb,lo) do {\
michael@0 127 if(!((p)->memb >= (lo))) \
michael@0 128 ERROR(#memb " out of range ["#lo"..]");\
michael@0 129 } while(0)
michael@0 130
michael@0 131 #define RANGE_CHECK_BOOL(p,memb) do {\
michael@0 132 if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
michael@0 133 } while(0)
michael@0 134
michael@0 135 static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
michael@0 136 const vpx_codec_enc_cfg_t *cfg,
michael@0 137 const struct vp8_extracfg *vp8_cfg,
michael@0 138 int finalize)
michael@0 139 {
michael@0 140 RANGE_CHECK(cfg, g_w, 1, 16383); /* 14 bits available */
michael@0 141 RANGE_CHECK(cfg, g_h, 1, 16383); /* 14 bits available */
michael@0 142 RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
michael@0 143 RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den);
michael@0 144 RANGE_CHECK_HI(cfg, g_profile, 3);
michael@0 145 RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
michael@0 146 RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
michael@0 147 RANGE_CHECK_HI(cfg, g_threads, 64);
michael@0 148 #if CONFIG_REALTIME_ONLY
michael@0 149 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
michael@0 150 #elif CONFIG_MULTI_RES_ENCODING
michael@0 151 if (ctx->base.enc.total_encoders > 1)
michael@0 152 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
michael@0 153 #else
michael@0 154 RANGE_CHECK_HI(cfg, g_lag_in_frames, 25);
michael@0 155 #endif
michael@0 156 RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
michael@0 157 RANGE_CHECK_HI(cfg, rc_undershoot_pct, 1000);
michael@0 158 RANGE_CHECK_HI(cfg, rc_overshoot_pct, 1000);
michael@0 159 RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
michael@0 160 RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
michael@0 161
michael@0 162 /* TODO: add spatial re-sampling support and frame dropping in
michael@0 163 * multi-res-encoder.*/
michael@0 164 #if CONFIG_MULTI_RES_ENCODING
michael@0 165 if (ctx->base.enc.total_encoders > 1)
michael@0 166 RANGE_CHECK_HI(cfg, rc_resize_allowed, 0);
michael@0 167 #else
michael@0 168 RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
michael@0 169 #endif
michael@0 170 RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
michael@0 171 RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
michael@0 172 RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
michael@0 173
michael@0 174 #if CONFIG_REALTIME_ONLY
michael@0 175 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
michael@0 176 #elif CONFIG_MULTI_RES_ENCODING
michael@0 177 if (ctx->base.enc.total_encoders > 1)
michael@0 178 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
michael@0 179 #else
michael@0 180 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
michael@0 181 #endif
michael@0 182
michael@0 183 /* VP8 does not support a lower bound on the keyframe interval in
michael@0 184 * automatic keyframe placement mode.
michael@0 185 */
michael@0 186 if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist
michael@0 187 && cfg->kf_min_dist > 0)
michael@0 188 ERROR("kf_min_dist not supported in auto mode, use 0 "
michael@0 189 "or kf_max_dist instead.");
michael@0 190
michael@0 191 RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref);
michael@0 192 RANGE_CHECK(vp8_cfg, cpu_used, -16, 16);
michael@0 193
michael@0 194 #if CONFIG_REALTIME_ONLY && !CONFIG_TEMPORAL_DENOISING
michael@0 195 RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0);
michael@0 196 #else
michael@0 197 RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6);
michael@0 198 #endif
michael@0 199
michael@0 200 RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION,
michael@0 201 VP8_EIGHT_TOKENPARTITION);
michael@0 202 RANGE_CHECK_HI(vp8_cfg, Sharpness, 7);
michael@0 203 RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
michael@0 204 RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6);
michael@0 205 RANGE_CHECK(vp8_cfg, arnr_type, 1, 3);
michael@0 206 RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
michael@0 207 if (finalize && (cfg->rc_end_usage == VPX_CQ || cfg->rc_end_usage == VPX_Q))
michael@0 208 RANGE_CHECK(vp8_cfg, cq_level,
michael@0 209 cfg->rc_min_quantizer, cfg->rc_max_quantizer);
michael@0 210
michael@0 211 #if !(CONFIG_REALTIME_ONLY)
michael@0 212 if (cfg->g_pass == VPX_RC_LAST_PASS)
michael@0 213 {
michael@0 214 size_t packet_sz = sizeof(FIRSTPASS_STATS);
michael@0 215 int n_packets = (int)(cfg->rc_twopass_stats_in.sz /
michael@0 216 packet_sz);
michael@0 217 FIRSTPASS_STATS *stats;
michael@0 218
michael@0 219 if (!cfg->rc_twopass_stats_in.buf)
michael@0 220 ERROR("rc_twopass_stats_in.buf not set.");
michael@0 221
michael@0 222 if (cfg->rc_twopass_stats_in.sz % packet_sz)
michael@0 223 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
michael@0 224
michael@0 225 if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
michael@0 226 ERROR("rc_twopass_stats_in requires at least two packets.");
michael@0 227
michael@0 228 stats = (void*)((char *)cfg->rc_twopass_stats_in.buf
michael@0 229 + (n_packets - 1) * packet_sz);
michael@0 230
michael@0 231 if ((int)(stats->count + 0.5) != n_packets - 1)
michael@0 232 ERROR("rc_twopass_stats_in missing EOS stats packet");
michael@0 233 }
michael@0 234 #endif
michael@0 235
michael@0 236 RANGE_CHECK(cfg, ts_number_layers, 1, 5);
michael@0 237
michael@0 238 if (cfg->ts_number_layers > 1)
michael@0 239 {
michael@0 240 unsigned int i;
michael@0 241 RANGE_CHECK_HI(cfg, ts_periodicity, 16);
michael@0 242
michael@0 243 for (i=1; i<cfg->ts_number_layers; i++)
michael@0 244 if (cfg->ts_target_bitrate[i] <= cfg->ts_target_bitrate[i-1])
michael@0 245 ERROR("ts_target_bitrate entries are not strictly increasing");
michael@0 246
michael@0 247 RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers-1], 1, 1);
michael@0 248 for (i=cfg->ts_number_layers-2; i>0; i--)
michael@0 249 if (cfg->ts_rate_decimator[i-1] != 2*cfg->ts_rate_decimator[i])
michael@0 250 ERROR("ts_rate_decimator factors are not powers of 2");
michael@0 251
michael@0 252 RANGE_CHECK_HI(cfg, ts_layer_id[i], cfg->ts_number_layers-1);
michael@0 253 }
michael@0 254
michael@0 255 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
michael@0 256 if(cfg->g_threads > (1 << vp8_cfg->token_partitions))
michael@0 257 ERROR("g_threads cannot be bigger than number of token partitions");
michael@0 258 #endif
michael@0 259
michael@0 260 return VPX_CODEC_OK;
michael@0 261 }
michael@0 262
michael@0 263
michael@0 264 static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
michael@0 265 const vpx_image_t *img)
michael@0 266 {
michael@0 267 switch (img->fmt)
michael@0 268 {
michael@0 269 case VPX_IMG_FMT_YV12:
michael@0 270 case VPX_IMG_FMT_I420:
michael@0 271 case VPX_IMG_FMT_VPXI420:
michael@0 272 case VPX_IMG_FMT_VPXYV12:
michael@0 273 break;
michael@0 274 default:
michael@0 275 ERROR("Invalid image format. Only YV12 and I420 images are supported");
michael@0 276 }
michael@0 277
michael@0 278 if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
michael@0 279 ERROR("Image size must match encoder init configuration size");
michael@0 280
michael@0 281 return VPX_CODEC_OK;
michael@0 282 }
michael@0 283
michael@0 284
michael@0 285 static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf,
michael@0 286 vpx_codec_enc_cfg_t cfg,
michael@0 287 struct vp8_extracfg vp8_cfg,
michael@0 288 vpx_codec_priv_enc_mr_cfg_t *mr_cfg)
michael@0 289 {
michael@0 290 oxcf->multi_threaded = cfg.g_threads;
michael@0 291 oxcf->Version = cfg.g_profile;
michael@0 292
michael@0 293 oxcf->Width = cfg.g_w;
michael@0 294 oxcf->Height = cfg.g_h;
michael@0 295 oxcf->timebase = cfg.g_timebase;
michael@0 296
michael@0 297 oxcf->error_resilient_mode = cfg.g_error_resilient;
michael@0 298
michael@0 299 switch (cfg.g_pass)
michael@0 300 {
michael@0 301 case VPX_RC_ONE_PASS:
michael@0 302 oxcf->Mode = MODE_BESTQUALITY;
michael@0 303 break;
michael@0 304 case VPX_RC_FIRST_PASS:
michael@0 305 oxcf->Mode = MODE_FIRSTPASS;
michael@0 306 break;
michael@0 307 case VPX_RC_LAST_PASS:
michael@0 308 oxcf->Mode = MODE_SECONDPASS_BEST;
michael@0 309 break;
michael@0 310 }
michael@0 311
michael@0 312 if (cfg.g_pass == VPX_RC_FIRST_PASS || cfg.g_pass == VPX_RC_ONE_PASS)
michael@0 313 {
michael@0 314 oxcf->allow_lag = 0;
michael@0 315 oxcf->lag_in_frames = 0;
michael@0 316 }
michael@0 317 else
michael@0 318 {
michael@0 319 oxcf->allow_lag = (cfg.g_lag_in_frames) > 0;
michael@0 320 oxcf->lag_in_frames = cfg.g_lag_in_frames;
michael@0 321 }
michael@0 322
michael@0 323 oxcf->allow_df = (cfg.rc_dropframe_thresh > 0);
michael@0 324 oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh;
michael@0 325
michael@0 326 oxcf->allow_spatial_resampling = cfg.rc_resize_allowed;
michael@0 327 oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh;
michael@0 328 oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh;
michael@0 329
michael@0 330 if (cfg.rc_end_usage == VPX_VBR) {
michael@0 331 oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
michael@0 332 } else if (cfg.rc_end_usage == VPX_CBR) {
michael@0 333 oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
michael@0 334 } else if (cfg.rc_end_usage == VPX_CQ) {
michael@0 335 oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
michael@0 336 } else if (cfg.rc_end_usage == VPX_Q) {
michael@0 337 oxcf->end_usage = USAGE_CONSTANT_QUALITY;
michael@0 338 }
michael@0 339
michael@0 340 oxcf->target_bandwidth = cfg.rc_target_bitrate;
michael@0 341 oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct;
michael@0 342
michael@0 343 oxcf->best_allowed_q = cfg.rc_min_quantizer;
michael@0 344 oxcf->worst_allowed_q = cfg.rc_max_quantizer;
michael@0 345 oxcf->cq_level = vp8_cfg.cq_level;
michael@0 346 oxcf->fixed_q = -1;
michael@0 347
michael@0 348 oxcf->under_shoot_pct = cfg.rc_undershoot_pct;
michael@0 349 oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
michael@0 350
michael@0 351 oxcf->maximum_buffer_size_in_ms = cfg.rc_buf_sz;
michael@0 352 oxcf->starting_buffer_level_in_ms = cfg.rc_buf_initial_sz;
michael@0 353 oxcf->optimal_buffer_level_in_ms = cfg.rc_buf_optimal_sz;
michael@0 354
michael@0 355 oxcf->maximum_buffer_size = cfg.rc_buf_sz;
michael@0 356 oxcf->starting_buffer_level = cfg.rc_buf_initial_sz;
michael@0 357 oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz;
michael@0 358
michael@0 359 oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct;
michael@0 360 oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct;
michael@0 361 oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct;
michael@0 362
michael@0 363 oxcf->auto_key = cfg.kf_mode == VPX_KF_AUTO
michael@0 364 && cfg.kf_min_dist != cfg.kf_max_dist;
michael@0 365 oxcf->key_freq = cfg.kf_max_dist;
michael@0 366
michael@0 367 oxcf->number_of_layers = cfg.ts_number_layers;
michael@0 368 oxcf->periodicity = cfg.ts_periodicity;
michael@0 369
michael@0 370 if (oxcf->number_of_layers > 1)
michael@0 371 {
michael@0 372 memcpy (oxcf->target_bitrate, cfg.ts_target_bitrate,
michael@0 373 sizeof(cfg.ts_target_bitrate));
michael@0 374 memcpy (oxcf->rate_decimator, cfg.ts_rate_decimator,
michael@0 375 sizeof(cfg.ts_rate_decimator));
michael@0 376 memcpy (oxcf->layer_id, cfg.ts_layer_id, sizeof(cfg.ts_layer_id));
michael@0 377 }
michael@0 378
michael@0 379 #if CONFIG_MULTI_RES_ENCODING
michael@0 380 /* When mr_cfg is NULL, oxcf->mr_total_resolutions and oxcf->mr_encoder_id
michael@0 381 * are both memset to 0, which ensures the correct logic under this
michael@0 382 * situation.
michael@0 383 */
michael@0 384 if(mr_cfg)
michael@0 385 {
michael@0 386 oxcf->mr_total_resolutions = mr_cfg->mr_total_resolutions;
michael@0 387 oxcf->mr_encoder_id = mr_cfg->mr_encoder_id;
michael@0 388 oxcf->mr_down_sampling_factor.num = mr_cfg->mr_down_sampling_factor.num;
michael@0 389 oxcf->mr_down_sampling_factor.den = mr_cfg->mr_down_sampling_factor.den;
michael@0 390 oxcf->mr_low_res_mode_info = mr_cfg->mr_low_res_mode_info;
michael@0 391 }
michael@0 392 #endif
michael@0 393
michael@0 394 oxcf->cpu_used = vp8_cfg.cpu_used;
michael@0 395 oxcf->encode_breakout = vp8_cfg.static_thresh;
michael@0 396 oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref;
michael@0 397 oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity;
michael@0 398 oxcf->Sharpness = vp8_cfg.Sharpness;
michael@0 399 oxcf->token_partitions = vp8_cfg.token_partitions;
michael@0 400
michael@0 401 oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in;
michael@0 402 oxcf->output_pkt_list = vp8_cfg.pkt_list;
michael@0 403
michael@0 404 oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
michael@0 405 oxcf->arnr_strength = vp8_cfg.arnr_strength;
michael@0 406 oxcf->arnr_type = vp8_cfg.arnr_type;
michael@0 407
michael@0 408 oxcf->tuning = vp8_cfg.tuning;
michael@0 409
michael@0 410 /*
michael@0 411 printf("Current VP8 Settings: \n");
michael@0 412 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
michael@0 413 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
michael@0 414 printf("Sharpness: %d\n", oxcf->Sharpness);
michael@0 415 printf("cpu_used: %d\n", oxcf->cpu_used);
michael@0 416 printf("Mode: %d\n", oxcf->Mode);
michael@0 417 printf("delete_first_pass_file: %d\n", oxcf->delete_first_pass_file);
michael@0 418 printf("auto_key: %d\n", oxcf->auto_key);
michael@0 419 printf("key_freq: %d\n", oxcf->key_freq);
michael@0 420 printf("end_usage: %d\n", oxcf->end_usage);
michael@0 421 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
michael@0 422 printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
michael@0 423 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
michael@0 424 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
michael@0 425 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
michael@0 426 printf("fixed_q: %d\n", oxcf->fixed_q);
michael@0 427 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
michael@0 428 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
michael@0 429 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
michael@0 430 printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
michael@0 431 printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
michael@0 432 printf("allow_df: %d\n", oxcf->allow_df);
michael@0 433 printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
michael@0 434 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
michael@0 435 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
michael@0 436 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
michael@0 437 printf("allow_lag: %d\n", oxcf->allow_lag);
michael@0 438 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
michael@0 439 printf("play_alternate: %d\n", oxcf->play_alternate);
michael@0 440 printf("Version: %d\n", oxcf->Version);
michael@0 441 printf("multi_threaded: %d\n", oxcf->multi_threaded);
michael@0 442 printf("encode_breakout: %d\n", oxcf->encode_breakout);
michael@0 443 */
michael@0 444 return VPX_CODEC_OK;
michael@0 445 }
michael@0 446
michael@0 447 static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx,
michael@0 448 const vpx_codec_enc_cfg_t *cfg)
michael@0 449 {
michael@0 450 vpx_codec_err_t res;
michael@0 451
michael@0 452 if (((cfg->g_w != ctx->cfg.g_w) || (cfg->g_h != ctx->cfg.g_h))
michael@0 453 && (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS))
michael@0 454 ERROR("Cannot change width or height after initialization");
michael@0 455
michael@0 456 /* Prevent increasing lag_in_frames. This check is stricter than it needs
michael@0 457 * to be -- the limit is not increasing past the first lag_in_frames
michael@0 458 * value, but we don't track the initial config, only the last successful
michael@0 459 * config.
michael@0 460 */
michael@0 461 if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
michael@0 462 ERROR("Cannot increase lag_in_frames");
michael@0 463
michael@0 464 res = validate_config(ctx, cfg, &ctx->vp8_cfg, 0);
michael@0 465
michael@0 466 if (!res)
michael@0 467 {
michael@0 468 ctx->cfg = *cfg;
michael@0 469 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
michael@0 470 vp8_change_config(ctx->cpi, &ctx->oxcf);
michael@0 471 }
michael@0 472
michael@0 473 return res;
michael@0 474 }
michael@0 475
michael@0 476
michael@0 477 int vp8_reverse_trans(int);
michael@0 478
michael@0 479
michael@0 480 static vpx_codec_err_t get_param(vpx_codec_alg_priv_t *ctx,
michael@0 481 int ctrl_id,
michael@0 482 va_list args)
michael@0 483 {
michael@0 484 void *arg = va_arg(args, void *);
michael@0 485
michael@0 486 #define MAP(id, var) case id: *(RECAST(id, arg)) = var; break
michael@0 487
michael@0 488 if (!arg)
michael@0 489 return VPX_CODEC_INVALID_PARAM;
michael@0 490
michael@0 491 switch (ctrl_id)
michael@0 492 {
michael@0 493 MAP(VP8E_GET_LAST_QUANTIZER, vp8_get_quantizer(ctx->cpi));
michael@0 494 MAP(VP8E_GET_LAST_QUANTIZER_64, vp8_reverse_trans(vp8_get_quantizer(ctx->cpi)));
michael@0 495 }
michael@0 496
michael@0 497 return VPX_CODEC_OK;
michael@0 498 #undef MAP
michael@0 499 }
michael@0 500
michael@0 501
michael@0 502 static vpx_codec_err_t set_param(vpx_codec_alg_priv_t *ctx,
michael@0 503 int ctrl_id,
michael@0 504 va_list args)
michael@0 505 {
michael@0 506 vpx_codec_err_t res = VPX_CODEC_OK;
michael@0 507 struct vp8_extracfg xcfg = ctx->vp8_cfg;
michael@0 508
michael@0 509 #define MAP(id, var) case id: var = CAST(id, args); break;
michael@0 510
michael@0 511 switch (ctrl_id)
michael@0 512 {
michael@0 513 MAP(VP8E_SET_CPUUSED, xcfg.cpu_used);
michael@0 514 MAP(VP8E_SET_ENABLEAUTOALTREF, xcfg.enable_auto_alt_ref);
michael@0 515 MAP(VP8E_SET_NOISE_SENSITIVITY, xcfg.noise_sensitivity);
michael@0 516 MAP(VP8E_SET_SHARPNESS, xcfg.Sharpness);
michael@0 517 MAP(VP8E_SET_STATIC_THRESHOLD, xcfg.static_thresh);
michael@0 518 MAP(VP8E_SET_TOKEN_PARTITIONS, xcfg.token_partitions);
michael@0 519
michael@0 520 MAP(VP8E_SET_ARNR_MAXFRAMES, xcfg.arnr_max_frames);
michael@0 521 MAP(VP8E_SET_ARNR_STRENGTH , xcfg.arnr_strength);
michael@0 522 MAP(VP8E_SET_ARNR_TYPE , xcfg.arnr_type);
michael@0 523 MAP(VP8E_SET_TUNING, xcfg.tuning);
michael@0 524 MAP(VP8E_SET_CQ_LEVEL, xcfg.cq_level);
michael@0 525 MAP(VP8E_SET_MAX_INTRA_BITRATE_PCT, xcfg.rc_max_intra_bitrate_pct);
michael@0 526
michael@0 527 }
michael@0 528
michael@0 529 res = validate_config(ctx, &ctx->cfg, &xcfg, 0);
michael@0 530
michael@0 531 if (!res)
michael@0 532 {
michael@0 533 ctx->vp8_cfg = xcfg;
michael@0 534 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
michael@0 535 vp8_change_config(ctx->cpi, &ctx->oxcf);
michael@0 536 }
michael@0 537
michael@0 538 return res;
michael@0 539 #undef MAP
michael@0 540 }
michael@0 541
michael@0 542 static vpx_codec_err_t vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t *cfg,
michael@0 543 void **mem_loc)
michael@0 544 {
michael@0 545 vpx_codec_err_t res = 0;
michael@0 546
michael@0 547 #if CONFIG_MULTI_RES_ENCODING
michael@0 548 LOWER_RES_FRAME_INFO *shared_mem_loc;
michael@0 549 int mb_rows = ((cfg->g_w + 15) >>4);
michael@0 550 int mb_cols = ((cfg->g_h + 15) >>4);
michael@0 551
michael@0 552 shared_mem_loc = calloc(1, sizeof(LOWER_RES_FRAME_INFO));
michael@0 553 if(!shared_mem_loc)
michael@0 554 {
michael@0 555 res = VPX_CODEC_MEM_ERROR;
michael@0 556 }
michael@0 557
michael@0 558 shared_mem_loc->mb_info = calloc(mb_rows*mb_cols, sizeof(LOWER_RES_MB_INFO));
michael@0 559 if(!(shared_mem_loc->mb_info))
michael@0 560 {
michael@0 561 res = VPX_CODEC_MEM_ERROR;
michael@0 562 }
michael@0 563 else
michael@0 564 {
michael@0 565 *mem_loc = (void *)shared_mem_loc;
michael@0 566 res = VPX_CODEC_OK;
michael@0 567 }
michael@0 568 #endif
michael@0 569 return res;
michael@0 570 }
michael@0 571
michael@0 572 static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx,
michael@0 573 vpx_codec_priv_enc_mr_cfg_t *mr_cfg)
michael@0 574 {
michael@0 575 vpx_codec_err_t res = VPX_CODEC_OK;
michael@0 576 struct vpx_codec_alg_priv *priv;
michael@0 577 vpx_codec_enc_cfg_t *cfg;
michael@0 578 unsigned int i;
michael@0 579
michael@0 580 struct VP8_COMP *optr;
michael@0 581
michael@0 582 vp8_rtcd();
michael@0 583
michael@0 584 if (!ctx->priv)
michael@0 585 {
michael@0 586 priv = calloc(1, sizeof(struct vpx_codec_alg_priv));
michael@0 587
michael@0 588 if (!priv)
michael@0 589 {
michael@0 590 return VPX_CODEC_MEM_ERROR;
michael@0 591 }
michael@0 592
michael@0 593 ctx->priv = &priv->base;
michael@0 594 ctx->priv->sz = sizeof(*ctx->priv);
michael@0 595 ctx->priv->iface = ctx->iface;
michael@0 596 ctx->priv->alg_priv = priv;
michael@0 597 ctx->priv->init_flags = ctx->init_flags;
michael@0 598
michael@0 599 if (ctx->config.enc)
michael@0 600 {
michael@0 601 /* Update the reference to the config structure to an
michael@0 602 * internal copy.
michael@0 603 */
michael@0 604 ctx->priv->alg_priv->cfg = *ctx->config.enc;
michael@0 605 ctx->config.enc = &ctx->priv->alg_priv->cfg;
michael@0 606 }
michael@0 607
michael@0 608 cfg = &ctx->priv->alg_priv->cfg;
michael@0 609
michael@0 610 /* Select the extra vp8 configuration table based on the current
michael@0 611 * usage value. If the current usage value isn't found, use the
michael@0 612 * values for usage case 0.
michael@0 613 */
michael@0 614 for (i = 0;
michael@0 615 extracfg_map[i].usage && extracfg_map[i].usage != cfg->g_usage;
michael@0 616 i++);
michael@0 617
michael@0 618 priv->vp8_cfg = extracfg_map[i].cfg;
michael@0 619 priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
michael@0 620
michael@0 621 priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
michael@0 622
michael@0 623 if (priv->cx_data_sz < 32768) priv->cx_data_sz = 32768;
michael@0 624
michael@0 625 priv->cx_data = malloc(priv->cx_data_sz);
michael@0 626
michael@0 627 if (!priv->cx_data)
michael@0 628 {
michael@0 629 return VPX_CODEC_MEM_ERROR;
michael@0 630 }
michael@0 631
michael@0 632 if(mr_cfg)
michael@0 633 ctx->priv->enc.total_encoders = mr_cfg->mr_total_resolutions;
michael@0 634 else
michael@0 635 ctx->priv->enc.total_encoders = 1;
michael@0 636
michael@0 637 res = validate_config(priv, &priv->cfg, &priv->vp8_cfg, 0);
michael@0 638
michael@0 639 if (!res)
michael@0 640 {
michael@0 641 set_vp8e_config(&ctx->priv->alg_priv->oxcf,
michael@0 642 ctx->priv->alg_priv->cfg,
michael@0 643 ctx->priv->alg_priv->vp8_cfg,
michael@0 644 mr_cfg);
michael@0 645
michael@0 646 optr = vp8_create_compressor(&ctx->priv->alg_priv->oxcf);
michael@0 647
michael@0 648 if (!optr)
michael@0 649 res = VPX_CODEC_MEM_ERROR;
michael@0 650 else
michael@0 651 ctx->priv->alg_priv->cpi = optr;
michael@0 652 }
michael@0 653 }
michael@0 654
michael@0 655 return res;
michael@0 656 }
michael@0 657
michael@0 658 static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx)
michael@0 659 {
michael@0 660 #if CONFIG_MULTI_RES_ENCODING
michael@0 661 /* Free multi-encoder shared memory */
michael@0 662 if (ctx->oxcf.mr_total_resolutions > 0 && (ctx->oxcf.mr_encoder_id == ctx->oxcf.mr_total_resolutions-1))
michael@0 663 {
michael@0 664 LOWER_RES_FRAME_INFO *shared_mem_loc = (LOWER_RES_FRAME_INFO *)ctx->oxcf.mr_low_res_mode_info;
michael@0 665 free(shared_mem_loc->mb_info);
michael@0 666 free(ctx->oxcf.mr_low_res_mode_info);
michael@0 667 }
michael@0 668 #endif
michael@0 669
michael@0 670 free(ctx->cx_data);
michael@0 671 vp8_remove_compressor(&ctx->cpi);
michael@0 672 free(ctx);
michael@0 673 return VPX_CODEC_OK;
michael@0 674 }
michael@0 675
michael@0 676 static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
michael@0 677 YV12_BUFFER_CONFIG *yv12)
michael@0 678 {
michael@0 679 vpx_codec_err_t res = VPX_CODEC_OK;
michael@0 680 yv12->y_buffer = img->planes[VPX_PLANE_Y];
michael@0 681 yv12->u_buffer = img->planes[VPX_PLANE_U];
michael@0 682 yv12->v_buffer = img->planes[VPX_PLANE_V];
michael@0 683
michael@0 684 yv12->y_crop_width = img->d_w;
michael@0 685 yv12->y_crop_height = img->d_h;
michael@0 686 yv12->y_width = img->d_w;
michael@0 687 yv12->y_height = img->d_h;
michael@0 688 yv12->uv_width = (1 + yv12->y_width) / 2;
michael@0 689 yv12->uv_height = (1 + yv12->y_height) / 2;
michael@0 690
michael@0 691 yv12->y_stride = img->stride[VPX_PLANE_Y];
michael@0 692 yv12->uv_stride = img->stride[VPX_PLANE_U];
michael@0 693
michael@0 694 yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2;
michael@0 695 return res;
michael@0 696 }
michael@0 697
michael@0 698 static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
michael@0 699 unsigned long duration,
michael@0 700 unsigned long deadline)
michael@0 701 {
michael@0 702 unsigned int new_qc;
michael@0 703
michael@0 704 #if !(CONFIG_REALTIME_ONLY)
michael@0 705 /* Use best quality mode if no deadline is given. */
michael@0 706 new_qc = MODE_BESTQUALITY;
michael@0 707
michael@0 708 if (deadline)
michael@0 709 {
michael@0 710 uint64_t duration_us;
michael@0 711
michael@0 712 /* Convert duration parameter from stream timebase to microseconds */
michael@0 713 duration_us = (uint64_t)duration * 1000000
michael@0 714 * (uint64_t)ctx->cfg.g_timebase.num
michael@0 715 / (uint64_t)ctx->cfg.g_timebase.den;
michael@0 716
michael@0 717 /* If the deadline is more that the duration this frame is to be shown,
michael@0 718 * use good quality mode. Otherwise use realtime mode.
michael@0 719 */
michael@0 720 new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
michael@0 721 }
michael@0 722
michael@0 723 #else
michael@0 724 new_qc = MODE_REALTIME;
michael@0 725 #endif
michael@0 726
michael@0 727 if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS)
michael@0 728 new_qc = MODE_FIRSTPASS;
michael@0 729 else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS)
michael@0 730 new_qc = (new_qc == MODE_BESTQUALITY)
michael@0 731 ? MODE_SECONDPASS_BEST
michael@0 732 : MODE_SECONDPASS;
michael@0 733
michael@0 734 if (ctx->oxcf.Mode != new_qc)
michael@0 735 {
michael@0 736 ctx->oxcf.Mode = new_qc;
michael@0 737 vp8_change_config(ctx->cpi, &ctx->oxcf);
michael@0 738 }
michael@0 739 }
michael@0 740
michael@0 741
michael@0 742 static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx,
michael@0 743 const vpx_image_t *img,
michael@0 744 vpx_codec_pts_t pts,
michael@0 745 unsigned long duration,
michael@0 746 vpx_enc_frame_flags_t flags,
michael@0 747 unsigned long deadline)
michael@0 748 {
michael@0 749 vpx_codec_err_t res = VPX_CODEC_OK;
michael@0 750
michael@0 751 if (!ctx->cfg.rc_target_bitrate)
michael@0 752 return res;
michael@0 753
michael@0 754 if (!ctx->cfg.rc_target_bitrate)
michael@0 755 return res;
michael@0 756
michael@0 757 if (img)
michael@0 758 res = validate_img(ctx, img);
michael@0 759
michael@0 760 if (!res)
michael@0 761 res = validate_config(ctx, &ctx->cfg, &ctx->vp8_cfg, 1);
michael@0 762
michael@0 763 pick_quickcompress_mode(ctx, duration, deadline);
michael@0 764 vpx_codec_pkt_list_init(&ctx->pkt_list);
michael@0 765
michael@0 766 /* Handle Flags */
michael@0 767 if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF))
michael@0 768 || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF)))
michael@0 769 {
michael@0 770 ctx->base.err_detail = "Conflicting flags.";
michael@0 771 return VPX_CODEC_INVALID_PARAM;
michael@0 772 }
michael@0 773
michael@0 774 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF
michael@0 775 | VP8_EFLAG_NO_REF_ARF))
michael@0 776 {
michael@0 777 int ref = 7;
michael@0 778
michael@0 779 if (flags & VP8_EFLAG_NO_REF_LAST)
michael@0 780 ref ^= VP8_LAST_FRAME;
michael@0 781
michael@0 782 if (flags & VP8_EFLAG_NO_REF_GF)
michael@0 783 ref ^= VP8_GOLD_FRAME;
michael@0 784
michael@0 785 if (flags & VP8_EFLAG_NO_REF_ARF)
michael@0 786 ref ^= VP8_ALTR_FRAME;
michael@0 787
michael@0 788 vp8_use_as_reference(ctx->cpi, ref);
michael@0 789 }
michael@0 790
michael@0 791 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF
michael@0 792 | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF
michael@0 793 | VP8_EFLAG_FORCE_ARF))
michael@0 794 {
michael@0 795 int upd = 7;
michael@0 796
michael@0 797 if (flags & VP8_EFLAG_NO_UPD_LAST)
michael@0 798 upd ^= VP8_LAST_FRAME;
michael@0 799
michael@0 800 if (flags & VP8_EFLAG_NO_UPD_GF)
michael@0 801 upd ^= VP8_GOLD_FRAME;
michael@0 802
michael@0 803 if (flags & VP8_EFLAG_NO_UPD_ARF)
michael@0 804 upd ^= VP8_ALTR_FRAME;
michael@0 805
michael@0 806 vp8_update_reference(ctx->cpi, upd);
michael@0 807 }
michael@0 808
michael@0 809 if (flags & VP8_EFLAG_NO_UPD_ENTROPY)
michael@0 810 {
michael@0 811 vp8_update_entropy(ctx->cpi, 0);
michael@0 812 }
michael@0 813
michael@0 814 /* Handle fixed keyframe intervals */
michael@0 815 if (ctx->cfg.kf_mode == VPX_KF_AUTO
michael@0 816 && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist)
michael@0 817 {
michael@0 818 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist)
michael@0 819 {
michael@0 820 flags |= VPX_EFLAG_FORCE_KF;
michael@0 821 ctx->fixed_kf_cntr = 1;
michael@0 822 }
michael@0 823 }
michael@0 824
michael@0 825 /* Initialize the encoder instance on the first frame*/
michael@0 826 if (!res && ctx->cpi)
michael@0 827 {
michael@0 828 unsigned int lib_flags;
michael@0 829 YV12_BUFFER_CONFIG sd;
michael@0 830 int64_t dst_time_stamp, dst_end_time_stamp;
michael@0 831 unsigned long size, cx_data_sz;
michael@0 832 unsigned char *cx_data;
michael@0 833 unsigned char *cx_data_end;
michael@0 834 int comp_data_state = 0;
michael@0 835
michael@0 836 /* Set up internal flags */
michael@0 837 if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
michael@0 838 ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1;
michael@0 839
michael@0 840 if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION)
michael@0 841 ((VP8_COMP *)ctx->cpi)->output_partition = 1;
michael@0 842
michael@0 843 /* Convert API flags to internal codec lib flags */
michael@0 844 lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
michael@0 845
michael@0 846 /* vp8 use 10,000,000 ticks/second as time stamp */
michael@0 847 dst_time_stamp = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
michael@0 848 dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
michael@0 849
michael@0 850 if (img != NULL)
michael@0 851 {
michael@0 852 res = image2yuvconfig(img, &sd);
michael@0 853
michael@0 854 if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
michael@0 855 &sd, dst_time_stamp, dst_end_time_stamp))
michael@0 856 {
michael@0 857 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
michael@0 858 res = update_error_state(ctx, &cpi->common.error);
michael@0 859 }
michael@0 860
michael@0 861 /* reset for next frame */
michael@0 862 ctx->next_frame_flag = 0;
michael@0 863 }
michael@0 864
michael@0 865 cx_data = ctx->cx_data;
michael@0 866 cx_data_sz = ctx->cx_data_sz;
michael@0 867 cx_data_end = ctx->cx_data + cx_data_sz;
michael@0 868 lib_flags = 0;
michael@0 869
michael@0 870 while (cx_data_sz >= ctx->cx_data_sz / 2)
michael@0 871 {
michael@0 872 comp_data_state = vp8_get_compressed_data(ctx->cpi,
michael@0 873 &lib_flags,
michael@0 874 &size,
michael@0 875 cx_data,
michael@0 876 cx_data_end,
michael@0 877 &dst_time_stamp,
michael@0 878 &dst_end_time_stamp,
michael@0 879 !img);
michael@0 880
michael@0 881 if(comp_data_state == VPX_CODEC_CORRUPT_FRAME)
michael@0 882 return VPX_CODEC_CORRUPT_FRAME;
michael@0 883 else if(comp_data_state == -1)
michael@0 884 break;
michael@0 885
michael@0 886 if (size)
michael@0 887 {
michael@0 888 vpx_codec_pts_t round, delta;
michael@0 889 vpx_codec_cx_pkt_t pkt;
michael@0 890 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
michael@0 891
michael@0 892 /* Add the frame packet to the list of returned packets. */
michael@0 893 round = (vpx_codec_pts_t)1000000
michael@0 894 * ctx->cfg.g_timebase.num / 2 - 1;
michael@0 895 delta = (dst_end_time_stamp - dst_time_stamp);
michael@0 896 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
michael@0 897 pkt.data.frame.pts =
michael@0 898 (dst_time_stamp * ctx->cfg.g_timebase.den + round)
michael@0 899 / ctx->cfg.g_timebase.num / 10000000;
michael@0 900 pkt.data.frame.duration = (unsigned long)
michael@0 901 ((delta * ctx->cfg.g_timebase.den + round)
michael@0 902 / ctx->cfg.g_timebase.num / 10000000);
michael@0 903 pkt.data.frame.flags = lib_flags << 16;
michael@0 904
michael@0 905 if (lib_flags & FRAMEFLAGS_KEY)
michael@0 906 pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
michael@0 907
michael@0 908 if (!cpi->common.show_frame)
michael@0 909 {
michael@0 910 pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
michael@0 911
michael@0 912 /* This timestamp should be as close as possible to the
michael@0 913 * prior PTS so that if a decoder uses pts to schedule when
michael@0 914 * to do this, we start right after last frame was decoded.
michael@0 915 * Invisible frames have no duration.
michael@0 916 */
michael@0 917 pkt.data.frame.pts = ((cpi->last_time_stamp_seen
michael@0 918 * ctx->cfg.g_timebase.den + round)
michael@0 919 / ctx->cfg.g_timebase.num / 10000000) + 1;
michael@0 920 pkt.data.frame.duration = 0;
michael@0 921 }
michael@0 922
michael@0 923 if (cpi->droppable)
michael@0 924 pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE;
michael@0 925
michael@0 926 if (cpi->output_partition)
michael@0 927 {
michael@0 928 int i;
michael@0 929 const int num_partitions =
michael@0 930 (1 << cpi->common.multi_token_partition) + 1;
michael@0 931
michael@0 932 pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT;
michael@0 933
michael@0 934 for (i = 0; i < num_partitions; ++i)
michael@0 935 {
michael@0 936 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
michael@0 937 pkt.data.frame.buf = cpi->partition_d[i];
michael@0 938 #else
michael@0 939 pkt.data.frame.buf = cx_data;
michael@0 940 cx_data += cpi->partition_sz[i];
michael@0 941 cx_data_sz -= cpi->partition_sz[i];
michael@0 942 #endif
michael@0 943 pkt.data.frame.sz = cpi->partition_sz[i];
michael@0 944 pkt.data.frame.partition_id = i;
michael@0 945 /* don't set the fragment bit for the last partition */
michael@0 946 if (i == (num_partitions - 1))
michael@0 947 pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT;
michael@0 948 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
michael@0 949 }
michael@0 950 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
michael@0 951 /* In lagged mode the encoder can buffer multiple frames.
michael@0 952 * We don't want this in partitioned output because
michael@0 953 * partitions are spread all over the output buffer.
michael@0 954 * So, force an exit!
michael@0 955 */
michael@0 956 cx_data_sz -= ctx->cx_data_sz / 2;
michael@0 957 #endif
michael@0 958 }
michael@0 959 else
michael@0 960 {
michael@0 961 pkt.data.frame.buf = cx_data;
michael@0 962 pkt.data.frame.sz = size;
michael@0 963 pkt.data.frame.partition_id = -1;
michael@0 964 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
michael@0 965 cx_data += size;
michael@0 966 cx_data_sz -= size;
michael@0 967 }
michael@0 968 }
michael@0 969 }
michael@0 970 }
michael@0 971
michael@0 972 return res;
michael@0 973 }
michael@0 974
michael@0 975
michael@0 976 static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx,
michael@0 977 vpx_codec_iter_t *iter)
michael@0 978 {
michael@0 979 return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
michael@0 980 }
michael@0 981
michael@0 982 static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx,
michael@0 983 int ctr_id,
michael@0 984 va_list args)
michael@0 985 {
michael@0 986 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
michael@0 987
michael@0 988 if (data)
michael@0 989 {
michael@0 990 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
michael@0 991 YV12_BUFFER_CONFIG sd;
michael@0 992
michael@0 993 image2yuvconfig(&frame->img, &sd);
michael@0 994 vp8_set_reference(ctx->cpi, frame->frame_type, &sd);
michael@0 995 return VPX_CODEC_OK;
michael@0 996 }
michael@0 997 else
michael@0 998 return VPX_CODEC_INVALID_PARAM;
michael@0 999
michael@0 1000 }
michael@0 1001
michael@0 1002 static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx,
michael@0 1003 int ctr_id,
michael@0 1004 va_list args)
michael@0 1005 {
michael@0 1006
michael@0 1007 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
michael@0 1008
michael@0 1009 if (data)
michael@0 1010 {
michael@0 1011 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
michael@0 1012 YV12_BUFFER_CONFIG sd;
michael@0 1013
michael@0 1014 image2yuvconfig(&frame->img, &sd);
michael@0 1015 vp8_get_reference(ctx->cpi, frame->frame_type, &sd);
michael@0 1016 return VPX_CODEC_OK;
michael@0 1017 }
michael@0 1018 else
michael@0 1019 return VPX_CODEC_INVALID_PARAM;
michael@0 1020 }
michael@0 1021
michael@0 1022 static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx,
michael@0 1023 int ctr_id,
michael@0 1024 va_list args)
michael@0 1025 {
michael@0 1026 #if CONFIG_POSTPROC
michael@0 1027 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
michael@0 1028 (void)ctr_id;
michael@0 1029
michael@0 1030 if (data)
michael@0 1031 {
michael@0 1032 ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
michael@0 1033 return VPX_CODEC_OK;
michael@0 1034 }
michael@0 1035 else
michael@0 1036 return VPX_CODEC_INVALID_PARAM;
michael@0 1037 #else
michael@0 1038 (void)ctx;
michael@0 1039 (void)ctr_id;
michael@0 1040 (void)args;
michael@0 1041 return VPX_CODEC_INCAPABLE;
michael@0 1042 #endif
michael@0 1043 }
michael@0 1044
michael@0 1045
michael@0 1046 static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx)
michael@0 1047 {
michael@0 1048
michael@0 1049 YV12_BUFFER_CONFIG sd;
michael@0 1050 vp8_ppflags_t flags = {0};
michael@0 1051
michael@0 1052 if (ctx->preview_ppcfg.post_proc_flag)
michael@0 1053 {
michael@0 1054 flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
michael@0 1055 flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
michael@0 1056 flags.noise_level = ctx->preview_ppcfg.noise_level;
michael@0 1057 }
michael@0 1058
michael@0 1059 if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags))
michael@0 1060 {
michael@0 1061
michael@0 1062 /*
michael@0 1063 vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
michael@0 1064 sd.y_width + 2*VP8BORDERINPIXELS,
michael@0 1065 sd.y_height + 2*VP8BORDERINPIXELS,
michael@0 1066 1,
michael@0 1067 sd.buffer_alloc);
michael@0 1068 vpx_img_set_rect(&ctx->preview_img,
michael@0 1069 VP8BORDERINPIXELS, VP8BORDERINPIXELS,
michael@0 1070 sd.y_width, sd.y_height);
michael@0 1071 */
michael@0 1072
michael@0 1073 ctx->preview_img.bps = 12;
michael@0 1074 ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer;
michael@0 1075 ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer;
michael@0 1076 ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer;
michael@0 1077
michael@0 1078 ctx->preview_img.fmt = VPX_IMG_FMT_I420;
michael@0 1079 ctx->preview_img.x_chroma_shift = 1;
michael@0 1080 ctx->preview_img.y_chroma_shift = 1;
michael@0 1081
michael@0 1082 ctx->preview_img.d_w = sd.y_width;
michael@0 1083 ctx->preview_img.d_h = sd.y_height;
michael@0 1084 ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride;
michael@0 1085 ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride;
michael@0 1086 ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride;
michael@0 1087 ctx->preview_img.w = sd.y_width;
michael@0 1088 ctx->preview_img.h = sd.y_height;
michael@0 1089
michael@0 1090 return &ctx->preview_img;
michael@0 1091 }
michael@0 1092 else
michael@0 1093 return NULL;
michael@0 1094 }
michael@0 1095
michael@0 1096 static vpx_codec_err_t vp8e_update_entropy(vpx_codec_alg_priv_t *ctx,
michael@0 1097 int ctr_id,
michael@0 1098 va_list args)
michael@0 1099 {
michael@0 1100 int update = va_arg(args, int);
michael@0 1101 vp8_update_entropy(ctx->cpi, update);
michael@0 1102 return VPX_CODEC_OK;
michael@0 1103
michael@0 1104 }
michael@0 1105
michael@0 1106 static vpx_codec_err_t vp8e_update_reference(vpx_codec_alg_priv_t *ctx,
michael@0 1107 int ctr_id,
michael@0 1108 va_list args)
michael@0 1109 {
michael@0 1110 int update = va_arg(args, int);
michael@0 1111 vp8_update_reference(ctx->cpi, update);
michael@0 1112 return VPX_CODEC_OK;
michael@0 1113 }
michael@0 1114
michael@0 1115 static vpx_codec_err_t vp8e_use_reference(vpx_codec_alg_priv_t *ctx,
michael@0 1116 int ctr_id,
michael@0 1117 va_list args)
michael@0 1118 {
michael@0 1119 int reference_flag = va_arg(args, int);
michael@0 1120 vp8_use_as_reference(ctx->cpi, reference_flag);
michael@0 1121 return VPX_CODEC_OK;
michael@0 1122 }
michael@0 1123
michael@0 1124 static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx,
michael@0 1125 int ctr_id,
michael@0 1126 va_list args)
michael@0 1127 {
michael@0 1128 vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
michael@0 1129
michael@0 1130 if (data)
michael@0 1131 {
michael@0 1132 vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
michael@0 1133
michael@0 1134 if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols, roi->delta_q, roi->delta_lf, roi->static_threshold))
michael@0 1135 return VPX_CODEC_OK;
michael@0 1136 else
michael@0 1137 return VPX_CODEC_INVALID_PARAM;
michael@0 1138 }
michael@0 1139 else
michael@0 1140 return VPX_CODEC_INVALID_PARAM;
michael@0 1141 }
michael@0 1142
michael@0 1143
michael@0 1144 static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx,
michael@0 1145 int ctr_id,
michael@0 1146 va_list args)
michael@0 1147 {
michael@0 1148 vpx_active_map_t *data = va_arg(args, vpx_active_map_t *);
michael@0 1149
michael@0 1150 if (data)
michael@0 1151 {
michael@0 1152
michael@0 1153 vpx_active_map_t *map = (vpx_active_map_t *)data;
michael@0 1154
michael@0 1155 if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols))
michael@0 1156 return VPX_CODEC_OK;
michael@0 1157 else
michael@0 1158 return VPX_CODEC_INVALID_PARAM;
michael@0 1159 }
michael@0 1160 else
michael@0 1161 return VPX_CODEC_INVALID_PARAM;
michael@0 1162 }
michael@0 1163
michael@0 1164 static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx,
michael@0 1165 int ctr_id,
michael@0 1166 va_list args)
michael@0 1167 {
michael@0 1168
michael@0 1169 vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *);
michael@0 1170
michael@0 1171 if (data)
michael@0 1172 {
michael@0 1173 int res;
michael@0 1174 vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data ;
michael@0 1175 res = vp8_set_internal_size(ctx->cpi,
michael@0 1176 (VPX_SCALING)scalemode.h_scaling_mode,
michael@0 1177 (VPX_SCALING)scalemode.v_scaling_mode);
michael@0 1178
michael@0 1179 if (!res)
michael@0 1180 {
michael@0 1181 /*force next frame a key frame to effect scaling mode */
michael@0 1182 ctx->next_frame_flag |= FRAMEFLAGS_KEY;
michael@0 1183 return VPX_CODEC_OK;
michael@0 1184 }
michael@0 1185 else
michael@0 1186 return VPX_CODEC_INVALID_PARAM;
michael@0 1187 }
michael@0 1188 else
michael@0 1189 return VPX_CODEC_INVALID_PARAM;
michael@0 1190 }
michael@0 1191
michael@0 1192
michael@0 1193 static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] =
michael@0 1194 {
michael@0 1195 {VP8_SET_REFERENCE, vp8e_set_reference},
michael@0 1196 {VP8_COPY_REFERENCE, vp8e_get_reference},
michael@0 1197 {VP8_SET_POSTPROC, vp8e_set_previewpp},
michael@0 1198 {VP8E_UPD_ENTROPY, vp8e_update_entropy},
michael@0 1199 {VP8E_UPD_REFERENCE, vp8e_update_reference},
michael@0 1200 {VP8E_USE_REFERENCE, vp8e_use_reference},
michael@0 1201 {VP8E_SET_ROI_MAP, vp8e_set_roi_map},
michael@0 1202 {VP8E_SET_ACTIVEMAP, vp8e_set_activemap},
michael@0 1203 {VP8E_SET_SCALEMODE, vp8e_set_scalemode},
michael@0 1204 {VP8E_SET_CPUUSED, set_param},
michael@0 1205 {VP8E_SET_NOISE_SENSITIVITY, set_param},
michael@0 1206 {VP8E_SET_ENABLEAUTOALTREF, set_param},
michael@0 1207 {VP8E_SET_SHARPNESS, set_param},
michael@0 1208 {VP8E_SET_STATIC_THRESHOLD, set_param},
michael@0 1209 {VP8E_SET_TOKEN_PARTITIONS, set_param},
michael@0 1210 {VP8E_GET_LAST_QUANTIZER, get_param},
michael@0 1211 {VP8E_GET_LAST_QUANTIZER_64, get_param},
michael@0 1212 {VP8E_SET_ARNR_MAXFRAMES, set_param},
michael@0 1213 {VP8E_SET_ARNR_STRENGTH , set_param},
michael@0 1214 {VP8E_SET_ARNR_TYPE , set_param},
michael@0 1215 {VP8E_SET_TUNING, set_param},
michael@0 1216 {VP8E_SET_CQ_LEVEL, set_param},
michael@0 1217 {VP8E_SET_MAX_INTRA_BITRATE_PCT, set_param},
michael@0 1218 { -1, NULL},
michael@0 1219 };
michael@0 1220
michael@0 1221 static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] =
michael@0 1222 {
michael@0 1223 {
michael@0 1224 0,
michael@0 1225 {
michael@0 1226 0, /* g_usage */
michael@0 1227 0, /* g_threads */
michael@0 1228 0, /* g_profile */
michael@0 1229
michael@0 1230 320, /* g_width */
michael@0 1231 240, /* g_height */
michael@0 1232 {1, 30}, /* g_timebase */
michael@0 1233
michael@0 1234 0, /* g_error_resilient */
michael@0 1235
michael@0 1236 VPX_RC_ONE_PASS, /* g_pass */
michael@0 1237
michael@0 1238 0, /* g_lag_in_frames */
michael@0 1239
michael@0 1240 0, /* rc_dropframe_thresh */
michael@0 1241 0, /* rc_resize_allowed */
michael@0 1242 60, /* rc_resize_down_thresold */
michael@0 1243 30, /* rc_resize_up_thresold */
michael@0 1244
michael@0 1245 VPX_VBR, /* rc_end_usage */
michael@0 1246 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
michael@0 1247 {0}, /* rc_twopass_stats_in */
michael@0 1248 #endif
michael@0 1249 256, /* rc_target_bandwidth */
michael@0 1250 4, /* rc_min_quantizer */
michael@0 1251 63, /* rc_max_quantizer */
michael@0 1252 100, /* rc_undershoot_pct */
michael@0 1253 100, /* rc_overshoot_pct */
michael@0 1254
michael@0 1255 6000, /* rc_max_buffer_size */
michael@0 1256 4000, /* rc_buffer_initial_size; */
michael@0 1257 5000, /* rc_buffer_optimal_size; */
michael@0 1258
michael@0 1259 50, /* rc_two_pass_vbrbias */
michael@0 1260 0, /* rc_two_pass_vbrmin_section */
michael@0 1261 400, /* rc_two_pass_vbrmax_section */
michael@0 1262
michael@0 1263 /* keyframing settings (kf) */
michael@0 1264 VPX_KF_AUTO, /* g_kfmode*/
michael@0 1265 0, /* kf_min_dist */
michael@0 1266 128, /* kf_max_dist */
michael@0 1267
michael@0 1268 #if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION)
michael@0 1269 1, /* g_delete_first_pass_file */
michael@0 1270 "vp8.fpf" /* first pass filename */
michael@0 1271 #endif
michael@0 1272 VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */
michael@0 1273 1, /* ts_number_layers */
michael@0 1274 {0}, /* ts_target_bitrate */
michael@0 1275 {0}, /* ts_rate_decimator */
michael@0 1276 0, /* ts_periodicity */
michael@0 1277 {0}, /* ts_layer_id */
michael@0 1278 }},
michael@0 1279 { -1, {NOT_IMPLEMENTED}}
michael@0 1280 };
michael@0 1281
michael@0 1282
michael@0 1283 #ifndef VERSION_STRING
michael@0 1284 #define VERSION_STRING
michael@0 1285 #endif
michael@0 1286 CODEC_INTERFACE(vpx_codec_vp8_cx) =
michael@0 1287 {
michael@0 1288 "WebM Project VP8 Encoder" VERSION_STRING,
michael@0 1289 VPX_CODEC_INTERNAL_ABI_VERSION,
michael@0 1290 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR |
michael@0 1291 VPX_CODEC_CAP_OUTPUT_PARTITION,
michael@0 1292 /* vpx_codec_caps_t caps; */
michael@0 1293 vp8e_init, /* vpx_codec_init_fn_t init; */
michael@0 1294 vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */
michael@0 1295 vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
michael@0 1296 NOT_IMPLEMENTED, /* vpx_codec_get_mmap_fn_t get_mmap; */
michael@0 1297 NOT_IMPLEMENTED, /* vpx_codec_set_mmap_fn_t set_mmap; */
michael@0 1298 {
michael@0 1299 NOT_IMPLEMENTED, /* vpx_codec_peek_si_fn_t peek_si; */
michael@0 1300 NOT_IMPLEMENTED, /* vpx_codec_get_si_fn_t get_si; */
michael@0 1301 NOT_IMPLEMENTED, /* vpx_codec_decode_fn_t decode; */
michael@0 1302 NOT_IMPLEMENTED, /* vpx_codec_frame_get_fn_t frame_get; */
michael@0 1303 },
michael@0 1304 {
michael@0 1305 vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t peek_si; */
michael@0 1306 vp8e_encode, /* vpx_codec_encode_fn_t encode; */
michael@0 1307 vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t frame_get; */
michael@0 1308 vp8e_set_config,
michael@0 1309 NOT_IMPLEMENTED,
michael@0 1310 vp8e_get_preview,
michael@0 1311 vp8e_mr_alloc_mem,
michael@0 1312 } /* encoder functions */
michael@0 1313 };

mercurial