1 /*****************************************************************************
2 * mpga.c : MPEG-I/II Audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mpga.c,v 1.1 2003/08/01 00:37:06 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;
68 static inline uint32_t GetDWBE( uint8_t *p )
70 return( ( p[0] << 24 )|( p[1] << 16 )|( p[2] << 8 )|( p[3] ) );
73 static int HeaderCheck( uint32_t h )
75 if( ((( h >> 20 )&0x0FFF) != 0x0FFF ) /* header sync */
76 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
77 || (((h >> 12)&0x0F) == 0x0F )
78 || (((h >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
79 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
80 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
87 static int mpga_sample_rate[2][4] =
89 { 44100, 48000, 32000, 0 },
90 { 22050, 24000, 16000, 0 }
93 static int mpga_bitrate[2][3][16] =
96 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
97 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
98 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}
101 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
102 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
103 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}
108 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
109 #define MPGA_LAYER( h ) ( 3 - (((h)>>17)&0x03) )
110 #define MPGA_SAMPLE_RATE(h) mpga_sample_rate[MPGA_VERSION(h)][((h)>>10)&0x03]
111 #define MPGA_CHANNELS(h) ( (((h)>>6)&0x03) == 3 ? 1 : 2)
112 #define MPGA_BITRATE(h) mpga_bitrate[MPGA_VERSION(h)][MPGA_LAYER(h)][((h)>>12)&0x0f]
113 #define MPGA_PADDING(h) ( ((h)>>9)&0x01 )
114 #define MPGA_MODE(h) (((h)>> 6)&0x03)
116 static int mpga_frame_size( uint32_t h )
118 switch( MPGA_LAYER(h) )
121 return ( ( 12000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h) ) * 4;
123 return ( 144000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
125 return ( ( MPGA_VERSION(h) ? 72000 : 144000 ) * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
131 static int mpga_frame_samples( uint32_t h )
133 switch( MPGA_LAYER(h) )
140 return MPGA_VERSION(h) ? 576 : 1152;
147 static int CheckPS( input_thread_t *p_input )
151 int i_size = input_Peek( p_input, &p_peek, 8196 );
155 if( ( p_peek[0] == 0 ) && ( p_peek[1] == 0 ) &&
156 ( p_peek[2] == 1 ) && ( p_peek[3] >= 0xb9 ) &&
169 /*****************************************************************************
170 * Open: initializes demux structures
171 *****************************************************************************/
172 static int Open( vlc_object_t * p_this )
174 input_thread_t *p_input = (input_thread_t *)p_this;
176 vlc_bool_t b_forced = VLC_FALSE;
177 vlc_bool_t b_extention = VLC_FALSE;
186 if( p_input->psz_demux &&
187 ( !strncmp( p_input->psz_demux, "mpga", 4 ) ||
188 !strncmp( p_input->psz_demux, "mp3", 3 ) ) )
192 if( p_input->psz_name )
194 int i_len = strlen( p_input->psz_name );
196 if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".mp3" ) )
198 b_extention = VLC_TRUE;
202 /* skip possible id3 header */
203 p_id3 = module_Need( p_input, "id3", NULL );
206 module_Unneed( p_input, p_id3 );
209 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
211 msg_Err( p_input, "cannot peek" );
215 if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
217 vlc_bool_t b_ok = VLC_FALSE;
220 if( !b_forced && !b_extention )
222 msg_Warn( p_input, "mpga module discarded" );
226 i_peek = input_Peek( p_input, &p_peek, 8096 );
230 if( HeaderCheck( header = GetDWBE( p_peek ) ) )
238 if( !b_ok && !b_forced )
240 msg_Warn( p_input, "mpga module discarded" );
245 p_input->pf_demux = Demux;
247 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
249 p_sys->i_bitrate_avg = 0;
251 if( ( p_sys->s = stream_OpenInput( p_input ) ) == NULL )
253 msg_Err( p_input, "cannot create stream" );
257 if( HeaderCheck( header ) )
262 input_info_category_t * p_cat;
263 static char* mpga_mode[4] =
265 "stereo", "joint stereo", "dual channel", "mono"
268 p_sys->i_bitrate_avg = MPGA_BITRATE( header ) * 1000;
269 if( ( i_xing = stream_Peek( p_sys->s, &p_xing, 1024 ) ) >= 21 )
273 if( MPGA_VERSION( header) == 0 )
275 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
279 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
281 if( i_skip + 8 < i_xing &&
282 !strncmp( &p_xing[i_skip], "Xing", 4 ) )
284 unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
285 unsigned int i_bytes = 0, i_frames = 0;
287 p_xing += i_skip + 8;
288 i_xing -= i_skip + 8;
291 if( i_flags&0x01 && i_skip + 4 <= i_xing ) /* XING_FRAMES */
293 i_frames = GetDWBE( &p_xing[i_skip] );
296 if( i_flags&0x02 && i_skip + 4 <= i_xing ) /* XING_BYTES */
298 i_bytes = GetDWBE( &p_xing[i_skip] );
301 if( i_flags&0x04 ) /* XING_TOC */
305 if( i_flags&0x08 && i_skip + 4 <= i_xing ) /* XING_VBR */
307 p_sys->i_bitrate_avg = GetDWBE( &p_xing[i_skip] );
308 msg_Dbg( p_input, "xing vbr value present (%d)", p_sys->i_bitrate_avg );
310 else if( i_frames > 0 && i_bytes > 0 )
312 p_sys->i_bitrate_avg = (int64_t)i_bytes *
314 (int64_t)MPGA_SAMPLE_RATE( header ) /
316 (int64_t)mpga_frame_samples( header );
317 msg_Dbg( p_input, "xing frames&bytes value present (%db/s)", p_sys->i_bitrate_avg );
322 msg_Dbg( p_input, "version=%d layer=%d channels=%d samplerate=%d",
323 MPGA_VERSION( header) + 1,
324 MPGA_LAYER( header ) + 1,
325 MPGA_CHANNELS( header ),
326 MPGA_SAMPLE_RATE( header ) );
328 vlc_mutex_lock( &p_input->stream.stream_lock );
330 p_cat = input_InfoCategory( p_input, _("MPEG") );
331 input_AddInfo( p_cat, _("Input Type"), "Audio MPEG-%d",
332 MPGA_VERSION( header) + 1 );
333 input_AddInfo( p_cat, _("Layer"), "%d",
334 MPGA_LAYER( header ) + 1 );
335 input_AddInfo( p_cat, _("Mode"),
336 mpga_mode[MPGA_MODE( header )] );
337 input_AddInfo( p_cat, _("Sample Rate"), "%dHz",
338 MPGA_SAMPLE_RATE( header ) );
339 input_AddInfo( p_cat, _("Average Bitrate"), "%dKb/s",
340 p_sys->i_bitrate_avg / 1000 );
341 vlc_mutex_unlock( &p_input->stream.stream_lock );
344 vlc_mutex_lock( &p_input->stream.stream_lock );
345 if( input_InitStream( p_input, 0 ) == -1)
347 vlc_mutex_unlock( &p_input->stream.stream_lock );
348 msg_Err( p_input, "cannot init stream" );
351 if( input_AddProgram( p_input, 0, 0) == NULL )
353 vlc_mutex_unlock( &p_input->stream.stream_lock );
354 msg_Err( p_input, "cannot add program" );
357 p_input->stream.pp_programs[0]->b_is_ok = 0;
358 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
360 p_input->stream.i_mux_rate = p_sys->i_bitrate_avg / 8 / 50;
362 p_sys->p_es = input_AddES( p_input,
363 p_input->stream.p_selected_program,
364 1 , AUDIO_ES, NULL, 0 );
366 p_sys->p_es->i_stream_id = 1;
367 p_sys->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
368 input_SelectES( p_input, p_sys->p_es );
370 p_input->stream.p_selected_program->b_is_ok = 1;
371 vlc_mutex_unlock( &p_input->stream.stream_lock );
378 stream_Release( p_sys->s );
385 /*****************************************************************************
386 * Demux: reads and demuxes data packets
387 *****************************************************************************
388 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
389 *****************************************************************************/
390 static int Demux( input_thread_t * p_input )
392 demux_sys_t *p_sys = p_input->p_demux_data;
398 if( stream_Peek( p_sys->s, &p_peek, 4 ) < 4 )
400 msg_Warn( p_input, "cannot peek" );
404 if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
406 /* we need to resynch */
407 vlc_bool_t b_ok = VLC_FALSE;
411 i_peek = stream_Peek( p_sys->s, &p_peek, 8096 );
414 msg_Warn( p_input, "cannot peek" );
420 if( HeaderCheck( header = GetDWBE( p_peek ) ) )
431 msg_Warn( p_input, "garbage=%d bytes", i_skip );
432 stream_Read( p_sys->s, NULL, i_skip );
436 input_ClockManageRef( p_input,
437 p_input->stream.p_selected_program,
438 p_sys->i_time * 9 / 100 );
440 if( ( p_pes = stream_PesPacket( p_sys->s, mpga_frame_size( header ) ) ) == NULL )
442 msg_Warn( p_input, "cannot read data" );
447 p_pes->i_pts = input_ClockGetTS( p_input,
448 p_input->stream.p_selected_program,
449 p_sys->i_time * 9 / 100 );
451 if( !p_sys->p_es->p_decoder_fifo )
453 msg_Err( p_input, "no audio decoder" );
454 input_DeletePES( p_input->p_method_data, p_pes );
458 input_DecodePES( p_sys->p_es->p_decoder_fifo, p_pes );
459 p_sys->i_time += (mtime_t)1000000 *
460 (mtime_t)mpga_frame_samples( header ) /
461 (mtime_t)MPGA_SAMPLE_RATE( header );
465 /*****************************************************************************
466 * Close: frees unused data
467 *****************************************************************************/
468 static void Close( vlc_object_t * p_this )
470 input_thread_t *p_input = (input_thread_t*)p_this;
471 demux_sys_t *p_sys = p_input->p_demux_data;
475 stream_Release( p_sys->s );