]> git.sesse.net Git - vlc/blob - modules/demux/mpc.c
vlc_plugin: fix non-LGPL plugins meta infos
[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 #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>
38
39 #ifdef HAVE_MPC_MPCDEC_H
40 #include <mpc/mpcdec.h>
41 #else
42 #include <mpcdec/mpcdec.h>
43 #endif
44
45 /* TODO:
46  *  - test stream version 4..6
47  *  - test fixed float version
48  *  - ...
49  *
50  *  XXX:
51  *  It is done the ugly way (the demux does the decode stage... but it works
52  */
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 static int  Open  ( vlc_object_t * );
58 static void Close ( vlc_object_t * );
59
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 )
65
66     set_callbacks( Open, Close )
67     add_shortcut( "mpc" )
68 vlc_module_end ()
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 static int Demux  ( demux_t * );
74 static int Control( demux_t *, int, va_list );
75
76 struct demux_sys_t
77 {
78     /* */
79     es_out_id_t   *p_es;
80
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;
89
90     /* */
91     int64_t        i_position;
92 };
93
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
107
108 /*****************************************************************************
109  * Open: initializes ES structures
110  *****************************************************************************/
111 static int Open( vlc_object_t * p_this )
112 {
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;
117
118     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
119         return VLC_EGENERIC;
120
121     if( memcmp( p_peek, "MP+", 3 )
122 #ifdef HAVE_MPC_MPCDEC_H
123         /* SV8 format */
124         && memcmp( p_peek, "MPCK", 4 )
125 #endif
126       )
127     {
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;
132
133         if( !p_demux->b_force )
134         {
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;
140         }
141     }
142
143     /* */
144     p_sys = calloc( 1, sizeof( *p_sys ) );
145     if( !p_sys )
146         return VLC_ENOMEM;
147
148     p_sys->i_position = 0;
149
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;
156
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;
162
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;
171
172     mpc_demux_get_info( p_sys->decoder, &p_sys->info );
173 #endif
174
175     /* Fill p_demux fields */
176     p_demux->pf_demux = Demux;
177     p_demux->pf_control = Control;
178     p_demux->p_sys = p_sys;
179
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;
196
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
204
205     if( p_sys->info.peak_title > 0 )
206     {
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 );
211     }
212     if( p_sys->info.peak_album > 0 )
213     {
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 );
218     }
219
220 #undef CONVERT_GAIN
221 #undef CONVERT_PEAK
222
223     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
224     if( !p_sys->p_es )
225         goto error;
226
227     return VLC_SUCCESS;
228
229 error:
230     free( p_sys );
231     return VLC_EGENERIC;
232 }
233
234 /*****************************************************************************
235  * Close: frees unused data
236  *****************************************************************************/
237 static void Close( vlc_object_t * p_this )
238 {
239     demux_t        *p_demux = (demux_t*)p_this;
240     demux_sys_t    *p_sys = p_demux->p_sys;
241
242 #ifdef HAVE_MPC_MPCDEC_H
243     if( p_sys->decoder )
244     mpc_demux_exit( p_sys->decoder );
245 #endif
246     free( p_sys );
247 }
248
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 )
255 {
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 -1;
266
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 )
272     {
273         block_Release( p_data );
274         return i_ret < 0 ? -1 : 0;
275     }
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 )
280     {
281         block_Release( p_data );
282         return -1;
283     }
284     else if( frame.bits == -1 )
285     {
286         block_Release( p_data );
287         return 0;
288     }
289
290     i_ret = frame.samples;
291 #endif
292
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_TS_0 + CLOCK_FREQ * p_sys->i_position / p_sys->info.sample_freq;
297
298     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
299
300     es_out_Send( p_demux->out, p_sys->p_es, p_data );
301
302     /* */
303     p_sys->i_position += i_ret;
304
305     return 1;
306 }
307
308 /*****************************************************************************
309  * Control:
310  *****************************************************************************/
311 static int Control( demux_t *p_demux, int i_query, va_list args )
312 {
313     demux_sys_t *p_sys = p_demux->p_sys;
314     double   f, *pf;
315     int64_t i64, *pi64;
316     bool *pb_bool;
317
318     switch( i_query )
319     {
320         case DEMUX_HAS_UNSUPPORTED_META:
321             pb_bool = (bool*)va_arg( args, bool* );
322             *pb_bool = true;
323             return VLC_SUCCESS;
324
325         case DEMUX_GET_LENGTH:
326             pi64 = (int64_t*)va_arg( args, int64_t * );
327 #ifndef HAVE_MPC_MPCDEC_H
328             *pi64 = CLOCK_FREQ * p_sys->info.pcm_samples /
329                         p_sys->info.sample_freq;
330 #else
331             *pi64 = CLOCK_FREQ * (p_sys->info.samples -
332                                         p_sys->info.beg_silence) /
333                 p_sys->info.sample_freq;
334 #endif
335             return VLC_SUCCESS;
336
337         case DEMUX_GET_POSITION:
338             pf = (double*)va_arg( args, double * );
339 #ifndef HAVE_MPC_MPCDEC_H
340             if( p_sys->info.pcm_samples > 0 )
341                 *pf = (double) p_sys->i_position /
342                       (double)p_sys->info.pcm_samples;
343 #else
344             if( p_sys->info.samples - p_sys->info.beg_silence > 0)
345                 *pf = (double) p_sys->i_position /
346                       (double)(p_sys->info.samples - p_sys->info.beg_silence);
347 #endif
348             else
349                 *pf = 0.0;
350             return VLC_SUCCESS;
351
352         case DEMUX_GET_TIME:
353             pi64 = (int64_t*)va_arg( args, int64_t * );
354             *pi64 = CLOCK_FREQ * p_sys->i_position /
355                         p_sys->info.sample_freq;
356             return VLC_SUCCESS;
357
358         case DEMUX_SET_POSITION:
359             f = (double)va_arg( args, double );
360 #ifndef HAVE_MPC_MPCDEC_H
361             i64 = (int64_t)(f * p_sys->info.pcm_samples);
362             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
363 #else
364             i64 = (int64_t)(f * (p_sys->info.samples -
365                                  p_sys->info.beg_silence));
366             if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
367 #endif
368             {
369                 p_sys->i_position = i64;
370                 return VLC_SUCCESS;
371             }
372             return VLC_EGENERIC;
373
374         case DEMUX_SET_TIME:
375             i64 = (int64_t)va_arg( args, int64_t );
376 #ifndef HAVE_MPC_MPCDEC_H
377             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
378 #else
379              if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
380 #endif
381             {
382                 p_sys->i_position = i64;
383                 return VLC_SUCCESS;
384             }
385             return VLC_EGENERIC;
386
387         default:
388             return VLC_EGENERIC;
389     }
390 }
391
392 #ifndef HAVE_MPC_MPCDEC_H
393 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
394 {
395     demux_t *p_demux = (demux_t*)p_private;
396 #else
397 static mpc_int32_t ReaderRead( mpc_reader *p_private, void *dst, mpc_int32_t i_size )
398 {
399     demux_t *p_demux = (demux_t*)p_private->data;
400 #endif
401     return stream_Read( p_demux->s, dst, i_size );
402 }
403
404 #ifndef HAVE_MPC_MPCDEC_H
405 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
406 {
407     demux_t *p_demux = (demux_t*)p_private;
408 #else
409 static mpc_bool_t ReaderSeek( mpc_reader *p_private, mpc_int32_t i_offset )
410 {
411     demux_t *p_demux = (demux_t*)p_private->data;
412 #endif
413     return !stream_Seek( p_demux->s, i_offset );
414 }
415
416 #ifndef HAVE_MPC_MPCDEC_H
417 static mpc_int32_t ReaderTell( void *p_private)
418 {
419     demux_t *p_demux = (demux_t*)p_private;
420 #else
421 static mpc_int32_t ReaderTell( mpc_reader *p_private)
422 {
423     demux_t *p_demux = (demux_t*)p_private->data;
424 #endif
425     return stream_Tell( p_demux->s );
426 }
427
428 #ifndef HAVE_MPC_MPCDEC_H
429 static mpc_int32_t ReaderGetSize( void *p_private )
430 {
431     demux_t *p_demux = (demux_t*)p_private;
432 #else
433 static mpc_int32_t ReaderGetSize( mpc_reader *p_private )
434 {
435     demux_t *p_demux = (demux_t*)p_private->data;
436 #endif
437     return stream_Size( p_demux->s );
438 }
439
440 #ifndef HAVE_MPC_MPCDEC_H
441 static mpc_bool_t ReaderCanSeek( void *p_private )
442 {
443     demux_t *p_demux = (demux_t*)p_private;
444 #else
445 static mpc_bool_t ReaderCanSeek( mpc_reader *p_private )
446 {
447     demux_t *p_demux = (demux_t*)p_private->data;
448 #endif
449     bool b_canseek;
450
451     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
452     return b_canseek;
453 }
454