Libav
libvpxdec.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Google, Inc.
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
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 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  avctx->pix_fmt = ff_vpx_imgfmt_to_pixfmt(img->fmt);
85  if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
86  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d)\n",
87  img->fmt);
88  return AVERROR_INVALIDDATA;
89  }
90 
91  if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
92  av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
93  avctx->width, avctx->height, img->d_w, img->d_h);
94  ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
95  if (ret < 0)
96  return ret;
97  }
98  if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
99  return ret;
100  av_image_copy(picture->data, picture->linesize, (const uint8_t **) img->planes,
101  img->stride, avctx->pix_fmt, img->d_w, img->d_h);
102 #if VPX_IMAGE_ABI_VERSION >= 4
103  switch (img->range) {
104  case VPX_CR_STUDIO_RANGE:
105  picture->color_range = AVCOL_RANGE_MPEG;
106  break;
107  case VPX_CR_FULL_RANGE:
108  picture->color_range = AVCOL_RANGE_JPEG;
109  break;
110  }
111 #endif
112  *got_frame = 1;
113  }
114  return avpkt->size;
115 }
116 
117 static av_cold int vp8_free(AVCodecContext *avctx)
118 {
119  VP8Context *ctx = avctx->priv_data;
120  vpx_codec_destroy(&ctx->decoder);
121  return 0;
122 }
123 
124 #if CONFIG_LIBVPX_VP8_DECODER
125 static av_cold int vp8_init(AVCodecContext *avctx)
126 {
127  return vpx_init(avctx, &vpx_codec_vp8_dx_algo);
128 }
129 
130 AVCodec ff_libvpx_vp8_decoder = {
131  .name = "libvpx",
132  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
133  .type = AVMEDIA_TYPE_VIDEO,
134  .id = AV_CODEC_ID_VP8,
135  .priv_data_size = sizeof(VP8Context),
136  .init = vp8_init,
137  .close = vp8_free,
138  .decode = vp8_decode,
140 };
141 #endif /* CONFIG_LIBVPX_VP8_DECODER */
142 
143 #if CONFIG_LIBVPX_VP9_DECODER
144 static av_cold int vp9_init(AVCodecContext *avctx)
145 {
146  return vpx_init(avctx, &vpx_codec_vp9_dx_algo);
147 }
148 
149 AVCodec ff_libvpx_vp9_decoder = {
150  .name = "libvpx-vp9",
151  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
152  .type = AVMEDIA_TYPE_VIDEO,
153  .id = AV_CODEC_ID_VP9,
154  .priv_data_size = sizeof(VP8Context),
155  .init = vp9_init,
156  .close = vp8_free,
157  .decode = vp8_decode,
158  .capabilities = AV_CODEC_CAP_AUTO_THREADS,
159 };
160 #endif /* CONFIG_LIBVPX_VP9_DECODER */
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
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:134
struct vpx_codec_ctx decoder
Definition: libvpxdec.c:37
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:905
enum AVPixelFormat ff_vpx_imgfmt_to_pixfmt(vpx_img_fmt_t img)
Definition: libvpx.c:25
AVCodec.
Definition: avcodec.h:3120
uint8_t
#define av_cold
Definition: attributes.h:66
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:1346
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#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:148
enum AVColorRange color_range
Definition: frame.h:351
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
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:268
static av_cold int vp8_free(AVCodecContext *avctx)
Definition: libvpxdec.c:117
#define FFMIN(a, b)
Definition: common.h:66
static const chunk_decoder decoder[8]
Definition: dfa.c:322
int width
picture width / height.
Definition: avcodec.h:1580
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
static av_cold int vpx_init(AVCodecContext *avctx, const struct vpx_codec_iface *iface)
Definition: libvpxdec.c:40
AVFormatContext * ctx
Definition: movenc.c:48
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:2806
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:365
NULL
Definition: eval.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
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:1409
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:364
static av_cold int vp8_init(AVFormatContext *s, int st_index, PayloadContext *vp8)
Definition: rtpdec_vp8.c:263
common internal api header.
common internal and external API header
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
void * priv_data
Definition: avcodec.h:1451
This structure stores compressed data.
Definition: avcodec.h:1323
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838