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