Add const where appropriate, also gets rid of a compiler warning.
[mplayer/glamo.git] / libass / ass_mp.c
blobb3c78b60ac544a0fb7a8d6fe6af8edd6413908da
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.h"
31 #include "ass_utils.h"
32 #include "ass_mp.h"
33 #include "ass_library.h"
35 #ifdef CONFIG_FONTCONFIG
36 #include <fontconfig/fontconfig.h>
37 #endif
39 // libass-related command line options
40 ass_library_t* ass_library;
41 int ass_enabled = 0;
42 float ass_font_scale = 1.;
43 float ass_line_spacing = 0.;
44 int ass_top_margin = 0;
45 int ass_bottom_margin = 0;
46 #if defined(FC_VERSION) && (FC_VERSION >= 20402)
47 int extract_embedded_fonts = 1;
48 #else
49 int extract_embedded_fonts = 0;
50 #endif
51 char **ass_force_style_list = NULL;
52 int ass_use_margins = 0;
53 char* ass_color = NULL;
54 char* ass_border_color = NULL;
55 char* ass_styles_file = NULL;
56 int ass_hinting = ASS_HINTING_NATIVE + 4; // native hinting for unscaled osd
58 #ifdef CONFIG_FONTCONFIG
59 extern int font_fontconfig;
60 #else
61 static int font_fontconfig = -1;
62 #endif
63 extern char* font_name;
64 extern char* sub_font_name;
65 extern float text_font_scale_factor;
66 extern int subtitle_autoscale;
68 #ifdef CONFIG_ICONV
69 extern char* sub_cp;
70 #else
71 static char* sub_cp = 0;
72 #endif
74 void process_force_style(ass_track_t* track);
76 ass_track_t* ass_default_track(ass_library_t* library) {
77 ass_track_t* 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_t* 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 && sub_font_name) ? strdup(sub_font_name) : (font_fontconfig >= 0 && font_name) ? strdup(font_name) : strdup("Sans");
97 style->treat_fontname_as_pattern = 1;
99 fs = track->PlayResY * text_font_scale_factor / 100.;
100 // approximate autoscale coefficients
101 if (subtitle_autoscale == 2)
102 fs *= 1.3;
103 else if (subtitle_autoscale == 3)
104 fs *= 1.4;
105 style->FontSize = fs;
107 if (ass_color) c1 = strtoll(ass_color, NULL, 16);
108 else c1 = 0xFFFF0000;
109 if (ass_border_color) c2 = strtoll(ass_border_color, NULL, 16);
110 else c2 = 0x00000000;
112 style->PrimaryColour = c1;
113 style->SecondaryColour = c1;
114 style->OutlineColour = c2;
115 style->BackColour = 0x00000000;
116 style->BorderStyle = 1;
117 style->Alignment = 2;
118 style->Outline = 2;
119 style->MarginL = 10;
120 style->MarginR = 10;
121 style->MarginV = 5;
122 style->ScaleX = 1.;
123 style->ScaleY = 1.;
126 process_force_style(track);
127 return track;
130 static int check_duplicate_plaintext_event(ass_track_t* track)
132 int i;
133 ass_event_t* evt = track->events + track->n_events - 1;
135 for (i = 0; i<track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
136 if (track->events[i].Start == evt->Start &&
137 track->events[i].Duration == evt->Duration &&
138 strcmp(track->events[i].Text, evt->Text) == 0)
139 return 1;
140 return 0;
144 * \brief Convert subtitle to ass_event_t for the given track
145 * \param ass_track_t track
146 * \param sub subtitle to convert
147 * \return event id
148 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
149 * Start and Duration in other case.
151 int ass_process_subtitle(ass_track_t* track, subtitle* sub)
153 int eid;
154 ass_event_t* event;
155 int len = 0, j;
156 char* p;
157 char* end;
159 eid = ass_alloc_event(track);
160 event = track->events + eid;
162 event->Start = sub->start * 10;
163 event->Duration = (sub->end - sub->start) * 10;
164 event->Style = 0;
166 for (j = 0; j < sub->lines; ++j)
167 len += sub->text[j] ? strlen(sub->text[j]) : 0;
169 len += 2 * sub->lines; // '\N', including the one after the last line
170 len += 6; // {\anX}
171 len += 1; // '\0'
173 event->Text = malloc(len);
174 end = event->Text + len;
175 p = event->Text;
177 if (sub->alignment)
178 p += snprintf(p, end - p, "{\\an%d}", sub->alignment);
180 for (j = 0; j < sub->lines; ++j)
181 p += snprintf(p, end - p, "%s\\N", sub->text[j]);
183 if (sub->lines > 0) p-=2; // remove last "\N"
184 *p = 0;
186 if (check_duplicate_plaintext_event(track)) {
187 ass_free_event(track, eid);
188 track->n_events--;
189 return -1;
192 mp_msg(MSGT_ASS, MSGL_V, "plaintext event at %" PRId64 ", +%" PRId64 ": %s \n",
193 (int64_t)event->Start, (int64_t)event->Duration, event->Text);
195 return eid;
200 * \brief Convert subdata to ass_track
201 * \param subdata subtitles struct from subreader
202 * \param fps video framerate
203 * \return newly allocated ass_track, filled with subtitles from subdata
205 ass_track_t* ass_read_subdata(ass_library_t* library, sub_data* subdata, double fps) {
206 ass_track_t* track;
207 int i;
209 track = ass_default_track(library);
210 track->name = subdata->filename ? strdup(subdata->filename) : 0;
212 for (i = 0; i < subdata->sub_num; ++i) {
213 int eid = ass_process_subtitle(track, subdata->subtitles + i);
214 if (eid < 0)
215 continue;
216 if (!subdata->sub_uses_time) {
217 track->events[eid].Start *= 100. / fps;
218 track->events[eid].Duration *= 100. / fps;
221 return track;
224 void ass_configure(ass_renderer_t* priv, int w, int h, int unscaled) {
225 int hinting;
226 ass_set_frame_size(priv, w, h);
227 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
228 ass_set_use_margins(priv, ass_use_margins);
229 ass_set_font_scale(priv, ass_font_scale);
230 if (!unscaled && (ass_hinting & 4))
231 hinting = 0;
232 else
233 hinting = ass_hinting & 3;
234 ass_set_hinting(priv, hinting);
235 ass_set_line_spacing(priv, ass_line_spacing);
238 void ass_configure_fonts(ass_renderer_t* priv) {
239 char *dir, *path, *family;
240 dir = get_path("fonts");
241 if (font_fontconfig < 0 && sub_font_name) path = strdup(sub_font_name);
242 else if (font_fontconfig < 0 && font_name) path = strdup(font_name);
243 else path = get_path("subfont.ttf");
244 if (font_fontconfig >= 0 && sub_font_name) family = strdup(sub_font_name);
245 else if (font_fontconfig >= 0 && font_name) family = strdup(font_name);
246 else family = 0;
248 if (font_fontconfig >= 0)
249 ass_set_fonts(priv, path, family);
250 else
251 ass_set_fonts_nofc(priv, path, family);
253 free(dir);
254 free(path);
255 free(family);
258 ass_library_t* ass_init(void) {
259 ass_library_t* priv;
260 char* path = get_path("fonts");
261 priv = ass_library_init();
262 ass_set_fonts_dir(priv, path);
263 ass_set_extract_fonts(priv, extract_embedded_fonts);
264 ass_set_style_overrides(priv, ass_force_style_list);
265 free(path);
266 return priv;
269 int ass_force_reload = 0; // flag set if global ass-related settings were changed
271 ass_image_t* ass_mp_render_frame(ass_renderer_t *priv, ass_track_t* track, long long now, int* detect_change) {
272 if (ass_force_reload) {
273 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
274 ass_set_use_margins(priv, ass_use_margins);
275 ass_set_font_scale(priv, ass_font_scale);
276 ass_force_reload = 0;
278 return ass_render_frame(priv, track, now, detect_change);