2006-10-12 12:02:58 +00:00
|
|
|
/*
|
|
|
|
* Tiertex Limited SEQ File Demuxer
|
|
|
|
* Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2006-10-12 12:02:58 +00:00
|
|
|
* Tiertex Limited SEQ file demuxer
|
|
|
|
*/
|
|
|
|
|
2012-04-07 22:44:20 +00:00
|
|
|
#include "libavutil/channel_layout.h"
|
2006-10-12 12:02:58 +00:00
|
|
|
#include "avformat.h"
|
2011-11-29 18:28:15 +00:00
|
|
|
#include "internal.h"
|
2006-10-12 12:02:58 +00:00
|
|
|
|
|
|
|
#define SEQ_FRAME_SIZE 6144
|
|
|
|
#define SEQ_FRAME_W 256
|
|
|
|
#define SEQ_FRAME_H 128
|
|
|
|
#define SEQ_NUM_FRAME_BUFFERS 30
|
|
|
|
#define SEQ_AUDIO_BUFFER_SIZE 882
|
|
|
|
#define SEQ_SAMPLE_RATE 22050
|
|
|
|
#define SEQ_FRAME_RATE 25
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct TiertexSeqFrameBuffer {
|
|
|
|
int fill_size;
|
|
|
|
int data_size;
|
|
|
|
unsigned char *data;
|
|
|
|
} TiertexSeqFrameBuffer;
|
|
|
|
|
|
|
|
typedef struct SeqDemuxContext {
|
|
|
|
int audio_stream_index;
|
|
|
|
int video_stream_index;
|
|
|
|
int current_frame_pts;
|
|
|
|
int current_frame_offs;
|
|
|
|
TiertexSeqFrameBuffer frame_buffers[SEQ_NUM_FRAME_BUFFERS];
|
|
|
|
int frame_buffers_count;
|
|
|
|
unsigned int current_audio_data_size;
|
|
|
|
unsigned int current_audio_data_offs;
|
|
|
|
unsigned int current_pal_data_size;
|
|
|
|
unsigned int current_pal_data_offs;
|
|
|
|
unsigned int current_video_data_size;
|
|
|
|
unsigned char *current_video_data_ptr;
|
|
|
|
int audio_buffer_full;
|
|
|
|
} SeqDemuxContext;
|
|
|
|
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int seq_probe(const AVProbeData *p)
|
2006-10-12 12:02:58 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-01-02 22:22:12 +00:00
|
|
|
if (p->buf_size < 258)
|
2006-10-12 12:02:58 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* there's no real header in a .seq file, the only thing they have in common */
|
|
|
|
/* is the first 256 bytes of the file which are always filled with 0 */
|
|
|
|
for (i = 0; i < 256; i++)
|
2008-05-28 20:33:22 +00:00
|
|
|
if (p->buf[i])
|
2006-10-12 12:02:58 +00:00
|
|
|
return 0;
|
|
|
|
|
2007-01-02 22:22:12 +00:00
|
|
|
if(p->buf[256]==0 && p->buf[257]==0)
|
|
|
|
return 0;
|
|
|
|
|
2006-10-12 12:02:58 +00:00
|
|
|
/* only one fourth of the score since the previous check is too naive */
|
|
|
|
return AVPROBE_SCORE_MAX / 4;
|
|
|
|
}
|
|
|
|
|
2011-02-20 10:04:12 +00:00
|
|
|
static int seq_init_frame_buffers(SeqDemuxContext *seq, AVIOContext *pb)
|
2006-10-12 12:02:58 +00:00
|
|
|
{
|
|
|
|
int i, sz;
|
|
|
|
TiertexSeqFrameBuffer *seq_buffer;
|
|
|
|
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(pb, 256, SEEK_SET);
|
2006-10-12 12:02:58 +00:00
|
|
|
|
|
|
|
for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++) {
|
2011-02-21 15:43:01 +00:00
|
|
|
sz = avio_rl16(pb);
|
2006-10-12 12:02:58 +00:00
|
|
|
if (sz == 0)
|
|
|
|
break;
|
|
|
|
else {
|
|
|
|
seq_buffer = &seq->frame_buffers[i];
|
|
|
|
seq_buffer->fill_size = 0;
|
|
|
|
seq_buffer->data_size = sz;
|
|
|
|
seq_buffer->data = av_malloc(sz);
|
|
|
|
if (!seq_buffer->data)
|
2007-07-19 15:21:30 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2006-10-12 12:02:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
seq->frame_buffers_count = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-20 10:04:12 +00:00
|
|
|
static int seq_fill_buffer(SeqDemuxContext *seq, AVIOContext *pb, int buffer_num, unsigned int data_offs, int data_size)
|
2006-10-12 12:02:58 +00:00
|
|
|
{
|
|
|
|
TiertexSeqFrameBuffer *seq_buffer;
|
|
|
|
|
|
|
|
if (buffer_num >= SEQ_NUM_FRAME_BUFFERS)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
seq_buffer = &seq->frame_buffers[buffer_num];
|
2008-05-28 20:30:42 +00:00
|
|
|
if (seq_buffer->fill_size + data_size > seq_buffer->data_size || data_size <= 0)
|
2006-10-12 12:02:58 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(pb, seq->current_frame_offs + data_offs, SEEK_SET);
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_read(pb, seq_buffer->data + seq_buffer->fill_size, data_size) != data_size)
|
2007-07-19 15:23:32 +00:00
|
|
|
return AVERROR(EIO);
|
2006-10-12 12:02:58 +00:00
|
|
|
|
|
|
|
seq_buffer->fill_size += data_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-20 10:04:12 +00:00
|
|
|
static int seq_parse_frame_data(SeqDemuxContext *seq, AVIOContext *pb)
|
2006-10-12 12:02:58 +00:00
|
|
|
{
|
|
|
|
unsigned int offset_table[4], buffer_num[4];
|
|
|
|
TiertexSeqFrameBuffer *seq_buffer;
|
2006-10-21 11:17:39 +00:00
|
|
|
int i, e, err;
|
2006-10-12 12:02:58 +00:00
|
|
|
|
|
|
|
seq->current_frame_offs += SEQ_FRAME_SIZE;
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(pb, seq->current_frame_offs, SEEK_SET);
|
2006-10-12 12:02:58 +00:00
|
|
|
|
|
|
|
/* sound data */
|
2011-02-21 15:43:01 +00:00
|
|
|
seq->current_audio_data_offs = avio_rl16(pb);
|
2008-05-28 20:33:22 +00:00
|
|
|
if (seq->current_audio_data_offs) {
|
2006-10-12 12:02:58 +00:00
|
|
|
seq->current_audio_data_size = SEQ_AUDIO_BUFFER_SIZE * 2;
|
|
|
|
} else {
|
|
|
|
seq->current_audio_data_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* palette data */
|
2011-02-21 15:43:01 +00:00
|
|
|
seq->current_pal_data_offs = avio_rl16(pb);
|
2008-05-28 20:33:22 +00:00
|
|
|
if (seq->current_pal_data_offs) {
|
2006-10-12 12:02:58 +00:00
|
|
|
seq->current_pal_data_size = 768;
|
|
|
|
} else {
|
|
|
|
seq->current_pal_data_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* video data */
|
|
|
|
for (i = 0; i < 4; i++)
|
2011-02-21 15:43:01 +00:00
|
|
|
buffer_num[i] = avio_r8(pb);
|
2006-10-12 12:02:58 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
2011-02-21 15:43:01 +00:00
|
|
|
offset_table[i] = avio_rl16(pb);
|
2006-10-12 12:02:58 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
2008-05-28 20:33:22 +00:00
|
|
|
if (offset_table[i]) {
|
2008-05-28 20:28:48 +00:00
|
|
|
for (e = i + 1; e < 3 && offset_table[e] == 0; e++);
|
2006-10-21 11:17:39 +00:00
|
|
|
err = seq_fill_buffer(seq, pb, buffer_num[1 + i],
|
2006-10-12 12:02:58 +00:00
|
|
|
offset_table[i],
|
|
|
|
offset_table[e] - offset_table[i]);
|
2008-05-28 20:33:22 +00:00
|
|
|
if (err)
|
2006-10-21 11:17:39 +00:00
|
|
|
return err;
|
2006-10-12 12:02:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer_num[0] != 255) {
|
2006-10-21 11:17:39 +00:00
|
|
|
if (buffer_num[0] >= SEQ_NUM_FRAME_BUFFERS)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2006-10-12 12:02:58 +00:00
|
|
|
seq_buffer = &seq->frame_buffers[buffer_num[0]];
|
|
|
|
seq->current_video_data_size = seq_buffer->fill_size;
|
|
|
|
seq->current_video_data_ptr = seq_buffer->data;
|
|
|
|
seq_buffer->fill_size = 0;
|
|
|
|
} else {
|
|
|
|
seq->current_video_data_size = 0;
|
|
|
|
seq->current_video_data_ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-08 07:24:52 +00:00
|
|
|
static int seq_read_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
SeqDemuxContext *seq = s->priv_data;
|
|
|
|
|
|
|
|
for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++)
|
|
|
|
av_freep(&seq->frame_buffers[i].data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 12:20:36 +00:00
|
|
|
static int seq_read_header(AVFormatContext *s)
|
2006-10-12 12:02:58 +00:00
|
|
|
{
|
|
|
|
int i, rc;
|
2007-04-08 20:24:16 +00:00
|
|
|
SeqDemuxContext *seq = s->priv_data;
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2006-10-12 12:02:58 +00:00
|
|
|
AVStream *st;
|
|
|
|
|
|
|
|
/* init internal buffers */
|
|
|
|
rc = seq_init_frame_buffers(seq, pb);
|
2019-06-08 07:25:27 +00:00
|
|
|
if (rc) {
|
|
|
|
seq_read_close(s);
|
2006-10-12 12:02:58 +00:00
|
|
|
return rc;
|
2019-06-08 07:25:27 +00:00
|
|
|
}
|
2006-10-12 12:02:58 +00:00
|
|
|
|
|
|
|
seq->current_frame_offs = 0;
|
|
|
|
|
|
|
|
/* preload (no audio data, just buffer operations related data) */
|
|
|
|
for (i = 1; i <= 100; i++) {
|
|
|
|
rc = seq_parse_frame_data(seq, pb);
|
2019-06-08 07:25:27 +00:00
|
|
|
if (rc) {
|
|
|
|
seq_read_close(s);
|
2006-10-12 12:02:58 +00:00
|
|
|
return rc;
|
2019-06-08 07:25:27 +00:00
|
|
|
}
|
2006-10-12 12:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
seq->current_frame_pts = 0;
|
|
|
|
|
|
|
|
seq->audio_buffer_full = 0;
|
|
|
|
|
|
|
|
/* initialize the video decoder stream */
|
2011-06-18 09:43:24 +00:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2019-06-08 07:25:27 +00:00
|
|
|
if (!st) {
|
|
|
|
seq_read_close(s);
|
2007-07-19 15:21:30 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2019-06-08 07:25:27 +00:00
|
|
|
}
|
2006-10-12 12:02:58 +00:00
|
|
|
|
2011-11-29 18:28:15 +00:00
|
|
|
avpriv_set_pts_info(st, 32, 1, SEQ_FRAME_RATE);
|
2006-10-12 12:02:58 +00:00
|
|
|
seq->video_stream_index = st->index;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_TIERTEXSEQVIDEO;
|
|
|
|
st->codecpar->codec_tag = 0; /* no fourcc */
|
|
|
|
st->codecpar->width = SEQ_FRAME_W;
|
|
|
|
st->codecpar->height = SEQ_FRAME_H;
|
2006-10-12 12:02:58 +00:00
|
|
|
|
|
|
|
/* initialize the audio decoder stream */
|
2011-06-18 09:43:24 +00:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2019-06-08 07:25:27 +00:00
|
|
|
if (!st) {
|
|
|
|
seq_read_close(s);
|
2007-07-19 15:21:30 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2019-06-08 07:25:27 +00:00
|
|
|
}
|
2006-10-12 12:02:58 +00:00
|
|
|
|
2012-01-20 04:01:53 +00:00
|
|
|
st->start_time = 0;
|
2011-11-29 18:28:15 +00:00
|
|
|
avpriv_set_pts_info(st, 32, 1, SEQ_SAMPLE_RATE);
|
2006-10-12 12:02:58 +00:00
|
|
|
seq->audio_stream_index = st->index;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
|
|
|
|
st->codecpar->codec_tag = 0; /* no tag */
|
|
|
|
st->codecpar->channels = 1;
|
|
|
|
st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
|
|
|
|
st->codecpar->sample_rate = SEQ_SAMPLE_RATE;
|
|
|
|
st->codecpar->bits_per_coded_sample = 16;
|
|
|
|
st->codecpar->bit_rate = st->codecpar->sample_rate * st->codecpar->bits_per_coded_sample * st->codecpar->channels;
|
|
|
|
st->codecpar->block_align = st->codecpar->channels * st->codecpar->bits_per_coded_sample / 8;
|
2006-10-12 12:02:58 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int seq_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int rc;
|
2007-04-08 20:24:16 +00:00
|
|
|
SeqDemuxContext *seq = s->priv_data;
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2006-10-12 12:02:58 +00:00
|
|
|
|
|
|
|
if (!seq->audio_buffer_full) {
|
|
|
|
rc = seq_parse_frame_data(seq, pb);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* video packet */
|
|
|
|
if (seq->current_pal_data_size + seq->current_video_data_size != 0) {
|
|
|
|
if (av_new_packet(pkt, 1 + seq->current_pal_data_size + seq->current_video_data_size))
|
2007-07-19 15:21:30 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2006-10-12 12:02:58 +00:00
|
|
|
|
|
|
|
pkt->data[0] = 0;
|
2008-05-28 20:33:22 +00:00
|
|
|
if (seq->current_pal_data_size) {
|
2006-10-12 12:02:58 +00:00
|
|
|
pkt->data[0] |= 1;
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(pb, seq->current_frame_offs + seq->current_pal_data_offs, SEEK_SET);
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_read(pb, &pkt->data[1], seq->current_pal_data_size) != seq->current_pal_data_size)
|
2007-07-19 15:23:32 +00:00
|
|
|
return AVERROR(EIO);
|
2006-10-12 12:02:58 +00:00
|
|
|
}
|
2008-05-28 20:33:22 +00:00
|
|
|
if (seq->current_video_data_size) {
|
2006-10-12 12:02:58 +00:00
|
|
|
pkt->data[0] |= 2;
|
|
|
|
memcpy(&pkt->data[1 + seq->current_pal_data_size],
|
|
|
|
seq->current_video_data_ptr,
|
|
|
|
seq->current_video_data_size);
|
|
|
|
}
|
|
|
|
pkt->stream_index = seq->video_stream_index;
|
|
|
|
pkt->pts = seq->current_frame_pts;
|
|
|
|
|
|
|
|
/* sound buffer will be processed on next read_packet() call */
|
|
|
|
seq->audio_buffer_full = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* audio packet */
|
|
|
|
if (seq->current_audio_data_offs == 0) /* end of data reached */
|
2007-07-19 15:23:32 +00:00
|
|
|
return AVERROR(EIO);
|
2006-10-12 12:02:58 +00:00
|
|
|
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(pb, seq->current_frame_offs + seq->current_audio_data_offs, SEEK_SET);
|
2006-10-12 12:02:58 +00:00
|
|
|
rc = av_get_packet(pb, pkt, seq->current_audio_data_size);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
pkt->stream_index = seq->audio_stream_index;
|
2008-07-06 10:00:12 +00:00
|
|
|
seq->current_frame_pts++;
|
2006-10-12 12:02:58 +00:00
|
|
|
|
|
|
|
seq->audio_buffer_full = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:03:28 +00:00
|
|
|
AVInputFormat ff_tiertexseq_demuxer = {
|
2011-07-16 20:18:12 +00:00
|
|
|
.name = "tiertexseq",
|
2012-07-24 21:51:41 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Tiertex Limited SEQ"),
|
2011-07-16 20:18:12 +00:00
|
|
|
.priv_data_size = sizeof(SeqDemuxContext),
|
|
|
|
.read_probe = seq_probe,
|
|
|
|
.read_header = seq_read_header,
|
|
|
|
.read_packet = seq_read_packet,
|
|
|
|
.read_close = seq_read_close,
|
2006-10-12 12:02:58 +00:00
|
|
|
};
|