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