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( vlc_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
->obj
.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_SetPCR( p_demux
->out
, 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
;
321 return vlc_stream_vaControl( p_demux
->s
, i_query
, args
);
323 case DEMUX_HAS_UNSUPPORTED_META
:
324 pb_bool
= va_arg( args
, bool* );
328 case DEMUX_GET_LENGTH
:
329 pi64
= va_arg( args
, int64_t * );
330 #ifndef HAVE_MPC_MPCDEC_H
331 *pi64
= CLOCK_FREQ
* p_sys
->info
.pcm_samples
/
332 p_sys
->info
.sample_freq
;
334 *pi64
= CLOCK_FREQ
* (p_sys
->info
.samples
-
335 p_sys
->info
.beg_silence
) /
336 p_sys
->info
.sample_freq
;
340 case DEMUX_GET_POSITION
:
341 pf
= va_arg( args
, double * );
342 #ifndef HAVE_MPC_MPCDEC_H
343 if( p_sys
->info
.pcm_samples
> 0 )
344 *pf
= (double) p_sys
->i_position
/
345 (double)p_sys
->info
.pcm_samples
;
347 if( p_sys
->info
.samples
- p_sys
->info
.beg_silence
> 0)
348 *pf
= (double) p_sys
->i_position
/
349 (double)(p_sys
->info
.samples
- p_sys
->info
.beg_silence
);
356 pi64
= va_arg( args
, int64_t * );
357 *pi64
= CLOCK_FREQ
* p_sys
->i_position
/
358 p_sys
->info
.sample_freq
;
361 case DEMUX_SET_POSITION
:
362 f
= va_arg( args
, double );
363 #ifndef HAVE_MPC_MPCDEC_H
364 i64
= (int64_t)(f
* p_sys
->info
.pcm_samples
);
365 if( mpc_decoder_seek_sample( &p_sys
->decoder
, i64
) )
367 i64
= (int64_t)(f
* (p_sys
->info
.samples
-
368 p_sys
->info
.beg_silence
));
369 if( mpc_demux_seek_sample( p_sys
->decoder
, i64
) == MPC_STATUS_OK
)
372 p_sys
->i_position
= i64
;
378 i64
= va_arg( args
, int64_t );
379 #ifndef HAVE_MPC_MPCDEC_H
380 if( mpc_decoder_seek_sample( &p_sys
->decoder
, i64
) )
382 if( mpc_demux_seek_sample( p_sys
->decoder
, i64
) == MPC_STATUS_OK
)
385 p_sys
->i_position
= i64
;
395 #ifndef HAVE_MPC_MPCDEC_H
396 static mpc_int32_t
ReaderRead( void *p_private
, void *dst
, mpc_int32_t i_size
)
398 demux_t
*p_demux
= (demux_t
*)p_private
;
400 static mpc_int32_t
ReaderRead( mpc_reader
*p_private
, void *dst
, mpc_int32_t i_size
)
402 demux_t
*p_demux
= (demux_t
*)p_private
->data
;
404 return vlc_stream_Read( p_demux
->s
, dst
, i_size
);
407 #ifndef HAVE_MPC_MPCDEC_H
408 static mpc_bool_t
ReaderSeek( void *p_private
, mpc_int32_t i_offset
)
410 demux_t
*p_demux
= (demux_t
*)p_private
;
412 static mpc_bool_t
ReaderSeek( mpc_reader
*p_private
, mpc_int32_t i_offset
)
414 demux_t
*p_demux
= (demux_t
*)p_private
->data
;
416 return !vlc_stream_Seek( p_demux
->s
, i_offset
);
419 #ifndef HAVE_MPC_MPCDEC_H
420 static mpc_int32_t
ReaderTell( void *p_private
)
422 demux_t
*p_demux
= (demux_t
*)p_private
;
424 static mpc_int32_t
ReaderTell( mpc_reader
*p_private
)
426 demux_t
*p_demux
= (demux_t
*)p_private
->data
;
428 return vlc_stream_Tell( p_demux
->s
);
431 #ifndef HAVE_MPC_MPCDEC_H
432 static mpc_int32_t
ReaderGetSize( void *p_private
)
434 demux_t
*p_demux
= (demux_t
*)p_private
;
436 static mpc_int32_t
ReaderGetSize( mpc_reader
*p_private
)
438 demux_t
*p_demux
= (demux_t
*)p_private
->data
;
440 return stream_Size( p_demux
->s
);
443 #ifndef HAVE_MPC_MPCDEC_H
444 static mpc_bool_t
ReaderCanSeek( void *p_private
)
446 demux_t
*p_demux
= (demux_t
*)p_private
;
448 static mpc_bool_t
ReaderCanSeek( mpc_reader
*p_private
)
450 demux_t
*p_demux
= (demux_t
*)p_private
->data
;
454 vlc_stream_Control( p_demux
->s
, STREAM_CAN_SEEK
, &b_canseek
);