summaryrefslogtreecommitdiff
path: root/pjmedia/src/pjmedia/plc_common.c
blob: 04ed88cb3e724831bc23683ee80f54dfd36cccf9 (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
/* $Id$ */
/* 
 * Copyright (C) 2003-2007 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/plc.h>
#include <pjmedia/errno.h>
#include <pj/assert.h>
#include <pj/pool.h>
#include <pj/string.h>


static void* plc_replay_create(pj_pool_t*, unsigned c, unsigned f);
static void  plc_replay_save(void*, pj_int16_t*);
static void  plc_replay_generate(void*, pj_int16_t*);

extern void* pjmedia_plc_steveu_create(pj_pool_t*, unsigned c, unsigned f);
extern void  pjmedia_plc_steveu_save(void*, pj_int16_t*);
extern void  pjmedia_plc_steveu_generate(void*, pj_int16_t*);


/**
 * This struct is used internally to represent a PLC backend.
 */
struct plc_alg
{
    void* (*plc_create)(pj_pool_t*, unsigned c, unsigned f);
    void  (*plc_save)(void*, pj_int16_t*);
    void  (*plc_generate)(void*, pj_int16_t*);
};


#if defined(PJMEDIA_HAS_STEVEU_PLC) && PJMEDIA_HAS_STEVEU_PLC!=0
static struct plc_alg plc_steveu =
{
    &pjmedia_plc_steveu_create,
    &pjmedia_plc_steveu_save,
    &pjmedia_plc_steveu_generate
};
#endif


static struct plc_alg plc_replay =
{
    &plc_replay_create,
    &plc_replay_save,
    &plc_replay_generate
};


struct pjmedia_plc
{
    void	    *obj;
    struct plc_alg  *op;
};


/*
 * Create PLC session. This function will select the PLC algorithm to
 * use based on the arguments.
 */
PJ_DEF(pj_status_t) pjmedia_plc_create( pj_pool_t *pool,
					unsigned clock_rate,
					unsigned samples_per_frame,
					unsigned options,
					pjmedia_plc **p_plc)
{
    pjmedia_plc *plc;

    PJ_ASSERT_RETURN(pool && clock_rate && samples_per_frame && p_plc,
		     PJ_EINVAL);
    PJ_ASSERT_RETURN(options == 0, PJ_EINVAL);

    PJ_UNUSED_ARG(options);

    plc = pj_pool_zalloc(pool, sizeof(pjmedia_plc));

    if (0)
	;
#if defined(PJMEDIA_HAS_STEVEU_PLC) && PJMEDIA_HAS_STEVEU_PLC!=0
    else if (clock_rate == 8000)
	plc->op = &plc_steveu;
#endif
    else
	plc->op = &plc_replay;

    plc->obj = plc->op->plc_create(pool, clock_rate, samples_per_frame);

    *p_plc = plc;

    return PJ_SUCCESS;
}


/*
 * Save a good frame to PLC.
 */
PJ_DEF(pj_status_t) pjmedia_plc_save( pjmedia_plc *plc,
				      pj_int16_t *frame )
{
    PJ_ASSERT_RETURN(plc && frame, PJ_EINVAL);
 
    plc->op->plc_save(plc->obj, frame);
    return PJ_SUCCESS;
}


/*
 * Generate a replacement for lost frame.
 */
PJ_DEF(pj_status_t) pjmedia_plc_generate( pjmedia_plc *plc,
					  pj_int16_t *frame )
{
    PJ_ASSERT_RETURN(plc && frame, PJ_EINVAL);
    
    plc->op->plc_generate(plc->obj, frame);
    return PJ_SUCCESS;
}


//////////////////////////////////////////////////////////////////////////////
/*
 * Simple replay based plc
 */
struct replay_plc
{
    unsigned	size;
    unsigned	replay_cnt;
    pj_int16_t *frame;
};


static void* plc_replay_create(pj_pool_t *pool, unsigned clock_rate, 
			       unsigned samples_per_frame)
{
    struct replay_plc *o;

    PJ_UNUSED_ARG(clock_rate);

    o = pj_pool_alloc(pool, sizeof(struct replay_plc));
    o->size = samples_per_frame * 2;
    o->replay_cnt = 0;
    o->frame = pj_pool_zalloc(pool, o->size);

    return o;
}

static void plc_replay_save(void *plc, pj_int16_t *frame)
{
    struct replay_plc *o = plc;

    pj_memcpy(o->frame, frame, o->size);
    o->replay_cnt = 0;
}

static void plc_replay_generate(void *plc, pj_int16_t *frame)
{
    struct replay_plc *o = plc;
    unsigned i, count;
    pj_int16_t *samp;

    ++o->replay_cnt;

    if (o->replay_cnt < 16) {
	pj_memcpy(frame, o->frame, o->size);
    

	count = o->size / 2;
	samp = o->frame;
	for (i=0; i<count; ++i)
	    samp[i] = (pj_int16_t)(samp[i] >> 1);
    } else {
	pj_bzero(frame, o->size);
    }
}