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