1/*
2 * Copyright (c) 2010, Google, Inc.
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file
23 * VP8 encoder support via libvpx
24 */
25
26#define VPX_DISABLE_CTRL_TYPECHECKS 1
27#define VPX_CODEC_DISABLE_COMPAT    1
28#include <vpx/vpx_encoder.h>
29#include <vpx/vp8cx.h>
30
31#include "avcodec.h"
32#include "internal.h"
33#include "libavutil/avassert.h"
34#include "libvpx.h"
35#include "libavutil/base64.h"
36#include "libavutil/common.h"
37#include "libavutil/intreadwrite.h"
38#include "libavutil/mathematics.h"
39#include "libavutil/opt.h"
40
41/**
42 * Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
43 * One encoded frame returned from the library.
44 */
45struct FrameListData {
46    void *buf;                       /**< compressed data buffer */
47    size_t sz;                       /**< length of compressed data */
48    void *buf_alpha;
49    size_t sz_alpha;
50    int64_t pts;                     /**< time stamp to show frame
51                                          (in timebase units) */
52    unsigned long duration;          /**< duration to show frame
53                                          (in timebase units) */
54    uint32_t flags;                  /**< flags for this frame */
55    uint64_t sse[4];
56    int have_sse;                    /**< true if we have pending sse[] */
57    uint64_t frame_number;
58    struct FrameListData *next;
59};
60
61typedef struct VP8EncoderContext {
62    AVClass *class;
63    struct vpx_codec_ctx encoder;
64    struct vpx_image rawimg;
65    struct vpx_codec_ctx encoder_alpha;
66    struct vpx_image rawimg_alpha;
67    uint8_t is_alpha;
68    struct vpx_fixed_buf twopass_stats;
69    int deadline; //i.e., RT/GOOD/BEST
70    uint64_t sse[4];
71    int have_sse; /**< true if we have pending sse[] */
72    uint64_t frame_number;
73    struct FrameListData *coded_frame_list;
74
75    int cpu_used;
76    /**
77     * VP8 specific flags, see VP8F_* below.
78     */
79    int flags;
80#define VP8F_ERROR_RESILIENT 0x00000001 ///< Enable measures appropriate for streaming over lossy links
81#define VP8F_AUTO_ALT_REF    0x00000002 ///< Enable automatic alternate reference frame generation
82
83    int auto_alt_ref;
84
85    int arnr_max_frames;
86    int arnr_strength;
87    int arnr_type;
88
89    int lag_in_frames;
90    int error_resilient;
91    int crf;
92    int max_intra_rate;
93
94    // VP9-only
95    int lossless;
96    int tile_columns;
97    int tile_rows;
98    int frame_parallel;
99} VP8Context;
100
101/** String mappings for enum vp8e_enc_control_id */
102static const char *const ctlidstr[] = {
103    [VP8E_UPD_ENTROPY]           = "VP8E_UPD_ENTROPY",
104    [VP8E_UPD_REFERENCE]         = "VP8E_UPD_REFERENCE",
105    [VP8E_USE_REFERENCE]         = "VP8E_USE_REFERENCE",
106    [VP8E_SET_ROI_MAP]           = "VP8E_SET_ROI_MAP",
107    [VP8E_SET_ACTIVEMAP]         = "VP8E_SET_ACTIVEMAP",
108    [VP8E_SET_SCALEMODE]         = "VP8E_SET_SCALEMODE",
109    [VP8E_SET_CPUUSED]           = "VP8E_SET_CPUUSED",
110    [VP8E_SET_ENABLEAUTOALTREF]  = "VP8E_SET_ENABLEAUTOALTREF",
111    [VP8E_SET_NOISE_SENSITIVITY] = "VP8E_SET_NOISE_SENSITIVITY",
112    [VP8E_SET_SHARPNESS]         = "VP8E_SET_SHARPNESS",
113    [VP8E_SET_STATIC_THRESHOLD]  = "VP8E_SET_STATIC_THRESHOLD",
114    [VP8E_SET_TOKEN_PARTITIONS]  = "VP8E_SET_TOKEN_PARTITIONS",
115    [VP8E_GET_LAST_QUANTIZER]    = "VP8E_GET_LAST_QUANTIZER",
116    [VP8E_SET_ARNR_MAXFRAMES]    = "VP8E_SET_ARNR_MAXFRAMES",
117    [VP8E_SET_ARNR_STRENGTH]     = "VP8E_SET_ARNR_STRENGTH",
118    [VP8E_SET_ARNR_TYPE]         = "VP8E_SET_ARNR_TYPE",
119    [VP8E_SET_CQ_LEVEL]          = "VP8E_SET_CQ_LEVEL",
120    [VP8E_SET_MAX_INTRA_BITRATE_PCT] = "VP8E_SET_MAX_INTRA_BITRATE_PCT",
121#if CONFIG_LIBVPX_VP9_ENCODER
122    [VP9E_SET_LOSSLESS]                = "VP9E_SET_LOSSLESS",
123    [VP9E_SET_TILE_COLUMNS]            = "VP9E_SET_TILE_COLUMNS",
124    [VP9E_SET_TILE_ROWS]               = "VP9E_SET_TILE_ROWS",
125    [VP9E_SET_FRAME_PARALLEL_DECODING] = "VP9E_SET_FRAME_PARALLEL_DECODING",
126#endif
127};
128
129static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
130{
131    VP8Context *ctx = avctx->priv_data;
132    const char *error  = vpx_codec_error(&ctx->encoder);
133    const char *detail = vpx_codec_error_detail(&ctx->encoder);
134
135    av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
136    if (detail)
137        av_log(avctx, AV_LOG_ERROR, "  Additional information: %s\n", detail);
138}
139
140static av_cold void dump_enc_cfg(AVCodecContext *avctx,
141                                 const struct vpx_codec_enc_cfg *cfg)
142{
143    int width = -30;
144    int level = AV_LOG_DEBUG;
145
146    av_log(avctx, level, "vpx_codec_enc_cfg\n");
147    av_log(avctx, level, "generic settings\n"
148           "  %*s%u\n  %*s%u\n  %*s%u\n  %*s%u\n  %*s%u\n"
149           "  %*s{%u/%u}\n  %*s%u\n  %*s%d\n  %*s%u\n",
150           width, "g_usage:",           cfg->g_usage,
151           width, "g_threads:",         cfg->g_threads,
152           width, "g_profile:",         cfg->g_profile,
153           width, "g_w:",               cfg->g_w,
154           width, "g_h:",               cfg->g_h,
155           width, "g_timebase:",        cfg->g_timebase.num, cfg->g_timebase.den,
156           width, "g_error_resilient:", cfg->g_error_resilient,
157           width, "g_pass:",            cfg->g_pass,
158           width, "g_lag_in_frames:",   cfg->g_lag_in_frames);
159    av_log(avctx, level, "rate control settings\n"
160           "  %*s%u\n  %*s%u\n  %*s%u\n  %*s%u\n"
161           "  %*s%d\n  %*s%p(%"SIZE_SPECIFIER")\n  %*s%u\n",
162           width, "rc_dropframe_thresh:",   cfg->rc_dropframe_thresh,
163           width, "rc_resize_allowed:",     cfg->rc_resize_allowed,
164           width, "rc_resize_up_thresh:",   cfg->rc_resize_up_thresh,
165           width, "rc_resize_down_thresh:", cfg->rc_resize_down_thresh,
166           width, "rc_end_usage:",          cfg->rc_end_usage,
167           width, "rc_twopass_stats_in:",   cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
168           width, "rc_target_bitrate:",     cfg->rc_target_bitrate);
169    av_log(avctx, level, "quantizer settings\n"
170           "  %*s%u\n  %*s%u\n",
171           width, "rc_min_quantizer:", cfg->rc_min_quantizer,
172           width, "rc_max_quantizer:", cfg->rc_max_quantizer);
173    av_log(avctx, level, "bitrate tolerance\n"
174           "  %*s%u\n  %*s%u\n",
175           width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
176           width, "rc_overshoot_pct:",  cfg->rc_overshoot_pct);
177    av_log(avctx, level, "decoder buffer model\n"
178            "  %*s%u\n  %*s%u\n  %*s%u\n",
179            width, "rc_buf_sz:",         cfg->rc_buf_sz,
180            width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
181            width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
182    av_log(avctx, level, "2 pass rate control settings\n"
183           "  %*s%u\n  %*s%u\n  %*s%u\n",
184           width, "rc_2pass_vbr_bias_pct:",       cfg->rc_2pass_vbr_bias_pct,
185           width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
186           width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
187    av_log(avctx, level, "keyframing settings\n"
188           "  %*s%d\n  %*s%u\n  %*s%u\n",
189           width, "kf_mode:",     cfg->kf_mode,
190           width, "kf_min_dist:", cfg->kf_min_dist,
191           width, "kf_max_dist:", cfg->kf_max_dist);
192    av_log(avctx, level, "\n");
193}
194
195static void coded_frame_add(void *list, struct FrameListData *cx_frame)
196{
197    struct FrameListData **p = list;
198
199    while (*p != NULL)
200        p = &(*p)->next;
201    *p = cx_frame;
202    cx_frame->next = NULL;
203}
204
205static av_cold void free_coded_frame(struct FrameListData *cx_frame)
206{
207    av_freep(&cx_frame->buf);
208    if (cx_frame->buf_alpha)
209        av_freep(&cx_frame->buf_alpha);
210    av_freep(&cx_frame);
211}
212
213static av_cold void free_frame_list(struct FrameListData *list)
214{
215    struct FrameListData *p = list;
216
217    while (p) {
218        list = list->next;
219        free_coded_frame(p);
220        p = list;
221    }
222}
223
224static av_cold int codecctl_int(AVCodecContext *avctx,
225                                enum vp8e_enc_control_id id, int val)
226{
227    VP8Context *ctx = avctx->priv_data;
228    char buf[80];
229    int width = -30;
230    int res;
231
232    snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
233    av_log(avctx, AV_LOG_DEBUG, "  %*s%d\n", width, buf, val);
234
235    res = vpx_codec_control(&ctx->encoder, id, val);
236    if (res != VPX_CODEC_OK) {
237        snprintf(buf, sizeof(buf), "Failed to set %s codec control",
238                 ctlidstr[id]);
239        log_encoder_error(avctx, buf);
240    }
241
242    return res == VPX_CODEC_OK ? 0 : AVERROR(EINVAL);
243}
244
245static av_cold int vp8_free(AVCodecContext *avctx)
246{
247    VP8Context *ctx = avctx->priv_data;
248
249    vpx_codec_destroy(&ctx->encoder);
250    if (ctx->is_alpha)
251        vpx_codec_destroy(&ctx->encoder_alpha);
252    av_freep(&ctx->twopass_stats.buf);
253    av_freep(&avctx->coded_frame);
254    av_freep(&avctx->stats_out);
255    free_frame_list(ctx->coded_frame_list);
256    return 0;
257}
258
259static av_cold int vpx_init(AVCodecContext *avctx,
260                            const struct vpx_codec_iface *iface)
261{
262    VP8Context *ctx = avctx->priv_data;
263    struct vpx_codec_enc_cfg enccfg;
264    struct vpx_codec_enc_cfg enccfg_alpha;
265    vpx_codec_flags_t flags = (avctx->flags & CODEC_FLAG_PSNR) ? VPX_CODEC_USE_PSNR : 0;
266    int res;
267
268    av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
269    av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
270
271    if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P)
272        ctx->is_alpha = 1;
273
274    if ((res = vpx_codec_enc_config_default(iface, &enccfg, 0)) != VPX_CODEC_OK) {
275        av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
276               vpx_codec_err_to_string(res));
277        return AVERROR(EINVAL);
278    }
279
280    if(!avctx->bit_rate)
281        if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) {
282            av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n");
283            return AVERROR(EINVAL);
284        }
285
286    dump_enc_cfg(avctx, &enccfg);
287
288    enccfg.g_w            = avctx->width;
289    enccfg.g_h            = avctx->height;
290    enccfg.g_timebase.num = avctx->time_base.num;
291    enccfg.g_timebase.den = avctx->time_base.den;
292    enccfg.g_threads      = avctx->thread_count;
293    enccfg.g_lag_in_frames= ctx->lag_in_frames;
294
295    if (avctx->flags & CODEC_FLAG_PASS1)
296        enccfg.g_pass = VPX_RC_FIRST_PASS;
297    else if (avctx->flags & CODEC_FLAG_PASS2)
298        enccfg.g_pass = VPX_RC_LAST_PASS;
299    else
300        enccfg.g_pass = VPX_RC_ONE_PASS;
301
302    if (avctx->rc_min_rate == avctx->rc_max_rate &&
303        avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate)
304        enccfg.rc_end_usage = VPX_CBR;
305    else if (ctx->crf)
306        enccfg.rc_end_usage = VPX_CQ;
307
308    if (avctx->bit_rate) {
309        enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
310                                                AV_ROUND_NEAR_INF);
311    } else {
312        if (enccfg.rc_end_usage == VPX_CQ) {
313            enccfg.rc_target_bitrate = 1000000;
314        } else {
315            avctx->bit_rate = enccfg.rc_target_bitrate * 1000;
316            av_log(avctx, AV_LOG_WARNING,
317                   "Neither bitrate nor constrained quality specified, using default bitrate of %dkbit/sec\n",
318                   enccfg.rc_target_bitrate);
319        }
320    }
321
322    if (avctx->qmin >= 0)
323        enccfg.rc_min_quantizer = avctx->qmin;
324    if (avctx->qmax >= 0)
325        enccfg.rc_max_quantizer = avctx->qmax;
326
327    if (enccfg.rc_end_usage == VPX_CQ) {
328        if (ctx->crf < enccfg.rc_min_quantizer || ctx->crf > enccfg.rc_max_quantizer) {
329                av_log(avctx, AV_LOG_ERROR,
330                       "CQ level must be between minimum and maximum quantizer value (%d-%d)\n",
331                       enccfg.rc_min_quantizer, enccfg.rc_max_quantizer);
332                return AVERROR(EINVAL);
333        }
334    }
335
336    enccfg.rc_dropframe_thresh = avctx->frame_skip_threshold;
337
338    //0-100 (0 => CBR, 100 => VBR)
339    enccfg.rc_2pass_vbr_bias_pct           = round(avctx->qcompress * 100);
340    if (avctx->bit_rate)
341        enccfg.rc_2pass_vbr_minsection_pct     =
342            avctx->rc_min_rate * 100LL / avctx->bit_rate;
343    if (avctx->rc_max_rate)
344        enccfg.rc_2pass_vbr_maxsection_pct =
345            avctx->rc_max_rate * 100LL / avctx->bit_rate;
346
347    if (avctx->rc_buffer_size)
348        enccfg.rc_buf_sz         =
349            avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
350    if (avctx->rc_initial_buffer_occupancy)
351        enccfg.rc_buf_initial_sz =
352            avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
353    enccfg.rc_buf_optimal_sz     = enccfg.rc_buf_sz * 5 / 6;
354    enccfg.rc_undershoot_pct     = round(avctx->rc_buffer_aggressivity * 100);
355
356    //_enc_init() will balk if kf_min_dist differs from max w/VPX_KF_AUTO
357    if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
358        enccfg.kf_min_dist = avctx->keyint_min;
359    if (avctx->gop_size >= 0)
360        enccfg.kf_max_dist = avctx->gop_size;
361
362    if (enccfg.g_pass == VPX_RC_FIRST_PASS)
363        enccfg.g_lag_in_frames = 0;
364    else if (enccfg.g_pass == VPX_RC_LAST_PASS) {
365        int decode_size;
366
367        if (!avctx->stats_in) {
368            av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
369            return AVERROR_INVALIDDATA;
370        }
371
372        ctx->twopass_stats.sz  = strlen(avctx->stats_in) * 3 / 4;
373        ctx->twopass_stats.buf = av_malloc(ctx->twopass_stats.sz);
374        if (!ctx->twopass_stats.buf) {
375            av_log(avctx, AV_LOG_ERROR,
376                   "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
377                   ctx->twopass_stats.sz);
378            return AVERROR(ENOMEM);
379        }
380        decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
381                                       ctx->twopass_stats.sz);
382        if (decode_size < 0) {
383            av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
384            return AVERROR_INVALIDDATA;
385        }
386
387        ctx->twopass_stats.sz      = decode_size;
388        enccfg.rc_twopass_stats_in = ctx->twopass_stats;
389    }
390
391    /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
392       complexity playback on low powered devices at the expense of encode
393       quality. */
394   if (avctx->profile != FF_PROFILE_UNKNOWN)
395       enccfg.g_profile = avctx->profile;
396
397    enccfg.g_error_resilient = ctx->error_resilient || ctx->flags & VP8F_ERROR_RESILIENT;
398
399    dump_enc_cfg(avctx, &enccfg);
400    /* Construct Encoder Context */
401    res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, flags);
402    if (res != VPX_CODEC_OK) {
403        log_encoder_error(avctx, "Failed to initialize encoder");
404        return AVERROR(EINVAL);
405    }
406
407    if (ctx->is_alpha) {
408        enccfg_alpha = enccfg;
409        res = vpx_codec_enc_init(&ctx->encoder_alpha, iface, &enccfg_alpha, flags);
410        if (res != VPX_CODEC_OK) {
411            log_encoder_error(avctx, "Failed to initialize alpha encoder");
412            return AVERROR(EINVAL);
413        }
414    }
415
416    //codec control failures are currently treated only as warnings
417    av_log(avctx, AV_LOG_DEBUG, "vpx_codec_control\n");
418    codecctl_int(avctx, VP8E_SET_CPUUSED,          ctx->cpu_used);
419    if (ctx->flags & VP8F_AUTO_ALT_REF)
420        ctx->auto_alt_ref = 1;
421    if (ctx->auto_alt_ref >= 0)
422        codecctl_int(avctx, VP8E_SET_ENABLEAUTOALTREF, ctx->auto_alt_ref);
423    if (ctx->arnr_max_frames >= 0)
424        codecctl_int(avctx, VP8E_SET_ARNR_MAXFRAMES,   ctx->arnr_max_frames);
425    if (ctx->arnr_strength >= 0)
426        codecctl_int(avctx, VP8E_SET_ARNR_STRENGTH,    ctx->arnr_strength);
427    if (ctx->arnr_type >= 0)
428        codecctl_int(avctx, VP8E_SET_ARNR_TYPE,        ctx->arnr_type);
429    codecctl_int(avctx, VP8E_SET_NOISE_SENSITIVITY, avctx->noise_reduction);
430    if (avctx->codec_id == AV_CODEC_ID_VP8)
431        codecctl_int(avctx, VP8E_SET_TOKEN_PARTITIONS,  av_log2(avctx->slices));
432    codecctl_int(avctx, VP8E_SET_STATIC_THRESHOLD,  avctx->mb_threshold);
433    codecctl_int(avctx, VP8E_SET_CQ_LEVEL,          ctx->crf);
434    if (ctx->max_intra_rate >= 0)
435        codecctl_int(avctx, VP8E_SET_MAX_INTRA_BITRATE_PCT, ctx->max_intra_rate);
436
437#if CONFIG_LIBVPX_VP9_ENCODER
438    if (avctx->codec_id == AV_CODEC_ID_VP9) {
439        if (ctx->lossless >= 0)
440            codecctl_int(avctx, VP9E_SET_LOSSLESS, ctx->lossless);
441        if (ctx->tile_columns >= 0)
442            codecctl_int(avctx, VP9E_SET_TILE_COLUMNS, ctx->tile_columns);
443        if (ctx->tile_rows >= 0)
444            codecctl_int(avctx, VP9E_SET_TILE_ROWS, ctx->tile_rows);
445        if (ctx->frame_parallel >= 0)
446            codecctl_int(avctx, VP9E_SET_FRAME_PARALLEL_DECODING, ctx->frame_parallel);
447    }
448#endif
449
450    av_log(avctx, AV_LOG_DEBUG, "Using deadline: %d\n", ctx->deadline);
451
452    //provide dummy value to initialize wrapper, values will be updated each _encode()
453    vpx_img_wrap(&ctx->rawimg, VPX_IMG_FMT_I420, avctx->width, avctx->height, 1,
454                 (unsigned char*)1);
455
456    if (ctx->is_alpha)
457        vpx_img_wrap(&ctx->rawimg_alpha, VPX_IMG_FMT_I420, avctx->width, avctx->height, 1,
458                     (unsigned char*)1);
459
460    avctx->coded_frame = av_frame_alloc();
461    if (!avctx->coded_frame) {
462        av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
463        vp8_free(avctx);
464        return AVERROR(ENOMEM);
465    }
466    return 0;
467}
468
469static inline void cx_pktcpy(struct FrameListData *dst,
470                             const struct vpx_codec_cx_pkt *src,
471                             const struct vpx_codec_cx_pkt *src_alpha,
472                             VP8Context *ctx)
473{
474    dst->pts      = src->data.frame.pts;
475    dst->duration = src->data.frame.duration;
476    dst->flags    = src->data.frame.flags;
477    dst->sz       = src->data.frame.sz;
478    dst->buf      = src->data.frame.buf;
479    dst->have_sse = 0;
480    /* For alt-ref frame, don't store PSNR or increment frame_number */
481    if (!(dst->flags & VPX_FRAME_IS_INVISIBLE)) {
482        dst->frame_number = ++ctx->frame_number;
483        dst->have_sse = ctx->have_sse;
484        if (ctx->have_sse) {
485            /* associate last-seen SSE to the frame. */
486            /* Transfers ownership from ctx to dst. */
487            /* WARNING! This makes the assumption that PSNR_PKT comes
488               just before the frame it refers to! */
489            memcpy(dst->sse, ctx->sse, sizeof(dst->sse));
490            ctx->have_sse = 0;
491        }
492    } else {
493        dst->frame_number = -1;   /* sanity marker */
494    }
495    if (src_alpha) {
496        dst->buf_alpha = src_alpha->data.frame.buf;
497        dst->sz_alpha = src_alpha->data.frame.sz;
498    }
499    else {
500        dst->buf_alpha = NULL;
501        dst->sz_alpha = 0;
502    }
503}
504
505/**
506 * Store coded frame information in format suitable for return from encode2().
507 *
508 * Write information from @a cx_frame to @a pkt
509 * @return packet data size on success
510 * @return a negative AVERROR on error
511 */
512static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
513                      AVPacket *pkt, AVFrame *coded_frame)
514{
515    int ret = ff_alloc_packet2(avctx, pkt, cx_frame->sz);
516    uint8_t *side_data;
517    if (ret >= 0) {
518        memcpy(pkt->data, cx_frame->buf, pkt->size);
519        pkt->pts = pkt->dts    = cx_frame->pts;
520        coded_frame->pts       = cx_frame->pts;
521        coded_frame->key_frame = !!(cx_frame->flags & VPX_FRAME_IS_KEY);
522
523        if (coded_frame->key_frame) {
524            coded_frame->pict_type = AV_PICTURE_TYPE_I;
525            pkt->flags            |= AV_PKT_FLAG_KEY;
526        } else
527            coded_frame->pict_type = AV_PICTURE_TYPE_P;
528
529        if (cx_frame->have_sse) {
530            int i;
531            /* Beware of the Y/U/V/all order! */
532            coded_frame->error[0] = cx_frame->sse[1];
533            coded_frame->error[1] = cx_frame->sse[2];
534            coded_frame->error[2] = cx_frame->sse[3];
535            coded_frame->error[3] = 0;    // alpha
536            for (i = 0; i < 4; ++i) {
537                avctx->error[i] += coded_frame->error[i];
538            }
539            cx_frame->have_sse = 0;
540        }
541        if (cx_frame->sz_alpha > 0) {
542            side_data = av_packet_new_side_data(pkt,
543                                                AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
544                                                cx_frame->sz_alpha + 8);
545            if(side_data == NULL) {
546                av_free_packet(pkt);
547                av_free(pkt);
548                return AVERROR(ENOMEM);
549            }
550            AV_WB64(side_data, 1);
551            memcpy(side_data + 8, cx_frame->buf_alpha, cx_frame->sz_alpha);
552        }
553    } else {
554        return ret;
555    }
556    return pkt->size;
557}
558
559/**
560 * Queue multiple output frames from the encoder, returning the front-most.
561 * In cases where vpx_codec_get_cx_data() returns more than 1 frame append
562 * the frame queue. Return the head frame if available.
563 * @return Stored frame size
564 * @return AVERROR(EINVAL) on output size error
565 * @return AVERROR(ENOMEM) on coded frame queue data allocation error
566 */
567static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out,
568                        AVFrame *coded_frame)
569{
570    VP8Context *ctx = avctx->priv_data;
571    const struct vpx_codec_cx_pkt *pkt;
572    const struct vpx_codec_cx_pkt *pkt_alpha = NULL;
573    const void *iter = NULL;
574    const void *iter_alpha = NULL;
575    int size = 0;
576
577    if (ctx->coded_frame_list) {
578        struct FrameListData *cx_frame = ctx->coded_frame_list;
579        /* return the leading frame if we've already begun queueing */
580        size = storeframe(avctx, cx_frame, pkt_out, coded_frame);
581        if (size < 0)
582            return size;
583        ctx->coded_frame_list = cx_frame->next;
584        free_coded_frame(cx_frame);
585    }
586
587    /* consume all available output from the encoder before returning. buffers
588       are only good through the next vpx_codec call */
589    while ((pkt = vpx_codec_get_cx_data(&ctx->encoder, &iter)) &&
590            (!ctx->is_alpha ||
591             (ctx->is_alpha && (pkt_alpha = vpx_codec_get_cx_data(&ctx->encoder_alpha, &iter_alpha))))) {
592        switch (pkt->kind) {
593        case VPX_CODEC_CX_FRAME_PKT:
594            if (!size) {
595                struct FrameListData cx_frame;
596
597                /* avoid storing the frame when the list is empty and we haven't yet
598                   provided a frame for output */
599                av_assert0(!ctx->coded_frame_list);
600                cx_pktcpy(&cx_frame, pkt, pkt_alpha, ctx);
601                size = storeframe(avctx, &cx_frame, pkt_out, coded_frame);
602                if (size < 0)
603                    return size;
604            } else {
605                struct FrameListData *cx_frame =
606                    av_malloc(sizeof(struct FrameListData));
607
608                if (!cx_frame) {
609                    av_log(avctx, AV_LOG_ERROR,
610                           "Frame queue element alloc failed\n");
611                    return AVERROR(ENOMEM);
612                }
613                cx_pktcpy(cx_frame, pkt, pkt_alpha, ctx);
614                cx_frame->buf = av_malloc(cx_frame->sz);
615
616                if (!cx_frame->buf) {
617                    av_log(avctx, AV_LOG_ERROR,
618                           "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
619                           cx_frame->sz);
620                    av_free(cx_frame);
621                    return AVERROR(ENOMEM);
622                }
623                memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
624                if (ctx->is_alpha) {
625                    cx_frame->buf_alpha = av_malloc(cx_frame->sz_alpha);
626                    if (!cx_frame->buf_alpha) {
627                        av_log(avctx, AV_LOG_ERROR,
628                               "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
629                               cx_frame->sz_alpha);
630                        av_free(cx_frame);
631                        return AVERROR(ENOMEM);
632                    }
633                    memcpy(cx_frame->buf_alpha, pkt_alpha->data.frame.buf, pkt_alpha->data.frame.sz);
634                }
635                coded_frame_add(&ctx->coded_frame_list, cx_frame);
636            }
637            break;
638        case VPX_CODEC_STATS_PKT: {
639            struct vpx_fixed_buf *stats = &ctx->twopass_stats;
640            int err;
641            if ((err = av_reallocp(&stats->buf,
642                                   stats->sz +
643                                   pkt->data.twopass_stats.sz)) < 0) {
644                stats->sz = 0;
645                av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
646                return err;
647            }
648            memcpy((uint8_t*)stats->buf + stats->sz,
649                   pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
650            stats->sz += pkt->data.twopass_stats.sz;
651            break;
652        }
653        case VPX_CODEC_PSNR_PKT:
654            av_assert0(!ctx->have_sse);
655            ctx->sse[0] = pkt->data.psnr.sse[0];
656            ctx->sse[1] = pkt->data.psnr.sse[1];
657            ctx->sse[2] = pkt->data.psnr.sse[2];
658            ctx->sse[3] = pkt->data.psnr.sse[3];
659            ctx->have_sse = 1;
660            break;
661        case VPX_CODEC_CUSTOM_PKT:
662            //ignore unsupported/unrecognized packet types
663            break;
664        }
665    }
666
667    return size;
668}
669
670static int vp8_encode(AVCodecContext *avctx, AVPacket *pkt,
671                      const AVFrame *frame, int *got_packet)
672{
673    VP8Context *ctx = avctx->priv_data;
674    struct vpx_image *rawimg = NULL;
675    struct vpx_image *rawimg_alpha = NULL;
676    int64_t timestamp = 0;
677    int res, coded_size;
678    vpx_enc_frame_flags_t flags = 0;
679
680    if (frame) {
681        rawimg                      = &ctx->rawimg;
682        rawimg->planes[VPX_PLANE_Y] = frame->data[0];
683        rawimg->planes[VPX_PLANE_U] = frame->data[1];
684        rawimg->planes[VPX_PLANE_V] = frame->data[2];
685        rawimg->stride[VPX_PLANE_Y] = frame->linesize[0];
686        rawimg->stride[VPX_PLANE_U] = frame->linesize[1];
687        rawimg->stride[VPX_PLANE_V] = frame->linesize[2];
688        if (ctx->is_alpha) {
689            uint8_t *u_plane, *v_plane;
690            rawimg_alpha = &ctx->rawimg_alpha;
691            rawimg_alpha->planes[VPX_PLANE_Y] = frame->data[3];
692            u_plane = av_malloc(frame->linesize[1] * frame->height);
693            memset(u_plane, 0x80, frame->linesize[1] * frame->height);
694            rawimg_alpha->planes[VPX_PLANE_U] = u_plane;
695            v_plane = av_malloc(frame->linesize[2] * frame->height);
696            memset(v_plane, 0x80, frame->linesize[2] * frame->height);
697            rawimg_alpha->planes[VPX_PLANE_V] = v_plane;
698            rawimg_alpha->stride[VPX_PLANE_Y] = frame->linesize[0];
699            rawimg_alpha->stride[VPX_PLANE_U] = frame->linesize[1];
700            rawimg_alpha->stride[VPX_PLANE_V] = frame->linesize[2];
701        }
702        timestamp                   = frame->pts;
703        if (frame->pict_type == AV_PICTURE_TYPE_I)
704            flags |= VPX_EFLAG_FORCE_KF;
705    }
706
707    res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp,
708                           avctx->ticks_per_frame, flags, ctx->deadline);
709    if (res != VPX_CODEC_OK) {
710        log_encoder_error(avctx, "Error encoding frame");
711        return AVERROR_INVALIDDATA;
712    }
713
714    if (ctx->is_alpha) {
715        res = vpx_codec_encode(&ctx->encoder_alpha, rawimg_alpha, timestamp,
716                               avctx->ticks_per_frame, flags, ctx->deadline);
717        if (res != VPX_CODEC_OK) {
718            log_encoder_error(avctx, "Error encoding alpha frame");
719            return AVERROR_INVALIDDATA;
720        }
721    }
722
723    coded_size = queue_frames(avctx, pkt, avctx->coded_frame);
724
725    if (!frame && avctx->flags & CODEC_FLAG_PASS1) {
726        unsigned int b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
727
728        avctx->stats_out = av_malloc(b64_size);
729        if (!avctx->stats_out) {
730            av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%d bytes) failed\n",
731                   b64_size);
732            return AVERROR(ENOMEM);
733        }
734        av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
735                         ctx->twopass_stats.sz);
736    }
737
738    if (rawimg_alpha) {
739        av_free(rawimg_alpha->planes[VPX_PLANE_U]);
740        av_free(rawimg_alpha->planes[VPX_PLANE_V]);
741    }
742
743    *got_packet = !!coded_size;
744    return 0;
745}
746
747#define OFFSET(x) offsetof(VP8Context, x)
748#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
749
750#ifndef VPX_ERROR_RESILIENT_DEFAULT
751#define VPX_ERROR_RESILIENT_DEFAULT 1
752#define VPX_ERROR_RESILIENT_PARTITIONS 2
753#endif
754
755#define COMMON_OPTIONS \
756    { "cpu-used",        "Quality/Speed ratio modifier",           OFFSET(cpu_used),        AV_OPT_TYPE_INT, {.i64 = 1},       -16,     16,      VE}, \
757    { "auto-alt-ref",    "Enable use of alternate reference " \
758                         "frames (2-pass only)",                   OFFSET(auto_alt_ref),    AV_OPT_TYPE_INT, {.i64 = -1},      -1,      1,       VE}, \
759    { "lag-in-frames",   "Number of frames to look ahead for " \
760                         "alternate reference frame selection",    OFFSET(lag_in_frames),   AV_OPT_TYPE_INT, {.i64 = -1},      -1,      INT_MAX, VE}, \
761    { "arnr-maxframes",  "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1},      -1,      INT_MAX, VE}, \
762    { "arnr-strength",   "altref noise reduction filter strength", OFFSET(arnr_strength),   AV_OPT_TYPE_INT, {.i64 = -1},      -1,      INT_MAX, VE}, \
763    { "arnr-type",       "altref noise reduction filter type",     OFFSET(arnr_type),       AV_OPT_TYPE_INT, {.i64 = -1},      -1,      INT_MAX, VE, "arnr_type"}, \
764    { "backward",        NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "arnr_type" }, \
765    { "forward",         NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "arnr_type" }, \
766    { "centered",        NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "arnr_type" }, \
767    { "deadline",        "Time to spend encoding, in microseconds.", OFFSET(deadline),      AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \
768    { "best",            NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_BEST_QUALITY}, 0, 0, VE, "quality"}, \
769    { "good",            NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_GOOD_QUALITY}, 0, 0, VE, "quality"}, \
770    { "realtime",        NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_REALTIME},     0, 0, VE, "quality"}, \
771    { "error-resilient", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"}, \
772    { "max-intra-rate",  "Maximum I-frame bitrate (pct) 0=unlimited",  OFFSET(max_intra_rate),  AV_OPT_TYPE_INT,  {.i64 = -1}, -1,      INT_MAX, VE}, \
773    { "default",         "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"}, \
774    { "partitions",      "The frame partitions are independently decodable " \
775                         "by the bool decoder, meaning that partitions can be decoded even " \
776                         "though earlier partitions have been lost. Note that intra predicition" \
777                         " is still done over the partition boundary.",       0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_PARTITIONS}, 0, 0, VE, "er"}, \
778    { "crf",              "Select the quality for constant quality mode", offsetof(VP8Context, crf), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 63, VE }, \
779
780#define LEGACY_OPTIONS \
781    {"speed", "", offsetof(VP8Context, cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -16, 16, VE}, \
782    {"quality", "", offsetof(VP8Context, deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \
783    {"vp8flags", "", offsetof(VP8Context, flags), FF_OPT_TYPE_FLAGS, {.i64 = 0}, 0, UINT_MAX, VE, "flags"}, \
784    {"error_resilient", "enable error resilience", 0, FF_OPT_TYPE_CONST, {.dbl = VP8F_ERROR_RESILIENT}, INT_MIN, INT_MAX, VE, "flags"}, \
785    {"altref", "enable use of alternate reference frames (VP8/2-pass only)", 0, FF_OPT_TYPE_CONST, {.dbl = VP8F_AUTO_ALT_REF}, INT_MIN, INT_MAX, VE, "flags"}, \
786    {"arnr_max_frames", "altref noise reduction max frame count", offsetof(VP8Context, arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 15, VE}, \
787    {"arnr_strength", "altref noise reduction filter strength", offsetof(VP8Context, arnr_strength), AV_OPT_TYPE_INT, {.i64 = 3}, 0, 6, VE}, \
788    {"arnr_type", "altref noise reduction filter type", offsetof(VP8Context, arnr_type), AV_OPT_TYPE_INT, {.i64 = 3}, 1, 3, VE}, \
789    {"rc_lookahead", "Number of frames to look ahead for alternate reference frame selection", offsetof(VP8Context, lag_in_frames), AV_OPT_TYPE_INT, {.i64 = 25}, 0, 25, VE}, \
790
791#if CONFIG_LIBVPX_VP8_ENCODER
792static const AVOption vp8_options[] = {
793    COMMON_OPTIONS
794    LEGACY_OPTIONS
795    { NULL }
796};
797#endif
798
799#if CONFIG_LIBVPX_VP9_ENCODER
800static const AVOption vp9_options[] = {
801    COMMON_OPTIONS
802    { "lossless",        "Lossless mode",                               OFFSET(lossless),        AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
803    { "tile-columns",    "Number of tile columns to use, log2",         OFFSET(tile_columns),    AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
804    { "tile-rows",       "Number of tile rows to use, log2",            OFFSET(tile_rows),       AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE},
805    { "frame-parallel",  "Enable frame parallel decodability features", OFFSET(frame_parallel),  AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
806    LEGACY_OPTIONS
807    { NULL }
808};
809#endif
810
811#undef COMMON_OPTIONS
812#undef LEGACY_OPTIONS
813
814static const AVCodecDefault defaults[] = {
815    { "qmin",             "-1" },
816    { "qmax",             "-1" },
817    { "g",                "-1" },
818    { "keyint_min",       "-1" },
819    { NULL },
820};
821
822#if CONFIG_LIBVPX_VP8_ENCODER
823static av_cold int vp8_init(AVCodecContext *avctx)
824{
825    return vpx_init(avctx, &vpx_codec_vp8_cx_algo);
826}
827
828static const AVClass class_vp8 = {
829    .class_name = "libvpx-vp8 encoder",
830    .item_name  = av_default_item_name,
831    .option     = vp8_options,
832    .version    = LIBAVUTIL_VERSION_INT,
833};
834
835AVCodec ff_libvpx_vp8_encoder = {
836    .name           = "libvpx",
837    .long_name      = NULL_IF_CONFIG_SMALL("libvpx VP8"),
838    .type           = AVMEDIA_TYPE_VIDEO,
839    .id             = AV_CODEC_ID_VP8,
840    .priv_data_size = sizeof(VP8Context),
841    .init           = vp8_init,
842    .encode2        = vp8_encode,
843    .close          = vp8_free,
844    .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
845    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE },
846    .priv_class     = &class_vp8,
847    .defaults       = defaults,
848};
849#endif /* CONFIG_LIBVPX_VP8_ENCODER */
850
851#if CONFIG_LIBVPX_VP9_ENCODER
852static av_cold int vp9_init(AVCodecContext *avctx)
853{
854    return vpx_init(avctx, &vpx_codec_vp9_cx_algo);
855}
856
857static const AVClass class_vp9 = {
858    .class_name = "libvpx-vp9 encoder",
859    .item_name  = av_default_item_name,
860    .option     = vp9_options,
861    .version    = LIBAVUTIL_VERSION_INT,
862};
863
864AVCodec ff_libvpx_vp9_encoder = {
865    .name           = "libvpx-vp9",
866    .long_name      = NULL_IF_CONFIG_SMALL("libvpx VP9"),
867    .type           = AVMEDIA_TYPE_VIDEO,
868    .id             = AV_CODEC_ID_VP9,
869    .priv_data_size = sizeof(VP8Context),
870    .init           = vp9_init,
871    .encode2        = vp8_encode,
872    .close          = vp8_free,
873    .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
874    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
875    .priv_class     = &class_vp9,
876    .defaults       = defaults,
877    .init_static_data = ff_vp9_init_static,
878};
879#endif /* CONFIG_LIBVPX_VP9_ENCODER */
880