blob: 3426f598640e3e493875e20f3573f91e0ecdaec9 [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar94c52e42010-06-18 12:39:21 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
John Koleszar94c52e42010-06-18 12:39:21 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
10
11
Minghai Shange8998592014-07-14 11:24:17 -070012#include "./vpx_config.h"
John Koleszara9c75972012-11-08 17:09:30 -080013#include "vp8_rtcd.h"
John Koleszarb7492342010-05-24 11:39:59 -040014#include "vpx/vpx_codec.h"
15#include "vpx/internal/vpx_codec_internal.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040016#include "vpx_version.h"
Dmitry Kovalevb08fab82014-09-02 18:26:53 -070017#include "vpx_mem/vpx_mem.h"
John Koleszar02321de2011-02-10 14:41:38 -050018#include "vp8/encoder/onyx_int.h"
John Koleszar2bf8fb52012-05-02 16:37:37 -070019#include "vpx/vp8cx.h"
John Koleszarbb7dd5b2010-10-14 16:40:12 -040020#include "vp8/encoder/firstpass.h"
John Koleszar02321de2011-02-10 14:41:38 -050021#include "vp8/common/onyx.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040022#include <stdlib.h>
23#include <string.h>
24
John Koleszar0ea50ce2010-05-18 11:58:33 -040025struct vp8_extracfg
26{
27 struct vpx_codec_pkt_list *pkt_list;
John Koleszar0ea50ce2010-05-18 11:58:33 -040028 int cpu_used; /** available cpu percentage in 1/16*/
29 unsigned int enable_auto_alt_ref; /** if encoder decides to uses alternate reference frame */
30 unsigned int noise_sensitivity;
31 unsigned int Sharpness;
32 unsigned int static_thresh;
33 unsigned int token_partitions;
34 unsigned int arnr_max_frames; /* alt_ref Noise Reduction Max Frame Count */
35 unsigned int arnr_strength; /* alt_ref Noise Reduction Strength */
36 unsigned int arnr_type; /* alt_ref filter type */
John Koleszarb0da9b32010-12-17 09:43:39 -050037 vp8e_tuning tuning;
Paul Wilkinse0846c92011-01-07 18:29:37 +000038 unsigned int cq_level; /* constrained quality level */
John Koleszar1654ae92011-07-28 09:17:32 -040039 unsigned int rc_max_intra_bitrate_pct;
Marcoaf898b52014-11-10 13:07:05 -080040 unsigned int screen_content_mode;
John Koleszar0ea50ce2010-05-18 11:58:33 -040041
42};
43
Dmitry Kovalev48274c62014-08-21 11:21:18 -070044static struct vp8_extracfg default_extracfg = {
45 NULL,
John Koleszar0ea50ce2010-05-18 11:58:33 -040046#if !(CONFIG_REALTIME_ONLY)
Dmitry Kovalev48274c62014-08-21 11:21:18 -070047 0, /* cpu_used */
John Koleszar0ea50ce2010-05-18 11:58:33 -040048#else
Dmitry Kovalev48274c62014-08-21 11:21:18 -070049 4, /* cpu_used */
John Koleszar0ea50ce2010-05-18 11:58:33 -040050#endif
Dmitry Kovalev48274c62014-08-21 11:21:18 -070051 0, /* enable_auto_alt_ref */
52 0, /* noise_sensitivity */
53 0, /* Sharpness */
54 0, /* static_thresh */
Attila Nagy52cf4dc2012-02-09 12:37:03 +020055#if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
Dmitry Kovalev48274c62014-08-21 11:21:18 -070056 VP8_EIGHT_TOKENPARTITION,
Attila Nagy52cf4dc2012-02-09 12:37:03 +020057#else
Dmitry Kovalev48274c62014-08-21 11:21:18 -070058 VP8_ONE_TOKENPARTITION, /* token_partitions */
Attila Nagy52cf4dc2012-02-09 12:37:03 +020059#endif
Dmitry Kovalev48274c62014-08-21 11:21:18 -070060 0, /* arnr_max_frames */
61 3, /* arnr_strength */
62 3, /* arnr_type*/
63 0, /* tuning*/
64 10, /* cq_level */
65 0, /* rc_max_intra_bitrate_pct */
Marcoaf898b52014-11-10 13:07:05 -080066 0, /* screen_content_mode */
John Koleszar0ea50ce2010-05-18 11:58:33 -040067};
68
69struct vpx_codec_alg_priv
70{
71 vpx_codec_priv_t base;
72 vpx_codec_enc_cfg_t cfg;
73 struct vp8_extracfg vp8_cfg;
74 VP8_CONFIG oxcf;
John Koleszarb0056c32011-12-20 16:54:54 -080075 struct VP8_COMP *cpi;
John Koleszar0ea50ce2010-05-18 11:58:33 -040076 unsigned char *cx_data;
77 unsigned int cx_data_sz;
78 vpx_image_t preview_img;
79 unsigned int next_frame_flag;
80 vp8_postproc_cfg_t preview_ppcfg;
John Koleszar0164a1c2012-05-21 14:30:56 -070081 /* pkt_list size depends on the maximum number of lagged frames allowed. */
82 vpx_codec_pkt_list_decl(64) pkt_list;
John Koleszar0ea50ce2010-05-18 11:58:33 -040083 unsigned int fixed_kf_cntr;
Marcoaf898b52014-11-10 13:07:05 -080084 vpx_enc_frame_flags_t control_frame_flags;
John Koleszar0ea50ce2010-05-18 11:58:33 -040085};
86
87
88static vpx_codec_err_t
89update_error_state(vpx_codec_alg_priv_t *ctx,
90 const struct vpx_internal_error_info *error)
91{
92 vpx_codec_err_t res;
93
94 if ((res = error->error_code))
95 ctx->base.err_detail = error->has_detail
96 ? error->detail
97 : NULL;
98
99 return res;
100}
101
102
John Koleszar79e2b1f2010-11-17 09:08:47 -0500103#undef ERROR
John Koleszar0ea50ce2010-05-18 11:58:33 -0400104#define ERROR(str) do {\
105 ctx->base.err_detail = str;\
106 return VPX_CODEC_INVALID_PARAM;\
107 } while(0)
108
109#define RANGE_CHECK(p,memb,lo,hi) do {\
Guillermo Ballester Valor5a726202010-06-11 14:33:49 -0400110 if(!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
John Koleszar0ea50ce2010-05-18 11:58:33 -0400111 ERROR(#memb " out of range ["#lo".."#hi"]");\
112 } while(0)
113
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400114#define RANGE_CHECK_HI(p,memb,hi) do {\
115 if(!((p)->memb <= (hi))) \
116 ERROR(#memb " out of range [.."#hi"]");\
117 } while(0)
118
John Koleszar0ea50ce2010-05-18 11:58:33 -0400119#define RANGE_CHECK_LO(p,memb,lo) do {\
120 if(!((p)->memb >= (lo))) \
121 ERROR(#memb " out of range ["#lo"..]");\
122 } while(0)
123
124#define RANGE_CHECK_BOOL(p,memb) do {\
125 if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
126 } while(0)
127
128static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
129 const vpx_codec_enc_cfg_t *cfg,
James Berryc1c47e82011-12-07 15:48:00 -0500130 const struct vp8_extracfg *vp8_cfg,
131 int finalize)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400132{
Attila Nagy1aadced2011-04-12 15:01:22 +0300133 RANGE_CHECK(cfg, g_w, 1, 16383); /* 14 bits available */
134 RANGE_CHECK(cfg, g_h, 1, 16383); /* 14 bits available */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400135 RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
136 RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400137 RANGE_CHECK_HI(cfg, g_profile, 3);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400138 RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
John Koleszar2d03f072011-01-28 11:56:18 -0500139 RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400140 RANGE_CHECK_HI(cfg, g_threads, 64);
Yunqing Wang7f009972012-07-11 11:43:51 -0700141#if CONFIG_REALTIME_ONLY
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400142 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
Yunqing Wang7f009972012-07-11 11:43:51 -0700143#elif CONFIG_MULTI_RES_ENCODING
144 if (ctx->base.enc.total_encoders > 1)
145 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
146#else
147 RANGE_CHECK_HI(cfg, g_lag_in_frames, 25);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400148#endif
Deb Mukherjeee378a892013-08-29 16:21:44 -0700149 RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
John Koleszarc99f9d72011-04-11 11:29:23 -0400150 RANGE_CHECK_HI(cfg, rc_undershoot_pct, 1000);
151 RANGE_CHECK_HI(cfg, rc_overshoot_pct, 1000);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400152 RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400153 RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
Yunqing Wang7f009972012-07-11 11:43:51 -0700154
155/* TODO: add spatial re-sampling support and frame dropping in
156 * multi-res-encoder.*/
157#if CONFIG_MULTI_RES_ENCODING
158 if (ctx->base.enc.total_encoders > 1)
Yunqing Wang7f009972012-07-11 11:43:51 -0700159 RANGE_CHECK_HI(cfg, rc_resize_allowed, 0);
Yunqing Wang7f009972012-07-11 11:43:51 -0700160#else
161 RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
Yunqing Wang7f009972012-07-11 11:43:51 -0700162#endif
Yunqing Wang4066c8b2012-06-08 11:17:50 -0400163 RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400164 RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
165 RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
Yunqing Wang7f009972012-07-11 11:43:51 -0700166
167#if CONFIG_REALTIME_ONLY
John Koleszar0ea50ce2010-05-18 11:58:33 -0400168 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
Yunqing Wang7f009972012-07-11 11:43:51 -0700169#elif CONFIG_MULTI_RES_ENCODING
170 if (ctx->base.enc.total_encoders > 1)
171 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
172#else
173 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400174#endif
175
176 /* VP8 does not support a lower bound on the keyframe interval in
177 * automatic keyframe placement mode.
178 */
179 if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist
180 && cfg->kf_min_dist > 0)
181 ERROR("kf_min_dist not supported in auto mode, use 0 "
182 "or kf_max_dist instead.");
183
184 RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref);
Yunqing Wang4fd81a92011-04-11 15:55:04 -0400185 RANGE_CHECK(vp8_cfg, cpu_used, -16, 16);
John Koleszarc8f4c182012-05-08 11:23:42 -0700186
187#if CONFIG_REALTIME_ONLY && !CONFIG_TEMPORAL_DENOISING
188 RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0);
189#else
190 RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6);
Stefan Holmer9c411432012-03-06 10:48:18 +0100191#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -0400192
Yunqing Wang7f009972012-07-11 11:43:51 -0700193 RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION,
194 VP8_EIGHT_TOKENPARTITION);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400195 RANGE_CHECK_HI(vp8_cfg, Sharpness, 7);
Adrian Grange8ee72842010-09-30 10:06:09 +0100196 RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
Frank Galligan15542722010-10-04 21:12:22 -0400197 RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6);
Adrian Grange8ee72842010-09-30 10:06:09 +0100198 RANGE_CHECK(vp8_cfg, arnr_type, 1, 3);
Paul Wilkinse0846c92011-01-07 18:29:37 +0000199 RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
Marcoaf898b52014-11-10 13:07:05 -0800200 RANGE_CHECK_BOOL(vp8_cfg, screen_content_mode);
Deb Mukherjeee378a892013-08-29 16:21:44 -0700201 if (finalize && (cfg->rc_end_usage == VPX_CQ || cfg->rc_end_usage == VPX_Q))
James Berryc1c47e82011-12-07 15:48:00 -0500202 RANGE_CHECK(vp8_cfg, cq_level,
203 cfg->rc_min_quantizer, cfg->rc_max_quantizer);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400204
Attila Nagycb791aa2011-01-10 11:14:10 +0200205#if !(CONFIG_REALTIME_ONLY)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400206 if (cfg->g_pass == VPX_RC_LAST_PASS)
207 {
Adrian Grangeed40ff92011-03-10 11:32:48 -0800208 size_t packet_sz = sizeof(FIRSTPASS_STATS);
Yaowu Xud71ba032012-08-17 10:05:35 -0700209 int n_packets = (int)(cfg->rc_twopass_stats_in.sz /
210 packet_sz);
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400211 FIRSTPASS_STATS *stats;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400212
213 if (!cfg->rc_twopass_stats_in.buf)
214 ERROR("rc_twopass_stats_in.buf not set.");
215
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400216 if (cfg->rc_twopass_stats_in.sz % packet_sz)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400217 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
218
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400219 if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400220 ERROR("rc_twopass_stats_in requires at least two packets.");
221
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400222 stats = (void*)((char *)cfg->rc_twopass_stats_in.buf
223 + (n_packets - 1) * packet_sz);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400224
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400225 if ((int)(stats->count + 0.5) != n_packets - 1)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400226 ERROR("rc_twopass_stats_in missing EOS stats packet");
227 }
Attila Nagycb791aa2011-01-10 11:14:10 +0200228#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -0400229
Adrian Grange217591f2011-10-06 15:49:11 -0700230 RANGE_CHECK(cfg, ts_number_layers, 1, 5);
231
232 if (cfg->ts_number_layers > 1)
233 {
Scott Graham92963df2012-05-08 11:45:35 -0700234 unsigned int i;
Adrian Grange217591f2011-10-06 15:49:11 -0700235 RANGE_CHECK_HI(cfg, ts_periodicity, 16);
236
237 for (i=1; i<cfg->ts_number_layers; i++)
Marcoaf898b52014-11-10 13:07:05 -0800238 if (cfg->ts_target_bitrate[i] <= cfg->ts_target_bitrate[i-1] &&
239 cfg->rc_target_bitrate > 0)
Adrian Grange217591f2011-10-06 15:49:11 -0700240 ERROR("ts_target_bitrate entries are not strictly increasing");
241
242 RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers-1], 1, 1);
243 for (i=cfg->ts_number_layers-2; i>0; i--)
244 if (cfg->ts_rate_decimator[i-1] != 2*cfg->ts_rate_decimator[i])
245 ERROR("ts_rate_decimator factors are not powers of 2");
246
247 RANGE_CHECK_HI(cfg, ts_layer_id[i], cfg->ts_number_layers-1);
248 }
249
Attila Nagy52cf4dc2012-02-09 12:37:03 +0200250#if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
251 if(cfg->g_threads > (1 << vp8_cfg->token_partitions))
252 ERROR("g_threads cannot be bigger than number of token partitions");
253#endif
254
John Koleszar0ea50ce2010-05-18 11:58:33 -0400255 return VPX_CODEC_OK;
256}
257
258
259static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
260 const vpx_image_t *img)
261{
262 switch (img->fmt)
263 {
James Zern6cd4a102010-05-20 23:22:39 -0400264 case VPX_IMG_FMT_YV12:
265 case VPX_IMG_FMT_I420:
266 case VPX_IMG_FMT_VPXI420:
267 case VPX_IMG_FMT_VPXYV12:
John Koleszar0ea50ce2010-05-18 11:58:33 -0400268 break;
269 default:
270 ERROR("Invalid image format. Only YV12 and I420 images are supported");
271 }
272
273 if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
274 ERROR("Image size must match encoder init configuration size");
275
276 return VPX_CODEC_OK;
277}
278
279
280static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf,
281 vpx_codec_enc_cfg_t cfg,
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400282 struct vp8_extracfg vp8_cfg,
283 vpx_codec_priv_enc_mr_cfg_t *mr_cfg)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400284{
285 oxcf->multi_threaded = cfg.g_threads;
286 oxcf->Version = cfg.g_profile;
287
288 oxcf->Width = cfg.g_w;
289 oxcf->Height = cfg.g_h;
John Koleszarbdd35c12011-11-11 10:47:20 -0800290 oxcf->timebase = cfg.g_timebase;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400291
Adrian Grange217591f2011-10-06 15:49:11 -0700292 oxcf->error_resilient_mode = cfg.g_error_resilient;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400293
294 switch (cfg.g_pass)
295 {
296 case VPX_RC_ONE_PASS:
297 oxcf->Mode = MODE_BESTQUALITY;
298 break;
299 case VPX_RC_FIRST_PASS:
300 oxcf->Mode = MODE_FIRSTPASS;
301 break;
302 case VPX_RC_LAST_PASS:
303 oxcf->Mode = MODE_SECONDPASS_BEST;
304 break;
305 }
306
James Zern7b0b6a22012-06-13 11:59:12 -0700307 if (cfg.g_pass == VPX_RC_FIRST_PASS || cfg.g_pass == VPX_RC_ONE_PASS)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400308 {
Adrian Grange217591f2011-10-06 15:49:11 -0700309 oxcf->allow_lag = 0;
310 oxcf->lag_in_frames = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400311 }
312 else
313 {
Adrian Grange217591f2011-10-06 15:49:11 -0700314 oxcf->allow_lag = (cfg.g_lag_in_frames) > 0;
315 oxcf->lag_in_frames = cfg.g_lag_in_frames;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400316 }
317
318 oxcf->allow_df = (cfg.rc_dropframe_thresh > 0);
319 oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh;
320
321 oxcf->allow_spatial_resampling = cfg.rc_resize_allowed;
322 oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh;
323 oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh;
324
Deb Mukherjeee378a892013-08-29 16:21:44 -0700325 if (cfg.rc_end_usage == VPX_VBR) {
326 oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
327 } else if (cfg.rc_end_usage == VPX_CBR) {
328 oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
329 } else if (cfg.rc_end_usage == VPX_CQ) {
330 oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
331 } else if (cfg.rc_end_usage == VPX_Q) {
332 oxcf->end_usage = USAGE_CONSTANT_QUALITY;
Paul Wilkinse0846c92011-01-07 18:29:37 +0000333 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400334
Adrian Grange217591f2011-10-06 15:49:11 -0700335 oxcf->target_bandwidth = cfg.rc_target_bitrate;
John Koleszar1654ae92011-07-28 09:17:32 -0400336 oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400337
Adrian Grange217591f2011-10-06 15:49:11 -0700338 oxcf->best_allowed_q = cfg.rc_min_quantizer;
339 oxcf->worst_allowed_q = cfg.rc_max_quantizer;
340 oxcf->cq_level = vp8_cfg.cq_level;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400341 oxcf->fixed_q = -1;
342
Adrian Grange217591f2011-10-06 15:49:11 -0700343 oxcf->under_shoot_pct = cfg.rc_undershoot_pct;
344 oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400345
Adrian Grangee4793792012-01-13 14:09:40 -0800346 oxcf->maximum_buffer_size_in_ms = cfg.rc_buf_sz;
347 oxcf->starting_buffer_level_in_ms = cfg.rc_buf_initial_sz;
348 oxcf->optimal_buffer_level_in_ms = cfg.rc_buf_optimal_sz;
349
Adrian Grange217591f2011-10-06 15:49:11 -0700350 oxcf->maximum_buffer_size = cfg.rc_buf_sz;
351 oxcf->starting_buffer_level = cfg.rc_buf_initial_sz;
352 oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400353
Adrian Grange217591f2011-10-06 15:49:11 -0700354 oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400355 oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct;
356 oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct;
357
Adrian Grange217591f2011-10-06 15:49:11 -0700358 oxcf->auto_key = cfg.kf_mode == VPX_KF_AUTO
359 && cfg.kf_min_dist != cfg.kf_max_dist;
Adrian Grange217591f2011-10-06 15:49:11 -0700360 oxcf->key_freq = cfg.kf_max_dist;
361
362 oxcf->number_of_layers = cfg.ts_number_layers;
363 oxcf->periodicity = cfg.ts_periodicity;
364
365 if (oxcf->number_of_layers > 1)
366 {
367 memcpy (oxcf->target_bitrate, cfg.ts_target_bitrate,
368 sizeof(cfg.ts_target_bitrate));
369 memcpy (oxcf->rate_decimator, cfg.ts_rate_decimator,
370 sizeof(cfg.ts_rate_decimator));
371 memcpy (oxcf->layer_id, cfg.ts_layer_id, sizeof(cfg.ts_layer_id));
372 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400373
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400374#if CONFIG_MULTI_RES_ENCODING
Yunqing Wangc647ec42011-12-16 16:50:29 -0500375 /* When mr_cfg is NULL, oxcf->mr_total_resolutions and oxcf->mr_encoder_id
376 * are both memset to 0, which ensures the correct logic under this
377 * situation.
378 */
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400379 if(mr_cfg)
380 {
381 oxcf->mr_total_resolutions = mr_cfg->mr_total_resolutions;
382 oxcf->mr_encoder_id = mr_cfg->mr_encoder_id;
383 oxcf->mr_down_sampling_factor.num = mr_cfg->mr_down_sampling_factor.num;
384 oxcf->mr_down_sampling_factor.den = mr_cfg->mr_down_sampling_factor.den;
385 oxcf->mr_low_res_mode_info = mr_cfg->mr_low_res_mode_info;
386 }
387#endif
388
Adrian Grange217591f2011-10-06 15:49:11 -0700389 oxcf->cpu_used = vp8_cfg.cpu_used;
390 oxcf->encode_breakout = vp8_cfg.static_thresh;
391 oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref;
392 oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity;
393 oxcf->Sharpness = vp8_cfg.Sharpness;
394 oxcf->token_partitions = vp8_cfg.token_partitions;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400395
Adrian Grange217591f2011-10-06 15:49:11 -0700396 oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in;
397 oxcf->output_pkt_list = vp8_cfg.pkt_list;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400398
Adrian Grange217591f2011-10-06 15:49:11 -0700399 oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
400 oxcf->arnr_strength = vp8_cfg.arnr_strength;
401 oxcf->arnr_type = vp8_cfg.arnr_type;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400402
Adrian Grange217591f2011-10-06 15:49:11 -0700403 oxcf->tuning = vp8_cfg.tuning;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400404
Marcoaf898b52014-11-10 13:07:05 -0800405 oxcf->screen_content_mode = vp8_cfg.screen_content_mode;
406
John Koleszar0ea50ce2010-05-18 11:58:33 -0400407 /*
408 printf("Current VP8 Settings: \n");
409 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
410 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
411 printf("Sharpness: %d\n", oxcf->Sharpness);
412 printf("cpu_used: %d\n", oxcf->cpu_used);
413 printf("Mode: %d\n", oxcf->Mode);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400414 printf("auto_key: %d\n", oxcf->auto_key);
415 printf("key_freq: %d\n", oxcf->key_freq);
416 printf("end_usage: %d\n", oxcf->end_usage);
417 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
John Koleszarc99f9d72011-04-11 11:29:23 -0400418 printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400419 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
420 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
421 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
422 printf("fixed_q: %d\n", oxcf->fixed_q);
423 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
424 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
425 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
426 printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
427 printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
428 printf("allow_df: %d\n", oxcf->allow_df);
429 printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
430 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
431 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
432 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
433 printf("allow_lag: %d\n", oxcf->allow_lag);
434 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
435 printf("play_alternate: %d\n", oxcf->play_alternate);
436 printf("Version: %d\n", oxcf->Version);
437 printf("multi_threaded: %d\n", oxcf->multi_threaded);
438 printf("encode_breakout: %d\n", oxcf->encode_breakout);
439 */
440 return VPX_CODEC_OK;
441}
442
443static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx,
444 const vpx_codec_enc_cfg_t *cfg)
445{
446 vpx_codec_err_t res;
447
John Koleszar51acb012012-02-07 17:07:49 -0800448 if (((cfg->g_w != ctx->cfg.g_w) || (cfg->g_h != ctx->cfg.g_h))
John Koleszar410ae572012-05-23 12:07:53 -0700449 && (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS))
John Koleszar0ea50ce2010-05-18 11:58:33 -0400450 ERROR("Cannot change width or height after initialization");
451
452 /* Prevent increasing lag_in_frames. This check is stricter than it needs
453 * to be -- the limit is not increasing past the first lag_in_frames
454 * value, but we don't track the initial config, only the last successful
455 * config.
456 */
457 if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
458 ERROR("Cannot increase lag_in_frames");
459
James Berryc1c47e82011-12-07 15:48:00 -0500460 res = validate_config(ctx, cfg, &ctx->vp8_cfg, 0);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400461
462 if (!res)
463 {
464 ctx->cfg = *cfg;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400465 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400466 vp8_change_config(ctx->cpi, &ctx->oxcf);
467 }
468
469 return res;
470}
471
John Koleszar0ea50ce2010-05-18 11:58:33 -0400472int vp8_reverse_trans(int);
473
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700474static vpx_codec_err_t get_quantizer(vpx_codec_alg_priv_t *ctx, va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400475{
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700476 int *const arg = va_arg(args, int *);
477 if (arg == NULL)
478 return VPX_CODEC_INVALID_PARAM;
479 *arg = vp8_get_quantizer(ctx->cpi);
480 return VPX_CODEC_OK;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400481}
482
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700483static vpx_codec_err_t get_quantizer64(vpx_codec_alg_priv_t *ctx, va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400484{
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700485 int *const arg = va_arg(args, int *);
486 if (arg == NULL)
487 return VPX_CODEC_INVALID_PARAM;
488 *arg = vp8_reverse_trans(vp8_get_quantizer(ctx->cpi));
489 return VPX_CODEC_OK;
490}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400491
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700492static vpx_codec_err_t update_extracfg(vpx_codec_alg_priv_t *ctx,
493 const struct vp8_extracfg *extra_cfg)
494{
495 const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg, 0);
496 if (res == VPX_CODEC_OK) {
497 ctx->vp8_cfg = *extra_cfg;
498 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
499 vp8_change_config(ctx->cpi, &ctx->oxcf);
500 }
501 return res;
502}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400503
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700504static vpx_codec_err_t set_cpu_used(vpx_codec_alg_priv_t *ctx, va_list args)
505{
506 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
507 extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
508 return update_extracfg(ctx, &extra_cfg);
509}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400510
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700511static vpx_codec_err_t set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
512 va_list args)
513{
514 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
515 extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args);
516 return update_extracfg(ctx, &extra_cfg);
517}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400518
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700519static vpx_codec_err_t set_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
520 va_list args)
521{
522 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
523 extra_cfg.noise_sensitivity = CAST(VP8E_SET_NOISE_SENSITIVITY, args);
524 return update_extracfg(ctx, &extra_cfg);
525}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400526
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700527static vpx_codec_err_t set_sharpness(vpx_codec_alg_priv_t *ctx, va_list args)
528{
529 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
530 extra_cfg.Sharpness = CAST(VP8E_SET_SHARPNESS, args);
531 return update_extracfg(ctx, &extra_cfg);
532}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400533
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700534static vpx_codec_err_t set_static_thresh(vpx_codec_alg_priv_t *ctx,
535 va_list args)
536{
537 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
538 extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
539 return update_extracfg(ctx, &extra_cfg);
540}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400541
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700542static vpx_codec_err_t set_token_partitions(vpx_codec_alg_priv_t *ctx,
543 va_list args)
544{
545 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
546 extra_cfg.token_partitions = CAST(VP8E_SET_TOKEN_PARTITIONS, args);
547 return update_extracfg(ctx, &extra_cfg);
548}
549
550static vpx_codec_err_t set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
551 va_list args)
552{
553 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
554 extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
555 return update_extracfg(ctx, &extra_cfg);
556}
557
558static vpx_codec_err_t set_arnr_strength(vpx_codec_alg_priv_t *ctx,
559 va_list args)
560{
561 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
562 extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
563 return update_extracfg(ctx, &extra_cfg);
564}
565
566static vpx_codec_err_t set_arnr_type(vpx_codec_alg_priv_t *ctx, va_list args)
567{
568 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
569 extra_cfg.arnr_type = CAST(VP8E_SET_ARNR_TYPE, args);
570 return update_extracfg(ctx, &extra_cfg);
571}
572
573static vpx_codec_err_t set_tuning(vpx_codec_alg_priv_t *ctx, va_list args)
574{
575 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
576 extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
577 return update_extracfg(ctx, &extra_cfg);
578}
579
580static vpx_codec_err_t set_cq_level(vpx_codec_alg_priv_t *ctx, va_list args)
581{
582 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
583 extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
584 return update_extracfg(ctx, &extra_cfg);
585}
586
587static vpx_codec_err_t set_rc_max_intra_bitrate_pct(vpx_codec_alg_priv_t *ctx,
588 va_list args)
589{
590 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
591 extra_cfg.rc_max_intra_bitrate_pct =
592 CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
593 return update_extracfg(ctx, &extra_cfg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400594}
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400595
Marcoaf898b52014-11-10 13:07:05 -0800596static vpx_codec_err_t set_screen_content_mode(vpx_codec_alg_priv_t *ctx,
597 va_list args)
598{
599 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
600 extra_cfg.screen_content_mode =
601 CAST(VP8E_SET_SCREEN_CONTENT_MODE, args);
602 return update_extracfg(ctx, &extra_cfg);
603}
604
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400605static vpx_codec_err_t vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t *cfg,
606 void **mem_loc)
607{
Yunqing Wangad479a92012-05-23 13:40:24 -0400608 vpx_codec_err_t res = 0;
609
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400610#if CONFIG_MULTI_RES_ENCODING
Yunqing Wang65dd1572012-05-16 15:06:42 -0400611 LOWER_RES_FRAME_INFO *shared_mem_loc;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400612 int mb_rows = ((cfg->g_w + 15) >>4);
613 int mb_cols = ((cfg->g_h + 15) >>4);
614
Yunqing Wang65dd1572012-05-16 15:06:42 -0400615 shared_mem_loc = calloc(1, sizeof(LOWER_RES_FRAME_INFO));
616 if(!shared_mem_loc)
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400617 {
Yunqing Wangad479a92012-05-23 13:40:24 -0400618 res = VPX_CODEC_MEM_ERROR;
Yunqing Wang65dd1572012-05-16 15:06:42 -0400619 }
620
621 shared_mem_loc->mb_info = calloc(mb_rows*mb_cols, sizeof(LOWER_RES_MB_INFO));
622 if(!(shared_mem_loc->mb_info))
623 {
Yunqing Wangad479a92012-05-23 13:40:24 -0400624 res = VPX_CODEC_MEM_ERROR;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400625 }
626 else
Yunqing Wang65dd1572012-05-16 15:06:42 -0400627 {
628 *mem_loc = (void *)shared_mem_loc;
Yunqing Wangad479a92012-05-23 13:40:24 -0400629 res = VPX_CODEC_OK;
Yunqing Wang65dd1572012-05-16 15:06:42 -0400630 }
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400631#endif
Yunqing Wangad479a92012-05-23 13:40:24 -0400632 return res;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400633}
634
635static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx,
636 vpx_codec_priv_enc_mr_cfg_t *mr_cfg)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400637{
John Koleszar2bf8fb52012-05-02 16:37:37 -0700638 vpx_codec_err_t res = VPX_CODEC_OK;
Dmitry Kovalevb08fab82014-09-02 18:26:53 -0700639
John Koleszar0ea50ce2010-05-18 11:58:33 -0400640
John Koleszara9c75972012-11-08 17:09:30 -0800641 vp8_rtcd();
John Koleszar8df79e92012-06-15 15:40:13 -0700642
John Koleszar0ea50ce2010-05-18 11:58:33 -0400643 if (!ctx->priv)
644 {
Dmitry Kovalevb08fab82014-09-02 18:26:53 -0700645 struct vpx_codec_alg_priv *priv =
646 (struct vpx_codec_alg_priv *)vpx_calloc(1, sizeof(*priv));
John Koleszar0ea50ce2010-05-18 11:58:33 -0400647
Attila Nagye6db21e2011-02-22 15:02:05 +0200648 if (!priv)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400649 {
Attila Nagye6db21e2011-02-22 15:02:05 +0200650 return VPX_CODEC_MEM_ERROR;
651 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400652
Dmitry Kovalev73edeb02014-08-20 17:02:10 -0700653 ctx->priv = (vpx_codec_priv_t *)priv;
Attila Nagye6db21e2011-02-22 15:02:05 +0200654 ctx->priv->init_flags = ctx->init_flags;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400655
Attila Nagye6db21e2011-02-22 15:02:05 +0200656 if (ctx->config.enc)
657 {
658 /* Update the reference to the config structure to an
659 * internal copy.
John Koleszar0ea50ce2010-05-18 11:58:33 -0400660 */
Dmitry Kovalev73edeb02014-08-20 17:02:10 -0700661 priv->cfg = *ctx->config.enc;
662 ctx->config.enc = &priv->cfg;
Attila Nagye6db21e2011-02-22 15:02:05 +0200663 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400664
Dmitry Kovalev48274c62014-08-21 11:21:18 -0700665 priv->vp8_cfg = default_extracfg;
Attila Nagye6db21e2011-02-22 15:02:05 +0200666 priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400667
Attila Nagye6db21e2011-02-22 15:02:05 +0200668 priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400669
James Berrybac6c222011-10-24 11:50:27 -0400670 if (priv->cx_data_sz < 32768) priv->cx_data_sz = 32768;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400671
Attila Nagye6db21e2011-02-22 15:02:05 +0200672 priv->cx_data = malloc(priv->cx_data_sz);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400673
Attila Nagye6db21e2011-02-22 15:02:05 +0200674 if (!priv->cx_data)
675 {
676 return VPX_CODEC_MEM_ERROR;
677 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400678
Yunqing Wang7f009972012-07-11 11:43:51 -0700679 if(mr_cfg)
680 ctx->priv->enc.total_encoders = mr_cfg->mr_total_resolutions;
681 else
682 ctx->priv->enc.total_encoders = 1;
683
James Berryc1c47e82011-12-07 15:48:00 -0500684 res = validate_config(priv, &priv->cfg, &priv->vp8_cfg, 0);
Attila Nagye6db21e2011-02-22 15:02:05 +0200685
686 if (!res)
687 {
Dmitry Kovalev73edeb02014-08-20 17:02:10 -0700688 set_vp8e_config(&priv->oxcf, priv->cfg, priv->vp8_cfg, mr_cfg);
689 priv->cpi = vp8_create_compressor(&priv->oxcf);
690 if (!priv->cpi)
Attila Nagye6db21e2011-02-22 15:02:05 +0200691 res = VPX_CODEC_MEM_ERROR;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400692 }
693 }
694
695 return res;
696}
697
698static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx)
699{
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400700#if CONFIG_MULTI_RES_ENCODING
701 /* Free multi-encoder shared memory */
702 if (ctx->oxcf.mr_total_resolutions > 0 && (ctx->oxcf.mr_encoder_id == ctx->oxcf.mr_total_resolutions-1))
Yunqing Wang65dd1572012-05-16 15:06:42 -0400703 {
704 LOWER_RES_FRAME_INFO *shared_mem_loc = (LOWER_RES_FRAME_INFO *)ctx->oxcf.mr_low_res_mode_info;
705 free(shared_mem_loc->mb_info);
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400706 free(ctx->oxcf.mr_low_res_mode_info);
Yunqing Wang65dd1572012-05-16 15:06:42 -0400707 }
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400708#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -0400709
710 free(ctx->cx_data);
711 vp8_remove_compressor(&ctx->cpi);
Dmitry Kovalevb08fab82014-09-02 18:26:53 -0700712 vpx_free(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400713 return VPX_CODEC_OK;
714}
715
716static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
717 YV12_BUFFER_CONFIG *yv12)
718{
Johann69dc8762014-08-15 10:50:40 -0700719 const int y_w = img->d_w;
720 const int y_h = img->d_h;
721 const int uv_w = (img->d_w + 1) / 2;
722 const int uv_h = (img->d_h + 1) / 2;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400723 vpx_codec_err_t res = VPX_CODEC_OK;
John Koleszarb6c71912010-05-24 21:45:05 -0400724 yv12->y_buffer = img->planes[VPX_PLANE_Y];
725 yv12->u_buffer = img->planes[VPX_PLANE_U];
726 yv12->v_buffer = img->planes[VPX_PLANE_V];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400727
Johann69dc8762014-08-15 10:50:40 -0700728 yv12->y_crop_width = y_w;
729 yv12->y_crop_height = y_h;
730 yv12->y_width = y_w;
731 yv12->y_height = y_h;
732 yv12->uv_crop_width = uv_w;
733 yv12->uv_crop_height = uv_h;
734 yv12->uv_width = uv_w;
735 yv12->uv_height = uv_h;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400736
John Koleszarb6c71912010-05-24 21:45:05 -0400737 yv12->y_stride = img->stride[VPX_PLANE_Y];
738 yv12->uv_stride = img->stride[VPX_PLANE_U];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400739
John Koleszarb6c71912010-05-24 21:45:05 -0400740 yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400741 return res;
742}
743
744static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
745 unsigned long duration,
746 unsigned long deadline)
747{
748 unsigned int new_qc;
749
750#if !(CONFIG_REALTIME_ONLY)
751 /* Use best quality mode if no deadline is given. */
752 new_qc = MODE_BESTQUALITY;
753
754 if (deadline)
755 {
756 uint64_t duration_us;
757
758 /* Convert duration parameter from stream timebase to microseconds */
759 duration_us = (uint64_t)duration * 1000000
760 * (uint64_t)ctx->cfg.g_timebase.num
761 / (uint64_t)ctx->cfg.g_timebase.den;
762
763 /* If the deadline is more that the duration this frame is to be shown,
764 * use good quality mode. Otherwise use realtime mode.
765 */
766 new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
767 }
768
769#else
770 new_qc = MODE_REALTIME;
771#endif
772
John Koleszar0ea50ce2010-05-18 11:58:33 -0400773 if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS)
774 new_qc = MODE_FIRSTPASS;
775 else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS)
776 new_qc = (new_qc == MODE_BESTQUALITY)
777 ? MODE_SECONDPASS_BEST
778 : MODE_SECONDPASS;
779
780 if (ctx->oxcf.Mode != new_qc)
781 {
782 ctx->oxcf.Mode = new_qc;
783 vp8_change_config(ctx->cpi, &ctx->oxcf);
784 }
785}
786
Marcoaf898b52014-11-10 13:07:05 -0800787static vpx_codec_err_t set_reference_and_update(vpx_codec_alg_priv_t *ctx,
788 int flags)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400789{
John Koleszar0ea50ce2010-05-18 11:58:33 -0400790
791 /* Handle Flags */
792 if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF))
793 || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF)))
794 {
795 ctx->base.err_detail = "Conflicting flags.";
796 return VPX_CODEC_INVALID_PARAM;
797 }
798
799 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF
800 | VP8_EFLAG_NO_REF_ARF))
801 {
802 int ref = 7;
803
804 if (flags & VP8_EFLAG_NO_REF_LAST)
Johann965d8682012-05-23 16:08:37 -0700805 ref ^= VP8_LAST_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400806
807 if (flags & VP8_EFLAG_NO_REF_GF)
Johann965d8682012-05-23 16:08:37 -0700808 ref ^= VP8_GOLD_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400809
810 if (flags & VP8_EFLAG_NO_REF_ARF)
Johann965d8682012-05-23 16:08:37 -0700811 ref ^= VP8_ALTR_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400812
813 vp8_use_as_reference(ctx->cpi, ref);
814 }
815
816 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF
817 | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF
818 | VP8_EFLAG_FORCE_ARF))
819 {
820 int upd = 7;
821
822 if (flags & VP8_EFLAG_NO_UPD_LAST)
Johann965d8682012-05-23 16:08:37 -0700823 upd ^= VP8_LAST_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400824
825 if (flags & VP8_EFLAG_NO_UPD_GF)
Johann965d8682012-05-23 16:08:37 -0700826 upd ^= VP8_GOLD_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400827
828 if (flags & VP8_EFLAG_NO_UPD_ARF)
Johann965d8682012-05-23 16:08:37 -0700829 upd ^= VP8_ALTR_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400830
831 vp8_update_reference(ctx->cpi, upd);
832 }
833
834 if (flags & VP8_EFLAG_NO_UPD_ENTROPY)
835 {
836 vp8_update_entropy(ctx->cpi, 0);
837 }
838
Marcoaf898b52014-11-10 13:07:05 -0800839 return VPX_CODEC_OK;
840}
841
842static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx,
843 const vpx_image_t *img,
844 vpx_codec_pts_t pts,
845 unsigned long duration,
846 vpx_enc_frame_flags_t flags,
847 unsigned long deadline)
848{
849 vpx_codec_err_t res = VPX_CODEC_OK;
850
851 if (!ctx->cfg.rc_target_bitrate)
852 return res;
853
854 if (!ctx->cfg.rc_target_bitrate)
855 return res;
856
857 if (img)
858 res = validate_img(ctx, img);
859
860 if (!res)
861 res = validate_config(ctx, &ctx->cfg, &ctx->vp8_cfg, 1);
862
863 pick_quickcompress_mode(ctx, duration, deadline);
864 vpx_codec_pkt_list_init(&ctx->pkt_list);
865
866 // If no flags are set in the encode call, then use the frame flags as
867 // defined via the control function: vp8e_set_frame_flags.
868 if (!flags) {
869 flags = ctx->control_frame_flags;
870 }
871 ctx->control_frame_flags = 0;
872
873 res = set_reference_and_update(ctx, flags);
874
John Koleszar0ea50ce2010-05-18 11:58:33 -0400875 /* Handle fixed keyframe intervals */
876 if (ctx->cfg.kf_mode == VPX_KF_AUTO
877 && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist)
878 {
879 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist)
880 {
881 flags |= VPX_EFLAG_FORCE_KF;
Andoni Morales Alastruey48140162011-02-07 18:04:02 +0100882 ctx->fixed_kf_cntr = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400883 }
884 }
885
886 /* Initialize the encoder instance on the first frame*/
887 if (!res && ctx->cpi)
888 {
889 unsigned int lib_flags;
890 YV12_BUFFER_CONFIG sd;
James Zernb45065d2011-07-25 18:44:59 -0700891 int64_t dst_time_stamp, dst_end_time_stamp;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400892 unsigned long size, cx_data_sz;
893 unsigned char *cx_data;
James Berrybc715112011-10-12 11:18:50 -0400894 unsigned char *cx_data_end;
895 int comp_data_state = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400896
897 /* Set up internal flags */
898 if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
899 ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1;
900
Stefan Holmer7296b3f2011-06-13 16:42:27 +0200901 if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION)
902 ((VP8_COMP *)ctx->cpi)->output_partition = 1;
903
John Koleszar0ea50ce2010-05-18 11:58:33 -0400904 /* Convert API flags to internal codec lib flags */
905 lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
906
907 /* vp8 use 10,000,000 ticks/second as time stamp */
908 dst_time_stamp = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
909 dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
910
911 if (img != NULL)
912 {
913 res = image2yuvconfig(img, &sd);
914
915 if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
916 &sd, dst_time_stamp, dst_end_time_stamp))
917 {
918 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
919 res = update_error_state(ctx, &cpi->common.error);
920 }
921
922 /* reset for next frame */
923 ctx->next_frame_flag = 0;
924 }
925
926 cx_data = ctx->cx_data;
927 cx_data_sz = ctx->cx_data_sz;
James Berrybc715112011-10-12 11:18:50 -0400928 cx_data_end = ctx->cx_data + cx_data_sz;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400929 lib_flags = 0;
930
James Berrybc715112011-10-12 11:18:50 -0400931 while (cx_data_sz >= ctx->cx_data_sz / 2)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400932 {
James Berrybc715112011-10-12 11:18:50 -0400933 comp_data_state = vp8_get_compressed_data(ctx->cpi,
934 &lib_flags,
935 &size,
936 cx_data,
937 cx_data_end,
938 &dst_time_stamp,
939 &dst_end_time_stamp,
940 !img);
941
942 if(comp_data_state == VPX_CODEC_CORRUPT_FRAME)
943 return VPX_CODEC_CORRUPT_FRAME;
944 else if(comp_data_state == -1)
945 break;
946
John Koleszar0ea50ce2010-05-18 11:58:33 -0400947 if (size)
948 {
949 vpx_codec_pts_t round, delta;
950 vpx_codec_cx_pkt_t pkt;
951 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
952
953 /* Add the frame packet to the list of returned packets. */
Dmitry Kovalev947748e2014-04-25 17:39:48 -0700954 round = (vpx_codec_pts_t)10000000
James Zern429743c2012-08-07 17:12:10 -0700955 * ctx->cfg.g_timebase.num / 2 - 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400956 delta = (dst_end_time_stamp - dst_time_stamp);
957 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400958 pkt.data.frame.pts =
959 (dst_time_stamp * ctx->cfg.g_timebase.den + round)
960 / ctx->cfg.g_timebase.num / 10000000;
Yaowu Xud71ba032012-08-17 10:05:35 -0700961 pkt.data.frame.duration = (unsigned long)
962 ((delta * ctx->cfg.g_timebase.den + round)
963 / ctx->cfg.g_timebase.num / 10000000);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400964 pkt.data.frame.flags = lib_flags << 16;
965
966 if (lib_flags & FRAMEFLAGS_KEY)
967 pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
968
969 if (!cpi->common.show_frame)
970 {
971 pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
972
John Koleszar0164a1c2012-05-21 14:30:56 -0700973 /* This timestamp should be as close as possible to the
974 * prior PTS so that if a decoder uses pts to schedule when
975 * to do this, we start right after last frame was decoded.
976 * Invisible frames have no duration.
977 */
Frank Galligan45e64942010-10-05 17:46:37 -0400978 pkt.data.frame.pts = ((cpi->last_time_stamp_seen
979 * ctx->cfg.g_timebase.den + round)
980 / ctx->cfg.g_timebase.num / 10000000) + 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400981 pkt.data.frame.duration = 0;
982 }
983
John Koleszar37de0b82011-07-07 10:38:23 -0400984 if (cpi->droppable)
985 pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE;
986
Stefan Holmer7296b3f2011-06-13 16:42:27 +0200987 if (cpi->output_partition)
988 {
989 int i;
990 const int num_partitions =
991 (1 << cpi->common.multi_token_partition) + 1;
Attila Nagy0afcc762011-07-20 14:09:42 +0300992
993 pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT;
994
Stefan Holmer7296b3f2011-06-13 16:42:27 +0200995 for (i = 0; i < num_partitions; ++i)
996 {
Attila Nagy52cf4dc2012-02-09 12:37:03 +0200997#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
998 pkt.data.frame.buf = cpi->partition_d[i];
999#else
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001000 pkt.data.frame.buf = cx_data;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001001 cx_data += cpi->partition_sz[i];
1002 cx_data_sz -= cpi->partition_sz[i];
1003#endif
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001004 pkt.data.frame.sz = cpi->partition_sz[i];
1005 pkt.data.frame.partition_id = i;
1006 /* don't set the fragment bit for the last partition */
Attila Nagy0afcc762011-07-20 14:09:42 +03001007 if (i == (num_partitions - 1))
1008 pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT;
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001009 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001010 }
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001011#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1012 /* In lagged mode the encoder can buffer multiple frames.
1013 * We don't want this in partitioned output because
1014 * partitions are spread all over the output buffer.
1015 * So, force an exit!
1016 */
1017 cx_data_sz -= ctx->cx_data_sz / 2;
1018#endif
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001019 }
1020 else
1021 {
1022 pkt.data.frame.buf = cx_data;
1023 pkt.data.frame.sz = size;
1024 pkt.data.frame.partition_id = -1;
1025 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1026 cx_data += size;
1027 cx_data_sz -= size;
1028 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001029 }
1030 }
1031 }
1032
1033 return res;
1034}
1035
1036
1037static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx,
1038 vpx_codec_iter_t *iter)
1039{
1040 return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1041}
1042
1043static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001044 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001045{
1046 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1047
1048 if (data)
1049 {
1050 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1051 YV12_BUFFER_CONFIG sd;
1052
1053 image2yuvconfig(&frame->img, &sd);
1054 vp8_set_reference(ctx->cpi, frame->frame_type, &sd);
1055 return VPX_CODEC_OK;
1056 }
1057 else
1058 return VPX_CODEC_INVALID_PARAM;
1059
1060}
1061
1062static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001063 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001064{
1065
1066 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1067
1068 if (data)
1069 {
1070 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1071 YV12_BUFFER_CONFIG sd;
1072
1073 image2yuvconfig(&frame->img, &sd);
1074 vp8_get_reference(ctx->cpi, frame->frame_type, &sd);
1075 return VPX_CODEC_OK;
1076 }
1077 else
1078 return VPX_CODEC_INVALID_PARAM;
1079}
1080
1081static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001082 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001083{
James Zern76640f82010-08-20 16:06:56 -04001084#if CONFIG_POSTPROC
John Koleszar0ea50ce2010-05-18 11:58:33 -04001085 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
1086
1087 if (data)
1088 {
1089 ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
1090 return VPX_CODEC_OK;
1091 }
1092 else
1093 return VPX_CODEC_INVALID_PARAM;
James Zern76640f82010-08-20 16:06:56 -04001094#else
1095 (void)ctx;
James Zern76640f82010-08-20 16:06:56 -04001096 (void)args;
1097 return VPX_CODEC_INCAPABLE;
1098#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -04001099}
1100
1101
1102static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx)
1103{
1104
1105 YV12_BUFFER_CONFIG sd;
Fritz Koenig647df002010-11-04 16:03:36 -07001106 vp8_ppflags_t flags = {0};
John Koleszar0ea50ce2010-05-18 11:58:33 -04001107
Fritz Koenig647df002010-11-04 16:03:36 -07001108 if (ctx->preview_ppcfg.post_proc_flag)
1109 {
1110 flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
1111 flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
1112 flags.noise_level = ctx->preview_ppcfg.noise_level;
1113 }
1114
1115 if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags))
John Koleszar0ea50ce2010-05-18 11:58:33 -04001116 {
1117
1118 /*
James Zern6cd4a102010-05-20 23:22:39 -04001119 vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001120 sd.y_width + 2*VP8BORDERINPIXELS,
1121 sd.y_height + 2*VP8BORDERINPIXELS,
1122 1,
1123 sd.buffer_alloc);
1124 vpx_img_set_rect(&ctx->preview_img,
1125 VP8BORDERINPIXELS, VP8BORDERINPIXELS,
1126 sd.y_width, sd.y_height);
1127 */
1128
1129 ctx->preview_img.bps = 12;
John Koleszarb6c71912010-05-24 21:45:05 -04001130 ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer;
1131 ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer;
1132 ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001133
James Zern4fc6c882013-07-12 14:11:53 -07001134 ctx->preview_img.fmt = VPX_IMG_FMT_I420;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001135 ctx->preview_img.x_chroma_shift = 1;
1136 ctx->preview_img.y_chroma_shift = 1;
1137
James Berryddacf1c2011-02-08 14:23:18 -05001138 ctx->preview_img.d_w = sd.y_width;
1139 ctx->preview_img.d_h = sd.y_height;
John Koleszarb6c71912010-05-24 21:45:05 -04001140 ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride;
1141 ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride;
1142 ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001143 ctx->preview_img.w = sd.y_width;
1144 ctx->preview_img.h = sd.y_height;
1145
1146 return &ctx->preview_img;
1147 }
1148 else
1149 return NULL;
1150}
1151
1152static vpx_codec_err_t vp8e_update_entropy(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001153 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001154{
1155 int update = va_arg(args, int);
1156 vp8_update_entropy(ctx->cpi, update);
1157 return VPX_CODEC_OK;
1158
1159}
1160
1161static vpx_codec_err_t vp8e_update_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001162 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001163{
1164 int update = va_arg(args, int);
1165 vp8_update_reference(ctx->cpi, update);
1166 return VPX_CODEC_OK;
1167}
1168
1169static vpx_codec_err_t vp8e_use_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001170 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001171{
1172 int reference_flag = va_arg(args, int);
1173 vp8_use_as_reference(ctx->cpi, reference_flag);
1174 return VPX_CODEC_OK;
1175}
1176
Marcoaf898b52014-11-10 13:07:05 -08001177static vpx_codec_err_t vp8e_set_frame_flags(vpx_codec_alg_priv_t *ctx,
1178 va_list args)
1179{
1180 int frame_flags = va_arg(args, int);
1181 ctx->control_frame_flags = frame_flags;
1182 return set_reference_and_update(ctx, frame_flags);
1183}
1184
1185static vpx_codec_err_t vp8e_set_temporal_layer_id(vpx_codec_alg_priv_t *ctx,
1186 va_list args)
1187{
1188 int layer_id = va_arg(args, int);
1189 if (layer_id < 0 || layer_id >= (int)ctx->cfg.ts_number_layers) {
1190 return VPX_CODEC_INVALID_PARAM;
1191 }
1192 ctx->cpi->temporal_layer_id = layer_id;
1193 return VPX_CODEC_OK;
1194}
1195
John Koleszar0ea50ce2010-05-18 11:58:33 -04001196static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001197 va_list args)
1198{
1199 vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
1200
1201 if (data)
1202 {
1203 vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
1204
1205 if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols, roi->delta_q, roi->delta_lf, roi->static_threshold))
1206 return VPX_CODEC_OK;
1207 else
1208 return VPX_CODEC_INVALID_PARAM;
1209 }
1210 else
1211 return VPX_CODEC_INVALID_PARAM;
1212}
1213
1214
1215static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001216 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001217{
1218 vpx_active_map_t *data = va_arg(args, vpx_active_map_t *);
1219
1220 if (data)
1221 {
1222
1223 vpx_active_map_t *map = (vpx_active_map_t *)data;
1224
1225 if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols))
1226 return VPX_CODEC_OK;
1227 else
1228 return VPX_CODEC_INVALID_PARAM;
1229 }
1230 else
1231 return VPX_CODEC_INVALID_PARAM;
1232}
1233
1234static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001235 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001236{
1237
1238 vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *);
1239
1240 if (data)
1241 {
1242 int res;
1243 vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data ;
Frank Galliganbc45f232013-01-11 15:34:05 -08001244 res = vp8_set_internal_size(ctx->cpi,
1245 (VPX_SCALING)scalemode.h_scaling_mode,
1246 (VPX_SCALING)scalemode.v_scaling_mode);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001247
1248 if (!res)
1249 {
1250 /*force next frame a key frame to effect scaling mode */
1251 ctx->next_frame_flag |= FRAMEFLAGS_KEY;
1252 return VPX_CODEC_OK;
1253 }
1254 else
1255 return VPX_CODEC_INVALID_PARAM;
1256 }
1257 else
1258 return VPX_CODEC_INVALID_PARAM;
1259}
1260
1261
1262static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] =
1263{
1264 {VP8_SET_REFERENCE, vp8e_set_reference},
1265 {VP8_COPY_REFERENCE, vp8e_get_reference},
1266 {VP8_SET_POSTPROC, vp8e_set_previewpp},
1267 {VP8E_UPD_ENTROPY, vp8e_update_entropy},
1268 {VP8E_UPD_REFERENCE, vp8e_update_reference},
1269 {VP8E_USE_REFERENCE, vp8e_use_reference},
Marcoaf898b52014-11-10 13:07:05 -08001270 {VP8E_SET_FRAME_FLAGS, vp8e_set_frame_flags},
1271 {VP8E_SET_TEMPORAL_LAYER_ID, vp8e_set_temporal_layer_id},
John Koleszar0ea50ce2010-05-18 11:58:33 -04001272 {VP8E_SET_ROI_MAP, vp8e_set_roi_map},
1273 {VP8E_SET_ACTIVEMAP, vp8e_set_activemap},
1274 {VP8E_SET_SCALEMODE, vp8e_set_scalemode},
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001275 {VP8E_SET_CPUUSED, set_cpu_used},
1276 {VP8E_SET_NOISE_SENSITIVITY, set_noise_sensitivity},
1277 {VP8E_SET_ENABLEAUTOALTREF, set_enable_auto_alt_ref},
1278 {VP8E_SET_SHARPNESS, set_sharpness},
1279 {VP8E_SET_STATIC_THRESHOLD, set_static_thresh},
1280 {VP8E_SET_TOKEN_PARTITIONS, set_token_partitions},
1281 {VP8E_GET_LAST_QUANTIZER, get_quantizer},
1282 {VP8E_GET_LAST_QUANTIZER_64, get_quantizer64},
1283 {VP8E_SET_ARNR_MAXFRAMES, set_arnr_max_frames},
1284 {VP8E_SET_ARNR_STRENGTH , set_arnr_strength},
1285 {VP8E_SET_ARNR_TYPE , set_arnr_type},
1286 {VP8E_SET_TUNING, set_tuning},
1287 {VP8E_SET_CQ_LEVEL, set_cq_level},
1288 {VP8E_SET_MAX_INTRA_BITRATE_PCT, set_rc_max_intra_bitrate_pct},
Marcoaf898b52014-11-10 13:07:05 -08001289 {VP8E_SET_SCREEN_CONTENT_MODE, set_screen_content_mode},
John Koleszar0ea50ce2010-05-18 11:58:33 -04001290 { -1, NULL},
1291};
1292
1293static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] =
1294{
1295 {
1296 0,
1297 {
1298 0, /* g_usage */
1299 0, /* g_threads */
1300 0, /* g_profile */
1301
1302 320, /* g_width */
1303 240, /* g_height */
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001304 VPX_BITS_8, /* g_bit_depth */
1305 8, /* g_input_bit_depth */
1306
John Koleszar0ea50ce2010-05-18 11:58:33 -04001307 {1, 30}, /* g_timebase */
1308
1309 0, /* g_error_resilient */
1310
1311 VPX_RC_ONE_PASS, /* g_pass */
1312
1313 0, /* g_lag_in_frames */
1314
John Koleszar23216212010-09-02 09:32:03 -04001315 0, /* rc_dropframe_thresh */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001316 0, /* rc_resize_allowed */
Adrian Grangef7bd1272014-04-09 14:51:29 -07001317 1, /* rc_scaled_width */
1318 1, /* rc_scaled_height */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001319 60, /* rc_resize_down_thresold */
1320 30, /* rc_resize_up_thresold */
1321
1322 VPX_VBR, /* rc_end_usage */
1323#if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
1324 {0}, /* rc_twopass_stats_in */
Pengchong Jinf349b072014-07-14 09:13:38 -07001325 {0}, /* rc_firstpass_mb_stats_in */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001326#endif
1327 256, /* rc_target_bandwidth */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001328 4, /* rc_min_quantizer */
1329 63, /* rc_max_quantizer */
John Koleszarc99f9d72011-04-11 11:29:23 -04001330 100, /* rc_undershoot_pct */
1331 100, /* rc_overshoot_pct */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001332
1333 6000, /* rc_max_buffer_size */
1334 4000, /* rc_buffer_initial_size; */
1335 5000, /* rc_buffer_optimal_size; */
1336
1337 50, /* rc_two_pass_vbrbias */
1338 0, /* rc_two_pass_vbrmin_section */
1339 400, /* rc_two_pass_vbrmax_section */
1340
1341 /* keyframing settings (kf) */
1342 VPX_KF_AUTO, /* g_kfmode*/
1343 0, /* kf_min_dist */
Ralph Giles2a0d7b12012-01-05 10:39:38 -06001344 128, /* kf_max_dist */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001345
1346#if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001347 "vp8.fpf" /* first pass filename */
1348#endif
Ivan Maltz01b35c32013-09-05 08:55:47 -07001349 VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */
Minghai Shange8998592014-07-14 11:24:17 -07001350 {0},
Minghai Shang8c196b22014-02-26 13:30:50 -08001351 {0}, /* ss_target_bitrate */
Adrian Grange217591f2011-10-06 15:49:11 -07001352 1, /* ts_number_layers */
1353 {0}, /* ts_target_bitrate */
1354 {0}, /* ts_rate_decimator */
1355 0, /* ts_periodicity */
1356 {0}, /* ts_layer_id */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001357 }},
John Koleszar0ea50ce2010-05-18 11:58:33 -04001358};
1359
1360
1361#ifndef VERSION_STRING
1362#define VERSION_STRING
1363#endif
John Koleszarfa7a55b2010-09-21 10:35:52 -04001364CODEC_INTERFACE(vpx_codec_vp8_cx) =
John Koleszar0ea50ce2010-05-18 11:58:33 -04001365{
John Koleszar317a6662010-06-10 12:08:01 -04001366 "WebM Project VP8 Encoder" VERSION_STRING,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001367 VPX_CODEC_INTERNAL_ABI_VERSION,
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001368 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR |
1369 VPX_CODEC_CAP_OUTPUT_PARTITION,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001370 /* vpx_codec_caps_t caps; */
1371 vp8e_init, /* vpx_codec_init_fn_t init; */
1372 vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */
1373 vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001374 {
Dmitry Kovalevcd6d9842014-08-20 17:16:28 -07001375 NULL, /* vpx_codec_peek_si_fn_t peek_si; */
1376 NULL, /* vpx_codec_get_si_fn_t get_si; */
1377 NULL, /* vpx_codec_decode_fn_t decode; */
1378 NULL, /* vpx_codec_frame_get_fn_t frame_get; */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001379 },
1380 {
Jim Bankoski8a774e12014-07-31 06:27:57 -07001381 1, /* 1 cfg map */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001382 vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t peek_si; */
1383 vp8e_encode, /* vpx_codec_encode_fn_t encode; */
1384 vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t frame_get; */
1385 vp8e_set_config,
Dmitry Kovalevcd6d9842014-08-20 17:16:28 -07001386 NULL,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001387 vp8e_get_preview,
Yunqing Wangaa7335e2011-10-25 15:14:16 -04001388 vp8e_mr_alloc_mem,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001389 } /* encoder functions */
1390};