jwatte
Posts: 203
Joined: Sat Aug 13, 2011 7:28 pm

Timecode in the h264 encoder?

Mon Apr 04, 2016 2:00 am

I'm recording on a Raspberry Pi 2 using a cut-down version of RaspiVid, and playing back on a Windows machine after transferring over the network (using scp when done -- not streaming.)

When I play back a h264 file that I have created on Raspberry Pi (using a Pi2, with Jesse) on VLC on Windows (latest version) the VLC time bar shows 00:00 on both the left and right sides. I can't get it to show how far into the movie it is currently displaying.
Is there something that should be in the H264 file to tell players like VLC where in time they are? And, if so, what can I do at the MMAL API level to insert such information?

Btw: These recordings typically aren't cleanly terminated; the signal to stop is "power was lost."

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26659
Joined: Sat Jul 30, 2011 7:41 pm

Re: Timecode in the h264 encoder?

Mon Apr 04, 2016 8:19 am

H264 bitstreams do not contain frame rate or timecode information. That stuff is added by putting the bitstream in a 'container' such as MP4 or MKV. You can use avconv to do this. Google will find out the mechanism.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8918
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Timecode in the h264 encoder?

Mon Apr 04, 2016 8:25 am

Better to ask these questions in the Camera subforum - it's been answered there multiple times.

Raspivid is saving an H264 elementary stream, and those have no timestamps as timestamps are generally stored in a container (MP4. AVI, MOV, MKV, etc) instead.
Use avconv or equivalent to multiplex the raw stream into a container. Something like:

Code: Select all

avconv -framerate 30-i my_video.h264 -c:v copy my_video2.mp4
(Just beaten to it by James!)
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26659
Joined: Sat Jul 30, 2011 7:41 pm

Re: Timecode in the h264 encoder?

Mon Apr 04, 2016 8:34 am

NINJA'D!!!!
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

drgeoff
Posts: 10765
Joined: Wed Jan 25, 2012 6:39 pm

Re: Timecode in the h264 encoder?

Mon Apr 04, 2016 4:30 pm

jamesh wrote:H264 bitstreams do not contain frame rate or timecode information..
Not completely true.

They can contain frame rate info. If timing_info_present_flag is 1.

Eg.

Code: Select all

timing_info_present_flag : 1
  num_units_in_tick : 100
  time_scale : 5994
  fixed_frame_rate_flag : 1

jwatte
Posts: 203
Joined: Sat Aug 13, 2011 7:28 pm

Re: Timecode in the h264 encoder?

Mon Apr 04, 2016 6:23 pm

Thanks for the pointers!

I'll go spelunking and see if it's possible to write the timecode/format while writing the raw stream, or whether I need to do this offline after the fact.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26659
Joined: Sat Jul 30, 2011 7:41 pm

Re: Timecode in the h264 encoder?

Tue Apr 05, 2016 8:44 am

jwatte wrote:Thanks for the pointers!

I'll go spelunking and see if it's possible to write the timecode/format while writing the raw stream, or whether I need to do this offline after the fact.
Since I've never heard of the above, I think it's unlikely that it is supported. Will double check with one of the guys who worked on the encoder, he's an H264 expert. If he comes in to work!
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8918
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Timecode in the h264 encoder?

Tue Apr 05, 2016 9:17 am

drgeoff wrote:
jamesh wrote:H264 bitstreams do not contain frame rate or timecode information..
Not completely true.

They can contain frame rate info. If timing_info_present_flag is 1.

Eg.

Code: Select all

timing_info_present_flag : 1
  num_units_in_tick : 100
  time_scale : 5994
  fixed_frame_rate_flag : 1
https://github.com/raspberrypi/firmware/issues/245
MMAL_PARAMETER_VIDEO_ENCODE_SPS_TIMING / OMX_IndexParamBrcmVideoAVCSPSTimingEnable exist and should then include the timing info in the headers. It's not enabled by default as I don't know the full implications of it, and there are too many Pi's out there to potentially break with a change like that.

NB That is only a nominal frame rate, not timestamps.
jamesh wrote:Will double check with one of the guys who worked on the encoder, he's an H264 expert.
(Now trying to remember which of the codec guys is at your place. I lost my tame codec expert a good while back, and the Pi Towers one isn't around.)
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

jwatte
Posts: 203
Joined: Sat Aug 13, 2011 7:28 pm

Re: Timecode in the h264 encoder?

Tue Apr 05, 2016 4:52 pm

Meanwhile, I've spelunked a little more, and the libavformat library comes with a sample program that reads a H.264 file in and writes a MP4 file out.
That program can be hacked-at and bashed into the RaspiVid-mini I'm using until it uses that library to create and write the file, rather than just fwrite().
It's one more copy step of data, but the data rate is kind-of low (the point of compression!) so that'll probably be OK.

The one possible restriction would be that the buffers received from the codec need to be "full frames" or something akin to it.
Is that what I receive from the codec? What is each buffer? Does it ever bundle multiple frames into a single buffer?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8918
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Timecode in the h264 encoder?

Tue Apr 05, 2016 5:25 pm

jwatte wrote:The one possible restriction would be that the buffers received from the codec need to be "full frames" or something akin to it.
Is that what I receive from the codec? What is each buffer? Does it ever bundle multiple frames into a single buffer?
You'll never get more than one frame encoded into one buffer.

There are situations where a single frame could be split into multiple buffers.
One is at when the codec wraps around the internal FIFO (2MB IIRC), but can be avoided with MMAL_PARAMETER_MINIMISE_FRAGMENTATION.
The second is if the buffer isn't large enough to store an entire frame. So I can predict the next question of "how big then". There isn't a simple answer. The codec sets a QP value for the frame based on past history, and if that is way off due to a scene change happening at that point, then the output frame is big. Based on previous experience, allow 768kB for 1080P, 384kB for <=720P. (This has been discussed previously on the Camera subforum).
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

jwatte
Posts: 203
Joined: Sat Aug 13, 2011 7:28 pm

Re: Timecode in the h264 encoder?

Tue Apr 05, 2016 5:40 pm

That's good information, too!
This has been discussed previously on the Camera subforum
I find that when I try to search the forum, I get a whole lot of talk about how to use motion/raspicam/whatever built-in tools, and very little about the underlying code and API to access it.

In the best of worlds, there would be an API section about MMAL with a "concepts" discussion, a "howto" section, and a reference for each of the functions and each of the constants/parameters.

And ponies would have wings ;-)

Return to “Advanced users”