]> git.sesse.net Git - ffmpeg/blob - libavcodec/libvpxdec.c
Merge commit '69a68593ce5684409c3c4dd9a901bfd8b16925b1'
[ffmpeg] / libavcodec / libvpxdec.c
1 /*
2  * Copyright (c) 2010, Google, Inc.
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * VP8 decoder support via libvpx
24  */
25
26 #define VPX_CODEC_DISABLE_COMPAT 1
27 #include <vpx/vpx_decoder.h>
28 #include <vpx/vp8dx.h>
29
30 #include "libavutil/common.h"
31 #include "libavutil/imgutils.h"
32 #include "avcodec.h"
33 #include "internal.h"
34 #include "libvpx.h"
35 #include "profiles.h"
36
37 typedef struct VP8DecoderContext {
38     struct vpx_codec_ctx decoder;
39 } VP8Context;
40
41 static av_cold int vpx_init(AVCodecContext *avctx,
42                             const struct vpx_codec_iface *iface)
43 {
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)
48     };
49
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());
52
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",
56                error);
57         return AVERROR(EINVAL);
58     }
59
60     return 0;
61 }
62
63 // returns 0 on success, AVERROR_INVALIDDATA otherwise
64 static int set_pix_fmt(AVCodecContext *avctx, struct vpx_image *img)
65 {
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,
70     };
71     avctx->colorspace = colorspaces[img->cs];
72 #endif
73     if (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
74         return AVERROR_INVALIDDATA;
75     switch (img->fmt) {
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;
80         return 0;
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;
85         return 0;
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;
90         return 0;
91 #endif
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;
97 #else
98         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
99 #endif
100         return 0;
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;
106             return 0;
107         } else if (img->bit_depth == 12) {
108             avctx->pix_fmt = AV_PIX_FMT_YUV420P12LE;
109             return 0;
110         } else {
111             return AVERROR_INVALIDDATA;
112         }
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;
117             return 0;
118         } else if (img->bit_depth == 12) {
119             avctx->pix_fmt = AV_PIX_FMT_YUV422P12LE;
120             return 0;
121         } else {
122             return AVERROR_INVALIDDATA;
123         }
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;
129             return 0;
130         } else if (img->bit_depth == 12) {
131             avctx->pix_fmt = AV_PIX_FMT_YUV440P12LE;
132             return 0;
133         } else {
134             return AVERROR_INVALIDDATA;
135         }
136 #endif
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;
143 #else
144             avctx->pix_fmt = AV_PIX_FMT_YUV444P10LE;
145 #endif
146             return 0;
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;
151 #else
152             avctx->pix_fmt = AV_PIX_FMT_YUV444P12LE;
153 #endif
154             return 0;
155         } else {
156             return AVERROR_INVALIDDATA;
157         }
158 #endif
159 #endif
160     default:
161         return AVERROR_INVALIDDATA;
162     }
163 }
164
165 static int vp8_decode(AVCodecContext *avctx,
166                       void *data, int *got_frame, AVPacket *avpkt)
167 {
168     VP8Context *ctx = avctx->priv_data;
169     AVFrame *picture = data;
170     const void *iter = NULL;
171     struct vpx_image *img;
172     int ret;
173
174     if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
175         VPX_CODEC_OK) {
176         const char *error  = vpx_codec_error(&ctx->decoder);
177         const char *detail = vpx_codec_error_detail(&ctx->decoder);
178
179         av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
180         if (detail)
181             av_log(avctx, AV_LOG_ERROR, "  Additional information: %s\n",
182                    detail);
183         return AVERROR_INVALIDDATA;
184     }
185
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);
191 #else
192             av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
193                    img->fmt, 8);
194 #endif
195             return ret;
196         }
197
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);
202             if (ret < 0)
203                 return ret;
204         }
205         if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
206             return ret;
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);
209         *got_frame           = 1;
210     }
211     return avpkt->size;
212 }
213
214 static av_cold int vp8_free(AVCodecContext *avctx)
215 {
216     VP8Context *ctx = avctx->priv_data;
217     vpx_codec_destroy(&ctx->decoder);
218     return 0;
219 }
220
221 #if CONFIG_LIBVPX_VP8_DECODER
222 static av_cold int vp8_init(AVCodecContext *avctx)
223 {
224     return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
225 }
226
227 AVCodec ff_libvpx_vp8_decoder = {
228     .name           = "libvpx",
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),
233     .init           = vp8_init,
234     .close          = vp8_free,
235     .decode         = vp8_decode,
236     .capabilities   = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
237 };
238 #endif /* CONFIG_LIBVPX_VP8_DECODER */
239
240 #if CONFIG_LIBVPX_VP9_DECODER
241 static av_cold int vp9_init(AVCodecContext *avctx)
242 {
243     return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
244 }
245
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),
252     .init           = vp9_init,
253     .close          = vp8_free,
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),
258 };
259 #endif /* CONFIG_LIBVPX_VP9_DECODER */