lin
2025-08-14 dae8bad597b6607a449b32bf76c523423f7720ed
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 * cl_3a_image_processor.cpp - CL 3A image processor
 *
 *  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 "cl_3a_image_processor.h"
#include "cl_context.h"
#include "cl_csc_handler.h"
#include "cl_bayer_pipe_handler.h"
#include "cl_yuv_pipe_handler.h"
#if ENABLE_YEENR_HANDLER
#include "cl_ee_handler.h"
#endif
#include "cl_tnr_handler.h"
#include "cl_tonemapping_handler.h"
#include "cl_newtonemapping_handler.h"
#include "cl_bayer_basic_handler.h"
 
#define XCAM_CL_3A_IMAGE_MAX_POOL_SIZE 6
 
namespace XCam {
 
CL3aImageProcessor::CL3aImageProcessor ()
    : CLImageProcessor ("CL3aImageProcessor")
    , _output_fourcc (V4L2_PIX_FMT_NV12)
    , _3a_stats_bits (8)
    , _pipeline_profile (BasicPipelineProfile)
    , _capture_stage (TonemappingStage)
    , _wdr_mode (WDRdisabled)
    , _tnr_mode (0)
    , _enable_gamma (true)
    , _enable_macc (true)
    , _snr_mode (0)
{
    keep_attached_buf (true);
    XCAM_LOG_DEBUG ("CL3aImageProcessor constructed");
}
 
CL3aImageProcessor::~CL3aImageProcessor ()
{
    XCAM_LOG_DEBUG ("CL3aImageProcessor destructed");
}
 
void
CL3aImageProcessor::set_stats_callback (const SmartPtr<StatsCallback> &callback)
{
    XCAM_ASSERT (callback.ptr ());
    _stats_callback = callback;
}
 
bool
CL3aImageProcessor::set_output_format (uint32_t fourcc)
{
    XCAM_FAIL_RETURN (
        WARNING,
        V4L2_PIX_FMT_NV12 == fourcc,
        false,
        "cl 3a processor doesn't support output format: %s",
        xcam_fourcc_to_string (fourcc));
 
    _output_fourcc = fourcc;
    return true;
}
 
bool
CL3aImageProcessor::set_capture_stage (CaptureStage capture_stage)
{
    _capture_stage = capture_stage;
    return true;
}
 
bool
CL3aImageProcessor::set_3a_stats_bits (uint32_t bits)
{
    switch (bits) {
    case 8:
    case 12:
        _3a_stats_bits = bits;
        break;
    default:
        XCAM_LOG_WARNING ("cl image processor 3a stats doesn't support %d-bits", bits);
        return false;
    }
    return true;
}
 
bool
CL3aImageProcessor::can_process_result (SmartPtr<X3aResult> &result)
{
    if (result.ptr() == NULL)
        return false;
    switch (result->get_type ()) {
    case XCAM_3A_RESULT_WHITE_BALANCE:
    case XCAM_3A_RESULT_BLACK_LEVEL:
    case XCAM_3A_RESULT_R_GAMMA:
    case XCAM_3A_RESULT_G_GAMMA:
    case XCAM_3A_RESULT_B_GAMMA:
    case XCAM_3A_RESULT_RGB2YUV_MATRIX:
    case XCAM_3A_RESULT_DEFECT_PIXEL_CORRECTION:
    case XCAM_3A_RESULT_MACC:
    case XCAM_3A_RESULT_BAYER_NOISE_REDUCTION:
    case XCAM_3A_RESULT_BRIGHTNESS:
    case XCAM_3A_RESULT_3D_NOISE_REDUCTION:
    case XCAM_3A_RESULT_TEMPORAL_NOISE_REDUCTION_YUV:
    case XCAM_3A_RESULT_EDGE_ENHANCEMENT:
        return true;
 
    default:
        return false;
    }
 
    return false;
}
 
XCamReturn
CL3aImageProcessor::apply_3a_results (X3aResultList &results)
{
    XCamReturn ret = XCAM_RETURN_NO_ERROR;
 
    for (X3aResultList::iterator iter = results.begin (); iter != results.end (); ++iter)
    {
        SmartPtr<X3aResult> &result = *iter;
        ret = apply_3a_result (result);
        if (ret != XCAM_RETURN_NO_ERROR)
            break;
    }
    return ret;
}
 
XCamReturn
CL3aImageProcessor::apply_3a_result (SmartPtr<X3aResult> &result)
{
    STREAM_LOCK;
 
    if (result.ptr() == NULL)
        return XCAM_RETURN_BYPASS;
 
    uint32_t res_type = result->get_type ();
 
    switch (res_type) {
    case XCAM_3A_RESULT_WHITE_BALANCE: {
        SmartPtr<X3aWhiteBalanceResult> wb_res = result.dynamic_cast_ptr<X3aWhiteBalanceResult> ();
        XCAM_ASSERT (wb_res.ptr ());
        if (_bayer_basic_pipe.ptr ()) {
            _bayer_basic_pipe->set_wb_config (wb_res->get_standard_result ());
            _bayer_basic_pipe->set_3a_result (result);
        }
        break;
    }
 
    case XCAM_3A_RESULT_BLACK_LEVEL: {
        SmartPtr<X3aBlackLevelResult> bl_res = result.dynamic_cast_ptr<X3aBlackLevelResult> ();
        XCAM_ASSERT (bl_res.ptr ());
        if (_bayer_basic_pipe.ptr ()) {
            _bayer_basic_pipe->set_blc_config (bl_res->get_standard_result ());
            _bayer_basic_pipe->set_3a_result (result);
        }
        break;
    }
 
    case XCAM_3A_RESULT_DEFECT_PIXEL_CORRECTION: {
        SmartPtr<X3aDefectPixelResult> def_res = result.dynamic_cast_ptr<X3aDefectPixelResult> ();
        XCAM_ASSERT (def_res.ptr ());
        XCAM_UNUSED (def_res);
        break;
    }
 
    case XCAM_3A_RESULT_RGB2YUV_MATRIX: {
        SmartPtr<X3aColorMatrixResult> csc_res = result.dynamic_cast_ptr<X3aColorMatrixResult> ();
        XCAM_ASSERT (csc_res.ptr ());
        if (_csc.ptr()) {
            _csc->set_matrix (csc_res->get_standard_result ());
            _csc->set_3a_result (result);
        }
        if (_yuv_pipe.ptr()) {
            _yuv_pipe->set_rgbtoyuv_matrix (csc_res->get_standard_result ());
            _yuv_pipe->set_3a_result (result);
        }
        break;
    }
 
    case XCAM_3A_RESULT_MACC: {
        SmartPtr<X3aMaccMatrixResult> macc_res = result.dynamic_cast_ptr<X3aMaccMatrixResult> ();
        XCAM_ASSERT (macc_res.ptr ());
        if (_yuv_pipe.ptr()) {
            _yuv_pipe->set_macc_table (macc_res->get_standard_result ());
            _yuv_pipe->set_3a_result (result);
        }
        break;
    }
    case XCAM_3A_RESULT_R_GAMMA:
    case XCAM_3A_RESULT_B_GAMMA:
        break;
 
    case XCAM_3A_RESULT_G_GAMMA:
    case XCAM_3A_RESULT_Y_GAMMA: {
        SmartPtr<X3aGammaTableResult> gamma_res = result.dynamic_cast_ptr<X3aGammaTableResult> ();
        XCAM_ASSERT (gamma_res.ptr ());
        if (_bayer_basic_pipe.ptr ()) {
            _bayer_basic_pipe->set_gamma_table (gamma_res->get_standard_result ());
            _bayer_basic_pipe->set_3a_result (result);
        }
        break;
    }
 
    case XCAM_3A_RESULT_3D_NOISE_REDUCTION: {
        SmartPtr<X3aTemporalNoiseReduction> nr_res = result.dynamic_cast_ptr<X3aTemporalNoiseReduction> ();
        XCAM_ASSERT (nr_res.ptr ());
        XCAM_UNUSED (nr_res);
 
        break;
    }
 
    case XCAM_3A_RESULT_TEMPORAL_NOISE_REDUCTION_YUV: {
        SmartPtr<X3aTemporalNoiseReduction> tnr_res = result.dynamic_cast_ptr<X3aTemporalNoiseReduction> ();
        XCAM_ASSERT (tnr_res.ptr ());
        if (_yuv_pipe.ptr ()) {
            _yuv_pipe->set_tnr_yuv_config(tnr_res->get_standard_result ());
            _yuv_pipe->set_3a_result (result);
        }
        break;
    }
 
    case XCAM_3A_RESULT_EDGE_ENHANCEMENT: {
        SmartPtr<X3aEdgeEnhancementResult> ee_ee_res = result.dynamic_cast_ptr<X3aEdgeEnhancementResult> ();
        XCAM_ASSERT (ee_ee_res.ptr ());
        if (_bayer_pipe.ptr()) {
            _bayer_pipe->set_ee_config (ee_ee_res->get_standard_result ());
            _bayer_pipe->set_3a_result (result);
        }
#if ENABLE_YEENR_HANDLER
        if (_ee.ptr()) {
            _ee->set_ee_config_ee (ee_ee_res->get_standard_result ());
            _ee->set_3a_result (result);
        }
#endif
        break;
    }
 
    case XCAM_3A_RESULT_BAYER_NOISE_REDUCTION: {
        SmartPtr<X3aBayerNoiseReduction> bnr_res = result.dynamic_cast_ptr<X3aBayerNoiseReduction> ();
        XCAM_ASSERT (bnr_res.ptr ());
        if (_bayer_pipe.ptr()) {
            _bayer_pipe->set_bnr_config (bnr_res->get_standard_result ());
            _bayer_pipe->set_3a_result (result);
        }
 
        break;
    }
 
    case XCAM_3A_RESULT_BRIGHTNESS: {
        SmartPtr<X3aBrightnessResult> brightness_res = result.dynamic_cast_ptr<X3aBrightnessResult> ();
        XCAM_ASSERT (brightness_res.ptr ());
        float brightness_level = ((XCam3aResultBrightness)brightness_res->get_standard_result()).brightness_level;
        XCAM_UNUSED (brightness_level);
        break;
    }
 
    default:
        XCAM_LOG_WARNING ("CL3aImageProcessor unknown 3a result:%d", res_type);
        break;
    }
 
    return XCAM_RETURN_NO_ERROR;
}
 
XCamReturn
CL3aImageProcessor::create_handlers ()
{
    SmartPtr<CLImageHandler> image_handler;
    SmartPtr<CLContext> context = get_cl_context ();
 
    XCAM_ASSERT (context.ptr ());
 
    /* bayer pipeline */
    image_handler = create_cl_bayer_basic_image_handler (context, _enable_gamma, _3a_stats_bits);
    _bayer_basic_pipe = image_handler.dynamic_cast_ptr<CLBayerBasicImageHandler> ();
    XCAM_FAIL_RETURN (
        WARNING,
        _bayer_basic_pipe.ptr (),
        XCAM_RETURN_ERROR_CL,
        "CL3aImageProcessor create bayer basic pipe handler failed");
    image_handler->set_pool_size (XCAM_CL_3A_IMAGE_MAX_POOL_SIZE);
    _bayer_basic_pipe->set_stats_callback (_stats_callback);
    add_handler (image_handler);
 
    /* tone mapping */
    switch(_wdr_mode) {
    case Gaussian: {
        image_handler = create_cl_tonemapping_image_handler (context);
        _tonemapping = image_handler.dynamic_cast_ptr<CLTonemappingImageHandler> ();
        XCAM_FAIL_RETURN (
            WARNING,
            _tonemapping.ptr (),
            XCAM_RETURN_ERROR_CL,
            "CL3aImageProcessor create tonemapping handler failed");
        _tonemapping->enable_handler (true);
        image_handler->set_pool_size (XCAM_CL_3A_IMAGE_MAX_POOL_SIZE);
        add_handler (image_handler);
        break;
    }
    case Haleq: {
        image_handler = create_cl_newtonemapping_image_handler (context);
        _newtonemapping = image_handler.dynamic_cast_ptr<CLNewTonemappingImageHandler> ();
        XCAM_FAIL_RETURN (
            WARNING,
            _newtonemapping.ptr (),
            XCAM_RETURN_ERROR_CL,
            "CL3aImageProcessor create tonemapping handler failed");
        _newtonemapping->enable_handler (true);
        image_handler->set_pool_size (XCAM_CL_3A_IMAGE_MAX_POOL_SIZE);
        add_handler (image_handler);
        break;
    }
    default:
        XCAM_LOG_DEBUG ("WDR disabled");
        break;
    }
 
    /* bayer pipe */
    image_handler = create_cl_bayer_pipe_image_handler (context);
    _bayer_pipe = image_handler.dynamic_cast_ptr<CLBayerPipeImageHandler> ();
    XCAM_FAIL_RETURN (
        WARNING,
        image_handler.ptr (),
        XCAM_RETURN_ERROR_CL,
        "CL3aImageProcessor create bayer pipe handler failed");
 
    _bayer_pipe->enable_denoise (XCAM_DENOISE_TYPE_BNR & _snr_mode);
    image_handler->set_pool_size (XCAM_CL_3A_IMAGE_MAX_POOL_SIZE * 2);
    add_handler (image_handler);
    if(_capture_stage == BasicbayerStage)
        return XCAM_RETURN_NO_ERROR;
 
    image_handler = create_cl_yuv_pipe_image_handler (context);
    _yuv_pipe = image_handler.dynamic_cast_ptr<CLYuvPipeImageHandler> ();
    XCAM_FAIL_RETURN (
        WARNING,
        _yuv_pipe.ptr (),
        XCAM_RETURN_ERROR_CL,
        "CL3aImageProcessor create yuv pipe handler failed");
    _yuv_pipe->set_tnr_enable (_tnr_mode & CL_TNR_TYPE_YUV);
    image_handler->set_pool_size (XCAM_CL_3A_IMAGE_MAX_POOL_SIZE * 2);
    add_handler (image_handler);
 
#if ENABLE_YEENR_HANDLER
    /* ee */
    image_handler = create_cl_ee_image_handler (context);
    _ee = image_handler.dynamic_cast_ptr<CLEeImageHandler> ();
    XCAM_FAIL_RETURN (
        WARNING,
        _ee.ptr (),
        XCAM_RETURN_ERROR_CL,
        "CL3aImageProcessor create ee handler failed");
    _ee->enable_handler (XCAM_DENOISE_TYPE_EE & _snr_mode);
    image_handler->set_pool_type (CLImageHandler::CLVideoPoolType);
    image_handler->set_pool_size (XCAM_CL_3A_IMAGE_MAX_POOL_SIZE);
    add_handler (image_handler);
#endif
 
    XCAM_FAIL_RETURN (
        WARNING,
        post_config (),
        XCAM_RETURN_ERROR_CL,
        "CL3aImageProcessor post_config failed");
 
    return XCAM_RETURN_NO_ERROR;
}
 
bool
CL3aImageProcessor::post_config ()
{
    CLImageProcessor::ImageHandlerList::iterator i_handler = handlers_begin ();
    CLImageProcessor::ImageHandlerList::iterator end = handlers_end ();
    uint32_t swap_y_count = 0;
    bool start_count = false;
    bool ret = true;
 
    if (!_yuv_pipe.ptr ())  //not necessary to check
        return true;
 
    for (; i_handler != end; ++i_handler) {
        if (!start_count) {
            SmartPtr<CLYuvPipeImageHandler> convert_yuv = (*i_handler).dynamic_cast_ptr<CLYuvPipeImageHandler> ();
            if (convert_yuv.ptr () && convert_yuv.ptr () == _yuv_pipe.ptr ())
                start_count = true;
            continue;
        }
 
        SmartPtr<CLCloneImageHandler> clone_y = (*i_handler).dynamic_cast_ptr<CLCloneImageHandler> ();
        if (clone_y.ptr () && clone_y->is_handler_enabled () && (clone_y->get_clone_flags () & SwappedBuffer::SwapY))
            swap_y_count++;
    }
 
    if (swap_y_count % 2 == 1)
        ret = _yuv_pipe->enable_buf_pool_swap_flags (SwappedBuffer::SwapY | SwappedBuffer::SwapUV, SwappedBuffer::OrderY1Y0 | SwappedBuffer::OrderUV0UV1);
    else
        ret = _yuv_pipe->enable_buf_pool_swap_flags (SwappedBuffer::SwapY | SwappedBuffer::SwapUV, SwappedBuffer::OrderY0Y1 | SwappedBuffer::OrderUV0UV1);
 
    return ret;
}
 
bool
CL3aImageProcessor::set_profile (const CL3aImageProcessor::PipelineProfile value)
{
    _pipeline_profile = value;
 
    if (value >= AdvancedPipelineProfile)
        _tnr_mode |= CL_TNR_TYPE_YUV;
 
    if (value >= ExtremePipelineProfile) {
        _snr_mode |= XCAM_DENOISE_TYPE_BNR;
    }
    STREAM_LOCK;
    if (_yuv_pipe.ptr ())
        _yuv_pipe->set_tnr_enable (_tnr_mode & CL_TNR_TYPE_YUV);
 
    return true;
}
 
bool
CL3aImageProcessor::set_gamma (bool enable)
{
    _enable_gamma = enable;
 
    STREAM_LOCK;
 
    return true;
}
 
bool
CL3aImageProcessor::set_denoise (uint32_t mode)
{
    _snr_mode = mode;
 
    STREAM_LOCK;
    if (_bayer_pipe.ptr ())
        _bayer_pipe->enable_denoise (XCAM_DENOISE_TYPE_BNR & _snr_mode);
 
    return true;
}
 
bool
CL3aImageProcessor::set_macc (bool enable)
{
    _enable_macc = enable;
 
    STREAM_LOCK;
    return true;
}
 
bool
CL3aImageProcessor::set_tonemapping (CLTonemappingMode wdr_mode)
{
    _wdr_mode = wdr_mode;
 
    STREAM_LOCK;
 
    return true;
}
 
bool
CL3aImageProcessor::set_tnr (uint32_t mode, uint8_t level)
{
    XCAM_UNUSED (level);
    _tnr_mode = mode;
 
    STREAM_LOCK;
    if (_yuv_pipe.ptr ())
        _yuv_pipe->set_tnr_enable (_tnr_mode & CL_TNR_TYPE_YUV);
 
    return true;
}
 
};