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"
39 typedef struct VPxDecoderContext {
40 struct vpx_codec_ctx decoder;
41 struct vpx_codec_ctx decoder_alpha;
44 int has_alpha_channel;
48 static int get_frame_buffer(void *priv, size_t min_size, vpx_codec_frame_buffer_t *fb)
50 VPxContext *ctx = priv;
53 if (min_size > ctx->pool_size) {
54 av_buffer_pool_uninit(&ctx->pool);
55 /* According to the libvpx docs the buffer must be zeroed out. */
56 ctx->pool = av_buffer_pool_init(min_size, av_buffer_allocz);
59 return AVERROR(ENOMEM);
61 ctx->pool_size = min_size;
64 buf = av_buffer_pool_get(ctx->pool);
66 return AVERROR(ENOMEM);
69 fb->size = ctx->pool_size;
75 static int release_frame_buffer(void *priv, vpx_codec_frame_buffer_t *fb)
77 AVBufferRef *buf = fb->priv;
78 av_buffer_unref(&buf);
82 static av_cold int vpx_init(AVCodecContext *avctx,
83 struct vpx_codec_ctx* decoder,
84 const struct vpx_codec_iface *iface)
86 struct vpx_codec_dec_cfg deccfg = {
87 .threads = FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), 16)
90 av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
91 av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
93 if (vpx_codec_dec_init(decoder, iface, &deccfg, 0) != VPX_CODEC_OK) {
94 const char *error = vpx_codec_error(decoder);
95 av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
97 return AVERROR(EINVAL);
100 if (avctx->codec_id == AV_CODEC_ID_VP9)
101 vpx_codec_set_frame_buffer_functions(decoder, get_frame_buffer, release_frame_buffer, avctx->priv_data);
106 // returns 0 on success, AVERROR_INVALIDDATA otherwise
107 static int set_pix_fmt(AVCodecContext *avctx, struct vpx_image *img,
108 int has_alpha_channel)
110 static const enum AVColorSpace colorspaces[8] = {
111 AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG, AVCOL_SPC_BT709, AVCOL_SPC_SMPTE170M,
112 AVCOL_SPC_SMPTE240M, AVCOL_SPC_BT2020_NCL, AVCOL_SPC_RESERVED, AVCOL_SPC_RGB,
114 #if VPX_IMAGE_ABI_VERSION >= 4
115 static const enum AVColorRange color_ranges[] = {
116 AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG
118 avctx->color_range = color_ranges[img->range];
120 avctx->colorspace = colorspaces[img->cs];
121 if (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
122 return AVERROR_INVALIDDATA;
124 case VPX_IMG_FMT_I420:
125 if (avctx->codec_id == AV_CODEC_ID_VP9)
126 avctx->profile = FF_PROFILE_VP9_0;
128 has_alpha_channel ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
130 #if CONFIG_LIBVPX_VP9_DECODER
131 case VPX_IMG_FMT_I422:
132 avctx->profile = FF_PROFILE_VP9_1;
133 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
135 case VPX_IMG_FMT_I440:
136 avctx->profile = FF_PROFILE_VP9_1;
137 avctx->pix_fmt = AV_PIX_FMT_YUV440P;
139 case VPX_IMG_FMT_I444:
140 avctx->profile = FF_PROFILE_VP9_1;
141 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
142 AV_PIX_FMT_GBRP : AV_PIX_FMT_YUV444P;
144 case VPX_IMG_FMT_I42016:
145 avctx->profile = FF_PROFILE_VP9_2;
146 if (img->bit_depth == 10) {
147 avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
149 } else if (img->bit_depth == 12) {
150 avctx->pix_fmt = AV_PIX_FMT_YUV420P12;
153 return AVERROR_INVALIDDATA;
155 case VPX_IMG_FMT_I42216:
156 avctx->profile = FF_PROFILE_VP9_3;
157 if (img->bit_depth == 10) {
158 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
160 } else if (img->bit_depth == 12) {
161 avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
164 return AVERROR_INVALIDDATA;
166 case VPX_IMG_FMT_I44016:
167 avctx->profile = FF_PROFILE_VP9_3;
168 if (img->bit_depth == 10) {
169 avctx->pix_fmt = AV_PIX_FMT_YUV440P10;
171 } else if (img->bit_depth == 12) {
172 avctx->pix_fmt = AV_PIX_FMT_YUV440P12;
175 return AVERROR_INVALIDDATA;
177 case VPX_IMG_FMT_I44416:
178 avctx->profile = FF_PROFILE_VP9_3;
179 if (img->bit_depth == 10) {
180 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
181 AV_PIX_FMT_GBRP10 : AV_PIX_FMT_YUV444P10;
183 } else if (img->bit_depth == 12) {
184 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
185 AV_PIX_FMT_GBRP12 : AV_PIX_FMT_YUV444P12;
188 return AVERROR_INVALIDDATA;
192 return AVERROR_INVALIDDATA;
196 static int decode_frame(AVCodecContext *avctx, vpx_codec_ctx_t *decoder,
197 uint8_t *data, uint32_t data_sz)
199 if (vpx_codec_decode(decoder, data, data_sz, NULL, 0) != VPX_CODEC_OK) {
200 const char *error = vpx_codec_error(decoder);
201 const char *detail = vpx_codec_error_detail(decoder);
203 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
205 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
208 return AVERROR_INVALIDDATA;
213 static int vpx_decode(AVCodecContext *avctx,
214 void *data, int *got_frame, AVPacket *avpkt)
216 VPxContext *ctx = avctx->priv_data;
217 AVFrame *picture = data;
218 const void *iter = NULL;
219 const void *iter_alpha = NULL;
220 struct vpx_image *img, *img_alpha;
222 uint8_t *side_data = NULL;
223 int side_data_size = 0;
225 ret = decode_frame(avctx, &ctx->decoder, avpkt->data, avpkt->size);
229 side_data = av_packet_get_side_data(avpkt,
230 AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
232 if (side_data_size >= 8) {
233 const uint64_t additional_id = AV_RB64(side_data);
236 if (additional_id == 1) { // 1 stands for alpha channel data.
237 if (!ctx->has_alpha_channel) {
238 ctx->has_alpha_channel = 1;
239 ret = vpx_init(avctx,
241 #if CONFIG_LIBVPX_VP8_DECODER && CONFIG_LIBVPX_VP9_DECODER
242 (avctx->codec_id == AV_CODEC_ID_VP8) ?
243 &vpx_codec_vp8_dx_algo : &vpx_codec_vp9_dx_algo
244 #elif CONFIG_LIBVPX_VP8_DECODER
245 &vpx_codec_vp8_dx_algo
247 &vpx_codec_vp9_dx_algo
253 ret = decode_frame(avctx, &ctx->decoder_alpha, side_data,
260 if ((img = vpx_codec_get_frame(&ctx->decoder, &iter)) &&
261 (!ctx->has_alpha_channel ||
262 (img_alpha = vpx_codec_get_frame(&ctx->decoder_alpha, &iter_alpha)))) {
266 if (img->d_w > img->w || img->d_h > img->h) {
267 av_log(avctx, AV_LOG_ERROR, "Display dimensions %dx%d exceed storage %dx%d\n",
268 img->d_w, img->d_h, img->w, img->h);
269 return AVERROR_EXTERNAL;
272 if ((ret = set_pix_fmt(avctx, img, ctx->has_alpha_channel)) < 0) {
273 av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
274 img->fmt, img->bit_depth);
278 if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
279 av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
280 avctx->width, avctx->height, img->d_w, img->d_h);
281 ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
286 if (ctx->has_alpha_channel &&
287 (img->d_w != img_alpha->d_w ||
288 img->d_h != img_alpha->d_h ||
289 img->bit_depth != img_alpha->bit_depth)) {
290 av_log(avctx, AV_LOG_ERROR,
291 "Video dimensions %dx%d@%dbpc differ from alpha dimensions %dx%d@%dbpc\n",
292 img->d_w, img->d_h, img->bit_depth,
293 img_alpha->d_w, img_alpha->d_h, img_alpha->bit_depth);
294 return AVERROR_INVALIDDATA;
297 planes[0] = img->planes[VPX_PLANE_Y];
298 planes[1] = img->planes[VPX_PLANE_U];
299 planes[2] = img->planes[VPX_PLANE_V];
301 ctx->has_alpha_channel ? img_alpha->planes[VPX_PLANE_Y] : NULL;
302 linesizes[0] = img->stride[VPX_PLANE_Y];
303 linesizes[1] = img->stride[VPX_PLANE_U];
304 linesizes[2] = img->stride[VPX_PLANE_V];
306 ctx->has_alpha_channel ? img_alpha->stride[VPX_PLANE_Y] : 0;
308 if (img->fb_priv && (!ctx->has_alpha_channel || img_alpha->fb_priv)) {
309 ret = ff_decode_frame_props(avctx, picture);
312 picture->buf[0] = av_buffer_ref(img->fb_priv);
313 if (!picture->buf[0])
314 return AVERROR(ENOMEM);
315 if (ctx->has_alpha_channel) {
316 picture->buf[1] = av_buffer_ref(img_alpha->fb_priv);
317 if (!picture->buf[1]) {
318 av_frame_unref(picture);
319 return AVERROR(ENOMEM);
322 for (int i = 0; i < 4; i++) {
323 picture->data[i] = planes[i];
324 picture->linesize[i] = linesizes[i];
327 if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
329 av_image_copy(picture->data, picture->linesize, (const uint8_t**)planes,
330 linesizes, avctx->pix_fmt, img->d_w, img->d_h);
337 static av_cold int vpx_free(AVCodecContext *avctx)
339 VPxContext *ctx = avctx->priv_data;
340 vpx_codec_destroy(&ctx->decoder);
341 if (ctx->has_alpha_channel)
342 vpx_codec_destroy(&ctx->decoder_alpha);
343 av_buffer_pool_uninit(&ctx->pool);
347 #if CONFIG_LIBVPX_VP8_DECODER
348 static av_cold int vp8_init(AVCodecContext *avctx)
350 VPxContext *ctx = avctx->priv_data;
351 return vpx_init(avctx, &ctx->decoder, &vpx_codec_vp8_dx_algo);
354 AVCodec ff_libvpx_vp8_decoder = {
356 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
357 .type = AVMEDIA_TYPE_VIDEO,
358 .id = AV_CODEC_ID_VP8,
359 .priv_data_size = sizeof(VPxContext),
362 .decode = vpx_decode,
363 .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
364 .wrapper_name = "libvpx",
366 #endif /* CONFIG_LIBVPX_VP8_DECODER */
368 #if CONFIG_LIBVPX_VP9_DECODER
369 static av_cold int vp9_init(AVCodecContext *avctx)
371 VPxContext *ctx = avctx->priv_data;
372 return vpx_init(avctx, &ctx->decoder, &vpx_codec_vp9_dx_algo);
375 AVCodec ff_libvpx_vp9_decoder = {
376 .name = "libvpx-vp9",
377 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
378 .type = AVMEDIA_TYPE_VIDEO,
379 .id = AV_CODEC_ID_VP9,
380 .priv_data_size = sizeof(VPxContext),
383 .decode = vpx_decode,
384 .capabilities = AV_CODEC_CAP_AUTO_THREADS,
385 .init_static_data = ff_vp9_init_static,
386 .profiles = NULL_IF_CONFIG_SMALL(ff_vp9_profiles),
387 .wrapper_name = "libvpx",
389 #endif /* CONFIG_LIBVPX_VP9_DECODER */