commands: Allow fullscreen switching during -idle.
[mplayer/glamo.git] / ass_mp.c
blob4161397c95fa9c64b89f5e84e5ef30a14c36a0dc
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>
26 #include <ass/ass.h>
27 #include <ass/ass_types.h>
29 #include <libavutil/common.h>
31 #include "mp_msg.h"
32 #include "path.h"
33 #include "ass_mp.h"
34 #include "subreader.h"
35 #include "stream/stream.h"
37 #ifdef CONFIG_FONTCONFIG
38 #include <fontconfig/fontconfig.h>
39 #endif
41 // libass-related command line options
42 ASS_Library *ass_library;
43 float ass_font_scale = 1.;
44 float ass_line_spacing = 0.;
45 int ass_top_margin = 0;
46 int ass_bottom_margin = 0;
47 int use_embedded_fonts = 1;
48 char **ass_force_style_list = NULL;
49 int ass_use_margins = 0;
50 char *ass_color = NULL;
51 char *ass_border_color = NULL;
52 char *ass_styles_file = NULL;
53 int ass_hinting = ASS_HINTING_LIGHT + 4; // light hinting for unscaled osd
55 #ifdef CONFIG_FONTCONFIG
56 extern int font_fontconfig;
57 #else
58 static int font_fontconfig = -1;
59 #endif
60 extern char *font_name;
61 extern char *sub_font_name;
62 extern float text_font_scale_factor;
63 extern int subtitle_autoscale;
65 #ifdef CONFIG_ICONV
66 extern char *sub_cp;
67 #else
68 static char *sub_cp = 0;
69 #endif
71 void process_force_style(ASS_Track *track);
73 ASS_Track *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 ASS_Style *style;
87 int sid;
88 double fs;
89 uint32_t c1, c2;
91 sid = ass_alloc_style(track);
92 style = track->styles + sid;
93 style->Name = strdup("Default");
94 style->FontName = (font_fontconfig >= 0
95 && sub_font_name) ? strdup(sub_font_name)
96 : (font_fontconfig >= 0
97 && font_name) ? strdup(font_name) : strdup("Sans");
98 style->treat_fontname_as_pattern = 1;
100 fs = track->PlayResY * text_font_scale_factor / 100.;
101 // approximate autoscale coefficients
102 if (subtitle_autoscale == 2)
103 fs *= 1.3;
104 else if (subtitle_autoscale == 3)
105 fs *= 1.4;
106 style->FontSize = fs;
108 if (ass_color)
109 c1 = strtoll(ass_color, NULL, 16);
110 else
111 c1 = 0xFFFF0000;
112 if (ass_border_color)
113 c2 = strtoll(ass_border_color, NULL, 16);
114 else
115 c2 = 0x00000000;
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 = 2;
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 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 *ass_read_subdata(ASS_Library *library, sub_data *subdata,
213 double fps)
215 ASS_Track *track;
216 int i;
218 track = 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 *ass_read_stream(ASS_Library *library, const char *fname, char *charset)
235 int i;
236 char *buf = NULL;
237 ASS_Track *track;
238 size_t sz = 0;
239 size_t buf_alloc = 0;
240 stream_t *fd;
242 fd = open_stream(fname, NULL, &i);
243 if (!fd)
244 // Stream code should have printed an error already
245 return NULL;
246 if (fd->end_pos > STREAM_BUFFER_SIZE)
247 /* read entire file if size is known */
248 buf_alloc = fd->end_pos;
249 else
250 buf_alloc = 1000;
251 for (;;) {
252 if (sz > 100000000) {
253 mp_tmsg(MSGT_ASS, MSGL_ERR, "Refusing to load subtitle file "
254 "larger than 100 MB: %s\n", fname);
255 sz = 0;
256 break;
258 buf_alloc = FFMAX(buf_alloc, sz + (sz >> 1));
259 buf_alloc = FFMIN(buf_alloc, 100000001);
260 buf = realloc(buf, buf_alloc + 1);
261 i = stream_read(fd, buf + sz, buf_alloc - sz);
262 if (i <= 0)
263 break;
264 sz += i;
266 free_stream(fd);
267 if (!sz) {
268 free(buf);
269 return NULL;
271 buf[sz] = 0;
272 buf = realloc(buf, sz + 1);
273 track = ass_read_memory(library, buf, sz, charset);
274 if (track) {
275 free(track->name);
276 track->name = strdup(fname);
278 free(buf);
279 return track;
282 void ass_configure(ASS_Renderer *priv, int w, int h, int unscaled)
284 int hinting;
285 ass_set_frame_size(priv, w, h);
286 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
287 ass_set_use_margins(priv, ass_use_margins);
288 ass_set_font_scale(priv, ass_font_scale);
289 if (!unscaled && (ass_hinting & 4))
290 hinting = 0;
291 else
292 hinting = ass_hinting & 3;
293 ass_set_hinting(priv, hinting);
294 ass_set_line_spacing(priv, ass_line_spacing);
297 void ass_configure_fonts(ASS_Renderer *priv)
299 char *dir, *path, *family;
300 dir = get_path("fonts");
301 if (font_fontconfig < 0 && sub_font_name)
302 path = strdup(sub_font_name);
303 else if (font_fontconfig < 0 && font_name)
304 path = strdup(font_name);
305 else
306 path = get_path("subfont.ttf");
307 if (font_fontconfig >= 0 && sub_font_name)
308 family = strdup(sub_font_name);
309 else if (font_fontconfig >= 0 && font_name)
310 family = strdup(font_name);
311 else
312 family = 0;
314 ass_set_fonts(priv, path, family, font_fontconfig + 1, NULL, 1);
316 free(dir);
317 free(path);
318 free(family);
321 static void message_callback(int level, const char *format, va_list va, void *ctx)
323 mp_msg(MSGT_ASS, level, "[ass] ");
324 mp_msg_va(MSGT_ASS, level, format, va);
325 // libass messages lack trailing \n
326 mp_msg(MSGT_ASS, level, "\n");
329 ASS_Library *ass_init(void)
331 ASS_Library *priv;
332 char *path = get_path("fonts");
333 priv = ass_library_init();
334 ass_set_message_cb(priv, message_callback, NULL);
335 ass_set_fonts_dir(priv, path);
336 ass_set_extract_fonts(priv, use_embedded_fonts);
337 ass_set_style_overrides(priv, ass_force_style_list);
338 free(path);
339 return priv;
342 int ass_force_reload = 0; // flag set if global ass-related settings were changed
344 ASS_Image *ass_mp_render_frame(ASS_Renderer *priv, ASS_Track *track,
345 long long now, int *detect_change)
347 if (ass_force_reload) {
348 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
349 ass_set_use_margins(priv, ass_use_margins);
350 ass_set_font_scale(priv, ass_font_scale);
351 ass_force_reload = 0;
353 return ass_render_frame(priv, track, now, detect_change);