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/9 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 "libavutil/intreadwrite.h"
33 #include "avcodec.h"
34 #include "internal.h"
35 #include "libvpx.h"
36 #include "profiles.h"
37 
38 typedef struct VPxDecoderContext {
39  struct vpx_codec_ctx decoder;
40  struct vpx_codec_ctx decoder_alpha;
42 } VPxContext;
43 
44 static av_cold int vpx_init(AVCodecContext *avctx,
45  const struct vpx_codec_iface *iface,
46  int is_alpha_decoder)
47 {
48  VPxContext *ctx = avctx->priv_data;
49  struct vpx_codec_dec_cfg deccfg = {
50  /* token partitions+1 would be a decent choice */
51  .threads = FFMIN(avctx->thread_count, 16)
52  };
53 
54  av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
55  av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
56 
57  if (vpx_codec_dec_init(
58  is_alpha_decoder ? &ctx->decoder_alpha : &ctx->decoder,
59  iface, &deccfg, 0) != VPX_CODEC_OK) {
60  const char *error = vpx_codec_error(&ctx->decoder);
61  av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
62  error);
63  return AVERROR(EINVAL);
64  }
65 
66  return 0;
67 }
68 
69 // returns 0 on success, AVERROR_INVALIDDATA otherwise
70 static int set_pix_fmt(AVCodecContext *avctx, struct vpx_image *img,
71  int has_alpha_channel)
72 {
73 #if VPX_IMAGE_ABI_VERSION >= 3
74  static const enum AVColorSpace colorspaces[8] = {
77  };
78 #if VPX_IMAGE_ABI_VERSION >= 4
79  static const enum AVColorRange color_ranges[] = {
81  };
82  avctx->color_range = color_ranges[img->range];
83 #endif
84  avctx->colorspace = colorspaces[img->cs];
85 #endif
86  if (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
87  return AVERROR_INVALIDDATA;
88  switch (img->fmt) {
89  case VPX_IMG_FMT_I420:
90  if (avctx->codec_id == AV_CODEC_ID_VP9)
91  avctx->profile = FF_PROFILE_VP9_0;
92  avctx->pix_fmt =
93  has_alpha_channel ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
94  return 0;
95 #if CONFIG_LIBVPX_VP9_DECODER
96  case VPX_IMG_FMT_I422:
97  avctx->profile = FF_PROFILE_VP9_1;
98  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
99  return 0;
100 #if VPX_IMAGE_ABI_VERSION >= 3
101  case VPX_IMG_FMT_I440:
102  avctx->profile = FF_PROFILE_VP9_1;
103  avctx->pix_fmt = AV_PIX_FMT_YUV440P;
104  return 0;
105 #endif
106  case VPX_IMG_FMT_I444:
107  avctx->profile = FF_PROFILE_VP9_1;
108 #if VPX_IMAGE_ABI_VERSION >= 3
109  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
111 #else
112  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
113 #endif
114  return 0;
115 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
116  case VPX_IMG_FMT_I42016:
117  avctx->profile = FF_PROFILE_VP9_2;
118  if (img->bit_depth == 10) {
119  avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
120  return 0;
121  } else if (img->bit_depth == 12) {
122  avctx->pix_fmt = AV_PIX_FMT_YUV420P12;
123  return 0;
124  } else {
125  return AVERROR_INVALIDDATA;
126  }
127  case VPX_IMG_FMT_I42216:
128  avctx->profile = FF_PROFILE_VP9_3;
129  if (img->bit_depth == 10) {
130  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
131  return 0;
132  } else if (img->bit_depth == 12) {
133  avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
134  return 0;
135  } else {
136  return AVERROR_INVALIDDATA;
137  }
138 #if VPX_IMAGE_ABI_VERSION >= 3
139  case VPX_IMG_FMT_I44016:
140  avctx->profile = FF_PROFILE_VP9_3;
141  if (img->bit_depth == 10) {
142  avctx->pix_fmt = AV_PIX_FMT_YUV440P10;
143  return 0;
144  } else if (img->bit_depth == 12) {
145  avctx->pix_fmt = AV_PIX_FMT_YUV440P12;
146  return 0;
147  } else {
148  return AVERROR_INVALIDDATA;
149  }
150 #endif
151  case VPX_IMG_FMT_I44416:
152  avctx->profile = FF_PROFILE_VP9_3;
153  if (img->bit_depth == 10) {
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_YUV444P10;
159 #endif
160  return 0;
161  } else if (img->bit_depth == 12) {
162 #if VPX_IMAGE_ABI_VERSION >= 3
163  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
165 #else
166  avctx->pix_fmt = AV_PIX_FMT_YUV444P12;
167 #endif
168  return 0;
169  } else {
170  return AVERROR_INVALIDDATA;
171  }
172 #endif
173 #endif
174  default:
175  return AVERROR_INVALIDDATA;
176  }
177 }
178 
179 static int decode_frame(AVCodecContext *avctx, vpx_codec_ctx_t *decoder,
180  uint8_t *data, uint32_t data_sz)
181 {
182  if (vpx_codec_decode(decoder, data, data_sz, NULL, 0) != VPX_CODEC_OK) {
183  const char *error = vpx_codec_error(decoder);
184  const char *detail = vpx_codec_error_detail(decoder);
185 
186  av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
187  if (detail) {
188  av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
189  detail);
190  }
191  return AVERROR_INVALIDDATA;
192  }
193  return 0;
194 }
195 
196 static int vpx_decode(AVCodecContext *avctx,
197  void *data, int *got_frame, AVPacket *avpkt)
198 {
199  VPxContext *ctx = avctx->priv_data;
200  AVFrame *picture = data;
201  const void *iter = NULL;
202  const void *iter_alpha = NULL;
203  struct vpx_image *img, *img_alpha;
204  int ret;
205  uint8_t *side_data = NULL;
206  int side_data_size = 0;
207 
208  ret = decode_frame(avctx, &ctx->decoder, avpkt->data, avpkt->size);
209  if (ret)
210  return ret;
211 
212  side_data = av_packet_get_side_data(avpkt,
214  &side_data_size);
215  if (side_data_size > 1) {
216  const uint64_t additional_id = AV_RB64(side_data);
217  side_data += 8;
218  side_data_size -= 8;
219  if (additional_id == 1) { // 1 stands for alpha channel data.
220  if (!ctx->has_alpha_channel) {
221  ctx->has_alpha_channel = 1;
222  ret = vpx_init(avctx,
223 #if CONFIG_LIBVPX_VP8_DECODER && CONFIG_LIBVPX_VP9_DECODER
224  (avctx->codec_id == AV_CODEC_ID_VP8) ?
225  &vpx_codec_vp8_dx_algo : &vpx_codec_vp9_dx_algo,
226 #elif CONFIG_LIBVPX_VP8_DECODER
227  &vpx_codec_vp8_dx_algo,
228 #else
229  &vpx_codec_vp9_dx_algo,
230 #endif
231  1);
232  if (ret)
233  return ret;
234  }
235  ret = decode_frame(avctx, &ctx->decoder_alpha, side_data,
236  side_data_size);
237  if (ret)
238  return ret;
239  }
240  }
241 
242  if ((img = vpx_codec_get_frame(&ctx->decoder, &iter)) &&
243  (!ctx->has_alpha_channel ||
244  (img_alpha = vpx_codec_get_frame(&ctx->decoder_alpha, &iter_alpha)))) {
245  uint8_t *planes[4];
246  int linesizes[4];
247  if ((ret = set_pix_fmt(avctx, img, ctx->has_alpha_channel)) < 0) {
248 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
249  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
250  img->fmt, img->bit_depth);
251 #else
252  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
253  img->fmt, 8);
254 #endif
255  return ret;
256  }
257 
258  if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
259  av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
260  avctx->width, avctx->height, img->d_w, img->d_h);
261  ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
262  if (ret < 0)
263  return ret;
264  }
265  if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
266  return ret;
267 
268  planes[0] = img->planes[VPX_PLANE_Y];
269  planes[1] = img->planes[VPX_PLANE_U];
270  planes[2] = img->planes[VPX_PLANE_V];
271  planes[3] =
272  ctx->has_alpha_channel ? img_alpha->planes[VPX_PLANE_Y] : NULL;
273  linesizes[0] = img->stride[VPX_PLANE_Y];
274  linesizes[1] = img->stride[VPX_PLANE_U];
275  linesizes[2] = img->stride[VPX_PLANE_V];
276  linesizes[3] =
277  ctx->has_alpha_channel ? img_alpha->stride[VPX_PLANE_Y] : 0;
278  av_image_copy(picture->data, picture->linesize, (const uint8_t**)planes,
279  linesizes, avctx->pix_fmt, img->d_w, img->d_h);
280  *got_frame = 1;
281  }
282  return avpkt->size;
283 }
284 
285 static av_cold int vpx_free(AVCodecContext *avctx)
286 {
287  VPxContext *ctx = avctx->priv_data;
288  vpx_codec_destroy(&ctx->decoder);
289  if (ctx->has_alpha_channel)
290  vpx_codec_destroy(&ctx->decoder_alpha);
291  return 0;
292 }
293 
294 #if CONFIG_LIBVPX_VP8_DECODER
295 static av_cold int vp8_init(AVCodecContext *avctx)
296 {
297  return vpx_init(avctx, &vpx_codec_vp8_dx_algo, 0);
298 }
299 
300 AVCodec ff_libvpx_vp8_decoder = {
301  .name = "libvpx",
302  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
303  .type = AVMEDIA_TYPE_VIDEO,
304  .id = AV_CODEC_ID_VP8,
305  .priv_data_size = sizeof(VPxContext),
306  .init = vp8_init,
307  .close = vpx_free,
308  .decode = vpx_decode,
310 };
311 #endif /* CONFIG_LIBVPX_VP8_DECODER */
312 
313 #if CONFIG_LIBVPX_VP9_DECODER
314 static av_cold int vp9_init(AVCodecContext *avctx)
315 {
316  return vpx_init(avctx, &vpx_codec_vp9_dx_algo, 0);
317 }
318 
319 AVCodec ff_libvpx_vp9_decoder = {
320  .name = "libvpx-vp9",
321  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
322  .type = AVMEDIA_TYPE_VIDEO,
323  .id = AV_CODEC_ID_VP9,
324  .priv_data_size = sizeof(VPxContext),
325  .init = vp9_init,
326  .close = vpx_free,
327  .decode = vpx_decode,
329  .init_static_data = ff_vp9_init_static,
331 };
332 #endif /* CONFIG_LIBVPX_VP9_DECODER */
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:438
#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:343
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
misc image utilities
static av_cold int vpx_free(AVCodecContext *avctx)
Definition: libvpxdec.c:285
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
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:442
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2413
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:180
int size
Definition: avcodec.h:1602
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:357
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1904
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:345
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:1034
int profile
profile
Definition: avcodec.h:3181
AVCodec.
Definition: avcodec.h:3600
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:437
functionally identical to above
Definition: pixfmt.h:444
#define FF_PROFILE_VP9_0
Definition: avcodec.h:3264
#define img
int has_alpha_channel
Definition: libvpxdec.c:41
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:102
uint8_t
#define av_cold
Definition: attributes.h:82
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:436
uint8_t * data
Definition: avcodec.h:1601
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
AVColorRange
MPEG vs JPEG YUV range.
Definition: pixfmt.h:457
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:346
#define av_log(a,...)
static int vpx_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: libvpxdec.c:196
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
static av_cold int vpx_init(AVCodecContext *avctx, const struct vpx_codec_iface *iface, int is_alpha_decoder)
Definition: libvpxdec.c:44
#define FF_PROFILE_VP9_3
Definition: avcodec.h:3267
#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:3607
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:344
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:3266
struct vpx_codec_ctx decoder
Definition: libvpxdec.c:39
struct vpx_codec_ctx decoder_alpha
Definition: libvpxdec.c:40
#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:1863
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:446
AVFormatContext * ctx
Definition: movenc.c:48
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:3107
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:460
#define FF_PROFILE_VP9_1
Definition: avcodec.h:3265
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:1693
av_cold void ff_vp9_init_static(AVCodec *codec)
Definition: libvpx.c:72
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
static av_cold int vp9_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
Definition: rtpdec_vp9.c:34
static int set_pix_fmt(AVCodecContext *avctx, struct vpx_image *img, int has_alpha_channel)
Definition: libvpxdec.c:70
main external API structure.
Definition: avcodec.h:1676
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:947
Data found in BlockAdditional element of matroska container.
Definition: avcodec.h:1508
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:341
static const AVProfile profiles[]
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2406
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:347
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:358
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:342
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:348
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:459
static av_cold int vp8_init(AVFormatContext *s, int st_index, PayloadContext *vp8)
Definition: rtpdec_vp8.c:263
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
Definition: bytestream.h:87
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
static int decode_frame(AVCodecContext *avctx, vpx_codec_ctx_t *decoder, uint8_t *data, uint32_t data_sz)
Definition: libvpxdec.c:179
common internal api header.
common internal and external API header
void * priv_data
Definition: avcodec.h:1718
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2035
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:338
const AVProfile ff_vp9_profiles[]
Definition: profiles.c:135
This structure stores compressed data.
Definition: avcodec.h:1578
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959