Merge "Copy macroblock data to a buffer before encoding it"
[profile/ivi/libvpx.git] / vp8 / vp8_cx_iface.c
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/vpx_codec.h"
13 #include "vpx/internal/vpx_codec_internal.h"
14 #include "vpx_version.h"
15 #include "vp8/encoder/onyx_int.h"
16 #include "vpx/vp8e.h"
17 #include "vp8/encoder/firstpass.h"
18 #include "vp8/common/onyx.h"
19 #include <stdlib.h>
20 #include <string.h>
21
22 /* This value is a sentinel for determining whether the user has set a mode
23  * directly through the deprecated VP8E_SET_ENCODING_MODE control.
24  */
25 #define NO_MODE_SET 255
26
27 struct vp8_extracfg
28 {
29     struct vpx_codec_pkt_list *pkt_list;
30     vp8e_encoding_mode      encoding_mode;               /** best, good, realtime            */
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
43 };
44
45 struct extraconfig_map
46 {
47     int                 usage;
48     struct vp8_extracfg cfg;
49 };
50
51 static const struct extraconfig_map extracfg_map[] =
52 {
53     {
54         0,
55         {
56             NULL,
57 #if !(CONFIG_REALTIME_ONLY)
58             VP8_BEST_QUALITY_ENCODING,  /* Encoding Mode */
59             0,                          /* cpu_used      */
60 #else
61             VP8_REAL_TIME_ENCODING,     /* Encoding Mode */
62             4,                          /* cpu_used      */
63 #endif
64             0,                          /* enable_auto_alt_ref */
65             0,                          /* noise_sensitivity */
66             0,                          /* Sharpness */
67             0,                          /* static_thresh */
68             VP8_ONE_TOKENPARTITION,     /* token_partitions */
69             0,                          /* arnr_max_frames */
70             3,                          /* arnr_strength */
71             3,                          /* arnr_type*/
72             0,                          /* tuning*/
73             10,                         /* cq_level */
74         }
75     }
76 };
77
78 struct vpx_codec_alg_priv
79 {
80     vpx_codec_priv_t        base;
81     vpx_codec_enc_cfg_t     cfg;
82     struct vp8_extracfg     vp8_cfg;
83     VP8_CONFIG              oxcf;
84     VP8_PTR             cpi;
85     unsigned char          *cx_data;
86     unsigned int            cx_data_sz;
87     vpx_image_t             preview_img;
88     unsigned int            next_frame_flag;
89     vp8_postproc_cfg_t      preview_ppcfg;
90     vpx_codec_pkt_list_decl(64) pkt_list;              // changed to accomendate the maximum number of lagged frames allowed
91     int                         deprecated_mode;
92     unsigned int                fixed_kf_cntr;
93 };
94
95
96 static vpx_codec_err_t
97 update_error_state(vpx_codec_alg_priv_t                 *ctx,
98                    const struct vpx_internal_error_info *error)
99 {
100     vpx_codec_err_t res;
101
102     if ((res = error->error_code))
103         ctx->base.err_detail = error->has_detail
104                                ? error->detail
105                                : NULL;
106
107     return res;
108 }
109
110
111 #undef ERROR
112 #define ERROR(str) do {\
113         ctx->base.err_detail = str;\
114         return VPX_CODEC_INVALID_PARAM;\
115     } while(0)
116
117 #define RANGE_CHECK(p,memb,lo,hi) do {\
118         if(!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
119             ERROR(#memb " out of range ["#lo".."#hi"]");\
120     } while(0)
121
122 #define RANGE_CHECK_HI(p,memb,hi) do {\
123         if(!((p)->memb <= (hi))) \
124             ERROR(#memb " out of range [.."#hi"]");\
125     } while(0)
126
127 #define RANGE_CHECK_LO(p,memb,lo) do {\
128         if(!((p)->memb >= (lo))) \
129             ERROR(#memb " out of range ["#lo"..]");\
130     } while(0)
131
132 #define RANGE_CHECK_BOOL(p,memb) do {\
133         if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
134     } while(0)
135
136 static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t      *ctx,
137                                        const vpx_codec_enc_cfg_t *cfg,
138                                        const struct vp8_extracfg *vp8_cfg)
139 {
140     RANGE_CHECK(cfg, g_w,                   1, 16383); /* 14 bits available */
141     RANGE_CHECK(cfg, g_h,                   1, 16383); /* 14 bits available */
142     RANGE_CHECK(cfg, g_timebase.den,        1, 1000000000);
143     RANGE_CHECK(cfg, g_timebase.num,        1, cfg->g_timebase.den);
144     RANGE_CHECK_HI(cfg, g_profile,          3);
145     RANGE_CHECK_HI(cfg, rc_max_quantizer,   63);
146     RANGE_CHECK_HI(cfg, rc_min_quantizer,   cfg->rc_max_quantizer);
147     RANGE_CHECK_HI(cfg, g_threads,          64);
148 #if !(CONFIG_REALTIME_ONLY)
149     RANGE_CHECK_HI(cfg, g_lag_in_frames,    25);
150 #else
151     RANGE_CHECK_HI(cfg, g_lag_in_frames,    0);
152 #endif
153     RANGE_CHECK(cfg, rc_end_usage,          VPX_VBR, VPX_CQ);
154     RANGE_CHECK_HI(cfg, rc_undershoot_pct,  1000);
155     RANGE_CHECK_HI(cfg, rc_overshoot_pct,   1000);
156     RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
157     RANGE_CHECK(cfg, kf_mode,               VPX_KF_DISABLED, VPX_KF_AUTO);
158     //RANGE_CHECK_BOOL(cfg,                 g_delete_firstpassfile);
159     RANGE_CHECK_BOOL(cfg,                   rc_resize_allowed);
160     RANGE_CHECK_HI(cfg, rc_dropframe_thresh,   100);
161     RANGE_CHECK_HI(cfg, rc_resize_up_thresh,   100);
162     RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
163 #if !(CONFIG_REALTIME_ONLY)
164     RANGE_CHECK(cfg,        g_pass,         VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
165 #else
166     RANGE_CHECK(cfg,        g_pass,         VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
167 #endif
168
169     /* VP8 does not support a lower bound on the keyframe interval in
170      * automatic keyframe placement mode.
171      */
172     if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist
173         && cfg->kf_min_dist > 0)
174         ERROR("kf_min_dist not supported in auto mode, use 0 "
175               "or kf_max_dist instead.");
176
177     RANGE_CHECK_BOOL(vp8_cfg,               enable_auto_alt_ref);
178     RANGE_CHECK(vp8_cfg, cpu_used,           -16, 16);
179
180 #if !(CONFIG_REALTIME_ONLY)
181     RANGE_CHECK(vp8_cfg, encoding_mode,      VP8_BEST_QUALITY_ENCODING, VP8_REAL_TIME_ENCODING);
182     RANGE_CHECK_HI(vp8_cfg, noise_sensitivity,  6);
183 #else
184     RANGE_CHECK(vp8_cfg, encoding_mode,      VP8_REAL_TIME_ENCODING, VP8_REAL_TIME_ENCODING);
185     RANGE_CHECK(vp8_cfg, noise_sensitivity,  0, 0);
186 #endif
187
188     RANGE_CHECK(vp8_cfg, token_partitions,   VP8_ONE_TOKENPARTITION, VP8_EIGHT_TOKENPARTITION);
189     RANGE_CHECK_HI(vp8_cfg, Sharpness,       7);
190     RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
191     RANGE_CHECK_HI(vp8_cfg, arnr_strength,   6);
192     RANGE_CHECK(vp8_cfg, arnr_type,       1, 3);
193     RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
194
195 #if !(CONFIG_REALTIME_ONLY)
196     if (cfg->g_pass == VPX_RC_LAST_PASS)
197     {
198         size_t           packet_sz = sizeof(FIRSTPASS_STATS);
199         int              n_packets = cfg->rc_twopass_stats_in.sz / packet_sz;
200         FIRSTPASS_STATS *stats;
201
202         if (!cfg->rc_twopass_stats_in.buf)
203             ERROR("rc_twopass_stats_in.buf not set.");
204
205         if (cfg->rc_twopass_stats_in.sz % packet_sz)
206             ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
207
208         if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
209             ERROR("rc_twopass_stats_in requires at least two packets.");
210
211         stats = (void*)((char *)cfg->rc_twopass_stats_in.buf
212                 + (n_packets - 1) * packet_sz);
213
214         if ((int)(stats->count + 0.5) != n_packets - 1)
215             ERROR("rc_twopass_stats_in missing EOS stats packet");
216     }
217 #endif
218
219     return VPX_CODEC_OK;
220 }
221
222
223 static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
224                                     const vpx_image_t    *img)
225 {
226     switch (img->fmt)
227     {
228     case VPX_IMG_FMT_YV12:
229     case VPX_IMG_FMT_I420:
230     case VPX_IMG_FMT_VPXI420:
231     case VPX_IMG_FMT_VPXYV12:
232         break;
233     default:
234         ERROR("Invalid image format. Only YV12 and I420 images are supported");
235     }
236
237     if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
238         ERROR("Image size must match encoder init configuration size");
239
240     return VPX_CODEC_OK;
241 }
242
243
244 static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf,
245                                        vpx_codec_enc_cfg_t cfg,
246                                        struct vp8_extracfg vp8_cfg)
247 {
248     oxcf->multi_threaded         = cfg.g_threads;
249     oxcf->Version               = cfg.g_profile;
250
251     oxcf->Width                 = cfg.g_w;
252     oxcf->Height                = cfg.g_h;
253     /* guess a frame rate if out of whack, use 30 */
254     oxcf->frame_rate             = (double)(cfg.g_timebase.den) / (double)(cfg.g_timebase.num);
255
256     if (oxcf->frame_rate > 180)
257     {
258         oxcf->frame_rate = 30;
259     }
260
261     oxcf->error_resilient_mode    = cfg.g_error_resilient;
262
263     switch (cfg.g_pass)
264     {
265     case VPX_RC_ONE_PASS:
266         oxcf->Mode = MODE_BESTQUALITY;
267         break;
268     case VPX_RC_FIRST_PASS:
269         oxcf->Mode = MODE_FIRSTPASS;
270         break;
271     case VPX_RC_LAST_PASS:
272         oxcf->Mode = MODE_SECONDPASS_BEST;
273         break;
274     }
275
276     if (cfg.g_pass == VPX_RC_FIRST_PASS)
277     {
278         oxcf->allow_lag              = 0;
279         oxcf->lag_in_frames           = 0;
280     }
281     else
282     {
283         oxcf->allow_lag              = (cfg.g_lag_in_frames) > 0;
284         oxcf->lag_in_frames           = cfg.g_lag_in_frames;
285     }
286
287     oxcf->allow_df               = (cfg.rc_dropframe_thresh > 0);
288     oxcf->drop_frames_water_mark   = cfg.rc_dropframe_thresh;
289
290     oxcf->allow_spatial_resampling = cfg.rc_resize_allowed;
291     oxcf->resample_up_water_mark   = cfg.rc_resize_up_thresh;
292     oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh;
293
294     if (cfg.rc_end_usage == VPX_VBR)
295     {
296         oxcf->end_usage          = USAGE_LOCAL_FILE_PLAYBACK;
297     }
298     else if (cfg.rc_end_usage == VPX_CBR)
299     {
300         oxcf->end_usage          = USAGE_STREAM_FROM_SERVER;
301     }
302     else if (cfg.rc_end_usage == VPX_CQ)
303     {
304         oxcf->end_usage          = USAGE_CONSTRAINED_QUALITY;
305     }
306
307     oxcf->target_bandwidth       = cfg.rc_target_bitrate;
308     oxcf->rc_max_intra_bitrate_pct = cfg.rc_max_intra_bitrate_pct;
309
310     oxcf->best_allowed_q          = cfg.rc_min_quantizer;
311     oxcf->worst_allowed_q         = cfg.rc_max_quantizer;
312     oxcf->cq_level                = vp8_cfg.cq_level;
313     oxcf->fixed_q = -1;
314
315     oxcf->under_shoot_pct         = cfg.rc_undershoot_pct;
316     oxcf->over_shoot_pct          = cfg.rc_overshoot_pct;
317
318     oxcf->maximum_buffer_size     = cfg.rc_buf_sz;
319     oxcf->starting_buffer_level   = cfg.rc_buf_initial_sz;
320     oxcf->optimal_buffer_level    = cfg.rc_buf_optimal_sz;
321
322     oxcf->two_pass_vbrbias        = cfg.rc_2pass_vbr_bias_pct;
323     oxcf->two_pass_vbrmin_section  = cfg.rc_2pass_vbr_minsection_pct;
324     oxcf->two_pass_vbrmax_section  = cfg.rc_2pass_vbr_maxsection_pct;
325
326     oxcf->auto_key               = cfg.kf_mode == VPX_KF_AUTO
327                                    && cfg.kf_min_dist != cfg.kf_max_dist;
328     //oxcf->kf_min_dist         = cfg.kf_min_dis;
329     oxcf->key_freq               = cfg.kf_max_dist;
330
331     //oxcf->delete_first_pass_file = cfg.g_delete_firstpassfile;
332     //strcpy(oxcf->first_pass_file, cfg.g_firstpass_file);
333
334     oxcf->cpu_used               =  vp8_cfg.cpu_used;
335     oxcf->encode_breakout        =  vp8_cfg.static_thresh;
336     oxcf->play_alternate         =  vp8_cfg.enable_auto_alt_ref;
337     oxcf->noise_sensitivity      =  vp8_cfg.noise_sensitivity;
338     oxcf->Sharpness             =  vp8_cfg.Sharpness;
339     oxcf->token_partitions       =  vp8_cfg.token_partitions;
340
341     oxcf->two_pass_stats_in        =  cfg.rc_twopass_stats_in;
342     oxcf->output_pkt_list         =  vp8_cfg.pkt_list;
343
344     oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
345     oxcf->arnr_strength =  vp8_cfg.arnr_strength;
346     oxcf->arnr_type =      vp8_cfg.arnr_type;
347
348     oxcf->tuning = vp8_cfg.tuning;
349
350     /*
351         printf("Current VP8 Settings: \n");
352         printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
353         printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
354         printf("Sharpness: %d\n",    oxcf->Sharpness);
355         printf("cpu_used: %d\n",  oxcf->cpu_used);
356         printf("Mode: %d\n",     oxcf->Mode);
357         printf("delete_first_pass_file: %d\n",  oxcf->delete_first_pass_file);
358         printf("auto_key: %d\n",  oxcf->auto_key);
359         printf("key_freq: %d\n", oxcf->key_freq);
360         printf("end_usage: %d\n", oxcf->end_usage);
361         printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
362         printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
363         printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
364         printf("optimal_buffer_level: %d\n",  oxcf->optimal_buffer_level);
365         printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
366         printf("fixed_q: %d\n",  oxcf->fixed_q);
367         printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
368         printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
369         printf("allow_spatial_resampling: %d\n",  oxcf->allow_spatial_resampling);
370         printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
371         printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
372         printf("allow_df: %d\n", oxcf->allow_df);
373         printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
374         printf("two_pass_vbrbias: %d\n",  oxcf->two_pass_vbrbias);
375         printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
376         printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
377         printf("allow_lag: %d\n", oxcf->allow_lag);
378         printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
379         printf("play_alternate: %d\n", oxcf->play_alternate);
380         printf("Version: %d\n", oxcf->Version);
381         printf("multi_threaded: %d\n",   oxcf->multi_threaded);
382         printf("encode_breakout: %d\n", oxcf->encode_breakout);
383     */
384     return VPX_CODEC_OK;
385 }
386
387 static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t       *ctx,
388                                        const vpx_codec_enc_cfg_t  *cfg)
389 {
390     vpx_codec_err_t res;
391
392     if ((cfg->g_w != ctx->cfg.g_w) || (cfg->g_h != ctx->cfg.g_h))
393         ERROR("Cannot change width or height after initialization");
394
395     /* Prevent increasing lag_in_frames. This check is stricter than it needs
396      * to be -- the limit is not increasing past the first lag_in_frames
397      * value, but we don't track the initial config, only the last successful
398      * config.
399      */
400     if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
401         ERROR("Cannot increase lag_in_frames");
402
403     res = validate_config(ctx, cfg, &ctx->vp8_cfg);
404
405     if (!res)
406     {
407         ctx->cfg = *cfg;
408         set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
409         vp8_change_config(ctx->cpi, &ctx->oxcf);
410     }
411
412     return res;
413 }
414
415
416 int vp8_reverse_trans(int);
417
418
419 static vpx_codec_err_t get_param(vpx_codec_alg_priv_t *ctx,
420                                  int                   ctrl_id,
421                                  va_list               args)
422 {
423     void *arg = va_arg(args, void *);
424
425 #define MAP(id, var) case id: *(RECAST(id, arg)) = var; break
426
427     if (!arg)
428         return VPX_CODEC_INVALID_PARAM;
429
430     switch (ctrl_id)
431     {
432         MAP(VP8E_GET_LAST_QUANTIZER, vp8_get_quantizer(ctx->cpi));
433         MAP(VP8E_GET_LAST_QUANTIZER_64, vp8_reverse_trans(vp8_get_quantizer(ctx->cpi)));
434     }
435
436     return VPX_CODEC_OK;
437 #undef MAP
438 }
439
440
441 static vpx_codec_err_t set_param(vpx_codec_alg_priv_t *ctx,
442                                  int                   ctrl_id,
443                                  va_list               args)
444 {
445     vpx_codec_err_t     res  = VPX_CODEC_OK;
446     struct vp8_extracfg xcfg = ctx->vp8_cfg;
447
448 #define MAP(id, var) case id: var = CAST(id, args); break;
449
450     switch (ctrl_id)
451     {
452         MAP(VP8E_SET_ENCODING_MODE,         ctx->deprecated_mode);
453         MAP(VP8E_SET_CPUUSED,               xcfg.cpu_used);
454         MAP(VP8E_SET_ENABLEAUTOALTREF,      xcfg.enable_auto_alt_ref);
455         MAP(VP8E_SET_NOISE_SENSITIVITY,     xcfg.noise_sensitivity);
456         MAP(VP8E_SET_SHARPNESS,             xcfg.Sharpness);
457         MAP(VP8E_SET_STATIC_THRESHOLD,      xcfg.static_thresh);
458         MAP(VP8E_SET_TOKEN_PARTITIONS,      xcfg.token_partitions);
459
460         MAP(VP8E_SET_ARNR_MAXFRAMES,        xcfg.arnr_max_frames);
461         MAP(VP8E_SET_ARNR_STRENGTH ,        xcfg.arnr_strength);
462         MAP(VP8E_SET_ARNR_TYPE     ,        xcfg.arnr_type);
463         MAP(VP8E_SET_TUNING,                xcfg.tuning);
464         MAP(VP8E_SET_CQ_LEVEL,              xcfg.cq_level);
465
466     }
467
468     res = validate_config(ctx, &ctx->cfg, &xcfg);
469
470     if (!res)
471     {
472         ctx->vp8_cfg = xcfg;
473         set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
474         vp8_change_config(ctx->cpi, &ctx->oxcf);
475     }
476
477     return res;
478 #undef MAP
479 }
480 static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx)
481 {
482     vpx_codec_err_t        res = VPX_DEC_OK;
483     struct vpx_codec_alg_priv *priv;
484     vpx_codec_enc_cfg_t       *cfg;
485     unsigned int               i;
486
487     VP8_PTR optr;
488
489     if (!ctx->priv)
490     {
491         priv = calloc(1, sizeof(struct vpx_codec_alg_priv));
492
493         if (!priv)
494         {
495             return VPX_CODEC_MEM_ERROR;
496         }
497
498         ctx->priv = &priv->base;
499         ctx->priv->sz = sizeof(*ctx->priv);
500         ctx->priv->iface = ctx->iface;
501         ctx->priv->alg_priv = priv;
502         ctx->priv->init_flags = ctx->init_flags;
503
504         if (ctx->config.enc)
505         {
506             /* Update the reference to the config structure to an
507              * internal copy.
508              */
509             ctx->priv->alg_priv->cfg = *ctx->config.enc;
510             ctx->config.enc = &ctx->priv->alg_priv->cfg;
511         }
512
513         cfg =  &ctx->priv->alg_priv->cfg;
514
515         /* Select the extra vp6 configuration table based on the current
516          * usage value. If the current usage value isn't found, use the
517          * values for usage case 0.
518          */
519         for (i = 0;
520              extracfg_map[i].usage && extracfg_map[i].usage != cfg->g_usage;
521              i++);
522
523         priv->vp8_cfg = extracfg_map[i].cfg;
524         priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
525
526         priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
527
528         if (priv->cx_data_sz < 4096) priv->cx_data_sz = 4096;
529
530         priv->cx_data = malloc(priv->cx_data_sz);
531
532         if (!priv->cx_data)
533         {
534             return VPX_CODEC_MEM_ERROR;
535         }
536
537         priv->deprecated_mode = NO_MODE_SET;
538
539         vp8_initialize();
540
541         res = validate_config(priv, &priv->cfg, &priv->vp8_cfg);
542
543         if (!res)
544         {
545             set_vp8e_config(&ctx->priv->alg_priv->oxcf,
546                              ctx->priv->alg_priv->cfg,
547                              ctx->priv->alg_priv->vp8_cfg);
548             optr = vp8_create_compressor(&ctx->priv->alg_priv->oxcf);
549
550             if (!optr)
551                 res = VPX_CODEC_MEM_ERROR;
552             else
553                 ctx->priv->alg_priv->cpi = optr;
554         }
555     }
556
557     return res;
558 }
559
560 static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx)
561 {
562
563     free(ctx->cx_data);
564     vp8_remove_compressor(&ctx->cpi);
565     free(ctx);
566     return VPX_CODEC_OK;
567 }
568
569 static vpx_codec_err_t image2yuvconfig(const vpx_image_t   *img,
570                                        YV12_BUFFER_CONFIG  *yv12)
571 {
572     vpx_codec_err_t        res = VPX_CODEC_OK;
573     yv12->y_buffer = img->planes[VPX_PLANE_Y];
574     yv12->u_buffer = img->planes[VPX_PLANE_U];
575     yv12->v_buffer = img->planes[VPX_PLANE_V];
576
577     yv12->y_width  = img->d_w;
578     yv12->y_height = img->d_h;
579     yv12->uv_width = (1 + yv12->y_width) / 2;
580     yv12->uv_height = (1 + yv12->y_height) / 2;
581
582     yv12->y_stride = img->stride[VPX_PLANE_Y];
583     yv12->uv_stride = img->stride[VPX_PLANE_U];
584
585     yv12->border  = (img->stride[VPX_PLANE_Y] - img->w) / 2;
586     yv12->clrtype = (img->fmt == VPX_IMG_FMT_VPXI420 || img->fmt == VPX_IMG_FMT_VPXYV12); //REG_YUV = 0
587     return res;
588 }
589
590 static void pick_quickcompress_mode(vpx_codec_alg_priv_t  *ctx,
591                                     unsigned long          duration,
592                                     unsigned long          deadline)
593 {
594     unsigned int new_qc;
595
596 #if !(CONFIG_REALTIME_ONLY)
597     /* Use best quality mode if no deadline is given. */
598     new_qc = MODE_BESTQUALITY;
599
600     if (deadline)
601     {
602         uint64_t     duration_us;
603
604         /* Convert duration parameter from stream timebase to microseconds */
605         duration_us = (uint64_t)duration * 1000000
606                       * (uint64_t)ctx->cfg.g_timebase.num
607                       / (uint64_t)ctx->cfg.g_timebase.den;
608
609         /* If the deadline is more that the duration this frame is to be shown,
610          * use good quality mode. Otherwise use realtime mode.
611          */
612         new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
613     }
614
615 #else
616     new_qc = MODE_REALTIME;
617 #endif
618
619     switch (ctx->deprecated_mode)
620     {
621     case VP8_BEST_QUALITY_ENCODING:
622         new_qc = MODE_BESTQUALITY;
623         break;
624     case VP8_GOOD_QUALITY_ENCODING:
625         new_qc = MODE_GOODQUALITY;
626         break;
627     case VP8_REAL_TIME_ENCODING:
628         new_qc = MODE_REALTIME;
629         break;
630     }
631
632     if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS)
633         new_qc = MODE_FIRSTPASS;
634     else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS)
635         new_qc = (new_qc == MODE_BESTQUALITY)
636                  ? MODE_SECONDPASS_BEST
637                  : MODE_SECONDPASS;
638
639     if (ctx->oxcf.Mode != new_qc)
640     {
641         ctx->oxcf.Mode = new_qc;
642         vp8_change_config(ctx->cpi, &ctx->oxcf);
643     }
644 }
645
646
647 static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t  *ctx,
648                                    const vpx_image_t     *img,
649                                    vpx_codec_pts_t        pts,
650                                    unsigned long          duration,
651                                    vpx_enc_frame_flags_t  flags,
652                                    unsigned long          deadline)
653 {
654     vpx_codec_err_t res = VPX_CODEC_OK;
655
656     if (img)
657         res = validate_img(ctx, img);
658
659     pick_quickcompress_mode(ctx, duration, deadline);
660     vpx_codec_pkt_list_init(&ctx->pkt_list);
661
662     /* Handle Flags */
663     if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF))
664         || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF)))
665     {
666         ctx->base.err_detail = "Conflicting flags.";
667         return VPX_CODEC_INVALID_PARAM;
668     }
669
670     if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF
671                  | VP8_EFLAG_NO_REF_ARF))
672     {
673         int ref = 7;
674
675         if (flags & VP8_EFLAG_NO_REF_LAST)
676             ref ^= VP8_LAST_FLAG;
677
678         if (flags & VP8_EFLAG_NO_REF_GF)
679             ref ^= VP8_GOLD_FLAG;
680
681         if (flags & VP8_EFLAG_NO_REF_ARF)
682             ref ^= VP8_ALT_FLAG;
683
684         vp8_use_as_reference(ctx->cpi, ref);
685     }
686
687     if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF
688                  | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF
689                  | VP8_EFLAG_FORCE_ARF))
690     {
691         int upd = 7;
692
693         if (flags & VP8_EFLAG_NO_UPD_LAST)
694             upd ^= VP8_LAST_FLAG;
695
696         if (flags & VP8_EFLAG_NO_UPD_GF)
697             upd ^= VP8_GOLD_FLAG;
698
699         if (flags & VP8_EFLAG_NO_UPD_ARF)
700             upd ^= VP8_ALT_FLAG;
701
702         vp8_update_reference(ctx->cpi, upd);
703     }
704
705     if (flags & VP8_EFLAG_NO_UPD_ENTROPY)
706     {
707         vp8_update_entropy(ctx->cpi, 0);
708     }
709
710     /* Handle fixed keyframe intervals */
711     if (ctx->cfg.kf_mode == VPX_KF_AUTO
712         && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist)
713     {
714         if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist)
715         {
716             flags |= VPX_EFLAG_FORCE_KF;
717             ctx->fixed_kf_cntr = 1;
718         }
719     }
720
721     /* Initialize the encoder instance on the first frame*/
722     if (!res && ctx->cpi)
723     {
724         unsigned int lib_flags;
725         YV12_BUFFER_CONFIG sd;
726         INT64 dst_time_stamp, dst_end_time_stamp;
727         unsigned long size, cx_data_sz;
728         unsigned char *cx_data;
729
730         /* Set up internal flags */
731         if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
732             ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1;
733
734         if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION)
735             ((VP8_COMP *)ctx->cpi)->output_partition = 1;
736
737         /* Convert API flags to internal codec lib flags */
738         lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
739
740         /* vp8 use 10,000,000 ticks/second as time stamp */
741         dst_time_stamp    = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
742         dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
743
744         if (img != NULL)
745         {
746             res = image2yuvconfig(img, &sd);
747
748             if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
749                                       &sd, dst_time_stamp, dst_end_time_stamp))
750             {
751                 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
752                 res = update_error_state(ctx, &cpi->common.error);
753             }
754
755             /* reset for next frame */
756             ctx->next_frame_flag = 0;
757         }
758
759         cx_data = ctx->cx_data;
760         cx_data_sz = ctx->cx_data_sz;
761         lib_flags = 0;
762
763         while (cx_data_sz >= ctx->cx_data_sz / 2
764                && -1 != vp8_get_compressed_data(ctx->cpi, &lib_flags, &size, cx_data, &dst_time_stamp, &dst_end_time_stamp, !img))
765         {
766             if (size)
767             {
768                 vpx_codec_pts_t    round, delta;
769                 vpx_codec_cx_pkt_t pkt;
770                 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
771
772                 /* Add the frame packet to the list of returned packets. */
773                 round = 1000000 * ctx->cfg.g_timebase.num / 2 - 1;
774                 delta = (dst_end_time_stamp - dst_time_stamp);
775                 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
776                 pkt.data.frame.pts =
777                     (dst_time_stamp * ctx->cfg.g_timebase.den + round)
778                     / ctx->cfg.g_timebase.num / 10000000;
779                 pkt.data.frame.duration =
780                     (delta * ctx->cfg.g_timebase.den + round)
781                     / ctx->cfg.g_timebase.num / 10000000;
782                 pkt.data.frame.flags = lib_flags << 16;
783
784                 if (lib_flags & FRAMEFLAGS_KEY)
785                     pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
786
787                 if (!cpi->common.show_frame)
788                 {
789                     pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
790
791                     // This timestamp should be as close as possible to the
792                     // prior PTS so that if a decoder uses pts to schedule when
793                     // to do this, we start right after last frame was decoded.
794                     // Invisible frames have no duration.
795                     pkt.data.frame.pts = ((cpi->last_time_stamp_seen
796                         * ctx->cfg.g_timebase.den + round)
797                         / ctx->cfg.g_timebase.num / 10000000) + 1;
798                     pkt.data.frame.duration = 0;
799                 }
800
801                 if (cpi->output_partition)
802                 {
803                     int i;
804                     const int num_partitions =
805                             (1 << cpi->common.multi_token_partition) + 1;
806                     for (i = 0; i < num_partitions; ++i)
807                     {
808                         pkt.data.frame.buf = cx_data;
809                         pkt.data.frame.sz = cpi->partition_sz[i];
810                         pkt.data.frame.partition_id = i;
811                         /* don't set the fragment bit for the last partition */
812                         if (i < num_partitions - 1)
813                             pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT;
814                         vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
815                         cx_data += cpi->partition_sz[i];
816                         cx_data_sz -= cpi->partition_sz[i];
817                     }
818                 }
819                 else
820                 {
821                     pkt.data.frame.buf = cx_data;
822                     pkt.data.frame.sz  = size;
823                     pkt.data.frame.partition_id = -1;
824                     vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
825                     cx_data += size;
826                     cx_data_sz -= size;
827                 }
828
829                 //printf("timestamp: %lld, duration: %d\n", pkt->data.frame.pts, pkt->data.frame.duration);
830             }
831         }
832     }
833
834     return res;
835 }
836
837
838 static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t  *ctx,
839         vpx_codec_iter_t      *iter)
840 {
841     return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
842 }
843
844 static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx,
845         int ctr_id,
846         va_list args)
847 {
848     vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
849
850     if (data)
851     {
852         vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
853         YV12_BUFFER_CONFIG sd;
854
855         image2yuvconfig(&frame->img, &sd);
856         vp8_set_reference(ctx->cpi, frame->frame_type, &sd);
857         return VPX_CODEC_OK;
858     }
859     else
860         return VPX_CODEC_INVALID_PARAM;
861
862 }
863
864 static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx,
865         int ctr_id,
866         va_list args)
867 {
868
869     vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
870
871     if (data)
872     {
873         vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
874         YV12_BUFFER_CONFIG sd;
875
876         image2yuvconfig(&frame->img, &sd);
877         vp8_get_reference(ctx->cpi, frame->frame_type, &sd);
878         return VPX_CODEC_OK;
879     }
880     else
881         return VPX_CODEC_INVALID_PARAM;
882 }
883
884 static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx,
885         int ctr_id,
886         va_list args)
887 {
888 #if CONFIG_POSTPROC
889     vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
890     (void)ctr_id;
891
892     if (data)
893     {
894         ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
895         return VPX_CODEC_OK;
896     }
897     else
898         return VPX_CODEC_INVALID_PARAM;
899 #else
900     (void)ctx;
901     (void)ctr_id;
902     (void)args;
903     return VPX_CODEC_INCAPABLE;
904 #endif
905 }
906
907
908 static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx)
909 {
910
911     YV12_BUFFER_CONFIG sd;
912     vp8_ppflags_t flags = {0};
913
914     if (ctx->preview_ppcfg.post_proc_flag)
915     {
916         flags.post_proc_flag        = ctx->preview_ppcfg.post_proc_flag;
917         flags.deblocking_level      = ctx->preview_ppcfg.deblocking_level;
918         flags.noise_level           = ctx->preview_ppcfg.noise_level;
919     }
920
921     if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags))
922     {
923
924         /*
925         vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
926             sd.y_width + 2*VP8BORDERINPIXELS,
927             sd.y_height + 2*VP8BORDERINPIXELS,
928             1,
929             sd.buffer_alloc);
930         vpx_img_set_rect(&ctx->preview_img,
931             VP8BORDERINPIXELS, VP8BORDERINPIXELS,
932             sd.y_width, sd.y_height);
933             */
934
935         ctx->preview_img.bps = 12;
936         ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer;
937         ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer;
938         ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer;
939
940         if (sd.clrtype == REG_YUV)
941             ctx->preview_img.fmt = VPX_IMG_FMT_I420;
942         else
943             ctx->preview_img.fmt = VPX_IMG_FMT_VPXI420;
944
945         ctx->preview_img.x_chroma_shift = 1;
946         ctx->preview_img.y_chroma_shift = 1;
947
948         ctx->preview_img.d_w = sd.y_width;
949         ctx->preview_img.d_h = sd.y_height;
950         ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride;
951         ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride;
952         ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride;
953         ctx->preview_img.w   = sd.y_width;
954         ctx->preview_img.h   = sd.y_height;
955
956         return &ctx->preview_img;
957     }
958     else
959         return NULL;
960 }
961
962 static vpx_codec_err_t vp8e_update_entropy(vpx_codec_alg_priv_t *ctx,
963         int ctr_id,
964         va_list args)
965 {
966     int update = va_arg(args, int);
967     vp8_update_entropy(ctx->cpi, update);
968     return VPX_CODEC_OK;
969
970 }
971
972 static vpx_codec_err_t vp8e_update_reference(vpx_codec_alg_priv_t *ctx,
973         int ctr_id,
974         va_list args)
975 {
976     int update = va_arg(args, int);
977     vp8_update_reference(ctx->cpi, update);
978     return VPX_CODEC_OK;
979 }
980
981 static vpx_codec_err_t vp8e_use_reference(vpx_codec_alg_priv_t *ctx,
982         int ctr_id,
983         va_list args)
984 {
985     int reference_flag = va_arg(args, int);
986     vp8_use_as_reference(ctx->cpi, reference_flag);
987     return VPX_CODEC_OK;
988 }
989
990 static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx,
991                                         int ctr_id,
992                                         va_list args)
993 {
994     vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
995
996     if (data)
997     {
998         vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
999
1000         if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols, roi->delta_q, roi->delta_lf, roi->static_threshold))
1001             return VPX_CODEC_OK;
1002         else
1003             return VPX_CODEC_INVALID_PARAM;
1004     }
1005     else
1006         return VPX_CODEC_INVALID_PARAM;
1007 }
1008
1009
1010 static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx,
1011         int ctr_id,
1012         va_list args)
1013 {
1014     vpx_active_map_t *data = va_arg(args, vpx_active_map_t *);
1015
1016     if (data)
1017     {
1018
1019         vpx_active_map_t *map = (vpx_active_map_t *)data;
1020
1021         if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols))
1022             return VPX_CODEC_OK;
1023         else
1024             return VPX_CODEC_INVALID_PARAM;
1025     }
1026     else
1027         return VPX_CODEC_INVALID_PARAM;
1028 }
1029
1030 static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx,
1031         int ctr_id,
1032         va_list args)
1033 {
1034
1035     vpx_scaling_mode_t *data =  va_arg(args, vpx_scaling_mode_t *);
1036
1037     if (data)
1038     {
1039         int res;
1040         vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data ;
1041         res = vp8_set_internal_size(ctx->cpi, scalemode.h_scaling_mode, scalemode.v_scaling_mode);
1042
1043         if (!res)
1044         {
1045             /*force next frame a key frame to effect scaling mode */
1046             ctx->next_frame_flag |= FRAMEFLAGS_KEY;
1047             return VPX_CODEC_OK;
1048         }
1049         else
1050             return VPX_CODEC_INVALID_PARAM;
1051     }
1052     else
1053         return VPX_CODEC_INVALID_PARAM;
1054 }
1055
1056
1057 static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] =
1058 {
1059     {VP8_SET_REFERENCE,                 vp8e_set_reference},
1060     {VP8_COPY_REFERENCE,                vp8e_get_reference},
1061     {VP8_SET_POSTPROC,                  vp8e_set_previewpp},
1062     {VP8E_UPD_ENTROPY,                  vp8e_update_entropy},
1063     {VP8E_UPD_REFERENCE,                vp8e_update_reference},
1064     {VP8E_USE_REFERENCE,                vp8e_use_reference},
1065     {VP8E_SET_ROI_MAP,                  vp8e_set_roi_map},
1066     {VP8E_SET_ACTIVEMAP,                vp8e_set_activemap},
1067     {VP8E_SET_SCALEMODE,                vp8e_set_scalemode},
1068     {VP8E_SET_ENCODING_MODE,            set_param},
1069     {VP8E_SET_CPUUSED,                  set_param},
1070     {VP8E_SET_NOISE_SENSITIVITY,        set_param},
1071     {VP8E_SET_ENABLEAUTOALTREF,         set_param},
1072     {VP8E_SET_SHARPNESS,                set_param},
1073     {VP8E_SET_STATIC_THRESHOLD,         set_param},
1074     {VP8E_SET_TOKEN_PARTITIONS,         set_param},
1075     {VP8E_GET_LAST_QUANTIZER,           get_param},
1076     {VP8E_GET_LAST_QUANTIZER_64,        get_param},
1077     {VP8E_SET_ARNR_MAXFRAMES,           set_param},
1078     {VP8E_SET_ARNR_STRENGTH ,           set_param},
1079     {VP8E_SET_ARNR_TYPE     ,           set_param},
1080     {VP8E_SET_TUNING,                   set_param},
1081     {VP8E_SET_CQ_LEVEL,                 set_param},
1082     { -1, NULL},
1083 };
1084
1085 static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] =
1086 {
1087     {
1088     0,
1089     {
1090         0,                  /* g_usage */
1091         0,                  /* g_threads */
1092         0,                  /* g_profile */
1093
1094         320,                /* g_width */
1095         240,                /* g_height */
1096         {1, 30},            /* g_timebase */
1097
1098         0,                  /* g_error_resilient */
1099
1100         VPX_RC_ONE_PASS,    /* g_pass */
1101
1102         0,                  /* g_lag_in_frames */
1103
1104         0,                  /* rc_dropframe_thresh */
1105         0,                  /* rc_resize_allowed */
1106         60,                 /* rc_resize_down_thresold */
1107         30,                 /* rc_resize_up_thresold */
1108
1109         VPX_VBR,            /* rc_end_usage */
1110 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
1111         {0},                /* rc_twopass_stats_in */
1112 #endif
1113         256,                /* rc_target_bandwidth */
1114         0,                  /* rc_max_intra_bitrate_pct */
1115         4,                  /* rc_min_quantizer */
1116         63,                 /* rc_max_quantizer */
1117         100,                /* rc_undershoot_pct */
1118         100,                /* rc_overshoot_pct */
1119
1120         6000,               /* rc_max_buffer_size */
1121         4000,               /* rc_buffer_initial_size; */
1122         5000,               /* rc_buffer_optimal_size; */
1123
1124         50,                 /* rc_two_pass_vbrbias  */
1125         0,                  /* rc_two_pass_vbrmin_section */
1126         400,                /* rc_two_pass_vbrmax_section */
1127
1128         /* keyframing settings (kf) */
1129         VPX_KF_AUTO,        /* g_kfmode*/
1130         0,                  /* kf_min_dist */
1131         9999,               /* kf_max_dist */
1132
1133 #if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION)
1134         1,                  /* g_delete_first_pass_file */
1135         "vp8.fpf"           /* first pass filename */
1136 #endif
1137     }},
1138     { -1, {NOT_IMPLEMENTED}}
1139 };
1140
1141
1142 #ifndef VERSION_STRING
1143 #define VERSION_STRING
1144 #endif
1145 CODEC_INTERFACE(vpx_codec_vp8_cx) =
1146 {
1147     "WebM Project VP8 Encoder" VERSION_STRING,
1148     VPX_CODEC_INTERNAL_ABI_VERSION,
1149     VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR |
1150     VPX_CODEC_CAP_OUTPUT_PARTITION,
1151     /* vpx_codec_caps_t          caps; */
1152     vp8e_init,          /* vpx_codec_init_fn_t       init; */
1153     vp8e_destroy,       /* vpx_codec_destroy_fn_t    destroy; */
1154     vp8e_ctf_maps,      /* vpx_codec_ctrl_fn_map_t  *ctrl_maps; */
1155     NOT_IMPLEMENTED,    /* vpx_codec_get_mmap_fn_t   get_mmap; */
1156     NOT_IMPLEMENTED,    /* vpx_codec_set_mmap_fn_t   set_mmap; */
1157     {
1158         NOT_IMPLEMENTED,    /* vpx_codec_peek_si_fn_t    peek_si; */
1159         NOT_IMPLEMENTED,    /* vpx_codec_get_si_fn_t     get_si; */
1160         NOT_IMPLEMENTED,    /* vpx_codec_decode_fn_t     decode; */
1161         NOT_IMPLEMENTED,    /* vpx_codec_frame_get_fn_t  frame_get; */
1162     },
1163     {
1164         vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t    peek_si; */
1165         vp8e_encode,        /* vpx_codec_encode_fn_t      encode; */
1166         vp8e_get_cxdata,    /* vpx_codec_get_cx_data_fn_t   frame_get; */
1167         vp8e_set_config,
1168         NOT_IMPLEMENTED,
1169         vp8e_get_preview,
1170     } /* encoder functions */
1171 };
1172
1173
1174 /*
1175  * BEGIN BACKWARDS COMPATIBILITY SHIM.
1176  */
1177 #define FORCE_KEY   2
1178 static vpx_codec_err_t api1_control(vpx_codec_alg_priv_t *ctx,
1179                                     int                   ctrl_id,
1180                                     va_list               args)
1181 {
1182     vpx_codec_ctrl_fn_map_t *entry;
1183
1184     switch (ctrl_id)
1185     {
1186     case VP8E_SET_FLUSHFLAG:
1187         /* VP8 sample code did VP8E_SET_FLUSHFLAG followed by
1188          * vpx_codec_get_cx_data() rather than vpx_codec_encode().
1189          */
1190         return vp8e_encode(ctx, NULL, 0, 0, 0, 0);
1191     case VP8E_SET_FRAMETYPE:
1192         ctx->base.enc.tbd |= FORCE_KEY;
1193         return VPX_CODEC_OK;
1194     }
1195
1196     for (entry = vp8e_ctf_maps; entry && entry->fn; entry++)
1197     {
1198         if (!entry->ctrl_id || entry->ctrl_id == ctrl_id)
1199         {
1200             return entry->fn(ctx, ctrl_id, args);
1201         }
1202     }
1203
1204     return VPX_CODEC_ERROR;
1205 }
1206
1207
1208 static vpx_codec_ctrl_fn_map_t api1_ctrl_maps[] =
1209 {
1210     {0, api1_control},
1211     { -1, NULL}
1212 };
1213
1214
1215 static vpx_codec_err_t api1_encode(vpx_codec_alg_priv_t  *ctx,
1216                                    const vpx_image_t     *img,
1217                                    vpx_codec_pts_t        pts,
1218                                    unsigned long          duration,
1219                                    vpx_enc_frame_flags_t  flags,
1220                                    unsigned long          deadline)
1221 {
1222     int force = ctx->base.enc.tbd;
1223
1224     ctx->base.enc.tbd = 0;
1225     return vp8e_encode
1226            (ctx,
1227             img,
1228             pts,
1229             duration,
1230             flags | ((force & FORCE_KEY) ? VPX_EFLAG_FORCE_KF : 0),
1231             deadline);
1232 }
1233
1234
1235 vpx_codec_iface_t vpx_enc_vp8_algo =
1236 {
1237     "WebM Project VP8 Encoder (Deprecated API)" VERSION_STRING,
1238     VPX_CODEC_INTERNAL_ABI_VERSION,
1239     VPX_CODEC_CAP_ENCODER,
1240     /* vpx_codec_caps_t          caps; */
1241     vp8e_init,          /* vpx_codec_init_fn_t       init; */
1242     vp8e_destroy,       /* vpx_codec_destroy_fn_t    destroy; */
1243     api1_ctrl_maps,     /* vpx_codec_ctrl_fn_map_t  *ctrl_maps; */
1244     NOT_IMPLEMENTED,    /* vpx_codec_get_mmap_fn_t   get_mmap; */
1245     NOT_IMPLEMENTED,    /* vpx_codec_set_mmap_fn_t   set_mmap; */
1246     {NOT_IMPLEMENTED},  /* decoder functions */
1247     {
1248         vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t    peek_si; */
1249         api1_encode,        /* vpx_codec_encode_fn_t      encode; */
1250         vp8e_get_cxdata,    /* vpx_codec_get_cx_data_fn_t   frame_get; */
1251         vp8e_set_config,
1252         NOT_IMPLEMENTED,
1253         vp8e_get_preview,
1254     } /* encoder functions */
1255 };