Posts: 76
Joined: Mon Nov 25, 2013 5:56 pm

Iframes h264

Tue Apr 14, 2015 5:47 am


I have a question to the mmal-interface: How is it possible to set the frequency of iframes in the h264 stream? In the documentation are some parameters like


and so on, but i can't find an explanation what they exactly do. My problem is that I have a circular buffer for the video and if this buffer is smaller than 3 seconds, it can happen that there is no iframe in the buffer anymore and hence the whole content useless.

Any help would be appreciated :)

Posts: 652
Joined: Wed Oct 02, 2013 12:28 pm

Re: Iframes h264

Tue Apr 14, 2015 8:18 am

When you enable your encoder(i.e., RaspiVid.c around line1525):

Code: Select all

MMAL_PARAMETER_UINT32_T param = {{ MMAL_PARAMETER_INTRAPERIOD, sizeof(param)}, state->intraperiod};
status = mmal_port_parameter_set(encoder_output, &param.hdr);
It is the number of frames after which a new I-frame is inserted in to the stream.

If you have 30fps and want each 3 seconds an I-frame a value of less the 90 should be ok. You can try to set it to 30 to get an I-frame every second.

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

Re: Iframes h264

Tue Apr 14, 2015 10:08 am

If you want to make life easier for yourself, then also set MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER to MMAL_TRUE to get a duplicate of the H264 header bytes before each IDR frame. The decoder has to receive these before any frames, so otherwise you'd have to store a copy of them from the start of the stream, and repeat them into every time you actually save out from your buffer.
The circular buffer mode from Raspivid should give you big pointers on how to split the stream up easily in the way you imply you want to.
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.

Posts: 76
Joined: Mon Nov 25, 2013 5:56 pm

Re: Iframes h264

Wed Apr 15, 2015 4:42 am

Thanks a lot, I'm going to try this. PS: I'm using your code from the PrePost-Branch for the buffer, nice work, thanks a lot. The problem is that I get a lot of iframe-errors if the buffer is too small, because the read-pointer doesn't move forward because there is no iframe left and the new buffer content overwrites the last iframe.

@6by9: This sounds clever, at the moment I'm really saving the header bytes for all further videos.

Return to “Camera board”