summaryrefslogtreecommitdiffstats
path: root/apps/codecs/libpcm/dvi_adpcm.c
blob: 3df5e901be081c22e32fd22ac5b99579ba4f7db7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Dave Chapman
 * Copyright (C) 2009 Yoshihisa Uchida
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "codeclib.h"
#include "pcm_common.h"
#include "support_formats.h"

/*
 * Intel DVI ADPCM
 */

static const uint16_t dvi_adpcm_steptab[89] ICONST_ATTR = {
    7, 8, 9, 10, 11, 12, 13, 14,
    16, 17, 19, 21, 23, 25, 28, 31,
    34, 37, 41, 45, 50, 55, 60, 66,
    73, 80, 88, 97, 107, 118, 130, 143,
    157, 173, 190, 209, 230, 253, 279, 307,
    337, 371, 408, 449, 494, 544, 598, 658,
    724, 796, 876, 963, 1060, 1166, 1282, 1411,
    1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
    3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
    7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
    32767 };

static const int dvi_adpcm_indextab4[8] ICONST_ATTR = {
    -1, -1, -1, -1, 2, 4, 6, 8 };

static const int dvi_adpcm_indextab3[4] ICONST_ATTR = { -1, -1, 1, 2 };

static struct pcm_format *fmt;

static bool set_format(struct pcm_format *format, const unsigned char *fmtpos)
{
    fmt = format;

    (void)fmtpos;

    if (fmt->bitspersample != 4 && fmt->bitspersample != 3)
    {
        DEBUGF("CODEC_ERROR: dvi_adpcm must have 3 or 4 bitspersample\n");
        return false;
    }

    if (fmt->size < 2) {
        DEBUGF("CODEC_ERROR: dvi_adpcm is missing SamplesPerBlock value\n");
        return false;
    }

    /* chunksize is computed so that one chunk is about 1/50s.
     * this make 4096 for 44.1kHz 16bits stereo.
     * It also has to be a multiple of blockalign */
    fmt->chunksize = (1 + fmt->avgbytespersec / (50*fmt->blockalign))*fmt->blockalign;

    /* check that the output buffer is big enough (convert to samplespersec,
       then round to the blockalign multiple below) */
    if ((((uint64_t)fmt->chunksize * ci->id3->frequency * fmt->channels * fmt->bitspersample)>>3)
        /(uint64_t)fmt->avgbytespersec >= PCM_CHUNK_SIZE)
        fmt->chunksize = ((uint64_t)PCM_CHUNK_SIZE * fmt->avgbytespersec
                         /((uint64_t)ci->id3->frequency * fmt->channels * 2
                         * fmt->blockalign)) * fmt->blockalign;

    return true;
}

static uint32_t get_seek_pos(long seek_time)
{
    uint32_t newpos;

    /* use avgbytespersec to round to the closest blockalign multiple,
       add firstblockposn. 64-bit casts to avoid overflows. */
    newpos = (((uint64_t)fmt->avgbytespersec*(seek_time - 1))
             / (1000LL*fmt->blockalign))*fmt->blockalign;
    return newpos;
}

static int decode_dvi_adpcm(const uint8_t *inbuf, size_t inbufsize,
                            int32_t *outbuf, size_t *outbufcount)
{
    size_t nsamples = 0;
    int sample[2];
    int samplecode[32][2];
    int i;
    int stepindex[2];
    int c;
    int diff;
    int step;
    int codem;
    int code;

    if (fmt->bitspersample != 4 && fmt->bitspersample != 3) {
        DEBUGF("decode_dvi_adpcm: wrong bitspersample\n");
        return CODEC_ERROR;
    }

    /* decode block header */
    for (c = 0; c < fmt->channels && inbufsize >= 4; c++) {
        /* decode + push first sample */
        sample[c] = (short)(inbuf[0]|(inbuf[1]<<8));/* need cast for sign-extend */
        outbuf[c] = sample[c] << 13;
        nsamples++;
        stepindex[c] = inbuf[2];
        /* check for step table index overflow */
        if (stepindex[c] > 88) {
            DEBUGF("decode_dvi_adpcm: stepindex[%d]=%d>88\n",c,stepindex[c]);
            return CODEC_ERROR;
        }

        inbuf += 4;
        inbufsize -= 4;
    }
    if (fmt->bitspersample == 4) {
        while (inbufsize >= (size_t)(fmt->channels*4) &&
               (nsamples + (fmt->channels*8) <= *outbufcount))
        {
            for (c = 0; c < fmt->channels; c++)
            {
                samplecode[0][c] = inbuf[0]&0xf;
                samplecode[1][c] = inbuf[0]>>4;
                samplecode[2][c] = inbuf[1]&0xf;
                samplecode[3][c] = inbuf[1]>>4;
                samplecode[4][c] = inbuf[2]&0xf;
                samplecode[5][c] = inbuf[2]>>4;
                samplecode[6][c] = inbuf[3]&0xf;
                samplecode[7][c] = inbuf[3]>>4;
                inbuf += 4;
                inbufsize -= 4;
            }
            for (i = 0; i < 8; i++)
            {
                for (c = 0; c < fmt->channels; c++)
                {
                    step = dvi_adpcm_steptab[stepindex[c]];
                    codem = samplecode[i][c];
                    code = codem & 0x07;

                    /* adjust the step table index */
                    stepindex[c] += dvi_adpcm_indextab4[code];
                    /* check for step table index overflow and underflow */
                    if (stepindex[c] > 88)
                        stepindex[c] = 88;
                    else if (stepindex[c] < 0)
                        stepindex[c] = 0;
                    /* calculate the difference */
#ifdef STRICT_IMA
                    diff = 0;
                    if (code & 4)
                        diff += step;
                    step = step >> 1;
                    if (code & 2)
                        diff += step;
                    step = step >> 1;
                    if (code & 1)
                        diff += step;
                    step = step >> 1;
                    diff += step;
#else
                    diff = ((code + code + 1) * step) >> 3; /* faster */
#endif
                    /* check the sign bit */
                    /* check for overflow and underflow errors */
                    if (code != codem)
                    {
                        sample[c] -= diff;
                        if (sample[c] < -32768)
                            sample[c] = -32768;
                    }
                    else
                    {
                        sample[c] += diff;
                        if (sample[c] > 32767)
                            sample[c] = 32767;
                    }
                    /* output the new sample */
                    outbuf[nsamples] = sample[c] << 13;
                    nsamples++;
                }
            }
        }
    } else {                  /* bitspersample == 3 */
        while (inbufsize >= (uint32_t)(fmt->channels*12) &&
               (nsamples + 32*fmt->channels) <= *outbufcount) {
            for (c = 0; c < fmt->channels; c++) {
                uint16_t bitstream = 0;
                int bitsread = 0;
                for (i = 0; i < 32 && inbufsize > 0; i++) {
                    if (bitsread < 3) {
                        /* read 8 more bits */
                        bitstream |= inbuf[0]<<bitsread;
                        bitsread += 8;
                        inbufsize--;
                        inbuf++;
                    }
                    samplecode[i][c] = bitstream & 7;
                    bitstream = bitstream>>3;
                    bitsread -= 3;
                }
                if (bitsread != 0) {
                    /* 32*3 = 3 words, so we should end with bitsread==0 */
                    DEBUGF("decode_dvi_adpcm: error in implementation\n");
                    return CODEC_ERROR;
                }
            }
            
            for (i = 0; i < 32; i++) {
                for (c = 0; c < fmt->channels; c++) {
                    step = dvi_adpcm_steptab[stepindex[c]];
                    codem = samplecode[i][c];
                    code = codem & 0x03;

                    /* adjust the step table index */
                    stepindex[c] += dvi_adpcm_indextab3[code];
                    /* check for step table index overflow and underflow */
                    if (stepindex[c] > 88)
                        stepindex[c] = 88;
                    else if (stepindex[c] < 0)
                        stepindex[c] = 0;
                    /* calculate the difference */
#ifdef STRICT_IMA
                    diff = 0;
                    if (code & 2)
                        diff += step;
                    step = step >> 1;
                    if (code & 1)
                        diff += step;
                    step = step >> 1;
                    diff += step;
#else
                    diff = ((code + code + 1) * step) >> 3; /* faster */
#endif
                    /* check the sign bit */
                    /* check for overflow and underflow errors */
                    if (code != codem) {
                        sample[c] -= diff;
                        if (sample[c] < -32768)
                            sample[c] = -32768;
                    }
                    else {
                        sample[c] += diff;
                        if (sample[c] > 32767)
                            sample[c] = 32767;
                    }
                    /* output the new sample */
                    outbuf[nsamples] = sample[c] << 13;
                    nsamples++;
                }
            }
        }
    }

    if (nsamples > *outbufcount) {
        DEBUGF("decode_dvi_adpcm: output buffer overflow!\n");
        return CODEC_ERROR;
    }
    *outbufcount = nsamples;
    if (inbufsize != 0) {
        DEBUGF("decode_dvi_adpcm: n=%d unprocessed bytes\n", (int)inbufsize);
    }
    return CODEC_OK;
}

static int decode(const uint8_t *inbuf, size_t inbufsize,
                  int32_t *outbuf, int *outbufsize)
{
    unsigned int i;
    unsigned int nblocks = fmt->chunksize / fmt->blockalign;

    (void)inbufsize;

    for (i = 0; i < nblocks; i++)
    {
        size_t decodedsize = fmt->samplesperblock * fmt->channels;
        if (decode_dvi_adpcm(inbuf + i * fmt->blockalign, fmt->blockalign,
                             outbuf + i * fmt->samplesperblock * fmt->channels,
                             &decodedsize) != CODEC_OK) {
           return CODEC_ERROR;
        }
    }
    *outbufsize = nblocks * fmt->samplesperblock;
    return CODEC_OK;
}

static const struct pcm_codec codec = {
                                          set_format,
                                          get_seek_pos,
                                          decode,
                                      };

const struct pcm_codec *get_dvi_adpcm_codec(void)
{
    return &codec;
}