Mercurial > mplayer.hg
annotate libmpdemux/demux_audio.c @ 4720:10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
author | melanson |
---|---|
date | Sat, 16 Feb 2002 05:45:16 +0000 |
parents | b7054dadd2c0 |
children | 9579496a91db |
rev | line source |
---|---|
4694 | 1 |
2 #include "config.h" | |
4712 | 3 #include "../mp_msg.h" |
4694 | 4 |
5 #include <stdlib.h> | |
6 #include <stdio.h> | |
7 #include "stream.h" | |
8 #include "demuxer.h" | |
9 #include "stheader.h" | |
10 #include "genres.h" | |
11 | |
12 #include <string.h> | |
13 #ifdef MP_DEBUG | |
14 #include <assert.h> | |
15 #endif | |
16 | |
17 #define MP3 1 | |
18 #define WAV 2 | |
19 | |
20 | |
21 #define HDR_SIZE 4 | |
22 | |
23 typedef struct da_priv { | |
24 int frmt; | |
25 } da_priv_t; | |
26 | |
27 extern int mp_decode_mp3_header(unsigned char* hbuf); | |
28 extern void free_sh_audio(sh_audio_t* sh); | |
4700
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
29 extern void resync_audio_stream(sh_audio_t *sh_audio); |
4694 | 30 |
31 | |
32 int demux_audio_open(demuxer_t* demuxer) { | |
33 stream_t *s; | |
34 sh_audio_t* sh_audio; | |
35 uint8_t hdr[HDR_SIZE]; | |
36 int st_pos = 0,frmt = 0, n = 0, pos = 0, step; | |
37 da_priv_t* priv; | |
38 #ifdef MP_DEBUG | |
39 assert(demuxer != NULL); | |
40 assert(demuxer->stream != NULL); | |
41 #endif | |
42 | |
43 s = demuxer->stream; | |
44 | |
45 while(n < 5 && ! s->eof) { | |
46 st_pos = stream_tell(s); | |
47 step = 1; | |
48 if(pos < HDR_SIZE) { | |
49 stream_read(s,&hdr[pos],HDR_SIZE-pos); | |
50 pos = HDR_SIZE; | |
51 } | |
52 | |
53 if( hdr[0] == 'R' && hdr[1] == 'I' && hdr[2] == 'F' && hdr[3] == 'F' ) { | |
54 stream_skip(s,4); | |
55 if(s->eof) | |
56 break; | |
57 stream_read(s,hdr,4); | |
58 if(s->eof) | |
59 break; | |
60 if(hdr[0] != 'W' || hdr[1] != 'A' || hdr[2] != 'V' || hdr[3] != 'E' ) | |
61 stream_skip(s,-8); | |
62 else | |
63 // We found wav header. Now we can have 'fmt ' or a mp3 header | |
64 // empty the buffer | |
65 step = 4; | |
66 } else if( hdr[0] == 'f' && hdr[1] == 'm' && hdr[2] == 't' && hdr[3] == ' ' ) { | |
67 frmt = WAV; | |
68 break; | |
4717
b7054dadd2c0
We should skip exactly the first frame on mp3 files.
albeu
parents:
4712
diff
changeset
|
69 } else if((n = mp_decode_mp3_header(hdr)) > 0) { |
4694 | 70 frmt = MP3; |
71 break; | |
72 } | |
73 // Add here some other audio format detection | |
74 if(step < HDR_SIZE) | |
75 memmove(hdr,&hdr[step],HDR_SIZE-step); | |
76 pos -= step; | |
77 } | |
78 | |
79 if(!frmt) | |
80 return 0; | |
81 | |
82 sh_audio = new_sh_audio(demuxer,0); | |
83 | |
84 switch(frmt) { | |
85 case MP3: | |
86 sh_audio->format = 0x55; | |
4717
b7054dadd2c0
We should skip exactly the first frame on mp3 files.
albeu
parents:
4712
diff
changeset
|
87 demuxer->movi_start = st_pos-HDR_SIZE+n; |
4700
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
88 for(n = 0; n < 5 ; n++) { |
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
89 pos = mp_decode_mp3_header(hdr); |
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
90 if(pos < 0) |
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
91 return 0; |
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
92 stream_skip(s,pos-4); |
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
93 if(s->eof) |
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
94 return 0; |
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
95 stream_read(s,hdr,4); |
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
96 if(s->eof) |
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
97 return 0; |
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
98 } |
4694 | 99 if(s->end_pos) { |
100 char tag[4]; | |
101 stream_seek(s,s->end_pos-128); | |
102 stream_read(s,tag,3); | |
103 tag[3] = '\0'; | |
104 if(strcmp(tag,"TAG")) | |
105 demuxer->movi_end = s->end_pos; | |
106 else { | |
107 char buf[31]; | |
108 uint8_t g; | |
109 demuxer->movi_end = stream_tell(s)-3; | |
110 stream_read(s,buf,30); | |
111 buf[30] = '\0'; | |
112 demux_info_add(demuxer,"Title",buf); | |
113 stream_read(s,buf,30); | |
114 buf[30] = '\0'; | |
115 demux_info_add(demuxer,"Artist",buf); | |
116 stream_read(s,buf,30); | |
117 buf[30] = '\0'; | |
118 demux_info_add(demuxer,"Album",buf); | |
119 stream_read(s,buf,4); | |
120 buf[5] = '\0'; | |
121 demux_info_add(demuxer,"Year",buf); | |
122 stream_read(s,buf,30); | |
123 buf[30] = '\0'; | |
124 demux_info_add(demuxer,"Comment",buf); | |
125 if(buf[28] == 0 && buf[29] != 0) { | |
126 uint8_t trk = (uint8_t)buf[29]; | |
127 sprintf(buf,"%d",trk); | |
128 demux_info_add(demuxer,"Track",buf); | |
129 } | |
130 g = stream_read_char(s); | |
131 demux_info_add(demuxer,"Genre",genres[g]); | |
132 } | |
133 } | |
134 break; | |
135 case WAV: { | |
4720
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
136 unsigned int chunk_type; |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
137 unsigned int chunk_size; |
4694 | 138 WAVEFORMATEX* w; |
139 int l; | |
140 sh_audio->wf = w = (WAVEFORMATEX*)malloc(sizeof(WAVEFORMATEX)); | |
141 l = stream_read_dword_le(s); | |
142 if(l < 16) { | |
143 printf("Bad wav header length : too short !!!\n"); | |
144 free_sh_audio(sh_audio); | |
145 return 0; | |
146 } | |
147 w->wFormatTag = sh_audio->format = stream_read_word_le(s); | |
148 w->nChannels = sh_audio->channels = stream_read_word_le(s); | |
149 w->nSamplesPerSec = sh_audio->samplerate = stream_read_dword_le(s); | |
150 w->nAvgBytesPerSec = stream_read_dword_le(s); | |
151 w->nBlockAlign = stream_read_word_le(s); | |
152 w->wBitsPerSample = sh_audio->samplesize = stream_read_word_le(s); | |
153 w->cbSize = 0; | |
154 l -= 16; | |
155 if(l) | |
156 stream_skip(s,l); | |
4720
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
157 do |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
158 { |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
159 chunk_type = stream_read_fourcc(demuxer->stream); |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
160 chunk_size = stream_read_dword_le(demuxer->stream); |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
161 if (chunk_type != mmioFOURCC('d', 'a', 't', 'a')) |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
162 stream_skip(demuxer->stream, chunk_size); |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
163 } while (chunk_type != mmioFOURCC('d', 'a', 't', 'a')); |
4694 | 164 demuxer->movi_start = stream_tell(s); |
165 demuxer->movi_end = s->end_pos; | |
166 } break; | |
167 } | |
168 | |
169 priv = (da_priv_t*)malloc(sizeof(da_priv_t)); | |
170 priv->frmt = frmt; | |
171 demuxer->priv = priv; | |
172 demuxer->audio->id = 0; | |
173 demuxer->audio->sh = sh_audio; | |
174 sh_audio->ds = demuxer->audio; | |
175 | |
176 if(stream_tell(s) != demuxer->movi_start) | |
177 stream_seek(s,demuxer->movi_start); | |
178 | |
4712 | 179 mp_msg(MSGT_DEMUX,MSGL_V,"demux_audio: audio data 0x%X - 0x%X \n",demuxer->movi_start,demuxer->movi_end); |
180 | |
4694 | 181 return 1; |
182 } | |
183 | |
184 | |
185 int demux_audio_fill_buffer(demux_stream_t *ds) { | |
186 sh_audio_t* sh_audio; | |
187 demuxer_t* demux; | |
188 da_priv_t* priv; | |
189 stream_t* s; | |
190 #ifdef MP_DEBUG | |
191 assert(ds != NULL); | |
192 assert(ds->sh != NULL); | |
193 assert(ds->demuxer != NULL); | |
194 #endif | |
195 sh_audio = ds->sh; | |
196 demux = ds->demuxer; | |
197 priv = demux->priv; | |
198 s = demux->stream; | |
199 | |
200 if(s->eof || (demux->movi_end && stream_tell(s) >= demux->movi_end) ) | |
201 return 0; | |
202 | |
203 switch(priv->frmt) { | |
204 case MP3 : | |
205 while(! s->eof || (demux->movi_end && stream_tell(s) >= demux->movi_end) ) { | |
206 uint8_t hdr[4]; | |
207 int len; | |
208 stream_read(s,hdr,4); | |
209 len = mp_decode_mp3_header(hdr); | |
210 if(len < 0) { | |
211 stream_skip(s,-3); | |
212 } else { | |
213 demux_packet_t* dp; | |
214 if(s->eof || (demux->movi_end && stream_tell(s) >= demux->movi_end) ) | |
215 return 0; | |
216 dp = new_demux_packet(len); | |
217 memcpy(dp->buffer,hdr,4); | |
218 stream_read(s,dp->buffer + 4,len-4); | |
219 ds_add_packet(ds,dp); | |
220 return 1; | |
221 } | |
4700
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
222 } break; |
4694 | 223 case WAV : { |
224 int l = sh_audio->wf->nAvgBytesPerSec; | |
225 demux_packet_t* dp = new_demux_packet(l); | |
226 stream_read(s,dp->buffer,l); | |
227 ds_add_packet(ds,dp); | |
228 return 1; | |
229 } | |
230 default: | |
4720
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
231 printf("Audio demuxer : unknown format %d\n",priv->frmt); |
4694 | 232 } |
233 | |
234 | |
235 return 0; | |
236 } | |
237 | |
238 void demux_audio_seek(demuxer_t *demuxer,float rel_seek_secs,int flags){ | |
239 sh_audio_t* sh_audio; | |
240 stream_t* s; | |
241 int base,pos; | |
242 float len; | |
243 da_priv_t* priv; | |
244 | |
245 sh_audio = demuxer->audio->sh; | |
246 s = demuxer->stream; | |
247 priv = demuxer->priv; | |
248 | |
249 base = flags&1 ? demuxer->movi_start : stream_tell(s) ; | |
250 len = (demuxer->movi_end && flags&2) ? (demuxer->movi_end - demuxer->movi_start)*rel_seek_secs : rel_seek_secs; | |
251 | |
252 pos = base+(len*sh_audio->i_bps); | |
253 | |
254 if(demuxer->movi_end && pos >= demuxer->movi_end) { | |
255 sh_audio->timer = (stream_tell(s) - demuxer->movi_start)/sh_audio->i_bps; | |
256 return; | |
257 } else if(pos < demuxer->movi_start) | |
258 pos = demuxer->movi_start; | |
259 | |
260 | |
261 sh_audio->timer = flags&1 ? rel_seek_secs : (pos-demuxer->movi_start)/sh_audio->i_bps; | |
262 | |
263 switch(priv->frmt) { | |
264 case WAV: | |
265 pos += (pos % (sh_audio->channels * sh_audio->samplesize) ); | |
266 break; | |
267 } | |
268 | |
269 stream_seek(s,pos); | |
270 | |
4700
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
271 resync_audio_stream(sh_audio); |
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
272 |
4694 | 273 } |