]> git.sesse.net Git - vlc/blob - modules/demux/aac.c
* include/input_ext-intf.h : added stream_t member to input_thread_t.
[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.4 2003/09/12 16:26:40 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     mtime_t         i_time;
58
59     es_descriptor_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
104     if( p_input->psz_demux && !strncmp( p_input->psz_demux, "aac", 3 ) )
105     {
106         b_forced = VLC_TRUE;
107     }
108
109     if( p_input->psz_name )
110     {
111         int  i_len = strlen( p_input->psz_name );
112
113         if( i_len > 4 && !strcasecmp( &p_input->psz_name[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_input, "AAC module discarded" );
125         return VLC_EGENERIC;
126     }
127
128     /* skip possible id3 header */
129     p_id3 = module_Need( p_input, "id3", NULL );
130     if ( p_id3 )
131     {
132         module_Unneed( p_input, p_id3 );
133     }
134
135     p_input->pf_demux = Demux;
136     p_input->pf_demux_control = demux_vaControlDefault;
137
138     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
139     p_sys->i_time = 0;
140
141     /* peek the begining (10 is for adts header) */
142     if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
143     {
144         msg_Err( p_input, "cannot peek" );
145         goto error;
146     }
147
148     if( !strncmp( p_peek, "ADIF", 4 ) )
149     {
150         msg_Err( p_input, "ADIF file. Not yet supported. (Please report)" );
151         goto error;
152     }
153
154     if( HeaderCheck( p_peek ) )
155     {
156         input_info_category_t * p_category;
157         msg_Dbg( p_input,
158                  "adts header: id=%d channels=%d sample_rate=%d",
159                  AAC_ID( p_peek ),
160                  AAC_CHANNELS( p_peek ),
161                  AAC_SAMPLE_RATE( p_peek ) );
162
163         vlc_mutex_lock( &p_input->stream.stream_lock );
164
165         p_category = input_InfoCategory( p_input, _("Aac") );
166
167         input_AddInfo( p_category, _("Input Type"), "MPEG-%d AAC",
168                        AAC_ID( p_peek ) == 1 ? 2 : 4 );
169         input_AddInfo( p_category, _("Channels"), "%d",
170                        AAC_CHANNELS( p_peek ) );
171         input_AddInfo( p_category, _("Sample Rate"), "%dHz",
172                        AAC_SAMPLE_RATE( p_peek ) );
173
174         vlc_mutex_unlock( &p_input->stream.stream_lock );
175     }
176
177     vlc_mutex_lock( &p_input->stream.stream_lock );
178     if( input_InitStream( p_input, 0 ) == -1)
179     {
180         vlc_mutex_unlock( &p_input->stream.stream_lock );
181         msg_Err( p_input, "cannot init stream" );
182         goto error;
183     }
184     if( input_AddProgram( p_input, 0, 0) == NULL )
185     {
186         vlc_mutex_unlock( &p_input->stream.stream_lock );
187         msg_Err( p_input, "cannot add program" );
188         goto error;
189     }
190     p_input->stream.pp_programs[0]->b_is_ok = 0;
191     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
192
193     p_input->stream.i_mux_rate = 0 / 50;
194
195     p_sys->p_es = input_AddES( p_input,
196                                p_input->stream.p_selected_program,
197                                1 , AUDIO_ES, NULL, 0 );
198
199     p_sys->p_es->i_stream_id = 1;
200     p_sys->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
201     input_SelectES( p_input, p_sys->p_es );
202
203     p_input->stream.p_selected_program->b_is_ok = 1;
204     vlc_mutex_unlock( &p_input->stream.stream_lock );
205
206     return VLC_SUCCESS;
207
208 error:
209     free( p_sys );
210     return VLC_EGENERIC;
211 }
212
213
214 /*****************************************************************************
215  * Demux: reads and demuxes data packets
216  *****************************************************************************
217  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
218  *****************************************************************************/
219 static int Demux( input_thread_t * p_input )
220 {
221     demux_sys_t  *p_sys = p_input->p_demux_data;
222     pes_packet_t *p_pes;
223
224     uint8_t      h[8];
225     uint8_t      *p_peek;
226
227     if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
228     {
229         msg_Warn( p_input, "cannot peek" );
230         return 0;
231     }
232
233     if( !HeaderCheck( p_peek ) )
234     {
235         /* we need to resynch */
236         vlc_bool_t  b_ok = VLC_FALSE;
237         int         i_skip = 0;
238         int         i_peek;
239
240         i_peek = stream_Peek( p_input->s, &p_peek, 8096 );
241         if( i_peek < 8 )
242         {
243             msg_Warn( p_input, "cannot peek" );
244             return 0;
245         }
246
247         while( i_peek >= 8 )
248         {
249             if( HeaderCheck( p_peek ) )
250             {
251                 b_ok = VLC_TRUE;
252                 break;
253             }
254
255             p_peek++;
256             i_peek--;
257             i_skip++;
258         }
259
260         msg_Warn( p_input, "garbage=%d bytes", i_skip );
261         stream_Read( p_input->s, NULL, i_skip );
262         return 1;
263     }
264
265     memcpy( h, p_peek, 8 );    /* can't use p_peek after stream_*  */
266
267     input_ClockManageRef( p_input,
268                           p_input->stream.p_selected_program,
269                           p_sys->i_time * 9 / 100 );
270
271     if( ( p_pes = stream_PesPacket( p_input->s, AAC_FRAME_SIZE( h ) ) )==NULL )
272     {
273         msg_Warn( p_input, "cannot read data" );
274         return 0;
275     }
276
277     p_pes->i_dts =
278     p_pes->i_pts = input_ClockGetTS( p_input,
279                                      p_input->stream.p_selected_program,
280                                      p_sys->i_time * 9 / 100 );
281
282     if( !p_sys->p_es->p_decoder_fifo )
283     {
284         msg_Err( p_input, "no audio decoder" );
285         input_DeletePES( p_input->p_method_data, p_pes );
286         return( -1 );
287     }
288
289     input_DecodePES( p_sys->p_es->p_decoder_fifo, p_pes );
290     p_sys->i_time += (mtime_t)1000000 *
291                      (mtime_t)AAC_FRAME_SAMPLES( h ) /
292                      (mtime_t)AAC_SAMPLE_RATE( h );
293     return( 1 );
294 }
295
296 /*****************************************************************************
297  * Close: frees unused data
298  *****************************************************************************/
299 static void Close( vlc_object_t * p_this )
300 {
301     input_thread_t *p_input = (input_thread_t*)p_this;
302     demux_sys_t    *p_sys = p_input->p_demux_data;
303
304     free( p_sys );
305 }
306