summaryrefslogtreecommitdiff
path: root/gst/siren/rmlt.c
blob: 229b9106875c91217c638b15fccfa88ee0439c51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Siren Encoder/Decoder library
 *
 *   @author: Youness Alaoui <kakaroto@kakaroto.homelinux.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


#include "siren7.h"


static int rmlt_initialized = 0;
static float rmlt_window_640[640];
static float rmlt_window_320[320];

#define PI_2     1.57079632679489661923

void siren_rmlt_init() {
  int i = 0;
  float angle;

  for (i = 0; i < 640; i++) {
    angle = (float) (((i + 0.5) * PI_2) / 640);
    rmlt_window_640[i] = (float) sin(angle);
  }
  for (i = 0; i < 320; i++) {
    angle = (float) (((i + 0.5) * PI_2) / 320);
    rmlt_window_320[i] = (float) sin(angle);
  }

  rmlt_initialized = 1;
}

int siren_rmlt_encode_samples(float *samples, float *old_samples, int dct_length, float *rmlt_coefs) {
  int half_dct_length = dct_length / 2;
  float *old_ptr = old_samples + half_dct_length;
  float *coef_high = rmlt_coefs + half_dct_length;
  float *coef_low = rmlt_coefs + half_dct_length;
  float *samples_low = samples;
  float *samples_high = samples + dct_length;
  float *window_low = NULL;
  float *window_high = NULL;
  int i = 0;

  if (rmlt_initialized == 0)
    siren_rmlt_init();

  if (dct_length == 320)
    window_low = rmlt_window_320;
  else if (dct_length == 640)
    window_low = rmlt_window_640;
  else
    return 4;

  window_high = window_low + dct_length;


  for (i = 0; i < half_dct_length; i++) {
    *--coef_low = *--old_ptr;
    *coef_high++ = (*samples_low * *--window_high) - (*--samples_high * *window_low);
    *old_ptr = (*samples_high * *window_high) + (*samples_low++ * *window_low++);
  }
  siren_dct4(rmlt_coefs, rmlt_coefs, dct_length);

  return 0;
}



int siren_rmlt_decode_samples(float *coefs, float *old_coefs, int dct_length, float *samples)  {
  int half_dct_length = dct_length / 2;
  float *old_low = old_coefs;
  float *old_high = old_coefs + half_dct_length;
  float *samples_low = samples ;
  float *samples_high = samples + dct_length;
  float *samples_middle_low = samples + half_dct_length;
  float *samples_middle_high = samples + half_dct_length;
  float *window_low = NULL;
  float *window_high = NULL;
  float *window_middle_low = NULL;
  float *window_middle_high = NULL;
  float sample_low_val;
  float sample_high_val;
  float sample_middle_low_val;
  float sample_middle_high_val;
  int i = 0;

  if (rmlt_initialized == 0)
    siren_rmlt_init();

  if (dct_length == 320)
    window_low = rmlt_window_320;
  else if (dct_length == 640)
    window_low = rmlt_window_640;
  else
    return 4;


  window_high = window_low + dct_length;
  window_middle_low = window_low + half_dct_length;
  window_middle_high = window_low + half_dct_length;

  siren_dct4(coefs, samples, dct_length);

  for (i = 0; i < half_dct_length; i+=2) {
    sample_low_val = *samples_low;
    sample_high_val = *--samples_high;
    sample_middle_low_val = *--samples_middle_low;
    sample_middle_high_val = *samples_middle_high;
    *samples_low++ = (*old_low * *--window_high) + (sample_middle_low_val * *window_low);
    *samples_high = (sample_middle_low_val * *window_high) - (*old_low * *window_low++);
    *samples_middle_high++ = (sample_low_val * *window_middle_high) - (*--old_high * *--window_middle_low);
    *samples_middle_low = (*old_high * *window_middle_high++) + (sample_low_val * *window_middle_low);
    *old_low++ = sample_middle_high_val;
    *old_high = sample_high_val;
  }

  return 0;
}