map media vicon popup
[goodguy/cinelerra.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, int direction)
58 {
59         double edit_frame_rate = nested_edl ?
60                 nested_edl->session->frame_rate : asset->frame_rate;
61 // Make position relative to edit
62         double edit_position = (position - startproject + startsource) *
63                         edit_frame_rate / edl->session->frame_rate;
64         *source_position = Units::to_int64(edit_position);
65         if( !nested_edl ) return asset;
66
67 // Descend into nested EDLs
68         PlayableTracks playable_tracks(nested_edl,
69                 *source_position, direction, TRACK_VIDEO, 1);
70         if( !playable_tracks.size() ) return 0;
71         VTrack *nested_track = (VTrack*)playable_tracks[0];
72         VEdit* nested_edit = (VEdit*)nested_track->edits->
73                 editof(*source_position, direction, 1);
74         if( !nested_edit ) return  0;
75         return nested_edit->get_nested_asset(source_position,
76                 *source_position, direction);
77 }
78
79 int VEdit::read_frame(VFrame *video_out, int64_t input_position, int direction,
80                 CICache *cache, int use_nudge, int use_cache, int use_asynchronous)
81 {
82         File *file = 0;
83         int result = 0;
84         int64_t source_position = 0;
85         const int debug = 0;
86
87         if(use_nudge) input_position += track->nudge;
88 if(debug) printf("VEdit::read_frame %d source_position=%jd input_position=%jd\n",
89   __LINE__, source_position, input_position);
90
91         Asset *asset = get_nested_asset(&source_position, input_position, direction);
92         if( !asset ) result = 1;
93
94 if(debug) printf("VEdit::read_frame %d source_position=%jd input_position=%jd\n",
95 __LINE__, source_position, input_position);
96
97         if( !result ) {
98                 file = cache->check_out(asset, edl);
99                 if( !file ) result = 1;
100         }
101 if(debug) printf("VEdit::read_frame %d path=%s source_position=%jd\n",
102 __LINE__, asset->path, source_position);
103
104         if( !result ) {
105                 if(direction == PLAY_REVERSE && source_position > 0)
106                         --source_position;
107                 if(use_asynchronous)
108                         file->start_video_decode_thread();
109                 else
110                         file->stop_video_thread();
111 if(debug) printf("VEdit::read_frame %d\n", __LINE__);
112
113                 file->set_layer(channel);
114 //printf("VEdit::read_frame %d %lld\n", __LINE__, source_position);
115                 file->set_video_position(source_position, 0);
116
117                 if(use_cache) file->set_cache_frames(use_cache);
118                 result = file->read_frame(video_out);
119 if(debug) printf("VEdit::read_frame %d\n", __LINE__);
120                 if(use_cache) file->set_cache_frames(0);
121
122 if(debug) printf("VEdit::read_frame %d\n", __LINE__);
123                 cache->check_in(asset);
124 if(debug) printf("VEdit::read_frame %d\n", __LINE__);
125         }
126
127 //for(int i = 0; i < video_out->get_w() * 3 * 20; i++) video_out->get_rows()[0][i] = 128;
128         return result;
129 }
130
131 int VEdit::copy_properties_derived(FileXML *xml, int64_t length_in_selection)
132 {
133         return 0;
134 }
135
136 int VEdit::dump_derived()
137 {
138         printf("        VEdit::dump_derived\n");
139         printf("                startproject %jd\n", startproject);
140         printf("                length %jd\n", length);
141         return 0;
142 }
143
144 int64_t VEdit::get_source_end(int64_t default_)
145 {
146         if(!nested_edl && !asset) return default_;   // Infinity
147
148         if(nested_edl)
149         {
150                 return (int64_t)(nested_edl->tracks->total_length() *
151                         edl->session->frame_rate + 0.5);
152         }
153
154         return (int64_t)((double)asset->video_length /
155                 asset->frame_rate *
156                 edl->session->frame_rate + 0.5);
157 }