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