cleanup: rename ass_* functions to mp_ass_*
[mplayer/glamo.git] / ass_mp.c
blob37e688d7f127bb2d1827036a60d1f9bca7a831b0
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 void process_force_style(ASS_Track *track);
75 ASS_Track *mp_ass_default_track(ASS_Library *library)
77 ASS_Track *track = ass_new_track(library);
79 track->track_type = TRACK_TYPE_ASS;
80 track->Timer = 100.;
81 track->PlayResY = 288;
82 track->WrapStyle = 0;
84 if (ass_styles_file)
85 ass_read_styles(track, ass_styles_file, sub_cp);
87 if (track->n_styles == 0) {
88 ASS_Style *style;
89 int sid;
90 double fs;
91 uint32_t c1, c2;
93 sid = ass_alloc_style(track);
94 style = track->styles + sid;
95 style->Name = strdup("Default");
96 style->FontName = (font_fontconfig >= 0
97 && sub_font_name) ? strdup(sub_font_name)
98 : (font_fontconfig >= 0
99 && font_name) ? strdup(font_name) : strdup("Sans");
100 style->treat_fontname_as_pattern = 1;
102 fs = track->PlayResY * text_font_scale_factor / 100.;
103 // approximate autoscale coefficients
104 if (subtitle_autoscale == 2)
105 fs *= 1.3;
106 else if (subtitle_autoscale == 3)
107 fs *= 1.4;
108 style->FontSize = fs;
110 if (ass_color)
111 c1 = strtoll(ass_color, NULL, 16);
112 else
113 c1 = 0xFFFF0000;
114 if (ass_border_color)
115 c2 = strtoll(ass_border_color, NULL, 16);
116 else
117 c2 = 0x00000000;
119 style->PrimaryColour = c1;
120 style->SecondaryColour = c1;
121 style->OutlineColour = c2;
122 style->BackColour = 0x00000000;
123 style->BorderStyle = 1;
124 style->Alignment = 2;
125 style->Outline = 2;
126 style->MarginL = 10;
127 style->MarginR = 10;
128 style->MarginV = 5;
129 style->ScaleX = 1.;
130 style->ScaleY = 1.;
133 ass_process_force_style(track);
134 return track;
137 static int check_duplicate_plaintext_event(ASS_Track *track)
139 int i;
140 ASS_Event *evt = track->events + track->n_events - 1;
142 for (i = 0; i < track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
143 if (track->events[i].Start == evt->Start &&
144 track->events[i].Duration == evt->Duration &&
145 strcmp(track->events[i].Text, evt->Text) == 0)
146 return 1;
147 return 0;
151 * \brief Convert subtitle to ASS_Events for the given track
152 * \param track track
153 * \param sub subtitle to convert
154 * \return event id
155 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
156 * Start and Duration in other case.
158 static int ass_process_subtitle(ASS_Track *track, subtitle *sub)
160 int eid;
161 ASS_Event *event;
162 int len = 0, j;
163 char *p;
164 char *end;
166 eid = ass_alloc_event(track);
167 event = track->events + eid;
169 event->Start = sub->start * 10;
170 event->Duration = (sub->end - sub->start) * 10;
171 event->Style = 0;
173 for (j = 0; j < sub->lines; ++j)
174 len += sub->text[j] ? strlen(sub->text[j]) : 0;
176 len += 2 * sub->lines; // '\N', including the one after the last line
177 len += 6; // {\anX}
178 len += 1; // '\0'
180 event->Text = malloc(len);
181 end = event->Text + len;
182 p = event->Text;
184 if (sub->alignment)
185 p += snprintf(p, end - p, "{\\an%d}", sub->alignment);
187 for (j = 0; j < sub->lines; ++j)
188 p += snprintf(p, end - p, "%s\\N", sub->text[j]);
190 if (sub->lines > 0)
191 p -= 2; // remove last "\N"
192 *p = 0;
194 if (check_duplicate_plaintext_event(track)) {
195 ass_free_event(track, eid);
196 track->n_events--;
197 return -1;
200 mp_msg(MSGT_ASS, MSGL_V,
201 "plaintext event at %" PRId64 ", +%" PRId64 ": %s \n",
202 (int64_t) event->Start, (int64_t) event->Duration, event->Text);
204 return eid;
209 * \brief Convert subdata to ASS_Track
210 * \param subdata subtitles struct from subreader
211 * \param fps video framerate
212 * \return newly allocated ASS_Track, filled with subtitles from subdata
214 ASS_Track *mp_ass_read_subdata(ASS_Library *library, sub_data *subdata,
215 double fps)
217 ASS_Track *track;
218 int i;
220 track = mp_ass_default_track(library);
221 track->name = subdata->filename ? strdup(subdata->filename) : 0;
223 for (i = 0; i < subdata->sub_num; ++i) {
224 int eid = ass_process_subtitle(track, subdata->subtitles + i);
225 if (eid < 0)
226 continue;
227 if (!subdata->sub_uses_time) {
228 track->events[eid].Start *= 100. / fps;
229 track->events[eid].Duration *= 100. / fps;
232 return track;
235 ASS_Track *mp_ass_read_stream(ASS_Library *library, const char *fname,
236 char *charset)
238 int i;
239 char *buf = NULL;
240 ASS_Track *track;
241 size_t sz = 0;
242 size_t buf_alloc = 0;
243 stream_t *fd;
245 fd = open_stream(fname, NULL, NULL);
246 if (!fd)
247 // Stream code should have printed an error already
248 return NULL;
249 if (fd->end_pos > STREAM_BUFFER_SIZE)
250 /* read entire file if size is known */
251 buf_alloc = fd->end_pos;
252 else
253 buf_alloc = 1000;
254 for (;;) {
255 if (sz > 100000000) {
256 mp_tmsg(MSGT_ASS, MSGL_ERR, "Refusing to load subtitle file "
257 "larger than 100 MB: %s\n", fname);
258 sz = 0;
259 break;
261 buf_alloc = FFMAX(buf_alloc, sz + (sz >> 1));
262 buf_alloc = FFMIN(buf_alloc, 100000001);
263 buf = realloc(buf, buf_alloc + 1);
264 i = stream_read(fd, buf + sz, buf_alloc - sz);
265 if (i <= 0)
266 break;
267 sz += i;
269 free_stream(fd);
270 if (!sz) {
271 free(buf);
272 return NULL;
274 buf[sz] = 0;
275 buf = realloc(buf, sz + 1);
276 track = ass_read_memory(library, buf, sz, charset);
277 if (track) {
278 free(track->name);
279 track->name = strdup(fname);
281 free(buf);
282 return track;
285 void mp_ass_configure(ASS_Renderer *priv, int w, int h, bool unscaled)
287 int hinting;
288 ass_set_frame_size(priv, w, h);
289 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
290 ass_set_use_margins(priv, ass_use_margins);
291 ass_set_font_scale(priv, ass_font_scale);
292 if (!unscaled && (ass_hinting & 4))
293 hinting = 0;
294 else
295 hinting = ass_hinting & 3;
296 ass_set_hinting(priv, hinting);
297 ass_set_line_spacing(priv, ass_line_spacing);
300 void mp_ass_configure_fonts(ASS_Renderer *priv)
302 char *dir, *path, *family;
303 dir = get_path("fonts");
304 if (font_fontconfig < 0 && sub_font_name)
305 path = strdup(sub_font_name);
306 else if (font_fontconfig < 0 && font_name)
307 path = strdup(font_name);
308 else
309 path = get_path("subfont.ttf");
310 if (font_fontconfig >= 0 && sub_font_name)
311 family = strdup(sub_font_name);
312 else if (font_fontconfig >= 0 && font_name)
313 family = strdup(font_name);
314 else
315 family = 0;
317 ass_set_fonts(priv, path, family, font_fontconfig + 1, NULL, 1);
319 free(dir);
320 free(path);
321 free(family);
324 static void message_callback(int level, const char *format, va_list va, void *ctx)
326 mp_msg(MSGT_ASS, level, "[ass] ");
327 mp_msg_va(MSGT_ASS, level, format, va);
328 // libass messages lack trailing \n
329 mp_msg(MSGT_ASS, level, "\n");
332 ASS_Library *mp_ass_init(void)
334 ASS_Library *priv;
335 char *path = get_path("fonts");
336 priv = ass_library_init();
337 ass_set_message_cb(priv, message_callback, NULL);
338 ass_set_fonts_dir(priv, path);
339 ass_set_extract_fonts(priv, use_embedded_fonts);
340 ass_set_style_overrides(priv, ass_force_style_list);
341 free(path);
342 return priv;
345 int ass_force_reload = 0; // flag set if global ass-related settings were changed
347 ASS_Image *mp_ass_render_frame(ASS_Renderer *priv, ASS_Track *track,
348 long long now, int *detect_change)
350 if (ass_force_reload) {
351 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
352 ass_set_use_margins(priv, ass_use_margins);
353 ass_set_font_scale(priv, ass_font_scale);
354 ass_force_reload = 0;
356 return ass_render_frame(priv, track, now, detect_change);