Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/dsfdec.c
Line
Count
Source
1
/*
2
 * DSD Stream File (DSF) demuxer
3
 * Copyright (c) 2014 Peter Ross
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include "libavutil/channel_layout.h"
23
#include "libavutil/intreadwrite.h"
24
#include "avformat.h"
25
#include "demux.h"
26
#include "internal.h"
27
#include "id3v2.h"
28
29
typedef struct {
30
    uint64_t data_end;
31
    uint64_t audio_size;
32
    uint64_t data_size;
33
} DSFContext;
34
35
static int dsf_probe(const AVProbeData *p)
36
958k
{
37
958k
    if (p->buf_size < 12 || memcmp(p->buf, "DSD ", 4) || AV_RL64(p->buf + 4) != 28)
38
958k
        return 0;
39
25
    return AVPROBE_SCORE_MAX;
40
958k
}
41
42
static const AVChannelLayout dsf_channel_layout[] = {
43
    { .order = AV_CHANNEL_ORDER_UNSPEC },
44
    AV_CHANNEL_LAYOUT_MONO,
45
    AV_CHANNEL_LAYOUT_STEREO,
46
    AV_CHANNEL_LAYOUT_SURROUND,
47
    AV_CHANNEL_LAYOUT_QUAD,
48
    AV_CHANNEL_LAYOUT_4POINT0,
49
    AV_CHANNEL_LAYOUT_5POINT0_BACK,
50
    AV_CHANNEL_LAYOUT_5POINT1_BACK,
51
};
52
53
static void read_id3(AVFormatContext *s, uint64_t id3pos)
54
4.38k
{
55
4.38k
    ID3v2ExtraMeta *id3v2_extra_meta;
56
4.38k
    if (avio_seek(s->pb, id3pos, SEEK_SET) < 0)
57
799
        return;
58
59
3.58k
    ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, 0);
60
3.58k
    if (id3v2_extra_meta) {
61
1.16k
        ff_id3v2_parse_apic(s, id3v2_extra_meta);
62
1.16k
        ff_id3v2_parse_chapters(s, id3v2_extra_meta);
63
1.16k
    }
64
3.58k
    ff_id3v2_free_extra_meta(&id3v2_extra_meta);
65
3.58k
}
66
67
static int dsf_read_header(AVFormatContext *s)
68
6.08k
{
69
6.08k
    DSFContext *dsf = s->priv_data;
70
6.08k
    AVIOContext *pb = s->pb;
71
6.08k
    AVStream *st;
72
6.08k
    uint64_t id3pos;
73
6.08k
    unsigned int channel_type;
74
6.08k
    int channels;
75
76
6.08k
    avio_skip(pb, 4);
77
6.08k
    if (avio_rl64(pb) != 28)
78
227
        return AVERROR_INVALIDDATA;
79
80
    /* create primary stream before any id3 coverart streams */
81
5.85k
    st = avformat_new_stream(s, NULL);
82
5.85k
    if (!st)
83
0
        return AVERROR(ENOMEM);
84
85
5.85k
    avio_skip(pb, 8);
86
5.85k
    id3pos = avio_rl64(pb);
87
5.85k
    if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
88
4.38k
        read_id3(s, id3pos);
89
4.38k
        avio_seek(pb, 28, SEEK_SET);
90
4.38k
    }
91
92
    /* fmt chunk */
93
94
5.85k
    if (avio_rl32(pb) != MKTAG('f', 'm', 't', ' ') || avio_rl64(pb) != 52)
95
2.29k
        return AVERROR_INVALIDDATA;
96
97
3.56k
    if (avio_rl32(pb) != 1) {
98
154
        avpriv_request_sample(s, "unknown format version");
99
154
        return AVERROR_INVALIDDATA;
100
154
    }
101
102
3.40k
    if (avio_rl32(pb)) {
103
116
        avpriv_request_sample(s, "unknown format id");
104
116
        return AVERROR_INVALIDDATA;
105
116
    }
106
107
3.29k
    channel_type = avio_rl32(pb);
108
3.29k
    if (channel_type < FF_ARRAY_ELEMS(dsf_channel_layout))
109
81
        st->codecpar->ch_layout = dsf_channel_layout[channel_type];
110
3.29k
    if (!st->codecpar->ch_layout.nb_channels)
111
3.22k
        avpriv_request_sample(s, "channel type %i", channel_type);
112
113
3.29k
    st->codecpar->codec_type   = AVMEDIA_TYPE_AUDIO;
114
3.29k
    channels = avio_rl32(pb);
115
3.29k
    if (!st->codecpar->ch_layout.nb_channels) {
116
3.22k
        st->codecpar->ch_layout.nb_channels = channels;
117
3.22k
    } else if (channels != st->codecpar->ch_layout.nb_channels) {
118
53
        av_log(s, AV_LOG_ERROR, "Channel count mismatch\n");
119
53
        return AVERROR(EINVAL);
120
53
    }
121
3.24k
    st->codecpar->sample_rate  = avio_rl32(pb) / 8;
122
123
3.24k
    if (st->codecpar->ch_layout.nb_channels <= 0)
124
56
        return AVERROR_INVALIDDATA;
125
126
3.18k
    switch(avio_rl32(pb)) {
127
3.00k
    case 1: st->codecpar->codec_id = AV_CODEC_ID_DSD_LSBF_PLANAR; break;
128
129
    case 8: st->codecpar->codec_id = AV_CODEC_ID_DSD_MSBF_PLANAR; break;
129
47
    default:
130
47
        avpriv_request_sample(s, "unknown most significant bit");
131
47
        return AVERROR_INVALIDDATA;
132
3.18k
    }
133
134
3.13k
    dsf->audio_size = avio_rl64(pb) / 8 * st->codecpar->ch_layout.nb_channels;
135
3.13k
    st->codecpar->block_align = avio_rl32(pb);
136
3.13k
    if (st->codecpar->block_align > INT_MAX / st->codecpar->ch_layout.nb_channels ||
137
3.10k
        st->codecpar->block_align <= 0) {
138
89
        avpriv_request_sample(s, "block_align invalid");
139
89
        return AVERROR_INVALIDDATA;
140
89
    }
141
3.04k
    st->codecpar->block_align *= st->codecpar->ch_layout.nb_channels;
142
3.04k
    st->codecpar->bit_rate = st->codecpar->ch_layout.nb_channels * 8LL * st->codecpar->sample_rate;
143
3.04k
    avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
144
3.04k
    avio_skip(pb, 4);
145
146
    /* data chunk */
147
148
3.04k
    dsf->data_end = avio_tell(pb);
149
3.04k
    if (avio_rl32(pb) != MKTAG('d', 'a', 't', 'a'))
150
110
        return AVERROR_INVALIDDATA;
151
2.93k
    dsf->data_size = avio_rl64(pb) - 12;
152
2.93k
    dsf->data_end += dsf->data_size + 12;
153
154
2.93k
    return 0;
155
3.04k
}
156
157
static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
158
18.8M
{
159
18.8M
    FFFormatContext *const si = ffformatcontext(s);
160
18.8M
    DSFContext *dsf = s->priv_data;
161
18.8M
    AVIOContext *pb = s->pb;
162
18.8M
    AVStream *st = s->streams[0];
163
18.8M
    int64_t pos = avio_tell(pb);
164
18.8M
    int channels = st->codecpar->ch_layout.nb_channels;
165
18.8M
    int ret;
166
167
18.8M
    if (pos >= dsf->data_end)
168
281
        return AVERROR_EOF;
169
170
18.8M
    if (dsf->data_size > dsf->audio_size) {
171
5.30M
        int last_packet = pos == (dsf->data_end - st->codecpar->block_align);
172
173
5.30M
        if (last_packet) {
174
334
            int64_t data_pos = pos - si->data_offset;
175
334
            int64_t packet_size = dsf->audio_size - data_pos;
176
334
            int64_t skip_size = dsf->data_size - data_pos - packet_size;
177
334
            uint8_t *dst;
178
334
            int ch, ret;
179
180
334
            if (packet_size <= 0 || skip_size <= 0)
181
192
                return AVERROR_INVALIDDATA;
182
183
142
            if ((ret = av_new_packet(pkt, packet_size)) < 0)
184
0
                return ret;
185
142
            dst = pkt->data;
186
424
            for (ch = 0; ch < st->codecpar->ch_layout.nb_channels; ch++) {
187
409
                ret = avio_read(pb, dst,  packet_size / st->codecpar->ch_layout.nb_channels);
188
409
                if (ret < packet_size / st->codecpar->ch_layout.nb_channels)
189
127
                    return AVERROR_EOF;
190
191
282
                dst += ret;
192
282
                avio_skip(pb, skip_size / st->codecpar->ch_layout.nb_channels);
193
282
            }
194
195
15
            pkt->pos = pos;
196
15
            pkt->stream_index = 0;
197
15
            pkt->pts = (pos - si->data_offset) / channels;
198
15
            pkt->duration = packet_size / channels;
199
15
            return 0;
200
142
        }
201
5.30M
    }
202
18.8M
    ret = av_get_packet(pb, pkt, FFMIN(dsf->data_end - pos, st->codecpar->block_align));
203
18.8M
    if (ret < 0)
204
3.62k
        return ret;
205
206
18.7M
    pkt->stream_index = 0;
207
18.7M
    pkt->pts = (pos - si->data_offset) / channels;
208
18.7M
    pkt->duration = st->codecpar->block_align / channels;
209
210
18.7M
    return 0;
211
18.8M
}
212
213
const FFInputFormat ff_dsf_demuxer = {
214
    .p.name         = "dsf",
215
    .p.long_name    = NULL_IF_CONFIG_SMALL("DSD Stream File (DSF)"),
216
    .p.flags        = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
217
    .priv_data_size = sizeof(DSFContext),
218
    .read_probe     = dsf_probe,
219
    .read_header    = dsf_read_header,
220
    .read_packet    = dsf_read_packet,
221
};