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 );
61 mtime_t i_time_offset;
63 int i_bitrate_avg; /* extracted from Xing header */
70 static int HeaderCheck( uint32_t h )
72 if( ((( h >> 21 )&0x07FF) != 0x07FF ) /* header sync */
73 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
74 || (((h >> 12)&0x0F) == 0x0F )
75 || (((h >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
76 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
77 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
84 static int mpga_sample_rate[2][4] =
86 { 44100, 48000, 32000, 0 },
87 { 22050, 24000, 16000, 0 }
90 static int mpga_bitrate[2][3][16] =
93 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
94 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
95 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}
98 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
99 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
100 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}
105 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
106 #define MPGA_LAYER( h ) ( 3 - (((h)>>17)&0x03) )
107 #define MPGA_SAMPLE_RATE(h) \
108 ( mpga_sample_rate[MPGA_VERSION(h)][((h)>>10)&0x03] / ( ((h>>20)&0x01) ? 1 : 2) )
109 #define MPGA_CHANNELS(h) ( (((h)>>6)&0x03) == 3 ? 1 : 2)
110 #define MPGA_BITRATE(h) mpga_bitrate[MPGA_VERSION(h)][MPGA_LAYER(h)][((h)>>12)&0x0f]
111 #define MPGA_PADDING(h) ( ((h)>>9)&0x01 )
112 #define MPGA_MODE(h) (((h)>> 6)&0x03)
114 static int mpga_frame_size( uint32_t h )
116 switch( MPGA_LAYER(h) )
119 return ( ( 12000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h) ) * 4;
121 return ( 144000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
123 return ( ( MPGA_VERSION(h) ? 72000 : 144000 ) * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
129 static int mpga_frame_samples( uint32_t h )
131 switch( MPGA_LAYER(h) )
138 return MPGA_VERSION(h) ? 576 : 1152;
144 /*****************************************************************************
145 * Open: initializes demux structures
146 *****************************************************************************/
147 static int Open( vlc_object_t * p_this )
149 demux_t *p_demux = (demux_t*)p_this;
151 vlc_bool_t b_forced = VLC_FALSE;
152 vlc_bool_t b_extention = VLC_FALSE;
159 if( !strncmp( p_demux->psz_demux, "mpga", 4 ) ||
160 !strncmp( p_demux->psz_demux, "mp3", 3 ) )
164 if( p_demux->psz_path )
166 int i_len = strlen( p_demux->psz_path );
167 if( i_len > 4 && !strcasecmp( &p_demux->psz_path[i_len - 4], ".mp3" ) )
169 b_extention = VLC_TRUE;
173 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
175 p_sys->i_time_offset = 0;
176 p_sys->i_bitrate_avg = 0;
179 /* skip/parse possible id3 header */
180 if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
182 p_sys->meta = (vlc_meta_t *)p_demux->p_private;
183 if( p_sys->meta != NULL )
186 msg_Dbg( p_demux, "Title : %s",
187 vlc_meta_GetValue( p_sys->meta,VLC_META_TITLE ) );
188 p_demux->p_private = NULL;
190 module_Unneed( p_demux, p_id3 );
193 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
195 msg_Err( p_demux, "cannot peek" );
199 if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
201 vlc_bool_t b_ok = VLC_FALSE;
204 if( !b_forced && !b_extention )
206 msg_Warn( p_demux, "mpga module discarded" );
210 i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
214 if( HeaderCheck( header = GetDWBE( p_peek ) ) )
222 if( !b_ok && !b_forced )
224 msg_Warn( p_demux, "mpga module discarded" );
229 p_demux->pf_demux = Demux;
230 p_demux->pf_control = Control;
232 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
234 if( HeaderCheck( header ) )
238 char psz_description[50];
240 p_sys->i_bitrate_avg = MPGA_BITRATE( header ) * 1000;
241 if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) >= 21 )
245 if( MPGA_VERSION( header) == 0 )
247 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
251 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
253 if( i_skip + 8 < i_xing &&
254 !strncmp( &p_xing[i_skip], "Xing", 4 ) )
256 unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
257 unsigned int i_bytes = 0, i_frames = 0;
259 p_xing += i_skip + 8;
260 i_xing -= i_skip + 8;
263 if( i_flags&0x01 && i_skip + 4 <= i_xing ) /* XING_FRAMES */
265 i_frames = GetDWBE( &p_xing[i_skip] );
268 if( i_flags&0x02 && i_skip + 4 <= i_xing ) /* XING_BYTES */
270 i_bytes = GetDWBE( &p_xing[i_skip] );
273 if( i_flags&0x04 ) /* XING_TOC */
278 // FIXME: doesn't return the right bitrage average, at least with some MP3's
279 if( i_flags&0x08 && i_skip + 4 <= i_xing ) /* XING_VBR */
281 p_sys->i_bitrate_avg = GetDWBE( &p_xing[i_skip] );
282 fprintf(stderr,"rate2 %d\n", p_sys->i_bitrate_avg);
283 msg_Dbg( p_input, "xing vbr value present (%d)", p_sys->i_bitrate_avg );
287 if( i_frames > 0 && i_bytes > 0 )
289 p_sys->i_bitrate_avg = (int64_t)i_bytes *
291 (int64_t)MPGA_SAMPLE_RATE( header ) /
293 (int64_t)mpga_frame_samples( header );
294 msg_Dbg( p_demux, "xing frames&bytes value present (%db/s)", p_sys->i_bitrate_avg );
299 msg_Dbg( p_demux, "version=%d layer=%d channels=%d samplerate=%d",
300 MPGA_VERSION( header ) + 1,
301 MPGA_LAYER( header ) + 1,
302 MPGA_CHANNELS( header ),
303 MPGA_SAMPLE_RATE( header ) );
305 fmt.audio.i_channels = MPGA_CHANNELS( header );
306 fmt.audio.i_rate = MPGA_SAMPLE_RATE( header );
307 fmt.i_bitrate = p_sys->i_bitrate_avg;
308 sprintf( psz_description, "MPEG Audio Layer %d, version %d",
309 MPGA_LAYER ( header ) + 1, MPGA_VERSION ( header ) + 1 );
310 fmt.psz_description = strdup( psz_description );
313 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
314 if( fmt.psz_description ) free( fmt.psz_description );
320 /*****************************************************************************
321 * Demux: reads and demuxes data packets
322 *****************************************************************************
323 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
324 *****************************************************************************/
325 static int Demux( demux_t *p_demux )
327 demux_sys_t *p_sys = p_demux->p_sys;
333 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
335 msg_Warn( p_demux, "cannot peek" );
339 if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
341 /* we need to resynch */
342 vlc_bool_t b_ok = VLC_FALSE;
346 i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
349 msg_Warn( p_demux, "cannot peek" );
355 if( HeaderCheck( header = GetDWBE( p_peek ) ) )
366 msg_Warn( p_demux, "garbage=%d bytes", i_skip );
367 stream_Read( p_demux->s, NULL, i_skip );
371 if( ( p_frame = stream_Block( p_demux->s,
372 mpga_frame_size( header ) ) ) == NULL )
374 msg_Warn( p_demux, "cannot read data" );
377 p_frame->i_dts = p_frame->i_pts = p_sys->i_time;
380 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
382 es_out_Send( p_demux->out, p_sys->p_es, p_frame );
384 p_sys->i_time += (mtime_t)1000000 *
385 (mtime_t)mpga_frame_samples( header ) /
386 (mtime_t)MPGA_SAMPLE_RATE( header );
390 /*****************************************************************************
391 * Close: frees unused data
392 *****************************************************************************/
393 static void Close( vlc_object_t * p_this )
395 demux_t *p_demux = (demux_t*)p_this;
396 demux_sys_t *p_sys = p_demux->p_sys;
401 /*****************************************************************************
403 *****************************************************************************/
404 static int Control( demux_t *p_demux, int i_query, va_list args )
406 demux_sys_t *p_sys = p_demux->p_sys;
408 vlc_meta_t **pp_meta;
414 pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
415 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
419 pi64 = (int64_t*)va_arg( args, int64_t * );
420 *pi64 = p_sys->i_time + p_sys->i_time_offset;
424 /* FIXME TODO: implement a high precision seek (with mp3 parsing)
425 * needed for multi-input */
427 i_ret = demux2_vaControlHelper( p_demux->s,
429 p_sys->i_bitrate_avg, 1, i_query,
431 if( !i_ret && p_sys->i_bitrate_avg > 0 &&
432 ( i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME ) )
434 int64_t i_time = I64C(8000000) * stream_Tell(p_demux->s) / p_sys->i_bitrate_avg;
436 /* fix time_offset */
438 p_sys->i_time_offset = i_time - p_sys->i_time;