faad.c 9.65 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * decoder.c: AAC decoder using libfaad2
 *****************************************************************************
 * Copyright (C) 2001, 2003 VideoLAN
5
 * $Id: faad.c,v 1.6 2003/11/27 20:51:31 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
 ****************************************************************************/
gbazin's avatar
   
gbazin 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 */
gbazin's avatar
   
gbazin committed
54
    audio_date_t date;
55
56

    /* temporary buffer */
gbazin's avatar
   
gbazin 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,
gbazin's avatar
   
gbazin 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 */
gbazin's avatar
   
gbazin committed
73
74
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
        AOUT_CHAN_REARRIGHT | AOUT_CHAN_REARCENTER
75
76
77
};

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

gbazin's avatar
   
gbazin committed
86
    if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','4','a') )
87
    {
gbazin's avatar
   
gbazin 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;
    }

gbazin's avatar
   
gbazin 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 */
gbazin's avatar
   
gbazin committed
115
116
        unsigned long i_rate;
        unsigned char i_channels;
117

gbazin's avatar
   
gbazin 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;
        }

gbazin's avatar
   
gbazin 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 */
gbazin's avatar
   
gbazin 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
    }

gbazin's avatar
   
gbazin 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;
}

/*****************************************************************************
gbazin's avatar
   
gbazin committed
156
 * DecodeBlock:
157
 *****************************************************************************/
gbazin's avatar
   
gbazin 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;
gbazin's avatar
   
gbazin committed
161
    block_t *p_block;
162

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

    p_block = *pp_block;
166

167
168
169
170
171
172
    if( p_block->b_discontinuity )
    {
        block_Release( p_block );
        return NULL;
    }

173
174
175
    /* Append the block to the temporary buffer */
    if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
    {
gbazin's avatar
   
gbazin committed
176
        p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer;
177
178
179
        p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
    }

gbazin's avatar
   
gbazin committed
180
181
182
183
184
185
186
187
188
    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 )
189
    {
gbazin's avatar
   
gbazin committed
190
191
        unsigned long i_rate;
        unsigned char i_channels;
192
193
194
195
196
197

        /* Init faad with the first frame */
        if( faacDecInit( p_sys->hfaad,
                         p_sys->p_buffer, p_sys->i_buffer,
                         &i_rate, &i_channels ) < 0 )
        {
198
            block_Release( p_block );
gbazin's avatar
   
gbazin committed
199
            return NULL;
200
        }
gbazin's avatar
   
gbazin committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

        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;
221
222
223
    }

    /* Decode all data */
gbazin's avatar
   
gbazin committed
224
    if( p_sys->i_buffer )
225
226
227
    {
        void *samples;
        faacDecFrameInfo frame;
gbazin's avatar
   
gbazin committed
228
        aout_buffer_t *p_out;
229
230

        samples = faacDecDecode( p_sys->hfaad, &frame,
gbazin's avatar
   
gbazin committed
231
                                 p_sys->p_buffer, p_sys->i_buffer );
232
233
234
235

        if( frame.error > 0 )
        {
            msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
gbazin's avatar
   
gbazin committed
236
237

            /* Flush the buffer */
238
            p_sys->i_buffer = 0;
239
            block_Release( p_block );
gbazin's avatar
   
gbazin committed
240
            return NULL;
241
        }
gbazin's avatar
   
gbazin committed
242

243
244
245
        if( frame.channels <= 0 || frame.channels > 6 )
        {
            msg_Warn( p_dec, "invalid channels count" );
gbazin's avatar
   
gbazin committed
246
247

            /* Flush the buffer */
248
            p_sys->i_buffer = 0;
249
            block_Release( p_block );
gbazin's avatar
   
gbazin committed
250
            return NULL;
251
        }
gbazin's avatar
   
gbazin committed
252

253
254
255
        if( frame.samples <= 0 )
        {
            msg_Warn( p_dec, "decoded zero samples" );
gbazin's avatar
   
gbazin committed
256
257

            /* Flush the buffer */
258
            p_sys->i_buffer = 0;
259
            block_Release( p_block );
gbazin's avatar
   
gbazin committed
260
            return NULL;
261
262
        }

gbazin's avatar
   
gbazin committed
263
264
        /* We decoded a valid frame */
        if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
265
        {
gbazin's avatar
   
gbazin committed
266
267
            aout_DateInit( &p_sys->date, frame.samplerate );
            aout_DateSet( &p_sys->date, p_block->i_pts );
268
        }
gbazin's avatar
   
gbazin committed
269
        p_block->i_pts = 0;  /* PTS is valid only once */
270

gbazin's avatar
   
gbazin committed
271
272
273
274
275
        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];
276

gbazin's avatar
   
gbazin committed
277
278
279
        p_out = p_dec->pf_aout_buffer_new( p_dec,
                                           frame.samples / frame.channels );
        if( p_out == NULL )
280
        {
gbazin's avatar
   
gbazin committed
281
            p_sys->i_buffer = 0;
282
            block_Release( p_block );
gbazin's avatar
   
gbazin committed
283
            return NULL;
284
285
        }

gbazin's avatar
   
gbazin committed
286
287
288
289
290
        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 );
291

gbazin's avatar
   
gbazin committed
292
293
        p_sys->i_buffer -= frame.bytesconsumed;
        if( p_sys->i_buffer > 0 )
294
        {
gbazin's avatar
   
gbazin committed
295
296
            memmove( p_sys->p_buffer, &p_sys->p_buffer[frame.bytesconsumed],
                     p_sys->i_buffer );
297
298
        }

gbazin's avatar
   
gbazin committed
299
        return p_out;
300
301
    }

302
    block_Release( p_block );
gbazin's avatar
   
gbazin committed
303
    return NULL;
304
305
306
}

/*****************************************************************************
307
 * Close:
308
 *****************************************************************************/
309
static void Close( vlc_object_t *p_this )
310
{
gbazin's avatar
   
gbazin committed
311
    decoder_t *p_dec = (decoder_t *)p_this;
312
313
314
315
316
    decoder_sys_t *p_sys = p_dec->p_sys;

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