3 * Copyright (c) 2001-2003 The ffmpeg Project
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * First version by Francois Revol (revol@free.fr)
25 * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
26 * by Mike Melanson (melanson@pcisys.net)
27 * CD-ROM XA ADPCM codec by BERO
29 * Features and limitations:
31 * Reference documents:
32 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
33 * http://www.geocities.com/SiliconValley/8682/aud3.txt
34 * http://openquicktime.sourceforge.net/plugins.htm
35 * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
36 * http://www.cs.ucla.edu/~leec/mediabench/applications.html
37 * SoX source code http://home.sprynet.com/~cbagwell/sox.html
40 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
41 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
42 * readstr http://www.geocities.co.jp/Playtown/2004/
47 #define CLAMP_TO_SHORT(value) \
50 else if (value < -32768) \
53 /* step_table[] and index_table[] are from the ADPCM reference source */
54 /* This is the index table: */
55 static const int index_table
[16] = {
56 -1, -1, -1, -1, 2, 4, 6, 8,
57 -1, -1, -1, -1, 2, 4, 6, 8,
61 * This is the step table. Note that many programs use slight deviations from
62 * this table, but such deviations are negligible:
64 static const int step_table
[89] = {
65 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
66 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
67 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
68 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
69 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
70 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
71 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
72 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
73 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
76 /* These are for MS-ADPCM */
77 /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
78 static const int AdaptationTable
[] = {
79 230, 230, 230, 230, 307, 409, 512, 614,
80 768, 614, 512, 409, 307, 230, 230, 230
83 static const int AdaptCoeff1
[] = {
84 256, 512, 0, 192, 240, 460, 392
87 static const int AdaptCoeff2
[] = {
88 0, -256, 0, 64, 0, -208, -232
91 /* These are for CD-ROM XA ADPCM */
92 const static int xa_adpcm_table
[5][2] = {
102 typedef struct ADPCMChannelStatus
{
104 short int step_index
;
115 } ADPCMChannelStatus
;
117 typedef struct ADPCMContext
{
118 int channel
; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
119 ADPCMChannelStatus status
[2];
120 short sample_buffer
[32]; /* hold left samples while waiting for right samples */
123 /* XXX: implement encoding */
125 #ifdef CONFIG_ENCODERS
126 static int adpcm_encode_init(AVCodecContext
*avctx
)
128 if (avctx
->channels
> 2)
129 return -1; /* only stereo or mono =) */
130 switch(avctx
->codec
->id
) {
131 case CODEC_ID_ADPCM_IMA_QT
:
132 fprintf(stderr
, "ADPCM: codec admcp_ima_qt unsupported for encoding !\n");
133 avctx
->frame_size
= 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
136 case CODEC_ID_ADPCM_IMA_WAV
:
137 avctx
->frame_size
= (BLKSIZE
- 4 * avctx
->channels
) * 8 / (4 * avctx
->channels
) + 1; /* each 16 bits sample gives one nibble */
138 /* and we have 4 bytes per channel overhead */
139 avctx
->block_align
= BLKSIZE
;
140 /* seems frame_size isn't taken into account... have to buffer the samples :-( */
142 case CODEC_ID_ADPCM_MS
:
143 fprintf(stderr
, "ADPCM: codec admcp_ms unsupported for encoding !\n");
151 avctx
->coded_frame
= avcodec_alloc_frame();
152 avctx
->coded_frame
->key_frame
= 1;
157 static int adpcm_encode_close(AVCodecContext
*avctx
)
159 av_freep(&avctx
->coded_frame
);
165 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus
*c
, short sample
)
168 unsigned char nibble
;
170 int sign
= 0; /* sign bit of the nibble (MSB) */
171 int delta
, predicted_delta
;
173 delta
= sample
- c
->prev_sample
;
180 step_index
= c
->step_index
;
182 /* nibble = 4 * delta / step_table[step_index]; */
183 nibble
= (delta
<< 2) / step_table
[step_index
];
188 step_index
+= index_table
[nibble
];
194 /* what the decoder will find */
195 predicted_delta
= ((step_table
[step_index
] * nibble
) / 4) + (step_table
[step_index
] / 8);
198 c
->prev_sample
-= predicted_delta
;
200 c
->prev_sample
+= predicted_delta
;
202 CLAMP_TO_SHORT(c
->prev_sample
);
205 nibble
+= sign
<< 3; /* sign * 8 */
208 c
->step_index
= step_index
;
213 static int adpcm_encode_frame(AVCodecContext
*avctx
,
214 unsigned char *frame
, int buf_size
, void *data
)
219 ADPCMContext
*c
= avctx
->priv_data
;
222 samples
= (short *)data
;
223 /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
225 switch(avctx
->codec
->id
) {
226 case CODEC_ID_ADPCM_IMA_QT
: /* XXX: can't test until we get .mov writer */
228 case CODEC_ID_ADPCM_IMA_WAV
:
229 n
= avctx
->frame_size
/ 8;
230 c
->status
[0].prev_sample
= (signed short)samples
[0]; /* XXX */
231 /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
232 *dst
++ = (c
->status
[0].prev_sample
) & 0xFF; /* little endian */
233 *dst
++ = (c
->status
[0].prev_sample
>> 8) & 0xFF;
234 *dst
++ = (unsigned char)c
->status
[0].step_index
;
235 *dst
++ = 0; /* unknown */
237 if (avctx
->channels
== 2) {
238 c
->status
[1].prev_sample
= (signed short)samples
[0];
239 /* c->status[1].step_index = 0; */
240 *dst
++ = (c
->status
[1].prev_sample
) & 0xFF;
241 *dst
++ = (c
->status
[1].prev_sample
>> 8) & 0xFF;
242 *dst
++ = (unsigned char)c
->status
[1].step_index
;
247 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
249 *dst
= adpcm_ima_compress_sample(&c
->status
[0], samples
[0]) & 0x0F;
250 *dst
|= (adpcm_ima_compress_sample(&c
->status
[0], samples
[avctx
->channels
]) << 4) & 0xF0;
252 *dst
= adpcm_ima_compress_sample(&c
->status
[0], samples
[avctx
->channels
* 2]) & 0x0F;
253 *dst
|= (adpcm_ima_compress_sample(&c
->status
[0], samples
[avctx
->channels
* 3]) << 4) & 0xF0;
255 *dst
= adpcm_ima_compress_sample(&c
->status
[0], samples
[avctx
->channels
* 4]) & 0x0F;
256 *dst
|= (adpcm_ima_compress_sample(&c
->status
[0], samples
[avctx
->channels
* 5]) << 4) & 0xF0;
258 *dst
= adpcm_ima_compress_sample(&c
->status
[0], samples
[avctx
->channels
* 6]) & 0x0F;
259 *dst
|= (adpcm_ima_compress_sample(&c
->status
[0], samples
[avctx
->channels
* 7]) << 4) & 0xF0;
262 if (avctx
->channels
== 2) {
263 *dst
= adpcm_ima_compress_sample(&c
->status
[1], samples
[1]);
264 *dst
|= adpcm_ima_compress_sample(&c
->status
[1], samples
[3]) << 4;
266 *dst
= adpcm_ima_compress_sample(&c
->status
[1], samples
[5]);
267 *dst
|= adpcm_ima_compress_sample(&c
->status
[1], samples
[7]) << 4;
269 *dst
= adpcm_ima_compress_sample(&c
->status
[1], samples
[9]);
270 *dst
|= adpcm_ima_compress_sample(&c
->status
[1], samples
[11]) << 4;
272 *dst
= adpcm_ima_compress_sample(&c
->status
[1], samples
[13]);
273 *dst
|= adpcm_ima_compress_sample(&c
->status
[1], samples
[15]) << 4;
276 samples
+= 8 * avctx
->channels
;
284 #endif //CONFIG_ENCODERS
286 static int adpcm_decode_init(AVCodecContext
* avctx
)
288 ADPCMContext
*c
= avctx
->priv_data
;
291 c
->status
[0].predictor
= c
->status
[1].predictor
= 0;
292 c
->status
[0].step_index
= c
->status
[1].step_index
= 0;
293 c
->status
[0].step
= c
->status
[1].step
= 0;
295 switch(avctx
->codec
->id
) {
302 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus
*c
, char nibble
)
306 int sign
, delta
, diff
, step
;
308 step
= step_table
[c
->step_index
];
309 step_index
= c
->step_index
+ index_table
[(unsigned)nibble
];
310 if (step_index
< 0) step_index
= 0;
311 else if (step_index
> 88) step_index
= 88;
315 /* perform direct multiplication instead of series of jumps proposed by
316 * the reference ADPCM implementation since modern CPUs can do the mults
318 diff
= ((2 * delta
+ 1) * step
) >> 3;
319 predictor
= c
->predictor
;
320 if (sign
) predictor
-= diff
;
321 else predictor
+= diff
;
323 CLAMP_TO_SHORT(predictor
);
324 c
->predictor
= predictor
;
325 c
->step_index
= step_index
;
327 return (short)predictor
;
330 static inline short adpcm_4xa_expand_nibble(ADPCMChannelStatus
*c
, char nibble
)
334 int sign
, delta
, diff
, step
;
336 step
= step_table
[c
->step_index
];
337 step_index
= c
->step_index
+ index_table
[(unsigned)nibble
];
338 if (step_index
< 0) step_index
= 0;
339 else if (step_index
> 88) step_index
= 88;
344 diff
= (delta
*step
+ (step
>>1))>>3; // difference to code above
346 predictor
= c
->predictor
;
347 if (sign
) predictor
-= diff
;
348 else predictor
+= diff
;
350 CLAMP_TO_SHORT(predictor
);
351 c
->predictor
= predictor
;
352 c
->step_index
= step_index
;
354 return (short)predictor
;
357 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus
*c
, char nibble
)
361 predictor
= (((c
->sample1
) * (c
->coeff1
)) + ((c
->sample2
) * (c
->coeff2
))) / 256;
362 predictor
+= (signed)((nibble
& 0x08)?
(nibble
- 0x10):(nibble
)) * c
->idelta
;
363 CLAMP_TO_SHORT(predictor
);
365 c
->sample2
= c
->sample1
;
366 c
->sample1
= predictor
;
367 c
->idelta
= (AdaptationTable
[(int)nibble
] * c
->idelta
) / 256;
368 if (c
->idelta
< 16) c
->idelta
= 16;
370 return (short)predictor
;
373 static void xa_decode(short *out
, const unsigned char *in
,
374 ADPCMChannelStatus
*left
, ADPCMChannelStatus
*right
, int inc
)
377 int shift
,filter
,f0
,f1
;
383 shift
= 12 - (in
[4+i
*2] & 15);
384 filter
= in
[4+i
*2] >> 4;
385 f0
= xa_adpcm_table
[filter
][0];
386 f1
= xa_adpcm_table
[filter
][1];
394 t
= (signed char)(d
<<4)>>4;
395 s
= ( t
<<shift
) + ((s_1
*f0
+ s_2
*f1
+32)>>6);
403 if (inc
==2) { /* stereo */
406 s_1
= right
->sample1
;
407 s_2
= right
->sample2
;
408 out
= out
+ 1 - 28*2;
411 shift
= 12 - (in
[5+i
*2] & 15);
412 filter
= in
[5+i
*2] >> 4;
414 f0
= xa_adpcm_table
[filter
][0];
415 f1
= xa_adpcm_table
[filter
][1];
420 t
= (signed char)d
>> 4;
421 s
= ( t
<<shift
) + ((s_1
*f0
+ s_2
*f1
+32)>>6);
429 if (inc
==2) { /* stereo */
430 right
->sample1
= s_1
;
431 right
->sample2
= s_2
;
441 /* DK3 ADPCM support macro */
442 #define DK3_GET_NEXT_NIBBLE() \
443 if (decode_top_nibble_next) \
445 nibble = (last_byte >> 4) & 0x0F; \
446 decode_top_nibble_next = 0; \
450 last_byte = *src++; \
451 if (src >= buf + buf_size) break; \
452 nibble = last_byte & 0x0F; \
453 decode_top_nibble_next = 1; \
456 static int adpcm_decode_frame(AVCodecContext
*avctx
,
457 void *data
, int *data_size
,
458 uint8_t *buf
, int buf_size
)
460 ADPCMContext
*c
= avctx
->priv_data
;
461 ADPCMChannelStatus
*cs
;
462 int n
, m
, channel
, i
;
463 int block_predictor
[2];
468 /* DK3 ADPCM accounting variables */
469 unsigned char last_byte
= 0;
470 unsigned char nibble
;
471 int decode_top_nibble_next
= 0;
477 st
= avctx
->channels
== 2;
479 switch(avctx
->codec
->id
) {
480 case CODEC_ID_ADPCM_IMA_QT
:
481 n
= (buf_size
- 2);/* >> 2*avctx->channels;*/
482 channel
= c
->channel
;
483 cs
= &(c
->status
[channel
]);
484 /* (pppppp) (piiiiiii) */
486 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
487 cs
->predictor
= (*src
++) << 8;
488 cs
->predictor
|= (*src
& 0x80);
489 cs
->predictor
&= 0xFF80;
492 if(cs
->predictor
& 0x8000)
493 cs
->predictor
-= 0x10000;
495 CLAMP_TO_SHORT(cs
->predictor
);
497 cs
->step_index
= (*src
++) & 0x7F;
499 if (cs
->step_index
> 88) fprintf(stderr
, "ERROR: step_index = %i\n", cs
->step_index
);
500 if (cs
->step_index
> 88) cs
->step_index
= 88;
502 cs
->step
= step_table
[cs
->step_index
];
507 *samples
++ = cs
->predictor
;
510 for(m
=32; n
>0 && m
>0; n
--, m
--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
511 *samples
= adpcm_ima_expand_nibble(cs
, src
[0] & 0x0F);
512 samples
+= avctx
->channels
;
513 *samples
= adpcm_ima_expand_nibble(cs
, (src
[0] >> 4) & 0x0F);
514 samples
+= avctx
->channels
;
518 if(st
) { /* handle stereo interlacing */
519 c
->channel
= (channel
+ 1) % 2; /* we get one packet for left, then one for right data */
520 if(channel
== 0) { /* wait for the other packet before outputing anything */
526 case CODEC_ID_ADPCM_IMA_WAV
:
527 if (buf_size
> BLKSIZE
) {
528 if (avctx
->block_align
!= 0)
529 buf_size
= avctx
->block_align
;
533 // XXX: do as per-channel loop
534 cs
= &(c
->status
[0]);
535 cs
->predictor
= (*src
++) & 0x0FF;
536 cs
->predictor
|= ((*src
++) << 8) & 0x0FF00;
537 if(cs
->predictor
& 0x8000)
538 cs
->predictor
-= 0x10000;
539 CLAMP_TO_SHORT(cs
->predictor
);
541 // XXX: is this correct ??: *samples++ = cs->predictor;
543 cs
->step_index
= *src
++;
544 if (cs
->step_index
< 0) cs
->step_index
= 0;
545 if (cs
->step_index
> 88) cs
->step_index
= 88;
546 if (*src
++) fprintf(stderr
, "unused byte should be null !!\n"); /* unused */
549 cs
= &(c
->status
[1]);
550 cs
->predictor
= (*src
++) & 0x0FF;
551 cs
->predictor
|= ((*src
++) << 8) & 0x0FF00;
552 if(cs
->predictor
& 0x8000)
553 cs
->predictor
-= 0x10000;
554 CLAMP_TO_SHORT(cs
->predictor
);
556 // XXX: is this correct ??: *samples++ = cs->predictor;
558 cs
->step_index
= *src
++;
559 if (cs
->step_index
< 0) cs
->step_index
= 0;
560 if (cs
->step_index
> 88) cs
->step_index
= 88;
561 src
++; /* if != 0 -> out-of-sync */
564 for(m
=4; src
< (buf
+ buf_size
);) {
565 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0], src
[0] & 0x0F);
567 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[1], src
[4] & 0x0F);
568 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0], (src
[0] >> 4) & 0x0F);
570 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[1], (src
[4] >> 4) & 0x0F);
579 case CODEC_ID_ADPCM_4XM
:
580 cs
= &(c
->status
[0]);
581 c
->status
[0].predictor
= (int16_t)(src
[0] + (src
[1]<<8)); src
+=2;
583 c
->status
[1].predictor
= (int16_t)(src
[0] + (src
[1]<<8)); src
+=2;
585 c
->status
[0].step_index
= (int16_t)(src
[0] + (src
[1]<<8)); src
+=2;
587 c
->status
[1].step_index
= (int16_t)(src
[0] + (src
[1]<<8)); src
+=2;
589 // if (cs->step_index < 0) cs->step_index = 0;
590 // if (cs->step_index > 88) cs->step_index = 88;
592 m
= (buf_size
- (src
- buf
))>>st
;
593 //printf("%d %d %d %d\n", st, m, c->status[0].predictor, c->status[0].step_index);
594 //FIXME / XXX decode chanels individual & interleave samples
596 *samples
++ = adpcm_4xa_expand_nibble(&c
->status
[0], src
[i
] & 0x0F);
598 *samples
++ = adpcm_4xa_expand_nibble(&c
->status
[1], src
[i
+m
] & 0x0F);
599 *samples
++ = adpcm_4xa_expand_nibble(&c
->status
[0], src
[i
] >> 4);
601 *samples
++ = adpcm_4xa_expand_nibble(&c
->status
[1], src
[i
+m
] >> 4);
607 case CODEC_ID_ADPCM_MS
:
609 if (buf_size
> BLKSIZE
) {
610 if (avctx
->block_align
!= 0)
611 buf_size
= avctx
->block_align
;
615 n
= buf_size
- 7 * avctx
->channels
;
618 block_predictor
[0] = (*src
++); /* should be bound */
619 block_predictor
[0] = (block_predictor
[0] < 0)?
(0):((block_predictor
[0] > 7)?
(7):(block_predictor
[0]));
620 block_predictor
[1] = 0;
622 block_predictor
[1] = (*src
++);
623 block_predictor
[1] = (block_predictor
[1] < 0)?
(0):((block_predictor
[1] > 7)?
(7):(block_predictor
[1]));
624 c
->status
[0].idelta
= ((*src
& 0xFF) | ((src
[1] << 8) & 0xFF00));
625 if (c
->status
[0].idelta
& 0x08000)
626 c
->status
[0].idelta
-= 0x10000;
629 c
->status
[1].idelta
= ((*src
& 0xFF) | ((src
[1] << 8) & 0xFF00));
630 if (st
&& c
->status
[1].idelta
& 0x08000)
631 c
->status
[1].idelta
|= 0xFFFF0000;
634 c
->status
[0].coeff1
= AdaptCoeff1
[block_predictor
[0]];
635 c
->status
[0].coeff2
= AdaptCoeff2
[block_predictor
[0]];
636 c
->status
[1].coeff1
= AdaptCoeff1
[block_predictor
[1]];
637 c
->status
[1].coeff2
= AdaptCoeff2
[block_predictor
[1]];
639 c
->status
[0].sample1
= ((*src
& 0xFF) | ((src
[1] << 8) & 0xFF00));
641 if (st
) c
->status
[1].sample1
= ((*src
& 0xFF) | ((src
[1] << 8) & 0xFF00));
643 c
->status
[0].sample2
= ((*src
& 0xFF) | ((src
[1] << 8) & 0xFF00));
645 if (st
) c
->status
[1].sample2
= ((*src
& 0xFF) | ((src
[1] << 8) & 0xFF00));
648 *samples
++ = c
->status
[0].sample1
;
649 if (st
) *samples
++ = c
->status
[1].sample1
;
650 *samples
++ = c
->status
[0].sample2
;
651 if (st
) *samples
++ = c
->status
[1].sample2
;
653 *samples
++ = adpcm_ms_expand_nibble(&c
->status
[0], (src
[0] >> 4) & 0x0F);
654 *samples
++ = adpcm_ms_expand_nibble(&c
->status
[st
], src
[0] & 0x0F);
658 case CODEC_ID_ADPCM_IMA_DK4
:
659 if (buf_size
> BLKSIZE
) {
660 if (avctx
->block_align
!= 0)
661 buf_size
= avctx
->block_align
;
665 c
->status
[0].predictor
= (src
[0] | (src
[1] << 8));
666 c
->status
[0].step_index
= src
[2];
668 if(c
->status
[0].predictor
& 0x8000)
669 c
->status
[0].predictor
-= 0x10000;
670 *samples
++ = c
->status
[0].predictor
;
672 c
->status
[1].predictor
= (src
[0] | (src
[1] << 8));
673 c
->status
[1].step_index
= src
[2];
675 if(c
->status
[1].predictor
& 0x8000)
676 c
->status
[1].predictor
-= 0x10000;
677 *samples
++ = c
->status
[1].predictor
;
679 while (src
< buf
+ buf_size
) {
681 /* take care of the top nibble (always left or mono channel) */
682 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0],
683 (src
[0] >> 4) & 0x0F);
685 /* take care of the bottom nibble, which is right sample for
686 * stereo, or another mono sample */
688 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[1],
691 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0],
697 case CODEC_ID_ADPCM_IMA_DK3
:
698 if (buf_size
> BLKSIZE
) {
699 if (avctx
->block_align
!= 0)
700 buf_size
= avctx
->block_align
;
704 c
->status
[0].predictor
= (src
[10] | (src
[11] << 8));
705 c
->status
[1].predictor
= (src
[12] | (src
[13] << 8));
706 c
->status
[0].step_index
= src
[14];
707 c
->status
[1].step_index
= src
[15];
708 /* sign extend the predictors */
709 if(c
->status
[0].predictor
& 0x8000)
710 c
->status
[0].predictor
-= 0x10000;
711 if(c
->status
[1].predictor
& 0x8000)
712 c
->status
[1].predictor
-= 0x10000;
714 diff_channel
= c
->status
[1].predictor
;
716 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
717 * the buffer is consumed */
720 /* for this algorithm, c->status[0] is the sum channel and
721 * c->status[1] is the diff channel */
723 /* process the first predictor of the sum channel */
724 DK3_GET_NEXT_NIBBLE();
725 adpcm_ima_expand_nibble(&c
->status
[0], nibble
);
727 /* process the diff channel predictor */
728 DK3_GET_NEXT_NIBBLE();
729 adpcm_ima_expand_nibble(&c
->status
[1], nibble
);
731 /* process the first pair of stereo PCM samples */
732 diff_channel
= (diff_channel
+ c
->status
[1].predictor
) / 2;
733 *samples
++ = c
->status
[0].predictor
+ c
->status
[1].predictor
;
734 *samples
++ = c
->status
[0].predictor
- c
->status
[1].predictor
;
736 /* process the second predictor of the sum channel */
737 DK3_GET_NEXT_NIBBLE();
738 adpcm_ima_expand_nibble(&c
->status
[0], nibble
);
740 /* process the second pair of stereo PCM samples */
741 diff_channel
= (diff_channel
+ c
->status
[1].predictor
) / 2;
742 *samples
++ = c
->status
[0].predictor
+ c
->status
[1].predictor
;
743 *samples
++ = c
->status
[0].predictor
- c
->status
[1].predictor
;
746 case CODEC_ID_ADPCM_IMA_WS
:
747 /* no per-block initialization; just start decoding the data */
748 while (src
< buf
+ buf_size
) {
751 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0],
752 (src
[0] >> 4) & 0x0F);
753 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[1],
756 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0],
757 (src
[0] >> 4) & 0x0F);
758 *samples
++ = adpcm_ima_expand_nibble(&c
->status
[0],
765 case CODEC_ID_ADPCM_XA
:
766 c
->status
[0].sample1
= c
->status
[0].sample2
=
767 c
->status
[1].sample1
= c
->status
[1].sample2
= 0;
768 while (buf_size
>= 128) {
769 xa_decode(samples
, src
, &c
->status
[0], &c
->status
[1],
780 *data_size
= (uint8_t *)samples
- (uint8_t *)data
;
786 #ifdef CONFIG_ENCODERS
787 #define ADPCM_ENCODER(id,name) \
788 AVCodec name ## _encoder = { \
792 sizeof(ADPCMContext), \
794 adpcm_encode_frame, \
795 adpcm_encode_close, \
799 #define ADPCM_ENCODER(id,name)
802 #ifdef CONFIG_DECODERS
803 #define ADPCM_DECODER(id,name) \
804 AVCodec name ## _decoder = { \
808 sizeof(ADPCMContext), \
812 adpcm_decode_frame, \
815 #define ADPCM_DECODER(id,name)
818 #define ADPCM_CODEC(id, name) \
819 ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
821 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT
, adpcm_ima_qt
);
822 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV
, adpcm_ima_wav
);
823 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3
, adpcm_ima_dk3
);
824 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4
, adpcm_ima_dk4
);
825 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS
, adpcm_ima_ws
);
826 ADPCM_CODEC(CODEC_ID_ADPCM_MS
, adpcm_ms
);
827 ADPCM_CODEC(CODEC_ID_ADPCM_4XM
, adpcm_4xm
);
828 ADPCM_CODEC(CODEC_ID_ADPCM_XA
, adpcm_xa
);
829 ADPCM_CODEC(CODEC_ID_ADPCM_ADX
, adpcm_adx
);