| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 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 video decoder | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include "libavutil/mem.h" | ||
| 28 | #include "avcodec.h" | ||
| 29 | #include "bytestream.h" | ||
| 30 | #include "codec_internal.h" | ||
| 31 | #include "decode.h" | ||
| 32 | |||
| 33 | typedef enum CinVideoBitmapIndex { | ||
| 34 | CIN_CUR_BMP = 0, /* current */ | ||
| 35 | CIN_PRE_BMP = 1, /* previous */ | ||
| 36 | CIN_INT_BMP = 2 /* intermediate */ | ||
| 37 | } CinVideoBitmapIndex; | ||
| 38 | |||
| 39 | typedef struct CinVideoContext { | ||
| 40 | AVCodecContext *avctx; | ||
| 41 | AVFrame *frame; | ||
| 42 | unsigned int bitmap_size; | ||
| 43 | uint32_t palette[256]; | ||
| 44 | uint8_t *bitmap_table[3]; | ||
| 45 | } CinVideoContext; | ||
| 46 | |||
| 47 | 3 | static av_cold void destroy_buffers(CinVideoContext *cin) | |
| 48 | { | ||
| 49 | int i; | ||
| 50 | |||
| 51 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
|
12 | for (i = 0; i < 3; ++i) |
| 52 | 9 | av_freep(&cin->bitmap_table[i]); | |
| 53 | 3 | } | |
| 54 | |||
| 55 | 3 | static av_cold int allocate_buffers(CinVideoContext *cin) | |
| 56 | { | ||
| 57 | int i; | ||
| 58 | |||
| 59 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
|
12 | for (i = 0; i < 3; ++i) { |
| 60 | 9 | cin->bitmap_table[i] = av_mallocz(cin->bitmap_size); | |
| 61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (!cin->bitmap_table[i]) { |
| 62 | ✗ | av_log(cin->avctx, AV_LOG_ERROR, "Can't allocate bitmap buffers.\n"); | |
| 63 | ✗ | return AVERROR(ENOMEM); | |
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | 3 | return 0; | |
| 68 | } | ||
| 69 | |||
| 70 | 3 | static av_cold int cinvideo_decode_init(AVCodecContext *avctx) | |
| 71 | { | ||
| 72 | 3 | CinVideoContext *cin = avctx->priv_data; | |
| 73 | |||
| 74 | 3 | cin->avctx = avctx; | |
| 75 | 3 | avctx->pix_fmt = AV_PIX_FMT_PAL8; | |
| 76 | |||
| 77 | 3 | cin->frame = av_frame_alloc(); | |
| 78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!cin->frame) |
| 79 | ✗ | return AVERROR(ENOMEM); | |
| 80 | |||
| 81 | 3 | cin->bitmap_size = avctx->width * avctx->height; | |
| 82 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (allocate_buffers(cin)) |
| 83 | ✗ | return AVERROR(ENOMEM); | |
| 84 | |||
| 85 | 3 | return 0; | |
| 86 | } | ||
| 87 | |||
| 88 | 8 | static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst, | |
| 89 | int size) | ||
| 90 | { | ||
| 91 |
2/2✓ Branch 0 taken 409600 times.
✓ Branch 1 taken 8 times.
|
409608 | while (size--) |
| 92 | 409600 | *dst++ += *src++; | |
| 93 | 8 | } | |
| 94 | |||
| 95 | ✗ | static int cin_decode_huffman(const unsigned char *src, int src_size, | |
| 96 | unsigned char *dst, int dst_size) | ||
| 97 | { | ||
| 98 | ✗ | int b, huff_code = 0; | |
| 99 | unsigned char huff_code_table[15]; | ||
| 100 | ✗ | unsigned char *dst_cur = dst; | |
| 101 | ✗ | unsigned char *dst_end = dst + dst_size; | |
| 102 | ✗ | const unsigned char *src_end = src + src_size; | |
| 103 | |||
| 104 | ✗ | memcpy(huff_code_table, src, 15); | |
| 105 | ✗ | src += 15; | |
| 106 | |||
| 107 | ✗ | while (src < src_end) { | |
| 108 | ✗ | huff_code = *src++; | |
| 109 | ✗ | if ((huff_code >> 4) == 15) { | |
| 110 | ✗ | b = huff_code << 4; | |
| 111 | ✗ | huff_code = *src++; | |
| 112 | ✗ | *dst_cur++ = b | (huff_code >> 4); | |
| 113 | } else | ||
| 114 | ✗ | *dst_cur++ = huff_code_table[huff_code >> 4]; | |
| 115 | ✗ | if (dst_cur >= dst_end) | |
| 116 | ✗ | break; | |
| 117 | |||
| 118 | ✗ | huff_code &= 15; | |
| 119 | ✗ | if (huff_code == 15) { | |
| 120 | ✗ | *dst_cur++ = *src++; | |
| 121 | } else | ||
| 122 | ✗ | *dst_cur++ = huff_code_table[huff_code]; | |
| 123 | ✗ | if (dst_cur >= dst_end) | |
| 124 | ✗ | break; | |
| 125 | } | ||
| 126 | |||
| 127 | ✗ | return dst_cur - dst; | |
| 128 | } | ||
| 129 | |||
| 130 | 92 | static int cin_decode_lzss(const unsigned char *src, int src_size, | |
| 131 | unsigned char *dst, int dst_size) | ||
| 132 | { | ||
| 133 | uint16_t cmd; | ||
| 134 | int i, sz, offset, code; | ||
| 135 | 92 | unsigned char *dst_end = dst + dst_size, *dst_start = dst; | |
| 136 | 92 | const unsigned char *src_end = src + src_size; | |
| 137 | |||
| 138 |
3/4✓ Branch 0 taken 49818 times.
✓ Branch 1 taken 92 times.
✓ Branch 2 taken 49818 times.
✗ Branch 3 not taken.
|
49910 | while (src < src_end && dst < dst_end) { |
| 139 | 49818 | code = *src++; | |
| 140 |
5/6✓ Branch 0 taken 398346 times.
✓ Branch 1 taken 49740 times.
✓ Branch 2 taken 398268 times.
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 398268 times.
✗ Branch 5 not taken.
|
448086 | for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) { |
| 141 |
2/2✓ Branch 0 taken 86707 times.
✓ Branch 1 taken 311561 times.
|
398268 | if (code & (1 << i)) { |
| 142 | 86707 | *dst++ = *src++; | |
| 143 | } else { | ||
| 144 | 311561 | cmd = AV_RL16(src); | |
| 145 | 311561 | src += 2; | |
| 146 | 311561 | offset = cmd >> 4; | |
| 147 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 311561 times.
|
311561 | if ((int)(dst - dst_start) < offset + 1) |
| 148 | ✗ | return AVERROR_INVALIDDATA; | |
| 149 | 311561 | sz = (cmd & 0xF) + 2; | |
| 150 | /* don't use memcpy/memmove here as the decoding routine | ||
| 151 | * (ab)uses buffer overlappings to repeat bytes in the | ||
| 152 | * destination */ | ||
| 153 | 311561 | sz = FFMIN(sz, dst_end - dst); | |
| 154 |
2/2✓ Branch 0 taken 4578447 times.
✓ Branch 1 taken 311561 times.
|
4890008 | while (sz--) { |
| 155 | 4578447 | *dst = *(dst - offset - 1); | |
| 156 | 4578447 | ++dst; | |
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
|
92 | if (dst_end - dst > dst_size - dst_size/10) |
| 163 | ✗ | return AVERROR_INVALIDDATA; | |
| 164 | |||
| 165 | 92 | return 0; | |
| 166 | } | ||
| 167 | |||
| 168 | ✗ | static int cin_decode_rle(const unsigned char *src, int src_size, | |
| 169 | unsigned char *dst, int dst_size) | ||
| 170 | { | ||
| 171 | int len, code; | ||
| 172 | ✗ | unsigned char *dst_end = dst + dst_size; | |
| 173 | ✗ | const unsigned char *src_end = src + src_size; | |
| 174 | |||
| 175 | ✗ | while (src + 1 < src_end && dst < dst_end) { | |
| 176 | ✗ | code = *src++; | |
| 177 | ✗ | if (code & 0x80) { | |
| 178 | ✗ | len = code - 0x7F; | |
| 179 | ✗ | memset(dst, *src++, FFMIN(len, dst_end - dst)); | |
| 180 | } else { | ||
| 181 | ✗ | len = code + 1; | |
| 182 | ✗ | if (len > src_end-src) { | |
| 183 | ✗ | av_log(NULL, AV_LOG_ERROR, "RLE overread\n"); | |
| 184 | ✗ | return AVERROR_INVALIDDATA; | |
| 185 | } | ||
| 186 | ✗ | memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src)); | |
| 187 | ✗ | src += len; | |
| 188 | } | ||
| 189 | ✗ | dst += len; | |
| 190 | } | ||
| 191 | |||
| 192 | ✗ | if (dst_end - dst > dst_size - dst_size/10) | |
| 193 | ✗ | return AVERROR_INVALIDDATA; | |
| 194 | |||
| 195 | ✗ | return 0; | |
| 196 | } | ||
| 197 | |||
| 198 | 92 | static int cinvideo_decode_frame(AVCodecContext *avctx, AVFrame *rframe, | |
| 199 | int *got_frame, AVPacket *avpkt) | ||
| 200 | { | ||
| 201 | 92 | const uint8_t *buf = avpkt->data; | |
| 202 | 92 | int buf_size = avpkt->size; | |
| 203 | 92 | CinVideoContext *cin = avctx->priv_data; | |
| 204 | int i, y, palette_type, palette_colors_count, | ||
| 205 | 92 | bitmap_frame_type, bitmap_frame_size, res = 0; | |
| 206 | |||
| 207 | 92 | palette_type = buf[0]; | |
| 208 | 92 | palette_colors_count = AV_RL16(buf + 1); | |
| 209 | 92 | bitmap_frame_type = buf[3]; | |
| 210 | 92 | buf += 4; | |
| 211 | |||
| 212 | 92 | bitmap_frame_size = buf_size - 4; | |
| 213 | |||
| 214 | /* handle palette */ | ||
| 215 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 92 times.
|
92 | if (bitmap_frame_size < palette_colors_count * (3 + (palette_type != 0))) |
| 216 | ✗ | return AVERROR_INVALIDDATA; | |
| 217 |
1/2✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
|
92 | if (palette_type == 0) { |
| 218 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
|
92 | if (palette_colors_count > 256) |
| 219 | ✗ | return AVERROR_INVALIDDATA; | |
| 220 |
2/2✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 92 times.
|
11356 | for (i = 0; i < palette_colors_count; ++i) { |
| 221 | 11264 | cin->palette[i] = 0xFFU << 24 | bytestream_get_le24(&buf); | |
| 222 | 11264 | bitmap_frame_size -= 3; | |
| 223 | } | ||
| 224 | } else { | ||
| 225 | ✗ | for (i = 0; i < palette_colors_count; ++i) { | |
| 226 | ✗ | cin->palette[buf[0]] = 0xFFU << 24 | AV_RL24(buf + 1); | |
| 227 | ✗ | buf += 4; | |
| 228 | ✗ | bitmap_frame_size -= 4; | |
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | /* note: the decoding routines below assumes that | ||
| 233 | * surface.width = surface.pitch */ | ||
| 234 |
2/8✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 84 times.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
|
92 | switch (bitmap_frame_type) { |
| 235 | ✗ | case 9: | |
| 236 | ✗ | res = cin_decode_rle(buf, bitmap_frame_size, | |
| 237 | ✗ | cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size); | |
| 238 | ✗ | if (res < 0) | |
| 239 | ✗ | return res; | |
| 240 | ✗ | break; | |
| 241 | ✗ | case 34: | |
| 242 | ✗ | res = cin_decode_rle(buf, bitmap_frame_size, | |
| 243 | ✗ | cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size); | |
| 244 | ✗ | if (res < 0) | |
| 245 | ✗ | return res; | |
| 246 | ✗ | cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP], | |
| 247 | ✗ | cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size); | |
| 248 | ✗ | break; | |
| 249 | ✗ | case 35: | |
| 250 | ✗ | bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size, | |
| 251 | ✗ | cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size); | |
| 252 | ✗ | res = cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size, | |
| 253 | ✗ | cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size); | |
| 254 | ✗ | if (res < 0) | |
| 255 | ✗ | return res; | |
| 256 | ✗ | break; | |
| 257 | ✗ | case 36: | |
| 258 | ✗ | bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size, | |
| 259 | ✗ | cin->bitmap_table[CIN_INT_BMP], | |
| 260 | ✗ | cin->bitmap_size); | |
| 261 | ✗ | res = cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size, | |
| 262 | ✗ | cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size); | |
| 263 | ✗ | if (res < 0) | |
| 264 | ✗ | return res; | |
| 265 | ✗ | cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP], | |
| 266 | ✗ | cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size); | |
| 267 | ✗ | break; | |
| 268 | ✗ | case 37: | |
| 269 | ✗ | res = cin_decode_huffman(buf, bitmap_frame_size, | |
| 270 | ✗ | cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size); | |
| 271 | |||
| 272 | ✗ | if (cin->bitmap_size - avctx->discard_damaged_percentage*cin->bitmap_size/100 > res) | |
| 273 | ✗ | return AVERROR_INVALIDDATA; | |
| 274 | ✗ | break; | |
| 275 | 84 | case 38: | |
| 276 | 84 | res = cin_decode_lzss(buf, bitmap_frame_size, | |
| 277 | 84 | cin->bitmap_table[CIN_CUR_BMP], | |
| 278 | 84 | cin->bitmap_size); | |
| 279 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
|
84 | if (res < 0) |
| 280 | ✗ | return res; | |
| 281 | 84 | break; | |
| 282 | 8 | case 39: | |
| 283 | 8 | res = cin_decode_lzss(buf, bitmap_frame_size, | |
| 284 | 8 | cin->bitmap_table[CIN_CUR_BMP], | |
| 285 | 8 | cin->bitmap_size); | |
| 286 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (res < 0) |
| 287 | ✗ | return res; | |
| 288 | 8 | cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP], | |
| 289 | 8 | cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size); | |
| 290 | 8 | break; | |
| 291 | } | ||
| 292 | |||
| 293 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
|
92 | if ((res = ff_reget_buffer(avctx, cin->frame, 0)) < 0) |
| 294 | ✗ | return res; | |
| 295 | |||
| 296 | 92 | memcpy(cin->frame->data[1], cin->palette, sizeof(cin->palette)); | |
| 297 |
2/2✓ Branch 0 taken 14720 times.
✓ Branch 1 taken 92 times.
|
14812 | for (y = 0; y < cin->avctx->height; ++y) |
| 298 | 14720 | memcpy(cin->frame->data[0] + (cin->avctx->height - 1 - y) * cin->frame->linesize[0], | |
| 299 | 14720 | cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width, | |
| 300 | 14720 | cin->avctx->width); | |
| 301 | |||
| 302 | 92 | FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP], | |
| 303 | cin->bitmap_table[CIN_PRE_BMP]); | ||
| 304 | |||
| 305 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
|
92 | if ((res = av_frame_ref(rframe, cin->frame)) < 0) |
| 306 | ✗ | return res; | |
| 307 | |||
| 308 | 92 | *got_frame = 1; | |
| 309 | |||
| 310 | 92 | return buf_size; | |
| 311 | } | ||
| 312 | |||
| 313 | 3 | static av_cold int cinvideo_decode_end(AVCodecContext *avctx) | |
| 314 | { | ||
| 315 | 3 | CinVideoContext *cin = avctx->priv_data; | |
| 316 | |||
| 317 | 3 | av_frame_free(&cin->frame); | |
| 318 | |||
| 319 | 3 | destroy_buffers(cin); | |
| 320 | |||
| 321 | 3 | return 0; | |
| 322 | } | ||
| 323 | |||
| 324 | const FFCodec ff_dsicinvideo_decoder = { | ||
| 325 | .p.name = "dsicinvideo", | ||
| 326 | CODEC_LONG_NAME("Delphine Software International CIN video"), | ||
| 327 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 328 | .p.id = AV_CODEC_ID_DSICINVIDEO, | ||
| 329 | .priv_data_size = sizeof(CinVideoContext), | ||
| 330 | .init = cinvideo_decode_init, | ||
| 331 | .close = cinvideo_decode_end, | ||
| 332 | FF_CODEC_DECODE_CB(cinvideo_decode_frame), | ||
| 333 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 334 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 335 | }; | ||
| 336 |