2 * Tiertex Limited SEQ File Demuxer
3 * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Tiertex Limited SEQ file demuxer
30 #define SEQ_FRAME_SIZE 6144
31 #define SEQ_FRAME_W 256
32 #define SEQ_FRAME_H 128
33 #define SEQ_NUM_FRAME_BUFFERS 30
34 #define SEQ_AUDIO_BUFFER_SIZE 882
35 #define SEQ_SAMPLE_RATE 22050
36 #define SEQ_FRAME_RATE 25
39 typedef struct TiertexSeqFrameBuffer {
43 } TiertexSeqFrameBuffer;
45 typedef struct SeqDemuxContext {
46 int audio_stream_index;
47 int video_stream_index;
48 int current_frame_pts;
49 int current_frame_offs;
50 TiertexSeqFrameBuffer frame_buffers[SEQ_NUM_FRAME_BUFFERS];
51 int frame_buffers_count;
52 unsigned int current_audio_data_size;
53 unsigned int current_audio_data_offs;
54 unsigned int current_pal_data_size;
55 unsigned int current_pal_data_offs;
56 unsigned int current_video_data_size;
57 unsigned char *current_video_data_ptr;
58 int audio_buffer_full;
62 static int seq_probe(AVProbeData *p)
66 if (p->buf_size < 258)
69 /* there's no real header in a .seq file, the only thing they have in common */
70 /* is the first 256 bytes of the file which are always filled with 0 */
71 for (i = 0; i < 256; i++)
75 if(p->buf[256]==0 && p->buf[257]==0)
78 /* only one fourth of the score since the previous check is too naive */
79 return AVPROBE_SCORE_MAX / 4;
82 static int seq_init_frame_buffers(SeqDemuxContext *seq, AVIOContext *pb)
85 TiertexSeqFrameBuffer *seq_buffer;
87 avio_seek(pb, 256, SEEK_SET);
89 for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++) {
94 seq_buffer = &seq->frame_buffers[i];
95 seq_buffer->fill_size = 0;
96 seq_buffer->data_size = sz;
97 seq_buffer->data = av_malloc(sz);
98 if (!seq_buffer->data)
99 return AVERROR(ENOMEM);
102 seq->frame_buffers_count = i;
106 static int seq_fill_buffer(SeqDemuxContext *seq, AVIOContext *pb, int buffer_num, unsigned int data_offs, int data_size)
108 TiertexSeqFrameBuffer *seq_buffer;
110 if (buffer_num >= SEQ_NUM_FRAME_BUFFERS)
111 return AVERROR_INVALIDDATA;
113 seq_buffer = &seq->frame_buffers[buffer_num];
114 if (seq_buffer->fill_size + data_size > seq_buffer->data_size || data_size <= 0)
115 return AVERROR_INVALIDDATA;
117 avio_seek(pb, seq->current_frame_offs + data_offs, SEEK_SET);
118 if (avio_read(pb, seq_buffer->data + seq_buffer->fill_size, data_size) != data_size)
121 seq_buffer->fill_size += data_size;
125 static int seq_parse_frame_data(SeqDemuxContext *seq, AVIOContext *pb)
127 unsigned int offset_table[4], buffer_num[4];
128 TiertexSeqFrameBuffer *seq_buffer;
131 seq->current_frame_offs += SEQ_FRAME_SIZE;
132 avio_seek(pb, seq->current_frame_offs, SEEK_SET);
135 seq->current_audio_data_offs = avio_rl16(pb);
136 if (seq->current_audio_data_offs) {
137 seq->current_audio_data_size = SEQ_AUDIO_BUFFER_SIZE * 2;
139 seq->current_audio_data_size = 0;
143 seq->current_pal_data_offs = avio_rl16(pb);
144 if (seq->current_pal_data_offs) {
145 seq->current_pal_data_size = 768;
147 seq->current_pal_data_size = 0;
151 for (i = 0; i < 4; i++)
152 buffer_num[i] = avio_r8(pb);
154 for (i = 0; i < 4; i++)
155 offset_table[i] = avio_rl16(pb);
157 for (i = 0; i < 3; i++) {
158 if (offset_table[i]) {
159 for (e = i + 1; e < 3 && offset_table[e] == 0; e++);
160 err = seq_fill_buffer(seq, pb, buffer_num[1 + i],
162 offset_table[e] - offset_table[i]);
168 if (buffer_num[0] != 255) {
169 if (buffer_num[0] >= SEQ_NUM_FRAME_BUFFERS)
170 return AVERROR_INVALIDDATA;
172 seq_buffer = &seq->frame_buffers[buffer_num[0]];
173 seq->current_video_data_size = seq_buffer->fill_size;
174 seq->current_video_data_ptr = seq_buffer->data;
175 seq_buffer->fill_size = 0;
177 seq->current_video_data_size = 0;
178 seq->current_video_data_ptr = 0;
184 static int seq_read_header(AVFormatContext *s)
187 SeqDemuxContext *seq = s->priv_data;
188 AVIOContext *pb = s->pb;
191 /* init internal buffers */
192 rc = seq_init_frame_buffers(seq, pb);
196 seq->current_frame_offs = 0;
198 /* preload (no audio data, just buffer operations related data) */
199 for (i = 1; i <= 100; i++) {
200 rc = seq_parse_frame_data(seq, pb);
205 seq->current_frame_pts = 0;
207 seq->audio_buffer_full = 0;
209 /* initialize the video decoder stream */
210 st = avformat_new_stream(s, NULL);
212 return AVERROR(ENOMEM);
214 avpriv_set_pts_info(st, 32, 1, SEQ_FRAME_RATE);
215 seq->video_stream_index = st->index;
216 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
217 st->codec->codec_id = CODEC_ID_TIERTEXSEQVIDEO;
218 st->codec->codec_tag = 0; /* no fourcc */
219 st->codec->width = SEQ_FRAME_W;
220 st->codec->height = SEQ_FRAME_H;
222 /* initialize the audio decoder stream */
223 st = avformat_new_stream(s, NULL);
225 return AVERROR(ENOMEM);
228 avpriv_set_pts_info(st, 32, 1, SEQ_SAMPLE_RATE);
229 seq->audio_stream_index = st->index;
230 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
231 st->codec->codec_id = CODEC_ID_PCM_S16BE;
232 st->codec->codec_tag = 0; /* no tag */
233 st->codec->channels = 1;
234 st->codec->sample_rate = SEQ_SAMPLE_RATE;
235 st->codec->bits_per_coded_sample = 16;
236 st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_coded_sample * st->codec->channels;
237 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample / 8;
242 static int seq_read_packet(AVFormatContext *s, AVPacket *pkt)
245 SeqDemuxContext *seq = s->priv_data;
246 AVIOContext *pb = s->pb;
248 if (!seq->audio_buffer_full) {
249 rc = seq_parse_frame_data(seq, pb);
254 if (seq->current_pal_data_size + seq->current_video_data_size != 0) {
255 if (av_new_packet(pkt, 1 + seq->current_pal_data_size + seq->current_video_data_size))
256 return AVERROR(ENOMEM);
259 if (seq->current_pal_data_size) {
261 avio_seek(pb, seq->current_frame_offs + seq->current_pal_data_offs, SEEK_SET);
262 if (avio_read(pb, &pkt->data[1], seq->current_pal_data_size) != seq->current_pal_data_size)
265 if (seq->current_video_data_size) {
267 memcpy(&pkt->data[1 + seq->current_pal_data_size],
268 seq->current_video_data_ptr,
269 seq->current_video_data_size);
271 pkt->stream_index = seq->video_stream_index;
272 pkt->pts = seq->current_frame_pts;
274 /* sound buffer will be processed on next read_packet() call */
275 seq->audio_buffer_full = 1;
281 if (seq->current_audio_data_offs == 0) /* end of data reached */
284 avio_seek(pb, seq->current_frame_offs + seq->current_audio_data_offs, SEEK_SET);
285 rc = av_get_packet(pb, pkt, seq->current_audio_data_size);
289 pkt->stream_index = seq->audio_stream_index;
290 seq->current_frame_pts++;
292 seq->audio_buffer_full = 0;
296 static int seq_read_close(AVFormatContext *s)
299 SeqDemuxContext *seq = s->priv_data;
301 for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++)
302 av_free(seq->frame_buffers[i].data);
307 AVInputFormat ff_tiertexseq_demuxer = {
308 .name = "tiertexseq",
309 .long_name = NULL_IF_CONFIG_SMALL("Tiertex Limited SEQ format"),
310 .priv_data_size = sizeof(SeqDemuxContext),
311 .read_probe = seq_probe,
312 .read_header = seq_read_header,
313 .read_packet = seq_read_packet,
314 .read_close = seq_read_close,