summaryrefslogtreecommitdiff
path: root/orkbasecxx/filters/ilbc/IlbcFilters.cpp
blob: c6fd9b51c511b3909ef0deb6198a9e95712345e4 (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
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */
#pragma warning( disable: 4786 ) // disables truncated symbols in browse-info warning

#define _WINSOCKAPI_		// prevents the inclusion of winsock.h

#include "IlbcFilters.h"

IlbcToPcmFilter::IlbcToPcmFilter()
{
	/* Initialize iLBC decoder states */
	memset(&dec30, 0, sizeof(dec30));
        memset(&dec20, 0, sizeof(dec20));

	initDecode(&dec30, 30, 0);
        initDecode(&dec20, 20, 0);

	this->s_ilbclog = Logger::getLogger("iLBC");
#if 0
	LOG4CXX_INFO(this->s_ilbclog, "Initialized new iLBC decoder");
#endif
}

IlbcToPcmFilter::~IlbcToPcmFilter()
{
        memset(&dec30, 0, sizeof(dec30));
        memset(&dec20, 0, sizeof(dec20));

#if 0
	LOG4CXX_INFO(this->s_ilbclog, "Decommissioned iLBC decoder");
#endif
}

FilterRef IlbcToPcmFilter::Instanciate()
{
	FilterRef Filter(new IlbcToPcmFilter());
	return Filter;
}

void IlbcToPcmFilter::AudioChunkIn(AudioChunkRef& inputAudioChunk)
{
	int r_samples = 0, fs = 0, i = 0, pos = 0, o_samples = 0, j = 0;
	float ilbcf[240];

	m_outputAudioChunk.reset();

	if(inputAudioChunk.get() == NULL) {
		return;
	}

	if(inputAudioChunk->GetNumSamples() == 0) {
		return;
	}

	AudioChunkDetails outputDetails = *inputAudioChunk->GetDetails();

	if(outputDetails.m_rtpPayloadType != GetInputRtpPayloadType()) {
#if 0
		LOG4CXX_ERROR(this->s_ilbclog, "Error, invalid payload type received");
#endif
		return;
	}

	r_samples = inputAudioChunk->GetNumSamples();
	if((r_samples % 50) && (r_samples % 38)) {
		/* Strange iLBC frame that is not a multiple of 50 bytes
		 * (30ms frame) and neither is it a multiple of 38 bytes
		 * (20ms frame). We should probably log something? */
                LOG4CXX_ERROR(this->s_ilbclog, "Error, received iLBC frame is not a multiple of 50 or 38!");
		return;
	}

	if(!(r_samples % 50)) {
		i = r_samples / 50;
		o_samples = i * 240;
		fs = 50;
#if 0
		LOG4CXX_INFO(this->s_ilbclog, "Frame size is 50 bytes");
#endif
	} else {
		i = r_samples / 38;
		o_samples = i * 160;
		fs = 38;
#if 0
		LOG4CXX_INFO(this->s_ilbclog, "Frame size is 38 bytes");
#endif
	}

	m_outputAudioChunk.reset(new AudioChunk());
	outputDetails.m_rtpPayloadType = -1;
	outputDetails.m_encoding = PcmAudio;

	outputDetails.m_numBytes = (o_samples * 2);
	short* outputBuffer = (short*)m_outputAudioChunk->CreateBuffer(outputDetails);
	unsigned char* inputBuffer = (unsigned char*)inputAudioChunk->m_pBuffer;

	for(i = 0; i < r_samples; i += fs) {
		if((pos+((fs == 50) ? 240 : 160)) <= o_samples) {
			if(fs == 50) {
				iLBC_decode(ilbcf, inputBuffer+i, &dec30, 1);

				for(j = 0; j < 240; j++) {
					outputBuffer[pos + j] = (short)ilbcf[j];
				}

				pos += 240;
			} else {
				iLBC_decode(ilbcf, inputBuffer+i, &dec20, 1);

				for(j = 0; j < 160; j++) {
                                        outputBuffer[pos + j] = (short)ilbcf[j];
                                }

                                pos += 160;
			}
		} else {
			/* This should ordinarily never happen.
			 * Log something? */
			CStdString logMsg;

			logMsg.Format("Strange, I ran out of space: pos=%d, o_samples=%d, r_samples=%d, i=%d, "
					"(pos+((fs == 50) ? 240 : 160))=%d",
					pos, o_samples, r_samples, i, (pos+((fs == 50) ? 240 : 160)));
			LOG4CXX_ERROR(this->s_ilbclog, logMsg);
			return;
		}
	}
}

void IlbcToPcmFilter::AudioChunkOut(AudioChunkRef& chunk)
{
	chunk = m_outputAudioChunk;
}

AudioEncodingEnum IlbcToPcmFilter::GetInputAudioEncoding()
{
	return IlbcAudio;
}

AudioEncodingEnum IlbcToPcmFilter::GetOutputAudioEncoding()
{
	return PcmAudio;
}

CStdString IlbcToPcmFilter::GetName()
{
	return "IlbcToPcm";
}

int IlbcToPcmFilter::GetInputRtpPayloadType()
{
	/* For now, return 0x61 (97) which is Asterisk's default */
	return 0x61;
}

void IlbcToPcmFilter::CaptureEventIn(CaptureEventRef& event)
{
	;
}

void IlbcToPcmFilter::CaptureEventOut(CaptureEventRef& event)
{
	;
}