static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
{
    MpegTSFilter *tss;
    int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
        has_adaptation, has_payload;
    const uint8_t *p, *p_end;
    int64_t pos;
    pid = AV_RB16(packet + 1) & 0x1fff;
    if (pid && discard_pid(ts, pid))
        return 0;
    is_start = packet[1] & 0x40;
    tss = ts->pids[pid];
    if (ts->auto_guess && !tss && is_start) {
        add_pes_stream(ts, pid, -1);
        tss = ts->pids[pid];
    if (!tss)
        return 0;
    ts->current_pid = pid;
    afc = (packet[3] >> 4) & 3;
    if (afc == 0) /* reserved value */
        return 0;
    has_adaptation   = afc & 2;
    has_payload      = afc & 1;
    is_discontinuity = has_adaptation &&
                       packet[4] != 0 && /* with length > 0 */
                       (packet[5] & 0x80); /* and discontinuity indicated */
    /* continuity check (currently not used) */
    cc = (packet[3] & 0xf);
    expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
    cc_ok = pid == 0x1FFF || // null packet PID
            is_discontinuity ||
            tss->last_cc < 0 ||
            expected_cc == cc;
    tss->last_cc = cc;
    if (!cc_ok) {
        av_log(ts->stream, AV_LOG_DEBUG,
               "Continuity check failed for pid %d expected %d got %d\n",
               pid, expected_cc, cc);
    p = packet + 4;
    if (has_adaptation) {
        int64_t pcr_h;
        int pcr_l;
        if (parse_pcr(&pcr_h, &pcr_l, packet) == 0)
            tss->last_pcr = pcr_h * 300 + pcr_l;
        /* skip adaptation field */
        p += p[0] + 1;
    /* if past the end of packet, ignore */
    p_end = packet + TS_PACKET_SIZE;
    if (p >= p_end || !has_payload)
        return 0;
    pos = avio_tell(ts->stream->pb);
    if (pos >= 0) {
        av_assert0(pos >= TS_PACKET_SIZE);
        ts->pos47_full = pos - TS_PACKET_SIZE;
    if (tss->type == MPEGTS_SECTION) {
        if (is_start) {
            /* pointer field present */
            len = *p++;
            if (len > p_end - p)
                return 0;
            if (len && cc_ok) {
                /* write remaining section bytes */
                write_section_data(ts, tss,
                                   p, len, 0);
                /* check whether filter has been closed */
                if (!ts->pids[pid])
                    return 0;
            p += len;
            if (p < p_end) {
                write_section_data(ts, tss,
                                   p, p_end - p, 1);
        } else {
            if (cc_ok) {
                write_section_data(ts, tss,
                                   p, p_end - p, 0);
        // stop find_stream_info from waiting for more streams
        // when all programs have received a PMT
        if (ts->stream->ctx_flags & AVFMTCTX_NOHEADER && ts->scan_all_pmts <= 0) {
            int i;
            for (i = 0; i < ts->nb_prg; i++) {
                if (!ts->prg[i].pmt_found)
                    break;
            if (i == ts->nb_prg && ts->nb_prg > 0) {
                int types = 0;
                for (i = 0; i < ts->stream->nb_streams; i++) {
                    AVStream *st = ts->stream->streams[i];
                    if (st->codecpar->codec_type >= 0)
                        types |= 1<<st->codecpar->codec_type;
                if ((types & (1<<AVMEDIA_TYPE_AUDIO) && types & (1<<AVMEDIA_TYPE_VIDEO)) || pos > 100000) {
                    av_log(ts->stream, AV_LOG_DEBUG, "All programs have pmt, headers found\n");
                    ts->stream->ctx_flags &= ~AVFMTCTX_NOHEADER;
    } else {
        int ret;
        // Note: The position here points actually behind the current packet.
            if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
                                                pos - ts->raw_packet_size)) < 0)
                return ret;
    return 0;
static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
{
    MpegTSFilter *tss;
    int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
        has_adaptation, has_payload;
    const uint8_t *p, *p_end;
    int64_t pos;
    pid = AV_RB16(packet + 1) & 0x1fff;
    if (pid && discard_pid(ts, pid))
        return 0;
    is_start = packet[1] & 0x40;
    tss = ts->pids[pid];
    if (ts->auto_guess && !tss && is_start) {
        add_pes_stream(ts, pid, -1);
        tss = ts->pids[pid];
    if (!tss)
        return 0;
    ts->current_pid = pid;
    afc = (packet[3] >> 4) & 3;
    if (afc == 0) 
        return 0;
    has_adaptation   = afc & 2;
    has_payload      = afc & 1;
    is_discontinuity = has_adaptation &&
                       packet[4] != 0 && 
                       (packet[5] & 0x80); 
    
    cc = (packet[3] & 0xf);
    expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
    cc_ok = pid == 0x1FFF || 
            is_discontinuity ||
            tss->last_cc < 0 ||
            expected_cc == cc;
    tss->last_cc = cc;
    if (!cc_ok) {
        av_log(ts->stream, AV_LOG_DEBUG,
               "Continuity check failed for pid %d expected %d got %d\n",
               pid, expected_cc, cc);
    p = packet + 4;
    if (has_adaptation) {
        int64_t pcr_h;
        int pcr_l;
        if (parse_pcr(&pcr_h, &pcr_l, packet) == 0)
            tss->last_pcr = pcr_h * 300 + pcr_l;
        
        p += p[0] + 1;
    
    p_end = packet + TS_PACKET_SIZE;
    if (p >= p_end || !has_payload)
        return 0;
    pos = avio_tell(ts->stream->pb);
    if (pos >= 0) {
        av_assert0(pos >= TS_PACKET_SIZE);
        ts->pos47_full = pos - TS_PACKET_SIZE;
    if (tss->type == MPEGTS_SECTION) {
        if (is_start) {
            
            len = *p++;
            if (len > p_end - p)
                return 0;
            if (len && cc_ok) {
                
                write_section_data(ts, tss,
                                   p, len, 0);
                
                if (!ts->pids[pid])
                    return 0;
            p += len;
            if (p < p_end) {
                write_section_data(ts, tss,
                                   p, p_end - p, 1);
        } else {
            if (cc_ok) {
                write_section_data(ts, tss,
                                   p, p_end - p, 0);
        
        
        if (ts->stream->ctx_flags & AVFMTCTX_NOHEADER && ts->scan_all_pmts <= 0) {
            int i;
            for (i = 0; i < ts->nb_prg; i++) {
                if (!ts->prg[i].pmt_found)
                    break;
            if (i == ts->nb_prg && ts->nb_prg > 0) {
                int types = 0;
                for (i = 0; i < ts->stream->nb_streams; i++) {
                    AVStream *st = ts->stream->streams[i];
                    if (st->codecpar->codec_type >= 0)
                        types |= 1<<st->codecpar->codec_type;
                if ((types & (1<<AVMEDIA_TYPE_AUDIO) && types & (1<<AVMEDIA_TYPE_VIDEO)) || pos > 100000) {
                    av_log(ts->stream, AV_LOG_DEBUG, "All programs have pmt, headers found\n");
                    ts->stream->ctx_flags &= ~AVFMTCTX_NOHEADER;
    } else {
        int ret;
        
            if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
                                                pos - ts->raw_packet_size)) < 0)
                return ret;
    return 0;
static int FUNC_0(MpegTSContext *VAR_0, const uint8_t *VAR_1)
{
    MpegTSFilter *tss;
    int VAR_2, VAR_3, VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9,
        VAR_10, VAR_11;
    const uint8_t *VAR_12, *p_end;
    int64_t pos;
    VAR_3 = AV_RB16(VAR_1 + 1) & 0x1fff;
    if (VAR_3 && discard_pid(VAR_0, VAR_3))
        return 0;
    VAR_8 = VAR_1[1] & 0x40;
    tss = VAR_0->pids[VAR_3];
    if (VAR_0->auto_guess && !tss && VAR_8) {
        add_pes_stream(VAR_0, VAR_3, -1);
        tss = VAR_0->pids[VAR_3];
    if (!tss)
        return 0;
    VAR_0->current_pid = VAR_3;
    VAR_7 = (VAR_1[3] >> 4) & 3;
    if (VAR_7 == 0) 
        return 0;
    VAR_10   = VAR_7 & 2;
    VAR_11      = VAR_7 & 1;
    VAR_9 = VAR_10 &&
                       VAR_1[4] != 0 && 
                       (VAR_1[5] & 0x80); 
    
    VAR_4 = (VAR_1[3] & 0xf);
    VAR_5 = VAR_11 ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
    VAR_6 = VAR_3 == 0x1FFF || 
            VAR_9 ||
            tss->last_cc < 0 ||
            VAR_5 == VAR_4;
    tss->last_cc = VAR_4;
    if (!VAR_6) {
        av_log(VAR_0->stream, AV_LOG_DEBUG,
               "Continuity check failed for VAR_3 %d expected %d got %d\n",
               VAR_3, VAR_5, VAR_4);
    VAR_12 = VAR_1 + 4;
    if (VAR_10) {
        int64_t pcr_h;
        int VAR_13;
        if (parse_pcr(&pcr_h, &VAR_13, VAR_1) == 0)
            tss->last_pcr = pcr_h * 300 + VAR_13;
        
        VAR_12 += VAR_12[0] + 1;
    
    p_end = VAR_1 + TS_PACKET_SIZE;
    if (VAR_12 >= p_end || !VAR_11)
        return 0;
    pos = avio_tell(VAR_0->stream->pb);
    if (pos >= 0) {
        av_assert0(pos >= TS_PACKET_SIZE);
        VAR_0->pos47_full = pos - TS_PACKET_SIZE;
    if (tss->type == MPEGTS_SECTION) {
        if (VAR_8) {
            
            VAR_2 = *VAR_12++;
            if (VAR_2 > p_end - VAR_12)
                return 0;
            if (VAR_2 && VAR_6) {
                
                write_section_data(VAR_0, tss,
                                   VAR_12, VAR_2, 0);
                
                if (!VAR_0->pids[VAR_3])
                    return 0;
            VAR_12 += VAR_2;
            if (VAR_12 < p_end) {
                write_section_data(VAR_0, tss,
                                   VAR_12, p_end - VAR_12, 1);
        } else {
            if (VAR_6) {
                write_section_data(VAR_0, tss,
                                   VAR_12, p_end - VAR_12, 0);
        
        
        if (VAR_0->stream->ctx_flags & AVFMTCTX_NOHEADER && VAR_0->scan_all_pmts <= 0) {
            int VAR_14;
            for (VAR_14 = 0; VAR_14 < VAR_0->nb_prg; VAR_14++) {
                if (!VAR_0->prg[VAR_14].pmt_found)
                    break;
            if (VAR_14 == VAR_0->nb_prg && VAR_0->nb_prg > 0) {
                int types = 0;
                for (VAR_14 = 0; VAR_14 < VAR_0->stream->nb_streams; VAR_14++) {
                    AVStream *st = VAR_0->stream->streams[VAR_14];
                    if (st->codecpar->codec_type >= 0)
                        types |= 1<<st->codecpar->codec_type;
                if ((types & (1<<AVMEDIA_TYPE_AUDIO) && types & (1<<AVMEDIA_TYPE_VIDEO)) || pos > 100000) {
                    av_log(VAR_0->stream, AV_LOG_DEBUG, "All programs have pmt, headers found\n");
                    VAR_0->stream->ctx_flags &= ~AVFMTCTX_NOHEADER;
    } else {
        int ret;
        
            if ((ret = tss->u.pes_filter.pes_cb(tss, VAR_12, p_end - VAR_12, VAR_8,
                                                pos - VAR_0->raw_packet_size)) < 0)
                return ret;
    return 0;
static int FUNC_0(MpegTSContext *VAR_0, const uint8_t *VAR_1)
{ MpegTSFilter *tss; int VAR_2, VAR_3, VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9,
VAR_10, VAR_11; const uint8_t *VAR_12, *p_end; int64_t pos; VAR_3 = AV_RB16(VAR_1 + 1) & 0x1fff; if (VAR_3 && discard_pid(VAR_0, VAR_3))
return 0; VAR_8 = VAR_1[1] & 0x40; tss = VAR_0->pids[VAR_3]; if (VAR_0->auto_guess && !tss && VAR_8) { add_pes_stream(VAR_0, VAR_3, -1); tss = VAR_0->pids[VAR_3]; if (!tss)
return 0; VAR_0->current_pid = VAR_3; VAR_7 = (VAR_1[3] >> 4) & 3; if (VAR_7 == 0)
return 0; VAR_10   = VAR_7 & 2; VAR_11      = VAR_7 & 1; VAR_9 = VAR_10 &&
VAR_1[4] != 0 &&
(VAR_1[5] & 0x80); VAR_4 = (VAR_1[3] & 0xf); VAR_5 = VAR_11 ? (tss->last_cc + 1) & 0x0f : tss->last_cc; VAR_6 = VAR_3 == 0x1FFF ||
VAR_9 ||
tss->last_cc < 0 ||
VAR_5 == VAR_4; tss->last_cc = VAR_4; if (!VAR_6) { av_log(VAR_0->stream, AV_LOG_DEBUG,
"Continuity check failed for VAR_3 %d expected %d got %d\n",
VAR_3, VAR_5, VAR_4); VAR_12 = VAR_1 + 4; if (VAR_10) { int64_t pcr_h; int VAR_13; if (parse_pcr(&pcr_h, &VAR_13, VAR_1) == 0)
tss->last_pcr = pcr_h * 300 + VAR_13; VAR_12 += VAR_12[0] + 1; p_end = VAR_1 + TS_PACKET_SIZE; if (VAR_12 >= p_end || !VAR_11)
return 0; pos = avio_tell(VAR_0->stream->pb); if (pos >= 0) { av_assert0(pos >= TS_PACKET_SIZE); VAR_0->pos47_full = pos - TS_PACKET_SIZE; if (tss->type == MPEGTS_SECTION) { if (VAR_8) { VAR_2 = *VAR_12++; if (VAR_2 > p_end - VAR_12)
return 0; if (VAR_2 && VAR_6) { write_section_data(VAR_0, tss,
VAR_12, VAR_2, 0); if (!VAR_0->pids[VAR_3])
return 0; VAR_12 += VAR_2; if (VAR_12 < p_end) { write_section_data(VAR_0, tss,
VAR_12, p_end - VAR_12, 1); } else { if (VAR_6) { write_section_data(VAR_0, tss,
VAR_12, p_end - VAR_12, 0); if (VAR_0->stream->ctx_flags & AVFMTCTX_NOHEADER && VAR_0->scan_all_pmts <= 0) { int VAR_14; for (VAR_14 = 0; VAR_14 < VAR_0->nb_prg; VAR_14++) { if (!VAR_0->prg[VAR_14].pmt_found)
break; if (VAR_14 == VAR_0->nb_prg && VAR_0->nb_prg > 0) { int types = 0; for (VAR_14 = 0; VAR_14 < VAR_0->stream->nb_streams; VAR_14++) { AVStream *st = VAR_0->stream->streams[VAR_14]; if (st->codecpar->codec_type >= 0)
types |= 1<<st->codecpar->codec_type; if ((types & (1<<AVMEDIA_TYPE_AUDIO) && types & (1<<AVMEDIA_TYPE_VIDEO)) || pos > 100000) { av_log(VAR_0->stream, AV_LOG_DEBUG, "All programs have pmt, headers found\n"); VAR_0->stream->ctx_flags &= ~AVFMTCTX_NOHEADER; } else { int ret; if ((ret = tss->u.pes_filter.pes_cb(tss, VAR_12, p_end - VAR_12, VAR_8,
pos - VAR_0->raw_packet_size)) < 0)
return ret; return 0;