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;
|
NSThread *encoderThread;
|
||||||
|
|
||||||
MP42OCRWrapper *ocr;
|
MP42OCRWrapper *ocr;
|
||||||
|
CIContext *_imgContext;
|
||||||
AVCodec *avCodec;
|
AVCodec *avCodec;
|
||||||
AVCodecContext *avContext;
|
AVCodecContext *avContext;
|
||||||
AVSubtitle subtitle;
|
AVSubtitle subtitle;
|
||||||
|
|||||||
@@ -18,6 +18,8 @@
|
|||||||
#import "MP42PrivateUtilities.h"
|
#import "MP42PrivateUtilities.h"
|
||||||
#import "MP42Track+Muxer.h"
|
#import "MP42Track+Muxer.h"
|
||||||
|
|
||||||
|
#import <QuartzCore/QuartzCore.h>
|
||||||
|
|
||||||
#define REGISTER_DECODER(x) { \
|
#define REGISTER_DECODER(x) { \
|
||||||
extern AVCodec ff_##x##_decoder; \
|
extern AVCodec ff_##x##_decoder; \
|
||||||
avcodec_register(&ff_##x##_decoder); }
|
avcodec_register(&ff_##x##_decoder); }
|
||||||
@@ -32,12 +34,47 @@ void FFInitFFmpeg()
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@interface MP42BitmapSubConverter ()
|
||||||
|
|
||||||
|
@property (nonatomic, readonly) CIContext *imgContext;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
@implementation MP42BitmapSubConverter
|
@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
|
- (void)VobSubDecoderThreadMainRoutine:(id)sender
|
||||||
{
|
{
|
||||||
@autoreleasepool {
|
@autoreleasepool {
|
||||||
while(1) {
|
while(1) {
|
||||||
|
@autoreleasepool {
|
||||||
MP42SampleBuffer *sampleBuffer = nil;
|
MP42SampleBuffer *sampleBuffer = nil;
|
||||||
|
|
||||||
while (!(sampleBuffer = [_inputSamplesBuffer dequeAndWait]) && !_readerDone);
|
while (!(sampleBuffer = [_inputSamplesBuffer dequeAndWait]) && !_readerDone);
|
||||||
@@ -164,23 +201,22 @@ void FFInitFFmpeg()
|
|||||||
kCGRenderingIntentDefault);
|
kCGRenderingIntentDefault);
|
||||||
CGColorSpaceRelease(colorSpace);
|
CGColorSpaceRelease(colorSpace);
|
||||||
|
|
||||||
#ifdef OCR_DEBUG
|
CGImageRef filteredCGImage = [self createfilteredCGImage:cgImage];
|
||||||
NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]initWithCGImage:(CGImageRef)cgImage];
|
|
||||||
[[bitmapImage representationUsingType:NSTIFFFileType properties:nil] writeToFile:@"/tmp/foo.tif" atomically:YES];
|
NSString *text = [ocr performOCROnCGImage:filteredCGImage];
|
||||||
[bitmapImage release];
|
|
||||||
#endif
|
|
||||||
NSString *text = [ocr performOCROnCGImage:cgImage];
|
|
||||||
|
|
||||||
MP42SampleBuffer *subSample = nil;
|
MP42SampleBuffer *subSample = nil;
|
||||||
if (text)
|
if (text) {
|
||||||
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
|
} else {
|
||||||
subSample = copyEmptySubtitleSample(sampleBuffer->trackId, sampleBuffer->duration, forced);
|
subSample = copyEmptySubtitleSample(sampleBuffer->trackId, sampleBuffer->duration, forced);
|
||||||
|
}
|
||||||
|
|
||||||
[_outputSamplesBuffer enqueue:subSample];
|
[_outputSamplesBuffer enqueue:subSample];
|
||||||
[subSample release];
|
[subSample release];
|
||||||
|
|
||||||
CGImageRelease(cgImage);
|
CGImageRelease(cgImage);
|
||||||
|
CGImageRelease(filteredCGImage);
|
||||||
CGDataProviderRelease(provider);
|
CGDataProviderRelease(provider);
|
||||||
CFRelease(imgData);
|
CFRelease(imgData);
|
||||||
|
|
||||||
@@ -192,6 +228,7 @@ void FFInitFFmpeg()
|
|||||||
|
|
||||||
[sampleBuffer release];
|
[sampleBuffer release];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_encoderDone = YES;
|
_encoderDone = YES;
|
||||||
dispatch_semaphore_signal(_done);
|
dispatch_semaphore_signal(_done);
|
||||||
@@ -283,7 +320,7 @@ void FFInitFFmpeg()
|
|||||||
|
|
||||||
MP42SampleBuffer *subSample = nil;
|
MP42SampleBuffer *subSample = nil;
|
||||||
if ((text = [ocr performOCROnCGImage:cgImage]))
|
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
|
else
|
||||||
subSample = copyEmptySubtitleSample(sampleBuffer->trackId, sampleBuffer->duration, forced);
|
subSample = copyEmptySubtitleSample(sampleBuffer->trackId, sampleBuffer->duration, forced);
|
||||||
|
|
||||||
@@ -414,6 +451,7 @@ void FFInitFFmpeg()
|
|||||||
|
|
||||||
[decoderThread release];
|
[decoderThread release];
|
||||||
[ocr release];
|
[ocr release];
|
||||||
|
[_imgContext release];
|
||||||
|
|
||||||
dispatch_release(_done);
|
dispatch_release(_done);
|
||||||
[super dealloc];
|
[super dealloc];
|
||||||
|
|||||||
@@ -872,7 +872,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
if ([sl->line isEqualToString:@"\n"])
|
if ([sl->line isEqualToString:@"\n"])
|
||||||
sample = copyEmptySubtitleSample(dstTrackId, sl->end_time - sl->begin_time, NO);
|
sample = copyEmptySubtitleSample(dstTrackId, sl->end_time - sl->begin_time, NO);
|
||||||
else
|
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)
|
if (!sample)
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -99,7 +99,7 @@
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
int top = (sl->top == INT_MAX) ? trackSize.height : sl->top;
|
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];
|
[self enqueue:sample];
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ unsigned ParseSubTime(const char *time, unsigned secondScale, BOOL hasSign);
|
|||||||
|
|
||||||
@class MP42SampleBuffer;
|
@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);
|
MP42SampleBuffer * copyEmptySubtitleSample(MP4TrackId subtitleTrackId, MP4Duration duration, BOOL forced);
|
||||||
|
|
||||||
typedef struct {
|
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;
|
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;
|
u_int8_t *sampleData = NULL, *styleAtom = NULL;
|
||||||
size_t styleSize = 0, sampleSize = 0, stringLength = 0;
|
size_t styleSize = 0, sampleSize = 0, stringLength = 0;
|
||||||
u_int64_t pos = 0;
|
u_int64_t pos = 0;
|
||||||
|
|
||||||
string = removeNewLines(string);
|
string = removeNewLines(string);
|
||||||
|
if (styles) {
|
||||||
string = createStyleAtomForString(string, &styleAtom, &styleSize);
|
string = createStyleAtomForString(string, &styleAtom, &styleSize);
|
||||||
|
}
|
||||||
|
|
||||||
stringLength = strlen([string UTF8String]);
|
stringLength = strlen([string UTF8String]);
|
||||||
sampleSize = 2 + (stringLength * sizeof(char)) + styleSize + (forced == 1 ? 8 : 0) + (verticalPlacement == 1 ? 16 : 0);
|
sampleSize = 2 + (stringLength * sizeof(char)) + styleSize + (forced == 1 ? 8 : 0) + (verticalPlacement == 1 ? 16 : 0);
|
||||||
|
|||||||
Reference in new issue
Block a user