-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
293 lines (229 loc) · 10.4 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
#include"shader_utils.h"
#include"desktop_capture.hpp"
#include"media_player.hpp"
#include"UIManager.hpp"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
#include <thread>
#include <string>
namespace Config {
constexpr char WINDOW_TITLE[] = "Rewind";
constexpr int INITIAL_WIDTH = 800;
constexpr int INITIAL_HEIGHT = 600;
const bool DEBUG = false;
}
class Rewind {
public:
const std::string filename = "video.mp4";
int frame_width = 0;
int frame_height = 0;
unsigned char* frame_data = nullptr;
float screen_aspect_ratio = 0.0f;
Rewind() {
}
int run() {
MediaPlayer mp;
mp.loadFile(this->filename);
VideoFrame firstFrame = mp.videoFrameCache[0];
std::cout << "First frame width: " << firstFrame.width << std::endl;
this->frame_width = firstFrame.width;
this->frame_height = firstFrame.height;
if (this->frame_width == 0 || this->frame_height == 0) {
std::cout << "Invalid frame dimensions" << std::endl;
return -1;
}
screen_aspect_ratio = (float)this->frame_width / this->frame_height;
if (firstFrame.data == nullptr) {
std::cout << "Error: Frame data is null" << std::endl;
return -1;
}
// Initialize GLFW
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return -1;
}
// Set GLFW context version to 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Initialize vertices
GLfloat vertices[] =
{
// First triangle
0.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom left
0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // bottom right
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // top left
// Second triangle
0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // bottom right
0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top right
0.0f, 0.0f, 0.0f, 0.0f, 0.0f // top left
};
// Create a window
GLFWwindow* window = glfwCreateWindow(Config::INITIAL_WIDTH, Config::INITIAL_HEIGHT, Config::WINDOW_TITLE, NULL, NULL);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Make the OpenGL context current
glfwMakeContextCurrent(window);
// Initialize GLEW
glewExperimental = GL_TRUE; // Ensure GLEW uses modern OpenGL techniques
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW" << std::endl;
return -1;
}
// Set the viewport
glViewport(0, 0, 800, 600);
// Load Shaders
GLuint shaderProgram = createShaderProgram("../shaders/vertex_shader.glsl", "../shaders/fragment_shader.glsl");
GLuint VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); // position
glEnableVertexAttribArray(0);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); // texture coords
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Generate textures
GLuint textureY, textureU, textureV;
glGenTextures(1, &textureY);
glGenTextures(1, &textureU);
glGenTextures(1, &textureV);
// Initialize Y texture
glBindTexture(GL_TEXTURE_2D, textureY);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, frame_width, frame_height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
// Initialize U texture
glBindTexture(GL_TEXTURE_2D, textureU);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, frame_width/2, frame_height/2, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
// Initialize V texture
glBindTexture(GL_TEXTURE_2D, textureV);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, frame_width/2, frame_height/2, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
// Register the framebuffer size callback
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
int width, height, sideBarWidth, bottomBarHeight;
UIManager ui;
ui.init(&mp, &width, &height, window, "#version 330", &this->screen_aspect_ratio);
// Main render loop
while (!glfwWindowShouldClose(window)) {
glfwGetWindowSize(window, &width, &height);
sideBarWidth = std::min(std::max((int)(width*0.2), 200), 400);
// Render
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
// Sync media
mp.syncMedia(glfwGetTime());
// Play next available video and audio frame
VideoFrame vFrame = mp.getVideoFrame();
AudioFrame aFrame = mp.getAudioFrame();
// Generate texture from frame
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureY);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, vFrame.width, vFrame.height, GL_RED, GL_UNSIGNED_BYTE, vFrame.data[0].data());
glUniform1i(glGetUniformLocation(shaderProgram, "textureY"), 0);
// Bind and update U plane texture
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textureU);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, vFrame.width / 2, vFrame.height / 2, GL_RED, GL_UNSIGNED_BYTE, vFrame.data[1].data());
glUniform1i(glGetUniformLocation(shaderProgram, "textureU"), 1);
// Bind and update V plane texture
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, textureV);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, vFrame.width / 2, vFrame.height / 2, GL_RED, GL_UNSIGNED_BYTE, vFrame.data[2].data());
glUniform1i(glGetUniformLocation(shaderProgram, "textureV"), 2);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, sizeof(vertices)/sizeof(vertices[0])/5);
ui.render();
// Process input
handleInput(window);
// Render Toolbar
int toolBarHeight = ui.getToolBarHeight();
// Edit vertices
bottomBarHeight = (int)std::min(std::max(height*0.25, 100.0), 200.0);
int availableWidth = width-sideBarWidth;
int availableHeight = height-bottomBarHeight;
// Maintain aspect ratio
float normalizedScreenWidth, normalizedScreenHeight;
getScreenDimensions(&normalizedScreenWidth, &normalizedScreenHeight, availableHeight, availableWidth);
// Update triangle vertices
updateScreenVertices(vertices, width, height, sideBarWidth, toolBarHeight, normalizedScreenWidth, normalizedScreenHeight, availableWidth, availableHeight);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
// Swap buffers and poll events
glfwSwapBuffers(window);
glfwPollEvents();
}
// Clean up and exit
glfwDestroyWindow(window);
glfwTerminate();
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
return 0;
}
private:
void handleInput(GLFWwindow* window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
float normalizeToCoordinateSpace(float a, float b) {
return (2.0*a)/b-1;
}
// Callback to handle window resizing
static void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
void getScreenDimensions(float* screenWidth, float* screenHeight, int availableHeight, int availableWidth) {
if ((float)availableWidth/availableHeight > this->screen_aspect_ratio) {
*screenHeight = availableHeight;
*screenWidth = availableHeight * this->screen_aspect_ratio;
} else {
*screenWidth = availableWidth;
*screenHeight = availableWidth / this->screen_aspect_ratio;
}
}
void updateScreenVertices(GLfloat* vertices, int width, int height, int sideBarWidth, int toolBarHeight, int screenWidth, int screenHeight, int availableWidth, int availableHeight) {
float centerX = sideBarWidth + (availableWidth - screenWidth) / 2.0f;
float centerY = toolBarHeight + (availableHeight - screenHeight) / 2.0f;
// Screen Triangle 1
vertices[0] = normalizeToCoordinateSpace(centerX, width);
vertices[1] = -normalizeToCoordinateSpace(centerY + screenHeight, height);
vertices[5] = normalizeToCoordinateSpace(centerX + screenWidth, width);
vertices[6] = -normalizeToCoordinateSpace(centerY + screenHeight, height);
vertices[10] = normalizeToCoordinateSpace(centerX, width);
vertices[11] = -normalizeToCoordinateSpace(centerY, height);
// Screen Triangle 2
vertices[15] = normalizeToCoordinateSpace(centerX + screenWidth, width);
vertices[16] = -normalizeToCoordinateSpace(centerY + screenHeight, height);
vertices[20] = normalizeToCoordinateSpace(centerX + screenWidth, width);
vertices[21] = -normalizeToCoordinateSpace(centerY, height);
vertices[25] = normalizeToCoordinateSpace(centerX, width);
vertices[26] = -normalizeToCoordinateSpace(centerY, height);
}
};
int main() {
Rewind rw;
rw.run();
}