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 $
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>
32 /*****************************************************************************
34 *****************************************************************************/
35 static int Open ( vlc_object_t * );
36 static void Close ( vlc_object_t * );
39 set_description( _("AAC demuxer" ) );
40 set_capability( "demux", 10 );
41 set_callbacks( Open, Close );
42 add_shortcut( "aac" );
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Demux ( input_thread_t * );
60 es_descriptor_t *p_es;
63 static int i_aac_samplerate[16] =
65 96000, 88200, 64000, 48000, 44100, 32000,
66 24000, 22050, 16000, 12000, 11025, 8000,
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
77 static inline int HeaderCheck( uint8_t *p )
80 ( p[1]&0xf6 ) != 0xf0 ||
81 AAC_SAMPLE_RATE( p ) == 0 ||
82 AAC_CHANNELS( p ) == 0 ||
83 AAC_FRAME_SIZE( p ) == 0 )
91 /*****************************************************************************
92 * Open: initializes AAC demux structures
93 *****************************************************************************/
94 static int Open( vlc_object_t * p_this )
96 input_thread_t *p_input = (input_thread_t *)p_this;
98 int b_forced = VLC_FALSE;
105 if( p_input->psz_demux && !strncmp( p_input->psz_demux, "aac", 3 ) )
110 if( p_input->psz_name )
112 int i_len = strlen( p_input->psz_name );
114 if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".aac" ) )
122 /* I haven't find any sure working aac detection so only forced or
125 msg_Warn( p_input, "AAC module discarded" );
129 /* skip possible id3 header */
130 p_id3 = module_Need( p_input, "id3", NULL );
133 module_Unneed( p_input, p_id3 );
136 p_input->pf_demux = Demux;
137 p_input->pf_demux_control = demux_vaControlDefault;
139 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
142 if( ( p_sys->s = stream_OpenInput( p_input ) ) == NULL )
144 msg_Err( p_input, "cannot create stream" );
148 /* peek the begining (10 is for adts header) */
149 if( stream_Peek( p_sys->s, &p_peek, 10 ) < 10 )
151 msg_Err( p_input, "cannot peek" );
155 if( !strncmp( p_peek, "ADIF", 4 ) )
157 msg_Err( p_input, "ADIF file. Not yet supported. (Please report)" );
161 if( HeaderCheck( p_peek ) )
163 input_info_category_t * p_category;
165 "adts header: id=%d channels=%d sample_rate=%d",
167 AAC_CHANNELS( p_peek ),
168 AAC_SAMPLE_RATE( p_peek ) );
170 vlc_mutex_lock( &p_input->stream.stream_lock );
172 p_category = input_InfoCategory( p_input, _("Aac") );
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 ) );
181 vlc_mutex_unlock( &p_input->stream.stream_lock );
184 vlc_mutex_lock( &p_input->stream.stream_lock );
185 if( input_InitStream( p_input, 0 ) == -1)
187 vlc_mutex_unlock( &p_input->stream.stream_lock );
188 msg_Err( p_input, "cannot init stream" );
191 if( input_AddProgram( p_input, 0, 0) == NULL )
193 vlc_mutex_unlock( &p_input->stream.stream_lock );
194 msg_Err( p_input, "cannot add program" );
197 p_input->stream.pp_programs[0]->b_is_ok = 0;
198 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
200 p_input->stream.i_mux_rate = 0 / 50;
202 p_sys->p_es = input_AddES( p_input,
203 p_input->stream.p_selected_program,
204 1 , AUDIO_ES, NULL, 0 );
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 );
210 p_input->stream.p_selected_program->b_is_ok = 1;
211 vlc_mutex_unlock( &p_input->stream.stream_lock );
218 stream_Release( p_sys->s );
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 )
232 demux_sys_t *p_sys = p_input->p_demux_data;
238 if( stream_Peek( p_sys->s, &p_peek, 8 ) < 8 )
240 msg_Warn( p_input, "cannot peek" );
244 if( !HeaderCheck( p_peek ) )
246 /* we need to resynch */
247 vlc_bool_t b_ok = VLC_FALSE;
251 i_peek = stream_Peek( p_sys->s, &p_peek, 8096 );
254 msg_Warn( p_input, "cannot peek" );
260 if( HeaderCheck( p_peek ) )
271 msg_Warn( p_input, "garbage=%d bytes", i_skip );
272 stream_Read( p_sys->s, NULL, i_skip );
276 memcpy( h, p_peek, 8 ); /* can't use p_peek after stream_* */
278 input_ClockManageRef( p_input,
279 p_input->stream.p_selected_program,
280 p_sys->i_time * 9 / 100 );
282 if( ( p_pes = stream_PesPacket( p_sys->s, AAC_FRAME_SIZE( h ) ) ) == NULL )
284 msg_Warn( p_input, "cannot read data" );
289 p_pes->i_pts = input_ClockGetTS( p_input,
290 p_input->stream.p_selected_program,
291 p_sys->i_time * 9 / 100 );
293 if( !p_sys->p_es->p_decoder_fifo )
295 msg_Err( p_input, "no audio decoder" );
296 input_DeletePES( p_input->p_method_data, p_pes );
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 );
307 /*****************************************************************************
308 * Close: frees unused data
309 *****************************************************************************/
310 static void Close( vlc_object_t * p_this )
312 input_thread_t *p_input = (input_thread_t*)p_this;
313 demux_sys_t *p_sys = p_input->p_demux_data;
317 stream_Release( p_sys->s );