annotate libvpxdec.c @ 12223:93e27a5401de libavcodec

Convert VP8 macroblock structures to a ring buffer. Uses a slightly nonintuitive ring buffer size of (width+height*2) to simplify addressing logic. Also split out the segmentation map to a separate structure, necessary to implement the ring buffer.
author darkshikari
date Thu, 22 Jul 2010 11:45:18 +0000
parents 6c1e78b8aba5
children 914f484bb476
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11756
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
1 /*
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
2 * Copyright (c) 2010, Google, Inc.
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
3 *
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
4 * This file is part of FFmpeg.
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
5 *
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
6 * FFmpeg is free software; you can redistribute it and/or
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
9 * version 2.1 of the License, or (at your option) any later version.
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
10 *
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
11 * FFmpeg is distributed in the hope that it will be useful,
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
14 * Lesser General Public License for more details.
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
15 *
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
17 * License along with FFmpeg; if not, write to the Free Software
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
19 */
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
20
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
21 /**
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
22 * @file
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
23 * VP8 decoder support via libvpx
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
24 */
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
25
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
26 #define VPX_CODEC_DISABLE_COMPAT 1
11758
6c1e78b8aba5 Headers for libvpx are installed into vpx subdirectory.
cehoyos
parents: 11756
diff changeset
27 #include <vpx/vpx_decoder.h>
6c1e78b8aba5 Headers for libvpx are installed into vpx subdirectory.
cehoyos
parents: 11756
diff changeset
28 #include <vpx/vp8dx.h>
11756
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
29
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
30 #include "avcodec.h"
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
31
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
32 typedef struct VP8DecoderContext {
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
33 struct vpx_codec_ctx decoder;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
34 } VP8Context;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
35
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
36 static av_cold int vp8_init(AVCodecContext *avctx)
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
37 {
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
38 VP8Context *ctx = avctx->priv_data;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
39 const struct vpx_codec_iface *iface = &vpx_codec_vp8_dx_algo;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
40 struct vpx_codec_dec_cfg deccfg = {
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
41 /* token partitions+1 would be a decent choice */
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
42 .threads = FFMIN(avctx->thread_count, 16)
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
43 };
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
44
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
45 av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
46 av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
47
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
48 if (vpx_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != VPX_CODEC_OK) {
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
49 const char *error = vpx_codec_error(&ctx->decoder);
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
50 av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
51 error);
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
52 return AVERROR(EINVAL);
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
53 }
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
54
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
55 avctx->pix_fmt = PIX_FMT_YUV420P;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
56 return 0;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
57 }
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
58
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
59 static int vp8_decode(AVCodecContext *avctx,
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
60 void *data, int *data_size, AVPacket *avpkt)
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
61 {
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
62 VP8Context *ctx = avctx->priv_data;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
63 AVFrame *picture = data;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
64 const void *iter = NULL;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
65 struct vpx_image *img;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
66
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
67 if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
68 VPX_CODEC_OK) {
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
69 const char *error = vpx_codec_error(&ctx->decoder);
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
70 const char *detail = vpx_codec_error_detail(&ctx->decoder);
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
71
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
72 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
73 if (detail)
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
74 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n",
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
75 detail);
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
76 return AVERROR_INVALIDDATA;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
77 }
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
78
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
79 if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
80 if (img->fmt != VPX_IMG_FMT_I420) {
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
81 av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d)\n",
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
82 img->fmt);
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
83 return AVERROR_INVALIDDATA;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
84 }
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
85
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
86 if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
87 av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
88 avctx->width, avctx->height, img->d_w, img->d_h);
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
89 if (avcodec_check_dimensions(avctx, img->d_w, img->d_h))
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
90 return AVERROR_INVALIDDATA;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
91 avcodec_set_dimensions(avctx, img->d_w, img->d_h);
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
92 }
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
93 picture->data[0] = img->planes[0];
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
94 picture->data[1] = img->planes[1];
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
95 picture->data[2] = img->planes[2];
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
96 picture->data[3] = NULL;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
97 picture->linesize[0] = img->stride[0];
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
98 picture->linesize[1] = img->stride[1];
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
99 picture->linesize[2] = img->stride[2];
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
100 picture->linesize[3] = 0;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
101 *data_size = sizeof(AVPicture);
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
102 }
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
103 return avpkt->size;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
104 }
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
105
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
106 static av_cold int vp8_free(AVCodecContext *avctx)
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
107 {
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
108 VP8Context *ctx = avctx->priv_data;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
109 vpx_codec_destroy(&ctx->decoder);
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
110 return 0;
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
111 }
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
112
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
113 AVCodec libvpx_decoder = {
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
114 "libvpx",
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
115 AVMEDIA_TYPE_VIDEO,
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
116 CODEC_ID_VP8,
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
117 sizeof(VP8Context),
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
118 vp8_init,
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
119 NULL, /* encode */
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
120 vp8_free,
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
121 vp8_decode,
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
122 0, /* capabilities */
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
123 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
cec5a6f0beec VP8 decoding via libvpx.
cehoyos
parents:
diff changeset
124 };