static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,

                                                      AudioData *in , int  in_count){

    AudioData *postin, *midbuf, *preout;

    int ret/*, in_max*/;

    AudioData preout_tmp, midbuf_tmp;



    if(s->full_convert){

        av_assert0(!s->resample);

        swri_audio_convert(s->full_convert, out, in, in_count);

        return out_count;

    }



//     in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;

//     in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);



    if((ret=swri_realloc_audio(&s->postin, in_count))<0)

        return ret;

    if(s->resample_first){

        av_assert0(s->midbuf.ch_count == s->used_ch_count);

        if((ret=swri_realloc_audio(&s->midbuf, out_count))<0)

            return ret;

    }else{

        av_assert0(s->midbuf.ch_count ==  s->out.ch_count);

        if((ret=swri_realloc_audio(&s->midbuf,  in_count))<0)

            return ret;

    }

    if((ret=swri_realloc_audio(&s->preout, out_count))<0)

        return ret;



    postin= &s->postin;



    midbuf_tmp= s->midbuf;

    midbuf= &midbuf_tmp;

    preout_tmp= s->preout;

    preout= &preout_tmp;



    if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar && !s->channel_map)

        postin= in;



    if(s->resample_first ? !s->resample : !s->rematrix)

        midbuf= postin;



    if(s->resample_first ? !s->rematrix : !s->resample)

        preout= midbuf;



    if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar

       && !(s->out_sample_fmt==AV_SAMPLE_FMT_S32P && (s->dither.output_sample_bits&31))){

        if(preout==in){

            out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant

            av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though

            copy(out, in, out_count);

            return out_count;

        }

        else if(preout==postin) preout= midbuf= postin= out;

        else if(preout==midbuf) preout= midbuf= out;

        else                    preout= out;

    }



    if(in != postin){

        swri_audio_convert(s->in_convert, postin, in, in_count);

    }



    if(s->resample_first){

        if(postin != midbuf)

            out_count= resample(s, midbuf, out_count, postin, in_count);

        if(midbuf != preout)

            swri_rematrix(s, preout, midbuf, out_count, preout==out);

    }else{

        if(postin != midbuf)

            swri_rematrix(s, midbuf, postin, in_count, midbuf==out);

        if(midbuf != preout)

            out_count= resample(s, preout, out_count, midbuf, in_count);

    }



    if(preout != out && out_count){

        AudioData *conv_src = preout;

        if(s->dither.method){

            int ch;

            int dither_count= FFMAX(out_count, 1<<16);



            if (preout == in) {

                conv_src = &s->dither.temp;

                if((ret=swri_realloc_audio(&s->dither.temp, dither_count))<0)

                    return ret;

            }



            if((ret=swri_realloc_audio(&s->dither.noise, dither_count))<0)

                return ret;

            if(ret)

                for(ch=0; ch<s->dither.noise.ch_count; ch++)

                    if((ret=swri_get_dither(s, s->dither.noise.ch[ch], s->dither.noise.count, 12345678913579<<ch, s->dither.noise.fmt))<0)

                        return ret;

            av_assert0(s->dither.noise.ch_count == preout->ch_count);



            if(s->dither.noise_pos + out_count > s->dither.noise.count)

                s->dither.noise_pos = 0;



            if (s->dither.method < SWR_DITHER_NS){

                if (s->mix_2_1_simd) {

                    int len1= out_count&~15;

                    int off = len1 * preout->bps;



                    if(len1)

                        for(ch=0; ch<preout->ch_count; ch++)

                            s->mix_2_1_simd(conv_src->ch[ch], preout->ch[ch], s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos, s->native_simd_one, 0, 0, len1);

                    if(out_count != len1)

                        for(ch=0; ch<preout->ch_count; ch++)

                            s->mix_2_1_f(conv_src->ch[ch] + off, preout->ch[ch] + off, s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos + off + len1, s->native_one, 0, 0, out_count - len1);

                } else {

                    for(ch=0; ch<preout->ch_count; ch++)

                        s->mix_2_1_f(conv_src->ch[ch], preout->ch[ch], s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos, s->native_one, 0, 0, out_count);

                }

            } else {

                switch(s->int_sample_fmt) {

                case AV_SAMPLE_FMT_S16P :swri_noise_shaping_int16(s, conv_src, preout, &s->dither.noise, out_count); break;

                case AV_SAMPLE_FMT_S32P :swri_noise_shaping_int32(s, conv_src, preout, &s->dither.noise, out_count); break;

                case AV_SAMPLE_FMT_FLTP :swri_noise_shaping_float(s, conv_src, preout, &s->dither.noise, out_count); break;

                case AV_SAMPLE_FMT_DBLP :swri_noise_shaping_double(s,conv_src, preout, &s->dither.noise, out_count); break;

                }

            }

            s->dither.noise_pos += out_count;

        }

//FIXME packed doesn't need more than 1 chan here!

        swri_audio_convert(s->out_convert, out, conv_src, out_count);

    }

    return out_count;

}

static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,

                                                      AudioData *in , int  in_count){

    AudioData *postin, *midbuf, *preout;

    int ret;

    AudioData preout_tmp, midbuf_tmp;



    if(s->full_convert){

        av_assert0(!s->resample);

        swri_audio_convert(s->full_convert, out, in, in_count);

        return out_count;

    }









    if((ret=swri_realloc_audio(&s->postin, in_count))<0)

        return ret;

    if(s->resample_first){

        av_assert0(s->midbuf.ch_count == s->used_ch_count);

        if((ret=swri_realloc_audio(&s->midbuf, out_count))<0)

            return ret;

    }else{

        av_assert0(s->midbuf.ch_count ==  s->out.ch_count);

        if((ret=swri_realloc_audio(&s->midbuf,  in_count))<0)

            return ret;

    }

    if((ret=swri_realloc_audio(&s->preout, out_count))<0)

        return ret;



    postin= &s->postin;



    midbuf_tmp= s->midbuf;

    midbuf= &midbuf_tmp;

    preout_tmp= s->preout;

    preout= &preout_tmp;



    if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar && !s->channel_map)

        postin= in;



    if(s->resample_first ? !s->resample : !s->rematrix)

        midbuf= postin;



    if(s->resample_first ? !s->rematrix : !s->resample)

        preout= midbuf;



    if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar

       && !(s->out_sample_fmt==AV_SAMPLE_FMT_S32P && (s->dither.output_sample_bits&31))){

        if(preout==in){

            out_count= FFMIN(out_count, in_count); 

            av_assert0(s->in.planar); 

            copy(out, in, out_count);

            return out_count;

        }

        else if(preout==postin) preout= midbuf= postin= out;

        else if(preout==midbuf) preout= midbuf= out;

        else                    preout= out;

    }



    if(in != postin){

        swri_audio_convert(s->in_convert, postin, in, in_count);

    }



    if(s->resample_first){

        if(postin != midbuf)

            out_count= resample(s, midbuf, out_count, postin, in_count);

        if(midbuf != preout)

            swri_rematrix(s, preout, midbuf, out_count, preout==out);

    }else{

        if(postin != midbuf)

            swri_rematrix(s, midbuf, postin, in_count, midbuf==out);

        if(midbuf != preout)

            out_count= resample(s, preout, out_count, midbuf, in_count);

    }



    if(preout != out && out_count){

        AudioData *conv_src = preout;

        if(s->dither.method){

            int ch;

            int dither_count= FFMAX(out_count, 1<<16);



            if (preout == in) {

                conv_src = &s->dither.temp;

                if((ret=swri_realloc_audio(&s->dither.temp, dither_count))<0)

                    return ret;

            }



            if((ret=swri_realloc_audio(&s->dither.noise, dither_count))<0)

                return ret;

            if(ret)

                for(ch=0; ch<s->dither.noise.ch_count; ch++)

                    if((ret=swri_get_dither(s, s->dither.noise.ch[ch], s->dither.noise.count, 12345678913579<<ch, s->dither.noise.fmt))<0)

                        return ret;

            av_assert0(s->dither.noise.ch_count == preout->ch_count);



            if(s->dither.noise_pos + out_count > s->dither.noise.count)

                s->dither.noise_pos = 0;



            if (s->dither.method < SWR_DITHER_NS){

                if (s->mix_2_1_simd) {

                    int len1= out_count&~15;

                    int off = len1 * preout->bps;



                    if(len1)

                        for(ch=0; ch<preout->ch_count; ch++)

                            s->mix_2_1_simd(conv_src->ch[ch], preout->ch[ch], s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos, s->native_simd_one, 0, 0, len1);

                    if(out_count != len1)

                        for(ch=0; ch<preout->ch_count; ch++)

                            s->mix_2_1_f(conv_src->ch[ch] + off, preout->ch[ch] + off, s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos + off + len1, s->native_one, 0, 0, out_count - len1);

                } else {

                    for(ch=0; ch<preout->ch_count; ch++)

                        s->mix_2_1_f(conv_src->ch[ch], preout->ch[ch], s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos, s->native_one, 0, 0, out_count);

                }

            } else {

                switch(s->int_sample_fmt) {

                case AV_SAMPLE_FMT_S16P :swri_noise_shaping_int16(s, conv_src, preout, &s->dither.noise, out_count); break;

                case AV_SAMPLE_FMT_S32P :swri_noise_shaping_int32(s, conv_src, preout, &s->dither.noise, out_count); break;

                case AV_SAMPLE_FMT_FLTP :swri_noise_shaping_float(s, conv_src, preout, &s->dither.noise, out_count); break;

                case AV_SAMPLE_FMT_DBLP :swri_noise_shaping_double(s,conv_src, preout, &s->dither.noise, out_count); break;

                }

            }

            s->dither.noise_pos += out_count;

        }



        swri_audio_convert(s->out_convert, out, conv_src, out_count);

    }

    return out_count;

}

                    if((ret=swri_get_dither(s, s->dither.noise.ch[ch], s->dither.noise.count, 12345678913579<<ch, s->dither.noise.fmt))<0)
static int FUNC_0(struct SwrContext *VAR_0, AudioData *VAR_1, int VAR_2,

                                                      AudioData *VAR_3 , int  VAR_4){

    AudioData *postin, *midbuf, *preout;

    int VAR_5;

    AudioData preout_tmp, midbuf_tmp;



    if(VAR_0->full_convert){

        av_assert0(!VAR_0->resample);

        swri_audio_convert(VAR_0->full_convert, VAR_1, VAR_3, VAR_4);

        return VAR_2;

    }









    if((VAR_5=swri_realloc_audio(&VAR_0->postin, VAR_4))<0)

        return VAR_5;

    if(VAR_0->resample_first){

        av_assert0(VAR_0->midbuf.ch_count == VAR_0->used_ch_count);

        if((VAR_5=swri_realloc_audio(&VAR_0->midbuf, VAR_2))<0)

            return VAR_5;

    }else{

        av_assert0(VAR_0->midbuf.ch_count ==  VAR_0->VAR_1.ch_count);

        if((VAR_5=swri_realloc_audio(&VAR_0->midbuf,  VAR_4))<0)

            return VAR_5;

    }

    if((VAR_5=swri_realloc_audio(&VAR_0->preout, VAR_2))<0)

        return VAR_5;



    postin= &VAR_0->postin;



    midbuf_tmp= VAR_0->midbuf;

    midbuf= &midbuf_tmp;

    preout_tmp= VAR_0->preout;

    preout= &preout_tmp;



    if(VAR_0->int_sample_fmt == VAR_0-> in_sample_fmt && VAR_0->VAR_3.planar && !VAR_0->channel_map)

        postin= VAR_3;



    if(VAR_0->resample_first ? !VAR_0->resample : !VAR_0->rematrix)

        midbuf= postin;



    if(VAR_0->resample_first ? !VAR_0->rematrix : !VAR_0->resample)

        preout= midbuf;



    if(VAR_0->int_sample_fmt == VAR_0->out_sample_fmt && VAR_0->VAR_1.planar

       && !(VAR_0->out_sample_fmt==AV_SAMPLE_FMT_S32P && (VAR_0->dither.output_sample_bits&31))){

        if(preout==VAR_3){

            VAR_2= FFMIN(VAR_2, VAR_4); 

            av_assert0(VAR_0->VAR_3.planar); 

            copy(VAR_1, VAR_3, VAR_2);

            return VAR_2;

        }

        else if(preout==postin) preout= midbuf= postin= VAR_1;

        else if(preout==midbuf) preout= midbuf= VAR_1;

        else                    preout= VAR_1;

    }



    if(VAR_3 != postin){

        swri_audio_convert(VAR_0->in_convert, postin, VAR_3, VAR_4);

    }



    if(VAR_0->resample_first){

        if(postin != midbuf)

            VAR_2= resample(VAR_0, midbuf, VAR_2, postin, VAR_4);

        if(midbuf != preout)

            swri_rematrix(VAR_0, preout, midbuf, VAR_2, preout==VAR_1);

    }else{

        if(postin != midbuf)

            swri_rematrix(VAR_0, midbuf, postin, VAR_4, midbuf==VAR_1);

        if(midbuf != preout)

            VAR_2= resample(VAR_0, preout, VAR_2, midbuf, VAR_4);

    }



    if(preout != VAR_1 && VAR_2){

        AudioData *conv_src = preout;

        if(VAR_0->dither.method){

            int VAR_6;

            int VAR_7= FFMAX(VAR_2, 1<<16);



            if (preout == VAR_3) {

                conv_src = &VAR_0->dither.temp;

                if((VAR_5=swri_realloc_audio(&VAR_0->dither.temp, VAR_7))<0)

                    return VAR_5;

            }



            if((VAR_5=swri_realloc_audio(&VAR_0->dither.noise, VAR_7))<0)

                return VAR_5;

            if(VAR_5)

                for(VAR_6=0; VAR_6<VAR_0->dither.noise.ch_count; VAR_6++)

                    if((VAR_5=swri_get_dither(VAR_0, VAR_0->dither.noise.VAR_6[VAR_6], VAR_0->dither.noise.count, 12345678913579<<VAR_6, VAR_0->dither.noise.fmt))<0)

                        return VAR_5;

            av_assert0(VAR_0->dither.noise.ch_count == preout->ch_count);



            if(VAR_0->dither.noise_pos + VAR_2 > VAR_0->dither.noise.count)

                VAR_0->dither.noise_pos = 0;



            if (VAR_0->dither.method < SWR_DITHER_NS){

                if (VAR_0->mix_2_1_simd) {

                    int VAR_8= VAR_2&~15;

                    int VAR_9 = VAR_8 * preout->bps;



                    if(VAR_8)

                        for(VAR_6=0; VAR_6<preout->ch_count; VAR_6++)

                            VAR_0->mix_2_1_simd(conv_src->VAR_6[VAR_6], preout->VAR_6[VAR_6], VAR_0->dither.noise.VAR_6[VAR_6] + VAR_0->dither.noise.bps * VAR_0->dither.noise_pos, VAR_0->native_simd_one, 0, 0, VAR_8);

                    if(VAR_2 != VAR_8)

                        for(VAR_6=0; VAR_6<preout->ch_count; VAR_6++)

                            VAR_0->mix_2_1_f(conv_src->VAR_6[VAR_6] + VAR_9, preout->VAR_6[VAR_6] + VAR_9, VAR_0->dither.noise.VAR_6[VAR_6] + VAR_0->dither.noise.bps * VAR_0->dither.noise_pos + VAR_9 + VAR_8, VAR_0->native_one, 0, 0, VAR_2 - VAR_8);

                } else {

                    for(VAR_6=0; VAR_6<preout->ch_count; VAR_6++)

                        VAR_0->mix_2_1_f(conv_src->VAR_6[VAR_6], preout->VAR_6[VAR_6], VAR_0->dither.noise.VAR_6[VAR_6] + VAR_0->dither.noise.bps * VAR_0->dither.noise_pos, VAR_0->native_one, 0, 0, VAR_2);

                }

            } else {

                switch(VAR_0->int_sample_fmt) {

                case AV_SAMPLE_FMT_S16P :swri_noise_shaping_int16(VAR_0, conv_src, preout, &VAR_0->dither.noise, VAR_2); break;

                case AV_SAMPLE_FMT_S32P :swri_noise_shaping_int32(VAR_0, conv_src, preout, &VAR_0->dither.noise, VAR_2); break;

                case AV_SAMPLE_FMT_FLTP :swri_noise_shaping_float(VAR_0, conv_src, preout, &VAR_0->dither.noise, VAR_2); break;

                case AV_SAMPLE_FMT_DBLP :swri_noise_shaping_double(VAR_0,conv_src, preout, &VAR_0->dither.noise, VAR_2); break;

                }

            }

            VAR_0->dither.noise_pos += VAR_2;

        }



        swri_audio_convert(VAR_0->out_convert, VAR_1, conv_src, VAR_2);

    }

    return VAR_2;

}

static int FUNC_0(struct SwrContext *VAR_0, AudioData *VAR_1, int VAR_2,
AudioData *VAR_3 , int  VAR_4){ AudioData *postin, *midbuf, *preout; int VAR_5; AudioData preout_tmp, midbuf_tmp; if(VAR_0->full_convert){ av_assert0(!VAR_0->resample); swri_audio_convert(VAR_0->full_convert, VAR_1, VAR_3, VAR_4); return VAR_2; } if((VAR_5=swri_realloc_audio(&VAR_0->postin, VAR_4))<0)
return VAR_5; if(VAR_0->resample_first){ av_assert0(VAR_0->midbuf.ch_count == VAR_0->used_ch_count); if((VAR_5=swri_realloc_audio(&VAR_0->midbuf, VAR_2))<0)
return VAR_5; }else{ av_assert0(VAR_0->midbuf.ch_count ==  VAR_0->VAR_1.ch_count); if((VAR_5=swri_realloc_audio(&VAR_0->midbuf,  VAR_4))<0)
return VAR_5; } if((VAR_5=swri_realloc_audio(&VAR_0->preout, VAR_2))<0)
return VAR_5; postin= &VAR_0->postin; midbuf_tmp= VAR_0->midbuf; midbuf= &midbuf_tmp; preout_tmp= VAR_0->preout; preout= &preout_tmp; if(VAR_0->int_sample_fmt == VAR_0-> in_sample_fmt && VAR_0->VAR_3.planar && !VAR_0->channel_map)
postin= VAR_3; if(VAR_0->resample_first ? !VAR_0->resample : !VAR_0->rematrix)
midbuf= postin; if(VAR_0->resample_first ? !VAR_0->rematrix : !VAR_0->resample)
preout= midbuf; if(VAR_0->int_sample_fmt == VAR_0->out_sample_fmt && VAR_0->VAR_1.planar
&& !(VAR_0->out_sample_fmt==AV_SAMPLE_FMT_S32P && (VAR_0->dither.output_sample_bits&31))){ if(preout==VAR_3){ VAR_2= FFMIN(VAR_2, VAR_4); av_assert0(VAR_0->VAR_3.planar); copy(VAR_1, VAR_3, VAR_2); return VAR_2; } else if(preout==postin) preout= midbuf= postin= VAR_1; else if(preout==midbuf) preout= midbuf= VAR_1; else                    preout= VAR_1; } if(VAR_3 != postin){ swri_audio_convert(VAR_0->in_convert, postin, VAR_3, VAR_4); } if(VAR_0->resample_first){ if(postin != midbuf)
VAR_2= resample(VAR_0, midbuf, VAR_2, postin, VAR_4); if(midbuf != preout)
swri_rematrix(VAR_0, preout, midbuf, VAR_2, preout==VAR_1); }else{ if(postin != midbuf)
swri_rematrix(VAR_0, midbuf, postin, VAR_4, midbuf==VAR_1); if(midbuf != preout)
VAR_2= resample(VAR_0, preout, VAR_2, midbuf, VAR_4); } if(preout != VAR_1 && VAR_2){ AudioData *conv_src = preout; if(VAR_0->dither.method){ int VAR_6; int VAR_7= FFMAX(VAR_2, 1<<16); if (preout == VAR_3) { conv_src = &VAR_0->dither.temp; if((VAR_5=swri_realloc_audio(&VAR_0->dither.temp, VAR_7))<0)
return VAR_5; } if((VAR_5=swri_realloc_audio(&VAR_0->dither.noise, VAR_7))<0)
return VAR_5; if(VAR_5)
for(VAR_6=0; VAR_6<VAR_0->dither.noise.ch_count; VAR_6++) if((VAR_5=swri_get_dither(VAR_0, VAR_0->dither.noise.VAR_6[VAR_6], VAR_0->dither.noise.count, 12345678913579<<VAR_6, VAR_0->dither.noise.fmt))<0)
return VAR_5; av_assert0(VAR_0->dither.noise.ch_count == preout->ch_count); if(VAR_0->dither.noise_pos + VAR_2 > VAR_0->dither.noise.count)
VAR_0->dither.noise_pos = 0; if (VAR_0->dither.method < SWR_DITHER_NS){ if (VAR_0->mix_2_1_simd) { int VAR_8= VAR_2&~15; int VAR_9 = VAR_8 * preout->bps; if(VAR_8)
for(VAR_6=0; VAR_6<preout->ch_count; VAR_6++) VAR_0->mix_2_1_simd(conv_src->VAR_6[VAR_6], preout->VAR_6[VAR_6], VAR_0->dither.noise.VAR_6[VAR_6] + VAR_0->dither.noise.bps * VAR_0->dither.noise_pos, VAR_0->native_simd_one, 0, 0, VAR_8); if(VAR_2 != VAR_8)
for(VAR_6=0; VAR_6<preout->ch_count; VAR_6++) VAR_0->mix_2_1_f(conv_src->VAR_6[VAR_6] + VAR_9, preout->VAR_6[VAR_6] + VAR_9, VAR_0->dither.noise.VAR_6[VAR_6] + VAR_0->dither.noise.bps * VAR_0->dither.noise_pos + VAR_9 + VAR_8, VAR_0->native_one, 0, 0, VAR_2 - VAR_8); } else { for(VAR_6=0; VAR_6<preout->ch_count; VAR_6++) VAR_0->mix_2_1_f(conv_src->VAR_6[VAR_6], preout->VAR_6[VAR_6], VAR_0->dither.noise.VAR_6[VAR_6] + VAR_0->dither.noise.bps * VAR_0->dither.noise_pos, VAR_0->native_one, 0, 0, VAR_2); } } else { switch(VAR_0->int_sample_fmt) { case AV_SAMPLE_FMT_S16P :swri_noise_shaping_int16(VAR_0, conv_src, preout, &VAR_0->dither.noise, VAR_2); break; case AV_SAMPLE_FMT_S32P :swri_noise_shaping_int32(VAR_0, conv_src, preout, &VAR_0->dither.noise, VAR_2); break; case AV_SAMPLE_FMT_FLTP :swri_noise_shaping_float(VAR_0, conv_src, preout, &VAR_0->dither.noise, VAR_2); break; case AV_SAMPLE_FMT_DBLP :swri_noise_shaping_double(VAR_0,conv_src, preout, &VAR_0->dither.noise, VAR_2); break; } } VAR_0->dither.noise_pos += VAR_2; } swri_audio_convert(VAR_0->out_convert, VAR_1, conv_src, VAR_2); } return VAR_2; }