media/libvpx/vp9/vp9_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 #include <stdlib.h>
michael@0 12 #include <string.h>
michael@0 13
michael@0 14 #include "vpx/vpx_codec.h"
michael@0 15 #include "vpx/internal/vpx_codec_internal.h"
michael@0 16 #include "./vpx_version.h"
michael@0 17 #include "vp9/encoder/vp9_onyx_int.h"
michael@0 18 #include "vpx/vp8cx.h"
michael@0 19 #include "vp9/encoder/vp9_firstpass.h"
michael@0 20 #include "vp9/common/vp9_onyx.h"
michael@0 21 #include "vp9/vp9_iface_common.h"
michael@0 22
michael@0 23 struct vp9_extracfg {
michael@0 24 struct vpx_codec_pkt_list *pkt_list;
michael@0 25 int cpu_used; /* available cpu percentage in 1/16 */
michael@0 26 unsigned int enable_auto_alt_ref;
michael@0 27 unsigned int noise_sensitivity;
michael@0 28 unsigned int Sharpness;
michael@0 29 unsigned int static_thresh;
michael@0 30 unsigned int tile_columns;
michael@0 31 unsigned int tile_rows;
michael@0 32 unsigned int arnr_max_frames;
michael@0 33 unsigned int arnr_strength;
michael@0 34 unsigned int arnr_type;
michael@0 35 unsigned int experimental;
michael@0 36 vp8e_tuning tuning;
michael@0 37 unsigned int cq_level; /* constrained quality level */
michael@0 38 unsigned int rc_max_intra_bitrate_pct;
michael@0 39 unsigned int lossless;
michael@0 40 unsigned int frame_parallel_decoding_mode;
michael@0 41 unsigned int aq_mode;
michael@0 42 };
michael@0 43
michael@0 44 struct extraconfig_map {
michael@0 45 int usage;
michael@0 46 struct vp9_extracfg cfg;
michael@0 47 };
michael@0 48
michael@0 49 static const struct extraconfig_map extracfg_map[] = {
michael@0 50 {
michael@0 51 0,
michael@0 52 { // NOLINT
michael@0 53 NULL,
michael@0 54 0, /* cpu_used */
michael@0 55 1, /* enable_auto_alt_ref */
michael@0 56 0, /* noise_sensitivity */
michael@0 57 0, /* Sharpness */
michael@0 58 0, /* static_thresh */
michael@0 59 0, /* tile_columns */
michael@0 60 0, /* tile_rows */
michael@0 61 7, /* arnr_max_frames */
michael@0 62 5, /* arnr_strength */
michael@0 63 3, /* arnr_type*/
michael@0 64 0, /* experimental mode */
michael@0 65 0, /* tuning*/
michael@0 66 10, /* cq_level */
michael@0 67 0, /* rc_max_intra_bitrate_pct */
michael@0 68 0, /* lossless */
michael@0 69 0, /* frame_parallel_decoding_mode */
michael@0 70 0, /* aq_mode */
michael@0 71 }
michael@0 72 }
michael@0 73 };
michael@0 74
michael@0 75 struct vpx_codec_alg_priv {
michael@0 76 vpx_codec_priv_t base;
michael@0 77 vpx_codec_enc_cfg_t cfg;
michael@0 78 struct vp9_extracfg vp8_cfg;
michael@0 79 VP9_CONFIG oxcf;
michael@0 80 VP9_PTR cpi;
michael@0 81 unsigned char *cx_data;
michael@0 82 unsigned int cx_data_sz;
michael@0 83 unsigned char *pending_cx_data;
michael@0 84 unsigned int pending_cx_data_sz;
michael@0 85 int pending_frame_count;
michael@0 86 uint32_t pending_frame_sizes[8];
michael@0 87 uint32_t pending_frame_magnitude;
michael@0 88 vpx_image_t preview_img;
michael@0 89 vp8_postproc_cfg_t preview_ppcfg;
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 static VP9_REFFRAME ref_frame_to_vp9_reframe(vpx_ref_frame_type_t frame) {
michael@0 95 switch (frame) {
michael@0 96 case VP8_LAST_FRAME:
michael@0 97 return VP9_LAST_FLAG;
michael@0 98 case VP8_GOLD_FRAME:
michael@0 99 return VP9_GOLD_FLAG;
michael@0 100 case VP8_ALTR_FRAME:
michael@0 101 return VP9_ALT_FLAG;
michael@0 102 }
michael@0 103 assert(!"Invalid Reference Frame");
michael@0 104 return VP9_LAST_FLAG;
michael@0 105 }
michael@0 106
michael@0 107 static vpx_codec_err_t
michael@0 108 update_error_state(vpx_codec_alg_priv_t *ctx,
michael@0 109 const struct vpx_internal_error_info *error) {
michael@0 110 vpx_codec_err_t res;
michael@0 111
michael@0 112 if ((res = error->error_code))
michael@0 113 ctx->base.err_detail = error->has_detail
michael@0 114 ? error->detail
michael@0 115 : NULL;
michael@0 116
michael@0 117 return res;
michael@0 118 }
michael@0 119
michael@0 120
michael@0 121 #undef ERROR
michael@0 122 #define ERROR(str) do {\
michael@0 123 ctx->base.err_detail = str;\
michael@0 124 return VPX_CODEC_INVALID_PARAM;\
michael@0 125 } while (0)
michael@0 126
michael@0 127 #define RANGE_CHECK(p, memb, lo, hi) do {\
michael@0 128 if (!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
michael@0 129 ERROR(#memb " out of range ["#lo".."#hi"]");\
michael@0 130 } while (0)
michael@0 131
michael@0 132 #define RANGE_CHECK_HI(p, memb, hi) do {\
michael@0 133 if (!((p)->memb <= (hi))) \
michael@0 134 ERROR(#memb " out of range [.."#hi"]");\
michael@0 135 } while (0)
michael@0 136
michael@0 137 #define RANGE_CHECK_LO(p, memb, lo) do {\
michael@0 138 if (!((p)->memb >= (lo))) \
michael@0 139 ERROR(#memb " out of range ["#lo"..]");\
michael@0 140 } while (0)
michael@0 141
michael@0 142 #define RANGE_CHECK_BOOL(p, memb) do {\
michael@0 143 if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
michael@0 144 } while (0)
michael@0 145
michael@0 146 static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
michael@0 147 const vpx_codec_enc_cfg_t *cfg,
michael@0 148 const struct vp9_extracfg *vp8_cfg) {
michael@0 149 RANGE_CHECK(cfg, g_w, 1, 65535); /* 16 bits available */
michael@0 150 RANGE_CHECK(cfg, g_h, 1, 65535); /* 16 bits available */
michael@0 151 RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
michael@0 152 RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den);
michael@0 153 RANGE_CHECK_HI(cfg, g_profile, 3);
michael@0 154
michael@0 155 RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
michael@0 156 RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
michael@0 157 RANGE_CHECK_BOOL(vp8_cfg, lossless);
michael@0 158 if (vp8_cfg->lossless) {
michael@0 159 RANGE_CHECK_HI(cfg, rc_max_quantizer, 0);
michael@0 160 RANGE_CHECK_HI(cfg, rc_min_quantizer, 0);
michael@0 161 }
michael@0 162 RANGE_CHECK(vp8_cfg, aq_mode, 0, AQ_MODES_COUNT - 1);
michael@0 163
michael@0 164 RANGE_CHECK_HI(cfg, g_threads, 64);
michael@0 165 RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
michael@0 166 RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
michael@0 167 RANGE_CHECK_HI(cfg, rc_undershoot_pct, 1000);
michael@0 168 RANGE_CHECK_HI(cfg, rc_overshoot_pct, 1000);
michael@0 169 RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
michael@0 170 RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
michael@0 171 // RANGE_CHECK_BOOL(cfg, g_delete_firstpassfile);
michael@0 172 RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
michael@0 173 RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
michael@0 174 RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
michael@0 175 RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
michael@0 176 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
michael@0 177
michael@0 178 RANGE_CHECK(cfg, ss_number_layers, 1,
michael@0 179 VPX_SS_MAX_LAYERS); /*Spatial layers max */
michael@0 180 /* VP8 does not support a lower bound on the keyframe interval in
michael@0 181 * automatic keyframe placement mode.
michael@0 182 */
michael@0 183 if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist
michael@0 184 && cfg->kf_min_dist > 0)
michael@0 185 ERROR("kf_min_dist not supported in auto mode, use 0 "
michael@0 186 "or kf_max_dist instead.");
michael@0 187
michael@0 188 RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref);
michael@0 189 RANGE_CHECK(vp8_cfg, cpu_used, -16, 16);
michael@0 190
michael@0 191 RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6);
michael@0 192
michael@0 193 RANGE_CHECK(vp8_cfg, tile_columns, 0, 6);
michael@0 194 RANGE_CHECK(vp8_cfg, tile_rows, 0, 2);
michael@0 195 RANGE_CHECK_HI(vp8_cfg, Sharpness, 7);
michael@0 196 RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
michael@0 197 RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6);
michael@0 198 RANGE_CHECK(vp8_cfg, arnr_type, 1, 3);
michael@0 199 RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
michael@0 200
michael@0 201 if (cfg->g_pass == VPX_RC_LAST_PASS) {
michael@0 202 size_t packet_sz = sizeof(FIRSTPASS_STATS);
michael@0 203 int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
michael@0 204 FIRSTPASS_STATS *stats;
michael@0 205
michael@0 206 if (!cfg->rc_twopass_stats_in.buf)
michael@0 207 ERROR("rc_twopass_stats_in.buf not set.");
michael@0 208
michael@0 209 if (cfg->rc_twopass_stats_in.sz % packet_sz)
michael@0 210 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
michael@0 211
michael@0 212 if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
michael@0 213 ERROR("rc_twopass_stats_in requires at least two packets.");
michael@0 214
michael@0 215 stats = (void *)((char *)cfg->rc_twopass_stats_in.buf
michael@0 216 + (n_packets - 1) * packet_sz);
michael@0 217
michael@0 218 if ((int)(stats->count + 0.5) != n_packets - 1)
michael@0 219 ERROR("rc_twopass_stats_in missing EOS stats packet");
michael@0 220 }
michael@0 221
michael@0 222 return VPX_CODEC_OK;
michael@0 223 }
michael@0 224
michael@0 225
michael@0 226 static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
michael@0 227 const vpx_image_t *img) {
michael@0 228 switch (img->fmt) {
michael@0 229 case VPX_IMG_FMT_YV12:
michael@0 230 case VPX_IMG_FMT_I420:
michael@0 231 case VPX_IMG_FMT_I422:
michael@0 232 case VPX_IMG_FMT_I444:
michael@0 233 break;
michael@0 234 default:
michael@0 235 ERROR("Invalid image format. Only YV12, I420, I422, I444 images are "
michael@0 236 "supported.");
michael@0 237 }
michael@0 238
michael@0 239 if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
michael@0 240 ERROR("Image size must match encoder init configuration size");
michael@0 241
michael@0 242 return VPX_CODEC_OK;
michael@0 243 }
michael@0 244
michael@0 245
michael@0 246 static vpx_codec_err_t set_vp9e_config(VP9_CONFIG *oxcf,
michael@0 247 vpx_codec_enc_cfg_t cfg,
michael@0 248 struct vp9_extracfg vp8_cfg) {
michael@0 249 oxcf->version = cfg.g_profile | (vp8_cfg.experimental ? 0x4 : 0);
michael@0 250 oxcf->width = cfg.g_w;
michael@0 251 oxcf->height = cfg.g_h;
michael@0 252 /* guess a frame rate if out of whack, use 30 */
michael@0 253 oxcf->framerate = (double)(cfg.g_timebase.den)
michael@0 254 / (double)(cfg.g_timebase.num);
michael@0 255
michael@0 256 if (oxcf->framerate > 180) {
michael@0 257 oxcf->framerate = 30;
michael@0 258 }
michael@0 259
michael@0 260 switch (cfg.g_pass) {
michael@0 261 case VPX_RC_ONE_PASS:
michael@0 262 oxcf->Mode = MODE_GOODQUALITY;
michael@0 263 break;
michael@0 264 case VPX_RC_FIRST_PASS:
michael@0 265 oxcf->Mode = MODE_FIRSTPASS;
michael@0 266 break;
michael@0 267 case VPX_RC_LAST_PASS:
michael@0 268 oxcf->Mode = MODE_SECONDPASS_BEST;
michael@0 269 break;
michael@0 270 }
michael@0 271
michael@0 272 if (cfg.g_pass == VPX_RC_FIRST_PASS) {
michael@0 273 oxcf->allow_lag = 0;
michael@0 274 oxcf->lag_in_frames = 0;
michael@0 275 } else {
michael@0 276 oxcf->allow_lag = (cfg.g_lag_in_frames) > 0;
michael@0 277 oxcf->lag_in_frames = cfg.g_lag_in_frames;
michael@0 278 }
michael@0 279
michael@0 280 // VBR only supported for now.
michael@0 281 // CBR code has been deprectated for experimental phase.
michael@0 282 // CQ mode not yet tested
michael@0 283 oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
michael@0 284 if (cfg.rc_end_usage == VPX_CQ)
michael@0 285 oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
michael@0 286 else if (cfg.rc_end_usage == VPX_Q)
michael@0 287 oxcf->end_usage = USAGE_CONSTANT_QUALITY;
michael@0 288 else if (cfg.rc_end_usage == VPX_CBR)
michael@0 289 oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
michael@0 290
michael@0 291 oxcf->target_bandwidth = cfg.rc_target_bitrate;
michael@0 292 oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct;
michael@0 293
michael@0 294 oxcf->best_allowed_q = cfg.rc_min_quantizer;
michael@0 295 oxcf->worst_allowed_q = cfg.rc_max_quantizer;
michael@0 296 oxcf->cq_level = vp8_cfg.cq_level;
michael@0 297 oxcf->fixed_q = -1;
michael@0 298
michael@0 299 oxcf->under_shoot_pct = cfg.rc_undershoot_pct;
michael@0 300 oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
michael@0 301
michael@0 302 oxcf->maximum_buffer_size = cfg.rc_buf_sz;
michael@0 303 oxcf->starting_buffer_level = cfg.rc_buf_initial_sz;
michael@0 304 oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz;
michael@0 305
michael@0 306 oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct;
michael@0 307 oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct;
michael@0 308 oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct;
michael@0 309
michael@0 310 oxcf->auto_key = cfg.kf_mode == VPX_KF_AUTO
michael@0 311 && cfg.kf_min_dist != cfg.kf_max_dist;
michael@0 312 // oxcf->kf_min_dist = cfg.kf_min_dis;
michael@0 313 oxcf->key_freq = cfg.kf_max_dist;
michael@0 314
michael@0 315 // oxcf->delete_first_pass_file = cfg.g_delete_firstpassfile;
michael@0 316 // strcpy(oxcf->first_pass_file, cfg.g_firstpass_file);
michael@0 317
michael@0 318 oxcf->cpu_used = vp8_cfg.cpu_used;
michael@0 319 oxcf->encode_breakout = vp8_cfg.static_thresh;
michael@0 320 oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref;
michael@0 321 oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity;
michael@0 322 oxcf->Sharpness = vp8_cfg.Sharpness;
michael@0 323
michael@0 324 oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in;
michael@0 325 oxcf->output_pkt_list = vp8_cfg.pkt_list;
michael@0 326
michael@0 327 oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
michael@0 328 oxcf->arnr_strength = vp8_cfg.arnr_strength;
michael@0 329 oxcf->arnr_type = vp8_cfg.arnr_type;
michael@0 330
michael@0 331 oxcf->tuning = vp8_cfg.tuning;
michael@0 332
michael@0 333 oxcf->tile_columns = vp8_cfg.tile_columns;
michael@0 334 oxcf->tile_rows = vp8_cfg.tile_rows;
michael@0 335
michael@0 336 oxcf->lossless = vp8_cfg.lossless;
michael@0 337
michael@0 338 oxcf->error_resilient_mode = cfg.g_error_resilient;
michael@0 339 oxcf->frame_parallel_decoding_mode = vp8_cfg.frame_parallel_decoding_mode;
michael@0 340
michael@0 341 oxcf->aq_mode = vp8_cfg.aq_mode;
michael@0 342
michael@0 343 oxcf->ss_number_layers = cfg.ss_number_layers;
michael@0 344 /*
michael@0 345 printf("Current VP9 Settings: \n");
michael@0 346 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
michael@0 347 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
michael@0 348 printf("Sharpness: %d\n", oxcf->Sharpness);
michael@0 349 printf("cpu_used: %d\n", oxcf->cpu_used);
michael@0 350 printf("Mode: %d\n", oxcf->Mode);
michael@0 351 // printf("delete_first_pass_file: %d\n", oxcf->delete_first_pass_file);
michael@0 352 printf("auto_key: %d\n", oxcf->auto_key);
michael@0 353 printf("key_freq: %d\n", oxcf->key_freq);
michael@0 354 printf("end_usage: %d\n", oxcf->end_usage);
michael@0 355 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
michael@0 356 printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
michael@0 357 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
michael@0 358 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
michael@0 359 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
michael@0 360 printf("fixed_q: %d\n", oxcf->fixed_q);
michael@0 361 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
michael@0 362 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
michael@0 363 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
michael@0 364 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
michael@0 365 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
michael@0 366 printf("allow_lag: %d\n", oxcf->allow_lag);
michael@0 367 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
michael@0 368 printf("play_alternate: %d\n", oxcf->play_alternate);
michael@0 369 printf("Version: %d\n", oxcf->Version);
michael@0 370 printf("encode_breakout: %d\n", oxcf->encode_breakout);
michael@0 371 printf("error resilient: %d\n", oxcf->error_resilient_mode);
michael@0 372 printf("frame parallel detokenization: %d\n",
michael@0 373 oxcf->frame_parallel_decoding_mode);
michael@0 374 */
michael@0 375 return VPX_CODEC_OK;
michael@0 376 }
michael@0 377
michael@0 378 static vpx_codec_err_t vp9e_set_config(vpx_codec_alg_priv_t *ctx,
michael@0 379 const vpx_codec_enc_cfg_t *cfg) {
michael@0 380 vpx_codec_err_t res;
michael@0 381
michael@0 382 if ((cfg->g_w != ctx->cfg.g_w) || (cfg->g_h != ctx->cfg.g_h))
michael@0 383 ERROR("Cannot change width or height after initialization");
michael@0 384
michael@0 385 /* Prevent increasing lag_in_frames. This check is stricter than it needs
michael@0 386 * to be -- the limit is not increasing past the first lag_in_frames
michael@0 387 * value, but we don't track the initial config, only the last successful
michael@0 388 * config.
michael@0 389 */
michael@0 390 if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
michael@0 391 ERROR("Cannot increase lag_in_frames");
michael@0 392
michael@0 393 res = validate_config(ctx, cfg, &ctx->vp8_cfg);
michael@0 394
michael@0 395 if (!res) {
michael@0 396 ctx->cfg = *cfg;
michael@0 397 set_vp9e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
michael@0 398 vp9_change_config(ctx->cpi, &ctx->oxcf);
michael@0 399 }
michael@0 400
michael@0 401 return res;
michael@0 402 }
michael@0 403
michael@0 404
michael@0 405 int vp9_reverse_trans(int q);
michael@0 406
michael@0 407
michael@0 408 static vpx_codec_err_t get_param(vpx_codec_alg_priv_t *ctx,
michael@0 409 int ctrl_id,
michael@0 410 va_list args) {
michael@0 411 void *arg = va_arg(args, void *);
michael@0 412
michael@0 413 #define MAP(id, var) case id: *(RECAST(id, arg)) = var; break
michael@0 414
michael@0 415 if (!arg)
michael@0 416 return VPX_CODEC_INVALID_PARAM;
michael@0 417
michael@0 418 switch (ctrl_id) {
michael@0 419 MAP(VP8E_GET_LAST_QUANTIZER, vp9_get_quantizer(ctx->cpi));
michael@0 420 MAP(VP8E_GET_LAST_QUANTIZER_64,
michael@0 421 vp9_reverse_trans(vp9_get_quantizer(ctx->cpi)));
michael@0 422 }
michael@0 423
michael@0 424 return VPX_CODEC_OK;
michael@0 425 #undef MAP
michael@0 426 }
michael@0 427
michael@0 428
michael@0 429 static vpx_codec_err_t set_param(vpx_codec_alg_priv_t *ctx,
michael@0 430 int ctrl_id,
michael@0 431 va_list args) {
michael@0 432 vpx_codec_err_t res = VPX_CODEC_OK;
michael@0 433 struct vp9_extracfg xcfg = ctx->vp8_cfg;
michael@0 434
michael@0 435 #define MAP(id, var) case id: var = CAST(id, args); break;
michael@0 436
michael@0 437 switch (ctrl_id) {
michael@0 438 MAP(VP8E_SET_CPUUSED, xcfg.cpu_used);
michael@0 439 MAP(VP8E_SET_ENABLEAUTOALTREF, xcfg.enable_auto_alt_ref);
michael@0 440 MAP(VP8E_SET_NOISE_SENSITIVITY, xcfg.noise_sensitivity);
michael@0 441 MAP(VP8E_SET_SHARPNESS, xcfg.Sharpness);
michael@0 442 MAP(VP8E_SET_STATIC_THRESHOLD, xcfg.static_thresh);
michael@0 443 MAP(VP9E_SET_TILE_COLUMNS, xcfg.tile_columns);
michael@0 444 MAP(VP9E_SET_TILE_ROWS, xcfg.tile_rows);
michael@0 445 MAP(VP8E_SET_ARNR_MAXFRAMES, xcfg.arnr_max_frames);
michael@0 446 MAP(VP8E_SET_ARNR_STRENGTH, xcfg.arnr_strength);
michael@0 447 MAP(VP8E_SET_ARNR_TYPE, xcfg.arnr_type);
michael@0 448 MAP(VP8E_SET_TUNING, xcfg.tuning);
michael@0 449 MAP(VP8E_SET_CQ_LEVEL, xcfg.cq_level);
michael@0 450 MAP(VP8E_SET_MAX_INTRA_BITRATE_PCT, xcfg.rc_max_intra_bitrate_pct);
michael@0 451 MAP(VP9E_SET_LOSSLESS, xcfg.lossless);
michael@0 452 MAP(VP9E_SET_FRAME_PARALLEL_DECODING, xcfg.frame_parallel_decoding_mode);
michael@0 453 MAP(VP9E_SET_AQ_MODE, xcfg.aq_mode);
michael@0 454 }
michael@0 455
michael@0 456 res = validate_config(ctx, &ctx->cfg, &xcfg);
michael@0 457
michael@0 458 if (!res) {
michael@0 459 ctx->vp8_cfg = xcfg;
michael@0 460 set_vp9e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
michael@0 461 vp9_change_config(ctx->cpi, &ctx->oxcf);
michael@0 462 }
michael@0 463
michael@0 464 return res;
michael@0 465 #undef MAP
michael@0 466 }
michael@0 467
michael@0 468
michael@0 469 static vpx_codec_err_t vp9e_common_init(vpx_codec_ctx_t *ctx,
michael@0 470 int experimental) {
michael@0 471 vpx_codec_err_t res = VPX_CODEC_OK;
michael@0 472 struct vpx_codec_alg_priv *priv;
michael@0 473 vpx_codec_enc_cfg_t *cfg;
michael@0 474 unsigned int i;
michael@0 475
michael@0 476 VP9_PTR optr;
michael@0 477
michael@0 478 if (!ctx->priv) {
michael@0 479 priv = calloc(1, sizeof(struct vpx_codec_alg_priv));
michael@0 480
michael@0 481 if (!priv) {
michael@0 482 return VPX_CODEC_MEM_ERROR;
michael@0 483 }
michael@0 484
michael@0 485 ctx->priv = &priv->base;
michael@0 486 ctx->priv->sz = sizeof(*ctx->priv);
michael@0 487 ctx->priv->iface = ctx->iface;
michael@0 488 ctx->priv->alg_priv = priv;
michael@0 489 ctx->priv->init_flags = ctx->init_flags;
michael@0 490 ctx->priv->enc.total_encoders = 1;
michael@0 491
michael@0 492 if (ctx->config.enc) {
michael@0 493 /* Update the reference to the config structure to an
michael@0 494 * internal copy.
michael@0 495 */
michael@0 496 ctx->priv->alg_priv->cfg = *ctx->config.enc;
michael@0 497 ctx->config.enc = &ctx->priv->alg_priv->cfg;
michael@0 498 }
michael@0 499
michael@0 500 cfg = &ctx->priv->alg_priv->cfg;
michael@0 501
michael@0 502 /* Select the extra vp6 configuration table based on the current
michael@0 503 * usage value. If the current usage value isn't found, use the
michael@0 504 * values for usage case 0.
michael@0 505 */
michael@0 506 for (i = 0;
michael@0 507 extracfg_map[i].usage && extracfg_map[i].usage != cfg->g_usage;
michael@0 508 i++) {}
michael@0 509
michael@0 510 priv->vp8_cfg = extracfg_map[i].cfg;
michael@0 511 priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
michael@0 512 priv->vp8_cfg.experimental = experimental;
michael@0 513
michael@0 514 // TODO(agrange) Check the limits set on this buffer, or the check that is
michael@0 515 // applied in vp9e_encode.
michael@0 516 priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 8;
michael@0 517 // priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
michael@0 518
michael@0 519 if (priv->cx_data_sz < 4096) priv->cx_data_sz = 4096;
michael@0 520
michael@0 521 priv->cx_data = malloc(priv->cx_data_sz);
michael@0 522
michael@0 523 if (!priv->cx_data) {
michael@0 524 return VPX_CODEC_MEM_ERROR;
michael@0 525 }
michael@0 526
michael@0 527 vp9_initialize_enc();
michael@0 528
michael@0 529 res = validate_config(priv, &priv->cfg, &priv->vp8_cfg);
michael@0 530
michael@0 531 if (!res) {
michael@0 532 set_vp9e_config(&ctx->priv->alg_priv->oxcf,
michael@0 533 ctx->priv->alg_priv->cfg,
michael@0 534 ctx->priv->alg_priv->vp8_cfg);
michael@0 535 optr = vp9_create_compressor(&ctx->priv->alg_priv->oxcf);
michael@0 536
michael@0 537 if (!optr)
michael@0 538 res = VPX_CODEC_MEM_ERROR;
michael@0 539 else
michael@0 540 ctx->priv->alg_priv->cpi = optr;
michael@0 541 }
michael@0 542 }
michael@0 543
michael@0 544 return res;
michael@0 545 }
michael@0 546
michael@0 547
michael@0 548 static vpx_codec_err_t vp9e_init(vpx_codec_ctx_t *ctx,
michael@0 549 vpx_codec_priv_enc_mr_cfg_t *data) {
michael@0 550 return vp9e_common_init(ctx, 0);
michael@0 551 }
michael@0 552
michael@0 553
michael@0 554 #if CONFIG_EXPERIMENTAL
michael@0 555 static vpx_codec_err_t vp9e_exp_init(vpx_codec_ctx_t *ctx,
michael@0 556 vpx_codec_priv_enc_mr_cfg_t *data) {
michael@0 557 return vp9e_common_init(ctx, 1);
michael@0 558 }
michael@0 559 #endif
michael@0 560
michael@0 561
michael@0 562 static vpx_codec_err_t vp9e_destroy(vpx_codec_alg_priv_t *ctx) {
michael@0 563 free(ctx->cx_data);
michael@0 564 vp9_remove_compressor(&ctx->cpi);
michael@0 565 free(ctx);
michael@0 566 return VPX_CODEC_OK;
michael@0 567 }
michael@0 568
michael@0 569 static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
michael@0 570 unsigned long duration,
michael@0 571 unsigned long deadline) {
michael@0 572 unsigned int new_qc;
michael@0 573
michael@0 574 /* Use best quality mode if no deadline is given. */
michael@0 575 if (deadline)
michael@0 576 new_qc = MODE_GOODQUALITY;
michael@0 577 else
michael@0 578 new_qc = MODE_BESTQUALITY;
michael@0 579
michael@0 580 if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS)
michael@0 581 new_qc = MODE_FIRSTPASS;
michael@0 582 else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS)
michael@0 583 new_qc = (new_qc == MODE_BESTQUALITY)
michael@0 584 ? MODE_SECONDPASS_BEST
michael@0 585 : MODE_SECONDPASS;
michael@0 586
michael@0 587 if (ctx->oxcf.Mode != new_qc) {
michael@0 588 ctx->oxcf.Mode = new_qc;
michael@0 589 vp9_change_config(ctx->cpi, &ctx->oxcf);
michael@0 590 }
michael@0 591 }
michael@0 592
michael@0 593
michael@0 594 static int write_superframe_index(vpx_codec_alg_priv_t *ctx) {
michael@0 595 uint8_t marker = 0xc0;
michael@0 596 unsigned int mask;
michael@0 597 int mag, index_sz;
michael@0 598
michael@0 599 assert(ctx->pending_frame_count);
michael@0 600 assert(ctx->pending_frame_count <= 8);
michael@0 601
michael@0 602 /* Add the number of frames to the marker byte */
michael@0 603 marker |= ctx->pending_frame_count - 1;
michael@0 604
michael@0 605 /* Choose the magnitude */
michael@0 606 for (mag = 0, mask = 0xff; mag < 4; mag++) {
michael@0 607 if (ctx->pending_frame_magnitude < mask)
michael@0 608 break;
michael@0 609 mask <<= 8;
michael@0 610 mask |= 0xff;
michael@0 611 }
michael@0 612 marker |= mag << 3;
michael@0 613
michael@0 614 /* Write the index */
michael@0 615 index_sz = 2 + (mag + 1) * ctx->pending_frame_count;
michael@0 616 if (ctx->pending_cx_data_sz + index_sz < ctx->cx_data_sz) {
michael@0 617 uint8_t *x = ctx->pending_cx_data + ctx->pending_cx_data_sz;
michael@0 618 int i, j;
michael@0 619
michael@0 620 *x++ = marker;
michael@0 621 for (i = 0; i < ctx->pending_frame_count; i++) {
michael@0 622 int this_sz = ctx->pending_frame_sizes[i];
michael@0 623
michael@0 624 for (j = 0; j <= mag; j++) {
michael@0 625 *x++ = this_sz & 0xff;
michael@0 626 this_sz >>= 8;
michael@0 627 }
michael@0 628 }
michael@0 629 *x++ = marker;
michael@0 630 ctx->pending_cx_data_sz += index_sz;
michael@0 631 }
michael@0 632 return index_sz;
michael@0 633 }
michael@0 634
michael@0 635 static vpx_codec_err_t vp9e_encode(vpx_codec_alg_priv_t *ctx,
michael@0 636 const vpx_image_t *img,
michael@0 637 vpx_codec_pts_t pts,
michael@0 638 unsigned long duration,
michael@0 639 vpx_enc_frame_flags_t flags,
michael@0 640 unsigned long deadline) {
michael@0 641 vpx_codec_err_t res = VPX_CODEC_OK;
michael@0 642
michael@0 643 if (img)
michael@0 644 res = validate_img(ctx, img);
michael@0 645
michael@0 646 pick_quickcompress_mode(ctx, duration, deadline);
michael@0 647 vpx_codec_pkt_list_init(&ctx->pkt_list);
michael@0 648
michael@0 649 /* Handle Flags */
michael@0 650 if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF))
michael@0 651 || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) {
michael@0 652 ctx->base.err_detail = "Conflicting flags.";
michael@0 653 return VPX_CODEC_INVALID_PARAM;
michael@0 654 }
michael@0 655
michael@0 656 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF
michael@0 657 | VP8_EFLAG_NO_REF_ARF)) {
michael@0 658 int ref = 7;
michael@0 659
michael@0 660 if (flags & VP8_EFLAG_NO_REF_LAST)
michael@0 661 ref ^= VP9_LAST_FLAG;
michael@0 662
michael@0 663 if (flags & VP8_EFLAG_NO_REF_GF)
michael@0 664 ref ^= VP9_GOLD_FLAG;
michael@0 665
michael@0 666 if (flags & VP8_EFLAG_NO_REF_ARF)
michael@0 667 ref ^= VP9_ALT_FLAG;
michael@0 668
michael@0 669 vp9_use_as_reference(ctx->cpi, ref);
michael@0 670 }
michael@0 671
michael@0 672 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF
michael@0 673 | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF
michael@0 674 | VP8_EFLAG_FORCE_ARF)) {
michael@0 675 int upd = 7;
michael@0 676
michael@0 677 if (flags & VP8_EFLAG_NO_UPD_LAST)
michael@0 678 upd ^= VP9_LAST_FLAG;
michael@0 679
michael@0 680 if (flags & VP8_EFLAG_NO_UPD_GF)
michael@0 681 upd ^= VP9_GOLD_FLAG;
michael@0 682
michael@0 683 if (flags & VP8_EFLAG_NO_UPD_ARF)
michael@0 684 upd ^= VP9_ALT_FLAG;
michael@0 685
michael@0 686 vp9_update_reference(ctx->cpi, upd);
michael@0 687 }
michael@0 688
michael@0 689 if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
michael@0 690 vp9_update_entropy(ctx->cpi, 0);
michael@0 691 }
michael@0 692
michael@0 693 /* Handle fixed keyframe intervals */
michael@0 694 if (ctx->cfg.kf_mode == VPX_KF_AUTO
michael@0 695 && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
michael@0 696 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
michael@0 697 flags |= VPX_EFLAG_FORCE_KF;
michael@0 698 ctx->fixed_kf_cntr = 1;
michael@0 699 }
michael@0 700 }
michael@0 701
michael@0 702 /* Initialize the encoder instance on the first frame*/
michael@0 703 if (!res && ctx->cpi) {
michael@0 704 unsigned int lib_flags;
michael@0 705 YV12_BUFFER_CONFIG sd;
michael@0 706 int64_t dst_time_stamp, dst_end_time_stamp;
michael@0 707 unsigned long size, cx_data_sz;
michael@0 708 unsigned char *cx_data;
michael@0 709
michael@0 710 /* Set up internal flags */
michael@0 711 if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
michael@0 712 ((VP9_COMP *)ctx->cpi)->b_calculate_psnr = 1;
michael@0 713
michael@0 714 // if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION)
michael@0 715 // ((VP9_COMP *)ctx->cpi)->output_partition = 1;
michael@0 716
michael@0 717 /* Convert API flags to internal codec lib flags */
michael@0 718 lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
michael@0 719
michael@0 720 /* vp8 use 10,000,000 ticks/second as time stamp */
michael@0 721 dst_time_stamp = pts * 10000000 * ctx->cfg.g_timebase.num
michael@0 722 / ctx->cfg.g_timebase.den;
michael@0 723 dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num /
michael@0 724 ctx->cfg.g_timebase.den;
michael@0 725
michael@0 726 if (img != NULL) {
michael@0 727 res = image2yuvconfig(img, &sd);
michael@0 728
michael@0 729 if (vp9_receive_raw_frame(ctx->cpi, lib_flags,
michael@0 730 &sd, dst_time_stamp, dst_end_time_stamp)) {
michael@0 731 VP9_COMP *cpi = (VP9_COMP *)ctx->cpi;
michael@0 732 res = update_error_state(ctx, &cpi->common.error);
michael@0 733 }
michael@0 734 }
michael@0 735
michael@0 736 cx_data = ctx->cx_data;
michael@0 737 cx_data_sz = ctx->cx_data_sz;
michael@0 738 lib_flags = 0;
michael@0 739
michael@0 740 /* Any pending invisible frames? */
michael@0 741 if (ctx->pending_cx_data) {
michael@0 742 memmove(cx_data, ctx->pending_cx_data, ctx->pending_cx_data_sz);
michael@0 743 ctx->pending_cx_data = cx_data;
michael@0 744 cx_data += ctx->pending_cx_data_sz;
michael@0 745 cx_data_sz -= ctx->pending_cx_data_sz;
michael@0 746
michael@0 747 /* TODO: this is a minimal check, the underlying codec doesn't respect
michael@0 748 * the buffer size anyway.
michael@0 749 */
michael@0 750 if (cx_data_sz < ctx->cx_data_sz / 2) {
michael@0 751 ctx->base.err_detail = "Compressed data buffer too small";
michael@0 752 return VPX_CODEC_ERROR;
michael@0 753 }
michael@0 754 }
michael@0 755
michael@0 756 while (cx_data_sz >= ctx->cx_data_sz / 2 &&
michael@0 757 -1 != vp9_get_compressed_data(ctx->cpi, &lib_flags, &size,
michael@0 758 cx_data, &dst_time_stamp,
michael@0 759 &dst_end_time_stamp, !img)) {
michael@0 760 if (size) {
michael@0 761 vpx_codec_pts_t round, delta;
michael@0 762 vpx_codec_cx_pkt_t pkt;
michael@0 763 VP9_COMP *cpi = (VP9_COMP *)ctx->cpi;
michael@0 764
michael@0 765 /* Pack invisible frames with the next visible frame */
michael@0 766 if (!cpi->common.show_frame) {
michael@0 767 if (!ctx->pending_cx_data)
michael@0 768 ctx->pending_cx_data = cx_data;
michael@0 769 ctx->pending_cx_data_sz += size;
michael@0 770 ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
michael@0 771 ctx->pending_frame_magnitude |= size;
michael@0 772 cx_data += size;
michael@0 773 cx_data_sz -= size;
michael@0 774 continue;
michael@0 775 }
michael@0 776
michael@0 777 /* Add the frame packet to the list of returned packets. */
michael@0 778 round = (vpx_codec_pts_t)1000000 * ctx->cfg.g_timebase.num / 2 - 1;
michael@0 779 delta = (dst_end_time_stamp - dst_time_stamp);
michael@0 780 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
michael@0 781 pkt.data.frame.pts =
michael@0 782 (dst_time_stamp * ctx->cfg.g_timebase.den + round)
michael@0 783 / ctx->cfg.g_timebase.num / 10000000;
michael@0 784 pkt.data.frame.duration = (unsigned long)
michael@0 785 ((delta * ctx->cfg.g_timebase.den + round)
michael@0 786 / ctx->cfg.g_timebase.num / 10000000);
michael@0 787 pkt.data.frame.flags = lib_flags << 16;
michael@0 788
michael@0 789 if (lib_flags & FRAMEFLAGS_KEY)
michael@0 790 pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
michael@0 791
michael@0 792 if (!cpi->common.show_frame) {
michael@0 793 pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
michael@0 794
michael@0 795 // This timestamp should be as close as possible to the
michael@0 796 // prior PTS so that if a decoder uses pts to schedule when
michael@0 797 // to do this, we start right after last frame was decoded.
michael@0 798 // Invisible frames have no duration.
michael@0 799 pkt.data.frame.pts = ((cpi->last_time_stamp_seen
michael@0 800 * ctx->cfg.g_timebase.den + round)
michael@0 801 / ctx->cfg.g_timebase.num / 10000000) + 1;
michael@0 802 pkt.data.frame.duration = 0;
michael@0 803 }
michael@0 804
michael@0 805 if (cpi->droppable)
michael@0 806 pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE;
michael@0 807
michael@0 808 /*if (cpi->output_partition)
michael@0 809 {
michael@0 810 int i;
michael@0 811 const int num_partitions = 1;
michael@0 812
michael@0 813 pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT;
michael@0 814
michael@0 815 for (i = 0; i < num_partitions; ++i)
michael@0 816 {
michael@0 817 pkt.data.frame.buf = cx_data;
michael@0 818 pkt.data.frame.sz = cpi->partition_sz[i];
michael@0 819 pkt.data.frame.partition_id = i;
michael@0 820 // don't set the fragment bit for the last partition
michael@0 821 if (i == (num_partitions - 1))
michael@0 822 pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT;
michael@0 823 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
michael@0 824 cx_data += cpi->partition_sz[i];
michael@0 825 cx_data_sz -= cpi->partition_sz[i];
michael@0 826 }
michael@0 827 }
michael@0 828 else*/
michael@0 829 {
michael@0 830 if (ctx->pending_cx_data) {
michael@0 831 ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
michael@0 832 ctx->pending_frame_magnitude |= size;
michael@0 833 ctx->pending_cx_data_sz += size;
michael@0 834 size += write_superframe_index(ctx);
michael@0 835 pkt.data.frame.buf = ctx->pending_cx_data;
michael@0 836 pkt.data.frame.sz = ctx->pending_cx_data_sz;
michael@0 837 ctx->pending_cx_data = NULL;
michael@0 838 ctx->pending_cx_data_sz = 0;
michael@0 839 ctx->pending_frame_count = 0;
michael@0 840 ctx->pending_frame_magnitude = 0;
michael@0 841 } else {
michael@0 842 pkt.data.frame.buf = cx_data;
michael@0 843 pkt.data.frame.sz = size;
michael@0 844 }
michael@0 845 pkt.data.frame.partition_id = -1;
michael@0 846 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
michael@0 847 cx_data += size;
michael@0 848 cx_data_sz -= size;
michael@0 849 }
michael@0 850 }
michael@0 851 }
michael@0 852 }
michael@0 853
michael@0 854 return res;
michael@0 855 }
michael@0 856
michael@0 857
michael@0 858 static const vpx_codec_cx_pkt_t *vp9e_get_cxdata(vpx_codec_alg_priv_t *ctx,
michael@0 859 vpx_codec_iter_t *iter) {
michael@0 860 return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
michael@0 861 }
michael@0 862
michael@0 863 static vpx_codec_err_t vp9e_set_reference(vpx_codec_alg_priv_t *ctx,
michael@0 864 int ctr_id,
michael@0 865 va_list args) {
michael@0 866 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
michael@0 867
michael@0 868 if (data) {
michael@0 869 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
michael@0 870 YV12_BUFFER_CONFIG sd;
michael@0 871
michael@0 872 image2yuvconfig(&frame->img, &sd);
michael@0 873 vp9_set_reference_enc(ctx->cpi, ref_frame_to_vp9_reframe(frame->frame_type),
michael@0 874 &sd);
michael@0 875 return VPX_CODEC_OK;
michael@0 876 } else {
michael@0 877 return VPX_CODEC_INVALID_PARAM;
michael@0 878 }
michael@0 879 }
michael@0 880
michael@0 881 static vpx_codec_err_t vp9e_copy_reference(vpx_codec_alg_priv_t *ctx,
michael@0 882 int ctr_id,
michael@0 883 va_list args) {
michael@0 884 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
michael@0 885
michael@0 886 if (data) {
michael@0 887 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
michael@0 888 YV12_BUFFER_CONFIG sd;
michael@0 889
michael@0 890 image2yuvconfig(&frame->img, &sd);
michael@0 891 vp9_copy_reference_enc(ctx->cpi,
michael@0 892 ref_frame_to_vp9_reframe(frame->frame_type), &sd);
michael@0 893 return VPX_CODEC_OK;
michael@0 894 } else {
michael@0 895 return VPX_CODEC_INVALID_PARAM;
michael@0 896 }
michael@0 897 }
michael@0 898
michael@0 899 static vpx_codec_err_t get_reference(vpx_codec_alg_priv_t *ctx,
michael@0 900 int ctr_id,
michael@0 901 va_list args) {
michael@0 902 vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
michael@0 903
michael@0 904 if (data) {
michael@0 905 YV12_BUFFER_CONFIG* fb;
michael@0 906
michael@0 907 vp9_get_reference_enc(ctx->cpi, data->idx, &fb);
michael@0 908 yuvconfig2image(&data->img, fb, NULL);
michael@0 909 return VPX_CODEC_OK;
michael@0 910 } else {
michael@0 911 return VPX_CODEC_INVALID_PARAM;
michael@0 912 }
michael@0 913 }
michael@0 914
michael@0 915 static vpx_codec_err_t vp9e_set_previewpp(vpx_codec_alg_priv_t *ctx,
michael@0 916 int ctr_id,
michael@0 917 va_list args) {
michael@0 918 #if CONFIG_VP9_POSTPROC
michael@0 919 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
michael@0 920 (void)ctr_id;
michael@0 921
michael@0 922 if (data) {
michael@0 923 ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
michael@0 924 return VPX_CODEC_OK;
michael@0 925 } else {
michael@0 926 return VPX_CODEC_INVALID_PARAM;
michael@0 927 }
michael@0 928 #else
michael@0 929 (void)ctx;
michael@0 930 (void)ctr_id;
michael@0 931 (void)args;
michael@0 932 return VPX_CODEC_INCAPABLE;
michael@0 933 #endif
michael@0 934 }
michael@0 935
michael@0 936
michael@0 937 static vpx_image_t *vp9e_get_preview(vpx_codec_alg_priv_t *ctx) {
michael@0 938 YV12_BUFFER_CONFIG sd;
michael@0 939 vp9_ppflags_t flags = {0};
michael@0 940
michael@0 941 if (ctx->preview_ppcfg.post_proc_flag) {
michael@0 942 flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
michael@0 943 flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
michael@0 944 flags.noise_level = ctx->preview_ppcfg.noise_level;
michael@0 945 }
michael@0 946
michael@0 947 if (0 == vp9_get_preview_raw_frame(ctx->cpi, &sd, &flags)) {
michael@0 948 yuvconfig2image(&ctx->preview_img, &sd, NULL);
michael@0 949 return &ctx->preview_img;
michael@0 950 } else {
michael@0 951 return NULL;
michael@0 952 }
michael@0 953 }
michael@0 954
michael@0 955 static vpx_codec_err_t vp9e_update_entropy(vpx_codec_alg_priv_t *ctx,
michael@0 956 int ctr_id,
michael@0 957 va_list args) {
michael@0 958 int update = va_arg(args, int);
michael@0 959 vp9_update_entropy(ctx->cpi, update);
michael@0 960 return VPX_CODEC_OK;
michael@0 961 }
michael@0 962
michael@0 963 static vpx_codec_err_t vp9e_update_reference(vpx_codec_alg_priv_t *ctx,
michael@0 964 int ctr_id,
michael@0 965 va_list args) {
michael@0 966 int update = va_arg(args, int);
michael@0 967 vp9_update_reference(ctx->cpi, update);
michael@0 968 return VPX_CODEC_OK;
michael@0 969 }
michael@0 970
michael@0 971 static vpx_codec_err_t vp9e_use_reference(vpx_codec_alg_priv_t *ctx,
michael@0 972 int ctr_id,
michael@0 973 va_list args) {
michael@0 974 int reference_flag = va_arg(args, int);
michael@0 975 vp9_use_as_reference(ctx->cpi, reference_flag);
michael@0 976 return VPX_CODEC_OK;
michael@0 977 }
michael@0 978
michael@0 979 static vpx_codec_err_t vp9e_set_roi_map(vpx_codec_alg_priv_t *ctx,
michael@0 980 int ctr_id,
michael@0 981 va_list args) {
michael@0 982 // TODO(yaowu): Need to re-implement and test for VP9.
michael@0 983 return VPX_CODEC_INVALID_PARAM;
michael@0 984 }
michael@0 985
michael@0 986
michael@0 987 static vpx_codec_err_t vp9e_set_activemap(vpx_codec_alg_priv_t *ctx,
michael@0 988 int ctr_id,
michael@0 989 va_list args) {
michael@0 990 // TODO(yaowu): Need to re-implement and test for VP9.
michael@0 991 return VPX_CODEC_INVALID_PARAM;
michael@0 992 }
michael@0 993
michael@0 994 static vpx_codec_err_t vp9e_set_scalemode(vpx_codec_alg_priv_t *ctx,
michael@0 995 int ctr_id,
michael@0 996 va_list args) {
michael@0 997 vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *);
michael@0 998
michael@0 999 if (data) {
michael@0 1000 int res;
michael@0 1001 vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data;
michael@0 1002 res = vp9_set_internal_size(ctx->cpi,
michael@0 1003 (VPX_SCALING)scalemode.h_scaling_mode,
michael@0 1004 (VPX_SCALING)scalemode.v_scaling_mode);
michael@0 1005
michael@0 1006 if (!res) {
michael@0 1007 return VPX_CODEC_OK;
michael@0 1008 } else {
michael@0 1009 return VPX_CODEC_INVALID_PARAM;
michael@0 1010 }
michael@0 1011 } else {
michael@0 1012 return VPX_CODEC_INVALID_PARAM;
michael@0 1013 }
michael@0 1014 }
michael@0 1015
michael@0 1016 static vpx_codec_err_t vp9e_set_svc(vpx_codec_alg_priv_t *ctx, int ctr_id,
michael@0 1017 va_list args) {
michael@0 1018 int data = va_arg(args, int);
michael@0 1019 vp9_set_svc(ctx->cpi, data);
michael@0 1020 return VPX_CODEC_OK;
michael@0 1021 }
michael@0 1022
michael@0 1023 static vpx_codec_err_t vp9e_set_svc_parameters(vpx_codec_alg_priv_t *ctx,
michael@0 1024 int ctr_id, va_list args) {
michael@0 1025 vpx_svc_parameters_t *data = va_arg(args, vpx_svc_parameters_t *);
michael@0 1026 VP9_COMP *cpi = (VP9_COMP *)ctx->cpi;
michael@0 1027 vpx_svc_parameters_t params;
michael@0 1028
michael@0 1029 if (data == NULL) {
michael@0 1030 return VPX_CODEC_INVALID_PARAM;
michael@0 1031 }
michael@0 1032
michael@0 1033 params = *(vpx_svc_parameters_t *)data;
michael@0 1034
michael@0 1035 cpi->current_layer = params.layer;
michael@0 1036 cpi->lst_fb_idx = params.lst_fb_idx;
michael@0 1037 cpi->gld_fb_idx = params.gld_fb_idx;
michael@0 1038 cpi->alt_fb_idx = params.alt_fb_idx;
michael@0 1039
michael@0 1040 if (vp9_set_size_literal(ctx->cpi, params.width, params.height) != 0) {
michael@0 1041 return VPX_CODEC_INVALID_PARAM;
michael@0 1042 }
michael@0 1043
michael@0 1044 ctx->cfg.rc_max_quantizer = params.max_quantizer;
michael@0 1045 ctx->cfg.rc_min_quantizer = params.min_quantizer;
michael@0 1046
michael@0 1047 set_vp9e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
michael@0 1048 vp9_change_config(ctx->cpi, &ctx->oxcf);
michael@0 1049
michael@0 1050 return VPX_CODEC_OK;
michael@0 1051 }
michael@0 1052
michael@0 1053 static vpx_codec_ctrl_fn_map_t vp9e_ctf_maps[] = {
michael@0 1054 {VP8_SET_REFERENCE, vp9e_set_reference},
michael@0 1055 {VP8_COPY_REFERENCE, vp9e_copy_reference},
michael@0 1056 {VP8_SET_POSTPROC, vp9e_set_previewpp},
michael@0 1057 {VP8E_UPD_ENTROPY, vp9e_update_entropy},
michael@0 1058 {VP8E_UPD_REFERENCE, vp9e_update_reference},
michael@0 1059 {VP8E_USE_REFERENCE, vp9e_use_reference},
michael@0 1060 {VP8E_SET_ROI_MAP, vp9e_set_roi_map},
michael@0 1061 {VP8E_SET_ACTIVEMAP, vp9e_set_activemap},
michael@0 1062 {VP8E_SET_SCALEMODE, vp9e_set_scalemode},
michael@0 1063 {VP8E_SET_CPUUSED, set_param},
michael@0 1064 {VP8E_SET_NOISE_SENSITIVITY, set_param},
michael@0 1065 {VP8E_SET_ENABLEAUTOALTREF, set_param},
michael@0 1066 {VP8E_SET_SHARPNESS, set_param},
michael@0 1067 {VP8E_SET_STATIC_THRESHOLD, set_param},
michael@0 1068 {VP9E_SET_TILE_COLUMNS, set_param},
michael@0 1069 {VP9E_SET_TILE_ROWS, set_param},
michael@0 1070 {VP8E_GET_LAST_QUANTIZER, get_param},
michael@0 1071 {VP8E_GET_LAST_QUANTIZER_64, get_param},
michael@0 1072 {VP8E_SET_ARNR_MAXFRAMES, set_param},
michael@0 1073 {VP8E_SET_ARNR_STRENGTH, set_param},
michael@0 1074 {VP8E_SET_ARNR_TYPE, set_param},
michael@0 1075 {VP8E_SET_TUNING, set_param},
michael@0 1076 {VP8E_SET_CQ_LEVEL, set_param},
michael@0 1077 {VP8E_SET_MAX_INTRA_BITRATE_PCT, set_param},
michael@0 1078 {VP9E_SET_LOSSLESS, set_param},
michael@0 1079 {VP9E_SET_FRAME_PARALLEL_DECODING, set_param},
michael@0 1080 {VP9E_SET_AQ_MODE, set_param},
michael@0 1081 {VP9_GET_REFERENCE, get_reference},
michael@0 1082 {VP9E_SET_SVC, vp9e_set_svc},
michael@0 1083 {VP9E_SET_SVC_PARAMETERS, vp9e_set_svc_parameters},
michael@0 1084 { -1, NULL},
michael@0 1085 };
michael@0 1086
michael@0 1087 static vpx_codec_enc_cfg_map_t vp9e_usage_cfg_map[] = {
michael@0 1088 {
michael@0 1089 0,
michael@0 1090 { // NOLINT
michael@0 1091 0, /* g_usage */
michael@0 1092 0, /* g_threads */
michael@0 1093 0, /* g_profile */
michael@0 1094
michael@0 1095 320, /* g_width */
michael@0 1096 240, /* g_height */
michael@0 1097 {1, 30}, /* g_timebase */
michael@0 1098
michael@0 1099 0, /* g_error_resilient */
michael@0 1100
michael@0 1101 VPX_RC_ONE_PASS, /* g_pass */
michael@0 1102
michael@0 1103 25, /* g_lag_in_frames */
michael@0 1104
michael@0 1105 0, /* rc_dropframe_thresh */
michael@0 1106 0, /* rc_resize_allowed */
michael@0 1107 60, /* rc_resize_down_thresold */
michael@0 1108 30, /* rc_resize_up_thresold */
michael@0 1109
michael@0 1110 VPX_VBR, /* rc_end_usage */
michael@0 1111 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
michael@0 1112 {0}, /* rc_twopass_stats_in */
michael@0 1113 #endif
michael@0 1114 256, /* rc_target_bandwidth */
michael@0 1115 0, /* rc_min_quantizer */
michael@0 1116 63, /* rc_max_quantizer */
michael@0 1117 100, /* rc_undershoot_pct */
michael@0 1118 100, /* rc_overshoot_pct */
michael@0 1119
michael@0 1120 6000, /* rc_max_buffer_size */
michael@0 1121 4000, /* rc_buffer_initial_size; */
michael@0 1122 5000, /* rc_buffer_optimal_size; */
michael@0 1123
michael@0 1124 50, /* rc_two_pass_vbrbias */
michael@0 1125 0, /* rc_two_pass_vbrmin_section */
michael@0 1126 2000, /* rc_two_pass_vbrmax_section */
michael@0 1127
michael@0 1128 /* keyframing settings (kf) */
michael@0 1129 VPX_KF_AUTO, /* g_kfmode*/
michael@0 1130 0, /* kf_min_dist */
michael@0 1131 9999, /* kf_max_dist */
michael@0 1132
michael@0 1133 VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */
michael@0 1134
michael@0 1135 #if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION)
michael@0 1136 1, /* g_delete_first_pass_file */
michael@0 1137 "vp8.fpf" /* first pass filename */
michael@0 1138 #endif
michael@0 1139 }
michael@0 1140 },
michael@0 1141 { -1, {NOT_IMPLEMENTED}}
michael@0 1142 };
michael@0 1143
michael@0 1144
michael@0 1145 #ifndef VERSION_STRING
michael@0 1146 #define VERSION_STRING
michael@0 1147 #endif
michael@0 1148 CODEC_INTERFACE(vpx_codec_vp9_cx) = {
michael@0 1149 "WebM Project VP9 Encoder" VERSION_STRING,
michael@0 1150 VPX_CODEC_INTERNAL_ABI_VERSION,
michael@0 1151 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR |
michael@0 1152 VPX_CODEC_CAP_OUTPUT_PARTITION,
michael@0 1153 /* vpx_codec_caps_t caps; */
michael@0 1154 vp9e_init, /* vpx_codec_init_fn_t init; */
michael@0 1155 vp9e_destroy, /* vpx_codec_destroy_fn_t destroy; */
michael@0 1156 vp9e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
michael@0 1157 NOT_IMPLEMENTED, /* vpx_codec_get_mmap_fn_t get_mmap; */
michael@0 1158 NOT_IMPLEMENTED, /* vpx_codec_set_mmap_fn_t set_mmap; */
michael@0 1159 { // NOLINT
michael@0 1160 NOT_IMPLEMENTED, /* vpx_codec_peek_si_fn_t peek_si; */
michael@0 1161 NOT_IMPLEMENTED, /* vpx_codec_get_si_fn_t get_si; */
michael@0 1162 NOT_IMPLEMENTED, /* vpx_codec_decode_fn_t decode; */
michael@0 1163 NOT_IMPLEMENTED, /* vpx_codec_frame_get_fn_t frame_get; */
michael@0 1164 },
michael@0 1165 { // NOLINT
michael@0 1166 vp9e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t peek_si; */
michael@0 1167 vp9e_encode, /* vpx_codec_encode_fn_t encode; */
michael@0 1168 vp9e_get_cxdata, /* vpx_codec_get_cx_data_fn_t frame_get; */
michael@0 1169 vp9e_set_config,
michael@0 1170 NOT_IMPLEMENTED,
michael@0 1171 vp9e_get_preview,
michael@0 1172 } /* encoder functions */
michael@0 1173 };
michael@0 1174
michael@0 1175
michael@0 1176 #if CONFIG_EXPERIMENTAL
michael@0 1177
michael@0 1178 CODEC_INTERFACE(vpx_codec_vp9x_cx) = {
michael@0 1179 "VP8 Experimental Encoder" VERSION_STRING,
michael@0 1180 VPX_CODEC_INTERNAL_ABI_VERSION,
michael@0 1181 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR,
michael@0 1182 /* vpx_codec_caps_t caps; */
michael@0 1183 vp9e_exp_init, /* vpx_codec_init_fn_t init; */
michael@0 1184 vp9e_destroy, /* vpx_codec_destroy_fn_t destroy; */
michael@0 1185 vp9e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
michael@0 1186 NOT_IMPLEMENTED, /* vpx_codec_get_mmap_fn_t get_mmap; */
michael@0 1187 NOT_IMPLEMENTED, /* vpx_codec_set_mmap_fn_t set_mmap; */
michael@0 1188 { // NOLINT
michael@0 1189 NOT_IMPLEMENTED, /* vpx_codec_peek_si_fn_t peek_si; */
michael@0 1190 NOT_IMPLEMENTED, /* vpx_codec_get_si_fn_t get_si; */
michael@0 1191 NOT_IMPLEMENTED, /* vpx_codec_decode_fn_t decode; */
michael@0 1192 NOT_IMPLEMENTED, /* vpx_codec_frame_get_fn_t frame_get; */
michael@0 1193 },
michael@0 1194 { // NOLINT
michael@0 1195 vp9e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t peek_si; */
michael@0 1196 vp9e_encode, /* vpx_codec_encode_fn_t encode; */
michael@0 1197 vp9e_get_cxdata, /* vpx_codec_get_cx_data_fn_t frame_get; */
michael@0 1198 vp9e_set_config,
michael@0 1199 NOT_IMPLEMENTED,
michael@0 1200 vp9e_get_preview,
michael@0 1201 } /* encoder functions */
michael@0 1202 };
michael@0 1203 #endif

mercurial