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