]> git.sesse.net Git - vlc/blobdiff - modules/codec/adpcm.c
Removes trailing spaces. Removes tabs.
[vlc] / modules / codec / adpcm.c
index 61e79df891cddd8f8fced342ab9e4efaf125ca85..830093fbd0c2d47b860027b15b76e75ff20ba4cc 100644 (file)
@@ -1,16 +1,17 @@
 /*****************************************************************************
  * adpcm.c : adpcm variant audio decoder
  *****************************************************************************
- * Copyright (C) 2001, 2002 VideoLAN
- * $Id: adpcm.c,v 1.2 2002/12/18 14:17:10 sam Exp $
+ * Copyright (C) 2001, 2002 the VideoLAN team
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
- *      
+ *          RĂ©mi Denis-Courmont <rem # 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
  * 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
@@ -18,7 +19,7 @@
  *
  * 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.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Documentation: http://www.pcisys.net/~melanson/codecs/adpcm.txt
  *****************************************************************************/
 #include <vlc/vlc.h>
-#include <vlc/aout.h>
-#include <vlc/decoder.h>
-#include <vlc/input.h>
+#include <vlc_aout.h>
+#include <vlc_codec.h>
 
-#include <stdlib.h>                                      /* malloc(), free() */
-#include <string.h>                                              /* strdup() */
-#include "codecs.h"
 /*****************************************************************************
- * Local prototypes
+ * Module descriptor
  *****************************************************************************/
+static int  OpenDecoder( vlc_object_t * );
+static void CloseDecoder( vlc_object_t * );
 
-#define ADPCM_IMA_QT    1
-#define ADPCM_IMA_WAV   2
-#define ADPCM_MS        3
+static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
 
-typedef struct adec_thread_s
+vlc_module_begin();
+    set_description( _("ADPCM audio decoder") );
+    set_capability( "decoder", 50 );
+    set_category( CAT_INPUT );
+    set_subcategory( SUBCAT_INPUT_ACODEC );
+    set_callbacks( OpenDecoder, CloseDecoder );
+vlc_module_end();
+
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+enum adpcm_codec_e
 {
-    int i_codec;
+    ADPCM_IMA_QT,
+    ADPCM_IMA_WAV,
+    ADPCM_MS,
+    ADPCM_DK3,
+    ADPCM_DK4,
+    ADPCM_EA
+};
+
+struct decoder_sys_t
+{
+    enum adpcm_codec_e codec;
 
-    WAVEFORMATEX    *p_wf;
-    
-    /* The bit stream structure handles the PES stream at the bit level */
-    bit_stream_t        bit_stream;
     int                 i_block;
-    uint8_t             *p_block;
     int                 i_samplesperblock;
-    
-    /* Input properties */
-    decoder_fifo_t *p_fifo;
-    
-    /* Output properties */
-    aout_instance_t *   p_aout;       /* opaque */
-    aout_input_t *      p_aout_input; /* opaque */
-    audio_sample_format_t output_format;
-
-    audio_date_t        date;
-    mtime_t             pts;
-
-} adec_thread_t;
-
-static int  OpenDecoder    ( vlc_object_t * );
-
-static int  RunDecoder     ( decoder_fifo_t * );
-static int  InitThread     ( adec_thread_t * );
-static void DecodeThread   ( adec_thread_t * );
-static void EndThread      ( adec_thread_t * );
-
 
-static void DecodeAdpcmMs( adec_thread_t *, aout_buffer_t * );
-static void DecodeAdpcmImaWav( adec_thread_t *, aout_buffer_t * );
-
-/*****************************************************************************
- * Module descriptor
- *****************************************************************************/
-
-vlc_module_begin();
-    set_description( _("ADPCM audio deocder") );
-    set_capability( "decoder", 50 );
-    set_callbacks( OpenDecoder, NULL );
-vlc_module_end();
+    audio_date_t        end_date;
+};
 
+static void DecodeAdpcmMs    ( decoder_t *, int16_t *, uint8_t * );
+static void DecodeAdpcmImaWav( decoder_t *, int16_t *, uint8_t * );
+static void DecodeAdpcmImaQT ( decoder_t *, int16_t *, uint8_t * );
+static void DecodeAdpcmDk4   ( decoder_t *, int16_t *, uint8_t * );
+static void DecodeAdpcmDk3   ( decoder_t *, int16_t *, uint8_t * );
+static void DecodeAdpcmEA    ( decoder_t *, int16_t *, uint8_t * );
 
 static int pi_channels_maps[6] =
 {
@@ -97,7 +85,7 @@ static int pi_channels_maps[6] =
     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
-     | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT 
+     | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
 };
 
 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
@@ -136,272 +124,237 @@ static int i_adaptation_coeff2[7] =
     0, -256, 0, 64, 0, -208, -232
 };
 
-
 /*****************************************************************************
  * OpenDecoder: probe the decoder and return score
- *****************************************************************************
- * Tries to launch a decoder and return score so that the interface is able
- * to choose.
  *****************************************************************************/
 static int OpenDecoder( vlc_object_t *p_this )
 {
-    decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
-    
-    switch( p_fifo->i_fourcc )
-    {   
-//        case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
+    decoder_t *p_dec = (decoder_t*)p_this;
+    decoder_sys_t *p_sys;
+
+    switch( p_dec->fmt_in.i_codec )
+    {
+        case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
         case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
         case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
-//        case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
-//        case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
-
-            p_fifo->pf_run = RunDecoder;
-            return VLC_SUCCESS;
-            
+        case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
+        case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
+        case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
+            break;
         default:
             return VLC_EGENERIC;
     }
 
-}
-
-/*****************************************************************************
- * RunDecoder: this function is called just after the thread is created
- *****************************************************************************/
-static int RunDecoder( decoder_fifo_t *p_fifo )
-{
-    adec_thread_t *p_adec;
-    int b_error;
-
-    if( !( p_adec = malloc( sizeof( adec_thread_t ) ) ) )
+    if( p_dec->fmt_in.audio.i_channels <= 0 ||
+        p_dec->fmt_in.audio.i_channels > 5 )
     {
-        msg_Err( p_fifo, "out of memory" );
-        DecoderError( p_fifo );
-        return( -1 );
+        msg_Err( p_dec, "invalid number of channel (not between 1 and 5): %i",
+                 p_dec->fmt_in.audio.i_channels );
+        return VLC_EGENERIC;
     }
-    memset( p_adec, 0, sizeof( adec_thread_t ) );
-    
-    p_adec->p_fifo = p_fifo;
 
-    if( InitThread( p_adec ) != 0 )
+    if( p_dec->fmt_in.audio.i_rate <= 0 )
     {
-        DecoderError( p_fifo );
-        return( -1 );
+        msg_Err( p_dec, "bad samplerate" );
+        return VLC_EGENERIC;
     }
 
-    while( ( !p_adec->p_fifo->b_die )&&( !p_adec->p_fifo->b_error ) )
+    /* 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 )
     {
-        DecodeThread( p_adec );
+        msg_Err( p_dec, "out of memory" );
+        return VLC_ENOMEM;
     }
 
-
-    if( ( b_error = p_adec->p_fifo->b_error ) )
-    {
-        DecoderError( p_adec->p_fifo );
-    }
-
-    EndThread( p_adec );
-    if( b_error )
-    {
-        return( -1 );
-    }
-
-    return( 0 );
-}
-
-
-#define FREE( p ) if( p ) free( p ); p = NULL
-#define GetWLE( p ) \
-    ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
-
-#define GetDWLE( p ) \
-    (  *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
-        ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
-
-/*****************************************************************************
- * InitThread: initialize data before entering main loop
- *****************************************************************************/
-
-static int InitThread( adec_thread_t * p_adec )
-{
-    if( !( p_adec->p_wf = (WAVEFORMATEX*)p_adec->p_fifo->p_demux_data ) )
-    {
-        msg_Err( p_adec->p_fifo, "missing format" );
-        return( -1 );
-    }
-    /* fourcc to codec */
-    switch( p_adec->p_fifo->i_fourcc )
+    switch( p_dec->fmt_in.i_codec )
     {
         case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
-            p_adec->i_codec = ADPCM_IMA_QT;
+            p_sys->codec = ADPCM_IMA_QT;
             break;
         case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
-            p_adec->i_codec = ADPCM_IMA_WAV;
+            p_sys->codec = ADPCM_IMA_WAV;
             break;
         case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
-            p_adec->i_codec = ADPCM_MS;
+            p_sys->codec = ADPCM_MS;
             break;
         case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
+            p_sys->codec = ADPCM_DK4;
+            break;
         case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
-            p_adec->i_codec = 0;
+            p_sys->codec = ADPCM_DK3;
+            break;
+        case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
+            p_sys->codec = ADPCM_EA;
+            p_dec->fmt_in.p_extra = calloc( 2 * p_dec->fmt_in.audio.i_channels,
+                                            sizeof( int16_t ) );
+            if( p_dec->fmt_in.p_extra == NULL )
+            {
+                free( p_sys );
+                return VLC_ENOMEM;
+            }
             break;
     }
 
-    if( p_adec->p_wf->nChannels < 1 || 
-            p_adec->p_wf->nChannels > 2 )
+    if( p_dec->fmt_in.audio.i_blockalign <= 0 )
     {
-        msg_Err( p_adec->p_fifo, "bad channels count(1-2)" );
-        return( -1 );
+        p_sys->i_block = (p_sys->codec == ADPCM_IMA_QT) ?
+            34 * p_dec->fmt_in.audio.i_channels : 1024;
+        msg_Warn( p_dec, "block size undefined, using %d", p_sys->i_block );
     }
-    if( !( p_adec->i_block = p_adec->p_wf->nBlockAlign ) )
+    else
     {
-        if( p_adec->i_codec == ADPCM_IMA_QT )
-        {
-            p_adec->i_block = 34;
-        }
-        else
-        {
-            p_adec->i_block = 1024; // XXX FIXME
-        }
-        msg_Err( p_adec->p_fifo,
-                 "block size undefined, using %d default", 
-                 p_adec->i_block );
+        p_sys->i_block = p_dec->fmt_in.audio.i_blockalign;
     }
-    p_adec->p_block = malloc( p_adec->i_block );
 
     /* calculate samples per block */
-    switch( p_adec->i_codec )
-    {
-        case ADPCM_IMA_QT:
-            p_adec->i_samplesperblock = 64;
-            break;
-        case ADPCM_IMA_WAV:
-            p_adec->i_samplesperblock = 
-                 2 * ( p_adec->i_block - 4 * p_adec->p_wf->nChannels )/
-                 p_adec->p_wf->nChannels;
-                 break;
-        case ADPCM_MS:
-            p_adec->i_samplesperblock = 
-                2 * ( p_adec->i_block - 7 * p_adec->p_wf->nChannels ) / 
-                p_adec->p_wf->nChannels + 2;
-            break;
-        default:
-            p_adec->i_samplesperblock = 0;
-    }
-   
-    msg_Dbg( p_adec->p_fifo,
-             "format: samplerate:%dHz channels:%d bits/sample:%d blockalign:%d samplesperblock %d",
-             p_adec->p_wf->nSamplesPerSec,
-             p_adec->p_wf->nChannels,
-             p_adec->p_wf->wBitsPerSample, 
-             p_adec->p_wf->nBlockAlign,
-             p_adec->i_samplesperblock );
-    
-    //p_adec->output_format.i_format = VLC_FOURCC('s','1','6','l');
-    /* FIXME good way ? */
-    p_adec->output_format.i_format = AOUT_FMT_S16_NE;
-    p_adec->output_format.i_rate = p_adec->p_wf->nSamplesPerSec;
-
-
-    p_adec->output_format.i_physical_channels = 
-        p_adec->output_format.i_original_channels =
-            pi_channels_maps[p_adec->p_wf->nChannels];
-    p_adec->p_aout = NULL;
-    p_adec->p_aout_input = NULL;
-
-    /* **** Create a new audio output **** */
-    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 )
+    switch( p_sys->codec )
     {
-        msg_Err( p_adec->p_fifo, "cannot create aout" );
-        return( -1 );
+    case ADPCM_IMA_QT:
+        p_sys->i_samplesperblock = 64;
+        break;
+    case ADPCM_IMA_WAV:
+        p_sys->i_samplesperblock =
+            2 * ( p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels ) /
+            p_dec->fmt_in.audio.i_channels;
+        break;
+    case ADPCM_MS:
+        p_sys->i_samplesperblock =
+            2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels) /
+            p_dec->fmt_in.audio.i_channels + 2;
+        break;
+    case ADPCM_DK4:
+        p_sys->i_samplesperblock =
+            2 * (p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels) /
+            p_dec->fmt_in.audio.i_channels + 1;
+        break;
+    case ADPCM_DK3:
+        p_dec->fmt_in.audio.i_channels = 2;
+        p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
+        break;
+    case ADPCM_EA:
+        p_sys->i_samplesperblock =
+            2 * (p_sys->i_block - p_dec->fmt_in.audio.i_channels) /
+            p_dec->fmt_in.audio.i_channels;
     }
 
-    /* Init the BitStream */
-    InitBitstream( &p_adec->bit_stream, p_adec->p_fifo,
-                   NULL, NULL );
+    msg_Dbg( p_dec, "format: samplerate:%d Hz channels:%d bits/sample:%d "
+             "blockalign:%d samplesperblock:%d",
+             p_dec->fmt_in.audio.i_rate, p_dec->fmt_in.audio.i_channels,
+             p_dec->fmt_in.audio.i_bitspersample, p_sys->i_block,
+             p_sys->i_samplesperblock );
 
-    return( 0 );
-}
+    p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
+    p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
+    p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
+    p_dec->fmt_out.audio.i_physical_channels =
+        p_dec->fmt_out.audio.i_original_channels =
+            pi_channels_maps[p_dec->fmt_in.audio.i_channels];
 
+    aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
+    aout_DateSet( &p_sys->end_date, 0 );
+
+    p_dec->pf_decode_audio = DecodeBlock;
+
+    return VLC_SUCCESS;
+}
 
 /*****************************************************************************
- * DecodeThread: decodes a frame
+ * DecodeBlock:
  *****************************************************************************/
-static void DecodeThread( adec_thread_t *p_adec )
+static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
 {
-    aout_buffer_t   *p_aout_buffer;
+    decoder_sys_t *p_sys  = p_dec->p_sys;
+    block_t *p_block;
 
-    /* get pts */
-    CurrentPTS( &p_adec->bit_stream, &p_adec->pts, NULL );
-    /* gather block */
-    GetChunk( &p_adec->bit_stream,
-              p_adec->p_block,
-              p_adec->i_block );
+    if( !pp_block || !*pp_block ) return NULL;
 
-    /* get output buffer */
-    if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
+    p_block = *pp_block;
+
+    if( p_block->i_pts != 0 &&
+        p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
     {
-        aout_DateSet( &p_adec->date, p_adec->pts );
+        aout_DateSet( &p_sys->end_date, p_block->i_pts );
     }
-    else if( !aout_DateGet( &p_adec->date ) )
+    else if( !aout_DateGet( &p_sys->end_date ) )
     {
-        return;
+        /* We've just started the stream, wait for the first PTS. */
+        block_Release( p_block );
+        return NULL;
     }
 
-    p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout, 
-                                       p_adec->p_aout_input,
-                                       p_adec->i_samplesperblock );
-    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,
-                                                  p_adec->i_samplesperblock );
-
-    /* decode */
-    
-    switch( p_adec->i_codec )
+    /* Don't re-use the same pts twice */
+    p_block->i_pts = 0;
+
+    if( p_block->i_buffer >= p_sys->i_block )
     {
+        aout_buffer_t *p_out;
+
+        p_out = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_samplesperblock );
+        if( p_out == NULL )
+        {
+            block_Release( p_block );
+            return NULL;
+        }
+
+        p_out->start_date = aout_DateGet( &p_sys->end_date );
+        p_out->end_date =
+            aout_DateIncrement( &p_sys->end_date, p_sys->i_samplesperblock );
+
+        switch( p_sys->codec )
+        {
         case ADPCM_IMA_QT:
+            DecodeAdpcmImaQT( p_dec, (int16_t*)p_out->p_buffer,
+                              p_block->p_buffer );
             break;
         case ADPCM_IMA_WAV:
-            DecodeAdpcmImaWav( p_adec, p_aout_buffer );
+            DecodeAdpcmImaWav( p_dec, (int16_t*)p_out->p_buffer,
+                               p_block->p_buffer );
             break;
         case ADPCM_MS:
-            DecodeAdpcmMs( p_adec, p_aout_buffer );
+            DecodeAdpcmMs( p_dec, (int16_t*)p_out->p_buffer,
+                           p_block->p_buffer );
+            break;
+        case ADPCM_DK4:
+            DecodeAdpcmDk4( p_dec, (int16_t*)p_out->p_buffer,
+                            p_block->p_buffer );
+            break;
+        case ADPCM_DK3:
+            DecodeAdpcmDk3( p_dec, (int16_t*)p_out->p_buffer,
+                            p_block->p_buffer );
             break;
+        case ADPCM_EA:
+            DecodeAdpcmEA( p_dec, (int16_t*)p_out->p_buffer,
+                           p_block->p_buffer );
         default:
             break;
-    }
+        }
 
+        p_block->p_buffer += p_sys->i_block;
+        p_block->i_buffer -= p_sys->i_block;
+        return p_out;
+    }
 
-    /* **** Now we can output these samples **** */
-    aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
+    block_Release( p_block );
+    return NULL;
 }
 
-
 /*****************************************************************************
- * EndThread : faad decoder thread destruction
+ * CloseDecoder:
  *****************************************************************************/
-static void EndThread (adec_thread_t *p_adec)
+static void CloseDecoder( vlc_object_t *p_this )
 {
-    if( p_adec->p_aout_input )
-    {
-        aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
-    }
+    decoder_t *p_dec = (decoder_t *)p_this;
+    decoder_sys_t *p_sys = p_dec->p_sys;
 
-    msg_Dbg( p_adec->p_fifo, "adpcm audio decoder closed" );
-        
-    free( p_adec->p_block );
-    free( p_adec );
+    if( p_sys->codec == ADPCM_EA )
+        free( p_dec->fmt_in.p_extra );
+    free( p_sys );
 }
+
+/*****************************************************************************
+ * Local functions
+ *****************************************************************************/
 #define CLAMP( v, min, max ) \
     if( (v) < (min) ) (v) = (min); \
     if( (v) > (max) ) (v) = (max)
@@ -414,9 +367,12 @@ static void EndThread (adec_thread_t *p_adec)
     (v) |= ( *p_buffer ) << 8; p_buffer++; \
     if( (v)&0x8000 ) (v) -= 0x010000;
 
+/*
+ * MS
+ */
 typedef struct adpcm_ms_channel_s
 {
-    int i_idelta; 
+    int i_idelta;
     int i_sample1, i_sample2;
     int i_coeff1, i_coeff2;
 
@@ -431,8 +387,8 @@ static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
     /* expand sign */
 
     i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
-    
-    i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 + 
+
+    i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
                     p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
                   i_snibble * p_channel->i_idelta;
 
@@ -441,7 +397,7 @@ static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
     p_channel->i_sample2 = p_channel->i_sample1;
     p_channel->i_sample1 = i_predictor;
 
-    p_channel->i_idelta = ( i_adaptation_table[i_nibble] * 
+    p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
                             p_channel->i_idelta ) / 256;
     if( p_channel->i_idelta < 16 )
     {
@@ -449,19 +405,17 @@ static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
     }
     return( i_predictor );
 }
-    
-static void DecodeAdpcmMs( adec_thread_t *p_adec,  
-                           aout_buffer_t *p_aout_buffer)
+
+static void DecodeAdpcmMs( decoder_t *p_dec, int16_t *p_sample,
+                           uint8_t *p_buffer )
 {
-    uint8_t            *p_buffer;
+    decoder_sys_t *p_sys  = p_dec->p_sys;
     adpcm_ms_channel_t channel[2];
     int i_nibbles;
-    uint16_t           *p_sample;
     int b_stereo;
     int i_block_predictor;
-    
-    p_buffer = p_adec->p_block;
-    b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
+
+    b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
 
     GetByte( i_block_predictor );
     CLAMP( i_block_predictor, 0, 6 );
@@ -480,7 +434,7 @@ static void DecodeAdpcmMs( adec_thread_t *p_adec,
     {
         GetWord( channel[1].i_idelta );
     }
-    
+
     GetWord( channel[0].i_sample1 );
     if( b_stereo )
     {
@@ -493,35 +447,31 @@ static void DecodeAdpcmMs( adec_thread_t *p_adec,
         GetWord( channel[1].i_sample2 );
     }
 
-    p_sample = (int16_t*)p_aout_buffer->p_buffer;
-
     if( b_stereo )
     {
-        *p_sample = channel[0].i_sample2; p_sample++;
-        *p_sample = channel[1].i_sample2; p_sample++;
-        *p_sample = channel[0].i_sample1; p_sample++;
-        *p_sample = channel[1].i_sample1; p_sample++;
+        *p_sample++ = channel[0].i_sample2;
+        *p_sample++ = channel[1].i_sample2;
+        *p_sample++ = channel[0].i_sample1;
+        *p_sample++ = channel[1].i_sample1;
     }
     else
     {
-        *p_sample = channel[0].i_sample2; p_sample++;
-        *p_sample = channel[0].i_sample1; p_sample++;
+        *p_sample++ = channel[0].i_sample2;
+        *p_sample++ = channel[0].i_sample1;
     }
 
-    for( i_nibbles =  2 *( p_adec->i_block - 7 * p_adec->p_wf->nChannels );
-         i_nibbles > 0; i_nibbles -= 2,p_buffer++ )
+    for( i_nibbles = 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels);
+         i_nibbles > 0; i_nibbles -= 2, p_buffer++ )
     {
-        *p_sample = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
-        p_sample++;
-        
-        *p_sample = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0], 
-                                         (*p_buffer)&0x0f);
-        p_sample++;
+        *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
+        *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
+                                           (*p_buffer)&0x0f);
     }
-
-    
 }
 
+/*
+ * IMA-WAV
+ */
 typedef struct adpcm_ima_wav_channel_s
 {
     int i_predictor;
@@ -552,17 +502,15 @@ static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
     return( p_channel->i_predictor );
 }
 
-static void DecodeAdpcmImaWav( adec_thread_t *p_adec,  
-                               aout_buffer_t *p_aout_buffer)
+static void DecodeAdpcmImaWav( decoder_t *p_dec, int16_t *p_sample,
+                               uint8_t *p_buffer )
 {
-    uint8_t                 *p_buffer;
+    decoder_sys_t *p_sys  = p_dec->p_sys;
     adpcm_ima_wav_channel_t channel[2];
     int                     i_nibbles;
-    uint16_t                *p_sample;
     int                     b_stereo;
-    
-    p_buffer = p_adec->p_block;
-    b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
+
+    b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
 
     GetWord( channel[0].i_predictor );
     GetByte( channel[0].i_step_index );
@@ -576,28 +524,27 @@ static void DecodeAdpcmImaWav( adec_thread_t *p_adec,
         CLAMP( channel[1].i_step_index, 0, 88 );
         p_buffer++;
     }
-    
-    p_sample = (int16_t*)p_aout_buffer->p_buffer;
+
     if( b_stereo )
     {
-        for( i_nibbles = 2 * (p_adec->i_block - 8); 
-             i_nibbles > 0; 
+        for( i_nibbles = 2 * (p_sys->i_block - 8);
+             i_nibbles > 0;
              i_nibbles -= 16 )
         {
             int i;
 
             for( i = 0; i < 4; i++ )
             {
-                p_sample[i * 4] = 
+                p_sample[i * 4] =
                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
                 p_sample[i * 4 + 2] =
                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
             }
             p_buffer += 4;
-            
+
             for( i = 0; i < 4; i++ )
             {
-                p_sample[i * 4 + 1] = 
+                p_sample[i * 4 + 1] =
                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
                 p_sample[i * 4 + 3] =
                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
@@ -611,18 +558,234 @@ static void DecodeAdpcmImaWav( adec_thread_t *p_adec,
     }
     else
     {
-        for( i_nibbles = 2 * (p_adec->i_block - 4); 
-             i_nibbles > 0; 
+        for( i_nibbles = 2 * (p_sys->i_block - 4);
+             i_nibbles > 0;
              i_nibbles -= 2, p_buffer++ )
         {
-            *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
-            p_sample++;
-            *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
-            p_sample++;
+            *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
+            *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
+        }
+    }
+}
+
+/*
+ * Ima4 in QT file
+ */
+static void DecodeAdpcmImaQT( decoder_t *p_dec, int16_t *p_sample,
+                              uint8_t *p_buffer )
+{
+    adpcm_ima_wav_channel_t channel[2];
+    int                     i_nibbles;
+    int                     i_ch;
+    int                     i_step;
+
+    i_step = p_dec->fmt_in.audio.i_channels;
+
+    for( i_ch = 0; i_ch < p_dec->fmt_in.audio.i_channels; i_ch++ )
+    {
+        /* load preambule */
+        channel[i_ch].i_predictor  = (int16_t)((( ( p_buffer[0] << 1 )|(  p_buffer[1] >> 7 ) ))<<7);
+        channel[i_ch].i_step_index = p_buffer[1]&0x7f;
+
+        CLAMP( channel[i_ch].i_step_index, 0, 88 );
+        p_buffer += 2;
+
+        for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
+        {
+            *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
+            p_sample += i_step;
+
+            *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
+            p_sample += i_step;
+
+            p_buffer++;
+        }
+
+        /* Next channel */
+        p_sample += 1 - 64 * i_step;
+    }
+}
+
+/*
+ * Dk4
+ */
+static void DecodeAdpcmDk4( decoder_t *p_dec, int16_t *p_sample,
+                            uint8_t *p_buffer )
+{
+    decoder_sys_t *p_sys  = p_dec->p_sys;
+    adpcm_ima_wav_channel_t channel[2];
+    int                     i_nibbles;
+    int                     b_stereo;
+
+    b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
+
+    GetWord( channel[0].i_predictor );
+    GetByte( channel[0].i_step_index );
+    CLAMP( channel[0].i_step_index, 0, 88 );
+    p_buffer++;
+
+    if( b_stereo )
+    {
+        GetWord( channel[1].i_predictor );
+        GetByte( channel[1].i_step_index );
+        CLAMP( channel[1].i_step_index, 0, 88 );
+        p_buffer++;
+    }
+
+    /* first output predictor */
+    *p_sample++ = channel[0].i_predictor;
+    if( b_stereo )
+    {
+        *p_sample++ = channel[1].i_predictor;
+    }
+
+    for( i_nibbles = 0;
+         i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
+         i_nibbles++ )
+    {
+        *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
+                                              (*p_buffer) >> 4);
+        *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
+                                               (*p_buffer)&0x0f);
+
+        p_buffer++;
+    }
+}
+
+/*
+ * Dk3
+ */
+static void DecodeAdpcmDk3( decoder_t *p_dec, int16_t *p_sample,
+                            uint8_t *p_buffer )
+{
+    decoder_sys_t *p_sys  = p_dec->p_sys;
+    uint8_t                 *p_end = &p_buffer[p_sys->i_block];
+    adpcm_ima_wav_channel_t sum;
+    adpcm_ima_wav_channel_t diff;
+    int                     i_diff_value;
+
+    p_buffer += 10;
+
+    GetWord( sum.i_predictor );
+    GetWord( diff.i_predictor );
+    GetByte( sum.i_step_index );
+    GetByte( diff.i_step_index );
+
+    i_diff_value = diff.i_predictor;
+    /* we process 6 nibbles at once */
+    while( p_buffer + 1 <= p_end )
+    {
+        /* first 3 nibbles */
+        AdpcmImaWavExpandNibble( &sum,
+                                 (*p_buffer)&0x0f);
+
+        AdpcmImaWavExpandNibble( &diff,
+                                 (*p_buffer) >> 4 );
+
+        i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
+
+        *p_sample++ = sum.i_predictor + i_diff_value;
+        *p_sample++ = sum.i_predictor - i_diff_value;
+
+        p_buffer++;
+
+        AdpcmImaWavExpandNibble( &sum,
+                                 (*p_buffer)&0x0f);
+
+        *p_sample++ = sum.i_predictor + i_diff_value;
+        *p_sample++ = sum.i_predictor - i_diff_value;
+
+        /* now last 3 nibbles */
+        AdpcmImaWavExpandNibble( &sum,
+                                 (*p_buffer)>>4);
+        p_buffer++;
+        if( p_buffer < p_end )
+        {
+            AdpcmImaWavExpandNibble( &diff,
+                                     (*p_buffer)&0x0f );
+
+            i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
+
+            *p_sample++ = sum.i_predictor + i_diff_value;
+            *p_sample++ = sum.i_predictor - i_diff_value;
+
+            AdpcmImaWavExpandNibble( &sum,
+                                     (*p_buffer)>>4);
+            p_buffer++;
+
+            *p_sample++ = sum.i_predictor + i_diff_value;
+            *p_sample++ = sum.i_predictor - i_diff_value;
         }
     }
 }
 
 
+/*
+ * EA ADPCM
+ */
+#define MAX_CHAN 5
+static void DecodeAdpcmEA( decoder_t *p_dec, int16_t *p_sample,
+                           uint8_t *p_buffer )
+{
+    static const uint32_t EATable[]=
+    {
+        0x00000000, 0x000000F0, 0x000001CC, 0x00000188,
+        0x00000000, 0x00000000, 0xFFFFFF30, 0xFFFFFF24,
+        0x00000000, 0x00000001, 0x00000003, 0x00000004,
+        0x00000007, 0x00000008, 0x0000000A, 0x0000000B,
+        0x00000000, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFC
+    };
+    decoder_sys_t *p_sys  = p_dec->p_sys;
+    uint8_t *p_end;
+    unsigned i_channels, c;
+    int16_t *prev, *cur;
+    int32_t c1[MAX_CHAN], c2[MAX_CHAN];
+    int8_t d[MAX_CHAN];
+
+    i_channels = p_dec->fmt_in.audio.i_channels;
+    p_end = &p_buffer[p_sys->i_block];
+
+    prev = (int16_t *)p_dec->fmt_in.p_extra;
+    cur = prev + i_channels;
+
+    for (c = 0; c < i_channels; c++)
+    {
+        uint8_t input;
+
+        input = p_buffer[c];
+        c1[c] = EATable[input >> 4];
+        c2[c] = EATable[(input >> 4) + 4];
+        d[c] = (input & 0xf) + 8;
+    }
+
+    for( p_buffer += i_channels; p_buffer < p_end ; p_buffer += i_channels)
+    {
+        for (c = 0; c < i_channels; c++)
+        {
+            int32_t spl;
+
+            spl = (p_buffer[c] >> 4) & 0xf;
+            spl = (spl << 0x1c) >> d[c];
+            spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
+            CLAMP( spl, -32768, 32767 );
+            prev[c] = cur[c];
+            cur[c] = spl;
+
+            *(p_sample++) = spl;
+        }
+
+        for (c = 0; c < i_channels; c++)
+        {
+            int32_t spl;
 
+            spl = p_buffer[c] & 0xf;
+            spl = (spl << 0x1c) >> d[c];
+            spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
+            CLAMP( spl, -32768, 32767 );
+            prev[c] = cur[c];
+            cur[c] = spl;
 
+            *(p_sample++) = spl;
+        }
+    }
+}