mux:ts: convert vlc_tick_t to seconds explicitly using SEC_FROM_VLC_TICK()
[vlc.git] / modules / demux / mpc.c
blobb665ea078af6f6b2869f3877cbca61427b30562d
1 /*****************************************************************************
2 * mpc.c : MPC stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_input.h>
36 #include <vlc_codec.h>
37 #include <math.h>
39 #ifdef HAVE_MPC_MPCDEC_H
40 #include <mpc/mpcdec.h>
41 #else
42 #include <mpcdec/mpcdec.h>
43 #endif
45 /* TODO:
46 * - test stream version 4..6
47 * - test fixed float version
48 * - ...
50 * XXX:
51 * It is done the ugly way (the demux does the decode stage... but it works
54 /*****************************************************************************
55 * Module descriptor
56 *****************************************************************************/
57 static int Open ( vlc_object_t * );
58 static void Close ( vlc_object_t * );
60 vlc_module_begin ()
61 set_category( CAT_INPUT )
62 set_subcategory( SUBCAT_INPUT_DEMUX )
63 set_description( N_("MusePack demuxer") )
64 set_capability( "demux", 145 )
66 set_callbacks( Open, Close )
67 add_shortcut( "mpc" )
68 vlc_module_end ()
70 /*****************************************************************************
71 * Local prototypes
72 *****************************************************************************/
73 static int Demux ( demux_t * );
74 static int Control( demux_t *, int, va_list );
76 typedef struct
78 /* */
79 es_out_id_t *p_es;
81 /* */
82 #ifndef HAVE_MPC_MPCDEC_H
83 mpc_decoder decoder;
84 #else
85 mpc_demux *decoder;
86 #endif
87 mpc_reader reader;
88 mpc_streaminfo info;
90 /* */
91 int64_t i_position;
92 } demux_sys_t;
94 #ifndef HAVE_MPC_MPCDEC_H
95 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
96 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset );
97 static mpc_int32_t ReaderTell( void *p_private);
98 static mpc_int32_t ReaderGetSize( void *p_private );
99 static mpc_bool_t ReaderCanSeek( void *p_private );
100 #else
101 static mpc_int32_t ReaderRead( mpc_reader *p_private, void *dst, mpc_int32_t i_size );
102 static mpc_bool_t ReaderSeek( mpc_reader *p_private, mpc_int32_t i_offset );
103 static mpc_int32_t ReaderTell( mpc_reader *p_private);
104 static mpc_int32_t ReaderGetSize( mpc_reader *p_private );
105 static mpc_bool_t ReaderCanSeek( mpc_reader *p_private );
106 #endif
108 /*****************************************************************************
109 * Open: initializes ES structures
110 *****************************************************************************/
111 static int Open( vlc_object_t * p_this )
113 demux_t *p_demux = (demux_t*)p_this;
114 demux_sys_t *p_sys;
115 es_format_t fmt;
116 const uint8_t *p_peek;
118 if( vlc_stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
119 return VLC_EGENERIC;
121 if( memcmp( p_peek, "MP+", 3 )
122 #ifdef HAVE_MPC_MPCDEC_H
123 /* SV8 format */
124 && memcmp( p_peek, "MPCK", 4 )
125 #endif
128 /* for v4..6 we check extension file */
129 const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
130 if( i_version < 4 || i_version > 6 )
131 return VLC_EGENERIC;
133 if( !p_demux->obj.force )
135 /* Check file name extension */
136 if( !demux_IsPathExtension( p_demux, ".mpc" ) &&
137 !demux_IsPathExtension( p_demux, ".mp+" ) &&
138 !demux_IsPathExtension( p_demux, ".mpp" ) )
139 return VLC_EGENERIC;
143 /* */
144 p_sys = calloc( 1, sizeof( *p_sys ) );
145 if( !p_sys )
146 return VLC_ENOMEM;
148 p_sys->i_position = 0;
150 p_sys->reader.read = ReaderRead;
151 p_sys->reader.seek = ReaderSeek;
152 p_sys->reader.tell = ReaderTell;
153 p_sys->reader.get_size = ReaderGetSize;
154 p_sys->reader.canseek = ReaderCanSeek;
155 p_sys->reader.data = p_demux;
157 #ifndef HAVE_MPC_MPCDEC_H
158 /* Load info */
159 mpc_streaminfo_init( &p_sys->info );
160 if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
161 goto error;
163 /* */
164 mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
165 if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
166 goto error;
167 #else
168 p_sys->decoder = mpc_demux_init( &p_sys->reader );
169 if( !p_sys->decoder )
170 goto error;
172 mpc_demux_get_info( p_sys->decoder, &p_sys->info );
173 #endif
175 /* Fill p_demux fields */
176 p_demux->pf_demux = Demux;
177 p_demux->pf_control = Control;
178 p_demux->p_sys = p_sys;
180 /* */
181 #ifndef MPC_FIXED_POINT
182 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_FL32 );
183 #else
184 # ifdef WORDS_BIGENDIAN
185 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32B );
186 # else
187 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32L );
188 # endif
189 #endif
190 fmt.audio.i_channels = p_sys->info.channels;
191 fmt.audio.i_rate = p_sys->info.sample_freq;
192 fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
193 fmt.audio.i_bitspersample = 32;
194 fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
195 fmt.audio.i_bitspersample;
197 #ifdef HAVE_MPC_MPCDEC_H
198 # define CONVERT_PEAK( mpc_peak ) (pow( 10, (mpc_peak) / 256.0 / 20.0 ) / 32767.0)
199 # define CONVERT_GAIN( mpc_gain ) (MPC_OLD_GAIN_REF - (mpc_gain) / 256.0)
200 #else
201 # define CONVERT_PEAK( mpc_peak ) ((mpc_peak) / 32767.0)
202 # define CONVERT_GAIN( mpc_gain ) ((mpc_gain) / 100.0)
203 #endif
205 if( p_sys->info.peak_title > 0 )
207 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
208 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float) CONVERT_PEAK( p_sys->info.peak_title );
209 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
210 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float) CONVERT_GAIN( p_sys->info.gain_title );
212 if( p_sys->info.peak_album > 0 )
214 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
215 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float) CONVERT_PEAK( p_sys->info.peak_album );
216 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
217 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float) CONVERT_GAIN( p_sys->info.gain_album );
220 #undef CONVERT_GAIN
221 #undef CONVERT_PEAK
223 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
224 if( !p_sys->p_es )
225 goto error;
227 return VLC_SUCCESS;
229 error:
230 free( p_sys );
231 return VLC_EGENERIC;
234 /*****************************************************************************
235 * Close: frees unused data
236 *****************************************************************************/
237 static void Close( vlc_object_t * p_this )
239 demux_t *p_demux = (demux_t*)p_this;
240 demux_sys_t *p_sys = p_demux->p_sys;
242 #ifdef HAVE_MPC_MPCDEC_H
243 if( p_sys->decoder )
244 mpc_demux_exit( p_sys->decoder );
245 #endif
246 free( p_sys );
249 /*****************************************************************************
250 * Demux:
251 *****************************************************************************
252 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
253 *****************************************************************************/
254 static int Demux( demux_t *p_demux )
256 demux_sys_t *p_sys = p_demux->p_sys;
257 block_t *p_data;
258 int i_ret;
259 #ifdef HAVE_MPC_MPCDEC_H
260 mpc_frame_info frame;
261 mpc_status err;
262 #endif
263 p_data = block_Alloc( MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
264 if( !p_data )
265 return VLC_DEMUXER_EGENERIC;
267 #ifndef HAVE_MPC_MPCDEC_H
268 i_ret = mpc_decoder_decode( &p_sys->decoder,
269 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
270 NULL, NULL );
271 if( i_ret <= 0 )
273 block_Release( p_data );
274 return i_ret < 0 ? VLC_DEMUXER_EGENERIC : VLC_DEMUXER_EOF;
276 #else
277 frame.buffer = (MPC_SAMPLE_FORMAT*)p_data->p_buffer;
278 err = mpc_demux_decode( p_sys->decoder, &frame );
279 if( err != MPC_STATUS_OK )
281 block_Release( p_data );
282 return VLC_DEMUXER_EGENERIC;
284 else if( frame.bits == -1 )
286 block_Release( p_data );
287 return VLC_DEMUXER_EOF;
290 i_ret = frame.samples;
291 #endif
293 /* */
294 p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
295 p_data->i_dts = p_data->i_pts =
296 VLC_TICK_0 + CLOCK_FREQ * p_sys->i_position / p_sys->info.sample_freq;
298 es_out_SetPCR( p_demux->out, p_data->i_dts );
300 es_out_Send( p_demux->out, p_sys->p_es, p_data );
302 /* */
303 p_sys->i_position += i_ret;
305 return VLC_DEMUXER_SUCCESS;
308 /*****************************************************************************
309 * Control:
310 *****************************************************************************/
311 static int Control( demux_t *p_demux, int i_query, va_list args )
313 demux_sys_t *p_sys = p_demux->p_sys;
314 double f, *pf;
315 int64_t i64, *pi64;
316 bool *pb_bool;
318 switch( i_query )
320 case DEMUX_CAN_SEEK:
321 return vlc_stream_vaControl( p_demux->s, i_query, args );
323 case DEMUX_HAS_UNSUPPORTED_META:
324 pb_bool = va_arg( args, bool* );
325 *pb_bool = true;
326 return VLC_SUCCESS;
328 case DEMUX_GET_LENGTH:
329 pi64 = va_arg( args, int64_t * );
330 #ifndef HAVE_MPC_MPCDEC_H
331 *pi64 = CLOCK_FREQ * p_sys->info.pcm_samples /
332 p_sys->info.sample_freq;
333 #else
334 *pi64 = CLOCK_FREQ * (p_sys->info.samples -
335 p_sys->info.beg_silence) /
336 p_sys->info.sample_freq;
337 #endif
338 return VLC_SUCCESS;
340 case DEMUX_GET_POSITION:
341 pf = va_arg( args, double * );
342 #ifndef HAVE_MPC_MPCDEC_H
343 if( p_sys->info.pcm_samples > 0 )
344 *pf = (double) p_sys->i_position /
345 (double)p_sys->info.pcm_samples;
346 #else
347 if( p_sys->info.samples - p_sys->info.beg_silence > 0)
348 *pf = (double) p_sys->i_position /
349 (double)(p_sys->info.samples - p_sys->info.beg_silence);
350 #endif
351 else
352 *pf = 0.0;
353 return VLC_SUCCESS;
355 case DEMUX_GET_TIME:
356 pi64 = va_arg( args, int64_t * );
357 *pi64 = CLOCK_FREQ * p_sys->i_position /
358 p_sys->info.sample_freq;
359 return VLC_SUCCESS;
361 case DEMUX_SET_POSITION:
362 f = va_arg( args, double );
363 #ifndef HAVE_MPC_MPCDEC_H
364 i64 = (int64_t)(f * p_sys->info.pcm_samples);
365 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
366 #else
367 i64 = (int64_t)(f * (p_sys->info.samples -
368 p_sys->info.beg_silence));
369 if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
370 #endif
372 p_sys->i_position = i64;
373 return VLC_SUCCESS;
375 return VLC_EGENERIC;
377 case DEMUX_SET_TIME:
378 i64 = va_arg( args, int64_t );
379 #ifndef HAVE_MPC_MPCDEC_H
380 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
381 #else
382 if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
383 #endif
385 p_sys->i_position = i64;
386 return VLC_SUCCESS;
388 return VLC_EGENERIC;
390 case DEMUX_CAN_PAUSE:
391 case DEMUX_SET_PAUSE_STATE:
392 case DEMUX_CAN_CONTROL_PACE:
393 case DEMUX_GET_PTS_DELAY:
394 return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
396 default:
397 return VLC_EGENERIC;
401 #ifndef HAVE_MPC_MPCDEC_H
402 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
404 demux_t *p_demux = (demux_t*)p_private;
405 #else
406 static mpc_int32_t ReaderRead( mpc_reader *p_private, void *dst, mpc_int32_t i_size )
408 demux_t *p_demux = (demux_t*)p_private->data;
409 #endif
410 return vlc_stream_Read( p_demux->s, dst, i_size );
413 #ifndef HAVE_MPC_MPCDEC_H
414 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
416 demux_t *p_demux = (demux_t*)p_private;
417 #else
418 static mpc_bool_t ReaderSeek( mpc_reader *p_private, mpc_int32_t i_offset )
420 demux_t *p_demux = (demux_t*)p_private->data;
421 #endif
422 return !vlc_stream_Seek( p_demux->s, i_offset );
425 #ifndef HAVE_MPC_MPCDEC_H
426 static mpc_int32_t ReaderTell( void *p_private)
428 demux_t *p_demux = (demux_t*)p_private;
429 #else
430 static mpc_int32_t ReaderTell( mpc_reader *p_private)
432 demux_t *p_demux = (demux_t*)p_private->data;
433 #endif
434 return vlc_stream_Tell( p_demux->s );
437 #ifndef HAVE_MPC_MPCDEC_H
438 static mpc_int32_t ReaderGetSize( void *p_private )
440 demux_t *p_demux = (demux_t*)p_private;
441 #else
442 static mpc_int32_t ReaderGetSize( mpc_reader *p_private )
444 demux_t *p_demux = (demux_t*)p_private->data;
445 #endif
446 return stream_Size( p_demux->s );
449 #ifndef HAVE_MPC_MPCDEC_H
450 static mpc_bool_t ReaderCanSeek( void *p_private )
452 demux_t *p_demux = (demux_t*)p_private;
453 #else
454 static mpc_bool_t ReaderCanSeek( mpc_reader *p_private )
456 demux_t *p_demux = (demux_t*)p_private->data;
457 #endif
458 bool b_canseek;
460 vlc_stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
461 return b_canseek;