liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
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
/*
 * Copyright (c) 2021 by Allwinnertech Co., Ltd.
 *
 * 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 V4L2_CAMERA_HAL_COMMON_H_
#define V4L2_CAMERA_HAL_COMMON_H_
 
#ifndef LOG_TAG
#define LOG_TAG "CameraHALv3_4"
#endif
 
#include <sys/time.h>
#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
#include <utils/Trace.h>
#include <linux/videodev2.h>
#include <system/graphics.h>
#include <hardware/gralloc.h>
#include <map>
#include <string>
 
#define ALIGN_4K(x) (((x) + (4095)) & ~(4095))
#define ALIGN_32B(x) (((x) + (31)) & ~(31))
#define ALIGN_16B(x) (((x) + (15)) & ~(15))
#define ALIGN_8B(x) (((x) + (7)) & ~(7))
 
#define DBG_CAMERA                    0
#define DBG_CAMERA_CONFIG             0
#define DBG_CAMERA_HAL                0
#define DBG_CAMERA_STREAM             0
#define DBG_STREAM_MANAGER            0
#define DBG_V4L2_CAMERA               0
#define DBG_V4L2_GRALLOC              0
#define DBG_V4L2_STREAM               0
#define DBG_V4L2_WRAPPER              0
// Debug setting.
#define DBG_PERFORMANCE               0
#define DBG_SAVE_OUTPUT               0
 
// Fix confliction in case it's defined elsewhere.
#ifndef DISALLOW_COPY_AND_ASSIGN
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
  TypeName(const TypeName&);  \
  void operator=(const TypeName&);
#endif
 
// Common setting.
 
#define DEVICE_FACING_FRONT   1
#define DEVICE_FACING_BACK    0
#define TIMEOUT_COUNT         0
#define MAX_STREAM_NUM        3
#define DROP_BUFFERS_NUM      3
#define MAX_FRAME_NUM         128
// ms delay between stream on and off.
#define DELAY_BETWEEN_STREAM  500
#define DELAY_BETWEEN_ON_OFF  0
 
#define MAIN_STREAM_PATH  "/dev/video0"
#define SUB_0_STREAM_PATH "/dev/video1"
#define MAIN_MERGE_STREAM_PATH "/dev/video1"
#define SUB_0_MERGE_STREAM_PATH "dev/video3"
#if defined __A100__ || defined __T509__
#define MAIN_FRONT_STREAM_PATH  "/dev/video2"
#define SUB_0_FRONT_STREAM_PATH "/dev/video3"
#else
#define MAIN_FRONT_STREAM_PATH  "/dev/video0"
#define SUB_0_FRONT_STREAM_PATH "/dev/video1"
#endif
 
enum STREAM_SERIAL {
  MAIN_STREAM = 0,
  MAIN_STREAM_BLOB,
  SUB_0_STREAM,
  SUB_0_STREAM_BLOB,
  MAIN_MIRROR_STREAM,
  MAIN_MIRROR_STREAM_BLOB,
  SUB_0_MIRROR_STREAM,
  SUB_0_MIRROR_STREAM_BLOB,
  MAX_STREAM,
  ERROR_STREAM
};
 
 
// Platform setting.
#if defined __A50__
#define MAX_NUM_OF_STREAMS    1
 
#define USE_CSI_VIN_DRIVER
#define USE_ISP
 
#define MAX_BUFFER_NUM         3
#define MAX_BUFFER_CSI_RESERVE 2
#define PICTURE_BUFFER_NUM     1
 
#define V4L2_PIX_FMT_DEFAULT V4L2_PIX_FMT_NV21
 
// Platform setting.
#elif defined __A100__
#define MAX_NUM_OF_STREAMS    1
 
#define USE_CSI_VIN_DRIVER
#define USE_ISP
 
#define MAX_BUFFER_NUM         3
#define MAX_BUFFER_CSI_RESERVE 2
#define PICTURE_BUFFER_NUM     1
 
#define V4L2_PIX_FMT_DEFAULT V4L2_PIX_FMT_NV21
 
// Platform setting.
#elif defined __T3__
#define MAX_NUM_OF_STREAMS    1
 
#define USE_CSI_VIN_DRIVER
#define USE_ISP
 
#define MAX_BUFFER_NUM         3
#define MAX_BUFFER_CSI_RESERVE 2
#define PICTURE_BUFFER_NUM     1
 
#define V4L2_PIX_FMT_DEFAULT V4L2_PIX_FMT_NV21
 
#elif defined __A63__
#define MAX_NUM_OF_STREAMS    1
 
#define USE_CSI_VIN_DRIVER
 
#define MAX_BUFFER_NUM         3
#define MAX_BUFFER_CSI_RESERVE 2
#define PICTURE_BUFFER_NUM     1
 
#define V4L2_PIX_FMT_DEFAULT V4L2_PIX_FMT_NV21
 
// universal implement.
#else
#define MAX_NUM_OF_STREAMS    1
 
#define USE_CSI_VIN_DRIVER
#define USE_ISP
 
#define MAX_BUFFER_NUM         3
#define MAX_BUFFER_CSI_RESERVE 2
#define PICTURE_BUFFER_NUM     1
 
#define V4L2_PIX_FMT_DEFAULT V4L2_PIX_FMT_NV21
 
#endif
 
#ifdef USE_CSI_VIN_DRIVER
#define V4L2_CAPTURE_TYPE V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
#else
#define V4L2_CAPTURE_TYPE V4L2_BUF_TYPE_VIDEO_CAPTURE
#endif
 
// Tools for save buffers.
#define DBG_BUFFER_SAVE            0
#define DBG_BUFFER_SAVE_ONE_FRAME  1
#define DBG_BUFFER_SAVE_MORE_FRAME 0
 
#define PATH "/data/camera/"
extern void * buffers_addr[MAX_BUFFER_NUM];
extern bool saveBuffers(char *str,
                        void *p,
                        unsigned int length,
                        bool is_oneframe);
 
extern bool saveBuffers(const char *path,
                        void *y_addr,
                        void *cr_addr,
                        unsigned int y_size,
                        unsigned int cr_size,
                        bool is_oneframe);
 
extern bool saveSizes(int width, int height);
extern int getSingleCameraId();
extern int getSupportCameraId(int cameraId);
 
typedef uint8_t byte;
typedef int32_t int32;
typedef int64_t int64;
 
#define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
 
extern std::map<int, std::string> v4l2_ioctl_map;
extern std::map<int, std::string> hal_pixel_format_map;
extern std::map<int, std::string> v4l2_pixel_format_map;
extern std::map<int, std::string> gralloc_usage_map;
 
extern std::string getV4l2IoctlString(int v4l2_ioctl_request);
extern std::string getHalPixelFormatString(int hal_pixel_format);
extern std::string getV4l2PixelFormatString(int v4l2_pixel_format);
extern std::string getGrallocUsageString(int usage);
#endif  // V4L2_CAMERA_HAL_COMMON_H_