static int a64_write_packet(struct AVFormatContext *s, AVPacket *pkt)

{

    AVCodecContext *avctx = s->streams[0]->codec;

    A64MuxerContext *c = s->priv_data;

    int i, j;

    int ch_chunksize;

    int lifetime;

    int frame_count;

    int charset_size;

    int frame_size;

    int num_frames;



    /* fetch values from extradata */

    switch (avctx->codec->id) {

    case CODEC_ID_A64_MULTI:

    case CODEC_ID_A64_MULTI5:

        if(c->interleaved) {

            /* Write interleaved, means we insert chunks of the future charset before each current frame.

             * Reason: if we load 1 charset + corresponding frames in one block on c64, we need to store

             * them first and then display frame by frame to keep in sync. Thus we would read and write

             * the data for colram from/to ram first and waste too much time. If we interleave and send the

             * charset beforehand, we assemble a new charset chunk by chunk, write current screen data to

             * screen-ram to be displayed and decode the colram directly to colram-location $d800 during

             * the overscan, while reading directly from source

             * This is the only way so far, to achieve 25fps on c64 */

            if(avctx->extradata) {

                /* fetch values from extradata */

                lifetime     = AV_RB32(avctx->extradata + 0);

                frame_count  = AV_RB32(avctx->extradata + 4);

                charset_size = AV_RB32(avctx->extradata + 8);

                frame_size   = AV_RB32(avctx->extradata + 12);



                /* TODO: sanity checks? */

            }

            else {

                av_log(avctx, AV_LOG_ERROR, "extradata not set\n");

                return AVERROR(EINVAL);

            }

            ch_chunksize=charset_size/lifetime;

            /* TODO: check if charset/size is % lifetime, but maybe check in codec */

            if(pkt->data) num_frames = lifetime;

            else num_frames = c->prev_frame_count;

            for(i = 0; i < num_frames; i++) {

                if(pkt->data) {

                    /* if available, put newest charset chunk into buffer */

                    put_buffer(s->pb, pkt->data + ch_chunksize * i, ch_chunksize);

                }

                else {

                    /* a bit ugly, but is there an alternative to put many zeros? */

                    for(j = 0; j < ch_chunksize; j++) put_byte(s->pb, 0);

                }

                if(c->prev_pkt.data) {

                    /* put frame (screen + colram) from last packet into buffer */

                    put_buffer(s->pb, c->prev_pkt.data + charset_size + frame_size * i, frame_size);

                }

                else {

                    /* a bit ugly, but is there an alternative to put many zeros? */

                    for(j = 0; j < frame_size; j++) put_byte(s->pb, 0);

                }

            }

            /* backup current packet for next turn */

            if(pkt->data) {

                av_new_packet(&c->prev_pkt, pkt->size);

                memcpy(c->prev_pkt.data, pkt->data, pkt->size);

            }

            c->prev_frame_count = frame_count;

            break;

        }

        default:

            /* Write things as is. Nice for self-contained frames from non-multicolor modes or if played

             * directly from ram and not from a streaming device (rrnet/mmc) */

            if(pkt) put_buffer(s->pb, pkt->data, pkt->size);

        break;

    }



    put_flush_packet(s->pb);

    return 0;

}

static int a64_write_packet(struct AVFormatContext *s, AVPacket *pkt)

{

    AVCodecContext *avctx = s->streams[0]->codec;

    A64MuxerContext *c = s->priv_data;

    int i, j;

    int ch_chunksize;

    int lifetime;

    int frame_count;

    int charset_size;

    int frame_size;

    int num_frames;



    

    switch (avctx->codec->id) {

    case CODEC_ID_A64_MULTI:

    case CODEC_ID_A64_MULTI5:

        if(c->interleaved) {

            















            if(avctx->extradata) {

                

                lifetime     = AV_RB32(avctx->extradata + 0);

                frame_count  = AV_RB32(avctx->extradata + 4);

                charset_size = AV_RB32(avctx->extradata + 8);

                frame_size   = AV_RB32(avctx->extradata + 12);



                

            }

            else {

                av_log(avctx, AV_LOG_ERROR, "extradata not set\n");

                return AVERROR(EINVAL);

            }

            ch_chunksize=charset_size/lifetime;

            

            if(pkt->data) num_frames = lifetime;

            else num_frames = c->prev_frame_count;

            for(i = 0; i < num_frames; i++) {

                if(pkt->data) {

                    

                    put_buffer(s->pb, pkt->data + ch_chunksize * i, ch_chunksize);

                }

                else {

                    

                    for(j = 0; j < ch_chunksize; j++) put_byte(s->pb, 0);

                }

                if(c->prev_pkt.data) {

                    

                    put_buffer(s->pb, c->prev_pkt.data + charset_size + frame_size * i, frame_size);

                }

                else {

                    

                    for(j = 0; j < frame_size; j++) put_byte(s->pb, 0);

                }

            }

            

            if(pkt->data) {

                av_new_packet(&c->prev_pkt, pkt->size);

                memcpy(c->prev_pkt.data, pkt->data, pkt->size);

            }

            c->prev_frame_count = frame_count;

            break;

        }

        default:

            



            if(pkt) put_buffer(s->pb, pkt->data, pkt->size);

        break;

    }



    put_flush_packet(s->pb);

    return 0;

}

                av_new_packet(&c->prev_pkt, pkt->size);                 memcpy(c->prev_pkt.data, pkt->data, pkt->size);
static int FUNC_0(struct AVFormatContext *VAR_0, AVPacket *VAR_1)

{

    AVCodecContext *avctx = VAR_0->streams[0]->codec;

    A64MuxerContext *c = VAR_0->priv_data;

    int VAR_2, VAR_3;

    int VAR_4;

    int VAR_5;

    int VAR_6;

    int VAR_7;

    int VAR_8;

    int VAR_9;



    

    switch (avctx->codec->id) {

    case CODEC_ID_A64_MULTI:

    case CODEC_ID_A64_MULTI5:

        if(c->interleaved) {

            















            if(avctx->extradata) {

                

                VAR_5     = AV_RB32(avctx->extradata + 0);

                VAR_6  = AV_RB32(avctx->extradata + 4);

                VAR_7 = AV_RB32(avctx->extradata + 8);

                VAR_8   = AV_RB32(avctx->extradata + 12);



                

            }

            else {

                av_log(avctx, AV_LOG_ERROR, "extradata not set\n");

                return AVERROR(EINVAL);

            }

            VAR_4=VAR_7/VAR_5;

            

            if(VAR_1->data) VAR_9 = VAR_5;

            else VAR_9 = c->prev_frame_count;

            for(VAR_2 = 0; VAR_2 < VAR_9; VAR_2++) {

                if(VAR_1->data) {

                    

                    put_buffer(VAR_0->pb, VAR_1->data + VAR_4 * VAR_2, VAR_4);

                }

                else {

                    

                    for(VAR_3 = 0; VAR_3 < VAR_4; VAR_3++) put_byte(VAR_0->pb, 0);

                }

                if(c->prev_pkt.data) {

                    

                    put_buffer(VAR_0->pb, c->prev_pkt.data + VAR_7 + VAR_8 * VAR_2, VAR_8);

                }

                else {

                    

                    for(VAR_3 = 0; VAR_3 < VAR_8; VAR_3++) put_byte(VAR_0->pb, 0);

                }

            }

            

            if(VAR_1->data) {

                av_new_packet(&c->prev_pkt, VAR_1->size);

                memcpy(c->prev_pkt.data, VAR_1->data, VAR_1->size);

            }

            c->prev_frame_count = VAR_6;

            break;

        }

        default:

            



            if(VAR_1) put_buffer(VAR_0->pb, VAR_1->data, VAR_1->size);

        break;

    }



    put_flush_packet(VAR_0->pb);

    return 0;

}

static int FUNC_0(struct AVFormatContext *VAR_0, AVPacket *VAR_1)
{ AVCodecContext *avctx = VAR_0->streams[0]->codec; A64MuxerContext *c = VAR_0->priv_data; int VAR_2, VAR_3; int VAR_4; int VAR_5; int VAR_6; int VAR_7; int VAR_8; int VAR_9; switch (avctx->codec->id) { case CODEC_ID_A64_MULTI:
case CODEC_ID_A64_MULTI5:
if(c->interleaved) { if(avctx->extradata) { VAR_5     = AV_RB32(avctx->extradata + 0); VAR_6  = AV_RB32(avctx->extradata + 4); VAR_7 = AV_RB32(avctx->extradata + 8); VAR_8   = AV_RB32(avctx->extradata + 12); } else { av_log(avctx, AV_LOG_ERROR, "extradata not set\n"); return AVERROR(EINVAL); } VAR_4=VAR_7/VAR_5; if(VAR_1->data) VAR_9 = VAR_5; else VAR_9 = c->prev_frame_count; for(VAR_2 = 0; VAR_2 < VAR_9; VAR_2++) { if(VAR_1->data) { put_buffer(VAR_0->pb, VAR_1->data + VAR_4 * VAR_2, VAR_4); } else { for(VAR_3 = 0; VAR_3 < VAR_4; VAR_3++) put_byte(VAR_0->pb, 0); } if(c->prev_pkt.data) { put_buffer(VAR_0->pb, c->prev_pkt.data + VAR_7 + VAR_8 * VAR_2, VAR_8); } else { for(VAR_3 = 0; VAR_3 < VAR_8; VAR_3++) put_byte(VAR_0->pb, 0); } } if(VAR_1->data) { av_new_packet(&c->prev_pkt, VAR_1->size); memcpy(c->prev_pkt.data, VAR_1->data, VAR_1->size); } c->prev_frame_count = VAR_6; break; } default:
if(VAR_1) put_buffer(VAR_0->pb, VAR_1->data, VAR_1->size); break; } put_flush_packet(VAR_0->pb); return 0; }