#import "DYManagedColor.h" #import "DYPlugIn.h" #import "../Shared/DYBetaDiagnostics.h" #import "DYModel.h" #import "DYRenderer.h" #import "DYInspector.h" #import "DYColorTables.h" #import "DYAutoGrade.h" #import "DYToolState.h" #include "DYFilmTime.h" #include "DYLevelGeometry.h" #import #import #import #include "MaskCore/DYTracker.hpp" #include "MaskCore/DYOpenCVTracker.hpp" #include "MaskCore/DYObjectTracker.hpp" #include #include "DYViewerParameterBridge.inc.h" #include "DYToolHostDeclarations.inc.h" #include "DYToolFlow.inc.h" @implementation DYPlugIn { DYRenderer *_renderer; NSColorSampler *_canvasSampler; BOOL _copyAnalyzed; std::atomic_bool _betaFirstRenderStarted,_modelRefreshPending; #include "DYToolHostIvars.inc.h" } #include "DYToolHostMethods.inc.h" - (instancetype)initWithAPIManager:(id)manager{uint64_t beta=DYBetaSpanBegin(@"plugin_init",nil);if((self=[super init])){_betaFirstRenderStarted.store(false);_modelRefreshPending.store(false);_apiManager=manager;_renderer=[DYRenderer new];[self initializeToolHost];NSNotificationCenter*n=NSNotificationCenter.defaultCenter;[n addObserver:self selector:@selector(exportRequested:) name:@"DYExportLUT" object:manager];[n addObserver:self selector:@selector(autoRequested:) name:@"DYAutoGrade" object:manager];[n addObserver:self selector:@selector(autoRequested:) name:@"DYAutoWhiteBalance" object:manager];[n addObserver:self selector:@selector(whiteBalanceRequested:) name:@"DYWhiteBalanceColor" object:manager];[n addObserver:self selector:@selector(canvasWhiteBalanceRequested:) name:@"DYViewerWhiteBalanceRequested" object:manager];}DYBetaSpanEnd(beta,nil);return self;} - (void)dealloc{[self tearDownToolHost];[NSNotificationCenter.defaultCenter removeObserver:self];[_canvasSampler release];[_renderer release];[super dealloc];} - (BOOL)properties:(NSDictionary**)p error:(NSError**)e{*p=@{kFxPropertyKey_MayRemapTime:@NO,kFxPropertyKey_PixelTransformSupport:@(kFxPixelTransform_ScaleTranslate),kFxPropertyKey_VariesWhenParamsAreStatic:@YES,kFxPropertyKey_DesiredProcessingColorInfo:@(kFxImageColorInfo_RGB_LINEAR),kFxPropertyKey_NeedsFullBuffer:@YES};return YES;} - (BOOL)addParametersWithError:(NSError**)e{return DYCreateParameters(_apiManager,e);} - (NSSet*)classesForCustomParameterID:(UInt32)pid{return pid==DYToolsParameterID?[NSSet setWithObjects:DYToolDocument.class,NSDictionary.class,NSArray.class,NSString.class,NSNumber.class,NSData.class,nil]:pid==DYAssetsParameterID?[NSSet setWithObjects:DYAssetState.class,NSDictionary.class,NSString.class,NSData.class,nil]:[NSSet setWithObjects:DYInspectorToken.class,NSDictionary.class,NSString.class,NSNumber.class,nil];} - (NSView*)createViewForParameterID:(UInt32)pid { [self scheduleToolCanvasRegistration]; if(pid!=DYInspectorParameterID)return [[NSView alloc]initWithFrame:NSZeroRect]; return [[DYInspector alloc]initWithAPIManager:_apiManager renderer:_renderer]; } - (BOOL)parameterChanged:(UInt32)pid atTime:(CMTime)t error:(NSError**)e{if(pid==DYToolsParameterID)[self adoptToolCanvasDocument:DYReadTools(_apiManager,t).document];if(pid==DYToolsParameterID||pid==2013||pid==2014||(pid>=2236&&pid<2400)||pid==DYAssetsParameterID)[_renderer invalidatePreview];// Many parameter callbacks arrive in one host transaction. Refresh the native // inspector once after the transaction unwinds, rather than queueing a full // remote parameter read and view rebuild for every scalar in the burst. if(!_modelRefreshPending.exchange(true)){id host=_apiManager;dispatch_async(dispatch_get_main_queue(),^{self->_modelRefreshPending.store(false);[NSNotificationCenter.defaultCenter postNotificationName:DYModelChanged object:host];});}return YES;} - (BOOL)pluginState:(NSData**)state atTime:(CMTime)t quality:(FxQuality)q error:(NSError**)e{ uint64_t stateSpan=DYBetaSpanBegin(@"plugin_state",nil);@try{ DYUniforms u;DYReadUniforms(_apiManager,t,&u);CMTime frameDuration=kCMTimeInvalid,origin=kCMTimeZero;idtiming=[_apiManager apiForProtocol:@protocol(FxTimingAPI_v4)];if([timing respondsToSelector:@selector(frameDuration:)])[timing frameDuration:&frameDuration];if([timing respondsToSelector:@selector(startTimeForEffect:)])[timing startTimeForEffect:&origin];int64_t frame=DYFilmFrameIndex(t,frameDuration,origin);u.filmFrameKey=DYFilmFrameKey(frame,u.film.grainHold,u.film.grainSeed);DYAssetState*a=DYReadAssets(_apiManager,t);DYToolDocument *tools=DYReadTools(_apiManager,t);[self adoptToolCanvasDocument:tools.document];[_viewerParameterBridge publishUniforms:&u document:tools.document atTime:t];[_viewerParameterBridge publishAssets:a atTime:t];double copyAuto=0;[[_apiManager apiForProtocol:@protocol(FxParameterRetrievalAPI_v6)]getFloatValue:©Auto fromParameter:8800 atTime:t];NSDictionary *snapshot=DYToolRenderSnapshot(tools.document,t);for(NSMutableDictionary *node in snapshot[@"layers"])if([node[@"type"]isEqual:@"film"]){DYUniforms layer=DYToolUniforms(node);node[@"filmFrameKey"]=@(DYFilmFrameKey(frame,layer.film.grainHold,layer.film.grainSeed));}NSInteger primaries=-1;idgamut=[_apiManager apiForProtocol:@protocol(FxColorGamutAPI_v2)];if([gamut respondsToSelector:@selector(colorPrimaries)])primaries=gamut.colorPrimaries;NSDictionary*s=@{@"colorManagement":a.colorManagement?:@{},@"hostPrimaries":@(primaries),@"schema":@2,@"tools":snapshot,@"copyAuto":@(copyAuto>.5),@"uniforms":[NSData dataWithBytes:&u length:sizeof(u)],@"paths":a.paths?:@{},@"spider":a.spiderMesh?:[NSData data]};*state=[NSPropertyListSerialization dataWithPropertyList:s format:NSPropertyListBinaryFormat_v1_0 options:0 error:e];return *state!=nil; }@finally{DYBetaSpanEnd(stateSpan,nil);} } - (BOOL)destinationImageRect:(FxRect*)rect sourceImages:(NSArray*)src destinationImage:(FxImageTile*)dst pluginState:(NSData*)s atTime:(CMTime)t error:(NSError**)e{if(!src.count)return NO;*rect=src[0].imagePixelBounds;return YES;} - (BOOL)sourceTileRect:(FxRect*)rect sourceImageIndex:(NSUInteger)i sourceImages:(NSArray*)src destinationTileRect:(FxRect)dstRect destinationImage:(FxImageTile*)dst pluginState:(NSData*)s atTime:(CMTime)t error:(NSError**)e{if(i>=src.count)return NO;*rect=src[i].imagePixelBounds;return YES;} - (BOOL)renderDestinationImage:(FxImageTile*)dst sourceImages:(NSArray*)src pluginState:(NSData*)s atTime:(CMTime)t error:(NSError**)e{ if(!src.count||!src[0].ioSurface||!dst.ioSurface||!s.length)return NO;NSDictionary*p=[NSPropertyListSerialization propertyListWithData:s options:NSPropertyListImmutable format:NULL error:e];if(![p isKindOfClass:NSDictionary.class])return NO;NSData*b=p[@"uniforms"];if(![@[@1,@2]containsObject:p[@"schema"]]||![b isKindOfClass:NSData.class]||b.length!=sizeof(DYUniforms)||![p[@"paths"]isKindOfClass:NSDictionary.class])return NO;DYUniforms u;[b getBytes:&u length:sizeof(u)];uint64_t renderSpan=DYBetaSpanBegin(@"render",@{@"width":@(src[0].ioSurface.width),@"height":@(src[0].ioSurface.height),@"layer_count":@([p[@"tools"][@"layers"]count])});uint64_t beta=!_betaFirstRenderStarted.exchange(true)?DYBetaSpanBegin(@"first_render",nil):0;BOOL ok=NO;@try{ok=[_renderer renderSource:src[0] destination:dst uniforms:u paths:p[@"paths"] spider:p[@"spider"] tools:p[@"tools"] colorManagement:p[@"colorManagement"] hostPrimaries:p[@"hostPrimaries"]?[p[@"hostPrimaries"]integerValue]:-1 error:e];}@finally{DYBetaSpanEnd(renderSpan,@{@"status":ok?@"passed":@"failed"});}DYBetaSpanEnd(beta,@{@"status":ok?@"passed":@"failed"});if(!ok)DYBetaEvent(@"render_error",DYBetaErrorFields((e&&*e)?*e:nil,@"render_source"));if(ok){[_renderer captureScopeTile:dst atTime:t];@synchronized(self){ FxRect image=src[0].imagePixelBounds; double aspect=src[0].pixelAspect,height=image.top-image.bottom,width=image.right-image.left; _toolSourceAspect=width>0&&height>0&&std::isfinite(aspect)&&aspect>0?width*aspect/height:0; if(_toolCaptureEnabled&&!_trackingActive.load()){_toolSourceFrame={};_toolSourceTime=t;} }BOOL analyze=NO;@synchronized(self){if(![p[@"copyAuto"]boolValue])_copyAnalyzed=NO;else if(!_copyAnalyzed){_copyAnalyzed=YES;analyze=YES;}}if(analyze){NSDictionary*values=DYAutoGrade([_renderer preview]);if(values)dispatch_async(dispatch_get_main_queue(),^{[self applyValues:values];});}}return ok; } - (void)alert:(NSString*)message{dispatch_async(dispatch_get_main_queue(),^{NSAlert*a=[NSAlert new];a.messageText=@"大鱼调色";a.informativeText=message?:@"操作未完成";[a runModal];[a release];});} - (void)exportRequested:(NSNotification*)n{ NSURL*url=n.userInfo[@"url"];if(![url isKindOfClass:NSURL.class])return;ida=[_apiManager apiForProtocol:@protocol(FxCustomParameterActionAPI_v4)];[a startAction:self];DYUniforms u;DYReadUniforms(_apiManager,a.currentTime,&u);DYAssetState *assets=DYReadAssets(_apiManager,a.currentTime);NSDictionary*paths=[assets.paths copy];NSDictionary *cms=[assets.colorManagement copy];NSData *mesh=[assets.spiderMesh copy];NSDictionary *tools=[DYReadTools(_apiManager,a.currentTime).document copy];[a endAction:self];NSError*e=nil;BOOL ok=[_renderer exportLUT:url uniforms:u paths:paths spider:mesh tools:tools colorManagement:cms error:&e];[cms release];[tools release];[mesh release];[paths release];if(!ok)[self alert:e.localizedDescription]; } - (void)applyValues:(NSDictionary*)values{if(!values){[self alert:@"尚未拿到当前片段画面,请移动播放头后重试。"];return;}ida=[_apiManager apiForProtocol:@protocol(FxCustomParameterActionAPI_v4)];[a startAction:self];@try{ids=[_apiManager apiForProtocol:@protocol(FxParameterSettingAPI_v6)];for(NSDictionary*r in DYDescriptors()){NSNumber*v=values[r[@"key"]];if(v)[s setFloatValue:v.doubleValue toParameter:[r[@"pid"]unsignedIntValue] atTime:a.currentTime];}}@finally{[a endAction:self];}[NSNotificationCenter.defaultCenter postNotificationName:DYModelChanged object:_apiManager];} - (void)autoRequested:(NSNotification*)n{NSData*frame=[_renderer preview];[self applyValues:[n.name isEqual:@"DYAutoGrade"]?DYAutoGrade(frame):DYAutoWhiteBalance(frame)];} - (void)canvasWhiteBalanceRequested:(NSNotification*)note { CMTime time=CMTimeMakeFromDictionary((CFDictionaryRef)note.userInfo[@"time"]); if(_canvasSampler||!CMTIME_IS_NUMERIC(time))return; _canvasSampler=[NSColorSampler new]; [_canvasSampler showSamplerWithSelectionHandler:^(NSColor *color){ [self->_canvasSampler release];self->_canvasSampler=nil; if(color)[NSNotificationCenter.defaultCenter postNotificationName:@"DYWhiteBalanceColor" object:self->_apiManager userInfo:@{@"color":color,@"time":[(NSDictionary*)CMTimeCopyAsDictionary(time,kCFAllocatorDefault) autorelease]}]; }]; } - (void)applyCanvasWhiteBalance:(NSDictionary*)values atTime:(CMTime)time { id action=[_apiManager apiForProtocol:@protocol(FxCustomParameterActionAPI_v4)]; if(!action)return; NSMutableArray *descriptors=[NSMutableArray array]; for(NSDictionary *d in DYDescriptors())if(values[d[@"key"]])[descriptors addObject:d]; NSString *node=nil; NSMutableDictionary *original=[NSMutableDictionary dictionary]; [action startAction:self];@try{ if(CMTimeCompare(DYHostDisplayedFrameTime(_apiManager,action.currentTime),time)!=0)return; NSDictionary *document=DYReadTools(_apiManager,time).document; NSString *selected=[_viewerParameterBridge snapshotAtTime:time][@"selectedNode"]; NSDictionary *layer=DYToolWithID(document,selected); if([layer[@"type"]isEqual:@"base"])node=selected; else if(!DYBaseToolRemoved(document,@"base"))node=@"base:base"; else for(NSString *ID in DYToolDisplayOrder(document))if([DYToolWithID(document,ID)[@"type"]isEqual:@"base"]){node=ID;break;} if(node){ BOOL base=[node isEqual:@"base:base"]; DYUniforms uniforms{}; if(!base)uniforms=DYToolUniforms(DYToolWithID(document,node)); id get=[_apiManager apiForProtocol:@protocol(FxParameterRetrievalAPI_v6)]; for(NSDictionary *d in descriptors){ double value=[d[@"default"]doubleValue]; if(base){if(![get getFloatValue:&value fromParameter:[d[@"pid"]unsignedIntValue] atTime:time])return;} else value=*(float*)((uint8_t*)&uniforms+[d[@"offset"]unsignedIntegerValue]); original[d[@"key"]]=@(value); } } }@finally{[action endAction:self];} if(!node){[self alert:@"请先添加基础调整模块,再吸取白点。"];return;} [_viewerParameterBridge beginParameters:descriptors node:node values:original atTime:time]; [_viewerParameterBridge changeValues:values];[_viewerParameterBridge endGesture]; } - (void)whiteBalanceRequested:(NSNotification*)n { NSColor *color=[n.userInfo[@"color"] colorUsingColorSpace:NSColorSpace.sRGBColorSpace]; if(!color)return; float temperature=0,tint=0; if(![DYColorTables neutralizeColor:(vector_float3){(float)color.redComponent,(float)color.greenComponent,(float)color.blueComponent} temperature:&temperature tint:&tint]){ [self alert:@"所选颜色过暗,请选择白色或中性灰区域。"];return; } NSDictionary *values=@{@"temperature":@(temperature),@"tint":@(tint)}; if(n.userInfo[@"time"])[self applyCanvasWhiteBalance:values atTime:CMTimeMakeFromDictionary((CFDictionaryRef)n.userInfo[@"time"])]; else [self applyValues:values]; } @end #include "DYViewerControls.inc.h" #include "DYToolOSC.inc.h"