]> git.sesse.net Git - vlc/blob - modules/demux/mpc.c
A bit of headers cleanup
[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_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, "meta reader", 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_SetCodecName( p_sys->p_meta, psz_info );
241     //   ^^ doesn't exist (yet?) to set VLC_META_CODEC_NAME, so...
242     fprintf( stderr, "***** WARNING: Unhandled child meta\n"); 
243
244     return VLC_SUCCESS;
245 }
246
247 /*****************************************************************************
248  * Close: frees unused data
249  *****************************************************************************/
250 static void Close( vlc_object_t * p_this )
251 {
252     demux_t        *p_demux = (demux_t*)p_this;
253     demux_sys_t    *p_sys = p_demux->p_sys;
254
255     free( p_sys );
256 }
257
258 /*****************************************************************************
259  * Demux:
260  *****************************************************************************
261  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
262  *****************************************************************************/
263 static int Demux( demux_t *p_demux )
264 {
265     demux_sys_t *p_sys = p_demux->p_sys;
266     block_t     *p_data;
267     int i_ret;
268
269     p_data = block_New( p_demux,
270                         MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
271     i_ret = mpc_decoder_decode( &p_sys->decoder,
272                                (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
273                                NULL, NULL );
274     if( i_ret <= 0 )
275     {
276         block_Release( p_data );
277         return i_ret < 0 ? -1 : 0;
278     }
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             1 + I64C(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     vlc_meta_t *p_meta;
304
305     switch( i_query )
306     {
307         case DEMUX_GET_META:
308             p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
309             if( p_sys->p_meta )
310                 vlc_meta_Merge( p_meta, p_sys->p_meta ); 
311             else
312                 p_meta = NULL;
313             return VLC_SUCCESS;
314
315         case DEMUX_GET_LENGTH:
316             pi64 = (int64_t*)va_arg( args, int64_t * );
317             *pi64 = I64C(1000000) * p_sys->info.pcm_samples /
318                         p_sys->info.sample_freq;
319             return VLC_SUCCESS;
320
321         case DEMUX_GET_POSITION:
322             pf = (double*)va_arg( args, double * );
323             if( p_sys->info.pcm_samples > 0 )
324                 *pf = (double) p_sys->i_position /
325                       (double)p_sys->info.pcm_samples;
326             else
327                 *pf = 0.0;
328             return VLC_SUCCESS;
329
330         case DEMUX_GET_TIME:
331             pi64 = (int64_t*)va_arg( args, int64_t * );
332             *pi64 = I64C(1000000) * p_sys->i_position /
333                         p_sys->info.sample_freq;
334             return VLC_SUCCESS;
335
336         case DEMUX_SET_POSITION:
337             f = (double)va_arg( args, double );
338             i64 = (int64_t)(f * p_sys->info.pcm_samples);
339             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
340             {
341                 p_sys->i_position = i64;
342                 return VLC_SUCCESS;
343             }
344             return VLC_EGENERIC;
345
346         case DEMUX_SET_TIME:
347             i64 = (int64_t)va_arg( args, int64_t );
348             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
349             {
350                 p_sys->i_position = i64;
351                 return VLC_SUCCESS;
352             }
353             return VLC_EGENERIC;
354
355         default:
356             return VLC_EGENERIC;
357     }
358 }
359
360 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
361 {
362     demux_t *p_demux = (demux_t*)p_private;
363     return stream_Read( p_demux->s, dst, i_size );
364 }
365
366 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
367 {
368     demux_t *p_demux = (demux_t*)p_private;
369     return !stream_Seek( p_demux->s, i_offset );
370 }
371
372 mpc_int32_t ReaderTell( void *p_private)
373 {
374     demux_t *p_demux = (demux_t*)p_private;
375     return stream_Tell( p_demux->s );
376 }
377
378 mpc_int32_t ReaderGetSize( void *p_private )
379 {
380     demux_t *p_demux = (demux_t*)p_private;
381     return stream_Size( p_demux->s );
382 }
383
384 mpc_bool_t ReaderCanSeek( void *p_private )
385 {
386     demux_t *p_demux = (demux_t*)p_private;
387     vlc_bool_t b_canseek;
388
389     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
390     return b_canseek;
391 }
392