Mercurial > mplayer.hg
annotate libmpdemux/demux_aac.c @ 29351:d58878f243ea
Replace incorrect use of strncpy by av_strlcpy.
Only a real issue if inet_ntoa can actually return a string of more than 255 bytes.
author | reimar |
---|---|
date | Fri, 26 Jun 2009 13:54:22 +0000 |
parents | 0f1b5b68af32 |
children | 928359c13d93 |
rev | line source |
---|---|
29238
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
1 /* |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
2 * This file is part of MPlayer. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
3 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
4 * MPlayer is free software; you can redistribute it and/or modify |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
6 * the Free Software Foundation; either version 2 of the License, or |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
7 * (at your option) any later version. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
8 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
9 * MPlayer is distributed in the hope that it will be useful, |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
12 * GNU General Public License for more details. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
13 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
14 * You should have received a copy of the GNU General Public License along |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
17 */ |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
26327
diff
changeset
|
18 |
15720 | 19 #include <stdio.h> |
20 #include <stdlib.h> | |
21 #include <string.h> | |
22 | |
23 #include "config.h" | |
24 #include "mp_msg.h" | |
25 #include "help_mp.h" | |
26 | |
22605
4d81dbdf46b9
Add explicit location for headers from the stream/ directory.
diego
parents:
21421
diff
changeset
|
27 #include "stream/stream.h" |
15720 | 28 #include "demuxer.h" |
29 #include "parse_es.h" | |
30 #include "stheader.h" | |
31 | |
32 #include "ms_hdr.h" | |
33 | |
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; | |
41 | |
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 | |
26327
fa91545f01bb
Split the aac header parsing out of aac demuxer to allow using libmpmux
albeu
parents:
26299
diff
changeset
|
46 int aac_parse_frame(uint8_t *buf, int *srate, int *num); |
15720 | 47 |
48 static int demux_aac_init(demuxer_t *demuxer) | |
49 { | |
50 aac_priv_t *priv; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
51 |
15720 | 52 priv = calloc(1, sizeof(aac_priv_t)); |
53 if(!priv) | |
54 return 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
55 |
15720 | 56 priv->buf = (uint8_t*) malloc(8); |
57 if(!priv->buf) | |
17789
9f0c42967ce5
fix minor (i.e. unlikely to ever happen) leak when init fails
reimar
parents:
17636
diff
changeset
|
58 { |
9f0c42967ce5
fix minor (i.e. unlikely to ever happen) leak when init fails
reimar
parents:
17636
diff
changeset
|
59 free(priv); |
15720 | 60 return 0; |
17789
9f0c42967ce5
fix minor (i.e. unlikely to ever happen) leak when init fails
reimar
parents:
17636
diff
changeset
|
61 } |
15720 | 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; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
70 |
15720 | 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; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
88 |
15720 | 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 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
94 |
15720 | 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; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
110 |
15720 | 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; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
123 |
16750
0a31740dd5e6
Use PRI?64 defines as format strings for 64 bit variables.
reimar
parents:
16175
diff
changeset
|
124 mp_msg(MSGT_DEMUX, MSGL_V, "demux_aac_probe, INIT: %"PRIu64", PROBED: %"PRIu64", 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'); | |
26299
4d56038ec730
Fix lots and lots of other demuxers broken by r26301
reimar
parents:
25883
diff
changeset
|
139 demuxer->audio->id = 0; |
15720 | 140 demuxer->audio->sh = sh; |
141 | |
142 demuxer->filepos = stream_tell(demuxer->stream); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
143 |
16175 | 144 return demuxer; |
15720 | 145 } |
146 | |
16175 | 147 static int demux_aac_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds) |
15720 | 148 { |
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; | |
153 | |
154 if(demuxer->stream->eof || (demuxer->movi_end && stream_tell(demuxer->stream) >= demuxer->movi_end)) | |
155 return 0; | |
156 | |
157 while(! demuxer->stream->eof) | |
158 { | |
159 c1 = c2 = 0; | |
160 while(c1 != 0xFF) | |
161 { | |
162 c1 = stream_read_char(demuxer->stream); | |
163 if(c1 < 0) | |
164 return 0; | |
165 } | |
166 c2 = stream_read_char(demuxer->stream); | |
167 if(c2 < 0) | |
168 return 0; | |
169 if((c2 & 0xF6) != 0xF0) | |
170 continue; | |
171 | |
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; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
176 |
15720 | 177 len = aac_parse_frame(priv->buf, &srate, &num); |
178 if(len > 0) | |
179 { | |
180 dp = new_demux_packet(len); | |
181 if(! dp) | |
182 { | |
183 mp_msg(MSGT_DEMUX, MSGL_ERR, "fill_buffer, NEW_ADD_PACKET(%d)FAILED\n", len); | |
184 return 0; | |
185 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
186 |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
187 |
15720 | 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; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
198 |
15720 | 199 priv->bitrate = (int) (priv->size / priv->time); |
200 demuxer->filepos = stream_tell(demuxer->stream); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
201 |
15720 | 202 return len; |
203 } | |
204 else | |
205 stream_skip(demuxer->stream, -6); | |
206 } | |
207 | |
208 return 0; | |
209 } | |
210 | |
211 | |
212 //This is an almost verbatim copy of high_res_mp3_seek(), from demux_audio.c | |
17636 | 213 static void demux_aac_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags) |
15720 | 214 { |
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; | |
219 | |
220 ds_free_packs(d_audio); | |
221 | |
25883
baf32110d3fc
Use defines to give names to the different seek flags.
reimar
parents:
25707
diff
changeset
|
222 time = (flags & SEEK_ABSOLUTE) ? rel_seek_secs - priv->last_pts : rel_seek_secs; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
223 if(time < 0) |
15720 | 224 { |
225 stream_seek(demuxer->stream, demuxer->movi_start); | |
226 time = priv->last_pts + time; | |
227 priv->last_pts = 0; | |
228 } | |
229 | |
230 if(time > 0) | |
231 { | |
232 int len, nf, srate, num; | |
233 | |
234 nf = time * sh_audio->samplerate/1024; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
235 |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
236 while(nf > 0) |
15720 | 237 { |
238 if(stream_read(demuxer->stream,priv->buf, 8) < 8) | |
239 break; | |
240 len = aac_parse_frame(priv->buf, &srate, &num); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
241 if(len <= 0) |
15720 | 242 { |
243 stream_skip(demuxer->stream, -7); | |
244 continue; | |
245 } | |
246 stream_skip(demuxer->stream, len - 8); | |
247 priv->last_pts += (float) (num*1024.0/srate); | |
248 nf -= num; | |
249 } | |
250 } | |
251 } | |
252 | |
16175 | 253 |
25707
d4fe6e23283e
Make all demuxer_desc_t const, thus moving them to .rodata
reimar
parents:
22605
diff
changeset
|
254 const demuxer_desc_t demuxer_desc_aac = { |
16175 | 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 | |
268 }; |