110
static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst, int size)
112
static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
113
116
*dst++ += *src++;
116
static int cin_decode_huffman(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
119
static int cin_decode_huffman(const unsigned char *src, int src_size,
120
unsigned char *dst, int dst_size)
118
122
int b, huff_code = 0;
119
123
unsigned char huff_code_table[15];
120
unsigned char *dst_cur = dst;
121
unsigned char *dst_end = dst + dst_size;
124
unsigned char *dst_cur = dst;
125
unsigned char *dst_end = dst + dst_size;
122
126
const unsigned char *src_end = src + src_size;
124
memcpy(huff_code_table, src, 15); src += 15; src_size -= 15;
128
memcpy(huff_code_table, src, 15);
126
131
while (src < src_end) {
127
132
huff_code = *src++;
128
133
if ((huff_code >> 4) == 15) {
131
136
*dst_cur++ = b | (huff_code >> 4);
133
138
*dst_cur++ = huff_code_table[huff_code >> 4];
146
151
return dst_cur - dst;
149
static int cin_decode_lzss(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
154
static int cin_decode_lzss(const unsigned char *src, int src_size,
155
unsigned char *dst, int dst_size)
152
158
int i, sz, offset, code;
153
unsigned char *dst_end = dst + dst_size, *dst_start = dst;
159
unsigned char *dst_end = dst + dst_size, *dst_start = dst;
154
160
const unsigned char *src_end = src + src_size;
156
162
while (src < src_end && dst < dst_end) {
159
165
if (code & (1 << i)) {
162
cmd = AV_RL16(src); src += 2;
163
170
offset = cmd >> 4;
164
if ((int) (dst - dst_start) < offset + 1)
171
if ((int)(dst - dst_start) < offset + 1)
165
172
return AVERROR_INVALIDDATA;
166
173
sz = (cmd & 0xF) + 2;
167
/* don't use memcpy/memmove here as the decoding routine (ab)uses */
168
/* buffer overlappings to repeat bytes in the destination */
174
/* don't use memcpy/memmove here as the decoding routine
175
* (ab)uses buffer overlappings to repeat bytes in the
169
177
sz = FFMIN(sz, dst_end - dst);
171
179
*dst = *(dst - offset - 1);
181
static void cin_decode_rle(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
189
static void cin_decode_rle(const unsigned char *src, int src_size,
190
unsigned char *dst, int dst_size)
184
unsigned char *dst_end = dst + dst_size;
193
unsigned char *dst_end = dst + dst_size;
185
194
const unsigned char *src_end = src + src_size;
187
196
while (src < src_end && dst < dst_end) {
189
198
if (code & 0x80) {
190
201
len = code - 0x7F;
191
202
memset(dst, *src++, FFMIN(len, dst_end - dst));
194
memcpy(dst, src, FFMIN(len, dst_end - dst));
205
memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
201
212
static int cinvideo_decode_frame(AVCodecContext *avctx,
202
void *data, int *data_size,
213
void *data, int *got_frame,
205
const uint8_t *buf = avpkt->data;
206
int buf_size = avpkt->size;
216
const uint8_t *buf = avpkt->data;
217
int buf_size = avpkt->size;
207
218
CinVideoContext *cin = avctx->priv_data;
208
int i, y, palette_type, palette_colors_count, bitmap_frame_type, bitmap_frame_size, res = 0;
219
int i, y, palette_type, palette_colors_count,
220
bitmap_frame_type, bitmap_frame_size, res = 0;
210
palette_type = buf[0];
211
palette_colors_count = AV_RL16(buf+1);
212
bitmap_frame_type = buf[3];
222
palette_type = buf[0];
223
palette_colors_count = AV_RL16(buf + 1);
224
bitmap_frame_type = buf[3];
215
227
bitmap_frame_size = buf_size - 4;
221
233
if (palette_colors_count > 256)
222
234
return AVERROR_INVALIDDATA;
223
235
for (i = 0; i < palette_colors_count; ++i) {
224
cin->palette[i] = bytestream_get_le24(&buf);
236
cin->palette[i] = bytestream_get_le24(&buf);
225
237
bitmap_frame_size -= 3;
228
240
for (i = 0; i < palette_colors_count; ++i) {
229
cin->palette[buf[0]] = AV_RL24(buf+1);
231
bitmap_frame_size -= 4;
241
cin->palette[buf[0]] = AV_RL24(buf + 1);
243
bitmap_frame_size -= 4;
235
/* note: the decoding routines below assumes that surface.width = surface.pitch */
247
bitmap_frame_size = FFMIN(cin->bitmap_size, bitmap_frame_size);
249
/* note: the decoding routines below assumes that
250
* surface.width = surface.pitch */
236
251
switch (bitmap_frame_type) {
238
253
cin_decode_rle(buf, bitmap_frame_size,
239
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
254
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
242
257
cin_decode_rle(buf, bitmap_frame_size,
243
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
258
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
244
259
cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
245
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
260
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
248
263
cin_decode_huffman(buf, bitmap_frame_size,
249
cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
264
cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
250
265
cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
251
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
266
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
254
269
bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
255
cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
270
cin->bitmap_table[CIN_INT_BMP],
256
272
cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
257
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
273
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
258
274
cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
259
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
275
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
262
278
cin_decode_huffman(buf, bitmap_frame_size,
263
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
279
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
266
282
res = cin_decode_lzss(buf, bitmap_frame_size,
278
294
cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
279
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
295
cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
283
299
cin->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
284
if (avctx->reget_buffer(avctx, &cin->frame)) {
285
av_log(cin->avctx, AV_LOG_ERROR, "delphinecinvideo: reget_buffer() failed to allocate a frame\n");
300
if ((res = avctx->reget_buffer(avctx, &cin->frame)) < 0) {
301
av_log(cin->avctx, AV_LOG_ERROR,
302
"delphinecinvideo: reget_buffer() failed to allocate a frame\n");
289
306
memcpy(cin->frame.data[1], cin->palette, sizeof(cin->palette));
290
307
cin->frame.palette_has_changed = 1;
291
308
for (y = 0; y < cin->avctx->height; ++y)
292
309
memcpy(cin->frame.data[0] + (cin->avctx->height - 1 - y) * cin->frame.linesize[0],
293
cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
296
FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_table[CIN_PRE_BMP]);
298
*data_size = sizeof(AVFrame);
310
cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
313
FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP],
314
cin->bitmap_table[CIN_PRE_BMP]);
299
317
*(AVFrame *)data = cin->frame;
320
338
CinAudioContext *cin = avctx->priv_data;
322
if (avctx->channels != 1) {
323
av_log_ask_for_sample(avctx, "Number of channels is not supported\n");
324
return AVERROR_PATCHWELCOME;
327
340
cin->initial_decode_frame = 1;
329
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
342
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
344
avctx->channel_layout = AV_CH_LAYOUT_MONO;
331
346
avcodec_get_frame_defaults(&cin->frame);
332
347
avctx->coded_frame = &cin->frame;
337
352
static int cinaudio_decode_frame(AVCodecContext *avctx, void *data,
338
353
int *got_frame_ptr, AVPacket *avpkt)
340
const uint8_t *buf = avpkt->data;
341
CinAudioContext *cin = avctx->priv_data;
355
const uint8_t *buf = avpkt->data;
356
CinAudioContext *cin = avctx->priv_data;
342
357
const uint8_t *buf_end = buf + avpkt->size;
343
358
int16_t *samples;
346
361
/* get output buffer */
347
362
cin->frame.nb_samples = avpkt->size - cin->initial_decode_frame;
348
if ((ret = avctx->get_buffer(avctx, &cin->frame)) < 0) {
363
if ((ret = ff_get_buffer(avctx, &cin->frame)) < 0) {
349
364
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
354
369
delta = cin->delta;
355
370
if (cin->initial_decode_frame) {
356
371
cin->initial_decode_frame = 0;
357
delta = sign_extend(AV_RL16(buf), 16);
372
delta = sign_extend(AV_RL16(buf), 16);
361
376
while (buf < buf_end) {
362
delta += cinaudio_delta16_table[*buf++];
363
delta = av_clip_int16(delta);
377
delta += cinaudio_delta16_table[*buf++];
378
delta = av_clip_int16(delta);
364
379
*samples++ = delta;
366
381
cin->delta = delta;
371
386
return avpkt->size;
375
389
AVCodec ff_dsicinvideo_decoder = {
376
390
.name = "dsicinvideo",
377
391
.type = AVMEDIA_TYPE_VIDEO,
378
.id = CODEC_ID_DSICINVIDEO,
392
.id = AV_CODEC_ID_DSICINVIDEO,
379
393
.priv_data_size = sizeof(CinVideoContext),
380
394
.init = cinvideo_decode_init,
381
395
.close = cinvideo_decode_end,
382
396
.decode = cinvideo_decode_frame,
383
397
.capabilities = CODEC_CAP_DR1,
384
.long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
398
.long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
387
401
AVCodec ff_dsicinaudio_decoder = {
388
402
.name = "dsicinaudio",
389
403
.type = AVMEDIA_TYPE_AUDIO,
390
.id = CODEC_ID_DSICINAUDIO,
404
.id = AV_CODEC_ID_DSICINAUDIO,
391
405
.priv_data_size = sizeof(CinAudioContext),
392
406
.init = cinaudio_decode_init,
393
407
.decode = cinaudio_decode_frame,
394
408
.capabilities = CODEC_CAP_DR1,
395
.long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN audio"),
409
.long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN audio"),