Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 1 | /* |
| 2 | * x3a_analyzer_aiq.h - 3a analyzer from AIQ |
| 3 | * |
| 4 | * Copyright (c) 2014-2015 Intel Corporation |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | * Author: Wind Yuan <feng.yuan@intel.com> |
| 19 | */ |
| 20 | |
| 21 | #include "x3a_analyzer_aiq.h" |
| 22 | #include "aiq_handler.h" |
| 23 | #include "isp_controller.h" |
| 24 | #include "xcam_cpf_reader.h" |
| 25 | #include "ia_types.h" |
| 26 | |
| 27 | namespace XCam { |
| 28 | |
| 29 | class CpfReader { |
| 30 | public: |
| 31 | explicit CpfReader (const char *name); |
| 32 | ~CpfReader(); |
| 33 | bool read (ia_binary_data &binary); |
| 34 | private: |
| 35 | XCamCpfBlob *_aiq_cpf; |
| 36 | char *_name; |
| 37 | }; |
| 38 | |
| 39 | CpfReader::CpfReader (const char *name) |
| 40 | : _name (strdup(name)) |
| 41 | { |
| 42 | _aiq_cpf = xcam_cpf_blob_new (); |
| 43 | XCAM_ASSERT (name); |
| 44 | } |
| 45 | CpfReader::~CpfReader() |
| 46 | { |
| 47 | if (_aiq_cpf) |
| 48 | xcam_cpf_blob_free (_aiq_cpf); |
| 49 | if (_name) |
| 50 | xcam_free (_name); |
| 51 | } |
| 52 | |
| 53 | bool CpfReader::read (ia_binary_data &binary) |
| 54 | { |
| 55 | if (!xcam_cpf_read (_name, _aiq_cpf, NULL)) { |
| 56 | XCAM_LOG_ERROR ("parse CPF(%s) failed", XCAM_STR (_name)); |
| 57 | return false; |
| 58 | } |
| 59 | binary.data = _aiq_cpf->data; |
| 60 | binary.size = _aiq_cpf->size; |
| 61 | XCAM_LOG_INFO ("read cpf(%s) ok", XCAM_STR (_name)); |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | X3aAnalyzerAiq::X3aAnalyzerAiq (SmartPtr<IspController> &isp, const char *cpf_path) |
| 66 | : X3aAnalyzer ("X3aAnalyzerAiq") |
| 67 | , _isp (isp) |
| 68 | , _cpf_path (NULL) |
| 69 | { |
| 70 | if (cpf_path) |
| 71 | _cpf_path = strdup (cpf_path); |
| 72 | |
| 73 | _aiq_compositor = new AiqCompositor (); |
| 74 | XCAM_ASSERT (_aiq_compositor.ptr()); |
| 75 | xcam_mem_clear (&_sensor_mode_data); |
| 76 | |
| 77 | XCAM_LOG_DEBUG ("X3aAnalyzerAiq constructed"); |
| 78 | } |
| 79 | |
| 80 | X3aAnalyzerAiq::~X3aAnalyzerAiq() |
| 81 | { |
| 82 | if (_cpf_path) |
| 83 | xcam_free (_cpf_path); |
| 84 | |
| 85 | XCAM_LOG_DEBUG ("~X3aAnalyzerAiq destructed"); |
| 86 | } |
| 87 | |
| 88 | SmartPtr<AeHandler> |
| 89 | X3aAnalyzerAiq::create_ae_handler () |
| 90 | { |
| 91 | |
| 92 | SmartPtr<AiqAeHandler> ae_handler = new AiqAeHandler (_aiq_compositor); |
| 93 | _aiq_compositor->set_ae_handler (ae_handler); |
| 94 | return ae_handler; |
| 95 | } |
| 96 | |
| 97 | SmartPtr<AwbHandler> |
| 98 | X3aAnalyzerAiq::create_awb_handler () |
| 99 | { |
| 100 | SmartPtr<AiqAwbHandler> awb_handler = new AiqAwbHandler (_aiq_compositor); |
| 101 | _aiq_compositor->set_awb_handler (awb_handler); |
| 102 | return awb_handler; |
| 103 | } |
| 104 | |
| 105 | SmartPtr<AfHandler> |
| 106 | X3aAnalyzerAiq::create_af_handler () |
| 107 | { |
| 108 | |
| 109 | SmartPtr<AiqAfHandler> af_handler = new AiqAfHandler (_aiq_compositor); |
| 110 | _aiq_compositor->set_af_handler (af_handler); |
| 111 | return af_handler; |
| 112 | } |
| 113 | |
| 114 | SmartPtr<CommonHandler> |
| 115 | X3aAnalyzerAiq::create_common_handler () |
| 116 | { |
| 117 | SmartPtr<AiqCommonHandler> common_handler = new AiqCommonHandler (_aiq_compositor); |
| 118 | _aiq_compositor->set_common_handler (common_handler); |
| 119 | return common_handler; |
| 120 | } |
| 121 | |
| 122 | XCamReturn |
Wind Yuan | 60aa8ce | 2015-01-26 16:56:09 +0800 | [diff] [blame] | 123 | X3aAnalyzerAiq::internal_init (uint32_t width, uint32_t height, double framerate) |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 124 | { |
Wind Yuan | 60aa8ce | 2015-01-26 16:56:09 +0800 | [diff] [blame] | 125 | XCAM_UNUSED (framerate); |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 126 | XCAM_ASSERT (_cpf_path); |
| 127 | CpfReader reader (_cpf_path); |
| 128 | ia_binary_data binary; |
| 129 | |
| 130 | XCAM_ASSERT (_aiq_compositor.ptr()); |
| 131 | |
| 132 | xcam_mem_clear (&binary); |
| 133 | XCAM_FAIL_RETURN ( |
| 134 | ERROR, |
| 135 | reader.read(binary), |
| 136 | XCAM_RETURN_ERROR_AIQ, |
| 137 | "read cpf file(%s) failed", _cpf_path); |
| 138 | |
| 139 | _aiq_compositor->set_size (width, height); |
| 140 | XCAM_FAIL_RETURN ( |
| 141 | ERROR, |
| 142 | _aiq_compositor->open (binary), |
| 143 | XCAM_RETURN_ERROR_AIQ, |
| 144 | "AIQ open failed"); |
| 145 | |
| 146 | return XCAM_RETURN_NO_ERROR; |
| 147 | } |
| 148 | |
| 149 | XCamReturn |
| 150 | X3aAnalyzerAiq::internal_deinit () |
| 151 | { |
| 152 | if (_aiq_compositor.ptr ()) |
| 153 | _aiq_compositor->close (); |
| 154 | |
| 155 | return XCAM_RETURN_NO_ERROR; |
| 156 | } |
| 157 | |
| 158 | XCamReturn |
| 159 | X3aAnalyzerAiq::configure_3a () |
| 160 | { |
| 161 | XCamReturn ret = XCAM_RETURN_NO_ERROR; |
| 162 | X3aResultList first_results; |
| 163 | struct atomisp_sensor_mode_data sensor_mode_data; |
| 164 | xcam_mem_clear (&sensor_mode_data); |
| 165 | |
| 166 | XCAM_ASSERT (_isp.ptr()); |
| 167 | xcam_mem_clear (&sensor_mode_data); |
| 168 | |
| 169 | ret = _isp->get_sensor_mode_data (sensor_mode_data); |
| 170 | XCAM_FAIL_RETURN (WARNING, ret == XCAM_RETURN_NO_ERROR, ret, "get sensor mode data failed"); |
| 171 | |
| 172 | if (!_aiq_compositor->set_sensor_mode_data (&sensor_mode_data)) { |
| 173 | XCAM_LOG_WARNING ("AIQ configure 3a failed"); |
| 174 | return XCAM_RETURN_ERROR_AIQ; |
| 175 | } |
| 176 | |
| 177 | XCAM_LOG_DEBUG ("X3aAnalyzerAiq got sensor mode data, coarse_time_min:%u, " |
| 178 | "coarse_time_max_margin:%u, " |
| 179 | "fine_time_min:%u, fine_time_max_margin:%u, " |
| 180 | "fine_time_def:%u, " |
| 181 | "frame_length_lines:%u, line_length_pck:%u, " |
| 182 | "vt_pix_clk_freq_mhz:%u, " |
| 183 | "crop_horizontal_start:%u, crop_vertical_start:%u, " |
| 184 | "crop_horizontal_end:%u, crop_vertical_end:%u, " |
| 185 | "output_width:%u, output_height:%u, " |
| 186 | "binning_factor_x:%u, binning_factor_y:%u", |
| 187 | sensor_mode_data.coarse_integration_time_min, |
| 188 | sensor_mode_data.coarse_integration_time_max_margin, |
| 189 | sensor_mode_data.fine_integration_time_min, |
| 190 | sensor_mode_data.fine_integration_time_max_margin, |
| 191 | sensor_mode_data.fine_integration_time_def, |
| 192 | sensor_mode_data.frame_length_lines, |
| 193 | sensor_mode_data.line_length_pck, |
| 194 | sensor_mode_data.vt_pix_clk_freq_mhz, |
| 195 | sensor_mode_data.crop_horizontal_start, |
| 196 | sensor_mode_data.crop_vertical_start, |
| 197 | sensor_mode_data.crop_horizontal_end, |
| 198 | sensor_mode_data.crop_vertical_end, |
| 199 | sensor_mode_data.output_width, |
| 200 | sensor_mode_data.output_height, |
| 201 | (uint32_t)sensor_mode_data.binning_factor_x, |
| 202 | (uint32_t)sensor_mode_data.binning_factor_y); |
| 203 | |
| 204 | // initialize ae and awb |
| 205 | get_ae_handler ()->analyze (first_results); |
| 206 | get_awb_handler ()->analyze (first_results); |
| 207 | |
| 208 | ret = _aiq_compositor->integrate (first_results); |
| 209 | XCAM_FAIL_RETURN (WARNING, ret == XCAM_RETURN_NO_ERROR, ret, "AIQ configure_3a failed on integrate results"); |
| 210 | |
| 211 | if (!first_results.empty()) { |
| 212 | notify_calculation_done (first_results); |
| 213 | } |
| 214 | |
| 215 | return XCAM_RETURN_NO_ERROR; |
| 216 | } |
| 217 | |
| 218 | XCamReturn |
| 219 | X3aAnalyzerAiq::pre_3a_analyze (SmartPtr<X3aIspStatistics> &stats) |
| 220 | { |
| 221 | if (!_aiq_compositor->set_3a_stats (stats)) { |
| 222 | XCAM_LOG_WARNING ("Aiq compositor set 3a stats failed"); |
| 223 | return XCAM_RETURN_ERROR_UNKNOWN; |
| 224 | } |
| 225 | |
| 226 | return XCAM_RETURN_NO_ERROR; |
| 227 | } |
| 228 | |
| 229 | XCamReturn |
| 230 | X3aAnalyzerAiq::post_3a_analyze (X3aResultList &results) |
| 231 | { |
| 232 | XCamReturn ret = XCAM_RETURN_NO_ERROR; |
| 233 | |
| 234 | ret = _aiq_compositor->integrate (results); |
| 235 | XCAM_FAIL_RETURN (WARNING, ret == XCAM_RETURN_NO_ERROR, ret, "AIQ integrate 3A results failed"); |
| 236 | |
| 237 | return XCAM_RETURN_NO_ERROR; |
| 238 | } |
| 239 | |
| 240 | }; |