From 52583f5c71d842514eb0518cfd98a4e4473ff79d Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Fri, 12 Jul 2024 19:02:20 -0500 Subject: [PATCH] Consolidate decoder checks to reduce code duplication --- app/streaming/video/ffmpeg.cpp | 72 ++++++++++++---------------------- app/streaming/video/ffmpeg.h | 2 +- 2 files changed, 27 insertions(+), 47 deletions(-) diff --git a/app/streaming/video/ffmpeg.cpp b/app/streaming/video/ffmpeg.cpp index b6f8092f..751d06d4 100644 --- a/app/streaming/video/ffmpeg.cpp +++ b/app/streaming/video/ffmpeg.cpp @@ -1,6 +1,5 @@ #include #include "ffmpeg.h" -#include "streaming/streamutils.h" #include "streaming/session.h" #include @@ -1181,24 +1180,6 @@ bool FFmpegVideoDecoder::tryInitializeRendererForUnknownDecoder(const AVCodec* d return false; } -bool FFmpegVideoDecoder::isDecoderIgnored(const AVCodec *decoder) -{ - Q_UNUSED(decoder); - -#if defined(HAVE_MMAL) && !defined(ALLOW_EGL_WITH_MMAL) - // Only enable V4L2M2M by default on non-MMAL (RPi) builds. The performance - // of the V4L2M2M wrapper around MMAL is not enough for 1080p 60 FPS, so we - // would rather show the missing hardware acceleration warning when the user - // is in Full KMS mode rather than try to use a poorly performing hwaccel. - // See discussion on https://github.com/jc-kynesim/rpi-ffmpeg/pull/25 - if (strcmp(decoder->name, "h264_v4l2m2m") == 0) { - return true; - } -#endif - - return false; -} - int FFmpegVideoDecoder::getAVCodecCapabilities(const AVCodec *codec) { int caps = codec->capabilities; @@ -1215,6 +1196,26 @@ int FFmpegVideoDecoder::getAVCodecCapabilities(const AVCodec *codec) return caps; } +bool FFmpegVideoDecoder::isDecoderMatchForParams(const AVCodec *decoder, PDECODER_PARAMETERS params) +{ + SDL_assert(params->videoFormat & (VIDEO_FORMAT_MASK_H264 | VIDEO_FORMAT_MASK_H265 | VIDEO_FORMAT_MASK_AV1)); + +#if defined(HAVE_MMAL) && !defined(ALLOW_EGL_WITH_MMAL) + // Only enable V4L2M2M by default on non-MMAL (RPi) builds. The performance + // of the V4L2M2M wrapper around MMAL is not enough for 1080p 60 FPS, so we + // would rather show the missing hardware acceleration warning when the user + // is in Full KMS mode rather than try to use a poorly performing hwaccel. + // See discussion on https://github.com/jc-kynesim/rpi-ffmpeg/pull/25 + if (strcmp(decoder->name, "h264_v4l2m2m") == 0) { + return false; + } +#endif + + return ((params->videoFormat & VIDEO_FORMAT_MASK_H264) && decoder->id == AV_CODEC_ID_H264) || + ((params->videoFormat & VIDEO_FORMAT_MASK_H265) && decoder->id == AV_CODEC_ID_HEVC) || + ((params->videoFormat & VIDEO_FORMAT_MASK_AV1) && decoder->id == AV_CODEC_ID_AV1); +} + bool FFmpegVideoDecoder::tryInitializeHwAccelDecoder(PDECODER_PARAMETERS params, int pass, QSet& terminallyFailedHardwareDecoders) { const AVCodec* decoder; @@ -1230,10 +1231,8 @@ bool FFmpegVideoDecoder::tryInitializeHwAccelDecoder(PDECODER_PARAMETERS params, continue; } - // Skip decoders that don't match our codec - if (((params->videoFormat & VIDEO_FORMAT_MASK_H264) && decoder->id != AV_CODEC_ID_H264) || - ((params->videoFormat & VIDEO_FORMAT_MASK_H265) && decoder->id != AV_CODEC_ID_HEVC) || - ((params->videoFormat & VIDEO_FORMAT_MASK_AV1) && decoder->id != AV_CODEC_ID_AV1)) { + // Skip decoders that don't match our decoding parameters + if (!isDecoderMatchForParams(decoder, params)) { continue; } @@ -1242,11 +1241,6 @@ bool FFmpegVideoDecoder::tryInitializeHwAccelDecoder(PDECODER_PARAMETERS params, continue; } - // Skip ignored decoders - if (isDecoderIgnored(decoder)) { - continue; - } - // Skip hardware decoders that have returned a terminal failure status if (terminallyFailedHardwareDecoders.contains(decoder)) { continue; @@ -1297,10 +1291,8 @@ bool FFmpegVideoDecoder::tryInitializeNonHwAccelDecoder(PDECODER_PARAMETERS para continue; } - // Skip decoders that don't match our codec - if (((params->videoFormat & VIDEO_FORMAT_MASK_H264) && decoder->id != AV_CODEC_ID_H264) || - ((params->videoFormat & VIDEO_FORMAT_MASK_H265) && decoder->id != AV_CODEC_ID_HEVC) || - ((params->videoFormat & VIDEO_FORMAT_MASK_AV1) && decoder->id != AV_CODEC_ID_AV1)) { + // Skip decoders that don't match our decoding parameters + if (!isDecoderMatchForParams(decoder, params)) { continue; } @@ -1309,11 +1301,6 @@ bool FFmpegVideoDecoder::tryInitializeNonHwAccelDecoder(PDECODER_PARAMETERS para continue; } - // Skip ignored decoders - if (isDecoderIgnored(decoder)) { - continue; - } - // Skip decoders without zero-copy output formats if requested if (requireZeroCopyFormat) { bool foundZeroCopyFormat = false; @@ -1449,10 +1436,8 @@ bool FFmpegVideoDecoder::initialize(PDECODER_PARAMETERS params) continue; } - // Skip decoders that don't match our codec - if (((params->videoFormat & VIDEO_FORMAT_MASK_H264) && decoder->id != AV_CODEC_ID_H264) || - ((params->videoFormat & VIDEO_FORMAT_MASK_H265) && decoder->id != AV_CODEC_ID_HEVC) || - ((params->videoFormat & VIDEO_FORMAT_MASK_AV1) && decoder->id != AV_CODEC_ID_AV1)) { + // Skip decoders that don't match our decoding parameters + if (!isDecoderMatchForParams(decoder, params)) { continue; } @@ -1466,11 +1451,6 @@ bool FFmpegVideoDecoder::initialize(PDECODER_PARAMETERS params) continue; } - // Skip ignored decoders - if (isDecoderIgnored(decoder)) { - continue; - } - // Try this decoder without hwaccel if (tryInitializeRendererForUnknownDecoder(decoder, params, false)) { return true; diff --git a/app/streaming/video/ffmpeg.h b/app/streaming/video/ffmpeg.h index 3935333f..2dd387fa 100644 --- a/app/streaming/video/ffmpeg.h +++ b/app/streaming/video/ffmpeg.h @@ -46,7 +46,7 @@ private: bool createFrontendRenderer(PDECODER_PARAMETERS params, bool useAlternateFrontend); static - bool isDecoderIgnored(const AVCodec* decoder); + bool isDecoderMatchForParams(const AVCodec *decoder, PDECODER_PARAMETERS params); static bool isZeroCopyFormat(AVPixelFormat format);