rework keyframe hide popup, keyframe auto render, textbox set_selection wide text
[goodguy/history.git] / cinelerra-5.1 / cinelerra / vedit.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 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 "bcsignals.h"
24 #include "cache.h"
25 #include "edl.h"
26 #include "edlsession.h"
27 #include "file.h"
28 #include "mwindow.h"
29 #include "patch.h"
30 #include "playabletracks.h"
31 #include "preferences.h"
32 #include "mainsession.h"
33 #include "trackcanvas.h"
34 #include "tracks.h"
35 #include "transportque.h"
36 #include "units.h"
37 #include "vedit.h"
38 #include "vedits.h"
39 #include "vframe.h"
40 #include "vtrack.h"
41
42 VEdit::VEdit(EDL *edl, Edits *edits)
43  : Edit(edl, edits)
44 {
45 }
46
47
48 VEdit::~VEdit() { }
49
50 int VEdit::load_properties_derived(FileXML *xml)
51 {
52         channel = xml->tag.get_property("CHANNEL", (int64_t)0);
53         return 0;
54 }
55
56 Asset* VEdit::get_nested_asset(int64_t *source_position,
57         int64_t position,
58         int direction)
59 {
60         const int debug = 0;
61         Asset *result = 0;
62 // Make position relative to edit
63         *source_position = position - startproject + startsource;
64
65 if(debug) printf("VEdit::get_nested_asset %d %jd %jd %jd %jd\n",
66 __LINE__, *source_position, position, startproject, startsource);
67
68 // Descend into nested EDLs
69         if(nested_edl)
70         {
71 // Convert position to nested EDL rate
72 if(debug) printf("VEdit::get_nested_asset %d\n",
73 __LINE__);
74                 int64_t pos = *source_position;
75                 if(direction == PLAY_REVERSE && pos > 0) --pos;
76                 *source_position = Units::to_int64((double)pos *
77                         nested_edl->session->frame_rate /
78                         edl->session->frame_rate);
79                 PlayableTracks *playable_tracks = new PlayableTracks(
80                         nested_edl,
81                         *source_position,
82                         direction,
83                         TRACK_VIDEO,
84                         1);
85                 if(playable_tracks->size())
86                 {
87                         VTrack *nested_track = (VTrack*)playable_tracks->get(0);
88                         VEdit* nested_edit = (VEdit*)nested_track->edits->editof(
89                                 *source_position,
90                                 direction,
91                                 1);
92                         if(nested_edit)
93                         {
94                                 result = nested_edit->get_nested_asset(
95                                         source_position,
96                                         *source_position,
97                                         direction);
98                         }
99                 }
100
101                 delete playable_tracks;
102 if(debug) printf("VEdit::get_nested_asset %d\n",
103 __LINE__);
104                 return result;
105         }
106         else
107         {
108 // Convert position to asset rate
109 if(debug) printf("VEdit::get_nested_asset %d %jd %f %f\n",
110 __LINE__,
111 *source_position,
112 asset->frame_rate,
113 edl->session->frame_rate);
114                 int64_t pos = *source_position;
115                 if(direction == PLAY_REVERSE && pos > 0) --pos;
116                 *source_position = Units::to_int64((double)pos *
117                         asset->frame_rate /
118                         edl->session->frame_rate);
119
120                 return asset;
121         }
122 }
123
124 int VEdit::read_frame(VFrame *video_out,
125         int64_t input_position,
126         int direction,
127         CICache *cache,
128         int use_nudge,
129         int use_cache,
130         int use_asynchronous)
131 {
132         int64_t source_position = 0;
133         const int debug = 0;
134
135         if(use_nudge) input_position += track->nudge;
136 if(debug) printf("VEdit::read_frame %d source_position=%jd input_position=%jd\n",
137   __LINE__, source_position, input_position);
138
139         Asset *asset = get_nested_asset(&source_position,
140                 input_position,
141                 direction);
142
143 if(debug) printf("VEdit::read_frame %d source_position=%jd input_position=%jd\n",
144 __LINE__, source_position, input_position);
145
146         File *file = cache->check_out(asset,
147                 edl);
148         int result = 0;
149
150 if(debug) printf("VEdit::read_frame %d path=%s source_position=%jd\n",
151 __LINE__, asset->path, source_position);
152
153         if(file)
154         {
155
156 if(debug) printf("VEdit::read_frame %d\n", __LINE__);
157                 source_position = (direction == PLAY_FORWARD) ?
158                         source_position :
159                         (source_position - 1);
160 if(debug) printf("VEdit::read_frame %d %jd %jd\n",
161   __LINE__, input_position, source_position);
162
163                 if(use_asynchronous)
164                         file->start_video_decode_thread();
165                 else
166                         file->stop_video_thread();
167 if(debug) printf("VEdit::read_frame %d\n", __LINE__);
168
169                 file->set_layer(channel);
170 //printf("VEdit::read_frame %d %lld\n", __LINE__, source_position);
171                 file->set_video_position(source_position, 0);
172
173                 if(use_cache) file->set_cache_frames(use_cache);
174                 result = file->read_frame(video_out);
175 if(debug) printf("VEdit::read_frame %d\n", __LINE__);
176                 if(use_cache) file->set_cache_frames(0);
177
178 if(debug) printf("VEdit::read_frame %d\n", __LINE__);
179                 cache->check_in(asset);
180 if(debug) printf("VEdit::read_frame %d\n", __LINE__);
181         }
182         else
183                 result = 1;
184
185 //for(int i = 0; i < video_out->get_w() * 3 * 20; i++) video_out->get_rows()[0][i] = 128;
186         return result;
187 }
188
189 int VEdit::copy_properties_derived(FileXML *xml, int64_t length_in_selection)
190 {
191         return 0;
192 }
193
194 int VEdit::dump_derived()
195 {
196         printf("        VEdit::dump_derived\n");
197         printf("                startproject %jd\n", startproject);
198         printf("                length %jd\n", length);
199         return 0;
200 }
201
202 int64_t VEdit::get_source_end(int64_t default_)
203 {
204         if(!nested_edl && !asset) return default_;   // Infinity
205
206         if(nested_edl)
207         {
208                 return (int64_t)(nested_edl->tracks->total_playable_length() *
209                         edl->session->frame_rate + 0.5);
210         }
211
212         return (int64_t)((double)asset->video_length /
213                 asset->frame_rate *
214                 edl->session->frame_rate + 0.5);
215 }