Mercurial > mplayer.hg
annotate libass/ass_utils.c @ 37130:1b073d52d748
configure: add config_blockdsp to prevent ffmpeg build failure
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
author | michael |
---|---|
date | Thu, 19 Jun 2014 03:14:03 +0000 |
parents | c3aaaf17c721 |
children |
rev | line source |
---|---|
20008
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19405
diff
changeset
|
1 /* |
26723 | 2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> |
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 | 5 * |
34011 | 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. | |
26723 | 9 * |
34011 | 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. | |
26723 | 17 */ |
20008
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19405
diff
changeset
|
18 |
18937 | 19 #include "config.h" |
20 | |
21 #include <stdlib.h> | |
30200 | 22 #include <stdio.h> |
19405 | 23 #include <inttypes.h> |
26034 | 24 #include <ft2build.h> |
25 #include FT_GLYPH_H | |
31875 | 26 #include <strings.h> |
18937 | 27 |
30200 | 28 #include "ass_library.h" |
29 #include "ass.h" | |
18937 | 30 #include "ass_utils.h" |
31 | |
30200 | 32 int mystrtoi(char **p, int *res) |
18937 | 33 { |
30200 | 34 double temp_res; |
35 char *start = *p; | |
36 temp_res = ass_strtod(*p, p); | |
37 *res = (int) (temp_res + (temp_res > 0 ? 0.5 : -0.5)); | |
38 if (*p != start) | |
39 return 1; | |
40 else | |
41 return 0; | |
28717
7bbe6626f0e0
Support fractional arguments for some override tags.
eugeni
parents:
26738
diff
changeset
|
42 } |
7bbe6626f0e0
Support fractional arguments for some override tags.
eugeni
parents:
26738
diff
changeset
|
43 |
30200 | 44 int mystrtoll(char **p, long long *res) |
28717
7bbe6626f0e0
Support fractional arguments for some override tags.
eugeni
parents:
26738
diff
changeset
|
45 { |
30200 | 46 double temp_res; |
47 char *start = *p; | |
48 temp_res = ass_strtod(*p, p); | |
49 *res = (int) (temp_res + (temp_res > 0 ? 0.5 : -0.5)); | |
50 if (*p != start) | |
51 return 1; | |
52 else | |
53 return 0; | |
18937 | 54 } |
55 | |
30200 | 56 int mystrtou32(char **p, int base, uint32_t *res) |
18937 | 57 { |
30200 | 58 char *start = *p; |
59 *res = strtoll(*p, p, base); | |
60 if (*p != start) | |
61 return 1; | |
62 else | |
63 return 0; | |
18937 | 64 } |
65 | |
30200 | 66 int mystrtod(char **p, double *res) |
18937 | 67 { |
30200 | 68 char *start = *p; |
69 *res = ass_strtod(*p, p); | |
70 if (*p != start) | |
71 return 1; | |
72 else | |
73 return 0; | |
18937 | 74 } |
75 | |
30200 | 76 int strtocolor(ASS_Library *library, char **q, uint32_t *res, int hex) |
18937 | 77 { |
30200 | 78 uint32_t color = 0; |
79 int result; | |
80 char *p = *q; | |
81 int base = hex ? 16 : 10; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28785
diff
changeset
|
82 |
30200 | 83 if (*p == '&') |
84 ++p; | |
85 else | |
86 ass_msg(library, MSGL_DBG2, "suspicious color format: \"%s\"\n", p); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28785
diff
changeset
|
87 |
30200 | 88 if (*p == 'H' || *p == 'h') { |
89 ++p; | |
90 result = mystrtou32(&p, 16, &color); | |
91 } else { | |
92 result = mystrtou32(&p, base, &color); | |
93 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28785
diff
changeset
|
94 |
30200 | 95 { |
96 unsigned char *tmp = (unsigned char *) (&color); | |
97 unsigned char b; | |
98 b = tmp[0]; | |
99 tmp[0] = tmp[3]; | |
100 tmp[3] = b; | |
101 b = tmp[1]; | |
102 tmp[1] = tmp[2]; | |
103 tmp[2] = b; | |
104 } | |
105 if (*p == '&') | |
106 ++p; | |
107 *q = p; | |
18937 | 108 |
30200 | 109 *res = color; |
110 return result; | |
18937 | 111 } |
112 | |
28785 | 113 // Return a boolean value for a string |
30200 | 114 char parse_bool(char *str) |
115 { | |
116 while (*str == ' ' || *str == '\t') | |
117 str++; | |
118 if (!strncasecmp(str, "yes", 3)) | |
119 return 1; | |
120 else if (strtol(str, NULL, 10) > 0) | |
121 return 1; | |
122 return 0; | |
123 } | |
124 | |
36363 | 125 int parse_ycbcr_matrix(char *str) |
126 { | |
127 while (*str == ' ' || *str == '\t') | |
128 str++; | |
129 if (*str == '\0') | |
130 return YCBCR_DEFAULT; | |
131 | |
132 char *end = str + strlen(str); | |
133 while (end[-1] == ' ' || end[-1] == '\t') | |
134 end--; | |
135 | |
136 // Trim a local copy of the input that we know is safe to | |
137 // modify. The buffer is larger than any valid string + NUL, | |
138 // so we can simply chop off the rest of the input. | |
139 char buffer[16]; | |
140 size_t n = FFMIN(end - str, sizeof buffer - 1); | |
141 strncpy(buffer, str, n); | |
142 buffer[n] = '\0'; | |
143 | |
144 if (!strcasecmp(buffer, "none")) | |
145 return YCBCR_NONE; | |
146 if (!strcasecmp(buffer, "tv.601")) | |
147 return YCBCR_BT601_TV; | |
148 if (!strcasecmp(buffer, "pc.601")) | |
149 return YCBCR_BT601_PC; | |
150 if (!strcasecmp(buffer, "tv.709")) | |
151 return YCBCR_BT709_TV; | |
152 if (!strcasecmp(buffer, "pc.709")) | |
153 return YCBCR_BT709_PC; | |
154 if (!strcasecmp(buffer, "tv.240m")) | |
155 return YCBCR_SMPTE240M_TV; | |
156 if (!strcasecmp(buffer, "pc.240m")) | |
157 return YCBCR_SMPTE240M_PC; | |
158 if (!strcasecmp(buffer, "tv.fcc")) | |
159 return YCBCR_FCC_TV; | |
160 if (!strcasecmp(buffer, "pc.fcc")) | |
161 return YCBCR_FCC_PC; | |
162 return YCBCR_UNKNOWN; | |
163 } | |
164 | |
30200 | 165 void ass_msg(ASS_Library *priv, int lvl, char *fmt, ...) |
166 { | |
167 va_list va; | |
168 va_start(va, fmt); | |
169 priv->msg_callback(lvl, fmt, va, priv->msg_callback_data); | |
170 va_end(va); | |
28785 | 171 } |
172 | |
30200 | 173 unsigned ass_utf8_get_char(char **str) |
26034 | 174 { |
30200 | 175 uint8_t *strp = (uint8_t *) * str; |
176 unsigned c = *strp++; | |
177 unsigned mask = 0x80; | |
178 int len = -1; | |
179 while (c & mask) { | |
180 mask >>= 1; | |
181 len++; | |
182 } | |
183 if (len <= 0 || len > 4) | |
184 goto no_utf8; | |
185 c &= mask - 1; | |
186 while ((*strp & 0xc0) == 0x80) { | |
187 if (len-- <= 0) | |
188 goto no_utf8; | |
189 c = (c << 6) | (*strp++ & 0x3f); | |
190 } | |
191 if (len) | |
192 goto no_utf8; | |
193 *str = (char *) strp; | |
194 return c; | |
195 | |
196 no_utf8: | |
197 strp = (uint8_t *) * str; | |
198 c = *strp++; | |
199 *str = (char *) strp; | |
200 return c; | |
26034 | 201 } |
202 | |
35262 | 203 /** |
204 * \brief find style by name | |
205 * \param track track | |
206 * \param name style name | |
207 * \return index in track->styles | |
208 * Returnes 0 if no styles found => expects at least 1 style. | |
209 * Parsing code always adds "Default" style in the end. | |
210 */ | |
211 int lookup_style(ASS_Track *track, char *name) | |
212 { | |
213 int i; | |
214 if (*name == '*') | |
215 ++name; // FIXME: what does '*' really mean ? | |
216 for (i = track->n_styles - 1; i >= 0; --i) { | |
217 if (strcmp(track->styles[i].Name, name) == 0) | |
218 return i; | |
219 } | |
220 i = track->default_style; | |
221 ass_msg(track->library, MSGL_WARN, | |
222 "[%p]: Warning: no style named '%s' found, using '%s'", | |
223 track, name, track->styles[i].Name); | |
224 return i; // use the first style | |
225 } | |
226 | |
30200 | 227 #ifdef CONFIG_ENCA |
228 void *ass_guess_buffer_cp(ASS_Library *library, unsigned char *buffer, | |
229 int buflen, char *preferred_language, | |
230 char *fallback) | |
26034 | 231 { |
30200 | 232 const char **languages; |
233 size_t langcnt; | |
234 EncaAnalyser analyser; | |
235 EncaEncoding encoding; | |
236 char *detected_sub_cp = NULL; | |
237 int i; | |
238 | |
239 languages = enca_get_languages(&langcnt); | |
240 ass_msg(library, MSGL_V, "ENCA supported languages"); | |
241 for (i = 0; i < langcnt; i++) { | |
242 ass_msg(library, MSGL_V, "lang %s", languages[i]); | |
243 } | |
244 | |
245 for (i = 0; i < langcnt; i++) { | |
246 const char *tmp; | |
247 | |
248 if (strcasecmp(languages[i], preferred_language) != 0) | |
249 continue; | |
250 analyser = enca_analyser_alloc(languages[i]); | |
251 encoding = enca_analyse_const(analyser, buffer, buflen); | |
252 tmp = enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV); | |
253 if (tmp && encoding.charset != ENCA_CS_UNKNOWN) { | |
254 detected_sub_cp = strdup(tmp); | |
255 ass_msg(library, MSGL_INFO, "ENCA detected charset: %s", tmp); | |
256 } | |
257 enca_analyser_free(analyser); | |
258 } | |
259 | |
260 free(languages); | |
261 | |
262 if (!detected_sub_cp) { | |
263 detected_sub_cp = strdup(fallback); | |
264 ass_msg(library, MSGL_INFO, | |
265 "ENCA detection failed: fallback to %s", fallback); | |
266 } | |
267 | |
268 return detected_sub_cp; | |
26034 | 269 } |
26036
8d8c52a169ad
Comment out dump_glyph(): it is unused and, as it is now, breaks compilation.
eugeni
parents:
26034
diff
changeset
|
270 #endif |