4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "bcsignals.h"
26 #include "framecache.h"
27 #include "indexable.h"
39 FrameCacheItem::FrameCacheItem()
44 frame_rate = (double)30000.0 / 1001;
47 FrameCacheItem::~FrameCacheItem()
52 int FrameCacheItem::get_size()
54 if(data) return data->get_data_size() + (path ? strlen(path)+1 : 0);
72 FrameCache::FrameCache()
77 FrameCache::~FrameCache()
82 // Returns 1 if frame exists in cache and copies it to the frame argument.
83 int FrameCache::get_frame(VFrame *frame,
89 lock->lock("FrameCache::get_frame");
90 FrameCacheItem *result = 0;
92 if(frame_exists(frame,
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(),
114 // result->data->get_w(),
115 // result->data->get_h(),
120 // result->data->get_color_model(),
121 // frame->get_color_model(),
123 // result->data->get_w(),
127 frame->copy_from(result->data);
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);
135 result->age = get_age();
147 VFrame* FrameCache::get_frame_ptr(int64_t position,
155 lock->lock("FrameCache::get_frame_ptr");
156 FrameCacheItem *result = 0;
157 if(frame_exists(position,
166 result->age = get_age();
175 // Puts frame in cache if enough space exists and the frame doesn't already
177 void FrameCache::put_frame(VFrame *frame, int64_t position,
178 int layer, double frame_rate, int use_copy, Indexable *indexable)
180 lock->lock("FrameCache::put_frame");
181 FrameCacheItem *item = 0;
183 if(indexable) source_id = indexable->id;
185 //printf("FrameCache::put_frame %d position=%jd\n", __LINE__, position);
187 if(frame_exists(frame, position, layer, frame_rate, &item, source_id)) {
188 item->age = get_age();
194 item = new FrameCacheItem;
196 item->data = use_copy ? new VFrame(*frame) : frame;
199 item->position = position;
201 item->frame_rate = frame_rate;
202 item->source_id = source_id;
204 item->path = cstrdup(indexable->path);
206 item->age = position < 0 ? INT_MAX : get_age();
208 //printf("FrameCache::put_frame %d position=%jd\n", __LINE__, position);
216 int FrameCache::frame_exists(VFrame *format, int64_t position,
217 int layer, double frame_rate, FrameCacheItem **item_return, int source_id)
219 FrameCacheItem *item = (FrameCacheItem*)get_item(position);
220 // printf("FrameCache::frame_exists %d item=%p item->position=%jd position=%jd\n",
223 // item ? item->position : 0,
226 while(item && item->position == position)
228 // printf("FrameCache::frame_exists %d %f,%f %d,%d %d,%d format match=%d item->data=%p\n",
236 // format->equivalent(item->data, 1),
238 // format->dump_params();
240 // This originally tested the frame stacks because a change in the
241 // interpolate plugin could cause CR2 to interpolate or not interpolate.
242 // This was disabled.
243 if(EQUIV(item->frame_rate, frame_rate) &&
244 layer == item->layer &&
245 format->equivalent(item->data, 0) &&
246 (source_id == -1 || item->source_id == -1 || source_id == item->source_id))
252 item = (FrameCacheItem*)item->next;
257 int FrameCache::frame_exists(int64_t position,
263 FrameCacheItem **item_return,
266 FrameCacheItem *item = (FrameCacheItem*)get_item(position);
267 while(item && item->position == position)
269 // printf("FrameCache::frame_exists %d %f,%f %d,%d %d,%d %d,%d\n",
275 // item->data->get_color_model(),
277 // item->data->get_w(),
279 // item->data->get_h(),
282 if(EQUIV(item->frame_rate, frame_rate) &&
283 layer == item->layer &&
284 color_model == item->data->get_color_model() &&
285 w == item->data->get_w() &&
286 h == item->data->get_h() &&
287 (source_id == -1 || item->source_id == -1 || source_id == item->source_id))
293 item = (FrameCacheItem*)item->next;
299 void FrameCache::dump()
301 // lock->lock("FrameCache::dump");
302 printf("FrameCache::dump 1 %d\n", total());
303 FrameCacheItem *item = (FrameCacheItem *)first;
305 printf(" position=%jd frame_rate=%f age=%d size=%jd\n",
306 item->position, item->frame_rate, item->age,
307 item->data->get_data_size());
308 item = (FrameCacheItem*)item->next;