blob: 96b4cb5f2870aca0f8640b9892f354fe69ce7b22 [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar94c52e42010-06-18 12:39:21 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
John Koleszar94c52e42010-06-18 12:39:21 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
10
11
Minghai Shange8998592014-07-14 11:24:17 -070012#include "./vpx_config.h"
John Koleszara9c75972012-11-08 17:09:30 -080013#include "vp8_rtcd.h"
John Koleszarb7492342010-05-24 11:39:59 -040014#include "vpx/vpx_codec.h"
15#include "vpx/internal/vpx_codec_internal.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040016#include "vpx_version.h"
Dmitry Kovalevb08fab82014-09-02 18:26:53 -070017#include "vpx_mem/vpx_mem.h"
John Koleszar02321de2011-02-10 14:41:38 -050018#include "vp8/encoder/onyx_int.h"
John Koleszar2bf8fb52012-05-02 16:37:37 -070019#include "vpx/vp8cx.h"
John Koleszarbb7dd5b2010-10-14 16:40:12 -040020#include "vp8/encoder/firstpass.h"
John Koleszar02321de2011-02-10 14:41:38 -050021#include "vp8/common/onyx.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040022#include <stdlib.h>
23#include <string.h>
24
John Koleszar0ea50ce2010-05-18 11:58:33 -040025struct vp8_extracfg
26{
27 struct vpx_codec_pkt_list *pkt_list;
John Koleszar0ea50ce2010-05-18 11:58:33 -040028 int cpu_used; /** available cpu percentage in 1/16*/
29 unsigned int enable_auto_alt_ref; /** if encoder decides to uses alternate reference frame */
30 unsigned int noise_sensitivity;
31 unsigned int Sharpness;
32 unsigned int static_thresh;
33 unsigned int token_partitions;
34 unsigned int arnr_max_frames; /* alt_ref Noise Reduction Max Frame Count */
35 unsigned int arnr_strength; /* alt_ref Noise Reduction Strength */
36 unsigned int arnr_type; /* alt_ref filter type */
John Koleszarb0da9b32010-12-17 09:43:39 -050037 vp8e_tuning tuning;
Paul Wilkinse0846c92011-01-07 18:29:37 +000038 unsigned int cq_level; /* constrained quality level */
John Koleszar1654ae92011-07-28 09:17:32 -040039 unsigned int rc_max_intra_bitrate_pct;
Marcoaf898b52014-11-10 13:07:05 -080040 unsigned int screen_content_mode;
John Koleszar0ea50ce2010-05-18 11:58:33 -040041
42};
43
Dmitry Kovalev48274c62014-08-21 11:21:18 -070044static struct vp8_extracfg default_extracfg = {
45 NULL,
John Koleszar0ea50ce2010-05-18 11:58:33 -040046#if !(CONFIG_REALTIME_ONLY)
Dmitry Kovalev48274c62014-08-21 11:21:18 -070047 0, /* cpu_used */
John Koleszar0ea50ce2010-05-18 11:58:33 -040048#else
Dmitry Kovalev48274c62014-08-21 11:21:18 -070049 4, /* cpu_used */
John Koleszar0ea50ce2010-05-18 11:58:33 -040050#endif
Dmitry Kovalev48274c62014-08-21 11:21:18 -070051 0, /* enable_auto_alt_ref */
52 0, /* noise_sensitivity */
53 0, /* Sharpness */
54 0, /* static_thresh */
Attila Nagy52cf4dc2012-02-09 12:37:03 +020055#if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
Dmitry Kovalev48274c62014-08-21 11:21:18 -070056 VP8_EIGHT_TOKENPARTITION,
Attila Nagy52cf4dc2012-02-09 12:37:03 +020057#else
Dmitry Kovalev48274c62014-08-21 11:21:18 -070058 VP8_ONE_TOKENPARTITION, /* token_partitions */
Attila Nagy52cf4dc2012-02-09 12:37:03 +020059#endif
Dmitry Kovalev48274c62014-08-21 11:21:18 -070060 0, /* arnr_max_frames */
61 3, /* arnr_strength */
62 3, /* arnr_type*/
63 0, /* tuning*/
64 10, /* cq_level */
65 0, /* rc_max_intra_bitrate_pct */
Marcoaf898b52014-11-10 13:07:05 -080066 0, /* screen_content_mode */
John Koleszar0ea50ce2010-05-18 11:58:33 -040067};
68
69struct vpx_codec_alg_priv
70{
71 vpx_codec_priv_t base;
72 vpx_codec_enc_cfg_t cfg;
73 struct vp8_extracfg vp8_cfg;
74 VP8_CONFIG oxcf;
John Koleszarb0056c32011-12-20 16:54:54 -080075 struct VP8_COMP *cpi;
John Koleszar0ea50ce2010-05-18 11:58:33 -040076 unsigned char *cx_data;
77 unsigned int cx_data_sz;
78 vpx_image_t preview_img;
79 unsigned int next_frame_flag;
80 vp8_postproc_cfg_t preview_ppcfg;
John Koleszar0164a1c2012-05-21 14:30:56 -070081 /* pkt_list size depends on the maximum number of lagged frames allowed. */
82 vpx_codec_pkt_list_decl(64) pkt_list;
John Koleszar0ea50ce2010-05-18 11:58:33 -040083 unsigned int fixed_kf_cntr;
Marcoaf898b52014-11-10 13:07:05 -080084 vpx_enc_frame_flags_t control_frame_flags;
John Koleszar0ea50ce2010-05-18 11:58:33 -040085};
86
87
88static vpx_codec_err_t
89update_error_state(vpx_codec_alg_priv_t *ctx,
90 const struct vpx_internal_error_info *error)
91{
92 vpx_codec_err_t res;
93
94 if ((res = error->error_code))
95 ctx->base.err_detail = error->has_detail
96 ? error->detail
97 : NULL;
98
99 return res;
100}
101
102
John Koleszar79e2b1f2010-11-17 09:08:47 -0500103#undef ERROR
John Koleszar0ea50ce2010-05-18 11:58:33 -0400104#define ERROR(str) do {\
105 ctx->base.err_detail = str;\
106 return VPX_CODEC_INVALID_PARAM;\
107 } while(0)
108
109#define RANGE_CHECK(p,memb,lo,hi) do {\
Guillermo Ballester Valor5a726202010-06-11 14:33:49 -0400110 if(!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
John Koleszar0ea50ce2010-05-18 11:58:33 -0400111 ERROR(#memb " out of range ["#lo".."#hi"]");\
112 } while(0)
113
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400114#define RANGE_CHECK_HI(p,memb,hi) do {\
115 if(!((p)->memb <= (hi))) \
116 ERROR(#memb " out of range [.."#hi"]");\
117 } while(0)
118
John Koleszar0ea50ce2010-05-18 11:58:33 -0400119#define RANGE_CHECK_LO(p,memb,lo) do {\
120 if(!((p)->memb >= (lo))) \
121 ERROR(#memb " out of range ["#lo"..]");\
122 } while(0)
123
124#define RANGE_CHECK_BOOL(p,memb) do {\
125 if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
126 } while(0)
127
128static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
129 const vpx_codec_enc_cfg_t *cfg,
James Berryc1c47e82011-12-07 15:48:00 -0500130 const struct vp8_extracfg *vp8_cfg,
131 int finalize)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400132{
Attila Nagy1aadced2011-04-12 15:01:22 +0300133 RANGE_CHECK(cfg, g_w, 1, 16383); /* 14 bits available */
134 RANGE_CHECK(cfg, g_h, 1, 16383); /* 14 bits available */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400135 RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
136 RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400137 RANGE_CHECK_HI(cfg, g_profile, 3);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400138 RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
John Koleszar2d03f072011-01-28 11:56:18 -0500139 RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400140 RANGE_CHECK_HI(cfg, g_threads, 64);
Yunqing Wang7f009972012-07-11 11:43:51 -0700141#if CONFIG_REALTIME_ONLY
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400142 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
Yunqing Wang7f009972012-07-11 11:43:51 -0700143#elif CONFIG_MULTI_RES_ENCODING
144 if (ctx->base.enc.total_encoders > 1)
145 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
146#else
147 RANGE_CHECK_HI(cfg, g_lag_in_frames, 25);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400148#endif
Deb Mukherjeee378a892013-08-29 16:21:44 -0700149 RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
John Koleszarc99f9d72011-04-11 11:29:23 -0400150 RANGE_CHECK_HI(cfg, rc_undershoot_pct, 1000);
151 RANGE_CHECK_HI(cfg, rc_overshoot_pct, 1000);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400152 RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400153 RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
Yunqing Wang7f009972012-07-11 11:43:51 -0700154
155/* TODO: add spatial re-sampling support and frame dropping in
156 * multi-res-encoder.*/
157#if CONFIG_MULTI_RES_ENCODING
158 if (ctx->base.enc.total_encoders > 1)
Yunqing Wang7f009972012-07-11 11:43:51 -0700159 RANGE_CHECK_HI(cfg, rc_resize_allowed, 0);
Yunqing Wang7f009972012-07-11 11:43:51 -0700160#else
161 RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
Yunqing Wang7f009972012-07-11 11:43:51 -0700162#endif
Yunqing Wang4066c8b2012-06-08 11:17:50 -0400163 RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400164 RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
165 RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
Yunqing Wang7f009972012-07-11 11:43:51 -0700166
167#if CONFIG_REALTIME_ONLY
John Koleszar0ea50ce2010-05-18 11:58:33 -0400168 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
Yunqing Wang7f009972012-07-11 11:43:51 -0700169#elif CONFIG_MULTI_RES_ENCODING
170 if (ctx->base.enc.total_encoders > 1)
171 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
172#else
173 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400174#endif
175
176 /* VP8 does not support a lower bound on the keyframe interval in
177 * automatic keyframe placement mode.
178 */
179 if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist
180 && cfg->kf_min_dist > 0)
181 ERROR("kf_min_dist not supported in auto mode, use 0 "
182 "or kf_max_dist instead.");
183
184 RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref);
Yunqing Wang4fd81a92011-04-11 15:55:04 -0400185 RANGE_CHECK(vp8_cfg, cpu_used, -16, 16);
John Koleszarc8f4c182012-05-08 11:23:42 -0700186
187#if CONFIG_REALTIME_ONLY && !CONFIG_TEMPORAL_DENOISING
188 RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0);
189#else
190 RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6);
Stefan Holmer9c411432012-03-06 10:48:18 +0100191#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -0400192
Yunqing Wang7f009972012-07-11 11:43:51 -0700193 RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION,
194 VP8_EIGHT_TOKENPARTITION);
Guillermo Ballester Valor23690682010-06-11 14:33:49 -0400195 RANGE_CHECK_HI(vp8_cfg, Sharpness, 7);
Adrian Grange8ee72842010-09-30 10:06:09 +0100196 RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
Frank Galligan15542722010-10-04 21:12:22 -0400197 RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6);
Adrian Grange8ee72842010-09-30 10:06:09 +0100198 RANGE_CHECK(vp8_cfg, arnr_type, 1, 3);
Paul Wilkinse0846c92011-01-07 18:29:37 +0000199 RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
Marcoaf898b52014-11-10 13:07:05 -0800200 RANGE_CHECK_BOOL(vp8_cfg, screen_content_mode);
Deb Mukherjeee378a892013-08-29 16:21:44 -0700201 if (finalize && (cfg->rc_end_usage == VPX_CQ || cfg->rc_end_usage == VPX_Q))
James Berryc1c47e82011-12-07 15:48:00 -0500202 RANGE_CHECK(vp8_cfg, cq_level,
203 cfg->rc_min_quantizer, cfg->rc_max_quantizer);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400204
Attila Nagycb791aa2011-01-10 11:14:10 +0200205#if !(CONFIG_REALTIME_ONLY)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400206 if (cfg->g_pass == VPX_RC_LAST_PASS)
207 {
Adrian Grangeed40ff92011-03-10 11:32:48 -0800208 size_t packet_sz = sizeof(FIRSTPASS_STATS);
Yaowu Xud71ba032012-08-17 10:05:35 -0700209 int n_packets = (int)(cfg->rc_twopass_stats_in.sz /
210 packet_sz);
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400211 FIRSTPASS_STATS *stats;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400212
213 if (!cfg->rc_twopass_stats_in.buf)
214 ERROR("rc_twopass_stats_in.buf not set.");
215
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400216 if (cfg->rc_twopass_stats_in.sz % packet_sz)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400217 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
218
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400219 if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400220 ERROR("rc_twopass_stats_in requires at least two packets.");
221
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400222 stats = (void*)((char *)cfg->rc_twopass_stats_in.buf
223 + (n_packets - 1) * packet_sz);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400224
John Koleszarbb7dd5b2010-10-14 16:40:12 -0400225 if ((int)(stats->count + 0.5) != n_packets - 1)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400226 ERROR("rc_twopass_stats_in missing EOS stats packet");
227 }
Attila Nagycb791aa2011-01-10 11:14:10 +0200228#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -0400229
Adrian Grange217591f2011-10-06 15:49:11 -0700230 RANGE_CHECK(cfg, ts_number_layers, 1, 5);
231
232 if (cfg->ts_number_layers > 1)
233 {
Scott Graham92963df2012-05-08 11:45:35 -0700234 unsigned int i;
Adrian Grange217591f2011-10-06 15:49:11 -0700235 RANGE_CHECK_HI(cfg, ts_periodicity, 16);
236
237 for (i=1; i<cfg->ts_number_layers; i++)
Marcoaf898b52014-11-10 13:07:05 -0800238 if (cfg->ts_target_bitrate[i] <= cfg->ts_target_bitrate[i-1] &&
239 cfg->rc_target_bitrate > 0)
Adrian Grange217591f2011-10-06 15:49:11 -0700240 ERROR("ts_target_bitrate entries are not strictly increasing");
241
242 RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers-1], 1, 1);
243 for (i=cfg->ts_number_layers-2; i>0; i--)
244 if (cfg->ts_rate_decimator[i-1] != 2*cfg->ts_rate_decimator[i])
245 ERROR("ts_rate_decimator factors are not powers of 2");
246
247 RANGE_CHECK_HI(cfg, ts_layer_id[i], cfg->ts_number_layers-1);
248 }
249
Attila Nagy52cf4dc2012-02-09 12:37:03 +0200250#if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
251 if(cfg->g_threads > (1 << vp8_cfg->token_partitions))
252 ERROR("g_threads cannot be bigger than number of token partitions");
253#endif
254
John Koleszar0ea50ce2010-05-18 11:58:33 -0400255 return VPX_CODEC_OK;
256}
257
258
259static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
260 const vpx_image_t *img)
261{
262 switch (img->fmt)
263 {
James Zern6cd4a102010-05-20 23:22:39 -0400264 case VPX_IMG_FMT_YV12:
265 case VPX_IMG_FMT_I420:
266 case VPX_IMG_FMT_VPXI420:
267 case VPX_IMG_FMT_VPXYV12:
John Koleszar0ea50ce2010-05-18 11:58:33 -0400268 break;
269 default:
270 ERROR("Invalid image format. Only YV12 and I420 images are supported");
271 }
272
273 if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
274 ERROR("Image size must match encoder init configuration size");
275
276 return VPX_CODEC_OK;
277}
278
279
280static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf,
281 vpx_codec_enc_cfg_t cfg,
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400282 struct vp8_extracfg vp8_cfg,
283 vpx_codec_priv_enc_mr_cfg_t *mr_cfg)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400284{
285 oxcf->multi_threaded = cfg.g_threads;
286 oxcf->Version = cfg.g_profile;
287
288 oxcf->Width = cfg.g_w;
289 oxcf->Height = cfg.g_h;
John Koleszarbdd35c12011-11-11 10:47:20 -0800290 oxcf->timebase = cfg.g_timebase;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400291
Adrian Grange217591f2011-10-06 15:49:11 -0700292 oxcf->error_resilient_mode = cfg.g_error_resilient;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400293
294 switch (cfg.g_pass)
295 {
296 case VPX_RC_ONE_PASS:
297 oxcf->Mode = MODE_BESTQUALITY;
298 break;
299 case VPX_RC_FIRST_PASS:
300 oxcf->Mode = MODE_FIRSTPASS;
301 break;
302 case VPX_RC_LAST_PASS:
303 oxcf->Mode = MODE_SECONDPASS_BEST;
304 break;
305 }
306
James Zern7b0b6a22012-06-13 11:59:12 -0700307 if (cfg.g_pass == VPX_RC_FIRST_PASS || cfg.g_pass == VPX_RC_ONE_PASS)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400308 {
Adrian Grange217591f2011-10-06 15:49:11 -0700309 oxcf->allow_lag = 0;
310 oxcf->lag_in_frames = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400311 }
312 else
313 {
Adrian Grange217591f2011-10-06 15:49:11 -0700314 oxcf->allow_lag = (cfg.g_lag_in_frames) > 0;
315 oxcf->lag_in_frames = cfg.g_lag_in_frames;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400316 }
317
318 oxcf->allow_df = (cfg.rc_dropframe_thresh > 0);
319 oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh;
320
321 oxcf->allow_spatial_resampling = cfg.rc_resize_allowed;
322 oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh;
323 oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh;
324
Deb Mukherjeee378a892013-08-29 16:21:44 -0700325 if (cfg.rc_end_usage == VPX_VBR) {
326 oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
327 } else if (cfg.rc_end_usage == VPX_CBR) {
328 oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
329 } else if (cfg.rc_end_usage == VPX_CQ) {
330 oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
331 } else if (cfg.rc_end_usage == VPX_Q) {
332 oxcf->end_usage = USAGE_CONSTANT_QUALITY;
Paul Wilkinse0846c92011-01-07 18:29:37 +0000333 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400334
Adrian Grange217591f2011-10-06 15:49:11 -0700335 oxcf->target_bandwidth = cfg.rc_target_bitrate;
John Koleszar1654ae92011-07-28 09:17:32 -0400336 oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400337
Adrian Grange217591f2011-10-06 15:49:11 -0700338 oxcf->best_allowed_q = cfg.rc_min_quantizer;
339 oxcf->worst_allowed_q = cfg.rc_max_quantizer;
340 oxcf->cq_level = vp8_cfg.cq_level;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400341 oxcf->fixed_q = -1;
342
Adrian Grange217591f2011-10-06 15:49:11 -0700343 oxcf->under_shoot_pct = cfg.rc_undershoot_pct;
344 oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400345
Adrian Grangee4793792012-01-13 14:09:40 -0800346 oxcf->maximum_buffer_size_in_ms = cfg.rc_buf_sz;
347 oxcf->starting_buffer_level_in_ms = cfg.rc_buf_initial_sz;
348 oxcf->optimal_buffer_level_in_ms = cfg.rc_buf_optimal_sz;
349
Adrian Grange217591f2011-10-06 15:49:11 -0700350 oxcf->maximum_buffer_size = cfg.rc_buf_sz;
351 oxcf->starting_buffer_level = cfg.rc_buf_initial_sz;
352 oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400353
Adrian Grange217591f2011-10-06 15:49:11 -0700354 oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400355 oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct;
356 oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct;
357
Adrian Grange217591f2011-10-06 15:49:11 -0700358 oxcf->auto_key = cfg.kf_mode == VPX_KF_AUTO
359 && cfg.kf_min_dist != cfg.kf_max_dist;
Adrian Grange217591f2011-10-06 15:49:11 -0700360 oxcf->key_freq = cfg.kf_max_dist;
361
362 oxcf->number_of_layers = cfg.ts_number_layers;
363 oxcf->periodicity = cfg.ts_periodicity;
364
365 if (oxcf->number_of_layers > 1)
366 {
367 memcpy (oxcf->target_bitrate, cfg.ts_target_bitrate,
368 sizeof(cfg.ts_target_bitrate));
369 memcpy (oxcf->rate_decimator, cfg.ts_rate_decimator,
370 sizeof(cfg.ts_rate_decimator));
371 memcpy (oxcf->layer_id, cfg.ts_layer_id, sizeof(cfg.ts_layer_id));
372 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400373
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400374#if CONFIG_MULTI_RES_ENCODING
Yunqing Wangc647ec42011-12-16 16:50:29 -0500375 /* When mr_cfg is NULL, oxcf->mr_total_resolutions and oxcf->mr_encoder_id
376 * are both memset to 0, which ensures the correct logic under this
377 * situation.
378 */
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400379 if(mr_cfg)
380 {
381 oxcf->mr_total_resolutions = mr_cfg->mr_total_resolutions;
382 oxcf->mr_encoder_id = mr_cfg->mr_encoder_id;
383 oxcf->mr_down_sampling_factor.num = mr_cfg->mr_down_sampling_factor.num;
384 oxcf->mr_down_sampling_factor.den = mr_cfg->mr_down_sampling_factor.den;
385 oxcf->mr_low_res_mode_info = mr_cfg->mr_low_res_mode_info;
386 }
Johann80b344d2014-12-16 12:22:10 -0800387#else
388 (void)mr_cfg;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400389#endif
390
Adrian Grange217591f2011-10-06 15:49:11 -0700391 oxcf->cpu_used = vp8_cfg.cpu_used;
392 oxcf->encode_breakout = vp8_cfg.static_thresh;
393 oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref;
394 oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity;
395 oxcf->Sharpness = vp8_cfg.Sharpness;
396 oxcf->token_partitions = vp8_cfg.token_partitions;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400397
Adrian Grange217591f2011-10-06 15:49:11 -0700398 oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in;
399 oxcf->output_pkt_list = vp8_cfg.pkt_list;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400400
Adrian Grange217591f2011-10-06 15:49:11 -0700401 oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
402 oxcf->arnr_strength = vp8_cfg.arnr_strength;
403 oxcf->arnr_type = vp8_cfg.arnr_type;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400404
Adrian Grange217591f2011-10-06 15:49:11 -0700405 oxcf->tuning = vp8_cfg.tuning;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400406
Marcoaf898b52014-11-10 13:07:05 -0800407 oxcf->screen_content_mode = vp8_cfg.screen_content_mode;
408
John Koleszar0ea50ce2010-05-18 11:58:33 -0400409 /*
410 printf("Current VP8 Settings: \n");
411 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
412 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
413 printf("Sharpness: %d\n", oxcf->Sharpness);
414 printf("cpu_used: %d\n", oxcf->cpu_used);
415 printf("Mode: %d\n", oxcf->Mode);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400416 printf("auto_key: %d\n", oxcf->auto_key);
417 printf("key_freq: %d\n", oxcf->key_freq);
418 printf("end_usage: %d\n", oxcf->end_usage);
419 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
John Koleszarc99f9d72011-04-11 11:29:23 -0400420 printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400421 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
422 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
423 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
424 printf("fixed_q: %d\n", oxcf->fixed_q);
425 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
426 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
427 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
428 printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
429 printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
430 printf("allow_df: %d\n", oxcf->allow_df);
431 printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
432 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
433 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
434 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
435 printf("allow_lag: %d\n", oxcf->allow_lag);
436 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
437 printf("play_alternate: %d\n", oxcf->play_alternate);
438 printf("Version: %d\n", oxcf->Version);
439 printf("multi_threaded: %d\n", oxcf->multi_threaded);
440 printf("encode_breakout: %d\n", oxcf->encode_breakout);
441 */
442 return VPX_CODEC_OK;
443}
444
445static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx,
446 const vpx_codec_enc_cfg_t *cfg)
447{
448 vpx_codec_err_t res;
449
Alex Converse581731a2015-01-16 16:02:05 -0800450 if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h)
451 {
452 if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)
453 ERROR("Cannot change width or height after initialization");
454 if ((ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
455 (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
456 ERROR("Cannot increast width or height larger than their initial values");
457 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400458
459 /* Prevent increasing lag_in_frames. This check is stricter than it needs
460 * to be -- the limit is not increasing past the first lag_in_frames
461 * value, but we don't track the initial config, only the last successful
462 * config.
463 */
464 if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
465 ERROR("Cannot increase lag_in_frames");
466
James Berryc1c47e82011-12-07 15:48:00 -0500467 res = validate_config(ctx, cfg, &ctx->vp8_cfg, 0);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400468
469 if (!res)
470 {
471 ctx->cfg = *cfg;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400472 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400473 vp8_change_config(ctx->cpi, &ctx->oxcf);
474 }
475
476 return res;
477}
478
John Koleszar0ea50ce2010-05-18 11:58:33 -0400479int vp8_reverse_trans(int);
480
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700481static vpx_codec_err_t get_quantizer(vpx_codec_alg_priv_t *ctx, va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400482{
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700483 int *const arg = va_arg(args, int *);
484 if (arg == NULL)
485 return VPX_CODEC_INVALID_PARAM;
486 *arg = vp8_get_quantizer(ctx->cpi);
487 return VPX_CODEC_OK;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400488}
489
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700490static vpx_codec_err_t get_quantizer64(vpx_codec_alg_priv_t *ctx, va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400491{
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700492 int *const arg = va_arg(args, int *);
493 if (arg == NULL)
494 return VPX_CODEC_INVALID_PARAM;
495 *arg = vp8_reverse_trans(vp8_get_quantizer(ctx->cpi));
496 return VPX_CODEC_OK;
497}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400498
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700499static vpx_codec_err_t update_extracfg(vpx_codec_alg_priv_t *ctx,
500 const struct vp8_extracfg *extra_cfg)
501{
502 const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg, 0);
503 if (res == VPX_CODEC_OK) {
504 ctx->vp8_cfg = *extra_cfg;
505 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
506 vp8_change_config(ctx->cpi, &ctx->oxcf);
507 }
508 return res;
509}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400510
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700511static vpx_codec_err_t set_cpu_used(vpx_codec_alg_priv_t *ctx, va_list args)
512{
513 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
514 extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
515 return update_extracfg(ctx, &extra_cfg);
516}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400517
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700518static vpx_codec_err_t set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
519 va_list args)
520{
521 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
522 extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args);
523 return update_extracfg(ctx, &extra_cfg);
524}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400525
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700526static vpx_codec_err_t set_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
527 va_list args)
528{
529 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
530 extra_cfg.noise_sensitivity = CAST(VP8E_SET_NOISE_SENSITIVITY, args);
531 return update_extracfg(ctx, &extra_cfg);
532}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400533
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700534static vpx_codec_err_t set_sharpness(vpx_codec_alg_priv_t *ctx, va_list args)
535{
536 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
537 extra_cfg.Sharpness = CAST(VP8E_SET_SHARPNESS, args);
538 return update_extracfg(ctx, &extra_cfg);
539}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400540
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700541static vpx_codec_err_t set_static_thresh(vpx_codec_alg_priv_t *ctx,
542 va_list args)
543{
544 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
545 extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
546 return update_extracfg(ctx, &extra_cfg);
547}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400548
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -0700549static vpx_codec_err_t set_token_partitions(vpx_codec_alg_priv_t *ctx,
550 va_list args)
551{
552 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
553 extra_cfg.token_partitions = CAST(VP8E_SET_TOKEN_PARTITIONS, args);
554 return update_extracfg(ctx, &extra_cfg);
555}
556
557static vpx_codec_err_t set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
558 va_list args)
559{
560 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
561 extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
562 return update_extracfg(ctx, &extra_cfg);
563}
564
565static vpx_codec_err_t set_arnr_strength(vpx_codec_alg_priv_t *ctx,
566 va_list args)
567{
568 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
569 extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
570 return update_extracfg(ctx, &extra_cfg);
571}
572
573static vpx_codec_err_t set_arnr_type(vpx_codec_alg_priv_t *ctx, va_list args)
574{
575 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
576 extra_cfg.arnr_type = CAST(VP8E_SET_ARNR_TYPE, args);
577 return update_extracfg(ctx, &extra_cfg);
578}
579
580static vpx_codec_err_t set_tuning(vpx_codec_alg_priv_t *ctx, va_list args)
581{
582 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
583 extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
584 return update_extracfg(ctx, &extra_cfg);
585}
586
587static vpx_codec_err_t set_cq_level(vpx_codec_alg_priv_t *ctx, va_list args)
588{
589 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
590 extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
591 return update_extracfg(ctx, &extra_cfg);
592}
593
594static vpx_codec_err_t set_rc_max_intra_bitrate_pct(vpx_codec_alg_priv_t *ctx,
595 va_list args)
596{
597 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
598 extra_cfg.rc_max_intra_bitrate_pct =
599 CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
600 return update_extracfg(ctx, &extra_cfg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400601}
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400602
Marcoaf898b52014-11-10 13:07:05 -0800603static vpx_codec_err_t set_screen_content_mode(vpx_codec_alg_priv_t *ctx,
604 va_list args)
605{
606 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
607 extra_cfg.screen_content_mode =
608 CAST(VP8E_SET_SCREEN_CONTENT_MODE, args);
609 return update_extracfg(ctx, &extra_cfg);
610}
611
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400612static vpx_codec_err_t vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t *cfg,
613 void **mem_loc)
614{
Yunqing Wangad479a92012-05-23 13:40:24 -0400615 vpx_codec_err_t res = 0;
616
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400617#if CONFIG_MULTI_RES_ENCODING
Yunqing Wang65dd1572012-05-16 15:06:42 -0400618 LOWER_RES_FRAME_INFO *shared_mem_loc;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400619 int mb_rows = ((cfg->g_w + 15) >>4);
620 int mb_cols = ((cfg->g_h + 15) >>4);
621
Yunqing Wang65dd1572012-05-16 15:06:42 -0400622 shared_mem_loc = calloc(1, sizeof(LOWER_RES_FRAME_INFO));
623 if(!shared_mem_loc)
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400624 {
Yunqing Wangad479a92012-05-23 13:40:24 -0400625 res = VPX_CODEC_MEM_ERROR;
Yunqing Wang65dd1572012-05-16 15:06:42 -0400626 }
627
628 shared_mem_loc->mb_info = calloc(mb_rows*mb_cols, sizeof(LOWER_RES_MB_INFO));
629 if(!(shared_mem_loc->mb_info))
630 {
Yunqing Wangad479a92012-05-23 13:40:24 -0400631 res = VPX_CODEC_MEM_ERROR;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400632 }
633 else
Yunqing Wang65dd1572012-05-16 15:06:42 -0400634 {
635 *mem_loc = (void *)shared_mem_loc;
Yunqing Wangad479a92012-05-23 13:40:24 -0400636 res = VPX_CODEC_OK;
Yunqing Wang65dd1572012-05-16 15:06:42 -0400637 }
Johann80b344d2014-12-16 12:22:10 -0800638#else
639 (void)cfg;
640 (void)mem_loc;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400641#endif
Yunqing Wangad479a92012-05-23 13:40:24 -0400642 return res;
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400643}
644
645static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx,
646 vpx_codec_priv_enc_mr_cfg_t *mr_cfg)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400647{
John Koleszar2bf8fb52012-05-02 16:37:37 -0700648 vpx_codec_err_t res = VPX_CODEC_OK;
Dmitry Kovalevb08fab82014-09-02 18:26:53 -0700649
John Koleszar0ea50ce2010-05-18 11:58:33 -0400650
John Koleszara9c75972012-11-08 17:09:30 -0800651 vp8_rtcd();
John Koleszar8df79e92012-06-15 15:40:13 -0700652
John Koleszar0ea50ce2010-05-18 11:58:33 -0400653 if (!ctx->priv)
654 {
Dmitry Kovalevb08fab82014-09-02 18:26:53 -0700655 struct vpx_codec_alg_priv *priv =
656 (struct vpx_codec_alg_priv *)vpx_calloc(1, sizeof(*priv));
John Koleszar0ea50ce2010-05-18 11:58:33 -0400657
Attila Nagye6db21e2011-02-22 15:02:05 +0200658 if (!priv)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400659 {
Attila Nagye6db21e2011-02-22 15:02:05 +0200660 return VPX_CODEC_MEM_ERROR;
661 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400662
Dmitry Kovalev73edeb02014-08-20 17:02:10 -0700663 ctx->priv = (vpx_codec_priv_t *)priv;
Attila Nagye6db21e2011-02-22 15:02:05 +0200664 ctx->priv->init_flags = ctx->init_flags;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400665
Attila Nagye6db21e2011-02-22 15:02:05 +0200666 if (ctx->config.enc)
667 {
668 /* Update the reference to the config structure to an
669 * internal copy.
John Koleszar0ea50ce2010-05-18 11:58:33 -0400670 */
Dmitry Kovalev73edeb02014-08-20 17:02:10 -0700671 priv->cfg = *ctx->config.enc;
672 ctx->config.enc = &priv->cfg;
Attila Nagye6db21e2011-02-22 15:02:05 +0200673 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400674
Dmitry Kovalev48274c62014-08-21 11:21:18 -0700675 priv->vp8_cfg = default_extracfg;
Attila Nagye6db21e2011-02-22 15:02:05 +0200676 priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400677
Attila Nagye6db21e2011-02-22 15:02:05 +0200678 priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400679
James Berrybac6c222011-10-24 11:50:27 -0400680 if (priv->cx_data_sz < 32768) priv->cx_data_sz = 32768;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400681
Attila Nagye6db21e2011-02-22 15:02:05 +0200682 priv->cx_data = malloc(priv->cx_data_sz);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400683
Attila Nagye6db21e2011-02-22 15:02:05 +0200684 if (!priv->cx_data)
685 {
686 return VPX_CODEC_MEM_ERROR;
687 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400688
Yunqing Wang7f009972012-07-11 11:43:51 -0700689 if(mr_cfg)
690 ctx->priv->enc.total_encoders = mr_cfg->mr_total_resolutions;
691 else
692 ctx->priv->enc.total_encoders = 1;
693
James Berryc1c47e82011-12-07 15:48:00 -0500694 res = validate_config(priv, &priv->cfg, &priv->vp8_cfg, 0);
Attila Nagye6db21e2011-02-22 15:02:05 +0200695
696 if (!res)
697 {
Dmitry Kovalev73edeb02014-08-20 17:02:10 -0700698 set_vp8e_config(&priv->oxcf, priv->cfg, priv->vp8_cfg, mr_cfg);
699 priv->cpi = vp8_create_compressor(&priv->oxcf);
700 if (!priv->cpi)
Attila Nagye6db21e2011-02-22 15:02:05 +0200701 res = VPX_CODEC_MEM_ERROR;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400702 }
703 }
704
705 return res;
706}
707
708static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx)
709{
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400710#if CONFIG_MULTI_RES_ENCODING
711 /* Free multi-encoder shared memory */
712 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 -0400713 {
714 LOWER_RES_FRAME_INFO *shared_mem_loc = (LOWER_RES_FRAME_INFO *)ctx->oxcf.mr_low_res_mode_info;
715 free(shared_mem_loc->mb_info);
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400716 free(ctx->oxcf.mr_low_res_mode_info);
Yunqing Wang65dd1572012-05-16 15:06:42 -0400717 }
Yunqing Wangaa7335e2011-10-25 15:14:16 -0400718#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -0400719
720 free(ctx->cx_data);
721 vp8_remove_compressor(&ctx->cpi);
Dmitry Kovalevb08fab82014-09-02 18:26:53 -0700722 vpx_free(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400723 return VPX_CODEC_OK;
724}
725
726static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
727 YV12_BUFFER_CONFIG *yv12)
728{
Johann69dc8762014-08-15 10:50:40 -0700729 const int y_w = img->d_w;
730 const int y_h = img->d_h;
731 const int uv_w = (img->d_w + 1) / 2;
732 const int uv_h = (img->d_h + 1) / 2;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400733 vpx_codec_err_t res = VPX_CODEC_OK;
John Koleszarb6c71912010-05-24 21:45:05 -0400734 yv12->y_buffer = img->planes[VPX_PLANE_Y];
735 yv12->u_buffer = img->planes[VPX_PLANE_U];
736 yv12->v_buffer = img->planes[VPX_PLANE_V];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400737
Johann69dc8762014-08-15 10:50:40 -0700738 yv12->y_crop_width = y_w;
739 yv12->y_crop_height = y_h;
740 yv12->y_width = y_w;
741 yv12->y_height = y_h;
742 yv12->uv_crop_width = uv_w;
743 yv12->uv_crop_height = uv_h;
744 yv12->uv_width = uv_w;
745 yv12->uv_height = uv_h;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400746
John Koleszarb6c71912010-05-24 21:45:05 -0400747 yv12->y_stride = img->stride[VPX_PLANE_Y];
748 yv12->uv_stride = img->stride[VPX_PLANE_U];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400749
John Koleszarb6c71912010-05-24 21:45:05 -0400750 yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400751 return res;
752}
753
754static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
755 unsigned long duration,
756 unsigned long deadline)
757{
758 unsigned int new_qc;
759
760#if !(CONFIG_REALTIME_ONLY)
761 /* Use best quality mode if no deadline is given. */
762 new_qc = MODE_BESTQUALITY;
763
764 if (deadline)
765 {
766 uint64_t duration_us;
767
768 /* Convert duration parameter from stream timebase to microseconds */
769 duration_us = (uint64_t)duration * 1000000
770 * (uint64_t)ctx->cfg.g_timebase.num
771 / (uint64_t)ctx->cfg.g_timebase.den;
772
773 /* If the deadline is more that the duration this frame is to be shown,
774 * use good quality mode. Otherwise use realtime mode.
775 */
776 new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
777 }
778
779#else
780 new_qc = MODE_REALTIME;
781#endif
782
John Koleszar0ea50ce2010-05-18 11:58:33 -0400783 if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS)
784 new_qc = MODE_FIRSTPASS;
785 else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS)
786 new_qc = (new_qc == MODE_BESTQUALITY)
787 ? MODE_SECONDPASS_BEST
788 : MODE_SECONDPASS;
789
790 if (ctx->oxcf.Mode != new_qc)
791 {
792 ctx->oxcf.Mode = new_qc;
793 vp8_change_config(ctx->cpi, &ctx->oxcf);
794 }
795}
796
Marcoaf898b52014-11-10 13:07:05 -0800797static vpx_codec_err_t set_reference_and_update(vpx_codec_alg_priv_t *ctx,
798 int flags)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400799{
John Koleszar0ea50ce2010-05-18 11:58:33 -0400800
801 /* Handle Flags */
802 if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF))
803 || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF)))
804 {
805 ctx->base.err_detail = "Conflicting flags.";
806 return VPX_CODEC_INVALID_PARAM;
807 }
808
809 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF
810 | VP8_EFLAG_NO_REF_ARF))
811 {
812 int ref = 7;
813
814 if (flags & VP8_EFLAG_NO_REF_LAST)
Johann965d8682012-05-23 16:08:37 -0700815 ref ^= VP8_LAST_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400816
817 if (flags & VP8_EFLAG_NO_REF_GF)
Johann965d8682012-05-23 16:08:37 -0700818 ref ^= VP8_GOLD_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400819
820 if (flags & VP8_EFLAG_NO_REF_ARF)
Johann965d8682012-05-23 16:08:37 -0700821 ref ^= VP8_ALTR_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400822
823 vp8_use_as_reference(ctx->cpi, ref);
824 }
825
826 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF
827 | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF
828 | VP8_EFLAG_FORCE_ARF))
829 {
830 int upd = 7;
831
832 if (flags & VP8_EFLAG_NO_UPD_LAST)
Johann965d8682012-05-23 16:08:37 -0700833 upd ^= VP8_LAST_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400834
835 if (flags & VP8_EFLAG_NO_UPD_GF)
Johann965d8682012-05-23 16:08:37 -0700836 upd ^= VP8_GOLD_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400837
838 if (flags & VP8_EFLAG_NO_UPD_ARF)
Johann965d8682012-05-23 16:08:37 -0700839 upd ^= VP8_ALTR_FRAME;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400840
841 vp8_update_reference(ctx->cpi, upd);
842 }
843
844 if (flags & VP8_EFLAG_NO_UPD_ENTROPY)
845 {
846 vp8_update_entropy(ctx->cpi, 0);
847 }
848
Marcoaf898b52014-11-10 13:07:05 -0800849 return VPX_CODEC_OK;
850}
851
852static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx,
853 const vpx_image_t *img,
854 vpx_codec_pts_t pts,
855 unsigned long duration,
856 vpx_enc_frame_flags_t flags,
857 unsigned long deadline)
858{
859 vpx_codec_err_t res = VPX_CODEC_OK;
860
861 if (!ctx->cfg.rc_target_bitrate)
862 return res;
863
864 if (!ctx->cfg.rc_target_bitrate)
865 return res;
866
867 if (img)
868 res = validate_img(ctx, img);
869
870 if (!res)
871 res = validate_config(ctx, &ctx->cfg, &ctx->vp8_cfg, 1);
872
873 pick_quickcompress_mode(ctx, duration, deadline);
874 vpx_codec_pkt_list_init(&ctx->pkt_list);
875
876 // If no flags are set in the encode call, then use the frame flags as
877 // defined via the control function: vp8e_set_frame_flags.
878 if (!flags) {
879 flags = ctx->control_frame_flags;
880 }
881 ctx->control_frame_flags = 0;
882
883 res = set_reference_and_update(ctx, flags);
884
John Koleszar0ea50ce2010-05-18 11:58:33 -0400885 /* Handle fixed keyframe intervals */
886 if (ctx->cfg.kf_mode == VPX_KF_AUTO
887 && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist)
888 {
889 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist)
890 {
891 flags |= VPX_EFLAG_FORCE_KF;
Andoni Morales Alastruey48140162011-02-07 18:04:02 +0100892 ctx->fixed_kf_cntr = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400893 }
894 }
895
896 /* Initialize the encoder instance on the first frame*/
897 if (!res && ctx->cpi)
898 {
899 unsigned int lib_flags;
900 YV12_BUFFER_CONFIG sd;
James Zernb45065d2011-07-25 18:44:59 -0700901 int64_t dst_time_stamp, dst_end_time_stamp;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400902 unsigned long size, cx_data_sz;
903 unsigned char *cx_data;
James Berrybc715112011-10-12 11:18:50 -0400904 unsigned char *cx_data_end;
905 int comp_data_state = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400906
907 /* Set up internal flags */
908 if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
909 ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1;
910
Stefan Holmer7296b3f2011-06-13 16:42:27 +0200911 if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION)
912 ((VP8_COMP *)ctx->cpi)->output_partition = 1;
913
John Koleszar0ea50ce2010-05-18 11:58:33 -0400914 /* Convert API flags to internal codec lib flags */
915 lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
916
917 /* vp8 use 10,000,000 ticks/second as time stamp */
918 dst_time_stamp = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
919 dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
920
921 if (img != NULL)
922 {
923 res = image2yuvconfig(img, &sd);
924
925 if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
926 &sd, dst_time_stamp, dst_end_time_stamp))
927 {
928 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
929 res = update_error_state(ctx, &cpi->common.error);
930 }
931
932 /* reset for next frame */
933 ctx->next_frame_flag = 0;
934 }
935
936 cx_data = ctx->cx_data;
937 cx_data_sz = ctx->cx_data_sz;
James Berrybc715112011-10-12 11:18:50 -0400938 cx_data_end = ctx->cx_data + cx_data_sz;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400939 lib_flags = 0;
940
James Berrybc715112011-10-12 11:18:50 -0400941 while (cx_data_sz >= ctx->cx_data_sz / 2)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400942 {
James Berrybc715112011-10-12 11:18:50 -0400943 comp_data_state = vp8_get_compressed_data(ctx->cpi,
944 &lib_flags,
945 &size,
946 cx_data,
947 cx_data_end,
948 &dst_time_stamp,
949 &dst_end_time_stamp,
950 !img);
951
952 if(comp_data_state == VPX_CODEC_CORRUPT_FRAME)
953 return VPX_CODEC_CORRUPT_FRAME;
954 else if(comp_data_state == -1)
955 break;
956
John Koleszar0ea50ce2010-05-18 11:58:33 -0400957 if (size)
958 {
959 vpx_codec_pts_t round, delta;
960 vpx_codec_cx_pkt_t pkt;
961 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
962
963 /* Add the frame packet to the list of returned packets. */
Dmitry Kovalev947748e2014-04-25 17:39:48 -0700964 round = (vpx_codec_pts_t)10000000
James Zern429743c2012-08-07 17:12:10 -0700965 * ctx->cfg.g_timebase.num / 2 - 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400966 delta = (dst_end_time_stamp - dst_time_stamp);
967 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400968 pkt.data.frame.pts =
969 (dst_time_stamp * ctx->cfg.g_timebase.den + round)
970 / ctx->cfg.g_timebase.num / 10000000;
Yaowu Xud71ba032012-08-17 10:05:35 -0700971 pkt.data.frame.duration = (unsigned long)
972 ((delta * ctx->cfg.g_timebase.den + round)
973 / ctx->cfg.g_timebase.num / 10000000);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400974 pkt.data.frame.flags = lib_flags << 16;
975
976 if (lib_flags & FRAMEFLAGS_KEY)
977 pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
978
979 if (!cpi->common.show_frame)
980 {
981 pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
982
John Koleszar0164a1c2012-05-21 14:30:56 -0700983 /* This timestamp should be as close as possible to the
984 * prior PTS so that if a decoder uses pts to schedule when
985 * to do this, we start right after last frame was decoded.
986 * Invisible frames have no duration.
987 */
Frank Galligan45e64942010-10-05 17:46:37 -0400988 pkt.data.frame.pts = ((cpi->last_time_stamp_seen
989 * ctx->cfg.g_timebase.den + round)
990 / ctx->cfg.g_timebase.num / 10000000) + 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400991 pkt.data.frame.duration = 0;
992 }
993
John Koleszar37de0b82011-07-07 10:38:23 -0400994 if (cpi->droppable)
995 pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE;
996
Stefan Holmer7296b3f2011-06-13 16:42:27 +0200997 if (cpi->output_partition)
998 {
999 int i;
1000 const int num_partitions =
1001 (1 << cpi->common.multi_token_partition) + 1;
Attila Nagy0afcc762011-07-20 14:09:42 +03001002
1003 pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT;
1004
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001005 for (i = 0; i < num_partitions; ++i)
1006 {
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001007#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1008 pkt.data.frame.buf = cpi->partition_d[i];
1009#else
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001010 pkt.data.frame.buf = cx_data;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001011 cx_data += cpi->partition_sz[i];
1012 cx_data_sz -= cpi->partition_sz[i];
1013#endif
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001014 pkt.data.frame.sz = cpi->partition_sz[i];
1015 pkt.data.frame.partition_id = i;
1016 /* don't set the fragment bit for the last partition */
Attila Nagy0afcc762011-07-20 14:09:42 +03001017 if (i == (num_partitions - 1))
1018 pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT;
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001019 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001020 }
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001021#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1022 /* In lagged mode the encoder can buffer multiple frames.
1023 * We don't want this in partitioned output because
1024 * partitions are spread all over the output buffer.
1025 * So, force an exit!
1026 */
1027 cx_data_sz -= ctx->cx_data_sz / 2;
1028#endif
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001029 }
1030 else
1031 {
1032 pkt.data.frame.buf = cx_data;
1033 pkt.data.frame.sz = size;
1034 pkt.data.frame.partition_id = -1;
1035 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1036 cx_data += size;
1037 cx_data_sz -= size;
1038 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001039 }
1040 }
1041 }
1042
1043 return res;
1044}
1045
1046
1047static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx,
1048 vpx_codec_iter_t *iter)
1049{
1050 return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1051}
1052
1053static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001054 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001055{
1056 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1057
1058 if (data)
1059 {
1060 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1061 YV12_BUFFER_CONFIG sd;
1062
1063 image2yuvconfig(&frame->img, &sd);
1064 vp8_set_reference(ctx->cpi, frame->frame_type, &sd);
1065 return VPX_CODEC_OK;
1066 }
1067 else
1068 return VPX_CODEC_INVALID_PARAM;
1069
1070}
1071
1072static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001073 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001074{
1075
1076 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1077
1078 if (data)
1079 {
1080 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1081 YV12_BUFFER_CONFIG sd;
1082
1083 image2yuvconfig(&frame->img, &sd);
1084 vp8_get_reference(ctx->cpi, frame->frame_type, &sd);
1085 return VPX_CODEC_OK;
1086 }
1087 else
1088 return VPX_CODEC_INVALID_PARAM;
1089}
1090
1091static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001092 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001093{
James Zern76640f82010-08-20 16:06:56 -04001094#if CONFIG_POSTPROC
John Koleszar0ea50ce2010-05-18 11:58:33 -04001095 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
1096
1097 if (data)
1098 {
1099 ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
1100 return VPX_CODEC_OK;
1101 }
1102 else
1103 return VPX_CODEC_INVALID_PARAM;
James Zern76640f82010-08-20 16:06:56 -04001104#else
1105 (void)ctx;
James Zern76640f82010-08-20 16:06:56 -04001106 (void)args;
1107 return VPX_CODEC_INCAPABLE;
1108#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -04001109}
1110
1111
1112static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx)
1113{
1114
1115 YV12_BUFFER_CONFIG sd;
Fritz Koenig647df002010-11-04 16:03:36 -07001116 vp8_ppflags_t flags = {0};
John Koleszar0ea50ce2010-05-18 11:58:33 -04001117
Fritz Koenig647df002010-11-04 16:03:36 -07001118 if (ctx->preview_ppcfg.post_proc_flag)
1119 {
1120 flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
1121 flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
1122 flags.noise_level = ctx->preview_ppcfg.noise_level;
1123 }
1124
1125 if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags))
John Koleszar0ea50ce2010-05-18 11:58:33 -04001126 {
1127
1128 /*
James Zern6cd4a102010-05-20 23:22:39 -04001129 vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001130 sd.y_width + 2*VP8BORDERINPIXELS,
1131 sd.y_height + 2*VP8BORDERINPIXELS,
1132 1,
1133 sd.buffer_alloc);
1134 vpx_img_set_rect(&ctx->preview_img,
1135 VP8BORDERINPIXELS, VP8BORDERINPIXELS,
1136 sd.y_width, sd.y_height);
1137 */
1138
1139 ctx->preview_img.bps = 12;
John Koleszarb6c71912010-05-24 21:45:05 -04001140 ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer;
1141 ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer;
1142 ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001143
James Zern4fc6c882013-07-12 14:11:53 -07001144 ctx->preview_img.fmt = VPX_IMG_FMT_I420;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001145 ctx->preview_img.x_chroma_shift = 1;
1146 ctx->preview_img.y_chroma_shift = 1;
1147
James Berryddacf1c2011-02-08 14:23:18 -05001148 ctx->preview_img.d_w = sd.y_width;
1149 ctx->preview_img.d_h = sd.y_height;
John Koleszarb6c71912010-05-24 21:45:05 -04001150 ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride;
1151 ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride;
1152 ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001153 ctx->preview_img.w = sd.y_width;
1154 ctx->preview_img.h = sd.y_height;
1155
1156 return &ctx->preview_img;
1157 }
1158 else
1159 return NULL;
1160}
1161
1162static vpx_codec_err_t vp8e_update_entropy(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001163 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001164{
1165 int update = va_arg(args, int);
1166 vp8_update_entropy(ctx->cpi, update);
1167 return VPX_CODEC_OK;
1168
1169}
1170
1171static vpx_codec_err_t vp8e_update_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001172 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001173{
1174 int update = va_arg(args, int);
1175 vp8_update_reference(ctx->cpi, update);
1176 return VPX_CODEC_OK;
1177}
1178
1179static vpx_codec_err_t vp8e_use_reference(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001180 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001181{
1182 int reference_flag = va_arg(args, int);
1183 vp8_use_as_reference(ctx->cpi, reference_flag);
1184 return VPX_CODEC_OK;
1185}
1186
Marcoaf898b52014-11-10 13:07:05 -08001187static vpx_codec_err_t vp8e_set_frame_flags(vpx_codec_alg_priv_t *ctx,
1188 va_list args)
1189{
1190 int frame_flags = va_arg(args, int);
1191 ctx->control_frame_flags = frame_flags;
1192 return set_reference_and_update(ctx, frame_flags);
1193}
1194
1195static vpx_codec_err_t vp8e_set_temporal_layer_id(vpx_codec_alg_priv_t *ctx,
1196 va_list args)
1197{
1198 int layer_id = va_arg(args, int);
1199 if (layer_id < 0 || layer_id >= (int)ctx->cfg.ts_number_layers) {
1200 return VPX_CODEC_INVALID_PARAM;
1201 }
1202 ctx->cpi->temporal_layer_id = layer_id;
1203 return VPX_CODEC_OK;
1204}
1205
John Koleszar0ea50ce2010-05-18 11:58:33 -04001206static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001207 va_list args)
1208{
1209 vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
1210
1211 if (data)
1212 {
1213 vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
1214
1215 if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols, roi->delta_q, roi->delta_lf, roi->static_threshold))
1216 return VPX_CODEC_OK;
1217 else
1218 return VPX_CODEC_INVALID_PARAM;
1219 }
1220 else
1221 return VPX_CODEC_INVALID_PARAM;
1222}
1223
1224
1225static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001226 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001227{
1228 vpx_active_map_t *data = va_arg(args, vpx_active_map_t *);
1229
1230 if (data)
1231 {
1232
1233 vpx_active_map_t *map = (vpx_active_map_t *)data;
1234
1235 if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols))
1236 return VPX_CODEC_OK;
1237 else
1238 return VPX_CODEC_INVALID_PARAM;
1239 }
1240 else
1241 return VPX_CODEC_INVALID_PARAM;
1242}
1243
1244static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx,
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001245 va_list args)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001246{
1247
1248 vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *);
1249
1250 if (data)
1251 {
1252 int res;
1253 vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data ;
Frank Galliganbc45f232013-01-11 15:34:05 -08001254 res = vp8_set_internal_size(ctx->cpi,
1255 (VPX_SCALING)scalemode.h_scaling_mode,
1256 (VPX_SCALING)scalemode.v_scaling_mode);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001257
1258 if (!res)
1259 {
1260 /*force next frame a key frame to effect scaling mode */
1261 ctx->next_frame_flag |= FRAMEFLAGS_KEY;
1262 return VPX_CODEC_OK;
1263 }
1264 else
1265 return VPX_CODEC_INVALID_PARAM;
1266 }
1267 else
1268 return VPX_CODEC_INVALID_PARAM;
1269}
1270
1271
1272static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] =
1273{
1274 {VP8_SET_REFERENCE, vp8e_set_reference},
1275 {VP8_COPY_REFERENCE, vp8e_get_reference},
1276 {VP8_SET_POSTPROC, vp8e_set_previewpp},
1277 {VP8E_UPD_ENTROPY, vp8e_update_entropy},
1278 {VP8E_UPD_REFERENCE, vp8e_update_reference},
1279 {VP8E_USE_REFERENCE, vp8e_use_reference},
Marcoaf898b52014-11-10 13:07:05 -08001280 {VP8E_SET_FRAME_FLAGS, vp8e_set_frame_flags},
1281 {VP8E_SET_TEMPORAL_LAYER_ID, vp8e_set_temporal_layer_id},
John Koleszar0ea50ce2010-05-18 11:58:33 -04001282 {VP8E_SET_ROI_MAP, vp8e_set_roi_map},
1283 {VP8E_SET_ACTIVEMAP, vp8e_set_activemap},
1284 {VP8E_SET_SCALEMODE, vp8e_set_scalemode},
Dmitry Kovalev8a8b6622014-05-27 16:45:58 -07001285 {VP8E_SET_CPUUSED, set_cpu_used},
1286 {VP8E_SET_NOISE_SENSITIVITY, set_noise_sensitivity},
1287 {VP8E_SET_ENABLEAUTOALTREF, set_enable_auto_alt_ref},
1288 {VP8E_SET_SHARPNESS, set_sharpness},
1289 {VP8E_SET_STATIC_THRESHOLD, set_static_thresh},
1290 {VP8E_SET_TOKEN_PARTITIONS, set_token_partitions},
1291 {VP8E_GET_LAST_QUANTIZER, get_quantizer},
1292 {VP8E_GET_LAST_QUANTIZER_64, get_quantizer64},
1293 {VP8E_SET_ARNR_MAXFRAMES, set_arnr_max_frames},
1294 {VP8E_SET_ARNR_STRENGTH , set_arnr_strength},
1295 {VP8E_SET_ARNR_TYPE , set_arnr_type},
1296 {VP8E_SET_TUNING, set_tuning},
1297 {VP8E_SET_CQ_LEVEL, set_cq_level},
1298 {VP8E_SET_MAX_INTRA_BITRATE_PCT, set_rc_max_intra_bitrate_pct},
Marcoaf898b52014-11-10 13:07:05 -08001299 {VP8E_SET_SCREEN_CONTENT_MODE, set_screen_content_mode},
John Koleszar0ea50ce2010-05-18 11:58:33 -04001300 { -1, NULL},
1301};
1302
1303static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] =
1304{
1305 {
1306 0,
1307 {
1308 0, /* g_usage */
1309 0, /* g_threads */
1310 0, /* g_profile */
1311
1312 320, /* g_width */
1313 240, /* g_height */
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001314 VPX_BITS_8, /* g_bit_depth */
1315 8, /* g_input_bit_depth */
1316
John Koleszar0ea50ce2010-05-18 11:58:33 -04001317 {1, 30}, /* g_timebase */
1318
1319 0, /* g_error_resilient */
1320
1321 VPX_RC_ONE_PASS, /* g_pass */
1322
1323 0, /* g_lag_in_frames */
1324
John Koleszar23216212010-09-02 09:32:03 -04001325 0, /* rc_dropframe_thresh */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001326 0, /* rc_resize_allowed */
Adrian Grangef7bd1272014-04-09 14:51:29 -07001327 1, /* rc_scaled_width */
1328 1, /* rc_scaled_height */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001329 60, /* rc_resize_down_thresold */
1330 30, /* rc_resize_up_thresold */
1331
1332 VPX_VBR, /* rc_end_usage */
1333#if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
1334 {0}, /* rc_twopass_stats_in */
Pengchong Jinf349b072014-07-14 09:13:38 -07001335 {0}, /* rc_firstpass_mb_stats_in */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001336#endif
1337 256, /* rc_target_bandwidth */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001338 4, /* rc_min_quantizer */
1339 63, /* rc_max_quantizer */
John Koleszarc99f9d72011-04-11 11:29:23 -04001340 100, /* rc_undershoot_pct */
1341 100, /* rc_overshoot_pct */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001342
1343 6000, /* rc_max_buffer_size */
1344 4000, /* rc_buffer_initial_size; */
1345 5000, /* rc_buffer_optimal_size; */
1346
1347 50, /* rc_two_pass_vbrbias */
1348 0, /* rc_two_pass_vbrmin_section */
1349 400, /* rc_two_pass_vbrmax_section */
1350
1351 /* keyframing settings (kf) */
1352 VPX_KF_AUTO, /* g_kfmode*/
1353 0, /* kf_min_dist */
Ralph Giles2a0d7b12012-01-05 10:39:38 -06001354 128, /* kf_max_dist */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001355
1356#if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION)
John Koleszar0ea50ce2010-05-18 11:58:33 -04001357 "vp8.fpf" /* first pass filename */
1358#endif
Ivan Maltz01b35c32013-09-05 08:55:47 -07001359 VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */
Minghai Shange8998592014-07-14 11:24:17 -07001360 {0},
Minghai Shang8c196b22014-02-26 13:30:50 -08001361 {0}, /* ss_target_bitrate */
Adrian Grange217591f2011-10-06 15:49:11 -07001362 1, /* ts_number_layers */
1363 {0}, /* ts_target_bitrate */
1364 {0}, /* ts_rate_decimator */
1365 0, /* ts_periodicity */
1366 {0}, /* ts_layer_id */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001367 }},
John Koleszar0ea50ce2010-05-18 11:58:33 -04001368};
1369
1370
1371#ifndef VERSION_STRING
1372#define VERSION_STRING
1373#endif
John Koleszarfa7a55b2010-09-21 10:35:52 -04001374CODEC_INTERFACE(vpx_codec_vp8_cx) =
John Koleszar0ea50ce2010-05-18 11:58:33 -04001375{
John Koleszar317a6662010-06-10 12:08:01 -04001376 "WebM Project VP8 Encoder" VERSION_STRING,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001377 VPX_CODEC_INTERNAL_ABI_VERSION,
Stefan Holmer7296b3f2011-06-13 16:42:27 +02001378 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR |
1379 VPX_CODEC_CAP_OUTPUT_PARTITION,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001380 /* vpx_codec_caps_t caps; */
1381 vp8e_init, /* vpx_codec_init_fn_t init; */
1382 vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */
1383 vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001384 {
Dmitry Kovalevcd6d9842014-08-20 17:16:28 -07001385 NULL, /* vpx_codec_peek_si_fn_t peek_si; */
1386 NULL, /* vpx_codec_get_si_fn_t get_si; */
1387 NULL, /* vpx_codec_decode_fn_t decode; */
1388 NULL, /* vpx_codec_frame_get_fn_t frame_get; */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001389 },
1390 {
Jim Bankoski8a774e12014-07-31 06:27:57 -07001391 1, /* 1 cfg map */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001392 vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t peek_si; */
1393 vp8e_encode, /* vpx_codec_encode_fn_t encode; */
1394 vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t frame_get; */
1395 vp8e_set_config,
Dmitry Kovalevcd6d9842014-08-20 17:16:28 -07001396 NULL,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001397 vp8e_get_preview,
Yunqing Wangaa7335e2011-10-25 15:14:16 -04001398 vp8e_mr_alloc_mem,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001399 } /* encoder functions */
1400};