|
18 | 18 | #include "zm_catch2.h" |
19 | 19 |
|
20 | 20 | #include "zm_ffmpeg.h" |
| 21 | +#include "zm_image.h" |
21 | 22 | #include "zm_swscale.h" |
22 | 23 |
|
23 | 24 | #include <cstdlib> |
| 25 | +#include <cstring> |
24 | 26 | #include <vector> |
25 | 27 |
|
| 28 | +namespace { |
| 29 | + |
| 30 | +// libav emits "deprecated pixel format used, make sure you did set range |
| 31 | +// correctly" whenever a context is built with a YUVJ* format. Because swscale |
| 32 | +// rewrites that format inside the context, sws_getCachedContext() then never |
| 33 | +// matches its cache and rebuilds the context on every single frame - the |
| 34 | +// warning appearing once per frame in zmc_*.log is how that shows up. Count |
| 35 | +// the warnings to assert we never hand a deprecated format to swscale. |
| 36 | +int deprecated_format_warnings = 0; |
| 37 | + |
| 38 | +void counting_log_callback(void *, int, const char *fmt, va_list) { |
| 39 | + if (fmt and strstr(fmt, "deprecated pixel format")) deprecated_format_warnings++; |
| 40 | +} |
| 41 | + |
| 42 | +// Installs the counting callback for the lifetime of the test case. |
| 43 | +class LogCounter { |
| 44 | + public: |
| 45 | + LogCounter() { |
| 46 | + deprecated_format_warnings = 0; |
| 47 | + av_log_set_callback(counting_log_callback); |
| 48 | + } |
| 49 | + ~LogCounter() { av_log_set_callback(av_log_default_callback); } |
| 50 | + int count() const { return deprecated_format_warnings; } |
| 51 | +}; |
| 52 | + |
| 53 | +} // namespace |
| 54 | + |
26 | 55 | TEST_CASE("pix_fmt_is_jpeg_range identifies full-range YUVJ formats", "[swscale]") { |
27 | 56 | REQUIRE(pix_fmt_is_jpeg_range(AV_PIX_FMT_YUVJ420P)); |
28 | 57 | REQUIRE(pix_fmt_is_jpeg_range(AV_PIX_FMT_YUVJ422P)); |
@@ -62,3 +91,71 @@ TEST_CASE("SWScale treats YUVJ420P input as full range when converting to RGB", |
62 | 91 | REQUIRE(std::abs(static_cast<int>(out[0]) - static_cast<int>(out[1])) <= 2); |
63 | 92 | REQUIRE(std::abs(static_cast<int>(out[1]) - static_cast<int>(out[2])) <= 2); |
64 | 93 | } |
| 94 | + |
| 95 | +// The mjpeg encode paths (zms, event snapshots) convert INTO YUVJ420P. The |
| 96 | +// output end needs the same treatment as the input end: with swscale left on |
| 97 | +// its default limited output range, mid grey RGB 128 lands at Y=126 instead of |
| 98 | +// Y=128 and the encoded jpeg comes out with a compressed contrast range. |
| 99 | +TEST_CASE("SWScale treats YUVJ420P output as full range when converting from RGB", "[swscale]") { |
| 100 | + const int w = 16, h = 16; |
| 101 | + const uint8_t grey = 128; |
| 102 | + |
| 103 | + std::vector<uint8_t> in(SWScale::GetBufferSize(AV_PIX_FMT_RGB24, w, h, 1), grey); |
| 104 | + std::vector<uint8_t> out(SWScale::GetBufferSize(AV_PIX_FMT_YUVJ420P, w, h, 1), 0); |
| 105 | + |
| 106 | + SWScale scaler; |
| 107 | + REQUIRE(scaler.init()); |
| 108 | + int r = scaler.Convert(in.data(), in.size(), out.data(), out.size(), |
| 109 | + AV_PIX_FMT_RGB24, AV_PIX_FMT_YUVJ420P, w, h, 1, 1); |
| 110 | + REQUIRE(r == 0); |
| 111 | + |
| 112 | + // Full range: Y == the RGB level. Limited range would scale it to ~126. |
| 113 | + REQUIRE(out[0] >= 127); |
| 114 | + REQUIRE(out[0] <= 129); |
| 115 | +} |
| 116 | + |
| 117 | +TEST_CASE("SWScale does not hand deprecated pixel formats to swscale", "[swscale]") { |
| 118 | + const int w = 16, h = 16; |
| 119 | + LogCounter counter; |
| 120 | + |
| 121 | + std::vector<uint8_t> yuvj(SWScale::GetBufferSize(AV_PIX_FMT_YUVJ420P, w, h, 1), 128); |
| 122 | + std::vector<uint8_t> rgb(SWScale::GetBufferSize(AV_PIX_FMT_RGB24, w, h, 1), 0); |
| 123 | + |
| 124 | + SWScale scaler; |
| 125 | + REQUIRE(scaler.init()); |
| 126 | + REQUIRE(scaler.Convert(yuvj.data(), yuvj.size(), rgb.data(), rgb.size(), |
| 127 | + AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_RGB24, w, h, 1, 1) == 0); |
| 128 | + REQUIRE(scaler.Convert(rgb.data(), rgb.size(), yuvj.data(), yuvj.size(), |
| 129 | + AV_PIX_FMT_RGB24, AV_PIX_FMT_YUVJ420P, w, h, 1, 1) == 0); |
| 130 | + |
| 131 | + REQUIRE(counter.count() == 0); |
| 132 | +} |
| 133 | + |
| 134 | +// Regression: an MJPEG monitor decodes to YUVJ422P and its Image is YUVJ422P |
| 135 | +// too, so Image::Assign should take the av_image_copy fast path. Fixing only |
| 136 | +// the source format left the identity check comparing YUV422P against |
| 137 | +// YUVJ422P: every frame fell through to swscale, which rebuilt its context and |
| 138 | +// logged the deprecated-format warning 15+ times a second per monitor. |
| 139 | +TEST_CASE("Image::Assign of a matching YUVJ frame copies without swscale", "[swscale]") { |
| 140 | + const int w = 32, h = 16; |
| 141 | + config.font_file_location = "data/fonts/04_valid.zmfnt"; |
| 142 | + |
| 143 | + av_frame_ptr frame{av_frame_alloc()}; |
| 144 | + REQUIRE(frame); |
| 145 | + frame->width = w; |
| 146 | + frame->height = h; |
| 147 | + frame->format = AV_PIX_FMT_YUVJ422P; |
| 148 | + REQUIRE(av_frame_get_buffer(frame.get(), 32) == 0); |
| 149 | + // Full-range white: a limited-range conversion would pull this down to 235. |
| 150 | + memset(frame->data[0], 255, frame->linesize[0] * h); |
| 151 | + memset(frame->data[1], 128, frame->linesize[1] * h); |
| 152 | + memset(frame->data[2], 128, frame->linesize[2] * h); |
| 153 | + |
| 154 | + Image image(frame.get()); |
| 155 | + REQUIRE(image.PixFormat() == AV_PIX_FMT_YUVJ422P); |
| 156 | + |
| 157 | + LogCounter counter; |
| 158 | + REQUIRE(image.Assign(frame.get())); |
| 159 | + REQUIRE(image.Buffer()[0] == 255); |
| 160 | + REQUIRE(counter.count() == 0); |
| 161 | +} |
0 commit comments