annotate wv.c @ 5353:70956fb584b3 libavformat

10l: WavPack demuxer forgot to seek back to initial position after block contents parsing (for custom sampling rate).
author kostya
date Fri, 06 Nov 2009 18:25:45 +0000
parents e5805e11f596
children 28094e9bd013
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
1 /*
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
2 * WavPack demuxer
4251
77e0c7511d41 cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents: 4237
diff changeset
3 * Copyright (c) 2006 Konstantin Shishkov
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
4 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1324
diff changeset
5 * This file is part of FFmpeg.
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1324
diff changeset
6 *
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1324
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1324
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
11 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1324
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
15 * Lesser General Public License for more details.
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
16 *
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1324
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
20 */
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
21
4201
7d2f3f1b68d8 Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
diego
parents: 3908
diff changeset
22 #include "libavutil/intreadwrite.h"
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
23 #include "avformat.h"
5300
e5c9ae62af86 Read metadata in WavPack files.
kostya
parents: 4909
diff changeset
24 #include "apetag.h"
e5c9ae62af86 Read metadata in WavPack files.
kostya
parents: 4909
diff changeset
25 #include "id3v1.h"
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
26
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
27 // specs say that maximum block size is 1Mb
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
28 #define WV_BLOCK_LIMIT 1047576
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
29
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
30 #define WV_EXTRA_SIZE 12
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
31
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
32 enum WV_FLAGS{
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
33 WV_MONO = 0x0004,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
34 WV_HYBRID = 0x0008,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
35 WV_JOINT = 0x0010,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
36 WV_CROSSD = 0x0020,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
37 WV_HSHAPE = 0x0040,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
38 WV_FLOAT = 0x0080,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
39 WV_INT32 = 0x0100,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
40 WV_HBR = 0x0200,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
41 WV_HBAL = 0x0400,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
42 WV_MCINIT = 0x0800,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
43 WV_MCEND = 0x1000,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
44 };
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
45
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
46 static const int wv_rates[16] = {
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
47 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
48 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
49 };
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
50
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
51 typedef struct{
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
52 uint32_t blksize, flags;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
53 int rate, chan, bpp;
1743
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
54 uint32_t samples, soff;
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
55 int block_parsed;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
56 uint8_t extra[WV_EXTRA_SIZE];
1743
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
57 int64_t pos;
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
58 }WVContext;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
59
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
60 static int wv_probe(AVProbeData *p)
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
61 {
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
62 /* check file header */
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
63 if (p->buf_size <= 32)
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
64 return 0;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
65 if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
66 p->buf[2] == 'p' && p->buf[3] == 'k')
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
67 return AVPROBE_SCORE_MAX;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
68 else
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
69 return 0;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
70 }
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
71
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
72 static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
73 {
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
74 WVContext *wc = ctx->priv_data;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
75 uint32_t tag, ver;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
76 int size;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
77 int rate, bpp, chan;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
78
1743
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
79 wc->pos = url_ftell(pb);
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
80 tag = get_le32(pb);
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
81 if (tag != MKTAG('w', 'v', 'p', 'k'))
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
82 return -1;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
83 size = get_le32(pb);
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
84 if(size < 24 || size > WV_BLOCK_LIMIT){
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
85 av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
86 return -1;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
87 }
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
88 wc->blksize = size;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
89 ver = get_le16(pb);
2380
41c9871ea3c4 Support for WavPack version 0x410 (false stereo chunks)
kostya
parents: 2314
diff changeset
90 if(ver < 0x402 || ver > 0x410){
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
91 av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
92 return -1;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
93 }
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
94 get_byte(pb); // track no
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
95 get_byte(pb); // track sub index
1743
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
96 wc->samples = get_le32(pb); // total samples in file
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
97 wc->soff = get_le32(pb); // offset in samples of current block
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
98 get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
1673
a782462e2497 rename BE/LE_8/16/32 to AV_RL/B_8/16/32
alex
parents: 1391
diff changeset
99 wc->flags = AV_RL32(wc->extra + 4);
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
100 //parse flags
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
101 bpp = ((wc->flags & 3) + 1) << 3;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
102 chan = 1 + !(wc->flags & WV_MONO);
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
103 rate = wv_rates[(wc->flags >> 23) & 0xF];
5350
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
104 if(rate == -1 && !wc->block_parsed){
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
105 int64_t block_end = url_ftell(pb) + wc->blksize - 24;
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
106 if(url_is_streamed(pb)){
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
107 av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
108 return -1;
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
109 }
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
110 while(url_ftell(pb) < block_end){
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
111 int id, size;
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
112 id = get_byte(pb);
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
113 size = (id & 0x80) ? get_le24(pb) : get_byte(pb);
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
114 size <<= 1;
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
115 if(id&0x40)
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
116 size--;
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
117 if((id&0x3F) == 0x27){
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
118 rate = get_le24(pb);
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
119 break;
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
120 }else{
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
121 url_fskip(pb, size);
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
122 }
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
123 }
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
124 if(rate == -1){
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
125 av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
126 return -1;
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
127 }
5353
70956fb584b3 10l: WavPack demuxer forgot to seek back to initial position after block
kostya
parents: 5350
diff changeset
128 url_fseek(pb, block_end - wc->blksize + 24, SEEK_SET);
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
129 }
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
130 if(!wc->bpp) wc->bpp = bpp;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
131 if(!wc->chan) wc->chan = chan;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
132 if(!wc->rate) wc->rate = rate;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
133
1389
9ed80abc8eb7 Ignore blocks with no samples and flags (but usually with MD5 sum)
kostya
parents: 1387
diff changeset
134 if(wc->flags && bpp != wc->bpp){
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
135 av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
136 return -1;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
137 }
1389
9ed80abc8eb7 Ignore blocks with no samples and flags (but usually with MD5 sum)
kostya
parents: 1387
diff changeset
138 if(wc->flags && chan != wc->chan){
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
139 av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
140 return -1;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
141 }
5350
e5805e11f596 If custom sampling rate is set in WavPack file, parse first block to find
kostya
parents: 5349
diff changeset
142 if(wc->flags && rate != -1 && rate != wc->rate){
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
143 av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
144 return -1;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
145 }
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
146 wc->blksize = size - 24;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
147 return 0;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
148 }
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
149
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
150 static int wv_read_header(AVFormatContext *s,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
151 AVFormatParameters *ap)
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
152 {
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2555
diff changeset
153 ByteIOContext *pb = s->pb;
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
154 WVContext *wc = s->priv_data;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
155 AVStream *st;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
156
5349
d2cdd13aacff Initialize block_parsed before reading first block header in WavPack demuxer,
kostya
parents: 5300
diff changeset
157 wc->block_parsed = 0;
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
158 if(wv_read_block_header(s, pb) < 0)
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
159 return -1;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
160
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
161 /* now we are ready: build format streams */
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
162 st = av_new_stream(s, 0);
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
163 if (!st)
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
164 return -1;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
165 st->codec->codec_type = CODEC_TYPE_AUDIO;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
166 st->codec->codec_id = CODEC_ID_WAVPACK;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
167 st->codec->channels = wc->chan;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
168 st->codec->sample_rate = wc->rate;
3908
1d3d17de20ba Bump Major version, this commit is almost just renaming bits_per_sample to
michael
parents: 3482
diff changeset
169 st->codec->bits_per_coded_sample = wc->bpp;
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
170 av_set_pts_info(st, 64, 1, wc->rate);
1743
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
171 s->start_time = 0;
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
172 s->duration = (int64_t)wc->samples * AV_TIME_BASE / st->codec->sample_rate;
5300
e5c9ae62af86 Read metadata in WavPack files.
kostya
parents: 4909
diff changeset
173
e5c9ae62af86 Read metadata in WavPack files.
kostya
parents: 4909
diff changeset
174 if(!url_is_streamed(s->pb)) {
e5c9ae62af86 Read metadata in WavPack files.
kostya
parents: 4909
diff changeset
175 int64_t cur = url_ftell(s->pb);
e5c9ae62af86 Read metadata in WavPack files.
kostya
parents: 4909
diff changeset
176 ff_ape_parse_tag(s);
e5c9ae62af86 Read metadata in WavPack files.
kostya
parents: 4909
diff changeset
177 if(!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
e5c9ae62af86 Read metadata in WavPack files.
kostya
parents: 4909
diff changeset
178 ff_id3v1_read(s);
e5c9ae62af86 Read metadata in WavPack files.
kostya
parents: 4909
diff changeset
179 url_fseek(s->pb, cur, SEEK_SET);
e5c9ae62af86 Read metadata in WavPack files.
kostya
parents: 4909
diff changeset
180 }
e5c9ae62af86 Read metadata in WavPack files.
kostya
parents: 4909
diff changeset
181
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
182 return 0;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
183 }
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
184
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
185 static int wv_read_packet(AVFormatContext *s,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
186 AVPacket *pkt)
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
187 {
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
188 WVContext *wc = s->priv_data;
1391
669d33f6dde5 Remove unused variable.
diego
parents: 1389
diff changeset
189 int ret;
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
190
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2555
diff changeset
191 if (url_feof(s->pb))
1787
eb16c64144ee This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents: 1743
diff changeset
192 return AVERROR(EIO);
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
193 if(wc->block_parsed){
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2555
diff changeset
194 if(wv_read_block_header(s, s->pb) < 0)
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
195 return -1;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
196 }
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
197
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
198 if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE) < 0)
2273
7eb456c4ed8a Replace all occurrences of AVERROR_NOMEM with AVERROR(ENOMEM).
takis
parents: 1787
diff changeset
199 return AVERROR(ENOMEM);
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
200 memcpy(pkt->data, wc->extra, WV_EXTRA_SIZE);
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2555
diff changeset
201 ret = get_buffer(s->pb, pkt->data + WV_EXTRA_SIZE, wc->blksize);
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
202 if(ret != wc->blksize){
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
203 av_free_packet(pkt);
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 2273
diff changeset
204 return AVERROR(EIO);
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
205 }
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
206 pkt->stream_index = 0;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
207 wc->block_parsed = 1;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
208 pkt->size = ret + WV_EXTRA_SIZE;
1743
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
209 pkt->pts = wc->soff;
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
210 av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
211 return 0;
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
212 }
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
213
1743
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
214 static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
215 {
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
216 AVStream *st = s->streams[stream_index];
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
217 WVContext *wc = s->priv_data;
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
218 AVPacket pkt1, *pkt = &pkt1;
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
219 int ret;
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
220 int index = av_index_search_timestamp(st, timestamp, flags);
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
221 int64_t pos, pts;
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
222
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
223 /* if found, seek there */
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
224 if (index >= 0){
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
225 wc->block_parsed = 1;
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2555
diff changeset
226 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
1743
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
227 return 0;
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
228 }
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
229 /* if timestamp is out of bounds, return error */
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
230 if(timestamp < 0 || timestamp >= s->duration)
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
231 return -1;
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
232
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2555
diff changeset
233 pos = url_ftell(s->pb);
1743
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
234 do{
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
235 ret = av_read_frame(s, pkt);
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
236 if (ret < 0){
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2555
diff changeset
237 url_fseek(s->pb, pos, SEEK_SET);
1743
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
238 return -1;
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
239 }
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
240 pts = pkt->pts;
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
241 av_free_packet(pkt);
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
242 }while(pts < timestamp);
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
243 return 0;
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
244 }
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
245
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
246 AVInputFormat wv_demuxer = {
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
247 "wv",
3424
7a0230981402 Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents: 3286
diff changeset
248 NULL_IF_CONFIG_SMALL("WavPack"),
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
249 sizeof(WVContext),
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
250 wv_probe,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
251 wv_read_header,
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
252 wv_read_packet,
3482
17c0ce4cc314 remove useless close func
bcoudurier
parents: 3424
diff changeset
253 NULL,
1743
75909fc5b5a9 Seeking support in WavPack
kostya
parents: 1673
diff changeset
254 wv_read_seek,
1324
024cf76ac4d1 WavPack lossless audio decoder
kostya
parents:
diff changeset
255 };