FFmpeg coverage


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