2 This file is part of the HandBrake source code.
3 Homepage: <http://handbrake.fr/>.
4 It may be used under the terms of the GNU General Public License. */
7 * Converts SSA subtitles to UTF-8 subtitles with limited HTML-style markup (<b>, <i>, <u>).
9 * SSA format references:
10 * http://www.matroska.org/technical/specs/subtitles/ssa.html
11 * http://moodub.free.fr/video/ass-specs.doc
12 * vlc-1.0.4/modules/codec/subtitles/subsass.c:ParseSSAString
14 * @author David Foster (davidfstr)
27 // "<b></b>".len + "<i></i>".len + "<u></u>".len
28 #define MAX_OVERHEAD_PER_OVERRIDE (7 * 3)
30 #define SSA_2_HB_TIME(hr,min,sec,centi) \
31 ( 90L * ( hr * 1000L * 60 * 60 +\
36 static StyleSet ssa_parse_style_override( uint8_t *pos, StyleSet prevStyles )
38 StyleSet nextStyles = prevStyles;
41 // Skip over leading '{' or last '\\'
44 // Scan for next \code
45 while ( *pos != '\\' && *pos != '}' && *pos != '\0' ) pos++;
48 // End of style override block
52 // If next chars are \[biu][01], interpret it
53 if ( strchr("biu", pos[1]) && strchr("01", pos[2]) )
56 pos[1] == 'b' ? BOLD :
57 pos[1] == 'i' ? ITALIC :
58 pos[1] == 'u' ? UNDERLINE : 0;
59 int enabled = (pos[2] == '1');
63 nextStyles |= styleID;
67 nextStyles &= ~styleID;
74 static void ssa_append_html_tags_for_style_change(
75 uint8_t **dst, StyleSet prevStyles, StyleSet nextStyles )
77 #define APPEND(str) { \
79 while (*src) { *(*dst)++ = *src++; } \
82 // Reverse-order close all previous styles
83 if (prevStyles & UNDERLINE) APPEND("</u>");
84 if (prevStyles & ITALIC) APPEND("</i>");
85 if (prevStyles & BOLD) APPEND("</b>");
87 // Forward-order open all next styles
88 if (nextStyles & BOLD) APPEND("<b>");
89 if (nextStyles & ITALIC) APPEND("<i>");
90 if (nextStyles & UNDERLINE) APPEND("<u>");
97 * Dialogue: Marked,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text
98 * 1 2 3 4 5 6 7 8 9 10
100 static hb_buffer_t *ssa_decode_to_utf8( hb_buffer_t *in )
102 uint8_t *pos = in->data;
103 uint8_t *end = in->data + in->size;
105 // Store NULL after the end of the buffer to make using sscanf safe
106 hb_buffer_realloc( in, in->size + 1 );
107 in->data[in->size] = '\0';
110 * Parse Start and End fields for timing information
112 int start_hr, start_min, start_sec, start_centi;
113 int end_hr, end_min, end_sec, end_centi;
114 int numPartsRead = sscanf( (char *) in->data, "%*128[^,],"
115 "%d:%d:%d.%d," // Start
116 "%d:%d:%d.%d,", // End
117 &start_hr, &start_min, &start_sec, &start_centi,
118 &end_hr, &end_min, &end_sec, &end_centi );
119 if ( numPartsRead != 8 )
122 in->start = SSA_2_HB_TIME(start_hr, start_min, start_sec, start_centi);
123 in->stop = SSA_2_HB_TIME( end_hr, end_min, end_sec, end_centi);
126 * Advance 'pos' to the beginning of the Text field
134 if ( curFieldID == 10 ) // Text
138 if ( curFieldID != 10 )
140 uint8_t *textFieldPos = pos;
142 // Count the number of style overrides in the Text field
143 int numStyleOverrides = 0;
152 int maxOutputSize = (end - textFieldPos) + ((numStyleOverrides + 1) * MAX_OVERHEAD_PER_OVERRIDE);
153 hb_buffer_t *out = hb_buffer_init( maxOutputSize );
158 * The Text field contains plain text marked up with:
160 * (2) '\N' -> newline
161 * (3) curly-brace control codes like '{\k44}' -> empty (strip them)
163 * Perform the above conversions and copy it to the output packet
165 StyleSet prevStyles = 0;
166 uint8_t *dst = out->data;
170 if ( pos[0] == '\\' && pos[1] == 'n' )
175 else if ( pos[0] == '\\' && pos[1] == 'N' )
180 else if ( pos[0] == '{' )
182 // Parse SSA style overrides and append appropriate HTML style tags
183 StyleSet nextStyles = ssa_parse_style_override( pos, prevStyles );
184 ssa_append_html_tags_for_style_change( &dst, prevStyles, nextStyles );
185 prevStyles = nextStyles;
187 // Skip past SSA control code
188 while ( pos < end && *pos != '}' ) pos++;
189 if ( pos < end && *pos == '}' ) pos++;
193 // Copy raw character
198 // Append closing HTML style tags
199 ssa_append_html_tags_for_style_change( &dst, prevStyles, 0 );
201 // Trim output buffer to the actual amount of data written
202 out->size = dst - out->data;
204 // Copy metadata from the input packet to the output packet
205 out->start = in->start;
206 out->stop = in->stop;
211 hb_log( "decssasub: malformed SSA subtitle packet: %.*s\n", in->size, in->data );
215 static int decssaInit( hb_work_object_t * w, hb_job_t * job )
220 static int decssaWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
221 hb_buffer_t ** buf_out )
223 hb_buffer_t * in = *buf_in;
224 hb_buffer_t * out = NULL;
226 if ( in->size > 0 ) {
227 out = ssa_decode_to_utf8(in);
229 out = hb_buffer_init( 0 );
233 // We shouldn't be storing the extra NULL character,
234 // but the MP4 muxer expects this, unfortunately.
235 if ( out->size > 0 && out->data[out->size - 1] != '\0' ) {
236 // NOTE: out->size remains unchanged
237 hb_buffer_realloc( out, out->size + 1 );
238 out->data[out->size] = '\0';
241 // If the input packet was non-empty, do not pass through
242 // an empty output packet (even if the subtitle was empty),
243 // as this would be interpreted as an end-of-stream
244 if ( in->size > 0 && out->size == 0 ) {
245 hb_buffer_close(&out);
249 // Dispose the input packet, as it is no longer needed
250 hb_buffer_close(&in);
257 static void decssaClose( hb_work_object_t * w )
262 hb_work_object_t hb_decssasub =
265 "SSA Subtitle Decoder",