summaryrefslogtreecommitdiff
path: root/apps/codecs/libpcm/ms_adpcm.c
blob: 79365807ef559927e48a9b295338414a41ff3c10 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 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 "support_formats.h"

/*
 * Microsoft ADPCM
 *
 * References
 * [1] Microsoft, New Multimedia Data Types and Data Techniques Revision 3.0, 1994
 * [2] MulitimediaWiki, Microsoft ADPCM, 2006
 * (http://wiki.multimedia.cx/index.php?title=Microsoft_ADPCM)
 * [3] ffmpeg source code, libavcodec/adpcm.c
 */

#define ADPCM_NUM_COEFF 7

static int16_t dec_coeff[2][2];
static uint16_t delta[2];
static int16_t sample[2][2];

static struct pcm_format *fmt;

static const int16_t adaptation_table[] ICONST_ATTR = {
    230, 230, 230, 230, 307, 409, 512, 614,
    768, 614, 512, 409, 307, 230, 230, 230
};

static bool set_format(struct pcm_format *format)
{
    fmt = format;

    if (fmt->bitspersample != 4)
    {
        DEBUGF("CODEC_ERROR: microsoft adpcm must be 4 bitspersample: %d\n",
                             fmt->bitspersample);
        return false;
    }

    fmt->chunksize = fmt->blockalign;

    return true;
}

static struct pcm_pos *get_seek_pos(long seek_time,
                                    uint8_t *(*read_buffer)(size_t *realsize))
{
    static struct pcm_pos newpos;
    uint32_t newblock = ((uint64_t)seek_time * ci->id3->frequency)
                              / (1000LL * fmt->samplesperblock);

    (void)read_buffer;
    newpos.pos     = newblock * fmt->blockalign;
    newpos.samples = newblock * fmt->samplesperblock;
    return &newpos;
}

static int16_t create_pcmdata(int ch, uint8_t nibble)
{
    int32_t pcmdata;

    pcmdata = (sample[ch][0] * dec_coeff[ch][0] +
               sample[ch][1] * dec_coeff[ch][1]) / 256;
    pcmdata += (delta[ch] * (nibble - ((nibble & 0x8) << 1)));

    CLIP(pcmdata, -32768, 32767);
  
    sample[ch][1] = sample[ch][0];
    sample[ch][0] = pcmdata;

    delta[ch] = (adaptation_table[nibble] * delta[ch]) >> 8;
    if (delta[ch] < 16)
        delta[ch] = 16;

    return (int16_t)pcmdata;
}

static int decode(const uint8_t *inbuf, size_t inbufsize,
                  int32_t *outbuf, int *outbufcount)
{
    int ch;
    size_t nsamples = 0;
    int size = fmt->samplesperblock;

    /* read block header */
    for (ch = 0; ch < fmt->channels; ch++)
    {
        if (*inbuf >= ADPCM_NUM_COEFF)
        {
            DEBUGF("CODEC_ERROR: microsoft adpcm illegal initial coeff=%d > 7\n",
                                 *inbuf);
            return CODEC_ERROR;
        }
        dec_coeff[ch][0] = fmt->coeffs[*inbuf][0];
        dec_coeff[ch][1] = fmt->coeffs[*inbuf][1];
        inbuf++;
    }

    for (ch = 0; ch < fmt->channels; ch++)
    {
        delta[ch] = inbuf[0] | (SE(inbuf[1]) << 8);
        inbuf += 2;
    }

    for (ch = 0; ch < fmt->channels; ch++)
    {
        sample[ch][0] = inbuf[0] | (SE(inbuf[1]) << 8);
        inbuf += 2;
    }

    for (ch = 0; ch < fmt->channels; ch++)
    {
        sample[ch][1] = inbuf[0] | (SE(inbuf[1]) << 8);
        inbuf += 2;
    }

    inbufsize -= 7 * fmt->channels;
    ch = fmt->channels - 1;

    while (size-- > 0)
    {
        *outbuf++ = create_pcmdata(0,  *inbuf >> 4 ) << (PCM_OUTPUT_DEPTH - 16);
        *outbuf++ = create_pcmdata(ch, *inbuf & 0xf) << (PCM_OUTPUT_DEPTH - 16);
        nsamples += 2;

        inbuf++;
        inbufsize--;
        if (inbufsize <= 0)
            break;
    }

    if (fmt->channels == 2)
        nsamples >>= 1;
    *outbufcount = nsamples;

    return CODEC_OK;
}

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

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