Add some more debugging to the Xv strides
[platform/upstream/gstreamer.git] / gst / audioresample / 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 #include <liboil/liboil.h>
31
32 #include "resample.h"
33 #include "buffer.h"
34 #include "debug.h"
35
36
37 static double
38 resample_sinc_window (double x, double halfwidth, double scale)
39 {
40   double y;
41
42   if (x == 0)
43     return 1.0;
44   if (x < -halfwidth || x > halfwidth)
45     return 0.0;
46
47   y = sin (x * M_PI * scale) / (x * M_PI * scale) * scale;
48
49   x /= halfwidth;
50   y *= (1 - x * x) * (1 - x * x);
51
52   return y;
53 }
54
55 void
56 resample_scale_ref (ResampleState * r)
57 {
58   if (r->need_reinit) {
59     RESAMPLE_DEBUG ("sample size %d", r->sample_size);
60
61     if (r->buffer)
62       free (r->buffer);
63     r->buffer_len = r->sample_size * r->filter_length;
64     r->buffer = malloc (r->buffer_len);
65     memset (r->buffer, 0, r->buffer_len);
66     r->buffer_filled = 0;
67
68     r->i_inc = r->o_rate / r->i_rate;
69     r->o_inc = r->i_rate / r->o_rate;
70     RESAMPLE_DEBUG ("i_inc %g o_inc %g", r->i_inc, r->o_inc);
71
72     r->i_start = -r->i_inc * r->filter_length;
73
74     r->need_reinit = 0;
75
76 #if 0
77     if (r->i_inc < 1.0) {
78       r->sinc_scale = r->i_inc;
79       if (r->sinc_scale == 0.5) {
80         /* strange things happen at integer multiples */
81         r->sinc_scale = 1.0;
82       }
83     } else {
84       r->sinc_scale = 1.0;
85     }
86 #else
87     r->sinc_scale = 1.0;
88 #endif
89   }
90
91   RESAMPLE_DEBUG ("asked to resample %d bytes", r->o_size);
92   RESAMPLE_DEBUG ("%d bytes in queue",
93       audioresample_buffer_queue_get_depth (r->queue));
94
95   while (r->o_size >= r->sample_size) {
96     double midpoint;
97     int i;
98     int j;
99
100     midpoint = r->i_start + (r->filter_length - 1) * 0.5 * r->i_inc;
101     RESAMPLE_DEBUG
102         ("still need to output %d bytes, %d input left, i_start %g, midpoint %f",
103         r->o_size, audioresample_buffer_queue_get_depth (r->queue), r->i_start,
104         midpoint);
105     if (midpoint > 0.5 * r->i_inc) {
106       RESAMPLE_ERROR ("inconsistent state");
107     }
108     while (midpoint < -0.5 * r->i_inc) {
109       AudioresampleBuffer *buffer;
110
111       RESAMPLE_DEBUG ("midpoint %f < %f, r->i_inc %f", midpoint,
112           -0.5 * r->i_inc, r->i_inc);
113       buffer = audioresample_buffer_queue_pull (r->queue, r->sample_size);
114       if (buffer == NULL) {
115         /* FIXME: for the first buffer, this isn't necessarily an error,
116          * since because of the filter length we'll output less buffers.
117          * deal with that so we don't print to console */
118         RESAMPLE_ERROR ("buffer_queue_pull returned NULL");
119         return;
120       }
121
122       r->i_start += r->i_inc;
123       RESAMPLE_DEBUG ("pulling (i_start = %g)", r->i_start);
124
125       midpoint += r->i_inc;
126       memmove (r->buffer, r->buffer + r->sample_size,
127           r->buffer_len - r->sample_size);
128
129       memcpy (r->buffer + r->buffer_len - r->sample_size, buffer->data,
130           r->sample_size);
131       r->buffer_filled = MIN (r->buffer_filled + r->sample_size, r->buffer_len);
132
133       audioresample_buffer_unref (buffer);
134     }
135
136     switch (r->format) {
137       case RESAMPLE_FORMAT_S16:
138         for (i = 0; i < r->n_channels; i++) {
139           double acc = 0;
140           double offset;
141           double x;
142
143           for (j = 0; j < r->filter_length; j++) {
144             offset = (r->i_start + j * r->i_inc) * r->o_inc;
145             x = *(int16_t *) (r->buffer + i * sizeof (int16_t) +
146                 j * r->sample_size);
147             acc +=
148                 resample_sinc_window (offset, r->filter_length * 0.5,
149                 r->sinc_scale) * x;
150           }
151           if (acc < -32768.0)
152             acc = -32768.0;
153           if (acc > 32767.0)
154             acc = 32767.0;
155
156           *(int16_t *) (r->o_buf + i * sizeof (int16_t)) = rint (acc);
157         }
158         break;
159       case RESAMPLE_FORMAT_S32:
160         for (i = 0; i < r->n_channels; i++) {
161           double acc = 0;
162           double offset;
163           double x;
164
165           for (j = 0; j < r->filter_length; j++) {
166             offset = (r->i_start + j * r->i_inc) * r->o_inc;
167             x = *(int32_t *) (r->buffer + i * sizeof (int32_t) +
168                 j * r->sample_size);
169             acc +=
170                 resample_sinc_window (offset, r->filter_length * 0.5,
171                 r->sinc_scale) * x;
172           }
173           if (acc < -2147483648.0)
174             acc = -2147483648.0;
175           if (acc > 2147483647.0)
176             acc = 2147483647.0;
177
178           *(int32_t *) (r->o_buf + i * sizeof (int32_t)) = rint (acc);
179         }
180         break;
181       case RESAMPLE_FORMAT_F32:
182         for (i = 0; i < r->n_channels; i++) {
183           double acc = 0;
184           double offset;
185           double x;
186
187           for (j = 0; j < r->filter_length; j++) {
188             offset = (r->i_start + j * r->i_inc) * r->o_inc;
189             x = *(float *) (r->buffer + i * sizeof (float) +
190                 j * r->sample_size);
191             acc +=
192                 resample_sinc_window (offset, r->filter_length * 0.5,
193                 r->sinc_scale) * x;
194           }
195
196           *(float *) (r->o_buf + i * sizeof (float)) = acc;
197         }
198         break;
199       case RESAMPLE_FORMAT_F64:
200         for (i = 0; i < r->n_channels; i++) {
201           double acc = 0;
202           double offset;
203           double x;
204
205           for (j = 0; j < r->filter_length; j++) {
206             offset = (r->i_start + j * r->i_inc) * r->o_inc;
207             x = *(double *) (r->buffer + i * sizeof (double) +
208                 j * r->sample_size);
209             acc +=
210                 resample_sinc_window (offset, r->filter_length * 0.5,
211                 r->sinc_scale) * x;
212           }
213
214           *(double *) (r->o_buf + i * sizeof (double)) = acc;
215         }
216         break;
217     }
218
219     r->i_start -= 1.0;
220     r->o_buf += r->sample_size;
221     r->o_size -= r->sample_size;
222   }
223 }