~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/adpcm.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc, Andrew Starr-Bochicchio, Lionel Le Folgoc
  • Date: 2008-12-26 00:10:06 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081226001006-2040ls9680bd1blt
Tags: 1.1.7-0.2ubuntu1
[ Andrew Starr-Bochicchio ]
* Merge from debian-multimedia (LP: #298547), Ubuntu Changes:
 - For ffmpeg-related build-deps, fix versionized dependencies
   as the ubuntu versioning is different than debian-multimedia's.

[ Lionel Le Folgoc ]
* LP: #311412 is fixed since the 1.1.7~rc1-0.1 revision.
* debian/patches/03_ffmpeg.diff: updated to fix FTBFS due to libswscale API
  change (cherry-pick from Gentoo #234383).

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 */
21
21
#include "avcodec.h"
22
22
#include "bitstream.h"
 
23
#include "bytestream.h"
23
24
 
24
25
/**
25
26
 * @file adpcm.c
29
30
 *   by Mike Melanson (melanson@pcisys.net)
30
31
 * CD-ROM XA ADPCM codec by BERO
31
32
 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
 
33
 * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
 
34
 * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
 
35
 * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
 
36
 * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
 
37
 * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
 
38
 * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
32
39
 *
33
40
 * Features and limitations:
34
41
 *
48
55
 
49
56
#define BLKSIZE 1024
50
57
 
51
 
#define CLAMP_TO_SHORT(value) \
52
 
if (value > 32767) \
53
 
    value = 32767; \
54
 
else if (value < -32768) \
55
 
    value = -32768; \
56
 
 
57
58
/* step_table[] and index_table[] are from the ADPCM reference source */
58
59
/* This is the index table: */
59
60
static const int index_table[16] = {
147
148
} ADPCMChannelStatus;
148
149
 
149
150
typedef struct ADPCMContext {
150
 
    int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
151
 
    ADPCMChannelStatus status[2];
152
 
    short sample_buffer[32]; /* hold left samples while waiting for right samples */
 
151
    ADPCMChannelStatus status[6];
153
152
} ADPCMContext;
154
153
 
155
154
/* XXX: implement encoding */
160
159
    if (avctx->channels > 2)
161
160
        return -1; /* only stereo or mono =) */
162
161
    switch(avctx->codec->id) {
163
 
    case CODEC_ID_ADPCM_IMA_QT:
164
 
        av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n");
165
 
        avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
166
 
        return -1;
167
 
        break;
168
162
    case CODEC_ID_ADPCM_IMA_WAV:
169
163
        avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
170
164
                                                             /* and we have 4 bytes per channel overhead */
171
165
        avctx->block_align = BLKSIZE;
172
166
        /* seems frame_size isn't taken into account... have to buffer the samples :-( */
173
167
        break;
 
168
    case CODEC_ID_ADPCM_IMA_QT:
 
169
        avctx->frame_size = 64;
 
170
        avctx->block_align = 34 * avctx->channels;
 
171
        break;
174
172
    case CODEC_ID_ADPCM_MS:
175
173
        avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
176
174
                                                             /* and we have 7 bytes per channel overhead */
180
178
        avctx->frame_size = BLKSIZE * avctx->channels;
181
179
        avctx->block_align = BLKSIZE;
182
180
        break;
 
181
    case CODEC_ID_ADPCM_SWF:
 
182
        if (avctx->sample_rate != 11025 &&
 
183
            avctx->sample_rate != 22050 &&
 
184
            avctx->sample_rate != 44100) {
 
185
            av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
 
186
            return -1;
 
187
        }
 
188
        avctx->frame_size = 512 * (avctx->sample_rate / 11025);
 
189
        break;
183
190
    default:
184
191
        return -1;
185
192
        break;
203
210
{
204
211
    int delta = sample - c->prev_sample;
205
212
    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);
207
 
    CLAMP_TO_SHORT(c->prev_sample);
 
213
    c->prev_sample += ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
 
214
    c->prev_sample = av_clip_int16(c->prev_sample);
208
215
    c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
209
216
    return nibble;
210
217
}
223
230
    nibble= av_clip(nibble, -8, 7)&0x0F;
224
231
 
225
232
    predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
226
 
    CLAMP_TO_SHORT(predictor);
227
233
 
228
234
    c->sample2 = c->sample1;
229
 
    c->sample1 = predictor;
 
235
    c->sample1 = av_clip_int16(predictor);
230
236
 
231
237
    c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
232
238
    if (c->idelta < 16) c->idelta = 16;
247
253
 
248
254
    nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
249
255
 
250
 
    c->predictor = c->predictor + ((c->step * yamaha_difflookup[nibble]) / 8);
251
 
    CLAMP_TO_SHORT(c->predictor);
 
256
    c->predictor += ((c->step * yamaha_difflookup[nibble]) / 8);
 
257
    c->predictor = av_clip_int16(c->predictor);
252
258
    c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
253
259
    c->step = av_clip(c->step, 127, 24567);
254
260
 
293
299
    nodes[0]->step = c->step_index;
294
300
    nodes[0]->sample1 = c->sample1;
295
301
    nodes[0]->sample2 = c->sample2;
296
 
    if(version == CODEC_ID_ADPCM_IMA_WAV)
 
302
    if((version == CODEC_ID_ADPCM_IMA_WAV) || (version == CODEC_ID_ADPCM_IMA_QT) || (version == CODEC_ID_ADPCM_SWF))
297
303
        nodes[0]->sample1 = c->prev_sample;
298
304
    if(version == CODEC_ID_ADPCM_MS)
299
305
        nodes[0]->step = c->idelta;
328
334
#define STORE_NODE(NAME, STEP_INDEX)\
329
335
                    int d;\
330
336
                    uint32_t ssd;\
331
 
                    CLAMP_TO_SHORT(dec_sample);\
 
337
                    dec_sample = av_clip_int16(dec_sample);\
332
338
                    d = sample - dec_sample;\
333
339
                    ssd = nodes[j]->ssd + d*d;\
334
340
                    if(nodes_next[frontier-1] && ssd >= nodes_next[frontier-1]->ssd)\
364
370
                    next_##NAME:;
365
371
                    STORE_NODE(ms, FFMAX(16, (AdaptationTable[nibble] * step) >> 8));
366
372
                }
367
 
            } else if(version == CODEC_ID_ADPCM_IMA_WAV) {
 
373
            } else if((version == CODEC_ID_ADPCM_IMA_WAV)|| (version == CODEC_ID_ADPCM_IMA_QT)|| (version == CODEC_ID_ADPCM_SWF)) {
368
374
#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
369
375
                const int predictor = nodes[j]->sample1;\
370
376
                const int div = (sample - predictor) * 4 / STEP_TABLE;\
440
446
/*    n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
441
447
 
442
448
    switch(avctx->codec->id) {
443
 
    case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
444
 
        break;
445
449
    case CODEC_ID_ADPCM_IMA_WAV:
446
450
        n = avctx->frame_size / 8;
447
451
            c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
448
452
/*            c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
449
 
            *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
450
 
            *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
 
453
            bytestream_put_le16(&dst, c->status[0].prev_sample);
451
454
            *dst++ = (unsigned char)c->status[0].step_index;
452
455
            *dst++ = 0; /* unknown */
453
456
            samples++;
454
457
            if (avctx->channels == 2) {
455
 
                c->status[1].prev_sample = (signed short)samples[1];
 
458
                c->status[1].prev_sample = (signed short)samples[0];
456
459
/*                c->status[1].step_index = 0; */
457
 
                *dst++ = (c->status[1].prev_sample) & 0xFF;
458
 
                *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
 
460
                bytestream_put_le16(&dst, c->status[1].prev_sample);
459
461
                *dst++ = (unsigned char)c->status[1].step_index;
460
462
                *dst++ = 0;
461
463
                samples++;
481
483
                }
482
484
            } else
483
485
            for (; n>0; n--) {
484
 
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
485
 
                *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
486
 
                dst++;
487
 
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
488
 
                *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
489
 
                dst++;
490
 
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
491
 
                *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
492
 
                dst++;
493
 
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
494
 
                *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
 
486
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]);
 
487
                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4;
 
488
                dst++;
 
489
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]);
 
490
                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4;
 
491
                dst++;
 
492
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]);
 
493
                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4;
 
494
                dst++;
 
495
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]);
 
496
                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4;
495
497
                dst++;
496
498
                /* right channel */
497
499
                if (avctx->channels == 2) {
511
513
                samples += 8 * avctx->channels;
512
514
            }
513
515
        break;
 
516
    case CODEC_ID_ADPCM_IMA_QT:
 
517
    {
 
518
        int ch, i;
 
519
        PutBitContext pb;
 
520
        init_put_bits(&pb, dst, buf_size*8);
 
521
 
 
522
        for(ch=0; ch<avctx->channels; ch++){
 
523
            put_bits(&pb, 9, (c->status[ch].prev_sample + 0x10000) >> 7);
 
524
            put_bits(&pb, 7, c->status[ch].step_index);
 
525
            if(avctx->trellis > 0) {
 
526
                uint8_t buf[64];
 
527
                adpcm_compress_trellis(avctx, samples+ch, buf, &c->status[ch], 64);
 
528
                for(i=0; i<64; i++)
 
529
                    put_bits(&pb, 4, buf[i^1]);
 
530
                c->status[ch].prev_sample = c->status[ch].predictor & ~0x7F;
 
531
            } else {
 
532
                for (i=0; i<64; i+=2){
 
533
                    int t1, t2;
 
534
                    t1 = adpcm_ima_compress_sample(&c->status[ch], samples[avctx->channels*(i+0)+ch]);
 
535
                    t2 = adpcm_ima_compress_sample(&c->status[ch], samples[avctx->channels*(i+1)+ch]);
 
536
                    put_bits(&pb, 4, t2);
 
537
                    put_bits(&pb, 4, t1);
 
538
                }
 
539
                c->status[ch].prev_sample &= ~0x7F;
 
540
            }
 
541
        }
 
542
 
 
543
        dst += put_bits_count(&pb)>>3;
 
544
        break;
 
545
    }
 
546
    case CODEC_ID_ADPCM_SWF:
 
547
    {
 
548
        int i;
 
549
        PutBitContext pb;
 
550
        init_put_bits(&pb, dst, buf_size*8);
 
551
 
 
552
        n = avctx->frame_size-1;
 
553
 
 
554
        //Store AdpcmCodeSize
 
555
        put_bits(&pb, 2, 2);                //Set 4bits flash adpcm format
 
556
 
 
557
        //Init the encoder state
 
558
        for(i=0; i<avctx->channels; i++){
 
559
            c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63); // clip step so it fits 6 bits
 
560
            put_bits(&pb, 16, samples[i] & 0xFFFF);
 
561
            put_bits(&pb, 6, c->status[i].step_index);
 
562
            c->status[i].prev_sample = (signed short)samples[i];
 
563
        }
 
564
 
 
565
        if(avctx->trellis > 0) {
 
566
            uint8_t buf[2][n];
 
567
            adpcm_compress_trellis(avctx, samples+2, buf[0], &c->status[0], n);
 
568
            if (avctx->channels == 2)
 
569
                adpcm_compress_trellis(avctx, samples+3, buf[1], &c->status[1], n);
 
570
            for(i=0; i<n; i++) {
 
571
                put_bits(&pb, 4, buf[0][i]);
 
572
                if (avctx->channels == 2)
 
573
                    put_bits(&pb, 4, buf[1][i]);
 
574
            }
 
575
        } else {
 
576
            for (i=1; i<avctx->frame_size; i++) {
 
577
                put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]));
 
578
                if (avctx->channels == 2)
 
579
                    put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]));
 
580
            }
 
581
        }
 
582
        flush_put_bits(&pb);
 
583
        dst += put_bits_count(&pb)>>3;
 
584
        break;
 
585
    }
514
586
    case CODEC_ID_ADPCM_MS:
515
587
        for(i=0; i<avctx->channels; i++){
516
588
            int predictor=0;
523
595
            if (c->status[i].idelta < 16)
524
596
                c->status[i].idelta = 16;
525
597
 
526
 
            *dst++ = c->status[i].idelta & 0xFF;
527
 
            *dst++ = c->status[i].idelta >> 8;
 
598
            bytestream_put_le16(&dst, c->status[i].idelta);
528
599
        }
529
600
        for(i=0; i<avctx->channels; i++){
530
601
            c->status[i].sample1= *samples++;
531
602
 
532
 
            *dst++ = c->status[i].sample1 & 0xFF;
533
 
            *dst++ = c->status[i].sample1 >> 8;
 
603
            bytestream_put_le16(&dst, c->status[i].sample1);
534
604
        }
535
605
        for(i=0; i<avctx->channels; i++){
536
606
            c->status[i].sample2= *samples++;
537
607
 
538
 
            *dst++ = c->status[i].sample2 & 0xFF;
539
 
            *dst++ = c->status[i].sample2 >> 8;
 
608
            bytestream_put_le16(&dst, c->status[i].sample2);
540
609
        }
541
610
 
542
611
        if(avctx->trellis > 0) {
594
663
}
595
664
#endif //CONFIG_ENCODERS
596
665
 
597
 
static int adpcm_decode_init(AVCodecContext * avctx)
 
666
static av_cold int adpcm_decode_init(AVCodecContext * avctx)
598
667
{
599
668
    ADPCMContext *c = avctx->priv_data;
 
669
    unsigned int max_channels = 2;
600
670
 
601
 
    if(avctx->channels > 2U){
 
671
    switch(avctx->codec->id) {
 
672
    case CODEC_ID_ADPCM_EA_R1:
 
673
    case CODEC_ID_ADPCM_EA_R2:
 
674
    case CODEC_ID_ADPCM_EA_R3:
 
675
        max_channels = 6;
 
676
        break;
 
677
    }
 
678
    if(avctx->channels > max_channels){
602
679
        return -1;
603
680
    }
604
681
 
605
 
    c->channel = 0;
606
 
    c->status[0].predictor = c->status[1].predictor = 0;
607
 
    c->status[0].step_index = c->status[1].step_index = 0;
608
 
    c->status[0].step = c->status[1].step = 0;
609
 
 
610
682
    switch(avctx->codec->id) {
611
683
    case CODEC_ID_ADPCM_CT:
612
684
        c->status[0].step = c->status[1].step = 511;
613
685
        break;
 
686
    case CODEC_ID_ADPCM_IMA_WS:
 
687
        if (avctx->extradata && avctx->extradata_size == 2 * 4) {
 
688
            c->status[0].predictor = AV_RL32(avctx->extradata);
 
689
            c->status[1].predictor = AV_RL32(avctx->extradata + 4);
 
690
        }
 
691
        break;
614
692
    default:
615
693
        break;
616
694
    }
638
716
    if (sign) predictor -= diff;
639
717
    else predictor += diff;
640
718
 
641
 
    CLAMP_TO_SHORT(predictor);
642
 
    c->predictor = predictor;
 
719
    c->predictor = av_clip_int16(predictor);
643
720
    c->step_index = step_index;
644
721
 
645
 
    return (short)predictor;
 
722
    return (short)c->predictor;
646
723
}
647
724
 
648
725
static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
651
728
 
652
729
    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
653
730
    predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
654
 
    CLAMP_TO_SHORT(predictor);
655
731
 
656
732
    c->sample2 = c->sample1;
657
 
    c->sample1 = predictor;
 
733
    c->sample1 = av_clip_int16(predictor);
658
734
    c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
659
735
    if (c->idelta < 16) c->idelta = 16;
660
736
 
661
 
    return (short)predictor;
 
737
    return c->sample1;
662
738
}
663
739
 
664
740
static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
665
741
{
666
 
    int predictor;
667
742
    int sign, delta, diff;
668
743
    int new_step;
669
744
 
673
748
     * the reference ADPCM implementation since modern CPUs can do the mults
674
749
     * quickly enough */
675
750
    diff = ((2 * delta + 1) * c->step) >> 3;
676
 
    predictor = c->predictor;
677
751
    /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
678
 
    if(sign)
679
 
        predictor = ((predictor * 254) >> 8) - diff;
680
 
    else
681
 
            predictor = ((predictor * 254) >> 8) + diff;
 
752
    c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
 
753
    c->predictor = av_clip_int16(c->predictor);
682
754
    /* calculate new step and clamp it to range 511..32767 */
683
755
    new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
684
 
    c->step = new_step;
685
 
    if(c->step < 511)
686
 
        c->step = 511;
687
 
    if(c->step > 32767)
688
 
        c->step = 32767;
 
756
    c->step = av_clip(new_step, 511, 32767);
689
757
 
690
 
    CLAMP_TO_SHORT(predictor);
691
 
    c->predictor = predictor;
692
 
    return (short)predictor;
 
758
    return (short)c->predictor;
693
759
}
694
760
 
695
761
static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
700
766
    delta = nibble & ((1<<(size-1))-1);
701
767
    diff = delta << (7 + c->step + shift);
702
768
 
703
 
    if (sign)
704
 
        c->predictor -= diff;
705
 
    else
706
 
        c->predictor += diff;
707
 
 
708
769
    /* clamp result */
709
 
    if (c->predictor > 16256)
710
 
        c->predictor = 16256;
711
 
    else if (c->predictor < -16384)
712
 
        c->predictor = -16384;
 
770
    c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
713
771
 
714
772
    /* calculate new step */
715
773
    if (delta >= (2*size - 3) && c->step < 3)
728
786
    }
729
787
 
730
788
    c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
731
 
    CLAMP_TO_SHORT(c->predictor);
 
789
    c->predictor = av_clip_int16(c->predictor);
732
790
    c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
733
791
    c->step = av_clip(c->step, 127, 24567);
734
792
    return c->predictor;
757
815
 
758
816
            t = (signed char)(d<<4)>>4;
759
817
            s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
760
 
            CLAMP_TO_SHORT(s);
761
 
            *out = s;
 
818
            s_2 = s_1;
 
819
            s_1 = av_clip_int16(s);
 
820
            *out = s_1;
762
821
            out += inc;
763
 
            s_2 = s_1;
764
 
            s_1 = s;
765
822
        }
766
823
 
767
824
        if (inc==2) { /* stereo */
783
840
 
784
841
            t = (signed char)d >> 4;
785
842
            s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
786
 
            CLAMP_TO_SHORT(s);
787
 
            *out = s;
 
843
            s_2 = s_1;
 
844
            s_1 = av_clip_int16(s);
 
845
            *out = s_1;
788
846
            out += inc;
789
 
            s_2 = s_1;
790
 
            s_1 = s;
791
847
        }
792
848
 
793
849
        if (inc==2) { /* stereo */
806
862
#define DK3_GET_NEXT_NIBBLE() \
807
863
    if (decode_top_nibble_next) \
808
864
    { \
809
 
        nibble = (last_byte >> 4) & 0x0F; \
 
865
        nibble = last_byte >> 4; \
810
866
        decode_top_nibble_next = 0; \
811
867
    } \
812
868
    else \
819
875
 
820
876
static int adpcm_decode_frame(AVCodecContext *avctx,
821
877
                            void *data, int *data_size,
822
 
                            uint8_t *buf, int buf_size)
 
878
                            const uint8_t *buf, int buf_size)
823
879
{
824
880
    ADPCMContext *c = avctx->priv_data;
825
881
    ADPCMChannelStatus *cs;
827
883
    int block_predictor[2];
828
884
    short *samples;
829
885
    short *samples_end;
830
 
    uint8_t *src;
 
886
    const uint8_t *src;
831
887
    int st; /* stereo */
832
888
 
833
889
    /* DK3 ADPCM accounting variables */
844
900
    int32_t coeff1l, coeff2l, coeff1r, coeff2r;
845
901
    uint8_t shift_left, shift_right;
846
902
    int count1, count2;
 
903
    int coeff[2][2], shift[2];//used in EA MAXIS ADPCM
847
904
 
848
905
    if (!buf_size)
849
906
        return 0;
863
920
 
864
921
    switch(avctx->codec->id) {
865
922
    case CODEC_ID_ADPCM_IMA_QT:
866
 
        n = (buf_size - 2);/* >> 2*avctx->channels;*/
867
 
        channel = c->channel;
868
 
        cs = &(c->status[channel]);
869
 
        /* (pppppp) (piiiiiii) */
870
 
 
871
 
        /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
872
 
        cs->predictor = (*src++) << 8;
873
 
        cs->predictor |= (*src & 0x80);
874
 
        cs->predictor &= 0xFF80;
875
 
 
876
 
        /* sign extension */
877
 
        if(cs->predictor & 0x8000)
878
 
            cs->predictor -= 0x10000;
879
 
 
880
 
        CLAMP_TO_SHORT(cs->predictor);
881
 
 
882
 
        cs->step_index = (*src++) & 0x7F;
883
 
 
884
 
        if (cs->step_index > 88){
885
 
            av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
886
 
            cs->step_index = 88;
887
 
        }
888
 
 
889
 
        cs->step = step_table[cs->step_index];
890
 
 
891
 
        if (st && channel)
892
 
            samples++;
893
 
 
894
 
        for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
895
 
            *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
896
 
            samples += avctx->channels;
897
 
            *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
898
 
            samples += avctx->channels;
899
 
            src ++;
900
 
        }
901
 
 
902
 
        if(st) { /* handle stereo interlacing */
903
 
            c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
904
 
            if(channel == 1) { /* wait for the other packet before outputing anything */
905
 
                return src - buf;
906
 
            }
907
 
        }
 
923
        n = buf_size - 2*avctx->channels;
 
924
        for (channel = 0; channel < avctx->channels; channel++) {
 
925
            cs = &(c->status[channel]);
 
926
            /* (pppppp) (piiiiiii) */
 
927
 
 
928
            /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
 
929
            cs->predictor = (*src++) << 8;
 
930
            cs->predictor |= (*src & 0x80);
 
931
            cs->predictor &= 0xFF80;
 
932
 
 
933
            /* sign extension */
 
934
            if(cs->predictor & 0x8000)
 
935
                cs->predictor -= 0x10000;
 
936
 
 
937
            cs->predictor = av_clip_int16(cs->predictor);
 
938
 
 
939
            cs->step_index = (*src++) & 0x7F;
 
940
 
 
941
            if (cs->step_index > 88){
 
942
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
 
943
                cs->step_index = 88;
 
944
            }
 
945
 
 
946
            cs->step = step_table[cs->step_index];
 
947
 
 
948
            samples = (short*)data + channel;
 
949
 
 
950
            for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
 
951
                *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
 
952
                samples += avctx->channels;
 
953
                *samples = adpcm_ima_expand_nibble(cs, src[0] >> 4  , 3);
 
954
                samples += avctx->channels;
 
955
                src ++;
 
956
            }
 
957
        }
 
958
        if (st)
 
959
            samples--;
908
960
        break;
909
961
    case CODEC_ID_ADPCM_IMA_WAV:
910
962
        if (avctx->block_align != 0 && buf_size > avctx->block_align)
914
966
 
915
967
        for(i=0; i<avctx->channels; i++){
916
968
            cs = &(c->status[i]);
917
 
            cs->predictor = (int16_t)(src[0] + (src[1]<<8));
 
969
            cs->predictor = *samples++ = (int16_t)(src[0] + (src[1]<<8));
918
970
            src+=2;
919
971
 
920
 
        // XXX: is this correct ??: *samples++ = cs->predictor;
921
 
 
922
972
            cs->step_index = *src++;
923
973
            if (cs->step_index > 88){
924
974
                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
999
1049
        *samples++ = c->status[0].sample2;
1000
1050
        if (st) *samples++ = c->status[1].sample2;
1001
1051
        for(;n>0;n--) {
1002
 
            *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
 
1052
            *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4  );
1003
1053
            *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
1004
1054
            src ++;
1005
1055
        }
1022
1072
 
1023
1073
            /* take care of the top nibble (always left or mono channel) */
1024
1074
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1025
 
                (src[0] >> 4) & 0x0F, 3);
 
1075
                src[0] >> 4, 3);
1026
1076
 
1027
1077
            /* take care of the bottom nibble, which is right sample for
1028
1078
             * stereo, or another mono sample */
1087
1137
 
1088
1138
            if (st) {
1089
1139
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1090
 
                    (src[0] >> 4) & 0x0F, 3);
 
1140
                    src[0] >> 4  , 3);
1091
1141
                *samples++ = adpcm_ima_expand_nibble(&c->status[1],
1092
1142
                    src[0] & 0x0F, 3);
1093
1143
            } else {
1094
1144
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1095
 
                    (src[0] >> 4) & 0x0F, 3);
 
1145
                    src[0] >> 4  , 3);
1096
1146
                *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1097
1147
                    src[0] & 0x0F, 3);
1098
1148
            }
1101
1151
        }
1102
1152
        break;
1103
1153
    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
1154
        while (buf_size >= 128) {
1107
1155
            xa_decode(samples, src, &c->status[0], &c->status[1],
1108
1156
                avctx->channels);
1111
1159
            buf_size -= 128;
1112
1160
        }
1113
1161
        break;
 
1162
    case CODEC_ID_ADPCM_IMA_EA_EACS:
 
1163
        samples_in_chunk = bytestream_get_le32(&src) >> (1-st);
 
1164
 
 
1165
        if (samples_in_chunk > buf_size-4-(8<<st)) {
 
1166
            src += buf_size - 4;
 
1167
            break;
 
1168
        }
 
1169
 
 
1170
        for (i=0; i<=st; i++)
 
1171
            c->status[i].step_index = bytestream_get_le32(&src);
 
1172
        for (i=0; i<=st; i++)
 
1173
            c->status[i].predictor  = bytestream_get_le32(&src);
 
1174
 
 
1175
        for (; samples_in_chunk; samples_in_chunk--, src++) {
 
1176
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],  *src>>4,   3);
 
1177
            *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
 
1178
        }
 
1179
        break;
 
1180
    case CODEC_ID_ADPCM_IMA_EA_SEAD:
 
1181
        for (; src < buf+buf_size; src++) {
 
1182
            *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
 
1183
            *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
 
1184
        }
 
1185
        break;
1114
1186
    case CODEC_ID_ADPCM_EA:
1115
1187
        samples_in_chunk = AV_RL32(src);
1116
1188
        if (samples_in_chunk >= ((buf_size - 12) * 2)) {
1128
1200
        src += 2;
1129
1201
 
1130
1202
        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];
 
1203
            coeff1l = ea_adpcm_table[ *src >> 4       ];
 
1204
            coeff2l = ea_adpcm_table[(*src >> 4  ) + 4];
1133
1205
            coeff1r = ea_adpcm_table[*src & 0x0F];
1134
1206
            coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
1135
1207
            src++;
1136
1208
 
1137
 
            shift_left = ((*src >> 4) & 0x0F) + 8;
 
1209
            shift_left  = (*src >> 4  ) + 8;
1138
1210
            shift_right = (*src & 0x0F) + 8;
1139
1211
            src++;
1140
1212
 
1141
1213
            for (count2 = 0; count2 < 28; count2++) {
1142
 
                next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
1143
 
                next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
 
1214
                next_left_sample  = (int32_t)((*src & 0xF0) << 24) >> shift_left;
 
1215
                next_right_sample = (int32_t)((*src & 0x0F) << 28) >> shift_right;
1144
1216
                src++;
1145
1217
 
1146
1218
                next_left_sample = (next_left_sample +
1149
1221
                next_right_sample = (next_right_sample +
1150
1222
                    (current_right_sample * coeff1r) +
1151
1223
                    (previous_right_sample * coeff2r) + 0x80) >> 8;
1152
 
                CLAMP_TO_SHORT(next_left_sample);
1153
 
                CLAMP_TO_SHORT(next_right_sample);
1154
1224
 
1155
1225
                previous_left_sample = current_left_sample;
1156
 
                current_left_sample = next_left_sample;
 
1226
                current_left_sample = av_clip_int16(next_left_sample);
1157
1227
                previous_right_sample = current_right_sample;
1158
 
                current_right_sample = next_right_sample;
 
1228
                current_right_sample = av_clip_int16(next_right_sample);
1159
1229
                *samples++ = (unsigned short)current_left_sample;
1160
1230
                *samples++ = (unsigned short)current_right_sample;
1161
1231
            }
1162
1232
        }
1163
1233
        break;
 
1234
    case CODEC_ID_ADPCM_EA_MAXIS_XA:
 
1235
        for(channel = 0; channel < avctx->channels; channel++) {
 
1236
            for (i=0; i<2; i++)
 
1237
                coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
 
1238
            shift[channel] = (*src & 0x0F) + 8;
 
1239
            src++;
 
1240
        }
 
1241
        for (count1 = 0; count1 < (buf_size - avctx->channels) / avctx->channels; count1++) {
 
1242
            for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
 
1243
                for(channel = 0; channel < avctx->channels; channel++) {
 
1244
                    int32_t sample = (int32_t)(((*(src+channel) >> i) & 0x0F) << 0x1C) >> shift[channel];
 
1245
                    sample = (sample +
 
1246
                             c->status[channel].sample1 * coeff[channel][0] +
 
1247
                             c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
 
1248
                    c->status[channel].sample2 = c->status[channel].sample1;
 
1249
                    c->status[channel].sample1 = av_clip_int16(sample);
 
1250
                    *samples++ = c->status[channel].sample1;
 
1251
                }
 
1252
            }
 
1253
            src+=avctx->channels;
 
1254
        }
 
1255
        break;
 
1256
    case CODEC_ID_ADPCM_EA_R1:
 
1257
    case CODEC_ID_ADPCM_EA_R2:
 
1258
    case CODEC_ID_ADPCM_EA_R3: {
 
1259
        /* channel numbering
 
1260
           2chan: 0=fl, 1=fr
 
1261
           4chan: 0=fl, 1=rl, 2=fr, 3=rr
 
1262
           6chan: 0=fl, 1=c,  2=fr, 3=rl,  4=rr, 5=sub */
 
1263
        const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
 
1264
        int32_t previous_sample, current_sample, next_sample;
 
1265
        int32_t coeff1, coeff2;
 
1266
        uint8_t shift;
 
1267
        unsigned int channel;
 
1268
        uint16_t *samplesC;
 
1269
        const uint8_t *srcC;
 
1270
 
 
1271
        samples_in_chunk = (big_endian ? bytestream_get_be32(&src)
 
1272
                                       : bytestream_get_le32(&src)) / 28;
 
1273
        if (samples_in_chunk > UINT32_MAX/(28*avctx->channels) ||
 
1274
            28*samples_in_chunk*avctx->channels > samples_end-samples) {
 
1275
            src += buf_size - 4;
 
1276
            break;
 
1277
        }
 
1278
 
 
1279
        for (channel=0; channel<avctx->channels; channel++) {
 
1280
            srcC = src + (big_endian ? bytestream_get_be32(&src)
 
1281
                                     : bytestream_get_le32(&src))
 
1282
                       + (avctx->channels-channel-1) * 4;
 
1283
            samplesC = samples + channel;
 
1284
 
 
1285
            if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
 
1286
                current_sample  = (int16_t)bytestream_get_le16(&srcC);
 
1287
                previous_sample = (int16_t)bytestream_get_le16(&srcC);
 
1288
            } else {
 
1289
                current_sample  = c->status[channel].predictor;
 
1290
                previous_sample = c->status[channel].prev_sample;
 
1291
            }
 
1292
 
 
1293
            for (count1=0; count1<samples_in_chunk; count1++) {
 
1294
                if (*srcC == 0xEE) {  /* only seen in R2 and R3 */
 
1295
                    srcC++;
 
1296
                    current_sample  = (int16_t)bytestream_get_be16(&srcC);
 
1297
                    previous_sample = (int16_t)bytestream_get_be16(&srcC);
 
1298
 
 
1299
                    for (count2=0; count2<28; count2++) {
 
1300
                        *samplesC = (int16_t)bytestream_get_be16(&srcC);
 
1301
                        samplesC += avctx->channels;
 
1302
                    }
 
1303
                } else {
 
1304
                    coeff1 = ea_adpcm_table[ *srcC>>4     ];
 
1305
                    coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
 
1306
                    shift = (*srcC++ & 0x0F) + 8;
 
1307
 
 
1308
                    for (count2=0; count2<28; count2++) {
 
1309
                        if (count2 & 1)
 
1310
                            next_sample = (int32_t)((*srcC++ & 0x0F) << 28) >> shift;
 
1311
                        else
 
1312
                            next_sample = (int32_t)((*srcC   & 0xF0) << 24) >> shift;
 
1313
 
 
1314
                        next_sample += (current_sample  * coeff1) +
 
1315
                                       (previous_sample * coeff2);
 
1316
                        next_sample = av_clip_int16(next_sample >> 8);
 
1317
 
 
1318
                        previous_sample = current_sample;
 
1319
                        current_sample  = next_sample;
 
1320
                        *samplesC = current_sample;
 
1321
                        samplesC += avctx->channels;
 
1322
                    }
 
1323
                }
 
1324
            }
 
1325
 
 
1326
            if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
 
1327
                c->status[channel].predictor   = current_sample;
 
1328
                c->status[channel].prev_sample = previous_sample;
 
1329
            }
 
1330
        }
 
1331
 
 
1332
        src = src + buf_size - (4 + 4*avctx->channels);
 
1333
        samples += 28 * samples_in_chunk * avctx->channels;
 
1334
        break;
 
1335
    }
 
1336
    case CODEC_ID_ADPCM_EA_XAS:
 
1337
        if (samples_end-samples < 32*4*avctx->channels
 
1338
            || buf_size < (4+15)*4*avctx->channels) {
 
1339
            src += buf_size;
 
1340
            break;
 
1341
        }
 
1342
        for (channel=0; channel<avctx->channels; channel++) {
 
1343
            int coeff[2][4], shift[4];
 
1344
            short *s2, *s = &samples[channel];
 
1345
            for (n=0; n<4; n++, s+=32*avctx->channels) {
 
1346
                for (i=0; i<2; i++)
 
1347
                    coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
 
1348
                shift[n] = (src[2]&0x0F) + 8;
 
1349
                for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
 
1350
                    s2[0] = (src[0]&0xF0) + (src[1]<<8);
 
1351
            }
 
1352
 
 
1353
            for (m=2; m<32; m+=2) {
 
1354
                s = &samples[m*avctx->channels + channel];
 
1355
                for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
 
1356
                    for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
 
1357
                        int level = (int32_t)((*src & (0xF0>>i)) << (24+i)) >> shift[n];
 
1358
                        int pred  = s2[-1*avctx->channels] * coeff[0][n]
 
1359
                                  + s2[-2*avctx->channels] * coeff[1][n];
 
1360
                        s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
 
1361
                    }
 
1362
                }
 
1363
            }
 
1364
        }
 
1365
        samples += 32*4*avctx->channels;
 
1366
        break;
 
1367
    case CODEC_ID_ADPCM_IMA_AMV:
1164
1368
    case CODEC_ID_ADPCM_IMA_SMJPEG:
1165
 
        c->status[0].predictor = *src;
1166
 
        src += 2;
1167
 
        c->status[0].step_index = *src++;
1168
 
        src++;  /* skip another byte before getting to the meat */
 
1369
        c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
 
1370
        c->status[0].step_index = bytestream_get_le16(&src);
 
1371
 
 
1372
        if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
 
1373
            src+=4;
 
1374
 
1169
1375
        while (src < buf + buf_size) {
1170
 
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1171
 
                *src & 0x0F, 3);
1172
 
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1173
 
                (*src >> 4) & 0x0F, 3);
 
1376
            char hi, lo;
 
1377
            lo = *src & 0x0F;
 
1378
            hi = *src >> 4;
 
1379
 
 
1380
            if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
 
1381
                FFSWAP(char, hi, lo);
 
1382
 
 
1383
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],
 
1384
                lo, 3);
 
1385
            *samples++ = adpcm_ima_expand_nibble(&c->status[0],
 
1386
                hi, 3);
1174
1387
            src++;
1175
1388
        }
1176
1389
        break;
1178
1391
        while (src < buf + buf_size) {
1179
1392
            if (st) {
1180
1393
                *samples++ = adpcm_ct_expand_nibble(&c->status[0],
1181
 
                    (src[0] >> 4) & 0x0F);
 
1394
                    src[0] >> 4);
1182
1395
                *samples++ = adpcm_ct_expand_nibble(&c->status[1],
1183
1396
                    src[0] & 0x0F);
1184
1397
            } else {
1185
1398
                *samples++ = adpcm_ct_expand_nibble(&c->status[0],
1186
 
                    (src[0] >> 4) & 0x0F);
 
1399
                    src[0] >> 4);
1187
1400
                *samples++ = adpcm_ct_expand_nibble(&c->status[0],
1188
1401
                    src[0] & 0x0F);
1189
1402
            }
1203
1416
        if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
1204
1417
            while (src < buf + buf_size) {
1205
1418
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1206
 
                    (src[0] >> 4) & 0x0F, 4, 0);
 
1419
                    src[0] >> 4, 4, 0);
1207
1420
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1208
1421
                    src[0] & 0x0F, 4, 0);
1209
1422
                src++;
1211
1424
        } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
1212
1425
            while (src < buf + buf_size && samples + 2 < samples_end) {
1213
1426
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1214
 
                    (src[0] >> 5) & 0x07, 3, 0);
 
1427
                     src[0] >> 5        , 3, 0);
1215
1428
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1216
1429
                    (src[0] >> 2) & 0x07, 3, 0);
1217
1430
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1221
1434
        } else {
1222
1435
            while (src < buf + buf_size && samples + 3 < samples_end) {
1223
1436
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1224
 
                    (src[0] >> 6) & 0x03, 2, 2);
 
1437
                     src[0] >> 6        , 2, 2);
1225
1438
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1226
1439
                    (src[0] >> 4) & 0x03, 2, 2);
1227
1440
                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1236
1449
    {
1237
1450
        GetBitContext gb;
1238
1451
        const int *table;
1239
 
        int k0, signmask, nb_bits;
 
1452
        int k0, signmask, nb_bits, count;
1240
1453
        int size = buf_size*8;
1241
1454
 
1242
1455
        init_get_bits(&gb, buf, size);
1243
1456
 
1244
 
        //read bits & inital values
 
1457
        //read bits & initial values
1245
1458
        nb_bits = get_bits(&gb, 2)+2;
1246
1459
        //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
1247
1460
        table = swf_index_tables[nb_bits-2];
1248
1461
        k0 = 1 << (nb_bits-2);
1249
1462
        signmask = 1 << (nb_bits-1);
1250
1463
 
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);
1254
 
        }
1255
 
 
1256
 
        while (get_bits_count(&gb) < size)
1257
 
        {
1258
 
            int i;
1259
 
 
 
1464
        while (get_bits_count(&gb) <= size - 22*avctx->channels) {
1260
1465
            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
1265
 
                int k = k0;
1266
 
 
1267
 
                do {
1268
 
                    if (delta & k)
1269
 
                        vpdiff += step;
1270
 
                    step >>= 1;
1271
 
                    k >>= 1;
1272
 
                } while(k);
1273
 
                vpdiff += step;
1274
 
 
1275
 
                if (delta & signmask)
1276
 
                    c->status[i].predictor -= vpdiff;
1277
 
                else
1278
 
                    c->status[i].predictor += vpdiff;
1279
 
 
1280
 
                c->status[i].step_index += table[delta & (~signmask)];
1281
 
 
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);
1284
 
 
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");
1288
 
                    return -1;
 
1466
                *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
 
1467
                c->status[i].step_index = get_bits(&gb, 6);
 
1468
            }
 
1469
 
 
1470
            for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
 
1471
                int i;
 
1472
 
 
1473
                for (i = 0; i < avctx->channels; i++) {
 
1474
                    // similar to IMA adpcm
 
1475
                    int delta = get_bits(&gb, nb_bits);
 
1476
                    int step = step_table[c->status[i].step_index];
 
1477
                    long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
 
1478
                    int k = k0;
 
1479
 
 
1480
                    do {
 
1481
                        if (delta & k)
 
1482
                            vpdiff += step;
 
1483
                        step >>= 1;
 
1484
                        k >>= 1;
 
1485
                    } while(k);
 
1486
                    vpdiff += step;
 
1487
 
 
1488
                    if (delta & signmask)
 
1489
                        c->status[i].predictor -= vpdiff;
 
1490
                    else
 
1491
                        c->status[i].predictor += vpdiff;
 
1492
 
 
1493
                    c->status[i].step_index += table[delta & (~signmask)];
 
1494
 
 
1495
                    c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
 
1496
                    c->status[i].predictor = av_clip_int16(c->status[i].predictor);
 
1497
 
 
1498
                    *samples++ = c->status[i].predictor;
 
1499
                    if (samples >= samples_end) {
 
1500
                        av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
 
1501
                        return -1;
 
1502
                    }
1289
1503
                }
1290
1504
            }
1291
1505
        }
1298
1512
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1299
1513
                        src[0] & 0x0F);
1300
1514
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
1301
 
                        (src[0] >> 4) & 0x0F);
 
1515
                        src[0] >> 4  );
1302
1516
            } else {
1303
1517
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1304
1518
                        src[0] & 0x0F);
1305
1519
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1306
 
                        (src[0] >> 4) & 0x0F);
 
1520
                        src[0] >> 4  );
1307
1521
            }
1308
1522
            src++;
1309
1523
        }
1310
1524
        break;
 
1525
    case CODEC_ID_ADPCM_THP:
 
1526
    {
 
1527
        int table[2][16];
 
1528
        unsigned int samplecnt;
 
1529
        int prev[2][2];
 
1530
        int ch;
 
1531
 
 
1532
        if (buf_size < 80) {
 
1533
            av_log(avctx, AV_LOG_ERROR, "frame too small\n");
 
1534
            return -1;
 
1535
        }
 
1536
 
 
1537
        src+=4;
 
1538
        samplecnt = bytestream_get_be32(&src);
 
1539
 
 
1540
        for (i = 0; i < 32; i++)
 
1541
            table[0][i] = (int16_t)bytestream_get_be16(&src);
 
1542
 
 
1543
        /* Initialize the previous sample.  */
 
1544
        for (i = 0; i < 4; i++)
 
1545
            prev[0][i] = (int16_t)bytestream_get_be16(&src);
 
1546
 
 
1547
        if (samplecnt >= (samples_end - samples) /  (st + 1)) {
 
1548
            av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
 
1549
            return -1;
 
1550
        }
 
1551
 
 
1552
        for (ch = 0; ch <= st; ch++) {
 
1553
            samples = (unsigned short *) data + ch;
 
1554
 
 
1555
            /* Read in every sample for this channel.  */
 
1556
            for (i = 0; i < samplecnt / 14; i++) {
 
1557
                int index = (*src >> 4) & 7;
 
1558
                unsigned int exp = 28 - (*src++ & 15);
 
1559
                int factor1 = table[ch][index * 2];
 
1560
                int factor2 = table[ch][index * 2 + 1];
 
1561
 
 
1562
                /* Decode 14 samples.  */
 
1563
                for (n = 0; n < 14; n++) {
 
1564
                    int32_t sampledat;
 
1565
                    if(n&1) sampledat=  *src++    <<28;
 
1566
                    else    sampledat= (*src&0xF0)<<24;
 
1567
 
 
1568
                    sampledat = ((prev[ch][0]*factor1
 
1569
                                + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
 
1570
                    *samples = av_clip_int16(sampledat);
 
1571
                    prev[ch][1] = prev[ch][0];
 
1572
                    prev[ch][0] = *samples++;
 
1573
 
 
1574
                    /* In case of stereo, skip one sample, this sample
 
1575
                       is for the other channel.  */
 
1576
                    samples += st;
 
1577
                }
 
1578
            }
 
1579
        }
 
1580
 
 
1581
        /* In the previous loop, in case stereo is used, samples is
 
1582
           increased exactly one time too often.  */
 
1583
        samples -= st;
 
1584
        break;
 
1585
    }
 
1586
 
1311
1587
    default:
1312
1588
        return -1;
1313
1589
    }
1352
1628
#define ADPCM_CODEC(id, name)                   \
1353
1629
ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
1354
1630
 
1355
 
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
1356
 
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
1357
 
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
1358
 
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
1359
 
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
1360
 
ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
1361
 
ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
1362
 
ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
1363
 
ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
1364
 
ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
1365
 
ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
1366
 
ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
1367
 
ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
1368
 
ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
1369
 
ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
1370
 
ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
1371
 
 
1372
 
#undef ADPCM_CODEC
 
1631
ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm);
 
1632
ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct);
 
1633
ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea);
 
1634
ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa);
 
1635
ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1);
 
1636
ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2);
 
1637
ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3);
 
1638
ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas);
 
1639
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv);
 
1640
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
 
1641
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
 
1642
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs);
 
1643
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead);
 
1644
ADPCM_CODEC  (CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
 
1645
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
 
1646
ADPCM_CODEC  (CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
 
1647
ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
 
1648
ADPCM_CODEC  (CODEC_ID_ADPCM_MS, adpcm_ms);
 
1649
ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
 
1650
ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
 
1651
ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
 
1652
ADPCM_CODEC  (CODEC_ID_ADPCM_SWF, adpcm_swf);
 
1653
ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp);
 
1654
ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa);
 
1655
ADPCM_CODEC  (CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);