xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/IspFec/src/rk_ispfec.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *  Copyright (c) 2022 Rockchip Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #include "rk_ispfec_api.h"
19 #include "RkIspFecHwMgr.h"
20 #include "RkIspFecGenMesh.h"
21 #include "RkIspFecVersion.h"
22 #include <errno.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <linux/videodev2.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 using namespace RKISPFEC;
35 
36 typedef struct rk_ispfec_ctx_s {
37     rk_ispfec_cfg_t _cfg;
38     bool _isCfg;
39     RkIspFecHwMgr* _fecHwMgr;
40     enum {
41         STATE_IDLE,
42         STATE_RUNNING,
43         STATE_QUIT,
44     };
45     pthread_mutex_t _mutex;
46     int _state;
47 
48     RkIspFecGenMesh* _fecGenMesh;
49 } rk_ispfec_ctx_t;
50 
51 static
_print_versions()52 void _print_versions()
53 {
54     printf("\n"
55          "************************** VERSION INFOS **************************\n"
56          "version release date: %s\n"
57          "         IspFec:       %s\n"
58          "************************ VERSION INFOS END ************************\n"
59          , RK_ISP_FEC_RELEASE_DATE
60          , RK_ISP_FEC_VERSION
61         );
62 }
63 
64 static int
_load_mesh(const char * file,void * vir_addr,size_t size)65 _load_mesh(const char* file, void* vir_addr, size_t size)
66 {
67     FILE *fp = fopen(file, "rb");
68     int ret = 0;
69 
70     if (fp) {
71         ret = fseek(fp, 0L, SEEK_END);
72         if (ret < 0) {
73             printf("E: %s fseek to end failed\n", file);
74             return ret;
75         }
76 
77         size_t len = ftell(fp);
78         rewind(fp);
79 
80         if (len > size)
81             printf("E: mesh file len %ld > buf size %ld", len, size);
82 #if 0
83         void* map = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, dstFd, 0);
84 
85         if (map == MAP_FAILED) {
86             printf("E: failed to mmap buffer\n");
87             ret = -1;
88         } else {
89             size_t rd_size = fread(map, 1, len, fp);
90             if (rd_size < len)
91                 printf("file read size:%ld < %ld \n", rd_size, len);
92             munmap(map, len);
93         }
94 #else
95         size_t rd_size = fread(vir_addr, 1, len, fp);
96         if (rd_size < len)
97             printf("file read size:%ld < %ld \n", rd_size, len);
98 #endif
99         fclose(fp);
100     } else {
101         printf("E: open file %s failed: %s \n", file, strerror(errno));
102     }
103 
104     return ret;
105 }
106 
107 static int
_configure(rk_ispfec_ctx_t * ctx,rk_ispfec_cfg_t * cfg)108 _configure(rk_ispfec_ctx_t* ctx, rk_ispfec_cfg_t* cfg)
109 {
110     #define MESH_FILE_MAX_LEN 256
111     int ret = 0;
112 
113     pthread_mutex_lock (&ctx->_mutex);
114 
115     if (cfg->mesh_upd_mode == RK_ISPFEC_UPDATE_MESH_ONLINE) {
116 #ifdef GENMESH_ONLINE
117         if (!ctx->_fecGenMesh) {
118             ctx->_fecGenMesh = new RkIspFecGenMesh();
119             if (!ctx->_fecGenMesh) {
120                 printf("E: Failed to new RkIspFecGenMesh\n");
121                 goto err;
122             }
123         }
124 
125         ret = ctx->_fecGenMesh->init(cfg->in_width, cfg->in_height, cfg->out_width, cfg->out_height,
126                                 cfg->u.mesh_online.light_center, cfg->u.mesh_online.coeff,
127                                 cfg->u.mesh_online.direction, cfg->u.mesh_online.style);
128         if (ret < 0) {
129             printf("E: Failed to init RkIspFecGenMesh\n");
130             goto err;
131         }
132 
133         ctx->_fecGenMesh->setMeshBuf(cfg->mesh_xint.vir_addr, cfg->mesh_yint.vir_addr,
134                                      cfg->mesh_xfra.vir_addr, cfg->mesh_yfra.vir_addr);
135 
136         ret = ctx->_fecGenMesh->genMesh(cfg->u.mesh_online.correct_level);
137         if (ret < 0) {
138             printf("E: Failed to genMesh RkIspFecGenMesh\n");
139             goto err;
140         }
141 #endif
142     } else {
143         char mesh_file[MESH_FILE_MAX_LEN] = {'\0'};
144 
145         if (ctx == NULL)
146             return -1;
147 
148         // load mesh_xint
149         snprintf(mesh_file, MESH_FILE_MAX_LEN, "%s/%s", cfg->u.mesh_file_path, cfg->mesh_xint.mesh_file);
150         ret = _load_mesh(mesh_file, cfg->mesh_xint.vir_addr, cfg->mesh_xint.size);
151         if (ret) {
152             printf("E: load mesh_xint error!\n");
153             goto err;
154         }
155         // load mesh_xfra
156         snprintf(mesh_file, MESH_FILE_MAX_LEN, "%s/%s", cfg->u.mesh_file_path, cfg->mesh_xfra.mesh_file);
157         ret = _load_mesh(mesh_file, cfg->mesh_xfra.vir_addr, cfg->mesh_xfra.size);
158         if (ret) {
159             printf("E: load mesh_xfra error!\n");
160             goto err;
161         }
162 
163         // load mesh_yint
164         snprintf(mesh_file, MESH_FILE_MAX_LEN, "%s/%s", cfg->u.mesh_file_path, cfg->mesh_yint.mesh_file);
165         ret = _load_mesh(mesh_file, cfg->mesh_yint.vir_addr, cfg->mesh_yint.size);
166         if (ret) {
167             printf("E: load mesh_yint error!\n");
168             goto err;
169         }
170 
171         // load mesh_yfra
172         snprintf(mesh_file, MESH_FILE_MAX_LEN, "%s/%s", cfg->u.mesh_file_path, cfg->mesh_yfra.mesh_file);
173         ret = _load_mesh(mesh_file, cfg->mesh_yfra.vir_addr, cfg->mesh_yfra.size);
174         if (ret) {
175             printf("E: load mesh_yfra error!\n");
176             goto err;
177         }
178 //#ifdef GENMESH_ONLINE
179     }
180 //#endif
181 
182     memcpy(&ctx->_cfg, cfg, sizeof(rk_ispfec_cfg_t));
183     ctx->_isCfg = true;
184 
185     pthread_mutex_unlock (&ctx->_mutex);
186 
187     return 0;
188 err:
189     pthread_mutex_unlock (&ctx->_mutex);
190     return -1;
191 }
192 
193 void
rk_ispfec_api_deinit(rk_ispfec_ctx_t * ctx)194 rk_ispfec_api_deinit(rk_ispfec_ctx_t* ctx)
195 {
196     if (ctx == NULL)
197         return ;
198 
199     pthread_mutex_lock (&ctx->_mutex);
200     while (ctx->_state == rk_ispfec_ctx_t::STATE_RUNNING) {
201         usleep(1 * 1000);
202         printf("I: in Running state, wait to IDLE ! \n");
203     }
204 
205     ctx->_state = rk_ispfec_ctx_t::STATE_QUIT;
206     pthread_mutex_unlock (&ctx->_mutex);
207 
208 #ifdef GENMESH_ONLINE
209     if (ctx->_fecGenMesh) {
210         ctx->_fecGenMesh->deinit();
211         delete ctx->_fecGenMesh;
212     }
213 #endif
214 
215     ctx->_fecHwMgr->deinit();
216     pthread_mutex_destroy (&ctx->_mutex);
217 
218     delete ctx;
219 
220     return;
221 }
222 
223 rk_ispfec_ctx_t*
rk_ispfec_api_init(rk_ispfec_cfg_t * cfg)224 rk_ispfec_api_init(rk_ispfec_cfg_t* cfg)
225 {
226     _print_versions();
227 
228     rk_ispfec_ctx_t* ctx = new rk_ispfec_ctx_t();
229 
230     ctx->_state = rk_ispfec_ctx_t::STATE_IDLE;
231 
232     int ret = pthread_mutex_init (&ctx->_mutex, NULL);
233     if (ret) {
234         printf("Mutex init failed: %d \n", ret);
235         goto err;
236     }
237     ctx->_isCfg = false;
238     ctx->_fecGenMesh = NULL;
239     ctx->_fecHwMgr = RkIspFecHwMgr::getInstance();
240     if (!ctx->_fecHwMgr) {
241         printf("get RkIspFecHwMgr failed: %d \n", ret);
242         goto err;
243     }
244 
245     if (cfg) {
246         ret = _configure(ctx, cfg);
247         if (ret) {
248             printf("E: configure failed: %d \n", ret);
249             goto err;
250         }
251     }
252 
253     return ctx;
254 err:
255     if (ctx) {
256         rk_ispfec_api_deinit(ctx);
257     }
258 
259     return NULL;
260 }
261 
262 int
rk_ispfec_api_prepare(rk_ispfec_ctx_t * ctx,rk_ispfec_cfg_t * cfg)263 rk_ispfec_api_prepare(rk_ispfec_ctx_t* ctx, rk_ispfec_cfg_t* cfg)
264 {
265     int ret = 0;
266 
267     if (ctx == NULL)
268         return -1;
269 
270 #if 0
271     while (ctx->_state == rk_ispfec_ctx_t::STATE_RUNNING) {
272         usleep(1 * 1000);
273         printf("I: in Running state, wait to IDLE ! \n");
274     }
275 #endif
276 
277 
278     if (cfg) {
279         ret = _configure(ctx, cfg);
280         if (ret)
281             ctx->_isCfg = false;
282     }
283 
284 
285     return ret;
286 }
287 
288 int
rk_ispfec_api_process(rk_ispfec_ctx_t * ctx,int src_fd,int dst_fd)289 rk_ispfec_api_process(rk_ispfec_ctx_t* ctx, int src_fd, int dst_fd)
290 {
291     int ret = 0;
292     struct rkispp_fec_in_out param;
293 
294     if (ctx == NULL)
295         return -1;
296 
297     pthread_mutex_lock (&ctx->_mutex);
298     if (ctx->_state == rk_ispfec_ctx_t::STATE_QUIT) {
299         pthread_mutex_unlock (&ctx->_mutex);
300         return 0;
301     }
302     ctx->_state = rk_ispfec_ctx_t::STATE_RUNNING;
303 
304     if (!ctx->_isCfg) {
305         printf("E: not configured !\n");
306         goto out;
307     }
308 
309     param.in_width      = ctx->_cfg.in_width;
310     param.in_height     = ctx->_cfg.in_height;
311     param.out_width     = ctx->_cfg.out_width;
312     param.out_height    = ctx->_cfg.out_height;
313     param.in_fourcc     = V4L2_PIX_FMT_NV12;
314     param.out_fourcc    = V4L2_PIX_FMT_NV12;
315     param.in_pic_fd     = src_fd;
316     param.out_pic_fd    = dst_fd;
317     param.mesh_xint_fd  = ctx->_cfg.mesh_xint.dmaFd;
318     param.mesh_xfra_fd  = ctx->_cfg.mesh_xfra.dmaFd;
319     param.mesh_yint_fd  = ctx->_cfg.mesh_yint.dmaFd;
320     param.mesh_yfra_fd  = ctx->_cfg.mesh_yfra.dmaFd;
321 
322     ret = ctx->_fecHwMgr->process(param);
323     if (ret) {
324         printf("E: process error:%d\n", ret);
325     }
326 
327 out:
328     ctx->_state = rk_ispfec_ctx_t::STATE_IDLE;
329     pthread_mutex_unlock (&ctx->_mutex);
330 
331     return ret;
332 }
333 
334 int
rk_ispfec_api_calFecMeshsize(int width,int height)335 rk_ispfec_api_calFecMeshsize(int width, int height)
336 {
337     int mesh_size, mesh_left_height;
338     int w = 32 * ((width + 31) / 32);
339     int h = 32 * ((height + 31) / 32);
340     int spb_num = (h + 127) >> 7;
341     int left_height = h & 127;
342     bool density = (width > 1920) ? true : false;
343     int mesh_width = density ? (w / 32 + 1) : (w / 16 + 1);
344     int mesh_height = density ? 9 : 17;
345 
346     if (!left_height)
347         left_height = 128;
348 
349     mesh_left_height = density ? (left_height / 16 + 1) : (left_height / 8 + 1);
350     mesh_size = (spb_num - 1) * mesh_width * mesh_height + mesh_width * mesh_left_height;
351 
352     return mesh_size;
353 }
354 
355 #ifdef __cplusplus
356 }
357 #endif
358