]> git.sesse.net Git - vlc/blob - modules/demux/mpc.c
contrib: remove speexdsp includedir patch
[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     if( p_sys->info.peak_title > 0 )
196     {
197         fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
198         fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.peak_title / 32767.0;
199         fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
200         fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.gain_title / 100.0;
201     }
202     if( p_sys->info.peak_album > 0 )
203     {
204         fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
205         fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.peak_album / 32767.0;
206         fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
207         fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.gain_album / 100.0;
208     }
209
210     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
211     if( !p_sys->p_es )
212         goto error;
213
214     return VLC_SUCCESS;
215
216 error:
217     free( p_sys );
218     return VLC_EGENERIC;
219 }
220
221 /*****************************************************************************
222  * Close: frees unused data
223  *****************************************************************************/
224 static void Close( vlc_object_t * p_this )
225 {
226     demux_t        *p_demux = (demux_t*)p_this;
227     demux_sys_t    *p_sys = p_demux->p_sys;
228
229 #ifdef HAVE_MPC_MPCDEC_H
230     if( p_sys->decoder )
231     mpc_demux_exit( p_sys->decoder );
232 #endif
233     free( p_sys );
234 }
235
236 /*****************************************************************************
237  * Demux:
238  *****************************************************************************
239  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
240  *****************************************************************************/
241 static int Demux( demux_t *p_demux )
242 {
243     demux_sys_t *p_sys = p_demux->p_sys;
244     block_t     *p_data;
245     int i_ret;
246 #ifdef HAVE_MPC_MPCDEC_H
247     mpc_frame_info frame;
248     mpc_status err;
249 #endif
250     p_data = block_Alloc( MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
251     if( !p_data )
252         return -1;
253
254 #ifndef HAVE_MPC_MPCDEC_H
255     i_ret = mpc_decoder_decode( &p_sys->decoder,
256                                 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
257                                 NULL, NULL );
258     if( i_ret <= 0 )
259     {
260         block_Release( p_data );
261         return i_ret < 0 ? -1 : 0;
262     }
263 #else
264     frame.buffer = (MPC_SAMPLE_FORMAT*)p_data->p_buffer;
265     err = mpc_demux_decode( p_sys->decoder, &frame );
266     if( err != MPC_STATUS_OK )
267     {
268         block_Release( p_data );
269         return -1;
270     }
271     else if( frame.bits == -1 )
272     {
273         block_Release( p_data );
274         return 0;
275     }
276
277     i_ret = frame.samples;
278 #endif
279
280     /* */
281     p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
282     p_data->i_dts = p_data->i_pts =
283             VLC_TS_0 + INT64_C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
284
285     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
286
287     es_out_Send( p_demux->out, p_sys->p_es, p_data );
288
289     /* */
290     p_sys->i_position += i_ret;
291
292     return 1;
293 }
294
295 /*****************************************************************************
296  * Control:
297  *****************************************************************************/
298 static int Control( demux_t *p_demux, int i_query, va_list args )
299 {
300     demux_sys_t *p_sys = p_demux->p_sys;
301     double   f, *pf;
302     int64_t i64, *pi64;
303     bool *pb_bool;
304
305     switch( i_query )
306     {
307         case DEMUX_HAS_UNSUPPORTED_META:
308             pb_bool = (bool*)va_arg( args, bool* );
309             *pb_bool = true;
310             return VLC_SUCCESS;
311
312         case DEMUX_GET_LENGTH:
313             pi64 = (int64_t*)va_arg( args, int64_t * );
314 #ifndef HAVE_MPC_MPCDEC_H
315             *pi64 = INT64_C(1000000) * p_sys->info.pcm_samples /
316                         p_sys->info.sample_freq;
317 #else
318             *pi64 = INT64_C(1000000) * (p_sys->info.samples -
319                                         p_sys->info.beg_silence) /
320                 p_sys->info.sample_freq;
321 #endif
322             return VLC_SUCCESS;
323
324         case DEMUX_GET_POSITION:
325             pf = (double*)va_arg( args, double * );
326 #ifndef HAVE_MPC_MPCDEC_H
327             if( p_sys->info.pcm_samples > 0 )
328                 *pf = (double) p_sys->i_position /
329                       (double)p_sys->info.pcm_samples;
330 #else
331             if( p_sys->info.samples - p_sys->info.beg_silence > 0)
332                 *pf = (double) p_sys->i_position /
333                       (double)(p_sys->info.samples - p_sys->info.beg_silence);
334 #endif
335             else
336                 *pf = 0.0;
337             return VLC_SUCCESS;
338
339         case DEMUX_GET_TIME:
340             pi64 = (int64_t*)va_arg( args, int64_t * );
341             *pi64 = INT64_C(1000000) * p_sys->i_position /
342                         p_sys->info.sample_freq;
343             return VLC_SUCCESS;
344
345         case DEMUX_SET_POSITION:
346             f = (double)va_arg( args, double );
347 #ifndef HAVE_MPC_MPCDEC_H
348             i64 = (int64_t)(f * p_sys->info.pcm_samples);
349             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
350 #else
351             i64 = (int64_t)(f * (p_sys->info.samples -
352                                  p_sys->info.beg_silence));
353             if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
354 #endif
355             {
356                 p_sys->i_position = i64;
357                 return VLC_SUCCESS;
358             }
359             return VLC_EGENERIC;
360
361         case DEMUX_SET_TIME:
362             i64 = (int64_t)va_arg( args, int64_t );
363 #ifndef HAVE_MPC_MPCDEC_H
364             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
365 #else
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         default:
375             return VLC_EGENERIC;
376     }
377 }
378
379 #ifndef HAVE_MPC_MPCDEC_H
380 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
381 {
382     demux_t *p_demux = (demux_t*)p_private;
383 #else
384 static mpc_int32_t ReaderRead( mpc_reader *p_private, void *dst, mpc_int32_t i_size )
385 {
386     demux_t *p_demux = (demux_t*)p_private->data;
387 #endif
388     return stream_Read( p_demux->s, dst, i_size );
389 }
390
391 #ifndef HAVE_MPC_MPCDEC_H
392 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
393 {
394     demux_t *p_demux = (demux_t*)p_private;
395 #else
396 static mpc_bool_t ReaderSeek( mpc_reader *p_private, mpc_int32_t i_offset )
397 {
398     demux_t *p_demux = (demux_t*)p_private->data;
399 #endif
400     return !stream_Seek( p_demux->s, i_offset );
401 }
402
403 #ifndef HAVE_MPC_MPCDEC_H
404 static mpc_int32_t ReaderTell( void *p_private)
405 {
406     demux_t *p_demux = (demux_t*)p_private;
407 #else
408 static mpc_int32_t ReaderTell( mpc_reader *p_private)
409 {
410     demux_t *p_demux = (demux_t*)p_private->data;
411 #endif
412     return stream_Tell( p_demux->s );
413 }
414
415 #ifndef HAVE_MPC_MPCDEC_H
416 static mpc_int32_t ReaderGetSize( void *p_private )
417 {
418     demux_t *p_demux = (demux_t*)p_private;
419 #else
420 static mpc_int32_t ReaderGetSize( mpc_reader *p_private )
421 {
422     demux_t *p_demux = (demux_t*)p_private->data;
423 #endif
424     return stream_Size( p_demux->s );
425 }
426
427 #ifndef HAVE_MPC_MPCDEC_H
428 static mpc_bool_t ReaderCanSeek( void *p_private )
429 {
430     demux_t *p_demux = (demux_t*)p_private;
431 #else
432 static mpc_bool_t ReaderCanSeek( mpc_reader *p_private )
433 {
434     demux_t *p_demux = (demux_t*)p_private->data;
435 #endif
436     bool b_canseek;
437
438     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
439     return b_canseek;
440 }
441