be6be51cb119c029a0d079f9c0fff11baaf288df
[goodguy/cinelerra.git] / cinelerra-5.1 / 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 "indexable.h"
28 #include "mutex.h"
29 #include "vframe.h"
30
31
32 #include <limits.h>
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 // no context data since keyframe updates may vary input
127                         frame->copy_from(result->data);
128                 }
129                 result->age = get_age();
130         }
131
132
133
134
135         lock->unlock();
136         if(result) return 1;
137         return 0;
138 }
139
140
141 VFrame* FrameCache::get_frame_ptr(int64_t position,
142         int layer,
143         double frame_rate,
144         int color_model,
145         int w,
146         int h,
147         int source_id)
148 {
149         lock->lock("FrameCache::get_frame_ptr");
150         FrameCacheItem *result = 0;
151         if(frame_exists(position,
152                 layer,
153                 frame_rate,
154                 color_model,
155                 w,
156                 h,
157                 &result,
158                 source_id))
159         {
160                 result->age = get_age();
161                 return result->data;
162         }
163
164
165         lock->unlock();
166         return 0;
167 }
168
169 // Puts frame in cache if enough space exists and the frame doesn't already
170 // exist.
171 void FrameCache::put_frame(VFrame *frame, int64_t position,
172         int layer, double frame_rate, int use_copy, Indexable *indexable)
173 {
174         lock->lock("FrameCache::put_frame");
175         FrameCacheItem *item = 0;
176         int source_id = -1;
177         if(indexable) source_id = indexable->id;
178
179 //printf("FrameCache::put_frame %d position=%jd\n", __LINE__, position);
180
181         if(frame_exists(frame, position, layer, frame_rate, &item, source_id)) {
182                 item->age = get_age();
183                 lock->unlock();
184                 return;
185         }
186
187
188         item = new FrameCacheItem;
189
190         item->data = use_copy ? new VFrame(*frame) : frame;
191
192 // Copy metadata
193         item->position = position;
194         item->layer = layer;
195         item->frame_rate = frame_rate;
196         item->source_id = source_id;
197         if(indexable)
198                 item->path = cstrdup(indexable->path);
199
200         item->age = position < 0 ? INT_MAX : get_age();
201
202 //printf("FrameCache::put_frame %d position=%jd\n", __LINE__, position);
203         put_item(item);
204         lock->unlock();
205 }
206
207 VFrame *FrameCache::new_cache_frame(int64_t position, int w, int h,
208                 int color_model, int layer, double frame_rate, int first_frame)
209 {
210         FrameCacheItem *item = 0;
211         lock->lock("FrameCache::put_vframe");
212         if( frame_exists(position, layer, frame_rate, color_model, w, h, &item, -1) ) {
213                 lock->unlock();
214                 return 0;
215         }
216         if( first_frame ) {
217                 while( last ) delete last;
218                 total_items = 0;
219                 current_item = 0;
220         }
221         item = new FrameCacheItem;
222         item->data = new VFrame(w, h, color_model);
223         item->position = position;
224         item->layer = layer;
225         item->frame_rate = frame_rate;
226         item->source_id = -1;
227         item->age = position < 0 ? INT_MAX : get_age();
228         put_item(item);
229         return item->data;
230 }
231 void FrameCache::put_cache_frame()
232 {
233         lock->unlock();
234 }
235
236 int FrameCache::frame_exists(VFrame *format, int64_t position,
237         int layer, double frame_rate, FrameCacheItem **item_return, int source_id)
238 {
239         FrameCacheItem *item = (FrameCacheItem*)get_item(position);
240         for( ; item && item->position == position; item = (FrameCacheItem*)item->next ) {
241                 if( !EQUIV(item->frame_rate, frame_rate) ) continue;
242                 if( layer != item->layer ) continue;
243                 if( !format->equivalent(item->data, 0) ) continue;
244                 if( source_id == -1 || item->source_id == -1 ||
245                     source_id == item->source_id ) {
246                         *item_return = item;
247                         return 1;
248                 }
249         }
250         return 0;
251 }
252
253 int FrameCache::frame_exists(int64_t position, int layer, double frame_rate,
254                 int color_model, int w, int h, FrameCacheItem **item_return, int source_id)
255 {
256         FrameCacheItem *item = (FrameCacheItem*)get_item(position);
257         for( ; item && item->position == position ; item = (FrameCacheItem*)item->next ) {
258                 if( !EQUIV(item->frame_rate, frame_rate) ) continue;
259                 if( layer != item->layer ) continue;
260                 if( color_model != item->data->get_color_model() ) continue;
261                 if( w != item->data->get_w() ) continue;
262                 if( h != item->data->get_h() ) continue;
263                 if( source_id == -1 || item->source_id == -1 ||
264                     source_id == item->source_id ) {
265                         *item_return = item;
266                         return 1;
267                 }
268         }
269         return 0;
270 }
271
272
273 void FrameCache::dump()
274 {
275 //      lock->lock("FrameCache::dump");
276         printf("FrameCache::dump 1 %d\n", total());
277         FrameCacheItem *item = (FrameCacheItem *)first;
278         while( item ) {
279                 printf("  position=%jd frame_rate=%f age=%d size=%ld\n",
280                         item->position, item->frame_rate, item->age,
281                         item->data->get_data_size());
282                 item = (FrameCacheItem*)item->next;
283         }
284 //      lock->unlock();
285 }
286
287
288
289