2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, 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 Translate via the use of pseudo channels
23 * \author Mark Spencer <markster@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include <sys/resource.h>
33 #include "asterisk/lock.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/translate.h"
36 #include "asterisk/module.h"
37 #include "asterisk/frame.h"
38 #include "asterisk/sched.h"
39 #include "asterisk/cli.h"
40 #include "asterisk/term.h"
42 #define MAX_RECALC 1000 /* max sample recalc */
44 /*! \brief the list of translators */
45 static AST_RWLIST_HEAD_STATIC(translators, ast_translator);
47 struct translator_path {
48 struct ast_translator *step; /*!< Next step translator */
49 unsigned int cost; /*!< Complete cost to destination */
50 unsigned int multistep; /*!< Multiple conversions required for this translation */
53 /*! \brief a matrix that, for any pair of supported formats,
54 * indicates the total cost of translation and the first step.
55 * The full path can be reconstricted iterating on the matrix
56 * until step->dstfmt == desired_format.
58 * Array indexes are 'src' and 'dest', in that order.
60 * Note: the lock in the 'translators' list is also used to protect
63 static struct translator_path tr_matrix[MAX_FORMAT][MAX_FORMAT];
66 * TODO: sample frames for each supported input format.
67 * We build this on the fly, by taking an SLIN frame and using
68 * the existing converter to play with it.
71 /*! \brief returns the index of the lowest bit set */
72 static force_inline int powerof(unsigned int d)
79 ast_log(LOG_WARNING, "No bits set? %d\n", d);
85 * wrappers around the translator routines.
89 * \brief Allocate the descriptor, required outbuf space,
90 * and possibly also plc and desc.
92 static void *newpvt(struct ast_translator *t)
94 struct ast_trans_pvt *pvt;
96 int useplc = t->plc_samples > 0 && t->useplc; /* cache, because it can change on the fly */
100 * compute the required size adding private descriptor,
101 * plc, buffer, AST_FRIENDLY_OFFSET.
103 len = sizeof(*pvt) + t->desc_size;
105 len += sizeof(plc_state_t);
107 len += AST_FRIENDLY_OFFSET + t->buf_size;
108 pvt = ast_calloc(1, len);
112 ofs = (char *)(pvt + 1); /* pointer to data space */
113 if (t->desc_size) { /* first comes the descriptor */
117 if (useplc) { /* then plc state */
118 pvt->plc = (plc_state_t *)ofs;
119 ofs += sizeof(plc_state_t);
121 if (t->buf_size) /* finally buffer and header */
122 pvt->outbuf = ofs + AST_FRIENDLY_OFFSET;
123 /* call local init routine, if present */
124 if (t->newpvt && t->newpvt(pvt)) {
128 ast_module_ref(t->module);
132 static void destroy(struct ast_trans_pvt *pvt)
134 struct ast_translator *t = pvt->t;
139 ast_module_unref(t->module);
142 /*! \brief framein wrapper, deals with plc and bound checks. */
143 static int framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
145 int16_t *dst = (int16_t *)pvt->outbuf;
147 int samples = pvt->samples; /* initial value */
149 /* Copy the last in jb timing info to the pvt */
150 pvt->f.has_timing_info = f->has_timing_info;
153 pvt->f.seqno = f->seqno;
155 if (f->samples == 0) {
156 ast_log(LOG_WARNING, "no samples for %s\n", pvt->t->name);
158 if (pvt->t->buffer_samples) { /* do not pass empty frames to callback */
159 if (f->datalen == 0) { /* perform PLC with nominal framesize of 20ms/160 samples */
161 int l = pvt->t->plc_samples;
162 if (pvt->samples + l > pvt->t->buffer_samples) {
163 ast_log(LOG_WARNING, "Out of buffer space\n");
166 l = plc_fillin(pvt->plc, dst + pvt->samples, l);
168 pvt->datalen = pvt->samples * 2; /* SLIN has 2bytes for 1sample */
170 /* We don't want generic PLC. If the codec has native PLC, then do that */
171 if (!pvt->t->native_plc)
174 if (pvt->samples + f->samples > pvt->t->buffer_samples) {
175 ast_log(LOG_WARNING, "Out of buffer space\n");
179 /* we require a framein routine, wouldn't know how to do
182 ret = pvt->t->framein(pvt, f);
183 /* possibly store data for plc */
184 if (!ret && pvt->plc) {
185 int l = pvt->t->plc_samples;
186 if (pvt->samples < l)
188 plc_rx(pvt->plc, dst + pvt->samples - l, l);
191 if (pvt->samples == samples)
192 ast_log(LOG_WARNING, "%s did not update samples %d\n",
193 pvt->t->name, pvt->samples);
197 /*! \brief generic frameout routine.
198 * If samples and datalen are 0, take whatever is in pvt
199 * and reset them, otherwise take the values in the caller and
200 * leave alone the pvt values.
202 struct ast_frame *ast_trans_frameout(struct ast_trans_pvt *pvt,
203 int datalen, int samples)
205 struct ast_frame *f = &pvt->f;
208 f->samples = samples;
210 if (pvt->samples == 0)
212 f->samples = pvt->samples;
216 f->datalen = datalen;
218 f->datalen = pvt->datalen;
222 f->frametype = AST_FRAME_VOICE;
223 f->subclass = 1 << (pvt->t->dstfmt);
225 f->offset = AST_FRIENDLY_OFFSET;
226 f->src = pvt->t->name;
227 f->data = pvt->outbuf;
231 static struct ast_frame *default_frameout(struct ast_trans_pvt *pvt)
233 return ast_trans_frameout(pvt, 0, 0);
236 /* end of callback wrappers and helpers */
238 void ast_translator_free_path(struct ast_trans_pvt *p)
240 struct ast_trans_pvt *pn = p;
247 /*! \brief Build a chain of translators based upon the given source and dest formats */
248 struct ast_trans_pvt *ast_translator_build_path(int dest, int source)
250 struct ast_trans_pvt *head = NULL, *tail = NULL;
252 source = powerof(source);
253 dest = powerof(dest);
255 AST_RWLIST_RDLOCK(&translators);
257 while (source != dest) {
258 struct ast_trans_pvt *cur;
259 struct ast_translator *t = tr_matrix[source][dest].step;
261 ast_log(LOG_WARNING, "No translator path from %s to %s\n",
262 ast_getformatname(source), ast_getformatname(dest));
263 AST_RWLIST_UNLOCK(&translators);
266 if (!(cur = newpvt(t))) {
267 ast_log(LOG_WARNING, "Failed to build translator step from %d to %d\n", source, dest);
269 ast_translator_free_path(head);
270 AST_RWLIST_UNLOCK(&translators);
278 cur->nextin = cur->nextout = ast_tv(0, 0);
279 /* Keep going if this isn't the final destination */
280 source = cur->t->dstfmt;
283 AST_RWLIST_UNLOCK(&translators);
287 /*! \brief do the actual translation */
288 struct ast_frame *ast_translate(struct ast_trans_pvt *path, struct ast_frame *f, int consume)
290 struct ast_trans_pvt *p = path;
291 struct ast_frame *out = f;
292 struct timeval delivery;
298 has_timing_info = f->has_timing_info;
303 /* XXX hmmm... check this below */
304 if (!ast_tvzero(f->delivery)) {
305 if (!ast_tvzero(path->nextin)) {
306 /* Make sure this is in line with what we were expecting */
307 if (!ast_tveq(path->nextin, f->delivery)) {
308 /* The time has changed between what we expected and this
309 most recent time on the new packet. If we have a
310 valid prediction adjust our output time appropriately */
311 if (!ast_tvzero(path->nextout)) {
312 path->nextout = ast_tvadd(path->nextout,
313 ast_tvsub(f->delivery, path->nextin));
315 path->nextin = f->delivery;
318 /* This is our first pass. Make sure the timing looks good */
319 path->nextin = f->delivery;
320 path->nextout = f->delivery;
322 /* Predict next incoming sample */
323 path->nextin = ast_tvadd(path->nextin, ast_samp2tv(f->samples, 8000));
325 delivery = f->delivery;
326 for ( ; out && p ; p = p->next) {
328 out = p->t->frameout(p);
334 /* we have a frame, play with times */
335 if (!ast_tvzero(delivery)) {
336 /* Regenerate prediction after a discontinuity */
337 if (ast_tvzero(path->nextout))
338 path->nextout = ast_tvnow();
340 /* Use next predicted outgoing timestamp */
341 out->delivery = path->nextout;
343 /* Predict next outgoing timestamp from samples in this
345 path->nextout = ast_tvadd(path->nextout, ast_samp2tv( out->samples, 8000));
347 out->delivery = ast_tv(0, 0);
348 out->has_timing_info = has_timing_info;
349 if (has_timing_info) {
355 /* Invalidate prediction if we're entering a silence period */
356 if (out->frametype == AST_FRAME_CNG)
357 path->nextout = ast_tv(0, 0);
361 /*! \brief compute the cost of a single translation step */
362 static void calc_cost(struct ast_translator *t, int seconds)
365 struct ast_trans_pvt *pvt;
373 /* If they don't make samples, give them a terrible score */
375 ast_log(LOG_WARNING, "Translator '%s' does not produce sample frames.\n", t->name);
381 ast_log(LOG_WARNING, "Translator '%s' appears to be broken and will probably fail.\n", t->name);
385 getrusage(RUSAGE_SELF, &start);
386 /* Call the encoder until we've processed the required number of samples */
387 while (sofar < seconds * 8000) {
388 struct ast_frame *f = t->sample();
390 ast_log(LOG_WARNING, "Translator '%s' failed to produce a sample frame.\n", t->name);
397 while ((f = t->frameout(pvt))) {
402 getrusage(RUSAGE_SELF, &end);
403 cost = ((end.ru_utime.tv_sec - start.ru_utime.tv_sec)*1000000) + end.ru_utime.tv_usec - start.ru_utime.tv_usec;
404 cost += ((end.ru_stime.tv_sec - start.ru_stime.tv_sec)*1000000) + end.ru_stime.tv_usec - start.ru_stime.tv_usec;
406 t->cost = cost / seconds;
412 * \brief rebuild a translation matrix.
413 * \note This function expects the list of translators to be locked
415 static void rebuild_matrix(int samples)
417 struct ast_translator *t;
418 int x; /* source format index */
419 int y; /* intermediate format index */
420 int z; /* destination format index */
422 ast_debug(1, "Resetting translation matrix\n");
424 bzero(tr_matrix, sizeof(tr_matrix));
426 /* first, compute all direct costs */
427 AST_RWLIST_TRAVERSE(&translators, t, list) {
435 calc_cost(t, samples);
437 if (!tr_matrix[x][z].step || t->cost < tr_matrix[x][z].cost) {
438 tr_matrix[x][z].step = t;
439 tr_matrix[x][z].cost = t->cost;
444 * For each triple x, y, z of distinct formats, check if there is
445 * a path from x to z through y which is cheaper than what is
446 * currently known, and in case, update the matrix.
447 * Repeat until the matrix is stable.
451 for (x = 0; x < MAX_FORMAT; x++) { /* source format */
452 for (y=0; y < MAX_FORMAT; y++) { /* intermediate format */
453 if (x == y) /* skip ourselves */
456 for (z=0; z<MAX_FORMAT; z++) { /* dst format */
459 if (z == x || z == y) /* skip null conversions */
461 if (!tr_matrix[x][y].step) /* no path from x to y */
463 if (!tr_matrix[y][z].step) /* no path from y to z */
465 newcost = tr_matrix[x][y].cost + tr_matrix[y][z].cost;
466 if (tr_matrix[x][z].step && newcost >= tr_matrix[x][z].cost)
467 continue; /* x->y->z is more expensive than
468 * the existing path */
469 /* ok, we can get from x to z via y with a cost that
470 is the sum of the transition from x to y and
473 tr_matrix[x][z].step = tr_matrix[x][y].step;
474 tr_matrix[x][z].cost = newcost;
475 tr_matrix[x][z].multistep = 1;
476 ast_debug(3, "Discovered %d cost path from %s to %s, via %d\n", tr_matrix[x][z].cost, ast_getformatname(x), ast_getformatname(z), y);
486 static char *handle_cli_core_show_translation(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
488 #define SHOW_TRANS 16
490 int curlen = 0, longest = 0;
494 e->command = "core show translation [recalc]";
496 "Usage: core show translation [recalc [<recalc seconds>]]\n"
497 " Displays known codec translators and the cost associated\n"
498 " with each conversion. If the argument 'recalc' is supplied along\n"
499 " with optional number of seconds to test a new test will be performed\n"
500 " as the chart is being displayed.\n";
507 return CLI_SHOWUSAGE;
509 if (a->argv[3] && !strcasecmp(a->argv[3], "recalc")) {
510 z = a->argv[4] ? atoi(a->argv[4]) : 1;
513 ast_cli(a->fd, " Recalc must be greater than 0. Defaulting to 1.\n");
517 if (z > MAX_RECALC) {
518 ast_cli(a->fd, " Maximum limit of recalc exceeded by %d, truncating value to %d\n", z - MAX_RECALC, MAX_RECALC);
521 ast_cli(a->fd, " Recalculating Codec Translation (number of sample seconds: %d)\n\n", z);
522 AST_RWLIST_WRLOCK(&translators);
524 AST_RWLIST_UNLOCK(&translators);
525 } else if (a->argc > 3)
526 return CLI_SHOWUSAGE;
528 AST_RWLIST_RDLOCK(&translators);
530 ast_cli(a->fd, " Translation times between formats (in microseconds) for one second of data\n");
531 ast_cli(a->fd, " Source Format (Rows) Destination Format (Columns)\n\n");
532 /* Get the length of the longest (usable?) codec name, so we know how wide the left side should be */
533 for (x = 0; x < SHOW_TRANS; x++) {
534 curlen = strlen(ast_getformatname(1 << (x)));
535 if (curlen > longest)
538 for (x = -1; x < SHOW_TRANS; x++) {
539 struct ast_str *out = ast_str_alloca(120);
541 ast_str_set(&out, -1, " ");
542 for (y = -1; y < SHOW_TRANS; y++) {
544 curlen = strlen(ast_getformatname(1 << (y)));
547 if (x >= 0 && y >= 0 && tr_matrix[x][y].step) {
548 /* XXX 99999 is a little hackish
549 We don't want this number being larger than the shortest (or current) codec
550 For now, that is "gsm" */
551 ast_str_append(&out, -1, "%*d", curlen + 1, tr_matrix[x][y].cost > 99999 ? 0 : tr_matrix[x][y].cost);
552 } else if (x == -1 && y >= 0) {
553 /* Top row - use a dynamic size */
554 ast_str_append(&out, -1, "%*s", curlen + 1, ast_getformatname(1 << (y)) );
555 } else if (y == -1 && x >= 0) {
556 /* Left column - use a static size. */
557 ast_str_append(&out, -1, "%*s", longest, ast_getformatname(1 << (x)) );
558 } else if (x >= 0 && y >= 0) {
559 ast_str_append(&out, -1, "%*s", curlen + 1, "-");
561 ast_str_append(&out, -1, "%*s", longest, "");
564 ast_str_append(&out, -1, "\n");
565 ast_cli(a->fd, out->str);
567 AST_RWLIST_UNLOCK(&translators);
571 static struct ast_cli_entry cli_translate[] = {
572 AST_CLI_DEFINE(handle_cli_core_show_translation, "Display translation matrix")
575 /*! \brief register codec translator */
576 int __ast_register_translator(struct ast_translator *t, struct ast_module *mod)
578 static int added_cli = 0;
579 struct ast_translator *u;
583 ast_log(LOG_WARNING, "Missing module pointer, you need to supply one\n");
588 ast_log(LOG_WARNING, "empty buf size, you need to supply one\n");
594 t->srcfmt = powerof(t->srcfmt);
595 t->dstfmt = powerof(t->dstfmt);
598 if (t->plc_samples) {
599 if (t->buffer_samples < t->plc_samples) {
600 ast_log(LOG_WARNING, "plc_samples %d buffer_samples %d\n",
601 t->plc_samples, t->buffer_samples);
604 if (t->dstfmt != powerof(AST_FORMAT_SLINEAR))
605 ast_log(LOG_WARNING, "plc_samples %d format %x\n",
606 t->plc_samples, t->dstfmt);
608 if (t->srcfmt >= MAX_FORMAT) {
609 ast_log(LOG_WARNING, "Source format %s is larger than MAX_FORMAT\n", ast_getformatname(t->srcfmt));
613 if (t->dstfmt >= MAX_FORMAT) {
614 ast_log(LOG_WARNING, "Destination format %s is larger than MAX_FORMAT\n", ast_getformatname(t->dstfmt));
620 * Align buf_size properly, rounding up to the machine-specific
621 * alignment for pointers.
623 struct _test_align { void *a, *b; } p;
624 int align = (char *)&p.b - (char *)&p.a;
626 t->buf_size = ((t->buf_size + align - 1) / align) * align;
629 if (t->frameout == NULL)
630 t->frameout = default_frameout;
634 ast_verb(2, "Registered translator '%s' from format %s to %s, cost %d\n",
635 term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)),
636 ast_getformatname(1 << t->srcfmt), ast_getformatname(1 << t->dstfmt), t->cost);
639 ast_cli_register_multiple(cli_translate, sizeof(cli_translate) / sizeof(struct ast_cli_entry));
643 AST_RWLIST_WRLOCK(&translators);
645 /* find any existing translators that provide this same srcfmt/dstfmt,
646 and put this one in order based on cost */
647 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&translators, u, list) {
648 if ((u->srcfmt == t->srcfmt) &&
649 (u->dstfmt == t->dstfmt) &&
650 (u->cost > t->cost)) {
651 AST_RWLIST_INSERT_BEFORE_CURRENT(t, list);
655 AST_RWLIST_TRAVERSE_SAFE_END;
657 /* if no existing translator was found for this format combination,
658 add it to the beginning of the list */
660 AST_RWLIST_INSERT_HEAD(&translators, t, list);
664 AST_RWLIST_UNLOCK(&translators);
669 /*! \brief unregister codec translator */
670 int ast_unregister_translator(struct ast_translator *t)
673 struct ast_translator *u;
676 AST_RWLIST_WRLOCK(&translators);
677 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&translators, u, list) {
679 AST_RWLIST_REMOVE_CURRENT(list);
680 ast_verb(2, "Unregistered translator '%s' from format %s to %s\n", term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)), ast_getformatname(1 << t->srcfmt), ast_getformatname(1 << t->dstfmt));
685 AST_RWLIST_TRAVERSE_SAFE_END;
690 AST_RWLIST_UNLOCK(&translators);
695 void ast_translator_activate(struct ast_translator *t)
697 AST_RWLIST_WRLOCK(&translators);
700 AST_RWLIST_UNLOCK(&translators);
703 void ast_translator_deactivate(struct ast_translator *t)
705 AST_RWLIST_WRLOCK(&translators);
708 AST_RWLIST_UNLOCK(&translators);
711 /*! \brief Calculate our best translator source format, given costs, and a desired destination */
712 int ast_translator_best_choice(int *dst, int *srcs)
718 int besttime = INT_MAX;
719 int beststeps = INT_MAX;
720 int common = ((*dst) & (*srcs)) & AST_FORMAT_AUDIO_MASK; /* are there common formats ? */
722 if (common) { /* yes, pick one and return */
723 for (cur = 1, y = 0; y <= MAX_AUDIO_FORMAT; cur <<= 1, y++) {
724 if (cur & common) /* guaranteed to find one */
727 /* We are done, this is a common format to both. */
730 } else { /* No, we will need to translate */
731 AST_RWLIST_RDLOCK(&translators);
732 for (cur = 1, y = 0; y <= MAX_AUDIO_FORMAT; cur <<= 1, y++) {
735 for (cursrc = 1, x = 0; x <= MAX_AUDIO_FORMAT; cursrc <<= 1, x++) {
736 if (!(*srcs & cursrc) || !tr_matrix[x][y].step ||
737 tr_matrix[x][y].cost > besttime)
738 continue; /* not existing or no better */
739 if (tr_matrix[x][y].cost < besttime ||
740 tr_matrix[x][y].multistep < beststeps) {
741 /* better than what we have so far */
744 besttime = tr_matrix[x][y].cost;
745 beststeps = tr_matrix[x][y].multistep;
749 AST_RWLIST_UNLOCK(&translators);
759 unsigned int ast_translate_path_steps(unsigned int dest, unsigned int src)
761 unsigned int res = -1;
763 /* convert bitwise format numbers into array indices */
765 dest = powerof(dest);
767 AST_RWLIST_RDLOCK(&translators);
769 if (tr_matrix[src][dest].step)
770 res = tr_matrix[src][dest].multistep + 1;
772 AST_RWLIST_UNLOCK(&translators);
777 unsigned int ast_translate_available_formats(unsigned int dest, unsigned int src)
779 unsigned int res = dest;
781 unsigned int src_audio = src & AST_FORMAT_AUDIO_MASK;
782 unsigned int src_video = src & AST_FORMAT_VIDEO_MASK;
784 /* if we don't have a source format, we just have to try all
785 possible destination formats */
789 /* If we have a source audio format, get its format index */
791 src_audio = powerof(src_audio);
793 /* If we have a source video format, get its format index */
795 src_video = powerof(src_video);
797 AST_RWLIST_RDLOCK(&translators);
799 /* For a given source audio format, traverse the list of
800 known audio formats to determine whether there exists
801 a translation path from the source format to the
802 destination format. */
803 for (x = 1; src_audio && (x & AST_FORMAT_AUDIO_MASK); x <<= 1) {
804 /* if this is not a desired format, nothing to do */
808 /* if the source is supplying this format, then
809 we can leave it in the result */
813 /* if we don't have a translation path from the src
814 to this format, remove it from the result */
815 if (!tr_matrix[src_audio][powerof(x)].step) {
820 /* now check the opposite direction */
821 if (!tr_matrix[powerof(x)][src_audio].step)
825 /* For a given source video format, traverse the list of
826 known video formats to determine whether there exists
827 a translation path from the source format to the
828 destination format. */
829 for (; src_video && (x & AST_FORMAT_VIDEO_MASK); x <<= 1) {
830 /* if this is not a desired format, nothing to do */
834 /* if the source is supplying this format, then
835 we can leave it in the result */
839 /* if we don't have a translation path from the src
840 to this format, remove it from the result */
841 if (!tr_matrix[src_video][powerof(x)].step) {
846 /* now check the opposite direction */
847 if (!tr_matrix[powerof(x)][src_video].step)
851 AST_RWLIST_UNLOCK(&translators);