mpc.c 13.7 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * mpc.c : MPC stream input module for vlc
 *****************************************************************************
 * Copyright (C) 2001 the VideoLAN team
Antoine Cellerier's avatar
Antoine Cellerier committed
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
34
#include <vlc_demux.h>
35
#include <vlc_input.h>
36 37 38
#include <vlc_codec.h>
#include <math.h>

39 40 41
#ifdef HAVE_MPC_MPCDEC_H
#include <mpc/mpcdec.h>
#else
42
#include <mpcdec/mpcdec.h>
43
#endif
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

/* TODO:
 *  - test stream version 4..6
 *  - test fixed float version
 *  - ...
 *
 *  XXX:
 *  It is done the ugly way (the demux does the decode stage... but it works
 */

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open  ( vlc_object_t * );
static void Close ( vlc_object_t * );

60 61 62 63 64
vlc_module_begin ()
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
    set_description( N_("MusePack demuxer") )
    set_capability( "demux", 145 )
65

66 67 68
    set_callbacks( Open, Close )
    add_shortcut( "mpc" )
vlc_module_end ()
69 70 71 72 73 74 75 76 77 78

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int Demux  ( demux_t * );
static int Control( demux_t *, int, va_list );

struct demux_sys_t
{
    /* */
Laurent Aimar's avatar
Laurent Aimar committed
79
    es_out_id_t   *p_es;
80 81

    /* */
82
#ifndef HAVE_MPC_MPCDEC_H
83
    mpc_decoder    decoder;
84 85 86
#else
    mpc_demux     *decoder;
#endif
87 88 89 90 91 92 93
    mpc_reader     reader;
    mpc_streaminfo info;

    /* */
    int64_t        i_position;
};

94
#ifndef HAVE_MPC_MPCDEC_H
Laurent Aimar's avatar
Laurent Aimar committed
95 96 97 98 99
static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
static mpc_bool_t  ReaderSeek( void *p_private, mpc_int32_t i_offset );
static mpc_int32_t ReaderTell( void *p_private);
static mpc_int32_t ReaderGetSize( void *p_private );
static mpc_bool_t  ReaderCanSeek( void *p_private );
100 101 102 103 104 105 106
#else
static mpc_int32_t ReaderRead( mpc_reader *p_private, void *dst, mpc_int32_t i_size );
static mpc_bool_t  ReaderSeek( mpc_reader *p_private, mpc_int32_t i_offset );
static mpc_int32_t ReaderTell( mpc_reader *p_private);
static mpc_int32_t ReaderGetSize( mpc_reader *p_private );
static mpc_bool_t  ReaderCanSeek( mpc_reader *p_private );
#endif
107 108 109 110 111 112 113 114 115

/*****************************************************************************
 * Open: initializes ES structures
 *****************************************************************************/
static int Open( vlc_object_t * p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
    es_format_t fmt;
116
    const uint8_t *p_peek;
117 118 119 120

    if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
        return VLC_EGENERIC;

121 122 123 124 125 126
    if( memcmp( p_peek, "MP+", 3 )
#ifdef HAVE_MPC_MPCDEC_H
        /* SV8 format */
        && memcmp( p_peek, "MPCK", 4 )
#endif
      )
127
    {
128
        /* for v4..6 we check extension file */
129 130 131 132
        const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
        if( i_version  < 4 || i_version > 6 )
            return VLC_EGENERIC;

133
        if( !p_demux->b_force )
134 135
        {
            /* Check file name extension */
136 137 138
            if( !demux_IsPathExtension( p_demux, ".mpc" ) &&
                !demux_IsPathExtension( p_demux, ".mp+" ) &&
                !demux_IsPathExtension( p_demux, ".mpp" ) )
139 140 141 142 143
                return VLC_EGENERIC;
        }
    }

    /* */
Laurent Aimar's avatar
Laurent Aimar committed
144 145 146
    p_sys = calloc( 1, sizeof( *p_sys ) );
    if( !p_sys )
        return VLC_ENOMEM;
147 148 149 150 151 152 153 154 155 156

    p_sys->i_position = 0;

    p_sys->reader.read = ReaderRead;
    p_sys->reader.seek = ReaderSeek;
    p_sys->reader.tell = ReaderTell;
    p_sys->reader.get_size = ReaderGetSize;
    p_sys->reader.canseek = ReaderCanSeek;
    p_sys->reader.data = p_demux;

157
#ifndef HAVE_MPC_MPCDEC_H
158 159 160
    /* Load info */
    mpc_streaminfo_init( &p_sys->info );
    if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
Laurent Aimar's avatar
Laurent Aimar committed
161
        goto error;
162 163 164 165

    /* */
    mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
    if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
Laurent Aimar's avatar
Laurent Aimar committed
166
        goto error;
167 168 169 170 171 172 173
#else
    p_sys->decoder = mpc_demux_init( &p_sys->reader );
    if( !p_sys->decoder )
        goto error;

    mpc_demux_get_info( p_sys->decoder, &p_sys->info );
#endif
174 175 176 177 178 179 180 181

    /* Fill p_demux fields */
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
    p_demux->p_sys = p_sys;

    /* */
#ifndef MPC_FIXED_POINT
182
    es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_FL32 );
183 184
#else
#   ifdef WORDS_BIGENDIAN
185
    es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32B );
186
#   else
187
    es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S32L );
188 189 190 191 192 193 194 195
#   endif
#endif
    fmt.audio.i_channels = p_sys->info.channels;
    fmt.audio.i_rate = p_sys->info.sample_freq;
    fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
    fmt.audio.i_bitspersample = 32;
    fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
                    fmt.audio.i_bitspersample;
196 197 198 199 200 201 202 203 204

#ifdef HAVE_MPC_MPCDEC_H
#   define CONVERT_PEAK( mpc_peak ) (pow( 10, (mpc_peak) / 256.0 / 20.0 ) / 32767.0)
#   define CONVERT_GAIN( mpc_gain ) (MPC_OLD_GAIN_REF - (mpc_gain) / 256.0)
#else
#   define CONVERT_PEAK( mpc_peak ) ((mpc_peak) / 32767.0)
#   define CONVERT_GAIN( mpc_gain ) ((mpc_gain) / 100.0)
#endif

205 206
    if( p_sys->info.peak_title > 0 )
    {
207
        fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
208
        fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float) CONVERT_PEAK( p_sys->info.peak_title );
209
        fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
210
        fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float) CONVERT_GAIN( p_sys->info.gain_title );
211 212 213
    }
    if( p_sys->info.peak_album > 0 )
    {
214
        fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
215
        fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float) CONVERT_PEAK( p_sys->info.peak_album );
216
        fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
217
        fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float) CONVERT_GAIN( p_sys->info.gain_album );
218
    }
219

220 221 222
#undef CONVERT_GAIN
#undef CONVERT_PEAK

223
    p_sys->p_es = es_out_Add( p_demux->out, &fmt );
Laurent Aimar's avatar
Laurent Aimar committed
224 225
    if( !p_sys->p_es )
        goto error;
226 227

    return VLC_SUCCESS;
Laurent Aimar's avatar
Laurent Aimar committed
228 229 230 231

error:
    free( p_sys );
    return VLC_EGENERIC;
232 233 234 235 236 237 238 239 240 241
}

/*****************************************************************************
 * Close: frees unused data
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
    demux_t        *p_demux = (demux_t*)p_this;
    demux_sys_t    *p_sys = p_demux->p_sys;

242 243 244 245
#ifdef HAVE_MPC_MPCDEC_H
    if( p_sys->decoder )
    mpc_demux_exit( p_sys->decoder );
#endif
246 247 248 249 250 251 252 253 254 255 256 257 258
    free( p_sys );
}

/*****************************************************************************
 * Demux:
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    block_t     *p_data;
    int i_ret;
259 260 261 262
#ifdef HAVE_MPC_MPCDEC_H
    mpc_frame_info frame;
    mpc_status err;
#endif
263
    p_data = block_Alloc( MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
Laurent Aimar's avatar
Laurent Aimar committed
264 265 266
    if( !p_data )
        return -1;

267
#ifndef HAVE_MPC_MPCDEC_H
268
    i_ret = mpc_decoder_decode( &p_sys->decoder,
Laurent Aimar's avatar
Laurent Aimar committed
269 270
                                (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
                                NULL, NULL );
271 272 273 274 275
    if( i_ret <= 0 )
    {
        block_Release( p_data );
        return i_ret < 0 ? -1 : 0;
    }
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
#else
    frame.buffer = (MPC_SAMPLE_FORMAT*)p_data->p_buffer;
    err = mpc_demux_decode( p_sys->decoder, &frame );
    if( err != MPC_STATUS_OK )
    {
        block_Release( p_data );
        return -1;
    }
    else if( frame.bits == -1 )
    {
        block_Release( p_data );
        return 0;
    }

    i_ret = frame.samples;
#endif
292 293 294 295

    /* */
    p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
    p_data->i_dts = p_data->i_pts =
296
            VLC_TS_0 + CLOCK_FREQ * p_sys->i_position / p_sys->info.sample_freq;
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );

    es_out_Send( p_demux->out, p_sys->p_es, p_data );

    /* */
    p_sys->i_position += i_ret;

    return 1;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    double   f, *pf;
    int64_t i64, *pi64;
316
    bool *pb_bool;
317 318 319

    switch( i_query )
    {
320 321 322
        case DEMUX_CAN_SEEK:
            return stream_vaControl( p_demux->s, i_query, args );

323
        case DEMUX_HAS_UNSUPPORTED_META:
324 325
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = true;
326 327 328 329
            return VLC_SUCCESS;

        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
330
#ifndef HAVE_MPC_MPCDEC_H
331
            *pi64 = CLOCK_FREQ * p_sys->info.pcm_samples /
332
                        p_sys->info.sample_freq;
333
#else
334
            *pi64 = CLOCK_FREQ * (p_sys->info.samples -
335 336 337
                                        p_sys->info.beg_silence) /
                p_sys->info.sample_freq;
#endif
338 339 340 341
            return VLC_SUCCESS;

        case DEMUX_GET_POSITION:
            pf = (double*)va_arg( args, double * );
342
#ifndef HAVE_MPC_MPCDEC_H
343 344 345
            if( p_sys->info.pcm_samples > 0 )
                *pf = (double) p_sys->i_position /
                      (double)p_sys->info.pcm_samples;
346 347 348 349 350
#else
            if( p_sys->info.samples - p_sys->info.beg_silence > 0)
                *pf = (double) p_sys->i_position /
                      (double)(p_sys->info.samples - p_sys->info.beg_silence);
#endif
351 352 353 354 355 356
            else
                *pf = 0.0;
            return VLC_SUCCESS;

        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
357
            *pi64 = CLOCK_FREQ * p_sys->i_position /
358 359 360 361 362
                        p_sys->info.sample_freq;
            return VLC_SUCCESS;

        case DEMUX_SET_POSITION:
            f = (double)va_arg( args, double );
363
#ifndef HAVE_MPC_MPCDEC_H
364 365
            i64 = (int64_t)(f * p_sys->info.pcm_samples);
            if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
366 367 368 369 370
#else
            i64 = (int64_t)(f * (p_sys->info.samples -
                                 p_sys->info.beg_silence));
            if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
#endif
371 372 373 374 375 376 377 378
            {
                p_sys->i_position = i64;
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        case DEMUX_SET_TIME:
            i64 = (int64_t)va_arg( args, int64_t );
379
#ifndef HAVE_MPC_MPCDEC_H
380
            if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
381 382 383
#else
             if( mpc_demux_seek_sample( p_sys->decoder, i64 ) == MPC_STATUS_OK )
#endif
384 385 386 387 388 389 390 391 392 393 394
            {
                p_sys->i_position = i64;
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        default:
            return VLC_EGENERIC;
    }
}

395
#ifndef HAVE_MPC_MPCDEC_H
Laurent Aimar's avatar
Laurent Aimar committed
396
static mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
397 398
{
    demux_t *p_demux = (demux_t*)p_private;
399 400 401 402 403
#else
static mpc_int32_t ReaderRead( mpc_reader *p_private, void *dst, mpc_int32_t i_size )
{
    demux_t *p_demux = (demux_t*)p_private->data;
#endif
404 405 406
    return stream_Read( p_demux->s, dst, i_size );
}

407
#ifndef HAVE_MPC_MPCDEC_H
Laurent Aimar's avatar
Laurent Aimar committed
408
static mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
409 410
{
    demux_t *p_demux = (demux_t*)p_private;
411 412 413 414 415
#else
static mpc_bool_t ReaderSeek( mpc_reader *p_private, mpc_int32_t i_offset )
{
    demux_t *p_demux = (demux_t*)p_private->data;
#endif
416 417 418
    return !stream_Seek( p_demux->s, i_offset );
}

419
#ifndef HAVE_MPC_MPCDEC_H
Laurent Aimar's avatar
Laurent Aimar committed
420
static mpc_int32_t ReaderTell( void *p_private)
421 422
{
    demux_t *p_demux = (demux_t*)p_private;
423 424 425 426 427
#else
static mpc_int32_t ReaderTell( mpc_reader *p_private)
{
    demux_t *p_demux = (demux_t*)p_private->data;
#endif
428 429 430
    return stream_Tell( p_demux->s );
}

431
#ifndef HAVE_MPC_MPCDEC_H
Laurent Aimar's avatar
Laurent Aimar committed
432
static mpc_int32_t ReaderGetSize( void *p_private )
433 434
{
    demux_t *p_demux = (demux_t*)p_private;
435 436 437 438 439
#else
static mpc_int32_t ReaderGetSize( mpc_reader *p_private )
{
    demux_t *p_demux = (demux_t*)p_private->data;
#endif
440 441 442
    return stream_Size( p_demux->s );
}

443
#ifndef HAVE_MPC_MPCDEC_H
Laurent Aimar's avatar
Laurent Aimar committed
444
static mpc_bool_t ReaderCanSeek( void *p_private )
445 446
{
    demux_t *p_demux = (demux_t*)p_private;
447 448 449 450 451
#else
static mpc_bool_t ReaderCanSeek( mpc_reader *p_private )
{
    demux_t *p_demux = (demux_t*)p_private->data;
#endif
452
    bool b_canseek;
453 454 455 456 457

    stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
    return b_canseek;
}