Statistics
| Branch: | Revision:

ffmpeg / libavformat / tiertexseq.c @ 4647c0ec

History | View | Annotate | Download (9.04 KB)

1 29f86228 Baptiste Coudurier
/*
2
 * Tiertex Limited SEQ File Demuxer
3
 * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
/**
23
 * @file tiertexseq.c
24
 * Tiertex Limited SEQ file demuxer
25
 */
26
27
#include "avformat.h"
28
29
#define SEQ_FRAME_SIZE         6144
30
#define SEQ_FRAME_W            256
31
#define SEQ_FRAME_H            128
32
#define SEQ_NUM_FRAME_BUFFERS  30
33
#define SEQ_AUDIO_BUFFER_SIZE  882
34
#define SEQ_SAMPLE_RATE        22050
35
#define SEQ_FRAME_RATE         25
36
37
38
typedef struct TiertexSeqFrameBuffer {
39
    int fill_size;
40
    int data_size;
41
    unsigned char *data;
42
} TiertexSeqFrameBuffer;
43
44
typedef struct SeqDemuxContext {
45
    int audio_stream_index;
46
    int video_stream_index;
47
    int current_frame_pts;
48
    int current_frame_offs;
49
    TiertexSeqFrameBuffer frame_buffers[SEQ_NUM_FRAME_BUFFERS];
50
    int frame_buffers_count;
51
    unsigned int current_audio_data_size;
52
    unsigned int current_audio_data_offs;
53
    unsigned int current_pal_data_size;
54
    unsigned int current_pal_data_offs;
55
    unsigned int current_video_data_size;
56
    unsigned char *current_video_data_ptr;
57
    int audio_buffer_full;
58
} SeqDemuxContext;
59
60
61
static int seq_probe(AVProbeData *p)
62
{
63
    int i;
64
65
    if (p->buf_size < 256)
66
        return 0;
67
68
    /* there's no real header in a .seq file, the only thing they have in common */
69
    /* is the first 256 bytes of the file which are always filled with 0 */
70
    for (i = 0; i < 256; i++)
71 487240b0 Andreas Öman
        if (p->buf[i] != 0)
72 29f86228 Baptiste Coudurier
            return 0;
73
74
    /* only one fourth of the score since the previous check is too naive */
75
    return AVPROBE_SCORE_MAX / 4;
76
}
77
78
static int seq_init_frame_buffers(SeqDemuxContext *seq, ByteIOContext *pb)
79
{
80
    int i, sz;
81
    TiertexSeqFrameBuffer *seq_buffer;
82
83
    url_fseek(pb, 256, SEEK_SET);
84
85
    for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++) {
86
        sz = get_le16(pb);
87
        if (sz == 0)
88
            break;
89
        else {
90
            seq_buffer = &seq->frame_buffers[i];
91
            seq_buffer->fill_size = 0;
92
            seq_buffer->data_size = sz;
93
            seq_buffer->data = av_malloc(sz);
94
            if (!seq_buffer->data)
95
                return AVERROR_NOMEM;
96
        }
97
    }
98
    seq->frame_buffers_count = i;
99
    return 0;
100
}
101
102
static int seq_fill_buffer(SeqDemuxContext *seq, ByteIOContext *pb, int buffer_num, unsigned int data_offs, int data_size)
103
{
104
    TiertexSeqFrameBuffer *seq_buffer;
105
106
    if (buffer_num >= SEQ_NUM_FRAME_BUFFERS)
107
        return AVERROR_INVALIDDATA;
108
109
    seq_buffer = &seq->frame_buffers[buffer_num];
110
    if (seq_buffer->fill_size + data_size > seq_buffer->data_size)
111
        return AVERROR_INVALIDDATA;
112
113
    url_fseek(pb, seq->current_frame_offs + data_offs, SEEK_SET);
114
    if (get_buffer(pb, seq_buffer->data + seq_buffer->fill_size, data_size) != data_size)
115
        return AVERROR_IO;
116
117
    seq_buffer->fill_size += data_size;
118
    return 0;
119
}
120
121
static int seq_parse_frame_data(SeqDemuxContext *seq, ByteIOContext *pb)
122
{
123
    unsigned int offset_table[4], buffer_num[4];
124
    TiertexSeqFrameBuffer *seq_buffer;
125 71bd023d Andreas Öman
    int i, e, err;
126 29f86228 Baptiste Coudurier
127
    seq->current_frame_offs += SEQ_FRAME_SIZE;
128
    url_fseek(pb, seq->current_frame_offs, SEEK_SET);
129
130
    /* sound data */
131
    seq->current_audio_data_offs = get_le16(pb);
132
    if (seq->current_audio_data_offs != 0) {
133
        seq->current_audio_data_size = SEQ_AUDIO_BUFFER_SIZE * 2;
134
    } else {
135
        seq->current_audio_data_size = 0;
136
    }
137
138
    /* palette data */
139
    seq->current_pal_data_offs = get_le16(pb);
140
    if (seq->current_pal_data_offs != 0) {
141
        seq->current_pal_data_size = 768;
142
    } else {
143
        seq->current_pal_data_size = 0;
144
    }
145
146
    /* video data */
147
    for (i = 0; i < 4; i++)
148
        buffer_num[i] = get_byte(pb);
149
150
    for (i = 0; i < 4; i++)
151
        offset_table[i] = get_le16(pb);
152
153
    for (i = 0; i < 3; i++) {
154
        if (offset_table[i] != 0) {
155
            for (e = i + 1; e < 4 && offset_table[e] == 0; e++);
156 71bd023d Andreas Öman
            err = seq_fill_buffer(seq, pb, buffer_num[1 + i],
157 29f86228 Baptiste Coudurier
              offset_table[i],
158
              offset_table[e] - offset_table[i]);
159 71bd023d Andreas Öman
            if (err != 0)
160
                return err;
161 29f86228 Baptiste Coudurier
        }
162
    }
163
164
    if (buffer_num[0] != 255) {
165 71bd023d Andreas Öman
        if (buffer_num[0] >= SEQ_NUM_FRAME_BUFFERS)
166
            return AVERROR_INVALIDDATA;
167
168 29f86228 Baptiste Coudurier
        seq_buffer = &seq->frame_buffers[buffer_num[0]];
169
        seq->current_video_data_size = seq_buffer->fill_size;
170
        seq->current_video_data_ptr  = seq_buffer->data;
171
        seq_buffer->fill_size = 0;
172
    } else {
173
        seq->current_video_data_size = 0;
174
        seq->current_video_data_ptr  = 0;
175
    }
176
177
    return 0;
178
}
179
180
static int seq_read_header(AVFormatContext *s, AVFormatParameters *ap)
181
{
182
    int i, rc;
183
    SeqDemuxContext *seq = (SeqDemuxContext *)s->priv_data;
184
    ByteIOContext *pb = &s->pb;
185
    AVStream *st;
186
187
    /* init internal buffers */
188
    rc = seq_init_frame_buffers(seq, pb);
189
    if (rc)
190
        return rc;
191
192
    seq->current_frame_offs = 0;
193
194
    /* preload (no audio data, just buffer operations related data) */
195
    for (i = 1; i <= 100; i++) {
196
        rc = seq_parse_frame_data(seq, pb);
197
        if (rc)
198
            return rc;
199
    }
200
201
    seq->current_frame_pts = 0;
202
203
    seq->audio_buffer_full = 0;
204
205
    /* initialize the video decoder stream */
206
    st = av_new_stream(s, 0);
207
    if (!st)
208
        return AVERROR_NOMEM;
209
210
    av_set_pts_info(st, 32, 1, SEQ_FRAME_RATE);
211
    seq->video_stream_index = st->index;
212
    st->codec->codec_type = CODEC_TYPE_VIDEO;
213
    st->codec->codec_id = CODEC_ID_TIERTEXSEQVIDEO;
214
    st->codec->codec_tag = 0;  /* no fourcc */
215
    st->codec->width = SEQ_FRAME_W;
216
    st->codec->height = SEQ_FRAME_H;
217
218
    /* initialize the audio decoder stream */
219
    st = av_new_stream(s, 0);
220
    if (!st)
221
        return AVERROR_NOMEM;
222
223
    av_set_pts_info(st, 32, 1, SEQ_SAMPLE_RATE);
224
    seq->audio_stream_index = st->index;
225
    st->codec->codec_type = CODEC_TYPE_AUDIO;
226
    st->codec->codec_id = CODEC_ID_PCM_S16BE;
227
    st->codec->codec_tag = 0;  /* no tag */
228
    st->codec->channels = 1;
229
    st->codec->sample_rate = SEQ_SAMPLE_RATE;
230
    st->codec->bits_per_sample = 16;
231
    st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_sample * st->codec->channels;
232
    st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
233
234
    return 0;
235
}
236
237
static int seq_read_packet(AVFormatContext *s, AVPacket *pkt)
238
{
239
    int rc;
240
    SeqDemuxContext *seq = (SeqDemuxContext *)s->priv_data;
241
    ByteIOContext *pb = &s->pb;
242
243
    if (!seq->audio_buffer_full) {
244
        rc = seq_parse_frame_data(seq, pb);
245
        if (rc)
246
            return rc;
247
248
        /* video packet */
249
        if (seq->current_pal_data_size + seq->current_video_data_size != 0) {
250
            if (av_new_packet(pkt, 1 + seq->current_pal_data_size + seq->current_video_data_size))
251
                return AVERROR_NOMEM;
252
253
            pkt->data[0] = 0;
254
            if (seq->current_pal_data_size != 0) {
255
                pkt->data[0] |= 1;
256
                url_fseek(pb, seq->current_frame_offs + seq->current_pal_data_offs, SEEK_SET);
257
                if (get_buffer(pb, &pkt->data[1], seq->current_pal_data_size) != seq->current_pal_data_size)
258
                    return AVERROR_IO;
259
            }
260
            if (seq->current_video_data_size != 0) {
261
                pkt->data[0] |= 2;
262
                memcpy(&pkt->data[1 + seq->current_pal_data_size],
263
                  seq->current_video_data_ptr,
264
                  seq->current_video_data_size);
265
            }
266
            pkt->stream_index = seq->video_stream_index;
267
            pkt->pts = seq->current_frame_pts;
268
269
            /* sound buffer will be processed on next read_packet() call */
270
            seq->audio_buffer_full = 1;
271
            return 0;
272
       }
273
    }
274
275
    /* audio packet */
276
    if (seq->current_audio_data_offs == 0) /* end of data reached */
277
        return AVERROR_IO;
278
279
    url_fseek(pb, seq->current_frame_offs + seq->current_audio_data_offs, SEEK_SET);
280
    rc = av_get_packet(pb, pkt, seq->current_audio_data_size);
281
    if (rc < 0)
282
        return rc;
283
284
    pkt->stream_index = seq->audio_stream_index;
285
    pkt->pts = seq->current_frame_pts++;
286
287
    seq->audio_buffer_full = 0;
288
    return 0;
289
}
290
291
static int seq_read_close(AVFormatContext *s)
292
{
293
    int i;
294
    SeqDemuxContext *seq = (SeqDemuxContext *)s->priv_data;
295
296
    for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++)
297
        av_free(seq->frame_buffers[i].data);
298
299
    return 0;
300
}
301
302
AVInputFormat tiertexseq_demuxer = {
303
    "tiertexseq",
304
    "Tiertex Limited SEQ format",
305
    sizeof(SeqDemuxContext),
306
    seq_probe,
307
    seq_read_header,
308
    seq_read_packet,
309
    seq_read_close,
310
};