vble: Do not abort decoding when version is not 1
[libav.git] / libavcodec / vble.c
CommitLineData
c433a3f9
DB
1/*
2 * VBLE Decoder
3 * Copyright (c) 2011 Derek Buitenhuis
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * VBLE Decoder
25 */
26
aaf47bcd 27#define BITSTREAM_READER_LE
c433a3f9
DB
28
29#include "avcodec.h"
adfdcf87 30#include "dsputil.h"
c433a3f9 31#include "get_bits.h"
d5c62122 32#include "mathops.h"
c433a3f9
DB
33
34typedef struct {
35 AVCodecContext *avctx;
adfdcf87 36 DSPContext dsp;
c433a3f9
DB
37
38 int size;
e46abbcf 39 uint8_t *val; /* First holds the lengths of vlc symbols and then their values */
c433a3f9
DB
40} VBLEContext;
41
42static uint8_t vble_read_reverse_unary(GetBitContext *gb)
43{
44 /* At most we need to read 9 bits total to get indices up to 8 */
45 uint8_t val = show_bits(gb, 8);
46
47 if (val) {
d5c62122 48 val = 7 - av_log2_16bit(ff_reverse[val]);
c433a3f9
DB
49 skip_bits(gb, val + 1);
50 return val;
51 } else {
52 skip_bits(gb, 8);
53 if (get_bits1(gb))
54 return 8;
55 }
56
57 /* Return something larger than 8 on error */
58 return UINT8_MAX;
59}
60
61static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
62{
63 int i;
64
65 /* Read all the lengths in first */
66 for (i = 0; i < ctx->size; i++) {
e46abbcf 67 ctx->val[i] = vble_read_reverse_unary(gb);
c433a3f9 68
e46abbcf 69 if (ctx->val[i] == UINT8_MAX)
c433a3f9
DB
70 return -1;
71 }
72
c433a3f9
DB
73 for (i = 0; i < ctx->size; i++) {
74 /* Check we have enough bits left */
e46abbcf 75 if (get_bits_left(gb) < ctx->val[i])
c433a3f9
DB
76 return -1;
77
78 /* get_bits can't take a length of 0 */
e46abbcf
DB
79 if (ctx->val[i])
80 ctx->val[i] = (1 << ctx->val[i]) + get_bits(gb, ctx->val[i]) - 1;
c433a3f9
DB
81 }
82
83 return 0;
84}
85
86static void vble_restore_plane(VBLEContext *ctx, int plane, int offset,
87 int width, int height)
88{
89 AVFrame *pic = ctx->avctx->coded_frame;
90 uint8_t *dst = pic->data[plane];
91 uint8_t *val = ctx->val + offset;
c433a3f9 92 int stride = pic->linesize[plane];
adfdcf87 93 int i, j, left, left_top;
c433a3f9
DB
94
95 for (i = 0; i < height; i++) {
adfdcf87
MN
96 for (j = 0; j < width; j++)
97 val[j] = (val[j] >> 1) ^ -(val[j] & 1);
98
99 if (i) {
100 left = 0;
101 left_top = dst[-stride];
102 ctx->dsp.add_hfyu_median_prediction(dst, dst-stride, val,
103 width, &left, &left_top);
104 } else {
105 dst[0] = val[0];
106 for (j = 1; j < width; j++)
107 dst[j] = val[j] + dst[j - 1];
c433a3f9
DB
108 }
109 dst += stride;
110 val += width;
c433a3f9
DB
111 }
112}
113
114static int vble_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
115 AVPacket *avpkt)
116{
117 VBLEContext *ctx = avctx->priv_data;
118 AVFrame *pic = avctx->coded_frame;
119 GetBitContext gb;
120 const uint8_t *src = avpkt->data;
121 int version;
122 int offset = 0;
123 int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
124
125 pic->reference = 0;
126
127 /* Clear buffer if need be */
128 if (pic->data[0])
129 avctx->release_buffer(avctx, pic);
130
131 /* Allocate buffer */
132 if (avctx->get_buffer(avctx, pic) < 0) {
133 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
134 return AVERROR(ENOMEM);
135 }
136
137 /* Set flags */
138 pic->key_frame = 1;
f3d02a8b 139 pic->pict_type = AV_PICTURE_TYPE_I;
c433a3f9
DB
140
141 /* Version should always be 1 */
142 version = AV_RL32(src);
143
f5fa0366
PB
144 if (version != 1)
145 av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
c433a3f9
DB
146
147 init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
148
149 /* Unpack */
150 if (vble_unpack(ctx, &gb) < 0) {
151 av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
152 return AVERROR_INVALIDDATA;
153 }
154
155 /* Restore planes. Should be almost identical to Huffyuv's. */
156 vble_restore_plane(ctx, 0, offset, avctx->width, avctx->height);
157
158 /* Chroma */
317ea97b 159 if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
c433a3f9
DB
160 offset += avctx->width * avctx->height;
161 vble_restore_plane(ctx, 1, offset, width_uv, height_uv);
162
163 offset += width_uv * height_uv;
164 vble_restore_plane(ctx, 2, offset, width_uv, height_uv);
165 }
166
167 *data_size = sizeof(AVFrame);
168 *(AVFrame *)data = *pic;
169
170 return avpkt->size;
171}
172
173static av_cold int vble_decode_close(AVCodecContext *avctx)
174{
175 VBLEContext *ctx = avctx->priv_data;
176 AVFrame *pic = avctx->coded_frame;
177
178 if (pic->data[0])
179 avctx->release_buffer(avctx, pic);
180
181 av_freep(&avctx->coded_frame);
c433a3f9
DB
182 av_freep(&ctx->val);
183
184 return 0;
185}
186
c433a3f9
DB
187static av_cold int vble_decode_init(AVCodecContext *avctx)
188{
189 VBLEContext *ctx = avctx->priv_data;
190
191 /* Stash for later use */
192 ctx->avctx = avctx;
9cf0841e 193 ff_dsputil_init(&ctx->dsp, avctx);
c433a3f9 194
716d413c 195 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
c433a3f9
DB
196 avctx->bits_per_raw_sample = 8;
197 avctx->coded_frame = avcodec_alloc_frame();
198
29ae0565
JG
199 if (!avctx->coded_frame) {
200 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
201 return AVERROR(ENOMEM);
202 }
c433a3f9
DB
203
204 ctx->size = avpicture_get_size(avctx->pix_fmt,
205 avctx->width, avctx->height);
206
c433a3f9
DB
207 ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
208
29ae0565
JG
209 if (!ctx->val) {
210 av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
211 vble_decode_close(avctx);
212 return AVERROR(ENOMEM);
213 }
c433a3f9
DB
214
215 return 0;
216}
217
218AVCodec ff_vble_decoder = {
219 .name = "vble",
220 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 221 .id = AV_CODEC_ID_VBLE,
c433a3f9
DB
222 .priv_data_size = sizeof(VBLEContext),
223 .init = vble_decode_init,
224 .close = vble_decode_close,
225 .decode = vble_decode_frame,
226 .capabilities = CODEC_CAP_DR1,
227 .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
228};