Browse code

support for Tiertex .seq files demuxing/video decoding, by Gregory Montoir %cyx A users P sourceforge P net%

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

Baptiste Coudurier authored on 2006/10/12 21:02:58
Showing 11 changed files
... ...
@@ -61,6 +61,7 @@ version <next>
61 61
 - WavPack lossless audio decoder
62 62
 - Targa (.TGA) picture decoder
63 63
 - Delphine Software .cin demuxer/audio and video decoder
64
+- Tiertex .seq demuxer/video decoder
64 65
 
65 66
 version 0.4.9-pre1:
66 67
 
... ...
@@ -740,6 +740,8 @@ library:
740 740
 @tab Multimedia format used by Delphine Software games.
741 741
 @item MXF @tab    @tab X
742 742
 @tab Material eXchange Format SMPTE 377M, used by D-Cinema, broadcast industry.
743
+@item SEQ @tab    @tab X
744
+@tab Tiertex .seq files used in the DOS CDROM version of the game Flashback.
743 745
 @end multitable
744 746
 
745 747
 @code{X} means that encoding (resp. decoding) is supported.
... ...
@@ -843,6 +845,7 @@ following image formats are supported:
843 843
 @item KMVC                   @tab     @tab  X @tab Codec used in Worms games.
844 844
 @item VMware Video           @tab     @tab  X @tab Codec used in videos captured by VMware.
845 845
 @item Cin Video              @tab     @tab  X @tab Codec used in Delphine Software games.
846
+@item Tiertex Seq Video      @tab     @tab  X @tab Codec used in DOS CDROM FlashBack game.
846 847
 @end multitable
847 848
 
848 849
 @code{X} means that encoding (resp. decoding) is supported.
... ...
@@ -146,6 +146,7 @@ OBJS-$(CONFIG_VCR1_ENCODER)            += vcr1.o
146 146
 OBJS-$(CONFIG_VMDAUDIO_DECODER)        += vmdav.o
147 147
 OBJS-$(CONFIG_VMDVIDEO_DECODER)        += vmdav.o
148 148
 OBJS-$(CONFIG_VMNC_DECODER)            += vmnc.o
149
+OBJS-$(CONFIG_TIERTEXSEQVIDEO_DECODER) += tiertexseqv.o
149 150
 OBJS-$(CONFIG_VORBIS_DECODER)          += vorbis.o vorbis_data.o
150 151
 OBJS-$(CONFIG_VORBIS_ENCODER)          += vorbis_enc.o vorbis_data.o
151 152
 OBJS-$(CONFIG_VP3_DECODER)             += vp3.o
... ...
@@ -554,6 +554,9 @@ void avcodec_register_all(void)
554 554
 #ifdef CONFIG_DSICINAUDIO_DECODER
555 555
     register_avcodec(&dsicinaudio_decoder);
556 556
 #endif //CONFIG_DSICINAUDIO_DECODER
557
+#ifdef CONFIG_TIERTEXSEQVIDEO_DECODER
558
+    register_avcodec(&tiertexseqvideo_decoder);
559
+#endif //CONFIG_TIERTEXSEQVIDEO_DECODER
557 560
 
558 561
 #if defined(CONFIG_AMR_NB) || defined(CONFIG_AMR_NB_FIXED)
559 562
 #ifdef CONFIG_AMR_NB_DECODER
... ...
@@ -37,8 +37,8 @@ extern "C" {
37 37
 #define AV_STRINGIFY(s)         AV_TOSTRING(s)
38 38
 #define AV_TOSTRING(s) #s
39 39
 
40
-#define LIBAVCODEC_VERSION_INT  ((51<<16)+(18<<8)+0)
41
-#define LIBAVCODEC_VERSION      51.18.0
40
+#define LIBAVCODEC_VERSION_INT  ((51<<16)+(19<<8)+0)
41
+#define LIBAVCODEC_VERSION      51.19.0
42 42
 #define LIBAVCODEC_BUILD        LIBAVCODEC_VERSION_INT
43 43
 
44 44
 #define LIBAVCODEC_IDENT        "Lavc" AV_STRINGIFY(LIBAVCODEC_VERSION)
... ...
@@ -146,6 +146,7 @@ enum CodecID {
146 146
     CODEC_ID_VP6F,
147 147
     CODEC_ID_TARGA,
148 148
     CODEC_ID_DSICINVIDEO,
149
+    CODEC_ID_TIERTEXSEQVIDEO,
149 150
 
150 151
     /* various pcm "codecs" */
151 152
     CODEC_ID_PCM_S16LE= 0x10000,
... ...
@@ -2298,6 +2299,7 @@ extern AVCodec wavpack_decoder;
2298 2298
 extern AVCodec targa_decoder;
2299 2299
 extern AVCodec dsicinvideo_decoder;
2300 2300
 extern AVCodec dsicinaudio_decoder;
2301
+extern AVCodec tiertexseqvideo_decoder;
2301 2302
 
2302 2303
 /* pcm codecs */
2303 2304
 #define PCM_CODEC(id, name) \
2304 2305
new file mode 100644
... ...
@@ -0,0 +1,232 @@
0
+/*
1
+ * Tiertex Limited SEQ Video Decoder
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 tiertexseqv.c
23
+ * Tiertex Limited SEQ video decoder
24
+ */
25
+
26
+#include "avcodec.h"
27
+#include "common.h"
28
+#define ALT_BITSTREAM_READER_LE
29
+#include "bitstream.h"
30
+
31
+
32
+typedef struct SeqVideoContext {
33
+    AVCodecContext *avctx;
34
+    AVFrame frame;
35
+    unsigned int palette[256];
36
+    unsigned char block[8 * 8];
37
+} SeqVideoContext;
38
+
39
+
40
+static unsigned char *seq_unpack_rle_block(unsigned char *src, unsigned char *dst, int dst_size)
41
+{
42
+    int i, len, sz;
43
+    GetBitContext gb;
44
+    int code_table[64];
45
+
46
+    /* get the rle codes (at most 64 bytes) */
47
+    init_get_bits(&gb, src, 64 * 8);
48
+    for (i = 0, sz = 0; i < 64 && sz < dst_size; i++) {
49
+        code_table[i] = get_sbits(&gb, 4);
50
+        sz += FFABS(code_table[i]);
51
+    }
52
+    src += (get_bits_count(&gb) + 7) / 8;
53
+
54
+    /* do the rle unpacking */
55
+    for (i = 0; i < 64 && dst_size > 0; i++) {
56
+        len = code_table[i];
57
+        if (len < 0) {
58
+            len = -len;
59
+            memset(dst, *src++, FFMIN(len, dst_size));
60
+        } else {
61
+            memcpy(dst, src, FFMIN(len, dst_size));
62
+            src += len;
63
+        }
64
+        dst += len;
65
+        dst_size -= len;
66
+    }
67
+    return src;
68
+}
69
+
70
+static unsigned char *seq_decode_op1(SeqVideoContext *seq, unsigned char *src, unsigned char *dst)
71
+{
72
+    unsigned char *color_table;
73
+    int b, i, len, bits;
74
+    GetBitContext gb;
75
+
76
+    len = *src++;
77
+    if (len & 0x80) {
78
+        switch (len & 3) {
79
+        case 1:
80
+            src = seq_unpack_rle_block(src, seq->block, sizeof(seq->block));
81
+            for (b = 0; b < 8; b++) {
82
+                memcpy(dst, &seq->block[b * 8], 8);
83
+                dst += seq->frame.linesize[0];
84
+            }
85
+            break;
86
+        case 2:
87
+            src = seq_unpack_rle_block(src, seq->block, sizeof(seq->block));
88
+            for (i = 0; i < 8; i++) {
89
+                for (b = 0; b < 8; b++)
90
+                    dst[b * seq->frame.linesize[0]] = seq->block[i * 8 + b];
91
+                ++dst;
92
+            }
93
+            break;
94
+        }
95
+    } else {
96
+        color_table = src;
97
+        src += len;
98
+        bits = ff_log2_tab[len - 1] + 1;
99
+        init_get_bits(&gb, src, bits * 8 * 8); src += bits * 8;
100
+        for (b = 0; b < 8; b++) {
101
+            for (i = 0; i < 8; i++)
102
+                dst[i] = color_table[get_bits(&gb, bits)];
103
+            dst += seq->frame.linesize[0];
104
+        }
105
+    }
106
+
107
+    return src;
108
+}
109
+
110
+static unsigned char *seq_decode_op2(SeqVideoContext *seq, unsigned char *src, unsigned char *dst)
111
+{
112
+    int i;
113
+
114
+    for (i = 0; i < 8; i++) {
115
+        memcpy(dst, src, 8);
116
+        src += 8;
117
+        dst += seq->frame.linesize[0];
118
+    }
119
+
120
+    return src;
121
+}
122
+
123
+static unsigned char *seq_decode_op3(SeqVideoContext *seq, unsigned char *src, unsigned char *dst)
124
+{
125
+    int pos, offset;
126
+
127
+    do {
128
+        pos = *src++;
129
+        offset = ((pos >> 3) & 7) * seq->frame.linesize[0] + (pos & 7);
130
+        dst[offset] = *src++;
131
+    } while (!(pos & 0x80));
132
+
133
+    return src;
134
+}
135
+
136
+static void seqvideo_decode(SeqVideoContext *seq, unsigned char *data, int data_size)
137
+{
138
+    GetBitContext gb;
139
+    int flags, i, j, x, y, op;
140
+    unsigned char c[3];
141
+    unsigned char *dst;
142
+
143
+    flags = *data++;
144
+
145
+    if (flags & 1) {
146
+        for (i = 0; i < 256; i++) {
147
+            for (j = 0; j < 3; j++, data++)
148
+                c[j] = (*data << 2) | (*data >> 4);
149
+            seq->palette[i] = (c[0] << 16) | (c[1] << 8) | c[2];
150
+        }
151
+        memcpy(seq->frame.data[1], seq->palette, sizeof(seq->palette));
152
+        seq->frame.palette_has_changed = 1;
153
+    }
154
+
155
+    if (flags & 2) {
156
+        init_get_bits(&gb, data, 128 * 8); data += 128;
157
+        for (y = 0; y < 128; y += 8)
158
+            for (x = 0; x < 256; x += 8) {
159
+                dst = &seq->frame.data[0][y * seq->frame.linesize[0] + x];
160
+                op = get_bits(&gb, 2);
161
+                switch (op) {
162
+                case 1:
163
+                    data = seq_decode_op1(seq, data, dst);
164
+                    break;
165
+                case 2:
166
+                    data = seq_decode_op2(seq, data, dst);
167
+                    break;
168
+                case 3:
169
+                    data = seq_decode_op3(seq, data, dst);
170
+                    break;
171
+                }
172
+            }
173
+    }
174
+}
175
+
176
+static int seqvideo_decode_init(AVCodecContext *avctx)
177
+{
178
+    SeqVideoContext *seq = (SeqVideoContext *)avctx->priv_data;
179
+
180
+    seq->avctx = avctx;
181
+    avctx->pix_fmt = PIX_FMT_PAL8;
182
+    avctx->has_b_frames = 0;
183
+
184
+    seq->frame.data[0] = NULL;
185
+
186
+    return 0;
187
+}
188
+
189
+static int seqvideo_decode_frame(AVCodecContext *avctx,
190
+                                 void *data, int *data_size,
191
+                                 uint8_t *buf, int buf_size)
192
+{
193
+
194
+    SeqVideoContext *seq = (SeqVideoContext *)avctx->priv_data;
195
+
196
+    seq->frame.reference = 1;
197
+    seq->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
198
+    if (avctx->reget_buffer(avctx, &seq->frame)) {
199
+        av_log(seq->avctx, AV_LOG_ERROR, "tiertexseqvideo: reget_buffer() failed\n");
200
+        return -1;
201
+    }
202
+
203
+    seqvideo_decode(seq, buf, buf_size);
204
+
205
+    *data_size = sizeof(AVFrame);
206
+    *(AVFrame *)data = seq->frame;
207
+
208
+    return buf_size;
209
+}
210
+
211
+static int seqvideo_decode_end(AVCodecContext *avctx)
212
+{
213
+    SeqVideoContext *seq = (SeqVideoContext *)avctx->priv_data;
214
+
215
+    if (seq->frame.data[0])
216
+        avctx->release_buffer(avctx, &seq->frame);
217
+
218
+    return 0;
219
+}
220
+
221
+AVCodec tiertexseqvideo_decoder = {
222
+    "tiertexseqvideo",
223
+    CODEC_TYPE_VIDEO,
224
+    CODEC_ID_TIERTEXSEQVIDEO,
225
+    sizeof(SeqVideoContext),
226
+    seqvideo_decode_init,
227
+    NULL,
228
+    seqvideo_decode_end,
229
+    seqvideo_decode_frame,
230
+    CODEC_CAP_DR1,
231
+};
... ...
@@ -130,6 +130,7 @@ OBJS-$(CONFIG_WSVQA_DEMUXER)             += westwood.o
130 130
 OBJS-$(CONFIG_WV_DEMUXER)                += wv.o
131 131
 OBJS-$(CONFIG_YUV4MPEGPIPE_MUXER)        += yuv4mpeg.o
132 132
 OBJS-$(CONFIG_YUV4MPEGPIPE_DEMUXER)      += yuv4mpeg.o
133
+OBJS-$(CONFIG_TIERTEXSEQ_DEMUXER)        += tiertexseq.o
133 134
 
134 135
 # image formats
135 136
 OBJS+= pnm.o yuv.o png.o jpeg.o gifdec.o sgi.o
... ...
@@ -478,6 +478,9 @@ void av_register_all(void)
478 478
 #ifdef CONFIG_YUV4MPEGPIPE_DEMUXER
479 479
     av_register_input_format(&yuv4mpegpipe_demuxer);
480 480
 #endif
481
+#ifdef CONFIG_TIERTEXSEQ_DEMUXER
482
+    av_register_input_format(&tiertexseq_demuxer);
483
+#endif
481 484
 
482 485
     /* image formats */
483 486
 #if 0
... ...
@@ -162,6 +162,7 @@ extern AVInputFormat wsvqa_demuxer;
162 162
 extern AVInputFormat wv_demuxer;
163 163
 extern AVOutputFormat yuv4mpegpipe_muxer;
164 164
 extern AVInputFormat yuv4mpegpipe_demuxer;
165
+extern AVInputFormat tiertexseq_demuxer;
165 166
 
166 167
 /* raw.c */
167 168
 int pcm_read_seek(AVFormatContext *s,
... ...
@@ -25,8 +25,8 @@
25 25
 extern "C" {
26 26
 #endif
27 27
 
28
-#define LIBAVFORMAT_VERSION_INT ((50<<16)+(5<<8)+0)
29
-#define LIBAVFORMAT_VERSION     50.5.0
28
+#define LIBAVFORMAT_VERSION_INT ((50<<16)+(6<<8)+0)
29
+#define LIBAVFORMAT_VERSION     50.6.0
30 30
 #define LIBAVFORMAT_BUILD       LIBAVFORMAT_VERSION_INT
31 31
 
32 32
 #define LIBAVFORMAT_IDENT       "Lavf" AV_STRINGIFY(LIBAVFORMAT_VERSION)
33 33
new file mode 100644
... ...
@@ -0,0 +1,305 @@
0
+/*
1
+ * Tiertex Limited SEQ 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 tiertexseq.c
23
+ * Tiertex Limited SEQ file demuxer
24
+ */
25
+
26
+#include "avformat.h"
27
+
28
+#define SEQ_FRAME_SIZE         6144
29
+#define SEQ_FRAME_W            256
30
+#define SEQ_FRAME_H            128
31
+#define SEQ_NUM_FRAME_BUFFERS  30
32
+#define SEQ_AUDIO_BUFFER_SIZE  882
33
+#define SEQ_SAMPLE_RATE        22050
34
+#define SEQ_FRAME_RATE         25
35
+
36
+
37
+typedef struct TiertexSeqFrameBuffer {
38
+    int fill_size;
39
+    int data_size;
40
+    unsigned char *data;
41
+} TiertexSeqFrameBuffer;
42
+
43
+typedef struct SeqDemuxContext {
44
+    int audio_stream_index;
45
+    int video_stream_index;
46
+    int current_frame_pts;
47
+    int current_frame_offs;
48
+    TiertexSeqFrameBuffer frame_buffers[SEQ_NUM_FRAME_BUFFERS];
49
+    int frame_buffers_count;
50
+    unsigned int current_audio_data_size;
51
+    unsigned int current_audio_data_offs;
52
+    unsigned int current_pal_data_size;
53
+    unsigned int current_pal_data_offs;
54
+    unsigned int current_video_data_size;
55
+    unsigned char *current_video_data_ptr;
56
+    int audio_buffer_full;
57
+} SeqDemuxContext;
58
+
59
+
60
+static int seq_probe(AVProbeData *p)
61
+{
62
+    int i;
63
+
64
+    if (p->buf_size < 256)
65
+        return 0;
66
+
67
+    /* there's no real header in a .seq file, the only thing they have in common */
68
+    /* is the first 256 bytes of the file which are always filled with 0 */
69
+    for (i = 0; i < 256; i++)
70
+        if (p->buf[0] != 0)
71
+            return 0;
72
+
73
+    /* only one fourth of the score since the previous check is too naive */
74
+    return AVPROBE_SCORE_MAX / 4;
75
+}
76
+
77
+static int seq_init_frame_buffers(SeqDemuxContext *seq, ByteIOContext *pb)
78
+{
79
+    int i, sz;
80
+    TiertexSeqFrameBuffer *seq_buffer;
81
+
82
+    url_fseek(pb, 256, SEEK_SET);
83
+
84
+    for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++) {
85
+        sz = get_le16(pb);
86
+        if (sz == 0)
87
+            break;
88
+        else {
89
+            seq_buffer = &seq->frame_buffers[i];
90
+            seq_buffer->fill_size = 0;
91
+            seq_buffer->data_size = sz;
92
+            seq_buffer->data = av_malloc(sz);
93
+            if (!seq_buffer->data)
94
+                return AVERROR_NOMEM;
95
+        }
96
+    }
97
+    seq->frame_buffers_count = i;
98
+    return 0;
99
+}
100
+
101
+static int seq_fill_buffer(SeqDemuxContext *seq, ByteIOContext *pb, int buffer_num, unsigned int data_offs, int data_size)
102
+{
103
+    TiertexSeqFrameBuffer *seq_buffer;
104
+
105
+    if (buffer_num >= SEQ_NUM_FRAME_BUFFERS)
106
+        return AVERROR_INVALIDDATA;
107
+
108
+    seq_buffer = &seq->frame_buffers[buffer_num];
109
+    if (seq_buffer->fill_size + data_size > seq_buffer->data_size)
110
+        return AVERROR_INVALIDDATA;
111
+
112
+    url_fseek(pb, seq->current_frame_offs + data_offs, SEEK_SET);
113
+    if (get_buffer(pb, seq_buffer->data + seq_buffer->fill_size, data_size) != data_size)
114
+        return AVERROR_IO;
115
+
116
+    seq_buffer->fill_size += data_size;
117
+    return 0;
118
+}
119
+
120
+static int seq_parse_frame_data(SeqDemuxContext *seq, ByteIOContext *pb)
121
+{
122
+    unsigned int offset_table[4], buffer_num[4];
123
+    TiertexSeqFrameBuffer *seq_buffer;
124
+    int i, e;
125
+
126
+    seq->current_frame_offs += SEQ_FRAME_SIZE;
127
+    url_fseek(pb, seq->current_frame_offs, SEEK_SET);
128
+
129
+    /* sound data */
130
+    seq->current_audio_data_offs = get_le16(pb);
131
+    if (seq->current_audio_data_offs != 0) {
132
+        seq->current_audio_data_size = SEQ_AUDIO_BUFFER_SIZE * 2;
133
+    } else {
134
+        seq->current_audio_data_size = 0;
135
+    }
136
+
137
+    /* palette data */
138
+    seq->current_pal_data_offs = get_le16(pb);
139
+    if (seq->current_pal_data_offs != 0) {
140
+        seq->current_pal_data_size = 768;
141
+    } else {
142
+        seq->current_pal_data_size = 0;
143
+    }
144
+
145
+    /* video data */
146
+    for (i = 0; i < 4; i++)
147
+        buffer_num[i] = get_byte(pb);
148
+
149
+    for (i = 0; i < 4; i++)
150
+        offset_table[i] = get_le16(pb);
151
+
152
+    for (i = 0; i < 3; i++) {
153
+        if (offset_table[i] != 0) {
154
+            for (e = i + 1; e < 4 && offset_table[e] == 0; e++);
155
+            seq_fill_buffer(seq, pb, buffer_num[1 + i],
156
+              offset_table[i],
157
+              offset_table[e] - offset_table[i]);
158
+        }
159
+    }
160
+
161
+    if (buffer_num[0] != 255) {
162
+        seq_buffer = &seq->frame_buffers[buffer_num[0]];
163
+        seq->current_video_data_size = seq_buffer->fill_size;
164
+        seq->current_video_data_ptr  = seq_buffer->data;
165
+        seq_buffer->fill_size = 0;
166
+    } else {
167
+        seq->current_video_data_size = 0;
168
+        seq->current_video_data_ptr  = 0;
169
+    }
170
+
171
+    return 0;
172
+}
173
+
174
+static int seq_read_header(AVFormatContext *s, AVFormatParameters *ap)
175
+{
176
+    int i, rc;
177
+    SeqDemuxContext *seq = (SeqDemuxContext *)s->priv_data;
178
+    ByteIOContext *pb = &s->pb;
179
+    AVStream *st;
180
+
181
+    /* init internal buffers */
182
+    rc = seq_init_frame_buffers(seq, pb);
183
+    if (rc)
184
+        return rc;
185
+
186
+    seq->current_frame_offs = 0;
187
+
188
+    /* preload (no audio data, just buffer operations related data) */
189
+    for (i = 1; i <= 100; i++) {
190
+        rc = seq_parse_frame_data(seq, pb);
191
+        if (rc)
192
+            return rc;
193
+    }
194
+
195
+    seq->current_frame_pts = 0;
196
+
197
+    seq->audio_buffer_full = 0;
198
+
199
+    /* initialize the video decoder stream */
200
+    st = av_new_stream(s, 0);
201
+    if (!st)
202
+        return AVERROR_NOMEM;
203
+
204
+    av_set_pts_info(st, 32, 1, SEQ_FRAME_RATE);
205
+    seq->video_stream_index = st->index;
206
+    st->codec->codec_type = CODEC_TYPE_VIDEO;
207
+    st->codec->codec_id = CODEC_ID_TIERTEXSEQVIDEO;
208
+    st->codec->codec_tag = 0;  /* no fourcc */
209
+    st->codec->width = SEQ_FRAME_W;
210
+    st->codec->height = SEQ_FRAME_H;
211
+
212
+    /* initialize the audio decoder stream */
213
+    st = av_new_stream(s, 0);
214
+    if (!st)
215
+        return AVERROR_NOMEM;
216
+
217
+    av_set_pts_info(st, 32, 1, SEQ_SAMPLE_RATE);
218
+    seq->audio_stream_index = st->index;
219
+    st->codec->codec_type = CODEC_TYPE_AUDIO;
220
+    st->codec->codec_id = CODEC_ID_PCM_S16BE;
221
+    st->codec->codec_tag = 0;  /* no tag */
222
+    st->codec->channels = 1;
223
+    st->codec->sample_rate = SEQ_SAMPLE_RATE;
224
+    st->codec->bits_per_sample = 16;
225
+    st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_sample * st->codec->channels;
226
+    st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
227
+
228
+    return 0;
229
+}
230
+
231
+static int seq_read_packet(AVFormatContext *s, AVPacket *pkt)
232
+{
233
+    int rc;
234
+    SeqDemuxContext *seq = (SeqDemuxContext *)s->priv_data;
235
+    ByteIOContext *pb = &s->pb;
236
+
237
+    if (!seq->audio_buffer_full) {
238
+        rc = seq_parse_frame_data(seq, pb);
239
+        if (rc)
240
+            return rc;
241
+
242
+        /* video packet */
243
+        if (seq->current_pal_data_size + seq->current_video_data_size != 0) {
244
+            if (av_new_packet(pkt, 1 + seq->current_pal_data_size + seq->current_video_data_size))
245
+                return AVERROR_NOMEM;
246
+
247
+            pkt->data[0] = 0;
248
+            if (seq->current_pal_data_size != 0) {
249
+                pkt->data[0] |= 1;
250
+                url_fseek(pb, seq->current_frame_offs + seq->current_pal_data_offs, SEEK_SET);
251
+                if (get_buffer(pb, &pkt->data[1], seq->current_pal_data_size) != seq->current_pal_data_size)
252
+                    return AVERROR_IO;
253
+            }
254
+            if (seq->current_video_data_size != 0) {
255
+                pkt->data[0] |= 2;
256
+                memcpy(&pkt->data[1 + seq->current_pal_data_size],
257
+                  seq->current_video_data_ptr,
258
+                  seq->current_video_data_size);
259
+            }
260
+            pkt->stream_index = seq->video_stream_index;
261
+            pkt->pts = seq->current_frame_pts;
262
+
263
+            /* sound buffer will be processed on next read_packet() call */
264
+            seq->audio_buffer_full = 1;
265
+            return 0;
266
+       }
267
+    }
268
+
269
+    /* audio packet */
270
+    if (seq->current_audio_data_offs == 0) /* end of data reached */
271
+        return AVERROR_IO;
272
+
273
+    url_fseek(pb, seq->current_frame_offs + seq->current_audio_data_offs, SEEK_SET);
274
+    rc = av_get_packet(pb, pkt, seq->current_audio_data_size);
275
+    if (rc < 0)
276
+        return rc;
277
+
278
+    pkt->stream_index = seq->audio_stream_index;
279
+    pkt->pts = seq->current_frame_pts++;
280
+
281
+    seq->audio_buffer_full = 0;
282
+    return 0;
283
+}
284
+
285
+static int seq_read_close(AVFormatContext *s)
286
+{
287
+    int i;
288
+    SeqDemuxContext *seq = (SeqDemuxContext *)s->priv_data;
289
+
290
+    for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++)
291
+        av_free(seq->frame_buffers[i].data);
292
+
293
+    return 0;
294
+}
295
+
296
+AVInputFormat tiertexseq_demuxer = {
297
+    "tiertexseq",
298
+    "Tiertex Limited SEQ format",
299
+    sizeof(SeqDemuxContext),
300
+    seq_probe,
301
+    seq_read_header,
302
+    seq_read_packet,
303
+    seq_read_close,
304
+};