blob: cadc8769e59a1dbce3fafabfd235d8e2a8b70b86 [file] [log] [blame]
Jacob Erlbeck9fac8002014-03-13 14:25:51 +01001/*
2 * (C) 2014 by Sysmocom s.f.m.c. GmbH
3 * (C) 2014 by On-Waves
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <stdlib.h>
22#include <string.h>
23#include <errno.h>
24
25#include "bscconfig.h"
26
27#include "g711common.h"
28#include <gsm.h>
29#ifdef HAVE_BCG729
30#include <bcg729/decoder.h>
31#include <bcg729/encoder.h>
32#endif
33
34#include <openbsc/debug.h>
35#include <openbsc/mgcp.h>
36#include <openbsc/mgcp_internal.h>
37
38#include <osmocom/core/talloc.h>
39
40enum audio_format {
41 AF_INVALID,
42 AF_S16,
43 AF_L16,
44 AF_GSM,
45 AF_G729,
46 AF_PCMA
47};
48
49struct mgcp_process_rtp_state {
50 /* decoding */
51 enum audio_format src_fmt;
52 union {
53 gsm gsm_handle;
54#ifdef HAVE_BCG729
55 bcg729DecoderChannelContextStruct *g729_dec;
56#endif
57 } src;
58 size_t src_frame_size;
59 size_t src_samples_per_frame;
60
61 /* processing */
62
63 /* encoding */
64 enum audio_format dst_fmt;
65 union {
66 gsm gsm_handle;
67#ifdef HAVE_BCG729
68 bcg729EncoderChannelContextStruct *g729_enc;
69#endif
70 } dst;
71 size_t dst_frame_size;
72 size_t dst_samples_per_frame;
73};
74
75static enum audio_format get_audio_format(const struct mgcp_rtp_end *rtp_end)
76{
77 if (rtp_end->subtype_name) {
78 if (!strcmp("GSM", rtp_end->subtype_name))
79 return AF_GSM;
80 if (!strcmp("PCMA", rtp_end->subtype_name))
81 return AF_PCMA;
82#ifdef HAVE_BCG729
83 if (!strcmp("G729", rtp_end->subtype_name))
84 return AF_G729;
85#endif
86 if (!strcmp("L16", rtp_end->subtype_name))
87 return AF_L16;
88 }
89
90 switch (rtp_end->payload_type) {
91 case 3 /* GSM */:
92 return AF_GSM;
93 case 8 /* PCMA */:
94 return AF_PCMA;
95#ifdef HAVE_BCG729
96 case 18 /* G.729 */:
97 return AF_G729;
98#endif
99 case 11 /* L16 */:
100 return AF_L16;
101 default:
102 return AF_INVALID;
103 }
104}
105
106static void l16_encode(short *sample, unsigned char *buf, size_t n)
107{
108 for (; n > 0; --n, ++sample, buf += 2) {
109 buf[0] = sample[0] >> 8;
110 buf[1] = sample[0] & 0xff;
111 }
112}
113
114static void l16_decode(unsigned char *buf, short *sample, size_t n)
115{
116 for (; n > 0; --n, ++sample, buf += 2)
117 sample[0] = ((short)buf[0] << 8) | buf[1];
118}
119
120static void alaw_encode(short *sample, unsigned char *buf, size_t n)
121{
122 for (; n > 0; --n)
123 *(buf++) = s16_to_alaw(*(sample++));
124}
125
126static void alaw_decode(unsigned char *buf, short *sample, size_t n)
127{
128 for (; n > 0; --n)
129 *(sample++) = alaw_to_s16(*(buf++));
130}
131
132static int processing_state_destructor(struct mgcp_process_rtp_state *state)
133{
134 switch (state->src_fmt) {
135 case AF_GSM:
136 if (state->dst.gsm_handle)
137 gsm_destroy(state->src.gsm_handle);
138 break;
139#ifdef HAVE_BCG729
140 case AF_G729:
141 if (state->src.g729_dec)
142 closeBcg729DecoderChannel(state->src.g729_dec);
143 break;
144#endif
145 default:
146 break;
147 }
148 switch (state->dst_fmt) {
149 case AF_GSM:
150 if (state->dst.gsm_handle)
151 gsm_destroy(state->dst.gsm_handle);
152 break;
153#ifdef HAVE_BCG729
154 case AF_G729:
155 if (state->dst.g729_enc)
156 closeBcg729EncoderChannel(state->dst.g729_enc);
157 break;
158#endif
159 default:
160 break;
161 }
162 return 0;
163}
164
165int mgcp_transcoding_setup(struct mgcp_endpoint *endp,
166 struct mgcp_rtp_end *dst_end,
167 struct mgcp_rtp_end *src_end)
168{
169 struct mgcp_process_rtp_state *state;
170 enum audio_format src_fmt, dst_fmt;
171
172 /* cleanup first */
173 if (dst_end->rtp_process_data) {
174 talloc_free(dst_end->rtp_process_data);
175 dst_end->rtp_process_data = NULL;
176 }
177
178 if (!src_end)
179 return 0;
180
181 src_fmt = get_audio_format(src_end);
182 dst_fmt = get_audio_format(dst_end);
183
184 LOGP(DMGCP, LOGL_ERROR,
185 "Checking transcoding: %s (%d) -> %s (%d)\n",
186 src_end->subtype_name, src_end->payload_type,
187 dst_end->subtype_name, dst_end->payload_type);
188
189 if (src_fmt == AF_INVALID || dst_fmt == AF_INVALID) {
190 if (!src_end->subtype_name || !dst_end->subtype_name)
191 /* Not enough info, do nothing */
192 return 0;
193
194 if (strcmp(src_end->subtype_name, dst_end->subtype_name) == 0)
195 /* Nothing to do */
196 return 0;
197
198 LOGP(DMGCP, LOGL_ERROR,
199 "Cannot transcode: %s codec not supported (%s -> %s).\n",
200 src_fmt != AF_INVALID ? "destination" : "source",
201 src_end->audio_name, dst_end->audio_name);
202 return -EINVAL;
203 }
204
205 if (src_end->rate && dst_end->rate && src_end->rate != dst_end->rate) {
206 LOGP(DMGCP, LOGL_ERROR,
207 "Cannot transcode: rate conversion (%d -> %d) not supported.\n",
208 src_end->rate, dst_end->rate);
209 return -EINVAL;
210 }
211
212 state = talloc_zero(endp->tcfg->cfg, struct mgcp_process_rtp_state);
213 talloc_set_destructor(state, processing_state_destructor);
214 dst_end->rtp_process_data = state;
215
216 state->src_fmt = src_fmt;
217
218 switch (state->src_fmt) {
219 case AF_L16:
220 case AF_S16:
221 state->src_frame_size = 80 * sizeof(short);
222 state->src_samples_per_frame = 80;
223 break;
224 case AF_GSM:
225 state->src_frame_size = sizeof(gsm_frame);
226 state->src_samples_per_frame = 160;
227 state->src.gsm_handle = gsm_create();
228 if (!state->src.gsm_handle) {
229 LOGP(DMGCP, LOGL_ERROR,
230 "Failed to initialize GSM decoder.\n");
231 return -EINVAL;
232 }
233 break;
234#ifdef HAVE_BCG729
235 case AF_G729:
236 state->src_frame_size = 10;
237 state->src_samples_per_frame = 80;
238 state->src.g729_dec = initBcg729DecoderChannel();
239 if (!state->src.g729_dec) {
240 LOGP(DMGCP, LOGL_ERROR,
241 "Failed to initialize G.729 decoder.\n");
242 return -EINVAL;
243 }
244 break;
245#endif
246 case AF_PCMA:
247 state->src_frame_size = 80;
248 state->src_samples_per_frame = 80;
249 break;
250 default:
251 break;
252 }
253
254 state->dst_fmt = dst_fmt;
255
256 switch (state->dst_fmt) {
257 case AF_L16:
258 case AF_S16:
259 state->dst_frame_size = 80*sizeof(short);
260 state->dst_samples_per_frame = 80;
261 break;
262 case AF_GSM:
263 state->dst_frame_size = sizeof(gsm_frame);
264 state->dst_samples_per_frame = 160;
265 state->dst.gsm_handle = gsm_create();
266 if (!state->dst.gsm_handle) {
267 LOGP(DMGCP, LOGL_ERROR,
268 "Failed to initialize GSM encoder.\n");
269 return -EINVAL;
270 }
271 break;
272#ifdef HAVE_BCG729
273 case AF_G729:
274 state->dst_frame_size = 10;
275 state->dst_samples_per_frame = 80;
276 state->dst.g729_enc = initBcg729EncoderChannel();
277 if (!state->dst.g729_enc) {
278 LOGP(DMGCP, LOGL_ERROR,
279 "Failed to initialize G.729 decoder.\n");
280 return -EINVAL;
281 }
282 break;
283#endif
284 case AF_PCMA:
285 state->dst_frame_size = 80;
286 state->dst_samples_per_frame = 80;
287 break;
288 default:
289 break;
290 }
291
292 LOGP(DMGCP, LOGL_INFO,
293 "Initialized RTP processing on: 0x%x "
294 "conv: %d (%d, %d, %s) -> %d (%d, %d, %s)\n",
295 ENDPOINT_NUMBER(endp),
296 src_fmt, src_end->payload_type, src_end->rate, src_end->fmtp_extra,
297 dst_fmt, dst_end->payload_type, dst_end->rate, dst_end->fmtp_extra);
298
299 return 0;
300}
301
302void mgcp_transcoding_net_downlink_format(struct mgcp_endpoint *endp,
303 int *payload_type,
304 const char**audio_name,
305 const char**fmtp_extra)
306{
307 struct mgcp_process_rtp_state *state = endp->net_end.rtp_process_data;
308 if (!state || endp->net_end.payload_type < 0) {
309 *payload_type = endp->bts_end.payload_type;
310 *audio_name = endp->bts_end.audio_name;
311 *fmtp_extra = endp->bts_end.fmtp_extra;
312 return;
313 }
314
315 *payload_type = endp->net_end.payload_type;
316 *fmtp_extra = endp->net_end.fmtp_extra;
317 *audio_name = endp->net_end.audio_name;
318}
319
320
321int mgcp_transcoding_process_rtp(struct mgcp_endpoint *endp,
322 struct mgcp_rtp_end *dst_end,
323 char *data, int *len, int buf_size)
324{
325 struct mgcp_process_rtp_state *state = dst_end->rtp_process_data;
326 size_t rtp_hdr_size = 12;
327 char *payload_data = data + rtp_hdr_size;
328 int payload_len = *len - rtp_hdr_size;
329 size_t sample_cnt = 0;
330 size_t sample_idx;
331 int16_t samples[10*160];
332 uint8_t *src = (uint8_t *)payload_data;
333 uint8_t *dst = (uint8_t *)payload_data;
334 size_t nbytes = payload_len;
335 size_t frame_remainder;
336
337 if (!state)
338 return 0;
339
340 if (state->src_fmt == state->dst_fmt)
341 return 0;
342
343 /* TODO: check payload type (-> G.711 comfort noise) */
344
345 /* Decode src into samples */
346 while (nbytes >= state->src_frame_size) {
347 if (sample_cnt + state->src_samples_per_frame > ARRAY_SIZE(samples)) {
348 LOGP(DMGCP, LOGL_ERROR,
349 "Sample buffer too small: %d > %d.\n",
350 sample_cnt + state->src_samples_per_frame,
351 ARRAY_SIZE(samples));
352 return -ENOSPC;
353 }
354 switch (state->src_fmt) {
355 case AF_GSM:
356 if (gsm_decode(state->src.gsm_handle,
357 (gsm_byte *)src, samples + sample_cnt) < 0) {
358 LOGP(DMGCP, LOGL_ERROR,
359 "Failed to decode GSM.\n");
360 return -EINVAL;
361 }
362 break;
363#ifdef HAVE_BCG729
364 case AF_G729:
365 bcg729Decoder(state->src.g729_dec, src, 0, samples + sample_cnt);
366 break;
367#endif
368 case AF_PCMA:
369 alaw_decode(src, samples + sample_cnt,
370 state->src_samples_per_frame);
371 break;
372 case AF_S16:
373 memmove(samples + sample_cnt, src,
374 state->src_frame_size);
375 break;
376 case AF_L16:
377 l16_decode(src, samples + sample_cnt,
378 state->src_samples_per_frame);
379 break;
380 default:
381 break;
382 }
383 src += state->src_frame_size;
384 nbytes -= state->src_frame_size;
385 sample_cnt += state->src_samples_per_frame;
386 }
387
388 /* Add silence if necessary */
389 frame_remainder = sample_cnt % state->dst_samples_per_frame;
390 if (frame_remainder) {
391 size_t silence = state->dst_samples_per_frame - frame_remainder;
392 if (sample_cnt + silence > ARRAY_SIZE(samples)) {
393 LOGP(DMGCP, LOGL_ERROR,
394 "Sample buffer too small for silence: %d > %d.\n",
395 sample_cnt + silence,
396 ARRAY_SIZE(samples));
397 return -ENOSPC;
398 }
399
400 while (silence > 0) {
401 samples[sample_cnt] = 0;
402 sample_cnt += 1;
403 silence -= 1;
404 }
405 }
406
407 /* Encode samples into dst */
408 sample_idx = 0;
409 nbytes = 0;
410 while (sample_idx + state->dst_samples_per_frame <= sample_cnt) {
411 if (nbytes + state->dst_frame_size > buf_size) {
412 LOGP(DMGCP, LOGL_ERROR,
413 "Encoding (RTP) buffer too small: %d > %d.\n",
414 nbytes + state->dst_frame_size, buf_size);
415 return -ENOSPC;
416 }
417 switch (state->dst_fmt) {
418 case AF_GSM:
419 gsm_encode(state->dst.gsm_handle,
420 samples + sample_idx, dst);
421 break;
422#ifdef HAVE_BCG729
423 case AF_G729:
424 bcg729Encoder(state->dst.g729_enc,
425 samples + sample_idx, dst);
426 break;
427#endif
428 case AF_PCMA:
429 alaw_encode(samples + sample_idx, dst,
430 state->src_samples_per_frame);
431 break;
432 case AF_S16:
433 memmove(dst, samples + sample_idx, state->dst_frame_size);
434 break;
435 case AF_L16:
436 l16_encode(samples + sample_idx, dst,
437 state->src_samples_per_frame);
438 break;
439 default:
440 break;
441 }
442 dst += state->dst_frame_size;
443 nbytes += state->dst_frame_size;
444 sample_idx += state->dst_samples_per_frame;
445 }
446
447 *len = rtp_hdr_size + nbytes;
448 /* Patch payload type */
449 data[1] = (data[1] & 0x80) | (dst_end->payload_type & 0x7f);
450
451 return 0;
452}