]> git.sesse.net Git - vlc/blobdiff - modules/codec/adpcm.c
* modules/gui/skins/win32/win32_run.cpp: clean exit of wxWindows thread
[vlc] / modules / codec / adpcm.c
index ce980da5c6cc6a0cfc7f667e13a6df7ca0d263c9..6c4d116a68d5d8a20213ae78a310bfb6ab277b4e 100644 (file)
@@ -2,15 +2,15 @@
  * adpcm.c : adpcm variant audio decoder
  *****************************************************************************
  * Copyright (C) 2001, 2002 VideoLAN
- * $Id: adpcm.c,v 1.5 2003/01/07 21:49:01 fenrir Exp $
+ * $Id: adpcm.c,v 1.10 2003/03/11 23:56:40 fenrir Exp $
  *
  * 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
 #define ADPCM_IMA_QT    1
 #define ADPCM_IMA_WAV   2
 #define ADPCM_MS        3
+#define ADPCM_DK3       4
+#define ADPCM_DK4       5
 
 typedef struct adec_thread_s
 {
     int i_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;
-    
+
+    uint8_t             *p_buffer;      /* buffer for gather pes */  \
+    int                 i_buffer;       /* bytes present in p_buffer */
+
     /* Input properties */
     decoder_fifo_t *p_fifo;
-    
+
     /* Output properties */
     aout_instance_t *   p_aout;       /* opaque */
     aout_input_t *      p_aout_input; /* opaque */
@@ -75,8 +78,11 @@ 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 * );
+static void DecodeAdpcmMs       ( adec_thread_t *, aout_buffer_t * );
+static void DecodeAdpcmImaWav   ( adec_thread_t *, aout_buffer_t * );
+static void DecodeAdpcmImaQT    ( adec_thread_t *, aout_buffer_t * );
+static void DecodeAdpcmDk4      ( adec_thread_t *, aout_buffer_t * );
+static void DecodeAdpcmDk3      ( adec_thread_t *, aout_buffer_t * );
 
 /*****************************************************************************
  * Module descriptor
@@ -97,7 +103,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 */
@@ -146,18 +152,18 @@ static int i_adaptation_coeff2[7] =
 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 */
+    {
+        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 */
+        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;
-            
+
         default:
             return VLC_EGENERIC;
     }
@@ -179,7 +185,7 @@ static int RunDecoder( decoder_fifo_t *p_fifo )
         return( -1 );
     }
     memset( p_adec, 0, sizeof( adec_thread_t ) );
-    
+
     p_adec->p_fifo = p_fifo;
 
     if( InitThread( p_adec ) != 0 )
@@ -211,11 +217,11 @@ static int RunDecoder( decoder_fifo_t *p_fifo )
 
 #define FREE( p ) if( p ) free( p ); p = NULL
 #define GetWLE( p ) \
-    ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
+    ( *(uint8_t*)(p) + ( *((uint8_t*)(p)+1) << 8 ) )
 
 #define GetDWLE( p ) \
-    (  *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
-        ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
+    (  *(uint8_t*)(p) + ( *((uint8_t*)(p)+1) << 8 ) + \
+        ( *((uint8_t*)(p)+2) << 16 ) + ( *((uint8_t*)(p)+3) << 24 ) )
 
 /*****************************************************************************
  * InitThread: initialize data before entering main loop
@@ -241,12 +247,14 @@ static int InitThread( adec_thread_t * p_adec )
             p_adec->i_codec = ADPCM_MS;
             break;
         case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
+            p_adec->i_codec = ADPCM_DK4;
+            break;
         case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
-            p_adec->i_codec = 0;
+            p_adec->i_codec = ADPCM_DK3;
             break;
     }
 
-    if( p_adec->p_wf->nChannels < 1 || 
+    if( p_adec->p_wf->nChannels < 1 ||
             p_adec->p_wf->nChannels > 2 )
     {
         msg_Err( p_adec->p_fifo, "bad channels count(1-2)" );
@@ -256,17 +264,17 @@ static int InitThread( adec_thread_t * p_adec )
     {
         if( p_adec->i_codec == ADPCM_IMA_QT )
         {
-            p_adec->i_block = 34;
+            p_adec->i_block = 34 * p_adec->p_wf->nChannels;
         }
         else
         {
             p_adec->i_block = 1024; // XXX FIXME
         }
-        msg_Err( p_adec->p_fifo,
-                 "block size undefined, using %d default", 
+        msg_Warn( p_adec->p_fifo,
+                 "block size undefined, using %d default",
                  p_adec->i_block );
     }
-    p_adec->p_block = malloc( p_adec->i_block );
+    p_adec->p_block = NULL;
 
     /* calculate samples per block */
     switch( p_adec->i_codec )
@@ -275,34 +283,44 @@ static int InitThread( adec_thread_t * p_adec )
             p_adec->i_samplesperblock = 64;
             break;
         case ADPCM_IMA_WAV:
-            p_adec->i_samplesperblock = 
+            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->i_samplesperblock =
+                2 * ( p_adec->i_block - 7 * p_adec->p_wf->nChannels ) /
                 p_adec->p_wf->nChannels + 2;
             break;
+        case ADPCM_DK4:
+            p_adec->i_samplesperblock =
+               2 * ( p_adec->i_block - 4 * p_adec->p_wf->nChannels ) /
+               p_adec->p_wf->nChannels + 1;
+            break;
+        case ADPCM_DK3:
+            p_adec->p_wf->nChannels = 2;
+            p_adec->i_samplesperblock = ( 4 * ( p_adec->i_block - 16 ) + 2 )/ 3;
+            break;
         default:
-            p_adec->i_samplesperblock = 0;
+            msg_Err( p_adec->p_fifo, "unknown adpcm variant" );
+            return( -1 );
     }
-   
+
     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->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_physical_channels =
         p_adec->output_format.i_original_channels =
             pi_channels_maps[p_adec->p_wf->nChannels];
     p_adec->p_aout = NULL;
@@ -320,70 +338,140 @@ static int InitThread( adec_thread_t * p_adec )
     }
 
     /* Init the BitStream */
-    InitBitstream( &p_adec->bit_stream, p_adec->p_fifo,
-                   NULL, NULL );
+//    InitBitstream( &p_adec->bit_stream, p_adec->p_fifo,
+//                   NULL, NULL );
 
     return( 0 );
 }
 
 
+static void GetPESData( uint8_t *p_buf, int i_max, pes_packet_t *p_pes )
+{
+    int i_copy;
+    int i_count;
+
+    data_packet_t   *p_data;
+
+    i_count = 0;
+    p_data = p_pes->p_first;
+    while( p_data != NULL && i_count < i_max )
+    {
+
+        i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
+                        i_max - i_count );
+
+        if( i_copy > 0 )
+        {
+            memcpy( p_buf,
+                    p_data->p_payload_start,
+                    i_copy );
+        }
+
+        p_data = p_data->p_next;
+        i_count += i_copy;
+        p_buf   += i_copy;
+    }
+
+    if( i_count < i_max )
+    {
+        memset( p_buf, 0, i_max - i_count );
+    }
+}
+
 /*****************************************************************************
  * DecodeThread: decodes a frame
  *****************************************************************************/
 static void DecodeThread( adec_thread_t *p_adec )
 {
     aout_buffer_t   *p_aout_buffer;
+    pes_packet_t    *p_pes;
 
-    /* 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 );
+    int             i_frame_size;
 
-    /* get output buffer */
-    if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
+    /* **** Get a new frames from streams **** */
+    do
     {
-        aout_DateSet( &p_adec->date, p_adec->pts );
-    }
-    else if( !aout_DateGet( &p_adec->date ) )
-    {
-        return;
-    }
+        input_ExtractPES( p_adec->p_fifo, &p_pes );
+        if( !p_pes )
+        {
+            p_adec->p_fifo->b_error = 1;
+            return;
+        }
+        if( p_pes->i_pts != 0 )
+        {
+            p_adec->pts = p_pes->i_pts;
+        }
+        i_frame_size = p_pes->i_pes_size;
 
-    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 );
+        if( i_frame_size > 0 )
+        {
+            if( p_adec->i_buffer < i_frame_size + 16 )
+            {
+                FREE( p_adec->p_buffer );
+                p_adec->p_buffer = malloc( i_frame_size + 16 );
+                p_adec->i_buffer = i_frame_size + 16;
+            }
 
-    /* decode */
-    
-    switch( p_adec->i_codec )
+            GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
+        }
+        input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
+
+    } while( i_frame_size <= 0 );
+
+    for( p_adec->p_block = p_adec->p_buffer;
+         i_frame_size >= p_adec->i_block;
+         p_adec->p_block += p_adec->i_block, i_frame_size -= p_adec->i_block  )
     {
-        case ADPCM_IMA_QT:
-            break;
-        case ADPCM_IMA_WAV:
-            DecodeAdpcmImaWav( p_adec, p_aout_buffer );
-            break;
-        case ADPCM_MS:
-            DecodeAdpcmMs( p_adec, p_aout_buffer );
-            break;
-        default:
-            break;
-    }
+        /* get output buffer */
+        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_adec->pts = 0;
+
+        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 )
+        {
+            case ADPCM_IMA_QT:
+                DecodeAdpcmImaQT( p_adec, p_aout_buffer );
+                break;
+            case ADPCM_IMA_WAV:
+                DecodeAdpcmImaWav( p_adec, p_aout_buffer );
+                break;
+            case ADPCM_MS:
+                DecodeAdpcmMs( p_adec, p_aout_buffer );
+                break;
+            case ADPCM_DK4:
+                DecodeAdpcmDk4( p_adec, p_aout_buffer );
+            case ADPCM_DK3:
+                DecodeAdpcmDk3( p_adec, p_aout_buffer );
+            default:
+                break;
+        }
 
 
-    /* **** Now we can output these samples **** */
-    aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
+        /* **** Now we can output these samples **** */
+        aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
+    }
 }
 
 
@@ -398,8 +486,8 @@ static void EndThread (adec_thread_t *p_adec)
     }
 
     msg_Dbg( p_adec->p_fifo, "adpcm audio decoder closed" );
-        
-    free( p_adec->p_block );
+
+    FREE( p_adec->p_buffer );
     free( p_adec );
 }
 #define CLAMP( v, min, max ) \
@@ -414,9 +502,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 +522,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 +532,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,8 +540,8 @@ static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
     }
     return( i_predictor );
 }
-    
-static void DecodeAdpcmMs( adec_thread_t *p_adec,  
+
+static void DecodeAdpcmMs( adec_thread_t *p_adec,
                            aout_buffer_t *p_aout_buffer)
 {
     uint8_t            *p_buffer;
@@ -459,7 +550,7 @@ static void DecodeAdpcmMs( adec_thread_t *p_adec,
     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;
 
@@ -480,7 +571,7 @@ static void DecodeAdpcmMs( adec_thread_t *p_adec,
     {
         GetWord( channel[1].i_idelta );
     }
-    
+
     GetWord( channel[0].i_sample1 );
     if( b_stereo )
     {
@@ -513,15 +604,18 @@ static void DecodeAdpcmMs( adec_thread_t *p_adec,
     {
         *p_sample = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
         p_sample++;
-        
-        *p_sample = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0], 
+
+        *p_sample = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
                                          (*p_buffer)&0x0f);
         p_sample++;
     }
 
-    
+
 }
 
+/*
+ * IMA-WAV
+ */
 typedef struct adpcm_ima_wav_channel_s
 {
     int i_predictor;
@@ -552,7 +646,7 @@ static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
     return( p_channel->i_predictor );
 }
 
-static void DecodeAdpcmImaWav( adec_thread_t *p_adec,  
+static void DecodeAdpcmImaWav( adec_thread_t *p_adec,
                                aout_buffer_t *p_aout_buffer)
 {
     uint8_t                 *p_buffer;
@@ -560,7 +654,7 @@ static void DecodeAdpcmImaWav( adec_thread_t *p_adec,
     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;
 
@@ -576,28 +670,28 @@ 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_adec->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,8 +705,8 @@ 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_adec->i_block - 4);
+             i_nibbles > 0;
              i_nibbles -= 2, p_buffer++ )
         {
             *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
@@ -623,6 +717,168 @@ static void DecodeAdpcmImaWav( adec_thread_t *p_adec,
     }
 }
 
+/*
+ * Ima4 in QT file
+ */
+static void DecodeAdpcmImaQT( adec_thread_t *p_adec,
+                              aout_buffer_t *p_aout_buffer )
+{
+    uint8_t                 *p_buffer;
+    adpcm_ima_wav_channel_t channel[2];
+    int                     i_nibbles;
+    uint16_t                *p_sample;
+    int                     i_ch;
+    int                     i_step;
+
+    p_buffer = p_adec->p_block;
+    i_step   = p_adec->p_wf->nChannels;
+
+    for( i_ch = 0; i_ch < p_adec->p_wf->nChannels; i_ch++ )
+    {
+        p_sample = ((int16_t*)p_aout_buffer->p_buffer) + 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++;
+        }
+    }
+}
+
+/*
+ * Dk4
+ */
+
+static void DecodeAdpcmDk4( adec_thread_t *p_adec,
+                               aout_buffer_t *p_aout_buffer)
+{
+    uint8_t                 *p_buffer;
+    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;
+
+    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++;
+    }
+
+    p_sample = (int16_t*)p_aout_buffer->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_adec->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( adec_thread_t *p_adec,
+                               aout_buffer_t *p_aout_buffer)
+{
+    uint8_t                 *p_buffer, *p_end;
+    adpcm_ima_wav_channel_t sum;
+    adpcm_ima_wav_channel_t diff;
+    uint16_t                *p_sample;
+    int                     i_diff_value;
+
+    p_buffer = p_adec->p_block;
+    p_end    = p_buffer + p_adec->i_block;
 
+    p_buffer += 10;
+    GetWord( sum.i_predictor );
+    GetWord( diff.i_predictor );
+    GetByte( sum.i_step_index );
+    GetByte( diff.i_step_index );
+
+    p_sample = (int16_t*)p_aout_buffer->p_buffer;
+    i_diff_value = diff.i_predictor;
+    /* we process 6 nibbles at once */
+    //for( i_group = 0; i_group < ( p_adec->i_block -16 ) / 3; i_group++ )
+    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;
+        }
+
+    }
+
+}