[x264-devel] Fixed GOP, keyint and keyint-min

Lars Kiesow lkiesow at uos.de
Wed Aug 23 21:08:14 CEST 2017


Hi everyone,
today we had some discussion about forcing a fixed GOP size using x264
with scenecut enabled to get additional I-frames. E.g. having an IDR
frame exactly every 50 frames with some additional non-IDR I-frames in
between if scenecut detects that having one would make sense.

My initial thought was was that this should be no problem, setting
keyint=keyint-min=50 and scenecut=40 (default).

While this would sometimes produce non-IDR I-frames, I was surprised to
see that the GOP was not fixed in the resulting video. That seemed
weird.

Looking at the x264 source code I found that first keyint may be
affected by keyint-min which kind of makes sense since keyint-min
should not be greater than keyint-max:

   common/common.c:733:

   p->i_keyint_min = atoi(value);
   if( p->i_keyint_max < p->i_keyint_min )
     p->i_keyint_max = p->i_keyint_min

Still, that would only modify keyint-max and only if min was actually
greater than max. But later I found that keyint-min was also modified to
enforce 1 <= keyint-min <= keyint-max/2+1 which I find unexpected:

   encoder/encoder.c:1017:

   h->param.i_keyint_min = x264_clip3( h->param.i_keyint_min, 1, h->param.i_keyint_max/2+1 );


Some question I find unable to answer myself:

- Is there any particular (undocumented?) reason to limit the keyint-min
  in this way? Is this a bug?
- Why first modify keyint-max if keyint-min is too large when keyint-min
  is modified later anyway?
- How would  someone archive a fixed GOP size using x264?


I found that using some FFmpeg options worked. Hence I assume there is
a magical way to do it:

 ffmpeg ... -force_key_frames 'expr:eq(mod(n,50),0)' -c:v libx264
   -keyint_min 50 ...

Note that keyint_min is smaller than 250(keyint default)/2+1 and will
hence not be modified. If keyint is set to a value too low, it will not
work.

Thanks in advance for any answer to this.

Best regards,
Lars


More information about the x264-devel mailing list