lavr: Use AV_SAMPLE_FMT_NONE to auto-select the internal sample format
[ffmpeg.git] / 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     /* select internal sample format if not specified by the user */
61     if (avr->internal_sample_fmt == AV_SAMPLE_FMT_NONE &&
62         (avr->mixing_needed || avr->resample_needed)) {
63         enum AVSampleFormat  in_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
64         enum AVSampleFormat out_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
65         int max_bps = FFMAX(av_get_bytes_per_sample(in_fmt),
66                             av_get_bytes_per_sample(out_fmt));
67         if (avr->resample_needed || max_bps <= 2) {
68             avr->internal_sample_fmt = AV_SAMPLE_FMT_S16P;
69         } else if (avr->mixing_needed) {
70             avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
71         }
72         av_log(avr, AV_LOG_DEBUG, "Using %s as internal sample format\n",
73                av_get_sample_fmt_name(avr->internal_sample_fmt));
74     }
75
76     /* set sample format conversion parameters */
77     if (avr->in_channels == 1)
78         avr->in_sample_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
79     if (avr->out_channels == 1)
80         avr->out_sample_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
81     avr->in_convert_needed = (avr->resample_needed || avr->mixing_needed) &&
82                               avr->in_sample_fmt != avr->internal_sample_fmt;
83     if (avr->resample_needed || avr->mixing_needed)
84         avr->out_convert_needed = avr->internal_sample_fmt != avr->out_sample_fmt;
85     else
86         avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;
87
88     /* allocate buffers */
89     if (avr->mixing_needed || avr->in_convert_needed) {
90         avr->in_buffer = ff_audio_data_alloc(FFMAX(avr->in_channels, avr->out_channels),
91                                              0, avr->internal_sample_fmt,
92                                              "in_buffer");
93         if (!avr->in_buffer) {
94             ret = AVERROR(EINVAL);
95             goto error;
96         }
97     }
98     if (avr->resample_needed) {
99         avr->resample_out_buffer = ff_audio_data_alloc(avr->out_channels,
100                                                        0, avr->internal_sample_fmt,
101                                                        "resample_out_buffer");
102         if (!avr->resample_out_buffer) {
103             ret = AVERROR(EINVAL);
104             goto error;
105         }
106     }
107     if (avr->out_convert_needed) {
108         avr->out_buffer = ff_audio_data_alloc(avr->out_channels, 0,
109                                               avr->out_sample_fmt, "out_buffer");
110         if (!avr->out_buffer) {
111             ret = AVERROR(EINVAL);
112             goto error;
113         }
114     }
115     avr->out_fifo = av_audio_fifo_alloc(avr->out_sample_fmt, avr->out_channels,
116                                         1024);
117     if (!avr->out_fifo) {
118         ret = AVERROR(ENOMEM);
119         goto error;
120     }
121
122     /* setup contexts */
123     if (avr->in_convert_needed) {
124         avr->ac_in = ff_audio_convert_alloc(avr, avr->internal_sample_fmt,
125                                             avr->in_sample_fmt, avr->in_channels);
126         if (!avr->ac_in) {
127             ret = AVERROR(ENOMEM);
128             goto error;
129         }
130     }
131     if (avr->out_convert_needed) {
132         enum AVSampleFormat src_fmt;
133         if (avr->in_convert_needed)
134             src_fmt = avr->internal_sample_fmt;
135         else
136             src_fmt = avr->in_sample_fmt;
137         avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
138                                              avr->out_channels);
139         if (!avr->ac_out) {
140             ret = AVERROR(ENOMEM);
141             goto error;
142         }
143     }
144     if (avr->resample_needed) {
145         avr->resample = ff_audio_resample_init(avr);
146         if (!avr->resample) {
147             ret = AVERROR(ENOMEM);
148             goto error;
149         }
150     }
151     if (avr->mixing_needed) {
152         ret = ff_audio_mix_init(avr);
153         if (ret < 0)
154             goto error;
155     }
156
157     return 0;
158
159 error:
160     avresample_close(avr);
161     return ret;
162 }
163
164 void avresample_close(AVAudioResampleContext *avr)
165 {
166     ff_audio_data_free(&avr->in_buffer);
167     ff_audio_data_free(&avr->resample_out_buffer);
168     ff_audio_data_free(&avr->out_buffer);
169     av_audio_fifo_free(avr->out_fifo);
170     avr->out_fifo = NULL;
171     av_freep(&avr->ac_in);
172     av_freep(&avr->ac_out);
173     ff_audio_resample_free(&avr->resample);
174     ff_audio_mix_close(avr->am);
175     return;
176 }
177
178 void avresample_free(AVAudioResampleContext **avr)
179 {
180     if (!*avr)
181         return;
182     avresample_close(*avr);
183     av_freep(&(*avr)->am);
184     av_opt_free(*avr);
185     av_freep(avr);
186 }
187
188 static int handle_buffered_output(AVAudioResampleContext *avr,
189                                   AudioData *output, AudioData *converted)
190 {
191     int ret;
192
193     if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
194         (converted && output->allocated_samples < converted->nb_samples)) {
195         if (converted) {
196             /* if there are any samples in the output FIFO or if the
197                user-supplied output buffer is not large enough for all samples,
198                we add to the output FIFO */
199             av_dlog(avr, "[FIFO] add %s to out_fifo\n", converted->name);
200             ret = ff_audio_data_add_to_fifo(avr->out_fifo, converted, 0,
201                                             converted->nb_samples);
202             if (ret < 0)
203                 return ret;
204         }
205
206         /* if the user specified an output buffer, read samples from the output
207            FIFO to the user output */
208         if (output && output->allocated_samples > 0) {
209             av_dlog(avr, "[FIFO] read from out_fifo to output\n");
210             av_dlog(avr, "[end conversion]\n");
211             return ff_audio_data_read_from_fifo(avr->out_fifo, output,
212                                                 output->allocated_samples);
213         }
214     } else if (converted) {
215         /* copy directly to output if it is large enough or there is not any
216            data in the output FIFO */
217         av_dlog(avr, "[copy] %s to output\n", converted->name);
218         output->nb_samples = 0;
219         ret = ff_audio_data_copy(output, converted);
220         if (ret < 0)
221             return ret;
222         av_dlog(avr, "[end conversion]\n");
223         return output->nb_samples;
224     }
225     av_dlog(avr, "[end conversion]\n");
226     return 0;
227 }
228
229 int avresample_convert(AVAudioResampleContext *avr, void **output,
230                        int out_plane_size, int out_samples, void **input,
231                        int in_plane_size, int in_samples)
232 {
233     AudioData input_buffer;
234     AudioData output_buffer;
235     AudioData *current_buffer;
236     int ret;
237
238     /* reset internal buffers */
239     if (avr->in_buffer) {
240         avr->in_buffer->nb_samples = 0;
241         ff_audio_data_set_channels(avr->in_buffer,
242                                    avr->in_buffer->allocated_channels);
243     }
244     if (avr->resample_out_buffer) {
245         avr->resample_out_buffer->nb_samples = 0;
246         ff_audio_data_set_channels(avr->resample_out_buffer,
247                                    avr->resample_out_buffer->allocated_channels);
248     }
249     if (avr->out_buffer) {
250         avr->out_buffer->nb_samples = 0;
251         ff_audio_data_set_channels(avr->out_buffer,
252                                    avr->out_buffer->allocated_channels);
253     }
254
255     av_dlog(avr, "[start conversion]\n");
256
257     /* initialize output_buffer with output data */
258     if (output) {
259         ret = ff_audio_data_init(&output_buffer, output, out_plane_size,
260                                  avr->out_channels, out_samples,
261                                  avr->out_sample_fmt, 0, "output");
262         if (ret < 0)
263             return ret;
264         output_buffer.nb_samples = 0;
265     }
266
267     if (input) {
268         /* initialize input_buffer with input data */
269         ret = ff_audio_data_init(&input_buffer, input, in_plane_size,
270                                  avr->in_channels, in_samples,
271                                  avr->in_sample_fmt, 1, "input");
272         if (ret < 0)
273             return ret;
274         current_buffer = &input_buffer;
275
276         if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&
277             !avr->out_convert_needed && output && out_samples >= in_samples) {
278             /* in some rare cases we can copy input to output and upmix
279                directly in the output buffer */
280             av_dlog(avr, "[copy] %s to output\n", current_buffer->name);
281             ret = ff_audio_data_copy(&output_buffer, current_buffer);
282             if (ret < 0)
283                 return ret;
284             current_buffer = &output_buffer;
285         } else if (avr->mixing_needed || avr->in_convert_needed) {
286             /* if needed, copy or convert input to in_buffer, and downmix if
287                applicable */
288             if (avr->in_convert_needed) {
289                 ret = ff_audio_data_realloc(avr->in_buffer,
290                                             current_buffer->nb_samples);
291                 if (ret < 0)
292                     return ret;
293                 av_dlog(avr, "[convert] %s to in_buffer\n", current_buffer->name);
294                 ret = ff_audio_convert(avr->ac_in, avr->in_buffer, current_buffer,
295                                        current_buffer->nb_samples);
296                 if (ret < 0)
297                     return ret;
298             } else {
299                 av_dlog(avr, "[copy] %s to in_buffer\n", current_buffer->name);
300                 ret = ff_audio_data_copy(avr->in_buffer, current_buffer);
301                 if (ret < 0)
302                     return ret;
303             }
304             ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);
305             if (avr->downmix_needed) {
306                 av_dlog(avr, "[downmix] in_buffer\n");
307                 ret = ff_audio_mix(avr->am, avr->in_buffer);
308                 if (ret < 0)
309                     return ret;
310             }
311             current_buffer = avr->in_buffer;
312         }
313     } else {
314         /* flush resampling buffer and/or output FIFO if input is NULL */
315         if (!avr->resample_needed)
316             return handle_buffered_output(avr, output ? &output_buffer : NULL,
317                                           NULL);
318         current_buffer = NULL;
319     }
320
321     if (avr->resample_needed) {
322         AudioData *resample_out;
323         int consumed = 0;
324
325         if (!avr->out_convert_needed && output && out_samples > 0)
326             resample_out = &output_buffer;
327         else
328             resample_out = avr->resample_out_buffer;
329         av_dlog(avr, "[resample] %s to %s\n", current_buffer->name,
330                 resample_out->name);
331         ret = ff_audio_resample(avr->resample, resample_out,
332                                 current_buffer, &consumed);
333         if (ret < 0)
334             return ret;
335
336         /* if resampling did not produce any samples, just return 0 */
337         if (resample_out->nb_samples == 0) {
338             av_dlog(avr, "[end conversion]\n");
339             return 0;
340         }
341
342         current_buffer = resample_out;
343     }
344
345     if (avr->upmix_needed) {
346         av_dlog(avr, "[upmix] %s\n", current_buffer->name);
347         ret = ff_audio_mix(avr->am, current_buffer);
348         if (ret < 0)
349             return ret;
350     }
351
352     /* if we resampled or upmixed directly to output, return here */
353     if (current_buffer == &output_buffer) {
354         av_dlog(avr, "[end conversion]\n");
355         return current_buffer->nb_samples;
356     }
357
358     if (avr->out_convert_needed) {
359         if (output && out_samples >= current_buffer->nb_samples) {
360             /* convert directly to output */
361             av_dlog(avr, "[convert] %s to output\n", current_buffer->name);
362             ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer,
363                                    current_buffer->nb_samples);
364             if (ret < 0)
365                 return ret;
366
367             av_dlog(avr, "[end conversion]\n");
368             return output_buffer.nb_samples;
369         } else {
370             ret = ff_audio_data_realloc(avr->out_buffer,
371                                         current_buffer->nb_samples);
372             if (ret < 0)
373                 return ret;
374             av_dlog(avr, "[convert] %s to out_buffer\n", current_buffer->name);
375             ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
376                                    current_buffer, current_buffer->nb_samples);
377             if (ret < 0)
378                 return ret;
379             current_buffer = avr->out_buffer;
380         }
381     }
382
383     return handle_buffered_output(avr, output ? &output_buffer : NULL,
384                                   current_buffer);
385 }
386
387 int avresample_available(AVAudioResampleContext *avr)
388 {
389     return av_audio_fifo_size(avr->out_fifo);
390 }
391
392 int avresample_read(AVAudioResampleContext *avr, void **output, int nb_samples)
393 {
394     if (!output)
395         return av_audio_fifo_drain(avr->out_fifo, nb_samples);
396     return av_audio_fifo_read(avr->out_fifo, output, nb_samples);
397 }
398
399 unsigned avresample_version(void)
400 {
401     return LIBAVRESAMPLE_VERSION_INT;
402 }
403
404 const char *avresample_license(void)
405 {
406 #define LICENSE_PREFIX "libavresample license: "
407     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
408 }
409
410 const char *avresample_configuration(void)
411 {
412     return LIBAV_CONFIGURATION;
413 }