applied AkhIL's cinelerra-cv surround patch
[cinelerra_cv/ct.git] / quicktime / qtcache.c
blob0648d28ab461ae8e7e6cb9b4a71e55318b1951ee
1 #include "funcprotos.h"
2 #include "qtprivate.h"
3 #include <string.h>
6 quicktime_cache_t* quicktime_new_cache()
8 quicktime_cache_t *result = calloc(1, sizeof(quicktime_cache_t));
9 return result;
12 void quicktime_delete_cache(quicktime_cache_t *ptr)
14 if(ptr->frames)
16 int i;
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);
25 free(ptr->frames);
26 free(ptr);
30 void quicktime_reset_cache(quicktime_cache_t *ptr)
32 ptr->total = 0;
35 void quicktime_put_frame(quicktime_cache_t *ptr,
36 int64_t frame_number,
37 unsigned char *y,
38 unsigned char *u,
39 unsigned char *v,
40 int y_size,
41 int u_size,
42 int v_size)
44 quicktime_cacheframe_t *frame = 0;
45 int i;
47 //printf("quicktime_put_frame 1\n");
48 // Get existing frame
49 for(i = 0; i < ptr->total; i++)
51 if(ptr->frames[i].frame_number == frame_number)
53 frame = &ptr->frames[i];
54 break;
59 if(!frame)
61 if(ptr->total >= ptr->allocation)
63 int new_allocation = ptr->allocation * 2;
64 //printf("quicktime_put_frame 10 %d\n", new_allocation);
65 if(!new_allocation) new_allocation = 32;
66 ptr->frames = realloc(ptr->frames,
67 sizeof(quicktime_cacheframe_t) * new_allocation);
68 bzero(ptr->frames + ptr->total,
69 sizeof(quicktime_cacheframe_t) * (new_allocation - ptr->allocation));
70 ptr->allocation = new_allocation;
71 //printf("quicktime_put_frame 20 %p %d %d\n", ptr, ptr->allocation, ptr->total);
74 frame = &ptr->frames[ptr->total];
75 //printf("quicktime_put_frame 30 %d %p %p %p\n", ptr->total, frame->y, frame->u, frame->v);
76 ptr->total++;
78 // Memcpy is a lot slower than just dropping the seeking frames.
79 if(y)
81 frame->y = realloc(frame->y, y_size);
82 frame->y_size = y_size;
83 memcpy(frame->y, y, y_size);
86 if(u)
88 frame->u = realloc(frame->u, u_size);
89 frame->u_size = u_size;
90 memcpy(frame->u, u, u_size);
93 if(v)
95 frame->v = realloc(frame->v, v_size);
96 frame->v_size = v_size;
97 memcpy(frame->v, v, v_size);
99 frame->frame_number = frame_number;
101 //printf("quicktime_put_frame 100\n");
104 int quicktime_get_frame(quicktime_cache_t *ptr,
105 int64_t frame_number,
106 unsigned char **y,
107 unsigned char **u,
108 unsigned char **v)
110 int i;
112 for(i = 0; i < ptr->total; i++)
114 quicktime_cacheframe_t *frame = &ptr->frames[i];
115 if(frame->frame_number == frame_number)
118 *y = frame->y;
119 *u = frame->u;
120 *v = frame->v;
121 return 1;
122 break;
126 return 0;
129 int quicktime_has_frame(quicktime_cache_t *ptr,
130 int64_t frame_number)
132 int i;
134 for(i = 0; i < ptr->total; i++)
136 quicktime_cacheframe_t *frame = &ptr->frames[i];
137 if(frame->frame_number == frame_number)
139 return 1;
140 break;
144 return 0;
147 int64_t quicktime_cache_usage(quicktime_cache_t *ptr)
149 int64_t result = 0;
150 int i;
151 //printf("quicktime_cache_usage %p %d %lld\n", ptr, ptr->allocation, result);
152 for(i = 0; i < ptr->allocation; i++)
154 quicktime_cacheframe_t *frame = &ptr->frames[i];
155 result += frame->y_size + frame->u_size + frame->v_size;
157 return result;