]> git.sesse.net Git - vlc/blob - modules/demux/aac.c
add45471399133028ac55d6455e71765172d4683
[vlc] / modules / demux / aac.c
1 /*****************************************************************************
2  * aac.c : Raw aac Stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: aac.c,v 1.2 2003/08/01 00:40:05 fenrir 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 #include <ninput.h>
33
34 /*****************************************************************************
35  * Module descriptor
36  *****************************************************************************/
37 static int  Open    ( vlc_object_t * );
38 static void Close  ( vlc_object_t * );
39
40 vlc_module_begin();
41     set_description( _("AAC demuxer" ) );
42     set_capability( "demux", 10 );
43     set_callbacks( Open, Close );
44     add_shortcut( "aac" );
45 vlc_module_end();
46
47 /* TODO:
48  * - adif support ?
49  *
50  */
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static int  Demux       ( input_thread_t * );
56
57 struct demux_sys_t
58 {
59     stream_t        *s;
60     mtime_t         i_time;
61
62     es_descriptor_t *p_es;
63 };
64
65 static int i_aac_samplerate[16] =
66 {
67     96000, 88200, 64000, 48000, 44100, 32000, 
68     24000, 22050, 16000, 12000, 11025, 8000,
69     7350,  0,     0,     0
70 };
71
72 #define AAC_ID( p )          ( ((p)[1]>>3)&0x01 )
73 #define AAC_SAMPLE_RATE( p ) i_aac_samplerate[((p)[2]>>2)&0x0f]
74 #define AAC_CHANNELS( p )    ( (((p)[2]&0x01)<<2) | (((p)[3]>>6)&0x03) )
75 #define AAC_FRAME_SIZE( p )  ( (((p)[3]&0x03) << 11)|( (p)[4] << 3 )|( (((p)[5]) >>5)&0x7 ) )
76 /* FIXME it's plain wrong */
77 #define AAC_FRAME_SAMPLES( p )  1024
78
79 static inline int HeaderCheck( uint8_t *p )
80 {
81     if( p[0] != 0xff ||
82         ( p[1]&0xf6 ) != 0xf0 ||
83         AAC_SAMPLE_RATE( p ) == 0 ||
84         AAC_CHANNELS( p ) == 0 ||
85         AAC_FRAME_SIZE( p ) == 0 )
86     {
87         return VLC_FALSE;
88     }
89     return VLC_TRUE;
90 }
91
92
93 /*****************************************************************************
94  * Open: initializes AAC demux structures
95  *****************************************************************************/
96 static int Open( vlc_object_t * p_this )
97 {
98     input_thread_t *p_input = (input_thread_t *)p_this;
99     demux_sys_t    *p_sys;
100     int            b_forced = VLC_FALSE;
101
102     uint8_t        *p_peek;
103
104     module_t       *p_id3;
105
106
107     if( p_input->psz_demux && !strncmp( p_input->psz_demux, "aac", 3 ) )
108     {
109         b_forced = VLC_TRUE;
110     }
111
112     if( p_input->psz_name )
113     {
114         int  i_len = strlen( p_input->psz_name );
115
116         if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".aac" ) )
117         {
118             b_forced = VLC_TRUE;
119         }
120     }
121
122     if( !b_forced )
123     {
124         /* I haven't find any sure working aac detection so only forced or
125          * extention check
126          */
127         msg_Warn( p_input, "AAC module discarded" );
128         return VLC_EGENERIC;
129     }
130
131     /* skip possible id3 header */
132     p_id3 = module_Need( p_input, "id3", NULL );
133     if ( p_id3 )
134     {
135         module_Unneed( p_input, p_id3 );
136     }
137
138     p_input->pf_demux = Demux;
139
140     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
141     p_sys->i_time = 0;
142
143     if( ( p_sys->s = stream_OpenInput( p_input ) ) == NULL )
144     {
145         msg_Err( p_input, "cannot create stream" );
146         goto error;
147     }
148
149     /* peek the begining (10 is for adts header) */
150     if( stream_Peek( p_sys->s, &p_peek, 10 ) < 10 )
151     {
152         msg_Err( p_input, "cannot peek" );
153         goto error;
154     }
155
156     if( !strncmp( p_peek, "ADIF", 4 ) )
157     {
158         msg_Err( p_input, "ADIF file. Not yet supported. (Please report)" );
159         goto error;
160     }
161
162     if( HeaderCheck( p_peek ) )
163     {
164         input_info_category_t * p_category;
165         msg_Dbg( p_input,
166                  "adts header: id=%d channels=%d sample_rate=%d",
167                  AAC_ID( p_peek ),
168                  AAC_CHANNELS( p_peek ),
169                  AAC_SAMPLE_RATE( p_peek ) );
170
171         vlc_mutex_lock( &p_input->stream.stream_lock );
172
173         p_category = input_InfoCategory( p_input, _("Aac") );
174
175         input_AddInfo( p_category, _("Input Type"), "MPEG-%d AAC",
176                        AAC_ID( p_peek ) == 1 ? 2 : 4 );
177         input_AddInfo( p_category, _("Channels"), "%d",
178                        AAC_CHANNELS( p_peek ) );
179         input_AddInfo( p_category, _("Sample Rate"), "%dHz",
180                        AAC_SAMPLE_RATE( p_peek ) );
181
182         vlc_mutex_unlock( &p_input->stream.stream_lock );
183     }
184
185     vlc_mutex_lock( &p_input->stream.stream_lock );
186     if( input_InitStream( p_input, 0 ) == -1)
187     {
188         vlc_mutex_unlock( &p_input->stream.stream_lock );
189         msg_Err( p_input, "cannot init stream" );
190         goto error;
191     }
192     if( input_AddProgram( p_input, 0, 0) == NULL )
193     {
194         vlc_mutex_unlock( &p_input->stream.stream_lock );
195         msg_Err( p_input, "cannot add program" );
196         goto error;
197     }
198     p_input->stream.pp_programs[0]->b_is_ok = 0;
199     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
200
201     p_input->stream.i_mux_rate = 0 / 50;
202
203     p_sys->p_es = input_AddES( p_input,
204                                p_input->stream.p_selected_program,
205                                1 , AUDIO_ES, NULL, 0 );
206
207     p_sys->p_es->i_stream_id = 1;
208     p_sys->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
209     input_SelectES( p_input, p_sys->p_es );
210
211     p_input->stream.p_selected_program->b_is_ok = 1;
212     vlc_mutex_unlock( &p_input->stream.stream_lock );
213
214     return VLC_SUCCESS;
215
216 error:
217     if( p_sys->s )
218     {
219         stream_Release( p_sys->s );
220     }
221     free( p_sys );
222     return VLC_EGENERIC;
223 }
224
225
226 /*****************************************************************************
227  * Demux: reads and demuxes data packets
228  *****************************************************************************
229  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
230  *****************************************************************************/
231 static int Demux( input_thread_t * p_input )
232 {
233     demux_sys_t  *p_sys = p_input->p_demux_data;
234     pes_packet_t *p_pes;
235
236     uint8_t      h[8];
237     uint8_t      *p_peek;
238
239     if( stream_Peek( p_sys->s, &p_peek, 8 ) < 8 )
240     {
241         msg_Warn( p_input, "cannot peek" );
242         return 0;
243     }
244
245     if( !HeaderCheck( p_peek ) )
246     {
247         /* we need to resynch */
248         vlc_bool_t  b_ok = VLC_FALSE;
249         int         i_skip = 0;
250         int         i_peek;
251
252         i_peek = stream_Peek( p_sys->s, &p_peek, 8096 );
253         if( i_peek < 8 )
254         {
255             msg_Warn( p_input, "cannot peek" );
256             return 0;
257         }
258
259         while( i_peek >= 8 )
260         {
261             if( HeaderCheck( p_peek ) )
262             {
263                 b_ok = VLC_TRUE;
264                 break;
265             }
266
267             p_peek++;
268             i_peek--;
269             i_skip++;
270         }
271
272         msg_Warn( p_input, "garbage=%d bytes", i_skip );
273         stream_Read( p_sys->s, NULL, i_skip );
274         return 1;
275     }
276
277     memcpy( h, p_peek, 8 );    /* can't use p_peek after stream_*  */
278
279     input_ClockManageRef( p_input,
280                           p_input->stream.p_selected_program,
281                           p_sys->i_time * 9 / 100 );
282
283     if( ( p_pes = stream_PesPacket( p_sys->s, AAC_FRAME_SIZE( h ) ) ) == NULL )
284     {
285         msg_Warn( p_input, "cannot read data" );
286         return 0;
287     }
288
289     p_pes->i_dts =
290     p_pes->i_pts = input_ClockGetTS( p_input,
291                                      p_input->stream.p_selected_program,
292                                      p_sys->i_time * 9 / 100 );
293
294     if( !p_sys->p_es->p_decoder_fifo )
295     {
296         msg_Err( p_input, "no audio decoder" );
297         input_DeletePES( p_input->p_method_data, p_pes );
298         return( -1 );
299     }
300
301     input_DecodePES( p_sys->p_es->p_decoder_fifo, p_pes );
302     p_sys->i_time += (mtime_t)1000000 *
303                      (mtime_t)AAC_FRAME_SAMPLES( h ) /
304                      (mtime_t)AAC_SAMPLE_RATE( h );
305     return( 1 );
306 }
307
308 /*****************************************************************************
309  * Close: frees unused data
310  *****************************************************************************/
311 static void Close( vlc_object_t * p_this )
312 {
313     input_thread_t *p_input = (input_thread_t*)p_this;
314     demux_sys_t    *p_sys = p_input->p_demux_data;
315
316     if( p_sys->s )
317     {
318         stream_Release( p_sys->s );
319     }
320     free( p_sys );
321 }
322