forked from mnakada/atomcam_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_callback.c
430 lines (363 loc) · 12.4 KB
/
video_callback.c
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <pthread.h>
// contrast of image effect.
extern int IMP_ISP_Tuning_SetContrast(unsigned char contrast);
extern int IMP_ISP_Tuning_GetContrast(unsigned char *pcontrast);
// brightness of image effect.
extern int IMP_ISP_Tuning_SetBrightness(unsigned char bright);
extern int IMP_ISP_Tuning_GetBrightness(unsigned char *pbright);
// saturation of image effect.
extern int IMP_ISP_Tuning_SetSaturation(unsigned char sat);
extern int IMP_ISP_Tuning_GetSaturation(unsigned char *psat);
// sharpness of image effect.
extern int IMP_ISP_Tuning_SetSharpness(unsigned char sharpness);
extern int IMP_ISP_Tuning_GetSharpness(unsigned char *psharpness);
// AE compensation.
// AE compensation parameters can adjust the target of the image AE.
extern int IMP_ISP_Tuning_SetAeComp(int comp);
extern int IMP_ISP_Tuning_GetAeComp(int *comp);
// AE Max parameters.
extern int IMP_ISP_Tuning_SetAe_IT_MAX(unsigned int it_max);
extern int IMP_ISP_Tuning_GetAE_IT_MAX(unsigned int *it_max);
// DPC Strength.
extern int IMP_ISP_Tuning_SetDPC_Strength(unsigned int ratio);
extern int IMP_ISP_Tuning_GetDPC_Strength(unsigned int *ratio);
// DRC Strength.
extern int IMP_ISP_Tuning_SetDRC_Strength(unsigned int ratio);
extern int IMP_ISP_Tuning_GetDRC_Strength(unsigned int *ratio);
// highlight intensity controls.
extern int IMP_ISP_Tuning_SetHiLightDepress(uint32_t strength);
extern int IMP_ISP_Tuning_GetHiLightDepress(uint32_t *strength);
// Set 3D noise reduction intensity.
extern int IMP_ISP_Tuning_SetTemperStrength(uint32_t ratio);
// Set 2D noise reduction intensity.
extern int IMP_ISP_Tuning_SetSinterStrength(uint32_t ratio);
// Max value of sensor analog gain.
extern int IMP_ISP_Tuning_SetMaxAgain(uint32_t gain);
extern int IMP_ISP_Tuning_GetMaxAgain(uint32_t *gain);
// Max value of sensor Digital gain.
extern int IMP_ISP_Tuning_SetMaxDgain(uint32_t gain);
extern int IMP_ISP_Tuning_GetMaxDgain(uint32_t *gain);
// ISP image mirror(horizontal) effect function (enable/disable)
extern int IMP_ISP_Tuning_SetISPHflip(int mode);
extern int IMP_ISP_Tuning_GetISPHflip(int *pmode);
// ISP image mirror(vertical) effect function (enable/disable)
extern int IMP_ISP_Tuning_SetISPVflip(int mode);
extern int IMP_ISP_Tuning_GetISPVflip(int *pmode);
struct frames_st {
unsigned char *buf;
size_t length;
};
typedef int (* framecb)(struct frames_st *);
static int (*real_local_sdk_video_set_encode_frame_callback)(int ch, void *callback);
static int video0_encode_capture(struct frames_st *frames);
static int video1_encode_capture(struct frames_st *frames);
static int video2_encode_capture(struct frames_st *frames);
struct video_capture_st {
framecb capture;
int width;
int height;
const char *device;
unsigned int format;
framecb callback;
int enable;
int initialized;
int fd;
};
static struct video_capture_st video_capture[] = {
{
.capture = video0_encode_capture,
.width = 1920,
.height = 1080,
.device = "/dev/video0",
.format = V4L2_PIX_FMT_H264,
.callback = NULL,
.enable = 0,
.initialized = 0,
.fd = -1,
},
{
.capture = video1_encode_capture,
.width = 640,
.height = 360,
.device = "/dev/video1",
.format = V4L2_PIX_FMT_HEVC,
.callback = NULL,
.enable = 0,
.initialized = 0,
.fd = -1,
},
};
static int MainVideoCh = 0;
static char videoResBuf[256];
static char *Flip(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) {
int vflip, hflip;
IMP_ISP_Tuning_GetISPHflip(&hflip);
IMP_ISP_Tuning_GetISPVflip(&vflip);
if(!hflip && !vflip) return "normal";
if(hflip && !vflip) return "flip";
if(!hflip && vflip) return "mirror";
return "flip_mirror";
}
if(!strcmp(p, "normal")) {
IMP_ISP_Tuning_SetISPVflip(0);
IMP_ISP_Tuning_SetISPHflip(0);
} else if(!strcmp(p, "flip")) {
IMP_ISP_Tuning_SetISPVflip(1);
IMP_ISP_Tuning_SetISPHflip(0);
} else if(!strcmp(p, "mirror")) {
IMP_ISP_Tuning_SetISPVflip(0);
IMP_ISP_Tuning_SetISPHflip(1);
} else if(!strcmp(p, "flip_mirror")) {
IMP_ISP_Tuning_SetISPVflip(1);
IMP_ISP_Tuning_SetISPHflip(1);
} else {
return "error";
}
return "ok";
}
static char *Contrast(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) {
unsigned char cont;
IMP_ISP_Tuning_GetContrast(&cont);
sprintf(videoResBuf, "%d\n", cont);
return videoResBuf;
}
int res = IMP_ISP_Tuning_SetContrast(atoi(p));
return res ? "error": "ok";
}
static char *Brightness(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) {
unsigned char bri;
IMP_ISP_Tuning_GetBrightness(&bri);
sprintf(videoResBuf, "%d\n", bri);
return videoResBuf;
}
int res = IMP_ISP_Tuning_SetBrightness(atoi(p));
return res ? "error": "ok";
}
static char *Saturation(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) {
unsigned char sat;
IMP_ISP_Tuning_GetSaturation(&sat);
sprintf(videoResBuf, "%d\n", sat);
return videoResBuf;
}
int res = IMP_ISP_Tuning_SetSaturation(atoi(p));
return res ? "error": "ok";
}
static char *Sharpness(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) {
unsigned char sharpness;
IMP_ISP_Tuning_GetSharpness(&sharpness);
sprintf(videoResBuf, "%d\n", sharpness);
return videoResBuf;
}
int res = IMP_ISP_Tuning_SetSharpness(atoi(p));
return res ? "error": "ok";
}
static char *AEComp(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) {
int comp;
IMP_ISP_Tuning_GetAeComp(&comp);
sprintf(videoResBuf, "%d\n", comp);
return videoResBuf;
}
int res = IMP_ISP_Tuning_SetAeComp(atoi(p));
return res ? "error": "ok";
}
static char *AEItMax(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) {
unsigned int itMax;
IMP_ISP_Tuning_GetAE_IT_MAX(&itMax);
sprintf(videoResBuf, "%d\n", itMax);
return videoResBuf;
}
int res = IMP_ISP_Tuning_SetAe_IT_MAX(atoi(p));
return res ? "error": "ok";
}
static char *Sinter(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) return "error";
int res = IMP_ISP_Tuning_SetSinterStrength(atoi(p));
return res ? "error": "ok";
}
static char *Temper(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) return "error";
int res = IMP_ISP_Tuning_SetTemperStrength(atoi(p));
return res ? "error": "ok";
}
static char *DPC(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) {
unsigned int dpc;
IMP_ISP_Tuning_GetDPC_Strength(&dpc);
sprintf(videoResBuf, "%d\n", dpc);
return videoResBuf;
}
int res = IMP_ISP_Tuning_SetDPC_Strength(atoi(p));
return res ? "error": "ok";
}
static char *DRC(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) {
unsigned int drc;
IMP_ISP_Tuning_GetDRC_Strength(&drc);
sprintf(videoResBuf, "%d\n", drc);
return videoResBuf;
}
int res = IMP_ISP_Tuning_SetDRC_Strength(atoi(p));
return res ? "error": "ok";
}
static char *HiLight(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) {
unsigned int strength;
IMP_ISP_Tuning_GetHiLightDepress(&strength);
sprintf(videoResBuf, "%d\n", strength);
return videoResBuf;
}
int res = IMP_ISP_Tuning_SetHiLightDepress(atoi(p));
return res ? "error": "ok";
}
static char *AGain(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) {
unsigned int gain;
IMP_ISP_Tuning_GetMaxAgain(&gain);
sprintf(videoResBuf, "%d\n", gain);
return videoResBuf;
}
int res = IMP_ISP_Tuning_SetMaxAgain(atoi(p));
return res ? "error": "ok";
}
static char *DGain(char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) {
unsigned int gain;
IMP_ISP_Tuning_GetMaxDgain(&gain);
sprintf(videoResBuf, "%d\n", gain);
return videoResBuf;
}
int res = IMP_ISP_Tuning_SetMaxDgain(atoi(p));
return res ? "error": "ok";
}
struct CommandTableSt {
const char *cmd;
char * (*func)(char *);
};
static struct CommandTableSt VideoCommandTable[] = {
{ "flip", &Flip }, // flip [normal/flip/mirror/flip_mirror]
{ "cont", &Contrast }, // cont 0 - 255(center:128)
{ "bri", &Brightness }, // bri 0 - 255(center:128)
{ "sat", &Saturation }, // sat 0 - 255(center:128)
{ "sharp", &Sharpness }, // sharp 0 - 255(center:128)
{ "sinter", &Sinter }, // sinter 0 - 255(center:128)
{ "temper", &Temper }, // temper 0 - 255(center:128)
{ "aecomp", &AEComp }, // aecomp 0 - 255
{ "aeitmax", &AEItMax }, // aeitmax 0-
{ "dpc", &DPC }, // dpc 0 - 255
{ "drc", &DRC }, // drc 0 - 255
{ "hilight", &HiLight }, // hilight 0 - 10
{ "again", &AGain }, // again 0 -
{ "dgain", &DGain }, // dgain 0 -
};
char *VideoCapture(int fd, char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(p) {
for(int i = 0; i < sizeof(VideoCommandTable) / sizeof(struct CommandTableSt); i++) {
if(!strcmp(p, VideoCommandTable[i].cmd)) return (*VideoCommandTable[i].func)(tokenPtr);
}
}
int ch = 0;
if(p && (!strcmp(p, "0") || !strcmp(p, "1"))) {
ch = atoi(p);
p = strtok_r(NULL, " \t\r\n", &tokenPtr);
}
if(!p) return video_capture[ch].enable ? "on" : "off";
if(!strcmp(p, "on")) {
video_capture[ch].enable = 1;
printf("[command] video %d capute on\n", ch);
return "ok";
}
if(!strcmp(p, "off")) {
video_capture[ch].enable = 0;
printf("[command] video %d capute off\n", ch);
return "ok";
}
return "error";
}
static int video_encode_capture(int ch, struct frames_st *frames) {
if(!video_capture[ch].initialized) {
video_capture[ch].initialized = 1;
int err;
video_capture[ch].fd = open(video_capture[ch].device, O_WRONLY, 0777);
if(video_capture[ch].fd < 0) fprintf(stderr, "Failed to open V4L2 device: %s\n", video_capture[ch].device);
struct v4l2_format vid_format;
memset(&vid_format, 0, sizeof(vid_format));
vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
vid_format.fmt.pix.width = video_capture[ch].width;
vid_format.fmt.pix.height = video_capture[ch].height;
vid_format.fmt.pix.pixelformat = video_capture[ch].format;
vid_format.fmt.pix.sizeimage = 0;
vid_format.fmt.pix.field = V4L2_FIELD_NONE;
vid_format.fmt.pix.bytesperline = 0;
vid_format.fmt.pix.colorspace = V4L2_PIX_FMT_YUV420;
err = ioctl(video_capture[ch].fd, VIDIOC_S_FMT, &vid_format);
if(err < 0) fprintf(stderr, "Unable to set V4L2 %s format: %d\n", video_capture[ch].device, err);
err = ioctl(video_capture[ch].fd, VIDIOC_STREAMON, &vid_format);
if(err < 0) fprintf(stderr, "Unable to perform VIDIOC_STREAMON %s: %d\n", video_capture[ch].device, err);
}
if((video_capture[ch].fd >= 0) && video_capture[ch].enable) {
int size = write(video_capture[ch].fd, frames->buf, frames->length);
if(size != frames->length) fprintf(stderr,"Stream write error %s: %s\n", video_capture[ch].device, size);
}
return (video_capture[ch].callback)(frames);
}
static int video0_encode_capture(struct frames_st *frames) {
return video_encode_capture(0, frames);
}
static int video1_encode_capture(struct frames_st *frames) {
return video_encode_capture(1, frames);
}
static int video2_encode_capture(struct frames_st *frames) {
return video_encode_capture(2, frames);
}
int local_sdk_video_set_encode_frame_callback(int sch, void *callback) {
fprintf(stderr, "local_sdk_video_set_encode_frame_callback streamChId=%d, callback=0x%x\n", sch, callback);
int ch = sch;
if((ch == MainVideoCh) || (ch == 1)) {
if(ch == MainVideoCh) ch = 0;
video_capture[ch].callback = callback;
fprintf(stderr,"enc func injection save video_encode_cb=0x%x\n", video_capture[ch].callback);
callback = video_capture[ch].capture;
}
return real_local_sdk_video_set_encode_frame_callback(sch, callback);
}
static void __attribute ((constructor)) video_callback_init(void) {
char *p = getenv("RTSP_MAIN_FORMAT_HEVC");
if(p && !strcmp(p, "on")) {
MainVideoCh = 3;
video_capture[0].format = V4L2_PIX_FMT_HEVC;
}
real_local_sdk_video_set_encode_frame_callback = dlsym(dlopen("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_video_set_encode_frame_callback");
}