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$")
30 #include <sys/types.h>
31 #include <sys/socket.h>
38 #include "asterisk/lock.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/translate.h"
42 #include "asterisk/module.h"
43 #include "asterisk/options.h"
44 #include "asterisk/frame.h"
45 #include "asterisk/sched.h"
46 #include "asterisk/cli.h"
47 #include "asterisk/term.h"
49 #define MAX_RECALC 200 /* max sample recalc */
51 /*! \brief the list of translators */
52 static AST_LIST_HEAD_STATIC(translators
, ast_translator
);
54 struct translator_path
{
55 struct ast_translator
*step
; /*!< Next step translator */
56 unsigned int cost
; /*!< Complete cost to destination */
57 unsigned int multistep
; /*!< Multiple conversions required for this translation */
60 /*! \brief a matrix that, for any pair of supported formats,
61 * indicates the total cost of translation and the first step.
62 * The full path can be reconstricted iterating on the matrix
63 * until step->dstfmt == desired_format.
65 * Array indexes are 'src' and 'dest', in that order.
67 * Note: the lock in the 'translators' list is also used to protect
70 static struct translator_path tr_matrix
[MAX_FORMAT
][MAX_FORMAT
];
73 * TODO: sample frames for each supported input format.
74 * We build this on the fly, by taking an SLIN frame and using
75 * the existing converter to play with it.
78 /*! \brief returns the index of the lowest bit set */
79 static force_inline
int powerof(unsigned int d
)
86 ast_log(LOG_WARNING
, "No bits set? %d\n", d
);
92 * wrappers around the translator routines.
96 * \brief Allocate the descriptor, required outbuf space,
97 * and possibly also plc and desc.
99 static void *newpvt(struct ast_translator
*t
)
101 struct ast_trans_pvt
*pvt
;
103 int useplc
= t
->plc_samples
> 0 && t
->useplc
; /* cache, because it can change on the fly */
107 * compute the required size adding private descriptor,
108 * plc, buffer, AST_FRIENDLY_OFFSET.
110 len
= sizeof(*pvt
) + t
->desc_size
;
112 len
+= sizeof(plc_state_t
);
114 len
+= AST_FRIENDLY_OFFSET
+ t
->buf_size
;
115 pvt
= ast_calloc(1, len
);
119 ofs
= (char *)(pvt
+ 1); /* pointer to data space */
120 if (t
->desc_size
) { /* first comes the descriptor */
124 if (useplc
) { /* then plc state */
125 pvt
->plc
= (plc_state_t
*)ofs
;
126 ofs
+= sizeof(plc_state_t
);
128 if (t
->buf_size
) /* finally buffer and header */
129 pvt
->outbuf
= ofs
+ AST_FRIENDLY_OFFSET
;
130 /* call local init routine, if present */
131 if (t
->newpvt
&& t
->newpvt(pvt
)) {
135 ast_module_ref(t
->module
);
139 static void destroy(struct ast_trans_pvt
*pvt
)
141 struct ast_translator
*t
= pvt
->t
;
143 if (ast_test_flag(&pvt
->f
, AST_FRFLAG_FROM_TRANSLATOR
)) {
144 /* If this flag is still set, that means that the translation path has
145 * been torn down, while we still have a frame out there being used.
146 * When ast_frfree() gets called on that frame, this ast_trans_pvt
147 * will get destroyed, too. */
149 /* Set the magic hint that this has been requested to be destroyed. */
158 ast_module_unref(t
->module
);
161 /*! \brief framein wrapper, deals with plc and bound checks. */
162 static int framein(struct ast_trans_pvt
*pvt
, struct ast_frame
*f
)
164 int16_t *dst
= (int16_t *)pvt
->outbuf
;
166 int samples
= pvt
->samples
; /* initial value */
168 /* Copy the last in jb timing info to the pvt */
169 ast_copy_flags(&pvt
->f
, f
, AST_FRFLAG_HAS_TIMING_INFO
);
172 pvt
->f
.seqno
= f
->seqno
;
174 if (f
->samples
== 0) {
175 ast_log(LOG_WARNING
, "no samples for %s\n", pvt
->t
->name
);
177 if (pvt
->t
->buffer_samples
) { /* do not pass empty frames to callback */
178 if (f
->datalen
== 0) { /* perform PLC with nominal framesize of 20ms/160 samples */
180 int l
= pvt
->t
->plc_samples
;
181 if (pvt
->samples
+ l
> pvt
->t
->buffer_samples
) {
182 ast_log(LOG_WARNING
, "Out of buffer space\n");
185 l
= plc_fillin(pvt
->plc
, dst
+ pvt
->samples
, l
);
187 pvt
->datalen
= pvt
->samples
* 2; /* SLIN has 2bytes for 1sample */
189 /* We don't want generic PLC. If the codec has native PLC, then do that */
190 if (!pvt
->t
->native_plc
)
193 if (pvt
->samples
+ f
->samples
> pvt
->t
->buffer_samples
) {
194 ast_log(LOG_WARNING
, "Out of buffer space\n");
198 /* we require a framein routine, wouldn't know how to do
201 ret
= pvt
->t
->framein(pvt
, f
);
202 /* possibly store data for plc */
203 if (!ret
&& pvt
->plc
) {
204 int l
= pvt
->t
->plc_samples
;
205 if (pvt
->samples
< l
)
207 plc_rx(pvt
->plc
, dst
+ pvt
->samples
- l
, l
);
210 if (pvt
->samples
== samples
)
211 ast_log(LOG_WARNING
, "%s did not update samples %d\n",
212 pvt
->t
->name
, pvt
->samples
);
216 /*! \brief generic frameout routine.
217 * If samples and datalen are 0, take whatever is in pvt
218 * and reset them, otherwise take the values in the caller and
219 * leave alone the pvt values.
221 struct ast_frame
*ast_trans_frameout(struct ast_trans_pvt
*pvt
,
222 int datalen
, int samples
)
224 struct ast_frame
*f
= &pvt
->f
;
227 f
->samples
= samples
;
229 if (pvt
->samples
== 0)
231 f
->samples
= pvt
->samples
;
235 f
->datalen
= datalen
;
237 f
->datalen
= pvt
->datalen
;
241 f
->frametype
= AST_FRAME_VOICE
;
242 f
->subclass
= 1 << (pvt
->t
->dstfmt
);
244 f
->offset
= AST_FRIENDLY_OFFSET
;
245 f
->src
= pvt
->t
->name
;
246 f
->data
= pvt
->outbuf
;
248 ast_set_flag(f
, AST_FRFLAG_FROM_TRANSLATOR
);
253 static struct ast_frame
*default_frameout(struct ast_trans_pvt
*pvt
)
255 return ast_trans_frameout(pvt
, 0, 0);
258 /* end of callback wrappers and helpers */
260 void ast_translator_free_path(struct ast_trans_pvt
*p
)
262 struct ast_trans_pvt
*pn
= p
;
269 /*! \brief Build a chain of translators based upon the given source and dest formats */
270 struct ast_trans_pvt
*ast_translator_build_path(int dest
, int source
)
272 struct ast_trans_pvt
*head
= NULL
, *tail
= NULL
;
274 source
= powerof(source
);
275 dest
= powerof(dest
);
277 AST_LIST_LOCK(&translators
);
279 while (source
!= dest
) {
280 struct ast_trans_pvt
*cur
;
281 struct ast_translator
*t
= tr_matrix
[source
][dest
].step
;
283 ast_log(LOG_WARNING
, "No translator path from %s to %s\n",
284 ast_getformatname(source
), ast_getformatname(dest
));
285 AST_LIST_UNLOCK(&translators
);
288 if (!(cur
= newpvt(t
))) {
289 ast_log(LOG_WARNING
, "Failed to build translator step from %d to %d\n", source
, dest
);
291 ast_translator_free_path(head
);
292 AST_LIST_UNLOCK(&translators
);
300 cur
->nextin
= cur
->nextout
= ast_tv(0, 0);
301 /* Keep going if this isn't the final destination */
302 source
= cur
->t
->dstfmt
;
305 AST_LIST_UNLOCK(&translators
);
309 /*! \brief do the actual translation */
310 struct ast_frame
*ast_translate(struct ast_trans_pvt
*path
, struct ast_frame
*f
, int consume
)
312 struct ast_trans_pvt
*p
= path
;
313 struct ast_frame
*out
= f
;
314 struct timeval delivery
;
320 has_timing_info
= ast_test_flag(f
, AST_FRFLAG_HAS_TIMING_INFO
);
325 /* XXX hmmm... check this below */
326 if (!ast_tvzero(f
->delivery
)) {
327 if (!ast_tvzero(path
->nextin
)) {
328 /* Make sure this is in line with what we were expecting */
329 if (!ast_tveq(path
->nextin
, f
->delivery
)) {
330 /* The time has changed between what we expected and this
331 most recent time on the new packet. If we have a
332 valid prediction adjust our output time appropriately */
333 if (!ast_tvzero(path
->nextout
)) {
334 path
->nextout
= ast_tvadd(path
->nextout
,
335 ast_tvsub(f
->delivery
, path
->nextin
));
337 path
->nextin
= f
->delivery
;
340 /* This is our first pass. Make sure the timing looks good */
341 path
->nextin
= f
->delivery
;
342 path
->nextout
= f
->delivery
;
344 /* Predict next incoming sample */
345 path
->nextin
= ast_tvadd(path
->nextin
, ast_samp2tv(f
->samples
, ast_format_rate(f
->subclass
)));
347 delivery
= f
->delivery
;
348 for ( ; out
&& p
; p
= p
->next
) {
352 out
= p
->t
->frameout(p
);
358 /* we have a frame, play with times */
359 if (!ast_tvzero(delivery
)) {
360 /* Regenerate prediction after a discontinuity */
361 if (ast_tvzero(path
->nextout
))
362 path
->nextout
= ast_tvnow();
364 /* Use next predicted outgoing timestamp */
365 out
->delivery
= path
->nextout
;
367 /* Predict next outgoing timestamp from samples in this
369 path
->nextout
= ast_tvadd(path
->nextout
, ast_samp2tv(out
->samples
, ast_format_rate(out
->subclass
)));
371 out
->delivery
= ast_tv(0, 0);
372 ast_set2_flag(out
, has_timing_info
, AST_FRFLAG_HAS_TIMING_INFO
);
373 if (has_timing_info
) {
379 /* Invalidate prediction if we're entering a silence period */
380 if (out
->frametype
== AST_FRAME_CNG
)
381 path
->nextout
= ast_tv(0, 0);
385 /*! \brief compute the cost of a single translation step */
386 static void calc_cost(struct ast_translator
*t
, int seconds
)
389 struct ast_trans_pvt
*pvt
;
390 struct timeval start
;
392 int out_rate
= ast_format_rate(t
->dstfmt
);
397 /* If they don't make samples, give them a terrible score */
399 ast_log(LOG_WARNING
, "Translator '%s' does not produce sample frames.\n", t
->name
);
406 ast_log(LOG_WARNING
, "Translator '%s' appears to be broken and will probably fail.\n", t
->name
);
413 /* Call the encoder until we've processed the required number of samples */
414 while (num_samples
< seconds
* out_rate
) {
415 struct ast_frame
*f
= t
->sample();
417 ast_log(LOG_WARNING
, "Translator '%s' failed to produce a sample frame.\n", t
->name
);
424 while ((f
= t
->frameout(pvt
))) {
425 num_samples
+= f
->samples
;
430 cost
= ast_tvdiff_ms(ast_tvnow(), start
);
434 t
->cost
= cost
/ seconds
;
441 * \brief rebuild a translation matrix.
442 * \note This function expects the list of translators to be locked
444 static void rebuild_matrix(int samples
)
446 struct ast_translator
*t
;
447 int x
; /* source format index */
448 int y
; /* intermediate format index */
449 int z
; /* destination format index */
452 ast_log(LOG_DEBUG
, "Resetting translation matrix\n");
454 bzero(tr_matrix
, sizeof(tr_matrix
));
456 /* first, compute all direct costs */
457 AST_LIST_TRAVERSE(&translators
, t
, list
) {
465 calc_cost(t
, samples
);
467 if (!tr_matrix
[x
][z
].step
|| t
->cost
< tr_matrix
[x
][z
].cost
) {
468 tr_matrix
[x
][z
].step
= t
;
469 tr_matrix
[x
][z
].cost
= t
->cost
;
474 * For each triple x, y, z of distinct formats, check if there is
475 * a path from x to z through y which is cheaper than what is
476 * currently known, and in case, update the matrix.
477 * Repeat until the matrix is stable.
481 for (x
= 0; x
< MAX_FORMAT
; x
++) { /* source format */
482 for (y
=0; y
< MAX_FORMAT
; y
++) { /* intermediate format */
483 if (x
== y
) /* skip ourselves */
486 for (z
=0; z
<MAX_FORMAT
; z
++) { /* dst format */
489 if (z
== x
|| z
== y
) /* skip null conversions */
491 if (!tr_matrix
[x
][y
].step
) /* no path from x to y */
493 if (!tr_matrix
[y
][z
].step
) /* no path from y to z */
495 newcost
= tr_matrix
[x
][y
].cost
+ tr_matrix
[y
][z
].cost
;
496 if (tr_matrix
[x
][z
].step
&& newcost
>= tr_matrix
[x
][z
].cost
)
497 continue; /* x->y->z is more expensive than
498 * the existing path */
499 /* ok, we can get from x to z via y with a cost that
500 is the sum of the transition from x to y and
503 tr_matrix
[x
][z
].step
= tr_matrix
[x
][y
].step
;
504 tr_matrix
[x
][z
].cost
= newcost
;
505 tr_matrix
[x
][z
].multistep
= 1;
507 ast_log(LOG_DEBUG
, "Discovered %d cost path from %s to %s, via %d\n", tr_matrix
[x
][z
].cost
, ast_getformatname(x
), ast_getformatname(z
), y
);
517 /*! \brief CLI "show translation" command handler */
518 static int show_translation_deprecated(int fd
, int argc
, char *argv
[])
520 #define SHOW_TRANS 13
522 int curlen
= 0, longest
= 0;
525 return RESULT_SHOWUSAGE
;
527 AST_LIST_LOCK(&translators
);
529 if (argv
[2] && !strcasecmp(argv
[2], "recalc")) {
530 z
= argv
[3] ? atoi(argv
[3]) : 1;
533 ast_cli(fd
, " C'mon let's be serious here... defaulting to 1.\n");
537 if (z
> MAX_RECALC
) {
538 ast_cli(fd
, " Maximum limit of recalc exceeded by %d, truncating value to %d\n", z
- MAX_RECALC
, MAX_RECALC
);
541 ast_cli(fd
, " Recalculating Codec Translation (number of sample seconds: %d)\n\n", z
);
545 ast_cli(fd
, " Translation times between formats (in milliseconds) for one second of data\n");
546 ast_cli(fd
, " Source Format (Rows) Destination Format (Columns)\n\n");
547 /* Get the length of the longest (usable?) codec name, so we know how wide the left side should be */
548 for (x
= 0; x
< SHOW_TRANS
; x
++) {
549 curlen
= strlen(ast_getformatname(1 << (x
)));
550 if (curlen
> longest
)
553 for (x
= -1; x
< SHOW_TRANS
; x
++) {
556 size_t left
= sizeof(line
) - 1; /* one initial space */
557 /* next 2 lines run faster than using ast_build_string() */
560 for (y
= -1; y
< SHOW_TRANS
; y
++) {
562 curlen
= strlen(ast_getformatname(1 << (y
)));
564 if (x
>= 0 && y
>= 0 && tr_matrix
[x
][y
].step
) {
565 /* XXX 999 is a little hackish
566 We don't want this number being larger than the shortest (or current) codec
567 For now, that is "gsm" */
568 ast_build_string(&buf
, &left
, "%*d", curlen
+ 1, tr_matrix
[x
][y
].cost
> 999 ? 0 : tr_matrix
[x
][y
].cost
);
569 } else if (x
== -1 && y
>= 0) {
570 /* Top row - use a dynamic size */
571 ast_build_string(&buf
, &left
, "%*s", curlen
+ 1, ast_getformatname(1 << (y
)) );
572 } else if (y
== -1 && x
>= 0) {
573 /* Left column - use a static size. */
574 ast_build_string(&buf
, &left
, "%*s", longest
, ast_getformatname(1 << (x
)) );
575 } else if (x
>= 0 && y
>= 0) {
576 ast_build_string(&buf
, &left
, "%*s", curlen
+ 1, "-");
578 ast_build_string(&buf
, &left
, "%*s", longest
, "");
581 ast_build_string(&buf
, &left
, "\n");
584 AST_LIST_UNLOCK(&translators
);
585 return RESULT_SUCCESS
;
588 static int show_translation(int fd
, int argc
, char *argv
[])
591 int curlen
= 0, longest
= 0;
594 return RESULT_SHOWUSAGE
;
596 AST_LIST_LOCK(&translators
);
598 if (argv
[3] && !strcasecmp(argv
[3], "recalc")) {
599 z
= argv
[4] ? atoi(argv
[4]) : 1;
602 ast_cli(fd
, " C'mon let's be serious here... defaulting to 1.\n");
606 if (z
> MAX_RECALC
) {
607 ast_cli(fd
, " Maximum limit of recalc exceeded by %d, truncating value to %d\n", z
- MAX_RECALC
, MAX_RECALC
);
610 ast_cli(fd
, " Recalculating Codec Translation (number of sample seconds: %d)\n\n", z
);
614 ast_cli(fd
, " Translation times between formats (in milliseconds) for one second of data\n");
615 ast_cli(fd
, " Source Format (Rows) Destination Format (Columns)\n\n");
616 /* Get the length of the longest (usable?) codec name, so we know how wide the left side should be */
617 for (x
= 0; x
< SHOW_TRANS
; x
++) {
618 curlen
= strlen(ast_getformatname(1 << (x
)));
619 if (curlen
> longest
)
622 for (x
= -1; x
< SHOW_TRANS
; x
++) {
625 size_t left
= sizeof(line
) - 1; /* one initial space */
626 /* next 2 lines run faster than using ast_build_string() */
629 for (y
= -1; y
< SHOW_TRANS
; y
++) {
631 curlen
= strlen(ast_getformatname(1 << (y
)));
633 if (x
>= 0 && y
>= 0 && tr_matrix
[x
][y
].step
) {
634 /* XXX 999 is a little hackish
635 We don't want this number being larger than the shortest (or current) codec
636 For now, that is "gsm" */
637 ast_build_string(&buf
, &left
, "%*d", curlen
+ 1, tr_matrix
[x
][y
].cost
> 999 ? 0 : tr_matrix
[x
][y
].cost
);
638 } else if (x
== -1 && y
>= 0) {
639 /* Top row - use a dynamic size */
640 ast_build_string(&buf
, &left
, "%*s", curlen
+ 1, ast_getformatname(1 << (y
)) );
641 } else if (y
== -1 && x
>= 0) {
642 /* Left column - use a static size. */
643 ast_build_string(&buf
, &left
, "%*s", longest
, ast_getformatname(1 << (x
)) );
644 } else if (x
>= 0 && y
>= 0) {
645 ast_build_string(&buf
, &left
, "%*s", curlen
+ 1, "-");
647 ast_build_string(&buf
, &left
, "%*s", longest
, "");
650 ast_build_string(&buf
, &left
, "\n");
653 AST_LIST_UNLOCK(&translators
);
654 return RESULT_SUCCESS
;
657 static char show_trans_usage
[] =
658 "Usage: core show translation [recalc] [<recalc seconds>]\n"
659 " Displays known codec translators and the cost associated\n"
660 "with each conversion. If the argument 'recalc' is supplied along\n"
661 "with optional number of seconds to test a new test will be performed\n"
662 "as the chart is being displayed.\n";
664 static struct ast_cli_entry cli_show_translation_deprecated
= {
665 { "show", "translation", NULL
},
666 show_translation_deprecated
, NULL
,
669 static struct ast_cli_entry cli_translate
[] = {
670 { { "core", "show", "translation", NULL
},
671 show_translation
, "Display translation matrix",
672 show_trans_usage
, NULL
, &cli_show_translation_deprecated
},
675 /*! \brief register codec translator */
676 int __ast_register_translator(struct ast_translator
*t
, struct ast_module
*mod
)
678 static int added_cli
= 0;
679 struct ast_translator
*u
;
682 ast_log(LOG_WARNING
, "Missing module pointer, you need to supply one\n");
687 ast_log(LOG_WARNING
, "empty buf size, you need to supply one\n");
693 t
->srcfmt
= powerof(t
->srcfmt
);
694 t
->dstfmt
= powerof(t
->dstfmt
);
697 if (t
->plc_samples
) {
698 if (t
->buffer_samples
< t
->plc_samples
) {
699 ast_log(LOG_WARNING
, "plc_samples %d buffer_samples %d\n",
700 t
->plc_samples
, t
->buffer_samples
);
703 if (t
->dstfmt
!= powerof(AST_FORMAT_SLINEAR
))
704 ast_log(LOG_WARNING
, "plc_samples %d format %x\n",
705 t
->plc_samples
, t
->dstfmt
);
707 if (t
->srcfmt
>= MAX_FORMAT
) {
708 ast_log(LOG_WARNING
, "Source format %s is larger than MAX_FORMAT\n", ast_getformatname(t
->srcfmt
));
712 if (t
->dstfmt
>= MAX_FORMAT
) {
713 ast_log(LOG_WARNING
, "Destination format %s is larger than MAX_FORMAT\n", ast_getformatname(t
->dstfmt
));
719 * Align buf_size properly, rounding up to the machine-specific
720 * alignment for pointers.
722 struct _test_align
{ void *a
, *b
; } p
;
723 int align
= (char *)&p
.b
- (char *)&p
.a
;
725 t
->buf_size
= ((t
->buf_size
+ align
- 1) / align
) * align
;
728 if (t
->frameout
== NULL
)
729 t
->frameout
= default_frameout
;
733 if (option_verbose
> 1) {
736 ast_verbose(VERBOSE_PREFIX_2
"Registered translator '%s' from format %s to %s, cost %d\n",
737 term_color(tmp
, t
->name
, COLOR_MAGENTA
, COLOR_BLACK
, sizeof(tmp
)),
738 ast_getformatname(1 << t
->srcfmt
), ast_getformatname(1 << t
->dstfmt
), t
->cost
);
742 ast_cli_register_multiple(cli_translate
, sizeof(cli_translate
) / sizeof(struct ast_cli_entry
));
746 AST_LIST_LOCK(&translators
);
748 /* find any existing translators that provide this same srcfmt/dstfmt,
749 and put this one in order based on cost */
750 AST_LIST_TRAVERSE_SAFE_BEGIN(&translators
, u
, list
) {
751 if ((u
->srcfmt
== t
->srcfmt
) &&
752 (u
->dstfmt
== t
->dstfmt
) &&
753 (u
->cost
> t
->cost
)) {
754 AST_LIST_INSERT_BEFORE_CURRENT(&translators
, t
, list
);
758 AST_LIST_TRAVERSE_SAFE_END
;
760 /* if no existing translator was found for this format combination,
761 add it to the beginning of the list */
763 AST_LIST_INSERT_HEAD(&translators
, t
, list
);
767 AST_LIST_UNLOCK(&translators
);
772 /*! \brief unregister codec translator */
773 int ast_unregister_translator(struct ast_translator
*t
)
776 struct ast_translator
*u
;
779 AST_LIST_LOCK(&translators
);
780 AST_LIST_TRAVERSE_SAFE_BEGIN(&translators
, u
, list
) {
782 AST_LIST_REMOVE_CURRENT(&translators
, list
);
783 if (option_verbose
> 1)
784 ast_verbose(VERBOSE_PREFIX_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
));
789 AST_LIST_TRAVERSE_SAFE_END
;
794 AST_LIST_UNLOCK(&translators
);
799 void ast_translator_activate(struct ast_translator
*t
)
801 AST_LIST_LOCK(&translators
);
804 AST_LIST_UNLOCK(&translators
);
807 void ast_translator_deactivate(struct ast_translator
*t
)
809 AST_LIST_LOCK(&translators
);
812 AST_LIST_UNLOCK(&translators
);
815 /*! \brief Calculate our best translator source format, given costs, and a desired destination */
816 int ast_translator_best_choice(int *dst
, int *srcs
)
822 int besttime
= INT_MAX
;
823 int beststeps
= INT_MAX
;
824 int common
= ((*dst
) & (*srcs
)) & AST_FORMAT_AUDIO_MASK
; /* are there common formats ? */
826 if (common
) { /* yes, pick one and return */
827 for (cur
= 1, y
= 0; y
<= MAX_AUDIO_FORMAT
; cur
<<= 1, y
++) {
828 if (cur
& common
) /* guaranteed to find one */
831 /* We are done, this is a common format to both. */
834 } else { /* No, we will need to translate */
835 AST_LIST_LOCK(&translators
);
836 for (cur
= 1, y
= 0; y
<= MAX_AUDIO_FORMAT
; cur
<<= 1, y
++) {
839 for (cursrc
= 1, x
= 0; x
<= MAX_AUDIO_FORMAT
; cursrc
<<= 1, x
++) {
840 if (!(*srcs
& cursrc
) || !tr_matrix
[x
][y
].step
||
841 tr_matrix
[x
][y
].cost
> besttime
)
842 continue; /* not existing or no better */
843 if (tr_matrix
[x
][y
].cost
< besttime
||
844 tr_matrix
[x
][y
].multistep
< beststeps
) {
845 /* better than what we have so far */
848 besttime
= tr_matrix
[x
][y
].cost
;
849 beststeps
= tr_matrix
[x
][y
].multistep
;
853 AST_LIST_UNLOCK(&translators
);
863 unsigned int ast_translate_path_steps(unsigned int dest
, unsigned int src
)
865 unsigned int res
= -1;
867 /* convert bitwise format numbers into array indices */
869 dest
= powerof(dest
);
871 AST_LIST_LOCK(&translators
);
873 if (tr_matrix
[src
][dest
].step
)
874 res
= tr_matrix
[src
][dest
].multistep
+ 1;
876 AST_LIST_UNLOCK(&translators
);
881 unsigned int ast_translate_available_formats(unsigned int dest
, unsigned int src
)
883 unsigned int res
= dest
;
885 unsigned int src_audio
= src
& AST_FORMAT_AUDIO_MASK
;
886 unsigned int src_video
= src
& AST_FORMAT_VIDEO_MASK
;
888 /* if we don't have a source format, we just have to try all
889 possible destination formats */
893 /* If we have a source audio format, get its format index */
895 src_audio
= powerof(src_audio
);
897 /* If we have a source video format, get its format index */
899 src_video
= powerof(src_video
);
901 AST_LIST_LOCK(&translators
);
903 /* For a given source audio format, traverse the list of
904 known audio formats to determine whether there exists
905 a translation path from the source format to the
906 destination format. */
907 for (x
= 1; src_audio
&& x
< AST_FORMAT_MAX_AUDIO
; x
<<= 1) {
908 /* if this is not a desired format, nothing to do */
912 /* if the source is supplying this format, then
913 we can leave it in the result */
917 /* if we don't have a translation path from the src
918 to this format, remove it from the result */
919 if (!tr_matrix
[src_audio
][powerof(x
)].step
) {
924 /* now check the opposite direction */
925 if (!tr_matrix
[powerof(x
)][src_audio
].step
)
929 /* For a given source video format, traverse the list of
930 known video formats to determine whether there exists
931 a translation path from the source format to the
932 destination format. */
933 for (; src_video
&& x
< AST_FORMAT_MAX_VIDEO
; x
<<= 1) {
934 /* if this is not a desired format, nothing to do */
938 /* if the source is supplying this format, then
939 we can leave it in the result */
943 /* if we don't have a translation path from the src
944 to this format, remove it from the result */
945 if (!tr_matrix
[src_video
][powerof(x
)].step
) {
950 /* now check the opposite direction */
951 if (!tr_matrix
[powerof(x
)][src_video
].step
)
955 AST_LIST_UNLOCK(&translators
);
960 void ast_translate_frame_freed(struct ast_frame
*fr
)
962 struct ast_trans_pvt
*pvt
;
964 ast_clear_flag(fr
, AST_FRFLAG_FROM_TRANSLATOR
);
966 pvt
= (struct ast_trans_pvt
*) (((char *) fr
) - offsetof(struct ast_trans_pvt
, f
));
968 if (pvt
->datalen
!= -1)