FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/dsfdec.c
Date: 2026-09-25 02:02:46
Exec Total Coverage
Lines: 3 110 2.7%
Functions: 1 4 25.0%
Branches: 2 61 3.3%

Line Branch Exec 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 8064 static int dsf_probe(const AVProbeData *p)
36 {
37
2/6
✓ Branch 0 taken 8064 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8064 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
8064 if (p->buf_size < 12 || memcmp(p->buf, "DSD ", 4) || AV_RL64(p->buf + 4) != 28)
38 8064 return 0;
39 ✗ return AVPROBE_SCORE_MAX;
40 }
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 {
55 ID3v2ExtraMeta *id3v2_extra_meta;
56 ✗ if (avio_seek(s->pb, id3pos, SEEK_SET) < 0)
57 ✗ return;
58
59 ✗ ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, 0);
60 ✗ if (id3v2_extra_meta) {
61 ✗ ff_id3v2_parse_apic(s, id3v2_extra_meta);
62 ✗ ff_id3v2_parse_chapters(s, id3v2_extra_meta);
63 }
64 ✗ ff_id3v2_free_extra_meta(&id3v2_extra_meta);
65 }
66
67 ✗ static int dsf_read_header(AVFormatContext *s)
68 {
69 ✗ DSFContext *dsf = s->priv_data;
70 ✗ AVIOContext *pb = s->pb;
71 AVStream *st;
72 uint64_t id3pos;
73 unsigned int channel_type;
74 int channels;
75
76 ✗ avio_skip(pb, 4);
77 ✗ if (avio_rl64(pb) != 28)
78 ✗ return AVERROR_INVALIDDATA;
79
80 /* create primary stream before any id3 coverart streams */
81 ✗ st = avformat_new_stream(s, NULL);
82 ✗ if (!st)
83 ✗ return AVERROR(ENOMEM);
84
85 ✗ avio_skip(pb, 8);
86 ✗ id3pos = avio_rl64(pb);
87 ✗ if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
88 ✗ read_id3(s, id3pos);
89 ✗ avio_seek(pb, 28, SEEK_SET);
90 }
91
92 /* fmt chunk */
93
94 ✗ if (avio_rl32(pb) != MKTAG('f', 'm', 't', ' ') || avio_rl64(pb) != 52)
95 ✗ return AVERROR_INVALIDDATA;
96
97 ✗ if (avio_rl32(pb) != 1) {
98 ✗ avpriv_request_sample(s, "unknown format version");
99 ✗ return AVERROR_INVALIDDATA;
100 }
101
102 ✗ if (avio_rl32(pb)) {
103 ✗ avpriv_request_sample(s, "unknown format id");
104 ✗ return AVERROR_INVALIDDATA;
105 }
106
107 ✗ channel_type = avio_rl32(pb);
108 ✗ if (channel_type < FF_ARRAY_ELEMS(dsf_channel_layout))
109 ✗ st->codecpar->ch_layout = dsf_channel_layout[channel_type];
110 ✗ if (!st->codecpar->ch_layout.nb_channels)
111 ✗ avpriv_request_sample(s, "channel type %i", channel_type);
112
113 ✗ st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
114 ✗ channels = avio_rl32(pb);
115 ✗ if (!st->codecpar->ch_layout.nb_channels) {
116 ✗ st->codecpar->ch_layout.nb_channels = channels;
117 ✗ } else if (channels != st->codecpar->ch_layout.nb_channels) {
118 ✗ av_log(s, AV_LOG_ERROR, "Channel count mismatch\n");
119 ✗ return AVERROR(EINVAL);
120 }
121 ✗ st->codecpar->sample_rate = avio_rl32(pb) / 8;
122
123 ✗ if (st->codecpar->ch_layout.nb_channels <= 0)
124 ✗ return AVERROR_INVALIDDATA;
125
126 ✗ switch(avio_rl32(pb)) {
127 ✗ case 1: st->codecpar->codec_id = AV_CODEC_ID_DSD_LSBF_PLANAR; break;
128 ✗ case 8: st->codecpar->codec_id = AV_CODEC_ID_DSD_MSBF_PLANAR; break;
129 ✗ default:
130 ✗ avpriv_request_sample(s, "unknown most significant bit");
131 ✗ return AVERROR_INVALIDDATA;
132 }
133 ✗ st->codecpar->format = AV_SAMPLE_FMT_DSD;
134
135 ✗ dsf->audio_size = avio_rl64(pb) / 8 * st->codecpar->ch_layout.nb_channels;
136 ✗ st->codecpar->block_align = avio_rl32(pb);
137 ✗ if (st->codecpar->block_align > INT_MAX / st->codecpar->ch_layout.nb_channels ||
138 ✗ st->codecpar->block_align <= 0) {
139 ✗ avpriv_request_sample(s, "block_align invalid");
140 ✗ return AVERROR_INVALIDDATA;
141 }
142 ✗ st->codecpar->block_align *= st->codecpar->ch_layout.nb_channels;
143 ✗ st->codecpar->bit_rate = st->codecpar->ch_layout.nb_channels * 8LL * st->codecpar->sample_rate;
144 ✗ avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
145 ✗ avio_skip(pb, 4);
146
147 /* data chunk */
148
149 ✗ dsf->data_end = avio_tell(pb);
150 ✗ if (avio_rl32(pb) != MKTAG('d', 'a', 't', 'a'))
151 ✗ return AVERROR_INVALIDDATA;
152 ✗ dsf->data_size = avio_rl64(pb) - 12;
153 ✗ dsf->data_end += dsf->data_size + 12;
154
155 ✗ return 0;
156 }
157
158 ✗ static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
159 {
160 ✗ FFFormatContext *const si = ffformatcontext(s);
161 ✗ DSFContext *dsf = s->priv_data;
162 ✗ AVIOContext *pb = s->pb;
163 ✗ AVStream *st = s->streams[0];
164 ✗ int64_t pos = avio_tell(pb);
165 ✗ int channels = st->codecpar->ch_layout.nb_channels;
166 int ret;
167
168 ✗ if (pos >= dsf->data_end)
169 ✗ return AVERROR_EOF;
170
171 ✗ if (dsf->data_size > dsf->audio_size) {
172 ✗ int last_packet = pos == (dsf->data_end - st->codecpar->block_align);
173
174 ✗ if (last_packet) {
175 ✗ int64_t data_pos = pos - si->data_offset;
176 ✗ int64_t packet_size = dsf->audio_size - data_pos;
177 ✗ int64_t skip_size = dsf->data_size - data_pos - packet_size;
178 uint8_t *dst;
179 int ch;
180
181 ✗ if (packet_size <= 0 || skip_size <= 0)
182 ✗ return AVERROR_INVALIDDATA;
183
184 ✗ if ((ret = av_new_packet(pkt, packet_size)) < 0)
185 ✗ return ret;
186 ✗ dst = pkt->data;
187 ✗ for (ch = 0; ch < st->codecpar->ch_layout.nb_channels; ch++) {
188 ✗ ret = avio_read(pb, dst, packet_size / st->codecpar->ch_layout.nb_channels);
189 ✗ if (ret < packet_size / st->codecpar->ch_layout.nb_channels)
190 ✗ return AVERROR_EOF;
191
192 ✗ dst += ret;
193 ✗ avio_skip(pb, skip_size / st->codecpar->ch_layout.nb_channels);
194 }
195
196 ✗ pkt->pos = pos;
197 ✗ pkt->stream_index = 0;
198 ✗ pkt->pts = (pos - si->data_offset) / channels;
199 ✗ pkt->duration = packet_size / channels;
200 ✗ return 0;
201 }
202 }
203 ✗ ret = av_get_packet(pb, pkt, FFMIN(dsf->data_end - pos, st->codecpar->block_align));
204 ✗ if (ret < 0)
205 ✗ return ret;
206
207 ✗ pkt->stream_index = 0;
208 ✗ pkt->pts = (pos - si->data_offset) / channels;
209 ✗ pkt->duration = st->codecpar->block_align / channels;
210
211 ✗ return 0;
212 }
213
214 const FFInputFormat ff_dsf_demuxer = {
215 .p.name = "dsf",
216 .p.long_name = NULL_IF_CONFIG_SMALL("DSD Stream File (DSF)"),
217 .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
218 .priv_data_size = sizeof(DSFContext),
219 .read_probe = dsf_probe,
220 .read_header = dsf_read_header,
221 .read_packet = dsf_read_packet,
222 };
223