FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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  if (avctx->codec_id == AV_CODEC_ID_VP8 && img->fmt != VPX_IMG_FMT_I420)
65  return AVERROR_INVALIDDATA;
66  switch (img->fmt) {
67  case VPX_IMG_FMT_I420:
68  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
69  return 0;
70 #if CONFIG_LIBVPX_VP9_DECODER
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 #endif
110  default:
111  return AVERROR_INVALIDDATA;
112  }
113 }
114 
115 static int vp8_decode(AVCodecContext *avctx,
116  void *data, int *got_frame, AVPacket *avpkt)
117 {
118  VP8Context *ctx = avctx->priv_data;
119  AVFrame *picture = data;
120  const void *iter = NULL;
121  struct vpx_image *img;
122  int ret;
123 
124  if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
125  VPX_CODEC_OK) {
126  const char *error = vpx_codec_error(&ctx->decoder);
127  const char *detail = vpx_codec_error_detail(&ctx->decoder);
128 
129  av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
130  if (detail)
131  av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
132  detail);
133  return AVERROR_INVALIDDATA;
134  }
135 
136  if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
137  if ((ret = set_pix_fmt(avctx, img)) < 0) {
138 #ifdef VPX_IMG_FMT_HIGHBITDEPTH
139  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
140  img->fmt, img->bit_depth);
141 #else
142  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d) / bit_depth (%d)\n",
143  img->fmt, 8);
144 #endif
145  return ret;
146  }
147 
148  if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
149  av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
150  avctx->width, avctx->height, img->d_w, img->d_h);
151  ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
152  if (ret < 0)
153  return ret;
154  }
155  if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
156  return ret;
157  av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
158  img->stride, avctx->pix_fmt, img->d_w, img->d_h);
159  *got_frame = 1;
160  }
161  return avpkt->size;
162 }
163 
164 static av_cold int vp8_free(AVCodecContext *avctx)
165 {
166  VP8Context *ctx = avctx->priv_data;
167  vpx_codec_destroy(&ctx->decoder);
168  return 0;
169 }
170 
171 #if CONFIG_LIBVPX_VP8_DECODER
172 static av_cold int vp8_init(AVCodecContext *avctx)
173 {
174  return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
175 }
176 
177 AVCodec ff_libvpx_vp8_decoder = {
178  .name = "libvpx",
179  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
180  .type = AVMEDIA_TYPE_VIDEO,
181  .id = AV_CODEC_ID_VP8,
182  .priv_data_size = sizeof(VP8Context),
183  .init = vp8_init,
184  .close = vp8_free,
185  .decode = vp8_decode,
186  .capabilities = CODEC_CAP_AUTO_THREADS | CODEC_CAP_DR1,
187 };
188 #endif /* CONFIG_LIBVPX_VP8_DECODER */
189 
190 #if CONFIG_LIBVPX_VP9_DECODER
191 static av_cold int vp9_init(AVCodecContext *avctx)
192 {
193  return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
194 }
195 
196 AVCodec ff_libvpx_vp9_decoder = {
197  .name = "libvpx-vp9",
198  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
199  .type = AVMEDIA_TYPE_VIDEO,
200  .id = AV_CODEC_ID_VP9,
201  .priv_data_size = sizeof(VP8Context),
202  .init = vp9_init,
203  .close = vp8_free,
204  .decode = vp8_decode,
205  .capabilities = CODEC_CAP_AUTO_THREADS | CODEC_CAP_DR1,
207 };
208 #endif /* CONFIG_LIBVPX_VP9_DECODER */
#define NULL
Definition: coverity.c:32
#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:163
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:164
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
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:229
struct vpx_codec_ctx decoder
Definition: libvpxdec.c:37
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1161
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1442
AVCodec.
Definition: avcodec.h:3173
#define img
uint8_t
#define av_cold
Definition: attributes.h:74
planar YUV 4:4:4,36bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:278
static av_cold void init_static_data(void)
Definition: dsddec.c:82
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:787
uint8_t * data
Definition: avcodec.h:1160
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:191
#define av_log(a,...)
planar YUV 4:4:4, 30bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:170
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:175
#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:180
planar YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:166
const char * name
Name of the codec implementation.
Definition: avcodec.h:3180
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:72
static av_cold int vp8_free(AVCodecContext *avctx)
Definition: libvpxdec.c:164
#define FFMIN(a, b)
Definition: common.h:81
ret
Definition: avfilter.c:974
static const chunk_decoder decoder[8]
Definition: dfa.c:327
int width
picture width / height.
Definition: avcodec.h:1412
static av_cold int vpx_init(AVCodecContext *avctx, const struct vpx_codec_iface *iface)
Definition: libvpxdec.c:40
#define CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:876
static int vp8_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: libvpxdec.c:115
planar YUV 4:2:0,18bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:270
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2751
#define AV_LOG_INFO
Standard information.
Definition: log.h:186
enum AVCodecID codec_id
Definition: avcodec.h:1256
av_cold void ff_vp9_init_static(AVCodec *codec)
Definition: libvpx.c:25
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:191
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:1239
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1032
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:174
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:520
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:68
planar YUV 4:2:2,24bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:274
common internal api header.
common internal and external API header
void * priv_data
Definition: avcodec.h:1281
This structure stores compressed data.
Definition: avcodec.h:1137