]> git.sesse.net Git - vlc/blob - modules/demux/aac.c
Improvements to preferences
[vlc] / modules / demux / aac.c
1 /*****************************************************************************
2  * aac.c : Raw aac Stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id$
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_category( CAT_INPUT );
40     set_subcategory( SUBCAT_INPUT_DEMUX );
41     set_description( _("AAC demuxer" ) );
42     set_capability( "demux2", 100 );
43     set_callbacks( Open, Close );
44     add_shortcut( "aac" );
45 vlc_module_end();
46
47 /* TODO:
48  * - adif support ?
49  *
50  */
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 struct demux_sys_t
56 {
57     mtime_t         i_time;
58
59     es_out_id_t     *p_es;
60 };
61
62 static int Demux  ( demux_t * );
63 static int Control( demux_t *, int, va_list );
64
65 static int i_aac_samplerate[16] =
66 {
67     96000, 88200, 64000, 48000, 44100, 32000,
68     24000, 22050, 16000, 12000, 11025, 8000,
69     7350,  0,     0,     0
70 };
71
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
77 /* FIXME it's plain wrong */
78 #define AAC_FRAME_SAMPLES( p )  1024
79
80 static inline int HeaderCheck( uint8_t *p )
81 {
82     if( p[0] != 0xff ||
83         ( p[1]&0xf6 ) != 0xf0 ||
84         AAC_SAMPLE_RATE( p ) == 0 ||
85         AAC_CHANNELS( p ) == 0 ||
86         AAC_FRAME_SIZE( p ) == 0 )
87     {
88         return VLC_FALSE;
89     }
90     return VLC_TRUE;
91 }
92
93 /*****************************************************************************
94  * Open: initializes AAC demux structures
95  *****************************************************************************/
96 static int Open( vlc_object_t * p_this )
97 {
98     demux_t     *p_demux = (demux_t*)p_this;
99     demux_sys_t *p_sys;
100     int         b_forced = VLC_FALSE;
101
102     uint8_t     *p_peek;
103     module_t    *p_id3;
104     es_format_t fmt;
105
106     if( !strncmp( p_demux->psz_demux, "aac", 3 ) )
107     {
108         b_forced = VLC_TRUE;
109     }
110
111     if( p_demux->psz_path )
112     {
113         int  i_len = strlen( p_demux->psz_path );
114
115         if( i_len > 4 && !strcasecmp( &p_demux->psz_path[i_len - 4], ".aac" ) )
116         {
117             b_forced = VLC_TRUE;
118         }
119     }
120
121     if( !b_forced )
122     {
123         /* I haven't find any sure working aac detection so only forced or
124          * extention check
125          */
126         msg_Warn( p_demux, "AAC module discarded" );
127         return VLC_EGENERIC;
128     }
129
130     /* skip possible id3 header */
131     if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
132     {
133         module_Unneed( p_demux, p_id3 );
134     }
135
136     p_demux->pf_demux   = Demux;
137     p_demux->pf_control = Control;
138     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
139
140     p_sys->i_time = 1;
141
142     /* peek the begining (10 is for adts header) */
143     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
144     {
145         msg_Err( p_demux, "cannot peek" );
146         goto error;
147     }
148     if( !strncmp( p_peek, "ADIF", 4 ) )
149     {
150         msg_Err( p_demux, "ADIF file. Not yet supported. (Please report)" );
151         goto error;
152     }
153
154     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
155     if( HeaderCheck( p_peek ) )
156     {
157         fmt.audio.i_channels = AAC_CHANNELS( p_peek );
158         fmt.audio.i_rate     = AAC_SAMPLE_RATE( p_peek );
159
160         msg_Dbg( p_demux,
161                  "adts header: id=%d channels=%d sample_rate=%d",
162                  AAC_ID( p_peek ),
163                  AAC_CHANNELS( p_peek ),
164                  AAC_SAMPLE_RATE( p_peek ) );
165     }
166
167     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
168     return VLC_SUCCESS;
169
170 error:
171     free( p_sys );
172     return VLC_EGENERIC;
173 }
174
175
176 /*****************************************************************************
177  * Demux: reads and demuxes data packets
178  *****************************************************************************
179  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
180  *****************************************************************************/
181 static int Demux( demux_t *p_demux )
182 {
183     demux_sys_t *p_sys = p_demux->p_sys;
184     block_t     *p_block;
185
186     uint8_t     h[8];
187     uint8_t     *p_peek;
188
189     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
190     {
191         msg_Warn( p_demux, "cannot peek" );
192         return 0;
193     }
194
195     if( !HeaderCheck( p_peek ) )
196     {
197         /* we need to resynch */
198         vlc_bool_t  b_ok = VLC_FALSE;
199         int         i_skip = 0;
200         int         i_peek;
201
202         i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
203         if( i_peek < 8 )
204         {
205             msg_Warn( p_demux, "cannot peek" );
206             return 0;
207         }
208
209         while( i_peek >= 8 )
210         {
211             if( HeaderCheck( p_peek ) )
212             {
213                 b_ok = VLC_TRUE;
214                 break;
215             }
216
217             p_peek++;
218             i_peek--;
219             i_skip++;
220         }
221
222         msg_Warn( p_demux, "garbage=%d bytes", i_skip );
223         stream_Read( p_demux->s, NULL, i_skip );
224         return 1;
225     }
226
227     memcpy( h, p_peek, 8 );    /* can't use p_peek after stream_*  */
228
229
230
231     /* set PCR */
232     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
233
234     if( ( p_block = stream_Block( p_demux->s, AAC_FRAME_SIZE( h ) ) ) == NULL )
235     {
236         msg_Warn( p_demux, "cannot read data" );
237         return 0;
238     }
239
240     p_block->i_dts = p_block->i_pts = p_sys->i_time;
241
242     es_out_Send( p_demux->out, p_sys->p_es, p_block );
243
244     p_sys->i_time += (mtime_t)1000000 *
245                      (mtime_t)AAC_FRAME_SAMPLES( h ) /
246                      (mtime_t)AAC_SAMPLE_RATE( h );
247     return( 1 );
248 }
249
250 /*****************************************************************************
251  * Close: frees unused data
252  *****************************************************************************/
253 static void Close( vlc_object_t * p_this )
254 {
255     demux_t     *p_demux = (demux_t*)p_this;
256     demux_sys_t *p_sys = p_demux->p_sys;
257
258     free( p_sys );
259 }
260
261 /*****************************************************************************
262  * Control:
263  *****************************************************************************/
264 static int Control( demux_t *p_demux, int i_query, va_list args )
265 {
266     /* demux_sys_t *p_sys  = p_demux->p_sys; */
267     /* FIXME calculate the bitrate */
268     if( i_query == DEMUX_SET_TIME )
269         return VLC_EGENERIC;
270     else
271         return demux2_vaControlHelper( p_demux->s,
272                                        0, -1,
273                                        8*0, 1, i_query, args );
274 }
275