url_fseek(pb, pos, SEEK_SET); mpegts_set_service(ts, sid, set_service_cb, ts);  handle_packets(ts, MAX_SCAN_PACKETS);
static int mpegts_read_header(AVFormatContext *s,                              AVFormatParameters *ap){    MpegTSContext *ts = s->priv_data;    ByteIOContext *pb = &s->pb;    uint8_t buf[1024];    int len, sid;    int64_t pos;    MpegTSService *service;        if (ap) {        ts->mpeg2ts_raw = ap->mpeg2ts_raw;        ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;    }    /* read the first 1024 bytes to get packet size */    pos = url_ftell(pb);    len = get_buffer(pb, buf, sizeof(buf));    if (len != sizeof(buf))        goto fail;    ts->raw_packet_size = get_packet_size(buf, sizeof(buf));    if (ts->raw_packet_size <= 0)        goto fail;    ts->stream = s;    ts->auto_guess = 0;    if (!ts->mpeg2ts_raw) {        /* normal demux */        if (!ts->auto_guess) {            ts->set_service_ret = -1;            /* first do a scaning to get all the services */            url_fseek(pb, pos, SEEK_SET);            mpegts_scan_sdt(ts);                        handle_packets(ts, MAX_SCAN_PACKETS);                        if (ts->nb_services <= 0) {                /* no SDT found, we try to look at the PAT */                                /* First remove the SDT filters from each PID */                int i;                for (i=0; i < NB_PID_MAX; i++) {                    if (ts->pids[i])                        mpegts_close_filter(ts, ts->pids[i]);                }                url_fseek(pb, pos, SEEK_SET);                mpegts_scan_pat(ts);                                handle_packets(ts, MAX_SCAN_PACKETS);            }                        if (ts->nb_services <= 0) {		/* raw transport stream */		ts->auto_guess = 1;		s->ctx_flags |= AVFMTCTX_NOHEADER;		goto do_pcr;	    }                        /* tune to first service found */            service = ts->services[0];            sid = service->sid;#ifdef DEBUG_SI            printf("tuning to '%s'\n", service->name);#endif                        /* now find the info for the first service if we found any,               otherwise try to filter all PATs */                        url_fseek(pb, pos, SEEK_SET);            mpegts_set_service(ts, sid, set_service_cb, ts);                        handle_packets(ts, MAX_SCAN_PACKETS);                        /* if could not find service, exit */            if (ts->set_service_ret != 0)                return -1;            #ifdef DEBUG_SI            printf("tuning done\n");#endif        }        s->ctx_flags |= AVFMTCTX_NOHEADER;    } else {        AVStream *st;        int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;        int64_t pcrs[2], pcr_h;        int packet_count[2];        uint8_t packet[TS_PACKET_SIZE];                /* only read packets */            do_pcr:        st = av_new_stream(s, 0);        if (!st)            goto fail;        av_set_pts_info(st, 60, 1, 27000000);        st->codec.codec_type = CODEC_TYPE_DATA;        st->codec.codec_id = CODEC_ID_MPEG2TS;                /* we iterate until we find two PCRs to estimate the bitrate */        pcr_pid = -1;        nb_pcrs = 0;        nb_packets = 0;        for(;;) {            ret = read_packet(&s->pb, packet, ts->raw_packet_size);            if (ret < 0)                return -1;            pid = ((packet[1] & 0x1f) << 8) | packet[2];            if ((pcr_pid == -1 || pcr_pid == pid) &&                parse_pcr(&pcr_h, &pcr_l, packet) == 0) {                pcr_pid = pid;                packet_count[nb_pcrs] = nb_packets;                pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;                nb_pcrs++;                if (nb_pcrs >= 2)                    break;            }            nb_packets++;        }        ts->pcr_pid = pcr_pid;        /* NOTE1: the bitrate is computed without the FEC */        /* NOTE2: it is only the bitrate of the start of the stream */        ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);        ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];        s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;        st->codec.bit_rate = s->bit_rate;        st->start_time = ts->cur_pcr * 1000000.0 / 27.0e6;#if 0        printf("start=%0.3f pcr=%0.3f incr=%d\n",               st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);#endif    }    url_fseek(pb, pos, SEEK_SET);    return 0; fail:    return -1;}