diff --git a/README.md b/README.md index 9281f4a..9e4e7c2 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,11 @@ the following devices: - Local Script to Create Images ## Configuration +When `auto_brightness` is set to a camera control ID, `auto_brightness_target` +sets the desired normalized image brightness (0.0 to 1.0). The controller +averages five images between adjustments to accommodate delayed camera +responses, and uses bounded PI feedback to reduce oscillation. + The configuration is loaded in the following order. - systemwide configuration file [/etc/miniwebcam/miniwebcam.conf] - program parameters diff --git a/main.cc b/main.cc index bf2d682..7f6fd8b 100644 --- a/main.cc +++ b/main.cc @@ -7,6 +7,7 @@ #include #include +#include #include #include "configuration.h" @@ -53,6 +54,14 @@ static void *thread_video(void *ignored_argument) { timeval tv_fps; time_t time_fps, time_refreshctls; float current_brightness = 0.5; + float brightness_error_sum = 0.0f; + float brightness_error_integral = 0.0f; + int brightness_sample_count = 0; + const int brightness_settle_frames = 5; + const float brightness_deadband = 0.01f; + const float brightness_kp = 0.10f; + const float brightness_ki = 0.01f; + const float brightness_integral_limit = 2.0f; time_fps = time(NULL); time_refreshctls = time(NULL); @@ -96,6 +105,7 @@ static void *thread_video(void *ignored_argument) { debug ("Brightness: %f", current_brightness); } Lock(); + current_brightness = 0.0f; inputimage.CopyTo(¤timage, config.web_width, config.web_height, ¤t_brightness); if (inputfloatfilter.AddScaledImage(&inputfloat, config.filter_ratio) == 0) inputfloat.CopyTo(&inputfloatfilter); @@ -119,17 +129,47 @@ static void *thread_video(void *ignored_argument) { config.auto_brightness = 0; // disable auto brightness if control not found } if (found) { - int new_ctrl_value = it->value; - if (config.auto_brightness_target < 0.0f) config.auto_brightness_target = 0.0f; if (config.auto_brightness_target > 1.0f) config.auto_brightness_target = 1.0f; - if (current_brightness < config.auto_brightness_target) { - new_ctrl_value += 1; // increase brightness - } else if (current_brightness > config.auto_brightness_target) { - new_ctrl_value -= 1; // decrease brightness + brightness_error_sum += config.auto_brightness_target - current_brightness; + if (++brightness_sample_count >= brightness_settle_frames) { + float error = brightness_error_sum / brightness_sample_count; + int control_range = it->max - it->min; + brightness_error_sum = 0.0f; + brightness_sample_count = 0; + + if (std::fabs(error) <= brightness_deadband) { + brightness_error_integral = 0.0f; + } else if (control_range > 0) { + if (!((it->value <= it->min && error < 0.0f) || + (it->value >= it->max && error > 0.0f))) { + brightness_error_integral += error; + if (brightness_error_integral > brightness_integral_limit) + brightness_error_integral = brightness_integral_limit; + if (brightness_error_integral < -brightness_integral_limit) + brightness_error_integral = -brightness_integral_limit; + } + + float adjustment = (brightness_kp * error + brightness_ki * brightness_error_integral) * control_range; + int max_step = control_range / 32; + if (max_step < 1) max_step = 1; + if (adjustment > max_step) adjustment = max_step; + if (adjustment < -max_step) adjustment = -max_step; + + int change = static_cast(std::lround(adjustment)); + if (change == 0) change = error > 0.0f ? 1 : -1; + + int new_ctrl_value = it->value + change; + if (new_ctrl_value < it->min) new_ctrl_value = it->min; + if (new_ctrl_value > it->max) new_ctrl_value = it->max; + + debug("Auto Brightness: current=%f, target=%f, error=%f, new_ctrl_value=%d", + current_brightness, config.auto_brightness_target, error, new_ctrl_value); + if (new_ctrl_value != it->value) + vdev->SetDevCtrl(config.auto_brightness, new_ctrl_value); + } } - vdev->SetDevCtrl(config.auto_brightness, new_ctrl_value); } } diff --git a/videoframe.cc b/videoframe.cc index 2c41aae..90323a4 100644 --- a/videoframe.cc +++ b/videoframe.cc @@ -290,7 +290,7 @@ int VideoFrameFloat::CopyTo(VideoFrameFloat *dest) { }; -int VideoFrameFloat::AddScaledImage(VideoFrameFloat *vf, float ratio) { +int VideoFrameFloat::AddScaledImage(VideoFrameFloat *vf, float ratio, float *brightness) { float *sptr, *dptr; int x, y; @@ -299,10 +299,16 @@ int VideoFrameFloat::AddScaledImage(VideoFrameFloat *vf, float ratio) { for (dptr = memf, sptr = vf->GetPixBuf(), y = 0; y < height; y++) for (x = 0; x < width*3; x++) { *dptr = (1.0-ratio) * (*dptr) + ratio * (*sptr); + if (brightness != NULL) *brightness += *dptr; dptr++; sptr++; } + if (brightness != NULL) { + *brightness /= (width * height * 3); + *brightness = sqrtf(*brightness); + } + return 1; }; diff --git a/videoframe.h b/videoframe.h index 8325003..db4e05c 100644 --- a/videoframe.h +++ b/videoframe.h @@ -49,7 +49,7 @@ class VideoFrameFloat { int CopyTo(VideoFrame *dest); int CopyTo(VideoFrameFloat *dest); - int AddScaledImage(VideoFrameFloat *vf, float ratio); + int AddScaledImage(VideoFrameFloat *vf, float ratio, float *brightness = NULL); };