Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / plugins / freezeframe / freezeframe.C
blob8242fa1355a67334660b893b30d9881b426f7db3
1 #include "bcdisplayinfo.h"
2 #include "bchash.h"
3 #include "filexml.h"
4 #include "freezeframe.h"
5 #include "language.h"
6 #include "picon_png.h"
10 #include <string.h>
13 REGISTER_PLUGIN(FreezeFrameMain)
19 FreezeFrameConfig::FreezeFrameConfig()
21         enabled = 0;
22         line_double = 0;
25 void FreezeFrameConfig::copy_from(FreezeFrameConfig &that)
27         enabled = that.enabled;
28         line_double = that.line_double;
31 int FreezeFrameConfig::equivalent(FreezeFrameConfig &that)
33         return enabled == that.enabled &&
34                 line_double == that.line_double;
37 void FreezeFrameConfig::interpolate(FreezeFrameConfig &prev, 
38         FreezeFrameConfig &next, 
39         long prev_frame, 
40         long next_frame, 
41         long current_frame)
43         this->enabled = prev.enabled;
44         this->line_double = prev.line_double;
56 FreezeFrameWindow::FreezeFrameWindow(FreezeFrameMain *client, int x, int y)
57  : BC_Window(client->get_gui_string(),
58         x,
59         y,
60         200,
61         100,
62         200,
63         100,
64         0,
65         0,
66         1)
68         this->client = client; 
71 FreezeFrameWindow::~FreezeFrameWindow()
75 int FreezeFrameWindow::create_objects()
77         int x = 10, y = 10;
78         add_tool(enabled = new FreezeFrameToggle(client, 
79                 &client->config.enabled,
80                 x, 
81                 y,
82                 _("Enabled")));
83 // Try using extra effect for the line double since it doesn't
84 // change the overhead.
85 //      y += 30;
86 //      add_tool(line_double = new FreezeFrameToggle(client, 
87 //              &client->config.line_double,
88 //              x, 
89 //              y,
90 //              _("Line double")));
91         show_window();
92         flush();
93         return 0;
96 WINDOW_CLOSE_EVENT(FreezeFrameWindow)
101 PLUGIN_THREAD_OBJECT(FreezeFrameMain, FreezeFrameThread, FreezeFrameWindow)
107 FreezeFrameToggle::FreezeFrameToggle(FreezeFrameMain *client, 
108         int *value, 
109         int x, 
110         int y,
111         char *text)
112  : BC_CheckBox(x, y, *value, text)
114         this->client = client;
115         this->value = value;
117 FreezeFrameToggle::~FreezeFrameToggle()
120 int FreezeFrameToggle::handle_event()
122         *value = get_value();
123         client->send_configure_change();
124         return 1;
138 FreezeFrameMain::FreezeFrameMain(PluginServer *server)
139  : PluginVClient(server)
141         PLUGIN_CONSTRUCTOR_MACRO
142         first_frame = 0;
143         first_frame_position = -1;
146 FreezeFrameMain::~FreezeFrameMain()
148         PLUGIN_DESTRUCTOR_MACRO
149         if(first_frame) delete first_frame;
152 char* FreezeFrameMain::plugin_title() { return N_("Freeze Frame"); }
153 int FreezeFrameMain::is_synthesis() { return 1; }
154 int FreezeFrameMain::is_realtime() { return 1; }
157 SHOW_GUI_MACRO(FreezeFrameMain, FreezeFrameThread)
159 RAISE_WINDOW_MACRO(FreezeFrameMain)
161 SET_STRING_MACRO(FreezeFrameMain)
163 NEW_PICON_MACRO(FreezeFrameMain)
165 int FreezeFrameMain::load_configuration()
167         KeyFrame *prev_keyframe = get_prev_keyframe(get_source_position());
168         int64_t prev_position = edl_to_local(prev_keyframe->position);
169         if(prev_position < get_source_start()) prev_position = get_source_start();
170         read_data(prev_keyframe);
171 // Invalidate stored frame
172         if(config.enabled) first_frame_position = prev_position;
173         return 0;
176 void FreezeFrameMain::update_gui()
178         if(thread)
179         {
180                 load_configuration();
181                 thread->window->lock_window();
182                 thread->window->enabled->update(config.enabled);
183 //              thread->window->line_double->update(config.line_double);
184                 thread->window->unlock_window();
185         }
188 void FreezeFrameMain::save_data(KeyFrame *keyframe)
190         FileXML output;
192 // cause data to be stored directly in text
193         output.set_shared_string(keyframe->data, MESSAGESIZE);
194         output.tag.set_title("FREEZEFRAME");
195         output.append_tag();
196         if(config.enabled)
197         {
198                 output.tag.set_title("ENABLED");
199                 output.append_tag();
200         }
201         if(config.line_double)
202         {
203                 output.tag.set_title("LINE_DOUBLE");
204                 output.append_tag();
205         }
206         output.terminate_string();
207 // data is now in *text
210 void FreezeFrameMain::read_data(KeyFrame *keyframe)
212         FileXML input;
214         input.set_shared_string(keyframe->data, strlen(keyframe->data));
216         int result = 0;
217         config.enabled = 0;
218         config.line_double = 0;
220         while(!result)
221         {
222                 result = input.read_tag();
224                 if(!result)
225                 {
226                         if(input.tag.title_is("ENABLED"))
227                         {
228                                 config.enabled = 1;
229                         }
230                         if(input.tag.title_is("LINE_DOUBLE"))
231                         {
232                                 config.line_double = 1;
233                         }
234                 }
235         }
238 int FreezeFrameMain::load_defaults()
240         char directory[BCTEXTLEN], string[BCTEXTLEN];
241 // set the default directory
242         sprintf(directory, "%sfreezeframe.rc", BCASTDIR);
244 // load the defaults
245         defaults = new BC_Hash(directory);
246         defaults->load();
248         config.enabled = defaults->get("ENABLED", config.enabled);
249         config.line_double = defaults->get("LINE_DOUBLE", config.line_double);
250         return 0;
253 int FreezeFrameMain::save_defaults()
255         defaults->update("ENABLED", config.enabled);
256         defaults->update("LINE_DOUBLE", config.line_double);
257         defaults->save();
258         return 0;
266 int FreezeFrameMain::process_buffer(VFrame *frame,
267                 int64_t start_position,
268                 double frame_rate)
270         int64_t previous_first_frame = first_frame_position;
271         load_configuration();
273 // Just entered frozen range
274         if(!first_frame && config.enabled)
275         {
276                 if(!first_frame)
277                         first_frame = new VFrame(0, 
278                                 frame->get_w(), 
279                                 frame->get_h(),
280                                 frame->get_color_model());
281 printf("FreezeFrameMain::process_buffer 1 %lld\n", first_frame_position);
282                 read_frame(first_frame, 
283                                 0, 
284                                 first_frame_position,
285                                 frame_rate,
286                                 get_use_opengl());
287                 if(get_use_opengl()) return run_opengl();
288                 frame->copy_from(first_frame);
289         }
290         else
291 // Still not frozen
292         if(!first_frame && !config.enabled)
293         {
294                 read_frame(frame, 
295                         0, 
296                         start_position,
297                         frame_rate,
298                         get_use_opengl());
299         }
300         else
301 // Just left frozen range
302         if(first_frame && !config.enabled)
303         {
304                 delete first_frame;
305                 first_frame = 0;
306                 read_frame(frame, 
307                         0, 
308                         start_position,
309                         frame_rate,
310                         get_use_opengl());
311         }
312         else
313 // Still frozen
314         if(first_frame && config.enabled)
315         {
316 // Had a keyframe in frozen range.  Load new first frame
317                 if(previous_first_frame != first_frame_position)
318                 {
319                         read_frame(first_frame, 
320                                 0, 
321                                 first_frame_position,
322                                 frame_rate,
323                                 get_use_opengl());
324                 }
325                 if(get_use_opengl()) return run_opengl();
326                 frame->copy_from(first_frame);
327         }
330 // Line double to support interlacing
331 //      if(config.line_double && config.enabled)
332 //      {
333 //              for(int i = 0; i < frame->get_h() - 1; i += 2)
334 //              {
335 //                      memcpy(frame->get_rows()[i + 1], 
336 //                              frame->get_rows()[i], 
337 //                              frame->get_bytes_per_line());
338 //              }
339 //      }
343         return 0;
346 int FreezeFrameMain::handle_opengl()
348 #ifdef HAVE_GL
349         get_output()->enable_opengl();
350         get_output()->init_screen();
351         first_frame->to_texture();
352         first_frame->bind_texture(0);
353         first_frame->draw_texture();
354         get_output()->set_opengl_state(VFrame::SCREEN);
355 #endif
356         return 0;