3
3
* Copyright (c) 2001-2003 The ffmpeg Project
5
* This library is free software; you can redistribute it and/or
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
6
8
* modify it under the terms of the GNU Lesser General Public
7
9
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
12
* FFmpeg is distributed in the hope that it will be useful,
11
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
15
* Lesser General Public License for more details.
15
17
* 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
18
* License along with FFmpeg; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
21
#include "avcodec.h"
22
#include "bitstream.h"
24
* First version by Francois Revol revol@free.fr
25
* Fringe ADPCM codecs (e.g., DK3 and DK4)
27
* First version by Francois Revol (revol@free.fr)
28
* Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
26
29
* by Mike Melanson (melanson@pcisys.net)
30
* CD-ROM XA ADPCM codec by BERO
31
* EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
28
33
* Features and limitations:
82
92
0, -256, 0, 64, 0, -208, -232
95
/* These are for CD-ROM XA ADPCM */
96
static const int xa_adpcm_table[5][2] = {
104
static const int ea_adpcm_table[] = {
105
0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
106
3, 4, 7, 8, 10, 11, 0, -1, -3, -4
109
static const int ct_adpcm_table[8] = {
110
0x00E6, 0x00E6, 0x00E6, 0x00E6,
111
0x0133, 0x0199, 0x0200, 0x0266
114
// padded to zero where table size is less then 16
115
static const int swf_index_tables[4][16] = {
117
/*3*/ { -1, -1, 2, 4 },
118
/*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
119
/*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
122
static const int yamaha_indexscale[] = {
123
230, 230, 230, 230, 307, 409, 512, 614,
124
230, 230, 230, 230, 307, 409, 512, 614
127
static const int yamaha_difflookup[] = {
128
1, 3, 5, 7, 9, 11, 13, 15,
129
-1, -3, -5, -7, -9, -11, -13, -15
85
132
/* end of tables */
87
134
typedef struct ADPCMChannelStatus {
149
202
static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
152
unsigned char nibble;
154
int sign = 0; /* sign bit of the nibble (MSB) */
155
int delta, predicted_delta;
157
delta = sample - c->prev_sample;
164
step_index = c->step_index;
166
/* nibble = 4 * delta / step_table[step_index]; */
167
nibble = (delta << 2) / step_table[step_index];
172
step_index += index_table[nibble];
178
/* what the decoder will find */
179
predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
182
c->prev_sample -= predicted_delta;
184
c->prev_sample += predicted_delta;
204
int delta = sample - c->prev_sample;
205
int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
206
c->prev_sample = c->prev_sample + ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
186
207
CLAMP_TO_SHORT(c->prev_sample);
189
nibble += sign << 3; /* sign * 8 */
192
c->step_index = step_index;
208
c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
212
static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
214
int predictor, nibble, bias;
216
predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
218
nibble= sample - predictor;
219
if(nibble>=0) bias= c->idelta/2;
220
else bias=-c->idelta/2;
222
nibble= (nibble + bias) / c->idelta;
223
nibble= av_clip(nibble, -8, 7)&0x0F;
225
predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
226
CLAMP_TO_SHORT(predictor);
228
c->sample2 = c->sample1;
229
c->sample1 = predictor;
231
c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
232
if (c->idelta < 16) c->idelta = 16;
237
static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
246
delta = sample - c->predictor;
248
nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
250
c->predictor = c->predictor + ((c->step * yamaha_difflookup[nibble]) / 8);
251
CLAMP_TO_SHORT(c->predictor);
252
c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
253
c->step = av_clip(c->step, 127, 24567);
258
typedef struct TrellisPath {
263
typedef struct TrellisNode {
271
static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
272
uint8_t *dst, ADPCMChannelStatus *c, int n)
274
#define FREEZE_INTERVAL 128
275
//FIXME 6% faster if frontier is a compile-time constant
276
const int frontier = 1 << avctx->trellis;
277
const int stride = avctx->channels;
278
const int version = avctx->codec->id;
279
const int max_paths = frontier*FREEZE_INTERVAL;
280
TrellisPath paths[max_paths], *p;
281
TrellisNode node_buf[2][frontier];
282
TrellisNode *nodep_buf[2][frontier];
283
TrellisNode **nodes = nodep_buf[0]; // nodes[] is always sorted by .ssd
284
TrellisNode **nodes_next = nodep_buf[1];
285
int pathn = 0, froze = -1, i, j, k;
287
assert(!(max_paths&(max_paths-1)));
289
memset(nodep_buf, 0, sizeof(nodep_buf));
290
nodes[0] = &node_buf[1][0];
293
nodes[0]->step = c->step_index;
294
nodes[0]->sample1 = c->sample1;
295
nodes[0]->sample2 = c->sample2;
296
if(version == CODEC_ID_ADPCM_IMA_WAV)
297
nodes[0]->sample1 = c->prev_sample;
298
if(version == CODEC_ID_ADPCM_MS)
299
nodes[0]->step = c->idelta;
300
if(version == CODEC_ID_ADPCM_YAMAHA) {
302
nodes[0]->step = 127;
303
nodes[0]->sample1 = 0;
305
nodes[0]->step = c->step;
306
nodes[0]->sample1 = c->predictor;
311
TrellisNode *t = node_buf[i&1];
313
int sample = samples[i*stride];
314
memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
315
for(j=0; j<frontier && nodes[j]; j++) {
316
// higher j have higher ssd already, so they're unlikely to use a suboptimal next sample too
317
const int range = (j < frontier/2) ? 1 : 0;
318
const int step = nodes[j]->step;
320
if(version == CODEC_ID_ADPCM_MS) {
321
const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 256;
322
const int div = (sample - predictor) / step;
323
const int nmin = av_clip(div-range, -8, 6);
324
const int nmax = av_clip(div+range, -7, 7);
325
for(nidx=nmin; nidx<=nmax; nidx++) {
326
const int nibble = nidx & 0xf;
327
int dec_sample = predictor + nidx * step;
328
#define STORE_NODE(NAME, STEP_INDEX)\
331
CLAMP_TO_SHORT(dec_sample);\
332
d = sample - dec_sample;\
333
ssd = nodes[j]->ssd + d*d;\
334
if(nodes_next[frontier-1] && ssd >= nodes_next[frontier-1]->ssd)\
336
/* Collapse any two states with the same previous sample value. \
337
* One could also distinguish states by step and by 2nd to last
338
* sample, but the effects of that are negligible. */\
339
for(k=0; k<frontier && nodes_next[k]; k++) {\
340
if(dec_sample == nodes_next[k]->sample1) {\
341
assert(ssd >= nodes_next[k]->ssd);\
345
for(k=0; k<frontier; k++) {\
346
if(!nodes_next[k] || ssd < nodes_next[k]->ssd) {\
347
TrellisNode *u = nodes_next[frontier-1];\
349
assert(pathn < max_paths);\
354
u->step = STEP_INDEX;\
355
u->sample2 = nodes[j]->sample1;\
356
u->sample1 = dec_sample;\
357
paths[u->path].nibble = nibble;\
358
paths[u->path].prev = nodes[j]->path;\
359
memmove(&nodes_next[k+1], &nodes_next[k], (frontier-k-1)*sizeof(TrellisNode*));\
365
STORE_NODE(ms, FFMAX(16, (AdaptationTable[nibble] * step) >> 8));
367
} else if(version == CODEC_ID_ADPCM_IMA_WAV) {
368
#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
369
const int predictor = nodes[j]->sample1;\
370
const int div = (sample - predictor) * 4 / STEP_TABLE;\
371
int nmin = av_clip(div-range, -7, 6);\
372
int nmax = av_clip(div+range, -6, 7);\
373
if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
375
for(nidx=nmin; nidx<=nmax; nidx++) {\
376
const int nibble = nidx<0 ? 7-nidx : nidx;\
377
int dec_sample = predictor + (STEP_TABLE * yamaha_difflookup[nibble]) / 8;\
378
STORE_NODE(NAME, STEP_INDEX);\
380
LOOP_NODES(ima, step_table[step], av_clip(step + index_table[nibble], 0, 88));
381
} else { //CODEC_ID_ADPCM_YAMAHA
382
LOOP_NODES(yamaha, step, av_clip((step * yamaha_indexscale[nibble]) >> 8, 127, 24567));
393
if(nodes[0]->ssd > (1<<28)) {
394
for(j=1; j<frontier && nodes[j]; j++)
395
nodes[j]->ssd -= nodes[0]->ssd;
399
// merge old paths to save memory
400
if(i == froze + FREEZE_INTERVAL) {
401
p = &paths[nodes[0]->path];
402
for(k=i; k>froze; k--) {
408
// other nodes might use paths that don't coincide with the frozen one.
409
// checking which nodes do so is too slow, so just kill them all.
410
// this also slightly improves quality, but I don't know why.
411
memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
415
p = &paths[nodes[0]->path];
416
for(i=n-1; i>froze; i--) {
421
c->predictor = nodes[0]->sample1;
422
c->sample1 = nodes[0]->sample1;
423
c->sample2 = nodes[0]->sample2;
424
c->step_index = nodes[0]->step;
425
c->step = nodes[0]->step;
426
c->idelta = nodes[0]->step;
197
429
static int adpcm_encode_frame(AVCodecContext *avctx,
198
unsigned char *frame, int buf_size, void *data)
430
unsigned char *frame, int buf_size, void *data)
202
434
unsigned char *dst;
203
435
ADPCMContext *c = avctx->priv_data;
206
438
samples = (short *)data;
439
st= avctx->channels == 2;
207
440
/* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
209
442
switch(avctx->codec->id) {
260
511
samples += 8 * avctx->channels;
514
case CODEC_ID_ADPCM_MS:
515
for(i=0; i<avctx->channels; i++){
519
c->status[i].coeff1 = AdaptCoeff1[predictor];
520
c->status[i].coeff2 = AdaptCoeff2[predictor];
522
for(i=0; i<avctx->channels; i++){
523
if (c->status[i].idelta < 16)
524
c->status[i].idelta = 16;
526
*dst++ = c->status[i].idelta & 0xFF;
527
*dst++ = c->status[i].idelta >> 8;
529
for(i=0; i<avctx->channels; i++){
530
c->status[i].sample1= *samples++;
532
*dst++ = c->status[i].sample1 & 0xFF;
533
*dst++ = c->status[i].sample1 >> 8;
535
for(i=0; i<avctx->channels; i++){
536
c->status[i].sample2= *samples++;
538
*dst++ = c->status[i].sample2 & 0xFF;
539
*dst++ = c->status[i].sample2 >> 8;
542
if(avctx->trellis > 0) {
543
int n = avctx->block_align - 7*avctx->channels;
545
if(avctx->channels == 1) {
547
adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
549
*dst++ = (buf[0][i] << 4) | buf[0][i+1];
551
adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
552
adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
554
*dst++ = (buf[0][i] << 4) | buf[1][i];
557
for(i=7*avctx->channels; i<avctx->block_align; i++) {
559
nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
560
nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
564
case CODEC_ID_ADPCM_YAMAHA:
565
n = avctx->frame_size / 2;
566
if(avctx->trellis > 0) {
569
if(avctx->channels == 1) {
570
adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
572
*dst++ = buf[0][i] | (buf[0][i+1] << 4);
574
adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
575
adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
577
*dst++ = buf[0][i] | (buf[1][i] << 4);
581
for(i = 0; i < avctx->channels; i++) {
583
nibble = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
584
nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
587
samples += 2 * avctx->channels;
266
593
return dst - frame;
595
#endif //CONFIG_ENCODERS
269
597
static int adpcm_decode_init(AVCodecContext * avctx)
271
599
ADPCMContext *c = avctx->priv_data;
601
if(avctx->channels > 2U){
274
606
c->status[0].predictor = c->status[1].predictor = 0;
275
607
c->status[0].step_index = c->status[1].step_index = 0;
276
608
c->status[0].step = c->status[1].step = 0;
278
610
switch(avctx->codec->id) {
611
case CODEC_ID_ADPCM_CT:
612
c->status[0].step = c->status[1].step = 511;
285
static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble)
620
static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
348
656
c->sample2 = c->sample1;
349
657
c->sample1 = predictor;
350
c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256;
658
c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
351
659
if (c->idelta < 16) c->idelta = 16;
353
661
return (short)predictor;
664
static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
667
int sign, delta, diff;
672
/* perform direct multiplication instead of series of jumps proposed by
673
* the reference ADPCM implementation since modern CPUs can do the mults
675
diff = ((2 * delta + 1) * c->step) >> 3;
676
predictor = c->predictor;
677
/* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
679
predictor = ((predictor * 254) >> 8) - diff;
681
predictor = ((predictor * 254) >> 8) + diff;
682
/* calculate new step and clamp it to range 511..32767 */
683
new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
690
CLAMP_TO_SHORT(predictor);
691
c->predictor = predictor;
692
return (short)predictor;
695
static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
697
int sign, delta, diff;
699
sign = nibble & (1<<(size-1));
700
delta = nibble & ((1<<(size-1))-1);
701
diff = delta << (7 + c->step + shift);
704
c->predictor -= diff;
706
c->predictor += diff;
709
if (c->predictor > 16256)
710
c->predictor = 16256;
711
else if (c->predictor < -16384)
712
c->predictor = -16384;
714
/* calculate new step */
715
if (delta >= (2*size - 3) && c->step < 3)
717
else if (delta == 0 && c->step > 0)
720
return (short) c->predictor;
723
static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
730
c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
731
CLAMP_TO_SHORT(c->predictor);
732
c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
733
c->step = av_clip(c->step, 127, 24567);
737
static void xa_decode(short *out, const unsigned char *in,
738
ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
741
int shift,filter,f0,f1;
747
shift = 12 - (in[4+i*2] & 15);
748
filter = in[4+i*2] >> 4;
749
f0 = xa_adpcm_table[filter][0];
750
f1 = xa_adpcm_table[filter][1];
758
t = (signed char)(d<<4)>>4;
759
s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
767
if (inc==2) { /* stereo */
770
s_1 = right->sample1;
771
s_2 = right->sample2;
772
out = out + 1 - 28*2;
775
shift = 12 - (in[5+i*2] & 15);
776
filter = in[5+i*2] >> 4;
778
f0 = xa_adpcm_table[filter][0];
779
f1 = xa_adpcm_table[filter][1];
784
t = (signed char)d >> 4;
785
s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
793
if (inc==2) { /* stereo */
794
right->sample1 = s_1;
795
right->sample2 = s_2;
356
805
/* DK3 ADPCM support macro */
357
806
#define DK3_GET_NEXT_NIBBLE() \
358
807
if (decode_top_nibble_next) \
412
882
cs->step_index = (*src++) & 0x7F;
414
if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index);
415
if (cs->step_index > 88) cs->step_index = 88;
884
if (cs->step_index > 88){
885
av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
417
889
cs->step = step_table[cs->step_index];
419
891
if (st && channel)
422
*samples++ = cs->predictor;
425
894
for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
426
*samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F);
895
*samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
427
896
samples += avctx->channels;
428
*samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
897
*samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
429
898
samples += avctx->channels;
433
902
if(st) { /* handle stereo interlacing */
434
903
c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
435
if(channel == 0) { /* wait for the other packet before outputing anything */
904
if(channel == 1) { /* wait for the other packet before outputing anything */
437
905
return src - buf;
441
909
case CODEC_ID_ADPCM_IMA_WAV:
442
if (buf_size > BLKSIZE) {
443
if (avctx->block_align != 0)
444
buf_size = avctx->block_align;
448
// XXX: do as per-channel loop
449
cs = &(c->status[0]);
450
cs->predictor = (*src++) & 0x0FF;
451
cs->predictor |= ((*src++) << 8) & 0x0FF00;
452
if(cs->predictor & 0x8000)
453
cs->predictor -= 0x10000;
454
CLAMP_TO_SHORT(cs->predictor);
456
// XXX: is this correct ??: *samples++ = cs->predictor;
458
cs->step_index = *src++;
459
if (cs->step_index < 0) cs->step_index = 0;
460
if (cs->step_index > 88) cs->step_index = 88;
461
if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */
464
cs = &(c->status[1]);
465
cs->predictor = (*src++) & 0x0FF;
466
cs->predictor |= ((*src++) << 8) & 0x0FF00;
467
if(cs->predictor & 0x8000)
468
cs->predictor -= 0x10000;
469
CLAMP_TO_SHORT(cs->predictor);
471
// XXX: is this correct ??: *samples++ = cs->predictor;
473
cs->step_index = *src++;
474
if (cs->step_index < 0) cs->step_index = 0;
475
if (cs->step_index > 88) cs->step_index = 88;
476
src++; /* if != 0 -> out-of-sync */
479
for(m=4; src < (buf + buf_size);) {
480
*samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
482
*samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F);
483
*samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
485
*samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
910
if (avctx->block_align != 0 && buf_size > avctx->block_align)
911
buf_size = avctx->block_align;
913
// samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
915
for(i=0; i<avctx->channels; i++){
916
cs = &(c->status[i]);
917
cs->predictor = (int16_t)(src[0] + (src[1]<<8));
920
// XXX: is this correct ??: *samples++ = cs->predictor;
922
cs->step_index = *src++;
923
if (cs->step_index > 88){
924
av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
927
if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
930
while(src < buf + buf_size){
933
*samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
935
*samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
494
941
case CODEC_ID_ADPCM_4XM:
495
942
cs = &(c->status[0]);
502
949
c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
504
// if (cs->step_index < 0) cs->step_index = 0;
505
// if (cs->step_index > 88) cs->step_index = 88;
951
if (cs->step_index < 0) cs->step_index = 0;
952
if (cs->step_index > 88) cs->step_index = 88;
507
954
m= (buf_size - (src - buf))>>st;
508
//printf("%d %d %d %d\n", st, m, c->status[0].predictor, c->status[0].step_index);
509
//FIXME / XXX decode chanels individual & interleave samples
510
955
for(i=0; i<m; i++) {
511
*samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] & 0x0F);
513
*samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] & 0x0F);
514
*samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] >> 4);
516
*samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] >> 4);
956
*samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
958
*samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
959
*samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
961
*samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
522
967
case CODEC_ID_ADPCM_MS:
524
if (buf_size > BLKSIZE) {
525
if (avctx->block_align != 0)
526
buf_size = avctx->block_align;
968
if (avctx->block_align != 0 && buf_size > avctx->block_align)
969
buf_size = avctx->block_align;
530
970
n = buf_size - 7 * avctx->channels;
533
block_predictor[0] = (*src++); /* should be bound */
534
block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
973
block_predictor[0] = av_clip(*src++, 0, 7);
535
974
block_predictor[1] = 0;
537
block_predictor[1] = (*src++);
538
block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
539
c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
540
if (c->status[0].idelta & 0x08000)
541
c->status[0].idelta -= 0x10000;
976
block_predictor[1] = av_clip(*src++, 0, 7);
977
c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
544
c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
545
if (st && c->status[1].idelta & 0x08000)
546
c->status[1].idelta |= 0xFFFF0000;
980
c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
549
983
c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
550
984
c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
551
985
c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
552
986
c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
554
988
c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
556
990
if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
573
1007
case CODEC_ID_ADPCM_IMA_DK4:
574
if (buf_size > BLKSIZE) {
575
if (avctx->block_align != 0)
576
buf_size = avctx->block_align;
580
c->status[0].predictor = (src[0] | (src[1] << 8));
1008
if (avctx->block_align != 0 && buf_size > avctx->block_align)
1009
buf_size = avctx->block_align;
1011
c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
581
1012
c->status[0].step_index = src[2];
583
if(c->status[0].predictor & 0x8000)
584
c->status[0].predictor -= 0x10000;
585
1014
*samples++ = c->status[0].predictor;
587
c->status[1].predictor = (src[0] | (src[1] << 8));
1016
c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
588
1017
c->status[1].step_index = src[2];
590
if(c->status[1].predictor & 0x8000)
591
c->status[1].predictor -= 0x10000;
592
1019
*samples++ = c->status[1].predictor;
594
1021
while (src < buf + buf_size) {
596
1023
/* take care of the top nibble (always left or mono channel) */
597
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
598
(src[0] >> 4) & 0x0F);
1024
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
1025
(src[0] >> 4) & 0x0F, 3);
600
1027
/* take care of the bottom nibble, which is right sample for
601
1028
* stereo, or another mono sample */
603
*samples++ = adpcm_ima_expand_nibble(&c->status[1],
1030
*samples++ = adpcm_ima_expand_nibble(&c->status[1],
606
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
1033
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
612
1039
case CODEC_ID_ADPCM_IMA_DK3:
613
if (buf_size > BLKSIZE) {
614
if (avctx->block_align != 0)
615
buf_size = avctx->block_align;
619
c->status[0].predictor = (src[10] | (src[11] << 8));
620
c->status[1].predictor = (src[12] | (src[13] << 8));
1040
if (avctx->block_align != 0 && buf_size > avctx->block_align)
1041
buf_size = avctx->block_align;
1043
if(buf_size + 16 > (samples_end - samples)*3/8)
1046
c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
1047
c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
621
1048
c->status[0].step_index = src[14];
622
1049
c->status[1].step_index = src[15];
623
1050
/* sign extend the predictors */
624
if(c->status[0].predictor & 0x8000)
625
c->status[0].predictor -= 0x10000;
626
if(c->status[1].predictor & 0x8000)
627
c->status[1].predictor -= 0x10000;
629
1052
diff_channel = c->status[1].predictor;
658
1081
*samples++ = c->status[0].predictor - c->status[1].predictor;
1084
case CODEC_ID_ADPCM_IMA_WS:
1085
/* no per-block initialization; just start decoding the data */
1086
while (src < buf + buf_size) {
1089
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
1090
(src[0] >> 4) & 0x0F, 3);
1091
*samples++ = adpcm_ima_expand_nibble(&c->status[1],
1094
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
1095
(src[0] >> 4) & 0x0F, 3);
1096
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
1103
case CODEC_ID_ADPCM_XA:
1104
c->status[0].sample1 = c->status[0].sample2 =
1105
c->status[1].sample1 = c->status[1].sample2 = 0;
1106
while (buf_size >= 128) {
1107
xa_decode(samples, src, &c->status[0], &c->status[1],
1114
case CODEC_ID_ADPCM_EA:
1115
samples_in_chunk = AV_RL32(src);
1116
if (samples_in_chunk >= ((buf_size - 12) * 2)) {
1121
current_left_sample = (int16_t)AV_RL16(src);
1123
previous_left_sample = (int16_t)AV_RL16(src);
1125
current_right_sample = (int16_t)AV_RL16(src);
1127
previous_right_sample = (int16_t)AV_RL16(src);
1130
for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
1131
coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
1132
coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
1133
coeff1r = ea_adpcm_table[*src & 0x0F];
1134
coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
1137
shift_left = ((*src >> 4) & 0x0F) + 8;
1138
shift_right = (*src & 0x0F) + 8;
1141
for (count2 = 0; count2 < 28; count2++) {
1142
next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
1143
next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
1146
next_left_sample = (next_left_sample +
1147
(current_left_sample * coeff1l) +
1148
(previous_left_sample * coeff2l) + 0x80) >> 8;
1149
next_right_sample = (next_right_sample +
1150
(current_right_sample * coeff1r) +
1151
(previous_right_sample * coeff2r) + 0x80) >> 8;
1152
CLAMP_TO_SHORT(next_left_sample);
1153
CLAMP_TO_SHORT(next_right_sample);
1155
previous_left_sample = current_left_sample;
1156
current_left_sample = next_left_sample;
1157
previous_right_sample = current_right_sample;
1158
current_right_sample = next_right_sample;
1159
*samples++ = (unsigned short)current_left_sample;
1160
*samples++ = (unsigned short)current_right_sample;
1164
case CODEC_ID_ADPCM_IMA_SMJPEG:
1165
c->status[0].predictor = *src;
1167
c->status[0].step_index = *src++;
1168
src++; /* skip another byte before getting to the meat */
1169
while (src < buf + buf_size) {
1170
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
1172
*samples++ = adpcm_ima_expand_nibble(&c->status[0],
1173
(*src >> 4) & 0x0F, 3);
1177
case CODEC_ID_ADPCM_CT:
1178
while (src < buf + buf_size) {
1180
*samples++ = adpcm_ct_expand_nibble(&c->status[0],
1181
(src[0] >> 4) & 0x0F);
1182
*samples++ = adpcm_ct_expand_nibble(&c->status[1],
1185
*samples++ = adpcm_ct_expand_nibble(&c->status[0],
1186
(src[0] >> 4) & 0x0F);
1187
*samples++ = adpcm_ct_expand_nibble(&c->status[0],
1193
case CODEC_ID_ADPCM_SBPRO_4:
1194
case CODEC_ID_ADPCM_SBPRO_3:
1195
case CODEC_ID_ADPCM_SBPRO_2:
1196
if (!c->status[0].step_index) {
1197
/* the first byte is a raw sample */
1198
*samples++ = 128 * (*src++ - 0x80);
1200
*samples++ = 128 * (*src++ - 0x80);
1201
c->status[0].step_index = 1;
1203
if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
1204
while (src < buf + buf_size) {
1205
*samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1206
(src[0] >> 4) & 0x0F, 4, 0);
1207
*samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1208
src[0] & 0x0F, 4, 0);
1211
} else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
1212
while (src < buf + buf_size && samples + 2 < samples_end) {
1213
*samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1214
(src[0] >> 5) & 0x07, 3, 0);
1215
*samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1216
(src[0] >> 2) & 0x07, 3, 0);
1217
*samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1218
src[0] & 0x03, 2, 0);
1222
while (src < buf + buf_size && samples + 3 < samples_end) {
1223
*samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1224
(src[0] >> 6) & 0x03, 2, 2);
1225
*samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1226
(src[0] >> 4) & 0x03, 2, 2);
1227
*samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1228
(src[0] >> 2) & 0x03, 2, 2);
1229
*samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1230
src[0] & 0x03, 2, 2);
1235
case CODEC_ID_ADPCM_SWF:
1239
int k0, signmask, nb_bits;
1240
int size = buf_size*8;
1242
init_get_bits(&gb, buf, size);
1244
//read bits & inital values
1245
nb_bits = get_bits(&gb, 2)+2;
1246
//av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
1247
table = swf_index_tables[nb_bits-2];
1248
k0 = 1 << (nb_bits-2);
1249
signmask = 1 << (nb_bits-1);
1251
for (i = 0; i < avctx->channels; i++) {
1252
*samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1253
c->status[i].step_index = get_bits(&gb, 6);
1256
while (get_bits_count(&gb) < size)
1260
for (i = 0; i < avctx->channels; i++) {
1261
// similar to IMA adpcm
1262
int delta = get_bits(&gb, nb_bits);
1263
int step = step_table[c->status[i].step_index];
1264
long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1275
if (delta & signmask)
1276
c->status[i].predictor -= vpdiff;
1278
c->status[i].predictor += vpdiff;
1280
c->status[i].step_index += table[delta & (~signmask)];
1282
c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
1283
c->status[i].predictor = av_clip(c->status[i].predictor, -32768, 32767);
1285
*samples++ = c->status[i].predictor;
1286
if (samples >= samples_end) {
1287
av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
1295
case CODEC_ID_ADPCM_YAMAHA:
1296
while (src < buf + buf_size) {
1298
*samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1300
*samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
1301
(src[0] >> 4) & 0x0F);
1303
*samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1305
*samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1306
(src[0] >> 4) & 0x0F);
665
1314
*data_size = (uint8_t *)samples - (uint8_t *)data;
666
1315
return src - buf;
669
#define ADPCM_CODEC(id, name) \
1320
#ifdef CONFIG_ENCODERS
1321
#define ADPCM_ENCODER(id,name) \
670
1322
AVCodec name ## _encoder = { \
672
1324
CODEC_TYPE_AUDIO, \