~ubuntu-branches/ubuntu/raring/sflphone/raring

« back to all changes in this revision

Viewing changes to sflphone-common/libs/pjproject/pjmedia/include/pjmedia/stereo.h

  • Committer: Package Import Robot
  • Author(s): Francois Marier
  • Date: 2011-11-25 13:24:12 UTC
  • mfrom: (4.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20111125132412-dc4qvhyosk74cd42
Tags: 1.0.1-4
Don't assume that arch:all packages will get built (closes: #649726)

Show diffs side-by-side

added added

removed removed

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