Document sws_getIdentityVec().
[libav.git] / libavcodec / resample.c
CommitLineData
de6d9b64 1/*
f1ea5c2a 2 * samplerate conversion for both audio and video
406792e7 3 * Copyright (c) 2000 Fabrice Bellard
de6d9b64 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
ff4ec49e
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
ff4ec49e 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
983e3246
MN
21
22/**
bad5537e 23 * @file libavcodec/resample.c
f1ea5c2a 24 * samplerate conversion for both audio and video
983e3246
MN
25 */
26
de6d9b64 27#include "avcodec.h"
69db4e10 28
aaaf1635 29struct AVResampleContext;
de6d9b64
FB
30
31struct ReSampleContext {
aaaf1635
MN
32 struct AVResampleContext *resample_context;
33 short *temp[2];
34 int temp_len;
de6d9b64
FB
35 float ratio;
36 /* channel convert */
37 int input_channels, output_channels, filter_channels;
38};
39
de6d9b64
FB
40/* n1: number of samples */
41static void stereo_to_mono(short *output, short *input, int n1)
42{
43 short *p, *q;
44 int n = n1;
45
46 p = input;
47 q = output;
48 while (n >= 4) {
49 q[0] = (p[0] + p[1]) >> 1;
50 q[1] = (p[2] + p[3]) >> 1;
51 q[2] = (p[4] + p[5]) >> 1;
52 q[3] = (p[6] + p[7]) >> 1;
53 q += 4;
54 p += 8;
55 n -= 4;
56 }
57 while (n > 0) {
58 q[0] = (p[0] + p[1]) >> 1;
59 q++;
60 p += 2;
61 n--;
62 }
63}
64
65/* n1: number of samples */
66static void mono_to_stereo(short *output, short *input, int n1)
67{
68 short *p, *q;
69 int n = n1;
70 int v;
71
72 p = input;
73 q = output;
74 while (n >= 4) {
75 v = p[0]; q[0] = v; q[1] = v;
76 v = p[1]; q[2] = v; q[3] = v;
77 v = p[2]; q[4] = v; q[5] = v;
78 v = p[3]; q[6] = v; q[7] = v;
79 q += 8;
80 p += 4;
81 n -= 4;
82 }
83 while (n > 0) {
84 v = p[0]; q[0] = v; q[1] = v;
85 q += 2;
86 p += 1;
87 n--;
88 }
89}
90
91/* XXX: should use more abstract 'N' channels system */
92static void stereo_split(short *output1, short *output2, short *input, int n)
93{
94 int i;
95
96 for(i=0;i<n;i++) {
97 *output1++ = *input++;
98 *output2++ = *input++;
99 }
100}
101
102static void stereo_mux(short *output, short *input1, short *input2, int n)
103{
104 int i;
105
106 for(i=0;i<n;i++) {
107 *output++ = *input1++;
108 *output++ = *input2++;
109 }
110}
111
743739d2
MN
112static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
113{
114 int i;
115 short l,r;
116
117 for(i=0;i<n;i++) {
118 l=*input1++;
119 r=*input2++;
120 *output++ = l; /* left */
121 *output++ = (l/2)+(r/2); /* center */
122 *output++ = r; /* right */
123 *output++ = 0; /* left surround */
124 *output++ = 0; /* right surroud */
125 *output++ = 0; /* low freq */
126 }
127}
128
115329f1 129ReSampleContext *audio_resample_init(int output_channels, int input_channels,
de6d9b64
FB
130 int output_rate, int input_rate)
131{
132 ReSampleContext *s;
115329f1 133
743739d2
MN
134 if ( input_channels > 2)
135 {
30dc5541 136 av_log(NULL, AV_LOG_ERROR, "Resampling with input channels greater than 2 unsupported.\n");
bb270c08 137 return NULL;
743739d2 138 }
de6d9b64
FB
139
140 s = av_mallocz(sizeof(ReSampleContext));
141 if (!s)
743739d2 142 {
30dc5541 143 av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
bb270c08 144 return NULL;
743739d2 145 }
de6d9b64
FB
146
147 s->ratio = (float)output_rate / (float)input_rate;
115329f1 148
de6d9b64
FB
149 s->input_channels = input_channels;
150 s->output_channels = output_channels;
115329f1 151
de6d9b64
FB
152 s->filter_channels = s->input_channels;
153 if (s->output_channels < s->filter_channels)
154 s->filter_channels = s->output_channels;
155
743739d2 156/*
14b70628 157 * AC-3 output is the only case where filter_channels could be greater than 2.
743739d2
MN
158 * input channels can't be greater than 2, so resample the 2 channels and then
159 * expand to 6 channels after the resampling.
160 */
161 if(s->filter_channels>2)
162 s->filter_channels = 2;
163
8ec04d34
MN
164#define TAPS 16
165 s->resample_context= av_resample_init(output_rate, input_rate, TAPS, 10, 0, 0.8);
115329f1 166
de6d9b64
FB
167 return s;
168}
169
170/* resample audio. 'nb_samples' is the number of input samples */
171/* XXX: optimize it ! */
de6d9b64
FB
172int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
173{
174 int i, nb_samples1;
1a565432
FB
175 short *bufin[2];
176 short *bufout[2];
de6d9b64 177 short *buftmp2[2], *buftmp3[2];
1a565432 178 int lenout;
de6d9b64 179
b9d2085b 180 if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
de6d9b64
FB
181 /* nothing to do */
182 memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
183 return nb_samples;
184 }
185
1a565432 186 /* XXX: move those malloc to resample init code */
aaaf1635 187 for(i=0; i<s->filter_channels; i++){
90901860 188 bufin[i]= av_malloc( (nb_samples + s->temp_len) * sizeof(short) );
aaaf1635
MN
189 memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
190 buftmp2[i] = bufin[i] + s->temp_len;
191 }
115329f1 192
1a565432 193 /* make some zoom to avoid round pb */
5d702d6d 194 lenout= 4*nb_samples * s->ratio + 16;
90901860
MN
195 bufout[0]= av_malloc( lenout * sizeof(short) );
196 bufout[1]= av_malloc( lenout * sizeof(short) );
1a565432 197
de6d9b64
FB
198 if (s->input_channels == 2 &&
199 s->output_channels == 1) {
de6d9b64
FB
200 buftmp3[0] = output;
201 stereo_to_mono(buftmp2[0], input, nb_samples);
743739d2 202 } else if (s->output_channels >= 2 && s->input_channels == 1) {
de6d9b64 203 buftmp3[0] = bufout[0];
aaaf1635 204 memcpy(buftmp2[0], input, nb_samples*sizeof(short));
743739d2 205 } else if (s->output_channels >= 2) {
de6d9b64
FB
206 buftmp3[0] = bufout[0];
207 buftmp3[1] = bufout[1];
208 stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);
209 } else {
de6d9b64 210 buftmp3[0] = output;
aaaf1635 211 memcpy(buftmp2[0], input, nb_samples*sizeof(short));
de6d9b64
FB
212 }
213
aaaf1635
MN
214 nb_samples += s->temp_len;
215
de6d9b64
FB
216 /* resample each channel */
217 nb_samples1 = 0; /* avoid warning */
218 for(i=0;i<s->filter_channels;i++) {
aaaf1635
MN
219 int consumed;
220 int is_last= i+1 == s->filter_channels;
221
222 nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i], &consumed, nb_samples, lenout, is_last);
223 s->temp_len= nb_samples - consumed;
224 s->temp[i]= av_realloc(s->temp[i], s->temp_len*sizeof(short));
225 memcpy(s->temp[i], bufin[i] + consumed, s->temp_len*sizeof(short));
de6d9b64
FB
226 }
227
228 if (s->output_channels == 2 && s->input_channels == 1) {
229 mono_to_stereo(output, buftmp3[0], nb_samples1);
230 } else if (s->output_channels == 2) {
231 stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
743739d2
MN
232 } else if (s->output_channels == 6) {
233 ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
de6d9b64
FB
234 }
235
dca97cbe
MN
236 for(i=0; i<s->filter_channels; i++)
237 av_free(bufin[i]);
1a565432 238
6000abfa
FB
239 av_free(bufout[0]);
240 av_free(bufout[1]);
de6d9b64
FB
241 return nb_samples1;
242}
243
244void audio_resample_close(ReSampleContext *s)
245{
aaaf1635
MN
246 av_resample_close(s->resample_context);
247 av_freep(&s->temp[0]);
248 av_freep(&s->temp[1]);
6000abfa 249 av_free(s);
de6d9b64 250}