Home | History | Annotate | Download | only in isp
      1 /*
      2  * isp_config_translator.cpp - isp config translator
      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 (at) intel.com>
     19  */
     20 
     21 #include "isp_config_translator.h"
     22 #include <math.h>
     23 
     24 namespace XCam {
     25 
     26 static uint32_t
     27 _get_max_bits (double value)
     28 {
     29     uint32_t max_int = 0;
     30     uint32_t interger_bits = 0;
     31 
     32     max_int = (uint32_t)value;
     33     while (max_int) {
     34         ++interger_bits;
     35         max_int = (max_int >> 1);
     36     }
     37     return interger_bits;
     38 }
     39 
     40 IspConfigTranslator::IspConfigTranslator (SmartPtr<SensorDescriptor> &sensor)
     41     : _sensor (sensor)
     42 {
     43     XCAM_ASSERT (_sensor.ptr());
     44 }
     45 
     46 IspConfigTranslator::~IspConfigTranslator ()
     47 {
     48 }
     49 
     50 XCamReturn
     51 IspConfigTranslator::translate_white_balance (
     52     const XCam3aResultWhiteBalance &from,
     53     struct atomisp_wb_config &to)
     54 {
     55     uint32_t interger_bits = 0;
     56     double multiplier = 0.0;
     57     double max_gain = XCAM_MAX (from.b_gain, from.r_gain);
     58     max_gain = XCAM_MAX (max_gain, from.gr_gain);
     59     max_gain = XCAM_MAX (max_gain, from.gb_gain);
     60 
     61     interger_bits = _get_max_bits (max_gain);
     62     multiplier = (double)(1 << (16 - interger_bits));
     63     to.integer_bits = interger_bits;
     64     to.gr = (uint32_t)(from.gr_gain * multiplier + 0.5);
     65     to.r = (uint32_t)(from.r_gain * multiplier + 0.5);
     66     to.b = (uint32_t)(from.b_gain * multiplier + 0.5);
     67     to.gb = (uint32_t)(from.gb_gain * multiplier + 0.5);
     68 
     69     return XCAM_RETURN_NO_ERROR;
     70 }
     71 
     72 XCamReturn
     73 IspConfigTranslator::translate_black_level (
     74     const XCam3aResultBlackLevel &from, struct atomisp_ob_config &to)
     75 {
     76     double multiplier = (double)(1 << 16);
     77 
     78     to.mode = atomisp_ob_mode_fixed;
     79     to.level_gr = (uint32_t)(from.gr_level * multiplier + 0.5);
     80     to.level_r = (uint32_t)(from.r_level * multiplier + 0.5);
     81     to.level_b = (uint32_t)(from.b_level * multiplier + 0.5);
     82     to.level_gb = (uint32_t)(from.gb_level * multiplier + 0.5);
     83 
     84     return XCAM_RETURN_NO_ERROR;
     85 }
     86 
     87 XCamReturn
     88 IspConfigTranslator::translate_color_matrix (
     89     const XCam3aResultColorMatrix &from, struct atomisp_cc_config &to)
     90 {
     91     double max_value = 0.0;
     92     uint32_t interger_bits = 0;
     93     double multiplier = 0.0;
     94     bool have_minus = false;
     95     uint32_t i = 0;
     96 
     97     for (i = 0; i < XCAM_COLOR_MATRIX_SIZE; ++i) {
     98         if (fabs(from.matrix [i]) > max_value)
     99             max_value = fabs(from.matrix [i]);
    100         if (from.matrix [i] < 0)
    101             have_minus = true;
    102     }
    103     interger_bits = _get_max_bits (max_value);
    104     if (have_minus)
    105         ++interger_bits;
    106 
    107     XCAM_ASSERT (interger_bits < 13);
    108     to.fraction_bits = 13 - interger_bits;
    109     multiplier = (double)(1 << (13 - interger_bits));
    110     for (i = 0; i < XCAM_COLOR_MATRIX_SIZE; ++i) {
    111         to.matrix[i] = (int32_t)(from.matrix [i] * multiplier);
    112     }
    113     return XCAM_RETURN_NO_ERROR;
    114 }
    115 
    116 
    117 XCamReturn
    118 IspConfigTranslator::translate_exposure (
    119     const XCam3aResultExposure &from,
    120     struct atomisp_exposure &to)
    121 {
    122     uint32_t coarse_time = 0, fine_time = 0;
    123     int32_t analog_code = 0, digital_code = 0;
    124     if (!_sensor->is_ready ()) {
    125         XCAM_LOG_WARNING ("translate exposure failed since sensor not ready");
    126         return XCAM_RETURN_ERROR_SENSOR;
    127     }
    128     if (!_sensor->exposure_time_to_integration (from.exposure_time, coarse_time, fine_time)) {
    129         XCAM_LOG_WARNING ("translate exposure time failed");
    130         return XCAM_RETURN_ERROR_SENSOR;
    131     }
    132     to.integration_time[0] = coarse_time;
    133     to.integration_time[1] = fine_time;
    134 
    135     if (!_sensor->exposure_gain_to_code (from.analog_gain, from.digital_gain, analog_code, digital_code)) {
    136         XCAM_LOG_WARNING ("translate exposure gain failed");
    137         return XCAM_RETURN_ERROR_SENSOR;
    138     }
    139     to.gain[0] = analog_code;
    140     to.gain[1] = digital_code;
    141     return XCAM_RETURN_NO_ERROR;
    142 }
    143 
    144 };
    145