1 /*****************************************************************************
2 * mpc.c : MPC stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc_demux.h>
30 #include <vlc_codec.h>
33 #include <mpcdec/mpcdec.h>
36 * - test stream version 4..6
37 * - test fixed float version
41 * It is done the ugly way (the demux does the decode stage... but it works
44 /*****************************************************************************
46 *****************************************************************************/
47 #define REPLAYGAIN_TYPE_TEXT N_("Replay Gain type" )
48 #define REPLAYGAIN_TYPE_LONGTEXT N_( "Musepack can have a title-specific " \
49 "replay gain (volume control) or an album-specific one. " \
50 "Choose which type you want to use" )
52 static int Open ( vlc_object_t * );
53 static void Close ( vlc_object_t * );
55 static int pi_replaygain_type[] = { 0, 1, 2 };
56 static char *ppsz_replaygain_type[] = { N_("None"), N_("Title"), N_("Album") };
59 set_shortname( "MPC" );
60 set_description( _("MusePack demuxer") );
61 set_category( CAT_INPUT );
62 set_subcategory( SUBCAT_INPUT_DEMUX );
63 set_capability( "demux2", 145 );
65 add_integer( "mpc-replaygain-type", 2, NULL,
66 REPLAYGAIN_TYPE_TEXT, REPLAYGAIN_TYPE_LONGTEXT, VLC_FALSE );
67 change_integer_list( pi_replaygain_type, ppsz_replaygain_type, 0 );
69 set_callbacks( Open, Close );
70 add_shortcut( "mpc" );
73 /*****************************************************************************
75 *****************************************************************************/
76 static int Demux ( demux_t * );
77 static int Control( demux_t *, int, va_list );
94 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
95 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset );
96 mpc_int32_t ReaderTell( void *p_private);
97 mpc_int32_t ReaderGetSize( void *p_private );
98 mpc_bool_t ReaderCanSeek( void *p_private );
100 /*****************************************************************************
101 * Open: initializes ES structures
102 *****************************************************************************/
103 static int Open( vlc_object_t * p_this )
105 demux_t *p_demux = (demux_t*)p_this;
112 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
115 if( memcmp( p_peek, "MP+", 3 ) )
117 /* for v4..6 we check extension file */
118 const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
120 if( i_version < 4 || i_version > 6 )
123 if( !p_demux->psz_demux || strcmp( p_demux->psz_demux, "mpc" ) )
125 /* Check file name extension */
127 if( !p_demux->psz_path )
130 i_len = strlen( p_demux->psz_path );
132 ( strcasecmp( &p_demux->psz_path[i_len-4], ".mpc" ) &&
133 strcasecmp( &p_demux->psz_path[i_len-4], ".mp+" ) &&
134 strcasecmp( &p_demux->psz_path[i_len-4], ".mpp" ) ) )
140 p_sys = malloc( sizeof( demux_sys_t ) );
141 memset( p_sys, 0, sizeof(demux_sys_t) );
143 p_sys->i_position = 0;
145 p_sys->reader.read = ReaderRead;
146 p_sys->reader.seek = ReaderSeek;
147 p_sys->reader.tell = ReaderTell;
148 p_sys->reader.get_size = ReaderGetSize;
149 p_sys->reader.canseek = ReaderCanSeek;
150 p_sys->reader.data = p_demux;
153 mpc_streaminfo_init( &p_sys->info );
154 if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
162 mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
163 if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
170 /* Handle reaply gain */
171 if( p_sys->info.peak_title != 32767 )
173 int i_type = var_CreateGetInteger( p_demux, "mpc-replaygain-type" );
177 if( i_type == 2 ) // album
179 gain = p_sys->info.gain_album;
180 peak = p_sys->info.peak_album;
182 else if( i_type == 1 ) // title
184 gain = p_sys->info.gain_title;
185 peak = p_sys->info.peak_title;
195 double g = pow( 10, (double)gain / 2000.0 );
196 double gmax = (double)32767.0 / (peak+1);
200 msg_Dbg( p_demux, "Using reaply gain factor %f", g );
201 mpc_decoder_scale_output( &p_sys->decoder, g );
205 /* Fill p_demux fields */
206 p_demux->pf_demux = Demux;
207 p_demux->pf_control = Control;
208 p_demux->p_sys = p_sys;
211 #ifndef MPC_FIXED_POINT
212 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', '3', '2' ) );
214 # ifdef WORDS_BIGENDIAN
215 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'b' ) );
217 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'l' ) );
220 fmt.audio.i_channels = p_sys->info.channels;
221 fmt.audio.i_rate = p_sys->info.sample_freq;
222 fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
223 fmt.audio.i_bitspersample = 32;
224 fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
225 fmt.audio.i_bitspersample;
226 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
229 /* Parse possible id3 header */
230 if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
232 p_sys->p_meta = (vlc_meta_t *)p_demux->p_private;
233 p_demux->p_private = NULL;
234 module_Unneed( p_demux, p_id3 );
238 p_sys->p_meta = vlc_meta_New();
240 sprintf( psz_info, "Musepack v%d", p_sys->info.stream_version );
241 //vlc_meta_SetCodecName( p_sys->p_meta, psz_info );
242 // ^^ doesn't exist (yet?) to set VLC_META_CODEC_NAME, so...
243 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
248 /*****************************************************************************
249 * Close: frees unused data
250 *****************************************************************************/
251 static void Close( vlc_object_t * p_this )
253 demux_t *p_demux = (demux_t*)p_this;
254 demux_sys_t *p_sys = p_demux->p_sys;
259 /*****************************************************************************
261 *****************************************************************************
262 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
263 *****************************************************************************/
264 static int Demux( demux_t *p_demux )
266 demux_sys_t *p_sys = p_demux->p_sys;
270 p_data = block_New( p_demux,
271 MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
272 i_ret = mpc_decoder_decode( &p_sys->decoder,
273 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
277 block_Release( p_data );
278 return i_ret < 0 ? -1 : 0;
282 p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
283 p_data->i_dts = p_data->i_pts =
284 1 + I64C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
286 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
288 es_out_Send( p_demux->out, p_sys->p_es, p_data );
291 p_sys->i_position += i_ret;
296 /*****************************************************************************
298 *****************************************************************************/
299 static int Control( demux_t *p_demux, int i_query, va_list args )
301 demux_sys_t *p_sys = p_demux->p_sys;
309 p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
311 vlc_meta_Merge( p_meta, p_sys->p_meta );
316 case DEMUX_GET_LENGTH:
317 pi64 = (int64_t*)va_arg( args, int64_t * );
318 *pi64 = I64C(1000000) * p_sys->info.pcm_samples /
319 p_sys->info.sample_freq;
322 case DEMUX_GET_POSITION:
323 pf = (double*)va_arg( args, double * );
324 if( p_sys->info.pcm_samples > 0 )
325 *pf = (double) p_sys->i_position /
326 (double)p_sys->info.pcm_samples;
332 pi64 = (int64_t*)va_arg( args, int64_t * );
333 *pi64 = I64C(1000000) * p_sys->i_position /
334 p_sys->info.sample_freq;
337 case DEMUX_SET_POSITION:
338 f = (double)va_arg( args, double );
339 i64 = (int64_t)(f * p_sys->info.pcm_samples);
340 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
342 p_sys->i_position = i64;
348 i64 = (int64_t)va_arg( args, int64_t );
349 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
351 p_sys->i_position = i64;
361 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
363 demux_t *p_demux = (demux_t*)p_private;
364 return stream_Read( p_demux->s, dst, i_size );
367 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
369 demux_t *p_demux = (demux_t*)p_private;
370 return !stream_Seek( p_demux->s, i_offset );
373 mpc_int32_t ReaderTell( void *p_private)
375 demux_t *p_demux = (demux_t*)p_private;
376 return stream_Tell( p_demux->s );
379 mpc_int32_t ReaderGetSize( void *p_private )
381 demux_t *p_demux = (demux_t*)p_private;
382 return stream_Size( p_demux->s );
385 mpc_bool_t ReaderCanSeek( void *p_private )
387 demux_t *p_demux = (demux_t*)p_private;
388 vlc_bool_t b_canseek;
390 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );