aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavcodec / dsicinvideo.c
blob7c62dcf15b34bef279e47fe15adccf0953a04d33
1 /*
2 * Delphine Software International CIN video decoder
3 * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
5 * This file is part of Libav.
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.
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.
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
22 /**
23 * @file
24 * Delphine Software International CIN video decoder
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "internal.h"
31 typedef enum CinVideoBitmapIndex {
32 CIN_CUR_BMP = 0, /* current */
33 CIN_PRE_BMP = 1, /* previous */
34 CIN_INT_BMP = 2 /* intermediate */
35 } CinVideoBitmapIndex;
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;
45 static av_cold int cinvideo_decode_init(AVCodecContext *avctx)
47 CinVideoContext *cin = avctx->priv_data;
48 unsigned int i;
50 cin->avctx = avctx;
51 avctx->pix_fmt = AV_PIX_FMT_PAL8;
53 cin->frame = av_frame_alloc();
54 if (!cin->frame)
55 return AVERROR(ENOMEM);
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");
64 return 0;
67 static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
68 int size)
70 while (size--)
71 *dst++ += *src++;
74 static int cin_decode_huffman(const unsigned char *src, int src_size,
75 unsigned char *dst, int dst_size)
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;
83 memcpy(huff_code_table, src, 15);
84 src += 15;
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;
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;
106 return dst_cur - dst;
109 static int cin_decode_lzss(const unsigned char *src, int src_size,
110 unsigned char *dst, int dst_size)
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;
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;
141 return 0;
144 static void cin_decode_rle(const unsigned char *src, int src_size,
145 unsigned char *dst, int dst_size)
147 int len, code;
148 unsigned char *dst_end = dst + dst_size;
149 const unsigned char *src_end = src + src_size;
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;
163 dst += len;
167 static int cinvideo_decode_frame(AVCodecContext *avctx,
168 void *data, int *got_frame,
169 AVPacket *avpkt)
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;
177 palette_type = buf[0];
178 palette_colors_count = AV_RL16(buf + 1);
179 bitmap_frame_type = buf[3];
180 buf += 4;
182 bitmap_frame_size = buf_size - 4;
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;
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;
202 bitmap_frame_size = FFMIN(cin->bitmap_size, bitmap_frame_size);
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;
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;
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);
267 FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP],
268 cin->bitmap_table[CIN_PRE_BMP]);
270 if ((res = av_frame_ref(data, cin->frame)) < 0)
271 return res;
273 *got_frame = 1;
275 return buf_size;
278 static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
280 CinVideoContext *cin = avctx->priv_data;
281 int i;
283 av_frame_free(&cin->frame);
285 for (i = 0; i < 3; ++i)
286 av_free(cin->bitmap_table[i]);
288 return 0;
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 = AV_CODEC_CAP_DR1,