2 /*****************************************************************************
3 * mpga.c : MPEG-I/II Audio input module for vlc
4 *****************************************************************************
5 * Copyright (C) 2001-2004 VideoLAN
8 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc/input.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close( vlc_object_t * );
42 set_description( _("MPEG-I/II audio demuxer" ) );
43 set_capability( "demux2", 100 );
44 set_callbacks( Open, Close );
45 add_shortcut( "mpga" );
46 add_shortcut( "mp3" );
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Demux ( demux_t * );
57 static int Control( demux_t *, int, va_list );
62 mtime_t i_time_offset;
64 int i_bitrate_avg; /* extracted from Xing header */
71 static int HeaderCheck( uint32_t h )
73 if( ((( h >> 21 )&0x07FF) != 0x07FF ) /* header sync */
74 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
75 || (((h >> 12)&0x0F) == 0x0F )
76 || (((h >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
77 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
78 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
85 static int mpga_sample_rate[2][4] =
87 { 44100, 48000, 32000, 0 },
88 { 22050, 24000, 16000, 0 }
91 static int mpga_bitrate[2][3][16] =
94 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
95 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
96 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}
99 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
100 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
101 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}
106 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
107 #define MPGA_LAYER( h ) ( 3 - (((h)>>17)&0x03) )
108 #define MPGA_SAMPLE_RATE(h) \
109 ( mpga_sample_rate[MPGA_VERSION(h)][((h)>>10)&0x03] / ( ((h>>20)&0x01) ? 1 : 2) )
110 #define MPGA_CHANNELS(h) ( (((h)>>6)&0x03) == 3 ? 1 : 2)
111 #define MPGA_BITRATE(h) mpga_bitrate[MPGA_VERSION(h)][MPGA_LAYER(h)][((h)>>12)&0x0f]
112 #define MPGA_PADDING(h) ( ((h)>>9)&0x01 )
113 #define MPGA_MODE(h) (((h)>> 6)&0x03)
115 static int mpga_frame_size( uint32_t h )
117 switch( MPGA_LAYER(h) )
120 return ( ( 12000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h) ) * 4;
122 return ( 144000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
124 return ( ( MPGA_VERSION(h) ? 72000 : 144000 ) * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
130 static int mpga_frame_samples( uint32_t h )
132 switch( MPGA_LAYER(h) )
139 return MPGA_VERSION(h) ? 576 : 1152;
145 /*****************************************************************************
146 * Open: initializes demux structures
147 *****************************************************************************/
148 static int Open( vlc_object_t * p_this )
150 demux_t *p_demux = (demux_t*)p_this;
152 vlc_bool_t b_forced = VLC_FALSE;
153 vlc_bool_t b_extention = VLC_FALSE;
160 if( !strncmp( p_demux->psz_demux, "mpga", 4 ) ||
161 !strncmp( p_demux->psz_demux, "mp3", 3 ) )
165 if( p_demux->psz_path )
167 int i_len = strlen( p_demux->psz_path );
168 if( i_len > 4 && !strcasecmp( &p_demux->psz_path[i_len - 4], ".mp3" ) )
170 b_extention = VLC_TRUE;
174 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
176 p_sys->i_time_offset = 0;
177 p_sys->i_bitrate_avg = 0;
180 /* skip/parse possible id3 header */
181 if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
183 p_sys->meta = (vlc_meta_t *)p_demux->p_private;
184 if( p_sys->meta != NULL )
187 msg_Dbg( p_demux, "Title : %s",
188 vlc_meta_GetValue( p_sys->meta,VLC_META_TITLE ) );
189 p_demux->p_private = NULL;
191 module_Unneed( p_demux, p_id3 );
194 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
196 msg_Err( p_demux, "cannot peek" );
200 if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
202 vlc_bool_t b_ok = VLC_FALSE;
205 if( !b_forced && !b_extention )
207 msg_Warn( p_demux, "mpga module discarded" );
211 i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
215 if( HeaderCheck( header = GetDWBE( p_peek ) ) )
223 if( !b_ok && !b_forced )
225 msg_Warn( p_demux, "mpga module discarded" );
230 p_demux->pf_demux = Demux;
231 p_demux->pf_control = Control;
233 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
235 if( HeaderCheck( header ) )
239 char psz_description[50];
241 p_sys->i_bitrate_avg = MPGA_BITRATE( header ) * 1000;
242 if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) >= 21 )
246 if( MPGA_VERSION( header) == 0 )
248 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
252 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
254 if( i_skip + 8 < i_xing &&
255 !strncmp( &p_xing[i_skip], "Xing", 4 ) )
257 unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
258 unsigned int i_bytes = 0, i_frames = 0;
260 p_xing += i_skip + 8;
261 i_xing -= i_skip + 8;
264 if( i_flags&0x01 && i_skip + 4 <= i_xing ) /* XING_FRAMES */
266 i_frames = GetDWBE( &p_xing[i_skip] );
269 if( i_flags&0x02 && i_skip + 4 <= i_xing ) /* XING_BYTES */
271 i_bytes = GetDWBE( &p_xing[i_skip] );
274 if( i_flags&0x04 ) /* XING_TOC */
279 // FIXME: doesn't return the right bitrage average, at least with some MP3's
280 if( i_flags&0x08 && i_skip + 4 <= i_xing ) /* XING_VBR */
282 p_sys->i_bitrate_avg = GetDWBE( &p_xing[i_skip] );
283 fprintf(stderr,"rate2 %d\n", p_sys->i_bitrate_avg);
284 msg_Dbg( p_input, "xing vbr value present (%d)", p_sys->i_bitrate_avg );
288 if( i_frames > 0 && i_bytes > 0 )
290 p_sys->i_bitrate_avg = (int64_t)i_bytes *
292 (int64_t)MPGA_SAMPLE_RATE( header ) /
294 (int64_t)mpga_frame_samples( header );
295 msg_Dbg( p_demux, "xing frames&bytes value present (%db/s)", p_sys->i_bitrate_avg );
300 msg_Dbg( p_demux, "version=%d layer=%d channels=%d samplerate=%d",
301 MPGA_VERSION( header ) + 1,
302 MPGA_LAYER( header ) + 1,
303 MPGA_CHANNELS( header ),
304 MPGA_SAMPLE_RATE( header ) );
306 fmt.audio.i_channels = MPGA_CHANNELS( header );
307 fmt.audio.i_rate = MPGA_SAMPLE_RATE( header );
308 fmt.i_bitrate = p_sys->i_bitrate_avg;
309 sprintf( psz_description, "MPEG Audio Layer %d, version %d",
310 MPGA_LAYER ( header ) + 1, MPGA_VERSION ( header ) + 1 );
311 fmt.psz_description = strdup( psz_description );
314 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
315 if( fmt.psz_description ) free( fmt.psz_description );
321 /*****************************************************************************
322 * Demux: reads and demuxes data packets
323 *****************************************************************************
324 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
325 *****************************************************************************/
326 static int Demux( demux_t *p_demux )
328 demux_sys_t *p_sys = p_demux->p_sys;
334 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
336 msg_Warn( p_demux, "cannot peek" );
340 if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
342 /* we need to resynch */
343 vlc_bool_t b_ok = VLC_FALSE;
347 i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
350 msg_Warn( p_demux, "cannot peek" );
356 if( HeaderCheck( header = GetDWBE( p_peek ) ) )
367 msg_Warn( p_demux, "garbage=%d bytes", i_skip );
368 stream_Read( p_demux->s, NULL, i_skip );
372 if( ( p_frame = stream_Block( p_demux->s,
373 mpga_frame_size( header ) ) ) == NULL )
375 msg_Warn( p_demux, "cannot read data" );
378 p_frame->i_dts = p_frame->i_pts = p_sys->i_time;
381 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
383 es_out_Send( p_demux->out, p_sys->p_es, p_frame );
385 p_sys->i_time += (mtime_t)1000000 *
386 (mtime_t)mpga_frame_samples( header ) /
387 (mtime_t)MPGA_SAMPLE_RATE( header );
391 /*****************************************************************************
392 * Close: frees unused data
393 *****************************************************************************/
394 static void Close( vlc_object_t * p_this )
396 demux_t *p_demux = (demux_t*)p_this;
397 demux_sys_t *p_sys = p_demux->p_sys;
402 /*****************************************************************************
404 *****************************************************************************/
405 static int Control( demux_t *p_demux, int i_query, va_list args )
407 demux_sys_t *p_sys = p_demux->p_sys;
409 vlc_meta_t **pp_meta;
415 pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
418 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
427 pi64 = (int64_t*)va_arg( args, int64_t * );
428 *pi64 = p_sys->i_time + p_sys->i_time_offset;
432 /* FIXME TODO: implement a high precision seek (with mp3 parsing)
433 * needed for multi-input */
435 i_ret = demux2_vaControlHelper( p_demux->s,
437 p_sys->i_bitrate_avg, 1, i_query,
439 if( !i_ret && p_sys->i_bitrate_avg > 0 &&
440 ( i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME ) )
442 int64_t i_time = I64C(8000000) * stream_Tell(p_demux->s) / p_sys->i_bitrate_avg;
444 /* fix time_offset */
446 p_sys->i_time_offset = i_time - p_sys->i_time;