]> git.sesse.net Git - ffmpeg/blob - libavcodec/dsicinav.c
Merge commit 'b45c87083de67969c1598cfef41e7c947b137922'
[ffmpeg] / libavcodec / dsicinav.c
1 /*
2  * Delphine Software International CIN Audio/Video Decoders
3  * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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  * Delphine Software International CIN audio/video decoders
25  */
26
27 #include "libavutil/channel_layout.h"
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "internal.h"
31 #include "mathops.h"
32
33
34 typedef enum CinVideoBitmapIndex {
35     CIN_CUR_BMP = 0, /* current */
36     CIN_PRE_BMP = 1, /* previous */
37     CIN_INT_BMP = 2  /* intermediate */
38 } CinVideoBitmapIndex;
39
40 typedef struct CinVideoContext {
41     AVCodecContext *avctx;
42     AVFrame frame;
43     unsigned int bitmap_size;
44     uint32_t palette[256];
45     uint8_t *bitmap_table[3];
46 } CinVideoContext;
47
48 typedef struct CinAudioContext {
49     int initial_decode_frame;
50     int delta;
51 } CinAudioContext;
52
53
54 /* table defining a geometric sequence with multiplier = 32767 ^ (1 / 128) */
55 static const int16_t cinaudio_delta16_table[256] = {
56          0,      0,      0,      0,      0,      0,      0,      0,
57          0,      0,      0,      0,      0,      0,      0,      0,
58          0,      0,      0, -30210, -27853, -25680, -23677, -21829,
59     -20126, -18556, -17108, -15774, -14543, -13408, -12362, -11398,
60     -10508,  -9689,  -8933,  -8236,  -7593,  -7001,  -6455,  -5951,
61      -5487,  -5059,  -4664,  -4300,  -3964,  -3655,  -3370,  -3107,
62      -2865,  -2641,  -2435,  -2245,  -2070,  -1908,  -1759,  -1622,
63      -1495,  -1379,  -1271,  -1172,  -1080,   -996,   -918,   -847,
64       -781,   -720,   -663,   -612,   -564,   -520,   -479,   -442,
65       -407,   -376,   -346,   -319,   -294,   -271,   -250,   -230,
66       -212,   -196,   -181,   -166,   -153,   -141,   -130,   -120,
67       -111,   -102,    -94,    -87,    -80,    -74,    -68,    -62,
68        -58,    -53,    -49,    -45,    -41,    -38,    -35,    -32,
69        -30,    -27,    -25,    -23,    -21,    -20,    -18,    -17,
70        -15,    -14,    -13,    -12,    -11,    -10,     -9,     -8,
71         -7,     -6,     -5,     -4,     -3,     -2,     -1,      0,
72          0,      1,      2,      3,      4,      5,      6,      7,
73          8,      9,     10,     11,     12,     13,     14,     15,
74         17,     18,     20,     21,     23,     25,     27,     30,
75         32,     35,     38,     41,     45,     49,     53,     58,
76         62,     68,     74,     80,     87,     94,    102,    111,
77        120,    130,    141,    153,    166,    181,    196,    212,
78        230,    250,    271,    294,    319,    346,    376,    407,
79        442,    479,    520,    564,    612,    663,    720,    781,
80        847,    918,    996,   1080,   1172,   1271,   1379,   1495,
81       1622,   1759,   1908,   2070,   2245,   2435,   2641,   2865,
82       3107,   3370,   3655,   3964,   4300,   4664,   5059,   5487,
83       5951,   6455,   7001,   7593,   8236,   8933,   9689,  10508,
84      11398,  12362,  13408,  14543,  15774,  17108,  18556,  20126,
85      21829,  23677,  25680,  27853,  30210,      0,      0,      0,
86          0,      0,      0,      0,      0,      0,      0,      0,
87          0,      0,      0,      0,      0,      0,      0,      0
88 };
89
90 static av_cold void destroy_buffers(CinVideoContext *cin)
91 {
92     int i;
93
94     for (i = 0; i < 3; ++i)
95         av_freep(&cin->bitmap_table[i]);
96 }
97
98 static av_cold int allocate_buffers(CinVideoContext *cin)
99 {
100     int i;
101
102     for (i = 0; i < 3; ++i) {
103         cin->bitmap_table[i] = av_mallocz(cin->bitmap_size);
104         if (!cin->bitmap_table[i]) {
105             av_log(cin->avctx, AV_LOG_ERROR, "Can't allocate bitmap buffers.\n");
106             destroy_buffers(cin);
107             return AVERROR(ENOMEM);
108         }
109     }
110
111     return 0;
112 }
113
114 static av_cold int cinvideo_decode_init(AVCodecContext *avctx)
115 {
116     CinVideoContext *cin = avctx->priv_data;
117
118     cin->avctx = avctx;
119     avctx->pix_fmt = AV_PIX_FMT_PAL8;
120
121     avcodec_get_frame_defaults(&cin->frame);
122
123     cin->bitmap_size = avctx->width * avctx->height;
124     if (allocate_buffers(cin))
125         return AVERROR(ENOMEM);
126
127     return 0;
128 }
129
130 static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
131                                  int size)
132 {
133     while (size--)
134         *dst++ += *src++;
135 }
136
137 static int cin_decode_huffman(const unsigned char *src, int src_size,
138                               unsigned char *dst, int dst_size)
139 {
140     int b, huff_code = 0;
141     unsigned char huff_code_table[15];
142     unsigned char *dst_cur       = dst;
143     unsigned char *dst_end       = dst + dst_size;
144     const unsigned char *src_end = src + src_size;
145
146     memcpy(huff_code_table, src, 15);
147     src += 15;
148
149     while (src < src_end) {
150         huff_code = *src++;
151         if ((huff_code >> 4) == 15) {
152             b          = huff_code << 4;
153             huff_code  = *src++;
154             *dst_cur++ = b | (huff_code >> 4);
155         } else
156             *dst_cur++ = huff_code_table[huff_code >> 4];
157         if (dst_cur >= dst_end)
158             break;
159
160         huff_code &= 15;
161         if (huff_code == 15) {
162             *dst_cur++ = *src++;
163         } else
164             *dst_cur++ = huff_code_table[huff_code];
165         if (dst_cur >= dst_end)
166             break;
167     }
168
169     return dst_cur - dst;
170 }
171
172 static int cin_decode_lzss(const unsigned char *src, int src_size,
173                            unsigned char *dst, int dst_size)
174 {
175     uint16_t cmd;
176     int i, sz, offset, code;
177     unsigned char *dst_end       = dst + dst_size, *dst_start = dst;
178     const unsigned char *src_end = src + src_size;
179
180     while (src < src_end && dst < dst_end) {
181         code = *src++;
182         for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) {
183             if (code & (1 << i)) {
184                 *dst++ = *src++;
185             } else {
186                 cmd    = AV_RL16(src);
187                 src   += 2;
188                 offset = cmd >> 4;
189                 if ((int)(dst - dst_start) < offset + 1)
190                     return AVERROR_INVALIDDATA;
191                 sz = (cmd & 0xF) + 2;
192                 /* don't use memcpy/memmove here as the decoding routine
193                  * (ab)uses buffer overlappings to repeat bytes in the
194                  * destination */
195                 sz = FFMIN(sz, dst_end - dst);
196                 while (sz--) {
197                     *dst = *(dst - offset - 1);
198                     ++dst;
199                 }
200             }
201         }
202     }
203
204     return 0;
205 }
206
207 static int cin_decode_rle(const unsigned char *src, int src_size,
208                            unsigned char *dst, int dst_size)
209 {
210     int len, code;
211     unsigned char *dst_end       = dst + dst_size;
212     const unsigned char *src_end = src + src_size;
213
214     while (src + 1 < src_end && dst < dst_end) {
215         code = *src++;
216         if (code & 0x80) {
217             len = code - 0x7F;
218             memset(dst, *src++, FFMIN(len, dst_end - dst));
219         } else {
220             len = code + 1;
221             if (len > src_end-src) {
222                 av_log(NULL, AV_LOG_ERROR, "RLE overread\n");
223                 return AVERROR_INVALIDDATA;
224             }
225             memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
226             src += len;
227         }
228         dst += len;
229     }
230     return 0;
231 }
232
233 static int cinvideo_decode_frame(AVCodecContext *avctx,
234                                  void *data, int *got_frame,
235                                  AVPacket *avpkt)
236 {
237     const uint8_t *buf   = avpkt->data;
238     int buf_size         = avpkt->size;
239     CinVideoContext *cin = avctx->priv_data;
240     int i, y, palette_type, palette_colors_count,
241         bitmap_frame_type, bitmap_frame_size, res = 0;
242
243     palette_type         = buf[0];
244     palette_colors_count = AV_RL16(buf + 1);
245     bitmap_frame_type    = buf[3];
246     buf                 += 4;
247
248     bitmap_frame_size = buf_size - 4;
249
250     /* handle palette */
251     if (bitmap_frame_size < palette_colors_count * (3 + (palette_type != 0)))
252         return AVERROR_INVALIDDATA;
253     if (palette_type == 0) {
254         if (palette_colors_count > 256)
255             return AVERROR_INVALIDDATA;
256         for (i = 0; i < palette_colors_count; ++i) {
257             cin->palette[i]    = 0xFFU << 24 | bytestream_get_le24(&buf);
258             bitmap_frame_size -= 3;
259         }
260     } else {
261         for (i = 0; i < palette_colors_count; ++i) {
262             cin->palette[buf[0]] = 0xFFU << 24 | AV_RL24(buf + 1);
263             buf                 += 4;
264             bitmap_frame_size   -= 4;
265         }
266     }
267
268     /* note: the decoding routines below assumes that
269      * surface.width = surface.pitch */
270     switch (bitmap_frame_type) {
271     case 9:
272         cin_decode_rle(buf, bitmap_frame_size,
273                        cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
274         break;
275     case 34:
276         cin_decode_rle(buf, bitmap_frame_size,
277                        cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
278         cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
279                              cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
280         break;
281     case 35:
282         bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
283                            cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
284         cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
285                        cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
286         break;
287     case 36:
288         bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
289                                                cin->bitmap_table[CIN_INT_BMP],
290                                                cin->bitmap_size);
291         cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
292                        cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
293         cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
294                              cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
295         break;
296     case 37:
297         cin_decode_huffman(buf, bitmap_frame_size,
298                            cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
299         break;
300     case 38:
301         res = cin_decode_lzss(buf, bitmap_frame_size,
302                               cin->bitmap_table[CIN_CUR_BMP],
303                               cin->bitmap_size);
304         if (res < 0)
305             return res;
306         break;
307     case 39:
308         res = cin_decode_lzss(buf, bitmap_frame_size,
309                               cin->bitmap_table[CIN_CUR_BMP],
310                               cin->bitmap_size);
311         if (res < 0)
312             return res;
313         cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
314                              cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
315         break;
316     }
317
318     if ((res = ff_reget_buffer(avctx, &cin->frame)) < 0)
319         return res;
320
321     memcpy(cin->frame.data[1], cin->palette, sizeof(cin->palette));
322     cin->frame.palette_has_changed = 1;
323     for (y = 0; y < cin->avctx->height; ++y)
324         memcpy(cin->frame.data[0] + (cin->avctx->height - 1 - y) * cin->frame.linesize[0],
325                cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
326                cin->avctx->width);
327
328     FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP],
329                       cin->bitmap_table[CIN_PRE_BMP]);
330
331     if ((res = av_frame_ref(data, &cin->frame)) < 0)
332         return res;
333
334     *got_frame = 1;
335
336     return buf_size;
337 }
338
339 static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
340 {
341     CinVideoContext *cin = avctx->priv_data;
342
343     av_frame_unref(&cin->frame);
344
345     destroy_buffers(cin);
346
347     return 0;
348 }
349
350 static av_cold int cinaudio_decode_init(AVCodecContext *avctx)
351 {
352     CinAudioContext *cin = avctx->priv_data;
353
354     cin->initial_decode_frame = 1;
355     cin->delta                = 0;
356     avctx->sample_fmt         = AV_SAMPLE_FMT_S16;
357     avctx->channels           = 1;
358     avctx->channel_layout     = AV_CH_LAYOUT_MONO;
359
360     return 0;
361 }
362
363 static int cinaudio_decode_frame(AVCodecContext *avctx, void *data,
364                                  int *got_frame_ptr, AVPacket *avpkt)
365 {
366     AVFrame *frame         = data;
367     const uint8_t *buf     = avpkt->data;
368     CinAudioContext *cin   = avctx->priv_data;
369     const uint8_t *buf_end = buf + avpkt->size;
370     int16_t *samples;
371     int delta, ret;
372
373     /* get output buffer */
374     frame->nb_samples = avpkt->size - cin->initial_decode_frame;
375     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
376         return ret;
377     samples = (int16_t *)frame->data[0];
378
379     delta = cin->delta;
380     if (cin->initial_decode_frame) {
381         cin->initial_decode_frame = 0;
382         delta                     = sign_extend(AV_RL16(buf), 16);
383         buf                      += 2;
384         *samples++                = delta;
385     }
386     while (buf < buf_end) {
387         delta     += cinaudio_delta16_table[*buf++];
388         delta      = av_clip_int16(delta);
389         *samples++ = delta;
390     }
391     cin->delta = delta;
392
393     *got_frame_ptr = 1;
394
395     return avpkt->size;
396 }
397
398 AVCodec ff_dsicinvideo_decoder = {
399     .name           = "dsicinvideo",
400     .long_name      = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
401     .type           = AVMEDIA_TYPE_VIDEO,
402     .id             = AV_CODEC_ID_DSICINVIDEO,
403     .priv_data_size = sizeof(CinVideoContext),
404     .init           = cinvideo_decode_init,
405     .close          = cinvideo_decode_end,
406     .decode         = cinvideo_decode_frame,
407     .capabilities   = CODEC_CAP_DR1,
408 };
409
410 AVCodec ff_dsicinaudio_decoder = {
411     .name           = "dsicinaudio",
412     .long_name      = NULL_IF_CONFIG_SMALL("Delphine Software International CIN audio"),
413     .type           = AVMEDIA_TYPE_AUDIO,
414     .id             = AV_CODEC_ID_DSICINAUDIO,
415     .priv_data_size = sizeof(CinAudioContext),
416     .init           = cinaudio_decode_init,
417     .decode         = cinaudio_decode_frame,
418     .capabilities   = CODEC_CAP_DR1,
419 };