Merged revisions 114708 via svnmerge from
[asterisk-bristuff.git] / main / frame.c
blobc49e8e661933f5b5953c3195252dff22af3e9c50
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Frame and codec manipulation routines
23 * \author Mark Spencer <markster@digium.com>
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/_private.h"
31 #include "asterisk/lock.h"
32 #include "asterisk/frame.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/cli.h"
35 #include "asterisk/term.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/threadstorage.h"
38 #include "asterisk/linkedlists.h"
39 #include "asterisk/translate.h"
40 #include "asterisk/dsp.h"
42 #ifdef TRACE_FRAMES
43 static int headers;
44 static AST_LIST_HEAD_STATIC(headerlist, ast_frame);
45 #endif
47 #if !defined(LOW_MEMORY)
48 static void frame_cache_cleanup(void *data);
50 /*! \brief A per-thread cache of frame headers */
51 AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
53 /*!
54 * \brief Maximum ast_frame cache size
56 * In most cases where the frame header cache will be useful, the size
57 * of the cache will stay very small. However, it is not always the case that
58 * the same thread that allocates the frame will be the one freeing them, so
59 * sometimes a thread will never have any frames in its cache, or the cache
60 * will never be pulled from. For the latter case, we limit the maximum size.
61 */
62 #define FRAME_CACHE_MAX_SIZE 10
64 /*! \brief This is just so ast_frames, a list head struct for holding a list of
65 * ast_frame structures, is defined. */
66 AST_LIST_HEAD_NOLOCK(ast_frames, ast_frame);
68 struct ast_frame_cache {
69 struct ast_frames list;
70 size_t size;
72 #endif
74 #define SMOOTHER_SIZE 8000
76 enum frame_type {
77 TYPE_HIGH, /* 0x0 */
78 TYPE_LOW, /* 0x1 */
79 TYPE_SILENCE, /* 0x2 */
80 TYPE_DONTSEND /* 0x3 */
83 #define TYPE_MASK 0x3
85 struct ast_smoother {
86 int size;
87 int format;
88 int readdata;
89 int flags;
90 float samplesperbyte;
91 struct ast_frame f;
92 struct timeval delivery;
93 char data[SMOOTHER_SIZE];
94 char framedata[SMOOTHER_SIZE + AST_FRIENDLY_OFFSET];
95 struct ast_frame *opt;
96 int len;
99 /*! \brief Definition of supported media formats (codecs) */
100 static struct ast_format_list AST_FORMAT_LIST[] = {
101 { AST_FORMAT_G723_1 , "g723", 8000, "G.723.1", 20, 30, 300, 30, 30 }, /*!< G723.1 */
102 { AST_FORMAT_GSM, "gsm", 8000, "GSM", 33, 20, 300, 20, 20 }, /*!< codec_gsm.c */
103 { AST_FORMAT_ULAW, "ulaw", 8000, "G.711 u-law", 80, 10, 150, 10, 20 }, /*!< codec_ulaw.c */
104 { AST_FORMAT_ALAW, "alaw", 8000, "G.711 A-law", 80, 10, 150, 10, 20 }, /*!< codec_alaw.c */
105 { AST_FORMAT_G726, "g726", 8000, "G.726 RFC3551", 40, 10, 300, 10, 20 }, /*!< codec_g726.c */
106 { AST_FORMAT_ADPCM, "adpcm" , 8000, "ADPCM", 40, 10, 300, 10, 20 }, /*!< codec_adpcm.c */
107 { AST_FORMAT_SLINEAR, "slin", 8000, "16 bit Signed Linear PCM", 160, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE }, /*!< Signed linear */
108 { AST_FORMAT_LPC10, "lpc10", 8000, "LPC10", 7, 20, 20, 20, 20 }, /*!< codec_lpc10.c */
109 { AST_FORMAT_G729A, "g729", 8000, "G.729A", 10, 10, 230, 10, 20, AST_SMOOTHER_FLAG_G729 }, /*!< Binary commercial distribution */
110 { AST_FORMAT_SPEEX, "speex", 8000, "SpeeX", 10, 10, 60, 10, 20 }, /*!< codec_speex.c */
111 { AST_FORMAT_ILBC, "ilbc", 8000, "iLBC", 50, 30, 30, 30, 30 }, /*!< codec_ilbc.c */ /* inc=30ms - workaround */
112 { AST_FORMAT_G726_AAL2, "g726aal2", 8000, "G.726 AAL2", 40, 10, 300, 10, 20 }, /*!< codec_g726.c */
113 { AST_FORMAT_G722, "g722", 16000, "G722", 80, 10, 150, 10, 20 }, /*!< codec_g722.c */
114 { AST_FORMAT_SLINEAR16, "slin16", 16000, "16 bit Signed Linear PCM (16kHz)", 320, 10, 70, 10, 20 }, /*!< Signed linear (16kHz) */
115 { AST_FORMAT_JPEG, "jpeg", 0, "JPEG image"}, /*!< See format_jpeg.c */
116 { AST_FORMAT_PNG, "png", 0, "PNG image"}, /*!< PNG Image format */
117 { AST_FORMAT_H261, "h261", 0, "H.261 Video" }, /*!< H.261 Video Passthrough */
118 { AST_FORMAT_H263, "h263", 0, "H.263 Video" }, /*!< H.263 Passthrough support, see format_h263.c */
119 { AST_FORMAT_H263_PLUS, "h263p", 0, "H.263+ Video" }, /*!< H.263plus passthrough support See format_h263.c */
120 { AST_FORMAT_H264, "h264", 0, "H.264 Video" }, /*!< Passthrough support, see format_h263.c */
121 { AST_FORMAT_MP4_VIDEO, "mpeg4", 0, "MPEG4 Video" }, /*!< Passthrough support for MPEG4 */
122 { AST_FORMAT_T140, "t140", 0, "Passthrough T.140 Realtime Text" }, /*!< Passthrough support for T.140 Realtime Text */
125 struct ast_frame ast_null_frame = { AST_FRAME_NULL, };
127 void ast_smoother_reset(struct ast_smoother *s, int size)
129 memset(s, 0, sizeof(*s));
130 s->size = size;
133 struct ast_smoother *ast_smoother_new(int size)
135 struct ast_smoother *s;
136 if (size < 1)
137 return NULL;
138 if ((s = ast_malloc(sizeof(*s))))
139 ast_smoother_reset(s, size);
140 return s;
143 int ast_smoother_get_flags(struct ast_smoother *s)
145 return s->flags;
148 void ast_smoother_set_flags(struct ast_smoother *s, int flags)
150 s->flags = flags;
153 int ast_smoother_test_flag(struct ast_smoother *s, int flag)
155 return (s->flags & flag);
158 int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
160 if (f->frametype != AST_FRAME_VOICE) {
161 ast_log(LOG_WARNING, "Huh? Can't smooth a non-voice frame!\n");
162 return -1;
164 if (!s->format) {
165 s->format = f->subclass;
166 s->samplesperbyte = (float)f->samples / (float)f->datalen;
167 } else if (s->format != f->subclass) {
168 ast_log(LOG_WARNING, "Smoother was working on %d format frames, now trying to feed %d?\n", s->format, f->subclass);
169 return -1;
171 if (s->len + f->datalen > SMOOTHER_SIZE) {
172 ast_log(LOG_WARNING, "Out of smoother space\n");
173 return -1;
175 if (((f->datalen == s->size) || ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729)))
176 && !s->opt && (f->offset >= AST_MIN_OFFSET)) {
177 if (!s->len) {
178 /* Optimize by sending the frame we just got
179 on the next read, thus eliminating the douple
180 copy */
181 if (swap)
182 ast_swapcopy_samples(f->data, f->data, f->samples);
183 s->opt = f;
184 return 0;
187 if (s->flags & AST_SMOOTHER_FLAG_G729) {
188 if (s->len % 10) {
189 ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
190 return 0;
193 if (swap)
194 ast_swapcopy_samples(s->data+s->len, f->data, f->samples);
195 else
196 memcpy(s->data + s->len, f->data, f->datalen);
197 /* If either side is empty, reset the delivery time */
198 if (!s->len || ast_tvzero(f->delivery) || ast_tvzero(s->delivery)) /* XXX really ? */
199 s->delivery = f->delivery;
200 s->len += f->datalen;
201 return 0;
204 struct ast_frame *ast_smoother_read(struct ast_smoother *s)
206 struct ast_frame *opt;
207 int len;
209 /* IF we have an optimization frame, send it */
210 if (s->opt) {
211 if (s->opt->offset < AST_FRIENDLY_OFFSET)
212 ast_log(LOG_WARNING, "Returning a frame of inappropriate offset (%d).\n",
213 s->opt->offset);
214 opt = s->opt;
215 s->opt = NULL;
216 return opt;
219 /* Make sure we have enough data */
220 if (s->len < s->size) {
221 /* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
222 if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->size % 10)))
223 return NULL;
225 len = s->size;
226 if (len > s->len)
227 len = s->len;
228 /* Make frame */
229 s->f.frametype = AST_FRAME_VOICE;
230 s->f.subclass = s->format;
231 s->f.data = s->framedata + AST_FRIENDLY_OFFSET;
232 s->f.offset = AST_FRIENDLY_OFFSET;
233 s->f.datalen = len;
234 /* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
235 s->f.samples = len * s->samplesperbyte; /* XXX rounding */
236 s->f.delivery = s->delivery;
237 /* Fill Data */
238 memcpy(s->f.data, s->data, len);
239 s->len -= len;
240 /* Move remaining data to the front if applicable */
241 if (s->len) {
242 /* In principle this should all be fine because if we are sending
243 G.729 VAD, the next timestamp will take over anyawy */
244 memmove(s->data, s->data + len, s->len);
245 if (!ast_tvzero(s->delivery)) {
246 /* If we have delivery time, increment it, otherwise, leave it at 0 */
247 s->delivery = ast_tvadd(s->delivery, ast_samp2tv(s->f.samples, 8000));
250 /* Return frame */
251 return &s->f;
254 void ast_smoother_free(struct ast_smoother *s)
256 ast_free(s);
259 static struct ast_frame *ast_frame_header_new(void)
261 struct ast_frame *f;
263 #if !defined(LOW_MEMORY)
264 struct ast_frame_cache *frames;
266 if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
267 if ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) {
268 size_t mallocd_len = f->mallocd_hdr_len;
269 memset(f, 0, sizeof(*f));
270 f->mallocd_hdr_len = mallocd_len;
271 f->mallocd = AST_MALLOCD_HDR;
272 frames->size--;
273 return f;
276 if (!(f = ast_calloc_cache(1, sizeof(*f))))
277 return NULL;
278 #else
279 if (!(f = ast_calloc(1, sizeof(*f))))
280 return NULL;
281 #endif
283 f->mallocd_hdr_len = sizeof(*f);
284 #ifdef TRACE_FRAMES
285 AST_LIST_LOCK(&headerlist);
286 headers++;
287 AST_LIST_INSERT_HEAD(&headerlist, f, frame_list);
288 AST_LIST_UNLOCK(&headerlist);
289 #endif
291 return f;
294 #if !defined(LOW_MEMORY)
295 static void frame_cache_cleanup(void *data)
297 struct ast_frame_cache *frames = data;
298 struct ast_frame *f;
300 while ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list)))
301 ast_free(f);
303 ast_free(frames);
305 #endif
307 void ast_frame_free(struct ast_frame *fr, int cache)
309 if (ast_test_flag(fr, AST_FRFLAG_FROM_TRANSLATOR))
310 ast_translate_frame_freed(fr);
311 else if (ast_test_flag(fr, AST_FRFLAG_FROM_DSP))
312 ast_dsp_frame_freed(fr);
314 if (!fr->mallocd)
315 return;
317 #if !defined(LOW_MEMORY)
318 if (cache && fr->mallocd == AST_MALLOCD_HDR) {
319 /* Cool, only the header is malloc'd, let's just cache those for now
320 * to keep things simple... */
321 struct ast_frame_cache *frames;
323 if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))
324 && frames->size < FRAME_CACHE_MAX_SIZE) {
325 AST_LIST_INSERT_HEAD(&frames->list, fr, frame_list);
326 frames->size++;
327 return;
330 #endif
332 if (fr->mallocd & AST_MALLOCD_DATA) {
333 if (fr->data)
334 ast_free(fr->data - fr->offset);
336 if (fr->mallocd & AST_MALLOCD_SRC) {
337 if (fr->src)
338 ast_free((char *)fr->src);
340 if (fr->mallocd & AST_MALLOCD_HDR) {
341 #ifdef TRACE_FRAMES
342 AST_LIST_LOCK(&headerlist);
343 headers--;
344 AST_LIST_REMOVE(&headerlist, fr, frame_list);
345 AST_LIST_UNLOCK(&headerlist);
346 #endif
347 ast_free(fr);
352 * \brief 'isolates' a frame by duplicating non-malloc'ed components
353 * (header, src, data).
354 * On return all components are malloc'ed
356 struct ast_frame *ast_frisolate(struct ast_frame *fr)
358 struct ast_frame *out;
359 void *newdata;
361 ast_clear_flag(fr, AST_FRFLAG_FROM_TRANSLATOR);
362 ast_clear_flag(fr, AST_FRFLAG_FROM_DSP);
364 if (!(fr->mallocd & AST_MALLOCD_HDR)) {
365 /* Allocate a new header if needed */
366 if (!(out = ast_frame_header_new()))
367 return NULL;
368 out->frametype = fr->frametype;
369 out->subclass = fr->subclass;
370 out->datalen = fr->datalen;
371 out->samples = fr->samples;
372 out->offset = fr->offset;
373 out->data = fr->data;
374 /* Copy the timing data */
375 ast_copy_flags(out, fr, AST_FRFLAG_HAS_TIMING_INFO);
376 if (ast_test_flag(fr, AST_FRFLAG_HAS_TIMING_INFO)) {
377 out->ts = fr->ts;
378 out->len = fr->len;
379 out->seqno = fr->seqno;
381 } else
382 out = fr;
384 if (!(fr->mallocd & AST_MALLOCD_SRC)) {
385 if (fr->src) {
386 if (!(out->src = ast_strdup(fr->src))) {
387 if (out != fr)
388 ast_free(out);
389 return NULL;
392 } else
393 out->src = fr->src;
395 if (!(fr->mallocd & AST_MALLOCD_DATA)) {
396 if (!(newdata = ast_malloc(fr->datalen + AST_FRIENDLY_OFFSET))) {
397 if (out->src != fr->src)
398 ast_free((void *) out->src);
399 if (out != fr)
400 ast_free(out);
401 return NULL;
403 newdata += AST_FRIENDLY_OFFSET;
404 out->offset = AST_FRIENDLY_OFFSET;
405 out->datalen = fr->datalen;
406 memcpy(newdata, fr->data, fr->datalen);
407 out->data = newdata;
410 out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
412 return out;
415 struct ast_frame *ast_frdup(const struct ast_frame *f)
417 struct ast_frame *out = NULL;
418 int len, srclen = 0;
419 void *buf = NULL;
421 #if !defined(LOW_MEMORY)
422 struct ast_frame_cache *frames;
423 #endif
425 /* Start with standard stuff */
426 len = sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
427 /* If we have a source, add space for it */
429 * XXX Watch out here - if we receive a src which is not terminated
430 * properly, we can be easily attacked. Should limit the size we deal with.
432 if (f->src)
433 srclen = strlen(f->src);
434 if (srclen > 0)
435 len += srclen + 1;
437 #if !defined(LOW_MEMORY)
438 if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
439 AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) {
440 if (out->mallocd_hdr_len >= len) {
441 size_t mallocd_len = out->mallocd_hdr_len;
443 AST_LIST_REMOVE_CURRENT(frame_list);
444 memset(out, 0, sizeof(*out));
445 out->mallocd_hdr_len = mallocd_len;
446 buf = out;
447 frames->size--;
448 break;
451 AST_LIST_TRAVERSE_SAFE_END;
453 #endif
455 if (!buf) {
456 if (!(buf = ast_calloc_cache(1, len)))
457 return NULL;
458 out = buf;
459 out->mallocd_hdr_len = len;
462 out->frametype = f->frametype;
463 out->subclass = f->subclass;
464 out->datalen = f->datalen;
465 out->samples = f->samples;
466 out->delivery = f->delivery;
467 /* Set us as having malloc'd header only, so it will eventually
468 get freed. */
469 out->mallocd = AST_MALLOCD_HDR;
470 out->offset = AST_FRIENDLY_OFFSET;
471 if (out->datalen) {
472 out->data = buf + sizeof(*out) + AST_FRIENDLY_OFFSET;
473 memcpy(out->data, f->data, out->datalen);
475 if (srclen > 0) {
476 out->src = buf + sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
477 /* Must have space since we allocated for it */
478 strcpy((char *)out->src, f->src);
480 ast_copy_flags(out, f, AST_FRFLAG_HAS_TIMING_INFO);
481 out->ts = f->ts;
482 out->len = f->len;
483 out->seqno = f->seqno;
484 return out;
487 void ast_swapcopy_samples(void *dst, const void *src, int samples)
489 int i;
490 unsigned short *dst_s = dst;
491 const unsigned short *src_s = src;
493 for (i = 0; i < samples; i++)
494 dst_s[i] = (src_s[i]<<8) | (src_s[i]>>8);
498 struct ast_format_list *ast_get_format_list_index(int index)
500 return &AST_FORMAT_LIST[index];
503 struct ast_format_list *ast_get_format_list(size_t *size)
505 *size = (sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]));
506 return AST_FORMAT_LIST;
509 char* ast_getformatname(int format)
511 int x;
512 char *ret = "unknown";
513 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
514 if (AST_FORMAT_LIST[x].bits == format) {
515 ret = AST_FORMAT_LIST[x].name;
516 break;
519 return ret;
522 char *ast_getformatname_multiple(char *buf, size_t size, int format)
524 int x;
525 unsigned len;
526 char *start, *end = buf;
528 if (!size)
529 return buf;
530 snprintf(end, size, "0x%x (", format);
531 len = strlen(end);
532 end += len;
533 size -= len;
534 start = end;
535 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
536 if (AST_FORMAT_LIST[x].bits & format) {
537 snprintf(end, size,"%s|",AST_FORMAT_LIST[x].name);
538 len = strlen(end);
539 end += len;
540 size -= len;
543 if (start == end)
544 ast_copy_string(start, "nothing)", size);
545 else if (size > 1)
546 *(end -1) = ')';
547 return buf;
550 static struct ast_codec_alias_table {
551 char *alias;
552 char *realname;
553 } ast_codec_alias_table[] = {
554 { "slinear", "slin"},
555 { "slinear16", "slin16"},
556 { "g723.1", "g723"},
559 static const char *ast_expand_codec_alias(const char *in)
561 int x;
563 for (x = 0; x < sizeof(ast_codec_alias_table) / sizeof(ast_codec_alias_table[0]); x++) {
564 if (!strcmp(in,ast_codec_alias_table[x].alias))
565 return ast_codec_alias_table[x].realname;
567 return in;
570 int ast_getformatbyname(const char *name)
572 int x, all, format = 0;
574 all = strcasecmp(name, "all") ? 0 : 1;
575 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
576 if (all ||
577 !strcasecmp(AST_FORMAT_LIST[x].name,name) ||
578 !strcasecmp(AST_FORMAT_LIST[x].name,ast_expand_codec_alias(name))) {
579 format |= AST_FORMAT_LIST[x].bits;
580 if (!all)
581 break;
585 return format;
588 char *ast_codec2str(int codec)
590 int x;
591 char *ret = "unknown";
592 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
593 if (AST_FORMAT_LIST[x].bits == codec) {
594 ret = AST_FORMAT_LIST[x].desc;
595 break;
598 return ret;
601 static char *show_codecs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
603 int i, found=0;
604 char hex[25];
606 switch (cmd) {
607 case CLI_INIT:
608 e->command = "core show codecs [audio|video|image]";
609 e->usage =
610 "Usage: core show codecs [audio|video|image]\n"
611 " Displays codec mapping\n";
612 return NULL;
613 case CLI_GENERATE:
614 return NULL;
617 if ((a->argc < 3) || (a->argc > 4))
618 return CLI_SHOWUSAGE;
620 if (!ast_opt_dont_warn)
621 ast_cli(a->fd, "Disclaimer: this command is for informational purposes only.\n"
622 "\tIt does not indicate anything about your configuration.\n");
624 ast_cli(a->fd, "%11s %9s %10s TYPE %8s %s\n","INT","BINARY","HEX","NAME","DESC");
625 ast_cli(a->fd, "--------------------------------------------------------------------------------\n");
626 if ((a->argc == 3) || (!strcasecmp(a->argv[3],"audio"))) {
627 found = 1;
628 for (i=0;i<13;i++) {
629 snprintf(hex,25,"(0x%x)",1<<i);
630 ast_cli(a->fd, "%11u (1 << %2d) %10s audio %8s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
634 if ((a->argc == 3) || (!strcasecmp(a->argv[3],"image"))) {
635 found = 1;
636 for (i=16;i<18;i++) {
637 snprintf(hex,25,"(0x%x)",1<<i);
638 ast_cli(a->fd, "%11u (1 << %2d) %10s image %8s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
642 if ((a->argc == 3) || (!strcasecmp(a->argv[3],"video"))) {
643 found = 1;
644 for (i=18;i<22;i++) {
645 snprintf(hex,25,"(0x%x)",1<<i);
646 ast_cli(a->fd, "%11u (1 << %2d) %10s video %8s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
650 if (!found)
651 return CLI_SHOWUSAGE;
652 else
653 return CLI_SUCCESS;
656 static char *show_codec_n(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
658 int codec, i, found=0;
660 switch (cmd) {
661 case CLI_INIT:
662 e->command = "core show codec";
663 e->usage =
664 "Usage: core show codec <number>\n"
665 " Displays codec mapping\n";
666 return NULL;
667 case CLI_GENERATE:
668 return NULL;
671 if (a->argc != 4)
672 return CLI_SHOWUSAGE;
674 if (sscanf(a->argv[3],"%d",&codec) != 1)
675 return CLI_SHOWUSAGE;
677 for (i = 0; i < 32; i++)
678 if (codec & (1 << i)) {
679 found = 1;
680 ast_cli(a->fd, "%11u (1 << %2d) %s\n",1 << i,i,ast_codec2str(1<<i));
683 if (!found)
684 ast_cli(a->fd, "Codec %d not found\n", codec);
686 return CLI_SUCCESS;
689 /*! Dump a frame for debugging purposes */
690 void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix)
692 const char noname[] = "unknown";
693 char ftype[40] = "Unknown Frametype";
694 char cft[80];
695 char subclass[40] = "Unknown Subclass";
696 char csub[80];
697 char moreinfo[40] = "";
698 char cn[60];
699 char cp[40];
700 char cmn[40];
701 const char *message = "Unknown";
703 if (!name)
704 name = noname;
707 if (!f) {
708 ast_verbose("%s [ %s (NULL) ] [%s]\n",
709 term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
710 term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
711 term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
712 return;
714 /* XXX We should probably print one each of voice and video when the format changes XXX */
715 if (f->frametype == AST_FRAME_VOICE)
716 return;
717 if (f->frametype == AST_FRAME_VIDEO)
718 return;
719 switch(f->frametype) {
720 case AST_FRAME_DTMF_BEGIN:
721 strcpy(ftype, "DTMF Begin");
722 subclass[0] = f->subclass;
723 subclass[1] = '\0';
724 break;
725 case AST_FRAME_DTMF_END:
726 strcpy(ftype, "DTMF End");
727 subclass[0] = f->subclass;
728 subclass[1] = '\0';
729 break;
730 case AST_FRAME_CONTROL:
731 strcpy(ftype, "Control");
732 switch(f->subclass) {
733 case AST_CONTROL_HANGUP:
734 strcpy(subclass, "Hangup");
735 break;
736 case AST_CONTROL_RING:
737 strcpy(subclass, "Ring");
738 break;
739 case AST_CONTROL_RINGING:
740 strcpy(subclass, "Ringing");
741 break;
742 case AST_CONTROL_ANSWER:
743 strcpy(subclass, "Answer");
744 break;
745 case AST_CONTROL_BUSY:
746 strcpy(subclass, "Busy");
747 break;
748 case AST_CONTROL_TAKEOFFHOOK:
749 strcpy(subclass, "Take Off Hook");
750 break;
751 case AST_CONTROL_OFFHOOK:
752 strcpy(subclass, "Line Off Hook");
753 break;
754 case AST_CONTROL_CONGESTION:
755 strcpy(subclass, "Congestion");
756 break;
757 case AST_CONTROL_FLASH:
758 strcpy(subclass, "Flash");
759 break;
760 case AST_CONTROL_WINK:
761 strcpy(subclass, "Wink");
762 break;
763 case AST_CONTROL_OPTION:
764 strcpy(subclass, "Option");
765 break;
766 case AST_CONTROL_RADIO_KEY:
767 strcpy(subclass, "Key Radio");
768 break;
769 case AST_CONTROL_RADIO_UNKEY:
770 strcpy(subclass, "Unkey Radio");
771 break;
772 case AST_CONTROL_HOLD:
773 strcpy(subclass, "Hold");
774 break;
775 case AST_CONTROL_UNHOLD:
776 strcpy(subclass, "Unhold");
777 break;
778 case AST_CONTROL_T38:
779 if (f->datalen != sizeof(enum ast_control_t38)) {
780 message = "Invalid";
781 } else {
782 enum ast_control_t38 state = *((enum ast_control_t38 *) f->data);
783 if (state == AST_T38_REQUEST_NEGOTIATE)
784 message = "Negotiation Requested";
785 else if (state == AST_T38_REQUEST_TERMINATE)
786 message = "Negotiation Request Terminated";
787 else if (state == AST_T38_NEGOTIATED)
788 message = "Negotiated";
789 else if (state == AST_T38_TERMINATED)
790 message = "Terminated";
791 else if (state == AST_T38_REFUSED)
792 message = "Refused";
794 snprintf(subclass, sizeof(subclass), "T38/%s", message);
795 break;
796 case -1:
797 strcpy(subclass, "Stop generators");
798 break;
799 default:
800 snprintf(subclass, sizeof(subclass), "Unknown control '%d'", f->subclass);
802 break;
803 case AST_FRAME_NULL:
804 strcpy(ftype, "Null Frame");
805 strcpy(subclass, "N/A");
806 break;
807 case AST_FRAME_IAX:
808 /* Should never happen */
809 strcpy(ftype, "IAX Specific");
810 snprintf(subclass, sizeof(subclass), "IAX Frametype %d", f->subclass);
811 break;
812 case AST_FRAME_TEXT:
813 strcpy(ftype, "Text");
814 strcpy(subclass, "N/A");
815 ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
816 break;
817 case AST_FRAME_IMAGE:
818 strcpy(ftype, "Image");
819 snprintf(subclass, sizeof(subclass), "Image format %s\n", ast_getformatname(f->subclass));
820 break;
821 case AST_FRAME_HTML:
822 strcpy(ftype, "HTML");
823 switch(f->subclass) {
824 case AST_HTML_URL:
825 strcpy(subclass, "URL");
826 ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
827 break;
828 case AST_HTML_DATA:
829 strcpy(subclass, "Data");
830 break;
831 case AST_HTML_BEGIN:
832 strcpy(subclass, "Begin");
833 break;
834 case AST_HTML_END:
835 strcpy(subclass, "End");
836 break;
837 case AST_HTML_LDCOMPLETE:
838 strcpy(subclass, "Load Complete");
839 break;
840 case AST_HTML_NOSUPPORT:
841 strcpy(subclass, "No Support");
842 break;
843 case AST_HTML_LINKURL:
844 strcpy(subclass, "Link URL");
845 ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
846 break;
847 case AST_HTML_UNLINK:
848 strcpy(subclass, "Unlink");
849 break;
850 case AST_HTML_LINKREJECT:
851 strcpy(subclass, "Link Reject");
852 break;
853 default:
854 snprintf(subclass, sizeof(subclass), "Unknown HTML frame '%d'\n", f->subclass);
855 break;
857 break;
858 case AST_FRAME_MODEM:
859 strcpy(ftype, "Modem");
860 switch (f->subclass) {
861 case AST_MODEM_T38:
862 strcpy(subclass, "T.38");
863 break;
864 case AST_MODEM_V150:
865 strcpy(subclass, "V.150");
866 break;
867 default:
868 snprintf(subclass, sizeof(subclass), "Unknown MODEM frame '%d'\n", f->subclass);
869 break;
871 break;
872 default:
873 snprintf(ftype, sizeof(ftype), "Unknown Frametype '%d'", f->frametype);
875 if (!ast_strlen_zero(moreinfo))
876 ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) '%s' ] [%s]\n",
877 term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
878 term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
879 f->frametype,
880 term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
881 f->subclass,
882 term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
883 term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
884 else
885 ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) ] [%s]\n",
886 term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
887 term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
888 f->frametype,
889 term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
890 f->subclass,
891 term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
895 #ifdef TRACE_FRAMES
896 static char *show_frame_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
898 struct ast_frame *f;
899 int x=1;
901 switch (cmd) {
902 case CLI_INIT:
903 e->command = "core show frame stats";
904 e->usage =
905 "Usage: core show frame stats\n"
906 " Displays debugging statistics from framer\n";
907 return NULL;
908 case CLI_GENERATE:
909 return NULL;
912 if (a->argc != 4)
913 return CLI_SHOWUSAGE;
914 AST_LIST_LOCK(&headerlist);
915 ast_cli(a->fd, " Framer Statistics \n");
916 ast_cli(a->fd, "---------------------------\n");
917 ast_cli(a->fd, "Total allocated headers: %d\n", headers);
918 ast_cli(a->fd, "Queue Dump:\n");
919 AST_LIST_TRAVERSE(&headerlist, f, frame_list)
920 ast_cli(a->fd, "%d. Type %d, subclass %d from %s\n", x++, f->frametype, f->subclass, f->src ? f->src : "<Unknown>");
921 AST_LIST_UNLOCK(&headerlist);
922 return CLI_SUCCESS;
924 #endif
926 /* Builtin Asterisk CLI-commands for debugging */
927 static struct ast_cli_entry my_clis[] = {
928 AST_CLI_DEFINE(show_codecs, "Displays a list of codecs"),
929 AST_CLI_DEFINE(show_codec_n, "Shows a specific codec"),
930 #ifdef TRACE_FRAMES
931 AST_CLI_DEFINE(show_frame_stats, "Shows frame statistics"),
932 #endif
935 int init_framer(void)
937 ast_cli_register_multiple(my_clis, sizeof(my_clis) / sizeof(struct ast_cli_entry));
938 return 0;
941 void ast_codec_pref_convert(struct ast_codec_pref *pref, char *buf, size_t size, int right)
943 int x, differential = (int) 'A', mem;
944 char *from, *to;
946 if (right) {
947 from = pref->order;
948 to = buf;
949 mem = size;
950 } else {
951 to = pref->order;
952 from = buf;
953 mem = 32;
956 memset(to, 0, mem);
957 for (x = 0; x < 32 ; x++) {
958 if (!from[x])
959 break;
960 to[x] = right ? (from[x] + differential) : (from[x] - differential);
964 int ast_codec_pref_string(struct ast_codec_pref *pref, char *buf, size_t size)
966 int x, codec;
967 size_t total_len, slen;
968 char *formatname;
970 memset(buf,0,size);
971 total_len = size;
972 buf[0] = '(';
973 total_len--;
974 for(x = 0; x < 32 ; x++) {
975 if (total_len <= 0)
976 break;
977 if (!(codec = ast_codec_pref_index(pref,x)))
978 break;
979 if ((formatname = ast_getformatname(codec))) {
980 slen = strlen(formatname);
981 if (slen > total_len)
982 break;
983 strncat(buf, formatname, total_len - 1); /* safe */
984 total_len -= slen;
986 if (total_len && x < 31 && ast_codec_pref_index(pref , x + 1)) {
987 strncat(buf, "|", total_len - 1); /* safe */
988 total_len--;
991 if (total_len) {
992 strncat(buf, ")", total_len - 1); /* safe */
993 total_len--;
996 return size - total_len;
999 int ast_codec_pref_index(struct ast_codec_pref *pref, int index)
1001 int slot = 0;
1004 if ((index >= 0) && (index < sizeof(pref->order))) {
1005 slot = pref->order[index];
1008 return slot ? AST_FORMAT_LIST[slot-1].bits : 0;
1011 /*! \brief Remove codec from pref list */
1012 void ast_codec_pref_remove(struct ast_codec_pref *pref, int format)
1014 struct ast_codec_pref oldorder;
1015 int x, y = 0;
1016 int slot;
1017 int size;
1019 if (!pref->order[0])
1020 return;
1022 memcpy(&oldorder, pref, sizeof(oldorder));
1023 memset(pref, 0, sizeof(*pref));
1025 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1026 slot = oldorder.order[x];
1027 size = oldorder.framing[x];
1028 if (! slot)
1029 break;
1030 if (AST_FORMAT_LIST[slot-1].bits != format) {
1031 pref->order[y] = slot;
1032 pref->framing[y++] = size;
1038 /*! \brief Append codec to list */
1039 int ast_codec_pref_append(struct ast_codec_pref *pref, int format)
1041 int x, newindex = 0;
1043 ast_codec_pref_remove(pref, format);
1045 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1046 if (AST_FORMAT_LIST[x].bits == format) {
1047 newindex = x + 1;
1048 break;
1052 if (newindex) {
1053 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1054 if (!pref->order[x]) {
1055 pref->order[x] = newindex;
1056 break;
1061 return x;
1064 /*! \brief Prepend codec to list */
1065 void ast_codec_pref_prepend(struct ast_codec_pref *pref, int format, int only_if_existing)
1067 int x, newindex = 0;
1069 /* First step is to get the codecs "index number" */
1070 for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
1071 if (AST_FORMAT_LIST[x].bits == format) {
1072 newindex = x + 1;
1073 break;
1076 /* Done if its unknown */
1077 if (!newindex)
1078 return;
1080 /* Now find any existing occurrence, or the end */
1081 for (x = 0; x < 32; x++) {
1082 if (!pref->order[x] || pref->order[x] == newindex)
1083 break;
1086 if (only_if_existing && !pref->order[x])
1087 return;
1089 /* Move down to make space to insert - either all the way to the end,
1090 or as far as the existing location (which will be overwritten) */
1091 for (; x > 0; x--) {
1092 pref->order[x] = pref->order[x - 1];
1093 pref->framing[x] = pref->framing[x - 1];
1096 /* And insert the new entry */
1097 pref->order[0] = newindex;
1098 pref->framing[0] = 0; /* ? */
1101 /*! \brief Set packet size for codec */
1102 int ast_codec_pref_setsize(struct ast_codec_pref *pref, int format, int framems)
1104 int x, index = -1;
1106 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1107 if (AST_FORMAT_LIST[x].bits == format) {
1108 index = x;
1109 break;
1113 if (index < 0)
1114 return -1;
1116 /* size validation */
1117 if (!framems)
1118 framems = AST_FORMAT_LIST[index].def_ms;
1120 if (AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
1121 framems -= framems % AST_FORMAT_LIST[index].inc_ms;
1123 if (framems < AST_FORMAT_LIST[index].min_ms)
1124 framems = AST_FORMAT_LIST[index].min_ms;
1126 if (framems > AST_FORMAT_LIST[index].max_ms)
1127 framems = AST_FORMAT_LIST[index].max_ms;
1130 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1131 if (pref->order[x] == (index + 1)) {
1132 pref->framing[x] = framems;
1133 break;
1137 return x;
1140 /*! \brief Get packet size for codec */
1141 struct ast_format_list ast_codec_pref_getsize(struct ast_codec_pref *pref, int format)
1143 int x, index = -1, framems = 0;
1144 struct ast_format_list fmt = { 0, };
1146 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1147 if (AST_FORMAT_LIST[x].bits == format) {
1148 fmt = AST_FORMAT_LIST[x];
1149 index = x;
1150 break;
1154 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1155 if (pref->order[x] == (index + 1)) {
1156 framems = pref->framing[x];
1157 break;
1161 /* size validation */
1162 if (!framems)
1163 framems = AST_FORMAT_LIST[index].def_ms;
1165 if (AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
1166 framems -= framems % AST_FORMAT_LIST[index].inc_ms;
1168 if (framems < AST_FORMAT_LIST[index].min_ms)
1169 framems = AST_FORMAT_LIST[index].min_ms;
1171 if (framems > AST_FORMAT_LIST[index].max_ms)
1172 framems = AST_FORMAT_LIST[index].max_ms;
1174 fmt.cur_ms = framems;
1176 return fmt;
1179 /*! \brief Pick a codec */
1180 int ast_codec_choose(struct ast_codec_pref *pref, int formats, int find_best)
1182 int x, ret = 0, slot;
1184 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1185 slot = pref->order[x];
1187 if (!slot)
1188 break;
1189 if (formats & AST_FORMAT_LIST[slot-1].bits) {
1190 ret = AST_FORMAT_LIST[slot-1].bits;
1191 break;
1194 if (ret & AST_FORMAT_AUDIO_MASK)
1195 return ret;
1197 ast_debug(4, "Could not find preferred codec - %s\n", find_best ? "Going for the best codec" : "Returning zero codec");
1199 return find_best ? ast_best_codec(formats) : 0;
1202 int ast_parse_allow_disallow(struct ast_codec_pref *pref, int *mask, const char *list, int allowing)
1204 int errors = 0;
1205 char *parse = NULL, *this = NULL, *psize = NULL;
1206 int format = 0, framems = 0;
1208 parse = ast_strdupa(list);
1209 while ((this = strsep(&parse, ","))) {
1210 framems = 0;
1211 if ((psize = strrchr(this, ':'))) {
1212 *psize++ = '\0';
1213 ast_debug(1, "Packetization for codec: %s is %s\n", this, psize);
1214 framems = atoi(psize);
1215 if (framems < 0) {
1216 framems = 0;
1217 errors++;
1218 ast_log(LOG_WARNING, "Bad packetization value for codec %s\n", this);
1221 if (!(format = ast_getformatbyname(this))) {
1222 ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", allowing ? "allow" : "disallow", this);
1223 errors++;
1224 continue;
1227 if (mask) {
1228 if (allowing)
1229 *mask |= format;
1230 else
1231 *mask &= ~format;
1234 /* Set up a preference list for audio. Do not include video in preferences
1235 since we can not transcode video and have to use whatever is offered
1237 if (pref && (format & AST_FORMAT_AUDIO_MASK)) {
1238 if (strcasecmp(this, "all")) {
1239 if (allowing) {
1240 ast_codec_pref_append(pref, format);
1241 ast_codec_pref_setsize(pref, format, framems);
1243 else
1244 ast_codec_pref_remove(pref, format);
1245 } else if (!allowing) {
1246 memset(pref, 0, sizeof(*pref));
1250 return errors;
1253 static int g723_len(unsigned char buf)
1255 enum frame_type type = buf & TYPE_MASK;
1257 switch(type) {
1258 case TYPE_DONTSEND:
1259 return 0;
1260 break;
1261 case TYPE_SILENCE:
1262 return 4;
1263 break;
1264 case TYPE_HIGH:
1265 return 24;
1266 break;
1267 case TYPE_LOW:
1268 return 20;
1269 break;
1270 default:
1271 ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", type);
1273 return -1;
1276 static int g723_samples(unsigned char *buf, int maxlen)
1278 int pos = 0;
1279 int samples = 0;
1280 int res;
1281 while(pos < maxlen) {
1282 res = g723_len(buf[pos]);
1283 if (res <= 0)
1284 break;
1285 samples += 240;
1286 pos += res;
1288 return samples;
1291 static unsigned char get_n_bits_at(unsigned char *data, int n, int bit)
1293 int byte = bit / 8; /* byte containing first bit */
1294 int rem = 8 - (bit % 8); /* remaining bits in first byte */
1295 unsigned char ret = 0;
1297 if (n <= 0 || n > 8)
1298 return 0;
1300 if (rem < n) {
1301 ret = (data[byte] << (n - rem));
1302 ret |= (data[byte + 1] >> (8 - n + rem));
1303 } else {
1304 ret = (data[byte] >> (rem - n));
1307 return (ret & (0xff >> (8 - n)));
1310 static int speex_get_wb_sz_at(unsigned char *data, int len, int bit)
1312 static int SpeexWBSubModeSz[] = {
1313 0, 36, 112, 192,
1314 352, 0, 0, 0 };
1315 int off = bit;
1316 unsigned char c;
1318 /* skip up to two wideband frames */
1319 if (((len * 8 - off) >= 5) &&
1320 get_n_bits_at(data, 1, off)) {
1321 c = get_n_bits_at(data, 3, off + 1);
1322 off += SpeexWBSubModeSz[c];
1324 if (((len * 8 - off) >= 5) &&
1325 get_n_bits_at(data, 1, off)) {
1326 c = get_n_bits_at(data, 3, off + 1);
1327 off += SpeexWBSubModeSz[c];
1329 if (((len * 8 - off) >= 5) &&
1330 get_n_bits_at(data, 1, off)) {
1331 ast_log(LOG_WARNING, "Encountered corrupt speex frame; too many wideband frames in a row.\n");
1332 return -1;
1337 return off - bit;
1340 static int speex_samples(unsigned char *data, int len)
1342 static int SpeexSubModeSz[] = {
1343 5, 43, 119, 160,
1344 220, 300, 364, 492,
1345 79, 0, 0, 0,
1346 0, 0, 0, 0 };
1347 static int SpeexInBandSz[] = {
1348 1, 1, 4, 4,
1349 4, 4, 4, 4,
1350 8, 8, 16, 16,
1351 32, 32, 64, 64 };
1352 int bit = 0;
1353 int cnt = 0;
1354 int off;
1355 unsigned char c;
1357 while ((len * 8 - bit) >= 5) {
1358 /* skip wideband frames */
1359 off = speex_get_wb_sz_at(data, len, bit);
1360 if (off < 0) {
1361 ast_log(LOG_WARNING, "Had error while reading wideband frames for speex samples\n");
1362 break;
1364 bit += off;
1366 if ((len * 8 - bit) < 5) {
1367 ast_log(LOG_WARNING, "Not enough bits remaining after wide band for speex samples.\n");
1368 break;
1371 /* get control bits */
1372 c = get_n_bits_at(data, 5, bit);
1373 bit += 5;
1375 if (c == 15) {
1376 /* terminator */
1377 break;
1378 } else if (c == 14) {
1379 /* in-band signal; next 4 bits contain signal id */
1380 c = get_n_bits_at(data, 4, bit);
1381 bit += 4;
1382 bit += SpeexInBandSz[c];
1383 } else if (c == 13) {
1384 /* user in-band; next 5 bits contain msg len */
1385 c = get_n_bits_at(data, 5, bit);
1386 bit += 5;
1387 bit += c * 8;
1388 } else if (c > 8) {
1389 /* unknown */
1390 break;
1391 } else {
1392 /* skip number bits for submode (less the 5 control bits) */
1393 bit += SpeexSubModeSz[c] - 5;
1394 cnt += 160; /* new frame */
1397 return cnt;
1400 int ast_codec_get_samples(struct ast_frame *f)
1402 int samples=0;
1403 switch(f->subclass) {
1404 case AST_FORMAT_SPEEX:
1405 samples = speex_samples(f->data, f->datalen);
1406 break;
1407 case AST_FORMAT_G723_1:
1408 samples = g723_samples(f->data, f->datalen);
1409 break;
1410 case AST_FORMAT_ILBC:
1411 samples = 240 * (f->datalen / 50);
1412 break;
1413 case AST_FORMAT_GSM:
1414 samples = 160 * (f->datalen / 33);
1415 break;
1416 case AST_FORMAT_G729A:
1417 samples = f->datalen * 8;
1418 break;
1419 case AST_FORMAT_SLINEAR:
1420 case AST_FORMAT_SLINEAR16:
1421 samples = f->datalen / 2;
1422 break;
1423 case AST_FORMAT_LPC10:
1424 /* assumes that the RTP packet contains one LPC10 frame */
1425 samples = 22 * 8;
1426 samples += (((char *)(f->data))[7] & 0x1) * 8;
1427 break;
1428 case AST_FORMAT_ULAW:
1429 case AST_FORMAT_ALAW:
1430 samples = f->datalen;
1431 break;
1432 case AST_FORMAT_G722:
1433 case AST_FORMAT_ADPCM:
1434 case AST_FORMAT_G726:
1435 case AST_FORMAT_G726_AAL2:
1436 samples = f->datalen * 2;
1437 break;
1438 default:
1439 ast_log(LOG_WARNING, "Unable to calculate samples for format %s\n", ast_getformatname(f->subclass));
1441 return samples;
1444 int ast_codec_get_len(int format, int samples)
1446 int len = 0;
1448 /* XXX Still need speex, g723, and lpc10 XXX */
1449 switch(format) {
1450 case AST_FORMAT_G723_1:
1451 len = (samples / 240) * 20;
1452 break;
1453 case AST_FORMAT_ILBC:
1454 len = (samples / 240) * 50;
1455 break;
1456 case AST_FORMAT_GSM:
1457 len = (samples / 160) * 33;
1458 break;
1459 case AST_FORMAT_G729A:
1460 len = samples / 8;
1461 break;
1462 case AST_FORMAT_SLINEAR:
1463 case AST_FORMAT_SLINEAR16:
1464 len = samples * 2;
1465 break;
1466 case AST_FORMAT_ULAW:
1467 case AST_FORMAT_ALAW:
1468 len = samples;
1469 break;
1470 case AST_FORMAT_G722:
1471 case AST_FORMAT_ADPCM:
1472 case AST_FORMAT_G726:
1473 case AST_FORMAT_G726_AAL2:
1474 len = samples / 2;
1475 break;
1476 default:
1477 ast_log(LOG_WARNING, "Unable to calculate sample length for format %s\n", ast_getformatname(format));
1480 return len;
1483 int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
1485 int count;
1486 short *fdata = f->data;
1487 short adjust_value = abs(adjustment);
1489 if ((f->frametype != AST_FRAME_VOICE) || (f->subclass != AST_FORMAT_SLINEAR))
1490 return -1;
1492 if (!adjustment)
1493 return 0;
1495 for (count = 0; count < f->samples; count++) {
1496 if (adjustment > 0) {
1497 ast_slinear_saturated_multiply(&fdata[count], &adjust_value);
1498 } else if (adjustment < 0) {
1499 ast_slinear_saturated_divide(&fdata[count], &adjust_value);
1503 return 0;
1506 int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2)
1508 int count;
1509 short *data1, *data2;
1511 if ((f1->frametype != AST_FRAME_VOICE) || (f1->subclass != AST_FORMAT_SLINEAR))
1512 return -1;
1514 if ((f2->frametype != AST_FRAME_VOICE) || (f2->subclass != AST_FORMAT_SLINEAR))
1515 return -1;
1517 if (f1->samples != f2->samples)
1518 return -1;
1520 for (count = 0, data1 = f1->data, data2 = f2->data;
1521 count < f1->samples;
1522 count++, data1++, data2++)
1523 ast_slinear_saturated_add(data1, data2);
1525 return 0;