diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 75536e6a95..3dd442e08c 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -3617,6 +3617,20 @@ size_t Monitor::DecoderQueueLimit(int max_keyframe_interval, const AVCodecContex return 2 * static_cast(max_keyframe_interval) + delay; } +bool Monitor::CanPassthroughFormat(AVPixelFormat format, Orientation orientation) { + switch (format) { + case AV_PIX_FMT_YUV422P: + case AV_PIX_FMT_YUVJ422P: + return orientation != ROTATE_90 && orientation != ROTATE_270; + case AV_PIX_FMT_YUV420P: + case AV_PIX_FMT_YUVJ420P: + case AV_PIX_FMT_GRAY8: + return true; + default: + return zm_is_rgb24(format) || zm_is_rgb32(format); + } +} + bool Monitor::SkipUntilKeyframe(bool keyframe) { if (!decoder_needs_keyframe) return false; if (!keyframe) return true; @@ -3876,15 +3890,7 @@ bool Monitor::Decode() { const char *native_fmt_name = av_get_pix_fmt_name(native_fmt); if (!native_fmt_name) native_fmt_name = "unknown"; - bool can_passthrough = (native_fmt == AV_PIX_FMT_YUV420P - || native_fmt == AV_PIX_FMT_YUVJ420P - || native_fmt == AV_PIX_FMT_YUV422P - || native_fmt == AV_PIX_FMT_YUVJ422P - || native_fmt == AV_PIX_FMT_GRAY8 - || zm_is_rgb24(native_fmt) - || zm_is_rgb32(native_fmt)); - - if (can_passthrough && zm_colours_from_pixformat(native_fmt, native_colours, native_subpixelorder)) { + if (CanPassthroughFormat(native_fmt, orientation) && zm_colours_from_pixformat(native_fmt, native_colours, native_subpixelorder)) { Debug(1, "Using native frame format %s", native_fmt_name); } else { Debug(1, "Converting %s to yuv420p for pipeline", native_fmt_name); diff --git a/src/zm_monitor.h b/src/zm_monitor.h index 4125f136e2..4db30aeed0 100644 --- a/src/zm_monitor.h +++ b/src/zm_monitor.h @@ -867,6 +867,8 @@ class Monitor : public std::enable_shared_from_this { // Whether a packet must be held back from the codec while it waits for a // keyframe; clears the wait once the keyframe comes. bool SkipUntilKeyframe(bool keyframe); + // A quarter turn needs 4:2:0 chroma, so 4:2:2 is converted when rotating. + static bool CanPassthroughFormat(AVPixelFormat format, Orientation orientation); packetqueue_iterator *analysis_it; std::unique_ptr analysis_thread; packetqueue_iterator *decoder_it; diff --git a/tests/zm_monitor_decoder_queue.cpp b/tests/zm_monitor_decoder_queue.cpp index 017a83a6ce..d1abf81b43 100644 --- a/tests/zm_monitor_decoder_queue.cpp +++ b/tests/zm_monitor_decoder_queue.cpp @@ -35,6 +35,7 @@ class DecoderQueue : public Monitor { public: DecoderQueue() : Monitor() {} using Monitor::DecoderQueueLimit; + using Monitor::CanPassthroughFormat; // Packets sent to the codec, in decode order, by pts. std::vector> Send(const std::vector &pts) { @@ -177,3 +178,21 @@ TEST_CASE("Monitor decoder queue: after a flush nothing is sent until a keyframe // The wait is over once the keyframe went to the codec CHECK_FALSE(q.Skip(false)); } + +TEST_CASE("Monitor::CanPassthroughFormat keeps 4:2:2 out of quarter turns", "[Monitor]") { + for (Monitor::Orientation o : {Monitor::ROTATE_0, Monitor::ROTATE_180, Monitor::FLIP_HORI, Monitor::FLIP_VERT}) { + CHECK(DecoderQueue::CanPassthroughFormat(AV_PIX_FMT_YUV422P, o)); + CHECK(DecoderQueue::CanPassthroughFormat(AV_PIX_FMT_YUVJ422P, o)); + } + for (Monitor::Orientation o : {Monitor::ROTATE_90, Monitor::ROTATE_270}) { + CHECK_FALSE(DecoderQueue::CanPassthroughFormat(AV_PIX_FMT_YUV422P, o)); + CHECK_FALSE(DecoderQueue::CanPassthroughFormat(AV_PIX_FMT_YUVJ422P, o)); + CHECK(DecoderQueue::CanPassthroughFormat(AV_PIX_FMT_YUV420P, o)); + CHECK(DecoderQueue::CanPassthroughFormat(AV_PIX_FMT_YUVJ420P, o)); + CHECK(DecoderQueue::CanPassthroughFormat(AV_PIX_FMT_GRAY8, o)); + CHECK(DecoderQueue::CanPassthroughFormat(AV_PIX_FMT_RGB24, o)); + CHECK(DecoderQueue::CanPassthroughFormat(AV_PIX_FMT_BGRA, o)); + } + CHECK_FALSE(DecoderQueue::CanPassthroughFormat(AV_PIX_FMT_YUV444P, Monitor::ROTATE_0)); + CHECK_FALSE(DecoderQueue::CanPassthroughFormat(AV_PIX_FMT_YUV420P10LE, Monitor::ROTATE_0)); +}