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