1 #include "funcprotos.h"
6 quicktime_cache_t* quicktime_new_cache()
8 quicktime_cache_t *result = calloc(1, sizeof(quicktime_cache_t));
12 void quicktime_delete_cache(quicktime_cache_t *ptr)
17 //printf("quicktime_delete_cache 1\n");
18 for(i = 0; i < ptr->allocation; i++)
20 quicktime_cacheframe_t *frame = &ptr->frames[i];
21 if(frame->y) free(frame->y);
22 if(frame->u) free(frame->u);
23 if(frame->v) free(frame->v);
30 void quicktime_reset_cache(quicktime_cache_t *ptr)
35 void quicktime_put_frame(quicktime_cache_t *ptr,
44 quicktime_cacheframe_t *frame = 0;
47 //printf("quicktime_put_frame %d total=%d allocation=%d\n", __LINE__, ptr->total, ptr->allocation);
49 for(i = 0; i < ptr->total; i++)
51 if(ptr->frames[i].frame_number == frame_number)
53 frame = &ptr->frames[i];
61 if(ptr->total >= ptr->allocation)
63 int new_allocation = ptr->allocation * 2;
65 // printf("quicktime_put_frame %d ptr->allocation=%d new_allocation=%d\n",
70 if(!new_allocation) new_allocation = 32;
71 ptr->frames = realloc(ptr->frames,
72 sizeof(quicktime_cacheframe_t) * new_allocation);
73 bzero(ptr->frames + ptr->total,
74 sizeof(quicktime_cacheframe_t) * (new_allocation - ptr->allocation));
75 ptr->allocation = new_allocation;
78 frame = &ptr->frames[ptr->total];
79 //printf("quicktime_put_frame 30 %d %p %p %p\n", ptr->total, frame->y, frame->u, frame->v);
82 // Memcpy is a lot slower than just dropping the seeking frames.
85 frame->y = realloc(frame->y, y_size);
86 frame->y_size = y_size;
87 memcpy(frame->y, y, y_size);
92 frame->u = realloc(frame->u, u_size);
93 frame->u_size = u_size;
94 memcpy(frame->u, u, u_size);
99 frame->v = realloc(frame->v, v_size);
100 frame->v_size = v_size;
101 memcpy(frame->v, v, v_size);
103 frame->frame_number = frame_number;
107 //printf("quicktime_put_frame %d total=%d allocation=%d\n", __LINE__, ptr->total, ptr->allocation);
108 // Delete oldest frames
111 while(quicktime_cache_usage(ptr) > ptr->max && ptr->total > 0)
113 quicktime_cacheframe_t *frame = &ptr->frames[0];
115 if(frame->y) free(frame->y);
116 if(frame->u) free(frame->u);
117 if(frame->v) free(frame->v);
119 for(i = 0; i < ptr->total - 1; i++)
121 quicktime_cacheframe_t *frame1 = &ptr->frames[i];
122 quicktime_cacheframe_t *frame2 = &ptr->frames[i + 1];
127 frame = &ptr->frames[ptr->total - 1];
137 // printf("quicktime_put_frame %d total=%d allocation=%d\n", __LINE__, ptr->total, ptr->allocation);
138 // printf("quicktime_put_frame %d max=0x%x current=0x%x\n",
141 // quicktime_cache_usage(ptr));
145 int quicktime_get_frame(quicktime_cache_t *ptr,
146 int64_t frame_number,
153 for(i = 0; i < ptr->total; i++)
155 quicktime_cacheframe_t *frame = &ptr->frames[i];
156 if(frame->frame_number == frame_number)
170 int quicktime_has_frame(quicktime_cache_t *ptr,
171 int64_t frame_number)
175 for(i = 0; i < ptr->total; i++)
177 quicktime_cacheframe_t *frame = &ptr->frames[i];
178 if(frame->frame_number == frame_number)
188 int64_t quicktime_cache_usage(quicktime_cache_t *ptr)
192 //printf("quicktime_cache_usage %p %d %lld\n", ptr, ptr->total, result);
193 for(i = 0; i < ptr->total; i++)
195 quicktime_cacheframe_t *frame = &ptr->frames[i];
196 result += frame->y_size + frame->u_size + frame->v_size;
201 void quicktime_cache_max(quicktime_cache_t *ptr, int bytes)