no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / colorbalance / colorbalance.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 "bccolors.h"
23 #include "filexml.h"
24 #include "clip.h"
25 #include "colorbalance.h"
26 #include "bchash.h"
27 #include "language.h"
28 #include "playback3d.h"
29
30 #include "aggregated.h"
31 #include "../interpolate/aggregated.h"
32 #include "../gamma/aggregated.h"
33
34 #include <stdio.h>
35 #include <string.h>
36
37 // 1000 corresponds to (1.0 + MAX_COLOR) * input
38 #define MAX_COLOR 1.0
39
40 REGISTER_PLUGIN(ColorBalanceMain)
41
42
43
44 ColorBalanceConfig::ColorBalanceConfig()
45 {
46         reset(RESET_ALL);
47 }
48
49 void ColorBalanceConfig::reset(int clear)
50 {
51         switch(clear) {
52                 case RESET_CYAN : cyan = 0;
53                         break;
54                 case RESET_MAGENTA : magenta = 0;
55                         break;
56                 case RESET_YELLOW : yellow = 0;
57                         break;
58                 case RESET_ALL :
59                 case RESET_DEFAULT_SETTINGS :
60                 default:
61                         cyan = 0;
62                         magenta = 0;
63                         yellow = 0;
64                         lock_params = 0;
65                         preserve = 0;
66                         break;
67         }
68 }
69
70 int ColorBalanceConfig::equivalent(ColorBalanceConfig &that)
71 {
72         return (cyan == that.cyan &&
73                 magenta == that.magenta &&
74                 yellow == that.yellow &&
75                 lock_params == that.lock_params &&
76         preserve == that.preserve);
77 }
78
79 void ColorBalanceConfig::copy_from(ColorBalanceConfig &that)
80 {
81         cyan = that.cyan;
82         magenta = that.magenta;
83         yellow = that.yellow;
84         lock_params = that.lock_params;
85     preserve = that.preserve;
86 }
87
88 void ColorBalanceConfig::interpolate(ColorBalanceConfig &prev,
89         ColorBalanceConfig &next,
90         int64_t prev_frame,
91         int64_t next_frame,
92         int64_t current_frame)
93 {
94         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
95         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
96
97         this->cyan = prev.cyan * prev_scale + next.cyan * next_scale;
98         this->magenta = prev.magenta * prev_scale + next.magenta * next_scale;
99         this->yellow = prev.yellow * prev_scale + next.yellow * next_scale;
100         this->preserve = prev.preserve;
101         this->lock_params = prev.lock_params;
102 }
103
104
105
106
107
108
109
110
111
112
113 ColorBalanceEngine::ColorBalanceEngine(ColorBalanceMain *plugin)
114  : Thread()
115 {
116         this->plugin = plugin;
117         last_frame = 0;
118         set_synchronous(1);
119 }
120
121 ColorBalanceEngine::~ColorBalanceEngine()
122 {
123         last_frame = 1;
124         input_lock.unlock();
125         Thread::join();
126 }
127
128
129 int ColorBalanceEngine::start_process_frame(VFrame *output, VFrame *input, int row_start, int row_end)
130 {
131         this->output = output;
132         this->input = input;
133         this->row_start = row_start;
134         this->row_end = row_end;
135         input_lock.unlock();
136         return 0;
137 }
138
139
140 int ColorBalanceEngine::wait_process_frame()
141 {
142         output_lock.lock("ColorBalanceEngine::wait_process_frame");
143         return 0;
144 }
145
146 void ColorBalanceEngine::run()
147 {
148         while(1)
149         {
150                 input_lock.lock("ColorBalanceEngine::run");
151                 if(last_frame)
152                 {
153                         output_lock.unlock();
154                         return;
155                 }
156
157 #define PROCESS(yuvtorgb,  \
158         rgbtoyuv,  \
159         r_lookup,  \
160         g_lookup,  \
161         b_lookup,  \
162         type,  \
163         max,  \
164         components,  \
165         do_yuv) \
166 { \
167         int j, k; \
168         int y, cr, cb, r, g, b, r_n, g_n, b_n; \
169         float h, s, v, h_old, s_old, r_f, g_f, b_f; \
170         type **input_rows, **output_rows; \
171         input_rows = (type**)input->get_rows(); \
172         output_rows = (type**)output->get_rows(); \
173  \
174         for(j = row_start; j < row_end; j++) \
175         { \
176                 for(k = 0; k < input->get_w() * components; k += components) \
177                 { \
178                         if(do_yuv) \
179                         { \
180                                 y = input_rows[j][k]; \
181                                 cb = input_rows[j][k + 1]; \
182                                 cr = input_rows[j][k + 2]; \
183                                 yuvtorgb(r, g, b, y, cb, cr); \
184                         } \
185                         else \
186                         { \
187                                 r = input_rows[j][k]; \
188                                 g = input_rows[j][k + 1]; \
189                                 b = input_rows[j][k + 2]; \
190                         } \
191  \
192                         r_n = plugin->r_lookup[r]; \
193                         g_n = plugin->g_lookup[g]; \
194                         b_n = plugin->b_lookup[b]; \
195  \
196                         if(plugin->config.preserve) \
197                         { \
198                                 HSV::rgb_to_hsv((float)r_n, (float)g_n, (float)b_n, h, s, v); \
199                                 HSV::rgb_to_hsv((float)r, (float)g, (float)b, h_old, s_old, v); \
200                                 HSV::hsv_to_rgb(r_f, g_f, b_f, h, s, v); \
201                                 r = (type)r_f; \
202                                 g = (type)g_f; \
203                                 b = (type)b_f; \
204                         } \
205                         else \
206                         { \
207                                 r = r_n; \
208                                 g = g_n; \
209                                 b = b_n; \
210                         } \
211  \
212                         if(do_yuv) \
213                         { \
214                                 rgbtoyuv(CLAMP(r, 0, max), CLAMP(g, 0, max), CLAMP(b, 0, max), y, cb, cr); \
215                                 output_rows[j][k] = y; \
216                                 output_rows[j][k + 1] = cb; \
217                                 output_rows[j][k + 2] = cr; \
218                         } \
219                         else \
220                         { \
221                                 output_rows[j][k] = CLAMP(r, 0, max); \
222                                 output_rows[j][k + 1] = CLAMP(g, 0, max); \
223                                 output_rows[j][k + 2] = CLAMP(b, 0, max); \
224                         } \
225                 } \
226         } \
227 }
228
229 #define PROCESS_F(components) \
230 { \
231         int j, k; \
232         float r, g, b, r_n, g_n, b_n; \
233         float h, s, v, h_old, s_old, r_f, g_f, b_f; \
234         float **input_rows, **output_rows; \
235         input_rows = (float**)input->get_rows(); \
236         output_rows = (float**)output->get_rows(); \
237         cyan_f = plugin->calculate_transfer(plugin->config.cyan); \
238         magenta_f = plugin->calculate_transfer(plugin->config.magenta); \
239         yellow_f = plugin->calculate_transfer(plugin->config.yellow); \
240  \
241         for(j = row_start; j < row_end; j++) \
242         { \
243                 for(k = 0; k < input->get_w() * components; k += components) \
244                 { \
245                         r = input_rows[j][k]; \
246                         g = input_rows[j][k + 1]; \
247                         b = input_rows[j][k + 2]; \
248  \
249                         r_n = r * cyan_f; \
250                         g_n = g * magenta_f; \
251                         b_n = b * yellow_f; \
252  \
253                         if(plugin->config.preserve) \
254                         { \
255                                 HSV::rgb_to_hsv(r_n, g_n, b_n, h, s, v); \
256                                 HSV::rgb_to_hsv(r, g, b, h_old, s_old, v); \
257                                 HSV::hsv_to_rgb(r_f, g_f, b_f, h, s, v); \
258                                 r = (float)r_f; \
259                                 g = (float)g_f; \
260                                 b = (float)b_f; \
261                         } \
262                         else \
263                         { \
264                                 r = r_n; \
265                                 g = g_n; \
266                                 b = b_n; \
267                         } \
268  \
269                         output_rows[j][k] = r; \
270                         output_rows[j][k + 1] = g; \
271                         output_rows[j][k + 2] = b; \
272                 } \
273         } \
274 }
275
276                 switch(input->get_color_model())
277                 {
278                         case BC_RGB888:
279                                 PROCESS(YUV::yuv.yuv_to_rgb_8, YUV::yuv.rgb_to_yuv_8,
280                                         r_lookup_8, g_lookup_8, b_lookup_8,
281                                         unsigned char, 0xff, 3, 0);
282                                 break;
283
284                         case BC_RGB_FLOAT:
285                                 PROCESS_F(3);
286                                 break;
287
288                         case BC_YUV888:
289                                 PROCESS(YUV::yuv.yuv_to_rgb_8, YUV::yuv.rgb_to_yuv_8,
290                                         r_lookup_8, g_lookup_8, b_lookup_8,
291                                         unsigned char, 0xff, 3, 1);
292                                 break;
293
294                         case BC_RGBA_FLOAT:
295                                 PROCESS_F(4);
296                                 break;
297
298                         case BC_RGBA8888:
299                                 PROCESS(YUV::yuv.yuv_to_rgb_8, YUV::yuv.rgb_to_yuv_8,
300                                         r_lookup_8, g_lookup_8, b_lookup_8,
301                                         unsigned char, 0xff, 4, 0);
302                                 break;
303
304                         case BC_YUVA8888:
305                                 PROCESS(YUV::yuv.yuv_to_rgb_8, YUV::yuv.rgb_to_yuv_8,
306                                         r_lookup_8, g_lookup_8, b_lookup_8,
307                                         unsigned char, 0xff, 4, 1);
308                                 break;
309
310                         case BC_YUV161616:
311                                 PROCESS(YUV::yuv.yuv_to_rgb_16, YUV::yuv.rgb_to_yuv_16,
312                                         r_lookup_16, g_lookup_16, b_lookup_16,
313                                         u_int16_t, 0xffff, 3, 1);
314                                 break;
315
316                         case BC_YUVA16161616:
317                                 PROCESS(YUV::yuv.yuv_to_rgb_16, YUV::yuv.rgb_to_yuv_16,
318                                         r_lookup_16, g_lookup_16, b_lookup_16,
319                                         u_int16_t, 0xffff, 4, 1);
320                                 break;
321                 }
322
323
324
325                 output_lock.unlock();
326         }
327 }
328
329
330
331
332 ColorBalanceMain::ColorBalanceMain(PluginServer *server)
333  : PluginVClient(server)
334 {
335         need_reconfigure = 1;
336         engine = 0;
337
338 }
339
340 ColorBalanceMain::~ColorBalanceMain()
341 {
342
343
344
345         if(engine)
346         {
347                 for(int i = 0; i < total_engines; i++)
348                 {
349                         delete engine[i];
350                 }
351                 delete [] engine;
352         }
353 }
354
355 const char* ColorBalanceMain::plugin_title() { return N_("Color Balance"); }
356 int ColorBalanceMain::is_realtime() { return 1; }
357
358
359 int ColorBalanceMain::reconfigure()
360 {
361         float r_scale = calculate_transfer(config.cyan);
362         float g_scale = calculate_transfer(config.magenta);
363         float b_scale = calculate_transfer(config.yellow);
364
365 #define RECONFIGURE(r_lookup, g_lookup, b_lookup, max) \
366         for(int i = 0; i <= max; i++) \
367         { \
368                 r_lookup[i] = CLIP((int)(r_scale * i), 0, max); \
369                 g_lookup[i] = CLIP((int)(g_scale * i), 0, max); \
370                 b_lookup[i] = CLIP((int)(b_scale * i), 0, max); \
371         }
372
373         RECONFIGURE(r_lookup_8, g_lookup_8, b_lookup_8, 0xff);
374         RECONFIGURE(r_lookup_16, g_lookup_16, b_lookup_16, 0xffff);
375
376         return 0;
377 }
378
379 int64_t ColorBalanceMain::calculate_slider(float in)
380 {
381         if(in < 1.0)
382         {
383                 return (int64_t)(in * 1000 - 1000.0);
384         }
385         else
386         if(in > 1.0)
387         {
388                 return (int64_t)(1000 * (in - 1.0) / MAX_COLOR);
389         }
390         else
391                 return 0;
392 }
393
394 float ColorBalanceMain::calculate_transfer(float in)
395 {
396         if(in < 0)
397         {
398                 return (1000.0 + in) / 1000.0;
399         }
400         else
401         if(in > 0)
402         {
403                 return 1.0 + in / 1000.0 * MAX_COLOR;
404         }
405         else
406                 return 1.0;
407 }
408
409
410
411
412 int ColorBalanceMain::test_boundary(float &value)
413 {
414
415         if(value < -1000) value = -1000;
416     if(value > 1000) value = 1000;
417         return 0;
418 }
419
420 int ColorBalanceMain::synchronize_params(ColorBalanceSlider *slider, float difference)
421 {
422         if(thread && config.lock_params) {
423                 if(slider != ((ColorBalanceWindow*)thread->window)->cyan) {
424                         config.cyan += difference;
425                         test_boundary(config.cyan);
426                         ((ColorBalanceWindow*)thread->window)->cyan->update((int64_t)config.cyan);
427                 }
428                 if(slider != ((ColorBalanceWindow*)thread->window)->magenta) {
429                         config.magenta += difference;
430                         test_boundary(config.magenta);
431                         ((ColorBalanceWindow*)thread->window)->magenta->update((int64_t)config.magenta);
432                 }
433                 if(slider != ((ColorBalanceWindow*)thread->window)->yellow) {
434                         config.yellow += difference;
435                         test_boundary(config.yellow);
436                         ((ColorBalanceWindow*)thread->window)->yellow->update((int64_t)config.yellow);
437                 }
438         }
439         return 0;
440 }
441
442
443
444
445
446
447 LOAD_CONFIGURATION_MACRO(ColorBalanceMain, ColorBalanceConfig)
448 NEW_WINDOW_MACRO(ColorBalanceMain, ColorBalanceWindow)
449
450
451
452
453
454 int ColorBalanceMain::process_buffer(VFrame *frame,
455         int64_t start_position,
456         double frame_rate)
457 {
458         need_reconfigure |= load_configuration();
459
460 //printf("ColorBalanceMain::process_realtime 1 %d\n", need_reconfigure);
461         if(need_reconfigure)
462         {
463                 if(!engine)
464                 {
465                         total_engines = PluginClient::smp + 1;
466                         engine = new ColorBalanceEngine*[total_engines];
467                         for(int i = 0; i < total_engines; i++)
468                         {
469                                 engine[i] = new ColorBalanceEngine(this);
470                                 engine[i]->start();
471                         }
472                 }
473
474                 reconfigure();
475                 need_reconfigure = 0;
476         }
477
478         frame->get_params()->update("COLORBALANCE_PRESERVE", config.preserve);
479         frame->get_params()->update("COLORBALANCE_CYAN", calculate_transfer(config.cyan));
480         frame->get_params()->update("COLORBALANCE_MAGENTA", calculate_transfer(config.magenta));
481         frame->get_params()->update("COLORBALANCE_YELLOW", calculate_transfer(config.yellow));
482
483
484         read_frame(frame,
485                 0,
486                 get_source_position(),
487                 get_framerate(),
488                 get_use_opengl());
489
490         int aggregate_interpolate = 0;
491         int aggregate_gamma = 0;
492         get_aggregation(&aggregate_interpolate,
493                 &aggregate_gamma);
494
495         if(!EQUIV(config.cyan, 0) ||
496                 !EQUIV(config.magenta, 0) ||
497                 !EQUIV(config.yellow, 0) ||
498                 (get_use_opengl() &&
499                         (aggregate_interpolate ||
500                         aggregate_gamma)))
501         {
502                 if(get_use_opengl())
503                 {
504 //get_output()->dump_stacks();
505 // Aggregate
506                         if(next_effect_is(_("Histogram"))) return 0;
507                         return run_opengl();
508                 }
509
510                 for(int i = 0; i < total_engines; i++)
511                 {
512                         engine[i]->start_process_frame(frame,
513                                 frame,
514                                 frame->get_h() * i / total_engines,
515                                 frame->get_h() * (i + 1) / total_engines);
516                 }
517
518                 for(int i = 0; i < total_engines; i++)
519                 {
520                         engine[i]->wait_process_frame();
521                 }
522         }
523
524
525         return 0;
526 }
527
528
529 void ColorBalanceMain::update_gui()
530 {
531         if(thread)
532         {
533                 load_configuration();
534                 ((ColorBalanceWindow*)thread->window)->lock_window("ColorBalanceMain::update_gui");
535                 ((ColorBalanceWindow*)thread->window)->cyan->update((int64_t)config.cyan);
536                 ((ColorBalanceWindow*)thread->window)->magenta->update((int64_t)config.magenta);
537                 ((ColorBalanceWindow*)thread->window)->yellow->update((int64_t)config.yellow);
538                 ((ColorBalanceWindow*)thread->window)->preserve->update(config.preserve);
539                 ((ColorBalanceWindow*)thread->window)->lock_params->update(config.lock_params);
540                 ((ColorBalanceWindow*)thread->window)->unlock_window();
541         }
542 }
543
544
545
546
547 void ColorBalanceMain::save_data(KeyFrame *keyframe)
548 {
549         FileXML output;
550
551 // cause data to be stored directly in text
552         output.set_shared_output(keyframe->xbuf);
553         output.tag.set_title("COLORBALANCE");
554         output.tag.set_property("CYAN", config.cyan);
555         output.tag.set_property("MAGENTA",  config.magenta);
556         output.tag.set_property("YELLOW",  config.yellow);
557         output.tag.set_property("PRESERVELUMINOSITY",  config.preserve);
558         output.tag.set_property("LOCKPARAMS",  config.lock_params);
559         output.append_tag();
560         output.tag.set_title("/COLORBALANCE");
561         output.append_tag();
562         output.append_newline();
563         output.terminate_string();
564 }
565
566 void ColorBalanceMain::read_data(KeyFrame *keyframe)
567 {
568         FileXML input;
569
570         input.set_shared_input(keyframe->xbuf);
571
572         int result = 0;
573
574         while(!result)
575         {
576                 result = input.read_tag();
577
578                 if(!result)
579                 {
580                         if(input.tag.title_is("COLORBALANCE"))
581                         {
582                                 config.cyan = input.tag.get_property("CYAN", config.cyan);
583                                 config.magenta = input.tag.get_property("MAGENTA", config.magenta);
584                                 config.yellow = input.tag.get_property("YELLOW", config.yellow);
585                                 config.preserve = input.tag.get_property("PRESERVELUMINOSITY", config.preserve);
586                                 config.lock_params = input.tag.get_property("LOCKPARAMS", config.lock_params);
587                         }
588                 }
589         }
590 }
591
592 void ColorBalanceMain::get_aggregation(int *aggregate_interpolate,
593         int *aggregate_gamma)
594 {
595         if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
596                 !strcmp(get_output()->get_prev_effect(0), _("Gamma")))
597         {
598                 *aggregate_interpolate = 1;
599                 *aggregate_gamma = 1;
600         }
601         else
602         if(!strcmp(get_output()->get_prev_effect(0), _("Interpolate Pixels")))
603         {
604                 *aggregate_interpolate = 1;
605         }
606         else
607         if(!strcmp(get_output()->get_prev_effect(0), _("Gamma")))
608         {
609                 *aggregate_gamma = 1;
610         }
611 }
612
613 int ColorBalanceMain::handle_opengl()
614 {
615 #ifdef HAVE_GL
616
617         get_output()->to_texture();
618         get_output()->enable_opengl();
619
620         const char *shader_stack[16];
621         memset(shader_stack,0, sizeof(shader_stack));
622         int current_shader = 0;
623
624         int need_color_matrix = BC_CModels::is_yuv(get_output()->get_color_model()) ? 1 : 0;
625         if( need_color_matrix )
626                 shader_stack[current_shader++] = bc_gl_colors;
627
628         int aggregate_interpolate = 0;
629         int aggregate_gamma = 0;
630
631         get_aggregation(&aggregate_interpolate,
632                 &aggregate_gamma);
633
634 //printf("ColorBalanceMain::handle_opengl %d %d\n", aggregate_interpolate, aggregate_gamma);
635         if(aggregate_interpolate)
636                 INTERPOLATE_COMPILE(shader_stack, current_shader);
637
638         if(aggregate_gamma)
639                 GAMMA_COMPILE(shader_stack, current_shader,
640                         aggregate_interpolate);
641
642         COLORBALANCE_COMPILE(shader_stack, current_shader,
643                 aggregate_gamma || aggregate_interpolate);
644
645         shader_stack[current_shader] = 0;
646         unsigned int shader = VFrame::make_shader(shader_stack);
647         if( shader > 0 ) {
648                 glUseProgram(shader);
649                 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
650
651                 if(aggregate_interpolate) INTERPOLATE_UNIFORMS(shader);
652                 if(aggregate_gamma) GAMMA_UNIFORMS(shader);
653
654                 COLORBALANCE_UNIFORMS(shader);
655                 if( need_color_matrix ) BC_GL_COLORS(shader);
656         }
657
658         get_output()->init_screen();
659         get_output()->bind_texture(0);
660         get_output()->draw_texture();
661         glUseProgram(0);
662         get_output()->set_opengl_state(VFrame::SCREEN);
663 #endif
664         return 0;
665 }
666
667
668
669
670
671
672