service->pcr_packet_freq = (total_bit_rate * PCR_RETRANS_TIME) / (TS_PACKET_SIZE * 8 * 1000);
static int mpegts_write_header(AVFormatContext *s){    MpegTSWrite *ts = s->priv_data;    MpegTSWriteStream *ts_st;    MpegTSService *service;    AVStream *st;    int i, total_bit_rate;    ts->tsid = DEFAULT_TSID;    ts->onid = DEFAULT_ONID;    /* allocate a single DVB service */    service = mpegts_add_service(ts, DEFAULT_SID,                                  DEFAULT_PROVIDER_NAME, DEFAULT_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;    /* assign pids to each stream */    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->pid = DEFAULT_START_PID + i;        ts_st->payload_pts = AV_NOPTS_VALUE;        /* update PCR pid if needed */        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 (total_bit_rate <= 8 * 1024)        total_bit_rate = 8 * 1024;    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    /* write info at the start of the file, so that it will be fast to       find them */    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;}