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 );
62 add_shortcut( "mpc" );
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Demux ( demux_t * );
69 static int Control( demux_t *, int, va_list );
85 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
86 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset );
87 mpc_int32_t ReaderTell( void *p_private);
88 mpc_int32_t ReaderGetSize( void *p_private );
89 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 = malloc( sizeof( demux_sys_t ) );
124 memset( p_sys, 0, sizeof(demux_sys_t) );
126 p_sys->i_position = 0;
128 p_sys->reader.read = ReaderRead;
129 p_sys->reader.seek = ReaderSeek;
130 p_sys->reader.tell = ReaderTell;
131 p_sys->reader.get_size = ReaderGetSize;
132 p_sys->reader.canseek = ReaderCanSeek;
133 p_sys->reader.data = p_demux;
136 mpc_streaminfo_init( &p_sys->info );
137 if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
145 mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
146 if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
153 /* Fill p_demux fields */
154 p_demux->pf_demux = Demux;
155 p_demux->pf_control = Control;
156 p_demux->p_sys = p_sys;
159 #ifndef MPC_FIXED_POINT
160 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', '3', '2' ) );
162 # ifdef WORDS_BIGENDIAN
163 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'b' ) );
165 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'l' ) );
168 fmt.audio.i_channels = p_sys->info.channels;
169 fmt.audio.i_rate = p_sys->info.sample_freq;
170 fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
171 fmt.audio.i_bitspersample = 32;
172 fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
173 fmt.audio.i_bitspersample;
174 if( p_sys->info.peak_title > 0 )
176 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
177 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.peak_title / 32767.0;
178 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
179 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.gain_title / 100.0;
181 if( p_sys->info.peak_album > 0 )
183 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
184 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.peak_album / 32767.0;
185 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
186 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.gain_album / 100.0;
189 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
194 /*****************************************************************************
195 * Close: frees unused data
196 *****************************************************************************/
197 static void Close( vlc_object_t * p_this )
199 demux_t *p_demux = (demux_t*)p_this;
200 demux_sys_t *p_sys = p_demux->p_sys;
205 /*****************************************************************************
207 *****************************************************************************
208 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
209 *****************************************************************************/
210 static int Demux( demux_t *p_demux )
212 demux_sys_t *p_sys = p_demux->p_sys;
216 p_data = block_New( p_demux,
217 MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
218 i_ret = mpc_decoder_decode( &p_sys->decoder,
219 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
223 block_Release( p_data );
224 return i_ret < 0 ? -1 : 0;
228 p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
229 p_data->i_dts = p_data->i_pts =
230 1 + INT64_C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
232 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
234 es_out_Send( p_demux->out, p_sys->p_es, p_data );
237 p_sys->i_position += i_ret;
242 /*****************************************************************************
244 *****************************************************************************/
245 static int Control( demux_t *p_demux, int i_query, va_list args )
247 demux_sys_t *p_sys = p_demux->p_sys;
254 case DEMUX_HAS_UNSUPPORTED_META:
255 pb_bool = (bool*)va_arg( args, bool* );
259 case DEMUX_GET_LENGTH:
260 pi64 = (int64_t*)va_arg( args, int64_t * );
261 *pi64 = INT64_C(1000000) * p_sys->info.pcm_samples /
262 p_sys->info.sample_freq;
265 case DEMUX_GET_POSITION:
266 pf = (double*)va_arg( args, double * );
267 if( p_sys->info.pcm_samples > 0 )
268 *pf = (double) p_sys->i_position /
269 (double)p_sys->info.pcm_samples;
275 pi64 = (int64_t*)va_arg( args, int64_t * );
276 *pi64 = INT64_C(1000000) * p_sys->i_position /
277 p_sys->info.sample_freq;
280 case DEMUX_SET_POSITION:
281 f = (double)va_arg( args, double );
282 i64 = (int64_t)(f * p_sys->info.pcm_samples);
283 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
285 p_sys->i_position = i64;
291 i64 = (int64_t)va_arg( args, int64_t );
292 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
294 p_sys->i_position = i64;
304 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
306 demux_t *p_demux = (demux_t*)p_private;
307 return stream_Read( p_demux->s, dst, i_size );
310 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
312 demux_t *p_demux = (demux_t*)p_private;
313 return !stream_Seek( p_demux->s, i_offset );
316 mpc_int32_t ReaderTell( void *p_private)
318 demux_t *p_demux = (demux_t*)p_private;
319 return stream_Tell( p_demux->s );
322 mpc_int32_t ReaderGetSize( void *p_private )
324 demux_t *p_demux = (demux_t*)p_private;
325 return stream_Size( p_demux->s );
328 mpc_bool_t ReaderCanSeek( void *p_private )
330 demux_t *p_demux = (demux_t*)p_private;
333 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );