Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / legacyresample / resample_ref.c
1 /* Resampling library
2  * Copyright (C) <2001> David A. Schleef <ds@schleef.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24
25 #include <string.h>
26 #include <math.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <limits.h>
30
31 #include <gst/math-compat.h>
32 #include "_stdint.h"
33
34 #include "resample.h"
35 #include "buffer.h"
36 #include "debug.h"
37
38 static double
39 resample_sinc_window (double x, double halfwidth, double scale)
40 {
41   double y;
42
43   if (x == 0)
44     return 1.0;
45   if (x < -halfwidth || x > halfwidth)
46     return 0.0;
47
48   y = sin (x * M_PI * scale) / (x * M_PI * scale) * scale;
49
50   x /= halfwidth;
51   y *= (1 - x * x) * (1 - x * x);
52
53   return y;
54 }
55
56 void
57 resample_scale_ref (ResampleState * r)
58 {
59   if (r->need_reinit) {
60     RESAMPLE_DEBUG ("sample size %d", r->sample_size);
61
62     if (r->buffer)
63       free (r->buffer);
64     r->buffer_len = r->sample_size * r->filter_length;
65     r->buffer = malloc (r->buffer_len);
66     memset (r->buffer, 0, r->buffer_len);
67     r->buffer_filled = 0;
68
69     r->i_inc = r->o_rate / r->i_rate;
70     r->o_inc = r->i_rate / r->o_rate;
71     RESAMPLE_DEBUG ("i_inc %g o_inc %g", r->i_inc, r->o_inc);
72
73     r->i_start = -r->i_inc * r->filter_length;
74
75     r->need_reinit = 0;
76
77 #if 0
78     if (r->i_inc < 1.0) {
79       r->sinc_scale = r->i_inc;
80       if (r->sinc_scale == 0.5) {
81         /* strange things happen at integer multiples */
82         r->sinc_scale = 1.0;
83       }
84     } else {
85       r->sinc_scale = 1.0;
86     }
87 #else
88     r->sinc_scale = 1.0;
89 #endif
90   }
91
92   RESAMPLE_DEBUG ("asked to resample %d bytes", r->o_size);
93   RESAMPLE_DEBUG ("%d bytes in queue",
94       audioresample_buffer_queue_get_depth (r->queue));
95
96   while (r->o_size >= r->sample_size) {
97     double midpoint;
98     int i;
99     int j;
100
101     midpoint = r->i_start + (r->filter_length - 1) * 0.5 * r->i_inc;
102     RESAMPLE_DEBUG
103         ("still need to output %d bytes, %d input left, i_start %g, midpoint %f",
104         r->o_size, audioresample_buffer_queue_get_depth (r->queue), r->i_start,
105         midpoint);
106     if (midpoint > 0.5 * r->i_inc) {
107       RESAMPLE_ERROR ("inconsistent state");
108     }
109     while (midpoint < -0.5 * r->i_inc) {
110       AudioresampleBuffer *buffer;
111
112       RESAMPLE_DEBUG ("midpoint %f < %f, r->i_inc %f", midpoint,
113           -0.5 * r->i_inc, r->i_inc);
114       buffer = audioresample_buffer_queue_pull (r->queue, r->sample_size);
115       if (buffer == NULL) {
116         /* FIXME: for the first buffer, this isn't necessarily an error,
117          * since because of the filter length we'll output less buffers.
118          * deal with that so we don't print to console */
119         RESAMPLE_ERROR ("buffer_queue_pull returned NULL");
120         return;
121       }
122
123       r->i_start += r->i_inc;
124       RESAMPLE_DEBUG ("pulling (i_start = %g)", r->i_start);
125
126       midpoint += r->i_inc;
127       memmove (r->buffer, r->buffer + r->sample_size,
128           r->buffer_len - r->sample_size);
129
130       memcpy (r->buffer + r->buffer_len - r->sample_size, buffer->data,
131           r->sample_size);
132       r->buffer_filled = MIN (r->buffer_filled + r->sample_size, r->buffer_len);
133
134       audioresample_buffer_unref (buffer);
135     }
136
137     switch (r->format) {
138       case RESAMPLE_FORMAT_S16:
139         for (i = 0; i < r->n_channels; i++) {
140           double acc = 0;
141           double offset;
142           double x;
143
144           for (j = 0; j < r->filter_length; j++) {
145             offset = (r->i_start + j * r->i_inc) * r->o_inc;
146             x = *(int16_t *) (r->buffer + i * sizeof (int16_t) +
147                 j * r->sample_size);
148             acc +=
149                 resample_sinc_window (offset, r->filter_length * 0.5,
150                 r->sinc_scale) * x;
151           }
152           if (acc < -32768.0)
153             acc = -32768.0;
154           if (acc > 32767.0)
155             acc = 32767.0;
156
157           *(int16_t *) (r->o_buf + i * sizeof (int16_t)) = rint (acc);
158         }
159         break;
160       case RESAMPLE_FORMAT_S32:
161         for (i = 0; i < r->n_channels; i++) {
162           double acc = 0;
163           double offset;
164           double x;
165
166           for (j = 0; j < r->filter_length; j++) {
167             offset = (r->i_start + j * r->i_inc) * r->o_inc;
168             x = *(int32_t *) (r->buffer + i * sizeof (int32_t) +
169                 j * r->sample_size);
170             acc +=
171                 resample_sinc_window (offset, r->filter_length * 0.5,
172                 r->sinc_scale) * x;
173           }
174           if (acc < -2147483648.0)
175             acc = -2147483648.0;
176           if (acc > 2147483647.0)
177             acc = 2147483647.0;
178
179           *(int32_t *) (r->o_buf + i * sizeof (int32_t)) = rint (acc);
180         }
181         break;
182       case RESAMPLE_FORMAT_F32:
183         for (i = 0; i < r->n_channels; i++) {
184           double acc = 0;
185           double offset;
186           double x;
187
188           for (j = 0; j < r->filter_length; j++) {
189             offset = (r->i_start + j * r->i_inc) * r->o_inc;
190             x = *(float *) (r->buffer + i * sizeof (float) +
191                 j * r->sample_size);
192             acc +=
193                 resample_sinc_window (offset, r->filter_length * 0.5,
194                 r->sinc_scale) * x;
195           }
196
197           *(float *) (r->o_buf + i * sizeof (float)) = acc;
198         }
199         break;
200       case RESAMPLE_FORMAT_F64:
201         for (i = 0; i < r->n_channels; i++) {
202           double acc = 0;
203           double offset;
204           double x;
205
206           for (j = 0; j < r->filter_length; j++) {
207             offset = (r->i_start + j * r->i_inc) * r->o_inc;
208             x = *(double *) (r->buffer + i * sizeof (double) +
209                 j * r->sample_size);
210             acc +=
211                 resample_sinc_window (offset, r->filter_length * 0.5,
212                 r->sinc_scale) * x;
213           }
214
215           *(double *) (r->o_buf + i * sizeof (double)) = acc;
216         }
217         break;
218     }
219
220     r->i_start -= 1.0;
221     r->o_buf += r->sample_size;
222     r->o_size -= r->sample_size;
223   }
224 }