static int synchronize_audio(VideoState *is, short *samples,

                             int samples_size1, double pts)

{

    int n, samples_size;

    double ref_clock;



    n = 2 * is->audio_st->codec->channels;

    samples_size = samples_size1;



    /* if not master, then we try to remove or add samples to correct the clock */

    if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||

         is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {

        double diff, avg_diff;

        int wanted_size, min_size, max_size, nb_samples;



        ref_clock = get_master_clock(is);

        diff = get_audio_clock(is) - ref_clock;



        if (diff < AV_NOSYNC_THRESHOLD) {

            is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;

            if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {

                /* not enough measures to have a correct estimate */

                is->audio_diff_avg_count++;

            } else {

                /* estimate the A-V difference */

                avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);



                if (fabs(avg_diff) >= is->audio_diff_threshold) {

                    wanted_size = samples_size + ((int)(diff * is->audio_st->codec->sample_rate) * n);

                    nb_samples = samples_size / n;



                    min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;

                    max_size = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;

                    if (wanted_size < min_size)

                        wanted_size = min_size;

                    else if (wanted_size > max_size)

                        wanted_size = max_size;



                    /* add or remove samples to correction the synchro */

                    if (wanted_size < samples_size) {

                        /* remove samples */

                        samples_size = wanted_size;

                    } else if (wanted_size > samples_size) {

                        uint8_t *samples_end, *q;

                        int nb;



                        /* add samples */

                        nb = (samples_size - wanted_size);

                        samples_end = (uint8_t *)samples + samples_size - n;

                        q = samples_end + n;

                        while (nb > 0) {

                            memcpy(q, samples_end, n);

                            q += n;

                            nb -= n;

                        }

                        samples_size = wanted_size;

                    }

                }

                av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",

                        diff, avg_diff, samples_size - samples_size1,

                        is->audio_clock, is->video_clock, is->audio_diff_threshold);

            }

        } else {

            /* too big difference : may be initial PTS errors, so

               reset A-V filter */

            is->audio_diff_avg_count = 0;

            is->audio_diff_cum       = 0;

        }

    }



    return samples_size;

}

static int synchronize_audio(VideoState *is, short *samples,

                             int samples_size1, double pts)

{

    int n, samples_size;

    double ref_clock;



    n = 2 * is->audio_st->codec->channels;

    samples_size = samples_size1;



    

    if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||

         is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {

        double diff, avg_diff;

        int wanted_size, min_size, max_size, nb_samples;



        ref_clock = get_master_clock(is);

        diff = get_audio_clock(is) - ref_clock;



        if (diff < AV_NOSYNC_THRESHOLD) {

            is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;

            if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {

                

                is->audio_diff_avg_count++;

            } else {

                

                avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);



                if (fabs(avg_diff) >= is->audio_diff_threshold) {

                    wanted_size = samples_size + ((int)(diff * is->audio_st->codec->sample_rate) * n);

                    nb_samples = samples_size / n;



                    min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;

                    max_size = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;

                    if (wanted_size < min_size)

                        wanted_size = min_size;

                    else if (wanted_size > max_size)

                        wanted_size = max_size;



                    

                    if (wanted_size < samples_size) {

                        

                        samples_size = wanted_size;

                    } else if (wanted_size > samples_size) {

                        uint8_t *samples_end, *q;

                        int nb;



                        

                        nb = (samples_size - wanted_size);

                        samples_end = (uint8_t *)samples + samples_size - n;

                        q = samples_end + n;

                        while (nb > 0) {

                            memcpy(q, samples_end, n);

                            q += n;

                            nb -= n;

                        }

                        samples_size = wanted_size;

                    }

                }

                av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",

                        diff, avg_diff, samples_size - samples_size1,

                        is->audio_clock, is->video_clock, is->audio_diff_threshold);

            }

        } else {

            



            is->audio_diff_avg_count = 0;

            is->audio_diff_cum       = 0;

        }

    }



    return samples_size;

}

static int FUNC_0(VideoState *VAR_0, short *VAR_1,

                             int VAR_2, double VAR_3)

{

    int VAR_4, VAR_5;

    double VAR_6;



    VAR_4 = 2 * VAR_0->audio_st->codec->channels;

    VAR_5 = VAR_2;



    

    if (((VAR_0->av_sync_type == AV_SYNC_VIDEO_MASTER && VAR_0->video_st) ||

         VAR_0->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {

        double VAR_7, VAR_8;

        int VAR_9, VAR_10, VAR_11, VAR_12;



        VAR_6 = get_master_clock(VAR_0);

        VAR_7 = get_audio_clock(VAR_0) - VAR_6;



        if (VAR_7 < AV_NOSYNC_THRESHOLD) {

            VAR_0->audio_diff_cum = VAR_7 + VAR_0->audio_diff_avg_coef * VAR_0->audio_diff_cum;

            if (VAR_0->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {

                

                VAR_0->audio_diff_avg_count++;

            } else {

                

                VAR_8 = VAR_0->audio_diff_cum * (1.0 - VAR_0->audio_diff_avg_coef);



                if (fabs(VAR_8) >= VAR_0->audio_diff_threshold) {

                    VAR_9 = VAR_5 + ((int)(VAR_7 * VAR_0->audio_st->codec->sample_rate) * VAR_4);

                    VAR_12 = VAR_5 / VAR_4;



                    VAR_10 = ((VAR_12 * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * VAR_4;

                    VAR_11 = ((VAR_12 * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * VAR_4;

                    if (VAR_9 < VAR_10)

                        VAR_9 = VAR_10;

                    else if (VAR_9 > VAR_11)

                        VAR_9 = VAR_11;



                    

                    if (VAR_9 < VAR_5) {

                        

                        VAR_5 = VAR_9;

                    } else if (VAR_9 > VAR_5) {

                        uint8_t *samples_end, *q;

                        int VAR_13;



                        

                        VAR_13 = (VAR_5 - VAR_9);

                        samples_end = (uint8_t *)VAR_1 + VAR_5 - VAR_4;

                        q = samples_end + VAR_4;

                        while (VAR_13 > 0) {

                            memcpy(q, samples_end, VAR_4);

                            q += VAR_4;

                            VAR_13 -= VAR_4;

                        }

                        VAR_5 = VAR_9;

                    }

                }

                av_dlog(NULL, "VAR_7=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\VAR_4",

                        VAR_7, VAR_8, VAR_5 - VAR_2,

                        VAR_0->audio_clock, VAR_0->video_clock, VAR_0->audio_diff_threshold);

            }

        } else {

            



            VAR_0->audio_diff_avg_count = 0;

            VAR_0->audio_diff_cum       = 0;

        }

    }



    return VAR_5;

}

static int FUNC_0(VideoState *VAR_0, short *VAR_1,
int VAR_2, double VAR_3)
{ int VAR_4, VAR_5; double VAR_6; VAR_4 = 2 * VAR_0->audio_st->codec->channels; VAR_5 = VAR_2; if (((VAR_0->av_sync_type == AV_SYNC_VIDEO_MASTER && VAR_0->video_st) ||
VAR_0->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) { double VAR_7, VAR_8; int VAR_9, VAR_10, VAR_11, VAR_12; VAR_6 = get_master_clock(VAR_0); VAR_7 = get_audio_clock(VAR_0) - VAR_6; if (VAR_7 < AV_NOSYNC_THRESHOLD) { VAR_0->audio_diff_cum = VAR_7 + VAR_0->audio_diff_avg_coef * VAR_0->audio_diff_cum; if (VAR_0->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) { VAR_0->audio_diff_avg_count++; } else { VAR_8 = VAR_0->audio_diff_cum * (1.0 - VAR_0->audio_diff_avg_coef); if (fabs(VAR_8) >= VAR_0->audio_diff_threshold) { VAR_9 = VAR_5 + ((int)(VAR_7 * VAR_0->audio_st->codec->sample_rate) * VAR_4); VAR_12 = VAR_5 / VAR_4; VAR_10 = ((VAR_12 * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * VAR_4; VAR_11 = ((VAR_12 * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * VAR_4; if (VAR_9 < VAR_10)
VAR_9 = VAR_10; else if (VAR_9 > VAR_11)
VAR_9 = VAR_11; if (VAR_9 < VAR_5) { VAR_5 = VAR_9; } else if (VAR_9 > VAR_5) { uint8_t *samples_end, *q; int VAR_13; VAR_13 = (VAR_5 - VAR_9); samples_end = (uint8_t *)VAR_1 + VAR_5 - VAR_4; q = samples_end + VAR_4; while (VAR_13 > 0) { memcpy(q, samples_end, VAR_4); q += VAR_4; VAR_13 -= VAR_4; } VAR_5 = VAR_9; } } av_dlog(NULL, "VAR_7=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\VAR_4",
VAR_7, VAR_8, VAR_5 - VAR_2,
VAR_0->audio_clock, VAR_0->video_clock, VAR_0->audio_diff_threshold); } } else { VAR_0->audio_diff_avg_count = 0; VAR_0->audio_diff_cum       = 0; } } return VAR_5; }