1324
|
1 /*
|
|
2 * WavPack demuxer
|
|
3 * Copyright (c) 2006 Konstantin Shishkov.
|
|
4 *
|
|
5 * This library is free software; you can redistribute it and/or
|
|
6 * modify it under the terms of the GNU Lesser General Public
|
|
7 * License as published by the Free Software Foundation; either
|
|
8 * version 2 of the License, or (at your option) any later version.
|
|
9 *
|
|
10 * This library is distributed in the hope that it will be useful,
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 * Lesser General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU Lesser General Public
|
|
16 * License along with this library; if not, write to the Free Software
|
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18 */
|
|
19
|
|
20 #include "avformat.h"
|
|
21 #include "allformats.h"
|
|
22 #include "bswap.h"
|
|
23
|
|
24 // specs say that maximum block size is 1Mb
|
|
25 #define WV_BLOCK_LIMIT 1047576
|
|
26
|
|
27 #define WV_EXTRA_SIZE 12
|
|
28
|
|
29 enum WV_FLAGS{
|
|
30 WV_MONO = 0x0004,
|
|
31 WV_HYBRID = 0x0008,
|
|
32 WV_JOINT = 0x0010,
|
|
33 WV_CROSSD = 0x0020,
|
|
34 WV_HSHAPE = 0x0040,
|
|
35 WV_FLOAT = 0x0080,
|
|
36 WV_INT32 = 0x0100,
|
|
37 WV_HBR = 0x0200,
|
|
38 WV_HBAL = 0x0400,
|
|
39 WV_MCINIT = 0x0800,
|
|
40 WV_MCEND = 0x1000,
|
|
41 };
|
|
42
|
|
43 static const int wv_rates[16] = {
|
|
44 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
|
|
45 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
|
|
46 };
|
|
47
|
|
48 typedef struct{
|
|
49 uint32_t blksize, flags;
|
|
50 int rate, chan, bpp;
|
|
51 int block_parsed;
|
|
52 uint8_t extra[WV_EXTRA_SIZE];
|
|
53 }WVContext;
|
|
54
|
|
55 static int wv_probe(AVProbeData *p)
|
|
56 {
|
|
57 /* check file header */
|
|
58 if (p->buf_size <= 32)
|
|
59 return 0;
|
|
60 if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
|
|
61 p->buf[2] == 'p' && p->buf[3] == 'k')
|
|
62 return AVPROBE_SCORE_MAX;
|
|
63 else
|
|
64 return 0;
|
|
65 }
|
|
66
|
|
67 static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
|
|
68 {
|
|
69 WVContext *wc = ctx->priv_data;
|
|
70 uint32_t tag, ver;
|
|
71 int size;
|
|
72 int rate, bpp, chan;
|
|
73
|
|
74 tag = get_le32(pb);
|
|
75 if (tag != MKTAG('w', 'v', 'p', 'k'))
|
|
76 return -1;
|
|
77 size = get_le32(pb);
|
|
78 if(size < 24 || size > WV_BLOCK_LIMIT){
|
|
79 av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
|
|
80 return -1;
|
|
81 }
|
|
82 wc->blksize = size;
|
|
83 ver = get_le16(pb);
|
|
84 if(ver < 0x402 || ver > 0x40F){
|
|
85 av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
|
|
86 return -1;
|
|
87 }
|
|
88 get_byte(pb); // track no
|
|
89 get_byte(pb); // track sub index
|
|
90 get_le32(pb); // total samples in file
|
|
91 get_le32(pb); // offset in samples of current block
|
|
92 get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
|
|
93 wc->flags = LE_32(wc->extra + 4);
|
|
94 //parse flags
|
|
95 if(wc->flags & WV_FLOAT){
|
|
96 av_log(ctx, AV_LOG_ERROR, "Floating point data is not supported\n");
|
|
97 return -1;
|
|
98 }
|
|
99 if(wc->flags & WV_HYBRID){
|
|
100 av_log(ctx, AV_LOG_ERROR, "Hybrid coding mode is not supported\n");
|
|
101 return -1;
|
|
102 }
|
|
103 if(wc->flags & WV_INT32){
|
|
104 av_log(ctx, AV_LOG_ERROR, "Integer point data is not supported\n");
|
|
105 return -1;
|
|
106 }
|
|
107
|
|
108 bpp = ((wc->flags & 3) + 1) << 3;
|
|
109 chan = 1 + !(wc->flags & WV_MONO);
|
|
110 rate = wv_rates[(wc->flags >> 23) & 0xF];
|
|
111 if(rate == -1){
|
|
112 av_log(ctx, AV_LOG_ERROR, "Unknown sampling rate\n");
|
|
113 return -1;
|
|
114 }
|
|
115 if(!wc->bpp) wc->bpp = bpp;
|
|
116 if(!wc->chan) wc->chan = chan;
|
|
117 if(!wc->rate) wc->rate = rate;
|
|
118
|
|
119 if(bpp != wc->bpp){
|
|
120 av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
|
|
121 return -1;
|
|
122 }
|
|
123 if(chan != wc->chan){
|
|
124 av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
|
|
125 return -1;
|
|
126 }
|
|
127 if(rate != wc->rate){
|
|
128 av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
|
|
129 return -1;
|
|
130 }
|
|
131 wc->blksize = size - 24;
|
|
132 return 0;
|
|
133 }
|
|
134
|
|
135 static int wv_read_header(AVFormatContext *s,
|
|
136 AVFormatParameters *ap)
|
|
137 {
|
|
138 ByteIOContext *pb = &s->pb;
|
|
139 WVContext *wc = s->priv_data;
|
|
140 AVStream *st;
|
|
141
|
|
142 if(wv_read_block_header(s, pb) < 0)
|
|
143 return -1;
|
|
144
|
|
145 wc->block_parsed = 0;
|
|
146 /* now we are ready: build format streams */
|
|
147 st = av_new_stream(s, 0);
|
|
148 if (!st)
|
|
149 return -1;
|
|
150 st->codec->codec_type = CODEC_TYPE_AUDIO;
|
|
151 st->codec->codec_id = CODEC_ID_WAVPACK;
|
|
152 st->codec->channels = wc->chan;
|
|
153 st->codec->sample_rate = wc->rate;
|
|
154 st->codec->bits_per_sample = wc->bpp;
|
|
155 av_set_pts_info(st, 64, 1, wc->rate);
|
|
156 return 0;
|
|
157 }
|
|
158
|
|
159 static int wv_read_packet(AVFormatContext *s,
|
|
160 AVPacket *pkt)
|
|
161 {
|
|
162 WVContext *wc = s->priv_data;
|
|
163 int ret, samples;
|
|
164
|
|
165 if (url_feof(&s->pb))
|
|
166 return -EIO;
|
|
167 if(wc->block_parsed){
|
|
168 if(wv_read_block_header(s, &s->pb) < 0)
|
|
169 return -1;
|
|
170 }
|
|
171
|
|
172 samples = LE_32(wc->extra);
|
|
173 /* should not happen but who knows */
|
|
174 if(samples * 2 * wc->chan > AVCODEC_MAX_AUDIO_FRAME_SIZE){
|
|
175 av_log(s, AV_LOG_ERROR, "Packet size is too big to be handled in lavc!\n");
|
|
176 return -EIO;
|
|
177 }
|
|
178 if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE) < 0)
|
|
179 return AVERROR_NOMEM;
|
|
180 memcpy(pkt->data, wc->extra, WV_EXTRA_SIZE);
|
|
181 ret = get_buffer(&s->pb, pkt->data + WV_EXTRA_SIZE, wc->blksize);
|
|
182 if(ret != wc->blksize){
|
|
183 av_free_packet(pkt);
|
|
184 return AVERROR_IO;
|
|
185 }
|
|
186 pkt->stream_index = 0;
|
|
187 wc->block_parsed = 1;
|
|
188 pkt->size = ret + WV_EXTRA_SIZE;
|
|
189
|
|
190 return 0;
|
|
191 }
|
|
192
|
|
193 static int wv_read_close(AVFormatContext *s)
|
|
194 {
|
|
195 return 0;
|
|
196 }
|
|
197
|
|
198 AVInputFormat wv_demuxer = {
|
|
199 "wv",
|
|
200 "WavPack",
|
|
201 sizeof(WVContext),
|
|
202 wv_probe,
|
|
203 wv_read_header,
|
|
204 wv_read_packet,
|
|
205 wv_read_close,
|
|
206 pcm_read_seek,
|
|
207 };
|