summaryrefslogtreecommitdiff
path: root/pjmedia/src/pjmedia/plc_common.c
blob: 18ffd3c596309105493a58ac68a2e6d1f37119e4 (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
/* $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/plc.h>
#include <pjmedia/errno.h>
#include <pjmedia/wsola.h>
#include <pj/assert.h>
#include <pj/pool.h>
#include <pj/string.h>


static void* plc_wsola_create(pj_pool_t*, unsigned c, unsigned f);
static void  plc_wsola_save(void*, pj_int16_t*);
static void  plc_wsola_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*);
};


static struct plc_alg plc_wsola =
{
    &plc_wsola_create,
    &plc_wsola_save,
    &plc_wsola_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_T(pool, pjmedia_plc);

    plc->op = &plc_wsola;
    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;
}


//////////////////////////////////////////////////////////////////////////////
/*
 * Packet loss concealment based on WSOLA
 */
struct wsola_plc
{
    pjmedia_wsola   *wsola;
    pj_bool_t	     prev_lost;
};


static void* plc_wsola_create(pj_pool_t *pool, unsigned clock_rate, 
			      unsigned samples_per_frame)
{
    struct wsola_plc *o;
    unsigned flag;
    pj_status_t status;

    PJ_UNUSED_ARG(clock_rate);

    o = PJ_POOL_ZALLOC_T(pool, struct wsola_plc);
    o->prev_lost = PJ_FALSE;

    flag = PJMEDIA_WSOLA_NO_DISCARD;
    if (PJMEDIA_WSOLA_PLC_NO_FADING)
	flag |= PJMEDIA_WSOLA_NO_FADING;

    status = pjmedia_wsola_create(pool, clock_rate, samples_per_frame, 1,
				  flag, &o->wsola);
    if (status != PJ_SUCCESS)
	return NULL;

    return o;
}

static void plc_wsola_save(void *plc, pj_int16_t *frame)
{
    struct wsola_plc *o = (struct wsola_plc*) plc;

    pjmedia_wsola_save(o->wsola, frame, o->prev_lost);
    o->prev_lost = PJ_FALSE;
}

static void plc_wsola_generate(void *plc, pj_int16_t *frame)
{
    struct wsola_plc *o = (struct wsola_plc*) plc;
    
    pjmedia_wsola_generate(o->wsola, frame);
    o->prev_lost = PJ_TRUE;
}