a1dcf2a203ac090d36d85aa85f1b7e3c57771ece
[ffmpeg.git] / libswresample / swresample.c
1 /*
2  * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
3  *
4  * This file is part of libswresample
5  *
6  * libswresample 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  * libswresample 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 libswresample; 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/opt.h"
22 #include "swresample_internal.h"
23 #include "audioconvert.h"
24 #include "libavutil/avassert.h"
25 #include "libavutil/audioconvert.h"
26
27 #define  C30DB  M_SQRT2
28 #define  C15DB  1.189207115
29 #define C__0DB  1.0
30 #define C_15DB  0.840896415
31 #define C_30DB  M_SQRT1_2
32 #define C_45DB  0.594603558
33 #define C_60DB  0.5
34
35
36 //TODO split options array out?
37 #define OFFSET(x) offsetof(SwrContext,x)
38 static const AVOption options[]={
39 {"ich",  "input channel count", OFFSET( in.ch_count   ), AV_OPT_TYPE_INT, {.dbl=2}, 0, SWR_CH_MAX, 0},
40 {"och", "output channel count", OFFSET(out.ch_count   ), AV_OPT_TYPE_INT, {.dbl=2}, 0, SWR_CH_MAX, 0},
41 {"uch",   "used channel count", OFFSET(used_ch_count  ), AV_OPT_TYPE_INT, {.dbl=0}, 0, SWR_CH_MAX, 0},
42 {"isr",  "input sample rate"  , OFFSET( in_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
43 {"osr", "output sample rate"  , OFFSET(out_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
44 //{"ip" ,  "input planar"       , OFFSET( in.planar     ), AV_OPT_TYPE_INT, {.dbl=0},    0,       1, 0},
45 //{"op" , "output planar"       , OFFSET(out.planar     ), AV_OPT_TYPE_INT, {.dbl=0},    0,       1, 0},
46 {"isf",  "input sample format", OFFSET( in_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_S16}, 0, AV_SAMPLE_FMT_NB-1+256, 0},
47 {"osf", "output sample format", OFFSET(out_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_S16}, 0, AV_SAMPLE_FMT_NB-1+256, 0},
48 {"tsf", "internal sample format", OFFSET(int_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_FLT, 0},
49 {"icl",  "input channel layout" , OFFSET( in_ch_layout), AV_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"},
50 {"ocl",  "output channel layout", OFFSET(out_ch_layout), AV_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"},
51 {"clev", "center mix level"     , OFFSET(clev)         , AV_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0},
52 {"slev", "sourround mix level"  , OFFSET(slev)         , AV_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0},
53 {"rmvol", "rematrix volume"     , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, -1000, 1000, 0},
54 {"flags", NULL                  , OFFSET(flags)        , AV_OPT_TYPE_FLAGS, {.dbl=0}, 0,  UINT_MAX, 0, "flags"},
55 {"res", "force resampling", 0, AV_OPT_TYPE_CONST, {.dbl=SWR_FLAG_RESAMPLE}, INT_MIN, INT_MAX, 0, "flags"},
56
57 {0}
58 };
59
60 static const char* context_to_name(void* ptr) {
61     return "SWR";
62 }
63
64 static const AVClass av_class = {
65     .class_name                = "SwrContext",
66     .item_name                 = context_to_name,
67     .option                    = options,
68     .version                   = LIBAVUTIL_VERSION_INT,
69     .log_level_offset_offset   = OFFSET(log_level_offset),
70     .parent_log_context_offset = OFFSET(log_ctx),
71 };
72
73 unsigned swresample_version(void)
74 {
75     av_assert0(LIBSWRESAMPLE_VERSION_MICRO >= 100);
76     return LIBSWRESAMPLE_VERSION_INT;
77 }
78
79 const char *swresample_configuration(void)
80 {
81     return FFMPEG_CONFIGURATION;
82 }
83
84 const char *swresample_license(void)
85 {
86 #define LICENSE_PREFIX "libswresample license: "
87     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
88 }
89
90 int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map){
91     if(!s || s->in_convert) // s needs to be allocated but not initialized
92         return AVERROR(EINVAL);
93     s->channel_map = channel_map;
94     return 0;
95 }
96
97 struct SwrContext *swr_alloc(void){
98     SwrContext *s= av_mallocz(sizeof(SwrContext));
99     if(s){
100         s->av_class= &av_class;
101         av_opt_set_defaults(s);
102     }
103     return s;
104 }
105
106 struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
107                                       int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
108                                       int64_t  in_ch_layout, enum AVSampleFormat  in_sample_fmt, int  in_sample_rate,
109                                       int log_offset, void *log_ctx){
110     if(!s) s= swr_alloc();
111     if(!s) return NULL;
112
113     s->log_level_offset= log_offset;
114     s->log_ctx= log_ctx;
115
116     av_opt_set_int(s, "ocl", out_ch_layout,   0);
117     av_opt_set_int(s, "osf", out_sample_fmt,  0);
118     av_opt_set_int(s, "osr", out_sample_rate, 0);
119     av_opt_set_int(s, "icl", in_ch_layout,    0);
120     av_opt_set_int(s, "isf", in_sample_fmt,   0);
121     av_opt_set_int(s, "isr", in_sample_rate,  0);
122     av_opt_set_int(s, "tsf", AV_SAMPLE_FMT_S16, 0);
123     av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> in_ch_layout), 0);
124     av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->out_ch_layout), 0);
125     av_opt_set_int(s, "uch", 0, 0);
126     return s;
127 }
128
129
130 static void free_temp(AudioData *a){
131     av_free(a->data);
132     memset(a, 0, sizeof(*a));
133 }
134
135 void swr_free(SwrContext **ss){
136     SwrContext *s= *ss;
137     if(s){
138         free_temp(&s->postin);
139         free_temp(&s->midbuf);
140         free_temp(&s->preout);
141         free_temp(&s->in_buffer);
142         swri_audio_convert_free(&s-> in_convert);
143         swri_audio_convert_free(&s->out_convert);
144         swri_audio_convert_free(&s->full_convert);
145         swri_resample_free(&s->resample);
146     }
147
148     av_freep(ss);
149 }
150
151 int swr_init(struct SwrContext *s){
152     s->in_buffer_index= 0;
153     s->in_buffer_count= 0;
154     s->resample_in_constraint= 0;
155     free_temp(&s->postin);
156     free_temp(&s->midbuf);
157     free_temp(&s->preout);
158     free_temp(&s->in_buffer);
159     swri_audio_convert_free(&s-> in_convert);
160     swri_audio_convert_free(&s->out_convert);
161     swri_audio_convert_free(&s->full_convert);
162
163     s->flushed = 0;
164
165     s-> in.planar= av_sample_fmt_is_planar(s-> in_sample_fmt);
166     s->out.planar= av_sample_fmt_is_planar(s->out_sample_fmt);
167     s-> in_sample_fmt= av_get_alt_sample_fmt(s-> in_sample_fmt, 0);
168     s->out_sample_fmt= av_get_alt_sample_fmt(s->out_sample_fmt, 0);
169
170     if(s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
171         av_log(s, AV_LOG_ERROR, "Requested input sample format %d is invalid\n", s->in_sample_fmt);
172         return AVERROR(EINVAL);
173     }
174     if(s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
175         av_log(s, AV_LOG_ERROR, "Requested output sample format %d is invalid\n", s->out_sample_fmt);
176         return AVERROR(EINVAL);
177     }
178
179     if(   s->int_sample_fmt != AV_SAMPLE_FMT_S16
180         &&s->int_sample_fmt != AV_SAMPLE_FMT_FLT){
181         av_log(s, AV_LOG_ERROR, "Requested sample format %s is not supported internally, only float & S16 is supported\n", av_get_sample_fmt_name(s->int_sample_fmt));
182         return AVERROR(EINVAL);
183     }
184
185     //FIXME should we allow/support using FLT on material that doesnt need it ?
186     if(s->in_sample_fmt <= AV_SAMPLE_FMT_S16 || s->int_sample_fmt==AV_SAMPLE_FMT_S16){
187         s->int_sample_fmt= AV_SAMPLE_FMT_S16;
188     }else
189         s->int_sample_fmt= AV_SAMPLE_FMT_FLT;
190
191
192     if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
193         s->resample = swri_resample_init(s->resample, s->out_sample_rate, s->in_sample_rate, 16, 10, 0, 0.8);
194     }else
195         swri_resample_free(&s->resample);
196     if(s->int_sample_fmt != AV_SAMPLE_FMT_S16 && s->resample){
197         av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16 currently\n"); //FIXME
198         return -1;
199     }
200
201     if(!s->used_ch_count)
202         s->used_ch_count= s->in.ch_count;
203
204     if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
205         av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n");
206         s-> in_ch_layout= 0;
207     }
208
209     if(!s-> in_ch_layout)
210         s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count);
211     if(!s->out_ch_layout)
212         s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
213
214     s->rematrix= s->out_ch_layout  !=s->in_ch_layout || s->rematrix_volume!=1.0 ||
215                  s->rematrix_custom;
216
217 #define RSC 1 //FIXME finetune
218     if(!s-> in.ch_count)
219         s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
220     if(!s->used_ch_count)
221         s->used_ch_count= s->in.ch_count;
222     if(!s->out.ch_count)
223         s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
224
225     if(!s-> in.ch_count){
226         av_assert0(!s->in_ch_layout);
227         av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
228         return -1;
229     }
230
231     if ((!s->out_ch_layout || !s->in_ch_layout) && s->used_ch_count != s->out.ch_count && !s->rematrix_custom) {
232         av_log(s, AV_LOG_ERROR, "Rematrix is needed but there is not enough information to do it\n");
233         return -1;
234     }
235
236 av_assert0(s->used_ch_count);
237 av_assert0(s->out.ch_count);
238     s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
239
240     s-> in.bps= av_get_bytes_per_sample(s-> in_sample_fmt);
241     s->int_bps= av_get_bytes_per_sample(s->int_sample_fmt);
242     s->out.bps= av_get_bytes_per_sample(s->out_sample_fmt);
243     s->in_buffer= s->in;
244
245     if(!s->resample && !s->rematrix && !s->channel_map){
246         s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
247                                                    s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
248         return 0;
249     }
250
251     s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
252                                              s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0);
253     s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
254                                              s->int_sample_fmt, s->out.ch_count, NULL, 0);
255
256
257     s->postin= s->in;
258     s->preout= s->out;
259     s->midbuf= s->in;
260
261     if(s->channel_map){
262         s->postin.ch_count=
263         s->midbuf.ch_count= s->used_ch_count;
264         if(s->resample)
265             s->in_buffer.ch_count= s->used_ch_count;
266     }
267     if(!s->resample_first){
268         s->midbuf.ch_count= s->out.ch_count;
269         if(s->resample)
270             s->in_buffer.ch_count = s->out.ch_count;
271     }
272
273     s->postin.bps    = s->midbuf.bps    = s->preout.bps    =  s->int_bps;
274     s->postin.planar = s->midbuf.planar = s->preout.planar =  1;
275
276     if(s->resample){
277         s->in_buffer.bps    = s->int_bps;
278         s->in_buffer.planar = 1;
279     }
280
281     if(s->rematrix)
282         return swri_rematrix_init(s);
283
284     return 0;
285 }
286
287 static int realloc_audio(AudioData *a, int count){
288     int i, countb;
289     AudioData old;
290
291     if(a->count >= count)
292         return 0;
293
294     count*=2;
295
296     countb= FFALIGN(count*a->bps, 32);
297     old= *a;
298
299     av_assert0(a->planar);
300     av_assert0(a->bps);
301     av_assert0(a->ch_count);
302
303     a->data= av_malloc(countb*a->ch_count);
304     if(!a->data)
305         return AVERROR(ENOMEM);
306     for(i=0; i<a->ch_count; i++){
307         a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
308         if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
309     }
310     av_free(old.data);
311     a->count= count;
312
313     return 1;
314 }
315
316 static void copy(AudioData *out, AudioData *in,
317                  int count){
318     av_assert0(out->planar == in->planar);
319     av_assert0(out->bps == in->bps);
320     av_assert0(out->ch_count == in->ch_count);
321     if(out->planar){
322         int ch;
323         for(ch=0; ch<out->ch_count; ch++)
324             memcpy(out->ch[ch], in->ch[ch], count*out->bps);
325     }else
326         memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
327 }
328
329 static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
330     int i;
331     if(out->planar){
332         for(i=0; i<out->ch_count; i++)
333             out->ch[i]= in_arg[i];
334     }else{
335         for(i=0; i<out->ch_count; i++)
336             out->ch[i]= in_arg[0] + i*out->bps;
337     }
338 }
339
340 /**
341  *
342  * out may be equal in.
343  */
344 static void buf_set(AudioData *out, AudioData *in, int count){
345     if(in->planar){
346         int ch;
347         for(ch=0; ch<out->ch_count; ch++)
348             out->ch[ch]= in->ch[ch] + count*out->bps;
349     }else
350         out->ch[0]= in->ch[0] + count*out->ch_count*out->bps;
351 }
352
353 /**
354  *
355  * @return number of samples output per channel
356  */
357 static int resample(SwrContext *s, AudioData *out_param, int out_count,
358                              const AudioData * in_param, int in_count){
359     AudioData in, out, tmp;
360     int ret_sum=0;
361     int border=0;
362
363     tmp=out=*out_param;
364     in =  *in_param;
365
366     do{
367         int ret, size, consumed;
368         if(!s->resample_in_constraint && s->in_buffer_count){
369             buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
370             ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
371             out_count -= ret;
372             ret_sum += ret;
373             buf_set(&out, &out, ret);
374             s->in_buffer_count -= consumed;
375             s->in_buffer_index += consumed;
376
377             if(!in_count)
378                 break;
379             if(s->in_buffer_count <= border){
380                 buf_set(&in, &in, -s->in_buffer_count);
381                 in_count += s->in_buffer_count;
382                 s->in_buffer_count=0;
383                 s->in_buffer_index=0;
384                 border = 0;
385             }
386         }
387
388         if(in_count && !s->in_buffer_count){
389             s->in_buffer_index=0;
390             ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
391             out_count -= ret;
392             ret_sum += ret;
393             buf_set(&out, &out, ret);
394             in_count -= consumed;
395             buf_set(&in, &in, consumed);
396         }
397
398         //TODO is this check sane considering the advanced copy avoidance below
399         size= s->in_buffer_index + s->in_buffer_count + in_count;
400         if(   size > s->in_buffer.count
401            && s->in_buffer_count + in_count <= s->in_buffer_index){
402             buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
403             copy(&s->in_buffer, &tmp, s->in_buffer_count);
404             s->in_buffer_index=0;
405         }else
406             if((ret=realloc_audio(&s->in_buffer, size)) < 0)
407                 return ret;
408
409         if(in_count){
410             int count= in_count;
411             if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
412
413             buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
414             copy(&tmp, &in, /*in_*/count);
415             s->in_buffer_count += count;
416             in_count -= count;
417             border += count;
418             buf_set(&in, &in, count);
419             s->resample_in_constraint= 0;
420             if(s->in_buffer_count != count || in_count)
421                 continue;
422         }
423         break;
424     }while(1);
425
426     s->resample_in_constraint= !!out_count;
427
428     return ret_sum;
429 }
430
431 static int swr_convert_internal(struct SwrContext *s, AudioData *out[SWR_CH_MAX], int out_count,
432                                                       AudioData *in [SWR_CH_MAX], int  in_count){
433     AudioData *postin, *midbuf, *preout;
434     int ret/*, in_max*/;
435     AudioData preout_tmp, midbuf_tmp;
436
437     if(s->full_convert){
438         av_assert0(!s->resample);
439         swri_audio_convert(s->full_convert, out, in, in_count);
440         return out_count;
441     }
442
443 //     in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
444 //     in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
445
446     if((ret=realloc_audio(&s->postin, in_count))<0)
447         return ret;
448     if(s->resample_first){
449         av_assert0(s->midbuf.ch_count == s->used_ch_count);
450         if((ret=realloc_audio(&s->midbuf, out_count))<0)
451             return ret;
452     }else{
453         av_assert0(s->midbuf.ch_count ==  s->out.ch_count);
454         if((ret=realloc_audio(&s->midbuf,  in_count))<0)
455             return ret;
456     }
457     if((ret=realloc_audio(&s->preout, out_count))<0)
458         return ret;
459
460     postin= &s->postin;
461
462     midbuf_tmp= s->midbuf;
463     midbuf= &midbuf_tmp;
464     preout_tmp= s->preout;
465     preout= &preout_tmp;
466
467     if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar)
468         postin= in;
469
470     if(s->resample_first ? !s->resample : !s->rematrix)
471         midbuf= postin;
472
473     if(s->resample_first ? !s->rematrix : !s->resample)
474         preout= midbuf;
475
476     if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
477         if(preout==in){
478             out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant
479             av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
480             copy(out, in, out_count);
481             return out_count;
482         }
483         else if(preout==postin) preout= midbuf= postin= out;
484         else if(preout==midbuf) preout= midbuf= out;
485         else                    preout= out;
486     }
487
488     if(in != postin){
489         swri_audio_convert(s->in_convert, postin, in, in_count);
490     }
491
492     if(s->resample_first){
493         if(postin != midbuf)
494             out_count= resample(s, midbuf, out_count, postin, in_count);
495         if(midbuf != preout)
496             swri_rematrix(s, preout, midbuf, out_count, preout==out);
497     }else{
498         if(postin != midbuf)
499             swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
500         if(midbuf != preout)
501             out_count= resample(s, preout, out_count, midbuf, in_count);
502     }
503
504     if(preout != out && out_count){
505 //FIXME packed doesnt need more than 1 chan here!
506         swri_audio_convert(s->out_convert, out, preout, out_count);
507     }
508     return out_count;
509 }
510
511 int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
512                                 const uint8_t *in_arg [SWR_CH_MAX], int  in_count){
513     AudioData * in= &s->in;
514     AudioData *out= &s->out;
515
516     if(!in_arg){
517         if(s->in_buffer_count){
518             if (s->resample && !s->flushed) {
519                 AudioData *a= &s->in_buffer;
520                 int i, j, ret;
521                 if((ret=realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0)
522                     return ret;
523                 av_assert0(a->planar);
524                 for(i=0; i<a->ch_count; i++){
525                     for(j=0; j<s->in_buffer_count; j++){
526                         memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j  )*a->bps,
527                             a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
528                     }
529                 }
530                 s->in_buffer_count += (s->in_buffer_count+1)/2;
531                 s->resample_in_constraint = 0;
532                 s->flushed = 1;
533             }
534         }else{
535             return 0;
536         }
537     }else
538         fill_audiodata(in ,  (void*)in_arg);
539
540     fill_audiodata(out, out_arg);
541
542     if(s->resample){
543         return swr_convert_internal(s, out, out_count, in, in_count);
544     }else{
545         AudioData tmp= *in;
546         int ret2=0;
547         int ret, size;
548         int in_buffer_count= s->in_buffer_count;
549         size = FFMIN(out_count, s->in_buffer_count);
550         if(size){
551             buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
552             ret= swr_convert_internal(s, out, size, &tmp, size);
553             if(ret<0)
554                 return ret;
555             ret2= ret;
556             s->in_buffer_count -= ret;
557             s->in_buffer_index += ret;
558             buf_set(out, out, ret);
559             out_count -= ret;
560             if(!s->in_buffer_count)
561                 s->in_buffer_index = 0;
562         }
563
564         if(in_count){
565             size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
566
567             if(in_count > out_count) { //FIXME move after swr_convert_internal
568                 if(   size > s->in_buffer.count
569                 && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
570                     buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
571                     copy(&s->in_buffer, &tmp, s->in_buffer_count);
572                     s->in_buffer_index=0;
573                 }else
574                     if((ret=realloc_audio(&s->in_buffer, size)) < 0)
575                         return ret;
576             }
577
578             if(out_count){
579                 size = FFMIN(in_count, out_count);
580                 ret= swr_convert_internal(s, out, size, in, size);
581                 if(ret<0)
582                     return ret;
583                 buf_set(in, in, ret);
584                 in_count -= ret;
585                 ret2 += ret;
586             }
587             if(in_count){
588                 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
589                 copy(&tmp, in, in_count);
590                 s->in_buffer_count += in_count;
591             }
592         }
593         return ret2;
594     }
595 }
596