]> git.sesse.net Git - vlc/blob - modules/demux/mpc.c
Removes trailing spaces. Removes tabs.
[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     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->psz_demux || strcmp( p_demux->psz_demux, "mpc" ) )
115         {
116             /* Check file name extension */
117             int i_len;
118             if( !p_demux->psz_path )
119                 return VLC_EGENERIC;
120
121             i_len = strlen( p_demux->psz_path );
122             if( i_len < 4 ||
123                 ( strcasecmp( &p_demux->psz_path[i_len-4], ".mpc" ) &&
124                   strcasecmp( &p_demux->psz_path[i_len-4], ".mp+" ) &&
125                   strcasecmp( &p_demux->psz_path[i_len-4], ".mpp" ) ) )
126                 return VLC_EGENERIC;
127         }
128     }
129
130     /* */
131     p_sys = malloc( sizeof( demux_sys_t ) );
132     memset( p_sys, 0, sizeof(demux_sys_t) );
133
134     p_sys->i_position = 0;
135
136     p_sys->reader.read = ReaderRead;
137     p_sys->reader.seek = ReaderSeek;
138     p_sys->reader.tell = ReaderTell;
139     p_sys->reader.get_size = ReaderGetSize;
140     p_sys->reader.canseek = ReaderCanSeek;
141     p_sys->reader.data = p_demux;
142
143     /* Load info */
144     mpc_streaminfo_init( &p_sys->info );
145     if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
146     {
147         /* invalid file */
148         free( p_sys );
149         return VLC_EGENERIC;
150     }
151
152     /* */
153     mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
154     if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
155     {
156         /* */
157         free( p_sys );
158         return VLC_EGENERIC;
159     }
160
161     /* Fill p_demux fields */
162     p_demux->pf_demux = Demux;
163     p_demux->pf_control = Control;
164     p_demux->p_sys = p_sys;
165
166     /* */
167 #ifndef MPC_FIXED_POINT
168     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', '3', '2' ) );
169 #else
170 #   ifdef WORDS_BIGENDIAN
171     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'b' ) );
172 #   else
173     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'l' ) );
174 #   endif
175 #endif
176     fmt.audio.i_channels = p_sys->info.channels;
177     fmt.audio.i_rate = p_sys->info.sample_freq;
178     fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
179     fmt.audio.i_bitspersample = 32;
180     fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
181                     fmt.audio.i_bitspersample;
182     if( p_sys->info.peak_title > 0 )
183     {
184         fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
185         fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.peak_title / 32767.0;
186         fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
187         fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.gain_title / 100.0;
188     }
189     if( p_sys->info.peak_album > 0 )
190     {
191         fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
192         fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.peak_album / 32767.0;
193         fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
194         fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.gain_album / 100.0;
195     }
196
197     /* Parse possible id3 header */
198     if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
199     {
200         p_sys->p_meta = (vlc_meta_t *)p_demux->p_private;
201         p_demux->p_private = NULL;
202         module_Unneed( p_demux, p_id3 );
203     }
204
205     if( !p_sys->p_meta )
206         p_sys->p_meta = vlc_meta_New();
207
208     vlc_audio_replay_gain_MergeFromMeta( &fmt.audio_replay_gain, p_sys->p_meta );
209     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
210
211     return VLC_SUCCESS;
212 }
213
214 /*****************************************************************************
215  * Close: frees unused data
216  *****************************************************************************/
217 static void Close( vlc_object_t * p_this )
218 {
219     demux_t        *p_demux = (demux_t*)p_this;
220     demux_sys_t    *p_sys = p_demux->p_sys;
221
222     free( p_sys );
223 }
224
225 /*****************************************************************************
226  * Demux:
227  *****************************************************************************
228  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
229  *****************************************************************************/
230 static int Demux( demux_t *p_demux )
231 {
232     demux_sys_t *p_sys = p_demux->p_sys;
233     block_t     *p_data;
234     int i_ret;
235
236     p_data = block_New( p_demux,
237                         MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
238     i_ret = mpc_decoder_decode( &p_sys->decoder,
239                                (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
240                                NULL, NULL );
241     if( i_ret <= 0 )
242     {
243         block_Release( p_data );
244         return i_ret < 0 ? -1 : 0;
245     }
246
247     /* */
248     p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
249     p_data->i_dts = p_data->i_pts =
250             1 + I64C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
251
252     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
253
254     es_out_Send( p_demux->out, p_sys->p_es, p_data );
255
256     /* */
257     p_sys->i_position += i_ret;
258
259     return 1;
260 }
261
262 /*****************************************************************************
263  * Control:
264  *****************************************************************************/
265 static int Control( demux_t *p_demux, int i_query, va_list args )
266 {
267     demux_sys_t *p_sys = p_demux->p_sys;
268     double   f, *pf;
269     int64_t i64, *pi64;
270     vlc_meta_t *p_meta;
271
272     switch( i_query )
273     {
274         case DEMUX_GET_META:
275             p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
276             if( p_sys->p_meta )
277                 vlc_meta_Merge( p_meta, p_sys->p_meta );
278             else
279                 p_meta = NULL;
280             return VLC_SUCCESS;
281
282         case DEMUX_GET_LENGTH:
283             pi64 = (int64_t*)va_arg( args, int64_t * );
284             *pi64 = I64C(1000000) * p_sys->info.pcm_samples /
285                         p_sys->info.sample_freq;
286             return VLC_SUCCESS;
287
288         case DEMUX_GET_POSITION:
289             pf = (double*)va_arg( args, double * );
290             if( p_sys->info.pcm_samples > 0 )
291                 *pf = (double) p_sys->i_position /
292                       (double)p_sys->info.pcm_samples;
293             else
294                 *pf = 0.0;
295             return VLC_SUCCESS;
296
297         case DEMUX_GET_TIME:
298             pi64 = (int64_t*)va_arg( args, int64_t * );
299             *pi64 = I64C(1000000) * p_sys->i_position /
300                         p_sys->info.sample_freq;
301             return VLC_SUCCESS;
302
303         case DEMUX_SET_POSITION:
304             f = (double)va_arg( args, double );
305             i64 = (int64_t)(f * p_sys->info.pcm_samples);
306             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
307             {
308                 p_sys->i_position = i64;
309                 return VLC_SUCCESS;
310             }
311             return VLC_EGENERIC;
312
313         case DEMUX_SET_TIME:
314             i64 = (int64_t)va_arg( args, int64_t );
315             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
316             {
317                 p_sys->i_position = i64;
318                 return VLC_SUCCESS;
319             }
320             return VLC_EGENERIC;
321
322         default:
323             return VLC_EGENERIC;
324     }
325 }
326
327 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
328 {
329     demux_t *p_demux = (demux_t*)p_private;
330     return stream_Read( p_demux->s, dst, i_size );
331 }
332
333 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
334 {
335     demux_t *p_demux = (demux_t*)p_private;
336     return !stream_Seek( p_demux->s, i_offset );
337 }
338
339 mpc_int32_t ReaderTell( void *p_private)
340 {
341     demux_t *p_demux = (demux_t*)p_private;
342     return stream_Tell( p_demux->s );
343 }
344
345 mpc_int32_t ReaderGetSize( void *p_private )
346 {
347     demux_t *p_demux = (demux_t*)p_private;
348     return stream_Size( p_demux->s );
349 }
350
351 mpc_bool_t ReaderCanSeek( void *p_private )
352 {
353     demux_t *p_demux = (demux_t*)p_private;
354     vlc_bool_t b_canseek;
355
356     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
357     return b_canseek;
358 }
359