FFmpeg
Loading...
Searching...
No Matches
tiertexseq.c
Go to the documentation of this file.
1/*
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
24 * Tiertex Limited SEQ file demuxer
25 */
26
28#include "libavutil/mem.h"
29#include "avformat.h"
30#include "avio_internal.h"
31#include "demux.h"
32#include "internal.h"
33
34#define SEQ_FRAME_SIZE 6144
35#define SEQ_FRAME_W 256
36#define SEQ_FRAME_H 128
37#define SEQ_NUM_FRAME_BUFFERS 30
38#define SEQ_AUDIO_BUFFER_SIZE 882
39#define SEQ_SAMPLE_RATE 22050
40#define SEQ_FRAME_RATE 25
41
42
43typedef struct TiertexSeqFrameBuffer {
46 unsigned char *data;
48
64
65
66static int seq_probe(const AVProbeData *p)
67{
68 int i;
69
70 if (p->buf_size < 258)
71 return 0;
72
73 /* there's no real header in a .seq file, the only thing they have in common */
74 /* is the first 256 bytes of the file which are always filled with 0 */
75 for (i = 0; i < 256; i++)
76 if (p->buf[i])
77 return 0;
78
79 if(p->buf[256]==0 && p->buf[257]==0)
80 return 0;
81
82 /* only one fourth of the score since the previous check is too naive */
83 return AVPROBE_SCORE_MAX / 4;
84}
85
87{
88 int i, sz;
89 TiertexSeqFrameBuffer *seq_buffer;
90
91 avio_seek(pb, 256, SEEK_SET);
92
93 for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++) {
94 sz = avio_rl16(pb);
95 if (sz == 0)
96 break;
97 else {
98 seq_buffer = &seq->frame_buffers[i];
99 seq_buffer->fill_size = 0;
100 seq_buffer->data_size = sz;
101 seq_buffer->data = av_malloc(sz);
102 if (!seq_buffer->data)
103 return AVERROR(ENOMEM);
104 }
105 }
106 seq->frame_buffers_count = i;
107 return 0;
108}
109
110static int seq_fill_buffer(SeqDemuxContext *seq, AVIOContext *pb, int buffer_num, unsigned int data_offs, int data_size)
111{
112 TiertexSeqFrameBuffer *seq_buffer;
113 int ret;
114
115 if (buffer_num >= SEQ_NUM_FRAME_BUFFERS)
116 return AVERROR_INVALIDDATA;
117
118 seq_buffer = &seq->frame_buffers[buffer_num];
119 if (seq_buffer->fill_size + data_size > seq_buffer->data_size || data_size <= 0)
120 return AVERROR_INVALIDDATA;
121
122 avio_seek(pb, seq->current_frame_offs + data_offs, SEEK_SET);
123 if ((ret = ffio_read_size(pb, seq_buffer->data + seq_buffer->fill_size, data_size)) < 0)
124 return ret;
125
126 seq_buffer->fill_size += data_size;
127 return 0;
128}
129
131{
132 unsigned int offset_table[4], buffer_num[4];
133 TiertexSeqFrameBuffer *seq_buffer;
134 int i, e, err;
135
137 avio_seek(pb, seq->current_frame_offs, SEEK_SET);
138
139 /* sound data */
141 if (seq->current_audio_data_offs) {
143 } else {
145 }
146
147 /* palette data */
149 if (seq->current_pal_data_offs) {
150 seq->current_pal_data_size = 768;
151 } else {
152 seq->current_pal_data_size = 0;
153 }
154
155 /* video data */
156 for (i = 0; i < 4; i++)
157 buffer_num[i] = avio_r8(pb);
158
159 for (i = 0; i < 4; i++)
160 offset_table[i] = avio_rl16(pb);
161
162 for (i = 0; i < 3; i++) {
163 if (offset_table[i]) {
164 for (e = i + 1; e < 3 && offset_table[e] == 0; e++);
165 err = seq_fill_buffer(seq, pb, buffer_num[1 + i],
168 if (err)
169 return err;
170 }
171 }
172
173 if (buffer_num[0] != 255) {
174 if (buffer_num[0] >= SEQ_NUM_FRAME_BUFFERS)
175 return AVERROR_INVALIDDATA;
176
177 seq_buffer = &seq->frame_buffers[buffer_num[0]];
178 seq->current_video_data_size = seq_buffer->fill_size;
179 seq->current_video_data_ptr = seq_buffer->data;
180 seq_buffer->fill_size = 0;
181 } else {
183 seq->current_video_data_ptr = 0;
184 }
185
186 return 0;
187}
188
190{
191 int i;
192 SeqDemuxContext *seq = s->priv_data;
193
194 for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++)
196
197 return 0;
198}
199
201{
202 int i, rc;
203 SeqDemuxContext *seq = s->priv_data;
204 AVIOContext *pb = s->pb;
205 AVStream *st;
206
207 /* init internal buffers */
208 rc = seq_init_frame_buffers(seq, pb);
209 if (rc < 0)
210 return rc;
211
212 seq->current_frame_offs = 0;
213
214 /* preload (no audio data, just buffer operations related data) */
215 for (i = 1; i <= 100; i++) {
216 rc = seq_parse_frame_data(seq, pb);
217 if (rc < 0)
218 return rc;
219 }
220
221 seq->current_frame_pts = 0;
222
223 seq->audio_buffer_full = 0;
224
225 /* initialize the video decoder stream */
227 if (!st)
228 return AVERROR(ENOMEM);
229
231 seq->video_stream_index = st->index;
234 st->codecpar->codec_tag = 0; /* no fourcc */
237
238 /* initialize the audio decoder stream */
240 if (!st)
241 return AVERROR(ENOMEM);
242
243 st->start_time = 0;
245 seq->audio_stream_index = st->index;
248 st->codecpar->codec_tag = 0; /* no tag */
254
255 return 0;
256}
257
259{
260 int rc;
261 SeqDemuxContext *seq = s->priv_data;
262 AVIOContext *pb = s->pb;
263
264 if (!seq->audio_buffer_full) {
265 rc = seq_parse_frame_data(seq, pb);
266 if (rc)
267 return rc;
268
269 /* video packet */
270 if (seq->current_pal_data_size + seq->current_video_data_size != 0) {
273 if (rc < 0)
274 return rc;
275
276 pkt->data[0] = 0;
277 if (seq->current_pal_data_size) {
278 int ret;
279 pkt->data[0] |= 1;
280 avio_seek(pb, seq->current_frame_offs + seq->current_pal_data_offs, SEEK_SET);
281 if ((ret = ffio_read_size(pb, &pkt->data[1], seq->current_pal_data_size)) < 0)
282 return ret;
283 }
284 if (seq->current_video_data_size) {
285 pkt->data[0] |= 2;
286 memcpy(&pkt->data[1 + seq->current_pal_data_size],
289 }
290 pkt->stream_index = seq->video_stream_index;
291 pkt->pts = seq->current_frame_pts;
292
293 /* sound buffer will be processed on next read_packet() call */
294 seq->audio_buffer_full = 1;
295 return 0;
296 }
297 }
298
299 /* audio packet */
300 if (seq->current_audio_data_offs == 0) /* end of data reached */
301 return AVERROR_EOF;
302
303 avio_seek(pb, seq->current_frame_offs + seq->current_audio_data_offs, SEEK_SET);
305 if (rc < 0)
306 return rc;
307
308 pkt->stream_index = seq->audio_stream_index;
309 seq->current_frame_pts++;
310
311 seq->audio_buffer_full = 0;
312 return 0;
313}
314
316 .p.name = "tiertexseq",
317 .p.long_name = NULL_IF_CONFIG_SMALL("Tiertex Limited SEQ"),
318 .priv_data_size = sizeof(SeqDemuxContext),
319 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
324};
const FFInputFormat ff_tiertexseq_demuxer
Definition tiertexseq.c:315
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
static AVPacket * pkt
static const uint8_t offset_table[]
Definition escape130.c:42
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:331
@ AV_CODEC_ID_TIERTEXSEQVIDEO
Definition codec_id.h:145
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int index
stream index in AVFormatContext
Definition avformat.h:772
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
unsigned int current_pal_data_size
Definition tiertexseq.c:58
unsigned int current_pal_data_offs
Definition tiertexseq.c:59
unsigned int current_audio_data_offs
Definition tiertexseq.c:57
int frame_buffers_count
Definition tiertexseq.c:55
unsigned int current_video_data_size
Definition tiertexseq.c:60
unsigned char * current_video_data_ptr
Definition tiertexseq.c:61
unsigned int current_audio_data_size
Definition tiertexseq.c:56
TiertexSeqFrameBuffer frame_buffers[SEQ_NUM_FRAME_BUFFERS]
Definition tiertexseq.c:54
unsigned char * data
Definition tiertexseq.c:46
#define av_freep(p)
#define SEQ_FRAME_SIZE
Definition tiertexseq.c:34
static int seq_probe(const AVProbeData *p)
Definition tiertexseq.c:66
#define SEQ_FRAME_H
Definition tiertexseq.c:36
#define SEQ_SAMPLE_RATE
Definition tiertexseq.c:39
static int seq_init_frame_buffers(SeqDemuxContext *seq, AVIOContext *pb)
Definition tiertexseq.c:86
static int seq_parse_frame_data(SeqDemuxContext *seq, AVIOContext *pb)
Definition tiertexseq.c:130
#define SEQ_NUM_FRAME_BUFFERS
Definition tiertexseq.c:37
static int seq_fill_buffer(SeqDemuxContext *seq, AVIOContext *pb, int buffer_num, unsigned int data_offs, int data_size)
Definition tiertexseq.c:110
static int seq_read_close(AVFormatContext *s)
Definition tiertexseq.c:189
#define SEQ_FRAME_RATE
Definition tiertexseq.c:40
static int seq_read_header(AVFormatContext *s)
Definition tiertexseq.c:200
#define SEQ_FRAME_W
Definition tiertexseq.c:35
#define SEQ_AUDIO_BUFFER_SIZE
Definition tiertexseq.c:38
static int seq_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition tiertexseq.c:258