]> git.sesse.net Git - vlc/blob - modules/codec/adpcm.c
* fix ima4 decoding.
[vlc] / modules / codec / adpcm.c
1 /*****************************************************************************
2  * adpcm.c : adpcm variant audio decoder
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: adpcm.c,v 1.10 2003/03/11 23:56:40 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *
27  * Documentation: http://www.pcisys.net/~melanson/codecs/adpcm.txt
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30 #include <vlc/aout.h>
31 #include <vlc/decoder.h>
32 #include <vlc/input.h>
33
34 #include <stdlib.h>                                      /* malloc(), free() */
35 #include <string.h>                                              /* strdup() */
36 #include "codecs.h"
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40
41 #define ADPCM_IMA_QT    1
42 #define ADPCM_IMA_WAV   2
43 #define ADPCM_MS        3
44 #define ADPCM_DK3       4
45 #define ADPCM_DK4       5
46
47 typedef struct adec_thread_s
48 {
49     int i_codec;
50
51     WAVEFORMATEX    *p_wf;
52
53     int                 i_block;
54     uint8_t             *p_block;
55     int                 i_samplesperblock;
56
57     uint8_t             *p_buffer;      /* buffer for gather pes */  \
58     int                 i_buffer;       /* bytes present in p_buffer */
59
60     /* Input properties */
61     decoder_fifo_t *p_fifo;
62
63     /* Output properties */
64     aout_instance_t *   p_aout;       /* opaque */
65     aout_input_t *      p_aout_input; /* opaque */
66     audio_sample_format_t output_format;
67
68     audio_date_t        date;
69     mtime_t             pts;
70
71 } adec_thread_t;
72
73 static int  OpenDecoder    ( vlc_object_t * );
74
75 static int  RunDecoder     ( decoder_fifo_t * );
76 static int  InitThread     ( adec_thread_t * );
77 static void DecodeThread   ( adec_thread_t * );
78 static void EndThread      ( adec_thread_t * );
79
80
81 static void DecodeAdpcmMs       ( adec_thread_t *, aout_buffer_t * );
82 static void DecodeAdpcmImaWav   ( adec_thread_t *, aout_buffer_t * );
83 static void DecodeAdpcmImaQT    ( adec_thread_t *, aout_buffer_t * );
84 static void DecodeAdpcmDk4      ( adec_thread_t *, aout_buffer_t * );
85 static void DecodeAdpcmDk3      ( adec_thread_t *, aout_buffer_t * );
86
87 /*****************************************************************************
88  * Module descriptor
89  *****************************************************************************/
90
91 vlc_module_begin();
92     set_description( _("ADPCM audio deocder") );
93     set_capability( "decoder", 50 );
94     set_callbacks( OpenDecoder, NULL );
95 vlc_module_end();
96
97
98 static int pi_channels_maps[6] =
99 {
100     0,
101     AOUT_CHAN_CENTER,
102     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
103     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
104     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
105     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
106      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
107 };
108
109 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
110 static int i_index_table[16] =
111 {
112     -1, -1, -1, -1, 2, 4, 6, 8,
113     -1, -1, -1, -1, 2, 4, 6, 8
114 };
115
116 static int i_step_table[89] =
117 {
118     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
119     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
120     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
121     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
122     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
123     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
124     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
125     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
126     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
127 };
128
129 static int i_adaptation_table[16] =
130 {
131     230, 230, 230, 230, 307, 409, 512, 614,
132     768, 614, 512, 409, 307, 230, 230, 230
133 };
134
135 static int i_adaptation_coeff1[7] =
136 {
137     256, 512, 0, 192, 240, 460, 392
138 };
139
140 static int i_adaptation_coeff2[7] =
141 {
142     0, -256, 0, 64, 0, -208, -232
143 };
144
145
146 /*****************************************************************************
147  * OpenDecoder: probe the decoder and return score
148  *****************************************************************************
149  * Tries to launch a decoder and return score so that the interface is able
150  * to choose.
151  *****************************************************************************/
152 static int OpenDecoder( vlc_object_t *p_this )
153 {
154     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
155
156     switch( p_fifo->i_fourcc )
157     {
158         case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
159         case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
160         case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
161         case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
162         case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
163
164             p_fifo->pf_run = RunDecoder;
165             return VLC_SUCCESS;
166
167         default:
168             return VLC_EGENERIC;
169     }
170
171 }
172
173 /*****************************************************************************
174  * RunDecoder: this function is called just after the thread is created
175  *****************************************************************************/
176 static int RunDecoder( decoder_fifo_t *p_fifo )
177 {
178     adec_thread_t *p_adec;
179     int b_error;
180
181     if( !( p_adec = malloc( sizeof( adec_thread_t ) ) ) )
182     {
183         msg_Err( p_fifo, "out of memory" );
184         DecoderError( p_fifo );
185         return( -1 );
186     }
187     memset( p_adec, 0, sizeof( adec_thread_t ) );
188
189     p_adec->p_fifo = p_fifo;
190
191     if( InitThread( p_adec ) != 0 )
192     {
193         DecoderError( p_fifo );
194         return( -1 );
195     }
196
197     while( ( !p_adec->p_fifo->b_die )&&( !p_adec->p_fifo->b_error ) )
198     {
199         DecodeThread( p_adec );
200     }
201
202
203     if( ( b_error = p_adec->p_fifo->b_error ) )
204     {
205         DecoderError( p_adec->p_fifo );
206     }
207
208     EndThread( p_adec );
209     if( b_error )
210     {
211         return( -1 );
212     }
213
214     return( 0 );
215 }
216
217
218 #define FREE( p ) if( p ) free( p ); p = NULL
219 #define GetWLE( p ) \
220     ( *(uint8_t*)(p) + ( *((uint8_t*)(p)+1) << 8 ) )
221
222 #define GetDWLE( p ) \
223     (  *(uint8_t*)(p) + ( *((uint8_t*)(p)+1) << 8 ) + \
224         ( *((uint8_t*)(p)+2) << 16 ) + ( *((uint8_t*)(p)+3) << 24 ) )
225
226 /*****************************************************************************
227  * InitThread: initialize data before entering main loop
228  *****************************************************************************/
229
230 static int InitThread( adec_thread_t * p_adec )
231 {
232     if( ( p_adec->p_wf = (WAVEFORMATEX*)p_adec->p_fifo->p_waveformatex ) == NULL )
233     {
234         msg_Err( p_adec->p_fifo, "missing format" );
235         return( -1 );
236     }
237     /* fourcc to codec */
238     switch( p_adec->p_fifo->i_fourcc )
239     {
240         case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
241             p_adec->i_codec = ADPCM_IMA_QT;
242             break;
243         case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
244             p_adec->i_codec = ADPCM_IMA_WAV;
245             break;
246         case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
247             p_adec->i_codec = ADPCM_MS;
248             break;
249         case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
250             p_adec->i_codec = ADPCM_DK4;
251             break;
252         case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
253             p_adec->i_codec = ADPCM_DK3;
254             break;
255     }
256
257     if( p_adec->p_wf->nChannels < 1 ||
258             p_adec->p_wf->nChannels > 2 )
259     {
260         msg_Err( p_adec->p_fifo, "bad channels count(1-2)" );
261         return( -1 );
262     }
263     if( !( p_adec->i_block = p_adec->p_wf->nBlockAlign ) )
264     {
265         if( p_adec->i_codec == ADPCM_IMA_QT )
266         {
267             p_adec->i_block = 34 * p_adec->p_wf->nChannels;
268         }
269         else
270         {
271             p_adec->i_block = 1024; // XXX FIXME
272         }
273         msg_Warn( p_adec->p_fifo,
274                  "block size undefined, using %d default",
275                  p_adec->i_block );
276     }
277     p_adec->p_block = NULL;
278
279     /* calculate samples per block */
280     switch( p_adec->i_codec )
281     {
282         case ADPCM_IMA_QT:
283             p_adec->i_samplesperblock = 64;
284             break;
285         case ADPCM_IMA_WAV:
286             p_adec->i_samplesperblock =
287                  2 * ( p_adec->i_block - 4 * p_adec->p_wf->nChannels )/
288                  p_adec->p_wf->nChannels;
289                  break;
290         case ADPCM_MS:
291             p_adec->i_samplesperblock =
292                 2 * ( p_adec->i_block - 7 * p_adec->p_wf->nChannels ) /
293                 p_adec->p_wf->nChannels + 2;
294             break;
295         case ADPCM_DK4:
296             p_adec->i_samplesperblock =
297                2 * ( p_adec->i_block - 4 * p_adec->p_wf->nChannels ) /
298                p_adec->p_wf->nChannels + 1;
299             break;
300         case ADPCM_DK3:
301             p_adec->p_wf->nChannels = 2;
302             p_adec->i_samplesperblock = ( 4 * ( p_adec->i_block - 16 ) + 2 )/ 3;
303             break;
304         default:
305             msg_Err( p_adec->p_fifo, "unknown adpcm variant" );
306             return( -1 );
307     }
308
309     msg_Dbg( p_adec->p_fifo,
310              "format: samplerate:%dHz channels:%d bits/sample:%d blockalign:%d samplesperblock %d",
311              p_adec->p_wf->nSamplesPerSec,
312              p_adec->p_wf->nChannels,
313              p_adec->p_wf->wBitsPerSample,
314              p_adec->p_wf->nBlockAlign,
315              p_adec->i_samplesperblock );
316
317     //p_adec->output_format.i_format = VLC_FOURCC('s','1','6','l');
318     /* FIXME good way ? */
319     p_adec->output_format.i_format = AOUT_FMT_S16_NE;
320     p_adec->output_format.i_rate = p_adec->p_wf->nSamplesPerSec;
321
322
323     p_adec->output_format.i_physical_channels =
324         p_adec->output_format.i_original_channels =
325             pi_channels_maps[p_adec->p_wf->nChannels];
326     p_adec->p_aout = NULL;
327     p_adec->p_aout_input = NULL;
328
329     /* **** Create a new audio output **** */
330     aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
331     p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
332                                         &p_adec->p_aout,
333                                         &p_adec->output_format );
334     if( !p_adec->p_aout_input )
335     {
336         msg_Err( p_adec->p_fifo, "cannot create aout" );
337         return( -1 );
338     }
339
340     /* Init the BitStream */
341 //    InitBitstream( &p_adec->bit_stream, p_adec->p_fifo,
342 //                   NULL, NULL );
343
344     return( 0 );
345 }
346
347
348 static void GetPESData( uint8_t *p_buf, int i_max, pes_packet_t *p_pes )
349 {
350     int i_copy;
351     int i_count;
352
353     data_packet_t   *p_data;
354
355     i_count = 0;
356     p_data = p_pes->p_first;
357     while( p_data != NULL && i_count < i_max )
358     {
359
360         i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
361                         i_max - i_count );
362
363         if( i_copy > 0 )
364         {
365             memcpy( p_buf,
366                     p_data->p_payload_start,
367                     i_copy );
368         }
369
370         p_data = p_data->p_next;
371         i_count += i_copy;
372         p_buf   += i_copy;
373     }
374
375     if( i_count < i_max )
376     {
377         memset( p_buf, 0, i_max - i_count );
378     }
379 }
380
381 /*****************************************************************************
382  * DecodeThread: decodes a frame
383  *****************************************************************************/
384 static void DecodeThread( adec_thread_t *p_adec )
385 {
386     aout_buffer_t   *p_aout_buffer;
387     pes_packet_t    *p_pes;
388
389     int             i_frame_size;
390
391     /* **** Get a new frames from streams **** */
392     do
393     {
394         input_ExtractPES( p_adec->p_fifo, &p_pes );
395         if( !p_pes )
396         {
397             p_adec->p_fifo->b_error = 1;
398             return;
399         }
400         if( p_pes->i_pts != 0 )
401         {
402             p_adec->pts = p_pes->i_pts;
403         }
404         i_frame_size = p_pes->i_pes_size;
405
406         if( i_frame_size > 0 )
407         {
408             if( p_adec->i_buffer < i_frame_size + 16 )
409             {
410                 FREE( p_adec->p_buffer );
411                 p_adec->p_buffer = malloc( i_frame_size + 16 );
412                 p_adec->i_buffer = i_frame_size + 16;
413             }
414
415             GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
416         }
417         input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
418
419     } while( i_frame_size <= 0 );
420
421     for( p_adec->p_block = p_adec->p_buffer;
422          i_frame_size >= p_adec->i_block;
423          p_adec->p_block += p_adec->i_block, i_frame_size -= p_adec->i_block  )
424     {
425         /* get output buffer */
426         if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
427         {
428             aout_DateSet( &p_adec->date, p_adec->pts );
429         }
430         else if( !aout_DateGet( &p_adec->date ) )
431         {
432             return;
433         }
434         p_adec->pts = 0;
435
436         p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
437                                            p_adec->p_aout_input,
438                                            p_adec->i_samplesperblock );
439         if( !p_aout_buffer )
440         {
441             msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
442             p_adec->p_fifo->b_error = 1;
443             return;
444         }
445
446         p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
447         p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
448                                                       p_adec->i_samplesperblock );
449
450         /* decode */
451
452         switch( p_adec->i_codec )
453         {
454             case ADPCM_IMA_QT:
455                 DecodeAdpcmImaQT( p_adec, p_aout_buffer );
456                 break;
457             case ADPCM_IMA_WAV:
458                 DecodeAdpcmImaWav( p_adec, p_aout_buffer );
459                 break;
460             case ADPCM_MS:
461                 DecodeAdpcmMs( p_adec, p_aout_buffer );
462                 break;
463             case ADPCM_DK4:
464                 DecodeAdpcmDk4( p_adec, p_aout_buffer );
465             case ADPCM_DK3:
466                 DecodeAdpcmDk3( p_adec, p_aout_buffer );
467             default:
468                 break;
469         }
470
471
472         /* **** Now we can output these samples **** */
473         aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
474     }
475 }
476
477
478 /*****************************************************************************
479  * EndThread : faad decoder thread destruction
480  *****************************************************************************/
481 static void EndThread (adec_thread_t *p_adec)
482 {
483     if( p_adec->p_aout_input )
484     {
485         aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
486     }
487
488     msg_Dbg( p_adec->p_fifo, "adpcm audio decoder closed" );
489
490     FREE( p_adec->p_buffer );
491     free( p_adec );
492 }
493 #define CLAMP( v, min, max ) \
494     if( (v) < (min) ) (v) = (min); \
495     if( (v) > (max) ) (v) = (max)
496
497 #define GetByte( v ) \
498     (v) = *p_buffer; p_buffer++;
499
500 #define GetWord( v ) \
501     (v) = *p_buffer; p_buffer++; \
502     (v) |= ( *p_buffer ) << 8; p_buffer++; \
503     if( (v)&0x8000 ) (v) -= 0x010000;
504
505 /*
506  * MS
507  */
508 typedef struct adpcm_ms_channel_s
509 {
510     int i_idelta;
511     int i_sample1, i_sample2;
512     int i_coeff1, i_coeff2;
513
514 } adpcm_ms_channel_t;
515
516
517 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
518                                int i_nibble )
519 {
520     int i_predictor;
521     int i_snibble;
522     /* expand sign */
523
524     i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
525
526     i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
527                     p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
528                   i_snibble * p_channel->i_idelta;
529
530     CLAMP( i_predictor, -32768, 32767 );
531
532     p_channel->i_sample2 = p_channel->i_sample1;
533     p_channel->i_sample1 = i_predictor;
534
535     p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
536                             p_channel->i_idelta ) / 256;
537     if( p_channel->i_idelta < 16 )
538     {
539         p_channel->i_idelta = 16;
540     }
541     return( i_predictor );
542 }
543
544 static void DecodeAdpcmMs( adec_thread_t *p_adec,
545                            aout_buffer_t *p_aout_buffer)
546 {
547     uint8_t            *p_buffer;
548     adpcm_ms_channel_t channel[2];
549     int i_nibbles;
550     uint16_t           *p_sample;
551     int b_stereo;
552     int i_block_predictor;
553
554     p_buffer = p_adec->p_block;
555     b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
556
557     GetByte( i_block_predictor );
558     CLAMP( i_block_predictor, 0, 6 );
559     channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
560     channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
561
562     if( b_stereo )
563     {
564         GetByte( i_block_predictor );
565         CLAMP( i_block_predictor, 0, 6 );
566         channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
567         channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
568     }
569     GetWord( channel[0].i_idelta );
570     if( b_stereo )
571     {
572         GetWord( channel[1].i_idelta );
573     }
574
575     GetWord( channel[0].i_sample1 );
576     if( b_stereo )
577     {
578         GetWord( channel[1].i_sample1 );
579     }
580
581     GetWord( channel[0].i_sample2 );
582     if( b_stereo )
583     {
584         GetWord( channel[1].i_sample2 );
585     }
586
587     p_sample = (int16_t*)p_aout_buffer->p_buffer;
588
589     if( b_stereo )
590     {
591         *p_sample = channel[0].i_sample2; p_sample++;
592         *p_sample = channel[1].i_sample2; p_sample++;
593         *p_sample = channel[0].i_sample1; p_sample++;
594         *p_sample = channel[1].i_sample1; p_sample++;
595     }
596     else
597     {
598         *p_sample = channel[0].i_sample2; p_sample++;
599         *p_sample = channel[0].i_sample1; p_sample++;
600     }
601
602     for( i_nibbles =  2 *( p_adec->i_block - 7 * p_adec->p_wf->nChannels );
603          i_nibbles > 0; i_nibbles -= 2,p_buffer++ )
604     {
605         *p_sample = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
606         p_sample++;
607
608         *p_sample = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
609                                          (*p_buffer)&0x0f);
610         p_sample++;
611     }
612
613
614 }
615
616 /*
617  * IMA-WAV
618  */
619 typedef struct adpcm_ima_wav_channel_s
620 {
621     int i_predictor;
622     int i_step_index;
623
624 } adpcm_ima_wav_channel_t;
625
626 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
627                                    int i_nibble )
628 {
629     int i_diff;
630
631     i_diff = i_step_table[p_channel->i_step_index] >> 3;
632     if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
633     if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
634     if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
635     if( i_nibble&0x08 )
636         p_channel->i_predictor -= i_diff;
637     else
638         p_channel->i_predictor += i_diff;
639
640     CLAMP( p_channel->i_predictor, -32768, 32767 );
641
642     p_channel->i_step_index += i_index_table[i_nibble];
643
644     CLAMP( p_channel->i_step_index, 0, 88 );
645
646     return( p_channel->i_predictor );
647 }
648
649 static void DecodeAdpcmImaWav( adec_thread_t *p_adec,
650                                aout_buffer_t *p_aout_buffer)
651 {
652     uint8_t                 *p_buffer;
653     adpcm_ima_wav_channel_t channel[2];
654     int                     i_nibbles;
655     uint16_t                *p_sample;
656     int                     b_stereo;
657
658     p_buffer = p_adec->p_block;
659     b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
660
661     GetWord( channel[0].i_predictor );
662     GetByte( channel[0].i_step_index );
663     CLAMP( channel[0].i_step_index, 0, 88 );
664     p_buffer++;
665
666     if( b_stereo )
667     {
668         GetWord( channel[1].i_predictor );
669         GetByte( channel[1].i_step_index );
670         CLAMP( channel[1].i_step_index, 0, 88 );
671         p_buffer++;
672     }
673
674     p_sample = (int16_t*)p_aout_buffer->p_buffer;
675     if( b_stereo )
676     {
677         for( i_nibbles = 2 * (p_adec->i_block - 8);
678              i_nibbles > 0;
679              i_nibbles -= 16 )
680         {
681             int i;
682
683             for( i = 0; i < 4; i++ )
684             {
685                 p_sample[i * 4] =
686                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
687                 p_sample[i * 4 + 2] =
688                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
689             }
690             p_buffer += 4;
691
692             for( i = 0; i < 4; i++ )
693             {
694                 p_sample[i * 4 + 1] =
695                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
696                 p_sample[i * 4 + 3] =
697                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
698             }
699             p_buffer += 4;
700             p_sample += 16;
701
702         }
703
704
705     }
706     else
707     {
708         for( i_nibbles = 2 * (p_adec->i_block - 4);
709              i_nibbles > 0;
710              i_nibbles -= 2, p_buffer++ )
711         {
712             *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
713             p_sample++;
714             *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
715             p_sample++;
716         }
717     }
718 }
719
720 /*
721  * Ima4 in QT file
722  */
723 static void DecodeAdpcmImaQT( adec_thread_t *p_adec,
724                               aout_buffer_t *p_aout_buffer )
725 {
726     uint8_t                 *p_buffer;
727     adpcm_ima_wav_channel_t channel[2];
728     int                     i_nibbles;
729     uint16_t                *p_sample;
730     int                     i_ch;
731     int                     i_step;
732
733     p_buffer = p_adec->p_block;
734     i_step   = p_adec->p_wf->nChannels;
735
736     for( i_ch = 0; i_ch < p_adec->p_wf->nChannels; i_ch++ )
737     {
738         p_sample = ((int16_t*)p_aout_buffer->p_buffer) + i_ch;
739         /* load preambule */
740         channel[i_ch].i_predictor  = (int16_t)((( ( p_buffer[0] << 1 )|(  p_buffer[1] >> 7 ) ))<<7);
741         channel[i_ch].i_step_index = p_buffer[1]&0x7f;
742
743         CLAMP( channel[i_ch].i_step_index, 0, 88 );
744         p_buffer += 2;
745
746         for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
747         {
748             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
749             p_sample += i_step;
750
751             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
752             p_sample += i_step;
753
754             p_buffer++;
755         }
756     }
757 }
758
759 /*
760  * Dk4
761  */
762
763 static void DecodeAdpcmDk4( adec_thread_t *p_adec,
764                                aout_buffer_t *p_aout_buffer)
765 {
766     uint8_t                 *p_buffer;
767     adpcm_ima_wav_channel_t channel[2];
768     int                     i_nibbles;
769     uint16_t                *p_sample;
770     int                     b_stereo;
771
772     p_buffer = p_adec->p_block;
773     b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
774
775     GetWord( channel[0].i_predictor );
776     GetByte( channel[0].i_step_index );
777     CLAMP( channel[0].i_step_index, 0, 88 );
778     p_buffer++;
779
780     if( b_stereo )
781     {
782         GetWord( channel[1].i_predictor );
783         GetByte( channel[1].i_step_index );
784         CLAMP( channel[1].i_step_index, 0, 88 );
785         p_buffer++;
786     }
787
788     p_sample = (int16_t*)p_aout_buffer->p_buffer;
789
790     /* first output predictor */
791     *p_sample++ = channel[0].i_predictor;
792     if( b_stereo )
793     {
794         *p_sample++ = channel[1].i_predictor;
795     }
796
797     for( i_nibbles = 0;
798          i_nibbles < p_adec->i_block - 4 * (b_stereo ? 2:1 );
799          i_nibbles++ )
800     {
801         *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
802                                               (*p_buffer) >> 4);
803         *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
804                                                (*p_buffer)&0x0f);
805
806         p_buffer++;
807     }
808 }
809
810 /*
811  * Dk3
812  */
813
814 static void DecodeAdpcmDk3( adec_thread_t *p_adec,
815                                aout_buffer_t *p_aout_buffer)
816 {
817     uint8_t                 *p_buffer, *p_end;
818     adpcm_ima_wav_channel_t sum;
819     adpcm_ima_wav_channel_t diff;
820     uint16_t                *p_sample;
821     int                     i_diff_value;
822
823     p_buffer = p_adec->p_block;
824     p_end    = p_buffer + p_adec->i_block;
825
826     p_buffer += 10;
827     GetWord( sum.i_predictor );
828     GetWord( diff.i_predictor );
829     GetByte( sum.i_step_index );
830     GetByte( diff.i_step_index );
831
832     p_sample = (int16_t*)p_aout_buffer->p_buffer;
833     i_diff_value = diff.i_predictor;
834     /* we process 6 nibbles at once */
835     //for( i_group = 0; i_group < ( p_adec->i_block -16 ) / 3; i_group++ )
836     while( p_buffer + 1 <= p_end )
837     {
838         /* first 3 nibbles */
839         AdpcmImaWavExpandNibble( &sum,
840                                  (*p_buffer)&0x0f);
841
842         AdpcmImaWavExpandNibble( &diff,
843                                  (*p_buffer) >> 4 );
844
845         i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
846
847         *p_sample++ = sum.i_predictor + i_diff_value;
848         *p_sample++ = sum.i_predictor - i_diff_value;
849
850         p_buffer++;
851
852         AdpcmImaWavExpandNibble( &sum,
853                                  (*p_buffer)&0x0f);
854
855         *p_sample++ = sum.i_predictor + i_diff_value;
856         *p_sample++ = sum.i_predictor - i_diff_value;
857
858         /* now last 3 nibbles */
859         AdpcmImaWavExpandNibble( &sum,
860                                  (*p_buffer)>>4);
861         p_buffer++;
862         if( p_buffer < p_end )
863         {
864             AdpcmImaWavExpandNibble( &diff,
865                                      (*p_buffer)&0x0f );
866
867             i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
868
869             *p_sample++ = sum.i_predictor + i_diff_value;
870             *p_sample++ = sum.i_predictor - i_diff_value;
871
872             AdpcmImaWavExpandNibble( &sum,
873                                      (*p_buffer)>>4);
874             p_buffer++;
875
876             *p_sample++ = sum.i_predictor + i_diff_value;
877             *p_sample++ = sum.i_predictor - i_diff_value;
878         }
879
880     }
881
882 }
883
884