annotate libass/ass_utils.c @ 20502:81a92ae66d78

Move ass_library initialization code to ass_mp.c.
author eugeni
date Sun, 29 Oct 2006 15:26:13 +0000
parents 30df9a64618a
children 5cbf1c33a668
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20008
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
2 // vim:ts=8:sw=8:noet:ai:
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
3 /*
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
4 Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
5
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
6 This program is free software; you can redistribute it and/or modify
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
7 it under the terms of the GNU General Public License as published by
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
8 the Free Software Foundation; either version 2 of the License, or
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
9 (at your option) any later version.
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
10
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
11 This program is distributed in the hope that it will be useful,
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
14 GNU General Public License for more details.
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
15
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
16 You should have received a copy of the GNU General Public License
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
17 along with this program; if not, write to the Free Software
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
19 */
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19405
diff changeset
20
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
21 #include "config.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
22
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
23 #include <stdlib.h>
19405
0797e1b4a4be Replace stdint.h with inttypes.h.
eugeni
parents: 19003
diff changeset
24 #include <inttypes.h>
20501
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
25 #include <string.h>
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
26 #include <sys/time.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
27 #include <time.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
28
20501
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
29 #ifdef HAVE_ENCA
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
30 #include <enca.h>
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
31 #endif
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
32
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
33 #include "mp_msg.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
34 #include "ass_utils.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
35
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
36 int mystrtoi(char** p, int base, int* res)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
37 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
38 char* start = *p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
39 *res = strtol(*p, p, base);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
40 if (*p != start) return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
41 else return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
42 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
43
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
44 int mystrtou32(char** p, int base, uint32_t* res)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
45 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
46 char* start = *p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
47 *res = strtoll(*p, p, base);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
48 if (*p != start) return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
49 else return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
50 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
51
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
52 int mystrtod(char** p, double* res)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
53 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
54 char* start = *p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
55 *res = strtod(*p, p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
56 if (*p != start) return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
57 else return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
58 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
59
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
60 int strtocolor(char** q, uint32_t* res)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
61 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
62 uint32_t color = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
63 int result;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
64 char* p = *q;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
65
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
66 if (*p == '&') ++p;
19003
9dc06456af0f Support some recently discovered color formats.
eugeni
parents: 18937
diff changeset
67 else mp_msg(MSGT_GLOBAL, MSGL_DBG2, "suspicious color format: \"%s\"\n", p);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
68
19003
9dc06456af0f Support some recently discovered color formats.
eugeni
parents: 18937
diff changeset
69 if (*p == 'H' || *p == 'h') {
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
70 ++p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
71 result = mystrtou32(&p, 16, &color);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
72 } else {
19003
9dc06456af0f Support some recently discovered color formats.
eugeni
parents: 18937
diff changeset
73 result = mystrtou32(&p, 0, &color);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
74 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
75
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
76 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
77 unsigned char* tmp = (unsigned char*)(&color);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
78 unsigned char b;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
79 b = tmp[0]; tmp[0] = tmp[3]; tmp[3] = b;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
80 b = tmp[1]; tmp[1] = tmp[2]; tmp[2] = b;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
81 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
82 if (*p == '&') ++p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
83 *q = p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
84
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
85 *res = color;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
86 return result;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
87 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
88
20501
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
89 unsigned ass_utf8_get_char(char **str)
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
90 {
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
91 uint8_t *strp = (uint8_t *)*str;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
92 unsigned c = *strp++;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
93 unsigned mask = 0x80;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
94 int len = -1;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
95 while (c & mask) {
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
96 mask >>= 1;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
97 len++;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
98 }
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
99 if (len <= 0 || len > 4)
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
100 goto no_utf8;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
101 c &= mask - 1;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
102 while ((*strp & 0xc0) == 0x80) {
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
103 if (len-- <= 0)
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
104 goto no_utf8;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
105 c = (c << 6) | (*strp++ & 0x3f);
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
106 }
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
107 if (len)
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
108 goto no_utf8;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
109 *str = (char *)strp;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
110 return c;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
111
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
112 no_utf8:
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
113 strp = (uint8_t *)*str;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
114 c = *strp++;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
115 *str = (char *)strp;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
116 return c;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
117 }
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
118
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
119 #ifdef HAVE_ENCA
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
120 void* ass_guess_buffer_cp(unsigned char* buffer, int buflen, char *preferred_language, char *fallback)
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
121 {
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
122 const char **languages;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
123 size_t langcnt;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
124 EncaAnalyser analyser;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
125 EncaEncoding encoding;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
126 char *detected_sub_cp = NULL;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
127 int i;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
128
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
129 languages = enca_get_languages(&langcnt);
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
130 mp_msg(MSGT_SUBREADER, MSGL_V, "ENCA supported languages: ");
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
131 for (i = 0; i < langcnt; i++) {
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
132 mp_msg(MSGT_SUBREADER, MSGL_V, "%s ", languages[i]);
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
133 }
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
134 mp_msg(MSGT_SUBREADER, MSGL_V, "\n");
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
135
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
136 for (i = 0; i < langcnt; i++) {
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
137 const char *tmp;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
138
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
139 if (strcasecmp(languages[i], preferred_language) != 0) continue;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
140 analyser = enca_analyser_alloc(languages[i]);
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
141 encoding = enca_analyse_const(analyser, buffer, buflen);
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
142 tmp = enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV);
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
143 if (tmp && encoding.charset != ENCA_CS_UNKNOWN) {
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
144 detected_sub_cp = strdup(tmp);
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
145 mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detected charset: %s\n", tmp);
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
146 }
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
147 enca_analyser_free(analyser);
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
148 }
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
149
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
150 free(languages);
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
151
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
152 if (!detected_sub_cp) {
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
153 detected_sub_cp = strdup(fallback);
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
154 mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detection failed: fallback to %s\n", fallback);
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
155 }
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
156
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
157 return detected_sub_cp;
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
158 }
30df9a64618a Copy the following functions to libass to avoid dependency on the rest of mplayer:
eugeni
parents: 20008
diff changeset
159 #endif