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