dfigus / addon-tvheadend

TVHeadend - Home Assistant Add-on
MIT License
3 stars 0 forks source link

FFMPEG with VAAPI enabled #141

Closed rucati closed 8 months ago

rucati commented 9 months ago

The Problem

Is ffmpeg with vaapi-enabled compiled? This is needed to transcode DVB-C stream.

Environment

Logs

log doesn't complain, /dev/dri/renderD128 is present, but only software transcoding works (high cpu load), except:

2024-03-06 20:44:54.340 vaapi: Bitrate = 0 kbps; Buffer size = 0 kbps; Max bitrate = 0 kbps 2024-03-06 20:44:54.342 libav: AVCodecContext: No usable encoding entrypoint found for profile VAProfileH264High (7).

Steps to reproduce

create a transcode profile in tvheadend, choose vaapi, add hardware accelaration, define /dev/dri/renderD128 as input

Additional Information

dfigus commented 9 months ago

In the add-on I use the precompiled Alpine package. The build log is here. There is one section which seems to be relevant:

Enabled hwaccels:
av1_vaapi               hevc_vdpau              vc1_vaapi
av1_vdpau               hevc_vulkan             vc1_vdpau
av1_vulkan              mjpeg_vaapi             vp8_vaapi
h263_vaapi              mpeg1_vdpau             vp9_vaapi
h264_vaapi              mpeg2_vaapi             vp9_vdpau
h264_vdpau              mpeg2_vdpau             wmv3_vaapi
h264_vulkan             mpeg4_vaapi             wmv3_vdpau
hevc_vaapi              mpeg4_vdpau

Can you check whether you can use ffmpeg from the addon command line to use hw encoder? I think the following could be used to test:

fmpeg -i <input_file> -c:v <codec> -c:a copy <output_file>

The supported codecs can be listed by

ffmpeg -encoders

I'm running on a RPI4 and my testing is a bit limited 😆.

Furthermore is there a difference compared to your host OS? What is your hardware offering the hw acceleration?

dfigus commented 9 months ago

@rucati It seems that direct video access is required for hw encoding to work. I updated the addon config to include this and released a new version v5.1.2. Can you give it a try?

On my RPI4 I could not get hw encoding to work so far, but after adding the video direct access it seems I hit another RPI issue.

rucati commented 9 months ago

Hi Useful pages (also REPi should be able to do h264): https://tvheadend.org/d/7953-adding-h264_v4l2m2m-encoder-for-rpi4-to-tvh/3 https://tvheadend.org/d/4194-hardware-transcoding/17 https://tvheadend.org/d/7609-help-needed-vaapi-and-tvheadend/16 I am trying to pipe the stream with ffmpeg (as links suggest). I will try to test some more this weekend. The internal ffmpeg of tvheadend just gives me a black picture (see below) and the logs don't tell me enough to find out what's wrong.

It's hard to keep up, already 5.1.3! Let's answer all questions: vainfo in docker gives me:

root@dae55a80-tvheadend:/$ vainfo Trying display: wayland error: XDG_RUNTIME_DIR is invalid or not set in the environment. Trying display: x11 error: can't connect to X server! Trying display: drm libva info: VA-API version 1.20.0 libva info: Trying to open /usr/lib/dri/iHD_drv_video.so libva info: va_openDriver() returns -1 libva info: Trying to open /usr/lib/dri/i965_drv_video.so libva info: Found init function __vaDriverInit_1_20 libva info: va_openDriver() returns 0 vainfo: VA-API version: 1.20 (libva 2.20.0) vainfo: Driver version: Intel i965 driver for Intel(R) CherryView - 2.4.1 vainfo: Supported profile and entrypoints VAProfileMPEG2Simple : VAEntrypointVLD VAProfileMPEG2Simple : VAEntrypointEncSlice VAProfileMPEG2Main : VAEntrypointVLD VAProfileMPEG2Main : VAEntrypointEncSlice VAProfileH264ConstrainedBaseline: VAEntrypointVLD VAProfileH264ConstrainedBaseline: VAEntrypointEncSlice VAProfileH264Main : VAEntrypointVLD VAProfileH264Main : VAEntrypointEncSlice VAProfileH264High : VAEntrypointVLD VAProfileH264High : VAEntrypointEncSlice VAProfileH264MultiviewHigh : VAEntrypointVLD VAProfileH264MultiviewHigh : VAEntrypointEncSlice VAProfileH264StereoHigh : VAEntrypointVLD VAProfileH264StereoHigh : VAEntrypointEncSlice VAProfileVC1Simple : VAEntrypointVLD VAProfileVC1Main : VAEntrypointVLD VAProfileVC1Advanced : VAEntrypointVLD VAProfileNone : VAEntrypointVideoProc VAProfileJPEGBaseline : VAEntrypointVLD VAProfileJPEGBaseline : VAEntrypointEncPicture VAProfileVP8Version0_3 : VAEntrypointVLD VAProfileVP8Version0_3 : VAEntrypointEncSlice VAProfileHEVCMain : VAEntrypointVLD

ffmpeg -encoders gives me:

root@dae55a80-tvheadend:/$ ffmpeg -encoders ffmpeg version 6.1.1 Copyright (c) 2000-2023 the FFmpeg developers built with gcc 13.2.1 (Alpine 13.2.1_git20231014) 20231014 configuration: --prefix=/usr --disable-librtmp --disable-lzma --disable-static --disable-stripping --enable-avfilter --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libdrm --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-libmp3lame --enable-libopenmpt --enable-libopus --enable-libplacebo --enable-libpulse --enable-librav1e --enable-librist --enable-libsoxr --enable-libsrt --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-lto=auto --enable-lv2 --enable-openssl --enable-pic --enable-postproc --enable-pthreads --enable-shared --enable-vaapi --enable-vdpau --enable-version3 --enable-vulkan --optflags=-O3 --enable-libjxl --enable-libsvtav1 --enable-libvpl libavutil 58. 29.100 / 58. 29.100 libavcodec 60. 31.102 / 60. 31.102 libavformat 60. 16.100 / 60. 16.100 libavdevice 60. 3.100 / 60. 3.100 libavfilter 9. 12.100 / 9. 12.100 libswscale 7. 5.100 / 7. 5.100 libswresample 5. 0.100 / 5. 0.100 libpostproc 57. 3.100 / 57. 3.100 Encoders: V..... = Video A..... = Audio S..... = Subtitle .F.... = Frame-level multithreading ..S... = Slice-level multithreading ...X.. = Codec is experimental ....B. = Supports draw_horiz_band .....D = Supports direct rendering method 1 V....D a64multi Multicolor charset for Commodore 64 (codec a64_multi) V....D a64multi5 Multicolor charset for Commodore 64, extended with 5th color (colram) (codec a64_multi5) V....D alias_pix Alias/Wavefront PIX image V..... amv AMV Video V....D apng APNG (Animated Portable Network Graphics) image V....D asv1 ASUS V1 V....D asv2 ASUS V2 V....D libaom-av1 libaom AV1 (codec av1) V....D librav1e librav1e AV1 (codec av1) V..... libsvtav1 SVT-AV1(Scalable Video Technology for AV1) encoder (codec av1) V..... av1_qsv AV1 (Intel Quick Sync Video acceleration) (codec av1) V....D av1_vaapi AV1 (VAAPI) (codec av1) V....D avrp Avid 1:1 10-bit RGB Packer V..X.D avui Avid Meridien Uncompressed V....D ayuv Uncompressed packed MS 4:4:4:4 VF...D bitpacked Bitpacked V....D bmp BMP (Windows and OS/2 bitmap) VF...D cfhd GoPro CineForm HD V....D cinepak Cinepak V....D cljr Cirrus Logic AccuPak V.S..D vc2 SMPTE VC-2 (codec dirac) VFS..D dnxhd VC3/DNxHD V....D dpx DPX (Digital Picture Exchange) image VFS..D dvvideo DV (Digital Video) VF...D exr OpenEXR image V.S..D ffv1 FFmpeg video codec #1 VF...D ffvhuff Huffyuv FFmpeg variant V....D fits Flexible Image Transport System V....D flashsv Flash Screen Video V....D flashsv2 Flash Screen Video Version 2 V..... flv FLV / Sorenson Spark / Sorenson H.263 (Flash Video) (codec flv1) V....D gif GIF (Graphics Interchange Format) V..... h261 H.261 V..... h263 H.263 / H.263-1996 V..... h263_v4l2m2m V4L2 mem2mem H.263 encoder wrapper (codec h263) V.S... h263p H.263+ / H.263-1998 / H.263 version 2 V....D libx264 libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (codec h264) V....D libx264rgb libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB (codec h264) V..... h264_qsv H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video acceleration) (codec h264) V..... h264_v4l2m2m V4L2 mem2mem H.264 encoder wrapper (codec h264) V....D h264_vaapi H.264/AVC (VAAPI) (codec h264) VF...D hdr HDR (Radiance RGBE format) image V....D libx265 libx265 H.265 / HEVC (codec hevc) V..... hevc_qsv HEVC (Intel Quick Sync Video acceleration) (codec hevc) V..... hevc_v4l2m2m V4L2 mem2mem HEVC encoder wrapper (codec hevc) V....D hevc_vaapi H.265/HEVC (VAAPI) (codec hevc) VF...D huffyuv Huffyuv / HuffYUV VF...D jpeg2000 JPEG 2000 VF...D jpegls JPEG-LS V..... libjxl libjxl JPEG XL (codec jpegxl) VF...D ljpeg Lossless JPEG VFS..D magicyuv MagicYUV video VFS... mjpeg MJPEG (Motion JPEG) V..... mjpeg_qsv MJPEG (Intel Quick Sync Video acceleration) (codec mjpeg) V....D mjpeg_vaapi MJPEG (VAAPI) (codec mjpeg) V.S... mpeg1video MPEG-1 video V.S... mpeg2video MPEG-2 video V..... mpeg2_qsv MPEG-2 video (Intel Quick Sync Video acceleration) (codec mpeg2video) V....D mpeg2_vaapi MPEG-2 (VAAPI) (codec mpeg2video) V.S... mpeg4 MPEG-4 part 2 V....D libxvid libxvidcore MPEG-4 part 2 (codec mpeg4) V..... mpeg4_v4l2m2m V4L2 mem2mem MPEG4 encoder wrapper (codec mpeg4) V..... msmpeg4v2 MPEG-4 part 2 Microsoft variant version 2 V..... msmpeg4 MPEG-4 part 2 Microsoft variant version 3 (codec msmpeg4v3) V....D msrle Microsoft RLE V..... msvideo1 Microsoft Video-1 V....D pam PAM (Portable AnyMap) image V....D pbm PBM (Portable BitMap) image V....D pcx PC Paintbrush PCX image V....D pfm PFM (Portable FloatMap) image V....D pgm PGM (Portable GrayMap) image V....D pgmyuv PGMYUV (Portable GrayMap YUV) image V....D phm PHM (Portable HalfFloatMap) image VF...D png PNG (Portable Network Graphics) image V....D ppm PPM (Portable PixelMap) image VF...D prores Apple ProRes VF...D prores_aw Apple ProRes (codec prores) VFS... prores_ks Apple ProRes (iCodec Pro) (codec prores) VF...D qoi QOI (Quite OK Image format) image V....D qtrle QuickTime Animation (RLE) video V....D r10k AJA Kona 10-bit RGB Codec V....D r210 Uncompressed RGB 10-bit VF...D rawvideo raw video V....D roqvideo id RoQ video (codec roq) V....D rpza QuickTime video (RPZA) V..... rv10 RealVideo 1.0 V..... rv20 RealVideo 2.0 V....D sgi SGI image V....D smc QuickTime Graphics (SMC) V....D snow Snow V..... speedhq NewTek SpeedHQ V....D sunrast Sun Rasterfile image V....D svq1 Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1 V....D targa Truevision Targa image V....D libtheora libtheora Theora (codec theora) VF...D tiff TIFF image VF...D utvideo Ut Video VF...D v210 Uncompressed 4:2:2 10-bit V....D v308 Uncompressed packed 4:4:4 V....D v408 Uncompressed packed QT 4:4:4:4 V....D v410 Uncompressed 4:4:4 10-bit V.S..D vbn Vizrt Binary Image V..... vnull null video V....D libvpx libvpx VP8 (codec vp8) V..... vp8_v4l2m2m V4L2 mem2mem VP8 encoder wrapper (codec vp8) V....D vp8_vaapi VP8 (VAAPI) (codec vp8) V....D libvpx-vp9 libvpx VP9 (codec vp9) V....D vp9_vaapi VP9 (VAAPI) (codec vp9) V..... vp9_qsv VP9 video (Intel Quick Sync Video acceleration) (codec vp9) VF...D wbmp WBMP (Wireless Application Protocol Bitmap) image V....D libwebp_anim libwebp WebP image (codec webp) V....D libwebp libwebp WebP image (codec webp) V..... wmv1 Windows Media Video 7 V..... wmv2 Windows Media Video 8 V..... wrapped_avframe AVFrame to AVPacket passthrough V....D xbm XBM (X BitMap) image V....D xface X-face image V....D xwd XWD (X Window Dump) image V....D y41p Uncompressed YUV 4:1:1 12-bit V....D yuv4 Uncompressed packed 4:2:0 VF...D zlib LCL (LossLess Codec Library) ZLIB V....D zmbv Zip Motion Blocks Video A....D aac AAC (Advanced Audio Coding) A....D ac3 ATSC A/52A (AC-3) A....D ac3_fixed ATSC A/52A (AC-3) (codec ac3) A....D adpcm_adx SEGA CRI ADX ADPCM A....D adpcm_argo ADPCM Argonaut Games A....D g722 G.722 ADPCM (codec adpcm_g722) A....D g726 G.726 ADPCM (codec adpcm_g726) A....D g726le G.726 little endian ADPCM ("right-justified") (codec adpcm_g726le) A....D adpcm_ima_alp ADPCM IMA High Voltage Software ALP A....D adpcm_ima_amv ADPCM IMA AMV A....D adpcm_ima_apm ADPCM IMA Ubisoft APM A....D adpcm_ima_qt ADPCM IMA QuickTime A....D adpcm_ima_ssi ADPCM IMA Simon & Schuster Interactive A....D adpcm_ima_wav ADPCM IMA WAV A....D adpcm_ima_ws ADPCM IMA Westwood A....D adpcm_ms ADPCM Microsoft A....D adpcm_swf ADPCM Shockwave Flash A....D adpcm_yamaha ADPCM Yamaha A....D alac ALAC (Apple Lossless Audio Codec) A..... anull null audio A....D aptx aptX (Audio Processing Technology for Bluetooth) A....D aptx_hd aptX HD (Audio Processing Technology for Bluetooth) A....D comfortnoise RFC 3389 comfort noise generator A....D dfpwm DFPWM1a audio A..X.D dca DCA (DTS Coherent Acoustics) (codec dts) A....D eac3 ATSC A/52 E-AC-3 A....D flac FLAC (Free Lossless Audio Codec) A....D g723_1 G.723.1 A..X.D mlp MLP (Meridian Lossless Packing) A....D mp2 MP2 (MPEG audio layer 2) A....D mp2fixed MP2 fixed point (MPEG audio layer 2) (codec mp2) A....D libmp3lame libmp3lame MP3 (MPEG audio layer 3) (codec mp3) A....D nellymoser Nellymoser Asao A..X.D opus Opus A....D libopus libopus Opus (codec opus) A....D pcm_alaw PCM A-law / G.711 A-law A....D pcm_bluray PCM signed 16|20|24-bit big-endian for Blu-ray media A....D pcm_dvd PCM signed 16|20|24-bit big-endian for DVD media A....D pcm_f32be PCM 32-bit floating point big-endian A....D pcm_f32le PCM 32-bit floating point little-endian A....D pcm_f64be PCM 64-bit floating point big-endian A....D pcm_f64le PCM 64-bit floating point little-endian A....D pcm_mulaw PCM mu-law / G.711 mu-law A....D pcm_s16be PCM signed 16-bit big-endian A....D pcm_s16be_planar PCM signed 16-bit big-endian planar A....D pcm_s16le PCM signed 16-bit little-endian A....D pcm_s16le_planar PCM signed 16-bit little-endian planar A....D pcm_s24be PCM signed 24-bit big-endian A....D pcm_s24daud PCM D-Cinema audio signed 24-bit A....D pcm_s24le PCM signed 24-bit little-endian A....D pcm_s24le_planar PCM signed 24-bit little-endian planar A....D pcm_s32be PCM signed 32-bit big-endian A....D pcm_s32le PCM signed 32-bit little-endian A....D pcm_s32le_planar PCM signed 32-bit little-endian planar A....D pcm_s64be PCM signed 64-bit big-endian A....D pcm_s64le PCM signed 64-bit little-endian A....D pcm_s8 PCM signed 8-bit A....D pcm_s8_planar PCM signed 8-bit planar A....D pcm_u16be PCM unsigned 16-bit big-endian A....D pcm_u16le PCM unsigned 16-bit little-endian A....D pcm_u24be PCM unsigned 24-bit big-endian A....D pcm_u24le PCM unsigned 24-bit little-endian A....D pcm_u32be PCM unsigned 32-bit big-endian A....D pcm_u32le PCM unsigned 32-bit little-endian A....D pcm_u8 PCM unsigned 8-bit A....D pcm_vidc PCM Archimedes VIDC A....D real_144 RealAudio 1.0 (14.4K) (codec ra_144) A....D roq_dpcm id RoQ DPCM A..X.D s302m SMPTE 302M A....D sbc SBC (low-complexity subband codec) A..X.D sonic Sonic A..X.D sonicls Sonic lossless A..X.D truehd TrueHD A....D tta TTA (True Audio) A..X.D vorbis Vorbis A....D libvorbis libvorbis (codec vorbis) A....D wavpack WavPack A....D wmav1 Windows Media Audio 1 A....D wmav2 Windows Media Audio 2 S..... ssa ASS (Advanced SubStation Alpha) subtitle (codec ass) S..... ass ASS (Advanced SubStation Alpha) subtitle S..... dvbsub DVB subtitles (codec dvb_subtitle) S..... dvdsub DVD subtitles (codec dvd_subtitle) S..... mov_text 3GPP Timed Text subtitle S..... srt SubRip subtitle (codec subrip) S..... subrip SubRip subtitle S..... text Raw text subtitle S..... ttml TTML subtitle S..... webvtt WebVTT subtitle S..... xsub DivX subtitles (XSUB)

The onboard graphics of my N3150 should be able to encode VP8 and h264. Basically I am running supervised on top of debian 12. I replaced my DVB-C card (former TBS, now hauppauge) which is natively supported in my kernel. With TBS I can get my card working, but I lose sound and graphics (so no /dev/dri). See: https://github.com/tbsdtv/linux_media/issues/250 Now I have:

ls -l /dev/dri/ by-path/ card0 renderD128

Encoding with a self created codec (VP8) profile (where I can define the path of my encoder = /dev/dri/renderD128) in tvheadend results in

2024-03-07 20:24:49.371 transcode: 0009: 01:H264: ==> Using profile vp8vaapi 2024-03-07 20:24:49.371 transcode: 0009: 02:MPEG2AUDIO: ==> Copy 2024-03-07 20:24:49.371 transcode: 0009: 03:MPEG2AUDIO: ==> Copy 2024-03-07 20:24:49.371 transcode: 0009: 06:TEXTSUB: ==> Copy 2024-03-07 20:24:51.071 libav: AVCodecContext: mmco: unref short failure 2024-03-07 20:24:51.115 libav: AVCodecContext: mmco: unref short failure 2024-03-07 20:24:51.141 libav: AVCodecContext: mmco: unref short failure 2024-03-07 20:24:51.141 libav: AVCodecContext: mmco: unref short failure 2024-03-07 20:24:51.457 vaapi: Bitrate = 0 kbps; Buffer size = 0 kbps; Max bitrate = 0 kbps 2024-03-07 20:24:51.462 libav: AVCodecContext: No usable encoding entrypoint found for profile VAProfileVP8Version0_3 (14).

Tvheadend is configured to do all the guesswork: afbeelding

To sum up: after the shizzle with TBS, I do not longer have complaints in tvheadend of not finding the encoder ( /dev/dri/renderD128). Still, I have a black picture. The logs don't tell me much (i.e.: libav: AVCodecContext: No usable encoding entrypoint found for profile VAProfileVP8Version0_3 (14).).

dfigus commented 9 months ago

@rucati Can you check whether calling ffmpeg directly inside and outside the addon shows any further details? Probably the following should be the command line version:

ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -vaapi_device /dev/dri/renderD128 -i <input_file>  -c:v vp8_vaapi -c:a copy <output_file>

Record some show via TVH and then feed it into the ffmpeg encoder separately via the command line to nail down where the problem is. E.g. for my RPI4 the h264_v4l2m2m codec for hw acceleration is not supported by TVH, hence it's a TVH problem and not ffmpeg or addon related.

Let's try to get it running on the host directly and then see what's missing in the addon for it.

Regarding TBS: After struggling several days I also gave up compiling the TBS open source drivers. Based on their install instructions they compile the whole linux media stack and doing this directly on the RPI takes ages. I did get my HA OS cross-compiling setup to work, but I would need to patch the kernel to include the TBS drivers here. But I would also need to check every difference between the regular linux media stack and the TBS version of it - and judge which difference might be relevant for TBS 5922...

rucati commented 9 months ago

@dfigus some success! Transcoding works and consumes only up to 15% cpu of the entire system (downscaled to 480p). Upscaling works too without wasting extra cpu-cycles, but then I get artefacts (probably to close to the limits of my gpu).

ffmpeg -hwaccel vaapi -vaapi_device /dev/dri/renderD128 -i pipe:0 -vf 'format=nv12,hwupload,scale_vaapi=w=-2:h=480' -c:v h264_vaapi -f mpegts -flags +global_header -crf 22 -preset:v veryfast -c:a aac -q:a 2 pipe:1

...which means I can transcode using the spawn-profile. This seems to be the way to go when I google how others get this working. The built-in options are very limited to success. Thanks for pointing out how the addon relates to ffmpeg and its built-in options. I've learned a lot. This issue can be closed, as it has no relevance to this addon. Now I can enjoy HD-channels from tvheadend on my GoogleTV-chromecast HD (its limited chip produced a choppy HD-stream from TVH).

Regarding TBS: we seem to be in the same boat there. Perhaps the RTL2832U-dongles may be interesting to work with. Many alternate options apart from dvb (such as ADS, SDR) and very cheap.