BayerISP.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #ifndef SAMPLE_COMMON_ISP_HPP_
  2. #define SAMPLE_COMMON_ISP_HPP_
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <string>
  7. #include <vector>
  8. #include <algorithm>
  9. #include "TyIsp.h"
  10. /**
  11. *The RGB image data output by some cameras is the original Bayer array.
  12. *By calling the API provided by this file, Bayer data can be converted to BGR array.
  13. *You can refer to the sample code: SimpleView_FetchFrame.
  14. */
  15. static int __TYCompareFirmwareVersion(const TY_DEVICE_BASE_INFO &info, int major, int minor){
  16. const TY_VERSION_INFO &v = info.firmwareVersion;
  17. if (v.major < major){
  18. return -1;
  19. }
  20. if (v.major == major && v.minor < minor){
  21. return -1;
  22. }
  23. if (v.major == major && v.minor == minor){
  24. return 0;
  25. }
  26. return 1;
  27. }
  28. static TY_STATUS __TYDetectOldVer21ColorCam(TY_DEV_HANDLE dev_handle,bool *is_v21_color_device){
  29. TY_DEVICE_BASE_INFO info;
  30. TY_STATUS res = TYGetDeviceInfo(dev_handle, &info);
  31. if (res != TY_STATUS_OK){
  32. LOGI("get device info failed");
  33. return res;
  34. }
  35. *is_v21_color_device = false;
  36. if (info.iface.type == TY_INTERFACE_USB){
  37. *is_v21_color_device = true;
  38. }
  39. if ((info.iface.type == TY_INTERFACE_ETHERNET || info.iface.type == TY_INTERFACE_RAW) &&
  40. __TYCompareFirmwareVersion(info, 2, 2) < 0){
  41. *is_v21_color_device = true;
  42. }
  43. return TY_STATUS_OK;
  44. }
  45. static void __TYParseSizeFromImageMode(TY_IMAGE_MODE mode , int *image_size) {
  46. const int mask = ((0x01 << 12) - 1);
  47. int height = mode & mask;
  48. int width = (mode >> 12) & mask;
  49. image_size[0] = width;
  50. image_size[1] = height;
  51. }
  52. ///init color isp setting
  53. ///for bayer raw image process
  54. static TY_STATUS ColorIspInitSetting(TY_ISP_HANDLE isp_handle, TY_DEV_HANDLE dev_handle){
  55. bool is_v21_color_device ;
  56. TY_STATUS res = __TYDetectOldVer21ColorCam(dev_handle, &is_v21_color_device);//old version device has different config
  57. if (res != TY_STATUS_OK){
  58. return res;
  59. }
  60. if (is_v21_color_device){
  61. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_BLACK_LEVEL, 11));
  62. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_BLACK_LEVEL_GAIN, 256.f / (256 - 11)));
  63. }
  64. else{
  65. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_BLACK_LEVEL, 0));
  66. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_BLACK_LEVEL_GAIN, 1.f));
  67. bool b;
  68. ASSERT_OK(TYHasFeature(dev_handle, TY_COMPONENT_RGB_CAM, TY_INT_ANALOG_GAIN, &b));
  69. if (b){
  70. TYSetInt(dev_handle, TY_COMPONENT_RGB_CAM, TY_INT_ANALOG_GAIN, 1);
  71. }
  72. }
  73. TYISPSetFeature(isp_handle, TY_ISP_FEATURE_BAYER_PATTERN, TY_ISP_BAYER_AUTO);
  74. float shading[9] = { 0.30890417098999026, 10.63355541229248, -6.433426856994629,
  75. 0.24413758516311646, 11.739893913269043, -8.148622512817383,
  76. 0.1255662441253662, 11.88359546661377, -7.865192413330078 };
  77. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_SHADING, (uint8_t*)shading, sizeof(shading)));
  78. int shading_center[2] = { 640, 480 };
  79. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_SHADING_CENTER, (uint8_t*)shading_center, sizeof(shading_center)));
  80. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_CCM_ENABLE, 0));//we are not using ccm by default
  81. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_CAM_DEV_HANDLE, (uint8_t*)&dev_handle, sizeof(dev_handle)));
  82. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_CAM_DEV_COMPONENT, int32_t(TY_COMPONENT_RGB_CAM)));
  83. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_GAMMA, 1.f));
  84. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_AUTOBRIGHT, 1));//enable auto bright control
  85. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_ENABLE_AUTO_EXPOSURE_GAIN, 0));//disable ae by default
  86. int default_image_size[2] = { 1280, 960 };// image size
  87. int current_image_size[2] = { 1280, 960 };// image size for current parameters
  88. TY_IMAGE_MODE img_mode;
  89. #if 1
  90. res = TYGetEnum(dev_handle, TY_COMPONENT_RGB_CAM, TY_ENUM_IMAGE_MODE, &img_mode);
  91. if (res == TY_STATUS_OK) {
  92. __TYParseSizeFromImageMode(img_mode, current_image_size);
  93. }
  94. TY_ENUM_ENTRY mode_entry[10];
  95. uint32_t num;
  96. res = TYGetEnumEntryInfo(dev_handle, TY_COMPONENT_RGB_CAM, TY_ENUM_IMAGE_MODE, mode_entry, 10, &num);
  97. if (res == TY_STATUS_OK) {
  98. __TYParseSizeFromImageMode(mode_entry[0].value, default_image_size);
  99. }
  100. #else
  101. //some device may not support WIDTH & HEIGHT feature. image mode is recommended
  102. TYGetInt(dev_handle, TY_COMPONENT_RGB_CAM, TY_INT_WIDTH, &image_size[0]);
  103. TYGetInt(dev_handle, TY_COMPONENT_RGB_CAM, TY_INT_HEIGHT, &image_size[1]);
  104. #endif
  105. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_IMAGE_SIZE, (uint8_t*)&default_image_size, sizeof(default_image_size)));//the orignal raw image size
  106. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_INPUT_RESAMPLE_SCALE, default_image_size[0] / current_image_size[0]));//resampled input
  107. #if 1
  108. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_ENABLE_AUTO_WHITEBALANCE, 1)); //eanble auto white balance
  109. #else
  110. //manual wb gain control
  111. const float wb_rgb_gain[3] = { 2.0123140811920168, 1, 1.481866478919983 };
  112. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_WHITEBALANCE_GAIN, (uint8_t*)wb_rgb_gain, sizeof(wb_rgb_gain)));
  113. #endif
  114. //try to load specifical device config from device storage
  115. TY_COMPONENT_ID comp_all;
  116. ASSERT_OK(TYGetComponentIDs(dev_handle, &comp_all));
  117. if (!(comp_all & TY_COMPONENT_STORAGE)){
  118. return TY_STATUS_OK;
  119. }
  120. bool has_isp_block = false;
  121. ASSERT_OK(TYHasFeature(dev_handle, TY_COMPONENT_STORAGE, TY_BYTEARRAY_ISP_BLOCK, &has_isp_block));
  122. if (!has_isp_block){
  123. return TY_STATUS_OK;
  124. }
  125. uint32_t sz = 0;
  126. ASSERT_OK(TYGetByteArraySize(dev_handle, TY_COMPONENT_STORAGE, TY_BYTEARRAY_ISP_BLOCK, &sz));
  127. if (sz <= 0){
  128. return TY_STATUS_OK;
  129. }
  130. std::vector<uint8_t> buff(sz);
  131. ASSERT_OK(TYGetByteArray(dev_handle, TY_COMPONENT_STORAGE, TY_BYTEARRAY_ISP_BLOCK, &buff[0], buff.size()));
  132. res = TYISPLoadConfig(isp_handle, &buff[0], buff.size());
  133. if (res == TY_STATUS_OK){
  134. LOGD("Load RGB ISP Config From Device");
  135. }
  136. return TY_STATUS_OK;
  137. }
  138. static TY_STATUS ColorIspInitAutoExposure(TY_ISP_HANDLE isp_handle, TY_DEV_HANDLE dev_handle){
  139. bool is_v21_color_device;
  140. TY_STATUS res = __TYDetectOldVer21ColorCam(dev_handle, &is_v21_color_device);//old version device has different config
  141. if (res != TY_STATUS_OK){
  142. return res;
  143. }
  144. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_ENABLE_AUTO_EXPOSURE_GAIN, 1));
  145. // do not enable gain auto control by default
  146. # if 1
  147. int auto_gain_range[2] = { -1, -1 };
  148. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_AUTO_GAIN_RANGE, (uint8_t*)&auto_gain_range, sizeof(auto_gain_range)));
  149. #else
  150. if(is_v21_color_device){
  151. const int old_auto_gain_range[2] = { 33, 255 };
  152. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_AUTO_GAIN_RANGE, (uint8_t*)&old_auto_gain_range, sizeof(old_auto_gain_range)));
  153. }
  154. else{
  155. #define CHECK_GO_FAILED(a) {if((a)!=TY_STATUS_OK) break;}
  156. do{
  157. TY_FEATURE_ID_LIST feature_id = TY_INT_GAIN;
  158. bool val;
  159. CHECK_GO_FAILED(TYHasFeature(dev_handle, TY_COMPONENT_RGB_CAM, TY_INT_GAIN, &val));
  160. if (val) {
  161. feature_id = TY_INT_GAIN;
  162. }
  163. CHECK_GO_FAILED(TYHasFeature(dev_handle, TY_COMPONENT_RGB_CAM, TY_INT_R_GAIN, &val));
  164. if (val) {
  165. feature_id = TY_INT_R_GAIN;
  166. }
  167. int auto_gain_range[2] = { 15, 255 };
  168. TY_INT_RANGE range;
  169. CHECK_GO_FAILED(TYGetIntRange(dev_handle, TY_COMPONENT_RGB_CAM, feature_id, &range));
  170. auto_gain_range[0] = std::min(range.min + 1, range.max);
  171. auto_gain_range[1] = std::max(range.max - 1, range.min);
  172. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_AUTO_GAIN_RANGE, (uint8_t*)&auto_gain_range, sizeof(auto_gain_range)));
  173. } while(0);
  174. #undef CHECK_GO_FAILED
  175. }
  176. #endif
  177. //constraint exposure time
  178. int auto_expo_range[2] = { 10, 100 };
  179. TY_INT_RANGE range;
  180. res = TYGetIntRange(dev_handle, TY_COMPONENT_RGB_CAM, TY_INT_EXPOSURE_TIME, &range);
  181. if (res == TY_STATUS_OK) {
  182. auto_expo_range[0] = std::min(range.min + 1, range.max);
  183. auto_expo_range[1] = std::max(range.max - 1, range.min);
  184. }
  185. ASSERT_OK(TYISPSetFeature(isp_handle, TY_ISP_FEATURE_AUTO_EXPOSURE_RANGE, (uint8_t*)&auto_expo_range, sizeof(auto_expo_range)));
  186. return TY_STATUS_OK;
  187. }
  188. static TY_STATUS ColorIspShowSupportedFeatures(TY_ISP_HANDLE handle){
  189. int sz;
  190. TY_STATUS res = TYISPGetFeatureInfoListSize(handle,&sz);
  191. if (res != TY_STATUS_OK){
  192. return res;
  193. }
  194. std::vector<TY_ISP_FEATURE_INFO> info;
  195. info.resize(sz);
  196. TYISPGetFeatureInfoList(handle, &info[0], info.size());
  197. for (int idx = 0; idx < sz; idx++){
  198. printf("feature name : %-50s type : %s \n", info[idx].name, info[idx].value_type);
  199. }
  200. return TY_STATUS_OK;
  201. }
  202. #endif