1 /*****************************************************************************
2 * mpga.c : MPEG-I/II Audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mpga.c,v 1.2 2003/08/17 23:02:52 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>
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( "demux", 100 );
43 set_callbacks( Open, Close );
44 add_shortcut( "mpga" );
45 add_shortcut( "mp3" );
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Demux ( input_thread_t * );
63 int i_bitrate_avg; /* extracted from Xing header */
64 es_descriptor_t *p_es;
67 static int HeaderCheck( uint32_t h )
69 if( ((( h >> 20 )&0x0FFF) != 0x0FFF ) /* header sync */
70 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
71 || (((h >> 12)&0x0F) == 0x0F )
72 || (((h >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
73 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
74 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
81 static int mpga_sample_rate[2][4] =
83 { 44100, 48000, 32000, 0 },
84 { 22050, 24000, 16000, 0 }
87 static int mpga_bitrate[2][3][16] =
90 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
91 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
92 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}
95 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
96 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
97 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}
102 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
103 #define MPGA_LAYER( h ) ( 3 - (((h)>>17)&0x03) )
104 #define MPGA_SAMPLE_RATE(h) mpga_sample_rate[MPGA_VERSION(h)][((h)>>10)&0x03]
105 #define MPGA_CHANNELS(h) ( (((h)>>6)&0x03) == 3 ? 1 : 2)
106 #define MPGA_BITRATE(h) mpga_bitrate[MPGA_VERSION(h)][MPGA_LAYER(h)][((h)>>12)&0x0f]
107 #define MPGA_PADDING(h) ( ((h)>>9)&0x01 )
108 #define MPGA_MODE(h) (((h)>> 6)&0x03)
110 static int mpga_frame_size( uint32_t h )
112 switch( MPGA_LAYER(h) )
115 return ( ( 12000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h) ) * 4;
117 return ( 144000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
119 return ( ( MPGA_VERSION(h) ? 72000 : 144000 ) * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
125 static int mpga_frame_samples( uint32_t h )
127 switch( MPGA_LAYER(h) )
134 return MPGA_VERSION(h) ? 576 : 1152;
141 static int CheckPS( input_thread_t *p_input )
145 int i_size = input_Peek( p_input, &p_peek, 8196 );
149 if( ( p_peek[0] == 0 ) && ( p_peek[1] == 0 ) &&
150 ( p_peek[2] == 1 ) && ( p_peek[3] >= 0xb9 ) &&
163 /*****************************************************************************
164 * Open: initializes demux structures
165 *****************************************************************************/
166 static int Open( vlc_object_t * p_this )
168 input_thread_t *p_input = (input_thread_t *)p_this;
170 vlc_bool_t b_forced = VLC_FALSE;
171 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;
241 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
243 p_sys->i_bitrate_avg = 0;
245 if( ( p_sys->s = stream_OpenInput( p_input ) ) == NULL )
247 msg_Err( p_input, "cannot create stream" );
251 if( HeaderCheck( header ) )
256 input_info_category_t * p_cat;
257 static char* mpga_mode[4] =
259 "stereo", "joint stereo", "dual channel", "mono"
262 p_sys->i_bitrate_avg = MPGA_BITRATE( header ) * 1000;
263 if( ( i_xing = stream_Peek( p_sys->s, &p_xing, 1024 ) ) >= 21 )
267 if( MPGA_VERSION( header) == 0 )
269 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
273 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
275 if( i_skip + 8 < i_xing &&
276 !strncmp( &p_xing[i_skip], "Xing", 4 ) )
278 unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
279 unsigned int i_bytes = 0, i_frames = 0;
281 p_xing += i_skip + 8;
282 i_xing -= i_skip + 8;
285 if( i_flags&0x01 && i_skip + 4 <= i_xing ) /* XING_FRAMES */
287 i_frames = GetDWBE( &p_xing[i_skip] );
290 if( i_flags&0x02 && i_skip + 4 <= i_xing ) /* XING_BYTES */
292 i_bytes = GetDWBE( &p_xing[i_skip] );
295 if( i_flags&0x04 ) /* XING_TOC */
299 if( i_flags&0x08 && i_skip + 4 <= i_xing ) /* XING_VBR */
301 p_sys->i_bitrate_avg = GetDWBE( &p_xing[i_skip] );
302 msg_Dbg( p_input, "xing vbr value present (%d)", p_sys->i_bitrate_avg );
304 else if( i_frames > 0 && i_bytes > 0 )
306 p_sys->i_bitrate_avg = (int64_t)i_bytes *
308 (int64_t)MPGA_SAMPLE_RATE( header ) /
310 (int64_t)mpga_frame_samples( header );
311 msg_Dbg( p_input, "xing frames&bytes value present (%db/s)", p_sys->i_bitrate_avg );
316 msg_Dbg( p_input, "version=%d layer=%d channels=%d samplerate=%d",
317 MPGA_VERSION( header) + 1,
318 MPGA_LAYER( header ) + 1,
319 MPGA_CHANNELS( header ),
320 MPGA_SAMPLE_RATE( header ) );
322 vlc_mutex_lock( &p_input->stream.stream_lock );
324 p_cat = input_InfoCategory( p_input, _("MPEG") );
325 input_AddInfo( p_cat, _("Input Type"), "Audio MPEG-%d",
326 MPGA_VERSION( header) + 1 );
327 input_AddInfo( p_cat, _("Layer"), "%d",
328 MPGA_LAYER( header ) + 1 );
329 input_AddInfo( p_cat, _("Mode"),
330 mpga_mode[MPGA_MODE( header )] );
331 input_AddInfo( p_cat, _("Sample Rate"), "%dHz",
332 MPGA_SAMPLE_RATE( header ) );
333 input_AddInfo( p_cat, _("Average Bitrate"), "%dKb/s",
334 p_sys->i_bitrate_avg / 1000 );
335 vlc_mutex_unlock( &p_input->stream.stream_lock );
338 vlc_mutex_lock( &p_input->stream.stream_lock );
339 if( input_InitStream( p_input, 0 ) == -1)
341 vlc_mutex_unlock( &p_input->stream.stream_lock );
342 msg_Err( p_input, "cannot init stream" );
345 if( input_AddProgram( p_input, 0, 0) == NULL )
347 vlc_mutex_unlock( &p_input->stream.stream_lock );
348 msg_Err( p_input, "cannot add program" );
351 p_input->stream.pp_programs[0]->b_is_ok = 0;
352 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
354 p_input->stream.i_mux_rate = p_sys->i_bitrate_avg / 8 / 50;
356 p_sys->p_es = input_AddES( p_input,
357 p_input->stream.p_selected_program,
358 1 , AUDIO_ES, NULL, 0 );
360 p_sys->p_es->i_stream_id = 1;
361 p_sys->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
362 input_SelectES( p_input, p_sys->p_es );
364 p_input->stream.p_selected_program->b_is_ok = 1;
365 vlc_mutex_unlock( &p_input->stream.stream_lock );
372 stream_Release( p_sys->s );
379 /*****************************************************************************
380 * Demux: reads and demuxes data packets
381 *****************************************************************************
382 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
383 *****************************************************************************/
384 static int Demux( input_thread_t * p_input )
386 demux_sys_t *p_sys = p_input->p_demux_data;
392 if( stream_Peek( p_sys->s, &p_peek, 4 ) < 4 )
394 msg_Warn( p_input, "cannot peek" );
398 if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
400 /* we need to resynch */
401 vlc_bool_t b_ok = VLC_FALSE;
405 i_peek = stream_Peek( p_sys->s, &p_peek, 8096 );
408 msg_Warn( p_input, "cannot peek" );
414 if( HeaderCheck( header = GetDWBE( p_peek ) ) )
425 msg_Warn( p_input, "garbage=%d bytes", i_skip );
426 stream_Read( p_sys->s, NULL, i_skip );
430 input_ClockManageRef( p_input,
431 p_input->stream.p_selected_program,
432 p_sys->i_time * 9 / 100 );
434 if( ( p_pes = stream_PesPacket( p_sys->s, mpga_frame_size( header ) ) ) == NULL )
436 msg_Warn( p_input, "cannot read data" );
441 p_pes->i_pts = input_ClockGetTS( p_input,
442 p_input->stream.p_selected_program,
443 p_sys->i_time * 9 / 100 );
445 if( !p_sys->p_es->p_decoder_fifo )
447 msg_Err( p_input, "no audio decoder" );
448 input_DeletePES( p_input->p_method_data, p_pes );
452 input_DecodePES( p_sys->p_es->p_decoder_fifo, p_pes );
453 p_sys->i_time += (mtime_t)1000000 *
454 (mtime_t)mpga_frame_samples( header ) /
455 (mtime_t)MPGA_SAMPLE_RATE( header );
459 /*****************************************************************************
460 * Close: frees unused data
461 *****************************************************************************/
462 static void Close( vlc_object_t * p_this )
464 input_thread_t *p_input = (input_thread_t*)p_this;
465 demux_sys_t *p_sys = p_input->p_demux_data;
469 stream_Release( p_sys->s );