demux_mkv: use new EBML parser for Info parsing
[mplayer/kovensky.git] / ass_mp.c
blobef2337e04341b5cff14a62e9c21b22e709699974
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 #if defined(FC_VERSION) && (FC_VERSION >= 20402)
45 int extract_embedded_fonts = 1;
46 #else
47 int extract_embedded_fonts = 0;
48 #endif
49 char **ass_force_style_list = NULL;
50 int ass_use_margins = 0;
51 char *ass_color = NULL;
52 char *ass_border_color = NULL;
53 char *ass_styles_file = NULL;
54 int ass_hinting = ASS_HINTING_LIGHT + 4; // light hinting for unscaled osd
56 #ifdef CONFIG_FONTCONFIG
57 extern int font_fontconfig;
58 #else
59 static int font_fontconfig = -1;
60 #endif
61 extern char *font_name;
62 extern char *sub_font_name;
63 extern float text_font_scale_factor;
64 extern int subtitle_autoscale;
66 #ifdef CONFIG_ICONV
67 extern char *sub_cp;
68 #else
69 static char *sub_cp = 0;
70 #endif
72 void process_force_style(ASS_Track *track);
74 ASS_Track *ass_default_track(ASS_Library *library)
76 ASS_Track *track = ass_new_track(library);
78 track->track_type = TRACK_TYPE_ASS;
79 track->Timer = 100.;
80 track->PlayResY = 288;
81 track->WrapStyle = 0;
83 if (ass_styles_file)
84 ass_read_styles(track, ass_styles_file, sub_cp);
86 if (track->n_styles == 0) {
87 ASS_Style *style;
88 int sid;
89 double fs;
90 uint32_t c1, c2;
92 sid = ass_alloc_style(track);
93 style = track->styles + sid;
94 style->Name = strdup("Default");
95 style->FontName = (font_fontconfig >= 0
96 && sub_font_name) ? strdup(sub_font_name)
97 : (font_fontconfig >= 0
98 && font_name) ? strdup(font_name) : strdup("Sans");
99 style->treat_fontname_as_pattern = 1;
101 fs = track->PlayResY * text_font_scale_factor / 100.;
102 // approximate autoscale coefficients
103 if (subtitle_autoscale == 2)
104 fs *= 1.3;
105 else if (subtitle_autoscale == 3)
106 fs *= 1.4;
107 style->FontSize = fs;
109 if (ass_color)
110 c1 = strtoll(ass_color, NULL, 16);
111 else
112 c1 = 0xFFFF0000;
113 if (ass_border_color)
114 c2 = strtoll(ass_border_color, NULL, 16);
115 else
116 c2 = 0x00000000;
118 style->PrimaryColour = c1;
119 style->SecondaryColour = c1;
120 style->OutlineColour = c2;
121 style->BackColour = 0x00000000;
122 style->BorderStyle = 1;
123 style->Alignment = 2;
124 style->Outline = 2;
125 style->MarginL = 10;
126 style->MarginR = 10;
127 style->MarginV = 5;
128 style->ScaleX = 1.;
129 style->ScaleY = 1.;
132 ass_process_force_style(track);
133 return track;
136 static int check_duplicate_plaintext_event(ASS_Track *track)
138 int i;
139 ASS_Event *evt = track->events + track->n_events - 1;
141 for (i = 0; i < track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
142 if (track->events[i].Start == evt->Start &&
143 track->events[i].Duration == evt->Duration &&
144 strcmp(track->events[i].Text, evt->Text) == 0)
145 return 1;
146 return 0;
150 * \brief Convert subtitle to ASS_Events for the given track
151 * \param track track
152 * \param sub subtitle to convert
153 * \return event id
154 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
155 * Start and Duration in other case.
157 int ass_process_subtitle(ASS_Track *track, subtitle *sub)
159 int eid;
160 ASS_Event *event;
161 int len = 0, j;
162 char *p;
163 char *end;
165 eid = ass_alloc_event(track);
166 event = track->events + eid;
168 event->Start = sub->start * 10;
169 event->Duration = (sub->end - sub->start) * 10;
170 event->Style = 0;
172 for (j = 0; j < sub->lines; ++j)
173 len += sub->text[j] ? strlen(sub->text[j]) : 0;
175 len += 2 * sub->lines; // '\N', including the one after the last line
176 len += 6; // {\anX}
177 len += 1; // '\0'
179 event->Text = malloc(len);
180 end = event->Text + len;
181 p = event->Text;
183 if (sub->alignment)
184 p += snprintf(p, end - p, "{\\an%d}", sub->alignment);
186 for (j = 0; j < sub->lines; ++j)
187 p += snprintf(p, end - p, "%s\\N", sub->text[j]);
189 if (sub->lines > 0)
190 p -= 2; // remove last "\N"
191 *p = 0;
193 if (check_duplicate_plaintext_event(track)) {
194 ass_free_event(track, eid);
195 track->n_events--;
196 return -1;
199 mp_msg(MSGT_ASS, MSGL_V,
200 "plaintext event at %" PRId64 ", +%" PRId64 ": %s \n",
201 (int64_t) event->Start, (int64_t) event->Duration, event->Text);
203 return eid;
208 * \brief Convert subdata to ASS_Track
209 * \param subdata subtitles struct from subreader
210 * \param fps video framerate
211 * \return newly allocated ASS_Track, filled with subtitles from subdata
213 ASS_Track *ass_read_subdata(ASS_Library *library, sub_data *subdata,
214 double fps)
216 ASS_Track *track;
217 int i;
219 track = ass_default_track(library);
220 track->name = subdata->filename ? strdup(subdata->filename) : 0;
222 for (i = 0; i < subdata->sub_num; ++i) {
223 int eid = ass_process_subtitle(track, subdata->subtitles + i);
224 if (eid < 0)
225 continue;
226 if (!subdata->sub_uses_time) {
227 track->events[eid].Start *= 100. / fps;
228 track->events[eid].Duration *= 100. / fps;
231 return track;
234 void ass_configure(ASS_Renderer *priv, int w, int h, int unscaled)
236 int hinting;
237 ass_set_frame_size(priv, w, h);
238 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
239 ass_set_use_margins(priv, ass_use_margins);
240 ass_set_font_scale(priv, ass_font_scale);
241 if (!unscaled && (ass_hinting & 4))
242 hinting = 0;
243 else
244 hinting = ass_hinting & 3;
245 ass_set_hinting(priv, hinting);
246 ass_set_line_spacing(priv, ass_line_spacing);
249 void ass_configure_fonts(ASS_Renderer *priv)
251 char *dir, *path, *family;
252 dir = get_path("fonts");
253 if (font_fontconfig < 0 && sub_font_name)
254 path = strdup(sub_font_name);
255 else if (font_fontconfig < 0 && font_name)
256 path = strdup(font_name);
257 else
258 path = get_path("subfont.ttf");
259 if (font_fontconfig >= 0 && sub_font_name)
260 family = strdup(sub_font_name);
261 else if (font_fontconfig >= 0 && font_name)
262 family = strdup(font_name);
263 else
264 family = 0;
266 ass_set_fonts(priv, path, family, font_fontconfig + 1, NULL, 1);
268 free(dir);
269 free(path);
270 free(family);
273 static void message_callback(int level, const char *format, va_list va, void *ctx)
275 mp_msg(MSGT_ASS, level, "[ass] ");
276 mp_msg_va(MSGT_ASS, level, format, va);
277 // libass messages lack trailing \n
278 mp_msg(MSGT_ASS, level, "\n");
281 ASS_Library *ass_init(void)
283 ASS_Library *priv;
284 char *path = get_path("fonts");
285 priv = ass_library_init();
286 ass_set_message_cb(priv, message_callback, NULL);
287 ass_set_fonts_dir(priv, path);
288 ass_set_extract_fonts(priv, extract_embedded_fonts);
289 ass_set_style_overrides(priv, ass_force_style_list);
290 free(path);
291 return priv;
294 int ass_force_reload = 0; // flag set if global ass-related settings were changed
296 ASS_Image *ass_mp_render_frame(ASS_Renderer *priv, ASS_Track *track,
297 long long now, int *detect_change)
299 if (ass_force_reload) {
300 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
301 ass_set_use_margins(priv, ass_use_margins);
302 ass_set_font_scale(priv, ass_font_scale);
303 ass_force_reload = 0;
305 return ass_render_frame(priv, track, now, detect_change);