[FFmpeg-cvslog] avocdec/mjpegenc_huffman: Avoid redundant loop

Andreas Rheinhardt git at videolan.org
Wed Apr 9 15:03:16 EEST 2025


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Wed Apr  2 12:54:07 2025 +0200| [31d5686c397a9db801aa891dec84d9f19a56d4d7] | committer: Andreas Rheinhardt

avocdec/mjpegenc_huffman: Avoid redundant loop

There is no point in iterating over the list twice.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=31d5686c397a9db801aa891dec84d9f19a56d4d7
---

 libavcodec/mjpegenc_huffman.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)

diff --git a/libavcodec/mjpegenc_huffman.c b/libavcodec/mjpegenc_huffman.c
index 898db7d060..2556959132 100644
--- a/libavcodec/mjpegenc_huffman.c
+++ b/libavcodec/mjpegenc_huffman.c
@@ -185,31 +185,27 @@ void ff_mjpeg_encode_huffman_init(MJpegEncHuffmanContext *s)
 void ff_mjpeg_encode_huffman_close(MJpegEncHuffmanContext *s, uint8_t bits[17],
                                    uint8_t val[], int max_nval)
 {
-    int i, j;
-    int nval = 0;
     PTable val_counts[257];
     HuffTable distincts[256];
 
-    for (i = 0; i < 256; i++) {
-        if (s->val_count[i]) nval++;
-    }
-    av_assert0 (nval <= max_nval);
+    av_assert1(max_nval <= FF_ARRAY_ELEMS(val_counts) - 1);
 
-    j = 0;
-    for (i = 0; i < 256; i++) {
+    int nval = 0;
+    for (int i = 0; i < 256; i++) {
         if (s->val_count[i]) {
-            val_counts[j].value = i;
-            val_counts[j].prob = s->val_count[i];
-            j++;
+            val_counts[nval].value = i;
+            val_counts[nval].prob  = s->val_count[i];
+            nval++;
+            av_assert2(nval <= max_nval);
         }
     }
-    val_counts[j].value = 256;
-    val_counts[j].prob = 0;
+    val_counts[nval].value = 256;
+    val_counts[nval].prob  = 0;
     mjpegenc_huffman_compute_bits(val_counts, distincts, nval + 1, 16);
     AV_QSORT(distincts, nval, HuffTable, compare_by_length);
 
     memset(bits, 0, sizeof(bits[0]) * 17);
-    for (i = 0; i < nval; i++) {
+    for (int i = 0; i < nval; i++) {
         val[i] = distincts[i].code;
         bits[distincts[i].length]++;
     }



More information about the ffmpeg-cvslog mailing list