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 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc/input.h>
32 #include "vlc_codec.h"
35 #define MPGA_PACKET_SIZE 1024
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
44 set_category( CAT_INPUT );
45 set_subcategory( SUBCAT_INPUT_DEMUX );
46 set_description( _("MPEG audio / MP3 demuxer" ) );
47 set_capability( "demux2", 100 );
48 set_callbacks( Open, Close );
49 add_shortcut( "mpga" );
50 add_shortcut( "mp3" );
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Demux ( demux_t * );
57 static int Control( demux_t *, int, va_list );
65 decoder_t *p_packetizer;
68 mtime_t i_time_offset;
69 int i_bitrate_avg; /* extracted from Xing header */
71 vlc_bool_t b_initial_sync_failed;
75 int i_xing_bitrate_avg;
76 int i_xing_frame_samples;
77 block_t *p_block_in, *p_block_out;
80 static int HeaderCheck( uint32_t h )
82 if( ((( h >> 21 )&0x07FF) != 0x07FF ) /* header sync */
83 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
84 || (((h >> 12)&0x0F) == 0x0F )
85 || (((h >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
86 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
87 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
94 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
95 #define MPGA_LAYER( h ) ( 3 - (((h)>>17)&0x03) )
96 #define MPGA_MODE(h) (((h)>> 6)&0x03)
98 static int mpga_frame_samples( uint32_t h )
100 switch( MPGA_LAYER(h) )
107 return MPGA_VERSION(h) ? 576 : 1152;
114 /*****************************************************************************
115 * Open: initializes demux structures
116 *****************************************************************************/
117 static int Open( vlc_object_t * p_this )
119 demux_t *p_demux = (demux_t*)p_this;
121 vlc_bool_t b_forced = VLC_FALSE;
126 block_t *p_block_in, *p_block_out;
128 if( p_demux->psz_path )
130 int i_len = strlen( p_demux->psz_path );
131 if( i_len > 4 && !strcasecmp( &p_demux->psz_path[i_len - 4], ".mp3" ) )
137 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
139 if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
141 vlc_bool_t b_ok = VLC_FALSE;
144 if( !p_demux->b_force && !b_forced ) return VLC_EGENERIC;
146 i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
149 if( HeaderCheck( header = GetDWBE( p_peek ) ) )
157 if( !b_ok && !p_demux->b_force ) return VLC_EGENERIC;
160 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
161 memset( p_sys, 0, sizeof( demux_sys_t ) );
163 p_sys->p_packetizer = 0;
164 p_sys->b_start = VLC_TRUE;
166 p_demux->pf_demux = Demux;
167 p_demux->pf_control = Control;
170 * Load the mpeg audio packetizer
172 p_sys->p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_PACKETIZER );
173 p_sys->p_packetizer->pf_decode_audio = NULL;
174 p_sys->p_packetizer->pf_decode_video = NULL;
175 p_sys->p_packetizer->pf_decode_sub = NULL;
176 p_sys->p_packetizer->pf_packetize = NULL;
177 es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
178 VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
179 es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
180 p_sys->p_packetizer->p_module =
181 module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
183 if( p_sys->p_packetizer->p_module == NULL )
185 msg_Err( p_demux, "cannot find mpga packetizer" );
186 Close( VLC_OBJECT(p_demux ) );
191 if( HeaderCheck( header ) )
196 if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) < 21 )
197 return VLC_SUCCESS; /* No header */
199 if( MPGA_VERSION( header ) == 0 )
201 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
205 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
208 if( i_skip + 8 < i_xing && !strncmp( (char *)&p_xing[i_skip], "Xing", 4 ) )
210 unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
212 p_xing += i_skip + 8;
213 i_xing -= i_skip + 8;
216 if( i_flags&0x01 && i_skip + 4 <= i_xing ) /* XING_FRAMES */
218 p_sys->i_xing_frames = GetDWBE( &p_xing[i_skip] );
221 if( i_flags&0x02 && i_skip + 4 <= i_xing ) /* XING_BYTES */
223 p_sys->i_xing_bytes = GetDWBE( &p_xing[i_skip] );
226 if( i_flags&0x04 ) /* XING_TOC */
231 // FIXME: doesn't return the right bitrage average, at least
233 if( i_flags&0x08 && i_skip + 4 <= i_xing ) /* XING_VBR */
235 p_sys->i_xing_bitrate_avg = GetDWBE( &p_xing[i_skip] );
236 msg_Dbg( p_demux, "xing vbr value present (%d)",
237 p_sys->i_xing_bitrate_avg );
240 if( p_sys->i_xing_frames > 0 && p_sys->i_xing_bytes > 0 )
242 p_sys->i_xing_frame_samples = mpga_frame_samples( header );
243 msg_Dbg( p_demux, "xing frames&bytes value present "
244 "(%d bytes, %d frames, %d samples/frame)",
245 p_sys->i_xing_bytes, p_sys->i_xing_frames,
246 p_sys->i_xing_frame_samples );
251 if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) ) == NULL )
255 p_block_in->i_pts = p_block_in->i_dts = 1;
256 p_block_out = p_sys->p_packetizer->pf_packetize(
257 p_sys->p_packetizer, &p_block_in );
259 if( p_block_out == NULL )
261 msg_Dbg( p_demux, "did not sync on first block" );
262 p_sys->b_initial_sync_failed = VLC_TRUE;
265 p_sys->b_initial_sync_failed = VLC_FALSE;
267 p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
268 p_sys->p_es = es_out_Add( p_demux->out,
269 &p_sys->p_packetizer->fmt_out);
270 p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
272 if( p_sys->i_xing_bytes && p_sys->i_xing_frames &&
273 p_sys->i_xing_frame_samples )
275 p_sys->i_bitrate_avg = p_sys->i_xing_bytes * I64C(8) *
276 p_sys->p_packetizer->fmt_out.audio.i_rate /
277 p_sys->i_xing_frames / p_sys->i_xing_frame_samples;
280 p_sys->p_block_in = p_block_in;
281 p_sys->p_block_out = p_block_out;
283 /* Parse possible id3 header */
284 if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
286 p_sys->meta = (vlc_meta_t *)p_demux->p_private;
287 p_demux->p_private = NULL;
288 module_Unneed( p_demux, p_id3 );
294 /*****************************************************************************
295 * Demux: reads and demuxes data packets
296 *****************************************************************************
297 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
298 *****************************************************************************/
299 static int Demux( demux_t *p_demux )
301 demux_sys_t *p_sys = p_demux->p_sys;
302 block_t *p_block_in, *p_block_out;
305 p_sys->b_start = VLC_FALSE;
306 p_block_in = p_sys->p_block_in;
307 p_block_out = p_sys->p_block_out;
311 if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) )
316 if( p_demux->p_sys->b_initial_sync_failed == VLC_TRUE )
318 p_block_in->i_pts = p_block_in->i_dts = 1;
319 /* Only try to resync once */
320 p_demux->p_sys->b_initial_sync_failed = 0;
323 p_block_in->i_pts = p_block_in->i_dts = 0;
324 p_block_out = p_sys->p_packetizer->pf_packetize(
325 p_sys->p_packetizer, &p_block_in );
333 block_t *p_next = p_block_out->p_next;
335 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
337 p_block_out->p_next = NULL;
338 p_sys->i_pts = p_block_out->i_pts;
339 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
341 p_block_out = p_next;
343 p_block_out = p_sys->p_packetizer->pf_packetize(
344 p_sys->p_packetizer, &p_block_in );
349 /*****************************************************************************
350 * Close: frees unused data
351 *****************************************************************************/
352 static void Close( vlc_object_t * p_this )
354 demux_t *p_demux = (demux_t*)p_this;
355 demux_sys_t *p_sys = p_demux->p_sys;
357 if( p_sys->meta ) vlc_meta_Delete( p_sys->meta );
359 if( p_sys->p_packetizer && p_sys->p_packetizer->p_module )
360 module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
361 if( p_sys->p_packetizer )
362 vlc_object_destroy( p_sys->p_packetizer );
367 /*****************************************************************************
369 *****************************************************************************/
370 static int Control( demux_t *p_demux, int i_query, va_list args )
372 demux_sys_t *p_sys = p_demux->p_sys;
374 vlc_meta_t **pp_meta;
380 pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
381 if( p_sys->meta ) *pp_meta = vlc_meta_Duplicate( p_sys->meta );
382 else *pp_meta = NULL;
386 pi64 = (int64_t*)va_arg( args, int64_t * );
387 *pi64 = p_sys->i_pts + p_sys->i_time_offset;
391 /* FIXME TODO: implement a high precision seek (with mp3 parsing)
392 * needed for multi-input */
395 i_ret = demux2_vaControlHelper( p_demux->s, 0, -1,
396 p_sys->i_bitrate_avg, 1, i_query,
398 if( !i_ret && p_sys->i_bitrate_avg > 0 &&
399 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
401 int64_t i_time = I64C(8000000) * stream_Tell(p_demux->s) /
402 p_sys->i_bitrate_avg;
404 /* Fix time_offset */
405 if( i_time >= 0 ) p_sys->i_time_offset = i_time - p_sys->i_pts;