audio.c 11.7 KB
Newer Older
1 2 3
/*****************************************************************************
 * audio.c: audio decoder using ffmpeg library
 *****************************************************************************
4
 * Copyright (C) 1999-2003 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Gildas Bazin <gbazin@videolan.org>
9 10 11 12 13
 *
 * 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.
14
 *
15 16 17 18 19 20 21
 * 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
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

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

32
#include <vlc_common.h>
Clément Stenac's avatar
Clément Stenac committed
33 34
#include <vlc_aout.h>
#include <vlc_codec.h>
35
#include <vlc_input.h>
36

Gildas Bazin's avatar
 
Gildas Bazin committed
37
/* ffmpeg header */
38 39 40
#ifdef HAVE_LIBAVCODEC_AVCODEC_H
#   include <libavcodec/avcodec.h>
#elif defined(HAVE_FFMPEG_AVCODEC_H)
41
#   include <ffmpeg/avcodec.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
42 43 44
#else
#   include <avcodec.h>
#endif
45

46
#include "avcodec.h"
47

48
static unsigned int pi_channels_maps[7] =
49 50
{
    0,
51 52
    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
53 54
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
     | AOUT_CHAN_REARRIGHT,
55
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
56 57 58
     | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
     | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
59
};
60 61

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
62
 * decoder_sys_t : decoder descriptor
63
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
64 65
struct decoder_sys_t
{
66
    FFMPEG_COMMON_MEMBERS
Gildas Bazin's avatar
 
Gildas Bazin committed
67 68 69 70 71 72 73 74 75

    /* Temporary buffer for libavcodec */
    uint8_t *p_output;

    /*
     * Output properties
     */
    audio_sample_format_t aout_format;
    audio_date_t          end_date;
76 77 78 79 80 81

    /*
     *
     */
    uint8_t *p_samples;
    int     i_samples;
82 83 84

    /* */
    int     i_reject_count;
85 86

    int i_input_rate;
Gildas Bazin's avatar
 
Gildas Bazin committed
87
};
88 89

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
90
 * InitAudioDec: initialize audio decoder
91
 *****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
92
 * The ffmpeg codec will be opened, some memory allocated.
93
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
94
int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
95
                      AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
96
{
Gildas Bazin's avatar
 
Gildas Bazin committed
97
    decoder_sys_t *p_sys;
98

Gildas Bazin's avatar
 
Gildas Bazin committed
99 100 101
    /* 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 )
102
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
103
        msg_Err( p_dec, "out of memory" );
104
        return VLC_ENOMEM;
Gildas Bazin's avatar
 
Gildas Bazin committed
105 106
    }

107 108 109 110
    p_sys->p_context = p_context;
    p_sys->p_codec = p_codec;
    p_sys->i_codec_id = i_codec_id;
    p_sys->psz_namecodec = psz_namecodec;
Gildas Bazin's avatar
 
Gildas Bazin committed
111

112
    /* ***** Fill p_context with init values ***** */
Gildas Bazin's avatar
 
Gildas Bazin committed
113 114
    p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
    p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
115 116 117 118 119 120 121 122 123 124 125 126
    if( !p_dec->fmt_in.audio.i_physical_channels )
    {
        msg_Warn( p_dec, "Physical channel configuration not set : guessing" );
        p_dec->fmt_in.audio.i_original_channels =
            p_dec->fmt_in.audio.i_physical_channels =
                pi_channels_maps[p_sys->p_context->channels];
    }

    p_dec->fmt_out.audio.i_physical_channels =
        p_dec->fmt_out.audio.i_original_channels =
        p_dec->fmt_in.audio.i_physical_channels;

Gildas Bazin's avatar
 
Gildas Bazin committed
127 128
    p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
    p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
129
    p_sys->p_context->bits_per_sample = p_dec->fmt_in.audio.i_bitspersample;
130

Gildas Bazin's avatar
 
Gildas Bazin committed
131
    if( ( p_sys->p_context->extradata_size = p_dec->fmt_in.i_extra ) > 0 )
132
    {
133 134 135 136 137 138
        int i_offset = 0;

        if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
            i_offset = 8;

        p_sys->p_context->extradata_size -= i_offset;
Gildas Bazin's avatar
 
Gildas Bazin committed
139
        p_sys->p_context->extradata =
140 141
            malloc( p_sys->p_context->extradata_size +
                    FF_INPUT_BUFFER_PADDING_SIZE );
Gildas Bazin's avatar
 
Gildas Bazin committed
142
        memcpy( p_sys->p_context->extradata,
143
                (char*)p_dec->fmt_in.p_extra + i_offset,
144 145 146
                p_sys->p_context->extradata_size );
        memset( (char*)p_sys->p_context->extradata +
                p_sys->p_context->extradata_size, 0,
Gildas Bazin's avatar
 
Gildas Bazin committed
147
                FF_INPUT_BUFFER_PADDING_SIZE );
148
    }
149 150
    else
        p_sys->p_context->extradata = NULL;
151

152
    /* ***** Open the codec ***** */
153 154 155 156 157 158 159 160
    vlc_mutex_t *lock = var_AcquireMutex( "avcodec" );
    if( lock == NULL )
    {
        free( p_sys->p_context->extradata );
        free( p_sys );
        return VLC_ENOMEM;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
161
    if (avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0)
162
    {
163
        vlc_mutex_unlock( lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
164
        msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
165
        free( p_sys->p_context->extradata );
166
        free( p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
167
        return VLC_EGENERIC;
168
    }
169
    vlc_mutex_unlock( lock );
170 171

    msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
172

173
    p_sys->p_output = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
174 175
    p_sys->p_samples = NULL;
    p_sys->i_samples = 0;
176
    p_sys->i_reject_count = 0;
177
    p_sys->i_input_rate = INPUT_RATE_DEFAULT;
178

Laurent Aimar's avatar
Laurent Aimar committed
179
    aout_DateSet( &p_sys->end_date, 0 );
180 181
    if( p_dec->fmt_in.audio.i_rate )
        aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
182

Gildas Bazin's avatar
 
Gildas Bazin committed
183 184 185
    /* Set output properties */
    p_dec->fmt_out.i_cat = AUDIO_ES;
    p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
186
    p_dec->fmt_out.audio.i_bitspersample = 16;
Gildas Bazin's avatar
 
Gildas Bazin committed
187

Gildas Bazin's avatar
 
Gildas Bazin committed
188
    return VLC_SUCCESS;
189 190
}

191 192 193 194
/*****************************************************************************
 * SplitBuffer: Needed because aout really doesn't like big audio chunk and
 * wma produces easily > 30000 samples...
 *****************************************************************************/
Clément Stenac's avatar
Clément Stenac committed
195
static aout_buffer_t *SplitBuffer( decoder_t *p_dec )
196 197 198 199 200
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    int i_samples = __MIN( p_sys->i_samples, 4096 );
    aout_buffer_t *p_buffer;

201 202
    if( i_samples == 0 ) return NULL;

203 204 205 206 207 208 209
    if( ( p_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples ) ) == NULL )
    {
        msg_Err( p_dec, "cannot get aout buffer" );
        return NULL;
    }

    p_buffer->start_date = aout_DateGet( &p_sys->end_date );
210 211
    p_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
                                             i_samples * p_sys->i_input_rate / INPUT_RATE_DEFAULT );
212 213 214 215 216 217 218 219 220

    memcpy( p_buffer->p_buffer, p_sys->p_samples, p_buffer->i_nb_bytes );

    p_sys->p_samples += p_buffer->i_nb_bytes;
    p_sys->i_samples -= i_samples;

    return p_buffer;
}

221
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
222
 * DecodeAudio: Called to decode one frame
223
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
224
aout_buffer_t * DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
225
{
Gildas Bazin's avatar
 
Gildas Bazin committed
226
    decoder_sys_t *p_sys = p_dec->p_sys;
227
    int i_used, i_output;
Gildas Bazin's avatar
 
Gildas Bazin committed
228 229 230 231
    aout_buffer_t *p_buffer;
    block_t *p_block;

    if( !pp_block || !*pp_block ) return NULL;
232

Gildas Bazin's avatar
 
Gildas Bazin committed
233
    p_block = *pp_block;
234

235 236 237
    if( p_block->i_rate > 0 )
        p_sys->i_input_rate = p_block->i_rate;

Laurent Aimar's avatar
Laurent Aimar committed
238 239 240 241
    if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
    {
        block_Release( p_block );
        avcodec_flush_buffers( p_sys->p_context );
242 243 244

        if( p_sys->i_codec_id == CODEC_ID_MP2 || p_sys->i_codec_id == CODEC_ID_MP3 )
            p_sys->i_reject_count = 3;
Laurent Aimar's avatar
Laurent Aimar committed
245 246 247 248
        return NULL;
    }

    if( p_sys->i_samples > 0 )
249 250 251
    {
        /* More data */
        p_buffer = SplitBuffer( p_dec );
252
        if( !p_buffer ) block_Release( p_block );
253 254 255
        return p_buffer;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
256 257 258 259
    if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
    {
        /* We've just started the stream, wait for the first PTS. */
        block_Release( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
260
        return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
261
    }
262

Laurent Aimar's avatar
Laurent Aimar committed
263
    if( p_block->i_buffer <= 0 )
264
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
265 266 267
        block_Release( p_block );
        return NULL;
    }
268 269 270 271 272 273
    if( p_block->i_buffer > AVCODEC_MAX_AUDIO_FRAME_SIZE )
    {
        /* Grow output buffer if necessary (eg. for PCM data) */
        p_sys->p_output = realloc(p_sys->p_output, p_block->i_buffer);
    }

Laurent Aimar's avatar
Laurent Aimar committed
274 275 276 277 278 279
    *pp_block = p_block = block_Realloc( p_block, 0, p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
    if( !p_block )
        return NULL;
    p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
    memset( &p_block->p_buffer[p_block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );

280
#if LIBAVCODEC_VERSION_INT >= ((52<<16)+(0<<8)+0)
Laurent Aimar's avatar
Laurent Aimar committed
281 282
    i_output = __MAX( AVCODEC_MAX_AUDIO_FRAME_SIZE, p_block->i_buffer );
    i_used = avcodec_decode_audio2( p_sys->p_context,
Gildas Bazin's avatar
 
Gildas Bazin committed
283 284
                                   (int16_t*)p_sys->p_output, &i_output,
                                   p_block->p_buffer, p_block->i_buffer );
285 286 287 288 289
#else
    i_used = avcodec_decode_audio( p_sys->p_context,
                                   (int16_t*)p_sys->p_output, &i_output,
                                   p_block->p_buffer, p_block->i_buffer );
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
290

291
    if( i_used < 0 || i_output < 0 )
Gildas Bazin's avatar
 
Gildas Bazin committed
292
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
293
        if( i_used < 0 )
Gildas Bazin's avatar
 
Gildas Bazin committed
294 295 296 297 298
            msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
                      p_block->i_buffer );

        block_Release( p_block );
        return NULL;
299
    }
300
    else if( (size_t)i_used > p_block->i_buffer )
301 302 303
    {
        i_used = p_block->i_buffer;
    }
304

Gildas Bazin's avatar
 
Gildas Bazin committed
305 306 307 308 309 310 311 312 313 314 315
    p_block->i_buffer -= i_used;
    p_block->p_buffer += i_used;

    if( p_sys->p_context->channels <= 0 || p_sys->p_context->channels > 6 )
    {
        msg_Warn( p_dec, "invalid channels count %d",
                  p_sys->p_context->channels );
        block_Release( p_block );
        return NULL;
    }

316
    if( p_dec->fmt_out.audio.i_rate != (unsigned int)p_sys->p_context->sample_rate )
Gildas Bazin's avatar
 
Gildas Bazin committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    {
        aout_DateInit( &p_sys->end_date, p_sys->p_context->sample_rate );
        aout_DateSet( &p_sys->end_date, p_block->i_pts );
    }

    /* **** Set audio output parameters **** */
    p_dec->fmt_out.audio.i_rate     = p_sys->p_context->sample_rate;
    p_dec->fmt_out.audio.i_channels = p_sys->p_context->channels;
    p_dec->fmt_out.audio.i_original_channels =
        p_dec->fmt_out.audio.i_physical_channels =
            pi_channels_maps[p_sys->p_context->channels];

    if( p_block->i_pts != 0 &&
        p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
    {
        aout_DateSet( &p_sys->end_date, p_block->i_pts );
    }
334
    p_block->i_pts = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
335 336

    /* **** Now we can output these samples **** */
337
    p_sys->i_samples = i_output / sizeof(int16_t) / p_sys->p_context->channels;
338
    p_sys->p_samples = p_sys->p_output;
Gildas Bazin's avatar
 
Gildas Bazin committed
339

340 341 342 343 344 345 346
    /* Silent unwanted samples */
    if( p_sys->i_reject_count > 0 )
    {
        memset( p_sys->p_output, 0, i_output );
        p_sys->i_reject_count--;
    }

347
    p_buffer = SplitBuffer( p_dec );
348
    if( !p_buffer ) block_Release( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
349
    return p_buffer;
350 351 352
}

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
353
 * EndAudioDec: audio decoder destruction
354
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
355
void EndAudioDec( decoder_t *p_dec )
356
{
Gildas Bazin's avatar
 
Gildas Bazin committed
357 358
    decoder_sys_t *p_sys = p_dec->p_sys;

359
    free( p_sys->p_output );
360
}