mpgatofixed32.c 8.24 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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
35
#include <assert.h>
36

37
#include <vlc_common.h>
38
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
39 40
#include <vlc_aout.h>
#include <vlc_block.h>
41
#include <vlc_filter.h>
42 43 44 45

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
46 47 48 49
static int  OpenFilter ( vlc_object_t * );
static void CloseFilter( vlc_object_t * );
static block_t *Convert( filter_t *, block_t * );

50 51 52
/*****************************************************************************
 * Local structures
 *****************************************************************************/
53
struct filter_sys_t
54 55
{
    struct mad_stream mad_stream;
56 57 58 59
    struct mad_frame  mad_frame;
    struct mad_synth  mad_synth;

    int               i_reject_count;
60 61 62 63 64
};

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
65 66 67 68
vlc_module_begin ()
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACODEC )
    set_description( N_("MPEG audio decoder") )
69
    set_capability( "audio filter", 100 )
70 71
    set_callbacks( OpenFilter, CloseFilter )
vlc_module_end ()
72 73 74 75

/*****************************************************************************
 * DoWork: decode an MPEG audio frame.
 *****************************************************************************/
76
static void DoWork( filter_t * p_filter,
77 78
                    aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
{
79
    filter_sys_t *p_sys = p_filter->p_sys;
80 81

    p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
82
    p_out_buf->i_buffer = p_in_buf->i_nb_samples * sizeof(vlc_fixed_t) *
83
                               aout_FormatNbChannels( &p_filter->fmt_out.audio );
84 85 86

    /* Do the actual decoding now. */
    mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
87
                       p_in_buf->i_buffer );
88 89
    if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
    {
90
        msg_Dbg( p_filter, "libmad error: %s",
91
                  mad_stream_errorstr( &p_sys->mad_stream ) );
92 93
        p_sys->i_reject_count = 3;
    }
94
    else if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
95 96 97 98 99 100
    {
        p_sys->i_reject_count = 3;
    }

    if( p_sys->i_reject_count > 0 )
    {
101
        if( p_filter->fmt_out.audio.i_format == VLC_CODEC_FL32 )
102 103
        {
            int i;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
104
            int i_size = p_out_buf->i_buffer / sizeof(float);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
105

106 107 108 109 110 111
            float * a = (float *)p_out_buf->p_buffer;
            for ( i = 0 ; i < i_size ; i++ )
                *a++ = 0.0;
        }
        else
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
112
            memset( p_out_buf->p_buffer, 0, p_out_buf->i_buffer );
113
        }
114
        p_sys->i_reject_count--;
115
        return;
116
    }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
117

118

119 120
    mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );

121 122 123 124 125 126
    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];

    assert( i_samples == p_out_buf->i_nb_samples );
127
    if ( p_filter->fmt_out.audio.i_format == VLC_CODEC_FI32 )
128 129 130 131
    {
        /* Interleave and keep buffers in mad_fixed_t format */
        mad_fixed_t * p_samples = (mad_fixed_t *)p_out_buf->p_buffer;

132
        if ( p_pcm->channels == 2 )
133
        {
134
            while ( i_samples-- )
135
            {
136 137
                *p_samples++ = *p_left++;
                *p_samples++ = *p_right++;
138
            }
139 140 141 142
        }
        else
        {
            assert( p_pcm->channels == 1 );
143
            vlc_memcpy( p_samples, p_left, i_samples * sizeof(mad_fixed_t) );
144 145 146 147 148 149
        }
    }
    else
    {
        /* float32 */
        float * p_samples = (float *)p_out_buf->p_buffer;
150
        const float f_temp = (float)FIXED32_ONE;
151

152
        if ( p_pcm->channels == 2 )
153
        {
154
            while ( i_samples-- )
155
            {
156 157
                *p_samples++ = (float)*p_left++ / f_temp;
                *p_samples++ = (float)*p_right++ / f_temp;
158
            }
159 160 161 162
        }
        else
        {
            assert( p_pcm->channels == 1 );
163 164
            while ( i_samples-- )
            {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
165
                *p_samples++ = (float)*p_left++ / f_temp;
166 167 168 169 170
            }
        }
    }
}

171
/*****************************************************************************
172
 * OpenFilter:
173 174 175 176 177 178
 *****************************************************************************/
static int OpenFilter( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;
    filter_sys_t *p_sys;

179 180
    if( p_filter->fmt_in.audio.i_format != VLC_CODEC_MPGA &&
        p_filter->fmt_in.audio.i_format != VLC_FOURCC('m','p','g','3') )
181
        return VLC_EGENERIC;
182 183 184 185 186

    if( p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32
     && p_filter->fmt_out.audio.i_format != VLC_CODEC_FI32 )
        return VLC_EGENERIC;

187 188
    if( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
        return VLC_EGENERIC;
189 190 191 192 193

    /* 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;
194
    p_sys->i_reject_count = 0;
195 196 197 198 199 200 201 202 203

    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 );

Clément Stenac's avatar
Clément Stenac committed
204
    msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
205 206 207
             (char *)&p_filter->fmt_in.audio.i_format,
             (char *)&p_filter->fmt_out.audio.i_format,
             p_filter->fmt_out.audio.i_bitspersample );
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

    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 )
{
228
    if( !p_block || !p_block->i_nb_samples )
Gildas Bazin's avatar
*...  
Gildas Bazin committed
229
    {
230 231
        if( p_block )
            block_Release( p_block );
Gildas Bazin's avatar
*...  
Gildas Bazin committed
232 233 234
        return NULL;
    }

235
    size_t i_out_size = p_block->i_nb_samples *
236
      p_filter->fmt_out.audio.i_bitspersample *
237
        p_filter->fmt_out.audio.i_channels / 8;
238

239
    block_t *p_out = block_Alloc( i_out_size );
240 241 242
    if( !p_out )
    {
        msg_Warn( p_filter, "can't get output buffer" );
243
        block_Release( p_block );
244 245 246
        return NULL;
    }

247
    p_out->i_nb_samples = p_block->i_nb_samples;
248 249 250 251
    p_out->i_dts = p_block->i_dts;
    p_out->i_pts = p_block->i_pts;
    p_out->i_length = p_block->i_length;

252
    DoWork( p_filter, p_block, p_out );
253

254
    block_Release( p_block );
Gildas Bazin's avatar
*...  
Gildas Bazin committed
255

256 257
    return p_out;
}