[Libav-user] UDP Stream Read Pixelation/Macroblock Corruption

Camera Man i.like.privacy.too at gmail.com
Tue Jun 26 11:18:42 CEST 2012

On 06/26/2012 04:02 AM, Alex Cohn wrote:
> The demonstrated effect could easily be explained by lost packets.
> With a UDP stream, some packets can be lost. You could try to improve
> the situation on the receiver side by making your socket buffers
> larger, but the problem may be out of your control. For example, with
> WiFi such behavior, especially with high bitrates, is quite typical.

For large frames (even as small as 1280x720), the way libavcodec works 
might be the culprit: It sets the UDP socket buffer size to 64K by 
default (which IIRC linux interprets to mean 128K, still small; not sure 
about other operating systems). If you give the UDP url yourself, you 
can add a ?buffer_size= parameter to fix this. However, if the URL is 
constructed by e.g. RTSP, there is no way to specify a buffer size. On 
the cameras I use, an I-frame at 1280x720 of a typical scene is often 
300K and almost always >200K - so packet loss is essentially guaranteed 
for almost every I-frame.

If the original poster is using rtsp, they might need to switch to 
rtsp/tcp until this is fixed, even if there is no UDP packet loss at the 
network level.

I pointed this out in 
<http://ffmpeg.org/pipermail/libav-user/2012-February/001294.html>; I've 
just noticed it was sent as HTML for some reason, so I'm copying again 
here in case this is relevant and hard to find or was impossible to read 
back then:


If you look at udp.c (just git pulled), routine udp_open actually parses 
the URI looking for libav-specific instructions such as "reuse", "ttl", 
"localport", "pkt_size", "buffer_size", "connect" and "localaddr". 
Specifically, the "buffer_size" parameter (which defaults to 
UDP_MAX_PKT_SIZE=65536 for reading) is used to set the UDP buffer size. 
I need to increase that buffer size significantly (to > 256KB) so that 
packets don't get lost., because I-frames at 1280x720 are already 
220KB/sec. I can recompile replacing the default buffer_size from 
UDP_MAX_PKT_SIZE to (e.g.) 4*UDP_MAX_PKT_SIZE, but I'd rather do that 
with configuration and use a stock libav.

So, the question is: given that I'm passing an rtsp url 
("rtsp://a.b.c.d/axis-media/media.amp"), how do I set buffer_size and/or 
ttl? There does not appear to be an avoption based way to do that, but 
maybe I'm missing something?

(This is also relevant to setting the timeout for tcp transports, and I 
guess; although the http transport does seem to get its parameters from 
avoptions; and rtsp does pass "localport", "ttl" and "connect" parameter 
to udp somehow)

More information about the Libav-user mailing list