1
/* Copyright (C) 2002 Jean-Marc Valin
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions
8
- Redistributions of source code must retain the above copyright
9
notice, this list of conditions and the following disclaimer.
11
- Redistributions in binary form must reproduce the above copyright
12
notice, this list of conditions and the following disclaimer in the
13
documentation and/or other materials provided with the distribution.
15
- Neither the name of the Xiph.org Foundation nor the names of its
16
contributors may be used to endorse or promote products derived from
17
this software without specific prior written permission.
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
#include "speex_stereo.h"
33
#include "speex_callbacks.h"
37
/*float e_ratio_quant[4] = {1, 1.26, 1.587, 2};*/
38
static const float e_ratio_quant[4] = {.25f, .315f, .397f, .5f};
40
void speex_encode_stereo(short *data, int frame_size, SpeexBits *bits)
43
float e_left=0, e_right=0, e_tot=0;
44
float balance, e_ratio;
45
for (i=0;i<frame_size;i++)
47
e_left += ((float)data[2*i])*data[2*i];
48
e_right += ((float)data[2*i+1])*data[2*i+1];
49
data[i] = .5*(((float)data[2*i])+data[2*i+1]);
50
e_tot += ((float)data[i])*data[i];
52
balance=(e_left+1)/(e_right+1);
53
e_ratio = e_tot/(1+e_left+e_right);
56
speex_bits_pack(bits, 14, 5);
57
speex_bits_pack(bits, SPEEX_INBAND_STEREO, 4);
59
balance=4*log(balance);
63
speex_bits_pack(bits, 0, 1);
65
speex_bits_pack(bits, 1, 1);
66
balance=floor(.5+fabs(balance));
70
speex_bits_pack(bits, (int)balance, 5);
72
/*Quantize energy ratio*/
73
tmp=vq_index(&e_ratio, e_ratio_quant, 1, 4);
74
speex_bits_pack(bits, tmp, 2);
77
void speex_decode_stereo(short *data, int frame_size, SpeexStereoState *stereo)
79
float balance, e_ratio;
81
float e_tot=0, e_left, e_right, e_sum;
83
balance=stereo->balance;
84
e_ratio=stereo->e_ratio;
85
for (i=frame_size-1;i>=0;i--)
87
e_tot += ((float)data[i])*data[i];
90
e_left = e_sum*balance / (1+balance);
91
e_right = e_sum-e_left;
93
e_left = sqrt(e_left/(e_tot+.01));
94
e_right = sqrt(e_right/(e_tot+.01));
96
for (i=frame_size-1;i>=0;i--)
99
stereo->smooth_left = .98*stereo->smooth_left + .02*e_left;
100
stereo->smooth_right = .98*stereo->smooth_right + .02*e_right;
101
data[2*i] = stereo->smooth_left*ftmp;
102
data[2*i+1] = stereo->smooth_right*ftmp;
106
int speex_std_stereo_request_handler(SpeexBits *bits, void *state, void *data)
108
SpeexStereoState *stereo;
112
stereo = (SpeexStereoState*)data;
113
if (speex_bits_unpack_unsigned(bits, 1))
115
tmp = speex_bits_unpack_unsigned(bits, 5);
116
stereo->balance = exp(sign*.25*tmp);
118
tmp = speex_bits_unpack_unsigned(bits, 2);
119
stereo->e_ratio = e_ratio_quant[tmp];