1
/* $Id: circbuf.h 3664 2011-07-19 03:42:28Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#ifndef __PJMEDIA_CIRC_BUF_H__
22
#define __PJMEDIA_CIRC_BUF_H__
26
* @brief Circular Buffer.
29
#include <pj/assert.h>
32
#include <pjmedia/frame.h>
35
* @defgroup PJMED_CIRCBUF Circular Buffer
36
* @ingroup PJMEDIA_FRAME_OP
37
* @brief Circular buffer manages read and write contiguous audio samples in a
38
* non-contiguous buffer as if the buffer were contiguous. This should give
39
* better performance than keeping contiguous samples in a contiguous buffer,
40
* since read/write operations will only update the pointers, instead of
41
* shifting audio samples.
45
* This section describes PJMEDIA's implementation of circular buffer.
48
/* Algorithm checkings, for development purpose only */
50
# define PJMEDIA_CIRC_BUF_CHECK(x) pj_assert(x)
52
# define PJMEDIA_CIRC_BUF_CHECK(x)
58
* Circular buffer structure
60
typedef struct pjmedia_circ_buf {
61
pj_int16_t *buf; /**< The buffer */
62
unsigned capacity; /**< Buffer capacity, in samples */
64
pj_int16_t *start; /**< Pointer to the first sample */
65
unsigned len; /**< Audio samples length,
71
* Create the circular buffer.
73
* @param pool Pool where the circular buffer will be allocated
75
* @param capacity Capacity of the buffer, in samples.
76
* @param p_cb Pointer to receive the circular buffer instance.
78
* @return PJ_SUCCESS if the circular buffer has been
79
* created successfully, otherwise the appropriate
80
* error will be returned.
82
PJ_INLINE(pj_status_t) pjmedia_circ_buf_create(pj_pool_t *pool,
84
pjmedia_circ_buf **p_cb)
86
pjmedia_circ_buf *cbuf;
88
cbuf = PJ_POOL_ZALLOC_T(pool, pjmedia_circ_buf);
89
cbuf->buf = (pj_int16_t*) pj_pool_calloc(pool, capacity,
91
cbuf->capacity = capacity;
92
cbuf->start = cbuf->buf;
102
* Reset the circular buffer.
104
* @param circbuf The circular buffer.
106
* @return PJ_SUCCESS when successful.
108
PJ_INLINE(pj_status_t) pjmedia_circ_buf_reset(pjmedia_circ_buf *circbuf)
110
circbuf->start = circbuf->buf;
118
* Get the circular buffer length, it is number of samples buffered in the
121
* @param circbuf The circular buffer.
123
* @return The buffer length.
125
PJ_INLINE(unsigned) pjmedia_circ_buf_get_len(pjmedia_circ_buf *circbuf)
132
* Set circular buffer length. This is useful when audio buffer is manually
133
* manipulated by the user, e.g: shrinked, expanded.
135
* @param circbuf The circular buffer.
136
* @param len The new buffer length.
138
PJ_INLINE(void) pjmedia_circ_buf_set_len(pjmedia_circ_buf *circbuf,
141
PJMEDIA_CIRC_BUF_CHECK(len <= circbuf->capacity);
147
* Advance the read pointer of circular buffer. This function will discard
148
* the skipped samples while advancing the read pointer, thus reducing
151
* @param circbuf The circular buffer.
152
* @param count Distance from current read pointer, can only be
153
* possitive number, in samples.
155
* @return PJ_SUCCESS when successful, otherwise
156
* the appropriate error will be returned.
158
PJ_INLINE(pj_status_t) pjmedia_circ_buf_adv_read_ptr(pjmedia_circ_buf *circbuf,
161
if (count >= circbuf->len)
162
return pjmedia_circ_buf_reset(circbuf);
164
PJMEDIA_CIRC_BUF_CHECK(count <= circbuf->len);
166
circbuf->start += count;
167
if (circbuf->start >= circbuf->buf + circbuf->capacity)
168
circbuf->start -= circbuf->capacity;
169
circbuf->len -= count;
176
* Advance the write pointer of circular buffer. Since write pointer is always
177
* pointing to a sample after the end of sample, so this function also means
178
* increasing the buffer length.
180
* @param circbuf The circular buffer.
181
* @param count Distance from current write pointer, can only be
182
* possitive number, in samples.
184
* @return PJ_SUCCESS when successful, otherwise
185
* the appropriate error will be returned.
187
PJ_INLINE(pj_status_t) pjmedia_circ_buf_adv_write_ptr(pjmedia_circ_buf *circbuf,
190
if (count + circbuf->len > circbuf->capacity)
193
circbuf->len += count;
200
* Get the real buffer addresses containing the audio samples.
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,
206
* @param reg2 Pointer to store the second buffer address.
207
* @param reg2_len Pointer to store the length of the second buffer,
210
PJ_INLINE(void) pjmedia_circ_buf_get_read_regions(pjmedia_circ_buf *circbuf,
216
*reg1 = circbuf->start;
217
*reg1_len = circbuf->len;
218
if (*reg1 + *reg1_len > circbuf->buf + circbuf->capacity) {
219
*reg1_len = circbuf->buf + circbuf->capacity - circbuf->start;
220
*reg2 = circbuf->buf;
221
*reg2_len = circbuf->len - *reg1_len;
227
PJMEDIA_CIRC_BUF_CHECK(*reg1_len != 0 || (*reg1_len == 0 &&
229
PJMEDIA_CIRC_BUF_CHECK(*reg1_len + *reg2_len == circbuf->len);
234
* Get the real buffer addresses that is empty or writeable.
236
* @param circbuf The circular buffer.
237
* @param reg1 Pointer to store the first buffer address.
238
* @param reg1_len Pointer to store the length of the first buffer,
240
* @param reg2 Pointer to store the second buffer address.
241
* @param reg2_len Pointer to store the length of the second buffer,
244
PJ_INLINE(void) pjmedia_circ_buf_get_write_regions(pjmedia_circ_buf *circbuf,
250
*reg1 = circbuf->start + circbuf->len;
251
if (*reg1 >= circbuf->buf + circbuf->capacity)
252
*reg1 -= circbuf->capacity;
253
*reg1_len = circbuf->capacity - circbuf->len;
254
if (*reg1 + *reg1_len > circbuf->buf + circbuf->capacity) {
255
*reg1_len = circbuf->buf + circbuf->capacity - *reg1;
256
*reg2 = circbuf->buf;
257
*reg2_len = circbuf->start - circbuf->buf;
263
PJMEDIA_CIRC_BUF_CHECK(*reg1_len != 0 || (*reg1_len == 0 &&
265
PJMEDIA_CIRC_BUF_CHECK(*reg1_len + *reg2_len == circbuf->capacity -
271
* Read audio samples from the circular buffer.
273
* @param circbuf The circular buffer.
274
* @param data Buffer to store the read audio samples.
275
* @param count Number of samples being read.
277
* @return PJ_SUCCESS when successful, otherwise
278
* the appropriate error will be returned.
280
PJ_INLINE(pj_status_t) pjmedia_circ_buf_read(pjmedia_circ_buf *circbuf,
284
pj_int16_t *reg1, *reg2;
285
unsigned reg1cnt, reg2cnt;
287
/* Data in the buffer is less than requested */
288
if (count > circbuf->len)
291
pjmedia_circ_buf_get_read_regions(circbuf, ®1, ®1cnt,
293
if (reg1cnt >= count) {
294
pjmedia_copy_samples(data, reg1, count);
296
pjmedia_copy_samples(data, reg1, reg1cnt);
297
pjmedia_copy_samples(data + reg1cnt, reg2, count - reg1cnt);
300
return pjmedia_circ_buf_adv_read_ptr(circbuf, count);
305
* Write audio samples to the circular buffer.
307
* @param circbuf The circular buffer.
308
* @param data Audio samples to be written.
309
* @param count Number of samples being written.
311
* @return PJ_SUCCESS when successful, otherwise
312
* the appropriate error will be returned.
314
PJ_INLINE(pj_status_t) pjmedia_circ_buf_write(pjmedia_circ_buf *circbuf,
318
pj_int16_t *reg1, *reg2;
319
unsigned reg1cnt, reg2cnt;
321
/* Data to write is larger than buffer can store */
322
if (count > circbuf->capacity - circbuf->len)
325
pjmedia_circ_buf_get_write_regions(circbuf, ®1, ®1cnt,
327
if (reg1cnt >= count) {
328
pjmedia_copy_samples(reg1, data, count);
330
pjmedia_copy_samples(reg1, data, reg1cnt);
331
pjmedia_copy_samples(reg2, data + reg1cnt, count - reg1cnt);
334
return pjmedia_circ_buf_adv_write_ptr(circbuf, count);
339
* Copy audio samples from the circular buffer without changing its state.
341
* @param circbuf The circular buffer.
342
* @param start_idx Starting sample index to be copied.
343
* @param data Buffer to store the read audio samples.
344
* @param count Number of samples being read.
346
* @return PJ_SUCCESS when successful, otherwise
347
* the appropriate error will be returned.
349
PJ_INLINE(pj_status_t) pjmedia_circ_buf_copy(pjmedia_circ_buf *circbuf,
354
pj_int16_t *reg1, *reg2;
355
unsigned reg1cnt, reg2cnt;
357
/* Data in the buffer is less than requested */
358
if (count + start_idx > circbuf->len)
361
pjmedia_circ_buf_get_read_regions(circbuf, ®1, ®1cnt,
363
if (reg1cnt > start_idx) {
365
tmp_len = reg1cnt - start_idx;
368
pjmedia_copy_samples(data, reg1 + start_idx, tmp_len);
370
pjmedia_copy_samples(data + tmp_len, reg2, count - tmp_len);
372
pjmedia_copy_samples(data, reg2 + start_idx - reg1cnt, count);
380
* Pack the buffer so the first sample will be in the beginning of the buffer.
381
* This will also make the buffer contiguous.
383
* @param circbuf The circular buffer.
385
* @return PJ_SUCCESS when successful, otherwise
386
* the appropriate error will be returned.
388
PJ_INLINE(pj_status_t) pjmedia_circ_buf_pack_buffer(pjmedia_circ_buf *circbuf)
390
pj_int16_t *reg1, *reg2;
391
unsigned reg1cnt, reg2cnt;
394
pjmedia_circ_buf_get_read_regions(circbuf, ®1, ®1cnt,
397
/* Check if not contigue */
399
/* Check if no space left to roll the buffer
400
* (or should this function provide temporary buffer?)
402
gap = circbuf->capacity - pjmedia_circ_buf_get_len(circbuf);
406
/* Roll buffer left using the gap until reg2cnt == 0 */
410
pjmedia_move_samples(reg1 - gap, reg1, reg1cnt);
411
pjmedia_copy_samples(reg1 + reg1cnt - gap, reg2, gap);
413
pjmedia_move_samples(reg2, reg2 + gap, reg2cnt - gap);
417
} while (reg2cnt > 0);
420
/* Finally, Shift samples to the left edge */
421
if (reg1 != circbuf->buf)
422
pjmedia_move_samples(circbuf->buf, reg1,
423
pjmedia_circ_buf_get_len(circbuf));
424
circbuf->start = circbuf->buf;