xpra icon
Bug tracker and wiki

Opened 2 years ago

Closed 15 months ago

#973 closed enhancement (fixed)

libyuv colourspace conversion

Reported by: Antoine Martin Owned by: Smo
Priority: blocker Milestone: 0.17
Component: encodings Version: 0.15.x
Keywords: Cc:

Description

libyuv is Optimized for SSE2/SSSE3/AVX2 on x86/x64. and it is packaged in Fedora: libyuv pkgdb entry

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

Attachments (2)

csc-libyuv-yuv420p-to-bgrx.patch (3.4 KB) - added by Antoine Martin 19 months ago.
work in progress patch
libyuv.pc (198 bytes) - added by Antoine Martin 18 months ago.
example pkgconfig file for osx

Download all attachments as: .zip

Change History (13)

comment:1 Changed 19 months ago by Antoine Martin

Status: newassigned

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:

Changed 19 months ago by Antoine Martin

work in progress patch

comment:2 Changed 19 months ago by Antoine Martin

Owner: changed from Antoine Martin to Smo
Status: assignednew

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:

  • please record CSC performance data on a reference system (Xeon?), also for #926 - and maybe someone can generate some pretty graphs to visually verify the differences - as it is, libyuv should win the video pipeline scoring system because it has a lower "setup cost" than all the other csc modules (set to zero because we don't need any setup at all apart from instantiating our Cython adapter class!)
  • try the osx and win32 builds: you can probably ask for help on the "unusual" chromium build system (I do plan to use libyuv client side, see patch, which will be useful for opengl-challenged systems like OSX + Intel cards)
  • then re-assign to afarr for testing / some testing can be done in parallel already but the ticketing system does not have this option

For testing:

  • verify that it is installed and gets loaded, using the codec loader (xpra/codecs/loader.py / Encoding_info.exe) and video helper (xpra/codecs/video_helper.py) test classes
  • start the server with --csc-modules=libyuv (we only use libyuv for RGB to yuv, so it only gets used on the server) and verify that it gets used: xpra info | grep csc
  • maybe force downscaling to exercise that code: XPRA_SCALING_HARDCODED=1:2 xpra start ... , or just make the scaling more aggressive: xpra start --video-scaling=100 ... - the downscaling should be visible from xpra info | grep csc.
  • try to break it, look for memory leaks, visual corruption, etc
Last edited 19 months ago by Antoine Martin (previous) (diff)

Changed 18 months ago by Antoine Martin

Attachment: libyuv.pc added

example pkgconfig file for osx

comment:3 Changed 18 months ago by Antoine Martin

Priority: majorcritical

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.

comment:4 Changed 17 months ago by Antoine Martin

Owner: changed from Smo to Antoine Martin
Status: newassigned

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]
Last edited 17 months ago by Antoine Martin (previous) (diff)

comment:5 Changed 17 months ago by Antoine Martin

Priority: criticalblocker

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).

comment:6 Changed 16 months ago by Antoine Martin

Owner: changed from Antoine Martin to Smo
Status: assignednew
Summary: libyuv support as a better fallback than the cython modulelibyuv colourspace conversion

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.

comment:7 Changed 16 months ago by Antoine Martin

Owner: changed from Smo to Antoine Martin
Status: newassigned

Hit it again, on Intel this time :(

comment:8 Changed 16 months ago by Antoine Martin

Owner: changed from Antoine Martin to Smo
Status: assignednew

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)

comment:9 Changed 16 months ago by 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.

comment:10 Changed 16 months ago by 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.

comment:11 Changed 15 months ago by Smo

Resolution: fixed
Status: newclosed

No issues building this on win32 following your instructions.

The command line way of building this is

msbuild Project.sln /p:Configuration=Release
Note: See TracTickets for help on using tickets.