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