diff mbox series

[FFmpeg-devel,03/13] avcodec/elbg: Merge avpriv_init_elbg() into avpriv_do_elbg()

Message ID AM7PR03MB6660296E8232B68CF8D507798FDD9@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit 05ccfcb7b0ea326a112b1f769588c07bf0e467be
Headers show
Series [FFmpeg-devel,01/13] avcodec/elbg: Remove avoidable buffer
Related show

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
These functions are always called directly after another with
the exact same arguments. This avoids exporting a symbol;
it also avoids having to perform two calls for every caller.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/a64multienc.c |  4 ----
 libavcodec/cinepakenc.c  |  1 -
 libavcodec/elbg.c        | 29 +++++++++++++++++++++++++----
 libavcodec/elbg.h        | 12 ------------
 libavcodec/msvideo1enc.c |  3 ---
 libavcodec/roqvideoenc.c |  4 ----
 libavfilter/vf_elbg.c    |  3 ---
 7 files changed, 25 insertions(+), 31 deletions(-)

Comments

Paul B Mahol Sept. 18, 2021, 10:24 a.m. UTC | #1
lgtm
diff mbox series

Patch

diff --git a/libavcodec/a64multienc.c b/libavcodec/a64multienc.c
index e2dd85b756..9ee0b07463 100644
--- a/libavcodec/a64multienc.c
+++ b/libavcodec/a64multienc.c
@@ -333,10 +333,6 @@  static int a64multi_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
             buf = pkt->data;
 
             /* calc optimal new charset + charmaps */
-            ret = avpriv_init_elbg(meta, 32, 1000 * c->mc_lifetime, best_cb,
-                               CHARSET_CHARS, 50, charmap, &c->randctx);
-            if (ret < 0)
-                return ret;
             ret = avpriv_do_elbg(meta, 32, 1000 * c->mc_lifetime, best_cb,
                              CHARSET_CHARS, 50, charmap, &c->randctx);
             if (ret < 0)
diff --git a/libavcodec/cinepakenc.c b/libavcodec/cinepakenc.c
index 41da231dfb..8e8b73ce1d 100644
--- a/libavcodec/cinepakenc.c
+++ b/libavcodec/cinepakenc.c
@@ -761,7 +761,6 @@  static int quantize(CinepakEncContext *s, int h, uint8_t *data[4],
     if (i < size)
         size = i;
 
-    avpriv_init_elbg(s->codebook_input, entry_size, i, codebook, size, 1, s->codebook_closest, &s->randctx);
     avpriv_do_elbg(s->codebook_input, entry_size, i, codebook, size, 1, s->codebook_closest, &s->randctx);
 
     // set up vq_data, which contains a single MB
diff --git a/libavcodec/elbg.c b/libavcodec/elbg.c
index 3ca67b400c..b563254bbc 100644
--- a/libavcodec/elbg.c
+++ b/libavcodec/elbg.c
@@ -332,7 +332,7 @@  static void do_shiftings(elbg_data *elbg)
         }
 }
 
-int avpriv_do_elbg(int *points, int dim, int numpoints, int *codebook,
+static int do_elbg(int *points, int dim, int numpoints, int *codebook,
                 int numCB, int max_steps, int *closest_cb,
                 AVLFG *rand_state)
 {
@@ -426,7 +426,14 @@  out:
 
 #define BIG_PRIME 433494437LL
 
-int avpriv_init_elbg(int *points, int dim, int numpoints, int *codebook,
+/**
+ * Initialize the codebook vector for the elbg algorithm.
+ * If numpoints < 8*numCB this function fills codebook with random numbers.
+ * If not, it calls do_elbg for a (smaller) random sample of the points in
+ * points.
+ * @return < 0 in case of error, 0 otherwise
+ */
+static int init_elbg(int *points, int dim, int numpoints, int *codebook,
                      int num_cb, int max_steps, int *closest_cb,
                      AVLFG *rand_state)
 {
@@ -443,13 +450,13 @@  int avpriv_init_elbg(int *points, int dim, int numpoints, int *codebook,
             memcpy(temp_points + i*dim, points + k*dim, dim * sizeof(*temp_points));
         }
 
-        ret = avpriv_init_elbg(temp_points, dim, numpoints / 8, codebook,
+        ret = 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,
+        ret = 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
@@ -458,3 +465,17 @@  int avpriv_init_elbg(int *points, int dim, int numpoints, int *codebook,
                    dim * sizeof(*codebook));
     return ret;
 }
+
+int avpriv_do_elbg(int *points, int dim, int numpoints,
+                   int *codebook, int num_cb, int max_steps,
+                   int *closest_cb, AVLFG *rand_state)
+{
+    int ret;
+
+    ret = init_elbg(points, dim, numpoints, codebook,
+                    num_cb, max_steps, closest_cb, rand_state);
+    if (ret < 0)
+        return ret;
+    return do_elbg (points, dim, numpoints, codebook,
+                    num_cb, max_steps, closest_cb, rand_state);
+}
diff --git a/libavcodec/elbg.h b/libavcodec/elbg.h
index f48aa3b443..3b9c2931f3 100644
--- a/libavcodec/elbg.h
+++ b/libavcodec/elbg.h
@@ -42,16 +42,4 @@  int avpriv_do_elbg(int *points, int dim, int numpoints, int *codebook,
                int numCB, int num_steps, int *closest_cb,
                AVLFG *rand_state);
 
-/**
- * Initialize the **codebook vector for the elbg algorithm. If you have already
- * a codebook and you want to refine it, you shouldn't call this function.
- * If numpoints < 8*numCB this function fills **codebook with random numbers.
- * If not, it calls avpriv_do_elbg for a (smaller) random sample of the points in
- * **points. Get the same parameters as avpriv_do_elbg.
- * @return < 0 in case of error, 0 otherwise
- */
-int avpriv_init_elbg(int *points, int dim, int numpoints, int *codebook,
-                 int numCB, int num_steps, int *closest_cb,
-                 AVLFG *rand_state);
-
 #endif /* AVCODEC_ELBG_H */
diff --git a/libavcodec/msvideo1enc.c b/libavcodec/msvideo1enc.c
index df621a6f48..fa65a2fbc4 100644
--- a/libavcodec/msvideo1enc.c
+++ b/libavcodec/msvideo1enc.c
@@ -117,7 +117,6 @@  static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
             }
             // try to find optimal value to fill whole 4x4 block
             score = 0;
-            avpriv_init_elbg(c->block, 3, 16, c->avg, 1, 1, c->output, &c->rnd);
             avpriv_do_elbg  (c->block, 3, 16, c->avg, 1, 1, c->output, &c->rnd);
             if(c->avg[0] == 1) // red component = 1 will be written as skip code
                 c->avg[0] = 0;
@@ -137,7 +136,6 @@  static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
             }
             // search for optimal filling of 2-color block
             score = 0;
-            avpriv_init_elbg(c->block, 3, 16, c->codebook, 2, 1, c->output, &c->rnd);
             avpriv_do_elbg  (c->block, 3, 16, c->codebook, 2, 1, c->output, &c->rnd);
             // last output value should be always 1, swap codebooks if needed
             if(!c->output[15]){
@@ -163,7 +161,6 @@  static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
             // search for optimal filling of 2-color 2x2 subblocks
             score = 0;
             for(i = 0; i < 4; i++){
-                avpriv_init_elbg(c->block2 + i*4*3, 3, 4, c->codebook2 + i*2*3, 2, 1, c->output2 + i*4, &c->rnd);
                 avpriv_do_elbg  (c->block2 + i*4*3, 3, 4, c->codebook2 + i*2*3, 2, 1, c->output2 + i*4, &c->rnd);
             }
             // last value should be always 1, swap codebooks if needed
diff --git a/libavcodec/roqvideoenc.c b/libavcodec/roqvideoenc.c
index f8e363ada7..167e6bc806 100644
--- a/libavcodec/roqvideoenc.c
+++ b/libavcodec/roqvideoenc.c
@@ -824,10 +824,6 @@  static int generate_codebook(RoqEncContext *enc,
     int *codebook = enc->tmp_codebook_buf;
     int *closest_cb = enc->closest_cb;
 
-    ret = avpriv_init_elbg(points, 6 * c_size, inputCount, codebook,
-                       cbsize, 1, closest_cb, &enc->randctx);
-    if (ret < 0)
-        return ret;
     ret = avpriv_do_elbg(points, 6 * c_size, inputCount, codebook,
                      cbsize, 1, closest_cb, &enc->randctx);
     if (ret < 0)
diff --git a/libavfilter/vf_elbg.c b/libavfilter/vf_elbg.c
index 8f97465f12..bf78030ffe 100644
--- a/libavfilter/vf_elbg.c
+++ b/libavfilter/vf_elbg.c
@@ -163,9 +163,6 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     }
 
     /* compute the codebook */
-    avpriv_init_elbg(elbg->codeword, NB_COMPONENTS, elbg->codeword_length,
-                     elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
-                     elbg->codeword_closest_codebook_idxs, &elbg->lfg);
     avpriv_do_elbg(elbg->codeword, NB_COMPONENTS, elbg->codeword_length,
                    elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
                    elbg->codeword_closest_codebook_idxs, &elbg->lfg);