remove whitespace at eol
[goodguy/history.git] / cinelerra-5.1 / plugins / aging / aging.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 "filexml.h"
25 #include "aging.h"
26 #include "agingwindow.h"
27 #include "effecttv.h"
28 #include "language.h"
29
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34
35
36
37
38
39
40 REGISTER_PLUGIN(AgingMain)
41
42
43
44
45
46
47 int AgingConfig::dx[] = { 1, 1, 0, -1, -1, -1,  0, 1};
48 int AgingConfig::dy[] = { 0, -1, -1, -1, 0, 1, 1, 1};
49
50 AgingConfig::AgingConfig()
51 {
52         dust_interval = 0;
53         pits_interval = 0;
54         aging_mode = 0;
55         area_scale = 10;
56         scratch_lines = 7;
57         colorage = 1;
58         scratch = 1;
59         pits = 1;
60         dust = 1;
61 }
62
63 AgingMain::AgingMain(PluginServer *server)
64  : PluginVClient(server)
65 {
66
67         aging_server = 0;
68 }
69
70 AgingMain::~AgingMain()
71 {
72
73         if(aging_server) delete aging_server;
74 }
75
76 const char* AgingMain::plugin_title() { return _("AgingTV"); }
77 int AgingMain::is_realtime() { return 1; }
78
79 NEW_WINDOW_MACRO(AgingMain, AgingWindow)
80
81 int AgingMain::load_defaults()
82 {
83         return 0;
84 }
85
86 int AgingMain::save_defaults()
87 {
88         return 0;
89 }
90
91 int AgingMain::load_configuration()
92 {
93         return 0;
94 }
95
96
97 void AgingMain::save_data(KeyFrame *keyframe)
98 {
99 }
100
101 void AgingMain::read_data(KeyFrame *keyframe)
102 {
103 }
104
105
106 int AgingMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
107 {
108 //printf("AgingMain::process_realtime 1\n");
109         load_configuration();
110 //printf("AgingMain::process_realtime 1\n");
111         this->input_ptr = input_ptr;
112         this->output_ptr = output_ptr;
113
114         if(!aging_server) aging_server = new AgingServer(this,
115                 PluginClient::smp + 1,
116                 PluginClient::smp + 1);
117         aging_server->process_packages();
118 //printf("AgingMain::process_realtime 2\n");
119
120         return 0;
121 }
122
123
124
125 AgingServer::AgingServer(AgingMain *plugin, int total_clients, int total_packages)
126  : LoadServer(1, 1 /* total_clients, total_packages */)
127 {
128         this->plugin = plugin;
129 }
130
131
132 LoadClient* AgingServer::new_client()
133 {
134         return new AgingClient(this);
135 }
136
137
138
139
140 LoadPackage* AgingServer::new_package()
141 {
142         return new AgingPackage;
143 }
144
145
146
147 void AgingServer::init_packages()
148 {
149         for(int i = 0; i < get_total_packages(); i++)
150         {
151                 AgingPackage *package = (AgingPackage*)get_package(i);
152                 package->row1 = plugin->input_ptr->get_h() * i / get_total_packages();
153                 package->row2 = plugin->input_ptr->get_h() * (i + 1) / get_total_packages();
154         }
155 }
156
157
158
159
160
161
162
163
164 AgingClient::AgingClient(AgingServer *server)
165  : LoadClient(server)
166 {
167         this->plugin = server->plugin;
168 }
169
170
171
172
173
174
175
176
177
178 #define COLORAGE(type, components) \
179 { \
180         int a, b; \
181         int i, j, k; \
182  \
183         for(i = 0; i < h; i++) \
184         { \
185                 for(j = 0; j < w; j++) \
186                 { \
187                         for(k = 0; k < 3; k++) \
188                         { \
189                                 if(sizeof(type) == 4) \
190                                 { \
191                                         a = (int)(((type**)input_rows)[i][j * components + k] * 0xffff); \
192                                         CLAMP(a, 0, 0xffff); \
193                                 } \
194                                 else \
195                                         a = (int)((type**)input_rows)[i][j * components + k]; \
196  \
197                                 if(sizeof(type) == 4) \
198                                 { \
199                                         b = (a & 0xffff) >> 2; \
200                                         ((type**)output_rows)[i][j * components + k] = \
201                                                 (type)(a - b + 0x1800 + (EffectTV::fastrand() & 0x1000)) / 0xffff; \
202                                 } \
203                                 else \
204                                 if(sizeof(type) == 2) \
205                                 { \
206                                         b = (a & 0xffff) >> 2; \
207                                         ((type**)output_rows)[i][j * components + k] = \
208                                                 (type)(a - b + 0x1800 + (EffectTV::fastrand() & 0x1000)); \
209                                 } \
210                                 else \
211                                 { \
212                                         b = (a & 0xff) >> 2; \
213                                         ((type**)output_rows)[i][j * components + k] =  \
214                                                 (type)(a - b + 0x18 + ((EffectTV::fastrand() >> 8) & 0x10)); \
215                                 } \
216                         } \
217                 } \
218         } \
219 }
220
221 void AgingClient::coloraging(unsigned char **output_rows,
222         unsigned char **input_rows,
223         int color_model,
224         int w,
225         int h)
226 {
227         switch(color_model)
228         {
229                 case BC_RGB888:
230                 case BC_YUV888:
231                         COLORAGE(uint8_t, 3);
232                         break;
233
234                 case BC_RGB_FLOAT:
235                         COLORAGE(float, 3);
236                         break;
237
238                 case BC_RGBA_FLOAT:
239                         COLORAGE(float, 4);
240                         break;
241
242                 case BC_RGBA8888:
243                 case BC_YUVA8888:
244                         COLORAGE(uint8_t, 4);
245                         break;
246
247                 case BC_RGB161616:
248                 case BC_YUV161616:
249                         COLORAGE(uint16_t, 3);
250                         break;
251
252                 case BC_RGBA16161616:
253                 case BC_YUVA16161616:
254                         COLORAGE(uint16_t, 4);
255                         break;
256         }
257 }
258
259
260
261
262
263
264 #define SCRATCHES(type, components, chroma) \
265 { \
266         int i, j, y, y1, y2; \
267         type *p; \
268         int a, b; \
269         int w_256 = w * 256; \
270  \
271         for(i = 0; i < plugin->config.scratch_lines; i++) \
272         { \
273                 if(plugin->config.scratches[i].life)  \
274                 { \
275                         plugin->config.scratches[i].x = plugin->config.scratches[i].x + plugin->config.scratches[i].dx; \
276                         if(plugin->config.scratches[i].x < 0 || plugin->config.scratches[i].x > w_256)  \
277                         { \
278                                 plugin->config.scratches[i].life = 0; \
279                                 break; \
280                         } \
281 \
282                         p = (type*)output_rows[0] + \
283                                 (plugin->config.scratches[i].x >> 8) * \
284                                 components; \
285 \
286                         if(plugin->config.scratches[i].init)  \
287                         { \
288                                 y1 = plugin->config.scratches[i].init; \
289                                 plugin->config.scratches[i].init = 0; \
290                         }  \
291                         else  \
292                         { \
293                                 y1 = 0; \
294                         } \
295 \
296                         plugin->config.scratches[i].life--; \
297                         if(plugin->config.scratches[i].life)  \
298                         { \
299                                 y2 = h; \
300                         }  \
301                         else  \
302                         { \
303                                 y2 = EffectTV::fastrand() % h; \
304                         } \
305  \
306                         for(y = y1; y < y2; y++)  \
307                         { \
308                                 for(j = 0; j < (chroma ? 1 : 3); j++) \
309                                 { \
310                                         if(sizeof(type) == 4) \
311                                         { \
312                                                 int temp = (int)(p[j] * 0xffff); \
313                                                 CLAMP(temp, 0, 0xffff); \
314                                                 a = temp & 0xfeff; \
315                                                 a += 0x2000; \
316                                                 b = a & 0x10000; \
317                                                 p[j] = (type)(a | (b - (b >> 8))) / 0xffff; \
318                                         } \
319                                         else \
320                                         if(sizeof(type) == 2) \
321                                         { \
322                                                 int temp = (int)p[j]; \
323                                                 a = temp & 0xfeff; \
324                                                 a += 0x2000; \
325                                                 b = a & 0x10000; \
326                                                 p[j] = (type)(a | (b - (b >> 8))); \
327                                         } \
328                                         else \
329                                         { \
330                                                 int temp = (int)p[j]; \
331                                                 a = temp & 0xfe; \
332                                                 a += 0x20; \
333                                                 b = a & 0x100; \
334                                                 p[j] = (type)(a | (b - (b >> 8))); \
335                                         } \
336                                 } \
337  \
338                                 if(chroma) \
339                                 { \
340                                         p[1] = chroma; \
341                                         p[2] = chroma; \
342                                 } \
343                                 p += w * components; \
344                         } \
345                 }  \
346                 else  \
347                 { \
348                         if((EffectTV::fastrand() & 0xf0000000) == 0)  \
349                         { \
350                                 plugin->config.scratches[i].life = 2 + (EffectTV::fastrand() >> 27); \
351                                 plugin->config.scratches[i].x = EffectTV::fastrand() % (w_256); \
352                                 plugin->config.scratches[i].dx = ((int)EffectTV::fastrand()) >> 23; \
353                                 plugin->config.scratches[i].init = (EffectTV::fastrand() % (h - 1)) + 1; \
354                         } \
355                 } \
356         } \
357 }
358
359
360
361 void AgingClient::scratching(unsigned char **output_rows,
362         int color_model,
363         int w,
364         int h)
365 {
366         switch(color_model)
367         {
368                 case BC_RGB888:
369                         SCRATCHES(uint8_t, 3, 0);
370                         break;
371
372                 case BC_RGB_FLOAT:
373                         SCRATCHES(float, 3, 0);
374                         break;
375
376                 case BC_YUV888:
377                         SCRATCHES(uint8_t, 3, 0x80);
378                         break;
379
380                 case BC_RGBA_FLOAT:
381                         SCRATCHES(float, 4, 0);
382                         break;
383
384                 case BC_RGBA8888:
385                         SCRATCHES(uint8_t, 4, 0);
386                         break;
387
388                 case BC_YUVA8888:
389                         SCRATCHES(uint8_t, 4, 0x80);
390                         break;
391
392                 case BC_RGB161616:
393                         SCRATCHES(uint16_t, 3, 0);
394                         break;
395
396                 case BC_YUV161616:
397                         SCRATCHES(uint16_t, 3, 0x8000);
398                         break;
399
400                 case BC_RGBA16161616:
401                         SCRATCHES(uint16_t, 4, 0);
402                         break;
403
404                 case BC_YUVA16161616:
405                         SCRATCHES(uint16_t, 4, 0x8000);
406                         break;
407         }
408 }
409
410
411
412 #define PITS(type, components, luma, chroma) \
413 { \
414         int i, j, k; \
415         int pnum, size, pnumscale; \
416         int x, y; \
417  \
418         pnumscale = plugin->config.area_scale * 2; \
419  \
420         if(plugin->config.pits_interval)  \
421         { \
422                 pnum = pnumscale + (EffectTV::fastrand() % pnumscale); \
423                 plugin->config.pits_interval--; \
424         }  \
425         else \
426         { \
427                 pnum = EffectTV::fastrand() % pnumscale; \
428                 if((EffectTV::fastrand() & 0xf8000000) == 0)  \
429                 { \
430                         plugin->config.pits_interval = (EffectTV::fastrand() >> 28) + 20; \
431                 } \
432         } \
433  \
434         for(i = 0; i < pnum; i++)  \
435         { \
436                 x = EffectTV::fastrand() % (w - 1); \
437                 y = EffectTV::fastrand() % (h - 1); \
438  \
439                 size = EffectTV::fastrand() >> 28; \
440  \
441                 for(j = 0; j < size; j++)  \
442                 { \
443                         x = x + EffectTV::fastrand() % 3 - 1; \
444                         y = y + EffectTV::fastrand() % 3 - 1; \
445  \
446                         CLAMP(x, 0, w - 1); \
447                         CLAMP(y, 0, h - 1); \
448                         for(k = 0; k < (chroma ? 1 : 3); k++) \
449                         { \
450                                 ((type**)output_rows)[y][x * components + k] = luma; \
451                         } \
452  \
453             if(chroma) \
454                         { \
455                                 ((type**)output_rows)[y][x * components + 1] = chroma; \
456                                 ((type**)output_rows)[y][x * components + 2] = chroma; \
457                         } \
458  \
459                 } \
460         } \
461 }
462
463
464
465
466
467
468 void AgingClient::pits(unsigned char **output_rows,
469         int color_model,
470         int w,
471         int h)
472 {
473         switch(color_model)
474         {
475                 case BC_RGB888:
476                         PITS(uint8_t, 3, 0xc0, 0);
477                         break;
478                 case BC_RGB_FLOAT:
479                         PITS(float, 3, (float)0xc0 / 0xff, 0);
480                         break;
481                 case BC_YUV888:
482                         PITS(uint8_t, 3, 0xc0, 0x80);
483                         break;
484
485                 case BC_RGBA_FLOAT:
486                         PITS(float, 4, (float)0xc0 / 0xff, 0);
487                         break;
488                 case BC_RGBA8888:
489                         PITS(uint8_t, 4, 0xc0, 0);
490                         break;
491                 case BC_YUVA8888:
492                         PITS(uint8_t, 4, 0xc0, 0x80);
493                         break;
494
495                 case BC_RGB161616:
496                         PITS(uint16_t, 3, 0xc000, 0);
497                         break;
498                 case BC_YUV161616:
499                         PITS(uint16_t, 3, 0xc000, 0x8000);
500                         break;
501
502                 case BC_RGBA16161616:
503                         PITS(uint16_t, 4, 0xc000, 0);
504                         break;
505                 case BC_YUVA16161616:
506                         PITS(uint16_t, 4, 0xc000, 0x8000);
507                         break;
508         }
509 }
510
511
512 #define DUSTS(type, components, luma, chroma) \
513 { \
514         int i, j, k; \
515         int dnum; \
516         int d, len; \
517         int x, y; \
518  \
519         if(plugin->config.dust_interval == 0)  \
520         { \
521                 if((EffectTV::fastrand() & 0xf0000000) == 0)  \
522                 { \
523                         plugin->config.dust_interval = EffectTV::fastrand() >> 29; \
524                 } \
525                 return; \
526         } \
527  \
528         dnum = plugin->config.area_scale * 4 + (EffectTV::fastrand() >> 27); \
529  \
530         for(i = 0; i < dnum; i++)  \
531         { \
532                 x = EffectTV::fastrand() % w; \
533                 y = EffectTV::fastrand() % h; \
534                 d = EffectTV::fastrand() >> 29; \
535                 len = EffectTV::fastrand() % plugin->config.area_scale + 5; \
536  \
537                 for(j = 0; j < len; j++)  \
538                 { \
539                         CLAMP(x, 0, w - 1); \
540                         CLAMP(y, 0, h - 1); \
541                         for(k = 0; k < (chroma ? 1 : 3); k++) \
542                         { \
543                                 ((type**)output_rows)[y][x * components + k] = luma; \
544                         } \
545  \
546                         if(chroma) \
547                         { \
548                                 ((type**)output_rows)[y][x * components + 1] = chroma; \
549                                 ((type**)output_rows)[y][x * components + 2] = chroma; \
550                         } \
551  \
552                         y += AgingConfig::dy[d]; \
553                         x += AgingConfig::dx[d]; \
554  \
555                         if(x < 0 || x >= w) break; \
556                         if(y < 0 || y >= h) break; \
557  \
558  \
559                         d = (d + EffectTV::fastrand() % 3 - 1) & 7; \
560                 } \
561         } \
562         plugin->config.dust_interval--; \
563 }
564
565
566
567
568 void AgingClient::dusts(unsigned char **output_rows,
569         int color_model,
570         int w,
571         int h)
572 {
573         switch(color_model)
574         {
575                 case BC_RGB888:
576                         DUSTS(uint8_t, 3, 0x10, 0);
577                         break;
578
579                 case BC_RGB_FLOAT:
580                         DUSTS(float, 3, (float)0x10 / 0xff, 0);
581                         break;
582
583                 case BC_YUV888:
584                         DUSTS(uint8_t, 3, 0x10, 0x80);
585                         break;
586
587                 case BC_RGBA_FLOAT:
588                         DUSTS(float, 4, (float)0x10 / 0xff, 0);
589                         break;
590
591                 case BC_RGBA8888:
592                         DUSTS(uint8_t, 4, 0x10, 0);
593                         break;
594
595                 case BC_YUVA8888:
596                         DUSTS(uint8_t, 4, 0x10, 0x80);
597                         break;
598
599                 case BC_RGB161616:
600                         DUSTS(uint16_t, 3, 0x1000, 0);
601                         break;
602
603                 case BC_YUV161616:
604                         DUSTS(uint16_t, 3, 0x1000, 0x8000);
605                         break;
606
607                 case BC_RGBA16161616:
608                         DUSTS(uint16_t, 4, 0x1000, 0);
609                         break;
610
611                 case BC_YUVA16161616:
612                         DUSTS(uint16_t, 4, 0x1000, 0x8000);
613                         break;
614         }
615 }
616
617
618
619 void AgingClient::process_package(LoadPackage *package)
620 {
621 //printf("AgingClient::process_package 1\n");
622         AgingPackage *local_package = (AgingPackage*)package;
623         unsigned char **input_rows = plugin->input_ptr->get_rows() + local_package->row1;
624         unsigned char **output_rows = plugin->output_ptr->get_rows() + local_package->row1;
625
626 //printf("AgingClient::process_package 1\n");
627         if(plugin->config.colorage)
628                 coloraging(output_rows,
629                         input_rows,
630                         plugin->input_ptr->get_color_model(),
631                         plugin->input_ptr->get_w(),
632                         local_package->row2 - local_package->row1);
633 //printf("AgingClient::process_package 2\n");
634         if(plugin->config.scratch)
635                 scratching(output_rows,
636                         plugin->input_ptr->get_color_model(),
637                         plugin->input_ptr->get_w(),
638                         local_package->row2 - local_package->row1);
639 //printf("AgingClient::process_package 3\n");
640         if(plugin->config.pits)
641                 pits(output_rows,
642                         plugin->input_ptr->get_color_model(),
643                         plugin->input_ptr->get_w(),
644                         local_package->row2 - local_package->row1);
645 //printf("AgingClient::process_package 4 %d\n", plugin->config.dust);
646         if(plugin->config.dust)
647                 dusts(output_rows,
648                         plugin->input_ptr->get_color_model(),
649                         plugin->input_ptr->get_w(),
650                         local_package->row2 - local_package->row1);
651 //printf("AgingClient::process_package 5\n");
652 }
653
654
655
656 AgingPackage::AgingPackage()
657 {
658 }
659
660
661