add auto zoombar/status color, fix 3 batchrender boobies, rotate plugin tweaks, add...
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / fieldframe / fieldframe.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 "bcdisplayinfo.h"
23 #include "bchash.h"
24 #include "filexml.h"
25 #include "guicast.h"
26 #include "keyframe.h"
27 #include "language.h"
28 #include "mainprogress.h"
29 #include "pluginvclient.h"
30 #include "transportque.inc"
31 #include "vframe.h"
32
33 #include <string.h>
34
35
36 #define TOP_FIELD_FIRST 0
37 #define BOTTOM_FIELD_FIRST 1
38
39 class FieldFrame;
40 class FieldFrameWindow;
41
42
43
44
45
46
47
48 class FieldFrameConfig
49 {
50 public:
51         FieldFrameConfig();
52         int equivalent(FieldFrameConfig &src);
53         int field_dominance;
54         int first_frame;
55 };
56
57
58
59
60 class FieldFrameTop : public BC_Radial
61 {
62 public:
63         FieldFrameTop(FieldFrame *plugin, FieldFrameWindow *gui, int x, int y);
64         int handle_event();
65         FieldFrame *plugin;
66         FieldFrameWindow *gui;
67 };
68
69
70 class FieldFrameBottom : public BC_Radial
71 {
72 public:
73         FieldFrameBottom(FieldFrame *plugin, FieldFrameWindow *gui, int x, int y);
74         int handle_event();
75         FieldFrame *plugin;
76         FieldFrameWindow *gui;
77 };
78
79 // class FieldFrameFirst : public BC_Radial
80 // {
81 // public:
82 //      FieldFrameFirst(FieldFrame *plugin, FieldFrameWindow *gui, int x, int y);
83 //      int handle_event();
84 //      FieldFrame *plugin;
85 //      FieldFrameWindow *gui;
86 // };
87 //
88 // class FieldFrameSecond : public BC_Radial
89 // {
90 // public:
91 //      FieldFrameSecond(FieldFrame *plugin, FieldFrameWindow *gui, int x, int y);
92 //      int handle_event();
93 //      FieldFrame *plugin;
94 //      FieldFrameWindow *gui;
95 // };
96
97 class FieldFrameWindow : public PluginClientWindow
98 {
99 public:
100         FieldFrameWindow(FieldFrame *plugin);
101         void create_objects();
102         FieldFrame *plugin;
103         FieldFrameTop *top;
104         FieldFrameBottom *bottom;
105 //      FieldFrameFirst *first;
106 //      FieldFrameSecond *second;
107 };
108
109
110
111
112
113
114 class FieldFrame : public PluginVClient
115 {
116 public:
117         FieldFrame(PluginServer *server);
118         ~FieldFrame();
119
120         PLUGIN_CLASS_MEMBERS(FieldFrameConfig);
121
122         int process_buffer(VFrame *frame,
123                 int64_t start_position,
124                 double frame_rate);
125         int is_realtime();
126         void save_data(KeyFrame *keyframe);
127         void read_data(KeyFrame *keyframe);
128         void update_gui();
129         void apply_field(VFrame *output, VFrame *input, int field);
130
131
132         VFrame *input;
133 };
134
135
136
137
138
139
140
141
142 REGISTER_PLUGIN(FieldFrame)
143
144
145
146
147 FieldFrameConfig::FieldFrameConfig()
148 {
149         field_dominance = TOP_FIELD_FIRST;
150         first_frame = 0;
151 }
152
153 int FieldFrameConfig::equivalent(FieldFrameConfig &src)
154 {
155         return src.field_dominance == field_dominance &&
156                 src.first_frame == first_frame;
157 }
158
159
160
161
162
163
164
165
166 FieldFrameWindow::FieldFrameWindow(FieldFrame *plugin)
167  : PluginClientWindow(plugin,
168         230,
169         100,
170         230,
171         100,
172         0)
173 {
174         this->plugin = plugin;
175 }
176
177 void FieldFrameWindow::create_objects()
178 {
179         int x = 10, y = 10;
180         add_subwindow(top = new FieldFrameTop(plugin, this, x, y));
181         y += 30;
182         add_subwindow(bottom = new FieldFrameBottom(plugin, this, x, y));
183 //      y += 30;
184 //      add_subwindow(first = new FieldFrameFirst(plugin, this, x, y));
185 //      y += 30;
186 //      add_subwindow(second = new FieldFrameSecond(plugin, this, x, y));
187
188         show_window();
189         flush();
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203
204 FieldFrameTop::FieldFrameTop(FieldFrame *plugin,
205         FieldFrameWindow *gui,
206         int x,
207         int y)
208  : BC_Radial(x,
209         y,
210         plugin->config.field_dominance == TOP_FIELD_FIRST,
211         _("Top field first"))
212 {
213         this->plugin = plugin;
214         this->gui = gui;
215 }
216
217 int FieldFrameTop::handle_event()
218 {
219         plugin->config.field_dominance = TOP_FIELD_FIRST;
220         gui->bottom->update(0);
221         plugin->send_configure_change();
222         return 1;
223 }
224
225
226
227
228
229 FieldFrameBottom::FieldFrameBottom(FieldFrame *plugin,
230         FieldFrameWindow *gui,
231         int x,
232         int y)
233  : BC_Radial(x,
234         y,
235         plugin->config.field_dominance == BOTTOM_FIELD_FIRST,
236         _("Bottom field first"))
237 {
238         this->plugin = plugin;
239         this->gui = gui;
240 }
241
242 int FieldFrameBottom::handle_event()
243 {
244         plugin->config.field_dominance = BOTTOM_FIELD_FIRST;
245         gui->top->update(0);
246         plugin->send_configure_change();
247         return 1;
248 }
249
250
251
252
253
254 // FieldFrameFirst::FieldFrameFirst(FieldFrame *plugin,
255 //      FieldFrameWindow *gui,
256 //      int x,
257 //      int y)
258 //  : BC_Radial(x,
259 //      y,
260 //      plugin->config.first_frame == 0,
261 //      _("First frame is first field"))
262 // {
263 //      this->plugin = plugin;
264 //      this->gui = gui;
265 // }
266 //
267 // int FieldFrameFirst::handle_event()
268 // {
269 //      plugin->config.first_frame = 0;
270 //      gui->second->update(0);
271 //      plugin->send_configure_change();
272 //      return 1;
273 // }
274 //
275 //
276 //
277 //
278 // FieldFrameSecond::FieldFrameSecond(FieldFrame *plugin,
279 //      FieldFrameWindow *gui,
280 //      int x,
281 //      int y)
282 //  : BC_Radial(x,
283 //      y,
284 //      plugin->config.first_frame == 1,
285 //      _("Second frame is first field"))
286 // {
287 //      this->plugin = plugin;
288 //      this->gui = gui;
289 // }
290 //
291 // int FieldFrameSecond::handle_event()
292 // {
293 //      plugin->config.first_frame = 1;
294 //      gui->first->update(0);
295 //      plugin->send_configure_change();
296 //      return 1;
297 // }
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320 FieldFrame::FieldFrame(PluginServer *server)
321  : PluginVClient(server)
322 {
323
324         input = 0;
325 }
326
327
328 FieldFrame::~FieldFrame()
329 {
330
331
332         if(input) delete input;
333 }
334
335 const char* FieldFrame::plugin_title() { return N_("Fields to frames"); }
336 int FieldFrame::is_realtime() { return 1; }
337
338
339 NEW_WINDOW_MACRO(FieldFrame, FieldFrameWindow)
340
341 int FieldFrame::load_configuration()
342 {
343         KeyFrame *prev_keyframe;
344         FieldFrameConfig old_config = config;
345
346         prev_keyframe = get_prev_keyframe(get_source_position());
347         read_data(prev_keyframe);
348
349         return !old_config.equivalent(config);
350 }
351
352
353
354 void FieldFrame::save_data(KeyFrame *keyframe)
355 {
356         FileXML output;
357
358 // cause data to be stored directly in text
359         output.set_shared_output(keyframe->xbuf);
360         output.tag.set_title("FIELD_FRAME");
361         output.tag.set_property("DOMINANCE", config.field_dominance);
362         output.tag.set_property("FIRST_FRAME", config.first_frame);
363         output.append_tag();
364         output.tag.set_title("/FIELD_FRAME");
365         output.append_tag();
366         output.append_newline();
367         output.terminate_string();
368 }
369
370 void FieldFrame::read_data(KeyFrame *keyframe)
371 {
372         FileXML input;
373
374         input.set_shared_input(keyframe->xbuf);
375
376         while(!input.read_tag())
377         {
378                 if(input.tag.title_is("FIELD_FRAME"))
379                 {
380                         config.field_dominance = input.tag.get_property("DOMINANCE", config.field_dominance);
381                         config.first_frame = input.tag.get_property("FIRST_FRAME", config.first_frame);
382                 }
383         }
384 }
385
386
387 void FieldFrame::update_gui()
388 {
389         if(thread)
390         {
391                 if(load_configuration())
392                 {
393                         thread->window->lock_window();
394                         ((FieldFrameWindow*)thread->window)->top->update(config.field_dominance == TOP_FIELD_FIRST);
395                         ((FieldFrameWindow*)thread->window)->bottom->update(config.field_dominance == BOTTOM_FIELD_FIRST);
396 //                      thread->window->first->update(config.first_frame == 0);
397 //                      thread->window->second->update(config.first_frame == 1);
398                         thread->window->unlock_window();
399                 }
400         }
401 }
402
403
404 int FieldFrame::process_buffer(VFrame *frame,
405                 int64_t start_position,
406                 double frame_rate)
407 {
408         int result = 0;
409         load_configuration();
410
411         if(input && !input->equivalent(frame, 0))
412         {
413                 delete input;
414                 input = 0;
415         }
416
417         if(!input)
418         {
419                 input = new VFrame(frame->get_w(), frame->get_h(),
420                                 frame->get_color_model(), 0);
421         }
422
423 // Get input frames
424         int64_t field1_position = start_position * 2;
425         int64_t field2_position = start_position * 2 + 1;
426
427         if (get_direction() == PLAY_REVERSE)
428         {
429                 field1_position -= 1;
430                 field2_position -= 1;
431         }
432
433
434 // printf("FieldFrame::process_buffer %d %lld %lld\n",
435 // config.field_dominance,
436 // field1_position,
437 // field2_position);
438         read_frame(input,
439                 0,
440                 field1_position,
441                 frame_rate * 2,
442                 0);
443         apply_field(frame,
444                 input,
445                 config.field_dominance == TOP_FIELD_FIRST ? 0 : 1);
446         read_frame(input,
447                 0,
448                 field2_position,
449                 frame_rate * 2,
450                 0);
451         apply_field(frame,
452                 input,
453                 config.field_dominance == TOP_FIELD_FIRST ? 1 : 0);
454
455
456
457
458
459         return result;
460 }
461
462
463 void FieldFrame::apply_field(VFrame *output, VFrame *input, int field)
464 {
465         unsigned char **input_rows = input->get_rows();
466         unsigned char **output_rows = output->get_rows();
467         int row_size = VFrame::calculate_bytes_per_pixel(output->get_color_model()) * output->get_w();
468         for(int i = field; i < output->get_h(); i += 2)
469         {
470                 memcpy(output_rows[i], input_rows[i], row_size);
471         }
472 }