r955: Fix the Diffkey icon.
[cinelerra_cv.git] / plugins / delayvideo / delayvideo.C
blobf5271548474a3bb8a6db11397c5926a517a4f439
1 #include "bcdisplayinfo.h"
2 #include "clip.h"
3 #include "bchash.h"
4 #include "delayvideo.h"
5 #include "filexml.h"
6 #include "language.h"
7 #include "picon_png.h"
8 #include "vframe.h"
13 #include <string.h>
20 REGISTER_PLUGIN(DelayVideo)
26 DelayVideoConfig::DelayVideoConfig()
28         length = 0;
31 int DelayVideoConfig::equivalent(DelayVideoConfig &that)
33         return EQUIV(length, that.length);
36 void DelayVideoConfig::copy_from(DelayVideoConfig &that)
38         length = that.length;
41 void DelayVideoConfig::interpolate(DelayVideoConfig &prev, 
42                 DelayVideoConfig &next, 
43                 int64_t prev_frame, 
44                 int64_t next_frame, 
45                 int64_t current_frame)
47         this->length = prev.length;
52 DelayVideoWindow::DelayVideoWindow(DelayVideo *plugin, int x, int y)
53  : BC_Window(plugin->gui_string, 
54         x,
55         y,
56         210, 
57         120, 
58         210, 
59         120, 
60         0, 
61         0,
62         1)
64         this->plugin = plugin;
67 DelayVideoWindow::~DelayVideoWindow()
71         
72 void DelayVideoWindow::create_objects()
74         int x = 10, y = 10;
75         
76         add_subwindow(new BC_Title(x, y, _("Delay seconds:")));
77         y += 20;
78         add_subwindow(slider = new DelayVideoSlider(plugin, x, y));
80         show_window();
81         flush();
84 WINDOW_CLOSE_EVENT(DelayVideoWindow)
86 void DelayVideoWindow::update_gui()
88         char string[BCTEXTLEN];
89         sprintf(string, "%.04f", plugin->config.length);
90         slider->update(string);
104 DelayVideoSlider::DelayVideoSlider(DelayVideo *plugin, int x, int y)
105  : BC_TextBox(x, y, 150, 1, plugin->config.length)
107         this->plugin = plugin;
110 int DelayVideoSlider::handle_event()
112         plugin->config.length = atof(get_text());
113         plugin->send_configure_change();
114         return 1;
127 PLUGIN_THREAD_OBJECT(DelayVideo, DelayVideoThread, DelayVideoWindow)
135 DelayVideo::DelayVideo(PluginServer *server)
136  : PluginVClient(server)
138         reset();
139         load_defaults();
142 DelayVideo::~DelayVideo()
144         PLUGIN_DESTRUCTOR_MACRO
146         if(buffer)
147         {
148 //printf("DelayVideo::~DelayVideo 1\n");
149                 for(int i = 0; i < allocation; i++)
150                         delete buffer[i];
151 //printf("DelayVideo::~DelayVideo 1\n");
152         
153                 delete [] buffer;
154 //printf("DelayVideo::~DelayVideo 1\n");
155         }
158 void DelayVideo::reset()
160         thread = 0;
161         defaults = 0;
162         need_reconfigure = 1;
163         buffer = 0;
164         allocation = 0;
167 void DelayVideo::reconfigure()
169         int new_allocation = 1 + (int)(config.length * PluginVClient::project_frame_rate);
170         VFrame **new_buffer = new VFrame*[new_allocation];
171         int reuse = MIN(new_allocation, allocation);
173         for(int i = 0; i < reuse; i++)
174         {
175                 new_buffer[i] = buffer[i];
176         }
178         for(int i = reuse; i < new_allocation; i++)
179         {
180                 new_buffer[i] = new VFrame(0, 
181                         input->get_w(),
182                         input->get_h(),
183                         PluginVClient::project_color_model);
184         }
186         for(int i = reuse; i < allocation; i++)
187         {
188                 delete buffer[i];
189         }
191         if(buffer) delete [] buffer;
192         
193         
194         buffer = new_buffer;
195         allocation = new_allocation;
196         
197         need_reconfigure = 0;
200 int DelayVideo::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
202 //printf("DelayVideo::process_realtime 1 %d\n", config.length);
203         this->input = input_ptr;
204         this->output = output_ptr;
205         need_reconfigure += load_configuration();
206         CLAMP(config.length, 0, 10);
208 //printf("DelayVideo::process_realtime 2 %d\n", config.length);
209         if(need_reconfigure) reconfigure();
210 //printf("DelayVideo::process_realtime 3 %d %d\n", config.length, allocation);
212         buffer[allocation - 1]->copy_from(input_ptr);
213         output_ptr->copy_from(buffer[0]);
214         
215         VFrame *temp = buffer[0];
216         for(int i = 0; i < allocation - 1; i++)
217         {
218                 buffer[i] = buffer[i + 1];
219         }
221         buffer[allocation - 1] = temp;
222 //printf("DelayVideo::process_realtime 4\n");
223         
224         
225         return 0;
228 int DelayVideo::is_realtime()
230         return 1;
233 char* DelayVideo::plugin_title() { return N_("Delay Video"); }
235 SET_STRING_MACRO(DelayVideo)
237 NEW_PICON_MACRO(DelayVideo)
239 LOAD_CONFIGURATION_MACRO(DelayVideo, DelayVideoConfig)
241 SHOW_GUI_MACRO(DelayVideo, DelayVideoThread)
243 RAISE_WINDOW_MACRO(DelayVideo)
246 void DelayVideo::save_data(KeyFrame *keyframe)
248         FileXML output;
249         output.set_shared_string(keyframe->data, MESSAGESIZE);
251         output.tag.set_title("DELAYVIDEO");
252         output.tag.set_property("LENGTH", (double)config.length);
253         output.append_tag();
254         output.append_newline();
255         output.terminate_string();
258 void DelayVideo::read_data(KeyFrame *keyframe)
260         FileXML input;
261         input.set_shared_string(keyframe->data, strlen(keyframe->data));
263         int result = 0;
264         while(!result)
265         {
266                 result = input.read_tag();
268                 if(!result)
269                 {
270                         if(input.tag.title_is("DELAYVIDEO"))
271                         {
272                                 config.length = input.tag.get_property("LENGTH", (double)config.length);
273                         }
274                 }
275         }
278 void DelayVideo::update_gui()
280         if(thread) 
281         {
282                 load_configuration();
283                 thread->window->lock_window();
284                 thread->window->slider->update(config.length);
285                 thread->window->unlock_window();
286         }
292 int DelayVideo::load_defaults()
294         char directory[BCTEXTLEN];
295         sprintf(directory, "%sdelayvideo.rc", BCASTDIR);
296         defaults = new BC_Hash(directory);
297         defaults->load();
298         config.length = defaults->get("LENGTH", (double)1);
299         return 0;
302 int DelayVideo::save_defaults()
304         defaults->update("LENGTH", config.length);
305         defaults->save();
306         return 0;