| 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 | #ifndef LIVEKIT_JETSON_PLANE_LAYOUT_H_ |
| 18 | #define LIVEKIT_JETSON_PLANE_LAYOUT_H_ |
| 19 | |
| 20 | namespace livekit { |
| 21 | |
| 22 | /// Plain-data view of everything known about a destination plane's memory |
| 23 | /// layout at copy time. |
| 24 | /// |
| 25 | /// These values are sourced from (sometimes unreliable) Jetson MMAPI / |
| 26 | /// `NvBufSurface` metadata, but the struct is intentionally free of any |
| 27 | /// hardware types so that the resolution logic below is pure and unit-testable. |
| 28 | struct PlaneLayoutHints { |
| 29 | /// `NvBuffer::NvBufferPlane::fmt.stride`. Often `0`/unset in MMAP mode. |
| 30 | int fmt_stride = 0; |
| 31 | /// Pitch reported by `NvBufSurfaceFromFd` for this plane's fd, or `0` when the |
| 32 | /// probe is unavailable/unsupported on the running JetPack version. |
| 33 | int probed_pitch = 0; |
| 34 | /// Byte length of the mapped plane, or `0` when unknown. |
| 35 | int plane_length = 0; |
| 36 | /// `NvBuffer::NvBufferPlane::fmt.height`. May be misleading (e.g. a UV plane |
| 37 | /// reporting full luma height), so it is only trusted as a divisor when it |
| 38 | /// yields a stride that meets `min_stride`. |
| 39 | int fmt_height = 0; |
| 40 | }; |
| 41 | |
| 42 | /// Resolves the destination stride (bytes per row) to use when writing a plane. |
| 43 | /// |
| 44 | /// `expected_height` is the number of rows that will be written, `min_stride` |
| 45 | /// the hard lower bound (typically the plane width in bytes), and `fallback` |
| 46 | /// the value to use when no reliable source is available. |
| 47 | /// |
| 48 | /// Preference order, each accepted only when it is at least `min_stride`: |
| 49 | /// [`PlaneLayoutHints::fmt_stride`] -> [`PlaneLayoutHints::probed_pitch`] -> |
| 50 | /// stride derived from [`PlaneLayoutHints::plane_length`] -> `fallback`. The |
| 51 | /// result is clamped to `min_stride` and, when derivable, to |
| 52 | /// `plane_length / expected_height` so the caller never under- or over-strides |
| 53 | /// the mapped allocation. |
| 54 | int ResolvePlaneStride(const PlaneLayoutHints& hints, |
| 55 | int expected_height, |
| 56 | int min_stride, |
| 57 | int fallback); |
| 58 | |
| 59 | /// Resolves the number of rows the destination plane can hold. |
| 60 | /// |
| 61 | /// Uses `probed_height` only when `have_probe` is set and the probed value is at |
| 62 | /// least `expected_height`; otherwise returns `expected_height`. |
| 63 | int ResolvePlaneHeight(bool have_probe, int probed_height, int expected_height); |
| 64 | |
| 65 | } // namespace livekit |
| 66 | |
| 67 | #endif // LIVEKIT_JETSON_PLANE_LAYOUT_H_ |