2015-10-27 19:58:23 +00:00
|
|
|
/*
|
|
|
|
* 3DO STR 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
|
|
|
|
*/
|
|
|
|
|
2020-09-17 21:19:18 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2015-10-27 19:58:23 +00:00
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int threedostr_probe(const AVProbeData *p)
|
2015-10-27 19:58:23 +00:00
|
|
|
{
|
2020-09-17 21:19:18 +00:00
|
|
|
for (int i = 0; i < p->buf_size;) {
|
|
|
|
unsigned chunk = AV_RL32(p->buf + i);
|
|
|
|
unsigned size = AV_RB32(p->buf + i + 4);
|
2015-10-27 19:58:23 +00:00
|
|
|
|
2020-09-17 21:19:18 +00:00
|
|
|
if (size < 8 || p->buf_size - i < size)
|
|
|
|
return 0;
|
2020-09-18 22:11:59 +00:00
|
|
|
i += 8;
|
2020-09-17 21:19:18 +00:00
|
|
|
size -= 8;
|
|
|
|
switch (chunk) {
|
|
|
|
case MKTAG('C','T','R','L'):
|
|
|
|
break;
|
|
|
|
case MKTAG('S','N','D','S'):
|
|
|
|
if (size < 56)
|
|
|
|
return 0;
|
|
|
|
i += 8;
|
|
|
|
if (AV_RL32(p->buf + i) != MKTAG('S','H','D','R'))
|
|
|
|
return 0;
|
|
|
|
i += 28;
|
|
|
|
|
|
|
|
if (AV_RB32(p->buf + i) <= 0)
|
|
|
|
return 0;
|
|
|
|
i += 4;
|
|
|
|
if (AV_RB32(p->buf + i) <= 0)
|
|
|
|
return 0;
|
|
|
|
i += 4;
|
|
|
|
if (AV_RL32(p->buf + i) == MKTAG('S','D','X','2'))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case MKTAG('S','H','D','R'):
|
|
|
|
if (size > 0x78) {
|
|
|
|
i += 0x78;
|
|
|
|
size -= 0x78;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
i += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-10-27 19:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int threedostr_read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
unsigned chunk, codec = 0, size, ctrl_size = -1, found_shdr = 0;
|
|
|
|
AVStream *st;
|
|
|
|
|
|
|
|
while (!avio_feof(s->pb) && !found_shdr) {
|
|
|
|
chunk = avio_rl32(s->pb);
|
|
|
|
size = avio_rb32(s->pb);
|
|
|
|
|
|
|
|
if (size < 8)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
size -= 8;
|
|
|
|
|
|
|
|
switch (chunk) {
|
|
|
|
case MKTAG('C','T','R','L'):
|
|
|
|
ctrl_size = size;
|
|
|
|
break;
|
|
|
|
case MKTAG('S','N','D','S'):
|
|
|
|
if (size < 56)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
avio_skip(s->pb, 8);
|
|
|
|
if (avio_rl32(s->pb) != MKTAG('S','H','D','R'))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
avio_skip(s->pb, 24);
|
|
|
|
|
|
|
|
st = avformat_new_stream(s, NULL);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->sample_rate = avio_rb32(s->pb);
|
|
|
|
st->codecpar->channels = avio_rb32(s->pb);
|
2021-02-08 13:29:02 +00:00
|
|
|
if (st->codecpar->channels <= 0 || st->codecpar->sample_rate <= 0)
|
2015-10-27 19:58:23 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
codec = avio_rl32(s->pb);
|
|
|
|
avio_skip(s->pb, 4);
|
|
|
|
if (ctrl_size == 20 || ctrl_size == 3 || ctrl_size == -1)
|
2016-04-10 19:58:15 +00:00
|
|
|
st->duration = (avio_rb32(s->pb) - 1) / st->codecpar->channels;
|
2015-10-27 19:58:23 +00:00
|
|
|
else
|
2016-04-10 19:58:15 +00:00
|
|
|
st->duration = avio_rb32(s->pb) * 16 / st->codecpar->channels;
|
2015-10-27 19:58:23 +00:00
|
|
|
size -= 56;
|
|
|
|
found_shdr = 1;
|
|
|
|
break;
|
|
|
|
case MKTAG('S','H','D','R'):
|
|
|
|
if (size > 0x78) {
|
|
|
|
avio_skip(s->pb, 0x74);
|
|
|
|
size -= 0x78;
|
|
|
|
if (avio_rl32(s->pb) == MKTAG('C','T','R','L') && size > 4) {
|
|
|
|
ctrl_size = avio_rb32(s->pb);
|
2015-10-30 02:02:41 +00:00
|
|
|
size -= 4;
|
2015-10-27 19:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(s, AV_LOG_DEBUG, "skipping unknown chunk: %X\n", chunk);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
avio_skip(s->pb, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (codec) {
|
|
|
|
case MKTAG('S','D','X','2'):
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_SDX2_DPCM;
|
|
|
|
st->codecpar->block_align = 1 * st->codecpar->channels;
|
2015-10-27 19:58:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
avpriv_request_sample(s, "codec %X", codec);
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
|
2016-04-10 19:58:15 +00:00
|
|
|
avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
|
2015-10-27 19:58:23 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int threedostr_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2020-07-27 03:19:18 +00:00
|
|
|
unsigned chunk, size;
|
2015-10-27 19:58:23 +00:00
|
|
|
AVStream *st = s->streams[0];
|
|
|
|
int64_t pos;
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-07-27 03:19:18 +00:00
|
|
|
while (!avio_feof(s->pb)) {
|
2015-10-27 19:58:23 +00:00
|
|
|
pos = avio_tell(s->pb);
|
|
|
|
chunk = avio_rl32(s->pb);
|
|
|
|
size = avio_rb32(s->pb);
|
|
|
|
|
|
|
|
if (!size)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (size < 8)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
size -= 8;
|
|
|
|
|
|
|
|
switch (chunk) {
|
|
|
|
case MKTAG('S','N','D','S'):
|
|
|
|
if (size <= 16)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
avio_skip(s->pb, 8);
|
|
|
|
if (avio_rl32(s->pb) != MKTAG('S','S','M','P'))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
avio_skip(s->pb, 4);
|
|
|
|
size -= 16;
|
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
|
|
|
pkt->pos = pos;
|
|
|
|
pkt->stream_index = 0;
|
2016-04-10 19:58:15 +00:00
|
|
|
pkt->duration = size / st->codecpar->channels;
|
2020-07-27 03:19:18 +00:00
|
|
|
return ret;
|
2015-10-27 19:58:23 +00:00
|
|
|
default:
|
|
|
|
av_log(s, AV_LOG_DEBUG, "skipping unknown chunk: %X\n", chunk);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
avio_skip(s->pb, size);
|
|
|
|
}
|
|
|
|
|
2020-07-27 03:19:18 +00:00
|
|
|
return AVERROR_EOF;
|
2015-10-27 19:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AVInputFormat ff_threedostr_demuxer = {
|
|
|
|
.name = "3dostr",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("3DO STR"),
|
|
|
|
.read_probe = threedostr_probe,
|
|
|
|
.read_header = threedostr_read_header,
|
|
|
|
.read_packet = threedostr_read_packet,
|
|
|
|
.extensions = "str",
|
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
|
|
|
};
|