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_New( p_demux
,
251 MPC_DECODER_BUFFER_LENGTH
*sizeof(MPC_SAMPLE_FORMAT
) );
255 #ifndef HAVE_MPC_MPCDEC_H
256 i_ret
= mpc_decoder_decode( &p_sys
->decoder
,
257 (MPC_SAMPLE_FORMAT
*)p_data
->p_buffer
,
261 block_Release( p_data
);
262 return i_ret
< 0 ? -1 : 0;
265 frame
.buffer
= (MPC_SAMPLE_FORMAT
*)p_data
->p_buffer
;
266 err
= mpc_demux_decode( p_sys
->decoder
, &frame
);
267 if( err
!= MPC_STATUS_OK
)
269 block_Release( p_data
);
272 else if( frame
.bits
== -1 )
274 block_Release( p_data
);
278 i_ret
= frame
.samples
;
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 VLC_TS_0
+ INT64_C(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
;
308 case DEMUX_HAS_UNSUPPORTED_META
:
309 pb_bool
= (bool*)va_arg( args
, bool* );
313 case DEMUX_GET_LENGTH
:
314 pi64
= (int64_t*)va_arg( args
, int64_t * );
315 #ifndef HAVE_MPC_MPCDEC_H
316 *pi64
= INT64_C(1000000) * p_sys
->info
.pcm_samples
/
317 p_sys
->info
.sample_freq
;
319 *pi64
= INT64_C(1000000) * (p_sys
->info
.samples
-
320 p_sys
->info
.beg_silence
) /
321 p_sys
->info
.sample_freq
;
325 case DEMUX_GET_POSITION
:
326 pf
= (double*)va_arg( args
, double * );
327 #ifndef HAVE_MPC_MPCDEC_H
328 if( p_sys
->info
.pcm_samples
> 0 )
329 *pf
= (double) p_sys
->i_position
/
330 (double)p_sys
->info
.pcm_samples
;
332 if( p_sys
->info
.samples
- p_sys
->info
.beg_silence
> 0)
333 *pf
= (double) p_sys
->i_position
/
334 (double)(p_sys
->info
.samples
- p_sys
->info
.beg_silence
);
341 pi64
= (int64_t*)va_arg( args
, int64_t * );
342 *pi64
= INT64_C(1000000) * p_sys
->i_position
/
343 p_sys
->info
.sample_freq
;
346 case DEMUX_SET_POSITION
:
347 f
= (double)va_arg( args
, double );
348 #ifndef HAVE_MPC_MPCDEC_H
349 i64
= (int64_t)(f
* p_sys
->info
.pcm_samples
);
350 if( mpc_decoder_seek_sample( &p_sys
->decoder
, i64
) )
352 i64
= (int64_t)(f
* (p_sys
->info
.samples
-
353 p_sys
->info
.beg_silence
));
354 if( mpc_demux_seek_sample( p_sys
->decoder
, i64
) == MPC_STATUS_OK
)
357 p_sys
->i_position
= i64
;
363 i64
= (int64_t)va_arg( args
, int64_t );
364 #ifndef HAVE_MPC_MPCDEC_H
365 if( mpc_decoder_seek_sample( &p_sys
->decoder
, i64
) )
367 if( mpc_demux_seek_sample( p_sys
->decoder
, i64
) == MPC_STATUS_OK
)
370 p_sys
->i_position
= i64
;
380 #ifndef HAVE_MPC_MPCDEC_H
381 static mpc_int32_t
ReaderRead( void *p_private
, void *dst
, mpc_int32_t i_size
)
383 demux_t
*p_demux
= (demux_t
*)p_private
;
385 static mpc_int32_t
ReaderRead( mpc_reader
*p_private
, void *dst
, mpc_int32_t i_size
)
387 demux_t
*p_demux
= (demux_t
*)p_private
->data
;
389 return stream_Read( p_demux
->s
, dst
, i_size
);
392 #ifndef HAVE_MPC_MPCDEC_H
393 static mpc_bool_t
ReaderSeek( void *p_private
, mpc_int32_t i_offset
)
395 demux_t
*p_demux
= (demux_t
*)p_private
;
397 static mpc_bool_t
ReaderSeek( mpc_reader
*p_private
, mpc_int32_t i_offset
)
399 demux_t
*p_demux
= (demux_t
*)p_private
->data
;
401 return !stream_Seek( p_demux
->s
, i_offset
);
404 #ifndef HAVE_MPC_MPCDEC_H
405 static mpc_int32_t
ReaderTell( void *p_private
)
407 demux_t
*p_demux
= (demux_t
*)p_private
;
409 static mpc_int32_t
ReaderTell( mpc_reader
*p_private
)
411 demux_t
*p_demux
= (demux_t
*)p_private
->data
;
413 return stream_Tell( p_demux
->s
);
416 #ifndef HAVE_MPC_MPCDEC_H
417 static mpc_int32_t
ReaderGetSize( void *p_private
)
419 demux_t
*p_demux
= (demux_t
*)p_private
;
421 static mpc_int32_t
ReaderGetSize( mpc_reader
*p_private
)
423 demux_t
*p_demux
= (demux_t
*)p_private
->data
;
425 return stream_Size( p_demux
->s
);
428 #ifndef HAVE_MPC_MPCDEC_H
429 static mpc_bool_t
ReaderCanSeek( void *p_private
)
431 demux_t
*p_demux
= (demux_t
*)p_private
;
433 static mpc_bool_t
ReaderCanSeek( mpc_reader
*p_private
)
435 demux_t
*p_demux
= (demux_t
*)p_private
->data
;
439 stream_Control( p_demux
->s
, STREAM_CAN_SEEK
, &b_canseek
);