summaryrefslogtreecommitdiffstats
path: root/src/audio/audio_encoder.h
blob: e2fb874d56d0ed488c533092ead66869c8eafc79 (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
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    This program 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 2 of the License, or
    (at your option) any later version.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// Audio encoders

#ifndef _AUDIO_ENCODER_H
#define _AUDIO_ENCODER_H

#include "twinkle_config.h"
#include "audio_codecs.h"
#include "user.h"

#ifdef HAVE_GSM
#include <gsm/gsm.h>
#else
#include "gsm/inc/gsm.h"
#endif

#ifdef HAVE_SPEEX
#include <speex/speex.h>
#endif

#ifdef HAVE_ILBC
#ifndef HAVE_ILBC_CPP
extern "C" {
#endif
#include <ilbc/iLBC_define.h>
#ifndef HAVE_ILBC_CPP
}
#endif
#endif

// Abstract definition of an audio encoder
class t_audio_encoder {
protected:
	t_audio_codec	_codec;
	uint16		_payload_id;		// payload id for the codec
	uint16		_ptime;			// in milliseconds
	uint16		_max_payload_size;	// maximum size of payload
	
	t_user		*_user_config;
	
	t_audio_encoder(uint16 payload_id, uint16 ptime, t_user *user_config);
	
public:
	virtual ~t_audio_encoder() {};
	
	t_audio_codec get_codec(void) const;
	uint16 get_payload_id(void) const;
	uint16 get_ptime(void) const;
	uint16 get_sample_rate(void) const;
	uint16 get_max_payload_size(void) const;
	
	// Encode a 16-bit PCM sample buffer to a encoded payload
	// Returns the number of bytes written into the payload.
	// The silence flag indicates if the returned sound samples represent silence
	// that may be suppressed.
	virtual uint16 encode(int16 *sample_buf, uint16 nsamples, 
			uint8 *payload, uint16 payload_size, bool &silence) = 0;
};


// G.711 A-law
class t_g711a_audio_encoder : public t_audio_encoder {
public:
	t_g711a_audio_encoder(uint16 payload_id, uint16 ptime, t_user *user_config);
	
	virtual uint16 encode(int16 *sample_buf, uint16 nsamples, 
			uint8 *payload, uint16 payload_size, bool &silence);
};


// G.711 u-law
class t_g711u_audio_encoder : public t_audio_encoder {
public:
	t_g711u_audio_encoder(uint16 payload_id, uint16 ptime, t_user *user_config);
	
	virtual uint16 encode(int16 *sample_buf, uint16 nsamples, 
			uint8 *payload, uint16 payload_size, bool &silence);
};


// GSM
class t_gsm_audio_encoder : public t_audio_encoder {
private:
	gsm 		gsm_encoder;
	
public:
	t_gsm_audio_encoder(uint16 payload_id, uint16 ptime, t_user *user_config);
	virtual ~t_gsm_audio_encoder();
	
	virtual uint16 encode(int16 *sample_buf, uint16 nsamples, 
			uint8 *payload, uint16 payload_size, bool &silence);
};


#ifdef HAVE_SPEEX
class t_speex_audio_encoder : public t_audio_encoder {
public:
	enum t_mode {
		MODE_NB,	// Narrow band
		MODE_WB,	// Wide band
		MODE_UWB	// Ultra wide band
	};
	
private:
	SpeexBits	speex_bits;
	void		*speex_enc_state;
	t_mode		_mode;
	
public:
	t_speex_audio_encoder(uint16 payload_id, uint16 ptime, t_mode mode, 
		t_user *user_config);
	virtual ~t_speex_audio_encoder();
	
	virtual uint16 encode(int16 *sample_buf, uint16 nsamples, 
			uint8 *payload, uint16 payload_size, bool &silence);
};
#endif

#ifdef HAVE_ILBC
class t_ilbc_audio_encoder : public t_audio_encoder {
private:
	iLBC_Enc_Inst_t	_ilbc_encoder;
	uint8		_mode;		// 20, 30 ms (frame size)
	
public:
	t_ilbc_audio_encoder(uint16 payload_id, uint16 ptime, 
		t_user *user_config);
	
	virtual uint16 encode(int16 *sample_buf, uint16 nsamples, 
			uint8 *payload, uint16 payload_size, bool &silence);
};
#endif

class t_g726_audio_encoder : public t_audio_encoder {
public:
	enum t_bit_rate {
		BIT_RATE_16,
		BIT_RATE_24,
		BIT_RATE_32,
		BIT_RATE_40
	};
	
private:
	uint16 encode_16(int16 *sample_buf, uint16 nsamples, 
			uint8 *payload, uint16 payload_size);
	uint16 encode_24(int16 *sample_buf, uint16 nsamples, 
			uint8 *payload, uint16 payload_size);
	uint16 encode_32(int16 *sample_buf, uint16 nsamples, 
			uint8 *payload, uint16 payload_size);
	uint16 encode_40(int16 *sample_buf, uint16 nsamples, 
			uint8 *payload, uint16 payload_size);

	g72x_state	_state;
	t_bit_rate	_bit_rate;
	t_g726_packing	_packing;
	
public:
	t_g726_audio_encoder(uint16 payload_id, uint16 ptime, t_bit_rate bit_rate, 
		t_user *user_config);
		
	virtual uint16 encode(int16 *sample_buf, uint16 nsamples, 
			uint8 *payload, uint16 payload_size, bool &silence);
};

#endif