frame.c 43 KB

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