FFmpeg coverage


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