MPV – Video playback, tearing? A possible solution!

      Kommentare deaktiviert für MPV – Video playback, tearing? A possible solution!

For some time I was annoyed with tearing problems while playing videos. You know this issue, do you? In scenes with horizontal movement it seems that the screen is split and updated differently. you see faces horizontally cut in half or scenery jittering by. Annoying.

The problem is old and has been discussed many times. The basic issues is a timing issue between the decoder updating / drawing the frame and the graphics subsystem copying it to the actual framebuffer or graphics memory. If the copy happens during a screen redraw, which usually happens at ~50Hz (or more) then it may happen that half the frame has been drawn to screen and suddenly the decoder kicks in and redraws the frame while the second half is painted.

The graphics system needs to have a notification system so that programs, like mpv or other video players, can get notified when the screen update is finished and the next one starts. This is quite a long timespan, more than enough to copy a frame. Such mechanisms are in place for quite some time in the Linux GUI stacks. But the stacks evolved and today we have a pretty ugly mixture of legacy code and modern GUI stuff, best known as X11 and OpenGL. X11 was designed for simple 2D framebuffers. Even 2D hardware acceleration was already difficult to implement. But with modern 3D GPUs it really became a mess. This is why new stacks like Wayland exist.

Back to the problem, video tearing. Many current desktop environment use a technique called “compositing” to create a nicer look and feel with various window effects, graphics transitions and opacity / transparency. Normal 2D acceleration can not do that but 3D GPUs can do that at almost no cost – so why not have some fun and use it? As far as I understood it the current why to do this is to create OpenGL surfaces and each X11 window then becomes such an OpenGL surface to which effects can be applied. Nice!

But this has a big drawback. The GPUs work pretty much on their own and in the way GPUs are integrated into X11 this breaks video playback synchronization which again breaks tear free video playback.

I am using XFCE4 for quite some time and also for quite some time had “compositing” enabled in the window manager settings. But it only occurred lately that this was the root cause for my video playback issues! After disabling compositing my videos, also full-hd, are as smooth as on any other TV set! Excellent!

As a nice add-on, I am using i915 graphics, using this commandline to play video reduces CPU load to ~10-20% using full hardware video decoding:

mpv -vo vaapi --hwdec=vaapi <videofile>