ao_pulse: support native mute control
[mplayer.git] / sub / ass_mp.c
blob8af3698a47fba1ee6f7e0cce7f836645ce3c864e
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"
38 #include "options.h"
40 #ifdef CONFIG_FONTCONFIG
41 extern int font_fontconfig;
42 #else
43 static int font_fontconfig = -1;
44 #endif
45 extern char *font_name;
46 extern char *sub_font_name;
47 extern float text_font_scale_factor;
48 extern int subtitle_autoscale;
50 #ifdef CONFIG_ICONV
51 extern char *sub_cp;
52 #else
53 static char *sub_cp = 0;
54 #endif
56 ASS_Track *mp_ass_default_track(ASS_Library *library, struct MPOpts *opts)
58 ASS_Track *track = ass_new_track(library);
60 track->track_type = TRACK_TYPE_ASS;
61 track->Timer = 100.;
62 track->PlayResY = 288;
63 track->WrapStyle = 0;
65 if (opts->ass_styles_file)
66 ass_read_styles(track, opts->ass_styles_file, sub_cp);
68 if (track->n_styles == 0) {
69 track->Kerning = true;
70 int sid = ass_alloc_style(track);
71 track->default_style = sid;
72 ASS_Style *style = track->styles + sid;
73 style->Name = strdup("Default");
74 style->FontName = (font_fontconfig >= 0
75 && sub_font_name) ? strdup(sub_font_name)
76 : (font_fontconfig >= 0
77 && font_name) ? strdup(font_name) : strdup("Sans");
78 style->treat_fontname_as_pattern = 1;
80 double fs = track->PlayResY * text_font_scale_factor / 100.;
81 /* The font size is always proportional to video height only;
82 * real -subfont-autoscale behavior is not implemented.
83 * Apply a correction that corresponds to about 4:3 aspect ratio
84 * video to get a size somewhat closer to what non-libass rendering
85 * would produce with the same text_font_scale_factor
86 * and subtitle_autoscale.
88 if (subtitle_autoscale == 2)
89 fs *= 1.3;
90 else if (subtitle_autoscale == 3)
91 fs *= 1.7;
93 uint32_t c1 = 0xFFFFFF00;
94 uint32_t c2 = 0x00000000;
95 if (opts->ass_color)
96 c1 = strtoll(opts->ass_color, NULL, 16);
97 if (opts->ass_border_color)
98 c2 = strtoll(opts->ass_border_color, NULL, 16);
100 style->FontSize = fs;
101 style->PrimaryColour = c1;
102 style->SecondaryColour = c1;
103 style->OutlineColour = c2;
104 style->BackColour = 0x00000000;
105 style->BorderStyle = 1;
106 style->Alignment = 2;
107 style->Outline = fs / 16;
108 style->MarginL = 10;
109 style->MarginR = 10;
110 style->MarginV = 5;
111 style->ScaleX = 1.;
112 style->ScaleY = 1.;
115 ass_process_force_style(track);
116 return track;
119 static int check_duplicate_plaintext_event(ASS_Track *track)
121 int i;
122 ASS_Event *evt = track->events + track->n_events - 1;
124 for (i = 0; i < track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
125 if (track->events[i].Start == evt->Start &&
126 track->events[i].Duration == evt->Duration &&
127 strcmp(track->events[i].Text, evt->Text) == 0)
128 return 1;
129 return 0;
133 * \brief Convert subtitle to ASS_Events for the given track
134 * \param track track
135 * \param sub subtitle to convert
136 * \return event id
137 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
138 * Start and Duration in other case.
140 static int ass_process_subtitle(ASS_Track *track, subtitle *sub)
142 int eid;
143 ASS_Event *event;
144 int len = 0, j;
145 char *p;
146 char *end;
148 eid = ass_alloc_event(track);
149 event = track->events + eid;
151 event->Start = sub->start * 10;
152 event->Duration = (sub->end - sub->start) * 10;
153 event->Style = track->default_style;
155 for (j = 0; j < sub->lines; ++j)
156 len += sub->text[j] ? strlen(sub->text[j]) : 0;
158 len += 2 * sub->lines; // '\N', including the one after the last line
159 len += 6; // {\anX}
160 len += 1; // '\0'
162 event->Text = malloc(len);
163 end = event->Text + len;
164 p = event->Text;
166 if (sub->alignment)
167 p += snprintf(p, end - p, "{\\an%d}", sub->alignment);
169 for (j = 0; j < sub->lines; ++j)
170 p += snprintf(p, end - p, "%s\\N", sub->text[j]);
172 if (sub->lines > 0)
173 p -= 2; // remove last "\N"
174 *p = 0;
176 if (check_duplicate_plaintext_event(track)) {
177 ass_free_event(track, eid);
178 track->n_events--;
179 return -1;
182 mp_msg(MSGT_ASS, MSGL_V,
183 "plaintext event at %" PRId64 ", +%" PRId64 ": %s \n",
184 (int64_t) event->Start, (int64_t) event->Duration, event->Text);
186 return eid;
191 * \brief Convert subdata to ASS_Track
192 * \param subdata subtitles struct from subreader
193 * \param fps video framerate
194 * \return newly allocated ASS_Track, filled with subtitles from subdata
196 ASS_Track *mp_ass_read_subdata(ASS_Library *library, struct MPOpts *opts,
197 sub_data *subdata, double fps)
199 ASS_Track *track;
200 int i;
202 track = mp_ass_default_track(library, opts);
203 track->name = subdata->filename ? strdup(subdata->filename) : 0;
205 for (i = 0; i < subdata->sub_num; ++i) {
206 int eid = ass_process_subtitle(track, subdata->subtitles + i);
207 if (eid < 0)
208 continue;
209 if (!subdata->sub_uses_time) {
210 track->events[eid].Start *= 100. / fps;
211 track->events[eid].Duration *= 100. / fps;
214 return track;
217 ASS_Track *mp_ass_read_stream(ASS_Library *library, const char *fname,
218 char *charset)
220 ASS_Track *track;
222 struct stream *s = open_stream(fname, NULL, NULL);
223 if (!s)
224 // Stream code should have printed an error already
225 return NULL;
226 struct bstr content = stream_read_complete(s, NULL, 100000000, 1);
227 if (content.start == NULL)
228 mp_tmsg(MSGT_ASS, MSGL_ERR, "Refusing to load subtitle file "
229 "larger than 100 MB: %s\n", fname);
230 free_stream(s);
231 if (content.len == 0) {
232 talloc_free(content.start);
233 return NULL;
235 content.start[content.len] = 0;
236 track = ass_read_memory(library, content.start, content.len, charset);
237 if (track) {
238 free(track->name);
239 track->name = strdup(fname);
241 talloc_free(content.start);
242 return track;
245 void mp_ass_configure(ASS_Renderer *priv, struct MPOpts *opts, int w, int h,
246 bool unscaled)
248 int hinting;
249 ass_set_frame_size(priv, w, h);
250 ass_set_margins(priv, opts->ass_top_margin, opts->ass_bottom_margin, 0, 0);
251 ass_set_use_margins(priv, opts->ass_use_margins);
252 ass_set_font_scale(priv, opts->ass_font_scale);
253 if (!unscaled && (opts->ass_hinting & 4))
254 hinting = 0;
255 else
256 hinting = opts->ass_hinting & 3;
257 ass_set_hinting(priv, hinting);
258 ass_set_line_spacing(priv, opts->ass_line_spacing);
261 void mp_ass_configure_fonts(ASS_Renderer *priv)
263 char *dir, *path, *family;
264 dir = get_path("fonts");
265 if (font_fontconfig < 0 && sub_font_name)
266 path = strdup(sub_font_name);
267 else if (font_fontconfig < 0 && font_name)
268 path = strdup(font_name);
269 else
270 path = get_path("subfont.ttf");
271 if (font_fontconfig >= 0 && sub_font_name)
272 family = strdup(sub_font_name);
273 else if (font_fontconfig >= 0 && font_name)
274 family = strdup(font_name);
275 else
276 family = 0;
278 ass_set_fonts(priv, path, family, font_fontconfig + 1, NULL, 1);
280 free(dir);
281 free(path);
282 free(family);
285 static void message_callback(int level, const char *format, va_list va, void *ctx)
287 mp_msg(MSGT_ASS, level, "[ass] ");
288 mp_msg_va(MSGT_ASS, level, format, va);
289 // libass messages lack trailing \n
290 mp_msg(MSGT_ASS, level, "\n");
293 ASS_Library *mp_ass_init(struct MPOpts *opts)
295 ASS_Library *priv;
296 char *path = get_path("fonts");
297 priv = ass_library_init();
298 ass_set_message_cb(priv, message_callback, NULL);
299 ass_set_fonts_dir(priv, path);
300 ass_set_extract_fonts(priv, opts->use_embedded_fonts);
301 free(path);
302 return priv;
305 void mp_ass_reload_options(ASS_Renderer *priv, struct MPOpts *opts)
307 /* This could be needed for vf_ass case if the margins were actually
308 * runtime configurable, but would be wrong with EOSD:
309 * ass_set_margins(priv, opts->ass_top_margin, opts->ass_bottom_margin,
310 * 0, 0);
312 ass_set_use_margins(priv, opts->ass_use_margins);
313 ass_set_font_scale(priv, opts->ass_font_scale);