Skip to content

Creating perfectly stable CBR MPEG transport stream

Jerzy Jaśkiewicz edited this page Feb 7, 2019 · 3 revisions

FFMpeg (w32threads vs. pthreads) and UDP stream input/output

1. INTRODUCTION

Current FFMpeg binaries for MS Windows platforms are compiled with w32threads support which is said to be a bit faster and more stable then pthreads. Therefore CaspareCG uses ffmpeg with w32threads enabled by default.

However, when it comes to UDP streaming input/output, FFMpeg with pthreads supports some additional UDP output parameters (e.g. bitrate, burst_bits, fifo_size, buffer_size) which are crucial for stable CBR input/output and for creating some specific DVB-T compatible UDP streams. FFMpeg with pthreads enabled uses an additional thread for capturing and sending UDP packets, avoiding spikes, bursts, packet loses and other inconsistencies.

We prepared both "w32threads" and "pthreads" enabled FFMpeg compilations for our fork of CasparCG which can be simply switched from one to another by just replacing some files. Current stable compiled version of FFMpeg inside our for of CasparCG is 4.0.2. The builds are available here.

2. INSTALLATION

  • stop any running CasparCG instances
  • copy all files from downloaded ffmpeg-pthreads/ffmpeg-w32threads folder and replace/overwrite original files inside CasparCG root folder
  • restart CasparCG

3. USAGE EXAMPLES

The "muxrate" parameter is crucial for setting up proper CBR stream with PCR intervals (20ns to 40ns) as required by most hardware muxers and DVB-T set-top boxes. Usually "muxrate" = (video bitrate + audio bitrate) + cca. 8% muxing overhead.

Special UDP parameter "bitrate" is passed as an additional UDP output option and it should be exactly the same as "muxrate". In case of random memory overflows, packet loses or other encoding errors, use additional parameters (e.g. burst_bits, buffer_size, fifo_size) as per ffmpeg UDP streaming instructions at: https://www.ffmpeg.org/ffmpeg-all.html#udp.

a) SD Stream specifications

  • mpeg2 video: 6000k CBR, 25fps, GOP 50, PCR 30ns, video pid 200
  • mp2 audio: 192k CBR, audio pid 300
  • UDP mpegts multicast output: 6450k CBR to IP 239.1.1.1 at port 10000

FFMpeg with w32threads enabled (lots of spikes, constant packet-bursts, not real CBR)

<stream>
 <path>udp://239.1.1.1:10000?pkt_size=1316</path>
 <vcodec>mpeg2video</vcodec>
 <acodec>mp2</acodec>
 <narrow>false</narrow>
 <options>g=50,minrate=6000k,maxrate=6000k,bufsize=645k,muxrate=6450k,pcr_period=30</options>
 <output-metadata>service_provider="IPTV Provider",service_name="TV Channel"</output-metadata>
 <vrate>6000</vrate>
 <arate>192</arate>
 <video_stream_id>200</video_stream_id>
 <audio_stream_id>300</audio_stream_id>
 <pixel-format>yuv420p</pixel-format>
</stream>

FFMpeg with pthreads enabled (perfect DVB-T compatible CBR output)

<stream>
 <path>udp://239.1.1.1:10000?pkt_size=1316&bitrate=6450000</path>
 <vcodec>mpeg2video</vcodec>
 <acodec>mp2</acodec>
 <narrow>false</narrow>
 <options>g=50,minrate=6000k,maxrate=6000k,bufsize=645k,muxrate=6450k,pcr_period=30</options>
 <output-metadata>service_provider="IPTV Provider",service_name="TV Channel"</output-metadata>
 <vrate>6000</vrate>
 <arate>192</arate>
 <video_stream_id>200</video_stream_id>
 <audio_stream_id>300</audio_stream_id>
 <pixel-format>yuv420p</pixel-format>
</stream>

b) HD Stream specifications

  • libx264/h.264 video: 8000k CBR, 25fps, GOP 50, PCR 30ns, video pid 200
  • aac audio: 192k CBR, audio pid 300
  • UDP mpegts multicast output: 8450k CBR to IP 239.1.1.1 at port 10000

FFMpeg with w32threads enabled (lots of spikes, constant packet-bursts, not real CBR)

<stream>
 <path>udp://239.1.1.1:10000?pkt_size=1316</path>
 <vcodec>libx264</vcodec>
 <acodec>aac</acodec>
 <narrow>false</narrow>
 <options>g=50,x264opts=nal-hrd=cbr,minrate=8000k,maxrate=8000k,bufsize=900k,muxrate=8450k,pcr_period=30</options>
 <output-metadata>service_provider="IPTV Provider",service_name="TV Channel"</output-metadata>
 <vrate>8000</vrate>
 <arate>192</arate>
 <pixel-format>yuv420p</pixel-format>
 <video_stream_id>200</video_stream_id>
 <audio_stream_id>300</audio_stream_id>
 <audio-metadata>language=en</audio-metadata>
</stream>

FFMpeg with pthreads enabled (perfect DVB-T compatible CBR output)

<stream>
 <path>udp://239.1.1.1:10000?pkt_size=1316&bitrate=8450000</path>
 <vcodec>libx264</vcodec>
 <acodec>aac</acodec>
 <narrow>false</narrow>
 <options>g=50,x264opts=nal-hrd=cbr,minrate=8000k,maxrate=8000k,bufsize=900k,muxrate=8450k,pcr_period=30</options>
 <output-metadata>service_provider="IPTV Provider",service_name="TV Channel"</output-metadata>
 <vrate>8000</vrate>
 <arate>192</arate>
 <pixel-format>yuv420p</pixel-format>
 <video_stream_id>200</video_stream_id>
 <audio_stream_id>300</audio_stream_id>
 <audio-metadata>language=en</audio-metadata>
</stream>