]> git.sesse.net Git - ffmpeg/blob - libavcodec/libvpxdec.c
Merge commit '3080b0497ddf8549d86ee99b79ac0c15f44ee382'
[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 (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
66         return AVERROR_INVALIDDATA;
67     switch (img->fmt) {
68     case VPX_IMG_FMT_I420:
69         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
70         return 0;
71 #if CONFIG_LIBVPX_VP9_DECODER
72     case VPX_IMG_FMT_I422:
73         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
74         return 0;
75     case VPX_IMG_FMT_I444:
76         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
77         return 0;
78 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
79     case VPX_IMG_FMT_I42016:
80         if (img->bit_depth == 10) {
81             avctx->pix_fmt = AV_PIX_FMT_YUV420P10LE;
82             return 0;
83         } else if (img->bit_depth == 12) {
84             avctx->pix_fmt = AV_PIX_FMT_YUV420P12LE;
85             return 0;
86         } else {
87             return AVERROR_INVALIDDATA;
88         }
89     case VPX_IMG_FMT_I42216:
90         if (img->bit_depth == 10) {
91             avctx->pix_fmt = AV_PIX_FMT_YUV422P10LE;
92             return 0;
93         } else if (img->bit_depth == 12) {
94             avctx->pix_fmt = AV_PIX_FMT_YUV422P12LE;
95             return 0;
96         } else {
97             return AVERROR_INVALIDDATA;
98         }
99     case VPX_IMG_FMT_I44416:
100         if (img->bit_depth == 10) {
101             avctx->pix_fmt = AV_PIX_FMT_YUV444P10LE;
102             return 0;
103         } else if (img->bit_depth == 12) {
104             avctx->pix_fmt = AV_PIX_FMT_YUV444P12LE;
105             return 0;
106         } else {
107             return AVERROR_INVALIDDATA;
108         }
109 #endif
110 #endif
111     default:
112         return AVERROR_INVALIDDATA;
113     }
114 }
115
116 static int vp8_decode(AVCodecContext *avctx,
117                       void *data, int *got_frame, AVPacket *avpkt)
118 {
119     VP8Context *ctx = avctx->priv_data;
120     AVFrame *picture = data;
121     const void *iter = NULL;
122     struct vpx_image *img;
123     int ret;
124
125     if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
126         VPX_CODEC_OK) {
127         const char *error  = vpx_codec_error(&ctx->decoder);
128         const char *detail = vpx_codec_error_detail(&ctx->decoder);
129
130         av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
131         if (detail)
132             av_log(avctx, AV_LOG_ERROR, "  Additional information: %s\n",
133                    detail);
134         return AVERROR_INVALIDDATA;
135     }
136
137     if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
138         if ((ret = set_pix_fmt(avctx, img)) < 0) {
139 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
140             av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
141                    img->fmt, img->bit_depth);
142 #else
143             av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
144                    img->fmt, 8);
145 #endif
146             return ret;
147         }
148
149         if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
150             av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
151                    avctx->width, avctx->height, img->d_w, img->d_h);
152             ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
153             if (ret < 0)
154                 return ret;
155         }
156         if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
157             return ret;
158         av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
159                       img->stride, avctx->pix_fmt, img->d_w, img->d_h);
160         *got_frame           = 1;
161     }
162     return avpkt->size;
163 }
164
165 static av_cold int vp8_free(AVCodecContext *avctx)
166 {
167     VP8Context *ctx = avctx->priv_data;
168     vpx_codec_destroy(&ctx->decoder);
169     return 0;
170 }
171
172 #if CONFIG_LIBVPX_VP8_DECODER
173 static av_cold int vp8_init(AVCodecContext *avctx)
174 {
175     return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
176 }
177
178 AVCodec ff_libvpx_vp8_decoder = {
179     .name           = "libvpx",
180     .long_name      = NULL_IF_CONFIG_SMALL("libvpx VP8"),
181     .type           = AVMEDIA_TYPE_VIDEO,
182     .id             = AV_CODEC_ID_VP8,
183     .priv_data_size = sizeof(VP8Context),
184     .init           = vp8_init,
185     .close          = vp8_free,
186     .decode         = vp8_decode,
187     .capabilities   = CODEC_CAP_AUTO_THREADS | CODEC_CAP_DR1,
188 };
189 #endif /* CONFIG_LIBVPX_VP8_DECODER */
190
191 #if CONFIG_LIBVPX_VP9_DECODER
192 static av_cold int vp9_init(AVCodecContext *avctx)
193 {
194     return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
195 }
196
197 AVCodec ff_libvpx_vp9_decoder = {
198     .name           = "libvpx-vp9",
199     .long_name      = NULL_IF_CONFIG_SMALL("libvpx VP9"),
200     .type           = AVMEDIA_TYPE_VIDEO,
201     .id             = AV_CODEC_ID_VP9,
202     .priv_data_size = sizeof(VP8Context),
203     .init           = vp9_init,
204     .close          = vp8_free,
205     .decode         = vp8_decode,
206     .capabilities   = CODEC_CAP_AUTO_THREADS | CODEC_CAP_DR1,
207     .init_static_data = ff_vp9_init_static,
208 };
209 #endif /* CONFIG_LIBVPX_VP9_DECODER */