hc
2023-11-22 f743a7adbd6e230d66a6206fa115b59fec2d88eb
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h> /* getopt_long() */
#include <fcntl.h> /* low-level i/o */
#include <inttypes.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <pthread.h>
 
#include <linux/videodev2.h>
#include "rk_aiq_user_api_sysctl.h"
#include "common/mediactl/mediactl.h"
 
#define CLEAR(x) memset(&(x), 0, sizeof(x))
#define DBG(...) do { if(!silent) printf("DBG: " __VA_ARGS__);} while(0)
#define ERR(...) do { fprintf(stderr, "ERR: " __VA_ARGS__); } while (0)
 
 
/* Private v4l2 event */
#define CIFISP_V4L2_EVENT_STREAM_START  \
                    (V4L2_EVENT_PRIVATE_START + 1)
#define CIFISP_V4L2_EVENT_STREAM_STOP   \
                    (V4L2_EVENT_PRIVATE_START + 2)
 
#define RKAIQ_FILE_PATH_LEN                       64
#define RKAIQ_FLASH_NUM_MAX                       2
 
/* 1 vicap + 2 mipi + 1 bridge + 1 redundance */
#define MAX_MEDIA_NODES                           5
 
#define IQ_PATH "/etc/iqfiles/"
 
static int silent = 0;
static int width = 2688;
static int height = 1520;
static int has_mul_cam = 0;
 
struct rkaiq_media_info {
    char sd_isp_path[RKAIQ_FILE_PATH_LEN];
    char vd_params_path[RKAIQ_FILE_PATH_LEN];
    char vd_stats_path[RKAIQ_FILE_PATH_LEN];
    char mainpath[RKAIQ_FILE_PATH_LEN];
    char sensor_entity_name[32];
 
    char mdev_path[32];
    int available;
    rk_aiq_sys_ctx_t* aiq_ctx;
 
    pthread_t pid;
};
 
static struct rkaiq_media_info media_infos[MAX_MEDIA_NODES];
 
static void errno_exit(const char *s)
{
    ERR("%s error %d, %s\n", s, errno, strerror(errno));
    exit(EXIT_FAILURE);
}
 
static int xioctl(int fh, int request, void *arg)
{
    int r;
 
    do {
        r = ioctl(fh, request, arg);
    } while (-1 == r && EINTR == errno);
 
    return r;
}
 
 
static int rkaiq_get_devname(struct media_device *device, const char *name, char *dev_name)
{
    const char *devname;
    struct media_entity *entity =  NULL;
 
    entity = media_get_entity_by_name(device, name, strlen(name));
    if (!entity)
        return -1;
 
    devname = media_entity_get_devname(entity);
 
    if (!devname) {
        fprintf(stderr, "can't find %s device path!", name);
        return -1;
    }
 
    strncpy(dev_name, devname, RKAIQ_FILE_PATH_LEN);
 
    DBG("get %s devname: %s\n", name, dev_name);
 
    return 0;
}
 
int rkaiq_get_media_info(struct rkaiq_media_info *media_info)
{
    struct media_device *device = NULL;
    const char *sensor_name;
    int ret;
 
    device = media_device_new (media_info->mdev_path);
    if (!device)
        return -ENOMEM;
    /* Enumerate entities, pads and links. */
    ret = media_device_enumerate (device);
    if (ret)
        return ret;
    if (!ret) {
        /* Try rkisp */
        ret = rkaiq_get_devname(device, "rkisp-isp-subdev",
                                media_info->sd_isp_path);
        ret |= rkaiq_get_devname(device, "rkisp-input-params",
                                media_info->vd_params_path);
        ret |= rkaiq_get_devname(device, "rkisp-statistics",
                                media_info->vd_stats_path);
        ret |= rkaiq_get_devname(device, "rkisp_mainpath",
                                media_info->mainpath);
    }
    if (ret) {
        fprintf(stderr, "Cound not find rkisp dev names, skipped %s\n", media_info->mdev_path);
        media_device_unref (device);
        return ret;
    }
 
    sensor_name = rk_aiq_uapi_sysctl_getBindedSnsEntNmByVd(media_info->mainpath);
    if (sensor_name == NULL || strlen(sensor_name) == 0) {
        fprintf(stderr, "ERR: No sensor attached to %s\n", media_info->mdev_path);
        media_device_unref (device);
        return -EINVAL;
    }
 
    strcpy(media_info->sensor_entity_name, sensor_name);
 
    media_device_unref (device);
 
    return ret;
}
 
static void init_engine(struct rkaiq_media_info *media_info)
{
    int index;
 
    media_info->aiq_ctx = rk_aiq_uapi_sysctl_init(media_info->sensor_entity_name,
                                                  IQ_PATH, NULL, NULL);
    if (has_mul_cam)
        rk_aiq_uapi_sysctl_setMulCamConc(media_info->aiq_ctx, 1);
 
    if (rk_aiq_uapi_sysctl_prepare(media_info->aiq_ctx,
            width, height, RK_AIQ_WORKING_MODE_NORMAL)) {
        ERR("rkaiq engine prepare failed !\n");
        exit(-1);
    }
}
 
static void start_engine(struct rkaiq_media_info *media_info)
{
    DBG("device manager start\n");
    rk_aiq_uapi_sysctl_start(media_info->aiq_ctx);
    if (media_info->aiq_ctx == NULL) {
        ERR("rkisp_init engine failed\n");
        exit(-1);
    } else {
        DBG("rkisp_init engine succeed\n");
    }
}
 
static void stop_engine(struct rkaiq_media_info *media_info)
{
    rk_aiq_uapi_sysctl_stop(media_info->aiq_ctx, false);
}
 
static void deinit_engine(struct rkaiq_media_info *media_info)
{
    rk_aiq_uapi_sysctl_deinit(media_info->aiq_ctx);
}
 
// blocked func
static int wait_stream_event(int fd, unsigned int event_type, int time_out_ms)
{
    int ret;
    struct v4l2_event event;
 
    CLEAR(event);
 
    do {
        /*
         * xioctl instead of poll.
         * Since poll() cannot wait for input before stream on,
         * it will return an error directly. So, use ioctl to
         * dequeue event and block until sucess.
         */
        ret = xioctl(fd, VIDIOC_DQEVENT, &event);
        if (ret == 0 && event.type == event_type)
            return 0;
    } while (true);
 
    return -1;
 
}
 
static int subscrible_stream_event(struct rkaiq_media_info *media_info, int fd, bool subs)
{
    struct v4l2_event_subscription sub;
    int ret = 0;
 
    CLEAR(sub);
    sub.type = CIFISP_V4L2_EVENT_STREAM_START;
    ret = xioctl(fd,
                 subs ? VIDIOC_SUBSCRIBE_EVENT : VIDIOC_UNSUBSCRIBE_EVENT,
                 &sub);
    if (ret) {
        ERR("can't subscribe %s start event!\n", media_info->vd_params_path);
        exit(EXIT_FAILURE);
    }
 
    CLEAR(sub);
    sub.type = CIFISP_V4L2_EVENT_STREAM_STOP;
    ret = xioctl(fd,
                 subs ? VIDIOC_SUBSCRIBE_EVENT : VIDIOC_UNSUBSCRIBE_EVENT,
                 &sub);
    if (ret) {
        ERR("can't subscribe %s stop event!\n", media_info->vd_params_path);
    }
 
    DBG("subscribe events from %s success !\n", media_info->vd_params_path);
 
    return 0;
}
 
void parse_args(int argc, char **argv)
{
   int c;
   int digit_optind = 0;
 
   while (1) {
       int this_option_optind = optind ? optind : 1;
       int option_index = 0;
       static struct option long_options[] = {
           {"silent",    no_argument,       0, 's' },
           {"help",      no_argument,       0, 'h' },
           {0,           0,                 0,  0  }
       };
 
       c = getopt_long(argc, argv, "sh", long_options, &option_index);
       if (c == -1)
           break;
 
       switch (c) {
       case 'w':
           width = atoi(optarg);
           break;
       case 'h':
           height = atoi(optarg);
           break;
       case 's':
           silent = 1;
           break;
       case '?':
           ERR("Usage: %s to start 3A engine\n"
               "         --silent,  optional, subpress debug log\n",
               argv[0]);
           exit(-1);
 
       default:
           ERR("?? getopt returned character code %c ??\n", c);
       }
   }
}
 
void *engine_thread(void *arg)
{
    int ret = 0;
    int isp_fd;
    unsigned int stream_event = -1;
    struct rkaiq_media_info *media_info;
 
    media_info = (struct rkaiq_media_info *) arg;
 
    isp_fd = open(media_info->vd_params_path, O_RDWR);
    if (isp_fd < 0) {
        ERR("open %s failed %s\n", media_info->vd_params_path, strerror(errno));
        return NULL;
    }
 
    subscrible_stream_event(media_info, isp_fd, true);
    init_engine(media_info);
 
    for (;;) {
        DBG("%s: wait stream start event...\n", media_info->mdev_path);
        wait_stream_event(isp_fd, CIFISP_V4L2_EVENT_STREAM_START, -1);
        DBG("%s: wait stream start event success ...\n", media_info->mdev_path);
 
        start_engine(media_info);
 
        DBG("%s: wait stream stop event...\n", media_info->mdev_path);
        wait_stream_event(isp_fd, CIFISP_V4L2_EVENT_STREAM_STOP, -1);
        DBG("%s: wait stream stop event success ...\n", media_info->mdev_path);
 
        stop_engine(media_info);
    }
 
    deinit_engine(media_info);
    subscrible_stream_event(media_info, isp_fd, false);
    close(isp_fd);
 
    return NULL;
}
 
int main(int argc, char **argv)
{
    int ret, i;
    int threads = 0;
 
    /* Line buffered so that printf can flash every line if redirected to
     * no-interactive device.
     */
    setlinebuf(stdout);
 
    parse_args(argc, argv);
 
    for (i = 0; i < MAX_MEDIA_NODES; i++) {
        sprintf(media_infos[i].mdev_path, "/dev/media%d", i);
        if (rkaiq_get_media_info(&media_infos[i])) {
            ERR("Bad media topology for: %s\n", media_infos[i].mdev_path);
            media_infos[i].available = 0;
            continue;
        }
        media_infos[i].available = 1;
        threads++;
    }
 
    if (1 || threads > 1)
        has_mul_cam = 1;
 
    for (i = 0; i < MAX_MEDIA_NODES; i++) {
        if (!media_infos[i].available)
            continue;
        ret = pthread_create(&media_infos[i].pid, NULL, engine_thread, &media_infos[i]);
        if (ret) {
            media_infos[i].pid = 0;
            ERR("Failed to create camera engine thread for: %s\n", media_infos[i].mdev_path);
            errno_exit("Create thread failed");
        }
    }
 
    for (i = 0; i < MAX_MEDIA_NODES; i++) {
        if (!media_infos[i].available || media_infos[i].pid == 0)
            continue;
        pthread_join(media_infos[i].pid, NULL);
    }
 
    return 0;
}