Skip to content

Commit 31d5686

Browse files
committed
avocdec/mjpegenc_huffman: Avoid redundant loop
There is no point in iterating over the list twice. Signed-off-by: Andreas Rheinhardt <[email protected]>
1 parent 7ad16a4 commit 31d5686

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

libavcodec/mjpegenc_huffman.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -185,31 +185,27 @@ void ff_mjpeg_encode_huffman_init(MJpegEncHuffmanContext *s)
185185
void ff_mjpeg_encode_huffman_close(MJpegEncHuffmanContext *s, uint8_t bits[17],
186186
uint8_t val[], int max_nval)
187187
{
188-
int i, j;
189-
int nval = 0;
190188
PTable val_counts[257];
191189
HuffTable distincts[256];
192190

193-
for (i = 0; i < 256; i++) {
194-
if (s->val_count[i]) nval++;
195-
}
196-
av_assert0 (nval <= max_nval);
191+
av_assert1(max_nval <= FF_ARRAY_ELEMS(val_counts) - 1);
197192

198-
j = 0;
199-
for (i = 0; i < 256; i++) {
193+
int nval = 0;
194+
for (int i = 0; i < 256; i++) {
200195
if (s->val_count[i]) {
201-
val_counts[j].value = i;
202-
val_counts[j].prob = s->val_count[i];
203-
j++;
196+
val_counts[nval].value = i;
197+
val_counts[nval].prob = s->val_count[i];
198+
nval++;
199+
av_assert2(nval <= max_nval);
204200
}
205201
}
206-
val_counts[j].value = 256;
207-
val_counts[j].prob = 0;
202+
val_counts[nval].value = 256;
203+
val_counts[nval].prob = 0;
208204
mjpegenc_huffman_compute_bits(val_counts, distincts, nval + 1, 16);
209205
AV_QSORT(distincts, nval, HuffTable, compare_by_length);
210206

211207
memset(bits, 0, sizeof(bits[0]) * 17);
212-
for (i = 0; i < nval; i++) {
208+
for (int i = 0; i < nval; i++) {
213209
val[i] = distincts[i].code;
214210
bits[distincts[i].length]++;
215211
}

0 commit comments

Comments
 (0)