frame.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, 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. *
  20. * \brief Frame and codec manipulation routines
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include "asterisk/_private.h"
  27. #include "asterisk/lock.h"
  28. #include "asterisk/frame.h"
  29. #include "asterisk/channel.h"
  30. #include "asterisk/cli.h"
  31. #include "asterisk/term.h"
  32. #include "asterisk/utils.h"
  33. #include "asterisk/threadstorage.h"
  34. #include "asterisk/linkedlists.h"
  35. #include "asterisk/translate.h"
  36. #include "asterisk/dsp.h"
  37. #include "asterisk/file.h"
  38. #if !defined(LOW_MEMORY)
  39. static void frame_cache_cleanup(void *data);
  40. /*! \brief A per-thread cache of frame headers */
  41. AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
  42. /*!
  43. * \brief Maximum ast_frame cache size
  44. *
  45. * In most cases where the frame header cache will be useful, the size
  46. * of the cache will stay very small. However, it is not always the case that
  47. * the same thread that allocates the frame will be the one freeing them, so
  48. * sometimes a thread will never have any frames in its cache, or the cache
  49. * will never be pulled from. For the latter case, we limit the maximum size.
  50. */
  51. #define FRAME_CACHE_MAX_SIZE 10
  52. /*! \brief This is just so ast_frames, a list head struct for holding a list of
  53. * ast_frame structures, is defined. */
  54. AST_LIST_HEAD_NOLOCK(ast_frames, ast_frame);
  55. struct ast_frame_cache {
  56. struct ast_frames list;
  57. size_t size;
  58. };
  59. #endif
  60. #define SMOOTHER_SIZE 8000
  61. enum frame_type {
  62. TYPE_HIGH, /* 0x0 */
  63. TYPE_LOW, /* 0x1 */
  64. TYPE_SILENCE, /* 0x2 */
  65. TYPE_DONTSEND /* 0x3 */
  66. };
  67. #define TYPE_MASK 0x3
  68. struct ast_smoother {
  69. int size;
  70. int format;
  71. int optimizablestream;
  72. int flags;
  73. float samplesperbyte;
  74. unsigned int opt_needs_swap:1;
  75. struct ast_frame f;
  76. struct timeval delivery;
  77. char data[SMOOTHER_SIZE];
  78. char framedata[SMOOTHER_SIZE + AST_FRIENDLY_OFFSET];
  79. struct ast_frame *opt;
  80. int len;
  81. };
  82. /*! \brief Definition of supported media formats (codecs) */
  83. static struct ast_format_list AST_FORMAT_LIST[] = {
  84. { AST_FORMAT_G723_1 , "g723", 8000, "G.723.1", 20, 30, 300, 30, 30 }, /*!< G723.1 */
  85. { AST_FORMAT_GSM, "gsm", 8000, "GSM", 33, 20, 300, 20, 20 }, /*!< codec_gsm.c */
  86. { AST_FORMAT_ULAW, "ulaw", 8000, "G.711 u-law", 80, 10, 150, 10, 20 }, /*!< codec_ulaw.c */
  87. { AST_FORMAT_ALAW, "alaw", 8000, "G.711 A-law", 80, 10, 150, 10, 20 }, /*!< codec_alaw.c */
  88. { AST_FORMAT_G726, "g726", 8000, "G.726 RFC3551", 40, 10, 300, 10, 20 }, /*!< codec_g726.c */
  89. { AST_FORMAT_ADPCM, "adpcm" , 8000, "ADPCM", 40, 10, 300, 10, 20 }, /*!< codec_adpcm.c */
  90. { AST_FORMAT_SLINEAR, "slin", 8000, "16 bit Signed Linear PCM", 160, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE }, /*!< Signed linear */
  91. { AST_FORMAT_LPC10, "lpc10", 8000, "LPC10", 7, 20, 20, 20, 20 }, /*!< codec_lpc10.c */
  92. { AST_FORMAT_G729A, "g729", 8000, "G.729A", 10, 10, 230, 10, 20, AST_SMOOTHER_FLAG_G729 }, /*!< Binary commercial distribution */
  93. { AST_FORMAT_SPEEX, "speex", 8000, "SpeeX", 10, 10, 60, 10, 20 }, /*!< codec_speex.c */
  94. { AST_FORMAT_ILBC, "ilbc", 8000, "iLBC", 50, 30, 30, 30, 30 }, /*!< codec_ilbc.c */ /* inc=30ms - workaround */
  95. { AST_FORMAT_G726_AAL2, "g726aal2", 8000, "G.726 AAL2", 40, 10, 300, 10, 20 }, /*!< codec_g726.c */
  96. { AST_FORMAT_G722, "g722", 16000, "G722", 80, 10, 150, 10, 20 }, /*!< codec_g722.c */
  97. { AST_FORMAT_SLINEAR16, "slin16", 16000, "16 bit Signed Linear PCM (16kHz)", 320, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE }, /*!< Signed linear (16kHz) */
  98. { AST_FORMAT_JPEG, "jpeg", 0, "JPEG image"}, /*!< See format_jpeg.c */
  99. { AST_FORMAT_PNG, "png", 0, "PNG image"}, /*!< PNG Image format */
  100. { AST_FORMAT_H261, "h261", 0, "H.261 Video" }, /*!< H.261 Video Passthrough */
  101. { AST_FORMAT_H263, "h263", 0, "H.263 Video" }, /*!< H.263 Passthrough support, see format_h263.c */
  102. { AST_FORMAT_H263_PLUS, "h263p", 0, "H.263+ Video" }, /*!< H.263plus passthrough support See format_h263.c */
  103. { AST_FORMAT_H264, "h264", 0, "H.264 Video" }, /*!< Passthrough support, see format_h263.c */
  104. { AST_FORMAT_MP4_VIDEO, "mpeg4", 0, "MPEG4 Video" }, /*!< Passthrough support for MPEG4 */
  105. { AST_FORMAT_T140, "t140", 0, "Passthrough T.140 Realtime Text" }, /*!< Passthrough support for T.140 Realtime Text */
  106. };
  107. struct ast_frame ast_null_frame = { AST_FRAME_NULL, };
  108. static int smoother_frame_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
  109. {
  110. if (s->flags & AST_SMOOTHER_FLAG_G729) {
  111. if (s->len % 10) {
  112. ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
  113. return 0;
  114. }
  115. }
  116. if (swap) {
  117. ast_swapcopy_samples(s->data + s->len, f->data, f->samples);
  118. } else {
  119. memcpy(s->data + s->len, f->data, f->datalen);
  120. }
  121. /* If either side is empty, reset the delivery time */
  122. if (!s->len || ast_tvzero(f->delivery) || ast_tvzero(s->delivery)) { /* XXX really ? */
  123. s->delivery = f->delivery;
  124. }
  125. s->len += f->datalen;
  126. return 0;
  127. }
  128. void ast_smoother_reset(struct ast_smoother *s, int bytes)
  129. {
  130. memset(s, 0, sizeof(*s));
  131. s->size = bytes;
  132. }
  133. void ast_smoother_reconfigure(struct ast_smoother *s, int bytes)
  134. {
  135. /* if there is no change, then nothing to do */
  136. if (s->size == bytes) {
  137. return;
  138. }
  139. /* set the new desired output size */
  140. s->size = bytes;
  141. /* if there is no 'optimized' frame in the smoother,
  142. * then there is nothing left to do
  143. */
  144. if (!s->opt) {
  145. return;
  146. }
  147. /* there is an 'optimized' frame here at the old size,
  148. * but it must now be put into the buffer so the data
  149. * can be extracted at the new size
  150. */
  151. smoother_frame_feed(s, s->opt, s->opt_needs_swap);
  152. s->opt = NULL;
  153. }
  154. struct ast_smoother *ast_smoother_new(int size)
  155. {
  156. struct ast_smoother *s;
  157. if (size < 1)
  158. return NULL;
  159. if ((s = ast_malloc(sizeof(*s))))
  160. ast_smoother_reset(s, size);
  161. return s;
  162. }
  163. int ast_smoother_get_flags(struct ast_smoother *s)
  164. {
  165. return s->flags;
  166. }
  167. void ast_smoother_set_flags(struct ast_smoother *s, int flags)
  168. {
  169. s->flags = flags;
  170. }
  171. int ast_smoother_test_flag(struct ast_smoother *s, int flag)
  172. {
  173. return (s->flags & flag);
  174. }
  175. int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
  176. {
  177. if (f->frametype != AST_FRAME_VOICE) {
  178. ast_log(LOG_WARNING, "Huh? Can't smooth a non-voice frame!\n");
  179. return -1;
  180. }
  181. if (!s->format) {
  182. s->format = f->subclass;
  183. s->samplesperbyte = (float)f->samples / (float)f->datalen;
  184. } else if (s->format != f->subclass) {
  185. ast_log(LOG_WARNING, "Smoother was working on %d format frames, now trying to feed %d?\n", s->format, f->subclass);
  186. return -1;
  187. }
  188. if (s->len + f->datalen > SMOOTHER_SIZE) {
  189. ast_log(LOG_WARNING, "Out of smoother space\n");
  190. return -1;
  191. }
  192. if (((f->datalen == s->size) ||
  193. ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729))) &&
  194. !s->opt &&
  195. !s->len &&
  196. (f->offset >= AST_MIN_OFFSET)) {
  197. /* Optimize by sending the frame we just got
  198. on the next read, thus eliminating the douple
  199. copy */
  200. if (swap)
  201. ast_swapcopy_samples(f->data, f->data, f->samples);
  202. s->opt = f;
  203. s->opt_needs_swap = swap ? 1 : 0;
  204. return 0;
  205. }
  206. return smoother_frame_feed(s, f, swap);
  207. }
  208. struct ast_frame *ast_smoother_read(struct ast_smoother *s)
  209. {
  210. struct ast_frame *opt;
  211. int len;
  212. /* IF we have an optimization frame, send it */
  213. if (s->opt) {
  214. if (s->opt->offset < AST_FRIENDLY_OFFSET)
  215. ast_log(LOG_WARNING, "Returning a frame of inappropriate offset (%d).\n",
  216. s->opt->offset);
  217. opt = s->opt;
  218. s->opt = NULL;
  219. return opt;
  220. }
  221. /* Make sure we have enough data */
  222. if (s->len < s->size) {
  223. /* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
  224. if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->len % 10)))
  225. return NULL;
  226. }
  227. len = s->size;
  228. if (len > s->len)
  229. len = s->len;
  230. /* Make frame */
  231. s->f.frametype = AST_FRAME_VOICE;
  232. s->f.subclass = s->format;
  233. s->f.data = s->framedata + AST_FRIENDLY_OFFSET;
  234. s->f.offset = AST_FRIENDLY_OFFSET;
  235. s->f.datalen = len;
  236. /* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
  237. s->f.samples = len * s->samplesperbyte; /* XXX rounding */
  238. s->f.delivery = s->delivery;
  239. /* Fill Data */
  240. memcpy(s->f.data, s->data, len);
  241. s->len -= len;
  242. /* Move remaining data to the front if applicable */
  243. if (s->len) {
  244. /* In principle this should all be fine because if we are sending
  245. G.729 VAD, the next timestamp will take over anyawy */
  246. memmove(s->data, s->data + len, s->len);
  247. if (!ast_tvzero(s->delivery)) {
  248. /* If we have delivery time, increment it, otherwise, leave it at 0 */
  249. s->delivery = ast_tvadd(s->delivery, ast_samp2tv(s->f.samples, ast_format_rate(s->format)));
  250. }
  251. }
  252. /* Return frame */
  253. return &s->f;
  254. }
  255. void ast_smoother_free(struct ast_smoother *s)
  256. {
  257. ast_free(s);
  258. }
  259. static struct ast_frame *ast_frame_header_new(void)
  260. {
  261. struct ast_frame *f;
  262. #if !defined(LOW_MEMORY)
  263. struct ast_frame_cache *frames;
  264. if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
  265. if ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) {
  266. size_t mallocd_len = f->mallocd_hdr_len;
  267. memset(f, 0, sizeof(*f));
  268. f->mallocd_hdr_len = mallocd_len;
  269. f->mallocd = AST_MALLOCD_HDR;
  270. frames->size--;
  271. return f;
  272. }
  273. }
  274. if (!(f = ast_calloc_cache(1, sizeof(*f))))
  275. return NULL;
  276. #else
  277. if (!(f = ast_calloc(1, sizeof(*f))))
  278. return NULL;
  279. #endif
  280. f->mallocd_hdr_len = sizeof(*f);
  281. return f;
  282. }
  283. #if !defined(LOW_MEMORY)
  284. static void frame_cache_cleanup(void *data)
  285. {
  286. struct ast_frame_cache *frames = data;
  287. struct ast_frame *f;
  288. while ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list)))
  289. ast_free(f);
  290. ast_free(frames);
  291. }
  292. #endif
  293. static void __frame_free(struct ast_frame *fr, int cache)
  294. {
  295. if (!fr->mallocd)
  296. return;
  297. #if !defined(LOW_MEMORY)
  298. if (cache && fr->mallocd == AST_MALLOCD_HDR) {
  299. /* Cool, only the header is malloc'd, let's just cache those for now
  300. * to keep things simple... */
  301. struct ast_frame_cache *frames;
  302. if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames))) &&
  303. (frames->size < FRAME_CACHE_MAX_SIZE)) {
  304. AST_LIST_INSERT_HEAD(&frames->list, fr, frame_list);
  305. frames->size++;
  306. return;
  307. }
  308. }
  309. #endif
  310. if (fr->mallocd & AST_MALLOCD_DATA) {
  311. if (fr->data)
  312. ast_free(fr->data - fr->offset);
  313. }
  314. if (fr->mallocd & AST_MALLOCD_SRC) {
  315. if (fr->src)
  316. ast_free((void *) fr->src);
  317. }
  318. if (fr->mallocd & AST_MALLOCD_HDR) {
  319. ast_free(fr);
  320. }
  321. }
  322. void ast_frame_free(struct ast_frame *frame, int cache)
  323. {
  324. struct ast_frame *next;
  325. for (next = AST_LIST_NEXT(frame, frame_list);
  326. frame;
  327. frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) {
  328. __frame_free(frame, cache);
  329. }
  330. }
  331. /*!
  332. * \brief 'isolates' a frame by duplicating non-malloc'ed components
  333. * (header, src, data).
  334. * On return all components are malloc'ed
  335. */
  336. struct ast_frame *ast_frisolate(struct ast_frame *fr)
  337. {
  338. struct ast_frame *out;
  339. void *newdata;
  340. /* if none of the existing frame is malloc'd, let ast_frdup() do it
  341. since it is more efficient
  342. */
  343. if (fr->mallocd == 0) {
  344. return ast_frdup(fr);
  345. }
  346. /* if everything is already malloc'd, we are done */
  347. if ((fr->mallocd & (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) ==
  348. (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) {
  349. return fr;
  350. }
  351. if (!(fr->mallocd & AST_MALLOCD_HDR)) {
  352. /* Allocate a new header if needed */
  353. if (!(out = ast_frame_header_new())) {
  354. return NULL;
  355. }
  356. out->frametype = fr->frametype;
  357. out->subclass = fr->subclass;
  358. out->datalen = fr->datalen;
  359. out->samples = fr->samples;
  360. out->offset = fr->offset;
  361. /* Copy the timing data */
  362. ast_copy_flags(out, fr, AST_FRFLAG_HAS_TIMING_INFO);
  363. if (ast_test_flag(fr, AST_FRFLAG_HAS_TIMING_INFO)) {
  364. out->ts = fr->ts;
  365. out->len = fr->len;
  366. out->seqno = fr->seqno;
  367. }
  368. } else {
  369. out = fr;
  370. }
  371. if (!(fr->mallocd & AST_MALLOCD_SRC) && fr->src) {
  372. if (!(out->src = ast_strdup(fr->src))) {
  373. if (out != fr) {
  374. ast_free(out);
  375. }
  376. return NULL;
  377. }
  378. } else {
  379. out->src = fr->src;
  380. fr->src = NULL;
  381. fr->mallocd &= ~AST_MALLOCD_SRC;
  382. }
  383. if (!(fr->mallocd & AST_MALLOCD_DATA)) {
  384. if (!(newdata = ast_malloc(fr->datalen + AST_FRIENDLY_OFFSET))) {
  385. if (out->src != fr->src) {
  386. ast_free((void *) out->src);
  387. }
  388. if (out != fr) {
  389. ast_free(out);
  390. }
  391. return NULL;
  392. }
  393. newdata += AST_FRIENDLY_OFFSET;
  394. out->offset = AST_FRIENDLY_OFFSET;
  395. out->datalen = fr->datalen;
  396. memcpy(newdata, fr->data, fr->datalen);
  397. out->data = newdata;
  398. } else {
  399. out->data = fr->data;
  400. memset(&fr->data, 0, sizeof(fr->data));
  401. fr->mallocd &= ~AST_MALLOCD_DATA;
  402. }
  403. out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
  404. return out;
  405. }
  406. struct ast_frame *ast_frdup(const struct ast_frame *f)
  407. {
  408. struct ast_frame *out = NULL;
  409. int len, srclen = 0;
  410. void *buf = NULL;
  411. #if !defined(LOW_MEMORY)
  412. struct ast_frame_cache *frames;
  413. #endif
  414. /* Start with standard stuff */
  415. len = sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
  416. /* If we have a source, add space for it */
  417. /*
  418. * XXX Watch out here - if we receive a src which is not terminated
  419. * properly, we can be easily attacked. Should limit the size we deal with.
  420. */
  421. if (f->src)
  422. srclen = strlen(f->src);
  423. if (srclen > 0)
  424. len += srclen + 1;
  425. #if !defined(LOW_MEMORY)
  426. if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
  427. AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) {
  428. if (out->mallocd_hdr_len >= len) {
  429. size_t mallocd_len = out->mallocd_hdr_len;
  430. AST_LIST_REMOVE_CURRENT(frame_list);
  431. memset(out, 0, sizeof(*out));
  432. out->mallocd_hdr_len = mallocd_len;
  433. buf = out;
  434. frames->size--;
  435. break;
  436. }
  437. }
  438. AST_LIST_TRAVERSE_SAFE_END;
  439. }
  440. #endif
  441. if (!buf) {
  442. if (!(buf = ast_calloc_cache(1, len)))
  443. return NULL;
  444. out = buf;
  445. out->mallocd_hdr_len = len;
  446. }
  447. out->frametype = f->frametype;
  448. out->subclass = f->subclass;
  449. out->datalen = f->datalen;
  450. out->samples = f->samples;
  451. out->delivery = f->delivery;
  452. /* Set us as having malloc'd header only, so it will eventually
  453. get freed. */
  454. out->mallocd = AST_MALLOCD_HDR;
  455. out->offset = AST_FRIENDLY_OFFSET;
  456. if (out->datalen) {
  457. out->data = buf + sizeof(*out) + AST_FRIENDLY_OFFSET;
  458. memcpy(out->data, f->data, out->datalen);
  459. }
  460. if (srclen > 0) {
  461. /* This may seem a little strange, but it's to avoid a gcc (4.2.4) compiler warning */
  462. char *src;
  463. out->src = buf + sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
  464. src = (char *) out->src;
  465. /* Must have space since we allocated for it */
  466. strcpy(src, f->src);
  467. }
  468. ast_copy_flags(out, f, AST_FRFLAG_HAS_TIMING_INFO);
  469. out->ts = f->ts;
  470. out->len = f->len;
  471. out->seqno = f->seqno;
  472. return out;
  473. }
  474. void ast_swapcopy_samples(void *dst, const void *src, int samples)
  475. {
  476. int i;
  477. unsigned short *dst_s = dst;
  478. const unsigned short *src_s = src;
  479. for (i = 0; i < samples; i++)
  480. dst_s[i] = (src_s[i]<<8) | (src_s[i]>>8);
  481. }
  482. struct ast_format_list *ast_get_format_list_index(int index)
  483. {
  484. return &AST_FORMAT_LIST[index];
  485. }
  486. struct ast_format_list *ast_get_format_list(size_t *size)
  487. {
  488. *size = ARRAY_LEN(AST_FORMAT_LIST);
  489. return AST_FORMAT_LIST;
  490. }
  491. char* ast_getformatname(int format)
  492. {
  493. int x;
  494. char *ret = "unknown";
  495. for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
  496. if (AST_FORMAT_LIST[x].bits == format) {
  497. ret = AST_FORMAT_LIST[x].name;
  498. break;
  499. }
  500. }
  501. return ret;
  502. }
  503. char *ast_getformatname_multiple(char *buf, size_t size, int format)
  504. {
  505. int x;
  506. unsigned len;
  507. char *start, *end = buf;
  508. if (!size)
  509. return buf;
  510. snprintf(end, size, "0x%x (", format);
  511. len = strlen(end);
  512. end += len;
  513. size -= len;
  514. start = end;
  515. for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
  516. if (AST_FORMAT_LIST[x].bits & format) {
  517. snprintf(end, size,"%s|",AST_FORMAT_LIST[x].name);
  518. len = strlen(end);
  519. end += len;
  520. size -= len;
  521. }
  522. }
  523. if (start == end)
  524. ast_copy_string(start, "nothing)", size);
  525. else if (size > 1)
  526. *(end -1) = ')';
  527. return buf;
  528. }
  529. static struct ast_codec_alias_table {
  530. char *alias;
  531. char *realname;
  532. } ast_codec_alias_table[] = {
  533. { "slinear", "slin"},
  534. { "slinear16", "slin16"},
  535. { "g723.1", "g723"},
  536. };
  537. static const char *ast_expand_codec_alias(const char *in)
  538. {
  539. int x;
  540. for (x = 0; x < ARRAY_LEN(ast_codec_alias_table); x++) {
  541. if (!strcmp(in,ast_codec_alias_table[x].alias))
  542. return ast_codec_alias_table[x].realname;
  543. }
  544. return in;
  545. }
  546. int ast_getformatbyname(const char *name)
  547. {
  548. int x, all, format = 0;
  549. all = strcasecmp(name, "all") ? 0 : 1;
  550. for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
  551. if (all ||
  552. !strcasecmp(AST_FORMAT_LIST[x].name,name) ||
  553. !strcasecmp(AST_FORMAT_LIST[x].name,ast_expand_codec_alias(name))) {
  554. format |= AST_FORMAT_LIST[x].bits;
  555. if (!all)
  556. break;
  557. }
  558. }
  559. return format;
  560. }
  561. char *ast_codec2str(int codec)
  562. {
  563. int x;
  564. char *ret = "unknown";
  565. for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
  566. if (AST_FORMAT_LIST[x].bits == codec) {
  567. ret = AST_FORMAT_LIST[x].desc;
  568. break;
  569. }
  570. }
  571. return ret;
  572. }
  573. static char *show_codecs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  574. {
  575. int i, found=0;
  576. char hex[25];
  577. switch (cmd) {
  578. case CLI_INIT:
  579. e->command = "core show codecs [audio|video|image]";
  580. e->usage =
  581. "Usage: core show codecs [audio|video|image]\n"
  582. " Displays codec mapping\n";
  583. return NULL;
  584. case CLI_GENERATE:
  585. return NULL;
  586. }
  587. if ((a->argc < 3) || (a->argc > 4))
  588. return CLI_SHOWUSAGE;
  589. if (!ast_opt_dont_warn)
  590. ast_cli(a->fd, "Disclaimer: this command is for informational purposes only.\n"
  591. "\tIt does not indicate anything about your configuration.\n");
  592. ast_cli(a->fd, "%11s %9s %10s TYPE %8s %s\n","INT","BINARY","HEX","NAME","DESC");
  593. ast_cli(a->fd, "--------------------------------------------------------------------------------\n");
  594. if ((a->argc == 3) || (!strcasecmp(a->argv[3],"audio"))) {
  595. found = 1;
  596. for (i=0;i<13;i++) {
  597. snprintf(hex,25,"(0x%x)",1<<i);
  598. ast_cli(a->fd, "%11u (1 << %2d) %10s audio %8s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
  599. }
  600. }
  601. if ((a->argc == 3) || (!strcasecmp(a->argv[3],"image"))) {
  602. found = 1;
  603. for (i=16;i<18;i++) {
  604. snprintf(hex,25,"(0x%x)",1<<i);
  605. ast_cli(a->fd, "%11u (1 << %2d) %10s image %8s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
  606. }
  607. }
  608. if ((a->argc == 3) || (!strcasecmp(a->argv[3],"video"))) {
  609. found = 1;
  610. for (i=18;i<22;i++) {
  611. snprintf(hex,25,"(0x%x)",1<<i);
  612. ast_cli(a->fd, "%11u (1 << %2d) %10s video %8s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
  613. }
  614. }
  615. if (!found)
  616. return CLI_SHOWUSAGE;
  617. else
  618. return CLI_SUCCESS;
  619. }
  620. static char *show_codec_n(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  621. {
  622. int codec, i, found=0;
  623. switch (cmd) {
  624. case CLI_INIT:
  625. e->command = "core show codec";
  626. e->usage =
  627. "Usage: core show codec <number>\n"
  628. " Displays codec mapping\n";
  629. return NULL;
  630. case CLI_GENERATE:
  631. return NULL;
  632. }
  633. if (a->argc != 4)
  634. return CLI_SHOWUSAGE;
  635. if (sscanf(a->argv[3], "%30d", &codec) != 1)
  636. return CLI_SHOWUSAGE;
  637. for (i = 0; i < 32; i++)
  638. if (codec & (1 << i)) {
  639. found = 1;
  640. ast_cli(a->fd, "%11u (1 << %2d) %s\n",1 << i,i,ast_codec2str(1<<i));
  641. }
  642. if (!found)
  643. ast_cli(a->fd, "Codec %d not found\n", codec);
  644. return CLI_SUCCESS;
  645. }
  646. /*! Dump a frame for debugging purposes */
  647. void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix)
  648. {
  649. const char noname[] = "unknown";
  650. char ftype[40] = "Unknown Frametype";
  651. char cft[80];
  652. char subclass[40] = "Unknown Subclass";
  653. char csub[80];
  654. char moreinfo[40] = "";
  655. char cn[60];
  656. char cp[40];
  657. char cmn[40];
  658. const char *message = "Unknown";
  659. if (!name)
  660. name = noname;
  661. if (!f) {
  662. ast_verbose("%s [ %s (NULL) ] [%s]\n",
  663. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  664. term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  665. term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  666. return;
  667. }
  668. /* XXX We should probably print one each of voice and video when the format changes XXX */
  669. if (f->frametype == AST_FRAME_VOICE)
  670. return;
  671. if (f->frametype == AST_FRAME_VIDEO)
  672. return;
  673. switch(f->frametype) {
  674. case AST_FRAME_DTMF_BEGIN:
  675. strcpy(ftype, "DTMF Begin");
  676. subclass[0] = f->subclass;
  677. subclass[1] = '\0';
  678. break;
  679. case AST_FRAME_DTMF_END:
  680. strcpy(ftype, "DTMF End");
  681. subclass[0] = f->subclass;
  682. subclass[1] = '\0';
  683. break;
  684. case AST_FRAME_CONTROL:
  685. strcpy(ftype, "Control");
  686. switch(f->subclass) {
  687. case AST_CONTROL_HANGUP:
  688. strcpy(subclass, "Hangup");
  689. break;
  690. case AST_CONTROL_RING:
  691. strcpy(subclass, "Ring");
  692. break;
  693. case AST_CONTROL_RINGING:
  694. strcpy(subclass, "Ringing");
  695. break;
  696. case AST_CONTROL_ANSWER:
  697. strcpy(subclass, "Answer");
  698. break;
  699. case AST_CONTROL_BUSY:
  700. strcpy(subclass, "Busy");
  701. break;
  702. case AST_CONTROL_TAKEOFFHOOK:
  703. strcpy(subclass, "Take Off Hook");
  704. break;
  705. case AST_CONTROL_OFFHOOK:
  706. strcpy(subclass, "Line Off Hook");
  707. break;
  708. case AST_CONTROL_CONGESTION:
  709. strcpy(subclass, "Congestion");
  710. break;
  711. case AST_CONTROL_FLASH:
  712. strcpy(subclass, "Flash");
  713. break;
  714. case AST_CONTROL_WINK:
  715. strcpy(subclass, "Wink");
  716. break;
  717. case AST_CONTROL_OPTION:
  718. strcpy(subclass, "Option");
  719. break;
  720. case AST_CONTROL_RADIO_KEY:
  721. strcpy(subclass, "Key Radio");
  722. break;
  723. case AST_CONTROL_RADIO_UNKEY:
  724. strcpy(subclass, "Unkey Radio");
  725. break;
  726. case AST_CONTROL_HOLD:
  727. strcpy(subclass, "Hold");
  728. break;
  729. case AST_CONTROL_UNHOLD:
  730. strcpy(subclass, "Unhold");
  731. break;
  732. case AST_CONTROL_T38_PARAMETERS:
  733. if (f->datalen != sizeof(struct ast_control_t38_parameters)) {
  734. message = "Invalid";
  735. } else {
  736. struct ast_control_t38_parameters *parameters = f->data;
  737. enum ast_control_t38 state = parameters->request_response;
  738. if (state == AST_T38_REQUEST_NEGOTIATE)
  739. message = "Negotiation Requested";
  740. else if (state == AST_T38_REQUEST_TERMINATE)
  741. message = "Negotiation Request Terminated";
  742. else if (state == AST_T38_NEGOTIATED)
  743. message = "Negotiated";
  744. else if (state == AST_T38_TERMINATED)
  745. message = "Terminated";
  746. else if (state == AST_T38_REFUSED)
  747. message = "Refused";
  748. }
  749. snprintf(subclass, sizeof(subclass), "T38_Parameters/%s", message);
  750. break;
  751. case -1:
  752. strcpy(subclass, "Stop generators");
  753. break;
  754. default:
  755. snprintf(subclass, sizeof(subclass), "Unknown control '%d'", f->subclass);
  756. }
  757. break;
  758. case AST_FRAME_NULL:
  759. strcpy(ftype, "Null Frame");
  760. strcpy(subclass, "N/A");
  761. break;
  762. case AST_FRAME_IAX:
  763. /* Should never happen */
  764. strcpy(ftype, "IAX Specific");
  765. snprintf(subclass, sizeof(subclass), "IAX Frametype %d", f->subclass);
  766. break;
  767. case AST_FRAME_TEXT:
  768. strcpy(ftype, "Text");
  769. strcpy(subclass, "N/A");
  770. ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
  771. break;
  772. case AST_FRAME_IMAGE:
  773. strcpy(ftype, "Image");
  774. snprintf(subclass, sizeof(subclass), "Image format %s\n", ast_getformatname(f->subclass));
  775. break;
  776. case AST_FRAME_HTML:
  777. strcpy(ftype, "HTML");
  778. switch(f->subclass) {
  779. case AST_HTML_URL:
  780. strcpy(subclass, "URL");
  781. ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
  782. break;
  783. case AST_HTML_DATA:
  784. strcpy(subclass, "Data");
  785. break;
  786. case AST_HTML_BEGIN:
  787. strcpy(subclass, "Begin");
  788. break;
  789. case AST_HTML_END:
  790. strcpy(subclass, "End");
  791. break;
  792. case AST_HTML_LDCOMPLETE:
  793. strcpy(subclass, "Load Complete");
  794. break;
  795. case AST_HTML_NOSUPPORT:
  796. strcpy(subclass, "No Support");
  797. break;
  798. case AST_HTML_LINKURL:
  799. strcpy(subclass, "Link URL");
  800. ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
  801. break;
  802. case AST_HTML_UNLINK:
  803. strcpy(subclass, "Unlink");
  804. break;
  805. case AST_HTML_LINKREJECT:
  806. strcpy(subclass, "Link Reject");
  807. break;
  808. default:
  809. snprintf(subclass, sizeof(subclass), "Unknown HTML frame '%d'\n", f->subclass);
  810. break;
  811. }
  812. break;
  813. case AST_FRAME_MODEM:
  814. strcpy(ftype, "Modem");
  815. switch (f->subclass) {
  816. case AST_MODEM_T38:
  817. strcpy(subclass, "T.38");
  818. break;
  819. case AST_MODEM_V150:
  820. strcpy(subclass, "V.150");
  821. break;
  822. default:
  823. snprintf(subclass, sizeof(subclass), "Unknown MODEM frame '%d'\n", f->subclass);
  824. break;
  825. }
  826. break;
  827. default:
  828. snprintf(ftype, sizeof(ftype), "Unknown Frametype '%d'", f->frametype);
  829. }
  830. if (!ast_strlen_zero(moreinfo))
  831. ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) '%s' ] [%s]\n",
  832. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  833. term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  834. f->frametype,
  835. term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
  836. f->subclass,
  837. term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
  838. term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  839. else
  840. ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) ] [%s]\n",
  841. term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
  842. term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
  843. f->frametype,
  844. term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
  845. f->subclass,
  846. term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
  847. }
  848. /* Builtin Asterisk CLI-commands for debugging */
  849. static struct ast_cli_entry my_clis[] = {
  850. AST_CLI_DEFINE(show_codecs, "Displays a list of codecs"),
  851. AST_CLI_DEFINE(show_codec_n, "Shows a specific codec"),
  852. };
  853. int init_framer(void)
  854. {
  855. ast_cli_register_multiple(my_clis, sizeof(my_clis) / sizeof(struct ast_cli_entry));
  856. return 0;
  857. }
  858. void ast_codec_pref_convert(struct ast_codec_pref *pref, char *buf, size_t size, int right)
  859. {
  860. int x, differential = (int) 'A', mem;
  861. char *from, *to;
  862. if (right) {
  863. from = pref->order;
  864. to = buf;
  865. mem = size;
  866. } else {
  867. to = pref->order;
  868. from = buf;
  869. mem = 32;
  870. }
  871. memset(to, 0, mem);
  872. for (x = 0; x < 32 ; x++) {
  873. if (!from[x])
  874. break;
  875. to[x] = right ? (from[x] + differential) : (from[x] - differential);
  876. }
  877. }
  878. int ast_codec_pref_string(struct ast_codec_pref *pref, char *buf, size_t size)
  879. {
  880. int x, codec;
  881. size_t total_len, slen;
  882. char *formatname;
  883. memset(buf,0,size);
  884. total_len = size;
  885. buf[0] = '(';
  886. total_len--;
  887. for(x = 0; x < 32 ; x++) {
  888. if (total_len <= 0)
  889. break;
  890. if (!(codec = ast_codec_pref_index(pref,x)))
  891. break;
  892. if ((formatname = ast_getformatname(codec))) {
  893. slen = strlen(formatname);
  894. if (slen > total_len)
  895. break;
  896. strncat(buf, formatname, total_len - 1); /* safe */
  897. total_len -= slen;
  898. }
  899. if (total_len && x < 31 && ast_codec_pref_index(pref , x + 1)) {
  900. strncat(buf, "|", total_len - 1); /* safe */
  901. total_len--;
  902. }
  903. }
  904. if (total_len) {
  905. strncat(buf, ")", total_len - 1); /* safe */
  906. total_len--;
  907. }
  908. return size - total_len;
  909. }
  910. int ast_codec_pref_index(struct ast_codec_pref *pref, int index)
  911. {
  912. int slot = 0;
  913. if ((index >= 0) && (index < sizeof(pref->order))) {
  914. slot = pref->order[index];
  915. }
  916. return slot ? AST_FORMAT_LIST[slot-1].bits : 0;
  917. }
  918. /*! \brief Remove codec from pref list */
  919. void ast_codec_pref_remove(struct ast_codec_pref *pref, int format)
  920. {
  921. struct ast_codec_pref oldorder;
  922. int x, y = 0;
  923. int slot;
  924. int size;
  925. if (!pref->order[0])
  926. return;
  927. memcpy(&oldorder, pref, sizeof(oldorder));
  928. memset(pref, 0, sizeof(*pref));
  929. for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
  930. slot = oldorder.order[x];
  931. size = oldorder.framing[x];
  932. if (! slot)
  933. break;
  934. if (AST_FORMAT_LIST[slot-1].bits != format) {
  935. pref->order[y] = slot;
  936. pref->framing[y++] = size;
  937. }
  938. }
  939. }
  940. /*! \brief Append codec to list */
  941. int ast_codec_pref_append(struct ast_codec_pref *pref, int format)
  942. {
  943. int x, newindex = 0;
  944. ast_codec_pref_remove(pref, format);
  945. for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
  946. if (AST_FORMAT_LIST[x].bits == format) {
  947. newindex = x + 1;
  948. break;
  949. }
  950. }
  951. if (newindex) {
  952. for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
  953. if (!pref->order[x]) {
  954. pref->order[x] = newindex;
  955. break;
  956. }
  957. }
  958. }
  959. return x;
  960. }
  961. /*! \brief Prepend codec to list */
  962. void ast_codec_pref_prepend(struct ast_codec_pref *pref, int format, int only_if_existing)
  963. {
  964. int x, newindex = 0;
  965. /* First step is to get the codecs "index number" */
  966. for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
  967. if (AST_FORMAT_LIST[x].bits == format) {
  968. newindex = x + 1;
  969. break;
  970. }
  971. }
  972. /* Done if its unknown */
  973. if (!newindex)
  974. return;
  975. /* Now find any existing occurrence, or the end */
  976. for (x = 0; x < 32; x++) {
  977. if (!pref->order[x] || pref->order[x] == newindex)
  978. break;
  979. }
  980. if (only_if_existing && !pref->order[x])
  981. return;
  982. /* Move down to make space to insert - either all the way to the end,
  983. or as far as the existing location (which will be overwritten) */
  984. for (; x > 0; x--) {
  985. pref->order[x] = pref->order[x - 1];
  986. pref->framing[x] = pref->framing[x - 1];
  987. }
  988. /* And insert the new entry */
  989. pref->order[0] = newindex;
  990. pref->framing[0] = 0; /* ? */
  991. }
  992. /*! \brief Set packet size for codec */
  993. int ast_codec_pref_setsize(struct ast_codec_pref *pref, int format, int framems)
  994. {
  995. int x, index = -1;
  996. for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
  997. if (AST_FORMAT_LIST[x].bits == format) {
  998. index = x;
  999. break;
  1000. }
  1001. }
  1002. if (index < 0)
  1003. return -1;
  1004. /* size validation */
  1005. if (!framems)
  1006. framems = AST_FORMAT_LIST[index].def_ms;
  1007. if (AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
  1008. framems -= framems % AST_FORMAT_LIST[index].inc_ms;
  1009. if (framems < AST_FORMAT_LIST[index].min_ms)
  1010. framems = AST_FORMAT_LIST[index].min_ms;
  1011. if (framems > AST_FORMAT_LIST[index].max_ms)
  1012. framems = AST_FORMAT_LIST[index].max_ms;
  1013. for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
  1014. if (pref->order[x] == (index + 1)) {
  1015. pref->framing[x] = framems;
  1016. break;
  1017. }
  1018. }
  1019. return x;
  1020. }
  1021. /*! \brief Get packet size for codec */
  1022. struct ast_format_list ast_codec_pref_getsize(struct ast_codec_pref *pref, int format)
  1023. {
  1024. int x, index = -1, framems = 0;
  1025. struct ast_format_list fmt = { 0, };
  1026. for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
  1027. if (AST_FORMAT_LIST[x].bits == format) {
  1028. fmt = AST_FORMAT_LIST[x];
  1029. index = x;
  1030. break;
  1031. }
  1032. }
  1033. for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
  1034. if (pref->order[x] == (index + 1)) {
  1035. framems = pref->framing[x];
  1036. break;
  1037. }
  1038. }
  1039. /* size validation */
  1040. if (!framems)
  1041. framems = AST_FORMAT_LIST[index].def_ms;
  1042. if (AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
  1043. framems -= framems % AST_FORMAT_LIST[index].inc_ms;
  1044. if (framems < AST_FORMAT_LIST[index].min_ms)
  1045. framems = AST_FORMAT_LIST[index].min_ms;
  1046. if (framems > AST_FORMAT_LIST[index].max_ms)
  1047. framems = AST_FORMAT_LIST[index].max_ms;
  1048. fmt.cur_ms = framems;
  1049. return fmt;
  1050. }
  1051. /*! \brief Pick a codec */
  1052. int ast_codec_choose(struct ast_codec_pref *pref, int formats, int find_best)
  1053. {
  1054. int x, ret = 0, slot;
  1055. for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
  1056. slot = pref->order[x];
  1057. if (!slot)
  1058. break;
  1059. if (formats & AST_FORMAT_LIST[slot-1].bits) {
  1060. ret = AST_FORMAT_LIST[slot-1].bits;
  1061. break;
  1062. }
  1063. }
  1064. if (ret & AST_FORMAT_AUDIO_MASK)
  1065. return ret;
  1066. ast_debug(4, "Could not find preferred codec - %s\n", find_best ? "Going for the best codec" : "Returning zero codec");
  1067. return find_best ? ast_best_codec(formats) : 0;
  1068. }
  1069. int ast_parse_allow_disallow(struct ast_codec_pref *pref, int *mask, const char *list, int allowing)
  1070. {
  1071. int errors = 0;
  1072. char *parse = NULL, *this = NULL, *psize = NULL;
  1073. int format = 0, framems = 0;
  1074. parse = ast_strdupa(list);
  1075. while ((this = strsep(&parse, ","))) {
  1076. framems = 0;
  1077. if ((psize = strrchr(this, ':'))) {
  1078. *psize++ = '\0';
  1079. ast_debug(1, "Packetization for codec: %s is %s\n", this, psize);
  1080. framems = atoi(psize);
  1081. if (framems < 0) {
  1082. framems = 0;
  1083. errors++;
  1084. ast_log(LOG_WARNING, "Bad packetization value for codec %s\n", this);
  1085. }
  1086. }
  1087. if (!(format = ast_getformatbyname(this))) {
  1088. ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", allowing ? "allow" : "disallow", this);
  1089. errors++;
  1090. continue;
  1091. }
  1092. if (mask) {
  1093. if (allowing)
  1094. *mask |= format;
  1095. else
  1096. *mask &= ~format;
  1097. }
  1098. /* Set up a preference list for audio. Do not include video in preferences
  1099. since we can not transcode video and have to use whatever is offered
  1100. */
  1101. if (pref && (format & AST_FORMAT_AUDIO_MASK)) {
  1102. if (strcasecmp(this, "all")) {
  1103. if (allowing) {
  1104. ast_codec_pref_append(pref, format);
  1105. ast_codec_pref_setsize(pref, format, framems);
  1106. }
  1107. else
  1108. ast_codec_pref_remove(pref, format);
  1109. } else if (!allowing) {
  1110. memset(pref, 0, sizeof(*pref));
  1111. }
  1112. }
  1113. }
  1114. return errors;
  1115. }
  1116. static int g723_len(unsigned char buf)
  1117. {
  1118. enum frame_type type = buf & TYPE_MASK;
  1119. switch(type) {
  1120. case TYPE_DONTSEND:
  1121. return 0;
  1122. break;
  1123. case TYPE_SILENCE:
  1124. return 4;
  1125. break;
  1126. case TYPE_HIGH:
  1127. return 24;
  1128. break;
  1129. case TYPE_LOW:
  1130. return 20;
  1131. break;
  1132. default:
  1133. ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", type);
  1134. }
  1135. return -1;
  1136. }
  1137. static int g723_samples(unsigned char *buf, int maxlen)
  1138. {
  1139. int pos = 0;
  1140. int samples = 0;
  1141. int res;
  1142. while(pos < maxlen) {
  1143. res = g723_len(buf[pos]);
  1144. if (res <= 0)
  1145. break;
  1146. samples += 240;
  1147. pos += res;
  1148. }
  1149. return samples;
  1150. }
  1151. static unsigned char get_n_bits_at(unsigned char *data, int n, int bit)
  1152. {
  1153. int byte = bit / 8; /* byte containing first bit */
  1154. int rem = 8 - (bit % 8); /* remaining bits in first byte */
  1155. unsigned char ret = 0;
  1156. if (n <= 0 || n > 8)
  1157. return 0;
  1158. if (rem < n) {
  1159. ret = (data[byte] << (n - rem));
  1160. ret |= (data[byte + 1] >> (8 - n + rem));
  1161. } else {
  1162. ret = (data[byte] >> (rem - n));
  1163. }
  1164. return (ret & (0xff >> (8 - n)));
  1165. }
  1166. static int speex_get_wb_sz_at(unsigned char *data, int len, int bit)
  1167. {
  1168. static int SpeexWBSubModeSz[] = {
  1169. 0, 36, 112, 192,
  1170. 352, 0, 0, 0 };
  1171. int off = bit;
  1172. unsigned char c;
  1173. /* skip up to two wideband frames */
  1174. if (((len * 8 - off) >= 5) &&
  1175. get_n_bits_at(data, 1, off)) {
  1176. c = get_n_bits_at(data, 3, off + 1);
  1177. off += SpeexWBSubModeSz[c];
  1178. if (((len * 8 - off) >= 5) &&
  1179. get_n_bits_at(data, 1, off)) {
  1180. c = get_n_bits_at(data, 3, off + 1);
  1181. off += SpeexWBSubModeSz[c];
  1182. if (((len * 8 - off) >= 5) &&
  1183. get_n_bits_at(data, 1, off)) {
  1184. ast_log(LOG_WARNING, "Encountered corrupt speex frame; too many wideband frames in a row.\n");
  1185. return -1;
  1186. }
  1187. }
  1188. }
  1189. return off - bit;
  1190. }
  1191. static int speex_samples(unsigned char *data, int len)
  1192. {
  1193. static int SpeexSubModeSz[] = {
  1194. 5, 43, 119, 160,
  1195. 220, 300, 364, 492,
  1196. 79, 0, 0, 0,
  1197. 0, 0, 0, 0 };
  1198. static int SpeexInBandSz[] = {
  1199. 1, 1, 4, 4,
  1200. 4, 4, 4, 4,
  1201. 8, 8, 16, 16,
  1202. 32, 32, 64, 64 };
  1203. int bit = 0;
  1204. int cnt = 0;
  1205. int off;
  1206. unsigned char c;
  1207. while ((len * 8 - bit) >= 5) {
  1208. /* skip wideband frames */
  1209. off = speex_get_wb_sz_at(data, len, bit);
  1210. if (off < 0) {
  1211. ast_log(LOG_WARNING, "Had error while reading wideband frames for speex samples\n");
  1212. break;
  1213. }
  1214. bit += off;
  1215. if ((len * 8 - bit) < 5) {
  1216. ast_log(LOG_WARNING, "Not enough bits remaining after wide band for speex samples.\n");
  1217. break;
  1218. }
  1219. /* get control bits */
  1220. c = get_n_bits_at(data, 5, bit);
  1221. bit += 5;
  1222. if (c == 15) {
  1223. /* terminator */
  1224. break;
  1225. } else if (c == 14) {
  1226. /* in-band signal; next 4 bits contain signal id */
  1227. c = get_n_bits_at(data, 4, bit);
  1228. bit += 4;
  1229. bit += SpeexInBandSz[c];
  1230. } else if (c == 13) {
  1231. /* user in-band; next 5 bits contain msg len */
  1232. c = get_n_bits_at(data, 5, bit);
  1233. bit += 5;
  1234. bit += c * 8;
  1235. } else if (c > 8) {
  1236. /* unknown */
  1237. break;
  1238. } else {
  1239. /* skip number bits for submode (less the 5 control bits) */
  1240. bit += SpeexSubModeSz[c] - 5;
  1241. cnt += 160; /* new frame */
  1242. }
  1243. }
  1244. return cnt;
  1245. }
  1246. int ast_codec_get_samples(struct ast_frame *f)
  1247. {
  1248. int samples=0;
  1249. switch(f->subclass) {
  1250. case AST_FORMAT_SPEEX:
  1251. samples = speex_samples(f->data, f->datalen);
  1252. break;
  1253. case AST_FORMAT_G723_1:
  1254. samples = g723_samples(f->data, f->datalen);
  1255. break;
  1256. case AST_FORMAT_ILBC:
  1257. samples = 240 * (f->datalen / 50);
  1258. break;
  1259. case AST_FORMAT_GSM:
  1260. samples = 160 * (f->datalen / 33);
  1261. break;
  1262. case AST_FORMAT_G729A:
  1263. samples = f->datalen * 8;
  1264. break;
  1265. case AST_FORMAT_SLINEAR:
  1266. case AST_FORMAT_SLINEAR16:
  1267. samples = f->datalen / 2;
  1268. break;
  1269. case AST_FORMAT_LPC10:
  1270. /* assumes that the RTP packet contains one LPC10 frame */
  1271. samples = 22 * 8;
  1272. samples += (((char *)(f->data))[7] & 0x1) * 8;
  1273. break;
  1274. case AST_FORMAT_ULAW:
  1275. case AST_FORMAT_ALAW:
  1276. samples = f->datalen;
  1277. break;
  1278. case AST_FORMAT_G722:
  1279. case AST_FORMAT_ADPCM:
  1280. case AST_FORMAT_G726:
  1281. case AST_FORMAT_G726_AAL2:
  1282. samples = f->datalen * 2;
  1283. break;
  1284. default:
  1285. ast_log(LOG_WARNING, "Unable to calculate samples for format %s\n", ast_getformatname(f->subclass));
  1286. }
  1287. return samples;
  1288. }
  1289. int ast_codec_get_len(int format, int samples)
  1290. {
  1291. int len = 0;
  1292. /* XXX Still need speex, g723, and lpc10 XXX */
  1293. switch(format) {
  1294. case AST_FORMAT_G723_1:
  1295. len = (samples / 240) * 20;
  1296. break;
  1297. case AST_FORMAT_ILBC:
  1298. len = (samples / 240) * 50;
  1299. break;
  1300. case AST_FORMAT_GSM:
  1301. len = (samples / 160) * 33;
  1302. break;
  1303. case AST_FORMAT_G729A:
  1304. len = samples / 8;
  1305. break;
  1306. case AST_FORMAT_SLINEAR:
  1307. case AST_FORMAT_SLINEAR16:
  1308. len = samples * 2;
  1309. break;
  1310. case AST_FORMAT_ULAW:
  1311. case AST_FORMAT_ALAW:
  1312. len = samples;
  1313. break;
  1314. case AST_FORMAT_G722:
  1315. case AST_FORMAT_ADPCM:
  1316. case AST_FORMAT_G726:
  1317. case AST_FORMAT_G726_AAL2:
  1318. len = samples / 2;
  1319. break;
  1320. default:
  1321. ast_log(LOG_WARNING, "Unable to calculate sample length for format %s\n", ast_getformatname(format));
  1322. }
  1323. return len;
  1324. }
  1325. int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
  1326. {
  1327. int count;
  1328. short *fdata = f->data;
  1329. short adjust_value = abs(adjustment);
  1330. if ((f->frametype != AST_FRAME_VOICE) || (f->subclass != AST_FORMAT_SLINEAR))
  1331. return -1;
  1332. if (!adjustment)
  1333. return 0;
  1334. for (count = 0; count < f->samples; count++) {
  1335. if (adjustment > 0) {
  1336. ast_slinear_saturated_multiply(&fdata[count], &adjust_value);
  1337. } else if (adjustment < 0) {
  1338. ast_slinear_saturated_divide(&fdata[count], &adjust_value);
  1339. }
  1340. }
  1341. return 0;
  1342. }
  1343. int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2)
  1344. {
  1345. int count;
  1346. short *data1, *data2;
  1347. if ((f1->frametype != AST_FRAME_VOICE) || (f1->subclass != AST_FORMAT_SLINEAR))
  1348. return -1;
  1349. if ((f2->frametype != AST_FRAME_VOICE) || (f2->subclass != AST_FORMAT_SLINEAR))
  1350. return -1;
  1351. if (f1->samples != f2->samples)
  1352. return -1;
  1353. for (count = 0, data1 = f1->data, data2 = f2->data;
  1354. count < f1->samples;
  1355. count++, data1++, data2++)
  1356. ast_slinear_saturated_add(data1, data2);
  1357. return 0;
  1358. }