debian/: delegate handling of mplayer.conf to dpkg.
[mplayer/glamo.git] / libmpdemux / demux_aac.c
blob2ebbb2995e6383c837cf8abb019e1acc17d2b1d6
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "config.h"
24 #include "mp_msg.h"
25 #include "help_mp.h"
27 #include "stream/stream.h"
28 #include "demuxer.h"
29 #include "parse_es.h"
30 #include "stheader.h"
32 #include "ms_hdr.h"
34 typedef struct {
35 uint8_t *buf;
36 uint64_t size; /// amount of time of data packets pushed to demuxer->audio (in bytes)
37 float time; /// amount of time elapsed based upon samples_per_frame/sample_rate (in milliseconds)
38 float last_pts; /// last pts seen
39 int bitrate; /// bitrate computed as size/time
40 } aac_priv_t;
42 /// \param srate (out) sample rate
43 /// \param num (out) number of audio frames in this ADTS frame
44 /// \return size of the ADTS frame in bytes
45 /// aac_parse_frames needs a buffer at least 8 bytes long
46 int aac_parse_frame(uint8_t *buf, int *srate, int *num);
48 static int demux_aac_init(demuxer_t *demuxer)
50 aac_priv_t *priv;
52 priv = calloc(1, sizeof(aac_priv_t));
53 if(!priv)
54 return 0;
56 priv->buf = (uint8_t*) malloc(8);
57 if(!priv->buf)
59 free(priv);
60 return 0;
63 demuxer->priv = priv;
64 return 1;
67 static void demux_close_aac(demuxer_t *demuxer)
69 aac_priv_t *priv = (aac_priv_t *) demuxer->priv;
71 if(!priv)
72 return;
74 if(priv->buf)
75 free(priv->buf);
77 free(demuxer->priv);
79 return;
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;
86 off_t init, probed;
87 aac_priv_t *priv;
89 if(! demux_aac_init(demuxer))
91 mp_msg(MSGT_DEMUX, MSGL_ERR, "COULDN'T INIT aac_demux, exit\n");
92 return 0;
95 priv = (aac_priv_t *) demuxer->priv;
97 init = probed = stream_tell(demuxer->stream);
98 while(probed-init <= 32768 && cnt < 8)
100 c = 0;
101 while(c != 0xFF)
103 c = stream_read_char(demuxer->stream);
104 if(c < 0)
105 goto fail;
107 priv->buf[0] = 0xFF;
108 if(stream_read(demuxer->stream, &(priv->buf[1]), 7) < 7)
109 goto fail;
111 len = aac_parse_frame(priv->buf, &srate, &num);
112 if(len > 0)
114 cnt++;
115 stream_skip(demuxer->stream, len - 8);
117 probed = stream_tell(demuxer->stream);
120 stream_seek(demuxer->stream, init);
121 if(cnt < 8)
122 goto fail;
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;
127 fail:
128 mp_msg(MSGT_DEMUX, MSGL_V, "demux_aac_probe, failed to detect an AAC stream\n");
129 return 0;
132 static demuxer_t* demux_aac_open(demuxer_t *demuxer)
134 sh_audio_t *sh;
136 sh = new_sh_audio(demuxer, 0);
137 sh->ds = demuxer->audio;
138 sh->format = mmioFOURCC('M', 'P', '4', 'A');
139 demuxer->audio->id = 0;
140 demuxer->audio->sh = sh;
142 demuxer->filepos = stream_tell(demuxer->stream);
144 return demuxer;
147 static int demux_aac_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds)
149 aac_priv_t *priv = (aac_priv_t *) demuxer->priv;
150 demux_packet_t *dp;
151 int c1, c2, len, srate, num;
152 float tm = 0;
154 if(demuxer->stream->eof || (demuxer->movi_end && stream_tell(demuxer->stream) >= demuxer->movi_end))
155 return 0;
157 while(! demuxer->stream->eof)
159 c1 = c2 = 0;
160 while(c1 != 0xFF)
162 c1 = stream_read_char(demuxer->stream);
163 if(c1 < 0)
164 return 0;
166 c2 = stream_read_char(demuxer->stream);
167 if(c2 < 0)
168 return 0;
169 if((c2 & 0xF6) != 0xF0)
170 continue;
172 priv->buf[0] = (unsigned char) c1;
173 priv->buf[1] = (unsigned char) c2;
174 if(stream_read(demuxer->stream, &(priv->buf[2]), 6) < 6)
175 return 0;
177 len = aac_parse_frame(priv->buf, &srate, &num);
178 if(len > 0)
180 dp = new_demux_packet(len);
181 if(! dp)
183 mp_msg(MSGT_DEMUX, MSGL_ERR, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", len);
184 return 0;
188 memcpy(dp->buffer, priv->buf, 8);
189 stream_read(demuxer->stream, &(dp->buffer[8]), len-8);
190 if(srate)
191 tm = (float) (num * 1024.0/srate);
192 priv->last_pts += tm;
193 dp->pts = priv->last_pts;
194 //fprintf(stderr, "\nPTS: %.3f\n", dp->pts);
195 ds_add_packet(demuxer->audio, dp);
196 priv->size += len;
197 priv->time += tm;
199 priv->bitrate = (int) (priv->size / priv->time);
200 demuxer->filepos = stream_tell(demuxer->stream);
202 return len;
204 else
205 stream_skip(demuxer->stream, -6);
208 return 0;
212 //This is an almost verbatim copy of high_res_mp3_seek(), from demux_audio.c
213 static void demux_aac_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
215 aac_priv_t *priv = (aac_priv_t *) demuxer->priv;
216 demux_stream_t *d_audio=demuxer->audio;
217 sh_audio_t *sh_audio=d_audio->sh;
218 float time;
220 ds_free_packs(d_audio);
222 time = (flags & SEEK_ABSOLUTE) ? rel_seek_secs - priv->last_pts : rel_seek_secs;
223 if(time < 0)
225 stream_seek(demuxer->stream, demuxer->movi_start);
226 time = priv->last_pts + time;
227 priv->last_pts = 0;
230 if(time > 0)
232 int len, nf, srate, num;
234 nf = time * sh_audio->samplerate/1024;
236 while(nf > 0)
238 if(stream_read(demuxer->stream,priv->buf, 8) < 8)
239 break;
240 len = aac_parse_frame(priv->buf, &srate, &num);
241 if(len <= 0)
243 stream_skip(demuxer->stream, -7);
244 continue;
246 stream_skip(demuxer->stream, len - 8);
247 priv->last_pts += (float) (num*1024.0/srate);
248 nf -= num;
254 const demuxer_desc_t demuxer_desc_aac = {
255 "AAC demuxer",
256 "aac",
257 "AAC",
258 "Nico Sabbi",
259 "Raw AAC files ",
260 DEMUXER_TYPE_AAC,
261 0, // unsafe autodetect
262 demux_aac_probe,
263 demux_aac_fill_buffer,
264 demux_aac_open,
265 demux_close_aac,
266 demux_aac_seek,
267 NULL