fix swr_convert buffering of packed audio
[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     int ch;
364     if(in->planar){
365         for(ch=0; ch<out->ch_count; ch++)
366             out->ch[ch]= in->ch[ch] + count*out->bps;
367     }else{
368         for(ch=0; ch<out->ch_count; ch++)
369             out->ch[ch]= in->ch[0] + (ch + count*out->ch_count) * out->bps;
370     }
371 }
372
373 /**
374  *
375  * @return number of samples output per channel
376  */
377 static int resample(SwrContext *s, AudioData *out_param, int out_count,
378                              const AudioData * in_param, int in_count){
379     AudioData in, out, tmp;
380     int ret_sum=0;
381     int border=0;
382
383     tmp=out=*out_param;
384     in =  *in_param;
385
386     do{
387         int ret, size, consumed;
388         if(!s->resample_in_constraint && s->in_buffer_count){
389             buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
390             ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
391             out_count -= ret;
392             ret_sum += ret;
393             buf_set(&out, &out, ret);
394             s->in_buffer_count -= consumed;
395             s->in_buffer_index += consumed;
396
397             if(!in_count)
398                 break;
399             if(s->in_buffer_count <= border){
400                 buf_set(&in, &in, -s->in_buffer_count);
401                 in_count += s->in_buffer_count;
402                 s->in_buffer_count=0;
403                 s->in_buffer_index=0;
404                 border = 0;
405             }
406         }
407
408         if(in_count && !s->in_buffer_count){
409             s->in_buffer_index=0;
410             ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
411             out_count -= ret;
412             ret_sum += ret;
413             buf_set(&out, &out, ret);
414             in_count -= consumed;
415             buf_set(&in, &in, consumed);
416         }
417
418         //TODO is this check sane considering the advanced copy avoidance below
419         size= s->in_buffer_index + s->in_buffer_count + in_count;
420         if(   size > s->in_buffer.count
421            && s->in_buffer_count + in_count <= s->in_buffer_index){
422             buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
423             copy(&s->in_buffer, &tmp, s->in_buffer_count);
424             s->in_buffer_index=0;
425         }else
426             if((ret=realloc_audio(&s->in_buffer, size)) < 0)
427                 return ret;
428
429         if(in_count){
430             int count= in_count;
431             if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
432
433             buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
434             copy(&tmp, &in, /*in_*/count);
435             s->in_buffer_count += count;
436             in_count -= count;
437             border += count;
438             buf_set(&in, &in, count);
439             s->resample_in_constraint= 0;
440             if(s->in_buffer_count != count || in_count)
441                 continue;
442         }
443         break;
444     }while(1);
445
446     s->resample_in_constraint= !!out_count;
447
448     return ret_sum;
449 }
450
451 static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,
452                                                       AudioData *in , int  in_count){
453     AudioData *postin, *midbuf, *preout;
454     int ret/*, in_max*/;
455     AudioData preout_tmp, midbuf_tmp;
456
457     if(s->full_convert){
458         av_assert0(!s->resample);
459         swri_audio_convert(s->full_convert, out, in, in_count);
460         return out_count;
461     }
462
463 //     in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
464 //     in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
465
466     if((ret=realloc_audio(&s->postin, in_count))<0)
467         return ret;
468     if(s->resample_first){
469         av_assert0(s->midbuf.ch_count == s->used_ch_count);
470         if((ret=realloc_audio(&s->midbuf, out_count))<0)
471             return ret;
472     }else{
473         av_assert0(s->midbuf.ch_count ==  s->out.ch_count);
474         if((ret=realloc_audio(&s->midbuf,  in_count))<0)
475             return ret;
476     }
477     if((ret=realloc_audio(&s->preout, out_count))<0)
478         return ret;
479
480     postin= &s->postin;
481
482     midbuf_tmp= s->midbuf;
483     midbuf= &midbuf_tmp;
484     preout_tmp= s->preout;
485     preout= &preout_tmp;
486
487     if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar)
488         postin= in;
489
490     if(s->resample_first ? !s->resample : !s->rematrix)
491         midbuf= postin;
492
493     if(s->resample_first ? !s->rematrix : !s->resample)
494         preout= midbuf;
495
496     if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
497         if(preout==in){
498             out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant
499             av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
500             copy(out, in, out_count);
501             return out_count;
502         }
503         else if(preout==postin) preout= midbuf= postin= out;
504         else if(preout==midbuf) preout= midbuf= out;
505         else                    preout= out;
506     }
507
508     if(in != postin){
509         swri_audio_convert(s->in_convert, postin, in, in_count);
510     }
511
512     if(s->resample_first){
513         if(postin != midbuf)
514             out_count= resample(s, midbuf, out_count, postin, in_count);
515         if(midbuf != preout)
516             swri_rematrix(s, preout, midbuf, out_count, preout==out);
517     }else{
518         if(postin != midbuf)
519             swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
520         if(midbuf != preout)
521             out_count= resample(s, preout, out_count, midbuf, in_count);
522     }
523
524     if(preout != out && out_count){
525         if(s->dither_method){
526             int ch;
527             int dither_count= FFMAX(out_count, 1<<16);
528             av_assert0(preout != in);
529
530             if((ret=realloc_audio(&s->dither, dither_count))<0)
531                 return ret;
532             if(ret)
533                 for(ch=0; ch<s->dither.ch_count; ch++)
534                     swri_get_dither(s, s->dither.ch[ch], s->dither.count, 12345678913579<<ch, s->out_sample_fmt, s->int_sample_fmt);
535             av_assert0(s->dither.ch_count == preout->ch_count);
536
537             if(s->dither_pos + out_count > s->dither.count)
538                 s->dither_pos = 0;
539             for(ch=0; ch<preout->ch_count; ch++)
540                 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);
541
542             s->dither_pos += out_count;
543         }
544 //FIXME packed doesnt need more than 1 chan here!
545         swri_audio_convert(s->out_convert, out, preout, out_count);
546     }
547     return out_count;
548 }
549
550 int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
551                                 const uint8_t *in_arg [SWR_CH_MAX], int  in_count){
552     AudioData * in= &s->in;
553     AudioData *out= &s->out;
554
555     if(!in_arg){
556         if(s->in_buffer_count){
557             if (s->resample && !s->flushed) {
558                 AudioData *a= &s->in_buffer;
559                 int i, j, ret;
560                 if((ret=realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0)
561                     return ret;
562                 av_assert0(a->planar);
563                 for(i=0; i<a->ch_count; i++){
564                     for(j=0; j<s->in_buffer_count; j++){
565                         memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j  )*a->bps,
566                             a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
567                     }
568                 }
569                 s->in_buffer_count += (s->in_buffer_count+1)/2;
570                 s->resample_in_constraint = 0;
571                 s->flushed = 1;
572             }
573         }else{
574             return 0;
575         }
576     }else
577         fill_audiodata(in ,  (void*)in_arg);
578
579     fill_audiodata(out, out_arg);
580
581     if(s->resample){
582         return swr_convert_internal(s, out, out_count, in, in_count);
583     }else{
584         AudioData tmp= *in;
585         int ret2=0;
586         int ret, size;
587         size = FFMIN(out_count, s->in_buffer_count);
588         if(size){
589             buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
590             ret= swr_convert_internal(s, out, size, &tmp, size);
591             if(ret<0)
592                 return ret;
593             ret2= ret;
594             s->in_buffer_count -= ret;
595             s->in_buffer_index += ret;
596             buf_set(out, out, ret);
597             out_count -= ret;
598             if(!s->in_buffer_count)
599                 s->in_buffer_index = 0;
600         }
601
602         if(in_count){
603             size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
604
605             if(in_count > out_count) { //FIXME move after swr_convert_internal
606                 if(   size > s->in_buffer.count
607                 && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
608                     buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
609                     copy(&s->in_buffer, &tmp, s->in_buffer_count);
610                     s->in_buffer_index=0;
611                 }else
612                     if((ret=realloc_audio(&s->in_buffer, size)) < 0)
613                         return ret;
614             }
615
616             if(out_count){
617                 size = FFMIN(in_count, out_count);
618                 ret= swr_convert_internal(s, out, size, in, size);
619                 if(ret<0)
620                     return ret;
621                 buf_set(in, in, ret);
622                 in_count -= ret;
623                 ret2 += ret;
624             }
625             if(in_count){
626                 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
627                 copy(&tmp, in, in_count);
628                 s->in_buffer_count += in_count;
629             }
630         }
631         return ret2;
632     }
633 }
634