2014-04-17 13:01:08 +02:00
|
|
|
/*
|
|
|
|
* DSD Stream File (DSF) demuxer
|
|
|
|
* Copyright (c) 2014 Peter Ross
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2021-06-12 01:10:58 +02:00
|
|
|
#include "libavutil/channel_layout.h"
|
2014-04-17 13:01:08 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "id3v2.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint64_t data_end;
|
2018-04-22 11:52:32 +02:00
|
|
|
uint64_t audio_size;
|
|
|
|
uint64_t data_size;
|
2014-04-17 13:01:08 +02:00
|
|
|
} DSFContext;
|
|
|
|
|
2019-03-21 01:18:37 +01:00
|
|
|
static int dsf_probe(const AVProbeData *p)
|
2014-04-17 13:01:08 +02:00
|
|
|
{
|
|
|
|
if (p->buf_size < 12 || memcmp(p->buf, "DSD ", 4) || AV_RL64(p->buf + 4) != 28)
|
|
|
|
return 0;
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uint64_t dsf_channel_layout[] = {
|
|
|
|
0,
|
|
|
|
AV_CH_LAYOUT_MONO,
|
|
|
|
AV_CH_LAYOUT_STEREO,
|
|
|
|
AV_CH_LAYOUT_SURROUND,
|
|
|
|
AV_CH_LAYOUT_QUAD,
|
|
|
|
AV_CH_LAYOUT_4POINT0,
|
|
|
|
AV_CH_LAYOUT_5POINT0_BACK,
|
|
|
|
AV_CH_LAYOUT_5POINT1_BACK,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void read_id3(AVFormatContext *s, uint64_t id3pos)
|
|
|
|
{
|
2021-04-13 01:24:03 +02:00
|
|
|
ID3v2ExtraMeta *id3v2_extra_meta;
|
2014-04-17 13:01:08 +02:00
|
|
|
if (avio_seek(s->pb, id3pos, SEEK_SET) < 0)
|
|
|
|
return;
|
|
|
|
|
2014-04-18 06:49:40 +02:00
|
|
|
ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, 0);
|
2017-10-05 03:34:19 +02:00
|
|
|
if (id3v2_extra_meta) {
|
2020-05-19 12:09:48 +02:00
|
|
|
ff_id3v2_parse_apic(s, id3v2_extra_meta);
|
|
|
|
ff_id3v2_parse_chapters(s, id3v2_extra_meta);
|
2017-10-05 03:34:19 +02:00
|
|
|
}
|
2014-04-17 13:01:08 +02:00
|
|
|
ff_id3v2_free_extra_meta(&id3v2_extra_meta);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dsf_read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
DSFContext *dsf = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
AVStream *st;
|
|
|
|
uint64_t id3pos;
|
|
|
|
unsigned int channel_type;
|
|
|
|
|
|
|
|
avio_skip(pb, 4);
|
|
|
|
if (avio_rl64(pb) != 28)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2014-04-18 07:56:28 +02:00
|
|
|
/* create primary stream before any id3 coverart streams */
|
|
|
|
st = avformat_new_stream(s, NULL);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2014-04-17 13:01:08 +02:00
|
|
|
avio_skip(pb, 8);
|
|
|
|
id3pos = avio_rl64(pb);
|
2017-03-21 21:02:30 +01:00
|
|
|
if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
|
2014-04-17 13:01:08 +02:00
|
|
|
read_id3(s, id3pos);
|
|
|
|
avio_seek(pb, 28, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fmt chunk */
|
|
|
|
|
|
|
|
if (avio_rl32(pb) != MKTAG('f', 'm', 't', ' ') || avio_rl64(pb) != 52)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
if (avio_rl32(pb) != 1) {
|
|
|
|
avpriv_request_sample(s, "unknown format version");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avio_rl32(pb)) {
|
|
|
|
avpriv_request_sample(s, "unknown format id");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel_type = avio_rl32(pb);
|
|
|
|
if (channel_type < FF_ARRAY_ELEMS(dsf_channel_layout))
|
2016-04-10 21:58:15 +02:00
|
|
|
st->codecpar->channel_layout = dsf_channel_layout[channel_type];
|
|
|
|
if (!st->codecpar->channel_layout)
|
2014-04-17 13:01:08 +02:00
|
|
|
avpriv_request_sample(s, "channel type %i", channel_type);
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->channels = avio_rl32(pb);
|
|
|
|
st->codecpar->sample_rate = avio_rl32(pb) / 8;
|
2014-04-17 13:01:08 +02:00
|
|
|
|
2016-04-30 17:50:22 +02:00
|
|
|
if (st->codecpar->channels <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2014-04-17 13:01:08 +02:00
|
|
|
switch(avio_rl32(pb)) {
|
2016-04-10 21:58:15 +02:00
|
|
|
case 1: st->codecpar->codec_id = AV_CODEC_ID_DSD_LSBF_PLANAR; break;
|
|
|
|
case 8: st->codecpar->codec_id = AV_CODEC_ID_DSD_MSBF_PLANAR; break;
|
2014-04-17 13:01:08 +02:00
|
|
|
default:
|
|
|
|
avpriv_request_sample(s, "unknown most significant bit");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2018-04-22 11:52:32 +02:00
|
|
|
dsf->audio_size = avio_rl64(pb) / 8 * st->codecpar->channels;
|
2016-04-10 21:58:15 +02:00
|
|
|
st->codecpar->block_align = avio_rl32(pb);
|
2020-11-05 21:22:13 +01:00
|
|
|
if (st->codecpar->block_align > INT_MAX / st->codecpar->channels || st->codecpar->block_align <= 0) {
|
|
|
|
avpriv_request_sample(s, "block_align invalid");
|
2014-04-17 13:01:08 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
st->codecpar->block_align *= st->codecpar->channels;
|
2021-04-23 19:39:16 +02:00
|
|
|
st->codecpar->bit_rate = st->codecpar->channels * 8LL * st->codecpar->sample_rate;
|
2019-07-30 13:16:33 +02:00
|
|
|
avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
|
2014-04-17 13:01:08 +02:00
|
|
|
avio_skip(pb, 4);
|
|
|
|
|
|
|
|
/* data chunk */
|
|
|
|
|
|
|
|
dsf->data_end = avio_tell(pb);
|
|
|
|
if (avio_rl32(pb) != MKTAG('d', 'a', 't', 'a'))
|
|
|
|
return AVERROR_INVALIDDATA;
|
2018-04-22 13:14:26 +02:00
|
|
|
dsf->data_size = avio_rl64(pb) - 12;
|
|
|
|
dsf->data_end += dsf->data_size + 12;
|
2014-04-17 13:01:08 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2021-08-24 14:58:07 +02:00
|
|
|
FFFormatContext *const si = ffformatcontext(s);
|
2014-04-17 13:01:08 +02:00
|
|
|
DSFContext *dsf = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
AVStream *st = s->streams[0];
|
|
|
|
int64_t pos = avio_tell(pb);
|
2019-07-30 13:16:33 +02:00
|
|
|
int ret;
|
2014-04-17 13:01:08 +02:00
|
|
|
|
|
|
|
if (pos >= dsf->data_end)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2018-04-22 11:52:32 +02:00
|
|
|
if (dsf->data_size > dsf->audio_size) {
|
|
|
|
int last_packet = pos == (dsf->data_end - st->codecpar->block_align);
|
|
|
|
|
|
|
|
if (last_packet) {
|
2021-08-24 14:58:07 +02:00
|
|
|
int64_t data_pos = pos - si->data_offset;
|
2018-04-22 11:52:32 +02:00
|
|
|
int64_t packet_size = dsf->audio_size - data_pos;
|
|
|
|
int64_t skip_size = dsf->data_size - data_pos - packet_size;
|
|
|
|
uint8_t *dst;
|
|
|
|
int ch, ret;
|
|
|
|
|
|
|
|
if (packet_size <= 0 || skip_size <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2019-12-10 22:59:53 +01:00
|
|
|
if ((ret = av_new_packet(pkt, packet_size)) < 0)
|
|
|
|
return ret;
|
2018-04-22 11:52:32 +02:00
|
|
|
dst = pkt->data;
|
|
|
|
for (ch = 0; ch < st->codecpar->channels; ch++) {
|
|
|
|
ret = avio_read(pb, dst, packet_size / st->codecpar->channels);
|
|
|
|
if (ret < packet_size / st->codecpar->channels)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
dst += ret;
|
|
|
|
avio_skip(pb, skip_size / st->codecpar->channels);
|
|
|
|
}
|
|
|
|
|
2019-07-30 13:16:33 +02:00
|
|
|
pkt->pos = pos;
|
|
|
|
pkt->stream_index = 0;
|
2021-08-24 14:58:07 +02:00
|
|
|
pkt->pts = (pos - si->data_offset) / st->codecpar->channels;
|
2019-07-30 13:16:33 +02:00
|
|
|
pkt->duration = packet_size / st->codecpar->channels;
|
2018-04-22 11:52:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 13:16:33 +02:00
|
|
|
ret = av_get_packet(pb, pkt, FFMIN(dsf->data_end - pos, st->codecpar->block_align));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
pkt->stream_index = 0;
|
2021-08-24 14:58:07 +02:00
|
|
|
pkt->pts = (pos - si->data_offset) / st->codecpar->channels;
|
2019-07-30 13:16:33 +02:00
|
|
|
pkt->duration = st->codecpar->block_align / st->codecpar->channels;
|
|
|
|
|
|
|
|
return 0;
|
2014-04-17 13:01:08 +02:00
|
|
|
}
|
|
|
|
|
2021-04-19 19:45:24 +02:00
|
|
|
const AVInputFormat ff_dsf_demuxer = {
|
2014-04-17 13:01:08 +02:00
|
|
|
.name = "dsf",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("DSD Stream File (DSF)"),
|
|
|
|
.priv_data_size = sizeof(DSFContext),
|
|
|
|
.read_probe = dsf_probe,
|
|
|
|
.read_header = dsf_read_header,
|
|
|
|
.read_packet = dsf_read_packet,
|
|
|
|
.flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
|
|
|
|
};
|