prevent popup deactivation while button_down
[goodguy/history.git] / cinelerra-5.0 / cinelerra / framecache.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 "bcsignals.h"
23 #include "clip.h"
24 #include "cstrdup.h"
25 #include "edl.h"
26 #include "framecache.h"
27 #include "format.inc"
28 #include "indexable.h"
29 #include "mutex.h"
30 #include "vframe.h"
31
32
33 #include <math.h>
34 #include <string.h>
35 #include <unistd.h>
36
37
38
39 FrameCacheItem::FrameCacheItem()
40  : CacheItemBase()
41 {
42         data = 0;
43         position = 0;
44         frame_rate = (double)30000.0 / 1001;
45 }
46
47 FrameCacheItem::~FrameCacheItem()
48 {
49         delete data;
50 }
51
52 int FrameCacheItem::get_size()
53 {
54         if(data) return data->get_data_size() + (path ? strlen(path)+1 : 0);
55         return 0;
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 FrameCache::FrameCache()
73  : CacheBase()
74 {
75 }
76
77 FrameCache::~FrameCache()
78 {
79 }
80
81
82 // Returns 1 if frame exists in cache and copies it to the frame argument.
83 int FrameCache::get_frame(VFrame *frame, 
84         int64_t position,
85         int layer,
86         double frame_rate,
87         int source_id)
88 {
89         lock->lock("FrameCache::get_frame");
90         FrameCacheItem *result = 0;
91
92         if(frame_exists(frame,
93                 position, 
94                 layer,
95                 frame_rate,
96                 &result,
97                 source_id))
98         {
99                 if(result->data) 
100                 {
101 // Frame may have come from the readahead thread.
102 // Those frames are in the codec color model.
103 // But to pass frame_exists, they must be identical.
104 //                      BC_CModels::transfer(frame->get_rows(), 
105 //                              result->data->get_rows(),
106 //                              result->data->get_y(),
107 //                              result->data->get_u(),
108 //                              result->data->get_v(),
109 //                              frame->get_y(),
110 //                              frame->get_u(),
111 //                              frame->get_v(),
112 //                              0, 
113 //                              0, 
114 //                              result->data->get_w(), 
115 //                              result->data->get_h(),
116 //                              0, 
117 //                              0, 
118 //                              frame->get_w(), 
119 //                              frame->get_h(),
120 //                              result->data->get_color_model(), 
121 //                              frame->get_color_model(),
122 //                              0,
123 //                              result->data->get_w(),
124 //                              frame->get_w());
125
126
127                         frame->copy_from(result->data);
128
129
130 // This would have copied the color matrix for interpolate, but
131 // required the same plugin stack as the reader.
132 //                      frame->copy_stacks(result->data);
133                         frame->copy_params(result->data);
134                 }
135                 result->age = get_age();
136         }
137
138
139
140
141         lock->unlock();
142         if(result) return 1;
143         return 0;
144 }
145
146
147 VFrame* FrameCache::get_frame_ptr(int64_t position,
148         int layer,
149         double frame_rate,
150         int color_model,
151         int w,
152         int h,
153         int source_id)
154 {
155         lock->lock("FrameCache::get_frame_ptr");
156         FrameCacheItem *result = 0;
157         if(frame_exists(position,
158                 layer,
159                 frame_rate,
160                 color_model,
161                 w,
162                 h,
163                 &result,
164                 source_id))
165         {
166                 result->age = get_age();
167                 return result->data;
168         }
169
170
171         lock->unlock();
172         return 0;
173 }
174
175 // Puts frame in cache if enough space exists and the frame doesn't already
176 // exist.
177 void FrameCache::put_frame(VFrame *frame, 
178         int64_t position,
179         int layer,
180         double frame_rate,
181         int use_copy,
182         Indexable *indexable)
183 {
184         lock->lock("FrameCache::put_frame");
185         FrameCacheItem *item = 0;
186         int source_id = -1;
187         if(indexable) source_id = indexable->id;
188
189 //printf("FrameCache::put_frame %d position=" _LD "\n", __LINE__, position);
190
191         if(frame_exists(frame,
192                 position, 
193                 layer,
194                 frame_rate,
195                 &item,
196                 source_id))
197         {
198                 item->age = get_age();
199                 lock->unlock();
200                 return;
201         }
202
203
204         item = new FrameCacheItem;
205
206         if(use_copy)
207         {
208                 item->data = new VFrame(*frame);
209         }
210         else
211         {
212                 item->data = frame;
213         }
214
215 // Copy metadata
216         item->position = position;
217         item->layer = layer;
218         item->frame_rate = frame_rate;
219         item->source_id = source_id;
220         if(indexable) 
221                 item->path = cstrdup(indexable->path);
222
223         item->age = get_age();
224
225 //printf("FrameCache::put_frame %d position=" _LD "\n", __LINE__, position);
226         put_item(item);
227         lock->unlock();
228 }
229
230
231
232
233 int FrameCache::frame_exists(VFrame *format,
234         int64_t position, 
235         int layer,
236         double frame_rate,
237         FrameCacheItem **item_return,
238         int source_id)
239 {
240         FrameCacheItem *item = (FrameCacheItem*)get_item(position);
241 // printf("FrameCache::frame_exists %d item=%p item->position=" _LD " position=" _LD "\n",
242 // __LINE__,
243 // item,
244 // item ? item->position : 0,
245 // position);
246
247         while(item && item->position == position)
248         {
249 // printf("FrameCache::frame_exists %d %f,%f %d,%d %d,%d format match=%d item->data=%p\n",
250 // __LINE__,
251 // item->frame_rate,
252 // frame_rate,
253 // item->layer,
254 // layer,
255 // item->source_id,
256 // source_id,
257 // format->equivalent(item->data, 1),
258 // item->data);
259 // format->dump_params();
260
261 // This originally tested the frame stacks because a change in the 
262 // interpolate plugin could cause CR2 to interpolate or not interpolate.
263 // This was disabled.
264                 if(EQUIV(item->frame_rate, frame_rate) &&
265                         layer == item->layer &&
266                         format->equivalent(item->data, 0) &&
267                         (source_id == -1 || item->source_id == -1 || source_id == item->source_id))
268                 {
269                         *item_return = item;
270                         return 1;
271                 }
272                 else
273                         item = (FrameCacheItem*)item->next;
274         }
275         return 0;
276 }
277
278 int FrameCache::frame_exists(int64_t position, 
279         int layer,
280         double frame_rate,
281         int color_model,
282         int w,
283         int h,
284         FrameCacheItem **item_return,
285         int source_id)
286 {
287         FrameCacheItem *item = (FrameCacheItem*)get_item(position);
288         while(item && item->position == position)
289         {
290 // printf("FrameCache::frame_exists %d %f,%f %d,%d %d,%d %d,%d\n",
291 // __LINE__,
292 // item->frame_rate,
293 // frame_rate,
294 // item->layer,
295 // layer,
296 // item->data->get_color_model(),
297 // color_model,
298 // item->data->get_w(),
299 // w,
300 // item->data->get_h(),
301 // h);
302
303                 if(EQUIV(item->frame_rate, frame_rate) &&
304                         layer == item->layer &&
305                         color_model == item->data->get_color_model() &&
306                         w == item->data->get_w() &&
307                         h == item->data->get_h() &&
308                         (source_id == -1 || item->source_id == -1 || source_id == item->source_id))
309                 {
310                         *item_return = item;
311                         return 1;
312                 }
313                 else
314                         item = (FrameCacheItem*)item->next;
315         }
316         return 0;
317 }
318
319
320 void FrameCache::dump()
321 {
322 //      lock->lock("FrameCache::dump");
323         printf("FrameCache::dump 1 %d\n", total());
324         FrameCacheItem *item = (FrameCacheItem *)first;
325         while( item ) {
326                 printf("  position=" _LD " frame_rate=%f age=%d size=" _LD "\n", 
327                         item->position, item->frame_rate, item->age,
328                         item->data->get_data_size());
329                 item = (FrameCacheItem*)item->next;
330         }
331 //      lock->unlock();
332 }
333
334
335
336