blob: c573ed496081b80f987e277e56eaf70b1d4a8f5d [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/* $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#ifndef __PJMEDIA_RESAMPLE_H__
21#define __PJMEDIA_RESAMPLE_H__
22
23
24
25/**
26 * @file resample.h
27 * @brief Sample rate converter.
28 */
29#include <pjmedia/types.h>
30#include <pjmedia/port.h>
31
32/**
33 * @defgroup PJMEDIA_RESAMPLE Resampling Algorithm
34 * @ingroup PJMEDIA_FRAME_OP
35 * @brief Sample rate conversion algorithm
36 * @{
37 *
38 * This section describes the base resampling functions. In addition to this,
39 * application can use the @ref PJMEDIA_RESAMPLE_PORT which provides
40 * media port abstraction for the base resampling algorithm.
41 */
42
43PJ_BEGIN_DECL
44
45/*
46 * This file declares two types of API:
47 *
48 * Application can use #pjmedia_resample_create() and #pjmedia_resample_run()
49 * to convert a frame from source rate to destination rate. The inpuit frame
50 * must have a constant length.
51 *
52 * Alternatively, application can create a resampling port with
53 * #pjmedia_resample_port_create() and connect the port to other ports to
54 * change the sampling rate of the samples.
55 */
56
57
58/**
59 * Opaque resample session.
60 */
61typedef struct pjmedia_resample pjmedia_resample;
62
63/**
64 * Create a frame based resample session.
65 *
66 * @param pool Pool to allocate the structure and buffers.
67 * @param high_quality If true, then high quality conversion will be
68 * used, at the expense of more CPU and memory,
69 * because temporary buffer needs to be created.
70 * @param large_filter If true, large filter size will be used.
71 * @param channel_count Number of channels.
72 * @param rate_in Clock rate of the input samples.
73 * @param rate_out Clock rate of the output samples.
74 * @param samples_per_frame Number of samples per frame in the input.
75 * @param p_resample Pointer to receive the resample session.
76 *
77 * @return PJ_SUCCESS on success.
78 */
79PJ_DECL(pj_status_t) pjmedia_resample_create(pj_pool_t *pool,
80 pj_bool_t high_quality,
81 pj_bool_t large_filter,
82 unsigned channel_count,
83 unsigned rate_in,
84 unsigned rate_out,
85 unsigned samples_per_frame,
86 pjmedia_resample **p_resample);
87
88
89/**
90 * Use the resample session to resample a frame. The frame must have the
91 * same size and settings as the resample session, or otherwise the
92 * behavior is undefined.
93 *
94 * @param resample The resample session.
95 * @param input Buffer containing the input samples.
96 * @param output Buffer to store the output samples.
97 */
98PJ_DECL(void) pjmedia_resample_run( pjmedia_resample *resample,
99 const pj_int16_t *input,
100 pj_int16_t *output );
101
102
103/**
104 * Get the input frame size of a resample session.
105 *
106 * @param resample The resample session.
107 *
108 * @return The frame size, in number of samples.
109 */
110PJ_DECL(unsigned) pjmedia_resample_get_input_size(pjmedia_resample *resample);
111
112
113/**
114 * Destroy the resample.
115 *
116 * @param resample The resample session.
117 */
118PJ_DECL(void) pjmedia_resample_destroy(pjmedia_resample *resample);
119
120/**
121 * @}
122 */
123
124/**
125 * @defgroup PJMEDIA_RESAMPLE_PORT Resample Port
126 * @ingroup PJMEDIA_PORT
127 * @brief Audio sample rate conversion
128 * @{
129 *
130 * This section describes media port abstraction for @ref PJMEDIA_RESAMPLE.
131 */
132
133
134/**
135 * Option flags that can be specified when creating resample port.
136 */
137enum pjmedia_resample_port_options
138{
139 /**
140 * Do not use high quality resampling algorithm, but use linear
141 * algorithm instead.
142 */
143 PJMEDIA_RESAMPLE_USE_LINEAR = 1,
144
145 /**
146 * Use small filter workspace when high quality resampling is
147 * used.
148 */
149 PJMEDIA_RESAMPLE_USE_SMALL_FILTER = 2,
150
151 /**
152 * Do not destroy downstream port when resample port is destroyed.
153 */
154 PJMEDIA_RESAMPLE_DONT_DESTROY_DN = 4
155};
156
157
158
159/**
160 * Create a resample port. This creates a bidirectional resample session,
161 * which will resample frames when the port's get_frame() and put_frame()
162 * is called.
163 *
164 * When the resample port's get_frame() is called, this port will get
165 * a frame from the downstream port and resample the frame to the target
166 * clock rate before returning it to the caller.
167 *
168 * When the resample port's put_frame() is called, this port will resample
169 * the frame to the downstream port's clock rate before giving the frame
170 * to the downstream port.
171 *
172 * @param pool Pool to allocate the structure and buffers.
173 * @param dn_port The downstream port, which clock rate is to
174 * be converted to the target clock rate.
175 * @param clock_rate Target clock rate.
176 * @param options Flags from #pjmedia_resample_port_options.
177 * When this flag is zero, the default behavior
178 * is to use high quality resampling with
179 * large filter, and to destroy downstream port
180 * when resample port is destroyed.
181 * @param p_port Pointer to receive the resample port instance.
182 *
183 * @return PJ_SUCCESS on success.
184 */
185PJ_DECL(pj_status_t) pjmedia_resample_port_create( pj_pool_t *pool,
186 pjmedia_port *dn_port,
187 unsigned clock_rate,
188 unsigned options,
189 pjmedia_port **p_port );
190
191
192PJ_END_DECL
193
194/**
195 * @}
196 */
197
198
199#endif /* __PJMEDIA_RESAMPLE_H__ */
200