H.264 streams with multiple PPS are undecodable because MediaMTX keeps only one PPS
Which version are you using?
v1.21.1 (also reproduced in production on v1.15.1)
Which operating system are you using?
Linux amd64 Docker
Describe the issue
Which version are you using?
v1.21.1 (also reproduced in production on v1.15.1)
Which operating system are you using?
Linux arm64 Docker (the bluenviron/mediamtx:1.21.1 image on an Apple-silicon host). The cause is platform-independent.
Describe the issue
When a publisher's H.264 stream uses more than one PPS, MediaMTX keeps only one of them and drops the rest. If the PPS the slices actually reference is not the one kept, every frame becomes undecodable for readers and recordings (non-existing PPS N referenced, 100% decode failure).
Cause, as far as I can tell:
internal/stream/format_updater.gostores a singleSPS/PPSpair onformat.H264. Every PPS NALU that differs from the stored one overwrites it, so the last PPS seen wins regardless of itspic_parameter_set_id.internal/stream/unit_remuxer.gostrips all in-band SPS/PPS NALUs from each access unit and prepends only the stored pair before IDRs.
So a stream carrying e.g. PPS 0 and PPS 1 per IDR, with slices referencing PPS 0, comes out carrying only PPS 1. Multiple PPS are valid H.264 (the spec allows up to 256 PPS ids), and some hardware encoders emit them. We hit this in production with an encoder that sends PPS 2 and PPS 3 and references PPS 2 from every slice. Every recorded segment contained only PPS 3.
Expected: parameter sets are tracked by id (or in-band PPS NALUs whose id differs from the stored one are passed through), so every PPS the slices need reaches readers and recordings.
Describe how to replicate the issue
A synthetic stream: plain x264 output with a second PPS (a copy of PPS 0 re-labelled as id 1) inserted after PPS 0. Slices still reference PPS 0. The input decodes cleanly with ffmpeg on its own.
Generate the input:
ffmpeg -f lavfi -i testsrc2=size=640x360:rate=30 -t 20 -c:v libx264 -profile:v high \ -g 30 -bf 0 -x264-params repeat-headers=1 -f h264 base.h264 python3 inject_pps.py base.h264 two_pps.h264 ffmpeg -v error -i two_pps.h264 -f null - # no output: decodes cleanlyinject_pps.py# Inserts a copy of PPS 0 re-labelled as PPS 1 after every PPS 0 NALU. # Slices still reference PPS 0, so the output is a valid H.264 stream with two PPS. import re, sys def unescape(b): return re.sub(rb'\x00\x00\x03', b'\x00\x00', b) def escape(b): return re.sub(rb'\x00\x00(?=[\x00-\x03])', b'\x00\x00\x03', b) def relabel_pps(nalu): bits = ''.join(f'{x:08b}' for x in unescape(nalu[1:])) assert bits[0] == '1', 'expected pic_parameter_set_id = 0' payload = '010' + bits[1:].rstrip('0')[:-1] # ue(1) = '010'; drop old rbsp stop bit + padding payload += '1' payload += '0' * (-len(payload) % 8) return nalu[:1] + escape(bytes(int(payload[i:i+8], 2) for i in range(0, len(payload), 8))) data = open(sys.argv[1], 'rb').read() parts = re.split(rb'(\x00\x00\x00\x01|\x00\x00\x01)', data) out = bytearray(parts[0]) for i in range(1, len(parts), 2): sc, nalu = parts[i], parts[i + 1] out += sc + nalu if nalu and nalu[0] & 0x1f == 8: out += b'\x00\x00\x00\x01' + relabel_pps(nalu.rstrip(b'\x00')) open(sys.argv[2], 'wb').write(out)Start MediaMTX with the configuration below:
docker run --rm -p 8554:8554 -v $PWD/mediamtx.yml:/mediamtx.yml \ -v $PWD/recordings:/recordings bluenviron/mediamtx:1.21.1Publish:
ffmpeg -re -f h264 -framerate 30 -i two_pps.h264 -c copy -f rtsp -rtsp_transport tcp rtsp://localhost:8554/testRead over RTSP (while publishing). Every frame fails:
ffmpeg -v error -rtsp_transport tcp -i rtsp://localhost:8554/test -t 5 -f null -[h264 @ ...] non-existing PPS 0 referenced [h264 @ ...] no frame! ... Decoding error: Invalid data found when processing inputInspect a recorded segment. Only PPS 1 is present, and nothing decodes:
ffmpeg -i recordings/test/<segment>.ts -c copy -bsf:v trace_headers -f null - 2>&1 | grep pic_parameter_set_id8 pic_parameter_set_id 010 = 1ffmpeg -v error -i recordings/test/<segment>.ts -f null -514 x [h264 @ ...] non-existing PPS 0 referenced 360 x [h264 @ ...] no frame! 180 x ... Decoding error: Invalid data found when processing input
The same input published directly (bypassing MediaMTX) decodes without errors.
MediaMTX configuration
logLevel: info
pathDefaults:
record: yes
recordPath: /recordings/%path/%Y-%m-%d_%H-%M-%S-%f
recordFormat: mpegts
recordSegmentDuration: 5s
paths:
all_others:MediaMTX logs
INF [RTSP] [session 19a7d672] created by 192.168.65.1:22364
INF [path test] [recorder] recording 1 track (H264)
INF [path test] stream is available and online, 1 track (H264)
INF [RTSP] [session 19a7d672] is publishing to path 'test'
INF [path test] RTP packets are too big (1460 > 1440), remuxing them into smaller ones
INF [path test] [recorder] recording stopped
INF [RTSP] [conn 192.168.65.1:22364] closed: EOF
INF [RTSP] [session 19a7d672] destroyed: torn down by 192.168.65.1:22364No errors or warnings are logged; the PPS is dropped silently.
Describe how to replicate the issue
- start the MediaMTX
- publish with ...
- read with ...
MediaMTX configuration
logLevel: info
pathDefaults:
record: yes
recordPath: /recordings/%path/%Y-%m-%d_%H-%M-%S-%f
recordFormat: mpegts
recordSegmentDuration: 5s
paths:
all_others:MediaMTX logs
INF [RTSP] [session 19a7d672] created by 192.168.65.1:22364 INF [path test] [recorder] recording 1 track (H264) INF [path test] stream is available and online, 1 track (H264) INF [RTSP] [session 19a7d672] is publishing to path 'test' INF [path test] RTP packets are too big (1460 > 1440), remuxing them into smaller ones INF [path test] [recorder] recording stopped INF [RTSP] [conn 192.168.65.1:22364] closed: EOF INF [RTSP] [session 19a7d672] destroyed: torn down by 192.168.65.1:22364
Packet dump
No response
Source: bluenviron/mediamtx