]> git.sesse.net Git - ffmpeg/blob - libavcodec/dsicinvideo.c
avcodec: Mark argument in av_{parser|hwaccel|bitstream_filter}_next as const
[ffmpeg] / libavcodec / dsicinvideo.c
1 /*
2  * Delphine Software International CIN video decoder
3  * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
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 /**
23  * @file
24  * Delphine Software International CIN video decoder
25  */
26
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "internal.h"
30
31 typedef enum CinVideoBitmapIndex {
32     CIN_CUR_BMP = 0, /* current */
33     CIN_PRE_BMP = 1, /* previous */
34     CIN_INT_BMP = 2  /* intermediate */
35 } CinVideoBitmapIndex;
36
37 typedef struct CinVideoContext {
38     AVCodecContext *avctx;
39     AVFrame *frame;
40     unsigned int bitmap_size;
41     uint32_t palette[256];
42     uint8_t *bitmap_table[3];
43 } CinVideoContext;
44
45 static av_cold int cinvideo_decode_init(AVCodecContext *avctx)
46 {
47     CinVideoContext *cin = avctx->priv_data;
48     unsigned int i;
49
50     cin->avctx = avctx;
51     avctx->pix_fmt = AV_PIX_FMT_PAL8;
52
53     cin->frame = av_frame_alloc();
54     if (!cin->frame)
55         return AVERROR(ENOMEM);
56
57     cin->bitmap_size = avctx->width * avctx->height;
58     for (i = 0; i < 3; ++i) {
59         cin->bitmap_table[i] = av_mallocz(cin->bitmap_size);
60         if (!cin->bitmap_table[i])
61             av_log(avctx, AV_LOG_ERROR, "Can't allocate bitmap buffers.\n");
62     }
63
64     return 0;
65 }
66
67 static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
68                                  int size)
69 {
70     while (size--)
71         *dst++ += *src++;
72 }
73
74 static int cin_decode_huffman(const unsigned char *src, int src_size,
75                               unsigned char *dst, int dst_size)
76 {
77     int b, huff_code = 0;
78     unsigned char huff_code_table[15];
79     unsigned char *dst_cur       = dst;
80     unsigned char *dst_end       = dst + dst_size;
81     const unsigned char *src_end = src + src_size;
82
83     memcpy(huff_code_table, src, 15);
84     src += 15;
85
86     while (src < src_end) {
87         huff_code = *src++;
88         if ((huff_code >> 4) == 15) {
89             b          = huff_code << 4;
90             huff_code  = *src++;
91             *dst_cur++ = b | (huff_code >> 4);
92         } else
93             *dst_cur++ = huff_code_table[huff_code >> 4];
94         if (dst_cur >= dst_end)
95             break;
96
97         huff_code &= 15;
98         if (huff_code == 15) {
99             *dst_cur++ = *src++;
100         } else
101             *dst_cur++ = huff_code_table[huff_code];
102         if (dst_cur >= dst_end)
103             break;
104     }
105
106     return dst_cur - dst;
107 }
108
109 static int cin_decode_lzss(const unsigned char *src, int src_size,
110                            unsigned char *dst, int dst_size)
111 {
112     uint16_t cmd;
113     int i, sz, offset, code;
114     unsigned char *dst_end       = dst + dst_size, *dst_start = dst;
115     const unsigned char *src_end = src + src_size;
116
117     while (src < src_end && dst < dst_end) {
118         code = *src++;
119         for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) {
120             if (code & (1 << i)) {
121                 *dst++ = *src++;
122             } else {
123                 cmd    = AV_RL16(src);
124                 src   += 2;
125                 offset = cmd >> 4;
126                 if ((int)(dst - dst_start) < offset + 1)
127                     return AVERROR_INVALIDDATA;
128                 sz = (cmd & 0xF) + 2;
129                 /* don't use memcpy/memmove here as the decoding routine
130                  * (ab)uses buffer overlappings to repeat bytes in the
131                  * destination */
132                 sz = FFMIN(sz, dst_end - dst);
133                 while (sz--) {
134                     *dst = *(dst - offset - 1);
135                     ++dst;
136                 }
137             }
138         }
139     }
140
141     return 0;
142 }
143
144 static void cin_decode_rle(const unsigned char *src, int src_size,
145                            unsigned char *dst, int dst_size)
146 {
147     int len, code;
148     unsigned char *dst_end       = dst + dst_size;
149     const unsigned char *src_end = src + src_size;
150
151     while (src < src_end && dst < dst_end) {
152         code = *src++;
153         if (code & 0x80) {
154             if (src >= src_end)
155                 break;
156             len = code - 0x7F;
157             memset(dst, *src++, FFMIN(len, dst_end - dst));
158         } else {
159             len = code + 1;
160             memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
161             src += len;
162         }
163         dst += len;
164     }
165 }
166
167 static int cinvideo_decode_frame(AVCodecContext *avctx,
168                                  void *data, int *got_frame,
169                                  AVPacket *avpkt)
170 {
171     const uint8_t *buf   = avpkt->data;
172     int buf_size         = avpkt->size;
173     CinVideoContext *cin = avctx->priv_data;
174     int i, y, palette_type, palette_colors_count,
175         bitmap_frame_type, bitmap_frame_size, res = 0;
176
177     palette_type         = buf[0];
178     palette_colors_count = AV_RL16(buf + 1);
179     bitmap_frame_type    = buf[3];
180     buf                 += 4;
181
182     bitmap_frame_size = buf_size - 4;
183
184     /* handle palette */
185     if (bitmap_frame_size < palette_colors_count * (3 + (palette_type != 0)))
186         return AVERROR_INVALIDDATA;
187     if (palette_type == 0) {
188         if (palette_colors_count > 256)
189             return AVERROR_INVALIDDATA;
190         for (i = 0; i < palette_colors_count; ++i) {
191             cin->palette[i]    = bytestream_get_le24(&buf);
192             bitmap_frame_size -= 3;
193         }
194     } else {
195         for (i = 0; i < palette_colors_count; ++i) {
196             cin->palette[buf[0]] = AV_RL24(buf + 1);
197             buf                 += 4;
198             bitmap_frame_size   -= 4;
199         }
200     }
201
202     bitmap_frame_size = FFMIN(cin->bitmap_size, bitmap_frame_size);
203
204     /* note: the decoding routines below assumes that
205      * surface.width = surface.pitch */
206     switch (bitmap_frame_type) {
207     case 9:
208         cin_decode_rle(buf, bitmap_frame_size,
209                        cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
210         break;
211     case 34:
212         cin_decode_rle(buf, bitmap_frame_size,
213                        cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
214         cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
215                              cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
216         break;
217     case 35:
218         cin_decode_huffman(buf, bitmap_frame_size,
219                            cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
220         cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
221                        cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
222         break;
223     case 36:
224         bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
225                                                cin->bitmap_table[CIN_INT_BMP],
226                                                cin->bitmap_size);
227         cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
228                        cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
229         cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
230                              cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
231         break;
232     case 37:
233         cin_decode_huffman(buf, bitmap_frame_size,
234                            cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
235         break;
236     case 38:
237         res = cin_decode_lzss(buf, bitmap_frame_size,
238                               cin->bitmap_table[CIN_CUR_BMP],
239                               cin->bitmap_size);
240         if (res < 0)
241             return res;
242         break;
243     case 39:
244         res = cin_decode_lzss(buf, bitmap_frame_size,
245                               cin->bitmap_table[CIN_CUR_BMP],
246                               cin->bitmap_size);
247         if (res < 0)
248             return res;
249         cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
250                              cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
251         break;
252     }
253
254     if ((res = ff_reget_buffer(avctx, cin->frame)) < 0) {
255         av_log(cin->avctx, AV_LOG_ERROR,
256                "delphinecinvideo: reget_buffer() failed to allocate a frame\n");
257         return res;
258     }
259
260     memcpy(cin->frame->data[1], cin->palette, sizeof(cin->palette));
261     cin->frame->palette_has_changed = 1;
262     for (y = 0; y < cin->avctx->height; ++y)
263         memcpy(cin->frame->data[0] + (cin->avctx->height - 1 - y) * cin->frame->linesize[0],
264                cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
265                cin->avctx->width);
266
267     FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP],
268                       cin->bitmap_table[CIN_PRE_BMP]);
269
270     if ((res = av_frame_ref(data, cin->frame)) < 0)
271         return res;
272
273     *got_frame = 1;
274
275     return buf_size;
276 }
277
278 static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
279 {
280     CinVideoContext *cin = avctx->priv_data;
281     int i;
282
283     av_frame_free(&cin->frame);
284
285     for (i = 0; i < 3; ++i)
286         av_free(cin->bitmap_table[i]);
287
288     return 0;
289 }
290
291 AVCodec ff_dsicinvideo_decoder = {
292     .name           = "dsicinvideo",
293     .long_name      = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
294     .type           = AVMEDIA_TYPE_VIDEO,
295     .id             = AV_CODEC_ID_DSICINVIDEO,
296     .priv_data_size = sizeof(CinVideoContext),
297     .init           = cinvideo_decode_init,
298     .close          = cinvideo_decode_end,
299     .decode         = cinvideo_decode_frame,
300     .capabilities   = CODEC_CAP_DR1,
301 };