]> git.sesse.net Git - vlc/blob - modules/demux/aac.c
35bbb0bce44215eb648a914e4d24d0458539e1f0
[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: aac.c,v 1.9 2004/01/25 20:05:28 hartman Exp $
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( "demux", 10 );
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 static int  Demux       ( input_thread_t * );
54
55 struct demux_sys_t
56 {
57     mtime_t         i_time;
58
59     es_out_id_t     *p_es;
60 };
61
62 static int i_aac_samplerate[16] =
63 {
64     96000, 88200, 64000, 48000, 44100, 32000, 
65     24000, 22050, 16000, 12000, 11025, 8000,
66     7350,  0,     0,     0
67 };
68
69 #define AAC_ID( p )          ( ((p)[1]>>3)&0x01 )
70 #define AAC_SAMPLE_RATE( p ) i_aac_samplerate[((p)[2]>>2)&0x0f]
71 #define AAC_CHANNELS( p )    ( (((p)[2]&0x01)<<2) | (((p)[3]>>6)&0x03) )
72 #define AAC_FRAME_SIZE( p )  ( (((p)[3]&0x03) << 11)|( (p)[4] << 3 )|( (((p)[5]) >>5)&0x7 ) )
73 /* FIXME it's plain wrong */
74 #define AAC_FRAME_SAMPLES( p )  1024
75
76 static inline int HeaderCheck( uint8_t *p )
77 {
78     if( p[0] != 0xff ||
79         ( p[1]&0xf6 ) != 0xf0 ||
80         AAC_SAMPLE_RATE( p ) == 0 ||
81         AAC_CHANNELS( p ) == 0 ||
82         AAC_FRAME_SIZE( p ) == 0 )
83     {
84         return VLC_FALSE;
85     }
86     return VLC_TRUE;
87 }
88
89
90 /*****************************************************************************
91  * Open: initializes AAC demux structures
92  *****************************************************************************/
93 static int Open( vlc_object_t * p_this )
94 {
95     input_thread_t *p_input = (input_thread_t *)p_this;
96     demux_sys_t    *p_sys;
97     int            b_forced = VLC_FALSE;
98
99     uint8_t        *p_peek;
100
101     module_t       *p_id3;
102
103     es_format_t    fmt;
104
105
106     if( p_input->psz_demux && !strncmp( p_input->psz_demux, "aac", 3 ) )
107     {
108         b_forced = VLC_TRUE;
109     }
110
111     if( p_input->psz_name )
112     {
113         int  i_len = strlen( p_input->psz_name );
114
115         if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".aac" ) )
116         {
117             b_forced = VLC_TRUE;
118         }
119     }
120
121     if( !b_forced )
122     {
123         /* I haven't find any sure working aac detection so only forced or
124          * extention check
125          */
126         msg_Warn( p_input, "AAC module discarded" );
127         return VLC_EGENERIC;
128     }
129
130     /* skip possible id3 header */
131     p_id3 = module_Need( p_input, "id3", NULL );
132     if ( p_id3 )
133     {
134         module_Unneed( p_input, p_id3 );
135     }
136
137     p_input->pf_demux = Demux;
138     p_input->pf_demux_control = demux_vaControlDefault;
139
140     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
141     p_sys->i_time = 0;
142
143     /* peek the begining (10 is for adts header) */
144     if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
145     {
146         msg_Err( p_input, "cannot peek" );
147         goto error;
148     }
149
150     if( !strncmp( p_peek, "ADIF", 4 ) )
151     {
152         msg_Err( p_input, "ADIF file. Not yet supported. (Please report)" );
153         goto error;
154     }
155
156     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
157     if( HeaderCheck( p_peek ) )
158     {
159         fmt.audio.i_channels = AAC_CHANNELS( p_peek );
160         fmt.audio.i_rate = AAC_SAMPLE_RATE( p_peek );
161
162         msg_Dbg( p_input,
163                  "adts header: id=%d channels=%d sample_rate=%d",
164                  AAC_ID( p_peek ),
165                  AAC_CHANNELS( p_peek ),
166                  AAC_SAMPLE_RATE( p_peek ) );
167     }
168
169     vlc_mutex_lock( &p_input->stream.stream_lock );
170     if( input_InitStream( p_input, 0 ) == -1)
171     {
172         vlc_mutex_unlock( &p_input->stream.stream_lock );
173         msg_Err( p_input, "cannot init stream" );
174         goto error;
175     }
176     p_input->stream.i_mux_rate = 0 / 50;
177     vlc_mutex_unlock( &p_input->stream.stream_lock );
178
179     p_sys->p_es = es_out_Add( p_input->p_es_out, &fmt );
180
181     return VLC_SUCCESS;
182
183 error:
184     free( p_sys );
185     return VLC_EGENERIC;
186 }
187
188
189 /*****************************************************************************
190  * Demux: reads and demuxes data packets
191  *****************************************************************************
192  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
193  *****************************************************************************/
194 static int Demux( input_thread_t * p_input )
195 {
196     demux_sys_t  *p_sys = p_input->p_demux_data;
197     block_t *p_block;
198
199     uint8_t      h[8];
200     uint8_t      *p_peek;
201
202     if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
203     {
204         msg_Warn( p_input, "cannot peek" );
205         return 0;
206     }
207
208     if( !HeaderCheck( p_peek ) )
209     {
210         /* we need to resynch */
211         vlc_bool_t  b_ok = VLC_FALSE;
212         int         i_skip = 0;
213         int         i_peek;
214
215         i_peek = stream_Peek( p_input->s, &p_peek, 8096 );
216         if( i_peek < 8 )
217         {
218             msg_Warn( p_input, "cannot peek" );
219             return 0;
220         }
221
222         while( i_peek >= 8 )
223         {
224             if( HeaderCheck( p_peek ) )
225             {
226                 b_ok = VLC_TRUE;
227                 break;
228             }
229
230             p_peek++;
231             i_peek--;
232             i_skip++;
233         }
234
235         msg_Warn( p_input, "garbage=%d bytes", i_skip );
236         stream_Read( p_input->s, NULL, i_skip );
237         return 1;
238     }
239
240     memcpy( h, p_peek, 8 );    /* can't use p_peek after stream_*  */
241
242     input_ClockManageRef( p_input,
243                           p_input->stream.p_selected_program,
244                           p_sys->i_time * 9 / 100 );
245
246     if( ( p_block = stream_Block( p_input->s, AAC_FRAME_SIZE( h ) ) ) == NULL )
247     {
248         msg_Warn( p_input, "cannot read data" );
249         return 0;
250     }
251
252     p_block->i_dts =
253     p_block->i_pts = input_ClockGetTS( p_input,
254                                      p_input->stream.p_selected_program,
255                                      p_sys->i_time * 9 / 100 );
256
257     es_out_Send( p_input->p_es_out, p_sys->p_es, p_block );
258
259     p_sys->i_time += (mtime_t)1000000 *
260                      (mtime_t)AAC_FRAME_SAMPLES( h ) /
261                      (mtime_t)AAC_SAMPLE_RATE( h );
262     return( 1 );
263 }
264
265 /*****************************************************************************
266  * Close: frees unused data
267  *****************************************************************************/
268 static void Close( vlc_object_t * p_this )
269 {
270     input_thread_t *p_input = (input_thread_t*)p_this;
271     demux_sys_t    *p_sys = p_input->p_demux_data;
272
273     free( p_sys );
274 }
275