]> git.sesse.net Git - vlc/blob - modules/demux/mpc.c
Merge branch 1.0-bugfix
[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_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
34 #include <vlc_input.h>
35 #include <vlc_codec.h>
36 #include <math.h>
37
38 #include <mpcdec/mpcdec.h>
39
40 /* TODO:
41  *  - test stream version 4..6
42  *  - test fixed float version
43  *  - ...
44  *
45  *  XXX:
46  *  It is done the ugly way (the demux does the decode stage... but it works
47  */
48
49 /*****************************************************************************
50  * Module descriptor
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( N_("MusePack demuxer") )
59     set_capability( "demux", 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     int64_t        i_position;
83 };
84
85 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
86 static mpc_bool_t  ReaderSeek( void *p_private, mpc_int32_t i_offset );
87 static mpc_int32_t ReaderTell( void *p_private);
88 static mpc_int32_t ReaderGetSize( void *p_private );
89 static mpc_bool_t  ReaderCanSeek( void *p_private );
90
91 /*****************************************************************************
92  * Open: initializes ES structures
93  *****************************************************************************/
94 static int Open( vlc_object_t * p_this )
95 {
96     demux_t     *p_demux = (demux_t*)p_this;
97     demux_sys_t *p_sys;
98     es_format_t fmt;
99     const uint8_t *p_peek;
100
101     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
102         return VLC_EGENERIC;
103
104     if( memcmp( p_peek, "MP+", 3 ) )
105     {
106         /* for v4..6 we check extension file */
107         const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
108
109         if( i_version  < 4 || i_version > 6 )
110             return VLC_EGENERIC;
111
112         if( !p_demux->b_force )
113         {
114             /* Check file name extension */
115             if( !demux_IsPathExtension( p_demux, ".mpc" ) &&
116                 !demux_IsPathExtension( p_demux, ".mp+" ) &&
117                 !demux_IsPathExtension( p_demux, ".mpp" ) )
118                 return VLC_EGENERIC;
119         }
120     }
121
122     /* */
123     p_sys = calloc( 1, sizeof( *p_sys ) );
124     if( !p_sys )
125         return VLC_ENOMEM;
126
127     p_sys->i_position = 0;
128
129     p_sys->reader.read = ReaderRead;
130     p_sys->reader.seek = ReaderSeek;
131     p_sys->reader.tell = ReaderTell;
132     p_sys->reader.get_size = ReaderGetSize;
133     p_sys->reader.canseek = ReaderCanSeek;
134     p_sys->reader.data = p_demux;
135
136     /* Load info */
137     mpc_streaminfo_init( &p_sys->info );
138     if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
139         goto error;
140
141     /* */
142     mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
143     if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
144         goto error;
145
146     /* Fill p_demux fields */
147     p_demux->pf_demux = Demux;
148     p_demux->pf_control = Control;
149     p_demux->p_sys = p_sys;
150
151     /* */
152 #ifndef MPC_FIXED_POINT
153     es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_FL32 );
154 #else
155 #   ifdef WORDS_BIGENDIAN
156     es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32B );
157 #   else
158     es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32L );
159 #   endif
160 #endif
161     fmt.audio.i_channels = p_sys->info.channels;
162     fmt.audio.i_rate = p_sys->info.sample_freq;
163     fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
164     fmt.audio.i_bitspersample = 32;
165     fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
166                     fmt.audio.i_bitspersample;
167     if( p_sys->info.peak_title > 0 )
168     {
169         fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
170         fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.peak_title / 32767.0;
171         fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
172         fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.gain_title / 100.0;
173     }
174     if( p_sys->info.peak_album > 0 )
175     {
176         fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
177         fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.peak_album / 32767.0;
178         fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
179         fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.gain_album / 100.0;
180     }
181
182     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
183     if( !p_sys->p_es )
184         goto error;
185
186     return VLC_SUCCESS;
187
188 error:
189     free( p_sys );
190     return VLC_EGENERIC;
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     if( !p_data )
218         return -1;
219
220     i_ret = mpc_decoder_decode( &p_sys->decoder,
221                                 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
222                                 NULL, NULL );
223     if( i_ret <= 0 )
224     {
225         block_Release( p_data );
226         return i_ret < 0 ? -1 : 0;
227     }
228
229     /* */
230     p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
231     p_data->i_dts = p_data->i_pts =
232             1 + INT64_C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
233
234     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
235
236     es_out_Send( p_demux->out, p_sys->p_es, p_data );
237
238     /* */
239     p_sys->i_position += i_ret;
240
241     return 1;
242 }
243
244 /*****************************************************************************
245  * Control:
246  *****************************************************************************/
247 static int Control( demux_t *p_demux, int i_query, va_list args )
248 {
249     demux_sys_t *p_sys = p_demux->p_sys;
250     double   f, *pf;
251     int64_t i64, *pi64;
252     bool *pb_bool;
253
254     switch( i_query )
255     {
256         case DEMUX_HAS_UNSUPPORTED_META:
257             pb_bool = (bool*)va_arg( args, bool* );
258             *pb_bool = true;
259             return VLC_SUCCESS;
260
261         case DEMUX_GET_LENGTH:
262             pi64 = (int64_t*)va_arg( args, int64_t * );
263             *pi64 = INT64_C(1000000) * p_sys->info.pcm_samples /
264                         p_sys->info.sample_freq;
265             return VLC_SUCCESS;
266
267         case DEMUX_GET_POSITION:
268             pf = (double*)va_arg( args, double * );
269             if( p_sys->info.pcm_samples > 0 )
270                 *pf = (double) p_sys->i_position /
271                       (double)p_sys->info.pcm_samples;
272             else
273                 *pf = 0.0;
274             return VLC_SUCCESS;
275
276         case DEMUX_GET_TIME:
277             pi64 = (int64_t*)va_arg( args, int64_t * );
278             *pi64 = INT64_C(1000000) * p_sys->i_position /
279                         p_sys->info.sample_freq;
280             return VLC_SUCCESS;
281
282         case DEMUX_SET_POSITION:
283             f = (double)va_arg( args, double );
284             i64 = (int64_t)(f * p_sys->info.pcm_samples);
285             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
286             {
287                 p_sys->i_position = i64;
288                 return VLC_SUCCESS;
289             }
290             return VLC_EGENERIC;
291
292         case DEMUX_SET_TIME:
293             i64 = (int64_t)va_arg( args, int64_t );
294             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
295             {
296                 p_sys->i_position = i64;
297                 return VLC_SUCCESS;
298             }
299             return VLC_EGENERIC;
300
301         default:
302             return VLC_EGENERIC;
303     }
304 }
305
306 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
307 {
308     demux_t *p_demux = (demux_t*)p_private;
309     return stream_Read( p_demux->s, dst, i_size );
310 }
311
312 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
313 {
314     demux_t *p_demux = (demux_t*)p_private;
315     return !stream_Seek( p_demux->s, i_offset );
316 }
317
318 static mpc_int32_t ReaderTell( void *p_private)
319 {
320     demux_t *p_demux = (demux_t*)p_private;
321     return stream_Tell( p_demux->s );
322 }
323
324 static mpc_int32_t ReaderGetSize( void *p_private )
325 {
326     demux_t *p_demux = (demux_t*)p_private;
327     return stream_Size( p_demux->s );
328 }
329
330 static mpc_bool_t ReaderCanSeek( void *p_private )
331 {
332     demux_t *p_demux = (demux_t*)p_private;
333     bool b_canseek;
334
335     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
336     return b_canseek;
337 }
338