10ec7f84338524d55e220ea0a905fbc830d8394e
[firmerware] / src / codec / wrap.c
1 /* wrap.c -- API wrappers for the selected codecs.
2  *
3  * "Een beetje van jezelf, een beetje is magisch."
4  * (Maar wij kunnen het zonder GSM of MSG.)
5  *
6  * This file is part of 0cpm Firmerware.
7  *
8  * 0cpm Firmerware is Copyright (c)2011 Rick van Rein, OpenFortress.
9  *
10  * 0cpm Firmerware is free software: you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation, version 3.
13  *
14  * 0cpm Firmerware is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with 0cpm Firmerware.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include <stdint.h>
25 #include <stdbool.h>
26
27 #include <0cpm/codec.h>
28
29
30 /** \defgroup codec
31  * Codecs pack and unpack media streams for delivery over RTP.
32  * The abilities of codecs are negotiated using SDP-descriptions,
33  * commonly sent in the body of a SIP or SAP message.
34  */
35
36 /** \ingroup codec
37  * The codec-wrapper functionality aligns the internal API used
38  * by the 0cpm Firmerware with codec software used from other
39  * open source projects.
40  */
41
42
43
44 /********** PAYLOAD TYPE ALLOCATIONS **********/
45
46 // Pay  Mime-type               Rate    Registry
47 //
48 // 0    audio/PCMU              8000    IANA
49 // 8    audio/PCMA              8000    IANA
50 // 99   audio/L16               var.    LOCAL (arbitrary sample rate)
51 // 98   text/t140               1000    LOCAL
52 // 100  text/red                1000    LOCAL
53 // 101  audio/telephone-event   8000    LOCAL (but commonly used)
54 // 110  audio/G726-32           8000    LOCAL
55 // 111  audio/AAL2-G726-32      8000    LOCAL
56 // 125  audio/x-codec2          2550    LOCAL
57 // 124  audio/x-codec2          2400    LOCAL
58 // 123  audio/x-codec2          2000    LOCAL
59 // 122  audio/x-codec2          1500    LOCAL
60 // 121  audio/x-codec2          1000?   RESERVED
61 // 126  audio/speex             var.    LOCAL
62 // 127  audio/vorbis            var.    LOCAL
63
64
65
66 /********** G.711 DEFINITIONS **********/
67
68 #ifdef CONFIG_CODEC_G711
69
70
71 static void g711_init_ulaw (struct codec *hdl, uint32_t samplerate) {
72         g711_init (&hdl->state.state_g711, G711_ULAW);
73 }
74
75
76 static void g711_init_alaw (struct codec *hdl, uint32_t samplerate) {
77         g711_init (&hdl->state.state_g711, G711_ALAW);
78 }
79
80 static void g711_finish (struct codec *hdl) {
81         g711_release (&hdl->state.state_g711);
82 }
83
84 static void g711_transform_encode (struct codec *hdl,
85                         int16_t *pcm, uint16_t *pcmlen,
86                         uint8_t *pkt, uint16_t *pktlen) {
87         uint16_t len = *pcmlen;
88         if (*pktlen < *pcmlen) {
89                 len = *pktlen;
90         }
91         len = g711_encode (&hdl->state.state_g711, pkt, pcm, len);
92         *pcmlen = *pktlen = len;
93 }
94
95 static void g711_transform_decode (struct codec *hdl,
96                         int16_t *pcm, uint16_t *pcmlen,
97                         uint8_t *pkt, uint16_t *pktlen) {
98         uint16_t len = *pcmlen;
99         if (*pktlen < *pcmlen) {
100                 len = *pktlen;
101         }
102         len = g711_decode (&hdl->state.state_g711, pcm, pkt, len);
103         *pcmlen = *pktlen = len;
104 }
105
106 struct codec_fun encoder_g711_ulaw = {
107         "audio", "PCMU", NULL,
108         "PCMU",
109         g711_init_ulaw, g711_finish, g711_transform_encode,
110         8000, 0
111 };
112
113 struct codec_fun decoder_g711_ulaw = {
114         "audio", "PCMU", NULL,
115         "PCMU",
116         g711_init_ulaw, g711_finish, g711_transform_decode,
117         8000, 0
118 };
119
120 struct codec_fun encoder_g711_alaw = {
121         "audio", "PCMA", NULL,
122         "PCMA",
123         g711_init_alaw, g711_finish, g711_transform_encode,
124         8000, 8
125 };
126
127 struct codec_fun decoder_g711_alaw = {
128         "audio", "PCMA", NULL,
129         "PCMA",
130         g711_init_alaw, g711_finish, g711_transform_decode,
131         8000, 8
132 };
133
134 #endif
135
136
137
138 /********** G.722 DEFINITIONS **********/
139
140 #ifdef CONFIG_CODEC_G722
141
142 static void g722_init_encode (struct codec *hdl, uint32_t samplerate) {
143         g722_encode_init (&hdl->state.state_g722_encode, 64000, 0);
144 }
145
146 static void g722_init_decode (struct codec *hdl, uint32_t samplerate) {
147         g722_decode_init (&hdl->state.state_g722_decode, 64000, 0);
148 }
149
150 static void g722_finish_encode (struct codec *hdl) {
151         g722_encode_release (&hdl->state.state_g722_encode);
152 }
153
154 static void g722_finish_decode (struct codec *hdl) {
155         g722_decode_release (&hdl->state.state_g722_decode);
156 }
157
158 static void g722_transform_encode (struct codec *hdl,
159                         int16_t *pcm, uint16_t *pcmlen,
160                         uint8_t *pkt, uint16_t *pktlen) {
161         uint16_t len = *pcmlen;
162         if (*pktlen < *pcmlen) {
163                 len = *pktlen;
164         }
165         len = g722_encode (&hdl->state.state_g722_encode, pkt, pcm, len);
166         *pcmlen = *pktlen = len;
167 }
168
169 static void g722_transform_decode (struct codec *hdl,
170                         int16_t *pcm, uint16_t *pcmlen,
171                         uint8_t *pkt, uint16_t *pktlen) {
172         uint16_t len = *pcmlen;
173         if (*pktlen < *pcmlen) {
174                 len = *pktlen;
175         }
176         len = g722_decode (&hdl->state.state_g722_decode, pcm, pkt, len);
177         *pcmlen = *pktlen = len;
178 }
179
180 struct codec_fun encoder_g722_historically_incorrect = {
181         "audio", "G722", NULL,
182         "HD-G722",
183         g722_init_encode, g722_finish_encode, g722_transform_encode,
184         8000 /*history error*/, 9
185 };
186 struct codec_fun decoder_g722_historically_incorrect = {
187         "audio", "G722", NULL,
188         "HD-G722",
189         g722_init_decode, g722_finish_decode, g722_transform_decode,
190         8000 /*history error*/, 9
191 };
192 struct codec_fun encoder_g722 = {
193         "audio", "G722", NULL,
194         "HD-G722",
195         g722_init_encode, g722_finish_encode, g722_transform_encode,
196         16000, 9
197 };
198 struct codec_fun decoder_g722 = {
199         "audio", "G722", NULL,
200         "HD-G722",
201         g722_init_decode, g722_finish_decode, g722_transform_decode,
202         16000, 9
203 };
204 #endif
205
206
207
208 /********** G.726 DEFINITIONS **********/
209
210
211 #ifdef CONFIG_CODEC_G726
212
213
214 static void g726_init_encode (struct codec *hdl, uint32_t samplerate) {
215         /* TODO: G726_ENCODING_ULAW... and/or _ALAW? RTP profile? */
216         g726_init (&hdl->state.state_g726_encode, 32000, G726_ENCODING_ULAW, G726_PACKING_LEFT);
217 }
218
219 static void g726_init_decode (struct codec *hdl, uint32_t samplerate) {
220         /* TODO: G726_ENCODING_ULAW... and/or _ALAW? RTP profile? */
221         g726_init (&hdl->state.state_g726_decode, 32000, G726_ENCODING_ULAW, G726_PACKING_LEFT);
222 }
223
224 static void g726aal2_init_encode (struct codec *hdl, uint32_t samplerate) {
225         /* TODO: G726_ENCODING_ULAW... and/or _ALAW? RTP profile? */
226         g726_init (&hdl->state.state_g726_encode, 32000, G726_ENCODING_ULAW, G726_PACKING_RIGHT);
227 }
228
229 static void g726aal2_init_decode (struct codec *hdl, uint32_t samplerate) {
230         /* TODO: G726_ENCODING_ULAW... and/or _ALAW? RTP profile? */
231         g726_init (&hdl->state.state_g726_decode, 32000, G726_ENCODING_ULAW, G726_PACKING_RIGHT);
232 }
233
234
235 static void g726_finish_encode (struct codec *hdl) {
236         g726_release (&hdl->state.state_g726_encode);
237 }
238
239 static void g726_finish_decode (struct codec *hdl) {
240         g726_release (&hdl->state.state_g726_decode);
241 }
242
243 static void g726_transform_encode (struct codec *hdl,
244                         int16_t *pcm, uint16_t *pcmlen,
245                         uint8_t *pkt, uint16_t *pktlen) {
246         uint16_t len = *pcmlen;
247         if (*pktlen < *pcmlen) {
248                 len = *pktlen;
249         }
250         len = g726_encode (&hdl->state.state_g726_encode, pkt, pcm, len);
251         *pcmlen = *pktlen = len;
252 }
253
254 static void g726_transform_decode (struct codec *hdl,
255                         int16_t *pcm, uint16_t *pcmlen,
256                         uint8_t *pkt, uint16_t *pktlen) {
257         uint16_t len = *pcmlen;
258         if (*pktlen < *pcmlen) {
259                 len = *pktlen;
260         }
261         len = g726_decode (&hdl->state.state_g726_decode, pcm, pkt, len);
262         *pcmlen = *pktlen = len;
263 }
264
265
266
267 struct codec_fun encoder_g726 = {
268         "audio", "G726-32", NULL,
269         "DECT",
270         g726_init_encode, g726_finish_encode, g726_transform_encode,
271         8000, 110
272 };
273 struct codec_fun decoder_g726 = {
274         "audio", "G726-32", NULL,
275         "DECT",
276         g726_init_decode, g726_finish_decode, g726_transform_decode,
277         8000, 110
278 };
279
280 struct codec_fun encoder_g726aal2 = {
281         "audio", "AAL2-G726-32", NULL,
282         "DECT",
283         g726aal2_init_encode, g726_finish_encode, g726_transform_encode,
284         8000, 111
285 };
286 struct codec_fun decoder_g726aal2 = {
287         "audio", "AAL2-G726-32", NULL,
288         "DECT",
289         g726aal2_init_decode, g726_finish_decode, g726_transform_decode,
290         8000, 111
291 };
292 #endif
293
294
295
296 /********** SPEEX DEFINITIONS **********/
297
298 #if defined (CONFIG_CODEC_SPEEX_NARROWBAND) || defined (CONFIG_CODEC_SPEEX_WIDEBAND) || defined (CONFIG_CODEC_SPEEX_ULTRAWIDEBAND)
299
300 void speex_init_encode (struct codec *hdl, uint32_t samplerate) {
301         speex_encoder_init (&hdl->state.state_speex_encode);
302 }
303 void speex_finish_encode (struct codec *hdl) {
304         speex_encoder_destroy (&hdl->state.state_speex_encode);
305 }
306 void speex_transform_encode (struct codec *hdl,
307                         uint16_t *pcm, uint16_t *pcmlen,
308                         uint8_t  *pkt, uint16_t *pktlen) {
309         speex_encode (&hdl->state.state_speex_encode, NULL /*TODO*/);
310 }
311 void speex_init_decode (struct codec *hdl, uint32_t samplerate) {
312         speex_decoder_init (&hdl->state.state_speex_decode);
313 }
314 void speex_finish_decode (struct codec *hdl) {
315         speex_decoder_destroy (&hdl->state.state_speex_decode);
316 }
317 void speex_transform_decode (struct codec *hdl, uint16_t *pktlen) {
318         speex_decode (&hdl->state.state_speex_decode, NULL /*TODO*/);
319 }
320
321 #endif
322
323 #ifdef CONFIG_CODEC_SPEEX_NARROWBAND
324 struct codec_fun encoder_speex_narrow = {
325         "audio", "speex", "mode=any;vbr=on;cng=on",
326         "SPEEX",
327         speex_init_encode, speex_finish_encode, speex_transform_encode,
328         8000, 126
329 };
330 struct codec_fun decoder_speex_narrow = {
331         "audio", "speex", "mode=any;vbr=on;cng=on",
332         "SPEEX",
333         speex_init_decode, speex_finish_decode, speex_transform_decode,
334         8000, 126
335 };
336 #endif
337
338 #ifdef CONFIG_CODEC_SPEEX_WIDEBAND
339 struct codec_fun encoder_speex_wide = {
340         "audio", "speex", "mode=any;vbr=on;cng=on",
341         "SPEEX",
342         speex_init_encode, speex_finish_encode, speex_transform_encode,
343         16000, 126
344 };
345 struct codec_fun decoder_speex_wide = {
346         "audio", "speex", "mode=any;vbr=on;cng=on",
347         "SPEEX",
348         speex_init_decode, speex_finish_decode, speex_transform_decode,
349         16000, 126
350 };
351 #endif
352
353 #ifdef CONFIG_CODEC_SPEEX_ULTRAWIDEBAND
354 struct codec_fun encoder_speex_ultra = {
355         "audio", "speex", "mode=any;vbr=on;cng=on",
356         "SPEEX",
357         speex_init_encode, speex_finish_encode, speex_transform_encode,
358         32000, 126
359 };
360 struct codec_fun decoder_speex_ultra = {
361         "audio", "speex", "mode=any;vbr=on;cng=on",
362         "SPEEX",
363         speex_init_decode, speex_finish_decode, speex_transform_decode,
364         32000, 126
365 };
366 #endif
367
368
369 /********** CODEC2 DEFINITIONS **********/
370
371 static void codec2_init (struct codec *hdl, uint32_t samplerate) {
372         //TODO// Static allocation
373         codec2_create (&hdl->state.codec2_state);
374 }
375
376 static void codec2_finish (struct codec *hdl, uint32_t samplerate) {
377         codec2_destroy (&hdl->state.codec2_state);
378 }
379
380 static void codec2_transform_encode (struct codec *hdl,
381                         int16_t *pcm, uint16_t *pcmlen,
382                         uint8_t *pkt, uint16_t *pktlen) {
383         codec2_encode (hdl, pkt, pcm);
384         *pcmlen = CODEC2_SAMPLES_PER_FRAME;
385         *pktlen = (CODEC2_BITS_PER_FRAME + 7) >> 3;
386 }
387
388 static void codec2_transform_decode (struct codec *hdl,
389                         int16_t *pcm, uint16_t *pcmlen,
390                         uint8_t *pkt, uint16_t *pktlen) {
391         codec2_decode (hdl, pcm, pkt);
392         *pcmlen = CODEC2_SAMPLES_PER_FRAME;
393         *pktlen = (CODEC2_BITS_PER_FRAME + 7) >> 3;
394 }
395
396
397 #ifdef CONFIG_CODEC_CODEC2
398 struct codec_fun encoder_codec2_1500 = {
399         "audio", "x-codec2", NULL,
400         "CODEC2",
401         codec2_init, codec2_finish, codec2_transform_encode,
402         1500, 122
403 };
404 struct codec_fun decoder_codec2_1500 = {
405         "audio", "x-codec2", NULL,
406         "CODEC2",
407         codec2_init, codec2_finish, codec2_transform_decode,
408         1500, 122
409 };
410 struct codec_fun encoder_codec2_2000 = {
411         "audio", "x-codec2", NULL,
412         "CODEC2",
413         codec2_init, codec2_finish, codec2_transform_encode,
414         2000, 123
415 };
416 struct codec_fun decoder_codec2_2000 = {
417         "audio", "x-codec2", NULL,
418         "CODEC2",
419         codec2_init, codec2_finish, codec2_transform_decode,
420         2000, 123
421 };
422 struct codec_fun encoder_codec2_2400 = {
423         "audio", "x-codec2", NULL,
424         "CODEC2",
425         codec2_init, codec2_finish, codec2_transform_encode,
426         2400, 124
427 };
428 struct codec_fun decoder_codec2_2400 = {
429         "audio", "x-codec2", NULL,
430         "CODEC2",
431         codec2_init, codec2_finish, codec2_transform_decode,
432         2400, 124
433 };
434 struct codec_fun encoder_codec2_2550 = {
435         "audio", "x-codec2", NULL,
436         "CODEC2",
437         codec2_init, codec2_finish, codec2_transform_encode,
438         2550, 125
439 };
440 struct codec_fun decoder_codec2_2550 = {
441         "audio", "x-codec2", NULL,
442         "CODEC2",
443         codec2_init, codec2_finish, codec2_transform_decode,
444         2550, 125
445 };
446 #endif
447
448
449 /********** TELEPHONE EVENT DEFINITIONS **********/
450
451 #ifdef CONFIG_SIP_PHONE
452 struct codec_fun encodor_televt = {
453         "audio", "telephone-event", "0-16",
454         "DTMF",
455         NULL, NULL, NULL, /*TODO*/
456         8000, 101
457 };
458 struct codec_fun decoder_televt = {
459         "audio", "telephone-event", "0-16",
460         "DTMF",
461         NULL, NULL, NULL, /*TODO*/
462         8000, 101
463 };
464 #endif
465
466 /********** REALTIME TEXT DEFINITIONS **********/
467
468 #ifdef CONFIG_CODEC_RTT
469 struct codec_fun encoder_rtt = {
470         "text", "t140", "cps=1000",
471         "TEXT",
472         NULL, NULL, NULL, /*TODO*/
473         1000, 98
474 };
475 struct codec_fun decoder_rtt = {
476         "text", "t140", "cps=1000",
477         "TEXT",
478         NULL, NULL, NULL, /*TODO*/
479         1000, 98
480 };
481 struct codec_fun encoder_rtt_red = {
482         "text", "red", "98/98/98",
483         "TEXT",
484         NULL, NULL, NULL, /*TODO*/
485         1000, 100
486 };
487 struct codec_fun decoder_rtt_red = {
488         "text", "red", "98/98/98",
489         "TEXT",
490         NULL, NULL, NULL, /*TODO*/
491         1000, 100
492 };
493 #endif
494
495 #ifdef CONFIG_MULTICAST_CODEC_RTT
496 struct codec_fun decoder_mcast_rtt = {
497         "text", "t140", "cps=1000",
498         "TEXT",
499         NULL, NULL, NULL, /*TODO*/
500         1000, 98
501 };
502 struct codec_fun decoder_mcast_rtt_red = {
503         "text", "red", "98/98/98",
504         "TEXT",
505         NULL, NULL, NULL, /*TODO*/
506         1000, 100
507 };
508 #endif
509
510 #ifdef CONFIG_MULTICAST_CODEC_L16
511 struct codec_fun decoder_mcast_l16 = {
512         "audio", "L16", NULL,
513         "L16",
514         NULL, NULL, NULL, /*TODO*/
515         0, 99
516 };
517 #endif
518
519 #ifdef CONFIG_MULTICAST_CODEC_VORBIS
520 struct codec_fun decoder_mcast_vorbis = {
521         "audio", "vorbis", "configuration=TODO:RFC5215",
522         "VORBIS",
523         NULL, NULL, NULL, /*TODO*/
524         0, 127
525 };
526 #endif
527
528
529
530 /********** SDP SUPPORT THROUGH CODEC LIST STRUCTURES **********/
531
532 struct codec_fun *codec_phone_audio_encoders [] = {
533 #ifdef CONFIG_CODEC_SPEEX_ULTRAWIDEBAND
534         &encoder_speex_ultra,
535 #endif
536 #ifdef CONFIG_CODEC_SPEEX_WIDEBAND
537         &encoder_speex_wide,
538 #endif
539 #ifdef CONFIG_CODEC_G722
540         &encoder_g722,
541         &encoder_g722_historically_incorrect,
542 #endif
543 #ifdef CONFIG_CODEC_SPEEX_NARROWBAND
544         &encoder_speex_narrow,
545 #endif
546 #ifdef CONFIG_CODEC_G726
547         &encoder_g726,
548 #endif
549 #ifdef CONFIG_CODEC_G711
550         &encoder_g711_ulaw,     /* 14 bits */
551         &encoder_g711_alaw,     /* 13 bits */
552 #endif
553 #ifdef CONFIG_CODEC_CODEC2
554         &encoder_codec2,
555 #endif
556 #ifdef CONFIG_SIP_PHONE
557         &encoder_televt,
558 #endif
559         NULL
560 };
561
562 struct codec_fun *codec_phone_audio_decoders [] = {
563 #ifdef CONFIG_CODEC_SPEEX_ULTRAWIDEBAND
564         &decoder_speex_ultra,
565 #endif
566 #ifdef CONFIG_CODEC_SPEEX_WIDEBAND
567         &decoder_speex_wide,
568 #endif
569 #ifdef CONFIG_CODEC_G722
570         &decoder_g722,
571         &decoder_g722_historically_incorrect,
572 #endif
573 #ifdef CONFIG_CODEC_SPEEX_NARROWBAND
574         &decoder_speex_narrow,
575 #endif
576 #ifdef CONFIG_CODEC_G726
577         &decoder_g726,
578 #endif
579 #ifdef CONFIG_CODEC_G711
580         &decoder_g711_ulaw,     /* 14 bits */
581         &decoder_g711_alaw,     /* 13 bits */
582 #endif
583 #ifdef CONFIG_CODEC_CODEC2
584         &decoder_codec2,
585 #endif
586 #ifdef CONFIG_SIP_PHONE
587         &decoder_televt,
588 #endif
589         NULL
590 };
591
592 struct codec_fun *codec_phone_video_encoders [] = {
593         NULL
594 };
595
596 struct codec_fun *codec_phone_video_decoders [] = {
597         NULL
598 };
599
600 struct codec_fun *codec_phone_text_encoders [] = {
601 #ifdef CONFIG_CODEC_RTT
602         &encoder_rtt_red,
603         &encoder_rtt,
604 #endif
605         NULL
606 };
607
608 struct codec_fun *codec_phone_text_decoders [] = {
609 #ifdef CONFIG_CODEC_RTT
610         &decoder_rtt_red,
611         &decoder_rtt,
612 #endif
613         NULL
614 };
615
616 struct codec_fun *codec_mcast_audio_decoders [] = {
617 #ifdef CONFIG_MULTICAST_CODEC_L16
618         &decoder_mcast_l16,
619 #endif
620 #ifdef CONFIG_MULTICAST_CODEC_VORBIS
621         &decoder_mcast_vorbis,
622 #endif
623 #ifdef CONFIG_MULTICAST_CODEC_SPEEX
624         &decoder_mcast_speex,
625 #endif
626         NULL
627 };
628
629 struct codec_fun *codec_mcast_video_decoders [] = {
630         NULL
631 };
632
633 struct codec_fun *codec_mcast_text_decoders [] = {
634 #ifdef CONFIG_CODEC_RTT
635         &encoder_rtt_red,
636         &encoder_rtt,
637 #endif
638         NULL
639 };
640