h264 reference frames

2019-04-11 09:44发布

I'm looking for a algorithm of finding reference frames in h264 stream. The most common metod I saw in different solutions was finding access unit delimiters and NAL of IDR type. Unfortunatelly most streams I checked didn't have NAL of IDR type. I'll be gratefull for help. Regards Jacek

1条回答
放荡不羁爱自由
2楼-- · 2019-04-11 10:10

H264 frames split up by a special tag, called the startcode prefix, which is either of 0x00 0x00 0x01 OR 0x00 0x00 0x00 0x01. All the data between two startcodes comprises a NAL unit in H264 speak. So what you want to do is search for the startcode prefix in your h264 stream. The byte following the startcode prefix immediately is the NAL header. The lowest 5 bits of the NAL header will give you the NAL unit type. If nal_unit_type = 5, that particular NAL unit is a reference frame.

Something like this:

void h264_find_IDR_frame(char *buf)
{
    while(1)
    {
        if (buf[0]==0x00 && buf[1]==0x00 && buf[2]==0x01)
        {
            // Found a NAL unit with 3-byte startcode
            if(buf[3] & 0x1F == 0x5)
            {
                // Found a reference frame, do something with it
            }
            break;
        }
        else if (buf[0]==0x00 && buf[1]==0x00 && buf[2]==0x00 && buf[3]==0x01)
        {
            // Found a NAL unit with 4-byte startcode
            if(buf[4] & 0x1F == 0x5)
            {
                // Found a reference frame, do something with it
            }
            break;
        }
        buf++;
    }
}
查看更多
登录 后发表回答