static void flush_packet(AVFormatContext *ctx, int stream_index, int last_pkt)

{

    MpegMuxContext *s = ctx->priv_data;

    StreamInfo *stream = ctx->streams[stream_index]->priv_data;

    uint8_t *buf_ptr;

    int size, payload_size, startcode, id, len, stuffing_size, i, header_len;

    int64_t timestamp;

    uint8_t buffer[128];

    int last = last_pkt ? 4 : 0;

    

    id = stream->id;

    timestamp = stream->start_pts;



#if 0

    printf("packet ID=%2x PTS=%0.3f\n", 

           id, timestamp / 90000.0);

#endif



    buf_ptr = buffer;

    if (((s->packet_number % s->pack_header_freq) == 0)) {

        /* output pack and systems header if needed */

        size = put_pack_header(ctx, buf_ptr, timestamp);

        buf_ptr += size;

        if ((s->packet_number % s->system_header_freq) == 0) {

            size = put_system_header(ctx, buf_ptr);

            buf_ptr += size;

        }

    }

    size = buf_ptr - buffer;

    put_buffer(&ctx->pb, buffer, size);



    /* packet header */

    if (s->is_mpeg2) {

        header_len = 8;

    } else {

        header_len = 5;

    }

    payload_size = s->packet_size - (size + 6 + header_len + last);

    if (id < 0xc0) {

        startcode = PRIVATE_STREAM_1;

        payload_size -= 4;

    } else {

        startcode = 0x100 + id;

    }

    stuffing_size = payload_size - stream->buffer_ptr;

    if (stuffing_size < 0)

        stuffing_size = 0;



    put_be32(&ctx->pb, startcode);



    put_be16(&ctx->pb, payload_size + header_len);

    /* stuffing */

    for(i=0;i<stuffing_size;i++)

        put_byte(&ctx->pb, 0xff);



    if (s->is_mpeg2) {

        put_byte(&ctx->pb, 0x80); /* mpeg2 id */

        put_byte(&ctx->pb, 0x80); /* flags */

        put_byte(&ctx->pb, 0x05); /* header len (only pts is included) */

    }

    put_byte(&ctx->pb, 

             (0x02 << 4) | 

             (((timestamp >> 30) & 0x07) << 1) | 

             1);

    put_be16(&ctx->pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));

    put_be16(&ctx->pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));



    if (startcode == PRIVATE_STREAM_1) {

        put_byte(&ctx->pb, id);

        if (id >= 0x80 && id <= 0xbf) {

            /* XXX: need to check AC3 spec */

            put_byte(&ctx->pb, 1);

            put_byte(&ctx->pb, 0);

            put_byte(&ctx->pb, 2);

        }

    }



    if (last_pkt) {

        put_be32(&ctx->pb, ISO_11172_END_CODE);

    }

    /* output data */

    put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);

    put_flush_packet(&ctx->pb);

    

    /* preserve remaining data */

    len = stream->buffer_ptr - payload_size;

    if (len < 0) 

        len = 0;

    memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);

    stream->buffer_ptr = len;



    s->packet_number++;

    stream->packet_number++;

    stream->start_pts = -1;

}

static void flush_packet(AVFormatContext *ctx, int stream_index, int last_pkt)

{

    MpegMuxContext *s = ctx->priv_data;

    StreamInfo *stream = ctx->streams[stream_index]->priv_data;

    uint8_t *buf_ptr;

    int size, payload_size, startcode, id, len, stuffing_size, i, header_len;

    int64_t timestamp;

    uint8_t buffer[128];

    int last = last_pkt ? 4 : 0;

    

    id = stream->id;

    timestamp = stream->start_pts;



#if 0

    printf("packet ID=%2x PTS=%0.3f\n", 

           id, timestamp / 90000.0);

#endif



    buf_ptr = buffer;

    if (((s->packet_number % s->pack_header_freq) == 0)) {

        

        size = put_pack_header(ctx, buf_ptr, timestamp);

        buf_ptr += size;

        if ((s->packet_number % s->system_header_freq) == 0) {

            size = put_system_header(ctx, buf_ptr);

            buf_ptr += size;

        }

    }

    size = buf_ptr - buffer;

    put_buffer(&ctx->pb, buffer, size);



    

    if (s->is_mpeg2) {

        header_len = 8;

    } else {

        header_len = 5;

    }

    payload_size = s->packet_size - (size + 6 + header_len + last);

    if (id < 0xc0) {

        startcode = PRIVATE_STREAM_1;

        payload_size -= 4;

    } else {

        startcode = 0x100 + id;

    }

    stuffing_size = payload_size - stream->buffer_ptr;

    if (stuffing_size < 0)

        stuffing_size = 0;



    put_be32(&ctx->pb, startcode);



    put_be16(&ctx->pb, payload_size + header_len);

    

    for(i=0;i<stuffing_size;i++)

        put_byte(&ctx->pb, 0xff);



    if (s->is_mpeg2) {

        put_byte(&ctx->pb, 0x80); 

        put_byte(&ctx->pb, 0x80); 

        put_byte(&ctx->pb, 0x05); 

    }

    put_byte(&ctx->pb, 

             (0x02 << 4) | 

             (((timestamp >> 30) & 0x07) << 1) | 

             1);

    put_be16(&ctx->pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));

    put_be16(&ctx->pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));



    if (startcode == PRIVATE_STREAM_1) {

        put_byte(&ctx->pb, id);

        if (id >= 0x80 && id <= 0xbf) {

            

            put_byte(&ctx->pb, 1);

            put_byte(&ctx->pb, 0);

            put_byte(&ctx->pb, 2);

        }

    }



    if (last_pkt) {

        put_be32(&ctx->pb, ISO_11172_END_CODE);

    }

    

    put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);

    put_flush_packet(&ctx->pb);

    

    

    len = stream->buffer_ptr - payload_size;

    if (len < 0) 

        len = 0;

    memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);

    stream->buffer_ptr = len;



    s->packet_number++;

    stream->packet_number++;

    stream->start_pts = -1;

}

static void FUNC_0(AVFormatContext *VAR_0, int VAR_1, int VAR_2)

{

    MpegMuxContext *s = VAR_0->priv_data;

    StreamInfo *stream = VAR_0->streams[VAR_1]->priv_data;

    uint8_t *buf_ptr;

    int VAR_3, VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9, VAR_10;

    int64_t timestamp;

    uint8_t buffer[128];

    int VAR_11 = VAR_2 ? 4 : 0;

    

    VAR_6 = stream->VAR_6;

    timestamp = stream->start_pts;



#if 0

    printf("packet ID=%2x PTS=%0.3f\n", 

           VAR_6, timestamp / 90000.0);

#endif



    buf_ptr = buffer;

    if (((s->packet_number % s->pack_header_freq) == 0)) {

        

        VAR_3 = put_pack_header(VAR_0, buf_ptr, timestamp);

        buf_ptr += VAR_3;

        if ((s->packet_number % s->system_header_freq) == 0) {

            VAR_3 = put_system_header(VAR_0, buf_ptr);

            buf_ptr += VAR_3;

        }

    }

    VAR_3 = buf_ptr - buffer;

    put_buffer(&VAR_0->pb, buffer, VAR_3);



    

    if (s->is_mpeg2) {

        VAR_10 = 8;

    } else {

        VAR_10 = 5;

    }

    VAR_4 = s->packet_size - (VAR_3 + 6 + VAR_10 + VAR_11);

    if (VAR_6 < 0xc0) {

        VAR_5 = PRIVATE_STREAM_1;

        VAR_4 -= 4;

    } else {

        VAR_5 = 0x100 + VAR_6;

    }

    VAR_8 = VAR_4 - stream->buffer_ptr;

    if (VAR_8 < 0)

        VAR_8 = 0;



    put_be32(&VAR_0->pb, VAR_5);



    put_be16(&VAR_0->pb, VAR_4 + VAR_10);

    

    for(VAR_9=0;VAR_9<VAR_8;VAR_9++)

        put_byte(&VAR_0->pb, 0xff);



    if (s->is_mpeg2) {

        put_byte(&VAR_0->pb, 0x80); 

        put_byte(&VAR_0->pb, 0x80); 

        put_byte(&VAR_0->pb, 0x05); 

    }

    put_byte(&VAR_0->pb, 

             (0x02 << 4) | 

             (((timestamp >> 30) & 0x07) << 1) | 

             1);

    put_be16(&VAR_0->pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));

    put_be16(&VAR_0->pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));



    if (VAR_5 == PRIVATE_STREAM_1) {

        put_byte(&VAR_0->pb, VAR_6);

        if (VAR_6 >= 0x80 && VAR_6 <= 0xbf) {

            

            put_byte(&VAR_0->pb, 1);

            put_byte(&VAR_0->pb, 0);

            put_byte(&VAR_0->pb, 2);

        }

    }



    if (VAR_2) {

        put_be32(&VAR_0->pb, ISO_11172_END_CODE);

    }

    

    put_buffer(&VAR_0->pb, stream->buffer, VAR_4 - VAR_8);

    put_flush_packet(&VAR_0->pb);

    

    

    VAR_7 = stream->buffer_ptr - VAR_4;

    if (VAR_7 < 0) 

        VAR_7 = 0;

    memmove(stream->buffer, stream->buffer + stream->buffer_ptr - VAR_7, VAR_7);

    stream->buffer_ptr = VAR_7;



    s->packet_number++;

    stream->packet_number++;

    stream->start_pts = -1;

}

static void FUNC_0(AVFormatContext *VAR_0, int VAR_1, int VAR_2)
{ MpegMuxContext *s = VAR_0->priv_data; StreamInfo *stream = VAR_0->streams[VAR_1]->priv_data; uint8_t *buf_ptr; int VAR_3, VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9, VAR_10; int64_t timestamp; uint8_t buffer[128]; int VAR_11 = VAR_2 ? 4 : 0; VAR_6 = stream->VAR_6; timestamp = stream->start_pts; #if 0
printf("packet ID=%2x PTS=%0.3f\n",
VAR_6, timestamp / 90000.0); #endif
buf_ptr = buffer; if (((s->packet_number % s->pack_header_freq) == 0)) { VAR_3 = put_pack_header(VAR_0, buf_ptr, timestamp); buf_ptr += VAR_3; if ((s->packet_number % s->system_header_freq) == 0) { VAR_3 = put_system_header(VAR_0, buf_ptr); buf_ptr += VAR_3; } } VAR_3 = buf_ptr - buffer; put_buffer(&VAR_0->pb, buffer, VAR_3); if (s->is_mpeg2) { VAR_10 = 8; } else { VAR_10 = 5; } VAR_4 = s->packet_size - (VAR_3 + 6 + VAR_10 + VAR_11); if (VAR_6 < 0xc0) { VAR_5 = PRIVATE_STREAM_1; VAR_4 -= 4; } else { VAR_5 = 0x100 + VAR_6; } VAR_8 = VAR_4 - stream->buffer_ptr; if (VAR_8 < 0)
VAR_8 = 0; put_be32(&VAR_0->pb, VAR_5); put_be16(&VAR_0->pb, VAR_4 + VAR_10); for(VAR_9=0;VAR_9<VAR_8;VAR_9++) put_byte(&VAR_0->pb, 0xff); if (s->is_mpeg2) { put_byte(&VAR_0->pb, 0x80); put_byte(&VAR_0->pb, 0x80); put_byte(&VAR_0->pb, 0x05); } put_byte(&VAR_0->pb,
(0x02 << 4) |
(((timestamp >> 30) & 0x07) << 1) |
1); put_be16(&VAR_0->pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1)); put_be16(&VAR_0->pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1)); if (VAR_5 == PRIVATE_STREAM_1) { put_byte(&VAR_0->pb, VAR_6); if (VAR_6 >= 0x80 && VAR_6 <= 0xbf) { put_byte(&VAR_0->pb, 1); put_byte(&VAR_0->pb, 0); put_byte(&VAR_0->pb, 2); } } if (VAR_2) { put_be32(&VAR_0->pb, ISO_11172_END_CODE); } put_buffer(&VAR_0->pb, stream->buffer, VAR_4 - VAR_8); put_flush_packet(&VAR_0->pb); VAR_7 = stream->buffer_ptr - VAR_4; if (VAR_7 < 0)
VAR_7 = 0; memmove(stream->buffer, stream->buffer + stream->buffer_ptr - VAR_7, VAR_7); stream->buffer_ptr = VAR_7; s->packet_number++; stream->packet_number++; stream->start_pts = -1; }