1 #include "timecode_renderer.h"
14 #include <QPaintDevice>
18 #include <movit/resource_pool.h>
19 #include <movit/util.h>
23 #include "embedded_files.h"
24 #include "shared/read_file.h"
27 using namespace movit;
29 TimecodeRenderer::TimecodeRenderer(movit::ResourcePool *resource_pool, unsigned display_width, unsigned display_height)
30 : resource_pool(resource_pool), display_width(display_width), display_height(display_height), height(28)
32 string vert_shader = read_file("timecode.vert", _binary_timecode_vert_data, _binary_timecode_vert_size);
34 if (global_flags.bit_depth > 8) {
35 frag_shader = read_file("timecode_10bit.frag", _binary_timecode_10bit_frag_data, _binary_timecode_10bit_frag_size);
37 frag_shader = read_file("timecode.frag", _binary_timecode_frag_data, _binary_timecode_frag_size);
40 vector<string> frag_shader_outputs;
41 program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader, frag_shader_outputs);
44 texture_sampler_uniform = glGetUniformLocation(program_num, "tex");
46 position_attribute_index = glGetAttribLocation(program_num, "position");
48 texcoord_attribute_index = glGetAttribLocation(program_num, "texcoord");
51 // Shared between the two.
57 vbo = generate_vbo(2, GL_FLOAT, sizeof(vertices), vertices);
60 tex = resource_pool->create_2d_texture(GL_R8, display_width, height);
62 image.reset(new QImage(display_width, height, QImage::Format_Grayscale8));
65 TimecodeRenderer::~TimecodeRenderer()
67 resource_pool->release_2d_texture(tex);
69 resource_pool->release_glsl_program(program_num);
71 glDeleteBuffers(1, &vbo);
75 string TimecodeRenderer::get_timecode_text(double pts, unsigned frame_num)
77 // Find the wall time, and round it to the nearest millisecond.
79 gettimeofday(&now, nullptr);
80 time_t unixtime = now.tv_sec;
81 unsigned msecs = (now.tv_usec + 500) / 1000;
88 gmtime_r(&unixtime, &utc_tm);
90 strftime(clock_text, sizeof(clock_text), "%Y-%m-%d %H:%M:%S", &utc_tm);
92 // Make the stream timecode, rounded to the nearest millisecond.
93 long stream_time = lrint(pts * 1e3);
94 assert(stream_time >= 0);
95 unsigned stream_time_ms = stream_time % 1000;
97 unsigned stream_time_sec = stream_time % 60;
99 unsigned stream_time_min = stream_time % 60;
100 unsigned stream_time_hour = stream_time / 60;
102 char timecode_text[512];
103 snprintf(timecode_text, sizeof(timecode_text), "Nageru " NAGERU_VERSION " - %s.%03u UTC - Stream time %02u:%02u:%02u.%03u (frame %u)",
104 clock_text, msecs, stream_time_hour, stream_time_min, stream_time_sec, stream_time_ms, frame_num);
105 return timecode_text;
108 void TimecodeRenderer::render_timecode(GLuint fbo, const string &text)
110 render_string_to_buffer(text);
111 render_buffer_to_fbo(fbo);
114 void TimecodeRenderer::render_string_to_buffer(const string &text)
117 QPainter painter(image.get());
119 painter.setPen(Qt::white);
120 QFont font = painter.font();
121 font.setFamily("Noto Sans");
122 font.setPointSize(16);
123 painter.setFont(font);
125 painter.drawText(QRectF(0, 0, display_width, height), Qt::AlignCenter, QString::fromStdString(text));
128 void TimecodeRenderer::render_buffer_to_fbo(GLuint fbo)
130 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
134 glGenVertexArrays(1, &vao);
137 glBindVertexArray(vao);
140 glViewport(0, display_height - height, display_width, height);
143 glActiveTexture(GL_TEXTURE0);
145 glBindTexture(GL_TEXTURE_2D, tex);
147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
149 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
151 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
154 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, display_width, height, GL_RED, GL_UNSIGNED_BYTE, image->bits());
157 glUseProgram(program_num);
159 glUniform1i(texture_sampler_uniform, 0);
162 glBindBuffer(GL_ARRAY_BUFFER, vbo);
165 for (GLint attr_index : { position_attribute_index, texcoord_attribute_index }) {
166 if (attr_index == -1) continue;
167 glEnableVertexAttribArray(attr_index);
169 glVertexAttribPointer(attr_index, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
173 glDrawArrays(GL_TRIANGLES, 0, 3);
176 for (GLint attr_index : { position_attribute_index, texcoord_attribute_index }) {
177 if (attr_index == -1) continue;
178 glDisableVertexAttribArray(attr_index);
182 glActiveTexture(GL_TEXTURE0);
187 glDeleteVertexArrays(1, &vao);
190 glBindFramebuffer(GL_FRAMEBUFFER, 0);