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)
38 BrightnessConfig::BrightnessConfig()
43 void BrightnessConfig::reset(int clear)
46 case RESET_CONTRAST : contrast = 0;
48 case RESET_BRIGHTNESS : brightness = 0;
59 int BrightnessConfig::equivalent(BrightnessConfig &that)
61 return (brightness == that.brightness &&
62 contrast == that.contrast &&
66 void BrightnessConfig::copy_from(BrightnessConfig &that)
68 brightness = that.brightness;
69 contrast = that.contrast;
73 void BrightnessConfig::interpolate(BrightnessConfig &prev,
74 BrightnessConfig &next,
77 int64_t current_frame)
79 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
80 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
82 this->brightness = prev.brightness * prev_scale + next.brightness * next_scale;
83 this->contrast = prev.contrast * prev_scale + next.contrast * next_scale;
84 this->luma = (int)(prev.luma * prev_scale + next.luma * next_scale);
95 BrightnessMain::BrightnessMain(PluginServer *server)
96 : PluginVClient(server)
103 BrightnessMain::~BrightnessMain()
106 if(engine) delete engine;
109 const char* BrightnessMain::plugin_title() { return N_("Brightness/Contrast"); }
110 int BrightnessMain::is_realtime() { return 1; }
112 NEW_WINDOW_MACRO(BrightnessMain, BrightnessWindow)
113 LOAD_CONFIGURATION_MACRO(BrightnessMain, BrightnessConfig)
115 int BrightnessMain::process_buffer(VFrame *frame,
116 int64_t start_position,
119 load_configuration();
138 if(!engine) engine = new BrightnessEngine(this, PluginClient::smp + 1);
141 this->output = frame;
143 if(!EQUIV(config.brightness, 0) || !EQUIV(config.contrast, 0))
145 engine->process_packages();
151 int BrightnessMain::handle_opengl()
154 static const char *brightness_yuvluma_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.r = yuva.r * contrast + offset;\n"
164 " gl_FragColor = yuva;\n"
167 static const char *brightness_yuv_frag =
168 "uniform sampler2D tex;\n"
169 "uniform float brightness;\n"
170 "uniform float contrast;\n"
171 "uniform float offset;\n"
174 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
175 " yuva.r += brightness;\n"
176 " yuva.rgb *= vec3(contrast, contrast, contrast);\n"
177 " yuva.rgb += vec3(offset, offset, offset);\n"
178 " gl_FragColor = yuva;\n"
181 static const char *brightness_rgb_frag =
182 "uniform sampler2D tex;\n"
183 "uniform float brightness;\n"
184 "uniform float contrast;\n"
185 "uniform float offset;\n"
188 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
189 " rgba.rgb += vec3(brightness, brightness, brightness);\n"
190 " rgba.rgb *= vec3(contrast, contrast, contrast);\n"
191 " rgba.rgb += vec3(offset, offset, offset);\n"
192 " gl_FragColor = rgba;\n"
195 static const char *brightness_rgbluma_frag =
196 "uniform sampler2D tex;\n"
197 "uniform float brightness;\n"
198 "uniform float contrast;\n"
199 "uniform float offset;\n"
200 "uniform mat3 yuv_to_rgb_matrix;\n"
201 "uniform mat3 rgb_to_yuv_matrix;\n"
205 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
206 " rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
207 " rgba.r += brightness;\n"
208 " rgba.r = rgba.r * contrast + offset;\n"
209 " rgba.rgb = yuv_to_rgb_matrix * rgba.rgb;\n"
210 " gl_FragColor = rgba;\n"
213 get_output()->to_texture();
214 get_output()->enable_opengl();
216 const char *brightness_frag = BC_CModels::is_yuv(get_output()->get_color_model()) ?
217 (config.luma ? (need_matrix = 0, brightness_yuvluma_frag) : brightness_yuv_frag) :
218 (config.luma ? (need_matrix = 1, brightness_rgbluma_frag) : brightness_rgb_frag) ;
220 unsigned int shader_id = VFrame::make_shader(0, brightness_frag, 0);
221 if( shader_id > 0 ) {
222 glUseProgram(shader_id);
223 glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
224 glUniform1f(glGetUniformLocation(shader_id, "brightness"), config.brightness / 100);
225 float contrast = (config.contrast < 0) ?
226 (config.contrast + 100) / 100 :
227 (config.contrast + 25) / 25;
228 glUniform1f(glGetUniformLocation(shader_id, "contrast"), contrast);
229 float offset = 0.5 - contrast / 2;
230 glUniform1f(glGetUniformLocation(shader_id, "offset"), offset);
232 BC_GL_MATRIX(shader_id, yuv_to_rgb_matrix);
233 BC_GL_MATRIX(shader_id, rgb_to_yuv_matrix);
237 get_output()->init_screen();
238 get_output()->bind_texture(0);
242 get_output()->draw_texture();
244 get_output()->set_opengl_state(VFrame::SCREEN);
245 //printf("BrightnessMain::handle_opengl 100 %x\n", glGetError());
251 void BrightnessMain::update_gui()
255 if(load_configuration())
257 ((BrightnessWindow*)thread->window)->lock_window("BrightnessMain::update_gui");
258 ((BrightnessWindow*)thread->window)->brightness->update(config.brightness);
259 ((BrightnessWindow*)thread->window)->contrast->update(config.contrast);
260 ((BrightnessWindow*)thread->window)->luma->update(config.luma);
261 ((BrightnessWindow*)thread->window)->unlock_window();
267 void BrightnessMain::save_data(KeyFrame *keyframe)
271 // cause data to be stored directly in text
272 output.set_shared_output(keyframe->xbuf);
273 output.tag.set_title("BRIGHTNESS");
274 output.tag.set_property("BRIGHTNESS", config.brightness);
275 output.tag.set_property("CONTRAST", config.contrast);
276 output.tag.set_property("LUMA", config.luma);
278 output.tag.set_title("/BRIGHTNESS");
280 output.append_newline();
281 output.terminate_string();
284 void BrightnessMain::read_data(KeyFrame *keyframe)
288 input.set_shared_input(keyframe->xbuf);
294 result = input.read_tag();
298 if(input.tag.title_is("BRIGHTNESS"))
300 config.brightness = input.tag.get_property("BRIGHTNESS", config.brightness);
301 config.contrast = input.tag.get_property("CONTRAST", config.contrast);
302 config.luma = input.tag.get_property("LUMA", config.luma);
320 BrightnessPackage::BrightnessPackage()
328 BrightnessUnit::BrightnessUnit(BrightnessEngine *server, BrightnessMain *plugin)
331 this->plugin = plugin;
334 BrightnessUnit::~BrightnessUnit()
338 void BrightnessUnit::process_package(LoadPackage *package)
340 BrightnessPackage *pkg = (BrightnessPackage*)package;
343 VFrame *output = plugin->output;
344 VFrame *input = plugin->input;
350 #define DO_BRIGHTNESS(max, type, components, is_yuv) \
352 type **input_rows = (type**)input->get_rows(); \
353 type **output_rows = (type**)output->get_rows(); \
354 int row1 = pkg->row1; \
355 int row2 = pkg->row2; \
356 int width = output->get_w(); \
359 if(!EQUIV(plugin->config.brightness, 0)) \
361 int offset = (int)(plugin->config.brightness / 100 * max); \
362 /*printf("DO_BRIGHTNESS offset=%d\n", offset);*/ \
364 for(int i = row1; i < row2; i++) \
366 type *input_row = input_rows[i]; \
367 type *output_row = output_rows[i]; \
369 for(int j = 0; j < width; j++) \
371 r = input_row[j * components] + offset; \
375 g = input_row[j * components + 1] + offset; \
376 b = input_row[j * components + 2] + offset; \
386 output_row[j * components] = r; \
390 output_row[j * components + 1] = g; \
391 output_row[j * components + 2] = b; \
395 output_row[j * components + 1] = input_row[j * components + 1]; \
396 output_row[j * components + 2] = input_row[j * components + 2]; \
399 if(components == 4) \
400 output_row[j * components + 3] = input_row[j * components + 3]; \
404 /* Data to be processed is now in the output buffer */ \
405 input_rows = output_rows; \
408 if(!EQUIV(plugin->config.contrast, 0)) \
410 float contrast = (plugin->config.contrast < 0) ? \
411 (plugin->config.contrast + 100) / 100 : \
412 (plugin->config.contrast + 25) / 25; \
413 /*printf("DO_BRIGHTNESS contrast=%f\n", contrast);*/ \
415 int scalar = (int)(contrast * 0x100); \
416 int offset = (max << 8) / 2 - max * scalar / 2; \
419 for(int i = row1; i < row2; i++) \
421 type *input_row = input_rows[i]; \
422 type *output_row = output_rows[i]; \
424 if(plugin->config.luma) \
426 for(int j = 0; j < width; j++) \
430 y = input_row[j * components]; \
434 r = input_row[j * components]; \
435 g = input_row[j * components + 1]; \
436 b = input_row[j * components + 2]; \
439 YUV::yuv.rgb_to_yuv_8( \
449 YUV::yuv.rgb_to_yuv_16( \
460 y = (y * scalar + offset) >> 8; \
466 output_row[j * components] = y; \
467 output_row[j * components + 1] = input_row[j * components + 1]; \
468 output_row[j * components + 2] = input_row[j * components + 2]; \
474 YUV::yuv.yuv_to_rgb_8( \
484 YUV::yuv.yuv_to_rgb_16( \
492 input_row[j * components] = r; \
493 input_row[j * components + 1] = g; \
494 input_row[j * components + 2] = b; \
497 if(components == 4) \
498 output_row[j * components + 3] = input_row[j * components + 3]; \
503 for(int j = 0; j < width; j++) \
505 r = input_row[j * components]; \
506 g = input_row[j * components + 1]; \
507 b = input_row[j * components + 2]; \
509 r = (r * scalar + offset) >> 8; \
510 g = (g * scalar + offset) >> 8; \
511 b = (b * scalar + offset) >> 8; \
517 output_row[j * components] = r; \
518 output_row[j * components + 1] = g; \
519 output_row[j * components + 2] = b; \
521 if(components == 4) \
522 output_row[j * components + 3] = input_row[j * components + 3]; \
531 #define DO_BRIGHTNESS_F(components) \
533 float **input_rows = (float**)input->get_rows(); \
534 float **output_rows = (float**)output->get_rows(); \
535 int row1 = pkg->row1; \
536 int row2 = pkg->row2; \
537 int width = output->get_w(); \
540 if(!EQUIV(plugin->config.brightness, 0)) \
542 float offset = plugin->config.brightness / 100; \
544 for(int i = row1; i < row2; i++) \
546 float *input_row = input_rows[i]; \
547 float *output_row = output_rows[i]; \
549 for(int j = 0; j < width; j++) \
551 r = input_row[j * components] + offset; \
552 g = input_row[j * components + 1] + offset; \
553 b = input_row[j * components + 2] + offset; \
555 output_row[j * components] = r; \
556 output_row[j * components + 1] = g; \
557 output_row[j * components + 2] = b; \
558 if(components == 4) \
559 output_row[j * components + 3] = input_row[j * components + 3]; \
563 /* Data to be processed is now in the output buffer */ \
564 input_rows = output_rows; \
567 if(!EQUIV(plugin->config.contrast, 0)) \
569 float contrast = (plugin->config.contrast < 0) ? \
570 (plugin->config.contrast + 100) / 100 : \
571 (plugin->config.contrast + 25) / 25; \
573 /* Shift black level down so shadows get darker instead of lighter */ \
574 float offset = 0.5 - contrast / 2; \
577 for(int i = row1; i < row2; i++) \
579 float *input_row = input_rows[i]; \
580 float *output_row = output_rows[i]; \
582 if(plugin->config.luma) \
584 for(int j = 0; j < width; j++) \
586 r = input_row[j * components]; \
587 g = input_row[j * components + 1]; \
588 b = input_row[j * components + 2]; \
589 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v); \
590 y = y * contrast + offset; \
591 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v); \
592 input_row[j * components] = r; \
593 input_row[j * components + 1] = g; \
594 input_row[j * components + 2] = b; \
596 if(components == 4) \
597 output_row[j * components + 3] = input_row[j * components + 3]; \
602 for(int j = 0; j < width; j++) \
604 r = input_row[j * components]; \
605 g = input_row[j * components + 1]; \
606 b = input_row[j * components + 2]; \
608 r = r * contrast + offset; \
609 g = g * contrast + offset; \
610 b = b * contrast + offset; \
612 output_row[j * components] = r; \
613 output_row[j * components + 1] = g; \
614 output_row[j * components + 2] = b; \
616 if(components == 4) \
617 output_row[j * components + 3] = input_row[j * components + 3]; \
625 switch(input->get_color_model())
628 DO_BRIGHTNESS(0xff, unsigned char, 3, 0)
636 DO_BRIGHTNESS(0xff, unsigned char, 3, 1)
640 DO_BRIGHTNESS(0xff, unsigned char, 4, 0)
648 DO_BRIGHTNESS(0xff, unsigned char, 4, 1)
652 DO_BRIGHTNESS(0xffff, uint16_t, 3, 0)
656 DO_BRIGHTNESS(0xffff, uint16_t, 3, 1)
659 case BC_RGBA16161616:
660 DO_BRIGHTNESS(0xffff, uint16_t, 4, 0)
663 case BC_YUVA16161616:
664 DO_BRIGHTNESS(0xffff, uint16_t, 4, 1)
683 BrightnessEngine::BrightnessEngine(BrightnessMain *plugin, int cpus)
684 : LoadServer(cpus, cpus)
686 this->plugin = plugin;
689 BrightnessEngine::~BrightnessEngine()
694 void BrightnessEngine::init_packages()
696 for(int i = 0; i < LoadServer::get_total_packages(); i++)
698 BrightnessPackage *package = (BrightnessPackage*)LoadServer::get_package(i);
699 package->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
700 package->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
704 LoadClient* BrightnessEngine::new_client()
706 return new BrightnessUnit(this, plugin);
709 LoadPackage* BrightnessEngine::new_package()
711 return new BrightnessPackage;