static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
                             const uint8_t *payload, int payload_size,
                             int64_t pts, int64_t dts)
{
    MpegTSWriteStream *ts_st = st->priv_data;
    uint8_t buf[TS_PACKET_SIZE];
    uint8_t *q;
    int val, is_start, len, header_len, write_pcr, private_code, flags;
    int afc_len, stuffing_len;
    int64_t pcr = -1;
    is_start = 1;
    while (payload_size > 0) {
        retransmit_si_info(s);
        write_pcr = 0;
        if (ts_st->pid == ts_st->service->pcr_pid) {
            ts_st->service->pcr_packet_count++;
            if (ts_st->service->pcr_packet_count >=
                ts_st->service->pcr_packet_freq) {
                ts_st->service->pcr_packet_count = 0;
                write_pcr = 1;
                pcr = pts;
            }
        }
        q = buf;
        *q++ = 0x47;
        val = (ts_st->pid >> 8);
        if (is_start)
            val |= 0x40;
        *q++ = val;
        *q++ = ts_st->pid;
        *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
        ts_st->cc = (ts_st->cc + 1) & 0xf;
        if (write_pcr) {
            *q++ = 7;
            *q++ = 0x10;
            *q++ = pcr >> 25;
            *q++ = pcr >> 17;
            *q++ = pcr >> 9;
            *q++ = pcr >> 1;
            *q++ = (pcr & 1) << 7;
            *q++ = 0;
        }
        if (is_start) {
            *q++ = 0x00;
            *q++ = 0x00;
            *q++ = 0x01;
            private_code = 0;
            if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
                *q++ = 0xe0;
            } else if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
                       (st->codec->codec_id == CODEC_ID_MP2 ||
                        st->codec->codec_id == CODEC_ID_MP3)) {
                *q++ = 0xc0;
            } else {
                *q++ = 0xbd;
                if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
                    private_code = 0x20;
                }
            }
            header_len = 0;
            flags = 0;
            if (pts != AV_NOPTS_VALUE) {
                header_len += 5;
                flags |= 0x80;
            }
            if (dts != AV_NOPTS_VALUE) {
                header_len += 5;
                flags |= 0x40;
            }
            len = payload_size + header_len + 3;
            if (private_code != 0)
                len++;
            *q++ = len >> 8;
            *q++ = len;
            val = 0x80;
            if (st->codec->codec_type == CODEC_TYPE_SUBTITLE)
                val |= 0x04;
            *q++ = val;
            *q++ = flags;
            *q++ = header_len;
            if (pts != AV_NOPTS_VALUE) {
                write_pts(q, flags >> 6, pts);
                q += 5;
            }
            if (dts != AV_NOPTS_VALUE) {
                write_pts(q, 1, dts);
                q += 5;
            }
            if (private_code != 0)
                *q++ = private_code;
            is_start = 0;
        }
        header_len = q - buf;
        len = TS_PACKET_SIZE - header_len;
        if (len > payload_size)
            len = payload_size;
        stuffing_len = TS_PACKET_SIZE - header_len - len;
        if (stuffing_len > 0) {
            if (buf[3] & 0x20) {
                afc_len = buf[4] + 1;
                memmove(buf + 4 + afc_len + stuffing_len,
                        buf + 4 + afc_len,
                        header_len - (4 + afc_len));
                buf[4] += stuffing_len;
                memset(buf + 4 + afc_len, 0xff, stuffing_len);
            } else {
                memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
                buf[3] |= 0x20;
                buf[4] = stuffing_len - 1;
                if (stuffing_len >= 2) {
                    buf[5] = 0x00;
                    memset(buf + 6, 0xff, stuffing_len - 2);
                }
            }
        }
        memcpy(buf + TS_PACKET_SIZE - len, payload, len);
        payload += len;
        payload_size -= len;
        put_buffer(s->pb, buf, TS_PACKET_SIZE);
    }
    put_flush_packet(s->pb);
}
['static void mpegts_write_pes(AVFormatContext *s, AVStream *st,\n                             const uint8_t *payload, int payload_size,\n                             int64_t pts, int64_t dts)\n{\n    MpegTSWriteStream *ts_st = st->priv_data;\n    uint8_t buf[TS_PACKET_SIZE];\n    uint8_t *q;\n    int val, is_start, len, header_len, write_pcr, private_code, flags;\n    int afc_len, stuffing_len;\n    int64_t pcr = -1;\n    is_start = 1;\n    while (payload_size > 0) {\n        retransmit_si_info(s);\n        write_pcr = 0;\n        if (ts_st->pid == ts_st->service->pcr_pid) {\n            ts_st->service->pcr_packet_count++;\n            if (ts_st->service->pcr_packet_count >=\n                ts_st->service->pcr_packet_freq) {\n                ts_st->service->pcr_packet_count = 0;\n                write_pcr = 1;\n                pcr = pts;\n            }\n        }\n        q = buf;\n        *q++ = 0x47;\n        val = (ts_st->pid >> 8);\n        if (is_start)\n            val |= 0x40;\n        *q++ = val;\n        *q++ = ts_st->pid;\n        *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);\n        ts_st->cc = (ts_st->cc + 1) & 0xf;\n        if (write_pcr) {\n            *q++ = 7;\n            *q++ = 0x10;\n            *q++ = pcr >> 25;\n            *q++ = pcr >> 17;\n            *q++ = pcr >> 9;\n            *q++ = pcr >> 1;\n            *q++ = (pcr & 1) << 7;\n            *q++ = 0;\n        }\n        if (is_start) {\n            *q++ = 0x00;\n            *q++ = 0x00;\n            *q++ = 0x01;\n            private_code = 0;\n            if (st->codec->codec_type == CODEC_TYPE_VIDEO) {\n                *q++ = 0xe0;\n            } else if (st->codec->codec_type == CODEC_TYPE_AUDIO &&\n                       (st->codec->codec_id == CODEC_ID_MP2 ||\n                        st->codec->codec_id == CODEC_ID_MP3)) {\n                *q++ = 0xc0;\n            } else {\n                *q++ = 0xbd;\n                if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {\n                    private_code = 0x20;\n                }\n            }\n            header_len = 0;\n            flags = 0;\n            if (pts != AV_NOPTS_VALUE) {\n                header_len += 5;\n                flags |= 0x80;\n            }\n            if (dts != AV_NOPTS_VALUE) {\n                header_len += 5;\n                flags |= 0x40;\n            }\n            len = payload_size + header_len + 3;\n            if (private_code != 0)\n                len++;\n            *q++ = len >> 8;\n            *q++ = len;\n            val = 0x80;\n            if (st->codec->codec_type == CODEC_TYPE_SUBTITLE)\n                val |= 0x04;\n            *q++ = val;\n            *q++ = flags;\n            *q++ = header_len;\n            if (pts != AV_NOPTS_VALUE) {\n                write_pts(q, flags >> 6, pts);\n                q += 5;\n            }\n            if (dts != AV_NOPTS_VALUE) {\n                write_pts(q, 1, dts);\n                q += 5;\n            }\n            if (private_code != 0)\n                *q++ = private_code;\n            is_start = 0;\n        }\n        header_len = q - buf;\n        len = TS_PACKET_SIZE - header_len;\n        if (len > payload_size)\n            len = payload_size;\n        stuffing_len = TS_PACKET_SIZE - header_len - len;\n        if (stuffing_len > 0) {\n            if (buf[3] & 0x20) {\n                afc_len = buf[4] + 1;\n                memmove(buf + 4 + afc_len + stuffing_len,\n                        buf + 4 + afc_len,\n                        header_len - (4 + afc_len));\n                buf[4] += stuffing_len;\n                memset(buf + 4 + afc_len, 0xff, stuffing_len);\n            } else {\n                memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);\n                buf[3] |= 0x20;\n                buf[4] = stuffing_len - 1;\n                if (stuffing_len >= 2) {\n                    buf[5] = 0x00;\n                    memset(buf + 6, 0xff, stuffing_len - 2);\n                }\n            }\n        }\n        memcpy(buf + TS_PACKET_SIZE - len, payload, len);\n        payload += len;\n        payload_size -= len;\n        put_buffer(s->pb, buf, TS_PACKET_SIZE);\n    }\n    put_flush_packet(s->pb);\n}']