Browse code

complete AVS playback system (from Creature Shock computer game), courtesy of Aurelien Jacobs <aurel at gnuage dot org>

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

Mike Melanson authored on 2006/02/17 12:17:42
Showing 10 changed files
... ...
@@ -40,6 +40,7 @@ version <next>
40 40
 - Zip Blocks Motion Video decoder
41 41
 - Improved Theora/VP3 decoder
42 42
 - True Audio (TTA) decoder
43
+- AVS demuxer and video decoder
43 44
 
44 45
 version 0.4.9-pre1:
45 46
 
... ...
@@ -700,6 +700,8 @@ library:
700 700
 @item Creative VOC @tab X @tab X @tab Created for the Sound Blaster Pro.
701 701
 @item American Laser Games MM  @tab    @tab X
702 702
 @tab Multimedia format used in games like Mad Dog McCree
703
+@item AVS @tab    @tab X
704
+@tab Multimedia format used by the Creature Shock game.
703 705
 @end multitable
704 706
 
705 707
 @code{X} means that encoding (resp. decoding) is supported.
... ...
@@ -792,6 +794,7 @@ following image formats are supported:
792 792
 @item CamStudio              @tab     @tab  X @tab fourcc: CSCD
793 793
 @item American Laser Games Video  @tab    @tab X @tab Used in games like Mad Dog McCree
794 794
 @item ZMBV                   @tab     @tab  X @tab
795
+@item AVS Video              @tab     @tab  X @tab Video encoding used by the Creature Shock game.
795 796
 @end multitable
796 797
 
797 798
 @code{X} means that encoding (resp. decoding) is supported.
... ...
@@ -29,6 +29,9 @@ endif
29 29
 ifneq ($(CONFIG_ASV1_DECODER)$(CONFIG_ASV1_ENCODER)$(CONFIG_ASV2_DECODER)$(CONFIG_ASV2_ENCODER),)
30 30
     OBJS+= asv1.o
31 31
 endif
32
+ifeq ($(CONFIG_AVS_DECODER),yes)
33
+    OBJS+= avs.o
34
+endif
32 35
 ifeq ($(CONFIG_CINEPAK_DECODER),yes)
33 36
     OBJS+= cinepak.o
34 37
 endif
... ...
@@ -511,6 +511,9 @@ void avcodec_register_all(void)
511 511
 #ifdef CONFIG_TTA_DECODER
512 512
     register_avcodec(&tta_decoder);
513 513
 #endif //CONFIG_TTA_DECODER
514
+#ifdef CONFIG_AVS_DECODER
515
+    register_avcodec(&avs_decoder);
516
+#endif //CONFIG_AVS_DECODER
514 517
 #ifdef CONFIG_RAWVIDEO_DECODER
515 518
     register_avcodec(&rawvideo_decoder);
516 519
 #endif //CONFIG_RAWVIDEO_DECODER
... ...
@@ -21,8 +21,8 @@ extern "C" {
21 21
 #define AV_STRINGIFY(s)         AV_TOSTRING(s)
22 22
 #define AV_TOSTRING(s) #s
23 23
 
24
-#define LIBAVCODEC_VERSION_INT  ((51<<16)+(6<<8)+0)
25
-#define LIBAVCODEC_VERSION      51.6.0
24
+#define LIBAVCODEC_VERSION_INT  ((51<<16)+(7<<8)+0)
25
+#define LIBAVCODEC_VERSION      51.7.0
26 26
 #define LIBAVCODEC_BUILD        LIBAVCODEC_VERSION_INT
27 27
 
28 28
 #define LIBAVCODEC_IDENT        "Lavc" AV_STRINGIFY(LIBAVCODEC_VERSION)
... ...
@@ -117,6 +117,7 @@ enum CodecID {
117 117
     CODEC_ID_CSCD,
118 118
     CODEC_ID_MMVIDEO,
119 119
     CODEC_ID_ZMBV,
120
+    CODEC_ID_AVS,
120 121
 
121 122
     /* various pcm "codecs" */
122 123
     CODEC_ID_PCM_S16LE= 0x10000,
... ...
@@ -2233,6 +2234,7 @@ extern AVCodec libgsm_decoder;
2233 2233
 extern AVCodec bmp_decoder;
2234 2234
 extern AVCodec mmvideo_decoder;
2235 2235
 extern AVCodec zmbv_decoder;
2236
+extern AVCodec avs_decoder;
2236 2237
 
2237 2238
 /* pcm codecs */
2238 2239
 #define PCM_CODEC(id, name) \
2239 2240
new file mode 100644
... ...
@@ -0,0 +1,158 @@
0
+/*
1
+ * AVS video decoder.
2
+ * Copyright (c) 2006  Aurelien Jacobs <aurel@gnuage.org>
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2 of the License, or (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
+ */
18
+
19
+#include "avcodec.h"
20
+#include "bitstream.h"
21
+
22
+
23
+typedef struct {
24
+    AVFrame picture;
25
+} avs_context_t;
26
+
27
+typedef enum {
28
+    AVS_VIDEO     = 0x01,
29
+    AVS_AUDIO     = 0x02,
30
+    AVS_PALETTE   = 0x03,
31
+    AVS_GAME_DATA = 0x04,
32
+} avs_block_type_t;
33
+
34
+typedef enum {
35
+    AVS_I_FRAME     = 0x00,
36
+    AVS_P_FRAME_3X3 = 0x01,
37
+    AVS_P_FRAME_2X2 = 0x02,
38
+    AVS_P_FRAME_2X3 = 0x03,
39
+} avs_video_sub_type_t;
40
+
41
+
42
+static int
43
+avs_decode_frame(AVCodecContext * avctx,
44
+                 void *data, int *data_size, uint8_t * buf, int buf_size)
45
+{
46
+    avs_context_t *const avs = avctx->priv_data;
47
+    AVFrame *picture = data;
48
+    AVFrame *const p = (AVFrame *) & avs->picture;
49
+    uint8_t *table, *vect, *out;
50
+    int i, j, x, y, stride, vect_w = 3, vect_h = 3;
51
+    int sub_type;
52
+    avs_block_type_t type;
53
+    GetBitContext change_map;
54
+
55
+    if (avctx->reget_buffer(avctx, p)) {
56
+        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
57
+        return -1;
58
+    }
59
+    p->reference = 1;
60
+    p->pict_type = FF_P_TYPE;
61
+    p->key_frame = 0;
62
+
63
+    out = avs->picture.data[0];
64
+    stride = avs->picture.linesize[0];
65
+
66
+    sub_type = buf[0];
67
+    type = buf[1];
68
+    buf += 4;
69
+
70
+    if (type == AVS_PALETTE) {
71
+        int first, last;
72
+        uint32_t *pal = (uint32_t *) avs->picture.data[1];
73
+
74
+        first = LE_16(buf);
75
+        last = first + LE_16(buf + 2);
76
+        buf += 4;
77
+        for (i=first; i<last; i++, buf+=3)
78
+            pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
79
+
80
+        sub_type = buf[0];
81
+        type = buf[1];
82
+        buf += 4;
83
+    }
84
+
85
+    if (type != AVS_VIDEO)
86
+        return -1;
87
+
88
+    switch (sub_type) {
89
+    case AVS_I_FRAME:
90
+        p->pict_type = FF_I_TYPE;
91
+        p->key_frame = 1;
92
+    case AVS_P_FRAME_3X3:
93
+        vect_w = 3;
94
+        vect_h = 3;
95
+        break;
96
+
97
+    case AVS_P_FRAME_2X2:
98
+        vect_w = 2;
99
+        vect_h = 2;
100
+        break;
101
+
102
+    case AVS_P_FRAME_2X3:
103
+        vect_w = 2;
104
+        vect_h = 3;
105
+        break;
106
+
107
+    default:
108
+      return -1;
109
+    }
110
+
111
+    table = buf + (256 * vect_w * vect_h);
112
+    if (sub_type != AVS_I_FRAME) {
113
+        int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
114
+        init_get_bits(&change_map, table, map_size);
115
+        table += map_size;
116
+    }
117
+
118
+    for (y=0; y<198; y+=vect_h) {
119
+        for (x=0; x<318; x+=vect_w) {
120
+            if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
121
+                vect = &buf[*table++ * (vect_w * vect_h)];
122
+                for (j=0; j<vect_w; j++) {
123
+                    out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
124
+                    out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
125
+                    if (vect_h == 3)
126
+                        out[(y + 2) * stride + x + j] =
127
+                            vect[(2 * vect_w) + j];
128
+                }
129
+            }
130
+        }
131
+        if (sub_type != AVS_I_FRAME)
132
+            align_get_bits(&change_map);
133
+    }
134
+
135
+    *picture = *(AVFrame *) & avs->picture;
136
+    *data_size = sizeof(AVPicture);
137
+
138
+    return buf_size;
139
+}
140
+
141
+static int avs_decode_init(AVCodecContext * avctx)
142
+{
143
+    avctx->pix_fmt = PIX_FMT_PAL8;
144
+    return 0;
145
+}
146
+
147
+AVCodec avs_decoder = {
148
+    "avs",
149
+    CODEC_TYPE_VIDEO,
150
+    CODEC_ID_AVS,
151
+    sizeof(avs_context_t),
152
+    avs_decode_init,
153
+    NULL,
154
+    NULL,
155
+    avs_decode_frame,
156
+    CODEC_CAP_DR1,
157
+};
... ...
@@ -18,7 +18,7 @@ OBJS+=mpeg.o mpegts.o mpegtsenc.o ffm.o crc.o img.o img2.o raw.o rm.o \
18 18
       nut.o wc3movie.o mp3.o westwood.o segafilm.o idcin.o flic.o \
19 19
       sierravmd.o matroska.o sol.o electronicarts.o nsvdec.o asf.o \
20 20
       ogg2.o oggparsevorbis.o oggparsetheora.o oggparseflac.o daud.o aiff.o \
21
-      voc.o tta.o mm.o
21
+      voc.o tta.o mm.o avs.o
22 22
 
23 23
 # muxers
24 24
 ifeq ($(CONFIG_MUXERS),yes)
... ...
@@ -118,6 +118,7 @@ void av_register_all(void)
118 118
     daud_init();
119 119
     voc_init();
120 120
     tta_init();
121
+    avs_init();
121 122
 
122 123
 #ifdef CONFIG_MUXERS
123 124
     /* image formats */
... ...
@@ -5,8 +5,8 @@
5 5
 extern "C" {
6 6
 #endif
7 7
 
8
-#define LIBAVFORMAT_VERSION_INT ((50<<16)+(2<<8)+1)
9
-#define LIBAVFORMAT_VERSION     50.2.1
8
+#define LIBAVFORMAT_VERSION_INT ((50<<16)+(3<<8)+0)
9
+#define LIBAVFORMAT_VERSION     50.3.0
10 10
 #define LIBAVFORMAT_BUILD       LIBAVFORMAT_VERSION_INT
11 11
 
12 12
 #define LIBAVFORMAT_IDENT       "Lavf" AV_STRINGIFY(LIBAVFORMAT_VERSION)
... ...
@@ -564,6 +564,9 @@ int ff_adts_init(void);
564 564
 /* mm.c */
565 565
 int mm_init(void);
566 566
 
567
+/* avs.c */
568
+int avs_init(void);
569
+
567 570
 #include "rtp.h"
568 571
 
569 572
 #include "rtsp.h"
570 573
new file mode 100644
... ...
@@ -0,0 +1,238 @@
0
+/*
1
+ * AVS demuxer.
2
+ * Copyright (c) 2006  Aurelien Jacobs <aurel@gnuage.org>
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2 of the License, or (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
+ */
18
+
19
+#include "avformat.h"
20
+#include "voc.h"
21
+
22
+
23
+typedef struct avs_format {
24
+    voc_dec_context_t voc;
25
+    AVStream *st_video;
26
+    AVStream *st_audio;
27
+    int width;
28
+    int height;
29
+    int bits_per_sample;
30
+    int fps;
31
+    int nb_frames;
32
+    int remaining_frame_size;
33
+    int remaining_audio_size;
34
+} avs_format_t;
35
+
36
+typedef enum avs_block_type {
37
+    AVS_VIDEO     = 0x01,
38
+    AVS_AUDIO     = 0x02,
39
+    AVS_PALETTE   = 0x03,
40
+    AVS_GAME_DATA = 0x04,
41
+} avs_block_type_t;
42
+
43
+
44
+#ifdef CONFIG_DEMUXERS
45
+
46
+static int avs_probe(AVProbeData * p)
47
+{
48
+    const uint8_t *d;
49
+
50
+    if (p->buf_size < 2)
51
+        return 0;
52
+    d = p->buf;
53
+    if (d[0] == 'w' && d[1] == 'W' && d[2] == 0x10 && d[3] == 0)
54
+        return 50;
55
+
56
+    return 0;
57
+}
58
+
59
+static int avs_read_header(AVFormatContext * s, AVFormatParameters * ap)
60
+{
61
+    avs_format_t *avs = s->priv_data;
62
+
63
+    s->ctx_flags |= AVFMTCTX_NOHEADER;
64
+
65
+    url_fskip(&s->pb, 4);
66
+    avs->width = get_le16(&s->pb);
67
+    avs->height = get_le16(&s->pb);
68
+    avs->bits_per_sample = get_le16(&s->pb);
69
+    avs->fps = get_le16(&s->pb);
70
+    avs->nb_frames = get_le32(&s->pb);
71
+    avs->remaining_frame_size = 0;
72
+    avs->remaining_audio_size = 0;
73
+
74
+    avs->st_video = avs->st_audio = NULL;
75
+
76
+    if (avs->width != 318 || avs->height != 198)
77
+        av_log(s, AV_LOG_ERROR, "This avs pretend to be %dx%d "
78
+               "when the avs format is supposed to be 318x198 only.\n",
79
+               avs->width, avs->height);
80
+
81
+    return 0;
82
+}
83
+
84
+static int
85
+avs_read_video_packet(AVFormatContext * s, AVPacket * pkt,
86
+                      avs_block_type_t type, int sub_type, int size,
87
+                      uint8_t * palette, int palette_size)
88
+{
89
+    avs_format_t *avs = s->priv_data;
90
+    int ret;
91
+
92
+    ret = av_new_packet(pkt, size + palette_size);
93
+    if (ret < 0)
94
+        return ret;
95
+
96
+    if (palette_size) {
97
+        pkt->data[0] = 0x00;
98
+        pkt->data[1] = 0x03;
99
+        pkt->data[2] = palette_size & 0xFF;
100
+        pkt->data[3] = (palette_size >> 8) & 0xFF;
101
+        memcpy(pkt->data + 4, palette, palette_size - 4);
102
+    }
103
+
104
+    pkt->data[palette_size + 0] = sub_type;
105
+    pkt->data[palette_size + 1] = type;
106
+    pkt->data[palette_size + 2] = size & 0xFF;
107
+    pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
108
+    ret = get_buffer(&s->pb, pkt->data + palette_size + 4, size - 4) + 4;
109
+    if (ret < size) {
110
+        av_free_packet(pkt);
111
+        return AVERROR_IO;
112
+    }
113
+
114
+    pkt->size = ret + palette_size;
115
+    pkt->stream_index = avs->st_video->index;
116
+    if (sub_type == 0)
117
+        pkt->flags |= PKT_FLAG_KEY;
118
+
119
+    return 0;
120
+}
121
+
122
+static int avs_read_audio_packet(AVFormatContext * s, AVPacket * pkt)
123
+{
124
+    avs_format_t *avs = s->priv_data;
125
+    int ret, size;
126
+
127
+    size = url_ftell(&s->pb);
128
+    ret = voc_get_packet(s, pkt, avs->st_audio, avs->remaining_audio_size);
129
+    size = url_ftell(&s->pb) - size;
130
+    avs->remaining_audio_size -= size;
131
+
132
+    if (ret == AVERROR_IO)
133
+        return 0;    /* this indicate EOS */
134
+    if (ret < 0)
135
+        return ret;
136
+
137
+    pkt->stream_index = avs->st_audio->index;
138
+    pkt->flags |= PKT_FLAG_KEY;
139
+
140
+    return size;
141
+}
142
+
143
+static int avs_read_packet(AVFormatContext * s, AVPacket * pkt)
144
+{
145
+    avs_format_t *avs = s->priv_data;
146
+    int sub_type = 0, size = 0;
147
+    avs_block_type_t type = 0;
148
+    int palette_size = 0;
149
+    uint8_t palette[4 + 3 * 256];
150
+    int ret;
151
+
152
+    if (avs->remaining_audio_size > 0)
153
+        if (avs_read_audio_packet(s, pkt) > 0)
154
+            return 0;
155
+
156
+    while (1) {
157
+        if (avs->remaining_frame_size <= 0) {
158
+            if (!get_le16(&s->pb))    /* found EOF */
159
+                return AVERROR_IO;
160
+            avs->remaining_frame_size = get_le16(&s->pb) - 4;
161
+        }
162
+
163
+        while (avs->remaining_frame_size > 0) {
164
+            sub_type = get_byte(&s->pb);
165
+            type = get_byte(&s->pb);
166
+            size = get_le16(&s->pb);
167
+            avs->remaining_frame_size -= size;
168
+
169
+            switch (type) {
170
+            case AVS_PALETTE:
171
+                ret = get_buffer(&s->pb, palette, size - 4);
172
+                if (ret < size - 4)
173
+                    return AVERROR_IO;
174
+                palette_size = size;
175
+                break;
176
+
177
+            case AVS_VIDEO:
178
+                if (!avs->st_video) {
179
+                    avs->st_video = av_new_stream(s, AVS_VIDEO);
180
+                    if (avs->st_video == NULL)
181
+                        return AVERROR_NOMEM;
182
+                    avs->st_video->codec->codec_type = CODEC_TYPE_VIDEO;
183
+                    avs->st_video->codec->codec_id = CODEC_ID_AVS;
184
+                    avs->st_video->codec->width = avs->width;
185
+                    avs->st_video->codec->height = avs->height;
186
+                    avs->st_video->codec->bits_per_sample=avs->bits_per_sample;
187
+                    avs->st_video->nb_frames = avs->nb_frames;
188
+                    avs->st_video->codec->time_base = (AVRational) {
189
+                    1, avs->fps};
190
+                }
191
+                return avs_read_video_packet(s, pkt, type, sub_type, size,
192
+                                             palette, palette_size);
193
+
194
+            case AVS_AUDIO:
195
+                if (!avs->st_audio) {
196
+                    avs->st_audio = av_new_stream(s, AVS_AUDIO);
197
+                    if (avs->st_audio == NULL)
198
+                        return AVERROR_NOMEM;
199
+                    avs->st_audio->codec->codec_type = CODEC_TYPE_AUDIO;
200
+                }
201
+                avs->remaining_audio_size = size - 4;
202
+                size = avs_read_audio_packet(s, pkt);
203
+                if (size != 0)
204
+                    return size;
205
+                break;
206
+
207
+            default:
208
+                url_fskip(&s->pb, size - 4);
209
+            }
210
+        }
211
+    }
212
+}
213
+
214
+static int avs_read_close(AVFormatContext * s)
215
+{
216
+    return 0;
217
+}
218
+
219
+static AVInputFormat avs_iformat = {
220
+    "avs",
221
+    "avs format",
222
+    sizeof(avs_format_t),
223
+    avs_probe,
224
+    avs_read_header,
225
+    avs_read_packet,
226
+    avs_read_close,
227
+};
228
+
229
+#endif /* CONFIG_DEMUXERS */
230
+
231
+int avs_init(void)
232
+{
233
+#ifdef CONFIG_DEMUXERS
234
+    av_register_input_format(&avs_iformat);
235
+#endif /* CONFIG_DEMUXERS */
236
+    return 0;
237
+}