fix: the decoder keeps its colour converter only while the frame format is unchanged refs #7500 (#7501)

This commit is contained in:
Jash Ambaliya authored and GitHub committed 2026-11-26 18:23:45 -05:00
1 parent 2be5c41a61
commit fe254ff3d3
3 files changed
+100 -3

No files matched your search

+6 -3
View File
@@ -3504,7 +3504,10 @@ bool Monitor::setupConvertContext(const AVFrame *input_frame, const Image *image
AVPixelFormat origPixFormat = (AVPixelFormat)input_frame->format;
AVPixelFormat inputPixFormat = fix_deprecated_pix_fmt(origPixFormat);
convert_context = sws_getContext(
// Called for every decoded frame: keeps the context while the frame's size and
// format are unchanged and rebuilds it when the stream changes them.
convert_context = sws_getCachedContext(
convert_context,
input_frame->width,
input_frame->height,
inputPixFormat,
@@ -3517,7 +3520,7 @@ bool Monitor::setupConvertContext(const AVFrame *input_frame, const Image *image
av_get_pix_fmt_name(imagePixFormat)
);
} else {
Debug(1, "Setup conversion context for %dx%d %s to %dx%d %s",
Debug(3, "Setup conversion context for %dx%d %s to %dx%d %s",
input_frame->width, input_frame->height,
av_get_pix_fmt_name(inputPixFormat),
image->Width(), image->Height(),
@@ -4025,7 +4028,7 @@ bool Monitor::Decode() {
packet->image = new Image(camera_width, camera_height, native_colours, native_subpixelorder);
bool have_converter = convert_context || setupConvertContext(packet->in_frame.get(), packet->image);
bool have_converter = setupConvertContext(packet->in_frame.get(), packet->image);
if (have_converter) {
if (!packet->image->Assign(packet->in_frame.get(), convert_context)) {
delete packet->image;
+1
View File
@@ -47,6 +47,7 @@ set(TEST_SOURCES
zm_monitor_ondemand.cpp
zm_monitor_privacy.cpp
zm_monitor_substitute.cpp
zm_monitor_convert_context.cpp
zm_monitorlink_expression.cpp
zm_monitorstream.cpp
zm_monitorlink_token.cpp
+93
View File
@@ -0,0 +1,93 @@
/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "zm_catch2.h"
#include "zm_config.h"
#include "zm_image.h"
#include "zm_monitor.h"
#include <memory>
extern "C" {
#include <libavutil/opt.h>
}
// Monitor::Decode calls setupConvertContext() for every decoded frame. A stream
// whose pixel format changes mid-stream (MJPEG alternating yuvj422p/yuvj444p) must
// get a converter for the new format, and an unchanged stream must keep its own.
namespace {
class ConvertMonitor : public Monitor {
public:
ConvertMonitor() : Monitor() {}
SwsContext *Ctx() const { return convert_context; }
};
struct FrameDeleter {
void operator()(AVFrame *f) const { av_frame_free(&f); }
};
std::unique_ptr<AVFrame, FrameDeleter> MakeFrame(AVPixelFormat fmt, int w, int h) {
std::unique_ptr<AVFrame, FrameDeleter> frame(av_frame_alloc());
frame->format = fmt;
frame->width = w;
frame->height = h;
REQUIRE(av_frame_get_buffer(frame.get(), 0) == 0);
return frame;
}
int64_t SrcFormat(SwsContext *ctx) {
// The option is "src_format" in newer libswscale and "srcformat" before.
int64_t v = -1;
if (av_opt_get_int(ctx, "src_format", 0, &v) < 0)
REQUIRE(av_opt_get_int(ctx, "srcformat", 0, &v) >= 0);
return v;
}
} // namespace
TEST_CASE("Monitor::setupConvertContext keeps the converter while the frame format is unchanged", "[Monitor]") {
if (!config.font_file_location) config.font_file_location = "";
auto monitor = std::make_shared<ConvertMonitor>();
Image image(64, 48, ZM_COLOUR_GRAY8, ZM_SUBPIX_ORDER_YUV420P);
auto frame = MakeFrame(AV_PIX_FMT_YUVJ422P, 64, 48);
REQUIRE(monitor->setupConvertContext(frame.get(), &image));
SwsContext *first = monitor->Ctx();
REQUIRE(first != nullptr);
REQUIRE(monitor->setupConvertContext(frame.get(), &image));
REQUIRE(monitor->Ctx() == first);
}
TEST_CASE("Monitor::setupConvertContext follows a pixel format change in the stream", "[Monitor]") {
if (!config.font_file_location) config.font_file_location = "";
auto monitor = std::make_shared<ConvertMonitor>();
Image image(64, 48, ZM_COLOUR_GRAY8, ZM_SUBPIX_ORDER_YUV420P);
auto f422 = MakeFrame(AV_PIX_FMT_YUVJ422P, 64, 48);
REQUIRE(monitor->setupConvertContext(f422.get(), &image));
REQUIRE(SrcFormat(monitor->Ctx()) == AV_PIX_FMT_YUV422P);
auto f444 = MakeFrame(AV_PIX_FMT_YUVJ444P, 64, 48);
REQUIRE(monitor->setupConvertContext(f444.get(), &image));
REQUIRE(SrcFormat(monitor->Ctx()) == AV_PIX_FMT_YUV444P);
auto f420 = MakeFrame(AV_PIX_FMT_YUVJ420P, 64, 48);
REQUIRE(monitor->setupConvertContext(f420.get(), &image));
REQUIRE(SrcFormat(monitor->Ctx()) == AV_PIX_FMT_YUV420P);
}