add auto zoombar/status color, fix 3 batchrender boobies, rotate plugin tweaks, add...
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / loopvideo / loopvideo.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 "clip.h"
24 #include "bchash.h"
25 #include "filexml.h"
26 #include "guicast.h"
27 #include "language.h"
28 #include "pluginvclient.h"
29 #include "transportque.h"
30
31 #include <string.h>
32
33 class LoopVideo;
34
35 class LoopVideoConfig
36 {
37 public:
38         LoopVideoConfig();
39         int64_t frames;
40 };
41
42
43 class LoopVideoFrames : public BC_TextBox
44 {
45 public:
46         LoopVideoFrames(LoopVideo *plugin,
47                 int x,
48                 int y);
49         int handle_event();
50         LoopVideo *plugin;
51 };
52
53 class LoopVideoWindow : public PluginClientWindow
54 {
55 public:
56         LoopVideoWindow(LoopVideo *plugin);
57         ~LoopVideoWindow();
58         void create_objects();
59         LoopVideo *plugin;
60         LoopVideoFrames *frames;
61 };
62
63
64 class LoopVideo : public PluginVClient
65 {
66 public:
67         LoopVideo(PluginServer *server);
68         ~LoopVideo();
69
70         PLUGIN_CLASS_MEMBERS(LoopVideoConfig)
71
72         void save_data(KeyFrame *keyframe);
73         void read_data(KeyFrame *keyframe);
74         void update_gui();
75         int is_realtime();
76         int is_synthesis();
77         int process_buffer(VFrame *frame,
78                 int64_t start_position,
79                 double frame_rate);
80 };
81
82
83
84
85
86
87
88 REGISTER_PLUGIN(LoopVideo);
89
90
91
92 LoopVideoConfig::LoopVideoConfig()
93 {
94         frames = 30;
95 }
96
97
98
99
100
101 LoopVideoWindow::LoopVideoWindow(LoopVideo *plugin)
102  : PluginClientWindow(plugin,
103         210,
104         160,
105         200,
106         160,
107         0)
108 {
109         this->plugin = plugin;
110 }
111
112 LoopVideoWindow::~LoopVideoWindow()
113 {
114 }
115
116 void LoopVideoWindow::create_objects()
117 {
118         int x = 10, y = 10;
119
120         add_subwindow(new BC_Title(x, y, _("Frames to loop:")));
121         y += 20;
122         add_subwindow(frames = new LoopVideoFrames(plugin,
123                 x,
124                 y));
125         show_window();
126         flush();
127 }
128
129
130
131
132
133
134
135
136
137
138
139 LoopVideoFrames::LoopVideoFrames(LoopVideo *plugin,
140         int x,
141         int y)
142  : BC_TextBox(x,
143         y,
144         100,
145         1,
146         plugin->config.frames)
147 {
148         this->plugin = plugin;
149 }
150
151 int LoopVideoFrames::handle_event()
152 {
153         plugin->config.frames = atol(get_text());
154         plugin->config.frames = MAX(1, plugin->config.frames);
155         plugin->send_configure_change();
156         return 1;
157 }
158
159
160
161
162
163
164
165
166
167 LoopVideo::LoopVideo(PluginServer *server)
168  : PluginVClient(server)
169 {
170
171 }
172
173
174 LoopVideo::~LoopVideo()
175 {
176
177 }
178
179 const char* LoopVideo::plugin_title() { return N_("Loop video"); }
180 int LoopVideo::is_realtime() { return 1; }
181 int LoopVideo::is_synthesis() { return 1; }
182
183
184 NEW_WINDOW_MACRO(LoopVideo, LoopVideoWindow)
185
186
187
188 int LoopVideo::process_buffer(VFrame *frame,
189                 int64_t start_position,
190                 double frame_rate)
191 {
192         int64_t current_loop_position;
193
194 // Truncate to next keyframe
195         if(get_direction() == PLAY_FORWARD)
196         {
197 // Get start of current loop
198                 KeyFrame *prev_keyframe = get_prev_keyframe(start_position);
199                 int64_t prev_position = edl_to_local(prev_keyframe->position);
200                 if(prev_position == 0)
201                         prev_position = get_source_start();
202                 read_data(prev_keyframe);
203
204 // Get start of fragment in current loop
205                 current_loop_position = prev_position +
206                         ((start_position - prev_position) %
207                                 config.frames);
208                 while(current_loop_position < prev_position) current_loop_position += config.frames;
209                 while(current_loop_position >= prev_position + config.frames) current_loop_position -= config.frames;
210         }
211         else
212         {
213                 KeyFrame *prev_keyframe = get_next_keyframe(start_position);
214                 int64_t prev_position = edl_to_local(prev_keyframe->position);
215                 if(prev_position == 0)
216                         prev_position = get_source_start() + get_total_len();
217                 read_data(prev_keyframe);
218
219                 current_loop_position = prev_position -
220                         ((prev_position - start_position) %
221                                   config.frames);
222                 while(current_loop_position <= prev_position - config.frames) current_loop_position += config.frames;
223                 while(current_loop_position > prev_position) current_loop_position -= config.frames;
224         }
225
226
227 // printf("LoopVideo::process_buffer 100 %lld %lld %lld %d\n",
228 // current_position, current_loop_position, current_loop_end, fragment_size);
229         read_frame(frame,
230                 0,
231                 current_loop_position,
232                 frame_rate,
233                 0);
234
235         return 0;
236 }
237
238
239
240
241 int LoopVideo::load_configuration()
242 {
243         KeyFrame *prev_keyframe;
244         int64_t old_frames = config.frames;
245         prev_keyframe = get_prev_keyframe(get_source_position());
246         read_data(prev_keyframe);
247         config.frames = MAX(config.frames, 1);
248         return old_frames != config.frames;
249 }
250
251
252 void LoopVideo::save_data(KeyFrame *keyframe)
253 {
254         FileXML output;
255
256 // cause data to be stored directly in text
257         output.set_shared_output(keyframe->xbuf);
258         output.tag.set_title("LOOPVIDEO");
259         output.tag.set_property("FRAMES", config.frames);
260         output.append_tag();
261         output.tag.set_title("/LOOPVIDEO");
262         output.append_tag();
263         output.append_newline();
264         output.terminate_string();
265 }
266
267 void LoopVideo::read_data(KeyFrame *keyframe)
268 {
269         FileXML input;
270
271         input.set_shared_input(keyframe->xbuf);
272
273         while(!input.read_tag())
274         {
275                 if(input.tag.title_is("LOOPVIDEO"))
276                 {
277                         config.frames = input.tag.get_property("FRAMES", config.frames);
278                 }
279         }
280 }
281
282 void LoopVideo::update_gui()
283 {
284         if(thread)
285         {
286                 load_configuration();
287                 thread->window->lock_window();
288                 ((LoopVideoWindow*)thread->window)->frames->update(config.frames);
289                 thread->window->unlock_window();
290         }
291 }
292
293
294
295
296