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>
31 #include <vlc_codec.h>
34 #include <mpcdec/mpcdec.h>
37 * - test stream version 4..6
38 * - test fixed float version
42 * It is done the ugly way (the demux does the decode stage... but it works
45 /*****************************************************************************
47 *****************************************************************************/
48 #define REPLAYGAIN_TYPE_TEXT N_("Replay Gain type" )
49 #define REPLAYGAIN_TYPE_LONGTEXT N_( "Musepack can have a title-specific " \
50 "replay gain (volume control) or an album-specific one. " \
51 "Choose which type you want to use" )
53 static int Open ( vlc_object_t * );
54 static void Close ( vlc_object_t * );
57 set_category( CAT_INPUT );
58 set_subcategory( SUBCAT_INPUT_DEMUX );
59 set_description( _("MusePack demuxer") );
60 set_capability( "demux2", 145 );
62 set_callbacks( Open, Close );
63 add_shortcut( "mpc" );
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Demux ( demux_t * );
70 static int Control( demux_t *, int, va_list );
87 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
88 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset );
89 mpc_int32_t ReaderTell( void *p_private);
90 mpc_int32_t ReaderGetSize( void *p_private );
91 mpc_bool_t ReaderCanSeek( void *p_private );
93 /*****************************************************************************
94 * Open: initializes ES structures
95 *****************************************************************************/
96 static int Open( vlc_object_t * p_this )
98 demux_t *p_demux = (demux_t*)p_this;
101 const uint8_t *p_peek;
104 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
107 if( memcmp( p_peek, "MP+", 3 ) )
109 /* for v4..6 we check extension file */
110 const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
112 if( i_version < 4 || i_version > 6 )
115 if( !p_demux->b_force )
117 /* Check file name extension */
118 if( !demux2_IsPathExtension( p_demux, ".mpc" ) &&
119 !demux2_IsPathExtension( p_demux, ".mp+" ) &&
120 !demux2_IsPathExtension( p_demux, ".mpp" ) )
126 p_sys = malloc( sizeof( demux_sys_t ) );
127 memset( p_sys, 0, sizeof(demux_sys_t) );
129 p_sys->i_position = 0;
131 p_sys->reader.read = ReaderRead;
132 p_sys->reader.seek = ReaderSeek;
133 p_sys->reader.tell = ReaderTell;
134 p_sys->reader.get_size = ReaderGetSize;
135 p_sys->reader.canseek = ReaderCanSeek;
136 p_sys->reader.data = p_demux;
139 mpc_streaminfo_init( &p_sys->info );
140 if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
148 mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
149 if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
156 /* Fill p_demux fields */
157 p_demux->pf_demux = Demux;
158 p_demux->pf_control = Control;
159 p_demux->p_sys = p_sys;
162 #ifndef MPC_FIXED_POINT
163 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', '3', '2' ) );
165 # ifdef WORDS_BIGENDIAN
166 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'b' ) );
168 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'l' ) );
171 fmt.audio.i_channels = p_sys->info.channels;
172 fmt.audio.i_rate = p_sys->info.sample_freq;
173 fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
174 fmt.audio.i_bitspersample = 32;
175 fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
176 fmt.audio.i_bitspersample;
177 if( p_sys->info.peak_title > 0 )
179 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
180 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.peak_title / 32767.0;
181 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
182 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.gain_title / 100.0;
184 if( p_sys->info.peak_album > 0 )
186 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
187 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.peak_album / 32767.0;
188 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
189 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.gain_album / 100.0;
192 /* Parse possible id3 header */
193 if( !var_CreateGetBool( p_demux, "meta-preparsed" ) )
195 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
196 if( !p_demux->p_private )
198 if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
200 module_Unneed( p_demux, p_id3 );
201 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
202 p_sys->p_meta = p_demux_meta->p_meta;
204 for( i = 0; i < p_demux_meta->i_attachments; i++ )
205 free( p_demux_meta->attachments[i] );
206 TAB_CLEAN( p_demux_meta->i_attachments,
207 p_demux_meta->attachments );
209 free( p_demux->p_private );
213 p_sys->p_meta = vlc_meta_New();
215 vlc_audio_replay_gain_MergeFromMeta( &fmt.audio_replay_gain, p_sys->p_meta );
216 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 var_Destroy( p_demux, "meta-preparsed" );
233 /*****************************************************************************
235 *****************************************************************************
236 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
237 *****************************************************************************/
238 static int Demux( demux_t *p_demux )
240 demux_sys_t *p_sys = p_demux->p_sys;
244 p_data = block_New( p_demux,
245 MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
246 i_ret = mpc_decoder_decode( &p_sys->decoder,
247 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
251 block_Release( p_data );
252 return i_ret < 0 ? -1 : 0;
256 p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
257 p_data->i_dts = p_data->i_pts =
258 1 + I64C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
260 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
262 es_out_Send( p_demux->out, p_sys->p_es, p_data );
265 p_sys->i_position += i_ret;
270 /*****************************************************************************
272 *****************************************************************************/
273 static int Control( demux_t *p_demux, int i_query, va_list args )
275 demux_sys_t *p_sys = p_demux->p_sys;
283 p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
285 vlc_meta_Merge( p_meta, p_sys->p_meta );
290 case DEMUX_GET_LENGTH:
291 pi64 = (int64_t*)va_arg( args, int64_t * );
292 *pi64 = I64C(1000000) * p_sys->info.pcm_samples /
293 p_sys->info.sample_freq;
296 case DEMUX_GET_POSITION:
297 pf = (double*)va_arg( args, double * );
298 if( p_sys->info.pcm_samples > 0 )
299 *pf = (double) p_sys->i_position /
300 (double)p_sys->info.pcm_samples;
306 pi64 = (int64_t*)va_arg( args, int64_t * );
307 *pi64 = I64C(1000000) * p_sys->i_position /
308 p_sys->info.sample_freq;
311 case DEMUX_SET_POSITION:
312 f = (double)va_arg( args, double );
313 i64 = (int64_t)(f * p_sys->info.pcm_samples);
314 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
316 p_sys->i_position = i64;
322 i64 = (int64_t)va_arg( args, int64_t );
323 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
325 p_sys->i_position = i64;
335 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
337 demux_t *p_demux = (demux_t*)p_private;
338 return stream_Read( p_demux->s, dst, i_size );
341 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
343 demux_t *p_demux = (demux_t*)p_private;
344 return !stream_Seek( p_demux->s, i_offset );
347 mpc_int32_t ReaderTell( void *p_private)
349 demux_t *p_demux = (demux_t*)p_private;
350 return stream_Tell( p_demux->s );
353 mpc_int32_t ReaderGetSize( void *p_private )
355 demux_t *p_demux = (demux_t*)p_private;
356 return stream_Size( p_demux->s );
359 mpc_bool_t ReaderCanSeek( void *p_private )
361 demux_t *p_demux = (demux_t*)p_private;
362 vlc_bool_t b_canseek;
364 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );