1 /*****************************************************************************
2 * mpga.c : MPEG-I/II Audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mpga.c,v 1.13 2003/12/27 14:47:10 asmax 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( _("MPEG-I/II audio demuxer" ) );
40 set_capability( "demux", 100 );
41 set_callbacks( Open, Close );
42 add_shortcut( "mpga" );
43 add_shortcut( "mp3" );
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Demux ( input_thread_t * );
59 int i_bitrate_avg; /* extracted from Xing header */
64 static int HeaderCheck( uint32_t h )
66 if( ((( h >> 21 )&0x07FF) != 0x07FF ) /* header sync */
67 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
68 || (((h >> 12)&0x0F) == 0x0F )
69 || (((h >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
70 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
71 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
78 static int mpga_sample_rate[2][4] =
80 { 44100, 48000, 32000, 0 },
81 { 22050, 24000, 16000, 0 }
84 static int mpga_bitrate[2][3][16] =
87 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
88 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
89 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}
92 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
93 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
94 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}
99 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
100 #define MPGA_LAYER( h ) ( 3 - (((h)>>17)&0x03) )
101 #define MPGA_SAMPLE_RATE(h) \
102 ( mpga_sample_rate[MPGA_VERSION(h)][((h)>>10)&0x03] / ( ((h>>20)&0x01) ? 1 : 2) )
103 #define MPGA_CHANNELS(h) ( (((h)>>6)&0x03) == 3 ? 1 : 2)
104 #define MPGA_BITRATE(h) mpga_bitrate[MPGA_VERSION(h)][MPGA_LAYER(h)][((h)>>12)&0x0f]
105 #define MPGA_PADDING(h) ( ((h)>>9)&0x01 )
106 #define MPGA_MODE(h) (((h)>> 6)&0x03)
108 static int mpga_frame_size( uint32_t h )
110 switch( MPGA_LAYER(h) )
113 return ( ( 12000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h) ) * 4;
115 return ( 144000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
117 return ( ( MPGA_VERSION(h) ? 72000 : 144000 ) * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
123 static int mpga_frame_samples( uint32_t h )
125 switch( MPGA_LAYER(h) )
132 return MPGA_VERSION(h) ? 576 : 1152;
139 static int CheckPS( input_thread_t *p_input )
143 int i_size = input_Peek( p_input, &p_peek, 8196 );
147 if( ( p_peek[0] == 0 ) && ( p_peek[1] == 0 ) &&
148 ( p_peek[2] == 1 ) && ( p_peek[3] >= 0xb9 ) &&
161 /*****************************************************************************
162 * Open: initializes demux structures
163 *****************************************************************************/
164 static int Open( vlc_object_t * p_this )
166 input_thread_t *p_input = (input_thread_t *)p_this;
168 vlc_bool_t b_forced = VLC_FALSE;
169 vlc_bool_t b_extention = VLC_FALSE;
180 if( p_input->psz_demux &&
181 ( !strncmp( p_input->psz_demux, "mpga", 4 ) ||
182 !strncmp( p_input->psz_demux, "mp3", 3 ) ) )
186 if( p_input->psz_name )
188 int i_len = strlen( p_input->psz_name );
190 if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".mp3" ) )
192 b_extention = VLC_TRUE;
196 /* skip possible id3 header */
197 p_id3 = module_Need( p_input, "id3", NULL );
200 module_Unneed( p_input, p_id3 );
203 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
205 msg_Err( p_input, "cannot peek" );
209 if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
211 vlc_bool_t b_ok = VLC_FALSE;
214 if( !b_forced && !b_extention )
216 msg_Warn( p_input, "mpga module discarded" );
220 i_peek = input_Peek( p_input, &p_peek, 8096 );
224 if( HeaderCheck( header = GetDWBE( p_peek ) ) )
232 if( !b_ok && !b_forced )
234 msg_Warn( p_input, "mpga module discarded" );
239 p_input->pf_demux = Demux;
240 p_input->pf_demux_control = demux_vaControlDefault;
242 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
244 p_sys->i_bitrate_avg = 0;
246 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
248 if( HeaderCheck( header ) )
252 char psz_description[50];
254 p_sys->i_bitrate_avg = MPGA_BITRATE( header ) * 1000;
255 fprintf(stderr,"rate1 %d\n", p_sys->i_bitrate_avg);
256 if( ( i_xing = stream_Peek( p_input->s, &p_xing, 1024 ) ) >= 21 )
260 if( MPGA_VERSION( header) == 0 )
262 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
266 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
268 if( i_skip + 8 < i_xing &&
269 !strncmp( &p_xing[i_skip], "Xing", 4 ) )
271 unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
272 unsigned int i_bytes = 0, i_frames = 0;
274 p_xing += i_skip + 8;
275 i_xing -= i_skip + 8;
278 if( i_flags&0x01 && i_skip + 4 <= i_xing ) /* XING_FRAMES */
280 i_frames = GetDWBE( &p_xing[i_skip] );
283 if( i_flags&0x02 && i_skip + 4 <= i_xing ) /* XING_BYTES */
285 i_bytes = GetDWBE( &p_xing[i_skip] );
288 if( i_flags&0x04 ) /* XING_TOC */
293 // FIXME: doesn't return the right bitrage average, at least with some MP3's
294 if( i_flags&0x08 && i_skip + 4 <= i_xing ) /* XING_VBR */
296 p_sys->i_bitrate_avg = GetDWBE( &p_xing[i_skip] );
297 fprintf(stderr,"rate2 %d\n", p_sys->i_bitrate_avg);
298 msg_Dbg( p_input, "xing vbr value present (%d)", p_sys->i_bitrate_avg );
302 if( i_frames > 0 && i_bytes > 0 )
304 p_sys->i_bitrate_avg = (int64_t)i_bytes *
306 (int64_t)MPGA_SAMPLE_RATE( header ) /
308 (int64_t)mpga_frame_samples( header );
309 msg_Dbg( p_input, "xing frames&bytes value present (%db/s)", p_sys->i_bitrate_avg );
314 msg_Dbg( p_input, "version=%d layer=%d channels=%d samplerate=%d",
315 MPGA_VERSION( header ) + 1,
316 MPGA_LAYER( header ) + 1,
317 MPGA_CHANNELS( header ),
318 MPGA_SAMPLE_RATE( header ) );
320 fmt.audio.i_channels = MPGA_CHANNELS( header );
321 fmt.audio.i_rate = MPGA_SAMPLE_RATE( header );
322 fmt.i_bitrate = p_sys->i_bitrate_avg;
323 sprintf( psz_description, "MPEG Audio Layer %d, version %d",
324 MPGA_LAYER ( header ) + 1, MPGA_VERSION ( header ) + 1 );
325 fmt.psz_description = strdup( psz_description );
328 vlc_mutex_lock( &p_input->stream.stream_lock );
329 if( input_InitStream( p_input, 0 ) == -1)
331 vlc_mutex_unlock( &p_input->stream.stream_lock );
332 msg_Err( p_input, "cannot init stream" );
335 p_input->stream.i_mux_rate = p_sys->i_bitrate_avg / 8 / 50;
336 vlc_mutex_unlock( &p_input->stream.stream_lock );
338 p_sys->p_es = es_out_Add( p_input->p_es_out, &fmt );
347 /*****************************************************************************
348 * Demux: reads and demuxes data packets
349 *****************************************************************************
350 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
351 *****************************************************************************/
352 static int Demux( input_thread_t * p_input )
354 demux_sys_t *p_sys = p_input->p_demux_data;
360 if( stream_Peek( p_input->s, &p_peek, 4 ) < 4 )
362 msg_Warn( p_input, "cannot peek" );
366 if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
368 /* we need to resynch */
369 vlc_bool_t b_ok = VLC_FALSE;
373 i_peek = stream_Peek( p_input->s, &p_peek, 8096 );
376 msg_Warn( p_input, "cannot peek" );
382 if( HeaderCheck( header = GetDWBE( p_peek ) ) )
393 msg_Warn( p_input, "garbage=%d bytes", i_skip );
394 stream_Read( p_input->s, NULL, i_skip );
398 input_ClockManageRef( p_input,
399 p_input->stream.p_selected_program,
400 p_sys->i_time * 9 / 100 );
402 if( ( p_frame = stream_Block( p_input->s, mpga_frame_size( header ) ) ) == NULL )
404 msg_Warn( p_input, "cannot read data" );
410 input_ClockGetTS( p_input,
411 p_input->stream.p_selected_program,
412 p_sys->i_time * 9 / 100 );
414 es_out_Send( p_input->p_es_out, p_sys->p_es, p_frame );
416 p_sys->i_time += (mtime_t)1000000 *
417 (mtime_t)mpga_frame_samples( header ) /
418 (mtime_t)MPGA_SAMPLE_RATE( header );
422 /*****************************************************************************
423 * Close: frees unused data
424 *****************************************************************************/
425 static void Close( vlc_object_t * p_this )
427 input_thread_t *p_input = (input_thread_t*)p_this;
428 demux_sys_t *p_sys = p_input->p_demux_data;