4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "brightness.h"
33 REGISTER_PLUGIN(BrightnessMain)
37 BrightnessConfig::BrightnessConfig()
44 int BrightnessConfig::equivalent(BrightnessConfig &that)
46 return (brightness == that.brightness &&
47 contrast == that.contrast &&
51 void BrightnessConfig::copy_from(BrightnessConfig &that)
53 brightness = that.brightness;
54 contrast = that.contrast;
58 void BrightnessConfig::interpolate(BrightnessConfig &prev,
59 BrightnessConfig &next,
62 int64_t current_frame)
64 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
65 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
67 this->brightness = prev.brightness * prev_scale + next.brightness * next_scale;
68 this->contrast = prev.contrast * prev_scale + next.contrast * next_scale;
69 this->luma = (int)(prev.luma * prev_scale + next.luma * next_scale);
80 YUV BrightnessMain::yuv;
82 BrightnessMain::BrightnessMain(PluginServer *server)
83 : PluginVClient(server)
90 BrightnessMain::~BrightnessMain()
93 if(engine) delete engine;
96 const char* BrightnessMain::plugin_title() { return _("Brightness/Contrast"); }
97 int BrightnessMain::is_realtime() { return 1; }
99 NEW_WINDOW_MACRO(BrightnessMain, BrightnessWindow)
100 LOAD_CONFIGURATION_MACRO(BrightnessMain, BrightnessConfig)
102 int BrightnessMain::process_buffer(VFrame *frame,
103 int64_t start_position,
106 load_configuration();
125 if(!engine) engine = new BrightnessEngine(this, PluginClient::smp + 1);
128 this->output = frame;
130 if(!EQUIV(config.brightness, 0) || !EQUIV(config.contrast, 0))
132 engine->process_packages();
138 int BrightnessMain::handle_opengl()
141 static const char *brightness_yuvluma_frag =
142 "uniform sampler2D tex;\n"
143 "uniform float brightness;\n"
144 "uniform float contrast;\n"
145 "uniform float offset;\n"
148 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
149 " yuva.r += brightness;\n"
150 " yuva.r = yuva.r * contrast + offset;\n"
151 " gl_FragColor = yuva;\n"
154 static const char *brightness_yuv_frag =
155 "uniform sampler2D tex;\n"
156 "uniform float brightness;\n"
157 "uniform float contrast;\n"
158 "uniform float offset;\n"
161 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
162 " yuva.r += brightness;\n"
163 " yuva.rgb *= vec3(contrast, contrast, contrast);\n"
164 " yuva.rgb += vec3(offset, offset, offset);\n"
165 " gl_FragColor = yuva;\n"
168 static const char *brightness_rgb_frag =
169 "uniform sampler2D tex;\n"
170 "uniform float brightness;\n"
171 "uniform float contrast;\n"
172 "uniform float offset;\n"
175 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
176 " rgba.rgb += vec3(brightness, brightness, brightness);\n"
177 " rgba.rgb *= vec3(contrast, contrast, contrast);\n"
178 " rgba.rgb += vec3(offset, offset, offset);\n"
179 " gl_FragColor = rgba;\n"
182 static const char *brightness_rgbluma_frag =
183 "uniform sampler2D tex;\n"
184 "uniform float brightness;\n"
185 "uniform float contrast;\n"
186 "uniform float offset;\n"
189 " const mat3 yuv_to_rgb_matrix = mat3(\n"
191 " 0, -0.34414, 1.77200, \n"
192 " 1.40200, -0.71414, 0);\n"
193 " const mat3 rgb_to_yuv_matrix = mat3(\n"
194 " 0.29900, -0.16874, 0.50000, \n"
195 " 0.58700, -0.33126, -0.41869, \n"
196 " 0.11400, 0.50000, -0.08131);\n"
197 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
198 " rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
199 " rgba.r += brightness;\n"
200 " rgba.r = rgba.r * contrast + offset;\n"
201 " rgba.rgb = yuv_to_rgb_matrix * rgba.rgb;\n"
202 " gl_FragColor = rgba;\n"
205 get_output()->to_texture();
206 get_output()->enable_opengl();
208 unsigned int shader_id = 0;
209 switch(get_output()->get_color_model())
214 shader_id = VFrame::make_shader(0,
215 brightness_yuvluma_frag,
218 shader_id = VFrame::make_shader(0,
224 shader_id = VFrame::make_shader(0,
225 brightness_rgbluma_frag,
228 shader_id = VFrame::make_shader(0,
237 glUseProgram(shader_id);
238 glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
239 glUniform1f(glGetUniformLocation(shader_id, "brightness"), config.brightness / 100);
240 float contrast = (config.contrast < 0) ?
241 (config.contrast + 100) / 100 :
242 (config.contrast + 25) / 25;
243 glUniform1f(glGetUniformLocation(shader_id, "contrast"), contrast);
244 float offset = 0.5 - contrast / 2;
245 glUniform1f(glGetUniformLocation(shader_id, "offset"), offset);
248 get_output()->init_screen();
249 get_output()->bind_texture(0);
253 get_output()->draw_texture();
255 get_output()->set_opengl_state(VFrame::SCREEN);
256 //printf("BrightnessMain::handle_opengl 100 %x\n", glGetError());
262 void BrightnessMain::update_gui()
266 if(load_configuration())
268 ((BrightnessWindow*)thread->window)->lock_window("BrightnessMain::update_gui");
269 ((BrightnessWindow*)thread->window)->brightness->update(config.brightness);
270 ((BrightnessWindow*)thread->window)->contrast->update(config.contrast);
271 ((BrightnessWindow*)thread->window)->luma->update(config.luma);
272 ((BrightnessWindow*)thread->window)->unlock_window();
278 void BrightnessMain::save_data(KeyFrame *keyframe)
282 // cause data to be stored directly in text
283 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
284 output.tag.set_title("BRIGHTNESS");
285 output.tag.set_property("BRIGHTNESS", config.brightness);
286 output.tag.set_property("CONTRAST", config.contrast);
287 output.tag.set_property("LUMA", config.luma);
289 output.tag.set_title("/BRIGHTNESS");
291 output.append_newline();
292 output.terminate_string();
295 void BrightnessMain::read_data(KeyFrame *keyframe)
299 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
305 result = input.read_tag();
309 if(input.tag.title_is("BRIGHTNESS"))
311 config.brightness = input.tag.get_property("BRIGHTNESS", config.brightness);
312 config.contrast = input.tag.get_property("CONTRAST", config.contrast);
313 config.luma = input.tag.get_property("LUMA", config.luma);
331 BrightnessPackage::BrightnessPackage()
339 BrightnessUnit::BrightnessUnit(BrightnessEngine *server, BrightnessMain *plugin)
342 this->plugin = plugin;
345 BrightnessUnit::~BrightnessUnit()
349 void BrightnessUnit::process_package(LoadPackage *package)
351 BrightnessPackage *pkg = (BrightnessPackage*)package;
354 VFrame *output = plugin->output;
355 VFrame *input = plugin->input;
361 #define DO_BRIGHTNESS(max, type, components, is_yuv) \
363 type **input_rows = (type**)input->get_rows(); \
364 type **output_rows = (type**)output->get_rows(); \
365 int row1 = pkg->row1; \
366 int row2 = pkg->row2; \
367 int width = output->get_w(); \
370 if(!EQUIV(plugin->config.brightness, 0)) \
372 int offset = (int)(plugin->config.brightness / 100 * max); \
373 /*printf("DO_BRIGHTNESS offset=%d\n", offset);*/ \
375 for(int i = row1; i < row2; i++) \
377 type *input_row = input_rows[i]; \
378 type *output_row = output_rows[i]; \
380 for(int j = 0; j < width; j++) \
382 r = input_row[j * components] + offset; \
386 g = input_row[j * components + 1] + offset; \
387 b = input_row[j * components + 2] + offset; \
397 output_row[j * components] = r; \
401 output_row[j * components + 1] = g; \
402 output_row[j * components + 2] = b; \
406 output_row[j * components + 1] = input_row[j * components + 1]; \
407 output_row[j * components + 2] = input_row[j * components + 2]; \
410 if(components == 4) \
411 output_row[j * components + 3] = input_row[j * components + 3]; \
415 /* Data to be processed is now in the output buffer */ \
416 input_rows = output_rows; \
419 if(!EQUIV(plugin->config.contrast, 0)) \
421 float contrast = (plugin->config.contrast < 0) ? \
422 (plugin->config.contrast + 100) / 100 : \
423 (plugin->config.contrast + 25) / 25; \
424 /*printf("DO_BRIGHTNESS contrast=%f\n", contrast);*/ \
426 int scalar = (int)(contrast * 0x100); \
427 int offset = (max << 8) / 2 - max * scalar / 2; \
430 for(int i = row1; i < row2; i++) \
432 type *input_row = input_rows[i]; \
433 type *output_row = output_rows[i]; \
435 if(plugin->config.luma) \
437 for(int j = 0; j < width; j++) \
441 y = input_row[j * components]; \
445 r = input_row[j * components]; \
446 g = input_row[j * components + 1]; \
447 b = input_row[j * components + 2]; \
450 BrightnessMain::yuv.rgb_to_yuv_8( \
460 BrightnessMain::yuv.rgb_to_yuv_16( \
471 y = (y * scalar + offset) >> 8; \
477 output_row[j * components] = y; \
478 output_row[j * components + 1] = input_row[j * components + 1]; \
479 output_row[j * components + 2] = input_row[j * components + 2]; \
485 BrightnessMain::yuv.yuv_to_rgb_8( \
495 BrightnessMain::yuv.yuv_to_rgb_16( \
503 input_row[j * components] = r; \
504 input_row[j * components + 1] = g; \
505 input_row[j * components + 2] = b; \
508 if(components == 4) \
509 output_row[j * components + 3] = input_row[j * components + 3]; \
514 for(int j = 0; j < width; j++) \
516 r = input_row[j * components]; \
517 g = input_row[j * components + 1]; \
518 b = input_row[j * components + 2]; \
520 r = (r * scalar + offset) >> 8; \
521 g = (g * scalar + offset) >> 8; \
522 b = (b * scalar + offset) >> 8; \
528 output_row[j * components] = r; \
529 output_row[j * components + 1] = g; \
530 output_row[j * components + 2] = b; \
532 if(components == 4) \
533 output_row[j * components + 3] = input_row[j * components + 3]; \
542 #define DO_BRIGHTNESS_F(components) \
544 float **input_rows = (float**)input->get_rows(); \
545 float **output_rows = (float**)output->get_rows(); \
546 int row1 = pkg->row1; \
547 int row2 = pkg->row2; \
548 int width = output->get_w(); \
551 if(!EQUIV(plugin->config.brightness, 0)) \
553 float offset = plugin->config.brightness / 100; \
555 for(int i = row1; i < row2; i++) \
557 float *input_row = input_rows[i]; \
558 float *output_row = output_rows[i]; \
560 for(int j = 0; j < width; j++) \
562 r = input_row[j * components] + offset; \
563 g = input_row[j * components + 1] + offset; \
564 b = input_row[j * components + 2] + offset; \
566 output_row[j * components] = r; \
567 output_row[j * components + 1] = g; \
568 output_row[j * components + 2] = b; \
569 if(components == 4) \
570 output_row[j * components + 3] = input_row[j * components + 3]; \
574 /* Data to be processed is now in the output buffer */ \
575 input_rows = output_rows; \
578 if(!EQUIV(plugin->config.contrast, 0)) \
580 float contrast = (plugin->config.contrast < 0) ? \
581 (plugin->config.contrast + 100) / 100 : \
582 (plugin->config.contrast + 25) / 25; \
584 /* Shift black level down so shadows get darker instead of lighter */ \
585 float offset = 0.5 - contrast / 2; \
588 for(int i = row1; i < row2; i++) \
590 float *input_row = input_rows[i]; \
591 float *output_row = output_rows[i]; \
593 if(plugin->config.luma) \
595 for(int j = 0; j < width; j++) \
597 r = input_row[j * components]; \
598 g = input_row[j * components + 1]; \
599 b = input_row[j * components + 2]; \
608 y = y * contrast + offset; \
618 input_row[j * components] = r; \
619 input_row[j * components + 1] = g; \
620 input_row[j * components + 2] = b; \
622 if(components == 4) \
623 output_row[j * components + 3] = input_row[j * components + 3]; \
628 for(int j = 0; j < width; j++) \
630 r = input_row[j * components]; \
631 g = input_row[j * components + 1]; \
632 b = input_row[j * components + 2]; \
634 r = r * contrast + offset; \
635 g = g * contrast + offset; \
636 b = b * contrast + offset; \
638 output_row[j * components] = r; \
639 output_row[j * components + 1] = g; \
640 output_row[j * components + 2] = b; \
642 if(components == 4) \
643 output_row[j * components + 3] = input_row[j * components + 3]; \
651 switch(input->get_color_model())
654 DO_BRIGHTNESS(0xff, unsigned char, 3, 0)
662 DO_BRIGHTNESS(0xff, unsigned char, 3, 1)
666 DO_BRIGHTNESS(0xff, unsigned char, 4, 0)
674 DO_BRIGHTNESS(0xff, unsigned char, 4, 1)
678 DO_BRIGHTNESS(0xffff, uint16_t, 3, 0)
682 DO_BRIGHTNESS(0xffff, uint16_t, 3, 1)
685 case BC_RGBA16161616:
686 DO_BRIGHTNESS(0xffff, uint16_t, 4, 0)
689 case BC_YUVA16161616:
690 DO_BRIGHTNESS(0xffff, uint16_t, 4, 1)
709 BrightnessEngine::BrightnessEngine(BrightnessMain *plugin, int cpus)
710 : LoadServer(cpus, cpus)
712 this->plugin = plugin;
715 BrightnessEngine::~BrightnessEngine()
720 void BrightnessEngine::init_packages()
722 for(int i = 0; i < LoadServer::get_total_packages(); i++)
724 BrightnessPackage *package = (BrightnessPackage*)LoadServer::get_package(i);
725 package->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
726 package->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
730 LoadClient* BrightnessEngine::new_client()
732 return new BrightnessUnit(this, plugin);
735 LoadPackage* BrightnessEngine::new_package()
737 return new BrightnessPackage;