qml: Create MediaGroupDisplay
[vlc.git] / modules / demux / mpc.c
blobbd8891d2fcc23ca61e5e5be10369392017a6a01b
1 /*****************************************************************************
2 * mpc.c : MPC stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 the VideoLAN team
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_demux.h>
33 #include <vlc_codec.h>
34 #include <math.h>
36 #include <mpcdec/mpcdec.h>
38 /* TODO:
39 * - test stream version 4..6
40 * - test fixed float version
41 * - ...
43 * XXX:
44 * It is done the ugly way (the demux does the decode stage... but it works
47 /*****************************************************************************
48 * Module descriptor
49 *****************************************************************************/
50 static int Open ( vlc_object_t * );
52 vlc_module_begin ()
53 set_category( CAT_INPUT )
54 set_subcategory( SUBCAT_INPUT_DEMUX )
55 set_description( N_("MusePack demuxer") )
56 set_capability( "demux", 145 )
58 set_callback( Open )
59 add_shortcut( "mpc" )
60 add_file_extension("mpc")
61 add_file_extension("mp+")
62 add_file_extension("mpp")
63 vlc_module_end ()
65 /*****************************************************************************
66 * Local prototypes
67 *****************************************************************************/
68 static int Demux ( demux_t * );
69 static int Control( demux_t *, int, va_list );
71 typedef struct
73 /* */
74 es_out_id_t *p_es;
76 /* */
77 mpc_decoder decoder;
78 mpc_reader reader;
79 mpc_streaminfo info;
81 /* */
82 int64_t i_position;
83 } demux_sys_t;
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 );
91 /*****************************************************************************
92 * Open: initializes ES structures
93 *****************************************************************************/
94 static int Open( vlc_object_t * p_this )
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;
101 if( vlc_stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
102 return VLC_EGENERIC;
104 if( memcmp( p_peek, "MP+", 3 ) )
106 /* for v4..6 we check extension file */
107 const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
109 if( i_version < 4 || i_version > 6 )
110 return VLC_EGENERIC;
112 if( !p_demux->obj.force )
113 return VLC_EGENERIC;
116 /* */
117 p_sys = vlc_obj_calloc( p_this, 1, sizeof( *p_sys ) );
118 if( !p_sys )
119 return VLC_ENOMEM;
121 p_sys->i_position = 0;
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;
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 return VLC_EGENERIC;
135 /* */
136 mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
137 if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
138 return VLC_EGENERIC;
140 /* Fill p_demux fields */
141 p_demux->pf_demux = Demux;
142 p_demux->pf_control = Control;
143 p_demux->p_sys = p_sys;
145 /* */
146 #ifndef MPC_FIXED_POINT
147 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_FL32 );
148 #else
149 # ifdef WORDS_BIGENDIAN
150 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32B );
151 # else
152 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32L );
153 # endif
154 #endif
155 fmt.audio.i_channels = p_sys->info.channels;
156 fmt.audio.i_rate = p_sys->info.sample_freq;
157 fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
158 fmt.audio.i_bitspersample = 32;
159 fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
160 fmt.audio.i_bitspersample;
162 #ifdef HAVE_MPC_MPCDEC_H
163 # define CONVERT_PEAK( mpc_peak ) (pow( 10, (mpc_peak) / 256.0 / 20.0 ) / 32767.0)
164 # define CONVERT_GAIN( mpc_gain ) (MPC_OLD_GAIN_REF - (mpc_gain) / 256.0)
165 #else
166 # define CONVERT_PEAK( mpc_peak ) ((mpc_peak) / 32767.0)
167 # define CONVERT_GAIN( mpc_gain ) ((mpc_gain) / 100.0)
168 #endif
170 if( p_sys->info.peak_title > 0 )
172 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
173 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float) CONVERT_PEAK( p_sys->info.peak_title );
174 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
175 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float) CONVERT_GAIN( p_sys->info.gain_title );
177 if( p_sys->info.peak_album > 0 )
179 fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
180 fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float) CONVERT_PEAK( p_sys->info.peak_album );
181 fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
182 fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float) CONVERT_GAIN( p_sys->info.gain_album );
185 #undef CONVERT_GAIN
186 #undef CONVERT_PEAK
188 fmt.i_id = 0;
189 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
190 if( !p_sys->p_es )
191 return VLC_EGENERIC;
193 return VLC_SUCCESS;
196 /*****************************************************************************
197 * Demux:
198 *****************************************************************************
199 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
200 *****************************************************************************/
201 static int Demux( demux_t *p_demux )
203 demux_sys_t *p_sys = p_demux->p_sys;
204 block_t *p_data;
205 int i_ret;
207 p_data = block_New( p_demux,
208 MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
209 if( !p_data )
210 return VLC_DEMUXER_EGENERIC;
212 i_ret = mpc_decoder_decode( &p_sys->decoder,
213 (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
214 NULL, NULL );
215 if( i_ret <= 0 )
217 block_Release( p_data );
218 return i_ret < 0 ? VLC_DEMUXER_EGENERIC : VLC_DEMUXER_EOF;
221 /* */
222 p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
223 p_data->i_dts = p_data->i_pts =
224 VLC_TICK_0 + vlc_tick_from_samples(p_sys->i_position, p_sys->info.sample_freq);
226 es_out_SetPCR( p_demux->out, p_data->i_dts );
228 es_out_Send( p_demux->out, p_sys->p_es, p_data );
230 /* */
231 p_sys->i_position += i_ret;
233 return VLC_DEMUXER_SUCCESS;
236 /*****************************************************************************
237 * Control:
238 *****************************************************************************/
239 static int Control( demux_t *p_demux, int i_query, va_list args )
241 demux_sys_t *p_sys = p_demux->p_sys;
242 double f, *pf;
243 vlc_tick_t i64;
244 vlc_tick_t *pi64;
245 bool *pb_bool;
247 switch( i_query )
249 case DEMUX_CAN_SEEK:
250 return vlc_stream_vaControl( p_demux->s, i_query, args );
252 case DEMUX_HAS_UNSUPPORTED_META:
253 pb_bool = va_arg( args, bool* );
254 *pb_bool = true;
255 return VLC_SUCCESS;
257 case DEMUX_GET_LENGTH:
258 *va_arg( args, vlc_tick_t * ) =
259 vlc_tick_from_samples(p_sys->info.pcm_samples, p_sys->info.sample_freq);
260 return VLC_SUCCESS;
262 case DEMUX_GET_POSITION:
263 pf = va_arg( args, double * );
264 pi64 = (int64_t*)va_arg( args, int64_t * );
265 *pi64 = INT64_C(1000000) * p_sys->info.pcm_samples /
266 p_sys->info.sample_freq;
267 else
268 *pf = 0.0;
269 return VLC_SUCCESS;
271 case DEMUX_GET_TIME:
272 *va_arg( args, vlc_tick_t * ) =
273 vlc_tick_from_samples(p_sys->i_position, p_sys->info.sample_freq);
274 return VLC_SUCCESS;
276 case DEMUX_SET_POSITION:
277 f = va_arg( args, double );
278 i64 = (int64_t)(f * p_sys->info.pcm_samples);
279 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
281 p_sys->i_position = i64;
282 return VLC_SUCCESS;
284 return VLC_EGENERIC;
286 case DEMUX_SET_TIME:
288 vlc_tick_t i64 = va_arg( args, vlc_tick_t );
289 if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
291 p_sys->i_position = i64;
292 return VLC_SUCCESS;
294 return VLC_EGENERIC;
297 case DEMUX_CAN_PAUSE:
298 case DEMUX_SET_PAUSE_STATE:
299 case DEMUX_CAN_CONTROL_PACE:
300 case DEMUX_GET_PTS_DELAY:
301 return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
303 default:
304 return VLC_EGENERIC;
308 static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
310 demux_t *p_demux = (demux_t*)p_private;
311 return vlc_stream_Read( p_demux->s, dst, i_size );
314 static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
316 demux_t *p_demux = (demux_t*)p_private;
317 return !vlc_stream_Seek( p_demux->s, i_offset );
320 static mpc_int32_t ReaderTell( void *p_private)
322 demux_t *p_demux = (demux_t*)p_private;
323 return vlc_stream_Tell( p_demux->s );
326 static mpc_int32_t ReaderGetSize( void *p_private )
328 demux_t *p_demux = (demux_t*)p_private;
329 return stream_Size( p_demux->s );
332 static mpc_bool_t ReaderCanSeek( void *p_private )
334 demux_t *p_demux = (demux_t*)p_private;
335 bool b_canseek;
337 vlc_stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
338 return b_canseek;