Browse code

support for Delphine Software .cin files demuxing / audio and video decoding, by Gregory Montoir %cyx A users P sourceforge P net%

Originally committed as revision 6659 to svn://svn.ffmpeg.org/ffmpeg/trunk

Baptiste Coudurier authored on 2006/10/12 01:57:00
Showing 8 changed files
... ...
@@ -65,6 +65,8 @@ OBJS-$(CONFIG_CLJR_ENCODER)            += cljr.o
65 65
 OBJS-$(CONFIG_COOK_DECODER)            += cook.o
66 66
 OBJS-$(CONFIG_CSCD_DECODER)            += cscd.o lzo.o
67 67
 OBJS-$(CONFIG_CYUV_DECODER)            += cyuv.o
68
+OBJS-$(CONFIG_DSICINVIDEO_DECODER)     += dsicinav.o
69
+OBJS-$(CONFIG_DSICINAUDIO_DECODER)     += dsicinav.o
68 70
 OBJS-$(CONFIG_DVBSUB_DECODER)          += dvbsubdec.o
69 71
 OBJS-$(CONFIG_DVBSUB_ENCODER)          += dvbsub.o
70 72
 OBJS-$(CONFIG_DVDSUB_DECODER)          += dvdsub.o
... ...
@@ -548,6 +548,12 @@ void avcodec_register_all(void)
548 548
 #ifdef CONFIG_TARGA_DECODER
549 549
     register_avcodec(&targa_decoder);
550 550
 #endif //CONFIG_TARGA_DECODER
551
+#ifdef CONFIG_DSICINVIDEO_DECODER
552
+    register_avcodec(&dsicinvideo_decoder);
553
+#endif //CONFIG_DSICINVIDEO_DECODER
554
+#ifdef CONFIG_DSICINAUDIO_DECODER
555
+    register_avcodec(&dsicinaudio_decoder);
556
+#endif //CONFIG_DSICINAUDIO_DECODER
551 557
 
552 558
 #if defined(CONFIG_AMR_NB) || defined(CONFIG_AMR_NB_FIXED)
553 559
 #ifdef CONFIG_AMR_NB_DECODER
... ...
@@ -145,6 +145,7 @@ enum CodecID {
145 145
     CODEC_ID_VP6,
146 146
     CODEC_ID_VP6F,
147 147
     CODEC_ID_TARGA,
148
+    CODEC_ID_DSICINVIDEO,
148 149
 
149 150
     /* various pcm "codecs" */
150 151
     CODEC_ID_PCM_S16LE= 0x10000,
... ...
@@ -228,6 +229,7 @@ enum CodecID {
228 228
     CODEC_ID_SMACKAUDIO,
229 229
     CODEC_ID_QCELP,
230 230
     CODEC_ID_WAVPACK,
231
+    CODEC_ID_DSICINAUDIO,
231 232
 
232 233
     /* subtitle codecs */
233 234
     CODEC_ID_DVD_SUBTITLE= 0x17000,
... ...
@@ -2294,6 +2296,8 @@ extern AVCodec cavs_decoder;
2294 2294
 extern AVCodec vmnc_decoder;
2295 2295
 extern AVCodec wavpack_decoder;
2296 2296
 extern AVCodec targa_decoder;
2297
+extern AVCodec dsicinvideo_decoder;
2298
+extern AVCodec dsicinaudio_decoder;
2297 2299
 
2298 2300
 /* pcm codecs */
2299 2301
 #define PCM_CODEC(id, name) \
2300 2302
new file mode 100644
... ...
@@ -0,0 +1,362 @@
0
+/*
1
+ * Delphine Software International CIN Audio/Video Decoders
2
+ * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file dsicinav.c
23
+ * Delphine Software International CIN audio/video decoders
24
+ */
25
+
26
+#include "avcodec.h"
27
+#include "common.h"
28
+
29
+
30
+typedef enum CinVideoBitmapIndex {
31
+    CIN_CUR_BMP = 0, /* current */
32
+    CIN_PRE_BMP = 1, /* previous */
33
+    CIN_INT_BMP = 2  /* intermediate */
34
+} CinVideoBitmapIndex;
35
+
36
+typedef struct CinVideoContext {
37
+    AVCodecContext *avctx;
38
+    AVFrame frame;
39
+    unsigned int bitmap_size;
40
+    uint32_t palette[256];
41
+    uint8_t *bitmap_table[3];
42
+} CinVideoContext;
43
+
44
+typedef struct CinAudioContext {
45
+    AVCodecContext *avctx;
46
+    int initial_decode_frame;
47
+    int delta;
48
+} CinAudioContext;
49
+
50
+
51
+/* table defining a geometric sequence with multiplier = 32767 ^ (1 / 128) */
52
+static const int16_t cinaudio_delta16_table[256] = {
53
+         0,      0,      0,      0,      0,      0,      0,      0,
54
+         0,      0,      0,      0,      0,      0,      0,      0,
55
+         0,      0,      0, -30210, -27853, -25680, -23677, -21829,
56
+    -20126, -18556, -17108, -15774, -14543, -13408, -12362, -11398,
57
+    -10508,  -9689,  -8933,  -8236,  -7593,  -7001,  -6455,  -5951,
58
+     -5487,  -5059,  -4664,  -4300,  -3964,  -3655,  -3370,  -3107,
59
+     -2865,  -2641,  -2435,  -2245,  -2070,  -1908,  -1759,  -1622,
60
+     -1495,  -1379,  -1271,  -1172,  -1080,   -996,   -918,   -847,
61
+      -781,   -720,   -663,   -612,   -564,   -520,   -479,   -442,
62
+      -407,   -376,   -346,   -319,   -294,   -271,   -250,   -230,
63
+      -212,   -196,   -181,   -166,   -153,   -141,   -130,   -120,
64
+      -111,   -102,    -94,    -87,    -80,    -74,    -68,    -62,
65
+       -58,    -53,    -49,    -45,    -41,    -38,    -35,    -32,
66
+       -30,    -27,    -25,    -23,    -21,    -20,    -18,    -17,
67
+       -15,    -14,    -13,    -12,    -11,    -10,     -9,     -8,
68
+        -7,     -6,     -5,     -4,     -3,     -2,     -1,      0,
69
+         0,      1,      2,      3,      4,      5,      6,      7,
70
+         8,      9,     10,     11,     12,     13,     14,     15,
71
+        17,     18,     20,     21,     23,     25,     27,     30,
72
+        32,     35,     38,     41,     45,     49,     53,     58,
73
+        62,     68,     74,     80,     87,     94,    102,    111,
74
+       120,    130,    141,    153,    166,    181,    196,    212,
75
+       230,    250,    271,    294,    319,    346,    376,    407,
76
+       442,    479,    520,    564,    612,    663,    720,    781,
77
+       847,    918,    996,   1080,   1172,   1271,   1379,   1495,
78
+      1622,   1759,   1908,   2070,   2245,   2435,   2641,   2865,
79
+      3107,   3370,   3655,   3964,   4300,   4664,   5059,   5487,
80
+      5951,   6455,   7001,   7593,   8236,   8933,   9689,  10508,
81
+     11398,  12362,  13408,  14543,  15774,  17108,  18556,  20126,
82
+     21829,  23677,  25680,  27853,  30210,      0,      0,      0,
83
+         0,      0,      0,      0,      0,      0,      0,      0,
84
+         0,      0,      0,      0,      0,      0,      0,      0
85
+};
86
+
87
+
88
+static int cinvideo_decode_init(AVCodecContext *avctx)
89
+{
90
+    CinVideoContext *cin = (CinVideoContext *)avctx->priv_data;
91
+    unsigned int i;
92
+
93
+    cin->avctx = avctx;
94
+    avctx->pix_fmt = PIX_FMT_PAL8;
95
+    avctx->has_b_frames = 0;
96
+
97
+    cin->frame.data[0] = NULL;
98
+
99
+    cin->bitmap_size = avctx->width * avctx->height;
100
+    for (i = 0; i < 3; ++i) {
101
+        cin->bitmap_table[i] = av_mallocz(cin->bitmap_size);
102
+        if (!cin->bitmap_table[i])
103
+            av_log(avctx, AV_LOG_ERROR, "Can't allocate bitmap buffers.\n");
104
+    }
105
+
106
+    return 0;
107
+}
108
+
109
+static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst, int size)
110
+{
111
+    while (size--)
112
+        *dst++ += *src++;
113
+}
114
+
115
+static int cin_decode_huffman(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
116
+{
117
+    int b, huff_code = 0;
118
+    unsigned char huff_code_table[15];
119
+    unsigned char *dst_cur = dst;
120
+    unsigned char *dst_end = dst + dst_size;
121
+    const unsigned char *src_end = src + src_size;
122
+
123
+    memcpy(huff_code_table, src, 15); src += 15; src_size -= 15;
124
+
125
+    while (src < src_end) {
126
+        huff_code = *src++;
127
+        if ((huff_code >> 4) == 15) {
128
+            b = huff_code << 4;
129
+            huff_code = *src++;
130
+            *dst_cur++ = b | (huff_code >> 4);
131
+        } else
132
+            *dst_cur++ = huff_code_table[huff_code >> 4];
133
+        if (dst_cur >= dst_end)
134
+            break;
135
+
136
+        huff_code &= 15;
137
+        if (huff_code == 15) {
138
+            *dst_cur++ = *src++;
139
+        } else
140
+            *dst_cur++ = huff_code_table[huff_code];
141
+        if (dst_cur >= dst_end)
142
+            break;
143
+    }
144
+
145
+    return dst_cur - dst;
146
+}
147
+
148
+static void cin_decode_lzss(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
149
+{
150
+    uint16_t cmd;
151
+    int i, sz, offset, code;
152
+    unsigned char *dst_end = dst + dst_size;
153
+    const unsigned char *src_end = src + src_size;
154
+
155
+    while (src < src_end && dst < dst_end) {
156
+        code = *src++;
157
+        for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) {
158
+            if (code & (1 << i)) {
159
+                *dst++ = *src++;
160
+            } else {
161
+                cmd = LE_16(src); src += 2;
162
+                offset = cmd >> 4;
163
+                sz = (cmd & 0xF) + 2;
164
+                /* don't use memcpy/memmove here as the decoding routine (ab)uses */
165
+                /* buffer overlappings to repeat bytes in the destination */
166
+                sz = FFMIN(sz, dst_end - dst);
167
+                while (sz--) {
168
+                    *dst = *(dst - offset - 1);
169
+                    ++dst;
170
+                }
171
+            }
172
+        }
173
+    }
174
+}
175
+
176
+static void cin_decode_rle(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
177
+{
178
+    int len, code;
179
+    unsigned char *dst_end = dst + dst_size;
180
+    const unsigned char *src_end = src + src_size;
181
+
182
+    while (src < src_end && dst < dst_end) {
183
+        code = *src++;
184
+        if (code & 0x80) {
185
+            len = code - 0x7F;
186
+            memset(dst, *src++, FFMIN(len, dst_end - dst));
187
+        } else {
188
+            len = code + 1;
189
+            memcpy(dst, src, FFMIN(len, dst_end - dst));
190
+            src += len;
191
+        }
192
+        dst += len;
193
+    }
194
+}
195
+
196
+static int cinvideo_decode_frame(AVCodecContext *avctx,
197
+                                 void *data, int *data_size,
198
+                                 uint8_t *buf, int buf_size)
199
+{
200
+    CinVideoContext *cin = (CinVideoContext *)avctx->priv_data;
201
+    int i, y, palette_type, palette_colors_count, bitmap_frame_type, bitmap_frame_size;
202
+
203
+    cin->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
204
+    if (avctx->reget_buffer(avctx, &cin->frame)) {
205
+        av_log(cin->avctx, AV_LOG_ERROR, "delphinecinvideo: reget_buffer() failed to allocate a frame\n");
206
+        return -1;
207
+    }
208
+
209
+    palette_type = buf[0];
210
+    palette_colors_count = buf[1] | (buf[2] << 8);
211
+    bitmap_frame_type = buf[3];
212
+    buf += 4;
213
+
214
+    bitmap_frame_size = buf_size - 4;
215
+
216
+    /* handle palette */
217
+    if (palette_type == 0) {
218
+        for (i = 0; i < palette_colors_count; ++i) {
219
+            cin->palette[i] = (buf[2] << 16) | (buf[1] << 8) | buf[0];
220
+            buf += 3;
221
+            bitmap_frame_size -= 3;
222
+        }
223
+    } else {
224
+        for (i = 0; i < palette_colors_count; ++i) {
225
+            cin->palette[buf[0]] = (buf[3] << 16) | (buf[2] << 8) | buf[1];
226
+            buf += 4;
227
+            bitmap_frame_size -= 4;
228
+        }
229
+    }
230
+    memcpy(cin->frame.data[1], cin->palette, sizeof(cin->palette));
231
+    cin->frame.palette_has_changed = 1;
232
+
233
+    /* note: the decoding routines below assumes that surface.width = surface.pitch */
234
+    switch (bitmap_frame_type) {
235
+    case 9:
236
+        cin_decode_rle(buf, bitmap_frame_size,
237
+          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
238
+        break;
239
+    case 34:
240
+        cin_decode_rle(buf, bitmap_frame_size,
241
+          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
242
+        cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
243
+          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
244
+        break;
245
+    case 35:
246
+        cin_decode_huffman(buf, bitmap_frame_size,
247
+          cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
248
+        cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
249
+          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
250
+        break;
251
+    case 36:
252
+        bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
253
+          cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
254
+        cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
255
+          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
256
+        cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
257
+          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
258
+        break;
259
+    case 37:
260
+        cin_decode_huffman(buf, bitmap_frame_size,
261
+          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
262
+        break;
263
+    case 38:
264
+        cin_decode_lzss(buf, bitmap_frame_size,
265
+          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
266
+        break;
267
+    case 39:
268
+        cin_decode_lzss(buf, bitmap_frame_size,
269
+          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
270
+        cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
271
+          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
272
+        break;
273
+    }
274
+
275
+    for (y = 0; y < cin->avctx->height; ++y)
276
+        memcpy(cin->frame.data[0] + (cin->avctx->height - 1 - y) * cin->frame.linesize[0],
277
+          cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
278
+          cin->avctx->width);
279
+
280
+    SWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_table[CIN_PRE_BMP]);
281
+
282
+    *data_size = sizeof(AVFrame);
283
+    *(AVFrame *)data = cin->frame;
284
+
285
+    return buf_size;
286
+}
287
+
288
+static int cinvideo_decode_end(AVCodecContext *avctx)
289
+{
290
+    CinVideoContext *cin = (CinVideoContext *)avctx->priv_data;
291
+    int i;
292
+
293
+    if (cin->frame.data[0])
294
+        avctx->release_buffer(avctx, &cin->frame);
295
+
296
+    for (i = 0; i < 3; ++i)
297
+        av_free(cin->bitmap_table[i]);
298
+
299
+    return 0;
300
+}
301
+
302
+static int cinaudio_decode_init(AVCodecContext *avctx)
303
+{
304
+    CinAudioContext *cin = (CinAudioContext *)avctx->priv_data;
305
+
306
+    cin->avctx = avctx;
307
+    cin->initial_decode_frame = 1;
308
+    cin->delta = 0;
309
+
310
+    return 0;
311
+}
312
+
313
+static int cinaudio_decode_frame(AVCodecContext *avctx,
314
+                                 void *data, int *data_size,
315
+                                 uint8_t *buf, int buf_size)
316
+{
317
+    CinAudioContext *cin = (CinAudioContext *)avctx->priv_data;
318
+    uint8_t *src = buf;
319
+    int16_t *samples = (int16_t *)data;
320
+
321
+    if (cin->initial_decode_frame) {
322
+        cin->initial_decode_frame = 0;
323
+        cin->delta = (int16_t)LE_16(src); src += 2;
324
+        *samples++ = cin->delta;
325
+        buf_size -= 2;
326
+    }
327
+    while (buf_size > 0) {
328
+        cin->delta += cinaudio_delta16_table[*src++];
329
+        cin->delta = clip(cin->delta, -32768, 32767);
330
+        *samples++ = cin->delta;
331
+        --buf_size;
332
+    }
333
+
334
+    *data_size = (uint8_t *)samples - (uint8_t *)data;
335
+
336
+    return src - buf;
337
+}
338
+
339
+
340
+AVCodec dsicinvideo_decoder = {
341
+    "dsicinvideo",
342
+    CODEC_TYPE_VIDEO,
343
+    CODEC_ID_DSICINVIDEO,
344
+    sizeof(CinVideoContext),
345
+    cinvideo_decode_init,
346
+    NULL,
347
+    cinvideo_decode_end,
348
+    cinvideo_decode_frame,
349
+    CODEC_CAP_DR1,
350
+};
351
+
352
+AVCodec dsicinaudio_decoder = {
353
+    "dsicinaudio",
354
+    CODEC_TYPE_AUDIO,
355
+    CODEC_ID_DSICINAUDIO,
356
+    sizeof(CinAudioContext),
357
+    cinaudio_decode_init,
358
+    NULL,
359
+    NULL,
360
+    cinaudio_decode_frame,
361
+};
... ...
@@ -29,6 +29,7 @@ OBJS-$(CONFIG_AVS_DEMUXER)               += avs.o
29 29
 OBJS-$(CONFIG_CRC_MUXER)                 += crc.o
30 30
 OBJS-$(CONFIG_FRAMECRC_MUXER)            += crc.o
31 31
 OBJS-$(CONFIG_DAUD_DEMUXER)              += daud.o
32
+OBJS-$(CONFIG_DSICIN_DEMUXER)            += dsicin.o
32 33
 OBJS-$(CONFIG_DV_DEMUXER)                += dv.o
33 34
 OBJS-$(CONFIG_DV_MUXER)                  += dvenc.o
34 35
 OBJS-$(CONFIG_EA_DEMUXER)                += electronicarts.o
... ...
@@ -105,6 +105,9 @@ void av_register_all(void)
105 105
     av_register_input_format(&dc1394_demuxer);
106 106
 #endif
107 107
 #endif /* CONFIG_DC1394 */
108
+#ifdef CONFIG_DSICIN_DEMUXER
109
+    av_register_input_format(&dsicin_demuxer);
110
+#endif
108 111
 #ifdef CONFIG_DV1394
109 112
 #ifdef CONFIG_DV1394_DEMUXER
110 113
     av_register_input_format(&dv1394_demuxer);
... ...
@@ -43,6 +43,7 @@ extern AVOutputFormat crc_muxer;
43 43
 extern AVOutputFormat framecrc_muxer;
44 44
 extern AVInputFormat daud_demuxer;
45 45
 extern AVInputFormat dc1394_demuxer;
46
+extern AVInputFormat dsicin_demuxer;
46 47
 extern AVInputFormat dv1394_demuxer;
47 48
 extern AVInputFormat dv_demuxer;
48 49
 extern AVOutputFormat dv_muxer;
49 50
new file mode 100644
... ...
@@ -0,0 +1,224 @@
0
+/*
1
+ * Delphine Software International CIN File Demuxer
2
+ * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file dsicin.c
23
+ * Delphine Software International CIN file demuxer
24
+ */
25
+
26
+#include "avformat.h"
27
+
28
+
29
+typedef struct CinFileHeader {
30
+    int video_frame_size;
31
+    int video_frame_width;
32
+    int video_frame_height;
33
+    int audio_frequency;
34
+    int audio_bits;
35
+    int audio_stereo;
36
+    int audio_frame_size;
37
+} CinFileHeader;
38
+
39
+typedef struct CinFrameHeader {
40
+    int audio_frame_type;
41
+    int video_frame_type;
42
+    int pal_colors_count;
43
+    int audio_frame_size;
44
+    int video_frame_size;
45
+} CinFrameHeader;
46
+
47
+typedef struct CinDemuxContext {
48
+    int audio_stream_index;
49
+    int video_stream_index;
50
+    CinFileHeader file_header;
51
+    int64_t audio_stream_pts;
52
+    int64_t video_stream_pts;
53
+    CinFrameHeader frame_header;
54
+    int audio_buffer_size;
55
+} CinDemuxContext;
56
+
57
+
58
+static int cin_probe(AVProbeData *p)
59
+{
60
+    if (p->buf_size < 18)
61
+        return 0;
62
+
63
+    /* header starts with this special marker */
64
+    if (LE_32(&p->buf[0]) != 0x55AA0000)
65
+        return 0;
66
+
67
+    /* for accuracy, check some header field values */
68
+    if (LE_32(&p->buf[12]) != 22050 || p->buf[16] != 16 || p->buf[17] != 0)
69
+        return 0;
70
+
71
+    return AVPROBE_SCORE_MAX;
72
+}
73
+
74
+static int cin_read_file_header(CinDemuxContext *cin, ByteIOContext *pb) {
75
+    CinFileHeader *hdr = &cin->file_header;
76
+
77
+    if (get_le32(pb) != 0x55AA0000)
78
+        return AVERROR_INVALIDDATA;
79
+
80
+    hdr->video_frame_size   = get_le32(pb);
81
+    hdr->video_frame_width  = get_le16(pb);
82
+    hdr->video_frame_height = get_le16(pb);
83
+    hdr->audio_frequency    = get_le32(pb);
84
+    hdr->audio_bits         = get_byte(pb);
85
+    hdr->audio_stereo       = get_byte(pb);
86
+    hdr->audio_frame_size   = get_le16(pb);
87
+
88
+    if (hdr->audio_frequency != 22050 || hdr->audio_bits != 16 || hdr->audio_stereo != 0)
89
+        return AVERROR_INVALIDDATA;
90
+
91
+    return 0;
92
+}
93
+
94
+static int cin_read_header(AVFormatContext *s, AVFormatParameters *ap)
95
+{
96
+    int rc;
97
+    CinDemuxContext *cin = (CinDemuxContext *)s->priv_data;
98
+    CinFileHeader *hdr = &cin->file_header;
99
+    ByteIOContext *pb = &s->pb;
100
+    AVStream *st;
101
+
102
+    rc = cin_read_file_header(cin, pb);
103
+    if (rc)
104
+        return rc;
105
+
106
+    cin->video_stream_pts = 0;
107
+    cin->audio_stream_pts = 0;
108
+    cin->audio_buffer_size = 0;
109
+
110
+    /* initialize the video decoder stream */
111
+    st = av_new_stream(s, 0);
112
+    if (!st)
113
+        return AVERROR_NOMEM;
114
+
115
+    av_set_pts_info(st, 32, 1, 12);
116
+    cin->video_stream_index = st->index;
117
+    st->codec->codec_type = CODEC_TYPE_VIDEO;
118
+    st->codec->codec_id = CODEC_ID_DSICINVIDEO;
119
+    st->codec->codec_tag = 0;  /* no fourcc */
120
+    st->codec->width = hdr->video_frame_width;
121
+    st->codec->height = hdr->video_frame_height;
122
+
123
+    /* initialize the audio decoder stream */
124
+    st = av_new_stream(s, 0);
125
+    if (!st)
126
+        return AVERROR_NOMEM;
127
+
128
+    av_set_pts_info(st, 32, 1, 22050);
129
+    cin->audio_stream_index = st->index;
130
+    st->codec->codec_type = CODEC_TYPE_AUDIO;
131
+    st->codec->codec_id = CODEC_ID_DSICINAUDIO;
132
+    st->codec->codec_tag = 0;  /* no tag */
133
+    st->codec->channels = 1;
134
+    st->codec->sample_rate = 22050;
135
+    st->codec->bits_per_sample = 16;
136
+    st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_sample * st->codec->channels;
137
+    st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
138
+
139
+    return 0;
140
+}
141
+
142
+static int cin_read_frame_header(CinDemuxContext *cin, ByteIOContext *pb) {
143
+    CinFrameHeader *hdr = &cin->frame_header;
144
+
145
+    hdr->video_frame_type = get_byte(pb);
146
+    hdr->audio_frame_type = get_byte(pb);
147
+    hdr->pal_colors_count = get_le16(pb);
148
+    hdr->video_frame_size = get_le32(pb);
149
+    hdr->audio_frame_size = get_le32(pb);
150
+
151
+    if (url_feof(pb) || url_ferror(pb))
152
+        return AVERROR_IO;
153
+
154
+    if (get_le32(pb) != 0xAA55AA55)
155
+        return AVERROR_INVALIDDATA;
156
+
157
+    return 0;
158
+}
159
+
160
+static int cin_read_packet(AVFormatContext *s, AVPacket *pkt)
161
+{
162
+    CinDemuxContext *cin = (CinDemuxContext *)s->priv_data;
163
+    ByteIOContext *pb = &s->pb;
164
+    CinFrameHeader *hdr = &cin->frame_header;
165
+    int rc, palette_type, pkt_size;
166
+
167
+    if (cin->audio_buffer_size == 0) {
168
+        rc = cin_read_frame_header(cin, pb);
169
+        if (rc)
170
+            return rc;
171
+
172
+        if ((int16_t)hdr->pal_colors_count < 0) {
173
+            hdr->pal_colors_count = -(int16_t)hdr->pal_colors_count;
174
+            palette_type = 1;
175
+        } else {
176
+            palette_type = 0;
177
+        }
178
+
179
+        /* palette and video packet */
180
+        pkt_size = (palette_type + 3) * hdr->pal_colors_count + hdr->video_frame_size;
181
+
182
+        if (av_new_packet(pkt, 4 + pkt_size))
183
+            return AVERROR_NOMEM;
184
+
185
+        pkt->stream_index = cin->video_stream_index;
186
+        pkt->pts = cin->video_stream_pts++;
187
+
188
+        pkt->data[0] = palette_type;
189
+        pkt->data[1] = hdr->pal_colors_count & 0xFF;
190
+        pkt->data[2] = hdr->pal_colors_count >> 8;
191
+        pkt->data[3] = hdr->video_frame_type;
192
+
193
+        if (get_buffer(pb, &pkt->data[4], pkt_size) != pkt_size)
194
+            return AVERROR_IO;
195
+
196
+        /* sound buffer will be processed on next read_packet() call */
197
+        cin->audio_buffer_size = hdr->audio_frame_size;
198
+        return 0;
199
+    }
200
+
201
+    /* audio packet */
202
+    if (av_new_packet(pkt, cin->audio_buffer_size))
203
+        return AVERROR_NOMEM;
204
+
205
+    pkt->stream_index = cin->audio_stream_index;
206
+    pkt->pts = cin->audio_stream_pts;
207
+    cin->audio_stream_pts += cin->audio_buffer_size * 2 / cin->file_header.audio_frame_size;
208
+
209
+    if (get_buffer(pb, pkt->data, cin->audio_buffer_size) != cin->audio_buffer_size)
210
+        return AVERROR_IO;
211
+
212
+    cin->audio_buffer_size = 0;
213
+    return 0;
214
+}
215
+
216
+AVInputFormat dsicin_demuxer = {
217
+    "dsicin",
218
+    "Delphine Software International CIN format",
219
+    sizeof(CinDemuxContext),
220
+    cin_probe,
221
+    cin_read_header,
222
+    cin_read_packet,
223
+};