hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Isp20SpThread.h - isp20 self-path frame reading thread for event and buffer
 *
 *  Copyright (c) 2019 Rockchip Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#ifndef _ISP20_SP_THREAD_H_
#define _ISP20_SP_THREAD_H_
 
#include "xcam_thread.h"
#include "xcam_mutex.h"
 
 
#define RK_AIQ_MOTION_DETECTION_VERSION_HEAD     "AIQ MDETECTION "
#define RK_AIQ_MOTION_DETECTION_VERSION_STR      "v1.2.0"
#define  RK_AIQ_MOTION_DETECTION_VERSION \
    RK_AIQ_MOTION_DETECTION_VERSION_HEAD \
    RK_AIQ_MOTION_DETECTION_VERSION_STR
 
#define MOTIONDETECT_SUBM                   (0x20)
 
using namespace XCam;
 
namespace RkCam {
 
#define CLIP(a, min_v, max_v)                (((a) < (min_v)) ? (min_v) : (((a) > (max_v)) ? (max_v) : (a)))
#define LOG_PRINT(a, b)            \
   if(1)                    \
       printf("%s",b);        \
   if(0)                        \
       fprintf(a, "%s", b);
 
#define ALIGN_UP(a, size)  ((a + size - 1) & (~(size - 1)))
 
typedef struct RKAnr_Mt_Params_t
{
      float    sigmaHScale[3][MAX_ISO_STEP];
   float    sigmaLScale[3][MAX_ISO_STEP];
   float     light_clp[3][MAX_ISO_STEP];;
   float     uv_weight[3][MAX_ISO_STEP];;
    float     mfnr_sigma_scale[3][MAX_ISO_STEP];
    float     yuvnr_gain_scale[3][MAX_ISO_STEP][3];
   float     reserved[10][3][MAX_ISO_STEP];
 
} RKAnr_Mt_Params_t;
 
typedef struct RKAnr_Mt_Params_Select_t
{
    int enable;
      float sigmaHScale;
   float sigmaLScale;
   float light_clp;
   float uv_weight;
    float mfnr_sigma_scale;
    float yuvnr_gain_scale[3];
    float frame_limit_y;
    float frame_limit_uv;
    float gain_scale_l_y;
    float gain_scale_h_y;
    float gain_scale_l_uv;
    float gain_scale_h_uv;
 
} RKAnr_Mt_Params_Select_t;
 
enum MSG_CMD {
    MSG_CMD_WR_START,
    MSG_CMD_WR_EXIT,
};
 
typedef struct _sp_message_s {
    int cmd;
    bool sync;
    SmartPtr<Mutex>             mutex;
    SmartPtr<XCam::Cond>        cond;
    uint32_t arg1;
    void* arg2;
    int32_t arg3;
} sp_msg_t;
 
class Isp20SpThread
    : public Thread
{
    friend class KgProcThread;
    friend class WrProcThread;
public:
    Isp20SpThread               ();
    virtual ~Isp20SpThread      ();
    void set_sp_dev                (SmartPtr<V4l2SubDevice> ispdev, SmartPtr<V4l2Device> ispspdev, SmartPtr<V4l2SubDevice> isppdev, SmartPtr<V4l2SubDevice> snsdev);
    void set_sp_img_size        (int w, int h, int w_align, int h_align);
    void set_gain_isp            (void *buf, uint8_t* ratio);
    void set_gain_wr            (void *buf, uint8_t* ratio);
   void set_gainkg                (void *buf, uint8_t* ratio, uint8_t* ratio_next);
    void start                  ();
    void stop                   ();
    void pause                  ();
    void resume                 ();
    void set_calibDb(const CamCalibDbV2Context_t* calib);
    void set_working_mode(int mode);
    void update_motion_detection_params(CalibDb_MFNR_Motion_t *motion);
protected:
    void init                                  ();
    void deinit                                ();
    virtual bool loop                          ();
    XCamReturn kg_proc_loop                    ();
    bool       wr_proc_loop                    ();
    int subscrible_ispgain_event               (bool on);
    int wait_ispgain_event                     (unsigned int event_type, struct v4l2_event *event);
    void destroy_stop_fds_ispsp                ();
    XCamReturn create_stop_fds_ispsp           ();
    void notify_stop_fds_exit                  ();
    void notify_wr_thread_exit                 ();
    bool notify_yg_cmd                         (SmartPtr<sp_msg_t> msg);
    bool init_fbcbuf_fd                        ();
    bool init_tnrbuf_fd                        ();
    XCamReturn select_motion_params(RKAnr_Mt_Params_Select_t *stmtParamsSelected, uint32_t frameid);
private:
    SmartPtr<V4l2Device>  _isp_sp_dev;
    SmartPtr<V4l2SubDevice>  _ispp_dev;
    SmartPtr<V4l2SubDevice>  _isp_dev;
    SmartPtr<V4l2SubDevice> _sensor_dev;
    SmartPtr<Thread> mKgThread;
    SmartPtr<Thread> mWrThread;
    int _img_width;
    int _img_height;
    int _img_width_align;
    int _img_height_align;
    bool _first_frame;
   struct v4l2_event event;
   int _working_mode;
   bool _gray_mode;
 
   std::list<SmartPtr<V4l2BufferProxy>> _isp_buf_list;
    XCam::Mutex _buf_list_mutex;
    XCam::Mutex _motion_param_mutex;
    int ispsp_stop_fds[2];
    int ispp_stop_fds[2];
    int sync_pipe_fd[2];
    SafeList<sp_msg_t>  _notifyYgCmdQ;
    const CamCalibDbContext_t *_calibDb;
    int _isp_fd_array[16];
    uint32_t _isp_idx_array[16];
    int _isp_buf_num;
    int _ispp_fd_array[16];
    uint32_t _ispp_idx_array[16];
    int _ispp_buf_num;
    bool _fd_init_flag;
 
   int max_list_num;
   int first_frame;
   int frame_id_pp_upt;
   int frame_id_isp_upt;
   int frame_num_pp;
   int frame_num_isp;
 
   int _img_width_uv;
    int _img_height_uv;
    int gain_buf_size;
    int img_buf_size;
    int img_buf_size_uv;
 
   int gain_kg_tile_w_align;
   int gain_kg_tile_h_align;
    int ratio_stride;
   int gain_tile_gainkg_w;
   int gain_tile_gainkg_h;
   int gain_tile_ispp_w;
   int gain_tile_ispp_h;
   int gain_tile_ispp_x;
   int gain_tile_ispp_y;
   int gainkg_unit;
   int gain_tile_gainkg_size;
   int gain_tile_gainkg_stride;
   int gainkg_tile_num;
 
    int gain_blk_isp_w;
    int gain_blk_isp_stride;
    int gain_blk_isp_h;
    int gain_blk_isp_mem_size;
 
    int gain_blk_ispp_w;
    int gain_blk_ispp_stride;
    int gain_blk_ispp_h;
    int gain_blk_ispp_mem_size;
 
    int gainkg_blk_isp_w;
    int gainkg_blk_isp_h;
   uint8_t **static_ratio;
   uint8_t *frame_detect_flg;
   uint8_t **pImgbuf;
   uint8_t **gain_isp_buf_bak;
   uint8_t *pPreAlpha;
 
 
   RKAnr_Mt_Params_t             mtParams;
   RKAnr_Mt_Params_Select_t     mtParamsSelect;
   CalibDb_MFNR_Motion_t  _motion_params;
 
   uint8_t static_ratio_idx_in;
   uint8_t static_ratio_idx_out;
   uint8_t static_ratio_num;
 
   uint8_t static_ratio_l;
   uint8_t static_ratio_l_bit;
   uint8_t static_ratio_r_bit;
   int16_t *pTmpBuf;
};
 
class KgProcThread
    : public Thread
{
public:
    KgProcThread (Isp20SpThread *poll)
        : Thread ("kgThread")
        , _poll (poll)
    {}
 
protected:
    virtual bool loop () {
        XCamReturn ret = _poll->kg_proc_loop ();
 
        if (ret == XCAM_RETURN_NO_ERROR || ret == XCAM_RETURN_ERROR_TIMEOUT)
            return true;
        return false;
    }
 
private:
    Isp20SpThread *_poll;
};
 
class WrProcThread
    : public Thread
{
public:
    WrProcThread (Isp20SpThread *poll)
        : Thread ("wrThread")
        , _poll (poll)
    {}
 
protected:
    virtual bool loop () {
        return _poll->wr_proc_loop ();
    }
 
private:
    Isp20SpThread *_poll;
};
 
}
#endif