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/vp8dx.h>
30 #include "libavutil/common.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/intreadwrite.h"
38 typedef struct VPxDecoderContext {
39 struct vpx_codec_ctx decoder;
40 struct vpx_codec_ctx decoder_alpha;
41 int has_alpha_channel;
44 static av_cold int vpx_init(AVCodecContext *avctx,
45 const struct vpx_codec_iface *iface,
48 VPxContext *ctx = avctx->priv_data;
49 struct vpx_codec_dec_cfg deccfg = {
50 /* token partitions+1 would be a decent choice */
51 .threads = FFMIN(avctx->thread_count, 16)
54 av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
55 av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
57 if (vpx_codec_dec_init(
58 is_alpha_decoder ? &ctx->decoder_alpha : &ctx->decoder,
59 iface, &deccfg, 0) != VPX_CODEC_OK) {
60 const char *error = vpx_codec_error(&ctx->decoder);
61 av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
63 return AVERROR(EINVAL);
69 // returns 0 on success, AVERROR_INVALIDDATA otherwise
70 static int set_pix_fmt(AVCodecContext *avctx, struct vpx_image *img,
71 int has_alpha_channel)
73 #if VPX_IMAGE_ABI_VERSION >= 3
74 static const enum AVColorSpace colorspaces[8] = {
75 AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG, AVCOL_SPC_BT709, AVCOL_SPC_SMPTE170M,
76 AVCOL_SPC_SMPTE240M, AVCOL_SPC_BT2020_NCL, AVCOL_SPC_RESERVED, AVCOL_SPC_RGB,
78 #if VPX_IMAGE_ABI_VERSION >= 4
79 static const enum AVColorRange color_ranges[] = {
80 AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG
82 avctx->color_range = color_ranges[img->range];
84 avctx->colorspace = colorspaces[img->cs];
86 if (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
87 return AVERROR_INVALIDDATA;
89 case VPX_IMG_FMT_I420:
90 if (avctx->codec_id == AV_CODEC_ID_VP9)
91 avctx->profile = FF_PROFILE_VP9_0;
93 has_alpha_channel ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
95 #if CONFIG_LIBVPX_VP9_DECODER
96 case VPX_IMG_FMT_I422:
97 avctx->profile = FF_PROFILE_VP9_1;
98 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
100 #if VPX_IMAGE_ABI_VERSION >= 3
101 case VPX_IMG_FMT_I440:
102 avctx->profile = FF_PROFILE_VP9_1;
103 avctx->pix_fmt = AV_PIX_FMT_YUV440P;
106 case VPX_IMG_FMT_I444:
107 avctx->profile = FF_PROFILE_VP9_1;
108 #if VPX_IMAGE_ABI_VERSION >= 3
109 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
110 AV_PIX_FMT_GBRP : AV_PIX_FMT_YUV444P;
112 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
115 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
116 case VPX_IMG_FMT_I42016:
117 avctx->profile = FF_PROFILE_VP9_2;
118 if (img->bit_depth == 10) {
119 avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
121 } else if (img->bit_depth == 12) {
122 avctx->pix_fmt = AV_PIX_FMT_YUV420P12;
125 return AVERROR_INVALIDDATA;
127 case VPX_IMG_FMT_I42216:
128 avctx->profile = FF_PROFILE_VP9_3;
129 if (img->bit_depth == 10) {
130 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
132 } else if (img->bit_depth == 12) {
133 avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
136 return AVERROR_INVALIDDATA;
138 #if VPX_IMAGE_ABI_VERSION >= 3
139 case VPX_IMG_FMT_I44016:
140 avctx->profile = FF_PROFILE_VP9_3;
141 if (img->bit_depth == 10) {
142 avctx->pix_fmt = AV_PIX_FMT_YUV440P10;
144 } else if (img->bit_depth == 12) {
145 avctx->pix_fmt = AV_PIX_FMT_YUV440P12;
148 return AVERROR_INVALIDDATA;
151 case VPX_IMG_FMT_I44416:
152 avctx->profile = FF_PROFILE_VP9_3;
153 if (img->bit_depth == 10) {
154 #if VPX_IMAGE_ABI_VERSION >= 3
155 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
156 AV_PIX_FMT_GBRP10 : AV_PIX_FMT_YUV444P10;
158 avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
161 } else if (img->bit_depth == 12) {
162 #if VPX_IMAGE_ABI_VERSION >= 3
163 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
164 AV_PIX_FMT_GBRP12 : AV_PIX_FMT_YUV444P12;
166 avctx->pix_fmt = AV_PIX_FMT_YUV444P12;
170 return AVERROR_INVALIDDATA;
175 return AVERROR_INVALIDDATA;
179 static int decode_frame(AVCodecContext *avctx, vpx_codec_ctx_t *decoder,
180 uint8_t *data, uint32_t data_sz)
182 if (vpx_codec_decode(decoder, data, data_sz, NULL, 0) != VPX_CODEC_OK) {
183 const char *error = vpx_codec_error(decoder);
184 const char *detail = vpx_codec_error_detail(decoder);
186 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
188 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
191 return AVERROR_INVALIDDATA;
196 static int vpx_decode(AVCodecContext *avctx,
197 void *data, int *got_frame, AVPacket *avpkt)
199 VPxContext *ctx = avctx->priv_data;
200 AVFrame *picture = data;
201 const void *iter = NULL;
202 const void *iter_alpha = NULL;
203 struct vpx_image *img, *img_alpha;
205 uint8_t *side_data = NULL;
206 int side_data_size = 0;
208 ret = decode_frame(avctx, &ctx->decoder, avpkt->data, avpkt->size);
212 side_data = av_packet_get_side_data(avpkt,
213 AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
215 if (side_data_size > 1) {
216 const uint64_t additional_id = AV_RB64(side_data);
219 if (additional_id == 1) { // 1 stands for alpha channel data.
220 if (!ctx->has_alpha_channel) {
221 ctx->has_alpha_channel = 1;
222 ret = vpx_init(avctx,
223 #if CONFIG_LIBVPX_VP8_DECODER && CONFIG_LIBVPX_VP9_DECODER
224 (avctx->codec_id == AV_CODEC_ID_VP8) ?
225 &vpx_codec_vp8_dx_algo : &vpx_codec_vp9_dx_algo,
226 #elif CONFIG_LIBVPX_VP8_DECODER
227 &vpx_codec_vp8_dx_algo,
229 &vpx_codec_vp9_dx_algo,
235 ret = decode_frame(avctx, &ctx->decoder_alpha, side_data,
242 if ((img = vpx_codec_get_frame(&ctx->decoder, &iter)) &&
243 (!ctx->has_alpha_channel ||
244 (img_alpha = vpx_codec_get_frame(&ctx->decoder_alpha, &iter_alpha)))) {
247 if ((ret = set_pix_fmt(avctx, img, ctx->has_alpha_channel)) < 0) {
248 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
249 av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
250 img->fmt, img->bit_depth);
252 av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
258 if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
259 av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
260 avctx->width, avctx->height, img->d_w, img->d_h);
261 ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
265 if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
268 planes[0] = img->planes[VPX_PLANE_Y];
269 planes[1] = img->planes[VPX_PLANE_U];
270 planes[2] = img->planes[VPX_PLANE_V];
272 ctx->has_alpha_channel ? img_alpha->planes[VPX_PLANE_Y] : NULL;
273 linesizes[0] = img->stride[VPX_PLANE_Y];
274 linesizes[1] = img->stride[VPX_PLANE_U];
275 linesizes[2] = img->stride[VPX_PLANE_V];
277 ctx->has_alpha_channel ? img_alpha->stride[VPX_PLANE_Y] : 0;
278 av_image_copy(picture->data, picture->linesize, (const uint8_t**)planes,
279 linesizes, avctx->pix_fmt, img->d_w, img->d_h);
285 static av_cold int vpx_free(AVCodecContext *avctx)
287 VPxContext *ctx = avctx->priv_data;
288 vpx_codec_destroy(&ctx->decoder);
289 if (ctx->has_alpha_channel)
290 vpx_codec_destroy(&ctx->decoder_alpha);
294 #if CONFIG_LIBVPX_VP8_DECODER
295 static av_cold int vp8_init(AVCodecContext *avctx)
297 return vpx_init(avctx, &vpx_codec_vp8_dx_algo, 0);
300 AVCodec ff_libvpx_vp8_decoder = {
302 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
303 .type = AVMEDIA_TYPE_VIDEO,
304 .id = AV_CODEC_ID_VP8,
305 .priv_data_size = sizeof(VPxContext),
308 .decode = vpx_decode,
309 .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
311 #endif /* CONFIG_LIBVPX_VP8_DECODER */
313 #if CONFIG_LIBVPX_VP9_DECODER
314 static av_cold int vp9_init(AVCodecContext *avctx)
316 return vpx_init(avctx, &vpx_codec_vp9_dx_algo, 0);
319 AVCodec ff_libvpx_vp9_decoder = {
320 .name = "libvpx-vp9",
321 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
322 .type = AVMEDIA_TYPE_VIDEO,
323 .id = AV_CODEC_ID_VP9,
324 .priv_data_size = sizeof(VPxContext),
327 .decode = vpx_decode,
328 .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
329 .init_static_data = ff_vp9_init_static,
330 .profiles = NULL_IF_CONFIG_SMALL(ff_vp9_profiles),
332 #endif /* CONFIG_LIBVPX_VP9_DECODER */