translate.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, 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. /*! \file
  19. * \brief Support for translation of data formats.
  20. * \ref translate.c
  21. */
  22. #ifndef _ASTERISK_TRANSLATE_H
  23. #define _ASTERISK_TRANSLATE_H
  24. #if defined(__cplusplus) || defined(c_plusplus)
  25. extern "C" {
  26. #endif
  27. #if 1 /* need lots of stuff... */
  28. #include "asterisk/frame.h"
  29. #include "asterisk/plc.h"
  30. #include "asterisk/linkedlists.h"
  31. #include "asterisk/format_cap.h"
  32. #include "asterisk/format_cache.h"
  33. #endif
  34. struct ast_trans_pvt; /* declared below */
  35. /*!
  36. * \brief Translator Cost Table definition.
  37. *
  38. * \note The defined values in this table must be used to set
  39. * the translator's table_cost value.
  40. *
  41. * \note The cost value of the first two values must always add
  42. * up to be greater than the largest value defined in this table.
  43. * This is done to guarantee a direct translation will always
  44. * have precedence over a multi step translation.
  45. *
  46. * \details This table is built in a way that allows translation
  47. * paths to be built that guarantee the best possible balance
  48. * between performance and quality. With this table direct
  49. * translation paths between two formats will always take precedence
  50. * over multi step paths, lossless intermediate steps will always
  51. * be chosen over lossy intermediate steps, and preservation of
  52. * sample rate across the translation will always have precedence
  53. * over a path that involves any re-sampling.
  54. */
  55. enum ast_trans_cost_table {
  56. /* Lossless Source Translation Costs */
  57. /*! [lossless -> lossless] original sampling */
  58. AST_TRANS_COST_LL_LL_ORIGSAMP = 400000,
  59. /*! [lossless -> lossy] original sampling */
  60. AST_TRANS_COST_LL_LY_ORIGSAMP = 600000,
  61. /*! [lossless -> lossless] up sample */
  62. AST_TRANS_COST_LL_LL_UPSAMP = 800000,
  63. /*! [lossless -> lossy] up sample */
  64. AST_TRANS_COST_LL_LY_UPSAMP = 825000,
  65. /*! [lossless -> lossless] down sample */
  66. AST_TRANS_COST_LL_LL_DOWNSAMP = 850000,
  67. /*! [lossless -> lossy] down sample */
  68. AST_TRANS_COST_LL_LY_DOWNSAMP = 875000,
  69. /*! [lossless -> unknown] unknown.
  70. * This value is for a lossless source translation
  71. * with an unknown destination and or sample rate conversion. */
  72. AST_TRANS_COST_LL_UNKNOWN = 885000,
  73. /* Lossy Source Translation Costs */
  74. /*! [lossy -> lossless] original sampling */
  75. AST_TRANS_COST_LY_LL_ORIGSAMP = 900000,
  76. /*! [lossy -> lossy] original sampling */
  77. AST_TRANS_COST_LY_LY_ORIGSAMP = 915000,
  78. /*! [lossy -> lossless] up sample */
  79. AST_TRANS_COST_LY_LL_UPSAMP = 930000,
  80. /*! [lossy -> lossy] up sample */
  81. AST_TRANS_COST_LY_LY_UPSAMP = 945000,
  82. /*! [lossy -> lossless] down sample */
  83. AST_TRANS_COST_LY_LL_DOWNSAMP = 960000,
  84. /*! [lossy -> lossy] down sample */
  85. AST_TRANS_COST_LY_LY_DOWNSAMP = 975000,
  86. /*! [lossy -> unknown] unknown.
  87. * This value is for a lossy source translation
  88. * with an unknown destination and or sample rate conversion. */
  89. AST_TRANS_COST_LY_UNKNOWN = 985000,
  90. };
  91. /*! \brief
  92. * Descriptor of a translator.
  93. *
  94. * Name, callbacks, and various options
  95. * related to run-time operation (size of buffers, auxiliary
  96. * descriptors, etc).
  97. *
  98. * A codec registers itself by filling the relevant fields
  99. * of a structure and passing it as an argument to
  100. * ast_register_translator(). The structure should not be
  101. * modified after a successful registration, and its address
  102. * must be used as an argument to ast_unregister_translator().
  103. *
  104. * As a minimum, a translator should supply name, srcfmt and dstfmt,
  105. * the required buf_size (in bytes) and buffer_samples (in samples),
  106. * and a few callbacks (framein, frameout, sample).
  107. * The outbuf is automatically prepended by AST_FRIENDLY_OFFSET
  108. * spare bytes so generic routines can place data in there.
  109. *
  110. * Note, the translator is not supposed to do any memory allocation
  111. * or deallocation, nor any locking, because all of this is done in
  112. * the generic code.
  113. *
  114. * Translators using generic plc (packet loss concealment) should
  115. * supply a non-zero plc_samples indicating the size (in samples)
  116. * of artificially generated frames and incoming data.
  117. * Generic plc is only available for dstfmt = SLINEAR
  118. */
  119. struct ast_translator {
  120. char name[80]; /*!< Name of translator */
  121. struct ast_codec src_codec; /*!< Source codec */
  122. struct ast_codec dst_codec; /*!< Destination codec */
  123. struct ast_codec *core_src_codec; /*!< Core registered source codec */
  124. struct ast_codec *core_dst_codec; /*!< Core registered destination codec */
  125. const char *format; /*!< Optional name of a cached format this translator produces */
  126. int table_cost; /*!< Cost value associated with this translator based
  127. * on translation cost table. */
  128. int comp_cost; /*!< Cost value associated with this translator based
  129. * on computation time. This cost value is computed based
  130. * on the time required to translate sample data. */
  131. int (*newpvt)(struct ast_trans_pvt *); /*!< initialize private data
  132. * associated with the translator */
  133. int (*framein)(struct ast_trans_pvt *pvt, struct ast_frame *in);
  134. /*!< Input frame callback. Store
  135. * (and possibly convert) input frame. */
  136. struct ast_frame * (*frameout)(struct ast_trans_pvt *pvt);
  137. /*!< Output frame callback. Generate a frame
  138. * with outbuf content. */
  139. void (*destroy)(struct ast_trans_pvt *pvt);
  140. /*!< cleanup private data, if needed
  141. * (often unnecessary). */
  142. struct ast_frame * (*sample)(void); /*!< Generate an example frame */
  143. /*!\brief size of outbuf, in samples. Leave it 0 if you want the framein
  144. * callback deal with the frame. Set it appropriately if you
  145. * want the code to checks if the incoming frame fits the
  146. * outbuf (this is e.g. required for plc).
  147. */
  148. int buffer_samples; /*< size of outbuf, in samples */
  149. /*! \brief size of outbuf, in bytes. Mandatory. The wrapper code will also
  150. * allocate an AST_FRIENDLY_OFFSET space before.
  151. */
  152. int buf_size;
  153. int desc_size; /*!< size of private descriptor in pvt->pvt, if any */
  154. int native_plc; /*!< true if the translator can do native plc */
  155. struct ast_module *module; /*!< opaque reference to the parent module */
  156. int active; /*!< Whether this translator should be used or not */
  157. int src_fmt_index; /*!< index of the source format in the matrix table */
  158. int dst_fmt_index; /*!< index of the destination format in the matrix table */
  159. AST_LIST_ENTRY(ast_translator) list; /*!< link field */
  160. };
  161. /*! \brief
  162. * Default structure for translators, with the basic fields and buffers,
  163. * all allocated as part of the same chunk of memory. The buffer is
  164. * preceded by \ref AST_FRIENDLY_OFFSET bytes in front of the user portion.
  165. * 'buf' points right after this space.
  166. *
  167. * *_framein() routines operate in two ways:
  168. * 1. some convert on the fly and place the data directly in outbuf;
  169. * in this case 'samples' and 'datalen' contain the number of samples
  170. * and number of bytes available in the buffer.
  171. * In this case we can use a generic *_frameout() routine that simply
  172. * takes whatever is there and places it into the output frame.
  173. * 2. others simply store the (unconverted) samples into a working
  174. * buffer, and leave the conversion task to *_frameout().
  175. * In this case, the intermediate buffer must be in the private
  176. * descriptor, 'datalen' is left to 0, while 'samples' is still
  177. * updated with the number of samples received.
  178. */
  179. struct ast_trans_pvt {
  180. struct ast_translator *t;
  181. struct ast_frame f; /*!< used in frameout. This frame holds a f.subclass.format ref. */
  182. int samples; /*!< samples available in outbuf */
  183. /*! \brief actual space used in outbuf */
  184. int datalen;
  185. void *pvt; /*!< more private data, if any */
  186. union {
  187. char *c; /*!< the useful portion of the buffer */
  188. unsigned char *uc; /*!< the useful portion of the buffer */
  189. int16_t *i16;
  190. uint8_t *ui8;
  191. } outbuf;
  192. plc_state_t *plc; /*!< optional plc pointer */
  193. struct ast_trans_pvt *next; /*!< next in translator chain */
  194. struct timeval nextin;
  195. struct timeval nextout;
  196. /*! If a translation path using a format with attributes requires the output
  197. * to be a specific set of attributes, this variable will be set describing
  198. * those attributes to the translator. Otherwise, the translator must choose
  199. * a set of format attributes for the destination that preserves the quality
  200. * of the audio in the best way possible. For example with the Opus Codec,
  201. * explicit_dst contains an attribute which describes whether both parties
  202. * want to do forward-error correction (FEC). */
  203. struct ast_format *explicit_dst;
  204. };
  205. /*! \brief generic frameout function */
  206. struct ast_frame *ast_trans_frameout(struct ast_trans_pvt *pvt,
  207. int datalen, int samples);
  208. struct ast_trans_pvt;
  209. /*!
  210. * \brief Register a translator
  211. * This registers a codec translator with asterisk
  212. * \param t populated ast_translator structure
  213. * \param mod module handle to the module that owns this translator
  214. * \return 0 on success, -1 on failure
  215. */
  216. int __ast_register_translator(struct ast_translator *t, struct ast_module *module);
  217. /*! \brief See \ref __ast_register_translator() */
  218. #define ast_register_translator(t) __ast_register_translator(t, ast_module_info->self)
  219. /*!
  220. * \brief Unregister a translator
  221. * Unregisters the given tranlator
  222. * \param t translator to unregister
  223. * \return 0 on success, -1 on failure
  224. */
  225. int ast_unregister_translator(struct ast_translator *t);
  226. /*!
  227. * \brief Activate a previously deactivated translator
  228. * \param t translator to activate
  229. * \return nothing
  230. *
  231. * Enables the specified translator for use.
  232. */
  233. void ast_translator_activate(struct ast_translator *t);
  234. /*!
  235. * \brief Deactivate a translator
  236. * \param t translator to deactivate
  237. * \return nothing
  238. *
  239. * Disables the specified translator from being used.
  240. */
  241. void ast_translator_deactivate(struct ast_translator *t);
  242. /*!
  243. * \brief Chooses the best translation path
  244. *
  245. * Given a list of sources, and a designed destination format, which should
  246. * I choose?
  247. *
  248. * \param dst_cap destination capabilities
  249. * \param src_cap source capabilities
  250. * \param dst_fmt_out destination format chosen out of destination capabilities
  251. * \param src_fmt_out source format chosen out of source capabilities
  252. * \return Returns 0 on success, -1 if no path could be found.
  253. *
  254. * \note dst_cap and src_cap are not mondified.
  255. */
  256. int ast_translator_best_choice(struct ast_format_cap *dst_cap,
  257. struct ast_format_cap *src_cap,
  258. struct ast_format **dst_fmt_out,
  259. struct ast_format **src_fmt_out);
  260. /*!
  261. * \brief Builds a translator path
  262. * Build a path (possibly NULL) from source to dest
  263. * \param dst dest destination format
  264. * \param src source source format
  265. * \return ast_trans_pvt on success, NULL on failure
  266. * */
  267. struct ast_trans_pvt *ast_translator_build_path(struct ast_format *dest, struct ast_format *source);
  268. /*!
  269. * \brief Frees a translator path
  270. * Frees the given translator path structure
  271. * \param tr translator path to get rid of
  272. */
  273. void ast_translator_free_path(struct ast_trans_pvt *tr);
  274. /*!
  275. * \brief translates one or more frames
  276. * Apply an input frame into the translator and receive zero or one output frames. Consume
  277. * determines whether the original frame should be freed
  278. * \param path tr translator structure to use for translation
  279. * \param f frame to translate
  280. * \param consume Whether or not to free the original frame
  281. * \return an ast_frame of the new translation format on success, NULL on failure
  282. */
  283. struct ast_frame *ast_translate(struct ast_trans_pvt *tr, struct ast_frame *f, int consume);
  284. /*!
  285. * \brief Returns the number of steps required to convert from 'src' to 'dest'.
  286. * \param dest destination format
  287. * \param src source format
  288. * \return the number of translation steps required, or -1 if no path is available
  289. */
  290. unsigned int ast_translate_path_steps(struct ast_format *dest, struct ast_format *src);
  291. /*!
  292. * \brief Find available formats
  293. * \param dest possible destination formats
  294. * \param src source formats
  295. * \param result capabilities structure to store available formats in
  296. *
  297. * \return the destination formats that are available in the source or translatable
  298. *
  299. * The result will include all formats from 'dest' that are either present
  300. * in 'src' or translatable from a format present in 'src'.
  301. *
  302. * \note Only a single audio format and a single video format can be
  303. * present in 'src', or the function will produce unexpected results.
  304. */
  305. void ast_translate_available_formats(struct ast_format_cap *dest, struct ast_format_cap *src, struct ast_format_cap *result);
  306. /*!
  307. * \brief Puts a string representation of the translation path into outbuf
  308. * \param t translator structure containing the translation path
  309. * \param str ast_str output buffer
  310. * \retval on success pointer to beginning of outbuf. on failure "".
  311. */
  312. const char *ast_translate_path_to_str(struct ast_trans_pvt *t, struct ast_str **str);
  313. /*!
  314. * \brief Initialize the translation matrix and index to format conversion table.
  315. * \retval 0 on success
  316. * \retval -1 on failure
  317. */
  318. int ast_translate_init(void);
  319. #if defined(__cplusplus) || defined(c_plusplus)
  320. }
  321. #endif
  322. #endif /* _ASTERISK_TRANSLATE_H */