]> git.sesse.net Git - vlc/blob - modules/codec/adpcm.c
* ALL: Introduction of a new api for decoders.
[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.13 2003/09/02 20:19:25 gbazin 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 decoder") );
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_t *p_dec = (decoder_t*)p_this;
155
156     switch( p_dec->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_dec->pf_run = RunDecoder;
165             return VLC_SUCCESS;
166
167         default:
168             return VLC_EGENERIC;
169     }
170 }
171
172 /*****************************************************************************
173  * RunDecoder: this function is called just after the thread is created
174  *****************************************************************************/
175 static int RunDecoder( decoder_fifo_t *p_fifo )
176 {
177     adec_thread_t *p_adec;
178     int b_error;
179
180     if( !( p_adec = malloc( sizeof( adec_thread_t ) ) ) )
181     {
182         msg_Err( p_fifo, "out of memory" );
183         DecoderError( p_fifo );
184         return( -1 );
185     }
186     memset( p_adec, 0, sizeof( adec_thread_t ) );
187
188     p_adec->p_fifo = p_fifo;
189
190     if( InitThread( p_adec ) != 0 )
191     {
192         DecoderError( p_fifo );
193         return( -1 );
194     }
195
196     while( ( !p_adec->p_fifo->b_die )&&( !p_adec->p_fifo->b_error ) )
197     {
198         DecodeThread( p_adec );
199     }
200
201
202     if( ( b_error = p_adec->p_fifo->b_error ) )
203     {
204         DecoderError( p_adec->p_fifo );
205     }
206
207     EndThread( p_adec );
208     if( b_error )
209     {
210         return( -1 );
211     }
212
213     return( 0 );
214 }
215
216
217 #define FREE( p ) if( p ) free( p ); p = NULL
218
219 /*****************************************************************************
220  * InitThread: initialize data before entering main loop
221  *****************************************************************************/
222
223 static int InitThread( adec_thread_t * p_adec )
224 {
225     if( ( p_adec->p_wf = (WAVEFORMATEX*)p_adec->p_fifo->p_waveformatex ) == NULL )
226     {
227         msg_Err( p_adec->p_fifo, "missing format" );
228         return( -1 );
229     }
230     /* fourcc to codec */
231     switch( p_adec->p_fifo->i_fourcc )
232     {
233         case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
234             p_adec->i_codec = ADPCM_IMA_QT;
235             break;
236         case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
237             p_adec->i_codec = ADPCM_IMA_WAV;
238             break;
239         case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
240             p_adec->i_codec = ADPCM_MS;
241             break;
242         case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
243             p_adec->i_codec = ADPCM_DK4;
244             break;
245         case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
246             p_adec->i_codec = ADPCM_DK3;
247             break;
248     }
249
250     if( p_adec->p_wf->nChannels < 1 ||
251             p_adec->p_wf->nChannels > 2 )
252     {
253         msg_Err( p_adec->p_fifo, "bad channels count(1-2)" );
254         return( -1 );
255     }
256     if( !( p_adec->i_block = p_adec->p_wf->nBlockAlign ) )
257     {
258         if( p_adec->i_codec == ADPCM_IMA_QT )
259         {
260             p_adec->i_block = 34 * p_adec->p_wf->nChannels;
261         }
262         else
263         {
264             p_adec->i_block = 1024; // XXX FIXME
265         }
266         msg_Warn( p_adec->p_fifo,
267                  "block size undefined, using %d default",
268                  p_adec->i_block );
269     }
270     p_adec->p_block = NULL;
271
272     /* calculate samples per block */
273     switch( p_adec->i_codec )
274     {
275         case ADPCM_IMA_QT:
276             p_adec->i_samplesperblock = 64;
277             break;
278         case ADPCM_IMA_WAV:
279             p_adec->i_samplesperblock =
280                  2 * ( p_adec->i_block - 4 * p_adec->p_wf->nChannels )/
281                  p_adec->p_wf->nChannels;
282                  break;
283         case ADPCM_MS:
284             p_adec->i_samplesperblock =
285                 2 * ( p_adec->i_block - 7 * p_adec->p_wf->nChannels ) /
286                 p_adec->p_wf->nChannels + 2;
287             break;
288         case ADPCM_DK4:
289             p_adec->i_samplesperblock =
290                2 * ( p_adec->i_block - 4 * p_adec->p_wf->nChannels ) /
291                p_adec->p_wf->nChannels + 1;
292             break;
293         case ADPCM_DK3:
294             p_adec->p_wf->nChannels = 2;
295             p_adec->i_samplesperblock = ( 4 * ( p_adec->i_block - 16 ) + 2 )/ 3;
296             break;
297         default:
298             msg_Err( p_adec->p_fifo, "unknown adpcm variant" );
299             return( -1 );
300     }
301
302     msg_Dbg( p_adec->p_fifo,
303              "format: samplerate:%dHz channels:%d bits/sample:%d blockalign:%d samplesperblock %d",
304              p_adec->p_wf->nSamplesPerSec,
305              p_adec->p_wf->nChannels,
306              p_adec->p_wf->wBitsPerSample,
307              p_adec->p_wf->nBlockAlign,
308              p_adec->i_samplesperblock );
309
310     //p_adec->output_format.i_format = VLC_FOURCC('s','1','6','l');
311     /* FIXME good way ? */
312     p_adec->output_format.i_format = AOUT_FMT_S16_NE;
313     p_adec->output_format.i_rate = p_adec->p_wf->nSamplesPerSec;
314
315
316     p_adec->output_format.i_physical_channels =
317         p_adec->output_format.i_original_channels =
318             pi_channels_maps[p_adec->p_wf->nChannels];
319     p_adec->p_aout = NULL;
320     p_adec->p_aout_input = NULL;
321
322     /* **** Create a new audio output **** */
323     aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
324     p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
325                                         &p_adec->p_aout,
326                                         &p_adec->output_format );
327     if( !p_adec->p_aout_input )
328     {
329         msg_Err( p_adec->p_fifo, "cannot create aout" );
330         return( -1 );
331     }
332
333     /* Init the BitStream */
334 //    InitBitstream( &p_adec->bit_stream, p_adec->p_fifo,
335 //                   NULL, NULL );
336
337     return( 0 );
338 }
339
340
341 static void GetPESData( uint8_t *p_buf, int i_max, pes_packet_t *p_pes )
342 {
343     int i_copy;
344     int i_count;
345
346     data_packet_t   *p_data;
347
348     i_count = 0;
349     p_data = p_pes->p_first;
350     while( p_data != NULL && i_count < i_max )
351     {
352
353         i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
354                         i_max - i_count );
355
356         if( i_copy > 0 )
357         {
358             memcpy( p_buf,
359                     p_data->p_payload_start,
360                     i_copy );
361         }
362
363         p_data = p_data->p_next;
364         i_count += i_copy;
365         p_buf   += i_copy;
366     }
367
368     if( i_count < i_max )
369     {
370         memset( p_buf, 0, i_max - i_count );
371     }
372 }
373
374 /*****************************************************************************
375  * DecodeThread: decodes a frame
376  *****************************************************************************/
377 static void DecodeThread( adec_thread_t *p_adec )
378 {
379     aout_buffer_t   *p_aout_buffer;
380     pes_packet_t    *p_pes;
381
382     int             i_frame_size;
383
384     /* **** Get a new frames from streams **** */
385     do
386     {
387         input_ExtractPES( p_adec->p_fifo, &p_pes );
388         if( !p_pes )
389         {
390             p_adec->p_fifo->b_error = 1;
391             return;
392         }
393         if( p_pes->i_pts != 0 )
394         {
395             p_adec->pts = p_pes->i_pts;
396         }
397         i_frame_size = p_pes->i_pes_size;
398
399         if( i_frame_size > 0 )
400         {
401             if( p_adec->i_buffer < i_frame_size + 16 )
402             {
403                 FREE( p_adec->p_buffer );
404                 p_adec->p_buffer = malloc( i_frame_size + 16 );
405                 p_adec->i_buffer = i_frame_size + 16;
406             }
407
408             GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
409         }
410         input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
411
412     } while( i_frame_size <= 0 );
413
414     for( p_adec->p_block = p_adec->p_buffer;
415          i_frame_size >= p_adec->i_block;
416          p_adec->p_block += p_adec->i_block, i_frame_size -= p_adec->i_block  )
417     {
418         /* get output buffer */
419         if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
420         {
421             aout_DateSet( &p_adec->date, p_adec->pts );
422         }
423         else if( !aout_DateGet( &p_adec->date ) )
424         {
425             return;
426         }
427         p_adec->pts = 0;
428
429         p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
430                                            p_adec->p_aout_input,
431                                            p_adec->i_samplesperblock );
432         if( !p_aout_buffer )
433         {
434             msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
435             p_adec->p_fifo->b_error = 1;
436             return;
437         }
438
439         p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
440         p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
441                                                       p_adec->i_samplesperblock );
442
443         /* decode */
444
445         switch( p_adec->i_codec )
446         {
447             case ADPCM_IMA_QT:
448                 DecodeAdpcmImaQT( p_adec, p_aout_buffer );
449                 break;
450             case ADPCM_IMA_WAV:
451                 DecodeAdpcmImaWav( p_adec, p_aout_buffer );
452                 break;
453             case ADPCM_MS:
454                 DecodeAdpcmMs( p_adec, p_aout_buffer );
455                 break;
456             case ADPCM_DK4:
457                 DecodeAdpcmDk4( p_adec, p_aout_buffer );
458             case ADPCM_DK3:
459                 DecodeAdpcmDk3( p_adec, p_aout_buffer );
460             default:
461                 break;
462         }
463
464
465         /* **** Now we can output these samples **** */
466         aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
467     }
468 }
469
470
471 /*****************************************************************************
472  * EndThread : faad decoder thread destruction
473  *****************************************************************************/
474 static void EndThread (adec_thread_t *p_adec)
475 {
476     if( p_adec->p_aout_input )
477     {
478         aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
479     }
480
481     msg_Dbg( p_adec->p_fifo, "adpcm audio decoder closed" );
482
483     FREE( p_adec->p_buffer );
484     free( p_adec );
485 }
486 #define CLAMP( v, min, max ) \
487     if( (v) < (min) ) (v) = (min); \
488     if( (v) > (max) ) (v) = (max)
489
490 #define GetByte( v ) \
491     (v) = *p_buffer; p_buffer++;
492
493 #define GetWord( v ) \
494     (v) = *p_buffer; p_buffer++; \
495     (v) |= ( *p_buffer ) << 8; p_buffer++; \
496     if( (v)&0x8000 ) (v) -= 0x010000;
497
498 /*
499  * MS
500  */
501 typedef struct adpcm_ms_channel_s
502 {
503     int i_idelta;
504     int i_sample1, i_sample2;
505     int i_coeff1, i_coeff2;
506
507 } adpcm_ms_channel_t;
508
509
510 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
511                                int i_nibble )
512 {
513     int i_predictor;
514     int i_snibble;
515     /* expand sign */
516
517     i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
518
519     i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
520                     p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
521                   i_snibble * p_channel->i_idelta;
522
523     CLAMP( i_predictor, -32768, 32767 );
524
525     p_channel->i_sample2 = p_channel->i_sample1;
526     p_channel->i_sample1 = i_predictor;
527
528     p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
529                             p_channel->i_idelta ) / 256;
530     if( p_channel->i_idelta < 16 )
531     {
532         p_channel->i_idelta = 16;
533     }
534     return( i_predictor );
535 }
536
537 static void DecodeAdpcmMs( adec_thread_t *p_adec,
538                            aout_buffer_t *p_aout_buffer)
539 {
540     uint8_t            *p_buffer;
541     adpcm_ms_channel_t channel[2];
542     int i_nibbles;
543     uint16_t           *p_sample;
544     int b_stereo;
545     int i_block_predictor;
546
547     p_buffer = p_adec->p_block;
548     b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
549
550     GetByte( i_block_predictor );
551     CLAMP( i_block_predictor, 0, 6 );
552     channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
553     channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
554
555     if( b_stereo )
556     {
557         GetByte( i_block_predictor );
558         CLAMP( i_block_predictor, 0, 6 );
559         channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
560         channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
561     }
562     GetWord( channel[0].i_idelta );
563     if( b_stereo )
564     {
565         GetWord( channel[1].i_idelta );
566     }
567
568     GetWord( channel[0].i_sample1 );
569     if( b_stereo )
570     {
571         GetWord( channel[1].i_sample1 );
572     }
573
574     GetWord( channel[0].i_sample2 );
575     if( b_stereo )
576     {
577         GetWord( channel[1].i_sample2 );
578     }
579
580     p_sample = (int16_t*)p_aout_buffer->p_buffer;
581
582     if( b_stereo )
583     {
584         *p_sample = channel[0].i_sample2; p_sample++;
585         *p_sample = channel[1].i_sample2; p_sample++;
586         *p_sample = channel[0].i_sample1; p_sample++;
587         *p_sample = channel[1].i_sample1; p_sample++;
588     }
589     else
590     {
591         *p_sample = channel[0].i_sample2; p_sample++;
592         *p_sample = channel[0].i_sample1; p_sample++;
593     }
594
595     for( i_nibbles =  2 *( p_adec->i_block - 7 * p_adec->p_wf->nChannels );
596          i_nibbles > 0; i_nibbles -= 2,p_buffer++ )
597     {
598         *p_sample = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
599         p_sample++;
600
601         *p_sample = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
602                                          (*p_buffer)&0x0f);
603         p_sample++;
604     }
605
606
607 }
608
609 /*
610  * IMA-WAV
611  */
612 typedef struct adpcm_ima_wav_channel_s
613 {
614     int i_predictor;
615     int i_step_index;
616
617 } adpcm_ima_wav_channel_t;
618
619 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
620                                    int i_nibble )
621 {
622     int i_diff;
623
624     i_diff = i_step_table[p_channel->i_step_index] >> 3;
625     if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
626     if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
627     if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
628     if( i_nibble&0x08 )
629         p_channel->i_predictor -= i_diff;
630     else
631         p_channel->i_predictor += i_diff;
632
633     CLAMP( p_channel->i_predictor, -32768, 32767 );
634
635     p_channel->i_step_index += i_index_table[i_nibble];
636
637     CLAMP( p_channel->i_step_index, 0, 88 );
638
639     return( p_channel->i_predictor );
640 }
641
642 static void DecodeAdpcmImaWav( adec_thread_t *p_adec,
643                                aout_buffer_t *p_aout_buffer)
644 {
645     uint8_t                 *p_buffer;
646     adpcm_ima_wav_channel_t channel[2];
647     int                     i_nibbles;
648     uint16_t                *p_sample;
649     int                     b_stereo;
650
651     p_buffer = p_adec->p_block;
652     b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
653
654     GetWord( channel[0].i_predictor );
655     GetByte( channel[0].i_step_index );
656     CLAMP( channel[0].i_step_index, 0, 88 );
657     p_buffer++;
658
659     if( b_stereo )
660     {
661         GetWord( channel[1].i_predictor );
662         GetByte( channel[1].i_step_index );
663         CLAMP( channel[1].i_step_index, 0, 88 );
664         p_buffer++;
665     }
666
667     p_sample = (int16_t*)p_aout_buffer->p_buffer;
668     if( b_stereo )
669     {
670         for( i_nibbles = 2 * (p_adec->i_block - 8);
671              i_nibbles > 0;
672              i_nibbles -= 16 )
673         {
674             int i;
675
676             for( i = 0; i < 4; i++ )
677             {
678                 p_sample[i * 4] =
679                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
680                 p_sample[i * 4 + 2] =
681                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
682             }
683             p_buffer += 4;
684
685             for( i = 0; i < 4; i++ )
686             {
687                 p_sample[i * 4 + 1] =
688                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
689                 p_sample[i * 4 + 3] =
690                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
691             }
692             p_buffer += 4;
693             p_sample += 16;
694
695         }
696
697
698     }
699     else
700     {
701         for( i_nibbles = 2 * (p_adec->i_block - 4);
702              i_nibbles > 0;
703              i_nibbles -= 2, p_buffer++ )
704         {
705             *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
706             p_sample++;
707             *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
708             p_sample++;
709         }
710     }
711 }
712
713 /*
714  * Ima4 in QT file
715  */
716 static void DecodeAdpcmImaQT( adec_thread_t *p_adec,
717                               aout_buffer_t *p_aout_buffer )
718 {
719     uint8_t                 *p_buffer;
720     adpcm_ima_wav_channel_t channel[2];
721     int                     i_nibbles;
722     uint16_t                *p_sample;
723     int                     i_ch;
724     int                     i_step;
725
726     p_buffer = p_adec->p_block;
727     i_step   = p_adec->p_wf->nChannels;
728
729     for( i_ch = 0; i_ch < p_adec->p_wf->nChannels; i_ch++ )
730     {
731         p_sample = ((int16_t*)p_aout_buffer->p_buffer) + i_ch;
732         /* load preambule */
733         channel[i_ch].i_predictor  = (int16_t)((( ( p_buffer[0] << 1 )|(  p_buffer[1] >> 7 ) ))<<7);
734         channel[i_ch].i_step_index = p_buffer[1]&0x7f;
735
736         CLAMP( channel[i_ch].i_step_index, 0, 88 );
737         p_buffer += 2;
738
739         for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
740         {
741             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
742             p_sample += i_step;
743
744             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
745             p_sample += i_step;
746
747             p_buffer++;
748         }
749     }
750 }
751
752 /*
753  * Dk4
754  */
755
756 static void DecodeAdpcmDk4( adec_thread_t *p_adec,
757                                aout_buffer_t *p_aout_buffer)
758 {
759     uint8_t                 *p_buffer;
760     adpcm_ima_wav_channel_t channel[2];
761     int                     i_nibbles;
762     uint16_t                *p_sample;
763     int                     b_stereo;
764
765     p_buffer = p_adec->p_block;
766     b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
767
768     GetWord( channel[0].i_predictor );
769     GetByte( channel[0].i_step_index );
770     CLAMP( channel[0].i_step_index, 0, 88 );
771     p_buffer++;
772
773     if( b_stereo )
774     {
775         GetWord( channel[1].i_predictor );
776         GetByte( channel[1].i_step_index );
777         CLAMP( channel[1].i_step_index, 0, 88 );
778         p_buffer++;
779     }
780
781     p_sample = (int16_t*)p_aout_buffer->p_buffer;
782
783     /* first output predictor */
784     *p_sample++ = channel[0].i_predictor;
785     if( b_stereo )
786     {
787         *p_sample++ = channel[1].i_predictor;
788     }
789
790     for( i_nibbles = 0;
791          i_nibbles < p_adec->i_block - 4 * (b_stereo ? 2:1 );
792          i_nibbles++ )
793     {
794         *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
795                                               (*p_buffer) >> 4);
796         *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
797                                                (*p_buffer)&0x0f);
798
799         p_buffer++;
800     }
801 }
802
803 /*
804  * Dk3
805  */
806
807 static void DecodeAdpcmDk3( adec_thread_t *p_adec,
808                                aout_buffer_t *p_aout_buffer)
809 {
810     uint8_t                 *p_buffer, *p_end;
811     adpcm_ima_wav_channel_t sum;
812     adpcm_ima_wav_channel_t diff;
813     uint16_t                *p_sample;
814     int                     i_diff_value;
815
816     p_buffer = p_adec->p_block;
817     p_end    = p_buffer + p_adec->i_block;
818
819     p_buffer += 10;
820     GetWord( sum.i_predictor );
821     GetWord( diff.i_predictor );
822     GetByte( sum.i_step_index );
823     GetByte( diff.i_step_index );
824
825     p_sample = (int16_t*)p_aout_buffer->p_buffer;
826     i_diff_value = diff.i_predictor;
827     /* we process 6 nibbles at once */
828     //for( i_group = 0; i_group < ( p_adec->i_block -16 ) / 3; i_group++ )
829     while( p_buffer + 1 <= p_end )
830     {
831         /* first 3 nibbles */
832         AdpcmImaWavExpandNibble( &sum,
833                                  (*p_buffer)&0x0f);
834
835         AdpcmImaWavExpandNibble( &diff,
836                                  (*p_buffer) >> 4 );
837
838         i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
839
840         *p_sample++ = sum.i_predictor + i_diff_value;
841         *p_sample++ = sum.i_predictor - i_diff_value;
842
843         p_buffer++;
844
845         AdpcmImaWavExpandNibble( &sum,
846                                  (*p_buffer)&0x0f);
847
848         *p_sample++ = sum.i_predictor + i_diff_value;
849         *p_sample++ = sum.i_predictor - i_diff_value;
850
851         /* now last 3 nibbles */
852         AdpcmImaWavExpandNibble( &sum,
853                                  (*p_buffer)>>4);
854         p_buffer++;
855         if( p_buffer < p_end )
856         {
857             AdpcmImaWavExpandNibble( &diff,
858                                      (*p_buffer)&0x0f );
859
860             i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
861
862             *p_sample++ = sum.i_predictor + i_diff_value;
863             *p_sample++ = sum.i_predictor - i_diff_value;
864
865             AdpcmImaWavExpandNibble( &sum,
866                                      (*p_buffer)>>4);
867             p_buffer++;
868
869             *p_sample++ = sum.i_predictor + i_diff_value;
870             *p_sample++ = sum.i_predictor - i_diff_value;
871         }
872
873     }
874
875 }