]> git.sesse.net Git - vlc/blob - modules/demux/mpc.c
Demuxers: do not load "meta reader" module if the input item has already been preparsed.
[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_input.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 vlc_module_begin();
56     set_category( CAT_INPUT );
57     set_subcategory( SUBCAT_INPUT_DEMUX );
58     set_description( _("MusePack demuxer") );
59     set_capability( "demux2", 145 );
60
61     set_callbacks( Open, Close );
62     add_shortcut( "mpc" );
63 vlc_module_end();
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int Demux  ( demux_t * );
69 static int Control( demux_t *, int, va_list );
70
71 struct demux_sys_t
72 {
73     /* */
74     es_out_id_t *p_es;
75
76     /* */
77     mpc_decoder    decoder;
78     mpc_reader     reader;
79     mpc_streaminfo info;
80
81     /* */
82     vlc_meta_t     *p_meta;
83     int64_t        i_position;
84 };
85
86 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
87 mpc_bool_t  ReaderSeek( void *p_private, mpc_int32_t i_offset );
88 mpc_int32_t ReaderTell( void *p_private);
89 mpc_int32_t ReaderGetSize( void *p_private );
90 mpc_bool_t  ReaderCanSeek( void *p_private );
91
92 /*****************************************************************************
93  * Open: initializes ES structures
94  *****************************************************************************/
95 static int Open( vlc_object_t * p_this )
96 {
97     demux_t     *p_demux = (demux_t*)p_this;
98     demux_sys_t *p_sys;
99     es_format_t fmt;
100     const uint8_t *p_peek;
101     module_t    *p_id3;
102
103     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
104         return VLC_EGENERIC;
105
106     if( memcmp( p_peek, "MP+", 3 ) )
107     {
108         /* for v4..6 we check extension file */
109         const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
110
111         if( i_version  < 4 || i_version > 6 )
112             return VLC_EGENERIC;
113
114         if( !p_demux->b_force )
115         {
116             /* Check file name extension */
117             if( !demux2_IsPathExtension( p_demux, ".mpc" ) &&
118                 !demux2_IsPathExtension( p_demux, ".mp+" ) &&
119                 !demux2_IsPathExtension( p_demux, ".mpp" ) )
120                 return VLC_EGENERIC;
121         }
122     }
123
124     /* */
125     p_sys = malloc( sizeof( demux_sys_t ) );
126     memset( p_sys, 0, sizeof(demux_sys_t) );
127
128     p_sys->i_position = 0;
129
130     p_sys->reader.read = ReaderRead;
131     p_sys->reader.seek = ReaderSeek;
132     p_sys->reader.tell = ReaderTell;
133     p_sys->reader.get_size = ReaderGetSize;
134     p_sys->reader.canseek = ReaderCanSeek;
135     p_sys->reader.data = p_demux;
136
137     /* Load info */
138     mpc_streaminfo_init( &p_sys->info );
139     if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
140     {
141         /* invalid file */
142         free( p_sys );
143         return VLC_EGENERIC;
144     }
145
146     /* */
147     mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
148     if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
149     {
150         /* */
151         free( p_sys );
152         return VLC_EGENERIC;
153     }
154
155     /* Fill p_demux fields */
156     p_demux->pf_demux = Demux;
157     p_demux->pf_control = Control;
158     p_demux->p_sys = p_sys;
159
160     /* */
161 #ifndef MPC_FIXED_POINT
162     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', '3', '2' ) );
163 #else
164 #   ifdef WORDS_BIGENDIAN
165     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'b' ) );
166 #   else
167     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'l' ) );
168 #   endif
169 #endif
170     fmt.audio.i_channels = p_sys->info.channels;
171     fmt.audio.i_rate = p_sys->info.sample_freq;
172     fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
173     fmt.audio.i_bitspersample = 32;
174     fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
175                     fmt.audio.i_bitspersample;
176     if( p_sys->info.peak_title > 0 )
177     {
178         fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
179         fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.peak_title / 32767.0;
180         fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
181         fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.gain_title / 100.0;
182     }
183     if( p_sys->info.peak_album > 0 )
184     {
185         fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
186         fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.peak_album / 32767.0;
187         fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
188         fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.gain_album / 100.0;
189     }
190
191     /* Parse possible id3 header */
192     input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
193     if( p_input )
194     {
195         if( !( input_GetItem( p_input )->p_meta->i_status & ITEM_PREPARSED ) )
196         {
197             p_demux->p_private = malloc( sizeof( demux_meta_t ) );
198             if( !p_demux->p_private )
199             {
200                 vlc_object_release( p_input );
201                 return VLC_ENOMEM;
202             }
203             if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
204             {
205                 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
206                 p_sys->p_meta = p_demux_meta->p_meta;
207                 module_Unneed( p_demux, p_id3 );
208                 int i;
209                 for( i = 0; i < p_demux_meta->i_attachments; i++ )
210                     free( p_demux_meta->attachments[i] );
211                 TAB_CLEAN( p_demux_meta->i_attachments,
212                             p_demux_meta->attachments );
213             }
214             free( p_demux->p_private );
215         }
216         vlc_object_release( p_input );
217     }
218
219     if( !p_sys->p_meta )
220         p_sys->p_meta = vlc_meta_New();
221
222     vlc_audio_replay_gain_MergeFromMeta( &fmt.audio_replay_gain, p_sys->p_meta );
223     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
224
225     return VLC_SUCCESS;
226 }
227
228 /*****************************************************************************
229  * Close: frees unused data
230  *****************************************************************************/
231 static void Close( vlc_object_t * p_this )
232 {
233     demux_t        *p_demux = (demux_t*)p_this;
234     demux_sys_t    *p_sys = p_demux->p_sys;
235
236     free( p_sys );
237 }
238
239 /*****************************************************************************
240  * Demux:
241  *****************************************************************************
242  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
243  *****************************************************************************/
244 static int Demux( demux_t *p_demux )
245 {
246     demux_sys_t *p_sys = p_demux->p_sys;
247     block_t     *p_data;
248     int i_ret;
249
250     p_data = block_New( p_demux,
251                         MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
252     i_ret = mpc_decoder_decode( &p_sys->decoder,
253                                (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
254                                NULL, NULL );
255     if( i_ret <= 0 )
256     {
257         block_Release( p_data );
258         return i_ret < 0 ? -1 : 0;
259     }
260
261     /* */
262     p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
263     p_data->i_dts = p_data->i_pts =
264             1 + I64C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
265
266     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
267
268     es_out_Send( p_demux->out, p_sys->p_es, p_data );
269
270     /* */
271     p_sys->i_position += i_ret;
272
273     return 1;
274 }
275
276 /*****************************************************************************
277  * Control:
278  *****************************************************************************/
279 static int Control( demux_t *p_demux, int i_query, va_list args )
280 {
281     demux_sys_t *p_sys = p_demux->p_sys;
282     double   f, *pf;
283     int64_t i64, *pi64;
284     vlc_meta_t *p_meta;
285
286     switch( i_query )
287     {
288         case DEMUX_GET_META:
289             p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
290             if( p_sys->p_meta )
291                 vlc_meta_Merge( p_meta, p_sys->p_meta );
292             else
293                 p_meta = NULL;
294             return VLC_SUCCESS;
295
296         case DEMUX_GET_LENGTH:
297             pi64 = (int64_t*)va_arg( args, int64_t * );
298             *pi64 = I64C(1000000) * p_sys->info.pcm_samples /
299                         p_sys->info.sample_freq;
300             return VLC_SUCCESS;
301
302         case DEMUX_GET_POSITION:
303             pf = (double*)va_arg( args, double * );
304             if( p_sys->info.pcm_samples > 0 )
305                 *pf = (double) p_sys->i_position /
306                       (double)p_sys->info.pcm_samples;
307             else
308                 *pf = 0.0;
309             return VLC_SUCCESS;
310
311         case DEMUX_GET_TIME:
312             pi64 = (int64_t*)va_arg( args, int64_t * );
313             *pi64 = I64C(1000000) * p_sys->i_position /
314                         p_sys->info.sample_freq;
315             return VLC_SUCCESS;
316
317         case DEMUX_SET_POSITION:
318             f = (double)va_arg( args, double );
319             i64 = (int64_t)(f * p_sys->info.pcm_samples);
320             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
321             {
322                 p_sys->i_position = i64;
323                 return VLC_SUCCESS;
324             }
325             return VLC_EGENERIC;
326
327         case DEMUX_SET_TIME:
328             i64 = (int64_t)va_arg( args, int64_t );
329             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
330             {
331                 p_sys->i_position = i64;
332                 return VLC_SUCCESS;
333             }
334             return VLC_EGENERIC;
335
336         default:
337             return VLC_EGENERIC;
338     }
339 }
340
341 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
342 {
343     demux_t *p_demux = (demux_t*)p_private;
344     return stream_Read( p_demux->s, dst, i_size );
345 }
346
347 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
348 {
349     demux_t *p_demux = (demux_t*)p_private;
350     return !stream_Seek( p_demux->s, i_offset );
351 }
352
353 mpc_int32_t ReaderTell( void *p_private)
354 {
355     demux_t *p_demux = (demux_t*)p_private;
356     return stream_Tell( p_demux->s );
357 }
358
359 mpc_int32_t ReaderGetSize( void *p_private )
360 {
361     demux_t *p_demux = (demux_t*)p_private;
362     return stream_Size( p_demux->s );
363 }
364
365 mpc_bool_t ReaderCanSeek( void *p_private )
366 {
367     demux_t *p_demux = (demux_t*)p_private;
368     vlc_bool_t b_canseek;
369
370     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
371     return b_canseek;
372 }
373