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