annotate libass/ass_cache.c @ 34674:a99f159b137d

Revise listMgr() command PLAYLIST_ITEM_ADD. Change return value to pointer to current playlist item. Additionally, some cosmetic code and formatting changes.
author ib
date Thu, 23 Feb 2012 11:44:49 +0000
parents 6e7f60f6f9d4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20008
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19965
diff changeset
1 /*
26723
0f892cd714b2 Use standard license header.
diego
parents: 23214
diff changeset
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
3 * Copyright (C) 2011 Grigori Goronzy <greg@chown.ath.cx>
26723
0f892cd714b2 Use standard license header.
diego
parents: 23214
diff changeset
4 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
5 * This file is part of libass.
26723
0f892cd714b2 Use standard license header.
diego
parents: 23214
diff changeset
6 *
34011
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31853
diff changeset
7 * Permission to use, copy, modify, and distribute this software for any
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31853
diff changeset
8 * purpose with or without fee is hereby granted, provided that the above
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31853
diff changeset
9 * copyright notice and this permission notice appear in all copies.
26723
0f892cd714b2 Use standard license header.
diego
parents: 23214
diff changeset
10 *
34011
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31853
diff changeset
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31853
diff changeset
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31853
diff changeset
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31853
diff changeset
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31853
diff changeset
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31853
diff changeset
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 31853
diff changeset
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26723
0f892cd714b2 Use standard license header.
diego
parents: 23214
diff changeset
18 */
20008
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19965
diff changeset
19
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
20 #include "config.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
21
22292
2f3f27a014ef Add some missing includes.
eugeni
parents: 22215
diff changeset
22 #include <inttypes.h>
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
23 #include <ft2build.h>
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
24 #include FT_OUTLINE_H
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
25 #include <assert.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
26
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
27 #include "ass_utils.h"
21322
bb55f4f7b3cb Fix compilation broken in previous commit.
eugeni
parents: 21317
diff changeset
28 #include "ass_font.h"
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
29 #include "ass_cache.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
30
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
31 // type-specific functions
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
32 // create hash/compare functions for bitmap, outline and composite cache
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
33 #define CREATE_HASH_FUNCTIONS
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
34 #include "ass_cache_template.h"
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
35 #define CREATE_COMPARISON_FUNCTIONS
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
36 #include "ass_cache_template.h"
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28860
diff changeset
37
23016
a1ced37f2ee5 Add generic hash map implementation.
eugeni
parents: 22292
diff changeset
38 // font cache
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
39 static unsigned font_hash(void *buf, size_t len)
23016
a1ced37f2ee5 Add generic hash map implementation.
eugeni
parents: 22292
diff changeset
40 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
41 ASS_FontDesc *desc = buf;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
42 unsigned hval;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
43 hval = fnv_32a_str(desc->family, FNV1_32A_INIT);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
44 hval = fnv_32a_buf(&desc->bold, sizeof(desc->bold), hval);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
45 hval = fnv_32a_buf(&desc->italic, sizeof(desc->italic), hval);
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
46 hval = fnv_32a_buf(&desc->treat_family_as_pattern,
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
47 sizeof(desc->treat_family_as_pattern), hval);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
48 hval = fnv_32a_buf(&desc->vertical, sizeof(desc->vertical), hval);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
49 return hval;
23016
a1ced37f2ee5 Add generic hash map implementation.
eugeni
parents: 22292
diff changeset
50 }
a1ced37f2ee5 Add generic hash map implementation.
eugeni
parents: 22292
diff changeset
51
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
52 static unsigned font_compare(void *key1, void *key2, size_t key_size)
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
53 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
54 ASS_FontDesc *a = key1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
55 ASS_FontDesc *b = key2;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
56 if (strcmp(a->family, b->family) != 0)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
57 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
58 if (a->bold != b->bold)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
59 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
60 if (a->italic != b->italic)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
61 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
62 if (a->treat_family_as_pattern != b->treat_family_as_pattern)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
63 return 0;
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
64 if (a->vertical != b->vertical)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
65 return 0;
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
66 return 1;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
67 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
68
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
69 static void font_destruct(void *key, void *value)
23016
a1ced37f2ee5 Add generic hash map implementation.
eugeni
parents: 22292
diff changeset
70 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
71 ass_font_free(value);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
72 free(key);
23016
a1ced37f2ee5 Add generic hash map implementation.
eugeni
parents: 22292
diff changeset
73 }
a1ced37f2ee5 Add generic hash map implementation.
eugeni
parents: 22292
diff changeset
74
23017
f3b04984b0da Rename glyph cache to bitmap cache.
eugeni
parents: 23016
diff changeset
75 // bitmap cache
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
76 static void bitmap_destruct(void *key, void *value)
23016
a1ced37f2ee5 Add generic hash map implementation.
eugeni
parents: 22292
diff changeset
77 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
78 BitmapHashValue *v = value;
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
79 BitmapHashKey *k = key;
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
80 if (v->bm)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
81 ass_free_bitmap(v->bm);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
82 if (v->bm_o)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
83 ass_free_bitmap(v->bm_o);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
84 if (v->bm_s)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
85 ass_free_bitmap(v->bm_s);
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
86 if (k->type == BITMAP_CLIP)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
87 free(k->u.clip.text);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
88 free(key);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
89 free(value);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
90 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
91
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
92 static size_t bitmap_size(void *value, size_t value_size)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
93 {
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
94 BitmapHashValue *val = value;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
95 if (val->bm_o)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
96 return val->bm_o->w * val->bm_o->h * 3;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
97 else if (val->bm)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
98 return val->bm->w * val->bm->h * 3;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
99 return 0;
19539
64009ae411fb Reset glyph cache on reconfigure.
eugeni
parents: 18937
diff changeset
100 }
64009ae411fb Reset glyph cache on reconfigure.
eugeni
parents: 18937
diff changeset
101
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
102 static unsigned bitmap_hash(void *key, size_t key_size)
23018
a4517aa83565 Add outline glyph cache (unused yet).
eugeni
parents: 23017
diff changeset
103 {
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
104 BitmapHashKey *k = key;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
105 switch (k->type) {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
106 case BITMAP_OUTLINE: return outline_bitmap_hash(&k->u, key_size);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
107 case BITMAP_CLIP: return clip_bitmap_hash(&k->u, key_size);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
108 default: return 0;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
109 }
23018
a4517aa83565 Add outline glyph cache (unused yet).
eugeni
parents: 23017
diff changeset
110 }
a4517aa83565 Add outline glyph cache (unused yet).
eugeni
parents: 23017
diff changeset
111
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
112 static unsigned bitmap_compare (void *a, void *b, size_t key_size)
23018
a4517aa83565 Add outline glyph cache (unused yet).
eugeni
parents: 23017
diff changeset
113 {
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
114 BitmapHashKey *ak = a;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
115 BitmapHashKey *bk = b;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
116 if (ak->type != bk->type) return 0;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
117 switch (ak->type) {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
118 case BITMAP_OUTLINE: return outline_bitmap_compare(&ak->u, &bk->u, key_size);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
119 case BITMAP_CLIP: return clip_bitmap_compare(&ak->u, &bk->u, key_size);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
120 default: return 0;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
121 }
23018
a4517aa83565 Add outline glyph cache (unused yet).
eugeni
parents: 23017
diff changeset
122 }
a4517aa83565 Add outline glyph cache (unused yet).
eugeni
parents: 23017
diff changeset
123
28789
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
124 // composite cache
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
125 static void composite_destruct(void *key, void *value)
28789
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
126 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
127 CompositeHashValue *v = value;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
128 free(v->a);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
129 free(v->b);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
130 free(key);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
131 free(value);
28789
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
132 }
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
133
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
134 // outline cache
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
135
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
136 static unsigned outline_hash(void *key, size_t key_size)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
137 {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
138 OutlineHashKey *k = key;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
139 switch (k->type) {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
140 case OUTLINE_GLYPH: return glyph_hash(&k->u, key_size);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
141 case OUTLINE_DRAWING: return drawing_hash(&k->u, key_size);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
142 default: return 0;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
143 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
144 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
145
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
146 static unsigned outline_compare(void *a, void *b, size_t key_size)
28789
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
147 {
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
148 OutlineHashKey *ak = a;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
149 OutlineHashKey *bk = b;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
150 if (ak->type != bk->type) return 0;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
151 switch (ak->type) {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
152 case OUTLINE_GLYPH: return glyph_compare(&ak->u, &bk->u, key_size);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
153 case OUTLINE_DRAWING: return drawing_compare(&ak->u, &bk->u, key_size);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
154 default: return 0;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
155 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
156 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
157
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
158 static void outline_destruct(void *key, void *value)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
159 {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
160 OutlineHashValue *v = value;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
161 OutlineHashKey *k = key;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
162 if (v->outline)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
163 outline_free(v->lib, v->outline);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
164 if (v->border)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
165 outline_free(v->lib, v->border);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
166 if (k->type == OUTLINE_DRAWING)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
167 free(k->u.drawing.text);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
168 free(key);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
169 free(value);
28789
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
170 }
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
171
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
172
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
173
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
174 // Cache data
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
175 typedef struct cache_item {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
176 void *key;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
177 void *value;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
178 struct cache_item *next;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
179 } CacheItem;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
180
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
181 struct cache {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
182 unsigned buckets;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
183 CacheItem **map;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
184
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
185 HashFunction hash_func;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
186 ItemSize size_func;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
187 HashCompare compare_func;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
188 CacheItemDestructor destruct_func;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
189 size_t key_size;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
190 size_t value_size;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
191
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
192 size_t cache_size;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
193 unsigned hits;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
194 unsigned misses;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
195 unsigned items;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
196 };
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
197
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
198 // Hash for a simple (single value or array) type
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
199 static unsigned hash_simple(void *key, size_t key_size)
28789
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
200 {
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
201 return fnv_32a_buf(key, key_size, FNV1_32A_INIT);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
202 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
203
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
204 // Comparison of a simple type
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
205 static unsigned compare_simple(void *a, void *b, size_t key_size)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
206 {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
207 return memcmp(a, b, key_size) == 0;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
208 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
209
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
210 // Default destructor
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
211 static void destruct_simple(void *key, void *value)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
212 {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
213 free(key);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
214 free(value);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
215 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
216
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
217
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
218 // Create a cache with type-specific hash/compare/destruct/size functions
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
219 Cache *ass_cache_create(HashFunction hash_func, HashCompare compare_func,
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
220 CacheItemDestructor destruct_func, ItemSize size_func,
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
221 size_t key_size, size_t value_size)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
222 {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
223 Cache *cache = calloc(1, sizeof(*cache));
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
224 cache->buckets = 0xFFFF;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
225 cache->hash_func = hash_simple;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
226 cache->compare_func = compare_simple;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
227 cache->destruct_func = destruct_simple;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
228 cache->size_func = size_func;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
229 if (hash_func)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
230 cache->hash_func = hash_func;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
231 if (compare_func)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
232 cache->compare_func = compare_func;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
233 if (destruct_func)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
234 cache->destruct_func = destruct_func;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
235 cache->key_size = key_size;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
236 cache->value_size = value_size;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
237 cache->map = calloc(cache->buckets, sizeof(CacheItem *));
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
238
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
239 return cache;
28789
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
240 }
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
241
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
242 void *ass_cache_put(Cache *cache, void *key, void *value)
28789
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
243 {
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
244 unsigned bucket = cache->hash_func(key, cache->key_size) % cache->buckets;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
245 CacheItem **item = &cache->map[bucket];
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
246 while (*item)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
247 item = &(*item)->next;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
248 (*item) = calloc(1, sizeof(CacheItem));
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
249 (*item)->key = malloc(cache->key_size);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
250 (*item)->value = malloc(cache->value_size);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
251 memcpy((*item)->key, key, cache->key_size);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
252 memcpy((*item)->value, value, cache->value_size);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
253
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
254 cache->items++;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
255 if (cache->size_func)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
256 cache->cache_size += cache->size_func(value, cache->value_size);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
257 else
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
258 cache->cache_size++;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
259
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
260 return (*item)->value;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
261 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
262
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
263 void *ass_cache_get(Cache *cache, void *key)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
264 {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
265 unsigned bucket = cache->hash_func(key, cache->key_size) % cache->buckets;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
266 CacheItem *item = cache->map[bucket];
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
267 while (item) {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
268 if (cache->compare_func(key, item->key, cache->key_size)) {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
269 cache->hits++;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
270 return item->value;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
271 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
272 item = item->next;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
273 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
274 cache->misses++;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
275 return NULL;
28789
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
276 }
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
277
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
278 int ass_cache_empty(Cache *cache, size_t max_size)
28789
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
279 {
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
280 int i;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
281
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
282 if (cache->cache_size < max_size)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
283 return 0;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
284
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
285 for (i = 0; i < cache->buckets; i++) {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
286 CacheItem *item = cache->map[i];
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
287 while (item) {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
288 CacheItem *next = item->next;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
289 cache->destruct_func(item->key, item->value);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
290 free(item);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
291 item = next;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
292 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
293 cache->map[i] = NULL;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
294 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
296 cache->items = cache->hits = cache->misses = cache->cache_size = 0;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
297
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
298 return 1;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
299 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
300
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
301 void ass_cache_stats(Cache *cache, size_t *size, unsigned *hits,
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
302 unsigned *misses, unsigned *count)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
303 {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
304 if (size)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
305 *size = cache->cache_size;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
306 if (hits)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
307 *hits = cache->hits;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
308 if (misses)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
309 *misses = cache->misses;
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
310 if (count)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
311 *count = cache->items;
28789
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
312 }
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
313
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
314 void ass_cache_done(Cache *cache)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
315 {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
316 ass_cache_empty(cache, 0);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
317 free(cache->map);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
318 free(cache);
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
319 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
320
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
321 // Type-specific creation function
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
322 Cache *ass_font_cache_create(void)
28789
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
323 {
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
324 return ass_cache_create(font_hash, font_compare, font_destruct,
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
325 (ItemSize)NULL, sizeof(ASS_FontDesc), sizeof(ASS_Font));
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
326 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
327
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
328 Cache *ass_outline_cache_create(void)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
329 {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
330 return ass_cache_create(outline_hash, outline_compare, outline_destruct,
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
331 NULL, sizeof(OutlineHashKey), sizeof(OutlineHashValue));
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
332 }
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29263
diff changeset
333
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
334 Cache *ass_glyph_metrics_cache_create(void)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
335 {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
336 return ass_cache_create(glyph_metrics_hash, glyph_metrics_compare, NULL,
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
337 (ItemSize) NULL, sizeof(GlyphMetricsHashKey),
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
338 sizeof(GlyphMetricsHashValue));
28789
a0ce88ba2557 Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents: 26759
diff changeset
339 }
34295
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
340
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
341 Cache *ass_bitmap_cache_create(void)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
342 {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
343 return ass_cache_create(bitmap_hash, bitmap_compare, bitmap_destruct,
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
344 bitmap_size, sizeof(BitmapHashKey), sizeof(BitmapHashValue));
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
345 }
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
346
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
347 Cache *ass_composite_cache_create(void)
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
348 {
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
349 return ass_cache_create(composite_hash, composite_compare,
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
350 composite_destruct, (ItemSize)NULL, sizeof(CompositeHashKey),
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
351 sizeof(CompositeHashValue));
6e7f60f6f9d4 Update libass to 0.10 release.
reimar
parents: 34011
diff changeset
352 }