1 /*
2  * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
3  *
4  * This file is part of libass.
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef LIBASS_TYPES_H
20 #define LIBASS_TYPES_H
21 
22 #include <stdint.h>
23 
24 #define VALIGN_SUB 0
25 #define VALIGN_CENTER 8
26 #define VALIGN_TOP 4
27 #define HALIGN_LEFT 1
28 #define HALIGN_CENTER 2
29 #define HALIGN_RIGHT 3
30 
31 /* Opaque objects internally used by libass.  Contents are private. */
32 typedef struct ass_renderer ASS_Renderer;
33 typedef struct render_priv ASS_RenderPriv;
34 typedef struct parser_priv ASS_ParserPriv;
35 typedef struct ass_library ASS_Library;
36 
37 #ifdef _MSC_VER
38 	#define strncasecmp(x,y,z) _strnicmp(x,y,z)
39 #endif
40 
41 /* ASS Style: line */
42 typedef struct ass_style {
43     char *Name;
44     char *FontName;
45     double FontSize;
46     uint32_t PrimaryColour;
47     uint32_t SecondaryColour;
48     uint32_t OutlineColour;
49     uint32_t BackColour;
50     int Bold;
51     int Italic;
52     int Underline;
53     int StrikeOut;
54     double ScaleX;
55     double ScaleY;
56     double Spacing;
57     double Angle;
58     int BorderStyle;
59     double Outline;
60     double Shadow;
61     int Alignment;
62     int MarginL;
63     int MarginR;
64     int MarginV;
65     int Encoding;
66     int treat_fontname_as_pattern;
67     double Blur;
68 } ASS_Style;
69 
70 /*
71  * ASS_Event corresponds to a single Dialogue line;
72  * text is stored as-is, style overrides will be parsed later.
73  */
74 typedef struct ass_event {
75     long long Start;            // ms
76     long long Duration;         // ms
77 
78     int ReadOrder;
79     int Layer;
80     int Style;
81     char *Name;
82     int MarginL;
83     int MarginR;
84     int MarginV;
85     char *Effect;
86     char *Text;
87 
88     ASS_RenderPriv *render_priv;
89 } ASS_Event;
90 
91 /**
92  * Support for (xy-)vsfilter mangled colors
93  *
94  * Generally, xy-vsfilter emulates the classic vsfilter behavior of
95  * rendering directly into the (usually YCbCr) video. vsfilter is
96  * hardcoded to use BT.601(TV) as target colorspace when converting
97  * the subtitle RGB color to the video colorspace. This led to major
98  * breakage when HDTV video was introduced: HDTV typically uses
99  * BT.709(TV), but vsfilter still used BT.601(TV) for conversion.
100  *
101  * This means classic vsfilter will mangle colors as follows:
102  *
103  *    screen_rgb = bt_709tv_to_rgb(rgb_to_bt601tv(ass_rgb))
104  *
105  * Or in general:
106  *
107  *    screen_rgb = video_csp_to_rgb(rgb_to_bt601tv(ass_rgb))
108  *
109  * where video_csp is the colorspace of the video with which the
110  * subtitle was muxed.
111  *
112  * xy-vsfilter did not fix this, but instead introduced explicit
113  * rules how colors were mangled by adding a "YCbCr Matrix" header.
114  * If this header specifies a real colorspace (like BT.601(TV) etc.),
115  * xy-vsfilter behaves exactly like vsfilter, but using the specified
116  * colorspace for conversion of ASS input RGB to screen RGB:
117  *
118  *    screen_rgb = video_csp_to_rgb(rgb_to_ycbcr_header_csp(ass_rgb))
119  *
120  * Further, xy-vsfilter behaves like vsfilter with no changes if the header
121  * is missing.
122  *
123  * The special value "None" means untouched RGB values. Keep in mind that
124  * some version of xy-vsfilter are buggy and don't interpret this correctly.
125  * It appears some people are advocating that this header value is
126  * intended for situations where exact colors do not matter.
127  *
128  * Note that newer Aegisub versions (the main application to produce ASS
129  * subtitle scripts) have an option that tries not to mangle the colors. It
130  * is said that if the header is not set to BT.601(TV), the colors are
131  * supposed not to be mangled, even if the "YCbCr Matrix" header is not
132  * set to "None". In other words, the video colorspace as detected by
133  * Aegisub is the same as identified in the file header.
134  *
135  * In general, misinterpreting this header or not using it will lead to
136  * slightly different subtitle colors, which can matter if the subtitle
137  * attempts to match solid colored areas in the video.
138  *
139  * Note that libass doesn't change colors based on this header. It
140  * absolutely can't do that, because the video colorspace is required
141  * in order to handle this as intended by xy-vsfilter.
142  */
143 typedef enum ASS_YCbCrMatrix {
144     YCBCR_DEFAULT = 0,  // Header missing
145     YCBCR_UNKNOWN,      // Header could not be parsed correctly
146     YCBCR_NONE,         // "None" special value
147     YCBCR_BT601_TV,
148     YCBCR_BT601_PC,
149     YCBCR_BT709_TV,
150     YCBCR_BT709_PC,
151     YCBCR_SMPTE240M_TV,
152     YCBCR_SMPTE240M_PC,
153     YCBCR_FCC_TV,
154     YCBCR_FCC_PC
155 } ASS_YCbCrMatrix;
156 
157 /*
158  * ass track represent either an external script or a matroska subtitle stream
159  * (no real difference between them); it can be used in rendering after the
160  * headers are parsed (i.e. events format line read).
161  */
162 typedef struct ass_track {
163     int n_styles;           // amount used
164     int max_styles;         // amount allocated
165     int n_events;
166     int max_events;
167     ASS_Style *styles;    // array of styles, max_styles length, n_styles used
168     ASS_Event *events;    // the same as styles
169 
170     char *style_format;     // style format line (everything after "Format: ")
171     char *event_format;     // event format line
172 
173     enum {
174         TRACK_TYPE_UNKNOWN = 0,
175         TRACK_TYPE_ASS,
176         TRACK_TYPE_SSA
177     } track_type;
178 
179     // Script header fields
180     int PlayResX;
181     int PlayResY;
182     double Timer;
183     int WrapStyle;
184     int ScaledBorderAndShadow;
185     int Kerning;
186     char *Language;
187     ASS_YCbCrMatrix YCbCrMatrix;
188 
189     int default_style;      // index of default style
190     char *name;             // file name in case of external subs, 0 for streams
191 
192     ASS_Library *library;
193     ASS_ParserPriv *parser_priv;
194 } ASS_Track;
195 
196 #endif /* LIBASS_TYPES_H */
197