• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * isp_image_processor.cpp - isp image processor
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 #include "isp_image_processor.h"
22 #include "x3a_isp_config.h"
23 #include "isp_controller.h"
24 #include "isp_config_translator.h"
25 
26 namespace XCam {
27 
IspImageProcessor(SmartPtr<IspController> & controller)28 IspImageProcessor::IspImageProcessor (SmartPtr<IspController> &controller)
29     : ImageProcessor ("IspImageProcessor")
30     , _controller (controller)
31     , _3a_config (new X3aIspConfig)
32 {
33     _sensor = new SensorDescriptor;
34     _translator = new IspConfigTranslator (_sensor);
35     XCAM_LOG_DEBUG ("IspImageProcessor construction");
36 }
37 
~IspImageProcessor()38 IspImageProcessor::~IspImageProcessor ()
39 {
40     XCAM_LOG_DEBUG ("~IspImageProcessor destruction");
41 }
42 
43 XCamReturn
process_buffer(SmartPtr<VideoBuffer> & input,SmartPtr<VideoBuffer> & output)44 IspImageProcessor::process_buffer(SmartPtr<VideoBuffer> &input, SmartPtr<VideoBuffer> &output)
45 {
46     output = input;
47     return XCAM_RETURN_NO_ERROR;
48 }
49 
50 bool
can_process_result(SmartPtr<X3aResult> & result)51 IspImageProcessor::can_process_result (SmartPtr<X3aResult> &result)
52 {
53     if (result.ptr() == NULL)
54         return false;
55 
56     switch (result->get_type()) {
57     case X3aIspConfig::IspExposureParameters:
58     case X3aIspConfig::IspAllParameters:
59     case XCAM_3A_RESULT_WHITE_BALANCE:
60     case XCAM_3A_RESULT_EXPOSURE:
61     case XCAM_3A_RESULT_BLACK_LEVEL:
62     case XCAM_3A_RESULT_YUV2RGB_MATRIX:
63     case XCAM_3A_RESULT_RGB2YUV_MATRIX:
64     case XCAM_3A_RESULT_R_GAMMA:
65     case XCAM_3A_RESULT_G_GAMMA:
66     case XCAM_3A_RESULT_B_GAMMA:
67     case XCAM_3A_RESULT_MACC:
68     case XCAM_3A_RESULT_BAYER_NOISE_REDUCTION:
69         return true;
70     default:
71         return false;
72     }
73 
74     return false;
75 }
76 
77 XCamReturn
apply_3a_results(X3aResultList & results)78 IspImageProcessor::apply_3a_results (X3aResultList &results)
79 {
80     XCamReturn ret = XCAM_RETURN_NO_ERROR;
81 
82     if (results.empty())
83         return XCAM_RETURN_ERROR_PARAM;
84 
85     // activate sensor to make translator work
86     if (!_sensor->is_ready()) {
87         struct atomisp_sensor_mode_data sensor_data;
88         xcam_mem_clear (sensor_data);
89         if (_controller->get_sensor_mode_data(sensor_data) != XCAM_RETURN_NO_ERROR) {
90             XCAM_LOG_WARNING ("ispimageprocessor initiliaze sensor failed");
91         } else
92             _sensor->set_sensor_data (sensor_data);
93         XCAM_ASSERT (_sensor->is_ready());
94     }
95 
96     if ((ret = merge_results (results)) != XCAM_RETURN_NO_ERROR) {
97         XCAM_LOG_WARNING ("merge 3a result to isp config failed");
98         return XCAM_RETURN_ERROR_UNKNOWN;
99     }
100 
101     if ((ret = apply_exposure_result (results)) != XCAM_RETURN_NO_ERROR) {
102         XCAM_LOG_WARNING ("set 3a exposure to sensor failed");
103     }
104 
105     // check _3a_config
106     XCAM_ASSERT (_3a_config.ptr());
107     XCAM_ASSERT (_controller.ptr());
108     ret = _controller->set_3a_config (_3a_config.ptr());
109     if (ret != XCAM_RETURN_NO_ERROR) {
110         XCAM_LOG_WARNING ("set 3a config to isp failed");
111     }
112     _3a_config->clear ();
113     return ret;
114 }
115 
116 XCamReturn
apply_3a_result(SmartPtr<X3aResult> & result)117 IspImageProcessor::apply_3a_result (SmartPtr<X3aResult> &result)
118 {
119     XCamReturn ret = XCAM_RETURN_NO_ERROR;
120 
121     X3aResultList results;
122     results.push_back (result);
123     ret = apply_3a_results (results);
124     return ret;
125 }
126 
127 XCamReturn
merge_results(X3aResultList & results)128 IspImageProcessor::merge_results (X3aResultList &results)
129 {
130     if (results.empty())
131         return XCAM_RETURN_ERROR_PARAM;
132 
133     for (X3aResultList::iterator iter = results.begin ();
134             iter != results.end ();)
135     {
136         SmartPtr<X3aResult> &x3a_result = *iter;
137         if (_3a_config->attach (x3a_result, _translator.ptr())) {
138             x3a_result->set_done (true);
139             results.erase (iter++);
140         } else
141             ++iter;
142     }
143     return XCAM_RETURN_NO_ERROR;
144 }
145 
146 XCamReturn
apply_exposure_result(X3aResultList & results)147 IspImageProcessor::apply_exposure_result (X3aResultList &results)
148 {
149     XCamReturn ret = XCAM_RETURN_NO_ERROR;
150 
151     for (X3aResultList::iterator iter = results.begin ();
152             iter != results.end ();)
153     {
154         if ((*iter)->get_type() == X3aIspConfig::IspExposureParameters) {
155             SmartPtr<X3aIspExposureResult> res = (*iter).dynamic_cast_ptr<X3aIspExposureResult> ();
156             if (!res.ptr () ||
157                     ((ret = _controller->set_3a_exposure (res.ptr ())) != XCAM_RETURN_NO_ERROR)) {
158                 XCAM_LOG_WARNING ("set 3a exposure to sensor failed");
159             }
160             if (res.ptr ())
161                 res->set_done (true);
162             results.erase (iter++);
163         } else if ((*iter)->get_type() == XCAM_3A_RESULT_EXPOSURE) {
164             SmartPtr<X3aExposureResult> res = (*iter).dynamic_cast_ptr<X3aExposureResult> ();
165             struct atomisp_exposure isp_exposure;
166             xcam_mem_clear (isp_exposure);
167             XCAM_ASSERT (res.ptr ());
168             ret = _translator->translate_exposure (res->get_standard_result (), isp_exposure);
169             if (ret != XCAM_RETURN_NO_ERROR) {
170                 XCAM_LOG_WARNING ("translate 3a exposure to sensor failed");
171             }
172             if ((ret = _controller->set_3a_exposure (isp_exposure)) != XCAM_RETURN_NO_ERROR) {
173                 XCAM_LOG_WARNING ("set 3a exposure to sensor failed");
174             }
175             res->set_done (true);
176             results.erase (iter++);
177         } else
178             ++iter;
179     }
180     return XCAM_RETURN_NO_ERROR;
181 }
182 
IspExposureImageProcessor(SmartPtr<IspController> & controller)183 IspExposureImageProcessor::IspExposureImageProcessor (SmartPtr<IspController> &controller)
184     : IspImageProcessor (controller)
185 {
186 }
187 
188 bool
can_process_result(SmartPtr<X3aResult> & result)189 IspExposureImageProcessor::can_process_result (SmartPtr<X3aResult> &result)
190 {
191     if (result.ptr() == NULL)
192         return false;
193 
194     switch (result->get_type()) {
195     case XCAM_3A_RESULT_EXPOSURE:
196         return true;
197 
198     default:
199         return false;
200     }
201 
202     return false;
203 }
204 
205 };
206