1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  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
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "./vpx_config.h"
12 #include "./vp8_rtcd.h"
13 #include "./vpx_dsp_rtcd.h"
14 #include "./vpx_scale_rtcd.h"
15 #include "vpx/vpx_codec.h"
16 #include "vpx/internal/vpx_codec_internal.h"
17 #include "vpx_version.h"
18 #include "vpx_mem/vpx_mem.h"
19 #include "vpx_ports/system_state.h"
20 #include "vpx_ports/vpx_once.h"
21 #include "vpx_util/vpx_timestamp.h"
22 #include "vp8/encoder/onyx_int.h"
23 #include "vpx/vp8cx.h"
24 #include "vp8/encoder/firstpass.h"
25 #include "vp8/common/onyx.h"
26 #include "vp8/common/common.h"
27 #include <stdlib.h>
28 #include <string.h>
29 
30 struct vp8_extracfg {
31   struct vpx_codec_pkt_list *pkt_list;
32   int cpu_used; /** available cpu percentage in 1/16*/
33   /** if encoder decides to uses alternate reference frame */
34   unsigned int enable_auto_alt_ref;
35   unsigned int noise_sensitivity;
36   unsigned int Sharpness;
37   unsigned int static_thresh;
38   unsigned int token_partitions;
39   unsigned int arnr_max_frames; /* alt_ref Noise Reduction Max Frame Count */
40   unsigned int arnr_strength;   /* alt_ref Noise Reduction Strength */
41   unsigned int arnr_type;       /* alt_ref filter type */
42   vp8e_tuning tuning;
43   unsigned int cq_level; /* constrained quality level */
44   unsigned int rc_max_intra_bitrate_pct;
45   unsigned int gf_cbr_boost_pct;
46   unsigned int screen_content_mode;
47 };
48 
49 static struct vp8_extracfg default_extracfg = {
50   NULL,
51 #if !(CONFIG_REALTIME_ONLY)
52   0, /* cpu_used      */
53 #else
54   4,                      /* cpu_used      */
55 #endif
56   0, /* enable_auto_alt_ref */
57   0, /* noise_sensitivity */
58   0, /* Sharpness */
59   0, /* static_thresh */
60 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
61   VP8_EIGHT_TOKENPARTITION,
62 #else
63   VP8_ONE_TOKENPARTITION, /* token_partitions */
64 #endif
65   0,  /* arnr_max_frames */
66   3,  /* arnr_strength */
67   3,  /* arnr_type*/
68   0,  /* tuning*/
69   10, /* cq_level */
70   0,  /* rc_max_intra_bitrate_pct */
71   0,  /* gf_cbr_boost_pct */
72   0,  /* screen_content_mode */
73 };
74 
75 struct vpx_codec_alg_priv {
76   vpx_codec_priv_t base;
77   vpx_codec_enc_cfg_t cfg;
78   struct vp8_extracfg vp8_cfg;
79   vpx_rational64_t timestamp_ratio;
80   vpx_codec_pts_t pts_offset;
81   unsigned char pts_offset_initialized;
82   VP8_CONFIG oxcf;
83   struct VP8_COMP *cpi;
84   unsigned char *cx_data;
85   unsigned int cx_data_sz;
86   vpx_image_t preview_img;
87   unsigned int next_frame_flag;
88   vp8_postproc_cfg_t preview_ppcfg;
89   /* pkt_list size depends on the maximum number of lagged frames allowed. */
90   vpx_codec_pkt_list_decl(64) pkt_list;
91   unsigned int fixed_kf_cntr;
92   vpx_enc_frame_flags_t control_frame_flags;
93 };
94 
update_error_state(vpx_codec_alg_priv_t * ctx,const struct vpx_internal_error_info * error)95 static vpx_codec_err_t update_error_state(
96     vpx_codec_alg_priv_t *ctx, const struct vpx_internal_error_info *error) {
97   vpx_codec_err_t res;
98 
99   if ((res = error->error_code)) {
100     ctx->base.err_detail = error->has_detail ? error->detail : NULL;
101   }
102 
103   return res;
104 }
105 
106 #undef ERROR
107 #define ERROR(str)                  \
108   do {                              \
109     ctx->base.err_detail = str;     \
110     return VPX_CODEC_INVALID_PARAM; \
111   } while (0)
112 
113 #define RANGE_CHECK(p, memb, lo, hi)                                     \
114   do {                                                                   \
115     if (!(((p)->memb == (lo) || (p)->memb > (lo)) && (p)->memb <= (hi))) \
116       ERROR(#memb " out of range [" #lo ".." #hi "]");                   \
117   } while (0)
118 
119 #define RANGE_CHECK_HI(p, memb, hi)                                     \
120   do {                                                                  \
121     if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
122   } while (0)
123 
124 #define RANGE_CHECK_LO(p, memb, lo)                                     \
125   do {                                                                  \
126     if (!((p)->memb >= (lo))) ERROR(#memb " out of range [" #lo "..]"); \
127   } while (0)
128 
129 #define RANGE_CHECK_BOOL(p, memb)                                     \
130   do {                                                                \
131     if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
132   } while (0)
133 
134 #if defined(_MSC_VER)
135 #define COMPILE_TIME_ASSERT(boolexp)              \
136   do {                                            \
137     char compile_time_assert[(boolexp) ? 1 : -1]; \
138     (void)compile_time_assert;                    \
139   } while (0)
140 #else /* !_MSC_VER */
141 #define COMPILE_TIME_ASSERT(boolexp)                         \
142   do {                                                       \
143     struct {                                                 \
144       unsigned int compile_time_assert : (boolexp) ? 1 : -1; \
145     } compile_time_assert;                                   \
146     (void)compile_time_assert;                               \
147   } while (0)
148 #endif /* _MSC_VER */
149 
validate_config(vpx_codec_alg_priv_t * ctx,const vpx_codec_enc_cfg_t * cfg,const struct vp8_extracfg * vp8_cfg,int finalize)150 static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
151                                        const vpx_codec_enc_cfg_t *cfg,
152                                        const struct vp8_extracfg *vp8_cfg,
153                                        int finalize) {
154   RANGE_CHECK(cfg, g_w, 1, 16383); /* 14 bits available */
155   RANGE_CHECK(cfg, g_h, 1, 16383); /* 14 bits available */
156   RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
157   RANGE_CHECK(cfg, g_timebase.num, 1, 1000000000);
158   RANGE_CHECK_HI(cfg, g_profile, 3);
159   RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
160   RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
161   RANGE_CHECK_HI(cfg, g_threads, 64);
162 #if CONFIG_REALTIME_ONLY
163   RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
164 #elif CONFIG_MULTI_RES_ENCODING
165   if (ctx->base.enc.total_encoders > 1) RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
166 #else
167   RANGE_CHECK_HI(cfg, g_lag_in_frames, 25);
168 #endif
169   RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
170   RANGE_CHECK_HI(cfg, rc_undershoot_pct, 1000);
171   RANGE_CHECK_HI(cfg, rc_overshoot_pct, 1000);
172   RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
173   RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
174 
175 /* TODO: add spatial re-sampling support and frame dropping in
176  * multi-res-encoder.*/
177 #if CONFIG_MULTI_RES_ENCODING
178   if (ctx->base.enc.total_encoders > 1)
179     RANGE_CHECK_HI(cfg, rc_resize_allowed, 0);
180 #else
181   RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
182 #endif
183   RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
184   RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
185   RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
186 
187 #if CONFIG_REALTIME_ONLY
188   RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
189 #elif CONFIG_MULTI_RES_ENCODING
190   if (ctx->base.enc.total_encoders > 1)
191     RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
192 #else
193   RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
194 #endif
195 
196   /* VP8 does not support a lower bound on the keyframe interval in
197    * automatic keyframe placement mode.
198    */
199   if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist &&
200       cfg->kf_min_dist > 0)
201     ERROR(
202         "kf_min_dist not supported in auto mode, use 0 "
203         "or kf_max_dist instead.");
204 
205   RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref);
206   RANGE_CHECK(vp8_cfg, cpu_used, -16, 16);
207 
208 #if CONFIG_REALTIME_ONLY && !CONFIG_TEMPORAL_DENOISING
209   RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0);
210 #else
211   RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6);
212 #endif
213 
214   RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION,
215               VP8_EIGHT_TOKENPARTITION);
216   RANGE_CHECK_HI(vp8_cfg, Sharpness, 7);
217   RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
218   RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6);
219   RANGE_CHECK(vp8_cfg, arnr_type, 1, 3);
220   RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
221   RANGE_CHECK_HI(vp8_cfg, screen_content_mode, 2);
222   if (finalize && (cfg->rc_end_usage == VPX_CQ || cfg->rc_end_usage == VPX_Q))
223     RANGE_CHECK(vp8_cfg, cq_level, cfg->rc_min_quantizer,
224                 cfg->rc_max_quantizer);
225 
226 #if !(CONFIG_REALTIME_ONLY)
227   if (cfg->g_pass == VPX_RC_LAST_PASS) {
228     size_t packet_sz = sizeof(FIRSTPASS_STATS);
229     int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
230     FIRSTPASS_STATS *stats;
231 
232     if (!cfg->rc_twopass_stats_in.buf)
233       ERROR("rc_twopass_stats_in.buf not set.");
234 
235     if (cfg->rc_twopass_stats_in.sz % packet_sz)
236       ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
237 
238     if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
239       ERROR("rc_twopass_stats_in requires at least two packets.");
240 
241     stats = (void *)((char *)cfg->rc_twopass_stats_in.buf +
242                      (n_packets - 1) * packet_sz);
243 
244     if ((int)(stats->count + 0.5) != n_packets - 1)
245       ERROR("rc_twopass_stats_in missing EOS stats packet");
246   }
247 #endif
248 
249   RANGE_CHECK(cfg, ts_number_layers, 1, 5);
250 
251   if (cfg->ts_number_layers > 1) {
252     unsigned int i;
253     RANGE_CHECK_HI(cfg, ts_periodicity, 16);
254 
255     for (i = 1; i < cfg->ts_number_layers; ++i) {
256       if (cfg->ts_target_bitrate[i] <= cfg->ts_target_bitrate[i - 1] &&
257           cfg->rc_target_bitrate > 0)
258         ERROR("ts_target_bitrate entries are not strictly increasing");
259     }
260 
261     RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers - 1], 1, 1);
262     for (i = cfg->ts_number_layers - 2; i > 0; i--) {
263       if (cfg->ts_rate_decimator[i - 1] != 2 * cfg->ts_rate_decimator[i])
264         ERROR("ts_rate_decimator factors are not powers of 2");
265     }
266 
267     RANGE_CHECK_HI(cfg, ts_layer_id[i], cfg->ts_number_layers - 1);
268   }
269 
270 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
271   if (cfg->g_threads > (1 << vp8_cfg->token_partitions))
272     ERROR("g_threads cannot be bigger than number of token partitions");
273 #endif
274 
275   return VPX_CODEC_OK;
276 }
277 
validate_img(vpx_codec_alg_priv_t * ctx,const vpx_image_t * img)278 static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
279                                     const vpx_image_t *img) {
280   switch (img->fmt) {
281     case VPX_IMG_FMT_YV12:
282     case VPX_IMG_FMT_I420: break;
283     default:
284       ERROR("Invalid image format. Only YV12 and I420 images are supported");
285   }
286 
287   if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
288     ERROR("Image size must match encoder init configuration size");
289 
290   return VPX_CODEC_OK;
291 }
292 
set_vp8e_config(VP8_CONFIG * oxcf,vpx_codec_enc_cfg_t cfg,struct vp8_extracfg vp8_cfg,vpx_codec_priv_enc_mr_cfg_t * mr_cfg)293 static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf,
294                                        vpx_codec_enc_cfg_t cfg,
295                                        struct vp8_extracfg vp8_cfg,
296                                        vpx_codec_priv_enc_mr_cfg_t *mr_cfg) {
297   oxcf->multi_threaded = cfg.g_threads;
298   oxcf->Version = cfg.g_profile;
299 
300   oxcf->Width = cfg.g_w;
301   oxcf->Height = cfg.g_h;
302   oxcf->timebase = cfg.g_timebase;
303 
304   oxcf->error_resilient_mode = cfg.g_error_resilient;
305 
306   switch (cfg.g_pass) {
307     case VPX_RC_ONE_PASS: oxcf->Mode = MODE_BESTQUALITY; break;
308     case VPX_RC_FIRST_PASS: oxcf->Mode = MODE_FIRSTPASS; break;
309     case VPX_RC_LAST_PASS: oxcf->Mode = MODE_SECONDPASS_BEST; break;
310   }
311 
312   if (cfg.g_pass == VPX_RC_FIRST_PASS || cfg.g_pass == VPX_RC_ONE_PASS) {
313     oxcf->allow_lag = 0;
314     oxcf->lag_in_frames = 0;
315   } else {
316     oxcf->allow_lag = (cfg.g_lag_in_frames) > 0;
317     oxcf->lag_in_frames = cfg.g_lag_in_frames;
318   }
319 
320   oxcf->allow_df = (cfg.rc_dropframe_thresh > 0);
321   oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh;
322 
323   oxcf->allow_spatial_resampling = cfg.rc_resize_allowed;
324   oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh;
325   oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh;
326 
327   if (cfg.rc_end_usage == VPX_VBR) {
328     oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
329   } else if (cfg.rc_end_usage == VPX_CBR) {
330     oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
331   } else if (cfg.rc_end_usage == VPX_CQ) {
332     oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
333   } else if (cfg.rc_end_usage == VPX_Q) {
334     oxcf->end_usage = USAGE_CONSTANT_QUALITY;
335   }
336 
337   oxcf->target_bandwidth = cfg.rc_target_bitrate;
338   oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct;
339   oxcf->gf_cbr_boost_pct = vp8_cfg.gf_cbr_boost_pct;
340 
341   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;
344   oxcf->fixed_q = -1;
345 
346   oxcf->under_shoot_pct = cfg.rc_undershoot_pct;
347   oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
348 
349   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 
353   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;
356 
357   oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct;
358   oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct;
359   oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct;
360 
361   oxcf->auto_key =
362       cfg.kf_mode == VPX_KF_AUTO && cfg.kf_min_dist != cfg.kf_max_dist;
363   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     memcpy(oxcf->target_bitrate, cfg.ts_target_bitrate,
370            sizeof(cfg.ts_target_bitrate));
371     memcpy(oxcf->rate_decimator, cfg.ts_rate_decimator,
372            sizeof(cfg.ts_rate_decimator));
373     memcpy(oxcf->layer_id, cfg.ts_layer_id, sizeof(cfg.ts_layer_id));
374   }
375 
376 #if CONFIG_MULTI_RES_ENCODING
377   /* When mr_cfg is NULL, oxcf->mr_total_resolutions and oxcf->mr_encoder_id
378    * are both memset to 0, which ensures the correct logic under this
379    * situation.
380    */
381   if (mr_cfg) {
382     oxcf->mr_total_resolutions = mr_cfg->mr_total_resolutions;
383     oxcf->mr_encoder_id = mr_cfg->mr_encoder_id;
384     oxcf->mr_down_sampling_factor.num = mr_cfg->mr_down_sampling_factor.num;
385     oxcf->mr_down_sampling_factor.den = mr_cfg->mr_down_sampling_factor.den;
386     oxcf->mr_low_res_mode_info = mr_cfg->mr_low_res_mode_info;
387   }
388 #else
389   (void)mr_cfg;
390 #endif
391 
392   oxcf->cpu_used = vp8_cfg.cpu_used;
393   oxcf->encode_breakout = vp8_cfg.static_thresh;
394   oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref;
395   oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity;
396   oxcf->Sharpness = vp8_cfg.Sharpness;
397   oxcf->token_partitions = vp8_cfg.token_partitions;
398 
399   oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in;
400   oxcf->output_pkt_list = vp8_cfg.pkt_list;
401 
402   oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
403   oxcf->arnr_strength = vp8_cfg.arnr_strength;
404   oxcf->arnr_type = vp8_cfg.arnr_type;
405 
406   oxcf->tuning = vp8_cfg.tuning;
407 
408   oxcf->screen_content_mode = vp8_cfg.screen_content_mode;
409 
410   /*
411       printf("Current VP8 Settings: \n");
412       printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
413       printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
414       printf("Sharpness: %d\n",    oxcf->Sharpness);
415       printf("cpu_used: %d\n",  oxcf->cpu_used);
416       printf("Mode: %d\n",     oxcf->Mode);
417       printf("auto_key: %d\n",  oxcf->auto_key);
418       printf("key_freq: %d\n", oxcf->key_freq);
419       printf("end_usage: %d\n", oxcf->end_usage);
420       printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
421       printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
422       printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
423       printf("optimal_buffer_level: %d\n",  oxcf->optimal_buffer_level);
424       printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
425       printf("fixed_q: %d\n",  oxcf->fixed_q);
426       printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
427       printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
428       printf("allow_spatial_resampling: %d\n",  oxcf->allow_spatial_resampling);
429       printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
430       printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
431       printf("allow_df: %d\n", oxcf->allow_df);
432       printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
433       printf("two_pass_vbrbias: %d\n",  oxcf->two_pass_vbrbias);
434       printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
435       printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
436       printf("allow_lag: %d\n", oxcf->allow_lag);
437       printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
438       printf("play_alternate: %d\n", oxcf->play_alternate);
439       printf("Version: %d\n", oxcf->Version);
440       printf("multi_threaded: %d\n",   oxcf->multi_threaded);
441       printf("encode_breakout: %d\n", oxcf->encode_breakout);
442   */
443   return VPX_CODEC_OK;
444 }
445 
vp8e_set_config(vpx_codec_alg_priv_t * ctx,const vpx_codec_enc_cfg_t * cfg)446 static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx,
447                                        const vpx_codec_enc_cfg_t *cfg) {
448   vpx_codec_err_t res;
449 
450   if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
451     if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)
452       ERROR("Cannot change width or height after initialization");
453     if ((ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
454         (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
455       ERROR("Cannot increase width or height larger than their initial values");
456   }
457 
458   /* Prevent increasing lag_in_frames. This check is stricter than it needs
459    * to be -- the limit is not increasing past the first lag_in_frames
460    * value, but we don't track the initial config, only the last successful
461    * config.
462    */
463   if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
464     ERROR("Cannot increase lag_in_frames");
465 
466   res = validate_config(ctx, cfg, &ctx->vp8_cfg, 0);
467 
468   if (!res) {
469     ctx->cfg = *cfg;
470     set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
471     vp8_change_config(ctx->cpi, &ctx->oxcf);
472   }
473 
474   return res;
475 }
476 
get_quantizer(vpx_codec_alg_priv_t * ctx,va_list args)477 static vpx_codec_err_t get_quantizer(vpx_codec_alg_priv_t *ctx, va_list args) {
478   int *const arg = va_arg(args, int *);
479   if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
480   *arg = vp8_get_quantizer(ctx->cpi);
481   return VPX_CODEC_OK;
482 }
483 
get_quantizer64(vpx_codec_alg_priv_t * ctx,va_list args)484 static vpx_codec_err_t get_quantizer64(vpx_codec_alg_priv_t *ctx,
485                                        va_list args) {
486   int *const arg = va_arg(args, int *);
487   if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
488   *arg = vp8_reverse_trans(vp8_get_quantizer(ctx->cpi));
489   return VPX_CODEC_OK;
490 }
491 
update_extracfg(vpx_codec_alg_priv_t * ctx,const struct vp8_extracfg * extra_cfg)492 static vpx_codec_err_t update_extracfg(vpx_codec_alg_priv_t *ctx,
493                                        const struct vp8_extracfg *extra_cfg) {
494   const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg, 0);
495   if (res == VPX_CODEC_OK) {
496     ctx->vp8_cfg = *extra_cfg;
497     set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
498     vp8_change_config(ctx->cpi, &ctx->oxcf);
499   }
500   return res;
501 }
502 
set_cpu_used(vpx_codec_alg_priv_t * ctx,va_list args)503 static vpx_codec_err_t set_cpu_used(vpx_codec_alg_priv_t *ctx, va_list args) {
504   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
505   extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
506   // Use fastest speed setting (speed 16 or -16) if it's set beyond the range.
507   extra_cfg.cpu_used = VPXMIN(16, extra_cfg.cpu_used);
508   extra_cfg.cpu_used = VPXMAX(-16, extra_cfg.cpu_used);
509   return update_extracfg(ctx, &extra_cfg);
510 }
511 
set_enable_auto_alt_ref(vpx_codec_alg_priv_t * ctx,va_list args)512 static vpx_codec_err_t set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
513                                                va_list args) {
514   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
515   extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args);
516   return update_extracfg(ctx, &extra_cfg);
517 }
518 
set_noise_sensitivity(vpx_codec_alg_priv_t * ctx,va_list args)519 static vpx_codec_err_t set_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
520                                              va_list args) {
521   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
522   extra_cfg.noise_sensitivity = CAST(VP8E_SET_NOISE_SENSITIVITY, args);
523   return update_extracfg(ctx, &extra_cfg);
524 }
525 
set_sharpness(vpx_codec_alg_priv_t * ctx,va_list args)526 static vpx_codec_err_t set_sharpness(vpx_codec_alg_priv_t *ctx, va_list args) {
527   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
528   extra_cfg.Sharpness = CAST(VP8E_SET_SHARPNESS, args);
529   return update_extracfg(ctx, &extra_cfg);
530 }
531 
set_static_thresh(vpx_codec_alg_priv_t * ctx,va_list args)532 static vpx_codec_err_t set_static_thresh(vpx_codec_alg_priv_t *ctx,
533                                          va_list args) {
534   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
535   extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
536   return update_extracfg(ctx, &extra_cfg);
537 }
538 
set_token_partitions(vpx_codec_alg_priv_t * ctx,va_list args)539 static vpx_codec_err_t set_token_partitions(vpx_codec_alg_priv_t *ctx,
540                                             va_list args) {
541   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
542   extra_cfg.token_partitions = CAST(VP8E_SET_TOKEN_PARTITIONS, args);
543   return update_extracfg(ctx, &extra_cfg);
544 }
545 
set_arnr_max_frames(vpx_codec_alg_priv_t * ctx,va_list args)546 static vpx_codec_err_t set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
547                                            va_list args) {
548   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
549   extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
550   return update_extracfg(ctx, &extra_cfg);
551 }
552 
set_arnr_strength(vpx_codec_alg_priv_t * ctx,va_list args)553 static vpx_codec_err_t set_arnr_strength(vpx_codec_alg_priv_t *ctx,
554                                          va_list args) {
555   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
556   extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
557   return update_extracfg(ctx, &extra_cfg);
558 }
559 
set_arnr_type(vpx_codec_alg_priv_t * ctx,va_list args)560 static vpx_codec_err_t set_arnr_type(vpx_codec_alg_priv_t *ctx, va_list args) {
561   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
562   extra_cfg.arnr_type = CAST(VP8E_SET_ARNR_TYPE, args);
563   return update_extracfg(ctx, &extra_cfg);
564 }
565 
set_tuning(vpx_codec_alg_priv_t * ctx,va_list args)566 static vpx_codec_err_t set_tuning(vpx_codec_alg_priv_t *ctx, va_list args) {
567   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
568   extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
569   return update_extracfg(ctx, &extra_cfg);
570 }
571 
set_cq_level(vpx_codec_alg_priv_t * ctx,va_list args)572 static vpx_codec_err_t set_cq_level(vpx_codec_alg_priv_t *ctx, va_list args) {
573   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
574   extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
575   return update_extracfg(ctx, &extra_cfg);
576 }
577 
set_rc_max_intra_bitrate_pct(vpx_codec_alg_priv_t * ctx,va_list args)578 static vpx_codec_err_t set_rc_max_intra_bitrate_pct(vpx_codec_alg_priv_t *ctx,
579                                                     va_list args) {
580   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
581   extra_cfg.rc_max_intra_bitrate_pct =
582       CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
583   return update_extracfg(ctx, &extra_cfg);
584 }
585 
ctrl_set_rc_gf_cbr_boost_pct(vpx_codec_alg_priv_t * ctx,va_list args)586 static vpx_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(vpx_codec_alg_priv_t *ctx,
587                                                     va_list args) {
588   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
589   extra_cfg.gf_cbr_boost_pct = CAST(VP8E_SET_GF_CBR_BOOST_PCT, args);
590   return update_extracfg(ctx, &extra_cfg);
591 }
592 
set_screen_content_mode(vpx_codec_alg_priv_t * ctx,va_list args)593 static vpx_codec_err_t set_screen_content_mode(vpx_codec_alg_priv_t *ctx,
594                                                va_list args) {
595   struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
596   extra_cfg.screen_content_mode = CAST(VP8E_SET_SCREEN_CONTENT_MODE, args);
597   return update_extracfg(ctx, &extra_cfg);
598 }
599 
vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t * cfg,void ** mem_loc)600 static vpx_codec_err_t vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t *cfg,
601                                          void **mem_loc) {
602   vpx_codec_err_t res = VPX_CODEC_OK;
603 
604 #if CONFIG_MULTI_RES_ENCODING
605   LOWER_RES_FRAME_INFO *shared_mem_loc;
606   int mb_rows = ((cfg->g_w + 15) >> 4);
607   int mb_cols = ((cfg->g_h + 15) >> 4);
608 
609   shared_mem_loc = calloc(1, sizeof(LOWER_RES_FRAME_INFO));
610   if (!shared_mem_loc) {
611     return VPX_CODEC_MEM_ERROR;
612   }
613 
614   shared_mem_loc->mb_info =
615       calloc(mb_rows * mb_cols, sizeof(LOWER_RES_MB_INFO));
616   if (!(shared_mem_loc->mb_info)) {
617     free(shared_mem_loc);
618     res = VPX_CODEC_MEM_ERROR;
619   } else {
620     *mem_loc = (void *)shared_mem_loc;
621     res = VPX_CODEC_OK;
622   }
623 #else
624   (void)cfg;
625   (void)mem_loc;
626 #endif
627   return res;
628 }
629 
vp8e_init(vpx_codec_ctx_t * ctx,vpx_codec_priv_enc_mr_cfg_t * mr_cfg)630 static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx,
631                                  vpx_codec_priv_enc_mr_cfg_t *mr_cfg) {
632   vpx_codec_err_t res = VPX_CODEC_OK;
633 
634   vp8_rtcd();
635   vpx_dsp_rtcd();
636   vpx_scale_rtcd();
637 
638   if (!ctx->priv) {
639     struct vpx_codec_alg_priv *priv =
640         (struct vpx_codec_alg_priv *)vpx_calloc(1, sizeof(*priv));
641 
642     if (!priv) {
643       return VPX_CODEC_MEM_ERROR;
644     }
645 
646     ctx->priv = (vpx_codec_priv_t *)priv;
647     ctx->priv->init_flags = ctx->init_flags;
648 
649     if (ctx->config.enc) {
650       /* Update the reference to the config structure to an
651        * internal copy.
652        */
653       priv->cfg = *ctx->config.enc;
654       ctx->config.enc = &priv->cfg;
655     }
656 
657     priv->vp8_cfg = default_extracfg;
658     priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
659 
660     priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
661 
662     if (priv->cx_data_sz < 32768) priv->cx_data_sz = 32768;
663 
664     priv->cx_data = malloc(priv->cx_data_sz);
665 
666     if (!priv->cx_data) {
667       return VPX_CODEC_MEM_ERROR;
668     }
669 
670     if (mr_cfg) {
671       ctx->priv->enc.total_encoders = mr_cfg->mr_total_resolutions;
672     } else {
673       ctx->priv->enc.total_encoders = 1;
674     }
675 
676     once(vp8_initialize_enc);
677 
678     res = validate_config(priv, &priv->cfg, &priv->vp8_cfg, 0);
679 
680     if (!res) {
681       priv->pts_offset_initialized = 0;
682       priv->timestamp_ratio.den = priv->cfg.g_timebase.den;
683       priv->timestamp_ratio.num = (int64_t)priv->cfg.g_timebase.num;
684       priv->timestamp_ratio.num *= TICKS_PER_SEC;
685       reduce_ratio(&priv->timestamp_ratio);
686 
687       set_vp8e_config(&priv->oxcf, priv->cfg, priv->vp8_cfg, mr_cfg);
688       priv->cpi = vp8_create_compressor(&priv->oxcf);
689       if (!priv->cpi) res = VPX_CODEC_MEM_ERROR;
690     }
691   }
692 
693   return res;
694 }
695 
vp8e_destroy(vpx_codec_alg_priv_t * ctx)696 static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx) {
697 #if CONFIG_MULTI_RES_ENCODING
698   /* Free multi-encoder shared memory */
699   if (ctx->oxcf.mr_total_resolutions > 0 &&
700       (ctx->oxcf.mr_encoder_id == ctx->oxcf.mr_total_resolutions - 1)) {
701     LOWER_RES_FRAME_INFO *shared_mem_loc =
702         (LOWER_RES_FRAME_INFO *)ctx->oxcf.mr_low_res_mode_info;
703     free(shared_mem_loc->mb_info);
704     free(ctx->oxcf.mr_low_res_mode_info);
705   }
706 #endif
707 
708   free(ctx->cx_data);
709   vp8_remove_compressor(&ctx->cpi);
710   vpx_free(ctx);
711   return VPX_CODEC_OK;
712 }
713 
image2yuvconfig(const vpx_image_t * img,YV12_BUFFER_CONFIG * yv12)714 static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
715                                        YV12_BUFFER_CONFIG *yv12) {
716   const int y_w = img->d_w;
717   const int y_h = img->d_h;
718   const int uv_w = (img->d_w + 1) / 2;
719   const int uv_h = (img->d_h + 1) / 2;
720   vpx_codec_err_t res = VPX_CODEC_OK;
721   yv12->y_buffer = img->planes[VPX_PLANE_Y];
722   yv12->u_buffer = img->planes[VPX_PLANE_U];
723   yv12->v_buffer = img->planes[VPX_PLANE_V];
724 
725   yv12->y_crop_width = y_w;
726   yv12->y_crop_height = y_h;
727   yv12->y_width = y_w;
728   yv12->y_height = y_h;
729   yv12->uv_crop_width = uv_w;
730   yv12->uv_crop_height = uv_h;
731   yv12->uv_width = uv_w;
732   yv12->uv_height = uv_h;
733 
734   yv12->y_stride = img->stride[VPX_PLANE_Y];
735   yv12->uv_stride = img->stride[VPX_PLANE_U];
736 
737   yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2;
738   return res;
739 }
740 
pick_quickcompress_mode(vpx_codec_alg_priv_t * ctx,unsigned long duration,unsigned long deadline)741 static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
742                                     unsigned long duration,
743                                     unsigned long deadline) {
744   int new_qc;
745 
746 #if !(CONFIG_REALTIME_ONLY)
747   /* Use best quality mode if no deadline is given. */
748   new_qc = MODE_BESTQUALITY;
749 
750   if (deadline) {
751     /* Convert duration parameter from stream timebase to microseconds */
752     uint64_t duration_us;
753 
754     COMPILE_TIME_ASSERT(TICKS_PER_SEC > 1000000 &&
755                         (TICKS_PER_SEC % 1000000) == 0);
756 
757     duration_us = duration * (uint64_t)ctx->timestamp_ratio.num /
758                   (ctx->timestamp_ratio.den * (TICKS_PER_SEC / 1000000));
759 
760     /* If the deadline is more that the duration this frame is to be shown,
761      * use good quality mode. Otherwise use realtime mode.
762      */
763     new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
764   }
765 
766 #else
767   (void)duration;
768   new_qc = MODE_REALTIME;
769 #endif
770 
771   if (deadline == VPX_DL_REALTIME) {
772     new_qc = MODE_REALTIME;
773   } else if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS) {
774     new_qc = MODE_FIRSTPASS;
775   } else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS) {
776     new_qc =
777         (new_qc == MODE_BESTQUALITY) ? MODE_SECONDPASS_BEST : MODE_SECONDPASS;
778   }
779 
780   if (ctx->oxcf.Mode != new_qc) {
781     ctx->oxcf.Mode = new_qc;
782     vp8_change_config(ctx->cpi, &ctx->oxcf);
783   }
784 }
785 
set_reference_and_update(vpx_codec_alg_priv_t * ctx,vpx_enc_frame_flags_t flags)786 static vpx_codec_err_t set_reference_and_update(vpx_codec_alg_priv_t *ctx,
787                                                 vpx_enc_frame_flags_t flags) {
788   /* Handle Flags */
789   if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF)) ||
790       ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) {
791     ctx->base.err_detail = "Conflicting flags.";
792     return VPX_CODEC_INVALID_PARAM;
793   }
794 
795   if (flags &
796       (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF)) {
797     int ref = 7;
798 
799     if (flags & VP8_EFLAG_NO_REF_LAST) ref ^= VP8_LAST_FRAME;
800 
801     if (flags & VP8_EFLAG_NO_REF_GF) ref ^= VP8_GOLD_FRAME;
802 
803     if (flags & VP8_EFLAG_NO_REF_ARF) ref ^= VP8_ALTR_FRAME;
804 
805     vp8_use_as_reference(ctx->cpi, ref);
806   }
807 
808   if (flags &
809       (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
810        VP8_EFLAG_FORCE_GF | VP8_EFLAG_FORCE_ARF)) {
811     int upd = 7;
812 
813     if (flags & VP8_EFLAG_NO_UPD_LAST) upd ^= VP8_LAST_FRAME;
814 
815     if (flags & VP8_EFLAG_NO_UPD_GF) upd ^= VP8_GOLD_FRAME;
816 
817     if (flags & VP8_EFLAG_NO_UPD_ARF) upd ^= VP8_ALTR_FRAME;
818 
819     vp8_update_reference(ctx->cpi, upd);
820   }
821 
822   if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
823     vp8_update_entropy(ctx->cpi, 0);
824   }
825 
826   return VPX_CODEC_OK;
827 }
828 
vp8e_encode(vpx_codec_alg_priv_t * ctx,const vpx_image_t * img,vpx_codec_pts_t pts,unsigned long duration,vpx_enc_frame_flags_t enc_flags,unsigned long deadline)829 static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx,
830                                    const vpx_image_t *img, vpx_codec_pts_t pts,
831                                    unsigned long duration,
832                                    vpx_enc_frame_flags_t enc_flags,
833                                    unsigned long deadline) {
834   volatile vpx_codec_err_t res = VPX_CODEC_OK;
835   // Make a copy as volatile to avoid -Wclobbered with longjmp.
836   volatile vpx_enc_frame_flags_t flags = enc_flags;
837   volatile vpx_codec_pts_t pts_val = pts;
838 
839   if (!ctx->cfg.rc_target_bitrate) {
840 #if CONFIG_MULTI_RES_ENCODING
841     if (!ctx->cpi) return VPX_CODEC_ERROR;
842     if (ctx->cpi->oxcf.mr_total_resolutions > 1) {
843       LOWER_RES_FRAME_INFO *low_res_frame_info =
844           (LOWER_RES_FRAME_INFO *)ctx->cpi->oxcf.mr_low_res_mode_info;
845       if (!low_res_frame_info) return VPX_CODEC_ERROR;
846       low_res_frame_info->skip_encoding_prev_stream = 1;
847       if (ctx->cpi->oxcf.mr_encoder_id == 0)
848         low_res_frame_info->skip_encoding_base_stream = 1;
849     }
850 #endif
851     return res;
852   }
853 
854   if (img) res = validate_img(ctx, img);
855 
856   if (!res) res = validate_config(ctx, &ctx->cfg, &ctx->vp8_cfg, 1);
857 
858   if (!ctx->pts_offset_initialized) {
859     ctx->pts_offset = pts_val;
860     ctx->pts_offset_initialized = 1;
861   }
862   pts_val -= ctx->pts_offset;
863 
864   pick_quickcompress_mode(ctx, duration, deadline);
865   vpx_codec_pkt_list_init(&ctx->pkt_list);
866 
867   // If no flags are set in the encode call, then use the frame flags as
868   // defined via the control function: vp8e_set_frame_flags.
869   if (!flags) {
870     flags = ctx->control_frame_flags;
871   }
872   ctx->control_frame_flags = 0;
873 
874   if (!res) res = set_reference_and_update(ctx, flags);
875 
876   /* Handle fixed keyframe intervals */
877   if (ctx->cfg.kf_mode == VPX_KF_AUTO &&
878       ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
879     if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
880       flags |= VPX_EFLAG_FORCE_KF;
881       ctx->fixed_kf_cntr = 1;
882     }
883   }
884 
885   if (setjmp(ctx->cpi->common.error.jmp)) {
886     ctx->cpi->common.error.setjmp = 0;
887     vpx_clear_system_state();
888     return VPX_CODEC_CORRUPT_FRAME;
889   }
890 
891   /* Initialize the encoder instance on the first frame*/
892   if (!res && ctx->cpi) {
893     unsigned int lib_flags;
894     YV12_BUFFER_CONFIG sd;
895     int64_t dst_time_stamp, dst_end_time_stamp;
896     size_t size, cx_data_sz;
897     unsigned char *cx_data;
898     unsigned char *cx_data_end;
899     int comp_data_state = 0;
900 
901     /* Set up internal flags */
902     if (ctx->base.init_flags & VPX_CODEC_USE_PSNR) {
903       ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1;
904     }
905 
906     if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION) {
907       ((VP8_COMP *)ctx->cpi)->output_partition = 1;
908     }
909 
910     /* Convert API flags to internal codec lib flags */
911     lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
912 
913     dst_time_stamp =
914         pts_val * ctx->timestamp_ratio.num / ctx->timestamp_ratio.den;
915     dst_end_time_stamp = (pts_val + (int64_t)duration) *
916                          ctx->timestamp_ratio.num / ctx->timestamp_ratio.den;
917 
918     if (img != NULL) {
919       res = image2yuvconfig(img, &sd);
920 
921       if (sd.y_width != ctx->cfg.g_w || sd.y_height != ctx->cfg.g_h) {
922         /* from vpx_encoder.h for g_w/g_h:
923            "Note that the frames passed as input to the encoder must have this
924            resolution"
925         */
926         ctx->base.err_detail = "Invalid input frame resolution";
927         res = VPX_CODEC_INVALID_PARAM;
928       } else {
929         if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
930                                   &sd, dst_time_stamp, dst_end_time_stamp)) {
931           VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
932           res = update_error_state(ctx, &cpi->common.error);
933         }
934       }
935 
936       /* reset for next frame */
937       ctx->next_frame_flag = 0;
938     }
939 
940     cx_data = ctx->cx_data;
941     cx_data_sz = ctx->cx_data_sz;
942     cx_data_end = ctx->cx_data + cx_data_sz;
943     lib_flags = 0;
944 
945     ctx->cpi->common.error.setjmp = 1;
946 
947     while (cx_data_sz >= ctx->cx_data_sz / 2) {
948       comp_data_state = vp8_get_compressed_data(
949           ctx->cpi, &lib_flags, &size, cx_data, cx_data_end, &dst_time_stamp,
950           &dst_end_time_stamp, !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       }
957 
958       if (size) {
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. */
964         round = (vpx_codec_pts_t)ctx->timestamp_ratio.num / 2;
965         if (round > 0) --round;
966         delta = (dst_end_time_stamp - dst_time_stamp);
967         pkt.kind = VPX_CODEC_CX_FRAME_PKT;
968         pkt.data.frame.pts =
969             (dst_time_stamp * ctx->timestamp_ratio.den + round) /
970                 ctx->timestamp_ratio.num +
971             ctx->pts_offset;
972         pkt.data.frame.duration =
973             (unsigned long)((delta * ctx->timestamp_ratio.den + round) /
974                             ctx->timestamp_ratio.num);
975         pkt.data.frame.flags = lib_flags << 16;
976         pkt.data.frame.width[0] = cpi->common.Width;
977         pkt.data.frame.height[0] = cpi->common.Height;
978         pkt.data.frame.spatial_layer_encoded[0] = 1;
979 
980         if (lib_flags & FRAMEFLAGS_KEY) {
981           pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
982         }
983 
984         if (!cpi->common.show_frame) {
985           pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
986 
987           /* This timestamp should be as close as possible to the
988            * prior PTS so that if a decoder uses pts to schedule when
989            * to do this, we start right after last frame was decoded.
990            * Invisible frames have no duration.
991            */
992           pkt.data.frame.pts =
993               ((cpi->last_time_stamp_seen * ctx->timestamp_ratio.den + round) /
994                ctx->timestamp_ratio.num) +
995               ctx->pts_offset + 1;
996           pkt.data.frame.duration = 0;
997         }
998 
999         if (cpi->droppable) pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE;
1000 
1001         if (cpi->output_partition) {
1002           int i;
1003           const int num_partitions =
1004               (1 << cpi->common.multi_token_partition) + 1;
1005 
1006           pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT;
1007 
1008           for (i = 0; i < num_partitions; ++i) {
1009 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1010             pkt.data.frame.buf = cpi->partition_d[i];
1011 #else
1012             pkt.data.frame.buf = cx_data;
1013             cx_data += cpi->partition_sz[i];
1014             cx_data_sz -= cpi->partition_sz[i];
1015 #endif
1016             pkt.data.frame.sz = cpi->partition_sz[i];
1017             pkt.data.frame.partition_id = i;
1018             /* don't set the fragment bit for the last partition */
1019             if (i == (num_partitions - 1)) {
1020               pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT;
1021             }
1022             vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1023           }
1024 #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
1032         } else {
1033           pkt.data.frame.buf = cx_data;
1034           pkt.data.frame.sz = size;
1035           pkt.data.frame.partition_id = -1;
1036           vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1037           cx_data += size;
1038           cx_data_sz -= size;
1039         }
1040       }
1041     }
1042   }
1043 
1044   return res;
1045 }
1046 
vp8e_get_cxdata(vpx_codec_alg_priv_t * ctx,vpx_codec_iter_t * iter)1047 static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx,
1048                                                  vpx_codec_iter_t *iter) {
1049   return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1050 }
1051 
vp8e_set_reference(vpx_codec_alg_priv_t * ctx,va_list args)1052 static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx,
1053                                           va_list args) {
1054   vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1055 
1056   if (data) {
1057     vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1058     YV12_BUFFER_CONFIG sd;
1059 
1060     image2yuvconfig(&frame->img, &sd);
1061     vp8_set_reference(ctx->cpi, frame->frame_type, &sd);
1062     return VPX_CODEC_OK;
1063   } else {
1064     return VPX_CODEC_INVALID_PARAM;
1065   }
1066 }
1067 
vp8e_get_reference(vpx_codec_alg_priv_t * ctx,va_list args)1068 static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx,
1069                                           va_list args) {
1070   vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1071 
1072   if (data) {
1073     vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1074     YV12_BUFFER_CONFIG sd;
1075 
1076     image2yuvconfig(&frame->img, &sd);
1077     vp8_get_reference(ctx->cpi, frame->frame_type, &sd);
1078     return VPX_CODEC_OK;
1079   } else {
1080     return VPX_CODEC_INVALID_PARAM;
1081   }
1082 }
1083 
vp8e_set_previewpp(vpx_codec_alg_priv_t * ctx,va_list args)1084 static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx,
1085                                           va_list args) {
1086 #if CONFIG_POSTPROC
1087   vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
1088 
1089   if (data) {
1090     ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
1091     return VPX_CODEC_OK;
1092   } else {
1093     return VPX_CODEC_INVALID_PARAM;
1094   }
1095 #else
1096   (void)ctx;
1097   (void)args;
1098   return VPX_CODEC_INCAPABLE;
1099 #endif
1100 }
1101 
vp8e_get_preview(vpx_codec_alg_priv_t * ctx)1102 static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx) {
1103   YV12_BUFFER_CONFIG sd;
1104   vp8_ppflags_t flags;
1105   vp8_zero(flags);
1106 
1107   if (ctx->preview_ppcfg.post_proc_flag) {
1108     flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
1109     flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
1110     flags.noise_level = ctx->preview_ppcfg.noise_level;
1111   }
1112 
1113   if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags)) {
1114     /*
1115     vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
1116         sd.y_width + 2*VP8BORDERINPIXELS,
1117         sd.y_height + 2*VP8BORDERINPIXELS,
1118         1,
1119         sd.buffer_alloc);
1120     vpx_img_set_rect(&ctx->preview_img,
1121         VP8BORDERINPIXELS, VP8BORDERINPIXELS,
1122         sd.y_width, sd.y_height);
1123         */
1124 
1125     ctx->preview_img.bps = 12;
1126     ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer;
1127     ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer;
1128     ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer;
1129 
1130     ctx->preview_img.fmt = VPX_IMG_FMT_I420;
1131     ctx->preview_img.x_chroma_shift = 1;
1132     ctx->preview_img.y_chroma_shift = 1;
1133 
1134     ctx->preview_img.d_w = sd.y_width;
1135     ctx->preview_img.d_h = sd.y_height;
1136     ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride;
1137     ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride;
1138     ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride;
1139     ctx->preview_img.w = sd.y_width;
1140     ctx->preview_img.h = sd.y_height;
1141 
1142     return &ctx->preview_img;
1143   } else {
1144     return NULL;
1145   }
1146 }
1147 
vp8e_set_frame_flags(vpx_codec_alg_priv_t * ctx,va_list args)1148 static vpx_codec_err_t vp8e_set_frame_flags(vpx_codec_alg_priv_t *ctx,
1149                                             va_list args) {
1150   int frame_flags = va_arg(args, int);
1151   ctx->control_frame_flags = frame_flags;
1152   return set_reference_and_update(ctx, frame_flags);
1153 }
1154 
vp8e_set_temporal_layer_id(vpx_codec_alg_priv_t * ctx,va_list args)1155 static vpx_codec_err_t vp8e_set_temporal_layer_id(vpx_codec_alg_priv_t *ctx,
1156                                                   va_list args) {
1157   int layer_id = va_arg(args, int);
1158   if (layer_id < 0 || layer_id >= (int)ctx->cfg.ts_number_layers) {
1159     return VPX_CODEC_INVALID_PARAM;
1160   }
1161   ctx->cpi->temporal_layer_id = layer_id;
1162   return VPX_CODEC_OK;
1163 }
1164 
vp8e_set_roi_map(vpx_codec_alg_priv_t * ctx,va_list args)1165 static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx,
1166                                         va_list args) {
1167   vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
1168 
1169   if (data) {
1170     vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
1171 
1172     if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols,
1173                         roi->delta_q, roi->delta_lf, roi->static_threshold)) {
1174       return VPX_CODEC_OK;
1175     } else {
1176       return VPX_CODEC_INVALID_PARAM;
1177     }
1178   } else {
1179     return VPX_CODEC_INVALID_PARAM;
1180   }
1181 }
1182 
vp8e_set_activemap(vpx_codec_alg_priv_t * ctx,va_list args)1183 static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx,
1184                                           va_list args) {
1185   vpx_active_map_t *data = va_arg(args, vpx_active_map_t *);
1186 
1187   if (data) {
1188     vpx_active_map_t *map = (vpx_active_map_t *)data;
1189 
1190     if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols)) {
1191       return VPX_CODEC_OK;
1192     } else {
1193       return VPX_CODEC_INVALID_PARAM;
1194     }
1195   } else {
1196     return VPX_CODEC_INVALID_PARAM;
1197   }
1198 }
1199 
vp8e_set_scalemode(vpx_codec_alg_priv_t * ctx,va_list args)1200 static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx,
1201                                           va_list args) {
1202   vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *);
1203 
1204   if (data) {
1205     int res;
1206     vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data;
1207     res = vp8_set_internal_size(ctx->cpi, (VPX_SCALING)scalemode.h_scaling_mode,
1208                                 (VPX_SCALING)scalemode.v_scaling_mode);
1209 
1210     if (!res) {
1211       /*force next frame a key frame to effect scaling mode */
1212       ctx->next_frame_flag |= FRAMEFLAGS_KEY;
1213       return VPX_CODEC_OK;
1214     } else {
1215       return VPX_CODEC_INVALID_PARAM;
1216     }
1217   } else {
1218     return VPX_CODEC_INVALID_PARAM;
1219   }
1220 }
1221 
1222 static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] = {
1223   { VP8_SET_REFERENCE, vp8e_set_reference },
1224   { VP8_COPY_REFERENCE, vp8e_get_reference },
1225   { VP8_SET_POSTPROC, vp8e_set_previewpp },
1226   { VP8E_SET_FRAME_FLAGS, vp8e_set_frame_flags },
1227   { VP8E_SET_TEMPORAL_LAYER_ID, vp8e_set_temporal_layer_id },
1228   { VP8E_SET_ROI_MAP, vp8e_set_roi_map },
1229   { VP8E_SET_ACTIVEMAP, vp8e_set_activemap },
1230   { VP8E_SET_SCALEMODE, vp8e_set_scalemode },
1231   { VP8E_SET_CPUUSED, set_cpu_used },
1232   { VP8E_SET_NOISE_SENSITIVITY, set_noise_sensitivity },
1233   { VP8E_SET_ENABLEAUTOALTREF, set_enable_auto_alt_ref },
1234   { VP8E_SET_SHARPNESS, set_sharpness },
1235   { VP8E_SET_STATIC_THRESHOLD, set_static_thresh },
1236   { VP8E_SET_TOKEN_PARTITIONS, set_token_partitions },
1237   { VP8E_GET_LAST_QUANTIZER, get_quantizer },
1238   { VP8E_GET_LAST_QUANTIZER_64, get_quantizer64 },
1239   { VP8E_SET_ARNR_MAXFRAMES, set_arnr_max_frames },
1240   { VP8E_SET_ARNR_STRENGTH, set_arnr_strength },
1241   { VP8E_SET_ARNR_TYPE, set_arnr_type },
1242   { VP8E_SET_TUNING, set_tuning },
1243   { VP8E_SET_CQ_LEVEL, set_cq_level },
1244   { VP8E_SET_MAX_INTRA_BITRATE_PCT, set_rc_max_intra_bitrate_pct },
1245   { VP8E_SET_SCREEN_CONTENT_MODE, set_screen_content_mode },
1246   { VP8E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
1247   { -1, NULL },
1248 };
1249 
1250 static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] = {
1251   { 0,
1252     {
1253         0, /* g_usage (unused) */
1254         0, /* g_threads */
1255         0, /* g_profile */
1256 
1257         320,        /* g_width */
1258         240,        /* g_height */
1259         VPX_BITS_8, /* g_bit_depth */
1260         8,          /* g_input_bit_depth */
1261 
1262         { 1, 30 }, /* g_timebase */
1263 
1264         0, /* g_error_resilient */
1265 
1266         VPX_RC_ONE_PASS, /* g_pass */
1267 
1268         0, /* g_lag_in_frames */
1269 
1270         0,  /* rc_dropframe_thresh */
1271         0,  /* rc_resize_allowed */
1272         1,  /* rc_scaled_width */
1273         1,  /* rc_scaled_height */
1274         60, /* rc_resize_down_thresold */
1275         30, /* rc_resize_up_thresold */
1276 
1277         VPX_VBR,     /* rc_end_usage */
1278         { NULL, 0 }, /* rc_twopass_stats_in */
1279         { NULL, 0 }, /* rc_firstpass_mb_stats_in */
1280         256,         /* rc_target_bandwidth */
1281         4,           /* rc_min_quantizer */
1282         63,          /* rc_max_quantizer */
1283         100,         /* rc_undershoot_pct */
1284         100,         /* rc_overshoot_pct */
1285 
1286         6000, /* rc_max_buffer_size */
1287         4000, /* rc_buffer_initial_size; */
1288         5000, /* rc_buffer_optimal_size; */
1289 
1290         50,  /* rc_two_pass_vbrbias  */
1291         0,   /* rc_two_pass_vbrmin_section */
1292         400, /* rc_two_pass_vbrmax_section */
1293         0,   // rc_2pass_vbr_corpus_complexity (only has meaningfull for VP9)
1294 
1295         /* keyframing settings (kf) */
1296         VPX_KF_AUTO, /* g_kfmode*/
1297         0,           /* kf_min_dist */
1298         128,         /* kf_max_dist */
1299 
1300         VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */
1301         { 0 },
1302         { 0 }, /* ss_target_bitrate */
1303         1,     /* ts_number_layers */
1304         { 0 }, /* ts_target_bitrate */
1305         { 0 }, /* ts_rate_decimator */
1306         0,     /* ts_periodicity */
1307         { 0 }, /* ts_layer_id */
1308         { 0 }, /* layer_target_bitrate */
1309         0      /* temporal_layering_mode */
1310     } },
1311 };
1312 
1313 #ifndef VERSION_STRING
1314 #define VERSION_STRING
1315 #endif
1316 CODEC_INTERFACE(vpx_codec_vp8_cx) = {
1317   "WebM Project VP8 Encoder" VERSION_STRING,
1318   VPX_CODEC_INTERNAL_ABI_VERSION,
1319   VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR | VPX_CODEC_CAP_OUTPUT_PARTITION,
1320   /* vpx_codec_caps_t          caps; */
1321   vp8e_init,     /* vpx_codec_init_fn_t       init; */
1322   vp8e_destroy,  /* vpx_codec_destroy_fn_t    destroy; */
1323   vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t  *ctrl_maps; */
1324   {
1325       NULL, /* vpx_codec_peek_si_fn_t    peek_si; */
1326       NULL, /* vpx_codec_get_si_fn_t     get_si; */
1327       NULL, /* vpx_codec_decode_fn_t     decode; */
1328       NULL, /* vpx_codec_frame_get_fn_t  frame_get; */
1329       NULL, /* vpx_codec_set_fb_fn_t     set_fb_fn; */
1330   },
1331   {
1332       1,                  /* 1 cfg map */
1333       vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t    cfg_maps; */
1334       vp8e_encode,        /* vpx_codec_encode_fn_t      encode; */
1335       vp8e_get_cxdata,    /* vpx_codec_get_cx_data_fn_t   get_cx_data; */
1336       vp8e_set_config,
1337       NULL,
1338       vp8e_get_preview,
1339       vp8e_mr_alloc_mem,
1340   } /* encoder functions */
1341 };
1342