8 #include <SDL2/SDL_error.h>
9 #include <SDL2/SDL_video.h>
18 #include <unordered_map>
20 #include <movit/util.h>
26 #define WIDTH_BLOCKS (WIDTH/8)
27 #define WIDTH_BLOCKS_CHROMA (WIDTH/16)
28 #define HEIGHT_BLOCKS (HEIGHT/8)
29 #define NUM_BLOCKS (WIDTH_BLOCKS * HEIGHT_BLOCKS)
30 #define NUM_BLOCKS_CHROMA (WIDTH_BLOCKS_CHROMA * HEIGHT_BLOCKS)
33 #define ESCAPE_LIMIT (NUM_SYMS - 1)
34 #define BLOCKS_PER_STREAM 320
36 static constexpr uint32_t prob_bits = 12;
37 static constexpr uint32_t prob_scale = 1 << prob_bits;
39 unsigned char rgb[WIDTH * HEIGHT * 3];
40 unsigned char pix_y[WIDTH * HEIGHT];
41 unsigned char pix_cb[(WIDTH/2) * HEIGHT];
42 unsigned char pix_cr[(WIDTH/2) * HEIGHT];
45 unsigned dist[4 * 256];
46 std::pair<unsigned, unsigned> ransdist[4 * 256];
50 using namespace std::chrono;
52 void write_varint(int x, FILE *fp)
55 putc((x & 0x7f) | 0x80, fp);
61 void readpix(unsigned char *ptr, const char *filename)
63 FILE *fp = fopen(filename, "rb");
69 fseek(fp, 0, SEEK_END);
71 assert(len >= WIDTH * HEIGHT * 3);
72 fseek(fp, len - WIDTH * HEIGHT * 3, SEEK_SET);
74 fread(ptr, 1, WIDTH * HEIGHT * 3, fp);
78 // Should be done on the GPU, of course, but irrelevant for the demonstration.
81 double coeff[3] = { 0.2126, 0.7152, 0.0722 }; // sum = 1.0
82 double cb_fac = 1.0 / (coeff[0] + coeff[1] + 1.0f - coeff[2]); // 0.539
83 double cr_fac = 1.0 / (1.0f - coeff[0] + coeff[1] + coeff[2]); // 0.635
85 unique_ptr<float[]> temp_cb(new float[WIDTH * HEIGHT]);
86 unique_ptr<float[]> temp_cr(new float[WIDTH * HEIGHT]);
87 for (unsigned yb = 0; yb < HEIGHT; ++yb) {
88 for (unsigned xb = 0; xb < WIDTH; ++xb) {
89 int r = rgb[((yb * WIDTH) + xb) * 3 + 0];
90 int g = rgb[((yb * WIDTH) + xb) * 3 + 1];
91 int b = rgb[((yb * WIDTH) + xb) * 3 + 2];
92 double y = std::min(std::max(coeff[0] * r + coeff[1] * g + coeff[2] * b, 0.0), 255.0);
93 double cb = (b - y) * cb_fac + 128.0;
94 double cr = (r - y) * cr_fac + 128.0;
95 pix_y[(yb * WIDTH) + xb] = lrint(y);
96 temp_cb[(yb * WIDTH) + xb] = cb;
97 temp_cr[(yb * WIDTH) + xb] = cr;
101 // Simple 4:2:2 subsampling with left convention.
102 for (unsigned yb = 0; yb < HEIGHT; ++yb) {
103 for (unsigned xb = 0; xb < WIDTH / 2; ++xb) {
104 int c0 = yb * WIDTH + std::max(int(xb) * 2 - 1, 0);
105 int c1 = yb * WIDTH + xb * 2;
106 int c2 = yb * WIDTH + xb * 2 + 1;
108 double cb = 0.25 * temp_cb[c0] + 0.5 * temp_cb[c1] + 0.25 * temp_cb[c2];
109 double cr = 0.25 * temp_cr[c0] + 0.5 * temp_cr[c1] + 0.25 * temp_cr[c2];
110 cb = std::min(std::max(cb, 0.0), 255.0);
111 cr = std::min(std::max(cr, 0.0), 255.0);
112 pix_cb[(yb * WIDTH/2) + xb] = lrint(cb);
113 pix_cr[(yb * WIDTH/2) + xb] = lrint(cr);
118 int main(int argc, char **argv)
120 // Set up an OpenGL context using SDL.
121 if (SDL_Init(SDL_INIT_VIDEO) == -1) {
122 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
125 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
126 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
127 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
128 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
129 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
130 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
132 SDL_Window *window = SDL_CreateWindow("OpenGL window for unit test",
133 SDL_WINDOWPOS_UNDEFINED,
134 SDL_WINDOWPOS_UNDEFINED,
137 SDL_GLContext context = SDL_GL_CreateContext(window);
138 assert(context != nullptr);
141 readpix(rgb, argv[1]);
143 readpix(rgb, "color.pnm");
146 // Compile the DCT shader.
147 string shader_src = ::read_file("encoder.shader");
148 GLuint shader_num = compile_shader(shader_src, GL_COMPUTE_SHADER);
149 GLuint glsl_program_num = glCreateProgram();
150 glAttachShader(glsl_program_num, shader_num);
151 glLinkProgram(glsl_program_num);
154 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
155 if (success == GL_FALSE) {
156 GLchar error_log[1024] = {0};
157 glGetProgramInfoLog(glsl_program_num, 1024, nullptr, error_log);
158 fprintf(stderr, "Error linking program: %s\n", error_log);
162 // Compile the tally shader.
163 shader_src = ::read_file("tally.shader");
164 shader_num = compile_shader(shader_src, GL_COMPUTE_SHADER);
165 GLuint glsl_tally_program_num = glCreateProgram();
166 glAttachShader(glsl_tally_program_num, shader_num);
167 glLinkProgram(glsl_tally_program_num);
169 glGetProgramiv(glsl_tally_program_num, GL_LINK_STATUS, &success);
170 if (success == GL_FALSE) {
171 GLchar error_log[1024] = {0};
172 glGetProgramInfoLog(glsl_tally_program_num, 1024, nullptr, error_log);
173 fprintf(stderr, "Error linking program: %s\n", error_log);
177 // Compile the rANS shader.
178 shader_src = ::read_file("rans.shader");
179 shader_num = compile_shader(shader_src, GL_COMPUTE_SHADER);
180 GLuint glsl_rans_program_num = glCreateProgram();
181 glAttachShader(glsl_rans_program_num, shader_num);
182 glLinkProgram(glsl_rans_program_num);
184 glGetProgramiv(glsl_rans_program_num, GL_LINK_STATUS, &success);
185 if (success == GL_FALSE) {
186 GLchar error_log[1024] = {0};
187 glGetProgramInfoLog(glsl_rans_program_num, 1024, nullptr, error_log);
188 fprintf(stderr, "Error linking program: %s\n", error_log);
193 // An SSBO for the rANS distributions.
195 glGenBuffers(1, &ssbo);
196 glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
197 glNamedBufferStorage(ssbo, 256 * 16 * sizeof(uint32_t), nullptr, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
200 // SSBOs for the rANS output (data and offsets).
202 glGenBuffers(1, &output_ssbo);
203 glBindBuffer(GL_SHADER_STORAGE_BUFFER, output_ssbo);
204 glNamedBufferStorage(output_ssbo, 45 * 64 * 1024, nullptr, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
207 GLuint output_offset_ssbo;
208 glGenBuffers(1, &output_offset_ssbo);
209 glBindBuffer(GL_SHADER_STORAGE_BUFFER, output_offset_ssbo);
210 glNamedBufferStorage(output_offset_ssbo, 45 * 64 * sizeof(uint32_t), nullptr, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
214 glUseProgram(glsl_program_num);
215 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 9, ssbo);
217 glUseProgram(glsl_tally_program_num);
218 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 9, ssbo);
220 glUseProgram(glsl_rans_program_num);
221 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 9, ssbo);
222 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 10, output_ssbo);
223 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 11, output_offset_ssbo);
225 glUseProgram(glsl_program_num);
230 glGenTextures(1, &y_tex);
231 glBindTexture(GL_TEXTURE_2D, y_tex);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
236 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8UI, WIDTH, HEIGHT, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, pix_y);
239 // Make destination textures.
240 GLuint dc_ac7_tex, ac1_ac6_tex, ac2_ac5_tex;
241 for (GLuint *tex : { &dc_ac7_tex, &ac1_ac6_tex, &ac2_ac5_tex }) {
242 glGenTextures(1, tex);
243 glBindTexture(GL_TEXTURE_2D, *tex);
244 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
245 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
246 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
248 glTexImage2D(GL_TEXTURE_2D, 0, GL_R16UI, WIDTH / 8, HEIGHT, 0, GL_RED_INTEGER, GL_UNSIGNED_SHORT, nullptr);
252 GLuint ac3_tex, ac4_tex;
253 for (GLuint *tex : { &ac3_tex, &ac4_tex }) {
254 glGenTextures(1, tex);
255 glBindTexture(GL_TEXTURE_2D, *tex);
256 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
257 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
258 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
259 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
260 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8I, WIDTH / 8, HEIGHT, 0, GL_RED_INTEGER, GL_BYTE, nullptr);
264 glBindImageTexture(0, dc_ac7_tex, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16UI);
265 glBindImageTexture(1, ac1_ac6_tex, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16UI);
266 glBindImageTexture(2, ac2_ac5_tex, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R16UI);
267 glBindImageTexture(3, ac3_tex, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R8I);
268 glBindImageTexture(4, ac4_tex, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R8I);
269 glBindImageTexture(5, y_tex, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8UI);
273 glUseProgram(glsl_program_num);
274 GLint dc_ac7_tex_uniform = glGetUniformLocation(glsl_program_num, "dc_ac7_tex");
275 GLint ac1_ac6_tex_uniform = glGetUniformLocation(glsl_program_num, "ac1_ac6_tex");
276 GLint ac2_ac5_tex_uniform = glGetUniformLocation(glsl_program_num, "ac2_ac5_tex");
277 GLint ac3_tex_uniform = glGetUniformLocation(glsl_program_num, "ac3_tex");
278 GLint ac4_tex_uniform = glGetUniformLocation(glsl_program_num, "ac4_tex");
279 GLint image_tex_uniform = glGetUniformLocation(glsl_program_num, "image_tex");
280 glUniform1i(dc_ac7_tex_uniform, 0);
281 glUniform1i(ac1_ac6_tex_uniform, 1);
282 glUniform1i(ac2_ac5_tex_uniform, 2);
283 glUniform1i(ac3_tex_uniform, 3);
284 glUniform1i(ac4_tex_uniform, 4);
285 glUniform1i(image_tex_uniform, 5);
287 glUseProgram(glsl_rans_program_num);
288 dc_ac7_tex_uniform = glGetUniformLocation(glsl_rans_program_num, "dc_ac7_tex");
289 ac1_ac6_tex_uniform = glGetUniformLocation(glsl_rans_program_num, "ac1_ac6_tex");
290 ac2_ac5_tex_uniform = glGetUniformLocation(glsl_rans_program_num, "ac2_ac5_tex");
291 ac3_tex_uniform = glGetUniformLocation(glsl_rans_program_num, "ac3_tex");
292 ac4_tex_uniform = glGetUniformLocation(glsl_rans_program_num, "ac4_tex");
293 image_tex_uniform = glGetUniformLocation(glsl_rans_program_num, "image_tex");
294 glUniform1i(dc_ac7_tex_uniform, 0);
295 glUniform1i(ac1_ac6_tex_uniform, 1);
296 glUniform1i(ac2_ac5_tex_uniform, 2);
297 glUniform1i(ac3_tex_uniform, 3);
298 glUniform1i(ac4_tex_uniform, 4);
300 steady_clock::time_point start = steady_clock::now();
301 unsigned num_iterations = 100;
302 for (unsigned i = 0; i < num_iterations; ++i) {
303 glClearNamedBufferSubData(ssbo, GL_R8, 0, 256 * 16 * sizeof(uint32_t), GL_RED, GL_UNSIGNED_BYTE, nullptr);
304 glUseProgram(glsl_program_num);
305 glDispatchCompute(WIDTH_BLOCKS / 16, HEIGHT_BLOCKS, 1);
306 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
308 glUseProgram(glsl_tally_program_num);
309 glDispatchCompute(4, 1, 1);
310 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
312 glUseProgram(glsl_rans_program_num);
313 glDispatchCompute(NUM_BLOCKS / BLOCKS_PER_STREAM, 8, 5);
318 steady_clock::time_point now = steady_clock::now();
321 printf("%ld bytes + %ld escape bits (%ld) = %ld total bytes\n",
322 tot_bytes - extra_bits / 8,
330 printf("Each iteration took %.3f ms.\n", 1e3 * duration<double>(now - start).count() / num_iterations);
332 FILE *codedfp = fopen("coded.dat", "wb");
333 if (codedfp == nullptr) {
338 // Write out the distributions.
339 const RansDistSSBO *rans_dist = (const RansDistSSBO *)glMapNamedBufferRange(ssbo, 0, 256 * 16 * sizeof(uint32_t), GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
340 for (unsigned r = 0; r < 2; ++r) { // Hack to write fake chroma tables.
341 // TODO: rather gamma-k or something
342 for (unsigned i = 0; i < 4; ++i) {
343 printf("writing table %d\n", i);
344 for (unsigned j = 0; j < NUM_SYMS; ++j) {
345 printf("%d,%d: start=%d freq=%d\n", i, j, rans_dist->ransdist[i * 256 + j].first, rans_dist->ransdist[i * 256 + j].second);
346 write_varint(rans_dist->ransdist[i * 256 + j].second, codedfp);
351 // Write out the actual data.
352 // TODO: Do the deduplication.
354 const uint32_t *offsets = (const uint32_t *)glMapNamedBufferRange(output_offset_ssbo, 0, 45 * 64 * sizeof(uint32_t), GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
356 for (int i = 0; i < 45*64; ++i) {
357 printf("%d,%d,%d: %u\n", i / 64, (i / 8) % 8, i % 8, 1024 * (i + 1) - offsets[i]);
361 const uint8_t *data = (const uint8_t *)glMapNamedBufferRange(output_ssbo, 0, 45 * 64 * 1024, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
363 for (unsigned y = 0; y < 8; ++y) {
364 for (unsigned x = 0; x < 8; ++x) {
365 for (unsigned int stream_idx = 0; stream_idx < 45; ++stream_idx) {
366 const uint8_t *out_end = data + (stream_idx * 64 + y * 8 + x + 1) * 1024;
367 const uint8_t *ptr = data + offsets[stream_idx * 64 + y * 8 + x];
368 uint32_t num_rans_bytes = out_end - ptr;
370 if (num_rans_bytes == last_block.size() &&
371 memcmp(last_block.data(), ptr, last_block.size()) == 0) {
372 write_varint(0, codedfp);
376 last_block = string((const char *)ptr, num_rans_bytes);
380 write_varint(num_rans_bytes, codedfp);
381 fwrite(ptr, 1, num_rans_bytes, codedfp);