This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.c
177 lines (141 loc) · 5.41 KB
/
video.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
#include <ti/sdo/dmai/Dmai.h>
#include <ti/sdo/dmai/Fifo.h>
#include <ti/sdo/dmai/BufTab.h>
#include <ti/sdo/dmai/BufferGfx.h>
#include <ti/sdo/dmai/Rendezvous.h>
#include <ti/sdo/dmai/ce/Venc1.h>
#include <ti/sdo/codecs/h264enc/ih264venc.h>
#include "video.h"
#include "main.h"
#define NUM_VIDEO_BUFS 4
Void *videoThrFxn(Void *arg)
{
VideoEnv *envp = (VideoEnv *) arg;
Venc1_Handle hVe1 = NULL;
VIDENC1_Params params = Venc1_Params_DEFAULT;
VIDENC1_DynamicParams dynParams = Venc1_DynamicParams_DEFAULT;
IH264VENC_Params h264Params = IH264VENC_PARAMS;
IH264VENC_DynamicParams h264DynParams = H264VENC_TI_IH264VENC_DYNAMICPARAMS;
VUIParamBuffer VUI_Buffer = H264VENC_TI_VUIPARAMBUFFER;
BufTab_Handle hVidBufTab = NULL;
Buffer_Handle hVInBuf, hWOutBuf;
BufferGfx_Attrs gfxAttrs = BufferGfx_Attrs_DEFAULT;
ColorSpace_Type colorSpace = ColorSpace_YUV420PSEMI;
Int bufSize = 0;
Void *status = THREAD_SUCCESS;
/* Initialization */
params.maxWidth = envp->imageWidth;
params.maxHeight = envp->imageHeight;
params.inputChromaFormat = XDM_YUV_420SP;
params.reconChromaFormat = XDM_YUV_420SP;
params.maxFrameRate = envp->videoFrameRate;
params.encodingPreset = XDM_USER_DEFINED;
params.rateControlPreset = IVIDEO_USER_DEFINED;
params.maxBitRate = 10000000;
dynParams.targetBitRate = envp->videoBitRate*0.9;
dynParams.inputWidth = envp->imageWidth;
dynParams.captureWidth = Dmai_roundUp(BufferGfx_calcLineLength(envp->imageWidth, colorSpace), 32);
dynParams.inputHeight = envp->imageHeight;
dynParams.refFrameRate = params.maxFrameRate;
dynParams.targetFrameRate = params.maxFrameRate;
dynParams.intraFrameInterval = 0;
dynParams.interFrameInterval = 0;
h264Params.videncParams = params;
h264Params.videncParams.size = sizeof(IH264VENC_Params);
h264Params.encQuality = 1;
h264Params.enableDDRbuff = 1; /* Uses DDR instead of VICP buffers */
h264Params.enableARM926Tcm = 0;
h264Params.enableVUIparams = (0x1 << 1);
h264Params.videncParams.inputContentType = IVIDEO_PROGRESSIVE;
h264DynParams.videncDynamicParams = dynParams;
h264DynParams.videncDynamicParams.size = sizeof(IH264VENC_DynamicParams);
h264DynParams.VUI_Buffer = &VUI_Buffer;
h264DynParams.VUI_Buffer->aspectRatioInfoPresentFlag = 1;
h264DynParams.VUI_Buffer->overscanInfoPresentFlag = 0;
h264DynParams.VUI_Buffer->videoSignalTypePresentFlag = 0;
h264DynParams.VUI_Buffer->timingInfoPresentFlag = 1;
h264DynParams.VUI_Buffer->numUnitsInTicks = 1;
h264DynParams.VUI_Buffer->timeScale = params.maxFrameRate / 1000;
h264DynParams.VUI_Buffer->fixedFrameRateFlag = 1;
h264DynParams.VUI_Buffer->nalHrdParameterspresentFlag = 1;
h264DynParams.VUI_Buffer->picStructPresentFlag = 1;
h264DynParams.idrFrameInterval = 15;
hVe1 = Venc1_create(envp->hEngine, envp->videoEncoder,
(IVIDENC1_Params *) &h264Params,
(IVIDENC1_DynamicParams *) &h264DynParams);
if (hVe1 == NULL) {
ERR("Failed to create video encoder: %s\n", envp->videoEncoder);
cleanup(THREAD_FAILURE);
}
/* Store the output buffer size in the environment */
envp->outBufSize = Venc1_getOutBufSize(hVe1);
/* Signal that the codec is created and output buffer size available */
Rendezvous_meet(envp->hRendezvousWriter);
/* Video BufTab create */
BufferGfx_calcDimensions(VideoStd_D1_PAL, colorSpace, &gfxAttrs.dim);
gfxAttrs.dim.width = 704;
gfxAttrs.dim.height = 576;
gfxAttrs.dim.lineLength = Dmai_roundUp(BufferGfx_calcLineLength(gfxAttrs.dim.width, colorSpace), 32);
gfxAttrs.colorSpace = colorSpace;
bufSize = gfxAttrs.dim.lineLength * gfxAttrs.dim.height * 3 / 2;
hVidBufTab = BufTab_create(NUM_VIDEO_BUFS, bufSize, BufferGfx_getBufferAttrs(&gfxAttrs));
if (hVidBufTab == NULL) {
ERR("Failed to create video buftab\n");
cleanup(THREAD_FAILURE);
}
/* Set input buffer table */
Venc1_setBufTab(hVe1, hVidBufTab);
/* Send video buffers to DEI */
Int nBufId = 0;
for (nBufId = 0; nBufId < NUM_VIDEO_BUFS; nBufId++) {
hVInBuf = BufTab_getBuf(hVidBufTab, nBufId);
if (Fifo_put(envp->hVideoOutFifo, hVInBuf) < 0) {
ERR("Failed to send buffer to dei thread\n");
cleanup(THREAD_FAILURE);
}
}
/* Signal that initialization is done and wait for other threads */
Rendezvous_meet(envp->hRendezvousInit);
while(1) {
/* Get buffer from DEI thread */
if(Fifo_get(envp->hVideoInFifo, &hVInBuf) < 0) {
ERR("Failed to get buffer from dei thread\n");
cleanup(THREAD_FAILURE);
}
/* Get buffer from Writer thread */
if(Fifo_get(envp->hWriterOutFifo, &hWOutBuf) < 0) {
ERR("Failed to get buffer from writer thread\n");
cleanup(THREAD_FAILURE);
}
/* Make sure the whole buffer is used for input */
BufferGfx_resetDimensions(hVInBuf);
/* Encode */
if (Venc1_process(hVe1, hVInBuf, hWOutBuf) < 0) {
ERR("Failed to encode video buffer\n");
cleanup(THREAD_FAILURE);
}
/* Put buffer to dei thread */
if (Fifo_put(envp->hVideoOutFifo, hVInBuf) < 0) {
ERR("Failed to send buffer to dei thread\n");
cleanup(THREAD_FAILURE);
}
/* Put buffer to writer thread */
if (Fifo_put(envp->hWriterInFifo, hWOutBuf) < 0) {
ERR("Failed to send buffer to dei thread\n");
cleanup(THREAD_FAILURE);
}
}
cleanup:
/* Make sure the other threads aren't waiting for us */
Rendezvous_force(envp->hRendezvousInit);
Rendezvous_force(envp->hRendezvousWriter);
/* Make sure the other threads aren't waiting for init to complete */
Rendezvous_meet(envp->hRendezvousCleanup);
if (hVidBufTab) {
BufTab_delete(hVidBufTab);
}
if (hVe1) {
Venc1_delete(hVe1);
}
return status;
}