229b9106875c91217c638b15fccfa88ee0439c51
[platform/upstream/gstreamer.git] / gst / siren / rmlt.c
1 /*
2  * Siren Encoder/Decoder library
3  *
4  *   @author: Youness Alaoui <kakaroto@kakaroto.homelinux.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22
23 #include "siren7.h"
24
25
26 static int rmlt_initialized = 0;
27 static float rmlt_window_640[640];
28 static float rmlt_window_320[320];
29
30 #define PI_2     1.57079632679489661923
31
32 void siren_rmlt_init() {
33   int i = 0;
34   float angle;
35
36   for (i = 0; i < 640; i++) {
37     angle = (float) (((i + 0.5) * PI_2) / 640);
38     rmlt_window_640[i] = (float) sin(angle);
39   }
40   for (i = 0; i < 320; i++) {
41     angle = (float) (((i + 0.5) * PI_2) / 320);
42     rmlt_window_320[i] = (float) sin(angle);
43   }
44
45   rmlt_initialized = 1;
46 }
47
48 int siren_rmlt_encode_samples(float *samples, float *old_samples, int dct_length, float *rmlt_coefs) {
49   int half_dct_length = dct_length / 2;
50   float *old_ptr = old_samples + half_dct_length;
51   float *coef_high = rmlt_coefs + half_dct_length;
52   float *coef_low = rmlt_coefs + half_dct_length;
53   float *samples_low = samples;
54   float *samples_high = samples + dct_length;
55   float *window_low = NULL;
56   float *window_high = NULL;
57   int i = 0;
58
59   if (rmlt_initialized == 0)
60     siren_rmlt_init();
61
62   if (dct_length == 320)
63     window_low = rmlt_window_320;
64   else if (dct_length == 640)
65     window_low = rmlt_window_640;
66   else
67     return 4;
68
69   window_high = window_low + dct_length;
70
71
72   for (i = 0; i < half_dct_length; i++) {
73     *--coef_low = *--old_ptr;
74     *coef_high++ = (*samples_low * *--window_high) - (*--samples_high * *window_low);
75     *old_ptr = (*samples_high * *window_high) + (*samples_low++ * *window_low++);
76   }
77   siren_dct4(rmlt_coefs, rmlt_coefs, dct_length);
78
79   return 0;
80 }
81
82
83
84 int siren_rmlt_decode_samples(float *coefs, float *old_coefs, int dct_length, float *samples)  {
85   int half_dct_length = dct_length / 2;
86   float *old_low = old_coefs;
87   float *old_high = old_coefs + half_dct_length;
88   float *samples_low = samples ;
89   float *samples_high = samples + dct_length;
90   float *samples_middle_low = samples + half_dct_length;
91   float *samples_middle_high = samples + half_dct_length;
92   float *window_low = NULL;
93   float *window_high = NULL;
94   float *window_middle_low = NULL;
95   float *window_middle_high = NULL;
96   float sample_low_val;
97   float sample_high_val;
98   float sample_middle_low_val;
99   float sample_middle_high_val;
100   int i = 0;
101
102   if (rmlt_initialized == 0)
103     siren_rmlt_init();
104
105   if (dct_length == 320)
106     window_low = rmlt_window_320;
107   else if (dct_length == 640)
108     window_low = rmlt_window_640;
109   else
110     return 4;
111
112
113   window_high = window_low + dct_length;
114   window_middle_low = window_low + half_dct_length;
115   window_middle_high = window_low + half_dct_length;
116
117   siren_dct4(coefs, samples, dct_length);
118
119   for (i = 0; i < half_dct_length; i+=2) {
120     sample_low_val = *samples_low;
121     sample_high_val = *--samples_high;
122     sample_middle_low_val = *--samples_middle_low;
123     sample_middle_high_val = *samples_middle_high;
124     *samples_low++ = (*old_low * *--window_high) + (sample_middle_low_val * *window_low);
125     *samples_high = (sample_middle_low_val * *window_high) - (*old_low * *window_low++);
126     *samples_middle_high++ = (sample_low_val * *window_middle_high) - (*--old_high * *--window_middle_low);
127     *samples_middle_low = (*old_high * *window_middle_high++) + (sample_low_val * *window_middle_low);
128     *old_low++ = sample_middle_high_val;
129     *old_high = sample_high_val;
130   }
131
132   return 0;
133 }