static int resample(SwrContext *s, AudioData *out_param, int out_count,

                             const AudioData * in_param, int in_count){

    AudioData in, out, tmp;

    int ret_sum=0;

    int border=0;



    av_assert1(s->in_buffer.ch_count == in_param->ch_count);

    av_assert1(s->in_buffer.planar   == in_param->planar);

    av_assert1(s->in_buffer.fmt      == in_param->fmt);



    tmp=out=*out_param;

    in =  *in_param;



    do{

        int ret, size, consumed;

        if(!s->resample_in_constraint && s->in_buffer_count){

            buf_set(&tmp, &s->in_buffer, s->in_buffer_index);

            ret= s->resampler->multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);

            out_count -= ret;

            ret_sum += ret;

            buf_set(&out, &out, ret);

            s->in_buffer_count -= consumed;

            s->in_buffer_index += consumed;



            if(!in_count)

                break;

            if(s->in_buffer_count <= border){

                buf_set(&in, &in, -s->in_buffer_count);

                in_count += s->in_buffer_count;

                s->in_buffer_count=0;

                s->in_buffer_index=0;

                border = 0;

            }

        }



        if((s->flushed || in_count) && !s->in_buffer_count){

            s->in_buffer_index=0;

            ret= s->resampler->multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);

            out_count -= ret;

            ret_sum += ret;

            buf_set(&out, &out, ret);

            in_count -= consumed;

            buf_set(&in, &in, consumed);

        }



        //TODO is this check sane considering the advanced copy avoidance below

        size= s->in_buffer_index + s->in_buffer_count + in_count;

        if(   size > s->in_buffer.count

           && s->in_buffer_count + in_count <= s->in_buffer_index){

            buf_set(&tmp, &s->in_buffer, s->in_buffer_index);

            copy(&s->in_buffer, &tmp, s->in_buffer_count);

            s->in_buffer_index=0;

        }else

            if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)

                return ret;



        if(in_count){

            int count= in_count;

            if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;



            buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);

            copy(&tmp, &in, /*in_*/count);

            s->in_buffer_count += count;

            in_count -= count;

            border += count;

            buf_set(&in, &in, count);

            s->resample_in_constraint= 0;

            if(s->in_buffer_count != count || in_count)

                continue;

        }

        break;

    }while(1);



    s->resample_in_constraint= !!out_count;



    return ret_sum;

}

static int resample(SwrContext *s, AudioData *out_param, int out_count,

                             const AudioData * in_param, int in_count){

    AudioData in, out, tmp;

    int ret_sum=0;

    int border=0;



    av_assert1(s->in_buffer.ch_count == in_param->ch_count);

    av_assert1(s->in_buffer.planar   == in_param->planar);

    av_assert1(s->in_buffer.fmt      == in_param->fmt);



    tmp=out=*out_param;

    in =  *in_param;



    do{

        int ret, size, consumed;

        if(!s->resample_in_constraint && s->in_buffer_count){

            buf_set(&tmp, &s->in_buffer, s->in_buffer_index);

            ret= s->resampler->multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);

            out_count -= ret;

            ret_sum += ret;

            buf_set(&out, &out, ret);

            s->in_buffer_count -= consumed;

            s->in_buffer_index += consumed;



            if(!in_count)

                break;

            if(s->in_buffer_count <= border){

                buf_set(&in, &in, -s->in_buffer_count);

                in_count += s->in_buffer_count;

                s->in_buffer_count=0;

                s->in_buffer_index=0;

                border = 0;

            }

        }



        if((s->flushed || in_count) && !s->in_buffer_count){

            s->in_buffer_index=0;

            ret= s->resampler->multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);

            out_count -= ret;

            ret_sum += ret;

            buf_set(&out, &out, ret);

            in_count -= consumed;

            buf_set(&in, &in, consumed);

        }



        

        size= s->in_buffer_index + s->in_buffer_count + in_count;

        if(   size > s->in_buffer.count

           && s->in_buffer_count + in_count <= s->in_buffer_index){

            buf_set(&tmp, &s->in_buffer, s->in_buffer_index);

            copy(&s->in_buffer, &tmp, s->in_buffer_count);

            s->in_buffer_index=0;

        }else

            if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)

                return ret;



        if(in_count){

            int count= in_count;

            if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;



            buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);

            copy(&tmp, &in, count);

            s->in_buffer_count += count;

            in_count -= count;

            border += count;

            buf_set(&in, &in, count);

            s->resample_in_constraint= 0;

            if(s->in_buffer_count != count || in_count)

                continue;

        }

        break;

    }while(1);



    s->resample_in_constraint= !!out_count;



    return ret_sum;

}

static int FUNC_0(SwrContext *VAR_0, AudioData *VAR_1, int VAR_2,

                             const AudioData * VAR_3, int VAR_4){

    AudioData in, out, tmp;

    int VAR_5=0;

    int VAR_6=0;



    av_assert1(VAR_0->in_buffer.ch_count == VAR_3->ch_count);

    av_assert1(VAR_0->in_buffer.planar   == VAR_3->planar);

    av_assert1(VAR_0->in_buffer.fmt      == VAR_3->fmt);



    tmp=out=*VAR_1;

    in =  *VAR_3;



    do{

        int VAR_7, VAR_8, VAR_9;

        if(!VAR_0->resample_in_constraint && VAR_0->in_buffer_count){

            buf_set(&tmp, &VAR_0->in_buffer, VAR_0->in_buffer_index);

            VAR_7= VAR_0->resampler->multiple_resample(VAR_0->FUNC_0, &out, VAR_2, &tmp, VAR_0->in_buffer_count, &VAR_9);

            VAR_2 -= VAR_7;

            VAR_5 += VAR_7;

            buf_set(&out, &out, VAR_7);

            VAR_0->in_buffer_count -= VAR_9;

            VAR_0->in_buffer_index += VAR_9;



            if(!VAR_4)

                break;

            if(VAR_0->in_buffer_count <= VAR_6){

                buf_set(&in, &in, -VAR_0->in_buffer_count);

                VAR_4 += VAR_0->in_buffer_count;

                VAR_0->in_buffer_count=0;

                VAR_0->in_buffer_index=0;

                VAR_6 = 0;

            }

        }



        if((VAR_0->flushed || VAR_4) && !VAR_0->in_buffer_count){

            VAR_0->in_buffer_index=0;

            VAR_7= VAR_0->resampler->multiple_resample(VAR_0->FUNC_0, &out, VAR_2, &in, VAR_4, &VAR_9);

            VAR_2 -= VAR_7;

            VAR_5 += VAR_7;

            buf_set(&out, &out, VAR_7);

            VAR_4 -= VAR_9;

            buf_set(&in, &in, VAR_9);

        }



        

        VAR_8= VAR_0->in_buffer_index + VAR_0->in_buffer_count + VAR_4;

        if(   VAR_8 > VAR_0->in_buffer.VAR_10

           && VAR_0->in_buffer_count + VAR_4 <= VAR_0->in_buffer_index){

            buf_set(&tmp, &VAR_0->in_buffer, VAR_0->in_buffer_index);

            copy(&VAR_0->in_buffer, &tmp, VAR_0->in_buffer_count);

            VAR_0->in_buffer_index=0;

        }else

            if((VAR_7=swri_realloc_audio(&VAR_0->in_buffer, VAR_8)) < 0)

                return VAR_7;



        if(VAR_4){

            int VAR_10= VAR_4;

            if(VAR_0->in_buffer_count && VAR_0->in_buffer_count+2 < VAR_10 && VAR_2) VAR_10= VAR_0->in_buffer_count+2;



            buf_set(&tmp, &VAR_0->in_buffer, VAR_0->in_buffer_index + VAR_0->in_buffer_count);

            copy(&tmp, &in, VAR_10);

            VAR_0->in_buffer_count += VAR_10;

            VAR_4 -= VAR_10;

            VAR_6 += VAR_10;

            buf_set(&in, &in, VAR_10);

            VAR_0->resample_in_constraint= 0;

            if(VAR_0->in_buffer_count != VAR_10 || VAR_4)

                continue;

        }

        break;

    }while(1);



    VAR_0->resample_in_constraint= !!VAR_2;



    return VAR_5;

}

static int FUNC_0(SwrContext *VAR_0, AudioData *VAR_1, int VAR_2,
const AudioData * VAR_3, int VAR_4){ AudioData in, out, tmp; int VAR_5=0; int VAR_6=0; av_assert1(VAR_0->in_buffer.ch_count == VAR_3->ch_count); av_assert1(VAR_0->in_buffer.planar   == VAR_3->planar); av_assert1(VAR_0->in_buffer.fmt      == VAR_3->fmt); tmp=out=*VAR_1; in =  *VAR_3; do{ int VAR_7, VAR_8, VAR_9; if(!VAR_0->resample_in_constraint && VAR_0->in_buffer_count){ buf_set(&tmp, &VAR_0->in_buffer, VAR_0->in_buffer_index); VAR_7= VAR_0->resampler->multiple_resample(VAR_0->FUNC_0, &out, VAR_2, &tmp, VAR_0->in_buffer_count, &VAR_9); VAR_2 -= VAR_7; VAR_5 += VAR_7; buf_set(&out, &out, VAR_7); VAR_0->in_buffer_count -= VAR_9; VAR_0->in_buffer_index += VAR_9; if(!VAR_4)
break; if(VAR_0->in_buffer_count <= VAR_6){ buf_set(&in, &in, -VAR_0->in_buffer_count); VAR_4 += VAR_0->in_buffer_count; VAR_0->in_buffer_count=0; VAR_0->in_buffer_index=0; VAR_6 = 0; } } if((VAR_0->flushed || VAR_4) && !VAR_0->in_buffer_count){ VAR_0->in_buffer_index=0; VAR_7= VAR_0->resampler->multiple_resample(VAR_0->FUNC_0, &out, VAR_2, &in, VAR_4, &VAR_9); VAR_2 -= VAR_7; VAR_5 += VAR_7; buf_set(&out, &out, VAR_7); VAR_4 -= VAR_9; buf_set(&in, &in, VAR_9); } VAR_8= VAR_0->in_buffer_index + VAR_0->in_buffer_count + VAR_4; if(   VAR_8 > VAR_0->in_buffer.VAR_10
&& VAR_0->in_buffer_count + VAR_4 <= VAR_0->in_buffer_index){ buf_set(&tmp, &VAR_0->in_buffer, VAR_0->in_buffer_index); copy(&VAR_0->in_buffer, &tmp, VAR_0->in_buffer_count); VAR_0->in_buffer_index=0; }else if((VAR_7=swri_realloc_audio(&VAR_0->in_buffer, VAR_8)) < 0)
return VAR_7; if(VAR_4){ int VAR_10= VAR_4; if(VAR_0->in_buffer_count && VAR_0->in_buffer_count+2 < VAR_10 && VAR_2) VAR_10= VAR_0->in_buffer_count+2; buf_set(&tmp, &VAR_0->in_buffer, VAR_0->in_buffer_index + VAR_0->in_buffer_count); copy(&tmp, &in, VAR_10); VAR_0->in_buffer_count += VAR_10; VAR_4 -= VAR_10; VAR_6 += VAR_10; buf_set(&in, &in, VAR_10); VAR_0->resample_in_constraint= 0; if(VAR_0->in_buffer_count != VAR_10 || VAR_4)
continue; } break; }while(1); VAR_0->resample_in_constraint= !!VAR_2; return VAR_5; }