]> git.sesse.net Git - nageru/blob - timecode_renderer.cpp
Add a switch for writing a timecode to the stream; useful for latency debugging.
[nageru] / timecode_renderer.cpp
1 #include "timecode_renderer.h"
2
3 #include <memory>
4 #include <string>
5 #include <vector>
6
7 #include <QImage>
8 #include <QPainter>
9
10 #include <epoxy/gl.h>
11 #include <movit/effect_util.h>
12 #include <movit/resource_pool.h>
13 #include <movit/util.h>
14 #include <sys/time.h>
15
16 using namespace std;
17 using namespace movit;
18
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)
21 {
22         string vert_shader =
23                 "#version 130 \n"
24                 " \n"
25                 "in vec2 position; \n"
26                 "in vec2 texcoord; \n"
27                 "out vec2 tc0; \n"
28                 " \n"
29                 "void main() \n"
30                 "{ \n"
31                 "    // The result of glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0) is: \n"
32                 "    // \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"
38                 "    tc0 = texcoord; \n"
39                 "} \n";
40         string frag_shader =
41                 "#version 130 \n"
42                 "in vec2 tc0; \n"
43                 "uniform sampler2D tex; \n"
44                 "out vec4 Y, CbCr, RGBA; \n"
45                 "void main() { \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.
49                 "    Y = gray.rrra; \n"
50                 "    CbCr = vec4(128.0/255.0, 128.0/255.0, 0.0, 1.0); \n"
51                 "} \n";
52
53         vector<string> frag_shader_outputs;
54         program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader, frag_shader_outputs);
55         check_error();
56
57         texture_sampler_uniform = glGetUniformLocation(program_num, "tex");
58         check_error();
59         position_attribute_index = glGetAttribLocation(program_num, "position");
60         check_error();
61         texcoord_attribute_index = glGetAttribLocation(program_num, "texcoord");
62         check_error();
63
64         // Shared between the two.
65         float vertices[] = {
66                 0.0f, 2.0f,
67                 0.0f, 0.0f,
68                 2.0f, 0.0f
69         };
70         vbo = generate_vbo(2, GL_FLOAT, sizeof(vertices), vertices);
71         check_error();
72
73         tex = resource_pool->create_2d_texture(GL_R8, display_width, height);
74
75         image.reset(new QImage(display_width, height, QImage::Format_Grayscale8));
76 }
77
78 TimecodeRenderer::~TimecodeRenderer()
79 {
80         resource_pool->release_2d_texture(tex);
81         check_error();
82         resource_pool->release_glsl_program(program_num);
83         check_error();
84         glDeleteBuffers(1, &vbo);
85         check_error();
86 }
87
88 string TimecodeRenderer::get_timecode_text(double pts, unsigned frame_num)
89 {
90         // Find the wall time, and round it to the nearest millisecond.
91         timeval now;
92         gettimeofday(&now, nullptr);
93         time_t unixtime = now.tv_sec;
94         unsigned msecs = (now.tv_usec + 500) / 1000;
95         if (msecs >= 1000) {
96                 msecs -= 1000;
97                 ++unixtime;
98         }
99
100         tm utc_tm;
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);
104
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;
109         stream_time /= 1000;
110         unsigned stream_time_sec = stream_time % 60;
111         stream_time /= 60;
112         unsigned stream_time_min = stream_time % 60;
113         unsigned stream_time_hour = stream_time / 60;
114
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;
119 }
120
121 void TimecodeRenderer::render_timecode(GLuint fbo, const string &text)
122 {
123         render_string_to_buffer(text);
124         render_buffer_to_fbo(fbo);
125 }
126
127 void TimecodeRenderer::render_string_to_buffer(const string &text)
128 {
129         image->fill(0);
130         QPainter painter(image.get());
131
132         painter.setPen(Qt::white);
133         QFont font = painter.font();
134         font.setPointSize(16);
135         painter.setFont(font);
136
137         painter.drawText(QRectF(0, 0, display_width, height), Qt::AlignCenter, QString::fromStdString(text));
138 }
139
140 void TimecodeRenderer::render_buffer_to_fbo(GLuint fbo)
141 {
142         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
143         check_error();
144
145         GLuint vao;
146         glGenVertexArrays(1, &vao);
147         check_error();
148
149         glBindVertexArray(vao);
150         check_error();
151
152         glViewport(0, display_height - height, display_width, height);
153         check_error();
154
155         glActiveTexture(GL_TEXTURE0);
156         check_error();
157         glBindTexture(GL_TEXTURE_2D, tex);
158         check_error();
159         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
160         check_error();
161         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
162         check_error();
163         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
164         check_error();
165
166         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, display_width, height, GL_RED, GL_UNSIGNED_BYTE, image->bits());
167         check_error();
168
169         glUseProgram(program_num);
170         check_error();
171         glUniform1i(texture_sampler_uniform, 0);
172         check_error();
173
174         glBindBuffer(GL_ARRAY_BUFFER, vbo);
175         check_error();
176
177         for (GLint attr_index : { position_attribute_index, texcoord_attribute_index }) {
178                 if (attr_index == -1) continue;
179                 glEnableVertexAttribArray(attr_index);
180                 check_error();
181                 glVertexAttribPointer(attr_index, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
182                 check_error();
183         }
184
185         glDrawArrays(GL_TRIANGLES, 0, 3);
186         check_error();
187
188         for (GLint attr_index : { position_attribute_index, texcoord_attribute_index }) {
189                 if (attr_index == -1) continue;
190                 glDisableVertexAttribArray(attr_index);
191                 check_error();
192         }
193
194         glActiveTexture(GL_TEXTURE0);
195         check_error();
196         glUseProgram(0);
197         check_error();
198
199         glDeleteVertexArrays(1, &vao);
200         check_error();
201
202         glBindFramebuffer(GL_FRAMEBUFFER, 0);
203         check_error();
204 }