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 *****************************************************************************/
28 #include <vlc_demux.h>
29 #include <vlc_input.h>
30 #include <vlc_codec.h>
33 #include <mpcdec/mpcdec.h>
36 * - test stream version 4..6
37 * - test fixed float version
41 * It is done the ugly way (the demux does the decode stage... but it works
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
51 set_category( CAT_INPUT );
52 set_subcategory( SUBCAT_INPUT_DEMUX );
53 set_description( _("MusePack demuxer") );
54 set_capability( "demux2", 145 );
56 set_callbacks( Open, Close );
57 add_shortcut( "mpc" );
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Demux ( demux_t * );
64 static int Control( demux_t *, int, va_list );
80 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
81 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset );
82 mpc_int32_t ReaderTell( void *p_private);
83 mpc_int32_t ReaderGetSize( void *p_private );
84 mpc_bool_t ReaderCanSeek( void *p_private );
86 /*****************************************************************************
87 * Open: initializes ES structures
88 *****************************************************************************/
89 static int Open( vlc_object_t * p_this )
91 demux_t *p_demux = (demux_t*)p_this;
94 const uint8_t *p_peek;
96 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
99 if( memcmp( p_peek, "MP+", 3 ) )
101 /* for v4..6 we check extension file */
102 const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
104 if( i_version < 4 || i_version > 6 )
107 if( !p_demux->b_force )
109 /* Check file name extension */
110 if( !demux2_IsPathExtension( p_demux, ".mpc" ) &&
111 !demux2_IsPathExtension( p_demux, ".mp+" ) &&
112 !demux2_IsPathExtension( p_demux, ".mpp" ) )
118 p_sys = malloc( sizeof( demux_sys_t ) );
119 memset( p_sys, 0, sizeof(demux_sys_t) );
121 p_sys->i_position = 0;
123 p_sys->reader.read = ReaderRead;
124 p_sys->reader.seek = ReaderSeek;
125 p_sys->reader.tell = ReaderTell;
126 p_sys->reader.get_size = ReaderGetSize;
127 p_sys->reader.canseek = ReaderCanSeek;
128 p_sys->reader.data = p_demux;
131 mpc_streaminfo_init( &p_sys->info );
132 if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
140 mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
141 if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
148 /* Fill p_demux fields */
149 p_demux->pf_demux = Demux;
150 p_demux->pf_control = Control;
151 p_demux->p_sys = p_sys;
154 #ifndef MPC_FIXED_POINT
155 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', '3', '2' ) );
157 # ifdef WORDS_BIGENDIAN
158 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'b' ) );
160 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'l' ) );
163 fmt.audio.i_channels = p_sys->info.channels;
164 fmt.audio.i_rate = p_sys->info.sample_freq;
165 fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
166 fmt.audio.i_bitspersample = 32;
167 fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
168 fmt.audio.i_bitspersample;
169 if( p_sys->info.peak_title > 0 )
171 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
172 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.peak_title / 32767.0;
173 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
174 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.gain_title / 100.0;
176 if( p_sys->info.peak_album > 0 )
178 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
179 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.peak_album / 32767.0;
180 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
181 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.gain_album / 100.0;
184 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
189 /*****************************************************************************
190 * Close: frees unused data
191 *****************************************************************************/
192 static void Close( vlc_object_t * p_this )
194 demux_t *p_demux = (demux_t*)p_this;
195 demux_sys_t *p_sys = p_demux->p_sys;
200 /*****************************************************************************
202 *****************************************************************************
203 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
204 *****************************************************************************/
205 static int Demux( demux_t *p_demux )
207 demux_sys_t *p_sys = p_demux->p_sys;
211 p_data = block_New( p_demux,
212 MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
213 i_ret = mpc_decoder_decode( &p_sys->decoder,
214 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
218 block_Release( p_data );
219 return i_ret < 0 ? -1 : 0;
223 p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
224 p_data->i_dts = p_data->i_pts =
225 1 + I64C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
227 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
229 es_out_Send( p_demux->out, p_sys->p_es, p_data );
232 p_sys->i_position += i_ret;
237 /*****************************************************************************
239 *****************************************************************************/
240 static int Control( demux_t *p_demux, int i_query, va_list args )
242 demux_sys_t *p_sys = p_demux->p_sys;
249 case DEMUX_HAS_UNSUPPORTED_META:
250 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
254 case DEMUX_GET_LENGTH:
255 pi64 = (int64_t*)va_arg( args, int64_t * );
256 *pi64 = I64C(1000000) * p_sys->info.pcm_samples /
257 p_sys->info.sample_freq;
260 case DEMUX_GET_POSITION:
261 pf = (double*)va_arg( args, double * );
262 if( p_sys->info.pcm_samples > 0 )
263 *pf = (double) p_sys->i_position /
264 (double)p_sys->info.pcm_samples;
270 pi64 = (int64_t*)va_arg( args, int64_t * );
271 *pi64 = I64C(1000000) * p_sys->i_position /
272 p_sys->info.sample_freq;
275 case DEMUX_SET_POSITION:
276 f = (double)va_arg( args, double );
277 i64 = (int64_t)(f * p_sys->info.pcm_samples);
278 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
280 p_sys->i_position = i64;
286 i64 = (int64_t)va_arg( args, int64_t );
287 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
289 p_sys->i_position = i64;
299 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
301 demux_t *p_demux = (demux_t*)p_private;
302 return stream_Read( p_demux->s, dst, i_size );
305 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
307 demux_t *p_demux = (demux_t*)p_private;
308 return !stream_Seek( p_demux->s, i_offset );
311 mpc_int32_t ReaderTell( void *p_private)
313 demux_t *p_demux = (demux_t*)p_private;
314 return stream_Tell( p_demux->s );
317 mpc_int32_t ReaderGetSize( void *p_private )
319 demux_t *p_demux = (demux_t*)p_private;
320 return stream_Size( p_demux->s );
323 mpc_bool_t ReaderCanSeek( void *p_private )
325 demux_t *p_demux = (demux_t*)p_private;
326 vlc_bool_t b_canseek;
328 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );