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
/*
 * 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 <pthread.h>
#include <stdbool.h>
#include "rockface_control.h"
#include "video_common.h"
 
#ifdef CAMERA_ENGINE_RKISP
#include <camera_engine_rkisp/interface/rkisp_api.h>
#endif
#ifdef CAMERA_ENGINE_RKAIQ
#include <rkaiq/rkisp_api.h>
#endif
#include "rga_control.h"
#include <linux/media-bus-format.h>
#include "rkfacial.h"
 
static const struct rkisp_api_ctx *ctx;
static const struct rkisp_api_buf *buf;
static bool g_run;
static pthread_t g_tid;
 
static bool g_ir_en;
static int g_ir_width;
static int g_ir_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_270;
 
void set_ir_rotation(int angle)
{
    if (angle == 90)
        g_rotation = HAL_TRANSFORM_ROT_90;
    else if (angle == 270)
        g_rotation = HAL_TRANSFORM_ROT_270;
}
 
void set_ir_display(display_callback cb)
{
    pthread_mutex_lock(&g_display_lock);
    g_display_cb = cb;
    pthread_mutex_unlock(&g_display_lock);
}
 
void set_ir_param(int width, int height, display_callback cb)
{
    g_ir_en = true;
    g_ir_width = width;
    g_ir_height = height;
    set_ir_display(cb);
}
 
static void *process(void *arg)
{
    rga_info_t src, dst;
 
    do {
        buf = rkisp_get_frame(ctx, 0);
 
        rockface_control_convert_ir(buf->buf, ctx->width, ctx->height,
                                    RK_FORMAT_YCbCr_420_SP, g_rotation);
 
        pthread_mutex_lock(&g_display_lock);
        if (g_display_cb)
            g_display_cb(buf->buf, buf->fd, RK_FORMAT_YCbCr_420_SP,
                    ctx->width, ctx->height, g_rotation);
        pthread_mutex_unlock(&g_display_lock);
 
        rkisp_put_frame(ctx, buf);
    } while (g_run);
 
    pthread_exit(NULL);
}
 
int camir_control_init(void)
{
    char name[32];
 
    if (!g_ir_en)
        return 0;
 
#ifdef CAMERA_ENGINE_RKISP
    int id = get_video_id("stream_cif_dvp");
    if (id < 0) {
        printf("%s: get video id fail!\n", __func__);
        return -1;
    }
 
    snprintf(name, sizeof(name), "/dev/video%d", id);
    printf("%s: %s\n", __func__, name);
    ctx = rkisp_open_device(name, 0);
#endif
#ifdef CAMERA_ENGINE_RKAIQ
    ctx = rkisp_open_device2(CAM_TYPE_RKCIF);
#endif
    if (ctx == NULL) {
        printf("%s: ctx is NULL\n", __func__);
        return -1;
    }
 
    rkisp_set_buf(ctx, 3, NULL, 0);
#ifdef CAMERA_ENGINE_RKISP
    rkisp_set_sensor_fmt(ctx, 1280, 720, MEDIA_BUS_FMT_YUYV8_2X8);
#endif
    rkisp_set_fmt(ctx, 1280, 720, V4L2_PIX_FMT_NV12);
 
    if (rkisp_start_capture(ctx))
        return -1;
 
    g_run = true;
    if (pthread_create(&g_tid, NULL, process, NULL)) {
        printf("pthread_create fail\n");
        return -1;
    }
 
    return 0;
}
 
void camir_control_exit(void)
{
    if (!g_ir_en)
        return;
 
    g_run = false;
    if (g_tid) {
        pthread_join(g_tid, NULL);
        g_tid = 0;
    }
 
    rkisp_stop_capture(ctx);
    rkisp_close_device(ctx);
}
 
bool camir_control_run(void)
{
    return g_run;
}