Replace sleep time calculation in main() with a separate function.
[mplayer.git] / libass / ass_mp.c
bloba4d195ffad80a58304d3744cafd63725bd2d964d
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 program 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 This program 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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <inttypes.h>
22 #include <string.h>
23 #include <stdlib.h>
25 #include "mp_msg.h"
27 #include "ass.h"
28 #include "ass_utils.h"
29 #include "ass_mp.h"
30 #include "ass_library.h"
32 // libass-related command line options
33 ass_library_t* ass_library;
34 int ass_enabled = 0;
35 float ass_font_scale = 1.;
36 float ass_line_spacing = 0.;
37 int ass_top_margin = 0;
38 int ass_bottom_margin = 0;
39 int extract_embedded_fonts = 0;
40 char **ass_force_style_list = NULL;
41 int ass_use_margins = 0;
42 char* ass_color = NULL;
43 char* ass_border_color = NULL;
44 char* ass_styles_file = NULL;
46 #ifdef HAVE_FONTCONFIG
47 extern int font_fontconfig;
48 #else
49 static int font_fontconfig = 0;
50 #endif
51 extern char* font_name;
52 extern float text_font_scale_factor;
53 extern int subtitle_autoscale;
55 #ifdef USE_ICONV
56 extern char* sub_cp;
57 #else
58 static char* sub_cp = 0;
59 #endif
61 extern double ass_internal_font_size_coeff;
62 extern void process_force_style(ass_track_t* track);
64 ass_track_t* ass_default_track(ass_library_t* library) {
65 ass_track_t* track = ass_new_track(library);
67 track->track_type = TRACK_TYPE_ASS;
68 track->Timer = 100.;
69 track->PlayResY = 288;
70 track->WrapStyle = 0;
72 if (ass_styles_file)
73 ass_read_styles(track, ass_styles_file, sub_cp);
75 if (track->n_styles == 0) {
76 ass_style_t* style;
77 int sid;
78 double fs;
79 uint32_t c1, c2;
81 sid = ass_alloc_style(track);
82 style = track->styles + sid;
83 style->Name = strdup("Default");
84 style->FontName = (font_fontconfig && font_name) ? strdup(font_name) : strdup("Sans");
86 fs = track->PlayResY * text_font_scale_factor / 100. / ass_internal_font_size_coeff;
87 // approximate autoscale coefficients
88 if (subtitle_autoscale == 2)
89 fs *= 1.3;
90 else if (subtitle_autoscale == 3)
91 fs *= 1.4;
92 style->FontSize = fs;
94 if (ass_color) c1 = strtoll(ass_color, NULL, 16);
95 else c1 = 0xFFFF0000;
96 if (ass_border_color) c2 = strtoll(ass_border_color, NULL, 16);
97 else c2 = 0x00000000;
99 style->PrimaryColour = c1;
100 style->SecondaryColour = c1;
101 style->OutlineColour = c2;
102 style->BackColour = 0x00000000;
103 style->BorderStyle = 1;
104 style->Alignment = 2;
105 style->Outline = 2;
106 style->MarginL = 10;
107 style->MarginR = 10;
108 style->MarginV = 5;
109 style->ScaleX = 1.;
110 style->ScaleY = 1.;
113 process_force_style(track);
114 return track;
117 static int check_duplicate_plaintext_event(ass_track_t* track)
119 int i;
120 ass_event_t* evt = track->events + track->n_events - 1;
122 for (i = 0; i<track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
123 if (track->events[i].Start == evt->Start &&
124 track->events[i].Duration == evt->Duration &&
125 strcmp(track->events[i].Text, evt->Text) == 0)
126 return 1;
127 return 0;
131 * \brief Convert subtitle to ass_event_t for the given track
132 * \param ass_track_t track
133 * \param sub subtitle to convert
134 * \return event id
135 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
136 * Start and Duration in other case.
138 int ass_process_subtitle(ass_track_t* track, subtitle* sub)
140 int eid;
141 ass_event_t* event;
142 int len = 0, j;
143 char* p;
144 char* end;
146 eid = ass_alloc_event(track);
147 event = track->events + eid;
149 event->Start = sub->start * 10;
150 event->Duration = (sub->end - sub->start) * 10;
151 event->Style = 0;
153 for (j = 0; j < sub->lines; ++j)
154 len += sub->text[j] ? strlen(sub->text[j]) : 0;
156 len += 2 * sub->lines; // '\N', including the one after the last line
157 len += 6; // {\anX}
158 len += 1; // '\0'
160 event->Text = malloc(len);
161 end = event->Text + len;
162 p = event->Text;
164 if (sub->alignment)
165 p += snprintf(p, end - p, "{\\an%d}", sub->alignment);
167 for (j = 0; j < sub->lines; ++j)
168 p += snprintf(p, end - p, "%s\\N", sub->text[j]);
170 p-=2; // remove last ' '
171 *p = 0;
173 if (check_duplicate_plaintext_event(track)) {
174 ass_free_event(track, eid);
175 track->n_events--;
176 return -1;
179 mp_msg(MSGT_ASS, MSGL_V, "plaintext event at %" PRId64 ", +%" PRId64 ": %s \n",
180 (int64_t)event->Start, (int64_t)event->Duration, event->Text);
182 return eid;
187 * \brief Convert subdata to ass_track
188 * \param subdata subtitles struct from subreader
189 * \param fps video framerate
190 * \return newly allocated ass_track, filled with subtitles from subdata
192 ass_track_t* ass_read_subdata(ass_library_t* library, sub_data* subdata, double fps) {
193 ass_track_t* track;
194 int i;
196 track = ass_default_track(library);
197 track->name = subdata->filename ? strdup(subdata->filename) : 0;
199 for (i = 0; i < subdata->sub_num; ++i) {
200 int eid = ass_process_subtitle(track, subdata->subtitles + i);
201 if (eid < 0)
202 continue;
203 if (!subdata->sub_uses_time) {
204 track->events[eid].Start *= 100. / fps;
205 track->events[eid].Duration *= 100. / fps;
208 return track;
211 char *get_path(char *);
213 void ass_configure(ass_renderer_t* priv, int w, int h) {
214 ass_set_frame_size(priv, w, h);
215 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
216 ass_set_use_margins(priv, ass_use_margins);
217 ass_set_font_scale(priv, ass_font_scale);
220 void ass_configure_fonts(ass_renderer_t* priv) {
221 char *dir, *path, *family;
222 dir = get_path("fonts");
223 if (!font_fontconfig && font_name) path = strdup(font_name);
224 else path = get_path("subfont.ttf");
225 if (font_fontconfig && font_name) family = strdup(font_name);
226 else family = 0;
228 ass_set_fonts(priv, path, family);
230 free(dir);
231 free(path);
232 free(family);
235 ass_library_t* ass_init() {
236 ass_library_t* priv;
237 char* path = get_path("fonts");
238 priv = ass_library_init();
239 ass_set_fonts_dir(priv, path);
240 ass_set_extract_fonts(priv, extract_embedded_fonts);
241 ass_set_style_overrides(priv, ass_force_style_list);
242 free(path);
243 return priv;