]> git.sesse.net Git - ffmpeg/blob - libavcodec/libvpxdec.c
Merge commit 'd68705c9756e6558c8e28d90b4c364f25ba72083'
[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
36 typedef struct VP8DecoderContext {
37     struct vpx_codec_ctx decoder;
38 } VP8Context;
39
40 static av_cold int vpx_init(AVCodecContext *avctx,
41                             const struct vpx_codec_iface *iface)
42 {
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)
47     };
48
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());
51
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",
55                error);
56         return AVERROR(EINVAL);
57     }
58
59     return 0;
60 }
61
62 // returns 0 on success, AVERROR_INVALIDDATA otherwise
63 static int set_pix_fmt(AVCodecContext *avctx, struct vpx_image *img)
64 {
65 #if VPX_IMAGE_ABI_VERSION >= 3
66     static const enum AVColorSpace colorspaces[8] = {
67         AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG, AVCOL_SPC_BT709, AVCOL_SPC_SMPTE170M,
68         AVCOL_SPC_SMPTE240M, AVCOL_SPC_BT2020_NCL, AVCOL_SPC_RESERVED, AVCOL_SPC_RGB,
69     };
70     avctx->colorspace = colorspaces[img->cs];
71 #endif
72     if (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
73         return AVERROR_INVALIDDATA;
74     switch (img->fmt) {
75     case VPX_IMG_FMT_I420:
76         if (avctx->codec_id == AV_CODEC_ID_VP9)
77             avctx->profile = FF_PROFILE_VP9_0;
78         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
79         return 0;
80 #if CONFIG_LIBVPX_VP9_DECODER
81     case VPX_IMG_FMT_I422:
82         avctx->profile = FF_PROFILE_VP9_1;
83         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
84         return 0;
85 #if VPX_IMAGE_ABI_VERSION >= 3
86     case VPX_IMG_FMT_I440:
87         avctx->profile = FF_PROFILE_VP9_1;
88         avctx->pix_fmt = AV_PIX_FMT_YUV440P;
89         return 0;
90 #endif
91     case VPX_IMG_FMT_I444:
92         avctx->profile = FF_PROFILE_VP9_1;
93         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
94         return 0;
95 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
96     case VPX_IMG_FMT_I42016:
97         avctx->profile = FF_PROFILE_VP9_2;
98         if (img->bit_depth == 10) {
99             avctx->pix_fmt = AV_PIX_FMT_YUV420P10LE;
100             return 0;
101         } else if (img->bit_depth == 12) {
102             avctx->pix_fmt = AV_PIX_FMT_YUV420P12LE;
103             return 0;
104         } else {
105             return AVERROR_INVALIDDATA;
106         }
107     case VPX_IMG_FMT_I42216:
108         avctx->profile = FF_PROFILE_VP9_3;
109         if (img->bit_depth == 10) {
110             avctx->pix_fmt = AV_PIX_FMT_YUV422P10LE;
111             return 0;
112         } else if (img->bit_depth == 12) {
113             avctx->pix_fmt = AV_PIX_FMT_YUV422P12LE;
114             return 0;
115         } else {
116             return AVERROR_INVALIDDATA;
117         }
118 #if VPX_IMAGE_ABI_VERSION >= 3
119     case VPX_IMG_FMT_I44016:
120         avctx->profile = FF_PROFILE_VP9_3;
121         if (img->bit_depth == 10) {
122             avctx->pix_fmt = AV_PIX_FMT_YUV440P10LE;
123             return 0;
124         } else if (img->bit_depth == 12) {
125             avctx->pix_fmt = AV_PIX_FMT_YUV440P12LE;
126             return 0;
127         } else {
128             return AVERROR_INVALIDDATA;
129         }
130 #endif
131     case VPX_IMG_FMT_I44416:
132         avctx->profile = FF_PROFILE_VP9_3;
133         if (img->bit_depth == 10) {
134             avctx->pix_fmt = AV_PIX_FMT_YUV444P10LE;
135             return 0;
136         } else if (img->bit_depth == 12) {
137             avctx->pix_fmt = AV_PIX_FMT_YUV444P12LE;
138             return 0;
139         } else {
140             return AVERROR_INVALIDDATA;
141         }
142 #endif
143 #endif
144     default:
145         return AVERROR_INVALIDDATA;
146     }
147 }
148
149 static int vp8_decode(AVCodecContext *avctx,
150                       void *data, int *got_frame, AVPacket *avpkt)
151 {
152     VP8Context *ctx = avctx->priv_data;
153     AVFrame *picture = data;
154     const void *iter = NULL;
155     struct vpx_image *img;
156     int ret;
157
158     if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
159         VPX_CODEC_OK) {
160         const char *error  = vpx_codec_error(&ctx->decoder);
161         const char *detail = vpx_codec_error_detail(&ctx->decoder);
162
163         av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
164         if (detail)
165             av_log(avctx, AV_LOG_ERROR, "  Additional information: %s\n",
166                    detail);
167         return AVERROR_INVALIDDATA;
168     }
169
170     if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
171         if ((ret = set_pix_fmt(avctx, img)) < 0) {
172 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
173             av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
174                    img->fmt, img->bit_depth);
175 #else
176             av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
177                    img->fmt, 8);
178 #endif
179             return ret;
180         }
181
182         if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
183             av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
184                    avctx->width, avctx->height, img->d_w, img->d_h);
185             ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
186             if (ret < 0)
187                 return ret;
188         }
189         if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
190             return ret;
191         av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
192                       img->stride, avctx->pix_fmt, img->d_w, img->d_h);
193         *got_frame           = 1;
194     }
195     return avpkt->size;
196 }
197
198 static av_cold int vp8_free(AVCodecContext *avctx)
199 {
200     VP8Context *ctx = avctx->priv_data;
201     vpx_codec_destroy(&ctx->decoder);
202     return 0;
203 }
204
205 #if CONFIG_LIBVPX_VP8_DECODER
206 static av_cold int vp8_init(AVCodecContext *avctx)
207 {
208     return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
209 }
210
211 AVCodec ff_libvpx_vp8_decoder = {
212     .name           = "libvpx",
213     .long_name      = NULL_IF_CONFIG_SMALL("libvpx VP8"),
214     .type           = AVMEDIA_TYPE_VIDEO,
215     .id             = AV_CODEC_ID_VP8,
216     .priv_data_size = sizeof(VP8Context),
217     .init           = vp8_init,
218     .close          = vp8_free,
219     .decode         = vp8_decode,
220     .capabilities   = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
221 };
222 #endif /* CONFIG_LIBVPX_VP8_DECODER */
223
224 #if CONFIG_LIBVPX_VP9_DECODER
225 static av_cold int vp9_init(AVCodecContext *avctx)
226 {
227     return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
228 }
229
230 static const AVProfile profiles[] = {
231     { FF_PROFILE_VP9_0, "Profile 0" },
232     { FF_PROFILE_VP9_1, "Profile 1" },
233     { FF_PROFILE_VP9_2, "Profile 2" },
234     { FF_PROFILE_VP9_3, "Profile 3" },
235     { FF_PROFILE_UNKNOWN },
236 };
237
238 AVCodec ff_libvpx_vp9_decoder = {
239     .name           = "libvpx-vp9",
240     .long_name      = NULL_IF_CONFIG_SMALL("libvpx VP9"),
241     .type           = AVMEDIA_TYPE_VIDEO,
242     .id             = AV_CODEC_ID_VP9,
243     .priv_data_size = sizeof(VP8Context),
244     .init           = vp9_init,
245     .close          = vp8_free,
246     .decode         = vp8_decode,
247     .capabilities   = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
248     .init_static_data = ff_vp9_init_static,
249     .profiles       = NULL_IF_CONFIG_SMALL(profiles),
250 };
251 #endif /* CONFIG_LIBVPX_VP9_DECODER */