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