2007-10-22 09:01:38 +03:00
|
|
|
/*
|
|
|
|
* Beam Software SIFF demuxer
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2007 Konstantin Shishkov
|
2007-10-22 09:01:38 +03:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2012-04-08 01:36:08 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2009-01-12 00:19:48 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2015-02-03 10:52:59 +02:00
|
|
|
|
2007-10-22 09:01:38 +03:00
|
|
|
#include "avformat.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2011-12-19 21:23:56 +03:00
|
|
|
#include "avio_internal.h"
|
2007-10-22 09:01:38 +03:00
|
|
|
|
2015-02-03 10:52:59 +02:00
|
|
|
enum SIFFTags {
|
2007-10-22 09:01:38 +03:00
|
|
|
TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
|
|
|
|
TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
|
|
|
|
TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
|
|
|
|
TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
|
|
|
|
TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
|
|
|
|
TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
|
|
|
|
};
|
|
|
|
|
2015-02-03 10:52:59 +02:00
|
|
|
enum VBFlags {
|
2007-10-22 09:01:38 +03:00
|
|
|
VB_HAS_GMC = 0x01,
|
|
|
|
VB_HAS_AUDIO = 0x04,
|
|
|
|
VB_HAS_VIDEO = 0x08,
|
|
|
|
VB_HAS_PALETTE = 0x10,
|
|
|
|
VB_HAS_LENGTH = 0x20
|
|
|
|
};
|
|
|
|
|
2015-02-03 10:52:59 +02:00
|
|
|
typedef struct SIFFContext {
|
2007-10-22 09:01:38 +03:00
|
|
|
int frames;
|
|
|
|
int cur_frame;
|
|
|
|
int rate;
|
|
|
|
int bits;
|
|
|
|
int block_align;
|
|
|
|
|
|
|
|
int has_video;
|
|
|
|
int has_audio;
|
|
|
|
|
|
|
|
int curstrm;
|
2015-03-09 01:59:58 +02:00
|
|
|
unsigned int pktsize;
|
2007-10-22 09:01:38 +03:00
|
|
|
int gmcsize;
|
2015-03-09 22:05:29 +02:00
|
|
|
unsigned int sndsize;
|
2007-10-22 09:01:38 +03:00
|
|
|
|
2015-03-09 01:59:58 +02:00
|
|
|
unsigned int flags;
|
2007-10-22 09:01:38 +03:00
|
|
|
uint8_t gmc[4];
|
2015-02-03 10:52:59 +02:00
|
|
|
} SIFFContext;
|
2007-10-22 09:01:38 +03:00
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int siff_probe(const AVProbeData *p)
|
2007-10-22 09:01:38 +03:00
|
|
|
{
|
2009-09-17 21:09:20 +03:00
|
|
|
uint32_t tag = AV_RL32(p->buf + 8);
|
2007-10-22 09:01:38 +03:00
|
|
|
/* check file header */
|
2009-09-17 21:09:20 +03:00
|
|
|
if (AV_RL32(p->buf) != TAG_SIFF ||
|
|
|
|
(tag != TAG_VBV1 && tag != TAG_SOUN))
|
2007-10-22 09:01:38 +03:00
|
|
|
return 0;
|
2009-09-17 21:09:20 +03:00
|
|
|
return AVPROBE_SCORE_MAX;
|
2007-10-22 09:01:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
|
|
|
|
{
|
|
|
|
AVStream *ast;
|
2011-06-18 12:43:24 +03:00
|
|
|
ast = avformat_new_stream(s, NULL);
|
2007-10-22 09:01:38 +03:00
|
|
|
if (!ast)
|
2012-12-06 21:00:37 +03:00
|
|
|
return AVERROR(ENOMEM);
|
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
|
|
|
ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
|
|
|
|
ast->codecpar->channels = 1;
|
|
|
|
ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
|
|
|
|
ast->codecpar->bits_per_coded_sample = 8;
|
|
|
|
ast->codecpar->sample_rate = c->rate;
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(ast, 16, 1, c->rate);
|
2015-02-03 10:52:59 +02:00
|
|
|
ast->start_time = 0;
|
2007-10-22 09:01:38 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-20 12:04:12 +02:00
|
|
|
static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
|
2007-10-22 09:01:38 +03:00
|
|
|
{
|
|
|
|
AVStream *st;
|
|
|
|
int width, height;
|
|
|
|
|
2015-02-03 10:52:59 +02:00
|
|
|
if (avio_rl32(pb) != TAG_VBHD) {
|
2007-10-22 09:01:38 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
|
2012-12-06 21:00:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-10-22 09:01:38 +03:00
|
|
|
}
|
2015-02-03 10:52:59 +02:00
|
|
|
if (avio_rb32(pb) != 32) {
|
2007-10-22 09:01:38 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
|
2012-12-06 21:00:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-10-22 09:01:38 +03:00
|
|
|
}
|
2015-02-03 10:52:59 +02:00
|
|
|
if (avio_rl16(pb) != 1) {
|
2007-10-22 09:01:38 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
|
2012-12-06 21:00:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-10-22 09:01:38 +03:00
|
|
|
}
|
2015-02-03 10:52:59 +02:00
|
|
|
width = avio_rl16(pb);
|
2011-02-21 17:43:01 +02:00
|
|
|
height = avio_rl16(pb);
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 4);
|
2011-02-21 17:43:01 +02:00
|
|
|
c->frames = avio_rl16(pb);
|
2015-02-03 10:52:59 +02:00
|
|
|
if (!c->frames) {
|
2007-10-22 09:01:38 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
|
2012-12-06 21:00:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-10-22 09:01:38 +03:00
|
|
|
}
|
2015-02-03 10:52:59 +02:00
|
|
|
c->bits = avio_rl16(pb);
|
|
|
|
c->rate = avio_rl16(pb);
|
2007-10-22 09:01:38 +03:00
|
|
|
c->block_align = c->rate * (c->bits >> 3);
|
|
|
|
|
2015-02-03 10:52:59 +02:00
|
|
|
avio_skip(pb, 16); // zeroes
|
2007-10-22 09:01:38 +03:00
|
|
|
|
2011-06-18 12:43:24 +03:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2007-10-22 09:01:38 +03:00
|
|
|
if (!st)
|
2012-12-06 21:00:37 +03:00
|
|
|
return AVERROR(ENOMEM);
|
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_VB;
|
|
|
|
st->codecpar->codec_tag = MKTAG('V', 'B', 'V', '1');
|
|
|
|
st->codecpar->width = width;
|
|
|
|
st->codecpar->height = height;
|
|
|
|
st->codecpar->format = AV_PIX_FMT_PAL8;
|
2016-04-10 21:58:15 +02:00
|
|
|
st->nb_frames =
|
|
|
|
st->duration = c->frames;
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(st, 16, 1, 12);
|
2007-10-22 09:01:38 +03:00
|
|
|
|
|
|
|
c->cur_frame = 0;
|
|
|
|
c->has_video = 1;
|
|
|
|
c->has_audio = !!c->rate;
|
2015-02-03 10:52:59 +02:00
|
|
|
c->curstrm = -1;
|
2015-02-03 10:53:00 +02:00
|
|
|
if (c->has_audio)
|
|
|
|
return create_audio_stream(s, c);
|
2007-10-22 09:01:38 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-20 12:04:12 +02:00
|
|
|
static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
|
2007-10-22 09:01:38 +03:00
|
|
|
{
|
2015-02-03 10:52:59 +02:00
|
|
|
if (avio_rl32(pb) != TAG_SHDR) {
|
2007-10-22 09:01:38 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
|
2012-12-06 21:00:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-10-22 09:01:38 +03:00
|
|
|
}
|
2015-02-03 10:52:59 +02:00
|
|
|
if (avio_rb32(pb) != 8) {
|
2007-10-22 09:01:38 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
|
2012-12-06 21:00:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-10-22 09:01:38 +03:00
|
|
|
}
|
2015-02-03 10:52:59 +02:00
|
|
|
avio_skip(pb, 4); // unknown value
|
|
|
|
c->rate = avio_rl16(pb);
|
|
|
|
c->bits = avio_rl16(pb);
|
2007-10-22 09:01:38 +03:00
|
|
|
c->block_align = c->rate * (c->bits >> 3);
|
|
|
|
return create_audio_stream(s, c);
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int siff_read_header(AVFormatContext *s)
|
2007-10-22 09:01:38 +03:00
|
|
|
{
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2015-02-03 10:52:59 +02:00
|
|
|
SIFFContext *c = s->priv_data;
|
2007-10-22 09:01:38 +03:00
|
|
|
uint32_t tag;
|
2012-12-06 21:00:37 +03:00
|
|
|
int ret;
|
2007-10-22 09:01:38 +03:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
if (avio_rl32(pb) != TAG_SIFF)
|
2012-12-06 21:00:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2015-02-03 10:52:59 +02:00
|
|
|
avio_skip(pb, 4); // ignore size
|
2011-02-21 17:43:01 +02:00
|
|
|
tag = avio_rl32(pb);
|
2007-10-22 09:01:38 +03:00
|
|
|
|
2015-02-03 10:52:59 +02:00
|
|
|
if (tag != TAG_VBV1 && tag != TAG_SOUN) {
|
2007-10-22 09:01:38 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
|
2012-12-06 21:00:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-10-22 09:01:38 +03:00
|
|
|
}
|
|
|
|
|
2012-12-06 21:00:37 +03:00
|
|
|
if (tag == TAG_VBV1 && (ret = siff_parse_vbv1(s, c, pb)) < 0)
|
|
|
|
return ret;
|
|
|
|
if (tag == TAG_SOUN && (ret = siff_parse_soun(s, c, pb)) < 0)
|
|
|
|
return ret;
|
2015-02-03 10:52:59 +02:00
|
|
|
if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')) {
|
2007-10-22 09:01:38 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
|
2012-12-06 21:00:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-10-22 09:01:38 +03:00
|
|
|
}
|
2015-02-03 10:52:59 +02:00
|
|
|
avio_skip(pb, 4); // ignore size
|
2007-10-22 09:01:38 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
SIFFContext *c = s->priv_data;
|
2019-12-10 23:59:53 +02:00
|
|
|
int ret;
|
2007-10-22 09:01:38 +03:00
|
|
|
|
2015-02-03 10:52:59 +02:00
|
|
|
if (c->has_video) {
|
2015-03-09 01:59:58 +02:00
|
|
|
unsigned int size;
|
2007-10-22 09:01:38 +03:00
|
|
|
if (c->cur_frame >= c->frames)
|
2012-11-21 19:28:44 +03:00
|
|
|
return AVERROR_EOF;
|
2015-02-03 10:52:59 +02:00
|
|
|
if (c->curstrm == -1) {
|
2011-02-21 17:43:01 +02:00
|
|
|
c->pktsize = avio_rl32(s->pb) - 4;
|
2015-02-03 10:52:59 +02:00
|
|
|
c->flags = avio_rl16(s->pb);
|
2020-08-11 14:41:13 +02:00
|
|
|
if (c->flags & VB_HAS_AUDIO && !c->has_audio)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2007-10-22 09:01:38 +03:00
|
|
|
c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
|
|
|
|
if (c->gmcsize)
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_read(s->pb, c->gmc, c->gmcsize);
|
2015-02-03 10:52:59 +02:00
|
|
|
c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb) : 0;
|
2007-10-22 09:01:38 +03:00
|
|
|
c->curstrm = !!(c->flags & VB_HAS_AUDIO);
|
|
|
|
}
|
|
|
|
|
2015-02-03 10:52:59 +02:00
|
|
|
if (!c->curstrm) {
|
2015-03-09 22:05:29 +02:00
|
|
|
if (c->pktsize < 2LL + c->sndsize + c->gmcsize)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2011-12-19 21:23:56 +03:00
|
|
|
size = c->pktsize - c->sndsize - c->gmcsize - 2;
|
|
|
|
size = ffio_limit(s->pb, size);
|
2019-12-10 23:59:53 +02:00
|
|
|
if ((ret = av_new_packet(pkt, size + c->gmcsize + 2)) < 0)
|
|
|
|
return ret;
|
2007-10-22 09:01:38 +03:00
|
|
|
AV_WL16(pkt->data, c->flags);
|
|
|
|
if (c->gmcsize)
|
|
|
|
memcpy(pkt->data + 2, c->gmc, c->gmcsize);
|
2013-12-29 15:20:03 +03:00
|
|
|
if (avio_read(s->pb, pkt->data + 2 + c->gmcsize, size) != size) {
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2007-10-22 09:01:38 +03:00
|
|
|
pkt->stream_index = 0;
|
2015-02-03 10:52:59 +02:00
|
|
|
c->curstrm = -1;
|
|
|
|
} else {
|
2015-03-09 01:59:58 +02:00
|
|
|
int pktsize = av_get_packet(s->pb, pkt, c->sndsize - 4);
|
|
|
|
if (pktsize < 0)
|
2007-10-22 09:01:38 +03:00
|
|
|
return AVERROR(EIO);
|
|
|
|
pkt->stream_index = 1;
|
2015-03-09 01:59:58 +02:00
|
|
|
pkt->duration = pktsize;
|
2015-02-03 10:52:59 +02:00
|
|
|
c->curstrm = 0;
|
2007-10-22 09:01:38 +03:00
|
|
|
}
|
2015-02-03 10:52:59 +02:00
|
|
|
if (!c->cur_frame || c->curstrm)
|
2010-03-31 15:29:58 +03:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2007-10-22 09:01:38 +03:00
|
|
|
if (c->curstrm == -1)
|
|
|
|
c->cur_frame++;
|
2015-02-03 10:52:59 +02:00
|
|
|
} else {
|
2015-03-09 01:59:58 +02:00
|
|
|
int pktsize = av_get_packet(s->pb, pkt, c->block_align);
|
2015-03-09 22:00:33 +02:00
|
|
|
if (!pktsize)
|
2012-11-21 19:28:44 +03:00
|
|
|
return AVERROR_EOF;
|
2015-03-09 01:59:58 +02:00
|
|
|
if (pktsize <= 0)
|
2007-10-22 09:01:38 +03:00
|
|
|
return AVERROR(EIO);
|
2015-03-09 01:59:58 +02:00
|
|
|
pkt->duration = pktsize;
|
2007-10-22 09:01:38 +03:00
|
|
|
}
|
|
|
|
return pkt->size;
|
|
|
|
}
|
|
|
|
|
2011-01-26 00:03:28 +02:00
|
|
|
AVInputFormat ff_siff_demuxer = {
|
2011-07-16 23:18:12 +03:00
|
|
|
.name = "siff",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
|
|
|
|
.priv_data_size = sizeof(SIFFContext),
|
|
|
|
.read_probe = siff_probe,
|
|
|
|
.read_header = siff_read_header,
|
|
|
|
.read_packet = siff_read_packet,
|
2012-04-06 17:50:48 +03:00
|
|
|
.extensions = "vb,son",
|
2007-10-22 09:01:38 +03:00
|
|
|
};
|