Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libkvazaar encoder in ffmepg produces no output #349

Closed
jozefchutka opened this issue Sep 28, 2022 · 28 comments
Closed

libkvazaar encoder in ffmepg produces no output #349

jozefchutka opened this issue Sep 28, 2022 · 28 comments

Comments

@jozefchutka
Copy link

Using various ffmpeg versions (windows: master, 5.1, 4.4) downloaded from https://github.com/BtbN/FFmpeg-Builds/releases , I am not able to make libkvazaar work:

ffmpeg -filter_complex "smptehdbars=size=400x120:rate=30:duration=10" -c:v libkvazaar -y out.mp4 -loglevel debug

ffmpeg-latest -filter_complex "smptehdbars=size=400x120:rate=30:duration=10" -c:v libkvazaar -y out.mp4 -loglevel debug
ffmpeg version N-108404-gc03f9654c9-20220927 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 12.1.0 (crosstool-NG 1.25.0.55_3defb7b)
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-libaribb24 --enable-avisynth --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --enable-frei0r --enable-libgme --enable-libkvazaar --enable-libass --enable-libbluray --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librist --enable-libssh --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --disable-vaapi --enable-libvidstab --enable-vulkan --enable-libshaderc --enable-libplacebo --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20220927
  libavutil      57. 37.100 / 57. 37.100
  libavcodec     59. 48.100 / 59. 48.100
  libavformat    59. 33.100 / 59. 33.100
  libavdevice    59.  8.101 / 59.  8.101
  libavfilter     8. 49.100 /  8. 49.100
  libswscale      6.  8.112 /  6.  8.112
  libswresample   4.  9.100 /  4.  9.100
  libpostproc    56.  7.100 / 56.  7.100
Splitting the commandline.
Reading option '-filter_complex' ... matched as option 'filter_complex' (create a complex filtergraph) with argument 'smptehdbars=size=400x120:rate=30:duration=10'.
Reading option '-c:v' ... matched as option 'c' (codec name) with argument 'libkvazaar'.
Reading option '-y' ... matched as option 'y' (overwrite output files) with argument '1'.
Reading option 'out.mp4' ... matched as output url.
Reading option '-loglevel' ... matched as option 'loglevel' (set logging level) with argument 'debug'.
Finished splitting the commandline.
Parsing a group of options: global .
Applying option filter_complex (create a complex filtergraph) with argument smptehdbars=size=400x120:rate=30:duration=10.
Applying option y (overwrite output files) with argument 1.
Applying option loglevel (set logging level) with argument debug.
Successfully parsed a group of options.
[Parsed_smptehdbars_0 @ 000001a18c02ea00] Setting 'size' to value '400x120'
[Parsed_smptehdbars_0 @ 000001a18c02ea00] Setting 'rate' to value '30'
[Parsed_smptehdbars_0 @ 000001a18c02ea00] Setting 'duration' to value '10'
[Parsed_smptehdbars_0 @ 000001a18c02ea00] size:400x120 rate:30/1 duration:10.000000 sar:1/1
Parsing a group of options: output url out.mp4.
Applying option c:v (codec name) with argument libkvazaar.
Successfully parsed a group of options.
Opening an output file: out.mp4.
[file @ 000001a18c02e840] Setting default whitelist 'file,crypto,data'
Successfully opened the file.
Stream mapping:
  smptehdbars:default -> Stream #0:0 (libkvazaar)
Press [q] to stop, [?] for help
cur_dts is invalid st:0 (0) [init:0 i_done:0 finish:0] (this is harmless if it occurs once at the start per stream)
detected 8 logical cores
[Parsed_smptehdbars_0 @ 000001a18c02b700] Setting 'size' to value '400x120'
[Parsed_smptehdbars_0 @ 000001a18c02b700] Setting 'rate' to value '30'
[Parsed_smptehdbars_0 @ 000001a18c02b700] Setting 'duration' to value '10'
[Parsed_smptehdbars_0 @ 000001a18c02b700] size:400x120 rate:30/1 duration:10.000000 sar:1/1
[format @ 000001a18d826680] Setting 'pix_fmts' to value 'yuv420p'
[AVFilterGraph @ 000001a18c02e5c0] query_formats: 3 queried, 2 merged, 0 already done, 0 delayed
Compiled: INTEL, flags: MMX SSE SSE2 SSSE3 SSE41 SSE42
Detected: INTEL, flags: MMX SSE SSE2 SSE3 SSSE3 SSE41 SSE42 AVX AVX2
Available: avx(11) avx2(54) sse2(2) sse41(4)
In use: avx(1) avx2(54)
--owf=auto value set to 4.
--threads=auto value set to 4.
Output #0, mp4, to 'out.mp4':
  Metadata:
    encoder         : Lavf59.33.100
  Stream #0:0, 0, 1/15360: Video: hevc, 1 reference frame (hev1 / 0x31766568), yuv420p(bt709/unknown/unknown, progressive), 400x120 (0x0) [SAR 1:1 DAR 10:3], 0/1, q=2-31, 30 fps, 15360 tbn
    Metadata:
      encoder         : Lavc59.48.100 libkvazaar
Clipping frame in rate conversion by 0.000008
frame=    0 fps=0.0 q=0.0 size=       0kB time=-577014:32:22.77 bitrate=  -0.0kbits/s speed=N/A

out.mp4 is 44 bytes file (empty mp4 box I guess?)

I have also tried re-ecndoing an existing video, or different kvazaar-params but with the same result.
Is there any extra argument to be used or the encoder is broken?

I have opened an ffmpeg issue as well.

Feel free to close this issue if you find it unrelated to kvazaar, but rather bug on ffmpeg side.

@fador
Copy link
Member

fador commented Oct 11, 2022

So I was checking on things and if I compile kvazaar + ffmpeg in msys2 it seems to work just fine:
https://gist.github.com/fador/19c4eee37a0cfd80dcc01434bbea8765
But if you enable KVZ_BIT_DEPTH=10 it does not work.
I think BtbN build uses 8bit but it still fails so no idea at the moment..

@jozefchutka
Copy link
Author

Hi @fador , thanks for the heads up. I am compiling kvazaar for ffmpeg with emscripten using this configuration https://github.com/wide-video/ffmpeg-wasm/blob/main/scripts/build-kvazaar.sh , and it does not work either. Any idea what to be changed?

@fador
Copy link
Member

fador commented Oct 11, 2022

Well there was some problems to actually compile kvazaar with emscripten.
I got it working by adding --disable-asm to scripts/build-kvazaar.sh CONF_FLAGS parameters, it doesn't seem to like compiling the asm files..

edit:
I didn't have time to test the actual ffmpeg yet 😅

@nanake
Copy link

nanake commented Oct 12, 2022

Hi, I'm the one who added kvazaar on BtbN build.

Can you be more specific what you mean by 'no output'?

You are talking about no file output or there is a file with zero byte or anything else?

It works fine for me.

ffmpeg -s:v 1920x1280 -pix_fmt yuv420p -i bali_640x360_P420.yuv -c:v libkvazaar bali.hevc

ffmpeg -s:v 352x288 -pix_fmt yuv420p -i highway_cif.yuv -c:v libkvazaar highway.hevc

even your example command

ffmpeg -filter_complex "smptehdbars=size=400x120:rate=30:duration=10" -c:v libkvazaar -y out.hevc

@jozefchutka
Copy link
Author

jozefchutka commented Oct 12, 2022

Hi @nanake I have tested the latest BtbN build and still having issues.

Following command creates out.hevc which is exactly 0 bytes.

ffmpeg -filter_complex "smptehdbars=size=400x120:rate=30:duration=10" -c:v libkvazaar -y out.hevc

ffmpeg version N-108614-g37ee36f689-20221011 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 12.1.0 (crosstool-NG 1.25.0.55_3defb7b)
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-libaribb24 --enable-avisynth --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --enable-frei0r --enable-libgme --enable-libkvazaar --enable-libass --enable-libbluray --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librist --enable-libssh --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --disable-vaapi --enable-libvidstab --enable-vulkan --enable-libshaderc --enable-libplacebo --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20221011
  libavutil      57. 39.100 / 57. 39.100
  libavcodec     59. 50.100 / 59. 50.100
  libavformat    59. 34.101 / 59. 34.101
  libavdevice    59.  8.101 / 59.  8.101
  libavfilter     8. 49.101 /  8. 49.101
  libswscale      6.  8.112 /  6.  8.112
  libswresample   4.  9.100 /  4.  9.100
  libpostproc    56.  7.100 / 56.  7.100
Stream mapping:
  smptehdbars:default -> Stream #0:0 (libkvazaar)
Press [q] to stop, [?] for help
Compiled: INTEL, flags: MMX SSE SSE2 SSSE3 SSE41 SSE42
Detected: INTEL, flags: MMX SSE SSE2 SSE3 SSSE3 SSE41 SSE42 AVX AVX2
Available: avx(11) avx2(54) sse2(2) sse41(4)
In use: avx(1) avx2(54)
--owf=auto value set to 4.
--threads=auto value set to 4.
Output #0, hevc, to 'out.hevc':
  Metadata:
    encoder         : Lavf59.34.101
  Stream #0:0: Video: hevc, yuv420p(bt709/unknown/unknown, progressive), 400x120 [SAR 1:1 DAR 10:3], q=2-31, 30 fps, 30 tbn
    Metadata:
      encoder         : Lavc59.50.100 libkvazaar
frame=    0 fps=0.0 q=0.0 size=       0kB time=-577014:32:22.77 bitrate=  -0.0kbits/s speed=N/A

Running my mp4 example produces 44 bytes out.mp4 (attached)

ffmpeg -filter_complex "smptehdbars=size=400x120:rate=30:duration=10" -c:v libkvazaar -y out.mp4

Steps to reproduce:

  1. windows 10
  2. download https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip
  3. execute ffmpeg -filter_complex "smptehdbars=size=400x120:rate=30:duration=10" -c:v libkvazaar -y out.hevc
  4. 0 bytes out.hevc is created
out.mp4

@nanake
Copy link

nanake commented Oct 12, 2022

Okay, my bad! ☹️ It might be a bug somewhere in compiler. BtbN uses GCC 12, mine GCC 10.3.

If this is the case which difficult to track down, I really don't know how can i help.

@fador
Copy link
Member

fador commented Oct 12, 2022

Actually I figured out something with the ffmpeg-wasm

ffmpeg.worker.js:169 Uncaught RuntimeError: null function or function signature mismatch
    at kvz_quantize_residual_generic (transform.c:210:3)
    at quantize_tr_residual (transform.c:403:18)
    at kvz_quantize_lcu_residual (transform.c:503:7)
    at kvz_intra_recon_cu (intra.c:686:5)
    at search_cu (search.c:804:7)
    at search_cu (search.c:961:44)
    at search_cu (search.c:961:44)
    at encoder_state_worker_encode_lcu (search.c:1213:17)
    at encoder_state_encode (encoderstate.c:800:47)
    at encoder_state_encode (encoderstate.c:912:3)

The actual function pointer signatures are slightly different from the implementation (return type unsigned instead of void in the actual functions)
It seems that in some situations the compiler is just optimizing those functions away because of this 🤔
I'll try to fix those signatures today and see if things are better at least with wasm

@fador
Copy link
Member

fador commented Oct 12, 2022

btw the linux version of the BtbN ffmpeg build works just fine, windows version has the problems

@jozefchutka
Copy link
Author

jozefchutka commented Oct 12, 2022

thanks for looking into it @fador , just wanted to confirm I am also having Uncaught RuntimeError: null function or function signature mismatch in my wasm build even after adding --disable-asm

@fador
Copy link
Member

fador commented Oct 12, 2022

@jozefchutka I pushed a400504 to fix the problem in the wasm build, it works at least for me.
Hopefully it also fixes other ffmpeg build problems, but I don't have any ffmpeg build that was failing so cannot test right now 😅

@nanake
Copy link

nanake commented Oct 12, 2022

I managed to build a400504 on top of BtbN, but the problem still persists.

https://github.com/nanake/kvazaar-test/actions/runs/3234090307

@jozefchutka
Copy link
Author

great thank you @fador , I can confirm wasm build from a400504 works ok (without --disable-asm)

Do you have any idea how we can help @nanake fixing BtbN windows builds?

@nanake
Copy link

nanake commented Oct 12, 2022

@jozefchutka can you test the build from https://github.com/nanake/ffmpeg-tinderbox/releases/latest if it works for you.

@jozefchutka
Copy link
Author

@nanake cool, the ffmpeg-N-108616-git-479747645f-win64-nonfree.7z from https://github.com/nanake/ffmpeg-tinderbox/releases/latest works ok. Any chance getting these changes into BtbN builds as well?

@fador
Copy link
Member

fador commented Oct 12, 2022

@nanake thank you for helping with the problem!
So I noticed that if I add -kvazaar-params "cpuid=0" to disable optimizations, the BtbN build seem to work normally.
I can also verify that https://github.com/nanake/ffmpeg-tinderbox/releases/latest win64 version seem to work without anything extra so what is the difference 🤔

@nanake
Copy link

nanake commented Oct 12, 2022

Any chance getting these changes into BtbN builds as well?

I can also verify that https://github.com/nanake/ffmpeg-tinderbox/releases/latest win64 version seem to work without anything extra so what is the difference 🤔

It builds from different toolchain. mine based on MinGW 10, GCC 10.3.
BtbN based on ct-ng, MinGW 9, GCC 12.

@fador
Copy link
Member

fador commented Oct 12, 2022

It would not be the first time a new compiler version breaks one of our AVX2 optimizations, I might just have to debug things with the GCC 12.

@fador
Copy link
Member

fador commented Oct 13, 2022

By iterating through the optimized functions I found the functions causing the problem, not just a single function unfortunately!

We have a feature to override optimizations with environment variables, the following works in windows:

set KVAZAAR_OVERRIDE_sao_edge_ddistortion=generic
set KVAZAAR_OVERRIDE_calc_sao_edge_dir=generic
set KVAZAAR_OVERRIDE_sao_reconstruct_color=generic
set KVAZAAR_OVERRIDE_sao_band_ddistortion=generic
set KVAZAAR_OVERRIDE_dct_4x4=generic
set KVAZAAR_OVERRIDE_idct_4x4=generic

ffmpeg -filter_complex "smptehdbars=size=400x120:rate=30:duration=10" -c:v libkvazaar -y out.mp4

@jozefchutka
Copy link
Author

Good findings.

I would like to stay notified on related updates. Can this github issue be used for tracking or there is some other issue or system to subscribe to?

@fador
Copy link
Member

fador commented Oct 13, 2022

@jozefchutka we don't have any other system in use and I think this is the only issue we have with the ffmpeg problem..
I can just update this issue if I find a solution, but I have no idea how to fix it yet 😅

@fador
Copy link
Member

fador commented Jul 11, 2023

I pushed a fix that seems to work for the BtbN ffmpeg build issue: 589ed47
It seems that there are some bugs in mingw gcc stack alignment and adding -Wa,-muse-unaligned-vector-move to convert all instructions to unaligned versions fixes it.
At least in my testing this worked, so when BtbN bumps the Kvazaar version next time, this should be fixed.

@fador fador closed this as completed Jul 11, 2023
nanake added a commit to nanake/ffmpeg-tinderbox that referenced this issue Jul 11, 2023
fixes ultravideo/kvazaar#349

kvazaar commit ultravideo/kvazaar@589ed47 doesn't seem to have had an effect

Signed-off-by: nanake <nanake@users.noreply.github.com>
@nanake
Copy link

nanake commented Jul 11, 2023

I'm not sure about BtbN build.
But It doesn't seem to have had an effect on my ucrt64 build.

Need adding -Wa,-muse-unaligned-vector-move on my end to see it fixed.

@fador
Copy link
Member

fador commented Jul 11, 2023

@nanake oh sorry, I didn't know the ucrt64 build was also broken, I'll do some testing if adding it to this list:

[cygwin*|msys*|mingw*], [
does the job..

@nanake
Copy link

nanake commented Jul 11, 2023

@fador it does. but this check will never work

if test x"${CC}" = x"gcc" ; then

echo $CC

x86_64-w64-mingw32ucrt-gcc

@nanake
Copy link

nanake commented Jul 12, 2023

I don't think it will work on BtbN either as CC=x86_64-w64-mingw32-gcc

https://github.com/BtbN/FFmpeg-Builds/blob/683e3a4dc9084673f7ea53f94b98a9895f89c811/images/base-win64/Dockerfile#L36

docker run --rm -it ghcr.io/btbn/ffmpeg-builds/base-win64
echo $CC

x86_64-w64-mingw32-gcc

that's why I have to add that flag on my end to make it work.
https://github.com/nanake/ffmpeg-tinderbox/blob/6280aa81d637dfbaf55e5d4e6f915dbae812f939/scripts.d/50-kvazaar.sh#L27

@fador
Copy link
Member

fador commented Jul 12, 2023

weird, I did run the docker build and the resulting binary did work, but I will fix this 😅👌🏻
Thanks for the help!

@fador
Copy link
Member

fador commented Jul 12, 2023

49dc5fc should fix the GCC detection with the wildcard match *gcc

@nanake
Copy link

nanake commented Jul 12, 2023

Rebuild locally, I can verify fixed on that commit.

nanake added a commit to nanake/ffmpeg-tinderbox that referenced this issue Jul 12, 2023
updated to ultravideo/kvazaar@49dc5fc as a fix ultravideo/kvazaar#349

Signed-off-by: nanake <nanake@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants