Skip to main content
Participant
January 1, 2025
Question

How to judge incoming and outgoing exist in premiere pro sdk?metal code error

  • January 1, 2025
  • 1 reply
  • 151 views

sir,i use sdk to develop a plugin in premiere pro and i use sdk_crossdissolve in the example project.Now i found a problem,because my transitions only add to start or end to make a clip animate,but i don't know how to judge if the inimg or outimg exists,so i deal with outimg first and then if 

if(pixel.w) i use outimg or use outimg,but i found in metal,it will always have some problems in image,second mission will break。if i deal with outimg first,inimg will break.I do a lot of try,but no result,so sad,can you help me where i do wrong.here is my metal code:

 

void Vignette_delegate(
    device float4* const outImg,
    device float4* const inImg,
    device float4* const destImg,
    int outPitch,
    int inPitch,
    int destPitch,
    int is16f,
    int width,
    int height,
    float progress,
    int zoom,
    int alpha,
    int flip,
    float xValue,
    float yValue,
    int pan,
    uint2 inXY
    ) {
    // 根据 hasInImg 判断 sourceImg 和 progress
//    device float4* sourceImg;
//    int sourcePitch;
    float adjustedProgress;
    // 如果 flip == 1,则对 progress 应用 easeInOut 函数
    if (flip == 1) {
        progress = easeInOut(progress);
    }
        adjustedProgress = 1.0f - progress;

    if (inXY.x >= (uint)width || inXY.y >= (uint)height) {
        return;
    }

    // 根据缩放模式计算缩放和偏移
    float scale = 1.0f;
    float offsetX = 0.0f;
    float offsetY = 0.0f;

    if (zoom == 0) {
        scale = 1.0f / adjustedProgress;
        offsetX = (width * (1.0f - adjustedProgress)) * scale;
        offsetY = (height * (1.0f - adjustedProgress)) * scale;
    } else if (zoom == 1) {
        scale = adjustedProgress;
        offsetX = (width * (1.0f - adjustedProgress));
        offsetY = (height * (1.0f - adjustedProgress));
    } else if (zoom == 2) {
        offsetX = (width * (1.0f - adjustedProgress));
        offsetY = (height * (1.0f - adjustedProgress));
    }

    // 计算平移偏移
    float panOffsetX = 0.0f;
    float panOffsetY = 0.0f;

    if (pan == 0) {
        panOffsetY = -offsetY;
    } else if (pan == 1) {
        panOffsetY = offsetY;
    } else if (pan == 2) {
        panOffsetX = -offsetX;
    } else if (pan == 3) {
        panOffsetX = offsetX;
    }

    // 中心点坐标
    float centerX = xValue;
    float centerY = yValue;

    // 将输出坐标映射到输入坐标
    float inputX = (inXY.x - centerX) * scale + centerX + panOffsetX;
    float inputY = (inXY.y - centerY) * scale + centerY + panOffsetY;

//     检查映射坐标是否越界
//    if (inputX < 0.0f || inputX >= width || inputY < 0.0f || inputY >= height) {
//        uint destIndex = (inXY.y * destPitch) + inXY.x;
//        if (is16f) {
//            ((device half4*)destImg)[destIndex] = half4(0.0h, 0.0h, 0.0h, 0.0h);
//        } else {
//            destImg[destIndex] = float4(0.0f, 0.0f, 0.0f, 0.0f);
//        }
//        return;
//    }

    // 获取输入像素坐标
    uint x = (uint)inputX;
    uint y = (uint)inputY;
    uint inIndex = (y * outPitch) + x;
//    uint destIndex = (inXY.y * destPitch) + inXY.x;

//    // 读取源像素
    float4 pixel;
    if (is16f) {
        half4 half_pixel = ((device half4*)outImg)[inIndex];
        pixel = float4((float)half_pixel.x, (float)half_pixel.y, (float)half_pixel.z, (float)half_pixel.w);
    } else {
        pixel = outImg[inIndex];
    }

    if(pixel.w){
        // 调整 alpha 通道
        if (alpha == 1) {
            pixel.w *= adjustedProgress;
        }
        // 写入目标图像
     if (is16f) {
           half4 write_value = half4((half)pixel.x, (half)pixel.y, (half)pixel.z, (half)pixel.w);
           ((device half4*)destImg)[destIndex] = write_value;
       } else {
           destImg[destIndex] = pixel;
       }

    }
    else{

        float adjustedProgress = progress; 
        
        // 根据缩放模式计算缩放和偏移
        float scale = 1.0f;
        float offsetX = 0.0f;
        float offsetY = 0.0f;

        if (zoom == 0) {
            scale = 1.0f / adjustedProgress;
            offsetX = (width * (1.0f - adjustedProgress)) * scale;
            offsetY = (height * (1.0f - adjustedProgress)) * scale;
        } else if (zoom == 1) {
            scale = adjustedProgress;
            offsetX = (width * (1.0f - adjustedProgress));
            offsetY = (height * (1.0f - adjustedProgress));
        } else if (zoom == 2) {
            offsetX = (width * (1.0f - adjustedProgress));
            offsetY = (height * (1.0f - adjustedProgress));
        }
        
        float panOffsetY = 0.0f;
        float panOffsetX = 0.0f;

        if (pan == 0) {
            panOffsetY = -offsetY;
        } else if (pan == 1) {
            panOffsetY = offsetY;
        } else if (pan == 2) {
            panOffsetX = -offsetX;
        } else if (pan == 3) {
            panOffsetX = offsetX;
        }
        
        
        float centerX = xValue;
        float centerY = yValue;

        // 将输出坐标映射到输入坐标
        inputX = (inXY.x - centerX) * scale + centerX + panOffsetX;
        inputY = (inXY.y - centerY) * scale + centerY + panOffsetY;

        uint x = (uint)inputX;
        uint y = (uint)inputY;
        uint outIndex = (y * inPitch) + x;
        uint destIndex = (inXY.y * destPitch) + inXY.x;

        float4 pixel2;
        if (is16f) {
            half4 half_pixel2 = ((device half4*)inImg)[outIndex];
            pixel2 = float4((float)half_pixel2.x, (float)half_pixel2.y, (float)half_pixel2.z, (float)half_pixel2.w);
        } else {
            pixel2 = inImg[outIndex];
        }
    
        if (alpha) {
            pixel2.w *= adjustedProgress;
        }
        
        // 写入目标图像
        if (is16f) {
            half4 write_value = half4((half)pixel2.x, (half)pixel2.y, (half)pixel2.z, (half)pixel2.w);
            ((device half4*)destImg)[destIndex] = write_value;
        } else {
            destImg[destIndex] = pixel2;
        }
    }
        if (inputX < 0.0f || inputX >= width || inputY < 0.0f || inputY >= height) {
            uint destIndex = (inXY.y * destPitch) + inXY.x;
            if (is16f) {
                ((device half4*)destImg)[destIndex] = half4(0.0h, 0.0h, 0.0h, 0.0h);
            } else {
                destImg[destIndex] = float4(0.0f, 0.0f, 0.0f, 0.0f);
            }
            return;
        }
}

 

1 reply

Kevin-Monahan
Community Manager
Community Manager
January 7, 2025

Hi @干38613184avdf,

Thanks for the post and welcome to the forum. Maybe @Justin Taylor-Hyper Brew can respond. I hope we can get an answer to your question shortly. Sorry for the hassle.

 

Thanks,
Kevin

 

 

Kevin Monahan - Sr. Community &amp; Engagement Strategist – Pro Video and Audio