mpgatofixed32.c 13.4 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * mpgatofixed32.c: MPEG-1 & 2 audio layer I, II, III + MPEG 2.5 decoder,
 * using MAD (MPEG Audio Decoder)
 *****************************************************************************
5
 * Copyright (C) 2001-2005 the VideoLAN team
6
 * $Id$
7 8
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
Jean-Paul Saman's avatar
Jean-Paul Saman committed
9
 *          Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
Jean-Paul Saman's avatar
Jean-Paul Saman committed
10
 *
11 12 13 14
 * 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.
15
 *
16 17 18 19 20 21 22
 * 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
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28 29 30 31
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#include <mad.h>

32 33 34 35
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

36
#include <vlc_common.h>
37
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
38 39
#include <vlc_aout.h>
#include <vlc_block.h>
40
#include <vlc_filter.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
41
#include <vlc_cpu.h>
42 43 44 45 46 47

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Create    ( vlc_object_t * );
static void Destroy   ( vlc_object_t * );
48
static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
49 50
                        aout_buffer_t * );

51 52 53 54
static int  OpenFilter ( vlc_object_t * );
static void CloseFilter( vlc_object_t * );
static block_t *Convert( filter_t *, block_t * );

55 56 57
/*****************************************************************************
 * Local structures
 *****************************************************************************/
58
struct filter_sys_t
59 60
{
    struct mad_stream mad_stream;
61 62 63 64
    struct mad_frame  mad_frame;
    struct mad_synth  mad_synth;

    int               i_reject_count;
65 66 67 68 69
};

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
70 71 72 73 74 75
vlc_module_begin ()
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACODEC )
    set_description( N_("MPEG audio decoder") )
    set_capability( "audio filter", 100 )
    set_callbacks( Create, Destroy )
76

77 78 79 80 81
    add_submodule ()
    set_description( N_("MPEG audio decoder") )
    set_capability( "audio filter2", 100 )
    set_callbacks( OpenFilter, CloseFilter )
vlc_module_end ()
82 83

/*****************************************************************************
84
 * Create:
85
 *****************************************************************************/
86
static int Create( vlc_object_t *p_this )
87
{
88 89
    aout_filter_t *p_filter = (aout_filter_t *)p_this;
    struct filter_sys_t *p_sys;
90

91
    if ( (p_filter->input.i_format != VLC_CODEC_MPGA
92
           && p_filter->input.i_format != VLC_FOURCC('m','p','g','3'))
93 94
            || (p_filter->output.i_format != VLC_CODEC_FL32
                 && p_filter->output.i_format != VLC_CODEC_FI32) )
95 96 97 98
    {
        return -1;
    }

99
    if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
100 101 102 103 104
    {
        return -1;
    }

    /* Allocate the memory needed to store the module's structure */
105 106
    p_sys = malloc( sizeof(filter_sys_t) );
    p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
107 108 109 110 111 112 113 114
    if( p_sys == NULL )
        return -1;

    /* Initialize libmad */
    mad_stream_init( &p_sys->mad_stream );
    mad_frame_init( &p_sys->mad_frame );
    mad_synth_init( &p_sys->mad_synth );
    mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
115
    p_sys->i_reject_count = 0;
116 117 118 119 120 121 122 123 124 125 126 127 128

    p_filter->pf_do_work = DoWork;
    p_filter->b_in_place = 0;

    return 0;
}

/*****************************************************************************
 * DoWork: decode an MPEG audio frame.
 *****************************************************************************/
static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
                    aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
{
129
    filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
130 131

    p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
132
    p_out_buf->i_nb_bytes = p_in_buf->i_nb_samples * sizeof(vlc_fixed_t) *
133
                               aout_FormatNbChannels( &p_filter->output );
134 135 136 137 138 139

    /* Do the actual decoding now. */
    mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
                       p_in_buf->i_nb_bytes );
    if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
    {
140
        msg_Dbg( p_aout, "libmad error: %s",
141
                  mad_stream_errorstr( &p_sys->mad_stream ) );
142 143 144 145 146 147 148 149 150
        p_sys->i_reject_count = 3;
    }
    else if( p_in_buf->b_discontinuity )
    {
        p_sys->i_reject_count = 3;
    }

    if( p_sys->i_reject_count > 0 )
    {
151
        if( p_filter->output.i_format == VLC_CODEC_FL32 )
152 153 154
        {
            int i;
            int i_size = p_out_buf->i_nb_bytes / sizeof(float);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
155

156 157 158 159 160 161 162 163
            float * a = (float *)p_out_buf->p_buffer;
            for ( i = 0 ; i < i_size ; i++ )
                *a++ = 0.0;
        }
        else
        {
            memset( p_out_buf->p_buffer, 0, p_out_buf->i_nb_bytes );
        }
164
        p_sys->i_reject_count--;
165
        return;
166
    }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
167

168

169 170
    mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );

171
    if ( p_filter->output.i_format == VLC_CODEC_FI32 )
172 173 174 175 176 177 178 179 180 181 182
    {
        /* Interleave and keep buffers in mad_fixed_t format */
        mad_fixed_t * p_samples = (mad_fixed_t *)p_out_buf->p_buffer;
        struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
        unsigned int i_samples = p_pcm->length;
        mad_fixed_t const * p_left = p_pcm->samples[0];
        mad_fixed_t const * p_right = p_pcm->samples[1];

        switch ( p_pcm->channels )
        {
        case 2:
183 184 185 186 187 188 189 190
            if ( p_filter->output.i_physical_channels == AOUT_CHAN_CENTER )
            {
                while ( i_samples-- )
                {
                    *p_samples++ = (*p_left++ >> 1) + (*p_right++ >> 1);
                }
            }
            else if ( p_filter->output.i_original_channels == AOUT_CHAN_LEFT )
191
            {
192 193 194 195 196 197 198 199 200 201 202 203 204
                while ( i_samples-- )
                {
                    *p_samples++ = *p_left;
                    *p_samples++ = *p_left++;
                }
            }
            else if ( p_filter->output.i_original_channels == AOUT_CHAN_RIGHT )
            {
                while ( i_samples-- )
                {
                    *p_samples++ = *p_right;
                    *p_samples++ = *p_right++;
                }
205 206
            }
            else
207
            {
208 209 210 211 212
                while ( i_samples-- )
                {
                    *p_samples++ = *p_left++;
                    *p_samples++ = *p_right++;
                }
213 214 215 216
            }
            break;

        case 1:
217
            vlc_memcpy( p_samples, p_left, i_samples * sizeof(mad_fixed_t) );
218 219 220
            break;

        default:
221
            msg_Err( p_aout, "cannot interleave %i channels",
222 223 224 225 226 227 228 229 230 231 232
                     p_pcm->channels );
        }
    }
    else
    {
        /* float32 */
        float * p_samples = (float *)p_out_buf->p_buffer;
        struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
        unsigned int i_samples = p_pcm->length;
        mad_fixed_t const * p_left = p_pcm->samples[0];
        mad_fixed_t const * p_right = p_pcm->samples[1];
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
233
        float f_temp = (float)FIXED32_ONE;
234

235
        switch ( p_pcm->channels )
236 237
        {
        case 2:
238
            if ( p_filter->output.i_physical_channels == AOUT_CHAN_CENTER )
239
            {
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
                while ( i_samples-- )
                {
                    *p_samples++ = (float)*p_left++ / f_temp / 2 +
                                   (float)*p_right++ / f_temp / 2;
                }
            }
            else if ( p_filter->output.i_original_channels == AOUT_CHAN_LEFT )
            {
                while ( i_samples-- )
                {
                    *p_samples++ = (float)*p_left / f_temp;
                    *p_samples++ = (float)*p_left++ / f_temp;
                }
            }
            else if ( p_filter->output.i_original_channels == AOUT_CHAN_RIGHT )
            {
                while ( i_samples-- )
                {
                    *p_samples++ = (float)*p_right / f_temp;
                    *p_samples++ = (float)*p_right++ / f_temp;
                }
261 262
            }
            else
263
            {
264 265 266 267 268
                while ( i_samples-- )
                {
                    *p_samples++ = (float)*p_left++ / f_temp;
                    *p_samples++ = (float)*p_right++ / f_temp;
                }
269 270 271 272 273 274
            }
            break;

        case 1:
            while ( i_samples-- )
            {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
275
                *p_samples++ = (float)*p_left++ / f_temp;
276 277 278 279
            }
            break;

        default:
280
            msg_Err( p_aout, "cannot interleave %i channels",
281 282 283 284 285 286 287 288
                     p_pcm->channels );
        }
    }
}

/*****************************************************************************
 * Destroy : deallocate data structures
 *****************************************************************************/
289
static void Destroy( vlc_object_t *p_this )
290
{
291 292
    aout_filter_t *p_filter = (aout_filter_t *)p_this;
    filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
293 294 295 296 297 298 299

    mad_synth_finish( &p_sys->mad_synth );
    mad_frame_finish( &p_sys->mad_frame );
    mad_stream_finish( &p_sys->mad_stream );
    free( p_sys );
}

300
/*****************************************************************************
301
 * OpenFilter:
302 303 304 305 306 307
 *****************************************************************************/
static int OpenFilter( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;
    filter_sys_t *p_sys;

308
    if( p_filter->fmt_in.i_codec != VLC_CODEC_MPGA &&
309 310 311 312 313 314 315 316 317
        p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','3') )
    {
        return VLC_EGENERIC;
    }

    /* Allocate the memory needed to store the module's structure */
    p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
    if( p_sys == NULL )
        return -1;
318
    p_sys->i_reject_count = 0;
319 320 321 322 323 324 325 326 327

    p_filter->pf_audio_filter = Convert;

    /* Initialize libmad */
    mad_stream_init( &p_sys->mad_stream );
    mad_frame_init( &p_sys->mad_frame );
    mad_synth_init( &p_sys->mad_synth );
    mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );

328
    if( vlc_CPU() & CPU_CAPABILITY_FPU )
329
        p_filter->fmt_out.i_codec = VLC_CODEC_FL32;
330
    else
331
        p_filter->fmt_out.i_codec = VLC_CODEC_FI32;
332
    p_filter->fmt_out.audio.i_format = p_filter->fmt_out.i_codec;
333 334
    p_filter->fmt_out.audio.i_bitspersample =
        aout_BitsPerSample( p_filter->fmt_out.i_codec );
335 336 337

    p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;

Clément Stenac's avatar
Clément Stenac committed
338
    msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
339 340
             (char *)&p_filter->fmt_in.i_codec,
             (char *)&p_filter->fmt_out.i_codec,
341
             p_filter->fmt_in.audio.i_bitspersample );
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

    return 0;
}

/*****************************************************************************
 * CloseFilter : deallocate data structures
 *****************************************************************************/
static void CloseFilter( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;
    filter_sys_t *p_sys = p_filter->p_sys;

    mad_synth_finish( &p_sys->mad_synth );
    mad_frame_finish( &p_sys->mad_frame );
    mad_stream_finish( &p_sys->mad_stream );
    free( p_sys );
}

static block_t *Convert( filter_t *p_filter, block_t *p_block )
{
    aout_filter_t aout_filter;
    aout_buffer_t in_buf, out_buf;
    block_t *p_out;
Gildas Bazin's avatar
*...  
Gildas Bazin committed
365
    int i_out_size;
366

Gildas Bazin's avatar
*...  
Gildas Bazin committed
367 368
    if( !p_block || !p_block->i_samples )
    {
369 370
        if( p_block )
            block_Release( p_block );
Gildas Bazin's avatar
*...  
Gildas Bazin committed
371 372 373 374
        return NULL;
    }

    i_out_size = p_block->i_samples *
375
      p_filter->fmt_out.audio.i_bitspersample *
376
        p_filter->fmt_out.audio.i_channels / 8;
377 378 379 380 381

    p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
    if( !p_out )
    {
        msg_Warn( p_filter, "can't get output buffer" );
382
        block_Release( p_block );
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
        return NULL;
    }

    p_out->i_samples = p_block->i_samples;
    p_out->i_dts = p_block->i_dts;
    p_out->i_pts = p_block->i_pts;
    p_out->i_length = p_block->i_length;

    aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
    aout_filter.input = p_filter->fmt_in.audio;
    aout_filter.input.i_format = p_filter->fmt_in.i_codec;
    aout_filter.output = p_filter->fmt_out.audio;
    aout_filter.output.i_format = p_filter->fmt_out.i_codec;

    in_buf.p_buffer = p_block->p_buffer;
398
    in_buf.b_discontinuity = false;
399 400 401 402 403 404 405 406
    in_buf.i_nb_bytes = p_block->i_buffer;
    in_buf.i_nb_samples = p_block->i_samples;
    out_buf.p_buffer = p_out->p_buffer;
    out_buf.i_nb_bytes = p_out->i_buffer;
    out_buf.i_nb_samples = p_out->i_samples;

    DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );

407
    block_Release( p_block );
Gildas Bazin's avatar
*...  
Gildas Bazin committed
408

409 410 411
    p_out->i_buffer = out_buf.i_nb_bytes;
    p_out->i_samples = out_buf.i_nb_samples;

412 413
    return p_out;
}