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
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
 
/*
 ******************************************************************************
 *
 * isp_dev.c
 *
 * Hawkview ISP - isp_dev.c module
 *
 * Copyright (c) 2016 by Allwinnertech Co., Ltd.  http://www.allwinnertech.com
 *
 * Version          Author         Date            Description
 *
 *   3.0          Yang Feng       2016/05/11    VIDEO INPUT
 *
 *****************************************************************************
 */
 
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 
#include "video/video_priv.h"
#include "isp_dev_priv.h"
#include "../include/device/isp_dev.h"
#include "isp_v4l2_helper.h"
#include "isp_subdev.h"
#include "../include/isp_ini_parse.h"
 
#define MEDIA_DEVICE "/dev/media0"
#define ENTITY_SUNXI_VIDEO "vin_video"
 
unsigned int isp_dev_log_param = 0;
 
int isp_dev_open(struct hw_isp_media_dev *isp_md, int id)
{
   int ret = -1;
   struct hw_isp_device *isp;
   struct media_entity *entity = NULL;
 
   if (id >= HW_ISP_DEVICE_NUM)
       return ret;
   if (NULL != isp_md->isp_dev[id])
       return 0;
 
   isp = malloc(sizeof(struct hw_isp_device));
   if (isp == NULL)
       goto error;
   memset(isp, 0, sizeof(*isp));
 
   isp->id = id;
   hw_isp_set_priv_data(isp, isp_md);
 
   ret = isp_stats_init(isp);
   if (ret < 0) {
       ISP_ERR("unable to initialize statistics engine.\n");
       goto free_isp;
   }
 
   ret = isp_subdev_init(isp);
   if (ret < 0) {
       ISP_ERR("unable to initialize isp subdev.\n");
       goto free_isp;
   }
 
   entity = media_pipeline_get_head(&isp->isp_sd.entity);
   if(entity) {
       ISP_PRINT("register sensor entity is %s\n", entity->info.name);
       isp->sensor.entity = *entity;
       ret = v4l2_subdev_open(&isp->sensor.entity);
       if (ret < 0) {
           ISP_ERR("unable to open sensor subdev.\n");
           goto free_isp;
       }
   } else {
       ISP_ERR("unable to get sensor subdev.\n");
       goto free_isp;
   }
 
   ISP_PRINT("open isp device[%d] success!\n", id);
   isp_md->isp_dev[id] = isp;
   return 0;
free_isp:
   free(isp);
error:
   isp_md->isp_dev[id] = NULL;
   ISP_ERR("unable to open isp device[%d]\n", id);
   return -1;
}
 
void isp_dev_close(struct hw_isp_media_dev *isp_md, int id)
{
   if (isp_md == NULL)
       return;
 
   if (isp_md->isp_dev[id] == NULL)
       return;
 
   isp_stats_exit(isp_md->isp_dev[id]);
   isp_subdev_exit(isp_md->isp_dev[id]);
   v4l2_subdev_close(&isp_md->isp_dev[id]->sensor.entity);
   free(isp_md->isp_dev[id]);
   isp_md->isp_dev[id] = NULL;
}
 
/*
 * make sure that, it is only one media device
 */
struct hw_isp_media_dev *isp_md_open(const char *devname)
{
   struct hw_isp_media_dev *isp_md;
 
   isp_md = malloc(sizeof *isp_md);
   if (isp_md == NULL)
       return NULL;
   if((access(devname, F_OK) != 0)) {
       printf("warning: mknod media device %s c 253 0\n", devname);
       system("mknod /dev/media0 c 253 0");
   }
 
   memset(isp_md, 0, sizeof *isp_md);
   isp_md->mdev = media_open(devname, 0);
   if (isp_md->mdev == NULL) {
       ISP_ERR("error: unable to open isp_md device %s\n", devname);
       return NULL;
   }
 
   //ISP_PRINT("media open at the first time!!!\n");
 
   return isp_md;
}
 
void isp_md_close(struct hw_isp_media_dev *isp_md)
{
   if (isp_md == NULL)
       return;
 
   media_close(isp_md->mdev);
   free(isp_md);
}
 
int isp_video_open(struct hw_isp_media_dev *isp_md, unsigned int id)
{
   int ret = -1;
   struct isp_video_device *video;
 
   if (id >= HW_VIDEO_DEVICE_NUM)
       return ret;
   if (NULL != isp_md->video_dev[id])
       return 0;
 
   video = malloc(sizeof(struct isp_video_device));
   if (video == NULL)
       goto error;
   memset(video, 0, sizeof(*video));
 
   video->id = id;
 
   video_set_priv_data(video, isp_md);
 
   ret = video_init(video);
   if (ret < 0) {
       ISP_ERR("error: unable to initialize video device.\n");
       free(video);
       goto error;
   }
 
   video->isp_id = media_video_to_isp_id(video->entity);
   if (video->isp_id < 0) {
       ISP_ERR("error: unable to initialize video device.\n");
       video_cleanup(video);
       free(video);
       goto error;
   }
 
   ISP_PRINT("open video device[%d] success!\n", id);
   isp_md->video_dev[id] = video;
   return 0;
error:
   isp_md->video_dev[id] = NULL;
   ISP_ERR("unable to open video device[%d]!\n", id);
   return ret;
}
 
int isp_get_isp_id(int video_id)
{
   struct isp_video_device *video;
   struct hw_isp_media_dev *media_dev_isp = NULL;
   char name[32];
   int ret = -1;
   int isp_id = 0;
 
   if(media_dev_isp!=NULL)
   {
       ISP_PRINT("mpi_vi already init\n");
       return 0;
   }
   media_dev_isp = isp_md_open(MEDIA_DEVICE);
 
   if (media_dev_isp == NULL) {
       ISP_PRINT("unable to open media device %s\n", MEDIA_DEVICE);
       return -1;
   }
 
   if (video_id >= HW_VIDEO_DEVICE_NUM) {
       free(media_dev_isp);
       return ret;
   }
   if (NULL != media_dev_isp->video_dev[video_id]) {
       free(media_dev_isp);
       return 0;
   }
 
   video = malloc(sizeof(struct isp_video_device));
   if (video == NULL) {
       free(media_dev_isp);
       return -1;
   }
   memset(video, 0, sizeof(*video));
 
   video->id = video_id;
   video_set_priv_data(video, media_dev_isp);
 
   snprintf(name, sizeof(name), ENTITY_SUNXI_VIDEO"%u", video->id);
   ISP_PRINT("video device name is %s\n", name);
   video->entity = media_get_entity_by_name(media_dev_isp->mdev, name);
   if (video->entity == NULL) {
       ISP_ERR("can not get entity by name %s\n", name);
       return -ENOENT;
   }
   isp_id = media_video_to_isp_id(video->entity);
 
   isp_md_close(media_dev_isp);
   free(video);
 
   return isp_id;
}
void isp_video_close(struct hw_isp_media_dev *isp_md, unsigned int id)
{
   if (isp_md == NULL)
       return;
 
   if (isp_md->video_dev[id]) {
       video_cleanup(isp_md->video_dev[id]);
       free(isp_md->video_dev[id]);
   }
   isp_md->video_dev[id] = NULL;
}
 
int isp_dev_start(struct hw_isp_device *isp)
{
   /* Start the isp lib. */
   isp_subdev_start(isp);
 
   /* Start the statistics engine. */
   isp_stats_start(isp);
 
   return 0;
}
 
int isp_dev_stop(struct hw_isp_device *isp)
{
   isp_subdev_stop(isp);
   isp_stats_stop(isp);
 
   return 0;
}
 
void isp_dev_register(struct hw_isp_device *isp,
               const struct isp_dev_operations *ops)
{
   isp->ops = ops;
}
 
void isp_dev_banding_ctx(struct hw_isp_device *isp, void *ctx)
{
   isp->ctx = ctx;
}
 
void isp_dev_unbanding_ctx(struct hw_isp_device *isp)
{
   isp->ctx = NULL;
}
 
void *isp_dev_get_ctx(struct hw_isp_device *isp)
{
   if (!isp->ctx) {
       ISP_ERR("ISP[%d] CTX is NULL.\n", isp->id);
       return NULL;
   }
   return isp->ctx;
}
 
void isp_dev_banding_tuning(struct hw_isp_device *isp, void *tuning)
{
   isp->tuning = tuning;
}
 
void isp_dev_unbanding_tuning(struct hw_isp_device *isp)
{
   isp->tuning = NULL;
}
 
void *isp_dev_get_tuning(struct hw_isp_device *isp)
{
   if (NULL == isp->tuning) {
       ISP_ERR("ISP[%d] tuning is NULL.\n", isp->id);
       return NULL;
   }
 
   return isp->tuning;
}
 
char *isp_dev_get_sensor_name(struct hw_isp_device *isp)
{
   if (NULL == isp) {
       ISP_ERR("ISP device is NULL.\n");
       return NULL;
   }
 
   return isp->sensor.entity.info.name;
}
 
int isp_dev_get_dev_id(struct hw_isp_device *isp)
{
   if (NULL == isp) {
       ISP_ERR("ISP device is NULL.\n");
       return -1;
   }
 
   return isp->id;
}