2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with libass; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <ass/ass_types.h>
30 #include <libavutil/common.h>
36 #include "subreader.h"
37 #include "stream/stream.h"
40 #ifdef CONFIG_FONTCONFIG
41 extern int font_fontconfig
;
43 static int font_fontconfig
= -1;
45 extern char *font_name
;
46 extern char *sub_font_name
;
47 extern float text_font_scale_factor
;
48 extern int subtitle_autoscale
;
53 static char *sub_cp
= 0;
56 ASS_Track
*mp_ass_default_track(ASS_Library
*library
, struct MPOpts
*opts
)
58 ASS_Track
*track
= ass_new_track(library
);
60 track
->track_type
= TRACK_TYPE_ASS
;
62 track
->PlayResY
= 288;
65 if (opts
->ass_styles_file
)
66 ass_read_styles(track
, opts
->ass_styles_file
, sub_cp
);
68 if (track
->n_styles
== 0) {
69 track
->Kerning
= true;
70 int sid
= ass_alloc_style(track
);
71 track
->default_style
= sid
;
72 ASS_Style
*style
= track
->styles
+ sid
;
73 style
->Name
= strdup("Default");
74 style
->FontName
= (font_fontconfig
>= 0
75 && sub_font_name
) ? strdup(sub_font_name
)
76 : (font_fontconfig
>= 0
77 && font_name
) ? strdup(font_name
) : strdup("Sans");
78 style
->treat_fontname_as_pattern
= 1;
80 double fs
= track
->PlayResY
* text_font_scale_factor
/ 100.;
81 /* The font size is always proportional to video height only;
82 * real -subfont-autoscale behavior is not implemented.
83 * Apply a correction that corresponds to about 4:3 aspect ratio
84 * video to get a size somewhat closer to what non-libass rendering
85 * would produce with the same text_font_scale_factor
86 * and subtitle_autoscale.
88 if (subtitle_autoscale
== 2)
90 else if (subtitle_autoscale
== 3)
93 uint32_t c1
= 0xFFFFFF00;
94 uint32_t c2
= 0x00000000;
96 c1
= strtoll(opts
->ass_color
, NULL
, 16);
97 if (opts
->ass_border_color
)
98 c2
= strtoll(opts
->ass_border_color
, NULL
, 16);
100 style
->FontSize
= fs
;
101 style
->PrimaryColour
= c1
;
102 style
->SecondaryColour
= c1
;
103 style
->OutlineColour
= c2
;
104 style
->BackColour
= 0x00000000;
105 style
->BorderStyle
= 1;
106 style
->Alignment
= 2;
107 style
->Outline
= fs
/ 16;
115 ass_process_force_style(track
);
119 static int check_duplicate_plaintext_event(ASS_Track
*track
)
122 ASS_Event
*evt
= track
->events
+ track
->n_events
- 1;
124 for (i
= 0; i
< track
->n_events
- 1; ++i
) // ignoring last event, it is the one we are comparing with
125 if (track
->events
[i
].Start
== evt
->Start
&&
126 track
->events
[i
].Duration
== evt
->Duration
&&
127 strcmp(track
->events
[i
].Text
, evt
->Text
) == 0)
133 * \brief Convert subtitle to ASS_Events for the given track
135 * \param sub subtitle to convert
137 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
138 * Start and Duration in other case.
140 static int ass_process_subtitle(ASS_Track
*track
, subtitle
*sub
)
148 eid
= ass_alloc_event(track
);
149 event
= track
->events
+ eid
;
151 event
->Start
= sub
->start
* 10;
152 event
->Duration
= (sub
->end
- sub
->start
) * 10;
153 event
->Style
= track
->default_style
;
155 for (j
= 0; j
< sub
->lines
; ++j
)
156 len
+= sub
->text
[j
] ? strlen(sub
->text
[j
]) : 0;
158 len
+= 2 * sub
->lines
; // '\N', including the one after the last line
162 event
->Text
= malloc(len
);
163 end
= event
->Text
+ len
;
167 p
+= snprintf(p
, end
- p
, "{\\an%d}", sub
->alignment
);
169 for (j
= 0; j
< sub
->lines
; ++j
)
170 p
+= snprintf(p
, end
- p
, "%s\\N", sub
->text
[j
]);
173 p
-= 2; // remove last "\N"
176 if (check_duplicate_plaintext_event(track
)) {
177 ass_free_event(track
, eid
);
182 mp_msg(MSGT_ASS
, MSGL_V
,
183 "plaintext event at %" PRId64
", +%" PRId64
": %s \n",
184 (int64_t) event
->Start
, (int64_t) event
->Duration
, event
->Text
);
191 * \brief Convert subdata to ASS_Track
192 * \param subdata subtitles struct from subreader
193 * \param fps video framerate
194 * \return newly allocated ASS_Track, filled with subtitles from subdata
196 ASS_Track
*mp_ass_read_subdata(ASS_Library
*library
, struct MPOpts
*opts
,
197 sub_data
*subdata
, double fps
)
202 track
= mp_ass_default_track(library
, opts
);
203 track
->name
= subdata
->filename
? strdup(subdata
->filename
) : 0;
205 for (i
= 0; i
< subdata
->sub_num
; ++i
) {
206 int eid
= ass_process_subtitle(track
, subdata
->subtitles
+ i
);
209 if (!subdata
->sub_uses_time
) {
210 track
->events
[eid
].Start
*= 100. / fps
;
211 track
->events
[eid
].Duration
*= 100. / fps
;
217 ASS_Track
*mp_ass_read_stream(ASS_Library
*library
, const char *fname
,
222 struct stream
*s
= open_stream(fname
, NULL
, NULL
);
224 // Stream code should have printed an error already
226 struct bstr content
= stream_read_complete(s
, NULL
, 100000000, 1);
227 if (content
.start
== NULL
)
228 mp_tmsg(MSGT_ASS
, MSGL_ERR
, "Refusing to load subtitle file "
229 "larger than 100 MB: %s\n", fname
);
231 if (content
.len
== 0) {
232 talloc_free(content
.start
);
235 content
.start
[content
.len
] = 0;
236 track
= ass_read_memory(library
, content
.start
, content
.len
, charset
);
239 track
->name
= strdup(fname
);
241 talloc_free(content
.start
);
245 void mp_ass_configure(ASS_Renderer
*priv
, struct MPOpts
*opts
, int w
, int h
,
249 ass_set_frame_size(priv
, w
, h
);
250 ass_set_margins(priv
, opts
->ass_top_margin
, opts
->ass_bottom_margin
, 0, 0);
251 ass_set_use_margins(priv
, opts
->ass_use_margins
);
252 ass_set_font_scale(priv
, opts
->ass_font_scale
);
253 if (!unscaled
&& (opts
->ass_hinting
& 4))
256 hinting
= opts
->ass_hinting
& 3;
257 ass_set_hinting(priv
, hinting
);
258 ass_set_line_spacing(priv
, opts
->ass_line_spacing
);
261 void mp_ass_configure_fonts(ASS_Renderer
*priv
)
263 char *dir
, *path
, *family
;
264 dir
= get_path("fonts");
265 if (font_fontconfig
< 0 && sub_font_name
)
266 path
= strdup(sub_font_name
);
267 else if (font_fontconfig
< 0 && font_name
)
268 path
= strdup(font_name
);
270 path
= get_path("subfont.ttf");
271 if (font_fontconfig
>= 0 && sub_font_name
)
272 family
= strdup(sub_font_name
);
273 else if (font_fontconfig
>= 0 && font_name
)
274 family
= strdup(font_name
);
278 ass_set_fonts(priv
, path
, family
, font_fontconfig
+ 1, NULL
, 1);
285 static void message_callback(int level
, const char *format
, va_list va
, void *ctx
)
287 mp_msg(MSGT_ASS
, level
, "[ass] ");
288 mp_msg_va(MSGT_ASS
, level
, format
, va
);
289 // libass messages lack trailing \n
290 mp_msg(MSGT_ASS
, level
, "\n");
293 ASS_Library
*mp_ass_init(struct MPOpts
*opts
)
296 char *path
= get_path("fonts");
297 priv
= ass_library_init();
298 ass_set_message_cb(priv
, message_callback
, NULL
);
299 ass_set_fonts_dir(priv
, path
);
300 ass_set_extract_fonts(priv
, opts
->use_embedded_fonts
);
305 void mp_ass_reload_options(ASS_Renderer
*priv
, struct MPOpts
*opts
)
307 /* This could be needed for vf_ass case if the margins were actually
308 * runtime configurable, but would be wrong with EOSD:
309 * ass_set_margins(priv, opts->ass_top_margin, opts->ass_bottom_margin,
312 ass_set_use_margins(priv
, opts
->ass_use_margins
);
313 ass_set_font_scale(priv
, opts
->ass_font_scale
);