]> git.sesse.net Git - ffmpeg/blob - libavformat/wv.c
Revert previous commit, as it was not meant to be pushed.
[ffmpeg] / libavformat / wv.c
1 /*
2  * WavPack demuxer
3  * Copyright (c) 2006 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/intreadwrite.h"
23 #include "avformat.h"
24 #include "apetag.h"
25 #include "id3v1.h"
26
27 // specs say that maximum block size is 1Mb
28 #define WV_BLOCK_LIMIT 1047576
29
30 #define WV_EXTRA_SIZE 12
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 };
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{
52     uint32_t blksize, flags;
53     int rate, chan, bpp;
54     uint32_t samples, soff;
55     int block_parsed;
56     uint8_t extra[WV_EXTRA_SIZE];
57     int64_t pos;
58 }WVContext;
59
60 static int wv_probe(AVProbeData *p)
61 {
62     /* check file header */
63     if (p->buf_size <= 32)
64         return 0;
65     if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
66         p->buf[2] == 'p' && p->buf[3] == 'k')
67         return AVPROBE_SCORE_MAX;
68     else
69         return 0;
70 }
71
72 static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
73 {
74     WVContext *wc = ctx->priv_data;
75     uint32_t tag, ver;
76     int size;
77     int rate, bpp, chan;
78
79     wc->pos = url_ftell(pb);
80     tag = get_le32(pb);
81     if (tag != MKTAG('w', 'v', 'p', 'k'))
82         return -1;
83     size = get_le32(pb);
84     if(size < 24 || size > WV_BLOCK_LIMIT){
85         av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
86         return -1;
87     }
88     wc->blksize = size;
89     ver = get_le16(pb);
90     if(ver < 0x402 || ver > 0x410){
91         av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
92         return -1;
93     }
94     get_byte(pb); // track no
95     get_byte(pb); // track sub index
96     wc->samples = get_le32(pb); // total samples in file
97     wc->soff = get_le32(pb); // offset in samples of current block
98     get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
99     wc->flags = AV_RL32(wc->extra + 4);
100     //parse flags
101     bpp = ((wc->flags & 3) + 1) << 3;
102     chan = 1 + !(wc->flags & WV_MONO);
103     rate = wv_rates[(wc->flags >> 23) & 0xF];
104     if((wc->flags & 0x1800) != 0x1800){
105         av_log(ctx, AV_LOG_ERROR, "Multichannel WavPack is not supported yet.\n");
106         return -1;
107     }
108     if(rate == -1 && !wc->block_parsed){
109         int64_t block_end = url_ftell(pb) + wc->blksize - 24;
110         if(url_is_streamed(pb)){
111             av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
112             return -1;
113         }
114         while(url_ftell(pb) < block_end){
115             int id, size;
116             id = get_byte(pb);
117             size = (id & 0x80) ? get_le24(pb) : get_byte(pb);
118             size <<= 1;
119             if(id&0x40)
120                 size--;
121             if((id&0x3F) == 0x27){
122                 rate = get_le24(pb);
123                 break;
124             }else{
125                 url_fskip(pb, size);
126             }
127         }
128         if(rate == -1){
129             av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
130             return -1;
131         }
132         url_fseek(pb, block_end - wc->blksize + 24, SEEK_SET);
133     }
134     if(!wc->bpp) wc->bpp = bpp;
135     if(!wc->chan) wc->chan = chan;
136     if(!wc->rate) wc->rate = rate;
137
138     if(wc->flags && bpp != wc->bpp){
139         av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
140         return -1;
141     }
142     if(wc->flags && chan != wc->chan){
143         av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
144         return -1;
145     }
146     if(wc->flags && rate != -1 && rate != wc->rate){
147         av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
148         return -1;
149     }
150     wc->blksize = size - 24;
151     return 0;
152 }
153
154 static int wv_read_header(AVFormatContext *s,
155                           AVFormatParameters *ap)
156 {
157     ByteIOContext *pb = s->pb;
158     WVContext *wc = s->priv_data;
159     AVStream *st;
160
161     wc->block_parsed = 0;
162     if(wv_read_block_header(s, pb) < 0)
163         return -1;
164
165     /* now we are ready: build format streams */
166     st = av_new_stream(s, 0);
167     if (!st)
168         return -1;
169     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
170     st->codec->codec_id = CODEC_ID_WAVPACK;
171     st->codec->channels = wc->chan;
172     st->codec->sample_rate = wc->rate;
173     st->codec->bits_per_coded_sample = wc->bpp;
174     av_set_pts_info(st, 64, 1, wc->rate);
175     st->start_time = 0;
176     st->duration = wc->samples;
177
178     if(!url_is_streamed(s->pb)) {
179         int64_t cur = url_ftell(s->pb);
180         ff_ape_parse_tag(s);
181         if(!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
182             ff_id3v1_read(s);
183         url_fseek(s->pb, cur, SEEK_SET);
184     }
185
186     return 0;
187 }
188
189 static int wv_read_packet(AVFormatContext *s,
190                           AVPacket *pkt)
191 {
192     WVContext *wc = s->priv_data;
193     int ret;
194
195     if (url_feof(s->pb))
196         return AVERROR(EIO);
197     if(wc->block_parsed){
198         if(wv_read_block_header(s, s->pb) < 0)
199             return -1;
200     }
201
202     if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE) < 0)
203         return AVERROR(ENOMEM);
204     memcpy(pkt->data, wc->extra, WV_EXTRA_SIZE);
205     ret = get_buffer(s->pb, pkt->data + WV_EXTRA_SIZE, wc->blksize);
206     if(ret != wc->blksize){
207         av_free_packet(pkt);
208         return AVERROR(EIO);
209     }
210     pkt->stream_index = 0;
211     wc->block_parsed = 1;
212     pkt->size = ret + WV_EXTRA_SIZE;
213     pkt->pts = wc->soff;
214     av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
215     return 0;
216 }
217
218 static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
219 {
220     AVStream *st = s->streams[stream_index];
221     WVContext *wc = s->priv_data;
222     AVPacket pkt1, *pkt = &pkt1;
223     int ret;
224     int index = av_index_search_timestamp(st, timestamp, flags);
225     int64_t pos, pts;
226
227     /* if found, seek there */
228     if (index >= 0){
229         wc->block_parsed = 1;
230         url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
231         return 0;
232     }
233     /* if timestamp is out of bounds, return error */
234     if(timestamp < 0 || timestamp >= s->duration)
235         return -1;
236
237     pos = url_ftell(s->pb);
238     do{
239         ret = av_read_frame(s, pkt);
240         if (ret < 0){
241             url_fseek(s->pb, pos, SEEK_SET);
242             return -1;
243         }
244         pts = pkt->pts;
245         av_free_packet(pkt);
246     }while(pts < timestamp);
247     return 0;
248 }
249
250 AVInputFormat wv_demuxer = {
251     "wv",
252     NULL_IF_CONFIG_SMALL("WavPack"),
253     sizeof(WVContext),
254     wv_probe,
255     wv_read_header,
256     wv_read_packet,
257     NULL,
258     wv_read_seek,
259 };