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>
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_( "Musepack can have a title-specific " \
48 "replay gain (volume control) or an album-specific one. " \
49 "Choose which type you want to use" )
51 static int Open ( vlc_object_t * );
52 static void Close ( vlc_object_t * );
54 static int pi_replaygain_type[] = { 0, 1, 2 };
55 static char *ppsz_replaygain_type[] = { N_("None"), N_("Title"), N_("Album") };
58 set_shortname( "MPC" );
59 set_description( _("MusePack demuxer") );
60 set_category( CAT_INPUT );
61 set_subcategory( SUBCAT_INPUT_DEMUX );
62 set_capability( "demux2", 145 );
64 add_integer( "mpc-replaygain-type", 2, NULL,
65 REPLAYGAIN_TYPE_TEXT, REPLAYGAIN_TYPE_LONGTEXT, VLC_FALSE );
66 change_integer_list( pi_replaygain_type, ppsz_replaygain_type, 0 );
68 set_callbacks( Open, Close );
69 add_shortcut( "mpc" );
72 /*****************************************************************************
74 *****************************************************************************/
75 static int Demux ( demux_t * );
76 static int Control( demux_t *, int, va_list );
93 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
94 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset );
95 mpc_int32_t ReaderTell( void *p_private);
96 mpc_int32_t ReaderGetSize( void *p_private );
97 mpc_bool_t ReaderCanSeek( void *p_private );
99 /*****************************************************************************
100 * Open: initializes ES structures
101 *****************************************************************************/
102 static int Open( vlc_object_t * p_this )
104 demux_t *p_demux = (demux_t*)p_this;
111 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
114 if( memcmp( p_peek, "MP+", 3 ) )
116 /* for v4..6 we check extension file */
117 const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
119 if( i_version < 4 || i_version > 6 )
122 if( !p_demux->psz_demux || strcmp( p_demux->psz_demux, "mpc" ) )
124 /* Check file name extension */
126 if( !p_demux->psz_path )
129 i_len = strlen( p_demux->psz_path );
131 ( strcasecmp( &p_demux->psz_path[i_len-4], ".mpc" ) &&
132 strcasecmp( &p_demux->psz_path[i_len-4], ".mp+" ) &&
133 strcasecmp( &p_demux->psz_path[i_len-4], ".mpp" ) ) )
139 p_sys = malloc( sizeof( demux_sys_t ) );
140 memset( p_sys, 0, sizeof(demux_sys_t) );
142 p_sys->i_position = 0;
144 p_sys->reader.read = ReaderRead;
145 p_sys->reader.seek = ReaderSeek;
146 p_sys->reader.tell = ReaderTell;
147 p_sys->reader.get_size = ReaderGetSize;
148 p_sys->reader.canseek = ReaderCanSeek;
149 p_sys->reader.data = p_demux;
152 mpc_streaminfo_init( &p_sys->info );
153 if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
161 mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
162 if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
169 /* Handle reaply gain */
170 if( p_sys->info.peak_title != 32767 )
172 int i_type = var_CreateGetInteger( p_demux, "mpc-replaygain-type" );
176 if( i_type == 2 ) // album
178 gain = p_sys->info.gain_album;
179 peak = p_sys->info.peak_album;
181 else if( i_type == 1 ) // title
183 gain = p_sys->info.gain_title;
184 peak = p_sys->info.peak_title;
194 double g = pow( 10, (double)gain / 2000.0 );
195 double gmax = (double)32767.0 / (peak+1);
199 msg_Dbg( p_demux, "Using reaply gain factor %f", g );
200 mpc_decoder_scale_output( &p_sys->decoder, g );
204 /* Fill p_demux fields */
205 p_demux->pf_demux = Demux;
206 p_demux->pf_control = Control;
207 p_demux->p_sys = p_sys;
210 #ifndef MPC_FIXED_POINT
211 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', '3', '2' ) );
213 # ifdef WORDS_BIGENDIAN
214 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'b' ) );
216 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'l' ) );
219 fmt.audio.i_channels = p_sys->info.channels;
220 fmt.audio.i_rate = p_sys->info.sample_freq;
221 fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
222 fmt.audio.i_bitspersample = 32;
223 fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
224 fmt.audio.i_bitspersample;
225 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
228 /* Parse possible id3 header */
229 if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
231 p_sys->p_meta = (vlc_meta_t *)p_demux->p_private;
232 p_demux->p_private = NULL;
233 module_Unneed( p_demux, p_id3 );
237 p_sys->p_meta = vlc_meta_New();
239 sprintf( psz_info, "Musepack v%d", p_sys->info.stream_version );
240 //vlc_meta_SetCodecName( p_sys->p_meta, psz_info );
241 // ^^ doesn't exist (yet?) to set VLC_META_CODEC_NAME, so...
242 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
247 /*****************************************************************************
248 * Close: frees unused data
249 *****************************************************************************/
250 static void Close( vlc_object_t * p_this )
252 demux_t *p_demux = (demux_t*)p_this;
253 demux_sys_t *p_sys = p_demux->p_sys;
258 /*****************************************************************************
260 *****************************************************************************
261 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
262 *****************************************************************************/
263 static int Demux( demux_t *p_demux )
265 demux_sys_t *p_sys = p_demux->p_sys;
269 p_data = block_New( p_demux,
270 MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
271 i_ret = mpc_decoder_decode( &p_sys->decoder,
272 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
276 block_Release( p_data );
277 return i_ret < 0 ? -1 : 0;
281 p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
282 p_data->i_dts = p_data->i_pts =
283 1 + I64C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
285 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
287 es_out_Send( p_demux->out, p_sys->p_es, p_data );
290 p_sys->i_position += i_ret;
295 /*****************************************************************************
297 *****************************************************************************/
298 static int Control( demux_t *p_demux, int i_query, va_list args )
300 demux_sys_t *p_sys = p_demux->p_sys;
308 p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
310 vlc_meta_Merge( p_meta, p_sys->p_meta );
315 case DEMUX_GET_LENGTH:
316 pi64 = (int64_t*)va_arg( args, int64_t * );
317 *pi64 = I64C(1000000) * p_sys->info.pcm_samples /
318 p_sys->info.sample_freq;
321 case DEMUX_GET_POSITION:
322 pf = (double*)va_arg( args, double * );
323 if( p_sys->info.pcm_samples > 0 )
324 *pf = (double) p_sys->i_position /
325 (double)p_sys->info.pcm_samples;
331 pi64 = (int64_t*)va_arg( args, int64_t * );
332 *pi64 = I64C(1000000) * p_sys->i_position /
333 p_sys->info.sample_freq;
336 case DEMUX_SET_POSITION:
337 f = (double)va_arg( args, double );
338 i64 = (int64_t)(f * p_sys->info.pcm_samples);
339 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
341 p_sys->i_position = i64;
347 i64 = (int64_t)va_arg( args, int64_t );
348 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
350 p_sys->i_position = i64;
360 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
362 demux_t *p_demux = (demux_t*)p_private;
363 return stream_Read( p_demux->s, dst, i_size );
366 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
368 demux_t *p_demux = (demux_t*)p_private;
369 return !stream_Seek( p_demux->s, i_offset );
372 mpc_int32_t ReaderTell( void *p_private)
374 demux_t *p_demux = (demux_t*)p_private;
375 return stream_Tell( p_demux->s );
378 mpc_int32_t ReaderGetSize( void *p_private )
380 demux_t *p_demux = (demux_t*)p_private;
381 return stream_Size( p_demux->s );
384 mpc_bool_t ReaderCanSeek( void *p_private )
386 demux_t *p_demux = (demux_t*)p_private;
387 vlc_bool_t b_canseek;
389 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );