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
22 #include "bcdisplayinfo.h"
29 #include "pluginvclient.h"
44 void copy_from(YUVConfig &src);
45 int equivalent(YUVConfig &src);
46 void interpolate(YUVConfig &prev,
55 class YUVLevel : public BC_FSlider
58 YUVLevel(YUVEffect *plugin, float *output, int x, int y);
64 class YUVWindow : public PluginClientWindow
67 YUVWindow(YUVEffect *plugin);
68 void create_objects();
75 class YUVEffect : public PluginVClient
78 YUVEffect(PluginServer *server);
82 PLUGIN_CLASS_MEMBERS(YUVConfig)
83 int process_realtime(VFrame *input, VFrame *output);
85 void save_data(KeyFrame *keyframe);
86 void read_data(KeyFrame *keyframe);
94 REGISTER_PLUGIN(YUVEffect)
102 YUVConfig::YUVConfig()
109 void YUVConfig::copy_from(YUVConfig &src)
116 int YUVConfig::equivalent(YUVConfig &src)
118 return EQUIV(y, src.y) && EQUIV(u, src.u) && EQUIV(v, src.v);
121 void YUVConfig::interpolate(YUVConfig &prev,
127 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
128 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
130 y = prev.y * prev_scale + next.y * next_scale;
131 u = prev.u * prev_scale + next.u * next_scale;
132 v = prev.v * prev_scale + next.v * next_scale;
142 YUVLevel::YUVLevel(YUVEffect *plugin, float *output, int x, int y)
152 this->plugin = plugin;
153 this->output = output;
156 int YUVLevel::handle_event()
158 *output = get_value();
159 plugin->send_configure_change();
164 YUVWindow::YUVWindow(YUVEffect *plugin)
165 : PluginClientWindow(plugin,
172 this->plugin = plugin;
175 void YUVWindow::create_objects()
177 int x = 10, y = 10, x1 = 50;
178 add_subwindow(new BC_Title(x, y, _("Y:")));
179 add_subwindow(this->y = new YUVLevel(plugin, &plugin->config.y, x1, y));
181 add_subwindow(new BC_Title(x, y, _("U:")));
182 add_subwindow(u = new YUVLevel(plugin, &plugin->config.u, x1, y));
184 add_subwindow(new BC_Title(x, y, _("V:")));
185 add_subwindow(v = new YUVLevel(plugin, &plugin->config.v, x1, y));
199 YUVEffect::YUVEffect(PluginServer *server)
200 : PluginVClient(server)
204 YUVEffect::~YUVEffect()
209 const char* YUVEffect::plugin_title() { return N_("YUV"); }
210 int YUVEffect::is_realtime() { return 1; }
213 NEW_WINDOW_MACRO(YUVEffect, YUVWindow)
214 LOAD_CONFIGURATION_MACRO(YUVEffect, YUVConfig)
216 void YUVEffect::update_gui()
220 thread->window->lock_window();
221 load_configuration();
222 ((YUVWindow*)thread->window)->y->update(config.y);
223 ((YUVWindow*)thread->window)->u->update(config.u);
224 ((YUVWindow*)thread->window)->v->update(config.v);
225 thread->window->unlock_window();
229 void YUVEffect::save_data(KeyFrame *keyframe)
232 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
233 output.tag.set_title("YUV");
234 output.tag.set_property("Y", config.y);
235 output.tag.set_property("U", config.u);
236 output.tag.set_property("V", config.v);
238 output.tag.set_title("/YUV");
240 output.append_newline();
241 output.terminate_string();
244 void YUVEffect::read_data(KeyFrame *keyframe)
247 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
248 while(!input.read_tag())
250 if(input.tag.title_is("YUV"))
252 config.y = input.tag.get_property("Y", config.y);
253 config.u = input.tag.get_property("U", config.u);
254 config.v = input.tag.get_property("V", config.v);
260 #define YUV_MACRO(type, temp_type, max, components, use_yuv) \
262 for(int i = 0; i < input->get_h(); i++) \
264 type *in_row = (type*)input->get_rows()[i]; \
265 type *out_row = (type*)output->get_rows()[i]; \
266 const float round = (sizeof(type) == 4) ? 0.0 : 0.5; \
268 for(int j = 0; j < w; j++) \
272 int y = (int)((float)in_row[0] * y_scale + round); \
273 int u = (int)((float)(in_row[1] - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
274 int v = (int)((float)(in_row[2] - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
275 out_row[0] = CLIP(y, 0, max); \
276 out_row[1] = CLIP(u, 0, max); \
277 out_row[2] = CLIP(v, 0, max); \
281 temp_type y, u, v, r, g, b; \
282 if(sizeof(type) == 4) \
284 YUV::yuv.rgb_to_yuv_f(in_row[0], in_row[1], in_row[2], y, u, v); \
287 if(sizeof(type) == 2) \
289 YUV::yuv.rgb_to_yuv_16(in_row[0], in_row[1], in_row[2], y, u, v); \
293 YUV::yuv.rgb_to_yuv_8(in_row[0], in_row[1], in_row[2], y, u, v); \
296 if(sizeof(type) < 4) \
302 y = temp_type((float)y * y_scale + round); \
303 u = temp_type((float)(u - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
304 v = temp_type((float)(v - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
312 y = temp_type((float)y * y_scale + round); \
313 u = temp_type((float)u * u_scale + round); \
314 v = temp_type((float)v * v_scale + round); \
317 if(sizeof(type) == 4) \
318 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v); \
320 if(sizeof(type) == 2) \
321 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
323 YUV::yuv.yuv_to_rgb_8(r, g, b, y, u, v); \
330 in_row += components; \
331 out_row += components; \
336 int YUVEffect::process_realtime(VFrame *input, VFrame *output)
338 load_configuration();
340 if(EQUIV(config.y, 0) && EQUIV(config.u, 0) && EQUIV(config.v, 0))
342 if(input->get_rows()[0] != output->get_rows()[0])
343 output->copy_from(input);
347 int w = input->get_w();
349 float y_scale = (float)(config.y + MAXVALUE) / MAXVALUE;
350 float u_scale = (float)(config.u + MAXVALUE) / MAXVALUE;
351 float v_scale = (float)(config.v + MAXVALUE) / MAXVALUE;
353 if(u_scale > 1) u_scale = 1 + (u_scale - 1) * 4;
354 if(v_scale > 1) v_scale = 1 + (v_scale - 1) * 4;
356 switch(input->get_color_model())
359 YUV_MACRO(float, float, 1, 3, 0)
363 YUV_MACRO(unsigned char, int, 0xff, 3, 0)
367 YUV_MACRO(unsigned char, int, 0xff, 3, 1)
371 YUV_MACRO(uint16_t, int, 0xffff, 3, 0)
375 YUV_MACRO(uint16_t, int, 0xffff, 3, 1)
379 YUV_MACRO(float, float, 1, 4, 0)
383 YUV_MACRO(unsigned char, int, 0xff, 4, 0)
387 YUV_MACRO(unsigned char, int, 0xff, 4, 1)
390 case BC_RGBA16161616:
391 YUV_MACRO(uint16_t, int, 0xffff, 4, 0)
394 case BC_YUVA16161616:
395 YUV_MACRO(uint16_t, int, 0xffff, 4, 1)