1
/* $Id: resample_port.c 3664 2011-07-19 03:42:28Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#include <pjmedia/resample.h>
21
#include <pjmedia/errno.h>
22
#include <pj/assert.h>
24
#include <pj/string.h>
27
#define BYTES_PER_SAMPLE 2
28
#define SIGNATURE PJMEDIA_SIG_PORT_RESAMPLE
34
pjmedia_port *dn_port;
36
pjmedia_resample *resample_get;
37
pjmedia_resample *resample_put;
44
static pj_status_t resample_put_frame(pjmedia_port *this_port,
45
pjmedia_frame *frame);
46
static pj_status_t resample_get_frame(pjmedia_port *this_port,
47
pjmedia_frame *frame);
48
static pj_status_t resample_destroy(pjmedia_port *this_port);
52
PJ_DEF(pj_status_t) pjmedia_resample_port_create( pj_pool_t *pool,
53
pjmedia_port *dn_port,
56
pjmedia_port **p_port )
58
const pj_str_t name = pj_str("resample");
59
struct resample_port *rport;
60
pjmedia_audio_format_detail *d_afd, *r_afd;
63
/* Validate arguments. */
64
PJ_ASSERT_RETURN(pool && dn_port && clock_rate && p_port, PJ_EINVAL);
66
/* Only supports 16bit samples per frame */
67
PJ_ASSERT_RETURN(PJMEDIA_PIA_BITS(&dn_port->info) == 16, PJMEDIA_ENCBITS);
69
d_afd = pjmedia_format_get_audio_format_detail(&dn_port->info.fmt, 1);
71
/* Create and initialize port. */
72
rport = PJ_POOL_ZALLOC_T(pool, struct resample_port);
73
PJ_ASSERT_RETURN(rport != NULL, PJ_ENOMEM);
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);
79
rport->dn_port = dn_port;
82
r_afd = pjmedia_format_get_audio_format_detail(&rport->base.info.fmt, 1);
85
* We need separate buffer for get_frame() and put_frame() since
86
* both functions may run simultaneously.
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);
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);
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,
104
PJMEDIA_PIA_SPF(&dn_port->info),
105
&rport->resample_get);
106
if (status != PJ_SUCCESS)
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,
116
PJMEDIA_PIA_SPF(&rport->base.info),
117
&rport->resample_put);
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;
126
*p_port = &rport->base;
133
static pj_status_t resample_put_frame(pjmedia_port *this_port,
134
pjmedia_frame *frame)
136
struct resample_port *rport = (struct resample_port*) this_port;
137
pjmedia_frame downstream_frame;
139
/* Return if we don't have downstream port. */
140
if (rport->dn_port == NULL) {
144
if (frame->type == PJMEDIA_FRAME_TYPE_AUDIO) {
145
pjmedia_resample_run( rport->resample_put,
146
(const pj_int16_t*) frame->buf,
149
downstream_frame.buf = rport->put_buf;
150
downstream_frame.size = PJMEDIA_PIA_AVG_FSZ(&rport->dn_port->info);
152
downstream_frame.buf = frame->buf;
153
downstream_frame.size = frame->size;
156
downstream_frame.type = frame->type;
157
downstream_frame.timestamp.u64 = frame->timestamp.u64;
159
return pjmedia_port_put_frame( rport->dn_port, &downstream_frame );
164
static pj_status_t resample_get_frame(pjmedia_port *this_port,
165
pjmedia_frame *frame)
167
struct resample_port *rport = (struct resample_port*) this_port;
168
pjmedia_frame tmp_frame;
171
/* Return silence if we don't have downstream port */
172
if (rport->dn_port == NULL) {
173
pj_bzero(frame->buf, frame->size);
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;
182
status = pjmedia_port_get_frame( rport->dn_port, &tmp_frame);
183
if (status != PJ_SUCCESS)
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,
200
pjmedia_resample_run( rport->resample_get,
201
(const pj_int16_t*) tmp_frame.buf,
202
(pj_int16_t*) frame->buf);
204
frame->size = PJMEDIA_PIA_AVG_FSZ(&rport->base.info);
205
frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
211
static pj_status_t resample_destroy(pjmedia_port *this_port)
213
struct resample_port *rport = (struct resample_port*) this_port;
215
if ((rport->options & PJMEDIA_RESAMPLE_DONT_DESTROY_DN)==0) {
216
pjmedia_port_destroy(rport->dn_port);
217
rport->dn_port = NULL;
220
if (rport->resample_get) {
221
pjmedia_resample_destroy(rport->resample_get);
222
rport->resample_get = NULL;
225
if (rport->resample_put) {
226
pjmedia_resample_destroy(rport->resample_put);
227
rport->resample_put = NULL;