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.
20 #include "ass_compat.h"
28 #include "ass_library.h"
30 #include "ass_utils.h"
31 #include "ass_string.h"
33 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_ASM
35 #include "x86/cpuid.h"
39 uint32_t eax = 1, ebx, ecx, edx;
40 ass_get_cpuid(&eax, &ebx, &ecx, &edx);
41 return (edx >> 26) & 0x1;
46 uint32_t eax = 1, ebx, ecx, edx;
47 ass_get_cpuid(&eax, &ebx, &ecx, &edx);
48 if(!(ecx & (1 << 27))) // not OSXSAVE
51 ass_get_xgetbv(0, &eax, &edx);
52 if((eax & 0x6) != 0x6)
55 ass_get_cpuid(&eax, &ebx, &ecx, &edx);
56 return (ecx & 0x6) == 0x6 ? (misc >> 28) & 0x1 : 0; // check high bits are relevant, then AVX support
61 uint32_t eax = 7, ebx, ecx, edx;
62 ass_get_cpuid(&eax, &ebx, &ecx, &edx);
63 return (ebx >> 5) & has_avx();
69 char *ass_strndup(const char *s, size_t n)
71 char *end = memchr(s, 0, n);
72 size_t len = end ? end - s : n;
73 char *new = len < SIZE_MAX ? malloc(len + 1) : NULL;
82 void *ass_aligned_alloc(size_t alignment, size_t size)
84 assert(!(alignment & (alignment - 1))); // alignment must be power of 2
85 if (size >= SIZE_MAX - alignment - sizeof(void *))
87 char *allocation = malloc(size + sizeof(void *) + alignment - 1);
90 char *ptr = allocation + sizeof(void *);
91 unsigned int misalign = (uintptr_t)ptr & (alignment - 1);
93 ptr += alignment - misalign;
94 *((void **)ptr - 1) = allocation;
98 void ass_aligned_free(void *ptr)
101 free(*((void **)ptr - 1));
105 * This works similar to realloc(ptr, nmemb * size), but checks for overflow.
107 * Unlike some implementations of realloc, this never acts as a call to free().
108 * If the total size is 0, it is bumped up to 1. This means a NULL return always
109 * means allocation failure, and the unportable realloc(0, 0) case is avoided.
111 void *ass_realloc_array(void *ptr, size_t nmemb, size_t size)
113 if (nmemb > (SIZE_MAX / size))
119 return realloc(ptr, size);
123 * Like ass_realloc_array(), but:
124 * 1. on failure, return the original ptr value, instead of NULL
125 * 2. set errno to indicate failure (errno!=0) or success (errno==0)
127 void *ass_try_realloc_array(void *ptr, size_t nmemb, size_t size)
129 void *new_ptr = ass_realloc_array(ptr, nmemb, size);
139 void skip_spaces(char **str)
142 while ((*p == ' ') || (*p == '\t'))
147 void rskip_spaces(char **str, char *limit)
150 while ((p > limit) && ((p[-1] == ' ') || (p[-1] == '\t')))
155 int mystrtoi(char **p, int *res)
158 double temp_res = ass_strtod(*p, p);
159 *res = (int) (temp_res + (temp_res > 0 ? 0.5 : -0.5));
163 int mystrtoll(char **p, long long *res)
166 double temp_res = ass_strtod(*p, p);
167 *res = (long long) (temp_res + (temp_res > 0 ? 0.5 : -0.5));
171 int mystrtod(char **p, double *res)
174 *res = ass_strtod(*p, p);
178 int mystrtoi32(char **p, int base, int32_t *res)
181 long long temp_res = strtoll(*p, p, base);
182 *res = FFMINMAX(temp_res, INT32_MIN, INT32_MAX);
186 static int read_digits(char **str, int base, uint32_t *res)
194 if (*p >= '0' && *p < base + '0')
196 else if (*p >= 'a' && *p < base - 10 + 'a')
197 digit = *p - 'a' + 10;
198 else if (*p >= 'A' && *p < base - 10 + 'A')
199 digit = *p - 'A' + 10;
202 val = val * base + digit;
212 * \brief Convert a string to an integer reduced modulo 2**32
213 * Follows the rules for strtoul but reduces the number modulo 2**32
214 * instead of saturating it to 2**32 - 1.
216 static int mystrtou32_modulo(char **p, int base, uint32_t *res)
218 // This emulates scanf with %d or %x format as it works on
219 // Windows, because that's what is used by VSFilter. In practice,
220 // scanf works the same way on other platforms too, but
221 // the standard leaves its behavior on overflow undefined.
223 // Unlike scanf and like strtoul, produce 0 for invalid inputs.
235 if (base == 16 && !ass_strncasecmp(*p, "0x", 2))
238 if (read_digits(p, base, res)) {
247 int32_t parse_alpha_tag(char *str)
251 while (*str == '&' || *str == 'H')
254 mystrtoi32(&str, 16, &alpha);
258 uint32_t parse_color_tag(char *str)
262 while (*str == '&' || *str == 'H')
265 mystrtoi32(&str, 16, &color);
266 return ass_bswap32((uint32_t) color);
269 uint32_t parse_color_header(char *str)
274 if (!ass_strncasecmp(str, "&h", 2) || !ass_strncasecmp(str, "0x", 2)) {
280 mystrtou32_modulo(&str, base, &color);
281 return ass_bswap32(color);
284 // Return a boolean value for a string
285 char parse_bool(char *str)
288 return !ass_strncasecmp(str, "yes", 3) || strtol(str, NULL, 10) > 0;
291 int parse_ycbcr_matrix(char *str)
295 return YCBCR_DEFAULT;
297 char *end = str + strlen(str);
298 rskip_spaces(&end, str);
300 // Trim a local copy of the input that we know is safe to
301 // modify. The buffer is larger than any valid string + NUL,
302 // so we can simply chop off the rest of the input.
304 size_t n = FFMIN(end - str, sizeof buffer - 1);
305 memcpy(buffer, str, n);
308 if (!ass_strcasecmp(buffer, "none"))
310 if (!ass_strcasecmp(buffer, "tv.601"))
311 return YCBCR_BT601_TV;
312 if (!ass_strcasecmp(buffer, "pc.601"))
313 return YCBCR_BT601_PC;
314 if (!ass_strcasecmp(buffer, "tv.709"))
315 return YCBCR_BT709_TV;
316 if (!ass_strcasecmp(buffer, "pc.709"))
317 return YCBCR_BT709_PC;
318 if (!ass_strcasecmp(buffer, "tv.240m"))
319 return YCBCR_SMPTE240M_TV;
320 if (!ass_strcasecmp(buffer, "pc.240m"))
321 return YCBCR_SMPTE240M_PC;
322 if (!ass_strcasecmp(buffer, "tv.fcc"))
324 if (!ass_strcasecmp(buffer, "pc.fcc"))
326 return YCBCR_UNKNOWN;
329 void ass_msg(ASS_Library *priv, int lvl, const char *fmt, ...)
333 priv->msg_callback(lvl, fmt, va, priv->msg_callback_data);
337 unsigned ass_utf8_get_char(char **str)
339 uint8_t *strp = (uint8_t *) * str;
340 unsigned c = *strp++;
341 unsigned mask = 0x80;
347 if (len <= 0 || len > 4)
350 while ((*strp & 0xc0) == 0x80) {
353 c = (c << 6) | (*strp++ & 0x3f);
357 *str = (char *) strp;
361 strp = (uint8_t *) * str;
363 *str = (char *) strp;
368 * Original version from http://www.cprogramming.com/tutorial/utf8.c
369 * \brief Converts a single UTF-32 code point to UTF-8
370 * \param dest Buffer to write to. Writes a NULL terminator.
371 * \param ch 32-bit character code to convert
372 * \return number of bytes written
373 * converts a single character and ASSUMES YOU HAVE ENOUGH SPACE
375 unsigned ass_utf8_put_char(char *dest, uint32_t ch)
377 char *orig_dest = dest;
381 } else if (ch < 0x800) {
382 *dest++ = (ch >> 6) | 0xC0;
383 *dest++ = (ch & 0x3F) | 0x80;
384 } else if (ch < 0x10000) {
385 *dest++ = (ch >> 12) | 0xE0;
386 *dest++ = ((ch >> 6) & 0x3F) | 0x80;
387 *dest++ = (ch & 0x3F) | 0x80;
388 } else if (ch < 0x110000) {
389 *dest++ = (ch >> 18) | 0xF0;
390 *dest++ = ((ch >> 12) & 0x3F) | 0x80;
391 *dest++ = ((ch >> 6) & 0x3F) | 0x80;
392 *dest++ = (ch & 0x3F) | 0x80;
396 return dest - orig_dest;
400 * \brief Parse UTF-16 and return the code point of the sequence starting at src.
401 * \param src pointer to a pointer to the start of the UTF-16 data
402 * (will be set to the start of the next code point)
403 * \return the code point
405 static uint32_t ass_read_utf16be(uint8_t **src, size_t bytes)
410 uint32_t cp = ((*src)[0] << 8) | (*src)[1];
414 if (cp >= 0xD800 && cp <= 0xDBFF) {
418 uint32_t cp2 = ((*src)[0] << 8) | (*src)[1];
420 if (cp2 < 0xDC00 || cp2 > 0xDFFF)
425 cp = 0x10000 + ((cp - 0xD800) << 10) + (cp2 - 0xDC00);
428 if (cp >= 0xDC00 && cp <= 0xDFFF)
438 void ass_utf16be_to_utf8(char *dst, size_t dst_size, uint8_t *src, size_t src_size)
440 uint8_t *end = src + src_size;
446 uint32_t cp = ass_read_utf16be(&src, end - src);
449 unsigned s = ass_utf8_put_char(dst, cp);
458 * \brief find style by name
460 * \param name style name
461 * \return index in track->styles
462 * Returns 0 if no styles found => expects at least 1 style.
463 * Parsing code always adds "Default" style in the beginning.
465 int lookup_style(ASS_Track *track, char *name)
468 // '*' seem to mean literally nothing;
469 // VSFilter removes them as soon as it can
472 // VSFilter then normalizes the case of "Default"
473 // (only in contexts where this function is called)
474 if (ass_strcasecmp(name, "Default") == 0)
476 for (i = track->n_styles - 1; i >= 0; --i) {
477 if (strcmp(track->styles[i].Name, name) == 0)
480 i = track->default_style;
481 ass_msg(track->library, MSGL_WARN,
482 "[%p]: Warning: no style named '%s' found, using '%s'",
483 track, name, track->styles[i].Name);
488 * \brief find style by name as in \r
490 * \param name style name
491 * \param len style name length
492 * \return style in track->styles
493 * Returns NULL if no style has the given name.
495 ASS_Style *lookup_style_strict(ASS_Track *track, char *name, size_t len)
498 for (i = track->n_styles - 1; i >= 0; --i) {
499 if (strncmp(track->styles[i].Name, name, len) == 0 &&
500 track->styles[i].Name[len] == '\0')
501 return track->styles + i;
503 ass_msg(track->library, MSGL_WARN,
504 "[%p]: Warning: no style named '%.*s' found",
505 track, (int) len, name);