huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * x3a_analyzer_simple.cpp - a simple 3a analyzer
 *
 *  Copyright (c) 2015 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Wind Yuan <feng.yuan@intel.com>
 */
 
#include "x3a_analyzer_simple.h"
 
namespace XCam {
 
#define SIMPLE_MIN_TARGET_EXPOSURE_TIME  5000 //5ms
#define SIMPLE_MAX_TARGET_EXPOSURE_TIME  33000 //33ms
#define SIMPLE_DEFAULT_BLACK_LEVEL       0.05
 
class SimpleAeHandler
    : public AeHandler
{
public:
    SimpleAeHandler (X3aAnalyzerSimple *analyzer)
        : _analyzer (analyzer)
    {}
    ~SimpleAeHandler () {}
 
    virtual XCamReturn analyze (X3aResultList &output) {
        return _analyzer->analyze_ae (output);
    }
private:
    X3aAnalyzerSimple *_analyzer;
};
 
class SimpleAwbHandler
    : public AwbHandler
{
public:
    SimpleAwbHandler (X3aAnalyzerSimple *analyzer)
        : _analyzer (analyzer)
    {}
    ~SimpleAwbHandler () {}
 
    virtual XCamReturn analyze (X3aResultList &output) {
        return _analyzer->analyze_awb (output);
    }
private:
    X3aAnalyzerSimple *_analyzer;
 
};
 
class SimpleAfHandler
    : public AfHandler
{
public:
    SimpleAfHandler (X3aAnalyzerSimple *analyzer)
        : _analyzer (analyzer)
    {}
    ~SimpleAfHandler () {}
 
    virtual XCamReturn analyze (X3aResultList &output) {
        return _analyzer->analyze_af (output);
    }
 
private:
    X3aAnalyzerSimple *_analyzer;
};
 
class SimpleCommonHandler
    : public CommonHandler
{
public:
    SimpleCommonHandler (X3aAnalyzerSimple *analyzer)
        : _analyzer (analyzer)
    {}
    ~SimpleCommonHandler () {}
 
    virtual XCamReturn analyze (X3aResultList &output) {
        XCAM_UNUSED (output);
        return XCAM_RETURN_NO_ERROR;
    }
 
private:
    X3aAnalyzerSimple *_analyzer;
};
 
X3aAnalyzerSimple::X3aAnalyzerSimple ()
    : X3aAnalyzer ("X3aAnalyzerSimple")
    , _last_target_exposure ((double)SIMPLE_MIN_TARGET_EXPOSURE_TIME)
    , _is_ae_started (false)
    , _ae_calculation_interval (0)
{
}
 
X3aAnalyzerSimple::~X3aAnalyzerSimple ()
{
}
 
SmartPtr<AeHandler>
X3aAnalyzerSimple::create_ae_handler ()
{
    SimpleAeHandler *handler = new SimpleAeHandler (this);
    return handler;
}
 
SmartPtr<AwbHandler>
X3aAnalyzerSimple::create_awb_handler ()
{
    SimpleAwbHandler *handler = new SimpleAwbHandler (this);
    return handler;
}
 
SmartPtr<AfHandler>
X3aAnalyzerSimple::create_af_handler ()
{
    SimpleAfHandler *handler = new SimpleAfHandler (this);
    return handler;
}
 
SmartPtr<CommonHandler>
X3aAnalyzerSimple::create_common_handler ()
{
    SimpleCommonHandler *handler = new SimpleCommonHandler (this);
    return handler;
}
 
XCamReturn
X3aAnalyzerSimple::configure_3a ()
{
    _is_ae_started = false;
    _ae_calculation_interval = 0;
    return XCAM_RETURN_NO_ERROR;
}
 
XCamReturn
X3aAnalyzerSimple::pre_3a_analyze (SmartPtr<X3aStats> &stats)
{
    _current_stats = stats;
    return XCAM_RETURN_NO_ERROR;
}
 
XCamReturn
X3aAnalyzerSimple::post_3a_analyze (X3aResultList &results)
{
    _current_stats.release ();
 
    XCam3aResultBlackLevel black_level;
    SmartPtr<X3aBlackLevelResult> bl_result = new X3aBlackLevelResult (XCAM_3A_RESULT_BLACK_LEVEL);
 
    xcam_mem_clear (black_level);
    black_level.r_level = SIMPLE_DEFAULT_BLACK_LEVEL;
    black_level.gr_level = SIMPLE_DEFAULT_BLACK_LEVEL;
    black_level.gb_level = SIMPLE_DEFAULT_BLACK_LEVEL;
    black_level.b_level = SIMPLE_DEFAULT_BLACK_LEVEL;
    bl_result->set_standard_result (black_level);
    results.push_back (bl_result);
 
    return XCAM_RETURN_NO_ERROR;
}
 
XCamReturn
X3aAnalyzerSimple::analyze_awb (X3aResultList &output)
{
    const XCam3AStats *stats = _current_stats->get_stats ();
    double sum_r = 0.0, sum_gr = 0.0, sum_gb = 0.0, sum_b = 0.0;
    double avg_r = 0.0, avg_gr = 0.0, avg_gb = 0.0, avg_b = 0.0;
    double target_avg = 0.0;
    XCam3aResultWhiteBalance wb;
 
    xcam_mem_clear (wb);
    XCAM_ASSERT (stats);
 
    // calculate avg r, gr, gb, b
    for (uint32_t i = 0; i < stats->info.height; ++i)
        for (uint32_t j = 0; j < stats->info.width; ++j) {
            sum_r += (double)(stats->stats[i * stats->info.aligned_width + j].avg_r);
            sum_gr += (double)(stats->stats[i * stats->info.aligned_width + j].avg_gr);
            sum_gb += (double)(stats->stats[i * stats->info.aligned_width + j].avg_gb);
            sum_b += (double)(stats->stats[i * stats->info.aligned_width + j].avg_b);
        }
 
    avg_r = sum_r / (stats->info.width * stats->info.height);
    avg_gr = sum_gr / (stats->info.width * stats->info.height);
    avg_gb = sum_gb / (stats->info.width * stats->info.height);
    avg_b = sum_b / (stats->info.width * stats->info.height);
 
    target_avg =  (avg_gr + avg_gb) / 2;
    wb.r_gain = target_avg / avg_r;
    wb.b_gain = target_avg / avg_b;
    wb.gr_gain = 1.0;
    wb.gb_gain = 1.0;
 
    SmartPtr<X3aWhiteBalanceResult> result = new X3aWhiteBalanceResult (XCAM_3A_RESULT_WHITE_BALANCE);
    result->set_standard_result (wb);
    output.push_back (result);
 
    XCAM_LOG_DEBUG ("X3aAnalyzerSimple analyze awb, r:%f, gr:%f, gb:%f, b:%f",
                    wb.r_gain, wb.gr_gain, wb.gb_gain, wb.b_gain);
 
    return XCAM_RETURN_NO_ERROR;
}
 
XCamReturn
X3aAnalyzerSimple::analyze_ae (X3aResultList &output)
{
    static const uint32_t expect_y_mean = 110;
 
    const XCam3AStats *stats = _current_stats->get_stats ();
    XCAM_FAIL_RETURN(
        WARNING,
        stats,
        XCAM_RETURN_ERROR_UNKNOWN,
        "failed to get XCam3AStats");
 
    double sum_y = 0.0;
    double target_exposure = 1.0;
    SmartPtr<X3aExposureResult> result = new X3aExposureResult (XCAM_3A_RESULT_EXPOSURE);
    XCam3aResultExposure exposure;
 
    xcam_mem_clear (exposure);
    exposure.digital_gain = 1.0;
 
    if (!_is_ae_started) {
        _last_target_exposure = SIMPLE_MIN_TARGET_EXPOSURE_TIME;
        exposure.exposure_time = _last_target_exposure;
        exposure.analog_gain = 1.0;
 
        result->set_standard_result (exposure);
        output.push_back (result);
        _is_ae_started = true;
        return XCAM_RETURN_NO_ERROR;
    }
 
    if (_ae_calculation_interval % 10 == 0) {
        for (uint32_t i = 0; i < stats->info.height; ++i)
            for (uint32_t j = 0; j < stats->info.width; ++j) {
                sum_y += (double)(stats->stats[i * stats->info.aligned_width + j].avg_y);
            }
        sum_y /= (stats->info.width * stats->info.height);
        target_exposure = (expect_y_mean / sum_y) * _last_target_exposure;
        target_exposure = XCAM_MAX (target_exposure, SIMPLE_MIN_TARGET_EXPOSURE_TIME);
 
        if (target_exposure > SIMPLE_MAX_TARGET_EXPOSURE_TIME * 255)
            target_exposure = SIMPLE_MAX_TARGET_EXPOSURE_TIME * 255;
 
        if (target_exposure > SIMPLE_MAX_TARGET_EXPOSURE_TIME) {
            exposure.exposure_time = SIMPLE_MAX_TARGET_EXPOSURE_TIME;
            exposure.analog_gain = target_exposure / exposure.exposure_time;
        } else {
            exposure.exposure_time = target_exposure;
            exposure.analog_gain = 1.0;
        }
 
        result->set_standard_result (exposure);
        output.push_back (result);
        _last_target_exposure = target_exposure;
    }
 
    _ae_calculation_interval++;
 
    return XCAM_RETURN_NO_ERROR;
}
 
XCamReturn X3aAnalyzerSimple::analyze_af (X3aResultList &output)
{
    XCAM_UNUSED (output);
    return XCAM_RETURN_NO_ERROR;
}
 
};