Mercurial > mplayer.hg
annotate libass/ass_cache.c @ 27651:f445847b8ec1
fix FAQ about compiling 32 bit mplayer on x86_64
Suggested by Wolfgang Knauf
author | gpoirier |
---|---|
date | Thu, 02 Oct 2008 22:15:45 +0000 |
parents | 8eff880f638c |
children | a0ce88ba2557 |
rev | line source |
---|---|
20008
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*- |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
2 // vim:ts=8:sw=8:noet:ai: |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
3 /* |
26723 | 4 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> |
5 * | |
26738
588ce97b44f2
Speak of libass instead of MPlayer in the libass license headers.
diego
parents:
26723
diff
changeset
|
6 * This file is part of libass. |
26723 | 7 * |
26738
588ce97b44f2
Speak of libass instead of MPlayer in the libass license headers.
diego
parents:
26723
diff
changeset
|
8 * libass is free software; you can redistribute it and/or modify |
26723 | 9 * it under the terms of the GNU General Public License as published by |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
26738
588ce97b44f2
Speak of libass instead of MPlayer in the libass license headers.
diego
parents:
26723
diff
changeset
|
13 * libass is distributed in the hope that it will be useful, |
26723 | 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License along | |
26738
588ce97b44f2
Speak of libass instead of MPlayer in the libass license headers.
diego
parents:
26723
diff
changeset
|
19 * with libass; if not, write to the Free Software Foundation, Inc., |
26723 | 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
21 */ | |
20008
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
22 |
18937 | 23 #include "config.h" |
24 | |
22292 | 25 #include <inttypes.h> |
18937 | 26 #include <ft2build.h> |
27 #include FT_FREETYPE_H | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19545
diff
changeset
|
28 #include FT_GLYPH_H |
18937 | 29 |
30 #include <assert.h> | |
31 | |
21026
d138463e820b
Collect all includes of mplayer headers in libass in a single file (mputils.h).
eugeni
parents:
20637
diff
changeset
|
32 #include "mputils.h" |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21348
diff
changeset
|
33 #include "ass.h" |
18937 | 34 #include "ass_fontconfig.h" |
21322 | 35 #include "ass_font.h" |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19545
diff
changeset
|
36 #include "ass_bitmap.h" |
18937 | 37 #include "ass_cache.h" |
38 | |
23016 | 39 |
40 typedef struct hashmap_item_s { | |
41 void* key; | |
42 void* value; | |
43 struct hashmap_item_s* next; | |
44 } hashmap_item_t; | |
45 typedef hashmap_item_t* hashmap_item_p; | |
46 | |
47 struct hashmap_s { | |
48 int nbuckets; | |
49 size_t key_size, value_size; | |
50 hashmap_item_p* root; | |
51 hashmap_item_dtor_t item_dtor; // a destructor for hashmap key/value pairs | |
52 hashmap_key_compare_t key_compare; | |
53 hashmap_hash_t hash; | |
23019
4934af4fdd0f
Collect hit/miss statistic in hash map, and print in -v mode.
eugeni
parents:
23018
diff
changeset
|
54 // stats |
4934af4fdd0f
Collect hit/miss statistic in hash map, and print in -v mode.
eugeni
parents:
23018
diff
changeset
|
55 int hit_count; |
4934af4fdd0f
Collect hit/miss statistic in hash map, and print in -v mode.
eugeni
parents:
23018
diff
changeset
|
56 int miss_count; |
4934af4fdd0f
Collect hit/miss statistic in hash map, and print in -v mode.
eugeni
parents:
23018
diff
changeset
|
57 int count; |
23016 | 58 }; |
59 | |
60 #define FNV1_32A_INIT (unsigned)0x811c9dc5 | |
61 | |
62 static inline unsigned fnv_32a_buf(void* buf, size_t len, unsigned hval) | |
63 { | |
64 unsigned char *bp = buf; | |
65 unsigned char *be = bp + len; | |
66 while (bp < be) { | |
67 hval ^= (unsigned)*bp++; | |
68 hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24); | |
69 } | |
70 return hval; | |
71 } | |
72 static inline unsigned fnv_32a_str(char* str, unsigned hval) | |
73 { | |
74 unsigned char* s = (unsigned char*)str; | |
75 while (*s) { | |
76 hval ^= (unsigned)*s++; | |
77 hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24); | |
78 } | |
79 return hval; | |
80 } | |
81 | |
82 static unsigned hashmap_hash(void* buf, size_t len) | |
83 { | |
84 return fnv_32a_buf(buf, len, FNV1_32A_INIT); | |
85 } | |
86 | |
87 static int hashmap_key_compare(void* a, void* b, size_t size) | |
88 { | |
26759
8eff880f638c
cosmetics: Remove useless parentheses from return statements.
diego
parents:
26738
diff
changeset
|
89 return memcmp(a, b, size) == 0; |
23016 | 90 } |
91 | |
92 static void hashmap_item_dtor(void* key, size_t key_size, void* value, size_t value_size) | |
93 { | |
94 free(key); | |
95 free(value); | |
96 } | |
18937 | 97 |
23016 | 98 hashmap_t* hashmap_init(size_t key_size, size_t value_size, int nbuckets, |
99 hashmap_item_dtor_t item_dtor, hashmap_key_compare_t key_compare, | |
100 hashmap_hash_t hash) | |
101 { | |
102 hashmap_t* map = calloc(1, sizeof(hashmap_t)); | |
103 map->nbuckets = nbuckets; | |
104 map->key_size = key_size; | |
105 map->value_size = value_size; | |
106 map->root = calloc(nbuckets, sizeof(hashmap_item_p)); | |
107 map->item_dtor = item_dtor ? item_dtor : hashmap_item_dtor; | |
108 map->key_compare = key_compare ? key_compare : hashmap_key_compare; | |
109 map->hash = hash ? hash : hashmap_hash; | |
110 return map; | |
111 } | |
112 | |
113 void hashmap_done(hashmap_t* map) | |
114 { | |
115 int i; | |
23019
4934af4fdd0f
Collect hit/miss statistic in hash map, and print in -v mode.
eugeni
parents:
23018
diff
changeset
|
116 // print stats |
4934af4fdd0f
Collect hit/miss statistic in hash map, and print in -v mode.
eugeni
parents:
23018
diff
changeset
|
117 if (map->count > 0 || map->hit_count + map->miss_count > 0) |
23020 | 118 mp_msg(MSGT_ASS, MSGL_V, "cache statistics: \n total accesses: %d\n hits: %d\n misses: %d\n object count: %d\n", |
23019
4934af4fdd0f
Collect hit/miss statistic in hash map, and print in -v mode.
eugeni
parents:
23018
diff
changeset
|
119 map->hit_count + map->miss_count, map->hit_count, map->miss_count, map->count); |
4934af4fdd0f
Collect hit/miss statistic in hash map, and print in -v mode.
eugeni
parents:
23018
diff
changeset
|
120 |
23016 | 121 for (i = 0; i < map->nbuckets; ++i) { |
122 hashmap_item_t* item = map->root[i]; | |
123 while (item) { | |
124 hashmap_item_t* next = item->next; | |
125 map->item_dtor(item->key, map->key_size, item->value, map->value_size); | |
126 free(item); | |
127 item = next; | |
128 } | |
129 } | |
130 free(map->root); | |
131 free(map); | |
132 } | |
18937 | 133 |
23016 | 134 // does nothing if key already exists |
23211
d9b4bfea1093
Make cache_*_add functions return the pointer to new (copied) value.
eugeni
parents:
23025
diff
changeset
|
135 void* hashmap_insert(hashmap_t* map, void* key, void* value) |
23016 | 136 { |
137 unsigned hash = map->hash(key, map->key_size); | |
138 hashmap_item_t** next = map->root + (hash % map->nbuckets); | |
139 while (*next) { | |
140 if (map->key_compare(key, (*next)->key, map->key_size)) | |
23214 | 141 return (*next)->value; |
23016 | 142 next = &((*next)->next); |
143 assert(next); | |
144 } | |
145 (*next) = malloc(sizeof(hashmap_item_t)); | |
146 (*next)->key = malloc(map->key_size); | |
147 (*next)->value = malloc(map->value_size); | |
148 memcpy((*next)->key, key, map->key_size); | |
149 memcpy((*next)->value, value, map->value_size); | |
150 (*next)->next = 0; | |
151 | |
152 map->count ++; | |
23211
d9b4bfea1093
Make cache_*_add functions return the pointer to new (copied) value.
eugeni
parents:
23025
diff
changeset
|
153 return (*next)->value; |
23016 | 154 } |
155 | |
156 void* hashmap_find(hashmap_t* map, void* key) | |
157 { | |
158 unsigned hash = map->hash(key, map->key_size); | |
159 hashmap_item_t* item = map->root[hash % map->nbuckets]; | |
160 while (item) { | |
161 if (map->key_compare(key, item->key, map->key_size)) { | |
23019
4934af4fdd0f
Collect hit/miss statistic in hash map, and print in -v mode.
eugeni
parents:
23018
diff
changeset
|
162 map->hit_count++; |
23016 | 163 return item->value; |
164 } | |
165 item = item->next; | |
166 } | |
23019
4934af4fdd0f
Collect hit/miss statistic in hash map, and print in -v mode.
eugeni
parents:
23018
diff
changeset
|
167 map->miss_count++; |
23016 | 168 return 0; |
169 } | |
170 | |
171 //--------------------------------- | |
172 // font cache | |
173 | |
174 hashmap_t* font_cache; | |
175 | |
176 static unsigned font_desc_hash(void* buf, size_t len) | |
177 { | |
178 ass_font_desc_t* desc = buf; | |
179 unsigned hval; | |
180 hval = fnv_32a_str(desc->family, FNV1_32A_INIT); | |
181 hval = fnv_32a_buf(&desc->bold, sizeof(desc->bold), hval); | |
182 hval = fnv_32a_buf(&desc->italic, sizeof(desc->italic), hval); | |
183 return hval; | |
184 } | |
185 | |
186 static int font_compare(void* key1, void* key2, size_t key_size) { | |
187 ass_font_desc_t* a = key1; | |
188 ass_font_desc_t* b = key2; | |
18937 | 189 if (strcmp(a->family, b->family) != 0) |
190 return 0; | |
191 if (a->bold != b->bold) | |
192 return 0; | |
193 if (a->italic != b->italic) | |
194 return 0; | |
195 return 1; | |
196 } | |
197 | |
23016 | 198 static void font_hash_dtor(void* key, size_t key_size, void* value, size_t value_size) |
199 { | |
200 ass_font_free(value); | |
201 free(key); | |
202 } | |
203 | |
21317 | 204 ass_font_t* ass_font_cache_find(ass_font_desc_t* desc) |
18937 | 205 { |
23016 | 206 return hashmap_find(font_cache, desc); |
21317 | 207 } |
18937 | 208 |
21317 | 209 /** |
210 * \brief Add a face struct to cache. | |
211 * \param font font struct | |
212 */ | |
23211
d9b4bfea1093
Make cache_*_add functions return the pointer to new (copied) value.
eugeni
parents:
23025
diff
changeset
|
213 void* ass_font_cache_add(ass_font_t* font) |
21317 | 214 { |
23211
d9b4bfea1093
Make cache_*_add functions return the pointer to new (copied) value.
eugeni
parents:
23025
diff
changeset
|
215 return hashmap_insert(font_cache, &(font->desc), font); |
18937 | 216 } |
217 | |
21265 | 218 void ass_font_cache_init(void) |
18937 | 219 { |
23016 | 220 font_cache = hashmap_init(sizeof(ass_font_desc_t), |
221 sizeof(ass_font_t), | |
222 1000, | |
223 font_hash_dtor, font_compare, font_desc_hash); | |
18937 | 224 } |
225 | |
21265 | 226 void ass_font_cache_done(void) |
18937 | 227 { |
23016 | 228 hashmap_done(font_cache); |
18937 | 229 } |
230 | |
231 //--------------------------------- | |
23017 | 232 // bitmap cache |
18937 | 233 |
23017 | 234 hashmap_t* bitmap_cache; |
18937 | 235 |
23017 | 236 static void bitmap_hash_dtor(void* key, size_t key_size, void* value, size_t value_size) |
23016 | 237 { |
23017 | 238 bitmap_hash_val_t* v = value; |
23016 | 239 if (v->bm) ass_free_bitmap(v->bm); |
240 if (v->bm_o) ass_free_bitmap(v->bm_o); | |
241 if (v->bm_s) ass_free_bitmap(v->bm_s); | |
242 free(key); | |
243 free(value); | |
18937 | 244 } |
245 | |
23211
d9b4bfea1093
Make cache_*_add functions return the pointer to new (copied) value.
eugeni
parents:
23025
diff
changeset
|
246 void* cache_add_bitmap(bitmap_hash_key_t* key, bitmap_hash_val_t* val) |
18937 | 247 { |
23211
d9b4bfea1093
Make cache_*_add functions return the pointer to new (copied) value.
eugeni
parents:
23025
diff
changeset
|
248 return hashmap_insert(bitmap_cache, key, val); |
18937 | 249 } |
250 | |
251 /** | |
23017 | 252 * \brief Get a bitmap from bitmap cache. |
18937 | 253 * \param key hash key |
254 * \return requested hash val or 0 if not found | |
255 */ | |
23017 | 256 bitmap_hash_val_t* cache_find_bitmap(bitmap_hash_key_t* key) |
18937 | 257 { |
23017 | 258 return hashmap_find(bitmap_cache, key); |
18937 | 259 } |
260 | |
23017 | 261 void ass_bitmap_cache_init(void) |
18937 | 262 { |
23017 | 263 bitmap_cache = hashmap_init(sizeof(bitmap_hash_key_t), |
264 sizeof(bitmap_hash_val_t), | |
23016 | 265 0xFFFF + 13, |
23017 | 266 bitmap_hash_dtor, NULL, NULL); |
18937 | 267 } |
268 | |
23017 | 269 void ass_bitmap_cache_done(void) |
18937 | 270 { |
23017 | 271 hashmap_done(bitmap_cache); |
18937 | 272 } |
273 | |
23017 | 274 void ass_bitmap_cache_reset(void) |
19539 | 275 { |
23017 | 276 ass_bitmap_cache_done(); |
277 ass_bitmap_cache_init(); | |
19539 | 278 } |
279 | |
23018 | 280 //--------------------------------- |
281 // glyph cache | |
282 | |
283 hashmap_t* glyph_cache; | |
284 | |
285 static void glyph_hash_dtor(void* key, size_t key_size, void* value, size_t value_size) | |
286 { | |
287 glyph_hash_val_t* v = value; | |
288 if (v->glyph) FT_Done_Glyph(v->glyph); | |
23025
ab0943242d1a
Store outline_glyph (glyph border) in glyph cache.
eugeni
parents:
23020
diff
changeset
|
289 if (v->outline_glyph) FT_Done_Glyph(v->outline_glyph); |
23018 | 290 free(key); |
291 free(value); | |
292 } | |
293 | |
23211
d9b4bfea1093
Make cache_*_add functions return the pointer to new (copied) value.
eugeni
parents:
23025
diff
changeset
|
294 void* cache_add_glyph(glyph_hash_key_t* key, glyph_hash_val_t* val) |
23018 | 295 { |
23211
d9b4bfea1093
Make cache_*_add functions return the pointer to new (copied) value.
eugeni
parents:
23025
diff
changeset
|
296 return hashmap_insert(glyph_cache, key, val); |
23018 | 297 } |
298 | |
299 /** | |
300 * \brief Get a glyph from glyph cache. | |
301 * \param key hash key | |
302 * \return requested hash val or 0 if not found | |
303 */ | |
304 glyph_hash_val_t* cache_find_glyph(glyph_hash_key_t* key) | |
305 { | |
306 return hashmap_find(glyph_cache, key); | |
307 } | |
308 | |
309 void ass_glyph_cache_init(void) | |
310 { | |
311 glyph_cache = hashmap_init(sizeof(glyph_hash_key_t), | |
312 sizeof(glyph_hash_val_t), | |
313 0xFFFF + 13, | |
314 glyph_hash_dtor, NULL, NULL); | |
315 } | |
316 | |
317 void ass_glyph_cache_done(void) | |
318 { | |
319 hashmap_done(glyph_cache); | |
320 } | |
321 | |
322 void ass_glyph_cache_reset(void) | |
323 { | |
324 ass_glyph_cache_done(); | |
325 ass_glyph_cache_init(); | |
326 } |