blob: 257d2a0c4e501c07aa4a3bf111466586dcbf3fff [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"
Johannd5d92892015-04-17 16:11:38 -040014#include "./vpx_dsp_rtcd.h"
Johann14ef4ae2015-04-15 09:27:00 -040015#include "./vpx_scale_rtcd.h"
John Koleszarb7492342010-05-24 11:39:59 -040016#include "vpx/vpx_codec.h"
17#include "vpx/internal/vpx_codec_internal.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040018#include "vpx_version.h"
Dmitry Kovalevb08fab82014-09-02 18:26:53 -070019#include "vpx_mem/vpx_mem.h"
Ronald S. Bultje54d48952015-09-30 11:07:35 -040020#include "vpx_ports/vpx_once.h"
John Koleszar02321de2011-02-10 14:41:38 -050021#include "vp8/encoder/onyx_int.h"
John Koleszar2bf8fb52012-05-02 16:37:37 -070022#include "vpx/vp8cx.h"
John Koleszarbb7dd5b2010-10-14 16:40:12 -040023#include "vp8/encoder/firstpass.h"
John Koleszar02321de2011-02-10 14:41:38 -050024#include "vp8/common/onyx.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040025#include <stdlib.h>
26#include <string.h>
27
John Koleszar0ea50ce2010-05-18 11:58:33 -040028struct vp8_extracfg
29{
30 struct vpx_codec_pkt_list *pkt_list;
John Koleszar0ea50ce2010-05-18 11:58:33 -040031 int cpu_used; /** available cpu percentage in 1/16*/
32 unsigned int enable_auto_alt_ref; /** if encoder decides to uses alternate reference frame */
33 unsigned int noise_sensitivity;
34 unsigned int Sharpness;
35 unsigned int static_thresh;
36 unsigned int token_partitions;
37 unsigned int arnr_max_frames; /* alt_ref Noise Reduction Max Frame Count */
38 unsigned int arnr_strength; /* alt_ref Noise Reduction Strength */
39 unsigned int arnr_type; /* alt_ref filter type */
John Koleszarb0da9b32010-12-17 09:43:39 -050040 vp8e_tuning tuning;
Paul Wilkinse0846c92011-01-07 18:29:37 +000041 unsigned int cq_level; /* constrained quality level */
John Koleszar1654ae92011-07-28 09:17:32 -040042 unsigned int rc_max_intra_bitrate_pct;
Marcoaf898b52014-11-10 13:07:05 -080043 unsigned int screen_content_mode;
John Koleszar0ea50ce2010-05-18 11:58:33 -040044
45};
46
Dmitry Kovalev48274c62014-08-21 11:21:18 -070047static struct vp8_extracfg default_extracfg = {
48 NULL,
John Koleszar0ea50ce2010-05-18 11:58:33 -040049#if !(CONFIG_REALTIME_ONLY)
Dmitry Kovalev48274c62014-08-21 11:21:18 -070050 0, /* cpu_used */
John Koleszar0ea50ce2010-05-18 11:58:33 -040051#else
Dmitry Kovalev48274c62014-08-21 11:21:18 -070052 4, /* cpu_used */
John Koleszar0ea50ce2010-05-18 11:58:33 -040053#endif
Dmitry Kovalev48274c62014-08-21 11:21:18 -070054 0, /* enable_auto_alt_ref */
55 0, /* noise_sensitivity */
56 0, /* Sharpness */
57 0, /* static_thresh */
Attila Nagy52cf4dc2012-02-09 12:37:03 +020058#if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
Dmitry Kovalev48274c62014-08-21 11:21:18 -070059 VP8_EIGHT_TOKENPARTITION,
Attila Nagy52cf4dc2012-02-09 12:37:03 +020060#else
Dmitry Kovalev48274c62014-08-21 11:21:18 -070061 VP8_ONE_TOKENPARTITION, /* token_partitions */
Attila Nagy52cf4dc2012-02-09 12:37:03 +020062#endif
Dmitry Kovalev48274c62014-08-21 11:21:18 -070063 0, /* arnr_max_frames */
64 3, /* arnr_strength */
65 3, /* arnr_type*/
66 0, /* tuning*/
67 10, /* cq_level */
68 0, /* rc_max_intra_bitrate_pct */
Marcoaf898b52014-11-10 13:07:05 -080069 0, /* screen_content_mode */
John Koleszar0ea50ce2010-05-18 11:58:33 -040070};
71
72struct vpx_codec_alg_priv
73{
74 vpx_codec_priv_t base;
75 vpx_codec_enc_cfg_t cfg;
76 struct vp8_extracfg vp8_cfg;
77 VP8_CONFIG oxcf;
John Koleszarb0056c32011-12-20 16:54:54 -080078 struct VP8_COMP *cpi;
John Koleszar0ea50ce2010-05-18 11:58:33 -040079 unsigned char *cx_data;
80 unsigned int cx_data_sz;
81 vpx_image_t preview_img;
82 unsigned int next_frame_flag;
83 vp8_postproc_cfg_t preview_ppcfg;
John Koleszar0164a1c2012-05-21 14:30:56 -070084 /* pkt_list size depends on the maximum number of lagged frames allowed. */
85 vpx_codec_pkt_list_decl(64) pkt_list;
John Koleszar0ea50ce2010-05-18 11:58:33 -040086 unsigned int fixed_kf_cntr;
Marcoaf898b52014-11-10 13:07:05 -080087 vpx_enc_frame_flags_t control_frame_flags;
John Koleszar0ea50ce2010-05-18 11:58:33 -040088};
89
90
91static vpx_codec_err_t
92update_error_state(vpx_codec_alg_priv_t *ctx,
93 const struct vpx_internal_error_info *error)
94{
95 vpx_codec_err_t res;
96
97 if ((res = error->error_code))
98 ctx->base.err_detail = error->has_detail
99 ? error->detail
100 : NULL;
101
102 return res;
103}
104
105
John Koleszar79e2b1f2010-11-17 09:08:47 -0500106#undef ERROR
John Koleszar0ea50ce2010-05-18 11:58:33 -0400107#define ERROR(str) do {\
108 ctx->base.err_detail = str;\
109 return VPX_CODEC_INVALID_PARAM;\
110 } while(0)
111
112#define RANGE_CHECK(p,memb,lo,hi) do {\
Guillermo Ballester Valor5a726202010-06-11 14:33:49 -0400113 if(!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
John Koleszar0ea50ce2010-05-18 11:58:33 -0400114 ERROR(#memb " out of range ["#lo".."#hi"]");\
115 } while(0)
116
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400117#define RANGE_CHECK_HI(p,memb,hi) do {\
118 if(!((p)->memb <= (hi))) \
119 ERROR(#memb " out of range [.."#hi"]");\
120 } while(0)
121
John Koleszar0ea50ce2010-05-18 11:58:33 -0400122#define RANGE_CHECK_LO(p,memb,lo) do {\
123 if(!((p)->memb >= (lo))) \
124 ERROR(#memb " out of range ["#lo"..]");\
125 } while(0)
126
127#define RANGE_CHECK_BOOL(p,memb) do {\
128 if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
129 } while(0)
130
131static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
132 const vpx_codec_enc_cfg_t *cfg,
James Berryc1c47e82011-12-07 15:48:00 -0500133 const struct vp8_extracfg *vp8_cfg,
134 int finalize)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400135{
Attila Nagy1aadced2011-04-12 15:01:22 +0300136 RANGE_CHECK(cfg, g_w, 1, 16383); /* 14 bits available */
137 RANGE_CHECK(cfg, g_h, 1, 16383); /* 14 bits available */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400138 RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
Johnny Klonarisd02aa042015-05-31 23:29:16 -0700139 RANGE_CHECK(cfg, g_timebase.num, 1, 1000000000);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400140 RANGE_CHECK_HI(cfg, g_profile, 3);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400141 RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
John Koleszar2d03f072011-01-28 11:56:18 -0500142 RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400143 RANGE_CHECK_HI(cfg, g_threads, 64);
Yunqing Wang7f009972012-07-11 11:43:51 -0700144#if CONFIG_REALTIME_ONLY
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400145 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
Yunqing Wang7f009972012-07-11 11:43:51 -0700146#elif CONFIG_MULTI_RES_ENCODING
147 if (ctx->base.enc.total_encoders > 1)
148 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
149#else
150 RANGE_CHECK_HI(cfg, g_lag_in_frames, 25);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400151#endif
Deb Mukherjeee378a892013-08-29 16:21:44 -0700152 RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
John Koleszarc99f9d72011-04-11 11:29:23 -0400153 RANGE_CHECK_HI(cfg, rc_undershoot_pct, 1000);
154 RANGE_CHECK_HI(cfg, rc_overshoot_pct, 1000);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400155 RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400156 RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
Yunqing Wang7f009972012-07-11 11:43:51 -0700157
158/* TODO: add spatial re-sampling support and frame dropping in
159 * multi-res-encoder.*/
160#if CONFIG_MULTI_RES_ENCODING
161 if (ctx->base.enc.total_encoders > 1)
Yunqing Wang7f009972012-07-11 11:43:51 -0700162 RANGE_CHECK_HI(cfg, rc_resize_allowed, 0);
Yunqing Wang7f009972012-07-11 11:43:51 -0700163#else
164 RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
Yunqing Wang7f009972012-07-11 11:43:51 -0700165#endif
Yunqing Wang4066c8b2012-06-08 11:17:50 -0400166 RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400167 RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
168 RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
Yunqing Wang7f009972012-07-11 11:43:51 -0700169
170#if CONFIG_REALTIME_ONLY
John Koleszar0ea50ce2010-05-18 11:58:33 -0400171 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
Yunqing Wang7f009972012-07-11 11:43:51 -0700172#elif CONFIG_MULTI_RES_ENCODING
173 if (ctx->base.enc.total_encoders > 1)
174 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
175#else
176 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400177#endif
178
179 /* VP8 does not support a lower bound on the keyframe interval in
180 * automatic keyframe placement mode.
181 */
182 if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist
183 && cfg->kf_min_dist > 0)
184 ERROR("kf_min_dist not supported in auto mode, use 0 "
185 "or kf_max_dist instead.");
186
187 RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref);
Yunqing Wang4fd81a92011-04-11 15:55:04 -0400188 RANGE_CHECK(vp8_cfg, cpu_used, -16, 16);
John Koleszarc8f4c182012-05-08 11:23:42 -0700189
190#if CONFIG_REALTIME_ONLY && !CONFIG_TEMPORAL_DENOISING
191 RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0);
192#else
193 RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6);
Stefan Holmer9c411432012-03-06 10:48:18 +0100194#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -0400195
Yunqing Wang7f009972012-07-11 11:43:51 -0700196 RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION,
197 VP8_EIGHT_TOKENPARTITION);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400198 RANGE_CHECK_HI(vp8_cfg, Sharpness, 7);
Adrian Grange8ee72842010-09-30 10:06:09 +0100199 RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
Frank Galligan15542722010-10-04 21:12:22 -0400200 RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6);
Adrian Grange8ee72842010-09-30 10:06:09 +0100201 RANGE_CHECK(vp8_cfg, arnr_type, 1, 3);
Paul Wilkinse0846c92011-01-07 18:29:37 +0000202 RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
Marco976f7f42015-04-28 08:29:48 -0700203 RANGE_CHECK_HI(vp8_cfg, screen_content_mode, 2);
Deb Mukherjeee378a892013-08-29 16:21:44 -0700204 if (finalize && (cfg->rc_end_usage == VPX_CQ || cfg->rc_end_usage == VPX_Q))
James Berryc1c47e82011-12-07 15:48:00 -0500205 RANGE_CHECK(vp8_cfg, cq_level,
206 cfg->rc_min_quantizer, cfg->rc_max_quantizer);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400207
Attila Nagycb791aa2011-01-10 11:14:10 +0200208#if !(CONFIG_REALTIME_ONLY)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400209 if (cfg->g_pass == VPX_RC_LAST_PASS)
210 {
Adrian Grangeed40ff92011-03-10 11:32:48 -0800211 size_t packet_sz = sizeof(FIRSTPASS_STATS);
Yaowu Xud71ba032012-08-17 10:05:35 -0700212 int n_packets = (int)(cfg->rc_twopass_stats_in.sz /
213 packet_sz);
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400214 FIRSTPASS_STATS *stats;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400215
216 if (!cfg->rc_twopass_stats_in.buf)
217 ERROR("rc_twopass_stats_in.buf not set.");
218
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400219 if (cfg->rc_twopass_stats_in.sz % packet_sz)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400220 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
221
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400222 if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400223 ERROR("rc_twopass_stats_in requires at least two packets.");
224
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400225 stats = (void*)((char *)cfg->rc_twopass_stats_in.buf
226 + (n_packets - 1) * packet_sz);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400227
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400228 if ((int)(stats->count + 0.5) != n_packets - 1)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400229 ERROR("rc_twopass_stats_in missing EOS stats packet");
230 }
Attila Nagycb791aa2011-01-10 11:14:10 +0200231#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -0400232
Adrian Grange217591f2011-10-06 15:49:11 -0700233 RANGE_CHECK(cfg, ts_number_layers, 1, 5);
234
235 if (cfg->ts_number_layers > 1)
236 {
Scott Graham92963df2012-05-08 11:45:35 -0700237 unsigned int i;
Adrian Grange217591f2011-10-06 15:49:11 -0700238 RANGE_CHECK_HI(cfg, ts_periodicity, 16);
239
240 for (i=1; i<cfg->ts_number_layers; i++)
Debargha Mukherjee1c8567f2015-09-04 17:31:51 -0700241 if (cfg->ts_target_bitrate[i] <= cfg->ts_target_bitrate[i-1] &&
Marcoaf898b52014-11-10 13:07:05 -0800242 cfg->rc_target_bitrate > 0)
Adrian Grange217591f2011-10-06 15:49:11 -0700243 ERROR("ts_target_bitrate entries are not strictly increasing");
244
245 RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers-1], 1, 1);
246 for (i=cfg->ts_number_layers-2; i>0; i--)
247 if (cfg->ts_rate_decimator[i-1] != 2*cfg->ts_rate_decimator[i])
248 ERROR("ts_rate_decimator factors are not powers of 2");
249
250 RANGE_CHECK_HI(cfg, ts_layer_id[i], cfg->ts_number_layers-1);
251 }
252
Attila Nagy52cf4dc2012-02-09 12:37:03 +0200253#if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
254 if(cfg->g_threads > (1 << vp8_cfg->token_partitions))
255 ERROR("g_threads cannot be bigger than number of token partitions");
256#endif
257
John Koleszar0ea50ce2010-05-18 11:58:33 -0400258 return VPX_CODEC_OK;
259}
260
261
262static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
263 const vpx_image_t *img)
264{
265 switch (img->fmt)
266 {
James Zern6cd4a102010-05-20 23:22:39 -0400267 case VPX_IMG_FMT_YV12:
268 case VPX_IMG_FMT_I420:
269 case VPX_IMG_FMT_VPXI420:
270 case VPX_IMG_FMT_VPXYV12:
John Koleszar0ea50ce2010-05-18 11:58:33 -0400271 break;
272 default:
273 ERROR("Invalid image format. Only YV12 and I420 images are supported");
274 }
275
276 if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
277 ERROR("Image size must match encoder init configuration size");
278
279 return VPX_CODEC_OK;
280}
281
282
283static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf,
284 vpx_codec_enc_cfg_t cfg,
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400285 struct vp8_extracfg vp8_cfg,
286 vpx_codec_priv_enc_mr_cfg_t *mr_cfg)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400287{
288 oxcf->multi_threaded = cfg.g_threads;
289 oxcf->Version = cfg.g_profile;
290
291 oxcf->Width = cfg.g_w;
292 oxcf->Height = cfg.g_h;
John Koleszarbdd35c12011-11-11 10:47:20 -0800293 oxcf->timebase = cfg.g_timebase;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400294
Adrian Grange217591f2011-10-06 15:49:11 -0700295 oxcf->error_resilient_mode = cfg.g_error_resilient;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400296
297 switch (cfg.g_pass)
298 {
299 case VPX_RC_ONE_PASS:
300 oxcf->Mode = MODE_BESTQUALITY;
301 break;
302 case VPX_RC_FIRST_PASS:
303 oxcf->Mode = MODE_FIRSTPASS;
304 break;
305 case VPX_RC_LAST_PASS:
306 oxcf->Mode = MODE_SECONDPASS_BEST;
307 break;
308 }
309
James Zern7b0b6a22012-06-13 11:59:12 -0700310 if (cfg.g_pass == VPX_RC_FIRST_PASS || cfg.g_pass == VPX_RC_ONE_PASS)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400311 {
Adrian Grange217591f2011-10-06 15:49:11 -0700312 oxcf->allow_lag = 0;
313 oxcf->lag_in_frames = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400314 }
315 else
316 {
Adrian Grange217591f2011-10-06 15:49:11 -0700317 oxcf->allow_lag = (cfg.g_lag_in_frames) > 0;
318 oxcf->lag_in_frames = cfg.g_lag_in_frames;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400319 }
320
321 oxcf->allow_df = (cfg.rc_dropframe_thresh > 0);
322 oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh;
323
324 oxcf->allow_spatial_resampling = cfg.rc_resize_allowed;
325 oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh;
326 oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh;
327
Deb Mukherjeee378a892013-08-29 16:21:44 -0700328 if (cfg.rc_end_usage == VPX_VBR) {
329 oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
330 } else if (cfg.rc_end_usage == VPX_CBR) {
331 oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
332 } else if (cfg.rc_end_usage == VPX_CQ) {
333 oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
334 } else if (cfg.rc_end_usage == VPX_Q) {
335 oxcf->end_usage = USAGE_CONSTANT_QUALITY;
Paul Wilkinse0846c92011-01-07 18:29:37 +0000336 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400337
Adrian Grange217591f2011-10-06 15:49:11 -0700338 oxcf->target_bandwidth = cfg.rc_target_bitrate;
John Koleszar1654ae92011-07-28 09:17:32 -0400339 oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400340
Adrian Grange217591f2011-10-06 15:49:11 -0700341 oxcf->best_allowed_q = cfg.rc_min_quantizer;
342 oxcf->worst_allowed_q = cfg.rc_max_quantizer;
343 oxcf->cq_level = vp8_cfg.cq_level;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400344 oxcf->fixed_q = -1;
345
Adrian Grange217591f2011-10-06 15:49:11 -0700346 oxcf->under_shoot_pct = cfg.rc_undershoot_pct;
347 oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400348
Adrian Grangee4793792012-01-13 14:09:40 -0800349 oxcf->maximum_buffer_size_in_ms = cfg.rc_buf_sz;
350 oxcf->starting_buffer_level_in_ms = cfg.rc_buf_initial_sz;
351 oxcf->optimal_buffer_level_in_ms = cfg.rc_buf_optimal_sz;
352
Adrian Grange217591f2011-10-06 15:49:11 -0700353 oxcf->maximum_buffer_size = cfg.rc_buf_sz;
354 oxcf->starting_buffer_level = cfg.rc_buf_initial_sz;
355 oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400356
Adrian Grange217591f2011-10-06 15:49:11 -0700357 oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400358 oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct;
359 oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct;
360
Adrian Grange217591f2011-10-06 15:49:11 -0700361 oxcf->auto_key = cfg.kf_mode == VPX_KF_AUTO
362 && cfg.kf_min_dist != cfg.kf_max_dist;
Adrian Grange217591f2011-10-06 15:49:11 -0700363 oxcf->key_freq = cfg.kf_max_dist;
364
365 oxcf->number_of_layers = cfg.ts_number_layers;
366 oxcf->periodicity = cfg.ts_periodicity;
367
368 if (oxcf->number_of_layers > 1)
369 {
370 memcpy (oxcf->target_bitrate, cfg.ts_target_bitrate,
James Zernf274c212015-04-23 20:42:19 -0700371 sizeof(cfg.ts_target_bitrate));
Adrian Grange217591f2011-10-06 15:49:11 -0700372 memcpy (oxcf->rate_decimator, cfg.ts_rate_decimator,
James Zernf274c212015-04-23 20:42:19 -0700373 sizeof(cfg.ts_rate_decimator));
Adrian Grange217591f2011-10-06 15:49:11 -0700374 memcpy (oxcf->layer_id, cfg.ts_layer_id, sizeof(cfg.ts_layer_id));
375 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400376
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400377#if CONFIG_MULTI_RES_ENCODING
Yunqing Wangc647ec42011-12-16 16:50:29 -0500378 /* When mr_cfg is NULL, oxcf->mr_total_resolutions and oxcf->mr_encoder_id
379 * are both memset to 0, which ensures the correct logic under this
380 * situation.
381 */
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400382 if(mr_cfg)
383 {
384 oxcf->mr_total_resolutions = mr_cfg->mr_total_resolutions;
385 oxcf->mr_encoder_id = mr_cfg->mr_encoder_id;
386 oxcf->mr_down_sampling_factor.num = mr_cfg->mr_down_sampling_factor.num;
387 oxcf->mr_down_sampling_factor.den = mr_cfg->mr_down_sampling_factor.den;
388 oxcf->mr_low_res_mode_info = mr_cfg->mr_low_res_mode_info;
389 }
Johann80b344d2014-12-16 12:22:10 -0800390#else
391 (void)mr_cfg;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400392#endif
393
Adrian Grange217591f2011-10-06 15:49:11 -0700394 oxcf->cpu_used = vp8_cfg.cpu_used;
395 oxcf->encode_breakout = vp8_cfg.static_thresh;
396 oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref;
397 oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity;
398 oxcf->Sharpness = vp8_cfg.Sharpness;
399 oxcf->token_partitions = vp8_cfg.token_partitions;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400400
Adrian Grange217591f2011-10-06 15:49:11 -0700401 oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in;
402 oxcf->output_pkt_list = vp8_cfg.pkt_list;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400403
Adrian Grange217591f2011-10-06 15:49:11 -0700404 oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
405 oxcf->arnr_strength = vp8_cfg.arnr_strength;
406 oxcf->arnr_type = vp8_cfg.arnr_type;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400407
Adrian Grange217591f2011-10-06 15:49:11 -0700408 oxcf->tuning = vp8_cfg.tuning;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400409
Marcoaf898b52014-11-10 13:07:05 -0800410 oxcf->screen_content_mode = vp8_cfg.screen_content_mode;
411
John Koleszar0ea50ce2010-05-18 11:58:33 -0400412 /*
413 printf("Current VP8 Settings: \n");
414 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
415 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
416 printf("Sharpness: %d\n", oxcf->Sharpness);
417 printf("cpu_used: %d\n", oxcf->cpu_used);
418 printf("Mode: %d\n", oxcf->Mode);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400419 printf("auto_key: %d\n", oxcf->auto_key);
420 printf("key_freq: %d\n", oxcf->key_freq);
421 printf("end_usage: %d\n", oxcf->end_usage);
422 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
John Koleszarc99f9d72011-04-11 11:29:23 -0400423 printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400424 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
425 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
426 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
427 printf("fixed_q: %d\n", oxcf->fixed_q);
428 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
429 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
430 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
431 printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
432 printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
433 printf("allow_df: %d\n", oxcf->allow_df);
434 printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
435 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
436 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
437 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
438 printf("allow_lag: %d\n", oxcf->allow_lag);
439 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
440 printf("play_alternate: %d\n", oxcf->play_alternate);
441 printf("Version: %d\n", oxcf->Version);
442 printf("multi_threaded: %d\n", oxcf->multi_threaded);
443 printf("encode_breakout: %d\n", oxcf->encode_breakout);
444 */
445 return VPX_CODEC_OK;
446}
447
448static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx,
449 const vpx_codec_enc_cfg_t *cfg)
450{
451 vpx_codec_err_t res;
452
Alex Converse581731a2015-01-16 16:02:05 -0800453 if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h)
454 {
455 if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)
456 ERROR("Cannot change width or height after initialization");
457 if ((ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
458 (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
Yaowu Xu47c55ac2015-07-29 16:23:14 -0700459 ERROR("Cannot increase width or height larger than their initial values");
Alex Converse581731a2015-01-16 16:02:05 -0800460 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400461
462 /* Prevent increasing lag_in_frames. This check is stricter than it needs
463 * to be -- the limit is not increasing past the first lag_in_frames
464 * value, but we don't track the initial config, only the last successful
465 * config.
466 */
467 if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
468 ERROR("Cannot increase lag_in_frames");
469
James Berryc1c47e82011-12-07 15:48:00 -0500470 res = validate_config(ctx, cfg, &ctx->vp8_cfg, 0);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400471
472 if (!res)
473 {
474 ctx->cfg = *cfg;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400475 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400476 vp8_change_config(ctx->cpi, &ctx->oxcf);
477 }
478
479 return res;
480}
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();
Johannd5d92892015-04-17 16:11:38 -0400653 vpx_dsp_rtcd();
Johann14ef4ae2015-04-15 09:27:00 -0400654 vpx_scale_rtcd();
John Koleszar8df79e92012-06-15 15:40:13 -0700655
John Koleszar0ea50ce2010-05-18 11:58:33 -0400656 if (!ctx->priv)
657 {
Dmitry Kovalevb08fab82014-09-02 18:26:53 -0700658 struct vpx_codec_alg_priv *priv =
659 (struct vpx_codec_alg_priv *)vpx_calloc(1, sizeof(*priv));
John Koleszar0ea50ce2010-05-18 11:58:33 -0400660
Attila Nagye6db21e2011-02-22 15:02:05 +0200661 if (!priv)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400662 {
Attila Nagye6db21e2011-02-22 15:02:05 +0200663 return VPX_CODEC_MEM_ERROR;
664 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400665
Dmitry Kovalev73edeb02014-08-20 17:02:10 -0700666 ctx->priv = (vpx_codec_priv_t *)priv;
Attila Nagye6db21e2011-02-22 15:02:05 +0200667 ctx->priv->init_flags = ctx->init_flags;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400668
Attila Nagye6db21e2011-02-22 15:02:05 +0200669 if (ctx->config.enc)
670 {
671 /* Update the reference to the config structure to an
672 * internal copy.
John Koleszar0ea50ce2010-05-18 11:58:33 -0400673 */
Dmitry Kovalev73edeb02014-08-20 17:02:10 -0700674 priv->cfg = *ctx->config.enc;
675 ctx->config.enc = &priv->cfg;
Attila Nagye6db21e2011-02-22 15:02:05 +0200676 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400677
Dmitry Kovalev48274c62014-08-21 11:21:18 -0700678 priv->vp8_cfg = default_extracfg;
Attila Nagye6db21e2011-02-22 15:02:05 +0200679 priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400680
Attila Nagye6db21e2011-02-22 15:02:05 +0200681 priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400682
James Berrybac6c222011-10-24 11:50:27 -0400683 if (priv->cx_data_sz < 32768) priv->cx_data_sz = 32768;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400684
Attila Nagye6db21e2011-02-22 15:02:05 +0200685 priv->cx_data = malloc(priv->cx_data_sz);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400686
Attila Nagye6db21e2011-02-22 15:02:05 +0200687 if (!priv->cx_data)
688 {
689 return VPX_CODEC_MEM_ERROR;
690 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400691
Yunqing Wang7f009972012-07-11 11:43:51 -0700692 if(mr_cfg)
693 ctx->priv->enc.total_encoders = mr_cfg->mr_total_resolutions;
694 else
695 ctx->priv->enc.total_encoders = 1;
696
Ronald S. Bultje54d48952015-09-30 11:07:35 -0400697 once(vp8_initialize_enc);
698
James Berryc1c47e82011-12-07 15:48:00 -0500699 res = validate_config(priv, &priv->cfg, &priv->vp8_cfg, 0);
Attila Nagye6db21e2011-02-22 15:02:05 +0200700
701 if (!res)
702 {
Dmitry Kovalev73edeb02014-08-20 17:02:10 -0700703 set_vp8e_config(&priv->oxcf, priv->cfg, priv->vp8_cfg, mr_cfg);
704 priv->cpi = vp8_create_compressor(&priv->oxcf);
705 if (!priv->cpi)
Attila Nagye6db21e2011-02-22 15:02:05 +0200706 res = VPX_CODEC_MEM_ERROR;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400707 }
708 }
709
710 return res;
711}
712
713static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx)
714{
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400715#if CONFIG_MULTI_RES_ENCODING
716 /* Free multi-encoder shared memory */
717 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 -0400718 {
719 LOWER_RES_FRAME_INFO *shared_mem_loc = (LOWER_RES_FRAME_INFO *)ctx->oxcf.mr_low_res_mode_info;
720 free(shared_mem_loc->mb_info);
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400721 free(ctx->oxcf.mr_low_res_mode_info);
Yunqing Wang65dd1572012-05-16 15:06:42 -0400722 }
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400723#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -0400724
725 free(ctx->cx_data);
726 vp8_remove_compressor(&ctx->cpi);
Dmitry Kovalevb08fab82014-09-02 18:26:53 -0700727 vpx_free(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400728 return VPX_CODEC_OK;
729}
730
731static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
732 YV12_BUFFER_CONFIG *yv12)
733{
Johann69dc8762014-08-15 10:50:40 -0700734 const int y_w = img->d_w;
735 const int y_h = img->d_h;
736 const int uv_w = (img->d_w + 1) / 2;
737 const int uv_h = (img->d_h + 1) / 2;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400738 vpx_codec_err_t res = VPX_CODEC_OK;
John Koleszarb6c71912010-05-24 21:45:05 -0400739 yv12->y_buffer = img->planes[VPX_PLANE_Y];
740 yv12->u_buffer = img->planes[VPX_PLANE_U];
741 yv12->v_buffer = img->planes[VPX_PLANE_V];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400742
Johann69dc8762014-08-15 10:50:40 -0700743 yv12->y_crop_width = y_w;
744 yv12->y_crop_height = y_h;
745 yv12->y_width = y_w;
746 yv12->y_height = y_h;
747 yv12->uv_crop_width = uv_w;
748 yv12->uv_crop_height = uv_h;
749 yv12->uv_width = uv_w;
750 yv12->uv_height = uv_h;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400751
John Koleszarb6c71912010-05-24 21:45:05 -0400752 yv12->y_stride = img->stride[VPX_PLANE_Y];
753 yv12->uv_stride = img->stride[VPX_PLANE_U];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400754
John Koleszarb6c71912010-05-24 21:45:05 -0400755 yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400756 return res;
757}
758
759static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
760 unsigned long duration,
761 unsigned long deadline)
762{
763 unsigned int new_qc;
764
765#if !(CONFIG_REALTIME_ONLY)
766 /* Use best quality mode if no deadline is given. */
767 new_qc = MODE_BESTQUALITY;
768
769 if (deadline)
770 {
771 uint64_t duration_us;
772
773 /* Convert duration parameter from stream timebase to microseconds */
774 duration_us = (uint64_t)duration * 1000000
775 * (uint64_t)ctx->cfg.g_timebase.num
776 / (uint64_t)ctx->cfg.g_timebase.den;
777
778 /* If the deadline is more that the duration this frame is to be shown,
779 * use good quality mode. Otherwise use realtime mode.
780 */
781 new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
782 }
783
784#else
785 new_qc = MODE_REALTIME;
786#endif
787
John Koleszar0ea50ce2010-05-18 11:58:33 -0400788 if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS)
789 new_qc = MODE_FIRSTPASS;
790 else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS)
791 new_qc = (new_qc == MODE_BESTQUALITY)
792 ? MODE_SECONDPASS_BEST
793 : MODE_SECONDPASS;
794
795 if (ctx->oxcf.Mode != new_qc)
796 {
797 ctx->oxcf.Mode = new_qc;
798 vp8_change_config(ctx->cpi, &ctx->oxcf);
799 }
800}
801
Marcoaf898b52014-11-10 13:07:05 -0800802static vpx_codec_err_t set_reference_and_update(vpx_codec_alg_priv_t *ctx,
803 int flags)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400804{
John Koleszar0ea50ce2010-05-18 11:58:33 -0400805
806 /* Handle Flags */
807 if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF))
808 || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF)))
809 {
810 ctx->base.err_detail = "Conflicting flags.";
811 return VPX_CODEC_INVALID_PARAM;
812 }
813
814 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF
815 | VP8_EFLAG_NO_REF_ARF))
816 {
817 int ref = 7;
818
819 if (flags & VP8_EFLAG_NO_REF_LAST)
Johann965d8682012-05-23 16:08:37 -0700820 ref ^= VP8_LAST_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400821
822 if (flags & VP8_EFLAG_NO_REF_GF)
Johann965d8682012-05-23 16:08:37 -0700823 ref ^= VP8_GOLD_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400824
825 if (flags & VP8_EFLAG_NO_REF_ARF)
Johann965d8682012-05-23 16:08:37 -0700826 ref ^= VP8_ALTR_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400827
828 vp8_use_as_reference(ctx->cpi, ref);
829 }
830
831 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF
832 | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF
833 | VP8_EFLAG_FORCE_ARF))
834 {
835 int upd = 7;
836
837 if (flags & VP8_EFLAG_NO_UPD_LAST)
Johann965d8682012-05-23 16:08:37 -0700838 upd ^= VP8_LAST_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400839
840 if (flags & VP8_EFLAG_NO_UPD_GF)
Johann965d8682012-05-23 16:08:37 -0700841 upd ^= VP8_GOLD_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400842
843 if (flags & VP8_EFLAG_NO_UPD_ARF)
Johann965d8682012-05-23 16:08:37 -0700844 upd ^= VP8_ALTR_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400845
846 vp8_update_reference(ctx->cpi, upd);
847 }
848
849 if (flags & VP8_EFLAG_NO_UPD_ENTROPY)
850 {
851 vp8_update_entropy(ctx->cpi, 0);
852 }
853
Marcoaf898b52014-11-10 13:07:05 -0800854 return VPX_CODEC_OK;
855}
856
857static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx,
858 const vpx_image_t *img,
859 vpx_codec_pts_t pts,
860 unsigned long duration,
861 vpx_enc_frame_flags_t flags,
862 unsigned long deadline)
863{
864 vpx_codec_err_t res = VPX_CODEC_OK;
865
866 if (!ctx->cfg.rc_target_bitrate)
867 return res;
868
Marcoaf898b52014-11-10 13:07:05 -0800869 if (img)
870 res = validate_img(ctx, img);
871
872 if (!res)
873 res = validate_config(ctx, &ctx->cfg, &ctx->vp8_cfg, 1);
874
875 pick_quickcompress_mode(ctx, duration, deadline);
876 vpx_codec_pkt_list_init(&ctx->pkt_list);
877
878 // If no flags are set in the encode call, then use the frame flags as
879 // defined via the control function: vp8e_set_frame_flags.
880 if (!flags) {
881 flags = ctx->control_frame_flags;
882 }
883 ctx->control_frame_flags = 0;
884
James Zernc0523092015-08-24 20:07:48 -0700885 if (!res)
886 res = set_reference_and_update(ctx, flags);
Marcoaf898b52014-11-10 13:07:05 -0800887
John Koleszar0ea50ce2010-05-18 11:58:33 -0400888 /* Handle fixed keyframe intervals */
889 if (ctx->cfg.kf_mode == VPX_KF_AUTO
890 && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist)
891 {
892 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist)
893 {
894 flags |= VPX_EFLAG_FORCE_KF;
Andoni Morales Alastruey48140162011-02-07 18:04:02 +0100895 ctx->fixed_kf_cntr = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400896 }
897 }
898
899 /* Initialize the encoder instance on the first frame*/
900 if (!res && ctx->cpi)
901 {
902 unsigned int lib_flags;
903 YV12_BUFFER_CONFIG sd;
James Zernb45065d2011-07-25 18:44:59 -0700904 int64_t dst_time_stamp, dst_end_time_stamp;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400905 unsigned long size, cx_data_sz;
906 unsigned char *cx_data;
James Berrybc715112011-10-12 11:18:50 -0400907 unsigned char *cx_data_end;
908 int comp_data_state = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400909
910 /* Set up internal flags */
911 if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
912 ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1;
913
Stefan Holmer7296b3f2011-06-13 16:42:27 +0200914 if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION)
915 ((VP8_COMP *)ctx->cpi)->output_partition = 1;
916
John Koleszar0ea50ce2010-05-18 11:58:33 -0400917 /* Convert API flags to internal codec lib flags */
918 lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
919
920 /* vp8 use 10,000,000 ticks/second as time stamp */
921 dst_time_stamp = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
922 dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
923
924 if (img != NULL)
925 {
926 res = image2yuvconfig(img, &sd);
927
928 if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
929 &sd, dst_time_stamp, dst_end_time_stamp))
930 {
931 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
932 res = update_error_state(ctx, &cpi->common.error);
933 }
934
935 /* reset for next frame */
936 ctx->next_frame_flag = 0;
937 }
938
939 cx_data = ctx->cx_data;
940 cx_data_sz = ctx->cx_data_sz;
James Berrybc715112011-10-12 11:18:50 -0400941 cx_data_end = ctx->cx_data + cx_data_sz;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400942 lib_flags = 0;
943
James Berrybc715112011-10-12 11:18:50 -0400944 while (cx_data_sz >= ctx->cx_data_sz / 2)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400945 {
James Berrybc715112011-10-12 11:18:50 -0400946 comp_data_state = vp8_get_compressed_data(ctx->cpi,
947 &lib_flags,
948 &size,
949 cx_data,
950 cx_data_end,
951 &dst_time_stamp,
952 &dst_end_time_stamp,
953 !img);
954
955 if(comp_data_state == VPX_CODEC_CORRUPT_FRAME)
956 return VPX_CODEC_CORRUPT_FRAME;
957 else if(comp_data_state == -1)
958 break;
959
John Koleszar0ea50ce2010-05-18 11:58:33 -0400960 if (size)
961 {
962 vpx_codec_pts_t round, delta;
963 vpx_codec_cx_pkt_t pkt;
964 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
965
966 /* Add the frame packet to the list of returned packets. */
Dmitry Kovalev947748e2014-04-25 17:39:48 -0700967 round = (vpx_codec_pts_t)10000000
James Zern429743c2012-08-07 17:12:10 -0700968 * ctx->cfg.g_timebase.num / 2 - 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400969 delta = (dst_end_time_stamp - dst_time_stamp);
970 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400971 pkt.data.frame.pts =
972 (dst_time_stamp * ctx->cfg.g_timebase.den + round)
973 / ctx->cfg.g_timebase.num / 10000000;
Yaowu Xud71ba032012-08-17 10:05:35 -0700974 pkt.data.frame.duration = (unsigned long)
975 ((delta * ctx->cfg.g_timebase.den + round)
976 / ctx->cfg.g_timebase.num / 10000000);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400977 pkt.data.frame.flags = lib_flags << 16;
978
979 if (lib_flags & FRAMEFLAGS_KEY)
980 pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
981
982 if (!cpi->common.show_frame)
983 {
984 pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
985
John Koleszar0164a1c2012-05-21 14:30:56 -0700986 /* This timestamp should be as close as possible to the
987 * prior PTS so that if a decoder uses pts to schedule when
988 * to do this, we start right after last frame was decoded.
989 * Invisible frames have no duration.
990 */
Frank Galligan45e64942010-10-05 17:46:37 -0400991 pkt.data.frame.pts = ((cpi->last_time_stamp_seen
992 * ctx->cfg.g_timebase.den + round)
993 / ctx->cfg.g_timebase.num / 10000000) + 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400994 pkt.data.frame.duration = 0;
995 }
996
John Koleszar37de0b82011-07-07 10:38:23 -0400997 if (cpi->droppable)
998 pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE;
999
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001000 if (cpi->output_partition)
1001 {
1002 int i;
1003 const int num_partitions =
1004 (1 << cpi->common.multi_token_partition) + 1;
Attila Nagy0afcc762011-07-20 14:09:42 +03001005
1006 pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT;
1007
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001008 for (i = 0; i < num_partitions; ++i)
1009 {
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001010#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1011 pkt.data.frame.buf = cpi->partition_d[i];
1012#else
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001013 pkt.data.frame.buf = cx_data;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001014 cx_data += cpi->partition_sz[i];
1015 cx_data_sz -= cpi->partition_sz[i];
1016#endif
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001017 pkt.data.frame.sz = cpi->partition_sz[i];
1018 pkt.data.frame.partition_id = i;
1019 /* don't set the fragment bit for the last partition */
Attila Nagy0afcc762011-07-20 14:09:42 +03001020 if (i == (num_partitions - 1))
1021 pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT;
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001022 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001023 }
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001024#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1025 /* In lagged mode the encoder can buffer multiple frames.
1026 * We don't want this in partitioned output because
1027 * partitions are spread all over the output buffer.
1028 * So, force an exit!
1029 */
1030 cx_data_sz -= ctx->cx_data_sz / 2;
1031#endif
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001032 }
1033 else
1034 {
1035 pkt.data.frame.buf = cx_data;
1036 pkt.data.frame.sz = size;
1037 pkt.data.frame.partition_id = -1;
1038 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1039 cx_data += size;
1040 cx_data_sz -= size;
1041 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001042 }
1043 }
1044 }
1045
1046 return res;
1047}
1048
1049
1050static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx,
1051 vpx_codec_iter_t *iter)
1052{
1053 return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1054}
1055
1056static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001057 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001058{
1059 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1060
1061 if (data)
1062 {
1063 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1064 YV12_BUFFER_CONFIG sd;
1065
1066 image2yuvconfig(&frame->img, &sd);
1067 vp8_set_reference(ctx->cpi, frame->frame_type, &sd);
1068 return VPX_CODEC_OK;
1069 }
1070 else
1071 return VPX_CODEC_INVALID_PARAM;
1072
1073}
1074
1075static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001076 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001077{
1078
1079 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1080
1081 if (data)
1082 {
1083 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1084 YV12_BUFFER_CONFIG sd;
1085
1086 image2yuvconfig(&frame->img, &sd);
1087 vp8_get_reference(ctx->cpi, frame->frame_type, &sd);
1088 return VPX_CODEC_OK;
1089 }
1090 else
1091 return VPX_CODEC_INVALID_PARAM;
1092}
1093
1094static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001095 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001096{
James Zern76640f82010-08-20 16:06:56 -04001097#if CONFIG_POSTPROC
John Koleszar0ea50ce2010-05-18 11:58:33 -04001098 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
1099
1100 if (data)
1101 {
1102 ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
1103 return VPX_CODEC_OK;
1104 }
1105 else
1106 return VPX_CODEC_INVALID_PARAM;
James Zern76640f82010-08-20 16:06:56 -04001107#else
1108 (void)ctx;
James Zern76640f82010-08-20 16:06:56 -04001109 (void)args;
1110 return VPX_CODEC_INCAPABLE;
1111#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -04001112}
1113
1114
1115static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx)
1116{
1117
1118 YV12_BUFFER_CONFIG sd;
Fritz Koenig647df002010-11-04 16:03:36 -07001119 vp8_ppflags_t flags = {0};
John Koleszar0ea50ce2010-05-18 11:58:33 -04001120
Fritz Koenig647df002010-11-04 16:03:36 -07001121 if (ctx->preview_ppcfg.post_proc_flag)
1122 {
1123 flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
1124 flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
1125 flags.noise_level = ctx->preview_ppcfg.noise_level;
1126 }
1127
1128 if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags))
John Koleszar0ea50ce2010-05-18 11:58:33 -04001129 {
1130
1131 /*
James Zern6cd4a102010-05-20 23:22:39 -04001132 vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001133 sd.y_width + 2*VP8BORDERINPIXELS,
1134 sd.y_height + 2*VP8BORDERINPIXELS,
1135 1,
1136 sd.buffer_alloc);
1137 vpx_img_set_rect(&ctx->preview_img,
1138 VP8BORDERINPIXELS, VP8BORDERINPIXELS,
1139 sd.y_width, sd.y_height);
1140 */
1141
1142 ctx->preview_img.bps = 12;
John Koleszarb6c71912010-05-24 21:45:05 -04001143 ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer;
1144 ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer;
1145 ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001146
James Zern4fc6c882013-07-12 14:11:53 -07001147 ctx->preview_img.fmt = VPX_IMG_FMT_I420;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001148 ctx->preview_img.x_chroma_shift = 1;
1149 ctx->preview_img.y_chroma_shift = 1;
1150
James Berryddacf1c2011-02-08 14:23:18 -05001151 ctx->preview_img.d_w = sd.y_width;
1152 ctx->preview_img.d_h = sd.y_height;
John Koleszarb6c71912010-05-24 21:45:05 -04001153 ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride;
1154 ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride;
1155 ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001156 ctx->preview_img.w = sd.y_width;
1157 ctx->preview_img.h = sd.y_height;
1158
1159 return &ctx->preview_img;
1160 }
1161 else
1162 return NULL;
1163}
1164
Marcoaf898b52014-11-10 13:07:05 -08001165static vpx_codec_err_t vp8e_set_frame_flags(vpx_codec_alg_priv_t *ctx,
1166 va_list args)
1167{
1168 int frame_flags = va_arg(args, int);
1169 ctx->control_frame_flags = frame_flags;
1170 return set_reference_and_update(ctx, frame_flags);
1171}
1172
1173static vpx_codec_err_t vp8e_set_temporal_layer_id(vpx_codec_alg_priv_t *ctx,
1174 va_list args)
1175{
1176 int layer_id = va_arg(args, int);
1177 if (layer_id < 0 || layer_id >= (int)ctx->cfg.ts_number_layers) {
1178 return VPX_CODEC_INVALID_PARAM;
1179 }
1180 ctx->cpi->temporal_layer_id = layer_id;
1181 return VPX_CODEC_OK;
1182}
1183
John Koleszar0ea50ce2010-05-18 11:58:33 -04001184static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001185 va_list args)
1186{
1187 vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
1188
1189 if (data)
1190 {
1191 vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
1192
1193 if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols, roi->delta_q, roi->delta_lf, roi->static_threshold))
1194 return VPX_CODEC_OK;
1195 else
1196 return VPX_CODEC_INVALID_PARAM;
1197 }
1198 else
1199 return VPX_CODEC_INVALID_PARAM;
1200}
1201
1202
1203static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001204 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001205{
1206 vpx_active_map_t *data = va_arg(args, vpx_active_map_t *);
1207
1208 if (data)
1209 {
1210
1211 vpx_active_map_t *map = (vpx_active_map_t *)data;
1212
1213 if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols))
1214 return VPX_CODEC_OK;
1215 else
1216 return VPX_CODEC_INVALID_PARAM;
1217 }
1218 else
1219 return VPX_CODEC_INVALID_PARAM;
1220}
1221
1222static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001223 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001224{
1225
1226 vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *);
1227
1228 if (data)
1229 {
1230 int res;
1231 vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data ;
Frank Galliganbc45f232013-01-11 15:34:05 -08001232 res = vp8_set_internal_size(ctx->cpi,
1233 (VPX_SCALING)scalemode.h_scaling_mode,
1234 (VPX_SCALING)scalemode.v_scaling_mode);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001235
1236 if (!res)
1237 {
1238 /*force next frame a key frame to effect scaling mode */
1239 ctx->next_frame_flag |= FRAMEFLAGS_KEY;
1240 return VPX_CODEC_OK;
1241 }
1242 else
1243 return VPX_CODEC_INVALID_PARAM;
1244 }
1245 else
1246 return VPX_CODEC_INVALID_PARAM;
1247}
1248
1249
1250static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] =
1251{
1252 {VP8_SET_REFERENCE, vp8e_set_reference},
1253 {VP8_COPY_REFERENCE, vp8e_get_reference},
1254 {VP8_SET_POSTPROC, vp8e_set_previewpp},
Marcoaf898b52014-11-10 13:07:05 -08001255 {VP8E_SET_FRAME_FLAGS, vp8e_set_frame_flags},
1256 {VP8E_SET_TEMPORAL_LAYER_ID, vp8e_set_temporal_layer_id},
John Koleszar0ea50ce2010-05-18 11:58:33 -04001257 {VP8E_SET_ROI_MAP, vp8e_set_roi_map},
1258 {VP8E_SET_ACTIVEMAP, vp8e_set_activemap},
1259 {VP8E_SET_SCALEMODE, vp8e_set_scalemode},
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001260 {VP8E_SET_CPUUSED, set_cpu_used},
1261 {VP8E_SET_NOISE_SENSITIVITY, set_noise_sensitivity},
1262 {VP8E_SET_ENABLEAUTOALTREF, set_enable_auto_alt_ref},
1263 {VP8E_SET_SHARPNESS, set_sharpness},
1264 {VP8E_SET_STATIC_THRESHOLD, set_static_thresh},
1265 {VP8E_SET_TOKEN_PARTITIONS, set_token_partitions},
1266 {VP8E_GET_LAST_QUANTIZER, get_quantizer},
1267 {VP8E_GET_LAST_QUANTIZER_64, get_quantizer64},
1268 {VP8E_SET_ARNR_MAXFRAMES, set_arnr_max_frames},
1269 {VP8E_SET_ARNR_STRENGTH , set_arnr_strength},
1270 {VP8E_SET_ARNR_TYPE , set_arnr_type},
1271 {VP8E_SET_TUNING, set_tuning},
1272 {VP8E_SET_CQ_LEVEL, set_cq_level},
1273 {VP8E_SET_MAX_INTRA_BITRATE_PCT, set_rc_max_intra_bitrate_pct},
Marcoaf898b52014-11-10 13:07:05 -08001274 {VP8E_SET_SCREEN_CONTENT_MODE, set_screen_content_mode},
John Koleszar0ea50ce2010-05-18 11:58:33 -04001275 { -1, NULL},
1276};
1277
1278static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] =
1279{
1280 {
1281 0,
1282 {
1283 0, /* g_usage */
1284 0, /* g_threads */
1285 0, /* g_profile */
1286
1287 320, /* g_width */
1288 240, /* g_height */
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001289 VPX_BITS_8, /* g_bit_depth */
1290 8, /* g_input_bit_depth */
1291
John Koleszar0ea50ce2010-05-18 11:58:33 -04001292 {1, 30}, /* g_timebase */
1293
1294 0, /* g_error_resilient */
1295
1296 VPX_RC_ONE_PASS, /* g_pass */
1297
1298 0, /* g_lag_in_frames */
1299
John Koleszar23216212010-09-02 09:32:03 -04001300 0, /* rc_dropframe_thresh */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001301 0, /* rc_resize_allowed */
Adrian Grangef7bd1272014-04-09 14:51:29 -07001302 1, /* rc_scaled_width */
1303 1, /* rc_scaled_height */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001304 60, /* rc_resize_down_thresold */
1305 30, /* rc_resize_up_thresold */
1306
1307 VPX_VBR, /* rc_end_usage */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001308 {0}, /* rc_twopass_stats_in */
Pengchong Jinf349b072014-07-14 09:13:38 -07001309 {0}, /* rc_firstpass_mb_stats_in */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001310 256, /* rc_target_bandwidth */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001311 4, /* rc_min_quantizer */
1312 63, /* rc_max_quantizer */
John Koleszarc99f9d72011-04-11 11:29:23 -04001313 100, /* rc_undershoot_pct */
1314 100, /* rc_overshoot_pct */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001315
1316 6000, /* rc_max_buffer_size */
1317 4000, /* rc_buffer_initial_size; */
1318 5000, /* rc_buffer_optimal_size; */
1319
1320 50, /* rc_two_pass_vbrbias */
1321 0, /* rc_two_pass_vbrmin_section */
1322 400, /* rc_two_pass_vbrmax_section */
1323
1324 /* keyframing settings (kf) */
1325 VPX_KF_AUTO, /* g_kfmode*/
1326 0, /* kf_min_dist */
Ralph Giles2a0d7b12012-01-05 10:39:38 -06001327 128, /* kf_max_dist */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001328
Ivan Maltz01b35c32013-09-05 08:55:47 -07001329 VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */
Minghai Shange8998592014-07-14 11:24:17 -07001330 {0},
Minghai Shang8c196b22014-02-26 13:30:50 -08001331 {0}, /* ss_target_bitrate */
Adrian Grange217591f2011-10-06 15:49:11 -07001332 1, /* ts_number_layers */
1333 {0}, /* ts_target_bitrate */
1334 {0}, /* ts_rate_decimator */
1335 0, /* ts_periodicity */
1336 {0}, /* ts_layer_id */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001337 }},
John Koleszar0ea50ce2010-05-18 11:58:33 -04001338};
1339
1340
1341#ifndef VERSION_STRING
1342#define VERSION_STRING
1343#endif
John Koleszarfa7a55b2010-09-21 10:35:52 -04001344CODEC_INTERFACE(vpx_codec_vp8_cx) =
John Koleszar0ea50ce2010-05-18 11:58:33 -04001345{
John Koleszar317a6662010-06-10 12:08:01 -04001346 "WebM Project VP8 Encoder" VERSION_STRING,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001347 VPX_CODEC_INTERNAL_ABI_VERSION,
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001348 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR |
1349 VPX_CODEC_CAP_OUTPUT_PARTITION,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001350 /* vpx_codec_caps_t caps; */
1351 vp8e_init, /* vpx_codec_init_fn_t init; */
1352 vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */
1353 vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001354 {
Dmitry Kovalevcd6d9842014-08-20 17:16:28 -07001355 NULL, /* vpx_codec_peek_si_fn_t peek_si; */
1356 NULL, /* vpx_codec_get_si_fn_t get_si; */
1357 NULL, /* vpx_codec_decode_fn_t decode; */
1358 NULL, /* vpx_codec_frame_get_fn_t frame_get; */
Adrian Grangef29961d2015-02-12 09:31:40 -08001359 NULL, /* vpx_codec_set_fb_fn_t set_fb_fn; */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001360 },
1361 {
Jim Bankoski8a774e12014-07-31 06:27:57 -07001362 1, /* 1 cfg map */
Adrian Grangef29961d2015-02-12 09:31:40 -08001363 vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t cfg_maps; */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001364 vp8e_encode, /* vpx_codec_encode_fn_t encode; */
Adrian Grangef29961d2015-02-12 09:31:40 -08001365 vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t get_cx_data; */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001366 vp8e_set_config,
Dmitry Kovalevcd6d9842014-08-20 17:16:28 -07001367 NULL,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001368 vp8e_get_preview,
Yunqing Wangaa7335e2011-10-25 15:14:16 -04001369 vp8e_mr_alloc_mem,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001370 } /* encoder functions */
1371};