4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #include "bccmodels.h"
27 #include "holowindow.h"
36 REGISTER_PLUGIN(HoloMain)
47 HoloConfig::HoloConfig()
57 HoloMain::HoloMain(PluginServer *server)
58 : PluginVClient(server)
76 const char* HoloMain::plugin_title() { return N_("HolographicTV"); }
77 int HoloMain::is_realtime() { return 1; }
79 NEW_WINDOW_MACRO(HoloMain, HoloWindow)
81 int HoloMain::load_configuration()
87 void HoloMain::save_data(KeyFrame *keyframe)
91 void HoloMain::read_data(KeyFrame *keyframe)
95 void HoloMain::reconfigure()
99 effecttv->image_set_threshold_y(config.threshold);
103 #define ADD_FRAMES(type, components) \
105 type **input_rows = (type**)input->get_rows(); \
106 type **output_rows = (type**)output->get_rows(); \
107 int w = input->get_w(); \
108 int h = input->get_h(); \
110 for(int i = 0; i < h; i++) \
112 type *output_row = (type*)output_rows[i]; \
113 type *input_row = (type*)input_rows[i]; \
115 for(int j = 0; j < w; j++) \
117 for(int k = 0; k < 3; k++) \
119 if(sizeof(type) == 4) \
121 int in_temp = (int)(*input_row * 0xffff); \
122 int out_temp = (int)(*output_row * 0xffff); \
123 int temp = (in_temp & out_temp) + \
124 ((in_temp ^ out_temp) >> 1); \
125 *output_row = (type)temp / 0xffff; \
129 *output_row = ((uint16_t)*input_row & (uint16_t)*output_row) + \
130 (((uint16_t)*input_row ^ (uint16_t)*output_row) >> 1); \
136 if(components == 4) \
146 // Add input to output and store result in output
147 void HoloMain::add_frames(VFrame *output, VFrame *input)
149 switch(output->get_color_model())
153 ADD_FRAMES(uint8_t, 3);
156 ADD_FRAMES(float, 3);
159 ADD_FRAMES(float, 4);
163 ADD_FRAMES(uint8_t, 4);
167 ADD_FRAMES(uint16_t, 3);
169 case BC_RGBA16161616:
170 case BC_YUVA16161616:
171 ADD_FRAMES(uint16_t, 4);
176 void HoloMain::set_background()
179 * grab 4 frames and composite them to get a quality background image
182 * For Cinelerra, we make every frame a holograph and expect the user to
190 /* step 1: grab frame-1 to buffer-1 */
191 // tmp = new VFrame(input_ptr->get_w(),input_ptr->get_h(),
192 // project_color_model, 0);
193 bgimage->copy_from(input_ptr);
197 /* step 2: add frame-2 to buffer-1 */
198 add_frames(bgimage, input_ptr);
202 /* step 3: grab frame-3 to buffer-2 */
203 tmp->copy_from(input_ptr);
207 /* step 4: add frame-4 to buffer-2 */
208 add_frames(tmp, input_ptr);
212 /* step 5: add buffer-3 to buffer-1 */
213 add_frames(bgimage, tmp);
215 effecttv->image_bgset_y(bgimage);
224 int HoloMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
226 this->input_ptr = input_ptr;
227 this->output_ptr = output_ptr;
232 load_configuration();
240 effecttv = new EffectTV(input_ptr->get_w(), input_ptr->get_h());
241 bgimage = new VFrame(input_ptr->get_w(), input_ptr->get_h(),
242 input_ptr->get_color_model(), 0);
244 for(int i = 0; i < 256; i++)
246 noisepattern[i] = (i * i * i / 40000)* i / 256;
249 holo_server = new HoloServer(this, 1, 1);
257 holo_server->process_packages();
260 if(total >= config.recycle * project_frame_rate)
270 HoloServer::HoloServer(HoloMain *plugin, int total_clients, int total_packages)
271 : LoadServer(total_clients, total_packages)
273 this->plugin = plugin;
277 LoadClient* HoloServer::new_client()
279 return new HoloClient(this);
285 LoadPackage* HoloServer::new_package()
287 return new HoloPackage;
292 void HoloServer::init_packages()
294 for(int i = 0; i < get_total_packages(); i++)
296 HoloPackage *package = (HoloPackage*)get_package(i);
297 package->row1 = plugin->input_ptr->get_h() * i / get_total_packages();
298 package->row2 = plugin->input_ptr->get_h() * (i + 1) / get_total_packages();
309 HoloClient::HoloClient(HoloServer *server)
312 this->plugin = server->plugin;
317 void HoloClient::process_package(LoadPackage *package)
320 HoloPackage *local_package = (HoloPackage*)package;
321 unsigned char **input_rows = plugin->input_ptr->get_rows() + local_package->row1;
322 unsigned char **output_rows = plugin->output_ptr->get_rows() + local_package->row1;
323 int width = plugin->input_ptr->get_w();
324 int height = local_package->row2 - local_package->row1;
329 diff = plugin->effecttv->image_diff_filter(plugin->effecttv->image_bgsubtract_y(input_rows,
330 plugin->input_ptr->get_color_model()));
337 // Convert discrete channels to a single 24 bit number
338 #define STORE_PIXEL(type, components, dest, src, is_yuv) \
339 if(sizeof(type) == 4) \
341 int r = (int)(src[0] * 0xff); \
342 int g = (int)(src[1] * 0xff); \
343 int b = (int)(src[2] * 0xff); \
347 dest = (r << 16) | (g << 8) | b; \
350 if(sizeof(type) == 2) \
354 int r = (int)src[0] >> 8; \
355 int g = (int)src[1] >> 8; \
356 int b = (int)src[2] >> 8; \
357 YUV::yuv.yuv_to_rgb_8(r, g, b); \
358 dest = (r << 16) | (g << 8) | b; \
362 dest = (((uint32_t)src[0] << 8) & 0xff0000) | \
363 ((uint32_t)src[1] & 0xff00) | \
364 ((uint32_t)src[2]) >> 8; \
371 int r = (int)src[0]; \
372 int g = (int)src[1]; \
373 int b = (int)src[2]; \
374 YUV::yuv.yuv_to_rgb_8(r, g, b); \
375 dest = (r << 16) | (g << 8) | b; \
379 dest = ((uint32_t)src[0] << 16) | \
380 ((uint32_t)src[1] << 8) | \
388 #define HOLO_CORE(type, components, is_yuv) \
389 for(y = 1; y < height - 1; y++) \
391 type *src = (type*)input_rows[y]; \
392 type *bg = (type*)plugin->bgimage->get_rows()[y]; \
393 type *dest = (type*)output_rows[y]; \
397 if(((y + phase) & 0x7f) < 0x58) \
399 for(x = 0 ; x < width; x++) \
403 STORE_PIXEL(type, components, s, src, is_yuv); \
406 ((s & 0xff00) >> 7) + \
407 ((s & 0xff0000) >> 16); \
408 t += plugin->noisepattern[EffectTV::fastrand() >> 24]; \
410 r = ((s & 0xff0000) >> 17) + t; \
411 g = ((s & 0xff00) >> 8) + t; \
412 b = (s & 0xff) + t; \
414 r = (r >> 1) - 100; \
415 g = (g >> 1) - 100; \
421 STORE_PIXEL(type, components, s, bg, is_yuv); \
423 r += (s & 0xff0000) >> 17; \
424 g += (s & 0xff00) >> 9; \
425 b += ((s & 0xff) >> 1) + 40; \
427 if(r > 255) r = 255; \
428 if(g > 255) g = 255; \
429 if(b > 255) b = 255; \
431 if(is_yuv) YUV::yuv.rgb_to_yuv_8(r, g, b); \
432 if(sizeof(type) == 4) \
434 dest[0] = (type)r / 0xff; \
435 dest[1] = (type)g / 0xff; \
436 dest[2] = (type)b / 0xff; \
439 if(sizeof(type) == 2) \
441 dest[0] = (r << 8) | r; \
442 dest[1] = (g << 8) | g; \
443 dest[2] = (b << 8) | b; \
461 dest += components; \
467 for(x = 0; x < width; x++) \
471 STORE_PIXEL(type, components, s, src, is_yuv); \
474 t = (s & 0xff) + ((s & 0xff00) >> 6) + ((s & 0xff0000) >> 16); \
475 t += plugin->noisepattern[EffectTV::fastrand() >> 24]; \
477 r = ((s & 0xff0000) >> 16) + t; \
478 g = ((s & 0xff00) >> 8) + t; \
479 b = (s & 0xff) + t; \
481 r = (r >> 1) - 100; \
482 g = (g >> 1) - 100; \
488 STORE_PIXEL(type, components, s, bg, is_yuv); \
490 r += ((s & 0xff0000) >> 17) + 10; \
491 g += ((s & 0xff00) >> 9) + 10; \
492 b += ((s & 0xff) >> 1) + 40; \
494 if(r > 255) r = 255; \
495 if(g > 255) g = 255; \
496 if(b > 255) b = 255; \
498 if(is_yuv) YUV::yuv.rgb_to_yuv_8(r, g, b); \
499 if(sizeof(type) == 4) \
501 dest[0] = (type)r / 0xff; \
502 dest[1] = (type)g / 0xff; \
503 dest[2] = (type)b / 0xff; \
506 if(sizeof(type) == 2) \
508 dest[0] = (r << 8) | r; \
509 dest[1] = (g << 8) | g; \
510 dest[2] = (b << 8) | b; \
528 dest += components; \
537 switch(plugin->input_ptr->get_color_model())
540 HOLO_CORE(uint8_t, 3, 0);
543 HOLO_CORE(float, 3, 0);
546 HOLO_CORE(uint8_t, 3, 1);
549 HOLO_CORE(float, 4, 0);
552 HOLO_CORE(uint8_t, 4, 0);
555 HOLO_CORE(uint8_t, 4, 1);
558 HOLO_CORE(uint16_t, 3, 0);
561 HOLO_CORE(uint16_t, 3, 1);
563 case BC_RGBA16161616:
564 HOLO_CORE(uint16_t, 4, 0);
566 case BC_YUVA16161616:
567 HOLO_CORE(uint16_t, 4, 1);
578 HoloPackage::HoloPackage()