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 #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] = {
70  };
71 #if VPX_IMAGE_ABI_VERSION >= 4
72  static const enum AVColorRange color_ranges[] = {
74  };
75  avctx->color_range = color_ranges[img->range];
76 #endif
77  avctx->colorspace = colorspaces[img->cs];
78 #endif
79  if (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
80  return AVERROR_INVALIDDATA;
81  switch (img->fmt) {
82  case VPX_IMG_FMT_I420:
83  if (avctx->codec_id == AV_CODEC_ID_VP9)
84  avctx->profile = FF_PROFILE_VP9_0;
85  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
86  return 0;
87 #if CONFIG_LIBVPX_VP9_DECODER
88  case VPX_IMG_FMT_I422:
89  avctx->profile = FF_PROFILE_VP9_1;
90  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
91  return 0;
92 #if VPX_IMAGE_ABI_VERSION >= 3
93  case VPX_IMG_FMT_I440:
94  avctx->profile = FF_PROFILE_VP9_1;
95  avctx->pix_fmt = AV_PIX_FMT_YUV440P;
96  return 0;
97 #endif
98  case VPX_IMG_FMT_I444:
99  avctx->profile = FF_PROFILE_VP9_1;
100 #if VPX_IMAGE_ABI_VERSION >= 3
101  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
103 #else
104  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
105 #endif
106  return 0;
107 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
108  case VPX_IMG_FMT_I42016:
109  avctx->profile = FF_PROFILE_VP9_2;
110  if (img->bit_depth == 10) {
111  avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
112  return 0;
113  } else if (img->bit_depth == 12) {
114  avctx->pix_fmt = AV_PIX_FMT_YUV420P12;
115  return 0;
116  } else {
117  return AVERROR_INVALIDDATA;
118  }
119  case VPX_IMG_FMT_I42216:
120  avctx->profile = FF_PROFILE_VP9_3;
121  if (img->bit_depth == 10) {
122  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
123  return 0;
124  } else if (img->bit_depth == 12) {
125  avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
126  return 0;
127  } else {
128  return AVERROR_INVALIDDATA;
129  }
130 #if VPX_IMAGE_ABI_VERSION >= 3
131  case VPX_IMG_FMT_I44016:
132  avctx->profile = FF_PROFILE_VP9_3;
133  if (img->bit_depth == 10) {
134  avctx->pix_fmt = AV_PIX_FMT_YUV440P10;
135  return 0;
136  } else if (img->bit_depth == 12) {
137  avctx->pix_fmt = AV_PIX_FMT_YUV440P12;
138  return 0;
139  } else {
140  return AVERROR_INVALIDDATA;
141  }
142 #endif
143  case VPX_IMG_FMT_I44416:
144  avctx->profile = FF_PROFILE_VP9_3;
145  if (img->bit_depth == 10) {
146 #if VPX_IMAGE_ABI_VERSION >= 3
147  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
149 #else
150  avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
151 #endif
152  return 0;
153  } else if (img->bit_depth == 12) {
154 #if VPX_IMAGE_ABI_VERSION >= 3
155  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
157 #else
158  avctx->pix_fmt = AV_PIX_FMT_YUV444P12;
159 #endif
160  return 0;
161  } else {
162  return AVERROR_INVALIDDATA;
163  }
164 #endif
165 #endif
166  default:
167  return AVERROR_INVALIDDATA;
168  }
169 }
170 
171 static int vp8_decode(AVCodecContext *avctx,
172  void *data, int *got_frame, AVPacket *avpkt)
173 {
174  VP8Context *ctx = avctx->priv_data;
175  AVFrame *picture = data;
176  const void *iter = NULL;
177  struct vpx_image *img;
178  int ret;
179 
180  if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
181  VPX_CODEC_OK) {
182  const char *error = vpx_codec_error(&ctx->decoder);
183  const char *detail = vpx_codec_error_detail(&ctx->decoder);
184 
185  av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
186  if (detail)
187  av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
188  detail);
189  return AVERROR_INVALIDDATA;
190  }
191 
192  if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
193  if ((ret = set_pix_fmt(avctx, img)) < 0) {
194 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
195  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
196  img->fmt, img->bit_depth);
197 #else
198  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
199  img->fmt, 8);
200 #endif
201  return ret;
202  }
203 
204  if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
205  av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
206  avctx->width, avctx->height, img->d_w, img->d_h);
207  ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
208  if (ret < 0)
209  return ret;
210  }
211  if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
212  return ret;
213  av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
214  img->stride, avctx->pix_fmt, img->d_w, img->d_h);
215  *got_frame = 1;
216  }
217  return avpkt->size;
218 }
219 
220 static av_cold int vp8_free(AVCodecContext *avctx)
221 {
222  VP8Context *ctx = avctx->priv_data;
223  vpx_codec_destroy(&ctx->decoder);
224  return 0;
225 }
226 
227 #if CONFIG_LIBVPX_VP8_DECODER
228 static av_cold int vp8_init(AVCodecContext *avctx)
229 {
230  return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
231 }
232 
233 AVCodec ff_libvpx_vp8_decoder = {
234  .name = "libvpx",
235  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
236  .type = AVMEDIA_TYPE_VIDEO,
237  .id = AV_CODEC_ID_VP8,
238  .priv_data_size = sizeof(VP8Context),
239  .init = vp8_init,
240  .close = vp8_free,
241  .decode = vp8_decode,
243 };
244 #endif /* CONFIG_LIBVPX_VP8_DECODER */
245 
246 #if CONFIG_LIBVPX_VP9_DECODER
247 static av_cold int vp9_init(AVCodecContext *avctx)
248 {
249  return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
250 }
251 
252 AVCodec ff_libvpx_vp9_decoder = {
253  .name = "libvpx-vp9",
254  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
255  .type = AVMEDIA_TYPE_VIDEO,
256  .id = AV_CODEC_ID_VP9,
257  .priv_data_size = sizeof(VP8Context),
258  .init = vp9_init,
259  .close = vp8_free,
260  .decode = vp8_decode,
262  .init_static_data = ff_vp9_init_static,
264 };
265 #endif /* CONFIG_LIBVPX_VP9_DECODER */
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:431
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AV_PIX_FMT_YUV440P10
Definition: pixfmt.h:340
This structure describes decoded (raw) audio or video data.
Definition: frame.h:180
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
AVFormatContext * ctx
Definition: movenc-test.c:48
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
misc image utilities
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:210
struct vpx_codec_ctx decoder
Definition: libvpxdec.c:38
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:435
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2378
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:180
int size
Definition: avcodec.h:1578
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:354
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:436
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1870
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:342
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:1028
int profile
profile
Definition: avcodec.h:3146
AVCodec.
Definition: avcodec.h:3535
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:430
functionally identical to above
Definition: pixfmt.h:437
#define FF_PROFILE_VP9_0
Definition: avcodec.h:3220
#define img
uint8_t
#define av_cold
Definition: attributes.h:82
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:429
uint8_t * data
Definition: avcodec.h:1577
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
AVColorRange
MPEG vs JPEG YUV range.
Definition: pixfmt.h:449
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:343
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define FF_PROFILE_VP9_3
Definition: avcodec.h:3223
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * name
Name of the codec implementation.
Definition: avcodec.h:3542
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:341
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:302
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
#define FF_PROFILE_VP9_2
Definition: avcodec.h:3222
static av_cold int vp8_free(AVCodecContext *avctx)
Definition: libvpxdec.c:220
#define FFMIN(a, b)
Definition: common.h:96
static const chunk_decoder decoder[8]
Definition: dfa.c:327
int width
picture width / height.
Definition: avcodec.h:1829
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:439
static av_cold int vpx_init(AVCodecContext *avctx, const struct vpx_codec_iface *iface)
Definition: libvpxdec.c:41
static int vp8_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: libvpxdec.c:171
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:3072
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:452
#define FF_PROFILE_VP9_1
Definition: avcodec.h:3221
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:1659
av_cold void ff_vp9_init_static(AVCodec *codec)
Definition: libvpx.c:69
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:211
static av_cold int vp9_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
Definition: rtpdec_vp9.c:34
main external API structure.
Definition: avcodec.h:1642
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:928
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:338
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2371
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:344
static int set_pix_fmt(AVCodecContext *avctx, struct vpx_image *img)
Definition: libvpxdec.c:64
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:355
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:339
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:345
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:194
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:451
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:722
static av_cold int vp8_init(AVFormatContext *s, int st_index, PayloadContext *vp8)
Definition: rtpdec_vp8.c:263
static const AVProfile profiles[]
Definition: libfaac.c:216
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
common internal api header.
common internal and external API header
void * priv_data
Definition: avcodec.h:1684
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
const AVProfile ff_vp9_profiles[]
Definition: profiles.c:123
This structure stores compressed data.
Definition: avcodec.h:1554
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:953