2 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/dict.h"
22 #include "libavutil/error.h"
23 #include "libavutil/log.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/opt.h"
27 #include "avresample.h"
28 #include "audio_data.h"
31 int avresample_open(AVAudioResampleContext *avr)
35 /* set channel mixing parameters */
36 avr->in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
37 if (avr->in_channels <= 0 || avr->in_channels > AVRESAMPLE_MAX_CHANNELS) {
38 av_log(avr, AV_LOG_ERROR, "Invalid input channel layout: %"PRIu64"\n",
39 avr->in_channel_layout);
40 return AVERROR(EINVAL);
42 avr->out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
43 if (avr->out_channels <= 0 || avr->out_channels > AVRESAMPLE_MAX_CHANNELS) {
44 av_log(avr, AV_LOG_ERROR, "Invalid output channel layout: %"PRIu64"\n",
45 avr->out_channel_layout);
46 return AVERROR(EINVAL);
48 avr->resample_channels = FFMIN(avr->in_channels, avr->out_channels);
49 avr->downmix_needed = avr->in_channels > avr->out_channels;
50 avr->upmix_needed = avr->out_channels > avr->in_channels ||
52 (avr->out_channels == avr->in_channels &&
53 avr->in_channel_layout != avr->out_channel_layout);
54 avr->mixing_needed = avr->downmix_needed || avr->upmix_needed;
56 /* set resampling parameters */
57 avr->resample_needed = avr->in_sample_rate != avr->out_sample_rate ||
58 avr->force_resampling;
60 /* set sample format conversion parameters */
61 /* override user-requested internal format to avoid unexpected failures
62 TODO: support more internal formats */
63 if (avr->resample_needed && avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P) {
64 av_log(avr, AV_LOG_WARNING, "Using s16p as internal sample format\n");
65 avr->internal_sample_fmt = AV_SAMPLE_FMT_S16P;
66 } else if (avr->mixing_needed &&
67 avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P &&
68 avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP) {
69 av_log(avr, AV_LOG_WARNING, "Using fltp as internal sample format\n");
70 avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
72 if (avr->in_channels == 1)
73 avr->in_sample_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
74 if (avr->out_channels == 1)
75 avr->out_sample_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
76 avr->in_convert_needed = (avr->resample_needed || avr->mixing_needed) &&
77 avr->in_sample_fmt != avr->internal_sample_fmt;
78 if (avr->resample_needed || avr->mixing_needed)
79 avr->out_convert_needed = avr->internal_sample_fmt != avr->out_sample_fmt;
81 avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;
83 /* allocate buffers */
84 if (avr->mixing_needed || avr->in_convert_needed) {
85 avr->in_buffer = ff_audio_data_alloc(FFMAX(avr->in_channels, avr->out_channels),
86 0, avr->internal_sample_fmt,
88 if (!avr->in_buffer) {
89 ret = AVERROR(EINVAL);
93 if (avr->resample_needed) {
94 avr->resample_out_buffer = ff_audio_data_alloc(avr->out_channels,
95 0, avr->internal_sample_fmt,
96 "resample_out_buffer");
97 if (!avr->resample_out_buffer) {
98 ret = AVERROR(EINVAL);
102 if (avr->out_convert_needed) {
103 avr->out_buffer = ff_audio_data_alloc(avr->out_channels, 0,
104 avr->out_sample_fmt, "out_buffer");
105 if (!avr->out_buffer) {
106 ret = AVERROR(EINVAL);
110 avr->out_fifo = av_audio_fifo_alloc(avr->out_sample_fmt, avr->out_channels,
112 if (!avr->out_fifo) {
113 ret = AVERROR(ENOMEM);
118 if (avr->in_convert_needed) {
119 avr->ac_in = ff_audio_convert_alloc(avr, avr->internal_sample_fmt,
120 avr->in_sample_fmt, avr->in_channels);
122 ret = AVERROR(ENOMEM);
126 if (avr->out_convert_needed) {
127 enum AVSampleFormat src_fmt;
128 if (avr->in_convert_needed)
129 src_fmt = avr->internal_sample_fmt;
131 src_fmt = avr->in_sample_fmt;
132 avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
135 ret = AVERROR(ENOMEM);
139 if (avr->resample_needed) {
140 avr->resample = ff_audio_resample_init(avr);
141 if (!avr->resample) {
142 ret = AVERROR(ENOMEM);
146 if (avr->mixing_needed) {
147 ret = ff_audio_mix_init(avr);
155 avresample_close(avr);
159 void avresample_close(AVAudioResampleContext *avr)
161 ff_audio_data_free(&avr->in_buffer);
162 ff_audio_data_free(&avr->resample_out_buffer);
163 ff_audio_data_free(&avr->out_buffer);
164 av_audio_fifo_free(avr->out_fifo);
165 avr->out_fifo = NULL;
166 av_freep(&avr->ac_in);
167 av_freep(&avr->ac_out);
168 ff_audio_resample_free(&avr->resample);
169 ff_audio_mix_close(avr->am);
173 void avresample_free(AVAudioResampleContext **avr)
177 avresample_close(*avr);
178 av_freep(&(*avr)->am);
183 static int handle_buffered_output(AVAudioResampleContext *avr,
184 AudioData *output, AudioData *converted)
188 if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
189 (converted && output->allocated_samples < converted->nb_samples)) {
191 /* if there are any samples in the output FIFO or if the
192 user-supplied output buffer is not large enough for all samples,
193 we add to the output FIFO */
194 av_dlog(avr, "[FIFO] add %s to out_fifo\n", converted->name);
195 ret = ff_audio_data_add_to_fifo(avr->out_fifo, converted, 0,
196 converted->nb_samples);
201 /* if the user specified an output buffer, read samples from the output
202 FIFO to the user output */
203 if (output && output->allocated_samples > 0) {
204 av_dlog(avr, "[FIFO] read from out_fifo to output\n");
205 av_dlog(avr, "[end conversion]\n");
206 return ff_audio_data_read_from_fifo(avr->out_fifo, output,
207 output->allocated_samples);
209 } else if (converted) {
210 /* copy directly to output if it is large enough or there is not any
211 data in the output FIFO */
212 av_dlog(avr, "[copy] %s to output\n", converted->name);
213 output->nb_samples = 0;
214 ret = ff_audio_data_copy(output, converted);
217 av_dlog(avr, "[end conversion]\n");
218 return output->nb_samples;
220 av_dlog(avr, "[end conversion]\n");
224 int avresample_convert(AVAudioResampleContext *avr, void **output,
225 int out_plane_size, int out_samples, void **input,
226 int in_plane_size, int in_samples)
228 AudioData input_buffer;
229 AudioData output_buffer;
230 AudioData *current_buffer;
233 /* reset internal buffers */
234 if (avr->in_buffer) {
235 avr->in_buffer->nb_samples = 0;
236 ff_audio_data_set_channels(avr->in_buffer,
237 avr->in_buffer->allocated_channels);
239 if (avr->resample_out_buffer) {
240 avr->resample_out_buffer->nb_samples = 0;
241 ff_audio_data_set_channels(avr->resample_out_buffer,
242 avr->resample_out_buffer->allocated_channels);
244 if (avr->out_buffer) {
245 avr->out_buffer->nb_samples = 0;
246 ff_audio_data_set_channels(avr->out_buffer,
247 avr->out_buffer->allocated_channels);
250 av_dlog(avr, "[start conversion]\n");
252 /* initialize output_buffer with output data */
254 ret = ff_audio_data_init(&output_buffer, output, out_plane_size,
255 avr->out_channels, out_samples,
256 avr->out_sample_fmt, 0, "output");
259 output_buffer.nb_samples = 0;
263 /* initialize input_buffer with input data */
264 ret = ff_audio_data_init(&input_buffer, input, in_plane_size,
265 avr->in_channels, in_samples,
266 avr->in_sample_fmt, 1, "input");
269 current_buffer = &input_buffer;
271 if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&
272 !avr->out_convert_needed && output && out_samples >= in_samples) {
273 /* in some rare cases we can copy input to output and upmix
274 directly in the output buffer */
275 av_dlog(avr, "[copy] %s to output\n", current_buffer->name);
276 ret = ff_audio_data_copy(&output_buffer, current_buffer);
279 current_buffer = &output_buffer;
280 } else if (avr->mixing_needed || avr->in_convert_needed) {
281 /* if needed, copy or convert input to in_buffer, and downmix if
283 if (avr->in_convert_needed) {
284 ret = ff_audio_data_realloc(avr->in_buffer,
285 current_buffer->nb_samples);
288 av_dlog(avr, "[convert] %s to in_buffer\n", current_buffer->name);
289 ret = ff_audio_convert(avr->ac_in, avr->in_buffer, current_buffer,
290 current_buffer->nb_samples);
294 av_dlog(avr, "[copy] %s to in_buffer\n", current_buffer->name);
295 ret = ff_audio_data_copy(avr->in_buffer, current_buffer);
299 ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);
300 if (avr->downmix_needed) {
301 av_dlog(avr, "[downmix] in_buffer\n");
302 ret = ff_audio_mix(avr->am, avr->in_buffer);
306 current_buffer = avr->in_buffer;
309 /* flush resampling buffer and/or output FIFO if input is NULL */
310 if (!avr->resample_needed)
311 return handle_buffered_output(avr, output ? &output_buffer : NULL,
313 current_buffer = NULL;
316 if (avr->resample_needed) {
317 AudioData *resample_out;
320 if (!avr->out_convert_needed && output && out_samples > 0)
321 resample_out = &output_buffer;
323 resample_out = avr->resample_out_buffer;
324 av_dlog(avr, "[resample] %s to %s\n", current_buffer->name,
326 ret = ff_audio_resample(avr->resample, resample_out,
327 current_buffer, &consumed);
331 /* if resampling did not produce any samples, just return 0 */
332 if (resample_out->nb_samples == 0) {
333 av_dlog(avr, "[end conversion]\n");
337 current_buffer = resample_out;
340 if (avr->upmix_needed) {
341 av_dlog(avr, "[upmix] %s\n", current_buffer->name);
342 ret = ff_audio_mix(avr->am, current_buffer);
347 /* if we resampled or upmixed directly to output, return here */
348 if (current_buffer == &output_buffer) {
349 av_dlog(avr, "[end conversion]\n");
350 return current_buffer->nb_samples;
353 if (avr->out_convert_needed) {
354 if (output && out_samples >= current_buffer->nb_samples) {
355 /* convert directly to output */
356 av_dlog(avr, "[convert] %s to output\n", current_buffer->name);
357 ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer,
358 current_buffer->nb_samples);
362 av_dlog(avr, "[end conversion]\n");
363 return output_buffer.nb_samples;
365 ret = ff_audio_data_realloc(avr->out_buffer,
366 current_buffer->nb_samples);
369 av_dlog(avr, "[convert] %s to out_buffer\n", current_buffer->name);
370 ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
371 current_buffer, current_buffer->nb_samples);
374 current_buffer = avr->out_buffer;
378 return handle_buffered_output(avr, &output_buffer, current_buffer);
381 int avresample_available(AVAudioResampleContext *avr)
383 return av_audio_fifo_size(avr->out_fifo);
386 int avresample_read(AVAudioResampleContext *avr, void **output, int nb_samples)
388 return av_audio_fifo_read(avr->out_fifo, output, nb_samples);
391 unsigned avresample_version(void)
393 return LIBAVRESAMPLE_VERSION_INT;
396 const char *avresample_license(void)
398 #define LICENSE_PREFIX "libavresample license: "
399 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
402 const char *avresample_configuration(void)
404 return LIBAV_CONFIGURATION;