faad.c 9.55 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * decoder.c: AAC decoder using libfaad2
 *****************************************************************************
 * Copyright (C) 2001, 2003 VideoLAN
5
 * $Id: faad.c,v 1.5 2003/11/22 23:39:14 fenrir Exp $
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

24
#include <vlc/vlc.h>
25 26 27 28 29 30 31 32 33
#include <vlc/aout.h>
#include <vlc/decoder.h>

#include <faad.h>

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open( vlc_object_t * );
34
static void Close( vlc_object_t * );
35 36 37 38

vlc_module_begin();
    set_description( _("AAC audio decoder (using libfaad2)") );
    set_capability( "decoder", 60 );
39
    set_callbacks( Open, Close );
40 41 42 43 44 45
vlc_module_end();


/****************************************************************************
 * Local prototypes
 ****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
46
static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
47 48 49 50 51 52 53

struct decoder_sys_t
{
    /* faad handler */
    faacDecHandle *hfaad;

    /* samples */
Gildas Bazin's avatar
 
Gildas Bazin committed
54
    audio_date_t date;
55 56

    /* temporary buffer */
Gildas Bazin's avatar
 
Gildas Bazin committed
57 58 59
    uint8_t *p_buffer;
    int     i_buffer;
    int     i_buffer_size;
60 61 62 63 64 65 66 67
};

static unsigned int pi_channels_maps[7] =
{
    0,
    AOUT_CHAN_CENTER,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
    AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
Gildas Bazin's avatar
 
Gildas Bazin committed
68 69 70 71
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT |
        AOUT_CHAN_REARRIGHT,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
        AOUT_CHAN_REARRIGHT,
72
     /* FIXME */
Gildas Bazin's avatar
 
Gildas Bazin committed
73 74
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
        AOUT_CHAN_REARRIGHT | AOUT_CHAN_REARCENTER
75 76 77
};

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
78
 * OpenDecoder: probe the decoder and return score
79
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
80
static int Open( vlc_object_t *p_this )
81
{
Gildas Bazin's avatar
 
Gildas Bazin committed
82
    decoder_t *p_dec = (decoder_t*)p_this;
83 84 85
    decoder_sys_t *p_sys = p_dec->p_sys;
    faacDecConfiguration *cfg;

Gildas Bazin's avatar
 
Gildas Bazin committed
86
    if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','4','a') )
87
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
88 89 90 91 92 93 94 95 96
        return VLC_EGENERIC;
    }

    /* Allocate the memory needed to store the decoder's structure */
    if( ( p_dec->p_sys = p_sys =
          (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
    {
        msg_Err( p_dec, "out of memory" );
        return VLC_EGENERIC;
97 98 99 100 101 102 103 104 105
    }

    /* Open a faad context */
    if( ( p_sys->hfaad = faacDecOpen() ) == NULL )
    {
        msg_Err( p_dec, "Cannot initialize faad" );
        return VLC_EGENERIC;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
106 107 108 109 110 111 112
    /* Misc init */
    aout_DateSet( &p_sys->date, 0 );
    p_dec->fmt_out.i_cat = AUDIO_ES;
    p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
    p_dec->pf_decode_audio = DecodeBlock;

    if( p_dec->fmt_in.i_extra > 0 )
113 114
    {
        /* We have a decoder config so init the handle */
Gildas Bazin's avatar
 
Gildas Bazin committed
115 116
        unsigned long i_rate;
        unsigned char i_channels;
117

Gildas Bazin's avatar
 
Gildas Bazin committed
118 119
        if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
                          p_dec->fmt_in.i_extra,
120 121 122 123 124
                          &i_rate, &i_channels ) < 0 )
        {
            return VLC_EGENERIC;
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
125 126 127 128 129 130 131
        p_dec->fmt_out.audio.i_rate = i_rate;
        p_dec->fmt_out.audio.i_channels = i_channels;
        p_dec->fmt_out.audio.i_physical_channels =
            p_dec->fmt_out.audio.i_original_channels =
                pi_channels_maps[i_channels];

        aout_DateInit( &p_sys->date, i_rate );
132 133 134 135
    }
    else
    {
        /* Will be initalised from first frame */
Gildas Bazin's avatar
 
Gildas Bazin committed
136 137 138 139
        p_dec->fmt_out.audio.i_rate = 0;
        p_dec->fmt_out.audio.i_channels = 0;
        p_dec->fmt_out.audio.i_physical_channels =
            p_dec->fmt_out.audio.i_original_channels = 0;
140 141
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
142
    /* Set the faad config */
143 144 145 146 147 148 149 150 151 152 153 154 155
    cfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
    cfg->outputFormat = FAAD_FMT_FLOAT;
    faacDecSetConfiguration( p_sys->hfaad, cfg );

    /* buffer */
    p_sys->i_buffer = 0;
    p_sys->i_buffer_size = 10000;
    p_sys->p_buffer = malloc( p_sys->i_buffer_size );

    return VLC_SUCCESS;
}

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
156
 * DecodeBlock:
157
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
158
static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
159 160
{
    decoder_sys_t *p_sys = p_dec->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
161
    block_t *p_block;
162

Gildas Bazin's avatar
 
Gildas Bazin committed
163 164 165
    if( !pp_block || !*pp_block ) return NULL;

    p_block = *pp_block;
166 167 168 169

    /* Append the block to the temporary buffer */
    if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
170
        p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer;
171 172 173
        p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
174 175 176 177 178 179 180 181 182
    if( p_block->i_buffer )
    {
        memcpy( &p_sys->p_buffer[p_sys->i_buffer],
                p_block->p_buffer, p_block->i_buffer );
        p_sys->i_buffer += p_block->i_buffer;
        p_block->i_buffer = 0;
    }

    if( p_dec->fmt_out.audio.i_rate == 0 && p_sys->i_buffer )
183
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
184 185
        unsigned long i_rate;
        unsigned char i_channels;
186 187 188 189 190 191

        /* Init faad with the first frame */
        if( faacDecInit( p_sys->hfaad,
                         p_sys->p_buffer, p_sys->i_buffer,
                         &i_rate, &i_channels ) < 0 )
        {
192
            block_Release( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
193
            return NULL;
194
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

        p_dec->fmt_out.audio.i_rate = i_rate;
        p_dec->fmt_out.audio.i_channels = i_channels;
        p_dec->fmt_out.audio.i_physical_channels =
            p_dec->fmt_out.audio.i_original_channels =
                pi_channels_maps[i_channels];

        aout_DateInit( &p_sys->date, i_rate );
    }

    if( p_block->i_pts != 0 && p_block->i_pts != aout_DateGet( &p_sys->date ) )
    {
        aout_DateSet( &p_sys->date, p_block->i_pts );
    }
    else if( !aout_DateGet( &p_sys->date ) )
    {
        /* We've just started the stream, wait for the first PTS. */
        block_Release( p_block );
        p_sys->i_buffer = 0;
        return NULL;
215 216 217
    }

    /* Decode all data */
Gildas Bazin's avatar
 
Gildas Bazin committed
218
    if( p_sys->i_buffer )
219 220 221
    {
        void *samples;
        faacDecFrameInfo frame;
Gildas Bazin's avatar
 
Gildas Bazin committed
222
        aout_buffer_t *p_out;
223 224

        samples = faacDecDecode( p_sys->hfaad, &frame,
Gildas Bazin's avatar
 
Gildas Bazin committed
225
                                 p_sys->p_buffer, p_sys->i_buffer );
226 227 228 229

        if( frame.error > 0 )
        {
            msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
230 231

            /* Flush the buffer */
232
            p_sys->i_buffer = 0;
233
            block_Release( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
234
            return NULL;
235
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
236

237 238 239
        if( frame.channels <= 0 || frame.channels > 6 )
        {
            msg_Warn( p_dec, "invalid channels count" );
Gildas Bazin's avatar
 
Gildas Bazin committed
240 241

            /* Flush the buffer */
242
            p_sys->i_buffer = 0;
243
            block_Release( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
244
            return NULL;
245
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
246

247 248 249
        if( frame.samples <= 0 )
        {
            msg_Warn( p_dec, "decoded zero samples" );
Gildas Bazin's avatar
 
Gildas Bazin committed
250 251

            /* Flush the buffer */
252
            p_sys->i_buffer = 0;
253
            block_Release( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
254
            return NULL;
255 256
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
257 258
        /* We decoded a valid frame */
        if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
259
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
260 261
            aout_DateInit( &p_sys->date, frame.samplerate );
            aout_DateSet( &p_sys->date, p_block->i_pts );
262
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
263
        p_block->i_pts = 0;  /* PTS is valid only once */
264

Gildas Bazin's avatar
 
Gildas Bazin committed
265 266 267 268 269
        p_dec->fmt_out.audio.i_rate = frame.samplerate;
        p_dec->fmt_out.audio.i_channels = frame.channels;
        p_dec->fmt_out.audio.i_physical_channels =
            p_dec->fmt_out.audio.i_original_channels =
                pi_channels_maps[frame.channels];
270

Gildas Bazin's avatar
 
Gildas Bazin committed
271 272 273
        p_out = p_dec->pf_aout_buffer_new( p_dec,
                                           frame.samples / frame.channels );
        if( p_out == NULL )
274
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
275
            p_sys->i_buffer = 0;
276
            block_Release( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
277
            return NULL;
278 279
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
280 281 282 283 284
        p_out->start_date = aout_DateGet( &p_sys->date );
        p_out->end_date = aout_DateIncrement( &p_sys->date,
                                              frame.samples / frame.channels );

        memcpy( p_out->p_buffer, samples, p_out->i_nb_bytes );
285

Gildas Bazin's avatar
 
Gildas Bazin committed
286 287
        p_sys->i_buffer -= frame.bytesconsumed;
        if( p_sys->i_buffer > 0 )
288
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
289 290
            memmove( p_sys->p_buffer, &p_sys->p_buffer[frame.bytesconsumed],
                     p_sys->i_buffer );
291 292
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
293
        return p_out;
294 295
    }

296
    block_Release( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
297
    return NULL;
298 299 300
}

/*****************************************************************************
301
 * Close:
302
 *****************************************************************************/
303
static void Close( vlc_object_t *p_this )
304
{
Gildas Bazin's avatar
 
Gildas Bazin committed
305
    decoder_t *p_dec = (decoder_t *)p_this;
306 307 308 309 310
    decoder_sys_t *p_sys = p_dec->p_sys;

    faacDecClose( p_sys->hfaad );
    free( p_sys );
}