circbuf.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJMEDIA_CIRC_BUF_H__
  20. #define __PJMEDIA_CIRC_BUF_H__
  21. /**
  22. * @file circbuf.h
  23. * @brief Circular Buffer.
  24. */
  25. #include <pj/assert.h>
  26. #include <pj/errno.h>
  27. #include <pj/pool.h>
  28. #include <pjmedia/frame.h>
  29. /**
  30. * @defgroup PJMED_CIRCBUF Circular Buffer
  31. * @ingroup PJMEDIA_FRAME_OP
  32. * @brief Circular buffer manages read and write contiguous audio samples in a
  33. * non-contiguous buffer as if the buffer were contiguous. This should give
  34. * better performance than keeping contiguous samples in a contiguous buffer,
  35. * since read/write operations will only update the pointers, instead of
  36. * shifting audio samples.
  37. *
  38. * @{
  39. *
  40. * This section describes PJMEDIA's implementation of circular buffer.
  41. */
  42. /* Algorithm checkings, for development purpose only */
  43. #if 0
  44. # define PJMEDIA_CIRC_BUF_CHECK(x) pj_assert(x)
  45. #else
  46. # define PJMEDIA_CIRC_BUF_CHECK(x)
  47. #endif
  48. PJ_BEGIN_DECL
  49. /**
  50. * Circular buffer structure
  51. */
  52. typedef struct pjmedia_circ_buf {
  53. pj_int16_t *buf; /**< The buffer */
  54. unsigned capacity; /**< Buffer capacity, in samples */
  55. pj_int16_t *start; /**< Pointer to the first sample */
  56. unsigned len; /**< Audio samples length,
  57. in samples */
  58. } pjmedia_circ_buf;
  59. /**
  60. * Create the circular buffer.
  61. *
  62. * @param pool Pool where the circular buffer will be allocated
  63. * from.
  64. * @param capacity Capacity of the buffer, in samples.
  65. * @param p_cb Pointer to receive the circular buffer instance.
  66. *
  67. * @return PJ_SUCCESS if the circular buffer has been
  68. * created successfully, otherwise the appropriate
  69. * error will be returned.
  70. */
  71. PJ_INLINE(pj_status_t) pjmedia_circ_buf_create(pj_pool_t *pool,
  72. unsigned capacity,
  73. pjmedia_circ_buf **p_cb)
  74. {
  75. pjmedia_circ_buf *cbuf;
  76. cbuf = PJ_POOL_ZALLOC_T(pool, pjmedia_circ_buf);
  77. cbuf->buf = (pj_int16_t*) pj_pool_calloc(pool, capacity,
  78. sizeof(pj_int16_t));
  79. cbuf->capacity = capacity;
  80. cbuf->start = cbuf->buf;
  81. cbuf->len = 0;
  82. *p_cb = cbuf;
  83. return PJ_SUCCESS;
  84. }
  85. /**
  86. * Reset the circular buffer.
  87. *
  88. * @param circbuf The circular buffer.
  89. *
  90. * @return PJ_SUCCESS when successful.
  91. */
  92. PJ_INLINE(pj_status_t) pjmedia_circ_buf_reset(pjmedia_circ_buf *circbuf)
  93. {
  94. circbuf->start = circbuf->buf;
  95. circbuf->len = 0;
  96. return PJ_SUCCESS;
  97. }
  98. /**
  99. * Get the circular buffer length, it is number of samples buffered in the
  100. * circular buffer.
  101. *
  102. * @param circbuf The circular buffer.
  103. *
  104. * @return The buffer length.
  105. */
  106. PJ_INLINE(unsigned) pjmedia_circ_buf_get_len(pjmedia_circ_buf *circbuf)
  107. {
  108. return circbuf->len;
  109. }
  110. /**
  111. * Set circular buffer length. This is useful when audio buffer is manually
  112. * manipulated by the user, e.g: shrinked, expanded.
  113. *
  114. * @param circbuf The circular buffer.
  115. * @param len The new buffer length.
  116. */
  117. PJ_INLINE(void) pjmedia_circ_buf_set_len(pjmedia_circ_buf *circbuf,
  118. unsigned len)
  119. {
  120. PJMEDIA_CIRC_BUF_CHECK(len <= circbuf->capacity);
  121. circbuf->len = len;
  122. }
  123. /**
  124. * Advance the read pointer of circular buffer. This function will discard
  125. * the skipped samples while advancing the read pointer, thus reducing
  126. * the buffer length.
  127. *
  128. * @param circbuf The circular buffer.
  129. * @param count Distance from current read pointer, can only be
  130. * possitive number, in samples.
  131. *
  132. * @return PJ_SUCCESS when successful, otherwise
  133. * the appropriate error will be returned.
  134. */
  135. PJ_INLINE(pj_status_t) pjmedia_circ_buf_adv_read_ptr(pjmedia_circ_buf *circbuf,
  136. unsigned count)
  137. {
  138. if (count >= circbuf->len)
  139. return pjmedia_circ_buf_reset(circbuf);
  140. PJMEDIA_CIRC_BUF_CHECK(count <= circbuf->len);
  141. circbuf->start += count;
  142. if (circbuf->start >= circbuf->buf + circbuf->capacity)
  143. circbuf->start -= circbuf->capacity;
  144. circbuf->len -= count;
  145. return PJ_SUCCESS;
  146. }
  147. /**
  148. * Advance the write pointer of circular buffer. Since write pointer is always
  149. * pointing to a sample after the end of sample, so this function also means
  150. * increasing the buffer length.
  151. *
  152. * @param circbuf The circular buffer.
  153. * @param count Distance from current write pointer, can only be
  154. * possitive number, in samples.
  155. *
  156. * @return PJ_SUCCESS when successful, otherwise
  157. * the appropriate error will be returned.
  158. */
  159. PJ_INLINE(pj_status_t) pjmedia_circ_buf_adv_write_ptr(pjmedia_circ_buf *circbuf,
  160. unsigned count)
  161. {
  162. if (count + circbuf->len > circbuf->capacity)
  163. return PJ_ETOOBIG;
  164. circbuf->len += count;
  165. return PJ_SUCCESS;
  166. }
  167. /**
  168. * Get the real buffer addresses containing the audio samples.
  169. *
  170. * @param circbuf The circular buffer.
  171. * @param reg1 Pointer to store the first buffer address.
  172. * @param reg1_len Pointer to store the length of the first buffer,
  173. * in samples.
  174. * @param reg2 Pointer to store the second buffer address.
  175. * @param reg2_len Pointer to store the length of the second buffer,
  176. * in samples.
  177. */
  178. PJ_INLINE(void) pjmedia_circ_buf_get_read_regions(pjmedia_circ_buf *circbuf,
  179. pj_int16_t **reg1,
  180. unsigned *reg1_len,
  181. pj_int16_t **reg2,
  182. unsigned *reg2_len)
  183. {
  184. *reg1 = circbuf->start;
  185. *reg1_len = circbuf->len;
  186. if (*reg1 + *reg1_len > circbuf->buf + circbuf->capacity) {
  187. *reg1_len = (unsigned)(circbuf->buf + circbuf->capacity -
  188. circbuf->start);
  189. *reg2 = circbuf->buf;
  190. *reg2_len = circbuf->len - *reg1_len;
  191. } else {
  192. *reg2 = NULL;
  193. *reg2_len = 0;
  194. }
  195. PJMEDIA_CIRC_BUF_CHECK(*reg1_len != 0 || (*reg1_len == 0 &&
  196. circbuf->len == 0));
  197. PJMEDIA_CIRC_BUF_CHECK(*reg1_len + *reg2_len == circbuf->len);
  198. }
  199. /**
  200. * Get the real buffer addresses that is empty or writeable.
  201. *
  202. * @param circbuf The circular buffer.
  203. * @param reg1 Pointer to store the first buffer address.
  204. * @param reg1_len Pointer to store the length of the first buffer,
  205. * in samples.
  206. * @param reg2 Pointer to store the second buffer address.
  207. * @param reg2_len Pointer to store the length of the second buffer,
  208. * in samples.
  209. */
  210. PJ_INLINE(void) pjmedia_circ_buf_get_write_regions(pjmedia_circ_buf *circbuf,
  211. pj_int16_t **reg1,
  212. unsigned *reg1_len,
  213. pj_int16_t **reg2,
  214. unsigned *reg2_len)
  215. {
  216. *reg1 = circbuf->start + circbuf->len;
  217. if (*reg1 >= circbuf->buf + circbuf->capacity)
  218. *reg1 -= circbuf->capacity;
  219. *reg1_len = circbuf->capacity - circbuf->len;
  220. if (*reg1 + *reg1_len > circbuf->buf + circbuf->capacity) {
  221. *reg1_len = (unsigned)(circbuf->buf + circbuf->capacity - *reg1);
  222. *reg2 = circbuf->buf;
  223. *reg2_len = (unsigned)(circbuf->start - circbuf->buf);
  224. } else {
  225. *reg2 = NULL;
  226. *reg2_len = 0;
  227. }
  228. PJMEDIA_CIRC_BUF_CHECK(*reg1_len != 0 || (*reg1_len == 0 &&
  229. circbuf->len == 0));
  230. PJMEDIA_CIRC_BUF_CHECK(*reg1_len + *reg2_len == circbuf->capacity -
  231. circbuf->len);
  232. }
  233. /**
  234. * Read audio samples from the circular buffer.
  235. *
  236. * @param circbuf The circular buffer.
  237. * @param data Buffer to store the read audio samples.
  238. * @param count Number of samples being read.
  239. *
  240. * @return PJ_SUCCESS when successful, otherwise
  241. * the appropriate error will be returned.
  242. */
  243. PJ_INLINE(pj_status_t) pjmedia_circ_buf_read(pjmedia_circ_buf *circbuf,
  244. pj_int16_t *data,
  245. unsigned count)
  246. {
  247. pj_int16_t *reg1, *reg2;
  248. unsigned reg1cnt, reg2cnt;
  249. /* Data in the buffer is less than requested */
  250. if (count > circbuf->len)
  251. return PJ_ETOOBIG;
  252. pjmedia_circ_buf_get_read_regions(circbuf, &reg1, &reg1cnt,
  253. &reg2, &reg2cnt);
  254. if (reg1cnt >= count) {
  255. pjmedia_copy_samples(data, reg1, count);
  256. } else {
  257. pjmedia_copy_samples(data, reg1, reg1cnt);
  258. pjmedia_copy_samples(data + reg1cnt, reg2, count - reg1cnt);
  259. }
  260. return pjmedia_circ_buf_adv_read_ptr(circbuf, count);
  261. }
  262. /**
  263. * Write audio samples to the circular buffer.
  264. *
  265. * @param circbuf The circular buffer.
  266. * @param data Audio samples to be written.
  267. * @param count Number of samples being written.
  268. *
  269. * @return PJ_SUCCESS when successful, otherwise
  270. * the appropriate error will be returned.
  271. */
  272. PJ_INLINE(pj_status_t) pjmedia_circ_buf_write(pjmedia_circ_buf *circbuf,
  273. pj_int16_t *data,
  274. unsigned count)
  275. {
  276. pj_int16_t *reg1, *reg2;
  277. unsigned reg1cnt, reg2cnt;
  278. /* Data to write is larger than buffer can store */
  279. if (count > circbuf->capacity - circbuf->len)
  280. return PJ_ETOOBIG;
  281. pjmedia_circ_buf_get_write_regions(circbuf, &reg1, &reg1cnt,
  282. &reg2, &reg2cnt);
  283. if (reg1cnt >= count) {
  284. pjmedia_copy_samples(reg1, data, count);
  285. } else {
  286. pjmedia_copy_samples(reg1, data, reg1cnt);
  287. pjmedia_copy_samples(reg2, data + reg1cnt, count - reg1cnt);
  288. }
  289. return pjmedia_circ_buf_adv_write_ptr(circbuf, count);
  290. }
  291. /**
  292. * Copy audio samples from the circular buffer without changing its state.
  293. *
  294. * @param circbuf The circular buffer.
  295. * @param start_idx Starting sample index to be copied.
  296. * @param data Buffer to store the read audio samples.
  297. * @param count Number of samples being read.
  298. *
  299. * @return PJ_SUCCESS when successful, otherwise
  300. * the appropriate error will be returned.
  301. */
  302. PJ_INLINE(pj_status_t) pjmedia_circ_buf_copy(pjmedia_circ_buf *circbuf,
  303. unsigned start_idx,
  304. pj_int16_t *data,
  305. unsigned count)
  306. {
  307. pj_int16_t *reg1, *reg2;
  308. unsigned reg1cnt, reg2cnt;
  309. /* Data in the buffer is less than requested */
  310. if (count + start_idx > circbuf->len)
  311. return PJ_ETOOBIG;
  312. pjmedia_circ_buf_get_read_regions(circbuf, &reg1, &reg1cnt,
  313. &reg2, &reg2cnt);
  314. if (reg1cnt > start_idx) {
  315. unsigned tmp_len;
  316. tmp_len = reg1cnt - start_idx;
  317. if (tmp_len > count)
  318. tmp_len = count;
  319. pjmedia_copy_samples(data, reg1 + start_idx, tmp_len);
  320. if (tmp_len < count)
  321. pjmedia_copy_samples(data + tmp_len, reg2, count - tmp_len);
  322. } else {
  323. pjmedia_copy_samples(data, reg2 + start_idx - reg1cnt, count);
  324. }
  325. return PJ_SUCCESS;
  326. }
  327. /**
  328. * Pack the buffer so the first sample will be in the beginning of the buffer.
  329. * This will also make the buffer contiguous.
  330. *
  331. * @param circbuf The circular buffer.
  332. *
  333. * @return PJ_SUCCESS when successful, otherwise
  334. * the appropriate error will be returned.
  335. */
  336. PJ_INLINE(pj_status_t) pjmedia_circ_buf_pack_buffer(pjmedia_circ_buf *circbuf)
  337. {
  338. pj_int16_t *reg1, *reg2;
  339. unsigned reg1cnt, reg2cnt;
  340. unsigned gap;
  341. pjmedia_circ_buf_get_read_regions(circbuf, &reg1, &reg1cnt,
  342. &reg2, &reg2cnt);
  343. /* Check if not contigue */
  344. if (reg2cnt != 0) {
  345. /* Check if no space left to roll the buffer
  346. * (or should this function provide temporary buffer?)
  347. */
  348. gap = circbuf->capacity - pjmedia_circ_buf_get_len(circbuf);
  349. if (gap == 0)
  350. return PJ_ETOOBIG;
  351. /* Roll buffer left using the gap until reg2cnt == 0 */
  352. do {
  353. if (gap > reg2cnt)
  354. gap = reg2cnt;
  355. pjmedia_move_samples(reg1 - gap, reg1, reg1cnt);
  356. pjmedia_copy_samples(reg1 + reg1cnt - gap, reg2, gap);
  357. if (gap < reg2cnt)
  358. pjmedia_move_samples(reg2, reg2 + gap, reg2cnt - gap);
  359. reg1 -= gap;
  360. reg1cnt += gap;
  361. reg2cnt -= gap;
  362. } while (reg2cnt > 0);
  363. }
  364. /* Finally, Shift samples to the left edge */
  365. if (reg1 != circbuf->buf)
  366. pjmedia_move_samples(circbuf->buf, reg1,
  367. pjmedia_circ_buf_get_len(circbuf));
  368. circbuf->start = circbuf->buf;
  369. return PJ_SUCCESS;
  370. }
  371. PJ_END_DECL
  372. /**
  373. * @}
  374. */
  375. #endif