static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,

                                const AVFrame *frame, int *got_packet_ptr)

{

    TWOLAMEContext *s = avctx->priv_data;

    int ret;



    if ((ret = ff_alloc_packet(avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)

        return ret;



    if (frame) {

        switch (avctx->sample_fmt) {

        case AV_SAMPLE_FMT_FLT:

            ret = twolame_encode_buffer_float32_interleaved(s->glopts,

                                                            (const float *)frame->data[0],

                                                            frame->nb_samples,

                                                            avpkt->data,

                                                            avpkt->size);

            break;

        case AV_SAMPLE_FMT_FLTP:

            ret = twolame_encode_buffer_float32(s->glopts,

                                                (const float *)frame->data[0],

                                                (const float *)frame->data[1],

                                                frame->nb_samples,

                                                avpkt->data, avpkt->size);

            break;

        case AV_SAMPLE_FMT_S16:

            ret = twolame_encode_buffer_interleaved(s->glopts,

                                                    (const short int *)frame->data[0],

                                                    frame->nb_samples,

                                                    avpkt->data, avpkt->size);

            break;

        case AV_SAMPLE_FMT_S16P:

            ret = twolame_encode_buffer(s->glopts,

                                        (const short int *)frame->data[0],

                                        (const short int *)frame->data[1],

                                        frame->nb_samples,

                                        avpkt->data, avpkt->size);

            break;

        default:

            av_log(avctx, AV_LOG_ERROR,

                   "Unsupported sample format %d.\n", avctx->sample_fmt);

            return AVERROR_BUG;

        }

    } else {

        ret = twolame_encode_flush(s->glopts, avpkt->data, avpkt->size);

    }



    if (!ret)     // no bytes written

        return 0;

    if (ret < 0)  // twolame error

        return AVERROR_UNKNOWN;



    avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);

    if (frame) {

        if (frame->pts != AV_NOPTS_VALUE)

            avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);

    } else {

        avpkt->pts = s->next_pts;

    }

    // this is for setting pts for flushed packet(s).

    if (avpkt->pts != AV_NOPTS_VALUE)

        s->next_pts = avpkt->pts + avpkt->duration;



    av_shrink_packet(avpkt, ret);

    *got_packet_ptr = 1;

    return 0;

}

static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,

                                const AVFrame *frame, int *got_packet_ptr)

{

    TWOLAMEContext *s = avctx->priv_data;

    int ret;



    if ((ret = ff_alloc_packet(avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)

        return ret;



    if (frame) {

        switch (avctx->sample_fmt) {

        case AV_SAMPLE_FMT_FLT:

            ret = twolame_encode_buffer_float32_interleaved(s->glopts,

                                                            (const float *)frame->data[0],

                                                            frame->nb_samples,

                                                            avpkt->data,

                                                            avpkt->size);

            break;

        case AV_SAMPLE_FMT_FLTP:

            ret = twolame_encode_buffer_float32(s->glopts,

                                                (const float *)frame->data[0],

                                                (const float *)frame->data[1],

                                                frame->nb_samples,

                                                avpkt->data, avpkt->size);

            break;

        case AV_SAMPLE_FMT_S16:

            ret = twolame_encode_buffer_interleaved(s->glopts,

                                                    (const short int *)frame->data[0],

                                                    frame->nb_samples,

                                                    avpkt->data, avpkt->size);

            break;

        case AV_SAMPLE_FMT_S16P:

            ret = twolame_encode_buffer(s->glopts,

                                        (const short int *)frame->data[0],

                                        (const short int *)frame->data[1],

                                        frame->nb_samples,

                                        avpkt->data, avpkt->size);

            break;

        default:

            av_log(avctx, AV_LOG_ERROR,

                   "Unsupported sample format %d.\n", avctx->sample_fmt);

            return AVERROR_BUG;

        }

    } else {

        ret = twolame_encode_flush(s->glopts, avpkt->data, avpkt->size);

    }



    if (!ret)     

        return 0;

    if (ret < 0)  

        return AVERROR_UNKNOWN;



    avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);

    if (frame) {

        if (frame->pts != AV_NOPTS_VALUE)

            avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);

    } else {

        avpkt->pts = s->next_pts;

    }

    

    if (avpkt->pts != AV_NOPTS_VALUE)

        s->next_pts = avpkt->pts + avpkt->duration;



    av_shrink_packet(avpkt, ret);

    *got_packet_ptr = 1;

    return 0;

}

    avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
static int FUNC_0(AVCodecContext *VAR_0, AVPacket *VAR_1,

                                const AVFrame *VAR_2, int *VAR_3)

{

    TWOLAMEContext *s = VAR_0->priv_data;

    int VAR_4;



    if ((VAR_4 = ff_alloc_packet(VAR_1, MPA_MAX_CODED_FRAME_SIZE)) < 0)

        return VAR_4;



    if (VAR_2) {

        switch (VAR_0->sample_fmt) {

        case AV_SAMPLE_FMT_FLT:

            VAR_4 = twolame_encode_buffer_float32_interleaved(s->glopts,

                                                            (const float *)VAR_2->data[0],

                                                            VAR_2->nb_samples,

                                                            VAR_1->data,

                                                            VAR_1->size);

            break;

        case AV_SAMPLE_FMT_FLTP:

            VAR_4 = twolame_encode_buffer_float32(s->glopts,

                                                (const float *)VAR_2->data[0],

                                                (const float *)VAR_2->data[1],

                                                VAR_2->nb_samples,

                                                VAR_1->data, VAR_1->size);

            break;

        case AV_SAMPLE_FMT_S16:

            VAR_4 = twolame_encode_buffer_interleaved(s->glopts,

                                                    (const short int *)VAR_2->data[0],

                                                    VAR_2->nb_samples,

                                                    VAR_1->data, VAR_1->size);

            break;

        case AV_SAMPLE_FMT_S16P:

            VAR_4 = twolame_encode_buffer(s->glopts,

                                        (const short int *)VAR_2->data[0],

                                        (const short int *)VAR_2->data[1],

                                        VAR_2->nb_samples,

                                        VAR_1->data, VAR_1->size);

            break;

        default:

            av_log(VAR_0, AV_LOG_ERROR,

                   "Unsupported sample format %d.\n", VAR_0->sample_fmt);

            return AVERROR_BUG;

        }

    } else {

        VAR_4 = twolame_encode_flush(s->glopts, VAR_1->data, VAR_1->size);

    }



    if (!VAR_4)     

        return 0;

    if (VAR_4 < 0)  

        return AVERROR_UNKNOWN;



    VAR_1->duration = ff_samples_to_time_base(VAR_0, VAR_2->nb_samples);

    if (VAR_2) {

        if (VAR_2->pts != AV_NOPTS_VALUE)

            VAR_1->pts = VAR_2->pts - ff_samples_to_time_base(VAR_0, VAR_0->initial_padding);

    } else {

        VAR_1->pts = s->next_pts;

    }

    

    if (VAR_1->pts != AV_NOPTS_VALUE)

        s->next_pts = VAR_1->pts + VAR_1->duration;



    av_shrink_packet(VAR_1, VAR_4);

    *VAR_3 = 1;

    return 0;

}

static int FUNC_0(AVCodecContext *VAR_0, AVPacket *VAR_1,
const AVFrame *VAR_2, int *VAR_3)
{ TWOLAMEContext *s = VAR_0->priv_data; int VAR_4; if ((VAR_4 = ff_alloc_packet(VAR_1, MPA_MAX_CODED_FRAME_SIZE)) < 0)
return VAR_4; if (VAR_2) { switch (VAR_0->sample_fmt) { case AV_SAMPLE_FMT_FLT:
VAR_4 = twolame_encode_buffer_float32_interleaved(s->glopts,
(const float *)VAR_2->data[0],
VAR_2->nb_samples,
VAR_1->data,
VAR_1->size); break; case AV_SAMPLE_FMT_FLTP:
VAR_4 = twolame_encode_buffer_float32(s->glopts,
(const float *)VAR_2->data[0],
(const float *)VAR_2->data[1],
VAR_2->nb_samples,
VAR_1->data, VAR_1->size); break; case AV_SAMPLE_FMT_S16:
VAR_4 = twolame_encode_buffer_interleaved(s->glopts,
(const short int *)VAR_2->data[0],
VAR_2->nb_samples,
VAR_1->data, VAR_1->size); break; case AV_SAMPLE_FMT_S16P:
VAR_4 = twolame_encode_buffer(s->glopts,
(const short int *)VAR_2->data[0],
(const short int *)VAR_2->data[1],
VAR_2->nb_samples,
VAR_1->data, VAR_1->size); break; default:
av_log(VAR_0, AV_LOG_ERROR,
"Unsupported sample format %d.\n", VAR_0->sample_fmt); return AVERROR_BUG; } } else { VAR_4 = twolame_encode_flush(s->glopts, VAR_1->data, VAR_1->size); } if (!VAR_4)
return 0; if (VAR_4 < 0)
return AVERROR_UNKNOWN; VAR_1->duration = ff_samples_to_time_base(VAR_0, VAR_2->nb_samples); if (VAR_2) { if (VAR_2->pts != AV_NOPTS_VALUE)
VAR_1->pts = VAR_2->pts - ff_samples_to_time_base(VAR_0, VAR_0->initial_padding); } else { VAR_1->pts = s->next_pts; } if (VAR_1->pts != AV_NOPTS_VALUE)
s->next_pts = VAR_1->pts + VAR_1->duration; av_shrink_packet(VAR_1, VAR_4); *VAR_3 = 1; return 0; }