1 /*
2  * aiq_handler.h - AIQ handler
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 #ifndef XCAM_AIQ_HANDLER_H
22 #define XCAM_AIQ_HANDLER_H
23 
24 #include <xcam_std.h>
25 #include "handler_interface.h"
26 #include "x3a_statistics_queue.h"
27 #include "ia_types.h"
28 #include "ia_aiq_types.h"
29 #include "ia_cmc_parser.h"
30 #include "ia_mkn_encoder.h"
31 #include "ia_aiq.h"
32 #include "ia_coordinate.h"
33 
34 typedef struct ia_isp_t ia_isp;
35 
36 namespace XCam {
37 
38 class AiqCompositor;
39 struct IspInputParameters;
40 
41 class IaIspAdaptor {
42 public:
IaIspAdaptor()43     explicit IaIspAdaptor()
44         : _handle (NULL)
45     {}
~IaIspAdaptor()46     virtual ~IaIspAdaptor() {}
47 
48     virtual bool init (
49         const ia_binary_data *cpf,
50         unsigned int max_width,
51         unsigned int max_height,
52         ia_cmc_t *cmc,
53         ia_mkn *mkn) = 0;
54     virtual bool convert_statistics (
55         void *statistics,
56         ia_aiq_rgbs_grid **out_rgbs_grid,
57         ia_aiq_af_grid **out_af_grid) = 0;
58     virtual bool run (
59         const IspInputParameters *isp_input_params,
60         ia_binary_data *output_data) = 0;
61 
62 private:
63     XCAM_DEAD_COPY (IaIspAdaptor);
64 
65 protected:
66     ia_isp *_handle;
67 };
68 
69 class AiqAeHandler
70     : public AeHandler
71 {
72     friend class AiqCompositor;
73 private:
74     struct AiqAeResult {
75         ia_aiq_ae_results                 ae_result;
76         ia_aiq_ae_exposure_result         ae_exp_ret;
77         ia_aiq_exposure_parameters        aiq_exp_param;
78         ia_aiq_exposure_sensor_parameters sensor_exp_param;
79         ia_aiq_hist_weight_grid           weight_grid;
80         ia_aiq_flash_parameters           flash_param;
81 
82         AiqAeResult();
83         void copy (ia_aiq_ae_results *result);
84 
85         XCAM_DEAD_COPY (AiqAeResult);
86     };
87 
88 public:
89     explicit AiqAeHandler (SmartPtr<AiqCompositor> &aiq_compositor);
~AiqAeHandler()90     ~AiqAeHandler () {}
91 
is_started()92     bool is_started () const {
93         return _started;
94     }
95 
96     bool set_description (struct atomisp_sensor_mode_data *sensor_mode_data);
97 
get_result()98     ia_aiq_ae_results *get_result () {
99         return &_result.ae_result;
100     }
101 
102     //virtual functions from AnalyzerHandler
103     virtual XCamReturn analyze (X3aResultList &output);
104 
105     // virtual functions from AeHandler
106     virtual XCamFlickerMode get_flicker_mode ();
107     virtual int64_t get_current_exposure_time ();
108     virtual double get_current_analog_gain ();
109     virtual double get_max_analog_gain ();
110 
111     XCamReturn set_RGBS_weight_grid (ia_aiq_rgbs_grid **out_rgbs_grid);
112     XCamReturn set_hist_weight_grid (ia_aiq_hist_weight_grid **out_weight_grid);
113     XCamReturn dump_hist_weight_grid (const ia_aiq_hist_weight_grid *weight_grid);
114     XCamReturn dump_RGBS_grid (const ia_aiq_rgbs_grid *rgbs_grid);
115 
116 private:
117     bool ensure_ia_parameters ();
118     bool ensure_ae_mode ();
119     bool ensure_ae_metering_mode ();
120     bool ensure_ae_priority_mode ();
121     bool ensure_ae_flicker_mode ();
122     bool ensure_ae_manual ();
123     bool ensure_ae_ev_shift ();
124 
125     void adjust_ae_speed (
126         ia_aiq_exposure_sensor_parameters &cur_res,
127         ia_aiq_exposure_parameters &cur_aiq_exp,
128         const ia_aiq_exposure_sensor_parameters &last_res, double ae_speed);
129     void adjust_ae_limitation (ia_aiq_exposure_sensor_parameters &cur_res,
130                                ia_aiq_exposure_parameters &cur_aiq_exp);
131     bool manual_control_result (
132         ia_aiq_exposure_sensor_parameters &cur_res,
133         ia_aiq_exposure_parameters &cur_aiq_exp,
134         const ia_aiq_exposure_sensor_parameters &last_res);
135 
136     SmartPtr<X3aResult> pop_result ();
137 
138     static void convert_xcam_window_to_ia (const XCam3AWindow &window, ia_rectangle &ia_window);
139 
140 private:
141     XCAM_DEAD_COPY (AiqAeHandler);
142 
143 protected:
144     SmartPtr<AiqCompositor>           _aiq_compositor;
145     /* AIQ */
146     ia_rectangle                      _ia_ae_window;
147     ia_aiq_exposure_sensor_descriptor _sensor_descriptor;
148     ia_aiq_ae_manual_limits           _manual_limits;
149 
150     ia_aiq_ae_input_params            _input;
151 
152     /* result */
153     AiqAeResult                       _result;
154     uint32_t                          _calculate_period;
155     bool                              _started;
156 };
157 
158 class AiqAwbHandler
159     : public AwbHandler
160 {
161     friend class AiqCompositor;
162 public:
163     explicit AiqAwbHandler (SmartPtr<AiqCompositor> &aiq_compositor);
~AiqAwbHandler()164     ~AiqAwbHandler () {}
165 
166     virtual XCamReturn analyze (X3aResultList &output);
167 
168     // virtual functions from AwbHandler
169     virtual uint32_t get_current_estimate_cct ();
170 
get_result()171     ia_aiq_awb_results *get_result () {
172         return &_result;
173     }
is_started()174     bool is_started () const {
175         return _started;
176     }
177 
178 private:
179     bool ensure_ia_parameters ();
180     bool ensure_awb_mode ();
181     void adjust_speed (const ia_aiq_awb_results &last_ret);
182 
183     XCAM_DEAD_COPY (AiqAwbHandler);
184 
185 protected:
186     SmartPtr<AiqCompositor>     _aiq_compositor;
187     /*aiq*/
188     ia_aiq_awb_input_params     _input;
189     ia_aiq_awb_manual_cct_range _cct_range;
190 
191     ia_aiq_awb_results          _result;
192     ia_aiq_awb_results          _history_result;
193     bool                        _started;
194 };
195 
196 class AiqAfHandler
197     : public AfHandler
198 {
199 public:
AiqAfHandler(SmartPtr<AiqCompositor> & aiq_compositor)200     explicit AiqAfHandler (SmartPtr<AiqCompositor> &aiq_compositor)
201         : _aiq_compositor (aiq_compositor)
202     {}
~AiqAfHandler()203     ~AiqAfHandler () {}
204 
205     virtual XCamReturn analyze (X3aResultList &output);
206 
207 private:
208     XCAM_DEAD_COPY (AiqAfHandler);
209 
210 protected:
211     SmartPtr<AiqCompositor>        _aiq_compositor;
212 };
213 
214 class AiqCommonHandler
215     : public CommonHandler
216 {
217     friend class AiqCompositor;
218 public:
219     explicit AiqCommonHandler (SmartPtr<AiqCompositor> &aiq_compositor);
~AiqCommonHandler()220     ~AiqCommonHandler () {}
221 
222     virtual XCamReturn analyze (X3aResultList &output);
get_gbce_result()223     ia_aiq_gbce_results *get_gbce_result () {
224         return _gbce_result;
225     }
get_color_effect()226     XCamColorEffect get_color_effect() {
227         return _params.color_effect;
228     }
229 
230 private:
231     XCAM_DEAD_COPY (AiqCommonHandler);
232 
233 protected:
234     SmartPtr<AiqCompositor>     _aiq_compositor;
235     ia_aiq_gbce_results        *_gbce_result;
236 };
237 
238 class AiqCompositor {
239 public:
240     explicit AiqCompositor ();
241     ~AiqCompositor ();
242 
243     void set_ae_handler (SmartPtr<AiqAeHandler> &handler);
244     void set_awb_handler (SmartPtr<AiqAwbHandler> &handler);
245     void set_af_handler (SmartPtr<AiqAfHandler> &handler);
246     void set_common_handler (SmartPtr<AiqCommonHandler> &handler);
247 
set_frame_use(ia_aiq_frame_use value)248     void set_frame_use (ia_aiq_frame_use value) {
249         _frame_use = value;
250     }
set_size(uint32_t width,uint32_t height)251     void set_size (uint32_t width, uint32_t height) {
252         _width = width;
253         _height = height;
254     }
get_size(uint32_t & out_width,uint32_t & out_height)255     void get_size (uint32_t &out_width, uint32_t &out_height) const {
256         out_width = _width;
257         out_height = _height;
258     }
set_framerate(double framerate)259     void set_framerate (double framerate) {
260         _framerate = framerate;
261     }
get_framerate()262     double get_framerate () {
263         return _framerate;
264     }
265     bool open (ia_binary_data &cpf);
266     void close ();
267 
268     bool set_sensor_mode_data (struct atomisp_sensor_mode_data *sensor_mode);
269     bool set_3a_stats (SmartPtr<X3aIspStatistics> &stats);
270 
get_handle()271     ia_aiq  * get_handle () {
272         return _ia_handle;
273     }
get_frame_use()274     ia_aiq_frame_use get_frame_use () const {
275         return _frame_use;
276     }
277 
278     XCamReturn integrate (  X3aResultList &results);
279 
280     SmartPtr<X3aResult> generate_3a_configs (struct atomisp_parameters *parameters);
281     void convert_window_to_ia (const XCam3AWindow &window, ia_rectangle &ia_window);
282     XCamReturn convert_color_effect (IspInputParameters &isp_input);
283 
get_ae_ev_shift_unlock()284     double get_ae_ev_shift_unlock () {
285         return _ae_handler->get_ev_shift_unlock();
286     }
287 
288 private:
289     XCamReturn apply_gamma_table (struct atomisp_parameters *isp_param);
290     XCamReturn apply_night_mode (struct atomisp_parameters *isp_param);
291     XCamReturn limit_nr_levels (struct atomisp_parameters *isp_param);
292     double calculate_value_by_factor (double factor, double min, double mid, double max);
293 
294     XCAM_DEAD_COPY (AiqCompositor);
295 
296 private:
297     SmartPtr<IaIspAdaptor>     _adaptor;
298     SmartPtr<AiqAeHandler>     _ae_handler;
299     SmartPtr<AiqAwbHandler>    _awb_handler;
300     SmartPtr<AiqAfHandler>     _af_handler;
301     SmartPtr<AiqCommonHandler> _common_handler;
302     ia_aiq                    *_ia_handle;
303     ia_mkn                    *_ia_mkn;
304     ia_aiq_pa_results         *_pa_result;
305 #ifdef HAVE_AIQ_2_7
306     ia_aiq_sa_results         *_sa_result;
307 #endif
308     ia_aiq_frame_use           _frame_use;
309     ia_aiq_frame_params        _frame_params;
310 
311     /*grids*/
312     ;
313 
314     uint32_t                   _width;
315     uint32_t                   _height;
316     double                     _framerate;
317 };
318 
319 };
320 
321 #endif //XCAM_AIQ_HANDLER_H
322