format_compatibility.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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. *
  20. * \brief Media Format Bitfield Compatibility API
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/logger.h"
  30. #include "asterisk/astobj2.h"
  31. #include "asterisk/codec.h"
  32. #include "asterisk/format.h"
  33. #include "asterisk/format_compatibility.h"
  34. #include "asterisk/format_cache.h"
  35. uint64_t ast_format_compatibility_format2bitfield(const struct ast_format *format)
  36. {
  37. if (ast_format_cmp(format, ast_format_g723) == AST_FORMAT_CMP_EQUAL) {
  38. return AST_FORMAT_G723;
  39. } else if (ast_format_cmp(format, ast_format_gsm) == AST_FORMAT_CMP_EQUAL) {
  40. return AST_FORMAT_GSM;
  41. } else if (ast_format_cmp(format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
  42. return AST_FORMAT_ULAW;
  43. } else if (ast_format_cmp(format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) {
  44. return AST_FORMAT_ALAW;
  45. } else if (ast_format_cmp(format, ast_format_g726_aal2) == AST_FORMAT_CMP_EQUAL) {
  46. return AST_FORMAT_G726_AAL2;
  47. } else if (ast_format_cmp(format, ast_format_adpcm) == AST_FORMAT_CMP_EQUAL) {
  48. return AST_FORMAT_ADPCM;
  49. } else if (ast_format_cmp(format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) {
  50. return AST_FORMAT_SLIN;
  51. } else if (ast_format_cmp(format, ast_format_lpc10) == AST_FORMAT_CMP_EQUAL) {
  52. return AST_FORMAT_LPC10;
  53. } else if (ast_format_cmp(format, ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
  54. return AST_FORMAT_G729;
  55. } else if (ast_format_cmp(format, ast_format_speex) == AST_FORMAT_CMP_EQUAL) {
  56. return AST_FORMAT_SPEEX;
  57. } else if (ast_format_cmp(format, ast_format_ilbc) == AST_FORMAT_CMP_EQUAL) {
  58. return AST_FORMAT_ILBC;
  59. } else if (ast_format_cmp(format, ast_format_g726) == AST_FORMAT_CMP_EQUAL) {
  60. return AST_FORMAT_G726;
  61. } else if (ast_format_cmp(format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) {
  62. return AST_FORMAT_G722;
  63. } else if (ast_format_cmp(format, ast_format_siren7) == AST_FORMAT_CMP_EQUAL) {
  64. return AST_FORMAT_SIREN7;
  65. } else if (ast_format_cmp(format, ast_format_siren14) == AST_FORMAT_CMP_EQUAL) {
  66. return AST_FORMAT_SIREN14;
  67. } else if (ast_format_cmp(format, ast_format_slin16) == AST_FORMAT_CMP_EQUAL) {
  68. return AST_FORMAT_SLIN16;
  69. } else if (ast_format_cmp(format, ast_format_g719) == AST_FORMAT_CMP_EQUAL) {
  70. return AST_FORMAT_G719;
  71. } else if (ast_format_cmp(format, ast_format_speex16) == AST_FORMAT_CMP_EQUAL) {
  72. return AST_FORMAT_SPEEX16;
  73. } else if (ast_format_cmp(format, ast_format_opus) == AST_FORMAT_CMP_EQUAL) {
  74. return AST_FORMAT_OPUS;
  75. } else if (ast_format_cmp(format, ast_format_testlaw) == AST_FORMAT_CMP_EQUAL) {
  76. return AST_FORMAT_TESTLAW;
  77. } else if (ast_format_cmp(format, ast_format_h261) == AST_FORMAT_CMP_EQUAL) {
  78. return AST_FORMAT_H261;
  79. } else if (ast_format_cmp(format, ast_format_h263) == AST_FORMAT_CMP_EQUAL) {
  80. return AST_FORMAT_H263;
  81. } else if (ast_format_cmp(format, ast_format_h263p) == AST_FORMAT_CMP_EQUAL) {
  82. return AST_FORMAT_H263P;
  83. } else if (ast_format_cmp(format, ast_format_h264) == AST_FORMAT_CMP_EQUAL) {
  84. return AST_FORMAT_H264;
  85. } else if (ast_format_cmp(format, ast_format_mp4) == AST_FORMAT_CMP_EQUAL) {
  86. return AST_FORMAT_MP4;
  87. } else if (ast_format_cmp(format, ast_format_vp8) == AST_FORMAT_CMP_EQUAL) {
  88. return AST_FORMAT_VP8;
  89. } else if (ast_format_cmp(format, ast_format_jpeg) == AST_FORMAT_CMP_EQUAL) {
  90. return AST_FORMAT_JPEG;
  91. } else if (ast_format_cmp(format, ast_format_png) == AST_FORMAT_CMP_EQUAL) {
  92. return AST_FORMAT_PNG;
  93. } else if (ast_format_cmp(format, ast_format_t140_red) == AST_FORMAT_CMP_EQUAL) {
  94. return AST_FORMAT_T140_RED;
  95. } else if (ast_format_cmp(format, ast_format_t140) == AST_FORMAT_CMP_EQUAL) {
  96. return AST_FORMAT_T140;
  97. }
  98. return 0;
  99. }
  100. uint64_t ast_format_compatibility_codec2bitfield(const struct ast_codec *codec)
  101. {
  102. if (codec->id == ast_format_get_codec_id(ast_format_g723)) {
  103. return AST_FORMAT_G723;
  104. } else if (codec->id == ast_format_get_codec_id(ast_format_gsm)) {
  105. return AST_FORMAT_GSM;
  106. } else if (codec->id == ast_format_get_codec_id(ast_format_ulaw)) {
  107. return AST_FORMAT_ULAW;
  108. } else if (codec->id == ast_format_get_codec_id(ast_format_alaw)) {
  109. return AST_FORMAT_ALAW;
  110. } else if (codec->id == ast_format_get_codec_id(ast_format_g726_aal2)) {
  111. return AST_FORMAT_G726_AAL2;
  112. } else if (codec->id == ast_format_get_codec_id(ast_format_adpcm)) {
  113. return AST_FORMAT_ADPCM;
  114. } else if (codec->id == ast_format_get_codec_id(ast_format_slin)) {
  115. return AST_FORMAT_SLIN;
  116. } else if (codec->id == ast_format_get_codec_id(ast_format_lpc10)) {
  117. return AST_FORMAT_LPC10;
  118. } else if (codec->id == ast_format_get_codec_id(ast_format_g729)) {
  119. return AST_FORMAT_G729;
  120. } else if (codec->id == ast_format_get_codec_id(ast_format_speex)) {
  121. return AST_FORMAT_SPEEX;
  122. } else if (codec->id == ast_format_get_codec_id(ast_format_ilbc)) {
  123. return AST_FORMAT_ILBC;
  124. } else if (codec->id == ast_format_get_codec_id(ast_format_g726)) {
  125. return AST_FORMAT_G726;
  126. } else if (codec->id == ast_format_get_codec_id(ast_format_g722)) {
  127. return AST_FORMAT_G722;
  128. } else if (codec->id == ast_format_get_codec_id(ast_format_siren7)) {
  129. return AST_FORMAT_SIREN7;
  130. } else if (codec->id == ast_format_get_codec_id(ast_format_siren14)) {
  131. return AST_FORMAT_SIREN14;
  132. } else if (codec->id == ast_format_get_codec_id(ast_format_slin16)) {
  133. return AST_FORMAT_SLIN16;
  134. } else if (codec->id == ast_format_get_codec_id(ast_format_g719)) {
  135. return AST_FORMAT_G719;
  136. } else if (codec->id == ast_format_get_codec_id(ast_format_speex16)) {
  137. return AST_FORMAT_SPEEX16;
  138. } else if (codec->id == ast_format_get_codec_id(ast_format_opus)) {
  139. return AST_FORMAT_OPUS;
  140. } else if (codec->id == ast_format_get_codec_id(ast_format_testlaw)) {
  141. return AST_FORMAT_TESTLAW;
  142. } else if (codec->id == ast_format_get_codec_id(ast_format_h261)) {
  143. return AST_FORMAT_H261;
  144. } else if (codec->id == ast_format_get_codec_id(ast_format_h263)) {
  145. return AST_FORMAT_H263;
  146. } else if (codec->id == ast_format_get_codec_id(ast_format_h263p)) {
  147. return AST_FORMAT_H263P;
  148. } else if (codec->id == ast_format_get_codec_id(ast_format_h264)) {
  149. return AST_FORMAT_H264;
  150. } else if (codec->id == ast_format_get_codec_id(ast_format_mp4)) {
  151. return AST_FORMAT_MP4;
  152. } else if (codec->id == ast_format_get_codec_id(ast_format_vp8)) {
  153. return AST_FORMAT_VP8;
  154. } else if (codec->id == ast_format_get_codec_id(ast_format_jpeg)) {
  155. return AST_FORMAT_JPEG;
  156. } else if (codec->id == ast_format_get_codec_id(ast_format_png)) {
  157. return AST_FORMAT_PNG;
  158. } else if (codec->id == ast_format_get_codec_id(ast_format_t140_red)) {
  159. return AST_FORMAT_T140_RED;
  160. } else if (codec->id == ast_format_get_codec_id(ast_format_t140)) {
  161. return AST_FORMAT_T140;
  162. }
  163. return 0;
  164. }
  165. struct ast_format *ast_format_compatibility_bitfield2format(uint64_t bitfield)
  166. {
  167. switch (bitfield) {
  168. /*! G.723.1 compression */
  169. case AST_FORMAT_G723:
  170. return ast_format_g723;
  171. /*! GSM compression */
  172. case AST_FORMAT_GSM:
  173. return ast_format_gsm;
  174. /*! Raw mu-law data (G.711) */
  175. case AST_FORMAT_ULAW:
  176. return ast_format_ulaw;
  177. /*! Raw A-law data (G.711) */
  178. case AST_FORMAT_ALAW:
  179. return ast_format_alaw;
  180. /*! ADPCM (G.726, 32kbps, AAL2 codeword packing) */
  181. case AST_FORMAT_G726_AAL2:
  182. return ast_format_g726_aal2;
  183. /*! ADPCM (IMA) */
  184. case AST_FORMAT_ADPCM:
  185. return ast_format_adpcm;
  186. /*! Raw 16-bit Signed Linear (8000 Hz) PCM */
  187. case AST_FORMAT_SLIN:
  188. return ast_format_slin;
  189. /*! LPC10, 180 samples/frame */
  190. case AST_FORMAT_LPC10:
  191. return ast_format_lpc10;
  192. /*! G.729A audio */
  193. case AST_FORMAT_G729:
  194. return ast_format_g729;
  195. /*! SpeeX Free Compression */
  196. case AST_FORMAT_SPEEX:
  197. return ast_format_speex;
  198. /*! iLBC Free Compression */
  199. case AST_FORMAT_ILBC:
  200. return ast_format_ilbc;
  201. /*! ADPCM (G.726, 32kbps, RFC3551 codeword packing) */
  202. case AST_FORMAT_G726:
  203. return ast_format_g726;
  204. /*! G.722 */
  205. case AST_FORMAT_G722:
  206. return ast_format_g722;
  207. /*! G.722.1 (also known as Siren7, 32kbps assumed) */
  208. case AST_FORMAT_SIREN7:
  209. return ast_format_siren7;
  210. /*! G.722.1 Annex C (also known as Siren14, 48kbps assumed) */
  211. case AST_FORMAT_SIREN14:
  212. return ast_format_siren14;
  213. /*! Raw 16-bit Signed Linear (16000 Hz) PCM */
  214. case AST_FORMAT_SLIN16:
  215. return ast_format_slin16;
  216. /*! G.719 (64 kbps assumed) */
  217. case AST_FORMAT_G719:
  218. return ast_format_g719;
  219. /*! SpeeX Wideband (16kHz) Free Compression */
  220. case AST_FORMAT_SPEEX16:
  221. return ast_format_speex16;
  222. /*! Opus audio (8kHz, 16kHz, 24kHz, 48Khz) */
  223. case AST_FORMAT_OPUS:
  224. return ast_format_opus;
  225. /*! Raw mu-law data (G.711) */
  226. case AST_FORMAT_TESTLAW:
  227. return ast_format_testlaw;
  228. /*! H.261 Video */
  229. case AST_FORMAT_H261:
  230. return ast_format_h261;
  231. /*! H.263 Video */
  232. case AST_FORMAT_H263:
  233. return ast_format_h263;
  234. /*! H.263+ Video */
  235. case AST_FORMAT_H263P:
  236. return ast_format_h263p;
  237. /*! H.264 Video */
  238. case AST_FORMAT_H264:
  239. return ast_format_h264;
  240. /*! MPEG4 Video */
  241. case AST_FORMAT_MP4:
  242. return ast_format_mp4;
  243. /*! VP8 Video */
  244. case AST_FORMAT_VP8:
  245. return ast_format_vp8;
  246. /*! JPEG Images */
  247. case AST_FORMAT_JPEG:
  248. return ast_format_jpeg;
  249. /*! PNG Images */
  250. case AST_FORMAT_PNG:
  251. return ast_format_png;
  252. /*! T.140 RED Text format RFC 4103 */
  253. case AST_FORMAT_T140_RED:
  254. return ast_format_t140_red;
  255. /*! T.140 Text format - ITU T.140, RFC 4103 */
  256. case AST_FORMAT_T140:
  257. return ast_format_t140;
  258. }
  259. return NULL;
  260. }