ark_isp_exposure.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  1. #include <linux/kernel.h>
  2. #include <linux/string.h>
  3. #include <linux/math64.h>
  4. #include "ark_camera.h"
  5. #include "ark_isp_exposure.h"
  6. #include "ark_isp_exposure_cmos.h"
  7. #define ENABLE_ERROR_RATIO 1
  8. static void histogram_bands_initialize (histogram_band_t bands[4])
  9. {
  10. bands[0].error = 0;
  11. bands[1].error = 0;
  12. bands[2].error = 0;
  13. bands[3].error = 0;
  14. bands[0].balance = 0;
  15. bands[1].balance = 0;
  16. bands[2].balance = 0;
  17. bands[3].balance = 0;
  18. }
  19. static void histogram_initialize (histogram_ptr_t p_hist)
  20. {
  21. p_hist->hist_balance = 0;
  22. p_hist->hist_dark = 0;
  23. p_hist->hist_dark_shift = 0x100;
  24. p_hist->hist_error = 0;
  25. histogram_bands_initialize (p_hist->bands);
  26. }
  27. void isp_system_ae_black_target_write (auto_exposure_ptr_t ae, u8_t data)
  28. {
  29. ae->ae_black_target = data;
  30. }
  31. u8_t isp_system_ae_black_target_read (auto_exposure_ptr_t ae)
  32. {
  33. return ae->ae_black_target;
  34. }
  35. void isp_system_ae_bright_target_write (auto_exposure_ptr_t ae, u8_t data)
  36. {
  37. ae->ae_bright_target = data;
  38. }
  39. u8_t isp_system_ae_bright_target_read (auto_exposure_ptr_t ae)
  40. {
  41. return ae->ae_bright_target;
  42. }
  43. void isp_histogram_thresh_write (auto_exposure_ptr_t ae, u8_t histoBand[4])
  44. {
  45. ae->metering_hist_thresh_0_1 = histoBand[0];
  46. ae->metering_hist_thresh_1_2 = histoBand[1];
  47. ae->metering_hist_thresh_3_4 = histoBand[2];
  48. ae->metering_hist_thresh_4_5 = histoBand[3];
  49. isp_histogram_bands_write (histoBand);
  50. }
  51. void isp_system_ae_compensation_write (auto_exposure_ptr_t ae, u8_t data)
  52. {
  53. ae->ae_compensation = data;
  54. }
  55. u8_t isp_system_ae_compensation_read (auto_exposure_ptr_t ae)
  56. {
  57. return ae->ae_compensation;
  58. }
  59. void isp_system_ae_ev_write( auto_exposure_ptr_t ae, i8_t data)
  60. {
  61. if(data < -6)
  62. data = -6;
  63. else if(data > 6)
  64. data = 6;
  65. ae->ae_ev = data;
  66. }
  67. i8_t isp_system_ae_ev_read (auto_exposure_ptr_t ae)
  68. {
  69. return ae->ae_ev;
  70. }
  71. void isp_histogram_thread_update (cmos_exposure_ptr_t p_exp)
  72. {
  73. histogram_band_t *bands = p_exp->cmos_ae.histogram.bands;
  74. auto_exposure_ptr_t ae = &p_exp->cmos_ae;
  75. bands[0].cent = (ae->metering_hist_thresh_0_1 + 0) / 2;
  76. bands[1].cent = (ae->metering_hist_thresh_0_1 + ae->metering_hist_thresh_1_2) / 2;
  77. bands[2].cent = (ae->metering_hist_thresh_1_2 + ae->metering_hist_thresh_3_4) / 2;
  78. bands[3].cent = (ae->metering_hist_thresh_3_4 + ae->metering_hist_thresh_4_5) / 2;
  79. bands[4].cent = (ae->metering_hist_thresh_4_5 + 255) / 2;
  80. }
  81. void isp_auto_exposure_compensation (auto_exposure_ptr_t ae, histogram_band_t bands[5])
  82. {
  83. u32_t ae_comp;
  84. //u8_t ev_value[7] = {32, 39, 50, 64, 83, 107, 128};
  85. u8_t ev_value[13] = {32, 35, 39, 44, 50, 57, 64, 74, 83, 95, 107, 117, 128};
  86. u8_t ev;
  87. if(ae->ae_ev < -6)
  88. ae->ae_ev = -6;
  89. else if(ae->ae_ev > 6)
  90. ae->ae_ev = 6;
  91. ev = ev_value[ae->ae_ev + 6];
  92. ae_comp = ae->ae_compensation;
  93. ae_comp *= ev;
  94. ae_comp /= ISP_SYSTEM_EV_DEFAULT;
  95. if(ae_comp < 1)
  96. ae_comp = 1;
  97. bands[0].target = (ae->ae_black_target << 8) / 2;
  98. bands[1].target = (ae->ae_black_target << 8);
  99. bands[2].target = 0x2000;
  100. bands[3].target = (ae->ae_bright_target << 8) * 2;
  101. bands[4].target = (ae->ae_bright_target << 8);
  102. if(ae_comp > 64)
  103. {
  104. bands[3].target = bands[3].target * ae_comp / 64;
  105. bands[4].target = bands[4].target * ae_comp / 64;
  106. }
  107. else
  108. {
  109. bands[0].target = bands[0].target * 64 / ae_comp;
  110. bands[1].target = bands[1].target * 64 / ae_comp;
  111. }
  112. bands[0].cent = (ae->metering_hist_thresh_0_1 + 0) / 2;
  113. bands[1].cent = (ae->metering_hist_thresh_0_1 + ae->metering_hist_thresh_1_2) / 2;
  114. bands[2].cent = (ae->metering_hist_thresh_1_2 + ae->metering_hist_thresh_3_4) / 2;
  115. bands[3].cent = (ae->metering_hist_thresh_3_4 + ae->metering_hist_thresh_4_5) / 2;
  116. bands[4].cent = (ae->metering_hist_thresh_4_5 + 255) / 2;
  117. }
  118. static void histogram_data_read (histogram_ptr_t p_hist, isp_ae_ptr_t isp_ae)
  119. {
  120. int _i;
  121. u32_t hacc = 0;
  122. isp_histogram_bands_read(p_hist->bands, isp_ae);
  123. for(_i = 0; _i < HISTOGRAM_BANDS; ++_i)
  124. {
  125. hacc += p_hist->bands[_i].value;
  126. }
  127. if(hacc >= 0xFFFF)
  128. p_hist->bands[2].value = 0;
  129. else
  130. p_hist->bands[2].value = (u16_t)(0xFFFF - hacc);
  131. }
  132. static void histogram_error_calculate (auto_exposure_ptr_t ae, histogram_ptr_t p_hist, isp_ae_ptr_t isp_ae)
  133. {
  134. u8_t ae_comp;
  135. //u8_t lum = isp_ae_lum_read ();
  136. u8_t lum = isp_ae->lumCurr;
  137. ae_comp = ae->ae_compensation;
  138. p_hist->hist_error = ae_comp - p_hist->hist_balance;
  139. p_hist->lum_hist[0] = p_hist->lum_hist[1];
  140. p_hist->lum_hist[1] = p_hist->lum_hist[2];
  141. p_hist->lum_hist[2] = p_hist->lum_hist[3];
  142. p_hist->lum_hist[3] = lum;
  143. #if 1
  144. if(p_hist->hist_error > 0)
  145. {
  146. // 曝光不够场景
  147. int total_lum = 0;
  148. int i;
  149. for (i = 0; i < 4; i ++)
  150. {
  151. if(p_hist->lum_hist[i] >= 3)
  152. break;
  153. total_lum += p_hist->lum_hist[i];
  154. }
  155. if(i == 4)
  156. {
  157. // 亮度偏暗场景下, 增加曝光
  158. if(total_lum == 0)
  159. p_hist->hist_error *= 3;
  160. else
  161. p_hist->hist_error = p_hist->hist_error * 3 * 4 / total_lum;
  162. }
  163. }
  164. #else
  165. // 亮度偏暗场景下, 增加曝光
  166. if(lum < 3 && p_hist->hist_error > 0)
  167. {
  168. if(lum == 0)
  169. p_hist->hist_error *= 3;
  170. else
  171. p_hist->hist_error = p_hist->hist_error * 3 / lum;
  172. }
  173. #endif
  174. }
  175. static void histogram_balance_calculate (histogram_ptr_t p_hist)
  176. {
  177. int _i;
  178. int64_t hbal;
  179. int center;
  180. int64_t temp;
  181. hbal = p_hist->bands[2].cent;
  182. for(_i = 0; _i < HISTOGRAM_BANDS; ++_i)
  183. {
  184. if(_i == 0)
  185. center = 0x00;
  186. else if(_i == HISTOGRAM_BANDS)
  187. center = 0xff;
  188. else
  189. center = p_hist->bands[_i].cent;
  190. temp = (p_hist->bands[_i].value + p_hist->bands[_i].target/4);
  191. temp *= (center - p_hist->bands[2].cent);
  192. temp = div_s64(temp, p_hist->bands[_i].target); // target越大,其对误差的计算影响越小
  193. hbal += temp;
  194. }
  195. p_hist->hist_balance = (int)hbal;
  196. }
  197. static void histogram_dark_calculate (histogram_ptr_t p_hist)
  198. {
  199. i32_t h_dark = 0;
  200. h_dark = (p_hist->hist_dark_shift*(p_hist->bands[0].value*2 + p_hist->bands[1].value) + 0x2000) /
  201. (p_hist->bands[2].value + 0x2000);
  202. p_hist->hist_dark = h_dark;
  203. }
  204. static void histogram_update (auto_exposure_ptr_t ae, histogram_ptr_t p_hist, isp_ae_ptr_t isp_ae)
  205. {
  206. histogram_data_read (p_hist, isp_ae);
  207. histogram_balance_calculate (p_hist);
  208. histogram_error_calculate (ae, p_hist, isp_ae);
  209. //histogram_dark_calculate (p_hist);
  210. }
  211. // 计算逆光场景的比例因子(>=256),
  212. static int base_ratio = 36;
  213. static void calc_bright_error_ratio (isp_ae_ptr_t isp_ae, histogram_ptr_t p_hist)
  214. {
  215. unsigned int y_max = 0;
  216. unsigned int y_total = 0;
  217. unsigned short *yavg_s = (unsigned short *)isp_ae->yavg_s;
  218. int i;
  219. for (i = 0; i < 9; i ++)
  220. {
  221. if(yavg_s[i] > y_max)
  222. y_max = yavg_s[i];
  223. y_total += yavg_s[i];
  224. }
  225. //XM_unlock();
  226. y_total -= y_max;
  227. y_total *= base_ratio;
  228. y_total /= 80;
  229. if(y_total == 0)
  230. y_total = 1;
  231. if(y_max > 10 * y_total)
  232. y_max = 10 * y_total;
  233. if(y_max <= y_total)
  234. {
  235. p_hist->hist_error_ratio = 256;
  236. }
  237. else
  238. {
  239. // 20181019 曝光发生突变,改为逐步逼近,直到稳态
  240. p_hist->hist_error_ratio = y_max * 256 / y_total;
  241. // p_hist->hist_error_ratio = (unsigned int)(11*256/10); // 提升10%
  242. }
  243. }
  244. #if ENABLE_ERROR_RATIO
  245. #define MAX_ERROR_RATIO 16
  246. static const int histogram_error_ratio[3][3] = {
  247. 0, 0, 0,
  248. 3, 5, 3,
  249. 5, 7, 5
  250. };
  251. static const int histogram_error_ratio_2[3][3] = {
  252. 1, 1, 1,
  253. 1, 1, 1,
  254. 1, 1, 1
  255. };
  256. #endif
  257. static void histogram_3x3_update (auto_exposure_ptr_t ae, histogram_ptr_t p_hist, isp_ae_ptr_t isp_ae)
  258. {
  259. int i, j;
  260. static histogram_t hist[3][3];
  261. unsigned short histogram_s[3][3][5];
  262. int hist_error_min = 65535;
  263. int hist_error_max = -65536;
  264. int last_error = p_hist->hist_error;
  265. memcpy (histogram_s, isp_ae->histogram_s, sizeof(histogram_s));
  266. for (i = 0; i < 3; i ++)
  267. {
  268. for (j = 0; j < 3; j ++)
  269. {
  270. memcpy (&hist[i][j], p_hist, sizeof(histogram_t));
  271. hist[i][j].bands[0].value = histogram_s[i][j][0] << 4;
  272. hist[i][j].bands[1].value = histogram_s[i][j][1] << 4;
  273. hist[i][j].bands[2].value = histogram_s[i][j][2] << 4;
  274. hist[i][j].bands[3].value = histogram_s[i][j][3] << 4;
  275. hist[i][j].bands[4].value = histogram_s[i][j][4] << 4;
  276. histogram_balance_calculate (&hist[i][j]);
  277. histogram_error_calculate (ae, &hist[i][j], isp_ae);
  278. #if ENABLE_ERROR_RATIO
  279. hist[i][j].hist_error = hist[i][j].hist_error * histogram_error_ratio[i][j] / MAX_ERROR_RATIO;
  280. #endif
  281. if(hist[i][j].hist_error < hist_error_min)
  282. hist_error_min = hist[i][j].hist_error;
  283. if(hist[i][j].hist_error > hist_error_max)
  284. hist_error_max = hist[i][j].hist_error;
  285. }
  286. }
  287. // 计算最小值
  288. histogram_update (ae, p_hist, isp_ae);
  289. p_hist->hist_error_one = p_hist->hist_error;
  290. p_hist->hist_error_min = hist_error_min;
  291. p_hist->hist_error_max = hist_error_max;
  292. p_hist->hist_error_last = last_error;
  293. if(ae->ae_mode == AE_MODE_BRIGHT)
  294. {
  295. calc_bright_error_ratio (isp_ae, p_hist);
  296. if(p_hist->hist_error > 0)
  297. {
  298. p_hist->hist_error = p_hist->hist_error * p_hist->hist_error_ratio / 256;
  299. }
  300. }
  301. else if(ae->ae_mode == AE_MODE_DARK)
  302. {
  303. int curr_error;
  304. if(hist_error_min < -4)
  305. {
  306. curr_error = hist_error_min;
  307. if(hist_error_min < -4)
  308. {
  309. //printf ("he %d --> %d\n", p_hist->hist_error, hist_error_min);
  310. //int curr_error = hist_error_min + p_hist->hist_error * 4 / abs(hist_error_min);
  311. p_hist->hist_error = hist_error_min + p_hist->hist_error * 4 / abs(hist_error_min);
  312. }
  313. }
  314. else
  315. {
  316. curr_error = p_hist->hist_error;
  317. // 20181113 增加一条正曝光约束条件
  318. // 当hist_error与hist_error_min均为正值时(曝光不够,增加曝光),若hist_error_min与hist_error差异较大,
  319. // 为避免hist_error_min翻转导致曝光振荡(明暗交替),此时应约束hist_error的值,
  320. // hist_error_min值越小,hist_error的变化约束程度越大。
  321. // hist_error_min值越大,hist_error的变化约束程度越小。
  322. if(hist_error_min > 0 && curr_error > 0)
  323. {
  324. if(hist_error_min < 8)
  325. {
  326. curr_error /= 2; // curr_error / 2
  327. }
  328. else if( (curr_error / hist_error_min) > 3)
  329. {
  330. curr_error = curr_error * 2 / 3; // curr_error / 1.5
  331. }
  332. if(curr_error < hist_error_min)
  333. curr_error = hist_error_min;
  334. p_hist->hist_error = curr_error;
  335. }
  336. if(last_error < 0 && curr_error > 0 || last_error > 0 && curr_error < 0)
  337. {
  338. //if(abs(last_error - curr_error) > 128)
  339. {
  340. // 避免亮度变化过大
  341. curr_error = (last_error + curr_error) / 4;
  342. p_hist->hist_error = curr_error;
  343. }
  344. }
  345. }
  346. }
  347. }
  348. extern isp_ae_t p_ae;
  349. // 暗场景判定规则
  350. static int check_dark_mode (unsigned int iso, isp_ae_t *ptr_ae)
  351. {
  352. int is_dark_mode = 0;
  353. unsigned int y_max = 0;
  354. unsigned int y_max_i = 0;
  355. unsigned int y_max_j = 0;
  356. int i,j;
  357. if(ptr_ae)
  358. {
  359. unsigned short *yavg = (unsigned short *)ptr_ae->yavg_s;
  360. int count = 0;
  361. for (i = 0; i < 3; i++)
  362. {
  363. for (j = 0; j < 3; j ++)
  364. {
  365. unsigned int yavg = ptr_ae->yavg_s[i][j];
  366. if(yavg == 0)
  367. count ++;
  368. if(y_max < yavg)
  369. {
  370. y_max = yavg;
  371. y_max_i = i;
  372. y_max_j = j;
  373. }
  374. }
  375. }
  376. if(count >= 2)
  377. {
  378. // 微光区至少2个
  379. // 检查最大亮度的区域,其高亮区是否小于256。晚上一般为点光源,不同于白天高光区
  380. unsigned int histo = ptr_ae->histogram_s[y_max_i][y_max_j][3] + ptr_ae->histogram_s[y_max_i][y_max_j][4];
  381. if(histo < 256)
  382. is_dark_mode = 1;
  383. }
  384. }
  385. return is_dark_mode;
  386. }
  387. // 逆光
  388. static int check_bl_mode (unsigned int iso, isp_ae_t *ptr_ae)
  389. {
  390. int is_bl_mode = 0;
  391. int i;
  392. if(ptr_ae && iso < 16)
  393. {
  394. unsigned short *yavg = (unsigned short *)ptr_ae->yavg_s;
  395. int count = 0;
  396. for (i = 0; i < 9; i++)
  397. {
  398. if(yavg[i] == 0)
  399. return 0;
  400. if(yavg[i] == 1)
  401. count ++;
  402. }
  403. if(count >= 2)
  404. is_bl_mode = 1;
  405. }
  406. return is_bl_mode;
  407. }
  408. // 判断是否是白天(亮场景)模式
  409. static int is_bright_mode (unsigned int iso, isp_ae_t *ptr_ae)
  410. {
  411. unsigned int y_max = 0;
  412. unsigned int y_max_i = 0;
  413. unsigned int y_max_j = 0;
  414. unsigned int zero_count = 0;
  415. int i,j;
  416. // 超短曝光
  417. if(iso <= 4)
  418. return 1;
  419. // 短曝光
  420. if(iso < 16 && ptr_ae->lumCurr >= 8)
  421. return 1;
  422. // 20190416 涓嬮潰鐨勬潯浠朵細瀹规槗瀵艰嚧鍛ㄦ湡鎬ф槑鏆楁洕鍏夛紝绉昏嚦鏈€涓嬮潰
  423. // 涓�洕鍏�
  424. //if(iso < 128 && ptr_ae->lumCurr >= 16)
  425. // return 1;
  426. // 判断每个块的亮度值均大于某个值
  427. int count = 0;
  428. for (i = 0; i < 3; i++)
  429. {
  430. for (j = 0; j < 3; j ++)
  431. {
  432. unsigned int yavg = ptr_ae->yavg_s[i][j];
  433. if(yavg == 0)
  434. zero_count ++;
  435. if(yavg > y_max)
  436. {
  437. y_max = yavg;
  438. y_max_i = i;
  439. y_max_j = j;
  440. }
  441. if(yavg <= 1)
  442. count ++;
  443. }
  444. }
  445. // 20181023 根据路测结果,增加一条亮场景(白天)的判断规则
  446. // 1) ISO < 48, 存在至少一个亮区(亮度值大于64,且高光区4+5大于1000)
  447. // ae_sensor_inttime = 35
  448. // ae_sensor_again = 1.000000
  449. // ae_sensor_dgain = 1.000000
  450. // [0][1]:yavg_s = 72;[0] = 2292; [1] = 25; [2] = 401; [3] = 848; [4] = 529;
  451. if(iso < 48)
  452. {
  453. unsigned int histo = ptr_ae->histogram_s[y_max_i][y_max_j][3] + ptr_ae->histogram_s[y_max_i][y_max_j][4];
  454. if(y_max >= 64 && histo >= 1000 )
  455. return 1;
  456. }
  457. // 无微光区
  458. if(zero_count >= 1)
  459. return 0;
  460. // 最大亮度值大于100
  461. if(y_max > 100)
  462. return 1;
  463. // 弱光区个数
  464. if(count >= 2)
  465. return 0;
  466. // 20190416 涓嬮潰鐨勬潯浠朵細瀹规槗瀵艰嚧鍛ㄦ湡鎬ф槑鏆楁洕鍏夛紝绉昏嚦姝ゅ�
  467. // 涓�洕鍏�
  468. if(iso < 128 && ptr_ae->lumCurr >= 16)
  469. return 1;
  470. return 0;
  471. }
  472. static int get_y_max (isp_ae_t *ptr_ae)
  473. {
  474. unsigned int y_max = 0;
  475. int i, j;
  476. // 鍒ゆ柇姣忎釜鍧楃殑浜�害鍊煎潎澶т簬鏌愪釜鍊�
  477. for (i = 0; i < 3; i++)
  478. {
  479. for (j = 0; j < 3; j ++)
  480. {
  481. unsigned int yavg = ptr_ae->yavg_s[i][j];
  482. if(yavg > y_max)
  483. {
  484. y_max = yavg;
  485. }
  486. }
  487. }
  488. return y_max;
  489. }
  490. void isp_auto_exposure_set_iso (cmos_exposure_t *p_isp_exposure, unsigned int iso)
  491. {
  492. if(p_isp_exposure->cmos_ae.ae_mode == AE_MODE_BRIGHT)
  493. {
  494. //if(iso > p_isp_exposure->cmos_inttime.full_lines * 4/5)
  495. // 20190407 娴嬭瘯鍙戠幇瀛樺湪鏌愮�鎯呭喌锛屾槑鏆楀弽澶嶏紝姝ゆ椂鐨勬渶澶т寒搴﹀€�150銆�
  496. // 鍦ㄤ寒鍦烘櫙涓嬶紝澧炲姞涓€涓�潯浠讹紝褰撴渶澶т寒搴﹀€�>=95, 绂佹�鍒囨崲鍒版殫鍦烘櫙妯″紡
  497. //if(iso > p_isp_exposure->cmos_inttime.full_lines * 1/2)
  498. if( (iso > p_isp_exposure->cmos_inttime.full_lines)
  499. || ((iso > p_isp_exposure->cmos_inttime.full_lines * 1/2) && (get_y_max(&p_ae) < 95)) )
  500. {
  501. p_isp_exposure->cmos_ae.n_cycle ++;
  502. p_isp_exposure->cmos_ae.d_cycle = 0;
  503. if(p_isp_exposure->cmos_ae.n_cycle > 5)
  504. {
  505. p_isp_exposure->cmos_ae.ae_mode = AE_MODE_DARK;
  506. p_isp_exposure->cmos_ae.n_cycle = 0;
  507. XM_printf ("dark Mode\n");
  508. }
  509. }
  510. else
  511. {
  512. p_isp_exposure->cmos_ae.n_cycle = 0;
  513. p_isp_exposure->cmos_ae.d_cycle = 0;
  514. }
  515. }
  516. else if(p_isp_exposure->cmos_ae.ae_mode == AE_MODE_DARK)
  517. {
  518. //if(iso < 16 && p_ae.lumCurr >= 5 || iso <= 3)
  519. if(is_bright_mode (iso, &p_ae))
  520. {
  521. p_isp_exposure->cmos_ae.d_cycle ++;
  522. p_isp_exposure->cmos_ae.n_cycle = 0;
  523. if(p_isp_exposure->cmos_ae.d_cycle >= 4)
  524. {
  525. p_isp_exposure->cmos_ae.ae_mode = AE_MODE_BRIGHT;
  526. p_isp_exposure->cmos_ae.d_cycle = 0;
  527. XM_printf ("short bright Mode\n");
  528. }
  529. }
  530. /*else if(iso < 128 && p_ae.lumCurr >= 8)
  531. {
  532. p_isp_exposure->cmos_ae.d_cycle ++;
  533. p_isp_exposure->cmos_ae.n_cycle = 0;
  534. if(p_isp_exposure->cmos_ae.d_cycle > 5)
  535. {
  536. p_isp_exposure->cmos_ae.ae_mode = AE_MODE_BRIGHT;
  537. p_isp_exposure->cmos_ae.d_cycle = 0;
  538. XM_printf ("bright Mode\n");
  539. }
  540. }*/
  541. else
  542. {
  543. p_isp_exposure->cmos_ae.n_cycle = 0;
  544. p_isp_exposure->cmos_ae.d_cycle = 0;
  545. }
  546. }
  547. // 根据亮度校正特殊场景下的模式
  548. if(p_isp_exposure->cmos_ae.ae_mode == AE_MODE_BRIGHT)
  549. {
  550. // 检查必须为暗场模式的某些极端场景
  551. if(check_dark_mode(iso, &p_ae))
  552. {
  553. p_isp_exposure->cmos_ae.ae_mode = AE_MODE_DARK;
  554. p_isp_exposure->cmos_ae.n_cycle = 0;
  555. p_isp_exposure->cmos_ae.d_cycle = 0;
  556. XM_printf ("dark correct Mode\n");
  557. }
  558. }
  559. else if(p_isp_exposure->cmos_ae.ae_mode == AE_MODE_DARK)
  560. {
  561. // 检查逆光模式
  562. if(check_bl_mode(iso, &p_ae))
  563. {
  564. p_isp_exposure->cmos_ae.ae_mode = AE_MODE_BRIGHT;
  565. p_isp_exposure->cmos_ae.n_cycle = 0;
  566. p_isp_exposure->cmos_ae.d_cycle = 0;
  567. XM_printf ("bright correct Mode\n");
  568. }
  569. }
  570. }
  571. void isp_auto_exposure_initialize (auto_exposure_ptr_t p_ae_block)
  572. {
  573. p_ae_block->exposure_quant = 0x0080;
  574. p_ae_block->increment_offset = (1 << 8) * 16;
  575. p_ae_block->increment = p_ae_block->increment_offset;
  576. p_ae_block->increment_damping = 0; // 反相阻尼关闭
  577. p_ae_block->exposure_target = 0;
  578. p_ae_block->exposure_steps = 0;
  579. p_ae_block->exposure_factor = 1;
  580. p_ae_block->steady_control.enable = 0;
  581. p_ae_block->steady_control.count = 0;
  582. histogram_initialize (&(p_ae_block->histogram));
  583. }
  584. // 根据当前的直方图误差绝对值计算曝光增量的步长
  585. // 误差绝对值越大,步长越大
  586. static u32_t auto_exposure_increment_get (auto_exposure_ptr_t p_ae_block)
  587. {
  588. u32_t _step = p_ae_block->exposure_quant;
  589. if(abs(p_ae_block->histogram.hist_error) <= 0x4)
  590. {
  591. _step = p_ae_block->exposure_quant / 64;
  592. }
  593. else if(abs(p_ae_block->histogram.hist_error) <= 0x8)
  594. {
  595. _step = p_ae_block->exposure_quant / 32;
  596. //_step = p_ae_block->exposure_quant / 20;
  597. }
  598. else if(abs(p_ae_block->histogram.hist_error) < 0x10)
  599. {
  600. //_step = p_ae_block->exposure_quant / 16;
  601. _step = p_ae_block->exposure_quant / 12;
  602. }
  603. else if(abs(p_ae_block->histogram.hist_error) < 0x18)
  604. {
  605. //_step = p_ae_block->exposure_quant / 10;
  606. //_step = p_ae_block->exposure_quant / 5;
  607. _step = p_ae_block->exposure_quant / 3;
  608. }
  609. else if(abs(p_ae_block->histogram.hist_error) < 0x20)
  610. {
  611. //_step = p_ae_block->exposure_quant / 5;
  612. //_step = p_ae_block->exposure_quant * 2 / 5; // 0.4
  613. _step = p_ae_block->exposure_quant * 1 / 2;
  614. //_step = p_ae_block->exposure_quant * 3 / 4; // 0.75
  615. }
  616. else if(abs(p_ae_block->histogram.hist_error) < 0x30) // 48
  617. {
  618. //_step = p_ae_block->exposure_quant / 3;
  619. _step = p_ae_block->exposure_quant * 3 / 4; // 0.75
  620. //_step = p_ae_block->exposure_quant * 1;
  621. }
  622. else if(abs(p_ae_block->histogram.hist_error) < 0x40) // 64
  623. {
  624. _step = p_ae_block->exposure_quant * 4 / 5 ;
  625. //_step = p_ae_block->exposure_quant * 3/2;
  626. }
  627. else if(abs(p_ae_block->histogram.hist_error) < 0x50) // 80
  628. {
  629. _step = p_ae_block->exposure_quant * 1;
  630. //_step = p_ae_block->exposure_quant * 2;
  631. }
  632. else if(abs(p_ae_block->histogram.hist_error) < 0x60) // 96
  633. {
  634. //_step = p_ae_block->exposure_quant * 3 / 2;
  635. _step = p_ae_block->exposure_quant * 5 / 4;
  636. //_step = p_ae_block->exposure_quant * 5/2;
  637. }
  638. // 定义太大的step可能导致曝光调整过大, 导致直方图错误位翻转(从正到负 或 从负到正), 导致曝光闪烁现象发生
  639. // 比如 下面实例允许大的步幅控制 (> 0x200)
  640. // hist_error --> -2357, step = 128(步幅过大), 减少曝光, 步幅调整过大, 新的曝光值 inttime= 61, again=1.000000, dgain=1.000000
  641. // hist_error --> 115, step = 16, 因为步幅调整过大,需要逐步增加曝光值, 新的曝光值 inttime= 64, again=1.000000, dgain=1.007813
  642. // ...
  643. // hist_error --> 5, step = 1, 曝光稳定, inttime= 91, again=1.000000, dgain=1.007813
  644. /*
  645. else if(abs(p_ae_block->histogram.hist_error) > 0x200)
  646. {
  647. _step = p_ae_block->exposure_quant * 8;
  648. }
  649. else if(abs(p_ae_block->histogram.hist_error) > 0x100)
  650. {
  651. _step = p_ae_block->exposure_quant * 4;
  652. }*/
  653. else if(abs(p_ae_block->histogram.hist_error) < 0x70) // 112
  654. {
  655. //_step = p_ae_block->exposure_quant * 2;
  656. //_step = p_ae_block->exposure_quant * 3;
  657. _step = p_ae_block->exposure_quant * 3/2;
  658. //_step = p_ae_block->exposure_quant * 4/3;
  659. }
  660. // 考虑下面的步幅过大,导致画面亮暗明显
  661. else if(abs(p_ae_block->histogram.hist_error) < 0x80)
  662. {
  663. //_step = p_ae_block->exposure_quant * 6/4;
  664. //_step = p_ae_block->exposure_quant * 7/2;
  665. _step = p_ae_block->exposure_quant * 2;
  666. }
  667. else if(abs(p_ae_block->histogram.hist_error) < 0x90) // 144
  668. {
  669. // _step = p_ae_block->exposure_quant * 2;
  670. _step = p_ae_block->exposure_quant * 5/2;
  671. //_step = p_ae_block->exposure_quant * 3/2;
  672. //_step = p_ae_block->exposure_quant * 7/4;
  673. //_step = p_ae_block->exposure_quant * 4;
  674. }
  675. else if(abs(p_ae_block->histogram.hist_error) < 0xA0) // 160
  676. {
  677. //_step = p_ae_block->exposure_quant * 3/2;
  678. _step = p_ae_block->exposure_quant * 3;
  679. //_step = p_ae_block->exposure_quant * 5 / 2;
  680. //_step = p_ae_block->exposure_quant * 2;
  681. //_step = p_ae_block->exposure_quant * 9/2;
  682. }
  683. else if(abs(p_ae_block->histogram.hist_error) < 0xB0) // 176
  684. {
  685. //_step = p_ae_block->exposure_quant * 2;
  686. //_step = p_ae_block->exposure_quant * 5;
  687. _step = p_ae_block->exposure_quant * 7/2;
  688. //_step = p_ae_block->exposure_quant * 5;
  689. }
  690. else if(abs(p_ae_block->histogram.hist_error) < 0xC0) // 192
  691. {
  692. //_step = p_ae_block->exposure_quant * 3/2;
  693. //_step = p_ae_block->exposure_quant * 2;
  694. //_step = p_ae_block->exposure_quant * 8;
  695. //_step = p_ae_block->exposure_quant * 5/2;
  696. //_step = p_ae_block->exposure_quant * 3;
  697. //_step = p_ae_block->exposure_quant * 6;
  698. _step = p_ae_block->exposure_quant * 4;
  699. }
  700. else if(abs(p_ae_block->histogram.hist_error) < 0xD0) // 192
  701. {
  702. //_step = p_ae_block->exposure_quant * 3/2;
  703. //_step = p_ae_block->exposure_quant * 2;
  704. //_step = p_ae_block->exposure_quant * 8;
  705. //_step = p_ae_block->exposure_quant * 5/2;
  706. //_step = p_ae_block->exposure_quant * 3;
  707. //_step = p_ae_block->exposure_quant * 13/2;
  708. _step = p_ae_block->exposure_quant * 9/2;
  709. }
  710. else if(abs(p_ae_block->histogram.hist_error) < 0xE0) // 192
  711. {
  712. //_step = p_ae_block->exposure_quant * 3/2;
  713. //_step = p_ae_block->exposure_quant * 2;
  714. //_step = p_ae_block->exposure_quant * 8;
  715. //_step = p_ae_block->exposure_quant * 5/2;
  716. //_step = p_ae_block->exposure_quant * 3;
  717. //_step = p_ae_block->exposure_quant * 7;
  718. _step = p_ae_block->exposure_quant * 5;
  719. }
  720. else if(abs(p_ae_block->histogram.hist_error) > 3000)
  721. {
  722. // _step = p_ae_block->exposure_quant * 2;
  723. //_step = p_ae_block->exposure_quant * 20;
  724. _step = p_ae_block->exposure_quant * 30;
  725. }
  726. else if(abs(p_ae_block->histogram.hist_error) > 2000)
  727. {
  728. // _step = p_ae_block->exposure_quant * 2;
  729. //_step = p_ae_block->exposure_quant * 15;
  730. //_step = p_ae_block->exposure_quant * 25;
  731. _step = p_ae_block->exposure_quant * 27;
  732. }
  733. else if(abs(p_ae_block->histogram.hist_error) > 1000)
  734. {
  735. // _step = p_ae_block->exposure_quant * 2;
  736. //_step = p_ae_block->exposure_quant * 11;
  737. //_step = p_ae_block->exposure_quant * 18;
  738. _step = p_ae_block->exposure_quant * 21;
  739. }
  740. else if(abs(p_ae_block->histogram.hist_error) > 900)
  741. {
  742. // _step = p_ae_block->exposure_quant * 2;
  743. //_step = p_ae_block->exposure_quant * 9;
  744. //_step = p_ae_block->exposure_quant * 16;
  745. _step = p_ae_block->exposure_quant * 18;
  746. }
  747. else if(abs(p_ae_block->histogram.hist_error) > 700)
  748. {
  749. // _step = p_ae_block->exposure_quant * 2;
  750. //_step = p_ae_block->exposure_quant * 8;
  751. //_step = p_ae_block->exposure_quant * 10;
  752. //_step = p_ae_block->exposure_quant * 16;
  753. //_step = p_ae_block->exposure_quant * 12;
  754. _step = p_ae_block->exposure_quant * 15;
  755. }
  756. else if(abs(p_ae_block->histogram.hist_error) > 600)
  757. {
  758. //_step = p_ae_block->exposure_quant * 6;
  759. //_step = p_ae_block->exposure_quant * 8;
  760. //_step = p_ae_block->exposure_quant * 14;
  761. //_step = p_ae_block->exposure_quant * 10;
  762. _step = p_ae_block->exposure_quant * 13;
  763. }
  764. else if(abs(p_ae_block->histogram.hist_error) > 500)
  765. {
  766. //_step = p_ae_block->exposure_quant * 5;
  767. //_step = p_ae_block->exposure_quant * 7;
  768. //_step = p_ae_block->exposure_quant * 12;
  769. //_step = p_ae_block->exposure_quant * 8;
  770. _step = p_ae_block->exposure_quant * 12;
  771. }
  772. else if(abs(p_ae_block->histogram.hist_error) > 400)
  773. {
  774. //_step = p_ae_block->exposure_quant * 4;
  775. //_step = p_ae_block->exposure_quant * 6;
  776. _step = p_ae_block->exposure_quant * 10;
  777. //_step = p_ae_block->exposure_quant * 7;
  778. //_step = p_ae_block->exposure_quant * 11;
  779. }
  780. else if(abs(p_ae_block->histogram.hist_error) > 300)
  781. {
  782. //_step = p_ae_block->exposure_quant * 3;
  783. //_step = p_ae_block->exposure_quant * 5;
  784. //_step = p_ae_block->exposure_quant * 10;
  785. //_step = p_ae_block->exposure_quant * 6;
  786. _step = p_ae_block->exposure_quant * 8;
  787. }
  788. else if(abs(p_ae_block->histogram.hist_error) > 0x100)
  789. {
  790. //_step = p_ae_block->exposure_quant * 2;
  791. //_step = p_ae_block->exposure_quant * 3;
  792. //_step = p_ae_block->exposure_quant * 9;
  793. _step = p_ae_block->exposure_quant * 7;
  794. //_step = p_ae_block->exposure_quant * 5;
  795. }
  796. /*
  797. else if(abs(p_ae_block->histogram.hist_error) > 240)
  798. {
  799. //_step = p_ae_block->exposure_quant * 2;
  800. //_step = p_ae_block->exposure_quant * 3;
  801. _step = p_ae_block->exposure_quant * 5;
  802. }
  803. else if(abs(p_ae_block->histogram.hist_error) > 220)
  804. {
  805. //_step = p_ae_block->exposure_quant * 2;
  806. _step = p_ae_block->exposure_quant * 3;
  807. //_step = p_ae_block->exposure_quant * 4;
  808. }*/
  809. else
  810. {
  811. // 0xA0 <= x >= 0x100
  812. // 2 ~ 2.5
  813. //XM_printf ("error=%d, _step=%d\n", p_ae_block->histogram.hist_error, _step);
  814. //_step = p_ae_block->exposure_quant * 9;
  815. //_step = p_ae_block->exposure_quant * 2;
  816. //_step = p_ae_block->exposure_quant * 4;
  817. //_step = p_ae_block->exposure_quant * 8;
  818. _step = p_ae_block->exposure_quant * 6;
  819. }
  820. // 以下步幅降低一半会导致隧道进出适应缓慢,重新恢复为原来的步幅.
  821. //_step /= 2;
  822. //_step *= 2;
  823. return _step;
  824. }
  825. #undef XM_printf
  826. #define XM_printf(...)
  827. void auto_exposure_step_calculate (auto_exposure_ptr_t p_ae_block, cmos_exposure_ptr_t exp_cmos_block)
  828. {
  829. u32_t _step;
  830. u32_t _step_1st;
  831. u32_t ratio = 1;
  832. u8_t old_steady_enable = p_ae_block->steady_control.enable;
  833. _step = auto_exposure_increment_get (p_ae_block);
  834. _step_1st = _step;
  835. // 极端曝光条件修正
  836. // 1) 该修正在某些光照条件下容易触发振荡, 需要较长时间才能平稳.
  837. // 增加修正条件的阈值 16 --> 32
  838. // 2) 减小阶梯值
  839. if(p_ae_block->histogram.hist_error < (-32))
  840. {
  841. // 曝光严重过度, 能量主要集中在最右侧高光区 (0xC0 ~ 0xFF)
  842. if(p_ae_block->histogram.bands[4].value >= 0xF800)
  843. {
  844. // 将曝光量衰减75%, 32*0.75 = 24
  845. XM_printf ("SC-OV-0 0.75\r\n");
  846. _step = p_ae_block->exposure_quant * 8 * 3;
  847. }
  848. // 能量主要集中在高光区 (0x80 ~ 0xFF)
  849. else if( (p_ae_block->histogram.bands[3].value + p_ae_block->histogram.bands[4].value) >= 0xF800)
  850. {
  851. // 将曝光量衰减50%, 16/32 = 0.5
  852. XM_printf ("SC-OV-1 0.5\r\n");
  853. _step = p_ae_block->exposure_quant * 8 * 2 ;
  854. }
  855. // 能量主体集中在高光区 (0x80 ~ 0xFF)
  856. else if( (p_ae_block->histogram.bands[3].value + p_ae_block->histogram.bands[4].value) >= 0xE000)
  857. {
  858. // 将曝光量衰减43.75%, 14/32 = 0.4375
  859. XM_printf ("SC-OV-2 0.4375\r\n");
  860. _step = p_ae_block->exposure_quant * 7 * 2 ;
  861. }
  862. // 能量主体集中在高光区 (0x80 ~ 0xFF)
  863. else if( (p_ae_block->histogram.bands[3].value + p_ae_block->histogram.bands[4].value) >= 0xD000)
  864. {
  865. // 将曝光量衰减31.25%, 10/32 = 0.3125
  866. XM_printf ("SC-OV-2 0.3125\r\n");
  867. _step = p_ae_block->exposure_quant * 5 * 2 ;
  868. }
  869. // 能量主体集中在高光区 (0x80 ~ 0xFF)
  870. else if( (p_ae_block->histogram.bands[3].value + p_ae_block->histogram.bands[4].value) >= 0xC000)
  871. {
  872. // 将曝光量衰减25%, 8/32 = 0.25
  873. XM_printf ("SC-OV-2 0.25\r\n");
  874. _step = p_ae_block->exposure_quant * 4 * 2 ;
  875. }
  876. // 能量主体集中在高光区 (0x80 ~ 0xFF)
  877. else if( (p_ae_block->histogram.bands[3].value + p_ae_block->histogram.bands[4].value) >= 0xB000)
  878. {
  879. // 将曝光量衰减12.5%, 4/32 = 0.125
  880. XM_printf ("SC-OV-2 0.125\r\n");
  881. _step = p_ae_block->exposure_quant * 2 * 2 ;
  882. }
  883. // 能量主体集中在高光区 (0x80 ~ 0xFF)
  884. else if( (p_ae_block->histogram.bands[3].value + p_ae_block->histogram.bands[4].value) >= 0xA000)
  885. {
  886. // 将曝光量衰减9.375%, 3/32 = 0.09375
  887. XM_printf ("SC-OV-2 0.09375\r\n");
  888. _step = p_ae_block->exposure_quant * 3 ;
  889. }
  890. // 能量主体集中在高光区 (0x80 ~ 0xFF)
  891. else if( (p_ae_block->histogram.bands[3].value + p_ae_block->histogram.bands[4].value) >= 0x8000)
  892. {
  893. // 将曝光量衰减0.0625, 2/32 = 0.0625
  894. XM_printf ("SC-OV-2 0.0625\r\n");
  895. _step = p_ae_block->exposure_quant * 2 * 1 ;
  896. }
  897. // 低光区(0x00 ~ 0x3F)能量基本不存在, 低于2/256
  898. else if( (p_ae_block->histogram.bands[0].value + p_ae_block->histogram.bands[1].value) <= 0x0200 )
  899. {
  900. // 低光区(0x00 ~ 0x3F)能量基本不存在
  901. // 将整体曝光量衰减25%, 8 / 32 = 0.25
  902. XM_printf ("SC-OV-2 0.25\r\n");
  903. _step = p_ae_block->exposure_quant * 8 ;
  904. }
  905. // 低光区能量(0x00 ~ 0x0F)对曝光衰减速度的影响很低
  906. // 低光区(0x00 ~ 0x0F)能量基本不存在, 低于2/256
  907. // 这一条规则没有考虑低光区不存在的场景
  908. else if( (p_ae_block->histogram.bands[0].value ) <= 0x0200 )
  909. {
  910. // 低光区(0x00 ~ 0x0F)能量基本不存在
  911. // 将整体曝光量衰减1/32,
  912. XM_printf ("SC-OV-3 0.03125\r\n");
  913. _step = p_ae_block->exposure_quant * 1 ;
  914. }
  915. }
  916. else if(p_ae_block->histogram.hist_error > 32)
  917. {
  918. // 以下短曝光,高光处为0, 执行下面的高光曝光正补偿过程,可能导致闪烁问题。
  919. // 减小曝光阶梯为exposure_quant / 2,避免闪烁
  920. // ae_hist_error = 29
  921. // ae_sensor_inttime = 22
  922. // ae_sensor_again = 1.000000
  923. // ae_sensor_dgain = 1.000000
  924. // ae info histoGram :
  925. // [0] = 2789;
  926. // [1] = 923;
  927. // [2] = 383;
  928. // [3] = 0;
  929. // [4] = 0;
  930. // 曝光严重不足, 能量主要集中在低光区(0x00 ~ 0x3F)
  931. if( (p_ae_block->histogram.bands[0].value + p_ae_block->histogram.bands[1].value) >= 0xFF80 )
  932. {
  933. // 曝光量增加2倍 (*3), 即使溢出, 溢出的比例较低
  934. if(p_ae_block->histogram.bands[1].value <= 0x4000)
  935. {
  936. // 0x10 ~ 0x3F区域的能量较小
  937. //ratio = 8; // 1024 * 8
  938. _step = p_ae_block->exposure_quant * 4;
  939. //_step = p_ae_block->exposure_quant / 2;
  940. XM_printf ("SC-UE-0 2.00\r\n");
  941. }
  942. else
  943. {
  944. //ratio = 4; // 1024 * 4
  945. _step = p_ae_block->exposure_quant * 2;
  946. //_step = p_ae_block->exposure_quant / 2;
  947. XM_printf ("SC-UE-0 1.00\r\n");
  948. }
  949. }
  950. // 高光区 (0x80 ~ 0xFF)的比例很低
  951. // 将 中色调的区域(0x40~0x7F)映射到(0x80~0xC0)
  952. else if( (p_ae_block->histogram.bands[4].value + p_ae_block->histogram.bands[3].value) < 0x00C0 )
  953. {
  954. // 曝光量增加0.5倍. (*1.5)
  955. // 根据 中色调/低光 的比例决定放大倍数
  956. if(p_ae_block->histogram.bands[2].value <= 0x1000)
  957. {
  958. // 0.375
  959. //ratio = 3; // 2048
  960. _step = p_ae_block->exposure_quant * 4; // 1024
  961. //_step = p_ae_block->exposure_quant / 2;
  962. XM_printf ("SC-UE-1 0.375\r\n");
  963. }
  964. else if(p_ae_block->histogram.bands[2].value <= 0x2000)
  965. {
  966. // 0.25
  967. //ratio = 1; // 2048
  968. _step = p_ae_block->exposure_quant * 4; // 1024
  969. //_step = p_ae_block->exposure_quant / 2;
  970. XM_printf ("SC-UE-1 0.250\r\n");
  971. }
  972. else if(p_ae_block->histogram.bands[2].value <= 0x4000)
  973. {
  974. // 0.125
  975. //ratio = 1; // 2048
  976. _step = p_ae_block->exposure_quant * 2; // 512
  977. //_step = p_ae_block->exposure_quant / 2;
  978. XM_printf ("SC-UE-1 0.125\r\n");
  979. }
  980. else if(p_ae_block->histogram.bands[2].value <= 0x8000)
  981. {
  982. // 0.0625
  983. //ratio = 1; // 2048
  984. _step = p_ae_block->exposure_quant * 1; // 256
  985. //_step = p_ae_block->exposure_quant / 2;
  986. XM_printf ("SC-UE-1 0.0625\r\n");
  987. }
  988. else
  989. {
  990. }
  991. }
  992. // 最右侧高光区 (0xC0 ~ 0xFF)的比例很低
  993. // 高光区 (0xC0 ~ 0xFF)对曝光增强速度的影响非常有限
  994. else if( p_ae_block->histogram.bands[4].value < 0x00C0)
  995. {
  996. if(p_ae_block->increment < p_ae_block->increment_offset)
  997. {
  998. //
  999. }
  1000. else
  1001. {
  1002. // 曝光量增加1/64
  1003. //ratio = 1; // 2048
  1004. _step = p_ae_block->exposure_quant/2; // 64
  1005. XM_printf ("SC-UE-2 0.016\r\n");
  1006. }
  1007. }
  1008. }
  1009. if(_step < _step_1st)
  1010. _step = _step_1st;
  1011. /*
  1012. // 曝光逆转过程增量衰减控制
  1013. if(p_ae_block->increment > p_ae_block->increment_offset && p_ae_block->histogram.hist_error < 0)
  1014. {
  1015. // 曝光增加过程中,检测到曝光过度. (即曝光过程逆转, 曝光反相衰减中)
  1016. // 将步长缩至计算增量的1/4
  1017. _step = _step/4;
  1018. // 进一步检查上面修正的步长是否大于最近的步长, 因为最近的步长导致曝光过程逆转,
  1019. // 那么新的曝光反相衰减过程中, 步长应不大于最近的步长
  1020. if(_step > (u32_t)p_ae_block->increment_step/2)
  1021. _step = p_ae_block->increment_step/2;
  1022. // 使能增量阻尼
  1023. p_ae_block->increment_damping = 1;
  1024. if(p_ae_block->steady_control.enable == 0)
  1025. {
  1026. if(p_ae_block->steady_control.count == 0)
  1027. {
  1028. // 保留当前的曝光参考
  1029. p_ae_block->steady_control.steady_error = p_ae_block->histogram.hist_error;
  1030. p_ae_block->steady_control.exposure = exp_cmos_block->last_exposure;
  1031. }
  1032. else if(abs(p_ae_block->steady_control.steady_error) > abs(p_ae_block->histogram.hist_error))
  1033. {
  1034. // 保留绝对值下的曝光参考
  1035. p_ae_block->steady_control.steady_error = p_ae_block->histogram.hist_error;
  1036. p_ae_block->steady_control.exposure = exp_cmos_block->last_exposure;
  1037. }
  1038. p_ae_block->steady_control.count ++;
  1039. if(p_ae_block->steady_control.count >= 3)
  1040. {
  1041. p_ae_block->steady_control.count = 0;
  1042. p_ae_block->steady_control.enable = 1;
  1043. XM_printf ("\t\t Steady Enabel 0, error=%d, exposure=%d\r\n",
  1044. p_ae_block->steady_control.steady_error,
  1045. p_ae_block->steady_control.exposure);
  1046. }
  1047. }
  1048. }
  1049. else if(p_ae_block->increment < p_ae_block->increment_offset && p_ae_block->histogram.hist_error > 0)
  1050. {
  1051. // 曝光减少过程中,检测到曝光不够. (即曝光过程逆转, 曝光反相增加中)
  1052. // 将步长缩至计算增量的1/4
  1053. _step = _step/4;
  1054. // 进一步检查上面修正的步长是否大于最近的步长, 因为最近的步长导致曝光过程逆转,
  1055. // 那么新的曝光反相增长过程中, 步长应不大于最近的步长
  1056. if(_step > (u32_t)p_ae_block->increment_step/2)
  1057. _step = p_ae_block->increment_step/2;
  1058. // 使能增量阻尼
  1059. p_ae_block->increment_damping = 1;
  1060. if(p_ae_block->steady_control.enable == 0)
  1061. {
  1062. if(p_ae_block->steady_control.count == 0)
  1063. {
  1064. // 保留当前的曝光参考
  1065. p_ae_block->steady_control.steady_error = p_ae_block->histogram.hist_error;
  1066. p_ae_block->steady_control.exposure = exp_cmos_block->last_exposure;
  1067. }
  1068. else if(abs(p_ae_block->steady_control.steady_error) > abs(p_ae_block->histogram.hist_error))
  1069. {
  1070. // 保留绝对值下的曝光参考
  1071. p_ae_block->steady_control.steady_error = p_ae_block->histogram.hist_error;
  1072. p_ae_block->steady_control.exposure = exp_cmos_block->last_exposure;
  1073. }
  1074. p_ae_block->steady_control.count ++;
  1075. if(p_ae_block->steady_control.count >= 3)
  1076. {
  1077. p_ae_block->steady_control.count = 0;
  1078. p_ae_block->steady_control.enable = 1;
  1079. XM_printf ("\t\t Steady Enabel 1, error=%d, exposure=%d\r\n",
  1080. p_ae_block->steady_control.steady_error,
  1081. p_ae_block->steady_control.exposure);
  1082. }
  1083. }
  1084. }
  1085. else if(p_ae_block->increment_damping)
  1086. {
  1087. // 关闭增量阻尼
  1088. _step = _step/2;
  1089. p_ae_block->increment_damping = 0;
  1090. if(p_ae_block->steady_control.enable == 0)
  1091. p_ae_block->steady_control.count = 0;
  1092. }
  1093. else
  1094. {
  1095. if(p_ae_block->steady_control.enable == 0)
  1096. p_ae_block->steady_control.count = 0;
  1097. }
  1098. */
  1099. #if 0
  1100. _step = _step / 16;
  1101. if(_step < 1)
  1102. _step = 1;
  1103. #else
  1104. //_step = (_step + 8) / 16;
  1105. // _step = (_step + 4) / 8;
  1106. if(_step < 1)
  1107. _step = 1;
  1108. #endif
  1109. p_ae_block->increment_step = (u16_t)_step;
  1110. p_ae_block->increment_ratio = (u16_t)ratio;
  1111. if(p_ae_block->increment_offset < _step)
  1112. p_ae_block->increment_min = 1;
  1113. else
  1114. p_ae_block->increment_min = p_ae_block->increment_offset - _step;
  1115. p_ae_block->increment_max = p_ae_block->increment_offset + _step * ratio;
  1116. if(p_ae_block->histogram.hist_error > 8)
  1117. //if(p_ae_block->histogram.hist_error > 4)
  1118. //if(p_ae_block->histogram.hist_error > 2)
  1119. {
  1120. // 直方图整体偏左, 增加曝光
  1121. p_ae_block->increment = p_ae_block->increment_max;
  1122. }
  1123. else if(p_ae_block->histogram.hist_error < -8)
  1124. //else if(p_ae_block->histogram.hist_error < -4)
  1125. //else if(p_ae_block->histogram.hist_error < -2)
  1126. {
  1127. // 直方图整体偏右, 减少曝光
  1128. p_ae_block->increment = p_ae_block->increment_min;
  1129. }
  1130. else
  1131. {
  1132. // 直方图整体平衡, 保持当前曝光值
  1133. p_ae_block->increment = p_ae_block->increment_offset;
  1134. }
  1135. /*
  1136. // 稳态控制过程下的解除稳态控制检查
  1137. if(p_ae_block->steady_control.enable)
  1138. {
  1139. // 检查是否是进入稳态控制过程后的第一次曝光
  1140. if(old_steady_enable == 1)
  1141. {
  1142. // 进入稳态控制过程后的非第一次曝光
  1143. // 检查稳态控制曝光错误的边界阈值是否超出
  1144. if( abs(p_ae_block->steady_control.steady_error - p_ae_block->histogram.hist_error) >= STEADY_CONTROL_AE_ERR_THREAD )
  1145. {
  1146. // 曝光错误误差已超出稳态控制的范围, 解除稳态控制
  1147. // 关闭稳态控制
  1148. p_ae_block->steady_control.enable = 0;
  1149. p_ae_block->steady_control.count = 0;
  1150. XM_printf ("\t\t Steady disable\r\n");
  1151. }
  1152. }
  1153. }
  1154. // 稳态控制过程下的曝光值及增量设定
  1155. if(p_ae_block->steady_control.enable)
  1156. {
  1157. // 进入稳态状态过程后, 锁定稳态过程的每次曝光的曝光值为同一个曝光值设定, 这样避免曝光值非稳态导致的闪烁
  1158. exp_cmos_block->exposure = p_ae_block->steady_control.exposure;
  1159. // 锁定曝光增量为1
  1160. p_ae_block->increment = p_ae_block->increment_offset;
  1161. // XM_printf ("\t\t Steady Control, exposure=%d, inc=%d\r\n", exp_cmos_block->exposure, p_ae_block->increment);
  1162. }
  1163. */
  1164. /*
  1165. XM_printf ("\t\tstep = %4d, offset = %10d, max = %10d, min = %10d\n",
  1166. _step,
  1167. p_ae_block->increment_offset,
  1168. p_ae_block->increment_max,
  1169. p_ae_block->increment_min);
  1170. */
  1171. }
  1172. static int cmos_exposure_get_error (cmos_exposure_ptr_t exp_cmos_block, int index)
  1173. {
  1174. if(exp_cmos_block->stat_count < AE_STAT_COUNT)
  1175. return 0;
  1176. index = exp_cmos_block->stat_index - 1 - index;
  1177. if(index < 0)
  1178. index += AE_STAT_COUNT;
  1179. return exp_cmos_block->stat_error[index];
  1180. }
  1181. #define min(a,b) ((a < b) ? a : b)
  1182. #define max(a,b) ((a > b) ? a : b)
  1183. static int steady_ae_enable = 1; // AE稳态控制使能, 消除曝光振荡现象
  1184. void cmos_exposure_set_steady_ae (int enable)
  1185. {
  1186. steady_ae_enable = enable;
  1187. }
  1188. extern isp_ae_t p_ae;
  1189. static int auto_exposure_increment_calculate (auto_exposure_ptr_t p_ae_block, cmos_exposure_ptr_t exp_cmos_block)
  1190. {
  1191. //histogram_update(p_ae_block, &(p_ae_block->histogram), &p_ae);
  1192. histogram_3x3_update (p_ae_block, &(p_ae_block->histogram), &p_ae);
  1193. //isp_auto_exposure_compensation(p_ae_block, p_ae_block->histogram.bands);
  1194. if(steady_ae_enable)
  1195. {
  1196. if(exp_cmos_block->stat_count < AE_STAT_COUNT)
  1197. {
  1198. exp_cmos_block->stat_error[exp_cmos_block->stat_count] = p_ae_block->histogram.hist_error;
  1199. exp_cmos_block->stat_lum[exp_cmos_block->stat_count] = p_ae.lumCurr; // isp_ae_lum_read ();
  1200. exp_cmos_block->stat_count ++;
  1201. }
  1202. else
  1203. {
  1204. unsigned int index = exp_cmos_block->stat_index;
  1205. int error_0, error_1, error_2;
  1206. int diff, min_error, max_error;
  1207. exp_cmos_block->stat_error[index] = p_ae_block->histogram.hist_error;
  1208. exp_cmos_block->stat_lum[index] = p_ae.lumCurr; //isp_ae_lum_read ();
  1209. index ++;
  1210. if(index >= AE_STAT_COUNT)
  1211. index = 0;
  1212. exp_cmos_block->stat_index = index;
  1213. // 读取最近的AE错误值
  1214. error_0 = cmos_exposure_get_error(exp_cmos_block, 0);
  1215. error_1 = cmos_exposure_get_error(exp_cmos_block, 1);
  1216. error_2 = cmos_exposure_get_error(exp_cmos_block, 2);
  1217. // 20181023 检查异常情况
  1218. // 1) 若整体hist_error_one与极小值hist_error_min极性一致,画面应存在较明显的过曝或者曝光不够,此时应立刻解除稳态控制,
  1219. // 否则画面会出现明显偏暗的场景
  1220. if(p_ae_block->histogram.hist_error_min < -6 && p_ae_block->histogram.hist_error_one < -6)
  1221. {
  1222. // 解除稳态控制
  1223. exp_cmos_block->locked = 0;
  1224. exp_cmos_block->locked_threshhold = 0;
  1225. goto step_calc;
  1226. }
  1227. else if(p_ae_block->histogram.hist_error_min > 6 && p_ae_block->histogram.hist_error_one > 6)
  1228. {
  1229. // 解除稳态控制
  1230. exp_cmos_block->locked = 0;
  1231. exp_cmos_block->locked_threshhold = 0;
  1232. goto step_calc;
  1233. }
  1234. // 检查稳态控制是否已应用
  1235. if(exp_cmos_block->locked)
  1236. {
  1237. // 振荡保护中
  1238. if(exp_cmos_block->locked_threshhold > 0)
  1239. {
  1240. if(error_0 > 0 && error_0 <= exp_cmos_block->locked_threshhold)
  1241. {
  1242. // 小于阈值, 被阻止
  1243. XM_printf ("error = %d, locked_threshhold = %d, blocking\n", error_0, exp_cmos_block->locked_threshhold);
  1244. return 1;
  1245. }
  1246. else
  1247. {
  1248. // 阻止解除
  1249. XM_printf ("error = %d, locked_threshhold = %d, blocking leave\n", error_0, exp_cmos_block->locked_threshhold);
  1250. exp_cmos_block->locked = 0;
  1251. exp_cmos_block->locked_threshhold = 0;
  1252. }
  1253. }
  1254. else
  1255. {
  1256. if(error_0 < 0 && error_0 >= exp_cmos_block->locked_threshhold)
  1257. {
  1258. // 大于阈值, 被阻止
  1259. XM_printf ("error = %d, locked_threshhold = %d, blocking\n", error_0, exp_cmos_block->locked_threshhold);
  1260. return 1;
  1261. }
  1262. else
  1263. {
  1264. // 阻止解除
  1265. XM_printf ("error = %d, locked_threshhold = %d, blocking leave\n", error_0, exp_cmos_block->locked_threshhold);
  1266. exp_cmos_block->locked = 0;
  1267. exp_cmos_block->locked_threshhold = 0;
  1268. }
  1269. }
  1270. }
  1271. else
  1272. {
  1273. // 判断是否存在振荡
  1274. if(error_0 > 0 && error_1 < 0 && error_2 > 0 )
  1275. {
  1276. // 振荡, 11, -29, 10
  1277. exp_cmos_block->locked = 1;
  1278. diff = abs(error_0 - error_2);
  1279. max_error = max(error_0, error_2);
  1280. exp_cmos_block->locked_threshhold = max_error + max(3, diff);
  1281. if(exp_cmos_block->locked_threshhold > 255)
  1282. {
  1283. // 复位
  1284. exp_cmos_block->stat_count = 0;
  1285. exp_cmos_block->locked = 0;
  1286. exp_cmos_block->locked_threshhold = 0;
  1287. goto step_calc;
  1288. }
  1289. XM_printf ("error = %d, locked_threshhold = %d, blocking enter\n", error_0, exp_cmos_block->locked_threshhold);
  1290. return 1;
  1291. }
  1292. else if(error_0 < 0 && error_1 > 0 && error_2 < 0 )
  1293. {
  1294. // 振荡, -9, 11, -11
  1295. exp_cmos_block->locked = 1;
  1296. diff = abs(error_0 - error_2);
  1297. min_error = min(error_0, error_2);
  1298. exp_cmos_block->locked_threshhold = min_error - max(3, diff);
  1299. if(exp_cmos_block->locked_threshhold < -255)
  1300. {
  1301. // 复位
  1302. exp_cmos_block->stat_count = 0;
  1303. exp_cmos_block->locked = 0;
  1304. exp_cmos_block->locked_threshhold = 0;
  1305. goto step_calc;
  1306. }
  1307. XM_printf ("error = %d, locked_threshhold = %d, blocking enter\n", error_0, exp_cmos_block->locked_threshhold);
  1308. return 1;
  1309. }
  1310. }
  1311. }
  1312. }
  1313. step_calc:
  1314. auto_exposure_step_calculate(p_ae_block, exp_cmos_block);
  1315. return 0;
  1316. }
  1317. int isp_auto_exposure_run (auto_exposure_ptr_t ae_block, cmos_exposure_ptr_t exp_cmos_block, int man_exp)
  1318. {
  1319. i64_t i64_temp;
  1320. u32_t new_physical_exposure; // 新的绝对(物理)曝光量
  1321. int ae_stable = 1;
  1322. if(man_exp == 1)
  1323. {
  1324. exp_cmos_block->exposure = ae_block->exposure_target;
  1325. }
  1326. exp_cmos_block->last_exposure = exp_cmos_block->exposure;
  1327. // 估计曝光量
  1328. int blocking = auto_exposure_increment_calculate(ae_block, exp_cmos_block);
  1329. if(blocking)
  1330. {
  1331. return ae_stable;
  1332. }
  1333. if(man_exp == 1)
  1334. {
  1335. ae_block->increment = ae_block->increment_offset;
  1336. }
  1337. // 根据曝光量, 重新计算sensor的曝光参数
  1338. isp_exposure_cmos_calculate (
  1339. exp_cmos_block,
  1340. ae_block->increment,
  1341. ae_block->increment_max,
  1342. ae_block->increment_min,
  1343. ae_block->increment_offset
  1344. );
  1345. i64_temp = exp_cmos_block->cmos_inttime.exposure_ashort * exp_cmos_block->sys_factor;
  1346. i64_temp *= exp_cmos_block->cmos_gain.again * exp_cmos_block->cmos_gain.dgain;
  1347. i64_temp >>= (exp_cmos_block->cmos_gain.again_shift + exp_cmos_block->cmos_gain.dgain_shift);
  1348. new_physical_exposure = (u32_t)i64_temp;
  1349. // 20170102 zhuoyonghong
  1350. // 为避免出现曝光不稳的现象, 不再执行逻辑曝光加速过程(循环执行逻辑曝光过程直至物理曝光出现差异).
  1351. // 逻辑曝光过程的理论曝光值不等于实际的物理曝光量, 会引入误差(物理曝光量-理论曝光量).
  1352. // 连续多次逻辑曝光过程产生的物理曝光量误差(物理曝光量-理论曝光量)会更大.
  1353. // 因此取消该逻辑曝光加速过程, 每次逐步执行
  1354. // 1)逻辑曝光量计算-->2)物理曝光量计算-->3)物理曝光 -->4曝光反馈
  1355. // 的过程, 避免较大曝光量误差的引入. 较大曝光量误差会导致画面亮度抖动
  1356. #if 0
  1357. if(man_exp == 0)
  1358. {
  1359. // 自动曝光模式下
  1360. //int loop = 16;
  1361. int loop = 1;
  1362. int same_exp = 0;
  1363. // 循环直到物理曝光量存在变化
  1364. while (loop > 0 && new_physical_exposure == exp_cmos_block->physical_exposure)
  1365. {
  1366. if(new_physical_exposure == exp_cmos_block->exp_llimit) // 最小曝光量
  1367. break;
  1368. // 使用与进入时同样的曝光量增量参数, 计算下一个理论的曝光量
  1369. // auto_exposure_step_calculate (ae_block, exp_cmos_block);
  1370. // 根据计算的理论曝光量, 重新计算sensor的曝光参数
  1371. same_exp = isp_exposure_cmos_calculate (
  1372. exp_cmos_block,
  1373. ae_block->increment,
  1374. ae_block->increment_max,
  1375. ae_block->increment_min,
  1376. ae_block->increment_offset
  1377. );
  1378. if(same_exp)
  1379. break;
  1380. //
  1381. i64_temp = exp_cmos_block->cmos_inttime.exposure_ashort * exp_cmos_block->sys_factor;
  1382. i64_temp *= exp_cmos_block->cmos_gain.again * exp_cmos_block->cmos_gain.dgain;
  1383. i64_temp >>= (exp_cmos_block->cmos_gain.again_shift + exp_cmos_block->cmos_gain.dgain_shift);
  1384. new_physical_exposure = (u32_t)i64_temp;
  1385. loop --;
  1386. }
  1387. if(new_physical_exposure == exp_cmos_block->physical_exposure)
  1388. {
  1389. // 连续的绝对曝光量相同
  1390. if( (ae_block->increment == ae_block->increment_offset)
  1391. || (new_physical_exposure == exp_cmos_block->exp_llimit)
  1392. || (exp_cmos_block->exposure == exp_cmos_block->exp_tlimit)
  1393. || (same_exp == 1)
  1394. )
  1395. ae_stable = 1;
  1396. else
  1397. ae_stable = 0;
  1398. }
  1399. else
  1400. ae_stable = 0;
  1401. }
  1402. #else
  1403. // 比较新的物理曝光值与最近的值比较. 相同则认为曝光稳定
  1404. if(exp_cmos_block->physical_exposure == new_physical_exposure)
  1405. ae_stable = 1;
  1406. else
  1407. ae_stable = 0;
  1408. #endif
  1409. exp_cmos_block->physical_exposure = new_physical_exposure;
  1410. /*
  1411. XM_printf ("\t\tlog_exposure=%10d, inttime=%5d, again=%f, dgain=%f\n",
  1412. (u32_t)new_physical_exposure,
  1413. exp_cmos_block->cmos_inttime.exposure_ashort,
  1414. ((float)exp_cmos_block->cmos_gain.again) / (1 << exp_cmos_block->cmos_gain.again_shift),
  1415. ((float)exp_cmos_block->cmos_gain.dgain) / (1 << exp_cmos_block->cmos_gain.dgain_shift));
  1416. */
  1417. // 调整sensor的曝光参数
  1418. exp_cmos_block->exposure_delay_cycle = isp_cmos_inttime_update (exp_cmos_block);
  1419. return ae_stable;
  1420. }
  1421. #define MAX_EXPOSURE_LUTS 3
  1422. static u16_t exposure_luts_defaults[MAX_EXPOSURE_LUTS][4] =
  1423. {
  1424. {0xA000, 0x1400, 0x0180, 0x0110 },
  1425. {0x8000, 0x4000, 0x0800, 0x0100 },
  1426. {0x2000, 0x3000, 0x0700, 0x0220 }
  1427. };
  1428. void auto_exposure_lut_load (auto_exposure_ptr_t p_ae_block, u8_t lut)
  1429. {
  1430. int i;
  1431. if(MAX_EXPOSURE_LUTS <= lut)
  1432. {
  1433. return;
  1434. }
  1435. for(i=0;i<HISTOGRAM_BANDS;++i)
  1436. {
  1437. p_ae_block->histogram.bands[i].target = exposure_luts_defaults[lut][i];
  1438. }
  1439. }