1 /*****************************************************************************
2 * mpga.c : MPEG-I/II Audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <vlc_demux.h>
31 #include <vlc_codec.h>
32 #include <vlc_input.h>
34 #define MPGA_PACKET_SIZE 1024
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
43 set_category( CAT_INPUT );
44 set_subcategory( SUBCAT_INPUT_DEMUX );
45 set_description( _("MPEG audio / MP3 demuxer" ) );
46 set_capability( "demux2", 100 );
47 set_callbacks( Open, Close );
48 add_shortcut( "mpga" );
49 add_shortcut( "mp3" );
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Demux ( demux_t * );
56 static int Control( demux_t *, int, va_list );
64 decoder_t *p_packetizer;
67 mtime_t i_time_offset;
68 int i_bitrate_avg; /* extracted from Xing header */
70 vlc_bool_t b_initial_sync_failed;
74 int i_xing_bitrate_avg;
75 int i_xing_frame_samples;
76 block_t *p_block_in, *p_block_out;
79 input_attachment_t **attachments;
82 static int HeaderCheck( uint32_t h )
84 if( ((( h >> 21 )&0x07FF) != 0x07FF ) /* header sync */
85 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
86 || (((h >> 12)&0x0F) == 0x0F )
87 || (((h >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
88 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
89 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
96 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
97 #define MPGA_LAYER( h ) ( 3 - (((h)>>17)&0x03) )
98 #define MPGA_MODE(h) (((h)>> 6)&0x03)
100 static int mpga_frame_samples( uint32_t h )
102 switch( MPGA_LAYER(h) )
109 return MPGA_VERSION(h) ? 576 : 1152;
116 /*****************************************************************************
117 * Open: initializes demux structures
118 *****************************************************************************/
119 static int Open( vlc_object_t * p_this )
121 demux_t *p_demux = (demux_t*)p_this;
123 vlc_bool_t b_forced = VLC_FALSE;
126 const uint8_t *p_peek;
128 block_t *p_block_in, *p_block_out;
130 if( demux2_IsPathExtension( p_demux, ".mp3" ) )
133 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
135 if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
137 vlc_bool_t b_ok = VLC_FALSE;
140 if( !p_demux->b_force && !b_forced ) return VLC_EGENERIC;
142 i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
145 if( HeaderCheck( header = GetDWBE( p_peek ) ) )
153 if( !b_ok && !p_demux->b_force ) return VLC_EGENERIC;
156 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
157 memset( p_sys, 0, sizeof( demux_sys_t ) );
159 p_sys->b_start = VLC_TRUE;
162 /* Load the mpeg audio packetizer */
163 INIT_APACKETIZER( p_sys->p_packetizer, 'm', 'p', 'g', 'a' );
164 es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
165 LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "mpga" );
168 if( HeaderCheck( header ) )
171 const uint8_t *p_xing;
173 if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) < 21 )
174 return VLC_SUCCESS; /* No header */
176 if( MPGA_VERSION( header ) == 0 )
178 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
182 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
185 if( i_skip + 8 < i_xing && !strncmp( (char *)&p_xing[i_skip], "Xing", 4 ) )
187 unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
189 p_xing += i_skip + 8;
190 i_xing -= i_skip + 8;
193 if( i_flags&0x01 && i_skip + 4 <= i_xing ) /* XING_FRAMES */
195 p_sys->i_xing_frames = GetDWBE( &p_xing[i_skip] );
198 if( i_flags&0x02 && i_skip + 4 <= i_xing ) /* XING_BYTES */
200 p_sys->i_xing_bytes = GetDWBE( &p_xing[i_skip] );
203 if( i_flags&0x04 ) /* XING_TOC */
208 // FIXME: doesn't return the right bitrage average, at least
210 if( i_flags&0x08 && i_skip + 4 <= i_xing ) /* XING_VBR */
212 p_sys->i_xing_bitrate_avg = GetDWBE( &p_xing[i_skip] );
213 msg_Dbg( p_demux, "xing vbr value present (%d)",
214 p_sys->i_xing_bitrate_avg );
217 if( p_sys->i_xing_frames > 0 && p_sys->i_xing_bytes > 0 )
219 p_sys->i_xing_frame_samples = mpga_frame_samples( header );
220 msg_Dbg( p_demux, "xing frames&bytes value present "
221 "(%d bytes, %d frames, %d samples/frame)",
222 p_sys->i_xing_bytes, p_sys->i_xing_frames,
223 p_sys->i_xing_frame_samples );
228 if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) ) == NULL )
232 p_block_in->i_pts = p_block_in->i_dts = 1;
233 p_block_out = p_sys->p_packetizer->pf_packetize(
234 p_sys->p_packetizer, &p_block_in );
236 if( p_block_out == NULL )
238 msg_Dbg( p_demux, "did not sync on first block" );
239 p_sys->b_initial_sync_failed = VLC_TRUE;
242 p_sys->b_initial_sync_failed = VLC_FALSE;
244 p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
246 if( p_sys->i_xing_bytes && p_sys->i_xing_frames &&
247 p_sys->i_xing_frame_samples )
249 p_sys->i_bitrate_avg = p_sys->i_xing_bytes * I64C(8) *
250 p_sys->p_packetizer->fmt_out.audio.i_rate /
251 p_sys->i_xing_frames / p_sys->i_xing_frame_samples;
254 p_sys->p_block_in = p_block_in;
255 p_sys->p_block_out = p_block_out;
257 /* Parse possible id3 header */
258 input_thread_t *p_input = (input_thread_t *)
259 vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
262 if( !( input_GetItem( p_input )->p_meta->i_status & ITEM_PREPARSED ) )
264 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
265 if( !p_demux->p_private )
267 vlc_object_release( p_input );
270 if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
272 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
273 p_sys->meta = p_demux_meta->p_meta;
274 p_demux->p_private = NULL;
275 module_Unneed( p_demux, p_id3 );
276 p_sys->i_attachments = p_demux_meta->i_attachments;
277 p_sys->attachments = p_demux_meta->attachments;
279 free( p_demux->p_private );
281 vlc_object_release( p_input );
285 p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
286 vlc_audio_replay_gain_MergeFromMeta( &p_sys->p_packetizer->fmt_out.audio_replay_gain,
288 p_sys->p_es = es_out_Add( p_demux->out,
289 &p_sys->p_packetizer->fmt_out);
293 /*****************************************************************************
294 * Demux: reads and demuxes data packets
295 *****************************************************************************
296 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
297 *****************************************************************************/
298 static int Demux( demux_t *p_demux )
300 demux_sys_t *p_sys = p_demux->p_sys;
301 block_t *p_block_in, *p_block_out;
304 p_sys->b_start = VLC_FALSE;
305 p_block_in = p_sys->p_block_in;
306 p_sys->p_block_in = NULL;
307 p_block_out = p_sys->p_block_out;
308 p_sys->p_block_out = NULL;
312 if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) )
317 if( p_demux->p_sys->b_initial_sync_failed == VLC_TRUE )
319 p_block_in->i_pts = p_block_in->i_dts = 1;
320 /* Only try to resync once */
321 p_demux->p_sys->b_initial_sync_failed = 0;
324 p_block_in->i_pts = p_block_in->i_dts = 0;
325 p_block_out = p_sys->p_packetizer->pf_packetize(
326 p_sys->p_packetizer, &p_block_in );
334 block_t *p_next = p_block_out->p_next;
336 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
338 p_block_out->p_next = NULL;
339 p_sys->i_pts = p_block_out->i_pts;
340 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
342 p_block_out = p_next;
344 p_block_out = p_sys->p_packetizer->pf_packetize(
345 p_sys->p_packetizer, &p_block_in );
350 /*****************************************************************************
351 * Close: frees unused data
352 *****************************************************************************/
353 static void Close( vlc_object_t * p_this )
355 demux_t *p_demux = (demux_t*)p_this;
356 demux_sys_t *p_sys = p_demux->p_sys;
358 DESTROY_PACKETIZER( p_sys->p_packetizer );
359 if( p_sys->meta ) vlc_meta_Delete( p_sys->meta );
360 if( p_sys->p_block_out ) block_Release( p_sys->p_block_out );
363 for( i = 0; i < p_sys->i_attachments; i++ )
364 free( p_sys->attachments[i] );
365 TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
370 /*****************************************************************************
372 *****************************************************************************/
373 static int Control( demux_t *p_demux, int i_query, va_list args )
375 demux_sys_t *p_sys = p_demux->p_sys;
380 input_attachment_t ***ppp_attach;
386 p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
387 vlc_meta_Merge( p_meta, p_sys->meta );
390 case DEMUX_GET_ATTACHMENTS:
392 (input_attachment_t***)va_arg( args, input_attachment_t*** );
393 pi_int = (int*)va_arg( args, int * );
395 if( p_sys->i_attachments <= 0 )
398 *pi_int = p_sys->i_attachments;
399 *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
400 for( i = 0; i < p_sys->i_attachments; i++ )
401 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
405 pi64 = (int64_t*)va_arg( args, int64_t * );
406 *pi64 = p_sys->i_pts + p_sys->i_time_offset;
410 /* FIXME TODO: implement a high precision seek (with mp3 parsing)
411 * needed for multi-input */
414 i_ret = demux2_vaControlHelper( p_demux->s, 0, -1,
415 p_sys->i_bitrate_avg, 1, i_query,
417 /* No bitrate, we can't have it precisely, but we can compute
418 * a raw approximation with time/position */
419 if( i_ret && i_query == DEMUX_GET_LENGTH &&!p_sys->i_bitrate_avg )
421 float f_pos = (double)( stream_Tell( p_demux->s ) ) /
422 (double)( stream_Size( p_demux->s ) );
423 /* The first few seconds are guaranteed to be very whacky,
424 * don't bother trying ... Too bad */
426 (p_sys->i_pts + p_sys->i_time_offset) < 8000000 )
429 pi64 = (int64_t *)va_arg( args, int64_t * );
430 *pi64 = (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
433 if( !i_ret && p_sys->i_bitrate_avg > 0 &&
434 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
436 int64_t i_time = I64C(8000000) * stream_Tell(p_demux->s) /
437 p_sys->i_bitrate_avg;
439 /* Fix time_offset */
440 if( i_time >= 0 ) p_sys->i_time_offset = i_time - p_sys->i_pts;