source: contrib/API/lib/resample.c

Last change on this file was 541, checked in by David Azarewicz, 15 years ago

Initial import

File size: 6.3 KB
Line 
1/*
2 * Sample rate convertion for both audio and video.
3 *
4 * This file is part of uniaud.dll.
5 *
6 * Copyright (c) 2010 Mensys BV
7 * Copyright (c) 2007 Vlad Stelmahovsky aka Vladest
8 * Copyright (c) 2000 Fabrice Bellard.
9 *
10 * This library is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation, either version 3 of
13 * the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License and the GNU General Public License along with this library.
22 * If not, see <http://www.gnu.org/licenses/>.
23 */
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
27#include "resample.h"
28
29/* n1: number of samples */
30static void stereo_to_mono(short *output, short *input, int n1)
31{
32 short *p, *q;
33 int n = n1;
34
35 p = input;
36 q = output;
37 while (n >= 4) {
38 q[0] = (p[0] + p[1]) >> 1;
39 q[1] = (p[2] + p[3]) >> 1;
40 q[2] = (p[4] + p[5]) >> 1;
41 q[3] = (p[6] + p[7]) >> 1;
42 q += 4;
43 p += 8;
44 n -= 4;
45 }
46 while (n > 0) {
47 q[0] = (p[0] + p[1]) >> 1;
48 q++;
49 p += 2;
50 n--;
51 }
52}
53
54/* n1: number of samples */
55static void mono_to_stereo(short *output, short *input, int n1)
56{
57 short *p, *q;
58 int n = n1;
59 int v;
60
61 p = input;
62 q = output;
63 while (n >= 4) {
64 v = p[0]; q[0] = v; q[1] = v;
65 v = p[1]; q[2] = v; q[3] = v;
66 v = p[2]; q[4] = v; q[5] = v;
67 v = p[3]; q[6] = v; q[7] = v;
68 q += 8;
69 p += 4;
70 n -= 4;
71 }
72 while (n > 0) {
73 v = p[0]; q[0] = v; q[1] = v;
74 q += 2;
75 p += 1;
76 n--;
77 }
78}
79
80/* XXX: should use more abstract 'N' channels system */
81static void stereo_split(short *output1, short *output2, short *input, int n)
82{
83 int i;
84
85 for(i=0;i<n;i++) {
86 *output1++ = *input++;
87 *output2++ = *input++;
88 }
89}
90
91static void stereo_mux(short *output, short *input1, short *input2, int n)
92{
93 int i;
94
95 for(i=0;i<n;i++) {
96 *output++ = *input1++;
97 *output++ = *input2++;
98 }
99}
100
101static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
102{
103 int i;
104 short l,r;
105
106 for(i=0;i<n;i++) {
107 l=*input1++;
108 r=*input2++;
109 *output++ = l; /* left */
110 *output++ = (l/2)+(r/2); /* center */
111 *output++ = r; /* right */
112 *output++ = 0; /* left surround */
113 *output++ = 0; /* right surroud */
114 *output++ = 0; /* low freq */
115 }
116}
117
118struct ReSampleContext *audio_resample_init(int output_channels, int input_channels,
119 int output_rate, int input_rate)
120{
121 struct ReSampleContext *s = NULL;
122
123 if ( input_channels > 2) {
124 printf("Resampling with input channels greater than 2 unsupported.");
125 return NULL;
126 }
127
128 s = malloc(sizeof(struct ReSampleContext));
129 if (!s) {
130 printf("Can't allocate memory for resample context.");
131 return NULL;
132 }
133
134 memset(s,0, sizeof(struct ReSampleContext));
135
136 s->ratio = (float)output_rate / (float)input_rate;
137
138 s->input_channels = input_channels;
139 s->output_channels = output_channels;
140
141 s->filter_channels = s->input_channels;
142 if (s->output_channels < s->filter_channels)
143 s->filter_channels = s->output_channels;
144
145/*
146 * ac3 output is the only case where filter_channels could be greater than 2.
147 * input channels can't be greater than 2, so resample the 2 channels and then
148 * expand to 6 channels after the resampling.
149 */
150 if(s->filter_channels>2)
151 s->filter_channels = 2;
152
153 s->resample_context= (AVResampleContext *)av_resample_init(output_rate, input_rate, 16, 10, 0, 1.0);
154
155 return s;
156}
157
158/* resample audio. 'nb_samples' is the number of input samples */
159/* XXX: optimize it ! */
160int audio_resample(struct ReSampleContext *s, short *output, short *input, int nb_samples)
161{
162 int i, nb_samples1;
163 short *bufin[2];
164 short *bufout[2];
165 short *buftmp2[2], *buftmp3[2];
166 int lenout;
167
168 if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
169 /* nothing to do */
170 memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
171 return nb_samples;
172 }
173 /* XXX: move those malloc to resample init code */
174 for(i=0; i<s->filter_channels; i++){
175 bufin[i]= (short*) malloc( (nb_samples + s->temp_len) * sizeof(short) );
176 memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
177 buftmp2[i] = bufin[i] + s->temp_len;
178 }
179
180 /* make some zoom to avoid round pb */
181 lenout= (int)(nb_samples * s->ratio) + 16;
182 bufout[0]= (short*) malloc( lenout * sizeof(short) );
183 bufout[1]= (short*) malloc( lenout * sizeof(short) );
184
185 if (s->input_channels == 2 &&
186 s->output_channels == 1) {
187 buftmp3[0] = output;
188 stereo_to_mono(buftmp2[0], input, nb_samples);
189 } else if (s->output_channels >= 2 && s->input_channels == 1) {
190 buftmp3[0] = bufout[0];
191 memcpy(buftmp2[0], input, nb_samples*sizeof(short));
192 } else if (s->output_channels >= 2) {
193 buftmp3[0] = bufout[0];
194 buftmp3[1] = bufout[1];
195 stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);
196 } else {
197 buftmp3[0] = output;
198 memcpy(buftmp2[0], input, nb_samples*sizeof(short));
199 }
200
201 nb_samples += s->temp_len;
202
203 /* resample each channel */
204 nb_samples1 = 0; /* avoid warning */
205 for(i=0;i<s->filter_channels;i++) {
206 int consumed;
207 int is_last= i+1 == s->filter_channels;
208
209 nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i], &consumed, nb_samples, lenout, is_last);
210 s->temp_len= nb_samples - consumed;
211 s->temp[i]= realloc(s->temp[i], s->temp_len*sizeof(short));
212 memcpy(s->temp[i], bufin[i] + consumed, s->temp_len*sizeof(short));
213 }
214
215 if (s->output_channels == 2 && s->input_channels == 1) {
216 mono_to_stereo(output, buftmp3[0], nb_samples1);
217 } else if (s->output_channels == 2) {
218 stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
219 } else if (s->output_channels == 6) {
220 ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
221 }
222
223 for(i=0; i<s->filter_channels; i++)
224 free(bufin[i]);
225
226 free(bufout[0]);
227 free(bufout[1]);
228
229 return nb_samples1;
230}
231
232void audio_resample_close(struct ReSampleContext *s)
233{
234 av_resample_close(s->resample_context);
235 free(s->temp[0]);
236 free(s->temp[1]);
237 free(s);
238}
Note: See TracBrowser for help on using the repository browser.