diff mbox series

[FFmpeg-devel,02/13] avcodec/elbg: Move avpriv_init_elbg() down

Message ID AM7PR03MB666060B3613A8BFB376367B58FDD9@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit d046e76515d8f30e2960e4888478002852bf0209
Headers show
Series [FFmpeg-devel,01/13] avcodec/elbg: Remove avoidable buffer | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Sept. 17, 2021, 2:07 a.m. UTC
It will avoid a forward declaration later.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/elbg.c | 71 +++++++++++++++++++++++------------------------
 1 file changed, 35 insertions(+), 36 deletions(-)

Comments

Paul B Mahol Sept. 17, 2021, 6:50 a.m. UTC | #1
lgtm
diff mbox series

Patch

diff --git a/libavcodec/elbg.c b/libavcodec/elbg.c
index 795fc83f16..3ca67b400c 100644
--- a/libavcodec/elbg.c
+++ b/libavcodec/elbg.c
@@ -332,42 +332,6 @@  static void do_shiftings(elbg_data *elbg)
         }
 }
 
-#define BIG_PRIME 433494437LL
-
-int avpriv_init_elbg(int *points, int dim, int numpoints, int *codebook,
-                 int numCB, int max_steps, int *closest_cb,
-                 AVLFG *rand_state)
-{
-    int i, k, ret = 0;
-
-    if (numpoints > 24*numCB) {
-        /* ELBG is very costly for a big number of points. So if we have a lot
-           of them, get a good initial codebook to save on iterations       */
-        int *temp_points = av_malloc_array(dim, (numpoints/8)*sizeof(int));
-        if (!temp_points)
-            return AVERROR(ENOMEM);
-        for (i=0; i<numpoints/8; i++) {
-            k = (i*BIG_PRIME) % numpoints;
-            memcpy(temp_points + i*dim, points + k*dim, dim*sizeof(int));
-        }
-
-        ret = avpriv_init_elbg(temp_points, dim, numpoints / 8, codebook,
-                               numCB, 2 * max_steps, closest_cb, rand_state);
-        if (ret < 0) {
-            av_freep(&temp_points);
-            return ret;
-        }
-        ret = avpriv_do_elbg(temp_points, dim, numpoints / 8, codebook,
-                             numCB, 2 * max_steps, closest_cb, rand_state);
-        av_free(temp_points);
-
-    } else  // If not, initialize the codebook with random positions
-        for (i=0; i < numCB; i++)
-            memcpy(codebook + i*dim, points + ((i*BIG_PRIME)%numpoints)*dim,
-                   dim*sizeof(int));
-    return ret;
-}
-
 int avpriv_do_elbg(int *points, int dim, int numpoints, int *codebook,
                 int numCB, int max_steps, int *closest_cb,
                 AVLFG *rand_state)
@@ -459,3 +423,38 @@  out:
     av_free(elbg->scratchbuf);
     return ret;
 }
+
+#define BIG_PRIME 433494437LL
+
+int avpriv_init_elbg(int *points, int dim, int numpoints, int *codebook,
+                     int num_cb, int max_steps, int *closest_cb,
+                     AVLFG *rand_state)
+{
+    int ret = 0;
+
+    if (numpoints > 24LL * num_cb) {
+        /* ELBG is very costly for a big number of points. So if we have a lot
+           of them, get a good initial codebook to save on iterations       */
+        int *temp_points = av_malloc_array(dim, (numpoints/8)*sizeof(*temp_points));
+        if (!temp_points)
+            return AVERROR(ENOMEM);
+        for (int i = 0; i < numpoints / 8; i++) {
+            int k = (i*BIG_PRIME) % numpoints;
+            memcpy(temp_points + i*dim, points + k*dim, dim * sizeof(*temp_points));
+        }
+
+        ret = avpriv_init_elbg(temp_points, dim, numpoints / 8, codebook,
+                               num_cb, 2 * max_steps, closest_cb, rand_state);
+        if (ret < 0) {
+            av_freep(&temp_points);
+            return ret;
+        }
+        ret = avpriv_do_elbg(temp_points, dim, numpoints / 8, codebook,
+                             num_cb, 2 * max_steps, closest_cb, rand_state);
+        av_free(temp_points);
+    } else  // If not, initialize the codebook with random positions
+        for (int i = 0; i < num_cb; i++)
+            memcpy(codebook + i * dim, points + ((i*BIG_PRIME)%numpoints)*dim,
+                   dim * sizeof(*codebook));
+    return ret;
+}