From 13efc421c42f6e9c06430d80be02ba2a8ea50643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harald=20Sch=C3=A4fer?= Date: Mon, 19 Jan 2026 16:27:41 -0800 Subject: [PATCH] NV12 buffer shape helpers (#36683) * Give this a try * can codex debug? * simpler * Revert "simpler" This reverts commit 572335008c1c719aa985d14bd740253ff94b94a9. * better * cleanup * try again * tie * try this * try this * do tests fail without this? * doesn't seem needed * unused * don't need duplicate * passes CI? * try this * try this * try this * I don't understand this, so back to before * keep that alignment * set uv_height --------- Co-authored-by: Adeeb Shihadeh --- system/camerad/cameras/camera_common.cc | 6 +----- system/camerad/cameras/nv12_info.h | 19 +++++++++++++++++++ system/camerad/cameras/spectra.cc | 14 +++----------- system/camerad/cameras/spectra.h | 1 + tools/replay/camera.cc | 11 ++--------- tools/replay/camera.h | 2 -- 6 files changed, 26 insertions(+), 27 deletions(-) create mode 100644 system/camerad/cameras/nv12_info.h diff --git a/system/camerad/cameras/camera_common.cc b/system/camerad/cameras/camera_common.cc index 1f6ad9b4be..88bca7f775 100644 --- a/system/camerad/cameras/camera_common.cc +++ b/system/camerad/cameras/camera_common.cc @@ -26,11 +26,7 @@ void CameraBuf::init(cl_device_id device_id, cl_context context, SpectraCamera * LOGD("allocated %d CL buffers", frame_buf_count); } - // the encoder HW tells us the size it wants after setting it up. - // TODO: VENUS_BUFFER_SIZE should give the size, but it's too small. dependent on encoder settings? - size_t nv12_size = (out_img_width <= 1344 ? 2900 : 2346)*cam->stride; - - vipc_server->create_buffers_with_sizes(stream_type, VIPC_BUFFER_COUNT, out_img_width, out_img_height, nv12_size, cam->stride, cam->uv_offset); + vipc_server->create_buffers_with_sizes(stream_type, VIPC_BUFFER_COUNT, out_img_width, out_img_height, cam->yuv_size, cam->stride, cam->uv_offset); LOGD("created %d YUV vipc buffers with size %dx%d", VIPC_BUFFER_COUNT, cam->stride, cam->y_height); } diff --git a/system/camerad/cameras/nv12_info.h b/system/camerad/cameras/nv12_info.h new file mode 100644 index 0000000000..0f4aee81a0 --- /dev/null +++ b/system/camerad/cameras/nv12_info.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include +#include + +#include "third_party/linux/include/msm_media_info.h" + +// Returns NV12 aligned width, height, and buffer size for the given frame. +inline std::tuple get_nv12_info(int width, int height) { + // the encoder HW tells us the size it wants after setting it up. + // TODO: VENUS_BUFFER_SIZE should give the size, but it's too small. dependent on encoder settings? + const uint32_t nv12_width = VENUS_Y_STRIDE(COLOR_FMT_NV12, width); + const uint32_t nv12_height = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height); + assert(nv12_width == VENUS_UV_STRIDE(COLOR_FMT_NV12, width)); + assert(nv12_height / 2 == VENUS_UV_SCANLINES(COLOR_FMT_NV12, height)); + const uint32_t nv12_buffer_size = (width <= 1344 ? 2900 : 2346)*nv12_width; + return {nv12_width, nv12_height, nv12_buffer_size}; +} diff --git a/system/camerad/cameras/spectra.cc b/system/camerad/cameras/spectra.cc index 0d93b70465..20f48bb556 100644 --- a/system/camerad/cameras/spectra.cc +++ b/system/camerad/cameras/spectra.cc @@ -283,20 +283,12 @@ void SpectraCamera::camera_open(VisionIpcServer *v, cl_device_id device_id, cl_c buf.out_img_width = sensor->frame_width / sensor->out_scale; buf.out_img_height = (sensor->hdr_offset > 0 ? (sensor->frame_height - sensor->hdr_offset) / 2 : sensor->frame_height) / sensor->out_scale; - - // size is driven by all the HW that handles frames, - // the video encoder has certain alignment requirements in this case - stride = VENUS_Y_STRIDE(COLOR_FMT_NV12, buf.out_img_width); - y_height = VENUS_Y_SCANLINES(COLOR_FMT_NV12, buf.out_img_height); - uv_height = VENUS_UV_SCANLINES(COLOR_FMT_NV12, buf.out_img_height); - uv_offset = stride*y_height; - yuv_size = uv_offset + stride*uv_height; + std::tie(stride, y_height, yuv_size) = get_nv12_info(buf.out_img_width, buf.out_img_height); + uv_height = y_height / 2; + uv_offset = stride * y_height; if (cc.output_type != ISP_RAW_OUTPUT) { uv_offset = ALIGNED_SIZE(uv_offset, 0x1000); - yuv_size = uv_offset + ALIGNED_SIZE(stride*uv_height, 0x1000); } - assert(stride == VENUS_UV_STRIDE(COLOR_FMT_NV12, buf.out_img_width)); - assert(y_height/2 == uv_height); open = true; configISP(); diff --git a/system/camerad/cameras/spectra.h b/system/camerad/cameras/spectra.h index 13cb13f98f..983873c7f8 100644 --- a/system/camerad/cameras/spectra.h +++ b/system/camerad/cameras/spectra.h @@ -12,6 +12,7 @@ #include "common/util.h" #include "common/swaglog.h" #include "system/camerad/cameras/hw.h" +#include "system/camerad/cameras/nv12_info.h" #include "system/camerad/cameras/camera_common.h" #include "system/camerad/sensors/sensor.h" diff --git a/tools/replay/camera.cc b/tools/replay/camera.cc index 73243ed20d..2962195aff 100644 --- a/tools/replay/camera.cc +++ b/tools/replay/camera.cc @@ -7,18 +7,11 @@ #include "third_party/linux/include/msm_media_info.h" #include "tools/replay/util.h" +#include "system/camerad/cameras/nv12_info.h" + const int BUFFER_COUNT = 40; -std::tuple get_nv12_info(int width, int height) { - int nv12_width = VENUS_Y_STRIDE(COLOR_FMT_NV12, width); - int nv12_height = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height); - assert(nv12_width == VENUS_UV_STRIDE(COLOR_FMT_NV12, width)); - assert(nv12_height / 2 == VENUS_UV_SCANLINES(COLOR_FMT_NV12, height)); - size_t nv12_buffer_size = 2346 * nv12_width; // comes from v4l2_format.fmt.pix_mp.plane_fmt[0].sizeimage - return {nv12_width, nv12_height, nv12_buffer_size}; -} - CameraServer::CameraServer(std::pair camera_size[MAX_CAMERAS]) { for (int i = 0; i < MAX_CAMERAS; ++i) { std::tie(cameras_[i].width, cameras_[i].height) = camera_size[i]; diff --git a/tools/replay/camera.h b/tools/replay/camera.h index 21c3d98dcf..9433018848 100644 --- a/tools/replay/camera.h +++ b/tools/replay/camera.h @@ -10,8 +10,6 @@ #include "tools/replay/framereader.h" #include "tools/replay/logreader.h" -std::tuple get_nv12_info(int width, int height); - class CameraServer { public: CameraServer(std::pair camera_size[MAX_CAMERAS] = nullptr);