]> git.sesse.net Git - ffmpeg/blob - libavformat/wv.c
Merge commit '9446d75941d639f19cfa9ae007eb4c5ca041f200'
[ffmpeg] / libavformat / wv.c
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/audioconvert.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/dict.h"
25 #include "avformat.h"
26 #include "apetag.h"
27 #include "id3v1.h"
28
29 // specs say that maximum block size is 1Mb
30 #define WV_BLOCK_LIMIT 1047576
31
32 #define WV_EXTRA_SIZE 12
33
34 #define WV_START_BLOCK  0x0800
35 #define WV_END_BLOCK    0x1000
36 #define WV_SINGLE_BLOCK (WV_START_BLOCK | WV_END_BLOCK)
37
38 enum WV_FLAGS{
39     WV_MONO   = 0x0004,
40     WV_HYBRID = 0x0008,
41     WV_JOINT  = 0x0010,
42     WV_CROSSD = 0x0020,
43     WV_HSHAPE = 0x0040,
44     WV_FLOAT  = 0x0080,
45     WV_INT32  = 0x0100,
46     WV_HBR    = 0x0200,
47     WV_HBAL   = 0x0400,
48     WV_MCINIT = 0x0800,
49     WV_MCEND  = 0x1000,
50 };
51
52 static const int wv_rates[16] = {
53      6000,  8000,  9600, 11025, 12000, 16000, 22050, 24000,
54     32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
55 };
56
57 typedef struct{
58     uint32_t blksize, flags;
59     int rate, chan, bpp;
60     uint32_t chmask;
61     uint32_t samples, soff;
62     int multichannel;
63     int block_parsed;
64     uint8_t extra[WV_EXTRA_SIZE];
65     int64_t pos;
66 }WVContext;
67
68 static int wv_probe(AVProbeData *p)
69 {
70     /* check file header */
71     if (p->buf_size <= 32)
72         return 0;
73     if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
74         p->buf[2] == 'p' && p->buf[3] == 'k')
75         return AVPROBE_SCORE_MAX;
76     else
77         return 0;
78 }
79
80 static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb, int append)
81 {
82     WVContext *wc = ctx->priv_data;
83     uint32_t tag, ver;
84     int size;
85     int rate, bpp, chan;
86     uint32_t chmask;
87
88     wc->pos = avio_tell(pb);
89     if(!append){
90         tag = avio_rl32(pb);
91         if (tag != MKTAG('w', 'v', 'p', 'k'))
92             return -1;
93         size = avio_rl32(pb);
94         if(size < 24 || size > WV_BLOCK_LIMIT){
95             av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
96             return -1;
97         }
98         wc->blksize = size;
99         ver = avio_rl16(pb);
100         if(ver < 0x402 || ver > 0x410){
101             av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
102             return -1;
103         }
104         avio_r8(pb); // track no
105         avio_r8(pb); // track sub index
106         wc->samples = avio_rl32(pb); // total samples in file
107         wc->soff = avio_rl32(pb); // offset in samples of current block
108         avio_read(pb, wc->extra, WV_EXTRA_SIZE);
109     }else{
110         size = wc->blksize;
111     }
112     wc->flags = AV_RL32(wc->extra + 4);
113     //parse flags
114     bpp = ((wc->flags & 3) + 1) << 3;
115     chan = 1 + !(wc->flags & WV_MONO);
116     chmask = wc->flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
117     rate = wv_rates[(wc->flags >> 23) & 0xF];
118     wc->multichannel = !!((wc->flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK);
119     if(wc->multichannel){
120         chan = wc->chan;
121         chmask = wc->chmask;
122     }
123     if((rate == -1 || !chan) && !wc->block_parsed){
124         int64_t block_end = avio_tell(pb) + wc->blksize - 24;
125         if(!pb->seekable){
126             av_log(ctx, AV_LOG_ERROR, "Cannot determine additional parameters\n");
127             return -1;
128         }
129         while(avio_tell(pb) < block_end){
130             int id, size;
131             id = avio_r8(pb);
132             size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
133             size <<= 1;
134             if(id&0x40)
135                 size--;
136             switch(id&0x3F){
137             case 0xD:
138                 if(size <= 1){
139                     av_log(ctx, AV_LOG_ERROR, "Insufficient channel information\n");
140                     return -1;
141                 }
142                 chan = avio_r8(pb);
143                 switch(size - 2){
144                 case 0:
145                     chmask = avio_r8(pb);
146                     break;
147                 case 1:
148                     chmask = avio_rl16(pb);
149                     break;
150                 case 2:
151                     chmask = avio_rl24(pb);
152                     break;
153                 case 3:
154                     chmask = avio_rl32(pb);
155                     break;
156                 case 5:
157                     avio_skip(pb, 1);
158                     chan |= (avio_r8(pb) & 0xF) << 8;
159                     chmask = avio_rl24(pb);
160                     break;
161                 default:
162                     av_log(ctx, AV_LOG_ERROR, "Invalid channel info size %d\n", size);
163                     return -1;
164                 }
165                 break;
166             case 0x27:
167                 rate = avio_rl24(pb);
168                 break;
169             default:
170                 avio_skip(pb, size);
171             }
172             if(id&0x40)
173                 avio_skip(pb, 1);
174         }
175         if(rate == -1){
176             av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
177             return -1;
178         }
179         avio_seek(pb, block_end - wc->blksize + 24, SEEK_SET);
180     }
181     if(!wc->bpp) wc->bpp = bpp;
182     if(!wc->chan) wc->chan = chan;
183     if(!wc->chmask) wc->chmask = chmask;
184     if(!wc->rate) wc->rate = rate;
185
186     if(wc->flags && bpp != wc->bpp){
187         av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
188         return -1;
189     }
190     if(wc->flags && !wc->multichannel && chan != wc->chan){
191         av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
192         return -1;
193     }
194     if(wc->flags && rate != -1 && rate != wc->rate){
195         av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
196         return -1;
197     }
198     wc->blksize = size - 24;
199     return 0;
200 }
201
202 static int wv_read_header(AVFormatContext *s,
203                           AVFormatParameters *ap)
204 {
205     AVIOContext *pb = s->pb;
206     WVContext *wc = s->priv_data;
207     AVStream *st;
208
209     wc->block_parsed = 0;
210     if(wv_read_block_header(s, pb, 0) < 0)
211         return -1;
212
213     /* now we are ready: build format streams */
214     st = av_new_stream(s, 0);
215     if (!st)
216         return -1;
217     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
218     st->codec->codec_id = CODEC_ID_WAVPACK;
219     st->codec->channels = wc->chan;
220     st->codec->channel_layout = wc->chmask;
221     st->codec->sample_rate = wc->rate;
222     st->codec->bits_per_coded_sample = wc->bpp;
223     av_set_pts_info(st, 64, 1, wc->rate);
224     st->start_time = 0;
225     st->duration = wc->samples;
226
227     if(s->pb->seekable) {
228         int64_t cur = avio_tell(s->pb);
229         ff_ape_parse_tag(s);
230         if(!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
231             ff_id3v1_read(s);
232         avio_seek(s->pb, cur, SEEK_SET);
233     }
234
235     return 0;
236 }
237
238 static int wv_read_packet(AVFormatContext *s,
239                           AVPacket *pkt)
240 {
241     WVContext *wc = s->priv_data;
242     int ret;
243     int size, ver, off;
244
245     if (url_feof(s->pb))
246         return AVERROR(EIO);
247     if(wc->block_parsed){
248         if(wv_read_block_header(s, s->pb, 0) < 0)
249             return -1;
250     }
251
252     off = wc->multichannel ? 4 : 0;
253     if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE + off) < 0)
254         return AVERROR(ENOMEM);
255     if(wc->multichannel)
256         AV_WL32(pkt->data, wc->blksize + WV_EXTRA_SIZE + 12);
257     memcpy(pkt->data + off, wc->extra, WV_EXTRA_SIZE);
258     ret = avio_read(s->pb, pkt->data + WV_EXTRA_SIZE + off, wc->blksize);
259     if(ret != wc->blksize){
260         av_free_packet(pkt);
261         return AVERROR(EIO);
262     }
263     while(!(wc->flags & WV_END_BLOCK)){
264         if(avio_rl32(s->pb) != MKTAG('w', 'v', 'p', 'k')){
265             av_free_packet(pkt);
266             return -1;
267         }
268         if((ret = av_append_packet(s->pb, pkt, 4)) < 0){
269             av_free_packet(pkt);
270             return ret;
271         }
272         size = AV_RL32(pkt->data + pkt->size - 4);
273         if(size < 24 || size > WV_BLOCK_LIMIT){
274             av_free_packet(pkt);
275             av_log(s, AV_LOG_ERROR, "Incorrect block size %d\n", size);
276             return -1;
277         }
278         wc->blksize = size;
279         ver = avio_rl16(s->pb);
280         if(ver < 0x402 || ver > 0x410){
281             av_free_packet(pkt);
282             av_log(s, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
283             return -1;
284         }
285         avio_r8(s->pb); // track no
286         avio_r8(s->pb); // track sub index
287         wc->samples = avio_rl32(s->pb); // total samples in file
288         wc->soff = avio_rl32(s->pb); // offset in samples of current block
289         if((ret = av_append_packet(s->pb, pkt, WV_EXTRA_SIZE)) < 0){
290             av_free_packet(pkt);
291             return ret;
292         }
293         memcpy(wc->extra, pkt->data + pkt->size - WV_EXTRA_SIZE, WV_EXTRA_SIZE);
294
295         if(wv_read_block_header(s, s->pb, 1) < 0){
296             av_free_packet(pkt);
297             return -1;
298         }
299         ret = av_append_packet(s->pb, pkt, wc->blksize);
300         if(ret < 0){
301             av_free_packet(pkt);
302             return ret;
303         }
304     }
305     pkt->stream_index = 0;
306     wc->block_parsed = 1;
307     pkt->pts = wc->soff;
308     av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
309     return 0;
310 }
311
312 static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
313 {
314     AVStream *st = s->streams[stream_index];
315     WVContext *wc = s->priv_data;
316     AVPacket pkt1, *pkt = &pkt1;
317     int ret;
318     int index = av_index_search_timestamp(st, timestamp, flags);
319     int64_t pos, pts;
320
321     /* if found, seek there */
322     if (index >= 0){
323         wc->block_parsed = 1;
324         avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
325         return 0;
326     }
327     /* if timestamp is out of bounds, return error */
328     if(timestamp < 0 || timestamp >= s->duration)
329         return -1;
330
331     pos = avio_tell(s->pb);
332     do{
333         ret = av_read_frame(s, pkt);
334         if (ret < 0){
335             avio_seek(s->pb, pos, SEEK_SET);
336             return -1;
337         }
338         pts = pkt->pts;
339         av_free_packet(pkt);
340     }while(pts < timestamp);
341     return 0;
342 }
343
344 AVInputFormat ff_wv_demuxer = {
345     "wv",
346     NULL_IF_CONFIG_SMALL("WavPack"),
347     sizeof(WVContext),
348     wv_probe,
349     wv_read_header,
350     wv_read_packet,
351     NULL,
352     wv_read_seek,
353 };