forked from feihengli/util
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsal_jpeg.c
371 lines (288 loc) · 11.3 KB
/
sal_jpeg.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
#include "sal_av.h"
#include "sal_jpeg.h"
#include "sal_debug.h"
#include "hi_comm.h"
#include "sal_config.h"
#define JPEG_LOCAL_TEST 0
typedef struct sal_jpeg_args
{
int channel;
int widht;
int height;
int fps;
sal_jpeg_cb cb;
int running;
pthread_t tid;
pthread_mutex_t mutex;
}sal_jpeg_args;
static sal_jpeg_args* g_jpeg_args = NULL;
static HI_S32 jpeg_create_chn()
{
HI_S32 s32Ret = HI_FAILURE;
VENC_CHN_ATTR_S stVencChnAttr;
memset(&stVencChnAttr, 0, sizeof(stVencChnAttr));
VENC_ATTR_JPEG_S stJpegAttr;
memset(&stJpegAttr, 0, sizeof(stJpegAttr));
stVencChnAttr.stVeAttr.enType = PT_JPEG;
stJpegAttr.u32MaxPicWidth = g_jpeg_args->widht;
stJpegAttr.u32MaxPicHeight = g_jpeg_args->height;
stJpegAttr.u32PicWidth = g_jpeg_args->widht;
stJpegAttr.u32PicHeight = g_jpeg_args->height;
stJpegAttr.u32BufSize = g_jpeg_args->widht*g_jpeg_args->height*3/4;
stJpegAttr.bByFrame = HI_TRUE;
stJpegAttr.bSupportDCF = HI_FALSE;
memcpy(&stVencChnAttr.stVeAttr.stAttrJpeg, &stJpegAttr, sizeof(VENC_ATTR_JPEG_S));
s32Ret = HI_MPI_VENC_CreateChn(g_jpeg_args->channel, &stVencChnAttr);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
return HI_SUCCESS;
}
static int jpeg_destroy_chn()
{
HI_S32 s32Ret;
s32Ret = HI_MPI_VENC_DestroyChn(g_jpeg_args->channel);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
return HI_SUCCESS;
}
static HI_S32 jpeg_venc_bind_vpss()
{
HI_S32 s32Ret = HI_SUCCESS;
MPP_CHN_S stSrcChn;
MPP_CHN_S stDestChn;
stSrcChn.enModId = HI_ID_VPSS;
stSrcChn.s32DevId = 0;
stSrcChn.s32ChnId = g_jpeg_args->channel;
stDestChn.enModId = HI_ID_VENC;
stDestChn.s32DevId = 0;
stDestChn.s32ChnId = g_jpeg_args->channel;
s32Ret = HI_MPI_SYS_Bind(&stSrcChn, &stDestChn);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
return s32Ret;
}
static int jpeg_venc_unbind_vpss()
{
HI_S32 s32Ret = HI_SUCCESS;
MPP_CHN_S stSrcChn;
MPP_CHN_S stDestChn;
stSrcChn.enModId = HI_ID_VPSS;
stSrcChn.s32DevId = 0;
stSrcChn.s32ChnId = g_jpeg_args->channel;
stDestChn.enModId = HI_ID_VENC;
stDestChn.s32DevId = 0;
stDestChn.s32ChnId = g_jpeg_args->channel;
s32Ret = HI_MPI_SYS_UnBind(&stSrcChn, &stDestChn);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
return s32Ret;
}
static HI_S32 jpeg_set_Qfactor()
{
HI_S32 s32Ret = HI_FAILURE;
VENC_PARAM_JPEG_S stJpegParam;
memset(&stJpegParam, 0, sizeof(stJpegParam));
s32Ret = HI_MPI_VENC_GetJpegParam(g_jpeg_args->channel, &stJpegParam);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
stJpegParam.u32Qfactor = 50;
DBG("u32Qfactor: %u\n", stJpegParam.u32Qfactor);
s32Ret = HI_MPI_VENC_SetJpegParam(g_jpeg_args->channel, &stJpegParam);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
return HI_SUCCESS;
}
static HI_S32 jpeg_enable_chn()
{
HI_S32 s32Ret = HI_FAILURE;
VPSS_CHN_ATTR_S stVpssChnAttr;
memset(&stVpssChnAttr, 0, sizeof(stVpssChnAttr));
VPSS_CHN_MODE_S stVpssChnMode;
memset(&stVpssChnAttr, 0, sizeof(stVpssChnAttr));
VPSS_GRP VpssGrp = 0;
sal_stream_s stream;
memset(&stream, 0, sizeof(stream));
s32Ret = sal_video_args_get(0, &stream);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
stVpssChnMode.enChnMode = VPSS_CHN_MODE_USER;
stVpssChnMode.bDouble = HI_FALSE;
stVpssChnMode.enPixelFormat = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
stVpssChnMode.u32Width = g_jpeg_args->widht;
stVpssChnMode.u32Height = g_jpeg_args->height;
stVpssChnMode.enCompressMode = COMPRESS_MODE_NONE;
stVpssChnAttr.s32SrcFrameRate = stream.framerate; // vin framerate
stVpssChnAttr.s32DstFrameRate = (g_jpeg_args->fps > stream.framerate) ? stream.framerate : g_jpeg_args->fps;
s32Ret = HI_MPI_VPSS_SetChnAttr(VpssGrp, g_jpeg_args->channel, &stVpssChnAttr);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
ROTATE_E enRotate = ROTATE_NONE;
s32Ret = HI_MPI_VPSS_GetRotate(0, 0, &enRotate); //get vpss chn0 rotate attr
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
if (enRotate == ROTATE_90 || enRotate == ROTATE_270)
{
HI_U32 temp = stVpssChnMode.u32Width;
stVpssChnMode.u32Width = stVpssChnMode.u32Height;
stVpssChnMode.u32Height = temp;
}
s32Ret = HI_MPI_VPSS_SetChnMode(VpssGrp, g_jpeg_args->channel, &stVpssChnMode);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
if (ROTATE_NONE != enRotate && g_jpeg_args->channel < VPSS_MAX_PHY_CHN_NUM)
{
s32Ret = HI_MPI_VPSS_SetRotate(VpssGrp, g_jpeg_args->channel, enRotate);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
}
s32Ret = HI_MPI_VPSS_EnableChn(VpssGrp, g_jpeg_args->channel);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
return HI_SUCCESS;
}
static int jpeg_disable_chn()
{
HI_S32 s32Ret;
s32Ret = HI_MPI_VPSS_DisableChn(0, g_jpeg_args->channel);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
return HI_SUCCESS;
}
static int jpeg_write_frame(char* addr, int size)
{
static FILE* fp = NULL;
static int count = 0;
char name[32] = "";
sprintf(name, "snap_%d.jpg", count++);
if (!access(name, F_OK))
remove(name);
fp = fopen(name, "wb+");
if (fp== NULL)
DBG("failed to open [%s]: %s\n", name, strerror(errno));
if (fp)
{
fwrite(addr, 1, size, fp);
fclose(fp);
}
return 0;
}
static HI_S32 jpeg_get_one_picture()
{
HI_S32 s32Ret = HI_FAILURE;
VENC_RECV_PIC_PARAM_S stRecvParam;
memset(&stRecvParam, 0, sizeof(stRecvParam));
VENC_CHN VencChn = g_jpeg_args->channel;
stRecvParam.s32RecvPicNum = 1;
s32Ret = HI_MPI_VENC_StartRecvPicEx(VencChn, &stRecvParam);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
HI_S32 s32VencFd = HI_MPI_VENC_GetFd(VencChn);
CHECK(s32VencFd > 0, HI_FAILURE, "Error with %#x.\n", s32Ret);
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(s32VencFd, &read_fds);
struct timeval TimeoutVal = {1, 0};
s32Ret = select(s32VencFd + 1, &read_fds, NULL, NULL, &TimeoutVal);
if (s32Ret < 0)
{
DBG("snap select failed!\n");
return HI_FAILURE;
}
else if (0 == s32Ret)
{
DBG("snap time out!\n");
}
else
{
if (FD_ISSET(s32VencFd, &read_fds))
{
VENC_CHN_STAT_S stStat;
s32Ret = HI_MPI_VENC_Query(VencChn, &stStat);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
if (0 == stStat.u32CurPacks)
{
DBG("NOTE: Current frame is NULL!\n");
return HI_SUCCESS;
}
// ʹÓõ¥°üģʽ»ñÈ¡
if (1 == stStat.u32CurPacks)
{
VENC_PACK_S pack;
memset(&pack, 0, sizeof(pack));
VENC_STREAM_S stStream;
memset(&stStream, 0, sizeof(stStream));
stStream.pstPack = &pack;
stStream.u32PackCount = stStat.u32CurPacks;
s32Ret = HI_MPI_VENC_GetStream(VencChn, &stStream, -1);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
HI_U8* frame_addr = stStream.pstPack->pu8Addr + stStream.pstPack->u32Offset;
HI_U32 frame_len = stStream.pstPack->u32Len - stStream.pstPack->u32Offset;
if (g_jpeg_args->cb)
{
g_jpeg_args->cb((char*)frame_addr, frame_len);
}
if (JPEG_LOCAL_TEST)
{
s32Ret = jpeg_write_frame((char*)frame_addr, frame_len);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
}
s32Ret = HI_MPI_VENC_ReleaseStream(VencChn, &stStream);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
}
}
}
s32Ret = HI_MPI_VENC_StopRecvPic(VencChn);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
return HI_SUCCESS;
}
static void* jpeg_get_stream_thr(void *p)
{
prctl(PR_SET_NAME, __FUNCTION__);
HI_S32 s32Ret = HI_FAILURE;
int interval = 1*1000*1000/g_jpeg_args->fps;
while (g_jpeg_args->running)
{
s32Ret = jpeg_get_one_picture();
CHECK(s32Ret == HI_SUCCESS, NULL, "Error with %#x.\n", s32Ret);
usleep(interval);
}
return NULL;
}
int sal_jpeg_init(sal_jpeg_cb cb)
{
CHECK(NULL == g_jpeg_args, HI_FAILURE, "reinit error, please exit first.\n");
HI_S32 s32Ret = HI_FAILURE;
g_jpeg_args = (sal_jpeg_args*)malloc(sizeof(sal_jpeg_args));
CHECK(g_jpeg_args != NULL, HI_FAILURE, "malloc %d bytes failed.\n", sizeof(sal_jpeg_args));
memset(g_jpeg_args, 0, sizeof(sal_jpeg_args));
pthread_mutex_init(&g_jpeg_args->mutex, NULL);
g_jpeg_args->channel = g_config.jpeg.channel;
g_jpeg_args->widht = g_config.jpeg.widht;
g_jpeg_args->height = g_config.jpeg.height;
g_jpeg_args->cb = cb;
g_jpeg_args->fps = 5;
sal_stream_s stream;
memset(&stream, 0, sizeof(stream));
s32Ret = sal_video_args_get(0, &stream);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
CHECK((g_jpeg_args->widht <= stream.width) && (g_jpeg_args->height <= stream.height), HI_FAILURE,
"jpeg[%dx%d] is too big than main stream[%dx%d].\n", g_jpeg_args->widht, g_jpeg_args->height, stream.width, stream.height);
s32Ret = jpeg_create_chn();
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
s32Ret = jpeg_enable_chn();
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
s32Ret = jpeg_set_Qfactor();
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
s32Ret = jpeg_venc_bind_vpss();
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
g_jpeg_args->running = 1;
s32Ret = pthread_create(&g_jpeg_args->tid, NULL, jpeg_get_stream_thr, NULL);
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %s.\n", strerror(errno));
return HI_SUCCESS;
}
int sal_jpeg_exit()
{
CHECK(NULL != g_jpeg_args, HI_FAILURE, "module not inited.\n");
HI_S32 s32Ret = HI_FAILURE;
if (g_jpeg_args->running)
{
g_jpeg_args->running = 0;
pthread_join(g_jpeg_args->tid, NULL);
}
s32Ret = jpeg_venc_unbind_vpss();
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
s32Ret = jpeg_destroy_chn();
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
s32Ret = jpeg_disable_chn();
CHECK(s32Ret == HI_SUCCESS, HI_FAILURE, "Error with %#x.\n", s32Ret);
pthread_mutex_destroy(&g_jpeg_args->mutex);
free(g_jpeg_args);
g_jpeg_args = NULL;
return 0;
}