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$")
36 #include "asterisk/lock.h"
37 #include "asterisk/frame.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/options.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/cli.h"
42 #include "asterisk/term.h"
43 #include "asterisk/utils.h"
44 #include "asterisk/threadstorage.h"
45 #include "asterisk/linkedlists.h"
46 #include "asterisk/translate.h"
47 #include "asterisk/dsp.h"
51 static AST_LIST_HEAD_STATIC(headerlist
, ast_frame
);
54 #if !defined(LOW_MEMORY)
55 static void frame_cache_cleanup(void *data
);
57 /*! \brief A per-thread cache of frame headers */
58 AST_THREADSTORAGE_CUSTOM(frame_cache
, frame_cache_init
, frame_cache_cleanup
);
61 * \brief Maximum ast_frame cache size
63 * In most cases where the frame header cache will be useful, the size
64 * of the cache will stay very small. However, it is not always the case that
65 * the same thread that allocates the frame will be the one freeing them, so
66 * sometimes a thread will never have any frames in its cache, or the cache
67 * will never be pulled from. For the latter case, we limit the maximum size.
69 #define FRAME_CACHE_MAX_SIZE 10
71 /*! \brief This is just so ast_frames, a list head struct for holding a list of
72 * ast_frame structures, is defined. */
73 AST_LIST_HEAD_NOLOCK(ast_frames
, ast_frame
);
75 struct ast_frame_cache
{
76 struct ast_frames list
;
81 #define SMOOTHER_SIZE 8000
86 TYPE_SILENCE
, /* 0x2 */
87 TYPE_DONTSEND
/* 0x3 */
96 int optimizablestream
;
100 struct timeval delivery
;
101 char data
[SMOOTHER_SIZE
];
102 char framedata
[SMOOTHER_SIZE
+ AST_FRIENDLY_OFFSET
];
103 struct ast_frame
*opt
;
107 /*! \brief Definition of supported media formats (codecs) */
108 static struct ast_format_list AST_FORMAT_LIST
[] = { /*!< Bit number: comment - Bit numbers are hard coded in show_codec() */
109 { 1, AST_FORMAT_G723_1
, "g723" , "G.723.1", 24, 30, 300, 30, 30 }, /*!< 1 */
110 { 1, AST_FORMAT_GSM
, "gsm" , "GSM", 33, 20, 300, 20, 20 }, /*!< 2: codec_gsm.c */
111 { 1, AST_FORMAT_ULAW
, "ulaw", "G.711 u-law", 80, 10, 150, 10, 20 }, /*!< 3: codec_ulaw.c */
112 { 1, AST_FORMAT_ALAW
, "alaw", "G.711 A-law", 80, 10, 150, 10, 20 }, /*!< 4: codec_alaw.c */
113 { 1, AST_FORMAT_G726
, "g726", "G.726 RFC3551", 40, 10, 300, 10, 20 }, /*!< 5: codec_g726.c */
114 { 1, AST_FORMAT_ADPCM
, "adpcm" , "ADPCM", 40, 10, 300, 10, 20 }, /*!< 6: codec_adpcm.c */
115 { 1, AST_FORMAT_SLINEAR
, "slin", "16 bit Signed Linear PCM", 160, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE
}, /*!< 7 */
116 { 1, AST_FORMAT_LPC10
, "lpc10", "LPC10", 7, 20, 20, 20, 20 }, /*!< 8: codec_lpc10.c */
117 { 1, AST_FORMAT_G729A
, "g729", "G.729A", 10, 10, 230, 10, 20, AST_SMOOTHER_FLAG_G729
}, /*!< 9: Binary commercial distribution */
118 { 1, AST_FORMAT_SPEEX
, "speex", "SpeeX", 10, 10, 60, 10, 20 }, /*!< 10: codec_speex.c */
119 { 1, AST_FORMAT_ILBC
, "ilbc", "iLBC", 50, 30, 30, 30, 30 }, /*!< 11: codec_ilbc.c */ /* inc=30ms - workaround */
120 { 1, AST_FORMAT_G726_AAL2
, "g726aal2", "G.726 AAL2", 40, 10, 300, 10, 20 }, /*!< 12: codec_g726.c */
121 { 1, AST_FORMAT_G722
, "g722", "G722"}, /*!< 13 */
122 { 0, 0, "nothing", "undefined" },
123 { 0, 0, "nothing", "undefined" },
124 { 0, 0, "nothing", "undefined" },
125 { 0, 0, "nothing", "undefined" },
126 { 0, AST_FORMAT_MAX_AUDIO
, "maxaudio", "Maximum audio format" },
127 { 1, AST_FORMAT_JPEG
, "jpeg", "JPEG image"}, /*!< 17: See format_jpeg.c */
128 { 1, AST_FORMAT_PNG
, "png", "PNG image"}, /*!< 18: Image format */
129 { 1, AST_FORMAT_H261
, "h261", "H.261 Video" }, /*!< 19: Video Passthrough */
130 { 1, AST_FORMAT_H263
, "h263", "H.263 Video" }, /*!< 20: Passthrough support, see format_h263.c */
131 { 1, AST_FORMAT_H263_PLUS
, "h263p", "H.263+ Video" }, /*!< 21: See format_h263.c */
132 { 1, AST_FORMAT_H264
, "h264", "H.264 Video" }, /*!< 22: Passthrough support, see format_h263.c */
133 { 0, 0, "nothing", "undefined" },
134 { 0, 0, "nothing", "undefined" },
135 { 0, 0, "nothing", "undefined" },
136 { 0, AST_FORMAT_MAX_VIDEO
, "maxvideo", "Maximum video format" },
139 struct ast_frame ast_null_frame
= { AST_FRAME_NULL
, };
141 void ast_smoother_reset(struct ast_smoother
*s
, int size
)
143 memset(s
, 0, sizeof(*s
));
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 s
->format
= f
->subclass
;
180 s
->samplesperbyte
= (float)f
->samples
/ (float)f
->datalen
;
181 } else if (s
->format
!= f
->subclass
) {
182 ast_log(LOG_WARNING
, "Smoother was working on %d format frames, now trying to feed %d?\n", s
->format
, f
->subclass
);
185 if (s
->len
+ f
->datalen
> SMOOTHER_SIZE
) {
186 ast_log(LOG_WARNING
, "Out of smoother space\n");
189 if (((f
->datalen
== s
->size
) || ((f
->datalen
< 10) && (s
->flags
& AST_SMOOTHER_FLAG_G729
)))
190 && !s
->opt
&& (f
->offset
>= AST_MIN_OFFSET
)) {
192 /* Optimize by sending the frame we just got
193 on the next read, thus eliminating the douple
196 ast_swapcopy_samples(f
->data
, f
->data
, f
->samples
);
201 if (s
->flags
& AST_SMOOTHER_FLAG_G729
) {
203 ast_log(LOG_NOTICE
, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
208 ast_swapcopy_samples(s
->data
+s
->len
, f
->data
, f
->samples
);
210 memcpy(s
->data
+ s
->len
, f
->data
, f
->datalen
);
211 /* If either side is empty, reset the delivery time */
212 if (!s
->len
|| ast_tvzero(f
->delivery
) || ast_tvzero(s
->delivery
)) /* XXX really ? */
213 s
->delivery
= f
->delivery
;
214 s
->len
+= f
->datalen
;
218 struct ast_frame
*ast_smoother_read(struct ast_smoother
*s
)
220 struct ast_frame
*opt
;
223 /* IF we have an optimization frame, send it */
225 if (s
->opt
->offset
< AST_FRIENDLY_OFFSET
)
226 ast_log(LOG_WARNING
, "Returning a frame of inappropriate offset (%d).\n",
233 /* Make sure we have enough data */
234 if (s
->len
< s
->size
) {
235 /* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
236 if (!((s
->flags
& AST_SMOOTHER_FLAG_G729
) && (s
->size
% 10)))
243 s
->f
.frametype
= AST_FRAME_VOICE
;
244 s
->f
.subclass
= s
->format
;
245 s
->f
.data
= s
->framedata
+ AST_FRIENDLY_OFFSET
;
246 s
->f
.offset
= AST_FRIENDLY_OFFSET
;
248 /* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
249 s
->f
.samples
= len
* s
->samplesperbyte
; /* XXX rounding */
250 s
->f
.delivery
= s
->delivery
;
252 memcpy(s
->f
.data
, s
->data
, len
);
254 /* Move remaining data to the front if applicable */
256 /* In principle this should all be fine because if we are sending
257 G.729 VAD, the next timestamp will take over anyawy */
258 memmove(s
->data
, s
->data
+ len
, s
->len
);
259 if (!ast_tvzero(s
->delivery
)) {
260 /* If we have delivery time, increment it, otherwise, leave it at 0 */
261 s
->delivery
= ast_tvadd(s
->delivery
, ast_samp2tv(s
->f
.samples
, 8000));
268 void ast_smoother_free(struct ast_smoother
*s
)
273 static struct ast_frame
*ast_frame_header_new(void)
277 #if !defined(LOW_MEMORY)
278 struct ast_frame_cache
*frames
;
280 if ((frames
= ast_threadstorage_get(&frame_cache
, sizeof(*frames
)))) {
281 if ((f
= AST_LIST_REMOVE_HEAD(&frames
->list
, frame_list
))) {
282 size_t mallocd_len
= f
->mallocd_hdr_len
;
283 memset(f
, 0, sizeof(*f
));
284 f
->mallocd_hdr_len
= mallocd_len
;
285 f
->mallocd
= AST_MALLOCD_HDR
;
290 if (!(f
= ast_calloc_cache(1, sizeof(*f
))))
293 if (!(f
= ast_calloc(1, sizeof(*f
))))
297 f
->mallocd_hdr_len
= sizeof(*f
);
299 AST_LIST_LOCK(&headerlist
);
301 AST_LIST_INSERT_HEAD(&headerlist
, f
, frame_list
);
302 AST_LIST_UNLOCK(&headerlist
);
308 #if !defined(LOW_MEMORY)
309 static void frame_cache_cleanup(void *data
)
311 struct ast_frame_cache
*frames
= data
;
314 while ((f
= AST_LIST_REMOVE_HEAD(&frames
->list
, frame_list
)))
321 void ast_frame_free(struct ast_frame
*fr
, int cache
)
323 if (ast_test_flag(fr
, AST_FRFLAG_FROM_TRANSLATOR
))
324 ast_translate_frame_freed(fr
);
325 else if (ast_test_flag(fr
, AST_FRFLAG_FROM_DSP
))
326 ast_dsp_frame_freed(fr
);
331 #if !defined(LOW_MEMORY)
332 if (cache
&& fr
->mallocd
== AST_MALLOCD_HDR
) {
333 /* Cool, only the header is malloc'd, let's just cache those for now
334 * to keep things simple... */
335 struct ast_frame_cache
*frames
;
337 if ((frames
= ast_threadstorage_get(&frame_cache
, sizeof(*frames
)))
338 && frames
->size
< FRAME_CACHE_MAX_SIZE
) {
339 AST_LIST_INSERT_HEAD(&frames
->list
, fr
, frame_list
);
346 if (fr
->mallocd
& AST_MALLOCD_DATA
) {
348 free(fr
->data
- fr
->offset
);
350 if (fr
->mallocd
& AST_MALLOCD_SRC
) {
352 free((char *)fr
->src
);
354 if (fr
->mallocd
& AST_MALLOCD_HDR
) {
356 AST_LIST_LOCK(&headerlist
);
358 AST_LIST_REMOVE(&headerlist
, fr
, frame_list
);
359 AST_LIST_UNLOCK(&headerlist
);
366 * \brief 'isolates' a frame by duplicating non-malloc'ed components
367 * (header, src, data).
368 * On return all components are malloc'ed
370 struct ast_frame
*ast_frisolate(struct ast_frame
*fr
)
372 struct ast_frame
*out
;
375 ast_clear_flag(fr
, AST_FRFLAG_FROM_TRANSLATOR
);
376 ast_clear_flag(fr
, AST_FRFLAG_FROM_DSP
);
378 if (!(fr
->mallocd
& AST_MALLOCD_HDR
)) {
379 /* Allocate a new header if needed */
380 if (!(out
= ast_frame_header_new()))
382 out
->frametype
= fr
->frametype
;
383 out
->subclass
= fr
->subclass
;
384 out
->datalen
= fr
->datalen
;
385 out
->samples
= fr
->samples
;
386 out
->offset
= fr
->offset
;
387 out
->data
= fr
->data
;
388 /* Copy the timing data */
389 ast_copy_flags(out
, fr
, AST_FRFLAG_HAS_TIMING_INFO
);
390 if (ast_test_flag(fr
, AST_FRFLAG_HAS_TIMING_INFO
)) {
393 out
->seqno
= fr
->seqno
;
398 if (!(fr
->mallocd
& AST_MALLOCD_SRC
)) {
400 if (!(out
->src
= ast_strdup(fr
->src
))) {
409 if (!(fr
->mallocd
& AST_MALLOCD_DATA
)) {
410 if (!(newdata
= ast_malloc(fr
->datalen
+ AST_FRIENDLY_OFFSET
))) {
411 if (out
->src
!= fr
->src
)
412 free((void *) out
->src
);
417 newdata
+= AST_FRIENDLY_OFFSET
;
418 out
->offset
= AST_FRIENDLY_OFFSET
;
419 out
->datalen
= fr
->datalen
;
420 memcpy(newdata
, fr
->data
, fr
->datalen
);
424 out
->mallocd
= AST_MALLOCD_HDR
| AST_MALLOCD_SRC
| AST_MALLOCD_DATA
;
429 struct ast_frame
*ast_frdup(const struct ast_frame
*f
)
431 struct ast_frame
*out
= NULL
;
435 #if !defined(LOW_MEMORY)
436 struct ast_frame_cache
*frames
;
439 /* Start with standard stuff */
440 len
= sizeof(*out
) + AST_FRIENDLY_OFFSET
+ f
->datalen
;
441 /* If we have a source, add space for it */
443 * XXX Watch out here - if we receive a src which is not terminated
444 * properly, we can be easily attacked. Should limit the size we deal with.
447 srclen
= strlen(f
->src
);
451 #if !defined(LOW_MEMORY)
452 if ((frames
= ast_threadstorage_get(&frame_cache
, sizeof(*frames
)))) {
453 AST_LIST_TRAVERSE_SAFE_BEGIN(&frames
->list
, out
, frame_list
) {
454 if (out
->mallocd_hdr_len
>= len
) {
455 size_t mallocd_len
= out
->mallocd_hdr_len
;
456 AST_LIST_REMOVE_CURRENT(&frames
->list
, frame_list
);
457 memset(out
, 0, sizeof(*out
));
458 out
->mallocd_hdr_len
= mallocd_len
;
464 AST_LIST_TRAVERSE_SAFE_END
469 if (!(buf
= ast_calloc_cache(1, len
)))
472 out
->mallocd_hdr_len
= len
;
475 out
->frametype
= f
->frametype
;
476 out
->subclass
= f
->subclass
;
477 out
->datalen
= f
->datalen
;
478 out
->samples
= f
->samples
;
479 out
->delivery
= f
->delivery
;
480 /* Set us as having malloc'd header only, so it will eventually
482 out
->mallocd
= AST_MALLOCD_HDR
;
483 out
->offset
= AST_FRIENDLY_OFFSET
;
485 out
->data
= buf
+ sizeof(*out
) + AST_FRIENDLY_OFFSET
;
486 memcpy(out
->data
, f
->data
, out
->datalen
);
489 out
->src
= buf
+ sizeof(*out
) + AST_FRIENDLY_OFFSET
+ f
->datalen
;
490 /* Must have space since we allocated for it */
491 strcpy((char *)out
->src
, f
->src
);
493 ast_copy_flags(out
, f
, AST_FRFLAG_HAS_TIMING_INFO
);
496 out
->seqno
= f
->seqno
;
500 void ast_swapcopy_samples(void *dst
, const void *src
, int samples
)
503 unsigned short *dst_s
= dst
;
504 const unsigned short *src_s
= src
;
506 for (i
= 0; i
< samples
; i
++)
507 dst_s
[i
] = (src_s
[i
]<<8) | (src_s
[i
]>>8);
511 struct ast_format_list
*ast_get_format_list_index(int index
)
513 return &AST_FORMAT_LIST
[index
];
516 struct ast_format_list
*ast_get_format_list(size_t *size
)
518 *size
= (sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]));
519 return AST_FORMAT_LIST
;
522 char* ast_getformatname(int format
)
525 char *ret
= "unknown";
526 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
527 if(AST_FORMAT_LIST
[x
].visible
&& AST_FORMAT_LIST
[x
].bits
== format
) {
528 ret
= AST_FORMAT_LIST
[x
].name
;
535 char *ast_getformatname_multiple(char *buf
, size_t size
, int format
)
539 char *start
, *end
= buf
;
543 snprintf(end
, size
, "0x%x (", format
);
548 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
549 if (AST_FORMAT_LIST
[x
].visible
&& (AST_FORMAT_LIST
[x
].bits
& format
)) {
550 snprintf(end
, size
,"%s|",AST_FORMAT_LIST
[x
].name
);
557 snprintf(start
, size
, "nothing)");
563 static struct ast_codec_alias_table
{
566 } ast_codec_alias_table
[] = {
567 { "slinear", "slin"},
571 static const char *ast_expand_codec_alias(const char *in
)
575 for (x
= 0; x
< sizeof(ast_codec_alias_table
) / sizeof(ast_codec_alias_table
[0]); x
++) {
576 if(!strcmp(in
,ast_codec_alias_table
[x
].alias
))
577 return ast_codec_alias_table
[x
].realname
;
582 int ast_getformatbyname(const char *name
)
584 int x
, all
, format
= 0;
586 all
= strcasecmp(name
, "all") ? 0 : 1;
587 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
588 if(AST_FORMAT_LIST
[x
].visible
&& (all
||
589 !strcasecmp(AST_FORMAT_LIST
[x
].name
,name
) ||
590 !strcasecmp(AST_FORMAT_LIST
[x
].name
,ast_expand_codec_alias(name
)))) {
591 format
|= AST_FORMAT_LIST
[x
].bits
;
600 char *ast_codec2str(int codec
)
603 char *ret
= "unknown";
604 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
605 if(AST_FORMAT_LIST
[x
].visible
&& AST_FORMAT_LIST
[x
].bits
== codec
) {
606 ret
= AST_FORMAT_LIST
[x
].desc
;
613 static int show_codecs_deprecated(int fd
, int argc
, char *argv
[])
618 if ((argc
< 2) || (argc
> 3))
619 return RESULT_SHOWUSAGE
;
621 if (!ast_opt_dont_warn
)
622 ast_cli(fd
, "Disclaimer: this command is for informational purposes only.\n"
623 "\tIt does not indicate anything about your configuration.\n");
625 ast_cli(fd
, "%11s %9s %10s TYPE %8s %s\n","INT","BINARY","HEX","NAME","DESC");
626 ast_cli(fd
, "--------------------------------------------------------------------------------\n");
627 if ((argc
== 2) || (!strcasecmp(argv
[1],"audio"))) {
630 snprintf(hex
,25,"(0x%x)",1<<i
);
631 ast_cli(fd
, "%11u (1 << %2d) %10s audio %8s (%s)\n",1 << i
,i
,hex
,ast_getformatname(1<<i
),ast_codec2str(1<<i
));
635 if ((argc
== 2) || (!strcasecmp(argv
[1],"image"))) {
637 for (i
=16;i
<18;i
++) {
638 snprintf(hex
,25,"(0x%x)",1<<i
);
639 ast_cli(fd
, "%11u (1 << %2d) %10s image %8s (%s)\n",1 << i
,i
,hex
,ast_getformatname(1<<i
),ast_codec2str(1<<i
));
643 if ((argc
== 2) || (!strcasecmp(argv
[1],"video"))) {
645 for (i
=18;i
<22;i
++) {
646 snprintf(hex
,25,"(0x%x)",1<<i
);
647 ast_cli(fd
, "%11u (1 << %2d) %10s video %8s (%s)\n",1 << i
,i
,hex
,ast_getformatname(1<<i
),ast_codec2str(1<<i
));
652 return RESULT_SHOWUSAGE
;
654 return RESULT_SUCCESS
;
657 static int show_codecs(int fd
, int argc
, char *argv
[])
662 if ((argc
< 3) || (argc
> 4))
663 return RESULT_SHOWUSAGE
;
665 if (!ast_opt_dont_warn
)
666 ast_cli(fd
, "Disclaimer: this command is for informational purposes only.\n"
667 "\tIt does not indicate anything about your configuration.\n");
669 ast_cli(fd
, "%11s %9s %10s TYPE %8s %s\n","INT","BINARY","HEX","NAME","DESC");
670 ast_cli(fd
, "--------------------------------------------------------------------------------\n");
671 if ((argc
== 3) || (!strcasecmp(argv
[3],"audio"))) {
674 snprintf(hex
,25,"(0x%x)",1<<i
);
675 ast_cli(fd
, "%11u (1 << %2d) %10s audio %8s (%s)\n",1 << i
,i
,hex
,ast_getformatname(1<<i
),ast_codec2str(1<<i
));
679 if ((argc
== 3) || (!strcasecmp(argv
[3],"image"))) {
681 for (i
=16;i
<18;i
++) {
682 snprintf(hex
,25,"(0x%x)",1<<i
);
683 ast_cli(fd
, "%11u (1 << %2d) %10s image %8s (%s)\n",1 << i
,i
,hex
,ast_getformatname(1<<i
),ast_codec2str(1<<i
));
687 if ((argc
== 3) || (!strcasecmp(argv
[3],"video"))) {
689 for (i
=18;i
<22;i
++) {
690 snprintf(hex
,25,"(0x%x)",1<<i
);
691 ast_cli(fd
, "%11u (1 << %2d) %10s video %8s (%s)\n",1 << i
,i
,hex
,ast_getformatname(1<<i
),ast_codec2str(1<<i
));
696 return RESULT_SHOWUSAGE
;
698 return RESULT_SUCCESS
;
701 static char frame_show_codecs_usage
[] =
702 "Usage: core show codecs [audio|video|image]\n"
703 " Displays codec mapping\n";
705 static int show_codec_n_deprecated(int fd
, int argc
, char *argv
[])
707 int codec
, i
, found
=0;
710 return RESULT_SHOWUSAGE
;
712 if (sscanf(argv
[2],"%d",&codec
) != 1)
713 return RESULT_SHOWUSAGE
;
715 for (i
= 0; i
< 32; i
++)
716 if (codec
& (1 << i
)) {
718 ast_cli(fd
, "%11u (1 << %2d) %s\n",1 << i
,i
,ast_codec2str(1<<i
));
722 ast_cli(fd
, "Codec %d not found\n", codec
);
724 return RESULT_SUCCESS
;
727 static int show_codec_n(int fd
, int argc
, char *argv
[])
729 int codec
, i
, found
=0;
732 return RESULT_SHOWUSAGE
;
734 if (sscanf(argv
[3],"%d",&codec
) != 1)
735 return RESULT_SHOWUSAGE
;
737 for (i
= 0; i
< 32; i
++)
738 if (codec
& (1 << i
)) {
740 ast_cli(fd
, "%11u (1 << %2d) %s\n",1 << i
,i
,ast_codec2str(1<<i
));
744 ast_cli(fd
, "Codec %d not found\n", codec
);
746 return RESULT_SUCCESS
;
749 static char frame_show_codec_n_usage
[] =
750 "Usage: core show codec <number>\n"
751 " Displays codec mapping\n";
753 /*! Dump a frame for debugging purposes */
754 void ast_frame_dump(const char *name
, struct ast_frame
*f
, char *prefix
)
756 const char noname
[] = "unknown";
757 char ftype
[40] = "Unknown Frametype";
759 char subclass
[40] = "Unknown Subclass";
761 char moreinfo
[40] = "";
771 ast_verbose("%s [ %s (NULL) ] [%s]\n",
772 term_color(cp
, prefix
, COLOR_BRMAGENTA
, COLOR_BLACK
, sizeof(cp
)),
773 term_color(cft
, "HANGUP", COLOR_BRRED
, COLOR_BLACK
, sizeof(cft
)),
774 term_color(cn
, name
, COLOR_YELLOW
, COLOR_BLACK
, sizeof(cn
)));
777 /* XXX We should probably print one each of voice and video when the format changes XXX */
778 if (f
->frametype
== AST_FRAME_VOICE
)
780 if (f
->frametype
== AST_FRAME_VIDEO
)
782 switch(f
->frametype
) {
783 case AST_FRAME_DTMF_BEGIN
:
784 strcpy(ftype
, "DTMF Begin");
785 subclass
[0] = f
->subclass
;
788 case AST_FRAME_DTMF_END
:
789 strcpy(ftype
, "DTMF End");
790 subclass
[0] = f
->subclass
;
793 case AST_FRAME_CONTROL
:
794 strcpy(ftype
, "Control");
795 switch(f
->subclass
) {
796 case AST_CONTROL_HANGUP
:
797 strcpy(subclass
, "Hangup");
799 case AST_CONTROL_RING
:
800 strcpy(subclass
, "Ring");
802 case AST_CONTROL_RINGING
:
803 strcpy(subclass
, "Ringing");
805 case AST_CONTROL_ANSWER
:
806 strcpy(subclass
, "Answer");
808 case AST_CONTROL_BUSY
:
809 strcpy(subclass
, "Busy");
811 case AST_CONTROL_TAKEOFFHOOK
:
812 strcpy(subclass
, "Take Off Hook");
814 case AST_CONTROL_OFFHOOK
:
815 strcpy(subclass
, "Line Off Hook");
817 case AST_CONTROL_CONGESTION
:
818 strcpy(subclass
, "Congestion");
820 case AST_CONTROL_FLASH
:
821 strcpy(subclass
, "Flash");
823 case AST_CONTROL_WINK
:
824 strcpy(subclass
, "Wink");
826 case AST_CONTROL_OPTION
:
827 strcpy(subclass
, "Option");
829 case AST_CONTROL_RADIO_KEY
:
830 strcpy(subclass
, "Key Radio");
832 case AST_CONTROL_RADIO_UNKEY
:
833 strcpy(subclass
, "Unkey Radio");
836 strcpy(subclass
, "Stop generators");
839 snprintf(subclass
, sizeof(subclass
), "Unknown control '%d'", f
->subclass
);
843 strcpy(ftype
, "Null Frame");
844 strcpy(subclass
, "N/A");
847 /* Should never happen */
848 strcpy(ftype
, "IAX Specific");
849 snprintf(subclass
, sizeof(subclass
), "IAX Frametype %d", f
->subclass
);
852 strcpy(ftype
, "Text");
853 strcpy(subclass
, "N/A");
854 ast_copy_string(moreinfo
, f
->data
, sizeof(moreinfo
));
856 case AST_FRAME_IMAGE
:
857 strcpy(ftype
, "Image");
858 snprintf(subclass
, sizeof(subclass
), "Image format %s\n", ast_getformatname(f
->subclass
));
861 strcpy(ftype
, "HTML");
862 switch(f
->subclass
) {
864 strcpy(subclass
, "URL");
865 ast_copy_string(moreinfo
, f
->data
, sizeof(moreinfo
));
868 strcpy(subclass
, "Data");
871 strcpy(subclass
, "Begin");
874 strcpy(subclass
, "End");
876 case AST_HTML_LDCOMPLETE
:
877 strcpy(subclass
, "Load Complete");
879 case AST_HTML_NOSUPPORT
:
880 strcpy(subclass
, "No Support");
882 case AST_HTML_LINKURL
:
883 strcpy(subclass
, "Link URL");
884 ast_copy_string(moreinfo
, f
->data
, sizeof(moreinfo
));
886 case AST_HTML_UNLINK
:
887 strcpy(subclass
, "Unlink");
889 case AST_HTML_LINKREJECT
:
890 strcpy(subclass
, "Link Reject");
893 snprintf(subclass
, sizeof(subclass
), "Unknown HTML frame '%d'\n", f
->subclass
);
897 case AST_FRAME_MODEM
:
898 strcpy(ftype
, "Modem");
899 switch (f
->subclass
) {
901 strcpy(subclass
, "T.38");
904 strcpy(subclass
, "V.150");
907 snprintf(subclass
, sizeof(subclass
), "Unknown MODEM frame '%d'\n", f
->subclass
);
912 snprintf(ftype
, sizeof(ftype
), "Unknown Frametype '%d'", f
->frametype
);
914 if (!ast_strlen_zero(moreinfo
))
915 ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) '%s' ] [%s]\n",
916 term_color(cp
, prefix
, COLOR_BRMAGENTA
, COLOR_BLACK
, sizeof(cp
)),
917 term_color(cft
, ftype
, COLOR_BRRED
, COLOR_BLACK
, sizeof(cft
)),
919 term_color(csub
, subclass
, COLOR_BRCYAN
, COLOR_BLACK
, sizeof(csub
)),
921 term_color(cmn
, moreinfo
, COLOR_BRGREEN
, COLOR_BLACK
, sizeof(cmn
)),
922 term_color(cn
, name
, COLOR_YELLOW
, COLOR_BLACK
, sizeof(cn
)));
924 ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) ] [%s]\n",
925 term_color(cp
, prefix
, COLOR_BRMAGENTA
, COLOR_BLACK
, sizeof(cp
)),
926 term_color(cft
, ftype
, COLOR_BRRED
, COLOR_BLACK
, sizeof(cft
)),
928 term_color(csub
, subclass
, COLOR_BRCYAN
, COLOR_BLACK
, sizeof(csub
)),
930 term_color(cn
, name
, COLOR_YELLOW
, COLOR_BLACK
, sizeof(cn
)));
935 static int show_frame_stats(int fd
, int argc
, char *argv
[])
940 return RESULT_SHOWUSAGE
;
941 AST_LIST_LOCK(&headerlist
);
942 ast_cli(fd
, " Framer Statistics \n");
943 ast_cli(fd
, "---------------------------\n");
944 ast_cli(fd
, "Total allocated headers: %d\n", headers
);
945 ast_cli(fd
, "Queue Dump:\n");
946 AST_LIST_TRAVERSE(&headerlist
, f
, frame_list
)
947 ast_cli(fd
, "%d. Type %d, subclass %d from %s\n", x
++, f
->frametype
, f
->subclass
, f
->src
? f
->src
: "<Unknown>");
948 AST_LIST_UNLOCK(&headerlist
);
949 return RESULT_SUCCESS
;
952 static char frame_stats_usage
[] =
953 "Usage: core show frame stats\n"
954 " Displays debugging statistics from framer\n";
957 /* Builtin Asterisk CLI-commands for debugging */
958 static struct ast_cli_entry cli_show_codecs
= {
959 { "show", "codecs", NULL
},
960 show_codecs_deprecated
, NULL
,
963 static struct ast_cli_entry cli_show_audio_codecs
= {
964 { "show", "audio", "codecs", NULL
},
965 show_codecs_deprecated
, NULL
,
968 static struct ast_cli_entry cli_show_video_codecs
= {
969 { "show", "video", "codecs", NULL
},
970 show_codecs_deprecated
, NULL
,
973 static struct ast_cli_entry cli_show_image_codecs
= {
974 { "show", "image", "codecs", NULL
},
975 show_codecs_deprecated
, NULL
,
978 static struct ast_cli_entry cli_show_codec
= {
979 { "show", "codec", NULL
},
980 show_codec_n_deprecated
, NULL
,
984 static struct ast_cli_entry cli_show_frame_stats
= {
985 { "show", "frame", "stats", NULL
},
986 show_frame_stats
, NULL
,
990 static struct ast_cli_entry my_clis
[] = {
991 { { "core", "show", "codecs", NULL
},
992 show_codecs
, "Displays a list of codecs",
993 frame_show_codecs_usage
, NULL
, &cli_show_codecs
},
995 { { "core", "show", "audio", "codecs", NULL
},
996 show_codecs
, "Displays a list of audio codecs",
997 frame_show_codecs_usage
, NULL
, &cli_show_audio_codecs
},
999 { { "core", "show", "video", "codecs", NULL
},
1000 show_codecs
, "Displays a list of video codecs",
1001 frame_show_codecs_usage
, NULL
, &cli_show_video_codecs
},
1003 { { "core", "show", "image", "codecs", NULL
},
1004 show_codecs
, "Displays a list of image codecs",
1005 frame_show_codecs_usage
, NULL
, &cli_show_image_codecs
},
1007 { { "core", "show", "codec", NULL
},
1008 show_codec_n
, "Shows a specific codec",
1009 frame_show_codec_n_usage
, NULL
, &cli_show_codec
},
1012 { { "core", "show", "frame", "stats", NULL
},
1013 show_frame_stats
, "Shows frame statistics",
1014 frame_stats_usage
, NULL
, &cli_show_frame_stats
},
1018 int init_framer(void)
1020 ast_cli_register_multiple(my_clis
, sizeof(my_clis
) / sizeof(struct ast_cli_entry
));
1024 void ast_codec_pref_convert(struct ast_codec_pref
*pref
, char *buf
, size_t size
, int right
)
1026 int x
, differential
= (int) 'A', mem
;
1040 for (x
= 0; x
< 32 ; x
++) {
1043 to
[x
] = right
? (from
[x
] + differential
) : (from
[x
] - differential
);
1047 int ast_codec_pref_string(struct ast_codec_pref
*pref
, char *buf
, size_t size
)
1050 size_t total_len
, slen
;
1057 for(x
= 0; x
< 32 ; x
++) {
1060 if(!(codec
= ast_codec_pref_index(pref
,x
)))
1062 if((formatname
= ast_getformatname(codec
))) {
1063 slen
= strlen(formatname
);
1064 if(slen
> total_len
)
1066 strncat(buf
, formatname
, total_len
- 1); /* safe */
1069 if(total_len
&& x
< 31 && ast_codec_pref_index(pref
, x
+ 1)) {
1070 strncat(buf
, "|", total_len
- 1); /* safe */
1075 strncat(buf
, ")", total_len
- 1); /* safe */
1079 return size
- total_len
;
1082 int ast_codec_pref_index(struct ast_codec_pref
*pref
, int index
)
1087 if((index
>= 0) && (index
< sizeof(pref
->order
))) {
1088 slot
= pref
->order
[index
];
1091 return slot
? AST_FORMAT_LIST
[slot
-1].bits
: 0;
1094 /*! \brief Remove codec from pref list */
1095 void ast_codec_pref_remove(struct ast_codec_pref
*pref
, int format
)
1097 struct ast_codec_pref oldorder
;
1105 memcpy(&oldorder
, pref
, sizeof(oldorder
));
1106 memset(pref
, 0, sizeof(*pref
));
1108 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1109 slot
= oldorder
.order
[x
];
1110 size
= oldorder
.framing
[x
];
1113 if(AST_FORMAT_LIST
[slot
-1].bits
!= format
) {
1114 pref
->order
[y
] = slot
;
1115 pref
->framing
[y
++] = size
;
1121 /*! \brief Append codec to list */
1122 int ast_codec_pref_append(struct ast_codec_pref
*pref
, int format
)
1124 int x
, newindex
= 0;
1126 ast_codec_pref_remove(pref
, format
);
1128 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1129 if(AST_FORMAT_LIST
[x
].bits
== format
) {
1136 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1137 if(!pref
->order
[x
]) {
1138 pref
->order
[x
] = newindex
;
1147 /*! \brief Prepend codec to list */
1148 void ast_codec_pref_prepend(struct ast_codec_pref
*pref
, int format
, int only_if_existing
)
1150 int x
, newindex
= 0;
1152 /* First step is to get the codecs "index number" */
1153 for (x
= 0; x
< ARRAY_LEN(AST_FORMAT_LIST
); x
++) {
1154 if (AST_FORMAT_LIST
[x
].bits
== format
) {
1159 /* Done if its unknown */
1163 /* Now find any existing occurrence, or the end */
1164 for (x
= 0; x
< 32; x
++) {
1165 if (!pref
->order
[x
] || pref
->order
[x
] == newindex
)
1169 if (only_if_existing
&& !pref
->order
[x
])
1172 /* Move down to make space to insert - either all the way to the end,
1173 or as far as the existing location (which will be overwritten) */
1174 for (; x
> 0; x
--) {
1175 pref
->order
[x
] = pref
->order
[x
- 1];
1176 pref
->framing
[x
] = pref
->framing
[x
- 1];
1179 /* And insert the new entry */
1180 pref
->order
[0] = newindex
;
1181 pref
->framing
[0] = 0; /* ? */
1184 /*! \brief Set packet size for codec */
1185 int ast_codec_pref_setsize(struct ast_codec_pref
*pref
, int format
, int framems
)
1189 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1190 if(AST_FORMAT_LIST
[x
].bits
== format
) {
1199 /* size validation */
1201 framems
= AST_FORMAT_LIST
[index
].def_ms
;
1203 if(AST_FORMAT_LIST
[index
].inc_ms
&& framems
% AST_FORMAT_LIST
[index
].inc_ms
) /* avoid division by zero */
1204 framems
-= framems
% AST_FORMAT_LIST
[index
].inc_ms
;
1206 if(framems
< AST_FORMAT_LIST
[index
].min_ms
)
1207 framems
= AST_FORMAT_LIST
[index
].min_ms
;
1209 if(framems
> AST_FORMAT_LIST
[index
].max_ms
)
1210 framems
= AST_FORMAT_LIST
[index
].max_ms
;
1213 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1214 if(pref
->order
[x
] == (index
+ 1)) {
1215 pref
->framing
[x
] = framems
;
1223 /*! \brief Get packet size for codec */
1224 struct ast_format_list
ast_codec_pref_getsize(struct ast_codec_pref
*pref
, int format
)
1226 int x
, index
= -1, framems
= 0;
1227 struct ast_format_list fmt
= {0};
1229 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1230 if(AST_FORMAT_LIST
[x
].bits
== format
) {
1231 fmt
= AST_FORMAT_LIST
[x
];
1237 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1238 if(pref
->order
[x
] == (index
+ 1)) {
1239 framems
= pref
->framing
[x
];
1244 /* size validation */
1246 framems
= AST_FORMAT_LIST
[index
].def_ms
;
1248 if(AST_FORMAT_LIST
[index
].inc_ms
&& framems
% AST_FORMAT_LIST
[index
].inc_ms
) /* avoid division by zero */
1249 framems
-= framems
% AST_FORMAT_LIST
[index
].inc_ms
;
1251 if(framems
< AST_FORMAT_LIST
[index
].min_ms
)
1252 framems
= AST_FORMAT_LIST
[index
].min_ms
;
1254 if(framems
> AST_FORMAT_LIST
[index
].max_ms
)
1255 framems
= AST_FORMAT_LIST
[index
].max_ms
;
1257 fmt
.cur_ms
= framems
;
1262 /*! \brief Pick a codec */
1263 int ast_codec_choose(struct ast_codec_pref
*pref
, int formats
, int find_best
)
1265 int x
, ret
= 0, slot
;
1267 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1268 slot
= pref
->order
[x
];
1272 if (formats
& AST_FORMAT_LIST
[slot
-1].bits
) {
1273 ret
= AST_FORMAT_LIST
[slot
-1].bits
;
1277 if(ret
& AST_FORMAT_AUDIO_MASK
)
1280 if (option_debug
> 3)
1281 ast_log(LOG_DEBUG
, "Could not find preferred codec - %s\n", find_best
? "Going for the best codec" : "Returning zero codec");
1283 return find_best
? ast_best_codec(formats
) : 0;
1286 void ast_parse_allow_disallow(struct ast_codec_pref
*pref
, int *mask
, const char *list
, int allowing
)
1288 char *parse
= NULL
, *this = NULL
, *psize
= NULL
;
1289 int format
= 0, framems
= 0;
1291 parse
= ast_strdupa(list
);
1292 while ((this = strsep(&parse
, ","))) {
1294 if ((psize
= strrchr(this, ':'))) {
1297 ast_log(LOG_DEBUG
,"Packetization for codec: %s is %s\n", this, psize
);
1298 framems
= atoi(psize
);
1302 if (!(format
= ast_getformatbyname(this))) {
1303 ast_log(LOG_WARNING
, "Cannot %s unknown format '%s'\n", allowing
? "allow" : "disallow", this);
1314 /* Set up a preference list for audio. Do not include video in preferences
1315 since we can not transcode video and have to use whatever is offered
1317 if (pref
&& (format
& AST_FORMAT_AUDIO_MASK
)) {
1318 if (strcasecmp(this, "all")) {
1320 ast_codec_pref_append(pref
, format
);
1321 ast_codec_pref_setsize(pref
, format
, framems
);
1324 ast_codec_pref_remove(pref
, format
);
1325 } else if (!allowing
) {
1326 memset(pref
, 0, sizeof(*pref
));
1332 static int g723_len(unsigned char buf
)
1334 enum frame_type type
= buf
& TYPE_MASK
;
1350 ast_log(LOG_WARNING
, "Badly encoded frame (%d)\n", type
);
1355 static int g723_samples(unsigned char *buf
, int maxlen
)
1360 while(pos
< maxlen
) {
1361 res
= g723_len(buf
[pos
]);
1370 static unsigned char get_n_bits_at(unsigned char *data
, int n
, int bit
)
1372 int byte
= bit
/ 8; /* byte containing first bit */
1373 int rem
= 8 - (bit
% 8); /* remaining bits in first byte */
1374 unsigned char ret
= 0;
1376 if (n
<= 0 || n
> 8)
1380 ret
= (data
[byte
] << (n
- rem
));
1381 ret
|= (data
[byte
+ 1] >> (8 - n
+ rem
));
1383 ret
= (data
[byte
] >> (rem
- n
));
1386 return (ret
& (0xff >> (8 - n
)));
1389 static int speex_get_wb_sz_at(unsigned char *data
, int len
, int bit
)
1391 static int SpeexWBSubModeSz
[] = {
1397 /* skip up to two wideband frames */
1398 if (((len
* 8 - off
) >= 5) &&
1399 get_n_bits_at(data
, 1, off
)) {
1400 c
= get_n_bits_at(data
, 3, off
+ 1);
1401 off
+= SpeexWBSubModeSz
[c
];
1403 if (((len
* 8 - off
) >= 5) &&
1404 get_n_bits_at(data
, 1, off
)) {
1405 c
= get_n_bits_at(data
, 3, off
+ 1);
1406 off
+= SpeexWBSubModeSz
[c
];
1408 if (((len
* 8 - off
) >= 5) &&
1409 get_n_bits_at(data
, 1, off
)) {
1410 ast_log(LOG_WARNING
, "Encountered corrupt speex frame; too many wideband frames in a row.\n");
1419 static int speex_samples(unsigned char *data
, int len
)
1421 static int SpeexSubModeSz
[] = {
1426 static int SpeexInBandSz
[] = {
1436 while ((len
* 8 - bit
) >= 5) {
1437 /* skip wideband frames */
1438 off
= speex_get_wb_sz_at(data
, len
, bit
);
1440 ast_log(LOG_WARNING
, "Had error while reading wideband frames for speex samples\n");
1445 if ((len
* 8 - bit
) < 5) {
1446 ast_log(LOG_WARNING
, "Not enough bits remaining after wide band for speex samples.\n");
1450 /* get control bits */
1451 c
= get_n_bits_at(data
, 5, bit
);
1457 } else if (c
== 14) {
1458 /* in-band signal; next 4 bits contain signal id */
1459 c
= get_n_bits_at(data
, 4, bit
);
1461 bit
+= SpeexInBandSz
[c
];
1462 } else if (c
== 13) {
1463 /* user in-band; next 5 bits contain msg len */
1464 c
= get_n_bits_at(data
, 5, bit
);
1471 /* skip number bits for submode (less the 5 control bits) */
1472 bit
+= SpeexSubModeSz
[c
] - 5;
1473 cnt
+= 160; /* new frame */
1479 int ast_codec_get_samples(struct ast_frame
*f
)
1482 switch(f
->subclass
) {
1483 case AST_FORMAT_SPEEX
:
1484 samples
= speex_samples(f
->data
, f
->datalen
);
1486 case AST_FORMAT_G723_1
:
1487 samples
= g723_samples(f
->data
, f
->datalen
);
1489 case AST_FORMAT_ILBC
:
1490 samples
= 240 * (f
->datalen
/ 50);
1492 case AST_FORMAT_GSM
:
1493 samples
= 160 * (f
->datalen
/ 33);
1495 case AST_FORMAT_G729A
:
1496 samples
= f
->datalen
* 8;
1498 case AST_FORMAT_SLINEAR
:
1499 samples
= f
->datalen
/ 2;
1501 case AST_FORMAT_LPC10
:
1502 /* assumes that the RTP packet contains one LPC10 frame */
1504 samples
+= (((char *)(f
->data
))[7] & 0x1) * 8;
1506 case AST_FORMAT_ULAW
:
1507 case AST_FORMAT_ALAW
:
1508 samples
= f
->datalen
;
1510 case AST_FORMAT_G722
:
1511 case AST_FORMAT_ADPCM
:
1512 case AST_FORMAT_G726
:
1513 case AST_FORMAT_G726_AAL2
:
1514 samples
= f
->datalen
* 2;
1517 ast_log(LOG_WARNING
, "Unable to calculate samples for format %s\n", ast_getformatname(f
->subclass
));
1522 int ast_codec_get_len(int format
, int samples
)
1526 /* XXX Still need speex, g723, and lpc10 XXX */
1528 case AST_FORMAT_ILBC
:
1529 len
= (samples
/ 240) * 50;
1531 case AST_FORMAT_GSM
:
1532 len
= (samples
/ 160) * 33;
1534 case AST_FORMAT_G729A
:
1537 case AST_FORMAT_SLINEAR
:
1540 case AST_FORMAT_ULAW
:
1541 case AST_FORMAT_ALAW
:
1544 case AST_FORMAT_G722
:
1545 case AST_FORMAT_ADPCM
:
1546 case AST_FORMAT_G726
:
1547 case AST_FORMAT_G726_AAL2
:
1551 ast_log(LOG_WARNING
, "Unable to calculate sample length for format %s\n", ast_getformatname(format
));
1557 int ast_frame_adjust_volume(struct ast_frame
*f
, int adjustment
)
1560 short *fdata
= f
->data
;
1561 short adjust_value
= abs(adjustment
);
1563 if ((f
->frametype
!= AST_FRAME_VOICE
) || (f
->subclass
!= AST_FORMAT_SLINEAR
))
1569 for (count
= 0; count
< f
->samples
; count
++) {
1570 if (adjustment
> 0) {
1571 ast_slinear_saturated_multiply(&fdata
[count
], &adjust_value
);
1572 } else if (adjustment
< 0) {
1573 ast_slinear_saturated_divide(&fdata
[count
], &adjust_value
);
1580 int ast_frame_slinear_sum(struct ast_frame
*f1
, struct ast_frame
*f2
)
1583 short *data1
, *data2
;
1585 if ((f1
->frametype
!= AST_FRAME_VOICE
) || (f1
->subclass
!= AST_FORMAT_SLINEAR
))
1588 if ((f2
->frametype
!= AST_FRAME_VOICE
) || (f2
->subclass
!= AST_FORMAT_SLINEAR
))
1591 if (f1
->samples
!= f2
->samples
)
1594 for (count
= 0, data1
= f1
->data
, data2
= f2
->data
;
1595 count
< f1
->samples
;
1596 count
++, data1
++, data2
++)
1597 ast_slinear_saturated_add(data1
, data2
);