]> git.sesse.net Git - ffmpeg/blob - libavformat/wvdec.c
avcodec/dvbsubdec: prefer to use variable instead of type for sizeof
[ffmpeg] / libavformat / wvdec.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/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/dict.h"
25 #include "avformat.h"
26 #include "internal.h"
27 #include "apetag.h"
28 #include "id3v1.h"
29 #include "wv.h"
30
31 enum WV_FLAGS {
32     WV_MONO   = 0x0004,
33     WV_HYBRID = 0x0008,
34     WV_JOINT  = 0x0010,
35     WV_CROSSD = 0x0020,
36     WV_HSHAPE = 0x0040,
37     WV_FLOAT  = 0x0080,
38     WV_INT32  = 0x0100,
39     WV_HBR    = 0x0200,
40     WV_HBAL   = 0x0400,
41     WV_MCINIT = 0x0800,
42     WV_MCEND  = 0x1000,
43     WV_DSD    = 0x80000000,
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 WVContext {
52     uint8_t block_header[WV_HEADER_SIZE];
53     WvHeader header;
54     int rate, chan, bpp;
55     uint32_t chmask;
56     int multichannel;
57     int block_parsed;
58     int64_t pos;
59
60     int64_t apetag_start;
61 } WVContext;
62
63 static 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
78 static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
79 {
80     WVContext *wc = ctx->priv_data;
81     int ret;
82     int rate, rate_x, bpp, chan;
83     uint32_t chmask, flags;
84
85     wc->pos = avio_tell(pb);
86
87     /* don't return bogus packets with the ape tag data */
88     if (wc->apetag_start && wc->pos >= wc->apetag_start)
89         return AVERROR_EOF;
90
91     ret = avio_read(pb, wc->block_header, WV_HEADER_SIZE);
92     if (ret != WV_HEADER_SIZE)
93         return (ret < 0) ? ret : AVERROR_EOF;
94
95     ret = ff_wv_parse_header(&wc->header, wc->block_header);
96     if (ret < 0) {
97         av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
98         return ret;
99     }
100
101     if (wc->header.version < 0x402 || wc->header.version > 0x410) {
102         avpriv_report_missing_feature(ctx, "WV version 0x%03X",
103                                       wc->header.version);
104         return AVERROR_PATCHWELCOME;
105     }
106
107     /* Blocks with zero samples don't contain actual audio information
108      * and should be ignored */
109     if (!wc->header.samples)
110         return 0;
111     // parse flags
112     flags  = wc->header.flags;
113     rate_x = (flags & WV_DSD) ? 4 : 1;
114     bpp    = (flags & WV_DSD) ? 0 : ((flags & 3) + 1) << 3;
115     chan   = 1 + !(flags & WV_MONO);
116     chmask = flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
117     rate   = wv_rates[(flags >> 23) & 0xF];
118     wc->multichannel = !(wc->header.initial && wc->header.final);
119     if (wc->multichannel) {
120         chan   = wc->chan;
121         chmask = wc->chmask;
122     }
123     if ((rate == -1 || !chan || flags & WV_DSD) && !wc->block_parsed) {
124         int64_t block_end = avio_tell(pb) + wc->header.blocksize;
125         if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
126             av_log(ctx, AV_LOG_ERROR,
127                    "Cannot determine additional parameters\n");
128             return AVERROR_INVALIDDATA;
129         }
130         while (avio_tell(pb) < block_end && !avio_feof(pb)) {
131             int id, size;
132             id   = avio_r8(pb);
133             size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
134             size <<= 1;
135             if (id & 0x40)
136                 size--;
137             switch (id & 0x3F) {
138             case 0xD:
139                 if (size <= 1) {
140                     av_log(ctx, AV_LOG_ERROR,
141                            "Insufficient channel information\n");
142                     return AVERROR_INVALIDDATA;
143                 }
144                 chan = avio_r8(pb);
145                 switch (size - 2) {
146                 case 0:
147                     chmask = avio_r8(pb);
148                     break;
149                 case 1:
150                     chmask = avio_rl16(pb);
151                     break;
152                 case 2:
153                     chmask = avio_rl24(pb);
154                     break;
155                 case 3:
156                     chmask = avio_rl32(pb);
157                     break;
158                 case 4:
159                     avio_skip(pb, 1);
160                     chan  |= (avio_r8(pb) & 0xF) << 8;
161                     chan  += 1;
162                     chmask = avio_rl24(pb);
163                     break;
164                 case 5:
165                     avio_skip(pb, 1);
166                     chan  |= (avio_r8(pb) & 0xF) << 8;
167                     chan  += 1;
168                     chmask = avio_rl32(pb);
169                     break;
170                 default:
171                     av_log(ctx, AV_LOG_ERROR,
172                            "Invalid channel info size %d\n", size);
173                     return AVERROR_INVALIDDATA;
174                 }
175                 break;
176             case 0xE:
177                 if (size <= 1) {
178                     av_log(ctx, AV_LOG_ERROR,
179                            "Invalid DSD block\n");
180                     return AVERROR_INVALIDDATA;
181                 }
182                 rate_x = 1U << (avio_r8(pb) & 0x1f);
183                 if (size)
184                     avio_skip(pb, size-1);
185                 break;
186             case 0x27:
187                 rate = avio_rl24(pb);
188                 break;
189             default:
190                 avio_skip(pb, size);
191             }
192             if (id & 0x40)
193                 avio_skip(pb, 1);
194         }
195         if (rate == -1) {
196             av_log(ctx, AV_LOG_ERROR,
197                    "Cannot determine custom sampling rate\n");
198             return AVERROR_INVALIDDATA;
199         }
200         avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
201     }
202     if (!wc->bpp)
203         wc->bpp    = bpp;
204     if (!wc->chan)
205         wc->chan   = chan;
206     if (!wc->chmask)
207         wc->chmask = chmask;
208     if (!wc->rate)
209         wc->rate   = rate * rate_x;
210
211     if (flags && bpp != wc->bpp) {
212         av_log(ctx, AV_LOG_ERROR,
213                "Bits per sample differ, this block: %i, header block: %i\n",
214                bpp, wc->bpp);
215         return AVERROR_INVALIDDATA;
216     }
217     if (flags && !wc->multichannel && chan != wc->chan) {
218         av_log(ctx, AV_LOG_ERROR,
219                "Channels differ, this block: %i, header block: %i\n",
220                chan, wc->chan);
221         return AVERROR_INVALIDDATA;
222     }
223     if (flags && rate != -1 && !(flags & WV_DSD) && rate * rate_x != wc->rate) {
224         av_log(ctx, AV_LOG_ERROR,
225                "Sampling rate differ, this block: %i, header block: %i\n",
226                rate * rate_x, wc->rate);
227         return AVERROR_INVALIDDATA;
228     }
229     return 0;
230 }
231
232 static int wv_read_header(AVFormatContext *s)
233 {
234     AVIOContext *pb = s->pb;
235     WVContext *wc = s->priv_data;
236     AVStream *st;
237     int ret;
238
239     wc->block_parsed = 0;
240     for (;;) {
241         if ((ret = wv_read_block_header(s, pb)) < 0)
242             return ret;
243         if (!wc->header.samples)
244             avio_skip(pb, wc->header.blocksize);
245         else
246             break;
247     }
248
249     /* now we are ready: build format streams */
250     st = avformat_new_stream(s, NULL);
251     if (!st)
252         return AVERROR(ENOMEM);
253     if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
254         return ret;
255     AV_WL16(st->codecpar->extradata, wc->header.version);
256     st->codecpar->codec_type            = AVMEDIA_TYPE_AUDIO;
257     st->codecpar->codec_id              = AV_CODEC_ID_WAVPACK;
258     st->codecpar->channels              = wc->chan;
259     st->codecpar->channel_layout        = wc->chmask;
260     st->codecpar->sample_rate           = wc->rate;
261     st->codecpar->bits_per_coded_sample = wc->bpp;
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);
269         wc->apetag_start = ff_ape_parse_tag(s);
270         if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
271             ff_id3v1_read(s);
272         avio_seek(s->pb, cur, SEEK_SET);
273     }
274
275     return 0;
276 }
277
278 static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
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 = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
298     if (ret != wc->header.blocksize) {
299         return AVERROR(EIO);
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)
323         av_log(s, AV_LOG_WARNING,
324                "Too many samples in block: %"PRIu32"\n", block_samples);
325     else
326         pkt->duration = block_samples;
327
328     return 0;
329 }
330
331 AVInputFormat ff_wv_demuxer = {
332     .name           = "wv",
333     .long_name      = NULL_IF_CONFIG_SMALL("WavPack"),
334     .priv_data_size = sizeof(WVContext),
335     .read_probe     = wv_probe,
336     .read_header    = wv_read_header,
337     .read_packet    = wv_read_packet,
338     .flags          = AVFMT_GENERIC_INDEX,
339 };