blob: b4c4c09e95336c80327ec872c9d4a810c8cb8c38 [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"
Johann14ef4ae2015-04-15 09:27:00 -040013#include "./vp8_rtcd.h"
14#include "./vpx_scale_rtcd.h"
John Koleszarb7492342010-05-24 11:39:59 -040015#include "vpx/vpx_codec.h"
16#include "vpx/internal/vpx_codec_internal.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040017#include "vpx_version.h"
Dmitry Kovalevb08fab82014-09-02 18:26:53 -070018#include "vpx_mem/vpx_mem.h"
John Koleszar02321de2011-02-10 14:41:38 -050019#include "vp8/encoder/onyx_int.h"
John Koleszar2bf8fb52012-05-02 16:37:37 -070020#include "vpx/vp8cx.h"
John Koleszarbb7dd5b2010-10-14 16:40:12 -040021#include "vp8/encoder/firstpass.h"
John Koleszar02321de2011-02-10 14:41:38 -050022#include "vp8/common/onyx.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040023#include <stdlib.h>
24#include <string.h>
25
John Koleszar0ea50ce2010-05-18 11:58:33 -040026struct vp8_extracfg
27{
28 struct vpx_codec_pkt_list *pkt_list;
John Koleszar0ea50ce2010-05-18 11:58:33 -040029 int cpu_used; /** available cpu percentage in 1/16*/
30 unsigned int enable_auto_alt_ref; /** if encoder decides to uses alternate reference frame */
31 unsigned int noise_sensitivity;
32 unsigned int Sharpness;
33 unsigned int static_thresh;
34 unsigned int token_partitions;
35 unsigned int arnr_max_frames; /* alt_ref Noise Reduction Max Frame Count */
36 unsigned int arnr_strength; /* alt_ref Noise Reduction Strength */
37 unsigned int arnr_type; /* alt_ref filter type */
John Koleszarb0da9b32010-12-17 09:43:39 -050038 vp8e_tuning tuning;
Paul Wilkinse0846c92011-01-07 18:29:37 +000039 unsigned int cq_level; /* constrained quality level */
John Koleszar1654ae92011-07-28 09:17:32 -040040 unsigned int rc_max_intra_bitrate_pct;
Marcoaf898b52014-11-10 13:07:05 -080041 unsigned int screen_content_mode;
John Koleszar0ea50ce2010-05-18 11:58:33 -040042
43};
44
Dmitry Kovalev48274c62014-08-21 11:21:18 -070045static struct vp8_extracfg default_extracfg = {
46 NULL,
John Koleszar0ea50ce2010-05-18 11:58:33 -040047#if !(CONFIG_REALTIME_ONLY)
Dmitry Kovalev48274c62014-08-21 11:21:18 -070048 0, /* cpu_used */
John Koleszar0ea50ce2010-05-18 11:58:33 -040049#else
Dmitry Kovalev48274c62014-08-21 11:21:18 -070050 4, /* cpu_used */
John Koleszar0ea50ce2010-05-18 11:58:33 -040051#endif
Dmitry Kovalev48274c62014-08-21 11:21:18 -070052 0, /* enable_auto_alt_ref */
53 0, /* noise_sensitivity */
54 0, /* Sharpness */
55 0, /* static_thresh */
Attila Nagy52cf4dc2012-02-09 12:37:03 +020056#if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
Dmitry Kovalev48274c62014-08-21 11:21:18 -070057 VP8_EIGHT_TOKENPARTITION,
Attila Nagy52cf4dc2012-02-09 12:37:03 +020058#else
Dmitry Kovalev48274c62014-08-21 11:21:18 -070059 VP8_ONE_TOKENPARTITION, /* token_partitions */
Attila Nagy52cf4dc2012-02-09 12:37:03 +020060#endif
Dmitry Kovalev48274c62014-08-21 11:21:18 -070061 0, /* arnr_max_frames */
62 3, /* arnr_strength */
63 3, /* arnr_type*/
64 0, /* tuning*/
65 10, /* cq_level */
66 0, /* rc_max_intra_bitrate_pct */
Marcoaf898b52014-11-10 13:07:05 -080067 0, /* screen_content_mode */
John Koleszar0ea50ce2010-05-18 11:58:33 -040068};
69
70struct vpx_codec_alg_priv
71{
72 vpx_codec_priv_t base;
73 vpx_codec_enc_cfg_t cfg;
74 struct vp8_extracfg vp8_cfg;
75 VP8_CONFIG oxcf;
John Koleszarb0056c32011-12-20 16:54:54 -080076 struct VP8_COMP *cpi;
John Koleszar0ea50ce2010-05-18 11:58:33 -040077 unsigned char *cx_data;
78 unsigned int cx_data_sz;
79 vpx_image_t preview_img;
80 unsigned int next_frame_flag;
81 vp8_postproc_cfg_t preview_ppcfg;
John Koleszar0164a1c2012-05-21 14:30:56 -070082 /* pkt_list size depends on the maximum number of lagged frames allowed. */
83 vpx_codec_pkt_list_decl(64) pkt_list;
John Koleszar0ea50ce2010-05-18 11:58:33 -040084 unsigned int fixed_kf_cntr;
Marcoaf898b52014-11-10 13:07:05 -080085 vpx_enc_frame_flags_t control_frame_flags;
John Koleszar0ea50ce2010-05-18 11:58:33 -040086};
87
88
89static vpx_codec_err_t
90update_error_state(vpx_codec_alg_priv_t *ctx,
91 const struct vpx_internal_error_info *error)
92{
93 vpx_codec_err_t res;
94
95 if ((res = error->error_code))
96 ctx->base.err_detail = error->has_detail
97 ? error->detail
98 : NULL;
99
100 return res;
101}
102
103
John Koleszar79e2b1f2010-11-17 09:08:47 -0500104#undef ERROR
John Koleszar0ea50ce2010-05-18 11:58:33 -0400105#define ERROR(str) do {\
106 ctx->base.err_detail = str;\
107 return VPX_CODEC_INVALID_PARAM;\
108 } while(0)
109
110#define RANGE_CHECK(p,memb,lo,hi) do {\
Guillermo Ballester Valor5a726202010-06-11 14:33:49 -0400111 if(!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
John Koleszar0ea50ce2010-05-18 11:58:33 -0400112 ERROR(#memb " out of range ["#lo".."#hi"]");\
113 } while(0)
114
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400115#define RANGE_CHECK_HI(p,memb,hi) do {\
116 if(!((p)->memb <= (hi))) \
117 ERROR(#memb " out of range [.."#hi"]");\
118 } while(0)
119
John Koleszar0ea50ce2010-05-18 11:58:33 -0400120#define RANGE_CHECK_LO(p,memb,lo) do {\
121 if(!((p)->memb >= (lo))) \
122 ERROR(#memb " out of range ["#lo"..]");\
123 } while(0)
124
125#define RANGE_CHECK_BOOL(p,memb) do {\
126 if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
127 } while(0)
128
129static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
130 const vpx_codec_enc_cfg_t *cfg,
James Berryc1c47e82011-12-07 15:48:00 -0500131 const struct vp8_extracfg *vp8_cfg,
132 int finalize)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400133{
Attila Nagy1aadced2011-04-12 15:01:22 +0300134 RANGE_CHECK(cfg, g_w, 1, 16383); /* 14 bits available */
135 RANGE_CHECK(cfg, g_h, 1, 16383); /* 14 bits available */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400136 RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
137 RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400138 RANGE_CHECK_HI(cfg, g_profile, 3);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400139 RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
John Koleszar2d03f072011-01-28 11:56:18 -0500140 RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400141 RANGE_CHECK_HI(cfg, g_threads, 64);
Yunqing Wang7f009972012-07-11 11:43:51 -0700142#if CONFIG_REALTIME_ONLY
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400143 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
Yunqing Wang7f009972012-07-11 11:43:51 -0700144#elif CONFIG_MULTI_RES_ENCODING
145 if (ctx->base.enc.total_encoders > 1)
146 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
147#else
148 RANGE_CHECK_HI(cfg, g_lag_in_frames, 25);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400149#endif
Deb Mukherjeee378a892013-08-29 16:21:44 -0700150 RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
John Koleszarc99f9d72011-04-11 11:29:23 -0400151 RANGE_CHECK_HI(cfg, rc_undershoot_pct, 1000);
152 RANGE_CHECK_HI(cfg, rc_overshoot_pct, 1000);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400153 RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400154 RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
Yunqing Wang7f009972012-07-11 11:43:51 -0700155
156/* TODO: add spatial re-sampling support and frame dropping in
157 * multi-res-encoder.*/
158#if CONFIG_MULTI_RES_ENCODING
159 if (ctx->base.enc.total_encoders > 1)
Yunqing Wang7f009972012-07-11 11:43:51 -0700160 RANGE_CHECK_HI(cfg, rc_resize_allowed, 0);
Yunqing Wang7f009972012-07-11 11:43:51 -0700161#else
162 RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
Yunqing Wang7f009972012-07-11 11:43:51 -0700163#endif
Yunqing Wang4066c8b2012-06-08 11:17:50 -0400164 RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400165 RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
166 RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
Yunqing Wang7f009972012-07-11 11:43:51 -0700167
168#if CONFIG_REALTIME_ONLY
John Koleszar0ea50ce2010-05-18 11:58:33 -0400169 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
Yunqing Wang7f009972012-07-11 11:43:51 -0700170#elif CONFIG_MULTI_RES_ENCODING
171 if (ctx->base.enc.total_encoders > 1)
172 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
173#else
174 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400175#endif
176
177 /* VP8 does not support a lower bound on the keyframe interval in
178 * automatic keyframe placement mode.
179 */
180 if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist
181 && cfg->kf_min_dist > 0)
182 ERROR("kf_min_dist not supported in auto mode, use 0 "
183 "or kf_max_dist instead.");
184
185 RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref);
Yunqing Wang4fd81a92011-04-11 15:55:04 -0400186 RANGE_CHECK(vp8_cfg, cpu_used, -16, 16);
John Koleszarc8f4c182012-05-08 11:23:42 -0700187
188#if CONFIG_REALTIME_ONLY && !CONFIG_TEMPORAL_DENOISING
189 RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0);
190#else
191 RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6);
Stefan Holmer9c411432012-03-06 10:48:18 +0100192#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -0400193
Yunqing Wang7f009972012-07-11 11:43:51 -0700194 RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION,
195 VP8_EIGHT_TOKENPARTITION);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400196 RANGE_CHECK_HI(vp8_cfg, Sharpness, 7);
Adrian Grange8ee72842010-09-30 10:06:09 +0100197 RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
Frank Galligan15542722010-10-04 21:12:22 -0400198 RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6);
Adrian Grange8ee72842010-09-30 10:06:09 +0100199 RANGE_CHECK(vp8_cfg, arnr_type, 1, 3);
Paul Wilkinse0846c92011-01-07 18:29:37 +0000200 RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
Marcoaf898b52014-11-10 13:07:05 -0800201 RANGE_CHECK_BOOL(vp8_cfg, screen_content_mode);
Deb Mukherjeee378a892013-08-29 16:21:44 -0700202 if (finalize && (cfg->rc_end_usage == VPX_CQ || cfg->rc_end_usage == VPX_Q))
James Berryc1c47e82011-12-07 15:48:00 -0500203 RANGE_CHECK(vp8_cfg, cq_level,
204 cfg->rc_min_quantizer, cfg->rc_max_quantizer);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400205
Attila Nagycb791aa2011-01-10 11:14:10 +0200206#if !(CONFIG_REALTIME_ONLY)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400207 if (cfg->g_pass == VPX_RC_LAST_PASS)
208 {
Adrian Grangeed40ff92011-03-10 11:32:48 -0800209 size_t packet_sz = sizeof(FIRSTPASS_STATS);
Yaowu Xud71ba032012-08-17 10:05:35 -0700210 int n_packets = (int)(cfg->rc_twopass_stats_in.sz /
211 packet_sz);
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400212 FIRSTPASS_STATS *stats;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400213
214 if (!cfg->rc_twopass_stats_in.buf)
215 ERROR("rc_twopass_stats_in.buf not set.");
216
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400217 if (cfg->rc_twopass_stats_in.sz % packet_sz)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400218 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
219
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400220 if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400221 ERROR("rc_twopass_stats_in requires at least two packets.");
222
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400223 stats = (void*)((char *)cfg->rc_twopass_stats_in.buf
224 + (n_packets - 1) * packet_sz);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400225
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400226 if ((int)(stats->count + 0.5) != n_packets - 1)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400227 ERROR("rc_twopass_stats_in missing EOS stats packet");
228 }
Attila Nagycb791aa2011-01-10 11:14:10 +0200229#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -0400230
Adrian Grange217591f2011-10-06 15:49:11 -0700231 RANGE_CHECK(cfg, ts_number_layers, 1, 5);
232
233 if (cfg->ts_number_layers > 1)
234 {
Scott Graham92963df2012-05-08 11:45:35 -0700235 unsigned int i;
Adrian Grange217591f2011-10-06 15:49:11 -0700236 RANGE_CHECK_HI(cfg, ts_periodicity, 16);
237
238 for (i=1; i<cfg->ts_number_layers; i++)
Marcoaf898b52014-11-10 13:07:05 -0800239 if (cfg->ts_target_bitrate[i] <= cfg->ts_target_bitrate[i-1] &&
240 cfg->rc_target_bitrate > 0)
Adrian Grange217591f2011-10-06 15:49:11 -0700241 ERROR("ts_target_bitrate entries are not strictly increasing");
242
243 RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers-1], 1, 1);
244 for (i=cfg->ts_number_layers-2; i>0; i--)
245 if (cfg->ts_rate_decimator[i-1] != 2*cfg->ts_rate_decimator[i])
246 ERROR("ts_rate_decimator factors are not powers of 2");
247
248 RANGE_CHECK_HI(cfg, ts_layer_id[i], cfg->ts_number_layers-1);
249 }
250
Attila Nagy52cf4dc2012-02-09 12:37:03 +0200251#if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
252 if(cfg->g_threads > (1 << vp8_cfg->token_partitions))
253 ERROR("g_threads cannot be bigger than number of token partitions");
254#endif
255
John Koleszar0ea50ce2010-05-18 11:58:33 -0400256 return VPX_CODEC_OK;
257}
258
259
260static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
261 const vpx_image_t *img)
262{
263 switch (img->fmt)
264 {
James Zern6cd4a102010-05-20 23:22:39 -0400265 case VPX_IMG_FMT_YV12:
266 case VPX_IMG_FMT_I420:
267 case VPX_IMG_FMT_VPXI420:
268 case VPX_IMG_FMT_VPXYV12:
John Koleszar0ea50ce2010-05-18 11:58:33 -0400269 break;
270 default:
271 ERROR("Invalid image format. Only YV12 and I420 images are supported");
272 }
273
274 if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
275 ERROR("Image size must match encoder init configuration size");
276
277 return VPX_CODEC_OK;
278}
279
280
281static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf,
282 vpx_codec_enc_cfg_t cfg,
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400283 struct vp8_extracfg vp8_cfg,
284 vpx_codec_priv_enc_mr_cfg_t *mr_cfg)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400285{
286 oxcf->multi_threaded = cfg.g_threads;
287 oxcf->Version = cfg.g_profile;
288
289 oxcf->Width = cfg.g_w;
290 oxcf->Height = cfg.g_h;
John Koleszarbdd35c12011-11-11 10:47:20 -0800291 oxcf->timebase = cfg.g_timebase;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400292
Adrian Grange217591f2011-10-06 15:49:11 -0700293 oxcf->error_resilient_mode = cfg.g_error_resilient;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400294
295 switch (cfg.g_pass)
296 {
297 case VPX_RC_ONE_PASS:
298 oxcf->Mode = MODE_BESTQUALITY;
299 break;
300 case VPX_RC_FIRST_PASS:
301 oxcf->Mode = MODE_FIRSTPASS;
302 break;
303 case VPX_RC_LAST_PASS:
304 oxcf->Mode = MODE_SECONDPASS_BEST;
305 break;
306 }
307
James Zern7b0b6a22012-06-13 11:59:12 -0700308 if (cfg.g_pass == VPX_RC_FIRST_PASS || cfg.g_pass == VPX_RC_ONE_PASS)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400309 {
Adrian Grange217591f2011-10-06 15:49:11 -0700310 oxcf->allow_lag = 0;
311 oxcf->lag_in_frames = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400312 }
313 else
314 {
Adrian Grange217591f2011-10-06 15:49:11 -0700315 oxcf->allow_lag = (cfg.g_lag_in_frames) > 0;
316 oxcf->lag_in_frames = cfg.g_lag_in_frames;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400317 }
318
319 oxcf->allow_df = (cfg.rc_dropframe_thresh > 0);
320 oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh;
321
322 oxcf->allow_spatial_resampling = cfg.rc_resize_allowed;
323 oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh;
324 oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh;
325
Deb Mukherjeee378a892013-08-29 16:21:44 -0700326 if (cfg.rc_end_usage == VPX_VBR) {
327 oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
328 } else if (cfg.rc_end_usage == VPX_CBR) {
329 oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
330 } else if (cfg.rc_end_usage == VPX_CQ) {
331 oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
332 } else if (cfg.rc_end_usage == VPX_Q) {
333 oxcf->end_usage = USAGE_CONSTANT_QUALITY;
Paul Wilkinse0846c92011-01-07 18:29:37 +0000334 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400335
Adrian Grange217591f2011-10-06 15:49:11 -0700336 oxcf->target_bandwidth = cfg.rc_target_bitrate;
John Koleszar1654ae92011-07-28 09:17:32 -0400337 oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400338
Adrian Grange217591f2011-10-06 15:49:11 -0700339 oxcf->best_allowed_q = cfg.rc_min_quantizer;
340 oxcf->worst_allowed_q = cfg.rc_max_quantizer;
341 oxcf->cq_level = vp8_cfg.cq_level;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400342 oxcf->fixed_q = -1;
343
Adrian Grange217591f2011-10-06 15:49:11 -0700344 oxcf->under_shoot_pct = cfg.rc_undershoot_pct;
345 oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400346
Adrian Grangee4793792012-01-13 14:09:40 -0800347 oxcf->maximum_buffer_size_in_ms = cfg.rc_buf_sz;
348 oxcf->starting_buffer_level_in_ms = cfg.rc_buf_initial_sz;
349 oxcf->optimal_buffer_level_in_ms = cfg.rc_buf_optimal_sz;
350
Adrian Grange217591f2011-10-06 15:49:11 -0700351 oxcf->maximum_buffer_size = cfg.rc_buf_sz;
352 oxcf->starting_buffer_level = cfg.rc_buf_initial_sz;
353 oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400354
Adrian Grange217591f2011-10-06 15:49:11 -0700355 oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400356 oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct;
357 oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct;
358
Adrian Grange217591f2011-10-06 15:49:11 -0700359 oxcf->auto_key = cfg.kf_mode == VPX_KF_AUTO
360 && cfg.kf_min_dist != cfg.kf_max_dist;
Adrian Grange217591f2011-10-06 15:49:11 -0700361 oxcf->key_freq = cfg.kf_max_dist;
362
363 oxcf->number_of_layers = cfg.ts_number_layers;
364 oxcf->periodicity = cfg.ts_periodicity;
365
366 if (oxcf->number_of_layers > 1)
367 {
368 memcpy (oxcf->target_bitrate, cfg.ts_target_bitrate,
James Zernf274c212015-04-23 20:42:19 -0700369 sizeof(cfg.ts_target_bitrate));
Adrian Grange217591f2011-10-06 15:49:11 -0700370 memcpy (oxcf->rate_decimator, cfg.ts_rate_decimator,
James Zernf274c212015-04-23 20:42:19 -0700371 sizeof(cfg.ts_rate_decimator));
Adrian Grange217591f2011-10-06 15:49:11 -0700372 memcpy (oxcf->layer_id, cfg.ts_layer_id, sizeof(cfg.ts_layer_id));
373 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400374
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400375#if CONFIG_MULTI_RES_ENCODING
Yunqing Wangc647ec42011-12-16 16:50:29 -0500376 /* When mr_cfg is NULL, oxcf->mr_total_resolutions and oxcf->mr_encoder_id
377 * are both memset to 0, which ensures the correct logic under this
378 * situation.
379 */
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400380 if(mr_cfg)
381 {
382 oxcf->mr_total_resolutions = mr_cfg->mr_total_resolutions;
383 oxcf->mr_encoder_id = mr_cfg->mr_encoder_id;
384 oxcf->mr_down_sampling_factor.num = mr_cfg->mr_down_sampling_factor.num;
385 oxcf->mr_down_sampling_factor.den = mr_cfg->mr_down_sampling_factor.den;
386 oxcf->mr_low_res_mode_info = mr_cfg->mr_low_res_mode_info;
387 }
Johann80b344d2014-12-16 12:22:10 -0800388#else
389 (void)mr_cfg;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400390#endif
391
Adrian Grange217591f2011-10-06 15:49:11 -0700392 oxcf->cpu_used = vp8_cfg.cpu_used;
393 oxcf->encode_breakout = vp8_cfg.static_thresh;
394 oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref;
395 oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity;
396 oxcf->Sharpness = vp8_cfg.Sharpness;
397 oxcf->token_partitions = vp8_cfg.token_partitions;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400398
Adrian Grange217591f2011-10-06 15:49:11 -0700399 oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in;
400 oxcf->output_pkt_list = vp8_cfg.pkt_list;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400401
Adrian Grange217591f2011-10-06 15:49:11 -0700402 oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
403 oxcf->arnr_strength = vp8_cfg.arnr_strength;
404 oxcf->arnr_type = vp8_cfg.arnr_type;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400405
Adrian Grange217591f2011-10-06 15:49:11 -0700406 oxcf->tuning = vp8_cfg.tuning;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400407
Marcoaf898b52014-11-10 13:07:05 -0800408 oxcf->screen_content_mode = vp8_cfg.screen_content_mode;
409
John Koleszar0ea50ce2010-05-18 11:58:33 -0400410 /*
411 printf("Current VP8 Settings: \n");
412 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
413 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
414 printf("Sharpness: %d\n", oxcf->Sharpness);
415 printf("cpu_used: %d\n", oxcf->cpu_used);
416 printf("Mode: %d\n", oxcf->Mode);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400417 printf("auto_key: %d\n", oxcf->auto_key);
418 printf("key_freq: %d\n", oxcf->key_freq);
419 printf("end_usage: %d\n", oxcf->end_usage);
420 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
John Koleszarc99f9d72011-04-11 11:29:23 -0400421 printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400422 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
423 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
424 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
425 printf("fixed_q: %d\n", oxcf->fixed_q);
426 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
427 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
428 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
429 printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
430 printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
431 printf("allow_df: %d\n", oxcf->allow_df);
432 printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
433 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
434 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
435 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
436 printf("allow_lag: %d\n", oxcf->allow_lag);
437 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
438 printf("play_alternate: %d\n", oxcf->play_alternate);
439 printf("Version: %d\n", oxcf->Version);
440 printf("multi_threaded: %d\n", oxcf->multi_threaded);
441 printf("encode_breakout: %d\n", oxcf->encode_breakout);
442 */
443 return VPX_CODEC_OK;
444}
445
446static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx,
447 const vpx_codec_enc_cfg_t *cfg)
448{
449 vpx_codec_err_t res;
450
Alex Converse581731a2015-01-16 16:02:05 -0800451 if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h)
452 {
453 if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)
454 ERROR("Cannot change width or height after initialization");
455 if ((ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
456 (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
457 ERROR("Cannot increast width or height larger than their initial values");
458 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400459
460 /* Prevent increasing lag_in_frames. This check is stricter than it needs
461 * to be -- the limit is not increasing past the first lag_in_frames
462 * value, but we don't track the initial config, only the last successful
463 * config.
464 */
465 if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
466 ERROR("Cannot increase lag_in_frames");
467
James Berryc1c47e82011-12-07 15:48:00 -0500468 res = validate_config(ctx, cfg, &ctx->vp8_cfg, 0);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400469
470 if (!res)
471 {
472 ctx->cfg = *cfg;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400473 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400474 vp8_change_config(ctx->cpi, &ctx->oxcf);
475 }
476
477 return res;
478}
479
John Koleszar0ea50ce2010-05-18 11:58:33 -0400480int vp8_reverse_trans(int);
481
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700482static vpx_codec_err_t get_quantizer(vpx_codec_alg_priv_t *ctx, va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400483{
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700484 int *const arg = va_arg(args, int *);
485 if (arg == NULL)
486 return VPX_CODEC_INVALID_PARAM;
487 *arg = vp8_get_quantizer(ctx->cpi);
488 return VPX_CODEC_OK;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400489}
490
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700491static vpx_codec_err_t get_quantizer64(vpx_codec_alg_priv_t *ctx, va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400492{
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700493 int *const arg = va_arg(args, int *);
494 if (arg == NULL)
495 return VPX_CODEC_INVALID_PARAM;
496 *arg = vp8_reverse_trans(vp8_get_quantizer(ctx->cpi));
497 return VPX_CODEC_OK;
498}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400499
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700500static vpx_codec_err_t update_extracfg(vpx_codec_alg_priv_t *ctx,
501 const struct vp8_extracfg *extra_cfg)
502{
503 const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg, 0);
504 if (res == VPX_CODEC_OK) {
505 ctx->vp8_cfg = *extra_cfg;
506 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
507 vp8_change_config(ctx->cpi, &ctx->oxcf);
508 }
509 return res;
510}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400511
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700512static vpx_codec_err_t set_cpu_used(vpx_codec_alg_priv_t *ctx, va_list args)
513{
514 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
515 extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, 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_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
520 va_list args)
521{
522 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
523 extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, 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_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
528 va_list args)
529{
530 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
531 extra_cfg.noise_sensitivity = CAST(VP8E_SET_NOISE_SENSITIVITY, args);
532 return update_extracfg(ctx, &extra_cfg);
533}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400534
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700535static vpx_codec_err_t set_sharpness(vpx_codec_alg_priv_t *ctx, va_list args)
536{
537 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
538 extra_cfg.Sharpness = CAST(VP8E_SET_SHARPNESS, 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_static_thresh(vpx_codec_alg_priv_t *ctx,
543 va_list args)
544{
545 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
546 extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
547 return update_extracfg(ctx, &extra_cfg);
548}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400549
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700550static vpx_codec_err_t set_token_partitions(vpx_codec_alg_priv_t *ctx,
551 va_list args)
552{
553 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
554 extra_cfg.token_partitions = CAST(VP8E_SET_TOKEN_PARTITIONS, args);
555 return update_extracfg(ctx, &extra_cfg);
556}
557
558static vpx_codec_err_t set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
559 va_list args)
560{
561 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
562 extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
563 return update_extracfg(ctx, &extra_cfg);
564}
565
566static vpx_codec_err_t set_arnr_strength(vpx_codec_alg_priv_t *ctx,
567 va_list args)
568{
569 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
570 extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
571 return update_extracfg(ctx, &extra_cfg);
572}
573
574static vpx_codec_err_t set_arnr_type(vpx_codec_alg_priv_t *ctx, va_list args)
575{
576 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
577 extra_cfg.arnr_type = CAST(VP8E_SET_ARNR_TYPE, args);
578 return update_extracfg(ctx, &extra_cfg);
579}
580
581static vpx_codec_err_t set_tuning(vpx_codec_alg_priv_t *ctx, va_list args)
582{
583 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
584 extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
585 return update_extracfg(ctx, &extra_cfg);
586}
587
588static vpx_codec_err_t set_cq_level(vpx_codec_alg_priv_t *ctx, va_list args)
589{
590 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
591 extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
592 return update_extracfg(ctx, &extra_cfg);
593}
594
595static vpx_codec_err_t set_rc_max_intra_bitrate_pct(vpx_codec_alg_priv_t *ctx,
596 va_list args)
597{
598 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
599 extra_cfg.rc_max_intra_bitrate_pct =
600 CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
601 return update_extracfg(ctx, &extra_cfg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400602}
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400603
Marcoaf898b52014-11-10 13:07:05 -0800604static vpx_codec_err_t set_screen_content_mode(vpx_codec_alg_priv_t *ctx,
605 va_list args)
606{
607 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
608 extra_cfg.screen_content_mode =
609 CAST(VP8E_SET_SCREEN_CONTENT_MODE, args);
610 return update_extracfg(ctx, &extra_cfg);
611}
612
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400613static vpx_codec_err_t vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t *cfg,
614 void **mem_loc)
615{
Yunqing Wangad479a92012-05-23 13:40:24 -0400616 vpx_codec_err_t res = 0;
617
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400618#if CONFIG_MULTI_RES_ENCODING
Yunqing Wang65dd1572012-05-16 15:06:42 -0400619 LOWER_RES_FRAME_INFO *shared_mem_loc;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400620 int mb_rows = ((cfg->g_w + 15) >>4);
621 int mb_cols = ((cfg->g_h + 15) >>4);
622
Yunqing Wang65dd1572012-05-16 15:06:42 -0400623 shared_mem_loc = calloc(1, sizeof(LOWER_RES_FRAME_INFO));
624 if(!shared_mem_loc)
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400625 {
Yunqing Wangad479a92012-05-23 13:40:24 -0400626 res = VPX_CODEC_MEM_ERROR;
Yunqing Wang65dd1572012-05-16 15:06:42 -0400627 }
628
629 shared_mem_loc->mb_info = calloc(mb_rows*mb_cols, sizeof(LOWER_RES_MB_INFO));
630 if(!(shared_mem_loc->mb_info))
631 {
Yunqing Wangad479a92012-05-23 13:40:24 -0400632 res = VPX_CODEC_MEM_ERROR;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400633 }
634 else
Yunqing Wang65dd1572012-05-16 15:06:42 -0400635 {
636 *mem_loc = (void *)shared_mem_loc;
Yunqing Wangad479a92012-05-23 13:40:24 -0400637 res = VPX_CODEC_OK;
Yunqing Wang65dd1572012-05-16 15:06:42 -0400638 }
Johann80b344d2014-12-16 12:22:10 -0800639#else
640 (void)cfg;
641 (void)mem_loc;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400642#endif
Yunqing Wangad479a92012-05-23 13:40:24 -0400643 return res;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400644}
645
646static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx,
647 vpx_codec_priv_enc_mr_cfg_t *mr_cfg)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400648{
John Koleszar2bf8fb52012-05-02 16:37:37 -0700649 vpx_codec_err_t res = VPX_CODEC_OK;
Dmitry Kovalevb08fab82014-09-02 18:26:53 -0700650
John Koleszar0ea50ce2010-05-18 11:58:33 -0400651
John Koleszara9c75972012-11-08 17:09:30 -0800652 vp8_rtcd();
Johann14ef4ae2015-04-15 09:27:00 -0400653 vpx_scale_rtcd();
John Koleszar8df79e92012-06-15 15:40:13 -0700654
John Koleszar0ea50ce2010-05-18 11:58:33 -0400655 if (!ctx->priv)
656 {
Dmitry Kovalevb08fab82014-09-02 18:26:53 -0700657 struct vpx_codec_alg_priv *priv =
658 (struct vpx_codec_alg_priv *)vpx_calloc(1, sizeof(*priv));
John Koleszar0ea50ce2010-05-18 11:58:33 -0400659
Attila Nagye6db21e2011-02-22 15:02:05 +0200660 if (!priv)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400661 {
Attila Nagye6db21e2011-02-22 15:02:05 +0200662 return VPX_CODEC_MEM_ERROR;
663 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400664
Dmitry Kovalev73edeb02014-08-20 17:02:10 -0700665 ctx->priv = (vpx_codec_priv_t *)priv;
Attila Nagye6db21e2011-02-22 15:02:05 +0200666 ctx->priv->init_flags = ctx->init_flags;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400667
Attila Nagye6db21e2011-02-22 15:02:05 +0200668 if (ctx->config.enc)
669 {
670 /* Update the reference to the config structure to an
671 * internal copy.
John Koleszar0ea50ce2010-05-18 11:58:33 -0400672 */
Dmitry Kovalev73edeb02014-08-20 17:02:10 -0700673 priv->cfg = *ctx->config.enc;
674 ctx->config.enc = &priv->cfg;
Attila Nagye6db21e2011-02-22 15:02:05 +0200675 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400676
Dmitry Kovalev48274c62014-08-21 11:21:18 -0700677 priv->vp8_cfg = default_extracfg;
Attila Nagye6db21e2011-02-22 15:02:05 +0200678 priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400679
Attila Nagye6db21e2011-02-22 15:02:05 +0200680 priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400681
James Berrybac6c222011-10-24 11:50:27 -0400682 if (priv->cx_data_sz < 32768) priv->cx_data_sz = 32768;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400683
Attila Nagye6db21e2011-02-22 15:02:05 +0200684 priv->cx_data = malloc(priv->cx_data_sz);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400685
Attila Nagye6db21e2011-02-22 15:02:05 +0200686 if (!priv->cx_data)
687 {
688 return VPX_CODEC_MEM_ERROR;
689 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400690
Yunqing Wang7f009972012-07-11 11:43:51 -0700691 if(mr_cfg)
692 ctx->priv->enc.total_encoders = mr_cfg->mr_total_resolutions;
693 else
694 ctx->priv->enc.total_encoders = 1;
695
James Berryc1c47e82011-12-07 15:48:00 -0500696 res = validate_config(priv, &priv->cfg, &priv->vp8_cfg, 0);
Attila Nagye6db21e2011-02-22 15:02:05 +0200697
698 if (!res)
699 {
Dmitry Kovalev73edeb02014-08-20 17:02:10 -0700700 set_vp8e_config(&priv->oxcf, priv->cfg, priv->vp8_cfg, mr_cfg);
701 priv->cpi = vp8_create_compressor(&priv->oxcf);
702 if (!priv->cpi)
Attila Nagye6db21e2011-02-22 15:02:05 +0200703 res = VPX_CODEC_MEM_ERROR;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400704 }
705 }
706
707 return res;
708}
709
710static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx)
711{
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400712#if CONFIG_MULTI_RES_ENCODING
713 /* Free multi-encoder shared memory */
714 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 -0400715 {
716 LOWER_RES_FRAME_INFO *shared_mem_loc = (LOWER_RES_FRAME_INFO *)ctx->oxcf.mr_low_res_mode_info;
717 free(shared_mem_loc->mb_info);
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400718 free(ctx->oxcf.mr_low_res_mode_info);
Yunqing Wang65dd1572012-05-16 15:06:42 -0400719 }
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400720#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -0400721
722 free(ctx->cx_data);
723 vp8_remove_compressor(&ctx->cpi);
Dmitry Kovalevb08fab82014-09-02 18:26:53 -0700724 vpx_free(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400725 return VPX_CODEC_OK;
726}
727
728static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
729 YV12_BUFFER_CONFIG *yv12)
730{
Johann69dc8762014-08-15 10:50:40 -0700731 const int y_w = img->d_w;
732 const int y_h = img->d_h;
733 const int uv_w = (img->d_w + 1) / 2;
734 const int uv_h = (img->d_h + 1) / 2;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400735 vpx_codec_err_t res = VPX_CODEC_OK;
John Koleszarb6c71912010-05-24 21:45:05 -0400736 yv12->y_buffer = img->planes[VPX_PLANE_Y];
737 yv12->u_buffer = img->planes[VPX_PLANE_U];
738 yv12->v_buffer = img->planes[VPX_PLANE_V];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400739
Johann69dc8762014-08-15 10:50:40 -0700740 yv12->y_crop_width = y_w;
741 yv12->y_crop_height = y_h;
742 yv12->y_width = y_w;
743 yv12->y_height = y_h;
744 yv12->uv_crop_width = uv_w;
745 yv12->uv_crop_height = uv_h;
746 yv12->uv_width = uv_w;
747 yv12->uv_height = uv_h;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400748
John Koleszarb6c71912010-05-24 21:45:05 -0400749 yv12->y_stride = img->stride[VPX_PLANE_Y];
750 yv12->uv_stride = img->stride[VPX_PLANE_U];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400751
John Koleszarb6c71912010-05-24 21:45:05 -0400752 yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400753 return res;
754}
755
756static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
757 unsigned long duration,
758 unsigned long deadline)
759{
760 unsigned int new_qc;
761
762#if !(CONFIG_REALTIME_ONLY)
763 /* Use best quality mode if no deadline is given. */
764 new_qc = MODE_BESTQUALITY;
765
766 if (deadline)
767 {
768 uint64_t duration_us;
769
770 /* Convert duration parameter from stream timebase to microseconds */
771 duration_us = (uint64_t)duration * 1000000
772 * (uint64_t)ctx->cfg.g_timebase.num
773 / (uint64_t)ctx->cfg.g_timebase.den;
774
775 /* If the deadline is more that the duration this frame is to be shown,
776 * use good quality mode. Otherwise use realtime mode.
777 */
778 new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
779 }
780
781#else
782 new_qc = MODE_REALTIME;
783#endif
784
John Koleszar0ea50ce2010-05-18 11:58:33 -0400785 if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS)
786 new_qc = MODE_FIRSTPASS;
787 else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS)
788 new_qc = (new_qc == MODE_BESTQUALITY)
789 ? MODE_SECONDPASS_BEST
790 : MODE_SECONDPASS;
791
792 if (ctx->oxcf.Mode != new_qc)
793 {
794 ctx->oxcf.Mode = new_qc;
795 vp8_change_config(ctx->cpi, &ctx->oxcf);
796 }
797}
798
Marcoaf898b52014-11-10 13:07:05 -0800799static vpx_codec_err_t set_reference_and_update(vpx_codec_alg_priv_t *ctx,
800 int flags)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400801{
John Koleszar0ea50ce2010-05-18 11:58:33 -0400802
803 /* Handle Flags */
804 if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF))
805 || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF)))
806 {
807 ctx->base.err_detail = "Conflicting flags.";
808 return VPX_CODEC_INVALID_PARAM;
809 }
810
811 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF
812 | VP8_EFLAG_NO_REF_ARF))
813 {
814 int ref = 7;
815
816 if (flags & VP8_EFLAG_NO_REF_LAST)
Johann965d8682012-05-23 16:08:37 -0700817 ref ^= VP8_LAST_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400818
819 if (flags & VP8_EFLAG_NO_REF_GF)
Johann965d8682012-05-23 16:08:37 -0700820 ref ^= VP8_GOLD_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400821
822 if (flags & VP8_EFLAG_NO_REF_ARF)
Johann965d8682012-05-23 16:08:37 -0700823 ref ^= VP8_ALTR_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400824
825 vp8_use_as_reference(ctx->cpi, ref);
826 }
827
828 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF
829 | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF
830 | VP8_EFLAG_FORCE_ARF))
831 {
832 int upd = 7;
833
834 if (flags & VP8_EFLAG_NO_UPD_LAST)
Johann965d8682012-05-23 16:08:37 -0700835 upd ^= VP8_LAST_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400836
837 if (flags & VP8_EFLAG_NO_UPD_GF)
Johann965d8682012-05-23 16:08:37 -0700838 upd ^= VP8_GOLD_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400839
840 if (flags & VP8_EFLAG_NO_UPD_ARF)
Johann965d8682012-05-23 16:08:37 -0700841 upd ^= VP8_ALTR_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400842
843 vp8_update_reference(ctx->cpi, upd);
844 }
845
846 if (flags & VP8_EFLAG_NO_UPD_ENTROPY)
847 {
848 vp8_update_entropy(ctx->cpi, 0);
849 }
850
Marcoaf898b52014-11-10 13:07:05 -0800851 return VPX_CODEC_OK;
852}
853
854static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx,
855 const vpx_image_t *img,
856 vpx_codec_pts_t pts,
857 unsigned long duration,
858 vpx_enc_frame_flags_t flags,
859 unsigned long deadline)
860{
861 vpx_codec_err_t res = VPX_CODEC_OK;
862
863 if (!ctx->cfg.rc_target_bitrate)
864 return res;
865
Marcoaf898b52014-11-10 13:07:05 -0800866 if (img)
867 res = validate_img(ctx, img);
868
869 if (!res)
870 res = validate_config(ctx, &ctx->cfg, &ctx->vp8_cfg, 1);
871
872 pick_quickcompress_mode(ctx, duration, deadline);
873 vpx_codec_pkt_list_init(&ctx->pkt_list);
874
875 // If no flags are set in the encode call, then use the frame flags as
876 // defined via the control function: vp8e_set_frame_flags.
877 if (!flags) {
878 flags = ctx->control_frame_flags;
879 }
880 ctx->control_frame_flags = 0;
881
882 res = set_reference_and_update(ctx, flags);
883
John Koleszar0ea50ce2010-05-18 11:58:33 -0400884 /* Handle fixed keyframe intervals */
885 if (ctx->cfg.kf_mode == VPX_KF_AUTO
886 && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist)
887 {
888 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist)
889 {
890 flags |= VPX_EFLAG_FORCE_KF;
Andoni Morales Alastruey48140162011-02-07 18:04:02 +0100891 ctx->fixed_kf_cntr = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400892 }
893 }
894
895 /* Initialize the encoder instance on the first frame*/
896 if (!res && ctx->cpi)
897 {
898 unsigned int lib_flags;
899 YV12_BUFFER_CONFIG sd;
James Zernb45065d2011-07-25 18:44:59 -0700900 int64_t dst_time_stamp, dst_end_time_stamp;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400901 unsigned long size, cx_data_sz;
902 unsigned char *cx_data;
James Berrybc715112011-10-12 11:18:50 -0400903 unsigned char *cx_data_end;
904 int comp_data_state = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400905
906 /* Set up internal flags */
907 if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
908 ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1;
909
Stefan Holmer7296b3f2011-06-13 16:42:27 +0200910 if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION)
911 ((VP8_COMP *)ctx->cpi)->output_partition = 1;
912
John Koleszar0ea50ce2010-05-18 11:58:33 -0400913 /* Convert API flags to internal codec lib flags */
914 lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
915
916 /* vp8 use 10,000,000 ticks/second as time stamp */
917 dst_time_stamp = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
918 dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
919
920 if (img != NULL)
921 {
922 res = image2yuvconfig(img, &sd);
923
924 if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
925 &sd, dst_time_stamp, dst_end_time_stamp))
926 {
927 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
928 res = update_error_state(ctx, &cpi->common.error);
929 }
930
931 /* reset for next frame */
932 ctx->next_frame_flag = 0;
933 }
934
935 cx_data = ctx->cx_data;
936 cx_data_sz = ctx->cx_data_sz;
James Berrybc715112011-10-12 11:18:50 -0400937 cx_data_end = ctx->cx_data + cx_data_sz;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400938 lib_flags = 0;
939
James Berrybc715112011-10-12 11:18:50 -0400940 while (cx_data_sz >= ctx->cx_data_sz / 2)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400941 {
James Berrybc715112011-10-12 11:18:50 -0400942 comp_data_state = vp8_get_compressed_data(ctx->cpi,
943 &lib_flags,
944 &size,
945 cx_data,
946 cx_data_end,
947 &dst_time_stamp,
948 &dst_end_time_stamp,
949 !img);
950
951 if(comp_data_state == VPX_CODEC_CORRUPT_FRAME)
952 return VPX_CODEC_CORRUPT_FRAME;
953 else if(comp_data_state == -1)
954 break;
955
John Koleszar0ea50ce2010-05-18 11:58:33 -0400956 if (size)
957 {
958 vpx_codec_pts_t round, delta;
959 vpx_codec_cx_pkt_t pkt;
960 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
961
962 /* Add the frame packet to the list of returned packets. */
Dmitry Kovalev947748e2014-04-25 17:39:48 -0700963 round = (vpx_codec_pts_t)10000000
James Zern429743c2012-08-07 17:12:10 -0700964 * ctx->cfg.g_timebase.num / 2 - 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400965 delta = (dst_end_time_stamp - dst_time_stamp);
966 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400967 pkt.data.frame.pts =
968 (dst_time_stamp * ctx->cfg.g_timebase.den + round)
969 / ctx->cfg.g_timebase.num / 10000000;
Yaowu Xud71ba032012-08-17 10:05:35 -0700970 pkt.data.frame.duration = (unsigned long)
971 ((delta * ctx->cfg.g_timebase.den + round)
972 / ctx->cfg.g_timebase.num / 10000000);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400973 pkt.data.frame.flags = lib_flags << 16;
974
975 if (lib_flags & FRAMEFLAGS_KEY)
976 pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
977
978 if (!cpi->common.show_frame)
979 {
980 pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
981
John Koleszar0164a1c2012-05-21 14:30:56 -0700982 /* This timestamp should be as close as possible to the
983 * prior PTS so that if a decoder uses pts to schedule when
984 * to do this, we start right after last frame was decoded.
985 * Invisible frames have no duration.
986 */
Frank Galligan45e64942010-10-05 17:46:37 -0400987 pkt.data.frame.pts = ((cpi->last_time_stamp_seen
988 * ctx->cfg.g_timebase.den + round)
989 / ctx->cfg.g_timebase.num / 10000000) + 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400990 pkt.data.frame.duration = 0;
991 }
992
John Koleszar37de0b82011-07-07 10:38:23 -0400993 if (cpi->droppable)
994 pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE;
995
Stefan Holmer7296b3f2011-06-13 16:42:27 +0200996 if (cpi->output_partition)
997 {
998 int i;
999 const int num_partitions =
1000 (1 << cpi->common.multi_token_partition) + 1;
Attila Nagy0afcc762011-07-20 14:09:42 +03001001
1002 pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT;
1003
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001004 for (i = 0; i < num_partitions; ++i)
1005 {
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001006#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1007 pkt.data.frame.buf = cpi->partition_d[i];
1008#else
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001009 pkt.data.frame.buf = cx_data;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001010 cx_data += cpi->partition_sz[i];
1011 cx_data_sz -= cpi->partition_sz[i];
1012#endif
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001013 pkt.data.frame.sz = cpi->partition_sz[i];
1014 pkt.data.frame.partition_id = i;
1015 /* don't set the fragment bit for the last partition */
Attila Nagy0afcc762011-07-20 14:09:42 +03001016 if (i == (num_partitions - 1))
1017 pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT;
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001018 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001019 }
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001020#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1021 /* In lagged mode the encoder can buffer multiple frames.
1022 * We don't want this in partitioned output because
1023 * partitions are spread all over the output buffer.
1024 * So, force an exit!
1025 */
1026 cx_data_sz -= ctx->cx_data_sz / 2;
1027#endif
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001028 }
1029 else
1030 {
1031 pkt.data.frame.buf = cx_data;
1032 pkt.data.frame.sz = size;
1033 pkt.data.frame.partition_id = -1;
1034 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1035 cx_data += size;
1036 cx_data_sz -= size;
1037 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001038 }
1039 }
1040 }
1041
1042 return res;
1043}
1044
1045
1046static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx,
1047 vpx_codec_iter_t *iter)
1048{
1049 return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1050}
1051
1052static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001053 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001054{
1055 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1056
1057 if (data)
1058 {
1059 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1060 YV12_BUFFER_CONFIG sd;
1061
1062 image2yuvconfig(&frame->img, &sd);
1063 vp8_set_reference(ctx->cpi, frame->frame_type, &sd);
1064 return VPX_CODEC_OK;
1065 }
1066 else
1067 return VPX_CODEC_INVALID_PARAM;
1068
1069}
1070
1071static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001072 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001073{
1074
1075 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1076
1077 if (data)
1078 {
1079 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1080 YV12_BUFFER_CONFIG sd;
1081
1082 image2yuvconfig(&frame->img, &sd);
1083 vp8_get_reference(ctx->cpi, frame->frame_type, &sd);
1084 return VPX_CODEC_OK;
1085 }
1086 else
1087 return VPX_CODEC_INVALID_PARAM;
1088}
1089
1090static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001091 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001092{
James Zern76640f82010-08-20 16:06:56 -04001093#if CONFIG_POSTPROC
John Koleszar0ea50ce2010-05-18 11:58:33 -04001094 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
1095
1096 if (data)
1097 {
1098 ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
1099 return VPX_CODEC_OK;
1100 }
1101 else
1102 return VPX_CODEC_INVALID_PARAM;
James Zern76640f82010-08-20 16:06:56 -04001103#else
1104 (void)ctx;
James Zern76640f82010-08-20 16:06:56 -04001105 (void)args;
1106 return VPX_CODEC_INCAPABLE;
1107#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -04001108}
1109
1110
1111static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx)
1112{
1113
1114 YV12_BUFFER_CONFIG sd;
Fritz Koenig647df002010-11-04 16:03:36 -07001115 vp8_ppflags_t flags = {0};
John Koleszar0ea50ce2010-05-18 11:58:33 -04001116
Fritz Koenig647df002010-11-04 16:03:36 -07001117 if (ctx->preview_ppcfg.post_proc_flag)
1118 {
1119 flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
1120 flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
1121 flags.noise_level = ctx->preview_ppcfg.noise_level;
1122 }
1123
1124 if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags))
John Koleszar0ea50ce2010-05-18 11:58:33 -04001125 {
1126
1127 /*
James Zern6cd4a102010-05-20 23:22:39 -04001128 vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001129 sd.y_width + 2*VP8BORDERINPIXELS,
1130 sd.y_height + 2*VP8BORDERINPIXELS,
1131 1,
1132 sd.buffer_alloc);
1133 vpx_img_set_rect(&ctx->preview_img,
1134 VP8BORDERINPIXELS, VP8BORDERINPIXELS,
1135 sd.y_width, sd.y_height);
1136 */
1137
1138 ctx->preview_img.bps = 12;
John Koleszarb6c71912010-05-24 21:45:05 -04001139 ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer;
1140 ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer;
1141 ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001142
James Zern4fc6c882013-07-12 14:11:53 -07001143 ctx->preview_img.fmt = VPX_IMG_FMT_I420;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001144 ctx->preview_img.x_chroma_shift = 1;
1145 ctx->preview_img.y_chroma_shift = 1;
1146
James Berryddacf1c2011-02-08 14:23:18 -05001147 ctx->preview_img.d_w = sd.y_width;
1148 ctx->preview_img.d_h = sd.y_height;
John Koleszarb6c71912010-05-24 21:45:05 -04001149 ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride;
1150 ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride;
1151 ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001152 ctx->preview_img.w = sd.y_width;
1153 ctx->preview_img.h = sd.y_height;
1154
1155 return &ctx->preview_img;
1156 }
1157 else
1158 return NULL;
1159}
1160
1161static vpx_codec_err_t vp8e_update_entropy(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_entropy(ctx->cpi, update);
1166 return VPX_CODEC_OK;
1167
1168}
1169
1170static vpx_codec_err_t vp8e_update_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001171 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001172{
1173 int update = va_arg(args, int);
1174 vp8_update_reference(ctx->cpi, update);
1175 return VPX_CODEC_OK;
1176}
1177
1178static vpx_codec_err_t vp8e_use_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001179 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001180{
1181 int reference_flag = va_arg(args, int);
1182 vp8_use_as_reference(ctx->cpi, reference_flag);
1183 return VPX_CODEC_OK;
1184}
1185
Marcoaf898b52014-11-10 13:07:05 -08001186static vpx_codec_err_t vp8e_set_frame_flags(vpx_codec_alg_priv_t *ctx,
1187 va_list args)
1188{
1189 int frame_flags = va_arg(args, int);
1190 ctx->control_frame_flags = frame_flags;
1191 return set_reference_and_update(ctx, frame_flags);
1192}
1193
1194static vpx_codec_err_t vp8e_set_temporal_layer_id(vpx_codec_alg_priv_t *ctx,
1195 va_list args)
1196{
1197 int layer_id = va_arg(args, int);
1198 if (layer_id < 0 || layer_id >= (int)ctx->cfg.ts_number_layers) {
1199 return VPX_CODEC_INVALID_PARAM;
1200 }
1201 ctx->cpi->temporal_layer_id = layer_id;
1202 return VPX_CODEC_OK;
1203}
1204
John Koleszar0ea50ce2010-05-18 11:58:33 -04001205static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001206 va_list args)
1207{
1208 vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
1209
1210 if (data)
1211 {
1212 vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
1213
1214 if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols, roi->delta_q, roi->delta_lf, roi->static_threshold))
1215 return VPX_CODEC_OK;
1216 else
1217 return VPX_CODEC_INVALID_PARAM;
1218 }
1219 else
1220 return VPX_CODEC_INVALID_PARAM;
1221}
1222
1223
1224static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001225 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001226{
1227 vpx_active_map_t *data = va_arg(args, vpx_active_map_t *);
1228
1229 if (data)
1230 {
1231
1232 vpx_active_map_t *map = (vpx_active_map_t *)data;
1233
1234 if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols))
1235 return VPX_CODEC_OK;
1236 else
1237 return VPX_CODEC_INVALID_PARAM;
1238 }
1239 else
1240 return VPX_CODEC_INVALID_PARAM;
1241}
1242
1243static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001244 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001245{
1246
1247 vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *);
1248
1249 if (data)
1250 {
1251 int res;
1252 vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data ;
Frank Galliganbc45f232013-01-11 15:34:05 -08001253 res = vp8_set_internal_size(ctx->cpi,
1254 (VPX_SCALING)scalemode.h_scaling_mode,
1255 (VPX_SCALING)scalemode.v_scaling_mode);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001256
1257 if (!res)
1258 {
1259 /*force next frame a key frame to effect scaling mode */
1260 ctx->next_frame_flag |= FRAMEFLAGS_KEY;
1261 return VPX_CODEC_OK;
1262 }
1263 else
1264 return VPX_CODEC_INVALID_PARAM;
1265 }
1266 else
1267 return VPX_CODEC_INVALID_PARAM;
1268}
1269
1270
1271static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] =
1272{
1273 {VP8_SET_REFERENCE, vp8e_set_reference},
1274 {VP8_COPY_REFERENCE, vp8e_get_reference},
1275 {VP8_SET_POSTPROC, vp8e_set_previewpp},
1276 {VP8E_UPD_ENTROPY, vp8e_update_entropy},
1277 {VP8E_UPD_REFERENCE, vp8e_update_reference},
1278 {VP8E_USE_REFERENCE, vp8e_use_reference},
Marcoaf898b52014-11-10 13:07:05 -08001279 {VP8E_SET_FRAME_FLAGS, vp8e_set_frame_flags},
1280 {VP8E_SET_TEMPORAL_LAYER_ID, vp8e_set_temporal_layer_id},
John Koleszar0ea50ce2010-05-18 11:58:33 -04001281 {VP8E_SET_ROI_MAP, vp8e_set_roi_map},
1282 {VP8E_SET_ACTIVEMAP, vp8e_set_activemap},
1283 {VP8E_SET_SCALEMODE, vp8e_set_scalemode},
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001284 {VP8E_SET_CPUUSED, set_cpu_used},
1285 {VP8E_SET_NOISE_SENSITIVITY, set_noise_sensitivity},
1286 {VP8E_SET_ENABLEAUTOALTREF, set_enable_auto_alt_ref},
1287 {VP8E_SET_SHARPNESS, set_sharpness},
1288 {VP8E_SET_STATIC_THRESHOLD, set_static_thresh},
1289 {VP8E_SET_TOKEN_PARTITIONS, set_token_partitions},
1290 {VP8E_GET_LAST_QUANTIZER, get_quantizer},
1291 {VP8E_GET_LAST_QUANTIZER_64, get_quantizer64},
1292 {VP8E_SET_ARNR_MAXFRAMES, set_arnr_max_frames},
1293 {VP8E_SET_ARNR_STRENGTH , set_arnr_strength},
1294 {VP8E_SET_ARNR_TYPE , set_arnr_type},
1295 {VP8E_SET_TUNING, set_tuning},
1296 {VP8E_SET_CQ_LEVEL, set_cq_level},
1297 {VP8E_SET_MAX_INTRA_BITRATE_PCT, set_rc_max_intra_bitrate_pct},
Marcoaf898b52014-11-10 13:07:05 -08001298 {VP8E_SET_SCREEN_CONTENT_MODE, set_screen_content_mode},
John Koleszar0ea50ce2010-05-18 11:58:33 -04001299 { -1, NULL},
1300};
1301
1302static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] =
1303{
1304 {
1305 0,
1306 {
1307 0, /* g_usage */
1308 0, /* g_threads */
1309 0, /* g_profile */
1310
1311 320, /* g_width */
1312 240, /* g_height */
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001313 VPX_BITS_8, /* g_bit_depth */
1314 8, /* g_input_bit_depth */
1315
John Koleszar0ea50ce2010-05-18 11:58:33 -04001316 {1, 30}, /* g_timebase */
1317
1318 0, /* g_error_resilient */
1319
1320 VPX_RC_ONE_PASS, /* g_pass */
1321
1322 0, /* g_lag_in_frames */
1323
John Koleszar23216212010-09-02 09:32:03 -04001324 0, /* rc_dropframe_thresh */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001325 0, /* rc_resize_allowed */
Adrian Grangef7bd1272014-04-09 14:51:29 -07001326 1, /* rc_scaled_width */
1327 1, /* rc_scaled_height */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001328 60, /* rc_resize_down_thresold */
1329 30, /* rc_resize_up_thresold */
1330
1331 VPX_VBR, /* rc_end_usage */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001332 {0}, /* rc_twopass_stats_in */
Pengchong Jinf349b072014-07-14 09:13:38 -07001333 {0}, /* rc_firstpass_mb_stats_in */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001334 256, /* rc_target_bandwidth */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001335 4, /* rc_min_quantizer */
1336 63, /* rc_max_quantizer */
John Koleszarc99f9d72011-04-11 11:29:23 -04001337 100, /* rc_undershoot_pct */
1338 100, /* rc_overshoot_pct */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001339
1340 6000, /* rc_max_buffer_size */
1341 4000, /* rc_buffer_initial_size; */
1342 5000, /* rc_buffer_optimal_size; */
1343
1344 50, /* rc_two_pass_vbrbias */
1345 0, /* rc_two_pass_vbrmin_section */
1346 400, /* rc_two_pass_vbrmax_section */
1347
1348 /* keyframing settings (kf) */
1349 VPX_KF_AUTO, /* g_kfmode*/
1350 0, /* kf_min_dist */
Ralph Giles2a0d7b12012-01-05 10:39:38 -06001351 128, /* kf_max_dist */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001352
Ivan Maltz01b35c32013-09-05 08:55:47 -07001353 VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */
Minghai Shange8998592014-07-14 11:24:17 -07001354 {0},
Minghai Shang8c196b22014-02-26 13:30:50 -08001355 {0}, /* ss_target_bitrate */
Adrian Grange217591f2011-10-06 15:49:11 -07001356 1, /* ts_number_layers */
1357 {0}, /* ts_target_bitrate */
1358 {0}, /* ts_rate_decimator */
1359 0, /* ts_periodicity */
1360 {0}, /* ts_layer_id */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001361 }},
John Koleszar0ea50ce2010-05-18 11:58:33 -04001362};
1363
1364
1365#ifndef VERSION_STRING
1366#define VERSION_STRING
1367#endif
John Koleszarfa7a55b2010-09-21 10:35:52 -04001368CODEC_INTERFACE(vpx_codec_vp8_cx) =
John Koleszar0ea50ce2010-05-18 11:58:33 -04001369{
John Koleszar317a6662010-06-10 12:08:01 -04001370 "WebM Project VP8 Encoder" VERSION_STRING,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001371 VPX_CODEC_INTERNAL_ABI_VERSION,
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001372 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR |
1373 VPX_CODEC_CAP_OUTPUT_PARTITION,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001374 /* vpx_codec_caps_t caps; */
1375 vp8e_init, /* vpx_codec_init_fn_t init; */
1376 vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */
1377 vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001378 {
Dmitry Kovalevcd6d9842014-08-20 17:16:28 -07001379 NULL, /* vpx_codec_peek_si_fn_t peek_si; */
1380 NULL, /* vpx_codec_get_si_fn_t get_si; */
1381 NULL, /* vpx_codec_decode_fn_t decode; */
1382 NULL, /* vpx_codec_frame_get_fn_t frame_get; */
Adrian Grangef29961d2015-02-12 09:31:40 -08001383 NULL, /* vpx_codec_set_fb_fn_t set_fb_fn; */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001384 },
1385 {
Jim Bankoski8a774e12014-07-31 06:27:57 -07001386 1, /* 1 cfg map */
Adrian Grangef29961d2015-02-12 09:31:40 -08001387 vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t cfg_maps; */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001388 vp8e_encode, /* vpx_codec_encode_fn_t encode; */
Adrian Grangef29961d2015-02-12 09:31:40 -08001389 vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t get_cx_data; */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001390 vp8e_set_config,
Dmitry Kovalevcd6d9842014-08-20 17:16:28 -07001391 NULL,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001392 vp8e_get_preview,
Yunqing Wangaa7335e2011-10-25 15:14:16 -04001393 vp8e_mr_alloc_mem,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001394 } /* encoder functions */
1395};