1 /*****************************************************************************
2 * mpc.c : MPC stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 the VideoLAN team
5 * $Id: a52.c 11699 2005-07-11 08:52:14Z sam $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr.com>
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 *****************************************************************************/
28 #include <vlc/input.h>
29 #include <vlc_codec.h>
32 #include <mpcdec/mpcdec.h>
35 * - test stream version 4..6
36 * - test fixed float version
40 * It is done the ugly way (the demux does the decode stage... but it works
43 /*****************************************************************************
45 *****************************************************************************/
46 #define REPLAYGAIN_TYPE_TEXT N_("Replay Gain type" )
47 #define REPLAYGAIN_TYPE_LONGTEXT N_( "Replay Gain type" )
49 static int Open ( vlc_object_t * );
50 static void Close ( vlc_object_t * );
52 static int pi_replaygain_type[] = { 0, 1, 2 };
53 static char *ppsz_replaygain_type[] = { N_("None"), N_("Title"), N_("Album") };
56 set_shortname( "MPC" );
57 set_description( _("MPC demuxer") );
58 set_category( CAT_INPUT );
59 set_subcategory( SUBCAT_INPUT_DEMUX );
60 set_capability( "demux2", 145 );
62 add_integer( "mpc-replaygain-type", 2, NULL,
63 REPLAYGAIN_TYPE_TEXT, REPLAYGAIN_TYPE_LONGTEXT, VLC_FALSE );
64 change_integer_list( pi_replaygain_type, ppsz_replaygain_type, 0 );
66 set_callbacks( Open, Close );
67 add_shortcut( "mpc" );
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Demux ( demux_t * );
74 static int Control( demux_t *, int, va_list );
91 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
92 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset );
93 mpc_int32_t ReaderTell( void *p_private);
94 mpc_int32_t ReaderGetSize( void *p_private );
95 mpc_bool_t ReaderCanSeek( void *p_private );
97 /*****************************************************************************
98 * Open: initializes ES structures
99 *****************************************************************************/
100 static int Open( vlc_object_t * p_this )
102 demux_t *p_demux = (demux_t*)p_this;
109 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
112 if( memcmp( p_peek, "MP+", 3 ) )
114 /* for v4..6 we check extention file */
115 const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
117 if( i_version < 4 || i_version > 6 )
120 if( !p_demux->psz_demux || strcmp( p_demux->psz_demux, "mpc" ) )
122 /* Check file name extension */
124 if( !p_demux->psz_path )
127 i_len = strlen( p_demux->psz_path );
129 ( strcasecmp( &p_demux->psz_path[i_len-4], ".mpc" ) &&
130 strcasecmp( &p_demux->psz_path[i_len-4], ".mp+" ) &&
131 strcasecmp( &p_demux->psz_path[i_len-4], ".mpp" ) ) )
137 p_sys = malloc( sizeof( demux_sys_t ) );
138 memset( p_sys, 0, sizeof(demux_sys_t) );
140 p_sys->i_position = 0;
142 p_sys->reader.read = ReaderRead;
143 p_sys->reader.seek = ReaderSeek;
144 p_sys->reader.tell = ReaderTell;
145 p_sys->reader.get_size = ReaderGetSize;
146 p_sys->reader.canseek = ReaderCanSeek;
147 p_sys->reader.data = p_demux;
150 mpc_streaminfo_init( &p_sys->info );
151 if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
159 mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
160 if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
167 /* Handle reaply gain */
168 if( p_sys->info.peak_title != 32767 )
170 int i_type = var_CreateGetInteger( p_demux, "mpc-replaygain-type" );
174 if( i_type == 2 ) // album
176 gain = p_sys->info.gain_album;
177 peak = p_sys->info.peak_album;
179 else if( i_type == 1 ) // title
181 gain = p_sys->info.gain_title;
182 peak = p_sys->info.peak_title;
192 double g = pow( 10, (double)gain / 2000.0 );
193 double gmax = (double)32767.0 / (peak+1);
197 msg_Dbg( p_demux, "Using reaply gain factor %f", g );
198 mpc_decoder_scale_output( &p_sys->decoder, g );
202 /* Fill p_demux fields */
203 p_demux->pf_demux = Demux;
204 p_demux->pf_control = Control;
205 p_demux->p_sys = p_sys;
208 #ifndef MPC_FIXED_POINT
209 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', '3', '2' ) );
211 # ifdef WORDS_BIGENDIAN
212 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'b' ) );
214 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'l' ) );
217 fmt.audio.i_channels = p_sys->info.channels;
218 fmt.audio.i_rate = p_sys->info.sample_freq;
219 fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
220 fmt.audio.i_bitspersample = 32;
221 fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
222 fmt.audio.i_bitspersample;
223 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
226 /* Parse possible id3 header */
227 if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
229 p_sys->p_meta = (vlc_meta_t *)p_demux->p_private;
230 p_demux->p_private = NULL;
231 module_Unneed( p_demux, p_id3 );
235 p_sys->p_meta = vlc_meta_New();
237 sprintf( psz_info, "Musepack v%d", p_sys->info.stream_version );
238 vlc_meta_Add( p_sys->p_meta, VLC_META_CODEC_NAME, psz_info );
243 /*****************************************************************************
244 * Close: frees unused data
245 *****************************************************************************/
246 static void Close( vlc_object_t * p_this )
248 demux_t *p_demux = (demux_t*)p_this;
249 demux_sys_t *p_sys = p_demux->p_sys;
254 /*****************************************************************************
256 *****************************************************************************
257 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
258 *****************************************************************************/
259 static int Demux( demux_t *p_demux )
261 demux_sys_t *p_sys = p_demux->p_sys;
265 p_data = block_New( p_demux,
266 MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
267 i_ret = mpc_decoder_decode( &p_sys->decoder,
268 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
272 block_Release( p_data );
273 return i_ret < 0 ? -1 : 0;
277 p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
278 p_data->i_dts = p_data->i_pts =
279 1 + I64C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
281 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
283 es_out_Send( p_demux->out, p_sys->p_es, p_data );
286 p_sys->i_position += i_ret;
291 /*****************************************************************************
293 *****************************************************************************/
294 static int Control( demux_t *p_demux, int i_query, va_list args )
296 demux_sys_t *p_sys = p_demux->p_sys;
299 vlc_meta_t **pp_meta;
304 pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
306 *pp_meta = vlc_meta_Duplicate( p_sys->p_meta );
311 case DEMUX_GET_LENGTH:
312 pi64 = (int64_t*)va_arg( args, int64_t * );
313 *pi64 = I64C(1000000) * p_sys->info.pcm_samples /
314 p_sys->info.sample_freq;
317 case DEMUX_GET_POSITION:
318 pf = (double*)va_arg( args, double * );
319 if( p_sys->info.pcm_samples > 0 )
320 *pf = (double) p_sys->i_position /
321 (double)p_sys->info.pcm_samples;
327 pi64 = (int64_t*)va_arg( args, int64_t * );
328 *pi64 = I64C(1000000) * p_sys->i_position /
329 p_sys->info.sample_freq;
332 case DEMUX_SET_POSITION:
333 f = (double)va_arg( args, double );
334 i64 = (int64_t)(f * p_sys->info.pcm_samples);
335 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
337 p_sys->i_position = i64;
343 i64 = (int64_t)va_arg( args, int64_t );
344 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
346 p_sys->i_position = i64;
356 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
358 demux_t *p_demux = (demux_t*)p_private;
359 return stream_Read( p_demux->s, dst, i_size );
362 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
364 demux_t *p_demux = (demux_t*)p_private;
365 return !stream_Seek( p_demux->s, i_offset );
368 mpc_int32_t ReaderTell( void *p_private)
370 demux_t *p_demux = (demux_t*)p_private;
371 return stream_Tell( p_demux->s );
374 mpc_int32_t ReaderGetSize( void *p_private )
376 demux_t *p_demux = (demux_t*)p_private;
377 return stream_Size( p_demux->s );
380 mpc_bool_t ReaderCanSeek( void *p_private )
382 demux_t *p_demux = (demux_t*)p_private;
383 vlc_bool_t b_canseek;
385 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );