OSDN Git Service

Fix potential crash in libbluray
[handbrake-jp/handbrake-jp-git.git] / libhb / decavcodec.c
index 9d1474b..abf7ed1 100644 (file)
@@ -61,6 +61,7 @@
 
 #include "hb.h"
 #include "hbffmpeg.h"
+#include "downmix.h"
 #include "libavcodec/audioconvert.h"
 
 static int  decavcodecInit( hb_work_object_t *, hb_job_t * );
@@ -108,8 +109,14 @@ struct hb_work_private_s
     pts_heap_t      pts_heap;
     void*           buffer;
     struct SwsContext *sws_context; // if we have to rescale or convert color space
+    hb_downmix_t    *downmix;
+    hb_sample_t     *downmix_buffer;
 };
 
+static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *data, int size );
+static hb_buffer_t *link_buf_list( hb_work_private_t *pv );
+
+
 static int64_t heap_pop( pts_heap_t *heap )
 {
     int64_t result;
@@ -183,6 +190,7 @@ static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
     w->private_data = pv;
 
     pv->job   = job;
+    pv->list  = hb_list_init();
 
     int codec_id = w->codec_param;
     /*XXX*/
@@ -195,6 +203,15 @@ static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
     pv->context = avcodec_alloc_context();
     hb_avcodec_open( pv->context, codec );
 
+    if ( w->audio != NULL &&
+         hb_need_downmix( w->audio->config.in.channel_layout, 
+                          w->audio->config.out.mixdown) )
+    {
+        pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout, 
+                                      w->audio->config.out.mixdown);
+        hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, &hb_qt_chan_map );
+    }
+
     return 0;
 }
 
@@ -236,6 +253,15 @@ static void decavcodecClose( hb_work_object_t * w )
             av_free( pv->buffer );
             pv->buffer = NULL;
         }
+        if ( pv->downmix )
+        {
+            hb_downmix_close( &(pv->downmix) );
+        }
+        if ( pv->downmix_buffer )
+        {
+            free( pv->downmix_buffer );
+            pv->downmix_buffer = NULL;
+        }
         free( pv );
         w->private_data = NULL;
     }
@@ -250,17 +276,12 @@ static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
                     hb_buffer_t ** buf_out )
 {
     hb_work_private_t * pv = w->private_data;
-    hb_buffer_t * in = *buf_in, * buf, * last = NULL;
-    int   pos, len, out_size, i, uncompressed_len;
-    short* bufaligned;
-    uint64_t cur;
-    unsigned char *parser_output_buffer;
-    int parser_output_buffer_len;
+    hb_buffer_t * in = *buf_in;
 
-    if ( (*buf_in)->size <= 0 )
+    if ( in->size <= 0 )
     {
         /* EOF on input stream - send it downstream & say that we're done */
-        *buf_out = *buf_in;
+        *buf_out = in;
         *buf_in = NULL;
         return HB_WORK_DONE;
     }
@@ -273,75 +294,48 @@ static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         return HB_WORK_OK;
     }
 
-    cur = ( in->start < 0 )? pv->pts_next : in->start;
+    // if the packet has a timestamp use it 
+    if ( in->start != -1 )
+    {
+        pv->pts_next = in->start;
+    }
 
-    bufaligned = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
-    pos = 0;
-    while( pos < in->size )
+    int pos, len;
+    for ( pos = 0; pos < in->size; pos += len )
     {
-        len = av_parser_parse2( pv->parser, pv->context,
-                                &parser_output_buffer, &parser_output_buffer_len,
-                                in->data + pos, in->size - pos, cur, cur, AV_NOPTS_VALUE );
-        out_size = 0;
-        uncompressed_len = 0;
-        if (parser_output_buffer_len)
-        {
-            AVPacket avp;
-            av_init_packet( &avp );
-            avp.data = parser_output_buffer;
-            avp.size = parser_output_buffer_len;
+        uint8_t *parser_output_buffer;
+        int parser_output_buffer_len;
+        int64_t cur = pv->pts_next;
 
-            out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
-            uncompressed_len = avcodec_decode_audio3( pv->context, bufaligned, &out_size, &avp );
+        if ( pv->parser != NULL )
+        {
+            len = av_parser_parse2( pv->parser, pv->context,
+                    &parser_output_buffer, &parser_output_buffer_len,
+                    in->data + pos, in->size - pos, cur, cur, AV_NOPTS_VALUE );
         }
-        if( out_size )
+        else
         {
-            short * s16;
-            float * fl32;
-
-            buf = hb_buffer_init( 2 * out_size );
-
-            int sample_size_in_bytes = 2;   // Default to 2 bytes
-            switch (pv->context->sample_fmt)
-            {
-              case SAMPLE_FMT_S16:
-                sample_size_in_bytes = 2;
-                break;
-              /* We should handle other formats here - but that needs additional format conversion work below */
-              /* For now we'll just report the error and try to carry on */
-              default:
-                hb_log("decavcodecWork - Unknown Sample Format from avcodec_decode_audio (%d) !", pv->context->sample_fmt);
-                break;
-            }
-
-            buf->start = cur;
-            buf->stop  = cur + 90000 * ( out_size / (sample_size_in_bytes * pv->context->channels) ) /
-                         pv->context->sample_rate;
-            cur = buf->stop;
-
-            s16  = bufaligned;
-            fl32 = (float *) buf->data;
-            for( i = 0; i < out_size / 2; i++ )
-            {
-                fl32[i] = s16[i];
-            }
-
-            if( last )
-            {
-                last = last->next = buf;
-            }
-            else
+            parser_output_buffer = in->data;
+            len = parser_output_buffer_len = in->size;
+        }
+        if (parser_output_buffer_len)
+        {
+            // set the duration on every frame since the stream format can
+            // change (it shouldn't but there's no way to guarantee it).
+            // duration is a scaling factor to go from #bytes in the decoded
+            // frame to frame time (in 90KHz mpeg ticks). 'channels' converts
+            // total samples to per-channel samples. 'sample_rate' converts
+            // per-channel samples to seconds per sample and the 90000
+            // is mpeg ticks per second.
+            if ( pv->context->sample_rate && pv->context->channels )
             {
-                *buf_out = last = buf;
+                pv->duration = 90000. /
+                            (double)( pv->context->sample_rate * pv->context->channels );
             }
+            decodeAudio( w->audio, pv, parser_output_buffer, parser_output_buffer_len );
         }
-
-        pos += len;
     }
-
-    pv->pts_next = cur;
-
-    av_free( bufaligned );
+    *buf_out = link_buf_list( pv );
     return HB_WORK_OK;
 }
 
@@ -364,18 +358,6 @@ static int decavcodecInfo( hb_work_object_t *w, hb_work_info_t *info )
     return 0;
 }
 
-static const int chan2layout[] = {
-    HB_INPUT_CH_LAYOUT_MONO,  // We should allow no audio really.
-    HB_INPUT_CH_LAYOUT_MONO,   
-    HB_INPUT_CH_LAYOUT_STEREO,
-    HB_INPUT_CH_LAYOUT_2F1R,   
-    HB_INPUT_CH_LAYOUT_2F2R,
-    HB_INPUT_CH_LAYOUT_3F2R,   
-    HB_INPUT_CH_LAYOUT_4F2R,
-    HB_INPUT_CH_LAYOUT_STEREO, 
-    HB_INPUT_CH_LAYOUT_STEREO,
-};
-
 static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
                              hb_work_info_t *info )
 {
@@ -408,35 +390,56 @@ static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
     uint8_t *buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
     int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
     unsigned char *pbuffer;
-    int pos = 0, pbuffer_size;
+    int pos, pbuffer_size;
 
-    while ( pos < buf->size )
+    while ( buf && !ret )
     {
-        int len = av_parser_parse2( parser, context, &pbuffer, &pbuffer_size,
-                                    buf->data + pos, buf->size - pos,
-                                    buf->start, buf->start, AV_NOPTS_VALUE );
-        pos += len;
-        if ( pbuffer_size > 0 )
+        pos = 0;
+        while ( pos < buf->size )
         {
-            AVPacket avp;
-            av_init_packet( &avp );
-            avp.data = pbuffer;
-            avp.size = pbuffer_size;
+            int len;
 
-            len = avcodec_decode_audio3( context, (int16_t*)buffer, &out_size, &avp );
-            if ( len > 0 && context->sample_rate > 0 )
+            if (parser != NULL )
+            {
+                len = av_parser_parse2( parser, context, &pbuffer, 
+                                        &pbuffer_size, buf->data + pos, 
+                                        buf->size - pos, buf->start, 
+                                        buf->start, AV_NOPTS_VALUE );
+            }
+            else
+            {
+                pbuffer = buf->data;
+                len = pbuffer_size = buf->size;
+            }
+            pos += len;
+            if ( pbuffer_size > 0 )
             {
-                info->bitrate = context->bit_rate;
-                info->rate = context->sample_rate;
-                info->rate_base = 1;
-                info->channel_layout = chan2layout[context->channels & 7];
-                ret = 1;
-                break;
+                AVPacket avp;
+                av_init_packet( &avp );
+                avp.data = pbuffer;
+                avp.size = pbuffer_size;
+
+                len = avcodec_decode_audio3( context, (int16_t*)buffer, 
+                                             &out_size, &avp );
+                if ( len > 0 && context->sample_rate > 0 )
+                {
+                    info->bitrate = context->bit_rate;
+                    info->rate = context->sample_rate;
+                    info->rate_base = 1;
+                    info->channel_layout = 
+                        hb_ff_layout_xlat(context->channel_layout, 
+                                          context->channels);
+                    ret = 1;
+                    break;
+                }
             }
         }
+        buf = buf->next;
     }
+
     av_free( buffer );
-    av_parser_close( parser );
+    if ( parser != NULL )
+        av_parser_close( parser );
     hb_avcodec_close( context );
     return ret;
 }
@@ -520,9 +523,22 @@ static int get_frame_buf( AVCodecContext *context, AVFrame *frame )
     return avcodec_default_get_buffer( context, frame );
 }
 
+static int reget_frame_buf( AVCodecContext *context, AVFrame *frame )
+{
+    hb_work_private_t *pv = context->opaque;
+    frame->pts = pv->pts;
+    pv->pts = -1;
+    return avcodec_default_reget_buffer( context, frame );
+}
+
 static void log_chapter( hb_work_private_t *pv, int chap_num, int64_t pts )
 {
-    hb_chapter_t *c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
+    hb_chapter_t *c;
+
+    if ( !pv->job )
+        return;
+
+    c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
     if ( c && c->title )
     {
         hb_log( "%s: \"%s\" (%d) at frame %u time %"PRId64,
@@ -550,7 +566,20 @@ static void flushDelayQueue( hb_work_private_t *pv )
     }
 }
 
-static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
+/*
+ * Decodes a video frame from the specified raw packet data ('data', 'size', 'sequence').
+ * The output of this function is stored in 'pv->list', which contains a list
+ * of zero or more decoded packets.
+ * 
+ * The returned packets are guaranteed to have their timestamps in the correct order,
+ * even if the original packets decoded by libavcodec have misordered timestamps,
+ * due to the use of 'packed B-frames'.
+ * 
+ * Internally the set of decoded packets may be buffered in 'pv->delayq'
+ * until enough packets have been decoded so that the timestamps can be
+ * correctly rewritten, if this is necessary.
+ */
+static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size, int sequence )
 {
     int got_picture, oldlevel = 0;
     AVFrame frame;
@@ -623,6 +652,18 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
         {
             buf = copy_frame( pv, &frame );
             buf->start = pts;
+            buf->sequence = sequence;
+            if ( pv->new_chap && buf->start >= pv->chap_time )
+            {
+                buf->new_chap = pv->new_chap;
+                pv->new_chap = 0;
+                pv->chap_time = 0;
+                log_chapter( pv, buf->new_chap, buf->start );
+            }
+            else if ( pv->nframes == 0 && pv->job )
+            {
+                log_chapter( pv, pv->job->chapter_start, buf->start );
+            }
             hb_list_add( pv->list, buf );
             ++pv->nframes;
             return got_picture;
@@ -662,7 +703,7 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
                 pv->chap_time = 0;
                 log_chapter( pv, buf->new_chap, buf->start );
             }
-            else if ( pv->nframes == 0 )
+            else if ( pv->nframes == 0 && pv->job )
             {
                 log_chapter( pv, pv->job->chapter_start, buf->start );
             }
@@ -670,7 +711,9 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
         }
 
         // add the new frame to the delayq & push its timestamp on the heap
-        pv->delayq[slot] = copy_frame( pv, &frame );
+        buf = copy_frame( pv, &frame );
+        buf->sequence = sequence;
+        pv->delayq[slot] = buf;
         heap_push( &pv->pts_heap, pts );
 
         ++pv->nframes;
@@ -679,7 +722,7 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
     return got_picture;
 }
 
-static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size,
+static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size, int sequence,
                          int64_t pts, int64_t dts )
 {
     /*
@@ -699,20 +742,24 @@ static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size,
         if ( pout_len > 0 )
         {
             pv->pts = pv->parser->pts;
-            decodeFrame( pv, pout, pout_len );
+            decodeFrame( pv, pout, pout_len, sequence );
         }
     } while ( pos < size );
 
     /* the stuff above flushed the parser, now flush the decoder */
     if ( size <= 0 )
     {
-        while ( decodeFrame( pv, NULL, 0 ) )
+        while ( decodeFrame( pv, NULL, 0, sequence ) )
         {
         }
         flushDelayQueue( pv );
     }
 }
 
+/*
+ * Removes all packets from 'pv->list', links them together into
+ * a linked-list, and returns the first packet in the list.
+ */
 static hb_buffer_t *link_buf_list( hb_work_private_t *pv )
 {
     hb_buffer_t *head = hb_list_item( pv->list, 0 );
@@ -749,6 +796,7 @@ static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
     /* we have to wrap ffmpeg's get_buffer to be able to set the pts (?!) */
     pv->context->opaque = pv;
     pv->context->get_buffer = get_frame_buf;
+    pv->context->reget_buffer = reget_frame_buf;
 
     return 0;
 }
@@ -851,7 +899,7 @@ static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     /* if we got an empty buffer signaling end-of-stream send it downstream */
     if ( in->size == 0 )
     {
-        decodeVideo( pv, in->data, in->size, pts, dts );
+        decodeVideo( pv, in->data, in->size, in->sequence, pts, dts );
         hb_list_add( pv->list, in );
         *buf_out = link_buf_list( pv );
         return HB_WORK_DONE;
@@ -888,7 +936,7 @@ static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         pv->new_chap = in->new_chap;
         pv->chap_time = pts >= 0? pts : pv->pts_next;
     }
-    decodeVideo( pv, in->data, in->size, pts, dts );
+    decodeVideo( pv, in->data, in->size, in->sequence, pts, dts );
     hb_buffer_close( &in );
     *buf_out = link_buf_list( pv );
     return HB_WORK_OK;
@@ -929,10 +977,11 @@ static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
         if ( info->pixel_aspect_width == 0 ||
              info->pixel_aspect_height == 0 )
         {
+            // There will not be an ffmpeg stream if the file is TS
             AVStream *st = hb_ffmpeg_avstream( w->codec_param );
-            info->pixel_aspect_width = st->sample_aspect_ratio.num ?
-                                        st->sample_aspect_ratio.num : 1;
-            info->pixel_aspect_height = st->sample_aspect_ratio.den ?
+            info->pixel_aspect_width = st && st->sample_aspect_ratio.num ?
+                                       st->sample_aspect_ratio.num : 1;
+            info->pixel_aspect_height = st && st->sample_aspect_ratio.den ?
                                         st->sample_aspect_ratio.den : 1;
         }
         /* ffmpeg returns the Pixel Aspect Ratio (PAR). Handbrake wants the
@@ -1013,8 +1062,14 @@ static void init_ffmpeg_context( hb_work_object_t *w )
         // Because the time bases are so screwed up, we only take values
         // in the range 8fps - 64fps.
         AVRational tb;
-        if ( st->time_base.num * 64 > st->time_base.den &&
-             st->time_base.den > st->time_base.num * 8 )
+        if ( st->avg_frame_rate.den * 64 > st->avg_frame_rate.num &&
+             st->avg_frame_rate.num > st->avg_frame_rate.den * 8 )
+        {
+            tb.num = st->avg_frame_rate.den;
+            tb.den = st->avg_frame_rate.num;
+        }
+        else if ( st->time_base.num * 64 > st->time_base.den &&
+                  st->time_base.den > st->time_base.num * 8 )
         {
             tb = st->time_base;
         }
@@ -1036,6 +1091,7 @@ static void init_ffmpeg_context( hb_work_object_t *w )
     // we have to wrap ffmpeg's get_buffer to be able to set the pts (?!)
     pv->context->opaque = pv;
     pv->context->get_buffer = get_frame_buf;
+    pv->context->reget_buffer = reget_frame_buf;
 
     // avi, mkv and possibly mp4 containers can contain the M$ VFW packed
     // b-frames abortion that messes up frame ordering and timestamps.
@@ -1072,6 +1128,16 @@ static int decavcodecviInit( hb_work_object_t * w, hb_job_t * job )
     pv->list = hb_list_init();
     pv->pts_next = -1;
     pv->pts = -1;
+
+    if ( w->audio != NULL &&
+         hb_need_downmix( w->audio->config.in.channel_layout, 
+                          w->audio->config.out.mixdown) )
+    {
+        pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout, 
+                                      w->audio->config.out.mixdown);
+        hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, &hb_qt_chan_map );
+    }
+
     return 0;
 }
 
@@ -1079,10 +1145,6 @@ static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
                              hb_buffer_t ** buf_out )
 {
     hb_work_private_t *pv = w->private_data;
-    if ( ! pv->context )
-    {
-        init_ffmpeg_context( w );
-    }
     hb_buffer_t *in = *buf_in;
     *buf_in = NULL;
 
@@ -1090,7 +1152,7 @@ static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     if ( in->size == 0 )
     {
         /* flush any frames left in the decoder */
-        while ( decodeFrame( pv, NULL, 0 ) )
+        while ( pv->context && decodeFrame( pv, NULL, 0, in->sequence ) )
         {
         }
         flushDelayQueue( pv );
@@ -1099,6 +1161,11 @@ static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         return HB_WORK_DONE;
     }
 
+    if ( ! pv->context )
+    {
+        init_ffmpeg_context( w );
+    }
+
     int64_t pts = in->start;
     if( pts >= 0 )
     {
@@ -1116,7 +1183,7 @@ static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         pv->chap_time = pts >= 0? pts : pv->pts_next;
     }
     prepare_ffmpeg_buffer( in );
-    decodeFrame( pv, in->data, in->size );
+    decodeFrame( pv, in->data, in->size, in->sequence );
     hb_buffer_close( &in );
     *buf_out = link_buf_list( pv );
     return HB_WORK_OK;
@@ -1140,10 +1207,11 @@ static int decavcodecviInfo( hb_work_object_t *w, hb_work_info_t *info )
     return 0;
 }
 
-static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
+static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *data, int size )
 {
     AVCodecContext *context = pv->context;
     int pos = 0;
+    int loop_limit = 256;
 
     while ( pos < size )
     {
@@ -1160,11 +1228,20 @@ static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
         avp.size = size - pos;
 
         int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
+        int nsamples;
         int len = avcodec_decode_audio3( context, buffer, &out_size, &avp );
-        if ( len <= 0 )
+        if ( len < 0 )
         {
             return;
         }
+        if ( len == 0 )
+        {
+            if ( !(loop_limit--) )
+                return;
+        }
+        else
+            loop_limit = 256;
+
         pos += len;
         if( out_size > 0 )
         {
@@ -1184,8 +1261,8 @@ static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
                                                               context->sample_fmt, 1,
                                                               NULL, 0 );
                 // get output buffer size (in 2-byte samples) then malloc a buffer
-                out_size = ( out_size * 2 ) / isamp;
-                buffer = av_malloc( out_size );
+                nsamples = out_size / isamp;
+                buffer = av_malloc( nsamples * 2 );
 
                 // we're doing straight sample format conversion which behaves as if
                 // there were only one channel.
@@ -1194,26 +1271,54 @@ static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
                 const int istride[6] = { isamp };
                 const int ostride[6] = { 2 };
 
-                av_audio_convert( ctx, obuf, ostride, ibuf, istride, out_size >> 1 );
+                av_audio_convert( ctx, obuf, ostride, ibuf, istride, nsamples );
                 av_audio_convert_free( ctx );
             }
-            hb_buffer_t *buf = hb_buffer_init( 2 * out_size );
+            else
+            {
+                nsamples = out_size / 2;
+            }
+
+            hb_buffer_t * buf;
 
-            // convert from bytes to total samples
-            out_size >>= 1;
+            if ( pv->downmix )
+            {
+                pv->downmix_buffer = realloc(pv->downmix_buffer, nsamples * sizeof(hb_sample_t));
+                
+                int i;
+                for( i = 0; i < nsamples; ++i )
+                {
+                    pv->downmix_buffer[i] = buffer[i];
+                }
+
+                int n_ch_samples = nsamples / context->channels;
+                int channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
+
+                buf = hb_buffer_init( n_ch_samples * channels * sizeof(float) );
+                hb_sample_t *samples = (hb_sample_t *)buf->data;
+                hb_downmix(pv->downmix, samples, pv->downmix_buffer, n_ch_samples);
+            }
+            else
+            {
+                buf = hb_buffer_init( nsamples * sizeof(float) );
+                float *fl32 = (float *)buf->data;
+                int i;
+                for( i = 0; i < nsamples; ++i )
+                {
+                    fl32[i] = buffer[i];
+                }
+                int n_ch_samples = nsamples / context->channels;
+                hb_layout_remap( &hb_smpte_chan_map, &hb_qt_chan_map,
+                                 audio->config.in.channel_layout, 
+                                 fl32, n_ch_samples );
+            }
 
             double pts = pv->pts_next;
             buf->start = pts;
-            pts += out_size * pv->duration;
+            pts += nsamples * pv->duration;
             buf->stop  = pts;
             pv->pts_next = pts;
 
-            float *fl32 = (float *)buf->data;
-            int i;
-            for( i = 0; i < out_size; ++i )
-            {
-                fl32[i] = buffer[i];
-            }
             hb_list_add( pv->list, buf );
 
             // if we allocated a buffer for sample format conversion, free it
@@ -1266,7 +1371,7 @@ static int decavcodecaiWork( hb_work_object_t *w, hb_buffer_t **buf_in,
         pv->pts_next = in->start;
     }
     prepare_ffmpeg_buffer( in );
-    decodeAudio( pv, in->data, in->size );
+    decodeAudio( w->audio, pv, in->data, in->size );
     *buf_out = link_buf_list( pv );
 
     return HB_WORK_OK;