]> granicus.if.org Git - libass/blob - libass/ass_utils.h
rasterizer: drop outlines with points at too large coordinates
[libass] / libass / ass_utils.h
1 /*
2  * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
3  *
4  * This file is part of libass.
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #ifndef LIBASS_UTILS_H
20 #define LIBASS_UTILS_H
21
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <errno.h>
29 #include <math.h>
30
31 #include "ass.h"
32
33 #ifndef SIZE_MAX
34 #define SIZE_MAX ((size_t)-1)
35 #endif
36
37 #define MSGL_FATAL 0
38 #define MSGL_ERR 1
39 #define MSGL_WARN 2
40 #define MSGL_INFO 4
41 #define MSGL_V 6
42 #define MSGL_DBG2 7
43
44 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
45 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
46 #define FFMINMAX(c,a,b) FFMIN(FFMAX(c, a), b)
47
48 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_ASM
49 int has_sse2(void);
50 int has_avx(void);
51 int has_avx2(void);
52 #endif
53
54 #ifndef HAVE_STRNDUP
55 char *ass_strndup(const char *s, size_t n);
56 #define strndup ass_strndup
57 #endif
58
59 void *ass_aligned_alloc(size_t alignment, size_t size);
60 void ass_aligned_free(void *ptr);
61
62 void *ass_realloc_array(void *ptr, size_t nmemb, size_t size);
63 void *ass_try_realloc_array(void *ptr, size_t nmemb, size_t size);
64
65 /**
66  * Reallocate the array in ptr to at least count elements. For example, if
67  * you do "int *ptr = NULL; ASS_REALLOC_ARRAY(ptr, 5)", you can access ptr[0]
68  * through ptr[4] (inclusive).
69  *
70  * If memory allocation fails, ptr is left unchanged, and the macro returns 0:
71  * "if (!ASS_REALLOC_ARRAY(ptr, 5)) goto error;"
72  *
73  * A count of 0 does not free the array (see ass_realloc_array for remarks).
74  */
75 #define ASS_REALLOC_ARRAY(ptr, count) \
76     (errno = 0, (ptr) = ass_try_realloc_array(ptr, count, sizeof(*ptr)), !errno)
77
78 void skip_spaces(char **str);
79 void rskip_spaces(char **str, char *limit);
80 int mystrtoi(char **p, int *res);
81 int mystrtoll(char **p, long long *res);
82 int mystrtod(char **p, double *res);
83 int mystrtoi32(char **p, int base, int32_t *res);
84 int32_t parse_alpha_tag(char *str);
85 uint32_t parse_color_tag(char *str);
86 uint32_t parse_color_header(char *str);
87 char parse_bool(char *str);
88 int parse_ycbcr_matrix(char *str);
89 unsigned ass_utf8_get_char(char **str);
90 unsigned ass_utf8_put_char(char *dest, uint32_t ch);
91 void ass_utf16be_to_utf8(char *dst, size_t dst_size, uint8_t *src, size_t src_size);
92 void ass_msg(ASS_Library *priv, int lvl, const char *fmt, ...);
93 int lookup_style(ASS_Track *track, char *name);
94 ASS_Style *lookup_style_strict(ASS_Track *track, char *name, size_t len);
95
96 /* defined in ass_strtod.c */
97 double ass_strtod(const char *string, char **endPtr);
98
99 static inline size_t ass_align(size_t alignment, size_t s)
100 {
101     if (s > SIZE_MAX - (alignment - 1))
102         return s;
103     return (s + (alignment - 1)) & ~(alignment - 1);
104 }
105
106 static inline uint32_t ass_bswap32(uint32_t x)
107 {
108 #ifdef _MSC_VER
109     return _byteswap_ulong(x);
110 #else
111     return (x & 0x000000FF) << 24 | (x & 0x0000FF00) <<  8 |
112            (x & 0x00FF0000) >>  8 | (x & 0xFF000000) >> 24;
113 #endif
114 }
115
116 static inline int d6_to_int(int x)
117 {
118     return (x + 32) >> 6;
119 }
120 static inline int d16_to_int(int x)
121 {
122     return (x + 32768) >> 16;
123 }
124 static inline int int_to_d6(int x)
125 {
126     return x * (1 << 6);
127 }
128 static inline int int_to_d16(int x)
129 {
130     return x * (1 << 16);
131 }
132 static inline int d16_to_d6(int x)
133 {
134     return (x + 512) >> 10;
135 }
136 static inline int d6_to_d16(int x)
137 {
138     return x * (1 << 10);
139 }
140 static inline double d6_to_double(int x)
141 {
142     return x / 64.;
143 }
144 static inline int double_to_d6(double x)
145 {
146     return (int) (x * 64);
147 }
148 static inline double d16_to_double(int x)
149 {
150     return ((double) x) / 0x10000;
151 }
152 static inline int double_to_d16(double x)
153 {
154     return (int) (x * 0x10000);
155 }
156 static inline double d22_to_double(int x)
157 {
158     return ((double) x) / 0x400000;
159 }
160 static inline int double_to_d22(double x)
161 {
162     return (int) (x * 0x400000);
163 }
164
165 // Calculate cache key for a rotational angle in radians
166 static inline int rot_key(double a)
167 {
168     return double_to_d22(remainder(a, 2 * M_PI));
169 }
170
171 #define FNV1_32A_INIT 0x811c9dc5U
172 #define FNV1_32A_PRIME 16777619U
173
174 static inline unsigned fnv_32a_buf(void *buf, size_t len, unsigned hval)
175 {
176     unsigned char *bp = (unsigned char*)buf;
177     size_t n = (len + 3) / 4;
178
179     switch (len % 4) {
180     case 0: do { hval ^= (unsigned) *bp++; hval *= FNV1_32A_PRIME;
181     case 3:      hval ^= (unsigned) *bp++; hval *= FNV1_32A_PRIME;
182     case 2:      hval ^= (unsigned) *bp++; hval *= FNV1_32A_PRIME;
183     case 1:      hval ^= (unsigned) *bp++; hval *= FNV1_32A_PRIME;
184                } while (--n > 0);
185     }
186
187     return hval;
188 }
189 static inline unsigned fnv_32a_str(char *str, unsigned hval)
190 {
191     unsigned char *s = (unsigned char *) str;
192     while (*s) {
193         hval ^= (unsigned) *s++;
194         hval *= FNV1_32A_PRIME;
195     }
196     return hval;
197 }
198
199 #endif                          /* LIBASS_UTILS_H */