• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * context_priv.cpp - capi private context
3  *
4  *  Copyright (c) 2017 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 "context_priv.h"
22 #include <ocl/cl_device.h>
23 #include <ocl/cl_image_handler.h>
24 #include <ocl/cl_tonemapping_handler.h>
25 #include <ocl/cl_gauss_handler.h>
26 #include <ocl/cl_wavelet_denoise_handler.h>
27 #include <ocl/cl_newwavelet_denoise_handler.h>
28 #include <ocl/cl_defog_dcp_handler.h>
29 #include <ocl/cl_3d_denoise_handler.h>
30 #include <ocl/cl_image_warp_handler.h>
31 #include <ocl/cl_fisheye_handler.h>
32 #include <ocl/cl_image_360_stitch.h>
33 #include <ocl/cl_utils.h>
34 
35 using namespace XCam;
36 
37 #define DEFAULT_INPUT_BUFFER_POOL_COUNT  20
38 static const char *HandleNames[] = {
39     "None",
40     "3DNR",
41     "WaveletNR",
42     "Fisheye",
43     "Defog",
44     "DVS",
45     "Stitch",
46 };
47 
48 bool
handle_name_equal(const char * name,HandleType type)49 handle_name_equal (const char *name, HandleType type)
50 {
51     return !strncmp (name, HandleNames[type], strlen(HandleNames[type]));
52 }
53 
ContextBase(HandleType type)54 ContextBase::ContextBase (HandleType type)
55     : _type (type)
56     , _usage (NULL)
57     , _image_width (0)
58     , _image_height (0)
59     , _alloc_out_buf (false)
60 {
61     if (!_inbuf_pool.ptr()) {
62         _inbuf_pool = new CLVideoBufferPool ();
63         XCAM_ASSERT (_inbuf_pool.ptr ());
64     }
65 }
66 
~ContextBase()67 ContextBase::~ContextBase ()
68 {
69     xcam_free (_usage);
70 }
71 
72 const char*
get_type_name() const73 ContextBase::get_type_name () const
74 {
75     XCAM_ASSERT ((int)_type < sizeof(HandleNames) / sizeof (HandleNames[0]));
76     return HandleNames [_type];
77 }
78 
79 static const char*
find_value(const ContextParams & param_list,const char * name)80 find_value (const ContextParams &param_list, const char *name)
81 {
82     ContextParams::const_iterator i = param_list.find (name);
83     if (i != param_list.end ())
84         return (i->second);
85     return NULL;
86 }
87 
88 XCamReturn
set_parameters(ContextParams & param_list)89 ContextBase::set_parameters (ContextParams &param_list)
90 {
91     VideoBufferInfo buf_info;
92     uint32_t image_format = V4L2_PIX_FMT_NV12;
93     _image_width = 1920;
94     _image_height = 1080;
95 
96     const char *width = find_value (param_list, "width");
97     if (width) {
98         _image_width = atoi(width);
99     }
100     const char *height = find_value (param_list, "height");
101     if (height) {
102         _image_height = atoi(height);
103     }
104     if (_image_width == 0 || _image_height == 0) {
105         XCAM_LOG_ERROR ("illegal image size width:%d height:%d", _image_width, _image_height);
106         return XCAM_RETURN_ERROR_PARAM;
107     }
108 
109     buf_info.init (image_format, _image_width, _image_height);
110     _inbuf_pool->set_video_info (buf_info);
111     if (!_inbuf_pool->reserve (DEFAULT_INPUT_BUFFER_POOL_COUNT)) {
112         XCAM_LOG_ERROR ("init buffer pool failed");
113         return XCAM_RETURN_ERROR_MEM;
114     }
115 
116     const char *flag = find_value (param_list, "alloc-out-buf");
117     if (flag && !strncasecmp (flag, "true", strlen("true"))) {
118         _alloc_out_buf = true;
119     } else {
120         _alloc_out_buf = false;
121     }
122     return XCAM_RETURN_NO_ERROR;
123 }
124 
125 XCamReturn
init_handler()126 ContextBase::init_handler ()
127 {
128     SmartPtr<CLContext> cl_context = CLDevice::instance()->get_context ();
129     XCAM_FAIL_RETURN (
130         ERROR, cl_context.ptr (), XCAM_RETURN_ERROR_UNKNOWN,
131         "ContextBase::init_handler(%s) failed since cl-context is NULL",
132         get_type_name ());
133 
134     SmartPtr<CLImageHandler> handler = create_handler (cl_context);
135     XCAM_FAIL_RETURN (
136         ERROR, handler.ptr (), XCAM_RETURN_ERROR_UNKNOWN,
137         "ContextBase::init_handler(%s) create handler failed", get_type_name ());
138 
139     handler->disable_buf_pool (!_alloc_out_buf);
140     set_handler (handler);
141     return XCAM_RETURN_NO_ERROR;
142 }
143 
144 XCamReturn
uinit_handler()145 ContextBase::uinit_handler ()
146 {
147     if (!_handler.ptr ())
148         return XCAM_RETURN_NO_ERROR;
149 
150     _handler->emit_stop ();
151     _handler.release ();
152     return XCAM_RETURN_NO_ERROR;
153 }
154 
155 XCamReturn
execute(SmartPtr<VideoBuffer> & buf_in,SmartPtr<VideoBuffer> & buf_out)156 ContextBase::execute (SmartPtr<VideoBuffer> &buf_in, SmartPtr<VideoBuffer> &buf_out)
157 {
158     if (!_alloc_out_buf) {
159         XCAM_FAIL_RETURN (
160             ERROR, buf_out.ptr (), XCAM_RETURN_ERROR_MEM,
161             "context (%s) execute failed, buf_out need set.", get_type_name ());
162     } else {
163         XCAM_FAIL_RETURN (
164             ERROR, !buf_out.ptr (), XCAM_RETURN_ERROR_MEM,
165             "context (%s) execute failed, buf_out need NULL.", get_type_name ());
166     }
167 
168     return _handler->execute (buf_in, buf_out);
169 }
170 
171 SmartPtr<CLImageHandler>
create_handler(SmartPtr<CLContext> & context)172 NR3DContext::create_handler (SmartPtr<CLContext> &context)
173 {
174     return create_cl_3d_denoise_image_handler (
175                context, CL_IMAGE_CHANNEL_Y | CL_IMAGE_CHANNEL_UV, 3);
176 }
177 
178 SmartPtr<CLImageHandler>
create_handler(SmartPtr<CLContext> & context)179 NRWaveletContext::create_handler (SmartPtr<CLContext> &context)
180 {
181     return create_cl_newwavelet_denoise_image_handler (
182                context, CL_IMAGE_CHANNEL_UV | CL_IMAGE_CHANNEL_Y, false);
183 }
184 
185 SmartPtr<CLImageHandler>
create_handler(SmartPtr<CLContext> & context)186 FisheyeContext::create_handler (SmartPtr<CLContext> &context)
187 {
188     return create_fisheye_handler (context);
189 }
190 
191 SmartPtr<CLImageHandler>
create_handler(SmartPtr<CLContext> & context)192 DefogContext::create_handler (SmartPtr<CLContext> &context)
193 {
194     return create_cl_defog_dcp_image_handler (context);;
195 }
196 
197 SmartPtr<CLImageHandler>
create_handler(SmartPtr<CLContext> & context)198 DVSContext::create_handler (SmartPtr<CLContext> &context)
199 {
200     return create_cl_image_warp_handler (context);
201 }
202 
203 SmartPtr<CLImageHandler>
create_handler(SmartPtr<CLContext> & context)204 StitchContext::create_handler (SmartPtr<CLContext> &context)
205 {
206     uint32_t sttch_width = _image_width;
207     uint32_t sttch_height = XCAM_ALIGN_UP (sttch_width / 2, 16);
208     if (sttch_width != sttch_height * 2) {
209         XCAM_LOG_ERROR ("incorrect stitch size width:%d height:%d", sttch_width, sttch_height);
210         return NULL;
211     }
212 
213     SurroundMode surround_mode = SphereView;
214     StitchResMode res_mode = StitchRes1080P;
215     if (_res_mode == StitchRes4K)
216         res_mode = StitchRes4K;
217 
218     SmartPtr<CLImage360Stitch> image_360 =
219         create_image_360_stitch (context, _need_seam, _scale_mode,
220                                  _fisheye_map, _need_lsc, surround_mode, res_mode).dynamic_cast_ptr<CLImage360Stitch> ();
221     XCAM_FAIL_RETURN (ERROR, image_360.ptr (), NULL, "create image stitch handler failed");
222     image_360->set_output_size (sttch_width, sttch_height);
223     XCAM_LOG_INFO ("stitch output size width:%d height:%d", sttch_width, sttch_height);
224 
225 #if HAVE_OPENCV
226     image_360->set_feature_match_ocl (_fm_ocl);
227 #endif
228 
229     return image_360;
230 }
231 
232