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
/*
 * Copyright (C) 2019 Rockchip Electronics Co., Ltd.
 *
 * 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.
 */
 
#ifndef __UVC_VIDEO_H__
#define __UVC_VIDEO_H__
 
#ifdef __cplusplus
extern "C" {
#endif
 
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <unistd.h>
#include <linux/videodev2.h>
#include "uvc-gadget.h"
 
#define V4L2_PIX_FMT_H265     v4l2_fourcc('H', '2', '6', '5') /* H265 with start codes */
 
#define UVC_BUFFER_NUM 3 //use mjpeg_fps can set 4
#define YUYV_AS_RAW 0
 
#define UVC_DYNAMIC_DEBUG_USE_TIME 1 //release version can set to 0
#define UVC_DYNAMIC_DEBUG_USE_TIME_CHECK "/tmp/uvc_use_time"
 
#define UVC_DYNAMIC_DEBUG_FPS 1 //release version can set to 0
#define UVC_DYNAMIC_DEBUG_ISP_FPS_CHECK "/tmp/uvc_isp_fps"
#define UVC_DYNAMIC_DEBUG_IPC_FPS_CHECK "/tmp/uvc_ipc_fps"
 
#define UVC_SEND_BUF_WHEN_ENC_READY 1
 
 
#if UVC_DYNAMIC_DEBUG_FPS
struct uvc_debug_info_def
{
    bool first_frm;
    bool debug_isp_fps;
    bool debug_ipc_fps;
    float fps;
    int ipc_frm;
    int isp_frm;
    struct timeval enter_time;
    unsigned int stream_on_pts;
};
 
extern struct uvc_debug_info_def uvc_debug_info;
#endif
 
struct uvc_device;
 
struct uvc_buffer
{
    void *buffer;
    size_t size; // encode out size
    size_t total_size;
    int width;
    int height;
    int video_id;
 
    size_t drm_buf_size;
    int fd; // for packet buf fd
    unsigned int handle; // for drm handle
 
    int v4l2_fd;
    bool abandon;
    unsigned int pts;
    int frame_count;
    unsigned int seq;
};
 
struct uvc_user
{
    unsigned int width;
    unsigned int height;
    bool run;
    unsigned int fcc;
};
 
struct uvc_video
{
    int id;
    bool uvc_process;
    pthread_t uvc_pid;
    struct video_uvc *uvc;
    pthread_mutex_t buffer_mutex;
    pthread_mutex_t user_mutex;
    struct uvc_user uvc_user;
    int idle_cnt;
    struct uvc_buffer *buffer_s;
    int drm_fd; // drm fd
    bool can_exit;
    unsigned int last_pts;
    unsigned int now_pts;
    unsigned int last_seq;
    struct uvc_device *dev;
};
 
int uvc_gadget_pthread_create(struct uvc_function_config *fc);
 
int uvc_video_id_check(int id);
int uvc_video_id_add(struct uvc_function_config *fc);
void uvc_video_id_remove(int id);
void uvc_video_id_exit_all();
int uvc_video_id_get(unsigned int seq);
 
void uvc_video_set_uvc_process(int id, bool state);
bool uvc_video_get_uvc_process(int id);
 
int uvc_buffer_init(int id);
void uvc_buffer_deinit(int id);
bool uvc_buffer_write_enable(int id);
bool uvc_mpp_buffer_write_enable(int id);
 
void uvc_buffer_write(unsigned short stamp,
                      void *extra_data,
                      size_t extra_size,
                      void *data,
                      size_t size,
                      unsigned int fcc,
                      int id);
void uvc_set_user_resolution(int width, int height, int id);
void uvc_get_user_resolution(int *width, int *height, int id);
bool uvc_get_user_run_state(int id);
void uvc_set_user_run_state(bool state, int id);
void uvc_set_user_fcc(unsigned int fcc, int id);
unsigned int uvc_get_user_fcc(int id);
void uvc_memset_uvc_user(int id);
pthread_t *uvc_video_get_uvc_pid(int id);
void uvc_user_fill_buffer(struct uvc_device *dev, struct v4l2_buffer *buf, int id);
void uvc_buffer_write_set(int id, struct uvc_buffer *buf);
void uvc_buffer_all_set(int id, struct uvc_buffer *buf);
struct uvc_buffer *uvc_get_enc_data(struct uvc_device *dev, struct uvc_video *v, bool init);
#if UVC_SEND_BUF_WHEN_ENC_READY
int uvc_video_qbuf_index(struct uvc_device *dev, struct uvc_buffer *send_buf, int index, int len);
struct uvc_buffer *uvc_user_fill_buffer_init(struct uvc_device *dev);
#endif
void uvc_user_set_write_buffer(struct uvc_device *dev, struct v4l2_buffer *buf, int id);
bool uvc_buffer_read_enable(int id);
 
#ifdef __cplusplus
}
#endif
 
#endif