FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
utils.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/common.h"
22 #include "libavutil/dict.h"
23 // #include "libavutil/error.h"
24 #include "libavutil/log.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/opt.h"
27 
28 #include "avresample.h"
29 #include "internal.h"
30 #include "audio_data.h"
31 #include "audio_convert.h"
32 #include "audio_mix.h"
33 #include "resample.h"
34 
36 {
37  int ret;
38 
39  if (avresample_is_open(avr)) {
40  av_log(avr, AV_LOG_ERROR, "The resampling context is already open.\n");
41  return AVERROR(EINVAL);
42  }
43 
44  /* set channel mixing parameters */
46  if (avr->in_channels <= 0 || avr->in_channels > AVRESAMPLE_MAX_CHANNELS) {
47  av_log(avr, AV_LOG_ERROR, "Invalid input channel layout: %"PRIu64"\n",
48  avr->in_channel_layout);
49  return AVERROR(EINVAL);
50  }
52  if (avr->out_channels <= 0 || avr->out_channels > AVRESAMPLE_MAX_CHANNELS) {
53  av_log(avr, AV_LOG_ERROR, "Invalid output channel layout: %"PRIu64"\n",
54  avr->out_channel_layout);
55  return AVERROR(EINVAL);
56  }
58  avr->downmix_needed = avr->in_channels > avr->out_channels;
59  avr->upmix_needed = avr->out_channels > avr->in_channels ||
60  (!avr->downmix_needed && (avr->mix_matrix ||
62  avr->mixing_needed = avr->downmix_needed || avr->upmix_needed;
63 
64  /* set resampling parameters */
65  avr->resample_needed = avr->in_sample_rate != avr->out_sample_rate ||
66  avr->force_resampling;
67 
68  /* select internal sample format if not specified by the user */
70  (avr->mixing_needed || avr->resample_needed)) {
73  int max_bps = FFMAX(av_get_bytes_per_sample(in_fmt),
74  av_get_bytes_per_sample(out_fmt));
75  if (max_bps <= 2) {
77  } else if (avr->mixing_needed) {
79  } else {
80  if (max_bps <= 4) {
81  if (in_fmt == AV_SAMPLE_FMT_S32P ||
82  out_fmt == AV_SAMPLE_FMT_S32P) {
83  if (in_fmt == AV_SAMPLE_FMT_FLTP ||
84  out_fmt == AV_SAMPLE_FMT_FLTP) {
85  /* if one is s32 and the other is flt, use dbl */
87  } else {
88  /* if one is s32 and the other is s32, s16, or u8, use s32 */
90  }
91  } else {
92  /* if one is flt and the other is flt, s16 or u8, use flt */
94  }
95  } else {
96  /* if either is dbl, use dbl */
98  }
99  }
100  av_log(avr, AV_LOG_DEBUG, "Using %s as internal sample format\n",
102  }
103 
104  /* treat all mono as planar for easier comparison */
105  if (avr->in_channels == 1)
107  if (avr->out_channels == 1)
109 
110  /* we may need to add an extra conversion in order to remap channels if
111  the output format is not planar */
112  if (avr->use_channel_map && !avr->mixing_needed && !avr->resample_needed &&
115  }
116 
117  /* set sample format conversion parameters */
118  if (avr->resample_needed || avr->mixing_needed)
120  else
121  avr->in_convert_needed = avr->use_channel_map &&
123 
124  if (avr->resample_needed || avr->mixing_needed || avr->in_convert_needed)
126  else
127  avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;
128 
129  avr->in_copy_needed = !avr->in_convert_needed && (avr->mixing_needed ||
130  (avr->use_channel_map && avr->resample_needed));
131 
132  if (avr->use_channel_map) {
133  if (avr->in_copy_needed) {
134  avr->remap_point = REMAP_IN_COPY;
135  av_dlog(avr, "remap channels during in_copy\n");
136  } else if (avr->in_convert_needed) {
138  av_dlog(avr, "remap channels during in_convert\n");
139  } else if (avr->out_convert_needed) {
141  av_dlog(avr, "remap channels during out_convert\n");
142  } else {
144  av_dlog(avr, "remap channels during out_copy\n");
145  }
146 
147 #ifdef DEBUG
148  {
149  int ch;
150  av_dlog(avr, "output map: ");
151  if (avr->ch_map_info.do_remap)
152  for (ch = 0; ch < avr->in_channels; ch++)
153  av_dlog(avr, " % 2d", avr->ch_map_info.channel_map[ch]);
154  else
155  av_dlog(avr, "n/a");
156  av_dlog(avr, "\n");
157  av_dlog(avr, "copy map: ");
158  if (avr->ch_map_info.do_copy)
159  for (ch = 0; ch < avr->in_channels; ch++)
160  av_dlog(avr, " % 2d", avr->ch_map_info.channel_copy[ch]);
161  else
162  av_dlog(avr, "n/a");
163  av_dlog(avr, "\n");
164  av_dlog(avr, "zero map: ");
165  if (avr->ch_map_info.do_zero)
166  for (ch = 0; ch < avr->in_channels; ch++)
167  av_dlog(avr, " % 2d", avr->ch_map_info.channel_zero[ch]);
168  else
169  av_dlog(avr, "n/a");
170  av_dlog(avr, "\n");
171  av_dlog(avr, "input map: ");
172  for (ch = 0; ch < avr->in_channels; ch++)
173  av_dlog(avr, " % 2d", avr->ch_map_info.input_map[ch]);
174  av_dlog(avr, "\n");
175  }
176 #endif
177  } else
178  avr->remap_point = REMAP_NONE;
179 
180  /* allocate buffers */
181  if (avr->in_copy_needed || avr->in_convert_needed) {
183  0, avr->internal_sample_fmt,
184  "in_buffer");
185  if (!avr->in_buffer) {
186  ret = AVERROR(EINVAL);
187  goto error;
188  }
189  }
190  if (avr->resample_needed) {
192  1024, avr->internal_sample_fmt,
193  "resample_out_buffer");
194  if (!avr->resample_out_buffer) {
195  ret = AVERROR(EINVAL);
196  goto error;
197  }
198  }
199  if (avr->out_convert_needed) {
201  avr->out_sample_fmt, "out_buffer");
202  if (!avr->out_buffer) {
203  ret = AVERROR(EINVAL);
204  goto error;
205  }
206  }
208  1024);
209  if (!avr->out_fifo) {
210  ret = AVERROR(ENOMEM);
211  goto error;
212  }
213 
214  /* setup contexts */
215  if (avr->in_convert_needed) {
217  avr->in_sample_fmt, avr->in_channels,
218  avr->in_sample_rate,
219  avr->remap_point == REMAP_IN_CONVERT);
220  if (!avr->ac_in) {
221  ret = AVERROR(ENOMEM);
222  goto error;
223  }
224  }
225  if (avr->out_convert_needed) {
226  enum AVSampleFormat src_fmt;
227  if (avr->in_convert_needed)
228  src_fmt = avr->internal_sample_fmt;
229  else
230  src_fmt = avr->in_sample_fmt;
231  avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
232  avr->out_channels,
233  avr->out_sample_rate,
235  if (!avr->ac_out) {
236  ret = AVERROR(ENOMEM);
237  goto error;
238  }
239  }
240  if (avr->resample_needed) {
241  avr->resample = ff_audio_resample_init(avr);
242  if (!avr->resample) {
243  ret = AVERROR(ENOMEM);
244  goto error;
245  }
246  }
247  if (avr->mixing_needed) {
248  avr->am = ff_audio_mix_alloc(avr);
249  if (!avr->am) {
250  ret = AVERROR(ENOMEM);
251  goto error;
252  }
253  }
254 
255  return 0;
256 
257 error:
258  avresample_close(avr);
259  return ret;
260 }
261 
263 {
264  return !!avr->out_fifo;
265 }
266 
268 {
273  avr->out_fifo = NULL;
277  ff_audio_mix_free(&avr->am);
278  av_freep(&avr->mix_matrix);
279 
280  avr->use_channel_map = 0;
281 }
282 
284 {
285  if (!*avr)
286  return;
287  avresample_close(*avr);
288  av_opt_free(*avr);
289  av_freep(avr);
290 }
291 
293  AudioData *output, AudioData *converted)
294 {
295  int ret;
296 
297  if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
298  (converted && output->allocated_samples < converted->nb_samples)) {
299  if (converted) {
300  /* if there are any samples in the output FIFO or if the
301  user-supplied output buffer is not large enough for all samples,
302  we add to the output FIFO */
303  av_dlog(avr, "[FIFO] add %s to out_fifo\n", converted->name);
304  ret = ff_audio_data_add_to_fifo(avr->out_fifo, converted, 0,
305  converted->nb_samples);
306  if (ret < 0)
307  return ret;
308  }
309 
310  /* if the user specified an output buffer, read samples from the output
311  FIFO to the user output */
312  if (output && output->allocated_samples > 0) {
313  av_dlog(avr, "[FIFO] read from out_fifo to output\n");
314  av_dlog(avr, "[end conversion]\n");
315  return ff_audio_data_read_from_fifo(avr->out_fifo, output,
316  output->allocated_samples);
317  }
318  } else if (converted) {
319  /* copy directly to output if it is large enough or there is not any
320  data in the output FIFO */
321  av_dlog(avr, "[copy] %s to output\n", converted->name);
322  output->nb_samples = 0;
323  ret = ff_audio_data_copy(output, converted,
324  avr->remap_point == REMAP_OUT_COPY ?
325  &avr->ch_map_info : NULL);
326  if (ret < 0)
327  return ret;
328  av_dlog(avr, "[end conversion]\n");
329  return output->nb_samples;
330  }
331  av_dlog(avr, "[end conversion]\n");
332  return 0;
333 }
334 
335 int attribute_align_arg avresample_convert(AVAudioResampleContext *avr,
336  uint8_t **output, int out_plane_size,
337  int out_samples, uint8_t **input,
338  int in_plane_size, int in_samples)
339 {
340  AudioData input_buffer;
341  AudioData output_buffer;
342  AudioData *current_buffer;
343  int ret, direct_output;
344 
345  /* reset internal buffers */
346  if (avr->in_buffer) {
347  avr->in_buffer->nb_samples = 0;
350  }
351  if (avr->resample_out_buffer) {
355  }
356  if (avr->out_buffer) {
357  avr->out_buffer->nb_samples = 0;
360  }
361 
362  av_dlog(avr, "[start conversion]\n");
363 
364  /* initialize output_buffer with output data */
365  direct_output = output && av_audio_fifo_size(avr->out_fifo) == 0;
366  if (output) {
367  ret = ff_audio_data_init(&output_buffer, output, out_plane_size,
368  avr->out_channels, out_samples,
369  avr->out_sample_fmt, 0, "output");
370  if (ret < 0)
371  return ret;
372  output_buffer.nb_samples = 0;
373  }
374 
375  if (input) {
376  /* initialize input_buffer with input data */
377  ret = ff_audio_data_init(&input_buffer, input, in_plane_size,
378  avr->in_channels, in_samples,
379  avr->in_sample_fmt, 1, "input");
380  if (ret < 0)
381  return ret;
382  current_buffer = &input_buffer;
383 
384  if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&
385  !avr->out_convert_needed && direct_output && out_samples >= in_samples) {
386  /* in some rare cases we can copy input to output and upmix
387  directly in the output buffer */
388  av_dlog(avr, "[copy] %s to output\n", current_buffer->name);
389  ret = ff_audio_data_copy(&output_buffer, current_buffer,
390  avr->remap_point == REMAP_OUT_COPY ?
391  &avr->ch_map_info : NULL);
392  if (ret < 0)
393  return ret;
394  current_buffer = &output_buffer;
395  } else if (avr->remap_point == REMAP_OUT_COPY &&
396  (!direct_output || out_samples < in_samples)) {
397  /* if remapping channels during output copy, we may need to
398  * use an intermediate buffer in order to remap before adding
399  * samples to the output fifo */
400  av_dlog(avr, "[copy] %s to out_buffer\n", current_buffer->name);
401  ret = ff_audio_data_copy(avr->out_buffer, current_buffer,
402  &avr->ch_map_info);
403  if (ret < 0)
404  return ret;
405  current_buffer = avr->out_buffer;
406  } else if (avr->in_copy_needed || avr->in_convert_needed) {
407  /* if needed, copy or convert input to in_buffer, and downmix if
408  applicable */
409  if (avr->in_convert_needed) {
410  ret = ff_audio_data_realloc(avr->in_buffer,
411  current_buffer->nb_samples);
412  if (ret < 0)
413  return ret;
414  av_dlog(avr, "[convert] %s to in_buffer\n", current_buffer->name);
415  ret = ff_audio_convert(avr->ac_in, avr->in_buffer,
416  current_buffer);
417  if (ret < 0)
418  return ret;
419  } else {
420  av_dlog(avr, "[copy] %s to in_buffer\n", current_buffer->name);
421  ret = ff_audio_data_copy(avr->in_buffer, current_buffer,
422  avr->remap_point == REMAP_IN_COPY ?
423  &avr->ch_map_info : NULL);
424  if (ret < 0)
425  return ret;
426  }
428  if (avr->downmix_needed) {
429  av_dlog(avr, "[downmix] in_buffer\n");
430  ret = ff_audio_mix(avr->am, avr->in_buffer);
431  if (ret < 0)
432  return ret;
433  }
434  current_buffer = avr->in_buffer;
435  }
436  } else {
437  /* flush resampling buffer and/or output FIFO if input is NULL */
438  if (!avr->resample_needed)
439  return handle_buffered_output(avr, output ? &output_buffer : NULL,
440  NULL);
441  current_buffer = NULL;
442  }
443 
444  if (avr->resample_needed) {
445  AudioData *resample_out;
446 
447  if (!avr->out_convert_needed && direct_output && out_samples > 0)
448  resample_out = &output_buffer;
449  else
450  resample_out = avr->resample_out_buffer;
451  av_dlog(avr, "[resample] %s to %s\n",
452  current_buffer ? current_buffer->name : "null",
453  resample_out->name);
454  ret = ff_audio_resample(avr->resample, resample_out,
455  current_buffer);
456  if (ret < 0)
457  return ret;
458 
459  /* if resampling did not produce any samples, just return 0 */
460  if (resample_out->nb_samples == 0) {
461  av_dlog(avr, "[end conversion]\n");
462  return 0;
463  }
464 
465  current_buffer = resample_out;
466  }
467 
468  if (avr->upmix_needed) {
469  av_dlog(avr, "[upmix] %s\n", current_buffer->name);
470  ret = ff_audio_mix(avr->am, current_buffer);
471  if (ret < 0)
472  return ret;
473  }
474 
475  /* if we resampled or upmixed directly to output, return here */
476  if (current_buffer == &output_buffer) {
477  av_dlog(avr, "[end conversion]\n");
478  return current_buffer->nb_samples;
479  }
480 
481  if (avr->out_convert_needed) {
482  if (direct_output && out_samples >= current_buffer->nb_samples) {
483  /* convert directly to output */
484  av_dlog(avr, "[convert] %s to output\n", current_buffer->name);
485  ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer);
486  if (ret < 0)
487  return ret;
488 
489  av_dlog(avr, "[end conversion]\n");
490  return output_buffer.nb_samples;
491  } else {
493  current_buffer->nb_samples);
494  if (ret < 0)
495  return ret;
496  av_dlog(avr, "[convert] %s to out_buffer\n", current_buffer->name);
497  ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
498  current_buffer);
499  if (ret < 0)
500  return ret;
501  current_buffer = avr->out_buffer;
502  }
503  }
504 
505  return handle_buffered_output(avr, output ? &output_buffer : NULL,
506  current_buffer);
507 }
508 
510  int stride)
511 {
512  int in_channels, out_channels, i, o;
513 
514  if (avr->am)
515  return ff_audio_mix_get_matrix(avr->am, matrix, stride);
516 
519 
520  if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
521  out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
522  av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
523  return AVERROR(EINVAL);
524  }
525 
526  if (!avr->mix_matrix) {
527  av_log(avr, AV_LOG_ERROR, "matrix is not set\n");
528  return AVERROR(EINVAL);
529  }
530 
531  for (o = 0; o < out_channels; o++)
532  for (i = 0; i < in_channels; i++)
533  matrix[o * stride + i] = avr->mix_matrix[o * in_channels + i];
534 
535  return 0;
536 }
537 
538 int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
539  int stride)
540 {
541  int in_channels, out_channels, i, o;
542 
543  if (avr->am)
544  return ff_audio_mix_set_matrix(avr->am, matrix, stride);
545 
548 
549  if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
550  out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
551  av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
552  return AVERROR(EINVAL);
553  }
554 
555  if (avr->mix_matrix)
556  av_freep(&avr->mix_matrix);
557  avr->mix_matrix = av_malloc(in_channels * out_channels *
558  sizeof(*avr->mix_matrix));
559  if (!avr->mix_matrix)
560  return AVERROR(ENOMEM);
561 
562  for (o = 0; o < out_channels; o++)
563  for (i = 0; i < in_channels; i++)
564  avr->mix_matrix[o * in_channels + i] = matrix[o * stride + i];
565 
566  return 0;
567 }
568 
570  const int *channel_map)
571 {
572  ChannelMapInfo *info = &avr->ch_map_info;
573  int in_channels, ch, i;
574 
576  if (in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS) {
577  av_log(avr, AV_LOG_ERROR, "Invalid input channel layout\n");
578  return AVERROR(EINVAL);
579  }
580 
581  memset(info, 0, sizeof(*info));
582  memset(info->input_map, -1, sizeof(info->input_map));
583 
584  for (ch = 0; ch < in_channels; ch++) {
585  if (channel_map[ch] >= in_channels) {
586  av_log(avr, AV_LOG_ERROR, "Invalid channel map\n");
587  return AVERROR(EINVAL);
588  }
589  if (channel_map[ch] < 0) {
590  info->channel_zero[ch] = 1;
591  info->channel_map[ch] = -1;
592  info->do_zero = 1;
593  } else if (info->input_map[channel_map[ch]] >= 0) {
594  info->channel_copy[ch] = info->input_map[channel_map[ch]];
595  info->channel_map[ch] = -1;
596  info->do_copy = 1;
597  } else {
598  info->channel_map[ch] = channel_map[ch];
599  info->input_map[channel_map[ch]] = ch;
600  info->do_remap = 1;
601  }
602  }
603  /* Fill-in unmapped input channels with unmapped output channels.
604  This is used when remapping during conversion from interleaved to
605  planar format. */
606  for (ch = 0, i = 0; ch < in_channels && i < in_channels; ch++, i++) {
607  while (ch < in_channels && info->input_map[ch] >= 0)
608  ch++;
609  while (i < in_channels && info->channel_map[i] >= 0)
610  i++;
611  if (ch >= in_channels || i >= in_channels)
612  break;
613  info->input_map[ch] = i;
614  }
615 
616  avr->use_channel_map = 1;
617  return 0;
618 }
619 
621 {
622  return av_audio_fifo_size(avr->out_fifo);
623 }
624 
626 {
627  int64_t samples = avresample_get_delay(avr) + (int64_t)in_nb_samples;
628 
629  if (avr->resample_needed) {
630  samples = av_rescale_rnd(samples,
631  avr->out_sample_rate,
632  avr->in_sample_rate,
633  AV_ROUND_UP);
634  }
635 
636  samples += avresample_available(avr);
637 
638  if (samples > INT_MAX)
639  return AVERROR(EINVAL);
640 
641  return samples;
642 }
643 
644 int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
645 {
646  if (!output)
647  return av_audio_fifo_drain(avr->out_fifo, nb_samples);
648  return av_audio_fifo_read(avr->out_fifo, (void**)output, nb_samples);
649 }
650 
651 unsigned avresample_version(void)
652 {
654 }
655 
656 const char *avresample_license(void)
657 {
658 #define LICENSE_PREFIX "libavresample license: "
659  return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
660 }
661 
662 const char *avresample_configuration(void)
663 {
664  return FFMPEG_CONFIGURATION;
665 }