FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/wvdec.c
Date: 2026-04-30 18:27:06
Exec Total Coverage
Lines: 131 185 70.8%
Functions: 4 4 100.0%
Branches: 104 145 71.7%

Line Branch Exec Source
1 /*
2 * WavPack demuxer
3 * Copyright (c) 2006,2011 Konstantin Shishkov
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 "libavutil/dict.h"
25 #include "avformat.h"
26 #include "avio_internal.h"
27 #include "demux.h"
28 #include "internal.h"
29 #include "apetag.h"
30 #include "id3v1.h"
31 #include "wv.h"
32
33 enum WV_FLAGS {
34 WV_MONO = 0x0004,
35 WV_HYBRID = 0x0008,
36 WV_JOINT = 0x0010,
37 WV_CROSSD = 0x0020,
38 WV_HSHAPE = 0x0040,
39 WV_FLOAT = 0x0080,
40 WV_INT32 = 0x0100,
41 WV_HBR = 0x0200,
42 WV_HBAL = 0x0400,
43 WV_MCINIT = 0x0800,
44 WV_MCEND = 0x1000,
45 WV_DSD = 0x80000000,
46 };
47
48 static const int wv_rates[16] = {
49 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
50 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
51 };
52
53 typedef struct WVContext {
54 uint8_t block_header[WV_HEADER_SIZE];
55 WvHeader header;
56 int rate, chan, bpp;
57 uint32_t chmask;
58 int multichannel;
59 int block_parsed;
60 int64_t pos;
61
62 int64_t apetag_start;
63 } WVContext;
64
65 7480 static int wv_probe(const AVProbeData *p)
66 {
67 /* check file header */
68
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7479 times.
7480 if (p->buf_size <= 32)
69 1 return 0;
70
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 7445 times.
7479 if (AV_RL32(&p->buf[0]) == MKTAG('w', 'v', 'p', 'k') &&
71
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 AV_RL32(&p->buf[4]) >= 24 &&
72
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 AV_RL32(&p->buf[4]) <= WV_BLOCK_LIMIT &&
73
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 AV_RL16(&p->buf[8]) >= 0x402 &&
74
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 AV_RL16(&p->buf[8]) <= 0x410)
75 34 return AVPROBE_SCORE_MAX;
76 else
77 7445 return 0;
78 }
79
80 8324 static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
81 {
82 8324 WVContext *wc = ctx->priv_data;
83 int ret;
84 int rate, bpp, chan;
85 uint32_t chmask, flags;
86 unsigned rate_x;
87
88 8324 wc->pos = avio_tell(pb);
89
90 /* don't return bogus packets with the ape tag data */
91
4/4
✓ Branch 0 taken 6789 times.
✓ Branch 1 taken 1535 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 6786 times.
8324 if (wc->apetag_start && wc->pos >= wc->apetag_start)
92 3 return AVERROR_EOF;
93
94 8321 ret = avio_read(pb, wc->block_header, WV_HEADER_SIZE);
95
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 8309 times.
8321 if (ret != WV_HEADER_SIZE)
96
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 return (ret < 0) ? ret : AVERROR_EOF;
97
98 8309 ret = ff_wv_parse_header(&wc->header, wc->block_header);
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8309 times.
8309 if (ret < 0) {
100 av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
101 return ret;
102 }
103
104
2/4
✓ Branch 0 taken 8309 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8309 times.
8309 if (wc->header.version < 0x402 || wc->header.version > 0x410) {
105 avpriv_report_missing_feature(ctx, "WV version 0x%03X",
106 wc->header.version);
107 return AVERROR_PATCHWELCOME;
108 }
109
110 /* Blocks with zero samples don't contain actual audio information
111 * and should be ignored */
112
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8303 times.
8309 if (!wc->header.samples)
113 6 return 0;
114 // parse flags
115 8303 flags = wc->header.flags;
116
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 8223 times.
8303 rate_x = (flags & WV_DSD) ? 4 : 1;
117
2/2
✓ Branch 0 taken 8223 times.
✓ Branch 1 taken 80 times.
8303 bpp = (flags & WV_DSD) ? 0 : ((flags & 3) + 1) << 3;
118
2/2
✓ Branch 0 taken 7657 times.
✓ Branch 1 taken 646 times.
8303 chan = 1 + !(flags & WV_MONO);
119
2/2
✓ Branch 0 taken 646 times.
✓ Branch 1 taken 7657 times.
8303 chmask = flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
120 8303 rate = wv_rates[(flags >> 23) & 0xF];
121
4/4
✓ Branch 0 taken 7629 times.
✓ Branch 1 taken 674 times.
✓ Branch 2 taken 284 times.
✓ Branch 3 taken 7345 times.
8303 wc->multichannel = !(wc->header.initial && wc->header.final);
122
2/2
✓ Branch 0 taken 958 times.
✓ Branch 1 taken 7345 times.
8303 if (wc->multichannel) {
123 958 chan = wc->chan;
124 958 chmask = wc->chmask;
125 }
126
7/8
✓ Branch 0 taken 8303 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8294 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 80 times.
✓ Branch 5 taken 8214 times.
✓ Branch 6 taken 10 times.
✓ Branch 7 taken 79 times.
8303 if ((rate == -1 || !chan || flags & WV_DSD) && !wc->block_parsed) {
127 10 int64_t block_end = avio_tell(pb) + wc->header.blocksize;
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
129 av_log(ctx, AV_LOG_ERROR,
130 "Cannot determine additional parameters\n");
131 return AVERROR_INVALIDDATA;
132 }
133
3/4
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 10 times.
✓ Branch 4 taken 83 times.
✗ Branch 5 not taken.
93 while (avio_tell(pb) < block_end && !avio_feof(pb)) {
134 int id, size;
135 83 id = avio_r8(pb);
136
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 73 times.
83 size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
137 83 size <<= 1;
138
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 62 times.
83 if (id & 0x40)
139 21 size--;
140
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 73 times.
83 switch (id & 0x3F) {
141 9 case 0xD:
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (size <= 1) {
143 av_log(ctx, AV_LOG_ERROR,
144 "Insufficient channel information\n");
145 return AVERROR_INVALIDDATA;
146 }
147 9 chan = avio_r8(pb);
148
1/7
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
9 switch (size - 2) {
149 case 0:
150 chmask = avio_r8(pb);
151 break;
152 9 case 1:
153 9 chmask = avio_rl16(pb);
154 9 break;
155 case 2:
156 chmask = avio_rl24(pb);
157 break;
158 case 3:
159 chmask = avio_rl32(pb);
160 break;
161 case 4:
162 avio_skip(pb, 1);
163 chan |= (avio_r8(pb) & 0xF) << 8;
164 chan += 1;
165 chmask = avio_rl24(pb);
166 break;
167 case 5:
168 avio_skip(pb, 1);
169 chan |= (avio_r8(pb) & 0xF) << 8;
170 chan += 1;
171 chmask = avio_rl32(pb);
172 break;
173 default:
174 av_log(ctx, AV_LOG_ERROR,
175 "Invalid channel info size %d\n", size);
176 return AVERROR_INVALIDDATA;
177 }
178 9 break;
179 1 case 0xE:
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (size <= 1) {
181 av_log(ctx, AV_LOG_ERROR,
182 "Invalid DSD block\n");
183 return AVERROR_INVALIDDATA;
184 }
185 1 rate_x = 1U << (avio_r8(pb) & 0x1f);
186
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (size)
187 1 avio_skip(pb, size-1);
188 1 break;
189 case 0x27:
190 rate = avio_rl24(pb);
191 break;
192 73 default:
193 73 avio_skip(pb, size);
194 }
195
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 62 times.
83 if (id & 0x40)
196 21 avio_skip(pb, 1);
197 }
198
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
10 if (rate == -1 || rate * (uint64_t)rate_x >= INT_MAX) {
199 av_log(ctx, AV_LOG_ERROR,
200 "Cannot determine custom sampling rate\n");
201 return AVERROR_INVALIDDATA;
202 }
203 10 avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
204 }
205
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 8190 times.
8303 if (!wc->bpp)
206 113 wc->bpp = bpp;
207
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 8269 times.
8303 if (!wc->chan)
208 34 wc->chan = chan;
209
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 8269 times.
8303 if (!wc->chmask)
210 34 wc->chmask = chmask;
211
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 8269 times.
8303 if (!wc->rate)
212 34 wc->rate = rate * rate_x;
213
214
2/4
✓ Branch 0 taken 8303 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8303 times.
8303 if (flags && bpp != wc->bpp) {
215 av_log(ctx, AV_LOG_ERROR,
216 "Bits per sample differ, this block: %i, header block: %i\n",
217 bpp, wc->bpp);
218 return AVERROR_INVALIDDATA;
219 }
220
4/6
✓ Branch 0 taken 8303 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7345 times.
✓ Branch 3 taken 958 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 7345 times.
8303 if (flags && !wc->multichannel && chan != wc->chan) {
221 av_log(ctx, AV_LOG_ERROR,
222 "Channels differ, this block: %i, header block: %i\n",
223 chan, wc->chan);
224 return AVERROR_INVALIDDATA;
225 }
226
5/8
✓ Branch 0 taken 8303 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8303 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8223 times.
✓ Branch 5 taken 80 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 8223 times.
8303 if (flags && rate != -1 && !(flags & WV_DSD) && rate * rate_x != wc->rate) {
227 av_log(ctx, AV_LOG_ERROR,
228 "Sampling rate differ, this block: %i, header block: %i\n",
229 rate * rate_x, wc->rate);
230 return AVERROR_INVALIDDATA;
231 }
232 8303 return 0;
233 }
234
235 34 static int wv_read_header(AVFormatContext *s)
236 {
237 34 AVIOContext *pb = s->pb;
238 34 WVContext *wc = s->priv_data;
239 AVStream *st;
240 int ret;
241
242 34 wc->block_parsed = 0;
243 for (;;) {
244
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
34 if ((ret = wv_read_block_header(s, pb)) < 0)
245 return ret;
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (!wc->header.samples)
247 avio_skip(pb, wc->header.blocksize);
248 else
249 34 break;
250 }
251
252 /* now we are ready: build format streams */
253 34 st = avformat_new_stream(s, NULL);
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (!st)
255 return AVERROR(ENOMEM);
256
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
34 if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
257 return ret;
258 34 AV_WL16(st->codecpar->extradata, wc->header.version);
259 34 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
260 34 st->codecpar->codec_id = AV_CODEC_ID_WAVPACK;
261 34 av_channel_layout_from_mask(&st->codecpar->ch_layout, wc->chmask);
262 34 st->codecpar->sample_rate = wc->rate;
263 34 st->codecpar->bits_per_coded_sample = wc->bpp;
264 34 avpriv_set_pts_info(st, 64, 1, wc->rate);
265 34 st->start_time = 0;
266
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (wc->header.total_samples != 0xFFFFFFFFu)
267 34 st->duration = wc->header.total_samples;
268
269
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
270 34 int64_t cur = avio_tell(s->pb);
271 34 wc->apetag_start = ff_ape_parse_tag(s);
272
2/2
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 4 times.
34 if (av_dict_count(s->metadata) == 0)
273 30 ff_id3v1_read(s);
274 34 avio_seek(s->pb, cur, SEEK_SET);
275 }
276
277 34 return 0;
278 }
279
280 7644 static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
281 {
282 7644 WVContext *wc = s->priv_data;
283 int ret;
284 int off;
285 int64_t pos;
286 uint32_t block_samples;
287
288
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7644 times.
7644 if (avio_feof(s->pb))
289 return AVERROR_EOF;
290
2/2
✓ Branch 0 taken 7610 times.
✓ Branch 1 taken 34 times.
7644 if (wc->block_parsed) {
291
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 7601 times.
7610 if ((ret = wv_read_block_header(s, s->pb)) < 0)
292 9 return ret;
293 }
294
295 7635 pos = wc->pos;
296
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7635 times.
7635 if ((ret = av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE)) < 0)
297 return ret;
298 7635 memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
299 7635 ret = ffio_read_size(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
300
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 7618 times.
7635 if (ret < 0) {
301 17 return ret;
302 }
303
2/2
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 7611 times.
8291 while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
304
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 674 times.
680 if ((ret = wv_read_block_header(s, s->pb)) < 0) {
305 6 return ret;
306 }
307
308 674 off = pkt->size;
309
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 674 times.
674 if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
310 return ret;
311 }
312 674 memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
313
314 674 ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
315
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 673 times.
674 if (ret != wc->header.blocksize) {
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 return (ret < 0) ? ret : AVERROR_EOF;
317 }
318 }
319 7611 pkt->stream_index = 0;
320 7611 pkt->pos = pos;
321 7611 wc->block_parsed = 1;
322 7611 pkt->pts = wc->header.block_idx;
323 7611 block_samples = wc->header.samples;
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7611 times.
7611 if (block_samples > INT32_MAX)
325 av_log(s, AV_LOG_WARNING,
326 "Too many samples in block: %"PRIu32"\n", block_samples);
327 else
328 7611 pkt->duration = block_samples;
329
330 7611 return 0;
331 }
332
333 const FFInputFormat ff_wv_demuxer = {
334 .p.name = "wv",
335 .p.long_name = NULL_IF_CONFIG_SMALL("WavPack"),
336 .p.flags = AVFMT_GENERIC_INDEX,
337 .priv_data_size = sizeof(WVContext),
338 .read_probe = wv_probe,
339 .read_header = wv_read_header,
340 .read_packet = wv_read_packet,
341 };
342