FFmpeg  2.1.1
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 
35 typedef struct VP8DecoderContext {
36  struct vpx_codec_ctx decoder;
37 } VP8Context;
38 
39 static av_cold int vpx_init(AVCodecContext *avctx,
40  const struct vpx_codec_iface *iface)
41 {
42  VP8Context *ctx = avctx->priv_data;
43  struct vpx_codec_dec_cfg deccfg = {
44  /* token partitions+1 would be a decent choice */
45  .threads = FFMIN(avctx->thread_count, 16)
46  };
47 
48  av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
49  av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
50 
51  if (vpx_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != VPX_CODEC_OK) {
52  const char *error = vpx_codec_error(&ctx->decoder);
53  av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
54  error);
55  return AVERROR(EINVAL);
56  }
57 
58  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
59  return 0;
60 }
61 
62 static int vp8_decode(AVCodecContext *avctx,
63  void *data, int *got_frame, AVPacket *avpkt)
64 {
65  VP8Context *ctx = avctx->priv_data;
66  AVFrame *picture = data;
67  const void *iter = NULL;
68  struct vpx_image *img;
69  int ret;
70 
71  if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
72  VPX_CODEC_OK) {
73  const char *error = vpx_codec_error(&ctx->decoder);
74  const char *detail = vpx_codec_error_detail(&ctx->decoder);
75 
76  av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
77  if (detail)
78  av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
79  detail);
80  return AVERROR_INVALIDDATA;
81  }
82 
83  if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
84  if (img->fmt != VPX_IMG_FMT_I420) {
85  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d)\n",
86  img->fmt);
87  return AVERROR_INVALIDDATA;
88  }
89 
90  if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
91  av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
92  avctx->width, avctx->height, img->d_w, img->d_h);
93  if (av_image_check_size(img->d_w, img->d_h, 0, avctx))
94  return AVERROR_INVALIDDATA;
95  avcodec_set_dimensions(avctx, img->d_w, img->d_h);
96  }
97  if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
98  return ret;
99  av_image_copy(picture->data, picture->linesize, (const uint8_t **)img->planes,
100  img->stride, avctx->pix_fmt, img->d_w, img->d_h);
101  *got_frame = 1;
102  }
103  return avpkt->size;
104 }
105 
106 static av_cold int vp8_free(AVCodecContext *avctx)
107 {
108  VP8Context *ctx = avctx->priv_data;
109  vpx_codec_destroy(&ctx->decoder);
110  return 0;
111 }
112 
113 #if CONFIG_LIBVPX_VP8_DECODER
114 static av_cold int vp8_init(AVCodecContext *avctx)
115 {
116  return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
117 }
118 
119 AVCodec ff_libvpx_vp8_decoder = {
120  .name = "libvpx",
121  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
122  .type = AVMEDIA_TYPE_VIDEO,
123  .id = AV_CODEC_ID_VP8,
124  .priv_data_size = sizeof(VP8Context),
125  .init = vp8_init,
126  .close = vp8_free,
127  .decode = vp8_decode,
128  .capabilities = CODEC_CAP_AUTO_THREADS | CODEC_CAP_DR1,
129 };
130 #endif /* CONFIG_LIBVPX_VP8_DECODER */
131 
132 #if CONFIG_LIBVPX_VP9_DECODER
133 static av_cold int vp9_init(AVCodecContext *avctx)
134 {
135  return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
136 }
137 
138 AVCodec ff_libvpx_vp9_decoder = {
139  .name = "libvpx-vp9",
140  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
141  .type = AVMEDIA_TYPE_VIDEO,
142  .id = AV_CODEC_ID_VP9,
143  .priv_data_size = sizeof(VP8Context),
144  .init = vp9_init,
145  .close = vp8_free,
146  .decode = vp8_decode,
148 };
149 #endif /* CONFIG_LIBVPX_VP9_DECODER */
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
struct vpx_codec_ctx decoder
Definition: libvpxdec.c:36
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:233
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
#define img
uint8_t
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:742
const char data[16]
Definition: mxf.c:68
#define AV_LOG_VERBOSE
Detailed information.
Definition: avcodec.h:4163
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
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:257
static av_cold int vp8_free(AVCodecContext *avctx)
Definition: libvpxdec.c:106
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:231
ret
Definition: avfilter.c:961
static const chunk_decoder decoder[8]
Definition: dfa.c:325
int width
picture width / height.
Definition: avcodec.h:1314
static av_cold int vpx_init(AVCodecContext *avctx, const struct vpx_codec_iface *iface)
Definition: libvpxdec.c:39
#define CODEC_CAP_AUTO_THREADS
Codec supports avctx-&gt;thread_count == 0 (auto).
Definition: avcodec.h:823
#define FFMIN(a, b)
Definition: avcodec.h:925
static int vp8_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: libvpxdec.c:62
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2596
planar YUV 4:2:0, 12bpp, (1 Cr &amp; Cb sample per 2x2 Y samples)
Definition: avcodec.h:4534
#define AV_LOG_INFO
Standard information.
Definition: avcodec.h:4158
main external API structure.
Definition: avcodec.h:1146
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:941
uint8_t * data
Definition: avcodec.h:1063
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
void * priv_data
Definition: avcodec.h:1182
common internal api header.
#define CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: avcodec.h:797
#define AVERROR_INVALIDDATA
#define AVERROR(e)
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
This structure stores compressed data.
Definition: avcodec.h:1040
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107