2 * Sample rate convertion for both audio and video
3 * Copyright (c) 2000 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Sample rate convertion for both audio and video.
27 struct AVResampleContext
;
29 struct ReSampleContext
{
30 struct AVResampleContext
*resample_context
;
35 int input_channels
, output_channels
, filter_channels
;
38 /* n1: number of samples */
39 static void stereo_to_mono(short *output
, short *input
, int n1
)
47 q
[0] = (p
[0] + p
[1]) >> 1;
48 q
[1] = (p
[2] + p
[3]) >> 1;
49 q
[2] = (p
[4] + p
[5]) >> 1;
50 q
[3] = (p
[6] + p
[7]) >> 1;
56 q
[0] = (p
[0] + p
[1]) >> 1;
63 /* n1: number of samples */
64 static void mono_to_stereo(short *output
, short *input
, int n1
)
73 v
= p
[0]; q
[0] = v
; q
[1] = v
;
74 v
= p
[1]; q
[2] = v
; q
[3] = v
;
75 v
= p
[2]; q
[4] = v
; q
[5] = v
;
76 v
= p
[3]; q
[6] = v
; q
[7] = v
;
82 v
= p
[0]; q
[0] = v
; q
[1] = v
;
89 /* XXX: should use more abstract 'N' channels system */
90 static void stereo_split(short *output1
, short *output2
, short *input
, int n
)
95 *output1
++ = *input
++;
96 *output2
++ = *input
++;
100 static void stereo_mux(short *output
, short *input1
, short *input2
, int n
)
105 *output
++ = *input1
++;
106 *output
++ = *input2
++;
110 static void ac3_5p1_mux(short *output
, short *input1
, short *input2
, int n
)
118 *output
++ = l
; /* left */
119 *output
++ = (l
/2)+(r
/2); /* center */
120 *output
++ = r
; /* right */
121 *output
++ = 0; /* left surround */
122 *output
++ = 0; /* right surroud */
123 *output
++ = 0; /* low freq */
127 ReSampleContext
*audio_resample_init(int output_channels
, int input_channels
,
128 int output_rate
, int input_rate
)
132 if ( input_channels
> 2)
134 av_log(NULL
, AV_LOG_ERROR
, "Resampling with input channels greater than 2 unsupported.");
138 s
= av_mallocz(sizeof(ReSampleContext
));
141 av_log(NULL
, AV_LOG_ERROR
, "Can't allocate memory for resample context.");
145 s
->ratio
= (float)output_rate
/ (float)input_rate
;
147 s
->input_channels
= input_channels
;
148 s
->output_channels
= output_channels
;
150 s
->filter_channels
= s
->input_channels
;
151 if (s
->output_channels
< s
->filter_channels
)
152 s
->filter_channels
= s
->output_channels
;
155 * ac3 output is the only case where filter_channels could be greater than 2.
156 * input channels can't be greater than 2, so resample the 2 channels and then
157 * expand to 6 channels after the resampling.
159 if(s
->filter_channels
>2)
160 s
->filter_channels
= 2;
162 s
->resample_context
= av_resample_init(output_rate
, input_rate
, 16, 10, 0, 1.0);
167 /* resample audio. 'nb_samples' is the number of input samples */
168 /* XXX: optimize it ! */
169 int audio_resample(ReSampleContext
*s
, short *output
, short *input
, int nb_samples
)
174 short *buftmp2
[2], *buftmp3
[2];
177 if (s
->input_channels
== s
->output_channels
&& s
->ratio
== 1.0 && 0) {
179 memcpy(output
, input
, nb_samples
* s
->input_channels
* sizeof(short));
183 /* XXX: move those malloc to resample init code */
184 for(i
=0; i
<s
->filter_channels
; i
++){
185 bufin
[i
]= (short*) av_malloc( (nb_samples
+ s
->temp_len
) * sizeof(short) );
186 memcpy(bufin
[i
], s
->temp
[i
], s
->temp_len
* sizeof(short));
187 buftmp2
[i
] = bufin
[i
] + s
->temp_len
;
190 /* make some zoom to avoid round pb */
191 lenout
= (int)(nb_samples
* s
->ratio
) + 16;
192 bufout
[0]= (short*) av_malloc( lenout
* sizeof(short) );
193 bufout
[1]= (short*) av_malloc( lenout
* sizeof(short) );
195 if (s
->input_channels
== 2 &&
196 s
->output_channels
== 1) {
198 stereo_to_mono(buftmp2
[0], input
, nb_samples
);
199 } else if (s
->output_channels
>= 2 && s
->input_channels
== 1) {
200 buftmp3
[0] = bufout
[0];
201 memcpy(buftmp2
[0], input
, nb_samples
*sizeof(short));
202 } else if (s
->output_channels
>= 2) {
203 buftmp3
[0] = bufout
[0];
204 buftmp3
[1] = bufout
[1];
205 stereo_split(buftmp2
[0], buftmp2
[1], input
, nb_samples
);
208 memcpy(buftmp2
[0], input
, nb_samples
*sizeof(short));
211 nb_samples
+= s
->temp_len
;
213 /* resample each channel */
214 nb_samples1
= 0; /* avoid warning */
215 for(i
=0;i
<s
->filter_channels
;i
++) {
217 int is_last
= i
+1 == s
->filter_channels
;
219 nb_samples1
= av_resample(s
->resample_context
, buftmp3
[i
], bufin
[i
], &consumed
, nb_samples
, lenout
, is_last
);
220 s
->temp_len
= nb_samples
- consumed
;
221 s
->temp
[i
]= av_realloc(s
->temp
[i
], s
->temp_len
*sizeof(short));
222 memcpy(s
->temp
[i
], bufin
[i
] + consumed
, s
->temp_len
*sizeof(short));
225 if (s
->output_channels
== 2 && s
->input_channels
== 1) {
226 mono_to_stereo(output
, buftmp3
[0], nb_samples1
);
227 } else if (s
->output_channels
== 2) {
228 stereo_mux(output
, buftmp3
[0], buftmp3
[1], nb_samples1
);
229 } else if (s
->output_channels
== 6) {
230 ac3_5p1_mux(output
, buftmp3
[0], buftmp3
[1], nb_samples1
);
233 for(i
=0; i
<s
->filter_channels
; i
++)
241 void audio_resample_close(ReSampleContext
*s
)
243 av_resample_close(s
->resample_context
);
244 av_freep(&s
->temp
[0]);
245 av_freep(&s
->temp
[1]);