-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplay.cpp
217 lines (174 loc) · 5.88 KB
/
play.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
#include <iostream>
#include <deque>
#include <pez.h>
#include <glew.h>
#include <iomanip>
#include <chrono>
#include <sstream>
#include "glvideo.h"
#include "debug.h"
using namespace std;
static const std::string VERTEX_SHADER_SOURCE =
R"EOF(
#version 410
in vec2 aPosition;
in vec2 aTexCoord;
out vec2 vTexCoord;
void main()
{
vTexCoord = aTexCoord;
gl_Position = vec4( aPosition, 0.0, 1.0 );
}
)EOF";
static const std::string FRAGMENT_SHADER_SOURCE =
R"EOF(
#version 410
in vec2 vTexCoord;
uniform sampler2D uTexture;
out vec4 oColor;
void main()
{
oColor = texture(uTexture, vTexCoord);
}
)EOF";
static const std::string YCoCg_FRAGMENT_SHADER_SOURCE =
R"EOF(
#version 410
in vec2 vTexCoord;
uniform sampler2D uTexture;
out vec4 oColor;
void main()
{
vec4 color = texture(uTexture, vTexCoord);
float Co = color.x - ( 0.5 * 256.0 / 255.0 );
float Cg = color.y - ( 0.5 * 256.0 / 255.0 );
float Y = color.w;
oColor = vec4( Y + Co - Cg, Y + Cg, Y - Co - Cg, color.a );
}
)EOF";
glvideo::Movie::ref movie;
glvideo::Context::ref context;
const float COORD_EXTENTS = 1.f;
static void BuildGeometry( int width, int height );
static void LoadEffect( bool isYCoCg = false );
deque<unsigned int> frameTimes;
static unsigned int sumElapsedMilliseconds = 0;
typedef chrono::high_resolution_clock hrclock;
static hrclock::time_point lastReportTime = hrclock::now();
enum {
PositionSlot, TexCoordSlot
};
void PezHandleMouse( int x, int y, int action ) {}
void PezUpdate( unsigned int elapsedMilliseconds )
{
frameTimes.push_back( elapsedMilliseconds );
sumElapsedMilliseconds += elapsedMilliseconds;
while ( frameTimes.size() > 100 ) {
sumElapsedMilliseconds -= frameTimes.front();
frameTimes.pop_front();
}
movie->update();
auto now = hrclock::now();
if ( chrono::duration_cast<chrono::seconds>( now - lastReportTime ).count() > 1 ) {
double avg = (double) sumElapsedMilliseconds / (double) frameTimes.size();
double fps = 1000.0 / avg;
DBOUT( "Frame AVG ms: " << setprecision( 2 ) << avg << "ms (" << fps << " fps)" )
lastReportTime = now;
}
DBGL;
}
void PezRender()
{
glClear( GL_COLOR_BUFFER_BIT );
glActiveTexture( GL_TEXTURE0 );
auto frame = movie->getCurrentFrame();
if ( frame ) {
glBindTexture( frame->getTextureTarget(), frame->getTextureId() );
glDrawArrays( GL_TRIANGLES, 0, 6 );
}
DBGL;
}
const char *PezInitialize( int width, int height )
{
string filename = "videos/hap-3840x2160-24fps.mov";
context = glvideo::Context::create( 2 );
movie = glvideo::Movie::create( context, filename );
BuildGeometry( movie->getWidth(), movie->getHeight() );
LoadEffect( movie->getCodec() == "HapY" );
DBOUT( "Format: " << movie->getFormat() );
DBOUT( "Duration (seconds): " << movie->getDuration() );
DBOUT( "Size: " << movie->getWidth() << "x" << movie->getHeight() );
DBOUT( "Framerate: " << movie->getFramerate() );
DBOUT( "Number of tracks: " << movie->getNumTracks() );
for ( int i = 0; i < movie->getNumTracks(); ++i ) {
DBOUT( "\tTrack " << i << " type: " << movie->getTrackDescription( i ) );
}
movie->loop().play();
return "Test Playback";
}
static void BuildGeometry( int width, int height )
{
float X = COORD_EXTENTS;
float Y = COORD_EXTENTS;
float verts[] = {
-X, -Y, 0.f, 1.f,
-X, +Y, 0.f, 0.f,
+X, +Y, 1.f, 0.f,
+X, +Y, 1.f, 0.f,
+X, -Y, 1.f, 1.f,
-X, -Y, 0.f, 1.f,
};
GLuint vboHandle;
GLsizeiptr vboSize = sizeof( verts );
GLsizei stride = 4 * sizeof( float );
GLenum usage = GL_STATIC_DRAW;
GLvoid *texCoordOffset = (GLvoid *) (sizeof( float ) * 2);
GLuint vao = 0;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
glGenBuffers( 1, &vboHandle );
glBindBuffer( GL_ARRAY_BUFFER, vboHandle );
glBufferData( GL_ARRAY_BUFFER, vboSize, verts, usage );
glVertexAttribPointer( PositionSlot, 2, GL_FLOAT, GL_FALSE, stride, 0 );
glVertexAttribPointer( TexCoordSlot, 2, GL_FLOAT, GL_FALSE, stride, texCoordOffset );
glEnableVertexAttribArray( PositionSlot );
glEnableVertexAttribArray( TexCoordSlot );
}
static void LoadEffect( bool isYCoCg )
{
const char *vsSource = VERTEX_SHADER_SOURCE.c_str();
const char *fsSource;
if ( isYCoCg ) {
fsSource = YCoCg_FRAGMENT_SHADER_SOURCE.c_str();
} else {
fsSource = FRAGMENT_SHADER_SOURCE.c_str();
}
GLuint vsHandle, fsHandle;
GLint compileSuccess, linkSuccess;
GLchar compilerSpew[256];
GLuint programHandle;
vsHandle = glCreateShader( GL_VERTEX_SHADER );
fsHandle = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( vsHandle, 1, &vsSource, 0 );
glCompileShader( vsHandle );
glGetShaderiv( vsHandle, GL_COMPILE_STATUS, &compileSuccess );
glGetShaderInfoLog( vsHandle, sizeof( compilerSpew ), 0, compilerSpew );
PezCheckCondition( compileSuccess, compilerSpew );
glShaderSource( fsHandle, 1, &fsSource, 0 );
glCompileShader( fsHandle );
glGetShaderiv( fsHandle, GL_COMPILE_STATUS, &compileSuccess );
glGetShaderInfoLog( fsHandle, sizeof( compilerSpew ), 0, compilerSpew );
PezCheckCondition( compileSuccess, compilerSpew );
programHandle = glCreateProgram();
glAttachShader( programHandle, vsHandle );
glAttachShader( programHandle, fsHandle );
glBindAttribLocation( programHandle, PositionSlot, "aPosition" );
glBindAttribLocation( programHandle, TexCoordSlot, "aTexCoord" );
glLinkProgram( programHandle );
glGetProgramiv( programHandle, GL_LINK_STATUS, &linkSuccess );
glGetProgramInfoLog( programHandle, sizeof( compilerSpew ), 0, compilerSpew );
PezCheckCondition( linkSuccess, compilerSpew );
glUseProgram( programHandle );
GLint loc = glGetUniformLocation( programHandle, "uTexture" );
glUniform1i( loc, 0 );
}