1 /*****************************************************************************
2 * mpc.c : MPC stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 the VideoLAN team
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_demux.h>
33 #include <vlc_codec.h>
36 #include <mpcdec/mpcdec.h>
39 * - test stream version 4..6
40 * - test fixed float version
44 * It is done the ugly way (the demux does the decode stage... but it works
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Open ( vlc_object_t
* );
53 set_category( CAT_INPUT
)
54 set_subcategory( SUBCAT_INPUT_DEMUX
)
55 set_description( N_("MusePack demuxer") )
56 set_capability( "demux", 145 )
60 add_file_extension("mpc")
61 add_file_extension("mp+")
62 add_file_extension("mpp")
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( vlc_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
->obj
.force
)
117 p_sys
= vlc_obj_calloc( p_this
, 1, sizeof( *p_sys
) );
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
)
136 mpc_decoder_setup( &p_sys
->decoder
, &p_sys
->reader
);
137 if( !mpc_decoder_initialize( &p_sys
->decoder
, &p_sys
->info
) )
140 /* Fill p_demux fields */
141 p_demux
->pf_demux
= Demux
;
142 p_demux
->pf_control
= Control
;
143 p_demux
->p_sys
= p_sys
;
146 #ifndef MPC_FIXED_POINT
147 es_format_Init( &fmt
, AUDIO_ES
, VLC_CODEC_FL32
);
149 # ifdef WORDS_BIGENDIAN
150 es_format_Init( &fmt
, AUDIO_ES
, VLC_CODEC_S32B
);
152 es_format_Init( &fmt
, AUDIO_ES
, VLC_CODEC_S32L
);
155 fmt
.audio
.i_channels
= p_sys
->info
.channels
;
156 fmt
.audio
.i_rate
= p_sys
->info
.sample_freq
;
157 fmt
.audio
.i_blockalign
= 4*fmt
.audio
.i_channels
;
158 fmt
.audio
.i_bitspersample
= 32;
159 fmt
.i_bitrate
= fmt
.i_bitrate
* fmt
.audio
.i_channels
*
160 fmt
.audio
.i_bitspersample
;
162 #ifdef HAVE_MPC_MPCDEC_H
163 # define CONVERT_PEAK( mpc_peak ) (pow( 10, (mpc_peak) / 256.0 / 20.0 ) / 32767.0)
164 # define CONVERT_GAIN( mpc_gain ) (MPC_OLD_GAIN_REF - (mpc_gain) / 256.0)
166 # define CONVERT_PEAK( mpc_peak ) ((mpc_peak) / 32767.0)
167 # define CONVERT_GAIN( mpc_gain ) ((mpc_gain) / 100.0)
170 if( p_sys
->info
.peak_title
> 0 )
172 fmt
.audio_replay_gain
.pb_peak
[AUDIO_REPLAY_GAIN_TRACK
] = true;
173 fmt
.audio_replay_gain
.pf_peak
[AUDIO_REPLAY_GAIN_TRACK
] = (float) CONVERT_PEAK( p_sys
->info
.peak_title
);
174 fmt
.audio_replay_gain
.pb_gain
[AUDIO_REPLAY_GAIN_TRACK
] = true;
175 fmt
.audio_replay_gain
.pf_gain
[AUDIO_REPLAY_GAIN_TRACK
] = (float) CONVERT_GAIN( p_sys
->info
.gain_title
);
177 if( p_sys
->info
.peak_album
> 0 )
179 fmt
.audio_replay_gain
.pb_peak
[AUDIO_REPLAY_GAIN_ALBUM
] = true;
180 fmt
.audio_replay_gain
.pf_peak
[AUDIO_REPLAY_GAIN_ALBUM
] = (float) CONVERT_PEAK( p_sys
->info
.peak_album
);
181 fmt
.audio_replay_gain
.pb_gain
[AUDIO_REPLAY_GAIN_ALBUM
] = true;
182 fmt
.audio_replay_gain
.pf_gain
[AUDIO_REPLAY_GAIN_ALBUM
] = (float) CONVERT_GAIN( p_sys
->info
.gain_album
);
189 p_sys
->p_es
= es_out_Add( p_demux
->out
, &fmt
);
196 /*****************************************************************************
198 *****************************************************************************
199 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
200 *****************************************************************************/
201 static int Demux( demux_t
*p_demux
)
203 demux_sys_t
*p_sys
= p_demux
->p_sys
;
207 p_data
= block_New( p_demux
,
208 MPC_DECODER_BUFFER_LENGTH
*sizeof(MPC_SAMPLE_FORMAT
) );
210 return VLC_DEMUXER_EGENERIC
;
212 i_ret
= mpc_decoder_decode( &p_sys
->decoder
,
213 (MPC_SAMPLE_FORMAT
*)p_data
->p_buffer
,
217 block_Release( p_data
);
218 return i_ret
< 0 ? VLC_DEMUXER_EGENERIC
: VLC_DEMUXER_EOF
;
222 p_data
->i_buffer
= i_ret
* sizeof(MPC_SAMPLE_FORMAT
) * p_sys
->info
.channels
;
223 p_data
->i_dts
= p_data
->i_pts
=
224 VLC_TICK_0
+ vlc_tick_from_samples(p_sys
->i_position
, p_sys
->info
.sample_freq
);
226 es_out_SetPCR( p_demux
->out
, p_data
->i_dts
);
228 es_out_Send( p_demux
->out
, p_sys
->p_es
, p_data
);
231 p_sys
->i_position
+= i_ret
;
233 return VLC_DEMUXER_SUCCESS
;
236 /*****************************************************************************
238 *****************************************************************************/
239 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
241 demux_sys_t
*p_sys
= p_demux
->p_sys
;
250 return vlc_stream_vaControl( p_demux
->s
, i_query
, args
);
252 case DEMUX_HAS_UNSUPPORTED_META
:
253 pb_bool
= va_arg( args
, bool* );
257 case DEMUX_GET_LENGTH
:
258 *va_arg( args
, vlc_tick_t
* ) =
259 vlc_tick_from_samples(p_sys
->info
.pcm_samples
, p_sys
->info
.sample_freq
);
262 case DEMUX_GET_POSITION
:
263 pf
= va_arg( args
, double * );
264 pi64
= (int64_t*)va_arg( args
, int64_t * );
265 *pi64
= INT64_C(1000000) * p_sys
->info
.pcm_samples
/
266 p_sys
->info
.sample_freq
;
272 *va_arg( args
, vlc_tick_t
* ) =
273 vlc_tick_from_samples(p_sys
->i_position
, p_sys
->info
.sample_freq
);
276 case DEMUX_SET_POSITION
:
277 f
= va_arg( args
, double );
278 i64
= (int64_t)(f
* p_sys
->info
.pcm_samples
);
279 if( mpc_decoder_seek_sample( &p_sys
->decoder
, i64
) )
281 p_sys
->i_position
= i64
;
288 vlc_tick_t i64
= va_arg( args
, vlc_tick_t
);
289 if( mpc_decoder_seek_sample( &p_sys
->decoder
, i64
) )
291 p_sys
->i_position
= i64
;
297 case DEMUX_CAN_PAUSE
:
298 case DEMUX_SET_PAUSE_STATE
:
299 case DEMUX_CAN_CONTROL_PACE
:
300 case DEMUX_GET_PTS_DELAY
:
301 return demux_vaControlHelper( p_demux
->s
, 0, -1, 0, 1, i_query
, args
);
308 static mpc_int32_t
ReaderRead( void *p_private
, void *dst
, mpc_int32_t i_size
)
310 demux_t
*p_demux
= (demux_t
*)p_private
;
311 return vlc_stream_Read( p_demux
->s
, dst
, i_size
);
314 static mpc_bool_t
ReaderSeek( void *p_private
, mpc_int32_t i_offset
)
316 demux_t
*p_demux
= (demux_t
*)p_private
;
317 return !vlc_stream_Seek( p_demux
->s
, i_offset
);
320 static mpc_int32_t
ReaderTell( void *p_private
)
322 demux_t
*p_demux
= (demux_t
*)p_private
;
323 return vlc_stream_Tell( p_demux
->s
);
326 static mpc_int32_t
ReaderGetSize( void *p_private
)
328 demux_t
*p_demux
= (demux_t
*)p_private
;
329 return stream_Size( p_demux
->s
);
332 static mpc_bool_t
ReaderCanSeek( void *p_private
)
334 demux_t
*p_demux
= (demux_t
*)p_private
;
337 vlc_stream_Control( p_demux
->s
, STREAM_CAN_SEEK
, &b_canseek
);