]> git.sesse.net Git - ffmpeg/blob - libavresample/utils.c
avresample: use valid log context in mixing functions
[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         if (!avr->ac_in) {
147             ret = AVERROR(ENOMEM);
148             goto error;
149         }
150     }
151     if (avr->out_convert_needed) {
152         enum AVSampleFormat src_fmt;
153         if (avr->in_convert_needed)
154             src_fmt = avr->internal_sample_fmt;
155         else
156             src_fmt = avr->in_sample_fmt;
157         avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
158                                              avr->out_channels);
159         if (!avr->ac_out) {
160             ret = AVERROR(ENOMEM);
161             goto error;
162         }
163     }
164     if (avr->resample_needed) {
165         avr->resample = ff_audio_resample_init(avr);
166         if (!avr->resample) {
167             ret = AVERROR(ENOMEM);
168             goto error;
169         }
170     }
171     if (avr->mixing_needed) {
172         avr->am = ff_audio_mix_alloc(avr);
173         if (!avr->am) {
174             ret = AVERROR(ENOMEM);
175             goto error;
176         }
177     }
178
179     return 0;
180
181 error:
182     avresample_close(avr);
183     return ret;
184 }
185
186 void avresample_close(AVAudioResampleContext *avr)
187 {
188     ff_audio_data_free(&avr->in_buffer);
189     ff_audio_data_free(&avr->resample_out_buffer);
190     ff_audio_data_free(&avr->out_buffer);
191     av_audio_fifo_free(avr->out_fifo);
192     avr->out_fifo = NULL;
193     av_freep(&avr->ac_in);
194     av_freep(&avr->ac_out);
195     ff_audio_resample_free(&avr->resample);
196     ff_audio_mix_free(&avr->am);
197     av_freep(&avr->mix_matrix);
198 }
199
200 void avresample_free(AVAudioResampleContext **avr)
201 {
202     if (!*avr)
203         return;
204     avresample_close(*avr);
205     av_opt_free(*avr);
206     av_freep(avr);
207 }
208
209 static int handle_buffered_output(AVAudioResampleContext *avr,
210                                   AudioData *output, AudioData *converted)
211 {
212     int ret;
213
214     if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
215         (converted && output->allocated_samples < converted->nb_samples)) {
216         if (converted) {
217             /* if there are any samples in the output FIFO or if the
218                user-supplied output buffer is not large enough for all samples,
219                we add to the output FIFO */
220             av_dlog(avr, "[FIFO] add %s to out_fifo\n", converted->name);
221             ret = ff_audio_data_add_to_fifo(avr->out_fifo, converted, 0,
222                                             converted->nb_samples);
223             if (ret < 0)
224                 return ret;
225         }
226
227         /* if the user specified an output buffer, read samples from the output
228            FIFO to the user output */
229         if (output && output->allocated_samples > 0) {
230             av_dlog(avr, "[FIFO] read from out_fifo to output\n");
231             av_dlog(avr, "[end conversion]\n");
232             return ff_audio_data_read_from_fifo(avr->out_fifo, output,
233                                                 output->allocated_samples);
234         }
235     } else if (converted) {
236         /* copy directly to output if it is large enough or there is not any
237            data in the output FIFO */
238         av_dlog(avr, "[copy] %s to output\n", converted->name);
239         output->nb_samples = 0;
240         ret = ff_audio_data_copy(output, converted);
241         if (ret < 0)
242             return ret;
243         av_dlog(avr, "[end conversion]\n");
244         return output->nb_samples;
245     }
246     av_dlog(avr, "[end conversion]\n");
247     return 0;
248 }
249
250 int attribute_align_arg avresample_convert(AVAudioResampleContext *avr,
251                                            uint8_t **output, int out_plane_size,
252                                            int out_samples, uint8_t **input,
253                                            int in_plane_size, int in_samples)
254 {
255     AudioData input_buffer;
256     AudioData output_buffer;
257     AudioData *current_buffer;
258     int ret, direct_output;
259
260     /* reset internal buffers */
261     if (avr->in_buffer) {
262         avr->in_buffer->nb_samples = 0;
263         ff_audio_data_set_channels(avr->in_buffer,
264                                    avr->in_buffer->allocated_channels);
265     }
266     if (avr->resample_out_buffer) {
267         avr->resample_out_buffer->nb_samples = 0;
268         ff_audio_data_set_channels(avr->resample_out_buffer,
269                                    avr->resample_out_buffer->allocated_channels);
270     }
271     if (avr->out_buffer) {
272         avr->out_buffer->nb_samples = 0;
273         ff_audio_data_set_channels(avr->out_buffer,
274                                    avr->out_buffer->allocated_channels);
275     }
276
277     av_dlog(avr, "[start conversion]\n");
278
279     /* initialize output_buffer with output data */
280     direct_output = output && av_audio_fifo_size(avr->out_fifo) == 0;
281     if (output) {
282         ret = ff_audio_data_init(&output_buffer, output, out_plane_size,
283                                  avr->out_channels, out_samples,
284                                  avr->out_sample_fmt, 0, "output");
285         if (ret < 0)
286             return ret;
287         output_buffer.nb_samples = 0;
288     }
289
290     if (input) {
291         /* initialize input_buffer with input data */
292         ret = ff_audio_data_init(&input_buffer, input, in_plane_size,
293                                  avr->in_channels, in_samples,
294                                  avr->in_sample_fmt, 1, "input");
295         if (ret < 0)
296             return ret;
297         current_buffer = &input_buffer;
298
299         if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&
300             !avr->out_convert_needed && direct_output && out_samples >= in_samples) {
301             /* in some rare cases we can copy input to output and upmix
302                directly in the output buffer */
303             av_dlog(avr, "[copy] %s to output\n", current_buffer->name);
304             ret = ff_audio_data_copy(&output_buffer, current_buffer);
305             if (ret < 0)
306                 return ret;
307             current_buffer = &output_buffer;
308         } else if (avr->mixing_needed || avr->in_convert_needed) {
309             /* if needed, copy or convert input to in_buffer, and downmix if
310                applicable */
311             if (avr->in_convert_needed) {
312                 ret = ff_audio_data_realloc(avr->in_buffer,
313                                             current_buffer->nb_samples);
314                 if (ret < 0)
315                     return ret;
316                 av_dlog(avr, "[convert] %s to in_buffer\n", current_buffer->name);
317                 ret = ff_audio_convert(avr->ac_in, avr->in_buffer,
318                                        current_buffer);
319                 if (ret < 0)
320                     return ret;
321             } else {
322                 av_dlog(avr, "[copy] %s to in_buffer\n", current_buffer->name);
323                 ret = ff_audio_data_copy(avr->in_buffer, current_buffer);
324                 if (ret < 0)
325                     return ret;
326             }
327             ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);
328             if (avr->downmix_needed) {
329                 av_dlog(avr, "[downmix] in_buffer\n");
330                 ret = ff_audio_mix(avr->am, avr->in_buffer);
331                 if (ret < 0)
332                     return ret;
333             }
334             current_buffer = avr->in_buffer;
335         }
336     } else {
337         /* flush resampling buffer and/or output FIFO if input is NULL */
338         if (!avr->resample_needed)
339             return handle_buffered_output(avr, output ? &output_buffer : NULL,
340                                           NULL);
341         current_buffer = NULL;
342     }
343
344     if (avr->resample_needed) {
345         AudioData *resample_out;
346
347         if (!avr->out_convert_needed && direct_output && out_samples > 0)
348             resample_out = &output_buffer;
349         else
350             resample_out = avr->resample_out_buffer;
351         av_dlog(avr, "[resample] %s to %s\n", current_buffer->name,
352                 resample_out->name);
353         ret = ff_audio_resample(avr->resample, resample_out,
354                                 current_buffer);
355         if (ret < 0)
356             return ret;
357
358         /* if resampling did not produce any samples, just return 0 */
359         if (resample_out->nb_samples == 0) {
360             av_dlog(avr, "[end conversion]\n");
361             return 0;
362         }
363
364         current_buffer = resample_out;
365     }
366
367     if (avr->upmix_needed) {
368         av_dlog(avr, "[upmix] %s\n", current_buffer->name);
369         ret = ff_audio_mix(avr->am, current_buffer);
370         if (ret < 0)
371             return ret;
372     }
373
374     /* if we resampled or upmixed directly to output, return here */
375     if (current_buffer == &output_buffer) {
376         av_dlog(avr, "[end conversion]\n");
377         return current_buffer->nb_samples;
378     }
379
380     if (avr->out_convert_needed) {
381         if (direct_output && out_samples >= current_buffer->nb_samples) {
382             /* convert directly to output */
383             av_dlog(avr, "[convert] %s to output\n", current_buffer->name);
384             ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer);
385             if (ret < 0)
386                 return ret;
387
388             av_dlog(avr, "[end conversion]\n");
389             return output_buffer.nb_samples;
390         } else {
391             ret = ff_audio_data_realloc(avr->out_buffer,
392                                         current_buffer->nb_samples);
393             if (ret < 0)
394                 return ret;
395             av_dlog(avr, "[convert] %s to out_buffer\n", current_buffer->name);
396             ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
397                                    current_buffer);
398             if (ret < 0)
399                 return ret;
400             current_buffer = avr->out_buffer;
401         }
402     }
403
404     return handle_buffered_output(avr, output ? &output_buffer : NULL,
405                                   current_buffer);
406 }
407
408 int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
409                           int stride)
410 {
411     int in_channels, out_channels, i, o;
412
413     if (avr->am)
414         return ff_audio_mix_get_matrix(avr->am, matrix, stride);
415
416     in_channels  = av_get_channel_layout_nb_channels(avr->in_channel_layout);
417     out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
418
419     if ( in_channels <= 0 ||  in_channels > AVRESAMPLE_MAX_CHANNELS ||
420         out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
421         av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
422         return AVERROR(EINVAL);
423     }
424
425     if (!avr->mix_matrix) {
426         av_log(avr, AV_LOG_ERROR, "matrix is not set\n");
427         return AVERROR(EINVAL);
428     }
429
430     for (o = 0; o < out_channels; o++)
431         for (i = 0; i < in_channels; i++)
432             matrix[o * stride + i] = avr->mix_matrix[o * in_channels + i];
433
434     return 0;
435 }
436
437 int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
438                           int stride)
439 {
440     int in_channels, out_channels, i, o;
441
442     if (avr->am)
443         return ff_audio_mix_set_matrix(avr->am, matrix, stride);
444
445     in_channels  = av_get_channel_layout_nb_channels(avr->in_channel_layout);
446     out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
447
448     if ( in_channels <= 0 ||  in_channels > AVRESAMPLE_MAX_CHANNELS ||
449         out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
450         av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
451         return AVERROR(EINVAL);
452     }
453
454     if (avr->mix_matrix)
455         av_freep(&avr->mix_matrix);
456     avr->mix_matrix = av_malloc(in_channels * out_channels *
457                                 sizeof(*avr->mix_matrix));
458     if (!avr->mix_matrix)
459         return AVERROR(ENOMEM);
460
461     for (o = 0; o < out_channels; o++)
462         for (i = 0; i < in_channels; i++)
463             avr->mix_matrix[o * in_channels + i] = matrix[o * stride + i];
464
465     return 0;
466 }
467
468 int avresample_available(AVAudioResampleContext *avr)
469 {
470     return av_audio_fifo_size(avr->out_fifo);
471 }
472
473 int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
474 {
475     if (!output)
476         return av_audio_fifo_drain(avr->out_fifo, nb_samples);
477     return av_audio_fifo_read(avr->out_fifo, (void**)output, nb_samples);
478 }
479
480 unsigned avresample_version(void)
481 {
482     return LIBAVRESAMPLE_VERSION_INT;
483 }
484
485 const char *avresample_license(void)
486 {
487 #define LICENSE_PREFIX "libavresample license: "
488     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
489 }
490
491 const char *avresample_configuration(void)
492 {
493     return LIBAV_CONFIGURATION;
494 }