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 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <vlc_codec.h>
37 #include <vlc_input.h>
39 #define MPGA_PACKET_SIZE 1024
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
48 set_category( CAT_INPUT );
49 set_subcategory( SUBCAT_INPUT_DEMUX );
50 set_description( N_("MPEG audio / MP3 demuxer" ) );
51 set_capability( "demux", 100 );
52 set_callbacks( Open, Close );
53 add_shortcut( "mpga" );
54 add_shortcut( "mp3" );
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Demux ( demux_t * );
61 static int Control( demux_t *, int, va_list );
68 decoder_t *p_packetizer;
71 mtime_t i_time_offset;
72 int i_bitrate_avg; /* extracted from Xing header */
74 bool b_initial_sync_failed;
78 int i_xing_bitrate_avg;
79 int i_xing_frame_samples;
80 block_t *p_block_in, *p_block_out;
83 static int HeaderCheck( uint32_t h )
85 if( ((( h >> 21 )&0x07FF) != 0x07FF ) /* header sync */
86 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
87 || (((h >> 12)&0x0F) == 0x0F )
88 || (((h >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
89 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
90 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
97 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
98 #define MPGA_LAYER( h ) ( 3 - (((h)>>17)&0x03) )
99 #define MPGA_MODE(h) (((h)>> 6)&0x03)
101 static int mpga_frame_samples( uint32_t h )
103 switch( MPGA_LAYER(h) )
110 return MPGA_VERSION(h) ? 576 : 1152;
117 /*****************************************************************************
118 * Open: initializes demux structures
119 *****************************************************************************/
120 static int Open( vlc_object_t * p_this )
122 demux_t *p_demux = (demux_t*)p_this;
124 bool b_forced = false;
127 const uint8_t *p_peek;
128 block_t *p_block_in, *p_block_out;
130 if( demux_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 ) ) )
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 = true;
161 /* Load the mpeg audio packetizer */
162 INIT_APACKETIZER( p_sys->p_packetizer, 'm', 'p', 'g', 'a' );
163 es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
164 LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "mpga" );
167 if( HeaderCheck( header ) )
170 const uint8_t *p_xing;
172 if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) < 21 )
173 return VLC_SUCCESS; /* No header */
175 if( MPGA_VERSION( header ) == 0 )
177 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
181 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
184 if( i_skip + 8 < i_xing && !strncmp( (char *)&p_xing[i_skip], "Xing", 4 ) )
186 unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
188 p_xing += i_skip + 8;
189 i_xing -= i_skip + 8;
192 if( i_flags&0x01 && i_skip + 4 <= i_xing ) /* XING_FRAMES */
194 p_sys->i_xing_frames = GetDWBE( &p_xing[i_skip] );
197 if( i_flags&0x02 && i_skip + 4 <= i_xing ) /* XING_BYTES */
199 p_sys->i_xing_bytes = GetDWBE( &p_xing[i_skip] );
202 if( i_flags&0x04 ) /* XING_TOC */
207 // FIXME: doesn't return the right bitrage average, at least
209 if( i_flags&0x08 && i_skip + 4 <= i_xing ) /* XING_VBR */
211 p_sys->i_xing_bitrate_avg = GetDWBE( &p_xing[i_skip] );
212 msg_Dbg( p_demux, "xing vbr value present (%d)",
213 p_sys->i_xing_bitrate_avg );
216 if( p_sys->i_xing_frames > 0 && p_sys->i_xing_bytes > 0 )
218 p_sys->i_xing_frame_samples = mpga_frame_samples( header );
219 msg_Dbg( p_demux, "xing frames&bytes value present "
220 "(%d bytes, %d frames, %d samples/frame)",
221 p_sys->i_xing_bytes, p_sys->i_xing_frames,
222 p_sys->i_xing_frame_samples );
227 if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) ) == NULL )
231 p_block_in->i_pts = p_block_in->i_dts = 1;
232 p_block_out = p_sys->p_packetizer->pf_packetize(
233 p_sys->p_packetizer, &p_block_in );
235 if( p_block_out == NULL )
237 msg_Dbg( p_demux, "did not sync on first block" );
238 p_sys->b_initial_sync_failed = true;
241 p_sys->b_initial_sync_failed = false;
243 p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
245 if( p_sys->i_xing_bytes && p_sys->i_xing_frames &&
246 p_sys->i_xing_frame_samples )
248 p_sys->i_bitrate_avg = p_sys->i_xing_bytes * INT64_C(8) *
249 p_sys->p_packetizer->fmt_out.audio.i_rate /
250 p_sys->i_xing_frames / p_sys->i_xing_frame_samples;
253 p_sys->p_block_in = p_block_in;
254 p_sys->p_block_out = p_block_out;
257 p_sys->p_packetizer->fmt_out.b_packetized = true;
258 p_sys->p_es = es_out_Add( p_demux->out,
259 &p_sys->p_packetizer->fmt_out);
263 /*****************************************************************************
264 * Demux: reads and demuxes data packets
265 *****************************************************************************
266 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
267 *****************************************************************************/
268 static int Demux( demux_t *p_demux )
270 demux_sys_t *p_sys = p_demux->p_sys;
271 block_t *p_block_in, *p_block_out;
274 p_sys->b_start = false;
275 p_block_in = p_sys->p_block_in;
276 p_sys->p_block_in = NULL;
277 p_block_out = p_sys->p_block_out;
278 p_sys->p_block_out = NULL;
282 if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) )
287 if( p_demux->p_sys->b_initial_sync_failed == true )
289 p_block_in->i_pts = p_block_in->i_dts = 1;
290 /* Only try to resync once */
291 p_demux->p_sys->b_initial_sync_failed = 0;
294 p_block_in->i_pts = p_block_in->i_dts = 0;
295 p_block_out = p_sys->p_packetizer->pf_packetize(
296 p_sys->p_packetizer, &p_block_in );
304 block_t *p_next = p_block_out->p_next;
306 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
308 p_block_out->p_next = NULL;
309 p_sys->i_pts = p_block_out->i_pts;
310 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
312 p_block_out = p_next;
314 p_block_out = p_sys->p_packetizer->pf_packetize(
315 p_sys->p_packetizer, &p_block_in );
320 /*****************************************************************************
321 * Close: frees unused data
322 *****************************************************************************/
323 static void Close( vlc_object_t * p_this )
325 demux_t *p_demux = (demux_t*)p_this;
326 demux_sys_t *p_sys = p_demux->p_sys;
328 DESTROY_PACKETIZER( p_sys->p_packetizer );
329 if( p_sys->p_block_out ) block_Release( p_sys->p_block_out );
334 /*****************************************************************************
336 *****************************************************************************/
337 static int Control( demux_t *p_demux, int i_query, va_list args )
339 demux_sys_t *p_sys = p_demux->p_sys;
345 va_copy ( args_save, args );
349 case DEMUX_HAS_UNSUPPORTED_META:
350 pb_bool = (bool*)va_arg( args, bool* );
355 pi64 = (int64_t*)va_arg( args, int64_t * );
356 *pi64 = p_sys->i_pts + p_sys->i_time_offset;
359 case DEMUX_GET_LENGTH:
360 i_ret = demux_vaControlHelper( p_demux->s, 0, -1,
361 p_sys->i_bitrate_avg, 1, i_query,
363 /* No bitrate, we can't have it precisely, but we can compute
364 * a raw approximation with time/position */
365 if( i_ret && !p_sys->i_bitrate_avg )
367 float f_pos = (double)(uint64_t)( stream_Tell( p_demux->s ) ) /
368 (double)(uint64_t)( stream_Size( p_demux->s ) );
369 /* The first few seconds are guaranteed to be very whacky,
370 * don't bother trying ... Too bad */
372 (p_sys->i_pts + p_sys->i_time_offset) < 8000000 )
375 pi64 = (int64_t *)va_arg( args_save, int64_t * );
376 *pi64 = (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
383 /* FIXME TODO: implement a high precision seek (with mp3 parsing)
384 * needed for multi-input */
386 i_ret = demux_vaControlHelper( p_demux->s, 0, -1,
387 p_sys->i_bitrate_avg, 1, i_query,
389 if( !i_ret && p_sys->i_bitrate_avg > 0 &&
390 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
392 int64_t i_time = INT64_C(8000000) * stream_Tell(p_demux->s) /
393 p_sys->i_bitrate_avg;
395 /* Fix time_offset */
396 if( i_time >= 0 ) p_sys->i_time_offset = i_time - p_sys->i_pts;