blob: 18ffd3c596309105493a58ac68a2e6d1f37119e4 [file] [log] [blame]
Alexandre Lision8af73cb2013-12-10 14:11:20 -05001/* $Id$ */
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#include <pjmedia/plc.h>
21#include <pjmedia/errno.h>
22#include <pjmedia/wsola.h>
23#include <pj/assert.h>
24#include <pj/pool.h>
25#include <pj/string.h>
26
27
28static void* plc_wsola_create(pj_pool_t*, unsigned c, unsigned f);
29static void plc_wsola_save(void*, pj_int16_t*);
30static void plc_wsola_generate(void*, pj_int16_t*);
31
32/**
33 * This struct is used internally to represent a PLC backend.
34 */
35struct plc_alg
36{
37 void* (*plc_create)(pj_pool_t*, unsigned c, unsigned f);
38 void (*plc_save)(void*, pj_int16_t*);
39 void (*plc_generate)(void*, pj_int16_t*);
40};
41
42
43static struct plc_alg plc_wsola =
44{
45 &plc_wsola_create,
46 &plc_wsola_save,
47 &plc_wsola_generate
48};
49
50
51struct pjmedia_plc
52{
53 void *obj;
54 struct plc_alg *op;
55};
56
57
58/*
59 * Create PLC session. This function will select the PLC algorithm to
60 * use based on the arguments.
61 */
62PJ_DEF(pj_status_t) pjmedia_plc_create( pj_pool_t *pool,
63 unsigned clock_rate,
64 unsigned samples_per_frame,
65 unsigned options,
66 pjmedia_plc **p_plc)
67{
68 pjmedia_plc *plc;
69
70 PJ_ASSERT_RETURN(pool && clock_rate && samples_per_frame && p_plc,
71 PJ_EINVAL);
72 PJ_ASSERT_RETURN(options == 0, PJ_EINVAL);
73
74 PJ_UNUSED_ARG(options);
75
76 plc = PJ_POOL_ZALLOC_T(pool, pjmedia_plc);
77
78 plc->op = &plc_wsola;
79 plc->obj = plc->op->plc_create(pool, clock_rate, samples_per_frame);
80
81 *p_plc = plc;
82
83 return PJ_SUCCESS;
84}
85
86
87/*
88 * Save a good frame to PLC.
89 */
90PJ_DEF(pj_status_t) pjmedia_plc_save( pjmedia_plc *plc,
91 pj_int16_t *frame )
92{
93 PJ_ASSERT_RETURN(plc && frame, PJ_EINVAL);
94
95 plc->op->plc_save(plc->obj, frame);
96 return PJ_SUCCESS;
97}
98
99
100/*
101 * Generate a replacement for lost frame.
102 */
103PJ_DEF(pj_status_t) pjmedia_plc_generate( pjmedia_plc *plc,
104 pj_int16_t *frame )
105{
106 PJ_ASSERT_RETURN(plc && frame, PJ_EINVAL);
107
108 plc->op->plc_generate(plc->obj, frame);
109 return PJ_SUCCESS;
110}
111
112
113//////////////////////////////////////////////////////////////////////////////
114/*
115 * Packet loss concealment based on WSOLA
116 */
117struct wsola_plc
118{
119 pjmedia_wsola *wsola;
120 pj_bool_t prev_lost;
121};
122
123
124static void* plc_wsola_create(pj_pool_t *pool, unsigned clock_rate,
125 unsigned samples_per_frame)
126{
127 struct wsola_plc *o;
128 unsigned flag;
129 pj_status_t status;
130
131 PJ_UNUSED_ARG(clock_rate);
132
133 o = PJ_POOL_ZALLOC_T(pool, struct wsola_plc);
134 o->prev_lost = PJ_FALSE;
135
136 flag = PJMEDIA_WSOLA_NO_DISCARD;
137 if (PJMEDIA_WSOLA_PLC_NO_FADING)
138 flag |= PJMEDIA_WSOLA_NO_FADING;
139
140 status = pjmedia_wsola_create(pool, clock_rate, samples_per_frame, 1,
141 flag, &o->wsola);
142 if (status != PJ_SUCCESS)
143 return NULL;
144
145 return o;
146}
147
148static void plc_wsola_save(void *plc, pj_int16_t *frame)
149{
150 struct wsola_plc *o = (struct wsola_plc*) plc;
151
152 pjmedia_wsola_save(o->wsola, frame, o->prev_lost);
153 o->prev_lost = PJ_FALSE;
154}
155
156static void plc_wsola_generate(void *plc, pj_int16_t *frame)
157{
158 struct wsola_plc *o = (struct wsola_plc*) plc;
159
160 pjmedia_wsola_generate(o->wsola, frame);
161 o->prev_lost = PJ_TRUE;
162}
163
164