From 300e4a02490481345808e6cd010cc094889649c0 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Tue, 3 Oct 2017 12:26:40 +0200 Subject: [PATCH] Fixed some regressions in the ssa converter. --- MP42/MP42SSAConverter.m | 62 +++++++++++++++++++++++++------------ MP42/MP42TextSubConverter.m | 45 +++++++++++++++------------ 2 files changed, 68 insertions(+), 39 deletions(-) diff --git a/MP42/MP42SSAConverter.m b/MP42/MP42SSAConverter.m index 330e6cd..0b76639 100644 --- a/MP42/MP42SSAConverter.m +++ b/MP42/MP42SSAConverter.m @@ -68,8 +68,25 @@ typedef NS_ENUM(NSUInteger, MP42SSATokenType) { { NSMutableString *result = [NSMutableString string]; NSArray *tokens = tokenizer(line.text); + NSUInteger textLength = 0; BOOL drawingMode = NO; + BOOL bold = NO; + BOOL italic = NO; + BOOL underlined = NO; + + if (line.style.bold) { + [result insertString:@"" atIndex:0]; + bold = YES; + } + if (line.style.underline) { + [result insertString:@"" atIndex:0]; + underlined = YES; + } + if (line.style.italic) { + [result insertString:@"" atIndex:0]; + italic = YES; + } for (MP42SSAToken *token in tokens) { NSString *textToAppend = nil; @@ -77,23 +94,29 @@ typedef NS_ENUM(NSUInteger, MP42SSATokenType) { if (token->_type == MP42SSATokenTypeText) { textToAppend = token->_text; } - else if (token->_type == MP42SSATokenTypeBoldOpen) { + else if (token->_type == MP42SSATokenTypeBoldOpen && bold == NO) { textToAppend = @""; + bold = YES; } - else if (token->_type == MP42SSATokenTypeBoldClose) { + else if (token->_type == MP42SSATokenTypeBoldClose && bold == YES) { textToAppend = @""; + bold = NO; } - else if (token->_type == MP42SSATokenTypeItalicOpen) { + else if (token->_type == MP42SSATokenTypeItalicOpen && italic == NO) { textToAppend = @""; + italic = YES; } - else if (token->_type == MP42SSATokenTypeItalicClose) { + else if (token->_type == MP42SSATokenTypeItalicClose && italic == YES) { textToAppend = @""; + italic = NO; } - else if (token->_type == MP42SSATokenTypeUnderlinedOpen) { + else if (token->_type == MP42SSATokenTypeUnderlinedOpen && underlined == NO) { textToAppend = @""; + underlined = YES; } - else if (token->_type == MP42SSATokenTypeUnderlinedClose) { + else if (token->_type == MP42SSATokenTypeUnderlinedClose && underlined == YES) { textToAppend = @""; + underlined = NO; } else if (token->_type == MP42SSATokenTypeDrawingOpen) { drawingMode = YES; @@ -104,25 +127,26 @@ typedef NS_ENUM(NSUInteger, MP42SSATokenType) { if (textToAppend && drawingMode == NO) { [result appendString:textToAppend]; + if (token->_type == MP42SSATokenTypeText) { + textLength += textToAppend.length; + } } } + + if (bold) { + [result appendString:@""]; + } + if (underlined) { + [result appendString:@""]; + } + if (italic) { + [result appendString:@""]; + } [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) { - if (line.style.bold) { - [result insertString:@"" atIndex:0]; - } - if (line.style.underline) { - [result insertString:@"" atIndex:0]; - } - if (line.style.italic) { - [result insertString:@"" atIndex:0]; - } - } - - return result; + return textLength ? result : @""; } static inline NSArray *tokenizer(NSString *line) diff --git a/MP42/MP42TextSubConverter.m b/MP42/MP42TextSubConverter.m index 46e9d99..f1439c7 100644 --- a/MP42/MP42TextSubConverter.m +++ b/MP42/MP42TextSubConverter.m @@ -56,27 +56,29 @@ break; } - NSString *text = [[NSString alloc] initWithBytes:sampleBuffer->data - length:sampleBuffer->size - encoding:NSUTF8StringEncoding]; + if (sampleBuffer->size > 1) { + NSString *text = [[NSString alloc] initWithBytes:sampleBuffer->data + length:sampleBuffer->size - 1 + encoding:NSUTF8StringEncoding]; - if (_converter && text.length) { - if (_converter) { - MP42SSALine *SSAline = [_parser addLine:text]; - if (SSAline) { - text = [_converter convertLine:SSAline]; - } - else { - text = nil; + if (_converter && text.length) { + if (_converter) { + MP42SSALine *SSAline = [_parser addLine:text]; + if (SSAline) { + text = [_converter convertLine:SSAline]; + } + else { + text = nil; + } } } - } - if (text.length) { - MP42SubLine *sl = [[MP42SubLine alloc] initWithLine:text - start:sampleBuffer->decodeTimestamp - end:sampleBuffer->decodeTimestamp + sampleBuffer->duration]; - [_ss addLine:sl]; + if (text.length) { + MP42SubLine *sl = [[MP42SubLine alloc] initWithLine:text + start:sampleBuffer->decodeTimestamp + end:sampleBuffer->decodeTimestamp + sampleBuffer->duration]; + [_ss addLine:sl]; + } } } } @@ -89,16 +91,19 @@ if ((self = [super init])) { MP42SubtitleCodecType format = track.format; + _ss = [[MP42SubSerializer alloc] init]; + _inputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100]; + _done = dispatch_semaphore_create(0); + if (format == kMP42SubtitleCodecType_SSA) { NSData *cookie = [track.importer magicCookieForTrack:track]; NSString *cookieString = [[NSString alloc] initWithData:cookie encoding:NSUTF8StringEncoding]; _parser = [[MP42SSAParser alloc] initWithMKVHeader:cookieString]; _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; _decoderThread = [[NSThread alloc] initWithTarget:self selector:@selector(TextConverterThreadMainRoutine) object:nil];