Stabilize clone stamp tracks and sample fractional motion continuously

这个提交包含在:
mac 于 2026-11-06 01:30:28 +08:00 提交
1 父节点 89eeaa9ba1
当前提交 7b6a3106ca
7 个文件已更改
+100 -15

没有与搜索条件相匹配的文件

+38 -1
查看文件
@@ -251,6 +251,42 @@ NSDictionary *DYStampKey(CMTime time,DYMaskParameters p,BOOL manual,float confid
@(motion.sourceScaleY),@(motion.targetScaleX),@(motion.targetScaleY)];
return paired;
}
// Analysis supplies a contiguous pass in either time direction. A symmetric
// kernel has no phase lag, and shrinking it at the ends keeps the seed and
// the last trusted frame fixed. Work in log scale so zoom in/out is symmetric.
static NSArray *smoothStampTrackingSamples(NSArray *samples){
if(samples.count<3)return samples;
for(NSDictionary *key in samples)
if(!key[@"stampMotion"]||[key[@"manual"]boolValue])return samples;
NSMutableArray *result=[NSMutableArray arrayWithCapacity:samples.count];
[result addObject:samples.firstObject];
for(NSUInteger i=1;i+1<samples.count;i++){
NSUInteger radius=std::min<NSUInteger>(2,std::min(i,samples.count-1-i));
const double weights[]={1,4,6,4,1};
const double endWeights[]={1,2,1};
double values[8]={};
for(NSUInteger j=0;j<=2*radius;j++){
NSDictionary *neighbor=samples[i-radius+j];
DYMaskParameters p;
if(!readParameters(neighbor[@"parameters"],&p))return samples;
auto m=stampMotionFromKey(neighbor);
double weight=radius==2?weights[j]/16:endWeights[j]/4;
const double components[]={p.centerX,p.centerY,m.sourceOffsetX,m.sourceOffsetY,
std::log(m.sourceScaleX),std::log(m.sourceScaleY),std::log(m.targetScaleX),std::log(m.targetScaleY)};
for(int k=0;k<8;k++)values[k]+=weight*components[k];
}
NSDictionary *original=samples[i];
DYMaskParameters p;
if(!readParameters(original[@"parameters"],&p))return samples;
p.centerX=values[0];p.centerY=values[1];
auto scale=[&](int index){return float(std::clamp(std::exp(values[index]),1./16,16.));};
DYStampMotion m={float(values[2]),float(values[3]),scale(4),scale(5),scale(6),scale(7),true};
[result addObject:DYStampKey(keyTime(original),p,NO,[original[@"confidence"]floatValue],m)];
}
[result addObject:samples.lastObject];
return result;
}
BOOL DYMergeTracking(NSMutableDictionary *m,NSArray *samples,CMTime frameDuration){
if(!samples.count)return YES;
NSMutableArray *all=[NSMutableArray array];
@@ -258,6 +294,7 @@ BOOL DYMergeTracking(NSMutableDictionary *m,NSArray *samples,CMTime frameDuratio
CMTime lo=keyTime(samples.firstObject),hi=keyTime(samples.lastObject);
if(CMTimeCompare(lo,hi)>0)std::swap(lo,hi);
if(m[@"stampSource"]&&CMTIME_IS_NUMERIC(frameDuration)&&frameDuration.value>0){
samples=smoothStampTrackingSamples(samples);
// FCP can shift analysis timestamps by a fraction of a frame between passes.
// Replace an older automatic key belonging to the first or last new frame.
CMTime half=CMTimeMultiplyByRatio(frameDuration,1,2);
@@ -300,7 +337,7 @@ NSDictionary *DYToolRenderSnapshot(NSDictionary *doc,CMTime t){
NSMutableDictionary *m=[[mask mutableCopy]autorelease];m[@"parameters"]=parameterData(DYMaskAtTime(mask,t));m[@"keys"]=@[];
if(mask[@"stampSource"]){
m[@"stampSource"]=DYStampSourceAtTime(mask,t);
m[@"stampSamplingScale"]=DYStampSamplingScaleAtTime(mask,t);
m[@"stampSamplingScale"]=DYStampSamplingScaleAtTime(mask,t)?:@[@1,@1];
}
[result addObject:m];
}