summaryrefslogtreecommitdiffstats
path: root/voip/jni/rtp/AudioCodec.cpp
blob: 4d8d36c1de57844a886fe0da0e99b4e1377e7dc7 (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
/*
 * Copyrightm (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string.h>

#include "AudioCodec.h"

namespace {

int8_t gExponents[128] = {
    0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
    6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
    6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
};

//------------------------------------------------------------------------------

class UlawCodec : public AudioCodec
{
public:
    int set(int sampleRate, const char *fmtp) {
        mSampleCount = sampleRate / 50;
        return mSampleCount;
    }
    int encode(void *payload, int16_t *samples);
    int decode(int16_t *samples, void *payload, int length);
private:
    int mSampleCount;
};

int UlawCodec::encode(void *payload, int16_t *samples)
{
    int8_t *ulaws = (int8_t *)payload;
    for (int i = 0; i < mSampleCount; ++i) {
        int sample = samples[i];
        int sign = (sample >> 8) & 0x80;
        if (sample < 0) {
            sample = -sample;
        }
        sample += 132;
        if (sample > 32767) {
            sample = 32767;
        }
        int exponent = gExponents[sample >> 8];
        int mantissa = (sample >> (exponent + 3)) & 0x0F;
        ulaws[i] = ~(sign | (exponent << 4) | mantissa);
    }
    return mSampleCount;
}

int UlawCodec::decode(int16_t *samples, void *payload, int length)
{
    int8_t *ulaws = (int8_t *)payload;
    for (int i = 0; i < length; ++i) {
        int ulaw = ~ulaws[i];
        int exponent = (ulaw >> 4) & 0x07;
        int mantissa = ulaw & 0x0F;
        int sample = (((mantissa << 3) + 132) << exponent) - 132;
        samples[i] = (ulaw < 0 ? -sample : sample);
    }
    return length;
}

AudioCodec *newUlawCodec()
{
    return new UlawCodec;
}

//------------------------------------------------------------------------------

class AlawCodec : public AudioCodec
{
public:
    int set(int sampleRate, const char *fmtp) {
        mSampleCount = sampleRate / 50;
        return mSampleCount;
    }
    int encode(void *payload, int16_t *samples);
    int decode(int16_t *samples, void *payload, int length);
private:
    int mSampleCount;
};

int AlawCodec::encode(void *payload, int16_t *samples)
{
    int8_t *alaws = (int8_t *)payload;
    for (int i = 0; i < mSampleCount; ++i) {
        int sample = samples[i];
        int sign = (sample >> 8) & 0x80;
        if (sample < 0) {
            sample = -sample;
        }
        if (sample > 32767) {
            sample = 32767;
        }
        int exponent = gExponents[sample >> 8];
        int mantissa = (sample >> (exponent == 0 ? 4 : exponent + 3)) & 0x0F;
        alaws[i] = (sign | (exponent << 4) | mantissa) ^ 0xD5;
    }
    return mSampleCount;
}

int AlawCodec::decode(int16_t *samples, void *payload, int length)
{
    int8_t *alaws = (int8_t *)payload;
    for (int i = 0; i < length; ++i) {
        int alaw = alaws[i] ^ 0x55;
        int exponent = (alaw >> 4) & 0x07;
        int mantissa = alaw & 0x0F;
        int sample = (exponent == 0 ? (mantissa << 4) + 8 :
            ((mantissa << 3) + 132) << exponent);
        samples[i] = (alaw < 0 ? sample : -sample);
    }
    return length;
}

AudioCodec *newAlawCodec()
{
    return new AlawCodec;
}

struct AudioCodecType {
    const char *name;
    AudioCodec *(*create)();
} gAudioCodecTypes[] = {
    {"PCMA", newAlawCodec},
    {"PCMU", newUlawCodec},
    {NULL, NULL},
};

} // namespace

AudioCodec *newAudioCodec(const char *codecName)
{
    AudioCodecType *type = gAudioCodecTypes;
    while (type->name != NULL) {
        if (strcasecmp(codecName, type->name) == 0) {
            AudioCodec *codec = type->create();
            codec->name = type->name;
            return codec;
        }
        ++type;
    }
    return NULL;
}