subassconvert: do not escape likely ASS override tags
[mplayer.git] / libmpdemux / demux_aac.c
blob9d37d75cf8c76d0ad459fff41417f9326eae7f05
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 free(priv->buf);
69 free(demuxer->priv);
71 return;
74 /// returns DEMUXER_TYPE_AAC if it finds 8 ADTS frames in 32768 bytes, 0 otherwise
75 static int demux_aac_probe(demuxer_t *demuxer)
77 int cnt = 0, c, len, srate, num;
78 off_t init, probed;
79 aac_priv_t *priv;
81 if(! demux_aac_init(demuxer))
83 mp_msg(MSGT_DEMUX, MSGL_ERR, "COULDN'T INIT aac_demux, exit\n");
84 return 0;
87 priv = (aac_priv_t *) demuxer->priv;
89 init = probed = stream_tell(demuxer->stream);
90 while(probed-init <= 32768 && cnt < 8)
92 c = 0;
93 while(c != 0xFF)
95 c = stream_read_char(demuxer->stream);
96 if(c < 0)
97 goto fail;
99 priv->buf[0] = 0xFF;
100 if(stream_read(demuxer->stream, &(priv->buf[1]), 7) < 7)
101 goto fail;
103 len = aac_parse_frame(priv->buf, &srate, &num);
104 if(len > 0)
106 cnt++;
107 stream_skip(demuxer->stream, len - 8);
109 probed = stream_tell(demuxer->stream);
112 stream_seek(demuxer->stream, init);
113 if(cnt < 8)
114 goto fail;
116 mp_msg(MSGT_DEMUX, MSGL_V, "demux_aac_probe, INIT: %"PRIu64", PROBED: %"PRIu64", cnt: %d\n", init, probed, cnt);
117 return DEMUXER_TYPE_AAC;
119 fail:
120 mp_msg(MSGT_DEMUX, MSGL_V, "demux_aac_probe, failed to detect an AAC stream\n");
121 return 0;
124 static demuxer_t* demux_aac_open(demuxer_t *demuxer)
126 sh_audio_t *sh;
128 sh = new_sh_audio(demuxer, 0);
129 sh->ds = demuxer->audio;
130 sh->format = mmioFOURCC('M', 'P', '4', 'A');
131 demuxer->audio->id = 0;
132 demuxer->audio->sh = sh;
134 demuxer->filepos = stream_tell(demuxer->stream);
136 return demuxer;
139 static int demux_aac_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds)
141 aac_priv_t *priv = (aac_priv_t *) demuxer->priv;
142 demux_packet_t *dp;
143 int c1, c2, len, srate, num;
144 float tm = 0;
146 if(demuxer->stream->eof || (demuxer->movi_end && stream_tell(demuxer->stream) >= demuxer->movi_end))
147 return 0;
149 while(! demuxer->stream->eof)
151 c1 = c2 = 0;
152 while(c1 != 0xFF)
154 c1 = stream_read_char(demuxer->stream);
155 if(c1 < 0)
156 return 0;
158 c2 = stream_read_char(demuxer->stream);
159 if(c2 < 0)
160 return 0;
161 if((c2 & 0xF6) != 0xF0)
162 continue;
164 priv->buf[0] = (unsigned char) c1;
165 priv->buf[1] = (unsigned char) c2;
166 if(stream_read(demuxer->stream, &(priv->buf[2]), 6) < 6)
167 return 0;
169 len = aac_parse_frame(priv->buf, &srate, &num);
170 if(len > 0)
172 dp = new_demux_packet(len);
173 if(! dp)
175 mp_msg(MSGT_DEMUX, MSGL_ERR, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", len);
176 return 0;
180 memcpy(dp->buffer, priv->buf, 8);
181 stream_read(demuxer->stream, &(dp->buffer[8]), len-8);
182 if(srate)
183 tm = (float) (num * 1024.0/srate);
184 priv->last_pts += tm;
185 dp->pts = priv->last_pts;
186 //fprintf(stderr, "\nPTS: %.3f\n", dp->pts);
187 ds_add_packet(demuxer->audio, dp);
188 priv->size += len;
189 priv->time += tm;
191 priv->bitrate = (int) (priv->size / priv->time);
192 demuxer->filepos = stream_tell(demuxer->stream);
194 return len;
196 else
197 stream_skip(demuxer->stream, -6);
200 return 0;
204 //This is an almost verbatim copy of high_res_mp3_seek(), from demux_audio.c
205 static void demux_aac_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
207 aac_priv_t *priv = (aac_priv_t *) demuxer->priv;
208 demux_stream_t *d_audio=demuxer->audio;
209 sh_audio_t *sh_audio=d_audio->sh;
210 float time;
212 ds_free_packs(d_audio);
214 time = (flags & SEEK_ABSOLUTE) ? rel_seek_secs - priv->last_pts : rel_seek_secs;
215 if(time < 0)
217 stream_seek(demuxer->stream, demuxer->movi_start);
218 time = priv->last_pts + time;
219 priv->last_pts = 0;
222 if(time > 0)
224 int len, nf, srate, num;
226 nf = time * sh_audio->samplerate/1024;
228 while(nf > 0)
230 if(stream_read(demuxer->stream,priv->buf, 8) < 8)
231 break;
232 len = aac_parse_frame(priv->buf, &srate, &num);
233 if(len <= 0)
235 stream_skip(demuxer->stream, -7);
236 continue;
238 stream_skip(demuxer->stream, len - 8);
239 priv->last_pts += (float) (num*1024.0/srate);
240 nf -= num;
246 const demuxer_desc_t demuxer_desc_aac = {
247 "AAC demuxer",
248 "aac",
249 "AAC",
250 "Nico Sabbi",
251 "Raw AAC files ",
252 DEMUXER_TYPE_AAC,
253 0, // unsafe autodetect
254 demux_aac_probe,
255 demux_aac_fill_buffer,
256 demux_aac_open,
257 demux_close_aac,
258 demux_aac_seek,
259 NULL