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 #if VPX_IMAGE_ABI_VERSION >= 4
72 static const enum AVColorRange color_ranges[] = {
73 AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG
75 avctx->color_range = color_ranges[img->range];
77 avctx->colorspace = colorspaces[img->cs];
79 if (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
80 return AVERROR_INVALIDDATA;
82 case VPX_IMG_FMT_I420:
83 if (avctx->codec_id == AV_CODEC_ID_VP9)
84 avctx->profile = FF_PROFILE_VP9_0;
85 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
87 #if CONFIG_LIBVPX_VP9_DECODER
88 case VPX_IMG_FMT_I422:
89 avctx->profile = FF_PROFILE_VP9_1;
90 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
92 #if VPX_IMAGE_ABI_VERSION >= 3
93 case VPX_IMG_FMT_I440:
94 avctx->profile = FF_PROFILE_VP9_1;
95 avctx->pix_fmt = AV_PIX_FMT_YUV440P;
98 case VPX_IMG_FMT_I444:
99 avctx->profile = FF_PROFILE_VP9_1;
100 #if VPX_IMAGE_ABI_VERSION >= 3
101 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
102 AV_PIX_FMT_GBRP : AV_PIX_FMT_YUV444P;
104 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
107 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
108 case VPX_IMG_FMT_I42016:
109 avctx->profile = FF_PROFILE_VP9_2;
110 if (img->bit_depth == 10) {
111 avctx->pix_fmt = AV_PIX_FMT_YUV420P10LE;
113 } else if (img->bit_depth == 12) {
114 avctx->pix_fmt = AV_PIX_FMT_YUV420P12LE;
117 return AVERROR_INVALIDDATA;
119 case VPX_IMG_FMT_I42216:
120 avctx->profile = FF_PROFILE_VP9_3;
121 if (img->bit_depth == 10) {
122 avctx->pix_fmt = AV_PIX_FMT_YUV422P10LE;
124 } else if (img->bit_depth == 12) {
125 avctx->pix_fmt = AV_PIX_FMT_YUV422P12LE;
128 return AVERROR_INVALIDDATA;
130 #if VPX_IMAGE_ABI_VERSION >= 3
131 case VPX_IMG_FMT_I44016:
132 avctx->profile = FF_PROFILE_VP9_3;
133 if (img->bit_depth == 10) {
134 avctx->pix_fmt = AV_PIX_FMT_YUV440P10LE;
136 } else if (img->bit_depth == 12) {
137 avctx->pix_fmt = AV_PIX_FMT_YUV440P12LE;
140 return AVERROR_INVALIDDATA;
143 case VPX_IMG_FMT_I44416:
144 avctx->profile = FF_PROFILE_VP9_3;
145 if (img->bit_depth == 10) {
146 #if VPX_IMAGE_ABI_VERSION >= 3
147 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
148 AV_PIX_FMT_GBRP10LE : AV_PIX_FMT_YUV444P10LE;
150 avctx->pix_fmt = AV_PIX_FMT_YUV444P10LE;
153 } else if (img->bit_depth == 12) {
154 #if VPX_IMAGE_ABI_VERSION >= 3
155 avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
156 AV_PIX_FMT_GBRP12LE : AV_PIX_FMT_YUV444P12LE;
158 avctx->pix_fmt = AV_PIX_FMT_YUV444P12LE;
162 return AVERROR_INVALIDDATA;
167 return AVERROR_INVALIDDATA;
171 static int vp8_decode(AVCodecContext *avctx,
172 void *data, int *got_frame, AVPacket *avpkt)
174 VP8Context *ctx = avctx->priv_data;
175 AVFrame *picture = data;
176 const void *iter = NULL;
177 struct vpx_image *img;
180 if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
182 const char *error = vpx_codec_error(&ctx->decoder);
183 const char *detail = vpx_codec_error_detail(&ctx->decoder);
185 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
187 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
189 return AVERROR_INVALIDDATA;
192 if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
193 if ((ret = set_pix_fmt(avctx, img)) < 0) {
194 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
195 av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
196 img->fmt, img->bit_depth);
198 av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
204 if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
205 av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
206 avctx->width, avctx->height, img->d_w, img->d_h);
207 ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
211 if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
213 av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
214 img->stride, avctx->pix_fmt, img->d_w, img->d_h);
220 static av_cold int vp8_free(AVCodecContext *avctx)
222 VP8Context *ctx = avctx->priv_data;
223 vpx_codec_destroy(&ctx->decoder);
227 #if CONFIG_LIBVPX_VP8_DECODER
228 static av_cold int vp8_init(AVCodecContext *avctx)
230 return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
233 AVCodec ff_libvpx_vp8_decoder = {
235 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
236 .type = AVMEDIA_TYPE_VIDEO,
237 .id = AV_CODEC_ID_VP8,
238 .priv_data_size = sizeof(VP8Context),
241 .decode = vp8_decode,
242 .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
244 #endif /* CONFIG_LIBVPX_VP8_DECODER */
246 #if CONFIG_LIBVPX_VP9_DECODER
247 static av_cold int vp9_init(AVCodecContext *avctx)
249 return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
252 AVCodec ff_libvpx_vp9_decoder = {
253 .name = "libvpx-vp9",
254 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
255 .type = AVMEDIA_TYPE_VIDEO,
256 .id = AV_CODEC_ID_VP9,
257 .priv_data_size = sizeof(VP8Context),
260 .decode = vp8_decode,
261 .capabilities = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
262 .init_static_data = ff_vp9_init_static,
263 .profiles = NULL_IF_CONFIG_SMALL(ff_vp9_profiles),
265 #endif /* CONFIG_LIBVPX_VP9_DECODER */