在webgl中使用fft写入隐形水印

zhangtgv
  • 184

我想使用webgl对视频内容添加实时的fft隐形水印,我在网上找到了如下的shader代码:

<script type="x-shader/x-fragment" id='shader-fs'>
    #define HORIZONTAL
    precision mediump float;

    varying highp vec2 vTextureCoord;

    uniform sampler2D uSampler;
    uniform sampler2D uWatermark;

    const float PI = 3.14159265359;
    const float subtransformSize = 8.0;
    const float transformSize = 256.0;

    vec2 multiplyComplex(vec2 a, vec2 b){
        return vec2(a[0]*b[0] - a[1]*b[1], a[1]*b[0] + a[0]*b[1]);
    }

    vec4 fft(){
        #ifdef HORIZONTAL
            float index = vTextureCoord.x * transformSize - 0.5;
        #else
            float index = vTextureCoord.y * transformSize - 0.5;
        #endif

        float evenIndex = floor(index / subtransformSize) * (subtransformSize * 0.5) + mod(index, subtransformSize * 0.5);

        #ifdef HORIZONTAL
            vec4 even = texture2D(uSampler, vec2(evenIndex+0.5, vTextureCoord.y) / transformSize).rgba;
            vec4 odd = texture2D(uSampler, vec2(evenIndex+transformSize*0.5+0.5, vTextureCoord.y) / transformSize).rgba;
        #else
            vec4 even = texture2D(uSampler, vec2(vTextureCoord.x, evenIndex+0.5) / transformSize).rgba;
            vec4 odd = texture2D(uSampler, vec2(vTextureCoord.x, evenIndex+transformSize*0.5+0.5) / transformSize).rgba;
        #endif

        float twiddleArgument1D = -2.0 * PI * (index / subtransformSize);
        vec2 twiddle1D = vec2(cos(twiddleArgument1D), sin(twiddleArgument1D));

        vec2 outputA = even.xy + multiplyComplex(twiddle1D, odd.xy);
        vec2 outputB = even.zw + multiplyComplex(twiddle1D, odd.zw);

        return vec4(outputA, outputB);
    }

    vec4 ifft(){
        #ifdef HORIZONTAL
            float index = vTextureCoord.x * transformSize - 0.5;
        #else
            float index = vTextureCoord.y * transformSize - 0.5;
        #endif

        float evenIndex = floor(index / subtransformSize) * (subtransformSize * 0.5) + mod(index, subtransformSize * 0.5);

        #ifdef HORIZONTAL
            vec4 even = texture2D(uSampler, vec2(evenIndex+0.5, gl_FragCoord.y) / transformSize).rgba;
            vec4 odd = texture2D(uSampler, vec2(evenIndex+transformSize*0.5+0.5, gl_FragCoord.y) / transformSize).rgba;
        #else
            vec4 even = texture2D(uSampler, vec2(gl_FragCoord.x, evenIndex+0.5) / transformSize).rgba;
            vec4 odd = texture2D(uSampler, vec2(gl_FragCoord.x, evenIndex+transformSize*0.5+0.5) / transformSize).rgba;
        #endif

        float twiddleArgument1D = 2.0 * PI * (index / subtransformSize);
        vec2 twiddle1D = vec2(cos(twiddleArgument1D), sin(twiddleArgument1D));

        vec2 outputA = even.xy + multiplyComplex(twiddle1D, odd.xy);
        vec2 outputB = even.zw + multiplyComplex(twiddle1D, odd.zw);

        return vec4(outputA, outputB);
    }

    void main(){
        gl_FragColor = fft();
        // gl_FragColor = texture2D(uSampler, vTextureCoord);
    }

现有如下疑问:
1.上述代码中的fft方法,是否仅仅只是做了傅里叶变换而并没有做逆变换
2.如果上述代码中的fft方法没有做逆变换的话,是否需要手动执行逆变换
3.如果按照上述代码中的ifft方法执行逆变换的话,他的输入其实也是一个sampler,但是我的输入应该只有一个点的数据,这个逆变换是否可以实现,如果可以实现的话能否提供一个思路

恳请各位大佬提供意见,感谢

回复
阅读 437
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏