I'm working on adding h264_v4l2m2m encoding support to TVH on Raspberry Pi. OMX is deprecated, and no longer works on the latest RPi4 OSes (Raspbian nor Ubuntu) in TVH. It seems v4l2m2m will be the method of choice in future for hardware encoding via ffmpeg on Raspberry Pi.
And luckily the ffmpeg that tvheadend builds already supports it without needing any special libs.
The h264_v4l2m2m encoder is added to the webui using the changes described below.
But right now I'm not getting any video at the output. The TVH debug log looks like this:
2022-03-07 22:19:50.962 subscription: 0087: "192.168.0.214 [ user | Kodi Media Center ]" subscribing on channel "Virgin Media 1", weight: 100, adapter: "DiBcom 7000PC #0 : DVB-T #0", network: "DVB-T Network", mux: "562MHz", provider: "RTÉNL", service: "Virgin Media 1", profile="webtv", hostname="192.168.0.214", username="user", client="Kodi Media Center"
2022-03-07 22:19:51.439 transcode: 0007: 01:H264: ==> Using profile webtv-h264-v4l2m2m
2022-03-07 22:19:51.439 transcode: 0007: 02:MPEG2AUDIO: ==> Copy
2022-03-07 22:19:51.439 transcode: 0007: 03:MPEG2AUDIO: ==> Copy
2022-03-07 22:19:51.439 transcode: 0007: 06:PCR: ==> Filtered out
2022-03-07 22:19:51.439 transcode: 0007: 04:TEXTSUB: ==> Filtered out
2022-03-07 22:19:52.420 libav: AVCodecContext: Using device /dev/video11
2022-03-07 22:19:52.420 libav: AVCodecContext: driver 'bcm2835-codec' on card 'bcm2835-codec-encode' in mplane mode
2022-03-07 22:19:52.420 libav: AVCodecContext: requesting formats: output=YU12 capture=H264
2022-03-07 22:19:52.428 libav: AVCodecContext: Encoder does not support b-frames yet
2022-03-07 22:19:52.429 libav: AVCodecContext: DTS/PTS calculation for V4L2 encoding
2022-03-07 22:19:52.429 libav: AVCodecContext: is not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.
The first three lines in the log above from AVCodecContext are good. It shows its using the correct HW encoder device and bcm2835-codec driver, and these messages match what I get when using ffmpeg on a file using the h264_v4l2m2m encoder. So far, so good.
The next lines in the log are about B-frames and DTS/PTS calculation not being supported/implemented. I don't know if these are warnings or errors. From looking at libavcodec source code, they seem to be warnings. I'm guessing no B-frames is OK, OMX doesn't have this supported either.
I'm also guessing DTS/PTS calculation not implemented could be where the problem is.
But then I had a similar problem getting OMX working, no errors in the log, and the DTS/PTS do get set there, but I still get no video output: https://tvheadend.org/d/7929-using-omx-broadcom-video_encode-on-rpi3b-but-no-video-output
To add h264_v4l2m2m in the GUI, these were the changes. I can submit a patch once I get it working and tested.
1. In <code class="shell">/src/transcoding/codec/codec.c</code>:
- add <code class="c">extern TVHCodec tvh_codec_v4l2m2m_h264;</code> to the encoders section, and
- add <code class="c">tvh_codec_register(&tvh_codec_v4l2m2m_h264);</code> to the <code class="c">tvh_codecs_register()</code> in the TVHCodec section.
2. create new file <code class="shell">/src/transcoding/codec/codecs/libs/v4l2m2m.c</code> which is similar to <code class="shell">omx.c</code>, but replacing <code class="c">omx</code> with <code class="c">v4l2m2m</code> and with libname, libprefix and zerocopy removed from the struct.
3. add <code class="shell">LIBS-CODECS += v4l2m2m</code> in Makefile.
4. add <code class="shell">ENCODERS += h264_v4l2m2m</code> in Makefile.ffmpeg.
I'd also add a config option for this before making any patch.
With the above edits, TVH builds OK (just using <code class="shell">./Autobuild.sh -j4</code> with no other options for now), and h264_v4l2m2m is then available in the codecs list (screenshot attached).
Using ffprobe, I see the output has the copied audio streams, but no video:
ffprobe http://user:xx@192.168.0.87:9981/stream/channel/6725f4a2a58c319c5645cb8a40434c06?profile=webtv
ffprobe version 5.0 Copyright (c) 2007-2022 the FFmpeg developers
built with gcc 10 (Debian 10.2.1-6)
configuration: --prefix=/opt/ffmpeg-builds/build/linux64/usr --extra-ldexeflags=-static --pkg-config-flags=--static --extra-cflags=-I/opt/ffmpeg-builds/lib/linux64/include --extra-ldflags=-L/opt/ffmpeg-builds/lib/linux64/lib --extra-libs='-lpthread -lm -lz' --extra-ldexeflags=-static --enable-gpl --enable-nonfree --enable-version3 --disable-debug --disable-indev=sndio --disable-outdev=sndio --enable-fontconfig --enable-frei0r --enable-openssl --enable-libaom --enable-libfribidi --enable-libass --enable-libfreetype --enable-libmp3lame --enable-libopenjpeg --enable-libsoxr --enable-libspeex --enable-libvorbis --enable-libopus --enable-libtheora --disable-libvidstab --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libdav1d --enable-libxvid --enable-libfdk-aac --enable-ffplay --enable-pic
libavutil 57. 17.100 / 57. 17.100
libavcodec 59. 18.100 / 59. 18.100
libavformat 59. 16.100 / 59. 16.100
libavdevice 59. 4.100 / 59. 4.100
libavfilter 8. 24.100 / 8. 24.100
libswscale 6. 4.100 / 6. 4.100
libswresample 4. 3.100 / 4. 3.100
libpostproc 56. 3.100 / 56. 3.100
[http @ 0x4c045c0] Stream ends prematurely at 71628, should be 18446744073709551615
Input #0, mpegts, from 'http://user:xx@192.168.0.87:9981/stream/channel/6725f4a2a58c319c5645cb8a40434c06?profile=webtv':
Duration: N/A, start: 1.446278, bitrate: 320 kb/s
Program 1
Metadata:
service_name : RTÉ One
service_provider: Tvheadend 0.0.0-unknown
Stream #0:0[0x100](eng): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, fltp, 192 kb/s
Stream #0:1[0x101](und): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, mono, fltp, 64 kb/s
Stream #0:2[0x102](nar): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, mono, fltp, 64 kb/s
Would be great if anyone has any ideas on how to further debug why no video is getting to the output stream, either for this, or the OMX thread linked above.