annotate libass/ass.c @ 22949:6613c5397aba

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