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 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"
37 typedef struct VP8DecoderContext {
38 struct vpx_codec_ctx decoder;
41 static av_cold int vpx_init(AVCodecContext *avctx,
42 const struct vpx_codec_iface *iface)
44 VP8Context *ctx = avctx->priv_data;
45 struct vpx_codec_dec_cfg deccfg = {
46 /* token partitions+1 would be a decent choice */
47 .threads = FFMIN(avctx->thread_count, 16)
50 av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
51 av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
53 if (vpx_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != VPX_CODEC_OK) {
54 const char *error = vpx_codec_error(&ctx->decoder);
55 av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
57 return AVERROR(EINVAL);
63 // returns 0 on success, AVERROR_INVALIDDATA otherwise
64 static int set_pix_fmt(AVCodecContext *avctx, struct vpx_image *img)
66 #if VPX_IMAGE_ABI_VERSION >= 3
67 static const enum AVColorSpace colorspaces[8] = {
68 AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG, AVCOL_SPC_BT709, AVCOL_SPC_SMPTE170M,
69 AVCOL_SPC_SMPTE240M, AVCOL_SPC_BT2020_NCL, AVCOL_SPC_RESERVED, AVCOL_SPC_RGB,
71 avctx->colorspace = colorspaces[img->cs];
73 if (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
74 return AVERROR_INVALIDDATA;
76 case VPX_IMG_FMT_I420:
77 if (avctx->codec_id == AV_CODEC_ID_VP9)
78 avctx->profile = FF_PROFILE_VP9_0;
79 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
81 #if CONFIG_LIBVPX_VP9_DECODER
82 case VPX_IMG_FMT_I422:
83 avctx->profile = FF_PROFILE_VP9_1;
84 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
86 #if VPX_IMAGE_ABI_VERSION >= 3
87 case VPX_IMG_FMT_I440:
88 avctx->profile = FF_PROFILE_VP9_1;
89 avctx->pix_fmt = AV_PIX_FMT_YUV440P;
92 case VPX_IMG_FMT_I444:
93 avctx->profile = FF_PROFILE_VP9_1;
94 #if VPX_IMAGE_ABI_VERSION >= 3
95 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
96 AV_PIX_FMT_GBRP : AV_PIX_FMT_YUV444P;
98 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
101 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
102 case VPX_IMG_FMT_I42016:
103 avctx->profile = FF_PROFILE_VP9_2;
104 if (img->bit_depth == 10) {
105 avctx->pix_fmt = AV_PIX_FMT_YUV420P10LE;
107 } else if (img->bit_depth == 12) {
108 avctx->pix_fmt = AV_PIX_FMT_YUV420P12LE;
111 return AVERROR_INVALIDDATA;
113 case VPX_IMG_FMT_I42216:
114 avctx->profile = FF_PROFILE_VP9_3;
115 if (img->bit_depth == 10) {
116 avctx->pix_fmt = AV_PIX_FMT_YUV422P10LE;
118 } else if (img->bit_depth == 12) {
119 avctx->pix_fmt = AV_PIX_FMT_YUV422P12LE;
122 return AVERROR_INVALIDDATA;
124 #if VPX_IMAGE_ABI_VERSION >= 3
125 case VPX_IMG_FMT_I44016:
126 avctx->profile = FF_PROFILE_VP9_3;
127 if (img->bit_depth == 10) {
128 avctx->pix_fmt = AV_PIX_FMT_YUV440P10LE;
130 } else if (img->bit_depth == 12) {
131 avctx->pix_fmt = AV_PIX_FMT_YUV440P12LE;
134 return AVERROR_INVALIDDATA;
137 case VPX_IMG_FMT_I44416:
138 avctx->profile = FF_PROFILE_VP9_3;
139 if (img->bit_depth == 10) {
140 #if VPX_IMAGE_ABI_VERSION >= 3
141 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
142 AV_PIX_FMT_GBRP10LE : AV_PIX_FMT_YUV444P10LE;
144 avctx->pix_fmt = AV_PIX_FMT_YUV444P10LE;
147 } else if (img->bit_depth == 12) {
148 #if VPX_IMAGE_ABI_VERSION >= 3
149 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
150 AV_PIX_FMT_GBRP12LE : AV_PIX_FMT_YUV444P12LE;
152 avctx->pix_fmt = AV_PIX_FMT_YUV444P12LE;
156 return AVERROR_INVALIDDATA;
161 return AVERROR_INVALIDDATA;
165 static int vp8_decode(AVCodecContext *avctx,
166 void *data, int *got_frame, AVPacket *avpkt)
168 VP8Context *ctx = avctx->priv_data;
169 AVFrame *picture = data;
170 const void *iter = NULL;
171 struct vpx_image *img;
174 if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
176 const char *error = vpx_codec_error(&ctx->decoder);
177 const char *detail = vpx_codec_error_detail(&ctx->decoder);
179 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
181 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
183 return AVERROR_INVALIDDATA;
186 if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
187 if ((ret = set_pix_fmt(avctx, img)) < 0) {
188 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
189 av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
190 img->fmt, img->bit_depth);
192 av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
198 if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
199 av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
200 avctx->width, avctx->height, img->d_w, img->d_h);
201 ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
205 if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
207 av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
208 img->stride, avctx->pix_fmt, img->d_w, img->d_h);
214 static av_cold int vp8_free(AVCodecContext *avctx)
216 VP8Context *ctx = avctx->priv_data;
217 vpx_codec_destroy(&ctx->decoder);
221 #if CONFIG_LIBVPX_VP8_DECODER
222 static av_cold int vp8_init(AVCodecContext *avctx)
224 return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
227 AVCodec ff_libvpx_vp8_decoder = {
229 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
230 .type = AVMEDIA_TYPE_VIDEO,
231 .id = AV_CODEC_ID_VP8,
232 .priv_data_size = sizeof(VP8Context),
235 .decode = vp8_decode,
236 .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
238 #endif /* CONFIG_LIBVPX_VP8_DECODER */
240 #if CONFIG_LIBVPX_VP9_DECODER
241 static av_cold int vp9_init(AVCodecContext *avctx)
243 return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
246 AVCodec ff_libvpx_vp9_decoder = {
247 .name = "libvpx-vp9",
248 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
249 .type = AVMEDIA_TYPE_VIDEO,
250 .id = AV_CODEC_ID_VP9,
251 .priv_data_size = sizeof(VP8Context),
254 .decode = vp8_decode,
255 .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
256 .init_static_data = ff_vp9_init_static,
257 .profiles = NULL_IF_CONFIG_SMALL(ff_vp9_profiles),
259 #endif /* CONFIG_LIBVPX_VP9_DECODER */