igor b reset btns, reframert bug fix
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / sharpen / sharpen.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 "bccmodels.h"
24 #include "condition.h"
25 #include "filexml.h"
26 #include "language.h"
27 #include "sharpen.h"
28 #include "sharpenwindow.h"
29
30 #include <stdio.h>
31 #include <string.h>
32
33 REGISTER_PLUGIN(SharpenMain)
34
35
36
37
38
39
40
41 SharpenConfig::SharpenConfig()
42 {
43         reset();
44 }
45
46 void SharpenConfig::reset()
47 {
48         horizontal = 0;
49         interlace = 0;
50         sharpness = 50;
51         luminance = 0;
52 }
53
54 void SharpenConfig::copy_from(SharpenConfig &that)
55 {
56         horizontal = that.horizontal;
57         interlace = that.interlace;
58         sharpness = that.sharpness;
59         luminance = that.luminance;
60 }
61
62 int SharpenConfig::equivalent(SharpenConfig &that)
63 {
64         return horizontal == that.horizontal &&
65                 interlace == that.interlace &&
66                 EQUIV(sharpness, that.sharpness) &&
67                 luminance == that.luminance;
68 }
69
70 void SharpenConfig::interpolate(SharpenConfig &prev,
71         SharpenConfig &next,
72         long prev_frame,
73         long next_frame,
74         long current_frame)
75 {
76         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
77         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
78         this->sharpness = prev.sharpness * prev_scale + next.sharpness * next_scale;
79         this->interlace = prev.interlace;
80         this->horizontal = prev.horizontal;
81         this->luminance = prev.luminance;
82 }
83
84
85
86
87
88
89
90
91
92
93
94 SharpenMain::SharpenMain(PluginServer *server)
95  : PluginVClient(server)
96 {
97
98         engine = 0;
99 }
100
101 SharpenMain::~SharpenMain()
102 {
103
104
105         if(engine)
106         {
107                 for(int i = 0; i < total_engines; i++)
108                 {
109                         delete engine[i];
110                 }
111                 delete engine;
112         }
113 }
114
115 NEW_WINDOW_MACRO(SharpenMain, SharpenWindow)
116
117
118 LOAD_CONFIGURATION_MACRO(SharpenMain, SharpenConfig)
119
120 const char* SharpenMain::plugin_title() { return N_("Sharpen"); }
121 int SharpenMain::is_realtime() { return 1; }
122
123
124
125 int SharpenMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
126 {
127         int j, k;
128         output = output_ptr;
129         input = input_ptr;
130
131         load_configuration();
132         if(!engine)
133         {
134
135                 total_engines = PluginClient::smp > 1 ? 2 : 1;
136                 engine = new SharpenEngine*[total_engines];
137                 for(int i = 0; i < total_engines; i++)
138                 {
139                         engine[i] = new SharpenEngine(this);
140                         engine[i]->start();
141                 }
142         }
143
144         get_luts(pos_lut, neg_lut, input_ptr->get_color_model());
145
146         if(config.sharpness != 0)
147         {
148 // Arm first row
149                 row_step = (config.interlace /* || config.horizontal */) ? 2 : 1;
150
151                 for(j = 0; j < row_step; j += total_engines)
152                 {
153                         for(k = 0; k < total_engines && k + j < row_step; k++)
154                         {
155                                 engine[k]->start_process_frame(input_ptr, input_ptr, k + j);
156                         }
157                         for(k = 0; k < total_engines && k + j < row_step; k++)
158                         {
159                                 engine[k]->wait_process_frame();
160                         }
161                 }
162         }
163         else
164         if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
165         {
166                 output_ptr->copy_from(input_ptr);
167         }
168         return 0;
169 }
170
171 void SharpenMain::update_gui()
172 {
173         if(thread)
174         {
175                 load_configuration();
176                 thread->window->lock_window("SharpenMain::update_gui");
177                 ((SharpenWindow*)thread->window)->sharpen_slider->update((int)config.sharpness);
178                 ((SharpenWindow*)thread->window)->sharpen_interlace->update(config.interlace);
179                 ((SharpenWindow*)thread->window)->sharpen_horizontal->update(config.horizontal);
180                 ((SharpenWindow*)thread->window)->sharpen_luminance->update(config.luminance);
181                 thread->window->unlock_window();
182         }
183 }
184
185
186
187
188 int SharpenMain::get_luts(int *pos_lut, int *neg_lut, int color_model)
189 {
190         int i, inv_sharpness, vmax;
191
192         vmax = BC_CModels::calculate_max(color_model);
193
194         inv_sharpness = (int)(100 - config.sharpness);
195         if(config.horizontal) inv_sharpness /= 2;
196         if(inv_sharpness < 1) inv_sharpness = 1;
197
198         for(i = 0; i < vmax + 1; i++)
199         {
200                 pos_lut[i] = 800 * i / inv_sharpness;
201                 neg_lut[i] = (4 + pos_lut[i] - (i << 3)) >> 3;
202         }
203
204         return 0;
205 }
206
207 void SharpenMain::save_data(KeyFrame *keyframe)
208 {
209         FileXML output;
210
211 // cause data to be stored directly in text
212         output.set_shared_output(keyframe->xbuf);
213         output.tag.set_title("SHARPNESS");
214         output.tag.set_property("VALUE", config.sharpness);
215         output.tag.set_property("INTERLACE", config.interlace);
216         output.tag.set_property("HORIZONTAL", config.horizontal);
217         output.tag.set_property("LUMINANCE", config.luminance);
218         output.append_tag();
219         output.tag.set_title("/SHARPNESS");
220         output.append_tag();
221         output.append_newline();
222         output.terminate_string();
223 }
224
225 void SharpenMain::read_data(KeyFrame *keyframe)
226 {
227         FileXML input;
228
229         input.set_shared_input(keyframe->xbuf);
230
231         int result = 0;
232
233         while(!result)
234         {
235                 result = input.read_tag();
236
237                 if(!result)
238                 {
239                         if(input.tag.title_is("SHARPNESS"))
240                         {
241                                 config.sharpness = input.tag.get_property("VALUE", config.sharpness);
242                                 config.interlace = input.tag.get_property("INTERLACE", config.interlace);
243                                 config.horizontal = input.tag.get_property("HORIZONTAL", config.horizontal);
244                                 config.luminance = input.tag.get_property("LUMINANCE", config.luminance);
245 //printf("SharpenMain::read_data %f\n", sharpness);
246                         }
247                 }
248         }
249
250         if(config.sharpness > MAXSHARPNESS)
251                 config.sharpness = MAXSHARPNESS;
252         else
253                 if(config.sharpness < 0) config.sharpness = 0;
254 }
255
256
257
258
259 SharpenEngine::SharpenEngine(SharpenMain *plugin)
260  : Thread(1, 0, 0)
261 {
262         this->plugin = plugin;
263         input_lock = new Condition(0,"SharpenEngine::input_lock");
264         output_lock = new Condition(0, "SharpenEngine::output_lock");
265         last_frame = 0;
266         for(int i = 0; i < 4; i++)
267         {
268                 neg_rows[i] = new unsigned char[plugin->input->get_w() *
269                         4 *
270                         MAX(sizeof(float), sizeof(int))];
271         }
272 }
273
274 SharpenEngine::~SharpenEngine()
275 {
276         last_frame = 1;
277         input_lock->unlock();
278         Thread::join();
279
280         for(int i = 0; i < 4; i++)
281         {
282                 delete [] neg_rows[i];
283         }
284         delete input_lock;
285         delete output_lock;
286 }
287
288 int SharpenEngine::start_process_frame(VFrame *output, VFrame *input, int field)
289 {
290         this->output = output;
291         this->input = input;
292         this->field = field;
293
294 // Get coefficient for floating point
295         sharpness_coef = 100 - plugin->config.sharpness;
296         if(plugin->config.horizontal) sharpness_coef /= 2;
297         if(sharpness_coef < 1) sharpness_coef = 1;
298         sharpness_coef = 800.0 / sharpness_coef;
299
300         input_lock->unlock();
301         return 0;
302 }
303
304 int SharpenEngine::wait_process_frame()
305 {
306         output_lock->lock("SharpenEngine::wait_process_frame");
307         return 0;
308 }
309
310 float SharpenEngine::calculate_pos(float value)
311 {
312         return sharpness_coef * value;
313 }
314
315 float SharpenEngine::calculate_neg(float value)
316 {
317         return (calculate_pos(value) - (value * 8)) / 8;
318 }
319
320 #define FILTER(components, vmax) \
321 { \
322         int *pos_lut = plugin->pos_lut; \
323         const int wordsize = sizeof(*src); \
324  \
325 /* Skip first pixel in row */ \
326         memcpy(dst, src, components * wordsize); \
327         dst += components; \
328         src += components; \
329  \
330         w -= 2; \
331  \
332         while(w > 0) \
333         { \
334                 long pixel; \
335                 pixel = (long)pos_lut[src[0]] -  \
336                         (long)neg0[-components] -  \
337                         (long)neg0[0] -  \
338                         (long)neg0[components] -  \
339                         (long)neg1[-components] -  \
340                         (long)neg1[components] -  \
341                         (long)neg2[-components] -  \
342                         (long)neg2[0] -  \
343                         (long)neg2[components]; \
344                 pixel = (pixel + 4) >> 3; \
345                 if(pixel < 0) dst[0] = 0; \
346                 else \
347                 if(pixel > vmax) dst[0] = vmax; \
348                 else \
349                 dst[0] = pixel; \
350  \
351                 pixel = (long)pos_lut[src[1]] -  \
352                         (long)neg0[-components + 1] -  \
353                         (long)neg0[1] -  \
354                         (long)neg0[components + 1] -  \
355                         (long)neg1[-components + 1] -  \
356                         (long)neg1[components + 1] -  \
357                         (long)neg2[-components + 1] -  \
358                         (long)neg2[1] -  \
359                         (long)neg2[components + 1]; \
360                 pixel = (pixel + 4) >> 3; \
361                 if(pixel < 0) dst[1] = 0; \
362                 else \
363                 if(pixel > vmax) dst[1] = vmax; \
364                 else \
365                 dst[1] = pixel; \
366  \
367                 pixel = (long)pos_lut[src[2]] -  \
368                         (long)neg0[-components + 2] -  \
369                         (long)neg0[2] -  \
370                         (long)neg0[components + 2] -  \
371                         (long)neg1[-components + 2] -  \
372                         (long)neg1[components + 2] -  \
373                         (long)neg2[-components + 2] -  \
374                         (long)neg2[2] -  \
375                         (long)neg2[components + 2]; \
376                 pixel = (pixel + 4) >> 3; \
377                 if(pixel < 0) dst[2] = 0; \
378                 else \
379                 if(pixel > vmax) dst[2] = vmax; \
380                 else \
381                 dst[2] = pixel; \
382  \
383                 src += components; \
384                 dst += components; \
385  \
386                 neg0 += components; \
387                 neg1 += components; \
388                 neg2 += components; \
389                 w--; \
390         } \
391  \
392 /* Skip last pixel in row */ \
393         memcpy(dst, src, components * wordsize); \
394 }
395
396 void SharpenEngine::filter(int components,
397         int vmax,
398         int w,
399         u_int16_t *src,
400         u_int16_t *dst,
401         int *neg0,
402         int *neg1,
403         int *neg2)
404 {
405         FILTER(components, vmax);
406 }
407
408 void SharpenEngine::filter(int components,
409         int vmax,
410         int w,
411         unsigned char *src,
412         unsigned char *dst,
413         int *neg0,
414         int *neg1,
415         int *neg2)
416 {
417         FILTER(components, vmax);
418 }
419
420 void SharpenEngine::filter(int components,
421         int vmax,
422         int w,
423         float *src,
424         float *dst,
425         float *neg0,
426         float *neg1,
427         float *neg2)
428 {
429         const int wordsize = sizeof(float);
430 // First pixel in row
431         memcpy(dst, src, components * wordsize);
432         dst += components;
433         src += components;
434
435         w -= 2;
436         while(w > 0)
437         {
438                 float pixel;
439                 pixel = calculate_pos(src[0]) -
440                         neg0[-components] -
441                         neg0[0] -
442                         neg0[components] -
443                         neg1[-components] -
444                         neg1[components] -
445                         neg2[-components] -
446                         neg2[0] -
447                         neg2[components];
448                 pixel /= 8;
449                 dst[0] = pixel;
450
451                 pixel = calculate_pos(src[1]) -
452                         neg0[-components + 1] -
453                         neg0[1] -
454                         neg0[components + 1] -
455                         neg1[-components + 1] -
456                         neg1[components + 1] -
457                         neg2[-components + 1] -
458                         neg2[1] -
459                         neg2[components + 1];
460                 pixel /= 8;
461                 dst[1] = pixel;
462
463                 pixel = calculate_pos(src[2]) -
464                         neg0[-components + 2] -
465                         neg0[2] -
466                         neg0[components + 2] -
467                         neg1[-components + 2] -
468                         neg1[components + 2] -
469                         neg2[-components + 2] -
470                         neg2[2] -
471                         neg2[components + 2];
472                 pixel /= 8;
473                 dst[2] = pixel;
474
475                 src += components;
476                 dst += components;
477                 neg0 += components;
478                 neg1 += components;
479                 neg2 += components;
480                 w--;
481         }
482
483 /* Last pixel */
484         memcpy(dst, src, components * wordsize);
485 }
486
487
488
489
490
491
492
493 #define SHARPEN(components, type, temp_type, vmax) \
494 { \
495         int count, row; \
496         int wordsize = sizeof(type); \
497         unsigned char **input_rows, **output_rows; \
498         int w = plugin->input->get_w(); \
499         int h = plugin->input->get_h(); \
500  \
501         input_rows = input->get_rows(); \
502         output_rows = output->get_rows(); \
503         src_rows[0] = input_rows[field]; \
504         src_rows[1] = input_rows[field]; \
505         src_rows[2] = input_rows[field]; \
506         src_rows[3] = input_rows[field]; \
507  \
508         for(int j = 0; j < w; j++) \
509         { \
510                 temp_type *neg = (temp_type*)neg_rows[0]; \
511                 type *src = (type*)src_rows[0]; \
512                 for(int k = 0; k < components; k++) \
513                 { \
514                         if(wordsize == 4) \
515                         { \
516                                 neg[j * components + k] = \
517                                         (temp_type)calculate_neg(src[j * components + k]); \
518                         } \
519                         else \
520                         { \
521                                 neg[j * components + k] = \
522                                         (temp_type)plugin->neg_lut[(int)src[j * components + k]]; \
523                         } \
524                 } \
525         } \
526  \
527         row = 1; \
528         count = 1; \
529  \
530         for(int i = field; i < h; i += plugin->row_step) \
531         { \
532                 if((i + plugin->row_step) < h) \
533                 { \
534                         if(count >= 3) count--; \
535 /* Arm next row */ \
536                         src_rows[row] = input_rows[i + plugin->row_step]; \
537 /* Calculate neg rows */ \
538                         type *src = (type*)src_rows[row]; \
539                         temp_type *neg = (temp_type*)neg_rows[row]; \
540                         for(int k = 0; k < w; k++) \
541                         { \
542                                 for(int j = 0; j < components; j++) \
543                                 { \
544                                         if(wordsize == 4) \
545                                         { \
546                                                 neg[k * components + j] = \
547                                                         (temp_type)calculate_neg(src[k * components + j]); \
548                                         } \
549                                         else \
550                                         { \
551                                                 neg[k * components + j] = \
552                                                         plugin->neg_lut[(int)src[k * components + j]]; \
553                                         } \
554                                 } \
555                         } \
556  \
557                         count++; \
558                         row = (row + 1) & 3; \
559                 } \
560                 else \
561                 { \
562                         count--; \
563                 } \
564  \
565                 dst_row = output_rows[i]; \
566                 if(count == 3) \
567                 { \
568 /* Do the filter */ \
569                         if(plugin->config.horizontal) \
570                                 filter(components, \
571                                         vmax, \
572                                         w,  \
573                                         (type*)src_rows[(row + 2) & 3],  \
574                                         (type*)dst_row, \
575                                         (temp_type*)neg_rows[(row + 2) & 3] + components, \
576                                         (temp_type*)neg_rows[(row + 2) & 3] + components, \
577                                         (temp_type*)neg_rows[(row + 2) & 3] + components); \
578                         else \
579                                 filter(components, \
580                                         vmax, \
581                                         w,  \
582                                         (type*)src_rows[(row + 2) & 3],  \
583                                         (type*)dst_row, \
584                                         (temp_type*)neg_rows[(row + 1) & 3] + components, \
585                                         (temp_type*)neg_rows[(row + 2) & 3] + components, \
586                                         (temp_type*)neg_rows[(row + 3) & 3] + components); \
587                 } \
588                 else  \
589                 if(count == 2) \
590                 { \
591                         if(i == 0) \
592                                 memcpy(dst_row, src_rows[0], w * components * wordsize); \
593                         else \
594                                 memcpy(dst_row, src_rows[2], w * components * wordsize); \
595                 } \
596         } \
597 }
598
599
600
601 void SharpenEngine::run()
602 {
603         while(1)
604         {
605                 input_lock->lock("SharpenEngine::run");
606                 if(last_frame)
607                 {
608                         output_lock->unlock();
609                         return;
610                 }
611
612
613                 switch(input->get_color_model())
614                 {
615                         case BC_RGB_FLOAT:
616                                 SHARPEN(3, float, float, 1);
617                                 break;
618
619                         case BC_RGB888:
620                         case BC_YUV888:
621                                 SHARPEN(3, unsigned char, int, 0xff);
622                                 break;
623
624                         case BC_RGBA_FLOAT:
625                                 SHARPEN(4, float, float, 1);
626                                 break;
627
628                         case BC_RGBA8888:
629                         case BC_YUVA8888:
630                                 SHARPEN(4, unsigned char, int, 0xff);
631                                 break;
632
633                         case BC_RGB161616:
634                         case BC_YUV161616:
635                                 SHARPEN(3, u_int16_t, int, 0xffff);
636                                 break;
637
638                         case BC_RGBA16161616:
639                         case BC_YUVA16161616:
640                                 SHARPEN(4, u_int16_t, int, 0xffff);
641                                 break;
642                 }
643
644                 output_lock->unlock();
645         }
646 }
647