Fixed some regressions in the ssa converter.

This commit is contained in:
Damiano Galassi committed 2017-10-03 12:26:40 +02:00
1 parent 8d8aaa0be6
commit 300e4a0249
2 files changed
+68 -39

No files matched your search

+43 -19
View File
@@ -68,8 +68,25 @@ typedef NS_ENUM(NSUInteger, MP42SSATokenType) {
{ {
NSMutableString *result = [NSMutableString string]; NSMutableString *result = [NSMutableString string];
NSArray<MP42SSAToken *> *tokens = tokenizer(line.text); NSArray<MP42SSAToken *> *tokens = tokenizer(line.text);
NSUInteger textLength = 0;
BOOL drawingMode = NO; BOOL drawingMode = NO;
BOOL bold = NO;
BOOL italic = NO;
BOOL underlined = NO;
if (line.style.bold) {
[result insertString:@"<b>" atIndex:0];
bold = YES;
}
if (line.style.underline) {
[result insertString:@"<u>" atIndex:0];
underlined = YES;
}
if (line.style.italic) {
[result insertString:@"<i>" atIndex:0];
italic = YES;
}
for (MP42SSAToken *token in tokens) { for (MP42SSAToken *token in tokens) {
NSString *textToAppend = nil; NSString *textToAppend = nil;
@@ -77,23 +94,29 @@ typedef NS_ENUM(NSUInteger, MP42SSATokenType) {
if (token->_type == MP42SSATokenTypeText) { if (token->_type == MP42SSATokenTypeText) {
textToAppend = token->_text; textToAppend = token->_text;
} }
else if (token->_type == MP42SSATokenTypeBoldOpen) { else if (token->_type == MP42SSATokenTypeBoldOpen && bold == NO) {
textToAppend = @"<b>"; textToAppend = @"<b>";
bold = YES;
} }
else if (token->_type == MP42SSATokenTypeBoldClose) { else if (token->_type == MP42SSATokenTypeBoldClose && bold == YES) {
textToAppend = @"</b>"; textToAppend = @"</b>";
bold = NO;
} }
else if (token->_type == MP42SSATokenTypeItalicOpen) { else if (token->_type == MP42SSATokenTypeItalicOpen && italic == NO) {
textToAppend = @"<i>"; textToAppend = @"<i>";
italic = YES;
} }
else if (token->_type == MP42SSATokenTypeItalicClose) { else if (token->_type == MP42SSATokenTypeItalicClose && italic == YES) {
textToAppend = @"</i>"; textToAppend = @"</i>";
italic = NO;
} }
else if (token->_type == MP42SSATokenTypeUnderlinedOpen) { else if (token->_type == MP42SSATokenTypeUnderlinedOpen && underlined == NO) {
textToAppend = @"<u>"; textToAppend = @"<u>";
underlined = YES;
} }
else if (token->_type == MP42SSATokenTypeUnderlinedClose) { else if (token->_type == MP42SSATokenTypeUnderlinedClose && underlined == YES) {
textToAppend = @"</u>"; textToAppend = @"</u>";
underlined = NO;
} }
else if (token->_type == MP42SSATokenTypeDrawingOpen) { else if (token->_type == MP42SSATokenTypeDrawingOpen) {
drawingMode = YES; drawingMode = YES;
@@ -104,25 +127,26 @@ typedef NS_ENUM(NSUInteger, MP42SSATokenType) {
if (textToAppend && drawingMode == NO) { if (textToAppend && drawingMode == NO) {
[result appendString:textToAppend]; [result appendString:textToAppend];
if (token->_type == MP42SSATokenTypeText) {
textLength += textToAppend.length;
}
} }
} }
if (bold) {
[result appendString:@"</b>"];
}
if (underlined) {
[result appendString:@"</u>"];
}
if (italic) {
[result appendString:@"</i>"];
}
[result replaceOccurrencesOfString:@"\\N" withString:@"\n" options:NSLiteralSearch range:NSMakeRange(0, result.length)]; [result replaceOccurrencesOfString:@"\\N" withString:@"\n" options:NSLiteralSearch range:NSMakeRange(0, result.length)];
[result replaceOccurrencesOfString:@"\\n" withString:@"\n" options:NSLiteralSearch range:NSMakeRange(0, result.length)]; [result replaceOccurrencesOfString:@"\\n" withString:@"\n" options:NSLiteralSearch range:NSMakeRange(0, result.length)];
if (result.length) { return textLength ? result : @"";
if (line.style.bold) {
[result insertString:@"<b>" atIndex:0];
}
if (line.style.underline) {
[result insertString:@"<u>" atIndex:0];
}
if (line.style.italic) {
[result insertString:@"<i>" atIndex:0];
}
}
return result;
} }
static inline NSArray<MP42SSAToken *> *tokenizer(NSString *line) static inline NSArray<MP42SSAToken *> *tokenizer(NSString *line)
+25 -20
View File
@@ -56,27 +56,29 @@
break; break;
} }
NSString *text = [[NSString alloc] initWithBytes:sampleBuffer->data if (sampleBuffer->size > 1) {
length:sampleBuffer->size NSString *text = [[NSString alloc] initWithBytes:sampleBuffer->data
encoding:NSUTF8StringEncoding]; length:sampleBuffer->size - 1
encoding:NSUTF8StringEncoding];
if (_converter && text.length) { if (_converter && text.length) {
if (_converter) { if (_converter) {
MP42SSALine *SSAline = [_parser addLine:text]; MP42SSALine *SSAline = [_parser addLine:text];
if (SSAline) { if (SSAline) {
text = [_converter convertLine:SSAline]; text = [_converter convertLine:SSAline];
} }
else { else {
text = nil; text = nil;
}
} }
} }
}
if (text.length) { if (text.length) {
MP42SubLine *sl = [[MP42SubLine alloc] initWithLine:text MP42SubLine *sl = [[MP42SubLine alloc] initWithLine:text
start:sampleBuffer->decodeTimestamp start:sampleBuffer->decodeTimestamp
end:sampleBuffer->decodeTimestamp + sampleBuffer->duration]; end:sampleBuffer->decodeTimestamp + sampleBuffer->duration];
[_ss addLine:sl]; [_ss addLine:sl];
}
} }
} }
} }
@@ -89,16 +91,19 @@
if ((self = [super init])) { if ((self = [super init])) {
MP42SubtitleCodecType format = track.format; MP42SubtitleCodecType format = track.format;
_ss = [[MP42SubSerializer alloc] init];
_inputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
_done = dispatch_semaphore_create(0);
if (format == kMP42SubtitleCodecType_SSA) { if (format == kMP42SubtitleCodecType_SSA) {
NSData *cookie = [track.importer magicCookieForTrack:track]; NSData *cookie = [track.importer magicCookieForTrack:track];
NSString *cookieString = [[NSString alloc] initWithData:cookie encoding:NSUTF8StringEncoding]; NSString *cookieString = [[NSString alloc] initWithData:cookie encoding:NSUTF8StringEncoding];
_parser = [[MP42SSAParser alloc] initWithMKVHeader:cookieString]; _parser = [[MP42SSAParser alloc] initWithMKVHeader:cookieString];
_converter = [[MP42SSAConverter alloc] initWithParser:_parser]; _converter = [[MP42SSAConverter alloc] initWithParser:_parser];
[_ss setSSA:YES];
} }
_ss = [[MP42SubSerializer alloc] init];
_inputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
_done = dispatch_semaphore_create(0);
_trackID = track.sourceId; _trackID = track.sourceId;
_decoderThread = [[NSThread alloc] initWithTarget:self selector:@selector(TextConverterThreadMainRoutine) object:nil]; _decoderThread = [[NSThread alloc] initWithTarget:self selector:@selector(TextConverterThreadMainRoutine) object:nil];