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  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] = {
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 #if VPX_IMAGE_ABI_VERSION >= 3
94  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
96 #else
97  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
98 #endif
99  return 0;
100 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
101  case VPX_IMG_FMT_I42016:
102  avctx->profile = FF_PROFILE_VP9_2;
103  if (img->bit_depth == 10) {
105  return 0;
106  } else if (img->bit_depth == 12) {
108  return 0;
109  } else {
110  return AVERROR_INVALIDDATA;
111  }
112  case VPX_IMG_FMT_I42216:
113  avctx->profile = FF_PROFILE_VP9_3;
114  if (img->bit_depth == 10) {
115 #if VPX_IMAGE_ABI_VERSION >= 3
116  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
118 #else
120 #endif
121  return 0;
122  } else if (img->bit_depth == 12) {
123 #if VPX_IMAGE_ABI_VERSION >= 3
124  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
126 #else
128 #endif
129  return 0;
130  } else {
131  return AVERROR_INVALIDDATA;
132  }
133 #if VPX_IMAGE_ABI_VERSION >= 3
134  case VPX_IMG_FMT_I44016:
135  avctx->profile = FF_PROFILE_VP9_3;
136  if (img->bit_depth == 10) {
138  return 0;
139  } else if (img->bit_depth == 12) {
141  return 0;
142  } else {
143  return AVERROR_INVALIDDATA;
144  }
145 #endif
146  case VPX_IMG_FMT_I44416:
147  avctx->profile = FF_PROFILE_VP9_3;
148  if (img->bit_depth == 10) {
150  return 0;
151  } else if (img->bit_depth == 12) {
153  return 0;
154  } else {
155  return AVERROR_INVALIDDATA;
156  }
157 #endif
158 #endif
159  default:
160  return AVERROR_INVALIDDATA;
161  }
162 }
163 
164 static int vp8_decode(AVCodecContext *avctx,
165  void *data, int *got_frame, AVPacket *avpkt)
166 {
167  VP8Context *ctx = avctx->priv_data;
168  AVFrame *picture = data;
169  const void *iter = NULL;
170  struct vpx_image *img;
171  int ret;
172 
173  if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
174  VPX_CODEC_OK) {
175  const char *error = vpx_codec_error(&ctx->decoder);
176  const char *detail = vpx_codec_error_detail(&ctx->decoder);
177 
178  av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
179  if (detail)
180  av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
181  detail);
182  return AVERROR_INVALIDDATA;
183  }
184 
185  if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
186  if ((ret = set_pix_fmt(avctx, img)) < 0) {
187 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
188  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
189  img->fmt, img->bit_depth);
190 #else
191  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
192  img->fmt, 8);
193 #endif
194  return ret;
195  }
196 
197  if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
198  av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
199  avctx->width, avctx->height, img->d_w, img->d_h);
200  ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
201  if (ret < 0)
202  return ret;
203  }
204  if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
205  return ret;
206  av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
207  img->stride, avctx->pix_fmt, img->d_w, img->d_h);
208  *got_frame = 1;
209  }
210  return avpkt->size;
211 }
212 
213 static av_cold int vp8_free(AVCodecContext *avctx)
214 {
215  VP8Context *ctx = avctx->priv_data;
216  vpx_codec_destroy(&ctx->decoder);
217  return 0;
218 }
219 
220 #if CONFIG_LIBVPX_VP8_DECODER
221 static av_cold int vp8_init(AVCodecContext *avctx)
222 {
223  return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
224 }
225 
226 AVCodec ff_libvpx_vp8_decoder = {
227  .name = "libvpx",
228  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
229  .type = AVMEDIA_TYPE_VIDEO,
230  .id = AV_CODEC_ID_VP8,
231  .priv_data_size = sizeof(VP8Context),
232  .init = vp8_init,
233  .close = vp8_free,
234  .decode = vp8_decode,
236 };
237 #endif /* CONFIG_LIBVPX_VP8_DECODER */
238 
239 #if CONFIG_LIBVPX_VP9_DECODER
240 static av_cold int vp9_init(AVCodecContext *avctx)
241 {
242  return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
243 }
244 
245 static const AVProfile profiles[] = {
246  { FF_PROFILE_VP9_0, "Profile 0" },
247  { FF_PROFILE_VP9_1, "Profile 1" },
248  { FF_PROFILE_VP9_2, "Profile 2" },
249  { FF_PROFILE_VP9_3, "Profile 3" },
250  { FF_PROFILE_UNKNOWN },
251 };
252 
253 AVCodec ff_libvpx_vp9_decoder = {
254  .name = "libvpx-vp9",
255  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
256  .type = AVMEDIA_TYPE_VIDEO,
257  .id = AV_CODEC_ID_VP9,
258  .priv_data_size = sizeof(VP8Context),
259  .init = vp9_init,
260  .close = vp8_free,
261  .decode = vp8_decode,
264  .profiles = NULL_IF_CONFIG_SMALL(profiles),
265 };
266 #endif /* CONFIG_LIBVPX_VP9_DECODER */
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:519
#define NULL
Definition: coverity.c:32
planar YUV 4:4:0,20bpp, (1 Cr & Cb sample per 1x2 Y samples), little-endian
Definition: pixfmt.h:319
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:171
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:68
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:216
struct vpx_codec_ctx decoder
Definition: libvpxdec.c:37
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:523
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:188
int size
Definition: avcodec.h:1424
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above ...
Definition: pixfmt.h:524
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1722
planar GBR 4:4:4 36bpp, little-endian
Definition: pixfmt.h:296
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:932
int profile
profile
Definition: avcodec.h:3115
AVCodec.
Definition: avcodec.h:3472
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:518
#define img
uint8_t
#define av_cold
Definition: attributes.h:74
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:517
planar YUV 4:4:4,36bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:292
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:3116
static av_cold void init_static_data(void)
Definition: dsddec.c:82
uint8_t * data
Definition: avcodec.h:1423
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define av_log(a,...)
planar YUV 4:4:4, 30bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:177
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
planar YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:173
const char * name
Name of the codec implementation.
Definition: avcodec.h:3479
Libavcodec external API header.
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:288
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
static av_cold int vp8_free(AVCodecContext *avctx)
Definition: libvpxdec.c:213
#define FFMIN(a, b)
Definition: common.h:81
static const chunk_decoder decoder[8]
Definition: dfa.c:327
int width
picture width / height.
Definition: avcodec.h:1681
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:527
static av_cold int vpx_init(AVCodecContext *avctx, const struct vpx_codec_iface *iface)
Definition: libvpxdec.c:40
static int vp8_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: libvpxdec.c:164
planar YUV 4:2:0,18bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:284
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:3033
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static const AVProfile profiles[]
enum AVCodecID codec_id
Definition: avcodec.h:1519
av_cold void ff_vp9_init_static(AVCodec *codec)
Definition: libvpx.c:61
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
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:1502
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1040
planar YUV 4:4:0,24bpp, (1 Cr & Cb sample per 1x2 Y samples), little-endian
Definition: pixfmt.h:321
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2230
static int set_pix_fmt(AVCodecContext *avctx, struct vpx_image *img)
Definition: libvpxdec.c:63
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:523
#define FF_PROFILE_VP9_1
Definition: avcodec.h:3190
static av_cold int vp8_init(AVFormatContext *s, int st_index, PayloadContext *vp8)
Definition: rtpdec_vp8.c:263
#define FF_PROFILE_VP9_3
Definition: avcodec.h:3192
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
planar YUV 4:2:2,24bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:288
common internal api header.
common internal and external API header
#define FF_PROFILE_VP9_0
Definition: avcodec.h:3189
AVProfile.
Definition: avcodec.h:3460
void * priv_data
Definition: avcodec.h:1544
#define FF_PROFILE_VP9_2
Definition: avcodec.h:3191
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:101
This structure stores compressed data.
Definition: avcodec.h:1400
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:857
planar GBR 4:4:4 30bpp, little-endian
Definition: pixfmt.h:192