]> git.sesse.net Git - vlc/blob - modules/demux/aac.c
90830b8838019b044b10af03415e266ae64181f1
[vlc] / modules / demux / aac.c
1 /*****************************************************************************
2  * aac.c : Raw aac Stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 /*****************************************************************************
33  * Module descriptor
34  *****************************************************************************/
35 static int  Open    ( vlc_object_t * );
36 static void Close  ( vlc_object_t * );
37
38 vlc_module_begin();
39     set_description( _("AAC demuxer" ) );
40     set_capability( "demux2", 100 );
41     set_callbacks( Open, Close );
42     add_shortcut( "aac" );
43 vlc_module_end();
44
45 /* TODO:
46  * - adif support ?
47  *
48  */
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 struct demux_sys_t
54 {
55     mtime_t         i_time;
56
57     es_out_id_t     *p_es;
58 };
59
60 static int Demux  ( demux_t * );
61 static int Control( demux_t *, int, va_list );
62
63 static int i_aac_samplerate[16] =
64 {
65     96000, 88200, 64000, 48000, 44100, 32000,
66     24000, 22050, 16000, 12000, 11025, 8000,
67     7350,  0,     0,     0
68 };
69
70 #define AAC_ID( p )          ( ((p)[1]>>3)&0x01 )
71 #define AAC_SAMPLE_RATE( p ) i_aac_samplerate[((p)[2]>>2)&0x0f]
72 #define AAC_CHANNELS( p )    ( (((p)[2]&0x01)<<2) | (((p)[3]>>6)&0x03) )
73 #define AAC_FRAME_SIZE( p )  ( (((p)[3]&0x03) << 11)|( (p)[4] << 3 )|( (((p)[5]) >>5)&0x7 ) )
74
75 /* FIXME it's plain wrong */
76 #define AAC_FRAME_SAMPLES( p )  1024
77
78 static inline int HeaderCheck( uint8_t *p )
79 {
80     if( p[0] != 0xff ||
81         ( p[1]&0xf6 ) != 0xf0 ||
82         AAC_SAMPLE_RATE( p ) == 0 ||
83         AAC_CHANNELS( p ) == 0 ||
84         AAC_FRAME_SIZE( p ) == 0 )
85     {
86         return VLC_FALSE;
87     }
88     return VLC_TRUE;
89 }
90
91 /*****************************************************************************
92  * Open: initializes AAC demux structures
93  *****************************************************************************/
94 static int Open( vlc_object_t * p_this )
95 {
96     demux_t     *p_demux = (demux_t*)p_this;
97     demux_sys_t *p_sys;
98     int         b_forced = VLC_FALSE;
99
100     uint8_t     *p_peek;
101     module_t    *p_id3;
102     es_format_t fmt;
103
104     if( !strncmp( p_demux->psz_demux, "aac", 3 ) )
105     {
106         b_forced = VLC_TRUE;
107     }
108
109     if( p_demux->psz_path )
110     {
111         int  i_len = strlen( p_demux->psz_path );
112
113         if( i_len > 4 && !strcasecmp( &p_demux->psz_path[i_len - 4], ".aac" ) )
114         {
115             b_forced = VLC_TRUE;
116         }
117     }
118
119     if( !b_forced )
120     {
121         /* I haven't find any sure working aac detection so only forced or
122          * extention check
123          */
124         msg_Warn( p_demux, "AAC module discarded" );
125         return VLC_EGENERIC;
126     }
127
128     /* skip possible id3 header */
129     if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
130     {
131         module_Unneed( p_demux, p_id3 );
132     }
133
134     p_demux->pf_demux   = Demux;
135     p_demux->pf_control = Control;
136     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
137
138     p_sys->i_time = 1;
139
140     /* peek the begining (10 is for adts header) */
141     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
142     {
143         msg_Err( p_demux, "cannot peek" );
144         goto error;
145     }
146     if( !strncmp( p_peek, "ADIF", 4 ) )
147     {
148         msg_Err( p_demux, "ADIF file. Not yet supported. (Please report)" );
149         goto error;
150     }
151
152     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
153     if( HeaderCheck( p_peek ) )
154     {
155         fmt.audio.i_channels = AAC_CHANNELS( p_peek );
156         fmt.audio.i_rate     = AAC_SAMPLE_RATE( p_peek );
157
158         msg_Dbg( p_demux,
159                  "adts header: id=%d channels=%d sample_rate=%d",
160                  AAC_ID( p_peek ),
161                  AAC_CHANNELS( p_peek ),
162                  AAC_SAMPLE_RATE( p_peek ) );
163     }
164
165     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
166     return VLC_SUCCESS;
167
168 error:
169     free( p_sys );
170     return VLC_EGENERIC;
171 }
172
173
174 /*****************************************************************************
175  * Demux: reads and demuxes data packets
176  *****************************************************************************
177  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
178  *****************************************************************************/
179 static int Demux( demux_t *p_demux )
180 {
181     demux_sys_t *p_sys = p_demux->p_sys;
182     block_t     *p_block;
183
184     uint8_t     h[8];
185     uint8_t     *p_peek;
186
187     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
188     {
189         msg_Warn( p_demux, "cannot peek" );
190         return 0;
191     }
192
193     if( !HeaderCheck( p_peek ) )
194     {
195         /* we need to resynch */
196         vlc_bool_t  b_ok = VLC_FALSE;
197         int         i_skip = 0;
198         int         i_peek;
199
200         i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
201         if( i_peek < 8 )
202         {
203             msg_Warn( p_demux, "cannot peek" );
204             return 0;
205         }
206
207         while( i_peek >= 8 )
208         {
209             if( HeaderCheck( p_peek ) )
210             {
211                 b_ok = VLC_TRUE;
212                 break;
213             }
214
215             p_peek++;
216             i_peek--;
217             i_skip++;
218         }
219
220         msg_Warn( p_demux, "garbage=%d bytes", i_skip );
221         stream_Read( p_demux->s, NULL, i_skip );
222         return 1;
223     }
224
225     memcpy( h, p_peek, 8 );    /* can't use p_peek after stream_*  */
226
227
228
229     /* set PCR */
230     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
231
232     if( ( p_block = stream_Block( p_demux->s, AAC_FRAME_SIZE( h ) ) ) == NULL )
233     {
234         msg_Warn( p_demux, "cannot read data" );
235         return 0;
236     }
237
238     p_block->i_dts = p_block->i_pts = p_sys->i_time;
239
240     es_out_Send( p_demux->out, p_sys->p_es, p_block );
241
242     p_sys->i_time += (mtime_t)1000000 *
243                      (mtime_t)AAC_FRAME_SAMPLES( h ) /
244                      (mtime_t)AAC_SAMPLE_RATE( h );
245     return( 1 );
246 }
247
248 /*****************************************************************************
249  * Close: frees unused data
250  *****************************************************************************/
251 static void Close( vlc_object_t * p_this )
252 {
253     demux_t     *p_demux = (demux_t*)p_this;
254     demux_sys_t *p_sys = p_demux->p_sys;
255
256     free( p_sys );
257 }
258
259 /*****************************************************************************
260  * Control:
261  *****************************************************************************/
262 static int Control( demux_t *p_demux, int i_query, va_list args )
263 {
264     /* demux_sys_t *p_sys  = p_demux->p_sys; */
265     /* FIXME calculate the bitrate */
266     if( i_query == DEMUX_SET_TIME )
267         return VLC_EGENERIC;
268     else
269         return demux2_vaControlHelper( p_demux->s,
270                                        0, -1,
271                                        8*0, 1, i_query, args );
272 }
273