]> git.sesse.net Git - ffmpeg/blob - libavformat/wvdec.c
vp9_raw_reorder_bsf: Remove a redundant allocation
[ffmpeg] / libavformat / wvdec.c
1 /*
2  * WavPack demuxer
3  * Copyright (c) 2006,2011 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 };
44
45 static const int wv_rates[16] = {
46      6000,  8000,  9600, 11025, 12000, 16000,  22050, 24000,
47     32000, 44100, 48000, 64000, 88200, 96000, 192000,    -1
48 };
49
50 typedef struct WVContext {
51     uint8_t block_header[WV_HEADER_SIZE];
52     WvHeader header;
53     int rate, chan, bpp;
54     uint32_t chmask;
55     int multichannel;
56     int block_parsed;
57     int64_t pos;
58
59     int64_t apetag_start;
60 } WVContext;
61
62 static int wv_probe(AVProbeData *p)
63 {
64     /* check file header */
65     if (p->buf_size <= 32)
66         return 0;
67     if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
68         p->buf[2] == 'p' && p->buf[3] == 'k')
69         return AVPROBE_SCORE_MAX;
70     else
71         return 0;
72 }
73
74 static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
75 {
76     WVContext *wc = ctx->priv_data;
77     int ret;
78     int rate, bpp, chan;
79     uint32_t chmask, flags;
80
81     wc->pos = avio_tell(pb);
82
83     /* don't return bogus packets with the ape tag data */
84     if (wc->apetag_start && wc->pos >= wc->apetag_start)
85         return AVERROR_EOF;
86
87     ret = avio_read(pb, wc->block_header, WV_HEADER_SIZE);
88     if (ret != WV_HEADER_SIZE)
89         return (ret < 0) ? ret : AVERROR_EOF;
90
91     ret = ff_wv_parse_header(&wc->header, wc->block_header);
92     if (ret < 0) {
93         av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
94         return ret;
95     }
96
97     if (wc->header.version < 0x402 || wc->header.version > 0x410) {
98         avpriv_report_missing_feature(ctx, "WV version 0x%03X",
99                                       wc->header.version);
100         return AVERROR_PATCHWELCOME;
101     }
102
103     /* Blocks with zero samples don't contain actual audio information
104      * and should be ignored */
105     if (!wc->header.samples)
106         return 0;
107     // parse flags
108     flags  = wc->header.flags;
109     bpp    = ((flags & 3) + 1) << 3;
110     chan   = 1 + !(flags & WV_MONO);
111     chmask = flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
112     rate   = wv_rates[(flags >> 23) & 0xF];
113     wc->multichannel = !(wc->header.initial && wc->header.final);
114     if (wc->multichannel) {
115         chan   = wc->chan;
116         chmask = wc->chmask;
117     }
118     if ((rate == -1 || !chan) && !wc->block_parsed) {
119         int64_t block_end = avio_tell(pb) + wc->header.blocksize;
120         if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
121             av_log(ctx, AV_LOG_ERROR,
122                    "Cannot determine additional parameters\n");
123             return AVERROR_INVALIDDATA;
124         }
125         while (avio_tell(pb) < block_end) {
126             int id, size;
127             id   = avio_r8(pb);
128             size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
129             size <<= 1;
130             if (id & 0x40)
131                 size--;
132             switch (id & 0x3F) {
133             case 0xD:
134                 if (size <= 1) {
135                     av_log(ctx, AV_LOG_ERROR,
136                            "Insufficient channel information\n");
137                     return AVERROR_INVALIDDATA;
138                 }
139                 chan = avio_r8(pb);
140                 switch (size - 2) {
141                 case 0:
142                     chmask = avio_r8(pb);
143                     break;
144                 case 1:
145                     chmask = avio_rl16(pb);
146                     break;
147                 case 2:
148                     chmask = avio_rl24(pb);
149                     break;
150                 case 3:
151                     chmask = avio_rl32(pb);
152                     break;
153                 case 5:
154                     avio_skip(pb, 1);
155                     chan  |= (avio_r8(pb) & 0xF) << 8;
156                     chmask = avio_rl24(pb);
157                     break;
158                 default:
159                     av_log(ctx, AV_LOG_ERROR,
160                            "Invalid channel info size %d\n", size);
161                     return AVERROR_INVALIDDATA;
162                 }
163                 break;
164             case 0x27:
165                 rate = avio_rl24(pb);
166                 break;
167             default:
168                 avio_skip(pb, size);
169             }
170             if (id & 0x40)
171                 avio_skip(pb, 1);
172         }
173         if (rate == -1) {
174             av_log(ctx, AV_LOG_ERROR,
175                    "Cannot determine custom sampling rate\n");
176             return AVERROR_INVALIDDATA;
177         }
178         avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
179     }
180     if (!wc->bpp)
181         wc->bpp    = bpp;
182     if (!wc->chan)
183         wc->chan   = chan;
184     if (!wc->chmask)
185         wc->chmask = chmask;
186     if (!wc->rate)
187         wc->rate   = rate;
188
189     if (flags && bpp != wc->bpp) {
190         av_log(ctx, AV_LOG_ERROR,
191                "Bits per sample differ, this block: %i, header block: %i\n",
192                bpp, wc->bpp);
193         return AVERROR_INVALIDDATA;
194     }
195     if (flags && !wc->multichannel && chan != wc->chan) {
196         av_log(ctx, AV_LOG_ERROR,
197                "Channels differ, this block: %i, header block: %i\n",
198                chan, wc->chan);
199         return AVERROR_INVALIDDATA;
200     }
201     if (flags && rate != -1 && rate != wc->rate) {
202         av_log(ctx, AV_LOG_ERROR,
203                "Sampling rate differ, this block: %i, header block: %i\n",
204                rate, wc->rate);
205         return AVERROR_INVALIDDATA;
206     }
207     return 0;
208 }
209
210 static int wv_read_header(AVFormatContext *s)
211 {
212     AVIOContext *pb = s->pb;
213     WVContext *wc = s->priv_data;
214     AVStream *st;
215     int ret;
216
217     wc->block_parsed = 0;
218     for (;;) {
219         if ((ret = wv_read_block_header(s, pb)) < 0)
220             return ret;
221         if (!wc->header.samples)
222             avio_skip(pb, wc->header.blocksize);
223         else
224             break;
225     }
226
227     /* now we are ready: build format streams */
228     st = avformat_new_stream(s, NULL);
229     if (!st)
230         return AVERROR(ENOMEM);
231     st->codecpar->codec_type            = AVMEDIA_TYPE_AUDIO;
232     st->codecpar->codec_id              = AV_CODEC_ID_WAVPACK;
233     st->codecpar->channels              = wc->chan;
234     st->codecpar->channel_layout        = wc->chmask;
235     st->codecpar->sample_rate           = wc->rate;
236     st->codecpar->bits_per_coded_sample = wc->bpp;
237     avpriv_set_pts_info(st, 64, 1, wc->rate);
238     st->start_time = 0;
239     st->duration   = wc->header.total_samples;
240
241     if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
242         int64_t cur = avio_tell(s->pb);
243         wc->apetag_start = ff_ape_parse_tag(s);
244         if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
245             ff_id3v1_read(s);
246         avio_seek(s->pb, cur, SEEK_SET);
247     }
248
249     return 0;
250 }
251
252 static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
253 {
254     WVContext *wc = s->priv_data;
255     int ret;
256     int off;
257     int64_t pos;
258     uint32_t block_samples;
259
260     if (s->pb->eof_reached)
261         return AVERROR_EOF;
262     if (wc->block_parsed) {
263         if ((ret = wv_read_block_header(s, s->pb)) < 0)
264             return ret;
265     }
266
267     pos = wc->pos;
268     if (av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE) < 0)
269         return AVERROR(ENOMEM);
270     memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
271     ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
272     if (ret != wc->header.blocksize) {
273         av_packet_unref(pkt);
274         return AVERROR(EIO);
275     }
276     while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
277         if ((ret = wv_read_block_header(s, s->pb)) < 0) {
278             av_packet_unref(pkt);
279             return ret;
280         }
281
282         off = pkt->size;
283         if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
284             av_packet_unref(pkt);
285             return ret;
286         }
287         memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
288
289         ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
290         if (ret != wc->header.blocksize) {
291             av_packet_unref(pkt);
292             return (ret < 0) ? ret : AVERROR_EOF;
293         }
294     }
295     pkt->stream_index = 0;
296     wc->block_parsed  = 1;
297     pkt->pts          = wc->header.block_idx;
298     block_samples     = wc->header.samples;
299     if (block_samples > INT32_MAX)
300         av_log(s, AV_LOG_WARNING,
301                "Too many samples in block: %"PRIu32"\n", block_samples);
302     else
303         pkt->duration = block_samples;
304
305     av_add_index_entry(s->streams[0], pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
306     return 0;
307 }
308
309 static int wv_read_seek(AVFormatContext *s, int stream_index,
310                         int64_t timestamp, int flags)
311 {
312     AVStream  *st = s->streams[stream_index];
313     WVContext *wc = s->priv_data;
314     AVPacket pkt1, *pkt = &pkt1;
315     int ret;
316     int index = av_index_search_timestamp(st, timestamp, flags);
317     int64_t pos, pts;
318
319     /* if found, seek there */
320     if (index >= 0 &&
321         timestamp <= st->index_entries[st->nb_index_entries - 1].timestamp) {
322         wc->block_parsed = 1;
323         avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
324         return 0;
325     }
326     /* if timestamp is out of bounds, return error */
327     if (timestamp < 0 || timestamp >= s->duration)
328         return AVERROR(EINVAL);
329
330     pos = avio_tell(s->pb);
331     do {
332         ret = av_read_frame(s, pkt);
333         if (ret < 0) {
334             avio_seek(s->pb, pos, SEEK_SET);
335             return ret;
336         }
337         pts = pkt->pts;
338         av_packet_unref(pkt);
339     } while(pts < timestamp);
340     return 0;
341 }
342
343 AVInputFormat ff_wv_demuxer = {
344     .name           = "wv",
345     .long_name      = NULL_IF_CONFIG_SMALL("WavPack"),
346     .priv_data_size = sizeof(WVContext),
347     .read_probe     = wv_probe,
348     .read_header    = wv_read_header,
349     .read_packet    = wv_read_packet,
350     .read_seek      = wv_read_seek,
351 };