-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
424 lines (327 loc) · 9.53 KB
/
main.cpp
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
#include "system.h"
#include "main.h"
#include "egl.h"
#include <GLES2/gl2.h>
#define EGL_EGLEXT_PROTOTYPES 1
#include <EGL/eglext.h>
#define GL_GLEXT_PROTOTYPES 1
#include <GLES2/gl2ext.h>
#include <drm/drm_fourcc.h>
#ifdef CLASSNAME
#undef CLASSNAME
#endif
#define CLASSNAME "Main"
void Cleanup() {
if (m_cVideoCodec)
delete m_cVideoCodec;
if (m_cHints)
delete m_cHints;
if (m_pDvdVideoPicture)
delete m_pDvdVideoPicture;
avcodec_close(codecCtx);
av_free(codecCtx);
avformat_close_input(&formatCtx);
}
void intHandler(int dummy=0) {
Cleanup();
exit(0);
}
/************************** EGL ****************************/
void GL_CheckError()
{
int error = glGetError();
if (error != GL_NO_ERROR)
{
printf("eglGetError(): %i (0x%.4x)\n", (int)error, (int)error);
exit(1);
}
}
const char* vertexSource = "\n \
attribute mediump vec4 Attr_Position;\n \
attribute mediump vec2 Attr_TexCoord0;\n \
\n \
uniform mat4 WorldViewProjection;\n \
\n \
varying mediump vec2 TexCoord0;\n \
\n \
void main()\n \
{\n \
\n \
gl_Position = Attr_Position * WorldViewProjection;\n \
TexCoord0 = Attr_TexCoord0;\n \
}\n \
\n \
";
const char* fragmentSource = "\n \
uniform lowp sampler2D DiffuseMap;\n \
\n \
varying mediump vec2 TexCoord0;\n \
\n \
void main()\n \
{\n \
mediump vec4 rgba = texture2D(DiffuseMap, TexCoord0);\n \
\n \
gl_FragColor = rgba;\n \
}\n \
\n \
";
const float quad[] =
{
-1, 1, 0,
-1, -1, 0,
1, -1, 0,
1, -1, 0,
1, 1, 0,
-1, 1, 0
};
const float quadUV[] =
{
0, 0,
0, 1,
1, 1,
1, 1,
1, 0,
0, 0
};
EGLDisplay display;
EGLSurface surface;
void initGL()
{
display = Egl_Initialize();
EGLConfig config;
surface = Egl_CreateWindow(display, &config);
EGLContext context = Egl_CreateContext(display, surface, config);
// Shader
GLuint vertexShader = 0;
GLuint fragmentShader = 0;
for (int i = 0; i < 2; ++i)
{
GLuint shaderType;
const char* sourceCode;
if (i == 0)
{
shaderType = GL_VERTEX_SHADER;
sourceCode = vertexSource;
}
else
{
shaderType = GL_FRAGMENT_SHADER;
sourceCode = fragmentSource;
}
GLuint openGLShaderID = glCreateShader(shaderType);
GL_CheckError();
const char* glSrcCode[1] = { sourceCode };
const int lengths[1] = { -1 }; // Tell OpenGL the string is NULL terminated
glShaderSource(openGLShaderID, 1, glSrcCode, lengths);
GL_CheckError();
glCompileShader(openGLShaderID);
GL_CheckError();
GLint param;
glGetShaderiv(openGLShaderID, GL_COMPILE_STATUS, ¶m);
GL_CheckError();
if (param == GL_FALSE)
{
puts("Shader Compilation Failed.");
exit(-1);
}
if (i == 0)
{
vertexShader = openGLShaderID;
}
else
{
fragmentShader = openGLShaderID;
}
}
// Program
GLuint openGLProgramID = glCreateProgram();
GL_CheckError();
glAttachShader(openGLProgramID, vertexShader);
GL_CheckError();
glAttachShader(openGLProgramID, fragmentShader);
GL_CheckError();
// Bind
glEnableVertexAttribArray(0);
GL_CheckError();
glBindAttribLocation(openGLProgramID, 0, "Attr_Position");
GL_CheckError();
glEnableVertexAttribArray(1);
GL_CheckError();
glBindAttribLocation(openGLProgramID, 1, "Attr_TexCoord0");
GL_CheckError();
glLinkProgram(openGLProgramID);
GL_CheckError();
glUseProgram(openGLProgramID);
GL_CheckError();
// Get program uniform(s)
GLuint wvpUniformLocation = glGetUniformLocation(openGLProgramID, "WorldViewProjection");
GL_CheckError();
if (wvpUniformLocation < 0)
{
printf("wvpUniformLocation failed");
exit(-1);
}
// Set the matrix
static float m[16]={1.0,0.0,0.0,0.0, 0.0,1.0,0.0,0.0, 0.0,0.0,1.0,0.0, 0.0,0.0,0.0,1.0};
glUniformMatrix4fv(wvpUniformLocation, 1, GL_FALSE, m);
GL_CheckError();
// Setup OpenGL
glClearColor(1, 0, 0, 1); // RED for diagnostic use
//glClearColor(0, 0, 0, 0); // Transparent Black
GL_CheckError();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
GL_CheckError();
glEnable(GL_CULL_FACE);
GL_CheckError();
glCullFace(GL_BACK);
GL_CheckError();
glFrontFace(GL_CCW);
GL_CheckError();
}
void EnableTexture(DVDVideoPicture *picture)
{
static GLuint textures[8]={0,0,0,0,0,0,0,0};
if(!textures[picture->iIndex])
{
EGLint img_attrs[] = {
EGL_WIDTH, (EGLint)picture->iWidth,
EGL_HEIGHT, (EGLint)picture->iHeight,
EGL_LINUX_DRM_FOURCC_EXT, DRM_FORMAT_RGBA8888,
EGL_DMA_BUF_PLANE0_FD_EXT, (EGLint)reinterpret_cast<long>(picture->data[0]),
EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
EGL_DMA_BUF_PLANE0_PITCH_EXT, picture->iLineSize[0],
EGL_NONE
};
EGLImageKHR image = eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, 0, img_attrs);
GL_CheckError();
glGenTextures(1, &textures[picture->iIndex]);
GL_CheckError();
glActiveTexture(GL_TEXTURE0);
GL_CheckError();
glBindTexture(GL_TEXTURE_2D, textures[picture->iIndex]);
GL_CheckError();
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
GL_CheckError();
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GL_CheckError();
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
GL_CheckError();
}
// Upload texture data
glActiveTexture(GL_TEXTURE0);
GL_CheckError();
glBindTexture(GL_TEXTURE_2D, textures[picture->iIndex]);
GL_CheckError();
// Set the quad vertex data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * 4, quad);
GL_CheckError();
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * 4, quadUV);
GL_CheckError();
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT |
GL_STENCIL_BUFFER_BIT);
// Draw
glDrawArrays(GL_TRIANGLES, 0, 3 * 2);
GL_CheckError();
eglSwapBuffers(display, surface);
GL_CheckError();
}
/************************** EGL ****************************/
int main(int argc, char** argv) {
m_cVideoCodec = NULL;
m_cHints = NULL;
m_pDvdVideoPicture = NULL;
formatCtx = NULL;
codecCtx = NULL;
codecParameters = NULL;
codec = NULL;
AVPacket packet;
int videoStream = -1;
const char* vidPath;
timespec startTs, endTs;
signal(SIGINT, intHandler);
if (argc > 1)
vidPath = (char *)argv[1];
else
vidPath = (char *)"video";
av_register_all();
if (avformat_open_input(&formatCtx, vidPath, NULL, NULL) != 0) {
CLog::Log(LOGERROR, "%s::%s - avformat_open_input() unable to open: %s", CLASSNAME, __func__, vidPath);
return false;
}
CLog::Log(LOGDEBUG, "%s::%s - video file: %s", CLASSNAME, __func__, vidPath);
if (avformat_find_stream_info(formatCtx, NULL) < 0) {
CLog::Log(LOGERROR, "%s::%s - avformat_find_stream_info() failed.", CLASSNAME, __func__);
return false;
}
for (unsigned int i = 0; i < formatCtx->nb_streams; ++i)
if (formatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
break;
}
if (videoStream == -1) {
CLog::Log(LOGERROR, "%s::%s - Unable to find video stream in the file.", CLASSNAME, __func__);
return false;
}
CLog::Log(LOGDEBUG, "%s::%s - Video stream in the file is stream number %d", CLASSNAME, __func__, videoStream);
codecParameters = formatCtx->streams[videoStream]->codecpar;
codec = avcodec_find_decoder(codecParameters->codec_id);
codecCtx = avcodec_alloc_context3(codec);
if (codec == NULL) {
CLog::Log(LOGERROR, "%s::%s - Unsupported codec.", CLASSNAME, __func__);
return false;
}
if (avcodec_open2(codecCtx, codec, NULL) < 0) {
CLog::Log(LOGERROR, "%s::%s - Unable to open codec.", CLASSNAME, __func__);
return false;
}
CLog::Log(LOGDEBUG, "%s::%s - AVCodec: %s, id %d", CLASSNAME, __func__, codec->name, codec->id);
m_cVideoCodec = new CDVDVideoCodecC1();
m_cHints = new CDVDStreamInfo();
m_cHints->software = false;
m_cHints->extradata = codecParameters->extradata;
m_cHints->extrasize = codecParameters->extradata_size;
m_cHints->codec = codecParameters->codec_id;
m_cHints->codec_tag = codecParameters->codec_tag;
m_cHints->width = codecParameters->width;
m_cHints->height = codecParameters->height;
CLog::Log(LOGDEBUG, "%s::%s - Header of size %d", CLASSNAME, __func__, codecCtx->extradata_size);
CDVDCodecOptions options;
if (!m_cVideoCodec->Open(*m_cHints, options)) {
Cleanup();
return false;
}
CLog::Log(LOGNOTICE, "%s::%s - ===START===", CLASSNAME, __func__);
initGL();
// MAIN LOOP
int frameNumber = 0;
int ret = 0;
m_pDvdVideoPicture = new DVDVideoPicture();
clock_gettime(CLOCK_REALTIME, &startTs);
av_init_packet(&packet);
while (av_read_frame(formatCtx, &packet) >= 0) {
if (packet.stream_index != videoStream)
continue;
if (ret < 0) {
CLog::Log(LOGNOTICE, "%s::%s - Parser has extracted all frames", CLASSNAME, __func__);
break;
}
frameNumber++;
CLog::Log(LOGDEBUG, "%s::%s - Extracted frame number %d of size %d", CLASSNAME, __func__, frameNumber, packet.size);
ret = m_cVideoCodec->Decode(packet.data, packet.size, packet.pts, packet.dts);
if (ret & VC_PICTURE)
{
m_cVideoCodec->GetPicture(m_pDvdVideoPicture);
//EnableTexture(m_pDvdVideoPicture);
}
av_packet_unref(&packet);
usleep(1000*10);
}
CLog::Log(LOGNOTICE, "%s::%s - ===STOP===", CLASSNAME, __func__);
clock_gettime(CLOCK_REALTIME, &endTs);
double seconds = (double )(endTs.tv_sec - startTs.tv_sec) + (double )(endTs.tv_nsec - startTs.tv_nsec) / 1000000000;
double fps = (double)frameNumber / seconds;
CLog::Log(LOGNOTICE, "%s::%s - Runtime %f sec, fps: %f", CLASSNAME, __func__, seconds, fps);
Cleanup();
return 0;
}