update ffmpeg.git files for newer versions
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / burn / burn.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 "effecttv.h"
25 #include "filexml.h"
26 #include "language.h"
27 #include "bccolors.h"
28 #include "burn.h"
29 #include "burnwindow.h"
30
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <string.h>
34
35
36
37 REGISTER_PLUGIN(BurnMain)
38
39
40
41
42
43
44
45
46
47
48
49
50 BurnConfig::BurnConfig()
51 {
52         threshold = 50;
53         decay = 15;
54         recycle = 1.0;
55 }
56
57 BurnMain::BurnMain(PluginServer *server)
58  : PluginVClient(server)
59 {
60         input_ptr = 0;
61         output_ptr = 0;
62         burn_server = 0;
63         buffer = 0;
64         effecttv = 0;
65 }
66
67 BurnMain::~BurnMain()
68 {
69
70
71         if(buffer) delete [] buffer;
72         if(burn_server) delete burn_server;
73         if(effecttv) delete effecttv;
74 }
75
76 const char* BurnMain::plugin_title() { return N_("BurningTV"); }
77 int BurnMain::is_realtime() { return 1; }
78
79
80 NEW_WINDOW_MACRO(BurnMain, BurnWindow)
81
82
83 int BurnMain::load_configuration()
84 {
85         return 0;
86 //printf("BurnMain::load_configuration %d\n", source_position);
87 }
88
89
90 void BurnMain::save_data(KeyFrame *keyframe)
91 {
92 }
93
94 void BurnMain::read_data(KeyFrame *keyframe)
95 {
96 }
97
98
99
100 #define MAXCOLOR 120
101
102 void BurnMain::HSItoRGB(double H,
103         double S,
104         double I,
105         int *r,
106         int *g,
107         int *b,
108         int color_model)
109 {
110         double T, Rv, Gv, Bv;
111
112         T = H;
113         Rv = 1 + S * sin(T - 2 * M_PI / 3);
114         Gv = 1 + S * sin(T);
115         Bv = 1 + S * sin(T + 2  * M_PI / 3);
116         T = 255.999 * I / 2;
117
118         *r = (int)CLIP(Rv * T, 0, 255);
119         *g = (int)CLIP(Gv * T, 0, 255);
120         *b = (int)CLIP(Bv * T, 0, 255);
121 }
122
123
124 void BurnMain::make_palette(int color_model)
125 {
126         int i, r, g, b;
127
128         for(i = 0; i < MAXCOLOR; i++)
129         {
130                 HSItoRGB(4.6 - 1.5 * i / MAXCOLOR,
131                         (double)i / MAXCOLOR,
132                         (double)i / MAXCOLOR,
133                         &r,
134                         &g,
135                         &b,
136                         color_model);
137                 palette[0][i] = r;
138                 palette[1][i] = g;
139                 palette[2][i] = b;
140 //printf("BurnMain::make_palette %d %d %d %d\n", i, palette[0][i], palette[1][i], palette[2][i]);
141         }
142
143
144         for(i = MAXCOLOR; i < 256; i++)
145         {
146                 if(r < 255) r++;
147                 if(r < 255) r++;
148                 if(r < 255) r++;
149                 if(g < 255) g++;
150                 if(g < 255) g++;
151                 if(b < 255) b++;
152                 if(b < 255) b++;
153                 palette[0][i] = r;
154                 palette[1][i] = g;
155                 palette[2][i] = b;
156 //printf("BurnMain::make_palette %d %d %d %d\n", i, palette[0][i], palette[1][i], palette[2][i]);
157         }
158 }
159
160
161
162
163 int BurnMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
164 {
165         this->input_ptr = input_ptr;
166         this->output_ptr = output_ptr;
167
168         load_configuration();
169
170         if(!burn_server)
171         {
172                 effecttv = new EffectTV(input_ptr->get_w(), input_ptr->get_h());
173                 buffer = (unsigned char *)new unsigned char[input_ptr->get_w() * input_ptr->get_h()];
174                 make_palette(input_ptr->get_color_model());
175
176                 effecttv->image_set_threshold_y(config.threshold);
177                 total = 0;
178
179                 burn_server = new BurnServer(this, 1, 1);
180         }
181
182         if(total == 0)
183         {
184                 bzero(buffer, input_ptr->get_w() * input_ptr->get_h());
185                 effecttv->image_bgset_y(input_ptr);
186         }
187         burn_server->process_packages();
188
189         total++;
190 //      if(total >= config.recycle * project_frame_rate) total = 0;
191         return 0;
192 }
193
194
195
196 BurnServer::BurnServer(BurnMain *plugin, int total_clients, int total_packages)
197  : LoadServer(total_clients, total_packages)
198 {
199         this->plugin = plugin;
200 }
201
202
203 LoadClient* BurnServer::new_client()
204 {
205         return new BurnClient(this);
206 }
207
208
209
210
211 LoadPackage* BurnServer::new_package()
212 {
213         return new BurnPackage;
214 }
215
216
217
218 void BurnServer::init_packages()
219 {
220         for(int i = 0; i < get_total_packages(); i++)
221         {
222                 BurnPackage *package = (BurnPackage*)get_package(i);
223                 package->row1 = plugin->input_ptr->get_h() * i / get_total_packages();
224                 package->row2 = plugin->input_ptr->get_h() * (i + 1) / get_total_packages();
225         }
226 }
227
228
229
230
231
232
233
234
235 BurnClient::BurnClient(BurnServer *server)
236  : LoadClient(server)
237 {
238         this->plugin = server->plugin;
239 }
240
241
242
243
244
245
246
247
248
249 #define BURN(type, components, is_yuv) \
250 { \
251         i = 1; \
252         type **rows = (type**)input_rows; \
253         for(y = 0; y < height; y++)  \
254         { \
255                 for(x = 1; x < width - 1; x++)  \
256                 { \
257                         if(sizeof(type) == 4) \
258                         { \
259                                 a1 = (int)(rows[0][i * components] * 0xff); \
260                                 a2 = (int)(rows[0][i * components + 1] * 0xff); \
261                                 a3 = (int)(rows[0][i * components + 2] * 0xff); \
262                                 CLAMP(a1, 0, 0xff); \
263                                 CLAMP(a2, 0, 0xff); \
264                                 CLAMP(a3, 0, 0xff); \
265                                 b1 = plugin->palette[0][plugin->buffer[i]]; \
266                                 b2 = plugin->palette[1][plugin->buffer[i]]; \
267                                 b3 = plugin->palette[2][plugin->buffer[i]]; \
268                                 a1 += b1; \
269                                 a2 += b2; \
270                                 a3 += b3; \
271                                 b1 = a1 & 0x100; \
272                                 b2 = a2 & 0x100; \
273                                 b3 = a3 & 0x100; \
274                                 rows[0][i * components] = (type)(a1 | (b1 - (b1 >> 8))) / 0xff; \
275                                 rows[0][i * components + 1] = (type)(a2 | (b2 - (b2 >> 8))) / 0xff; \
276                                 rows[0][i * components + 2] = (type)(a3 | (b3 - (b3 >> 8))) / 0xff; \
277                         } \
278                         else \
279                         if(sizeof(type) == 2) \
280                         { \
281                                 a1 = ((int)rows[0][i * components + 0]) >> 8; \
282                                 a2 = ((int)rows[0][i * components + 1]) >> 8; \
283                                 a3 = ((int)rows[0][i * components + 2]) >> 8; \
284                                 b1 = plugin->palette[0][plugin->buffer[i]]; \
285                                 b2 = plugin->palette[1][plugin->buffer[i]]; \
286                                 b3 = plugin->palette[2][plugin->buffer[i]]; \
287                                 if(is_yuv) YUV::yuv.yuv_to_rgb_8(a1, a2, a3); \
288                                 a1 += b1; \
289                                 a2 += b2; \
290                                 a3 += b3; \
291                                 b1 = a1 & 0x100; \
292                                 b2 = a2 & 0x100; \
293                                 b3 = a3 & 0x100; \
294                                 a1 = (a1 | (b1 - (b1 >> 8))); \
295                                 a2 = (a2 | (b2 - (b2 >> 8))); \
296                                 a3 = (a3 | (b3 - (b3 >> 8))); \
297                                 if(is_yuv) \
298                                 { \
299                                         CLAMP(a1, 0, 0xff); \
300                                         CLAMP(a2, 0, 0xff); \
301                                         CLAMP(a3, 0, 0xff); \
302                                         YUV::yuv.rgb_to_yuv_8(a1, a2, a3); \
303                                 } \
304                                 rows[0][i * components + 0] = a1 | (a1 << 8); \
305                                 rows[0][i * components + 1] = a2 | (a2 << 8); \
306                                 rows[0][i * components + 2] = a3 | (a3 << 8); \
307                         } \
308                         else \
309                         { \
310                                 a1 = (int)rows[0][i * components + 0]; \
311                                 a2 = (int)rows[0][i * components + 1]; \
312                                 a3 = (int)rows[0][i * components + 2]; \
313                                 b1 = plugin->palette[0][plugin->buffer[i]]; \
314                                 b2 = plugin->palette[1][plugin->buffer[i]]; \
315                                 b3 = plugin->palette[2][plugin->buffer[i]]; \
316                                 if(is_yuv) YUV::yuv.yuv_to_rgb_8(a1, a2, a3); \
317                                 a1 += b1; \
318                                 a2 += b2; \
319                                 a3 += b3; \
320                                 b1 = a1 & 0x100; \
321                                 b2 = a2 & 0x100; \
322                                 b3 = a3 & 0x100; \
323                                 a1 = (a1 | (b1 - (b1 >> 8))); \
324                                 a2 = (a2 | (b2 - (b2 >> 8))); \
325                                 a3 = (a3 | (b3 - (b3 >> 8))); \
326                                 if(is_yuv) \
327                                 { \
328                                         CLAMP(a1, 0, 0xff); \
329                                         CLAMP(a2, 0, 0xff); \
330                                         CLAMP(a3, 0, 0xff); \
331                                         YUV::yuv.rgb_to_yuv_8(a1, a2, a3); \
332                                 } \
333                                 rows[0][i * components + 0] = a1; \
334                                 rows[0][i * components + 1] = a2; \
335                                 rows[0][i * components + 2] = a3; \
336                         } \
337                         i++; \
338                 } \
339                 i += 2; \
340         } \
341 }
342
343
344
345
346 void BurnClient::process_package(LoadPackage *package)
347 {
348         BurnPackage *local_package = (BurnPackage*)package;
349         unsigned char **input_rows = plugin->input_ptr->get_rows() + local_package->row1;
350         //unsigned char **output_rows = plugin->output_ptr->get_rows() + local_package->row1;
351         int width = plugin->input_ptr->get_w();
352         int height = local_package->row2 - local_package->row1;
353         unsigned char *diff;
354         //int pitch = width * plugin->input_ptr->get_bytes_per_pixel();
355         int i, x, y;
356         int a1, a2, a3;
357         int b1, b2, b3;
358
359         diff = plugin->effecttv->image_bgsubtract_y(input_rows,
360                 plugin->input_ptr->get_color_model());
361
362         for(x = 1; x < width - 1; x++)
363         {
364                 unsigned int v = 0;
365                 for(y = 0; y < height - 1; y++)
366                 {
367                         unsigned int w = diff[y * width + x];
368                         plugin->buffer[y * width + x] |= v ^ w;
369                         v = w;
370                 }
371         }
372
373         for(x = 1; x < width - 1; x++)
374         {
375                 i = width + x;
376
377                 for(y = 1; y < height; y++)
378                 {
379                         int v = plugin->buffer[i];
380
381                         if(v < plugin->config.decay)
382                                 plugin->buffer[i - width] = 0;
383                         else
384                                 plugin->buffer[i - width + EffectTV::fastrand() % 3 - 1] =
385                                         v - (EffectTV::fastrand() & plugin->config.decay);
386
387                         i += width;
388                 }
389         }
390
391
392
393         switch(plugin->input_ptr->get_color_model())
394         {
395                 case BC_RGB888:
396                         BURN(uint8_t, 3, 0);
397                         break;
398                 case BC_YUV888:
399                         BURN(uint8_t, 3, 1);
400                         break;
401
402                 case BC_RGB_FLOAT:
403                         BURN(float, 3, 0);
404                         break;
405
406                 case BC_RGBA_FLOAT:
407                         BURN(float, 4, 0);
408                         break;
409
410                 case BC_RGBA8888:
411                         BURN(uint8_t, 4, 0);
412                         break;
413                 case BC_YUVA8888:
414                         BURN(uint8_t, 4, 1);
415                         break;
416
417                 case BC_RGB161616:
418                         BURN(uint16_t, 3, 0);
419                         break;
420                 case BC_YUV161616:
421                         BURN(uint16_t, 3, 1);
422                         break;
423
424                 case BC_RGBA16161616:
425                         BURN(uint16_t, 4, 0);
426                         break;
427                 case BC_YUVA16161616:
428                         BURN(uint16_t, 4, 1);
429                         break;
430         }
431
432
433 }
434
435
436
437 BurnPackage::BurnPackage()
438 {
439 }
440
441
442