summaryrefslogtreecommitdiffstats
path: root/libass/ass_utils.h
Commit message (Collapse)AuthorAgeFilesLines
* Switch to a new CPU flag infrastructureDr.Smile2022-12-041-4/+0
| | | | | This opens a possibility to selectively disable particular implementations at runtime.
* refactor: prefix all internal API with ass_Oneric2022-10-221-1/+1
| | | | | | | | | | | | | | | If static libass is linked into a binary defining functions of the same name there will be issues. To avoid this use an ass_ prefix for namespacing. Before this commit we already did this for most but not yet all internal API. read_file is renamed to ass_load_file as ass_read_file already exists as a public API function. All other functions are simply prefixed with ass_. Fixes: https://github.com/libass/libass/issues/222 Fixes: https://github.com/libass/libass/issues/654
* refactor/utils: turn some functions into static inlinesOneric2022-10-221-3/+39
| | | | They are used in multiple files but short enough to inline.
* refactor: move and static'fy some internal functionsOneric2022-10-221-7/+0
| | | | | | Although declared and defined in ass_utils.{h,c}, those functions are only used in one other file and aren't useful at other places.
* Accept hexadecimal for all non-inline int valuesOneric2022-10-141-0/+1
| | | | | | | | | | | | | | | | | | | VSFilter uses the same GetInt method used for colour values also for all other integer headers and Style or Dialogue integer fields. This method accepts hexadecimal with a case-insensitive '&H' or '0x' prefix. GetInt is practically unchanged from its initial form in 2003's revision 8 of guliverkli to today's xy*. (MPC-HC'S ISR changed it more but it still behaves the same wrt to hex) The previously used atoi never accepts hexadecimal input and is now only used for the ReadOrder pseudo-field which isn't parsed by VSFilter. This relies on uint32_t <-> int32_t conversions just keeping the bitpattern unchanged (as do many other places in libass) and mystrtou32_modulo taking sign prefixes into account rather than rejecting non-positive input.
* render: avoid UB on left shiftsOneric2022-04-261-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The two shifts could be used with too large or negative shift operands which is undefined behaviour. To avoid UB but keep VSFilter compatibility, simulate the practical effects of such shifts on x86 Windows instead of directly using a simple shift. The simulated behaviour matches MSYS2's gcc. Clang, gcc and tcc were observed to exhibit different behaviour on Linux, iff the operand was a compile-time constant (they would use an arithmetic right shift instead). Ideally this should be checked against MSVC, but it seems unlikely to deviate since the related x86 instruction SHL/SAL is documented to mask the upper three bits of the count operand. The > 0 check is not present in VSFilter, but we'd like to avoid negative scales and the only possible negative value is INT32_MIN i.e. -2^31. Luckily using zero instead of the (font_scale / INT32_MIN) won't make much difference here. In guliverkli2 parsed drawing coordinates are also int32_t in canvas space and only later multiplied by (64 * m_scale{x,y}) where m_scale combines canvas and drawing scale and is a double. The result is then cast back to int32_t, meaning coordinates absolutley larger than or equal to min(2^(25 + draw_scale_exp) / canvas_scale{x,y}, INT32_MAX) will be turned into INT32_MIN. For the \p32 case this means any drawing without overflow will be at most 2×2 canvas units large. If only some coordinates overflow the result can cover more. In xy-VSFilter drawing coordinates are floating point numbers and parsed as 64-bit IEEE floats, then multiplied by 64 and cast to int32_t. Here a truncation already happens before the scaling values are applied, so any coordinate larger than 2^25 turn into INT32_MIN during initial parsing. For the \p32 this means a non-overflowing drawing is at most 1/32×1/32 canvas units large. As a consequence of this parsing difference, drawing coordinates larger than 2^25 are non-portable showing different behaviour in guliverkli2- and xy-VSFilter. Thus treating \p32 as an effective \fscx0 seems unproblematic, since even when scaling the default height of 288 up to a UHD frame size, 1/32×1/32 canvas units don't extend beyond 1/4 pixel, which unless stacked won't be too noticeable. The remaining left shifts in ass_render.c, should all be safe as they shift a constant 1 and use macros defined by us as the shift operands and blur_radius which is limited to 100. Found by AFL++ and UBSAN. Samples assert-fail+shift9999999_fuzzer_w3:id:000159,sig:04,src:000968+000719,time:452375,execs:57769,op:splice,rep:16 shift125_id:000001,sig:04,src:000008+000014,time:526595,execs:18418,op:splice,rep:8 shift-neg_id:000000,sig:04,src:000008+000014,time:466566,execs:18013,op:splice,rep:16
* parse: replace argtoi with argtoi32Oneric2022-04-261-8/+0
| | | | | | | | | | | | | | | This continues our transition towards fixed-width types to improve VSFilter compatibility regardless of the platform libass is compiled for. Local variables are also switched to int32_t, but struct members are left as is for now. By dropping mystrtoi, which was only used by argtoi, we also fix float-cast-overflow issues in it identified by UBSAN in libass' public OSS-Fuzz corpus. mystroi32 does not suffer from this problem. Sample ID (one instance there are duplicates): OSSFuzz-123cf9a553c5745854037a52e87947721257f1f3fb
* utils: drop unused functionOneric2022-04-171-8/+0
| | | | mystrtoll was not used since 7913e4a64c704a3b82719d70920b5b153b43d254
* asm/x86: check highest supported leaf for cpuidOneric2022-03-171-3/+1
| | | | | | | | If a higher then supported leaf is requested, at least Intel processors will silently return the data of the highest supported leaf, invalidating the following feature tests. Reported in: https://github.com/libass/libass/pull/603
* Consistently use x86 asm code if we've built itOleg Oshmyan2022-02-121-1/+1
| | | | | | | | | | | We detect x86 and enable building asm code in `configure`. However, before this commit, we don't actually use that code unless we detect x86 via the C compiler's predefined macros. We could check more macros to support more C compilers, but what we really want is to use the same trigger for both building and using this code. To that end, add ARCH_X86 to config.h iff x86 asm is being built.
* Refactor track features to bitflagsOneric2021-12-141-0/+2
| | | | | | This is purely an internal refactor. With the existing and currently planned boolean features, a bit flag will scale better.
* Move the cache function to ass_cache.cOneric2021-10-151-22/+0
|
* Use gnu_printf format attribute to avoid warnings on MinGWOleg Oshmyan2021-07-101-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | We use %zu format specifiers to log size_t values. However, __attribute__((format(printf, ...))) in modern GCC means "system's default" printf, which on MinGW means MSVCRT.DLL printf, which does not support the z length modifier (even on Windows 10), so GCC warns that our format strings are invalid. Older GCC is unable to check Microsoft's format strings at all and has only format(printf), which is equivalent to the newer gnu_printf. Clang also lacks gnu_printf, but it is covered by the same macro check because its __GNUC...__ macros always report version 4.2.1. On non-Microsoft platforms, printf and gnu_printf are currently aliases, so the __MINGW32__ check is redundant. However, with some luck, GCC may start to check other platforms' printf formats more carefully in the future; and we would like to receive warnings if our format strings don't work on some platform, although we intend to stick to standard C99 format strings. Indeed, if we use an extension by accident, this might help us catch it. And even if we make no mistake but there is another platform that fails to support C99 format strings, this might warn some poor soul building on that platform that their system printf doesn't understand our log format strings, so they will know they need to work around it in their log callback or to patch libass.
* ass_utils: add format-string attr to ass_msgrcombs2021-05-311-0/+3
| | | | Allows compilers to print warnings when the args we pass don't match the string
* Replace M_PIOneric2021-03-281-0/+2
| | | | | | M_PI is not part of ISO C and also not part of base POSIX, but an XSI extension. For simplicity, just always use our own macro for PI. The value is identical to the one used in glibc and musl.
* Add strdup fallbackOneric2021-03-281-5/+0
| | | | | | | | And move fallback declarations to ass_compat.h As ass_compat.h is already included in every source file we no longer need to include _both_ string.h and ass_utils.h to use str(n)dup. Definitions are still in ass_utils.c since a separate source file just for two functions seemed overkill.
* Fix crash on empty stringsDr.Smile2021-02-241-0/+3
| | | | | | Our hash function doesn't handle zero-length buffers, but it was not a problem until 82b225b3d6653091d028b39d561d185ed76a7be5. Found by OSS-Fuzz.
* parsing: use string references for font family and drawing textDr.Smile2021-02-221-11/+22
| | | | That eliminates most uses of strdup() in the rendering process.
* ass_utils: round double_to_d* properlyOleg Oshmyan2020-10-291-3/+3
|
* Remove unused rot_key functionOleg Oshmyan2020-10-271-6/+0
| | | | Unused since commit c80f332798238731e1ddf1b541748f3d5c8030f3.
* ass_utils: make ass_strtod/strtoll wrappers inlineablercombs2020-08-301-4/+31
| | | | | Also makes a measurable difference on float-parse-heavy scripts, and it's not like these had a reason to not be in the header.
* Supress -Wimplicit-fallthrough warnings for false positivesOneric2020-07-051-3/+3
|
* cache: cleanupDr.Smile2019-05-191-8/+8
|
* Reuse numpad2align in parse_tagOleg Oshmyan2017-02-141-0/+1
|
* bitmap: use calloc instead of malloc/memsetRodger Combs2016-09-241-1/+2
| | | | This can improve perf somewhat with large bitmaps
* fontselect: don't trim font namesOleg Oshmyan2015-10-231-2/+0
| | | | | | | | | | | | This matches the behavior of GDI and hence VSFilter. Note that \fn arguments are trimmed during parsing. However, none of the names inside fonts should be trimmed, and @-prefixed fonts should keep whitespace following the @, both of which this commit addresses. Remove strdup_trimmed because it is no longer used. Also remove the declaration of a function that was deleted a few months ago.
* Remove ENCA supportGrigori Goronzy2015-09-231-9/+0
| | | | | | | | | | | There is no real value in it. ENCA only works in some cases and I can't find any information about software that integrates libass which relies on it or users that rely on ENCA support in case the interface is exposed directly (e.g. in MPlayer). If there is still a wish to have charset detection integrated into libass (it is out of scope, IMHO), a better library like uchardet should be used for that.
* Merge pull request #84 from astiob/msvcGrigori Goronzy2015-09-221-2/+0
|\ | | | | MSVC/ICL and general header fixes
| * Include config.h in all source files and in no headersOleg Oshmyan2015-09-171-2/+0
| | | | | | | | | | | | | | | | | | This way, #include "config.h" is consistently the very first thing the compiler sees when compiling any file. Some source files currently don't use anything defined in config.h, but it's easier and less error-prone to include it now to anticipate possible future changes in those files, config.h or other headers.
* | fontselect: make iconv optional againwm42015-09-211-0/+1
|/ | | | It was needed for UTF16BE -> UTF8 only, which is trivial to implement.
* Constify ass_msg format (NFC)Grigori Goronzy2015-07-101-1/+1
|
* fontselect: add and use strdup_trimmedGrigori Goronzy2015-07-101-0/+1
| | | | This is just a cleaner and safer interface for string trimming.
* directwrite: add initial fontselect implementationfeliwir2015-07-101-1/+1
| | | | Incomplete, leaks memory, but capable of rendering something.
* Trim spaces of font family stringsGrigori Goronzy2015-07-101-0/+1
| | | | | This adds a trimming utility function that is used for trimming strings of font requests in the font sorter.
* Parse and animate all colors and alpha values like VSFilterOleg Oshmyan2015-05-251-2/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | * Allow exactly one of these prefixes in header values: 0x, 0X, &h, &H. Note that "0x0xFFFFFF" is a correct value, as the first 0x is consumed by the parser and the second by the string-to-number conversion following strtol semantics. * Allow arbitrary numbers of leading & and H (and not h) in any order in override tag values. * Reduce header values modulo 2**32 instead of saturating them to LLONG_MIN/MAX. * Saturate override tag values to INT32_MIN/MAX rather than to LLONG_MIN/MAX. * Don't fiddle with bytes in alpha override tag values. (They can be outside of the 0..255 range.) Also change the byte swapping code to be more sensible. Fixes #80. Fixes #145. Fixes #178. Also fixes our behavior in the case described in https://code.google.com/p/xy-vsfilter/issues/detail?id=80.
* Fix a wrong commentOleg Oshmyan2015-05-231-1/+1
|
* Fix range in rot_keyRodger Combs2015-03-121-1/+1
| | | | Oops, trig is hard. We output from -pi to +pi now.
* Fix degrees/radians cache confusion; avoid a fixed-point overflowRodger Combs2015-03-121-2/+2
| | | | Also fix an incorrect comment
* Provide a strndup() fallbackwm42014-11-171-0/+7
| | | | | This standard function is not available everywhere, so we detect it and provide a fallback if missing.
* Check bitmap allocation for overflowswm42014-11-161-0/+2
| | | | | | | | This actually fixes #146. The overflow check itself is obvious. Also, make ass_align() return an unaligned value if aligning it would overflow. This is probably better, as it for example makes the overflow check in the caller simpler.
* Add another helper-macro for array allocationwm42014-11-141-0/+15
|
* Add ass_realloc_array()wm42014-11-111-0/+2
| | | | Helps with overflow and allocation failure checking.
* Provide SIZE_MAX fallbackwm42014-11-091-0/+4
| | | | | SIZE_MAX is in C99, but not in C89. It is in <stdint.h>, and is always a macro, so this fallback should be pretty portable and cause no issues.
* Fix UB at left shifts of negative integersDr.Smile2014-11-071-3/+3
|
* parse_tag: remove unnecessary mallocOleg Oshmyan2014-06-061-1/+1
|
* Parse override tag arguments exactly like VSFilter 2.38Oleg Oshmyan2014-06-061-1/+1
| | | | | Also replace strtocolor in ass_utils with string2color from ass.c, because that is more useful everywhere now.
* Move (r)skip_spaces to ass_utilsOleg Oshmyan2014-06-061-0/+2
|
* Use a function for aligned memory allocationswm42014-02-021-0/+8
| | | | ...instead of doing this manually.
* Unroll FNV-1A hash functionGrigori Goronzy2014-01-291-4/+9
| | | | Unroll the hash function with Duff's device for improved performance.
* Added x86 ASM functions11rcombs2014-01-251-0/+6
|
* Combine bitmaps before applying blur and shadow11rcombs2014-01-251-0/+1
|
* Simplify hash function11rcombs2014-01-251-7/+4
| | | | | | | | | We can rely on fast multiplication and good compilers. v2: use default FNV-1a prime Signed-off-by: wm4 <wm4@nowhere> Signed-off-by: Grigori Goronzy <greg@chown.ath.cx>
* Fix \r style lookupOleg Oshmyan2013-06-221-0/+1
| | | | | Make \rSTYLENAME with an invalid STYLENAME fall back to line style rather than to Default. This fixes issue #104.
* Support reading the YCbCr Matrix headerOleg Oshmyan2013-03-031-0/+1
| | | | | | | | | | | | The value is parsed and stored as an enum constant that the consumer can read from ASS_Track. All output images are still plain RGB, and the consumer is expected to perform its own color correction. Supported header values: (TV|PC).(601|709|240M|FCC) and None. If the header is missing, a special compatibility value is used that should be treated as TV.601 if the accompanying video stream is YCbCr and as None otherwise. If the header is present but has an invalid/unknown value, a different special value is substituted.
* Support \rSTYLENAME syntaxGrigori Goronzy2012-03-111-0/+1
| | | | | This allows to reset to a certain style, instead of the default style for the current line. For some reason, this was completely missing.
* Relicense to ISCGrigori Goronzy2010-09-281-12/+10
| | | | | | In hope to make libass as useful as possible, relicense libass to ISC, a simplified 2-clause BSD license. All contributors who provided non-trivial changes have granted their permission for this.
* Improve rotation cache accuracyGrigori Goronzy2010-01-071-0/+15
| | | | | | In some cases 16.16 precision is not good enough. Instead use 10.22 and use modulo 360.0 on the angles to make overflows impossible and improve cache hit ratio sometimes.
* Replace strtod with locale-independent strtodGrigori Goronzy2010-01-051-0/+3
| | | | | | | | | strtod respects the locale and in some locales, the decimal separator is not a point, leading to parsing errors in tags like \pos(23.4,5), which are perfectly valid. As there isn't a really portable way to use a particular locale just for one call to strtod, reimplement it. The implementation was taken from the 1.8 branch of Ruby.
* Always parse colors as hex for ASS tracksGrigori Goronzy2009-08-121-1/+1
| | | | | | According to the ASS specification, colors can only be specified in hex. Modify the color parsing accordingly; this especially means that colors where the hex sigil (the "H") is missing can now be parsed.
* Rename typedefs (breaks API)Grigori Goronzy2009-08-061-3/+3
| | | | | | Rename all typedefs from the convention foo_bar_t, which can possibly conflict with POSIX types, to FooBar (and ASS_FooBar for public API typedefs). Fix formatting and stray comments while at it.
* Clamp clip rectangle to frame sizeGrigori Goronzy2009-07-211-0/+1
| | | | | Make sure the clip rectangle is inside the screen boundaries. Idea by Evgeniy Stepanov.
* Message callback funtionalityGrigori Goronzy2009-07-111-4/+9
| | | | | | | | | | | Introduce functionality for providing a message callback that is used for passing messages to the controlling application instead of simply printing them to standard output. The function pointer to the callback is stored in the ass_library_t instance. ass_msg needs access to it, so in many places the library instance needs to be passed around now. The default behavior is the old one: messages of MSGL_INFO or lower are printed to the standard output, prefixed with "[ass]".
* Replace string defines with real stringsGrigori Goronzy2009-07-111-2/+0
| | | | | | Instead of referencing string defines from help_mp.h, use the strings directly in ass_msg. Consequently, help_mp.h is useless and can be deleted.
* Move gaussian blur into bitmap handling codeGrigori Goronzy2009-07-101-3/+0
|
* Implement drawing mode (\p)Grigori Goronzy2009-07-061-0/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Finally implement the drawing mode, which allows drawing of custom vector graphics. Drawings are intercepted in ass_render_event; a hash of the drawing string is generated which is then used for looking up drawings and bitmaps of drawings in the cache. The drawings itself are "fake" glyphs. They are created by parsing the simple drawing description language, evaluating the curves described (lines, cubic beziers and/or a special kind of b-splines) and creating vector outlines. Afterwards, these drawings are (with a few exceptions, e.g. ascender/descender) exactly handled like regular glyphs. Support for vector clippings is still missing, but otherwise the implementation should be complete and compatible with VSFilter. The libass integration of the drawing parsing/processing code is still a bit sketchy and should be refactored. History: WIP: Drawing mode inf