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