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