]> git.sesse.net Git - vlc/blob - modules/demux/mpc.c
Use var_Inherit* instead of var_CreateGet*.
[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_New( p_demux,
251                         MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
252     if( !p_data )
253         return -1;
254
255 #ifndef HAVE_MPC_MPCDEC_H
256     i_ret = mpc_decoder_decode( &p_sys->decoder,
257                                 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
258                                 NULL, NULL );
259     if( i_ret <= 0 )
260     {
261         block_Release( p_data );
262         return i_ret < 0 ? -1 : 0;
263     }
264 #else
265     frame.buffer = (MPC_SAMPLE_FORMAT*)p_data->p_buffer;
266     err = mpc_demux_decode( p_sys->decoder, &frame );
267     if( err != MPC_STATUS_OK )
268     {
269         block_Release( p_data );
270         return -1;
271     }
272     else if( frame.bits == -1 )
273     {
274         block_Release( p_data );
275         return 0;
276     }
277
278     i_ret = frame.samples;
279 #endif
280
281     /* */
282     p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
283     p_data->i_dts = p_data->i_pts =
284             VLC_TS_0 + INT64_C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
285
286     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
287
288     es_out_Send( p_demux->out, p_sys->p_es, p_data );
289
290     /* */
291     p_sys->i_position += i_ret;
292
293     return 1;
294 }
295
296 /*****************************************************************************
297  * Control:
298  *****************************************************************************/
299 static int Control( demux_t *p_demux, int i_query, va_list args )
300 {
301     demux_sys_t *p_sys = p_demux->p_sys;
302     double   f, *pf;
303     int64_t i64, *pi64;
304     bool *pb_bool;
305
306     switch( i_query )
307     {
308         case DEMUX_HAS_UNSUPPORTED_META:
309             pb_bool = (bool*)va_arg( args, bool* );
310             *pb_bool = true;
311             return VLC_SUCCESS;
312
313         case DEMUX_GET_LENGTH:
314             pi64 = (int64_t*)va_arg( args, int64_t * );
315 #ifndef HAVE_MPC_MPCDEC_H
316             *pi64 = INT64_C(1000000) * p_sys->info.pcm_samples /
317                         p_sys->info.sample_freq;
318 #else
319             *pi64 = INT64_C(1000000) * (p_sys->info.samples -
320                                         p_sys->info.beg_silence) /
321                 p_sys->info.sample_freq;
322 #endif
323             return VLC_SUCCESS;
324
325         case DEMUX_GET_POSITION:
326             pf = (double*)va_arg( args, double * );
327 #ifndef HAVE_MPC_MPCDEC_H
328             if( p_sys->info.pcm_samples > 0 )
329                 *pf = (double) p_sys->i_position /
330                       (double)p_sys->info.pcm_samples;
331 #else
332             if( p_sys->info.samples - p_sys->info.beg_silence > 0)
333                 *pf = (double) p_sys->i_position /
334                       (double)(p_sys->info.samples - p_sys->info.beg_silence);
335 #endif
336             else
337                 *pf = 0.0;
338             return VLC_SUCCESS;
339
340         case DEMUX_GET_TIME:
341             pi64 = (int64_t*)va_arg( args, int64_t * );
342             *pi64 = INT64_C(1000000) * p_sys->i_position /
343                         p_sys->info.sample_freq;
344             return VLC_SUCCESS;
345
346         case DEMUX_SET_POSITION:
347             f = (double)va_arg( args, double );
348 #ifndef HAVE_MPC_MPCDEC_H
349             i64 = (int64_t)(f * p_sys->info.pcm_samples);
350             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
351 #else
352             i64 = (int64_t)(f * (p_sys->info.samples -
353                                  p_sys->info.beg_silence));
354             if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
355 #endif
356             {
357                 p_sys->i_position = i64;
358                 return VLC_SUCCESS;
359             }
360             return VLC_EGENERIC;
361
362         case DEMUX_SET_TIME:
363             i64 = (int64_t)va_arg( args, int64_t );
364 #ifndef HAVE_MPC_MPCDEC_H
365             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
366 #else
367              if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
368 #endif
369             {
370                 p_sys->i_position = i64;
371                 return VLC_SUCCESS;
372             }
373             return VLC_EGENERIC;
374
375         default:
376             return VLC_EGENERIC;
377     }
378 }
379
380 #ifndef HAVE_MPC_MPCDEC_H
381 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
382 {
383     demux_t *p_demux = (demux_t*)p_private;
384 #else
385 static mpc_int32_t ReaderRead( mpc_reader *p_private, void *dst, mpc_int32_t i_size )
386 {
387     demux_t *p_demux = (demux_t*)p_private->data;
388 #endif
389     return stream_Read( p_demux->s, dst, i_size );
390 }
391
392 #ifndef HAVE_MPC_MPCDEC_H
393 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
394 {
395     demux_t *p_demux = (demux_t*)p_private;
396 #else
397 static mpc_bool_t ReaderSeek( mpc_reader *p_private, mpc_int32_t i_offset )
398 {
399     demux_t *p_demux = (demux_t*)p_private->data;
400 #endif
401     return !stream_Seek( p_demux->s, i_offset );
402 }
403
404 #ifndef HAVE_MPC_MPCDEC_H
405 static mpc_int32_t ReaderTell( void *p_private)
406 {
407     demux_t *p_demux = (demux_t*)p_private;
408 #else
409 static mpc_int32_t ReaderTell( mpc_reader *p_private)
410 {
411     demux_t *p_demux = (demux_t*)p_private->data;
412 #endif
413     return stream_Tell( p_demux->s );
414 }
415
416 #ifndef HAVE_MPC_MPCDEC_H
417 static mpc_int32_t ReaderGetSize( void *p_private )
418 {
419     demux_t *p_demux = (demux_t*)p_private;
420 #else
421 static mpc_int32_t ReaderGetSize( mpc_reader *p_private )
422 {
423     demux_t *p_demux = (demux_t*)p_private->data;
424 #endif
425     return stream_Size( p_demux->s );
426 }
427
428 #ifndef HAVE_MPC_MPCDEC_H
429 static mpc_bool_t ReaderCanSeek( void *p_private )
430 {
431     demux_t *p_demux = (demux_t*)p_private;
432 #else
433 static mpc_bool_t ReaderCanSeek( mpc_reader *p_private )
434 {
435     demux_t *p_demux = (demux_t*)p_private->data;
436 #endif
437     bool b_canseek;
438
439     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
440     return b_canseek;
441 }
442