int swr_init(SwrContext *s){

    s->in_buffer_index= 0;

    s->in_buffer_count= 0;

    s->resample_in_constraint= 0;

    free_temp(&s->postin);

    free_temp(&s->midbuf);

    free_temp(&s->preout);

    free_temp(&s->in_buffer);

    swr_audio_convert_free(&s-> in_convert);

    swr_audio_convert_free(&s->out_convert);



    s-> in.planar= s-> in_sample_fmt >= 0x100;

    s->out.planar= s->out_sample_fmt >= 0x100;

    s-> in_sample_fmt &= 0xFF;

    s->out_sample_fmt &= 0xFF;



    //We assume AVOptions checked the various values and the defaults where allowed

    if(   s->int_sample_fmt != AV_SAMPLE_FMT_S16

        &&s->int_sample_fmt != AV_SAMPLE_FMT_FLT){

        av_log(s, AV_LOG_ERROR, "Requested sample format %s is not supported internally, only float & S16 is supported\n", av_get_sample_fmt_name(s->int_sample_fmt));

        return AVERROR(EINVAL);

    }



    //FIXME should we allow/support using FLT on material that doesnt need it ?

    if(s->in_sample_fmt <= AV_SAMPLE_FMT_S16 || s->int_sample_fmt==AV_SAMPLE_FMT_S16){

        s->int_sample_fmt= AV_SAMPLE_FMT_S16;

    }else

        s->int_sample_fmt= AV_SAMPLE_FMT_FLT;





    if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){

        s->resample = swr_resample_init(s->resample, s->out_sample_rate, s->in_sample_rate, 16, 10, 0, 0.8);

    }else

        swr_resample_free(&s->resample);

    if(s->int_sample_fmt != AV_SAMPLE_FMT_S16 && s->resample){

        av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16 currently\n"); //FIXME

        return -1;

    }



    if(!s-> in_ch_layout)

        s-> in_ch_layout= guess_layout(s->in.ch_count);

    if(!s->out_ch_layout)

        s->out_ch_layout= guess_layout(s->out.ch_count);



    s->rematrix= s->out_ch_layout  !=s->in_ch_layout;



#define RSC 1 //FIXME finetune

    if(!s-> in.ch_count)

        s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);

    if(!s->out.ch_count)

        s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);



av_assert0(s-> in.ch_count);

av_assert0(s->out.ch_count);

    s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;



    s-> in.bps= av_get_bits_per_sample_fmt(s-> in_sample_fmt)/8;

    s->int_bps= av_get_bits_per_sample_fmt(s->int_sample_fmt)/8;

    s->out.bps= av_get_bits_per_sample_fmt(s->out_sample_fmt)/8;



    s->in_convert = swr_audio_convert_alloc(s->int_sample_fmt,

                                            s-> in_sample_fmt, s-> in.ch_count, 0);

    s->out_convert= swr_audio_convert_alloc(s->out_sample_fmt,

                                            s->int_sample_fmt, s->out.ch_count, 0);





    s->postin= s->in;

    s->preout= s->out;

    s->midbuf= s->in;

    s->in_buffer= s->in;

    if(!s->resample_first){

        s->midbuf.ch_count= s->out.ch_count;

        s->in_buffer.ch_count = s->out.ch_count;

    }



    s->in_buffer.bps = s->postin.bps = s->midbuf.bps = s->preout.bps =  s->int_bps;

    s->in_buffer.planar = s->postin.planar = s->midbuf.planar = s->preout.planar =  1;





    if(s->rematrix && swr_rematrix_init(s)<0)

        return -1;



    return 0;

}

int swr_init(SwrContext *s){

    s->in_buffer_index= 0;

    s->in_buffer_count= 0;

    s->resample_in_constraint= 0;

    free_temp(&s->postin);

    free_temp(&s->midbuf);

    free_temp(&s->preout);

    free_temp(&s->in_buffer);

    swr_audio_convert_free(&s-> in_convert);

    swr_audio_convert_free(&s->out_convert);



    s-> in.planar= s-> in_sample_fmt >= 0x100;

    s->out.planar= s->out_sample_fmt >= 0x100;

    s-> in_sample_fmt &= 0xFF;

    s->out_sample_fmt &= 0xFF;



    

    if(   s->int_sample_fmt != AV_SAMPLE_FMT_S16

        &&s->int_sample_fmt != AV_SAMPLE_FMT_FLT){

        av_log(s, AV_LOG_ERROR, "Requested sample format %s is not supported internally, only float & S16 is supported\n", av_get_sample_fmt_name(s->int_sample_fmt));

        return AVERROR(EINVAL);

    }



    

    if(s->in_sample_fmt <= AV_SAMPLE_FMT_S16 || s->int_sample_fmt==AV_SAMPLE_FMT_S16){

        s->int_sample_fmt= AV_SAMPLE_FMT_S16;

    }else

        s->int_sample_fmt= AV_SAMPLE_FMT_FLT;





    if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){

        s->resample = swr_resample_init(s->resample, s->out_sample_rate, s->in_sample_rate, 16, 10, 0, 0.8);

    }else

        swr_resample_free(&s->resample);

    if(s->int_sample_fmt != AV_SAMPLE_FMT_S16 && s->resample){

        av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16 currently\n"); 

        return -1;

    }



    if(!s-> in_ch_layout)

        s-> in_ch_layout= guess_layout(s->in.ch_count);

    if(!s->out_ch_layout)

        s->out_ch_layout= guess_layout(s->out.ch_count);



    s->rematrix= s->out_ch_layout  !=s->in_ch_layout;



#define RSC 1  finetune

    if(!s-> in.ch_count)

        s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);

    if(!s->out.ch_count)

        s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);



av_assert0(s-> in.ch_count);

av_assert0(s->out.ch_count);

    s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;



    s-> in.bps= av_get_bits_per_sample_fmt(s-> in_sample_fmt)/8;

    s->int_bps= av_get_bits_per_sample_fmt(s->int_sample_fmt)/8;

    s->out.bps= av_get_bits_per_sample_fmt(s->out_sample_fmt)/8;



    s->in_convert = swr_audio_convert_alloc(s->int_sample_fmt,

                                            s-> in_sample_fmt, s-> in.ch_count, 0);

    s->out_convert= swr_audio_convert_alloc(s->out_sample_fmt,

                                            s->int_sample_fmt, s->out.ch_count, 0);





    s->postin= s->in;

    s->preout= s->out;

    s->midbuf= s->in;

    s->in_buffer= s->in;

    if(!s->resample_first){

        s->midbuf.ch_count= s->out.ch_count;

        s->in_buffer.ch_count = s->out.ch_count;

    }



    s->in_buffer.bps = s->postin.bps = s->midbuf.bps = s->preout.bps =  s->int_bps;

    s->in_buffer.planar = s->postin.planar = s->midbuf.planar = s->preout.planar =  1;





    if(s->rematrix && swr_rematrix_init(s)<0)

        return -1;



    return 0;

}

int FUNC_0(SwrContext *VAR_0){

    VAR_0->in_buffer_index= 0;

    VAR_0->in_buffer_count= 0;

    VAR_0->resample_in_constraint= 0;

    free_temp(&VAR_0->postin);

    free_temp(&VAR_0->midbuf);

    free_temp(&VAR_0->preout);

    free_temp(&VAR_0->in_buffer);

    swr_audio_convert_free(&VAR_0-> in_convert);

    swr_audio_convert_free(&VAR_0->out_convert);



    VAR_0-> in.planar= VAR_0-> in_sample_fmt >= 0x100;

    VAR_0->out.planar= VAR_0->out_sample_fmt >= 0x100;

    VAR_0-> in_sample_fmt &= 0xFF;

    VAR_0->out_sample_fmt &= 0xFF;



    

    if(   VAR_0->int_sample_fmt != AV_SAMPLE_FMT_S16

        &&VAR_0->int_sample_fmt != AV_SAMPLE_FMT_FLT){

        av_log(VAR_0, AV_LOG_ERROR, "Requested sample format %VAR_0 is not supported internally, only float & S16 is supported\n", av_get_sample_fmt_name(VAR_0->int_sample_fmt));

        return AVERROR(EINVAL);

    }



    

    if(VAR_0->in_sample_fmt <= AV_SAMPLE_FMT_S16 || VAR_0->int_sample_fmt==AV_SAMPLE_FMT_S16){

        VAR_0->int_sample_fmt= AV_SAMPLE_FMT_S16;

    }else

        VAR_0->int_sample_fmt= AV_SAMPLE_FMT_FLT;





    if (VAR_0->out_sample_rate!=VAR_0->in_sample_rate || (VAR_0->flags & SWR_FLAG_RESAMPLE)){

        VAR_0->resample = swr_resample_init(VAR_0->resample, VAR_0->out_sample_rate, VAR_0->in_sample_rate, 16, 10, 0, 0.8);

    }else

        swr_resample_free(&VAR_0->resample);

    if(VAR_0->int_sample_fmt != AV_SAMPLE_FMT_S16 && VAR_0->resample){

        av_log(VAR_0, AV_LOG_ERROR, "Resampling only supported with internal s16 currently\n"); 

        return -1;

    }



    if(!VAR_0-> in_ch_layout)

        VAR_0-> in_ch_layout= guess_layout(VAR_0->in.ch_count);

    if(!VAR_0->out_ch_layout)

        VAR_0->out_ch_layout= guess_layout(VAR_0->out.ch_count);



    VAR_0->rematrix= VAR_0->out_ch_layout  !=VAR_0->in_ch_layout;



#define RSC 1  finetune

    if(!VAR_0-> in.ch_count)

        VAR_0-> in.ch_count= av_get_channel_layout_nb_channels(VAR_0-> in_ch_layout);

    if(!VAR_0->out.ch_count)

        VAR_0->out.ch_count= av_get_channel_layout_nb_channels(VAR_0->out_ch_layout);



av_assert0(VAR_0-> in.ch_count);

av_assert0(VAR_0->out.ch_count);

    VAR_0->resample_first= RSC*VAR_0->out.ch_count/VAR_0->in.ch_count - RSC < VAR_0->out_sample_rate/(float)VAR_0-> in_sample_rate - 1.0;



    VAR_0-> in.bps= av_get_bits_per_sample_fmt(VAR_0-> in_sample_fmt)/8;

    VAR_0->int_bps= av_get_bits_per_sample_fmt(VAR_0->int_sample_fmt)/8;

    VAR_0->out.bps= av_get_bits_per_sample_fmt(VAR_0->out_sample_fmt)/8;



    VAR_0->in_convert = swr_audio_convert_alloc(VAR_0->int_sample_fmt,

                                            VAR_0-> in_sample_fmt, VAR_0-> in.ch_count, 0);

    VAR_0->out_convert= swr_audio_convert_alloc(VAR_0->out_sample_fmt,

                                            VAR_0->int_sample_fmt, VAR_0->out.ch_count, 0);





    VAR_0->postin= VAR_0->in;

    VAR_0->preout= VAR_0->out;

    VAR_0->midbuf= VAR_0->in;

    VAR_0->in_buffer= VAR_0->in;

    if(!VAR_0->resample_first){

        VAR_0->midbuf.ch_count= VAR_0->out.ch_count;

        VAR_0->in_buffer.ch_count = VAR_0->out.ch_count;

    }



    VAR_0->in_buffer.bps = VAR_0->postin.bps = VAR_0->midbuf.bps = VAR_0->preout.bps =  VAR_0->int_bps;

    VAR_0->in_buffer.planar = VAR_0->postin.planar = VAR_0->midbuf.planar = VAR_0->preout.planar =  1;





    if(VAR_0->rematrix && swr_rematrix_init(VAR_0)<0)

        return -1;



    return 0;

}

int FUNC_0(SwrContext *VAR_0){ VAR_0->in_buffer_index= 0; VAR_0->in_buffer_count= 0; VAR_0->resample_in_constraint= 0; free_temp(&VAR_0->postin); free_temp(&VAR_0->midbuf); free_temp(&VAR_0->preout); free_temp(&VAR_0->in_buffer); swr_audio_convert_free(&VAR_0-> in_convert); swr_audio_convert_free(&VAR_0->out_convert); VAR_0-> in.planar= VAR_0-> in_sample_fmt >= 0x100; VAR_0->out.planar= VAR_0->out_sample_fmt >= 0x100; VAR_0-> in_sample_fmt &= 0xFF; VAR_0->out_sample_fmt &= 0xFF; if(   VAR_0->int_sample_fmt != AV_SAMPLE_FMT_S16
&&VAR_0->int_sample_fmt != AV_SAMPLE_FMT_FLT){ av_log(VAR_0, AV_LOG_ERROR, "Requested sample format %VAR_0 is not supported internally, only float & S16 is supported\n", av_get_sample_fmt_name(VAR_0->int_sample_fmt)); return AVERROR(EINVAL); } if(VAR_0->in_sample_fmt <= AV_SAMPLE_FMT_S16 || VAR_0->int_sample_fmt==AV_SAMPLE_FMT_S16){ VAR_0->int_sample_fmt= AV_SAMPLE_FMT_S16; }else VAR_0->int_sample_fmt= AV_SAMPLE_FMT_FLT; if (VAR_0->out_sample_rate!=VAR_0->in_sample_rate || (VAR_0->flags & SWR_FLAG_RESAMPLE)){ VAR_0->resample = swr_resample_init(VAR_0->resample, VAR_0->out_sample_rate, VAR_0->in_sample_rate, 16, 10, 0, 0.8); }else swr_resample_free(&VAR_0->resample); if(VAR_0->int_sample_fmt != AV_SAMPLE_FMT_S16 && VAR_0->resample){ av_log(VAR_0, AV_LOG_ERROR, "Resampling only supported with internal s16 currently\n"); return -1; } if(!VAR_0-> in_ch_layout)
VAR_0-> in_ch_layout= guess_layout(VAR_0->in.ch_count); if(!VAR_0->out_ch_layout)
VAR_0->out_ch_layout= guess_layout(VAR_0->out.ch_count); VAR_0->rematrix= VAR_0->out_ch_layout  !=VAR_0->in_ch_layout; #define RSC 1  finetune
if(!VAR_0-> in.ch_count)
VAR_0-> in.ch_count= av_get_channel_layout_nb_channels(VAR_0-> in_ch_layout); if(!VAR_0->out.ch_count)
VAR_0->out.ch_count= av_get_channel_layout_nb_channels(VAR_0->out_ch_layout); av_assert0(VAR_0-> in.ch_count); av_assert0(VAR_0->out.ch_count); VAR_0->resample_first= RSC*VAR_0->out.ch_count/VAR_0->in.ch_count - RSC < VAR_0->out_sample_rate/(float)VAR_0-> in_sample_rate - 1.0; VAR_0-> in.bps= av_get_bits_per_sample_fmt(VAR_0-> in_sample_fmt)/8; VAR_0->int_bps= av_get_bits_per_sample_fmt(VAR_0->int_sample_fmt)/8; VAR_0->out.bps= av_get_bits_per_sample_fmt(VAR_0->out_sample_fmt)/8; VAR_0->in_convert = swr_audio_convert_alloc(VAR_0->int_sample_fmt,
VAR_0-> in_sample_fmt, VAR_0-> in.ch_count, 0); VAR_0->out_convert= swr_audio_convert_alloc(VAR_0->out_sample_fmt,
VAR_0->int_sample_fmt, VAR_0->out.ch_count, 0); VAR_0->postin= VAR_0->in; VAR_0->preout= VAR_0->out; VAR_0->midbuf= VAR_0->in; VAR_0->in_buffer= VAR_0->in; if(!VAR_0->resample_first){ VAR_0->midbuf.ch_count= VAR_0->out.ch_count; VAR_0->in_buffer.ch_count = VAR_0->out.ch_count; } VAR_0->in_buffer.bps = VAR_0->postin.bps = VAR_0->midbuf.bps = VAR_0->preout.bps =  VAR_0->int_bps; VAR_0->in_buffer.planar = VAR_0->postin.planar = VAR_0->midbuf.planar = VAR_0->preout.planar =  1; if(VAR_0->rematrix && swr_rematrix_init(VAR_0)<0)
return -1; return 0; }