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