static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt, int discard) {

    AviSynthContext *avs = s->priv_data;

    AVRational fps, samplerate;

    int samples;

    const char* error;



    if (avs->curr_sample >= avs->vi->num_audio_samples)

        return AVERROR_EOF;



    fps.num = avs->vi->fps_numerator;

    fps.den = avs->vi->fps_denominator;

    samplerate.num = avs->vi->audio_samples_per_second;

    samplerate.den = 1;



    if (avs_has_video(avs->vi)) {

        if (avs->curr_frame < avs->vi->num_frames)

            samples = av_rescale_q(avs->curr_frame, samplerate, fps) - avs->curr_sample;

        else

            samples = av_rescale_q(1, samplerate, fps);

    } else {

        samples = 1000;

    }



    // After seeking, audio may catch up with video.

    if (samples <= 0) {

        pkt->size = 0;

        pkt->data = NULL;

        return 0;

    }



    if (avs->curr_sample + samples > avs->vi->num_audio_samples)

        samples = avs->vi->num_audio_samples - avs->curr_sample;



    // This must happen even if the stream is discarded to prevent desync.

    avs->curr_sample += samples;

    if (discard)

        return 0;



    pkt->pts = avs->curr_sample;

    pkt->dts = avs->curr_sample;

    pkt->duration = samples;



    pkt->size = avs_bytes_per_channel_sample(avs->vi) * samples * avs->vi->nchannels;

    if (!pkt->size)

        return AVERROR_UNKNOWN;

    pkt->data = av_malloc(pkt->size);

    if (!pkt->data)

        return AVERROR_UNKNOWN;



    avs_library->avs_get_audio(avs->clip, pkt->data, avs->curr_sample, samples);

    error = avs_library->avs_clip_get_error(avs->clip);

    if (error) {

        av_log(s, AV_LOG_ERROR, "%s\n", error);

        avs->error = 1;

        av_freep(&pkt->data);

        return AVERROR_UNKNOWN;

    }

    return 0;

}

static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt, int discard) {

    AviSynthContext *avs = s->priv_data;

    AVRational fps, samplerate;

    int samples;

    const char* error;



    if (avs->curr_sample >= avs->vi->num_audio_samples)

        return AVERROR_EOF;



    fps.num = avs->vi->fps_numerator;

    fps.den = avs->vi->fps_denominator;

    samplerate.num = avs->vi->audio_samples_per_second;

    samplerate.den = 1;



    if (avs_has_video(avs->vi)) {

        if (avs->curr_frame < avs->vi->num_frames)

            samples = av_rescale_q(avs->curr_frame, samplerate, fps) - avs->curr_sample;

        else

            samples = av_rescale_q(1, samplerate, fps);

    } else {

        samples = 1000;

    }



    

    if (samples <= 0) {

        pkt->size = 0;

        pkt->data = NULL;

        return 0;

    }



    if (avs->curr_sample + samples > avs->vi->num_audio_samples)

        samples = avs->vi->num_audio_samples - avs->curr_sample;



    

    avs->curr_sample += samples;

    if (discard)

        return 0;



    pkt->pts = avs->curr_sample;

    pkt->dts = avs->curr_sample;

    pkt->duration = samples;



    pkt->size = avs_bytes_per_channel_sample(avs->vi) * samples * avs->vi->nchannels;

    if (!pkt->size)

        return AVERROR_UNKNOWN;

    pkt->data = av_malloc(pkt->size);

    if (!pkt->data)

        return AVERROR_UNKNOWN;



    avs_library->avs_get_audio(avs->clip, pkt->data, avs->curr_sample, samples);

    error = avs_library->avs_clip_get_error(avs->clip);

    if (error) {

        av_log(s, AV_LOG_ERROR, "%s\n", error);

        avs->error = 1;

        av_freep(&pkt->data);

        return AVERROR_UNKNOWN;

    }

    return 0;

}

    avs_library->avs_get_audio(avs->clip, pkt->data, avs->curr_sample, samples);
static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1, int VAR_2) {

    AviSynthContext *avs = VAR_0->priv_data;

    AVRational fps, samplerate;

    int VAR_3;

    const char* VAR_4;



    if (avs->curr_sample >= avs->vi->num_audio_samples)

        return AVERROR_EOF;



    fps.num = avs->vi->fps_numerator;

    fps.den = avs->vi->fps_denominator;

    samplerate.num = avs->vi->audio_samples_per_second;

    samplerate.den = 1;



    if (avs_has_video(avs->vi)) {

        if (avs->curr_frame < avs->vi->num_frames)

            VAR_3 = av_rescale_q(avs->curr_frame, samplerate, fps) - avs->curr_sample;

        else

            VAR_3 = av_rescale_q(1, samplerate, fps);

    } else {

        VAR_3 = 1000;

    }



    

    if (VAR_3 <= 0) {

        VAR_1->size = 0;

        VAR_1->data = NULL;

        return 0;

    }



    if (avs->curr_sample + VAR_3 > avs->vi->num_audio_samples)

        VAR_3 = avs->vi->num_audio_samples - avs->curr_sample;



    

    avs->curr_sample += VAR_3;

    if (VAR_2)

        return 0;



    VAR_1->pts = avs->curr_sample;

    VAR_1->dts = avs->curr_sample;

    VAR_1->duration = VAR_3;



    VAR_1->size = avs_bytes_per_channel_sample(avs->vi) * VAR_3 * avs->vi->nchannels;

    if (!VAR_1->size)

        return AVERROR_UNKNOWN;

    VAR_1->data = av_malloc(VAR_1->size);

    if (!VAR_1->data)

        return AVERROR_UNKNOWN;



    avs_library->avs_get_audio(avs->clip, VAR_1->data, avs->curr_sample, VAR_3);

    VAR_4 = avs_library->avs_clip_get_error(avs->clip);

    if (VAR_4) {

        av_log(VAR_0, AV_LOG_ERROR, "%VAR_0\n", VAR_4);

        avs->VAR_4 = 1;

        av_freep(&VAR_1->data);

        return AVERROR_UNKNOWN;

    }

    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1, int VAR_2) { AviSynthContext *avs = VAR_0->priv_data; AVRational fps, samplerate; int VAR_3; const char* VAR_4; if (avs->curr_sample >= avs->vi->num_audio_samples)
return AVERROR_EOF; fps.num = avs->vi->fps_numerator; fps.den = avs->vi->fps_denominator; samplerate.num = avs->vi->audio_samples_per_second; samplerate.den = 1; if (avs_has_video(avs->vi)) { if (avs->curr_frame < avs->vi->num_frames)
VAR_3 = av_rescale_q(avs->curr_frame, samplerate, fps) - avs->curr_sample; else
VAR_3 = av_rescale_q(1, samplerate, fps); } else { VAR_3 = 1000; } if (VAR_3 <= 0) { VAR_1->size = 0; VAR_1->data = NULL; return 0; } if (avs->curr_sample + VAR_3 > avs->vi->num_audio_samples)
VAR_3 = avs->vi->num_audio_samples - avs->curr_sample; avs->curr_sample += VAR_3; if (VAR_2)
return 0; VAR_1->pts = avs->curr_sample; VAR_1->dts = avs->curr_sample; VAR_1->duration = VAR_3; VAR_1->size = avs_bytes_per_channel_sample(avs->vi) * VAR_3 * avs->vi->nchannels; if (!VAR_1->size)
return AVERROR_UNKNOWN; VAR_1->data = av_malloc(VAR_1->size); if (!VAR_1->data)
return AVERROR_UNKNOWN; avs_library->avs_get_audio(avs->clip, VAR_1->data, avs->curr_sample, VAR_3); VAR_4 = avs_library->avs_clip_get_error(avs->clip); if (VAR_4) { av_log(VAR_0, AV_LOG_ERROR, "%VAR_0\n", VAR_4); avs->VAR_4 = 1; av_freep(&VAR_1->data); return AVERROR_UNKNOWN; } return 0; }