put_buffer(&s->pb, payload, len);
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);
static void mpegts_write_pes(AVFormatContext *s, AVStream *st,                             const uint8_t *payload, int payload_size,                             int64_t pts){    MpegTSWriteStream *ts_st = st->priv_data;    uint8_t buf[TS_PACKET_SIZE];    uint8_t *q;    int val, is_start, len, ts_len, header_len;    is_start = 1;    while (payload_size > 0) {        retransmit_si_info(s);        /* prepare packet header */        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;        ts_st->cc = (ts_st->cc + 1) & 0xf;        if (is_start) {            /* write PES header */            *q++ = 0x00;            *q++ = 0x00;            *q++ = 0x01;            if (st->codec.codec_type == CODEC_TYPE_VIDEO)                *q++ = 0xe0;            else                *q++ = 0xc0;            if (pts != AV_NOPTS_VALUE)                header_len = 8;            else                header_len = 3;            len = payload_size + header_len;            *q++ = len >> 8;            *q++ = len;            *q++ = 0x80;            if (pts != AV_NOPTS_VALUE) {                *q++ = 0x80; /* PTS only */                *q++ = 0x05; /* header len */                val = (0x02 << 4) |                     (((pts >> 30) & 0x07) << 1) | 1;                *q++ = val;                val = (((pts >> 15) & 0x7fff) << 1) | 1;                *q++ = val >> 8;                *q++ = val;                val = (((pts) & 0x7fff) << 1) | 1;                *q++ = val >> 8;                *q++ = val;            } else {                *q++ = 0x00;                *q++ = 0x00;            }            is_start = 0;        }        /* write header */        ts_len = q - buf;        put_buffer(&s->pb, buf, ts_len);        /* write data */        len = TS_PACKET_SIZE - ts_len;        if (len > payload_size)            len = payload_size;        put_buffer(&s->pb, payload, len);        payload += len;        payload_size -= len;        ts_len += len;        /* stuffing */        len = TS_PACKET_SIZE - ts_len;        if (len > 0) {            memset(buf, 0xff, len);            put_buffer(&s->pb, buf, len);        }    }    put_flush_packet(&s->pb);}