2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4 * This file is part of libass.
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.
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.
19 #ifndef LIBASS_UTILS_H
20 #define LIBASS_UTILS_H
34 #define SIZE_MAX ((size_t)-1)
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)
48 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_ASM
55 char *ass_strndup(const char *s, size_t n);
56 #define strndup ass_strndup
59 void *ass_aligned_alloc(size_t alignment, size_t size);
60 void ass_aligned_free(void *ptr);
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);
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).
70 * If memory allocation fails, ptr is left unchanged, and the macro returns 0:
71 * "if (!ASS_REALLOC_ARRAY(ptr, 5)) goto error;"
73 * A count of 0 does not free the array (see ass_realloc_array for remarks).
75 #define ASS_REALLOC_ARRAY(ptr, count) \
76 (errno = 0, (ptr) = ass_try_realloc_array(ptr, count, sizeof(*ptr)), !errno)
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);
96 /* defined in ass_strtod.c */
97 double ass_strtod(const char *string, char **endPtr);
99 static inline size_t ass_align(size_t alignment, size_t s)
101 if (s > SIZE_MAX - (alignment - 1))
103 return (s + (alignment - 1)) & ~(alignment - 1);
106 static inline uint32_t ass_bswap32(uint32_t x)
109 return _byteswap_ulong(x);
111 return (x & 0x000000FF) << 24 | (x & 0x0000FF00) << 8 |
112 (x & 0x00FF0000) >> 8 | (x & 0xFF000000) >> 24;
116 static inline int d6_to_int(int x)
118 return (x + 32) >> 6;
120 static inline int d16_to_int(int x)
122 return (x + 32768) >> 16;
124 static inline int int_to_d6(int x)
128 static inline int int_to_d16(int x)
130 return x * (1 << 16);
132 static inline int d16_to_d6(int x)
134 return (x + 512) >> 10;
136 static inline int d6_to_d16(int x)
138 return x * (1 << 10);
140 static inline double d6_to_double(int x)
144 static inline int double_to_d6(double x)
146 return (int) (x * 64);
148 static inline double d16_to_double(int x)
150 return ((double) x) / 0x10000;
152 static inline int double_to_d16(double x)
154 return (int) (x * 0x10000);
156 static inline double d22_to_double(int x)
158 return ((double) x) / 0x400000;
160 static inline int double_to_d22(double x)
162 return (int) (x * 0x400000);
165 // Calculate cache key for a rotational angle in radians
166 static inline int rot_key(double a)
168 return double_to_d22(remainder(a, 2 * M_PI));
171 #define FNV1_32A_INIT 0x811c9dc5U
172 #define FNV1_32A_PRIME 16777619U
174 static inline unsigned fnv_32a_buf(void *buf, size_t len, unsigned hval)
176 unsigned char *bp = (unsigned char*)buf;
177 size_t n = (len + 3) / 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;
189 static inline unsigned fnv_32a_str(char *str, unsigned hval)
191 unsigned char *s = (unsigned char *) str;
193 hval ^= (unsigned) *s++;
194 hval *= FNV1_32A_PRIME;
199 #endif /* LIBASS_UTILS_H */