From 0a3295aa5cb7c43fe82219eec5866a988e3030ba Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Wed, 4 Jan 2023 17:16:11 -0700 Subject: [PATCH] Rewrite encoding logic and cleanup vaapi presets (#4898) * Remove duplicated vaapi presets * Move encoding to string with inputs and outputs * Formatting * Fix formatting * Fix typo * Remove vaapi encoder --- .../configuration/hardware_acceleration.md | 4 +- frigate/ffmpeg_presets.py | 132 ++---------------- frigate/restream.py | 2 +- 3 files changed, 16 insertions(+), 122 deletions(-) diff --git a/docs/docs/configuration/hardware_acceleration.md b/docs/docs/configuration/hardware_acceleration.md index 78c0b8389..477c574b3 100644 --- a/docs/docs/configuration/hardware_acceleration.md +++ b/docs/docs/configuration/hardware_acceleration.md @@ -25,7 +25,7 @@ ffmpeg: ```yaml ffmpeg: - hwaccel_args: preset-intel-vaapi + hwaccel_args: preset-vaapi ``` **NOTICE**: With some of the processors, like the J4125, the default driver `iHD` doesn't seem to work correctly for hardware acceleration. You may need to change the driver to `i965` by adding the following environment variable `LIBVA_DRIVER_NAME=i965` to your docker-compose file or [in the frigate.yml for HA OS users](advanced.md#environment_vars). @@ -42,7 +42,7 @@ ffmpeg: ```yaml ffmpeg: - hwaccel_args: preset-amd-vaapi + hwaccel_args: preset-vaapi ``` ### NVIDIA GPU diff --git a/frigate/ffmpeg_presets.py b/frigate/ffmpeg_presets.py index eb981a21c..c0ed81829 100644 --- a/frigate/ffmpeg_presets.py +++ b/frigate/ffmpeg_presets.py @@ -12,7 +12,7 @@ _user_agent_args = [ PRESETS_HW_ACCEL_DECODE = { "preset-rpi-32-h264": ["-c:v", "h264_v4l2m2m"], "preset-rpi-64-h264": ["-c:v", "h264_v4l2m2m"], - "preset-intel-vaapi": [ + "preset-vaapi": [ "-hwaccel_flags", "allow_profile_mismatch", "-hwaccel", @@ -42,16 +42,6 @@ PRESETS_HW_ACCEL_DECODE = { "-c:v", "hevc_qsv", ], - "preset-amd-vaapi": [ - "-hwaccel_flags", - "allow_profile_mismatch", - "-hwaccel", - "vaapi", - "-hwaccel_device", - "/dev/dri/renderD128", - "-hwaccel_output_format", - "vaapi", - ], "preset-nvidia-h264": [ "-hwaccel", "cuda", @@ -85,7 +75,7 @@ PRESETS_HW_ACCEL_DECODE = { } PRESETS_HW_ACCEL_SCALE = { - "preset-intel-vaapi": [ + "preset-vaapi": [ "-vf", "fps={},scale_vaapi=w={}:h={},hwdownload,format=yuv420p", "-f", @@ -103,12 +93,6 @@ PRESETS_HW_ACCEL_SCALE = { "-f", "rawvideo", ], - "preset-amd-vaapi": [ - "-vf", - "fps={},scale_vaapi=w={}:h={},hwdownload,format=yuv420p", - "-f", - "rawvideo", - ], "preset-nvidia-h264": [ "-vf", "fps={},scale_cuda=w={}:h={}:format=nv12,hwdownload,format=nv12,format=yuv420p", @@ -130,104 +114,11 @@ PRESETS_HW_ACCEL_SCALE = { } PRESETS_HW_ACCEL_ENCODE = { - "preset-intel-vaapi": [ - "-c:v", - "h264_vaapi", - "-g", - "50", - "-bf", - "0", - "-profile:v", - "high", - "-level:v", - "4.1", - "-sei:v", - "0", - ], - "preset-intel-qsv-h264": [ - "-c:v", - "h264_qsv", - "-g", - "50", - "-bf", - "0", - "-profile:v", - "high", - "-level:v", - "4.1", - "-async_depth:v", - "1", - ], - "preset-intel-qsv-h265": [ - "-c:v", - "h264_qsv", - "-g", - "50", - "-bf", - "0", - "-profile:v", - "high", - "-level:v", - "4.1", - "-async_depth:v", - "1", - ], - "preset-amd-vaapi": [ - "-c:v", - "h264_vaapi", - "-g", - "50", - "-bf", - "0", - "-profile:v", - "high", - "-level:v", - "4.1", - "-sei:v", - "0", - ], - "preset-nvidia-h264": [ - "-c:v", - "h264_nvenc", - "-g", - "50", - "-profile:v", - "high", - "-level:v", - "auto", - "-preset:v", - "p2", - "-tune:v", - "ll", - ], - "preset-nvidia-h265": [ - "-c:v", - "h264_nvenc", - "-g", - "50", - "-profile:v", - "high", - "-level:v", - "auto", - "-preset:v", - "p2", - "-tune:v", - "ll", - ], - "default": [ - "-c:v", - "libx264", - "-g", - "50", - "-profile:v", - "high", - "-level:v", - "4.1", - "-preset:v", - "superfast", - "-tune:v", - "zerolatency", - ], + "preset-intel-qsv-h264": "ffmpeg -hide_banner {0} -c:v h264_qsv -g 50 -bf 0 -profile:v high -level:v 4.1 -async_depth:v 1 {1}", + "preset-intel-qsv-h265": "ffmpeg -hide_banner {0} -c:v h264_qsv -g 50 -bf 0 -profile:v high -level:v 4.1 -async_depth:v 1 {1}", + "preset-nvidia-h264": "ffmpeg -hide_banner {0} -c:v h264_nvenc -g 50 -profile:v high -level:v auto -preset:v p2 -tune:v ll {1}", + "preset-nvidia-h265": "ffmpeg -hide_banner {0} -c:v h264_nvenc -g 50 -profile:v high -level:v auto -preset:v p2 -tune:v ll {1}", + "default": "ffmpeg -hide_banner {0} -c:v libx264 -g 50 -profile:v high -level:v 4.1 -preset:v superfast -tune:v zerolatency {1}", } PRESETS_HW_ACCEL_GO2RTC_ENGINE = { @@ -268,12 +159,15 @@ def parse_preset_hardware_acceleration_scale( return scale -def parse_preset_hardware_acceleration_encode(arg: Any) -> list[str]: +def parse_preset_hardware_acceleration_encode(arg: Any, input: str, output: str) -> str: """Return the correct scaling preset or default preset if none is set.""" if not isinstance(arg, str): - return PRESETS_HW_ACCEL_ENCODE["default"] + return PRESETS_HW_ACCEL_ENCODE["default"].format(input, output) - return PRESETS_HW_ACCEL_ENCODE.get(arg, PRESETS_HW_ACCEL_ENCODE["default"]) + return PRESETS_HW_ACCEL_ENCODE.get(arg, PRESETS_HW_ACCEL_ENCODE["default"]).format( + input, + output, + ) def parse_preset_hardware_acceleration_go2rtc_engine(arg: Any) -> list[str]: diff --git a/frigate/restream.py b/frigate/restream.py index 6ef6ebade..a16d5acc6 100644 --- a/frigate/restream.py +++ b/frigate/restream.py @@ -66,7 +66,7 @@ class RestreamApi: if self.config.restream.birdseye: self.relays[ "birdseye" - ] = f"exec:ffmpeg -hide_banner -f rawvideo -pix_fmt yuv420p -video_size {self.config.birdseye.width}x{self.config.birdseye.height} -r 10 -i {BIRDSEYE_PIPE} {' '.join(parse_preset_hardware_acceleration_encode(self.config.ffmpeg.hwaccel_args))} -rtsp_transport tcp -f rtsp {{output}}" + ] = f"exec:{parse_preset_hardware_acceleration_encode(self.config.ffmpeg.hwaccel_args, f'-f rawvideo -pix_fmt yuv420p -video_size {self.config.birdseye.width}x{self.config.birdseye.height} -r 10 -i {BIRDSEYE_PIPE}', '-rtsp_transport tcp -f rtsp {output}')}" for name, path in self.relays.items(): params = {"src": path, "name": name}