Disable console colors by default
[mplayer/kovensky.git] / ass_mp.c
blob959f985446516d7ebd111437c0f4c3f553357637
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 "mp_msg.h"
30 #include "get_path.h"
31 #include "ass_mp.h"
32 #include "subreader.h"
34 #ifdef CONFIG_FONTCONFIG
35 #include <fontconfig/fontconfig.h>
36 #endif
38 // libass-related command line options
39 ASS_Library *ass_library;
40 float ass_font_scale = 1.;
41 float ass_line_spacing = 0.;
42 int ass_top_margin = 0;
43 int ass_bottom_margin = 0;
44 int use_embedded_fonts = 1;
45 char **ass_force_style_list = NULL;
46 int ass_use_margins = 0;
47 char *ass_color = NULL;
48 char *ass_border_color = NULL;
49 char *ass_styles_file = NULL;
50 int ass_hinting = ASS_HINTING_LIGHT + 4; // light hinting for unscaled osd
52 #ifdef CONFIG_FONTCONFIG
53 extern int font_fontconfig;
54 #else
55 static int font_fontconfig = -1;
56 #endif
57 extern char *font_name;
58 extern char *sub_font_name;
59 extern float text_font_scale_factor;
60 extern int subtitle_autoscale;
62 #ifdef CONFIG_ICONV
63 extern char *sub_cp;
64 #else
65 static char *sub_cp = 0;
66 #endif
68 void process_force_style(ASS_Track *track);
70 ASS_Track *ass_default_track(ASS_Library *library)
72 ASS_Track *track = ass_new_track(library);
74 track->track_type = TRACK_TYPE_ASS;
75 track->Timer = 100.;
76 track->PlayResY = 288;
77 track->WrapStyle = 0;
79 if (ass_styles_file)
80 ass_read_styles(track, ass_styles_file, sub_cp);
82 if (track->n_styles == 0) {
83 ASS_Style *style;
84 int sid;
85 double fs;
86 uint32_t c1, c2;
88 sid = ass_alloc_style(track);
89 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 fs = track->PlayResY * text_font_scale_factor / 100.;
98 // approximate autoscale coefficients
99 if (subtitle_autoscale == 2)
100 fs *= 1.3;
101 else if (subtitle_autoscale == 3)
102 fs *= 1.4;
103 style->FontSize = fs;
105 if (ass_color)
106 c1 = strtoll(ass_color, NULL, 16);
107 else
108 c1 = 0xFFFF0000;
109 if (ass_border_color)
110 c2 = strtoll(ass_border_color, NULL, 16);
111 else
112 c2 = 0x00000000;
114 style->PrimaryColour = c1;
115 style->SecondaryColour = c1;
116 style->OutlineColour = c2;
117 style->BackColour = 0x00000000;
118 style->BorderStyle = 1;
119 style->Alignment = 2;
120 style->Outline = 2;
121 style->MarginL = 10;
122 style->MarginR = 10;
123 style->MarginV = 5;
124 style->ScaleX = 1.;
125 style->ScaleY = 1.;
128 ass_process_force_style(track);
129 return track;
132 static int check_duplicate_plaintext_event(ASS_Track *track)
134 int i;
135 ASS_Event *evt = track->events + track->n_events - 1;
137 for (i = 0; i < track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
138 if (track->events[i].Start == evt->Start &&
139 track->events[i].Duration == evt->Duration &&
140 strcmp(track->events[i].Text, evt->Text) == 0)
141 return 1;
142 return 0;
146 * \brief Convert subtitle to ASS_Events for the given track
147 * \param track track
148 * \param sub subtitle to convert
149 * \return event id
150 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
151 * Start and Duration in other case.
153 int ass_process_subtitle(ASS_Track *track, subtitle *sub)
155 int eid;
156 ASS_Event *event;
157 int len = 0, j;
158 char *p;
159 char *end;
161 eid = ass_alloc_event(track);
162 event = track->events + eid;
164 event->Start = sub->start * 10;
165 event->Duration = (sub->end - sub->start) * 10;
166 event->Style = 0;
168 for (j = 0; j < sub->lines; ++j)
169 len += sub->text[j] ? strlen(sub->text[j]) : 0;
171 len += 2 * sub->lines; // '\N', including the one after the last line
172 len += 6; // {\anX}
173 len += 1; // '\0'
175 event->Text = malloc(len);
176 end = event->Text + len;
177 p = event->Text;
179 if (sub->alignment)
180 p += snprintf(p, end - p, "{\\an%d}", sub->alignment);
182 for (j = 0; j < sub->lines; ++j)
183 p += snprintf(p, end - p, "%s\\N", sub->text[j]);
185 if (sub->lines > 0)
186 p -= 2; // remove last "\N"
187 *p = 0;
189 if (check_duplicate_plaintext_event(track)) {
190 ass_free_event(track, eid);
191 track->n_events--;
192 return -1;
195 mp_msg(MSGT_ASS, MSGL_V,
196 "plaintext event at %" PRId64 ", +%" PRId64 ": %s \n",
197 (int64_t) event->Start, (int64_t) event->Duration, event->Text);
199 return eid;
204 * \brief Convert subdata to ASS_Track
205 * \param subdata subtitles struct from subreader
206 * \param fps video framerate
207 * \return newly allocated ASS_Track, filled with subtitles from subdata
209 ASS_Track *ass_read_subdata(ASS_Library *library, sub_data *subdata,
210 double fps)
212 ASS_Track *track;
213 int i;
215 track = ass_default_track(library);
216 track->name = subdata->filename ? strdup(subdata->filename) : 0;
218 for (i = 0; i < subdata->sub_num; ++i) {
219 int eid = ass_process_subtitle(track, subdata->subtitles + i);
220 if (eid < 0)
221 continue;
222 if (!subdata->sub_uses_time) {
223 track->events[eid].Start *= 100. / fps;
224 track->events[eid].Duration *= 100. / fps;
227 return track;
230 void ass_configure(ASS_Renderer *priv, int w, int h, int unscaled)
232 int hinting;
233 ass_set_frame_size(priv, w, h);
234 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
235 ass_set_use_margins(priv, ass_use_margins);
236 ass_set_font_scale(priv, ass_font_scale);
237 if (!unscaled && (ass_hinting & 4))
238 hinting = 0;
239 else
240 hinting = ass_hinting & 3;
241 ass_set_hinting(priv, hinting);
242 ass_set_line_spacing(priv, ass_line_spacing);
245 void ass_configure_fonts(ASS_Renderer *priv)
247 char *dir, *path, *family;
248 dir = get_path("fonts");
249 if (font_fontconfig < 0 && sub_font_name)
250 path = strdup(sub_font_name);
251 else if (font_fontconfig < 0 && font_name)
252 path = strdup(font_name);
253 else
254 path = get_path("subfont.ttf");
255 if (font_fontconfig >= 0 && sub_font_name)
256 family = strdup(sub_font_name);
257 else if (font_fontconfig >= 0 && font_name)
258 family = strdup(font_name);
259 else
260 family = 0;
262 ass_set_fonts(priv, path, family, font_fontconfig + 1, NULL, 1);
264 free(dir);
265 free(path);
266 free(family);
269 static void message_callback(int level, const char *format, va_list va, void *ctx)
271 mp_msg(MSGT_ASS, level, "[ass] ");
272 mp_msg_va(MSGT_ASS, level, format, va);
273 // libass messages lack trailing \n
274 mp_msg(MSGT_ASS, level, "\n");
277 ASS_Library *ass_init(void)
279 ASS_Library *priv;
280 char *path = get_path("fonts");
281 priv = ass_library_init();
282 ass_set_message_cb(priv, message_callback, NULL);
283 ass_set_fonts_dir(priv, path);
284 ass_set_extract_fonts(priv, use_embedded_fonts);
285 ass_set_style_overrides(priv, ass_force_style_list);
286 free(path);
287 return priv;
290 int ass_force_reload = 0; // flag set if global ass-related settings were changed
292 ASS_Image *ass_mp_render_frame(ASS_Renderer *priv, ASS_Track *track,
293 long long now, int *detect_change)
295 if (ass_force_reload) {
296 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
297 ass_set_use_margins(priv, ass_use_margins);
298 ass_set_font_scale(priv, ass_font_scale);
299 ass_force_reload = 0;
301 return ass_render_frame(priv, track, now, detect_change);