static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
{
    unsigned int crc;
    unsigned char packet[TS_PACKET_SIZE];
    const unsigned char *buf_ptr;
    unsigned char *q;
    int first, b, len1, left;
    crc = bswap_32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, buf, len - 4));
    buf[len - 4] = (crc >> 24) & 0xff;
    buf[len - 3] = (crc >> 16) & 0xff;
    buf[len - 2] = (crc >> 8) & 0xff;
    buf[len - 1] = (crc) & 0xff;
    buf_ptr = buf;
    while (len > 0) {
        first = (buf == buf_ptr);
        q = packet;
        *q++ = 0x47;
        b = (s->pid >> 8);
        if (first)
            b |= 0x40;
        *q++ = b;
        *q++ = s->pid;
        s->cc = (s->cc + 1) & 0xf;
        *q++ = 0x10 | s->cc;
        if (first)
            *q++ = 0;
        len1 = TS_PACKET_SIZE - (q - packet);
        if (len1 > len)
            len1 = len;
        memcpy(q, buf_ptr, len1);
        q += len1;
        left = TS_PACKET_SIZE - (q - packet);
        if (left > 0)
            memset(q, 0xff, left);
        s->write_packet(s, packet);
        buf_ptr += len1;
        len -= len1;
    }
}
['static void mpegts_write_pat(AVFormatContext *s)\n{\n    MpegTSWrite *ts = s->priv_data;\n    MpegTSService *service;\n    uint8_t data[1012], *q;\n    int i;\n    q = data;\n    for(i = 0; i < ts->nb_services; i++) {\n        service = ts->services[i];\n        put16(&q, service->sid);\n        put16(&q, 0xe000 | service->pmt.pid);\n    }\n    mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,\n                          data, q - data);\n}', 'static int mpegts_write_section1(MpegTSSection *s, int tid, int id,\n                          int version, int sec_num, int last_sec_num,\n                          uint8_t *buf, int len)\n{\n    uint8_t section[1024], *q;\n    unsigned int tot_len;\n    tot_len = 3 + 5 + len + 4;\n    if (tot_len > 1024)\n        return -1;\n    q = section;\n    *q++ = tid;\n    put16(&q, 0xb000 | (len + 5 + 4));\n    put16(&q, id);\n    *q++ = 0xc1 | (version << 1);\n    *q++ = sec_num;\n    *q++ = last_sec_num;\n    memcpy(q, buf, len);\n    mpegts_write_section(s, section, tot_len);\n    return 0;\n}', 'static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)\n{\n    unsigned int crc;\n    unsigned char packet[TS_PACKET_SIZE];\n    const unsigned char *buf_ptr;\n    unsigned char *q;\n    int first, b, len1, left;\n    crc = bswap_32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, buf, len - 4));\n    buf[len - 4] = (crc >> 24) & 0xff;\n    buf[len - 3] = (crc >> 16) & 0xff;\n    buf[len - 2] = (crc >> 8) & 0xff;\n    buf[len - 1] = (crc) & 0xff;\n    buf_ptr = buf;\n    while (len > 0) {\n        first = (buf == buf_ptr);\n        q = packet;\n        *q++ = 0x47;\n        b = (s->pid >> 8);\n        if (first)\n            b |= 0x40;\n        *q++ = b;\n        *q++ = s->pid;\n        s->cc = (s->cc + 1) & 0xf;\n        *q++ = 0x10 | s->cc;\n        if (first)\n            *q++ = 0;\n        len1 = TS_PACKET_SIZE - (q - packet);\n        if (len1 > len)\n            len1 = len;\n        memcpy(q, buf_ptr, len1);\n        q += len1;\n        left = TS_PACKET_SIZE - (q - packet);\n        if (left > 0)\n            memset(q, 0xff, left);\n        s->write_packet(s, packet);\n        buf_ptr += len1;\n        len -= len1;\n    }\n}']