Add a simple pre-processing to vobsub samples and disable styles creation for vobsub.
This commit is contained in:
1 parent
6b73413c7c
commit
2e559043eb
6 files changed
+55
-14
No files matched your search
@@ -21,6 +21,7 @@
|
||||
NSThread *encoderThread;
|
||||
|
||||
MP42OCRWrapper *ocr;
|
||||
CIContext *_imgContext;
|
||||
AVCodec *avCodec;
|
||||
AVCodecContext *avContext;
|
||||
AVSubtitle subtitle;
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
#import "MP42PrivateUtilities.h"
|
||||
#import "MP42Track+Muxer.h"
|
||||
|
||||
#import <QuartzCore/QuartzCore.h>
|
||||
|
||||
#define REGISTER_DECODER(x) { \
|
||||
extern AVCodec ff_##x##_decoder; \
|
||||
avcodec_register(&ff_##x##_decoder); }
|
||||
@@ -32,12 +34,47 @@ void FFInitFFmpeg()
|
||||
});
|
||||
}
|
||||
|
||||
@interface MP42BitmapSubConverter ()
|
||||
|
||||
@property (nonatomic, readonly) CIContext *imgContext;
|
||||
|
||||
@end
|
||||
|
||||
@implementation MP42BitmapSubConverter
|
||||
|
||||
@synthesize imgContext = _imgContext;
|
||||
|
||||
- (CIContext *)imgContext {
|
||||
if (!_imgContext) {
|
||||
_imgContext = [[CIContext contextWithCGContext:[[NSGraphicsContext currentContext] graphicsPort]
|
||||
options:nil] retain];
|
||||
}
|
||||
return _imgContext;
|
||||
}
|
||||
|
||||
- (CGImageRef)createfilteredCGImage:(CGImageRef)image CF_RETURNS_RETAINED
|
||||
{
|
||||
CIImage *ciImage = [CIImage imageWithCGImage:image];
|
||||
CIFilter *contrastFilter = [CIFilter filterWithName:@"CIColorControls"
|
||||
keysAndValues:kCIInputImageKey, ciImage,
|
||||
@"inputContrast", @(1.6f),
|
||||
@"inputBrightness", @(0.4f),
|
||||
nil];
|
||||
CIFilter *invertFilter = [CIFilter filterWithName:@"CIColorInvert"
|
||||
keysAndValues:kCIInputImageKey, [contrastFilter valueForKey:kCIOutputImageKey],
|
||||
nil];
|
||||
|
||||
CIImage *filteredImage = [invertFilter valueForKey:kCIOutputImageKey];
|
||||
CGImageRef filteredImgRef = [self.imgContext createCGImage:filteredImage fromRect:[filteredImage extent]];
|
||||
|
||||
return filteredImgRef;
|
||||
}
|
||||
|
||||
- (void)VobSubDecoderThreadMainRoutine:(id)sender
|
||||
{
|
||||
@autoreleasepool {
|
||||
while(1) {
|
||||
@autoreleasepool {
|
||||
MP42SampleBuffer *sampleBuffer = nil;
|
||||
|
||||
while (!(sampleBuffer = [_inputSamplesBuffer dequeAndWait]) && !_readerDone);
|
||||
@@ -164,23 +201,22 @@ void FFInitFFmpeg()
|
||||
kCGRenderingIntentDefault);
|
||||
CGColorSpaceRelease(colorSpace);
|
||||
|
||||
#ifdef OCR_DEBUG
|
||||
NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]initWithCGImage:(CGImageRef)cgImage];
|
||||
[[bitmapImage representationUsingType:NSTIFFFileType properties:nil] writeToFile:@"/tmp/foo.tif" atomically:YES];
|
||||
[bitmapImage release];
|
||||
#endif
|
||||
NSString *text = [ocr performOCROnCGImage:cgImage];
|
||||
CGImageRef filteredCGImage = [self createfilteredCGImage:cgImage];
|
||||
|
||||
NSString *text = [ocr performOCROnCGImage:filteredCGImage];
|
||||
|
||||
MP42SampleBuffer *subSample = nil;
|
||||
if (text)
|
||||
subSample = copySubtitleSample(sampleBuffer->trackId, text, sampleBuffer->duration, forced, NO, CGSizeMake(0,0), 0);
|
||||
else
|
||||
if (text) {
|
||||
subSample = copySubtitleSample(sampleBuffer->trackId, text, sampleBuffer->duration, forced, NO, NO, CGSizeMake(0,0), 0);
|
||||
} else {
|
||||
subSample = copyEmptySubtitleSample(sampleBuffer->trackId, sampleBuffer->duration, forced);
|
||||
}
|
||||
|
||||
[_outputSamplesBuffer enqueue:subSample];
|
||||
[subSample release];
|
||||
|
||||
CGImageRelease(cgImage);
|
||||
CGImageRelease(filteredCGImage);
|
||||
CGDataProviderRelease(provider);
|
||||
CFRelease(imgData);
|
||||
|
||||
@@ -192,6 +228,7 @@ void FFInitFFmpeg()
|
||||
|
||||
[sampleBuffer release];
|
||||
}
|
||||
}
|
||||
|
||||
_encoderDone = YES;
|
||||
dispatch_semaphore_signal(_done);
|
||||
@@ -283,7 +320,7 @@ void FFInitFFmpeg()
|
||||
|
||||
MP42SampleBuffer *subSample = nil;
|
||||
if ((text = [ocr performOCROnCGImage:cgImage]))
|
||||
subSample = copySubtitleSample(sampleBuffer->trackId, text, sampleBuffer->duration, forced, NO, CGSizeMake(0,0), 0);
|
||||
subSample = copySubtitleSample(sampleBuffer->trackId, text, sampleBuffer->duration, forced, NO, NO, CGSizeMake(0,0), 0);
|
||||
else
|
||||
subSample = copyEmptySubtitleSample(sampleBuffer->trackId, sampleBuffer->duration, forced);
|
||||
|
||||
@@ -414,6 +451,7 @@ void FFInitFFmpeg()
|
||||
|
||||
[decoderThread release];
|
||||
[ocr release];
|
||||
[_imgContext release];
|
||||
|
||||
dispatch_release(_done);
|
||||
[super dealloc];
|
||||
|
||||
@@ -872,7 +872,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
||||
if ([sl->line isEqualToString:@"\n"])
|
||||
sample = copyEmptySubtitleSample(dstTrackId, sl->end_time - sl->begin_time, NO);
|
||||
else
|
||||
sample = copySubtitleSample(dstTrackId, sl->line, sl->end_time - sl->begin_time, NO, NO, CGSizeMake(0, 0), 0);
|
||||
sample = copySubtitleSample(dstTrackId, sl->line, sl->end_time - sl->begin_time, NO, NO, YES, CGSizeMake(0, 0), 0);
|
||||
|
||||
if (!sample)
|
||||
break;
|
||||
|
||||
@@ -99,7 +99,7 @@
|
||||
}
|
||||
else {
|
||||
int top = (sl->top == INT_MAX) ? trackSize.height : sl->top;
|
||||
sample = copySubtitleSample(track.sourceId, sl->line, sl->end_time - sl->begin_time, sl->forced, _verticalPlacement, trackSize, top);
|
||||
sample = copySubtitleSample(track.sourceId, sl->line, sl->end_time - sl->begin_time, sl->forced, _verticalPlacement, YES, trackSize, top);
|
||||
}
|
||||
|
||||
[self enqueue:sample];
|
||||
|
||||
@@ -57,7 +57,7 @@ unsigned ParseSubTime(const char *time, unsigned secondScale, BOOL hasSign);
|
||||
|
||||
@class MP42SampleBuffer;
|
||||
|
||||
MP42SampleBuffer* copySubtitleSample(MP4TrackId subtitleTrackId, NSString* string, MP4Duration duration, BOOL forced, BOOL verticalPlacement, CGSize trackSize, int top);
|
||||
MP42SampleBuffer * copySubtitleSample(MP4TrackId subtitleTrackId, NSString *string, MP4Duration duration, BOOL forced, BOOL verticalPlacement, BOOL styles, CGSize trackSize, int top);
|
||||
MP42SampleBuffer * copyEmptySubtitleSample(MP4TrackId subtitleTrackId, MP4Duration duration, BOOL forced);
|
||||
|
||||
typedef struct {
|
||||
|
||||
@@ -997,14 +997,16 @@ void createTboxAtom(u_int8_t* buffer, u_int16_t top, u_int16_t left, u_int16_t b
|
||||
buffer[15] = right & 0xFF;
|
||||
}
|
||||
|
||||
MP42SampleBuffer* copySubtitleSample(MP4TrackId subtitleTrackId, NSString *string, MP4Duration duration, BOOL forced, BOOL verticalPlacement, CGSize trackSize, int top)
|
||||
MP42SampleBuffer * copySubtitleSample(MP4TrackId subtitleTrackId, NSString *string, MP4Duration duration, BOOL forced, BOOL verticalPlacement, BOOL styles, CGSize trackSize, int top)
|
||||
{
|
||||
u_int8_t *sampleData = NULL, *styleAtom = NULL;
|
||||
size_t styleSize = 0, sampleSize = 0, stringLength = 0;
|
||||
u_int64_t pos = 0;
|
||||
|
||||
string = removeNewLines(string);
|
||||
if (styles) {
|
||||
string = createStyleAtomForString(string, &styleAtom, &styleSize);
|
||||
}
|
||||
|
||||
stringLength = strlen([string UTF8String]);
|
||||
sampleSize = 2 + (stringLength * sizeof(char)) + styleSize + (forced == 1 ? 8 : 0) + (verticalPlacement == 1 ? 16 : 0);
|
||||
|
||||
Reference in new issue
Block a user