]> git.sesse.net Git - ffmpeg/blob - libavcodec/bmv.c
Vivo demuxer
[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 "libavutil/channel_layout.h"
23 #include "avcodec.h"
24 #include "bytestream.h"
25 #include "libavutil/avassert.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(shift>22)
104                     return -1;
105                 if (!read_two_nibbles) {
106                     if (src < source || src >= source_end)
107                         return -1;
108                     shift += 2;
109                     val |= *src << shift;
110                     if (*src & 0xC)
111                         break;
112                 }
113                 // two upper bits of the nibble is zero,
114                 // so shift top nibble value down into their place
115                 read_two_nibbles = 0;
116                 shift += 2;
117                 mask = (1 << shift) - 1;
118                 val = ((val >> 2) & ~mask) | (val & mask);
119                 NEXT_BYTE(src);
120                 if ((val & (0xC << shift))) {
121                     flag = 1;
122                     break;
123                 }
124             }
125         } else if (mode) {
126             flag = tmplen != 4;
127         }
128         if (flag) {
129             tmplen = 4;
130         } else {
131             saved_val = val >> (4 + shift);
132             tmplen = 0;
133             val &= (1 << (shift + 4)) - 1;
134             NEXT_BYTE(src);
135         }
136         advance_mode = val & 1;
137         len = (val >> 1) - 1;
138         av_assert0(len>0);
139         mode += 1 + advance_mode;
140         if (mode >= 4)
141             mode -= 3;
142         if (FFABS(dst_end - dst) < len)
143             return -1;
144         switch (mode) {
145         case 1:
146             if (forward) {
147                 if (dst - frame + SCREEN_WIDE < frame_off ||
148                         dst - frame + SCREEN_WIDE + frame_off < 0 ||
149                         frame_end - dst < frame_off + len ||
150                         frame_end - dst < len)
151                     return -1;
152                 for (i = 0; i < len; i++)
153                     dst[i] = dst[frame_off + i];
154                 dst += len;
155             } else {
156                 dst -= len;
157                 if (dst - frame + SCREEN_WIDE < frame_off ||
158                         dst - frame + SCREEN_WIDE + frame_off < 0 ||
159                         frame_end - dst < frame_off + len ||
160                         frame_end - dst < len)
161                     return -1;
162                 for (i = len - 1; i >= 0; i--)
163                     dst[i] = dst[frame_off + i];
164             }
165             break;
166         case 2:
167             if (forward) {
168                 if (source + src_len - src < len)
169                     return -1;
170                 memcpy(dst, src, len);
171                 dst += len;
172                 src += len;
173             } else {
174                 if (src - source < len)
175                     return -1;
176                 dst -= len;
177                 src -= len;
178                 memcpy(dst, src, len);
179             }
180             break;
181         case 3:
182             val = forward ? dst[-1] : dst[1];
183             if (forward) {
184                 memset(dst, val, len);
185                 dst += len;
186             } else {
187                 dst -= len;
188                 memset(dst, val, len);
189             }
190             break;
191         }
192         if (dst == dst_end)
193             return 0;
194     }
195     return 0;
196 }
197
198 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
199 {
200     BMVDecContext * const c = avctx->priv_data;
201     int type, scr_off;
202     int i, ret;
203     uint8_t *srcptr, *outptr;
204
205     c->stream = pkt->data;
206     type = bytestream_get_byte(&c->stream);
207     if (type & BMV_AUDIO) {
208         int blobs = bytestream_get_byte(&c->stream);
209         if (pkt->size < blobs * 65 + 2) {
210             av_log(avctx, AV_LOG_ERROR, "Audio data doesn't fit in frame\n");
211             return AVERROR_INVALIDDATA;
212         }
213         c->stream += blobs * 65;
214     }
215     if (type & BMV_COMMAND) {
216         int command_size = (type & BMV_PRINT) ? 8 : 10;
217         if (c->stream - pkt->data + command_size > pkt->size) {
218             av_log(avctx, AV_LOG_ERROR, "Command data doesn't fit in frame\n");
219             return AVERROR_INVALIDDATA;
220         }
221         c->stream += command_size;
222     }
223     if (type & BMV_PALETTE) {
224         if (c->stream - pkt->data > pkt->size - 768) {
225             av_log(avctx, AV_LOG_ERROR, "Palette data doesn't fit in frame\n");
226             return AVERROR_INVALIDDATA;
227         }
228         for (i = 0; i < 256; i++)
229             c->pal[i] = 0xFFU << 24 | bytestream_get_be24(&c->stream);
230     }
231     if (type & BMV_SCROLL) {
232         if (c->stream - pkt->data > pkt->size - 2) {
233             av_log(avctx, AV_LOG_ERROR, "Screen offset data doesn't fit in frame\n");
234             return AVERROR_INVALIDDATA;
235         }
236         scr_off = (int16_t)bytestream_get_le16(&c->stream);
237     } else if ((type & BMV_INTRA) == BMV_INTRA) {
238         scr_off = -640;
239     } else {
240         scr_off = 0;
241     }
242
243     if (c->pic.data[0])
244         avctx->release_buffer(avctx, &c->pic);
245
246     c->pic.reference = 3;
247     if ((ret = avctx->get_buffer(avctx, &c->pic)) < 0) {
248         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
249         return ret;
250     }
251
252     if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
253         av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
254         return AVERROR_INVALIDDATA;
255     }
256
257     memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
258     c->pic.palette_has_changed = type & BMV_PALETTE;
259
260     outptr = c->pic.data[0];
261     srcptr = c->frame;
262
263     for (i = 0; i < avctx->height; i++) {
264         memcpy(outptr, srcptr, avctx->width);
265         srcptr += avctx->width;
266         outptr += c->pic.linesize[0];
267     }
268
269     *data_size = sizeof(AVFrame);
270     *(AVFrame*)data = c->pic;
271
272     /* always report that the buffer was completely consumed */
273     return pkt->size;
274 }
275
276 static av_cold int decode_init(AVCodecContext *avctx)
277 {
278     BMVDecContext * const c = avctx->priv_data;
279
280     c->avctx = avctx;
281     avctx->pix_fmt = AV_PIX_FMT_PAL8;
282
283     if (avctx->width != SCREEN_WIDE || avctx->height != SCREEN_HIGH) {
284         av_log(avctx, AV_LOG_ERROR, "Invalid dimension %dx%d\n", avctx->width, avctx->height);
285         return AVERROR_INVALIDDATA;
286     }
287
288     c->frame = c->frame_base + 640;
289
290     return 0;
291 }
292
293 static av_cold int decode_end(AVCodecContext *avctx)
294 {
295     BMVDecContext *c = avctx->priv_data;
296
297     if (c->pic.data[0])
298         avctx->release_buffer(avctx, &c->pic);
299
300     return 0;
301 }
302
303 typedef struct BMVAudioDecContext {
304     AVFrame frame;
305 } BMVAudioDecContext;
306
307 static const int bmv_aud_mults[16] = {
308     16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
309 };
310
311 static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
312 {
313     BMVAudioDecContext *c = avctx->priv_data;
314
315     avctx->channels       = 2;
316     avctx->channel_layout = AV_CH_LAYOUT_STEREO;
317     avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
318
319     avcodec_get_frame_defaults(&c->frame);
320     avctx->coded_frame = &c->frame;
321
322     return 0;
323 }
324
325 static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
326                                 int *got_frame_ptr, AVPacket *avpkt)
327 {
328     BMVAudioDecContext *c = avctx->priv_data;
329     const uint8_t *buf = avpkt->data;
330     int buf_size = avpkt->size;
331     int blocks = 0, total_blocks, i;
332     int ret;
333     int16_t *output_samples;
334     int scale[2];
335
336     total_blocks = *buf++;
337     if (buf_size < total_blocks * 65 + 1) {
338         av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n",
339                total_blocks * 65 + 1, buf_size);
340         return AVERROR_INVALIDDATA;
341     }
342
343     /* get output buffer */
344     c->frame.nb_samples = total_blocks * 32;
345     if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
346         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
347         return ret;
348     }
349     output_samples = (int16_t *)c->frame.data[0];
350
351     for (blocks = 0; blocks < total_blocks; blocks++) {
352         uint8_t code = *buf++;
353         code = (code >> 1) | (code << 7);
354         scale[0] = bmv_aud_mults[code & 0xF];
355         scale[1] = bmv_aud_mults[code >> 4];
356         for (i = 0; i < 32; i++) {
357             *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
358             *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
359         }
360     }
361
362     *got_frame_ptr   = 1;
363     *(AVFrame *)data = c->frame;
364
365     return buf_size;
366 }
367
368 AVCodec ff_bmv_video_decoder = {
369     .name           = "bmv_video",
370     .type           = AVMEDIA_TYPE_VIDEO,
371     .id             = AV_CODEC_ID_BMV_VIDEO,
372     .priv_data_size = sizeof(BMVDecContext),
373     .init           = decode_init,
374     .close          = decode_end,
375     .decode         = decode_frame,
376     .capabilities   = CODEC_CAP_DR1,
377     .long_name      = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
378 };
379
380 AVCodec ff_bmv_audio_decoder = {
381     .name           = "bmv_audio",
382     .type           = AVMEDIA_TYPE_AUDIO,
383     .id             = AV_CODEC_ID_BMV_AUDIO,
384     .priv_data_size = sizeof(BMVAudioDecContext),
385     .init           = bmv_aud_decode_init,
386     .decode         = bmv_aud_decode_frame,
387     .capabilities   = CODEC_CAP_DR1,
388     .long_name      = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
389 };