1 #include "timecode_renderer.h"
11 #include <movit/effect_util.h>
12 #include <movit/resource_pool.h>
13 #include <movit/util.h>
17 using namespace movit;
19 TimecodeRenderer::TimecodeRenderer(movit::ResourcePool *resource_pool, unsigned display_width, unsigned display_height)
20 : resource_pool(resource_pool), display_width(display_width), display_height(display_height), height(28)
25 "in vec2 position; \n"
26 "in vec2 texcoord; \n"
31 " // The result of glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0) is: \n"
33 " // 2.000 0.000 0.000 -1.000 \n"
34 " // 0.000 2.000 0.000 -1.000 \n"
35 " // 0.000 0.000 -2.000 -1.000 \n"
36 " // 0.000 0.000 0.000 1.000 \n"
37 " gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0); \n"
43 "uniform sampler2D tex; \n"
44 "out vec4 Y, CbCr, RGBA; \n"
46 " vec4 gray = texture(tex, tc0); \n"
47 " RGBA = gray.rrra; \n"
48 " gray.r = gray.r * ((235.0-16.0)/255.0) + 16.0/255.0; \n" // Limited-range Y'CbCr.
50 " CbCr = vec4(128.0/255.0, 128.0/255.0, 0.0, 1.0); \n"
53 vector<string> frag_shader_outputs;
54 program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader, frag_shader_outputs);
57 texture_sampler_uniform = glGetUniformLocation(program_num, "tex");
59 position_attribute_index = glGetAttribLocation(program_num, "position");
61 texcoord_attribute_index = glGetAttribLocation(program_num, "texcoord");
64 // Shared between the two.
70 vbo = generate_vbo(2, GL_FLOAT, sizeof(vertices), vertices);
73 tex = resource_pool->create_2d_texture(GL_R8, display_width, height);
75 image.reset(new QImage(display_width, height, QImage::Format_Grayscale8));
78 TimecodeRenderer::~TimecodeRenderer()
80 resource_pool->release_2d_texture(tex);
82 resource_pool->release_glsl_program(program_num);
84 glDeleteBuffers(1, &vbo);
88 string TimecodeRenderer::get_timecode_text(double pts, unsigned frame_num)
90 // Find the wall time, and round it to the nearest millisecond.
92 gettimeofday(&now, nullptr);
93 time_t unixtime = now.tv_sec;
94 unsigned msecs = (now.tv_usec + 500) / 1000;
101 gmtime_r(&unixtime, &utc_tm);
102 char clock_text[256];
103 strftime(clock_text, sizeof(clock_text), "%Y-%m-%d %H:%M:%S", &utc_tm);
105 // Make the stream timecode, rounded to the nearest millisecond.
106 long stream_time = lrint(pts * 1e3);
107 assert(stream_time >= 0);
108 unsigned stream_time_ms = stream_time % 1000;
110 unsigned stream_time_sec = stream_time % 60;
112 unsigned stream_time_min = stream_time % 60;
113 unsigned stream_time_hour = stream_time / 60;
115 char timecode_text[256];
116 snprintf(timecode_text, sizeof(timecode_text), "Nageru - %s.%03u UTC - Stream time %02u:%02u:%02u.%03u (frame %u)",
117 clock_text, msecs, stream_time_hour, stream_time_min, stream_time_sec, stream_time_ms, frame_num);
118 return timecode_text;
121 void TimecodeRenderer::render_timecode(GLuint fbo, const string &text)
123 render_string_to_buffer(text);
124 render_buffer_to_fbo(fbo);
127 void TimecodeRenderer::render_string_to_buffer(const string &text)
130 QPainter painter(image.get());
132 painter.setPen(Qt::white);
133 QFont font = painter.font();
134 font.setPointSize(16);
135 painter.setFont(font);
137 painter.drawText(QRectF(0, 0, display_width, height), Qt::AlignCenter, QString::fromStdString(text));
140 void TimecodeRenderer::render_buffer_to_fbo(GLuint fbo)
142 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
146 glGenVertexArrays(1, &vao);
149 glBindVertexArray(vao);
152 glViewport(0, display_height - height, display_width, height);
155 glActiveTexture(GL_TEXTURE0);
157 glBindTexture(GL_TEXTURE_2D, tex);
159 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
163 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
166 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, display_width, height, GL_RED, GL_UNSIGNED_BYTE, image->bits());
169 glUseProgram(program_num);
171 glUniform1i(texture_sampler_uniform, 0);
174 glBindBuffer(GL_ARRAY_BUFFER, vbo);
177 for (GLint attr_index : { position_attribute_index, texcoord_attribute_index }) {
178 if (attr_index == -1) continue;
179 glEnableVertexAttribArray(attr_index);
181 glVertexAttribPointer(attr_index, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
185 glDrawArrays(GL_TRIANGLES, 0, 3);
188 for (GLint attr_index : { position_attribute_index, texcoord_attribute_index }) {
189 if (attr_index == -1) continue;
190 glDisableVertexAttribArray(attr_index);
194 glActiveTexture(GL_TEXTURE0);
199 glDeleteVertexArrays(1, &vao);
202 glBindFramebuffer(GL_FRAMEBUFFER, 0);