find_subfiles: try to determine if a .sub file is text or vobsub
[mplayer/greg.git] / sub / ass_mp.c
blob78f607bc7aa42f98dbec74118d627082e5207135
1 /*
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.
21 #include <inttypes.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdbool.h>
27 #include <ass/ass.h>
28 #include <ass/ass_types.h>
30 #include <libavutil/common.h>
32 #include "config.h"
33 #include "mp_msg.h"
34 #include "path.h"
35 #include "ass_mp.h"
36 #include "subreader.h"
37 #include "stream/stream.h"
39 #ifdef CONFIG_FONTCONFIG
40 #include <fontconfig/fontconfig.h>
41 #endif
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;
59 #else
60 static int font_fontconfig = -1;
61 #endif
62 extern char *font_name;
63 extern char *sub_font_name;
64 extern float text_font_scale_factor;
65 extern int subtitle_autoscale;
67 #ifdef CONFIG_ICONV
68 extern char *sub_cp;
69 #else
70 static char *sub_cp = 0;
71 #endif
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;
78 track->Timer = 100.;
79 track->PlayResY = 288;
80 track->WrapStyle = 0;
82 if (ass_styles_file)
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 ASS_Style *style = track->styles + sid;
89 style->Name = strdup("Default");
90 style->FontName = (font_fontconfig >= 0
91 && sub_font_name) ? strdup(sub_font_name)
92 : (font_fontconfig >= 0
93 && font_name) ? strdup(font_name) : strdup("Sans");
94 style->treat_fontname_as_pattern = 1;
96 double fs = track->PlayResY * text_font_scale_factor / 100.;
97 /* The font size is always proportional to video height only;
98 * real -subfont-autoscale behavior is not implemented.
99 * Apply a correction that corresponds to about 4:3 aspect ratio
100 * video to get a size somewhat closer to what non-libass rendering
101 * would produce with the same text_font_scale_factor
102 * and subtitle_autoscale.
104 if (subtitle_autoscale == 2)
105 fs *= 1.3;
106 else if (subtitle_autoscale == 3)
107 fs *= 1.7;
109 uint32_t c1 = 0xFFFFFF00;
110 uint32_t c2 = 0x00000000;
111 if (ass_color)
112 c1 = strtoll(ass_color, NULL, 16);
113 if (ass_border_color)
114 c2 = strtoll(ass_border_color, NULL, 16);
116 style->FontSize = fs;
117 style->PrimaryColour = c1;
118 style->SecondaryColour = c1;
119 style->OutlineColour = c2;
120 style->BackColour = 0x00000000;
121 style->BorderStyle = 1;
122 style->Alignment = 2;
123 style->Outline = fs / 16;
124 style->MarginL = 10;
125 style->MarginR = 10;
126 style->MarginV = 5;
127 style->ScaleX = 1.;
128 style->ScaleY = 1.;
131 ass_process_force_style(track);
132 return track;
135 static int check_duplicate_plaintext_event(ASS_Track *track)
137 int i;
138 ASS_Event *evt = track->events + track->n_events - 1;
140 for (i = 0; i < track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
141 if (track->events[i].Start == evt->Start &&
142 track->events[i].Duration == evt->Duration &&
143 strcmp(track->events[i].Text, evt->Text) == 0)
144 return 1;
145 return 0;
149 * \brief Convert subtitle to ASS_Events for the given track
150 * \param track track
151 * \param sub subtitle to convert
152 * \return event id
153 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
154 * Start and Duration in other case.
156 static int ass_process_subtitle(ASS_Track *track, subtitle *sub)
158 int eid;
159 ASS_Event *event;
160 int len = 0, j;
161 char *p;
162 char *end;
164 eid = ass_alloc_event(track);
165 event = track->events + eid;
167 event->Start = sub->start * 10;
168 event->Duration = (sub->end - sub->start) * 10;
169 event->Style = 0;
171 for (j = 0; j < sub->lines; ++j)
172 len += sub->text[j] ? strlen(sub->text[j]) : 0;
174 len += 2 * sub->lines; // '\N', including the one after the last line
175 len += 6; // {\anX}
176 len += 1; // '\0'
178 event->Text = malloc(len);
179 end = event->Text + len;
180 p = event->Text;
182 if (sub->alignment)
183 p += snprintf(p, end - p, "{\\an%d}", sub->alignment);
185 for (j = 0; j < sub->lines; ++j)
186 p += snprintf(p, end - p, "%s\\N", sub->text[j]);
188 if (sub->lines > 0)
189 p -= 2; // remove last "\N"
190 *p = 0;
192 if (check_duplicate_plaintext_event(track)) {
193 ass_free_event(track, eid);
194 track->n_events--;
195 return -1;
198 mp_msg(MSGT_ASS, MSGL_V,
199 "plaintext event at %" PRId64 ", +%" PRId64 ": %s \n",
200 (int64_t) event->Start, (int64_t) event->Duration, event->Text);
202 return eid;
207 * \brief Convert subdata to ASS_Track
208 * \param subdata subtitles struct from subreader
209 * \param fps video framerate
210 * \return newly allocated ASS_Track, filled with subtitles from subdata
212 ASS_Track *mp_ass_read_subdata(ASS_Library *library, sub_data *subdata,
213 double fps)
215 ASS_Track *track;
216 int i;
218 track = mp_ass_default_track(library);
219 track->name = subdata->filename ? strdup(subdata->filename) : 0;
221 for (i = 0; i < subdata->sub_num; ++i) {
222 int eid = ass_process_subtitle(track, subdata->subtitles + i);
223 if (eid < 0)
224 continue;
225 if (!subdata->sub_uses_time) {
226 track->events[eid].Start *= 100. / fps;
227 track->events[eid].Duration *= 100. / fps;
230 return track;
233 ASS_Track *mp_ass_read_stream(ASS_Library *library, const char *fname,
234 char *charset)
236 ASS_Track *track;
238 struct stream *s = open_stream(fname, NULL, NULL);
239 if (!s)
240 // Stream code should have printed an error already
241 return NULL;
242 struct bstr content = stream_read_complete(s, NULL, 100000000, 1);
243 if (content.start == NULL)
244 mp_tmsg(MSGT_ASS, MSGL_ERR, "Refusing to load subtitle file "
245 "larger than 100 MB: %s\n", fname);
246 free_stream(s);
247 if (content.len == 0) {
248 talloc_free(content.start);
249 return NULL;
251 content.start[content.len] = 0;
252 track = ass_read_memory(library, content.start, content.len, charset);
253 if (track) {
254 free(track->name);
255 track->name = strdup(fname);
257 talloc_free(content.start);
258 return track;
261 void mp_ass_configure(ASS_Renderer *priv, int w, int h, bool unscaled)
263 int hinting;
264 ass_set_frame_size(priv, w, h);
265 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
266 ass_set_use_margins(priv, ass_use_margins);
267 ass_set_font_scale(priv, ass_font_scale);
268 if (!unscaled && (ass_hinting & 4))
269 hinting = 0;
270 else
271 hinting = ass_hinting & 3;
272 ass_set_hinting(priv, hinting);
273 ass_set_line_spacing(priv, ass_line_spacing);
276 void mp_ass_configure_fonts(ASS_Renderer *priv)
278 char *dir, *path, *family;
279 dir = get_path("fonts");
280 if (font_fontconfig < 0 && sub_font_name)
281 path = strdup(sub_font_name);
282 else if (font_fontconfig < 0 && font_name)
283 path = strdup(font_name);
284 else
285 path = get_path("subfont.ttf");
286 if (font_fontconfig >= 0 && sub_font_name)
287 family = strdup(sub_font_name);
288 else if (font_fontconfig >= 0 && font_name)
289 family = strdup(font_name);
290 else
291 family = 0;
293 ass_set_fonts(priv, path, family, font_fontconfig + 1, NULL, 1);
295 free(dir);
296 free(path);
297 free(family);
300 static void message_callback(int level, const char *format, va_list va, void *ctx)
302 mp_msg(MSGT_ASS, level, "[ass] ");
303 mp_msg_va(MSGT_ASS, level, format, va);
304 // libass messages lack trailing \n
305 mp_msg(MSGT_ASS, level, "\n");
308 ASS_Library *mp_ass_init(void)
310 ASS_Library *priv;
311 char *path = get_path("fonts");
312 priv = ass_library_init();
313 ass_set_message_cb(priv, message_callback, NULL);
314 ass_set_fonts_dir(priv, path);
315 ass_set_extract_fonts(priv, use_embedded_fonts);
316 ass_set_style_overrides(priv, ass_force_style_list);
317 free(path);
318 return priv;
321 int ass_force_reload = 0; // flag set if global ass-related settings were changed
323 ASS_Image *mp_ass_render_frame(ASS_Renderer *priv, ASS_Track *track,
324 long long now, int *detect_change)
326 if (ass_force_reload) {
327 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
328 ass_set_use_margins(priv, ass_use_margins);
329 ass_set_font_scale(priv, ass_font_scale);
330 ass_force_reload = 0;
332 return ass_render_frame(priv, track, now, detect_change);