Skip to content
Snippets Groups Projects
frame.c 41 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    /*
    
     * Asterisk -- An open source telephony toolkit.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Copyright (C) 1999 - 2005, Digium, Inc.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Mark Spencer <markster@digium.com>
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
    
    Mark Spencer's avatar
    Mark Spencer committed
     * This program is free software, distributed under the terms of
    
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    
    Olle Johansson's avatar
    Olle Johansson committed
     * \brief Frame and codec manipulation routines
    
     *
     * \author Mark Spencer <markster@digium.com> 
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    #include "asterisk/_private.h"
    
    #include "asterisk/lock.h"
    #include "asterisk/frame.h"
    #include "asterisk/channel.h"
    #include "asterisk/cli.h"
    #include "asterisk/term.h"
    #include "asterisk/utils.h"
    
    #include "asterisk/threadstorage.h"
    #include "asterisk/linkedlists.h"
    
    #include "asterisk/translate.h"
    
    #include "asterisk/dsp.h"
    
    #include "asterisk/file.h"
    
    #if !defined(LOW_MEMORY)
    
    static void frame_cache_cleanup(void *data);
    
    /*! \brief A per-thread cache of frame headers */
    
    AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
    
    
    /*! 
     * \brief Maximum ast_frame cache size
     *
     * In most cases where the frame header cache will be useful, the size
     * of the cache will stay very small.  However, it is not always the case that
     * the same thread that allocates the frame will be the one freeing them, so
     * sometimes a thread will never have any frames in its cache, or the cache
     * will never be pulled from.  For the latter case, we limit the maximum size. 
     */ 
    #define FRAME_CACHE_MAX_SIZE	10
    
    /*! \brief This is just so ast_frames, a list head struct for holding a list of
     *  ast_frame structures, is defined. */
    AST_LIST_HEAD_NOLOCK(ast_frames, ast_frame);
    
    struct ast_frame_cache {
    	struct ast_frames list;
    	size_t size;
    };
    
    Mark Spencer's avatar
    Mark Spencer committed
    #define SMOOTHER_SIZE 8000
    
    
    enum frame_type {
    	TYPE_HIGH,     /* 0x0 */
    	TYPE_LOW,      /* 0x1 */
    	TYPE_SILENCE,  /* 0x2 */
    	TYPE_DONTSEND  /* 0x3 */
    };
    
    #define TYPE_MASK 0x3
    
    Mark Spencer's avatar
    Mark Spencer committed
    struct ast_smoother {
    	int size;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	float samplesperbyte;
    
    	unsigned int opt_needs_swap:1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	struct ast_frame f;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char data[SMOOTHER_SIZE];
    	char framedata[SMOOTHER_SIZE + AST_FRIENDLY_OFFSET];
    
    	struct ast_frame *opt;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int len;
    };
    
    
    /*! \brief Definition of supported media formats (codecs) */
    
    static const struct ast_format_list AST_FORMAT_LIST[] = {
    
    	{ AST_FORMAT_G723_1 , "g723", 8000, "G.723.1", 20, 30, 300, 30, 30 },                                  /*!< G723.1 */
    	{ AST_FORMAT_GSM, "gsm", 8000, "GSM", 33, 20, 300, 20, 20 },                                           /*!< codec_gsm.c */
    	{ AST_FORMAT_ULAW, "ulaw", 8000, "G.711 u-law", 80, 10, 150, 10, 20 },                                 /*!< codec_ulaw.c */
    	{ AST_FORMAT_ALAW, "alaw", 8000, "G.711 A-law", 80, 10, 150, 10, 20 },                                 /*!< codec_alaw.c */
    	{ AST_FORMAT_G726, "g726", 8000, "G.726 RFC3551", 40, 10, 300, 10, 20 },                               /*!< codec_g726.c */
    	{ AST_FORMAT_ADPCM, "adpcm" , 8000, "ADPCM", 40, 10, 300, 10, 20 },                                    /*!< codec_adpcm.c */
    	{ AST_FORMAT_SLINEAR, "slin", 8000, "16 bit Signed Linear PCM", 160, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE }, /*!< Signed linear */
    	{ AST_FORMAT_LPC10, "lpc10", 8000, "LPC10", 7, 20, 20, 20, 20 },                                       /*!< codec_lpc10.c */ 
    	{ AST_FORMAT_G729A, "g729", 8000, "G.729A", 10, 10, 230, 10, 20, AST_SMOOTHER_FLAG_G729 },             /*!< Binary commercial distribution */
    	{ AST_FORMAT_SPEEX, "speex", 8000, "SpeeX", 10, 10, 60, 10, 20 },                                      /*!< codec_speex.c */
    	{ AST_FORMAT_ILBC, "ilbc", 8000, "iLBC", 50, 30, 30, 30, 30 },                                         /*!< codec_ilbc.c */ /* inc=30ms - workaround */
    	{ AST_FORMAT_G726_AAL2, "g726aal2", 8000, "G.726 AAL2", 40, 10, 300, 10, 20 },                         /*!< codec_g726.c */
    	{ AST_FORMAT_G722, "g722", 16000, "G722", 80, 10, 150, 10, 20 },                                       /*!< codec_g722.c */
    
    	{ AST_FORMAT_SLINEAR16, "slin16", 16000, "16 bit Signed Linear PCM (16kHz)", 320, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE },    /*!< Signed linear (16kHz) */
    
    	{ AST_FORMAT_JPEG, "jpeg", 0, "JPEG image"},                                                           /*!< See format_jpeg.c */
    	{ AST_FORMAT_PNG, "png", 0, "PNG image"},                                                              /*!< PNG Image format */
    	{ AST_FORMAT_H261, "h261", 0, "H.261 Video" },                                                         /*!< H.261 Video Passthrough */
    	{ AST_FORMAT_H263, "h263", 0, "H.263 Video" },                                                         /*!< H.263 Passthrough support, see format_h263.c */
    	{ AST_FORMAT_H263_PLUS, "h263p", 0, "H.263+ Video" },                                                  /*!< H.263plus passthrough support See format_h263.c */
    	{ AST_FORMAT_H264, "h264", 0, "H.264 Video" },                                                         /*!< Passthrough support, see format_h263.c */
    	{ AST_FORMAT_MP4_VIDEO, "mpeg4", 0, "MPEG4 Video" },                                                   /*!< Passthrough support for MPEG4 */
    
    	{ AST_FORMAT_T140RED, "red", 1, "T.140 Realtime Text with redundancy"},                                /*!< Redundant T.140 Realtime Text */
    
    	{ AST_FORMAT_T140, "t140", 0, "Passthrough T.140 Realtime Text" },                                     /*!< Passthrough support for T.140 Realtime Text */
    
    	{ AST_FORMAT_SIREN7, "siren7", 16000, "ITU G.722.1 (Siren7, licensed from Polycom)", 80, 20, 80, 20, 20 },			/*!< Binary commercial distribution */
    	{ AST_FORMAT_SIREN14, "siren14", 32000, "ITU G.722.1 Annex C, (Siren14, licensed from Polycom)", 120, 20, 80, 20, 20 },	/*!< Binary commercial distribution */
    
    	{ AST_FORMAT_TESTLAW, "testlaw", 8000, "G.711 test-law", 80, 10, 150, 10, 20 },                                 /*!< codec_ulaw.c */
    
    struct ast_frame ast_null_frame = { AST_FRAME_NULL, };
    
    
    static int smoother_frame_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
    {
    	if (s->flags & AST_SMOOTHER_FLAG_G729) {
    		if (s->len % 10) {
    			ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
    			return 0;
    		}
    	}
    	if (swap) {
    		ast_swapcopy_samples(s->data + s->len, f->data.ptr, f->samples);
    	} else {
    		memcpy(s->data + s->len, f->data.ptr, f->datalen);
    	}
    	/* If either side is empty, reset the delivery time */
    	if (!s->len || ast_tvzero(f->delivery) || ast_tvzero(s->delivery)) {	/* XXX really ? */
    		s->delivery = f->delivery;
    	}
    	s->len += f->datalen;
    
    	return 0;
    }
    
    void ast_smoother_reset(struct ast_smoother *s, int bytes)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	s->size = bytes;
    }
    
    void ast_smoother_reconfigure(struct ast_smoother *s, int bytes)
    {
    	/* if there is no change, then nothing to do */
    	if (s->size == bytes) {
    		return;
    	}
    	/* set the new desired output size */
    	s->size = bytes;
    	/* if there is no 'optimized' frame in the smoother,
    	 *   then there is nothing left to do
    	 */
    	if (!s->opt) {
    		return;
    	}
    	/* there is an 'optimized' frame here at the old size,
    	 * but it must now be put into the buffer so the data
    	 * can be extracted at the new size
    	 */
    	smoother_frame_feed(s, s->opt, s->opt_needs_swap);
    	s->opt = NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    struct ast_smoother *ast_smoother_new(int size)
    {
    	struct ast_smoother *s;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (size < 1)
    		return NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_smoother_reset(s, size);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return s;
    }
    
    
    int ast_smoother_get_flags(struct ast_smoother *s)
    {
    	return s->flags;
    }
    
    void ast_smoother_set_flags(struct ast_smoother *s, int flags)
    {
    	s->flags = flags;
    }
    
    
    int ast_smoother_test_flag(struct ast_smoother *s, int flag)
    {
    	return (s->flags & flag);
    }
    
    
    int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	if (f->frametype != AST_FRAME_VOICE) {
    		ast_log(LOG_WARNING, "Huh?  Can't smooth a non-voice frame!\n");
    		return -1;
    	}
    	if (!s->format) {
    
    		s->format = f->subclass.codec;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		s->samplesperbyte = (float)f->samples / (float)f->datalen;
    
    	} else if (s->format != f->subclass.codec) {
    		ast_log(LOG_WARNING, "Smoother was working on %s format frames, now trying to feed %s?\n",
    			ast_getformatname(s->format), ast_getformatname(f->subclass.codec));
    
    Mark Spencer's avatar
    Mark Spencer committed
    		return -1;
    	}
    	if (s->len + f->datalen > SMOOTHER_SIZE) {
    		ast_log(LOG_WARNING, "Out of smoother space\n");
    		return -1;
    	}
    
    	if (((f->datalen == s->size) ||
    	     ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729))) &&
    	    !s->opt &&
    	    !s->len &&
    	    (f->offset >= AST_MIN_OFFSET)) {
    		/* Optimize by sending the frame we just got
    		   on the next read, thus eliminating the douple
    		   copy */
    		if (swap)
    			ast_swapcopy_samples(f->data.ptr, f->data.ptr, f->samples);
    		s->opt = f;
    		s->opt_needs_swap = swap ? 1 : 0;
    		return 0;
    
    
    	return smoother_frame_feed(s, f, swap);
    
    Mark Spencer's avatar
    Mark Spencer committed
    }
    
    struct ast_frame *ast_smoother_read(struct ast_smoother *s)
    {
    
    	struct ast_frame *opt;
    
    Olle Johansson's avatar
    Olle Johansson committed
    
    
    	/* IF we have an optimization frame, send it */
    	if (s->opt) {
    
    		if (s->opt->offset < AST_FRIENDLY_OFFSET)
    
    			ast_log(LOG_WARNING, "Returning a frame of inappropriate offset (%d).\n",
    
    							s->opt->offset);
    
    		opt = s->opt;
    		s->opt = NULL;
    		return opt;
    	}
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Make sure we have enough data */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (s->len < s->size) {
    
    		/* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
    
    		if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->len % 10)))
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	len = s->size;
    	if (len > s->len)
    		len = s->len;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Make frame */
    	s->f.frametype = AST_FRAME_VOICE;
    
    	s->f.subclass.codec = s->format;
    
    	s->f.data.ptr = s->framedata + AST_FRIENDLY_OFFSET;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	s->f.offset = AST_FRIENDLY_OFFSET;
    
    	s->f.datalen = len;
    	/* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
    
    	s->f.samples = len * s->samplesperbyte;	/* XXX rounding */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Fill Data */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Move remaining data to the front if applicable */
    
    		/* In principle this should all be fine because if we are sending
    		   G.729 VAD, the next timestamp will take over anyawy */
    		memmove(s->data, s->data + len, s->len);
    
    			/* If we have delivery time, increment it, otherwise, leave it at 0 */
    
    			s->delivery = ast_tvadd(s->delivery, ast_samp2tv(s->f.samples, ast_format_rate(s->format)));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Return frame */
    	return &s->f;
    }
    
    void ast_smoother_free(struct ast_smoother *s)
    {
    
    Mark Spencer's avatar
    Mark Spencer committed
    static struct ast_frame *ast_frame_header_new(void)
    {
    
    	struct ast_frame *f;
    
    	struct ast_frame_cache *frames;
    
    	if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
    		if ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) {
    			size_t mallocd_len = f->mallocd_hdr_len;
    			memset(f, 0, sizeof(*f));
    			f->mallocd_hdr_len = mallocd_len;
    			f->mallocd = AST_MALLOCD_HDR;
    			frames->size--;
    			return f;
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	if (!(f = ast_calloc_cache(1, sizeof(*f))))
    
    		return NULL;
    
    #else
    	if (!(f = ast_calloc(1, sizeof(*f))))
    		return NULL;
    #endif
    
    
    	f->mallocd_hdr_len = sizeof(*f);
    	
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return f;
    }
    
    static void frame_cache_cleanup(void *data)
    {
    	struct ast_frame_cache *frames = data;
    	struct ast_frame *f;
    
    	while ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list)))
    
    static void __frame_free(struct ast_frame *fr, int cache)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	if (!fr->mallocd)
    		return;
    
    
    	if (cache && fr->mallocd == AST_MALLOCD_HDR) {
    
    		/* Cool, only the header is malloc'd, let's just cache those for now 
    		 * to keep things simple... */
    		struct ast_frame_cache *frames;
    
    
    		if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames))) &&
    		    (frames->size < FRAME_CACHE_MAX_SIZE)) {
    
    			AST_LIST_INSERT_HEAD(&frames->list, fr, frame_list);
    			frames->size++;
    			return;
    		}
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (fr->mallocd & AST_MALLOCD_DATA) {
    
    		if (fr->data.ptr) 
    			ast_free(fr->data.ptr - fr->offset);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    	if (fr->mallocd & AST_MALLOCD_SRC) {
    		if (fr->src)
    
    			ast_free((void *) fr->src);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    	if (fr->mallocd & AST_MALLOCD_HDR) {
    
    
    void ast_frame_free(struct ast_frame *frame, int cache)
    {
    	struct ast_frame *next;
    
    	for (next = AST_LIST_NEXT(frame, frame_list);
    	     frame;
    	     frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) {
    		__frame_free(frame, cache);
    	}
    }
    
    
    /*!
     * \brief 'isolates' a frame by duplicating non-malloc'ed components
    
     * (header, src, data).
     * On return all components are malloc'ed
     */
    
    Mark Spencer's avatar
    Mark Spencer committed
    struct ast_frame *ast_frisolate(struct ast_frame *fr)
    {
    	struct ast_frame *out;
    
    	/* if none of the existing frame is malloc'd, let ast_frdup() do it
    	   since it is more efficient
    	*/
    	if (fr->mallocd == 0) {
    		return ast_frdup(fr);
    	}
    
    	/* if everything is already malloc'd, we are done */
    	if ((fr->mallocd & (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) ==
    	    (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) {
    		return fr;
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (!(fr->mallocd & AST_MALLOCD_HDR)) {
    		/* Allocate a new header if needed */
    
    		if (!(out = ast_frame_header_new())) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    			return NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		out->frametype = fr->frametype;
    
    		out->subclass.codec = fr->subclass.codec;
    
    		out->datalen = fr->datalen;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		out->samples = fr->samples;
    
    		out->offset = fr->offset;
    
    		ast_copy_flags(out, fr, AST_FRFLAG_HAS_TIMING_INFO);
    		if (ast_test_flag(fr, AST_FRFLAG_HAS_TIMING_INFO)) {
    
    			out->ts = fr->ts;
    			out->len = fr->len;
    			out->seqno = fr->seqno;
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		out = fr;
    
    	if (!(fr->mallocd & AST_MALLOCD_SRC) && fr->src) {
    		if (!(out->src = ast_strdup(fr->src))) {
    			if (out != fr) {
    				ast_free(out);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		out->src = fr->src;
    
    		fr->src = NULL;
    		fr->mallocd &= ~AST_MALLOCD_SRC;
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (!(fr->mallocd & AST_MALLOCD_DATA))  {
    
    		if (!(newdata = ast_malloc(fr->datalen + AST_FRIENDLY_OFFSET))) {
    
    			if (out->src != fr->src) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    			return NULL;
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		out->offset = AST_FRIENDLY_OFFSET;
    		out->datalen = fr->datalen;
    
    		memcpy(newdata, fr->data.ptr, fr->datalen);
    		out->data.ptr = newdata;
    
    	} else {
    		out->data = fr->data;
    		memset(&fr->data, 0, sizeof(fr->data));
    		fr->mallocd &= ~AST_MALLOCD_DATA;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return out;
    }
    
    struct ast_frame *ast_frdup(const struct ast_frame *f)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	struct ast_frame *out = NULL;
    
    	void *buf = NULL;
    
    
    #if !defined(LOW_MEMORY)
    	struct ast_frame_cache *frames;
    #endif
    
    
    	/* Start with standard stuff */
    
    	len = sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
    
    	/* If we have a source, add space for it */
    
    	/*
    	 * XXX Watch out here - if we receive a src which is not terminated
    	 * properly, we can be easily attacked. Should limit the size we deal with.
    	 */
    
    	if (f->src)
    		srclen = strlen(f->src);
    	if (srclen > 0)
    		len += srclen + 1;
    
    	if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
    		AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) {
    			if (out->mallocd_hdr_len >= len) {
    				size_t mallocd_len = out->mallocd_hdr_len;
    
    
    				AST_LIST_REMOVE_CURRENT(frame_list);
    
    				memset(out, 0, sizeof(*out));
    				out->mallocd_hdr_len = mallocd_len;
    				buf = out;
    				frames->size--;
    				break;
    			}
    		}
    
    		AST_LIST_TRAVERSE_SAFE_END;
    
    	if (!buf) {
    
    		if (!(buf = ast_calloc_cache(1, len)))
    
    			return NULL;
    		out = buf;
    		out->mallocd_hdr_len = len;
    	}
    
    
    	out->frametype = f->frametype;
    
    	out->subclass.codec = f->subclass.codec;
    
    	out->datalen = f->datalen;
    	out->samples = f->samples;
    
    	/* Set us as having malloc'd header only, so it will eventually
    	   get freed. */
    
    	out->mallocd = AST_MALLOCD_HDR;
    	out->offset = AST_FRIENDLY_OFFSET;
    
    		out->data.ptr = buf + sizeof(*out) + AST_FRIENDLY_OFFSET;
    		memcpy(out->data.ptr, f->data.ptr, out->datalen);	
    
    	} else {
    		out->data.uint32 = f->data.uint32;
    
    		/* This may seem a little strange, but it's to avoid a gcc (4.2.4) compiler warning */
    		char *src;
    
    		out->src = buf + sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
    
    		src = (char *) out->src;
    
    		/* Must have space since we allocated for it */
    
    		strcpy(src, f->src);
    
    	ast_copy_flags(out, f, AST_FRFLAG_HAS_TIMING_INFO);
    
    	out->ts = f->ts;
    	out->len = f->len;
    	out->seqno = f->seqno;
    
    	return out;
    
    void ast_swapcopy_samples(void *dst, const void *src, int samples)
    
    	const unsigned short *src_s = src;
    
    Olle Johansson's avatar
    Olle Johansson committed
    	for (i = 0; i < samples; i++)
    
    const struct ast_format_list *ast_get_format_list_index(int idx) 
    
    	return &AST_FORMAT_LIST[idx];
    
    const struct ast_format_list *ast_get_format_list(size_t *size) 
    
    	*size = ARRAY_LEN(AST_FORMAT_LIST);
    
    char* ast_getformatname(format_t format)
    
    	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
    
    		if (AST_FORMAT_LIST[x].bits == format) {
    
    char *ast_getformatname_multiple(char *buf, size_t size, format_t format)
    
    Olle Johansson's avatar
    Olle Johansson committed
    {
    
    Olle Johansson's avatar
    Olle Johansson committed
    
    	if (!size)
    		return buf;
    
    	snprintf(end, size, "0x%llx (", (unsigned long long) format);
    
    	len = strlen(end);
    	end += len;
    	size -= len;
    	start = end;
    
    	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
    
    		if (AST_FORMAT_LIST[x].bits & format) {
    
    			snprintf(end, size, "%s|", AST_FORMAT_LIST[x].name);
    
    			len = strlen(end);
    			end += len;
    			size -= len;
    
    		ast_copy_string(start, "nothing)", size);
    
    static struct ast_codec_alias_table {
    	char *alias;
    	char *realname;
    } ast_codec_alias_table[] = {
    
    Olle Johansson's avatar
    Olle Johansson committed
    	{ "slinear", "slin"},
    
    	{ "slinear16", "slin16"},
    
    Olle Johansson's avatar
    Olle Johansson committed
    	{ "g723.1", "g723"},
    
    	{ "g722.1", "siren7"},
    	{ "g722.1c", "siren14"},
    
    Olle Johansson's avatar
    Olle Johansson committed
    static const char *ast_expand_codec_alias(const char *in)
    {
    
    	for (x = 0; x < ARRAY_LEN(ast_codec_alias_table); x++) {
    
    		if (!strcmp(in,ast_codec_alias_table[x].alias))
    
    			return ast_codec_alias_table[x].realname;
    	}
    	return in;
    }
    
    
    format_t ast_getformatbyname(const char *name)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	int x, all;
    	format_t format = 0;
    
    	all = strcasecmp(name, "all") ? 0 : 1;
    
    	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
    
    Olle Johansson's avatar
    Olle Johansson committed
    			  !strcasecmp(AST_FORMAT_LIST[x].name,name) ||
    
    			  !strcasecmp(AST_FORMAT_LIST[x].name, ast_expand_codec_alias(name))) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    }
    
    char *ast_codec2str(format_t codec)
    
    Olle Johansson's avatar
    Olle Johansson committed
    {
    
    	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
    
    		if (AST_FORMAT_LIST[x].bits == codec) {
    
    			ret = AST_FORMAT_LIST[x].desc;
    			break;
    		}
    	}
    	return ret;
    
    Jason Parker's avatar
    Jason Parker committed
    static char *show_codecs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
    
    {
    	int i, found=0;
    	char hex[25];
    
    Jason Parker's avatar
    Jason Parker committed
    
    	switch (cmd) {
    	case CLI_INIT:
    
    		e->command = "core show codecs [audio|video|image|text]";
    		e->usage =
    			"Usage: core show codecs [audio|video|image|text]\n"
    
    Jason Parker's avatar
    Jason Parker committed
    			"       Displays codec mapping\n";
    		return NULL;
    	case CLI_GENERATE:
    		return NULL;
    	}
    
    Jason Parker's avatar
    Jason Parker committed
    	if ((a->argc < 3) || (a->argc > 4))
    		return CLI_SHOWUSAGE;
    
    
    	if (!ast_opt_dont_warn)
    
    Jason Parker's avatar
    Jason Parker committed
    		ast_cli(a->fd, "Disclaimer: this command is for informational purposes only.\n"
    
    				"\tIt does not indicate anything about your configuration.\n");
    
    
    	ast_cli(a->fd, "%19s %9s %20s   TYPE   %8s   %s\n","INT","BINARY","HEX","NAME","DESCRIPTION");
    	ast_cli(a->fd, "-----------------------------------------------------------------------------------\n");
    	for (i = 0; i < 63; i++) {
    
    		if (a->argc == 4) {
    			if (!strcasecmp(a->argv[3], "audio")) {
    				if (!((1LL << i) & AST_FORMAT_AUDIO_MASK)) {
    					continue;
    				}
    			} else if (!strcasecmp(a->argv[3], "video")) {
    				if (!((1LL << i) & AST_FORMAT_VIDEO_MASK)) {
    					continue;
    				}
    			} else if (!strcasecmp(a->argv[3], "image")) {
    				if (i != 16 && i != 17) {
    					continue;
    				}
    			} else if (!strcasecmp(a->argv[3], "text")) {
    				if (!((1LL << i) & AST_FORMAT_TEXT_MASK)) {
    					continue;
    				}
    			} else {
    
    		snprintf(hex, sizeof(hex), "(0x%llx)", 1LL << i);
    		ast_cli(a->fd, "%19llu (1 << %2d) %20s  %5s   %8s   (%s)\n", 1LL << i, i, hex,
    
    			((1LL << i) & AST_FORMAT_AUDIO_MASK) ? "audio" :
    			i == 16 || i == 17 ? "image" :
    			((1LL << i) & AST_FORMAT_VIDEO_MASK) ? "video" :
    			((1LL << i) & AST_FORMAT_TEXT_MASK) ? "text" :
    			"(unk)",
    			ast_getformatname(1LL << i), ast_codec2str(1LL << i));
    
    Jason Parker's avatar
    Jason Parker committed
    		return CLI_SHOWUSAGE;
    
    Jason Parker's avatar
    Jason Parker committed
    		return CLI_SUCCESS;
    
    Jason Parker's avatar
    Jason Parker committed
    static char *show_codec_n(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
    
    	format_t codec;
    	int i, found = 0;
    
    	long long type_punned_codec;
    
    Jason Parker's avatar
    Jason Parker committed
    	switch (cmd) {
    	case CLI_INIT:
    		e->command = "core show codec";
    
    Jason Parker's avatar
    Jason Parker committed
    			"Usage: core show codec <number>\n"
    			"       Displays codec mapping\n";
    		return NULL;
    	case CLI_GENERATE:
    		return NULL;
    	}
    
    Jason Parker's avatar
    Jason Parker committed
    	if (a->argc != 4)
    		return CLI_SHOWUSAGE;
    
    
    	if (sscanf(a->argv[3], "%30lld", &type_punned_codec) != 1) {
    
    Jason Parker's avatar
    Jason Parker committed
    		return CLI_SHOWUSAGE;
    
    	}
    	codec = type_punned_codec;
    
    	for (i = 0; i < 63; i++)
    		if (codec & (1LL << i)) {
    
    			ast_cli(a->fd, "%11llu (1 << %2d)  %s\n", 1LL << i, i, ast_codec2str(1LL << i));
    
    		ast_cli(a->fd, "Codec %lld not found\n", (long long) codec);
    
    Jason Parker's avatar
    Jason Parker committed
    	return CLI_SUCCESS;
    
    /*! Dump a frame for debugging purposes */
    
    void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix)
    
    	const char noname[] = "unknown";
    
    	char ftype[40] = "Unknown Frametype";
    	char cft[80];
    	char subclass[40] = "Unknown Subclass";
    	char csub[80];
    	char moreinfo[40] = "";
    
    	char cp[40];
    	char cmn[40];
    
    	if (!f) {
    		ast_verbose("%s [ %s (NULL) ] [%s]\n", 
    			term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
    			term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)), 
    
    			term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
    
    		return;
    	}
    	/* XXX We should probably print one each of voice and video when the format changes XXX */
    	if (f->frametype == AST_FRAME_VOICE)
    		return;
    	if (f->frametype == AST_FRAME_VIDEO)
    		return;
    	switch(f->frametype) {
    
    	case AST_FRAME_DTMF_BEGIN:
    		strcpy(ftype, "DTMF Begin");
    
    		subclass[0] = f->subclass.integer;
    
    		subclass[1] = '\0';
    		break;
    	case AST_FRAME_DTMF_END:
    		strcpy(ftype, "DTMF End");
    
    		subclass[0] = f->subclass.integer;
    
    		subclass[1] = '\0';
    		break;
    	case AST_FRAME_CONTROL:
    		strcpy(ftype, "Control");
    
    		switch (f->subclass.integer) {
    
    		case AST_CONTROL_HANGUP:
    			strcpy(subclass, "Hangup");
    			break;
    		case AST_CONTROL_RING:
    			strcpy(subclass, "Ring");
    			break;
    		case AST_CONTROL_RINGING:
    			strcpy(subclass, "Ringing");
    			break;
    		case AST_CONTROL_ANSWER:
    			strcpy(subclass, "Answer");
    			break;
    		case AST_CONTROL_BUSY:
    			strcpy(subclass, "Busy");
    			break;
    		case AST_CONTROL_TAKEOFFHOOK:
    			strcpy(subclass, "Take Off Hook");
    			break;
    		case AST_CONTROL_OFFHOOK:
    			strcpy(subclass, "Line Off Hook");
    			break;
    		case AST_CONTROL_CONGESTION:
    			strcpy(subclass, "Congestion");
    			break;
    		case AST_CONTROL_FLASH:
    			strcpy(subclass, "Flash");
    			break;
    		case AST_CONTROL_WINK:
    			strcpy(subclass, "Wink");
    			break;
    		case AST_CONTROL_OPTION:
    			strcpy(subclass, "Option");
    			break;
    		case AST_CONTROL_RADIO_KEY:
    			strcpy(subclass, "Key Radio");
    			break;
    		case AST_CONTROL_RADIO_UNKEY:
    			strcpy(subclass, "Unkey Radio");
    			break;
    
    		case AST_CONTROL_HOLD:
    			strcpy(subclass, "Hold");
    			break;
    		case AST_CONTROL_UNHOLD:
    			strcpy(subclass, "Unhold");
    			break;
    
    			if (f->datalen != sizeof(struct ast_control_t38_parameters)) {
    
    				message = "Invalid";
    			} else {
    				struct ast_control_t38_parameters *parameters = f->data.ptr;
    				enum ast_control_t38 state = parameters->request_response;
    				if (state == AST_T38_REQUEST_NEGOTIATE)
    					message = "Negotiation Requested";
    				else if (state == AST_T38_REQUEST_TERMINATE)
    					message = "Negotiation Request Terminated";
    				else if (state == AST_T38_NEGOTIATED)
    					message = "Negotiated";
    				else if (state == AST_T38_TERMINATED)
    					message = "Terminated";
    				else if (state == AST_T38_REFUSED)
    					message = "Refused";
    			}
    			snprintf(subclass, sizeof(subclass), "T38_Parameters/%s", message);
    			break;
    
    			snprintf(subclass, sizeof(subclass), "Unknown control '%d'", f->subclass.integer);
    
    	case AST_FRAME_NULL:
    		strcpy(ftype, "Null Frame");
    		strcpy(subclass, "N/A");
    		break;
    	case AST_FRAME_IAX:
    		/* Should never happen */
    		strcpy(ftype, "IAX Specific");
    
    		snprintf(subclass, sizeof(subclass), "IAX Frametype %d", f->subclass.integer);
    
    		break;
    	case AST_FRAME_TEXT:
    		strcpy(ftype, "Text");
    		strcpy(subclass, "N/A");
    
    		ast_copy_string(moreinfo, f->data.ptr, sizeof(moreinfo));
    
    		break;
    	case AST_FRAME_IMAGE:
    		strcpy(ftype, "Image");
    
    		snprintf(subclass, sizeof(subclass), "Image format %s\n", ast_getformatname(f->subclass.codec));
    
    		break;
    	case AST_FRAME_HTML:
    		strcpy(ftype, "HTML");
    
    		switch (f->subclass.integer) {
    
    		case AST_HTML_URL:
    			strcpy(subclass, "URL");
    
    			ast_copy_string(moreinfo, f->data.ptr, sizeof(moreinfo));
    
    			break;
    		case AST_HTML_DATA:
    			strcpy(subclass, "Data");
    			break;
    		case AST_HTML_BEGIN:
    			strcpy(subclass, "Begin");
    			break;
    		case AST_HTML_END:
    			strcpy(subclass, "End");
    			break;
    		case AST_HTML_LDCOMPLETE:
    			strcpy(subclass, "Load Complete");
    			break;
    		case AST_HTML_NOSUPPORT:
    			strcpy(subclass, "No Support");
    			break;
    		case AST_HTML_LINKURL:
    			strcpy(subclass, "Link URL");
    
    			ast_copy_string(moreinfo, f->data.ptr, sizeof(moreinfo));
    
    			break;
    		case AST_HTML_UNLINK:
    			strcpy(subclass, "Unlink");
    			break;
    		case AST_HTML_LINKREJECT:
    			strcpy(subclass, "Link Reject");
    			break;
    		default:
    
    			snprintf(subclass, sizeof(subclass), "Unknown HTML frame '%d'\n", f->subclass.integer);
    
    	case AST_FRAME_MODEM:
    		strcpy(ftype, "Modem");
    
    		switch (f->subclass.integer) {
    
    		case AST_MODEM_T38:
    			strcpy(subclass, "T.38");
    			break;
    		case AST_MODEM_V150:
    			strcpy(subclass, "V.150");
    			break;
    		default:
    
    			snprintf(subclass, sizeof(subclass), "Unknown MODEM frame '%d'\n", f->subclass.integer);
    
    	default:
    		snprintf(ftype, sizeof(ftype), "Unknown Frametype '%d'", f->frametype);
    	}
    
    	if (!ast_strlen_zero(moreinfo))
    
    		ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) '%s' ] [%s]\n",  
    
    			    term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
    			    term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
    			    f->frametype, 
    			    term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
    
    			    f->subclass.integer, 
    
    			    term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
    			    term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
    
    	else
    		ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) ] [%s]\n",  
    
    			    term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
    			    term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
    			    f->frametype, 
    			    term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
    
    			    f->subclass.integer, 
    
    			    term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
    
    /* Builtin Asterisk CLI-commands for debugging */
    
    static struct ast_cli_entry my_clis[] = {
    
    	AST_CLI_DEFINE(show_codecs, "Displays a list of codecs"),
    	AST_CLI_DEFINE(show_codec_n, "Shows a specific codec"),
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    int init_framer(void)
    {
    
    	ast_cli_register_multiple(my_clis, ARRAY_LEN(my_clis));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return 0;	
    }
    
    void ast_codec_pref_convert(struct ast_codec_pref *pref, char *buf, size_t size, int right) 
    
    	int x, differential = (int) 'A', mem;
    	char *from, *to;
    
    		from = pref->order;
    		to = buf;
    		mem = size;
    	} else {
    		to = pref->order;
    		from = buf;
    
    		mem = sizeof(format_t) * 8;
    
    	for (x = 0; x < sizeof(format_t) * 8; x++) {
    
    			break;
    		to[x] = right ? (from[x] + differential) : (from[x] - differential);
    	}