diff --git a/tools/verify_real_tracking.mm b/tools/verify_real_tracking.mm new file mode 100644 index 0000000..480266f --- /dev/null +++ b/tools/verify_real_tracking.mm @@ -0,0 +1,53 @@ +// Read-only tracker probe for caller-decoded top-first gray8 frames. +// Media remains outside the repository. This is independent of host scheduling. +#import +#include "../大鱼调色/Plugin/MaskCore/DYTracker.hpp" +#include "../大鱼调色/Plugin/DYToolFlow.inc.h" +#include +#include + +class MeasuredFlow final : public dy::FlowBackend { + std::shared_ptr base_; +public: + size_t matches=0; + explicit MeasuredFlow(std::shared_ptr base):base_(std::move(base)){} + bool reset(const dy::GrayFrame& f) override {matches=0;return base_->reset(f);} + bool advance(const dy::GrayFrame& f,const std::vector& p, + std::vector& out) override { + bool ok=base_->advance(f,p,out);matches=out.size();return ok; + } +}; + +int main(int argc,const char*argv[]) {@autoreleasepool { + if(argc!=14){fprintf(stderr,"gray8 width height start end step metallib|- cx cy width height angle output.json\n");return 2;} + const int width=atoi(argv[2]),height=atoi(argv[3]),start=atoi(argv[4]),end=atoi(argv[5]),step=atoi(argv[6]); + if(width<16||height<16||start<0||end<0||(step!=1&&step!=-1)||(end-start)*step<0)return 2; + dy::TrackerROI roi{float(atof(argv[8])),float(atof(argv[9])),float(atof(argv[10])),float(atof(argv[11])),float(atof(argv[12]))}; + std::ifstream input(argv[1],std::ios::binary);if(!input)return 2; + auto read=[&](int index){ + std::vector bytes(size_t(width)*height); + input.clear();input.seekg(std::streamoff(index)*bytes.size());input.read((char*)bytes.data(),bytes.size()); + if(input.gcount()!=std::streamsize(bytes.size()))return dy::GrayFrame{}; + dy::GrayFrame f{width,height,std::vector(bytes.size())}; + for(size_t i=0;i measured; + bool metal=strcmp(argv[7],"-")!=0; + if(metal){NSError *error=nil;auto backend=DYToolCreateMetalFlow([NSURL fileURLWithPath:[NSString stringWithUTF8String:argv[7]]],&error);if(!backend){fprintf(stderr,"%s\n",error.localizedDescription.UTF8String);return 2;}measured=std::make_shared(backend);tracker.setBackend(measured);} + tracker.setMask({{-.5f,-.5f},{.5f,-.5f},{.5f,.5f},{-.5f,.5f}}); + NSMutableArray *frames=[NSMutableArray array];bool complete=true; + auto beginning=std::chrono::steady_clock::now(); + for(int index=start;(end-index)*step>=0;index+=step){ + auto frame=read(index);if(!frame.valid())return 2; + bool valid=index==start?tracker.reset(frame,roi):tracker.track(frame).valid; + auto r=tracker.result(); + [frames addObject:@{@"frame":@(index),@"valid":@(valid),@"features":@(r.features),@"flowMatches":@(r.matches),@"inliers":@(r.inliers),@"confidence":@(r.confidence),@"medianResidualPixels":@(r.medianError),@"failure":@((int)r.failure),@"centerX":@(r.cx),@"centerY":@(r.cy),@"scaleX":@(r.scaleX),@"scaleY":@(r.scaleY),@"rotation":@(r.rotation)}]; + if(!valid){complete=false;break;} + } + auto milliseconds=std::chrono::duration(std::chrono::steady_clock::now()-beginning).count(); + NSDictionary *report=@{@"complete":@(complete),@"backend":metal?@"Metal":@"CPU",@"width":@(width),@"height":@(height),@"start":@(start),@"end":@(end),@"milliseconds":@(milliseconds),@"frames":frames,@"scope":@"Caller-decoded top-first gray8 frames; no host timing or original-video equivalence claim."}; + NSData *json=[NSJSONSerialization dataWithJSONObject:report options:NSJSONWritingPrettyPrinted error:nil]; + if(![json writeToFile:[NSString stringWithUTF8String:argv[13]] atomically:YES])return 2; + fprintf(stdout,"%s frames=%lu last=%d complete=%d\n",metal?"Metal":"CPU",(unsigned long)frames.count,[frames.lastObject[@"frame"]intValue],complete);return 0; +}} diff --git a/大鱼调色/Plugin/DYToolHostMethods.inc.h b/大鱼调色/Plugin/DYToolHostMethods.inc.h index 9db9b38..3091433 100644 --- a/大鱼调色/Plugin/DYToolHostMethods.inc.h +++ b/大鱼调色/Plugin/DYToolHostMethods.inc.h @@ -36,10 +36,14 @@ ida=[_apiManager apiForProtocol:@protocol(FxCustomParameterActionAPI_v4)];if(!a)return;[a startAction:self];@try{ idanalysis=[_apiManager apiForProtocol:@protocol(FxAnalysisAPI)];FxAnalysisState state=[analysis analysisStateForEffect];if(!analysis||state==kFxAnalysisState_AnalysisRequested||state==kFxAnalysisState_AnalysisStarted){[self toolStatus:@"宿主未就绪,请等待当前分析结束"];return;} CMTime time=a.currentTime;NSDictionary *doc=DYReadTools(_apiManager,time).document;NSDictionary *mask=[self toolMask:notification.userInfo[@"mask"] node:notification.userInfo[@"node"] document:doc];if(!mask||![mask[@"enabled"]boolValue]){[self toolStatus:@"请先选择当前工具的启用蒙版"];return;}DYMaskParameters seed=DYMaskAtTime(mask,time);if(seed.kind>DYMaskBezier||(seed.kind==DYMaskBezier&&[mask[@"nodes"]length]<3*sizeof(DYBezierNode))){[self toolStatus:@"跟踪需要矩形、椭圆或闭合钢笔区域"];return;} - @synchronized(self){if(!_toolSourceFrame.valid()||!CMTIME_IS_NUMERIC(_toolSourceTime)||CMTimeCompare(_toolSourceTime,time)!=0){[self toolStatus:@"请移动播放头至起点,等待当前画面完成后再开始跟踪"];return;}_trackingReferenceFrame=_toolSourceFrame;} + // The host may reuse an already rendered frame without calling our renderer. + // Analyzer input is authoritative; a matching preview is only an optional + // orientation check, never a prerequisite for starting at the playhead. + @synchronized(self){_trackingReferenceFrame={};if(_toolSourceFrame.valid()&&CMTIME_IS_NUMERIC(_toolSourceTime)&&CMTimeCompare(_toolSourceTime,time)==0)_trackingReferenceFrame=_toolSourceFrame;} idtiming=[_apiManager apiForProtocol:@protocol(FxTimingAPI_v4)];NSUInteger numerator=[timing timelineFpsNumeratorForEffect:self],denominator=[timing timelineFpsDenominatorForEffect:self];_trackingFrameDuration=numerator&&denominator&&numerator<=INT32_MAX?CMTimeMake(denominator,(int32_t)numerator):kCMTimeInvalid;if(!CMTIME_IS_NUMERIC(_trackingFrameDuration)||_trackingFrameDuration.value<=0){[self toolStatus:@"宿主未提供精确帧长,跟踪未启动"];return;} [_trackingNode release];_trackingNode=[notification.userInfo[@"node"]copy];[_trackingMaskID release];_trackingMaskID=[notification.userInfo[@"mask"]copy];[_trackingFrozenMask release];_trackingFrozenMask=[mask copy];[_trackingCandidate release];_trackingCandidate=[DYMutablePlist(mask)retain];[_trackingSamples release];_trackingSamples=[NSMutableArray new];[_trackingMessage release];_trackingMessage=nil; _trackingReferenceTime=time;_trackingSeed=seed;_trackingBackward=direction<0;_trackingBoth=direction==0;_trackingSingle=std::abs((int)direction)==2;_trackingDiscard=NO;_trackingCancelled.store(false);_trackingFinishing.store(false);_trackingActive.store(true);++_trackingGeneration;_trackingStarted=NSProcessInfo.processInfo.systemUptime; + os_log(OS_LOG_DEFAULT,"dayu_tool_track_start direction=%{public}ld reference=%{public}.6f cached=%{public}d",(long)direction,CMTimeGetSeconds(time),_trackingReferenceFrame.valid()); NSError *error=nil;BOOL started=_trackingBackward?[analysis startBackwardAnalysis:kFxAnalysisLocation_GPU error:&error]:[analysis startForwardAnalysis:kFxAnalysisLocation_GPU error:&error];if(!started){_trackingActive.store(false);[self toolStatus:error.localizedDescription?:@"宿主未启动逐帧分析"];return;} [self toolStatus:@"正在读取宿主源帧并跟踪…"];if(!_toolAnalysisTimer)_toolAnalysisTimer=[[NSTimer scheduledTimerWithTimeInterval:.3 target:self selector:@selector(pollToolAnalysis:) userInfo:nil repeats:YES]retain]; }@finally{[a endAction:self];} @@ -62,12 +66,12 @@ if(!_trackingActive.load()||_trackingFinishing.load())return NO;_trackingInFrame.store(true);@try{ if(_trackingCancelled.load())return [self stopToolAnalysis:@"已取消;保留最后连续可信轨迹" error:nil];if(_trackingSingle&&_trackingSamples.count>=2)return [self stopToolAnalysis:nil error:nil];if(_trackingSamples.count>=DYToolKeyLimit)return [self stopToolAnalysis:@"轨迹已到数量上限,保留已完成部分" error:error]; CMTime expected=CMTIME_IS_NUMERIC(_trackingPreviousTime)?(_trackingBackward?CMTimeSubtract(_trackingPreviousTime,_trackingFrameDuration):CMTimeAdd(_trackingPreviousTime,_trackingFrameDuration)):_trackingReferenceTime;if(!CMTIME_IS_NUMERIC(time)||time.epoch!=expected.epoch||CMTimeCompare(time,expected)!=0)return [self stopToolAnalysis:@"宿主帧序列不连续,停在最后可信帧" error:error]; - dy::GrayFrame frame=[self toolGrayFrame:tile error:error];if(!frame.valid()||frame.width!=_trackingReferenceFrame.width||frame.height!=_trackingReferenceFrame.height)return [self stopToolAnalysis:@"宿主未返回完整源画面" error:error]; - if(!_trackingSamples.count){double same=0,flip=0;size_t count=0;for(int y=0;y_undoStarts==writes+1); DYWriteTools(base,doc,kCMTimeZero);capture(2);host->_state=kFxAnalysisState_NotAnalyzing;request(0);callbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];for(int i=2;i<6&&callbacks;i++){setFrame(i);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}[plugin cleanupAnalysis:nil];drain();BOOL backward=host->_backwardStarts==1;callbacks&=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];for(int i=2;i>=0&&callbacks;i--){setFrame(i);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}[plugin cleanupAnalysis:nil];drain();tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];run.record(@"bidirectional-passes-share-reference-and-merge-once",callbacks&&backward&&[tracked[@"keys"]count]==6&&base->_undoStarts==writes+2); DYWriteTools(base,doc,kCMTimeZero);capture(2);host->_state=kFxAnalysisState_NotAnalyzing;request(-2);BOOL range=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&CMTimeCompare(desired.start,timeAt(1))==0&&CMTimeCompare(desired.duration,timeAt(2))==0;[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];for(int i=2;i>=1;i--){setFrame(i);[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}setFrame(0);stopped=![plugin analyzeFrame:frame atTime:timeAt(0) error:nil];drain();tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];run.record(@"backward-single-step-includes-reference-and-one-neighbor",range&&stopped&&[tracked[@"keys"]count]==2); + // A host cache hit can move the playhead without re-entering renderDestinationImage. + // Starting at a middle frame must use analyzer input even on a fresh instance. + DYWriteTools(base,doc,kCMTimeZero);idcold=(id)[[NSClassFromString(@"DYPlugIn")alloc]initWithAPIManager:host];host->_time=timeAt(2);host->_state=kFxAnalysisState_NotAnalyzing;int starts=host->_forwardStarts; + [cold toolTrackRequested:[NSNotification notificationWithName:@"DYToolTrack" object:host userInfo:@{@"node":@"base:hsl",@"mask":mask[@"id"],@"direction":@1}]]; + callbacks=host->_forwardStarts==starts+1&&[cold desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&CMTimeCompare(desired.start,timeAt(2))==0&&[cold setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil]; + for(int i=2;i<6&&callbacks;i++){setFrame(i);callbacks&=[cold analyzeFrame:frame atTime:timeAt(i) error:nil];}[cold cleanupAnalysis:nil];drain();tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];run.record(@"middle-frame-start-with-no-preview-render-cache",callbacks&&[tracked[@"keys"]count]==4&&CMTimeCompare(CMTimeMake([tracked[@"keys"][0][@"value"]longLongValue],[tracked[@"keys"][0][@"scale"]intValue]),timeAt(2))==0); + [cold release]; + DYWriteTools(base,doc,kCMTimeZero);capture(0);host->_time=timeAt(2);request(1);callbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil]; + for(int i=2;i<6&&callbacks;i++){setFrame(i);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}[plugin cleanupAnalysis:nil];drain();tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];run.record(@"middle-frame-start-ignores-stale-preview-cache",callbacks&&[tracked[@"keys"]count]==4); + DYWriteTools(base,doc,kCMTimeZero);capture(0);request(1);callbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil]; + // Deliberately different preview pixels must not veto an authoritative analysis seed. + for(int i=0;i<3&&callbacks;i++){setFrame(i+2);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}[plugin cleanupAnalysis:nil];drain();tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];run.record(@"analysis-seed-does-not-require-preview-pixel-equality",callbacks&&[tracked[@"keys"]count]==3); [plugin release]; } diff --git a/大鱼调色/Plugin/Info.plist b/大鱼调色/Plugin/Info.plist index fcbc8ae..d0a1b07 100644 --- a/大鱼调色/Plugin/Info.plist +++ b/大鱼调色/Plugin/Info.plist @@ -17,9 +17,9 @@ CFBundlePackageType XPC! CFBundleShortVersionString - 1.4.1 + 1.4.2 CFBundleVersion - 26 + 27 NSHumanReadableCopyright PlugInKit @@ -71,7 +71,7 @@ uuid 1C17B73B-F229-4C09-A434-EAF6DFF5F22A version - 1.4.1 + 1.4.2 className diff --git a/大鱼调色/Plugin/MaskCore/DYTracker.hpp b/大鱼调色/Plugin/MaskCore/DYTracker.hpp index f82ef38..275f073 100644 --- a/大鱼调色/Plugin/MaskCore/DYTracker.hpp +++ b/大鱼调色/Plugin/MaskCore/DYTracker.hpp @@ -18,6 +18,7 @@ constexpr size_t TrackerFeatureLimit = 300; constexpr int TrackerPyramidLevels = 4; constexpr int TrackerPatchRadius = 10; constexpr int TrackerIterations = 30; +constexpr float TrackerFitTolerance = 3.f; struct GrayFrame { int width = 0, height = 0; @@ -50,6 +51,11 @@ struct TrackerROI { float cx = .5f, cy = .5f, width = .25f, height = .25f, angle = 0; }; +enum class TrackFailure { + None, InvalidFrame, InsufficientTexture, Backend, InsufficientMatches, + IncoherentMotion, LowConfidence, InvalidTransform, OutsideFrame +}; + struct TrackResult { float cx = .5f, cy = .5f; float scale = 1.f, rotation = 0.f; @@ -58,6 +64,9 @@ struct TrackResult { bool valid = false; // Analysis-only additions; the legacy on-disk DYTrackKey stays unchanged. float scaleX = 1.f, scaleY = 1.f; + int matches = 0; + float medianError = 0; + TrackFailure failure = TrackFailure::None; }; struct FlowPoint { float x,y; }; @@ -307,15 +316,16 @@ class Tracker { std::vector ids;double e=0; for (size_t k=0;k(k));e+=d*d;} + if (d(k));e+=d*d;} } if (ids.size()>best.size() || (ids.size()==best.size() && e(i)); + if (distance(model.map(matches[i].p),matches[i].q)(i)); if (inliers.size()<8) return false; best=inliers; } @@ -341,23 +351,24 @@ public: result_={roi.cx,roi.cy,1,0,0,0,0,false}; if (!frame.valid() || !std::isfinite(roi.cx) || !std::isfinite(roi.cy) || !std::isfinite(roi.width) || !std::isfinite(roi.height) || !std::isfinite(roi.angle) || - roi.width<=0 || roi.height<=0 || roi.width>8 || roi.height>8 || roi.cx<0 || roi.cx>1 || roi.cy<0 || roi.cy>1) return false; + roi.width<=0 || roi.height<=0 || roi.width>8 || roi.height>8 || roi.cx<0 || roi.cx>1 || roi.cy<0 || roi.cy>1) {result_.failure=TrackFailure::InvalidFrame;return false;} width_=frame.width;height_=frame.height;initialROI_=roi; center_={roi.cx*width_,roi.cy*height_}; points_=detect(frame); result_.features=static_cast(points_.size());result_.inliers=result_.features; - if (points_.size()<12) return false; - if(backend_){if(!backend_->reset(frame))return false;}else previous_=makePyramid(frame);active_=true;result_.valid=true;result_.confidence=1; + if (points_.size()<12) {result_.failure=TrackFailure::InsufficientTexture;return false;} + if(backend_){if(!backend_->reset(frame)){result_.failure=TrackFailure::Backend;return false;}}else previous_=makePyramid(frame);active_=true;result_.valid=true;result_.confidence=1; return true; } TrackResult track(const GrayFrame& frame) { if (!active_ || !frame.valid() || frame.width!=width_ || frame.height!=height_) { - result_.valid=false;result_.confidence=0;active_=false;return result_; + result_.valid=false;result_.confidence=0;result_.failure=TrackFailure::InvalidFrame;active_=false;return result_; } + result_.failure=TrackFailure::None;result_.matches=0;result_.inliers=0;result_.medianError=0; Pyramid next;if(!backend_)next=makePyramid(frame); std::vector matches;matches.reserve(points_.size()); if(backend_) { - if(!backend_->advance(frame,points_,matches)){active_=false;result_.valid=false;result_.confidence=0;return result_;} + if(!backend_->advance(frame,points_,matches)){active_=false;result_.valid=false;result_.confidence=0;result_.failure=TrackFailure::Backend;return result_;} } else for (P p:points_) { P q,back;float error=0,backError=0; if (!flow(previous_,next,p,p,q,error)) continue; @@ -366,18 +377,22 @@ public: } Affine model;std::vector inliers;float medianError=0; result_.features=static_cast(points_.size()); - result_.inliers=0; + result_.matches=static_cast(matches.size()); if (!estimate(matches,model,inliers,medianError)) { - active_=false;result_.valid=false;result_.confidence=0;return result_; + active_=false;result_.valid=false;result_.confidence=0;result_.inliers=static_cast(inliers.size());result_.failure=matches.size()<8?TrackFailure::InsufficientMatches:TrackFailure::IncoherentMotion;return result_; } double stepScale=std::sqrt(model.determinant()); double stepRotation=std::atan2(model.c-model.b,model.a+model.d); P nextCenter=model.map(center_); float retained=static_cast(inliers.size())/points_.size(); - float confidence=std::clamp(retained*std::exp(-medianError*.8f),0.f,1.f); + // Residual quality is relative to the accepted fit budget. Keeping the + // old 1.5px penalty with a 3px fit incorrectly rejects coherent detail + // motion before the geometric/consensus guards can make that decision. + float confidence=std::clamp(retained*std::exp(-medianError*(1.2f/TrackerFitTolerance)),0.f,1.f); + result_.inliers=static_cast(inliers.size());result_.medianError=medianError; if (stepScale<.9 || stepScale>1.12 || std::abs(stepRotation)>.12 || confidence<.3f || !inside(frame,nextCenter,3) || result_.scale*stepScale<.1 || result_.scale*stepScale>10) { - active_=false;result_.valid=false;result_.confidence=confidence;return result_; + active_=false;result_.valid=false;result_.confidence=confidence;result_.failure=confidence<.3f?TrackFailure::LowConfidence:!inside(frame,nextCenter,3)?TrackFailure::OutsideFrame:TrackFailure::InvalidTransform;return result_; } auto nextCumulative=Affine::compose(model,cumulative_); // QR decomposition in the initial shape's axes. Output matches the @@ -385,7 +400,7 @@ public: double cs=std::cos(initialROI_.angle),sn=std::sin(initialROI_.angle); double ax=nextCumulative.a*cs+nextCumulative.b*sn,ay=nextCumulative.c*cs+nextCumulative.d*sn; double sx=std::hypot(ax,ay),sy=nextCumulative.determinant()/sx; - if(!std::isfinite(sx)||!std::isfinite(sy)||sx<.1||sx>10||sy<.1||sy>10){active_=false;result_.valid=false;result_.confidence=0;return result_;} + if(!std::isfinite(sx)||!std::isfinite(sy)||sx<.1||sx>10||sy<.1||sy>10){active_=false;result_.valid=false;result_.confidence=0;result_.failure=TrackFailure::InvalidTransform;return result_;} cumulative_=nextCumulative; center_=nextCenter; result_.cx=center_.x/width_;result_.cy=center_.y/height_; diff --git a/大鱼调色/Wrapper Application/Info.plist b/大鱼调色/Wrapper Application/Info.plist index 654c3a4..d67778e 100644 --- a/大鱼调色/Wrapper Application/Info.plist +++ b/大鱼调色/Wrapper Application/Info.plist @@ -17,9 +17,9 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.4.1 + 1.4.2 CFBundleVersion - 26 + 27 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) NSHumanReadableCopyright