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 $
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 * );
59 es_descriptor_t *p_es;
62 static int i_aac_samplerate[16] =
64 96000, 88200, 64000, 48000, 44100, 32000,
65 24000, 22050, 16000, 12000, 11025, 8000,
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
76 static inline int HeaderCheck( uint8_t *p )
79 ( p[1]&0xf6 ) != 0xf0 ||
80 AAC_SAMPLE_RATE( p ) == 0 ||
81 AAC_CHANNELS( p ) == 0 ||
82 AAC_FRAME_SIZE( p ) == 0 )
90 /*****************************************************************************
91 * Open: initializes AAC demux structures
92 *****************************************************************************/
93 static int Open( vlc_object_t * p_this )
95 input_thread_t *p_input = (input_thread_t *)p_this;
97 int b_forced = VLC_FALSE;
104 if( p_input->psz_demux && !strncmp( p_input->psz_demux, "aac", 3 ) )
109 if( p_input->psz_name )
111 int i_len = strlen( p_input->psz_name );
113 if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".aac" ) )
121 /* I haven't find any sure working aac detection so only forced or
124 msg_Warn( p_input, "AAC module discarded" );
128 /* skip possible id3 header */
129 p_id3 = module_Need( p_input, "id3", NULL );
132 module_Unneed( p_input, p_id3 );
135 p_input->pf_demux = Demux;
136 p_input->pf_demux_control = demux_vaControlDefault;
138 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
141 /* peek the begining (10 is for adts header) */
142 if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
144 msg_Err( p_input, "cannot peek" );
148 if( !strncmp( p_peek, "ADIF", 4 ) )
150 msg_Err( p_input, "ADIF file. Not yet supported. (Please report)" );
154 if( HeaderCheck( p_peek ) )
156 input_info_category_t * p_category;
158 "adts header: id=%d channels=%d sample_rate=%d",
160 AAC_CHANNELS( p_peek ),
161 AAC_SAMPLE_RATE( p_peek ) );
163 vlc_mutex_lock( &p_input->stream.stream_lock );
165 p_category = input_InfoCategory( p_input, _("Aac") );
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 ) );
174 vlc_mutex_unlock( &p_input->stream.stream_lock );
177 vlc_mutex_lock( &p_input->stream.stream_lock );
178 if( input_InitStream( p_input, 0 ) == -1)
180 vlc_mutex_unlock( &p_input->stream.stream_lock );
181 msg_Err( p_input, "cannot init stream" );
184 if( input_AddProgram( p_input, 0, 0) == NULL )
186 vlc_mutex_unlock( &p_input->stream.stream_lock );
187 msg_Err( p_input, "cannot add program" );
190 p_input->stream.pp_programs[0]->b_is_ok = 0;
191 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
193 p_input->stream.i_mux_rate = 0 / 50;
195 p_sys->p_es = input_AddES( p_input,
196 p_input->stream.p_selected_program,
197 1 , AUDIO_ES, NULL, 0 );
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 );
203 p_input->stream.p_selected_program->b_is_ok = 1;
204 vlc_mutex_unlock( &p_input->stream.stream_lock );
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 )
221 demux_sys_t *p_sys = p_input->p_demux_data;
227 if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
229 msg_Warn( p_input, "cannot peek" );
233 if( !HeaderCheck( p_peek ) )
235 /* we need to resynch */
236 vlc_bool_t b_ok = VLC_FALSE;
240 i_peek = stream_Peek( p_input->s, &p_peek, 8096 );
243 msg_Warn( p_input, "cannot peek" );
249 if( HeaderCheck( p_peek ) )
260 msg_Warn( p_input, "garbage=%d bytes", i_skip );
261 stream_Read( p_input->s, NULL, i_skip );
265 memcpy( h, p_peek, 8 ); /* can't use p_peek after stream_* */
267 input_ClockManageRef( p_input,
268 p_input->stream.p_selected_program,
269 p_sys->i_time * 9 / 100 );
271 if( ( p_pes = stream_PesPacket( p_input->s, AAC_FRAME_SIZE( h ) ) )==NULL )
273 msg_Warn( p_input, "cannot read data" );
278 p_pes->i_pts = input_ClockGetTS( p_input,
279 p_input->stream.p_selected_program,
280 p_sys->i_time * 9 / 100 );
282 if( !p_sys->p_es->p_decoder_fifo )
284 msg_Err( p_input, "no audio decoder" );
285 input_DeletePES( p_input->p_method_data, p_pes );
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 );
296 /*****************************************************************************
297 * Close: frees unused data
298 *****************************************************************************/
299 static void Close( vlc_object_t * p_this )
301 input_thread_t *p_input = (input_thread_t*)p_this;
302 demux_sys_t *p_sys = p_input->p_demux_data;