annotate libass/ass.c @ 30035:e05b7683063c

Run vidix/pci_db2c.awk directly instead of passing it to awk.
author diego
date Sun, 20 Dec 2009 14:14:56 +0000
parents ce53e67e8633
children 73a32da1ef18
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: 19905
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: 19905
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: 19905
diff changeset
3 /*
26723
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
4 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
5 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
6 * This file is part of libass.
26723
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
7 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
8 * libass is free software; you can redistribute it and/or modify
26723
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
9 * it under the terms of the GNU General Public License as published by
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
11 * (at your option) any later version.
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
12 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
13 * libass is distributed in the hope that it will be useful,
26723
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
16 * GNU General Public License for more details.
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
17 *
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
18 * You should have received a copy of the GNU General Public License along
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
19 * with libass; if not, write to the Free Software Foundation, Inc.,
26723
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
21 */
20008
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19905
diff changeset
22
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
23 #include "config.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
24
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
25 #include <stdio.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
26 #include <stdlib.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
27 #include <string.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
28 #include <assert.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
29 #include <errno.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
30 #include <sys/types.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
31 #include <sys/stat.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
32 #include <unistd.h>
19374
acb26d9717da MinGW treats %lld as %d. Replace it with PRId64.
eugeni
parents: 18937
diff changeset
33 #include <inttypes.h>
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
34
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
35 #ifdef CONFIG_ICONV
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
36 #include <iconv.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
37 #endif
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
38
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
39 #include "ass.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
40 #include "ass_utils.h"
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
41 #include "ass_library.h"
21026
d138463e820b Collect all includes of mplayer headers in libass in a single file (mputils.h).
eugeni
parents: 20698
diff changeset
42 #include "mputils.h"
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
43
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
44 typedef enum {PST_UNKNOWN = 0, PST_INFO, PST_STYLES, PST_EVENTS, PST_FONTS} parser_state_t;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
45
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
46 struct parser_priv_s {
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
47 parser_state_t state;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
48 char* fontname;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
49 char* fontdata;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
50 int fontdata_size;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
51 int fontdata_used;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
52 };
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
53
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
54 #define ASS_STYLES_ALLOC 20
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
55 #define ASS_EVENTS_ALLOC 200
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
56
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
57 void ass_free_track(ass_track_t* track) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
58 int i;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
59
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
60 if (track->parser_priv) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
61 if (track->parser_priv->fontname)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
62 free(track->parser_priv->fontname);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
63 if (track->parser_priv->fontdata)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
64 free(track->parser_priv->fontdata);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
65 free(track->parser_priv);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
66 }
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
67 if (track->style_format)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
68 free(track->style_format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
69 if (track->event_format)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
70 free(track->event_format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
71 if (track->styles) {
19474
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
72 for (i = 0; i < track->n_styles; ++i)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
73 ass_free_style(track, i);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
74 free(track->styles);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
75 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
76 if (track->events) {
19474
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
77 for (i = 0; i < track->n_events; ++i)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
78 ass_free_event(track, i);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
79 free(track->events);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
80 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
81 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
82
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
83 /// \brief Allocate a new style struct
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
84 /// \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
85 /// \return style id
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
86 int ass_alloc_style(ass_track_t* track) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
87 int sid;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
88
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
89 assert(track->n_styles <= track->max_styles);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
90
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
91 if (track->n_styles == track->max_styles) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
92 track->max_styles += ASS_STYLES_ALLOC;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
93 track->styles = (ass_style_t*)realloc(track->styles, sizeof(ass_style_t)*track->max_styles);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
94 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
95
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
96 sid = track->n_styles++;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
97 memset(track->styles + sid, 0, sizeof(ass_style_t));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
98 return sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
99 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
100
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
101 /// \brief Allocate a new event struct
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
102 /// \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
103 /// \return event id
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
104 int ass_alloc_event(ass_track_t* track) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
105 int eid;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
106
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
107 assert(track->n_events <= track->max_events);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
108
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
109 if (track->n_events == track->max_events) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
110 track->max_events += ASS_EVENTS_ALLOC;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
111 track->events = (ass_event_t*)realloc(track->events, sizeof(ass_event_t)*track->max_events);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
112 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
113
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
114 eid = track->n_events++;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
115 memset(track->events + eid, 0, sizeof(ass_event_t));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
116 return eid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
117 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
118
19474
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
119 void ass_free_event(ass_track_t* track, int eid) {
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
120 ass_event_t* event = track->events + eid;
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
121 if (event->Name)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
122 free(event->Name);
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
123 if (event->Effect)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
124 free(event->Effect);
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
125 if (event->Text)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
126 free(event->Text);
19638
a3473d990fed Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents: 19635
diff changeset
127 if (event->render_priv)
a3473d990fed Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents: 19635
diff changeset
128 free(event->render_priv);
19474
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
129 }
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
130
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
131 void ass_free_style(ass_track_t* track, int sid) {
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
132 ass_style_t* style = track->styles + sid;
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
133 if (style->Name)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
134 free(style->Name);
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
135 if (style->FontName)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
136 free(style->FontName);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
137 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
138
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
139 // ==============================================================================================
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
140
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
141 static void skip_spaces(char** str) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
142 char* p = *str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
143 while ((*p==' ') || (*p=='\t'))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
144 ++p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
145 *str = p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
146 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
147
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
148 static void rskip_spaces(char** str, char* limit) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
149 char* p = *str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
150 while ((p >= limit) && ((*p==' ') || (*p=='\t')))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
151 --p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
152 *str = p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
153 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
154
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
155 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
156 * \brief find style by name
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
157 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
158 * \param name style name
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
159 * \return index in track->styles
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
160 * Returnes 0 if no styles found => expects at least 1 style.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
161 * Parsing code always adds "Default" style in the end.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
162 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
163 static int lookup_style(ass_track_t* track, char* name) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
164 int i;
19567
1a03b2c8ac28 Ignore '*' at the beginning of style name.
eugeni
parents: 19495
diff changeset
165 if (*name == '*') ++name; // FIXME: what does '*' really mean ?
26807
1b34360d41de In case 2 styles have the same name, prefer the latest one.
eugeni
parents: 26738
diff changeset
166 for (i = track->n_styles - 1; i >= 0; --i) {
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
167 // FIXME: mb strcasecmp ?
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
168 if (strcmp(track->styles[i].Name, name) == 0)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
169 return i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
170 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
171 i = track->default_style;
21066
6196ba31e97e MSGTRs for libass
kraymer
parents: 21026
diff changeset
172 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoStyleNamedXFoundUsingY, track, name, track->styles[i].Name);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
173 return i; // use the first style
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
174 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
175
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
176 static uint32_t string2color(char* p) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
177 uint32_t tmp;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
178 (void)strtocolor(&p, &tmp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
179 return tmp;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
180 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
181
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
182 static long long string2timecode(char* p) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
183 unsigned h, m, s, ms;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
184 long long tm;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
185 int res = sscanf(p, "%1d:%2d:%2d.%2d", &h, &m, &s, &ms);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
186 if (res < 4) {
21066
6196ba31e97e MSGTRs for libass
kraymer
parents: 21026
diff changeset
187 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_BadTimestamp);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
188 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
189 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
190 tm = ((h * 60 + m) * 60 + s) * 1000 + ms * 10;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
191 return tm;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
192 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
193
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
194 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
195 * \brief converts numpad-style align to align.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
196 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
197 static int numpad2align(int val) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
198 int res, v;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
199 v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
200 if (v != 0) v = 3 - v;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
201 res = ((val - 1) % 3) + 1; // horizontal alignment
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
202 res += v*4;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
203 return res;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
204 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
205
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
206 #define NEXT(str,token) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
207 token = next_token(&str); \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
208 if (!token) break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
209
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
210 #define ANYVAL(name,func) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
211 } else if (strcasecmp(tname, #name) == 0) { \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
212 target->name = func(token); \
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
213 mp_msg(MSGT_ASS, MSGL_DBG2, "%s = %s\n", #name, token);
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
214
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
215 #define STRVAL(name) \
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
216 } else if (strcasecmp(tname, #name) == 0) { \
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
217 if (target->name != NULL) free(target->name); \
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
218 target->name = strdup(token); \
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
219 mp_msg(MSGT_ASS, MSGL_DBG2, "%s = %s\n", #name, token);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
220
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
221 #define COLORVAL(name) ANYVAL(name,string2color)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
222 #define INTVAL(name) ANYVAL(name,atoi)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
223 #define FPVAL(name) ANYVAL(name,atof)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
224 #define TIMEVAL(name) ANYVAL(name,string2timecode)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
225 #define STYLEVAL(name) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
226 } else if (strcasecmp(tname, #name) == 0) { \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
227 target->name = lookup_style(track, token); \
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
228 mp_msg(MSGT_ASS, MSGL_DBG2, "%s = %s\n", #name, token);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
229
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
230 #define ALIAS(alias,name) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
231 if (strcasecmp(tname, #alias) == 0) {tname = #name;}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
232
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
233 static char* next_token(char** str) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
234 char* p = *str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
235 char* start;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
236 skip_spaces(&p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
237 if (*p == '\0') {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
238 *str = p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
239 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
240 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
241 start = p; // start of the token
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
242 for (; (*p != '\0') && (*p != ','); ++p) {}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
243 if (*p == '\0') {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
244 *str = p; // eos found, str will point to '\0' at exit
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
245 } else {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
246 *p = '\0';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
247 *str = p + 1; // ',' found, str will point to the next char (beginning of the next token)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
248 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
249 --p; // end of current token
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
250 rskip_spaces(&p, start);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
251 if (p < start)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
252 p = start; // empty token
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
253 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
254 ++p; // the first space character, or '\0'
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
255 *p = '\0';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
256 return start;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
257 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
258 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
259 * \brief Parse the tail of Dialogue line
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
260 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
261 * \param event parsed data goes here
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
262 * \param str string to parse, zero-terminated
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
263 * \param n_ignored number of format options to skip at the beginning
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
264 */
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
265 static int process_event_tail(ass_track_t* track, ass_event_t* event, char* str, int n_ignored)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
266 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
267 char* token;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
268 char* tname;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
269 char* p = str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
270 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
271 ass_event_t* target = event;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
272
29472
ce53e67e8633 Fix crash when event format is not specified
greg
parents: 29263
diff changeset
273 char* format;
ce53e67e8633 Fix crash when event format is not specified
greg
parents: 29263
diff changeset
274 char* q; // format scanning pointer
ce53e67e8633 Fix crash when event format is not specified
greg
parents: 29263
diff changeset
275
ce53e67e8633 Fix crash when event format is not specified
greg
parents: 29263
diff changeset
276 if (!track->event_format) {
ce53e67e8633 Fix crash when event format is not specified
greg
parents: 29263
diff changeset
277 track->event_format = strdup("Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text");
ce53e67e8633 Fix crash when event format is not specified
greg
parents: 29263
diff changeset
278 mp_msg(MSGT_ASS, MSGL_V, "Event format is broken, reseting to defaults.\n");
ce53e67e8633 Fix crash when event format is not specified
greg
parents: 29263
diff changeset
279 }
ce53e67e8633 Fix crash when event format is not specified
greg
parents: 29263
diff changeset
280
ce53e67e8633 Fix crash when event format is not specified
greg
parents: 29263
diff changeset
281 q = format = strdup(track->event_format);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
282
23539
790efaddf175 Make sure there is at least one style in ass_track when parsing events.
eugeni
parents: 23424
diff changeset
283 if (track->n_styles == 0) {
790efaddf175 Make sure there is at least one style in ass_track when parsing events.
eugeni
parents: 23424
diff changeset
284 // add "Default" style to the end
790efaddf175 Make sure there is at least one style in ass_track when parsing events.
eugeni
parents: 23424
diff changeset
285 // will be used if track does not contain a default style (or even does not contain styles at all)
790efaddf175 Make sure there is at least one style in ass_track when parsing events.
eugeni
parents: 23424
diff changeset
286 int sid = ass_alloc_style(track);
790efaddf175 Make sure there is at least one style in ass_track when parsing events.
eugeni
parents: 23424
diff changeset
287 track->styles[sid].Name = strdup("Default");
790efaddf175 Make sure there is at least one style in ass_track when parsing events.
eugeni
parents: 23424
diff changeset
288 track->styles[sid].FontName = strdup("Arial");
790efaddf175 Make sure there is at least one style in ass_track when parsing events.
eugeni
parents: 23424
diff changeset
289 }
790efaddf175 Make sure there is at least one style in ass_track when parsing events.
eugeni
parents: 23424
diff changeset
290
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
291 for (i = 0; i < n_ignored; ++i) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
292 NEXT(q, tname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
293 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
294
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
295 while (1) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
296 NEXT(q, tname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
297 if (strcasecmp(tname, "Text") == 0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
298 char* last;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
299 event->Text = strdup(p);
19612
dcd0f218008f Bugfix: potential write of unallocated memory.
eugeni
parents: 19584
diff changeset
300 if (*event->Text != 0) {
dcd0f218008f Bugfix: potential write of unallocated memory.
eugeni
parents: 19584
diff changeset
301 last = event->Text + strlen(event->Text) - 1;
dcd0f218008f Bugfix: potential write of unallocated memory.
eugeni
parents: 19584
diff changeset
302 if (last >= event->Text && *last == '\r')
dcd0f218008f Bugfix: potential write of unallocated memory.
eugeni
parents: 19584
diff changeset
303 *last = 0;
dcd0f218008f Bugfix: potential write of unallocated memory.
eugeni
parents: 19584
diff changeset
304 }
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
305 mp_msg(MSGT_ASS, MSGL_DBG2, "Text = %s\n", event->Text);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
306 event->Duration -= event->Start;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
307 free(format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
308 return 0; // "Text" is always the last
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
309 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
310 NEXT(p, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
311
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
312 ALIAS(End,Duration) // temporarily store end timecode in event->Duration
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
313 if (0) { // cool ;)
19635
4a64fecfb425 Read layer info from external subs.
eugeni
parents: 19612
diff changeset
314 INTVAL(Layer)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
315 STYLEVAL(Style)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
316 STRVAL(Name)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
317 STRVAL(Effect)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
318 INTVAL(MarginL)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
319 INTVAL(MarginR)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
320 INTVAL(MarginV)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
321 TIMEVAL(Start)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
322 TIMEVAL(Duration)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
323 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
324 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
325 free(format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
326 return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
327 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
328
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
329 /**
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
330 * \brief Parse command line style overrides (--ass-force-style option)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
331 * \param track track to apply overrides to
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
332 * The format for overrides is [StyleName.]Field=Value
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
333 */
19584
586e7d259d05 Apply -ass-force-style also to tracks generated from subdata.
eugeni
parents: 19567
diff changeset
334 void process_force_style(ass_track_t* track) {
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
335 char **fs, *eq, *dt, *style, *tname, *token;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
336 ass_style_t* target;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
337 int sid;
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
338 char** list = track->library->style_overrides;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
339
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
340 if (!list) return;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
341
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
342 for (fs = list; *fs; ++fs) {
23540
d655edc9d107 When parsing ass-force-style arguments, search for '=' and '.' characters with
eugeni
parents: 23539
diff changeset
343 eq = strrchr(*fs, '=');
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
344 if (!eq)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
345 continue;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
346 *eq = '\0';
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
347 token = eq + 1;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
348
25582
6b1d7568ae3d Allow overriding [Script Info] parameters with -ass-force-style option.
eugeni
parents: 25515
diff changeset
349 if(!strcasecmp(*fs, "PlayResX"))
6b1d7568ae3d Allow overriding [Script Info] parameters with -ass-force-style option.
eugeni
parents: 25515
diff changeset
350 track->PlayResX = atoi(token);
6b1d7568ae3d Allow overriding [Script Info] parameters with -ass-force-style option.
eugeni
parents: 25515
diff changeset
351 else if(!strcasecmp(*fs, "PlayResY"))
6b1d7568ae3d Allow overriding [Script Info] parameters with -ass-force-style option.
eugeni
parents: 25515
diff changeset
352 track->PlayResY = atoi(token);
6b1d7568ae3d Allow overriding [Script Info] parameters with -ass-force-style option.
eugeni
parents: 25515
diff changeset
353 else if(!strcasecmp(*fs, "Timer"))
6b1d7568ae3d Allow overriding [Script Info] parameters with -ass-force-style option.
eugeni
parents: 25515
diff changeset
354 track->Timer = atof(token);
6b1d7568ae3d Allow overriding [Script Info] parameters with -ass-force-style option.
eugeni
parents: 25515
diff changeset
355 else if(!strcasecmp(*fs, "WrapStyle"))
6b1d7568ae3d Allow overriding [Script Info] parameters with -ass-force-style option.
eugeni
parents: 25515
diff changeset
356 track->WrapStyle = atoi(token);
28786
9359f0ac6dd6 Style override for ScaledBorderAndShadow.
greg
parents: 28785
diff changeset
357 else if(!strcasecmp(*fs, "ScaledBorderAndShadow"))
9359f0ac6dd6 Style override for ScaledBorderAndShadow.
greg
parents: 28785
diff changeset
358 track->ScaledBorderAndShadow = parse_bool(token);
25582
6b1d7568ae3d Allow overriding [Script Info] parameters with -ass-force-style option.
eugeni
parents: 25515
diff changeset
359
23540
d655edc9d107 When parsing ass-force-style arguments, search for '=' and '.' characters with
eugeni
parents: 23539
diff changeset
360 dt = strrchr(*fs, '.');
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
361 if (dt) {
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
362 *dt = '\0';
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
363 style = *fs;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
364 tname = dt + 1;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
365 } else {
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
366 style = NULL;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
367 tname = *fs;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
368 }
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
369 for (sid = 0; sid < track->n_styles; ++sid) {
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
370 if (style == NULL || strcasecmp(track->styles[sid].Name, style) == 0) {
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
371 target = track->styles + sid;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
372 if (0) {
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
373 STRVAL(FontName)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
374 COLORVAL(PrimaryColour)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
375 COLORVAL(SecondaryColour)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
376 COLORVAL(OutlineColour)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
377 COLORVAL(BackColour)
23300
04dbd42b3962 Support fractional font sizes.
eugeni
parents: 23264
diff changeset
378 FPVAL(FontSize)
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
379 INTVAL(Bold)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
380 INTVAL(Italic)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
381 INTVAL(Underline)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
382 INTVAL(StrikeOut)
22259
cb409839a110 Correct implementation of text spacing.
eugeni
parents: 21458
diff changeset
383 FPVAL(Spacing)
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
384 INTVAL(Angle)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
385 INTVAL(BorderStyle)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
386 INTVAL(Alignment)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
387 INTVAL(MarginL)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
388 INTVAL(MarginR)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
389 INTVAL(MarginV)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
390 INTVAL(Encoding)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
391 FPVAL(ScaleX)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
392 FPVAL(ScaleY)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
393 FPVAL(Outline)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
394 FPVAL(Shadow)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
395 }
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
396 }
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
397 }
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
398 *eq = '=';
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
399 if (dt) *dt = '.';
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
400 }
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
401 }
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
402
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
403 /**
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
404 * \brief Parse the Style line
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
405 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
406 * \param str string to parse, zero-terminated
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
407 * Allocates a new style struct.
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
408 */
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
409 static int process_style(ass_track_t* track, char *str)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
410 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
411
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
412 char* token;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
413 char* tname;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
414 char* p = str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
415 char* format;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
416 char* q; // format scanning pointer
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
417 int sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
418 ass_style_t* style;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
419 ass_style_t* target;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
420
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
421 if (!track->style_format) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
422 // no style format header
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
423 // probably an ancient script version
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
424 if (track->track_type == TRACK_TYPE_SSA)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
425 track->style_format = strdup("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
426 "TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
427 "Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
428 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
429 track->style_format = strdup("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
430 "OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
431 "ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
432 "Alignment, MarginL, MarginR, MarginV, Encoding");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
433 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
434
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
435 q = format = strdup(track->style_format);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
436
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
437 mp_msg(MSGT_ASS, MSGL_V, "[%p] Style: %s\n", track, str);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
438
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
439 sid = ass_alloc_style(track);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
440
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
441 style = track->styles + sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
442 target = style;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
443 // fill style with some default values
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
444 style->ScaleX = 100.;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
445 style->ScaleY = 100.;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
446
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
447 while (1) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
448 NEXT(q, tname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
449 NEXT(p, token);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
450
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
451 // ALIAS(TertiaryColour,OutlineColour) // ignore TertiaryColour; it appears only in SSA, and is overridden by BackColour
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
452
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
453 if (0) { // cool ;)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
454 STRVAL(Name)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
455 if ((strcmp(target->Name, "Default")==0) || (strcmp(target->Name, "*Default")==0))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
456 track->default_style = sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
457 STRVAL(FontName)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
458 COLORVAL(PrimaryColour)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
459 COLORVAL(SecondaryColour)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
460 COLORVAL(OutlineColour) // TertiaryColor
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
461 COLORVAL(BackColour)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
462 // SSA uses BackColour for both outline and shadow
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
463 // this will destroy SSA's TertiaryColour, but i'm not going to use it anyway
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
464 if (track->track_type == TRACK_TYPE_SSA)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
465 target->OutlineColour = target->BackColour;
23300
04dbd42b3962 Support fractional font sizes.
eugeni
parents: 23264
diff changeset
466 FPVAL(FontSize)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
467 INTVAL(Bold)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
468 INTVAL(Italic)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
469 INTVAL(Underline)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
470 INTVAL(StrikeOut)
22259
cb409839a110 Correct implementation of text spacing.
eugeni
parents: 21458
diff changeset
471 FPVAL(Spacing)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
472 INTVAL(Angle)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
473 INTVAL(BorderStyle)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
474 INTVAL(Alignment)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
475 if (track->track_type == TRACK_TYPE_ASS)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
476 target->Alignment = numpad2align(target->Alignment);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
477 INTVAL(MarginL)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
478 INTVAL(MarginR)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
479 INTVAL(MarginV)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
480 INTVAL(Encoding)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
481 FPVAL(ScaleX)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
482 FPVAL(ScaleY)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
483 FPVAL(Outline)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
484 FPVAL(Shadow)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
485 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
486 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
487 style->ScaleX /= 100.;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
488 style->ScaleY /= 100.;
22263
1bf17db53cc3 Both (-1) and 1 mean bold font in SSA/ASS styles.
eugeni
parents: 22259
diff changeset
489 style->Bold = !!style->Bold;
1bf17db53cc3 Both (-1) and 1 mean bold font in SSA/ASS styles.
eugeni
parents: 22259
diff changeset
490 style->Italic = !!style->Italic;
1bf17db53cc3 Both (-1) and 1 mean bold font in SSA/ASS styles.
eugeni
parents: 22259
diff changeset
491 style->Underline = !!style->Underline;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
492 if (!style->Name)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
493 style->Name = strdup("Default");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
494 if (!style->FontName)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
495 style->FontName = strdup("Arial");
26593
b42233eb4b38 Skip '@' at the beginning of the font name.
eugeni
parents: 26271
diff changeset
496 // skip '@' at the start of the font name
b42233eb4b38 Skip '@' at the beginning of the font name.
eugeni
parents: 26271
diff changeset
497 if (*style->FontName == '@') {
b42233eb4b38 Skip '@' at the beginning of the font name.
eugeni
parents: 26271
diff changeset
498 p = style->FontName;
b42233eb4b38 Skip '@' at the beginning of the font name.
eugeni
parents: 26271
diff changeset
499 style->FontName = strdup(p + 1);
b42233eb4b38 Skip '@' at the beginning of the font name.
eugeni
parents: 26271
diff changeset
500 free(p);
b42233eb4b38 Skip '@' at the beginning of the font name.
eugeni
parents: 26271
diff changeset
501 }
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
502 free(format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
503 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
504
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
505 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
506
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
507 static int process_styles_line(ass_track_t* track, char *str)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
508 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
509 if (!strncmp(str,"Format:", 7)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
510 char* p = str + 7;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
511 skip_spaces(&p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
512 track->style_format = strdup(p);
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
513 mp_msg(MSGT_ASS, MSGL_DBG2, "Style format: %s\n", track->style_format);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
514 } else if (!strncmp(str,"Style:", 6)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
515 char* p = str + 6;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
516 skip_spaces(&p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
517 process_style(track, p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
518 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
519 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
520 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
521
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
522 static int process_info_line(ass_track_t* track, char *str)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
523 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
524 if (!strncmp(str, "PlayResX:", 9)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
525 track->PlayResX = atoi(str + 9);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
526 } else if (!strncmp(str,"PlayResY:", 9)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
527 track->PlayResY = atoi(str + 9);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
528 } else if (!strncmp(str,"Timer:", 6)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
529 track->Timer = atof(str + 6);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
530 } else if (!strncmp(str,"WrapStyle:", 10)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
531 track->WrapStyle = atoi(str + 10);
28785
0366ab2c1cb9 Support ScaledBorderAndShadow property.
greg
parents: 27499
diff changeset
532 } else if (!strncmp(str, "ScaledBorderAndShadow:", 22)) {
0366ab2c1cb9 Support ScaledBorderAndShadow property.
greg
parents: 27499
diff changeset
533 track->ScaledBorderAndShadow = parse_bool(str + 22);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
534 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
535 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
536 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
537
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
538 static int process_events_line(ass_track_t* track, char *str)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
539 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
540 if (!strncmp(str, "Format:", 7)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
541 char* p = str + 7;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
542 skip_spaces(&p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
543 track->event_format = strdup(p);
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
544 mp_msg(MSGT_ASS, MSGL_DBG2, "Event format: %s\n", track->event_format);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
545 } else if (!strncmp(str, "Dialogue:", 9)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
546 // This should never be reached for embedded subtitles.
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
547 // They have slightly different format and are parsed in ass_process_chunk,
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
548 // called directly from demuxer
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
549 int eid;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
550 ass_event_t* event;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
551
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
552 str += 9;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
553 skip_spaces(&str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
554
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
555 eid = ass_alloc_event(track);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
556 event = track->events + eid;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
557
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
558 process_event_tail(track, event, str, 0);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
559 } else {
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
560 mp_msg(MSGT_ASS, MSGL_V, "Not understood: %s \n", str);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
561 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
562 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
563 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
564
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
565 // Copied from mkvtoolnix
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
566 static unsigned char* decode_chars(unsigned char c1, unsigned char c2,
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
567 unsigned char c3, unsigned char c4, unsigned char* dst, int cnt)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
568 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
569 uint32_t value;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
570 unsigned char bytes[3];
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
571 int i;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
572
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
573 value = ((c1 - 33) << 18) + ((c2 - 33) << 12) + ((c3 - 33) << 6) + (c4 - 33);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
574 bytes[2] = value & 0xff;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
575 bytes[1] = (value & 0xff00) >> 8;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
576 bytes[0] = (value & 0xff0000) >> 16;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
577
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
578 for (i = 0; i < cnt; ++i)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
579 *dst++ = bytes[i];
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
580 return dst;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
581 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
582
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
583 static int decode_font(ass_track_t* track)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
584 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
585 unsigned char* p;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
586 unsigned char* q;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
587 int i;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
588 int size; // original size
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
589 int dsize; // decoded size
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
590 unsigned char* buf = 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
591
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
592 mp_msg(MSGT_ASS, MSGL_V, "font: %d bytes encoded data \n", track->parser_priv->fontdata_used);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
593 size = track->parser_priv->fontdata_used;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
594 if (size % 4 == 1) {
21066
6196ba31e97e MSGTRs for libass
kraymer
parents: 21026
diff changeset
595 mp_msg(MSGT_ASS, MSGL_ERR, MSGTR_LIBASS_BadEncodedDataSize);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
596 goto error_decode_font;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
597 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
598 buf = malloc(size / 4 * 3 + 2);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
599 q = buf;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
600 for (i = 0, p = (unsigned char*)track->parser_priv->fontdata; i < size / 4; i++, p+=4) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
601 q = decode_chars(p[0], p[1], p[2], p[3], q, 3);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
602 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
603 if (size % 4 == 2) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
604 q = decode_chars(p[0], p[1], 0, 0, q, 1);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
605 } else if (size % 4 == 3) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
606 q = decode_chars(p[0], p[1], p[2], 0, q, 2);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
607 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
608 dsize = q - buf;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
609 assert(dsize <= size / 4 * 3 + 2);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
610
23264
dc12bca7d4b5 Don't deallocate font data if it will be used later.
eugeni
parents: 22263
diff changeset
611 if (track->library->extract_fonts) {
21455
dd49d3712e6f Rename: ass_process_font -> ass_add_font.
eugeni
parents: 21453
diff changeset
612 ass_add_font(track->library, track->parser_priv->fontname, (char*)buf, dsize);
23264
dc12bca7d4b5 Don't deallocate font data if it will be used later.
eugeni
parents: 22263
diff changeset
613 buf = 0;
dc12bca7d4b5 Don't deallocate font data if it will be used later.
eugeni
parents: 22263
diff changeset
614 }
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
615
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
616 error_decode_font:
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
617 if (buf) free(buf);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
618 free(track->parser_priv->fontname);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
619 free(track->parser_priv->fontdata);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
620 track->parser_priv->fontname = 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
621 track->parser_priv->fontdata = 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
622 track->parser_priv->fontdata_size = 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
623 track->parser_priv->fontdata_used = 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
624 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
625 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
626
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
627 static int process_fonts_line(ass_track_t* track, char *str)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
628 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
629 int len;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
630
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
631 if (!strncmp(str, "fontname:", 9)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
632 char* p = str + 9;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
633 skip_spaces(&p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
634 if (track->parser_priv->fontname) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
635 decode_font(track);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
636 }
21453
9dd976e14792 Avoid calling validate_fname() twice for one string.
eugeni
parents: 21066
diff changeset
637 track->parser_priv->fontname = strdup(p);
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
638 mp_msg(MSGT_ASS, MSGL_V, "fontname: %s\n", track->parser_priv->fontname);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
639 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
640 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
641
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
642 if (!track->parser_priv->fontname) {
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
643 mp_msg(MSGT_ASS, MSGL_V, "Not understood: %s \n", str);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
644 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
645 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
646
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
647 len = strlen(str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
648 if (len > 80) {
21066
6196ba31e97e MSGTRs for libass
kraymer
parents: 21026
diff changeset
649 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FontLineTooLong, len, str);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
650 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
651 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
652 if (track->parser_priv->fontdata_used + len > track->parser_priv->fontdata_size) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
653 track->parser_priv->fontdata_size += 100 * 1024;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
654 track->parser_priv->fontdata = realloc(track->parser_priv->fontdata, track->parser_priv->fontdata_size);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
655 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
656 memcpy(track->parser_priv->fontdata + track->parser_priv->fontdata_used, str, len);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
657 track->parser_priv->fontdata_used += len;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
658
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
659 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
660 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
661
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
662 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
663 * \brief Parse a header line
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
664 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
665 * \param str string to parse, zero-terminated
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
666 */
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
667 static int process_line(ass_track_t* track, char *str)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
668 {
26271
fa145ef5ac13 Case insensitive parsing of SSA/ASS section headers.
eugeni
parents: 26270
diff changeset
669 if (!strncasecmp(str, "[Script Info]", 13)) {
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
670 track->parser_priv->state = PST_INFO;
26271
fa145ef5ac13 Case insensitive parsing of SSA/ASS section headers.
eugeni
parents: 26270
diff changeset
671 } else if (!strncasecmp(str, "[V4 Styles]", 11)) {
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
672 track->parser_priv->state = PST_STYLES;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
673 track->track_type = TRACK_TYPE_SSA;
26271
fa145ef5ac13 Case insensitive parsing of SSA/ASS section headers.
eugeni
parents: 26270
diff changeset
674 } else if (!strncasecmp(str, "[V4+ Styles]", 12)) {
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
675 track->parser_priv->state = PST_STYLES;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
676 track->track_type = TRACK_TYPE_ASS;
26271
fa145ef5ac13 Case insensitive parsing of SSA/ASS section headers.
eugeni
parents: 26270
diff changeset
677 } else if (!strncasecmp(str, "[Events]", 8)) {
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
678 track->parser_priv->state = PST_EVENTS;
26271
fa145ef5ac13 Case insensitive parsing of SSA/ASS section headers.
eugeni
parents: 26270
diff changeset
679 } else if (!strncasecmp(str, "[Fonts]", 7)) {
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
680 track->parser_priv->state = PST_FONTS;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
681 } else {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
682 switch (track->parser_priv->state) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
683 case PST_INFO:
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
684 process_info_line(track, str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
685 break;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
686 case PST_STYLES:
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
687 process_styles_line(track, str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
688 break;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
689 case PST_EVENTS:
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
690 process_events_line(track, str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
691 break;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
692 case PST_FONTS:
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
693 process_fonts_line(track, str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
694 break;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
695 default:
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
696 break;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
697 }
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
698 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
699
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
700 // there is no explicit end-of-font marker in ssa/ass
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
701 if ((track->parser_priv->state != PST_FONTS) && (track->parser_priv->fontname))
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
702 decode_font(track);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
703
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
704 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
705 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
706
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
707 static int process_text(ass_track_t* track, char* str)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
708 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
709 char* p = str;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
710 while(1) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
711 char* q;
26270
75a108423c11 Skip BOM at the beginning of text in ASS parser.
eugeni
parents: 25582
diff changeset
712 while (1) {
75a108423c11 Skip BOM at the beginning of text in ASS parser.
eugeni
parents: 25582
diff changeset
713 if ((*p=='\r')||(*p=='\n')) ++p;
75a108423c11 Skip BOM at the beginning of text in ASS parser.
eugeni
parents: 25582
diff changeset
714 else if (p[0]=='\xef' && p[1]=='\xbb' && p[2]=='\xbf') p+=3; // U+FFFE (BOM)
75a108423c11 Skip BOM at the beginning of text in ASS parser.
eugeni
parents: 25582
diff changeset
715 else break;
75a108423c11 Skip BOM at the beginning of text in ASS parser.
eugeni
parents: 25582
diff changeset
716 }
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
717 for (q=p; ((*q!='\0')&&(*q!='\r')&&(*q!='\n')); ++q) {};
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
718 if (q==p)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
719 break;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
720 if (*q != '\0')
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
721 *(q++) = '\0';
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
722 process_line(track, p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
723 if (*q == '\0')
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
724 break;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
725 p = q;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
726 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
727 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
728 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
729
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
730 /**
27498
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
731 * \brief Process a chunk of subtitle stream data.
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
732 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
733 * \param data string to parse
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
734 * \param size length of data
27498
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
735 */
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
736 void ass_process_data(ass_track_t* track, char* data, int size)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
737 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
738 char* str = malloc(size + 1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
739
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
740 memcpy(str, data, size);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
741 str[size] = '\0';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
742
27498
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
743 mp_msg(MSGT_ASS, MSGL_V, "event: %s\n", str);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
744 process_text(track, str);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
745 free(str);
27498
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
746 }
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
747
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
748 /**
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
749 * \brief Process CodecPrivate section of subtitle stream
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
750 * \param track track
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
751 * \param data string to parse
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
752 * \param size length of data
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
753 CodecPrivate section contains [Stream Info] and [V4+ Styles] ([V4 Styles] for SSA) sections
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
754 */
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
755 void ass_process_codec_private(ass_track_t* track, char *data, int size)
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
756 {
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
757 ass_process_data(track, data, size);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
758
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
759 if (!track->event_format) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
760 // probably an mkv produced by ancient mkvtoolnix
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
761 // such files don't have [Events] and Format: headers
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
762 track->parser_priv->state = PST_EVENTS;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
763 if (track->track_type == TRACK_TYPE_SSA)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
764 track->event_format = strdup("Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
765 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
766 track->event_format = strdup("Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
767 }
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
768
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
769 process_force_style(track);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
770 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
771
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
772 static int check_duplicate_event(ass_track_t* track, int ReadOrder)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
773 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
774 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
775 for (i = 0; i<track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
776 if (track->events[i].ReadOrder == ReadOrder)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
777 return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
778 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
779 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
780
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
781 /**
25515
diego
parents: 24851
diff changeset
782 * \brief Process a chunk of subtitle stream data. In Matroska, this contains exactly 1 event (or a commentary).
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
783 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
784 * \param data string to parse
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
785 * \param size length of data
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
786 * \param timecode starting time of the event (milliseconds)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
787 * \param duration duration of the event (milliseconds)
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
788 */
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
789 void ass_process_chunk(ass_track_t* track, char *data, int size, long long timecode, long long duration)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
790 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
791 char* str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
792 int eid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
793 char* p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
794 char* token;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
795 ass_event_t* event;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
796
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
797 if (!track->event_format) {
21066
6196ba31e97e MSGTRs for libass
kraymer
parents: 21026
diff changeset
798 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_EventFormatHeaderMissing);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
799 return;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
800 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
801
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
802 str = malloc(size + 1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
803 memcpy(str, data, size);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
804 str[size] = '\0';
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
805 mp_msg(MSGT_ASS, MSGL_V, "event at %" PRId64 ", +%" PRId64 ": %s \n", (int64_t)timecode, (int64_t)duration, str);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
806
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
807 eid = ass_alloc_event(track);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
808 event = track->events + eid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
809
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
810 p = str;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
811
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
812 do {
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
813 NEXT(p, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
814 event->ReadOrder = atoi(token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
815 if (check_duplicate_event(track, event->ReadOrder))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
816 break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
817
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
818 NEXT(p, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
819 event->Layer = atoi(token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
820
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
821 process_event_tail(track, event, p, 3);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
822
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
823 event->Start = timecode;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
824 event->Duration = duration;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
825
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
826 free(str);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
827 return;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
828 // dump_events(tid);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
829 } while (0);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
830 // some error
19474
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
831 ass_free_event(track, eid);
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
832 track->n_events--;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
833 free(str);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
834 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
835
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
836 #ifdef CONFIG_ICONV
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
837 /** \brief recode buffer to utf-8
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
838 * constraint: codepage != 0
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
839 * \param data pointer to text buffer
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
840 * \param size buffer size
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
841 * \return a pointer to recoded buffer, caller is responsible for freeing it
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
842 **/
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
843 static char* sub_recode(char* data, size_t size, char* codepage)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
844 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
845 static iconv_t icdsc = (iconv_t)(-1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
846 char* tocp = "UTF-8";
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
847 char* outbuf;
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
848 assert(codepage);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
849
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
850 {
24851
e36efda34616 Our enca code uses strdup() on the input encoding name, as we don't modify it we can use the original constant string.
iive
parents: 23540
diff changeset
851 const char* cp_tmp = codepage;
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
852 #ifdef CONFIG_ENCA
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
853 char enca_lang[3], enca_fallback[100];
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
854 if (sscanf(codepage, "enca:%2s:%99s", enca_lang, enca_fallback) == 2
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
855 || sscanf(codepage, "ENCA:%2s:%99s", enca_lang, enca_fallback) == 2) {
20503
5cbf1c33a668 Revert r20517.
eugeni
parents: 20501
diff changeset
856 cp_tmp = guess_buffer_cp((unsigned char*)data, size, enca_lang, enca_fallback);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
857 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
858 #endif
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
859 if ((icdsc = iconv_open (tocp, cp_tmp)) != (iconv_t)(-1)){
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
860 mp_msg(MSGT_ASS,MSGL_V,"LIBSUB: opened iconv descriptor.\n");
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
861 } else
21066
6196ba31e97e MSGTRs for libass
kraymer
parents: 21026
diff changeset
862 mp_msg(MSGT_ASS,MSGL_ERR,MSGTR_LIBASS_ErrorOpeningIconvDescriptor);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
863 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
864
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
865 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
866 size_t osize = size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
867 size_t ileft = size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
868 size_t oleft = size - 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
869 char* ip;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
870 char* op;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
871 size_t rc;
26891
29e92a7a75b6 Clear iconv conversion state also in libass.
eugeni
parents: 26890
diff changeset
872 int clear = 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
873
26892
3c360ca1c304 The size of output buffer is stored in 'osize', not 'size'.
eugeni
parents: 26891
diff changeset
874 outbuf = malloc(osize);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
875 ip = data;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
876 op = outbuf;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
877
26891
29e92a7a75b6 Clear iconv conversion state also in libass.
eugeni
parents: 26890
diff changeset
878 while (1) {
29e92a7a75b6 Clear iconv conversion state also in libass.
eugeni
parents: 26890
diff changeset
879 if (ileft)
29e92a7a75b6 Clear iconv conversion state also in libass.
eugeni
parents: 26890
diff changeset
880 rc = iconv(icdsc, &ip, &ileft, &op, &oleft);
29e92a7a75b6 Clear iconv conversion state also in libass.
eugeni
parents: 26890
diff changeset
881 else {// clear the conversion state and leave
29e92a7a75b6 Clear iconv conversion state also in libass.
eugeni
parents: 26890
diff changeset
882 clear = 1;
29e92a7a75b6 Clear iconv conversion state also in libass.
eugeni
parents: 26890
diff changeset
883 rc = iconv(icdsc, NULL, NULL, &op, &oleft);
29e92a7a75b6 Clear iconv conversion state also in libass.
eugeni
parents: 26890
diff changeset
884 }
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
885 if (rc == (size_t)(-1)) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
886 if (errno == E2BIG) {
26890
80bf10a56b7e Offset should be size_t.
eugeni
parents: 26807
diff changeset
887 size_t offset = op - outbuf;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
888 outbuf = (char*)realloc(outbuf, osize + size);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
889 op = outbuf + offset;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
890 osize += size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
891 oleft += size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
892 } else {
21066
6196ba31e97e MSGTRs for libass
kraymer
parents: 21026
diff changeset
893 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorRecodingFile);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
894 return NULL;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
895 }
26891
29e92a7a75b6 Clear iconv conversion state also in libass.
eugeni
parents: 26890
diff changeset
896 } else
29e92a7a75b6 Clear iconv conversion state also in libass.
eugeni
parents: 26890
diff changeset
897 if (clear)
29e92a7a75b6 Clear iconv conversion state also in libass.
eugeni
parents: 26890
diff changeset
898 break;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
899 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
900 outbuf[osize - oleft - 1] = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
901 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
902
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
903 if (icdsc != (iconv_t)(-1)) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
904 (void)iconv_close(icdsc);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
905 icdsc = (iconv_t)(-1);
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
906 mp_msg(MSGT_ASS,MSGL_V,"LIBSUB: closed iconv descriptor.\n");
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
907 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
908
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
909 return outbuf;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
910 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
911 #endif // ICONV
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
912
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
913 /**
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
914 * \brief read file contents into newly allocated buffer
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
915 * \param fname file name
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
916 * \param bufsize out: file size
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
917 * \return pointer to file contents. Caller is responsible for its deallocation.
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
918 */
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
919 static char* read_file(char* fname, size_t *bufsize)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
920 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
921 int res;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
922 long sz;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
923 long bytes_read;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
924 char* buf;
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
925
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
926 FILE* fp = fopen(fname, "rb");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
927 if (!fp) {
21066
6196ba31e97e MSGTRs for libass
kraymer
parents: 21026
diff changeset
928 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FopenFailed, fname);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
929 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
930 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
931 res = fseek(fp, 0, SEEK_END);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
932 if (res == -1) {
21066
6196ba31e97e MSGTRs for libass
kraymer
parents: 21026
diff changeset
933 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FseekFailed, fname);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
934 fclose(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
935 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
936 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
937
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
938 sz = ftell(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
939 rewind(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
940
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
941 if (sz > 10*1024*1024) {
21066
6196ba31e97e MSGTRs for libass
kraymer
parents: 21026
diff changeset
942 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_RefusingToLoadSubtitlesLargerThan10M, fname);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
943 fclose(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
944 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
945 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
946
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20603
diff changeset
947 mp_msg(MSGT_ASS, MSGL_V, "file size: %ld\n", sz);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
948
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
949 buf = malloc(sz + 1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
950 assert(buf);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
951 bytes_read = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
952 do {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
953 res = fread(buf + bytes_read, 1, sz - bytes_read, fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
954 if (res <= 0) {
21066
6196ba31e97e MSGTRs for libass
kraymer
parents: 21026
diff changeset
955 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_ReadFailed, errno, strerror(errno));
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
956 fclose(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
957 free(buf);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
958 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
959 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
960 bytes_read += res;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
961 } while (sz - bytes_read > 0);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
962 buf[sz] = '\0';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
963 fclose(fp);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
964
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
965 if (bufsize)
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
966 *bufsize = sz;
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
967 return buf;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
968 }
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
969
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
970 /*
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
971 * \param buf pointer to subtitle text in utf-8
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
972 */
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
973 static ass_track_t* parse_memory(ass_library_t* library, char* buf)
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
974 {
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
975 ass_track_t* track;
19905
5a24682d9dd5 Set ReadOrder values for external subtitles.
eugeni
parents: 19652
diff changeset
976 int i;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
977
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
978 track = ass_new_track(library);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
979
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
980 // process header
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
981 process_text(track, buf);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
982
19905
5a24682d9dd5 Set ReadOrder values for external subtitles.
eugeni
parents: 19652
diff changeset
983 // external SSA/ASS subs does not have ReadOrder field
5a24682d9dd5 Set ReadOrder values for external subtitles.
eugeni
parents: 19652
diff changeset
984 for (i = 0; i < track->n_events; ++i)
5a24682d9dd5 Set ReadOrder values for external subtitles.
eugeni
parents: 19652
diff changeset
985 track->events[i].ReadOrder = i;
5a24682d9dd5 Set ReadOrder values for external subtitles.
eugeni
parents: 19652
diff changeset
986
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
987 // there is no explicit end-of-font marker in ssa/ass
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
988 if (track->parser_priv->fontname)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
989 decode_font(track);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
990
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
991 if (track->track_type == TRACK_TYPE_UNKNOWN) {
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
992 ass_free_track(track);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
993 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
994 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
995
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
996 process_force_style(track);
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
997
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
998 return track;
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
999 }
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1000
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1001 /**
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1002 * \brief Read subtitles from memory.
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1003 * \param library libass library object
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1004 * \param buf pointer to subtitles text
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1005 * \param bufsize size of buffer
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1006 * \param codepage recode buffer contents from given codepage
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1007 * \return newly allocated track
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1008 */
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1009 ass_track_t* ass_read_memory(ass_library_t* library, char* buf, size_t bufsize, char* codepage)
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1010 {
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1011 ass_track_t* track;
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1012 int need_free = 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1013
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1014 if (!buf)
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1015 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1016
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
1017 #ifdef CONFIG_ICONV
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1018 if (codepage)
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1019 buf = sub_recode(buf, bufsize, codepage);
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1020 if (!buf)
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1021 return 0;
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1022 else
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1023 need_free = 1;
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1024 #endif
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1025 track = parse_memory(library, buf);
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1026 if (need_free)
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1027 free(buf);
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1028 if (!track)
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1029 return 0;
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1030
21066
6196ba31e97e MSGTRs for libass
kraymer
parents: 21026
diff changeset
1031 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_AddedSubtitleFileMemory, track->n_styles, track->n_events);
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1032 return track;
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1033 }
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1034
27499
1dfad1b382fa libass: fix type mismatch between size parameter and the way it's used
aurel
parents: 27498
diff changeset
1035 char* read_file_recode(char* fname, char* codepage, size_t* size)
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1036 {
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1037 char* buf;
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1038 size_t bufsize;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1039
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1040 buf = read_file(fname, &bufsize);
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1041 if (!buf)
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1042 return 0;
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
1043 #ifdef CONFIG_ICONV
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1044 if (codepage) {
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1045 char* tmpbuf = sub_recode(buf, bufsize, codepage);
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1046 free(buf);
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1047 buf = tmpbuf;
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1048 }
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1049 if (!buf)
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1050 return 0;
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1051 #endif
23424
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1052 *size = bufsize;
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1053 return buf;
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1054 }
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1055
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1056 /**
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1057 * \brief Read subtitles from file.
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1058 * \param library libass library object
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1059 * \param fname file name
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1060 * \param codepage recode buffer contents from given codepage
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1061 * \return newly allocated track
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1062 */
23424
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1063 ass_track_t* ass_read_file(ass_library_t* library, char* fname, char* codepage)
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1064 {
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1065 char* buf;
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1066 ass_track_t* track;
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1067 size_t bufsize;
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1068
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1069 buf = read_file_recode(fname, codepage, &bufsize);
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1070 if (!buf)
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1071 return 0;
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1072 track = parse_memory(library, buf);
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1073 free(buf);
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1074 if (!track)
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1075 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1076
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1077 track->name = strdup(fname);
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1078
21066
6196ba31e97e MSGTRs for libass
kraymer
parents: 21026
diff changeset
1079 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_AddedSubtitleFileFname, fname, track->n_styles, track->n_events);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1080
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1081 // dump_events(forced_tid);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1082 return track;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1083 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1084
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1085 /**
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1086 * \brief read styles from file into already initialized track
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1087 */
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
1088 int ass_read_styles(ass_track_t* track, char* fname, char* codepage)
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1089 {
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1090 char* buf;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1091 parser_state_t old_state;
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1092 size_t sz;
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1093
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1094 buf = read_file(fname, &sz);
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1095 if (!buf)
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1096 return 1;
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
1097 #ifdef CONFIG_ICONV
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1098 if (codepage) {
20698
7088929a2cb8 Fix the following compiler warnings:
eugeni
parents: 20629
diff changeset
1099 char* tmpbuf;
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1100 tmpbuf = sub_recode(buf, sz, codepage);
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1101 free(buf);
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1102 buf = tmpbuf;
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1103 }
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1104 if (!buf)
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1105 return 0;
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1106 #endif
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1107
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1108 old_state = track->parser_priv->state;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1109 track->parser_priv->state = PST_STYLES;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1110 process_text(track, buf);
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1111 track->parser_priv->state = old_state;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1112
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1113 return 0;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1114 }
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1115
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1116 long long ass_step_sub(ass_track_t* track, long long now, int movement) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1117 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1118
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1119 if (movement == 0) return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1120 if (track->n_events == 0) return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1121
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1122 if (movement < 0)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1123 for (i = 0; (i < track->n_events) && ((long long)(track->events[i].Start + track->events[i].Duration) <= now); ++i) {}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1124 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1125 for (i = track->n_events - 1; (i >= 0) && ((long long)(track->events[i].Start) > now); --i) {}
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1126
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1127 // -1 and n_events are ok
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1128 assert(i >= -1); assert(i <= track->n_events);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1129 i += movement;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1130 if (i < 0) i = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1131 if (i >= track->n_events) i = track->n_events - 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1132 return ((long long)track->events[i].Start) - now;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1133 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1134
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
1135 ass_track_t* ass_new_track(ass_library_t* library) {
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1136 ass_track_t* track = calloc(1, sizeof(ass_track_t));
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
1137 track->library = library;
28900
b2d3d9408a99 Use ScaledBorderAndShadow: yes by default.
greg
parents: 28786
diff changeset
1138 track->ScaledBorderAndShadow = 1;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
1139 track->parser_priv = calloc(1, sizeof(parser_priv_t));
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1140 return track;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1141 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1142