blob: 635bb0c00bbdb090b36dcb820f6507a7b0b6f586 [file] [log] [blame]
Alexandre Lision67916dd2014-01-24 13:33:04 -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#ifndef __PJMEDIA_STEREO_H__
21#define __PJMEDIA_STEREO_H__
22
23/**
24 * @file stereo.h
25 * @brief Monochannel and multichannel converter.
26 */
27
28#include <pjmedia/errno.h>
29#include <pjmedia/port.h>
30#include <pjmedia/types.h>
31#include <pj/assert.h>
32
33
34/**
35 * @defgroup PJMEDIA_STEREO Monochannel and multichannel audio frame converter
36 * @ingroup PJMEDIA_FRAME_OP
37 * @brief Mono - multi-channels audio conversion
38 * @{
39 *
40 */
41
42PJ_BEGIN_DECL
43
44
45/**
46 * Multichannel to monochannel conversion mixes samples from all channels
47 * into the monochannel.
48 */
49#define PJMEDIA_STEREO_MIX PJ_TRUE
50
51
52
53/**
54 * Multichannel to monochannel conversion, it has two operation mode specified
55 * by param options, @see pjmedia_stereo_options. This function can work safely
56 * using the same buffer (in place conversion).
57 *
58 * @param mono Output buffer to store the mono frame extracted
59 * from the multichannels frame.
60 * @param multi Input frame containing multichannels audio.
61 * @param channel_count Number of channels in the input frame.
62 * @param samples_per_frame Number of samples in the input frame.
63 * @param mix If the value is PJ_TRUE then the input channels
64 * will be mixed to produce output frame, otherwise
65 * only frame from channel_src will be copied to the
66 * output frame.
67 * @param channel_src When mixing is disabled, the mono output frame
68 * will be copied from this channel number.
69 *
70 * @return PJ_SUCCESS on success;
71 */
72PJ_INLINE(pj_status_t) pjmedia_convert_channel_nto1(pj_int16_t mono[],
73 const pj_int16_t multi[],
74 unsigned channel_count,
75 unsigned samples_per_frame,
76 pj_bool_t mix,
77 unsigned channel_src)
78{
79 unsigned i;
80
81 PJ_ASSERT_RETURN(mono && multi && channel_count && samples_per_frame &&
82 channel_src < channel_count, PJ_EINVAL);
83
84 if (mix==PJ_FALSE) {
85 for (i = channel_src; i < samples_per_frame; i += channel_count) {
86 *mono = multi[i];
87 ++mono;
88 }
89 } else {
90 unsigned j;
91 for (i = 0; i < samples_per_frame; i += channel_count) {
92 int tmp = 0;
93 for(j = 0; j < channel_count; ++j)
94 tmp += multi[i+j];
95
96 if (tmp > 32767) tmp = 32767;
97 else if (tmp < -32768) tmp = -32768;
98 *mono = (pj_int16_t) tmp;
99 ++mono;
100 }
101 }
102
103 return PJ_SUCCESS;
104}
105
106
107/**
108 * Monochannel to multichannel conversion, it will just duplicate the samples
109 * from monochannel frame to all channels in the multichannel frame.
110 * This function can work safely using the same buffer (in place conversion)
111 * as long as the buffer is big enough for the multichannel samples.
112 *
113 * @param multi Output buffer to store the multichannels frame
114 * mixed from the mono frame.
115 * @param mono The input monochannel audio frame.
116 * @param channel_count Desired number of channels in the output frame.
117 * @param samples_per_frame Number of samples in the input frame.
118 * @param options Options for conversion, currently must be zero.
119 *
120 * @return PJ_SUCCESS on success;
121 */
122PJ_INLINE(pj_status_t) pjmedia_convert_channel_1ton(pj_int16_t multi[],
123 const pj_int16_t mono[],
124 unsigned channel_count,
125 unsigned samples_per_frame,
126 unsigned options)
127{
128 const pj_int16_t *src;
129
130 PJ_ASSERT_RETURN(mono && multi && channel_count && samples_per_frame,
131 PJ_EINVAL);
132 PJ_ASSERT_RETURN(options == 0, PJ_EINVAL);
133
134 PJ_UNUSED_ARG(options);
135
136 src = mono + samples_per_frame - 1;
137 samples_per_frame *= channel_count;
138 while (samples_per_frame) {
139 unsigned i;
140 for (i=1; i<=channel_count; ++i)
141 multi[samples_per_frame-i] = *src;
142 samples_per_frame -= channel_count;
143 --src;
144 }
145
146 return PJ_SUCCESS;
147}
148
149
150/**
151 * Options for channel converter port. The #pjmedia_stereo_options is also
152 * valid for this port options.
153 */
154typedef enum pjmedia_stereo_port_options
155{
156 /**
157 * Specifies whether this port should not destroy downstream port when
158 * this port is destroyed.
159 */
160 PJMEDIA_STEREO_DONT_DESTROY_DN = 4
161} pjmedia_stereo_port_options;
162
163
164/**
165 * Create a mono-multi channel converter port. This creates a converter session,
166 * which will adjust the samples of audio frame to a different channel count
167 * when the port's get_frame() and put_frame() is called.
168 *
169 * When the port's get_frame() is called, this port will get a frame from
170 * the downstream port and convert the frame to the target channel count before
171 * returning it to the caller.
172 *
173 * When the port's put_frame() is called, this port will convert the frame
174 * to the downstream port's channel count before giving the frame to the
175 * downstream port.
176 *
177 * @param pool Pool to allocate the structure and buffers.
178 * @param dn_port The downstream port, which channel count is to
179 * be converted to the target channel count.
180 * @param channel_count This port channel count.
181 * @param options Bitmask flags from #pjmedia_stereo_port_options
182 * and also application may add PJMEDIA_STEREO_MIX
183 * to mix channels.
184 * When this flag is zero, the default behavior
185 * is to use simple N-to-1 channel converter and
186 * to destroy downstream port when this port is
187 * destroyed.
188 * @param p_port Pointer to receive the stereo port instance.
189 *
190 * @return PJ_SUCCESS on success.
191 */
192PJ_DECL(pj_status_t) pjmedia_stereo_port_create( pj_pool_t *pool,
193 pjmedia_port *dn_port,
194 unsigned channel_count,
195 unsigned options,
196 pjmedia_port **p_port );
197
198PJ_END_DECL
199
200/**
201 * @}
202 */
203
204
205#endif /* __PJMEDIA_STEREO_H__ */
206