From e0378d41cb34923c8a5b197bcfd30cadb9c120ae Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Thu, 8 Jul 2021 15:22:00 +0200 Subject: [PATCH] Refactor camerad exposure logging (#21506) * simplify gain logging * clean up metadata * log grey fractions * those are not used * set grey fracs in message * fix qcom old-commit-hash: 47be6c751ec69d2f8957c9a1c6f7776a8bde4f7e --- cereal | 2 +- selfdrive/camerad/cameras/camera_common.cc | 6 ++++-- selfdrive/camerad/cameras/camera_common.h | 14 +++++++++++--- selfdrive/camerad/cameras/camera_frame_stream.cc | 4 +--- selfdrive/camerad/cameras/camera_qcom.cc | 12 ++++++++++-- selfdrive/camerad/cameras/camera_qcom.h | 4 ++++ selfdrive/camerad/cameras/camera_qcom2.cc | 9 +++++++-- selfdrive/camerad/cameras/camera_qcom2.h | 3 +++ selfdrive/ui/ui.cc | 6 +++--- 9 files changed, 44 insertions(+), 16 deletions(-) diff --git a/cereal b/cereal index 197912765..353d8a924 160000 --- a/cereal +++ b/cereal @@ -1 +1 @@ -Subproject commit 1979127659dc7c47c6ad7b8234ff1f6ca93526fc +Subproject commit 353d8a9249e05c1bb4d1fec7ba7ba93293166131 diff --git a/selfdrive/camerad/cameras/camera_common.cc b/selfdrive/camerad/cameras/camera_common.cc index a99c2b900..08cd37188 100644 --- a/selfdrive/camerad/cameras/camera_common.cc +++ b/selfdrive/camerad/cameras/camera_common.cc @@ -180,12 +180,14 @@ void fill_frame_data(cereal::FrameData::Builder &framed, const FrameMetadata &fr framed.setTimestampSof(frame_data.timestamp_sof); framed.setFrameLength(frame_data.frame_length); framed.setIntegLines(frame_data.integ_lines); - framed.setGlobalGain(frame_data.global_gain); + framed.setGain(frame_data.gain); + framed.setHighConversionGain(frame_data.high_conversion_gain); + framed.setMeasuredGreyFraction(frame_data.measured_grey_fraction); + framed.setTargetGreyFraction(frame_data.target_grey_fraction); framed.setLensPos(frame_data.lens_pos); framed.setLensSag(frame_data.lens_sag); framed.setLensErr(frame_data.lens_err); framed.setLensTruePos(frame_data.lens_true_pos); - framed.setGainFrac(frame_data.gain_frac); } kj::Array get_frame_image(const CameraBuf *b) { diff --git a/selfdrive/camerad/cameras/camera_common.h b/selfdrive/camerad/cameras/camera_common.h index f0f118af7..5580f1636 100644 --- a/selfdrive/camerad/cameras/camera_common.h +++ b/selfdrive/camerad/cameras/camera_common.h @@ -67,16 +67,24 @@ typedef struct LogCameraInfo { typedef struct FrameMetadata { uint32_t frame_id; + unsigned int frame_length; + + // Timestamps uint64_t timestamp_sof; // only set on tici uint64_t timestamp_eof; - unsigned int frame_length; + + // Exposure unsigned int integ_lines; - unsigned int global_gain; + bool high_conversion_gain; + float gain; + float measured_grey_fraction; + float target_grey_fraction; + + // Focus unsigned int lens_pos; float lens_sag; float lens_err; float lens_true_pos; - float gain_frac; } FrameMetadata; typedef struct CameraExpInfo { diff --git a/selfdrive/camerad/cameras/camera_frame_stream.cc b/selfdrive/camerad/cameras/camera_frame_stream.cc index ff789d848..8e6c69e36 100644 --- a/selfdrive/camerad/cameras/camera_frame_stream.cc +++ b/selfdrive/camerad/cameras/camera_frame_stream.cc @@ -56,9 +56,7 @@ void run_frame_stream(CameraState &camera, const char* frame_pkt) { camera.buf.camera_bufs_metadata[buf_idx] = { .frame_id = frame.get("frameId").as(), .timestamp_eof = frame.get("timestampEof").as(), - .frame_length = frame.get("frameLength").as(), - .integ_lines = frame.get("integLines").as(), - .global_gain = frame.get("globalGain").as(), + .timestamp_sof = frame.get("timestampSof").as(), }; cl_command_queue q = camera.buf.camera_bufs[buf_idx].copy_q; diff --git a/selfdrive/camerad/cameras/camera_qcom.cc b/selfdrive/camerad/cameras/camera_qcom.cc index 187663431..5ac90e4b3 100644 --- a/selfdrive/camerad/cameras/camera_qcom.cc +++ b/selfdrive/camerad/cameras/camera_qcom.cc @@ -282,6 +282,12 @@ static void set_exposure(CameraState *s, float exposure_frac, float gain_frac) { static void do_autoexposure(CameraState *s, float grey_frac) { const float target_grey = 0.3; + + s->frame_info_lock.lock(); + s->measured_grey_fraction = grey_frac; + s->target_grey_fraction = target_grey; + s->frame_info_lock.unlock(); + if (s->apply_exposure == ov8865_apply_exposure) { // gain limits downstream const float gain_frac_min = 0.015625; @@ -1162,12 +1168,14 @@ void cameras_run(MultiCameraState *s) { .timestamp_eof = timestamp, .frame_length = (uint32_t)c->frame_length, .integ_lines = (uint32_t)c->cur_integ_lines, - .global_gain = (uint32_t)c->cur_gain, .lens_pos = c->cur_lens_pos, .lens_sag = c->last_sag_acc_z, .lens_err = c->focus_err, .lens_true_pos = c->lens_true_pos, - .gain_frac = c->cur_gain_frac, + .gain = c->cur_gain_frac, + .measured_grey_fraction = c->measured_grey_fraction, + .target_grey_fraction = c->target_grey_fraction, + .high_conversion_gain = false, }; c->frame_metadata_idx = (c->frame_metadata_idx + 1) % METADATA_BUF_COUNT; diff --git a/selfdrive/camerad/cameras/camera_qcom.h b/selfdrive/camerad/cameras/camera_qcom.h index 684df4a37..207857e05 100644 --- a/selfdrive/camerad/cameras/camera_qcom.h +++ b/selfdrive/camerad/cameras/camera_qcom.h @@ -66,6 +66,10 @@ typedef struct CameraState { unsigned int max_gain; float cur_exposure_frac, cur_gain_frac; int cur_gain, cur_integ_lines; + + float measured_grey_fraction; + float target_grey_fraction; + std::atomic digital_gain; camera_apply_exposure_func apply_exposure; diff --git a/selfdrive/camerad/cameras/camera_qcom2.cc b/selfdrive/camerad/cameras/camera_qcom2.cc index 8194d7903..ff512e658 100644 --- a/selfdrive/camerad/cameras/camera_qcom2.cc +++ b/selfdrive/camerad/cameras/camera_qcom2.cc @@ -905,9 +905,11 @@ void handle_camera_event(CameraState *s, void *evdat) { meta_data.frame_id = main_id - s->idx_offset; meta_data.timestamp_sof = timestamp; s->exp_lock.lock(); - meta_data.global_gain = s->analog_gain + (100*s->dc_gain_enabled); - meta_data.gain_frac = s->analog_gain_frac; + meta_data.gain = s->dc_gain_enabled ? s->analog_gain_frac * 2.5 : s->analog_gain_frac; + meta_data.high_conversion_gain = s->dc_gain_enabled; meta_data.integ_lines = s->exposure_time; + meta_data.measured_grey_fraction = s->measured_grey_fraction; + meta_data.target_grey_fraction = s->target_grey_fraction; s->exp_lock.unlock(); // dispatch @@ -969,6 +971,9 @@ static void set_camera_exposure(CameraState *s, float grey_frac) { } s->exp_lock.lock(); + s->measured_grey_fraction = grey_frac; + s->target_grey_fraction = target_grey; + // always prioritize exposure time adjust s->exposure_time *= exposure_factor; diff --git a/selfdrive/camerad/cameras/camera_qcom2.h b/selfdrive/camerad/cameras/camera_qcom2.h index 3d249253b..469a2f858 100644 --- a/selfdrive/camerad/cameras/camera_qcom2.h +++ b/selfdrive/camerad/cameras/camera_qcom2.h @@ -32,6 +32,9 @@ typedef struct CameraState { int exposure_time_max; float ef_filtered; + float measured_grey_fraction; + float target_grey_fraction; + unique_fd sensor_fd; unique_fd csiphy_fd; diff --git a/selfdrive/ui/ui.cc b/selfdrive/ui/ui.cc index 30d335e5f..06b91d2ac 100644 --- a/selfdrive/ui/ui.cc +++ b/selfdrive/ui/ui.cc @@ -199,11 +199,11 @@ static void update_state(UIState *s) { auto camera_state = sm["roadCameraState"].getRoadCameraState(); float max_lines = Hardware::EON() ? 5408 : 1757; - float gain = camera_state.getGainFrac(); + float gain = camera_state.getGain(); if (Hardware::TICI()) { - // gainFrac can go up to 4, with another 2.5x multiplier based on globalGain. Scale back to 0 - 1 - gain *= (camera_state.getGlobalGain() > 100 ? 2.5 : 1.0) / 10.0; + // Max gain is 4 * 2.5 (High Conversion Gain) + gain /= 10.0; } scene.light_sensor = std::clamp((1023.0 / max_lines) * (max_lines - camera_state.getIntegLines() * gain), 0.0, 1023.0);