2 * Copyright (c) 2010, Google, Inc.
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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"
36 typedef struct VP8DecoderContext
{
37 struct vpx_codec_ctx decoder
;
40 static av_cold
int vpx_init(AVCodecContext
*avctx
,
41 const struct vpx_codec_iface
*iface
)
43 VP8Context
*ctx
= avctx
->priv_data
;
44 struct vpx_codec_dec_cfg deccfg
= {
45 /* token partitions+1 would be a decent choice */
46 .threads
= FFMIN(avctx
->thread_count
, 16)
49 av_log(avctx
, AV_LOG_INFO
, "%s\n", vpx_codec_version_str());
50 av_log(avctx
, AV_LOG_VERBOSE
, "%s\n", vpx_codec_build_config());
52 if (vpx_codec_dec_init(&ctx
->decoder
, iface
, &deccfg
, 0) != VPX_CODEC_OK
) {
53 const char *error
= vpx_codec_error(&ctx
->decoder
);
54 av_log(avctx
, AV_LOG_ERROR
, "Failed to initialize decoder: %s\n",
56 return AVERROR(EINVAL
);
62 static int vp8_decode(AVCodecContext
*avctx
,
63 void *data
, int *got_frame
, AVPacket
*avpkt
)
65 VP8Context
*ctx
= avctx
->priv_data
;
66 AVFrame
*picture
= data
;
67 const void *iter
= NULL
;
68 struct vpx_image
*img
;
71 if (vpx_codec_decode(&ctx
->decoder
, avpkt
->data
, avpkt
->size
, NULL
, 0) !=
73 const char *error
= vpx_codec_error(&ctx
->decoder
);
74 const char *detail
= vpx_codec_error_detail(&ctx
->decoder
);
76 av_log(avctx
, AV_LOG_ERROR
, "Failed to decode frame: %s\n", error
);
78 av_log(avctx
, AV_LOG_ERROR
, " Additional information: %s\n",
80 return AVERROR_INVALIDDATA
;
83 if ((img
= vpx_codec_get_frame(&ctx
->decoder
, &iter
))) {
84 avctx
->pix_fmt
= ff_vpx_imgfmt_to_pixfmt(img
->fmt
);
85 if (avctx
->pix_fmt
== AV_PIX_FMT_NONE
) {
86 av_log(avctx
, AV_LOG_ERROR
, "Unsupported output colorspace (%d)\n",
88 return AVERROR_INVALIDDATA
;
91 if ((int) img
->d_w
!= avctx
->width
|| (int) img
->d_h
!= avctx
->height
) {
92 av_log(avctx
, AV_LOG_INFO
, "dimension change! %dx%d -> %dx%d\n",
93 avctx
->width
, avctx
->height
, img
->d_w
, img
->d_h
);
94 ret
= ff_set_dimensions(avctx
, img
->d_w
, img
->d_h
);
98 if ((ret
= ff_get_buffer(avctx
, picture
, 0)) < 0)
100 av_image_copy(picture
->data
, picture
->linesize
, (const uint8_t **) img
->planes
,
101 img
->stride
, avctx
->pix_fmt
, img
->d_w
, img
->d_h
);
102 #if VPX_IMAGE_ABI_VERSION >= 4
103 switch (img
->range
) {
104 case VPX_CR_STUDIO_RANGE
:
105 picture
->color_range
= AVCOL_RANGE_MPEG
;
107 case VPX_CR_FULL_RANGE
:
108 picture
->color_range
= AVCOL_RANGE_JPEG
;
117 static av_cold
int vp8_free(AVCodecContext
*avctx
)
119 VP8Context
*ctx
= avctx
->priv_data
;
120 vpx_codec_destroy(&ctx
->decoder
);
124 #if CONFIG_LIBVPX_VP8_DECODER
125 static av_cold
int vp8_init(AVCodecContext
*avctx
)
127 return vpx_init(avctx
, &vpx_codec_vp8_dx_algo
);
130 AVCodec ff_libvpx_vp8_decoder
= {
132 .long_name
= NULL_IF_CONFIG_SMALL("libvpx VP8"),
133 .type
= AVMEDIA_TYPE_VIDEO
,
134 .id
= AV_CODEC_ID_VP8
,
135 .priv_data_size
= sizeof(VP8Context
),
138 .decode
= vp8_decode
,
139 .capabilities
= AV_CODEC_CAP_AUTO_THREADS
| AV_CODEC_CAP_DR1
,
140 .wrapper_name
= "libvpx",
142 #endif /* CONFIG_LIBVPX_VP8_DECODER */
144 #if CONFIG_LIBVPX_VP9_DECODER
145 static av_cold
int vp9_init(AVCodecContext
*avctx
)
147 return vpx_init(avctx
, &vpx_codec_vp9_dx_algo
);
150 AVCodec ff_libvpx_vp9_decoder
= {
151 .name
= "libvpx-vp9",
152 .long_name
= NULL_IF_CONFIG_SMALL("libvpx VP9"),
153 .type
= AVMEDIA_TYPE_VIDEO
,
154 .id
= AV_CODEC_ID_VP9
,
155 .priv_data_size
= sizeof(VP8Context
),
158 .decode
= vp8_decode
,
159 .capabilities
= AV_CODEC_CAP_AUTO_THREADS
,
160 .wrapper_name
= "libvpx",
162 #endif /* CONFIG_LIBVPX_VP9_DECODER */