FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * samplerate conversion for both audio and video
25  */
26 
27 #include <string.h>
28 
29 #include "avcodec.h"
30 #include "audioconvert.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/samplefmt.h"
34 
35 #if FF_API_AVCODEC_RESAMPLE
36 
37 #define MAX_CHANNELS 8
38 
39 struct AVResampleContext;
40 
41 static const char *context_to_name(void *ptr)
42 {
43  return "audioresample";
44 }
45 
46 static const AVOption options[] = {{NULL}};
47 static const AVClass audioresample_context_class = {
48  "ReSampleContext", context_to_name, options, LIBAVUTIL_VERSION_INT
49 };
50 
51 struct ReSampleContext {
52  struct AVResampleContext *resample_context;
53  short *temp[MAX_CHANNELS];
54  int temp_len;
55  float ratio;
56  /* channel convert */
57  int input_channels, output_channels, filter_channels;
58  AVAudioConvert *convert_ctx[2];
59  enum AVSampleFormat sample_fmt[2]; ///< input and output sample format
60  unsigned sample_size[2]; ///< size of one sample in sample_fmt
61  short *buffer[2]; ///< buffers used for conversion to S16
62  unsigned buffer_size[2]; ///< sizes of allocated buffers
63 };
64 
65 /* n1: number of samples */
66 static void stereo_to_mono(short *output, short *input, int n1)
67 {
68  short *p, *q;
69  int n = n1;
70 
71  p = input;
72  q = output;
73  while (n >= 4) {
74  q[0] = (p[0] + p[1]) >> 1;
75  q[1] = (p[2] + p[3]) >> 1;
76  q[2] = (p[4] + p[5]) >> 1;
77  q[3] = (p[6] + p[7]) >> 1;
78  q += 4;
79  p += 8;
80  n -= 4;
81  }
82  while (n > 0) {
83  q[0] = (p[0] + p[1]) >> 1;
84  q++;
85  p += 2;
86  n--;
87  }
88 }
89 
90 /* n1: number of samples */
91 static void mono_to_stereo(short *output, short *input, int n1)
92 {
93  short *p, *q;
94  int n = n1;
95  int v;
96 
97  p = input;
98  q = output;
99  while (n >= 4) {
100  v = p[0]; q[0] = v; q[1] = v;
101  v = p[1]; q[2] = v; q[3] = v;
102  v = p[2]; q[4] = v; q[5] = v;
103  v = p[3]; q[6] = v; q[7] = v;
104  q += 8;
105  p += 4;
106  n -= 4;
107  }
108  while (n > 0) {
109  v = p[0]; q[0] = v; q[1] = v;
110  q += 2;
111  p += 1;
112  n--;
113  }
114 }
115 
116 /*
117 5.1 to stereo input: [fl, fr, c, lfe, rl, rr]
118 - Left = front_left + rear_gain * rear_left + center_gain * center
119 - Right = front_right + rear_gain * rear_right + center_gain * center
120 Where rear_gain is usually around 0.5-1.0 and
121  center_gain is almost always 0.7 (-3 dB)
122 */
123 static void surround_to_stereo(short **output, short *input, int channels, int samples)
124 {
125  int i;
126  short l, r;
127 
128  for (i = 0; i < samples; i++) {
129  int fl,fr,c,rl,rr;
130  fl = input[0];
131  fr = input[1];
132  c = input[2];
133  // lfe = input[3];
134  rl = input[4];
135  rr = input[5];
136 
137  l = av_clip_int16(fl + (0.5 * rl) + (0.7 * c));
138  r = av_clip_int16(fr + (0.5 * rr) + (0.7 * c));
139 
140  /* output l & r. */
141  *output[0]++ = l;
142  *output[1]++ = r;
143 
144  /* increment input. */
145  input += channels;
146  }
147 }
148 
149 static void deinterleave(short **output, short *input, int channels, int samples)
150 {
151  int i, j;
152 
153  for (i = 0; i < samples; i++) {
154  for (j = 0; j < channels; j++) {
155  *output[j]++ = *input++;
156  }
157  }
158 }
159 
160 static void interleave(short *output, short **input, int channels, int samples)
161 {
162  int i, j;
163 
164  for (i = 0; i < samples; i++) {
165  for (j = 0; j < channels; j++) {
166  *output++ = *input[j]++;
167  }
168  }
169 }
170 
171 static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
172 {
173  int i;
174  short l, r;
175 
176  for (i = 0; i < n; i++) {
177  l = *input1++;
178  r = *input2++;
179  *output++ = l; /* left */
180  *output++ = (l / 2) + (r / 2); /* center */
181  *output++ = r; /* right */
182  *output++ = 0; /* left surround */
183  *output++ = 0; /* right surroud */
184  *output++ = 0; /* low freq */
185  }
186 }
187 
188 #define SUPPORT_RESAMPLE(ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8) \
189  ch8<<7 | ch7<<6 | ch6<<5 | ch5<<4 | ch4<<3 | ch3<<2 | ch2<<1 | ch1<<0
190 
191 static const uint8_t supported_resampling[MAX_CHANNELS] = {
192  // output ch: 1 2 3 4 5 6 7 8
193  SUPPORT_RESAMPLE(1, 1, 0, 0, 0, 0, 0, 0), // 1 input channel
194  SUPPORT_RESAMPLE(1, 1, 0, 0, 0, 1, 0, 0), // 2 input channels
195  SUPPORT_RESAMPLE(0, 0, 1, 0, 0, 0, 0, 0), // 3 input channels
196  SUPPORT_RESAMPLE(0, 0, 0, 1, 0, 0, 0, 0), // 4 input channels
197  SUPPORT_RESAMPLE(0, 0, 0, 0, 1, 0, 0, 0), // 5 input channels
198  SUPPORT_RESAMPLE(0, 1, 0, 0, 0, 1, 0, 0), // 6 input channels
199  SUPPORT_RESAMPLE(0, 0, 0, 0, 0, 0, 1, 0), // 7 input channels
200  SUPPORT_RESAMPLE(0, 0, 0, 0, 0, 0, 0, 1), // 8 input channels
201 };
202 
203 ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
204  int output_rate, int input_rate,
205  enum AVSampleFormat sample_fmt_out,
206  enum AVSampleFormat sample_fmt_in,
207  int filter_length, int log2_phase_count,
208  int linear, double cutoff)
209 {
210  ReSampleContext *s;
211 
212  if (input_channels > MAX_CHANNELS) {
213  av_log(NULL, AV_LOG_ERROR,
214  "Resampling with input channels greater than %d is unsupported.\n",
215  MAX_CHANNELS);
216  return NULL;
217  }
218  if (!(supported_resampling[input_channels-1] & (1<<(output_channels-1)))) {
219  int i;
220  av_log(NULL, AV_LOG_ERROR, "Unsupported audio resampling. Allowed "
221  "output channels for %d input channel%s", input_channels,
222  input_channels > 1 ? "s:" : ":");
223  for (i = 0; i < MAX_CHANNELS; i++)
224  if (supported_resampling[input_channels-1] & (1<<i))
225  av_log(NULL, AV_LOG_ERROR, " %d", i + 1);
226  av_log(NULL, AV_LOG_ERROR, "\n");
227  return NULL;
228  }
229 
230  s = av_mallocz(sizeof(ReSampleContext));
231  if (!s) {
232  av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
233  return NULL;
234  }
235 
236  s->ratio = (float)output_rate / (float)input_rate;
237 
238  s->input_channels = input_channels;
239  s->output_channels = output_channels;
240 
241  s->filter_channels = s->input_channels;
242  if (s->output_channels < s->filter_channels)
243  s->filter_channels = s->output_channels;
244 
245  s->sample_fmt[0] = sample_fmt_in;
246  s->sample_fmt[1] = sample_fmt_out;
247  s->sample_size[0] = av_get_bytes_per_sample(s->sample_fmt[0]);
248  s->sample_size[1] = av_get_bytes_per_sample(s->sample_fmt[1]);
249 
250  if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
251  if (!(s->convert_ctx[0] = av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1,
252  s->sample_fmt[0], 1, NULL, 0))) {
253  av_log(s, AV_LOG_ERROR,
254  "Cannot convert %s sample format to s16 sample format\n",
255  av_get_sample_fmt_name(s->sample_fmt[0]));
256  av_free(s);
257  return NULL;
258  }
259  }
260 
261  if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
262  if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1,
263  AV_SAMPLE_FMT_S16, 1, NULL, 0))) {
264  av_log(s, AV_LOG_ERROR,
265  "Cannot convert s16 sample format to %s sample format\n",
266  av_get_sample_fmt_name(s->sample_fmt[1]));
267  av_audio_convert_free(s->convert_ctx[0]);
268  av_free(s);
269  return NULL;
270  }
271  }
272 
273  s->resample_context = av_resample_init(output_rate, input_rate,
274  filter_length, log2_phase_count,
275  linear, cutoff);
276 
277  *(const AVClass**)s->resample_context = &audioresample_context_class;
278 
279  return s;
280 }
281 
282 /* resample audio. 'nb_samples' is the number of input samples */
283 /* XXX: optimize it ! */
284 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
285 {
286  int i, nb_samples1;
287  short *bufin[MAX_CHANNELS];
288  short *bufout[MAX_CHANNELS];
289  short *buftmp2[MAX_CHANNELS], *buftmp3[MAX_CHANNELS];
290  short *output_bak = NULL;
291  int lenout;
292 
293  if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
294  /* nothing to do */
295  memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
296  return nb_samples;
297  }
298 
299  if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
300  int istride[1] = { s->sample_size[0] };
301  int ostride[1] = { 2 };
302  const void *ibuf[1] = { input };
303  void *obuf[1];
304  unsigned input_size = nb_samples * s->input_channels * 2;
305 
306  if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
307  av_free(s->buffer[0]);
308  s->buffer_size[0] = input_size;
309  s->buffer[0] = av_malloc(s->buffer_size[0]);
310  if (!s->buffer[0]) {
311  av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
312  return 0;
313  }
314  }
315 
316  obuf[0] = s->buffer[0];
317 
318  if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
319  ibuf, istride, nb_samples * s->input_channels) < 0) {
320  av_log(s->resample_context, AV_LOG_ERROR,
321  "Audio sample format conversion failed\n");
322  return 0;
323  }
324 
325  input = s->buffer[0];
326  }
327 
328  lenout= 2*s->output_channels*nb_samples * s->ratio + 16;
329 
330  if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
331  int out_size = lenout * av_get_bytes_per_sample(s->sample_fmt[1]) *
332  s->output_channels;
333  output_bak = output;
334 
335  if (!s->buffer_size[1] || s->buffer_size[1] < out_size) {
336  av_free(s->buffer[1]);
337  s->buffer_size[1] = out_size;
338  s->buffer[1] = av_malloc(s->buffer_size[1]);
339  if (!s->buffer[1]) {
340  av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
341  return 0;
342  }
343  }
344 
345  output = s->buffer[1];
346  }
347 
348  /* XXX: move those malloc to resample init code */
349  for (i = 0; i < s->filter_channels; i++) {
350  bufin[i] = av_malloc((nb_samples + s->temp_len) * sizeof(short));
351  memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
352  buftmp2[i] = bufin[i] + s->temp_len;
353  bufout[i] = av_malloc(lenout * sizeof(short));
354  }
355 
356  if (s->input_channels == 2 && s->output_channels == 1) {
357  buftmp3[0] = output;
358  stereo_to_mono(buftmp2[0], input, nb_samples);
359  } else if (s->output_channels >= 2 && s->input_channels == 1) {
360  buftmp3[0] = bufout[0];
361  memcpy(buftmp2[0], input, nb_samples * sizeof(short));
362  } else if (s->input_channels == 6 && s->output_channels ==2) {
363  buftmp3[0] = bufout[0];
364  buftmp3[1] = bufout[1];
365  surround_to_stereo(buftmp2, input, s->input_channels, nb_samples);
366  } else if (s->output_channels >= s->input_channels && s->input_channels >= 2) {
367  for (i = 0; i < s->input_channels; i++) {
368  buftmp3[i] = bufout[i];
369  }
370  deinterleave(buftmp2, input, s->input_channels, nb_samples);
371  } else {
372  buftmp3[0] = output;
373  memcpy(buftmp2[0], input, nb_samples * sizeof(short));
374  }
375 
376  nb_samples += s->temp_len;
377 
378  /* resample each channel */
379  nb_samples1 = 0; /* avoid warning */
380  for (i = 0; i < s->filter_channels; i++) {
381  int consumed;
382  int is_last = i + 1 == s->filter_channels;
383 
384  nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i],
385  &consumed, nb_samples, lenout, is_last);
386  s->temp_len = nb_samples - consumed;
387  s->temp[i] = av_realloc(s->temp[i], s->temp_len * sizeof(short));
388  memcpy(s->temp[i], bufin[i] + consumed, s->temp_len * sizeof(short));
389  }
390 
391  if (s->output_channels == 2 && s->input_channels == 1) {
392  mono_to_stereo(output, buftmp3[0], nb_samples1);
393  } else if (s->output_channels == 6 && s->input_channels == 2) {
394  ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
395  } else if ((s->output_channels == s->input_channels && s->input_channels >= 2) ||
396  (s->output_channels == 2 && s->input_channels == 6)) {
397  interleave(output, buftmp3, s->output_channels, nb_samples1);
398  }
399 
400  if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
401  int istride[1] = { 2 };
402  int ostride[1] = { s->sample_size[1] };
403  const void *ibuf[1] = { output };
404  void *obuf[1] = { output_bak };
405 
406  if (av_audio_convert(s->convert_ctx[1], obuf, ostride,
407  ibuf, istride, nb_samples1 * s->output_channels) < 0) {
408  av_log(s->resample_context, AV_LOG_ERROR,
409  "Audio sample format conversion failed\n");
410  return 0;
411  }
412  }
413 
414  for (i = 0; i < s->filter_channels; i++) {
415  av_free(bufin[i]);
416  av_free(bufout[i]);
417  }
418 
419  return nb_samples1;
420 }
421 
422 void audio_resample_close(ReSampleContext *s)
423 {
424  int i;
425  av_resample_close(s->resample_context);
426  for (i = 0; i < s->filter_channels; i++)
427  av_freep(&s->temp[i]);
428  av_freep(&s->buffer[0]);
429  av_freep(&s->buffer[1]);
430  av_audio_convert_free(s->convert_ctx[0]);
431  av_audio_convert_free(s->convert_ctx[1]);
432  av_free(s);
433 }
434 
435 #endif