blob: 17c490586d281b6dfbca5c89b4dfbb277d093e7d [file] [log] [blame]
Wind Yuan75564b12015-01-15 06:51:15 -05001/*
2 * x3a_analyzer_simple.cpp - a simple 3a analyzer
3 *
4 * Copyright (c) 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_simple.h"
22#include "x3a_statistics_queue.h"
23#include <linux/atomisp.h>
24
25namespace XCam {
26
27#define SIMPLE_MIN_TARGET_EXPOSURE_TIME 5000 //5ms
28#define SIMPLE_MAX_TARGET_EXPOSURE_TIME 33000 //33ms
29#define SIMPLE_DEFAULT_BLACK_LEVEL 0.05
30
31class SimpleAeHandler
32 : public AeHandler
33{
34public:
35 SimpleAeHandler (X3aAnalyzerSimple *analyzer)
36 : _analyzer (analyzer)
37 {}
38 ~SimpleAeHandler () {}
39
40 virtual XCamReturn analyze (X3aResultList &output) {
41 return _analyzer->analyze_ae (output);
42 }
43private:
44 X3aAnalyzerSimple *_analyzer;
45};
46
47class SimpleAwbHandler
48 : public AwbHandler
49{
50public:
51 SimpleAwbHandler (X3aAnalyzerSimple *analyzer)
52 : _analyzer (analyzer)
53 {}
54 ~SimpleAwbHandler () {}
55
56 virtual XCamReturn analyze (X3aResultList &output) {
57 return _analyzer->analyze_awb (output);
58 }
59private:
60 X3aAnalyzerSimple *_analyzer;
61
62};
63
64class SimpleAfHandler
65 : public AfHandler
66{
67public:
68 SimpleAfHandler (X3aAnalyzerSimple *analyzer)
69 : _analyzer (analyzer)
70 {}
71 ~SimpleAfHandler () {}
72
73 virtual XCamReturn analyze (X3aResultList &output) {
74 return _analyzer->analyze_af (output);
75 }
76
77private:
78 X3aAnalyzerSimple *_analyzer;
79};
80
81class SimpleCommonHandler
82 : public CommonHandler
83{
84public:
85 SimpleCommonHandler (X3aAnalyzerSimple *analyzer)
86 : _analyzer (analyzer)
87 {}
88 ~SimpleCommonHandler () {}
89
90 virtual XCamReturn analyze (X3aResultList &output) {
91 XCAM_UNUSED (output);
92 return XCAM_RETURN_NO_ERROR;
93 }
94
95private:
96 X3aAnalyzerSimple *_analyzer;
97};
98
99X3aAnalyzerSimple::X3aAnalyzerSimple ()
100 : X3aAnalyzer ("X3aAnalyzerSimple")
101 , _last_target_exposure ((double)SIMPLE_MIN_TARGET_EXPOSURE_TIME)
102 , _is_ae_started (false)
103{
104}
105
106X3aAnalyzerSimple::~X3aAnalyzerSimple ()
107{
108}
109
110SmartPtr<AeHandler>
111X3aAnalyzerSimple::create_ae_handler ()
112{
113 SimpleAeHandler *handler = new SimpleAeHandler (this);
114 return handler;
115}
116
117SmartPtr<AwbHandler>
118X3aAnalyzerSimple::create_awb_handler ()
119{
120 SimpleAwbHandler *handler = new SimpleAwbHandler (this);
121 return handler;
122}
123
124SmartPtr<AfHandler>
125X3aAnalyzerSimple::create_af_handler ()
126{
127 SimpleAfHandler *handler = new SimpleAfHandler (this);
128 return handler;
129}
130
131SmartPtr<CommonHandler>
132X3aAnalyzerSimple::create_common_handler ()
133{
134 SimpleCommonHandler *handler = new SimpleCommonHandler (this);
135 return handler;
136}
137
138XCamReturn
139X3aAnalyzerSimple::configure_3a ()
140{
141 _is_ae_started = false;
142 return XCAM_RETURN_NO_ERROR;
143}
144
145XCamReturn
146X3aAnalyzerSimple::pre_3a_analyze (SmartPtr<X3aIspStatistics> &stats)
147{
148 _current_stats = stats;
149 return XCAM_RETURN_NO_ERROR;
150}
151
152XCamReturn
153X3aAnalyzerSimple::post_3a_analyze (X3aResultList &results)
154{
155 _current_stats.release ();
156
157 XCam3aResultBlackLevel black_level;
158 SmartPtr<X3aBlackLevelResult> bl_result = new X3aBlackLevelResult (XCAM_3A_RESULT_BLACK_LEVEL);
159
160 xcam_mem_clear (&black_level);
161 black_level.r_level = SIMPLE_DEFAULT_BLACK_LEVEL;
162 black_level.gr_level = SIMPLE_DEFAULT_BLACK_LEVEL;
163 black_level.gb_level = SIMPLE_DEFAULT_BLACK_LEVEL;
164 black_level.b_level = SIMPLE_DEFAULT_BLACK_LEVEL;
165 bl_result->set_standard_result (black_level);
166 results.push_back (bl_result);
167
168 return XCAM_RETURN_NO_ERROR;
169}
170
171XCamReturn
172X3aAnalyzerSimple::analyze_awb (X3aResultList &output)
173{
174 const struct atomisp_3a_statistics *stats = _current_stats->get_3a_stats ();
175 uint32_t cell_count = stats->grid_info.bqs_per_grid_cell * stats->grid_info.bqs_per_grid_cell;
176 uint32_t bits_depth = stats->grid_info.elem_bit_depth;
177 double sum_r = 0.0, sum_gr = 0.0, sum_gb = 0.0, sum_b = 0.0;
178 double avg_r = 0.0, avg_gr = 0.0, avg_gb = 0.0, avg_b = 0.0;
179 double target_avg = 0.0;
180 XCam3aResultWhiteBalance wb;
181
182 xcam_mem_clear (&wb);
183
184 // calculate avg r, gr, gb, b
185 for (uint32_t i = 0; i < stats->grid_info.height; ++i)
186 for (uint32_t j = 0; j < stats->grid_info.width; ++j) {
187 sum_r += ((double)(stats->data[i * stats->grid_info.width + j].awb_r)) / cell_count;
188 sum_gr += ((double)(stats->data[i * stats->grid_info.width + j].awb_gr)) / cell_count;
189 sum_gb += ((double)(stats->data[i * stats->grid_info.width + j].awb_gb)) / cell_count;
190 sum_b += ((double)(stats->data[i * stats->grid_info.width + j].awb_b)) / cell_count;
191 }
192
193 avg_r = sum_r / (stats->grid_info.width * stats->grid_info.height);
194 avg_gr = sum_gr / (stats->grid_info.width * stats->grid_info.height);
195 avg_gb = sum_gb / (stats->grid_info.width * stats->grid_info.height);
196 avg_b = sum_b / (stats->grid_info.width * stats->grid_info.height);
197 avg_r = avg_r / (1 << (bits_depth - 8));
198 avg_gr = avg_gr / (1 << (bits_depth - 8));
199 avg_gb = avg_gb / (1 << (bits_depth - 8));
200 avg_b = avg_b / (1 << (bits_depth - 8));
201 target_avg = (avg_gr + avg_gb) / 2;
202 wb.r_gain = target_avg / avg_r;
203 wb.b_gain = target_avg / avg_b;
204 wb.gr_gain = 1.0;
205 wb.gb_gain = 1.0;
206
207 SmartPtr<X3aWhiteBalanceResult> result = new X3aWhiteBalanceResult (XCAM_3A_RESULT_WHITE_BALANCE);
208 result->set_standard_result (wb);
209 output.push_back (result);
210
211 XCAM_LOG_DEBUG ("X3aAnalyzerSimple analyze awb, r:%f, gr:%f, gb:%f, b:%f",
212 wb.r_gain, wb.gr_gain, wb.gb_gain, wb.b_gain);
213
214 return XCAM_RETURN_NO_ERROR;
215}
216
217XCamReturn
218X3aAnalyzerSimple::analyze_ae (X3aResultList &output)
219{
220 static const uint32_t expect_y_mean = 150;
221
222 const struct atomisp_3a_statistics *stats = _current_stats->get_3a_stats ();
223 uint32_t cell_count = stats->grid_info.bqs_per_grid_cell * stats->grid_info.bqs_per_grid_cell;
224 uint32_t bits_depth = stats->grid_info.elem_bit_depth;
225 double sum_y = 0.0;
226 double target_exposure = 1.0;
227 SmartPtr<X3aExposureResult> result = new X3aExposureResult (XCAM_3A_RESULT_EXPOSURE);;
228 XCam3aResultExposure exposure;
229
230 xcam_mem_clear (&exposure);
231 exposure.digital_gain = 1.0;
232
233 if (!_is_ae_started) {
234 _last_target_exposure = SIMPLE_MIN_TARGET_EXPOSURE_TIME;
235 exposure.exposure_time = _last_target_exposure;
236 exposure.analog_gain = 1.0;
237
238 result->set_standard_result (exposure);
239 output.push_back (result);
240 _is_ae_started = true;
241 return XCAM_RETURN_NO_ERROR;
242 }
243
244 for (uint32_t i = 0; i < stats->grid_info.height; ++i)
245 for (uint32_t j = 0; j < stats->grid_info.width; ++j) {
246 sum_y += ((double)(stats->data[i * stats->grid_info.width + j].ae_y)) / cell_count;
247 }
248 sum_y /= (stats->grid_info.width * stats->grid_info.height);
249 sum_y /= (1 << (bits_depth - 8)); // make it in 8 bits
250 target_exposure = (expect_y_mean / sum_y) * _last_target_exposure;
251 target_exposure = XCAM_MAX (target_exposure, SIMPLE_MIN_TARGET_EXPOSURE_TIME);
252
253 if (target_exposure > SIMPLE_MAX_TARGET_EXPOSURE_TIME) {
254 exposure.exposure_time = SIMPLE_MAX_TARGET_EXPOSURE_TIME;
255 exposure.analog_gain = target_exposure / exposure.exposure_time;
256 } else {
257 exposure.exposure_time = target_exposure;
258 exposure.analog_gain = 1.0;
259 }
260 result->set_standard_result (exposure);
261 output.push_back (result);
262
263 return XCAM_RETURN_NO_ERROR;
264}
265
266XCamReturn X3aAnalyzerSimple::analyze_af (X3aResultList &output)
267{
268 XCAM_UNUSED (output);
269 return XCAM_RETURN_NO_ERROR;
270}
271
272};