Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / libmpeg3 / video / mpeg3cache.c
blobaac470fce27a91f4671603e78536d1f1cc7ada7b
1 #include "mpeg3private.h"
2 #include <stdlib.h>
3 #include <string.h>
8 // This is basically qtcache.c with quicktime_ replaced by mpeg3_
11 mpeg3_cache_t* mpeg3_new_cache()
13 mpeg3_cache_t *result = calloc(1, sizeof(mpeg3_cache_t));
14 return result;
17 void mpeg3_delete_cache(mpeg3_cache_t *ptr)
19 if(ptr->frames)
21 int i;
22 for(i = 0; i < ptr->allocation; i++)
24 mpeg3_cacheframe_t *frame = &ptr->frames[i];
25 if(frame->y) free(frame->y);
26 if(frame->u) free(frame->u);
27 if(frame->v) free(frame->v);
29 free(ptr->frames);
30 free(ptr);
34 void mpeg3_reset_cache(mpeg3_cache_t *ptr)
36 ptr->total = 0;
39 void mpeg3_cache_put_frame(mpeg3_cache_t *ptr,
40 int64_t frame_number,
41 unsigned char *y,
42 unsigned char *u,
43 unsigned char *v,
44 int y_size,
45 int u_size,
46 int v_size)
48 mpeg3_cacheframe_t *frame = 0;
49 int i;
51 //printf("mpeg3_put_frame 1\n");
52 // Get existing frame
53 for(i = 0; i < ptr->total; i++)
55 if(ptr->frames[i].frame_number == frame_number)
57 frame = &ptr->frames[i];
58 break;
63 if(!frame)
65 if(ptr->total >= ptr->allocation)
67 int new_allocation = ptr->allocation * 2;
68 //printf("mpeg3_put_frame 10 %d\n", new_allocation);
69 if(!new_allocation) new_allocation = 32;
70 ptr->frames = realloc(ptr->frames,
71 sizeof(mpeg3_cacheframe_t) * new_allocation);
72 bzero(ptr->frames + ptr->total,
73 sizeof(mpeg3_cacheframe_t) * (new_allocation - ptr->allocation));
74 //printf("mpeg3_put_frame 20 %d %d %d\n", new_allocation, ptr->allocation, ptr->total);
75 ptr->allocation = new_allocation;
78 frame = &ptr->frames[ptr->total];
79 //printf("mpeg3_put_frame 30 %d %p %p %p\n", ptr->total, frame->y, frame->u, frame->v);
80 ptr->total++;
82 // Memcpy is a lot slower than just dropping the seeking frames.
83 if(y)
85 frame->y = realloc(frame->y, y_size);
86 frame->y_size = y_size;
87 memcpy(frame->y, y, y_size);
90 if(u)
92 frame->u = realloc(frame->u, u_size);
93 frame->u_size = u_size;
94 memcpy(frame->u, u, u_size);
97 if(v)
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;
105 //printf("mpeg3_put_frame 100\n");
108 int mpeg3_cache_get_frame(mpeg3_cache_t *ptr,
109 int64_t frame_number,
110 unsigned char **y,
111 unsigned char **u,
112 unsigned char **v)
114 int i;
116 for(i = 0; i < ptr->total; i++)
118 mpeg3_cacheframe_t *frame = &ptr->frames[i];
119 if(frame->frame_number == frame_number)
122 *y = frame->y;
123 *u = frame->u;
124 *v = frame->v;
125 return 1;
126 break;
130 return 0;
134 int mpeg3_cache_has_frame(mpeg3_cache_t *ptr,
135 int64_t frame_number)
137 int i;
139 for(i = 0; i < ptr->total; i++)
141 mpeg3_cacheframe_t *frame = &ptr->frames[i];
142 if(frame->frame_number == frame_number)
143 return 1;
146 return 0;
149 int64_t mpeg3_cache_usage(mpeg3_cache_t *ptr)
151 int64_t result = 0;
152 int i;
153 for(i = 0; i < ptr->allocation; i++)
155 mpeg3_cacheframe_t *frame = &ptr->frames[i];
156 result += frame->y_size + frame->u_size + frame->v_size;
158 return result;