View Full Version : h.264 frame count calculation
pfvniuser
23rd March 2009, 18:44
I've have searched and have looked at some of the open source code but I am having a bit of trouble getting the number of frames in a h.264 transport stream. I can demux the h.264 out and parse it. I am currently using the count of the NAL units of non idr slice and idr slice 0x1 and 0x5 as this was something I saw used in some ones code. It doesn't come out right when compared with other tools though. Any ideas on the count of which nal units or which fields in which nal unit(s) to use to calulcate the number of frames in a video?
Thank you.
neuron2
23rd March 2009, 19:15
If your stream has AUDs then it is easy. Just count the AUD NALUs. By spec, transport streams are supposed to carry AUDs, but sometimes they do not.
If your stream doesn't have AUDs, then it is not easy. For that case see paragraph 7.4.1.2.4 here:
http://neuron2.net/library/avc/T-REC-H[1].264-200711-I!!PDF-E.pdf
"7.4.1.2.4 Detection of the first VCL NAL unit of a primary coded picture"
There is a lot of parsing to do. You can take the JM reference software as a base and adapt that to your purpose.
If you can trust the PTS timestamps in the stream, then you can get the first and last timestamps and calculate the number of frames from that. But it's not reliable because many streams have timestamp discontinuities. Counting pictures at the NAL layer is the most reliable approach. That's what I use in DGAVCIndex and VBV Checker.
pfvniuser
23rd March 2009, 20:06
Thank you. I have that spec but it is a little hard to read. I am looking at the JM reference software and I'll see what I can find. Not to be too needy but do you have any tips of the fields or nal units to look into? I am decoding nals 0x1 and 0x5 (idr pictures) and the stream doesn't seem to have any 0x2-0x4 nals. The count of the idrs is 254 but other tools are reporting 902 frames in the video.
neuron2
23rd March 2009, 21:22
Thank you. I have that spec but it is a little hard to read. I am looking at the JM reference software and I'll see what I can find. There is code that implements the spec paragraph I quoted above.
Not to be too needy but do you have any tips of the fields or nal units to look into? As described in the spec paragraph I mentioned above.
I am decoding nals 0x1 and 0x5 (idr pictures) and the stream doesn't seem to have any 0x2-0x4 nals. The count of the idrs is 254 but other tools are reporting 902 frames in the video. If you are sure that all pictures have only one slice and there is no data partitioning, then you can count slices to determine the number of pictures. The sum of the number of IDR slices (NALU type 5) *and* non-IDR slices (NALU type 1) should be equal to the number of pictures.
And don't forget that for field coded streams the number of frames will be one half the number of pictures.
pfvniuser
23rd March 2009, 22:28
If you are sure that all pictures have only one slice and there is no data partitioning.
And don't forget that for field coded streams the number of frames will be one half the number of pictures.
Thanks for the tip. I'll keep digging, this is a little beyond me at the moment.
pfvniuser
27th March 2009, 00:12
I think i have got it. How do you tell if it is a field coded stream though?
neuron2
27th March 2009, 02:38
Search for field_pic_flag in the spec and/or the JM reference software.
pfvniuser
27th March 2009, 18:58
Perfect. Thank you very much.
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.