hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * Copyright (C) 2019 Rockchip Electronics Co., Ltd.
 * author: Zhihua Wang, hogan.wang@rock-chips.com
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL), available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <stdbool.h>
#include <pthread.h>
 
#include "usb_camera.h"
#include "vpu_decode.h"
#include "rga_control.h"
#include "rockface_control.h"
#include "video_common.h"
#include "rkfacial.h"
 
#define CAMERA_NUM 15
#define BUFFER_COUNT 4
 
struct map_buffer {
    void *start;
    size_t length;
};
 
static int g_fd = -1;
static struct map_buffer g_map_buf[BUFFER_COUNT] = {0};
static int g_width, g_height;
static bool g_run;
static pthread_t g_th;
static struct vpu_decode g_decode;
static bo_t g_dec_bo;
static int g_dec_fd = -1;
 
static bool g_usb_en;
static int g_usb_width;
static int g_usb_height;
static display_callback g_display_cb = NULL;
static pthread_mutex_t g_display_lock = PTHREAD_MUTEX_INITIALIZER;
static int g_rotation = HAL_TRANSFORM_ROT_90;
 
void set_usb_rotation(int angle)
{
    if (angle == 90)
        g_rotation = HAL_TRANSFORM_ROT_90;
    else if (angle == 270)
        g_rotation = HAL_TRANSFORM_ROT_270;
}
 
void set_usb_display(display_callback cb)
{
    pthread_mutex_lock(&g_display_lock);
    g_display_cb = cb;
    pthread_mutex_unlock(&g_display_lock);
}
 
void set_usb_param(int width, int height, display_callback cb)
{
    g_usb_en = true;
    g_usb_width = width;
    g_usb_height = height;
    set_usb_display(cb);
}
 
static int qbuf(int fd, struct v4l2_buffer *buf)
{
    int ret;
 
    ret = ioctl(fd, VIDIOC_QBUF, buf);
    if (ret < 0) {
        perror("VIDIOC_QBUF");
        return -1;
    }
    return 0;
}
 
static int dqbuf(int fd, struct v4l2_buffer *buf)
{
    int ret;
 
    ret = ioctl(fd, VIDIOC_DQBUF, buf);
    if (ret < 0) {
        perror("VIDIOC_DQBUF");
        return -1;
    }
    return 0;
}
 
static int stream_off(int fd)
{
    int ret;
    enum v4l2_buf_type type;
 
    memset(&type, 0, sizeof(type));
    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    ret = ioctl(fd, VIDIOC_STREAMOFF, &type);
    if (ret < 0) {
        perror("VIDIOC_STREAMOFF");
        return -1;
    }
    return 0;
}
 
static int stream_on(int fd)
{
    int i;
    int ret;
    enum v4l2_buf_type type;
 
    memset(&type, 0, sizeof(type));
    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    ret = ioctl(fd, VIDIOC_STREAMON, &type);
    if (ret < 0) {
        perror("VIDIOC_STREAMON");
        return -1;
    }
    return 0;
}
 
static void free_bufs(struct map_buffer *map_buf)
{
    int i;
 
    for (i = 0; i < BUFFER_COUNT; i++) {
        if (map_buf[i].start) {
            munmap(map_buf[i].start, map_buf[i].length);
            map_buf[i].start = NULL;
        }
    }
}
 
static int req_bufs(int fd, struct map_buffer *map_buf)
{
    int i;
    int ret;
    struct v4l2_requestbuffers reqbuf;
 
    memset(&reqbuf, 0, sizeof(reqbuf));
    reqbuf.count = BUFFER_COUNT;
    reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    reqbuf.memory = V4L2_MEMORY_MMAP;
    ret = ioctl(fd, VIDIOC_REQBUFS, &reqbuf);
    if (ret < 0) {
        perror("VIDIOC_REQBUFS");
        return -1;
    }
    for (i = 0; i < reqbuf.count; i++) {
        struct v4l2_buffer buf;
        memset(&buf, 0, sizeof(buf));
        buf.index = i;
        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        buf.memory = V4L2_MEMORY_MMAP;
        ret = ioctl(fd, VIDIOC_QUERYBUF, &buf);
        if (ret < 0) {
            perror("VIDIOC_QUERYBUF");
            return -1;
        }
        map_buf[i].length = buf.length;
        map_buf[i].start = (char*)mmap(0, buf.length, PROT_READ | PROT_WRITE,
                MAP_SHARED, fd, buf.m.offset);
        if (map_buf[i].start == MAP_FAILED) {
            perror("mmap");
            return -1;
        }
        ret = ioctl(fd, VIDIOC_QBUF, &buf);
        if (ret < 0) {
            perror("VIDIOC_QBUF");
            return -1;
        }
    }
    return 0;
}
 
static int set_fmt(int fd, int *width, int *height)
{
    int ret;
    struct v4l2_format fmt;
 
    memset(&fmt, 0, sizeof(fmt));
    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    fmt.fmt.pix.width = *width;
    fmt.fmt.pix.height = *height;
    fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
    ret = ioctl(fd, VIDIOC_S_FMT, &fmt);
    if (ret < 0) {
        perror("VIDIOC_S_FMT");
        return -1;
    }
 
    ret = ioctl(fd, VIDIOC_G_FMT, &fmt);
    if (ret < 0) {
        perror("VIDIOC_G_FMT");
        return -1;
    }
    *width = fmt.fmt.pix.width;
    *height = fmt.fmt.pix.height;
    printf("%s: %dx%d\n", __func__, *width, *height);
    return 0;
}
 
static int querycap(int fd, char *type)
{
    struct v4l2_capability cap;
    memset(&cap, 0, sizeof(cap));
    if (ioctl(fd, VIDIOC_QUERYCAP, &cap)) {
        perror("VIDIOC_QUERYCAP");
        return -1;
    }
    if (!strstr(cap.bus_info, type))
        return -1;
    return 0;
}
 
static int open_usb_camera(void)
{
    int i;
    struct stat st;
    char name[32];
    int fd;
 
    for (i = 0; i < CAMERA_NUM; i++) {
        snprintf(name, sizeof(name), "/dev/video%d", i);
        if (stat(name, &st) == -1)
            continue;
        fd = open(name, O_RDWR, 0);
        if (fd < 0)
            continue;
        if (!querycap(fd, "usb")) {
            printf("%s: %s\n", __func__, name);
            return fd;
        }
        close(fd);
    }
    return -1;
}
 
static void *process(void *arg)
{
    struct v4l2_buffer buf;
    rga_info_t src, dst;
    int id = 0;
    RgaSURF_FORMAT fmt;
 
    memset(&buf, 0, sizeof(buf));
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buf.memory = V4L2_MEMORY_MMAP;
    while (g_run) {
        id++;
        if (dqbuf(g_fd, &buf))
            break;
 
        vpu_decode_jpeg_doing(&g_decode, g_map_buf[buf.index].start, buf.bytesused,
                              g_dec_fd, g_dec_bo.ptr);
 
        fmt = (g_decode.fmt == MPP_FMT_YUV422SP ? RK_FORMAT_YCbCr_422_SP : RK_FORMAT_YCbCr_420_SP);
        if (!rockface_control_convert_detect(g_dec_bo.ptr, g_width, g_height, fmt, g_rotation, id))
            rockface_control_convert_feature(g_dec_bo.ptr, g_width, g_height, fmt, g_rotation, id);
 
        pthread_mutex_lock(&g_display_lock);
        if (g_display_cb)
            g_display_cb(g_dec_bo.ptr, g_dec_fd, fmt,
                    g_width, g_height, g_rotation);
        pthread_mutex_unlock(&g_display_lock);
 
        if (qbuf(g_fd, &buf))
            break;
    }
 
    pthread_exit(NULL);
}
 
int usb_camera_init(void)
{
    int width = g_usb_width;
    int height = g_usb_height;
    if (!g_usb_en)
        return 0;
 
    g_fd = open_usb_camera();
    if (g_fd < 0) {
        printf("%s: %d exit!\n", __func__, __LINE__);
        return -1;
    }
    if (set_fmt(g_fd, &width, &height)) {
        printf("%s: %d exit!\n", __func__, __LINE__);
        return -1;
    }
    if (req_bufs(g_fd, g_map_buf)) {
        printf("%s: %d exit!\n", __func__, __LINE__);
        return -1;
    }
    if (stream_on(g_fd)) {
        printf("%s: %d exit!\n", __func__, __LINE__);
        return -1;
    }
 
    if (rga_control_buffer_init(&g_dec_bo, &g_dec_fd, width, height, 16)) {
        printf("%s: %d exit!\n", __func__, __LINE__);
        return -1;
    }
 
    if (vpu_decode_jpeg_init(&g_decode, width, height)) {
        printf("%s: %d exit!\n", __func__, __LINE__);
        return -1;
    }
 
    g_width = width;
    g_height = height;
 
    g_run = true;
    if (pthread_create(&g_th, NULL, process, NULL)) {
        printf("%s: %d exit!\n", __func__, __LINE__);
        return -1;
    }
 
    return 0;
}
 
void usb_camera_exit(void)
{
    if (!g_usb_en)
        return;
 
    g_run = false;
    stream_off(g_fd);
    free_bufs(g_map_buf);
    close(g_fd);
 
    if (g_th) {
        pthread_join(g_th, NULL);
        g_th = 0;
    }
    vpu_decode_jpeg_done(&g_decode);
    rga_control_buffer_deinit(&g_dec_bo, g_dec_fd);
}