2012-11-23 11:20:11 +00:00
|
|
|
/*
|
|
|
|
* BRSTM demuxer
|
|
|
|
* Copyright (c) 2012 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/intreadwrite.h"
|
|
|
|
#include "libavcodec/bytestream.h"
|
|
|
|
#include "avformat.h"
|
2022-05-06 16:28:08 +00:00
|
|
|
#include "demux.h"
|
2012-11-23 11:20:11 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
2020-09-12 12:45:31 +00:00
|
|
|
typedef struct BRSTMCoeffOffset {
|
|
|
|
uint8_t channel;
|
|
|
|
uint32_t offset;
|
|
|
|
} BRSTMCoeffOffset;
|
|
|
|
|
2012-11-23 11:20:11 +00:00
|
|
|
typedef struct BRSTMDemuxContext {
|
|
|
|
uint32_t block_size;
|
|
|
|
uint32_t block_count;
|
|
|
|
uint32_t current_block;
|
|
|
|
uint32_t samples_per_block;
|
|
|
|
uint32_t last_block_used_bytes;
|
2015-06-20 10:01:20 +00:00
|
|
|
uint32_t last_block_size;
|
|
|
|
uint32_t last_block_samples;
|
2015-06-20 10:01:21 +00:00
|
|
|
uint32_t data_start;
|
2020-09-12 12:49:15 +00:00
|
|
|
uint8_t table[256 * 32];
|
2012-11-23 11:20:11 +00:00
|
|
|
uint8_t *adpc;
|
2020-09-12 12:45:31 +00:00
|
|
|
BRSTMCoeffOffset offsets[256];
|
2015-06-20 10:01:15 +00:00
|
|
|
int little_endian;
|
2012-11-23 11:20:11 +00:00
|
|
|
} BRSTMDemuxContext;
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int probe(const AVProbeData *p)
|
2012-11-23 11:20:11 +00:00
|
|
|
{
|
|
|
|
if (AV_RL32(p->buf) == MKTAG('R','S','T','M') &&
|
|
|
|
(AV_RL16(p->buf + 4) == 0xFFFE ||
|
|
|
|
AV_RL16(p->buf + 4) == 0xFEFF))
|
|
|
|
return AVPROBE_SCORE_MAX / 3 * 2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int probe_bfstm(const AVProbeData *p)
|
2015-06-17 08:09:52 +00:00
|
|
|
{
|
2015-06-20 10:01:15 +00:00
|
|
|
if ((AV_RL32(p->buf) == MKTAG('F','S','T','M') ||
|
|
|
|
AV_RL32(p->buf) == MKTAG('C','S','T','M')) &&
|
2015-06-17 08:09:52 +00:00
|
|
|
(AV_RL16(p->buf + 4) == 0xFFFE ||
|
|
|
|
AV_RL16(p->buf + 4) == 0xFEFF))
|
|
|
|
return AVPROBE_SCORE_MAX / 3 * 2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-23 11:20:11 +00:00
|
|
|
static int read_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
BRSTMDemuxContext *b = s->priv_data;
|
|
|
|
|
|
|
|
av_freep(&b->adpc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-12 12:45:31 +00:00
|
|
|
static int sort_offsets(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const BRSTMCoeffOffset *s1 = a;
|
|
|
|
const BRSTMCoeffOffset *s2 = b;
|
|
|
|
return FFDIFFSIGN(s1->offset, s2->offset);
|
|
|
|
}
|
|
|
|
|
2015-06-20 10:01:15 +00:00
|
|
|
static av_always_inline unsigned int read16(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
BRSTMDemuxContext *b = s->priv_data;
|
|
|
|
if (b->little_endian)
|
|
|
|
return avio_rl16(s->pb);
|
|
|
|
else
|
|
|
|
return avio_rb16(s->pb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_always_inline unsigned int read32(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
BRSTMDemuxContext *b = s->priv_data;
|
|
|
|
if (b->little_endian)
|
|
|
|
return avio_rl32(s->pb);
|
|
|
|
else
|
|
|
|
return avio_rb32(s->pb);
|
|
|
|
}
|
|
|
|
|
2012-11-23 11:20:11 +00:00
|
|
|
static int read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
BRSTMDemuxContext *b = s->priv_data;
|
|
|
|
int bom, major, minor, codec, chunk;
|
2015-06-20 10:01:20 +00:00
|
|
|
int64_t h1offset, pos, toffset;
|
|
|
|
uint32_t size, asize, start = 0;
|
2012-11-23 11:20:11 +00:00
|
|
|
AVStream *st;
|
2015-06-23 17:35:33 +00:00
|
|
|
int loop = 0;
|
2015-06-20 10:01:19 +00:00
|
|
|
int bfstm = !strcmp("bfstm", s->iformat->name);
|
2015-06-17 08:09:52 +00:00
|
|
|
|
2012-11-23 11:20:11 +00:00
|
|
|
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;
|
2012-11-23 11:20:11 +00:00
|
|
|
|
|
|
|
avio_skip(s->pb, 4);
|
|
|
|
|
|
|
|
bom = avio_rb16(s->pb);
|
|
|
|
if (bom != 0xFEFF && bom != 0xFFFE) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "invalid byte order: %X\n", bom);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2015-06-20 10:01:15 +00:00
|
|
|
|
|
|
|
if (bom == 0xFFFE)
|
|
|
|
b->little_endian = 1;
|
2012-11-23 11:20:11 +00:00
|
|
|
|
2015-06-20 10:01:19 +00:00
|
|
|
if (!bfstm) {
|
2015-06-17 08:09:52 +00:00
|
|
|
major = avio_r8(s->pb);
|
|
|
|
minor = avio_r8(s->pb);
|
|
|
|
avio_skip(s->pb, 4); // size of file
|
2015-06-20 10:01:15 +00:00
|
|
|
size = read16(s);
|
2015-06-17 08:09:52 +00:00
|
|
|
if (size < 14)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
avio_skip(s->pb, size - 14);
|
|
|
|
pos = avio_tell(s->pb);
|
|
|
|
if (avio_rl32(s->pb) != MKTAG('H','E','A','D'))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
} else {
|
2015-07-10 14:35:44 +00:00
|
|
|
uint32_t info_offset = 0;
|
2015-06-17 08:09:52 +00:00
|
|
|
uint16_t section_count, header_size, i;
|
|
|
|
|
2015-06-20 10:01:15 +00:00
|
|
|
header_size = read16(s); // 6
|
2015-06-17 08:09:52 +00:00
|
|
|
|
|
|
|
avio_skip(s->pb, 4); // Unknown constant 0x00030000
|
|
|
|
avio_skip(s->pb, 4); // size of file
|
2015-06-20 10:01:15 +00:00
|
|
|
section_count = read16(s);
|
2015-06-17 08:09:52 +00:00
|
|
|
avio_skip(s->pb, 2); // padding
|
|
|
|
for (i = 0; avio_tell(s->pb) < header_size
|
2015-06-20 10:01:20 +00:00
|
|
|
&& !(start && info_offset)
|
2015-06-17 08:09:52 +00:00
|
|
|
&& i < section_count; i++) {
|
2015-06-20 10:01:15 +00:00
|
|
|
uint16_t flag = read16(s);
|
|
|
|
avio_skip(s->pb, 2);
|
2015-06-17 08:09:52 +00:00
|
|
|
switch (flag) {
|
2015-06-20 10:01:15 +00:00
|
|
|
case 0x4000:
|
|
|
|
info_offset = read32(s);
|
2015-07-10 14:35:44 +00:00
|
|
|
/*info_size =*/ read32(s);
|
2015-06-17 08:09:52 +00:00
|
|
|
break;
|
2015-06-20 10:01:15 +00:00
|
|
|
case 0x4001:
|
2015-06-17 08:09:52 +00:00
|
|
|
avio_skip(s->pb, 4); // seek offset
|
|
|
|
avio_skip(s->pb, 4); // seek size
|
|
|
|
break;
|
2015-06-20 10:01:15 +00:00
|
|
|
case 0x4002:
|
2015-06-20 10:01:20 +00:00
|
|
|
start = read32(s) + 8;
|
2015-06-20 10:01:15 +00:00
|
|
|
avio_skip(s->pb, 4); //data_size = read32(s);
|
2015-06-17 08:09:52 +00:00
|
|
|
break;
|
2015-06-20 10:01:15 +00:00
|
|
|
case 0x4003:
|
2015-06-17 08:09:52 +00:00
|
|
|
avio_skip(s->pb, 4); // REGN offset
|
|
|
|
avio_skip(s->pb, 4); // REGN size
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-20 10:01:20 +00:00
|
|
|
if (!info_offset || !start)
|
2015-06-17 08:09:52 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
avio_skip(s->pb, info_offset - avio_tell(s->pb));
|
|
|
|
pos = avio_tell(s->pb);
|
|
|
|
if (avio_rl32(s->pb) != MKTAG('I','N','F','O'))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2012-11-23 11:20:11 +00:00
|
|
|
|
2015-06-20 10:01:15 +00:00
|
|
|
size = read32(s);
|
2016-04-03 17:12:07 +00:00
|
|
|
if (size < 40)
|
2012-11-23 11:20:11 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
avio_skip(s->pb, 4); // unknown
|
2015-06-20 10:01:15 +00:00
|
|
|
h1offset = read32(s);
|
2012-11-23 11:20:11 +00:00
|
|
|
if (h1offset > size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
avio_skip(s->pb, 12);
|
2015-06-20 10:01:15 +00:00
|
|
|
toffset = read32(s) + 16LL;
|
2012-11-23 11:20:11 +00:00
|
|
|
if (toffset > size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
avio_skip(s->pb, pos + h1offset + 8 - avio_tell(s->pb));
|
|
|
|
codec = avio_r8(s->pb);
|
|
|
|
|
|
|
|
switch (codec) {
|
|
|
|
case 0: codec = AV_CODEC_ID_PCM_S8_PLANAR; break;
|
2015-06-21 10:21:20 +00:00
|
|
|
case 1: codec = b->little_endian ?
|
|
|
|
AV_CODEC_ID_PCM_S16LE_PLANAR :
|
|
|
|
AV_CODEC_ID_PCM_S16BE_PLANAR; break;
|
2015-06-20 10:01:15 +00:00
|
|
|
case 2: codec = b->little_endian ?
|
|
|
|
AV_CODEC_ID_ADPCM_THP_LE :
|
|
|
|
AV_CODEC_ID_ADPCM_THP; break;
|
2012-11-23 11:20:11 +00:00
|
|
|
default:
|
2013-03-15 17:13:52 +00:00
|
|
|
avpriv_request_sample(s, "codec %d", codec);
|
2012-11-23 11:20:11 +00:00
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
|
2015-06-23 17:35:33 +00:00
|
|
|
loop = avio_r8(s->pb); // loop flag
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->codec_id = codec;
|
2019-05-13 09:11:26 +00:00
|
|
|
st->codecpar->ch_layout.nb_channels = avio_r8(s->pb);
|
|
|
|
if (!st->codecpar->ch_layout.nb_channels)
|
2012-11-23 11:20:11 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
avio_skip(s->pb, 1); // padding
|
2015-06-17 08:09:52 +00:00
|
|
|
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->sample_rate = bfstm ? read32(s) : read16(s);
|
|
|
|
if (st->codecpar->sample_rate <= 0)
|
2012-11-23 11:20:11 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2015-06-20 10:01:19 +00:00
|
|
|
if (!bfstm)
|
2015-06-17 08:09:52 +00:00
|
|
|
avio_skip(s->pb, 2); // padding
|
2015-06-23 17:35:33 +00:00
|
|
|
|
|
|
|
if (loop) {
|
|
|
|
if (av_dict_set_int(&s->metadata, "loop_start",
|
|
|
|
av_rescale(read32(s), AV_TIME_BASE,
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->sample_rate),
|
2015-06-23 17:35:33 +00:00
|
|
|
0) < 0)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
} else {
|
|
|
|
avio_skip(s->pb, 4);
|
|
|
|
}
|
|
|
|
|
2012-11-23 11:20:11 +00:00
|
|
|
st->start_time = 0;
|
2015-06-20 10:01:15 +00:00
|
|
|
st->duration = read32(s);
|
2016-04-10 19:58:15 +00:00
|
|
|
avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
|
2012-11-23 11:20:11 +00:00
|
|
|
|
2015-06-20 10:01:19 +00:00
|
|
|
if (!bfstm)
|
2015-06-20 10:01:15 +00:00
|
|
|
start = read32(s);
|
2012-11-23 11:20:11 +00:00
|
|
|
b->current_block = 0;
|
2015-06-20 10:01:15 +00:00
|
|
|
b->block_count = read32(s);
|
2012-11-23 11:20:11 +00:00
|
|
|
if (b->block_count > UINT16_MAX) {
|
2017-03-27 19:31:46 +00:00
|
|
|
av_log(s, AV_LOG_WARNING, "too many blocks: %"PRIu32"\n", b->block_count);
|
2012-11-23 11:20:11 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2015-06-20 10:01:15 +00:00
|
|
|
b->block_size = read32(s);
|
2019-05-13 09:11:26 +00:00
|
|
|
if (b->block_size > UINT32_MAX / st->codecpar->ch_layout.nb_channels)
|
2012-11-23 11:20:11 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2015-06-20 10:01:15 +00:00
|
|
|
b->samples_per_block = read32(s);
|
|
|
|
b->last_block_used_bytes = read32(s);
|
2015-06-20 10:01:20 +00:00
|
|
|
b->last_block_samples = read32(s);
|
|
|
|
b->last_block_size = read32(s);
|
2019-05-13 09:11:26 +00:00
|
|
|
if (b->last_block_size > UINT32_MAX / st->codecpar->ch_layout.nb_channels)
|
2015-06-20 10:01:20 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
if (b->last_block_used_bytes > b->last_block_size)
|
2012-11-23 11:20:11 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
|
2015-06-20 10:01:15 +00:00
|
|
|
if (codec == AV_CODEC_ID_ADPCM_THP || codec == AV_CODEC_ID_ADPCM_THP_LE) {
|
2012-11-23 11:20:11 +00:00
|
|
|
int ch;
|
|
|
|
|
|
|
|
avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
|
2015-06-20 10:01:19 +00:00
|
|
|
if (!bfstm)
|
2015-06-20 10:01:15 +00:00
|
|
|
toffset = read32(s) + 16LL;
|
2015-06-17 08:09:52 +00:00
|
|
|
else
|
2019-05-13 09:11:26 +00:00
|
|
|
toffset = toffset + read32(s) + st->codecpar->ch_layout.nb_channels * 8 - 8;
|
2012-11-23 11:20:11 +00:00
|
|
|
if (toffset > size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2020-09-12 12:45:31 +00:00
|
|
|
if (!bfstm) {
|
2019-05-13 09:11:26 +00:00
|
|
|
avio_skip(s->pb, pos + toffset - avio_tell(s->pb) - 8LL * (st->codecpar->ch_layout.nb_channels + 1));
|
|
|
|
for (ch = 0; ch < st->codecpar->ch_layout.nb_channels; ch++) {
|
2020-09-12 12:45:31 +00:00
|
|
|
avio_skip(s->pb, 4);
|
|
|
|
b->offsets[ch].channel = ch;
|
|
|
|
b->offsets[ch].offset = read32(s);
|
|
|
|
}
|
|
|
|
|
2019-05-13 09:11:26 +00:00
|
|
|
qsort(b->offsets, st->codecpar->ch_layout.nb_channels, sizeof(*b->offsets), sort_offsets);
|
2020-09-12 12:45:31 +00:00
|
|
|
}
|
|
|
|
|
2012-11-23 11:20:11 +00:00
|
|
|
avio_skip(s->pb, pos + toffset - avio_tell(s->pb));
|
|
|
|
|
2019-05-13 09:11:26 +00:00
|
|
|
for (ch = 0; ch < st->codecpar->ch_layout.nb_channels; ch++) {
|
2020-09-12 12:45:31 +00:00
|
|
|
if (!bfstm)
|
|
|
|
avio_skip(s->pb, pos + 16LL + b->offsets[ch].offset - avio_tell(s->pb));
|
|
|
|
|
2020-03-21 17:31:06 +00:00
|
|
|
if (avio_read(s->pb, b->table + ch * 32, 32) != 32)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2020-09-12 12:45:31 +00:00
|
|
|
|
|
|
|
if (bfstm)
|
|
|
|
avio_skip(s->pb, 14);
|
2012-11-23 11:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 17:31:06 +00:00
|
|
|
if (size < (avio_tell(s->pb) - pos))
|
|
|
|
return AVERROR_INVALIDDATA;
|
2015-06-17 08:09:52 +00:00
|
|
|
|
2015-06-20 10:01:19 +00:00
|
|
|
avio_skip(s->pb, size - (avio_tell(s->pb) - pos));
|
2012-11-23 11:20:11 +00:00
|
|
|
|
2014-08-07 20:12:41 +00:00
|
|
|
while (!avio_feof(s->pb)) {
|
2012-11-23 11:20:11 +00:00
|
|
|
chunk = avio_rl32(s->pb);
|
2015-06-20 10:01:15 +00:00
|
|
|
size = read32(s);
|
2020-03-21 17:31:06 +00:00
|
|
|
if (size < 8)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-11-23 11:20:11 +00:00
|
|
|
size -= 8;
|
|
|
|
switch (chunk) {
|
2015-06-20 10:01:20 +00:00
|
|
|
case MKTAG('S','E','E','K'):
|
2012-11-23 11:20:11 +00:00
|
|
|
case MKTAG('A','D','P','C'):
|
2015-06-20 10:01:15 +00:00
|
|
|
if (codec != AV_CODEC_ID_ADPCM_THP &&
|
|
|
|
codec != AV_CODEC_ID_ADPCM_THP_LE)
|
2012-11-23 11:20:11 +00:00
|
|
|
goto skip;
|
|
|
|
|
2019-05-13 09:11:26 +00:00
|
|
|
asize = b->block_count * st->codecpar->ch_layout.nb_channels * 4;
|
2020-03-21 17:31:06 +00:00
|
|
|
if (size < asize)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-11-23 11:20:11 +00:00
|
|
|
if (b->adpc) {
|
2014-06-03 02:46:04 +00:00
|
|
|
av_log(s, AV_LOG_WARNING, "skipping additional ADPC chunk\n");
|
2012-11-23 11:20:11 +00:00
|
|
|
goto skip;
|
|
|
|
} else {
|
|
|
|
b->adpc = av_mallocz(asize);
|
2020-03-21 17:31:06 +00:00
|
|
|
if (!b->adpc)
|
|
|
|
return AVERROR(ENOMEM);
|
2015-06-20 10:01:26 +00:00
|
|
|
if (bfstm && codec != AV_CODEC_ID_ADPCM_THP_LE) {
|
|
|
|
// Big-endian BFSTMs have little-endian SEEK tables
|
|
|
|
// for some strange reason.
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < asize; i += 2) {
|
|
|
|
b->adpc[i+1] = avio_r8(s->pb);
|
|
|
|
b->adpc[i] = avio_r8(s->pb);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
avio_read(s->pb, b->adpc, asize);
|
|
|
|
}
|
2012-11-23 11:20:11 +00:00
|
|
|
avio_skip(s->pb, size - asize);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MKTAG('D','A','T','A'):
|
|
|
|
if ((start < avio_tell(s->pb)) ||
|
2015-06-20 10:01:15 +00:00
|
|
|
(!b->adpc && (codec == AV_CODEC_ID_ADPCM_THP ||
|
2020-03-21 17:31:06 +00:00
|
|
|
codec == AV_CODEC_ID_ADPCM_THP_LE)))
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-11-23 11:20:11 +00:00
|
|
|
avio_skip(s->pb, start - avio_tell(s->pb));
|
2012-12-16 11:57:43 +00:00
|
|
|
|
2015-06-20 10:01:20 +00:00
|
|
|
if (bfstm && (codec == AV_CODEC_ID_ADPCM_THP ||
|
|
|
|
codec == AV_CODEC_ID_ADPCM_THP_LE))
|
|
|
|
avio_skip(s->pb, 24);
|
|
|
|
|
2015-06-20 10:01:21 +00:00
|
|
|
b->data_start = avio_tell(s->pb);
|
|
|
|
|
2015-06-24 17:16:41 +00:00
|
|
|
if (!bfstm && (major != 1 || minor))
|
2013-03-15 17:13:52 +00:00
|
|
|
avpriv_request_sample(s, "Version %d.%d", major, minor);
|
2012-12-16 11:57:43 +00:00
|
|
|
|
2012-11-23 11:20:11 +00:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
av_log(s, AV_LOG_WARNING, "skipping unknown chunk: %X\n", chunk);
|
|
|
|
skip:
|
|
|
|
avio_skip(s->pb, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 17:31:06 +00:00
|
|
|
return AVERROR_EOF;
|
2012-11-23 11:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2016-04-10 19:58:15 +00:00
|
|
|
AVCodecParameters *par = s->streams[0]->codecpar;
|
2012-11-23 11:20:11 +00:00
|
|
|
BRSTMDemuxContext *b = s->priv_data;
|
2015-06-20 10:01:20 +00:00
|
|
|
uint32_t samples, size, skip = 0;
|
2019-05-13 09:11:26 +00:00
|
|
|
int channels = par->ch_layout.nb_channels;
|
2015-06-20 10:01:20 +00:00
|
|
|
int ret, i;
|
2012-11-23 11:20:11 +00:00
|
|
|
|
2014-08-07 20:12:41 +00:00
|
|
|
if (avio_feof(s->pb))
|
2012-11-23 11:20:11 +00:00
|
|
|
return AVERROR_EOF;
|
|
|
|
b->current_block++;
|
|
|
|
if (b->current_block == b->block_count) {
|
|
|
|
size = b->last_block_used_bytes;
|
2015-06-20 10:01:20 +00:00
|
|
|
samples = b->last_block_samples;
|
|
|
|
skip = b->last_block_size - b->last_block_used_bytes;
|
2015-06-23 17:35:35 +00:00
|
|
|
|
|
|
|
if (samples < size * 14 / 8) {
|
|
|
|
uint32_t adjusted_size = samples / 14 * 8;
|
|
|
|
if (samples % 14)
|
|
|
|
adjusted_size += (samples % 14 + 1) / 2 + 1;
|
|
|
|
|
|
|
|
skip += size - adjusted_size;
|
|
|
|
size = adjusted_size;
|
|
|
|
}
|
2012-11-23 11:20:11 +00:00
|
|
|
} else if (b->current_block < b->block_count) {
|
|
|
|
size = b->block_size;
|
|
|
|
samples = b->samples_per_block;
|
|
|
|
} else {
|
|
|
|
return AVERROR_EOF;
|
|
|
|
}
|
|
|
|
|
2016-04-10 19:58:15 +00:00
|
|
|
if (par->codec_id == AV_CODEC_ID_ADPCM_THP ||
|
|
|
|
par->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
|
2012-11-23 11:20:11 +00:00
|
|
|
uint8_t *dst;
|
|
|
|
|
2016-01-04 11:57:38 +00:00
|
|
|
if (!b->adpc) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "adpcm_thp requires ADPC chunk, but none was found.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
2016-01-04 12:44:16 +00:00
|
|
|
}
|
2016-01-04 11:57:38 +00:00
|
|
|
|
2015-09-23 17:07:48 +00:00
|
|
|
if (size > (INT_MAX - 32 - 4) ||
|
2019-05-13 09:11:26 +00:00
|
|
|
(32 + 4 + size) > (INT_MAX / channels) ||
|
|
|
|
(32 + 4 + size) * channels > INT_MAX - 8)
|
2015-09-23 17:07:48 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2019-05-13 09:11:26 +00:00
|
|
|
if ((ret = av_new_packet(pkt, 8 + (32 + 4 + size) * channels)) < 0)
|
2019-12-10 21:59:53 +00:00
|
|
|
return ret;
|
2012-11-23 11:20:11 +00:00
|
|
|
dst = pkt->data;
|
2016-04-10 19:58:15 +00:00
|
|
|
if (par->codec_id == AV_CODEC_ID_ADPCM_THP_LE) {
|
2019-05-13 09:11:26 +00:00
|
|
|
bytestream_put_le32(&dst, size * channels);
|
2015-06-20 10:01:20 +00:00
|
|
|
bytestream_put_le32(&dst, samples);
|
|
|
|
} else {
|
2019-05-13 09:11:26 +00:00
|
|
|
bytestream_put_be32(&dst, size * channels);
|
2015-06-20 10:01:20 +00:00
|
|
|
bytestream_put_be32(&dst, samples);
|
|
|
|
}
|
2019-05-13 09:11:26 +00:00
|
|
|
bytestream_put_buffer(&dst, b->table, 32 * channels);
|
|
|
|
bytestream_put_buffer(&dst, b->adpc + 4 * channels *
|
|
|
|
(b->current_block - 1), 4 * channels);
|
2012-11-23 11:20:11 +00:00
|
|
|
|
2019-05-13 09:11:26 +00:00
|
|
|
for (i = 0; i < channels; i++) {
|
2015-06-20 10:01:20 +00:00
|
|
|
ret = avio_read(s->pb, dst, size);
|
|
|
|
dst += size;
|
|
|
|
avio_skip(s->pb, skip);
|
|
|
|
if (ret != size) {
|
2020-01-07 13:55:40 +00:00
|
|
|
return AVERROR(EIO);
|
2015-06-20 10:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-23 11:20:11 +00:00
|
|
|
pkt->duration = samples;
|
|
|
|
} else {
|
2019-05-13 09:11:26 +00:00
|
|
|
size *= channels;
|
2012-11-23 11:20:11 +00:00
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt->stream_index = 0;
|
|
|
|
|
2012-12-07 17:52:23 +00:00
|
|
|
if (ret != size)
|
|
|
|
ret = AVERROR(EIO);
|
|
|
|
|
2012-11-23 11:20:11 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-20 10:01:21 +00:00
|
|
|
static int read_seek(AVFormatContext *s, int stream_index,
|
|
|
|
int64_t timestamp, int flags)
|
|
|
|
{
|
|
|
|
AVStream *st = s->streams[stream_index];
|
|
|
|
BRSTMDemuxContext *b = s->priv_data;
|
|
|
|
int64_t ret = 0;
|
|
|
|
|
2020-09-10 15:23:41 +00:00
|
|
|
if (timestamp < 0)
|
|
|
|
timestamp = 0;
|
2015-06-20 10:01:21 +00:00
|
|
|
timestamp /= b->samples_per_block;
|
2020-09-10 15:23:41 +00:00
|
|
|
if (timestamp >= b->block_count)
|
|
|
|
timestamp = b->block_count - 1;
|
2015-06-20 10:01:21 +00:00
|
|
|
ret = avio_seek(s->pb, b->data_start + timestamp * b->block_size *
|
2019-05-13 09:11:26 +00:00
|
|
|
st->codecpar->ch_layout.nb_channels, SEEK_SET);
|
2015-06-20 10:01:21 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
b->current_block = timestamp;
|
2021-06-05 14:09:03 +00:00
|
|
|
avpriv_update_cur_dts(s, st, timestamp * b->samples_per_block);
|
2015-06-20 10:01:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:45:24 +00:00
|
|
|
const AVInputFormat ff_brstm_demuxer = {
|
2012-11-23 11:20:11 +00:00
|
|
|
.name = "brstm",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("BRSTM (Binary Revolution Stream)"),
|
|
|
|
.priv_data_size = sizeof(BRSTMDemuxContext),
|
2020-03-21 17:31:06 +00:00
|
|
|
.flags_internal = FF_FMT_INIT_CLEANUP,
|
2012-11-23 11:20:11 +00:00
|
|
|
.read_probe = probe,
|
|
|
|
.read_header = read_header,
|
|
|
|
.read_packet = read_packet,
|
|
|
|
.read_close = read_close,
|
2015-06-20 10:01:21 +00:00
|
|
|
.read_seek = read_seek,
|
2012-11-23 11:20:11 +00:00
|
|
|
.extensions = "brstm",
|
|
|
|
};
|
2015-06-17 08:09:52 +00:00
|
|
|
|
2021-04-19 17:45:24 +00:00
|
|
|
const AVInputFormat ff_bfstm_demuxer = {
|
2015-06-17 08:09:52 +00:00
|
|
|
.name = "bfstm",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("BFSTM (Binary Cafe Stream)"),
|
|
|
|
.priv_data_size = sizeof(BRSTMDemuxContext),
|
2020-03-21 17:31:06 +00:00
|
|
|
.flags_internal = FF_FMT_INIT_CLEANUP,
|
2015-06-17 08:09:52 +00:00
|
|
|
.read_probe = probe_bfstm,
|
|
|
|
.read_header = read_header,
|
|
|
|
.read_packet = read_packet,
|
|
|
|
.read_close = read_close,
|
2015-06-20 10:01:21 +00:00
|
|
|
.read_seek = read_seek,
|
2015-06-20 10:01:15 +00:00
|
|
|
.extensions = "bfstm,bcstm",
|
2015-06-17 08:09:52 +00:00
|
|
|
};
|