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

                               const AVFrame *frame, int *got_packet_ptr)

{

    S302MEncContext *s = avctx->priv_data;

    const int buf_size = AES3_HEADER_LEN +

                        (frame->nb_samples *

                         avctx->channels *

                        (avctx->bits_per_raw_sample + 4)) / 8;

    int ret, c, channels;

    uint8_t *o;

    PutBitContext pb;



    if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size)) < 0)

        return ret;



    o = avpkt->data;

    init_put_bits(&pb, o, buf_size * 8);

    put_bits(&pb, 16, buf_size - AES3_HEADER_LEN);

    put_bits(&pb, 2, (avctx->channels - 2) >> 1);   // number of channels

    put_bits(&pb, 8, 0);                            // channel ID

    put_bits(&pb, 2, (avctx->bits_per_raw_sample - 16) / 4); // bits per samples (0 = 16bit, 1 = 20bit, 2 = 24bit)

    put_bits(&pb, 4, 0);                            // alignments

    flush_put_bits(&pb);

    o += AES3_HEADER_LEN;



    if (avctx->bits_per_raw_sample == 24) {

        const uint32_t *samples = (uint32_t *)frame->data[0];



        for (c = 0; c < frame->nb_samples; c++) {

            uint8_t vucf = s->framing_index == 0 ? 0x10: 0;



            for (channels = 0; channels < avctx->channels; channels += 2) {

                o[0] = ff_reverse[(samples[0] & 0x0000FF00) >> 8];

                o[1] = ff_reverse[(samples[0] & 0x00FF0000) >> 16];

                o[2] = ff_reverse[(samples[0] & 0xFF000000) >> 24];

                o[3] = ff_reverse[(samples[1] & 0x00000F00) >> 4] | vucf;

                o[4] = ff_reverse[(samples[1] & 0x000FF000) >> 12];

                o[5] = ff_reverse[(samples[1] & 0x0FF00000) >> 20];

                o[6] = ff_reverse[(samples[1] & 0xF0000000) >> 28];

                o += 7;

                samples += 2;

            }



            s->framing_index++;

            if (s->framing_index >= 192)

                s->framing_index = 0;

        }

    } else if (avctx->bits_per_raw_sample == 20) {

        const uint32_t *samples = (uint32_t *)frame->data[0];



        for (c = 0; c < frame->nb_samples; c++) {

            uint8_t vucf = s->framing_index == 0 ? 0x80: 0;



            for (channels = 0; channels < avctx->channels; channels += 2) {

                o[0] = ff_reverse[ (samples[0] & 0x000FF000) >> 12];

                o[1] = ff_reverse[ (samples[0] & 0x0FF00000) >> 20];

                o[2] = ff_reverse[((samples[0] & 0xF0000000) >> 28) | vucf];

                o[3] = ff_reverse[ (samples[1] & 0x000FF000) >> 12];

                o[4] = ff_reverse[ (samples[1] & 0x0FF00000) >> 20];

                o[5] = ff_reverse[ (samples[1] & 0xF0000000) >> 28];

                o += 6;

                samples += 2;

            }



            s->framing_index++;

            if (s->framing_index >= 192)

                s->framing_index = 0;

        }

    } else if (avctx->bits_per_raw_sample == 16) {

        const uint16_t *samples = (uint16_t *)frame->data[0];



        for (c = 0; c < frame->nb_samples; c++) {

            uint8_t vucf = s->framing_index == 0 ? 0x10 : 0;



            for (channels = 0; channels < avctx->channels; channels += 2) {

                o[0] = ff_reverse[ samples[0] & 0xFF];

                o[1] = ff_reverse[(samples[0] & 0xFF00) >>  8];

                o[2] = ff_reverse[(samples[1] & 0x0F)   <<  4] | vucf;

                o[3] = ff_reverse[(samples[1] & 0x0FF0) >>  4];

                o[4] = ff_reverse[(samples[1] & 0xF000) >> 12];

                o += 5;

                samples += 2;



            }



            s->framing_index++;

            if (s->framing_index >= 192)

                s->framing_index = 0;

        }

    }



    *got_packet_ptr = 1;



    return 0;

}

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

                               const AVFrame *frame, int *got_packet_ptr)

{

    S302MEncContext *s = avctx->priv_data;

    const int buf_size = AES3_HEADER_LEN +

                        (frame->nb_samples *

                         avctx->channels *

                        (avctx->bits_per_raw_sample + 4)) / 8;

    int ret, c, channels;

    uint8_t *o;

    PutBitContext pb;



    if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size)) < 0)

        return ret;



    o = avpkt->data;

    init_put_bits(&pb, o, buf_size * 8);

    put_bits(&pb, 16, buf_size - AES3_HEADER_LEN);

    put_bits(&pb, 2, (avctx->channels - 2) >> 1);   

    put_bits(&pb, 8, 0);                            

    put_bits(&pb, 2, (avctx->bits_per_raw_sample - 16) / 4); 

    put_bits(&pb, 4, 0);                            

    flush_put_bits(&pb);

    o += AES3_HEADER_LEN;



    if (avctx->bits_per_raw_sample == 24) {

        const uint32_t *samples = (uint32_t *)frame->data[0];



        for (c = 0; c < frame->nb_samples; c++) {

            uint8_t vucf = s->framing_index == 0 ? 0x10: 0;



            for (channels = 0; channels < avctx->channels; channels += 2) {

                o[0] = ff_reverse[(samples[0] & 0x0000FF00) >> 8];

                o[1] = ff_reverse[(samples[0] & 0x00FF0000) >> 16];

                o[2] = ff_reverse[(samples[0] & 0xFF000000) >> 24];

                o[3] = ff_reverse[(samples[1] & 0x00000F00) >> 4] | vucf;

                o[4] = ff_reverse[(samples[1] & 0x000FF000) >> 12];

                o[5] = ff_reverse[(samples[1] & 0x0FF00000) >> 20];

                o[6] = ff_reverse[(samples[1] & 0xF0000000) >> 28];

                o += 7;

                samples += 2;

            }



            s->framing_index++;

            if (s->framing_index >= 192)

                s->framing_index = 0;

        }

    } else if (avctx->bits_per_raw_sample == 20) {

        const uint32_t *samples = (uint32_t *)frame->data[0];



        for (c = 0; c < frame->nb_samples; c++) {

            uint8_t vucf = s->framing_index == 0 ? 0x80: 0;



            for (channels = 0; channels < avctx->channels; channels += 2) {

                o[0] = ff_reverse[ (samples[0] & 0x000FF000) >> 12];

                o[1] = ff_reverse[ (samples[0] & 0x0FF00000) >> 20];

                o[2] = ff_reverse[((samples[0] & 0xF0000000) >> 28) | vucf];

                o[3] = ff_reverse[ (samples[1] & 0x000FF000) >> 12];

                o[4] = ff_reverse[ (samples[1] & 0x0FF00000) >> 20];

                o[5] = ff_reverse[ (samples[1] & 0xF0000000) >> 28];

                o += 6;

                samples += 2;

            }



            s->framing_index++;

            if (s->framing_index >= 192)

                s->framing_index = 0;

        }

    } else if (avctx->bits_per_raw_sample == 16) {

        const uint16_t *samples = (uint16_t *)frame->data[0];



        for (c = 0; c < frame->nb_samples; c++) {

            uint8_t vucf = s->framing_index == 0 ? 0x10 : 0;



            for (channels = 0; channels < avctx->channels; channels += 2) {

                o[0] = ff_reverse[ samples[0] & 0xFF];

                o[1] = ff_reverse[(samples[0] & 0xFF00) >>  8];

                o[2] = ff_reverse[(samples[1] & 0x0F)   <<  4] | vucf;

                o[3] = ff_reverse[(samples[1] & 0x0FF0) >>  4];

                o[4] = ff_reverse[(samples[1] & 0xF000) >> 12];

                o += 5;

                samples += 2;



            }



            s->framing_index++;

            if (s->framing_index >= 192)

                s->framing_index = 0;

        }

    }



    *got_packet_ptr = 1;



    return 0;

}

    init_put_bits(&pb, o, buf_size * 8);
static int FUNC_0(AVCodecContext *VAR_0, AVPacket *VAR_1,

                               const AVFrame *VAR_2, int *VAR_3)

{

    S302MEncContext *s = VAR_0->priv_data;

    const int VAR_4 = AES3_HEADER_LEN +

                        (VAR_2->nb_samples *

                         VAR_0->VAR_7 *

                        (VAR_0->bits_per_raw_sample + 4)) / 8;

    int VAR_5, VAR_6, VAR_7;

    uint8_t *o;

    PutBitContext pb;



    if ((VAR_5 = ff_alloc_packet2(VAR_0, VAR_1, VAR_4)) < 0)

        return VAR_5;



    o = VAR_1->data;

    init_put_bits(&pb, o, VAR_4 * 8);

    put_bits(&pb, 16, VAR_4 - AES3_HEADER_LEN);

    put_bits(&pb, 2, (VAR_0->VAR_7 - 2) >> 1);   

    put_bits(&pb, 8, 0);                            

    put_bits(&pb, 2, (VAR_0->bits_per_raw_sample - 16) / 4); 

    put_bits(&pb, 4, 0);                            

    flush_put_bits(&pb);

    o += AES3_HEADER_LEN;



    if (VAR_0->bits_per_raw_sample == 24) {

        const uint32_t *VAR_9 = (uint32_t *)VAR_2->data[0];



        for (VAR_6 = 0; VAR_6 < VAR_2->nb_samples; VAR_6++) {

            uint8_t vucf = s->framing_index == 0 ? 0x10: 0;



            for (VAR_7 = 0; VAR_7 < VAR_0->VAR_7; VAR_7 += 2) {

                o[0] = ff_reverse[(VAR_9[0] & 0x0000FF00) >> 8];

                o[1] = ff_reverse[(VAR_9[0] & 0x00FF0000) >> 16];

                o[2] = ff_reverse[(VAR_9[0] & 0xFF000000) >> 24];

                o[3] = ff_reverse[(VAR_9[1] & 0x00000F00) >> 4] | vucf;

                o[4] = ff_reverse[(VAR_9[1] & 0x000FF000) >> 12];

                o[5] = ff_reverse[(VAR_9[1] & 0x0FF00000) >> 20];

                o[6] = ff_reverse[(VAR_9[1] & 0xF0000000) >> 28];

                o += 7;

                VAR_9 += 2;

            }



            s->framing_index++;

            if (s->framing_index >= 192)

                s->framing_index = 0;

        }

    } else if (VAR_0->bits_per_raw_sample == 20) {

        const uint32_t *VAR_9 = (uint32_t *)VAR_2->data[0];



        for (VAR_6 = 0; VAR_6 < VAR_2->nb_samples; VAR_6++) {

            uint8_t vucf = s->framing_index == 0 ? 0x80: 0;



            for (VAR_7 = 0; VAR_7 < VAR_0->VAR_7; VAR_7 += 2) {

                o[0] = ff_reverse[ (VAR_9[0] & 0x000FF000) >> 12];

                o[1] = ff_reverse[ (VAR_9[0] & 0x0FF00000) >> 20];

                o[2] = ff_reverse[((VAR_9[0] & 0xF0000000) >> 28) | vucf];

                o[3] = ff_reverse[ (VAR_9[1] & 0x000FF000) >> 12];

                o[4] = ff_reverse[ (VAR_9[1] & 0x0FF00000) >> 20];

                o[5] = ff_reverse[ (VAR_9[1] & 0xF0000000) >> 28];

                o += 6;

                VAR_9 += 2;

            }



            s->framing_index++;

            if (s->framing_index >= 192)

                s->framing_index = 0;

        }

    } else if (VAR_0->bits_per_raw_sample == 16) {

        const uint16_t *VAR_9 = (uint16_t *)VAR_2->data[0];



        for (VAR_6 = 0; VAR_6 < VAR_2->nb_samples; VAR_6++) {

            uint8_t vucf = s->framing_index == 0 ? 0x10 : 0;



            for (VAR_7 = 0; VAR_7 < VAR_0->VAR_7; VAR_7 += 2) {

                o[0] = ff_reverse[ VAR_9[0] & 0xFF];

                o[1] = ff_reverse[(VAR_9[0] & 0xFF00) >>  8];

                o[2] = ff_reverse[(VAR_9[1] & 0x0F)   <<  4] | vucf;

                o[3] = ff_reverse[(VAR_9[1] & 0x0FF0) >>  4];

                o[4] = ff_reverse[(VAR_9[1] & 0xF000) >> 12];

                o += 5;

                VAR_9 += 2;



            }



            s->framing_index++;

            if (s->framing_index >= 192)

                s->framing_index = 0;

        }

    }



    *VAR_3 = 1;



    return 0;

}

static int FUNC_0(AVCodecContext *VAR_0, AVPacket *VAR_1,
const AVFrame *VAR_2, int *VAR_3)
{ S302MEncContext *s = VAR_0->priv_data; const int VAR_4 = AES3_HEADER_LEN +
(VAR_2->nb_samples *
VAR_0->VAR_7 *
(VAR_0->bits_per_raw_sample + 4)) / 8; int VAR_5, VAR_6, VAR_7; uint8_t *o; PutBitContext pb; if ((VAR_5 = ff_alloc_packet2(VAR_0, VAR_1, VAR_4)) < 0)
return VAR_5; o = VAR_1->data; init_put_bits(&pb, o, VAR_4 * 8); put_bits(&pb, 16, VAR_4 - AES3_HEADER_LEN); put_bits(&pb, 2, (VAR_0->VAR_7 - 2) >> 1); put_bits(&pb, 8, 0); put_bits(&pb, 2, (VAR_0->bits_per_raw_sample - 16) / 4); put_bits(&pb, 4, 0); flush_put_bits(&pb); o += AES3_HEADER_LEN; if (VAR_0->bits_per_raw_sample == 24) { const uint32_t *VAR_9 = (uint32_t *)VAR_2->data[0]; for (VAR_6 = 0; VAR_6 < VAR_2->nb_samples; VAR_6++) { uint8_t vucf = s->framing_index == 0 ? 0x10: 0; for (VAR_7 = 0; VAR_7 < VAR_0->VAR_7; VAR_7 += 2) { o[0] = ff_reverse[(VAR_9[0] & 0x0000FF00) >> 8]; o[1] = ff_reverse[(VAR_9[0] & 0x00FF0000) >> 16]; o[2] = ff_reverse[(VAR_9[0] & 0xFF000000) >> 24]; o[3] = ff_reverse[(VAR_9[1] & 0x00000F00) >> 4] | vucf; o[4] = ff_reverse[(VAR_9[1] & 0x000FF000) >> 12]; o[5] = ff_reverse[(VAR_9[1] & 0x0FF00000) >> 20]; o[6] = ff_reverse[(VAR_9[1] & 0xF0000000) >> 28]; o += 7; VAR_9 += 2; } s->framing_index++; if (s->framing_index >= 192)
s->framing_index = 0; } } else if (VAR_0->bits_per_raw_sample == 20) { const uint32_t *VAR_9 = (uint32_t *)VAR_2->data[0]; for (VAR_6 = 0; VAR_6 < VAR_2->nb_samples; VAR_6++) { uint8_t vucf = s->framing_index == 0 ? 0x80: 0; for (VAR_7 = 0; VAR_7 < VAR_0->VAR_7; VAR_7 += 2) { o[0] = ff_reverse[ (VAR_9[0] & 0x000FF000) >> 12]; o[1] = ff_reverse[ (VAR_9[0] & 0x0FF00000) >> 20]; o[2] = ff_reverse[((VAR_9[0] & 0xF0000000) >> 28) | vucf]; o[3] = ff_reverse[ (VAR_9[1] & 0x000FF000) >> 12]; o[4] = ff_reverse[ (VAR_9[1] & 0x0FF00000) >> 20]; o[5] = ff_reverse[ (VAR_9[1] & 0xF0000000) >> 28]; o += 6; VAR_9 += 2; } s->framing_index++; if (s->framing_index >= 192)
s->framing_index = 0; } } else if (VAR_0->bits_per_raw_sample == 16) { const uint16_t *VAR_9 = (uint16_t *)VAR_2->data[0]; for (VAR_6 = 0; VAR_6 < VAR_2->nb_samples; VAR_6++) { uint8_t vucf = s->framing_index == 0 ? 0x10 : 0; for (VAR_7 = 0; VAR_7 < VAR_0->VAR_7; VAR_7 += 2) { o[0] = ff_reverse[ VAR_9[0] & 0xFF]; o[1] = ff_reverse[(VAR_9[0] & 0xFF00) >>  8]; o[2] = ff_reverse[(VAR_9[1] & 0x0F)   <<  4] | vucf; o[3] = ff_reverse[(VAR_9[1] & 0x0FF0) >>  4]; o[4] = ff_reverse[(VAR_9[1] & 0xF000) >> 12]; o += 5; VAR_9 += 2; } s->framing_index++; if (s->framing_index >= 192)
s->framing_index = 0; } } *VAR_3 = 1; return 0; }