1 /*****************************************************************************
2 * mpga.c : MPEG-I/II Audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 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>
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Open ( vlc_object_t * );
38 static void Close( vlc_object_t * );
41 set_description( _("MPEG-I/II audio demuxer" ) );
42 set_capability( "demux2", 100 );
43 set_callbacks( Open, Close );
44 add_shortcut( "mpga" );
45 add_shortcut( "mp3" );
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Demux ( demux_t * );
56 static int Control( demux_t *, int, va_list );
62 int i_bitrate_avg; /* extracted from Xing header */
69 static int HeaderCheck( uint32_t h )
71 if( ((( h >> 21 )&0x07FF) != 0x07FF ) /* header sync */
72 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
73 || (((h >> 12)&0x0F) == 0x0F )
74 || (((h >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
75 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
76 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
83 static int mpga_sample_rate[2][4] =
85 { 44100, 48000, 32000, 0 },
86 { 22050, 24000, 16000, 0 }
89 static int mpga_bitrate[2][3][16] =
92 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
93 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
94 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}
97 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
98 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
99 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}
104 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
105 #define MPGA_LAYER( h ) ( 3 - (((h)>>17)&0x03) )
106 #define MPGA_SAMPLE_RATE(h) \
107 ( mpga_sample_rate[MPGA_VERSION(h)][((h)>>10)&0x03] / ( ((h>>20)&0x01) ? 1 : 2) )
108 #define MPGA_CHANNELS(h) ( (((h)>>6)&0x03) == 3 ? 1 : 2)
109 #define MPGA_BITRATE(h) mpga_bitrate[MPGA_VERSION(h)][MPGA_LAYER(h)][((h)>>12)&0x0f]
110 #define MPGA_PADDING(h) ( ((h)>>9)&0x01 )
111 #define MPGA_MODE(h) (((h)>> 6)&0x03)
113 static int mpga_frame_size( uint32_t h )
115 switch( MPGA_LAYER(h) )
118 return ( ( 12000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h) ) * 4;
120 return ( 144000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
122 return ( ( MPGA_VERSION(h) ? 72000 : 144000 ) * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
128 static int mpga_frame_samples( uint32_t h )
130 switch( MPGA_LAYER(h) )
137 return MPGA_VERSION(h) ? 576 : 1152;
143 /*****************************************************************************
144 * Open: initializes demux structures
145 *****************************************************************************/
146 static int Open( vlc_object_t * p_this )
148 demux_t *p_demux = (demux_t*)p_this;
150 vlc_bool_t b_forced = VLC_FALSE;
151 vlc_bool_t b_extention = VLC_FALSE;
158 if( !strncmp( p_demux->psz_demux, "mpga", 4 ) ||
159 !strncmp( p_demux->psz_demux, "mp3", 3 ) )
163 if( p_demux->psz_path )
165 int i_len = strlen( p_demux->psz_path );
166 if( i_len > 4 && !strcasecmp( &p_demux->psz_path[i_len - 4], ".mp3" ) )
168 b_extention = VLC_TRUE;
172 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
174 p_sys->i_bitrate_avg = 0;
177 /* skip/parse possible id3 header */
178 if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
180 p_sys->meta = (vlc_meta_t *)p_demux->p_private;
182 msg_Dbg( p_demux, "Title : %s",
183 vlc_meta_GetValue( p_sys->meta,VLC_META_TITLE ) );
184 p_demux->p_private = NULL;
185 module_Unneed( p_demux, p_id3 );
188 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
190 msg_Err( p_demux, "cannot peek" );
194 if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
196 vlc_bool_t b_ok = VLC_FALSE;
199 if( !b_forced && !b_extention )
201 msg_Warn( p_demux, "mpga module discarded" );
205 i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
209 if( HeaderCheck( header = GetDWBE( p_peek ) ) )
217 if( !b_ok && !b_forced )
219 msg_Warn( p_demux, "mpga module discarded" );
224 p_demux->pf_demux = Demux;
225 p_demux->pf_control = Control;
227 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
229 if( HeaderCheck( header ) )
233 char psz_description[50];
235 p_sys->i_bitrate_avg = MPGA_BITRATE( header ) * 1000;
236 if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) >= 21 )
240 if( MPGA_VERSION( header) == 0 )
242 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
246 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
248 if( i_skip + 8 < i_xing &&
249 !strncmp( &p_xing[i_skip], "Xing", 4 ) )
251 unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
252 unsigned int i_bytes = 0, i_frames = 0;
254 p_xing += i_skip + 8;
255 i_xing -= i_skip + 8;
258 if( i_flags&0x01 && i_skip + 4 <= i_xing ) /* XING_FRAMES */
260 i_frames = GetDWBE( &p_xing[i_skip] );
263 if( i_flags&0x02 && i_skip + 4 <= i_xing ) /* XING_BYTES */
265 i_bytes = GetDWBE( &p_xing[i_skip] );
268 if( i_flags&0x04 ) /* XING_TOC */
273 // FIXME: doesn't return the right bitrage average, at least with some MP3's
274 if( i_flags&0x08 && i_skip + 4 <= i_xing ) /* XING_VBR */
276 p_sys->i_bitrate_avg = GetDWBE( &p_xing[i_skip] );
277 fprintf(stderr,"rate2 %d\n", p_sys->i_bitrate_avg);
278 msg_Dbg( p_input, "xing vbr value present (%d)", p_sys->i_bitrate_avg );
282 if( i_frames > 0 && i_bytes > 0 )
284 p_sys->i_bitrate_avg = (int64_t)i_bytes *
286 (int64_t)MPGA_SAMPLE_RATE( header ) /
288 (int64_t)mpga_frame_samples( header );
289 msg_Dbg( p_demux, "xing frames&bytes value present (%db/s)", p_sys->i_bitrate_avg );
294 msg_Dbg( p_demux, "version=%d layer=%d channels=%d samplerate=%d",
295 MPGA_VERSION( header ) + 1,
296 MPGA_LAYER( header ) + 1,
297 MPGA_CHANNELS( header ),
298 MPGA_SAMPLE_RATE( header ) );
300 fmt.audio.i_channels = MPGA_CHANNELS( header );
301 fmt.audio.i_rate = MPGA_SAMPLE_RATE( header );
302 fmt.i_bitrate = p_sys->i_bitrate_avg;
303 sprintf( psz_description, "MPEG Audio Layer %d, version %d",
304 MPGA_LAYER ( header ) + 1, MPGA_VERSION ( header ) + 1 );
305 fmt.psz_description = strdup( psz_description );
308 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
309 if( fmt.psz_description ) free( fmt.psz_description );
315 /*****************************************************************************
316 * Demux: reads and demuxes data packets
317 *****************************************************************************
318 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
319 *****************************************************************************/
320 static int Demux( demux_t *p_demux )
322 demux_sys_t *p_sys = p_demux->p_sys;
328 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
330 msg_Warn( p_demux, "cannot peek" );
334 if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
336 /* we need to resynch */
337 vlc_bool_t b_ok = VLC_FALSE;
341 i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
344 msg_Warn( p_demux, "cannot peek" );
350 if( HeaderCheck( header = GetDWBE( p_peek ) ) )
361 msg_Warn( p_demux, "garbage=%d bytes", i_skip );
362 stream_Read( p_demux->s, NULL, i_skip );
366 if( ( p_frame = stream_Block( p_demux->s,
367 mpga_frame_size( header ) ) ) == NULL )
369 msg_Warn( p_demux, "cannot read data" );
372 p_frame->i_dts = p_frame->i_pts = p_sys->i_time;
375 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
377 es_out_Send( p_demux->out, p_sys->p_es, p_frame );
379 p_sys->i_time += (mtime_t)1000000 *
380 (mtime_t)mpga_frame_samples( header ) /
381 (mtime_t)MPGA_SAMPLE_RATE( header );
385 /*****************************************************************************
386 * Close: frees unused data
387 *****************************************************************************/
388 static void Close( vlc_object_t * p_this )
390 demux_t *p_demux = (demux_t*)p_this;
391 demux_sys_t *p_sys = p_demux->p_sys;
396 /*****************************************************************************
398 *****************************************************************************/
399 static int Control( demux_t *p_demux, int i_query, va_list args )
401 demux_sys_t *p_sys = p_demux->p_sys;
403 vlc_meta_t **pp_meta;
408 pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
409 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
413 return demux2_vaControlHelper( p_demux->s,
415 p_sys->i_bitrate_avg, 1, i_query,