lavr: Do not change the sample format for mono audio
[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     if (avresample_is_open(avr)) {
40         av_log(avr, AV_LOG_ERROR, "The resampling context is already open.\n");
41         return AVERROR(EINVAL);
42     }
43
44     /* set channel mixing parameters */
45     avr->in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
46     if (avr->in_channels <= 0 || avr->in_channels > AVRESAMPLE_MAX_CHANNELS) {
47         av_log(avr, AV_LOG_ERROR, "Invalid input channel layout: %"PRIu64"\n",
48                avr->in_channel_layout);
49         return AVERROR(EINVAL);
50     }
51     avr->out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
52     if (avr->out_channels <= 0 || avr->out_channels > AVRESAMPLE_MAX_CHANNELS) {
53         av_log(avr, AV_LOG_ERROR, "Invalid output channel layout: %"PRIu64"\n",
54                avr->out_channel_layout);
55         return AVERROR(EINVAL);
56     }
57     avr->resample_channels = FFMIN(avr->in_channels, avr->out_channels);
58     avr->downmix_needed    = avr->in_channels  > avr->out_channels;
59     avr->upmix_needed      = avr->out_channels > avr->in_channels ||
60                              (!avr->downmix_needed && (avr->mix_matrix ||
61                               avr->in_channel_layout != avr->out_channel_layout));
62     avr->mixing_needed     = avr->downmix_needed || avr->upmix_needed;
63
64     /* set resampling parameters */
65     avr->resample_needed   = avr->in_sample_rate != avr->out_sample_rate ||
66                              avr->force_resampling;
67
68     /* select internal sample format if not specified by the user */
69     if (avr->internal_sample_fmt == AV_SAMPLE_FMT_NONE &&
70         (avr->mixing_needed || avr->resample_needed)) {
71         enum AVSampleFormat  in_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
72         enum AVSampleFormat out_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
73         int max_bps = FFMAX(av_get_bytes_per_sample(in_fmt),
74                             av_get_bytes_per_sample(out_fmt));
75         if (max_bps <= 2) {
76             avr->internal_sample_fmt = AV_SAMPLE_FMT_S16P;
77         } else if (avr->mixing_needed) {
78             avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
79         } else {
80             if (max_bps <= 4) {
81                 if (in_fmt  == AV_SAMPLE_FMT_S32P ||
82                     out_fmt == AV_SAMPLE_FMT_S32P) {
83                     if (in_fmt  == AV_SAMPLE_FMT_FLTP ||
84                         out_fmt == AV_SAMPLE_FMT_FLTP) {
85                         /* if one is s32 and the other is flt, use dbl */
86                         avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
87                     } else {
88                         /* if one is s32 and the other is s32, s16, or u8, use s32 */
89                         avr->internal_sample_fmt = AV_SAMPLE_FMT_S32P;
90                     }
91                 } else {
92                     /* if one is flt and the other is flt, s16 or u8, use flt */
93                     avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
94                 }
95             } else {
96                 /* if either is dbl, use dbl */
97                 avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
98             }
99         }
100         av_log(avr, AV_LOG_DEBUG, "Using %s as internal sample format\n",
101                av_get_sample_fmt_name(avr->internal_sample_fmt));
102     }
103
104     /* we may need to add an extra conversion in order to remap channels if
105        the output format is not planar */
106     if (avr->use_channel_map && !avr->mixing_needed && !avr->resample_needed &&
107         !ff_sample_fmt_is_planar(avr->out_sample_fmt, avr->out_channels)) {
108         avr->internal_sample_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
109     }
110
111     /* set sample format conversion parameters */
112     if (avr->resample_needed || avr->mixing_needed)
113         avr->in_convert_needed = avr->in_sample_fmt != avr->internal_sample_fmt;
114     else
115         avr->in_convert_needed = avr->use_channel_map &&
116                                  !ff_sample_fmt_is_planar(avr->out_sample_fmt, avr->out_channels);
117
118     if (avr->resample_needed || avr->mixing_needed || avr->in_convert_needed)
119         avr->out_convert_needed = avr->internal_sample_fmt != avr->out_sample_fmt;
120     else
121         avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;
122
123     avr->in_copy_needed = !avr->in_convert_needed && (avr->mixing_needed ||
124                           (avr->use_channel_map && avr->resample_needed));
125
126     if (avr->use_channel_map) {
127         if (avr->in_copy_needed) {
128             avr->remap_point = REMAP_IN_COPY;
129             av_dlog(avr, "remap channels during in_copy\n");
130         } else if (avr->in_convert_needed) {
131             avr->remap_point = REMAP_IN_CONVERT;
132             av_dlog(avr, "remap channels during in_convert\n");
133         } else if (avr->out_convert_needed) {
134             avr->remap_point = REMAP_OUT_CONVERT;
135             av_dlog(avr, "remap channels during out_convert\n");
136         } else {
137             avr->remap_point = REMAP_OUT_COPY;
138             av_dlog(avr, "remap channels during out_copy\n");
139         }
140
141 #ifdef DEBUG
142         {
143             int ch;
144             av_dlog(avr, "output map: ");
145             if (avr->ch_map_info.do_remap)
146                 for (ch = 0; ch < avr->in_channels; ch++)
147                     av_dlog(avr, " % 2d", avr->ch_map_info.channel_map[ch]);
148             else
149                 av_dlog(avr, "n/a");
150             av_dlog(avr, "\n");
151             av_dlog(avr, "copy map:   ");
152             if (avr->ch_map_info.do_copy)
153                 for (ch = 0; ch < avr->in_channels; ch++)
154                     av_dlog(avr, " % 2d", avr->ch_map_info.channel_copy[ch]);
155             else
156                 av_dlog(avr, "n/a");
157             av_dlog(avr, "\n");
158             av_dlog(avr, "zero map:   ");
159             if (avr->ch_map_info.do_zero)
160                 for (ch = 0; ch < avr->in_channels; ch++)
161                     av_dlog(avr, " % 2d", avr->ch_map_info.channel_zero[ch]);
162             else
163                 av_dlog(avr, "n/a");
164             av_dlog(avr, "\n");
165             av_dlog(avr, "input map:  ");
166             for (ch = 0; ch < avr->in_channels; ch++)
167                 av_dlog(avr, " % 2d", avr->ch_map_info.input_map[ch]);
168             av_dlog(avr, "\n");
169         }
170 #endif
171     } else
172         avr->remap_point = REMAP_NONE;
173
174     /* allocate buffers */
175     if (avr->in_copy_needed || avr->in_convert_needed) {
176         avr->in_buffer = ff_audio_data_alloc(FFMAX(avr->in_channels, avr->out_channels),
177                                              0, avr->internal_sample_fmt,
178                                              "in_buffer");
179         if (!avr->in_buffer) {
180             ret = AVERROR(EINVAL);
181             goto error;
182         }
183     }
184     if (avr->resample_needed) {
185         avr->resample_out_buffer = ff_audio_data_alloc(avr->out_channels,
186                                                        1024, avr->internal_sample_fmt,
187                                                        "resample_out_buffer");
188         if (!avr->resample_out_buffer) {
189             ret = AVERROR(EINVAL);
190             goto error;
191         }
192     }
193     if (avr->out_convert_needed) {
194         avr->out_buffer = ff_audio_data_alloc(avr->out_channels, 0,
195                                               avr->out_sample_fmt, "out_buffer");
196         if (!avr->out_buffer) {
197             ret = AVERROR(EINVAL);
198             goto error;
199         }
200     }
201     avr->out_fifo = av_audio_fifo_alloc(avr->out_sample_fmt, avr->out_channels,
202                                         1024);
203     if (!avr->out_fifo) {
204         ret = AVERROR(ENOMEM);
205         goto error;
206     }
207
208     /* setup contexts */
209     if (avr->in_convert_needed) {
210         avr->ac_in = ff_audio_convert_alloc(avr, avr->internal_sample_fmt,
211                                             avr->in_sample_fmt, avr->in_channels,
212                                             avr->in_sample_rate,
213                                             avr->remap_point == REMAP_IN_CONVERT);
214         if (!avr->ac_in) {
215             ret = AVERROR(ENOMEM);
216             goto error;
217         }
218     }
219     if (avr->out_convert_needed) {
220         enum AVSampleFormat src_fmt;
221         if (avr->in_convert_needed)
222             src_fmt = avr->internal_sample_fmt;
223         else
224             src_fmt = avr->in_sample_fmt;
225         avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
226                                              avr->out_channels,
227                                              avr->out_sample_rate,
228                                              avr->remap_point == REMAP_OUT_CONVERT);
229         if (!avr->ac_out) {
230             ret = AVERROR(ENOMEM);
231             goto error;
232         }
233     }
234     if (avr->resample_needed) {
235         avr->resample = ff_audio_resample_init(avr);
236         if (!avr->resample) {
237             ret = AVERROR(ENOMEM);
238             goto error;
239         }
240     }
241     if (avr->mixing_needed) {
242         avr->am = ff_audio_mix_alloc(avr);
243         if (!avr->am) {
244             ret = AVERROR(ENOMEM);
245             goto error;
246         }
247     }
248
249     return 0;
250
251 error:
252     avresample_close(avr);
253     return ret;
254 }
255
256 int avresample_is_open(AVAudioResampleContext *avr)
257 {
258     return !!avr->out_fifo;
259 }
260
261 void avresample_close(AVAudioResampleContext *avr)
262 {
263     ff_audio_data_free(&avr->in_buffer);
264     ff_audio_data_free(&avr->resample_out_buffer);
265     ff_audio_data_free(&avr->out_buffer);
266     av_audio_fifo_free(avr->out_fifo);
267     avr->out_fifo = NULL;
268     ff_audio_convert_free(&avr->ac_in);
269     ff_audio_convert_free(&avr->ac_out);
270     ff_audio_resample_free(&avr->resample);
271     ff_audio_mix_free(&avr->am);
272     av_freep(&avr->mix_matrix);
273
274     avr->use_channel_map = 0;
275 }
276
277 void avresample_free(AVAudioResampleContext **avr)
278 {
279     if (!*avr)
280         return;
281     avresample_close(*avr);
282     av_opt_free(*avr);
283     av_freep(avr);
284 }
285
286 static int handle_buffered_output(AVAudioResampleContext *avr,
287                                   AudioData *output, AudioData *converted)
288 {
289     int ret;
290
291     if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
292         (converted && output->allocated_samples < converted->nb_samples)) {
293         if (converted) {
294             /* if there are any samples in the output FIFO or if the
295                user-supplied output buffer is not large enough for all samples,
296                we add to the output FIFO */
297             av_dlog(avr, "[FIFO] add %s to out_fifo\n", converted->name);
298             ret = ff_audio_data_add_to_fifo(avr->out_fifo, converted, 0,
299                                             converted->nb_samples);
300             if (ret < 0)
301                 return ret;
302         }
303
304         /* if the user specified an output buffer, read samples from the output
305            FIFO to the user output */
306         if (output && output->allocated_samples > 0) {
307             av_dlog(avr, "[FIFO] read from out_fifo to output\n");
308             av_dlog(avr, "[end conversion]\n");
309             return ff_audio_data_read_from_fifo(avr->out_fifo, output,
310                                                 output->allocated_samples);
311         }
312     } else if (converted) {
313         /* copy directly to output if it is large enough or there is not any
314            data in the output FIFO */
315         av_dlog(avr, "[copy] %s to output\n", converted->name);
316         output->nb_samples = 0;
317         ret = ff_audio_data_copy(output, converted,
318                                  avr->remap_point == REMAP_OUT_COPY ?
319                                  &avr->ch_map_info : NULL);
320         if (ret < 0)
321             return ret;
322         av_dlog(avr, "[end conversion]\n");
323         return output->nb_samples;
324     }
325     av_dlog(avr, "[end conversion]\n");
326     return 0;
327 }
328
329 int attribute_align_arg avresample_convert(AVAudioResampleContext *avr,
330                                            uint8_t **output, int out_plane_size,
331                                            int out_samples, uint8_t **input,
332                                            int in_plane_size, int in_samples)
333 {
334     AudioData input_buffer;
335     AudioData output_buffer;
336     AudioData *current_buffer;
337     int ret, direct_output;
338
339     /* reset internal buffers */
340     if (avr->in_buffer) {
341         avr->in_buffer->nb_samples = 0;
342         ff_audio_data_set_channels(avr->in_buffer,
343                                    avr->in_buffer->allocated_channels);
344     }
345     if (avr->resample_out_buffer) {
346         avr->resample_out_buffer->nb_samples = 0;
347         ff_audio_data_set_channels(avr->resample_out_buffer,
348                                    avr->resample_out_buffer->allocated_channels);
349     }
350     if (avr->out_buffer) {
351         avr->out_buffer->nb_samples = 0;
352         ff_audio_data_set_channels(avr->out_buffer,
353                                    avr->out_buffer->allocated_channels);
354     }
355
356     av_dlog(avr, "[start conversion]\n");
357
358     /* initialize output_buffer with output data */
359     direct_output = output && av_audio_fifo_size(avr->out_fifo) == 0;
360     if (output) {
361         ret = ff_audio_data_init(&output_buffer, output, out_plane_size,
362                                  avr->out_channels, out_samples,
363                                  avr->out_sample_fmt, 0, "output");
364         if (ret < 0)
365             return ret;
366         output_buffer.nb_samples = 0;
367     }
368
369     if (input) {
370         /* initialize input_buffer with input data */
371         ret = ff_audio_data_init(&input_buffer, input, in_plane_size,
372                                  avr->in_channels, in_samples,
373                                  avr->in_sample_fmt, 1, "input");
374         if (ret < 0)
375             return ret;
376         current_buffer = &input_buffer;
377
378         if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&
379             !avr->out_convert_needed && direct_output && out_samples >= in_samples) {
380             /* in some rare cases we can copy input to output and upmix
381                directly in the output buffer */
382             av_dlog(avr, "[copy] %s to output\n", current_buffer->name);
383             ret = ff_audio_data_copy(&output_buffer, current_buffer,
384                                      avr->remap_point == REMAP_OUT_COPY ?
385                                      &avr->ch_map_info : NULL);
386             if (ret < 0)
387                 return ret;
388             current_buffer = &output_buffer;
389         } else if (avr->remap_point == REMAP_OUT_COPY &&
390                    (!direct_output || out_samples < in_samples)) {
391             /* if remapping channels during output copy, we may need to
392              * use an intermediate buffer in order to remap before adding
393              * samples to the output fifo */
394             av_dlog(avr, "[copy] %s to out_buffer\n", current_buffer->name);
395             ret = ff_audio_data_copy(avr->out_buffer, current_buffer,
396                                      &avr->ch_map_info);
397             if (ret < 0)
398                 return ret;
399             current_buffer = avr->out_buffer;
400         } else if (avr->in_copy_needed || avr->in_convert_needed) {
401             /* if needed, copy or convert input to in_buffer, and downmix if
402                applicable */
403             if (avr->in_convert_needed) {
404                 ret = ff_audio_data_realloc(avr->in_buffer,
405                                             current_buffer->nb_samples);
406                 if (ret < 0)
407                     return ret;
408                 av_dlog(avr, "[convert] %s to in_buffer\n", current_buffer->name);
409                 ret = ff_audio_convert(avr->ac_in, avr->in_buffer,
410                                        current_buffer);
411                 if (ret < 0)
412                     return ret;
413             } else {
414                 av_dlog(avr, "[copy] %s to in_buffer\n", current_buffer->name);
415                 ret = ff_audio_data_copy(avr->in_buffer, current_buffer,
416                                          avr->remap_point == REMAP_IN_COPY ?
417                                          &avr->ch_map_info : NULL);
418                 if (ret < 0)
419                     return ret;
420             }
421             ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);
422             if (avr->downmix_needed) {
423                 av_dlog(avr, "[downmix] in_buffer\n");
424                 ret = ff_audio_mix(avr->am, avr->in_buffer);
425                 if (ret < 0)
426                     return ret;
427             }
428             current_buffer = avr->in_buffer;
429         }
430     } else {
431         /* flush resampling buffer and/or output FIFO if input is NULL */
432         if (!avr->resample_needed)
433             return handle_buffered_output(avr, output ? &output_buffer : NULL,
434                                           NULL);
435         current_buffer = NULL;
436     }
437
438     if (avr->resample_needed) {
439         AudioData *resample_out;
440
441         if (!avr->out_convert_needed && direct_output && out_samples > 0)
442             resample_out = &output_buffer;
443         else
444             resample_out = avr->resample_out_buffer;
445         av_dlog(avr, "[resample] %s to %s\n",
446                 current_buffer ? current_buffer->name : "null",
447                 resample_out->name);
448         ret = ff_audio_resample(avr->resample, resample_out,
449                                 current_buffer);
450         if (ret < 0)
451             return ret;
452
453         /* if resampling did not produce any samples, just return 0 */
454         if (resample_out->nb_samples == 0) {
455             av_dlog(avr, "[end conversion]\n");
456             return 0;
457         }
458
459         current_buffer = resample_out;
460     }
461
462     if (avr->upmix_needed) {
463         av_dlog(avr, "[upmix] %s\n", current_buffer->name);
464         ret = ff_audio_mix(avr->am, current_buffer);
465         if (ret < 0)
466             return ret;
467     }
468
469     /* if we resampled or upmixed directly to output, return here */
470     if (current_buffer == &output_buffer) {
471         av_dlog(avr, "[end conversion]\n");
472         return current_buffer->nb_samples;
473     }
474
475     if (avr->out_convert_needed) {
476         if (direct_output && out_samples >= current_buffer->nb_samples) {
477             /* convert directly to output */
478             av_dlog(avr, "[convert] %s to output\n", current_buffer->name);
479             ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer);
480             if (ret < 0)
481                 return ret;
482
483             av_dlog(avr, "[end conversion]\n");
484             return output_buffer.nb_samples;
485         } else {
486             ret = ff_audio_data_realloc(avr->out_buffer,
487                                         current_buffer->nb_samples);
488             if (ret < 0)
489                 return ret;
490             av_dlog(avr, "[convert] %s to out_buffer\n", current_buffer->name);
491             ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
492                                    current_buffer);
493             if (ret < 0)
494                 return ret;
495             current_buffer = avr->out_buffer;
496         }
497     }
498
499     return handle_buffered_output(avr, output ? &output_buffer : NULL,
500                                   current_buffer);
501 }
502
503 int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
504                           int stride)
505 {
506     int in_channels, out_channels, i, o;
507
508     if (avr->am)
509         return ff_audio_mix_get_matrix(avr->am, matrix, stride);
510
511     in_channels  = av_get_channel_layout_nb_channels(avr->in_channel_layout);
512     out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
513
514     if ( in_channels <= 0 ||  in_channels > AVRESAMPLE_MAX_CHANNELS ||
515         out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
516         av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
517         return AVERROR(EINVAL);
518     }
519
520     if (!avr->mix_matrix) {
521         av_log(avr, AV_LOG_ERROR, "matrix is not set\n");
522         return AVERROR(EINVAL);
523     }
524
525     for (o = 0; o < out_channels; o++)
526         for (i = 0; i < in_channels; i++)
527             matrix[o * stride + i] = avr->mix_matrix[o * in_channels + i];
528
529     return 0;
530 }
531
532 int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
533                           int stride)
534 {
535     int in_channels, out_channels, i, o;
536
537     if (avr->am)
538         return ff_audio_mix_set_matrix(avr->am, matrix, stride);
539
540     in_channels  = av_get_channel_layout_nb_channels(avr->in_channel_layout);
541     out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
542
543     if ( in_channels <= 0 ||  in_channels > AVRESAMPLE_MAX_CHANNELS ||
544         out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
545         av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
546         return AVERROR(EINVAL);
547     }
548
549     if (avr->mix_matrix)
550         av_freep(&avr->mix_matrix);
551     avr->mix_matrix = av_malloc(in_channels * out_channels *
552                                 sizeof(*avr->mix_matrix));
553     if (!avr->mix_matrix)
554         return AVERROR(ENOMEM);
555
556     for (o = 0; o < out_channels; o++)
557         for (i = 0; i < in_channels; i++)
558             avr->mix_matrix[o * in_channels + i] = matrix[o * stride + i];
559
560     return 0;
561 }
562
563 int avresample_set_channel_mapping(AVAudioResampleContext *avr,
564                                    const int *channel_map)
565 {
566     ChannelMapInfo *info = &avr->ch_map_info;
567     int in_channels, ch, i;
568
569     in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
570     if (in_channels <= 0 ||  in_channels > AVRESAMPLE_MAX_CHANNELS) {
571         av_log(avr, AV_LOG_ERROR, "Invalid input channel layout\n");
572         return AVERROR(EINVAL);
573     }
574
575     memset(info, 0, sizeof(*info));
576     memset(info->input_map, -1, sizeof(info->input_map));
577
578     for (ch = 0; ch < in_channels; ch++) {
579         if (channel_map[ch] >= in_channels) {
580             av_log(avr, AV_LOG_ERROR, "Invalid channel map\n");
581             return AVERROR(EINVAL);
582         }
583         if (channel_map[ch] < 0) {
584             info->channel_zero[ch] =  1;
585             info->channel_map[ch]  = -1;
586             info->do_zero          =  1;
587         } else if (info->input_map[channel_map[ch]] >= 0) {
588             info->channel_copy[ch] = info->input_map[channel_map[ch]];
589             info->channel_map[ch]  = -1;
590             info->do_copy          =  1;
591         } else {
592             info->channel_map[ch]            = channel_map[ch];
593             info->input_map[channel_map[ch]] = ch;
594             info->do_remap                   =  1;
595         }
596     }
597     /* Fill-in unmapped input channels with unmapped output channels.
598        This is used when remapping during conversion from interleaved to
599        planar format. */
600     for (ch = 0, i = 0; ch < in_channels && i < in_channels; ch++, i++) {
601         while (ch < in_channels && info->input_map[ch] >= 0)
602             ch++;
603         while (i < in_channels && info->channel_map[i] >= 0)
604             i++;
605         if (ch >= in_channels || i >= in_channels)
606             break;
607         info->input_map[ch] = i;
608     }
609
610     avr->use_channel_map = 1;
611     return 0;
612 }
613
614 int avresample_available(AVAudioResampleContext *avr)
615 {
616     return av_audio_fifo_size(avr->out_fifo);
617 }
618
619 int avresample_get_out_samples(AVAudioResampleContext *avr, int in_nb_samples)
620 {
621     int64_t samples = avresample_get_delay(avr) + (int64_t)in_nb_samples;
622
623     if (avr->resample_needed) {
624         samples = av_rescale_rnd(samples,
625                                  avr->out_sample_rate,
626                                  avr->in_sample_rate,
627                                  AV_ROUND_UP);
628     }
629
630     samples += avresample_available(avr);
631
632     if (samples > INT_MAX)
633         return AVERROR(EINVAL);
634
635     return samples;
636 }
637
638 int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
639 {
640     if (!output)
641         return av_audio_fifo_drain(avr->out_fifo, nb_samples);
642     return av_audio_fifo_read(avr->out_fifo, (void**)output, nb_samples);
643 }
644
645 unsigned avresample_version(void)
646 {
647     return LIBAVRESAMPLE_VERSION_INT;
648 }
649
650 const char *avresample_license(void)
651 {
652 #define LICENSE_PREFIX "libavresample license: "
653     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
654 }
655
656 const char *avresample_configuration(void)
657 {
658     return LIBAV_CONFIGURATION;
659 }