~ubuntu-branches/ubuntu/vivid/linphone/vivid

« back to all changes in this revision

Viewing changes to speex/libspeex/stereo.c

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2006-11-15 10:34:50 UTC
  • mfrom: (1.2.1 upstream) (2.1.8 feisty)
  • Revision ID: james.westby@ubuntu.com-20061115103450-qgafwcks2lkhctlj
* New upstream release.
* Enable video support.
* Fix mismatched #endif in mscommon.h, closes: #398307.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2002 Jean-Marc Valin 
2
 
   File: stereo.c
3
 
 
4
 
   Redistribution and use in source and binary forms, with or without
5
 
   modification, are permitted provided that the following conditions
6
 
   are met:
7
 
   
8
 
   - Redistributions of source code must retain the above copyright
9
 
   notice, this list of conditions and the following disclaimer.
10
 
   
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.
14
 
   
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.
18
 
   
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.
30
 
*/
31
 
 
32
 
#include "speex_stereo.h"
33
 
#include "speex_callbacks.h"
34
 
#include "vq.h"
35
 
#include <math.h>
36
 
 
37
 
/*float e_ratio_quant[4] = {1, 1.26, 1.587, 2};*/
38
 
static const float e_ratio_quant[4] = {.25f, .315f, .397f, .5f};
39
 
 
40
 
void speex_encode_stereo(short *data, int frame_size, SpeexBits *bits)
41
 
{
42
 
   int i, tmp;
43
 
   float e_left=0, e_right=0, e_tot=0;
44
 
   float balance, e_ratio;
45
 
   for (i=0;i<frame_size;i++)
46
 
   {
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];
51
 
   }
52
 
   balance=(e_left+1)/(e_right+1);
53
 
   e_ratio = e_tot/(1+e_left+e_right);
54
 
 
55
 
   /*Quantization*/
56
 
   speex_bits_pack(bits, 14, 5);
57
 
   speex_bits_pack(bits, SPEEX_INBAND_STEREO, 4);
58
 
   
59
 
   balance=4*log(balance);
60
 
 
61
 
   /*Pack sign*/
62
 
   if (balance>0)
63
 
      speex_bits_pack(bits, 0, 1);
64
 
   else
65
 
      speex_bits_pack(bits, 1, 1);
66
 
   balance=floor(.5+fabs(balance));
67
 
   if (balance>30)
68
 
      balance=31;
69
 
   
70
 
   speex_bits_pack(bits, (int)balance, 5);
71
 
   
72
 
   /*Quantize energy ratio*/
73
 
   tmp=vq_index(&e_ratio, e_ratio_quant, 1, 4);
74
 
   speex_bits_pack(bits, tmp, 2);
75
 
}
76
 
 
77
 
void speex_decode_stereo(short *data, int frame_size, SpeexStereoState *stereo)
78
 
{
79
 
   float balance, e_ratio;
80
 
   int i;
81
 
   float e_tot=0, e_left, e_right, e_sum;
82
 
 
83
 
   balance=stereo->balance;
84
 
   e_ratio=stereo->e_ratio;
85
 
   for (i=frame_size-1;i>=0;i--)
86
 
   {
87
 
      e_tot += ((float)data[i])*data[i];
88
 
   }
89
 
   e_sum=e_tot/e_ratio;
90
 
   e_left  = e_sum*balance / (1+balance);
91
 
   e_right = e_sum-e_left;
92
 
 
93
 
   e_left  = sqrt(e_left/(e_tot+.01));
94
 
   e_right = sqrt(e_right/(e_tot+.01));
95
 
 
96
 
   for (i=frame_size-1;i>=0;i--)
97
 
   {
98
 
      float ftmp=data[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;
103
 
   }
104
 
}
105
 
 
106
 
int speex_std_stereo_request_handler(SpeexBits *bits, void *state, void *data)
107
 
{
108
 
   SpeexStereoState *stereo;
109
 
   float sign=1;
110
 
   int tmp;
111
 
 
112
 
   stereo = (SpeexStereoState*)data;
113
 
   if (speex_bits_unpack_unsigned(bits, 1))
114
 
      sign=-1;
115
 
   tmp = speex_bits_unpack_unsigned(bits, 5);
116
 
   stereo->balance = exp(sign*.25*tmp);
117
 
 
118
 
   tmp = speex_bits_unpack_unsigned(bits, 2);
119
 
   stereo->e_ratio = e_ratio_quant[tmp];
120
 
 
121
 
   return 0;
122
 
}