⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mpegts.c

📁 ffmpeg源码分析
💻 C
📖 第 1 页 / 共 3 页
字号:
static void pat_cb(void *opaque, const uint8_t *section, int section_len){    MpegTSContext *ts = opaque;    SectionHeader h1, *h = &h1;    const uint8_t *p, *p_end;    int sid, pmt_pid;#ifdef DEBUG_SI    printf("PAT:\n");    av_hex_dump(stdout, (uint8_t *)section, section_len);#endif    p_end = section + section_len - 4;    p = section;    if (parse_section_header(h, &p, p_end) < 0)        return;    if (h->tid != PAT_TID)        return;    for(;;) {        sid = get16(&p, p_end);        if (sid < 0)            break;        pmt_pid = get16(&p, p_end) & 0x1fff;        if (pmt_pid < 0)            break;#ifdef DEBUG_SI        printf("sid=0x%x pid=0x%x\n", sid, pmt_pid);#endif        if (sid == 0x0000) {            /* NIT info */        } else {            if (ts->req_sid == sid) {                ts->pmt_filter = mpegts_open_section_filter(ts, pmt_pid,                                                            pmt_cb, ts, 1);                goto found;            }        }    }    /* not found */    ts->set_service_cb(ts->set_service_opaque, -1); found:    mpegts_close_filter(ts, ts->pat_filter);    ts->pat_filter = NULL;}/* add all services found in the PAT */static void pat_scan_cb(void *opaque, const uint8_t *section, int section_len){    MpegTSContext *ts = opaque;    SectionHeader h1, *h = &h1;    const uint8_t *p, *p_end;    int sid, pmt_pid;    char *provider_name, *name;    char buf[256];#ifdef DEBUG_SI    printf("PAT:\n");    av_hex_dump(stdout, (uint8_t *)section, section_len);#endif    p_end = section + section_len - 4;    p = section;    if (parse_section_header(h, &p, p_end) < 0)        return;    if (h->tid != PAT_TID)        return;    for(;;) {        sid = get16(&p, p_end);        if (sid < 0)            break;        pmt_pid = get16(&p, p_end) & 0x1fff;        if (pmt_pid < 0)            break;#ifdef DEBUG_SI        printf("sid=0x%x pid=0x%x\n", sid, pmt_pid);#endif        if (sid == 0x0000) {            /* NIT info */        } else {            /* add the service with a dummy name */            snprintf(buf, sizeof(buf), "Service %x\n", sid);            name = av_strdup(buf);            provider_name = av_strdup("");            if (name && provider_name) {                new_service(ts, sid, provider_name, name);            } else {                av_freep(&name);                av_freep(&provider_name);            }        }    }    ts->stop_parse = 1;    /* remove filter */    mpegts_close_filter(ts, ts->pat_filter);    ts->pat_filter = NULL;}void mpegts_set_service(MpegTSContext *ts, int sid,                        SetServiceCallback *set_service_cb, void *opaque){    ts->set_service_cb = set_service_cb;    ts->set_service_opaque = opaque;    ts->req_sid = sid;    ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,                                                pat_cb, ts, 1);}static void sdt_cb(void *opaque, const uint8_t *section, int section_len){    MpegTSContext *ts = opaque;    SectionHeader h1, *h = &h1;    const uint8_t *p, *p_end, *desc_list_end, *desc_end;    int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;    char *name, *provider_name;#ifdef DEBUG_SI    printf("SDT:\n");    av_hex_dump(stdout, (uint8_t *)section, section_len);#endif    p_end = section + section_len - 4;    p = section;    if (parse_section_header(h, &p, p_end) < 0)        return;    if (h->tid != SDT_TID)        return;    onid = get16(&p, p_end);    if (onid < 0)        return;    val = get8(&p, p_end);    if (val < 0)        return;    for(;;) {        sid = get16(&p, p_end);        if (sid < 0)            break;        val = get8(&p, p_end);        if (val < 0)            break;        desc_list_len = get16(&p, p_end) & 0xfff;        if (desc_list_len < 0)            break;        desc_list_end = p + desc_list_len;        if (desc_list_end > p_end)            break;        for(;;) {            desc_tag = get8(&p, desc_list_end);            if (desc_tag < 0)                break;            desc_len = get8(&p, desc_list_end);            desc_end = p + desc_len;            if (desc_end > desc_list_end)                break;#ifdef DEBUG_SI            printf("tag: 0x%02x len=%d\n", desc_tag, desc_len);#endif            switch(desc_tag) {            case 0x48:                service_type = get8(&p, p_end);                if (service_type < 0)                    break;                provider_name = getstr8(&p, p_end);                if (!provider_name)                    break;                name = getstr8(&p, p_end);                if (!name)                    break;                new_service(ts, sid, provider_name, name);                break;            default:                break;            }            p = desc_end;        }        p = desc_list_end;    }    ts->stop_parse = 1;    /* remove filter */    mpegts_close_filter(ts, ts->sdt_filter);    ts->sdt_filter = NULL;}/* scan services in a transport stream by looking at the SDT */void mpegts_scan_sdt(MpegTSContext *ts){    ts->sdt_filter = mpegts_open_section_filter(ts, SDT_PID,                                                sdt_cb, ts, 1);}/* scan services in a transport stream by looking at the PAT (better   than nothing !) */void mpegts_scan_pat(MpegTSContext *ts){    ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,                                                pat_scan_cb, ts, 1);}/* TS stream handling */enum MpegTSState {    MPEGTS_HEADER = 0,    MPEGTS_PESHEADER_FILL,    MPEGTS_PAYLOAD,    MPEGTS_SKIP,};/* enough for PES header + length */#define PES_START_SIZE 9#define MAX_PES_HEADER_SIZE (9 + 255)struct PESContext {    int pid;    int stream_type;    MpegTSContext *ts;    AVFormatContext *stream;    AVStream *st;    enum MpegTSState state;    /* used to get the format */    int data_index;    int total_size;    int pes_header_size;    int64_t pts, dts;    uint8_t header[MAX_PES_HEADER_SIZE];};static int64_t get_pts(const uint8_t *p){    int64_t pts;    int val;    pts = (int64_t)((p[0] >> 1) & 0x07) << 30;    val = (p[1] << 8) | p[2];    pts |= (int64_t)(val >> 1) << 15;    val = (p[3] << 8) | p[4];    pts |= (int64_t)(val >> 1);    return pts;}/* return non zero if a packet could be constructed */static void mpegts_push_data(void *opaque,                             const uint8_t *buf, int buf_size, int is_start){    PESContext *pes = opaque;    MpegTSContext *ts = pes->ts;    const uint8_t *p;    int len, code;    if (is_start) {        pes->state = MPEGTS_HEADER;        pes->data_index = 0;    }    p = buf;    while (buf_size > 0) {        switch(pes->state) {        case MPEGTS_HEADER:            len = PES_START_SIZE - pes->data_index;            if (len > buf_size)                len = buf_size;            memcpy(pes->header + pes->data_index, p, len);            pes->data_index += len;            p += len;            buf_size -= len;            if (pes->data_index == PES_START_SIZE) {                /* we got all the PES or section header. We can now                   decide */#if 0                av_hex_dump(pes->header, pes->data_index);#endif                if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&                    pes->header[2] == 0x01) {                    /* it must be an mpeg2 PES stream */                    code = pes->header[3] | 0x100;                    if (!((code >= 0x1c0 && code <= 0x1df) ||                          (code >= 0x1e0 && code <= 0x1ef) ||                          (code == 0x1bd)))                        goto skip;                    if (!pes->st) {                        /* allocate stream */                        new_pes_av_stream(pes, code);                    }                    pes->state = MPEGTS_PESHEADER_FILL;                    pes->total_size = (pes->header[4] << 8) | pes->header[5];                    /* NOTE: a zero total size means the PES size is                       unbounded */                    if (pes->total_size)                        pes->total_size += 6;                    pes->pes_header_size = pes->header[8] + 9;                } else {                    /* otherwise, it should be a table */                    /* skip packet */                skip:                    pes->state = MPEGTS_SKIP;                    continue;                }            }            break;            /**********************************************/            /* PES packing parsing */        case MPEGTS_PESHEADER_FILL:            len = pes->pes_header_size - pes->data_index;            if (len > buf_size)                len = buf_size;            memcpy(pes->header + pes->data_index, p, len);            pes->data_index += len;            p += len;            buf_size -= len;            if (pes->data_index == pes->pes_header_size) {                const uint8_t *r;                unsigned int flags;                flags = pes->header[7];                r = pes->header + 9;                pes->pts = AV_NOPTS_VALUE;                pes->dts = AV_NOPTS_VALUE;                if ((flags & 0xc0) == 0x80) {                    pes->pts = get_pts(r);                    r += 5;                } else if ((flags & 0xc0) == 0xc0) {                    pes->pts = get_pts(r);                    r += 5;                    pes->dts = get_pts(r);                    r += 5;                }                /* we got the full header. We parse it and get the payload */                pes->state = MPEGTS_PAYLOAD;            }            break;        case MPEGTS_PAYLOAD:            if (pes->total_size) {                len = pes->total_size - pes->data_index;                if (len > buf_size)                    len = buf_size;            } else {                len = buf_size;            }            if (len > 0) {                AVPacket *pkt = ts->pkt;                if (pes->st && av_new_packet(pkt, len) == 0) {                    memcpy(pkt->data, p, len);                    pkt->stream_index = pes->st->index;                    pkt->pts = pes->pts;                    pkt->dts = pes->dts;                    /* reset pts values */                    pes->pts = AV_NOPTS_VALUE;                    pes->dts = AV_NOPTS_VALUE;                    ts->stop_parse = 1;                    return;                }            }            buf_size = 0;            break;        case MPEGTS_SKIP:            buf_size = 0;            break;        }    }}static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code){    AVStream *st;    int codec_type, codec_id;    switch(pes->stream_type){    case STREAM_TYPE_AUDIO_MPEG1:    case STREAM_TYPE_AUDIO_MPEG2:        codec_type = CODEC_TYPE_AUDIO;        codec_id = CODEC_ID_MP3;        break;    case STREAM_TYPE_VIDEO_MPEG1:    case STREAM_TYPE_VIDEO_MPEG2:        codec_type = CODEC_TYPE_VIDEO;        codec_id = CODEC_ID_MPEG2VIDEO;        break;    case STREAM_TYPE_VIDEO_MPEG4:        codec_type = CODEC_TYPE_VIDEO;        codec_id = CODEC_ID_MPEG4;        break;    case STREAM_TYPE_VIDEO_H264:        codec_type = CODEC_TYPE_VIDEO;        codec_id = CODEC_ID_H264;        break;    case STREAM_TYPE_AUDIO_AAC:        codec_type = CODEC_TYPE_AUDIO;        codec_id = CODEC_ID_AAC;        break;    case STREAM_TYPE_AUDIO_AC3:        codec_type = CODEC_TYPE_AUDIO;        codec_id = CODEC_ID_AC3;        break;    case STREAM_TYPE_AUDIO_DTS:        codec_type = CODEC_TYPE_AUDIO;        codec_id = CODEC_ID_DTS;        break;    case STREAM_TYPE_SUBTITLE_DVB:        codec_type = CODEC_TYPE_SUBTITLE;        codec_id = CODEC_ID_DVB_SUBTITLE;        break;    default:        if (code >= 0x1c0 && code <= 0x1df) {            codec_type = CODEC_TYPE_AUDIO;            codec_id = CODEC_ID_MP2;        } else if (code == 0x1bd) {            codec_type = CODEC_TYPE_AUDIO;            codec_id = CODEC_ID_AC3;        } else {            codec_type = CODEC_TYPE_VIDEO;            codec_id = CODEC_ID_MPEG1VIDEO;        }        break;    }    st = av_new_stream(pes->stream, pes->pid);    if (st) {        av_set_pts_info(st, 33, 1, 90000);        st->priv_data = pes;        st->codec->codec_type = codec_type;        st->codec->codec_id = codec_id;        st->need_parsing = 1;        pes->st = st;    }    return st;}static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int stream_type){    MpegTSFilter *tss;    PESContext *pes;    /* if no pid found, then add a pid context */    pes = av_mallocz(sizeof(PESContext));    if (!pes)        return 0;    pes->ts = ts;    pes->stream = ts->stream;    pes->pid = pid;    pes->stream_type = stream_type;    tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);    if (!tss) {        av_free(pes);        return 0;    }    return pes;}/* handle one TS packet */static void handle_packet(MpegTSContext *ts, const uint8_t *packet){    AVFormatContext *s = ts->stream;    MpegTSFilter *tss;    int len, pid, cc, cc_ok, afc, is_start;    const uint8_t *p, *p_end;    pid = ((packet[1] & 0x1f) << 8) | packet[2];    is_start = packet[1] & 0x40;    tss = ts->pids[pid];    if (ts->auto_guess && tss == NULL && is_start) {        add_pes_stream(ts, pid, 0);        tss = ts->pids[pid];    }    if (!tss)        return;    /* continuity check (currently not used) */    cc = (packet[3] & 0xf);    cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));    tss->last_cc = cc;    /* skip adaptation field */    afc = (packet[3] >> 4) & 3;    p = packet + 4;    if (afc == 0) /* reserved value */        return;    if (afc == 2) /* adaptation field only */        return;    if (afc == 3) {        /* skip adapation field */        p += p[0] + 1;    }    /* if past the end of packet, ignore */    p_end = packet + TS_PACKET_SIZE;    if (p >= p_end)        return;    if (tss->type == MPEGTS_SECTION) {        if (is_start) {            /* pointer field present */            len = *p++;            if (p + len > p_end)                return;            if (len && cc_ok) {                /* write remaining section bytes */                write_section_data(s, tss,                                   p, len, 0);                /* check whether filter has been closed */                if (!ts->pids[pid])                    return;            }            p += len;            if (p < p_end) {                write_section_data(s, tss,                                   p, p_end - p, 1);            }        } else {            if (cc_ok) {                write_section_data(s, tss,                                   p, p_end - p, 0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -