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 $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Open ( vlc_object_t * );
38 static void Close ( vlc_object_t * );
41 set_description( _("AAC demuxer" ) );
42 set_capability( "demux", 10 );
43 set_callbacks( Open, Close );
44 add_shortcut( "aac" );
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Demux ( input_thread_t * );
62 es_descriptor_t *p_es;
65 static int i_aac_samplerate[16] =
67 96000, 88200, 64000, 48000, 44100, 32000,
68 24000, 22050, 16000, 12000, 11025, 8000,
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
79 static inline int HeaderCheck( uint8_t *p )
82 ( p[1]&0xf6 ) != 0xf0 ||
83 AAC_SAMPLE_RATE( p ) == 0 ||
84 AAC_CHANNELS( p ) == 0 ||
85 AAC_FRAME_SIZE( p ) == 0 )
93 /*****************************************************************************
94 * Open: initializes AAC demux structures
95 *****************************************************************************/
96 static int Open( vlc_object_t * p_this )
98 input_thread_t *p_input = (input_thread_t *)p_this;
100 int b_forced = VLC_FALSE;
107 if( p_input->psz_demux && !strncmp( p_input->psz_demux, "aac", 3 ) )
112 if( p_input->psz_name )
114 int i_len = strlen( p_input->psz_name );
116 if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".aac" ) )
124 /* I haven't find any sure working aac detection so only forced or
127 msg_Warn( p_input, "AAC module discarded" );
131 /* skip possible id3 header */
132 p_id3 = module_Need( p_input, "id3", NULL );
135 module_Unneed( p_input, p_id3 );
138 p_input->pf_demux = Demux;
140 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
143 if( ( p_sys->s = stream_OpenInput( p_input ) ) == NULL )
145 msg_Err( p_input, "cannot create stream" );
149 /* peek the begining (10 is for adts header) */
150 if( stream_Peek( p_sys->s, &p_peek, 10 ) < 10 )
152 msg_Err( p_input, "cannot peek" );
156 if( !strncmp( p_peek, "ADIF", 4 ) )
158 msg_Err( p_input, "ADIF file. Not yet supported. (Please report)" );
162 if( HeaderCheck( p_peek ) )
164 input_info_category_t * p_category;
166 "adts header: id=%d channels=%d sample_rate=%d",
168 AAC_CHANNELS( p_peek ),
169 AAC_SAMPLE_RATE( p_peek ) );
171 vlc_mutex_lock( &p_input->stream.stream_lock );
173 p_category = input_InfoCategory( p_input, _("Aac") );
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 ) );
182 vlc_mutex_unlock( &p_input->stream.stream_lock );
185 vlc_mutex_lock( &p_input->stream.stream_lock );
186 if( input_InitStream( p_input, 0 ) == -1)
188 vlc_mutex_unlock( &p_input->stream.stream_lock );
189 msg_Err( p_input, "cannot init stream" );
192 if( input_AddProgram( p_input, 0, 0) == NULL )
194 vlc_mutex_unlock( &p_input->stream.stream_lock );
195 msg_Err( p_input, "cannot add program" );
198 p_input->stream.pp_programs[0]->b_is_ok = 0;
199 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
201 p_input->stream.i_mux_rate = 0 / 50;
203 p_sys->p_es = input_AddES( p_input,
204 p_input->stream.p_selected_program,
205 1 , AUDIO_ES, NULL, 0 );
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 );
211 p_input->stream.p_selected_program->b_is_ok = 1;
212 vlc_mutex_unlock( &p_input->stream.stream_lock );
219 stream_Release( p_sys->s );
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 )
233 demux_sys_t *p_sys = p_input->p_demux_data;
239 if( stream_Peek( p_sys->s, &p_peek, 8 ) < 8 )
241 msg_Warn( p_input, "cannot peek" );
245 if( !HeaderCheck( p_peek ) )
247 /* we need to resynch */
248 vlc_bool_t b_ok = VLC_FALSE;
252 i_peek = stream_Peek( p_sys->s, &p_peek, 8096 );
255 msg_Warn( p_input, "cannot peek" );
261 if( HeaderCheck( p_peek ) )
272 msg_Warn( p_input, "garbage=%d bytes", i_skip );
273 stream_Read( p_sys->s, NULL, i_skip );
277 memcpy( h, p_peek, 8 ); /* can't use p_peek after stream_* */
279 input_ClockManageRef( p_input,
280 p_input->stream.p_selected_program,
281 p_sys->i_time * 9 / 100 );
283 if( ( p_pes = stream_PesPacket( p_sys->s, AAC_FRAME_SIZE( h ) ) ) == NULL )
285 msg_Warn( p_input, "cannot read data" );
290 p_pes->i_pts = input_ClockGetTS( p_input,
291 p_input->stream.p_selected_program,
292 p_sys->i_time * 9 / 100 );
294 if( !p_sys->p_es->p_decoder_fifo )
296 msg_Err( p_input, "no audio decoder" );
297 input_DeletePES( p_input->p_method_data, p_pes );
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 );
308 /*****************************************************************************
309 * Close: frees unused data
310 *****************************************************************************/
311 static void Close( vlc_object_t * p_this )
313 input_thread_t *p_input = (input_thread_t*)p_this;
314 demux_sys_t *p_sys = p_input->p_demux_data;
318 stream_Release( p_sys->s );