]> git.sesse.net Git - vlc/blobdiff - modules/codec/ffmpeg/audio.c
* block: added
[vlc] / modules / codec / ffmpeg / audio.c
index bd0ce1cf722d6efee93eb6ee79a2da2412d85a67..1a3c3b5edc59eeeb64d46736251044d6a141e557 100644 (file)
@@ -2,10 +2,10 @@
  * audio.c: audio decoder using ffmpeg library
  *****************************************************************************
  * Copyright (C) 1999-2003 VideoLAN
- * $Id: audio.c,v 1.23 2003/11/17 02:52:39 fenrir Exp $
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
- *          Gildas Bazin <gbazin@netcourrier.com>
+ *          Gildas Bazin <gbazin@videolan.org>
  *
  * 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
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
-#include <string.h>
-
 #include <vlc/vlc.h>
 #include <vlc/decoder.h>
-#include <vlc/input.h>
-
-#include "codecs.h"
-#include "aout_internal.h"
 
 /* ffmpeg header */
 #ifdef HAVE_FFMPEG_AVCODEC_H
@@ -44,7 +37,7 @@
 
 #include "ffmpeg.h"
 
-static unsigned int pi_channels_maps[6] =
+static unsigned int pi_channels_maps[7] =
 {
     0,
     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
@@ -52,7 +45,9 @@ static unsigned int pi_channels_maps[6] =
     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
+     | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
+    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
+     | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
 };
 
 /*****************************************************************************
@@ -75,6 +70,12 @@ struct decoder_sys_t
      */
     audio_sample_format_t aout_format;
     audio_date_t          end_date;
+
+    /*
+     *
+     */
+    uint8_t *p_samples;
+    int     i_samples;
 };
 
 /*****************************************************************************
@@ -86,6 +87,9 @@ int E_(InitAudioDec)( decoder_t *p_dec, AVCodecContext *p_context,
                       AVCodec *p_codec, int i_codec_id, char *psz_namecodec )
 {
     decoder_sys_t *p_sys;
+    vlc_value_t lockval;
+
+    var_Get( p_dec->p_libvlc, "avcodec", &lockval );
 
     /* Allocate the memory needed to store the decoder's structure */
     if( ( p_dec->p_sys = p_sys =
@@ -95,10 +99,10 @@ int E_(InitAudioDec)( decoder_t *p_dec, AVCodecContext *p_context,
         return VLC_EGENERIC;
     }
 
-    p_dec->p_sys->p_context = p_context;
-    p_dec->p_sys->p_codec = p_codec;
-    p_dec->p_sys->i_codec_id = i_codec_id;
-    p_dec->p_sys->psz_namecodec = psz_namecodec;
+    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;
 
     /* ***** Fill p_context with init values ***** */
     p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
@@ -112,40 +116,77 @@ int E_(InitAudioDec)( decoder_t *p_dec, AVCodecContext *p_context,
             malloc( p_dec->fmt_in.i_extra + FF_INPUT_BUFFER_PADDING_SIZE );
         memcpy( p_sys->p_context->extradata,
                 p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
-        memset( p_sys->p_context->extradata + p_dec->fmt_in.i_extra, 0,
+        memset( (char*)p_sys->p_context->extradata + p_dec->fmt_in.i_extra, 0,
                 FF_INPUT_BUFFER_PADDING_SIZE );
     }
 
     /* ***** Open the codec ***** */
+    vlc_mutex_lock( lockval.p_address );
     if (avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0)
     {
+        vlc_mutex_unlock( lockval.p_address );
         msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
+        free( p_sys );
         return VLC_EGENERIC;
     }
-    else
-    {
-        msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
-    }
+    vlc_mutex_unlock( lockval.p_address );
+
+    msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
 
     p_sys->p_output = malloc( 3 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
+    p_sys->p_samples = NULL;
+    p_sys->i_samples = 0;
 
-    aout_DateSet( &p_sys->end_date, 0 );
+    if( p_dec->fmt_in.audio.i_rate )
+    {
+        aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
+        aout_DateSet( &p_sys->end_date, 0 );
+    }
 
     /* Set output properties */
     p_dec->fmt_out.i_cat = AUDIO_ES;
     p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
+    p_dec->fmt_out.audio.i_bitspersample = 16;
 
     return VLC_SUCCESS;
 }
 
+/*****************************************************************************
+ * SplitBuffer: Needed because aout really doesn't like big audio chunk and
+ * wma produces easily > 30000 samples...
+ *****************************************************************************/
+aout_buffer_t *SplitBuffer( decoder_t *p_dec )
+{
+    decoder_sys_t *p_sys = p_dec->p_sys;
+    int i_samples = __MIN( p_sys->i_samples, 4096 );
+    aout_buffer_t *p_buffer;
+
+    if( i_samples == 0 ) return NULL;
+
+    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 );
+    p_buffer->end_date = aout_DateIncrement( &p_sys->end_date, i_samples );
+
+    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;
+}
+
 /*****************************************************************************
  * DecodeAudio: Called to decode one frame
  *****************************************************************************/
 aout_buffer_t *E_( DecodeAudio )( decoder_t *p_dec, block_t **pp_block )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
-    int i_used, i_output, i_samples;
-    uint8_t *p_samples;
+    int i_used, i_output;
     aout_buffer_t *p_buffer;
     block_t *p_block;
 
@@ -153,6 +194,14 @@ aout_buffer_t *E_( DecodeAudio )( decoder_t *p_dec, block_t **pp_block )
 
     p_block = *pp_block;
 
+    if( p_block->i_buffer <= 0 && p_sys->i_samples > 0 )
+    {
+        /* More data */
+        p_buffer = SplitBuffer( p_dec );
+        if( !p_buffer ) block_Release( p_block );
+        return p_buffer;
+    }
+
     if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
     {
         /* We've just started the stream, wait for the first PTS. */
@@ -160,7 +209,7 @@ aout_buffer_t *E_( DecodeAudio )( decoder_t *p_dec, block_t **pp_block )
         return NULL;
     }
 
-    if( p_block->i_buffer <= 0 )
+    if( p_block->i_buffer <= 0 || ( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) )
     {
         block_Release( p_block );
         return NULL;
@@ -170,7 +219,7 @@ aout_buffer_t *E_( DecodeAudio )( decoder_t *p_dec, block_t **pp_block )
                                    (int16_t*)p_sys->p_output, &i_output,
                                    p_block->p_buffer, p_block->i_buffer );
 
-    if( i_used < 0 )//|| i_output == 0 )
+    if( i_used < 0 || i_output < 0 )
     {
         if( i_used < 0 )
             msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
@@ -212,26 +261,15 @@ aout_buffer_t *E_( DecodeAudio )( decoder_t *p_dec, block_t **pp_block )
         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
     {
         aout_DateSet( &p_sys->end_date, p_block->i_pts );
-        p_block->i_pts = 0;
     }
+    p_block->i_pts = 0;
 
     /* **** Now we can output these samples **** */
-    i_samples = i_output / 2 / p_sys->p_context->channels;
-    p_samples = p_sys->p_output;
-
-    p_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples );
-    if( !p_buffer )
-    {
-        msg_Err( p_dec, "cannot get aout buffer" );
-        block_Release( p_block );
-        return NULL;
-    }
-
-    p_buffer->start_date = aout_DateGet( &p_sys->end_date );
-    p_buffer->end_date = aout_DateIncrement( &p_sys->end_date, i_samples );
-
-    memcpy( p_buffer->p_buffer, p_samples, p_buffer->i_nb_bytes );
+    p_sys->i_samples = i_output / 2 / p_sys->p_context->channels;
+    p_sys->p_samples = p_sys->p_output;
 
+    p_buffer = SplitBuffer( p_dec );
+    if( !p_buffer ) block_Release( p_block );
     return p_buffer;
 }