9 #include "stream/stream.h"
18 uint64_t size
; /// amount of time of data packets pushed to demuxer->audio (in bytes)
19 float time
; /// amount of time elapsed based upon samples_per_frame/sample_rate (in milliseconds)
20 float last_pts
; /// last pts seen
21 int bitrate
; /// bitrate computed as size/time
24 /// \param srate (out) sample rate
25 /// \param num (out) number of audio frames in this ADTS frame
26 /// \return size of the ADTS frame in bytes
27 /// aac_parse_frames needs a buffer at least 8 bytes long
28 int aac_parse_frame(uint8_t *buf
, int *srate
, int *num
)
30 int i
= 0, sr
, fl
= 0, id
;
31 static int srates
[] = {96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 0, 0, 0};
33 if((buf
[i
] != 0xFF) || ((buf
[i
+1] & 0xF6) != 0xF0))
36 id
= (buf
[i
+1] >> 3) & 0x01; //id=1 mpeg2, 0: mpeg4
37 sr
= (buf
[i
+2] >> 2) & 0x0F;
42 fl
= ((buf
[i
+3] & 0x03) << 11) | (buf
[i
+4] << 3) | ((buf
[i
+5] >> 5) & 0x07);
43 *num
= (buf
[i
+6] & 0x02) + 1;
48 static int demux_aac_init(demuxer_t
*demuxer
)
52 priv
= calloc(1, sizeof(aac_priv_t
));
56 priv
->buf
= (uint8_t*) malloc(8);
67 static void demux_close_aac(demuxer_t
*demuxer
)
69 aac_priv_t
*priv
= (aac_priv_t
*) demuxer
->priv
;
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
)
85 int cnt
= 0, c
, len
, srate
, num
;
89 if(! demux_aac_init(demuxer
))
91 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "COULDN'T INIT aac_demux, exit\n");
95 priv
= (aac_priv_t
*) demuxer
->priv
;
97 init
= probed
= stream_tell(demuxer
->stream
);
98 while(probed
-init
<= 32768 && cnt
< 8)
103 c
= stream_read_char(demuxer
->stream
);
108 if(stream_read(demuxer
->stream
, &(priv
->buf
[1]), 7) < 7)
111 len
= aac_parse_frame(priv
->buf
, &srate
, &num
);
115 stream_skip(demuxer
->stream
, len
- 8);
117 probed
= stream_tell(demuxer
->stream
);
120 stream_seek(demuxer
->stream
, init
);
124 mp_msg(MSGT_DEMUX
, MSGL_V
, "demux_aac_probe, INIT: %"PRIu64
", PROBED: %"PRIu64
", cnt: %d\n", init
, probed
, cnt
);
125 return DEMUXER_TYPE_AAC
;
128 mp_msg(MSGT_DEMUX
, MSGL_V
, "demux_aac_probe, failed to detect an AAC stream\n");
132 static demuxer_t
* demux_aac_open(demuxer_t
*demuxer
)
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
;
141 demuxer
->filepos
= stream_tell(demuxer
->stream
);
146 static int demux_aac_fill_buffer(demuxer_t
*demuxer
, demux_stream_t
*ds
)
148 aac_priv_t
*priv
= (aac_priv_t
*) demuxer
->priv
;
150 int c1
, c2
, len
, srate
, num
;
153 if(demuxer
->stream
->eof
|| (demuxer
->movi_end
&& stream_tell(demuxer
->stream
) >= demuxer
->movi_end
))
156 while(! demuxer
->stream
->eof
)
161 c1
= stream_read_char(demuxer
->stream
);
165 c2
= stream_read_char(demuxer
->stream
);
168 if((c2
& 0xF6) != 0xF0)
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)
176 len
= aac_parse_frame(priv
->buf
, &srate
, &num
);
179 dp
= new_demux_packet(len
);
182 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", len
);
187 memcpy(dp
->buffer
, priv
->buf
, 8);
188 stream_read(demuxer
->stream
, &(dp
->buffer
[8]), len
-8);
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
);
198 priv
->bitrate
= (int) (priv
->size
/ priv
->time
);
199 demuxer
->filepos
= stream_tell(demuxer
->stream
);
204 stream_skip(demuxer
->stream
, -6);
211 //This is an almost verbatim copy of high_res_mp3_seek(), from demux_audio.c
212 static void demux_aac_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
)
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
;
219 ds_free_packs(d_audio
);
221 time
= (flags
& 1) ? rel_seek_secs
- priv
->last_pts
: rel_seek_secs
;
224 stream_seek(demuxer
->stream
, demuxer
->movi_start
);
225 time
= priv
->last_pts
+ time
;
231 int len
, nf
, srate
, num
;
233 nf
= time
* sh_audio
->samplerate
/1024;
237 if(stream_read(demuxer
->stream
,priv
->buf
, 8) < 8)
239 len
= aac_parse_frame(priv
->buf
, &srate
, &num
);
242 stream_skip(demuxer
->stream
, -7);
245 stream_skip(demuxer
->stream
, len
- 8);
246 priv
->last_pts
+= (float) (num
*1024.0/srate
);
253 demuxer_desc_t demuxer_desc_aac
= {
260 0, // unsafe autodetect
262 demux_aac_fill_buffer
,