summaryrefslogtreecommitdiff
path: root/pjmedia/src/pjmedia/delaybuf.c
blob: 9042ef99272ace151491958762381c582b47d591 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* $Id$ */
/* 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

#include <pjmedia/delaybuf.h>
#include <pjmedia/circbuf.h>
#include <pjmedia/errno.h>
#include <pjmedia/frame.h>
#include <pjmedia/wsola.h>
#include <pj/assert.h>
#include <pj/lock.h>
#include <pj/log.h>
#include <pj/math.h>
#include <pj/pool.h>


#if 0
#   define TRACE__(x) PJ_LOG(3,x)
#else
#   define TRACE__(x)
#endif

/* Operation types of delay buffer */
enum OP
{
    OP_PUT,
    OP_GET
};

/* Specify time for delaybuf to recalculate effective delay, in ms.
 */
#define RECALC_TIME	    2000

/* Default value of maximum delay, in ms, this value is used when 
 * maximum delay requested is less than ptime (one frame length).
 */
#define DEFAULT_MAX_DELAY   400

/* Number of frames to add to learnt level for additional stability.
 */
#define SAFE_MARGIN	    0

/* This structure describes internal delaybuf settings and states.
 */
struct pjmedia_delay_buf
{
    /* Properties and configuration */
    char	     obj_name[PJ_MAX_OBJ_NAME];
    pj_lock_t	    *lock;		/**< Lock object.		     */
    unsigned	     samples_per_frame; /**< Number of samples in one frame  */
    unsigned	     ptime;		/**< Frame time, in ms		     */
    unsigned	     channel_count;	/**< Channel count, in ms	     */
    pjmedia_circ_buf *circ_buf;		/**< Circular buffer to store audio
					     samples			     */
    unsigned	     max_cnt;		/**< Maximum samples to be buffered  */
    unsigned	     eff_cnt;		/**< Effective count of buffered 
					     samples to keep the optimum
					     balance between delay and 
					     stability. This is calculated 
					     based on burst level.	     */

    /* Learning vars */
    unsigned	     level;		/**< Burst level counter	     */
    enum OP	     last_op;		/**< Last op (GET or PUT) of learning*/
    int		     recalc_timer;	/**< Timer for recalculating max_level*/
    unsigned	     max_level;		/**< Current max burst level	     */

    /* Drift handler */
    pjmedia_wsola   *wsola;		/**< Drift handler		     */
};


PJ_DEF(pj_status_t) pjmedia_delay_buf_create( pj_pool_t *pool,
					      const char *name,
					      unsigned clock_rate,
					      unsigned samples_per_frame,
					      unsigned channel_count,
					      unsigned max_delay,
					      unsigned options,
					      pjmedia_delay_buf **p_b)
{
    pjmedia_delay_buf *b;
    pj_status_t status;

    PJ_ASSERT_RETURN(pool && samples_per_frame && clock_rate && channel_count &&
		     p_b, PJ_EINVAL);

    if (!name) {
	name = "delaybuf";
    }

    b = PJ_POOL_ZALLOC_T(pool, pjmedia_delay_buf);

    pj_ansi_strncpy(b->obj_name, name, PJ_MAX_OBJ_NAME-1);

    b->samples_per_frame = samples_per_frame;
    b->channel_count = channel_count;
    b->ptime = samples_per_frame * 1000 / clock_rate / channel_count;
    if (max_delay < b->ptime)
	max_delay = PJ_MAX(DEFAULT_MAX_DELAY, b->ptime);

    b->max_cnt = samples_per_frame * max_delay / b->ptime;
    b->eff_cnt = b->max_cnt >> 1;
    b->recalc_timer = RECALC_TIME;

    /* Create circular buffer */
    status = pjmedia_circ_buf_create(pool, b->max_cnt, &b->circ_buf);
    if (status != PJ_SUCCESS)
	return status;

    if (!(options & PJMEDIA_DELAY_BUF_SIMPLE_FIFO)) {
        /* Create WSOLA */
        status = pjmedia_wsola_create(pool, clock_rate, samples_per_frame, 1,
				      PJMEDIA_WSOLA_NO_FADING, &b->wsola);
        if (status != PJ_SUCCESS)
	    return status;
        PJ_LOG(5, (b->obj_name, "Using delay buffer with WSOLA."));
    } else {
        PJ_LOG(5, (b->obj_name, "Using simple FIFO delay buffer."));
    }

    /* Finally, create mutex */
    status = pj_lock_create_recursive_mutex(pool, b->obj_name, 
					    &b->lock);
    if (status != PJ_SUCCESS)
	return status;

    *p_b = b;

    TRACE__((b->obj_name,"Delay buffer created"));

    return PJ_SUCCESS;
}

PJ_DEF(pj_status_t) pjmedia_delay_buf_destroy(pjmedia_delay_buf *b)
{
    pj_status_t status = PJ_SUCCESS;

    PJ_ASSERT_RETURN(b, PJ_EINVAL);

    pj_lock_acquire(b->lock);

    if (b->wsola) {
        status = pjmedia_wsola_destroy(b->wsola);
        if (status == PJ_SUCCESS)
	    b->wsola = NULL;
    }

    pj_lock_release(b->lock);

    pj_lock_destroy(b->lock);
    b->lock = NULL;

    return status;
}

/* This function will erase samples from delay buffer.
 * The number of erased samples is guaranteed to be >= erase_cnt.
 */
static void shrink_buffer(pjmedia_delay_buf *b, unsigned erase_cnt)
{
    pj_int16_t *buf1, *buf2;
    unsigned buf1len;
    unsigned buf2len;
    pj_status_t status;

    pj_assert(b && erase_cnt && pjmedia_circ_buf_get_len(b->circ_buf));

    pjmedia_circ_buf_get_read_regions(b->circ_buf, &buf1, &buf1len, 
				      &buf2, &buf2len);
    status = pjmedia_wsola_discard(b->wsola, buf1, buf1len, buf2, buf2len,
				   &erase_cnt);

    if ((status == PJ_SUCCESS) && (erase_cnt > 0)) {
	/* WSOLA discard will manage the first buffer to be full, unless 
	 * erase_cnt is greater than second buffer length. So it is safe
	 * to just set the circular buffer length.
	 */

	pjmedia_circ_buf_set_len(b->circ_buf, 
				 pjmedia_circ_buf_get_len(b->circ_buf) - 
				 erase_cnt);

	PJ_LOG(5,(b->obj_name,"%d samples reduced, buf_cnt=%d", 
	       erase_cnt, pjmedia_circ_buf_get_len(b->circ_buf)));
    }
}

/* Fast increase, slow decrease */
#define AGC_UP(cur, target) cur = (cur + target*3) >> 2
#define AGC_DOWN(cur, target) cur = (cur*3 + target) >> 2
#define AGC(cur, target) \
    if (cur < target) AGC_UP(cur, target); \
    else AGC_DOWN(cur, target)

static void update(pjmedia_delay_buf *b, enum OP op)
{
    /* Sequential operation */
    if (op == b->last_op) {
	++b->level;
	return;
    } 

    /* Switching operation */
    if (b->level > b->max_level)
	b->max_level = b->level;

    b->recalc_timer -= (b->level * b->ptime) >> 1;

    b->last_op = op;
    b->level = 1;

    /* Recalculate effective count based on max_level */
    if (b->recalc_timer <= 0) {
	unsigned new_eff_cnt = (b->max_level+SAFE_MARGIN)*b->samples_per_frame;

	/* Smoothening effective count transition */
	AGC(b->eff_cnt, new_eff_cnt);
	
	/* Make sure the new effective count is multiplication of 
	 * channel_count, so let's round it up.
	 */
	if (b->eff_cnt % b->channel_count)
	    b->eff_cnt += b->channel_count - (b->eff_cnt % b->channel_count);

	TRACE__((b->obj_name,"Cur eff_cnt=%d", b->eff_cnt));
	
	b->max_level = 0;
	b->recalc_timer = RECALC_TIME;
    }

    /* See if we need to shrink the buffer to reduce delay */
    if (op == OP_PUT && pjmedia_circ_buf_get_len(b->circ_buf) > 
	b->samples_per_frame + b->eff_cnt)
    {
	unsigned erase_cnt = b->samples_per_frame >> 1;
	unsigned old_buf_cnt = pjmedia_circ_buf_get_len(b->circ_buf);

	shrink_buffer(b, erase_cnt);
	PJ_LOG(4,(b->obj_name,"Buffer size adjusted from %d to %d (eff_cnt=%d)",
	          old_buf_cnt,
		  pjmedia_circ_buf_get_len(b->circ_buf),
		  b->eff_cnt));
    }
}

PJ_DEF(pj_status_t) pjmedia_delay_buf_put(pjmedia_delay_buf *b,
					   pj_int16_t frame[])
{
    pj_status_t status;

    PJ_ASSERT_RETURN(b && frame, PJ_EINVAL);

    pj_lock_acquire(b->lock);

    if (b->wsola) {
        update(b, OP_PUT);
    
        status = pjmedia_wsola_save(b->wsola, frame, PJ_FALSE);
        if (status != PJ_SUCCESS) {
	    pj_lock_release(b->lock);
	    return status;
        }
    }

    /* Overflow checking */
    if (pjmedia_circ_buf_get_len(b->circ_buf) + b->samples_per_frame > 
	b->max_cnt)
    {
	unsigned erase_cnt;

        if (b->wsola) {
	    /* shrink one frame or just the diff? */
	    //erase_cnt = b->samples_per_frame;
	    erase_cnt = pjmedia_circ_buf_get_len(b->circ_buf) + 
		        b->samples_per_frame - b->max_cnt;

	    shrink_buffer(b, erase_cnt);
        }

	/* Check if shrinking failed or erased count is less than requested,
	 * delaybuf needs to drop eldest samples, this is bad since the voice
	 * samples get rough transition which may produce tick noise.
	 */
	if (pjmedia_circ_buf_get_len(b->circ_buf) + b->samples_per_frame > 
	    b->max_cnt) 
	{
	    erase_cnt = pjmedia_circ_buf_get_len(b->circ_buf) + 
			b->samples_per_frame - b->max_cnt;

	    pjmedia_circ_buf_adv_read_ptr(b->circ_buf, erase_cnt);

	    PJ_LOG(4,(b->obj_name,"%sDropping %d eldest samples, buf_cnt=%d",
                      (b->wsola? "Shrinking failed or insufficient. ": ""),
                      erase_cnt, pjmedia_circ_buf_get_len(b->circ_buf)));
	}
    }

    pjmedia_circ_buf_write(b->circ_buf, frame, b->samples_per_frame);

    pj_lock_release(b->lock);
    return PJ_SUCCESS;
}

PJ_DEF(pj_status_t) pjmedia_delay_buf_get( pjmedia_delay_buf *b,
					   pj_int16_t frame[])
{
    pj_status_t status = PJ_SUCCESS;

    PJ_ASSERT_RETURN(b && frame, PJ_EINVAL);

    pj_lock_acquire(b->lock);

    if (b->wsola)
        update(b, OP_GET);

    /* Starvation checking */
    if (pjmedia_circ_buf_get_len(b->circ_buf) < b->samples_per_frame) {

	PJ_LOG(4,(b->obj_name,"Underflow, buf_cnt=%d, will generate 1 frame",
		  pjmedia_circ_buf_get_len(b->circ_buf)));

        if (b->wsola) {
            status = pjmedia_wsola_generate(b->wsola, frame);

	    if (status == PJ_SUCCESS) {
	        TRACE__((b->obj_name,"Successfully generate 1 frame"));
	        if (pjmedia_circ_buf_get_len(b->circ_buf) == 0) {
		    pj_lock_release(b->lock);
		    return PJ_SUCCESS;
	        }

	        /* Put generated frame into buffer */
	        pjmedia_circ_buf_write(b->circ_buf, frame,
                                       b->samples_per_frame);
            }
        }

	if (!b->wsola || status != PJ_SUCCESS) {
	    unsigned buf_len = pjmedia_circ_buf_get_len(b->circ_buf);
	    
	    /* Give all what delay buffer has, then pad with zeroes */
            if (b->wsola)
	        PJ_LOG(4,(b->obj_name,"Error generating frame, status=%d", 
		          status));

	    pjmedia_circ_buf_read(b->circ_buf, frame, buf_len);
	    pjmedia_zero_samples(&frame[buf_len], 
				 b->samples_per_frame - buf_len);

	    /* The buffer is empty now, reset it */
	    pjmedia_circ_buf_reset(b->circ_buf);

	    pj_lock_release(b->lock);

	    return PJ_SUCCESS;
	}
    }

    pjmedia_circ_buf_read(b->circ_buf, frame, b->samples_per_frame);

    pj_lock_release(b->lock);

    return PJ_SUCCESS;
}


PJ_DEF(pj_status_t) pjmedia_delay_buf_reset(pjmedia_delay_buf *b)
{
    PJ_ASSERT_RETURN(b, PJ_EINVAL);

    pj_lock_acquire(b->lock);

    b->recalc_timer = RECALC_TIME;

    /* Reset buffer */
    pjmedia_circ_buf_reset(b->circ_buf);

    /* Reset WSOLA */
    if (b->wsola)
        pjmedia_wsola_reset(b->wsola, 0);

    pj_lock_release(b->lock);

    PJ_LOG(5,(b->obj_name,"Delay buffer is reset"));

    return PJ_SUCCESS;
}