x264 version 0.83 is required.
[mplayer/glamo.git] / libass / ass_mp.c
blobb5f55cc29a4013f9b52fa7e43bd548fc68320432
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
2 // vim:ts=8:sw=8:noet:ai:
3 /*
4 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
6 * This file is part of libass.
8 * libass is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * libass is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with libass; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <inttypes.h>
24 #include <string.h>
25 #include <stdlib.h>
27 #include "mp_msg.h"
28 #include "get_path.h"
30 #include "ass_mp.h"
32 #ifdef CONFIG_FONTCONFIG
33 #include <fontconfig/fontconfig.h>
34 #endif
36 // libass-related command line options
37 ass_library_t* ass_library;
38 int ass_enabled = 0;
39 float ass_font_scale = 1.;
40 float ass_line_spacing = 0.;
41 int ass_top_margin = 0;
42 int ass_bottom_margin = 0;
43 #if defined(FC_VERSION) && (FC_VERSION >= 20402)
44 int extract_embedded_fonts = 1;
45 #else
46 int extract_embedded_fonts = 0;
47 #endif
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_NATIVE + 4; // native 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 ass_track_t* ass_default_track(ass_library_t* library) {
72 ass_track_t* 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_t* 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 && sub_font_name) ? strdup(sub_font_name) : (font_fontconfig >= 0 && font_name) ? strdup(font_name) : strdup("Sans");
92 style->treat_fontname_as_pattern = 1;
94 fs = track->PlayResY * text_font_scale_factor / 100.;
95 // approximate autoscale coefficients
96 if (subtitle_autoscale == 2)
97 fs *= 1.3;
98 else if (subtitle_autoscale == 3)
99 fs *= 1.4;
100 style->FontSize = fs;
102 if (ass_color) c1 = strtoll(ass_color, NULL, 16);
103 else c1 = 0xFFFF0000;
104 if (ass_border_color) c2 = strtoll(ass_border_color, NULL, 16);
105 else c2 = 0x00000000;
107 style->PrimaryColour = c1;
108 style->SecondaryColour = c1;
109 style->OutlineColour = c2;
110 style->BackColour = 0x00000000;
111 style->BorderStyle = 1;
112 style->Alignment = 2;
113 style->Outline = 2;
114 style->MarginL = 10;
115 style->MarginR = 10;
116 style->MarginV = 5;
117 style->ScaleX = 1.;
118 style->ScaleY = 1.;
121 process_force_style(track);
122 return track;
125 static int check_duplicate_plaintext_event(ass_track_t* track)
127 int i;
128 ass_event_t* evt = track->events + track->n_events - 1;
130 for (i = 0; i<track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
131 if (track->events[i].Start == evt->Start &&
132 track->events[i].Duration == evt->Duration &&
133 strcmp(track->events[i].Text, evt->Text) == 0)
134 return 1;
135 return 0;
139 * \brief Convert subtitle to ass_event_t for the given track
140 * \param ass_track_t track
141 * \param sub subtitle to convert
142 * \return event id
143 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
144 * Start and Duration in other case.
146 int ass_process_subtitle(ass_track_t* track, subtitle* sub)
148 int eid;
149 ass_event_t* event;
150 int len = 0, j;
151 char* p;
152 char* end;
154 eid = ass_alloc_event(track);
155 event = track->events + eid;
157 event->Start = sub->start * 10;
158 event->Duration = (sub->end - sub->start) * 10;
159 event->Style = 0;
161 for (j = 0; j < sub->lines; ++j)
162 len += sub->text[j] ? strlen(sub->text[j]) : 0;
164 len += 2 * sub->lines; // '\N', including the one after the last line
165 len += 6; // {\anX}
166 len += 1; // '\0'
168 event->Text = malloc(len);
169 end = event->Text + len;
170 p = event->Text;
172 if (sub->alignment)
173 p += snprintf(p, end - p, "{\\an%d}", sub->alignment);
175 for (j = 0; j < sub->lines; ++j)
176 p += snprintf(p, end - p, "%s\\N", sub->text[j]);
178 if (sub->lines > 0) p-=2; // remove last "\N"
179 *p = 0;
181 if (check_duplicate_plaintext_event(track)) {
182 ass_free_event(track, eid);
183 track->n_events--;
184 return -1;
187 mp_msg(MSGT_ASS, MSGL_V, "plaintext event at %" PRId64 ", +%" PRId64 ": %s \n",
188 (int64_t)event->Start, (int64_t)event->Duration, event->Text);
190 return eid;
195 * \brief Convert subdata to ass_track
196 * \param subdata subtitles struct from subreader
197 * \param fps video framerate
198 * \return newly allocated ass_track, filled with subtitles from subdata
200 ass_track_t* ass_read_subdata(ass_library_t* library, sub_data* subdata, double fps) {
201 ass_track_t* track;
202 int i;
204 track = ass_default_track(library);
205 track->name = subdata->filename ? strdup(subdata->filename) : 0;
207 for (i = 0; i < subdata->sub_num; ++i) {
208 int eid = ass_process_subtitle(track, subdata->subtitles + i);
209 if (eid < 0)
210 continue;
211 if (!subdata->sub_uses_time) {
212 track->events[eid].Start *= 100. / fps;
213 track->events[eid].Duration *= 100. / fps;
216 return track;
219 void ass_configure(ass_renderer_t* priv, int w, int h, int unscaled) {
220 int hinting;
221 ass_set_frame_size(priv, w, h);
222 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
223 ass_set_use_margins(priv, ass_use_margins);
224 ass_set_font_scale(priv, ass_font_scale);
225 if (!unscaled && (ass_hinting & 4))
226 hinting = 0;
227 else
228 hinting = ass_hinting & 3;
229 ass_set_hinting(priv, hinting);
230 ass_set_line_spacing(priv, ass_line_spacing);
233 void ass_configure_fonts(ass_renderer_t* priv) {
234 char *dir, *path, *family;
235 dir = get_path("fonts");
236 if (font_fontconfig < 0 && sub_font_name) path = strdup(sub_font_name);
237 else if (font_fontconfig < 0 && font_name) path = strdup(font_name);
238 else path = get_path("subfont.ttf");
239 if (font_fontconfig >= 0 && sub_font_name) family = strdup(sub_font_name);
240 else if (font_fontconfig >= 0 && font_name) family = strdup(font_name);
241 else family = 0;
243 #if defined(LIBASS_VERSION) && LIBASS_VERSION >= 0x00907010
244 ass_set_fonts(priv, path, family, font_fontconfig, NULL, 1);
245 #else
246 if (font_fontconfig >= 0)
247 ass_set_fonts(priv, path, family);
248 else
249 ass_set_fonts_nofc(priv, path, family);
250 #endif
252 free(dir);
253 free(path);
254 free(family);
257 ass_library_t* ass_init(void) {
258 ass_library_t* priv;
259 char* path = get_path("fonts");
260 priv = ass_library_init();
261 ass_set_fonts_dir(priv, path);
262 ass_set_extract_fonts(priv, extract_embedded_fonts);
263 ass_set_style_overrides(priv, ass_force_style_list);
264 free(path);
265 return priv;
268 int ass_force_reload = 0; // flag set if global ass-related settings were changed
270 ass_image_t* ass_mp_render_frame(ass_renderer_t *priv, ass_track_t* track, long long now, int* detect_change) {
271 if (ass_force_reload) {
272 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
273 ass_set_use_margins(priv, ass_use_margins);
274 ass_set_font_scale(priv, ass_font_scale);
275 ass_force_reload = 0;
277 return ass_render_frame(priv, track, now, detect_change);