Mercurial > mplayer.hg
annotate libmpdemux/demux_aac.c @ 17579:f1ce086b5eed
Clarify feature removal policy: it must be approved on the mailing list.
author | rathann |
---|---|
date | Fri, 10 Feb 2006 18:12:17 +0000 |
parents | 9081ae3a702c |
children | b849a99cdc3c |
rev | line source |
---|---|
15720 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 | |
5 #include "config.h" | |
6 #include "mp_msg.h" | |
7 #include "help_mp.h" | |
8 | |
9 #include "stream.h" | |
10 #include "demuxer.h" | |
11 #include "parse_es.h" | |
12 #include "stheader.h" | |
13 | |
14 #include "bswap.h" | |
15 #include "ms_hdr.h" | |
16 | |
17 typedef struct { | |
18 uint8_t *buf; | |
19 uint64_t size; /// amount of time of data packets pushed to demuxer->audio (in bytes) | |
20 float time; /// amount of time elapsed based upon samples_per_frame/sample_rate (in milliseconds) | |
21 float last_pts; /// last pts seen | |
22 int bitrate; /// bitrate computed as size/time | |
23 } aac_priv_t; | |
24 | |
25 /// \param srate (out) sample rate | |
26 /// \param num (out) number of audio frames in this ADTS frame | |
27 /// \return size of the ADTS frame in bytes | |
28 /// aac_parse_frames needs a buffer at least 8 bytes long | |
29 int aac_parse_frame(uint8_t *buf, int *srate, int *num) | |
30 { | |
31 int i = 0, sr, fl = 0, id; | |
32 static int srates[] = {96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 0, 0, 0}; | |
33 | |
34 if((buf[i] != 0xFF) || ((buf[i+1] & 0xF6) != 0xF0)) | |
35 return 0; | |
36 | |
37 id = (buf[i+1] >> 3) & 0x01; //id=1 mpeg2, 0: mpeg4 | |
38 sr = (buf[i+2] >> 2) & 0x0F; | |
39 if(sr > 11) | |
40 return 0; | |
41 *srate = srates[sr]; | |
42 | |
43 fl = ((buf[i+3] & 0x03) << 11) | (buf[i+4] << 3) | ((buf[i+5] >> 5) & 0x07); | |
44 *num = (buf[i+6] & 0x02) + 1; | |
45 | |
46 return fl; | |
47 } | |
48 | |
49 static int demux_aac_init(demuxer_t *demuxer) | |
50 { | |
51 aac_priv_t *priv; | |
52 | |
53 priv = calloc(1, sizeof(aac_priv_t)); | |
54 if(!priv) | |
55 return 0; | |
56 | |
57 priv->buf = (uint8_t*) malloc(8); | |
58 if(!priv->buf) | |
59 return 0; | |
60 | |
61 demuxer->priv = priv; | |
62 return 1; | |
63 } | |
64 | |
16175 | 65 static void demux_close_aac(demuxer_t *demuxer) |
15720 | 66 { |
67 aac_priv_t *priv = (aac_priv_t *) demuxer->priv; | |
68 | |
69 if(!priv) | |
70 return; | |
71 | |
72 if(priv->buf) | |
73 free(priv->buf); | |
74 | |
75 free(demuxer->priv); | |
76 | |
77 return; | |
78 } | |
79 | |
16175 | 80 /// returns DEMUXER_TYPE_AAC if it finds 8 ADTS frames in 32768 bytes, 0 otherwise |
81 static int demux_aac_probe(demuxer_t *demuxer) | |
15720 | 82 { |
83 int cnt = 0, c, len, srate, num; | |
84 off_t init, probed; | |
85 aac_priv_t *priv; | |
86 | |
87 if(! demux_aac_init(demuxer)) | |
88 { | |
89 mp_msg(MSGT_DEMUX, MSGL_ERR, "COULDN'T INIT aac_demux, exit\n"); | |
90 return 0; | |
91 } | |
92 | |
93 priv = (aac_priv_t *) demuxer->priv; | |
94 | |
95 init = probed = stream_tell(demuxer->stream); | |
96 while(probed-init <= 32768 && cnt < 8) | |
97 { | |
98 c = 0; | |
99 while(c != 0xFF) | |
100 { | |
101 c = stream_read_char(demuxer->stream); | |
102 if(c < 0) | |
103 goto fail; | |
104 } | |
105 priv->buf[0] = 0xFF; | |
106 if(stream_read(demuxer->stream, &(priv->buf[1]), 7) < 7) | |
107 goto fail; | |
108 | |
109 len = aac_parse_frame(priv->buf, &srate, &num); | |
110 if(len > 0) | |
111 { | |
112 cnt++; | |
113 stream_skip(demuxer->stream, len - 8); | |
114 } | |
115 probed = stream_tell(demuxer->stream); | |
116 } | |
117 | |
118 stream_seek(demuxer->stream, init); | |
119 if(cnt < 8) | |
120 goto fail; | |
121 | |
16750
0a31740dd5e6
Use PRI?64 defines as format strings for 64 bit variables.
reimar
parents:
16175
diff
changeset
|
122 mp_msg(MSGT_DEMUX, MSGL_V, "demux_aac_probe, INIT: %"PRIu64", PROBED: %"PRIu64", cnt: %d\n", init, probed, cnt); |
16175 | 123 return DEMUXER_TYPE_AAC; |
15720 | 124 |
125 fail: | |
126 mp_msg(MSGT_DEMUX, MSGL_V, "demux_aac_probe, failed to detect an AAC stream\n"); | |
127 return 0; | |
128 } | |
129 | |
16175 | 130 static demuxer_t* demux_aac_open(demuxer_t *demuxer) |
15720 | 131 { |
132 sh_audio_t *sh; | |
133 | |
134 sh = new_sh_audio(demuxer, 0); | |
135 sh->ds = demuxer->audio; | |
136 sh->format = mmioFOURCC('M', 'P', '4', 'A'); | |
137 demuxer->audio->sh = sh; | |
138 | |
139 demuxer->filepos = stream_tell(demuxer->stream); | |
140 | |
16175 | 141 return demuxer; |
15720 | 142 } |
143 | |
16175 | 144 static int demux_aac_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds) |
15720 | 145 { |
146 aac_priv_t *priv = (aac_priv_t *) demuxer->priv; | |
147 demux_packet_t *dp; | |
148 int c1, c2, len, srate, num; | |
149 float tm = 0; | |
150 | |
151 if(demuxer->stream->eof || (demuxer->movi_end && stream_tell(demuxer->stream) >= demuxer->movi_end)) | |
152 return 0; | |
153 | |
154 while(! demuxer->stream->eof) | |
155 { | |
156 c1 = c2 = 0; | |
157 while(c1 != 0xFF) | |
158 { | |
159 c1 = stream_read_char(demuxer->stream); | |
160 if(c1 < 0) | |
161 return 0; | |
162 } | |
163 c2 = stream_read_char(demuxer->stream); | |
164 if(c2 < 0) | |
165 return 0; | |
166 if((c2 & 0xF6) != 0xF0) | |
167 continue; | |
168 | |
169 priv->buf[0] = (unsigned char) c1; | |
170 priv->buf[1] = (unsigned char) c2; | |
171 if(stream_read(demuxer->stream, &(priv->buf[2]), 6) < 6) | |
172 return 0; | |
173 | |
174 len = aac_parse_frame(priv->buf, &srate, &num); | |
175 if(len > 0) | |
176 { | |
177 dp = new_demux_packet(len); | |
178 if(! dp) | |
179 { | |
180 mp_msg(MSGT_DEMUX, MSGL_ERR, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", len); | |
181 return 0; | |
182 } | |
183 | |
184 | |
185 memcpy(dp->buffer, priv->buf, 8); | |
186 stream_read(demuxer->stream, &(dp->buffer[8]), len-8); | |
187 if(srate) | |
188 tm = (float) (num * 1024.0/srate); | |
189 priv->last_pts += tm; | |
190 dp->pts = priv->last_pts; | |
191 //fprintf(stderr, "\nPTS: %.3f\n", dp->pts); | |
192 ds_add_packet(demuxer->audio, dp); | |
193 priv->size += len; | |
194 priv->time += tm; | |
195 | |
196 priv->bitrate = (int) (priv->size / priv->time); | |
197 demuxer->filepos = stream_tell(demuxer->stream); | |
198 | |
199 return len; | |
200 } | |
201 else | |
202 stream_skip(demuxer->stream, -6); | |
203 } | |
204 | |
205 return 0; | |
206 } | |
207 | |
208 | |
209 //This is an almost verbatim copy of high_res_mp3_seek(), from demux_audio.c | |
16175 | 210 static void demux_aac_seek(demuxer_t *demuxer, float rel_seek_secs, int flags) |
15720 | 211 { |
212 aac_priv_t *priv = (aac_priv_t *) demuxer->priv; | |
213 demux_stream_t *d_audio=demuxer->audio; | |
214 sh_audio_t *sh_audio=d_audio->sh; | |
215 float time; | |
216 | |
217 ds_free_packs(d_audio); | |
218 | |
219 time = (flags & 1) ? rel_seek_secs - priv->last_pts : rel_seek_secs; | |
220 if(time < 0) | |
221 { | |
222 stream_seek(demuxer->stream, demuxer->movi_start); | |
223 time = priv->last_pts + time; | |
224 priv->last_pts = 0; | |
225 } | |
226 | |
227 if(time > 0) | |
228 { | |
229 int len, nf, srate, num; | |
230 | |
231 nf = time * sh_audio->samplerate/1024; | |
232 | |
233 while(nf > 0) | |
234 { | |
235 if(stream_read(demuxer->stream,priv->buf, 8) < 8) | |
236 break; | |
237 len = aac_parse_frame(priv->buf, &srate, &num); | |
238 if(len <= 0) | |
239 { | |
240 stream_skip(demuxer->stream, -7); | |
241 continue; | |
242 } | |
243 stream_skip(demuxer->stream, len - 8); | |
244 priv->last_pts += (float) (num*1024.0/srate); | |
245 nf -= num; | |
246 } | |
247 } | |
248 sh_audio->delay = priv->last_pts - (ds_tell_pts(demuxer->audio)-sh_audio->a_in_buffer_len)/(float)priv->bitrate; | |
249 } | |
250 | |
16175 | 251 |
252 demuxer_desc_t demuxer_desc_aac = { | |
253 "AAC demuxer", | |
254 "aac", | |
255 "AAC", | |
256 "Nico Sabbi", | |
257 "Raw AAC files ", | |
258 DEMUXER_TYPE_AAC, | |
259 0, // unsafe autodetect | |
260 demux_aac_probe, | |
261 demux_aac_fill_buffer, | |
262 demux_aac_open, | |
263 demux_close_aac, | |
264 demux_aac_seek, | |
265 NULL | |
266 }; |