From fedbe58e70202940e6a776f066a9798312571da1 Mon Sep 17 00:00:00 2001 From: Nick Mowen Date: Wed, 28 Dec 2022 11:13:13 -0700 Subject: [PATCH] Use hardware accelerated scaling when hwaccel preset is set --- frigate/config.py | 19 +++++------ frigate/ffmpeg_presets.py | 69 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 13 deletions(-) diff --git a/frigate/config.py b/frigate/config.py index 5469fbd16..5b6fb8c6a 100644 --- a/frigate/config.py +++ b/frigate/config.py @@ -27,7 +27,8 @@ from frigate.util import ( load_labels, ) from frigate.ffmpeg_presets import ( - parse_preset_hardware_acceleration, + parse_preset_hardware_acceleration_decode, + parse_preset_hardware_acceleration_scale, parse_preset_input, parse_preset_output_record, parse_preset_output_rtmp, @@ -626,14 +627,10 @@ class CameraConfig(FrigateBaseModel): ffmpeg_output_args = [] if "detect" in ffmpeg_input.roles: detect_args = get_ffmpeg_arg_list(self.ffmpeg.output_args.detect) + scale_args = parse_preset_hardware_acceleration_scale(ffmpeg_input.hwaccel_args, self.detect.fps, self.detect.width, self.detect.height) ffmpeg_output_args = ( - [ - "-r", - str(self.detect.fps), - "-s", - f"{self.detect.width}x{self.detect.height}", - ] + scale_args + detect_args + ffmpeg_output_args + ["pipe:"] @@ -667,12 +664,14 @@ class CameraConfig(FrigateBaseModel): ffmpeg_input.global_args or self.ffmpeg.global_args ) hwaccel_args = get_ffmpeg_arg_list( - ffmpeg_input.hwaccel_args - or parse_preset_hardware_acceleration(self.ffmpeg.hwaccel_args) + parse_preset_hardware_acceleration_decode(ffmpeg_input.hwaccel_args) + or ffmpeg_input.hwaccel_args + or parse_preset_hardware_acceleration_decode(self.ffmpeg.hwaccel_args) or self.ffmpeg.hwaccel_args ) input_args = get_ffmpeg_arg_list( - ffmpeg_input.input_args + parse_preset_input(ffmpeg_input.input_args, self.detect.fps) + or ffmpeg_input.input_args or parse_preset_input(self.ffmpeg.input_args, self.detect.fps) or self.ffmpeg.input_args ) diff --git a/frigate/ffmpeg_presets.py b/frigate/ffmpeg_presets.py index af6d86337..9ffd4e022 100644 --- a/frigate/ffmpeg_presets.py +++ b/frigate/ffmpeg_presets.py @@ -9,7 +9,7 @@ _user_agent_args = [ f"FFmpeg Frigate/{VERSION}", ] -PRESETS_HW_ACCEL = { +PRESETS_HW_ACCEL_DECODE = { "preset-rpi-32-h264": ["-c:v", "h264_v4l2m2m"], "preset-rpi-64-h264": ["-c:v", "h264_v4l2m2m"], "preset-intel-vaapi": [ @@ -35,13 +35,76 @@ PRESETS_HW_ACCEL = { "preset-nvidia-mjpeg": ["-c:v", "mjpeg_cuvid"], } +PRESETS_HW_ACCEL_SCALE = { + "preset-intel-vaapi": [ + "-vf", + "fps={},deinterlace_vaapi=rate=field:auto=1,scale_vaapi=w={}:h={},hwdownload,format=yuv420p", + "-f", + "rawvideo", + ], + "preset-intel-qsv-h264": [ + "-vf", + "vpp_qsv=framerate={}:scale_mode=1:w={}:h={}:detail=50:denoise=100:deinterlace=2:format=nv12,hwdownload,format=nv12,format=yuv420p", + "-f", + "rawvideo", + ], + "preset-intel-qsv-h265": [ + "-vf", + "vpp_qsv=framerate={}:scale_mode=1:w={}:h={}:detail=50:denoise=100:deinterlace=2:format=nv12,hwdownload,format=nv12,format=yuv420p", + "-f", + "rawvideo", + ], + "preset-amd-vaapi": [ + "-vf", + "fps={},deinterlace_vaapi=rate=field:auto=1,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", + "-f", + "rawvideo", + ], + "preset-nvidia-h265": [ + "-vf", + "fps={},scale_cuda=w={}:h={}:format=nv12,hwdownload,format=nv12,format=yuv420p", + "-f", + "rawvideo", + ], + "default": [ + "-r", + "{}", + "-s", + "{}", + ], +} -def parse_preset_hardware_acceleration(arg: Any) -> list[str]: + +def parse_preset_hardware_acceleration_decode(arg: Any) -> list[str]: """Return the correct preset if in preset format otherwise return None.""" if not isinstance(arg, str): return None - return PRESETS_HW_ACCEL.get(arg, None) + return PRESETS_HW_ACCEL_DECODE.get(arg, None) + + +def parse_preset_hardware_acceleration_scale( + arg: Any, + fps: int, + width: int, + height: int, +) -> list[str]: + """Return the correct scaling preset or default preset if none is set.""" + if not isinstance(arg, str): + scale = PRESETS_HW_ACCEL_SCALE["default"] + scale[1] = str(fps) + scale[3] = f"{width}x{height}" + return scale + + scale = PRESETS_HW_ACCEL_SCALE.get(arg, PRESETS_HW_ACCEL_SCALE["default"]) + scale[1] = scale[1].format(fps, height, width) + return scale PRESETS_INPUT = {