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 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
34 #include <vlc_input.h>
35 #include <vlc_codec.h>
38 #ifdef HAVE_MPC_MPCDEC_H
39 #include <mpc/mpcdec.h>
41 #include <mpcdec/mpcdec.h>
45 * - test stream version 4..6
46 * - test fixed float version
50 * It is done the ugly way (the demux does the decode stage... but it works
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Open ( vlc_object_t * );
57 static void Close ( vlc_object_t * );
60 set_category( CAT_INPUT )
61 set_subcategory( SUBCAT_INPUT_DEMUX )
62 set_description( N_("MusePack demuxer") )
63 set_capability( "demux", 145 )
65 set_callbacks( Open, Close )
69 /*****************************************************************************
71 *****************************************************************************/
72 static int Demux ( demux_t * );
73 static int Control( demux_t *, int, va_list );
81 #ifndef HAVE_MPC_MPCDEC_H
93 #ifndef HAVE_MPC_MPCDEC_H
94 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
95 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset );
96 static mpc_int32_t ReaderTell( void *p_private);
97 static mpc_int32_t ReaderGetSize( void *p_private );
98 static mpc_bool_t ReaderCanSeek( void *p_private );
100 static mpc_int32_t ReaderRead( mpc_reader *p_private, void *dst, mpc_int32_t i_size );
101 static mpc_bool_t ReaderSeek( mpc_reader *p_private, mpc_int32_t i_offset );
102 static mpc_int32_t ReaderTell( mpc_reader *p_private);
103 static mpc_int32_t ReaderGetSize( mpc_reader *p_private );
104 static mpc_bool_t ReaderCanSeek( mpc_reader *p_private );
107 /*****************************************************************************
108 * Open: initializes ES structures
109 *****************************************************************************/
110 static int Open( vlc_object_t * p_this )
112 demux_t *p_demux = (demux_t*)p_this;
115 const uint8_t *p_peek;
117 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
120 if( memcmp( p_peek, "MP+", 3 )
121 #ifdef HAVE_MPC_MPCDEC_H
123 && memcmp( p_peek, "MPCK", 4 )
127 /* for v4..6 we check extension file */
128 const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
129 if( i_version < 4 || i_version > 6 )
132 if( !p_demux->b_force )
134 /* Check file name extension */
135 if( !demux_IsPathExtension( p_demux, ".mpc" ) &&
136 !demux_IsPathExtension( p_demux, ".mp+" ) &&
137 !demux_IsPathExtension( p_demux, ".mpp" ) )
143 p_sys = calloc( 1, sizeof( *p_sys ) );
147 p_sys->i_position = 0;
149 p_sys->reader.read = ReaderRead;
150 p_sys->reader.seek = ReaderSeek;
151 p_sys->reader.tell = ReaderTell;
152 p_sys->reader.get_size = ReaderGetSize;
153 p_sys->reader.canseek = ReaderCanSeek;
154 p_sys->reader.data = p_demux;
156 #ifndef HAVE_MPC_MPCDEC_H
158 mpc_streaminfo_init( &p_sys->info );
159 if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
163 mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
164 if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
167 p_sys->decoder = mpc_demux_init( &p_sys->reader );
168 if( !p_sys->decoder )
171 mpc_demux_get_info( p_sys->decoder, &p_sys->info );
174 /* Fill p_demux fields */
175 p_demux->pf_demux = Demux;
176 p_demux->pf_control = Control;
177 p_demux->p_sys = p_sys;
180 #ifndef MPC_FIXED_POINT
181 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_FL32 );
183 # ifdef WORDS_BIGENDIAN
184 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32B );
186 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32L );
189 fmt.audio.i_channels = p_sys->info.channels;
190 fmt.audio.i_rate = p_sys->info.sample_freq;
191 fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
192 fmt.audio.i_bitspersample = 32;
193 fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
194 fmt.audio.i_bitspersample;
195 if( p_sys->info.peak_title > 0 )
197 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
198 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.peak_title / 32767.0;
199 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
200 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.gain_title / 100.0;
202 if( p_sys->info.peak_album > 0 )
204 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
205 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.peak_album / 32767.0;
206 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
207 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.gain_album / 100.0;
210 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
221 /*****************************************************************************
222 * Close: frees unused data
223 *****************************************************************************/
224 static void Close( vlc_object_t * p_this )
226 demux_t *p_demux = (demux_t*)p_this;
227 demux_sys_t *p_sys = p_demux->p_sys;
229 #ifdef HAVE_MPC_MPCDEC_H
231 mpc_demux_exit( p_sys->decoder );
236 /*****************************************************************************
238 *****************************************************************************
239 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
240 *****************************************************************************/
241 static int Demux( demux_t *p_demux )
243 demux_sys_t *p_sys = p_demux->p_sys;
246 #ifdef HAVE_MPC_MPCDEC_H
247 mpc_frame_info frame;
250 p_data = block_Alloc( MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
254 #ifndef HAVE_MPC_MPCDEC_H
255 i_ret = mpc_decoder_decode( &p_sys->decoder,
256 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
260 block_Release( p_data );
261 return i_ret < 0 ? -1 : 0;
264 frame.buffer = (MPC_SAMPLE_FORMAT*)p_data->p_buffer;
265 err = mpc_demux_decode( p_sys->decoder, &frame );
266 if( err != MPC_STATUS_OK )
268 block_Release( p_data );
271 else if( frame.bits == -1 )
273 block_Release( p_data );
277 i_ret = frame.samples;
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 VLC_TS_0 + INT64_C(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;
307 case DEMUX_HAS_UNSUPPORTED_META:
308 pb_bool = (bool*)va_arg( args, bool* );
312 case DEMUX_GET_LENGTH:
313 pi64 = (int64_t*)va_arg( args, int64_t * );
314 #ifndef HAVE_MPC_MPCDEC_H
315 *pi64 = INT64_C(1000000) * p_sys->info.pcm_samples /
316 p_sys->info.sample_freq;
318 *pi64 = INT64_C(1000000) * (p_sys->info.samples -
319 p_sys->info.beg_silence) /
320 p_sys->info.sample_freq;
324 case DEMUX_GET_POSITION:
325 pf = (double*)va_arg( args, double * );
326 #ifndef HAVE_MPC_MPCDEC_H
327 if( p_sys->info.pcm_samples > 0 )
328 *pf = (double) p_sys->i_position /
329 (double)p_sys->info.pcm_samples;
331 if( p_sys->info.samples - p_sys->info.beg_silence > 0)
332 *pf = (double) p_sys->i_position /
333 (double)(p_sys->info.samples - p_sys->info.beg_silence);
340 pi64 = (int64_t*)va_arg( args, int64_t * );
341 *pi64 = INT64_C(1000000) * p_sys->i_position /
342 p_sys->info.sample_freq;
345 case DEMUX_SET_POSITION:
346 f = (double)va_arg( args, double );
347 #ifndef HAVE_MPC_MPCDEC_H
348 i64 = (int64_t)(f * p_sys->info.pcm_samples);
349 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
351 i64 = (int64_t)(f * (p_sys->info.samples -
352 p_sys->info.beg_silence));
353 if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
356 p_sys->i_position = i64;
362 i64 = (int64_t)va_arg( args, int64_t );
363 #ifndef HAVE_MPC_MPCDEC_H
364 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
366 if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
369 p_sys->i_position = i64;
379 #ifndef HAVE_MPC_MPCDEC_H
380 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
382 demux_t *p_demux = (demux_t*)p_private;
384 static mpc_int32_t ReaderRead( mpc_reader *p_private, void *dst, mpc_int32_t i_size )
386 demux_t *p_demux = (demux_t*)p_private->data;
388 return stream_Read( p_demux->s, dst, i_size );
391 #ifndef HAVE_MPC_MPCDEC_H
392 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
394 demux_t *p_demux = (demux_t*)p_private;
396 static mpc_bool_t ReaderSeek( mpc_reader *p_private, mpc_int32_t i_offset )
398 demux_t *p_demux = (demux_t*)p_private->data;
400 return !stream_Seek( p_demux->s, i_offset );
403 #ifndef HAVE_MPC_MPCDEC_H
404 static mpc_int32_t ReaderTell( void *p_private)
406 demux_t *p_demux = (demux_t*)p_private;
408 static mpc_int32_t ReaderTell( mpc_reader *p_private)
410 demux_t *p_demux = (demux_t*)p_private->data;
412 return stream_Tell( p_demux->s );
415 #ifndef HAVE_MPC_MPCDEC_H
416 static mpc_int32_t ReaderGetSize( void *p_private )
418 demux_t *p_demux = (demux_t*)p_private;
420 static mpc_int32_t ReaderGetSize( mpc_reader *p_private )
422 demux_t *p_demux = (demux_t*)p_private->data;
424 return stream_Size( p_demux->s );
427 #ifndef HAVE_MPC_MPCDEC_H
428 static mpc_bool_t ReaderCanSeek( void *p_private )
430 demux_t *p_demux = (demux_t*)p_private;
432 static mpc_bool_t ReaderCanSeek( mpc_reader *p_private )
434 demux_t *p_demux = (demux_t*)p_private->data;
438 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );