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 /* set channel mixing parameters */
40 avr->in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
41 if (avr->in_channels <= 0 || avr->in_channels > AVRESAMPLE_MAX_CHANNELS) {
42 av_log(avr, AV_LOG_ERROR, "Invalid input channel layout: %"PRIu64"\n",
43 avr->in_channel_layout);
44 return AVERROR(EINVAL);
46 avr->out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
47 if (avr->out_channels <= 0 || avr->out_channels > AVRESAMPLE_MAX_CHANNELS) {
48 av_log(avr, AV_LOG_ERROR, "Invalid output channel layout: %"PRIu64"\n",
49 avr->out_channel_layout);
50 return AVERROR(EINVAL);
52 avr->resample_channels = FFMIN(avr->in_channels, avr->out_channels);
53 avr->downmix_needed = avr->in_channels > avr->out_channels;
54 avr->upmix_needed = avr->out_channels > avr->in_channels ||
55 (!avr->downmix_needed && (avr->mix_matrix ||
56 avr->in_channel_layout != avr->out_channel_layout));
57 avr->mixing_needed = avr->downmix_needed || avr->upmix_needed;
59 /* set resampling parameters */
60 avr->resample_needed = avr->in_sample_rate != avr->out_sample_rate ||
61 avr->force_resampling;
63 /* select internal sample format if not specified by the user */
64 if (avr->internal_sample_fmt == AV_SAMPLE_FMT_NONE &&
65 (avr->mixing_needed || avr->resample_needed)) {
66 enum AVSampleFormat in_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
67 enum AVSampleFormat out_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
68 int max_bps = FFMAX(av_get_bytes_per_sample(in_fmt),
69 av_get_bytes_per_sample(out_fmt));
71 avr->internal_sample_fmt = AV_SAMPLE_FMT_S16P;
72 } else if (avr->mixing_needed) {
73 avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
76 if (in_fmt == AV_SAMPLE_FMT_S32P ||
77 out_fmt == AV_SAMPLE_FMT_S32P) {
78 if (in_fmt == AV_SAMPLE_FMT_FLTP ||
79 out_fmt == AV_SAMPLE_FMT_FLTP) {
80 /* if one is s32 and the other is flt, use dbl */
81 avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
83 /* if one is s32 and the other is s32, s16, or u8, use s32 */
84 avr->internal_sample_fmt = AV_SAMPLE_FMT_S32P;
87 /* if one is flt and the other is flt, s16 or u8, use flt */
88 avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
91 /* if either is dbl, use dbl */
92 avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
95 av_log(avr, AV_LOG_DEBUG, "Using %s as internal sample format\n",
96 av_get_sample_fmt_name(avr->internal_sample_fmt));
99 /* treat all mono as planar for easier comparison */
100 if (avr->in_channels == 1)
101 avr->in_sample_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
102 if (avr->out_channels == 1)
103 avr->out_sample_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
105 /* we may need to add an extra conversion in order to remap channels if
106 the output format is not planar */
107 if (avr->use_channel_map && !avr->mixing_needed && !avr->resample_needed &&
108 !av_sample_fmt_is_planar(avr->out_sample_fmt)) {
109 avr->internal_sample_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
112 /* set sample format conversion parameters */
113 if (avr->resample_needed || avr->mixing_needed)
114 avr->in_convert_needed = avr->in_sample_fmt != avr->internal_sample_fmt;
116 avr->in_convert_needed = avr->use_channel_map &&
117 !av_sample_fmt_is_planar(avr->out_sample_fmt);
119 if (avr->resample_needed || avr->mixing_needed || avr->in_convert_needed)
120 avr->out_convert_needed = avr->internal_sample_fmt != avr->out_sample_fmt;
122 avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;
124 avr->in_copy_needed = !avr->in_convert_needed && (avr->mixing_needed ||
125 (avr->use_channel_map && avr->resample_needed));
127 if (avr->use_channel_map) {
128 if (avr->in_copy_needed) {
129 avr->remap_point = REMAP_IN_COPY;
130 av_dlog(avr, "remap channels during in_copy\n");
131 } else if (avr->in_convert_needed) {
132 avr->remap_point = REMAP_IN_CONVERT;
133 av_dlog(avr, "remap channels during in_convert\n");
134 } else if (avr->out_convert_needed) {
135 avr->remap_point = REMAP_OUT_CONVERT;
136 av_dlog(avr, "remap channels during out_convert\n");
138 avr->remap_point = REMAP_OUT_COPY;
139 av_dlog(avr, "remap channels during out_copy\n");
145 av_dlog(avr, "output map: ");
146 if (avr->ch_map_info.do_remap)
147 for (ch = 0; ch < avr->in_channels; ch++)
148 av_dlog(avr, " % 2d", avr->ch_map_info.channel_map[ch]);
152 av_dlog(avr, "copy map: ");
153 if (avr->ch_map_info.do_copy)
154 for (ch = 0; ch < avr->in_channels; ch++)
155 av_dlog(avr, " % 2d", avr->ch_map_info.channel_copy[ch]);
159 av_dlog(avr, "zero map: ");
160 if (avr->ch_map_info.do_zero)
161 for (ch = 0; ch < avr->in_channels; ch++)
162 av_dlog(avr, " % 2d", avr->ch_map_info.channel_zero[ch]);
166 av_dlog(avr, "input map: ");
167 for (ch = 0; ch < avr->in_channels; ch++)
168 av_dlog(avr, " % 2d", avr->ch_map_info.input_map[ch]);
173 avr->remap_point = REMAP_NONE;
175 /* allocate buffers */
176 if (avr->in_copy_needed || avr->in_convert_needed) {
177 avr->in_buffer = ff_audio_data_alloc(FFMAX(avr->in_channels, avr->out_channels),
178 0, avr->internal_sample_fmt,
180 if (!avr->in_buffer) {
181 ret = AVERROR(EINVAL);
185 if (avr->resample_needed) {
186 avr->resample_out_buffer = ff_audio_data_alloc(avr->out_channels,
187 0, avr->internal_sample_fmt,
188 "resample_out_buffer");
189 if (!avr->resample_out_buffer) {
190 ret = AVERROR(EINVAL);
194 if (avr->out_convert_needed) {
195 avr->out_buffer = ff_audio_data_alloc(avr->out_channels, 0,
196 avr->out_sample_fmt, "out_buffer");
197 if (!avr->out_buffer) {
198 ret = AVERROR(EINVAL);
202 avr->out_fifo = av_audio_fifo_alloc(avr->out_sample_fmt, avr->out_channels,
204 if (!avr->out_fifo) {
205 ret = AVERROR(ENOMEM);
210 if (avr->in_convert_needed) {
211 avr->ac_in = ff_audio_convert_alloc(avr, avr->internal_sample_fmt,
212 avr->in_sample_fmt, avr->in_channels,
214 avr->remap_point == REMAP_IN_CONVERT);
216 ret = AVERROR(ENOMEM);
220 if (avr->out_convert_needed) {
221 enum AVSampleFormat src_fmt;
222 if (avr->in_convert_needed)
223 src_fmt = avr->internal_sample_fmt;
225 src_fmt = avr->in_sample_fmt;
226 avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
228 avr->out_sample_rate,
229 avr->remap_point == REMAP_OUT_CONVERT);
231 ret = AVERROR(ENOMEM);
235 if (avr->resample_needed) {
236 avr->resample = ff_audio_resample_init(avr);
237 if (!avr->resample) {
238 ret = AVERROR(ENOMEM);
242 if (avr->mixing_needed) {
243 avr->am = ff_audio_mix_alloc(avr);
245 ret = AVERROR(ENOMEM);
253 avresample_close(avr);
257 void avresample_close(AVAudioResampleContext *avr)
259 ff_audio_data_free(&avr->in_buffer);
260 ff_audio_data_free(&avr->resample_out_buffer);
261 ff_audio_data_free(&avr->out_buffer);
262 av_audio_fifo_free(avr->out_fifo);
263 avr->out_fifo = NULL;
264 ff_audio_convert_free(&avr->ac_in);
265 ff_audio_convert_free(&avr->ac_out);
266 ff_audio_resample_free(&avr->resample);
267 ff_audio_mix_free(&avr->am);
268 av_freep(&avr->mix_matrix);
270 avr->use_channel_map = 0;
273 void avresample_free(AVAudioResampleContext **avr)
277 avresample_close(*avr);
282 static int handle_buffered_output(AVAudioResampleContext *avr,
283 AudioData *output, AudioData *converted)
287 if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
288 (converted && output->allocated_samples < converted->nb_samples)) {
290 /* if there are any samples in the output FIFO or if the
291 user-supplied output buffer is not large enough for all samples,
292 we add to the output FIFO */
293 av_dlog(avr, "[FIFO] add %s to out_fifo\n", converted->name);
294 ret = ff_audio_data_add_to_fifo(avr->out_fifo, converted, 0,
295 converted->nb_samples);
300 /* if the user specified an output buffer, read samples from the output
301 FIFO to the user output */
302 if (output && output->allocated_samples > 0) {
303 av_dlog(avr, "[FIFO] read from out_fifo to output\n");
304 av_dlog(avr, "[end conversion]\n");
305 return ff_audio_data_read_from_fifo(avr->out_fifo, output,
306 output->allocated_samples);
308 } else if (converted) {
309 /* copy directly to output if it is large enough or there is not any
310 data in the output FIFO */
311 av_dlog(avr, "[copy] %s to output\n", converted->name);
312 output->nb_samples = 0;
313 ret = ff_audio_data_copy(output, converted,
314 avr->remap_point == REMAP_OUT_COPY ?
315 &avr->ch_map_info : NULL);
318 av_dlog(avr, "[end conversion]\n");
319 return output->nb_samples;
321 av_dlog(avr, "[end conversion]\n");
325 int attribute_align_arg avresample_convert(AVAudioResampleContext *avr,
326 uint8_t **output, int out_plane_size,
327 int out_samples, uint8_t **input,
328 int in_plane_size, int in_samples)
330 AudioData input_buffer;
331 AudioData output_buffer;
332 AudioData *current_buffer;
333 int ret, direct_output;
335 /* reset internal buffers */
336 if (avr->in_buffer) {
337 avr->in_buffer->nb_samples = 0;
338 ff_audio_data_set_channels(avr->in_buffer,
339 avr->in_buffer->allocated_channels);
341 if (avr->resample_out_buffer) {
342 avr->resample_out_buffer->nb_samples = 0;
343 ff_audio_data_set_channels(avr->resample_out_buffer,
344 avr->resample_out_buffer->allocated_channels);
346 if (avr->out_buffer) {
347 avr->out_buffer->nb_samples = 0;
348 ff_audio_data_set_channels(avr->out_buffer,
349 avr->out_buffer->allocated_channels);
352 av_dlog(avr, "[start conversion]\n");
354 /* initialize output_buffer with output data */
355 direct_output = output && av_audio_fifo_size(avr->out_fifo) == 0;
357 ret = ff_audio_data_init(&output_buffer, output, out_plane_size,
358 avr->out_channels, out_samples,
359 avr->out_sample_fmt, 0, "output");
362 output_buffer.nb_samples = 0;
366 /* initialize input_buffer with input data */
367 ret = ff_audio_data_init(&input_buffer, input, in_plane_size,
368 avr->in_channels, in_samples,
369 avr->in_sample_fmt, 1, "input");
372 current_buffer = &input_buffer;
374 if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&
375 !avr->out_convert_needed && direct_output && out_samples >= in_samples) {
376 /* in some rare cases we can copy input to output and upmix
377 directly in the output buffer */
378 av_dlog(avr, "[copy] %s to output\n", current_buffer->name);
379 ret = ff_audio_data_copy(&output_buffer, current_buffer,
380 avr->remap_point == REMAP_OUT_COPY ?
381 &avr->ch_map_info : NULL);
384 current_buffer = &output_buffer;
385 } else if (avr->remap_point == REMAP_OUT_COPY &&
386 (!direct_output || out_samples < in_samples)) {
387 /* if remapping channels during output copy, we may need to
388 * use an intermediate buffer in order to remap before adding
389 * samples to the output fifo */
390 av_dlog(avr, "[copy] %s to out_buffer\n", current_buffer->name);
391 ret = ff_audio_data_copy(avr->out_buffer, current_buffer,
395 current_buffer = avr->out_buffer;
396 } else if (avr->in_copy_needed || avr->in_convert_needed) {
397 /* if needed, copy or convert input to in_buffer, and downmix if
399 if (avr->in_convert_needed) {
400 ret = ff_audio_data_realloc(avr->in_buffer,
401 current_buffer->nb_samples);
404 av_dlog(avr, "[convert] %s to in_buffer\n", current_buffer->name);
405 ret = ff_audio_convert(avr->ac_in, avr->in_buffer,
410 av_dlog(avr, "[copy] %s to in_buffer\n", current_buffer->name);
411 ret = ff_audio_data_copy(avr->in_buffer, current_buffer,
412 avr->remap_point == REMAP_IN_COPY ?
413 &avr->ch_map_info : NULL);
417 ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);
418 if (avr->downmix_needed) {
419 av_dlog(avr, "[downmix] in_buffer\n");
420 ret = ff_audio_mix(avr->am, avr->in_buffer);
424 current_buffer = avr->in_buffer;
427 /* flush resampling buffer and/or output FIFO if input is NULL */
428 if (!avr->resample_needed)
429 return handle_buffered_output(avr, output ? &output_buffer : NULL,
431 current_buffer = NULL;
434 if (avr->resample_needed) {
435 AudioData *resample_out;
437 if (!avr->out_convert_needed && direct_output && out_samples > 0)
438 resample_out = &output_buffer;
440 resample_out = avr->resample_out_buffer;
441 av_dlog(avr, "[resample] %s to %s\n",
442 current_buffer ? current_buffer->name : "null",
444 ret = ff_audio_resample(avr->resample, resample_out,
449 /* if resampling did not produce any samples, just return 0 */
450 if (resample_out->nb_samples == 0) {
451 av_dlog(avr, "[end conversion]\n");
455 current_buffer = resample_out;
458 if (avr->upmix_needed) {
459 av_dlog(avr, "[upmix] %s\n", current_buffer->name);
460 ret = ff_audio_mix(avr->am, current_buffer);
465 /* if we resampled or upmixed directly to output, return here */
466 if (current_buffer == &output_buffer) {
467 av_dlog(avr, "[end conversion]\n");
468 return current_buffer->nb_samples;
471 if (avr->out_convert_needed) {
472 if (direct_output && out_samples >= current_buffer->nb_samples) {
473 /* convert directly to output */
474 av_dlog(avr, "[convert] %s to output\n", current_buffer->name);
475 ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer);
479 av_dlog(avr, "[end conversion]\n");
480 return output_buffer.nb_samples;
482 ret = ff_audio_data_realloc(avr->out_buffer,
483 current_buffer->nb_samples);
486 av_dlog(avr, "[convert] %s to out_buffer\n", current_buffer->name);
487 ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
491 current_buffer = avr->out_buffer;
495 return handle_buffered_output(avr, output ? &output_buffer : NULL,
499 int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
502 int in_channels, out_channels, i, o;
505 return ff_audio_mix_get_matrix(avr->am, matrix, stride);
507 in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
508 out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
510 if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
511 out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
512 av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
513 return AVERROR(EINVAL);
516 if (!avr->mix_matrix) {
517 av_log(avr, AV_LOG_ERROR, "matrix is not set\n");
518 return AVERROR(EINVAL);
521 for (o = 0; o < out_channels; o++)
522 for (i = 0; i < in_channels; i++)
523 matrix[o * stride + i] = avr->mix_matrix[o * in_channels + i];
528 int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
531 int in_channels, out_channels, i, o;
534 return ff_audio_mix_set_matrix(avr->am, matrix, stride);
536 in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
537 out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
539 if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
540 out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
541 av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
542 return AVERROR(EINVAL);
546 av_freep(&avr->mix_matrix);
547 avr->mix_matrix = av_malloc(in_channels * out_channels *
548 sizeof(*avr->mix_matrix));
549 if (!avr->mix_matrix)
550 return AVERROR(ENOMEM);
552 for (o = 0; o < out_channels; o++)
553 for (i = 0; i < in_channels; i++)
554 avr->mix_matrix[o * in_channels + i] = matrix[o * stride + i];
559 int avresample_set_channel_mapping(AVAudioResampleContext *avr,
560 const int *channel_map)
562 ChannelMapInfo *info = &avr->ch_map_info;
563 int in_channels, ch, i;
565 in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
566 if (in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS) {
567 av_log(avr, AV_LOG_ERROR, "Invalid input channel layout\n");
568 return AVERROR(EINVAL);
571 memset(info, 0, sizeof(*info));
572 memset(info->input_map, -1, sizeof(info->input_map));
574 for (ch = 0; ch < in_channels; ch++) {
575 if (channel_map[ch] >= in_channels) {
576 av_log(avr, AV_LOG_ERROR, "Invalid channel map\n");
577 return AVERROR(EINVAL);
579 if (channel_map[ch] < 0) {
580 info->channel_zero[ch] = 1;
581 info->channel_map[ch] = -1;
583 } else if (info->input_map[channel_map[ch]] >= 0) {
584 info->channel_copy[ch] = info->input_map[channel_map[ch]];
585 info->channel_map[ch] = -1;
588 info->channel_map[ch] = channel_map[ch];
589 info->input_map[channel_map[ch]] = ch;
593 /* Fill-in unmapped input channels with unmapped output channels.
594 This is used when remapping during conversion from interleaved to
596 for (ch = 0, i = 0; ch < in_channels && i < in_channels; ch++, i++) {
597 while (ch < in_channels && info->input_map[ch] >= 0)
599 while (i < in_channels && info->channel_map[i] >= 0)
601 if (ch >= in_channels || i >= in_channels)
603 info->input_map[ch] = i;
606 avr->use_channel_map = 1;
610 int avresample_available(AVAudioResampleContext *avr)
612 return av_audio_fifo_size(avr->out_fifo);
615 int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
618 return av_audio_fifo_drain(avr->out_fifo, nb_samples);
619 return av_audio_fifo_read(avr->out_fifo, (void**)output, nb_samples);
622 unsigned avresample_version(void)
624 return LIBAVRESAMPLE_VERSION_INT;
627 const char *avresample_license(void)
629 #define LICENSE_PREFIX "libavresample license: "
630 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
633 const char *avresample_configuration(void)
635 return FFMPEG_CONFIGURATION;