Add Android.bp to fft2d to compile correctly. Update to most recent
release.

Bug: 155825630
Test: unit tests in follow up CLs.
Change-Id: I7d152a5b444dad32dac3c43ee4b3dc1646206090
diff --git a/Android.bp b/Android.bp
new file mode 100644
index 0000000..c9fc014
--- /dev/null
+++ b/Android.bp
@@ -0,0 +1,37 @@
+// Copyright 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+cc_library_static {
+    name: "libfft2d",
+    sdk_version: "current",
+    min_sdk_version: "30",
+    srcs: [
+        "src/fft2d/fft2d/fftsg.c",
+        "src/fft2d/fft2d/fftsg2d.c",
+        "src/fft2d/fft2d/alloc.c",
+	"src/fft2d/fft2d/shrtdct.c"
+    ],
+    cflags: [
+        "-Wno-unused-parameter",
+	"-Wno-unused-function",
+    ],
+    export_include_dirs: ["src/fft2d"],
+    sanitize: {
+      integer_overflow: true,
+    },
+    apex_available: [
+        "//apex_available:platform",
+        "com.android.extservices",
+    ],
+}
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..2bd8550
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,3 @@
+Copyright(C) 1997,2001 Takuya OOURA (email: ooura@kurims.kyoto-u.ac.jp).
+You may use, copy, modify this code for any purpose and 
+without fee. You may distribute this ORIGINAL package.
diff --git a/METADATA b/METADATA
new file mode 100644
index 0000000..3f90982
--- /dev/null
+++ b/METADATA
@@ -0,0 +1,21 @@
+name: "FFT"
+description:
+  "This directory contains the third-party one- and two-dimensional "
+  "fast Fourier transform and related sinusoidal transform code "
+  "written by Takuya Ooura."
+
+third_party {
+  url {
+    type: HOMEPAGE
+    value: "http://www.kurims.kyoto-u.ac.jp/~ooura/fft.html"
+  }
+  url {
+    type: ARCHIVE
+    value: "http://www.kurims.kyoto-u.ac.jp/~ooura/fft2d.tgz"
+  }
+  version: "2006/12/28"
+  last_upgrade_date { year: 2021 month: 2 day: 8 }
+
+  # explicitly approved by opensource-licensing@ on Feb. 12 2021.
+  license_type: BY_EXCEPTION_ONLY
+}
diff --git a/OWNERS b/OWNERS
new file mode 100644
index 0000000..a687bc6
--- /dev/null
+++ b/OWNERS
@@ -0,0 +1,2 @@
+wanggang@google.com
+chaviw@google.com
diff --git a/src/fft2d/fft2d/fftsg.f b/src/fft2d/fft2d/fftsg.f
index c676066..6fb26c4 100644
--- a/src/fft2d/fft2d/fftsg.f
+++ b/src/fft2d/fft2d/fftsg.f
@@ -2022,7 +2022,7 @@
 !
       integer function cfttree(n, j, k, a, nw, w)
       integer n, j, k, nw, i, isplt, m
-      real*8 a(0 : n - 1), w(0 : nw - 1)
+      real*8 a(0 : j - 1), w(0 : nw - 1)
       if (mod(k, 4) .ne. 0) then
           isplt = mod(k, 2)
           if (isplt .ne. 0) then
diff --git a/src/fft2d/fft2d/fftsg3d.c b/src/fft2d/fft2d/fftsg3d.c
index 223c0e2..62f7daa 100644
--- a/src/fft2d/fft2d/fftsg3d.c
+++ b/src/fft2d/fft2d/fftsg3d.c
@@ -152,10 +152,10 @@
                                      = R[(n1-k1)%n1][n2-k2][0], 
                         a[k1][k2][1] = I[k1][k2][0]
                                      = -I[(n1-k1)%n1][n2-k2][0], 
-                        a[k1][n2-k2][1] = R[k1][k2][n3/2]
-                                        = R[(n1-k1)%n1][n2-k2][n3/2], 
-                        a[k1][n2-k2][0] = -I[k1][k2][n3/2]
-                                        = I[(n1-k1)%n1][n2-k2][n3/2], 
+                        a[k1][n2-k2][1] = R[(n1-k1)%n1][k2][n3/2]
+                                        = R[k1][n2-k2][n3/2], 
+                        a[k1][n2-k2][0] = -I[(n1-k1)%n1][k2][n3/2]
+                                        = I[k1][n2-k2][n3/2], 
                             0<=k1<n1, 0<k2<n2/2, 
                         a[k1][0][0] = R[k1][0][0]
                                     = R[n1-k1][0][0], 
@@ -193,10 +193,10 @@
                                      = R[(n1-j1)%n1][n2-j2][0], 
                         a[j1][j2][1] = I[j1][j2][0]
                                      = -I[(n1-j1)%n1][n2-j2][0], 
-                        a[j1][n2-j2][1] = R[j1][j2][n3/2]
-                                        = R[(n1-j1)%n1][n2-j2][n3/2], 
-                        a[j1][n2-j2][0] = -I[j1][j2][n3/2]
-                                        = I[(n1-j1)%n1][n2-j2][n3/2], 
+                        a[j1][n2-j2][1] = R[(n1-j1)%n1][j2][n3/2]
+                                        = R[j1][n2-j2][n3/2], 
+                        a[j1][n2-j2][0] = -I[(n1-j1)%n1][j2][n3/2]
+                                        = I[j1][n2-j2][n3/2], 
                             0<=j1<n1, 0<j2<n2/2, 
                         a[j1][0][0] = R[j1][0][0]
                                     = R[n1-j1][0][0], 
diff --git a/src/fft2d/fft2d/fftsg3d.f b/src/fft2d/fft2d/fftsg3d.f
index 350bbef..f08b1dc 100644
--- a/src/fft2d/fft2d/fftsg3d.f
+++ b/src/fft2d/fft2d/fftsg3d.f
@@ -135,10 +135,10 @@
 !                                    = R(0,n2-k2,mod(n3-k3,n3)), 
 !                         a(1,k2,k3) = I(0,k2,k3)
 !                                    = -I(0,n2-k2,mod(n3-k3,n3)), 
-!                         a(1,n2-k2,k3) = R(n1/2,k2,k3)
-!                                       = R(n1/2,n2-k2,mod(n3-k3,n3)), 
-!                         a(0,n2-k2,k3) = -I(n1/2,k2,k3)
-!                                       = I(n1/2,n2-k2,mod(n3-k3,n3)), 
+!                         a(1,n2-k2,k3) = R(n1/2,k2,mod(n3-k3,n3))
+!                                       = R(n1/2,n2-k2,k3), 
+!                         a(0,n2-k2,k3) = -I(n1/2,k2,mod(n3-k3,n3))
+!                                       = I(n1/2,n2-k2,k3), 
 !                             0<k2<n2/2, 0<=k3<n3, 
 !                         a(0,0,k3) = R(0,0,k3)
 !                                   = R(0,0,n3-k3), 
@@ -176,10 +176,10 @@
 !                                    = R(0,n2-j2,mod(n3-j3,n3)), 
 !                         a(1,j2,j3) = I(0,j2,j3)
 !                                    = -I(0,n2-j2,mod(n3-j3,n3)), 
-!                         a(1,n2-j2,j3) = R(n1/2,j2,j3)
-!                                       = R(n1/2,n2-j2,mod(n3-j3,n3)), 
-!                         a(0,n2-j2,j3) = -I(n1/2,j2,j3)
-!                                       = I(n1/2,n2-j2,mod(n3-j3,n3)), 
+!                         a(1,n2-j2,j3) = R(n1/2,j2,mod(n3-j3,n3))
+!                                       = R(n1/2,n2-j2,j3), 
+!                         a(0,n2-j2,j3) = -I(n1/2,j2,mod(n3-j3,n3))
+!                                       = I(n1/2,n2-j2,j3), 
 !                             0<j2<n2/2, 0<=j3<n3, 
 !                         a(0,0,j3) = R(0,0,j3)
 !                                   = R(0,0,n3-j3), 
diff --git a/src/fft2d/fft2d/readme2d.txt b/src/fft2d/fft2d/readme2d.txt
index 5a48e1d..7cc4dfa 100644
--- a/src/fft2d/fft2d/readme2d.txt
+++ b/src/fft2d/fft2d/readme2d.txt
@@ -69,3 +69,9 @@
     You may use, copy, modify this code for any purpose and 
     without fee. You may distribute this ORIGINAL package.
 
+History
+    ...
+    Nov. 2001  : Add 3D-FFT routines
+    Dec. 2006  : Fix a documentation bug in "fftsg3d.*"
+    Dec. 2006  : Fix a minor bug in "fftsg.f"
+