FFmpeg  4.0
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  static const enum AVColorSpace colorspaces[8] = {
76  };
77 #if VPX_IMAGE_ABI_VERSION >= 4
78  static const enum AVColorRange color_ranges[] = {
80  };
81  avctx->color_range = color_ranges[img->range];
82 #endif
83  avctx->colorspace = colorspaces[img->cs];
84  if (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
85  return AVERROR_INVALIDDATA;
86  switch (img->fmt) {
87  case VPX_IMG_FMT_I420:
88  if (avctx->codec_id == AV_CODEC_ID_VP9)
89  avctx->profile = FF_PROFILE_VP9_0;
90  avctx->pix_fmt =
91  has_alpha_channel ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
92  return 0;
93 #if CONFIG_LIBVPX_VP9_DECODER
94  case VPX_IMG_FMT_I422:
95  avctx->profile = FF_PROFILE_VP9_1;
96  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
97  return 0;
98  case VPX_IMG_FMT_I440:
99  avctx->profile = FF_PROFILE_VP9_1;
100  avctx->pix_fmt = AV_PIX_FMT_YUV440P;
101  return 0;
102  case VPX_IMG_FMT_I444:
103  avctx->profile = FF_PROFILE_VP9_1;
104  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
106  return 0;
107  case VPX_IMG_FMT_I42016:
108  avctx->profile = FF_PROFILE_VP9_2;
109  if (img->bit_depth == 10) {
110  avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
111  return 0;
112  } else if (img->bit_depth == 12) {
113  avctx->pix_fmt = AV_PIX_FMT_YUV420P12;
114  return 0;
115  } else {
116  return AVERROR_INVALIDDATA;
117  }
118  case VPX_IMG_FMT_I42216:
119  avctx->profile = FF_PROFILE_VP9_3;
120  if (img->bit_depth == 10) {
121  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
122  return 0;
123  } else if (img->bit_depth == 12) {
124  avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
125  return 0;
126  } else {
127  return AVERROR_INVALIDDATA;
128  }
129  case VPX_IMG_FMT_I44016:
130  avctx->profile = FF_PROFILE_VP9_3;
131  if (img->bit_depth == 10) {
132  avctx->pix_fmt = AV_PIX_FMT_YUV440P10;
133  return 0;
134  } else if (img->bit_depth == 12) {
135  avctx->pix_fmt = AV_PIX_FMT_YUV440P12;
136  return 0;
137  } else {
138  return AVERROR_INVALIDDATA;
139  }
140  case VPX_IMG_FMT_I44416:
141  avctx->profile = FF_PROFILE_VP9_3;
142  if (img->bit_depth == 10) {
143  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
145  return 0;
146  } else if (img->bit_depth == 12) {
147  avctx->pix_fmt = avctx->colorspace == AVCOL_SPC_RGB ?
149  return 0;
150  } else {
151  return AVERROR_INVALIDDATA;
152  }
153 #endif
154  default:
155  return AVERROR_INVALIDDATA;
156  }
157 }
158 
159 static int decode_frame(AVCodecContext *avctx, vpx_codec_ctx_t *decoder,
160  uint8_t *data, uint32_t data_sz)
161 {
162  if (vpx_codec_decode(decoder, data, data_sz, NULL, 0) != VPX_CODEC_OK) {
163  const char *error = vpx_codec_error(decoder);
164  const char *detail = vpx_codec_error_detail(decoder);
165 
166  av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
167  if (detail) {
168  av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
169  detail);
170  }
171  return AVERROR_INVALIDDATA;
172  }
173  return 0;
174 }
175 
176 static int vpx_decode(AVCodecContext *avctx,
177  void *data, int *got_frame, AVPacket *avpkt)
178 {
179  VPxContext *ctx = avctx->priv_data;
180  AVFrame *picture = data;
181  const void *iter = NULL;
182  const void *iter_alpha = NULL;
183  struct vpx_image *img, *img_alpha;
184  int ret;
185  uint8_t *side_data = NULL;
186  int side_data_size = 0;
187 
188  ret = decode_frame(avctx, &ctx->decoder, avpkt->data, avpkt->size);
189  if (ret)
190  return ret;
191 
192  side_data = av_packet_get_side_data(avpkt,
194  &side_data_size);
195  if (side_data_size > 1) {
196  const uint64_t additional_id = AV_RB64(side_data);
197  side_data += 8;
198  side_data_size -= 8;
199  if (additional_id == 1) { // 1 stands for alpha channel data.
200  if (!ctx->has_alpha_channel) {
201  ctx->has_alpha_channel = 1;
202  ret = vpx_init(avctx,
204  (avctx->codec_id == AV_CODEC_ID_VP8) ?
205  &vpx_codec_vp8_dx_algo : &vpx_codec_vp9_dx_algo,
207  &vpx_codec_vp8_dx_algo,
208 #else
209  &vpx_codec_vp9_dx_algo,
210 #endif
211  1);
212  if (ret)
213  return ret;
214  }
215  ret = decode_frame(avctx, &ctx->decoder_alpha, side_data,
216  side_data_size);
217  if (ret)
218  return ret;
219  }
220  }
221 
222  if ((img = vpx_codec_get_frame(&ctx->decoder, &iter)) &&
223  (!ctx->has_alpha_channel ||
224  (img_alpha = vpx_codec_get_frame(&ctx->decoder_alpha, &iter_alpha)))) {
225  uint8_t *planes[4];
226  int linesizes[4];
227 
228  if (img->d_w > img->w || img->d_h > img->h) {
229  av_log(avctx, AV_LOG_ERROR, "Display dimensions %dx%d exceed storage %dx%d\n",
230  img->d_w, img->d_h, img->w, img->h);
231  return AVERROR_EXTERNAL;
232  }
233 
234  if ((ret = set_pix_fmt(avctx, img, ctx->has_alpha_channel)) < 0) {
235  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
236  img->fmt, img->bit_depth);
237  return ret;
238  }
239 
240  if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
241  av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
242  avctx->width, avctx->height, img->d_w, img->d_h);
243  ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
244  if (ret < 0)
245  return ret;
246  }
247  if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
248  return ret;
249 
250  planes[0] = img->planes[VPX_PLANE_Y];
251  planes[1] = img->planes[VPX_PLANE_U];
252  planes[2] = img->planes[VPX_PLANE_V];
253  planes[3] =
254  ctx->has_alpha_channel ? img_alpha->planes[VPX_PLANE_Y] : NULL;
255  linesizes[0] = img->stride[VPX_PLANE_Y];
256  linesizes[1] = img->stride[VPX_PLANE_U];
257  linesizes[2] = img->stride[VPX_PLANE_V];
258  linesizes[3] =
259  ctx->has_alpha_channel ? img_alpha->stride[VPX_PLANE_Y] : 0;
260  av_image_copy(picture->data, picture->linesize, (const uint8_t**)planes,
261  linesizes, avctx->pix_fmt, img->d_w, img->d_h);
262  *got_frame = 1;
263  }
264  return avpkt->size;
265 }
266 
267 static av_cold int vpx_free(AVCodecContext *avctx)
268 {
269  VPxContext *ctx = avctx->priv_data;
270  vpx_codec_destroy(&ctx->decoder);
271  if (ctx->has_alpha_channel)
272  vpx_codec_destroy(&ctx->decoder_alpha);
273  return 0;
274 }
275 
276 #if CONFIG_LIBVPX_VP8_DECODER
277 static av_cold int vp8_init(AVCodecContext *avctx)
278 {
279  return vpx_init(avctx, &vpx_codec_vp8_dx_algo, 0);
280 }
281 
283  .name = "libvpx",
284  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
285  .type = AVMEDIA_TYPE_VIDEO,
286  .id = AV_CODEC_ID_VP8,
287  .priv_data_size = sizeof(VPxContext),
288  .init = vp8_init,
289  .close = vpx_free,
290  .decode = vpx_decode,
292  .wrapper_name = "libvpx",
293 };
294 #endif /* CONFIG_LIBVPX_VP8_DECODER */
295 
296 #if CONFIG_LIBVPX_VP9_DECODER
297 static av_cold int vp9_init(AVCodecContext *avctx)
298 {
299  return vpx_init(avctx, &vpx_codec_vp9_dx_algo, 0);
300 }
301 
303  .name = "libvpx-vp9",
304  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
305  .type = AVMEDIA_TYPE_VIDEO,
306  .id = AV_CODEC_ID_VP9,
307  .priv_data_size = sizeof(VPxContext),
308  .init = vp9_init,
309  .close = vpx_free,
310  .decode = vpx_decode,
312  .init_static_data = ff_vp9_init_static,
314  .wrapper_name = "libvpx",
315 };
316 #endif /* CONFIG_LIBVPX_VP9_DECODER */
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:475
#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:370
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
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:267
#define AV_RB64
Definition: intreadwrite.h:164
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:104
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:479
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2148
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:164
int size
Definition: avcodec.h:1431
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:384
AVCodec ff_libvpx_vp8_decoder
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:480
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:372
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:1027
int profile
profile
Definition: avcodec.h:2843
AVCodec.
Definition: avcodec.h:3408
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:474
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
functionally identical to above
Definition: pixfmt.h:481
#define FF_PROFILE_VP9_0
Definition: avcodec.h:2926
#define img
#define CONFIG_LIBVPX_VP8_DECODER
Definition: config.h:1171
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:97
uint8_t
#define av_cold
Definition: attributes.h:82
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:473
const char data[16]
Definition: mxf.c:90
uint8_t * data
Definition: avcodec.h:1430
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
AVColorRange
MPEG vs JPEG YUV range.
Definition: pixfmt.h:496
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:373
#define av_log(a,...)
static int vpx_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: libvpxdec.c:176
AVCodec ff_libvpx_vp9_decoder
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:350
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:2929
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:371
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:387
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:2928
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:330
int width
picture width / height.
Definition: avcodec.h:1690
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:484
AVFormatContext * ctx
Definition: movenc.c:48
#define CONFIG_LIBVPX_VP9_DECODER
Definition: config.h:1172
static void error(const char *err)
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2769
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:499
#define FF_PROFILE_VP9_1
Definition: avcodec.h:2927
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:1528
av_cold void ff_vp9_init_static(AVCodec *codec)
Definition: libvpx.c:68
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
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:1518
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1891
Data found in BlockAdditional element of matroska container.
Definition: avcodec.h:1294
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:368
static const AVProfile profiles[]
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2141
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:374
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:385
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:369
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:375
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:498
static av_cold int vp8_init(AVFormatContext *s, int st_index, PayloadContext *vp8)
Definition: rtpdec_vp8.c:263
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:159
common internal api header.
common internal and external API header
static const struct @272 planes[]
void * priv_data
Definition: avcodec.h:1545
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:95
const AVProfile ff_vp9_profiles[]
Definition: profiles.c:135
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
This structure stores compressed data.
Definition: avcodec.h:1407
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959