demux_lavf: for avi, use packet .dts instead of .pts
[mplayer/glamo.git] / libmpdemux / demux_aac.c
blob1924ec97b147874291711ffebef9b2f7d5329d42
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"
26 #include "stream/stream.h"
27 #include "demuxer.h"
28 #include "parse_es.h"
29 #include "stheader.h"
30 #include "aac_hdr.h"
31 #include "ms_hdr.h"
33 typedef struct {
34 uint8_t *buf;
35 uint64_t size; /// amount of time of data packets pushed to demuxer->audio (in bytes)
36 float time; /// amount of time elapsed based upon samples_per_frame/sample_rate (in milliseconds)
37 float last_pts; /// last pts seen
38 int bitrate; /// bitrate computed as size/time
39 } aac_priv_t;
41 static int demux_aac_init(demuxer_t *demuxer)
43 aac_priv_t *priv;
45 priv = calloc(1, sizeof(aac_priv_t));
46 if(!priv)
47 return 0;
49 priv->buf = malloc(8);
50 if(!priv->buf)
52 free(priv);
53 return 0;
56 demuxer->priv = priv;
57 return 1;
60 static void demux_close_aac(demuxer_t *demuxer)
62 aac_priv_t *priv = (aac_priv_t *) demuxer->priv;
64 if(!priv)
65 return;
67 if(priv->buf)
68 free(priv->buf);
70 free(demuxer->priv);
72 return;
75 /// returns DEMUXER_TYPE_AAC if it finds 8 ADTS frames in 32768 bytes, 0 otherwise
76 static int demux_aac_probe(demuxer_t *demuxer)
78 int cnt = 0, c, len, srate, num;
79 off_t init, probed;
80 aac_priv_t *priv;
82 if(! demux_aac_init(demuxer))
84 mp_msg(MSGT_DEMUX, MSGL_ERR, "COULDN'T INIT aac_demux, exit\n");
85 return 0;
88 priv = (aac_priv_t *) demuxer->priv;
90 init = probed = stream_tell(demuxer->stream);
91 while(probed-init <= 32768 && cnt < 8)
93 c = 0;
94 while(c != 0xFF)
96 c = stream_read_char(demuxer->stream);
97 if(c < 0)
98 goto fail;
100 priv->buf[0] = 0xFF;
101 if(stream_read(demuxer->stream, &(priv->buf[1]), 7) < 7)
102 goto fail;
104 len = aac_parse_frame(priv->buf, &srate, &num);
105 if(len > 0)
107 cnt++;
108 stream_skip(demuxer->stream, len - 8);
110 probed = stream_tell(demuxer->stream);
113 stream_seek(demuxer->stream, init);
114 if(cnt < 8)
115 goto fail;
117 mp_msg(MSGT_DEMUX, MSGL_V, "demux_aac_probe, INIT: %"PRIu64", PROBED: %"PRIu64", cnt: %d\n", init, probed, cnt);
118 return DEMUXER_TYPE_AAC;
120 fail:
121 mp_msg(MSGT_DEMUX, MSGL_V, "demux_aac_probe, failed to detect an AAC stream\n");
122 return 0;
125 static demuxer_t* demux_aac_open(demuxer_t *demuxer)
127 sh_audio_t *sh;
129 sh = new_sh_audio(demuxer, 0);
130 sh->ds = demuxer->audio;
131 sh->format = mmioFOURCC('M', 'P', '4', 'A');
132 demuxer->audio->id = 0;
133 demuxer->audio->sh = sh;
135 demuxer->filepos = stream_tell(demuxer->stream);
137 return demuxer;
140 static int demux_aac_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds)
142 aac_priv_t *priv = (aac_priv_t *) demuxer->priv;
143 demux_packet_t *dp;
144 int c1, c2, len, srate, num;
145 float tm = 0;
147 if(demuxer->stream->eof || (demuxer->movi_end && stream_tell(demuxer->stream) >= demuxer->movi_end))
148 return 0;
150 while(! demuxer->stream->eof)
152 c1 = c2 = 0;
153 while(c1 != 0xFF)
155 c1 = stream_read_char(demuxer->stream);
156 if(c1 < 0)
157 return 0;
159 c2 = stream_read_char(demuxer->stream);
160 if(c2 < 0)
161 return 0;
162 if((c2 & 0xF6) != 0xF0)
163 continue;
165 priv->buf[0] = (unsigned char) c1;
166 priv->buf[1] = (unsigned char) c2;
167 if(stream_read(demuxer->stream, &(priv->buf[2]), 6) < 6)
168 return 0;
170 len = aac_parse_frame(priv->buf, &srate, &num);
171 if(len > 0)
173 dp = new_demux_packet(len);
174 if(! dp)
176 mp_msg(MSGT_DEMUX, MSGL_ERR, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", len);
177 return 0;
181 memcpy(dp->buffer, priv->buf, 8);
182 stream_read(demuxer->stream, &(dp->buffer[8]), len-8);
183 if(srate)
184 tm = (float) (num * 1024.0/srate);
185 priv->last_pts += tm;
186 dp->pts = priv->last_pts;
187 //fprintf(stderr, "\nPTS: %.3f\n", dp->pts);
188 ds_add_packet(demuxer->audio, dp);
189 priv->size += len;
190 priv->time += tm;
192 priv->bitrate = (int) (priv->size / priv->time);
193 demuxer->filepos = stream_tell(demuxer->stream);
195 return len;
197 else
198 stream_skip(demuxer->stream, -6);
201 return 0;
205 //This is an almost verbatim copy of high_res_mp3_seek(), from demux_audio.c
206 static void demux_aac_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
208 aac_priv_t *priv = (aac_priv_t *) demuxer->priv;
209 demux_stream_t *d_audio=demuxer->audio;
210 sh_audio_t *sh_audio=d_audio->sh;
211 float time;
213 ds_free_packs(d_audio);
215 time = (flags & SEEK_ABSOLUTE) ? rel_seek_secs - priv->last_pts : rel_seek_secs;
216 if(time < 0)
218 stream_seek(demuxer->stream, demuxer->movi_start);
219 time = priv->last_pts + time;
220 priv->last_pts = 0;
223 if(time > 0)
225 int len, nf, srate, num;
227 nf = time * sh_audio->samplerate/1024;
229 while(nf > 0)
231 if(stream_read(demuxer->stream,priv->buf, 8) < 8)
232 break;
233 len = aac_parse_frame(priv->buf, &srate, &num);
234 if(len <= 0)
236 stream_skip(demuxer->stream, -7);
237 continue;
239 stream_skip(demuxer->stream, len - 8);
240 priv->last_pts += (float) (num*1024.0/srate);
241 nf -= num;
247 const demuxer_desc_t demuxer_desc_aac = {
248 "AAC demuxer",
249 "aac",
250 "AAC",
251 "Nico Sabbi",
252 "Raw AAC files ",
253 DEMUXER_TYPE_AAC,
254 0, // unsafe autodetect
255 demux_aac_probe,
256 demux_aac_fill_buffer,
257 demux_aac_open,
258 demux_close_aac,
259 demux_aac_seek,
260 NULL