H264 reference frames

I am looking for an algorithm to search for reference frames in a h264 stream. The most common method that I saw in different solutions was to search for access block separators and NALs of type IDR. Unfortunately, most of the streams I checked do not have NAL type IDR. I will be grateful for the help. Regards Jacek

+3
source share
1 answer

H264 frames are separated by a special tag called the initial code prefix, which has the value 0x00 0x00 0x01 OR 0x00 0x00 0x00 0x01 . All data between the two start codes includes a NAL block in H264. So what you want to do is search for the start code prefix in the h264 stream. The byte following the initial code prefix is ​​the NAL header . The lowest 5 bits of the NAL header will give you the type of NAL unit. If nal_unit_type = 5, then this NAL block is the reference frame.

Something like that:

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++;
    }
}
+7
source

Source: https://habr.com/ru/post/1760272/


All Articles