Alejandro Acuña
2024-08-12 1876e65234c20209001178705cfa50d8f9ded67a
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
// G711Codec.cpp: implementation of the G711Codec class.
//
//////////////////////////////////////////////////////////////////////
 
#include "G711Codec.h"
#include "G711Table.h"
 
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
 
G711Codec::G711Codec()
{
    SetULaw();
}
 
G711Codec::~G711Codec()
{
 
}
 
int
G711Codec::Encode(void *input, int inputSizeBytes, void *output, int *outputSizeBytes)
{
    char *dataIn = (char *)input;
    char *dataOut = (char *)output;
 
    if (*outputSizeBytes < inputSizeBytes/2)
    {
        *outputSizeBytes = 0;
        return -10;
    }
    else 
    {
        unsigned char *compTable;
        if (GetCodecType() == CODECTYPE_ULAW)
        {
            compTable = ulaw_comp_table;
        }
        else
        {
            compTable = alaw_comp_table;
        }
        // convert the sample
        for (int i=0; i<inputSizeBytes; i+=2) {
            short input = *((short *)(dataIn + i));
            dataOut[i/2] = compTable[(input / 4) & 0x3fff];
        }
        *outputSizeBytes = inputSizeBytes / 2;
    }
    return 0;
}
 
int
G711Codec::Decode(void *input, int inputSizeBytes, void *output, int *outputSizeBytes)
{
    unsigned char  *dataIn = (unsigned char *)input;
    short *dataOut = (short *)output;
 
    if (inputSizeBytes*2 > *outputSizeBytes) 
    {
        *outputSizeBytes = 0;
        return -10;
    }
    else 
    {
        short *expTable;
        if (GetCodecType() == CODECTYPE_ULAW)
        {
            expTable = ulaw_exp_table;
        }
        else
        {
            expTable = alaw_exp_table;
        }
        // convert the sample
        for (int i=0; i<inputSizeBytes; i++)
        {
            dataOut[i] = expTable[dataIn[i]];
        }
        *outputSizeBytes = inputSizeBytes * 2;
    }
    return 0;
}