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
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_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
524
c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
525
CLAMP_TO_SHORT(c->predictor);
526
c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
527
c->step = clip(c->step, 127, 24567);
531
static void xa_decode(short *out, const unsigned char *in,
532
ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
535
int shift,filter,f0,f1;
541
shift = 12 - (in[4+i*2] & 15);
542
filter = in[4+i*2] >> 4;
543
f0 = xa_adpcm_table[filter][0];
544
f1 = xa_adpcm_table[filter][1];
552
t = (signed char)(d<<4)>>4;
553
s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
561
if (inc==2) { /* stereo */
564
s_1 = right->sample1;
565
s_2 = right->sample2;
566
out = out + 1 - 28*2;
569
shift = 12 - (in[5+i*2] & 15);
570
filter = in[5+i*2] >> 4;
572
f0 = xa_adpcm_table[filter][0];
573
f1 = xa_adpcm_table[filter][1];
578
t = (signed char)d >> 4;
579
s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
587
if (inc==2) { /* stereo */
588
right->sample1 = s_1;
589
right->sample2 = s_2;
599
/* DK3 ADPCM support macro */
600
#define DK3_GET_NEXT_NIBBLE() \
601
if (decode_top_nibble_next) \
603
nibble = (last_byte >> 4) & 0x0F; \
604
decode_top_nibble_next = 0; \
608
last_byte = *src++; \
609
if (src >= buf + buf_size) break; \
610
nibble = last_byte & 0x0F; \
611
decode_top_nibble_next = 1; \
614
static int adpcm_decode_frame(AVCodecContext *avctx,
615
void *data, int *data_size,
616
uint8_t *buf, int buf_size)
618
ADPCMContext *c = avctx->priv_data;
619
ADPCMChannelStatus *cs;
620
int n, m, channel, i;
621
int block_predictor[2];
626
/* DK3 ADPCM accounting variables */
627
unsigned char last_byte = 0;
628
unsigned char nibble;
629
int decode_top_nibble_next = 0;
632
/* EA ADPCM state variables */
633
uint32_t samples_in_chunk;
634
int32_t previous_left_sample, previous_right_sample;
635
int32_t current_left_sample, current_right_sample;
636
int32_t next_left_sample, next_right_sample;
637
int32_t coeff1l, coeff2l, coeff1r, coeff2r;
638
uint8_t shift_left, shift_right;
647
st = avctx->channels == 2;
649
switch(avctx->codec->id) {
650
case CODEC_ID_ADPCM_IMA_QT:
651
n = (buf_size - 2);/* >> 2*avctx->channels;*/
652
channel = c->channel;
653
cs = &(c->status[channel]);
654
/* (pppppp) (piiiiiii) */
656
/* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
657
cs->predictor = (*src++) << 8;
658
cs->predictor |= (*src & 0x80);
659
cs->predictor &= 0xFF80;
662
if(cs->predictor & 0x8000)
663
cs->predictor -= 0x10000;
665
CLAMP_TO_SHORT(cs->predictor);
667
cs->step_index = (*src++) & 0x7F;
669
if (cs->step_index > 88) av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
670
if (cs->step_index > 88) cs->step_index = 88;
672
cs->step = step_table[cs->step_index];
677
for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
678
*samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
679
samples += avctx->channels;
680
*samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
681
samples += avctx->channels;
685
if(st) { /* handle stereo interlacing */
686
c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
687
if(channel == 1) { /* wait for the other packet before outputing anything */
692
case CODEC_ID_ADPCM_IMA_WAV:
693
if (avctx->block_align != 0 && buf_size > avctx->block_align)
694
buf_size = avctx->block_align;
696
for(i=0; i<avctx->channels; i++){
697
cs = &(c->status[i]);
698
cs->predictor = *src++;
699
cs->predictor |= (*src++) << 8;
700
if(cs->predictor & 0x8000)
701
cs->predictor -= 0x10000;
702
CLAMP_TO_SHORT(cs->predictor);
704
// XXX: is this correct ??: *samples++ = cs->predictor;
706
cs->step_index = *src++;
707
if (cs->step_index < 0) cs->step_index = 0;
708
if (cs->step_index > 88) cs->step_index = 88;
709
if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */
712
for(m=4; src < (buf + buf_size);) {
713
*samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3);
715
*samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3);
716
*samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3);
718
*samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3);
727
case CODEC_ID_ADPCM_4XM:
728
cs = &(c->status[0]);
729
c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
731
c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
733
c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
735
c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
737
if (cs->step_index < 0) cs->step_index = 0;
738
if (cs->step_index > 88) cs->step_index = 88;
740
m= (buf_size - (src - buf))>>st;
742
*samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
744
*samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
745
*samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
747
*samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
753
case CODEC_ID_ADPCM_MS:
754
if (avctx->block_align != 0 && buf_size > avctx->block_align)
755
buf_size = avctx->block_align;
756
n = buf_size - 7 * avctx->channels;
759
block_predictor[0] = clip(*src++, 0, 7);
760
block_predictor[1] = 0;
762
block_predictor[1] = clip(*src++, 0, 7);
763
c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
766
c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
769
c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
770
c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
771
c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
772
c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
774
c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
776
if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
778
c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
780
if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
783
*samples++ = c->status[0].sample1;
784
if (st) *samples++ = c->status[1].sample1;
785
*samples++ = c->status[0].sample2;
786
if (st) *samples++ = c->status[1].sample2;
788
*samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
789
*samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
793
case CODEC_ID_ADPCM_IMA_DK4:
794
if (avctx->block_align != 0 && buf_size > avctx->block_align)
795
buf_size = avctx->block_align;
797
c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
798
c->status[0].step_index = src[2];
800
*samples++ = c->status[0].predictor;
802
c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
803
c->status[1].step_index = src[2];
805
*samples++ = c->status[1].predictor;
807
while (src < buf + buf_size) {
809
/* take care of the top nibble (always left or mono channel) */
810
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
811
(src[0] >> 4) & 0x0F, 3);
813
/* take care of the bottom nibble, which is right sample for
814
* stereo, or another mono sample */
816
*samples++ = adpcm_ima_expand_nibble(&c->status[1],
819
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
825
case CODEC_ID_ADPCM_IMA_DK3:
826
if (avctx->block_align != 0 && buf_size > avctx->block_align)
827
buf_size = avctx->block_align;
829
c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
830
c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
831
c->status[0].step_index = src[14];
832
c->status[1].step_index = src[15];
833
/* sign extend the predictors */
835
diff_channel = c->status[1].predictor;
837
/* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
838
* the buffer is consumed */
841
/* for this algorithm, c->status[0] is the sum channel and
842
* c->status[1] is the diff channel */
844
/* process the first predictor of the sum channel */
845
DK3_GET_NEXT_NIBBLE();
846
adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
848
/* process the diff channel predictor */
849
DK3_GET_NEXT_NIBBLE();
850
adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
852
/* process the first pair of stereo PCM samples */
853
diff_channel = (diff_channel + c->status[1].predictor) / 2;
854
*samples++ = c->status[0].predictor + c->status[1].predictor;
855
*samples++ = c->status[0].predictor - c->status[1].predictor;
857
/* process the second predictor of the sum channel */
858
DK3_GET_NEXT_NIBBLE();
859
adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
861
/* process the second pair of stereo PCM samples */
862
diff_channel = (diff_channel + c->status[1].predictor) / 2;
863
*samples++ = c->status[0].predictor + c->status[1].predictor;
864
*samples++ = c->status[0].predictor - c->status[1].predictor;
867
case CODEC_ID_ADPCM_IMA_WS:
868
/* no per-block initialization; just start decoding the data */
869
while (src < buf + buf_size) {
872
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
873
(src[0] >> 4) & 0x0F, 3);
874
*samples++ = adpcm_ima_expand_nibble(&c->status[1],
877
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
878
(src[0] >> 4) & 0x0F, 3);
879
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
886
case CODEC_ID_ADPCM_XA:
887
c->status[0].sample1 = c->status[0].sample2 =
888
c->status[1].sample1 = c->status[1].sample2 = 0;
889
while (buf_size >= 128) {
890
xa_decode(samples, src, &c->status[0], &c->status[1],
897
case CODEC_ID_ADPCM_EA:
898
samples_in_chunk = LE_32(src);
899
if (samples_in_chunk >= ((buf_size - 12) * 2)) {
904
current_left_sample = (int16_t)LE_16(src);
906
previous_left_sample = (int16_t)LE_16(src);
908
current_right_sample = (int16_t)LE_16(src);
910
previous_right_sample = (int16_t)LE_16(src);
913
for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
914
coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
915
coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
916
coeff1r = ea_adpcm_table[*src & 0x0F];
917
coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
920
shift_left = ((*src >> 4) & 0x0F) + 8;
921
shift_right = (*src & 0x0F) + 8;
924
for (count2 = 0; count2 < 28; count2++) {
925
next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
926
next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
929
next_left_sample = (next_left_sample +
930
(current_left_sample * coeff1l) +
931
(previous_left_sample * coeff2l) + 0x80) >> 8;
932
next_right_sample = (next_right_sample +
933
(current_right_sample * coeff1r) +
934
(previous_right_sample * coeff2r) + 0x80) >> 8;
935
CLAMP_TO_SHORT(next_left_sample);
936
CLAMP_TO_SHORT(next_right_sample);
938
previous_left_sample = current_left_sample;
939
current_left_sample = next_left_sample;
940
previous_right_sample = current_right_sample;
941
current_right_sample = next_right_sample;
942
*samples++ = (unsigned short)current_left_sample;
943
*samples++ = (unsigned short)current_right_sample;
947
case CODEC_ID_ADPCM_IMA_SMJPEG:
948
c->status[0].predictor = *src;
950
c->status[0].step_index = *src++;
951
src++; /* skip another byte before getting to the meat */
952
while (src < buf + buf_size) {
953
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
955
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
956
(*src >> 4) & 0x0F, 3);
960
case CODEC_ID_ADPCM_CT:
961
while (src < buf + buf_size) {
963
*samples++ = adpcm_ct_expand_nibble(&c->status[0],
964
(src[0] >> 4) & 0x0F);
965
*samples++ = adpcm_ct_expand_nibble(&c->status[1],
968
*samples++ = adpcm_ct_expand_nibble(&c->status[0],
969
(src[0] >> 4) & 0x0F);
970
*samples++ = adpcm_ct_expand_nibble(&c->status[0],
976
case CODEC_ID_ADPCM_SWF:
981
int size = buf_size*8;
983
init_get_bits(&gb, buf, size);
985
// first frame, read bits & inital values
988
c->nb_bits = get_bits(&gb, 2)+2;
989
// av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
992
table = swf_index_tables[c->nb_bits-2];
993
k0 = 1 << (c->nb_bits-2);
994
signmask = 1 << (c->nb_bits-1);
996
while (get_bits_count(&gb) <= size)
1001
// wrap around at every 4096 samples...
1002
if ((c->nb_samples & 0xfff) == 1)
1004
for (i = 0; i <= st; i++)
1006
*samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1007
c->status[i].step_index = get_bits(&gb, 6);
1011
// similar to IMA adpcm
1012
for (i = 0; i <= st; i++)
1014
int delta = get_bits(&gb, c->nb_bits);
1015
int step = step_table[c->status[i].step_index];
1016
long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1027
if (delta & signmask)
1028
c->status[i].predictor -= vpdiff;
1030
c->status[i].predictor += vpdiff;
1032
c->status[i].step_index += table[delta & (~signmask)];
1034
c->status[i].step_index = clip(c->status[i].step_index, 0, 88);
1035
c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
1037
*samples++ = c->status[i].predictor;
1041
// src += get_bits_count(&gb)*8;
1046
case CODEC_ID_ADPCM_YAMAHA:
1047
while (src < buf + buf_size) {
1049
*samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1051
*samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
1052
(src[0] >> 4) & 0x0F);
1054
*samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1056
*samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1057
(src[0] >> 4) & 0x0F);
1065
*data_size = (uint8_t *)samples - (uint8_t *)data;
1071
#ifdef CONFIG_ENCODERS
1072
#define ADPCM_ENCODER(id,name) \
1073
AVCodec name ## _encoder = { \
1077
sizeof(ADPCMContext), \
1078
adpcm_encode_init, \
1079
adpcm_encode_frame, \
1080
adpcm_encode_close, \
1084
#define ADPCM_ENCODER(id,name)
1087
#ifdef CONFIG_DECODERS
1088
#define ADPCM_DECODER(id,name) \
1089
AVCodec name ## _decoder = { \
1093
sizeof(ADPCMContext), \
1094
adpcm_decode_init, \
1097
adpcm_decode_frame, \
1100
#define ADPCM_DECODER(id,name)
1103
#define ADPCM_CODEC(id, name) \
1104
ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
1106
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
1107
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
1108
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
1109
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
1110
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
1111
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
1112
ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
1113
ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
1114
ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
1115
ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
1116
ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
1117
ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
1118
ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
1119
ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);