Search code examples
videoencodingdirectshowh.264directshow.net

Setting Keyframe interval on DirectShow Encoder


I have seen some strange behavior when seeking during playback on a video, I have created the video using DirectShow - the issue is on Windows 7 Only (W8/W10 works fine).

The graph is built like this:

Source(Webcam) -> H264 Encoder -> MP4 Mux -> File Writer

So I am using a H264 Encoder, however for some reason the Encoder removes all Keyframes(Sync) after the encoder Filter. Using another older H264 Encoder I can see that it sets a Keyframe(Sync) each 20 frames perfectly, and the output File from this encoder works flawlessly on all operating systems.

So I suspect the issue lies with the encoder not setting Keyframes correctly and therefor won't be able to seek correctly (however Windows 8 and 10 can figure out how to seek on the file without Keyframes correctly, just not Windows 7)

Using VLC/WMP on Windows 7 when seeking on the file the file will act strange (No video for a long while, gray pixelated video for over 10 seconds, suddenly stop of video..)

So my question is, how do I set the interval of the keyframe on an Encoding Filter in DirectShow?

Edit: Another interesting thing I found is that when I seek, this weird output is seen via the Analyzer Filter (inserted just after the decoder) during playback. enter image description here


Solution

  • Decision on insertion of a key frame is encoder's. It might have a setting that define GOP length and minimal frequency of key frames. It might additionally insert a key frame on scene change. It might additionally accept external requests to insert at specific point.

    Generic approach is IAMVideoCompression::put_KeyFrameRate. In the same time I would say it's encoder specific. You should check documentation for specific encoder you are using to find out how to do it.