media formats: re-architect handling of media for performance improvements
[asterisk/asterisk.git] / main / frame.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Frame and codec manipulation routines
22  *
23  * \author Mark Spencer <markster@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/_private.h"
35 #include "asterisk/lock.h"
36 #include "asterisk/frame.h"
37 #include "asterisk/format_cache.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/cli.h"
40 #include "asterisk/term.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/threadstorage.h"
43 #include "asterisk/linkedlists.h"
44 #include "asterisk/translate.h"
45 #include "asterisk/dsp.h"
46 #include "asterisk/file.h"
47
48 #if !defined(LOW_MEMORY)
49 static void frame_cache_cleanup(void *data);
50
51 /*! \brief A per-thread cache of frame headers */
52 AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
53
54 /*!
55  * \brief Maximum ast_frame cache size
56  *
57  * In most cases where the frame header cache will be useful, the size
58  * of the cache will stay very small.  However, it is not always the case that
59  * the same thread that allocates the frame will be the one freeing them, so
60  * sometimes a thread will never have any frames in its cache, or the cache
61  * will never be pulled from.  For the latter case, we limit the maximum size.
62  */
63 #define FRAME_CACHE_MAX_SIZE    10
64
65 /*! \brief This is just so ast_frames, a list head struct for holding a list of
66  *  ast_frame structures, is defined. */
67 AST_LIST_HEAD_NOLOCK(ast_frames, ast_frame);
68
69 struct ast_frame_cache {
70         struct ast_frames list;
71         size_t size;
72 };
73 #endif
74
75 struct ast_frame ast_null_frame = { AST_FRAME_NULL, };
76
77 static struct ast_frame *ast_frame_header_new(void)
78 {
79         struct ast_frame *f;
80
81 #if !defined(LOW_MEMORY)
82         struct ast_frame_cache *frames;
83
84         if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
85                 if ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) {
86                         size_t mallocd_len = f->mallocd_hdr_len;
87                         memset(f, 0, sizeof(*f));
88                         f->mallocd_hdr_len = mallocd_len;
89                         f->mallocd = AST_MALLOCD_HDR;
90                         frames->size--;
91                         return f;
92                 }
93         }
94         if (!(f = ast_calloc_cache(1, sizeof(*f))))
95                 return NULL;
96 #else
97         if (!(f = ast_calloc(1, sizeof(*f))))
98                 return NULL;
99 #endif
100
101         f->mallocd_hdr_len = sizeof(*f);
102
103         return f;
104 }
105
106 #if !defined(LOW_MEMORY)
107 static void frame_cache_cleanup(void *data)
108 {
109         struct ast_frame_cache *frames = data;
110         struct ast_frame *f;
111
112         while ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list)))
113                 ast_free(f);
114
115         ast_free(frames);
116 }
117 #endif
118
119 static void __frame_free(struct ast_frame *fr, int cache)
120 {
121         if (!fr->mallocd)
122                 return;
123
124 #if !defined(LOW_MEMORY)
125         if (cache && fr->mallocd == AST_MALLOCD_HDR) {
126                 /* Cool, only the header is malloc'd, let's just cache those for now
127                  * to keep things simple... */
128                 struct ast_frame_cache *frames;
129                 if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames))) &&
130                     (frames->size < FRAME_CACHE_MAX_SIZE)) {
131                         if ((fr->frametype == AST_FRAME_VOICE) || (fr->frametype == AST_FRAME_VIDEO) ||
132                                 (fr->frametype == AST_FRAME_IMAGE)) {
133                                 ao2_cleanup(fr->subclass.format);
134                         }
135
136                         AST_LIST_INSERT_HEAD(&frames->list, fr, frame_list);
137                         frames->size++;
138                         return;
139                 }
140         }
141 #endif
142
143         if (fr->mallocd & AST_MALLOCD_DATA) {
144                 if (fr->data.ptr)
145                         ast_free(fr->data.ptr - fr->offset);
146         }
147         if (fr->mallocd & AST_MALLOCD_SRC) {
148                 if (fr->src)
149                         ast_free((void *) fr->src);
150         }
151         if (fr->mallocd & AST_MALLOCD_HDR) {
152                 if ((fr->frametype == AST_FRAME_VOICE) || (fr->frametype == AST_FRAME_VIDEO) ||
153                         (fr->frametype == AST_FRAME_IMAGE)) {
154                         ao2_cleanup(fr->subclass.format);
155                 }
156
157                 ast_free(fr);
158         } else {
159                 fr->mallocd = 0;
160         }
161 }
162
163
164 void ast_frame_free(struct ast_frame *frame, int cache)
165 {
166         struct ast_frame *next;
167
168         for (next = AST_LIST_NEXT(frame, frame_list);
169              frame;
170              frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) {
171                 __frame_free(frame, cache);
172         }
173 }
174
175 void ast_frame_dtor(struct ast_frame *f)
176 {
177         if (f) {
178                 ast_frfree(f);
179         }
180 }
181
182 /*!
183  * \brief 'isolates' a frame by duplicating non-malloc'ed components
184  * (header, src, data).
185  * On return all components are malloc'ed
186  */
187 struct ast_frame *ast_frisolate(struct ast_frame *fr)
188 {
189         struct ast_frame *out;
190         void *newdata;
191
192         /* if none of the existing frame is malloc'd, let ast_frdup() do it
193            since it is more efficient
194         */
195         if (fr->mallocd == 0) {
196                 return ast_frdup(fr);
197         }
198
199         /* if everything is already malloc'd, we are done */
200         if ((fr->mallocd & (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) ==
201             (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) {
202                 return fr;
203         }
204
205         if (!(fr->mallocd & AST_MALLOCD_HDR)) {
206                 /* Allocate a new header if needed */
207                 if (!(out = ast_frame_header_new())) {
208                         return NULL;
209                 }
210                 out->frametype = fr->frametype;
211                 if ((fr->frametype == AST_FRAME_VOICE) || (fr->frametype == AST_FRAME_VIDEO) ||
212                         (fr->frametype == AST_FRAME_IMAGE)) {
213                         out->subclass.format = ao2_bump(fr->subclass.format);
214                 } else {
215                         memcpy(&out->subclass, &fr->subclass, sizeof(out->subclass));
216                 }
217                 out->datalen = fr->datalen;
218                 out->samples = fr->samples;
219                 out->offset = fr->offset;
220                 /* Copy the timing data */
221                 ast_copy_flags(out, fr, AST_FLAGS_ALL);
222                 if (ast_test_flag(fr, AST_FRFLAG_HAS_TIMING_INFO)) {
223                         out->ts = fr->ts;
224                         out->len = fr->len;
225                         out->seqno = fr->seqno;
226                 }
227         } else {
228                 out = fr;
229         }
230
231         if (!(fr->mallocd & AST_MALLOCD_SRC) && fr->src) {
232                 if (!(out->src = ast_strdup(fr->src))) {
233                         if (out != fr) {
234                                 ast_free(out);
235                         }
236                         return NULL;
237                 }
238         } else {
239                 out->src = fr->src;
240                 fr->src = NULL;
241                 fr->mallocd &= ~AST_MALLOCD_SRC;
242         }
243
244         if (!(fr->mallocd & AST_MALLOCD_DATA))  {
245                 if (!fr->datalen) {
246                         out->data.uint32 = fr->data.uint32;
247                         out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC;
248                         return out;
249                 }
250                 if (!(newdata = ast_malloc(fr->datalen + AST_FRIENDLY_OFFSET))) {
251                         if (out->src != fr->src) {
252                                 ast_free((void *) out->src);
253                         }
254                         if (out != fr) {
255                                 ast_free(out);
256                         }
257                         return NULL;
258                 }
259                 newdata += AST_FRIENDLY_OFFSET;
260                 out->offset = AST_FRIENDLY_OFFSET;
261                 out->datalen = fr->datalen;
262                 memcpy(newdata, fr->data.ptr, fr->datalen);
263                 out->data.ptr = newdata;
264         } else {
265                 out->data = fr->data;
266                 memset(&fr->data, 0, sizeof(fr->data));
267                 fr->mallocd &= ~AST_MALLOCD_DATA;
268         }
269
270         out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
271
272         return out;
273 }
274
275 struct ast_frame *ast_frdup(const struct ast_frame *f)
276 {
277         struct ast_frame *out = NULL;
278         int len, srclen = 0;
279         void *buf = NULL;
280
281 #if !defined(LOW_MEMORY)
282         struct ast_frame_cache *frames;
283 #endif
284
285         /* Start with standard stuff */
286         len = sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
287         /* If we have a source, add space for it */
288         /*
289          * XXX Watch out here - if we receive a src which is not terminated
290          * properly, we can be easily attacked. Should limit the size we deal with.
291          */
292         if (f->src)
293                 srclen = strlen(f->src);
294         if (srclen > 0)
295                 len += srclen + 1;
296
297 #if !defined(LOW_MEMORY)
298         if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
299                 AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) {
300                         if (out->mallocd_hdr_len >= len) {
301                                 size_t mallocd_len = out->mallocd_hdr_len;
302
303                                 AST_LIST_REMOVE_CURRENT(frame_list);
304                                 memset(out, 0, sizeof(*out));
305                                 out->mallocd_hdr_len = mallocd_len;
306                                 buf = out;
307                                 frames->size--;
308                                 break;
309                         }
310                 }
311                 AST_LIST_TRAVERSE_SAFE_END;
312         }
313 #endif
314
315         if (!buf) {
316                 if (!(buf = ast_calloc_cache(1, len)))
317                         return NULL;
318                 out = buf;
319                 out->mallocd_hdr_len = len;
320         }
321
322         out->frametype = f->frametype;
323         if ((f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_VIDEO) ||
324                 (f->frametype == AST_FRAME_IMAGE)) {
325                 out->subclass.format = ao2_bump(f->subclass.format);
326         } else {
327                 memcpy(&out->subclass, &f->subclass, sizeof(out->subclass));
328         }
329         out->datalen = f->datalen;
330         out->samples = f->samples;
331         out->delivery = f->delivery;
332         /* Even though this new frame was allocated from the heap, we can't mark it
333          * with AST_MALLOCD_HDR, AST_MALLOCD_DATA and AST_MALLOCD_SRC, because that
334          * would cause ast_frfree() to attempt to individually free each of those
335          * under the assumption that they were separately allocated. Since this frame
336          * was allocated in a single allocation, we'll only mark it as if the header
337          * was heap-allocated; this will result in the entire frame being properly freed.
338          */
339         out->mallocd = AST_MALLOCD_HDR;
340         out->offset = AST_FRIENDLY_OFFSET;
341         if (out->datalen) {
342                 out->data.ptr = buf + sizeof(*out) + AST_FRIENDLY_OFFSET;
343                 memcpy(out->data.ptr, f->data.ptr, out->datalen);
344         } else {
345                 out->data.uint32 = f->data.uint32;
346         }
347         if (srclen > 0) {
348                 /* This may seem a little strange, but it's to avoid a gcc (4.2.4) compiler warning */
349                 char *src;
350                 out->src = buf + sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
351                 src = (char *) out->src;
352                 /* Must have space since we allocated for it */
353                 strcpy(src, f->src);
354         }
355         ast_copy_flags(out, f, AST_FLAGS_ALL);
356         out->ts = f->ts;
357         out->len = f->len;
358         out->seqno = f->seqno;
359         return out;
360 }
361
362 void ast_swapcopy_samples(void *dst, const void *src, int samples)
363 {
364         int i;
365         unsigned short *dst_s = dst;
366         const unsigned short *src_s = src;
367
368         for (i = 0; i < samples; i++)
369                 dst_s[i] = (src_s[i]<<8) | (src_s[i]>>8);
370 }
371
372 void ast_frame_subclass2str(struct ast_frame *f, char *subclass, size_t slen, char *moreinfo, size_t mlen)
373 {
374         switch(f->frametype) {
375         case AST_FRAME_DTMF_BEGIN:
376                 if (slen > 1) {
377                         subclass[0] = f->subclass.integer;
378                         subclass[1] = '\0';
379                 }
380                 break;
381         case AST_FRAME_DTMF_END:
382                 if (slen > 1) {
383                         subclass[0] = f->subclass.integer;
384                         subclass[1] = '\0';
385                 }
386                 break;
387         case AST_FRAME_CONTROL:
388                 switch (f->subclass.integer) {
389                 case AST_CONTROL_HANGUP:
390                         ast_copy_string(subclass, "Hangup", slen);
391                         break;
392                 case AST_CONTROL_RING:
393                         ast_copy_string(subclass, "Ring", slen);
394                         break;
395                 case AST_CONTROL_RINGING:
396                         ast_copy_string(subclass, "Ringing", slen);
397                         break;
398                 case AST_CONTROL_ANSWER:
399                         ast_copy_string(subclass, "Answer", slen);
400                         break;
401                 case AST_CONTROL_BUSY:
402                         ast_copy_string(subclass, "Busy", slen);
403                         break;
404                 case AST_CONTROL_TAKEOFFHOOK:
405                         ast_copy_string(subclass, "Take Off Hook", slen);
406                         break;
407                 case AST_CONTROL_OFFHOOK:
408                         ast_copy_string(subclass, "Line Off Hook", slen);
409                         break;
410                 case AST_CONTROL_CONGESTION:
411                         ast_copy_string(subclass, "Congestion", slen);
412                         break;
413                 case AST_CONTROL_FLASH:
414                         ast_copy_string(subclass, "Flash", slen);
415                         break;
416                 case AST_CONTROL_WINK:
417                         ast_copy_string(subclass, "Wink", slen);
418                         break;
419                 case AST_CONTROL_OPTION:
420                         ast_copy_string(subclass, "Option", slen);
421                         break;
422                 case AST_CONTROL_RADIO_KEY:
423                         ast_copy_string(subclass, "Key Radio", slen);
424                         break;
425                 case AST_CONTROL_RADIO_UNKEY:
426                         ast_copy_string(subclass, "Unkey Radio", slen);
427                         break;
428                 case AST_CONTROL_HOLD:
429                         ast_copy_string(subclass, "Hold", slen);
430                         break;
431                 case AST_CONTROL_UNHOLD:
432                         ast_copy_string(subclass, "Unhold", slen);
433                         break;
434                 case AST_CONTROL_T38_PARAMETERS: {
435                         char *message = "Unknown";
436                         if (f->datalen != sizeof(struct ast_control_t38_parameters)) {
437                                 message = "Invalid";
438                         } else {
439                                 struct ast_control_t38_parameters *parameters = f->data.ptr;
440                                 enum ast_control_t38 state = parameters->request_response;
441                                 if (state == AST_T38_REQUEST_NEGOTIATE)
442                                         message = "Negotiation Requested";
443                                 else if (state == AST_T38_REQUEST_TERMINATE)
444                                         message = "Negotiation Request Terminated";
445                                 else if (state == AST_T38_NEGOTIATED)
446                                         message = "Negotiated";
447                                 else if (state == AST_T38_TERMINATED)
448                                         message = "Terminated";
449                                 else if (state == AST_T38_REFUSED)
450                                         message = "Refused";
451                         }
452                         snprintf(subclass, slen, "T38_Parameters/%s", message);
453                         break;
454                 }
455                 case -1:
456                         ast_copy_string(subclass, "Stop generators", slen);
457                         break;
458                 default:
459                         snprintf(subclass, slen, "Unknown control '%d'", f->subclass.integer);
460                 }
461                 break;
462         case AST_FRAME_NULL:
463                 ast_copy_string(subclass, "N/A", slen);
464                 break;
465         case AST_FRAME_IAX:
466                 /* Should never happen */
467                 snprintf(subclass, slen, "IAX Frametype %d", f->subclass.integer);
468                 break;
469         case AST_FRAME_BRIDGE_ACTION:
470                 /* Should never happen */
471                 snprintf(subclass, slen, "Bridge Frametype %d", f->subclass.integer);
472                 break;
473         case AST_FRAME_BRIDGE_ACTION_SYNC:
474                 /* Should never happen */
475                 snprintf(subclass, slen, "Synchronous Bridge Frametype %d", f->subclass.integer);
476                 break;
477         case AST_FRAME_TEXT:
478                 ast_copy_string(subclass, "N/A", slen);
479                 if (moreinfo) {
480                         ast_copy_string(moreinfo, f->data.ptr, mlen);
481                 }
482                 break;
483         case AST_FRAME_IMAGE:
484                 snprintf(subclass, slen, "Image format %s\n", ast_format_get_name(f->subclass.format));
485                 break;
486         case AST_FRAME_HTML:
487                 switch (f->subclass.integer) {
488                 case AST_HTML_URL:
489                         ast_copy_string(subclass, "URL", slen);
490                         if (moreinfo) {
491                                 ast_copy_string(moreinfo, f->data.ptr, mlen);
492                         }
493                         break;
494                 case AST_HTML_DATA:
495                         ast_copy_string(subclass, "Data", slen);
496                         break;
497                 case AST_HTML_BEGIN:
498                         ast_copy_string(subclass, "Begin", slen);
499                         break;
500                 case AST_HTML_END:
501                         ast_copy_string(subclass, "End", slen);
502                         break;
503                 case AST_HTML_LDCOMPLETE:
504                         ast_copy_string(subclass, "Load Complete", slen);
505                         break;
506                 case AST_HTML_NOSUPPORT:
507                         ast_copy_string(subclass, "No Support", slen);
508                         break;
509                 case AST_HTML_LINKURL:
510                         ast_copy_string(subclass, "Link URL", slen);
511                         if (moreinfo) {
512                                 ast_copy_string(moreinfo, f->data.ptr, mlen);
513                         }
514                         break;
515                 case AST_HTML_UNLINK:
516                         ast_copy_string(subclass, "Unlink", slen);
517                         break;
518                 case AST_HTML_LINKREJECT:
519                         ast_copy_string(subclass, "Link Reject", slen);
520                         break;
521                 default:
522                         snprintf(subclass, slen, "Unknown HTML frame '%d'\n", f->subclass.integer);
523                         break;
524                 }
525                 break;
526         case AST_FRAME_MODEM:
527                 switch (f->subclass.integer) {
528                 case AST_MODEM_T38:
529                         ast_copy_string(subclass, "T.38", slen);
530                         break;
531                 case AST_MODEM_V150:
532                         ast_copy_string(subclass, "V.150", slen);
533                         break;
534                 default:
535                         snprintf(subclass, slen, "Unknown MODEM frame '%d'\n", f->subclass.integer);
536                         break;
537                 }
538                 break;
539         default:
540                 ast_copy_string(subclass, "Unknown Subclass", slen);
541                 break;
542         }
543 }
544
545 void ast_frame_type2str(enum ast_frame_type frame_type, char *ftype, size_t len)
546 {
547         switch (frame_type) {
548         case AST_FRAME_DTMF_BEGIN:
549                 ast_copy_string(ftype, "DTMF Begin", len);
550                 break;
551         case AST_FRAME_DTMF_END:
552                 ast_copy_string(ftype, "DTMF End", len);
553                 break;
554         case AST_FRAME_CONTROL:
555                 ast_copy_string(ftype, "Control", len);
556                 break;
557         case AST_FRAME_NULL:
558                 ast_copy_string(ftype, "Null Frame", len);
559                 break;
560         case AST_FRAME_IAX:
561                 /* Should never happen */
562                 ast_copy_string(ftype, "IAX Specific", len);
563                 break;
564         case AST_FRAME_BRIDGE_ACTION:
565                 /* Should never happen */
566                 ast_copy_string(ftype, "Bridge Specific", len);
567                 break;
568         case AST_FRAME_BRIDGE_ACTION_SYNC:
569                 /* Should never happen */
570                 ast_copy_string(ftype, "Bridge Specific", len);
571                 break;
572         case AST_FRAME_TEXT:
573                 ast_copy_string(ftype, "Text", len);
574                 break;
575         case AST_FRAME_IMAGE:
576                 ast_copy_string(ftype, "Image", len);
577                 break;
578         case AST_FRAME_HTML:
579                 ast_copy_string(ftype, "HTML", len);
580                 break;
581         case AST_FRAME_MODEM:
582                 ast_copy_string(ftype, "Modem", len);
583                 break;
584         case AST_FRAME_VOICE:
585                 ast_copy_string(ftype, "Voice", len);
586                 break;
587         case AST_FRAME_VIDEO:
588                 ast_copy_string(ftype, "Video", len);
589                 break;
590         default:
591                 snprintf(ftype, len, "Unknown Frametype '%u'", frame_type);
592                 break;
593         }
594 }
595
596 /*! Dump a frame for debugging purposes */
597 void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix)
598 {
599         const char noname[] = "unknown";
600         char ftype[40] = "Unknown Frametype";
601         char cft[80];
602         char subclass[40] = "Unknown Subclass";
603         char csub[80];
604         char moreinfo[40] = "";
605         char cn[60];
606         char cp[40];
607         char cmn[40];
608
609         if (!name) {
610                 name = noname;
611         }
612
613         if (!f) {
614                 ast_verb(-1, "%s [ %s (NULL) ] [%s]\n",
615                         term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
616                         term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
617                         term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
618                 return;
619         }
620         /* XXX We should probably print one each of voice and video when the format changes XXX */
621         if (f->frametype == AST_FRAME_VOICE) {
622                 return;
623         }
624         if (f->frametype == AST_FRAME_VIDEO) {
625                 return;
626         }
627
628         ast_frame_type2str(f->frametype, ftype, sizeof(ftype));
629         ast_frame_subclass2str(f, subclass, sizeof(subclass), moreinfo, sizeof(moreinfo));
630
631         if (!ast_strlen_zero(moreinfo))
632                 ast_verb(-1, "%s [ TYPE: %s (%u) SUBCLASS: %s (%d) '%s' ] [%s]\n",
633                             term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
634                             term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
635                             f->frametype,
636                             term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
637                             f->subclass.integer,
638                             term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
639                             term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
640         else
641                 ast_verb(-1, "%s [ TYPE: %s (%u) SUBCLASS: %s (%d) ] [%s]\n",
642                             term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
643                             term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
644                             f->frametype,
645                             term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
646                             f->subclass.integer,
647                             term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
648 }
649
650 int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
651 {
652         int count;
653         short *fdata = f->data.ptr;
654         short adjust_value = abs(adjustment);
655
656         if ((f->frametype != AST_FRAME_VOICE) || !(ast_format_cache_is_slinear(f->subclass.format))) {
657                 return -1;
658         }
659
660         if (!adjustment) {
661                 return 0;
662         }
663
664         for (count = 0; count < f->samples; count++) {
665                 if (adjustment > 0) {
666                         ast_slinear_saturated_multiply(&fdata[count], &adjust_value);
667                 } else if (adjustment < 0) {
668                         ast_slinear_saturated_divide(&fdata[count], &adjust_value);
669                 }
670         }
671
672         return 0;
673 }
674
675 int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2)
676 {
677         int count;
678         short *data1, *data2;
679
680         if ((f1->frametype != AST_FRAME_VOICE) || (ast_format_cmp(f1->subclass.format, ast_format_slin) != AST_FORMAT_CMP_NOT_EQUAL))
681                 return -1;
682
683         if ((f2->frametype != AST_FRAME_VOICE) || (ast_format_cmp(f2->subclass.format, ast_format_slin) != AST_FORMAT_CMP_NOT_EQUAL))
684                 return -1;
685
686         if (f1->samples != f2->samples)
687                 return -1;
688
689         for (count = 0, data1 = f1->data.ptr, data2 = f2->data.ptr;
690              count < f1->samples;
691              count++, data1++, data2++)
692                 ast_slinear_saturated_add(data1, data2);
693
694         return 0;
695 }
696
697 int ast_frame_clear(struct ast_frame *frame)
698 {
699         struct ast_frame *next;
700
701         for (next = AST_LIST_NEXT(frame, frame_list);
702                  frame;
703                  frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) {
704                 memset(frame->data.ptr, 0, frame->datalen);
705         }
706         return 0;
707 }