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