ark_isp_exposure_cmos.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //****************************************************************************
  2. //
  3. // Copyright (C) 2015 ZhuoYongHong
  4. //
  5. // Author ZhuoYongHong
  6. //
  7. // File name: arkn141_exposure_cmos.h
  8. // constant,macro & basic typedef definition of ISP exposure
  9. //
  10. // Revision history
  11. //
  12. // 2015.08.30 ZhuoYongHong Initial version
  13. //
  14. //****************************************************************************
  15. #ifndef _ARK_ISP_EXPOSURE_CMOS_H_
  16. #define _ARK_ISP_EXPOSURE_CMOS_H_
  17. #include "ark_isp_exposure.h"
  18. #include "Gem_isp_ae.h"
  19. #include "Gem_isp_awb.h"
  20. #include "Gem_isp_colors.h"
  21. #include "Gem_isp_denoise.h"
  22. #include "Gem_isp_enhance.h"
  23. #include "Gem_isp_eris.h"
  24. #include "Gem_isp_fesp.h"
  25. #include "Gem_isp_sys.h"
  26. #include "Gem_isp_sensor.h"
  27. #ifdef __cplusplus
  28. #if __cplusplus
  29. extern "C"{
  30. #endif
  31. #endif
  32. typedef struct _isp_denoise_inttime_polyline_tbl {
  33. int inttime_gain;
  34. // 2D Filter 0
  35. int y_thres0;
  36. int u_thres0;
  37. int v_thres0;
  38. // 2D Filter 1
  39. int y_thres1;
  40. int u_thres1;
  41. int v_thres1;
  42. int sensitiv0; // 2D滤波器0灵敏度
  43. int sensitiv1; // 2D滤波器1灵敏度
  44. // 3D Filter
  45. int y_thres2;
  46. int u_thres2;
  47. int v_thres2;
  48. } isp_denoise_inttime_polyline_tbl;
  49. // 20170216 Demk对噪声的影响很大
  50. typedef struct _isp_demosaic_polyline_tbl {
  51. int inttime_gain;
  52. int demk; // demk影响解析度, 值越大, 解析度越高, 噪声也越大.
  53. // 低照度时, 降低demk可减少噪声
  54. } isp_demosaic_polyline_tbl;
  55. typedef struct _isp_gamma_polyline_tbl {
  56. int inttime;
  57. int gamma_lut[65];
  58. } isp_gamma_polyline_tbl;
  59. // iris自动控制参数表
  60. typedef struct _isp_eris_polyline_tbl {
  61. int inttime_gain;
  62. int gain_max; // 最大亮度增益
  63. int resolt_ratio; // 解析度强度因子
  64. int colort_ratio; // 饱和度强度因子
  65. } isp_eris_polyline_tbl;
  66. // iris手动控制参数表
  67. typedef struct _isp_eris_man_polyline_tbl {
  68. int inttime_gain;
  69. int gain_man; // 亮度增益
  70. int cont_man; // 对比度
  71. int resolt_ratio; // 解析度强度因子
  72. int colort_ratio; // 饱和度强度因子
  73. } isp_eris_man_polyline_tbl;
  74. // crosstalk参数表
  75. typedef struct _isp_crosstalk_polyline_tbl {
  76. int inttime_gain;
  77. int thres; // threshold
  78. } isp_crosstalk_polyline_tbl;
  79. // fix-pattern noise参数表
  80. typedef struct _isp_fpn_polyline_tbl {
  81. int inttime_gain;
  82. int rBlacklevel;
  83. int grBlacklevel;
  84. int gbBlacklevel;
  85. int bBlacklevel;
  86. } isp_fpn_polyline_tbl;
  87. typedef struct _isp_lsc_polyline_tbl {
  88. int inttime_gain;
  89. int lscoff; // 同心圆半径, lsc仅校正同心圆之外的区域, 半径越大, 校正区域越小, 强度越弱.
  90. } isp_lsc_polyline_tbl;
  91. typedef struct _isp_satuation_polyline_tbl {
  92. int inttime_gain;
  93. int satuation;
  94. } isp_satuation_polyline_tbl;
  95. typedef struct _isp_enhance_polyline_tbl {
  96. int inttime;
  97. int bright; // 亮度 10 bit, 有效值 -255 ~ 255
  98. // 增加或减小每个像素点的亮度值
  99. int contrast; // 对比度
  100. } isp_enhance_polyline_tbl;
  101. typedef struct _isp_sharp_polyline_tbl {
  102. int inttime;
  103. int strength; // 锐化强度,一般设置为64
  104. int gainmax; // 锐化最大增益,一般设置为256
  105. } isp_sharp_polyline_tbl;
  106. typedef struct _isp_ae_polyline_tbl {
  107. int inttime;
  108. u8_t compensation;
  109. u8_t black_target;
  110. u8_t window_weight[9];
  111. } isp_ae_polyline_tbl;
  112. // CMOS sensor 增益控制结构定义
  113. typedef struct tag_cmos_gain {
  114. u32_t again; // sensor当前设置的模拟增益
  115. u32_t dgain; // sensor当前设置的数字增益
  116. u32_t iso; // sensor当前应用的ISO值, ISO = (100 * again * dgain) >> (again_shift + dgain_shift)
  117. // 基准ISO = 100, 此时无数字/模拟增益使能
  118. // 可配置增益, 手动曝光时可指定模拟/数字曝光值
  119. // 可配置的最大模拟增益, max_again_target <= max_again
  120. u32_t max_dgain;
  121. // 可配置的最大数字增益, max_dgain_target <= max_dgain
  122. u32_t max_again; // sensor的最大模拟增益
  123. u32_t max_again_target; // sensor的最大数字增益
  124. u32_t max_dgain_target; // sensor的最大模拟增益
  125. u32_t again_count; // 模拟增益可用的项目数
  126. u32_t dgain_count; // 数字增益可用的项目数
  127. u32_t again_shift; // 模拟增益放大倍数, dgain_max = (d) << dgain_shift;
  128. u32_t dgain_shift; // 数字增益放大倍数, dgain_max = (a) << dgain_shift;
  129. u32_t aindex; // 快速索引, again与模拟增益寄存器之间的映射
  130. u32_t dindex; // 快速索引, dgain与数字增益寄存器之间的映射
  131. u16_t again_lookup; // 模拟增益表查找值
  132. float log_again; // 模拟增益
  133. float log_dgain; // 数字增益
  134. } cmos_gain_t, *cmos_gain_ptr_t;
  135. // CMOS sensor积分时间结构定义
  136. typedef struct tag_cmos_inttime {
  137. // 画面闪烁校正参数
  138. u16_t flicker_freq;
  139. u16_t lines_per_500ms;
  140. u16_t max_flicker_lines;
  141. u16_t min_flicker_lines;
  142. u16_t full_lines; // 每帧行数, 包括 所有有效数据行 + 所有消隐行
  143. u16_t full_lines_limit; // 允许的最大曝光积分行数, 可允许1帧或多帧曝光积分行设置
  144. // 可配置的最小/最大曝光积分行数设置
  145. u16_t min_lines_target; // 可配置的最小曝光积分行数设置
  146. u16_t max_lines_target; // 可配置的最大曝光积分行数设置
  147. // 当前应用的最小/最大曝光行数量设置
  148. u16_t min_lines;
  149. u16_t max_lines;
  150. u32_t exposure_ashort; // 积分行数
  151. } cmos_inttime_t, * cmos_inttime_ptr_t;
  152. typedef struct tag_cmos_sensor {
  153. // 初始化CMOS sensor的曝光积分时间实例
  154. cmos_inttime_ptr_t (*cmos_inttime_initialize) (void);
  155. // 初始化CMOS sensor的增益实例
  156. cmos_gain_ptr_t (*cmos_gain_initialize)(void);
  157. // 设置CMOS sensor允许使用的最大增益
  158. int (*cmos_max_gain_set) (cmos_gain_ptr_t gain, unsigned int max_analog_gain, unsigned int max_digital_gain);
  159. // 获取CMOS sensor允许使用的最大增益
  160. int (*cmos_max_gain_get) (cmos_gain_ptr_t gain, unsigned int *max_analog_gain, unsigned int *max_digital_gain);
  161. // 修改曝光积分时间及增益
  162. int (*cmos_inttime_gain_update)(cmos_inttime_ptr_t p_inttime, cmos_gain_ptr_t gain);
  163. // 手工修改曝光积分时间及增益
  164. void (*cmos_inttime_gain_update_manual)(cmos_inttime_ptr_t p_inttime, cmos_gain_ptr_t gain);
  165. // 根据曝光量exposure计算模拟增益
  166. u32_t (*analog_gain_from_exposure_calculate) (cmos_gain_ptr_t gain, u32_t exposure, u32_t exposure_max);
  167. // 根据曝光量exposure计算数字增益
  168. u32_t (*digital_gain_from_exposure_calculate) (cmos_gain_ptr_t gain, u32_t exposure, u32_t exposure_max);
  169. // 获取当前曝光的ISO
  170. u32_t (*cmos_get_iso)(cmos_gain_ptr_t gain);
  171. // 设置帧率
  172. void (*cmos_fps_set) (cmos_inttime_ptr_t p_inttime, u8_t fps);
  173. // 设置sensor readout drection
  174. // horz_reverse_direction --> 1 horz reverse direction 垂直反向
  175. // --> 0 horz normal direction
  176. // vert_reverse_direction --> 1 vert reverse direction 水平反向
  177. // --> 0 vert normal direction
  178. int (*cmos_sensor_set_readout_direction) (u8_t horz_reverse_direction, u8_t vert_reverse_direction);
  179. // sensor的名称
  180. const char * (*cmos_sensor_get_sensor_name) (void);
  181. // sensor初始化
  182. int (*cmos_isp_sensor_init) (isp_sen_ptr_t p_sen);
  183. // ISP初始设置
  184. void (*cmos_isp_awb_init) (isp_awb_ptr_t p_awb); // 白平衡初始参数
  185. void (*cmos_isp_colors_init) (isp_colors_ptr_t p_colors); // 色彩初始参数
  186. void (*cmos_isp_denoise_init) (isp_denoise_ptr_t p_denoise); // 降噪初始设置
  187. void (*cmos_isp_eris_init)(isp_eris_ptr_t p_eris); // 宽动态初始设置
  188. void (*cmos_isp_fesp_init) (isp_fesp_ptr_t p_fesp); // 镜头校正, fix-pattern-correction, 坏点去除初始设置
  189. void (*cmos_isp_enhance_init) (isp_enhance_ptr_t p_enhance); // 图像增强初始设置
  190. void (*cmos_isp_ae_init) (isp_ae_ptr_t p_ae); // 自动曝光初始设置
  191. void (*cmos_isp_sys_init) (isp_sys_ptr_t p_sys, isp_param_ptr_t p_isp); // 系统初始设置 (sensor pixel位数, bayer mode)
  192. // ISP运行设置
  193. void (*cmos_isp_awb_run) (int inttime_gain, isp_awb_ptr_t p_awb); // 白平衡动态参数调整
  194. void (*cmos_isp_colors_run) (int inttime_gain, isp_colors_ptr_t p_colors, isp_awb_ptr_t p_awb, isp_ae_ptr_t p_ae); // 色彩表动态参数调整
  195. void (*cmos_isp_denoise_run) (int inttime_gain, isp_denoise_ptr_t p_denoise, isp_ae_ptr_t p_ae); // 降噪动态调整
  196. void (*cmos_isp_eris_run) (int inttime_gain, isp_eris_ptr_t p_eris ,isp_ae_ptr_t p_ae); // 宽动态动态调整
  197. void (*cmos_isp_fesp_run) (int inttime_gain, isp_fesp_ptr_t p_fesp, isp_ae_ptr_t p_ae); // fesp动态调整
  198. void (*cmos_isp_enhance_run) (int inttime_gain, isp_enhance_ptr_t p_enhance); // enhance动态调整
  199. void (*cmos_isp_ae_run) (int inttime_gain, isp_ae_ptr_t p_ae); // AE动态调整
  200. void (*cmos_isp_crosstalk_run) (int inttime_gain, isp_fesp_ptr_t p_fesp, isp_ae_ptr_t p_ae); // CrossTalk de-noise动态调整
  201. void (*cmos_isp_sharp_run) (int inttime_gain, isp_enhance_ptr_t p_enhance); // sharp动态调整
  202. void (*cmos_isp_gamma_run) (int inttime_gain, isp_colors_ptr_t p_colors); // gamma动态调整
  203. // 20170824 增加微光增强模式
  204. void (*cmos_isp_set_day_night_mode) (int day_night); // day_night = 1, 夜晚增强模式, day_night = 0, 普通模式
  205. // 运行动态参数获取
  206. // gamma
  207. const isp_gamma_polyline_tbl * (*cmos_isp_get_gamma_table) (int *tbl_count);
  208. // demosaic
  209. const isp_demosaic_polyline_tbl * (*cmos_isp_get_demosaic_table) (int *tbl_count);
  210. // 2D & 3D de-noise
  211. const isp_denoise_inttime_polyline_tbl * (*cmos_isp_get_denoise_table) (int *tbl_count);
  212. // iris manual
  213. const isp_eris_man_polyline_tbl * (*cmos_isp_get_eris_man_table) (int *tbl_count);
  214. // iris auto
  215. const isp_eris_polyline_tbl * (*cmos_isp_get_eris_auto_table) (int *tbl_count);
  216. // crosstalk de-noise
  217. const isp_crosstalk_polyline_tbl * (*cmos_isp_get_crosstalk_table) (int *tbl_count);
  218. // fpn de-noise
  219. const isp_fpn_polyline_tbl * (*cmos_isp_get_fpn_table) (int *tbl_count);
  220. // lens-shading
  221. const isp_lsc_polyline_tbl *(*cmos_isp_get_lsc_table)(int *tbl_count);
  222. // satuation
  223. const isp_satuation_polyline_tbl * (*cmos_isp_get_satuation_table)(int *tbl_count);
  224. // bright/contrast
  225. const isp_enhance_polyline_tbl * (*cmos_isp_get_enhance_table)(int *tbl_count);
  226. // sharp
  227. const isp_sharp_polyline_tbl * (*cmos_isp_get_sharp_table)(int *tbl_count);
  228. // auto-exposure
  229. const isp_ae_polyline_tbl * (*cmos_isp_get_ae_table)(int *tbl_count);
  230. } cmos_sensor_t, *cmos_sensor_ptr_t;
  231. // ISP AE系统信息
  232. typedef struct tag_isp_ae_sys_record {
  233. u16_t version; // 版本号
  234. u16_t size; // 结构字节大小
  235. u32_t ev; // 曝光补偿设置 0.01 ~ 10.0
  236. u32_t threshold[4]; // 阈值
  237. u32_t black;
  238. u32_t white;
  239. u32_t comp;
  240. // sensor
  241. u32_t sensor_ashift;
  242. u32_t sensor_dshift;
  243. u32_t max_again_target;
  244. u32_t max_dgain_target;
  245. u32_t sensor_type;
  246. u32_t record_count; // 记录个数
  247. } isp_ae_sys_record;
  248. #define ARKN141_ISP_AE_RECORD_V_0001 0x0001
  249. typedef struct tag_isp_ae_frame_record {
  250. u16_t version; // 版本号
  251. u16_t size; // 结构字节大小
  252. u16_t index;
  253. u16_t lum;
  254. u32_t hist[5];
  255. u32_t balance;
  256. i32_t error;
  257. u8_t ae_compensation; // 值越大, 图像的亮度值越大. 值越小, 图像的亮度值越小.
  258. //
  259. u8_t ae_black_target;
  260. u8_t ae_bright_target;
  261. // 曝光量增量(加/减)控制参数
  262. u32_t increment_step; // 曝光量增量阶梯控制
  263. u32_t increment_offset; // 曝光量增量基准
  264. u32_t increment_max; // 允许最大曝光量
  265. u32_t increment_min; // 允许最小曝光量
  266. u32_t increment; // 当前应用的增量值
  267. // 曝光量
  268. u32_t last_exposure; // 最近一次的曝光量
  269. u32_t exposure; // 理论曝光值
  270. u32_t physical_exposure; // 实际应用的曝光量
  271. // sensor
  272. u32_t sensor_inttime;
  273. u32_t sensor_again;
  274. u32_t sensor_dgain;
  275. u16_t sensor_aindex;
  276. u16_t sensor_dindex;
  277. u16_t sensor_again_shift;
  278. u16_t sensor_dgain_shift;
  279. // AE
  280. i32_t hist_error_min;
  281. i32_t hist_error_max;
  282. i32_t hist_error_one;
  283. i32_t hist_error_last;
  284. } isp_ae_frame_record_t, *isp_ae_frame_record_ptr_t;
  285. #define AE_STAT_COUNT 4
  286. typedef struct tag_cmos_exposure
  287. {
  288. u16_t sys_factor; // 曝光值放大系数
  289. // 当曝光量较小时, 为改善曝光量计算的准确性, 将曝光量乘以1个放大系数, 减少计算误差
  290. u32_t exposure; // 当前理论曝光值
  291. u32_t last_exposure;
  292. u32_t physical_exposure; // 最近的物理曝光值
  293. int exposure_delay_cycle; // 曝光参数延时影响周期
  294. cmos_gain_t cmos_gain;
  295. cmos_inttime_t cmos_inttime;
  296. cmos_sensor_t cmos_sensor;
  297. auto_exposure_t cmos_ae;
  298. u32_t exp_tlimit;
  299. u32_t exp_llimit;
  300. u8_t fps;
  301. // 记录最近的统计值
  302. i32_t stat_error[AE_STAT_COUNT];
  303. u16_t stat_lum[AE_STAT_COUNT];
  304. int stat_count;
  305. int stat_index;
  306. int locked;
  307. int locked_threshhold;
  308. //
  309. isp_ae_frame_record_t ae_record;
  310. } cmos_exposure_t, *cmos_exposure_ptr_t;
  311. /*
  312. * This function calculates applies increment/decrement to
  313. * current exposure and translates it into next values for
  314. * gains and exposures.
  315. */
  316. int isp_exposure_cmos_calculate (
  317. cmos_exposure_ptr_t p_exp,
  318. u32_t exp_increment,
  319. u32_t exp_increment_max,
  320. u32_t exp_increment_min,
  321. u32_t exp_increment_offset
  322. );
  323. /* Min Integration Time in lines */
  324. void isp_min_integration_time_set (cmos_exposure_ptr_t p_exp, u16_t value);
  325. /* Max Integration Time in lines */
  326. void isp_max_integration_time_set (cmos_exposure_ptr_t p_exp, u16_t value);
  327. // 根据计算的曝光设置更新sensor设置
  328. int isp_cmos_inttime_update (cmos_exposure_ptr_t p_exp);
  329. // 初始化cmos sensor的实例
  330. int isp_cmos_sensor_initialize (cmos_exposure_ptr_t p_exp);
  331. u32_t isp_init_cmos_sensor (cmos_sensor_t *cmos_sensor);
  332. int isp_auto_exposure_run (auto_exposure_ptr_t ae_block, cmos_exposure_ptr_t exp_cmos_block, int man_exp);
  333. // 初始化cmos sensor的ISP实例
  334. int arkn141_isp_ae_initialize (cmos_exposure_ptr_t p_exp);
  335. int arkn141_isp_ae_run (cmos_exposure_ptr_t p_exp);
  336. void isp_histogram_thread_update (cmos_exposure_ptr_t p_exp);
  337. void auto_exposure_step_calculate (auto_exposure_ptr_t p_ae_block, cmos_exposure_ptr_t exp_cmos_block);
  338. // 设置sensor的读出方向(水平及垂直方向)
  339. int arkn141_isp_set_sensor_readout_direction (cmos_exposure_ptr_t p_exp,
  340. unsigned int horz_reverse_direction, unsigned int vert_reverse_direction);
  341. void isp_cmos_inttime_update_manual (cmos_exposure_ptr_t p_exp);
  342. unsigned int cmos_calc_inttime_gain (cmos_exposure_t *p_isp_exposure);
  343. // 使能/禁止flicker
  344. int isp_cmos_set_flicker_freq (cmos_exposure_ptr_t p_exp, u8_t flicker_freq);
  345. unsigned int cmos_exposure_get_eris_dimlight (void);
  346. void cmos_exposure_set_eris_dimlight (unsigned int enable);
  347. #ifdef __cplusplus
  348. #if __cplusplus
  349. }
  350. #endif
  351. #endif
  352. #endif // _ARKN141_ISP_EXPOSURE_CMOS_H_