blob: 82211758b86cf316ed1c31a5046db75d3fa74d69 [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/resample.h>
21#include <pjmedia/errno.h>
22#include <pj/assert.h>
23#include <pj/pool.h>
24#include <pj/string.h>
25
26
27#define BYTES_PER_SAMPLE 2
28#define SIGNATURE PJMEDIA_SIG_PORT_RESAMPLE
29
30
31struct resample_port
32{
33 pjmedia_port base;
34 pjmedia_port *dn_port;
35 unsigned options;
36 pjmedia_resample *resample_get;
37 pjmedia_resample *resample_put;
38 pj_int16_t *get_buf;
39 pj_int16_t *put_buf;
40};
41
42
43
44static pj_status_t resample_put_frame(pjmedia_port *this_port,
45 pjmedia_frame *frame);
46static pj_status_t resample_get_frame(pjmedia_port *this_port,
47 pjmedia_frame *frame);
48static pj_status_t resample_destroy(pjmedia_port *this_port);
49
50
51
52PJ_DEF(pj_status_t) pjmedia_resample_port_create( pj_pool_t *pool,
53 pjmedia_port *dn_port,
54 unsigned clock_rate,
55 unsigned opt,
56 pjmedia_port **p_port )
57{
58 const pj_str_t name = pj_str("resample");
59 struct resample_port *rport;
60 pjmedia_audio_format_detail *d_afd, *r_afd;
61 pj_status_t status;
62
63 /* Validate arguments. */
64 PJ_ASSERT_RETURN(pool && dn_port && clock_rate && p_port, PJ_EINVAL);
65
66 /* Only supports 16bit samples per frame */
67 PJ_ASSERT_RETURN(PJMEDIA_PIA_BITS(&dn_port->info) == 16, PJMEDIA_ENCBITS);
68
69 d_afd = pjmedia_format_get_audio_format_detail(&dn_port->info.fmt, 1);
70
71 /* Create and initialize port. */
72 rport = PJ_POOL_ZALLOC_T(pool, struct resample_port);
73 PJ_ASSERT_RETURN(rport != NULL, PJ_ENOMEM);
74
75 pjmedia_port_info_init(&rport->base.info, &name, SIGNATURE, clock_rate,
76 d_afd->channel_count, BYTES_PER_SAMPLE * 8,
77 clock_rate * d_afd->frame_time_usec / 1000000);
78
79 rport->dn_port = dn_port;
80 rport->options = opt;
81
82 r_afd = pjmedia_format_get_audio_format_detail(&rport->base.info.fmt, 1);
83
84 /* Create buffers.
85 * We need separate buffer for get_frame() and put_frame() since
86 * both functions may run simultaneously.
87 */
88 rport->get_buf = (pj_int16_t*)
89 pj_pool_alloc(pool, PJMEDIA_PIA_AVG_FSZ(&dn_port->info));
90 PJ_ASSERT_RETURN(rport->get_buf != NULL, PJ_ENOMEM);
91
92 rport->put_buf = (pj_int16_t*)
93 pj_pool_alloc(pool, PJMEDIA_PIA_AVG_FSZ(&dn_port->info));
94 PJ_ASSERT_RETURN(rport->put_buf != NULL, PJ_ENOMEM);
95
96
97 /* Create "get_frame" resample */
98 status = pjmedia_resample_create(pool,
99 (opt&PJMEDIA_RESAMPLE_USE_LINEAR)==0,
100 (opt&PJMEDIA_RESAMPLE_USE_SMALL_FILTER)==0,
101 d_afd->channel_count,
102 d_afd->clock_rate,
103 r_afd->clock_rate,
104 PJMEDIA_PIA_SPF(&dn_port->info),
105 &rport->resample_get);
106 if (status != PJ_SUCCESS)
107 return status;
108
109 /* Create "put_frame" resample */
110 status = pjmedia_resample_create(pool,
111 (opt&PJMEDIA_RESAMPLE_USE_LINEAR)==0,
112 (opt&PJMEDIA_RESAMPLE_USE_SMALL_FILTER)==0,
113 d_afd->channel_count,
114 r_afd->clock_rate,
115 d_afd->clock_rate,
116 PJMEDIA_PIA_SPF(&rport->base.info),
117 &rport->resample_put);
118
119 /* Media port interface */
120 rport->base.get_frame = &resample_get_frame;
121 rport->base.put_frame = &resample_put_frame;
122 rport->base.on_destroy = &resample_destroy;
123
124
125 /* Done */
126 *p_port = &rport->base;
127
128 return PJ_SUCCESS;
129}
130
131
132
133static pj_status_t resample_put_frame(pjmedia_port *this_port,
134 pjmedia_frame *frame)
135{
136 struct resample_port *rport = (struct resample_port*) this_port;
137 pjmedia_frame downstream_frame;
138
139 /* Return if we don't have downstream port. */
140 if (rport->dn_port == NULL) {
141 return PJ_SUCCESS;
142 }
143
144 if (frame->type == PJMEDIA_FRAME_TYPE_AUDIO) {
145 pjmedia_resample_run( rport->resample_put,
146 (const pj_int16_t*) frame->buf,
147 rport->put_buf);
148
149 downstream_frame.buf = rport->put_buf;
150 downstream_frame.size = PJMEDIA_PIA_AVG_FSZ(&rport->dn_port->info);
151 } else {
152 downstream_frame.buf = frame->buf;
153 downstream_frame.size = frame->size;
154 }
155
156 downstream_frame.type = frame->type;
157 downstream_frame.timestamp.u64 = frame->timestamp.u64;
158
159 return pjmedia_port_put_frame( rport->dn_port, &downstream_frame );
160}
161
162
163
164static pj_status_t resample_get_frame(pjmedia_port *this_port,
165 pjmedia_frame *frame)
166{
167 struct resample_port *rport = (struct resample_port*) this_port;
168 pjmedia_frame tmp_frame;
169 pj_status_t status;
170
171 /* Return silence if we don't have downstream port */
172 if (rport->dn_port == NULL) {
173 pj_bzero(frame->buf, frame->size);
174 return PJ_SUCCESS;
175 }
176
177 tmp_frame.buf = rport->get_buf;
178 tmp_frame.size = PJMEDIA_PIA_AVG_FSZ(&rport->dn_port->info);
179 tmp_frame.timestamp.u64 = frame->timestamp.u64;
180 tmp_frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
181
182 status = pjmedia_port_get_frame( rport->dn_port, &tmp_frame);
183 if (status != PJ_SUCCESS)
184 return status;
185
186 if (tmp_frame.type != PJMEDIA_FRAME_TYPE_AUDIO) {
187 frame->type = tmp_frame.type;
188 frame->timestamp = tmp_frame.timestamp;
189 /* Copy whatever returned as long as the buffer size is enough */
190 frame->size = tmp_frame.size < PJMEDIA_PIA_AVG_FSZ(&rport->base.info) ?
191 tmp_frame.size : PJMEDIA_PIA_AVG_FSZ(&rport->base.info);
192 if (tmp_frame.size) {
193 pjmedia_copy_samples((pj_int16_t*)frame->buf,
194 (const pj_int16_t*)tmp_frame.buf,
195 (unsigned)frame->size >> 1);
196 }
197 return PJ_SUCCESS;
198 }
199
200 pjmedia_resample_run( rport->resample_get,
201 (const pj_int16_t*) tmp_frame.buf,
202 (pj_int16_t*) frame->buf);
203
204 frame->size = PJMEDIA_PIA_AVG_FSZ(&rport->base.info);
205 frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
206
207 return PJ_SUCCESS;
208}
209
210
211static pj_status_t resample_destroy(pjmedia_port *this_port)
212{
213 struct resample_port *rport = (struct resample_port*) this_port;
214
215 if ((rport->options & PJMEDIA_RESAMPLE_DONT_DESTROY_DN)==0) {
216 pjmedia_port_destroy(rport->dn_port);
217 rport->dn_port = NULL;
218 }
219
220 if (rport->resample_get) {
221 pjmedia_resample_destroy(rport->resample_get);
222 rport->resample_get = NULL;
223 }
224
225 if (rport->resample_put) {
226 pjmedia_resample_destroy(rport->resample_put);
227 rport->resample_put = NULL;
228 }
229
230 return PJ_SUCCESS;
231}
232