int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,

                                const uint8_t *in_arg [SWR_CH_MAX], int  in_count){

    AudioData * in= &s->in;

    AudioData *out= &s->out;



    if (!swr_is_initialized(s)) {

        av_log(s, AV_LOG_ERROR, "Context has not been initialized\n");

        return AVERROR(EINVAL);

    }



    while(s->drop_output > 0){

        int ret;

        uint8_t *tmp_arg[SWR_CH_MAX];

#define MAX_DROP_STEP 16384

        if((ret=swri_realloc_audio(&s->drop_temp, FFMIN(s->drop_output, MAX_DROP_STEP)))<0)

            return ret;



        reversefill_audiodata(&s->drop_temp, tmp_arg);

        s->drop_output *= -1; //FIXME find a less hackish solution

        ret = swr_convert(s, tmp_arg, FFMIN(-s->drop_output, MAX_DROP_STEP), in_arg, in_count); //FIXME optimize but this is as good as never called so maybe it doesn't matter

        s->drop_output *= -1;

        in_count = 0;

        if(ret>0) {

            s->drop_output -= ret;

            if (!s->drop_output && !out_arg)

                return 0;

            continue;

        }



        av_assert0(s->drop_output);

        return 0;

    }



    if(!in_arg){

        if(s->resample){

            if (!s->flushed)

                s->resampler->flush(s);

            s->resample_in_constraint = 0;

            s->flushed = 1;

        }else if(!s->in_buffer_count){

            return 0;

        }

    }else

        fill_audiodata(in ,  (void*)in_arg);



    fill_audiodata(out, out_arg);



    if(s->resample){

        int ret = swr_convert_internal(s, out, out_count, in, in_count);

        if(ret>0 && !s->drop_output)

            s->outpts += ret * (int64_t)s->in_sample_rate;

        return ret;

    }else{

        AudioData tmp= *in;

        int ret2=0;

        int ret, size;

        size = FFMIN(out_count, s->in_buffer_count);

        if(size){

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

            ret= swr_convert_internal(s, out, size, &tmp, size);

            if(ret<0)

                return ret;

            ret2= ret;

            s->in_buffer_count -= ret;

            s->in_buffer_index += ret;

            buf_set(out, out, ret);

            out_count -= ret;

            if(!s->in_buffer_count)

                s->in_buffer_index = 0;

        }



        if(in_count){

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



            if(in_count > out_count) { //FIXME move after swr_convert_internal

                if(   size > s->in_buffer.count

                && s->in_buffer_count + in_count - out_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(out_count){

                size = FFMIN(in_count, out_count);

                ret= swr_convert_internal(s, out, size, in, size);

                if(ret<0)

                    return ret;

                buf_set(in, in, ret);

                in_count -= ret;

                ret2 += ret;

            }

            if(in_count){

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

                copy(&tmp, in, in_count);

                s->in_buffer_count += in_count;

            }

        }

        if(ret2>0 && !s->drop_output)

            s->outpts += ret2 * (int64_t)s->in_sample_rate;

        return ret2;

    }

}

int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,

                                const uint8_t *in_arg [SWR_CH_MAX], int  in_count){

    AudioData * in= &s->in;

    AudioData *out= &s->out;



    if (!swr_is_initialized(s)) {

        av_log(s, AV_LOG_ERROR, "Context has not been initialized\n");

        return AVERROR(EINVAL);

    }



    while(s->drop_output > 0){

        int ret;

        uint8_t *tmp_arg[SWR_CH_MAX];

#define MAX_DROP_STEP 16384

        if((ret=swri_realloc_audio(&s->drop_temp, FFMIN(s->drop_output, MAX_DROP_STEP)))<0)

            return ret;



        reversefill_audiodata(&s->drop_temp, tmp_arg);

        s->drop_output *= -1; 

        ret = swr_convert(s, tmp_arg, FFMIN(-s->drop_output, MAX_DROP_STEP), in_arg, in_count); 

        s->drop_output *= -1;

        in_count = 0;

        if(ret>0) {

            s->drop_output -= ret;

            if (!s->drop_output && !out_arg)

                return 0;

            continue;

        }



        av_assert0(s->drop_output);

        return 0;

    }



    if(!in_arg){

        if(s->resample){

            if (!s->flushed)

                s->resampler->flush(s);

            s->resample_in_constraint = 0;

            s->flushed = 1;

        }else if(!s->in_buffer_count){

            return 0;

        }

    }else

        fill_audiodata(in ,  (void*)in_arg);



    fill_audiodata(out, out_arg);



    if(s->resample){

        int ret = swr_convert_internal(s, out, out_count, in, in_count);

        if(ret>0 && !s->drop_output)

            s->outpts += ret * (int64_t)s->in_sample_rate;

        return ret;

    }else{

        AudioData tmp= *in;

        int ret2=0;

        int ret, size;

        size = FFMIN(out_count, s->in_buffer_count);

        if(size){

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

            ret= swr_convert_internal(s, out, size, &tmp, size);

            if(ret<0)

                return ret;

            ret2= ret;

            s->in_buffer_count -= ret;

            s->in_buffer_index += ret;

            buf_set(out, out, ret);

            out_count -= ret;

            if(!s->in_buffer_count)

                s->in_buffer_index = 0;

        }



        if(in_count){

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



            if(in_count > out_count) { 

                if(   size > s->in_buffer.count

                && s->in_buffer_count + in_count - out_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(out_count){

                size = FFMIN(in_count, out_count);

                ret= swr_convert_internal(s, out, size, in, size);

                if(ret<0)

                    return ret;

                buf_set(in, in, ret);

                in_count -= ret;

                ret2 += ret;

            }

            if(in_count){

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

                copy(&tmp, in, in_count);

                s->in_buffer_count += in_count;

            }

        }

        if(ret2>0 && !s->drop_output)

            s->outpts += ret2 * (int64_t)s->in_sample_rate;

        return ret2;

    }

}

int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,                                 const uint8_t *in_arg [SWR_CH_MAX], int  in_count){
int FUNC_0(struct SwrContext *VAR_0, uint8_t *VAR_1[SWR_CH_MAX], int VAR_2,

                                const uint8_t *VAR_3 [SWR_CH_MAX], int  VAR_4){

    AudioData * in= &VAR_0->in;

    AudioData *out= &VAR_0->out;



    if (!swr_is_initialized(VAR_0)) {

        av_log(VAR_0, AV_LOG_ERROR, "Context has not been initialized\n");

        return AVERROR(EINVAL);

    }



    while(VAR_0->drop_output > 0){

        int VAR_7;

        uint8_t *tmp_arg[SWR_CH_MAX];

#define MAX_DROP_STEP 16384

        if((VAR_7=swri_realloc_audio(&VAR_0->drop_temp, FFMIN(VAR_0->drop_output, MAX_DROP_STEP)))<0)

            return VAR_7;



        reversefill_audiodata(&VAR_0->drop_temp, tmp_arg);

        VAR_0->drop_output *= -1; 

        VAR_7 = FUNC_0(VAR_0, tmp_arg, FFMIN(-VAR_0->drop_output, MAX_DROP_STEP), VAR_3, VAR_4); 

        VAR_0->drop_output *= -1;

        VAR_4 = 0;

        if(VAR_7>0) {

            VAR_0->drop_output -= VAR_7;

            if (!VAR_0->drop_output && !VAR_1)

                return 0;

            continue;

        }



        av_assert0(VAR_0->drop_output);

        return 0;

    }



    if(!VAR_3){

        if(VAR_0->resample){

            if (!VAR_0->flushed)

                VAR_0->resampler->flush(VAR_0);

            VAR_0->resample_in_constraint = 0;

            VAR_0->flushed = 1;

        }else if(!VAR_0->in_buffer_count){

            return 0;

        }

    }else

        fill_audiodata(in ,  (void*)VAR_3);



    fill_audiodata(out, VAR_1);



    if(VAR_0->resample){

        int VAR_7 = swr_convert_internal(VAR_0, out, VAR_2, in, VAR_4);

        if(VAR_7>0 && !VAR_0->drop_output)

            VAR_0->outpts += VAR_7 * (int64_t)VAR_0->in_sample_rate;

        return VAR_7;

    }else{

        AudioData tmp= *in;

        int VAR_6=0;

        int VAR_7, VAR_7;

        VAR_7 = FFMIN(VAR_2, VAR_0->in_buffer_count);

        if(VAR_7){

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

            VAR_7= swr_convert_internal(VAR_0, out, VAR_7, &tmp, VAR_7);

            if(VAR_7<0)

                return VAR_7;

            VAR_6= VAR_7;

            VAR_0->in_buffer_count -= VAR_7;

            VAR_0->in_buffer_index += VAR_7;

            buf_set(out, out, VAR_7);

            VAR_2 -= VAR_7;

            if(!VAR_0->in_buffer_count)

                VAR_0->in_buffer_index = 0;

        }



        if(VAR_4){

            VAR_7= VAR_0->in_buffer_index + VAR_0->in_buffer_count + VAR_4 - VAR_2;



            if(VAR_4 > VAR_2) { 

                if(   VAR_7 > VAR_0->in_buffer.count

                && VAR_0->in_buffer_count + VAR_4 - VAR_2 <= 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_7)) < 0)

                        return VAR_7;

            }



            if(VAR_2){

                VAR_7 = FFMIN(VAR_4, VAR_2);

                VAR_7= swr_convert_internal(VAR_0, out, VAR_7, in, VAR_7);

                if(VAR_7<0)

                    return VAR_7;

                buf_set(in, in, VAR_7);

                VAR_4 -= VAR_7;

                VAR_6 += VAR_7;

            }

            if(VAR_4){

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

                copy(&tmp, in, VAR_4);

                VAR_0->in_buffer_count += VAR_4;

            }

        }

        if(VAR_6>0 && !VAR_0->drop_output)

            VAR_0->outpts += VAR_6 * (int64_t)VAR_0->in_sample_rate;

        return VAR_6;

    }

}

int FUNC_0(struct SwrContext *VAR_0, uint8_t *VAR_1[SWR_CH_MAX], int VAR_2,
const uint8_t *VAR_3 [SWR_CH_MAX], int  VAR_4){ AudioData * in= &VAR_0->in; AudioData *out= &VAR_0->out; if (!swr_is_initialized(VAR_0)) { av_log(VAR_0, AV_LOG_ERROR, "Context has not been initialized\n"); return AVERROR(EINVAL); } while(VAR_0->drop_output > 0){ int VAR_7; uint8_t *tmp_arg[SWR_CH_MAX]; #define MAX_DROP_STEP 16384
if((VAR_7=swri_realloc_audio(&VAR_0->drop_temp, FFMIN(VAR_0->drop_output, MAX_DROP_STEP)))<0)
return VAR_7; reversefill_audiodata(&VAR_0->drop_temp, tmp_arg); VAR_0->drop_output *= -1; VAR_7 = FUNC_0(VAR_0, tmp_arg, FFMIN(-VAR_0->drop_output, MAX_DROP_STEP), VAR_3, VAR_4); VAR_0->drop_output *= -1; VAR_4 = 0; if(VAR_7>0) { VAR_0->drop_output -= VAR_7; if (!VAR_0->drop_output && !VAR_1)
return 0; continue; } av_assert0(VAR_0->drop_output); return 0; } if(!VAR_3){ if(VAR_0->resample){ if (!VAR_0->flushed)
VAR_0->resampler->flush(VAR_0); VAR_0->resample_in_constraint = 0; VAR_0->flushed = 1; }else if(!VAR_0->in_buffer_count){ return 0; } }else fill_audiodata(in ,  (void*)VAR_3); fill_audiodata(out, VAR_1); if(VAR_0->resample){ int VAR_7 = swr_convert_internal(VAR_0, out, VAR_2, in, VAR_4); if(VAR_7>0 && !VAR_0->drop_output)
VAR_0->outpts += VAR_7 * (int64_t)VAR_0->in_sample_rate; return VAR_7; }else{ AudioData tmp= *in; int VAR_6=0; int VAR_7, VAR_7; VAR_7 = FFMIN(VAR_2, VAR_0->in_buffer_count); if(VAR_7){ buf_set(&tmp, &VAR_0->in_buffer, VAR_0->in_buffer_index); VAR_7= swr_convert_internal(VAR_0, out, VAR_7, &tmp, VAR_7); if(VAR_7<0)
return VAR_7; VAR_6= VAR_7; VAR_0->in_buffer_count -= VAR_7; VAR_0->in_buffer_index += VAR_7; buf_set(out, out, VAR_7); VAR_2 -= VAR_7; if(!VAR_0->in_buffer_count)
VAR_0->in_buffer_index = 0; } if(VAR_4){ VAR_7= VAR_0->in_buffer_index + VAR_0->in_buffer_count + VAR_4 - VAR_2; if(VAR_4 > VAR_2) { if(   VAR_7 > VAR_0->in_buffer.count
&& VAR_0->in_buffer_count + VAR_4 - VAR_2 <= 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_7)) < 0)
return VAR_7; } if(VAR_2){ VAR_7 = FFMIN(VAR_4, VAR_2); VAR_7= swr_convert_internal(VAR_0, out, VAR_7, in, VAR_7); if(VAR_7<0)
return VAR_7; buf_set(in, in, VAR_7); VAR_4 -= VAR_7; VAR_6 += VAR_7; } if(VAR_4){ buf_set(&tmp, &VAR_0->in_buffer, VAR_0->in_buffer_index + VAR_0->in_buffer_count); copy(&tmp, in, VAR_4); VAR_0->in_buffer_count += VAR_4; } } if(VAR_6>0 && !VAR_0->drop_output)
VAR_0->outpts += VAR_6 * (int64_t)VAR_0->in_sample_rate; return VAR_6; } }