Xpra: Ticket #973: libyuv colourspace conversion

libyuv pkgdb entry

It could compete with ffmpeg on speed, sadly scaling requires a separate step.



Mon, 11 Jan 2016 09:39:36 GMT - Antoine Martin: status changed

Added preliminary support and packaging (Fedora only) in r11646, only for BGRX to YUV420P. That thing is seriously fast. It beats the current record holder (ffmpeg's swscale) by a huge margin on 256x256 and up (this sort of size is pretty common for video). On a somewhat old AMD FX 8150:

CSC 16x16 128x128 256x256 512x512 1920x1080 2560x1600
swscale 12 137 168 188 183 184
libyuv 18 811 1582 2101 1039 1087

Still TODO:


Mon, 11 Jan 2016 13:37:55 GMT - Antoine Martin: attachment set

work in progress patch


Mon, 11 Jan 2016 13:44:59 GMT - Antoine Martin: owner, status changed

r11647 adds support for scaling and improves the tests to measure the performance. Scaling costs us about 10 to 20%, and swscale is a lot more competitive when scaling is involved. I have also tested the size limits of this csc mode, and it can scale past 32kx32k which will be helpful for #969.


@smo:


For testing:


Sun, 07 Feb 2016 01:35:35 GMT - Antoine Martin: attachment set

example pkgconfig file for osx


Sun, 07 Feb 2016 01:36:57 GMT - Antoine Martin: priority changed

I have figured out the win32 build, as as of r11874 you just do:

cd libyuv-r1446
mkdir out
cd out
cmake .. -G "Visual Studio 9 2008"

Then build the solution with the visual studio 9 GUI (or directly with nmake? should work too). Note: I changed from "debug" to "Release" build before building. If building for Python 3, we should probably use a new Visual Studio version... but the "Xpra-Build-Libs" directory structure is not version specific. Maybe it should be.

Talking about which, you have to place the files where our build system will find them, ie for me:

E:\Xpra-Build-Libs\
                   libyuv\
                          bin\
                              convert.exe
                          lib\
                              yub.lib
                          include\
                                  libyuv.h
                                  libyuv\
                                         *.h

The OSX build is more problematic, first you have to install cmake (that's easy), then it fails with:

libyuv-r1446/source/row_gcc.cc: In function 'void libyuv::ARGBToUVRow_SSSE3(const uint8*, int, uint8*, uint8*, int)':
libyuv-r1446/source/row_gcc.cc:886: error: can't find a register in class 'GENERAL_REGS' while reloading 'asm'
libyuv-r1446/source/row_gcc.cc:886: error: 'asm' operand has impossible constraints

Because of this gcc bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=11203.

The workaround:

LDFLAGS="-read_only_relocs suppress" \
CXXFLAGS="-march=i686 -O3 -fno-pic -fomit-frame-pointer -frename-registers -pipe" \
    cmake -DCMAKE_INSTALL_PREFIX:PATH=$PREFIX ..

It's ugly and uses -fno-pic... but at least it builds. (this is not problem with 64-bit builds)

Then you need to generate and install the pkgconfig file attached to this ticket since libyuv does not provide a template. But it still fails because of the text relocs, and adding the flag to the compiler options does not help:

creating build/temp.macosx-10.5-i386-2.7/xpra/codecs/csc_libyuv
/usr/bin/gcc-4.2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch i386 -I/Developer/SDKs/MacOSX10.5.sdk/usr/include \
    -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -I/Users/osx/gtk/inst/include -arch i386 \
    -I/Developer/SDKs/MacOSX10.5.sdk/usr/include -isysroot /Developer/SDKs/MacOSX10.5.sdk -I/Users/osx/gtk/inst/include/python2.7 \
    -c xpra/codecs/csc_libyuv/colorspace_converter.cpp -o build/temp.macosx-10.5-i386-2.7/xpra/codecs/csc_libyuv/colorspace_converter.o \
    -Wall -fPIC -read_only_relocs suppress
cc1plus: warning: command line option "-Wstrict-prototypes" is valid for C/ObjC but not for C++
/Developer/usr/bin/g++-4.2 -bundle -undefined dynamic_lookup \
    -L/Users/osx/gtk/inst/lib -L/Users/osx/gtk/inst/lib -arch i386 -L/Developer/SDKs/MacOSX10.5.sdk/usr/lib \
    -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -Wl,-headerpad_max_install_names \
    -L/Users/osx/gtk/inst/lib -L/Users/osx/gtk/inst/lib -arch i386 -L/Developer/SDKs/MacOSX10.5.sdk/usr/lib \
    -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -Wl,-headerpad_max_install_names -arch i386 \
    -I/Developer/SDKs/MacOSX10.5.sdk/usr/include -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 \
    -I/Users/osx/gtk/inst/include -arch i386 -I/Developer/SDKs/MacOSX10.5.sdk/usr/include -isysroot /Developer/SDKs/MacOSX10.5.sdk \
    build/temp.macosx-10.5-i386-2.7/xpra/codecs/csc_libyuv/colorspace_converter.o -L/Users/osx/gtk/inst/lib \
    -lyuv -o build/lib.macosx-10.5-i386-2.7/xpra/codecs/csc_libyuv/colorspace_converter.so -Wall -read_only_relocs suppress
ld: absolute addressing (perhaps -mdynamic-no-pic) used in _GetARGBBlend from /Users/osx/gtk/inst/lib/libyuv.a(planar_functions.cc.o) \
   not allowed in slidable image. Use '-read_only_relocs suppress' to enable text relocs

And at this point I give up. We'll enable libyuv when we get 64-bit builds.


Sun, 13 Mar 2016 04:12:11 GMT - Antoine Martin: owner, status changed

Not sure why I am only seeing this now, but I get reliable crashes with a different user over tcp with vp8 and d-feet as client app:

2016-03-13 11:13:46,179 libyuv.ColorspaceConverter.init_context(499, 316, 'BGRX', 499, 316, 'YUV420P', 48)
2016-03-13 11:13:46,180 buffer size=243712, scaling=0, filtermode=None
2016-03-13 11:13:46,183 libyuv.ARGBToI420 took 0.2ms
2016-03-13 11:13:46,188 YUVImageWrapper.free() cython_buffer=0x7fbc883ee220
2016-03-13 11:13:46,188 YUVImageWrapper.free() cython_buffer=0x0
2016-03-13 11:13:49,606 libyuv.ColorspaceConverter.init_context(499, 311, 'BGRX', 499, 311, 'YUV420P', 38)
2016-03-13 11:13:49,606 buffer size=239616, scaling=0, filtermode=None
2016-03-13 11:13:49,609 libyuv.ARGBToI420 took 0.3ms
2016-03-13 11:13:49,616 YUVImageWrapper.free() cython_buffer=0x7fbc887e6050
*** Error in `/bin/python': double free or corruption (!prev): 0x00007fbc887e6050 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x77da5)[0x7fbcc256dda5]
/lib64/libc.so.6(+0x804fa)[0x7fbcc25764fa]
/lib64/libc.so.6(cfree+0x4c)[0x7fbcc2579cac]
/usr/lib64/python2.7/site-packages/xpra/codecs/csc_libyuv/colorspace_converter.so(+0xdbaf)[0x7fbc92983baf]

Sun, 13 Mar 2016 05:43:13 GMT - Antoine Martin: priority changed

The crash only occurs with the vpx codec (happens with both with vp8 and vp9)... h264, and mpeg4 are not affected when using the exact same csc (using XPRA_FORCE_CSC_MODE=YUV420P for x264)

But the libyuv code looks fine, and temporarily removing all calls to free() the memory does not help!? Raising. This may require using valgrind (oh noes).


Thu, 31 Mar 2016 15:01:12 GMT - Antoine Martin: owner, status, summary changed

I cannot reproduce the problem on an Intel system, maybe it was a bad build somehow, or maybe this crashes on a different setup. (was on an AMD CPU system) valgrind didn't show anything even remotely suspicious.

@smo: back over to you, can you break it? if not, just record some performance stats.


Fri, 01 Apr 2016 12:42:00 GMT - Antoine Martin: owner, status changed

Hit it again, on Intel this time :(


Sat, 02 Apr 2016 05:52:42 GMT - Antoine Martin: owner, status changed

Turns out it's a trivial rounding error, fixed in r12303 - which I never hit because the tests used even dimensions, the bug only occurred with an odd input height. (not sure why this fired more with vpx than x264! The memory corruption would affect any buffer allocated after this one)


Thu, 07 Apr 2016 20:15:33 GMT - Smo:

Ran some of these tests on my machine to confirm libyuv is much faster here are my results

Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz BGRX to YUV420P

CSC 16x16 128x128 256x256 512x512 1920x1080 2560x1600
cython116064646162
swscale10126165182188199
libyuv164271039143217431444

I will do more testing like mentioned in comment:2 to make sure there are no crashes and update this ticket if there is.

It is quite obvious that libyuv is much faster than the alternatives.


Thu, 14 Apr 2016 18:14:08 GMT - Smo:

I haven't run across any issues on the server end in linux with this but I haven't got to compiling and bundling libyuv on win32 yet. I will get to this shortly and update the ticket if I find any issues.


Thu, 21 Apr 2016 21:29:05 GMT - Smo: status changed; resolution set

No issues building this on win32 following your instructions.

The command line way of building this is

msbuild Project.sln /p:Configuration=Release

Thu, 11 Oct 2018 08:38:03 GMT - Antoine Martin:

See also #1280, #1883, #2004


Sat, 23 Jan 2021 05:11:18 GMT - migration script:

this ticket has been moved to: https://github.com/Xpra-org/xpra/issues/973