a30388092e3ba6e73c31925e445e40f77eddd709
[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/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"
27
28 #include "avresample.h"
29 #include "internal.h"
30 #include "audio_data.h"
31 #include "audio_convert.h"
32 #include "audio_mix.h"
33 #include "resample.h"
34
35 int avresample_open(AVAudioResampleContext *avr)
36 {
37     int ret;
38
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);
45     }
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);
51     }
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;
58
59     /* set resampling parameters */
60     avr->resample_needed   = avr->in_sample_rate != avr->out_sample_rate ||
61                              avr->force_resampling;
62
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));
70         if (max_bps <= 2) {
71             avr->internal_sample_fmt = AV_SAMPLE_FMT_S16P;
72         } else if (avr->mixing_needed) {
73             avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
74         } else {
75             if (max_bps <= 4) {
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;
82                     } else {
83                         /* if one is s32 and the other is s32, s16, or u8, use s32 */
84                         avr->internal_sample_fmt = AV_SAMPLE_FMT_S32P;
85                     }
86                 } else {
87                     /* if one is flt and the other is flt, s16 or u8, use flt */
88                     avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
89                 }
90             } else {
91                 /* if either is dbl, use dbl */
92                 avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
93             }
94         }
95         av_log(avr, AV_LOG_DEBUG, "Using %s as internal sample format\n",
96                av_get_sample_fmt_name(avr->internal_sample_fmt));
97     }
98
99     /* set sample format conversion parameters */
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);
104     avr->in_convert_needed = (avr->resample_needed || avr->mixing_needed) &&
105                               avr->in_sample_fmt != avr->internal_sample_fmt;
106     if (avr->resample_needed || avr->mixing_needed)
107         avr->out_convert_needed = avr->internal_sample_fmt != avr->out_sample_fmt;
108     else
109         avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;
110
111     /* allocate buffers */
112     if (avr->mixing_needed || avr->in_convert_needed) {
113         avr->in_buffer = ff_audio_data_alloc(FFMAX(avr->in_channels, avr->out_channels),
114                                              0, avr->internal_sample_fmt,
115                                              "in_buffer");
116         if (!avr->in_buffer) {
117             ret = AVERROR(EINVAL);
118             goto error;
119         }
120     }
121     if (avr->resample_needed) {
122         avr->resample_out_buffer = ff_audio_data_alloc(avr->out_channels,
123                                                        0, avr->internal_sample_fmt,
124                                                        "resample_out_buffer");
125         if (!avr->resample_out_buffer) {
126             ret = AVERROR(EINVAL);
127             goto error;
128         }
129     }
130     if (avr->out_convert_needed) {
131         avr->out_buffer = ff_audio_data_alloc(avr->out_channels, 0,
132                                               avr->out_sample_fmt, "out_buffer");
133         if (!avr->out_buffer) {
134             ret = AVERROR(EINVAL);
135             goto error;
136         }
137     }
138     avr->out_fifo = av_audio_fifo_alloc(avr->out_sample_fmt, avr->out_channels,
139                                         1024);
140     if (!avr->out_fifo) {
141         ret = AVERROR(ENOMEM);
142         goto error;
143     }
144
145     /* setup contexts */
146     if (avr->in_convert_needed) {
147         avr->ac_in = ff_audio_convert_alloc(avr, avr->internal_sample_fmt,
148                                             avr->in_sample_fmt, avr->in_channels,
149                                             avr->in_sample_rate);
150         if (!avr->ac_in) {
151             ret = AVERROR(ENOMEM);
152             goto error;
153         }
154     }
155     if (avr->out_convert_needed) {
156         enum AVSampleFormat src_fmt;
157         if (avr->in_convert_needed)
158             src_fmt = avr->internal_sample_fmt;
159         else
160             src_fmt = avr->in_sample_fmt;
161         avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
162                                              avr->out_channels,
163                                              avr->out_sample_rate);
164         if (!avr->ac_out) {
165             ret = AVERROR(ENOMEM);
166             goto error;
167         }
168     }
169     if (avr->resample_needed) {
170         avr->resample = ff_audio_resample_init(avr);
171         if (!avr->resample) {
172             ret = AVERROR(ENOMEM);
173             goto error;
174         }
175     }
176     if (avr->mixing_needed) {
177         avr->am = ff_audio_mix_alloc(avr);
178         if (!avr->am) {
179             ret = AVERROR(ENOMEM);
180             goto error;
181         }
182     }
183
184     return 0;
185
186 error:
187     avresample_close(avr);
188     return ret;
189 }
190
191 void avresample_close(AVAudioResampleContext *avr)
192 {
193     ff_audio_data_free(&avr->in_buffer);
194     ff_audio_data_free(&avr->resample_out_buffer);
195     ff_audio_data_free(&avr->out_buffer);
196     av_audio_fifo_free(avr->out_fifo);
197     avr->out_fifo = NULL;
198     ff_audio_convert_free(&avr->ac_in);
199     ff_audio_convert_free(&avr->ac_out);
200     ff_audio_resample_free(&avr->resample);
201     ff_audio_mix_free(&avr->am);
202     av_freep(&avr->mix_matrix);
203 }
204
205 void avresample_free(AVAudioResampleContext **avr)
206 {
207     if (!*avr)
208         return;
209     avresample_close(*avr);
210     av_opt_free(*avr);
211     av_freep(avr);
212 }
213
214 static int handle_buffered_output(AVAudioResampleContext *avr,
215                                   AudioData *output, AudioData *converted)
216 {
217     int ret;
218
219     if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
220         (converted && output->allocated_samples < converted->nb_samples)) {
221         if (converted) {
222             /* if there are any samples in the output FIFO or if the
223                user-supplied output buffer is not large enough for all samples,
224                we add to the output FIFO */
225             av_dlog(avr, "[FIFO] add %s to out_fifo\n", converted->name);
226             ret = ff_audio_data_add_to_fifo(avr->out_fifo, converted, 0,
227                                             converted->nb_samples);
228             if (ret < 0)
229                 return ret;
230         }
231
232         /* if the user specified an output buffer, read samples from the output
233            FIFO to the user output */
234         if (output && output->allocated_samples > 0) {
235             av_dlog(avr, "[FIFO] read from out_fifo to output\n");
236             av_dlog(avr, "[end conversion]\n");
237             return ff_audio_data_read_from_fifo(avr->out_fifo, output,
238                                                 output->allocated_samples);
239         }
240     } else if (converted) {
241         /* copy directly to output if it is large enough or there is not any
242            data in the output FIFO */
243         av_dlog(avr, "[copy] %s to output\n", converted->name);
244         output->nb_samples = 0;
245         ret = ff_audio_data_copy(output, converted);
246         if (ret < 0)
247             return ret;
248         av_dlog(avr, "[end conversion]\n");
249         return output->nb_samples;
250     }
251     av_dlog(avr, "[end conversion]\n");
252     return 0;
253 }
254
255 int attribute_align_arg avresample_convert(AVAudioResampleContext *avr,
256                                            uint8_t **output, int out_plane_size,
257                                            int out_samples, uint8_t **input,
258                                            int in_plane_size, int in_samples)
259 {
260     AudioData input_buffer;
261     AudioData output_buffer;
262     AudioData *current_buffer;
263     int ret, direct_output;
264
265     /* reset internal buffers */
266     if (avr->in_buffer) {
267         avr->in_buffer->nb_samples = 0;
268         ff_audio_data_set_channels(avr->in_buffer,
269                                    avr->in_buffer->allocated_channels);
270     }
271     if (avr->resample_out_buffer) {
272         avr->resample_out_buffer->nb_samples = 0;
273         ff_audio_data_set_channels(avr->resample_out_buffer,
274                                    avr->resample_out_buffer->allocated_channels);
275     }
276     if (avr->out_buffer) {
277         avr->out_buffer->nb_samples = 0;
278         ff_audio_data_set_channels(avr->out_buffer,
279                                    avr->out_buffer->allocated_channels);
280     }
281
282     av_dlog(avr, "[start conversion]\n");
283
284     /* initialize output_buffer with output data */
285     direct_output = output && av_audio_fifo_size(avr->out_fifo) == 0;
286     if (output) {
287         ret = ff_audio_data_init(&output_buffer, output, out_plane_size,
288                                  avr->out_channels, out_samples,
289                                  avr->out_sample_fmt, 0, "output");
290         if (ret < 0)
291             return ret;
292         output_buffer.nb_samples = 0;
293     }
294
295     if (input) {
296         /* initialize input_buffer with input data */
297         ret = ff_audio_data_init(&input_buffer, input, in_plane_size,
298                                  avr->in_channels, in_samples,
299                                  avr->in_sample_fmt, 1, "input");
300         if (ret < 0)
301             return ret;
302         current_buffer = &input_buffer;
303
304         if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&
305             !avr->out_convert_needed && direct_output && out_samples >= in_samples) {
306             /* in some rare cases we can copy input to output and upmix
307                directly in the output buffer */
308             av_dlog(avr, "[copy] %s to output\n", current_buffer->name);
309             ret = ff_audio_data_copy(&output_buffer, current_buffer);
310             if (ret < 0)
311                 return ret;
312             current_buffer = &output_buffer;
313         } else if (avr->mixing_needed || avr->in_convert_needed) {
314             /* if needed, copy or convert input to in_buffer, and downmix if
315                applicable */
316             if (avr->in_convert_needed) {
317                 ret = ff_audio_data_realloc(avr->in_buffer,
318                                             current_buffer->nb_samples);
319                 if (ret < 0)
320                     return ret;
321                 av_dlog(avr, "[convert] %s to in_buffer\n", current_buffer->name);
322                 ret = ff_audio_convert(avr->ac_in, avr->in_buffer,
323                                        current_buffer);
324                 if (ret < 0)
325                     return ret;
326             } else {
327                 av_dlog(avr, "[copy] %s to in_buffer\n", current_buffer->name);
328                 ret = ff_audio_data_copy(avr->in_buffer, current_buffer);
329                 if (ret < 0)
330                     return ret;
331             }
332             ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);
333             if (avr->downmix_needed) {
334                 av_dlog(avr, "[downmix] in_buffer\n");
335                 ret = ff_audio_mix(avr->am, avr->in_buffer);
336                 if (ret < 0)
337                     return ret;
338             }
339             current_buffer = avr->in_buffer;
340         }
341     } else {
342         /* flush resampling buffer and/or output FIFO if input is NULL */
343         if (!avr->resample_needed)
344             return handle_buffered_output(avr, output ? &output_buffer : NULL,
345                                           NULL);
346         current_buffer = NULL;
347     }
348
349     if (avr->resample_needed) {
350         AudioData *resample_out;
351
352         if (!avr->out_convert_needed && direct_output && out_samples > 0)
353             resample_out = &output_buffer;
354         else
355             resample_out = avr->resample_out_buffer;
356         av_dlog(avr, "[resample] %s to %s\n", current_buffer->name,
357                 resample_out->name);
358         ret = ff_audio_resample(avr->resample, resample_out,
359                                 current_buffer);
360         if (ret < 0)
361             return ret;
362
363         /* if resampling did not produce any samples, just return 0 */
364         if (resample_out->nb_samples == 0) {
365             av_dlog(avr, "[end conversion]\n");
366             return 0;
367         }
368
369         current_buffer = resample_out;
370     }
371
372     if (avr->upmix_needed) {
373         av_dlog(avr, "[upmix] %s\n", current_buffer->name);
374         ret = ff_audio_mix(avr->am, current_buffer);
375         if (ret < 0)
376             return ret;
377     }
378
379     /* if we resampled or upmixed directly to output, return here */
380     if (current_buffer == &output_buffer) {
381         av_dlog(avr, "[end conversion]\n");
382         return current_buffer->nb_samples;
383     }
384
385     if (avr->out_convert_needed) {
386         if (direct_output && out_samples >= current_buffer->nb_samples) {
387             /* convert directly to output */
388             av_dlog(avr, "[convert] %s to output\n", current_buffer->name);
389             ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer);
390             if (ret < 0)
391                 return ret;
392
393             av_dlog(avr, "[end conversion]\n");
394             return output_buffer.nb_samples;
395         } else {
396             ret = ff_audio_data_realloc(avr->out_buffer,
397                                         current_buffer->nb_samples);
398             if (ret < 0)
399                 return ret;
400             av_dlog(avr, "[convert] %s to out_buffer\n", current_buffer->name);
401             ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
402                                    current_buffer);
403             if (ret < 0)
404                 return ret;
405             current_buffer = avr->out_buffer;
406         }
407     }
408
409     return handle_buffered_output(avr, output ? &output_buffer : NULL,
410                                   current_buffer);
411 }
412
413 int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
414                           int stride)
415 {
416     int in_channels, out_channels, i, o;
417
418     if (avr->am)
419         return ff_audio_mix_get_matrix(avr->am, matrix, stride);
420
421     in_channels  = av_get_channel_layout_nb_channels(avr->in_channel_layout);
422     out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
423
424     if ( in_channels <= 0 ||  in_channels > AVRESAMPLE_MAX_CHANNELS ||
425         out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
426         av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
427         return AVERROR(EINVAL);
428     }
429
430     if (!avr->mix_matrix) {
431         av_log(avr, AV_LOG_ERROR, "matrix is not set\n");
432         return AVERROR(EINVAL);
433     }
434
435     for (o = 0; o < out_channels; o++)
436         for (i = 0; i < in_channels; i++)
437             matrix[o * stride + i] = avr->mix_matrix[o * in_channels + i];
438
439     return 0;
440 }
441
442 int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
443                           int stride)
444 {
445     int in_channels, out_channels, i, o;
446
447     if (avr->am)
448         return ff_audio_mix_set_matrix(avr->am, matrix, stride);
449
450     in_channels  = av_get_channel_layout_nb_channels(avr->in_channel_layout);
451     out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
452
453     if ( in_channels <= 0 ||  in_channels > AVRESAMPLE_MAX_CHANNELS ||
454         out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
455         av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
456         return AVERROR(EINVAL);
457     }
458
459     if (avr->mix_matrix)
460         av_freep(&avr->mix_matrix);
461     avr->mix_matrix = av_malloc(in_channels * out_channels *
462                                 sizeof(*avr->mix_matrix));
463     if (!avr->mix_matrix)
464         return AVERROR(ENOMEM);
465
466     for (o = 0; o < out_channels; o++)
467         for (i = 0; i < in_channels; i++)
468             avr->mix_matrix[o * in_channels + i] = matrix[o * stride + i];
469
470     return 0;
471 }
472
473 int avresample_available(AVAudioResampleContext *avr)
474 {
475     return av_audio_fifo_size(avr->out_fifo);
476 }
477
478 int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
479 {
480     if (!output)
481         return av_audio_fifo_drain(avr->out_fifo, nb_samples);
482     return av_audio_fifo_read(avr->out_fifo, (void**)output, nb_samples);
483 }
484
485 unsigned avresample_version(void)
486 {
487     return LIBAVRESAMPLE_VERSION_INT;
488 }
489
490 const char *avresample_license(void)
491 {
492 #define LICENSE_PREFIX "libavresample license: "
493     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
494 }
495
496 const char *avresample_configuration(void)
497 {
498     return LIBAV_CONFIGURATION;
499 }