CasparCG vs. FFMPEG - libx264 PCR Interval differences!

Hello all,

I’m using CasparCG version 2.0.6 with TVPlay (Releases · jaskie/Server · GitHub). Current ffmpeg shared libraries version is 4.0. I’m using same ffmpeg version for testing. I put together the following ffmpeg command:

ffmpeg -re -i 1.mp4 -c:v libx264 -x264opts nal-hrd=cbr -b:v 6000k -minrate 6000k -maxrate 6000k -bufsize 645k -muxrate 6450k -pcr_period 30 -c:a aac -b:a 192k -ac 2 -ar 48000 -f mpegts “udp://239.1.1.1:10000?pkt_size=1316”

This command above produces perfect CBR (libx264, aac) UDP MpegTS stream with perfect PCR interval around 30ms.


I parsed the ffmpeg parameters above to CasparCG and created the following config:

CasparCG options
x264opts=nal-hrd=cbr,minrate=6000k,maxrate=6000k,muxrate=6450k,bufsize=645k,pcr_period=30

The CasparCG created stream is without any errors, but PCR-interval jumps from 1ms to 30ms all the time.

What could be the reason for this situation? Is there maybe any special ffmpeg parameter which is set by default with ffmpeg, but CasparCG ignores it for some reason or isn’t set at all? Is it possible there’s something in the source code?

The erratic PCR-interval only happens with libx264 video codec, but if I use mpeg2video codec it works perfectly ok. I pasted the screenshots with ffmpeg and CasparCG configurations for comparison.

https://imgur.com/a/zFx8hzw

Thanks in advance for any help.

FYI, that version of CasparCG uses ffmpeg 2.8. CasparCG does not use system libraries.

@mint This isn’t true. The version I’m using (please check the link in the first post) uses latest ffmpeg 4.0 - 32bit shared libraries.

However, I have found the issue with that CasparCG version. It seems like gop size or keyint are somehow fixed at very low values (0 or 1). Same result happens if I enter “-x264opts keyint=1” or “g=1” to direct ffmpeg command. I tried to parse the parameters to CasparCG (keyint=25, g=25), but it seems the keyint parameter is ignored for some reason.
I also checked the source code at: https://github.com/jaskie/Server/blob/master/modules/ffmpeg/consumer/ffmpeg_consumer.cpp

I’m not 100% sure, but is it possible the line 387 (c->max_b_frames = 0;) is the issue? It sets fixed b-frames value to zero, so only I-frames are used and that is actually the same as setting “keyint=1” (only use I-frames)? Maybe some other parameter is the issue. I’m not a good programmer, but however, I’ll post the ffmpeg command that produces the same wrong PCR stream, so if someone can check that ffmpeg command and compare it against the ffmpeg-consumer cpp code, I’d be really thankful.

ffmpeg -re -i 1.mp4 -c:v libx264 -x264opts nal-hrd=cbr:keyint=1 -b:v 6000k -minrate 6000k -maxrate 6000k -bufsize 645k -muxrate 6450k -pcr_period 30 -c:a aac -b:a 192k -ac 2 -ar 48000 -f mpegts “udp://239.1.1.1:10000?pkt_size=1316”

The code above produces the exact same veriable PCR-interval as does CasparCG v2.6 I’m using above. The same happens if I set “-g 1” to ffmpeg command.

Thanks in advance, regards.

Interesting, I wasn’t aware that Jaskie’s version was so far off from the original repository. It’s a shame he hasn’t tried to merge this back into the main repositories.

Oh well, I’m afraid I don’t have much to add this otherwise.

That small GOP and no b-frames is probably there so file recordings are frame accurate when seeked.

This seems quite reasonable explanation for these two parameters. But still, is it possible that setting b-frames to 0 means that there are only I-frames and therefore keyint is froced to keyint=1 as well?

As said, I tried to enter keyint=1 to ffmpeg’s x264opts and the PCR-Interval is exactly the same as produced by CasparCG in that case. However, if I enter higher keyint value to CasparCG config, it is ignored for some reason. Is there maybe something in the code that need to be fixed so I’d be able to set keyint to some reasonable higher then 1 values?

Regards!