Mercurial > libavformat.hg
annotate mpc8.c @ 5204:9dea59f5436a libavformat
Enhance Musepack SV8 probing code
author | kostya |
---|---|
date | Wed, 16 Sep 2009 04:43:33 +0000 |
parents | 06903f8f7247 |
children | af13c900ff26 |
rev | line source |
---|---|
2709 | 1 /* |
2 * Musepack SV8 demuxer | |
3 * Copyright (c) 2007 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 */ | |
3286 | 21 |
4872 | 22 #include "libavcodec/get_bits.h" |
3286 | 23 #include "libavcodec/unary.h" |
2709 | 24 #include "avformat.h" |
25 | |
26 /// Two-byte MPC tag | |
27 #define MKMPCTAG(a, b) (a | (b << 8)) | |
28 | |
29 #define TAG_MPCK MKTAG('M','P','C','K') | |
30 | |
31 /// Reserved MPC tags | |
32 enum MPCPacketTags{ | |
33 TAG_STREAMHDR = MKMPCTAG('S','H'), | |
34 TAG_STREAMEND = MKMPCTAG('S','E'), | |
35 | |
36 TAG_AUDIOPACKET = MKMPCTAG('A','P'), | |
37 | |
38 TAG_SEEKTBLOFF = MKMPCTAG('S','O'), | |
39 TAG_SEEKTABLE = MKMPCTAG('S','T'), | |
40 | |
41 TAG_REPLAYGAIN = MKMPCTAG('R','G'), | |
42 TAG_ENCINFO = MKMPCTAG('E','I'), | |
43 }; | |
44 | |
45 static const int mpc8_rate[8] = { 44100, 48000, 37800, 32000, -1, -1, -1, -1 }; | |
46 | |
47 typedef struct { | |
48 int ver; | |
49 int frame; | |
50 int64_t header_pos; | |
51 int64_t samples; | |
52 } MPCContext; | |
53 | |
54 static int mpc8_probe(AVProbeData *p) | |
55 { | |
5204 | 56 if (p->buf_size < 16) |
57 return 0; | |
58 if (AV_RL32(p->buf) != TAG_MPCK) | |
59 return 0; | |
60 if (p->buf[4] == 'S' && p->buf[5] == 'H') { | |
61 int size = p->buf[6]; | |
62 | |
63 if (size < 12 || size > 30) | |
64 return 0; | |
65 if (!AV_RL32(&p->buf[7])) //zero CRC is invalid | |
66 return 0; | |
67 //check whether some tag follows stream header or not | |
68 if (p->buf[4 + size] < 'A' || p->buf[4 + size] > 'Z') | |
69 return 0; | |
70 if (p->buf[5 + size] < 'A' || p->buf[5 + size] > 'Z') | |
71 return 0; | |
72 if (p->buf[6 + size] < 3) | |
73 return 0; | |
2709 | 74 return AVPROBE_SCORE_MAX; |
5204 | 75 } |
76 /* file magic number should be followed by tag name which consists of | |
77 two uppercase letters */ | |
78 if (p->buf[4] < 'A' || p->buf[4] > 'Z' || p->buf[5] < 'A' || p->buf[5] > 'Z') | |
79 return 0; | |
80 // tag size should be >= 3 | |
81 if (p->buf[6] < 3) | |
82 return 0; | |
83 // if first tag is not stream header, that's suspicious | |
84 return AVPROBE_SCORE_MAX / 4; | |
2709 | 85 } |
86 | |
87 static inline int64_t gb_get_v(GetBitContext *gb) | |
88 { | |
89 int64_t v = 0; | |
90 int bits = 0; | |
91 while(get_bits1(gb) && bits < 64-7){ | |
92 v <<= 7; | |
93 v |= get_bits(gb, 7); | |
94 bits += 7; | |
95 } | |
96 v <<= 7; | |
97 v |= get_bits(gb, 7); | |
98 | |
99 return v; | |
100 } | |
101 | |
102 static void mpc8_get_chunk_header(ByteIOContext *pb, int *tag, int64_t *size) | |
103 { | |
104 int64_t pos; | |
105 pos = url_ftell(pb); | |
106 *tag = get_le16(pb); | |
107 *size = ff_get_v(pb); | |
108 *size -= url_ftell(pb) - pos; | |
109 } | |
110 | |
111 static void mpc8_parse_seektable(AVFormatContext *s, int64_t off) | |
112 { | |
113 MPCContext *c = s->priv_data; | |
114 int tag; | |
115 int64_t size, pos, ppos[2]; | |
116 uint8_t *buf; | |
117 int i, t, seekd; | |
118 GetBitContext gb; | |
119 | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2709
diff
changeset
|
120 url_fseek(s->pb, off, SEEK_SET); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2709
diff
changeset
|
121 mpc8_get_chunk_header(s->pb, &tag, &size); |
2709 | 122 if(tag != TAG_SEEKTABLE){ |
123 av_log(s, AV_LOG_ERROR, "No seek table at given position\n"); | |
124 return; | |
125 } | |
5030
06903f8f7247
Allocate a bit more memory for MPC SV8 seek table, so bitreader won't read
kostya
parents:
4872
diff
changeset
|
126 if(!(buf = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE))) |
2709 | 127 return; |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2709
diff
changeset
|
128 get_buffer(s->pb, buf, size); |
2709 | 129 init_get_bits(&gb, buf, size * 8); |
130 size = gb_get_v(&gb); | |
131 if(size > UINT_MAX/4 || size > c->samples/1152){ | |
132 av_log(s, AV_LOG_ERROR, "Seek table is too big\n"); | |
133 return; | |
134 } | |
135 seekd = get_bits(&gb, 4); | |
136 for(i = 0; i < 2; i++){ | |
137 pos = gb_get_v(&gb) + c->header_pos; | |
138 ppos[1 - i] = pos; | |
139 av_add_index_entry(s->streams[0], pos, i, 0, 0, AVINDEX_KEYFRAME); | |
140 } | |
141 for(; i < size; i++){ | |
142 t = get_unary(&gb, 1, 33) << 12; | |
143 t += get_bits(&gb, 12); | |
144 if(t & 1) | |
145 t = -(t & ~1); | |
146 pos = (t >> 1) + ppos[0]*2 - ppos[1]; | |
147 av_add_index_entry(s->streams[0], pos, i << seekd, 0, 0, AVINDEX_KEYFRAME); | |
148 ppos[1] = ppos[0]; | |
149 ppos[0] = pos; | |
150 } | |
151 av_free(buf); | |
152 } | |
153 | |
154 static void mpc8_handle_chunk(AVFormatContext *s, int tag, int64_t chunk_pos, int64_t size) | |
155 { | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2709
diff
changeset
|
156 ByteIOContext *pb = s->pb; |
2709 | 157 int64_t pos, off; |
158 | |
159 switch(tag){ | |
160 case TAG_SEEKTBLOFF: | |
161 pos = url_ftell(pb) + size; | |
162 off = ff_get_v(pb); | |
163 mpc8_parse_seektable(s, chunk_pos + off); | |
164 url_fseek(pb, pos, SEEK_SET); | |
165 break; | |
166 default: | |
167 url_fskip(pb, size); | |
168 } | |
169 } | |
170 | |
171 static int mpc8_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
172 { | |
173 MPCContext *c = s->priv_data; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2709
diff
changeset
|
174 ByteIOContext *pb = s->pb; |
2709 | 175 AVStream *st; |
176 int tag = 0; | |
177 int64_t size, pos; | |
178 | |
179 c->header_pos = url_ftell(pb); | |
180 if(get_le32(pb) != TAG_MPCK){ | |
181 av_log(s, AV_LOG_ERROR, "Not a Musepack8 file\n"); | |
182 return -1; | |
183 } | |
184 | |
185 while(!url_feof(pb)){ | |
186 pos = url_ftell(pb); | |
187 mpc8_get_chunk_header(pb, &tag, &size); | |
188 if(tag == TAG_STREAMHDR) | |
189 break; | |
190 mpc8_handle_chunk(s, tag, pos, size); | |
191 } | |
192 if(tag != TAG_STREAMHDR){ | |
193 av_log(s, AV_LOG_ERROR, "Stream header not found\n"); | |
194 return -1; | |
195 } | |
196 pos = url_ftell(pb); | |
197 url_fskip(pb, 4); //CRC | |
198 c->ver = get_byte(pb); | |
199 if(c->ver != 8){ | |
200 av_log(s, AV_LOG_ERROR, "Unknown stream version %d\n", c->ver); | |
201 return -1; | |
202 } | |
203 c->samples = ff_get_v(pb); | |
204 ff_get_v(pb); //silence samples at the beginning | |
205 | |
206 st = av_new_stream(s, 0); | |
207 if (!st) | |
208 return AVERROR(ENOMEM); | |
209 st->codec->codec_type = CODEC_TYPE_AUDIO; | |
210 st->codec->codec_id = CODEC_ID_MUSEPACK8; | |
3908
1d3d17de20ba
Bump Major version, this commit is almost just renaming bits_per_sample to
michael
parents:
3424
diff
changeset
|
211 st->codec->bits_per_coded_sample = 16; |
2709 | 212 |
213 st->codec->extradata_size = 2; | |
214 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
215 get_buffer(pb, st->codec->extradata, st->codec->extradata_size); | |
216 | |
217 st->codec->channels = (st->codec->extradata[1] >> 4) + 1; | |
218 st->codec->sample_rate = mpc8_rate[st->codec->extradata[0] >> 5]; | |
219 av_set_pts_info(st, 32, 1152 << (st->codec->extradata[1]&3)*2, st->codec->sample_rate); | |
220 st->duration = c->samples / (1152 << (st->codec->extradata[1]&3)*2); | |
221 size -= url_ftell(pb) - pos; | |
222 | |
223 return 0; | |
224 } | |
225 | |
226 static int mpc8_read_packet(AVFormatContext *s, AVPacket *pkt) | |
227 { | |
228 MPCContext *c = s->priv_data; | |
229 int tag; | |
230 int64_t pos, size; | |
231 | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2709
diff
changeset
|
232 while(!url_feof(s->pb)){ |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2709
diff
changeset
|
233 pos = url_ftell(s->pb); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2709
diff
changeset
|
234 mpc8_get_chunk_header(s->pb, &tag, &size); |
2709 | 235 if(tag == TAG_AUDIOPACKET){ |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2709
diff
changeset
|
236 if(av_get_packet(s->pb, pkt, size) < 0) |
2709 | 237 return AVERROR(ENOMEM); |
238 pkt->stream_index = 0; | |
239 pkt->pts = c->frame; | |
240 return 0; | |
241 } | |
242 if(tag == TAG_STREAMEND) | |
243 return AVERROR(EIO); | |
244 mpc8_handle_chunk(s, tag, pos, size); | |
245 } | |
246 return 0; | |
247 } | |
248 | |
249 static int mpc8_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) | |
250 { | |
251 AVStream *st = s->streams[stream_index]; | |
252 MPCContext *c = s->priv_data; | |
253 int index = av_index_search_timestamp(st, timestamp, flags); | |
254 | |
255 if(index < 0) return -1; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2709
diff
changeset
|
256 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET); |
2709 | 257 c->frame = st->index_entries[index].timestamp; |
258 return 0; | |
259 } | |
260 | |
261 | |
262 AVInputFormat mpc8_demuxer = { | |
263 "mpc8", | |
3424
7a0230981402
Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents:
3286
diff
changeset
|
264 NULL_IF_CONFIG_SMALL("Musepack SV8"), |
2709 | 265 sizeof(MPCContext), |
266 mpc8_probe, | |
267 mpc8_read_header, | |
268 mpc8_read_packet, | |
269 NULL, | |
270 mpc8_read_seek, | |
271 }; |