Mercurial > libavformat.hg
annotate wv.c @ 1967:8c20d8c19e1f libavformat
fix demuxing of IMAG0002.ASF
author | michael |
---|---|
date | Fri, 30 Mar 2007 16:20:40 +0000 |
parents | eb16c64144ee |
children | 7eb456c4ed8a |
rev | line source |
---|---|
1324 | 1 /* |
2 * WavPack demuxer | |
3 * Copyright (c) 2006 Konstantin Shishkov. | |
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 | 8 * modify it under the terms of the GNU Lesser General Public |
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 | 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 | 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 | |
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 | 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 */ | |
21 | |
22 #include "avformat.h" | |
23 #include "allformats.h" | |
24 #include "bswap.h" | |
25 | |
26 // specs say that maximum block size is 1Mb | |
27 #define WV_BLOCK_LIMIT 1047576 | |
28 | |
29 #define WV_EXTRA_SIZE 12 | |
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{ | |
51 uint32_t blksize, flags; | |
52 int rate, chan, bpp; | |
1743 | 53 uint32_t samples, soff; |
1324 | 54 int block_parsed; |
55 uint8_t extra[WV_EXTRA_SIZE]; | |
1743 | 56 int64_t pos; |
1324 | 57 }WVContext; |
58 | |
59 static int wv_probe(AVProbeData *p) | |
60 { | |
61 /* check file header */ | |
62 if (p->buf_size <= 32) | |
63 return 0; | |
64 if (p->buf[0] == 'w' && p->buf[1] == 'v' && | |
65 p->buf[2] == 'p' && p->buf[3] == 'k') | |
66 return AVPROBE_SCORE_MAX; | |
67 else | |
68 return 0; | |
69 } | |
70 | |
71 static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb) | |
72 { | |
73 WVContext *wc = ctx->priv_data; | |
74 uint32_t tag, ver; | |
75 int size; | |
76 int rate, bpp, chan; | |
77 | |
1743 | 78 wc->pos = url_ftell(pb); |
1324 | 79 tag = get_le32(pb); |
80 if (tag != MKTAG('w', 'v', 'p', 'k')) | |
81 return -1; | |
82 size = get_le32(pb); | |
83 if(size < 24 || size > WV_BLOCK_LIMIT){ | |
84 av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size); | |
85 return -1; | |
86 } | |
87 wc->blksize = size; | |
88 ver = get_le16(pb); | |
89 if(ver < 0x402 || ver > 0x40F){ | |
90 av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver); | |
91 return -1; | |
92 } | |
93 get_byte(pb); // track no | |
94 get_byte(pb); // track sub index | |
1743 | 95 wc->samples = get_le32(pb); // total samples in file |
96 wc->soff = get_le32(pb); // offset in samples of current block | |
1324 | 97 get_buffer(pb, wc->extra, WV_EXTRA_SIZE); |
1673 | 98 wc->flags = AV_RL32(wc->extra + 4); |
1324 | 99 //parse flags |
100 if(wc->flags & WV_FLOAT){ | |
101 av_log(ctx, AV_LOG_ERROR, "Floating point data is not supported\n"); | |
102 return -1; | |
103 } | |
104 if(wc->flags & WV_HYBRID){ | |
105 av_log(ctx, AV_LOG_ERROR, "Hybrid coding mode is not supported\n"); | |
106 return -1; | |
107 } | |
108 if(wc->flags & WV_INT32){ | |
109 av_log(ctx, AV_LOG_ERROR, "Integer point data is not supported\n"); | |
110 return -1; | |
111 } | |
112 | |
113 bpp = ((wc->flags & 3) + 1) << 3; | |
114 chan = 1 + !(wc->flags & WV_MONO); | |
115 rate = wv_rates[(wc->flags >> 23) & 0xF]; | |
116 if(rate == -1){ | |
117 av_log(ctx, AV_LOG_ERROR, "Unknown sampling rate\n"); | |
118 return -1; | |
119 } | |
120 if(!wc->bpp) wc->bpp = bpp; | |
121 if(!wc->chan) wc->chan = chan; | |
122 if(!wc->rate) wc->rate = rate; | |
123 | |
1389
9ed80abc8eb7
Ignore blocks with no samples and flags (but usually with MD5 sum)
kostya
parents:
1387
diff
changeset
|
124 if(wc->flags && bpp != wc->bpp){ |
1324 | 125 av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp); |
126 return -1; | |
127 } | |
1389
9ed80abc8eb7
Ignore blocks with no samples and flags (but usually with MD5 sum)
kostya
parents:
1387
diff
changeset
|
128 if(wc->flags && chan != wc->chan){ |
1324 | 129 av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan); |
130 return -1; | |
131 } | |
1389
9ed80abc8eb7
Ignore blocks with no samples and flags (but usually with MD5 sum)
kostya
parents:
1387
diff
changeset
|
132 if(wc->flags && rate != wc->rate){ |
1324 | 133 av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate); |
134 return -1; | |
135 } | |
136 wc->blksize = size - 24; | |
137 return 0; | |
138 } | |
139 | |
140 static int wv_read_header(AVFormatContext *s, | |
141 AVFormatParameters *ap) | |
142 { | |
143 ByteIOContext *pb = &s->pb; | |
144 WVContext *wc = s->priv_data; | |
145 AVStream *st; | |
146 | |
147 if(wv_read_block_header(s, pb) < 0) | |
148 return -1; | |
149 | |
150 wc->block_parsed = 0; | |
151 /* now we are ready: build format streams */ | |
152 st = av_new_stream(s, 0); | |
153 if (!st) | |
154 return -1; | |
155 st->codec->codec_type = CODEC_TYPE_AUDIO; | |
156 st->codec->codec_id = CODEC_ID_WAVPACK; | |
157 st->codec->channels = wc->chan; | |
158 st->codec->sample_rate = wc->rate; | |
159 st->codec->bits_per_sample = wc->bpp; | |
160 av_set_pts_info(st, 64, 1, wc->rate); | |
1743 | 161 s->start_time = 0; |
162 s->duration = (int64_t)wc->samples * AV_TIME_BASE / st->codec->sample_rate; | |
1324 | 163 return 0; |
164 } | |
165 | |
166 static int wv_read_packet(AVFormatContext *s, | |
167 AVPacket *pkt) | |
168 { | |
169 WVContext *wc = s->priv_data; | |
1391 | 170 int ret; |
1324 | 171 |
172 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
|
173 return AVERROR(EIO); |
1324 | 174 if(wc->block_parsed){ |
175 if(wv_read_block_header(s, &s->pb) < 0) | |
176 return -1; | |
177 } | |
178 | |
179 if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE) < 0) | |
180 return AVERROR_NOMEM; | |
181 memcpy(pkt->data, wc->extra, WV_EXTRA_SIZE); | |
182 ret = get_buffer(&s->pb, pkt->data + WV_EXTRA_SIZE, wc->blksize); | |
183 if(ret != wc->blksize){ | |
184 av_free_packet(pkt); | |
185 return AVERROR_IO; | |
186 } | |
187 pkt->stream_index = 0; | |
188 wc->block_parsed = 1; | |
189 pkt->size = ret + WV_EXTRA_SIZE; | |
1743 | 190 pkt->pts = wc->soff; |
191 av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME); | |
1324 | 192 return 0; |
193 } | |
194 | |
195 static int wv_read_close(AVFormatContext *s) | |
196 { | |
197 return 0; | |
198 } | |
199 | |
1743 | 200 static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) |
201 { | |
202 AVStream *st = s->streams[stream_index]; | |
203 WVContext *wc = s->priv_data; | |
204 AVPacket pkt1, *pkt = &pkt1; | |
205 int ret; | |
206 int index = av_index_search_timestamp(st, timestamp, flags); | |
207 int64_t pos, pts; | |
208 | |
209 /* if found, seek there */ | |
210 if (index >= 0){ | |
211 wc->block_parsed = 1; | |
212 url_fseek(&s->pb, st->index_entries[index].pos, SEEK_SET); | |
213 return 0; | |
214 } | |
215 /* if timestamp is out of bounds, return error */ | |
216 if(timestamp < 0 || timestamp >= s->duration) | |
217 return -1; | |
218 | |
219 pos = url_ftell(&s->pb); | |
220 do{ | |
221 ret = av_read_frame(s, pkt); | |
222 if (ret < 0){ | |
223 url_fseek(&s->pb, pos, SEEK_SET); | |
224 return -1; | |
225 } | |
226 pts = pkt->pts; | |
227 av_free_packet(pkt); | |
228 }while(pts < timestamp); | |
229 return 0; | |
230 } | |
231 | |
1324 | 232 AVInputFormat wv_demuxer = { |
233 "wv", | |
234 "WavPack", | |
235 sizeof(WVContext), | |
236 wv_probe, | |
237 wv_read_header, | |
238 wv_read_packet, | |
239 wv_read_close, | |
1743 | 240 wv_read_seek, |
1324 | 241 }; |