]> git.sesse.net Git - ffmpeg/blob - libavresample/utils.c
Add libavresample
[ffmpeg] / libavresample / utils.c
1 /*
2  * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20
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"
26
27 #include "avresample.h"
28 #include "audio_data.h"
29 #include "internal.h"
30
31 int avresample_open(AVAudioResampleContext *avr)
32 {
33     int ret;
34
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);
41     }
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);
47     }
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 ||
51                              avr->am->matrix                      ||
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;
55
56     /* set resampling parameters */
57     avr->resample_needed   = avr->in_sample_rate != avr->out_sample_rate ||
58                              avr->force_resampling;
59
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;
71     }
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;
80     else
81         avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;
82
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,
87                                              "in_buffer");
88         if (!avr->in_buffer) {
89             ret = AVERROR(EINVAL);
90             goto error;
91         }
92     }
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);
99             goto error;
100         }
101     }
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);
107             goto error;
108         }
109     }
110     avr->out_fifo = av_audio_fifo_alloc(avr->out_sample_fmt, avr->out_channels,
111                                         1024);
112     if (!avr->out_fifo) {
113         ret = AVERROR(ENOMEM);
114         goto error;
115     }
116
117     /* setup contexts */
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);
121         if (!avr->ac_in) {
122             ret = AVERROR(ENOMEM);
123             goto error;
124         }
125     }
126     if (avr->out_convert_needed) {
127         enum AVSampleFormat src_fmt;
128         if (avr->in_convert_needed)
129             src_fmt = avr->internal_sample_fmt;
130         else
131             src_fmt = avr->in_sample_fmt;
132         avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
133                                              avr->out_channels);
134         if (!avr->ac_out) {
135             ret = AVERROR(ENOMEM);
136             goto error;
137         }
138     }
139     if (avr->resample_needed) {
140         avr->resample = ff_audio_resample_init(avr);
141         if (!avr->resample) {
142             ret = AVERROR(ENOMEM);
143             goto error;
144         }
145     }
146     if (avr->mixing_needed) {
147         ret = ff_audio_mix_init(avr);
148         if (ret < 0)
149             goto error;
150     }
151
152     return 0;
153
154 error:
155     avresample_close(avr);
156     return ret;
157 }
158
159 void avresample_close(AVAudioResampleContext *avr)
160 {
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);
170     return;
171 }
172
173 void avresample_free(AVAudioResampleContext **avr)
174 {
175     if (!*avr)
176         return;
177     avresample_close(*avr);
178     av_freep(&(*avr)->am);
179     av_opt_free(*avr);
180     av_freep(avr);
181 }
182
183 static int handle_buffered_output(AVAudioResampleContext *avr,
184                                   AudioData *output, AudioData *converted)
185 {
186     int ret;
187
188     if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
189         (converted && output->allocated_samples < converted->nb_samples)) {
190         if (converted) {
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);
197             if (ret < 0)
198                 return ret;
199         }
200
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);
208         }
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);
215         if (ret < 0)
216             return ret;
217         av_dlog(avr, "[end conversion]\n");
218         return output->nb_samples;
219     }
220     av_dlog(avr, "[end conversion]\n");
221     return 0;
222 }
223
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)
227 {
228     AudioData input_buffer;
229     AudioData output_buffer;
230     AudioData *current_buffer;
231     int ret;
232
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);
238     }
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);
243     }
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);
248     }
249
250     av_dlog(avr, "[start conversion]\n");
251
252     /* initialize output_buffer with output data */
253     if (output) {
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");
257         if (ret < 0)
258             return ret;
259         output_buffer.nb_samples = 0;
260     }
261
262     if (input) {
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");
267         if (ret < 0)
268             return ret;
269         current_buffer = &input_buffer;
270
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);
277             if (ret < 0)
278                 return ret;
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
282                applicable */
283             if (avr->in_convert_needed) {
284                 ret = ff_audio_data_realloc(avr->in_buffer,
285                                             current_buffer->nb_samples);
286                 if (ret < 0)
287                     return ret;
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);
291                 if (ret < 0)
292                     return ret;
293             } else {
294                 av_dlog(avr, "[copy] %s to in_buffer\n", current_buffer->name);
295                 ret = ff_audio_data_copy(avr->in_buffer, current_buffer);
296                 if (ret < 0)
297                     return ret;
298             }
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);
303                 if (ret < 0)
304                     return ret;
305             }
306             current_buffer = avr->in_buffer;
307         }
308     } else {
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,
312                                           NULL);
313         current_buffer = NULL;
314     }
315
316     if (avr->resample_needed) {
317         AudioData *resample_out;
318         int consumed = 0;
319
320         if (!avr->out_convert_needed && output && out_samples > 0)
321             resample_out = &output_buffer;
322         else
323             resample_out = avr->resample_out_buffer;
324         av_dlog(avr, "[resample] %s to %s\n", current_buffer->name,
325                 resample_out->name);
326         ret = ff_audio_resample(avr->resample, resample_out,
327                                 current_buffer, &consumed);
328         if (ret < 0)
329             return ret;
330
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");
334             return 0;
335         }
336
337         current_buffer = resample_out;
338     }
339
340     if (avr->upmix_needed) {
341         av_dlog(avr, "[upmix] %s\n", current_buffer->name);
342         ret = ff_audio_mix(avr->am, current_buffer);
343         if (ret < 0)
344             return ret;
345     }
346
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;
351     }
352
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);
359             if (ret < 0)
360                 return ret;
361
362             av_dlog(avr, "[end conversion]\n");
363             return output_buffer.nb_samples;
364         } else {
365             ret = ff_audio_data_realloc(avr->out_buffer,
366                                         current_buffer->nb_samples);
367             if (ret < 0)
368                 return ret;
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);
372             if (ret < 0)
373                 return ret;
374             current_buffer = avr->out_buffer;
375         }
376     }
377
378     return handle_buffered_output(avr, &output_buffer, current_buffer);
379 }
380
381 int avresample_available(AVAudioResampleContext *avr)
382 {
383     return av_audio_fifo_size(avr->out_fifo);
384 }
385
386 int avresample_read(AVAudioResampleContext *avr, void **output, int nb_samples)
387 {
388     return av_audio_fifo_read(avr->out_fifo, output, nb_samples);
389 }
390
391 unsigned avresample_version(void)
392 {
393     return LIBAVRESAMPLE_VERSION_INT;
394 }
395
396 const char *avresample_license(void)
397 {
398 #define LICENSE_PREFIX "libavresample license: "
399     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
400 }
401
402 const char *avresample_configuration(void)
403 {
404     return LIBAV_CONFIGURATION;
405 }