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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "bitstream.h"
25 * First version by Francois Revol (revol@free.fr)
26 * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
27 * by Mike Melanson (melanson@pcisys.net)
28 * CD-ROM XA ADPCM codec by BERO
29 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
31 * Features and limitations:
33 * Reference documents:
34 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
35 * http://www.geocities.com/SiliconValley/8682/aud3.txt
36 * http://openquicktime.sourceforge.net/plugins.htm
37 * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
38 * http://www.cs.ucla.edu/~leec/mediabench/applications.html
39 * SoX source code http://home.sprynet.com/~cbagwell/sox.html
42 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
43 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
44 * readstr http://www.geocities.co.jp/Playtown/2004/
49 #define CLAMP_TO_SHORT(value) \
52 else if (value < -32768) \
55 /* step_table[] and index_table[] are from the ADPCM reference source */
56 /* This is the index table: */
57 static const int index_table[16] = {
58 -1, -1, -1, -1, 2, 4, 6, 8,
59 -1, -1, -1, -1, 2, 4, 6, 8,
63 * This is the step table. Note that many programs use slight deviations from
64 * this table, but such deviations are negligible:
66 static const int step_table[89] = {
67 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
68 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
69 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
70 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
71 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
72 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
73 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
74 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
75 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
78 /* These are for MS-ADPCM */
79 /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
80 static const int AdaptationTable[] = {
81 230, 230, 230, 230, 307, 409, 512, 614,
82 768, 614, 512, 409, 307, 230, 230, 230
85 static const int AdaptCoeff1[] = {
86 256, 512, 0, 192, 240, 460, 392
89 static const int AdaptCoeff2[] = {
90 0, -256, 0, 64, 0, -208, -232
93 /* These are for CD-ROM XA ADPCM */
94 static const int xa_adpcm_table[5][2] = {
102 static const int ea_adpcm_table[] = {
103 0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
104 3, 4, 7, 8, 10, 11, 0, -1, -3, -4
107 static const int ct_adpcm_table[8] = {
108 0x00E6, 0x00E6, 0x00E6, 0x00E6,
109 0x0133, 0x0199, 0x0200, 0x0266
112 // padded to zero where table size is less then 16
113 static const int swf_index_tables[4][16] = {
115 /*3*/ { -1, -1, 2, 4 },
116 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
117 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
120 static const int yamaha_indexscale[] = {
121 230, 230, 230, 230, 307, 409, 512, 614,
122 230, 230, 230, 230, 307, 409, 512, 614
125 static const int yamaha_difflookup[] = {
126 1, 3, 5, 7, 9, 11, 13, 15,
127 -1, -3, -5, -7, -9, -11, -13, -15
132 typedef struct ADPCMChannelStatus {
134 short int step_index;
145 } ADPCMChannelStatus;
147 typedef struct ADPCMContext {
148 int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
149 ADPCMChannelStatus status[2];
150 short sample_buffer[32]; /* hold left samples while waiting for right samples */
157 /* XXX: implement encoding */
159 #ifdef CONFIG_ENCODERS
160 static int adpcm_encode_init(AVCodecContext *avctx)
162 if (avctx->channels > 2)
163 return -1; /* only stereo or mono =) */
164 switch(avctx->codec->id) {
165 case CODEC_ID_ADPCM_IMA_QT:
166 av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n");
167 avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
170 case CODEC_ID_ADPCM_IMA_WAV:
171 avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
172 /* and we have 4 bytes per channel overhead */
173 avctx->block_align = BLKSIZE;
174 /* seems frame_size isn't taken into account... have to buffer the samples :-( */
176 case CODEC_ID_ADPCM_MS:
177 avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
178 /* and we have 7 bytes per channel overhead */
179 avctx->block_align = BLKSIZE;
181 case CODEC_ID_ADPCM_YAMAHA:
182 avctx->frame_size = BLKSIZE * avctx->channels;
183 avctx->block_align = BLKSIZE;
190 avctx->coded_frame= avcodec_alloc_frame();
191 avctx->coded_frame->key_frame= 1;
196 static int adpcm_encode_close(AVCodecContext *avctx)
198 av_freep(&avctx->coded_frame);
204 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
207 unsigned char nibble;
209 int sign = 0; /* sign bit of the nibble (MSB) */
210 int delta, predicted_delta;
212 delta = sample - c->prev_sample;
219 step_index = c->step_index;
221 /* nibble = 4 * delta / step_table[step_index]; */
222 nibble = (delta << 2) / step_table[step_index];
227 step_index += index_table[nibble];
233 /* what the decoder will find */
234 predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
237 c->prev_sample -= predicted_delta;
239 c->prev_sample += predicted_delta;
241 CLAMP_TO_SHORT(c->prev_sample);
244 nibble += sign << 3; /* sign * 8 */
247 c->step_index = step_index;
252 static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
254 int predictor, nibble, bias;
256 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
258 nibble= sample - predictor;
259 if(nibble>=0) bias= c->idelta/2;
260 else bias=-c->idelta/2;
262 nibble= (nibble + bias) / c->idelta;
263 nibble= clip(nibble, -8, 7)&0x0F;
265 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
266 CLAMP_TO_SHORT(predictor);
268 c->sample2 = c->sample1;
269 c->sample1 = predictor;
271 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
272 if (c->idelta < 16) c->idelta = 16;
277 static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
285 j1 = sample - c->predictor;
287 j1 = (j1 * 8) / c->step;
294 c->predictor = c->predictor + ((c->step * yamaha_difflookup[i1]) / 8);
295 CLAMP_TO_SHORT(c->predictor);
296 c->step = (c->step * yamaha_indexscale[i1]) >> 8;
297 c->step = clip(c->step, 127, 24567);
302 static int adpcm_encode_frame(AVCodecContext *avctx,
303 unsigned char *frame, int buf_size, void *data)
308 ADPCMContext *c = avctx->priv_data;
311 samples = (short *)data;
312 st= avctx->channels == 2;
313 /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
315 switch(avctx->codec->id) {
316 case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
318 case CODEC_ID_ADPCM_IMA_WAV:
319 n = avctx->frame_size / 8;
320 c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
321 /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
322 *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
323 *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
324 *dst++ = (unsigned char)c->status[0].step_index;
325 *dst++ = 0; /* unknown */
327 if (avctx->channels == 2) {
328 c->status[1].prev_sample = (signed short)samples[1];
329 /* c->status[1].step_index = 0; */
330 *dst++ = (c->status[1].prev_sample) & 0xFF;
331 *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
332 *dst++ = (unsigned char)c->status[1].step_index;
337 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
339 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
340 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
342 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
343 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
345 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
346 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
348 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
349 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
352 if (avctx->channels == 2) {
353 *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
354 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
356 *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
357 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
359 *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
360 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
362 *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
363 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
366 samples += 8 * avctx->channels;
369 case CODEC_ID_ADPCM_MS:
370 for(i=0; i<avctx->channels; i++){
374 c->status[i].coeff1 = AdaptCoeff1[predictor];
375 c->status[i].coeff2 = AdaptCoeff2[predictor];
377 for(i=0; i<avctx->channels; i++){
378 if (c->status[i].idelta < 16)
379 c->status[i].idelta = 16;
381 *dst++ = c->status[i].idelta & 0xFF;
382 *dst++ = c->status[i].idelta >> 8;
384 for(i=0; i<avctx->channels; i++){
385 c->status[i].sample1= *samples++;
387 *dst++ = c->status[i].sample1 & 0xFF;
388 *dst++ = c->status[i].sample1 >> 8;
390 for(i=0; i<avctx->channels; i++){
391 c->status[i].sample2= *samples++;
393 *dst++ = c->status[i].sample2 & 0xFF;
394 *dst++ = c->status[i].sample2 >> 8;
397 for(i=7*avctx->channels; i<avctx->block_align; i++) {
399 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
400 nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
404 case CODEC_ID_ADPCM_YAMAHA:
405 n = avctx->frame_size / 2;
407 for(i = 0; i < avctx->channels; i++) {
409 nibble = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
410 nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
413 samples += 2 * avctx->channels;
421 #endif //CONFIG_ENCODERS
423 static int adpcm_decode_init(AVCodecContext * avctx)
425 ADPCMContext *c = avctx->priv_data;
428 c->status[0].predictor = c->status[1].predictor = 0;
429 c->status[0].step_index = c->status[1].step_index = 0;
430 c->status[0].step = c->status[1].step = 0;
432 switch(avctx->codec->id) {
433 case CODEC_ID_ADPCM_CT:
434 c->status[0].step = c->status[1].step = 511;
442 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
446 int sign, delta, diff, step;
448 step = step_table[c->step_index];
449 step_index = c->step_index + index_table[(unsigned)nibble];
450 if (step_index < 0) step_index = 0;
451 else if (step_index > 88) step_index = 88;
455 /* perform direct multiplication instead of series of jumps proposed by
456 * the reference ADPCM implementation since modern CPUs can do the mults
458 diff = ((2 * delta + 1) * step) >> shift;
459 predictor = c->predictor;
460 if (sign) predictor -= diff;
461 else predictor += diff;
463 CLAMP_TO_SHORT(predictor);
464 c->predictor = predictor;
465 c->step_index = step_index;
467 return (short)predictor;
470 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
474 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
475 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
476 CLAMP_TO_SHORT(predictor);
478 c->sample2 = c->sample1;
479 c->sample1 = predictor;
480 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
481 if (c->idelta < 16) c->idelta = 16;
483 return (short)predictor;
486 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
489 int sign, delta, diff;
494 /* perform direct multiplication instead of series of jumps proposed by
495 * the reference ADPCM implementation since modern CPUs can do the mults
497 diff = ((2 * delta + 1) * c->step) >> 3;
498 predictor = c->predictor;
499 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
501 predictor = ((predictor * 254) >> 8) - diff;
503 predictor = ((predictor * 254) >> 8) + diff;
504 /* calculate new step and clamp it to range 511..32767 */
505 new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
512 CLAMP_TO_SHORT(predictor);
513 c->predictor = predictor;
514 return (short)predictor;
517 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
519 int sign, delta, diff;
521 sign = nibble & (1<<(size-1));
522 delta = nibble & ((1<<(size-1))-1);
523 diff = delta << (7 + c->step + shift);
526 c->predictor -= diff;
528 c->predictor += diff;
531 if (c->predictor > 16256)
532 c->predictor = 16256;
533 else if (c->predictor < -16384)
534 c->predictor = -16384;
536 /* calculate new step */
537 if (delta >= (2*size - 3) && c->step < 3)
539 else if (delta == 0 && c->step > 0)
542 return (short) c->predictor;
545 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
552 c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
553 CLAMP_TO_SHORT(c->predictor);
554 c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
555 c->step = clip(c->step, 127, 24567);
559 static void xa_decode(short *out, const unsigned char *in,
560 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
563 int shift,filter,f0,f1;
569 shift = 12 - (in[4+i*2] & 15);
570 filter = in[4+i*2] >> 4;
571 f0 = xa_adpcm_table[filter][0];
572 f1 = xa_adpcm_table[filter][1];
580 t = (signed char)(d<<4)>>4;
581 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
589 if (inc==2) { /* stereo */
592 s_1 = right->sample1;
593 s_2 = right->sample2;
594 out = out + 1 - 28*2;
597 shift = 12 - (in[5+i*2] & 15);
598 filter = in[5+i*2] >> 4;
600 f0 = xa_adpcm_table[filter][0];
601 f1 = xa_adpcm_table[filter][1];
606 t = (signed char)d >> 4;
607 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
615 if (inc==2) { /* stereo */
616 right->sample1 = s_1;
617 right->sample2 = s_2;
627 /* DK3 ADPCM support macro */
628 #define DK3_GET_NEXT_NIBBLE() \
629 if (decode_top_nibble_next) \
631 nibble = (last_byte >> 4) & 0x0F; \
632 decode_top_nibble_next = 0; \
636 last_byte = *src++; \
637 if (src >= buf + buf_size) break; \
638 nibble = last_byte & 0x0F; \
639 decode_top_nibble_next = 1; \
642 static int adpcm_decode_frame(AVCodecContext *avctx,
643 void *data, int *data_size,
644 uint8_t *buf, int buf_size)
646 ADPCMContext *c = avctx->priv_data;
647 ADPCMChannelStatus *cs;
648 int n, m, channel, i;
649 int block_predictor[2];
654 /* DK3 ADPCM accounting variables */
655 unsigned char last_byte = 0;
656 unsigned char nibble;
657 int decode_top_nibble_next = 0;
660 /* EA ADPCM state variables */
661 uint32_t samples_in_chunk;
662 int32_t previous_left_sample, previous_right_sample;
663 int32_t current_left_sample, current_right_sample;
664 int32_t next_left_sample, next_right_sample;
665 int32_t coeff1l, coeff2l, coeff1r, coeff2r;
666 uint8_t shift_left, shift_right;
675 st = avctx->channels == 2 ? 1 : 0;
677 switch(avctx->codec->id) {
678 case CODEC_ID_ADPCM_IMA_QT:
679 n = (buf_size - 2);/* >> 2*avctx->channels;*/
680 channel = c->channel;
681 cs = &(c->status[channel]);
682 /* (pppppp) (piiiiiii) */
684 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
685 cs->predictor = (*src++) << 8;
686 cs->predictor |= (*src & 0x80);
687 cs->predictor &= 0xFF80;
690 if(cs->predictor & 0x8000)
691 cs->predictor -= 0x10000;
693 CLAMP_TO_SHORT(cs->predictor);
695 cs->step_index = (*src++) & 0x7F;
697 if (cs->step_index > 88){
698 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
702 cs->step = step_table[cs->step_index];
707 for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
708 *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
709 samples += avctx->channels;
710 *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
711 samples += avctx->channels;
715 if(st) { /* handle stereo interlacing */
716 c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
717 if(channel == 1) { /* wait for the other packet before outputing anything */
722 case CODEC_ID_ADPCM_IMA_WAV:
723 if (avctx->block_align != 0 && buf_size > avctx->block_align)
724 buf_size = avctx->block_align;
726 // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
728 for(i=0; i<avctx->channels; i++){
729 cs = &(c->status[i]);
730 cs->predictor = (int16_t)(src[0] + (src[1]<<8));
733 // XXX: is this correct ??: *samples++ = cs->predictor;
735 cs->step_index = *src++;
736 if (cs->step_index > 88){
737 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
740 if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
743 while(src < buf + buf_size){
746 *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
748 *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
754 case CODEC_ID_ADPCM_4XM:
755 cs = &(c->status[0]);
756 c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
758 c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
760 c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
762 c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
764 if (cs->step_index < 0) cs->step_index = 0;
765 if (cs->step_index > 88) cs->step_index = 88;
767 m= (buf_size - (src - buf))>>st;
769 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
771 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
772 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
774 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
780 case CODEC_ID_ADPCM_MS:
781 if (avctx->block_align != 0 && buf_size > avctx->block_align)
782 buf_size = avctx->block_align;
783 n = buf_size - 7 * avctx->channels;
786 block_predictor[0] = clip(*src++, 0, 7);
787 block_predictor[1] = 0;
789 block_predictor[1] = clip(*src++, 0, 7);
790 c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
793 c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
796 c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
797 c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
798 c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
799 c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
801 c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
803 if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
805 c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
807 if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
810 *samples++ = c->status[0].sample1;
811 if (st) *samples++ = c->status[1].sample1;
812 *samples++ = c->status[0].sample2;
813 if (st) *samples++ = c->status[1].sample2;
815 *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
816 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
820 case CODEC_ID_ADPCM_IMA_DK4:
821 if (avctx->block_align != 0 && buf_size > avctx->block_align)
822 buf_size = avctx->block_align;
824 c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
825 c->status[0].step_index = src[2];
827 *samples++ = c->status[0].predictor;
829 c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
830 c->status[1].step_index = src[2];
832 *samples++ = c->status[1].predictor;
834 while (src < buf + buf_size) {
836 /* take care of the top nibble (always left or mono channel) */
837 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
838 (src[0] >> 4) & 0x0F, 3);
840 /* take care of the bottom nibble, which is right sample for
841 * stereo, or another mono sample */
843 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
846 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
852 case CODEC_ID_ADPCM_IMA_DK3:
853 if (avctx->block_align != 0 && buf_size > avctx->block_align)
854 buf_size = avctx->block_align;
856 c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
857 c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
858 c->status[0].step_index = src[14];
859 c->status[1].step_index = src[15];
860 /* sign extend the predictors */
862 diff_channel = c->status[1].predictor;
864 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
865 * the buffer is consumed */
868 /* for this algorithm, c->status[0] is the sum channel and
869 * c->status[1] is the diff channel */
871 /* process the first predictor of the sum channel */
872 DK3_GET_NEXT_NIBBLE();
873 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
875 /* process the diff channel predictor */
876 DK3_GET_NEXT_NIBBLE();
877 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
879 /* process the first pair of stereo PCM samples */
880 diff_channel = (diff_channel + c->status[1].predictor) / 2;
881 *samples++ = c->status[0].predictor + c->status[1].predictor;
882 *samples++ = c->status[0].predictor - c->status[1].predictor;
884 /* process the second predictor of the sum channel */
885 DK3_GET_NEXT_NIBBLE();
886 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
888 /* process the second pair of stereo PCM samples */
889 diff_channel = (diff_channel + c->status[1].predictor) / 2;
890 *samples++ = c->status[0].predictor + c->status[1].predictor;
891 *samples++ = c->status[0].predictor - c->status[1].predictor;
894 case CODEC_ID_ADPCM_IMA_WS:
895 /* no per-block initialization; just start decoding the data */
896 while (src < buf + buf_size) {
899 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
900 (src[0] >> 4) & 0x0F, 3);
901 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
904 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
905 (src[0] >> 4) & 0x0F, 3);
906 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
913 case CODEC_ID_ADPCM_XA:
914 c->status[0].sample1 = c->status[0].sample2 =
915 c->status[1].sample1 = c->status[1].sample2 = 0;
916 while (buf_size >= 128) {
917 xa_decode(samples, src, &c->status[0], &c->status[1],
924 case CODEC_ID_ADPCM_EA:
925 samples_in_chunk = LE_32(src);
926 if (samples_in_chunk >= ((buf_size - 12) * 2)) {
931 current_left_sample = (int16_t)LE_16(src);
933 previous_left_sample = (int16_t)LE_16(src);
935 current_right_sample = (int16_t)LE_16(src);
937 previous_right_sample = (int16_t)LE_16(src);
940 for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
941 coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
942 coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
943 coeff1r = ea_adpcm_table[*src & 0x0F];
944 coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
947 shift_left = ((*src >> 4) & 0x0F) + 8;
948 shift_right = (*src & 0x0F) + 8;
951 for (count2 = 0; count2 < 28; count2++) {
952 next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
953 next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
956 next_left_sample = (next_left_sample +
957 (current_left_sample * coeff1l) +
958 (previous_left_sample * coeff2l) + 0x80) >> 8;
959 next_right_sample = (next_right_sample +
960 (current_right_sample * coeff1r) +
961 (previous_right_sample * coeff2r) + 0x80) >> 8;
962 CLAMP_TO_SHORT(next_left_sample);
963 CLAMP_TO_SHORT(next_right_sample);
965 previous_left_sample = current_left_sample;
966 current_left_sample = next_left_sample;
967 previous_right_sample = current_right_sample;
968 current_right_sample = next_right_sample;
969 *samples++ = (unsigned short)current_left_sample;
970 *samples++ = (unsigned short)current_right_sample;
974 case CODEC_ID_ADPCM_IMA_SMJPEG:
975 c->status[0].predictor = *src;
977 c->status[0].step_index = *src++;
978 src++; /* skip another byte before getting to the meat */
979 while (src < buf + buf_size) {
980 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
982 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
983 (*src >> 4) & 0x0F, 3);
987 case CODEC_ID_ADPCM_CT:
988 while (src < buf + buf_size) {
990 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
991 (src[0] >> 4) & 0x0F);
992 *samples++ = adpcm_ct_expand_nibble(&c->status[1],
995 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
996 (src[0] >> 4) & 0x0F);
997 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
1003 case CODEC_ID_ADPCM_SBPRO_4:
1004 case CODEC_ID_ADPCM_SBPRO_3:
1005 case CODEC_ID_ADPCM_SBPRO_2:
1006 if (!c->status[0].step_index) {
1007 /* the first byte is a raw sample */
1008 *samples++ = 128 * (*src++ - 0x80);
1010 *samples++ = 128 * (*src++ - 0x80);
1011 c->status[0].step_index = 1;
1013 if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
1014 while (src < buf + buf_size) {
1015 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1016 (src[0] >> 4) & 0x0F, 4, 0);
1017 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1018 src[0] & 0x0F, 4, 0);
1021 } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
1022 while (src < buf + buf_size) {
1023 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1024 (src[0] >> 5) & 0x07, 3, 0);
1025 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1026 (src[0] >> 2) & 0x07, 3, 0);
1027 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1028 src[0] & 0x03, 2, 0);
1032 while (src < buf + buf_size) {
1033 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1034 (src[0] >> 6) & 0x03, 2, 2);
1035 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1036 (src[0] >> 4) & 0x03, 2, 2);
1037 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1038 (src[0] >> 2) & 0x03, 2, 2);
1039 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1040 src[0] & 0x03, 2, 2);
1045 case CODEC_ID_ADPCM_SWF:
1050 int size = buf_size*8;
1052 init_get_bits(&gb, buf, size);
1054 // first frame, read bits & inital values
1057 c->nb_bits = get_bits(&gb, 2)+2;
1058 // av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
1061 table = swf_index_tables[c->nb_bits-2];
1062 k0 = 1 << (c->nb_bits-2);
1063 signmask = 1 << (c->nb_bits-1);
1065 while (get_bits_count(&gb) <= size)
1070 // wrap around at every 4096 samples...
1071 if ((c->nb_samples & 0xfff) == 1)
1073 for (i = 0; i <= st; i++)
1075 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1076 c->status[i].step_index = get_bits(&gb, 6);
1080 // similar to IMA adpcm
1081 for (i = 0; i <= st; i++)
1083 int delta = get_bits(&gb, c->nb_bits);
1084 int step = step_table[c->status[i].step_index];
1085 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1096 if (delta & signmask)
1097 c->status[i].predictor -= vpdiff;
1099 c->status[i].predictor += vpdiff;
1101 c->status[i].step_index += table[delta & (~signmask)];
1103 c->status[i].step_index = clip(c->status[i].step_index, 0, 88);
1104 c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
1106 *samples++ = c->status[i].predictor;
1110 // src += get_bits_count(&gb)*8;
1115 case CODEC_ID_ADPCM_YAMAHA:
1116 while (src < buf + buf_size) {
1118 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1120 *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
1121 (src[0] >> 4) & 0x0F);
1123 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1125 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1126 (src[0] >> 4) & 0x0F);
1134 *data_size = (uint8_t *)samples - (uint8_t *)data;
1140 #ifdef CONFIG_ENCODERS
1141 #define ADPCM_ENCODER(id,name) \
1142 AVCodec name ## _encoder = { \
1146 sizeof(ADPCMContext), \
1147 adpcm_encode_init, \
1148 adpcm_encode_frame, \
1149 adpcm_encode_close, \
1153 #define ADPCM_ENCODER(id,name)
1156 #ifdef CONFIG_DECODERS
1157 #define ADPCM_DECODER(id,name) \
1158 AVCodec name ## _decoder = { \
1162 sizeof(ADPCMContext), \
1163 adpcm_decode_init, \
1166 adpcm_decode_frame, \
1169 #define ADPCM_DECODER(id,name)
1172 #define ADPCM_CODEC(id, name) \
1173 ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
1175 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
1176 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
1177 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
1178 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
1179 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
1180 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
1181 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
1182 ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
1183 ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
1184 ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
1185 ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
1186 ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
1187 ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
1188 ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
1189 ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
1190 ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
1191 ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);