Import the current wip animation datatype and subclasses. further development will...
[AROS.git] / workbench / classes / datatypes / wav / wave_ima_adpcm.c
blob836cdc4808f40dab0571592e783bf67b5e12fd38
1 /*
2 * wave.datatype
3 * (c) Fredrik Wikstrom
4 */
6 // Note: DVI & IMA formats are identical
8 #include "wave_ima_adpcm.h"
9 #include "bitpack.h"
10 #include "endian.h"
12 struct IMA_ADPCM_Format {
13 UWORD formatTag;
14 WORD numChannels;
15 LONG samplesPerSec;
16 LONG avgBytesPerSec;
17 WORD blockAlign; /* amount to read for each block */
18 WORD bitsPerSample; /* 3 or 4 */
20 WORD extraSize; /* 2 */
21 WORD samplesPerBlock;
24 DEC_SETUPPROTO(SetupIMA_ADPCM) {
25 struct IMA_ADPCM_Format * fmt;
27 fmt = (struct IMA_ADPCM_Format *)data->fmt;
29 /* check bitsPerSample */
30 if (fmt->bitsPerSample != 3 && fmt->bitsPerSample != 4) {
31 return DTERROR_UNKNOWN_COMPRESSION;
33 if (data->chunk.size != sizeof(*fmt)) {
34 return NOTOK;
36 data->blockFrames = fmt->samplesPerBlock = read_le16(&fmt->samplesPerBlock);
37 return OK;
40 static const WORD steptab[89] = {
41 7, 8, 9, 10, 11, 12, 13, 14,
42 16, 17, 19, 21, 23, 25, 28, 31,
43 34, 37, 41, 45, 50, 55, 60, 66,
44 73, 80, 88, 97, 107, 118, 130, 143,
45 157, 173, 190, 209, 230, 253, 279, 307,
46 337, 371, 408, 449, 494, 544, 598, 658,
47 724, 796, 876, 963, 1060, 1166, 1282, 1411,
48 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
49 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
50 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
51 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
52 32767
55 /* 3 bps */
56 static const BYTE indextab3[8] = {
57 -1, -1, 1, 2,
58 -1, -1, 1, 2
61 /* 4 bps */
62 static const BYTE indextab4[16] = {
63 -1, -1, -1, -1, 2, 4, 6, 8,
64 -1, -1, -1, -1, 2, 4, 6, 8
67 /* Each block has nchannels of these */
68 struct IMA_BlockHeader {
69 UBYTE isamp0[2]; /* Prev sample ro start decoding with (int) */
70 BYTE stepTableIndex; /* Current index in steptable array (0-88) */
71 UBYTE reserved;
74 DECODERPROTO(DecodeIMA_ADPCM) {
75 LONG Index[MAX_CHANNELS],Diff,Value[MAX_CHANNELS];
76 WORD Code;
77 LONG sampleSize,frame,frame2,chan;
78 LONG bframes,skip=0,t;
79 const BYTE *indextab;
80 BitPack_buffer b;
82 sampleSize = fmt->bitsPerSample;
84 if (sampleSize == 3) {
85 indextab = indextab3;
86 bframes = 32;
87 } else {
88 indextab = indextab4;
89 bframes = 8;
92 for (chan=0;chan<fmt->numChannels;chan++) {
93 Value[chan] = (WORD)read_le16(Src);Src+=2;
94 Index[chan] = *Src;Src+=2;
95 *Dst[chan]++ = Value[chan]>>8;
98 bitpack_init_lsb(&b, Src, 0);
100 for (frame=1;frame<numFrames;frame+=bframes) {
101 t=numFrames-frame;
102 if (t < bframes) {
103 skip = t * sampleSize;
104 bframes -= t;
106 for (chan=0;chan<fmt->numChannels;chan++) {
107 for (frame2=0;frame2<bframes;frame2++) {
109 /* Step 1 - Get delta value */
111 Code=bitpack_read_lsb(&b, sampleSize);
113 /* Step 2 - Calculate difference and new expected value */
116 LONG Step,mask;
117 Step = steptab[Index[chan]];
118 Diff = 0;
119 for (mask=1<<(sampleSize-2);mask;mask>>=1,Step>>=1) {
120 if (Code & mask) {
121 Diff += Step;
124 Diff += Step;
125 if (Code & (1<<(sampleSize-1))) {
126 Value[chan] -= Diff;
127 if (Value[chan]<-32678) Value[chan]=-32678;
128 } else {
129 Value[chan] += Diff;
130 if (Value[chan]>32767) Value[chan]=32767;
134 /* Step 3 - Find next index value */
136 Index[chan]+=indextab[Code];
138 if (Index[chan]<0) Index[chan]=0;
139 if (Index[chan]>88) Index[chan]=88;
141 /* Step 4 - Output value */
143 *Dst[chan]++ = Value[chan]>>8;
146 if (skip)
147 bitpack_seek_lsb(&b,OFFSET_CURRENT,skip);
151 return numFrames;