static int mpegts_read_header(AVFormatContext *s,
                              AVFormatParameters *ap)
{
    MpegTSContext *ts = s->priv_data;
    ByteIOContext *pb = s->pb;
    uint8_t buf[1024];
    int len;
    int64_t pos;
    if (ap) {
        ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
        if(ap->mpeg2ts_raw){
            av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
            return -1;
        }
    }
    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 (s->iformat == &mpegts_demuxer) {
        url_fseek(pb, pos, SEEK_SET);
        mpegts_scan_sdt(ts);
        mpegts_set_service(ts);
        handle_packets(ts, s->probesize);
        ts->auto_guess = 1;
#ifdef DEBUG_SI
        av_log(ts->stream, AV_LOG_DEBUG, "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];
        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;
        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 = AV_RB16(packet + 1) & 0x1fff;
            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_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;
#if 0
        av_log(ts->stream, AV_LOG_DEBUG, "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;
}
['static int mpegts_read_header(AVFormatContext *s,\n                              AVFormatParameters *ap)\n{\n    MpegTSContext *ts = s->priv_data;\n    ByteIOContext *pb = s->pb;\n    uint8_t buf[1024];\n    int len;\n    int64_t pos;\n    if (ap) {\n        ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;\n        if(ap->mpeg2ts_raw){\n            av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\\n");\n            return -1;\n        }\n    }\n    pos = url_ftell(pb);\n    len = get_buffer(pb, buf, sizeof(buf));\n    if (len != sizeof(buf))\n        goto fail;\n    ts->raw_packet_size = get_packet_size(buf, sizeof(buf));\n    if (ts->raw_packet_size <= 0)\n        goto fail;\n    ts->stream = s;\n    ts->auto_guess = 0;\n    if (s->iformat == &mpegts_demuxer) {\n        url_fseek(pb, pos, SEEK_SET);\n        mpegts_scan_sdt(ts);\n        mpegts_set_service(ts);\n        handle_packets(ts, s->probesize);\n        ts->auto_guess = 1;\n#ifdef DEBUG_SI\n        av_log(ts->stream, AV_LOG_DEBUG, "tuning done\\n");\n#endif\n        s->ctx_flags |= AVFMTCTX_NOHEADER;\n    } else {\n        AVStream *st;\n        int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;\n        int64_t pcrs[2], pcr_h;\n        int packet_count[2];\n        uint8_t packet[TS_PACKET_SIZE];\n        st = av_new_stream(s, 0);\n        if (!st)\n            goto fail;\n        av_set_pts_info(st, 60, 1, 27000000);\n        st->codec->codec_type = CODEC_TYPE_DATA;\n        st->codec->codec_id = CODEC_ID_MPEG2TS;\n        pcr_pid = -1;\n        nb_pcrs = 0;\n        nb_packets = 0;\n        for(;;) {\n            ret = read_packet(s->pb, packet, ts->raw_packet_size);\n            if (ret < 0)\n                return -1;\n            pid = AV_RB16(packet + 1) & 0x1fff;\n            if ((pcr_pid == -1 || pcr_pid == pid) &&\n                parse_pcr(&pcr_h, &pcr_l, packet) == 0) {\n                pcr_pid = pid;\n                packet_count[nb_pcrs] = nb_packets;\n                pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;\n                nb_pcrs++;\n                if (nb_pcrs >= 2)\n                    break;\n            }\n            nb_packets++;\n        }\n        ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);\n        ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];\n        s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;\n        st->codec->bit_rate = s->bit_rate;\n        st->start_time = ts->cur_pcr;\n#if 0\n        av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\\n",\n               st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);\n#endif\n    }\n    url_fseek(pb, pos, SEEK_SET);\n    return 0;\n fail:\n    return -1;\n}']