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