initial commit
[goodguy/history.git] / cinelerra-5.0 / cinelerra / vtrack.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 "asset.h"
23 #include "autoconf.h"
24 #include "cache.h"
25 #include "clip.h"
26 #include "datatype.h"
27 #include "edit.h"
28 #include "edits.h"
29 #include "edl.h"
30 #include "edlsession.h"
31 #include "filexml.h"
32 #include "floatauto.h"
33 #include "floatautos.h"
34 #include "format.inc"
35 #include "language.h"
36 #include "localsession.h"
37 #include "patch.h"
38 #include "mainsession.h"
39 #include "theme.h"
40 #include "trackcanvas.h"
41 #include "tracks.h"
42 #include "transportque.inc"
43 #include "units.h"
44 #include "vautomation.h"
45 #include "vedit.h"
46 #include "vedits.h"
47 #include "vframe.h"
48 #include "vmodule.h"
49 #include "vpluginset.h"
50 #include "vtrack.h"
51
52 VTrack::VTrack(EDL *edl, Tracks *tracks)
53  : Track(edl, tracks)
54 {
55         data_type = TRACK_VIDEO;
56         draw = 1;
57 }
58
59 VTrack::~VTrack()
60 {
61 }
62
63 void VTrack::create_objects()
64 {
65         Track::create_objects();
66         automation = new VAutomation(edl, this);
67         automation->create_objects();
68         edits = new VEdits(edl, this);
69 }
70
71 // Used by PlaybackEngine
72 void VTrack::synchronize_params(Track *track)
73 {
74         Track::synchronize_params(track);
75         //VTrack *vtrack = (VTrack*)track;
76 }
77
78 // Used by EDL::operator=
79 int VTrack::copy_settings(Track *track)
80 {
81         Track::copy_settings(track);
82         //VTrack *vtrack = (VTrack*)track;
83         return 0;
84 }
85
86 int VTrack::vertical_span(Theme *theme)
87 {
88         int track_h = Track::vertical_span(theme);
89         int patch_h = 0;
90         if(expand_view)
91         {
92                 patch_h += theme->title_h + theme->play_h + theme->fade_h + theme->mode_h;
93         }
94         return MAX(track_h, patch_h);
95 }
96
97
98 PluginSet* VTrack::new_plugins()
99 {
100         return new VPluginSet(edl, this);
101 }
102
103 int VTrack::load_defaults(BC_Hash *defaults)
104 {
105         Track::load_defaults(defaults);
106         return 0;
107 }
108
109 void VTrack::set_default_title()
110 {
111         Track *current = ListItem<Track>::owner->first;
112         int i;
113         for(i = 0; current; current = NEXT)
114         {
115                 if(current->data_type == TRACK_VIDEO) i++;
116         }
117         sprintf(title, _("Video %d"), i);
118 }
119
120 int64_t VTrack::to_units(double position, int round)
121 {
122         if(round)
123         {
124                 return Units::round(position * edl->session->frame_rate);
125         }
126         else
127         {
128 // Kludge for rounding errors, just on a smaller scale than formal rounding
129                 position *= edl->session->frame_rate;
130                 return Units::to_int64(position);
131         }
132 }
133
134 double VTrack::to_doubleunits(double position)
135 {
136         return position * edl->session->frame_rate;
137 }
138
139
140 double VTrack::from_units(int64_t position)
141 {
142         return (double)position / edl->session->frame_rate;
143 }
144
145
146
147
148 int VTrack::identical(int64_t sample1, int64_t sample2)
149 {
150 // Units of frames
151         if(labs(sample1 - sample2) <= 1) return 1; else return 0;
152 }
153
154 int VTrack::save_header(FileXML *file)
155 {
156         file->tag.set_property("TYPE", "VIDEO");
157         return 0;
158 }
159
160 int VTrack::save_derived(FileXML *file)
161 {
162         return 0;
163 }
164
165 int VTrack::load_header(FileXML *file, uint32_t load_flags)
166 {
167         return 0;
168 }
169
170 int VTrack::load_derived(FileXML *file, uint32_t load_flags)
171 {
172         return 0;
173 }
174
175
176 int VTrack::direct_copy_possible(int64_t start, int direction, int use_nudge)
177 {
178         if(use_nudge) start += nudge;
179
180 // Track size must equal output size
181         if(track_w != edl->session->output_w || track_h != edl->session->output_h)
182                 return 0;
183                 
184         if(has_speed()) return 0;
185
186 // No automation must be present in the track
187         if(!automation->direct_copy_possible(start, direction))
188                 return 0;
189
190 // No plugin must be present
191         if(plugin_used(start, direction)) 
192                 return 0;
193
194 // No transition
195         if(get_current_transition(start, direction, 0, 0))
196                 return 0;
197
198         return 1;
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 int VTrack::create_derived_objs(int flash)
218 {
219         edits = new VEdits(edl, this);
220         return 0;
221 }
222
223
224 // int VTrack::get_dimensions(double &view_start, 
225 //      double &view_units, 
226 //      double &zoom_units)
227 // {
228 //      view_start = edl->local_session->view_start * edl->session->frame_rate;
229 //      view_units = 0;
230 //      zoom_units = edl->local_session->zoom_sample / edl->session->sample_rate * edl->session->frame_rate;
231 //      return 0;
232 // }
233
234 int VTrack::copy_derived(int64_t start, int64_t end, FileXML *xml)
235 {
236 // automation is copied in the Track::copy
237         return 0;
238 }
239
240 int VTrack::copy_automation_derived(AutoConf *auto_conf, int64_t start, int64_t end, FileXML *file)
241 {
242         return 0;
243 }
244
245 int VTrack::paste_derived(int64_t start, int64_t end, int64_t total_length, FileXML *xml, int &current_channel)
246 {
247         return 0;
248 }
249
250 int VTrack::paste_output(int64_t startproject, int64_t endproject, int64_t startsource, int64_t endsource, int layer, Asset *asset)
251 {
252         return 0;
253 }
254
255 int VTrack::clear_derived(int64_t start, int64_t end)
256 {
257         return 0;
258 }
259
260 int VTrack::paste_automation_derived(int64_t start, int64_t end, int64_t total_length, FileXML *xml, int shift_autos, int &current_pan)
261 {
262         return 0;
263 }
264
265 int VTrack::clear_automation_derived(AutoConf *auto_conf, int64_t start, int64_t end, int shift_autos)
266 {
267         return 0;
268 }
269
270 int VTrack::draw_autos_derived(float view_start, float zoom_units, AutoConf *auto_conf)
271 {
272         return 0;
273 }
274
275
276 int VTrack::select_auto_derived(float zoom_units, float view_start, AutoConf *auto_conf, int cursor_x, int cursor_y)
277 {
278         return 0;
279 }
280
281
282 int VTrack::move_auto_derived(float zoom_units, float view_start, AutoConf *auto_conf, int cursor_x, int cursor_y, int shift_down)
283 {
284         return 0;
285 }
286
287 int VTrack::draw_floating_autos_derived(float view_start, float zoom_units, AutoConf *auto_conf, int flash)
288 {
289         return 0;
290 }
291
292 int VTrack::is_playable(int64_t position, int direction)
293 {
294         int result = 0;
295         float in_x, in_y, in_w, in_h;
296         float out_x, out_y, out_w, out_h;
297
298         calculate_output_transfer(position, 
299                 direction, 
300                 in_x, in_y, in_w, in_h,
301                 out_x, out_y, out_w, out_h);
302
303 //printf("VTrack::is_playable %0.0f %0.0f %0.0f %0.0f %0.0f %0.0f %0.0f %0.0f\n", 
304 //in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h);
305         if(out_w > 0 && out_h > 0) 
306                 result = 1;
307         return result;
308 }
309
310 void VTrack::calculate_input_transfer(int asset_w, int asset_h,
311         int64_t position, int direction, 
312         float &in_x, float &in_y, float &in_w, float &in_h,
313         float &out_x, float &out_y, float &out_w, float &out_h)
314 {
315         float auto_x, auto_y, auto_z;
316         float camera_z = 1.;
317         float camera_x = asset_w / 2.;
318         float camera_y = asset_h / 2.;
319 // camera and output coords
320         float x[6], y[6];        
321
322 //printf("VTrack::calculate_input_transfer " _LD "\n", position);
323
324 // get camera center in asset
325         automation->get_camera(&auto_x, &auto_y, &auto_z, 
326                 position, direction);
327
328         camera_z *= auto_z;
329         camera_x += auto_x;
330         camera_y += auto_y;
331
332 // get camera coords on asset
333         x[0] = camera_x - (float)track_w / 2 / camera_z;
334         y[0] = camera_y - (float)track_h / 2 / camera_z;
335         x[1] = x[0] + (float)track_w / camera_z;
336         y[1] = y[0] + (float)track_h / camera_z;
337
338 // get asset coords on camera
339         x[2] = 0;
340         y[2] = 0;
341         x[3] = track_w;
342         y[3] = track_h;
343
344 // crop asset coords on camera
345         if(x[0] < 0)
346         {
347                 x[2] -= x[0] * camera_z;
348                 x[0] = 0;
349         }
350         if(y[0] < 0)
351         {
352                 y[2] -= y[0] * camera_z;
353                 y[0] = 0;
354         }
355         if(x[1] > asset_w)
356         {
357                 x[3] -= (x[1] - asset_w) * camera_z;
358                 x[1] = asset_w;
359         }
360         if(y[1] > asset_h)
361         {
362                 y[3] -= (y[1] - asset_h) * camera_z;
363                 y[1] = asset_h;
364         }
365
366 // get output bounding box
367         out_x = x[2];
368         out_y = y[2];
369         out_w = x[3] - x[2];
370         out_h = y[3] - y[2];
371
372         in_x = x[0];
373         in_y = y[0];
374         in_w = x[1] - x[0];
375         in_h = y[1] - y[0];
376 // printf("VTrack::calculate_input_transfer %f %f %f %f -> %f %f %f %f\n", 
377 // in_x, in_y, in_w, in_h,
378 // out_x, out_y, out_w, out_h);
379 }
380
381 void VTrack::calculate_output_transfer(int64_t position, int direction, 
382         float &in_x, float &in_y, float &in_w, float &in_h,
383         float &out_x, float &out_y, float &out_w, float &out_h)
384 {
385         float center_x, center_y, center_z;
386         float x[4], y[4];
387
388         x[0] = 0;
389         y[0] = 0;
390         x[1] = track_w;
391         y[1] = track_h;
392
393         automation->get_projector(&center_x, 
394                 &center_y, 
395                 &center_z, 
396                 position, 
397                 direction);
398
399         center_x += edl->session->output_w / 2;
400         center_y += edl->session->output_h / 2;
401
402         x[2] = center_x - (track_w / 2) * center_z;
403         y[2] = center_y - (track_h / 2) * center_z;
404         x[3] = x[2] + track_w * center_z;
405         y[3] = y[2] + track_h * center_z;
406
407 // Clip to boundaries of output
408         if(x[2] < 0)
409         {
410                 x[0] -= (x[2] - 0) / center_z;
411                 x[2] = 0;
412         }
413         if(y[2] < 0)
414         {
415                 y[0] -= (y[2] - 0) / center_z;
416                 y[2] = 0;
417         }
418         if(x[3] > edl->session->output_w)
419         {
420                 x[1] -= (x[3] - edl->session->output_w) / center_z;
421                 x[3] = edl->session->output_w;
422         }
423         if(y[3] > edl->session->output_h)
424         {
425                 y[1] -= (y[3] - edl->session->output_h) / center_z;
426                 y[3] = edl->session->output_h;
427         }
428
429         in_x = x[0];
430         in_y = y[0];
431         in_w = x[1] - x[0];
432         in_h = y[1] - y[0];
433         out_x = x[2];
434         out_y = y[2];
435         out_w = x[3] - x[2];
436         out_h = y[3] - y[2];
437 // printf("VTrack::calculate_output_transfer %f %f %f %f -> %f %f %f %f\n", 
438 // in_x, in_y, in_w, in_h,
439 // out_x, out_y, out_w, out_h);
440 }
441
442
443
444 int VTrack::get_projection(float &in_x1, float &in_y1, float &in_x2, float &in_y2, 
445         float &out_x1, float &out_y1, float &out_x2, float &out_y2, 
446         int frame_w, int frame_h, int64_t real_position, int direction)
447 {
448         float center_x, center_y, center_z;
449         float x[4], y[4];
450
451         automation->get_projector(&center_x, 
452                 &center_y, 
453                 &center_z, 
454                 real_position, 
455                 direction);
456
457         x[0] = y[0] = 0;
458         x[1] = frame_w;
459         y[1] = frame_h;
460
461         center_x += edl->session->output_w / 2;
462         center_y += edl->session->output_h / 2;
463
464         x[2] = center_x - (frame_w / 2) * center_z;
465         y[2] = center_y - (frame_h / 2) * center_z;
466         x[3] = x[2] + frame_w * center_z;
467         y[3] = y[2] + frame_h * center_z;
468
469         if(x[2] < 0)
470         {
471                 x[0] -= x[2] / center_z;
472                 x[2] = 0;
473         }
474         if(y[2] < 0)
475         {
476                 y[0] -= y[2] / center_z;
477                 y[2] = 0;
478         }
479         if(x[3] > edl->session->output_w)
480         {
481                 x[1] -= (x[3] - edl->session->output_w) / center_z;
482                 x[3] = edl->session->output_w;
483         }
484         if(y[3] > edl->session->output_h)
485         {
486                 y[1] -= (y[3] - edl->session->output_h) / center_z;
487                 y[3] = edl->session->output_h;
488         }
489
490         in_x1 = x[0];  in_y1 = y[0];  in_x2 = x[1];  in_y2 = y[1];
491         out_x1 = x[2]; out_y1 = y[2]; out_x2 = x[3]; out_y2 = y[3];
492         return 0;
493 }
494
495
496 void VTrack::translate(float offset_x, float offset_y, int do_camera)
497 {
498         int subscript;
499         if(do_camera) 
500                 subscript = AUTOMATION_CAMERA_X;
501         else
502                 subscript = AUTOMATION_PROJECTOR_X;
503         
504 // Translate default keyframe
505         ((FloatAuto*)automation->autos[subscript]->default_auto)->value += offset_x;
506         ((FloatAuto*)automation->autos[subscript + 1]->default_auto)->value += offset_y;
507
508 // Translate everyone else
509         for(Auto *current = automation->autos[subscript]->first; 
510                 current; 
511                 current = NEXT)
512         {
513                 ((FloatAuto*)current)->value += offset_x;
514         }
515
516         for(Auto *current = automation->autos[subscript + 1]->first; 
517                 current; 
518                 current = NEXT)
519         {
520                 ((FloatAuto*)current)->value += offset_y;
521         }
522 }
523