summaryrefslogtreecommitdiffstats
path: root/tinyDAV/src/codecs/ilbc/tdav_codec_ilbc.c
blob: 65df6adf9cb2ad7ee160de957e1f0af23199ab9d (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
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO 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 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

/**@file tdav_codec_ilbc.c
 * @brief iLBC codec
 *
 * @author Mamadou Diop <diopmamadou(at)doubango.org>
 *

 */
#include "tinydav/codecs/ilbc/tdav_codec_ilbc.h"

#if HAVE_ILBC

#include "tsk_string.h"
#include "tsk_memory.h"
#include "tsk_debug.h"

#define TDAV_ILBC_MODE			20

/* ============ iLBC Plugin interface ================= */

#define tdav_codec_ilbc_set tsk_null

static int tdav_codec_ilbc_open(tmedia_codec_t* self)
{
	tdav_codec_ilbc_t* ilbc = (tdav_codec_ilbc_t*)self;
	
	initEncode(&ilbc->encoder, TDAV_ILBC_MODE);
    initDecode(&ilbc->decoder, TDAV_ILBC_MODE, tsk_true/* Enhancer */);

	return 0;
}

static int tdav_codec_ilbc_close(tmedia_codec_t* self)
{
	tdav_codec_ilbc_t* ilbc = (tdav_codec_ilbc_t*)self;

	//ilbc->encoder = {0};
	//ilbc->decoder = {0};

	return 0;
}

static tsk_size_t tdav_codec_ilbc_encode(tmedia_codec_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size)
{	
	tdav_codec_ilbc_t* ilbc = (tdav_codec_ilbc_t*)self;
	int k;
	
	if(!self || !in_data || !in_size || !out_data){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}
	
   /* convert signal to float */
	for(k=0; k<ilbc->encoder.blockl; k++){
       ilbc->encblock[k] = (float)((short*)in_data)[k];
	}
	
	/* allocate new buffer if needed */
	if((int)*out_max_size <ilbc->encoder.no_of_bytes){
		if(!(*out_data = tsk_realloc(*out_data, ilbc->encoder.no_of_bytes))){
			TSK_DEBUG_ERROR("Failed to allocate new buffer");
			*out_max_size = 0;
			return 0;
		}
		*out_max_size = ilbc->encoder.no_of_bytes;
	}
	
	/* do the actual encoding */
    iLBC_encode(*out_data, ilbc->encblock, &ilbc->encoder);
	
	return ilbc->encoder.no_of_bytes;
}

static tsk_size_t tdav_codec_ilbc_decode(tmedia_codec_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr)
{
	int blocks, i, k, block_size;
	float dtmp;
	tsk_size_t out_size;
	tdav_codec_ilbc_t* ilbc = (tdav_codec_ilbc_t*)self;

	if(!self || !in_data || !in_size || !out_data){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}

	if((in_size % NO_OF_BYTES_20MS) == 0){
		/* Using 20ms mode */
		blocks = (in_size/NO_OF_BYTES_20MS);
		out_size = (BLOCKL_20MS * blocks) * sizeof(short);
		block_size = out_size/blocks;
		if(ilbc->decoder.mode != 20){
			 initDecode(&ilbc->decoder, 20, tsk_true/* Enhancer */);
		}
	}
	else if((in_size % NO_OF_BYTES_30MS) == 0){
		/* Using 30ms mode */
		blocks = (in_size/NO_OF_BYTES_30MS);
		out_size = (BLOCKL_30MS * blocks) * sizeof(short);
		block_size = out_size/blocks;
		if(ilbc->decoder.mode != 30){
			 initDecode(&ilbc->decoder, 30, tsk_true/* Enhancer */);
		}
	}
	else{
		TSK_DEBUG_ERROR("Invalid iLBC mode");
		return 0;
	}

	/* allocate new buffer if needed */
	if(*out_max_size<out_size){
		if(!(*out_data = tsk_realloc(*out_data, out_size))){
			TSK_DEBUG_ERROR("Failed to allocate new buffer");
			*out_max_size = 0;
			return 0;
		}
		*out_max_size = out_size;
	}

	for(i = 0; i<blocks; i++){
		iLBC_decode(ilbc->decblock, &((uint8_t*)in_data)[i*block_size], &ilbc->decoder, 1/* Normal */);

       /* convert to short */
       for(k=0; k<ilbc->decoder.blockl; k++){
           dtmp=ilbc->decblock[k];

		   if(dtmp<MIN_SAMPLE){
               dtmp = MIN_SAMPLE;
		   }
		   else if(dtmp>MAX_SAMPLE){
               dtmp = MAX_SAMPLE;
		   }
		  
		   ((short*)*out_data)[(i*block_size) + k] = ((short) dtmp);
       }
	}

	return out_size;
}

static char* tdav_codec_ilbc_sdp_att_get(const tmedia_codec_t* codec, const char* att_name)
{
	if(tsk_striequals(att_name, "fmtp")){
		char* fmtp = tsk_null;
		tsk_sprintf(&fmtp, "mode=%d", TDAV_ILBC_MODE);
		return fmtp;
	}
	return tsk_null;
}

static tsk_bool_t tdav_codec_ilbc_sdp_att_match(const tmedia_codec_t* codec, const char* att_name, const char* att_value)
{	
	if(tsk_striequals(att_name, "fmtp")){
		/*	RFC 3952 - 5. Mapping To SDP Parameters
			
			The offer contains the preferred mode of the offerer.  The answerer
			may agree to that mode by including the same mode in the answer, or
			may include a different mode.  The resulting mode used by both
			parties SHALL be the lower of the bandwidth modes in the offer and
			answer.

			That is, an offer of "mode=20" receiving an answer of "mode=30" will
			result in "mode=30" being used by both participants.  Similarly, an
			offer of "mode=30" and an answer of "mode=20" will result in
			"mode=30" being used by both participants.

			This is important when one end point utilizes a bandwidth constrained
			link (e.g., 28.8k modem link or slower), where only the lower frame
			size will work.
		*/
		return tsk_true; // FIXME
	}
	return tsk_true;
}


//
//	iLBC Plugin definition
//

/* constructor */
static tsk_object_t* tdav_codec_ilbc_ctor(tsk_object_t * self, va_list * app)
{
	tdav_codec_ilbc_t *ilbc = self;
	if(ilbc){
		/* init base: called by tmedia_codec_create() */
		/* init self */
	}
	return self;
}
/* destructor */
static tsk_object_t* tdav_codec_ilbc_dtor(tsk_object_t * self)
{ 
	tdav_codec_ilbc_t *ilbc = self;
	if(ilbc){
		/* deinit base */
		tmedia_codec_audio_deinit(ilbc);
		/* deinit self */
		//ilbc->encoder = {0};
		//ilbc->decoder = {0};
	}

	return self;
}
/* object definition */
static const tsk_object_def_t tdav_codec_ilbc_def_s = 
{
	sizeof(tdav_codec_ilbc_t),
	tdav_codec_ilbc_ctor, 
	tdav_codec_ilbc_dtor,
	tmedia_codec_cmp, 
};
/* plugin definition*/
static const tmedia_codec_plugin_def_t tdav_codec_ilbc_plugin_def_s = 
{
	&tdav_codec_ilbc_def_s,

	tmedia_audio,
	tmedia_codec_id_ilbc,
	"iLBC",
	"iLBC codec (libILBc)",
	TMEDIA_CODEC_FORMAT_ILBC,
	tsk_true,
	8000, // rate
	
	{ /* audio */
		1, // channels
		20 // ptime
	},

	/* video */
	{0},

	tdav_codec_ilbc_set,
	tdav_codec_ilbc_open,
	tdav_codec_ilbc_close,
	tdav_codec_ilbc_encode,
	tdav_codec_ilbc_decode,
	tdav_codec_ilbc_sdp_att_match,
	tdav_codec_ilbc_sdp_att_get
};
const tmedia_codec_plugin_def_t *tdav_codec_ilbc_plugin_def_t = &tdav_codec_ilbc_plugin_def_s;


#endif /* HAVE_ILBC */
OpenPOWER on IntegriCloud