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