]> git.sesse.net Git - vlc/blob - modules/demux/mpc.c
a3b657a453023dcdf22ea2ab673946fd5f63a827
[vlc] / modules / demux / mpc.c
1 /*****************************************************************************
2  * mpc.c : MPC stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
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>
36 #include <math.h>
37
38 #ifdef HAVE_MPC_MPCDEC_H
39 #include <mpc/mpcdec.h>
40 #else
41 #include <mpcdec/mpcdec.h>
42 #endif
43
44 /* TODO:
45  *  - test stream version 4..6
46  *  - test fixed float version
47  *  - ...
48  *
49  *  XXX:
50  *  It is done the ugly way (the demux does the decode stage... but it works
51  */
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 static int  Open  ( vlc_object_t * );
57 static void Close ( vlc_object_t * );
58
59 vlc_module_begin ()
60     set_category( CAT_INPUT )
61     set_subcategory( SUBCAT_INPUT_DEMUX )
62     set_description( N_("MusePack demuxer") )
63     set_capability( "demux", 145 )
64
65     set_callbacks( Open, Close )
66     add_shortcut( "mpc" )
67 vlc_module_end ()
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 static int Demux  ( demux_t * );
73 static int Control( demux_t *, int, va_list );
74
75 struct demux_sys_t
76 {
77     /* */
78     es_out_id_t   *p_es;
79
80     /* */
81 #ifndef HAVE_MPC_MPCDEC_H
82     mpc_decoder    decoder;
83 #else
84     mpc_demux     *decoder;
85 #endif
86     mpc_reader     reader;
87     mpc_streaminfo info;
88
89     /* */
90     int64_t        i_position;
91 };
92
93 #ifndef HAVE_MPC_MPCDEC_H
94 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
95 static mpc_bool_t  ReaderSeek( void *p_private, mpc_int32_t i_offset );
96 static mpc_int32_t ReaderTell( void *p_private);
97 static mpc_int32_t ReaderGetSize( void *p_private );
98 static mpc_bool_t  ReaderCanSeek( void *p_private );
99 #else
100 static mpc_int32_t ReaderRead( mpc_reader *p_private, void *dst, mpc_int32_t i_size );
101 static mpc_bool_t  ReaderSeek( mpc_reader *p_private, mpc_int32_t i_offset );
102 static mpc_int32_t ReaderTell( mpc_reader *p_private);
103 static mpc_int32_t ReaderGetSize( mpc_reader *p_private );
104 static mpc_bool_t  ReaderCanSeek( mpc_reader *p_private );
105 #endif
106
107 /*****************************************************************************
108  * Open: initializes ES structures
109  *****************************************************************************/
110 static int Open( vlc_object_t * p_this )
111 {
112     demux_t     *p_demux = (demux_t*)p_this;
113     demux_sys_t *p_sys;
114     es_format_t fmt;
115     const uint8_t *p_peek;
116
117     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
118         return VLC_EGENERIC;
119
120     if( memcmp( p_peek, "MP+", 3 )
121 #ifdef HAVE_MPC_MPCDEC_H
122         /* SV8 format */
123         && memcmp( p_peek, "MPCK", 4 )
124 #endif
125       )
126     {
127         /* for v4..6 we check extension file */
128         const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
129         if( i_version  < 4 || i_version > 6 )
130             return VLC_EGENERIC;
131
132         if( !p_demux->b_force )
133         {
134             /* Check file name extension */
135             if( !demux_IsPathExtension( p_demux, ".mpc" ) &&
136                 !demux_IsPathExtension( p_demux, ".mp+" ) &&
137                 !demux_IsPathExtension( p_demux, ".mpp" ) )
138                 return VLC_EGENERIC;
139         }
140     }
141
142     /* */
143     p_sys = calloc( 1, sizeof( *p_sys ) );
144     if( !p_sys )
145         return VLC_ENOMEM;
146
147     p_sys->i_position = 0;
148
149     p_sys->reader.read = ReaderRead;
150     p_sys->reader.seek = ReaderSeek;
151     p_sys->reader.tell = ReaderTell;
152     p_sys->reader.get_size = ReaderGetSize;
153     p_sys->reader.canseek = ReaderCanSeek;
154     p_sys->reader.data = p_demux;
155
156 #ifndef HAVE_MPC_MPCDEC_H
157     /* Load info */
158     mpc_streaminfo_init( &p_sys->info );
159     if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
160         goto error;
161
162     /* */
163     mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
164     if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
165         goto error;
166 #else
167     p_sys->decoder = mpc_demux_init( &p_sys->reader );
168     if( !p_sys->decoder )
169         goto error;
170
171     mpc_demux_get_info( p_sys->decoder, &p_sys->info );
172 #endif
173
174     /* Fill p_demux fields */
175     p_demux->pf_demux = Demux;
176     p_demux->pf_control = Control;
177     p_demux->p_sys = p_sys;
178
179     /* */
180 #ifndef MPC_FIXED_POINT
181     es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_FL32 );
182 #else
183 #   ifdef WORDS_BIGENDIAN
184     es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32B );
185 #   else
186     es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32L );
187 #   endif
188 #endif
189     fmt.audio.i_channels = p_sys->info.channels;
190     fmt.audio.i_rate = p_sys->info.sample_freq;
191     fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
192     fmt.audio.i_bitspersample = 32;
193     fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
194                     fmt.audio.i_bitspersample;
195
196 #ifdef HAVE_MPC_MPCDEC_H
197 #   define CONVERT_PEAK( mpc_peak ) (pow( 10, (mpc_peak) / 256.0 / 20.0 ) / 32767.0)
198 #   define CONVERT_GAIN( mpc_gain ) (MPC_OLD_GAIN_REF - (mpc_gain) / 256.0)
199 #else
200 #   define CONVERT_PEAK( mpc_peak ) ((mpc_peak) / 32767.0)
201 #   define CONVERT_GAIN( mpc_gain ) ((mpc_gain) / 100.0)
202 #endif
203
204     if( p_sys->info.peak_title > 0 )
205     {
206         fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
207         fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float) CONVERT_PEAK( p_sys->info.peak_title );
208         fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
209         fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float) CONVERT_GAIN( p_sys->info.gain_title );
210     }
211     if( p_sys->info.peak_album > 0 )
212     {
213         fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
214         fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float) CONVERT_PEAK( p_sys->info.peak_album );
215         fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
216         fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float) CONVERT_GAIN( p_sys->info.gain_album );
217     }
218
219 #undef CONVERT_GAIN
220 #undef CONVERT_PEAK
221
222     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
223     if( !p_sys->p_es )
224         goto error;
225
226     return VLC_SUCCESS;
227
228 error:
229     free( p_sys );
230     return VLC_EGENERIC;
231 }
232
233 /*****************************************************************************
234  * Close: frees unused data
235  *****************************************************************************/
236 static void Close( vlc_object_t * p_this )
237 {
238     demux_t        *p_demux = (demux_t*)p_this;
239     demux_sys_t    *p_sys = p_demux->p_sys;
240
241 #ifdef HAVE_MPC_MPCDEC_H
242     if( p_sys->decoder )
243     mpc_demux_exit( p_sys->decoder );
244 #endif
245     free( p_sys );
246 }
247
248 /*****************************************************************************
249  * Demux:
250  *****************************************************************************
251  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
252  *****************************************************************************/
253 static int Demux( demux_t *p_demux )
254 {
255     demux_sys_t *p_sys = p_demux->p_sys;
256     block_t     *p_data;
257     int i_ret;
258 #ifdef HAVE_MPC_MPCDEC_H
259     mpc_frame_info frame;
260     mpc_status err;
261 #endif
262     p_data = block_Alloc( MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
263     if( !p_data )
264         return -1;
265
266 #ifndef HAVE_MPC_MPCDEC_H
267     i_ret = mpc_decoder_decode( &p_sys->decoder,
268                                 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
269                                 NULL, NULL );
270     if( i_ret <= 0 )
271     {
272         block_Release( p_data );
273         return i_ret < 0 ? -1 : 0;
274     }
275 #else
276     frame.buffer = (MPC_SAMPLE_FORMAT*)p_data->p_buffer;
277     err = mpc_demux_decode( p_sys->decoder, &frame );
278     if( err != MPC_STATUS_OK )
279     {
280         block_Release( p_data );
281         return -1;
282     }
283     else if( frame.bits == -1 )
284     {
285         block_Release( p_data );
286         return 0;
287     }
288
289     i_ret = frame.samples;
290 #endif
291
292     /* */
293     p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
294     p_data->i_dts = p_data->i_pts =
295             VLC_TS_0 + CLOCK_FREQ * p_sys->i_position / p_sys->info.sample_freq;
296
297     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
298
299     es_out_Send( p_demux->out, p_sys->p_es, p_data );
300
301     /* */
302     p_sys->i_position += i_ret;
303
304     return 1;
305 }
306
307 /*****************************************************************************
308  * Control:
309  *****************************************************************************/
310 static int Control( demux_t *p_demux, int i_query, va_list args )
311 {
312     demux_sys_t *p_sys = p_demux->p_sys;
313     double   f, *pf;
314     int64_t i64, *pi64;
315     bool *pb_bool;
316
317     switch( i_query )
318     {
319         case DEMUX_HAS_UNSUPPORTED_META:
320             pb_bool = (bool*)va_arg( args, bool* );
321             *pb_bool = true;
322             return VLC_SUCCESS;
323
324         case DEMUX_GET_LENGTH:
325             pi64 = (int64_t*)va_arg( args, int64_t * );
326 #ifndef HAVE_MPC_MPCDEC_H
327             *pi64 = CLOCK_FREQ * p_sys->info.pcm_samples /
328                         p_sys->info.sample_freq;
329 #else
330             *pi64 = CLOCK_FREQ * (p_sys->info.samples -
331                                         p_sys->info.beg_silence) /
332                 p_sys->info.sample_freq;
333 #endif
334             return VLC_SUCCESS;
335
336         case DEMUX_GET_POSITION:
337             pf = (double*)va_arg( args, double * );
338 #ifndef HAVE_MPC_MPCDEC_H
339             if( p_sys->info.pcm_samples > 0 )
340                 *pf = (double) p_sys->i_position /
341                       (double)p_sys->info.pcm_samples;
342 #else
343             if( p_sys->info.samples - p_sys->info.beg_silence > 0)
344                 *pf = (double) p_sys->i_position /
345                       (double)(p_sys->info.samples - p_sys->info.beg_silence);
346 #endif
347             else
348                 *pf = 0.0;
349             return VLC_SUCCESS;
350
351         case DEMUX_GET_TIME:
352             pi64 = (int64_t*)va_arg( args, int64_t * );
353             *pi64 = CLOCK_FREQ * p_sys->i_position /
354                         p_sys->info.sample_freq;
355             return VLC_SUCCESS;
356
357         case DEMUX_SET_POSITION:
358             f = (double)va_arg( args, double );
359 #ifndef HAVE_MPC_MPCDEC_H
360             i64 = (int64_t)(f * p_sys->info.pcm_samples);
361             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
362 #else
363             i64 = (int64_t)(f * (p_sys->info.samples -
364                                  p_sys->info.beg_silence));
365             if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
366 #endif
367             {
368                 p_sys->i_position = i64;
369                 return VLC_SUCCESS;
370             }
371             return VLC_EGENERIC;
372
373         case DEMUX_SET_TIME:
374             i64 = (int64_t)va_arg( args, int64_t );
375 #ifndef HAVE_MPC_MPCDEC_H
376             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
377 #else
378              if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
379 #endif
380             {
381                 p_sys->i_position = i64;
382                 return VLC_SUCCESS;
383             }
384             return VLC_EGENERIC;
385
386         default:
387             return VLC_EGENERIC;
388     }
389 }
390
391 #ifndef HAVE_MPC_MPCDEC_H
392 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
393 {
394     demux_t *p_demux = (demux_t*)p_private;
395 #else
396 static mpc_int32_t ReaderRead( mpc_reader *p_private, void *dst, mpc_int32_t i_size )
397 {
398     demux_t *p_demux = (demux_t*)p_private->data;
399 #endif
400     return stream_Read( p_demux->s, dst, i_size );
401 }
402
403 #ifndef HAVE_MPC_MPCDEC_H
404 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
405 {
406     demux_t *p_demux = (demux_t*)p_private;
407 #else
408 static mpc_bool_t ReaderSeek( mpc_reader *p_private, mpc_int32_t i_offset )
409 {
410     demux_t *p_demux = (demux_t*)p_private->data;
411 #endif
412     return !stream_Seek( p_demux->s, i_offset );
413 }
414
415 #ifndef HAVE_MPC_MPCDEC_H
416 static mpc_int32_t ReaderTell( void *p_private)
417 {
418     demux_t *p_demux = (demux_t*)p_private;
419 #else
420 static mpc_int32_t ReaderTell( mpc_reader *p_private)
421 {
422     demux_t *p_demux = (demux_t*)p_private->data;
423 #endif
424     return stream_Tell( p_demux->s );
425 }
426
427 #ifndef HAVE_MPC_MPCDEC_H
428 static mpc_int32_t ReaderGetSize( void *p_private )
429 {
430     demux_t *p_demux = (demux_t*)p_private;
431 #else
432 static mpc_int32_t ReaderGetSize( mpc_reader *p_private )
433 {
434     demux_t *p_demux = (demux_t*)p_private->data;
435 #endif
436     return stream_Size( p_demux->s );
437 }
438
439 #ifndef HAVE_MPC_MPCDEC_H
440 static mpc_bool_t ReaderCanSeek( void *p_private )
441 {
442     demux_t *p_demux = (demux_t*)p_private;
443 #else
444 static mpc_bool_t ReaderCanSeek( mpc_reader *p_private )
445 {
446     demux_t *p_demux = (demux_t*)p_private->data;
447 #endif
448     bool b_canseek;
449
450     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
451     return b_canseek;
452 }
453