static void flush_packet(AVFormatContext *ctx, int stream_index, 

                         int64_t pts, int64_t dts, int64_t scr)

{

    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;

    uint8_t buffer[128];

    

    id = stream->id;

    

#if 0

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

           id, pts / 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, scr);

        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 = 3;

    } else {

        header_len = 0;

    }

    if (pts != AV_NOPTS_VALUE) {

        if (dts != AV_NOPTS_VALUE)

            header_len += 5 + 5;

        else

            header_len += 5;

    } else {

        if (!s->is_mpeg2)

            header_len++;

    }



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

    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 */



        if (pts != AV_NOPTS_VALUE) {

            if (dts != AV_NOPTS_VALUE) {

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

                put_byte(&ctx->pb, header_len - 3);

                put_timestamp(&ctx->pb, 0x03, pts);

                put_timestamp(&ctx->pb, 0x01, dts);

            } else {

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

                put_byte(&ctx->pb, header_len - 3);

                put_timestamp(&ctx->pb, 0x02, pts);

            }

        } else {

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

            put_byte(&ctx->pb, header_len - 3);

        }

    } else {

        if (pts != AV_NOPTS_VALUE) {

            if (dts != AV_NOPTS_VALUE) {

                put_timestamp(&ctx->pb, 0x03, pts);

                put_timestamp(&ctx->pb, 0x01, dts);

            } else {

                put_timestamp(&ctx->pb, 0x02, pts);

            }

        } else {

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

        }

    }



    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);

        }

    }



    /* 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++;

}

static void flush_packet(AVFormatContext *ctx, int stream_index, 

                         int64_t pts, int64_t dts, int64_t scr)

{

    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;

    uint8_t buffer[128];

    

    id = stream->id;

    

#if 0

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

           id, pts / 90000.0);

#endif



    buf_ptr = buffer;

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

        

        size = put_pack_header(ctx, buf_ptr, scr);

        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 = 3;

    } else {

        header_len = 0;

    }

    if (pts != AV_NOPTS_VALUE) {

        if (dts != AV_NOPTS_VALUE)

            header_len += 5 + 5;

        else

            header_len += 5;

    } else {

        if (!s->is_mpeg2)

            header_len++;

    }



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

    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); 



        if (pts != AV_NOPTS_VALUE) {

            if (dts != AV_NOPTS_VALUE) {

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

                put_byte(&ctx->pb, header_len - 3);

                put_timestamp(&ctx->pb, 0x03, pts);

                put_timestamp(&ctx->pb, 0x01, dts);

            } else {

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

                put_byte(&ctx->pb, header_len - 3);

                put_timestamp(&ctx->pb, 0x02, pts);

            }

        } else {

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

            put_byte(&ctx->pb, header_len - 3);

        }

    } else {

        if (pts != AV_NOPTS_VALUE) {

            if (dts != AV_NOPTS_VALUE) {

                put_timestamp(&ctx->pb, 0x03, pts);

                put_timestamp(&ctx->pb, 0x01, dts);

            } else {

                put_timestamp(&ctx->pb, 0x02, pts);

            }

        } else {

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

        }

    }



    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);

        }

    }



    

    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++;

}

    int size, payload_size, startcode, id, len, stuffing_size, i, header_len;     payload_size = s->packet_size - (size + 6 + header_len);     put_be16(&ctx->pb, payload_size + header_len);             put_byte(&ctx->pb, 1);             put_byte(&ctx->pb, 0);             put_byte(&ctx->pb, 2);     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;
static void FUNC_0(AVFormatContext *VAR_0, int VAR_1, 

                         int64_t VAR_2, int64_t VAR_3, int64_t VAR_4)

{

    MpegMuxContext *s = VAR_0->priv_data;

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

    uint8_t *buf_ptr;

    int VAR_5, VAR_6, VAR_7, VAR_8, VAR_9, VAR_10, VAR_11, VAR_12;

    uint8_t buffer[128];

    

    VAR_8 = stream->VAR_8;

    

#if 0

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

           VAR_8, VAR_2 / 90000.0);

#endif



    buf_ptr = buffer;

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

        

        VAR_5 = put_pack_header(VAR_0, buf_ptr, VAR_4);

        buf_ptr += VAR_5;

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

            VAR_5 = put_system_header(VAR_0, buf_ptr);

            buf_ptr += VAR_5;

        }

    }

    VAR_5 = buf_ptr - buffer;

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



    

    if (s->is_mpeg2) {

        VAR_12 = 3;

    } else {

        VAR_12 = 0;

    }

    if (VAR_2 != AV_NOPTS_VALUE) {

        if (VAR_3 != AV_NOPTS_VALUE)

            VAR_12 += 5 + 5;

        else

            VAR_12 += 5;

    } else {

        if (!s->is_mpeg2)

            VAR_12++;

    }



    VAR_6 = s->packet_size - (VAR_5 + 6 + VAR_12);

    if (VAR_8 < 0xc0) {

        VAR_7 = PRIVATE_STREAM_1;

        VAR_6 -= 4;

    } else {

        VAR_7 = 0x100 + VAR_8;

    }

    VAR_10 = VAR_6 - stream->buffer_ptr;

    if (VAR_10 < 0)

        VAR_10 = 0;



    put_be32(&VAR_0->pb, VAR_7);



    put_be16(&VAR_0->pb, VAR_6 + VAR_12);

    

    for(VAR_11=0;VAR_11<VAR_10;VAR_11++)

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



    if (s->is_mpeg2) {

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



        if (VAR_2 != AV_NOPTS_VALUE) {

            if (VAR_3 != AV_NOPTS_VALUE) {

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

                put_byte(&VAR_0->pb, VAR_12 - 3);

                put_timestamp(&VAR_0->pb, 0x03, VAR_2);

                put_timestamp(&VAR_0->pb, 0x01, VAR_3);

            } else {

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

                put_byte(&VAR_0->pb, VAR_12 - 3);

                put_timestamp(&VAR_0->pb, 0x02, VAR_2);

            }

        } else {

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

            put_byte(&VAR_0->pb, VAR_12 - 3);

        }

    } else {

        if (VAR_2 != AV_NOPTS_VALUE) {

            if (VAR_3 != AV_NOPTS_VALUE) {

                put_timestamp(&VAR_0->pb, 0x03, VAR_2);

                put_timestamp(&VAR_0->pb, 0x01, VAR_3);

            } else {

                put_timestamp(&VAR_0->pb, 0x02, VAR_2);

            }

        } else {

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

        }

    }



    if (VAR_7 == PRIVATE_STREAM_1) {

        put_byte(&VAR_0->pb, VAR_8);

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

            

            put_byte(&VAR_0->pb, 1);

            put_byte(&VAR_0->pb, 0);

            put_byte(&VAR_0->pb, 2);

        }

    }



    

    put_buffer(&VAR_0->pb, stream->buffer, VAR_6 - VAR_10);

    put_flush_packet(&VAR_0->pb);

    

    

    VAR_9 = stream->buffer_ptr - VAR_6;

    if (VAR_9 < 0) 

        VAR_9 = 0;

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

    stream->buffer_ptr = VAR_9;



    s->packet_number++;

    stream->packet_number++;

}

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