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 
34 typedef struct VP8DecoderContext {
35  struct vpx_codec_ctx decoder;
36 } VP8Context;
37 
38 static av_cold int vp8_init(AVCodecContext *avctx)
39 {
40  VP8Context *ctx = avctx->priv_data;
41  const struct vpx_codec_iface *iface = &vpx_codec_vp8_dx_algo;
42  struct vpx_codec_dec_cfg deccfg = {
43  /* token partitions+1 would be a decent choice */
44  .threads = FFMIN(avctx->thread_count, 16)
45  };
46 
47  av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
48  av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
49 
50  if (vpx_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != VPX_CODEC_OK) {
51  const char *error = vpx_codec_error(&ctx->decoder);
52  av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
53  error);
54  return AVERROR(EINVAL);
55  }
56 
57  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
58  return 0;
59 }
60 
61 static int vp8_decode(AVCodecContext *avctx,
62  void *data, int *got_frame, AVPacket *avpkt)
63 {
64  VP8Context *ctx = avctx->priv_data;
65  AVFrame *picture = data;
66  const void *iter = NULL;
67  struct vpx_image *img;
68 
69  if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
70  VPX_CODEC_OK) {
71  const char *error = vpx_codec_error(&ctx->decoder);
72  const char *detail = vpx_codec_error_detail(&ctx->decoder);
73 
74  av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
75  if (detail)
76  av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
77  detail);
78  return AVERROR_INVALIDDATA;
79  }
80 
81  if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
82  if (img->fmt != VPX_IMG_FMT_I420) {
83  av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d)\n",
84  img->fmt);
85  return AVERROR_INVALIDDATA;
86  }
87 
88  if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
89  av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
90  avctx->width, avctx->height, img->d_w, img->d_h);
91  if (av_image_check_size(img->d_w, img->d_h, 0, avctx))
92  return AVERROR_INVALIDDATA;
93  avcodec_set_dimensions(avctx, img->d_w, img->d_h);
94  }
95  picture->data[0] = img->planes[0];
96  picture->data[1] = img->planes[1];
97  picture->data[2] = img->planes[2];
98  picture->data[3] = NULL;
99  picture->linesize[0] = img->stride[0];
100  picture->linesize[1] = img->stride[1];
101  picture->linesize[2] = img->stride[2];
102  picture->linesize[3] = 0;
103  *got_frame = 1;
104  }
105  return avpkt->size;
106 }
107 
108 static av_cold int vp8_free(AVCodecContext *avctx)
109 {
110  VP8Context *ctx = avctx->priv_data;
111  vpx_codec_destroy(&ctx->decoder);
112  return 0;
113 }
114 
116  .name = "libvpx",
117  .type = AVMEDIA_TYPE_VIDEO,
118  .id = AV_CODEC_ID_VP8,
119  .priv_data_size = sizeof(VP8Context),
120  .init = vp8_init,
121  .close = vp8_free,
122  .decode = vp8_decode,
123  .capabilities = CODEC_CAP_AUTO_THREADS,
124  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
125 };
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
misc image utilities
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:149
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
AVCodec.
Definition: avcodec.h:2960
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
struct vpx_codec_ctx decoder
Definition: libvpxdec.c:35
struct VP8DecoderContext VP8Context
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static av_cold int vp8_free(AVCodecContext *avctx)
Definition: libvpxdec.c:108
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:220
static AVFrame * picture
int width
picture width / height.
Definition: avcodec.h:1508
static int vp8_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: libvpxdec.c:61
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2733
NULL
Definition: eval.c:52
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
static av_cold int vp8_init(AVCodecContext *avctx)
Definition: libvpxdec.c:38
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
AVCodec ff_libvpx_decoder
Definition: libvpxdec.c:115
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
common internal and external API header
void * priv_data
Definition: avcodec.h:1382
This structure stores compressed data.
Definition: avcodec.h:898