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 *****************************************************************************/
31 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_input.h>
36 #include <vlc_codec.h>
39 #ifdef HAVE_MPC_MPCDEC_H
40 #include <mpc/mpcdec.h>
42 #include <mpcdec/mpcdec.h>
46 * - test stream version 4..6
47 * - test fixed float version
51 * It is done the ugly way (the demux does the decode stage... but it works
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Open ( vlc_object_t * );
58 static void Close ( vlc_object_t * );
61 set_category( CAT_INPUT )
62 set_subcategory( SUBCAT_INPUT_DEMUX )
63 set_description( N_("MusePack demuxer") )
64 set_capability( "demux", 145 )
66 set_callbacks( Open, Close )
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Demux ( demux_t * );
74 static int Control( demux_t *, int, va_list );
82 #ifndef HAVE_MPC_MPCDEC_H
94 #ifndef HAVE_MPC_MPCDEC_H
95 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
96 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset );
97 static mpc_int32_t ReaderTell( void *p_private);
98 static mpc_int32_t ReaderGetSize( void *p_private );
99 static mpc_bool_t ReaderCanSeek( void *p_private );
101 static mpc_int32_t ReaderRead( mpc_reader *p_private, void *dst, mpc_int32_t i_size );
102 static mpc_bool_t ReaderSeek( mpc_reader *p_private, mpc_int32_t i_offset );
103 static mpc_int32_t ReaderTell( mpc_reader *p_private);
104 static mpc_int32_t ReaderGetSize( mpc_reader *p_private );
105 static mpc_bool_t ReaderCanSeek( mpc_reader *p_private );
108 /*****************************************************************************
109 * Open: initializes ES structures
110 *****************************************************************************/
111 static int Open( vlc_object_t * p_this )
113 demux_t *p_demux = (demux_t*)p_this;
116 const uint8_t *p_peek;
118 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
121 if( memcmp( p_peek, "MP+", 3 )
122 #ifdef HAVE_MPC_MPCDEC_H
124 && memcmp( p_peek, "MPCK", 4 )
128 /* for v4..6 we check extension file */
129 const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
130 if( i_version < 4 || i_version > 6 )
133 if( !p_demux->b_force )
135 /* Check file name extension */
136 if( !demux_IsPathExtension( p_demux, ".mpc" ) &&
137 !demux_IsPathExtension( p_demux, ".mp+" ) &&
138 !demux_IsPathExtension( p_demux, ".mpp" ) )
144 p_sys = calloc( 1, sizeof( *p_sys ) );
148 p_sys->i_position = 0;
150 p_sys->reader.read = ReaderRead;
151 p_sys->reader.seek = ReaderSeek;
152 p_sys->reader.tell = ReaderTell;
153 p_sys->reader.get_size = ReaderGetSize;
154 p_sys->reader.canseek = ReaderCanSeek;
155 p_sys->reader.data = p_demux;
157 #ifndef HAVE_MPC_MPCDEC_H
159 mpc_streaminfo_init( &p_sys->info );
160 if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
164 mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
165 if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
168 p_sys->decoder = mpc_demux_init( &p_sys->reader );
169 if( !p_sys->decoder )
172 mpc_demux_get_info( p_sys->decoder, &p_sys->info );
175 /* Fill p_demux fields */
176 p_demux->pf_demux = Demux;
177 p_demux->pf_control = Control;
178 p_demux->p_sys = p_sys;
181 #ifndef MPC_FIXED_POINT
182 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_FL32 );
184 # ifdef WORDS_BIGENDIAN
185 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32B );
187 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32L );
190 fmt.audio.i_channels = p_sys->info.channels;
191 fmt.audio.i_rate = p_sys->info.sample_freq;
192 fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
193 fmt.audio.i_bitspersample = 32;
194 fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
195 fmt.audio.i_bitspersample;
197 #ifdef HAVE_MPC_MPCDEC_H
198 # define CONVERT_PEAK( mpc_peak ) (pow( 10, (mpc_peak) / 256.0 / 20.0 ) / 32767.0)
199 # define CONVERT_GAIN( mpc_gain ) (MPC_OLD_GAIN_REF - (mpc_gain) / 256.0)
201 # define CONVERT_PEAK( mpc_peak ) ((mpc_peak) / 32767.0)
202 # define CONVERT_GAIN( mpc_gain ) ((mpc_gain) / 100.0)
205 if( p_sys->info.peak_title > 0 )
207 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
208 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float) CONVERT_PEAK( p_sys->info.peak_title );
209 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
210 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float) CONVERT_GAIN( p_sys->info.gain_title );
212 if( p_sys->info.peak_album > 0 )
214 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
215 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float) CONVERT_PEAK( p_sys->info.peak_album );
216 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
217 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float) CONVERT_GAIN( p_sys->info.gain_album );
223 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
234 /*****************************************************************************
235 * Close: frees unused data
236 *****************************************************************************/
237 static void Close( vlc_object_t * p_this )
239 demux_t *p_demux = (demux_t*)p_this;
240 demux_sys_t *p_sys = p_demux->p_sys;
242 #ifdef HAVE_MPC_MPCDEC_H
244 mpc_demux_exit( p_sys->decoder );
249 /*****************************************************************************
251 *****************************************************************************
252 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
253 *****************************************************************************/
254 static int Demux( demux_t *p_demux )
256 demux_sys_t *p_sys = p_demux->p_sys;
259 #ifdef HAVE_MPC_MPCDEC_H
260 mpc_frame_info frame;
263 p_data = block_Alloc( MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
267 #ifndef HAVE_MPC_MPCDEC_H
268 i_ret = mpc_decoder_decode( &p_sys->decoder,
269 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
273 block_Release( p_data );
274 return i_ret < 0 ? -1 : 0;
277 frame.buffer = (MPC_SAMPLE_FORMAT*)p_data->p_buffer;
278 err = mpc_demux_decode( p_sys->decoder, &frame );
279 if( err != MPC_STATUS_OK )
281 block_Release( p_data );
284 else if( frame.bits == -1 )
286 block_Release( p_data );
290 i_ret = frame.samples;
294 p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
295 p_data->i_dts = p_data->i_pts =
296 VLC_TS_0 + CLOCK_FREQ * p_sys->i_position / p_sys->info.sample_freq;
298 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
300 es_out_Send( p_demux->out, p_sys->p_es, p_data );
303 p_sys->i_position += i_ret;
308 /*****************************************************************************
310 *****************************************************************************/
311 static int Control( demux_t *p_demux, int i_query, va_list args )
313 demux_sys_t *p_sys = p_demux->p_sys;
320 case DEMUX_HAS_UNSUPPORTED_META:
321 pb_bool = (bool*)va_arg( args, bool* );
325 case DEMUX_GET_LENGTH:
326 pi64 = (int64_t*)va_arg( args, int64_t * );
327 #ifndef HAVE_MPC_MPCDEC_H
328 *pi64 = CLOCK_FREQ * p_sys->info.pcm_samples /
329 p_sys->info.sample_freq;
331 *pi64 = CLOCK_FREQ * (p_sys->info.samples -
332 p_sys->info.beg_silence) /
333 p_sys->info.sample_freq;
337 case DEMUX_GET_POSITION:
338 pf = (double*)va_arg( args, double * );
339 #ifndef HAVE_MPC_MPCDEC_H
340 if( p_sys->info.pcm_samples > 0 )
341 *pf = (double) p_sys->i_position /
342 (double)p_sys->info.pcm_samples;
344 if( p_sys->info.samples - p_sys->info.beg_silence > 0)
345 *pf = (double) p_sys->i_position /
346 (double)(p_sys->info.samples - p_sys->info.beg_silence);
353 pi64 = (int64_t*)va_arg( args, int64_t * );
354 *pi64 = CLOCK_FREQ * p_sys->i_position /
355 p_sys->info.sample_freq;
358 case DEMUX_SET_POSITION:
359 f = (double)va_arg( args, double );
360 #ifndef HAVE_MPC_MPCDEC_H
361 i64 = (int64_t)(f * p_sys->info.pcm_samples);
362 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
364 i64 = (int64_t)(f * (p_sys->info.samples -
365 p_sys->info.beg_silence));
366 if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
369 p_sys->i_position = i64;
375 i64 = (int64_t)va_arg( args, int64_t );
376 #ifndef HAVE_MPC_MPCDEC_H
377 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
379 if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
382 p_sys->i_position = i64;
392 #ifndef HAVE_MPC_MPCDEC_H
393 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
395 demux_t *p_demux = (demux_t*)p_private;
397 static mpc_int32_t ReaderRead( mpc_reader *p_private, void *dst, mpc_int32_t i_size )
399 demux_t *p_demux = (demux_t*)p_private->data;
401 return stream_Read( p_demux->s, dst, i_size );
404 #ifndef HAVE_MPC_MPCDEC_H
405 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
407 demux_t *p_demux = (demux_t*)p_private;
409 static mpc_bool_t ReaderSeek( mpc_reader *p_private, mpc_int32_t i_offset )
411 demux_t *p_demux = (demux_t*)p_private->data;
413 return !stream_Seek( p_demux->s, i_offset );
416 #ifndef HAVE_MPC_MPCDEC_H
417 static mpc_int32_t ReaderTell( void *p_private)
419 demux_t *p_demux = (demux_t*)p_private;
421 static mpc_int32_t ReaderTell( mpc_reader *p_private)
423 demux_t *p_demux = (demux_t*)p_private->data;
425 return stream_Tell( p_demux->s );
428 #ifndef HAVE_MPC_MPCDEC_H
429 static mpc_int32_t ReaderGetSize( void *p_private )
431 demux_t *p_demux = (demux_t*)p_private;
433 static mpc_int32_t ReaderGetSize( mpc_reader *p_private )
435 demux_t *p_demux = (demux_t*)p_private->data;
437 return stream_Size( p_demux->s );
440 #ifndef HAVE_MPC_MPCDEC_H
441 static mpc_bool_t ReaderCanSeek( void *p_private )
443 demux_t *p_demux = (demux_t*)p_private;
445 static mpc_bool_t ReaderCanSeek( mpc_reader *p_private )
447 demux_t *p_demux = (demux_t*)p_private->data;
451 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );