summaryrefslogtreecommitdiffstats
path: root/tinyDAV/src/codecs/opus/tdav_codec_opus.c
blob: 355fc73d490edab4b5726e3075c71f8ae5cf6819 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* Copyright (C) 2010-2013 Doubango Telecom <http://www.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_opus.c
 * @brief OPUS audio codec.
 * SDP: http://tools.ietf.org/html/draft-spittka-payload-rtp-opus-03
 */
#include "tinydav/codecs/opus/tdav_codec_opus.h"

#if HAVE_LIBOPUS

#include <opus/opus.h>

#include "tinymedia/tmedia_defaults.h"

#include "tinyrtp/rtp/trtp_rtp_packet.h"

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

#if !defined(TDAV_OPUS_MAX_FRAME_SIZE_IN_SAMPLES)
#	define TDAV_OPUS_MAX_FRAME_SIZE_IN_SAMPLES (5760) /* 120ms@48kHz */
#endif
#if !defined(TDAV_OPUS_MAX_FRAME_SIZE_IN_BYTES)
#	define TDAV_OPUS_MAX_FRAME_SIZE_IN_BYTES (TDAV_OPUS_MAX_FRAME_SIZE_IN_SAMPLES << 1) /* 120ms@48kHz */
#endif
#if !defined(TDAV_OPUS_FEC_ENABLED)
#	define TDAV_OPUS_FEC_ENABLED	0
#endif
#if !defined(TDAV_OPUS_DTX_ENABLED)
#	define TDAV_OPUS_DTX_ENABLED	0
#endif

typedef struct tdav_codec_opus_s
{
	TMEDIA_DECLARE_CODEC_AUDIO;

	struct {
		OpusEncoder *inst;
	} encoder;

	struct {
		OpusDecoder *inst;
		opus_int16 buff[TDAV_OPUS_MAX_FRAME_SIZE_IN_SAMPLES];
		tsk_bool_t fec_enabled;
		tsk_bool_t dtx_enabled;
		uint16_t last_seq;
	} decoder;
}
tdav_codec_opus_t;


static tsk_bool_t _tdav_codec_opus_rate_is_valid(const int32_t rate)
{
	switch(rate){
		case 8000: case 12000: case 16000: case 24000: case 48000: return tsk_true;
		default: return tsk_false;
	}
}

static int tdav_codec_opus_open(tmedia_codec_t* self)
{
	tdav_codec_opus_t* opus = (tdav_codec_opus_t*)self;
	int opus_err;

	if(!opus){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	// Initialize the decoder
	if(!opus->decoder.inst){
		TSK_DEBUG_INFO("[OPUS] Open decoder: rate=%d, channels=%d", (int)self->in.rate, (int)TMEDIA_CODEC_AUDIO(self)->in.channels);
		if(!(opus->decoder.inst = opus_decoder_create((opus_int32)self->in.rate, (int)TMEDIA_CODEC_AUDIO(self)->in.channels, &opus_err)) || opus_err != OPUS_OK){
			TSK_DEBUG_ERROR("Failed to create Opus decoder(rate=%d, channels=%d) instance with error code=%d.", (int)self->in.rate, (int)TMEDIA_CODEC_AUDIO(self)->in.channels, opus_err);
			return -2;
		}		
	}
    opus->decoder.last_seq  = 0;

	// Initialize the encoder
	if(!opus->encoder.inst){
		TSK_DEBUG_INFO("[OPUS] Open encoder: rate=%d, channels=%d", (int)self->out.rate, (int)TMEDIA_CODEC_AUDIO(self)->out.channels);
		if(!(opus->encoder.inst = opus_encoder_create((opus_int32)self->out.rate, (int)TMEDIA_CODEC_AUDIO(self)->out.channels, OPUS_APPLICATION_VOIP, &opus_err)) || opus_err != OPUS_OK){
			TSK_DEBUG_ERROR("Failed to create Opus decoder(rate=%d, channels=%d) instance with error code=%d.", (int)self->out.rate, (int)TMEDIA_CODEC_AUDIO(self)->out.channels, opus_err);
			return -2;
		}		
	}
#if TDAV_UNDER_MOBILE /* iOS, Android and WP8 */
	opus_encoder_ctl(opus->encoder.inst, OPUS_SET_COMPLEXITY(3));
#endif
    opus_encoder_ctl(opus->encoder.inst, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));

	return 0;
}

static int tdav_codec_opus_close(tmedia_codec_t* self)
{
	tdav_codec_opus_t* opus = (tdav_codec_opus_t*)self;	

	(void)(opus);
	
	/* resources will be freed by the dctor() */
	
	return 0;
}

static tsk_size_t tdav_codec_opus_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_opus_t* opus = (tdav_codec_opus_t*)self;	
	opus_int32 ret;
	
	if(!self || !in_data || !in_size || !out_data){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}

	if(!opus->encoder.inst){
		TSK_DEBUG_ERROR("Encoder not ready");
		return 0;
	}
	
	// we're sure that the output (encoded) size cannot be higher than the input (raw)
	if(*out_max_size < in_size){
		if(!(*out_data = tsk_realloc(*out_data, in_size))){
			TSK_DEBUG_ERROR("Failed to allocate buffer with size = %u", in_size);
			*out_max_size  = 0;
			return 0;
		}
		*out_max_size = in_size;
	}

	ret = opus_encode(opus->encoder.inst, 
		(const opus_int16 *)in_data, (int)(in_size >> 1), 
		(unsigned char *)*out_data, (opus_int32)*out_max_size);

	if(ret < 0){
		TSK_DEBUG_ERROR("opus_encode() failed with error code = %d", ret);
		return 0;
	}

	return (tsk_size_t)ret;
}

static tsk_size_t tdav_codec_opus_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)
{
	tdav_codec_opus_t* opus = (tdav_codec_opus_t*)self;
	int frame_size;
	const trtp_rtp_header_t* rtp_hdr = proto_hdr;

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

	if(!opus->decoder.inst){
		TSK_DEBUG_ERROR("Decoder not ready");
		return 0;
	}

	/* Packet loss? */
	if(opus->decoder.last_seq != (rtp_hdr->seq_num - 1) && opus->decoder.last_seq){
		if(opus->decoder.last_seq == rtp_hdr->seq_num){
			// Could happen on some stupid emulators
			//TSK_DEBUG_INFO("Packet duplicated, seq_num=%d", rtp_hdr->seq_num);
			return 0;
		}
		TSK_DEBUG_INFO("[Opus] Packet loss, seq_num=%d", rtp_hdr->seq_num);
		opus_decode(opus->decoder.inst, tsk_null/*packet loss*/, (opus_int32)0, opus->decoder.buff, TDAV_OPUS_MAX_FRAME_SIZE_IN_SAMPLES, opus->decoder.fec_enabled);
	}
	opus->decoder.last_seq = rtp_hdr->seq_num;

	frame_size = opus_decode(opus->decoder.inst, (const unsigned char *)in_data, (opus_int32)in_size, opus->decoder.buff, TDAV_OPUS_MAX_FRAME_SIZE_IN_SAMPLES, opus->decoder.fec_enabled ? 1 : 0);
	if(frame_size > 0){
		tsk_size_t frame_size_inbytes = (frame_size << 1);
		if(*out_max_size < frame_size_inbytes){
			if(!(*out_data = tsk_realloc(*out_data, frame_size_inbytes))){
				TSK_DEBUG_ERROR("Failed to allocate new buffer");
				*out_max_size = 0;
				return 0;
			}
			*out_max_size = frame_size_inbytes;
		}
		memcpy(*out_data, opus->decoder.buff, frame_size_inbytes);
		return frame_size_inbytes;
	}
	else{
		return 0;
	}
}

static tsk_bool_t tdav_codec_opus_sdp_att_match(const tmedia_codec_t* codec, const char* att_name, const char* att_value)
{
	tdav_codec_opus_t* opus = (tdav_codec_opus_t*)codec;

	if(!opus){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_false;
	}

	TSK_DEBUG_INFO("[OPUS] Trying to match [%s:%s]", att_name, att_value);

	if(tsk_striequals(att_name, "fmtp")){
		int val_int;
		tsk_params_L_t* params;
		/* e.g. FIXME */
		if((params = tsk_params_fromstring(att_value, ";", tsk_true))){
			tsk_bool_t ret = tsk_false;
			/* === maxplaybackrate ===*/
			if((val_int = tsk_params_get_param_value_as_int(params, "maxplaybackrate")) != -1){
				if(!_tdav_codec_opus_rate_is_valid(val_int)){
					TSK_DEBUG_ERROR("[OPUS] %d not valid as maxplaybackrate value", val_int);
					goto done;
				}
				TMEDIA_CODEC(opus)->out.rate = TSK_MIN((int32_t)TMEDIA_CODEC(opus)->out.rate, val_int);
				TMEDIA_CODEC_AUDIO(opus)->out.timestamp_multiplier = tmedia_codec_audio_get_timestamp_multiplier(codec->id, codec->out.rate);
			}
			/* === sprop-maxcapturerate ===*/
			if((val_int = tsk_params_get_param_value_as_int(params, "sprop-maxcapturerate")) != -1){
				if(!_tdav_codec_opus_rate_is_valid(val_int)){
					TSK_DEBUG_ERROR("[OPUS] %d not valid as sprop-maxcapturerate value", val_int);
					goto done;
				}
				TMEDIA_CODEC(opus)->in.rate = TSK_MIN((int32_t)TMEDIA_CODEC(opus)->in.rate, val_int);
				TMEDIA_CODEC_AUDIO(opus)->in.timestamp_multiplier = tmedia_codec_audio_get_timestamp_multiplier(codec->id, codec->in.rate);
			}
			ret = tsk_true;
done:
			TSK_OBJECT_SAFE_FREE(params);
			return ret;
		}
	}

	return tsk_true;
}

static char* tdav_codec_opus_sdp_att_get(const tmedia_codec_t* codec, const char* att_name)
{
	tdav_codec_opus_t* opus = (tdav_codec_opus_t*)codec;

	if(!opus){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_null;
	}

	if(tsk_striequals(att_name, "fmtp")){
		char* fmtp = tsk_null;
		tsk_sprintf(&fmtp, "maxplaybackrate=%d; sprop-maxcapturerate=%d; stereo=%d; sprop-stereo=%d; useinbandfec=%d; usedtx=%d", 
				TMEDIA_CODEC(opus)->in.rate, 
				TMEDIA_CODEC(opus)->out.rate, 
				(TMEDIA_CODEC_AUDIO(opus)->in.channels == 2) ? 1 : 0, 
				(TMEDIA_CODEC_AUDIO(opus)->out.channels == 2) ? 1 : 0, 
				opus->decoder.fec_enabled ? 1 : 0,
				opus->decoder.dtx_enabled ? 1 : 0
			);
		return fmtp;
	}

	return tsk_null;
}

//
//	OPUS Plugin definition
//

/* constructor */
static tsk_object_t* tdav_codec_opus_ctor(tsk_object_t * self, va_list * app)
{
	tdav_codec_opus_t *opus = self;
	if(opus){
		/* init base: called by tmedia_codec_create() */
		/* init self */
		TMEDIA_CODEC(opus)->in.rate = tmedia_defaults_get_opus_maxplaybackrate();
		TMEDIA_CODEC(opus)->out.rate = tmedia_defaults_get_opus_maxcapturerate();
		TMEDIA_CODEC_AUDIO(opus)->in.channels = 1;
		TMEDIA_CODEC_AUDIO(opus)->out.channels = 1;
#if TDAV_OPUS_FEC_ENABLED
		opus->decoder.fec_enabled = tsk_true;
#endif
#if TDAV_OPUS_DTX_ENABLED
		opus->decoder.dtx_enabled = tsk_true;
#endif
	}
	return self;
}
/* destructor */
static tsk_object_t* tdav_codec_opus_dtor(tsk_object_t * self)
{ 
	tdav_codec_opus_t *opus = self;
	if(opus){
		/* deinit base */
		tmedia_codec_audio_deinit(opus);
		/* deinit self */
		if(opus->decoder.inst){
			opus_decoder_destroy(opus->decoder.inst), opus->decoder.inst = tsk_null;
		}
		if(opus->encoder.inst){
			opus_encoder_destroy(opus->encoder.inst), opus->encoder.inst = tsk_null;
		}
	}

	return self;
}
/* object definition */
static const tsk_object_def_t tdav_codec_opus_def_s = 
{
	sizeof(tdav_codec_opus_t),
	tdav_codec_opus_ctor, 
	tdav_codec_opus_dtor,
	tmedia_codec_cmp, 
};
/* plugin definition*/
static const tmedia_codec_plugin_def_t tdav_codec_opus_plugin_def_s = 
{
	&tdav_codec_opus_def_s,

	tmedia_audio,
	tmedia_codec_id_opus,
	"opus",
	"opus Codec",
	TMEDIA_CODEC_FORMAT_OPUS,
	tsk_true,
	48000, // this is the default sample rate
	
	{ /* audio */
		2, // channels
		0 // ptime @deprecated
	},

	/* video */
	{0},

	tsk_null, // set()
	tdav_codec_opus_open,
	tdav_codec_opus_close,
	tdav_codec_opus_encode,
	tdav_codec_opus_decode,
	tdav_codec_opus_sdp_att_match,
	tdav_codec_opus_sdp_att_get
};
const tmedia_codec_plugin_def_t *tdav_codec_opus_plugin_def_t = &tdav_codec_opus_plugin_def_s;


#endif /* HAVE_LIBOPUS */
OpenPOWER on IntegriCloud