2 * Copyright (c) 2010, Google, Inc.
4 * This file is part of FFmpeg.
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.
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.
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
23 * VP8/9 decoder support via libvpx
26 #define VPX_CODEC_DISABLE_COMPAT 1
27 #include <vpx/vpx_decoder.h>
28 #include <vpx/vpx_frame_buffer.h>
29 #include <vpx/vp8dx.h>
31 #include "libavutil/common.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/intreadwrite.h"
40 typedef struct VPxDecoderContext {
41 struct vpx_codec_ctx decoder;
42 struct vpx_codec_ctx decoder_alpha;
45 int has_alpha_channel;
49 static int get_frame_buffer(void *priv, size_t min_size, vpx_codec_frame_buffer_t *fb)
51 VPxContext *ctx = priv;
54 if (min_size > ctx->pool_size) {
55 av_buffer_pool_uninit(&ctx->pool);
56 /* According to the libvpx docs the buffer must be zeroed out. */
57 ctx->pool = av_buffer_pool_init(min_size, av_buffer_allocz);
60 return AVERROR(ENOMEM);
62 ctx->pool_size = min_size;
65 buf = av_buffer_pool_get(ctx->pool);
67 return AVERROR(ENOMEM);
70 fb->size = ctx->pool_size;
76 static int release_frame_buffer(void *priv, vpx_codec_frame_buffer_t *fb)
78 AVBufferRef *buf = fb->priv;
79 av_buffer_unref(&buf);
83 static av_cold int vpx_init(AVCodecContext *avctx,
84 struct vpx_codec_ctx* decoder,
85 const struct vpx_codec_iface *iface)
87 struct vpx_codec_dec_cfg deccfg = {
88 .threads = FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), 16)
91 av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
92 av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
94 if (vpx_codec_dec_init(decoder, iface, &deccfg, 0) != VPX_CODEC_OK) {
95 const char *error = vpx_codec_error(decoder);
96 av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
98 return AVERROR(EINVAL);
101 if (avctx->codec_id == AV_CODEC_ID_VP9)
102 vpx_codec_set_frame_buffer_functions(decoder, get_frame_buffer, release_frame_buffer, avctx->priv_data);
107 // returns 0 on success, AVERROR_INVALIDDATA otherwise
108 static int set_pix_fmt(AVCodecContext *avctx, struct vpx_image *img,
109 int has_alpha_channel)
111 static const enum AVColorSpace colorspaces[8] = {
112 AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG, AVCOL_SPC_BT709, AVCOL_SPC_SMPTE170M,
113 AVCOL_SPC_SMPTE240M, AVCOL_SPC_BT2020_NCL, AVCOL_SPC_RESERVED, AVCOL_SPC_RGB,
115 #if VPX_IMAGE_ABI_VERSION >= 4
116 static const enum AVColorRange color_ranges[] = {
117 AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG
119 avctx->color_range = color_ranges[img->range];
121 avctx->colorspace = colorspaces[img->cs];
122 if (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
123 return AVERROR_INVALIDDATA;
125 case VPX_IMG_FMT_I420:
126 if (avctx->codec_id == AV_CODEC_ID_VP9)
127 avctx->profile = FF_PROFILE_VP9_0;
129 has_alpha_channel ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
131 #if CONFIG_LIBVPX_VP9_DECODER
132 case VPX_IMG_FMT_I422:
133 avctx->profile = FF_PROFILE_VP9_1;
134 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
136 case VPX_IMG_FMT_I440:
137 avctx->profile = FF_PROFILE_VP9_1;
138 avctx->pix_fmt = AV_PIX_FMT_YUV440P;
140 case VPX_IMG_FMT_I444:
141 avctx->profile = FF_PROFILE_VP9_1;
142 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
143 AV_PIX_FMT_GBRP : AV_PIX_FMT_YUV444P;
145 case VPX_IMG_FMT_I42016:
146 avctx->profile = FF_PROFILE_VP9_2;
147 if (img->bit_depth == 10) {
148 avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
150 } else if (img->bit_depth == 12) {
151 avctx->pix_fmt = AV_PIX_FMT_YUV420P12;
154 return AVERROR_INVALIDDATA;
156 case VPX_IMG_FMT_I42216:
157 avctx->profile = FF_PROFILE_VP9_3;
158 if (img->bit_depth == 10) {
159 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
161 } else if (img->bit_depth == 12) {
162 avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
165 return AVERROR_INVALIDDATA;
167 case VPX_IMG_FMT_I44016:
168 avctx->profile = FF_PROFILE_VP9_3;
169 if (img->bit_depth == 10) {
170 avctx->pix_fmt = AV_PIX_FMT_YUV440P10;
172 } else if (img->bit_depth == 12) {
173 avctx->pix_fmt = AV_PIX_FMT_YUV440P12;
176 return AVERROR_INVALIDDATA;
178 case VPX_IMG_FMT_I44416:
179 avctx->profile = FF_PROFILE_VP9_3;
180 if (img->bit_depth == 10) {
181 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
182 AV_PIX_FMT_GBRP10 : AV_PIX_FMT_YUV444P10;
184 } else if (img->bit_depth == 12) {
185 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
186 AV_PIX_FMT_GBRP12 : AV_PIX_FMT_YUV444P12;
189 return AVERROR_INVALIDDATA;
193 return AVERROR_INVALIDDATA;
197 static int decode_frame(AVCodecContext *avctx, vpx_codec_ctx_t *decoder,
198 uint8_t *data, uint32_t data_sz)
200 if (vpx_codec_decode(decoder, data, data_sz, NULL, 0) != VPX_CODEC_OK) {
201 const char *error = vpx_codec_error(decoder);
202 const char *detail = vpx_codec_error_detail(decoder);
204 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
206 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
209 return AVERROR_INVALIDDATA;
214 static int vpx_decode(AVCodecContext *avctx,
215 void *data, int *got_frame, AVPacket *avpkt)
217 VPxContext *ctx = avctx->priv_data;
218 AVFrame *picture = data;
219 const void *iter = NULL;
220 const void *iter_alpha = NULL;
221 struct vpx_image *img, *img_alpha;
223 uint8_t *side_data = NULL;
226 ret = decode_frame(avctx, &ctx->decoder, avpkt->data, avpkt->size);
230 side_data = av_packet_get_side_data(avpkt,
231 AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
233 if (side_data_size >= 8) {
234 const uint64_t additional_id = AV_RB64(side_data);
237 if (additional_id == 1) { // 1 stands for alpha channel data.
238 if (!ctx->has_alpha_channel) {
239 ctx->has_alpha_channel = 1;
240 ret = vpx_init(avctx,
242 #if CONFIG_LIBVPX_VP8_DECODER && CONFIG_LIBVPX_VP9_DECODER
243 (avctx->codec_id == AV_CODEC_ID_VP8) ?
244 &vpx_codec_vp8_dx_algo : &vpx_codec_vp9_dx_algo
245 #elif CONFIG_LIBVPX_VP8_DECODER
246 &vpx_codec_vp8_dx_algo
248 &vpx_codec_vp9_dx_algo
254 ret = decode_frame(avctx, &ctx->decoder_alpha, side_data,
261 if ((img = vpx_codec_get_frame(&ctx->decoder, &iter)) &&
262 (!ctx->has_alpha_channel ||
263 (img_alpha = vpx_codec_get_frame(&ctx->decoder_alpha, &iter_alpha)))) {
267 if (img->d_w > img->w || img->d_h > img->h) {
268 av_log(avctx, AV_LOG_ERROR, "Display dimensions %dx%d exceed storage %dx%d\n",
269 img->d_w, img->d_h, img->w, img->h);
270 return AVERROR_EXTERNAL;
273 if ((ret = set_pix_fmt(avctx, img, ctx->has_alpha_channel)) < 0) {
274 av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
275 img->fmt, img->bit_depth);
279 if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
280 av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
281 avctx->width, avctx->height, img->d_w, img->d_h);
282 ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
287 if (ctx->has_alpha_channel &&
288 (img->d_w != img_alpha->d_w ||
289 img->d_h != img_alpha->d_h ||
290 img->bit_depth != img_alpha->bit_depth)) {
291 av_log(avctx, AV_LOG_ERROR,
292 "Video dimensions %dx%d@%dbpc differ from alpha dimensions %dx%d@%dbpc\n",
293 img->d_w, img->d_h, img->bit_depth,
294 img_alpha->d_w, img_alpha->d_h, img_alpha->bit_depth);
295 return AVERROR_INVALIDDATA;
298 planes[0] = img->planes[VPX_PLANE_Y];
299 planes[1] = img->planes[VPX_PLANE_U];
300 planes[2] = img->planes[VPX_PLANE_V];
302 ctx->has_alpha_channel ? img_alpha->planes[VPX_PLANE_Y] : NULL;
303 linesizes[0] = img->stride[VPX_PLANE_Y];
304 linesizes[1] = img->stride[VPX_PLANE_U];
305 linesizes[2] = img->stride[VPX_PLANE_V];
307 ctx->has_alpha_channel ? img_alpha->stride[VPX_PLANE_Y] : 0;
309 if (img->fb_priv && (!ctx->has_alpha_channel || img_alpha->fb_priv)) {
310 ret = ff_decode_frame_props(avctx, picture);
313 picture->buf[0] = av_buffer_ref(img->fb_priv);
314 if (!picture->buf[0])
315 return AVERROR(ENOMEM);
316 if (ctx->has_alpha_channel) {
317 picture->buf[1] = av_buffer_ref(img_alpha->fb_priv);
318 if (!picture->buf[1]) {
319 av_frame_unref(picture);
320 return AVERROR(ENOMEM);
323 for (int i = 0; i < 4; i++) {
324 picture->data[i] = planes[i];
325 picture->linesize[i] = linesizes[i];
328 if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
330 av_image_copy(picture->data, picture->linesize, (const uint8_t**)planes,
331 linesizes, avctx->pix_fmt, img->d_w, img->d_h);
338 static av_cold int vpx_free(AVCodecContext *avctx)
340 VPxContext *ctx = avctx->priv_data;
341 vpx_codec_destroy(&ctx->decoder);
342 if (ctx->has_alpha_channel)
343 vpx_codec_destroy(&ctx->decoder_alpha);
344 av_buffer_pool_uninit(&ctx->pool);
348 #if CONFIG_LIBVPX_VP8_DECODER
349 static av_cold int vp8_init(AVCodecContext *avctx)
351 VPxContext *ctx = avctx->priv_data;
352 return vpx_init(avctx, &ctx->decoder, &vpx_codec_vp8_dx_algo);
355 AVCodec ff_libvpx_vp8_decoder = {
357 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
358 .type = AVMEDIA_TYPE_VIDEO,
359 .id = AV_CODEC_ID_VP8,
360 .priv_data_size = sizeof(VPxContext),
363 .decode = vpx_decode,
364 .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
365 .wrapper_name = "libvpx",
367 #endif /* CONFIG_LIBVPX_VP8_DECODER */
369 #if CONFIG_LIBVPX_VP9_DECODER
370 static av_cold int vp9_init(AVCodecContext *avctx)
372 VPxContext *ctx = avctx->priv_data;
373 return vpx_init(avctx, &ctx->decoder, &vpx_codec_vp9_dx_algo);
376 AVCodec ff_libvpx_vp9_decoder = {
377 .name = "libvpx-vp9",
378 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
379 .type = AVMEDIA_TYPE_VIDEO,
380 .id = AV_CODEC_ID_VP9,
381 .priv_data_size = sizeof(VPxContext),
384 .decode = vpx_decode,
385 .capabilities = AV_CODEC_CAP_AUTO_THREADS,
386 .init_static_data = ff_vp9_init_static,
387 .profiles = NULL_IF_CONFIG_SMALL(ff_vp9_profiles),
388 .wrapper_name = "libvpx",
390 #endif /* CONFIG_LIBVPX_VP9_DECODER */