]> git.sesse.net Git - vlc/blob - modules/demux/mpc.c
« En essayant continuellement on finit par réussir. Donc : plus ça rate, plus on...
[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 #include <vlc/vlc.h>
28 #include <vlc_demux.h>
29 #include <vlc_meta.h>
30 #include <vlc_codec.h>
31 #include <math.h>
32
33 #include <mpcdec/mpcdec.h>
34
35 /* TODO:
36  *  - test stream version 4..6
37  *  - test fixed float version
38  *  - ...
39  *
40  *  XXX:
41  *  It is done the ugly way (the demux does the decode stage... but it works
42  */
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 #define REPLAYGAIN_TYPE_TEXT N_("Replay Gain type" )
48 #define REPLAYGAIN_TYPE_LONGTEXT N_( "Musepack can have a title-specific " \
49               "replay gain (volume control) or an album-specific one. "  \
50               "Choose which type you want to use" )
51
52 static int  Open  ( vlc_object_t * );
53 static void Close ( vlc_object_t * );
54
55 static int  pi_replaygain_type[] = { 0, 1, 2 };
56 static char *ppsz_replaygain_type[] = { N_("None"), N_("Title"), N_("Album") };
57
58 vlc_module_begin();
59     set_shortname( "MPC" );
60     set_description( _("MusePack demuxer") );
61     set_category( CAT_INPUT );
62     set_subcategory( SUBCAT_INPUT_DEMUX );
63     set_capability( "demux2", 145 );
64
65     add_integer( "mpc-replaygain-type", 2, NULL,
66                 REPLAYGAIN_TYPE_TEXT, REPLAYGAIN_TYPE_LONGTEXT, VLC_FALSE );
67         change_integer_list( pi_replaygain_type, ppsz_replaygain_type, 0 );
68
69     set_callbacks( Open, Close );
70     add_shortcut( "mpc" );
71 vlc_module_end();
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76 static int Demux  ( demux_t * );
77 static int Control( demux_t *, int, va_list );
78
79 struct demux_sys_t
80 {
81     /* */
82     es_out_id_t *p_es;
83
84     /* */
85     mpc_decoder    decoder;
86     mpc_reader     reader;
87     mpc_streaminfo info;
88
89     /* */
90     vlc_meta_t     *p_meta;
91     int64_t        i_position;
92 };
93
94 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
95 mpc_bool_t  ReaderSeek( void *p_private, mpc_int32_t i_offset );
96 mpc_int32_t ReaderTell( void *p_private);
97 mpc_int32_t ReaderGetSize( void *p_private );
98 mpc_bool_t  ReaderCanSeek( void *p_private );
99
100 /*****************************************************************************
101  * Open: initializes ES structures
102  *****************************************************************************/
103 static int Open( vlc_object_t * p_this )
104 {
105     demux_t     *p_demux = (demux_t*)p_this;
106     demux_sys_t *p_sys;
107     char        psz_info[4096];
108     es_format_t fmt;
109     uint8_t     *p_peek;
110     module_t    *p_id3;
111
112     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
113         return VLC_EGENERIC;
114
115     if( memcmp( p_peek, "MP+", 3 ) )
116     {
117         /* for v4..6 we check extension file */
118         const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
119
120         if( i_version  < 4 || i_version > 6 )
121             return VLC_EGENERIC;
122
123         if( !p_demux->psz_demux || strcmp( p_demux->psz_demux, "mpc" ) )
124         {
125             /* Check file name extension */
126             int i_len;
127             if( !p_demux->psz_path )
128                 return VLC_EGENERIC;
129
130             i_len = strlen( p_demux->psz_path );
131             if( i_len < 4 ||
132                 ( strcasecmp( &p_demux->psz_path[i_len-4], ".mpc" ) &&
133                   strcasecmp( &p_demux->psz_path[i_len-4], ".mp+" ) &&
134                   strcasecmp( &p_demux->psz_path[i_len-4], ".mpp" ) ) )
135                 return VLC_EGENERIC;
136         }
137     }
138
139     /* */
140     p_sys = malloc( sizeof( demux_sys_t ) );
141     memset( p_sys, 0, sizeof(demux_sys_t) );
142
143     p_sys->i_position = 0;
144
145     p_sys->reader.read = ReaderRead;
146     p_sys->reader.seek = ReaderSeek;
147     p_sys->reader.tell = ReaderTell;
148     p_sys->reader.get_size = ReaderGetSize;
149     p_sys->reader.canseek = ReaderCanSeek;
150     p_sys->reader.data = p_demux;
151
152     /* Load info */
153     mpc_streaminfo_init( &p_sys->info );
154     if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
155     {
156         /* invalid file */
157         free( p_sys );
158         return VLC_EGENERIC;
159     }
160
161     /* */
162     mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
163     if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
164     {
165         /* */
166         free( p_sys );
167         return VLC_EGENERIC;
168     }
169
170     /* Handle reaply gain */
171     if( p_sys->info.peak_title != 32767 )
172     {
173         int i_type = var_CreateGetInteger( p_demux, "mpc-replaygain-type" );
174         int gain;
175         int peak;
176
177         if( i_type == 2 )       // album
178         {
179             gain = p_sys->info.gain_album;
180             peak = p_sys->info.peak_album;
181         }
182         else if( i_type == 1 )  // title
183         {
184             gain = p_sys->info.gain_title;
185             peak = p_sys->info.peak_title;
186         }
187         else
188         {
189             gain = 0;
190             peak = 0;
191         }
192
193         if( gain )
194         {
195             double g = pow( 10, (double)gain / 2000.0 );
196             double gmax = (double)32767.0 / (peak+1);
197             if( g > gmax )
198                 g = gmax;
199
200             msg_Dbg( p_demux, "Using reaply gain factor %f", g );
201             mpc_decoder_scale_output( &p_sys->decoder, g );
202         }
203     }
204
205     /* Fill p_demux fields */
206     p_demux->pf_demux = Demux;
207     p_demux->pf_control = Control;
208     p_demux->p_sys = p_sys;
209
210     /* */
211 #ifndef MPC_FIXED_POINT
212     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', '3', '2' ) );
213 #else
214 #   ifdef WORDS_BIGENDIAN
215     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'b' ) );
216 #   else
217     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'l' ) );
218 #   endif
219 #endif
220     fmt.audio.i_channels = p_sys->info.channels;
221     fmt.audio.i_rate = p_sys->info.sample_freq;
222     fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
223     fmt.audio.i_bitspersample = 32;
224     fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
225                     fmt.audio.i_bitspersample;
226     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
227
228
229     /* Parse possible id3 header */
230     if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
231     {
232         p_sys->p_meta = (vlc_meta_t *)p_demux->p_private;
233         p_demux->p_private = NULL;
234         module_Unneed( p_demux, p_id3 );
235     }
236
237     if( !p_sys->p_meta )
238         p_sys->p_meta = vlc_meta_New();
239
240     sprintf( psz_info, "Musepack v%d", p_sys->info.stream_version );
241     //vlc_meta_SetCodecName( p_sys->p_meta, psz_info );
242     //   ^^ doesn't exist (yet?) to set VLC_META_CODEC_NAME, so...
243     fprintf( stderr, "***** WARNING: Unhandled child meta\n"); 
244
245     return VLC_SUCCESS;
246 }
247
248 /*****************************************************************************
249  * Close: frees unused data
250  *****************************************************************************/
251 static void Close( vlc_object_t * p_this )
252 {
253     demux_t        *p_demux = (demux_t*)p_this;
254     demux_sys_t    *p_sys = p_demux->p_sys;
255
256     free( p_sys );
257 }
258
259 /*****************************************************************************
260  * Demux:
261  *****************************************************************************
262  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
263  *****************************************************************************/
264 static int Demux( demux_t *p_demux )
265 {
266     demux_sys_t *p_sys = p_demux->p_sys;
267     block_t     *p_data;
268     int i_ret;
269
270     p_data = block_New( p_demux,
271                         MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
272     i_ret = mpc_decoder_decode( &p_sys->decoder,
273                                (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
274                                NULL, NULL );
275     if( i_ret <= 0 )
276     {
277         block_Release( p_data );
278         return i_ret < 0 ? -1 : 0;
279     }
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             1 + I64C(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     vlc_meta_t *p_meta;
305
306     switch( i_query )
307     {
308         case DEMUX_GET_META:
309             p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
310             if( p_sys->p_meta )
311                 vlc_meta_Merge( p_meta, p_sys->p_meta ); 
312             else
313                 p_meta = NULL;
314             return VLC_SUCCESS;
315
316         case DEMUX_GET_LENGTH:
317             pi64 = (int64_t*)va_arg( args, int64_t * );
318             *pi64 = I64C(1000000) * p_sys->info.pcm_samples /
319                         p_sys->info.sample_freq;
320             return VLC_SUCCESS;
321
322         case DEMUX_GET_POSITION:
323             pf = (double*)va_arg( args, double * );
324             if( p_sys->info.pcm_samples > 0 )
325                 *pf = (double) p_sys->i_position /
326                       (double)p_sys->info.pcm_samples;
327             else
328                 *pf = 0.0;
329             return VLC_SUCCESS;
330
331         case DEMUX_GET_TIME:
332             pi64 = (int64_t*)va_arg( args, int64_t * );
333             *pi64 = I64C(1000000) * p_sys->i_position /
334                         p_sys->info.sample_freq;
335             return VLC_SUCCESS;
336
337         case DEMUX_SET_POSITION:
338             f = (double)va_arg( args, double );
339             i64 = (int64_t)(f * p_sys->info.pcm_samples);
340             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
341             {
342                 p_sys->i_position = i64;
343                 return VLC_SUCCESS;
344             }
345             return VLC_EGENERIC;
346
347         case DEMUX_SET_TIME:
348             i64 = (int64_t)va_arg( args, int64_t );
349             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
350             {
351                 p_sys->i_position = i64;
352                 return VLC_SUCCESS;
353             }
354             return VLC_EGENERIC;
355
356         default:
357             return VLC_EGENERIC;
358     }
359 }
360
361 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
362 {
363     demux_t *p_demux = (demux_t*)p_private;
364     return stream_Read( p_demux->s, dst, i_size );
365 }
366
367 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
368 {
369     demux_t *p_demux = (demux_t*)p_private;
370     return !stream_Seek( p_demux->s, i_offset );
371 }
372
373 mpc_int32_t ReaderTell( void *p_private)
374 {
375     demux_t *p_demux = (demux_t*)p_private;
376     return stream_Tell( p_demux->s );
377 }
378
379 mpc_int32_t ReaderGetSize( void *p_private )
380 {
381     demux_t *p_demux = (demux_t*)p_private;
382     return stream_Size( p_demux->s );
383 }
384
385 mpc_bool_t ReaderCanSeek( void *p_private )
386 {
387     demux_t *p_demux = (demux_t*)p_private;
388     vlc_bool_t b_canseek;
389
390     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
391     return b_canseek;
392 }
393