~ubuntu-branches/ubuntu/trusty/sflphone/trusty

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.0.1/pjmedia/include/pjmedia/circbuf.h

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (4.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20140128182336-jrsv0k9u6cawc068
Tags: 1.3.0-1
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: circbuf.h 3664 2011-07-19 03:42:28Z nanang $ */
2
 
/*
3
 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
 
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5
 
 *
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.
10
 
 *
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.
15
 
 *
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
19
 
 */
20
 
 
21
 
#ifndef __PJMEDIA_CIRC_BUF_H__
22
 
#define __PJMEDIA_CIRC_BUF_H__
23
 
 
24
 
/**
25
 
 * @file circbuf.h
26
 
 * @brief Circular Buffer.
27
 
 */
28
 
 
29
 
#include <pj/assert.h>
30
 
#include <pj/errno.h>
31
 
#include <pj/pool.h>
32
 
#include <pjmedia/frame.h>
33
 
 
34
 
/**
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.
42
 
 *
43
 
 * @{
44
 
 *
45
 
 * This section describes PJMEDIA's implementation of circular buffer.
46
 
 */
47
 
 
48
 
/* Algorithm checkings, for development purpose only */
49
 
#if 0
50
 
#   define PJMEDIA_CIRC_BUF_CHECK(x) pj_assert(x)
51
 
#else
52
 
#   define PJMEDIA_CIRC_BUF_CHECK(x)
53
 
#endif
54
 
 
55
 
PJ_BEGIN_DECL
56
 
 
57
 
/**
58
 
 * Circular buffer structure
59
 
 */
60
 
typedef struct pjmedia_circ_buf {
61
 
    pj_int16_t      *buf;           /**< The buffer                     */
62
 
    unsigned         capacity;      /**< Buffer capacity, in samples    */
63
 
 
64
 
    pj_int16_t      *start;         /**< Pointer to the first sample    */
65
 
    unsigned         len;           /**< Audio samples length,
66
 
                                         in samples                     */
67
 
} pjmedia_circ_buf;
68
 
 
69
 
 
70
 
/**
71
 
 * Create the circular buffer.
72
 
 *
73
 
 * @param pool              Pool where the circular buffer will be allocated
74
 
 *                          from.
75
 
 * @param capacity          Capacity of the buffer, in samples.
76
 
 * @param p_cb              Pointer to receive the circular buffer instance.
77
 
 *
78
 
 * @return                  PJ_SUCCESS if the circular buffer has been
79
 
 *                          created successfully, otherwise the appropriate
80
 
 *                          error will be returned.
81
 
 */
82
 
PJ_INLINE(pj_status_t) pjmedia_circ_buf_create(pj_pool_t *pool,
83
 
                                               unsigned capacity,
84
 
                                               pjmedia_circ_buf **p_cb)
85
 
{
86
 
    pjmedia_circ_buf *cbuf;
87
 
 
88
 
    cbuf = PJ_POOL_ZALLOC_T(pool, pjmedia_circ_buf);
89
 
    cbuf->buf = (pj_int16_t*) pj_pool_calloc(pool, capacity,
90
 
                                             sizeof(pj_int16_t));
91
 
    cbuf->capacity = capacity;
92
 
    cbuf->start = cbuf->buf;
93
 
    cbuf->len = 0;
94
 
 
95
 
    *p_cb = cbuf;
96
 
 
97
 
    return PJ_SUCCESS;
98
 
}
99
 
 
100
 
 
101
 
/**
102
 
 * Reset the circular buffer.
103
 
 *
104
 
 * @param circbuf           The circular buffer.
105
 
 *
106
 
 * @return                  PJ_SUCCESS when successful.
107
 
 */
108
 
PJ_INLINE(pj_status_t) pjmedia_circ_buf_reset(pjmedia_circ_buf *circbuf)
109
 
{
110
 
    circbuf->start = circbuf->buf;
111
 
    circbuf->len = 0;
112
 
 
113
 
    return PJ_SUCCESS;
114
 
}
115
 
 
116
 
 
117
 
/**
118
 
 * Get the circular buffer length, it is number of samples buffered in the
119
 
 * circular buffer.
120
 
 *
121
 
 * @param circbuf           The circular buffer.
122
 
 *
123
 
 * @return                  The buffer length.
124
 
 */
125
 
PJ_INLINE(unsigned) pjmedia_circ_buf_get_len(pjmedia_circ_buf *circbuf)
126
 
{
127
 
    return circbuf->len;
128
 
}
129
 
 
130
 
 
131
 
/**
132
 
 * Set circular buffer length. This is useful when audio buffer is manually
133
 
 * manipulated by the user, e.g: shrinked, expanded.
134
 
 *
135
 
 * @param circbuf           The circular buffer.
136
 
 * @param len               The new buffer length.
137
 
 */
138
 
PJ_INLINE(void) pjmedia_circ_buf_set_len(pjmedia_circ_buf *circbuf,
139
 
                                         unsigned len)
140
 
{
141
 
    PJMEDIA_CIRC_BUF_CHECK(len <= circbuf->capacity);
142
 
    circbuf->len = len;
143
 
}
144
 
 
145
 
 
146
 
/**
147
 
 * Advance the read pointer of circular buffer. This function will discard
148
 
 * the skipped samples while advancing the read pointer, thus reducing
149
 
 * the buffer length.
150
 
 *
151
 
 * @param circbuf           The circular buffer.
152
 
 * @param count             Distance from current read pointer, can only be
153
 
 *                          possitive number, in samples.
154
 
 *
155
 
 * @return                  PJ_SUCCESS when successful, otherwise
156
 
 *                          the appropriate error will be returned.
157
 
 */
158
 
PJ_INLINE(pj_status_t) pjmedia_circ_buf_adv_read_ptr(pjmedia_circ_buf *circbuf,
159
 
                                                     unsigned count)
160
 
{
161
 
    if (count >= circbuf->len)
162
 
        return pjmedia_circ_buf_reset(circbuf);
163
 
 
164
 
    PJMEDIA_CIRC_BUF_CHECK(count <= circbuf->len);
165
 
 
166
 
    circbuf->start += count;
167
 
    if (circbuf->start >= circbuf->buf + circbuf->capacity)
168
 
        circbuf->start -= circbuf->capacity;
169
 
    circbuf->len -= count;
170
 
 
171
 
    return PJ_SUCCESS;
172
 
}
173
 
 
174
 
 
175
 
/**
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.
179
 
 *
180
 
 * @param circbuf           The circular buffer.
181
 
 * @param count             Distance from current write pointer, can only be
182
 
 *                          possitive number, in samples.
183
 
 *
184
 
 * @return                  PJ_SUCCESS when successful, otherwise
185
 
 *                          the appropriate error will be returned.
186
 
 */
187
 
PJ_INLINE(pj_status_t) pjmedia_circ_buf_adv_write_ptr(pjmedia_circ_buf *circbuf,
188
 
                                                      unsigned count)
189
 
{
190
 
    if (count + circbuf->len > circbuf->capacity)
191
 
        return PJ_ETOOBIG;
192
 
 
193
 
    circbuf->len += count;
194
 
 
195
 
    return PJ_SUCCESS;
196
 
}
197
 
 
198
 
 
199
 
/**
200
 
 * Get the real buffer addresses containing the audio samples.
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_read_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;
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;
222
 
    } else {
223
 
        *reg2 = NULL;
224
 
        *reg2_len = 0;
225
 
    }
226
 
 
227
 
    PJMEDIA_CIRC_BUF_CHECK(*reg1_len != 0 || (*reg1_len == 0 &&
228
 
                                              circbuf->len == 0));
229
 
    PJMEDIA_CIRC_BUF_CHECK(*reg1_len + *reg2_len == circbuf->len);
230
 
}
231
 
 
232
 
 
233
 
/**
234
 
 * Get the real buffer addresses that is empty or writeable.
235
 
 *
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,
239
 
 *                          in samples.
240
 
 * @param reg2              Pointer to store the second buffer address.
241
 
 * @param reg2_len          Pointer to store the length of the second buffer,
242
 
 *                          in samples.
243
 
 */
244
 
PJ_INLINE(void) pjmedia_circ_buf_get_write_regions(pjmedia_circ_buf *circbuf,
245
 
                                                   pj_int16_t **reg1,
246
 
                                                   unsigned *reg1_len,
247
 
                                                   pj_int16_t **reg2,
248
 
                                                   unsigned *reg2_len)
249
 
{
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;
258
 
    } else {
259
 
        *reg2 = NULL;
260
 
        *reg2_len = 0;
261
 
    }
262
 
 
263
 
    PJMEDIA_CIRC_BUF_CHECK(*reg1_len != 0 || (*reg1_len == 0 &&
264
 
                                              circbuf->len == 0));
265
 
    PJMEDIA_CIRC_BUF_CHECK(*reg1_len + *reg2_len == circbuf->capacity -
266
 
                           circbuf->len);
267
 
}
268
 
 
269
 
 
270
 
/**
271
 
 * Read audio samples from the circular buffer.
272
 
 *
273
 
 * @param circbuf           The circular buffer.
274
 
 * @param data              Buffer to store the read audio samples.
275
 
 * @param count             Number of samples being read.
276
 
 *
277
 
 * @return                  PJ_SUCCESS when successful, otherwise
278
 
 *                          the appropriate error will be returned.
279
 
 */
280
 
PJ_INLINE(pj_status_t) pjmedia_circ_buf_read(pjmedia_circ_buf *circbuf,
281
 
                                             pj_int16_t *data,
282
 
                                             unsigned count)
283
 
{
284
 
    pj_int16_t *reg1, *reg2;
285
 
    unsigned reg1cnt, reg2cnt;
286
 
 
287
 
    /* Data in the buffer is less than requested */
288
 
    if (count > circbuf->len)
289
 
        return PJ_ETOOBIG;
290
 
 
291
 
    pjmedia_circ_buf_get_read_regions(circbuf, &reg1, &reg1cnt,
292
 
                                      &reg2, &reg2cnt);
293
 
    if (reg1cnt >= count) {
294
 
        pjmedia_copy_samples(data, reg1, count);
295
 
    } else {
296
 
        pjmedia_copy_samples(data, reg1, reg1cnt);
297
 
        pjmedia_copy_samples(data + reg1cnt, reg2, count - reg1cnt);
298
 
    }
299
 
 
300
 
    return pjmedia_circ_buf_adv_read_ptr(circbuf, count);
301
 
}
302
 
 
303
 
 
304
 
/**
305
 
 * Write audio samples to the circular buffer.
306
 
 *
307
 
 * @param circbuf           The circular buffer.
308
 
 * @param data              Audio samples to be written.
309
 
 * @param count             Number of samples being written.
310
 
 *
311
 
 * @return                  PJ_SUCCESS when successful, otherwise
312
 
 *                          the appropriate error will be returned.
313
 
 */
314
 
PJ_INLINE(pj_status_t) pjmedia_circ_buf_write(pjmedia_circ_buf *circbuf,
315
 
                                              pj_int16_t *data,
316
 
                                              unsigned count)
317
 
{
318
 
    pj_int16_t *reg1, *reg2;
319
 
    unsigned reg1cnt, reg2cnt;
320
 
 
321
 
    /* Data to write is larger than buffer can store */
322
 
    if (count > circbuf->capacity - circbuf->len)
323
 
        return PJ_ETOOBIG;
324
 
 
325
 
    pjmedia_circ_buf_get_write_regions(circbuf, &reg1, &reg1cnt,
326
 
                                       &reg2, &reg2cnt);
327
 
    if (reg1cnt >= count) {
328
 
        pjmedia_copy_samples(reg1, data, count);
329
 
    } else {
330
 
        pjmedia_copy_samples(reg1, data, reg1cnt);
331
 
        pjmedia_copy_samples(reg2, data + reg1cnt, count - reg1cnt);
332
 
    }
333
 
 
334
 
    return pjmedia_circ_buf_adv_write_ptr(circbuf, count);
335
 
}
336
 
 
337
 
 
338
 
/**
339
 
 * Copy audio samples from the circular buffer without changing its state.
340
 
 *
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.
345
 
 *
346
 
 * @return                  PJ_SUCCESS when successful, otherwise
347
 
 *                          the appropriate error will be returned.
348
 
 */
349
 
PJ_INLINE(pj_status_t) pjmedia_circ_buf_copy(pjmedia_circ_buf *circbuf,
350
 
                                             unsigned start_idx,
351
 
                                             pj_int16_t *data,
352
 
                                             unsigned count)
353
 
{
354
 
    pj_int16_t *reg1, *reg2;
355
 
    unsigned reg1cnt, reg2cnt;
356
 
 
357
 
    /* Data in the buffer is less than requested */
358
 
    if (count + start_idx > circbuf->len)
359
 
        return PJ_ETOOBIG;
360
 
 
361
 
    pjmedia_circ_buf_get_read_regions(circbuf, &reg1, &reg1cnt,
362
 
                                      &reg2, &reg2cnt);
363
 
    if (reg1cnt > start_idx) {
364
 
        unsigned tmp_len;
365
 
        tmp_len = reg1cnt - start_idx;
366
 
        if (tmp_len > count)
367
 
            tmp_len = count;
368
 
        pjmedia_copy_samples(data, reg1 + start_idx, tmp_len);
369
 
        if (tmp_len < count)
370
 
            pjmedia_copy_samples(data + tmp_len, reg2, count - tmp_len);
371
 
    } else {
372
 
        pjmedia_copy_samples(data, reg2 + start_idx - reg1cnt, count);
373
 
    }
374
 
 
375
 
    return PJ_SUCCESS;
376
 
}
377
 
 
378
 
 
379
 
/**
380
 
 * Pack the buffer so the first sample will be in the beginning of the buffer.
381
 
 * This will also make the buffer contiguous.
382
 
 *
383
 
 * @param circbuf           The circular buffer.
384
 
 *
385
 
 * @return                  PJ_SUCCESS when successful, otherwise
386
 
 *                          the appropriate error will be returned.
387
 
 */
388
 
PJ_INLINE(pj_status_t) pjmedia_circ_buf_pack_buffer(pjmedia_circ_buf *circbuf)
389
 
{
390
 
    pj_int16_t *reg1, *reg2;
391
 
    unsigned reg1cnt, reg2cnt;
392
 
    unsigned gap;
393
 
 
394
 
    pjmedia_circ_buf_get_read_regions(circbuf, &reg1, &reg1cnt,
395
 
                                      &reg2, &reg2cnt);
396
 
 
397
 
    /* Check if not contigue */
398
 
    if (reg2cnt != 0) {
399
 
        /* Check if no space left to roll the buffer
400
 
         * (or should this function provide temporary buffer?)
401
 
         */
402
 
        gap = circbuf->capacity - pjmedia_circ_buf_get_len(circbuf);
403
 
        if (gap == 0)
404
 
            return PJ_ETOOBIG;
405
 
 
406
 
        /* Roll buffer left using the gap until reg2cnt == 0 */
407
 
        do {
408
 
            if (gap > reg2cnt)
409
 
                gap = reg2cnt;
410
 
            pjmedia_move_samples(reg1 - gap, reg1, reg1cnt);
411
 
            pjmedia_copy_samples(reg1 + reg1cnt - gap, reg2, gap);
412
 
            if (gap < reg2cnt)
413
 
                pjmedia_move_samples(reg2, reg2 + gap, reg2cnt - gap);
414
 
            reg1 -= gap;
415
 
            reg1cnt += gap;
416
 
            reg2cnt -= gap;
417
 
        } while (reg2cnt > 0);
418
 
    }
419
 
 
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;
425
 
 
426
 
    return PJ_SUCCESS;
427
 
}
428
 
 
429
 
 
430
 
PJ_END_DECL
431
 
 
432
 
/**
433
 
 * @}
434
 
 */
435
 
 
436
 
#endif