annotate libass/ass_mp.c @ 32195:25bb8d59af90

Unconditionally #include subreader.h. This fixes compilation with iconv disabled.
author diego
date Thu, 16 Sep 2010 19:19:13 +0000
parents 7773438f5abf
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20008
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19654
diff changeset
1 /*
26723
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
3 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
4 * This file is part of libass.
26723
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
5 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
6 * libass is free software; you can redistribute it and/or modify
26723
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
7 * it under the terms of the GNU General Public License as published by
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
9 * (at your option) any later version.
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
10 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
11 * libass is distributed in the hope that it will be useful,
26723
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
14 * GNU General Public License for more details.
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
15 *
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
16 * You should have received a copy of the GNU General Public License along
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
17 * with libass; if not, write to the Free Software Foundation, Inc.,
26723
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0f892cd714b2 Use standard license header.
diego
parents: 26582
diff changeset
19 */
20008
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19654
diff changeset
20
19405
0797e1b4a4be Replace stdint.h with inttypes.h.
eugeni
parents: 19401
diff changeset
21 #include <inttypes.h>
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
22 #include <string.h>
19484
6eb79b2384f8 use of malloc without prototype.. very bad on 64bit archs
rfelker
parents: 19405
diff changeset
23 #include <stdlib.h>
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
24
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
25 #include "mp_msg.h"
32057
d6def8038a47 Replace a bunch of extern declarations by proper mpcommon.h #include.
diego
parents: 32015
diff changeset
26 #include "mpcommon.h"
30901
76a13038105e Rename get_path.[ch] --> path.[ch].
diego
parents: 30698
diff changeset
27 #include "path.h"
32195
25bb8d59af90 Unconditionally #include subreader.h.
diego
parents: 32193
diff changeset
28 #include "subreader.h"
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
29
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
30 #include "ass_mp.h"
30473
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
31 #include "help_mp.h"
32191
ca6eda0ae749 Add necessary #include for text_font_scale_factor and subtitle_autoscale.
diego
parents: 32139
diff changeset
32 #include "libvo/font_load.h"
30473
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
33 #include "stream/stream.h"
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
34
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
35 #ifdef CONFIG_FONTCONFIG
21632
e3e6d6fbb561 Make -embeddedfonts enabled by default with FontConfig >= 2.4.2.
eugeni
parents: 20706
diff changeset
36 #include <fontconfig/fontconfig.h>
e3e6d6fbb561 Make -embeddedfonts enabled by default with FontConfig >= 2.4.2.
eugeni
parents: 20706
diff changeset
37 #endif
e3e6d6fbb561 Make -embeddedfonts enabled by default with FontConfig >= 2.4.2.
eugeni
parents: 20706
diff changeset
38
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
39 // libass-related command line options
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
40 ASS_Library* ass_library;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
41 int ass_enabled = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
42 float ass_font_scale = 1.;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
43 float ass_line_spacing = 0.;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
44 int ass_top_margin = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
45 int ass_bottom_margin = 0;
21632
e3e6d6fbb561 Make -embeddedfonts enabled by default with FontConfig >= 2.4.2.
eugeni
parents: 20706
diff changeset
46 int extract_embedded_fonts = 1;
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19484
diff changeset
47 char **ass_force_style_list = NULL;
19563
661d6c8a4adb Add -(no)ass-use-margins option.
eugeni
parents: 19495
diff changeset
48 int ass_use_margins = 0;
19646
e6cdf80eb941 ass-color and ass-border-color options.
eugeni
parents: 19643
diff changeset
49 char* ass_color = NULL;
e6cdf80eb941 ass-color and ass-border-color options.
eugeni
parents: 19643
diff changeset
50 char* ass_border_color = NULL;
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19646
diff changeset
51 char* ass_styles_file = NULL;
23134
1de2a46a0987 Add -ass-hinting option for setting font hinting method.
eugeni
parents: 22886
diff changeset
52 int ass_hinting = ASS_HINTING_NATIVE + 4; // native hinting for unscaled osd
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
53
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
54 ASS_Track* ass_default_track(ASS_Library* library) {
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
55 ASS_Track* track = ass_new_track(library);
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
56
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
57 track->track_type = TRACK_TYPE_ASS;
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
58 track->Timer = 100.;
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
59 track->PlayResY = 288;
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
60 track->WrapStyle = 0;
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
61
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19646
diff changeset
62 if (ass_styles_file)
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20469
diff changeset
63 ass_read_styles(track, ass_styles_file, sub_cp);
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19646
diff changeset
64
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19646
diff changeset
65 if (track->n_styles == 0) {
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
66 ASS_Style* style;
19653
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
67 int sid;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
68 double fs;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
69 uint32_t c1, c2;
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19646
diff changeset
70
19653
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
71 sid = ass_alloc_style(track);
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
72 style = track->styles + sid;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
73 style->Name = strdup("Default");
25392
2865633412d9 make libass use sub_font_name whenever it's possible
ben
parents: 25351
diff changeset
74 style->FontName = (font_fontconfig >= 0 && sub_font_name) ? strdup(sub_font_name) : (font_fontconfig >= 0 && font_name) ? strdup(font_name) : strdup("Sans");
28860
7fcc0bf5b27a Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents: 28051
diff changeset
75 style->treat_fontname_as_pattern = 1;
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
76
23328
99ac5d381aed Correct font size in libass.
eugeni
parents: 23134
diff changeset
77 fs = track->PlayResY * text_font_scale_factor / 100.;
19653
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
78 // approximate autoscale coefficients
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
79 if (subtitle_autoscale == 2)
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
80 fs *= 1.3;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
81 else if (subtitle_autoscale == 3)
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
82 fs *= 1.4;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
83 style->FontSize = fs;
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
84
19653
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
85 if (ass_color) c1 = strtoll(ass_color, NULL, 16);
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
86 else c1 = 0xFFFF0000;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
87 if (ass_border_color) c2 = strtoll(ass_border_color, NULL, 16);
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
88 else c2 = 0x00000000;
19646
e6cdf80eb941 ass-color and ass-border-color options.
eugeni
parents: 19643
diff changeset
89
19653
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
90 style->PrimaryColour = c1;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
91 style->SecondaryColour = c1;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
92 style->OutlineColour = c2;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
93 style->BackColour = 0x00000000;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
94 style->BorderStyle = 1;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
95 style->Alignment = 2;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
96 style->Outline = 2;
20138
f753fbd5ae66 Reduce margins for plaintext subtitles.
eugeni
parents: 20008
diff changeset
97 style->MarginL = 10;
f753fbd5ae66 Reduce margins for plaintext subtitles.
eugeni
parents: 20008
diff changeset
98 style->MarginR = 10;
f753fbd5ae66 Reduce margins for plaintext subtitles.
eugeni
parents: 20008
diff changeset
99 style->MarginV = 5;
19653
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
100 style->ScaleX = 1.;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
101 style->ScaleY = 1.;
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19646
diff changeset
102 }
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
103
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
104 ass_process_force_style(track);
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
105 return track;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
106 }
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
107
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
108 static int check_duplicate_plaintext_event(ASS_Track* track)
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
109 {
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
110 int i;
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
111 ASS_Event* evt = track->events + track->n_events - 1;
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
112
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
113 for (i = 0; i<track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
114 if (track->events[i].Start == evt->Start &&
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
115 track->events[i].Duration == evt->Duration &&
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
116 strcmp(track->events[i].Text, evt->Text) == 0)
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
117 return 1;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
118 return 0;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
119 }
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
120
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
121 /**
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
122 * \brief Convert subtitle to ASS_Event for the given track
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
123 * \param track ASS_Track
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
124 * \param sub subtitle to convert
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
125 * \return event id
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
126 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
127 * Start and Duration in other case.
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
128 **/
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
129 int ass_process_subtitle(ASS_Track* track, subtitle* sub)
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
130 {
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
131 int eid;
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
132 ASS_Event* event;
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
133 int len = 0, j;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
134 char* p;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
135 char* end;
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
136
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
137 eid = ass_alloc_event(track);
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
138 event = track->events + eid;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
139
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
140 event->Start = sub->start * 10;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
141 event->Duration = (sub->end - sub->start) * 10;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
142 event->Style = 0;
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
143
19653
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
144 for (j = 0; j < sub->lines; ++j)
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
145 len += sub->text[j] ? strlen(sub->text[j]) : 0;
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
146
19653
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
147 len += 2 * sub->lines; // '\N', including the one after the last line
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
148 len += 6; // {\anX}
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
149 len += 1; // '\0'
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
150
19653
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
151 event->Text = malloc(len);
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
152 end = event->Text + len;
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
153 p = event->Text;
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
154
19653
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
155 if (sub->alignment)
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
156 p += snprintf(p, end - p, "{\\an%d}", sub->alignment);
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
157
19653
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
158 for (j = 0; j < sub->lines; ++j)
20495
27c1fa3a228e Add \N at the end of each subtitle line when converting from subdata.
eugeni
parents: 20477
diff changeset
159 p += snprintf(p, end - p, "%s\\N", sub->text[j]);
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
160
21835
5ee5281c78f8 Avoid invalid memory access in ass_process_subtitle for empty subtitles
reimar
parents: 21632
diff changeset
161 if (sub->lines > 0) p-=2; // remove last "\N"
19653
c03c705573c1 Cosmetics: restore indentation after recent changes.
eugeni
parents: 19652
diff changeset
162 *p = 0;
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
163
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
164 if (check_duplicate_plaintext_event(track)) {
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
165 ass_free_event(track, eid);
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
166 track->n_events--;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
167 return -1;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
168 }
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
169
20629
e8885ec63928 Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents: 20502
diff changeset
170 mp_msg(MSGT_ASS, MSGL_V, "plaintext event at %" PRId64 ", +%" PRId64 ": %s \n",
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
171 (int64_t)event->Start, (int64_t)event->Duration, event->Text);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28860
diff changeset
172
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
173 return eid;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
174 }
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
175
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
176
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
177 /**
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
178 * \brief Convert subdata to ASS_Track
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
179 * \param subdata subtitles struct from subreader
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
180 * \param fps video framerate
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
181 * \return newly allocated ASS_Track, filled with subtitles from subdata
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
182 */
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
183 ASS_Track* ass_read_subdata(ASS_Library* library, sub_data* subdata, double fps) {
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
184 ASS_Track* track;
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
185 int i;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
186
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20469
diff changeset
187 track = ass_default_track(library);
19643
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
188 track->name = subdata->filename ? strdup(subdata->filename) : 0;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
189
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
190 for (i = 0; i < subdata->sub_num; ++i) {
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
191 int eid = ass_process_subtitle(track, subdata->subtitles + i);
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
192 if (eid < 0)
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
193 continue;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
194 if (!subdata->sub_uses_time) {
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
195 track->events[eid].Start *= 100. / fps;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
196 track->events[eid].Duration *= 100. / fps;
f48d49b400cf Add support for rendering matroska plaintext subtitles with libass.
eugeni
parents: 19592
diff changeset
197 }
19401
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
198 }
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
199 return track;
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
200 }
c0c3a2f8bb32 Add subdata to ass_track conversion for external subtitles.
eugeni
parents: 18937
diff changeset
201
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
202 ASS_Track* ass_read_stream(ASS_Library* library, const char *fname, char *charset) {
30473
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
203 char *buf = NULL;
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
204 ASS_Track *track;
30473
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
205 size_t sz = 0;
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
206 size_t buf_alloc = 0;
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
207 stream_t *fd;
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
208
31871
c3775dbb8509 Simplify code: allow file_format argument to open_stream to be NULL.
reimar
parents: 31792
diff changeset
209 fd = open_stream(fname, NULL, NULL);
30473
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
210 if (!fd) {
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
211 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FopenFailed, fname);
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
212 return NULL;
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
213 }
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
214 if (fd->end_pos > STREAM_BUFFER_SIZE)
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
215 /* read entire file if size is known */
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
216 buf_alloc = fd->end_pos;
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
217 for (;;) {
31871
c3775dbb8509 Simplify code: allow file_format argument to open_stream to be NULL.
reimar
parents: 31792
diff changeset
218 int i;
30473
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
219 if (buf_alloc >= 100*1024*1024) {
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
220 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_RefusingToLoadSubtitlesLargerThan100M, fname);
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
221 sz = 0;
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
222 break;
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
223 }
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
224 if (buf_alloc < sz + STREAM_BUFFER_SIZE)
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
225 buf_alloc += STREAM_BUFFER_SIZE;
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
226 buf = realloc(buf, buf_alloc + 1);
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
227 i = stream_read(fd, buf + sz, buf_alloc - sz);
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
228 if (i <= 0) break;
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
229 sz += i;
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
230 }
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
231 free_stream(fd);
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
232 if (!sz) {
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
233 free(buf);
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
234 return NULL;
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
235 }
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
236 buf[sz] = 0;
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
237 buf = realloc(buf, sz + 1);
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
238 track = ass_read_memory(library, buf, sz, charset);
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
239 if (track) {
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
240 free(track->name);
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
241 track->name = strdup(fname);
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
242 }
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
243 free(buf);
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
244 return track;
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
245 }
7446f58b6899 Add support for loading ASS subtitles through the stream layer and thus e.g.
reimar
parents: 30197
diff changeset
246
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
247 void ass_configure(ASS_Renderer* priv, int w, int h, int unscaled) {
23134
1de2a46a0987 Add -ass-hinting option for setting font hinting method.
eugeni
parents: 22886
diff changeset
248 int hinting;
20446
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
249 ass_set_frame_size(priv, w, h);
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
250 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
251 ass_set_use_margins(priv, ass_use_margins);
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
252 ass_set_font_scale(priv, ass_font_scale);
23134
1de2a46a0987 Add -ass-hinting option for setting font hinting method.
eugeni
parents: 22886
diff changeset
253 if (!unscaled && (ass_hinting & 4))
1de2a46a0987 Add -ass-hinting option for setting font hinting method.
eugeni
parents: 22886
diff changeset
254 hinting = 0;
1de2a46a0987 Add -ass-hinting option for setting font hinting method.
eugeni
parents: 22886
diff changeset
255 else
1de2a46a0987 Add -ass-hinting option for setting font hinting method.
eugeni
parents: 22886
diff changeset
256 hinting = ass_hinting & 3;
1de2a46a0987 Add -ass-hinting option for setting font hinting method.
eugeni
parents: 22886
diff changeset
257 ass_set_hinting(priv, hinting);
24554
ffc2c7164bc0 Enable ass_line_spacing option.
eugeni
parents: 24242
diff changeset
258 ass_set_line_spacing(priv, ass_line_spacing);
20706
6ae01628975f Initialize fontconfig in VFCTRL_INIT_EOSD handler.
eugeni
parents: 20702
diff changeset
259 }
20446
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
260
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
261 void ass_configure_fonts(ASS_Renderer* priv) {
20706
6ae01628975f Initialize fontconfig in VFCTRL_INIT_EOSD handler.
eugeni
parents: 20702
diff changeset
262 char *dir, *path, *family;
20446
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
263 dir = get_path("fonts");
25392
2865633412d9 make libass use sub_font_name whenever it's possible
ben
parents: 25351
diff changeset
264 if (font_fontconfig < 0 && sub_font_name) path = strdup(sub_font_name);
2865633412d9 make libass use sub_font_name whenever it's possible
ben
parents: 25351
diff changeset
265 else if (font_fontconfig < 0 && font_name) path = strdup(font_name);
20446
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
266 else path = get_path("subfont.ttf");
25392
2865633412d9 make libass use sub_font_name whenever it's possible
ben
parents: 25351
diff changeset
267 if (font_fontconfig >= 0 && sub_font_name) family = strdup(sub_font_name);
2865633412d9 make libass use sub_font_name whenever it's possible
ben
parents: 25351
diff changeset
268 else if (font_fontconfig >= 0 && font_name) family = strdup(font_name);
20446
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
269 else family = 0;
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
270
30065
d5d20b8256b7 Some ugly hacks to make compiling against a newer external version of libass work.
reimar
parents: 30064
diff changeset
271 ass_set_fonts(priv, path, family, font_fontconfig, NULL, 1);
20446
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
272
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
273 free(dir);
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
274 free(path);
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
275 free(family);
e8adc3778348 Split ass_configure() into several smaller functions.
eugeni
parents: 20138
diff changeset
276 }
20502
81a92ae66d78 Move ass_library initialization code to ass_mp.c.
eugeni
parents: 20496
diff changeset
277
31689
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
278 static void message_callback(int level, const char *format, va_list va, void *ctx)
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
279 {
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
280 int n;
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
281 char *str;
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
282 va_list dst;
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
283
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
284 va_copy(dst, va);
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
285 n = vsnprintf(NULL, 0, format, va);
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
286 if (n > 0 && (str = malloc(n + 1))) {
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
287 vsnprintf(str, n + 1, format, dst);
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
288 mp_msg(MSGT_ASS, level, "[ass] %s\n", str);
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
289 free(str);
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
290 }
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
291 }
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
292
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
293 ASS_Library* ass_init(void) {
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
294 ASS_Library* priv;
20702
34bb6f334242 Cosmetics: indentation fix.
eugeni
parents: 20629
diff changeset
295 char* path = get_path("fonts");
34bb6f334242 Cosmetics: indentation fix.
eugeni
parents: 20629
diff changeset
296 priv = ass_library_init();
31689
c447fb125915 Pass libass messages to mp_msg
greg
parents: 31688
diff changeset
297 ass_set_message_cb(priv, message_callback, NULL);
20702
34bb6f334242 Cosmetics: indentation fix.
eugeni
parents: 20629
diff changeset
298 ass_set_fonts_dir(priv, path);
34bb6f334242 Cosmetics: indentation fix.
eugeni
parents: 20629
diff changeset
299 ass_set_extract_fonts(priv, extract_embedded_fonts);
34bb6f334242 Cosmetics: indentation fix.
eugeni
parents: 20629
diff changeset
300 ass_set_style_overrides(priv, ass_force_style_list);
34bb6f334242 Cosmetics: indentation fix.
eugeni
parents: 20629
diff changeset
301 free(path);
34bb6f334242 Cosmetics: indentation fix.
eugeni
parents: 20629
diff changeset
302 return priv;
20502
81a92ae66d78 Move ass_library initialization code to ass_mp.c.
eugeni
parents: 20496
diff changeset
303 }
25813
ba7bd4f7e322 Add global ass_force_reload flag.
eugeni
parents: 25392
diff changeset
304
ba7bd4f7e322 Add global ass_force_reload flag.
eugeni
parents: 25392
diff changeset
305 int ass_force_reload = 0; // flag set if global ass-related settings were changed
ba7bd4f7e322 Add global ass_force_reload flag.
eugeni
parents: 25392
diff changeset
306
31792
55dacfca4a43 Rename libass types to match upstream libass >= 0.9.7
greg
parents: 31689
diff changeset
307 ASS_Image* ass_mp_render_frame(ASS_Renderer *priv, ASS_Track* track, long long now, int* detect_change) {
25813
ba7bd4f7e322 Add global ass_force_reload flag.
eugeni
parents: 25392
diff changeset
308 if (ass_force_reload) {
ba7bd4f7e322 Add global ass_force_reload flag.
eugeni
parents: 25392
diff changeset
309 ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
ba7bd4f7e322 Add global ass_force_reload flag.
eugeni
parents: 25392
diff changeset
310 ass_set_use_margins(priv, ass_use_margins);
ba7bd4f7e322 Add global ass_force_reload flag.
eugeni
parents: 25392
diff changeset
311 ass_set_font_scale(priv, ass_font_scale);
ba7bd4f7e322 Add global ass_force_reload flag.
eugeni
parents: 25392
diff changeset
312 ass_force_reload = 0;
ba7bd4f7e322 Add global ass_force_reload flag.
eugeni
parents: 25392
diff changeset
313 }
ba7bd4f7e322 Add global ass_force_reload flag.
eugeni
parents: 25392
diff changeset
314 return ass_render_frame(priv, track, now, detect_change);
ba7bd4f7e322 Add global ass_force_reload flag.
eugeni
parents: 25392
diff changeset
315 }