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"
39 #ifdef CONFIG_FONTCONFIG
40 #include <fontconfig/fontconfig.h>
43 // libass-related command line options
44 ASS_Library
*ass_library
;
45 float ass_font_scale
= 1.;
46 float ass_line_spacing
= 0.;
47 int ass_top_margin
= 0;
48 int ass_bottom_margin
= 0;
49 int use_embedded_fonts
= 1;
50 char **ass_force_style_list
= NULL
;
51 int ass_use_margins
= 0;
52 char *ass_color
= NULL
;
53 char *ass_border_color
= NULL
;
54 char *ass_styles_file
= NULL
;
55 int ass_hinting
= ASS_HINTING_LIGHT
+ 4; // light hinting for unscaled osd
57 #ifdef CONFIG_FONTCONFIG
58 extern int font_fontconfig
;
60 static int font_fontconfig
= -1;
62 extern char *font_name
;
63 extern char *sub_font_name
;
64 extern float text_font_scale_factor
;
65 extern int subtitle_autoscale
;
70 static char *sub_cp
= 0;
73 ASS_Track
*mp_ass_default_track(ASS_Library
*library
)
75 ASS_Track
*track
= ass_new_track(library
);
77 track
->track_type
= TRACK_TYPE_ASS
;
79 track
->PlayResY
= 288;
83 ass_read_styles(track
, ass_styles_file
, sub_cp
);
85 if (track
->n_styles
== 0) {
86 track
->Kerning
= true;
87 int sid
= ass_alloc_style(track
);
88 track
->default_style
= sid
;
89 ASS_Style
*style
= track
->styles
+ sid
;
90 style
->Name
= strdup("Default");
91 style
->FontName
= (font_fontconfig
>= 0
92 && sub_font_name
) ? strdup(sub_font_name
)
93 : (font_fontconfig
>= 0
94 && font_name
) ? strdup(font_name
) : strdup("Sans");
95 style
->treat_fontname_as_pattern
= 1;
97 double fs
= track
->PlayResY
* text_font_scale_factor
/ 100.;
98 /* The font size is always proportional to video height only;
99 * real -subfont-autoscale behavior is not implemented.
100 * Apply a correction that corresponds to about 4:3 aspect ratio
101 * video to get a size somewhat closer to what non-libass rendering
102 * would produce with the same text_font_scale_factor
103 * and subtitle_autoscale.
105 if (subtitle_autoscale
== 2)
107 else if (subtitle_autoscale
== 3)
110 uint32_t c1
= 0xFFFFFF00;
111 uint32_t c2
= 0x00000000;
113 c1
= strtoll(ass_color
, NULL
, 16);
114 if (ass_border_color
)
115 c2
= strtoll(ass_border_color
, NULL
, 16);
117 style
->FontSize
= fs
;
118 style
->PrimaryColour
= c1
;
119 style
->SecondaryColour
= c1
;
120 style
->OutlineColour
= c2
;
121 style
->BackColour
= 0x00000000;
122 style
->BorderStyle
= 1;
123 style
->Alignment
= 2;
124 style
->Outline
= fs
/ 16;
132 ass_process_force_style(track
);
136 static int check_duplicate_plaintext_event(ASS_Track
*track
)
139 ASS_Event
*evt
= track
->events
+ track
->n_events
- 1;
141 for (i
= 0; i
< track
->n_events
- 1; ++i
) // ignoring last event, it is the one we are comparing with
142 if (track
->events
[i
].Start
== evt
->Start
&&
143 track
->events
[i
].Duration
== evt
->Duration
&&
144 strcmp(track
->events
[i
].Text
, evt
->Text
) == 0)
150 * \brief Convert subtitle to ASS_Events for the given track
152 * \param sub subtitle to convert
154 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
155 * Start and Duration in other case.
157 static int ass_process_subtitle(ASS_Track
*track
, subtitle
*sub
)
165 eid
= ass_alloc_event(track
);
166 event
= track
->events
+ eid
;
168 event
->Start
= sub
->start
* 10;
169 event
->Duration
= (sub
->end
- sub
->start
) * 10;
170 event
->Style
= track
->default_style
;
172 for (j
= 0; j
< sub
->lines
; ++j
)
173 len
+= sub
->text
[j
] ? strlen(sub
->text
[j
]) : 0;
175 len
+= 2 * sub
->lines
; // '\N', including the one after the last line
179 event
->Text
= malloc(len
);
180 end
= event
->Text
+ len
;
184 p
+= snprintf(p
, end
- p
, "{\\an%d}", sub
->alignment
);
186 for (j
= 0; j
< sub
->lines
; ++j
)
187 p
+= snprintf(p
, end
- p
, "%s\\N", sub
->text
[j
]);
190 p
-= 2; // remove last "\N"
193 if (check_duplicate_plaintext_event(track
)) {
194 ass_free_event(track
, eid
);
199 mp_msg(MSGT_ASS
, MSGL_V
,
200 "plaintext event at %" PRId64
", +%" PRId64
": %s \n",
201 (int64_t) event
->Start
, (int64_t) event
->Duration
, event
->Text
);
208 * \brief Convert subdata to ASS_Track
209 * \param subdata subtitles struct from subreader
210 * \param fps video framerate
211 * \return newly allocated ASS_Track, filled with subtitles from subdata
213 ASS_Track
*mp_ass_read_subdata(ASS_Library
*library
, sub_data
*subdata
,
219 track
= mp_ass_default_track(library
);
220 track
->name
= subdata
->filename
? strdup(subdata
->filename
) : 0;
222 for (i
= 0; i
< subdata
->sub_num
; ++i
) {
223 int eid
= ass_process_subtitle(track
, subdata
->subtitles
+ i
);
226 if (!subdata
->sub_uses_time
) {
227 track
->events
[eid
].Start
*= 100. / fps
;
228 track
->events
[eid
].Duration
*= 100. / fps
;
234 ASS_Track
*mp_ass_read_stream(ASS_Library
*library
, const char *fname
,
239 struct stream
*s
= open_stream(fname
, NULL
, NULL
);
241 // Stream code should have printed an error already
243 struct bstr content
= stream_read_complete(s
, NULL
, 100000000, 1);
244 if (content
.start
== NULL
)
245 mp_tmsg(MSGT_ASS
, MSGL_ERR
, "Refusing to load subtitle file "
246 "larger than 100 MB: %s\n", fname
);
248 if (content
.len
== 0) {
249 talloc_free(content
.start
);
252 content
.start
[content
.len
] = 0;
253 track
= ass_read_memory(library
, content
.start
, content
.len
, charset
);
256 track
->name
= strdup(fname
);
258 talloc_free(content
.start
);
262 void mp_ass_configure(ASS_Renderer
*priv
, int w
, int h
, bool unscaled
)
265 ass_set_frame_size(priv
, w
, h
);
266 ass_set_margins(priv
, ass_top_margin
, ass_bottom_margin
, 0, 0);
267 ass_set_use_margins(priv
, ass_use_margins
);
268 ass_set_font_scale(priv
, ass_font_scale
);
269 if (!unscaled
&& (ass_hinting
& 4))
272 hinting
= ass_hinting
& 3;
273 ass_set_hinting(priv
, hinting
);
274 ass_set_line_spacing(priv
, ass_line_spacing
);
277 void mp_ass_configure_fonts(ASS_Renderer
*priv
)
279 char *dir
, *path
, *family
;
280 dir
= get_path("fonts");
281 if (font_fontconfig
< 0 && sub_font_name
)
282 path
= strdup(sub_font_name
);
283 else if (font_fontconfig
< 0 && font_name
)
284 path
= strdup(font_name
);
286 path
= get_path("subfont.ttf");
287 if (font_fontconfig
>= 0 && sub_font_name
)
288 family
= strdup(sub_font_name
);
289 else if (font_fontconfig
>= 0 && font_name
)
290 family
= strdup(font_name
);
294 ass_set_fonts(priv
, path
, family
, font_fontconfig
+ 1, NULL
, 1);
301 static void message_callback(int level
, const char *format
, va_list va
, void *ctx
)
303 mp_msg(MSGT_ASS
, level
, "[ass] ");
304 mp_msg_va(MSGT_ASS
, level
, format
, va
);
305 // libass messages lack trailing \n
306 mp_msg(MSGT_ASS
, level
, "\n");
309 ASS_Library
*mp_ass_init(void)
312 char *path
= get_path("fonts");
313 priv
= ass_library_init();
314 ass_set_message_cb(priv
, message_callback
, NULL
);
315 ass_set_fonts_dir(priv
, path
);
316 ass_set_extract_fonts(priv
, use_embedded_fonts
);
317 ass_set_style_overrides(priv
, ass_force_style_list
);
322 int ass_force_reload
= 0; // flag set if global ass-related settings were changed
324 ASS_Image
*mp_ass_render_frame(ASS_Renderer
*priv
, ASS_Track
*track
,
325 long long now
, int *detect_change
)
327 if (ass_force_reload
) {
328 ass_set_margins(priv
, ass_top_margin
, ass_bottom_margin
, 0, 0);
329 ass_set_use_margins(priv
, ass_use_margins
);
330 ass_set_font_scale(priv
, ass_font_scale
);
331 ass_force_reload
= 0;
333 return ass_render_frame(priv
, track
, now
, detect_change
);