audio.c 11.5 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * audio.c: audio decoder using ffmpeg library
 *****************************************************************************
 * Copyright (C) 1999-2001 VideoLAN
5
 * $Id: audio.c,v 1.13 2003/01/25 16:59:49 fenrir Exp $
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 *
 * 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */

#include <vlc/vlc.h>
#include <vlc/vout.h>
#include <vlc/aout.h>
#include <vlc/decoder.h>
#include <vlc/input.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>                                              /* getpid() */
#endif

#include <errno.h>
#include <string.h>

#ifdef HAVE_SYS_TIMES_H
#   include <sys/times.h>
#endif
45
#include "codecs.h"
46
#include "aout_internal.h"
47 48 49

#include "avcodec.h"                                            /* ffmpeg */

50
//#include "postprocessing/postprocessing.h"
51 52 53 54 55 56 57 58 59 60
#include "ffmpeg.h"
#include "audio.h"

/*
 * Local prototypes
 */
int      E_( InitThread_Audio )   ( adec_thread_t * );
void     E_( EndThread_Audio )    ( adec_thread_t * );
void     E_( DecodeThread_Audio ) ( adec_thread_t * );

61
static unsigned int pi_channels_maps[6] =
62 63
{
    0,
64 65 66 67 68
    AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
    AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
    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
69 70 71 72 73
};  

/*****************************************************************************
 * locales Functions
 *****************************************************************************/
74
#if 0
75 76 77
static void ffmpeg_GetWaveFormatEx( waveformatex_t *p_wh,
                                    u8 *p_data )
{
78 79 80 81 82 83 84 85 86
    WAVEFORMATEX *p_wfdata = (WAVEFORMATEX*)p_data;
    
    p_wh->i_formattag     = p_wfdata->wFormatTag;
    p_wh->i_nb_channels   = p_wfdata->nChannels;
    p_wh->i_samplespersec = p_wfdata->nSamplesPerSec;
    p_wh->i_avgbytespersec= p_wfdata->nAvgBytesPerSec;
    p_wh->i_blockalign    = p_wfdata->nBlockAlign;
    p_wh->i_bitspersample = p_wfdata->wBitsPerSample;
    p_wh->i_size          = p_wfdata->cbSize;
87 88 89 90

    if( p_wh->i_size )
    {
        p_wh->p_data = malloc( p_wh->i_size );
91 92 93
        memcpy( p_wh->p_data, 
                p_data + sizeof(WAVEFORMATEX) , 
                p_wh->i_size );
94 95
    }
}
96
#endif
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

/*****************************************************************************
 *
 * Functions that initialize, decode and end the decoding process
 *
 * Functions exported for ffmpeg.c
 *   * E_( InitThread_Audio )
 *   * E_( DecodeThread_Audio )
 *   * E_( EndThread_Video_Audio )
 *****************************************************************************/

/*****************************************************************************
 * InitThread: initialize vdec output thread
 *****************************************************************************
 * This function is called from decoder_Run and performs the second step 
 * of the initialization. It returns 0 on success. Note that the thread's 
 * flag are not modified inside this function.
 *
 * ffmpeg codec will be open, some memory allocated.
 *****************************************************************************/
int E_( InitThread_Audio )( adec_thread_t *p_adec )
{
119
    WAVEFORMATEX wf, *p_wf;
120

121
    if( ( p_wf = p_adec->p_fifo->p_waveformatex ) == NULL )
122 123
    {
        msg_Warn( p_adec->p_fifo, "audio informations missing" );
124 125
        p_wf = &wf;
        memset( p_wf, 0, sizeof( WAVEFORMATEX ) );
126 127 128
    }

    /* ***** Fill p_context with init values ***** */
129 130
    p_adec->p_context->sample_rate = p_wf->nSamplesPerSec;
    p_adec->p_context->channels = p_wf->nChannels;
gbazin's avatar
 
gbazin committed
131
#if LIBAVCODEC_BUILD >= 4618
132
    p_adec->p_context->block_align = p_wf->nBlockAlign;
gbazin's avatar
 
gbazin committed
133
#endif
134
    p_adec->p_context->bit_rate = p_wf->nAvgBytesPerSec * 8;
135

136
    if( ( p_adec->p_context->extradata_size = p_wf->cbSize ) > 0 )
137
    {
138 139
        p_adec->p_context->extradata =
            malloc( p_wf->cbSize );
140 141

        memcpy( p_adec->p_context->extradata,
142 143
                &p_wf[1],
                p_wf->cbSize);
144
    }
145

146
    /* ***** Open the codec ***** */
147 148
    if (avcodec_open(p_adec->p_context, p_adec->p_codec) < 0)
    {
149
        msg_Err( p_adec->p_fifo,
150 151 152 153 154 155
                 "cannot open codec (%s)",
                 p_adec->psz_namecodec );
        return( -1 );
    }
    else
    {
156
        msg_Dbg( p_adec->p_fifo,
157 158 159 160
                 "ffmpeg codec (%s) started",
                 p_adec->psz_namecodec );
    }

161 162
    p_adec->p_output = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );

163 164

    p_adec->output_format.i_format = AOUT_FMT_S16_NE;
165
    p_adec->output_format.i_rate = p_wf->nSamplesPerSec;
166 167
    p_adec->output_format.i_physical_channels
        = p_adec->output_format.i_original_channels
168
        = p_wf->nChannels;
169

170 171
    p_adec->p_aout = NULL;
    p_adec->p_aout_input = NULL;
172

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    return( 0 );
}


/*****************************************************************************
 * DecodeThread: Called for decode one frame
 *****************************************************************************/
void  E_( DecodeThread_Audio )( adec_thread_t *p_adec )
{
    pes_packet_t    *p_pes;
    aout_buffer_t   *p_aout_buffer;

    int     i_samplesperchannel;
    int     i_output_size;
    int     i_frame_size;
188
    int     i_used;
189 190 191 192 193 194 195 196 197 198 199 200 201 202

    do
    {
        input_ExtractPES( p_adec->p_fifo, &p_pes );
        if( !p_pes )
        {
            p_adec->p_fifo->b_error = 1;
            return;
        }
        p_adec->pts = p_pes->i_pts;
        i_frame_size = p_pes->i_pes_size;

        if( i_frame_size > 0 )
        {
203 204 205 206 207 208
            uint8_t *p_last;
            int     i_need;


            i_need = i_frame_size + 16 + p_adec->i_buffer;
            if( p_adec->i_buffer_size < i_need )
209
            {
210 211 212 213 214 215 216 217
                p_last = p_adec->p_buffer;
                p_adec->p_buffer = malloc( i_need );
                p_adec->i_buffer_size = i_need;
                if( p_adec->i_buffer > 0 )
                {
                    memcpy( p_adec->p_buffer, p_last, p_adec->i_buffer );
                }
                FREE( p_last );
218
            }
219 220 221 222 223 224 225 226
            i_frame_size =
                E_( GetPESData )( p_adec->p_buffer + p_adec->i_buffer,
                                  i_frame_size,
                                  p_pes );
            /* make ffmpeg happier but I'm not sure it's needed for audio */
            memset( p_adec->p_buffer + p_adec->i_buffer + i_frame_size,
                    0,
                    16 );
227 228 229 230
        }
        input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
    } while( i_frame_size <= 0 );

231 232 233 234 235 236 237 238 239 240

    i_frame_size += p_adec->i_buffer;

usenextdata:
    i_used = avcodec_decode_audio( p_adec->p_context,
                                   (int16_t*)p_adec->p_output,
                                   &i_output_size,
                                   p_adec->p_buffer,
                                   i_frame_size );
    if( i_used < 0 )
241
    {
242
        msg_Warn( p_adec->p_fifo,
243 244
                  "cannot decode one frame (%d bytes)",
                  i_frame_size );
245
        p_adec->i_buffer = 0;
246 247
        return;
    }
248 249 250 251 252 253 254 255 256 257 258 259
    else if( i_used < i_frame_size )
    {
        memmove( p_adec->p_buffer,
                 p_adec->p_buffer + i_used,
                 p_adec->i_buffer_size - i_used );

        p_adec->i_buffer = i_frame_size - i_used;
    }
    else
    {
        p_adec->i_buffer = 0;
    }
260

261 262 263
    i_frame_size -= i_used;

//    msg_Dbg( p_adec->p_fifo, "frame size:%d buffer used:%d", i_frame_size, i_used );
264 265 266 267 268 269 270 271 272 273 274
    if( i_output_size <= 0 )
    {
         msg_Warn( p_adec->p_fifo, 
                  "decoded %d samples bytes",
                  i_output_size );
    }

    if( p_adec->p_context->channels <= 0 || 
        p_adec->p_context->channels > 5 )
    {
        msg_Warn( p_adec->p_fifo,
275
                  "invalid channels count %d",
276 277 278 279
                  p_adec->p_context->channels );
    }

    /* **** Now we can output these samples **** */
280 281
    i_samplesperchannel = i_output_size / 2
                           / aout_FormatNbChannels( &p_adec->output_format );
282
    /* **** First check if we have a valid output **** */
283 284 285
    if( ( p_adec->p_aout_input == NULL )||
        ( p_adec->output_format.i_original_channels != 
                    pi_channels_maps[p_adec->p_context->channels] ) )
286
    {
287
        if( p_adec->p_aout_input != NULL )
288 289 290 291 292 293
        {
            /* **** Delete the old **** */
            aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
        }

        /* **** Create a new audio output **** */
294 295 296
        p_adec->output_format.i_physical_channels = 
            p_adec->output_format.i_original_channels = 
                pi_channels_maps[p_adec->p_context->channels];
297 298 299 300 301 302 303 304 305 306 307 308

        aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
        p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
                                            &p_adec->p_aout,
                                            &p_adec->output_format );
    }

    if( !p_adec->p_aout_input )
    {
        msg_Err( p_adec->p_fifo, "cannot create aout" );
        return;
    }
309

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
    {
        aout_DateSet( &p_adec->date, p_adec->pts );
    }
    else if( !aout_DateGet( &p_adec->date ) )
    {
        return;
    }

    p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
                                       p_adec->p_aout_input,
                                       i_samplesperchannel );
    if( !p_aout_buffer )
    {
        msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
        p_adec->p_fifo->b_error = 1;
        return;
    }

    p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
    p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
                                                  i_samplesperchannel );
    memcpy( p_aout_buffer->p_buffer,
            p_adec->p_output,
            p_aout_buffer->i_nb_bytes );

    aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
337 338 339 340 341 342

    if( i_frame_size > 0 )
    {
        goto usenextdata;
    }

343 344 345 346 347 348 349 350 351 352 353 354
    return;
}


/*****************************************************************************
 * EndThread: thread destruction
 *****************************************************************************
 * This function is called when the thread ends after a sucessful
 * initialization.
 *****************************************************************************/
void E_( EndThread_Audio )( adec_thread_t *p_adec )
{
355 356
//    FREE( p_adec->format.p_data );
    FREE( p_adec->p_output );
357 358 359 360 361

    if( p_adec->p_aout_input )
    {
        aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
    }
362

363 364
}