lavc: add a wrapper for AVCodecContext.get_buffer().
[libav.git] / libavcodec / bmv.c
1 /*
2 * Discworld II BMV video and audio decoder
3 * Copyright (c) 2011 Konstantin Shishkov
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 #include "libavutil/channel_layout.h"
23 #include "avcodec.h"
24 #include "bytestream.h"
25 #include "internal.h"
26
27 enum BMVFlags{
28 BMV_NOP = 0,
29 BMV_END,
30 BMV_DELTA,
31 BMV_INTRA,
32
33 BMV_SCROLL = 0x04,
34 BMV_PALETTE = 0x08,
35 BMV_COMMAND = 0x10,
36 BMV_AUDIO = 0x20,
37 BMV_EXT = 0x40,
38 BMV_PRINT = 0x80
39 };
40
41 #define SCREEN_WIDE 640
42 #define SCREEN_HIGH 429
43
44 typedef struct BMVDecContext {
45 AVCodecContext *avctx;
46 AVFrame pic;
47
48 uint8_t *frame, frame_base[SCREEN_WIDE * (SCREEN_HIGH + 1)];
49 uint32_t pal[256];
50 const uint8_t *stream;
51 } BMVDecContext;
52
53 #define NEXT_BYTE(v) v = forward ? v + 1 : v - 1;
54
55 static int decode_bmv_frame(const uint8_t *source, int src_len, uint8_t *frame, int frame_off)
56 {
57 unsigned val, saved_val = 0;
58 int tmplen = src_len;
59 const uint8_t *src, *source_end = source + src_len;
60 uint8_t *frame_end = frame + SCREEN_WIDE * SCREEN_HIGH;
61 uint8_t *dst, *dst_end;
62 int len, mask;
63 int forward = (frame_off <= -SCREEN_WIDE) || (frame_off >= 0);
64 int read_two_nibbles, flag;
65 int advance_mode;
66 int mode = 0;
67 int i;
68
69 if (src_len <= 0)
70 return -1;
71
72 if (forward) {
73 src = source;
74 dst = frame;
75 dst_end = frame_end;
76 } else {
77 src = source + src_len - 1;
78 dst = frame_end - 1;
79 dst_end = frame - 1;
80 }
81 for (;;) {
82 int shift = 0;
83 flag = 0;
84
85 /* The mode/len decoding is a bit strange:
86 * values are coded as variable-length codes with nibble units,
87 * code end is signalled by two top bits in the nibble being nonzero.
88 * And since data is bytepacked and we read two nibbles at a time,
89 * we may get a nibble belonging to the next code.
90 * Hence this convoluted loop.
91 */
92 if (!mode || (tmplen == 4)) {
93 if (src < source || src >= source_end)
94 return -1;
95 val = *src;
96 read_two_nibbles = 1;
97 } else {
98 val = saved_val;
99 read_two_nibbles = 0;
100 }
101 if (!(val & 0xC)) {
102 for (;;) {
103 if (!read_two_nibbles) {
104 if (src < source || src >= source_end)
105 return -1;
106 shift += 2;
107 val |= *src << shift;
108 if (*src & 0xC)
109 break;
110 }
111 // two upper bits of the nibble is zero,
112 // so shift top nibble value down into their place
113 read_two_nibbles = 0;
114 shift += 2;
115 mask = (1 << shift) - 1;
116 val = ((val >> 2) & ~mask) | (val & mask);
117 NEXT_BYTE(src);
118 if ((val & (0xC << shift))) {
119 flag = 1;
120 break;
121 }
122 }
123 } else if (mode) {
124 flag = tmplen != 4;
125 }
126 if (flag) {
127 tmplen = 4;
128 } else {
129 saved_val = val >> (4 + shift);
130 tmplen = 0;
131 val &= (1 << (shift + 4)) - 1;
132 NEXT_BYTE(src);
133 }
134 advance_mode = val & 1;
135 len = (val >> 1) - 1;
136 mode += 1 + advance_mode;
137 if (mode >= 4)
138 mode -= 3;
139 if (FFABS(dst_end - dst) < len)
140 return -1;
141 switch (mode) {
142 case 1:
143 if (forward) {
144 if (dst - frame + SCREEN_WIDE < frame_off ||
145 dst - frame + SCREEN_WIDE + frame_off < 0 ||
146 frame_end - dst < frame_off + len ||
147 frame_end - dst < len)
148 return -1;
149 for (i = 0; i < len; i++)
150 dst[i] = dst[frame_off + i];
151 dst += len;
152 } else {
153 dst -= len;
154 if (dst - frame + SCREEN_WIDE < frame_off ||
155 dst - frame + SCREEN_WIDE + frame_off < 0 ||
156 frame_end - dst < frame_off + len ||
157 frame_end - dst < len)
158 return -1;
159 for (i = len - 1; i >= 0; i--)
160 dst[i] = dst[frame_off + i];
161 }
162 break;
163 case 2:
164 if (forward) {
165 if (source + src_len - src < len)
166 return -1;
167 memcpy(dst, src, len);
168 dst += len;
169 src += len;
170 } else {
171 if (src - source < len)
172 return -1;
173 dst -= len;
174 src -= len;
175 memcpy(dst, src, len);
176 }
177 break;
178 case 3:
179 val = forward ? dst[-1] : dst[1];
180 if (forward) {
181 memset(dst, val, len);
182 dst += len;
183 } else {
184 dst -= len;
185 memset(dst, val, len);
186 }
187 break;
188 default:
189 break;
190 }
191 if (dst == dst_end)
192 return 0;
193 }
194 return 0;
195 }
196
197 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
198 {
199 BMVDecContext * const c = avctx->priv_data;
200 int type, scr_off;
201 int i, ret;
202 uint8_t *srcptr, *outptr;
203
204 c->stream = pkt->data;
205 type = bytestream_get_byte(&c->stream);
206 if (type & BMV_AUDIO) {
207 int blobs = bytestream_get_byte(&c->stream);
208 if (pkt->size < blobs * 65 + 2) {
209 av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
210 return AVERROR_INVALIDDATA;
211 }
212 c->stream += blobs * 65;
213 }
214 if (type & BMV_COMMAND) {
215 int command_size = (type & BMV_PRINT) ? 8 : 10;
216 if (c->stream - pkt->data + command_size > pkt->size) {
217 av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
218 return AVERROR_INVALIDDATA;
219 }
220 c->stream += command_size;
221 }
222 if (type & BMV_PALETTE) {
223 if (c->stream - pkt->data > pkt->size - 768) {
224 av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
225 return AVERROR_INVALIDDATA;
226 }
227 for (i = 0; i < 256; i++)
228 c->pal[i] = bytestream_get_be24(&c->stream);
229 }
230 if (type & BMV_SCROLL) {
231 if (c->stream - pkt->data > pkt->size - 2) {
232 av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
233 return AVERROR_INVALIDDATA;
234 }
235 scr_off = (int16_t)bytestream_get_le16(&c->stream);
236 } else if ((type & BMV_INTRA) == BMV_INTRA) {
237 scr_off = -640;
238 } else {
239 scr_off = 0;
240 }
241
242 if (c->pic.data[0])
243 avctx->release_buffer(avctx, &c->pic);
244
245 c->pic.reference = 3;
246 if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
247 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
248 return ret;
249 }
250
251 if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
252 av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
253 return AVERROR_INVALIDDATA;
254 }
255
256 memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
257 c->pic.palette_has_changed = type & BMV_PALETTE;
258
259 outptr = c->pic.data[0];
260 srcptr = c->frame;
261
262 for (i = 0; i < avctx->height; i++) {
263 memcpy(outptr, srcptr, avctx->width);
264 srcptr += avctx->width;
265 outptr += c->pic.linesize[0];
266 }
267
268 *data_size = sizeof(AVFrame);
269 *(AVFrame*)data = c->pic;
270
271 /* always report that the buffer was completely consumed */
272 return pkt->size;
273 }
274
275 static av_cold int decode_init(AVCodecContext *avctx)
276 {
277 BMVDecContext * const c = avctx->priv_data;
278
279 c->avctx = avctx;
280 avctx->pix_fmt = AV_PIX_FMT_PAL8;
281
282 c->frame = c->frame_base + 640;
283
284 return 0;
285 }
286
287 static av_cold int decode_end(AVCodecContext *avctx)
288 {
289 BMVDecContext *c = avctx->priv_data;
290
291 if (c->pic.data[0])
292 avctx->release_buffer(avctx, &c->pic);
293
294 return 0;
295 }
296
297 typedef struct BMVAudioDecContext {
298 AVFrame frame;
299 } BMVAudioDecContext;
300
301 static const int bmv_aud_mults[16] = {
302 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
303 };
304
305 static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
306 {
307 BMVAudioDecContext *c = avctx->priv_data;
308
309 avctx->channels = 2;
310 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
311 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
312
313 avcodec_get_frame_defaults(&c->frame);
314 avctx->coded_frame = &c->frame;
315
316 return 0;
317 }
318
319 static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
320 int *got_frame_ptr, AVPacket *avpkt)
321 {
322 BMVAudioDecContext *c = avctx->priv_data;
323 const uint8_t *buf = avpkt->data;
324 int buf_size = avpkt->size;
325 int blocks = 0, total_blocks, i;
326 int ret;
327 int16_t *output_samples;
328 int scale[2];
329
330 total_blocks = *buf++;
331 if (buf_size < total_blocks * 65 + 1) {
332 av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
333 total_blocks * 65 + 1, buf_size);
334 return AVERROR_INVALIDDATA;
335 }
336
337 /* get output buffer */
338 c->frame.nb_samples = total_blocks * 32;
339 if ((ret = ff_get_buffer(avctx, &c->frame)) < 0) {
340 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
341 return ret;
342 }
343 output_samples = (int16_t *)c->frame.data[0];
344
345 for (blocks = 0; blocks < total_blocks; blocks++) {
346 uint8_t code = *buf++;
347 code = (code >> 1) | (code << 7);
348 scale[0] = bmv_aud_mults[code & 0xF];
349 scale[1] = bmv_aud_mults[code >> 4];
350 for (i = 0; i < 32; i++) {
351 *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
352 *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
353 }
354 }
355
356 *got_frame_ptr = 1;
357 *(AVFrame *)data = c->frame;
358
359 return buf_size;
360 }
361
362 AVCodec ff_bmv_video_decoder = {
363 .name = "bmv_video",
364 .type = AVMEDIA_TYPE_VIDEO,
365 .id = AV_CODEC_ID_BMV_VIDEO,
366 .priv_data_size = sizeof(BMVDecContext),
367 .init = decode_init,
368 .close = decode_end,
369 .decode = decode_frame,
370 .capabilities = CODEC_CAP_DR1,
371 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
372 };
373
374 AVCodec ff_bmv_audio_decoder = {
375 .name = "bmv_audio",
376 .type = AVMEDIA_TYPE_AUDIO,
377 .id = AV_CODEC_ID_BMV_AUDIO,
378 .priv_data_size = sizeof(BMVAudioDecContext),
379 .init = bmv_aud_decode_init,
380 .decode = bmv_aud_decode_frame,
381 .capabilities = CODEC_CAP_DR1,
382 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
383 };