2015-10-19 14:42:56 +02:00
|
|
|
/*
|
|
|
|
* FSB demuxer
|
|
|
|
* Copyright (c) 2015 Paul B Mahol
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "avio.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int fsb_probe(const AVProbeData *p)
|
2015-10-19 14:42:56 +02:00
|
|
|
{
|
|
|
|
if (memcmp(p->buf, "FSB", 3) || p->buf[3] - '0' < 1 || p->buf[3] - '0' > 5)
|
|
|
|
return 0;
|
|
|
|
if (AV_RL32(p->buf + 4) != 1)
|
|
|
|
return 0;
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fsb_read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
unsigned format, version, c;
|
|
|
|
int64_t offset;
|
2016-04-10 21:58:15 +02:00
|
|
|
AVCodecParameters *par;
|
2015-10-19 14:42:56 +02:00
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
2019-12-10 23:59:53 +02:00
|
|
|
int ret;
|
2015-10-19 14:42:56 +02:00
|
|
|
|
|
|
|
avio_skip(pb, 3); // "FSB"
|
|
|
|
version = avio_r8(pb) - '0';
|
|
|
|
if (version != 4 && version != 3) {
|
|
|
|
avpriv_request_sample(s, "version %d", version);
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
|
|
|
|
avio_skip(pb, 4);
|
|
|
|
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
2016-04-10 21:58:15 +02:00
|
|
|
par = st->codecpar;
|
|
|
|
par->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
par->codec_tag = 0;
|
2015-10-19 14:42:56 +02:00
|
|
|
|
|
|
|
if (version == 3) {
|
|
|
|
offset = avio_rl32(pb) + 0x18;
|
|
|
|
avio_skip(pb, 44);
|
|
|
|
st->duration = avio_rl32(pb);
|
|
|
|
avio_skip(pb, 12);
|
|
|
|
format = avio_rl32(pb);
|
2016-04-10 21:58:15 +02:00
|
|
|
par->sample_rate = avio_rl32(pb);
|
|
|
|
if (par->sample_rate <= 0)
|
2015-10-19 14:42:56 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
avio_skip(pb, 6);
|
2016-04-10 21:58:15 +02:00
|
|
|
par->channels = avio_rl16(pb);
|
|
|
|
if (!par->channels)
|
2015-10-19 14:42:56 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
if (format & 0x00000100) {
|
2016-04-10 21:58:15 +02:00
|
|
|
par->codec_id = AV_CODEC_ID_PCM_S16LE;
|
|
|
|
par->block_align = 4096 * par->channels;
|
2015-10-19 14:42:56 +02:00
|
|
|
} else if (format & 0x00400000) {
|
2016-04-10 21:58:15 +02:00
|
|
|
par->bits_per_coded_sample = 4;
|
|
|
|
par->codec_id = AV_CODEC_ID_ADPCM_IMA_WAV;
|
|
|
|
par->block_align = 36 * par->channels;
|
2015-10-19 14:42:56 +02:00
|
|
|
} else if (format & 0x00800000) {
|
2016-04-10 21:58:15 +02:00
|
|
|
par->codec_id = AV_CODEC_ID_ADPCM_PSX;
|
|
|
|
par->block_align = 16 * par->channels;
|
2015-10-19 14:42:56 +02:00
|
|
|
} else if (format & 0x02000000) {
|
2016-04-10 21:58:15 +02:00
|
|
|
par->codec_id = AV_CODEC_ID_ADPCM_THP;
|
|
|
|
par->block_align = 8 * par->channels;
|
|
|
|
if (par->channels > INT_MAX / 32)
|
2015-10-19 14:42:56 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2019-12-10 23:59:53 +02:00
|
|
|
ret = ff_alloc_extradata(par, 32 * par->channels);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-10-19 14:42:56 +02:00
|
|
|
avio_seek(pb, 0x68, SEEK_SET);
|
2016-04-10 21:58:15 +02:00
|
|
|
for (c = 0; c < par->channels; c++) {
|
|
|
|
avio_read(pb, par->extradata + 32 * c, 32);
|
2015-10-19 14:42:56 +02:00
|
|
|
avio_skip(pb, 14);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
avpriv_request_sample(s, "format 0x%X", format);
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
} else if (version == 4) {
|
|
|
|
offset = avio_rl32(pb) + 0x30;
|
|
|
|
avio_skip(pb, 80);
|
|
|
|
st->duration = avio_rl32(pb);
|
|
|
|
|
|
|
|
format = avio_rb32(pb);
|
|
|
|
switch(format) {
|
|
|
|
case 0x40001001:
|
|
|
|
case 0x00001005:
|
|
|
|
case 0x40001081:
|
|
|
|
case 0x40200001:
|
2016-04-10 21:58:15 +02:00
|
|
|
par->codec_id = AV_CODEC_ID_XMA2;
|
2015-10-19 14:42:56 +02:00
|
|
|
break;
|
|
|
|
case 0x40000802:
|
2016-04-10 21:58:15 +02:00
|
|
|
par->codec_id = AV_CODEC_ID_ADPCM_THP;
|
2015-10-19 14:42:56 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
avpriv_request_sample(s, "format 0x%X", format);
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
par->sample_rate = avio_rl32(pb);
|
|
|
|
if (par->sample_rate <= 0)
|
2015-10-19 14:42:56 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
avio_skip(pb, 6);
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
par->channels = avio_rl16(pb);
|
|
|
|
if (!par->channels)
|
2015-10-19 14:42:56 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
switch (par->codec_id) {
|
2015-10-19 14:42:56 +02:00
|
|
|
case AV_CODEC_ID_XMA2:
|
2019-12-10 23:59:53 +02:00
|
|
|
ret = ff_alloc_extradata(par, 34);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-04-10 21:58:15 +02:00
|
|
|
memset(par->extradata, 0, 34);
|
|
|
|
par->block_align = 2048;
|
2015-10-19 14:42:56 +02:00
|
|
|
break;
|
|
|
|
case AV_CODEC_ID_ADPCM_THP:
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->channels > INT_MAX / 32)
|
2015-10-19 14:42:56 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2019-12-10 23:59:53 +02:00
|
|
|
ret = ff_alloc_extradata(par, 32 * par->channels);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-10-19 14:42:56 +02:00
|
|
|
avio_seek(pb, 0x80, SEEK_SET);
|
2016-04-10 21:58:15 +02:00
|
|
|
for (c = 0; c < par->channels; c++) {
|
|
|
|
avio_read(pb, par->extradata + 32 * c, 32);
|
2015-10-19 14:42:56 +02:00
|
|
|
avio_skip(pb, 14);
|
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
par->block_align = 8 * par->channels;
|
2015-10-19 14:42:56 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
av_assert0(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
avio_skip(pb, offset - avio_tell(pb));
|
|
|
|
s->internal->data_offset = avio_tell(pb);
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
avpriv_set_pts_info(st, 64, 1, par->sample_rate);
|
2015-10-19 14:42:56 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fsb_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2016-04-10 21:58:15 +02:00
|
|
|
AVCodecParameters *par = s->streams[0]->codecpar;
|
2015-10-19 14:42:56 +02:00
|
|
|
int64_t pos;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (avio_feof(s->pb))
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
pos = avio_tell(s->pb);
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->codec_id == AV_CODEC_ID_ADPCM_THP &&
|
|
|
|
par->channels > 1) {
|
2015-10-19 14:42:56 +02:00
|
|
|
int i, ch;
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
ret = av_new_packet(pkt, par->block_align);
|
2015-10-19 14:42:56 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
for (i = 0; i < 4; i++) {
|
2016-04-10 21:58:15 +02:00
|
|
|
for (ch = 0; ch < par->channels; ch++) {
|
2015-10-19 14:42:56 +02:00
|
|
|
pkt->data[ch * 8 + i * 2 + 0] = avio_r8(s->pb);
|
|
|
|
pkt->data[ch * 8 + i * 2 + 1] = avio_r8(s->pb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
2016-04-10 21:58:15 +02:00
|
|
|
ret = av_get_packet(s->pb, pkt, par->block_align);
|
2015-10-19 14:42:56 +02:00
|
|
|
}
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->codec_id == AV_CODEC_ID_XMA2 && pkt->size >= 1)
|
2015-10-19 14:42:56 +02:00
|
|
|
pkt->duration = (pkt->data[0] >> 2) * 512;
|
|
|
|
|
|
|
|
pkt->pos = pos;
|
|
|
|
pkt->stream_index = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:45:24 +02:00
|
|
|
const AVInputFormat ff_fsb_demuxer = {
|
2015-10-19 14:42:56 +02:00
|
|
|
.name = "fsb",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("FMOD Sample Bank"),
|
|
|
|
.read_probe = fsb_probe,
|
|
|
|
.read_header = fsb_read_header,
|
|
|
|
.read_packet = fsb_read_packet,
|
|
|
|
.extensions = "fsb",
|
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
|
|
|
};
|