]> git.sesse.net Git - vlc/blob - modules/codec/adpcm.c
* all: who has seen that I have just forgot to release any buffers...
[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.15 2003/11/05 01:47: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 <stdlib.h>                                      /* malloc(), free() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/aout.h>
33 #include <vlc/decoder.h>
34 #include <vlc/input.h>
35
36 #include "codecs.h"
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  Open    ( vlc_object_t * );
42
43 vlc_module_begin();
44     set_description( _("ADPCM audio decoder") );
45     set_capability( "decoder", 50 );
46     set_callbacks( Open, NULL );
47 vlc_module_end();
48
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 enum adpcm_codec_e
54 {
55     ADPCM_IMA_QT,
56     ADPCM_IMA_WAV,
57     ADPCM_MS,
58     ADPCM_DK3,
59     ADPCM_DK4
60 };
61
62 struct decoder_sys_t
63 {
64     WAVEFORMATEX       *p_wf;
65     enum adpcm_codec_e codec;
66
67     int                 i_block;
68     int                 i_samplesperblock;
69
70     /* audio output */
71     aout_instance_t *   p_aout;       /* opaque */
72     aout_input_t *      p_aout_input; /* opaque */
73     audio_sample_format_t output_format;
74
75     audio_date_t        date;
76 };
77
78 static int Init  ( decoder_t * );
79 static int Decode( decoder_t *, block_t * );
80 static int End   ( decoder_t * );
81
82
83
84 static void DecodeAdpcmMs       ( decoder_sys_t *, int16_t *, uint8_t * );
85 static void DecodeAdpcmImaWav   ( decoder_sys_t *, int16_t *, uint8_t * );
86 static void DecodeAdpcmImaQT    ( decoder_sys_t *, int16_t *, uint8_t * );
87 static void DecodeAdpcmDk4      ( decoder_sys_t *, int16_t *, uint8_t * );
88 static void DecodeAdpcmDk3      ( decoder_sys_t *, int16_t *, uint8_t * );
89
90 static int pi_channels_maps[6] =
91 {
92     0,
93     AOUT_CHAN_CENTER,
94     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
95     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
96     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
97     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
98      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
99 };
100
101 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
102 static int i_index_table[16] =
103 {
104     -1, -1, -1, -1, 2, 4, 6, 8,
105     -1, -1, -1, -1, 2, 4, 6, 8
106 };
107
108 static int i_step_table[89] =
109 {
110     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
111     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
112     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
113     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
114     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
115     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
116     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
117     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
118     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
119 };
120
121 static int i_adaptation_table[16] =
122 {
123     230, 230, 230, 230, 307, 409, 512, 614,
124     768, 614, 512, 409, 307, 230, 230, 230
125 };
126
127 static int i_adaptation_coeff1[7] =
128 {
129     256, 512, 0, 192, 240, 460, 392
130 };
131
132 static int i_adaptation_coeff2[7] =
133 {
134     0, -256, 0, 64, 0, -208, -232
135 };
136
137
138 /*****************************************************************************
139  * OpenDecoder: probe the decoder and return score
140  *****************************************************************************
141  * Tries to launch a decoder and return score so that the interface is able
142  * to choose.
143  *****************************************************************************/
144 static int Open( vlc_object_t *p_this )
145 {
146     decoder_t *p_dec = (decoder_t*)p_this;
147
148     switch( p_dec->p_fifo->i_fourcc )
149     {
150         case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
151         case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
152         case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
153         case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
154         case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
155
156             p_dec->pf_init   = Init;
157             p_dec->pf_decode = Decode;
158             p_dec->pf_end    = End;
159
160             p_dec->p_sys     = malloc( sizeof( decoder_sys_t ) );
161             return VLC_SUCCESS;
162
163         default:
164             return VLC_EGENERIC;
165     }
166 }
167
168 /*****************************************************************************
169  * Init:
170  *****************************************************************************/
171 static int Init  ( decoder_t *p_dec )
172 {
173     decoder_sys_t *p_sys = p_dec->p_sys;
174
175     WAVEFORMATEX *p_wf;
176     if( ( p_wf = (WAVEFORMATEX*)p_dec->p_fifo->p_waveformatex ) == NULL )
177     {
178         msg_Err( p_dec, "unknown raw format" );
179         return VLC_EGENERIC;
180     }
181
182     if( p_wf->nChannels < 1 || p_wf->nChannels > 2 )
183     {
184         msg_Err( p_dec, "bad channels count(1-2)" );
185         return VLC_EGENERIC;
186     }
187     if( p_wf->nSamplesPerSec <= 0 )
188     {
189         msg_Err( p_dec, "bad samplerate" );
190         return VLC_EGENERIC;
191     }
192
193     p_sys->p_wf = p_wf;
194     switch( p_dec->p_fifo->i_fourcc )
195     {
196         case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
197             p_sys->codec = ADPCM_IMA_QT;
198             break;
199         case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
200             p_sys->codec = ADPCM_IMA_WAV;
201             break;
202         case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
203             p_sys->codec = ADPCM_MS;
204             break;
205         case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
206             p_sys->codec = ADPCM_DK4;
207             break;
208         case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
209             p_sys->codec = ADPCM_DK3;
210             break;
211     }
212
213     if( ( p_sys->i_block = p_wf->nBlockAlign ) <= 0 )
214     {
215         p_sys->i_block = p_sys->codec==ADPCM_IMA_QT ? 34*p_wf->nChannels:1024;
216         msg_Warn( p_dec, "block size undefined, -> using %d", p_sys->i_block );
217     }
218
219     /* calculate samples per block */
220     switch( p_sys->codec )
221     {
222         case ADPCM_IMA_QT:
223             p_sys->i_samplesperblock = 64;
224             break;
225         case ADPCM_IMA_WAV:
226             p_sys->i_samplesperblock =
227                  2 * ( p_sys->i_block - 4 * p_wf->nChannels )/ p_wf->nChannels;
228                  break;
229         case ADPCM_MS:
230             p_sys->i_samplesperblock =
231                 2 * (p_sys->i_block - 7 * p_wf->nChannels)/p_wf->nChannels + 2;
232             break;
233         case ADPCM_DK4:
234             p_sys->i_samplesperblock =
235                2 * (p_sys->i_block - 4 * p_wf->nChannels)/p_wf->nChannels + 1;
236             break;
237         case ADPCM_DK3:
238             p_wf->nChannels = 2;
239             p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
240             break;
241     }
242     msg_Dbg( p_dec,
243              "format: samplerate:%dHz channels:%d bits/sample:%d blockalign:%d samplesperblock %d",
244              p_wf->nSamplesPerSec, p_wf->nChannels,
245              p_wf->wBitsPerSample, p_wf->nBlockAlign,
246              p_sys->i_samplesperblock );
247
248     p_sys->output_format.i_format = AOUT_FMT_S16_NE;
249     p_sys->output_format.i_rate = p_wf->nSamplesPerSec;
250     p_sys->output_format.i_physical_channels =
251     p_sys->output_format.i_original_channels =
252             pi_channels_maps[p_wf->nChannels];
253
254     p_sys->p_aout = NULL;
255     p_sys->p_aout_input = aout_DecNew( p_dec,
256                                        &p_sys->p_aout, &p_sys->output_format);
257     if( p_sys->p_aout_input == NULL )
258     {
259         msg_Err( p_dec, "cannot create aout" );
260         return VLC_EGENERIC;
261     }
262
263     aout_DateInit( &p_sys->date, p_sys->output_format.i_rate );
264     aout_DateSet( &p_sys->date, 0 );
265
266     return VLC_SUCCESS;
267 }
268
269 /*****************************************************************************
270  * Decode:
271  *****************************************************************************/
272 static int Decode( decoder_t *p_dec, block_t *p_block )
273 {
274     decoder_sys_t *p_sys  = p_dec->p_sys;
275     mtime_t        i_pts  = p_block->i_pts;
276     uint8_t       *p_data = p_block->p_buffer;
277     int            i_data = p_block->i_buffer;
278
279     while( i_data >= p_sys->i_block )
280     {
281         aout_buffer_t *out;
282
283         if( i_pts != 0 && i_pts != aout_DateGet( &p_sys->date ) )
284         {
285             aout_DateSet( &p_sys->date, i_pts );
286         }
287         else if( !aout_DateGet( &p_sys->date ) )
288         {
289             block_Release( p_block );
290             return VLC_SUCCESS;
291         }
292         i_pts = 0;
293
294         out = aout_DecNewBuffer( p_sys->p_aout,
295                                  p_sys->p_aout_input,
296                                  p_sys->i_samplesperblock );
297         if( out == NULL )
298         {
299             msg_Err( p_dec, "cannot get aout buffer" );
300             block_Release( p_block );
301             return VLC_EGENERIC;
302         }
303         out->start_date = aout_DateGet( &p_sys->date );
304         out->end_date   = aout_DateIncrement( &p_sys->date,
305                                               p_sys->i_samplesperblock );
306
307         switch( p_sys->codec )
308         {
309             case ADPCM_IMA_QT:
310                 DecodeAdpcmImaQT( p_sys, (int16_t*)out->p_buffer, p_data );
311                 break;
312             case ADPCM_IMA_WAV:
313                 DecodeAdpcmImaWav( p_sys, (int16_t*)out->p_buffer, p_data );
314                 break;
315             case ADPCM_MS:
316                 DecodeAdpcmMs( p_sys, (int16_t*)out->p_buffer, p_data );
317                 break;
318             case ADPCM_DK4:
319                 DecodeAdpcmDk4( p_sys, (int16_t*)out->p_buffer, p_data );
320                 break;
321             case ADPCM_DK3:
322                 DecodeAdpcmDk3( p_sys, (int16_t*)out->p_buffer, p_data );
323                 break;
324             default:
325                 break;
326         }
327         aout_DecPlay( p_sys->p_aout, p_sys->p_aout_input, out );
328
329         p_data += p_sys->i_block;
330         i_data -= p_sys->i_block;
331     }
332
333     block_Release( p_block );
334     return VLC_SUCCESS;
335 }
336
337 /*****************************************************************************
338  * End:
339  *****************************************************************************/
340 static int End   ( decoder_t *p_dec )
341 {
342     decoder_sys_t *p_sys = p_dec->p_sys;
343
344     if( p_sys->p_aout_input )
345     {
346         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
347     }
348     free( p_sys );
349
350     return VLC_SUCCESS;
351 }
352
353 #define CLAMP( v, min, max ) \
354     if( (v) < (min) ) (v) = (min); \
355     if( (v) > (max) ) (v) = (max)
356
357 #define GetByte( v ) \
358     (v) = *p_buffer; p_buffer++;
359
360 #define GetWord( v ) \
361     (v) = *p_buffer; p_buffer++; \
362     (v) |= ( *p_buffer ) << 8; p_buffer++; \
363     if( (v)&0x8000 ) (v) -= 0x010000;
364
365 /*
366  * MS
367  */
368 typedef struct adpcm_ms_channel_s
369 {
370     int i_idelta;
371     int i_sample1, i_sample2;
372     int i_coeff1, i_coeff2;
373
374 } adpcm_ms_channel_t;
375
376
377 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
378                                int i_nibble )
379 {
380     int i_predictor;
381     int i_snibble;
382     /* expand sign */
383
384     i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
385
386     i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
387                     p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
388                   i_snibble * p_channel->i_idelta;
389
390     CLAMP( i_predictor, -32768, 32767 );
391
392     p_channel->i_sample2 = p_channel->i_sample1;
393     p_channel->i_sample1 = i_predictor;
394
395     p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
396                             p_channel->i_idelta ) / 256;
397     if( p_channel->i_idelta < 16 )
398     {
399         p_channel->i_idelta = 16;
400     }
401     return( i_predictor );
402 }
403
404 static void DecodeAdpcmMs( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
405 {
406     adpcm_ms_channel_t channel[2];
407     int i_nibbles;
408     int b_stereo;
409     int i_block_predictor;
410
411     b_stereo = p_sys->p_wf->nChannels == 2 ? 1 : 0;
412
413     GetByte( i_block_predictor );
414     CLAMP( i_block_predictor, 0, 6 );
415     channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
416     channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
417
418     if( b_stereo )
419     {
420         GetByte( i_block_predictor );
421         CLAMP( i_block_predictor, 0, 6 );
422         channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
423         channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
424     }
425     GetWord( channel[0].i_idelta );
426     if( b_stereo )
427     {
428         GetWord( channel[1].i_idelta );
429     }
430
431     GetWord( channel[0].i_sample1 );
432     if( b_stereo )
433     {
434         GetWord( channel[1].i_sample1 );
435     }
436
437     GetWord( channel[0].i_sample2 );
438     if( b_stereo )
439     {
440         GetWord( channel[1].i_sample2 );
441     }
442
443     if( b_stereo )
444     {
445         *p_sample++ = channel[0].i_sample2;
446         *p_sample++ = channel[1].i_sample2;
447         *p_sample++ = channel[0].i_sample1;
448         *p_sample++ = channel[1].i_sample1;
449     }
450     else
451     {
452         *p_sample++ = channel[0].i_sample2;
453         *p_sample++ = channel[0].i_sample1;
454     }
455
456     for( i_nibbles =  2 *( p_sys->i_block - 7 * p_sys->p_wf->nChannels );
457          i_nibbles > 0; i_nibbles -= 2,p_buffer++ )
458     {
459         *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
460         *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
461                                            (*p_buffer)&0x0f);
462     }
463 }
464
465 /*
466  * IMA-WAV
467  */
468 typedef struct adpcm_ima_wav_channel_s
469 {
470     int i_predictor;
471     int i_step_index;
472
473 } adpcm_ima_wav_channel_t;
474
475 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
476                                    int i_nibble )
477 {
478     int i_diff;
479
480     i_diff = i_step_table[p_channel->i_step_index] >> 3;
481     if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
482     if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
483     if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
484     if( i_nibble&0x08 )
485         p_channel->i_predictor -= i_diff;
486     else
487         p_channel->i_predictor += i_diff;
488
489     CLAMP( p_channel->i_predictor, -32768, 32767 );
490
491     p_channel->i_step_index += i_index_table[i_nibble];
492
493     CLAMP( p_channel->i_step_index, 0, 88 );
494
495     return( p_channel->i_predictor );
496 }
497
498 static void DecodeAdpcmImaWav( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
499 {
500     adpcm_ima_wav_channel_t channel[2];
501     int                     i_nibbles;
502     int                     b_stereo;
503
504     b_stereo = p_sys->p_wf->nChannels == 2 ? 1 : 0;
505
506     GetWord( channel[0].i_predictor );
507     GetByte( channel[0].i_step_index );
508     CLAMP( channel[0].i_step_index, 0, 88 );
509     p_buffer++;
510
511     if( b_stereo )
512     {
513         GetWord( channel[1].i_predictor );
514         GetByte( channel[1].i_step_index );
515         CLAMP( channel[1].i_step_index, 0, 88 );
516         p_buffer++;
517     }
518
519     if( b_stereo )
520     {
521         for( i_nibbles = 2 * (p_sys->i_block - 8);
522              i_nibbles > 0;
523              i_nibbles -= 16 )
524         {
525             int i;
526
527             for( i = 0; i < 4; i++ )
528             {
529                 p_sample[i * 4] =
530                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
531                 p_sample[i * 4 + 2] =
532                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
533             }
534             p_buffer += 4;
535
536             for( i = 0; i < 4; i++ )
537             {
538                 p_sample[i * 4 + 1] =
539                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
540                 p_sample[i * 4 + 3] =
541                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
542             }
543             p_buffer += 4;
544             p_sample += 16;
545
546         }
547
548
549     }
550     else
551     {
552         for( i_nibbles = 2 * (p_sys->i_block - 4);
553              i_nibbles > 0;
554              i_nibbles -= 2, p_buffer++ )
555         {
556             *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
557             *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
558         }
559     }
560 }
561
562 /*
563  * Ima4 in QT file
564  */
565 static void DecodeAdpcmImaQT( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
566 {
567     adpcm_ima_wav_channel_t channel[2];
568     int                     i_nibbles;
569     int                     i_ch;
570     int                     i_step;
571
572     i_step   = p_sys->p_wf->nChannels;
573
574     for( i_ch = 0; i_ch < p_sys->p_wf->nChannels; i_ch++ )
575     {
576         /* load preambule */
577         channel[i_ch].i_predictor  = (int16_t)((( ( p_buffer[0] << 1 )|(  p_buffer[1] >> 7 ) ))<<7);
578         channel[i_ch].i_step_index = p_buffer[1]&0x7f;
579
580         CLAMP( channel[i_ch].i_step_index, 0, 88 );
581         p_buffer += 2;
582
583         for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
584         {
585             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
586             p_sample += i_step;
587
588             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
589             p_sample += i_step;
590
591             p_buffer++;
592         }
593
594         /* Next channel */
595         p_sample += 1 - 64 * i_step;
596     }
597 }
598
599 /*
600  * Dk4
601  */
602
603 static void DecodeAdpcmDk4( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
604 {
605     adpcm_ima_wav_channel_t channel[2];
606     int                     i_nibbles;
607     int                     b_stereo;
608
609     b_stereo = p_sys->p_wf->nChannels == 2 ? 1 : 0;
610
611     GetWord( channel[0].i_predictor );
612     GetByte( channel[0].i_step_index );
613     CLAMP( channel[0].i_step_index, 0, 88 );
614     p_buffer++;
615
616     if( b_stereo )
617     {
618         GetWord( channel[1].i_predictor );
619         GetByte( channel[1].i_step_index );
620         CLAMP( channel[1].i_step_index, 0, 88 );
621         p_buffer++;
622     }
623
624     /* first output predictor */
625     *p_sample++ = channel[0].i_predictor;
626     if( b_stereo )
627     {
628         *p_sample++ = channel[1].i_predictor;
629     }
630
631     for( i_nibbles = 0;
632          i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
633          i_nibbles++ )
634     {
635         *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
636                                               (*p_buffer) >> 4);
637         *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
638                                                (*p_buffer)&0x0f);
639
640         p_buffer++;
641     }
642 }
643
644 /*
645  * Dk3
646  */
647 static void DecodeAdpcmDk3( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
648 {
649     uint8_t                 *p_end = &p_buffer[p_sys->i_block];
650     adpcm_ima_wav_channel_t sum;
651     adpcm_ima_wav_channel_t diff;
652     int                     i_diff_value;
653
654     p_buffer += 10;
655
656     GetWord( sum.i_predictor );
657     GetWord( diff.i_predictor );
658     GetByte( sum.i_step_index );
659     GetByte( diff.i_step_index );
660
661     i_diff_value = diff.i_predictor;
662     /* we process 6 nibbles at once */
663     while( p_buffer + 1 <= p_end )
664     {
665         /* first 3 nibbles */
666         AdpcmImaWavExpandNibble( &sum,
667                                  (*p_buffer)&0x0f);
668
669         AdpcmImaWavExpandNibble( &diff,
670                                  (*p_buffer) >> 4 );
671
672         i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
673
674         *p_sample++ = sum.i_predictor + i_diff_value;
675         *p_sample++ = sum.i_predictor - i_diff_value;
676
677         p_buffer++;
678
679         AdpcmImaWavExpandNibble( &sum,
680                                  (*p_buffer)&0x0f);
681
682         *p_sample++ = sum.i_predictor + i_diff_value;
683         *p_sample++ = sum.i_predictor - i_diff_value;
684
685         /* now last 3 nibbles */
686         AdpcmImaWavExpandNibble( &sum,
687                                  (*p_buffer)>>4);
688         p_buffer++;
689         if( p_buffer < p_end )
690         {
691             AdpcmImaWavExpandNibble( &diff,
692                                      (*p_buffer)&0x0f );
693
694             i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
695
696             *p_sample++ = sum.i_predictor + i_diff_value;
697             *p_sample++ = sum.i_predictor - i_diff_value;
698
699             AdpcmImaWavExpandNibble( &sum,
700                                      (*p_buffer)>>4);
701             p_buffer++;
702
703             *p_sample++ = sum.i_predictor + i_diff_value;
704             *p_sample++ = sum.i_predictor - i_diff_value;
705         }
706     }
707 }
708