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.terminate_string();
292 void BrightnessMain::read_data(KeyFrame *keyframe)
296 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
302 result = input.read_tag();
306 if(input.tag.title_is("BRIGHTNESS"))
308 config.brightness = input.tag.get_property("BRIGHTNESS", config.brightness);
309 config.contrast = input.tag.get_property("CONTRAST", config.contrast);
310 config.luma = input.tag.get_property("LUMA", config.luma);
328 BrightnessPackage::BrightnessPackage()
336 BrightnessUnit::BrightnessUnit(BrightnessEngine *server, BrightnessMain *plugin)
339 this->plugin = plugin;
342 BrightnessUnit::~BrightnessUnit()
346 void BrightnessUnit::process_package(LoadPackage *package)
348 BrightnessPackage *pkg = (BrightnessPackage*)package;
351 VFrame *output = plugin->output;
352 VFrame *input = plugin->input;
358 #define DO_BRIGHTNESS(max, type, components, is_yuv) \
360 type **input_rows = (type**)input->get_rows(); \
361 type **output_rows = (type**)output->get_rows(); \
362 int row1 = pkg->row1; \
363 int row2 = pkg->row2; \
364 int width = output->get_w(); \
367 if(!EQUIV(plugin->config.brightness, 0)) \
369 int offset = (int)(plugin->config.brightness / 100 * max); \
370 /*printf("DO_BRIGHTNESS offset=%d\n", offset);*/ \
372 for(int i = row1; i < row2; i++) \
374 type *input_row = input_rows[i]; \
375 type *output_row = output_rows[i]; \
377 for(int j = 0; j < width; j++) \
379 r = input_row[j * components] + offset; \
383 g = input_row[j * components + 1] + offset; \
384 b = input_row[j * components + 2] + offset; \
394 output_row[j * components] = r; \
398 output_row[j * components + 1] = g; \
399 output_row[j * components + 2] = b; \
403 output_row[j * components + 1] = input_row[j * components + 1]; \
404 output_row[j * components + 2] = input_row[j * components + 2]; \
407 if(components == 4) \
408 output_row[j * components + 3] = input_row[j * components + 3]; \
412 /* Data to be processed is now in the output buffer */ \
413 input_rows = output_rows; \
416 if(!EQUIV(plugin->config.contrast, 0)) \
418 float contrast = (plugin->config.contrast < 0) ? \
419 (plugin->config.contrast + 100) / 100 : \
420 (plugin->config.contrast + 25) / 25; \
421 /*printf("DO_BRIGHTNESS contrast=%f\n", contrast);*/ \
423 int scalar = (int)(contrast * 0x100); \
424 int offset = (max << 8) / 2 - max * scalar / 2; \
427 for(int i = row1; i < row2; i++) \
429 type *input_row = input_rows[i]; \
430 type *output_row = output_rows[i]; \
432 if(plugin->config.luma) \
434 for(int j = 0; j < width; j++) \
438 y = input_row[j * components]; \
442 r = input_row[j * components]; \
443 g = input_row[j * components + 1]; \
444 b = input_row[j * components + 2]; \
447 BrightnessMain::yuv.rgb_to_yuv_8( \
457 BrightnessMain::yuv.rgb_to_yuv_16( \
468 y = (y * scalar + offset) >> 8; \
474 output_row[j * components] = y; \
475 output_row[j * components + 1] = input_row[j * components + 1]; \
476 output_row[j * components + 2] = input_row[j * components + 2]; \
482 BrightnessMain::yuv.yuv_to_rgb_8( \
492 BrightnessMain::yuv.yuv_to_rgb_16( \
500 input_row[j * components] = r; \
501 input_row[j * components + 1] = g; \
502 input_row[j * components + 2] = b; \
505 if(components == 4) \
506 output_row[j * components + 3] = input_row[j * components + 3]; \
511 for(int j = 0; j < width; j++) \
513 r = input_row[j * components]; \
514 g = input_row[j * components + 1]; \
515 b = input_row[j * components + 2]; \
517 r = (r * scalar + offset) >> 8; \
518 g = (g * scalar + offset) >> 8; \
519 b = (b * scalar + offset) >> 8; \
525 output_row[j * components] = r; \
526 output_row[j * components + 1] = g; \
527 output_row[j * components + 2] = b; \
529 if(components == 4) \
530 output_row[j * components + 3] = input_row[j * components + 3]; \
539 #define DO_BRIGHTNESS_F(components) \
541 float **input_rows = (float**)input->get_rows(); \
542 float **output_rows = (float**)output->get_rows(); \
543 int row1 = pkg->row1; \
544 int row2 = pkg->row2; \
545 int width = output->get_w(); \
548 if(!EQUIV(plugin->config.brightness, 0)) \
550 float offset = plugin->config.brightness / 100; \
552 for(int i = row1; i < row2; i++) \
554 float *input_row = input_rows[i]; \
555 float *output_row = output_rows[i]; \
557 for(int j = 0; j < width; j++) \
559 r = input_row[j * components] + offset; \
560 g = input_row[j * components + 1] + offset; \
561 b = input_row[j * components + 2] + offset; \
563 output_row[j * components] = r; \
564 output_row[j * components + 1] = g; \
565 output_row[j * components + 2] = b; \
566 if(components == 4) \
567 output_row[j * components + 3] = input_row[j * components + 3]; \
571 /* Data to be processed is now in the output buffer */ \
572 input_rows = output_rows; \
575 if(!EQUIV(plugin->config.contrast, 0)) \
577 float contrast = (plugin->config.contrast < 0) ? \
578 (plugin->config.contrast + 100) / 100 : \
579 (plugin->config.contrast + 25) / 25; \
581 /* Shift black level down so shadows get darker instead of lighter */ \
582 float offset = 0.5 - contrast / 2; \
585 for(int i = row1; i < row2; i++) \
587 float *input_row = input_rows[i]; \
588 float *output_row = output_rows[i]; \
590 if(plugin->config.luma) \
592 for(int j = 0; j < width; j++) \
594 r = input_row[j * components]; \
595 g = input_row[j * components + 1]; \
596 b = input_row[j * components + 2]; \
605 y = y * contrast + offset; \
615 input_row[j * components] = r; \
616 input_row[j * components + 1] = g; \
617 input_row[j * components + 2] = b; \
619 if(components == 4) \
620 output_row[j * components + 3] = input_row[j * components + 3]; \
625 for(int j = 0; j < width; j++) \
627 r = input_row[j * components]; \
628 g = input_row[j * components + 1]; \
629 b = input_row[j * components + 2]; \
631 r = r * contrast + offset; \
632 g = g * contrast + offset; \
633 b = b * contrast + offset; \
635 output_row[j * components] = r; \
636 output_row[j * components + 1] = g; \
637 output_row[j * components + 2] = b; \
639 if(components == 4) \
640 output_row[j * components + 3] = input_row[j * components + 3]; \
648 switch(input->get_color_model())
651 DO_BRIGHTNESS(0xff, unsigned char, 3, 0)
659 DO_BRIGHTNESS(0xff, unsigned char, 3, 1)
663 DO_BRIGHTNESS(0xff, unsigned char, 4, 0)
671 DO_BRIGHTNESS(0xff, unsigned char, 4, 1)
675 DO_BRIGHTNESS(0xffff, uint16_t, 3, 0)
679 DO_BRIGHTNESS(0xffff, uint16_t, 3, 1)
682 case BC_RGBA16161616:
683 DO_BRIGHTNESS(0xffff, uint16_t, 4, 0)
686 case BC_YUVA16161616:
687 DO_BRIGHTNESS(0xffff, uint16_t, 4, 1)
706 BrightnessEngine::BrightnessEngine(BrightnessMain *plugin, int cpus)
707 : LoadServer(cpus, cpus)
709 this->plugin = plugin;
712 BrightnessEngine::~BrightnessEngine()
717 void BrightnessEngine::init_packages()
719 for(int i = 0; i < LoadServer::get_total_packages(); i++)
721 BrightnessPackage *package = (BrightnessPackage*)LoadServer::get_package(i);
722 package->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
723 package->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
727 LoadClient* BrightnessEngine::new_client()
729 return new BrightnessUnit(this, plugin);
732 LoadPackage* BrightnessEngine::new_package()
734 return new BrightnessPackage;