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