resample.c
Go to the documentation of this file.
1 /*
2  * samplerate conversion for both audio and video
3  * Copyright (c) 2000 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "avcodec.h"
28 #include "audioconvert.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/samplefmt.h"
31 
32 #define MAX_CHANNELS 8
33 
34 struct AVResampleContext;
35 
36 static const char *context_to_name(void *ptr)
37 {
38  return "audioresample";
39 }
40 
41 static const AVOption options[] = {{NULL}};
43  "ReSampleContext", context_to_name, options, LIBAVUTIL_VERSION_INT
44 };
45 
48  short *temp[MAX_CHANNELS];
49  int temp_len;
50  float ratio;
51  /* channel convert */
55  unsigned sample_size[2];
56  short *buffer[2];
57  unsigned buffer_size[2];
58 };
59 
60 /* n1: number of samples */
61 static void stereo_to_mono(short *output, short *input, int n1)
62 {
63  short *p, *q;
64  int n = n1;
65 
66  p = input;
67  q = output;
68  while (n >= 4) {
69  q[0] = (p[0] + p[1]) >> 1;
70  q[1] = (p[2] + p[3]) >> 1;
71  q[2] = (p[4] + p[5]) >> 1;
72  q[3] = (p[6] + p[7]) >> 1;
73  q += 4;
74  p += 8;
75  n -= 4;
76  }
77  while (n > 0) {
78  q[0] = (p[0] + p[1]) >> 1;
79  q++;
80  p += 2;
81  n--;
82  }
83 }
84 
85 /* n1: number of samples */
86 static void mono_to_stereo(short *output, short *input, int n1)
87 {
88  short *p, *q;
89  int n = n1;
90  int v;
91 
92  p = input;
93  q = output;
94  while (n >= 4) {
95  v = p[0]; q[0] = v; q[1] = v;
96  v = p[1]; q[2] = v; q[3] = v;
97  v = p[2]; q[4] = v; q[5] = v;
98  v = p[3]; q[6] = v; q[7] = v;
99  q += 8;
100  p += 4;
101  n -= 4;
102  }
103  while (n > 0) {
104  v = p[0]; q[0] = v; q[1] = v;
105  q += 2;
106  p += 1;
107  n--;
108  }
109 }
110 
111 static void deinterleave(short **output, short *input, int channels, int samples)
112 {
113  int i, j;
114 
115  for (i = 0; i < samples; i++) {
116  for (j = 0; j < channels; j++) {
117  *output[j]++ = *input++;
118  }
119  }
120 }
121 
122 static void interleave(short *output, short **input, int channels, int samples)
123 {
124  int i, j;
125 
126  for (i = 0; i < samples; i++) {
127  for (j = 0; j < channels; j++) {
128  *output++ = *input[j]++;
129  }
130  }
131 }
132 
133 static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
134 {
135  int i;
136  short l, r;
137 
138  for (i = 0; i < n; i++) {
139  l = *input1++;
140  r = *input2++;
141  *output++ = l; /* left */
142  *output++ = (l / 2) + (r / 2); /* center */
143  *output++ = r; /* right */
144  *output++ = 0; /* left surround */
145  *output++ = 0; /* right surroud */
146  *output++ = 0; /* low freq */
147  }
148 }
149 
150 ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
151  int output_rate, int input_rate,
152  enum AVSampleFormat sample_fmt_out,
153  enum AVSampleFormat sample_fmt_in,
154  int filter_length, int log2_phase_count,
155  int linear, double cutoff)
156 {
157  ReSampleContext *s;
158 
159  if (input_channels > MAX_CHANNELS) {
161  "Resampling with input channels greater than %d is unsupported.\n",
162  MAX_CHANNELS);
163  return NULL;
164  }
165  if (output_channels != input_channels &&
166  (input_channels > 2 ||
167  output_channels > 2 &&
168  !(output_channels == 6 && input_channels == 2))) {
170  "Resampling output channel count must be 1 or 2 for mono input; 1, 2 or 6 for stereo input; or N for N channel input.\n");
171  return NULL;
172  }
173 
174  s = av_mallocz(sizeof(ReSampleContext));
175  if (!s) {
176  av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
177  return NULL;
178  }
179 
180  s->ratio = (float)output_rate / (float)input_rate;
181 
182  s->input_channels = input_channels;
183  s->output_channels = output_channels;
184 
186  if (s->output_channels < s->filter_channels)
188 
189  s->sample_fmt[0] = sample_fmt_in;
190  s->sample_fmt[1] = sample_fmt_out;
193 
194  if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
196  s->sample_fmt[0], 1, NULL, 0))) {
197  av_log(s, AV_LOG_ERROR,
198  "Cannot convert %s sample format to s16 sample format\n",
200  av_free(s);
201  return NULL;
202  }
203  }
204 
205  if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
206  if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1,
207  AV_SAMPLE_FMT_S16, 1, NULL, 0))) {
208  av_log(s, AV_LOG_ERROR,
209  "Cannot convert s16 sample format to %s sample format\n",
212  av_free(s);
213  return NULL;
214  }
215  }
216 
217  s->resample_context = av_resample_init(output_rate, input_rate,
218  filter_length, log2_phase_count,
219  linear, cutoff);
220 
221  *(const AVClass**)s->resample_context = &audioresample_context_class;
222 
223  return s;
224 }
225 
226 /* resample audio. 'nb_samples' is the number of input samples */
227 /* XXX: optimize it ! */
228 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
229 {
230  int i, nb_samples1;
231  short *bufin[MAX_CHANNELS];
232  short *bufout[MAX_CHANNELS];
233  short *buftmp2[MAX_CHANNELS], *buftmp3[MAX_CHANNELS];
234  short *output_bak = NULL;
235  int lenout;
236 
237  if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
238  /* nothing to do */
239  memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
240  return nb_samples;
241  }
242 
243  if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
244  int istride[1] = { s->sample_size[0] };
245  int ostride[1] = { 2 };
246  const void *ibuf[1] = { input };
247  void *obuf[1];
248  unsigned input_size = nb_samples * s->input_channels * 2;
249 
250  if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
251  av_free(s->buffer[0]);
252  s->buffer_size[0] = input_size;
253  s->buffer[0] = av_malloc(s->buffer_size[0]);
254  if (!s->buffer[0]) {
255  av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
256  return 0;
257  }
258  }
259 
260  obuf[0] = s->buffer[0];
261 
262  if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
263  ibuf, istride, nb_samples * s->input_channels) < 0) {
265  "Audio sample format conversion failed\n");
266  return 0;
267  }
268 
269  input = s->buffer[0];
270  }
271 
272  lenout = 4 * nb_samples * s->ratio + 16;
273 
274  if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
275  output_bak = output;
276 
277  if (!s->buffer_size[1] || s->buffer_size[1] < lenout) {
278  av_free(s->buffer[1]);
279  s->buffer_size[1] = lenout;
280  s->buffer[1] = av_malloc(s->buffer_size[1]);
281  if (!s->buffer[1]) {
282  av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
283  return 0;
284  }
285  }
286 
287  output = s->buffer[1];
288  }
289 
290  /* XXX: move those malloc to resample init code */
291  for (i = 0; i < s->filter_channels; i++) {
292  bufin[i] = av_malloc((nb_samples + s->temp_len) * sizeof(short));
293  memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
294  buftmp2[i] = bufin[i] + s->temp_len;
295  bufout[i] = av_malloc(lenout * sizeof(short));
296  }
297 
298  if (s->input_channels == 2 && s->output_channels == 1) {
299  buftmp3[0] = output;
300  stereo_to_mono(buftmp2[0], input, nb_samples);
301  } else if (s->output_channels >= 2 && s->input_channels == 1) {
302  buftmp3[0] = bufout[0];
303  memcpy(buftmp2[0], input, nb_samples * sizeof(short));
304  } else if (s->output_channels >= s->input_channels && s->input_channels >= 2) {
305  for (i = 0; i < s->input_channels; i++) {
306  buftmp3[i] = bufout[i];
307  }
308  deinterleave(buftmp2, input, s->input_channels, nb_samples);
309  } else {
310  buftmp3[0] = output;
311  memcpy(buftmp2[0], input, nb_samples * sizeof(short));
312  }
313 
314  nb_samples += s->temp_len;
315 
316  /* resample each channel */
317  nb_samples1 = 0; /* avoid warning */
318  for (i = 0; i < s->filter_channels; i++) {
319  int consumed;
320  int is_last = i + 1 == s->filter_channels;
321 
322  nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i],
323  &consumed, nb_samples, lenout, is_last);
324  s->temp_len = nb_samples - consumed;
325  s->temp[i] = av_realloc(s->temp[i], s->temp_len * sizeof(short));
326  memcpy(s->temp[i], bufin[i] + consumed, s->temp_len * sizeof(short));
327  }
328 
329  if (s->output_channels == 2 && s->input_channels == 1) {
330  mono_to_stereo(output, buftmp3[0], nb_samples1);
331  } else if (s->output_channels == 6 && s->input_channels == 2) {
332  ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
333  } else if (s->output_channels == s->input_channels && s->input_channels >= 2) {
334  interleave(output, buftmp3, s->output_channels, nb_samples1);
335  }
336 
337  if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
338  int istride[1] = { 2 };
339  int ostride[1] = { s->sample_size[1] };
340  const void *ibuf[1] = { output };
341  void *obuf[1] = { output_bak };
342 
343  if (av_audio_convert(s->convert_ctx[1], obuf, ostride,
344  ibuf, istride, nb_samples1 * s->output_channels) < 0) {
346  "Audio sample format convertion failed\n");
347  return 0;
348  }
349  }
350 
351  for (i = 0; i < s->filter_channels; i++) {
352  av_free(bufin[i]);
353  av_free(bufout[i]);
354  }
355 
356  return nb_samples1;
357 }
358 
360 {
361  int i;
363  for (i = 0; i < s->filter_channels; i++)
364  av_freep(&s->temp[i]);
365  av_freep(&s->buffer[0]);
366  av_freep(&s->buffer[1]);
369  av_free(s);
370 }