codec_builtin.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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 Built-in supported codecs
  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_cache.h"
  34. #include "asterisk/frame.h"
  35. #include "asterisk/smoother.h"
  36. int __ast_codec_register_with_format(struct ast_codec *codec, const char *format_name,
  37. struct ast_module *mod);
  38. enum frame_type {
  39. TYPE_HIGH, /* 0x0 */
  40. TYPE_LOW, /* 0x1 */
  41. TYPE_SILENCE, /* 0x2 */
  42. TYPE_DONTSEND /* 0x3 */
  43. };
  44. #define TYPE_MASK 0x3
  45. static int g723_len(unsigned char buf)
  46. {
  47. enum frame_type type = buf & TYPE_MASK;
  48. switch(type) {
  49. case TYPE_DONTSEND:
  50. return 0;
  51. break;
  52. case TYPE_SILENCE:
  53. return 4;
  54. break;
  55. case TYPE_HIGH:
  56. return 24;
  57. break;
  58. case TYPE_LOW:
  59. return 20;
  60. break;
  61. default:
  62. ast_log(LOG_WARNING, "Badly encoded frame (%u)\n", type);
  63. }
  64. return -1;
  65. }
  66. static int g723_samples(struct ast_frame *frame)
  67. {
  68. unsigned char *buf = frame->data.ptr;
  69. int pos = 0, samples = 0, res;
  70. while(pos < frame->datalen) {
  71. res = g723_len(buf[pos]);
  72. if (res <= 0)
  73. break;
  74. samples += 240;
  75. pos += res;
  76. }
  77. return samples;
  78. }
  79. static int g723_length(unsigned int samples)
  80. {
  81. return (samples / 240) * 20;
  82. }
  83. static struct ast_codec g723 = {
  84. .name = "g723",
  85. .description = "G.723.1",
  86. .type = AST_MEDIA_TYPE_AUDIO,
  87. .sample_rate = 8000,
  88. .minimum_ms = 30,
  89. .maximum_ms = 300,
  90. .default_ms = 30,
  91. .minimum_bytes = 20,
  92. .samples_count = g723_samples,
  93. .get_length = g723_length,
  94. };
  95. static int none_samples(struct ast_frame *frame)
  96. {
  97. return frame->datalen;
  98. }
  99. static int none_length(unsigned int samples) {
  100. return samples;
  101. }
  102. static struct ast_codec none = {
  103. .name = "none",
  104. .description = "<Null> codec",
  105. .type = AST_MEDIA_TYPE_AUDIO,
  106. .sample_rate = 8000, /* This must have some sample rate to prevent divide by 0 */
  107. .minimum_ms = 10,
  108. .maximum_ms = 150,
  109. .default_ms = 20,
  110. .minimum_bytes = 20,
  111. .samples_count = none_samples,
  112. .get_length = none_length,
  113. };
  114. static int ulaw_samples(struct ast_frame *frame)
  115. {
  116. return frame->datalen;
  117. }
  118. static int ulaw_length(unsigned int samples)
  119. {
  120. return samples;
  121. }
  122. static struct ast_codec ulaw = {
  123. .name = "ulaw",
  124. .description = "G.711 u-law",
  125. .type = AST_MEDIA_TYPE_AUDIO,
  126. .sample_rate = 8000,
  127. .minimum_ms = 10,
  128. .maximum_ms = 150,
  129. .default_ms = 20,
  130. .minimum_bytes = 80,
  131. .samples_count = ulaw_samples,
  132. .get_length = ulaw_length,
  133. .smooth = 1,
  134. };
  135. static struct ast_codec alaw = {
  136. .name = "alaw",
  137. .description = "G.711 a-law",
  138. .type = AST_MEDIA_TYPE_AUDIO,
  139. .sample_rate = 8000,
  140. .minimum_ms = 10,
  141. .maximum_ms = 150,
  142. .default_ms = 20,
  143. .minimum_bytes = 80,
  144. .samples_count = ulaw_samples,
  145. .get_length = ulaw_length,
  146. .smooth = 1,
  147. };
  148. static int gsm_samples(struct ast_frame *frame)
  149. {
  150. return 160 * (frame->datalen / 33);
  151. }
  152. static int gsm_length(unsigned int samples)
  153. {
  154. return (samples / 160) * 33;
  155. }
  156. static struct ast_codec gsm = {
  157. .name = "gsm",
  158. .description = "GSM",
  159. .type = AST_MEDIA_TYPE_AUDIO,
  160. .sample_rate = 8000,
  161. .minimum_ms = 20,
  162. .maximum_ms = 300,
  163. .default_ms = 20,
  164. .minimum_bytes = 33,
  165. .samples_count = gsm_samples,
  166. .get_length = gsm_length,
  167. .smooth = 1,
  168. };
  169. static int g726_samples(struct ast_frame *frame)
  170. {
  171. return frame->datalen * 2;
  172. }
  173. static int g726_length(unsigned int samples)
  174. {
  175. return samples / 2;
  176. }
  177. static struct ast_codec g726rfc3551 = {
  178. .name = "g726",
  179. .description = "G.726 RFC3551",
  180. .type = AST_MEDIA_TYPE_AUDIO,
  181. .sample_rate = 8000,
  182. .minimum_ms = 10,
  183. .maximum_ms = 300,
  184. .default_ms = 20,
  185. .minimum_bytes = 40,
  186. .samples_count = g726_samples,
  187. .get_length = g726_length,
  188. .smooth = 1,
  189. };
  190. static struct ast_codec g726aal2 = {
  191. .name = "g726aal2",
  192. .description = "G.726 AAL2",
  193. .type = AST_MEDIA_TYPE_AUDIO,
  194. .sample_rate = 8000,
  195. .minimum_ms = 10,
  196. .maximum_ms = 300,
  197. .default_ms = 20,
  198. .minimum_bytes = 40,
  199. .samples_count = g726_samples,
  200. .get_length = g726_length,
  201. .smooth = 1,
  202. };
  203. static struct ast_codec adpcm = {
  204. .name = "adpcm",
  205. .description = "Dialogic ADPCM",
  206. .type = AST_MEDIA_TYPE_AUDIO,
  207. .sample_rate = 8000,
  208. .minimum_ms = 10,
  209. .maximum_ms = 300,
  210. .default_ms = 20,
  211. .minimum_bytes = 40,
  212. .samples_count = g726_samples,
  213. .get_length = g726_length,
  214. .smooth = 1,
  215. };
  216. static int slin_samples(struct ast_frame *frame)
  217. {
  218. return frame->datalen / 2;
  219. }
  220. static int slin_length(unsigned int samples)
  221. {
  222. return samples * 2;
  223. }
  224. static struct ast_codec slin8 = {
  225. .name = "slin",
  226. .description = "16 bit Signed Linear PCM",
  227. .type = AST_MEDIA_TYPE_AUDIO,
  228. .sample_rate = 8000,
  229. .minimum_ms = 10,
  230. .maximum_ms = 70,
  231. .default_ms = 20,
  232. .minimum_bytes = 160,
  233. .samples_count = slin_samples,
  234. .get_length = slin_length,
  235. .smooth = AST_SMOOTHER_FLAGS_PACK(AST_SMOOTHER_FLAG_BE | AST_SMOOTHER_FLAG_FORCED),
  236. };
  237. static struct ast_codec slin12 = {
  238. .name = "slin",
  239. .description = "16 bit Signed Linear PCM (12kHz)",
  240. .type = AST_MEDIA_TYPE_AUDIO,
  241. .sample_rate = 12000,
  242. .minimum_ms = 10,
  243. .maximum_ms = 70,
  244. .default_ms = 20,
  245. .minimum_bytes = 240,
  246. .samples_count = slin_samples,
  247. .get_length = slin_length,
  248. .smooth = AST_SMOOTHER_FLAGS_PACK(AST_SMOOTHER_FLAG_BE | AST_SMOOTHER_FLAG_FORCED),
  249. };
  250. static struct ast_codec slin16 = {
  251. .name = "slin",
  252. .description = "16 bit Signed Linear PCM (16kHz)",
  253. .type = AST_MEDIA_TYPE_AUDIO,
  254. .sample_rate = 16000,
  255. .minimum_ms = 10,
  256. .maximum_ms = 70,
  257. .default_ms = 20,
  258. .minimum_bytes = 320,
  259. .samples_count = slin_samples,
  260. .get_length = slin_length,
  261. .smooth = AST_SMOOTHER_FLAGS_PACK(AST_SMOOTHER_FLAG_BE | AST_SMOOTHER_FLAG_FORCED),
  262. };
  263. static struct ast_codec slin24 = {
  264. .name = "slin",
  265. .description = "16 bit Signed Linear PCM (24kHz)",
  266. .type = AST_MEDIA_TYPE_AUDIO,
  267. .sample_rate = 24000,
  268. .minimum_ms = 10,
  269. .maximum_ms = 70,
  270. .default_ms = 20,
  271. .minimum_bytes = 480,
  272. .samples_count = slin_samples,
  273. .get_length = slin_length,
  274. .smooth = AST_SMOOTHER_FLAGS_PACK(AST_SMOOTHER_FLAG_BE | AST_SMOOTHER_FLAG_FORCED),
  275. };
  276. static struct ast_codec slin32 = {
  277. .name = "slin",
  278. .description = "16 bit Signed Linear PCM (32kHz)",
  279. .type = AST_MEDIA_TYPE_AUDIO,
  280. .sample_rate = 32000,
  281. .minimum_ms = 10,
  282. .maximum_ms = 70,
  283. .default_ms = 20,
  284. .minimum_bytes = 640,
  285. .samples_count = slin_samples,
  286. .get_length = slin_length,
  287. .smooth = AST_SMOOTHER_FLAGS_PACK(AST_SMOOTHER_FLAG_BE | AST_SMOOTHER_FLAG_FORCED),
  288. };
  289. static struct ast_codec slin44 = {
  290. .name = "slin",
  291. .description = "16 bit Signed Linear PCM (44kHz)",
  292. .type = AST_MEDIA_TYPE_AUDIO,
  293. .sample_rate = 44100,
  294. .minimum_ms = 10,
  295. .maximum_ms = 70,
  296. .default_ms = 20,
  297. .minimum_bytes = 882,
  298. .samples_count = slin_samples,
  299. .get_length = slin_length,
  300. .smooth = AST_SMOOTHER_FLAGS_PACK(AST_SMOOTHER_FLAG_BE | AST_SMOOTHER_FLAG_FORCED),
  301. };
  302. static struct ast_codec slin48 = {
  303. .name = "slin",
  304. .description = "16 bit Signed Linear PCM (48kHz)",
  305. .type = AST_MEDIA_TYPE_AUDIO,
  306. .sample_rate = 48000,
  307. .minimum_ms = 10,
  308. .maximum_ms = 70,
  309. .default_ms = 20,
  310. .minimum_bytes = 960,
  311. .samples_count = slin_samples,
  312. .get_length = slin_length,
  313. .smooth = AST_SMOOTHER_FLAGS_PACK(AST_SMOOTHER_FLAG_BE | AST_SMOOTHER_FLAG_FORCED),
  314. };
  315. static struct ast_codec slin96 = {
  316. .name = "slin",
  317. .description = "16 bit Signed Linear PCM (96kHz)",
  318. .type = AST_MEDIA_TYPE_AUDIO,
  319. .sample_rate = 96000,
  320. .minimum_ms = 10,
  321. .maximum_ms = 70,
  322. .default_ms = 20,
  323. .minimum_bytes = 1920,
  324. .samples_count = slin_samples,
  325. .get_length = slin_length,
  326. .smooth = AST_SMOOTHER_FLAGS_PACK(AST_SMOOTHER_FLAG_BE | AST_SMOOTHER_FLAG_FORCED),
  327. };
  328. static struct ast_codec slin192 = {
  329. .name = "slin",
  330. .description = "16 bit Signed Linear PCM (192kHz)",
  331. .type = AST_MEDIA_TYPE_AUDIO,
  332. .sample_rate = 192000,
  333. .minimum_ms = 10,
  334. .maximum_ms = 70,
  335. .default_ms = 20,
  336. .minimum_bytes = 3840,
  337. .samples_count = slin_samples,
  338. .get_length = slin_length,
  339. .smooth = AST_SMOOTHER_FLAGS_PACK(AST_SMOOTHER_FLAG_BE | AST_SMOOTHER_FLAG_FORCED),
  340. };
  341. static int lpc10_samples(struct ast_frame *frame)
  342. {
  343. int samples = 22 * 8;
  344. /* assumes that the RTP packet contains one LPC10 frame */
  345. samples += (((char *)(frame->data.ptr))[7] & 0x1) * 8;
  346. return samples;
  347. }
  348. static struct ast_codec lpc10 = {
  349. .name = "lpc10",
  350. .description = "LPC10",
  351. .type = AST_MEDIA_TYPE_AUDIO,
  352. .sample_rate = 8000,
  353. .minimum_ms = 20,
  354. .maximum_ms = 20,
  355. .default_ms = 20,
  356. .minimum_bytes = 7,
  357. .samples_count = lpc10_samples,
  358. .smooth = 1,
  359. };
  360. static int g729_samples(struct ast_frame *frame)
  361. {
  362. return frame->datalen * 8;
  363. }
  364. static int g729_length(unsigned int samples)
  365. {
  366. return samples / 8;
  367. }
  368. static struct ast_codec g729a = {
  369. .name = "g729",
  370. .description = "G.729A",
  371. .type = AST_MEDIA_TYPE_AUDIO,
  372. .sample_rate = 8000,
  373. .minimum_ms = 10,
  374. .maximum_ms = 230,
  375. .default_ms = 20,
  376. .minimum_bytes = 10,
  377. .samples_count = g729_samples,
  378. .get_length = g729_length,
  379. .smooth = 1,
  380. };
  381. static unsigned char get_n_bits_at(unsigned char *data, int n, int bit)
  382. {
  383. int byte = bit / 8; /* byte containing first bit */
  384. int rem = 8 - (bit % 8); /* remaining bits in first byte */
  385. unsigned char ret = 0;
  386. if (n <= 0 || n > 8)
  387. return 0;
  388. if (rem < n) {
  389. ret = (data[byte] << (n - rem));
  390. ret |= (data[byte + 1] >> (8 - n + rem));
  391. } else {
  392. ret = (data[byte] >> (rem - n));
  393. }
  394. return (ret & (0xff >> (8 - n)));
  395. }
  396. static int speex_get_wb_sz_at(unsigned char *data, int len, int bit)
  397. {
  398. static const int SpeexWBSubModeSz[] = {
  399. 4, 36, 112, 192,
  400. 352, 0, 0, 0 };
  401. int off = bit;
  402. unsigned char c;
  403. /* skip up to two wideband frames */
  404. if (((len * 8 - off) >= 5) &&
  405. get_n_bits_at(data, 1, off)) {
  406. c = get_n_bits_at(data, 3, off + 1);
  407. off += SpeexWBSubModeSz[c];
  408. if (((len * 8 - off) >= 5) &&
  409. get_n_bits_at(data, 1, off)) {
  410. c = get_n_bits_at(data, 3, off + 1);
  411. off += SpeexWBSubModeSz[c];
  412. if (((len * 8 - off) >= 5) &&
  413. get_n_bits_at(data, 1, off)) {
  414. ast_log(LOG_WARNING, "Encountered corrupt speex frame; too many wideband frames in a row.\n");
  415. return -1;
  416. }
  417. }
  418. }
  419. return off - bit;
  420. }
  421. static int speex_samples(unsigned char *data, int len)
  422. {
  423. static const int SpeexSubModeSz[] = {
  424. 5, 43, 119, 160,
  425. 220, 300, 364, 492,
  426. 79, 0, 0, 0,
  427. 0, 0, 0, 0 };
  428. static const int SpeexInBandSz[] = {
  429. 1, 1, 4, 4,
  430. 4, 4, 4, 4,
  431. 8, 8, 16, 16,
  432. 32, 32, 64, 64 };
  433. int bit = 0;
  434. int cnt = 0;
  435. int off;
  436. unsigned char c;
  437. while ((len * 8 - bit) >= 5) {
  438. /* skip wideband frames */
  439. off = speex_get_wb_sz_at(data, len, bit);
  440. if (off < 0) {
  441. ast_log(LOG_WARNING, "Had error while reading wideband frames for speex samples\n");
  442. break;
  443. }
  444. bit += off;
  445. if ((len * 8 - bit) < 5)
  446. break;
  447. /* get control bits */
  448. c = get_n_bits_at(data, 5, bit);
  449. bit += 5;
  450. if (c == 15) {
  451. /* terminator */
  452. break;
  453. } else if (c == 14) {
  454. /* in-band signal; next 4 bits contain signal id */
  455. c = get_n_bits_at(data, 4, bit);
  456. bit += 4;
  457. bit += SpeexInBandSz[c];
  458. } else if (c == 13) {
  459. /* user in-band; next 4 bits contain msg len */
  460. c = get_n_bits_at(data, 4, bit);
  461. bit += 4;
  462. /* after which it's 5-bit signal id + c bytes of data */
  463. bit += 5 + c * 8;
  464. } else if (c > 8) {
  465. /* unknown */
  466. ast_log(LOG_WARNING, "Unknown speex control frame %d\n", c);
  467. break;
  468. } else {
  469. /* skip number bits for submode (less the 5 control bits) */
  470. bit += SpeexSubModeSz[c] - 5;
  471. cnt += 160; /* new frame */
  472. }
  473. }
  474. return cnt;
  475. }
  476. static int speex8_samples(struct ast_frame *frame)
  477. {
  478. return speex_samples(frame->data.ptr, frame->datalen);
  479. }
  480. static struct ast_codec speex8 = {
  481. .name = "speex",
  482. .description = "SpeeX",
  483. .type = AST_MEDIA_TYPE_AUDIO,
  484. .sample_rate = 8000,
  485. .minimum_ms = 10,
  486. .maximum_ms = 60,
  487. .default_ms = 20,
  488. .minimum_bytes = 10,
  489. .samples_count = speex8_samples,
  490. };
  491. static int speex16_samples(struct ast_frame *frame)
  492. {
  493. return 2 * speex_samples(frame->data.ptr, frame->datalen);
  494. }
  495. static struct ast_codec speex16 = {
  496. .name = "speex",
  497. .description = "SpeeX 16khz",
  498. .type = AST_MEDIA_TYPE_AUDIO,
  499. .sample_rate = 16000,
  500. .minimum_ms = 10,
  501. .maximum_ms = 60,
  502. .default_ms = 20,
  503. .minimum_bytes = 10,
  504. .samples_count = speex16_samples,
  505. };
  506. static int speex32_samples(struct ast_frame *frame)
  507. {
  508. return 4 * speex_samples(frame->data.ptr, frame->datalen);
  509. }
  510. static struct ast_codec speex32 = {
  511. .name = "speex",
  512. .description = "SpeeX 32khz",
  513. .type = AST_MEDIA_TYPE_AUDIO,
  514. .sample_rate = 32000,
  515. .minimum_ms = 10,
  516. .maximum_ms = 60,
  517. .default_ms = 20,
  518. .minimum_bytes = 10,
  519. .samples_count = speex32_samples,
  520. };
  521. static int ilbc_samples(struct ast_frame *frame)
  522. {
  523. return 240 * (frame->datalen / 50);
  524. }
  525. static struct ast_codec ilbc = {
  526. .name = "ilbc",
  527. .description = "iLBC",
  528. .type = AST_MEDIA_TYPE_AUDIO,
  529. .sample_rate = 8000,
  530. .minimum_ms = 30,
  531. .maximum_ms = 300,
  532. .default_ms = 30,
  533. .minimum_bytes = 50,
  534. .samples_count = ilbc_samples,
  535. .smooth = 1,
  536. };
  537. static struct ast_codec g722 = {
  538. .name = "g722",
  539. .description = "G722",
  540. .type = AST_MEDIA_TYPE_AUDIO,
  541. .sample_rate = 16000,
  542. .minimum_ms = 10,
  543. .maximum_ms = 150,
  544. .default_ms = 20,
  545. .minimum_bytes = 80,
  546. .samples_count = g726_samples,
  547. .get_length = g726_length,
  548. .smooth = 1,
  549. };
  550. static int siren7_samples(struct ast_frame *frame)
  551. {
  552. return frame->datalen * (16000 / 4000);
  553. }
  554. static int siren7_length(unsigned int samples)
  555. {
  556. return samples / (16000 / 4000);
  557. }
  558. static struct ast_codec siren7 = {
  559. .name = "siren7",
  560. .description = "ITU G.722.1 (Siren7, licensed from Polycom)",
  561. .type = AST_MEDIA_TYPE_AUDIO,
  562. .sample_rate = 16000,
  563. .minimum_ms = 20,
  564. .maximum_ms = 80,
  565. .default_ms = 20,
  566. .minimum_bytes = 80,
  567. .samples_count = siren7_samples,
  568. .get_length = siren7_length,
  569. };
  570. static int siren14_samples(struct ast_frame *frame)
  571. {
  572. return (int) frame->datalen * ((float) 32000 / 6000);
  573. }
  574. static int siren14_length(unsigned int samples)
  575. {
  576. return (int) samples / ((float) 32000 / 6000);;
  577. }
  578. static struct ast_codec siren14 = {
  579. .name = "siren14",
  580. .description = "ITU G.722.1 Annex C, (Siren14, licensed from Polycom)",
  581. .type = AST_MEDIA_TYPE_AUDIO,
  582. .sample_rate = 32000,
  583. .minimum_ms = 20,
  584. .maximum_ms = 80,
  585. .default_ms = 20,
  586. .minimum_bytes = 120,
  587. .samples_count = siren14_samples,
  588. .get_length = siren14_length,
  589. };
  590. static struct ast_codec testlaw = {
  591. .name = "testlaw",
  592. .description = "G.711 test-law",
  593. .type = AST_MEDIA_TYPE_AUDIO,
  594. .sample_rate = 8000,
  595. .minimum_ms = 10,
  596. .maximum_ms = 150,
  597. .default_ms = 20,
  598. .minimum_bytes = 80,
  599. .samples_count = ulaw_samples,
  600. .get_length = ulaw_length,
  601. .smooth = 1,
  602. };
  603. static int g719_samples(struct ast_frame *frame)
  604. {
  605. return (int) frame->datalen * ((float) 48000 / 8000);
  606. }
  607. static int g719_length(unsigned int samples)
  608. {
  609. return (int) samples / ((float) 48000 / 8000);
  610. }
  611. static struct ast_codec g719 = {
  612. .name = "g719",
  613. .description = "ITU G.719",
  614. .type = AST_MEDIA_TYPE_AUDIO,
  615. .sample_rate = 48000,
  616. .minimum_ms = 20,
  617. .maximum_ms = 80,
  618. .default_ms = 20,
  619. .minimum_bytes = 160,
  620. .samples_count = g719_samples,
  621. .get_length = g719_length,
  622. };
  623. static int opus_samples(struct ast_frame *frame)
  624. {
  625. /*
  626. * XXX This is likely not at all what's intended from this
  627. * callback. If you have codec_opus.so loaded then this
  628. * function is overridden anyway. However, since opus is
  629. * variable bit rate and I cannot extract the calculation code
  630. * from the opus library, I am going to punt and assume 20ms
  631. * worth of samples. In testing, this has worked just fine.
  632. * Pass through support doesn't seem to care about the value
  633. * returned anyway.
  634. */
  635. return ast_format_get_sample_rate(frame->subclass.format) / 50;
  636. }
  637. static struct ast_codec opus = {
  638. .name = "opus",
  639. .description = "Opus Codec",
  640. .type = AST_MEDIA_TYPE_AUDIO,
  641. .sample_rate = 48000,
  642. .minimum_ms = 20,
  643. .maximum_ms = 60,
  644. .default_ms = 20,
  645. .samples_count = opus_samples,
  646. .minimum_bytes = 10,
  647. };
  648. static struct ast_codec jpeg = {
  649. .name = "jpeg",
  650. .description = "JPEG image",
  651. .type = AST_MEDIA_TYPE_IMAGE,
  652. };
  653. static struct ast_codec png = {
  654. .name = "png",
  655. .description = "PNG Image",
  656. .type = AST_MEDIA_TYPE_IMAGE,
  657. };
  658. static struct ast_codec h261 = {
  659. .name = "h261",
  660. .description = "H.261 video",
  661. .type = AST_MEDIA_TYPE_VIDEO,
  662. .sample_rate = 1000,
  663. };
  664. static struct ast_codec h263 = {
  665. .name = "h263",
  666. .description = "H.263 video",
  667. .type = AST_MEDIA_TYPE_VIDEO,
  668. .sample_rate = 1000,
  669. };
  670. static struct ast_codec h263p = {
  671. .name = "h263p",
  672. .description = "H.263+ video",
  673. .type = AST_MEDIA_TYPE_VIDEO,
  674. .sample_rate = 1000,
  675. };
  676. static struct ast_codec h264 = {
  677. .name = "h264",
  678. .description = "H.264 video",
  679. .type = AST_MEDIA_TYPE_VIDEO,
  680. .sample_rate = 1000,
  681. };
  682. static struct ast_codec mpeg4 = {
  683. .name = "mpeg4",
  684. .description = "MPEG4 video",
  685. .type = AST_MEDIA_TYPE_VIDEO,
  686. .sample_rate = 1000,
  687. };
  688. static struct ast_codec vp8 = {
  689. .name = "vp8",
  690. .description = "VP8 video",
  691. .type = AST_MEDIA_TYPE_VIDEO,
  692. .sample_rate = 1000,
  693. };
  694. static struct ast_codec vp9 = {
  695. .name = "vp9",
  696. .description = "VP9 video",
  697. .type = AST_MEDIA_TYPE_VIDEO,
  698. .sample_rate = 1000,
  699. };
  700. static struct ast_codec t140red = {
  701. .name = "red",
  702. .description = "T.140 Realtime Text with redundancy",
  703. .type = AST_MEDIA_TYPE_TEXT,
  704. };
  705. static struct ast_codec t140 = {
  706. .name = "t140",
  707. .description = "Passthrough T.140 Realtime Text",
  708. .type = AST_MEDIA_TYPE_TEXT,
  709. };
  710. static int silk_samples(struct ast_frame *frame)
  711. {
  712. /* XXX This is likely not at all what's intended from this callback. However,
  713. * since SILK is variable bit rate, I have no idea how to take a frame of data
  714. * and determine the number of samples present. Instead, we base this on the
  715. * sample rate of the codec and the expected number of samples to receive in 20ms.
  716. * In testing, this has worked just fine.
  717. */
  718. return ast_format_get_sample_rate(frame->subclass.format) / 50;
  719. }
  720. static struct ast_codec silk8 = {
  721. .name = "silk",
  722. .description = "SILK Codec (8 KHz)",
  723. .type = AST_MEDIA_TYPE_AUDIO,
  724. .sample_rate = 8000,
  725. .minimum_ms = 20,
  726. .maximum_ms = 100,
  727. .default_ms = 20,
  728. .minimum_bytes = 160,
  729. .samples_count = silk_samples
  730. };
  731. static struct ast_codec silk12 = {
  732. .name = "silk",
  733. .description = "SILK Codec (12 KHz)",
  734. .type = AST_MEDIA_TYPE_AUDIO,
  735. .sample_rate = 12000,
  736. .minimum_ms = 20,
  737. .maximum_ms = 100,
  738. .default_ms = 20,
  739. .minimum_bytes = 240,
  740. .samples_count = silk_samples
  741. };
  742. static struct ast_codec silk16 = {
  743. .name = "silk",
  744. .description = "SILK Codec (16 KHz)",
  745. .type = AST_MEDIA_TYPE_AUDIO,
  746. .sample_rate = 16000,
  747. .minimum_ms = 20,
  748. .maximum_ms = 100,
  749. .default_ms = 20,
  750. .minimum_bytes = 320,
  751. .samples_count = silk_samples
  752. };
  753. static struct ast_codec silk24 = {
  754. .name = "silk",
  755. .description = "SILK Codec (24 KHz)",
  756. .type = AST_MEDIA_TYPE_AUDIO,
  757. .sample_rate = 24000,
  758. .minimum_ms = 20,
  759. .maximum_ms = 100,
  760. .default_ms = 20,
  761. .minimum_bytes = 480,
  762. .samples_count = silk_samples
  763. };
  764. #define CODEC_REGISTER_AND_CACHE(codec) \
  765. ({ \
  766. int __res_ ## __LINE__ = 0; \
  767. struct ast_format *__fmt_ ## __LINE__; \
  768. struct ast_codec *__codec_ ## __LINE__; \
  769. res |= __ast_codec_register_with_format(&(codec), (codec).name, NULL); \
  770. __codec_ ## __LINE__ = ast_codec_get((codec).name, (codec).type, (codec).sample_rate); \
  771. __fmt_ ## __LINE__ = __codec_ ## __LINE__ ? ast_format_create(__codec_ ## __LINE__) : NULL; \
  772. res |= ast_format_cache_set(__fmt_ ## __LINE__); \
  773. ao2_ref(__fmt_ ## __LINE__, -1); \
  774. ao2_ref(__codec_ ## __LINE__, -1); \
  775. __res_ ## __LINE__; \
  776. })
  777. #define CODEC_REGISTER_AND_CACHE_NAMED(fmt_name, codec) \
  778. ({ \
  779. int __res_ ## __LINE__ = 0; \
  780. struct ast_format *__fmt_ ## __LINE__; \
  781. struct ast_codec *__codec_ ## __LINE__; \
  782. res |= __ast_codec_register_with_format(&(codec), fmt_name, NULL); \
  783. __codec_ ## __LINE__ = ast_codec_get((codec).name, (codec).type, (codec).sample_rate); \
  784. __fmt_ ## __LINE__ = ast_format_create_named((fmt_name), __codec_ ## __LINE__); \
  785. res |= ast_format_cache_set(__fmt_ ## __LINE__); \
  786. ao2_ref(__fmt_ ## __LINE__, -1); \
  787. ao2_ref(__codec_ ## __LINE__, -1); \
  788. __res_ ## __LINE__; \
  789. })
  790. int ast_codec_builtin_init(void)
  791. {
  792. int res = 0;
  793. res |= CODEC_REGISTER_AND_CACHE(g723);
  794. res |= CODEC_REGISTER_AND_CACHE(ulaw);
  795. res |= CODEC_REGISTER_AND_CACHE(alaw);
  796. res |= CODEC_REGISTER_AND_CACHE(gsm);
  797. res |= CODEC_REGISTER_AND_CACHE(g726rfc3551);
  798. res |= CODEC_REGISTER_AND_CACHE(g726aal2);
  799. res |= CODEC_REGISTER_AND_CACHE(adpcm);
  800. res |= CODEC_REGISTER_AND_CACHE(slin8);
  801. res |= CODEC_REGISTER_AND_CACHE_NAMED("slin12", slin12);
  802. res |= CODEC_REGISTER_AND_CACHE_NAMED("slin16", slin16);
  803. res |= CODEC_REGISTER_AND_CACHE_NAMED("slin24", slin24);
  804. res |= CODEC_REGISTER_AND_CACHE_NAMED("slin32", slin32);
  805. res |= CODEC_REGISTER_AND_CACHE_NAMED("slin44", slin44);
  806. res |= CODEC_REGISTER_AND_CACHE_NAMED("slin48", slin48);
  807. res |= CODEC_REGISTER_AND_CACHE_NAMED("slin96", slin96);
  808. res |= CODEC_REGISTER_AND_CACHE_NAMED("slin192", slin192);
  809. res |= CODEC_REGISTER_AND_CACHE(lpc10);
  810. res |= CODEC_REGISTER_AND_CACHE(g729a);
  811. res |= CODEC_REGISTER_AND_CACHE(speex8);
  812. res |= CODEC_REGISTER_AND_CACHE_NAMED("speex16", speex16);
  813. res |= CODEC_REGISTER_AND_CACHE_NAMED("speex32", speex32);
  814. res |= CODEC_REGISTER_AND_CACHE(ilbc);
  815. res |= CODEC_REGISTER_AND_CACHE(g722);
  816. res |= CODEC_REGISTER_AND_CACHE(siren7);
  817. res |= CODEC_REGISTER_AND_CACHE(siren14);
  818. res |= CODEC_REGISTER_AND_CACHE(testlaw);
  819. res |= CODEC_REGISTER_AND_CACHE(g719);
  820. res |= CODEC_REGISTER_AND_CACHE(opus);
  821. res |= CODEC_REGISTER_AND_CACHE(jpeg);
  822. res |= CODEC_REGISTER_AND_CACHE(png);
  823. res |= CODEC_REGISTER_AND_CACHE(h261);
  824. res |= CODEC_REGISTER_AND_CACHE(h263);
  825. res |= CODEC_REGISTER_AND_CACHE(h263p);
  826. res |= CODEC_REGISTER_AND_CACHE(h264);
  827. res |= CODEC_REGISTER_AND_CACHE(mpeg4);
  828. res |= CODEC_REGISTER_AND_CACHE(vp8);
  829. res |= CODEC_REGISTER_AND_CACHE(vp9);
  830. res |= CODEC_REGISTER_AND_CACHE(t140red);
  831. res |= CODEC_REGISTER_AND_CACHE(t140);
  832. res |= CODEC_REGISTER_AND_CACHE(none);
  833. res |= CODEC_REGISTER_AND_CACHE_NAMED("silk8", silk8);
  834. res |= CODEC_REGISTER_AND_CACHE_NAMED("silk12", silk12);
  835. res |= CODEC_REGISTER_AND_CACHE_NAMED("silk16", silk16);
  836. res |= CODEC_REGISTER_AND_CACHE_NAMED("silk24", silk24);
  837. return res;
  838. }