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
22 #include "brightness.h"
28 #include "playback3d.h"
35 REGISTER_PLUGIN(BrightnessMain)
39 BrightnessConfig::BrightnessConfig()
46 int BrightnessConfig::equivalent(BrightnessConfig &that)
48 return (brightness == that.brightness &&
49 contrast == that.contrast &&
53 void BrightnessConfig::copy_from(BrightnessConfig &that)
55 brightness = that.brightness;
56 contrast = that.contrast;
60 void BrightnessConfig::interpolate(BrightnessConfig &prev,
61 BrightnessConfig &next,
64 int64_t current_frame)
66 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
67 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
69 this->brightness = prev.brightness * prev_scale + next.brightness * next_scale;
70 this->contrast = prev.contrast * prev_scale + next.contrast * next_scale;
71 this->luma = (int)(prev.luma * prev_scale + next.luma * next_scale);
82 BrightnessMain::BrightnessMain(PluginServer *server)
83 : PluginVClient(server)
90 BrightnessMain::~BrightnessMain()
93 if(engine) delete engine;
96 const char* BrightnessMain::plugin_title() { return N_("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"
187 "uniform mat3 yuv_to_rgb_matrix;\n"
188 "uniform mat3 rgb_to_yuv_matrix;\n"
192 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
193 " rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
194 " rgba.r += brightness;\n"
195 " rgba.r = rgba.r * contrast + offset;\n"
196 " rgba.rgb = yuv_to_rgb_matrix * rgba.rgb;\n"
197 " gl_FragColor = rgba;\n"
200 get_output()->to_texture();
201 get_output()->enable_opengl();
203 const char *brightness_frag = BC_CModels::is_yuv(get_output()->get_color_model()) ?
204 (config.luma ? (need_matrix = 0, brightness_yuvluma_frag) : brightness_yuv_frag) :
205 (config.luma ? (need_matrix = 1, brightness_rgbluma_frag) : brightness_rgb_frag) ;
207 unsigned int shader_id = VFrame::make_shader(0, brightness_frag, 0);
208 if( shader_id > 0 ) {
209 glUseProgram(shader_id);
210 glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
211 glUniform1f(glGetUniformLocation(shader_id, "brightness"), config.brightness / 100);
212 float contrast = (config.contrast < 0) ?
213 (config.contrast + 100) / 100 :
214 (config.contrast + 25) / 25;
215 glUniform1f(glGetUniformLocation(shader_id, "contrast"), contrast);
216 float offset = 0.5 - contrast / 2;
217 glUniform1f(glGetUniformLocation(shader_id, "offset"), offset);
219 BC_GL_MATRIX(shader_id, yuv_to_rgb_matrix);
220 BC_GL_MATRIX(shader_id, rgb_to_yuv_matrix);
224 get_output()->init_screen();
225 get_output()->bind_texture(0);
229 get_output()->draw_texture();
231 get_output()->set_opengl_state(VFrame::SCREEN);
232 //printf("BrightnessMain::handle_opengl 100 %x\n", glGetError());
238 void BrightnessMain::update_gui()
242 if(load_configuration())
244 ((BrightnessWindow*)thread->window)->lock_window("BrightnessMain::update_gui");
245 ((BrightnessWindow*)thread->window)->brightness->update(config.brightness);
246 ((BrightnessWindow*)thread->window)->contrast->update(config.contrast);
247 ((BrightnessWindow*)thread->window)->luma->update(config.luma);
248 ((BrightnessWindow*)thread->window)->unlock_window();
254 void BrightnessMain::save_data(KeyFrame *keyframe)
258 // cause data to be stored directly in text
259 output.set_shared_output(keyframe->xbuf);
260 output.tag.set_title("BRIGHTNESS");
261 output.tag.set_property("BRIGHTNESS", config.brightness);
262 output.tag.set_property("CONTRAST", config.contrast);
263 output.tag.set_property("LUMA", config.luma);
265 output.tag.set_title("/BRIGHTNESS");
267 output.append_newline();
268 output.terminate_string();
271 void BrightnessMain::read_data(KeyFrame *keyframe)
275 input.set_shared_input(keyframe->xbuf);
281 result = input.read_tag();
285 if(input.tag.title_is("BRIGHTNESS"))
287 config.brightness = input.tag.get_property("BRIGHTNESS", config.brightness);
288 config.contrast = input.tag.get_property("CONTRAST", config.contrast);
289 config.luma = input.tag.get_property("LUMA", config.luma);
307 BrightnessPackage::BrightnessPackage()
315 BrightnessUnit::BrightnessUnit(BrightnessEngine *server, BrightnessMain *plugin)
318 this->plugin = plugin;
321 BrightnessUnit::~BrightnessUnit()
325 void BrightnessUnit::process_package(LoadPackage *package)
327 BrightnessPackage *pkg = (BrightnessPackage*)package;
330 VFrame *output = plugin->output;
331 VFrame *input = plugin->input;
337 #define DO_BRIGHTNESS(max, type, components, is_yuv) \
339 type **input_rows = (type**)input->get_rows(); \
340 type **output_rows = (type**)output->get_rows(); \
341 int row1 = pkg->row1; \
342 int row2 = pkg->row2; \
343 int width = output->get_w(); \
346 if(!EQUIV(plugin->config.brightness, 0)) \
348 int offset = (int)(plugin->config.brightness / 100 * max); \
349 /*printf("DO_BRIGHTNESS offset=%d\n", offset);*/ \
351 for(int i = row1; i < row2; i++) \
353 type *input_row = input_rows[i]; \
354 type *output_row = output_rows[i]; \
356 for(int j = 0; j < width; j++) \
358 r = input_row[j * components] + offset; \
362 g = input_row[j * components + 1] + offset; \
363 b = input_row[j * components + 2] + offset; \
373 output_row[j * components] = r; \
377 output_row[j * components + 1] = g; \
378 output_row[j * components + 2] = b; \
382 output_row[j * components + 1] = input_row[j * components + 1]; \
383 output_row[j * components + 2] = input_row[j * components + 2]; \
386 if(components == 4) \
387 output_row[j * components + 3] = input_row[j * components + 3]; \
391 /* Data to be processed is now in the output buffer */ \
392 input_rows = output_rows; \
395 if(!EQUIV(plugin->config.contrast, 0)) \
397 float contrast = (plugin->config.contrast < 0) ? \
398 (plugin->config.contrast + 100) / 100 : \
399 (plugin->config.contrast + 25) / 25; \
400 /*printf("DO_BRIGHTNESS contrast=%f\n", contrast);*/ \
402 int scalar = (int)(contrast * 0x100); \
403 int offset = (max << 8) / 2 - max * scalar / 2; \
406 for(int i = row1; i < row2; i++) \
408 type *input_row = input_rows[i]; \
409 type *output_row = output_rows[i]; \
411 if(plugin->config.luma) \
413 for(int j = 0; j < width; j++) \
417 y = input_row[j * components]; \
421 r = input_row[j * components]; \
422 g = input_row[j * components + 1]; \
423 b = input_row[j * components + 2]; \
426 YUV::yuv.rgb_to_yuv_8( \
436 YUV::yuv.rgb_to_yuv_16( \
447 y = (y * scalar + offset) >> 8; \
453 output_row[j * components] = y; \
454 output_row[j * components + 1] = input_row[j * components + 1]; \
455 output_row[j * components + 2] = input_row[j * components + 2]; \
461 YUV::yuv.yuv_to_rgb_8( \
471 YUV::yuv.yuv_to_rgb_16( \
479 input_row[j * components] = r; \
480 input_row[j * components + 1] = g; \
481 input_row[j * components + 2] = b; \
484 if(components == 4) \
485 output_row[j * components + 3] = input_row[j * components + 3]; \
490 for(int j = 0; j < width; j++) \
492 r = input_row[j * components]; \
493 g = input_row[j * components + 1]; \
494 b = input_row[j * components + 2]; \
496 r = (r * scalar + offset) >> 8; \
497 g = (g * scalar + offset) >> 8; \
498 b = (b * scalar + offset) >> 8; \
504 output_row[j * components] = r; \
505 output_row[j * components + 1] = g; \
506 output_row[j * components + 2] = b; \
508 if(components == 4) \
509 output_row[j * components + 3] = input_row[j * components + 3]; \
518 #define DO_BRIGHTNESS_F(components) \
520 float **input_rows = (float**)input->get_rows(); \
521 float **output_rows = (float**)output->get_rows(); \
522 int row1 = pkg->row1; \
523 int row2 = pkg->row2; \
524 int width = output->get_w(); \
527 if(!EQUIV(plugin->config.brightness, 0)) \
529 float offset = plugin->config.brightness / 100; \
531 for(int i = row1; i < row2; i++) \
533 float *input_row = input_rows[i]; \
534 float *output_row = output_rows[i]; \
536 for(int j = 0; j < width; j++) \
538 r = input_row[j * components] + offset; \
539 g = input_row[j * components + 1] + offset; \
540 b = input_row[j * components + 2] + offset; \
542 output_row[j * components] = r; \
543 output_row[j * components + 1] = g; \
544 output_row[j * components + 2] = b; \
545 if(components == 4) \
546 output_row[j * components + 3] = input_row[j * components + 3]; \
550 /* Data to be processed is now in the output buffer */ \
551 input_rows = output_rows; \
554 if(!EQUIV(plugin->config.contrast, 0)) \
556 float contrast = (plugin->config.contrast < 0) ? \
557 (plugin->config.contrast + 100) / 100 : \
558 (plugin->config.contrast + 25) / 25; \
560 /* Shift black level down so shadows get darker instead of lighter */ \
561 float offset = 0.5 - contrast / 2; \
564 for(int i = row1; i < row2; i++) \
566 float *input_row = input_rows[i]; \
567 float *output_row = output_rows[i]; \
569 if(plugin->config.luma) \
571 for(int j = 0; j < width; j++) \
573 r = input_row[j * components]; \
574 g = input_row[j * components + 1]; \
575 b = input_row[j * components + 2]; \
576 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v); \
577 y = y * contrast + offset; \
578 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v); \
579 input_row[j * components] = r; \
580 input_row[j * components + 1] = g; \
581 input_row[j * components + 2] = b; \
583 if(components == 4) \
584 output_row[j * components + 3] = input_row[j * components + 3]; \
589 for(int j = 0; j < width; j++) \
591 r = input_row[j * components]; \
592 g = input_row[j * components + 1]; \
593 b = input_row[j * components + 2]; \
595 r = r * contrast + offset; \
596 g = g * contrast + offset; \
597 b = b * contrast + offset; \
599 output_row[j * components] = r; \
600 output_row[j * components + 1] = g; \
601 output_row[j * components + 2] = b; \
603 if(components == 4) \
604 output_row[j * components + 3] = input_row[j * components + 3]; \
612 switch(input->get_color_model())
615 DO_BRIGHTNESS(0xff, unsigned char, 3, 0)
623 DO_BRIGHTNESS(0xff, unsigned char, 3, 1)
627 DO_BRIGHTNESS(0xff, unsigned char, 4, 0)
635 DO_BRIGHTNESS(0xff, unsigned char, 4, 1)
639 DO_BRIGHTNESS(0xffff, uint16_t, 3, 0)
643 DO_BRIGHTNESS(0xffff, uint16_t, 3, 1)
646 case BC_RGBA16161616:
647 DO_BRIGHTNESS(0xffff, uint16_t, 4, 0)
650 case BC_YUVA16161616:
651 DO_BRIGHTNESS(0xffff, uint16_t, 4, 1)
670 BrightnessEngine::BrightnessEngine(BrightnessMain *plugin, int cpus)
671 : LoadServer(cpus, cpus)
673 this->plugin = plugin;
676 BrightnessEngine::~BrightnessEngine()
681 void BrightnessEngine::init_packages()
683 for(int i = 0; i < LoadServer::get_total_packages(); i++)
685 BrightnessPackage *package = (BrightnessPackage*)LoadServer::get_package(i);
686 package->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
687 package->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
691 LoadClient* BrightnessEngine::new_client()
693 return new BrightnessUnit(this, plugin);
696 LoadPackage* BrightnessEngine::new_package()
698 return new BrightnessPackage;