initial commit
[goodguy/history.git] / cinelerra-5.0 / plugins / brightness / brightness.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
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.
10  * 
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.
15  * 
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
19  * 
20  */
21
22 #include "clip.h"
23 #include "filexml.h"
24 #include "brightness.h"
25 #include "bchash.h"
26 #include "language.h"
27
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <string.h>
31
32
33 REGISTER_PLUGIN(BrightnessMain)
34
35
36
37 BrightnessConfig::BrightnessConfig()
38 {
39         brightness = 0;
40         contrast = 0;
41         luma = 1;
42 }
43
44 int BrightnessConfig::equivalent(BrightnessConfig &that)
45 {
46         return (brightness == that.brightness && 
47                 contrast == that.contrast &&
48                 luma == that.luma);
49 }
50
51 void BrightnessConfig::copy_from(BrightnessConfig &that)
52 {
53         brightness = that.brightness;
54         contrast = that.contrast;
55         luma = that.luma;
56 }
57
58 void BrightnessConfig::interpolate(BrightnessConfig &prev, 
59         BrightnessConfig &next, 
60         int64_t prev_frame, 
61         int64_t next_frame, 
62         int64_t current_frame)
63 {
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);
66
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);
70 }
71
72
73
74
75
76
77
78
79
80 YUV BrightnessMain::yuv;
81
82 BrightnessMain::BrightnessMain(PluginServer *server)
83  : PluginVClient(server)
84 {
85     redo_buffers = 1;
86         engine = 0;
87         
88 }
89
90 BrightnessMain::~BrightnessMain()
91 {
92         
93         if(engine) delete engine;
94 }
95
96 const char* BrightnessMain::plugin_title() { return N_("Brightness/Contrast"); }
97 int BrightnessMain::is_realtime() { return 1; }
98
99 NEW_WINDOW_MACRO(BrightnessMain, BrightnessWindow)      
100 LOAD_CONFIGURATION_MACRO(BrightnessMain, BrightnessConfig)
101
102 int BrightnessMain::process_buffer(VFrame *frame,
103         int64_t start_position,
104         double frame_rate)
105 {
106         load_configuration();
107
108         read_frame(frame, 
109                 0, 
110                 start_position, 
111                 frame_rate,
112                 get_use_opengl());
113
114
115 // Use hardware
116         if(get_use_opengl())
117         {
118                 run_opengl();
119                 return 0;
120         }
121
122
123
124
125         if(!engine) engine = new BrightnessEngine(this, PluginClient::smp + 1);
126
127         this->input = frame;
128         this->output = frame;
129
130         if(!EQUIV(config.brightness, 0) || !EQUIV(config.contrast, 0))
131         {
132                 engine->process_packages();
133         }
134
135         return 0;
136 }
137
138 int BrightnessMain::handle_opengl()
139 {
140 #ifdef HAVE_GL
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"
146                 "void main()\n"
147                 "{\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"
152                 "}\n";
153
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"
159                 "void main()\n"
160                 "{\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"
166                 "}\n";
167
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"
173                 "void main()\n"
174                 "{\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"
180                 "}\n";
181
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                 "void main()\n"
188                 "{\n"
189                 "       const mat3 yuv_to_rgb_matrix = mat3(\n"
190                 "               1,       1,        1, \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"
203                 "}\n";
204
205         get_output()->to_texture();
206         get_output()->enable_opengl();
207
208         unsigned int shader_id = 0;
209         switch(get_output()->get_color_model())
210         {
211                 case BC_YUV888:
212                 case BC_YUVA8888:
213                         if(config.luma)
214                                 shader_id = VFrame::make_shader(0,
215                                         brightness_yuvluma_frag,
216                                         0);
217                         else
218                                 shader_id = VFrame::make_shader(0,
219                                         brightness_yuv_frag,
220                                         0);
221                         break;
222                 default:
223                         if(config.luma)
224                                 shader_id = VFrame::make_shader(0,
225                                         brightness_rgbluma_frag,
226                                         0);
227                         else
228                                 shader_id = VFrame::make_shader(0,
229                                         brightness_rgb_frag,
230                                         0);
231                         break;
232         }
233
234
235         if(shader_id > 0) 
236         {
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);
246         }
247
248         get_output()->init_screen();
249         get_output()->bind_texture(0);
250
251         
252
253         get_output()->draw_texture();
254         glUseProgram(0);
255         get_output()->set_opengl_state(VFrame::SCREEN);
256 //printf("BrightnessMain::handle_opengl 100 %x\n", glGetError());
257 #endif
258         return 0;
259 }
260
261
262 void BrightnessMain::update_gui()
263 {
264         if(thread)
265         {
266                 if(load_configuration())
267                 {
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();
273                 }
274         }
275 }
276
277  
278 void BrightnessMain::save_data(KeyFrame *keyframe)
279 {
280         FileXML output;
281
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);
288         output.append_tag();
289         output.terminate_string();
290 }
291
292 void BrightnessMain::read_data(KeyFrame *keyframe)
293 {
294         FileXML input;
295
296         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
297
298         int result = 0;
299
300         while(!result)
301         {
302                 result = input.read_tag();
303
304                 if(!result)
305                 {
306                         if(input.tag.title_is("BRIGHTNESS"))
307                         {
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);
311                         }
312                 }
313         }
314 }
315
316
317
318
319
320
321
322
323
324
325
326
327
328 BrightnessPackage::BrightnessPackage()
329  : LoadPackage()
330 {
331 }
332
333
334
335
336 BrightnessUnit::BrightnessUnit(BrightnessEngine *server, BrightnessMain *plugin)
337  : LoadClient(server)
338 {
339         this->plugin = plugin;
340 }
341
342 BrightnessUnit::~BrightnessUnit()
343 {
344 }
345         
346 void BrightnessUnit::process_package(LoadPackage *package)
347 {
348         BrightnessPackage *pkg = (BrightnessPackage*)package;
349
350
351         VFrame *output = plugin->output;
352         VFrame *input = plugin->input;
353         
354
355
356
357
358 #define DO_BRIGHTNESS(max, type, components, is_yuv) \
359 { \
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(); \
365         int r, g, b; \
366  \
367         if(!EQUIV(plugin->config.brightness, 0)) \
368         { \
369                 int offset = (int)(plugin->config.brightness / 100 * max); \
370 /*printf("DO_BRIGHTNESS offset=%d\n", offset);*/ \
371  \
372                 for(int i = row1; i < row2; i++) \
373                 { \
374                         type *input_row = input_rows[i]; \
375                         type *output_row = output_rows[i]; \
376  \
377                         for(int j = 0; j < width; j++) \
378                         { \
379                                 r = input_row[j * components] + offset; \
380  \
381                                 if(!is_yuv) \
382                                 { \
383                                         g = input_row[j * components + 1] + offset; \
384                                         b = input_row[j * components + 2] + offset; \
385                                 } \
386  \
387                                 CLAMP(r, 0, max); \
388                                 if(!is_yuv) \
389                                 { \
390                                         CLAMP(g, 0, max); \
391                                         CLAMP(b, 0, max); \
392                                 } \
393  \
394                                 output_row[j * components] = r; \
395  \
396                                 if(!is_yuv) \
397                                 { \
398                                         output_row[j * components + 1] = g; \
399                                         output_row[j * components + 2] = b; \
400                                 } \
401                                 else \
402                                 { \
403                                         output_row[j * components + 1] = input_row[j * components + 1]; \
404                                         output_row[j * components + 2] = input_row[j * components + 2]; \
405                                 } \
406  \
407                                 if(components == 4)  \
408                                         output_row[j * components + 3] = input_row[j * components + 3]; \
409                         } \
410                 } \
411  \
412 /* Data to be processed is now in the output buffer */ \
413                 input_rows = output_rows; \
414         } \
415  \
416         if(!EQUIV(plugin->config.contrast, 0)) \
417         { \
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);*/ \
422  \
423                 int scalar = (int)(contrast * 0x100); \
424                 int offset = (max << 8) / 2 - max * scalar / 2; \
425                 int y, u, v; \
426  \
427                 for(int i = row1; i < row2; i++) \
428                 { \
429                         type *input_row = input_rows[i]; \
430                         type *output_row = output_rows[i]; \
431  \
432                         if(plugin->config.luma) \
433                         { \
434                                 for(int j = 0; j < width; j++) \
435                                 { \
436                                         if(is_yuv) \
437                                         { \
438                                                 y = input_row[j * components]; \
439                                         } \
440                                         else \
441                                         { \
442                                                 r = input_row[j * components]; \
443                                                 g = input_row[j * components + 1]; \
444                                                 b = input_row[j * components + 2]; \
445                                                 if(max == 0xff) \
446                                                 { \
447                                                         BrightnessMain::yuv.rgb_to_yuv_8( \
448                                                                 r,  \
449                                                                 g,  \
450                                                                 b,  \
451                                                                 y,  \
452                                                                 u,  \
453                                                                 v); \
454                                                 } \
455                                                 else \
456                                                 { \
457                                                         BrightnessMain::yuv.rgb_to_yuv_16( \
458                                                                 r,  \
459                                                                 g,  \
460                                                                 b,  \
461                                                                 y,  \
462                                                                 u,  \
463                                                                 v); \
464                                                 } \
465          \
466                                         } \
467          \
468                                         y = (y * scalar + offset) >> 8; \
469                                         CLAMP(y, 0, max); \
470          \
471          \
472                                         if(is_yuv) \
473                                         { \
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]; \
477                                         } \
478                                         else \
479                                         { \
480                                                 if(max == 0xff) \
481                                                 { \
482                                                         BrightnessMain::yuv.yuv_to_rgb_8( \
483                                                                 r,  \
484                                                                 g,  \
485                                                                 b,  \
486                                                                 y,  \
487                                                                 u,  \
488                                                                 v); \
489                                                 } \
490                                                 else \
491                                                 { \
492                                                         BrightnessMain::yuv.yuv_to_rgb_16( \
493                                                                 r,  \
494                                                                 g,  \
495                                                                 b,  \
496                                                                 y,  \
497                                                                 u,  \
498                                                                 v); \
499                                                 } \
500                                                 input_row[j * components] = r; \
501                                                 input_row[j * components + 1] = g; \
502                                                 input_row[j * components + 2] = b; \
503                                         } \
504          \
505                                         if(components == 4)  \
506                                                 output_row[j * components + 3] = input_row[j * components + 3]; \
507                                 } \
508                         } \
509                         else \
510                         { \
511                                 for(int j = 0; j < width; j++) \
512                                 { \
513                                         r = input_row[j * components]; \
514                                         g = input_row[j * components + 1]; \
515                                         b = input_row[j * components + 2]; \
516  \
517                                         r = (r * scalar + offset) >> 8; \
518                                         g = (g * scalar + offset) >> 8; \
519                                         b = (b * scalar + offset) >> 8; \
520  \
521                                         CLAMP(r, 0, max); \
522                                         CLAMP(g, 0, max); \
523                                         CLAMP(b, 0, max); \
524  \
525                                         output_row[j * components] = r; \
526                                         output_row[j * components + 1] = g; \
527                                         output_row[j * components + 2] = b; \
528  \
529                                         if(components == 4)  \
530                                                 output_row[j * components + 3] = input_row[j * components + 3]; \
531                                 } \
532                         } \
533                 } \
534         } \
535 }
536
537
538
539 #define DO_BRIGHTNESS_F(components) \
540 { \
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(); \
546         float r, g, b; \
547  \
548         if(!EQUIV(plugin->config.brightness, 0)) \
549         { \
550                 float offset = plugin->config.brightness / 100; \
551  \
552                 for(int i = row1; i < row2; i++) \
553                 { \
554                         float *input_row = input_rows[i]; \
555                         float *output_row = output_rows[i]; \
556  \
557                         for(int j = 0; j < width; j++) \
558                         { \
559                                 r = input_row[j * components] + offset; \
560                                 g = input_row[j * components + 1] + offset; \
561                                 b = input_row[j * components + 2] + offset; \
562  \
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]; \
568                         } \
569                 } \
570  \
571 /* Data to be processed is now in the output buffer */ \
572                 input_rows = output_rows; \
573         } \
574  \
575         if(!EQUIV(plugin->config.contrast, 0)) \
576         { \
577                 float contrast = (plugin->config.contrast < 0) ?  \
578                         (plugin->config.contrast + 100) / 100 :  \
579                         (plugin->config.contrast + 25) / 25; \
580  \
581 /* Shift black level down so shadows get darker instead of lighter */ \
582                 float offset = 0.5 - contrast / 2; \
583                 float y, u, v; \
584  \
585                 for(int i = row1; i < row2; i++) \
586                 { \
587                         float *input_row = input_rows[i]; \
588                         float *output_row = output_rows[i]; \
589  \
590                         if(plugin->config.luma) \
591                         { \
592                                 for(int j = 0; j < width; j++) \
593                                 { \
594                                         r = input_row[j * components]; \
595                                         g = input_row[j * components + 1]; \
596                                         b = input_row[j * components + 2]; \
597                                         YUV::rgb_to_yuv_f( \
598                                                 r,  \
599                                                 g,  \
600                                                 b,  \
601                                                 y,  \
602                                                 u,  \
603                                                 v); \
604  \
605                                         y = y * contrast + offset; \
606  \
607  \
608                                         YUV::yuv_to_rgb_f( \
609                                                 r,  \
610                                                 g,  \
611                                                 b,  \
612                                                 y,  \
613                                                 u,  \
614                                                 v); \
615                                         input_row[j * components] = r; \
616                                         input_row[j * components + 1] = g; \
617                                         input_row[j * components + 2] = b; \
618  \
619                                         if(components == 4)  \
620                                                 output_row[j * components + 3] = input_row[j * components + 3]; \
621                                 } \
622                         } \
623                         else \
624                         { \
625                                 for(int j = 0; j < width; j++) \
626                                 { \
627                                         r = input_row[j * components]; \
628                                         g = input_row[j * components + 1]; \
629                                         b = input_row[j * components + 2]; \
630  \
631                                         r = r * contrast + offset; \
632                                         g = g * contrast + offset; \
633                                         b = b * contrast + offset; \
634  \
635                                         output_row[j * components] = r; \
636                                         output_row[j * components + 1] = g; \
637                                         output_row[j * components + 2] = b; \
638  \
639                                         if(components == 4)  \
640                                                 output_row[j * components + 3] = input_row[j * components + 3]; \
641                                 } \
642                         } \
643                 } \
644         } \
645 }
646
647
648         switch(input->get_color_model())
649         {
650                 case BC_RGB888:
651                         DO_BRIGHTNESS(0xff, unsigned char, 3, 0)
652                         break;
653
654                 case BC_RGB_FLOAT:
655                         DO_BRIGHTNESS_F(3)
656                         break;
657
658                 case BC_YUV888:
659                         DO_BRIGHTNESS(0xff, unsigned char, 3, 1)
660                         break;
661
662                 case BC_RGBA8888:
663                         DO_BRIGHTNESS(0xff, unsigned char, 4, 0)
664                         break;
665
666                 case BC_RGBA_FLOAT:
667                         DO_BRIGHTNESS_F(4)
668                         break;
669
670                 case BC_YUVA8888:
671                         DO_BRIGHTNESS(0xff, unsigned char, 4, 1)
672                         break;
673
674                 case BC_RGB161616:
675                         DO_BRIGHTNESS(0xffff, uint16_t, 3, 0)
676                         break;
677
678                 case BC_YUV161616:
679                         DO_BRIGHTNESS(0xffff, uint16_t, 3, 1)
680                         break;
681
682                 case BC_RGBA16161616:
683                         DO_BRIGHTNESS(0xffff, uint16_t, 4, 0)
684                         break;
685
686                 case BC_YUVA16161616:
687                         DO_BRIGHTNESS(0xffff, uint16_t, 4, 1)
688                         break;
689         }
690
691
692
693
694
695
696
697
698
699 }
700
701
702
703
704
705
706 BrightnessEngine::BrightnessEngine(BrightnessMain *plugin, int cpus)
707  : LoadServer(cpus, cpus)
708 {
709         this->plugin = plugin;
710 }
711
712 BrightnessEngine::~BrightnessEngine()
713 {
714 }
715
716
717 void BrightnessEngine::init_packages()
718 {
719         for(int i = 0; i < LoadServer::get_total_packages(); i++)
720         {
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();
724         }
725 }
726
727 LoadClient* BrightnessEngine::new_client()
728 {
729         return new BrightnessUnit(this, plugin);
730 }
731
732 LoadPackage* BrightnessEngine::new_package()
733 {
734         return new BrightnessPackage;
735 }
736
737
738
739
740