annotate resample.c @ 1897:4e8ed93524f6 libavcodec

h264 loop filter for progressive I&P frames by (Laurent Aimar <fenrir at via dot ecp dot fr>)
author michael
date Fri, 19 Mar 2004 21:21:17 +0000
parents 932d306bf1dc
children 3dc9bbe1b152
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
2 * Sample rate convertion for both audio and video
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
3 * Copyright (c) 2000 Fabrice Bellard.
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 *
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
5 * This library is free software; you can redistribute it and/or
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
7 * License as published by the Free Software Foundation; either
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
8 * version 2 of the License, or (at your option) any later version.
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 *
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
10 * This library is distributed in the hope that it will be useful,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
13 * Lesser General Public License for more details.
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 *
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
16 * License along with this library; if not, write to the Free Software
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
18 */
1106
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
19
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
20 /**
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
21 * @file resample.c
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
22 * Sample rate convertion for both audio and video.
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
23 */
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
24
64
5aa6292a1660 win32 fixes
glantau
parents: 0
diff changeset
25 #include "avcodec.h"
1125
0980ae063f4e restoring OS/2 compatibility patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents: 1106
diff changeset
26
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
27 typedef struct {
986e461dc072 Initial revision
glantau
parents:
diff changeset
28 /* fractional resampling */
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1057
diff changeset
29 uint32_t incr; /* fractional increment */
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1057
diff changeset
30 uint32_t frac;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
31 int last_sample;
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 /* integer down sample */
986e461dc072 Initial revision
glantau
parents:
diff changeset
33 int iratio; /* integer divison ratio */
986e461dc072 Initial revision
glantau
parents:
diff changeset
34 int icount, isum;
986e461dc072 Initial revision
glantau
parents:
diff changeset
35 int inv;
986e461dc072 Initial revision
glantau
parents:
diff changeset
36 } ReSampleChannelContext;
986e461dc072 Initial revision
glantau
parents:
diff changeset
37
986e461dc072 Initial revision
glantau
parents:
diff changeset
38 struct ReSampleContext {
986e461dc072 Initial revision
glantau
parents:
diff changeset
39 ReSampleChannelContext channel_ctx[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
40 float ratio;
986e461dc072 Initial revision
glantau
parents:
diff changeset
41 /* channel convert */
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 int input_channels, output_channels, filter_channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
43 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
44
986e461dc072 Initial revision
glantau
parents:
diff changeset
45
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 #define FRAC_BITS 16
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 #define FRAC (1 << FRAC_BITS)
986e461dc072 Initial revision
glantau
parents:
diff changeset
48
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 static void init_mono_resample(ReSampleChannelContext *s, float ratio)
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 ratio = 1.0 / ratio;
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 739
diff changeset
52 s->iratio = (int)floorf(ratio);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 if (s->iratio == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 s->iratio = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 s->incr = (int)((ratio / s->iratio) * FRAC);
373
3007abcbc510 * Fix a problem with the first sample when down sampling.
philipjsg
parents: 64
diff changeset
56 s->frac = FRAC;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 s->last_sample = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
58 s->icount = s->iratio;
986e461dc072 Initial revision
glantau
parents:
diff changeset
59 s->isum = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 s->inv = (FRAC / s->iratio);
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
62
986e461dc072 Initial revision
glantau
parents:
diff changeset
63 /* fractional audio resampling */
986e461dc072 Initial revision
glantau
parents:
diff changeset
64 static int fractional_resample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
986e461dc072 Initial revision
glantau
parents:
diff changeset
65 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 unsigned int frac, incr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 int l0, l1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 short *q, *p, *pend;
986e461dc072 Initial revision
glantau
parents:
diff changeset
69
986e461dc072 Initial revision
glantau
parents:
diff changeset
70 l0 = s->last_sample;
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 incr = s->incr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 frac = s->frac;
986e461dc072 Initial revision
glantau
parents:
diff changeset
73
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 p = input;
986e461dc072 Initial revision
glantau
parents:
diff changeset
75 pend = input + nb_samples;
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 q = output;
986e461dc072 Initial revision
glantau
parents:
diff changeset
77
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 l1 = *p++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
79 for(;;) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
80 /* interpolate */
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 *q++ = (l0 * (FRAC - frac) + l1 * frac) >> FRAC_BITS;
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 frac = frac + s->incr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 while (frac >= FRAC) {
739
f40723ee806d resample fix by Petros Tsantoulis
bellard
parents: 429
diff changeset
84 frac -= FRAC;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 if (p >= pend)
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 goto the_end;
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 l0 = l1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 l1 = *p++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 the_end:
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 s->last_sample = l1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 s->frac = frac;
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 return q - output;
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
96
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 static int integer_downsample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 short *q, *p, *pend;
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 int c, sum;
986e461dc072 Initial revision
glantau
parents:
diff changeset
101
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 p = input;
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 pend = input + nb_samples;
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 q = output;
986e461dc072 Initial revision
glantau
parents:
diff changeset
105
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 c = s->icount;
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 sum = s->isum;
986e461dc072 Initial revision
glantau
parents:
diff changeset
108
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 for(;;) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 sum += *p++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 if (--c == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
112 *q++ = (sum * s->inv) >> FRAC_BITS;
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 c = s->iratio;
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 sum = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 if (p >= pend)
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 s->isum = sum;
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 s->icount = c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 return q - output;
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
123
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 /* n1: number of samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 static void stereo_to_mono(short *output, short *input, int n1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 short *p, *q;
986e461dc072 Initial revision
glantau
parents:
diff changeset
128 int n = n1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
129
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 p = input;
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 q = output;
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 while (n >= 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 q[0] = (p[0] + p[1]) >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 q[1] = (p[2] + p[3]) >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
135 q[2] = (p[4] + p[5]) >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 q[3] = (p[6] + p[7]) >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 q += 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 p += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 n -= 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 while (n > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 q[0] = (p[0] + p[1]) >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 q++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 p += 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 n--;
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
148
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 /* n1: number of samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 static void mono_to_stereo(short *output, short *input, int n1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 short *p, *q;
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 int n = n1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 int v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
155
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 p = input;
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 q = output;
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 while (n >= 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 v = p[0]; q[0] = v; q[1] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 v = p[1]; q[2] = v; q[3] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 v = p[2]; q[4] = v; q[5] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 v = p[3]; q[6] = v; q[7] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 q += 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
164 p += 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 n -= 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 while (n > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
168 v = p[0]; q[0] = v; q[1] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 q += 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 p += 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 n--;
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
174
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 /* XXX: should use more abstract 'N' channels system */
986e461dc072 Initial revision
glantau
parents:
diff changeset
176 static void stereo_split(short *output1, short *output2, short *input, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
177 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
178 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
179
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 for(i=0;i<n;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 *output1++ = *input++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
182 *output2++ = *input++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
184 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
185
986e461dc072 Initial revision
glantau
parents:
diff changeset
186 static void stereo_mux(short *output, short *input1, short *input2, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
187 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
188 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
189
986e461dc072 Initial revision
glantau
parents:
diff changeset
190 for(i=0;i<n;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
191 *output++ = *input1++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
192 *output++ = *input2++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
195
1408
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
196 static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
197 {
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
198 int i;
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
199 short l,r;
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
200
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
201 for(i=0;i<n;i++) {
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
202 l=*input1++;
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
203 r=*input2++;
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
204 *output++ = l; /* left */
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
205 *output++ = (l/2)+(r/2); /* center */
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
206 *output++ = r; /* right */
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
207 *output++ = 0; /* left surround */
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
208 *output++ = 0; /* right surroud */
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
209 *output++ = 0; /* low freq */
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
210 }
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
211 }
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
212
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
213 static int mono_resample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 {
64
5aa6292a1660 win32 fixes
glantau
parents: 0
diff changeset
215 short *buf1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
216 short *buftmp;
986e461dc072 Initial revision
glantau
parents:
diff changeset
217
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 373
diff changeset
218 buf1= (short*)av_malloc( nb_samples * sizeof(short) );
64
5aa6292a1660 win32 fixes
glantau
parents: 0
diff changeset
219
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
220 /* first downsample by an integer factor with averaging filter */
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 if (s->iratio > 1) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 buftmp = buf1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
223 nb_samples = integer_downsample(s, buftmp, input, nb_samples);
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
225 buftmp = input;
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
227
986e461dc072 Initial revision
glantau
parents:
diff changeset
228 /* then do a fractional resampling with linear interpolation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
229 if (s->incr != FRAC) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
230 nb_samples = fractional_resample(s, output, buftmp, nb_samples);
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
232 memcpy(output, buftmp, nb_samples * sizeof(short));
986e461dc072 Initial revision
glantau
parents:
diff changeset
233 }
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 373
diff changeset
234 av_free(buf1);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 return nb_samples;
986e461dc072 Initial revision
glantau
parents:
diff changeset
236 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
237
986e461dc072 Initial revision
glantau
parents:
diff changeset
238 ReSampleContext *audio_resample_init(int output_channels, int input_channels,
986e461dc072 Initial revision
glantau
parents:
diff changeset
239 int output_rate, int input_rate)
986e461dc072 Initial revision
glantau
parents:
diff changeset
240 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
241 ReSampleContext *s;
986e461dc072 Initial revision
glantau
parents:
diff changeset
242 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
243
1408
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
244 if ( input_channels > 2)
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
245 {
1598
932d306bf1dc av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents: 1446
diff changeset
246 av_log(NULL, AV_LOG_ERROR, "Resampling with input channels greater than 2 unsupported.");
1408
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
247 return NULL;
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
248 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
249
986e461dc072 Initial revision
glantau
parents:
diff changeset
250 s = av_mallocz(sizeof(ReSampleContext));
986e461dc072 Initial revision
glantau
parents:
diff changeset
251 if (!s)
1408
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
252 {
1598
932d306bf1dc av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents: 1446
diff changeset
253 av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.");
1408
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
254 return NULL;
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
255 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
256
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 s->ratio = (float)output_rate / (float)input_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
258
986e461dc072 Initial revision
glantau
parents:
diff changeset
259 s->input_channels = input_channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
260 s->output_channels = output_channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
261
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 s->filter_channels = s->input_channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
263 if (s->output_channels < s->filter_channels)
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 s->filter_channels = s->output_channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
265
1408
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
266 /*
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
267 * ac3 output is the only case where filter_channels could be greater than 2.
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
268 * input channels can't be greater than 2, so resample the 2 channels and then
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
269 * expand to 6 channels after the resampling.
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
270 */
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
271 if(s->filter_channels>2)
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
272 s->filter_channels = 2;
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
273
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 for(i=0;i<s->filter_channels;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 init_mono_resample(&s->channel_ctx[i], s->ratio);
986e461dc072 Initial revision
glantau
parents:
diff changeset
276 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
277 return s;
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
279
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 /* resample audio. 'nb_samples' is the number of input samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 /* XXX: optimize it ! */
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 /* XXX: do it with polyphase filters, since the quality here is
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 HORRIBLE. Return the number of samples available in output */
986e461dc072 Initial revision
glantau
parents:
diff changeset
284 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
286 int i, nb_samples1;
64
5aa6292a1660 win32 fixes
glantau
parents: 0
diff changeset
287 short *bufin[2];
5aa6292a1660 win32 fixes
glantau
parents: 0
diff changeset
288 short *bufout[2];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 short *buftmp2[2], *buftmp3[2];
64
5aa6292a1660 win32 fixes
glantau
parents: 0
diff changeset
290 int lenout;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
291
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 if (s->input_channels == s->output_channels && s->ratio == 1.0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
293 /* nothing to do */
986e461dc072 Initial revision
glantau
parents:
diff changeset
294 memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 return nb_samples;
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
297
64
5aa6292a1660 win32 fixes
glantau
parents: 0
diff changeset
298 /* XXX: move those malloc to resample init code */
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 373
diff changeset
299 bufin[0]= (short*) av_malloc( nb_samples * sizeof(short) );
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 373
diff changeset
300 bufin[1]= (short*) av_malloc( nb_samples * sizeof(short) );
64
5aa6292a1660 win32 fixes
glantau
parents: 0
diff changeset
301
5aa6292a1660 win32 fixes
glantau
parents: 0
diff changeset
302 /* make some zoom to avoid round pb */
5aa6292a1660 win32 fixes
glantau
parents: 0
diff changeset
303 lenout= (int)(nb_samples * s->ratio) + 16;
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 373
diff changeset
304 bufout[0]= (short*) av_malloc( lenout * sizeof(short) );
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 373
diff changeset
305 bufout[1]= (short*) av_malloc( lenout * sizeof(short) );
64
5aa6292a1660 win32 fixes
glantau
parents: 0
diff changeset
306
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 if (s->input_channels == 2 &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 s->output_channels == 1) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
309 buftmp2[0] = bufin[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 buftmp3[0] = output;
986e461dc072 Initial revision
glantau
parents:
diff changeset
311 stereo_to_mono(buftmp2[0], input, nb_samples);
1408
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
312 } else if (s->output_channels >= 2 && s->input_channels == 1) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 buftmp2[0] = input;
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 buftmp3[0] = bufout[0];
1408
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
315 } else if (s->output_channels >= 2) {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 buftmp2[0] = bufin[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
317 buftmp2[1] = bufin[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
318 buftmp3[0] = bufout[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 buftmp3[1] = bufout[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);
986e461dc072 Initial revision
glantau
parents:
diff changeset
321 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
322 buftmp2[0] = input;
986e461dc072 Initial revision
glantau
parents:
diff changeset
323 buftmp3[0] = output;
986e461dc072 Initial revision
glantau
parents:
diff changeset
324 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
325
986e461dc072 Initial revision
glantau
parents:
diff changeset
326 /* resample each channel */
986e461dc072 Initial revision
glantau
parents:
diff changeset
327 nb_samples1 = 0; /* avoid warning */
986e461dc072 Initial revision
glantau
parents:
diff changeset
328 for(i=0;i<s->filter_channels;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
329 nb_samples1 = mono_resample(&s->channel_ctx[i], buftmp3[i], buftmp2[i], nb_samples);
986e461dc072 Initial revision
glantau
parents:
diff changeset
330 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
331
986e461dc072 Initial revision
glantau
parents:
diff changeset
332 if (s->output_channels == 2 && s->input_channels == 1) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
333 mono_to_stereo(output, buftmp3[0], nb_samples1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
334 } else if (s->output_channels == 2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
335 stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
1408
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
336 } else if (s->output_channels == 6) {
4d67eb341a0c AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents: 1128
diff changeset
337 ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
338 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
339
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 373
diff changeset
340 av_free(bufin[0]);
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 373
diff changeset
341 av_free(bufin[1]);
64
5aa6292a1660 win32 fixes
glantau
parents: 0
diff changeset
342
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 373
diff changeset
343 av_free(bufout[0]);
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 373
diff changeset
344 av_free(bufout[1]);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
345 return nb_samples1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
346 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
347
986e461dc072 Initial revision
glantau
parents:
diff changeset
348 void audio_resample_close(ReSampleContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
349 {
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 373
diff changeset
350 av_free(s);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
351 }