开发者

how can I distinguish H264 encoded Video frames by some special tag?

开发者 https://www.devze.com 2023-03-12 20:43 出处:网络
I have H264 encoded Video file came from Android mobile camera and I want to get the frames and store them as files one by one. The problem is, how can I distinguish the frames, do the frames split up

I have H264 encoded Video file came from Android mobile camera and I want to get the frames and store them as files one by one. The problem is, how can I distinguish the frames, do the frames split up by some special tag? Now I have this function which can get the frames length by bytes, maybe it helps to understand my questions,Thx:)

  public sta开发者_开发技巧tic int h263Parse(byte[]buf, int offset, int nLen)
{
            int vop_found, i;          
    vop_found = 0;        
    i=0;
    if(vop_found == 0)
        {
        for(i=(offset + 3); i<(offset+nLen); i++)
                {
            if(buf[i-3] == 0x00)
                    if(buf[i-2] ==  0x00)
                            if((buf[i-1]&0xff) < 0x84)
                                    if((buf[i-1]&0xff) >= 0x80)
                        {
                i++;
                vop_found=1;
                break;
            }
        }
    }

    if(vop_found == 1)
        {
        for(; i<(offset+nLen); i++)
                {
                if(buf[i-3] == 0x00)
                    if(buf[i-2] ==  0x00)
                            if((buf[i-1]&0xff) < 0x84)
                                    if((buf[i-1]&0xff) >= 0x80)
                        {
                return i-3-offset;
            }
        }
    }
    return -1;
}


I seriously don't know what your code is doing (because it is named h263parse) and you are asking about h264.

Anyways, H264 frames do 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 perhaps what you want to do is search for the startcode prefix in your h264 stream and do something with the NAL unit that follows (until the next startcode prefix) in the stream.

Something like this perhaps:

void h264parse_and_process(char *buf)
{
    while(1)
    {
        if (buf[0]==0x00 && buf[1]==0x00 && buf[2]==0x01)
        {
            // Found a NAL unit with 3-byte startcode, do something with it
            do_something(buf); // May be write to a file
            break;
        }
        else if (buf[0]==0x00 && buf[1]==0x00 && buf[2]==0x00 && buf[3]==0x01)
        {
            // Found a NAL unit with 4-byte startcode, do something with it
            do_something(buf); // May be write to a file
            break;
        }
        buf++;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消