| 1 | /* |
| 2 | * Copyright 2026 LiveKit, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // Standalone, hardware-free unit test for the Jetson plane-layout helpers. |
| 18 | // It depends on nothing but the C++ standard library, so it can be built and |
| 19 | // run on any host (no Jetson Multimedia API required): |
| 20 | // |
| 21 | // c++ -std=c++17 -I ../src/jetson \ |
| 22 | // jetson_plane_layout_test.cc ../src/jetson/jetson_plane_layout.cpp \ |
| 23 | // -o jetson_plane_layout_test && ./jetson_plane_layout_test |
| 24 | |
| 25 | #include "jetson_plane_layout.h" |
| 26 | |
| 27 | #include <cstdio> |
| 28 | #include <cstdlib> |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | int g_failures = 0; |
| 33 | |
| 34 | void ExpectEq(int expected, int actual, const char* what) { |
| 35 | if (expected != actual) { |
| 36 | std::fprintf(stderr, "FAIL: %s: expected %d, got %d\n", what, expected, |
| 37 | actual); |
| 38 | ++g_failures; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | using livekit::PlaneLayoutHints; |
| 43 | using livekit::ResolvePlaneHeight; |
| 44 | using livekit::ResolvePlaneStride; |
| 45 | |
| 46 | void TestStridePrefersFmtStride() { |
| 47 | PlaneLayoutHints h; |
| 48 | h.fmt_stride = 1024; |
| 49 | h.probed_pitch = 2048; // Should be ignored when fmt_stride is sane. |
| 50 | h.plane_length = 1024 * 720; |
| 51 | h.fmt_height = 720; |
| 52 | ExpectEq(1024, ResolvePlaneStride(h, 720, /*min*/ 640, /*fallback*/ 640), |
| 53 | "prefers fmt_stride"); |
| 54 | } |
| 55 | |
| 56 | void TestStrideFallsBackToProbedPitch() { |
| 57 | PlaneLayoutHints h; |
| 58 | h.fmt_stride = 0; // Unset in MMAP mode. |
| 59 | h.probed_pitch = 768; |
| 60 | h.plane_length = 768 * 720; |
| 61 | h.fmt_height = 720; |
| 62 | ExpectEq(768, ResolvePlaneStride(h, 720, /*min*/ 640, /*fallback*/ 640), |
| 63 | "falls back to probed pitch"); |
| 64 | } |
| 65 | |
| 66 | void TestStrideDerivesFromLengthAndFmtHeight() { |
| 67 | PlaneLayoutHints h; |
| 68 | h.fmt_stride = 0; |
| 69 | h.probed_pitch = 0; |
| 70 | h.plane_length = 768 * 720; // -> 768 per row |
| 71 | h.fmt_height = 720; |
| 72 | ExpectEq(768, ResolvePlaneStride(h, 720, /*min*/ 640, /*fallback*/ 640), |
| 73 | "derives from length / fmt_height"); |
| 74 | } |
| 75 | |
| 76 | void TestStrideIgnoresMisleadingFmtHeight() { |
| 77 | // UV plane reporting full luma height would derive pitch/2 (an under-stride), |
| 78 | // which must be rejected in favour of expected_height. |
| 79 | PlaneLayoutHints h; |
| 80 | h.fmt_stride = 0; |
| 81 | h.probed_pitch = 0; |
| 82 | h.plane_length = 384 * 360; // chroma plane: 384 stride * 360 rows |
| 83 | h.fmt_height = 720; // misleading: full luma height -> derives 192 |
| 84 | ExpectEq(384, ResolvePlaneStride(h, /*expected_height*/ 360, /*min*/ 320, |
| 85 | /*fallback*/ 320), |
| 86 | "ignores misleading fmt_height under-stride"); |
| 87 | } |
| 88 | |
| 89 | void TestStrideUsesFallbackWhenNothingReliable() { |
| 90 | PlaneLayoutHints h; // all zero |
| 91 | ExpectEq(640, ResolvePlaneStride(h, 720, /*min*/ 640, /*fallback*/ 640), |
| 92 | "uses fallback"); |
| 93 | } |
| 94 | |
| 95 | void TestStrideClampsUpToMinStride() { |
| 96 | PlaneLayoutHints h; // all zero; fallback below min |
| 97 | ExpectEq(640, ResolvePlaneStride(h, 720, /*min*/ 640, /*fallback*/ 100), |
| 98 | "clamps up to min_stride"); |
| 99 | } |
| 100 | |
| 101 | void TestStrideCapsDerivedToAllocation() { |
| 102 | // A derived stride that exceeds what the allocation can hold (here because |
| 103 | // fmt_height under-reports the true row count) is capped to |
| 104 | // plane_length / expected_height. Note: the cap intentionally does not apply |
| 105 | // to the fmt_stride / probed_pitch early-return paths. |
| 106 | PlaneLayoutHints h; |
| 107 | h.fmt_stride = 0; |
| 108 | h.probed_pitch = 0; |
| 109 | h.plane_length = 800 * 720; // max_stride == 800 at expected_height 720 |
| 110 | h.fmt_height = 600; // derives 960, which must be capped to 800 |
| 111 | ExpectEq(800, ResolvePlaneStride(h, /*expected_height*/ 720, /*min*/ 640, |
| 112 | /*fallback*/ 640), |
| 113 | "caps derived stride to allocation max_stride"); |
| 114 | } |
| 115 | |
| 116 | void TestHeightUsesProbeWhenValid() { |
| 117 | ExpectEq(736, ResolvePlaneHeight(/*have_probe*/ true, 736, 720), |
| 118 | "uses probed height when >= expected"); |
| 119 | } |
| 120 | |
| 121 | void TestHeightFallsBackWhenProbeMissingOrSmall() { |
| 122 | ExpectEq(720, ResolvePlaneHeight(/*have_probe*/ false, 736, 720), |
| 123 | "ignores probe when unavailable"); |
| 124 | ExpectEq(720, ResolvePlaneHeight(/*have_probe*/ true, 700, 720), |
| 125 | "ignores probe smaller than expected"); |
| 126 | } |
| 127 | |
| 128 | } // namespace |
| 129 | |
| 130 | int main() { |
| 131 | TestStridePrefersFmtStride(); |
| 132 | TestStrideFallsBackToProbedPitch(); |
| 133 | TestStrideDerivesFromLengthAndFmtHeight(); |
| 134 | TestStrideIgnoresMisleadingFmtHeight(); |
| 135 | TestStrideUsesFallbackWhenNothingReliable(); |
| 136 | TestStrideClampsUpToMinStride(); |
| 137 | TestStrideCapsDerivedToAllocation(); |
| 138 | TestHeightUsesProbeWhenValid(); |
| 139 | TestHeightFallsBackWhenProbeMissingOrSmall(); |
| 140 | |
| 141 | if (g_failures != 0) { |
| 142 | std::fprintf(stderr, "%d test(s) failed\n", g_failures); |
| 143 | return EXIT_FAILURE; |
| 144 | } |
| 145 | std::printf("All jetson_plane_layout tests passed\n"); |
| 146 | return EXIT_SUCCESS; |
| 147 | } |