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