2 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 * This file is part of FFmpeg.
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.
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.
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
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"
28 #include "avresample.h"
30 #include "audio_data.h"
31 #include "audio_convert.h"
32 #include "audio_mix.h"
35 int avresample_open(AVAudioResampleContext *avr)
39 if (avresample_is_open(avr)) {
40 av_log(avr, AV_LOG_ERROR, "The resampling context is already open.\n");
41 return AVERROR(EINVAL);
44 /* set channel mixing parameters */
45 avr->in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
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);
51 avr->out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
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);
57 avr->resample_channels = FFMIN(avr->in_channels, avr->out_channels);
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 ||
61 avr->in_channel_layout != avr->out_channel_layout));
62 avr->mixing_needed = avr->downmix_needed || avr->upmix_needed;
64 /* set resampling parameters */
65 avr->resample_needed = avr->in_sample_rate != avr->out_sample_rate ||
66 avr->force_resampling;
68 /* select internal sample format if not specified by the user */
69 if (avr->internal_sample_fmt == AV_SAMPLE_FMT_NONE &&
70 (avr->mixing_needed || avr->resample_needed)) {
71 enum AVSampleFormat in_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
72 enum AVSampleFormat out_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
73 int max_bps = FFMAX(av_get_bytes_per_sample(in_fmt),
74 av_get_bytes_per_sample(out_fmt));
76 avr->internal_sample_fmt = AV_SAMPLE_FMT_S16P;
77 } else if (avr->mixing_needed) {
78 avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
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 */
86 avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
88 /* if one is s32 and the other is s32, s16, or u8, use s32 */
89 avr->internal_sample_fmt = AV_SAMPLE_FMT_S32P;
92 /* if one is flt and the other is flt, s16 or u8, use flt */
93 avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
96 /* if either is dbl, use dbl */
97 avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
100 av_log(avr, AV_LOG_DEBUG, "Using %s as internal sample format\n",
101 av_get_sample_fmt_name(avr->internal_sample_fmt));
104 /* treat all mono as planar for easier comparison */
105 if (avr->in_channels == 1)
106 avr->in_sample_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
107 if (avr->out_channels == 1)
108 avr->out_sample_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
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 &&
113 !av_sample_fmt_is_planar(avr->out_sample_fmt)) {
114 avr->internal_sample_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
117 /* set sample format conversion parameters */
118 if (avr->resample_needed || avr->mixing_needed)
119 avr->in_convert_needed = avr->in_sample_fmt != avr->internal_sample_fmt;
121 avr->in_convert_needed = avr->use_channel_map &&
122 !av_sample_fmt_is_planar(avr->out_sample_fmt);
124 if (avr->resample_needed || avr->mixing_needed || avr->in_convert_needed)
125 avr->out_convert_needed = avr->internal_sample_fmt != avr->out_sample_fmt;
127 avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;
129 avr->in_copy_needed = !avr->in_convert_needed && (avr->mixing_needed ||
130 (avr->use_channel_map && avr->resample_needed));
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) {
137 avr->remap_point = REMAP_IN_CONVERT;
138 av_dlog(avr, "remap channels during in_convert\n");
139 } else if (avr->out_convert_needed) {
140 avr->remap_point = REMAP_OUT_CONVERT;
141 av_dlog(avr, "remap channels during out_convert\n");
143 avr->remap_point = REMAP_OUT_COPY;
144 av_dlog(avr, "remap channels during out_copy\n");
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]);
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]);
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]);
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]);
178 avr->remap_point = REMAP_NONE;
180 /* allocate buffers */
181 if (avr->in_copy_needed || avr->in_convert_needed) {
182 avr->in_buffer = ff_audio_data_alloc(FFMAX(avr->in_channels, avr->out_channels),
183 0, avr->internal_sample_fmt,
185 if (!avr->in_buffer) {
186 ret = AVERROR(EINVAL);
190 if (avr->resample_needed) {
191 avr->resample_out_buffer = ff_audio_data_alloc(avr->out_channels,
192 0, avr->internal_sample_fmt,
193 "resample_out_buffer");
194 if (!avr->resample_out_buffer) {
195 ret = AVERROR(EINVAL);
199 if (avr->out_convert_needed) {
200 avr->out_buffer = ff_audio_data_alloc(avr->out_channels, 0,
201 avr->out_sample_fmt, "out_buffer");
202 if (!avr->out_buffer) {
203 ret = AVERROR(EINVAL);
207 avr->out_fifo = av_audio_fifo_alloc(avr->out_sample_fmt, avr->out_channels,
209 if (!avr->out_fifo) {
210 ret = AVERROR(ENOMEM);
215 if (avr->in_convert_needed) {
216 avr->ac_in = ff_audio_convert_alloc(avr, avr->internal_sample_fmt,
217 avr->in_sample_fmt, avr->in_channels,
219 avr->remap_point == REMAP_IN_CONVERT);
221 ret = AVERROR(ENOMEM);
225 if (avr->out_convert_needed) {
226 enum AVSampleFormat src_fmt;
227 if (avr->in_convert_needed)
228 src_fmt = avr->internal_sample_fmt;
230 src_fmt = avr->in_sample_fmt;
231 avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
233 avr->out_sample_rate,
234 avr->remap_point == REMAP_OUT_CONVERT);
236 ret = AVERROR(ENOMEM);
240 if (avr->resample_needed) {
241 avr->resample = ff_audio_resample_init(avr);
242 if (!avr->resample) {
243 ret = AVERROR(ENOMEM);
247 if (avr->mixing_needed) {
248 avr->am = ff_audio_mix_alloc(avr);
250 ret = AVERROR(ENOMEM);
258 avresample_close(avr);
262 int avresample_is_open(AVAudioResampleContext *avr)
264 return !!avr->out_fifo;
267 void avresample_close(AVAudioResampleContext *avr)
269 ff_audio_data_free(&avr->in_buffer);
270 ff_audio_data_free(&avr->resample_out_buffer);
271 ff_audio_data_free(&avr->out_buffer);
272 av_audio_fifo_free(avr->out_fifo);
273 avr->out_fifo = NULL;
274 ff_audio_convert_free(&avr->ac_in);
275 ff_audio_convert_free(&avr->ac_out);
276 ff_audio_resample_free(&avr->resample);
277 ff_audio_mix_free(&avr->am);
278 av_freep(&avr->mix_matrix);
280 avr->use_channel_map = 0;
283 void avresample_free(AVAudioResampleContext **avr)
287 avresample_close(*avr);
292 static int handle_buffered_output(AVAudioResampleContext *avr,
293 AudioData *output, AudioData *converted)
297 if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
298 (converted && output->allocated_samples < converted->nb_samples)) {
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);
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);
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);
328 av_dlog(avr, "[end conversion]\n");
329 return output->nb_samples;
331 av_dlog(avr, "[end conversion]\n");
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)
340 AudioData input_buffer;
341 AudioData output_buffer;
342 AudioData *current_buffer;
343 int ret, direct_output;
345 /* reset internal buffers */
346 if (avr->in_buffer) {
347 avr->in_buffer->nb_samples = 0;
348 ff_audio_data_set_channels(avr->in_buffer,
349 avr->in_buffer->allocated_channels);
351 if (avr->resample_out_buffer) {
352 avr->resample_out_buffer->nb_samples = 0;
353 ff_audio_data_set_channels(avr->resample_out_buffer,
354 avr->resample_out_buffer->allocated_channels);
356 if (avr->out_buffer) {
357 avr->out_buffer->nb_samples = 0;
358 ff_audio_data_set_channels(avr->out_buffer,
359 avr->out_buffer->allocated_channels);
362 av_dlog(avr, "[start conversion]\n");
364 /* initialize output_buffer with output data */
365 direct_output = output && av_audio_fifo_size(avr->out_fifo) == 0;
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");
372 output_buffer.nb_samples = 0;
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");
382 current_buffer = &input_buffer;
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);
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,
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
409 if (avr->in_convert_needed) {
410 ret = ff_audio_data_realloc(avr->in_buffer,
411 current_buffer->nb_samples);
414 av_dlog(avr, "[convert] %s to in_buffer\n", current_buffer->name);
415 ret = ff_audio_convert(avr->ac_in, avr->in_buffer,
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);
427 ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);
428 if (avr->downmix_needed) {
429 av_dlog(avr, "[downmix] in_buffer\n");
430 ret = ff_audio_mix(avr->am, avr->in_buffer);
434 current_buffer = avr->in_buffer;
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,
441 current_buffer = NULL;
444 if (avr->resample_needed) {
445 AudioData *resample_out;
447 if (!avr->out_convert_needed && direct_output && out_samples > 0)
448 resample_out = &output_buffer;
450 resample_out = avr->resample_out_buffer;
451 av_dlog(avr, "[resample] %s to %s\n",
452 current_buffer ? current_buffer->name : "null",
454 ret = ff_audio_resample(avr->resample, resample_out,
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");
465 current_buffer = resample_out;
468 if (avr->upmix_needed) {
469 av_dlog(avr, "[upmix] %s\n", current_buffer->name);
470 ret = ff_audio_mix(avr->am, current_buffer);
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;
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);
489 av_dlog(avr, "[end conversion]\n");
490 return output_buffer.nb_samples;
492 ret = ff_audio_data_realloc(avr->out_buffer,
493 current_buffer->nb_samples);
496 av_dlog(avr, "[convert] %s to out_buffer\n", current_buffer->name);
497 ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
501 current_buffer = avr->out_buffer;
505 return handle_buffered_output(avr, output ? &output_buffer : NULL,
509 int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
512 int in_channels, out_channels, i, o;
515 return ff_audio_mix_get_matrix(avr->am, matrix, stride);
517 in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
518 out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
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);
526 if (!avr->mix_matrix) {
527 av_log(avr, AV_LOG_ERROR, "matrix is not set\n");
528 return AVERROR(EINVAL);
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];
538 int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
541 int in_channels, out_channels, i, o;
544 return ff_audio_mix_set_matrix(avr->am, matrix, stride);
546 in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
547 out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
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);
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);
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];
569 int avresample_set_channel_mapping(AVAudioResampleContext *avr,
570 const int *channel_map)
572 ChannelMapInfo *info = &avr->ch_map_info;
573 int in_channels, ch, i;
575 in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
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);
581 memset(info, 0, sizeof(*info));
582 memset(info->input_map, -1, sizeof(info->input_map));
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);
589 if (channel_map[ch] < 0) {
590 info->channel_zero[ch] = 1;
591 info->channel_map[ch] = -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;
598 info->channel_map[ch] = channel_map[ch];
599 info->input_map[channel_map[ch]] = ch;
603 /* Fill-in unmapped input channels with unmapped output channels.
604 This is used when remapping during conversion from interleaved to
606 for (ch = 0, i = 0; ch < in_channels && i < in_channels; ch++, i++) {
607 while (ch < in_channels && info->input_map[ch] >= 0)
609 while (i < in_channels && info->channel_map[i] >= 0)
611 if (ch >= in_channels || i >= in_channels)
613 info->input_map[ch] = i;
616 avr->use_channel_map = 1;
620 int avresample_available(AVAudioResampleContext *avr)
622 return av_audio_fifo_size(avr->out_fifo);
625 int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
628 return av_audio_fifo_drain(avr->out_fifo, nb_samples);
629 return av_audio_fifo_read(avr->out_fifo, (void**)output, nb_samples);
632 unsigned avresample_version(void)
634 return LIBAVRESAMPLE_VERSION_INT;
637 const char *avresample_license(void)
639 #define LICENSE_PREFIX "libavresample license: "
640 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
643 const char *avresample_configuration(void)
645 return FFMPEG_CONFIGURATION;