1 /*****************************************************************************
2 * aac.c : Raw aac Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
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( "demux2", 100 );
41 set_callbacks( Open, Close );
42 add_shortcut( "aac" );
50 /*****************************************************************************
52 *****************************************************************************/
60 static int Demux ( demux_t * );
61 static int Control( demux_t *, int, va_list );
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 ) )
75 /* FIXME it's plain wrong */
76 #define AAC_FRAME_SAMPLES( p ) 1024
78 static inline int HeaderCheck( uint8_t *p )
81 ( p[1]&0xf6 ) != 0xf0 ||
82 AAC_SAMPLE_RATE( p ) == 0 ||
83 AAC_CHANNELS( p ) == 0 ||
84 AAC_FRAME_SIZE( p ) == 0 )
91 /*****************************************************************************
92 * Open: initializes AAC demux structures
93 *****************************************************************************/
94 static int Open( vlc_object_t * p_this )
96 demux_t *p_demux = (demux_t*)p_this;
98 int b_forced = VLC_FALSE;
104 if( !strncmp( p_demux->psz_demux, "aac", 3 ) )
109 if( p_demux->psz_path )
111 int i_len = strlen( p_demux->psz_path );
113 if( i_len > 4 && !strcasecmp( &p_demux->psz_path[i_len - 4], ".aac" ) )
121 /* I haven't find any sure working aac detection so only forced or
124 msg_Warn( p_demux, "AAC module discarded" );
128 /* skip possible id3 header */
129 if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
131 module_Unneed( p_demux, p_id3 );
134 p_demux->pf_demux = Demux;
135 p_demux->pf_control = Control;
136 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
140 /* peek the begining (10 is for adts header) */
141 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
143 msg_Err( p_demux, "cannot peek" );
146 if( !strncmp( p_peek, "ADIF", 4 ) )
148 msg_Err( p_demux, "ADIF file. Not yet supported. (Please report)" );
152 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
153 if( HeaderCheck( p_peek ) )
155 fmt.audio.i_channels = AAC_CHANNELS( p_peek );
156 fmt.audio.i_rate = AAC_SAMPLE_RATE( p_peek );
159 "adts header: id=%d channels=%d sample_rate=%d",
161 AAC_CHANNELS( p_peek ),
162 AAC_SAMPLE_RATE( p_peek ) );
165 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
174 /*****************************************************************************
175 * Demux: reads and demuxes data packets
176 *****************************************************************************
177 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
178 *****************************************************************************/
179 static int Demux( demux_t *p_demux )
181 demux_sys_t *p_sys = p_demux->p_sys;
187 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
189 msg_Warn( p_demux, "cannot peek" );
193 if( !HeaderCheck( p_peek ) )
195 /* we need to resynch */
196 vlc_bool_t b_ok = VLC_FALSE;
200 i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
203 msg_Warn( p_demux, "cannot peek" );
209 if( HeaderCheck( p_peek ) )
220 msg_Warn( p_demux, "garbage=%d bytes", i_skip );
221 stream_Read( p_demux->s, NULL, i_skip );
225 memcpy( h, p_peek, 8 ); /* can't use p_peek after stream_* */
230 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
232 if( ( p_block = stream_Block( p_demux->s, AAC_FRAME_SIZE( h ) ) ) == NULL )
234 msg_Warn( p_demux, "cannot read data" );
238 p_block->i_dts = p_block->i_pts = p_sys->i_time;
240 es_out_Send( p_demux->out, p_sys->p_es, p_block );
242 p_sys->i_time += (mtime_t)1000000 *
243 (mtime_t)AAC_FRAME_SAMPLES( h ) /
244 (mtime_t)AAC_SAMPLE_RATE( h );
248 /*****************************************************************************
249 * Close: frees unused data
250 *****************************************************************************/
251 static void Close( vlc_object_t * p_this )
253 demux_t *p_demux = (demux_t*)p_this;
254 demux_sys_t *p_sys = p_demux->p_sys;
259 /*****************************************************************************
261 *****************************************************************************/
262 static int Control( demux_t *p_demux, int i_query, va_list args )
264 /* demux_sys_t *p_sys = p_demux->p_sys; */
265 /* FIXME calculate the bitrate */
266 if( i_query == DEMUX_SET_TIME )
269 return demux2_vaControlHelper( p_demux->s,
271 8*0, 1, i_query, args );