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