]> git.sesse.net Git - vlc/blob - modules/demux/mpc.c
49703dfd25ab0331e272b2e5fc35a2af0f30282a
[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: a52.c 11699 2005-07-11 08:52:14Z sam $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29 #include <vlc_codec.h>
30 #include <math.h>
31
32 #include <mpcdec/mpcdec.h>
33
34 /* TODO:
35  *  - test stream version 4..6
36  *  - test fixed float version
37  *  - ...
38  *
39  *  XXX:
40  *  It is done the ugly way (the demux does the decode stage... but it works
41  */
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 #define REPLAYGAIN_TYPE_TEXT N_("Replay Gain type" )
47 #define REPLAYGAIN_TYPE_LONGTEXT N_( "Replay Gain type" )
48
49 static int  Open  ( vlc_object_t * );
50 static void Close ( vlc_object_t * );
51
52 static int  pi_replaygain_type[] = { 0, 1, 2 };
53 static char *ppsz_replaygain_type[] = { N_("None"), N_("Title"), N_("Album") };
54
55 vlc_module_begin();
56     set_shortname( "MPC" );
57     set_description( _("MPC demuxer") );
58     set_category( CAT_INPUT );
59     set_subcategory( SUBCAT_INPUT_DEMUX );
60     set_capability( "demux2", 145 );
61
62     add_integer( "mpc-replaygain-type", 2, NULL,
63                 REPLAYGAIN_TYPE_TEXT, REPLAYGAIN_TYPE_LONGTEXT, VLC_FALSE );
64         change_integer_list( pi_replaygain_type, ppsz_replaygain_type, 0 );
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     mpc_decoder    decoder;
83     mpc_reader     reader;
84     mpc_streaminfo info;
85
86     /* */
87     vlc_meta_t     *p_meta;
88     int64_t        i_position;
89 };
90
91 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
92 mpc_bool_t  ReaderSeek( void *p_private, mpc_int32_t i_offset );
93 mpc_int32_t ReaderTell( void *p_private);
94 mpc_int32_t ReaderGetSize( void *p_private );
95 mpc_bool_t  ReaderCanSeek( void *p_private );
96
97 /*****************************************************************************
98  * Open: initializes ES structures
99  *****************************************************************************/
100 static int Open( vlc_object_t * p_this )
101 {
102     demux_t     *p_demux = (demux_t*)p_this;
103     demux_sys_t *p_sys;
104     char        psz_info[4096];
105     es_format_t fmt;
106     uint8_t     *p_peek;
107     module_t    *p_id3;
108
109     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
110         return VLC_EGENERIC;
111
112     if( memcmp( p_peek, "MP+", 3 ) )
113     {
114         /* for v4..6 we check extention file */
115         const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
116
117         if( i_version  < 4 || i_version > 6 )
118             return VLC_EGENERIC;
119
120         if( !p_demux->psz_demux || strcmp( p_demux->psz_demux, "mpc" ) )
121         {
122             /* Check file name extension */
123             int i_len;
124             if( !p_demux->psz_path )
125                 return VLC_EGENERIC;
126
127             i_len = strlen( p_demux->psz_path );
128             if( i_len < 4 ||
129                 ( strcasecmp( &p_demux->psz_path[i_len-4], ".mpc" ) &&
130                   strcasecmp( &p_demux->psz_path[i_len-4], ".mp+" ) &&
131                   strcasecmp( &p_demux->psz_path[i_len-4], ".mpp" ) ) )
132                 return VLC_EGENERIC;
133         }
134     }
135
136     /* */
137     p_sys = malloc( sizeof( demux_sys_t ) );
138     memset( p_sys, 0, sizeof(demux_sys_t) );
139
140     p_sys->i_position = 0;
141
142     p_sys->reader.read = ReaderRead;
143     p_sys->reader.seek = ReaderSeek;
144     p_sys->reader.tell = ReaderTell;
145     p_sys->reader.get_size = ReaderGetSize;
146     p_sys->reader.canseek = ReaderCanSeek;
147     p_sys->reader.data = p_demux;
148
149     /* Load info */
150     mpc_streaminfo_init( &p_sys->info );
151     if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
152     {
153         /* invalid file */
154         free( p_sys );
155         return VLC_EGENERIC;
156     }
157
158     /* */
159     mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
160     if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
161     {
162         /* */
163         free( p_sys );
164         return VLC_EGENERIC;
165     }
166
167     /* Handle reaply gain */
168     if( p_sys->info.peak_title != 32767 )
169     {
170         int i_type = var_CreateGetInteger( p_demux, "mpc-replaygain-type" );
171         int gain;
172         int peak;
173
174         if( i_type == 2 )       // album
175         {
176             gain = p_sys->info.gain_album;
177             peak = p_sys->info.peak_album;
178         }
179         else if( i_type == 1 )  // title
180         {
181             gain = p_sys->info.gain_title;
182             peak = p_sys->info.peak_title;
183         }
184         else
185         {
186             gain = 0;
187             peak = 0;
188         }
189
190         if( gain )
191         {
192             double g = pow( 10, (double)gain / 2000.0 );
193             double gmax = (double)32767.0 / (peak+1);
194             if( g > gmax )
195                 g = gmax;
196
197             msg_Dbg( p_demux, "Using reaply gain factor %f", g );
198             mpc_decoder_scale_output( &p_sys->decoder, g );
199         }
200     }
201
202     /* Fill p_demux fields */
203     p_demux->pf_demux = Demux;
204     p_demux->pf_control = Control;
205     p_demux->p_sys = p_sys;
206
207     /* */
208 #ifndef MPC_FIXED_POINT
209     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', '3', '2' ) );
210 #else
211 #   ifdef WORDS_BIGENDIAN
212     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'b' ) );
213 #   else
214     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'l' ) );
215 #   endif
216 #endif
217     fmt.audio.i_channels = p_sys->info.channels;
218     fmt.audio.i_rate = p_sys->info.sample_freq;
219     fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
220     fmt.audio.i_bitspersample = 32;
221     fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
222                     fmt.audio.i_bitspersample;
223     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
224
225
226     /* Parse possible id3 header */
227     if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
228     {
229         p_sys->p_meta = (vlc_meta_t *)p_demux->p_private;
230         p_demux->p_private = NULL;
231         module_Unneed( p_demux, p_id3 );
232     }
233
234     if( !p_sys->p_meta )
235         p_sys->p_meta = vlc_meta_New();
236
237     sprintf( psz_info, "Musepack v%d", p_sys->info.stream_version );
238     vlc_meta_Add( p_sys->p_meta, VLC_META_CODEC_NAME, psz_info );
239
240     return VLC_SUCCESS;
241 }
242
243 /*****************************************************************************
244  * Close: frees unused data
245  *****************************************************************************/
246 static void Close( vlc_object_t * p_this )
247 {
248     demux_t        *p_demux = (demux_t*)p_this;
249     demux_sys_t    *p_sys = p_demux->p_sys;
250
251     free( p_sys );
252 }
253
254 /*****************************************************************************
255  * Demux:
256  *****************************************************************************
257  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
258  *****************************************************************************/
259 static int Demux( demux_t *p_demux )
260 {
261     demux_sys_t *p_sys = p_demux->p_sys;
262     block_t     *p_data;
263     int i_ret;
264
265     p_data = block_New( p_demux,
266                         MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
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
276     /* */
277     p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
278     p_data->i_dts = p_data->i_pts =
279             1 + I64C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
280
281     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
282
283     es_out_Send( p_demux->out, p_sys->p_es, p_data );
284
285     /* */
286     p_sys->i_position += i_ret;
287
288     return 1;
289 }
290
291 /*****************************************************************************
292  * Control:
293  *****************************************************************************/
294 static int Control( demux_t *p_demux, int i_query, va_list args )
295 {
296     demux_sys_t *p_sys = p_demux->p_sys;
297     double   f, *pf;
298     int64_t i64, *pi64;
299     vlc_meta_t **pp_meta;
300
301     switch( i_query )
302     {
303         case DEMUX_GET_META:
304             pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
305             if( p_sys->p_meta )
306                 *pp_meta = vlc_meta_Duplicate( p_sys->p_meta );
307             else
308                 *pp_meta = NULL;
309             return VLC_SUCCESS;
310
311         case DEMUX_GET_LENGTH:
312             pi64 = (int64_t*)va_arg( args, int64_t * );
313             *pi64 = I64C(1000000) * p_sys->info.pcm_samples /
314                         p_sys->info.sample_freq;
315             return VLC_SUCCESS;
316
317         case DEMUX_GET_POSITION:
318             pf = (double*)va_arg( args, double * );
319             if( p_sys->info.pcm_samples > 0 )
320                 *pf = (double) p_sys->i_position /
321                       (double)p_sys->info.pcm_samples;
322             else
323                 *pf = 0.0;
324             return VLC_SUCCESS;
325
326         case DEMUX_GET_TIME:
327             pi64 = (int64_t*)va_arg( args, int64_t * );
328             *pi64 = I64C(1000000) * p_sys->i_position /
329                         p_sys->info.sample_freq;
330             return VLC_SUCCESS;
331
332         case DEMUX_SET_POSITION:
333             f = (double)va_arg( args, double );
334             i64 = (int64_t)(f * p_sys->info.pcm_samples);
335             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
336             {
337                 p_sys->i_position = i64;
338                 return VLC_SUCCESS;
339             }
340             return VLC_EGENERIC;
341
342         case DEMUX_SET_TIME:
343             i64 = (int64_t)va_arg( args, int64_t );
344             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
345             {
346                 p_sys->i_position = i64;
347                 return VLC_SUCCESS;
348             }
349             return VLC_EGENERIC;
350
351         default:
352             return VLC_EGENERIC;
353     }
354 }
355
356 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
357 {
358     demux_t *p_demux = (demux_t*)p_private;
359     return stream_Read( p_demux->s, dst, i_size );
360 }
361
362 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
363 {
364     demux_t *p_demux = (demux_t*)p_private;
365     return !stream_Seek( p_demux->s, i_offset );
366 }
367
368 mpc_int32_t ReaderTell( void *p_private)
369 {
370     demux_t *p_demux = (demux_t*)p_private;
371     return stream_Tell( p_demux->s );
372 }
373
374 mpc_int32_t ReaderGetSize( void *p_private )
375 {
376     demux_t *p_demux = (demux_t*)p_private;
377     return stream_Size( p_demux->s );
378 }
379
380 mpc_bool_t ReaderCanSeek( void *p_private )
381 {
382     demux_t *p_demux = (demux_t*)p_private;
383     vlc_bool_t b_canseek;
384
385     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
386     return b_canseek;
387 }
388