[FFmpeg-devel] [PATCH 30/60] avcodec/ffv1enc: fix shadowing and narrow variable scopes

Marvin Scholz epirat07 at gmail.com
Mon Sep 9 00:55:16 EEST 2024


---
 libavcodec/ffv1enc.c          | 113 +++++++++++++++++-----------------
 libavcodec/ffv1enc_template.c |  19 +++---
 2 files changed, 63 insertions(+), 69 deletions(-)

diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index 23d757e5c6..f054ef0974 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -517,7 +517,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
 {
     FFV1Context *s = avctx->priv_data;
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
-    int i, j, k, m, ret;
+    int ret;
 
     if ((ret = ff_ffv1_common_init(avctx)) < 0)
         return ret;
@@ -704,16 +704,16 @@ static av_cold int encode_init(AVCodecContext *avctx)
     }
 
     if (s->ac == AC_RANGE_CUSTOM_TAB) {
-        for (i = 1; i < 256; i++)
+        for (int i = 1; i < 256; i++)
             s->state_transition[i] = ver2_state[i];
     } else {
         RangeCoder c;
         ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
-        for (i = 1; i < 256; i++)
+        for (int i = 1; i < 256; i++)
             s->state_transition[i] = c.one_state[i];
     }
 
-    for (i = 0; i < 256; i++) {
+    for (int i = 0; i < 256; i++) {
         s->quant_table_count = 2;
         if (s->bits_per_raw_sample <= 8) {
             s->quant_tables[0][0][i]=           quant11[i];
@@ -753,7 +753,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
     s->picture_number = 0;
 
     if (avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
-        for (i = 0; i < s->quant_table_count; i++) {
+        for (int i = 0; i < s->quant_table_count; i++) {
             s->rc_stat2[i] = av_mallocz(s->context_count[i] *
                                         sizeof(*s->rc_stat2[i]));
             if (!s->rc_stat2[i])
@@ -761,7 +761,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
         }
     }
     if (avctx->stats_in) {
-        char *p = avctx->stats_in;
+        char *stats = avctx->stats_in;
         uint8_t (*best_state)[256] = av_malloc_array(256, 256);
         int gob_count = 0;
         char *next;
@@ -771,42 +771,42 @@ static av_cold int encode_init(AVCodecContext *avctx)
         av_assert0(s->version >= 2);
 
         for (;;) {
-            for (j = 0; j < 256; j++)
-                for (i = 0; i < 2; i++) {
-                    s->rc_stat[j][i] = strtol(p, &next, 0);
-                    if (next == p) {
+            for (int j = 0; j < 256; j++)
+                for (int i = 0; i < 2; i++) {
+                    s->rc_stat[j][i] = strtol(stats, &next, 0);
+                    if (next == stats) {
                         av_log(avctx, AV_LOG_ERROR,
-                               "2Pass file invalid at %d %d [%s]\n", j, i, p);
+                               "2Pass file invalid at %d %d [%s]\n", j, i, stats);
                         av_freep(&best_state);
                         return AVERROR_INVALIDDATA;
                     }
-                    p = next;
+                    stats = next;
                 }
-            for (i = 0; i < s->quant_table_count; i++)
-                for (j = 0; j < s->context_count[i]; j++) {
-                    for (k = 0; k < 32; k++)
-                        for (m = 0; m < 2; m++) {
-                            s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
-                            if (next == p) {
+            for (int i = 0; i < s->quant_table_count; i++)
+                for (int j = 0; j < s->context_count[i]; j++) {
+                    for (int k = 0; k < 32; k++)
+                        for (int m = 0; m < 2; m++) {
+                            s->rc_stat2[i][j][k][m] = strtol(stats, &next, 0);
+                            if (next == stats) {
                                 av_log(avctx, AV_LOG_ERROR,
                                        "2Pass file invalid at %d %d %d %d [%s]\n",
-                                       i, j, k, m, p);
+                                       i, j, k, m, stats);
                                 av_freep(&best_state);
                                 return AVERROR_INVALIDDATA;
                             }
-                            p = next;
+                            stats = next;
                         }
                 }
-            gob_count = strtol(p, &next, 0);
-            if (next == p || gob_count <= 0) {
+            gob_count = strtol(stats, &next, 0);
+            if (next == stats || gob_count <= 0) {
                 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
                 av_freep(&best_state);
                 return AVERROR_INVALIDDATA;
             }
-            p = next;
-            while (*p == '\n' || *p == ' ')
-                p++;
-            if (p[0] == 0)
+            stats = next;
+            while (*stats == '\n' || *stats == ' ')
+                stats++;
+            if (stats[0] == 0)
                 break;
         }
         if (s->ac == AC_RANGE_CUSTOM_TAB)
@@ -814,11 +814,11 @@ static av_cold int encode_init(AVCodecContext *avctx)
 
         find_best_state(best_state, s->state_transition);
 
-        for (i = 0; i < s->quant_table_count; i++) {
-            for (k = 0; k < 32; k++) {
+        for (int i = 0; i < s->quant_table_count; i++) {
+            for (int k = 0; k < 32; k++) {
                 double a=0, b=0;
                 int jp = 0;
-                for (j = 0; j < s->context_count[i]; j++) {
+                for (int j = 0; j < s->context_count[i]; j++) {
                     double p = 128;
                     if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1] > 200 && j || a+b > 200) {
                         if (a+b)
@@ -878,10 +878,10 @@ slices_ok:
 
     for (int j = 0; j < s->slice_count; j++) {
         for (int i = 0; i < s->plane_count; i++) {
-            PlaneContext *const p = &s->slices[j].plane[i];
+            PlaneContext *const plane = &s->slices[j].plane[i];
 
-            p->quant_table_index = s->context_model;
-            p->context_count     = s->context_count[p->quant_table_index];
+            plane->quant_table_index = s->context_model;
+            plane->context_count     = s->context_count[plane->quant_table_index];
         }
 
         ff_build_rac_states(&s->slices[j].c, 0.05 * (1LL << 32), 256 - 8);
@@ -895,8 +895,8 @@ slices_ok:
         avctx->stats_out = av_mallocz(STATS_OUT_SIZE);
         if (!avctx->stats_out)
             return AVERROR(ENOMEM);
-        for (i = 0; i < s->quant_table_count; i++)
-            for (j = 0; j < s->max_slice_count; j++) {
+        for (int i = 0; i < s->quant_table_count; i++)
+            for (int j = 0; j < s->max_slice_count; j++) {
                 FFV1SliceContext *sc = &s->slices[j];
                 av_assert0(!sc->rc_stat2[i]);
                 sc->rc_stat2[i] = av_mallocz(s->context_count[i] *
@@ -913,14 +913,13 @@ static void encode_slice_header(FFV1Context *f, FFV1SliceContext *sc)
 {
     RangeCoder *c = &sc->c;
     uint8_t state[CONTEXT_SIZE];
-    int j;
     memset(state, 128, sizeof(state));
 
     put_symbol(c, state, (sc->slice_x     +1)*f->num_h_slices / f->width   , 0);
     put_symbol(c, state, (sc->slice_y     +1)*f->num_v_slices / f->height  , 0);
     put_symbol(c, state, (sc->slice_width +1)*f->num_h_slices / f->width -1, 0);
     put_symbol(c, state, (sc->slice_height+1)*f->num_v_slices / f->height-1, 0);
-    for (j=0; j<f->plane_count; j++) {
+    for (int j = 0; j < f->plane_count; j++) {
         put_symbol(c, state, sc->plane[j].quant_table_index, 0);
         av_assert0(sc->plane[j].quant_table_index == f->context_model);
     }
@@ -966,16 +965,16 @@ static void choose_rct_params(const FFV1Context *f, FFV1SliceContext *sc,
     };
 
     int stat[NB_Y_COEFF] = {0};
-    int x, y, i, p, best;
+    int best;
     int16_t *sample[3];
     int lbd = f->bits_per_raw_sample <= 8;
 
-    for (y = 0; y < h; y++) {
+    for (int y = 0; y < h; y++) {
         int lastr=0, lastg=0, lastb=0;
-        for (p = 0; p < 3; p++)
+        for (int p = 0; p < 3; p++)
             sample[p] = sc->sample_buffer + p*w;
 
-        for (x = 0; x < w; x++) {
+        for (int x = 0; x < w; x++) {
             int b, g, r;
             int ab, ag, ar;
             if (lbd) {
@@ -1000,7 +999,7 @@ static void choose_rct_params(const FFV1Context *f, FFV1SliceContext *sc,
                 br -= bg;
                 bb -= bg;
 
-                for (i = 0; i<NB_Y_COEFF; i++) {
+                for (int i = 0; i<NB_Y_COEFF; i++) {
                     stat[i] += FFABS(bg + ((br*rct_y_coeff[i][0] + bb*rct_y_coeff[i][1])>>2));
                 }
 
@@ -1016,7 +1015,7 @@ static void choose_rct_params(const FFV1Context *f, FFV1SliceContext *sc,
     }
 
     best = 0;
-    for (i=1; i<NB_Y_COEFF; i++) {
+    for (int i = 1; i < NB_Y_COEFF; i++) {
         if (stat[i] < stat[best])
             best = i;
     }
@@ -1108,46 +1107,45 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     RangeCoder *const c = &f->slices[0].c;
     uint8_t keystate    = 128;
     uint8_t *buf_p;
-    int i, ret;
+    int ret;
     int64_t maxsize =   FF_INPUT_BUFFER_MIN_SIZE
                       + avctx->width*avctx->height*37LL*4;
 
     if(!pict) {
         if (avctx->flags & AV_CODEC_FLAG_PASS1) {
-            int j, k, m;
             char *p   = avctx->stats_out;
             char *end = p + STATS_OUT_SIZE;
 
             memset(f->rc_stat, 0, sizeof(f->rc_stat));
-            for (i = 0; i < f->quant_table_count; i++)
+            for (int i = 0; i < f->quant_table_count; i++)
                 memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
 
             av_assert0(f->slice_count == f->max_slice_count);
-            for (j = 0; j < f->slice_count; j++) {
+            for (int j = 0; j < f->slice_count; j++) {
                 const FFV1SliceContext *sc = &f->slices[j];
-                for (i = 0; i < 256; i++) {
+                for (int i = 0; i < 256; i++) {
                     f->rc_stat[i][0] += sc->rc_stat[i][0];
                     f->rc_stat[i][1] += sc->rc_stat[i][1];
                 }
-                for (i = 0; i < f->quant_table_count; i++) {
-                    for (k = 0; k < f->context_count[i]; k++)
-                        for (m = 0; m < 32; m++) {
+                for (int i = 0; i < f->quant_table_count; i++) {
+                    for (int k = 0; k < f->context_count[i]; k++)
+                        for (int m = 0; m < 32; m++) {
                             f->rc_stat2[i][k][m][0] += sc->rc_stat2[i][k][m][0];
                             f->rc_stat2[i][k][m][1] += sc->rc_stat2[i][k][m][1];
                         }
                 }
             }
 
-            for (j = 0; j < 256; j++) {
+            for (int j = 0; j < 256; j++) {
                 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
                         f->rc_stat[j][0], f->rc_stat[j][1]);
                 p += strlen(p);
             }
             snprintf(p, end - p, "\n");
 
-            for (i = 0; i < f->quant_table_count; i++) {
-                for (j = 0; j < f->context_count[i]; j++)
-                    for (m = 0; m < 32; m++) {
+            for (int i = 0; i < f->quant_table_count; i++) {
+                for (int j = 0; j < f->context_count[i]; j++)
+                    for (int m = 0; m < 32; m++) {
                         snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
                                 f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
                         p += strlen(p);
@@ -1185,14 +1183,13 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     }
 
     if (f->ac == AC_RANGE_CUSTOM_TAB) {
-        int i;
-        for (i = 1; i < 256; i++) {
+        for (int i = 1; i < 256; i++) {
             c->one_state[i]        = f->state_transition[i];
             c->zero_state[256 - i] = 256 - c->one_state[i];
         }
     }
 
-    for (i = 0; i < f->slice_count; i++) {
+    for (int i = 0; i < f->slice_count; i++) {
         FFV1SliceContext *sc = &f->slices[i];
         uint8_t *start  = pkt->data + pkt->size * (int64_t)i / f->slice_count;
         int len         = pkt->size / f->slice_count;
@@ -1208,7 +1205,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                    f->slice_count, sizeof(*f->slices));
 
     buf_p = pkt->data;
-    for (i = 0; i < f->slice_count; i++) {
+    for (int i = 0; i < f->slice_count; i++) {
         FFV1SliceContext *sc = &f->slices[i];
         int bytes;
 
diff --git a/libavcodec/ffv1enc_template.c b/libavcodec/ffv1enc_template.c
index bc14926ab9..af000c0b51 100644
--- a/libavcodec/ffv1enc_template.c
+++ b/libavcodec/ffv1enc_template.c
@@ -30,7 +30,6 @@ RENAME(encode_line)(FFV1Context *f, FFV1SliceContext *sc,
 {
     PlaneContext *const p = &sc->plane[plane_index];
     RangeCoder *const c   = &sc->c;
-    int x;
     int run_index = sc->run_index;
     int run_count = 0;
     int run_mode  = 0;
@@ -48,10 +47,9 @@ RENAME(encode_line)(FFV1Context *f, FFV1SliceContext *sc,
     }
 
     if (sc->slice_coding_mode == 1) {
-        for (x = 0; x < w; x++) {
-            int i;
+        for (int x = 0; x < w; x++) {
             int v = sample[0][x];
-            for (i = bits-1; i>=0; i--) {
+            for (int i = bits-1; i>=0; i--) {
                 uint8_t state = 128;
                 put_rac(c, &state, (v>>i) & 1);
             }
@@ -59,7 +57,7 @@ RENAME(encode_line)(FFV1Context *f, FFV1SliceContext *sc,
         return 0;
     }
 
-    for (x = 0; x < w; x++) {
+    for (int x = 0; x < w; x++) {
         int diff, context;
 
         context = RENAME(get_context)(f->quant_tables[p->quant_table_index],
@@ -131,7 +129,6 @@ static int RENAME(encode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
                                     const uint8_t *src[4],
                                     int w, int h, const int stride[4])
 {
-    int x, y, p, i;
     const int ring_size = f->context_model ? 3 : 2;
     TYPE *sample[4][3];
     const int ac = f->ac;
@@ -148,12 +145,12 @@ static int RENAME(encode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
     memset(RENAME(sc->sample_buffer), 0, ring_size * MAX_PLANES *
            (w + 6) * sizeof(*RENAME(sc->sample_buffer)));
 
-    for (y = 0; y < h; y++) {
-        for (i = 0; i < ring_size; i++)
-            for (p = 0; p < MAX_PLANES; p++)
+    for (int y = 0; y < h; y++) {
+        for (int i = 0; i < ring_size; i++)
+            for (int p = 0; p < MAX_PLANES; p++)
                 sample[p][i]= RENAME(sc->sample_buffer) + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
 
-        for (x = 0; x < w; x++) {
+        for (int x = 0; x < w; x++) {
             int b, g, r, av_uninit(a);
             if (lbd) {
                 unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
@@ -193,7 +190,7 @@ static int RENAME(encode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
             sample[2][0][x] = r;
             sample[3][0][x] = a;
         }
-        for (p = 0; p < 3 + transparency; p++) {
+        for (int p = 0; p < 3 + transparency; p++) {
             int ret;
             sample[p][0][-1] = sample[p][1][0  ];
             sample[p][1][ w] = sample[p][1][w-1];
-- 
2.39.3 (Apple Git-146)




More information about the ffmpeg-devel mailing list