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.
21 * \brief Frame and codec manipulation routines
23 * \author Mark Spencer <markster@digium.com>
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"
41 #include "asterisk/file.h"
43 #if !defined(LOW_MEMORY)
44 static void frame_cache_cleanup(void *data);
46 /*! \brief A per-thread cache of frame headers */
47 AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
50 * \brief Maximum ast_frame cache size
52 * In most cases where the frame header cache will be useful, the size
53 * of the cache will stay very small. However, it is not always the case that
54 * the same thread that allocates the frame will be the one freeing them, so
55 * sometimes a thread will never have any frames in its cache, or the cache
56 * will never be pulled from. For the latter case, we limit the maximum size.
58 #define FRAME_CACHE_MAX_SIZE 10
60 /*! \brief This is just so ast_frames, a list head struct for holding a list of
61 * ast_frame structures, is defined. */
62 AST_LIST_HEAD_NOLOCK(ast_frames, ast_frame);
64 struct ast_frame_cache {
65 struct ast_frames list;
70 #define SMOOTHER_SIZE 8000
75 TYPE_SILENCE, /* 0x2 */
76 TYPE_DONTSEND /* 0x3 */
83 struct ast_format format;
86 unsigned int opt_needs_swap:1;
88 struct timeval delivery;
89 char data[SMOOTHER_SIZE];
90 char framedata[SMOOTHER_SIZE + AST_FRIENDLY_OFFSET];
91 struct ast_frame *opt;
95 struct ast_frame ast_null_frame = { AST_FRAME_NULL, };
97 static int smoother_frame_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
99 if (s->flags & AST_SMOOTHER_FLAG_G729) {
101 ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
106 ast_swapcopy_samples(s->data + s->len, f->data.ptr, f->samples);
108 memcpy(s->data + s->len, f->data.ptr, f->datalen);
110 /* If either side is empty, reset the delivery time */
111 if (!s->len || ast_tvzero(f->delivery) || ast_tvzero(s->delivery)) { /* XXX really ? */
112 s->delivery = f->delivery;
114 s->len += f->datalen;
119 void ast_smoother_reset(struct ast_smoother *s, int bytes)
121 memset(s, 0, sizeof(*s));
125 void ast_smoother_reconfigure(struct ast_smoother *s, int bytes)
127 /* if there is no change, then nothing to do */
128 if (s->size == bytes) {
131 /* set the new desired output size */
133 /* if there is no 'optimized' frame in the smoother,
134 * then there is nothing left to do
139 /* there is an 'optimized' frame here at the old size,
140 * but it must now be put into the buffer so the data
141 * can be extracted at the new size
143 smoother_frame_feed(s, s->opt, s->opt_needs_swap);
147 struct ast_smoother *ast_smoother_new(int size)
149 struct ast_smoother *s;
152 if ((s = ast_malloc(sizeof(*s))))
153 ast_smoother_reset(s, size);
157 int ast_smoother_get_flags(struct ast_smoother *s)
162 void ast_smoother_set_flags(struct ast_smoother *s, int flags)
167 int ast_smoother_test_flag(struct ast_smoother *s, int flag)
169 return (s->flags & flag);
172 int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
174 if (f->frametype != AST_FRAME_VOICE) {
175 ast_log(LOG_WARNING, "Huh? Can't smooth a non-voice frame!\n");
179 ast_format_copy(&s->format, &f->subclass.format);
180 s->samplesperbyte = (float)f->samples / (float)f->datalen;
181 } else if (ast_format_cmp(&s->format, &f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
182 ast_log(LOG_WARNING, "Smoother was working on %s format frames, now trying to feed %s?\n",
183 ast_getformatname(&s->format), ast_getformatname(&f->subclass.format));
186 if (s->len + f->datalen > SMOOTHER_SIZE) {
187 ast_log(LOG_WARNING, "Out of smoother space\n");
190 if (((f->datalen == s->size) ||
191 ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729))) &&
194 (f->offset >= AST_MIN_OFFSET)) {
195 /* Optimize by sending the frame we just got
196 on the next read, thus eliminating the douple
199 ast_swapcopy_samples(f->data.ptr, f->data.ptr, f->samples);
201 s->opt_needs_swap = swap ? 1 : 0;
205 return smoother_frame_feed(s, f, swap);
208 struct ast_frame *ast_smoother_read(struct ast_smoother *s)
210 struct ast_frame *opt;
213 /* IF we have an optimization frame, send it */
215 if (s->opt->offset < AST_FRIENDLY_OFFSET)
216 ast_log(LOG_WARNING, "Returning a frame of inappropriate offset (%d).\n",
223 /* Make sure we have enough data */
224 if (s->len < s->size) {
225 /* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
226 if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->len % 10)))
233 s->f.frametype = AST_FRAME_VOICE;
234 ast_format_copy(&s->f.subclass.format, &s->format);
235 s->f.data.ptr = s->framedata + AST_FRIENDLY_OFFSET;
236 s->f.offset = AST_FRIENDLY_OFFSET;
238 /* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
239 s->f.samples = len * s->samplesperbyte; /* XXX rounding */
240 s->f.delivery = s->delivery;
242 memcpy(s->f.data.ptr, s->data, len);
244 /* Move remaining data to the front if applicable */
246 /* In principle this should all be fine because if we are sending
247 G.729 VAD, the next timestamp will take over anyawy */
248 memmove(s->data, s->data + len, s->len);
249 if (!ast_tvzero(s->delivery)) {
250 /* If we have delivery time, increment it, otherwise, leave it at 0 */
251 s->delivery = ast_tvadd(s->delivery, ast_samp2tv(s->f.samples, ast_format_rate(&s->format)));
258 void ast_smoother_free(struct ast_smoother *s)
263 static struct ast_frame *ast_frame_header_new(void)
267 #if !defined(LOW_MEMORY)
268 struct ast_frame_cache *frames;
270 if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
271 if ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) {
272 size_t mallocd_len = f->mallocd_hdr_len;
273 memset(f, 0, sizeof(*f));
274 f->mallocd_hdr_len = mallocd_len;
275 f->mallocd = AST_MALLOCD_HDR;
280 if (!(f = ast_calloc_cache(1, sizeof(*f))))
283 if (!(f = ast_calloc(1, sizeof(*f))))
287 f->mallocd_hdr_len = sizeof(*f);
292 #if !defined(LOW_MEMORY)
293 static void frame_cache_cleanup(void *data)
295 struct ast_frame_cache *frames = data;
298 while ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list)))
305 static void __frame_free(struct ast_frame *fr, int cache)
310 #if !defined(LOW_MEMORY)
311 if (cache && fr->mallocd == AST_MALLOCD_HDR) {
312 /* Cool, only the header is malloc'd, let's just cache those for now
313 * to keep things simple... */
314 struct ast_frame_cache *frames;
316 if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames))) &&
317 (frames->size < FRAME_CACHE_MAX_SIZE)) {
318 AST_LIST_INSERT_HEAD(&frames->list, fr, frame_list);
325 if (fr->mallocd & AST_MALLOCD_DATA) {
327 ast_free(fr->data.ptr - fr->offset);
329 if (fr->mallocd & AST_MALLOCD_SRC) {
331 ast_free((void *) fr->src);
333 if (fr->mallocd & AST_MALLOCD_HDR) {
339 void ast_frame_free(struct ast_frame *frame, int cache)
341 struct ast_frame *next;
343 for (next = AST_LIST_NEXT(frame, frame_list);
345 frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) {
346 __frame_free(frame, cache);
351 * \brief 'isolates' a frame by duplicating non-malloc'ed components
352 * (header, src, data).
353 * On return all components are malloc'ed
355 struct ast_frame *ast_frisolate(struct ast_frame *fr)
357 struct ast_frame *out;
360 /* if none of the existing frame is malloc'd, let ast_frdup() do it
361 since it is more efficient
363 if (fr->mallocd == 0) {
364 return ast_frdup(fr);
367 /* if everything is already malloc'd, we are done */
368 if ((fr->mallocd & (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) ==
369 (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) {
373 if (!(fr->mallocd & AST_MALLOCD_HDR)) {
374 /* Allocate a new header if needed */
375 if (!(out = ast_frame_header_new())) {
378 out->frametype = fr->frametype;
379 ast_format_copy(&out->subclass.format, &fr->subclass.format);
380 out->datalen = fr->datalen;
381 out->samples = fr->samples;
382 out->offset = fr->offset;
383 /* Copy the timing data */
384 ast_copy_flags(out, fr, AST_FLAGS_ALL);
385 if (ast_test_flag(fr, AST_FRFLAG_HAS_TIMING_INFO)) {
388 out->seqno = fr->seqno;
394 if (!(fr->mallocd & AST_MALLOCD_SRC) && fr->src) {
395 if (!(out->src = ast_strdup(fr->src))) {
404 fr->mallocd &= ~AST_MALLOCD_SRC;
407 if (!(fr->mallocd & AST_MALLOCD_DATA)) {
409 out->data.uint32 = fr->data.uint32;
410 out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC;
413 if (!(newdata = ast_malloc(fr->datalen + AST_FRIENDLY_OFFSET))) {
414 if (out->src != fr->src) {
415 ast_free((void *) out->src);
422 newdata += AST_FRIENDLY_OFFSET;
423 out->offset = AST_FRIENDLY_OFFSET;
424 out->datalen = fr->datalen;
425 memcpy(newdata, fr->data.ptr, fr->datalen);
426 out->data.ptr = newdata;
428 out->data = fr->data;
429 memset(&fr->data, 0, sizeof(fr->data));
430 fr->mallocd &= ~AST_MALLOCD_DATA;
433 out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
438 struct ast_frame *ast_frdup(const struct ast_frame *f)
440 struct ast_frame *out = NULL;
444 #if !defined(LOW_MEMORY)
445 struct ast_frame_cache *frames;
448 /* Start with standard stuff */
449 len = sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
450 /* If we have a source, add space for it */
452 * XXX Watch out here - if we receive a src which is not terminated
453 * properly, we can be easily attacked. Should limit the size we deal with.
456 srclen = strlen(f->src);
460 #if !defined(LOW_MEMORY)
461 if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
462 AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) {
463 if (out->mallocd_hdr_len >= len) {
464 size_t mallocd_len = out->mallocd_hdr_len;
466 AST_LIST_REMOVE_CURRENT(frame_list);
467 memset(out, 0, sizeof(*out));
468 out->mallocd_hdr_len = mallocd_len;
474 AST_LIST_TRAVERSE_SAFE_END;
479 if (!(buf = ast_calloc_cache(1, len)))
482 out->mallocd_hdr_len = len;
485 out->frametype = f->frametype;
486 ast_format_copy(&out->subclass.format, &f->subclass.format);
487 out->datalen = f->datalen;
488 out->samples = f->samples;
489 out->delivery = f->delivery;
490 /* Set us as having malloc'd header only, so it will eventually
492 out->mallocd = AST_MALLOCD_HDR;
493 out->offset = AST_FRIENDLY_OFFSET;
495 out->data.ptr = buf + sizeof(*out) + AST_FRIENDLY_OFFSET;
496 memcpy(out->data.ptr, f->data.ptr, out->datalen);
498 out->data.uint32 = f->data.uint32;
501 /* This may seem a little strange, but it's to avoid a gcc (4.2.4) compiler warning */
503 out->src = buf + sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
504 src = (char *) out->src;
505 /* Must have space since we allocated for it */
508 ast_copy_flags(out, f, AST_FLAGS_ALL);
511 out->seqno = f->seqno;
515 void ast_swapcopy_samples(void *dst, const void *src, int samples)
518 unsigned short *dst_s = dst;
519 const unsigned short *src_s = src;
521 for (i = 0; i < samples; i++)
522 dst_s[i] = (src_s[i]<<8) | (src_s[i]>>8);
525 /*! Dump a frame for debugging purposes */
526 void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix)
528 const char noname[] = "unknown";
529 char ftype[40] = "Unknown Frametype";
531 char subclass[40] = "Unknown Subclass";
533 char moreinfo[40] = "";
537 const char *message = "Unknown";
544 ast_verbose("%s [ %s (NULL) ] [%s]\n",
545 term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
546 term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
547 term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
550 /* XXX We should probably print one each of voice and video when the format changes XXX */
551 if (f->frametype == AST_FRAME_VOICE)
553 if (f->frametype == AST_FRAME_VIDEO)
555 switch(f->frametype) {
556 case AST_FRAME_DTMF_BEGIN:
557 strcpy(ftype, "DTMF Begin");
558 subclass[0] = f->subclass.integer;
561 case AST_FRAME_DTMF_END:
562 strcpy(ftype, "DTMF End");
563 subclass[0] = f->subclass.integer;
566 case AST_FRAME_CONTROL:
567 strcpy(ftype, "Control");
568 switch (f->subclass.integer) {
569 case AST_CONTROL_HANGUP:
570 strcpy(subclass, "Hangup");
572 case AST_CONTROL_RING:
573 strcpy(subclass, "Ring");
575 case AST_CONTROL_RINGING:
576 strcpy(subclass, "Ringing");
578 case AST_CONTROL_ANSWER:
579 strcpy(subclass, "Answer");
581 case AST_CONTROL_BUSY:
582 strcpy(subclass, "Busy");
584 case AST_CONTROL_TAKEOFFHOOK:
585 strcpy(subclass, "Take Off Hook");
587 case AST_CONTROL_OFFHOOK:
588 strcpy(subclass, "Line Off Hook");
590 case AST_CONTROL_CONGESTION:
591 strcpy(subclass, "Congestion");
593 case AST_CONTROL_FLASH:
594 strcpy(subclass, "Flash");
596 case AST_CONTROL_WINK:
597 strcpy(subclass, "Wink");
599 case AST_CONTROL_OPTION:
600 strcpy(subclass, "Option");
602 case AST_CONTROL_RADIO_KEY:
603 strcpy(subclass, "Key Radio");
605 case AST_CONTROL_RADIO_UNKEY:
606 strcpy(subclass, "Unkey Radio");
608 case AST_CONTROL_HOLD:
609 strcpy(subclass, "Hold");
611 case AST_CONTROL_UNHOLD:
612 strcpy(subclass, "Unhold");
614 case AST_CONTROL_T38_PARAMETERS:
615 if (f->datalen != sizeof(struct ast_control_t38_parameters)) {
618 struct ast_control_t38_parameters *parameters = f->data.ptr;
619 enum ast_control_t38 state = parameters->request_response;
620 if (state == AST_T38_REQUEST_NEGOTIATE)
621 message = "Negotiation Requested";
622 else if (state == AST_T38_REQUEST_TERMINATE)
623 message = "Negotiation Request Terminated";
624 else if (state == AST_T38_NEGOTIATED)
625 message = "Negotiated";
626 else if (state == AST_T38_TERMINATED)
627 message = "Terminated";
628 else if (state == AST_T38_REFUSED)
631 snprintf(subclass, sizeof(subclass), "T38_Parameters/%s", message);
634 strcpy(subclass, "Stop generators");
637 snprintf(subclass, sizeof(subclass), "Unknown control '%d'", f->subclass.integer);
641 strcpy(ftype, "Null Frame");
642 strcpy(subclass, "N/A");
645 /* Should never happen */
646 strcpy(ftype, "IAX Specific");
647 snprintf(subclass, sizeof(subclass), "IAX Frametype %d", f->subclass.integer);
650 strcpy(ftype, "Text");
651 strcpy(subclass, "N/A");
652 ast_copy_string(moreinfo, f->data.ptr, sizeof(moreinfo));
654 case AST_FRAME_IMAGE:
655 strcpy(ftype, "Image");
656 snprintf(subclass, sizeof(subclass), "Image format %s\n", ast_getformatname(&f->subclass.format));
659 strcpy(ftype, "HTML");
660 switch (f->subclass.integer) {
662 strcpy(subclass, "URL");
663 ast_copy_string(moreinfo, f->data.ptr, sizeof(moreinfo));
666 strcpy(subclass, "Data");
669 strcpy(subclass, "Begin");
672 strcpy(subclass, "End");
674 case AST_HTML_LDCOMPLETE:
675 strcpy(subclass, "Load Complete");
677 case AST_HTML_NOSUPPORT:
678 strcpy(subclass, "No Support");
680 case AST_HTML_LINKURL:
681 strcpy(subclass, "Link URL");
682 ast_copy_string(moreinfo, f->data.ptr, sizeof(moreinfo));
684 case AST_HTML_UNLINK:
685 strcpy(subclass, "Unlink");
687 case AST_HTML_LINKREJECT:
688 strcpy(subclass, "Link Reject");
691 snprintf(subclass, sizeof(subclass), "Unknown HTML frame '%d'\n", f->subclass.integer);
695 case AST_FRAME_MODEM:
696 strcpy(ftype, "Modem");
697 switch (f->subclass.integer) {
699 strcpy(subclass, "T.38");
702 strcpy(subclass, "V.150");
705 snprintf(subclass, sizeof(subclass), "Unknown MODEM frame '%d'\n", f->subclass.integer);
710 snprintf(ftype, sizeof(ftype), "Unknown Frametype '%d'", f->frametype);
712 if (!ast_strlen_zero(moreinfo))
713 ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) '%s' ] [%s]\n",
714 term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
715 term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
717 term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
719 term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
720 term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
722 ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) ] [%s]\n",
723 term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
724 term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
726 term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
728 term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
731 int ast_parse_allow_disallow(struct ast_codec_pref *pref, struct ast_format_cap *cap, const char *list, int allowing)
733 int errors = 0, framems = 0, all = 0;
734 char *parse = NULL, *this = NULL, *psize = NULL;
735 struct ast_format format;
737 parse = ast_strdupa(list);
738 while ((this = strsep(&parse, ","))) {
740 if ((psize = strrchr(this, ':'))) {
742 ast_debug(1, "Packetization for codec: %s is %s\n", this, psize);
743 framems = atoi(psize);
747 ast_log(LOG_WARNING, "Bad packetization value for codec %s\n", this);
750 all = strcasecmp(this, "all") ? 0 : 1;
752 if (!all && !ast_getformatbyname(this, &format)) {
753 ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", allowing ? "allow" : "disallow", this);
761 ast_format_cap_add_all(cap);
763 ast_format_cap_add(cap, &format);
767 ast_format_cap_remove_all(cap);
769 ast_format_cap_remove(cap, &format);
777 ast_codec_pref_append(pref, &format);
778 ast_codec_pref_setsize(pref, &format, framems);
780 ast_codec_pref_remove(pref, &format);
782 } else if (!allowing) {
783 memset(pref, 0, sizeof(*pref));
790 static int g723_len(unsigned char buf)
792 enum frame_type type = buf & TYPE_MASK;
808 ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", type);
813 static int g723_samples(unsigned char *buf, int maxlen)
818 while(pos < maxlen) {
819 res = g723_len(buf[pos]);
828 static unsigned char get_n_bits_at(unsigned char *data, int n, int bit)
830 int byte = bit / 8; /* byte containing first bit */
831 int rem = 8 - (bit % 8); /* remaining bits in first byte */
832 unsigned char ret = 0;
838 ret = (data[byte] << (n - rem));
839 ret |= (data[byte + 1] >> (8 - n + rem));
841 ret = (data[byte] >> (rem - n));
844 return (ret & (0xff >> (8 - n)));
847 static int speex_get_wb_sz_at(unsigned char *data, int len, int bit)
849 static const int SpeexWBSubModeSz[] = {
855 /* skip up to two wideband frames */
856 if (((len * 8 - off) >= 5) &&
857 get_n_bits_at(data, 1, off)) {
858 c = get_n_bits_at(data, 3, off + 1);
859 off += SpeexWBSubModeSz[c];
861 if (((len * 8 - off) >= 5) &&
862 get_n_bits_at(data, 1, off)) {
863 c = get_n_bits_at(data, 3, off + 1);
864 off += SpeexWBSubModeSz[c];
866 if (((len * 8 - off) >= 5) &&
867 get_n_bits_at(data, 1, off)) {
868 ast_log(LOG_WARNING, "Encountered corrupt speex frame; too many wideband frames in a row.\n");
877 static int speex_samples(unsigned char *data, int len)
879 static const int SpeexSubModeSz[] = {
884 static const int SpeexInBandSz[] = {
894 while ((len * 8 - bit) >= 5) {
895 /* skip wideband frames */
896 off = speex_get_wb_sz_at(data, len, bit);
898 ast_log(LOG_WARNING, "Had error while reading wideband frames for speex samples\n");
903 if ((len * 8 - bit) < 5)
906 /* get control bits */
907 c = get_n_bits_at(data, 5, bit);
913 } else if (c == 14) {
914 /* in-band signal; next 4 bits contain signal id */
915 c = get_n_bits_at(data, 4, bit);
917 bit += SpeexInBandSz[c];
918 } else if (c == 13) {
919 /* user in-band; next 4 bits contain msg len */
920 c = get_n_bits_at(data, 4, bit);
922 /* after which it's 5-bit signal id + c bytes of data */
926 ast_log(LOG_WARNING, "Unknown speex control frame %d\n", c);
929 /* skip number bits for submode (less the 5 control bits) */
930 bit += SpeexSubModeSz[c] - 5;
931 cnt += 160; /* new frame */
937 int ast_codec_get_samples(struct ast_frame *f)
941 switch (f->subclass.format.id) {
942 case AST_FORMAT_SPEEX:
943 samples = speex_samples(f->data.ptr, f->datalen);
945 case AST_FORMAT_SPEEX16:
946 samples = 2 * speex_samples(f->data.ptr, f->datalen);
948 case AST_FORMAT_SPEEX32:
949 samples = 4 * speex_samples(f->data.ptr, f->datalen);
951 case AST_FORMAT_G723_1:
952 samples = g723_samples(f->data.ptr, f->datalen);
954 case AST_FORMAT_ILBC:
955 samples = 240 * (f->datalen / 50);
958 samples = 160 * (f->datalen / 33);
960 case AST_FORMAT_G729A:
961 samples = f->datalen * 8;
963 case AST_FORMAT_SLINEAR:
964 case AST_FORMAT_SLINEAR16:
965 samples = f->datalen / 2;
967 case AST_FORMAT_LPC10:
968 /* assumes that the RTP packet contains one LPC10 frame */
970 samples += (((char *)(f->data.ptr))[7] & 0x1) * 8;
972 case AST_FORMAT_ULAW:
973 case AST_FORMAT_ALAW:
974 case AST_FORMAT_TESTLAW:
975 samples = f->datalen;
977 case AST_FORMAT_G722:
978 case AST_FORMAT_ADPCM:
979 case AST_FORMAT_G726:
980 case AST_FORMAT_G726_AAL2:
981 samples = f->datalen * 2;
983 case AST_FORMAT_SIREN7:
984 /* 16,000 samples per second at 32kbps is 4,000 bytes per second */
985 samples = f->datalen * (16000 / 4000);
987 case AST_FORMAT_SIREN14:
988 /* 32,000 samples per second at 48kbps is 6,000 bytes per second */
989 samples = (int) f->datalen * ((float) 32000 / 6000);
991 case AST_FORMAT_G719:
992 /* 48,000 samples per second at 64kbps is 8,000 bytes per second */
993 samples = (int) f->datalen * ((float) 48000 / 8000);
995 case AST_FORMAT_SILK:
996 if (!(ast_format_isset(&f->subclass.format,
997 SILK_ATTR_KEY_SAMP_RATE,
998 SILK_ATTR_VAL_SAMP_24KHZ,
999 AST_FORMAT_ATTR_END))) {
1001 } else if (!(ast_format_isset(&f->subclass.format,
1002 SILK_ATTR_KEY_SAMP_RATE,
1003 SILK_ATTR_VAL_SAMP_16KHZ,
1004 AST_FORMAT_ATTR_END))) {
1006 } else if (!(ast_format_isset(&f->subclass.format,
1007 SILK_ATTR_KEY_SAMP_RATE,
1008 SILK_ATTR_VAL_SAMP_12KHZ,
1009 AST_FORMAT_ATTR_END))) {
1015 ast_log(LOG_WARNING, "Unable to calculate samples for format %s\n", ast_getformatname(&f->subclass.format));
1020 int ast_codec_get_len(struct ast_format *format, int samples)
1024 /* XXX Still need speex, and lpc10 XXX */
1025 switch(format->id) {
1026 case AST_FORMAT_G723_1:
1027 len = (samples / 240) * 20;
1029 case AST_FORMAT_ILBC:
1030 len = (samples / 240) * 50;
1032 case AST_FORMAT_GSM:
1033 len = (samples / 160) * 33;
1035 case AST_FORMAT_G729A:
1038 case AST_FORMAT_SLINEAR:
1039 case AST_FORMAT_SLINEAR16:
1042 case AST_FORMAT_ULAW:
1043 case AST_FORMAT_ALAW:
1044 case AST_FORMAT_TESTLAW:
1047 case AST_FORMAT_G722:
1048 case AST_FORMAT_ADPCM:
1049 case AST_FORMAT_G726:
1050 case AST_FORMAT_G726_AAL2:
1053 case AST_FORMAT_SIREN7:
1054 /* 16,000 samples per second at 32kbps is 4,000 bytes per second */
1055 len = samples / (16000 / 4000);
1057 case AST_FORMAT_SIREN14:
1058 /* 32,000 samples per second at 48kbps is 6,000 bytes per second */
1059 len = (int) samples / ((float) 32000 / 6000);
1061 case AST_FORMAT_G719:
1062 /* 48,000 samples per second at 64kbps is 8,000 bytes per second */
1063 len = (int) samples / ((float) 48000 / 8000);
1066 ast_log(LOG_WARNING, "Unable to calculate sample length for format %s\n", ast_getformatname(format));
1072 int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
1075 short *fdata = f->data.ptr;
1076 short adjust_value = abs(adjustment);
1078 if ((f->frametype != AST_FRAME_VOICE) || !(ast_format_is_slinear(&f->subclass.format))) {
1086 for (count = 0; count < f->samples; count++) {
1087 if (adjustment > 0) {
1088 ast_slinear_saturated_multiply(&fdata[count], &adjust_value);
1089 } else if (adjustment < 0) {
1090 ast_slinear_saturated_divide(&fdata[count], &adjust_value);
1097 int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2)
1100 short *data1, *data2;
1102 if ((f1->frametype != AST_FRAME_VOICE) || (f1->subclass.format.id != AST_FORMAT_SLINEAR))
1105 if ((f2->frametype != AST_FRAME_VOICE) || (f2->subclass.format.id != AST_FORMAT_SLINEAR))
1108 if (f1->samples != f2->samples)
1111 for (count = 0, data1 = f1->data.ptr, data2 = f2->data.ptr;
1112 count < f1->samples;
1113 count++, data1++, data2++)
1114 ast_slinear_saturated_add(data1, data2);
1119 int ast_frame_clear(struct ast_frame *frame)
1121 struct ast_frame *next;
1123 for (next = AST_LIST_NEXT(frame, frame_list);
1125 frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) {
1126 memset(frame->data.ptr, 0, frame->datalen);