annotate libass/ass_utils.h @ 35316:18fdd24d97ff

Do not pointlessly call getenv("HOME") twice, reuse the result instead.
author reimar
date Sat, 10 Nov 2012 14:45:56 +0000
parents 49fc594fda43
children c3aaaf17c721
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: 18937
diff changeset
1 /*
26723
0f892cd714b2 Use standard license header.
diego
parents: 26138
diff changeset
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
0f892cd714b2 Use standard license header.
diego
parents: 26138
diff changeset
3 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
4 * This file is part of libass.
26723
0f892cd714b2 Use standard license header.
diego
parents: 26138
diff changeset
5 *
34011
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 30200
diff changeset
6 * Permission to use, copy, modify, and distribute this software for any
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 30200
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 30200
diff changeset
8 * copyright notice and this permission notice appear in all copies.
26723
0f892cd714b2 Use standard license header.
diego
parents: 26138
diff changeset
9 *
34011
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 30200
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 30200
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 30200
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 30200
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 30200
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 30200
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
88eebbbbd6a0 Update included libass copy to 0.9.13 release.
reimar
parents: 30200
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26723
0f892cd714b2 Use standard license header.
diego
parents: 26138
diff changeset
17 */
20008
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 18937
diff changeset
18
25897
aaebaf255b23 Consistently give all libass multiple inclusion guards a LIBASS_ prefix.
diego
parents: 25535
diff changeset
19 #ifndef LIBASS_UTILS_H
aaebaf255b23 Consistently give all libass multiple inclusion guards a LIBASS_ prefix.
diego
parents: 25535
diff changeset
20 #define LIBASS_UTILS_H
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
21
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
22 #include <stdio.h>
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
23 #include <stdarg.h>
26138
74055622161d Add missing header #includes to fix 'make checkheaders'.
diego
parents: 26036
diff changeset
24 #include <stdint.h>
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
25 #include <stdlib.h>
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
26 #include <string.h>
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
27 #include <assert.h>
26138
74055622161d Add missing header #includes to fix 'make checkheaders'.
diego
parents: 26036
diff changeset
28
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
29 #ifdef CONFIG_ENCA
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
30 #include <enca.h>
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
31 #endif
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
32
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
33 #include "ass.h"
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
34
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
35 #define MSGL_FATAL 0
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
36 #define MSGL_ERR 1
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
37 #define MSGL_WARN 2
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
38 #define MSGL_INFO 4
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
39 #define MSGL_V 6
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
40 #define MSGL_DBG2 7
22213
66abe12ad374 Move conversions between 16.16, 26.6 fixed point and int, double to separate
eugeni
parents: 20503
diff changeset
41
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
42 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
43 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
44 #define FFMINMAX(c,a,b) FFMIN(FFMAX(c, a), b)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
45
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
46 int mystrtoi(char **p, int *res);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
47 int mystrtoll(char **p, long long *res);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
48 int mystrtou32(char **p, int base, uint32_t *res);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
49 int mystrtod(char **p, double *res);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
50 int strtocolor(ASS_Library *library, char **q, uint32_t *res, int hex);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
51 char parse_bool(char *str);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
52 unsigned ass_utf8_get_char(char **str);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
53 void ass_msg(ASS_Library *priv, int lvl, char *fmt, ...);
35262
49fc594fda43 Updated libass to 0.10.1
SubJunk
parents: 34011
diff changeset
54 int lookup_style(ASS_Track *track, char *name);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
55 #ifdef CONFIG_ENCA
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
56 void *ass_guess_buffer_cp(ASS_Library *library, unsigned char *buffer,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
57 int buflen, char *preferred_language,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
58 char *fallback);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
59 #endif
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
60
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
61 /* defined in ass_strtod.c */
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
62 double ass_strtod(const char *string, char **endPtr);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
63
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
64 static inline int d6_to_int(int x)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
65 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
66 return (x + 32) >> 6;
22213
66abe12ad374 Move conversions between 16.16, 26.6 fixed point and int, double to separate
eugeni
parents: 20503
diff changeset
67 }
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
68 static inline int d16_to_int(int x)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
69 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
70 return (x + 32768) >> 16;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
71 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
72 static inline int int_to_d6(int x)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
73 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
74 return x << 6;
22213
66abe12ad374 Move conversions between 16.16, 26.6 fixed point and int, double to separate
eugeni
parents: 20503
diff changeset
75 }
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
76 static inline int int_to_d16(int x)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
77 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
78 return x << 16;
22213
66abe12ad374 Move conversions between 16.16, 26.6 fixed point and int, double to separate
eugeni
parents: 20503
diff changeset
79 }
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
80 static inline int d16_to_d6(int x)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
81 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
82 return (x + 512) >> 10;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
83 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
84 static inline int d6_to_d16(int x)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
85 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
86 return x << 10;
22213
66abe12ad374 Move conversions between 16.16, 26.6 fixed point and int, double to separate
eugeni
parents: 20503
diff changeset
87 }
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
88 static inline double d6_to_double(int x)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
89 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
90 return x / 64.;
22213
66abe12ad374 Move conversions between 16.16, 26.6 fixed point and int, double to separate
eugeni
parents: 20503
diff changeset
91 }
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
92 static inline int double_to_d6(double x)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
93 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
94 return (int) (x * 64);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
95 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
96 static inline double d16_to_double(int x)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
97 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
98 return ((double) x) / 0x10000;
22213
66abe12ad374 Move conversions between 16.16, 26.6 fixed point and int, double to separate
eugeni
parents: 20503
diff changeset
99 }
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
100 static inline int double_to_d16(double x)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
101 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
102 return (int) (x * 0x10000);
23299
0ee56ec36a40 Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents: 22213
diff changeset
103 }
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
104 static inline double d22_to_double(int x)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
105 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
106 return ((double) x) / 0x400000;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
107 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
108 static inline int double_to_d22(double x)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
109 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
110 return (int) (x * 0x400000);
23299
0ee56ec36a40 Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents: 22213
diff changeset
111 }
22213
66abe12ad374 Move conversions between 16.16, 26.6 fixed point and int, double to separate
eugeni
parents: 20503
diff changeset
112
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
113 // Calculate cache key for a rotational angle in degrees
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
114 static inline int rot_key(double a)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
115 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
116 const int m = double_to_d22(360.0);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
117 return double_to_d22(a) % m;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
118 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
119
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
120 #define FNV1_32A_INIT (unsigned)0x811c9dc5
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
121
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
122 static inline unsigned fnv_32a_buf(void *buf, size_t len, unsigned hval)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
123 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
124 unsigned char *bp = buf;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
125 unsigned char *be = bp + len;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
126 while (bp < be) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
127 hval ^= (unsigned) *bp++;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
128 hval +=
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
129 (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) +
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
130 (hval << 24);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
131 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
132 return hval;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
133 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
134 static inline unsigned fnv_32a_str(char *str, unsigned hval)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
135 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
136 unsigned char *s = (unsigned char *) str;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
137 while (*s) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
138 hval ^= (unsigned) *s++;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
139 hval +=
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
140 (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) +
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
141 (hval << 24);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
142 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
143 return hval;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
144 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
145
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 28785
diff changeset
146 #endif /* LIBASS_UTILS_H */