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 #include <mpcdec/mpcdec.h>
41 * - test stream version 4..6
42 * - test fixed float version
46 * It is done the ugly way (the demux does the decode stage... but it works
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Open ( vlc_object_t * );
53 static void Close ( vlc_object_t * );
56 set_category( CAT_INPUT )
57 set_subcategory( SUBCAT_INPUT_DEMUX )
58 set_description( N_("MusePack demuxer") )
59 set_capability( "demux", 145 )
61 set_callbacks( Open, Close )
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Demux ( demux_t * );
69 static int Control( demux_t *, int, va_list );
85 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
86 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset );
87 static mpc_int32_t ReaderTell( void *p_private);
88 static mpc_int32_t ReaderGetSize( void *p_private );
89 static mpc_bool_t ReaderCanSeek( void *p_private );
91 /*****************************************************************************
92 * Open: initializes ES structures
93 *****************************************************************************/
94 static int Open( vlc_object_t * p_this )
96 demux_t *p_demux = (demux_t*)p_this;
99 const uint8_t *p_peek;
101 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
104 if( memcmp( p_peek, "MP+", 3 ) )
106 /* for v4..6 we check extension file */
107 const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
109 if( i_version < 4 || i_version > 6 )
112 if( !p_demux->b_force )
114 /* Check file name extension */
115 if( !demux_IsPathExtension( p_demux, ".mpc" ) &&
116 !demux_IsPathExtension( p_demux, ".mp+" ) &&
117 !demux_IsPathExtension( p_demux, ".mpp" ) )
123 p_sys = calloc( 1, sizeof( *p_sys ) );
127 p_sys->i_position = 0;
129 p_sys->reader.read = ReaderRead;
130 p_sys->reader.seek = ReaderSeek;
131 p_sys->reader.tell = ReaderTell;
132 p_sys->reader.get_size = ReaderGetSize;
133 p_sys->reader.canseek = ReaderCanSeek;
134 p_sys->reader.data = p_demux;
137 mpc_streaminfo_init( &p_sys->info );
138 if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
142 mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
143 if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
146 /* Fill p_demux fields */
147 p_demux->pf_demux = Demux;
148 p_demux->pf_control = Control;
149 p_demux->p_sys = p_sys;
152 #ifndef MPC_FIXED_POINT
153 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_FL32 );
155 # ifdef WORDS_BIGENDIAN
156 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32B );
158 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32L );
161 fmt.audio.i_channels = p_sys->info.channels;
162 fmt.audio.i_rate = p_sys->info.sample_freq;
163 fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
164 fmt.audio.i_bitspersample = 32;
165 fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
166 fmt.audio.i_bitspersample;
167 if( p_sys->info.peak_title > 0 )
169 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
170 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.peak_title / 32767.0;
171 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
172 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.gain_title / 100.0;
174 if( p_sys->info.peak_album > 0 )
176 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
177 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.peak_album / 32767.0;
178 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
179 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.gain_album / 100.0;
182 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
193 /*****************************************************************************
194 * Close: frees unused data
195 *****************************************************************************/
196 static void Close( vlc_object_t * p_this )
198 demux_t *p_demux = (demux_t*)p_this;
199 demux_sys_t *p_sys = p_demux->p_sys;
204 /*****************************************************************************
206 *****************************************************************************
207 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
208 *****************************************************************************/
209 static int Demux( demux_t *p_demux )
211 demux_sys_t *p_sys = p_demux->p_sys;
215 p_data = block_New( p_demux,
216 MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
220 i_ret = mpc_decoder_decode( &p_sys->decoder,
221 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
225 block_Release( p_data );
226 return i_ret < 0 ? -1 : 0;
230 p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
231 p_data->i_dts = p_data->i_pts =
232 1 + INT64_C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
234 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
236 es_out_Send( p_demux->out, p_sys->p_es, p_data );
239 p_sys->i_position += i_ret;
244 /*****************************************************************************
246 *****************************************************************************/
247 static int Control( demux_t *p_demux, int i_query, va_list args )
249 demux_sys_t *p_sys = p_demux->p_sys;
256 case DEMUX_HAS_UNSUPPORTED_META:
257 pb_bool = (bool*)va_arg( args, bool* );
261 case DEMUX_GET_LENGTH:
262 pi64 = (int64_t*)va_arg( args, int64_t * );
263 *pi64 = INT64_C(1000000) * p_sys->info.pcm_samples /
264 p_sys->info.sample_freq;
267 case DEMUX_GET_POSITION:
268 pf = (double*)va_arg( args, double * );
269 if( p_sys->info.pcm_samples > 0 )
270 *pf = (double) p_sys->i_position /
271 (double)p_sys->info.pcm_samples;
277 pi64 = (int64_t*)va_arg( args, int64_t * );
278 *pi64 = INT64_C(1000000) * p_sys->i_position /
279 p_sys->info.sample_freq;
282 case DEMUX_SET_POSITION:
283 f = (double)va_arg( args, double );
284 i64 = (int64_t)(f * p_sys->info.pcm_samples);
285 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
287 p_sys->i_position = i64;
293 i64 = (int64_t)va_arg( args, int64_t );
294 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
296 p_sys->i_position = i64;
306 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
308 demux_t *p_demux = (demux_t*)p_private;
309 return stream_Read( p_demux->s, dst, i_size );
312 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
314 demux_t *p_demux = (demux_t*)p_private;
315 return !stream_Seek( p_demux->s, i_offset );
318 static mpc_int32_t ReaderTell( void *p_private)
320 demux_t *p_demux = (demux_t*)p_private;
321 return stream_Tell( p_demux->s );
324 static mpc_int32_t ReaderGetSize( void *p_private )
326 demux_t *p_demux = (demux_t*)p_private;
327 return stream_Size( p_demux->s );
330 static mpc_bool_t ReaderCanSeek( void *p_private )
332 demux_t *p_demux = (demux_t*)p_private;
335 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );