2006-10-12 15:02:58 +03: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 17:45:34 +03:00
|
|
|
* @file
|
2006-10-12 15:02:58 +03:00
|
|
|
* Tiertex Limited SEQ file demuxer
|
|
|
|
*/
|
|
|
|
|
2012-04-08 01:44:20 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2006-10-12 15:02:58 +03:00
|
|
|
#include "avformat.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2006-10-12 15:02:58 +03: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 02:18:37 +02:00
|
|
|
static int seq_probe(const AVProbeData *p)
|
2006-10-12 15:02:58 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-01-03 00:22:12 +02:00
|
|
|
if (p->buf_size < 258)
|
2006-10-12 15:02:58 +03: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 23:33:22 +03:00
|
|
|
if (p->buf[i])
|
2006-10-12 15:02:58 +03:00
|
|
|
return 0;
|
|
|
|
|
2007-01-03 00:22:12 +02:00
|
|
|
if(p->buf[256]==0 && p->buf[257]==0)
|
|
|
|
return 0;
|
|
|
|
|
2006-10-12 15:02:58 +03:00
|
|
|
/* only one fourth of the score since the previous check is too naive */
|
|
|
|
return AVPROBE_SCORE_MAX / 4;
|
|
|
|
}
|
|
|
|
|
2011-02-20 12:04:12 +02:00
|
|
|
static int seq_init_frame_buffers(SeqDemuxContext *seq, AVIOContext *pb)
|
2006-10-12 15:02:58 +03:00
|
|
|
{
|
|
|
|
int i, sz;
|
|
|
|
TiertexSeqFrameBuffer *seq_buffer;
|
|
|
|
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(pb, 256, SEEK_SET);
|
2006-10-12 15:02:58 +03:00
|
|
|
|
|
|
|
for (i = 0; i < SEQ_NUM_FRAME_BUFFERS; i++) {
|
2011-02-21 17:43:01 +02:00
|
|
|
sz = avio_rl16(pb);
|
2006-10-12 15:02:58 +03: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 18:21:30 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2006-10-12 15:02:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
seq->frame_buffers_count = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-20 12:04:12 +02:00
|
|
|
static int seq_fill_buffer(SeqDemuxContext *seq, AVIOContext *pb, int buffer_num, unsigned int data_offs, int data_size)
|
2006-10-12 15:02:58 +03:00
|
|
|
{
|
|
|
|
TiertexSeqFrameBuffer *seq_buffer;
|
|
|
|
|
|
|
|
if (buffer_num >= SEQ_NUM_FRAME_BUFFERS)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
seq_buffer = &seq->frame_buffers[buffer_num];
|
2008-05-28 23:30:42 +03:00
|
|
|
if (seq_buffer->fill_size + data_size > seq_buffer->data_size || data_size <= 0)
|
2006-10-12 15:02:58 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(pb, seq->current_frame_offs + data_offs, SEEK_SET);
|
2011-02-21 17:43:01 +02:00
|
|
|
if (avio_read(pb, seq_buffer->data + seq_buffer->fill_size, data_size) != data_size)
|
2007-07-19 18:23:32 +03:00
|
|
|
return AVERROR(EIO);
|
2006-10-12 15:02:58 +03:00
|
|
|
|
|
|
|
seq_buffer->fill_size += data_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-20 12:04:12 +02:00
|
|
|
static int seq_parse_frame_data(SeqDemuxContext *seq, AVIOContext *pb)
|
2006-10-12 15:02:58 +03:00
|
|
|
{
|
|
|
|
unsigned int offset_table[4], buffer_num[4];
|
|
|
|
TiertexSeqFrameBuffer *seq_buffer;
|
2006-10-21 14:17:39 +03:00
|
|
|
int i, e, err;
|
2006-10-12 15:02:58 +03:00
|
|
|
|
|
|
|
seq->current_frame_offs += SEQ_FRAME_SIZE;
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(pb, seq->current_frame_offs, SEEK_SET);
|
2006-10-12 15:02:58 +03:00
|
|
|
|
|
|
|
/* sound data */
|
2011-02-21 17:43:01 +02:00
|
|
|
seq->current_audio_data_offs = avio_rl16(pb);
|
2008-05-28 23:33:22 +03:00
|
|
|
if (seq->current_audio_data_offs) {
|
2006-10-12 15:02:58 +03:00
|
|
|
seq->current_audio_data_size = SEQ_AUDIO_BUFFER_SIZE * 2;
|
|
|
|
} else {
|
|
|
|
seq->current_audio_data_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* palette data */
|
2011-02-21 17:43:01 +02:00
|
|
|
seq->current_pal_data_offs = avio_rl16(pb);
|
2008-05-28 23:33:22 +03:00
|
|
|
if (seq->current_pal_data_offs) {
|
2006-10-12 15:02:58 +03: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 17:43:01 +02:00
|
|
|
buffer_num[i] = avio_r8(pb);
|
2006-10-12 15:02:58 +03:00
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
2011-02-21 17:43:01 +02:00
|
|
|
offset_table[i] = avio_rl16(pb);
|
2006-10-12 15:02:58 +03:00
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
2008-05-28 23:33:22 +03:00
|
|
|
if (offset_table[i]) {
|
2008-05-28 23:28:48 +03:00
|
|
|
for (e = i + 1; e < 3 && offset_table[e] == 0; e++);
|
2006-10-21 14:17:39 +03:00
|
|
|
err = seq_fill_buffer(seq, pb, buffer_num[1 + i],
|
2006-10-12 15:02:58 +03:00
|
|
|
offset_table[i],
|
|
|
|
offset_table[e] - offset_table[i]);
|
2008-05-28 23:33:22 +03:00
|
|
|
if (err)
|
2006-10-21 14:17:39 +03:00
|
|
|
return err;
|
2006-10-12 15:02:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer_num[0] != 255) {
|
2006-10-21 14:17:39 +03:00
|
|
|
if (buffer_num[0] >= SEQ_NUM_FRAME_BUFFERS)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2006-10-12 15:02:58 +03: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 09:24:52 +02: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 15:20:36 +03:00
|
|
|
static int seq_read_header(AVFormatContext *s)
|
2006-10-12 15:02:58 +03:00
|
|
|
{
|
|
|
|
int i, rc;
|
2007-04-08 23:24:16 +03:00
|
|
|
SeqDemuxContext *seq = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2006-10-12 15:02:58 +03:00
|
|
|
AVStream *st;
|
|
|
|
|
|
|
|
/* init internal buffers */
|
|
|
|
rc = seq_init_frame_buffers(seq, pb);
|
2019-06-08 09:25:27 +02:00
|
|
|
if (rc) {
|
|
|
|
seq_read_close(s);
|
2006-10-12 15:02:58 +03:00
|
|
|
return rc;
|
2019-06-08 09:25:27 +02:00
|
|
|
}
|
2006-10-12 15:02:58 +03: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 09:25:27 +02:00
|
|
|
if (rc) {
|
|
|
|
seq_read_close(s);
|
2006-10-12 15:02:58 +03:00
|
|
|
return rc;
|
2019-06-08 09:25:27 +02:00
|
|
|
}
|
2006-10-12 15:02:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
seq->current_frame_pts = 0;
|
|
|
|
|
|
|
|
seq->audio_buffer_full = 0;
|
|
|
|
|
|
|
|
/* initialize the video decoder stream */
|
2011-06-18 12:43:24 +03:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2019-06-08 09:25:27 +02:00
|
|
|
if (!st) {
|
|
|
|
seq_read_close(s);
|
2007-07-19 18:21:30 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2019-06-08 09:25:27 +02:00
|
|
|
}
|
2006-10-12 15:02:58 +03:00
|
|
|
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(st, 32, 1, SEQ_FRAME_RATE);
|
2006-10-12 15:02:58 +03: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 21:42:52 +03: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 15:02:58 +03:00
|
|
|
|
|
|
|
/* initialize the audio decoder stream */
|
2011-06-18 12:43:24 +03:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2019-06-08 09:25:27 +02:00
|
|
|
if (!st) {
|
|
|
|
seq_read_close(s);
|
2007-07-19 18:21:30 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2019-06-08 09:25:27 +02:00
|
|
|
}
|
2006-10-12 15:02:58 +03:00
|
|
|
|
2012-01-20 07:01:53 +03:00
|
|
|
st->start_time = 0;
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(st, 32, 1, SEQ_SAMPLE_RATE);
|
2006-10-12 15:02:58 +03: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 21:42:52 +03: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 15:02:58 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int seq_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int rc;
|
2007-04-08 23:24:16 +03:00
|
|
|
SeqDemuxContext *seq = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2006-10-12 15:02:58 +03: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 18:21:30 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2006-10-12 15:02:58 +03:00
|
|
|
|
|
|
|
pkt->data[0] = 0;
|
2008-05-28 23:33:22 +03:00
|
|
|
if (seq->current_pal_data_size) {
|
2006-10-12 15:02:58 +03:00
|
|
|
pkt->data[0] |= 1;
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(pb, seq->current_frame_offs + seq->current_pal_data_offs, SEEK_SET);
|
2011-02-21 17:43:01 +02:00
|
|
|
if (avio_read(pb, &pkt->data[1], seq->current_pal_data_size) != seq->current_pal_data_size)
|
2007-07-19 18:23:32 +03:00
|
|
|
return AVERROR(EIO);
|
2006-10-12 15:02:58 +03:00
|
|
|
}
|
2008-05-28 23:33:22 +03:00
|
|
|
if (seq->current_video_data_size) {
|
2006-10-12 15:02:58 +03: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 18:23:32 +03:00
|
|
|
return AVERROR(EIO);
|
2006-10-12 15:02:58 +03:00
|
|
|
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(pb, seq->current_frame_offs + seq->current_audio_data_offs, SEEK_SET);
|
2006-10-12 15:02:58 +03: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 13:00:12 +03:00
|
|
|
seq->current_frame_pts++;
|
2006-10-12 15:02:58 +03:00
|
|
|
|
|
|
|
seq->audio_buffer_full = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-26 00:03:28 +02:00
|
|
|
AVInputFormat ff_tiertexseq_demuxer = {
|
2011-07-16 23:18:12 +03:00
|
|
|
.name = "tiertexseq",
|
2012-07-25 00:51:41 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Tiertex Limited SEQ"),
|
2011-07-16 23:18:12 +03: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 15:02:58 +03:00
|
|
|
};
|