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 *****************************************************************************/
32 #include <vlc_demux.h>
33 #include <vlc_input.h>
34 #include <vlc_codec.h>
37 #include <mpcdec/mpcdec.h>
40 * - test stream version 4..6
41 * - test fixed float version
45 * It is done the ugly way (the demux does the decode stage... but it works
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Open ( vlc_object_t * );
52 static void Close ( vlc_object_t * );
55 set_category( CAT_INPUT );
56 set_subcategory( SUBCAT_INPUT_DEMUX );
57 set_description( _("MusePack demuxer") );
58 set_capability( "demux2", 145 );
60 set_callbacks( Open, Close );
61 add_shortcut( "mpc" );
64 /*****************************************************************************
66 *****************************************************************************/
67 static int Demux ( demux_t * );
68 static int Control( demux_t *, int, va_list );
84 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
85 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset );
86 mpc_int32_t ReaderTell( void *p_private);
87 mpc_int32_t ReaderGetSize( void *p_private );
88 mpc_bool_t ReaderCanSeek( void *p_private );
90 /*****************************************************************************
91 * Open: initializes ES structures
92 *****************************************************************************/
93 static int Open( vlc_object_t * p_this )
95 demux_t *p_demux = (demux_t*)p_this;
98 const uint8_t *p_peek;
100 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
103 if( memcmp( p_peek, "MP+", 3 ) )
105 /* for v4..6 we check extension file */
106 const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
108 if( i_version < 4 || i_version > 6 )
111 if( !p_demux->b_force )
113 /* Check file name extension */
114 if( !demux2_IsPathExtension( p_demux, ".mpc" ) &&
115 !demux2_IsPathExtension( p_demux, ".mp+" ) &&
116 !demux2_IsPathExtension( p_demux, ".mpp" ) )
122 p_sys = malloc( sizeof( demux_sys_t ) );
123 memset( p_sys, 0, sizeof(demux_sys_t) );
125 p_sys->i_position = 0;
127 p_sys->reader.read = ReaderRead;
128 p_sys->reader.seek = ReaderSeek;
129 p_sys->reader.tell = ReaderTell;
130 p_sys->reader.get_size = ReaderGetSize;
131 p_sys->reader.canseek = ReaderCanSeek;
132 p_sys->reader.data = p_demux;
135 mpc_streaminfo_init( &p_sys->info );
136 if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
144 mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
145 if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
152 /* Fill p_demux fields */
153 p_demux->pf_demux = Demux;
154 p_demux->pf_control = Control;
155 p_demux->p_sys = p_sys;
158 #ifndef MPC_FIXED_POINT
159 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', '3', '2' ) );
161 # ifdef WORDS_BIGENDIAN
162 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'b' ) );
164 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'l' ) );
167 fmt.audio.i_channels = p_sys->info.channels;
168 fmt.audio.i_rate = p_sys->info.sample_freq;
169 fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
170 fmt.audio.i_bitspersample = 32;
171 fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
172 fmt.audio.i_bitspersample;
173 if( p_sys->info.peak_title > 0 )
175 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
176 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.peak_title / 32767.0;
177 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
178 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.gain_title / 100.0;
180 if( p_sys->info.peak_album > 0 )
182 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
183 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.peak_album / 32767.0;
184 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
185 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.gain_album / 100.0;
188 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) );
217 i_ret = mpc_decoder_decode( &p_sys->decoder,
218 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
222 block_Release( p_data );
223 return i_ret < 0 ? -1 : 0;
227 p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
228 p_data->i_dts = p_data->i_pts =
229 1 + I64C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
231 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
233 es_out_Send( p_demux->out, p_sys->p_es, p_data );
236 p_sys->i_position += i_ret;
241 /*****************************************************************************
243 *****************************************************************************/
244 static int Control( demux_t *p_demux, int i_query, va_list args )
246 demux_sys_t *p_sys = p_demux->p_sys;
253 case DEMUX_HAS_UNSUPPORTED_META:
254 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
258 case DEMUX_GET_LENGTH:
259 pi64 = (int64_t*)va_arg( args, int64_t * );
260 *pi64 = I64C(1000000) * p_sys->info.pcm_samples /
261 p_sys->info.sample_freq;
264 case DEMUX_GET_POSITION:
265 pf = (double*)va_arg( args, double * );
266 if( p_sys->info.pcm_samples > 0 )
267 *pf = (double) p_sys->i_position /
268 (double)p_sys->info.pcm_samples;
274 pi64 = (int64_t*)va_arg( args, int64_t * );
275 *pi64 = I64C(1000000) * p_sys->i_position /
276 p_sys->info.sample_freq;
279 case DEMUX_SET_POSITION:
280 f = (double)va_arg( args, double );
281 i64 = (int64_t)(f * p_sys->info.pcm_samples);
282 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
284 p_sys->i_position = i64;
290 i64 = (int64_t)va_arg( args, int64_t );
291 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
293 p_sys->i_position = i64;
303 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
305 demux_t *p_demux = (demux_t*)p_private;
306 return stream_Read( p_demux->s, dst, i_size );
309 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
311 demux_t *p_demux = (demux_t*)p_private;
312 return !stream_Seek( p_demux->s, i_offset );
315 mpc_int32_t ReaderTell( void *p_private)
317 demux_t *p_demux = (demux_t*)p_private;
318 return stream_Tell( p_demux->s );
321 mpc_int32_t ReaderGetSize( void *p_private )
323 demux_t *p_demux = (demux_t*)p_private;
324 return stream_Size( p_demux->s );
327 mpc_bool_t ReaderCanSeek( void *p_private )
329 demux_t *p_demux = (demux_t*)p_private;
330 vlc_bool_t b_canseek;
332 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );