static int mpegts_write_header(AVFormatContext *s)
{
    MpegTSWrite *ts = s->priv_data;
    MpegTSWriteStream *ts_st;
    MpegTSService *service;
    AVStream *st;
    int i, total_bit_rate;
    const char *service_name;
    ts->tsid = DEFAULT_TSID;
    ts->onid = DEFAULT_ONID;
    service_name = s->title;
    if (service_name[0] == '\0')
        service_name = DEFAULT_SERVICE_NAME;
    service = mpegts_add_service(ts, DEFAULT_SID,
                                 DEFAULT_PROVIDER_NAME, service_name);
    service->pmt.write_packet = section_write_packet;
    service->pmt.opaque = s;
    ts->pat.pid = PAT_PID;
    ts->pat.cc = 0;
    ts->pat.write_packet = section_write_packet;
    ts->pat.opaque = s;
    ts->sdt.pid = SDT_PID;
    ts->sdt.cc = 0;
    ts->sdt.write_packet = section_write_packet;
    ts->sdt.opaque = s;
    total_bit_rate = 0;
    for(i = 0;i < s->nb_streams; i++) {
        st = s->streams[i];
        ts_st = av_mallocz(sizeof(MpegTSWriteStream));
        if (!ts_st)
            goto fail;
        st->priv_data = ts_st;
        ts_st->service = service;
        ts_st->pid = DEFAULT_START_PID + i;
        ts_st->payload_pts = AV_NOPTS_VALUE;
        ts_st->payload_dts = AV_NOPTS_VALUE;
        if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
            service->pcr_pid == 0x1fff)
            service->pcr_pid = ts_st->pid;
        total_bit_rate += st->codec->bit_rate;
    }
    if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
        ts_st = s->streams[0]->priv_data;
        service->pcr_pid = ts_st->pid;
    }
    if (total_bit_rate <= 8 * 1024)
        total_bit_rate = 8 * 1024;
    service->pcr_packet_freq = (total_bit_rate * PCR_RETRANS_TIME) /
        (TS_PACKET_SIZE * 8 * 1000);
    ts->sdt_packet_freq = (total_bit_rate * SDT_RETRANS_TIME) /
        (TS_PACKET_SIZE * 8 * 1000);
    ts->pat_packet_freq = (total_bit_rate * PAT_RETRANS_TIME) /
        (TS_PACKET_SIZE * 8 * 1000);
#if 0
    printf("%d %d %d\n",
           total_bit_rate, ts->sdt_packet_freq, ts->pat_packet_freq);
#endif
    mpegts_write_sdt(s);
    mpegts_write_pat(s);
    for(i = 0; i < ts->nb_services; i++) {
        mpegts_write_pmt(s, ts->services[i]);
    }
    put_flush_packet(s->pb);
    return 0;
 fail:
    for(i = 0;i < s->nb_streams; i++) {
        st = s->streams[i];
        av_free(st->priv_data);
    }
    return -1;
}
['static int mpegts_write_header(AVFormatContext *s)\n{\n    MpegTSWrite *ts = s->priv_data;\n    MpegTSWriteStream *ts_st;\n    MpegTSService *service;\n    AVStream *st;\n    int i, total_bit_rate;\n    const char *service_name;\n    ts->tsid = DEFAULT_TSID;\n    ts->onid = DEFAULT_ONID;\n    service_name = s->title;\n    if (service_name[0] == \'\\0\')\n        service_name = DEFAULT_SERVICE_NAME;\n    service = mpegts_add_service(ts, DEFAULT_SID,\n                                 DEFAULT_PROVIDER_NAME, service_name);\n    service->pmt.write_packet = section_write_packet;\n    service->pmt.opaque = s;\n    ts->pat.pid = PAT_PID;\n    ts->pat.cc = 0;\n    ts->pat.write_packet = section_write_packet;\n    ts->pat.opaque = s;\n    ts->sdt.pid = SDT_PID;\n    ts->sdt.cc = 0;\n    ts->sdt.write_packet = section_write_packet;\n    ts->sdt.opaque = s;\n    total_bit_rate = 0;\n    for(i = 0;i < s->nb_streams; i++) {\n        st = s->streams[i];\n        ts_st = av_mallocz(sizeof(MpegTSWriteStream));\n        if (!ts_st)\n            goto fail;\n        st->priv_data = ts_st;\n        ts_st->service = service;\n        ts_st->pid = DEFAULT_START_PID + i;\n        ts_st->payload_pts = AV_NOPTS_VALUE;\n        ts_st->payload_dts = AV_NOPTS_VALUE;\n        if (st->codec->codec_type == CODEC_TYPE_VIDEO &&\n            service->pcr_pid == 0x1fff)\n            service->pcr_pid = ts_st->pid;\n        total_bit_rate += st->codec->bit_rate;\n    }\n    if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {\n        ts_st = s->streams[0]->priv_data;\n        service->pcr_pid = ts_st->pid;\n    }\n    if (total_bit_rate <= 8 * 1024)\n        total_bit_rate = 8 * 1024;\n    service->pcr_packet_freq = (total_bit_rate * PCR_RETRANS_TIME) /\n        (TS_PACKET_SIZE * 8 * 1000);\n    ts->sdt_packet_freq = (total_bit_rate * SDT_RETRANS_TIME) /\n        (TS_PACKET_SIZE * 8 * 1000);\n    ts->pat_packet_freq = (total_bit_rate * PAT_RETRANS_TIME) /\n        (TS_PACKET_SIZE * 8 * 1000);\n#if 0\n    printf("%d %d %d\\n",\n           total_bit_rate, ts->sdt_packet_freq, ts->pat_packet_freq);\n#endif\n    mpegts_write_sdt(s);\n    mpegts_write_pat(s);\n    for(i = 0; i < ts->nb_services; i++) {\n        mpegts_write_pmt(s, ts->services[i]);\n    }\n    put_flush_packet(s->pb);\n    return 0;\n fail:\n    for(i = 0;i < s->nb_streams; i++) {\n        st = s->streams[i];\n        av_free(st->priv_data);\n    }\n    return -1;\n}']