xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkisp_demo/demo/sample/sample_csm_module.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 "sample_comm.h"
19 #include "uAPI2/rk_aiq_user_api2_acsm.h"
20 
sample_print_csm_info(const void * arg)21 void sample_print_csm_info(const void *arg)
22 {
23     printf ("enter CSM modult test!\n");
24 }
25 
sample_set_csm_attrib(const rk_aiq_sys_ctx_t * ctx,rk_aiq_uapi_mode_sync_e sync)26 static int sample_set_csm_attrib(const rk_aiq_sys_ctx_t* ctx, rk_aiq_uapi_mode_sync_e sync)
27 {
28     XCamReturn ret = XCAM_RETURN_NO_ERROR;
29     rk_aiq_uapi_acsm_attrib_t attrib;
30     memset(&attrib, 0, sizeof(attrib));
31     // TODO: get attrib first ?
32     ret = rk_aiq_user_api2_acsm_GetAttrib(ctx, &attrib);
33     RKAIQ_SAMPLE_CHECK_RET(ret, "setCsmAttr failed in getting csm attrib!");
34     printf("csm coeff[0]: %f\n", attrib.param.coeff[0]);
35 
36     attrib.sync.sync_mode = sync;
37     /* NOTE: RK_AIQ_OP_MODE_AUTO means default value now */
38     if (attrib.param.op_mode == RK_AIQ_OP_MODE_AUTO) {
39         attrib.param.op_mode = RK_AIQ_OP_MODE_MANUAL;
40         //limit_range coeff
41         attrib.param.full_range = true;
42         attrib.param.y_offset = 0;
43         attrib.param.c_offset = 0;
44         attrib.param.coeff[0] = 0.257;
45         attrib.param.coeff[1] = 0.504;
46         attrib.param.coeff[2] = 0.098;
47         attrib.param.coeff[3] = -0.148;
48         attrib.param.coeff[4] = -0.291;
49         attrib.param.coeff[5] = 0.439;
50         attrib.param.coeff[6] = 0.439;
51         attrib.param.coeff[7] = -0.368;
52         attrib.param.coeff[8] = -0.071;
53     } else {
54         attrib.param.op_mode = RK_AIQ_OP_MODE_AUTO;
55     }
56 
57     ret = rk_aiq_user_api2_acsm_SetAttrib(ctx, &attrib);
58     RKAIQ_SAMPLE_CHECK_RET(ret, "set CSM Attr failed!");
59     printf("set CSM Attr\n\n");
60 
61     return 0;
62 }
63 
sample_get_csm_attrib(const rk_aiq_sys_ctx_t * ctx)64 static int sample_get_csm_attrib(const rk_aiq_sys_ctx_t* ctx)
65 {
66     XCamReturn ret = XCAM_RETURN_NO_ERROR;
67     rk_aiq_uapi_acsm_attrib_t attrib;
68     memset(&attrib, 0, sizeof(rk_aiq_uapi_acsm_attrib_t));
69 
70     ret = rk_aiq_user_api2_acsm_GetAttrib(ctx, &attrib);
71     RKAIQ_SAMPLE_CHECK_RET(ret, "get CSM Attr failed!");
72     printf("get CSM Attr\n\n");
73     printf("\t sync = %d, done = %d\n", attrib.sync.sync_mode, attrib.sync.done);
74     if (attrib.param.op_mode == RK_AIQ_OP_MODE_AUTO) {
75         printf("\t mode = Auto\n");
76     } else if (attrib.param.op_mode == RK_AIQ_OP_MODE_MANUAL) {
77         printf("\t mode = Manual\n");
78     } else {
79         printf("\t mode = Invalid\n");
80     }
81     printf("csm range: %s\n", attrib.param.full_range ? "full" : "limit");
82     printf("csm y_offset: %d\n", attrib.param.y_offset);
83     printf("csm c_offset: %d\n", attrib.param.c_offset);
84     printf("csm coeff:\n");
85     for (int i = 0; i < 3; i+=3) {
86         printf("[%f %f %f]\n",
87                attrib.param.coeff[i],
88                attrib.param.coeff[i + 1],
89                attrib.param.coeff[i + 2]);
90     }
91     return 0;
92 }
93 
sample_set_csm_cspace(const rk_aiq_sys_ctx_t * ctx)94 static int sample_set_csm_cspace(const rk_aiq_sys_ctx_t* ctx)
95 {
96     XCamReturn ret = XCAM_RETURN_NO_ERROR;
97     int Cspace = -1;
98     printf("\t please press the Cspace: (0-601f 1-601l 2-709f 3-709l)");
99 
100     Cspace = getchar();
101     while (Cspace == '\n' || Cspace == '\r')
102         Cspace = getchar();
103     printf ("\n");
104     Cspace = Cspace - '0';
105     ret = rk_aiq_uapi2_setColorSpace(ctx, Cspace);
106     RKAIQ_SAMPLE_CHECK_RET(ret, "set colorspace failed!");
107     printf("Set Cspace: %d (0-601f 1-601l 2-709f 3-709l)\n", Cspace);
108 
109     return 0;
110 }
111 
sample_get_colorspace(const rk_aiq_sys_ctx_t * ctx)112 static int sample_get_colorspace(const rk_aiq_sys_ctx_t* ctx)
113 {
114     XCamReturn ret = XCAM_RETURN_NO_ERROR;
115     int Cspace = -1;
116     ret = rk_aiq_uapi2_getColorSpace(ctx, &Cspace);
117     RKAIQ_SAMPLE_CHECK_RET(ret, "get colorspace failed!");
118     switch (Cspace)
119     {
120         case 0:
121             printf("Color Space: BT.601 full\n\n");
122             break;
123         case 1:
124             printf("Color Space: BT.601 limit\n\n");
125             break;
126         case 2:
127             printf("Color Space: BT.709 full\n\n");
128             break;
129         case 3:
130             printf("Color Space: BT.709 limit\n\n");
131             break;
132         default:
133             printf("Color Space: other\n\n");
134             break;
135     }
136     return 0;
137 }
138 
sample_csm_set_attr_async(const rk_aiq_sys_ctx_t * ctx)139 static int sample_csm_set_attr_async(const rk_aiq_sys_ctx_t* ctx)
140 {
141     sample_set_csm_attrib(ctx, RK_AIQ_UAPI_MODE_ASYNC);
142     sample_get_csm_attrib(ctx);
143     usleep(40*1000);
144     sample_get_csm_attrib(ctx);
145 
146   return 0;
147 }
148 
sample_csm_set_attr_sync(const rk_aiq_sys_ctx_t * ctx)149 static int sample_csm_set_attr_sync(const rk_aiq_sys_ctx_t* ctx)
150 {
151     sample_set_csm_attrib(ctx, RK_AIQ_UAPI_MODE_DEFAULT);
152     sample_get_csm_attrib(ctx);
153 
154   return 0;
155 }
156 
157 uapi_case_t csm_uapi_list[] = {
158   { .desc = "CSM: set csm attr async",
159     .func = (uapi_case_func)sample_csm_set_attr_async
160   },
161   { .desc = "CSM: set csm attr sync",
162     .func = (uapi_case_func)sample_csm_set_attr_sync
163   },
164   { .desc = "CSM: set csm color space",
165     .func = (uapi_case_func)sample_set_csm_cspace
166   },
167   { .desc = "CSM: get csm color space",
168     .func = (uapi_case_func)sample_get_colorspace
169   },
170   {
171     .desc = NULL,
172     .func = NULL,
173   }
174 };
175 
sample_csm_module(const void * arg)176 XCamReturn sample_csm_module(const void *arg)
177 {
178     int key = -1;
179     CLEAR();
180     const demo_context_t *demo_ctx = (demo_context_t *)arg;
181     const rk_aiq_sys_ctx_t* ctx;
182     if (demo_ctx->camGroup) {
183         ctx = (rk_aiq_sys_ctx_t*)(demo_ctx->camgroup_ctx);
184     } else {
185         ctx = (rk_aiq_sys_ctx_t*)(demo_ctx->aiq_ctx);
186     }
187 
188     /*TODO: when rkaiq_3A_server & rkisp_demo run in two different shell, rk_aiq_sys_ctx_t would be null?*/
189     if (ctx == NULL) {
190         ERR ("%s, ctx is nullptr\n", __FUNCTION__);
191         return XCAM_RETURN_ERROR_PARAM;
192     }
193 
194     uapi_process_loop(ctx, csm_uapi_list);
195 
196     return XCAM_RETURN_NO_ERROR;
197 }
198