Skip to main content
Version: v5.6.0

Improving MPEG-TS Compliance with FFmpeg

This guide explains how to improve MPEG-TS compliance when using FFmpeg with NETINT Quadra hardware encoders, focusing on PCR jitter reduction and bitrate stability.

Understanding MPEG-TS Parameters

Key Components

  1. Video Stream

    • Main content bitrate
    • Encoder settings for stability
    • Buffer management
  2. Audio Stream

    • Audio bitrate
    • Channel configuration
    • Audio buffer settings
  3. Overhead Components

    • PAT/PMT: Program Association Table/Program Map Table
    • SDT: Service Description Table
    • PCR: Program Clock Reference
    • SCTE35: Splicing information
    • Subtitles and Teletext

Calculating Required Bitrates

1. Measuring Your Content

Before setting any parameters, you need to determine the following information about your input stream:

  1. Video Stream Bitrate

    • Measure the actual video bitrate of your input stream
    • Account for any bitrate variations in your content
  2. Audio Stream Bitrate

    • Measure the audio bitrate for all audio tracks
    • Include all audio channels and streams
  3. Overhead Bitrate

    • Measure or estimate the overhead from PAT/PMT tables
    • Account for PCR packets and other MPEG-TS overhead
    • Include any SCTE35, subtitles, or teletext data
  4. PCR and PAT/PMT Intervals

    • Determine the PCR packet intervals in your stream
    • Note the PAT/PMT packet frequency
    • Ensure these intervals meet compliance requirements for your target standard
    • Note: Intervals vary by standard:
      • DVB: PCR ≤40ms, PAT/PMT ≤100ms per ETSI TR 101 211 guidelines
      • ATSC: Follows ISO/IEC 13818-1 (MPEG-2 TS), which allows PCR up to 100ms maximum (this is the maximum allowed interval, not a typical operational parameter)

Use analysis tools like FFprobe or stream analyzers to gather these details before proceeding with bitrate calculations.

2. Calculating Total Bitrate

Once you have your measurements:

  1. Base Calculation

    Total Muxrate = Measured Video + Measured Audio + Measured Overhead + Safety Margin
  2. Safety Margin

    • Add 5-10% for stability (depends on content complexity)
    • Increase margin for variable content or high-motion video
    • Consider network conditions

3. Libxcoder Parameter Configuration

For bitrate stability in CBR mode, use these recommended xcoder parameters:

-xcoder-params:v:0 "profile=2:gopPresetIdx=8:RcEnable=1:cuLevelRcEnable=1:intraPeriod=0:vbvBufferSize=800:lookaheadDepth=4:rcQpDeltaRange=15:ctbRowQpStep=400"

Key parameters explained:

Buffer Settings

  • vbvBufferSize=800: Size of the VBV buffer in milliseconds

    • Default: Varies by mode (3000ms in CBR, 0 in CRF)
    • Supported values: -1, 0, or [min_vbv_size ~ 3000ms]
    • min_vbv_size: Defined as 1000/framerate (one frame time) but not less than 10ms for vbvMaxRate 0 (disable), or any value >= bitrate
    • Must be specified with bitrate parameter
  • lookaheadDepth=4: Number of frames to look ahead

    • Default: 0 (disabled)
    • Range: 0 or 4-40 frames
    • Note: Reduces bitrate spikes but increases delay

Rate Control

  • RcEnable=1: Enables rate control (required for CBR mode)

  • cuLevelRcEnable=1: Coding unit level rate control

    • Default: 0 (disabled)
    • Note: If enabled with small intraPeriod (less than 30), may cause I-frame flickering
  • rcQpDeltaRange=15: Maximum QP delta range within a frame

    • Default: 10
    • Range: 0-15
    • Only effective if cuLevelRcEnable=1
  • ctbRowQpStep=400: Maximum QP adjustment per CTB row

    • Default: Codec-specific (4 for H.264, 16 for HEVC)
    • Range: 0-500
    • Only effective if cuLevelRcEnable=1
    • Helps reduce bitrate spikes

Best Practices

PCR Period

Use -pcr_period 20 for 20ms PCR intervals

Muxer Flags

  • Use -mpegts_flags +initial_discontinuity+system_b
  • Enable -mpegts_copyts for proper timestamp handling
  • Use -scte35_copyts for SCTE35 timing

Buffer Management

  • Set appropriate -max_delay
  • Enable -flush_packets 1 for tee muxer

Troubleshooting

Common Issues and Solutions

PCR Jitter

Often indicates bitrate instability

  • Solution: Enable bitrate stabilization parameters and null stuffing

DTS less than PCR Issues

Caused by bitrate spikes

  • Solutions:
    • Increase muxrate
    • Increase max_delay
    • Improve bitrate stability with encoder parameters

PAT/PMT Frequency

Must be sent at least every 100ms per DVB guidelines (ETSI TR 101 211 recommends ≤100ms for fast acquisition)

  • Note: PAT/PMT at 100ms is standard practice for DVB. SDT at ≤2s is a recommended practice (not a mandatory specification in ETSI EN 300 468)
  • Use appropriate period settings:
    • -sdt_period 0.25 (250ms for SDT, recommended practice)
    • -pat_period 0.1 (100ms for PAT/PMT)

Notes

  • These improvements may impact video quality or latency
  • Always test with your specific content and requirements
  • Monitor PCR jitter and bitrate stability after changes
  • Consider network conditions when setting UDP parameters

Example Configurations

1. Basic Configuration

This example shows a basic MPEG-TS encoding setup with standard parameters. Use this as a starting point for most use cases.

Note: The input UDP multicast address (udp://239.0.0.1:1234) is for testing purposes. Replace with your actual input source. Output UDP parameters should be tuned based on your network conditions.

ffmpeg -loglevel info -rw_timeout 5000000 -copyts -vsync vfr \
-analyzeduration 15000000 -thread_queue_size 2048 \
-timeout 5000000 -probesize 50000000 -fflags discardcorrupt+nobuffer \
-noautorotate -ec 0 -enable_sei_recovery 1 \
-i udp://239.0.0.1:1234?fifo_size=0 \
-filter_complex "[0:v]scale=1920x1080[v0]" \
-c:s copy -c:d copy -map [v0] -c:v:0 h264_ni_quadra_enc -b:v:0 4500K -r:v:0 25/1 \
-xcoder-params:v:0 "repeatHeaders=1:RcEnable=1:gopPresetIdx=8:intraPeriod=0:genHdrs=1:lookaheadDepth=4:EnableRdoQuant=1:rdoLevel=1" \
-f mpegts -mpegts_copyts=1:scte35_copyts=1:max_delay=1000000 \
-mpegts_flags +initial_discontinuity+system_b \
-mpegts_max_video_bitrate=9000K -mpegts_max_audio_bitrates=201K -mpegts_max_bitrate=9246K \
udp://127.0.0.1:1234?pkt_size=1316

2. With Bitrate Fluctuation Reduction

This example adds bitrate stabilization parameters to reduce PCR jitter and improve compliance:

  • vbvBufferSize=800: Controls VBV buffer size for peak constraint
  • cuLevelRcEnable=1: Enables coding unit level rate control for finer bitrate control
  • rcQpDeltaRange=15: Maximum QP delta range within a frame
  • ctbRowQpStep=400: Controls QP adjustment per CTB row to reduce bitrate spikes

Note: The input UDP multicast address (udp://239.0.0.1:1234) is for testing purposes. Replace with your actual input source.

Use this configuration when you experience PCR jitter or bitrate instability issues.

ffmpeg -loglevel info -rw_timeout 5000000 -copyts -vsync vfr \
-analyzeduration 15000000 -thread_queue_size 2048 \
-timeout 5000000 -probesize 50000000 -fflags discardcorrupt+nobuffer \
-noautorotate -ec 0 -enable_sei_recovery 1 \
-i udp://239.0.0.1:1234?fifo_size=0 \
-filter_complex "[0:v]scale=1920x1080[v0]" \
-c:s copy -c:d copy -map [v0] -c:v:0 h264_ni_quadra_enc -b:v:0 4500K -r:v:0 25/1 \
-xcoder-params:v:0 "repeatHeaders=1:RcEnable=1:gopPresetIdx=8:intraPeriod=0:genHdrs=1:lookaheadDepth=4:vbvBufferSize=800:cuLevelRcEnable=1:rcQpDeltaRange=15:ctbRowQpStep=400:EnableRdoQuant=1:rdoLevel=1" \
-f mpegts -mpegts_copyts=1:scte35_copyts=1:max_delay=1000000 \
-mpegts_flags +initial_discontinuity+system_b \
-mpegts_max_video_bitrate=9000K -mpegts_max_audio_bitrates=201K -mpegts_max_bitrate=9246K \
udp://127.0.0.1:1234?pkt_size=1316

3. With Null Stuffing

This example adds null stuffing to maintain constant bitrate and improve PCR accuracy:

  • muxrate=5171K: Total muxrate including null stuffing (200 Kbps overhead)
  • mpegts_max_bitrate=5171K: Maximum bitrate including null stuffing
  • mpegts_max_video_bitrate=4725K: Maximum video bitrate (reduced to allow for stuffing)
  • mpegts_max_audio_bitrates=201K: Maximum audio bitrate
  • sdt_period=0.25: SDT period in seconds (250ms, recommended practice)
  • pat_period=0.1: PAT period in seconds (100ms, recommended per DVB guidelines)
  • force_pat_pmt_key_off=1 and force_pcr_key_off=1: Ensures PAT/PMT and PCR are not scrambled

Note: Output UDP parameters (e.g., bitrate=5171000&buffer_size=212992&burst_bits=1000000&fifo_size=68763) are for network tuning. These values are examples and should be adjusted based on your network conditions and requirements.

Use this configuration when you need strict CBR compliance and improved PCR accuracy, especially for broadcast applications.

ffmpeg -loglevel info -rw_timeout 5000000 -copyts -vsync vfr \
-analyzeduration 15000000 -thread_queue_size 2048 \
-timeout 5000000 -probesize 50000000 -fflags discardcorrupt+nobuffer \
-noautorotate -ec 0 -enable_sei_recovery 1 \
-i udp://239.0.0.1:1234?fifo_size=0 \
-filter_complex "[0:v]scale=1920x1080[v0]" \
-c:s copy -c:d copy -map [v0] -c:v:0 h264_ni_quadra_enc -b:v:0 4500K -r:v:0 25/1 \
-xcoder-params:v:0 "repeatHeaders=1:RcEnable=1:gopPresetIdx=8:intraPeriod=0:genHdrs=1:lookaheadDepth=4:EnableRdoQuant=1:rdoLevel=1" \
-f mpegts -mpegts_copyts=1:scte35_copyts=1:max_delay=1000000 \
-mpegts_flags +initial_discontinuity+system_b \
-mpegts_max_video_bitrate=4725K -mpegts_max_audio_bitrates=201K \
-sdt_period 0.25 -pat_period 0.1 -force_pat_pmt_key_off=1 -force_pcr_key_off=1 \
-muxrate 5171K -mpegts_max_bitrate 5171K \
udp://127.0.0.1:1234?bitrate=5171000&buffer_size=212992&burst_bits=1000000&fifo_size=68763&pkt_size=1316

4. With Both Improvements

This example combines both bitrate fluctuation reduction and null stuffing for maximum MPEG-TS compliance:

  • Includes all bitrate stabilization parameters from Example 2
  • Includes all null stuffing and period settings from Example 3
  • Provides the best PCR accuracy and bitrate stability

Note: Output UDP parameters (e.g., bitrate=5171000&buffer_size=212992&burst_bits=1000000&fifo_size=68763) are for network tuning. These values are examples and should be adjusted based on your network conditions and requirements.

Use this configuration when you need the highest level of MPEG-TS compliance, such as for professional broadcast or distribution systems where PCR jitter must be minimized.

ffmpeg -loglevel info -rw_timeout 5000000 -copyts -vsync vfr \
-analyzeduration 15000000 -thread_queue_size 2048 \
-timeout 5000000 -probesize 50000000 -fflags discardcorrupt+nobuffer \
-noautorotate -ec 0 -enable_sei_recovery 1 \
-i udp://239.0.0.1:1234?fifo_size=0 \
-filter_complex "[0:v]scale=1920x1080[v0]" \
-c:s copy -c:d copy -map [v0] -c:v:0 h264_ni_quadra_enc -b:v:0 4500K -r:v:0 25/1 \
-xcoder-params:v:0 "repeatHeaders=1:RcEnable=1:gopPresetIdx=8:intraPeriod=0:genHdrs=1:lookaheadDepth=4:vbvBufferSize=800:cuLevelRcEnable=1:rcQpDeltaRange=15:ctbRowQpStep=400:EnableRdoQuant=1:rdoLevel=1" \
-f mpegts -mpegts_copyts=1:scte35_copyts=1:max_delay=1000000 \
-mpegts_flags +initial_discontinuity+system_b \
-mpegts_max_video_bitrate=4725K -mpegts_max_audio_bitrates=201K \
-sdt_period 0.25 -pat_period 0.1 -force_pat_pmt_key_off=1 -force_pcr_key_off=1 \
-muxrate 5171K -mpegts_max_bitrate 5171K \
udp://127.0.0.1:1234?bitrate=5171000&buffer_size=212992&burst_bits=1000000&fifo_size=68763&pkt_size=1316

Parameter Reference Table

ParameterDefaultRangeNotes
vbvBufferSizeVaries by mode-1, 0, or [min_vbv_size ~ 3000ms]min_vbv_size = 1000/framerate (≥10ms) or ≥ bitrate
lookaheadDepth00 or 4-40Reduces bitrate spikes but increases delay
cuLevelRcEnable00-1Finer control, potential flicker
rcQpDeltaRange100-150 disables CU RC
ctbRowQpStepCodec-specific0-500Reduces spikes

Key References