Browse code

Interplay C93 demuxer and video decoder patch by Anssi Hannula, anssi.hannula gmail com

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

Anssi Hannula authored on 2007/04/07 23:25:25
Showing 10 changed files
... ...
@@ -77,6 +77,7 @@ version <next>
77 77
 - DNxHD decoder
78 78
 - Gamecube movie (.THP) playback system
79 79
 - Blackfin optimizations
80
+- Interplay C93 demuxer and video decoder
80 81
 
81 82
 version 0.4.9-pre1:
82 83
 
... ...
@@ -903,6 +903,8 @@ library:
903 903
 different game cutscenes repacked for use with ScummVM.
904 904
 @item THP @tab    @tab X
905 905
 @tab Used on the Nintendo GameCube (video only)
906
+@item C93 @tab    @tab X
907
+@tab Used in the game Cyberia from Interplay.
906 908
 @end multitable
907 909
 
908 910
 @code{X} means that encoding (resp. decoding) is supported.
... ...
@@ -1012,6 +1014,7 @@ following image formats are supported:
1012 1012
 @item Tiertex Seq Video      @tab     @tab  X @tab Codec used in DOS CDROM FlashBack game.
1013 1013
 @item DXA Video              @tab     @tab  X @tab Codec originally used in Feeble Files game.
1014 1014
 @item AVID DNxHD             @tab     @tab  X @tab aka SMPTE VC3
1015
+@item C93 Video              @tab     @tab  X @tab Codec used in Cyberia game.
1015 1016
 @end multitable
1016 1017
 
1017 1018
 @code{X} means that encoding (resp. decoding) is supported.
... ...
@@ -56,6 +56,7 @@ OBJS-$(CONFIG_ASV2_ENCODER)            += asv1.o
56 56
 OBJS-$(CONFIG_AVS_DECODER)             += avs.o
57 57
 OBJS-$(CONFIG_BMP_DECODER)             += bmp.o
58 58
 OBJS-$(CONFIG_BMP_ENCODER)             += bmpenc.o
59
+OBJS-$(CONFIG_C93_DECODER)             += c93.o
59 60
 OBJS-$(CONFIG_CAVS_DECODER)            += cavs.o cavsdsp.o
60 61
 OBJS-$(CONFIG_CINEPAK_DECODER)         += cinepak.o
61 62
 OBJS-$(CONFIG_CLJR_DECODER)            += cljr.o
... ...
@@ -59,6 +59,7 @@ void avcodec_register_all(void)
59 59
     REGISTER_ENCDEC (ASV2, asv2);
60 60
     REGISTER_DECODER(AVS, avs);
61 61
     REGISTER_ENCDEC (BMP, bmp);
62
+    REGISTER_DECODER(C93, c93);
62 63
     REGISTER_DECODER(CAVS, cavs);
63 64
     REGISTER_DECODER(CINEPAK, cinepak);
64 65
     REGISTER_DECODER(CLJR, cljr);
... ...
@@ -160,6 +160,7 @@ enum CodecID {
160 160
     CODEC_ID_DNXHD,
161 161
     CODEC_ID_THP,
162 162
     CODEC_ID_SGI,
163
+    CODEC_ID_C93,
163 164
 
164 165
     /* various PCM "codecs" */
165 166
     CODEC_ID_PCM_S16LE= 0x10000,
... ...
@@ -2251,6 +2252,7 @@ extern AVCodec asv1_decoder;
2251 2251
 extern AVCodec asv2_decoder;
2252 2252
 extern AVCodec avs_decoder;
2253 2253
 extern AVCodec bmp_decoder;
2254
+extern AVCodec c93_decoder;
2254 2255
 extern AVCodec cavs_decoder;
2255 2256
 extern AVCodec cinepak_decoder;
2256 2257
 extern AVCodec cljr_decoder;
2257 2258
new file mode 100644
... ...
@@ -0,0 +1,253 @@
0
+/*
1
+ * Interplay C93 video decoder
2
+ * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
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,
19
+ * MA 02110-1301 USA
20
+ */
21
+
22
+#include "avcodec.h"
23
+#include "bytestream.h"
24
+
25
+typedef struct {
26
+    AVFrame pictures[2];
27
+    int currentpic;
28
+} C93DecoderContext;
29
+
30
+typedef enum {
31
+    C93_8X8_FROM_PREV  = 0x02,
32
+    C93_4X4_FROM_PREV  = 0x06,
33
+    C93_4X4_FROM_CURR  = 0x07,
34
+    C93_8X8_2COLOR     = 0x08,
35
+    C93_4X4_2COLOR     = 0x0A,
36
+    C93_4X4_4COLOR_GRP = 0x0B,
37
+    C93_4X4_4COLOR     = 0x0D,
38
+    C93_NOOP           = 0x0E,
39
+    C93_8X8_INTRA      = 0x0F,
40
+} C93BlockType;
41
+
42
+#define WIDTH   320
43
+#define HEIGHT  192
44
+
45
+#define C93_HAS_PALETTE 0x01
46
+#define C93_FIRST_FRAME 0x02
47
+
48
+static int c93_decode_init(AVCodecContext *avctx)
49
+{
50
+    avctx->pix_fmt = PIX_FMT_PAL8;
51
+    return 0;
52
+}
53
+
54
+static int c93_decode_end(AVCodecContext *avctx)
55
+{
56
+    C93DecoderContext * const c93 = avctx->priv_data;
57
+
58
+    if (c93->pictures[0].data[0])
59
+        avctx->release_buffer(avctx, &c93->pictures[0]);
60
+    if (c93->pictures[1].data[0])
61
+        avctx->release_buffer(avctx, &c93->pictures[1]);
62
+    return 0;
63
+}
64
+
65
+static inline int c93_copy_block(AVCodecContext *avctx, uint8_t *to,
66
+        uint8_t *from, int offset, int height, int stride)
67
+{
68
+    int i;
69
+    int width = height;
70
+    int from_x = offset % WIDTH;
71
+    int from_y = offset / WIDTH;
72
+    int overflow = from_x + width - WIDTH;
73
+
74
+    if (!from) {
75
+        /* silently ignoring predictive blocks in first frame */
76
+        return 0;
77
+    }
78
+
79
+    if (from_y + height > HEIGHT) {
80
+        av_log(avctx, AV_LOG_ERROR, "invalid offset %d during C93 decoding\n",
81
+               offset);
82
+        return -1;
83
+    }
84
+
85
+    if (overflow > 0) {
86
+        width -= overflow;
87
+        for (i = 0; i < height; i++) {
88
+            memcpy(&to[i*stride+width], &from[(from_y+i)*stride], overflow);
89
+        }
90
+    }
91
+
92
+    for (i = 0; i < height; i++) {
93
+        memcpy(&to[i*stride], &from[(from_y+i)*stride+from_x], width);
94
+    }
95
+
96
+    return 0;
97
+}
98
+
99
+static inline void c93_draw_n_color(uint8_t *out, int stride, int width,
100
+         int height, int bpp, uint8_t cols[4], uint8_t grps[4], uint32_t col)
101
+{
102
+    int x, y;
103
+    for (y = 0; y < height; y++) {
104
+        if (grps)
105
+            cols[0] = grps[3 * (y >> 1)];
106
+        for (x = 0; x < width; x++) {
107
+            if (grps)
108
+                cols[1]= grps[(x >> 1) + 1];
109
+            out[x + y*stride] = cols[col & ((1 << bpp) - 1)];
110
+            col >>= bpp;
111
+        }
112
+    }
113
+}
114
+
115
+static int c93_decode_frame(AVCodecContext *avctx, void *data,
116
+                            int *data_size, uint8_t * buf, int buf_size)
117
+{
118
+    C93DecoderContext * const c93 = avctx->priv_data;
119
+    AVFrame * const newpic = &c93->pictures[c93->currentpic];
120
+    AVFrame * const oldpic = &c93->pictures[c93->currentpic^1];
121
+    AVFrame *picture = data;
122
+    uint8_t *out;
123
+    int stride, i, x, y;
124
+    C93BlockType bt = 0;
125
+
126
+    c93->currentpic ^= 1;
127
+
128
+    newpic->reference = 1;
129
+    newpic->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
130
+                         FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
131
+    if (avctx->reget_buffer(avctx, newpic)) {
132
+        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
133
+        return -1;
134
+    }
135
+
136
+    stride = newpic->linesize[0];
137
+
138
+    if (buf[0] & C93_FIRST_FRAME) {
139
+        newpic->pict_type = FF_I_TYPE;
140
+        newpic->key_frame = 1;
141
+    } else {
142
+        newpic->pict_type = FF_P_TYPE;
143
+        newpic->key_frame = 0;
144
+    }
145
+
146
+    if (*buf++ & C93_HAS_PALETTE) {
147
+        uint32_t *palette = (uint32_t *) newpic->data[1];
148
+        uint8_t *palbuf = buf + buf_size - 768 - 1;
149
+        for (i = 0; i < 256; i++) {
150
+            palette[i] = bytestream_get_be24(&palbuf);
151
+        }
152
+    } else {
153
+        if (oldpic->data[1])
154
+            memcpy(newpic->data[1], oldpic->data[1], 256 * 4);
155
+    }
156
+
157
+    for (y = 0; y < HEIGHT; y += 8) {
158
+        out = newpic->data[0] + y * stride;
159
+        for (x = 0; x < WIDTH; x += 8) {
160
+            uint8_t *copy_from = oldpic->data[0];
161
+            unsigned int offset, j;
162
+            uint8_t cols[4], grps[4];
163
+
164
+            if (!bt)
165
+                bt = *buf++;
166
+
167
+            switch (bt & 0x0F) {
168
+            case C93_8X8_FROM_PREV:
169
+                offset = bytestream_get_le16(&buf);
170
+                if (c93_copy_block(avctx, out, copy_from, offset, 8, stride))
171
+                    return -1;
172
+                break;
173
+
174
+            case C93_4X4_FROM_CURR:
175
+                copy_from = newpic->data[0];
176
+            case C93_4X4_FROM_PREV:
177
+                for (j = 0; j < 8; j += 4) {
178
+                    for (i = 0; i < 8; i += 4) {
179
+                        offset = bytestream_get_le16(&buf);
180
+                        if (c93_copy_block(avctx, &out[j*stride+i],
181
+                                           copy_from, offset, 4, stride))
182
+                            return -1;
183
+                    }
184
+                }
185
+                break;
186
+
187
+            case C93_8X8_2COLOR:
188
+                bytestream_get_buffer(&buf, cols, 2);
189
+                for (i = 0; i < 8; i++) {
190
+                    c93_draw_n_color(out + i*stride, stride, 8, 1, 1, cols,
191
+                                     NULL, *buf++);
192
+                }
193
+
194
+                break;
195
+
196
+            case C93_4X4_2COLOR:
197
+            case C93_4X4_4COLOR:
198
+            case C93_4X4_4COLOR_GRP:
199
+                for (j = 0; j < 8; j += 4) {
200
+                    for (i = 0; i < 8; i += 4) {
201
+                        if ((bt & 0x0F) == C93_4X4_2COLOR) {
202
+                            bytestream_get_buffer(&buf, cols, 2);
203
+                            c93_draw_n_color(out + i + j*stride, stride, 4, 4,
204
+                                    1, cols, NULL, bytestream_get_le16(&buf));
205
+                        } else if ((bt & 0x0F) == C93_4X4_4COLOR) {
206
+                            bytestream_get_buffer(&buf, cols, 4);
207
+                            c93_draw_n_color(out + i + j*stride, stride, 4, 4,
208
+                                    2, cols, NULL, bytestream_get_le32(&buf));
209
+                        } else {
210
+                            bytestream_get_buffer(&buf, grps, 4);
211
+                            c93_draw_n_color(out + i + j*stride, stride, 4, 4,
212
+                                    1, cols, grps, bytestream_get_le16(&buf));
213
+                        }
214
+                    }
215
+                }
216
+                break;
217
+
218
+            case C93_NOOP:
219
+                break;
220
+
221
+            case C93_8X8_INTRA:
222
+                for (j = 0; j < 8; j++)
223
+                    bytestream_get_buffer(&buf, out + j*stride, 8);
224
+                break;
225
+
226
+            default:
227
+                av_log(avctx, AV_LOG_ERROR, "unexpected type %x at %dx%d\n",
228
+                       bt & 0x0F, x, y);
229
+                return -1;
230
+            }
231
+            bt >>= 4;
232
+            out += 8;
233
+        }
234
+    }
235
+
236
+    *picture = *newpic;
237
+    *data_size = sizeof(AVFrame);
238
+
239
+    return buf_size;
240
+}
241
+
242
+AVCodec c93_decoder = {
243
+    "c93",
244
+    CODEC_TYPE_VIDEO,
245
+    CODEC_ID_C93,
246
+    sizeof(C93DecoderContext),
247
+    c93_decode_init,
248
+    NULL,
249
+    c93_decode_end,
250
+    c93_decode_frame,
251
+    CODEC_CAP_DR1,
252
+};
... ...
@@ -28,6 +28,7 @@ OBJS-$(CONFIG_AVI_DEMUXER)               += avidec.o riff.o
28 28
 OBJS-$(CONFIG_AVI_MUXER)                 += avienc.o riff.o
29 29
 OBJS-$(CONFIG_AVISYNTH)                  += avisynth.o
30 30
 OBJS-$(CONFIG_AVS_DEMUXER)               += avs.o vocdec.o voc.o riff.o
31
+OBJS-$(CONFIG_C93_DEMUXER)               += c93.o
31 32
 OBJS-$(CONFIG_CRC_MUXER)                 += crc.o
32 33
 OBJS-$(CONFIG_FRAMECRC_MUXER)            += crc.o
33 34
 OBJS-$(CONFIG_DAUD_DEMUXER)              += daud.o
... ...
@@ -58,6 +58,7 @@ void av_register_all(void)
58 58
     av_register_input_format(&avisynth_demuxer);
59 59
 #endif
60 60
     REGISTER_DEMUXER (AVS, avs);
61
+    REGISTER_DEMUXER (C93, c93);
61 62
     REGISTER_MUXER   (CRC, crc);
62 63
     REGISTER_DEMUXER (DAUD, daud);
63 64
     REGISTER_DEMUXER (DC1394, dc1394);
... ...
@@ -32,6 +32,7 @@ extern AVInputFormat audio_demuxer;
32 32
 extern AVInputFormat avi_demuxer;
33 33
 extern AVInputFormat avisynth_demuxer;
34 34
 extern AVInputFormat avs_demuxer;
35
+extern AVInputFormat c93_demuxer;
35 36
 extern AVInputFormat daud_demuxer;
36 37
 extern AVInputFormat dc1394_demuxer;
37 38
 extern AVInputFormat dsicin_demuxer;
38 39
new file mode 100644
... ...
@@ -0,0 +1,202 @@
0
+/*
1
+ * Interplay C93 demuxer
2
+ * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
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,
19
+ * MA 02110-1301 USA
20
+ */
21
+
22
+#include "avformat.h"
23
+#include "voc.h"
24
+
25
+typedef struct {
26
+    uint16_t index;
27
+    uint8_t length;
28
+    uint8_t frames;
29
+} C93BlockRecord;
30
+
31
+typedef struct {
32
+    voc_dec_context_t voc;
33
+
34
+    C93BlockRecord block_records[512];
35
+    int current_block;
36
+
37
+    uint32_t frame_offsets[32];
38
+    int current_frame;
39
+    int next_pkt_is_audio;
40
+
41
+    AVStream *audio;
42
+} C93DemuxContext;
43
+
44
+static int c93_probe(AVProbeData *p)
45
+{
46
+    if (p->buf_size < 13)
47
+        return 0;
48
+
49
+    if (p->buf[0] == 0x01 && p->buf[1] == 0x00 &&
50
+        p->buf[4] == 0x01 + p->buf[2] &&
51
+        p->buf[8] == p->buf[4] + p->buf[6] &&
52
+        p->buf[12] == p->buf[8] + p->buf[10])
53
+        return AVPROBE_SCORE_MAX;
54
+
55
+    return 0;
56
+}
57
+
58
+static int c93_read_header(AVFormatContext *s,
59
+                           AVFormatParameters *ap)
60
+{
61
+    AVStream *video;
62
+    ByteIOContext *pb = &s->pb;
63
+    C93DemuxContext *c93 = s->priv_data;
64
+    int i;
65
+    int framecount = 0;
66
+
67
+    for (i = 0; i < 512; i++) {
68
+        c93->block_records[i].index = get_le16(pb);
69
+        c93->block_records[i].length = get_byte(pb);
70
+        c93->block_records[i].frames = get_byte(pb);
71
+        if (c93->block_records[i].frames > 32) {
72
+            av_log(s, AV_LOG_ERROR, "too many frames in block\n");
73
+            return AVERROR_INVALIDDATA;
74
+        }
75
+        framecount += c93->block_records[i].frames;
76
+    }
77
+
78
+    /* Audio streams are added if audio packets are found */
79
+    s->ctx_flags |= AVFMTCTX_NOHEADER;
80
+
81
+    video = av_new_stream(s, 0);
82
+    if (!video)
83
+        return AVERROR_NOMEM;
84
+
85
+    video->codec->codec_type = CODEC_TYPE_VIDEO;
86
+    video->codec->codec_id = CODEC_ID_C93;
87
+    video->codec->width = 320;
88
+    video->codec->height = 192;
89
+    /* 4:3 320x200 with 8 empty lines */
90
+    video->codec->sample_aspect_ratio = (AVRational) { 5, 6 };
91
+    video->time_base = (AVRational) { 2, 25 };
92
+    video->nb_frames = framecount;
93
+    video->duration = framecount;
94
+    video->start_time = 0;
95
+
96
+    c93->current_block = 0;
97
+    c93->current_frame = 0;
98
+    c93->next_pkt_is_audio = 0;
99
+    return 0;
100
+}
101
+
102
+#define C93_HAS_PALETTE 0x01
103
+#define C93_FIRST_FRAME 0x02
104
+
105
+static int c93_read_packet(AVFormatContext *s, AVPacket *pkt)
106
+{
107
+    ByteIOContext *pb = &s->pb;
108
+    C93DemuxContext *c93 = s->priv_data;
109
+    C93BlockRecord *br = &c93->block_records[c93->current_block];
110
+    int datasize;
111
+    int ret, i;
112
+
113
+    if (c93->next_pkt_is_audio) {
114
+        c93->current_frame++;
115
+        c93->next_pkt_is_audio = 0;
116
+        datasize = get_le16(pb);
117
+        if (datasize > 42) {
118
+            if (!c93->audio) {
119
+                c93->audio = av_new_stream(s, 1);
120
+                if (!c93->audio)
121
+                    return AVERROR_NOMEM;
122
+                c93->audio->codec->codec_type = CODEC_TYPE_AUDIO;
123
+            }
124
+            url_fskip(pb, 26); /* VOC header */
125
+            ret = voc_get_packet(s, pkt, c93->audio, datasize - 26);
126
+            if (ret > 0) {
127
+                pkt->stream_index = 1;
128
+                pkt->flags |= PKT_FLAG_KEY;
129
+                return ret;
130
+            }
131
+        }
132
+    }
133
+    if (c93->current_frame >= br->frames) {
134
+        if (c93->current_block >= 511 || !br[1].length)
135
+            return AVERROR_IO;
136
+        br++;
137
+        c93->current_block++;
138
+        c93->current_frame = 0;
139
+    }
140
+
141
+    if (c93->current_frame == 0) {
142
+        url_fseek(pb, br->index * 2048, SEEK_SET);
143
+        for (i = 0; i < 32; i++) {
144
+            c93->frame_offsets[i] = get_le32(pb);
145
+        }
146
+    }
147
+
148
+    url_fseek(pb,br->index * 2048 +
149
+            c93->frame_offsets[c93->current_frame], SEEK_SET);
150
+    datasize = get_le16(pb); /* video frame size */
151
+
152
+    ret = av_new_packet(pkt, datasize + 768 + 1);
153
+    if (ret < 0)
154
+        return ret;
155
+    pkt->data[0] = 0;
156
+    pkt->size = datasize + 1;
157
+
158
+    ret = get_buffer(pb, pkt->data + 1, datasize);
159
+    if (ret < datasize) {
160
+        ret = AVERROR_IO;
161
+        goto fail;
162
+    }
163
+
164
+    datasize = get_le16(pb); /* palette size */
165
+    if (datasize) {
166
+        if (datasize != 768) {
167
+            av_log(s, AV_LOG_ERROR, "invalid palette size %u\n", datasize);
168
+            ret = AVERROR_INVALIDDATA;
169
+            goto fail;
170
+        }
171
+        pkt->data[0] |= C93_HAS_PALETTE;
172
+        ret = get_buffer(pb, pkt->data + pkt->size, datasize);
173
+        if (ret < datasize) {
174
+            ret = AVERROR_IO;
175
+            goto fail;
176
+        }
177
+        pkt->size += 768;
178
+    }
179
+    pkt->stream_index = 0;
180
+    c93->next_pkt_is_audio = 1;
181
+
182
+    /* only the first frame is guaranteed to not reference previous frames */
183
+    if (c93->current_block == 0 && c93->current_frame == 0) {
184
+        pkt->flags |= PKT_FLAG_KEY;
185
+        pkt->data[0] |= C93_FIRST_FRAME;
186
+    }
187
+    return 0;
188
+
189
+    fail:
190
+    av_free_packet(pkt);
191
+    return ret;
192
+}
193
+
194
+AVInputFormat c93_demuxer = {
195
+    "c93",
196
+    "Interplay C93",
197
+    sizeof(C93DemuxContext),
198
+    c93_probe,
199
+    c93_read_header,
200
+    c93_read_packet,
201
+};