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 "avformat.h"
29 #include "internal.h"
30 
31 #define SEQ_FRAME_SIZE 6144
32 #define SEQ_FRAME_W 256
33 #define SEQ_FRAME_H 128
34 #define SEQ_NUM_FRAME_BUFFERS 30
35 #define SEQ_AUDIO_BUFFER_SIZE 882
36 #define SEQ_SAMPLE_RATE 22050
37 #define SEQ_FRAME_RATE 25
38 
39 
40 typedef struct TiertexSeqFrameBuffer {
41  int fill_size;
42  int data_size;
43  unsigned char *data;
45 
46 typedef struct SeqDemuxContext {
55  unsigned int current_pal_data_size;
56  unsigned int current_pal_data_offs;
58  unsigned char *current_video_data_ptr;
61 
62 
63 static int seq_probe(AVProbeData *p)
64 {
65  int i;
66 
67  if (p->buf_size < 258)
68  return 0;
69 
70  /* there's no real header in a .seq file, the only thing they have in common */
71  /* is the first 256 bytes of the file which are always filled with 0 */
72  for (i = 0; i < 256; i++)
73  if (p->buf[i])
74  return 0;
75 
76  if(p->buf[256]==0 && p->buf[257]==0)
77  return 0;
78 
79  /* only one fourth of the score since the previous check is too naive */
80  return AVPROBE_SCORE_MAX / 4;
81 }
82 
84 {
85  int i, sz;
86  TiertexSeqFrameBuffer *seq_buffer;
87 
88  avio_seek(pb, 256, SEEK_SET);
89 
90  for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++) {
91  sz = avio_rl16(pb);
92  if (sz == 0)
93  break;
94  else {
95  seq_buffer = &seq->frame_buffers[i];
96  seq_buffer->fill_size = 0;
97  seq_buffer->data_size = sz;
98  seq_buffer->data = av_malloc(sz);
99  if (!seq_buffer->data)
100  return AVERROR(ENOMEM);
101  }
102  }
103  seq->frame_buffers_count = i;
104  return 0;
105 }
106 
107 static int seq_fill_buffer(SeqDemuxContext *seq, AVIOContext *pb, int buffer_num, unsigned int data_offs, int data_size)
108 {
109  TiertexSeqFrameBuffer *seq_buffer;
110 
111  if (buffer_num >= SEQ_NUM_FRAME_BUFFERS)
112  return AVERROR_INVALIDDATA;
113 
114  seq_buffer = &seq->frame_buffers[buffer_num];
115  if (seq_buffer->fill_size + data_size > seq_buffer->data_size || data_size <= 0)
116  return AVERROR_INVALIDDATA;
117 
118  avio_seek(pb, seq->current_frame_offs + data_offs, SEEK_SET);
119  if (avio_read(pb, seq_buffer->data + seq_buffer->fill_size, data_size) != data_size)
120  return AVERROR(EIO);
121 
122  seq_buffer->fill_size += data_size;
123  return 0;
124 }
125 
127 {
128  unsigned int offset_table[4], buffer_num[4];
129  TiertexSeqFrameBuffer *seq_buffer;
130  int i, e, err;
131 
133  avio_seek(pb, seq->current_frame_offs, SEEK_SET);
134 
135  /* sound data */
137  if (seq->current_audio_data_offs) {
139  } else {
140  seq->current_audio_data_size = 0;
141  }
142 
143  /* palette data */
144  seq->current_pal_data_offs = avio_rl16(pb);
145  if (seq->current_pal_data_offs) {
146  seq->current_pal_data_size = 768;
147  } else {
148  seq->current_pal_data_size = 0;
149  }
150 
151  /* video data */
152  for (i = 0; i < 4; i++)
153  buffer_num[i] = avio_r8(pb);
154 
155  for (i = 0; i < 4; i++)
156  offset_table[i] = avio_rl16(pb);
157 
158  for (i = 0; i < 3; i++) {
159  if (offset_table[i]) {
160  for (e = i + 1; e < 3 && offset_table[e] == 0; e++);
161  err = seq_fill_buffer(seq, pb, buffer_num[1 + i],
162  offset_table[i],
163  offset_table[e] - offset_table[i]);
164  if (err)
165  return err;
166  }
167  }
168 
169  if (buffer_num[0] != 255) {
170  if (buffer_num[0] >= SEQ_NUM_FRAME_BUFFERS)
171  return AVERROR_INVALIDDATA;
172 
173  seq_buffer = &seq->frame_buffers[buffer_num[0]];
174  seq->current_video_data_size = seq_buffer->fill_size;
175  seq->current_video_data_ptr = seq_buffer->data;
176  seq_buffer->fill_size = 0;
177  } else {
178  seq->current_video_data_size = 0;
179  seq->current_video_data_ptr = 0;
180  }
181 
182  return 0;
183 }
184 
186 {
187  int i, rc;
188  SeqDemuxContext *seq = s->priv_data;
189  AVIOContext *pb = s->pb;
190  AVStream *st;
191 
192  /* init internal buffers */
193  rc = seq_init_frame_buffers(seq, pb);
194  if (rc)
195  return rc;
196 
197  seq->current_frame_offs = 0;
198 
199  /* preload (no audio data, just buffer operations related data) */
200  for (i = 1; i <= 100; i++) {
201  rc = seq_parse_frame_data(seq, pb);
202  if (rc)
203  return rc;
204  }
205 
206  seq->current_frame_pts = 0;
207 
208  seq->audio_buffer_full = 0;
209 
210  /* initialize the video decoder stream */
211  st = avformat_new_stream(s, NULL);
212  if (!st)
213  return AVERROR(ENOMEM);
214 
216  seq->video_stream_index = st->index;
219  st->codec->codec_tag = 0; /* no fourcc */
220  st->codec->width = SEQ_FRAME_W;
221  st->codec->height = SEQ_FRAME_H;
222 
223  /* initialize the audio decoder stream */
224  st = avformat_new_stream(s, NULL);
225  if (!st)
226  return AVERROR(ENOMEM);
227 
228  st->start_time = 0;
230  seq->audio_stream_index = st->index;
233  st->codec->codec_tag = 0; /* no tag */
234  st->codec->channels = 1;
237  st->codec->bits_per_coded_sample = 16;
240 
241  return 0;
242 }
243 
245 {
246  int rc;
247  SeqDemuxContext *seq = s->priv_data;
248  AVIOContext *pb = s->pb;
249 
250  if (!seq->audio_buffer_full) {
251  rc = seq_parse_frame_data(seq, pb);
252  if (rc)
253  return rc;
254 
255  /* video packet */
256  if (seq->current_pal_data_size + seq->current_video_data_size != 0) {
258  return AVERROR(ENOMEM);
259 
260  pkt->data[0] = 0;
261  if (seq->current_pal_data_size) {
262  pkt->data[0] |= 1;
263  avio_seek(pb, seq->current_frame_offs + seq->current_pal_data_offs, SEEK_SET);
264  if (avio_read(pb, &pkt->data[1], seq->current_pal_data_size) != seq->current_pal_data_size)
265  return AVERROR(EIO);
266  }
267  if (seq->current_video_data_size) {
268  pkt->data[0] |= 2;
269  memcpy(&pkt->data[1 + seq->current_pal_data_size],
272  }
273  pkt->stream_index = seq->video_stream_index;
274  pkt->pts = seq->current_frame_pts;
275 
276  /* sound buffer will be processed on next read_packet() call */
277  seq->audio_buffer_full = 1;
278  return 0;
279  }
280  }
281 
282  /* audio packet */
283  if (seq->current_audio_data_offs == 0) /* end of data reached */
284  return AVERROR(EIO);
285 
286  avio_seek(pb, seq->current_frame_offs + seq->current_audio_data_offs, SEEK_SET);
287  rc = av_get_packet(pb, pkt, seq->current_audio_data_size);
288  if (rc < 0)
289  return rc;
290 
291  pkt->stream_index = seq->audio_stream_index;
292  seq->current_frame_pts++;
293 
294  seq->audio_buffer_full = 0;
295  return 0;
296 }
297 
299 {
300  int i;
301  SeqDemuxContext *seq = s->priv_data;
302 
303  for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++)
304  av_free(seq->frame_buffers[i].data);
305 
306  return 0;
307 }
308 
310  .name = "tiertexseq",
311  .long_name = NULL_IF_CONFIG_SMALL("Tiertex Limited SEQ"),
312  .priv_data_size = sizeof(SeqDemuxContext),
317 };
unsigned int current_audio_data_size
Definition: tiertexseq.c:53
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int seq_probe(AVProbeData *p)
Definition: tiertexseq.c:63
AVInputFormat ff_tiertexseq_demuxer
Definition: tiertexseq.c:309
int audio_buffer_full
Definition: tiertexseq.c:59
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
int index
stream index in AVFormatContext
Definition: avformat.h:644
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
#define SEQ_AUDIO_BUFFER_SIZE
Definition: tiertexseq.c:35
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
unsigned int current_audio_data_offs
Definition: tiertexseq.c:54
Format I/O context.
Definition: avformat.h:944
int video_stream_index
Definition: tiertexseq.c:48
static AVPacket pkt
Definition: demuxing.c:56
#define SEQ_FRAME_W
Definition: tiertexseq.c:32
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint8_t * data
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
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.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
unsigned int current_pal_data_size
Definition: tiertexseq.c:55
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
static int seq_parse_frame_data(SeqDemuxContext *seq, AVIOContext *pb)
Definition: tiertexseq.c:126
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:73
#define SEQ_NUM_FRAME_BUFFERS
Definition: tiertexseq.c:34
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
static int seq_fill_buffer(SeqDemuxContext *seq, AVIOContext *pb, int buffer_num, unsigned int data_offs, int data_size)
Definition: tiertexseq.c:107
int current_frame_offs
Definition: tiertexseq.c:50
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
struct SeqDemuxContext SeqDemuxContext
struct TiertexSeqFrameBuffer TiertexSeqFrameBuffer
static int seq_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: tiertexseq.c:244
uint64_t channel_layout
Audio channel layout.
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:469
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:337
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
int current_frame_pts
Definition: tiertexseq.c:49
int frame_buffers_count
Definition: tiertexseq.c:52
int bit_rate
the average bitrate
audio channel layout utility functions
unsigned int current_pal_data_offs
Definition: tiertexseq.c:56
static int read_probe(AVProbeData *pd)
int width
picture width / height.
unsigned char * current_video_data_ptr
Definition: tiertexseq.c:58
static const int offset_table[6]
Definition: vc1dec.c:3315
TiertexSeqFrameBuffer frame_buffers[SEQ_NUM_FRAME_BUFFERS]
Definition: tiertexseq.c:51
#define SEQ_FRAME_RATE
Definition: tiertexseq.c:37
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
Stream structure.
Definition: avformat.h:643
NULL
Definition: eval.c:55
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
static int seq_read_close(AVFormatContext *s)
Definition: tiertexseq.c:298
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
synthesis window for stochastic i
unsigned int current_video_data_size
Definition: tiertexseq.c:57
int audio_stream_index
Definition: tiertexseq.c:47
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:563
Main libavformat public API header.
unsigned char * data
Definition: tiertexseq.c:43
#define SEQ_SAMPLE_RATE
Definition: tiertexseq.c:36
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:689
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
#define SEQ_FRAME_SIZE
Definition: tiertexseq.c:31
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
#define AV_CH_LAYOUT_MONO
static int seq_read_header(AVFormatContext *s)
Definition: tiertexseq.c:185
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static int seq_init_frame_buffers(SeqDemuxContext *seq, AVIOContext *pb)
Definition: tiertexseq.c:83
#define SEQ_FRAME_H
Definition: tiertexseq.c:33