FFmpeg
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 "demux.h"
31 #include "internal.h"
32 
33 #define SEQ_FRAME_SIZE 6144
34 #define SEQ_FRAME_W 256
35 #define SEQ_FRAME_H 128
36 #define SEQ_NUM_FRAME_BUFFERS 30
37 #define SEQ_AUDIO_BUFFER_SIZE 882
38 #define SEQ_SAMPLE_RATE 22050
39 #define SEQ_FRAME_RATE 25
40 
41 
42 typedef struct TiertexSeqFrameBuffer {
43  int fill_size;
44  int data_size;
45  unsigned char *data;
47 
48 typedef struct SeqDemuxContext {
57  unsigned int current_pal_data_size;
58  unsigned int current_pal_data_offs;
60  unsigned char *current_video_data_ptr;
63 
64 
65 static int seq_probe(const AVProbeData *p)
66 {
67  int i;
68 
69  if (p->buf_size < 258)
70  return 0;
71 
72  /* there's no real header in a .seq file, the only thing they have in common */
73  /* is the first 256 bytes of the file which are always filled with 0 */
74  for (i = 0; i < 256; i++)
75  if (p->buf[i])
76  return 0;
77 
78  if(p->buf[256]==0 && p->buf[257]==0)
79  return 0;
80 
81  /* only one fourth of the score since the previous check is too naive */
82  return AVPROBE_SCORE_MAX / 4;
83 }
84 
86 {
87  int i, sz;
88  TiertexSeqFrameBuffer *seq_buffer;
89 
90  avio_seek(pb, 256, SEEK_SET);
91 
92  for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++) {
93  sz = avio_rl16(pb);
94  if (sz == 0)
95  break;
96  else {
97  seq_buffer = &seq->frame_buffers[i];
98  seq_buffer->fill_size = 0;
99  seq_buffer->data_size = sz;
100  seq_buffer->data = av_malloc(sz);
101  if (!seq_buffer->data)
102  return AVERROR(ENOMEM);
103  }
104  }
105  seq->frame_buffers_count = i;
106  return 0;
107 }
108 
109 static int seq_fill_buffer(SeqDemuxContext *seq, AVIOContext *pb, int buffer_num, unsigned int data_offs, int data_size)
110 {
111  TiertexSeqFrameBuffer *seq_buffer;
112 
113  if (buffer_num >= SEQ_NUM_FRAME_BUFFERS)
114  return AVERROR_INVALIDDATA;
115 
116  seq_buffer = &seq->frame_buffers[buffer_num];
117  if (seq_buffer->fill_size + data_size > seq_buffer->data_size || data_size <= 0)
118  return AVERROR_INVALIDDATA;
119 
120  avio_seek(pb, seq->current_frame_offs + data_offs, SEEK_SET);
121  if (avio_read(pb, seq_buffer->data + seq_buffer->fill_size, data_size) != data_size)
122  return AVERROR(EIO);
123 
124  seq_buffer->fill_size += data_size;
125  return 0;
126 }
127 
129 {
130  unsigned int offset_table[4], buffer_num[4];
131  TiertexSeqFrameBuffer *seq_buffer;
132  int i, e, err;
133 
135  avio_seek(pb, seq->current_frame_offs, SEEK_SET);
136 
137  /* sound data */
139  if (seq->current_audio_data_offs) {
141  } else {
142  seq->current_audio_data_size = 0;
143  }
144 
145  /* palette data */
146  seq->current_pal_data_offs = avio_rl16(pb);
147  if (seq->current_pal_data_offs) {
148  seq->current_pal_data_size = 768;
149  } else {
150  seq->current_pal_data_size = 0;
151  }
152 
153  /* video data */
154  for (i = 0; i < 4; i++)
155  buffer_num[i] = avio_r8(pb);
156 
157  for (i = 0; i < 4; i++)
158  offset_table[i] = avio_rl16(pb);
159 
160  for (i = 0; i < 3; i++) {
161  if (offset_table[i]) {
162  for (e = i + 1; e < 3 && offset_table[e] == 0; e++);
163  err = seq_fill_buffer(seq, pb, buffer_num[1 + i],
164  offset_table[i],
165  offset_table[e] - offset_table[i]);
166  if (err)
167  return err;
168  }
169  }
170 
171  if (buffer_num[0] != 255) {
172  if (buffer_num[0] >= SEQ_NUM_FRAME_BUFFERS)
173  return AVERROR_INVALIDDATA;
174 
175  seq_buffer = &seq->frame_buffers[buffer_num[0]];
176  seq->current_video_data_size = seq_buffer->fill_size;
177  seq->current_video_data_ptr = seq_buffer->data;
178  seq_buffer->fill_size = 0;
179  } else {
180  seq->current_video_data_size = 0;
181  seq->current_video_data_ptr = 0;
182  }
183 
184  return 0;
185 }
186 
188 {
189  int i;
190  SeqDemuxContext *seq = s->priv_data;
191 
192  for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++)
193  av_freep(&seq->frame_buffers[i].data);
194 
195  return 0;
196 }
197 
199 {
200  int i, rc;
201  SeqDemuxContext *seq = s->priv_data;
202  AVIOContext *pb = s->pb;
203  AVStream *st;
204 
205  /* init internal buffers */
206  rc = seq_init_frame_buffers(seq, pb);
207  if (rc < 0)
208  return rc;
209 
210  seq->current_frame_offs = 0;
211 
212  /* preload (no audio data, just buffer operations related data) */
213  for (i = 1; i <= 100; i++) {
214  rc = seq_parse_frame_data(seq, pb);
215  if (rc < 0)
216  return rc;
217  }
218 
219  seq->current_frame_pts = 0;
220 
221  seq->audio_buffer_full = 0;
222 
223  /* initialize the video decoder stream */
224  st = avformat_new_stream(s, NULL);
225  if (!st)
226  return AVERROR(ENOMEM);
227 
229  seq->video_stream_index = st->index;
232  st->codecpar->codec_tag = 0; /* no fourcc */
233  st->codecpar->width = SEQ_FRAME_W;
234  st->codecpar->height = SEQ_FRAME_H;
235 
236  /* initialize the audio decoder stream */
237  st = avformat_new_stream(s, NULL);
238  if (!st)
239  return AVERROR(ENOMEM);
240 
241  st->start_time = 0;
243  seq->audio_stream_index = st->index;
246  st->codecpar->codec_tag = 0; /* no tag */
252 
253  return 0;
254 }
255 
257 {
258  int rc;
259  SeqDemuxContext *seq = s->priv_data;
260  AVIOContext *pb = s->pb;
261 
262  if (!seq->audio_buffer_full) {
263  rc = seq_parse_frame_data(seq, pb);
264  if (rc)
265  return rc;
266 
267  /* video packet */
268  if (seq->current_pal_data_size + seq->current_video_data_size != 0) {
270  + seq->current_video_data_size);
271  if (rc < 0)
272  return rc;
273 
274  pkt->data[0] = 0;
275  if (seq->current_pal_data_size) {
276  pkt->data[0] |= 1;
277  avio_seek(pb, seq->current_frame_offs + seq->current_pal_data_offs, SEEK_SET);
278  if (avio_read(pb, &pkt->data[1], seq->current_pal_data_size) != seq->current_pal_data_size)
279  return AVERROR(EIO);
280  }
281  if (seq->current_video_data_size) {
282  pkt->data[0] |= 2;
283  memcpy(&pkt->data[1 + seq->current_pal_data_size],
286  }
288  pkt->pts = seq->current_frame_pts;
289 
290  /* sound buffer will be processed on next read_packet() call */
291  seq->audio_buffer_full = 1;
292  return 0;
293  }
294  }
295 
296  /* audio packet */
297  if (seq->current_audio_data_offs == 0) /* end of data reached */
298  return AVERROR(EIO);
299 
300  avio_seek(pb, seq->current_frame_offs + seq->current_audio_data_offs, SEEK_SET);
301  rc = av_get_packet(pb, pkt, seq->current_audio_data_size);
302  if (rc < 0)
303  return rc;
304 
306  seq->current_frame_pts++;
307 
308  seq->audio_buffer_full = 0;
309  return 0;
310 }
311 
313  .p.name = "tiertexseq",
314  .p.long_name = NULL_IF_CONFIG_SMALL("Tiertex Limited SEQ"),
315  .priv_data_size = sizeof(SeqDemuxContext),
316  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
321 };
seq_probe
static int seq_probe(const AVProbeData *p)
Definition: tiertexseq.c:65
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
seq_read_packet
static int seq_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: tiertexseq.c:256
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
SEQ_FRAME_RATE
#define SEQ_FRAME_RATE
Definition: tiertexseq.c:39
SeqDemuxContext::current_frame_offs
int current_frame_offs
Definition: tiertexseq.c:52
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
seq_read_close
static int seq_read_close(AVFormatContext *s)
Definition: tiertexseq.c:187
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
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:867
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:335
SeqDemuxContext::current_audio_data_offs
unsigned int current_audio_data_offs
Definition: tiertexseq.c:56
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
SEQ_SAMPLE_RATE
#define SEQ_SAMPLE_RATE
Definition: tiertexseq.c:38
pkt
AVPacket * pkt
Definition: movenc.c:60
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
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
SeqDemuxContext::current_frame_pts
int current_frame_pts
Definition: tiertexseq.c:51
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
SEQ_FRAME_SIZE
#define SEQ_FRAME_SIZE
Definition: tiertexseq.c:33
AV_CODEC_ID_TIERTEXSEQVIDEO
@ AV_CODEC_ID_TIERTEXSEQVIDEO
Definition: codec_id.h:147
SeqDemuxContext::audio_buffer_full
int audio_buffer_full
Definition: tiertexseq.c:61
FF_INFMT_FLAG_INIT_CLEANUP
#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
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
NULL
#define NULL
Definition: coverity.c:32
SeqDemuxContext::audio_stream_index
int audio_stream_index
Definition: tiertexseq.c:49
seq_read_header
static int seq_read_header(AVFormatContext *s)
Definition: tiertexseq.c:198
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
SeqDemuxContext::frame_buffers_count
int frame_buffers_count
Definition: tiertexseq.c:54
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:311
TiertexSeqFrameBuffer::data_size
int data_size
Definition: tiertexseq.c:44
seq_init_frame_buffers
static int seq_init_frame_buffers(SeqDemuxContext *seq, AVIOContext *pb)
Definition: tiertexseq.c:85
SEQ_FRAME_H
#define SEQ_FRAME_H
Definition: tiertexseq.c:35
SEQ_AUDIO_BUFFER_SIZE
#define SEQ_AUDIO_BUFFER_SIZE
Definition: tiertexseq.c:37
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
SEQ_FRAME_W
#define SEQ_FRAME_W
Definition: tiertexseq.c:34
TiertexSeqFrameBuffer::data
unsigned char * data
Definition: tiertexseq.c:45
ff_tiertexseq_demuxer
const FFInputFormat ff_tiertexseq_demuxer
Definition: tiertexseq.c:312
TiertexSeqFrameBuffer
Definition: tiertexseq.c:42
offset_table
static const uint8_t offset_table[]
Definition: escape130.c:42
SeqDemuxContext::current_video_data_size
unsigned int current_video_data_size
Definition: tiertexseq.c:59
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
AVCodecParameters::height
int height
Definition: codec_par.h:135
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
SeqDemuxContext
Definition: tiertexseq.c:48
demux.h
av_get_packet
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:91
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
seq_parse_frame_data
static int seq_parse_frame_data(SeqDemuxContext *seq, AVIOContext *pb)
Definition: tiertexseq.c:128
avformat.h
SEQ_NUM_FRAME_BUFFERS
#define SEQ_NUM_FRAME_BUFFERS
Definition: tiertexseq.c:36
SeqDemuxContext::current_pal_data_offs
unsigned int current_pal_data_offs
Definition: tiertexseq.c:58
SeqDemuxContext::current_video_data_ptr
unsigned char * current_video_data_ptr
Definition: tiertexseq.c:60
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:754
channel_layout.h
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
SeqDemuxContext::frame_buffers
TiertexSeqFrameBuffer frame_buffers[SEQ_NUM_FRAME_BUFFERS]
Definition: tiertexseq.c:53
AVPacket::stream_index
int stream_index
Definition: packet.h:541
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
mem.h
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:386
SeqDemuxContext::video_stream_index
int video_stream_index
Definition: tiertexseq.c:50
seq_fill_buffer
static int seq_fill_buffer(SeqDemuxContext *seq, AVIOContext *pb, int buffer_num, unsigned int data_offs, int data_size)
Definition: tiertexseq.c:109
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
SeqDemuxContext::current_audio_data_size
unsigned int current_audio_data_size
Definition: tiertexseq.c:55
FFInputFormat
Definition: demux.h:42
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:797
SeqDemuxContext::current_pal_data_size
unsigned int current_pal_data_size
Definition: tiertexseq.c:57
TiertexSeqFrameBuffer::fill_size
int fill_size
Definition: tiertexseq.c:43