2 #include "framecache.h"
9 FrameCacheItem::FrameCacheItem()
13 frame_rate = (double)30000.0 / 1001;
17 FrameCacheItem::~FrameCacheItem()
37 FrameCache::FrameCache()
39 lock = new Mutex("FrameCache::lock");
44 FrameCache::~FrameCache()
46 items.remove_all_objects();
51 // Returns 1 if frame exists in cache and copies it to the frame argument.
52 int FrameCache::get_frame(VFrame *frame,
56 lock->lock("FrameCache::get_frame");
59 if(frame_exists(frame,
64 FrameCacheItem *item = items.values[item_number];
65 if(item->data) frame->copy_from(item->data);
66 item->age = current_age;
71 if(item_number >= 0) return 1;
76 VFrame* FrameCache::get_frame_ptr(int64_t position,
82 lock->lock("FrameCache::get_frame");
84 FrameCacheItem *item = 0;
85 if(frame_exists(position,
92 item = items.values[item_number];
93 item->age = current_age;
107 void FrameCache::unlock()
112 // Puts frame in cache if enough space exists and the frame doesn't already
114 void FrameCache::put_frame(VFrame *frame,
119 lock->lock("FrameCache::put_frame");
120 int item_number = -1;
121 if(frame_exists(frame,
126 FrameCacheItem *item = items.values[item_number];
127 item->age = current_age;
134 FrameCacheItem *item = new FrameCacheItem;
138 item->data = new VFrame(*frame);
145 item->position = position;
146 item->frame_rate = frame_rate;
147 item->age = current_age;
157 int FrameCache::frame_exists(VFrame *format,
162 for(int i = 0; i < items.total; i++)
164 FrameCacheItem *item = items.values[i];
165 if(item->position == position &&
166 EQUIV(item->frame_rate, frame_rate) &&
167 format->equivalent(item->data))
176 int FrameCache::frame_exists(int64_t position,
183 for(int i = 0; i < items.total; i++)
185 FrameCacheItem *item = items.values[i];
186 if(item->position == position &&
187 EQUIV(item->frame_rate, frame_rate) &&
188 color_model == item->data->get_color_model() &&
189 w == item->data->get_w() &&
190 h == item->data->get_h())
199 // Calculate current size of cache in bytes
200 int64_t FrameCache::get_memory_usage()
203 lock->lock("FrameCache::get_memory_usage");
204 for(int i = 0; i < items.total; i++)
206 FrameCacheItem *item = items.values[i];
207 result += item->data->get_data_size();
213 int FrameCache::delete_oldest()
215 int64_t oldest = 0x7fffffff;
216 int oldest_item = -1;
218 lock->lock("FrameCache::delete_oldest");
219 for(int i = 0; i < items.total; i++)
221 if(items.values[i]->age < oldest)
223 oldest = items.values[i]->age;
230 items.remove_object_number(oldest_item);
238 void FrameCache::dump()
240 lock->lock("FrameCache::dump");
241 printf("FrameCache::dump 1 %d\n", items.total);
242 for(int i = 0; i < items.total; i++)
244 FrameCacheItem *item = items.values[i];
245 printf(" position=%lld frame_rate=%f age=%d size=%d\n",
249 item->data->get_data_size());