如何在h.264 RTP数据包中找到Nal头

时间:2015-06-01 10:21:53

标签: header h.264 rtp

我需要通过解析RTP数据包来找到NAL头,其中每个NAL单元被封装到一个RTP数据包中,然后我解析Nal头以知道它是否是PPS单元。我尝试了以下但没有结果:

       dataBuffer = (char*)MESSAGE_ReturnPacket(msg);

       byte * hdr = (byte*)dataBuffer + RTP_HDR_SIZE; //dataBuffer contains  the RTP packet

        RTPParsing((byte*)dataBuffer,rp,hdr);

        if (rp.nal_type == 8 )
        {
            printf("\n  PPS is found \n");

        }
        else
       {
       printf("\n No PPS is found\n");

        }

其中

    int RTPParsing(byte *pData,RTPpacket_t &rp, byte *hdr)
    {
    if ((pData[0] & 0xc0) != (2 << 6)){
    printf("[RTP] version is incorrect! dump = 0x%x 0x%x 0x%x 0x%x 

    \n",pData[0], pData[1], pData[2], pData[3]);
    return 0;
    }

/* parse RTP header */

rp.v = (pData[0] & 0xc0) >> 6; /* protocol version */
rp.p = (pData[0] & 0x40) >> 5; /* padding flag */
rp.x = (pData[0] & 0x20) >> 4; /* header extension flag */
rp.cc = (pData[0] & 0x0f);  /* CSRC count */
rp.m = (pData[1] & 0x80) >> 7; /* marker bit */
rp.pt = (pData[1] & 0x7F); //Payload Type
rp.seq = ntohs (((unsigned short *) pData)[1]);    /* sequence number */
rp.timestamp = ntohl (((unsigned int *) pData)[1]); /* timestamp */
rp.ssrc = ntohl (((unsigned int *) pData)[2]);  /* synchronization source */
rp.nal_type = (hdr[1] & 0x1F); // get NAL unit's type

if (rp.cc)

    {
    for (int i = 0; i < rp.cc; i++)
    {
        //fprintf (out, " csrc: 0x%08x",ntohl (((unsigned int *) data)[3 + i]));
    }
    }
    return 0;
    }

任何帮助?

1 个答案:

答案 0 :(得分:0)

根据单个NAL单元模式中的RFC6184,“NAL单元的第一个字节共同用作 RTP有效负载标头“

你的偏移是不正确的(1而不是0):

rp.nal_type = (hdr[1] & 0x1F); // get NAL unit's type

此外,将RTP_HDR_SIZE硬编码为12(如果这是您正在做的事情)可能会导致问题,因为标头的大小可能会因扩展标头,CSRC等而异。