FFmpeg
Loading...
Searching...
No Matches
wvdec.c
Go to the documentation of this file.
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
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
46static 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
62
63static int wv_probe(const AVProbeData *p)
64{
65 /* check file header */
66 if (p->buf_size <= 32)
67 return 0;
68 if (AV_RL32(&p->buf[0]) == MKTAG('w', 'v', 'p', 'k') &&
69 AV_RL32(&p->buf[4]) >= 24 &&
70 AV_RL32(&p->buf[4]) <= WV_BLOCK_LIMIT &&
71 AV_RL16(&p->buf[8]) >= 0x402 &&
72 AV_RL16(&p->buf[8]) <= 0x410)
73 return AVPROBE_SCORE_MAX;
74 else
75 return 0;
76}
77
79{
80 WVContext *wc = ctx->priv_data;
81 int ret;
82 int rate, bpp, chan;
83 uint32_t chmask, flags;
84 unsigned rate_x;
85
86 wc->pos = avio_tell(pb);
87
88 /* don't return bogus packets with the ape tag data */
89 if (wc->apetag_start && wc->pos >= wc->apetag_start)
90 return AVERROR_EOF;
91
93 if (ret != WV_HEADER_SIZE)
94 return (ret < 0) ? ret : AVERROR_EOF;
95
96 ret = ff_wv_parse_header(&wc->header, wc->block_header);
97 if (ret < 0) {
98 av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
99 return ret;
100 }
101
102 if (wc->header.version < 0x402 || wc->header.version > 0x410) {
103 avpriv_report_missing_feature(ctx, "WV version 0x%03X",
104 wc->header.version);
106 }
107
108 /* Blocks with zero samples don't contain actual audio information
109 * and should be ignored */
110 if (!wc->header.samples)
111 return 0;
112 // parse flags
113 flags = wc->header.flags;
114 rate_x = (flags & WV_DSD) ? 4 : 1;
115 bpp = (flags & WV_DSD) ? 0 : ((flags & 3) + 1) << 3;
116 chan = 1 + !(flags & WV_MONO);
118 rate = wv_rates[(flags >> 23) & 0xF];
119 wc->multichannel = !(wc->header.initial && wc->header.final);
120 if (wc->multichannel) {
121 chan = wc->chan;
122 chmask = wc->chmask;
123 }
124 if ((rate == -1 || !chan || flags & WV_DSD) && !wc->block_parsed) {
125 int64_t block_end = avio_tell(pb) + wc->header.blocksize;
126 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
128 "Cannot determine additional parameters\n");
129 return AVERROR_INVALIDDATA;
130 }
131 while (avio_tell(pb) < block_end && !avio_feof(pb)) {
132 int id, size;
133 id = avio_r8(pb);
134 size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
135 size <<= 1;
136 if (id & 0x40)
137 size--;
138 switch (id & 0x3F) {
139 case 0xD:
140 if (size <= 1) {
142 "Insufficient channel information\n");
143 return AVERROR_INVALIDDATA;
144 }
145 chan = avio_r8(pb);
146 switch (size - 2) {
147 case 0:
148 chmask = avio_r8(pb);
149 break;
150 case 1:
151 chmask = avio_rl16(pb);
152 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:
173 "Invalid channel info size %d\n", size);
174 return AVERROR_INVALIDDATA;
175 }
176 break;
177 case 0xE:
178 if (size <= 1) {
180 "Invalid DSD block\n");
181 return AVERROR_INVALIDDATA;
182 }
183 rate_x = 1U << (avio_r8(pb) & 0x1f);
184 if (size)
185 avio_skip(pb, size-1);
186 break;
187 case 0x27:
188 rate = avio_rl24(pb);
189 break;
190 default:
191 avio_skip(pb, size);
192 }
193 if (id & 0x40)
194 avio_skip(pb, 1);
195 }
196 if (rate == -1 || rate * (uint64_t)rate_x >= INT_MAX) {
198 "Cannot determine custom sampling rate\n");
199 return AVERROR_INVALIDDATA;
200 }
201 avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
202 }
203 if (!wc->bpp)
204 wc->bpp = bpp;
205 if (!wc->chan)
206 wc->chan = chan;
207 if (!wc->chmask)
208 wc->chmask = chmask;
209 if (!wc->rate)
210 wc->rate = rate * rate_x;
211
212 if (flags && bpp != wc->bpp) {
214 "Bits per sample differ, this block: %i, header block: %i\n",
215 bpp, wc->bpp);
216 return AVERROR_INVALIDDATA;
217 }
218 if (flags && !wc->multichannel && chan != wc->chan) {
220 "Channels differ, this block: %i, header block: %i\n",
221 chan, wc->chan);
222 return AVERROR_INVALIDDATA;
223 }
224 if (flags && rate != -1 && !(flags & WV_DSD) && rate * rate_x != wc->rate) {
226 "Sampling rate differ, this block: %i, header block: %i\n",
227 rate * rate_x, wc->rate);
228 return AVERROR_INVALIDDATA;
229 }
230 return 0;
231}
232
234{
235 AVIOContext *pb = s->pb;
236 WVContext *wc = s->priv_data;
237 AVStream *st;
238 int ret;
239
240 wc->block_parsed = 0;
241 for (;;) {
242 if ((ret = wv_read_block_header(s, pb)) < 0)
243 return ret;
244 if (!wc->header.samples)
245 avio_skip(pb, wc->header.blocksize);
246 else
247 break;
248 }
249
250 /* now we are ready: build format streams */
252 if (!st)
253 return AVERROR(ENOMEM);
254 if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
255 return ret;
260 st->codecpar->sample_rate = wc->rate;
262 avpriv_set_pts_info(st, 64, 1, wc->rate);
263 st->start_time = 0;
264 if (wc->header.total_samples != 0xFFFFFFFFu)
265 st->duration = wc->header.total_samples;
266
267 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
268 int64_t cur = avio_tell(s->pb);
270 if (av_dict_count(s->metadata) == 0)
272 avio_seek(s->pb, cur, SEEK_SET);
273 }
274
275 return 0;
276}
277
279{
280 WVContext *wc = s->priv_data;
281 int ret;
282 int off;
283 int64_t pos;
284 uint32_t block_samples;
285
286 if (avio_feof(s->pb))
287 return AVERROR_EOF;
288 if (wc->block_parsed) {
289 if ((ret = wv_read_block_header(s, s->pb)) < 0)
290 return ret;
291 }
292
293 pos = wc->pos;
294 if ((ret = av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE)) < 0)
295 return ret;
296 memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
297 ret = ffio_read_size(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
298 if (ret < 0) {
299 return ret;
300 }
301 while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
302 if ((ret = wv_read_block_header(s, s->pb)) < 0) {
303 return ret;
304 }
305
306 off = pkt->size;
307 if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
308 return ret;
309 }
310 memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
311
312 ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
313 if (ret != wc->header.blocksize) {
314 return (ret < 0) ? ret : AVERROR_EOF;
315 }
316 }
317 pkt->stream_index = 0;
318 pkt->pos = pos;
319 wc->block_parsed = 1;
320 pkt->pts = wc->header.block_idx;
321 block_samples = wc->header.samples;
322 if (block_samples > INT32_MAX)
324 "Too many samples in block: %"PRIu32"\n", block_samples);
325 else
326 pkt->duration = block_samples;
327
328 return 0;
329}
330
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),
339};
const FFInputFormat ff_wv_demuxer
Definition wvdec.c:331
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition apetag.c:110
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
unsigned int avio_rl24(AVIOContext *s)
Definition aviobuf.c:725
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
Public dictionary API.
enum AVCodecID id
Definition dts2pts.c:607
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_STEREO
@ AV_CODEC_ID_WAVPACK
Definition codec_id.h:478
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition packet.c:121
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition dict.c:37
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
void ff_id3v1_read(AVFormatContext *s)
Read an ID3v1 tag.
Definition id3v1.c:278
#define AV_RL32(p)
#define AV_RL16(p)
#define AV_WL16(p, v)
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define MKTAG(a, b, c, d)
Definition macros.h:55
unsigned int pos
Definition spdifenc.c:431
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
int64_t pos
Definition wvdec.c:58
uint32_t chmask
Definition wvdec.c:55
int multichannel
Definition wvdec.c:56
int block_parsed
Definition wvdec.c:57
int64_t apetag_start
Definition wvdec.c:60
int chan
Definition wvdec.c:54
WvHeader header
Definition wvdec.c:53
int rate
Definition wvdec.c:54
uint8_t block_header[WV_HEADER_SIZE]
Definition wvdec.c:52
int bpp
Definition wvdec.c:54
Definition wv.h:34
uint32_t total_samples
Definition wv.h:37
uint32_t block_idx
Definition wv.h:38
int final
Definition wv.h:43
int initial
Definition wv.h:43
uint32_t samples
Definition wv.h:39
uint32_t flags
Definition wv.h:40
uint16_t version
Definition wv.h:36
uint32_t blocksize
Definition wv.h:35
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
int size
#define WV_MONO
Definition wavpack.h:35
static const int wv_rates[16]
Definition wavpack.h:125
#define WV_HEADER_SIZE
Definition wavpack.h:33
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
Parse a WavPack block header.
Definition wv.c:30
#define WV_FLAG_FINAL_BLOCK
Definition wv.h:29
#define WV_BLOCK_LIMIT
Definition wv.h:32
static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition wvdec.c:278
#define WV_DSD
Definition wvdec.c:44
static int wv_read_header(AVFormatContext *s)
Definition wvdec.c:233
static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
Definition wvdec.c:78
static int wv_probe(const AVProbeData *p)
Definition wvdec.c:63