Skip to content

Commit f3fb528

Browse files
committed
avutil/half2float: move tables to header-internal structs
Having to put the knowledge of the size of those arrays into a multitude of places is rather smelly.
1 parent cb8ad00 commit f3fb528

File tree

7 files changed

+84
-111
lines changed

7 files changed

+84
-111
lines changed

libavcodec/exr.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ typedef struct EXRContext {
191191
float gamma;
192192
union av_intfloat32 gamma_table[65536];
193193

194-
uint32_t mantissatable[3072];
195-
uint32_t exponenttable[64];
196-
uint16_t offsettable[64];
194+
Half2FloatTables h2f_tables;
197195
} EXRContext;
198196

199197
static int zip_uncompress(const EXRContext *s, const uint8_t *src, int compressed_size,
@@ -899,10 +897,7 @@ static int ac_uncompress(const EXRContext *s, GetByteContext *gb, float *block)
899897
n += val & 0xff;
900898
} else {
901899
ret = n;
902-
block[ff_zigzag_direct[n]] = av_int2float(half2float(val,
903-
s->mantissatable,
904-
s->exponenttable,
905-
s->offsettable));
900+
block[ff_zigzag_direct[n]] = av_int2float(half2float(val, &s->h2f_tables));
906901
n++;
907902
}
908903
}
@@ -1120,8 +1115,7 @@ static int dwa_uncompress(const EXRContext *s, const uint8_t *src, int compresse
11201115
uint16_t *dc = (uint16_t *)td->dc_data;
11211116
union av_intfloat32 dc_val;
11221117

1123-
dc_val.i = half2float(dc[idx], s->mantissatable,
1124-
s->exponenttable, s->offsettable);
1118+
dc_val.i = half2float(dc[idx], &s->h2f_tables);
11251119

11261120
block[0] = dc_val.f;
11271121
ac_uncompress(s, &agb, block);
@@ -1171,7 +1165,7 @@ static int dwa_uncompress(const EXRContext *s, const uint8_t *src, int compresse
11711165
for (int x = 0; x < td->xsize; x++) {
11721166
uint16_t ha = ai0[x] | (ai1[x] << 8);
11731167

1174-
ao[x] = half2float(ha, s->mantissatable, s->exponenttable, s->offsettable);
1168+
ao[x] = half2float(ha, &s->h2f_tables);
11751169
}
11761170
}
11771171

@@ -1427,10 +1421,7 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
14271421
}
14281422
} else {
14291423
for (x = 0; x < xsize; x++) {
1430-
ptr_x[0].i = half2float(bytestream_get_le16(&src),
1431-
s->mantissatable,
1432-
s->exponenttable,
1433-
s->offsettable);
1424+
ptr_x[0].i = half2float(bytestream_get_le16(&src), &s->h2f_tables);
14341425
ptr_x++;
14351426
}
14361427
}
@@ -2217,7 +2208,7 @@ static av_cold int decode_init(AVCodecContext *avctx)
22172208
float one_gamma = 1.0f / s->gamma;
22182209
avpriv_trc_function trc_func = NULL;
22192210

2220-
half2float_table(s->mantissatable, s->exponenttable, s->offsettable);
2211+
init_half2float_tables(&s->h2f_tables);
22212212

22222213
s->avctx = avctx;
22232214

@@ -2230,18 +2221,18 @@ static av_cold int decode_init(AVCodecContext *avctx)
22302221
trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
22312222
if (trc_func) {
22322223
for (i = 0; i < 65536; ++i) {
2233-
t.i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable);
2224+
t.i = half2float(i, &s->h2f_tables);
22342225
t.f = trc_func(t.f);
22352226
s->gamma_table[i] = t;
22362227
}
22372228
} else {
22382229
if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
22392230
for (i = 0; i < 65536; ++i) {
2240-
s->gamma_table[i].i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable);
2231+
s->gamma_table[i].i = half2float(i, &s->h2f_tables);
22412232
}
22422233
} else {
22432234
for (i = 0; i < 65536; ++i) {
2244-
t.i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable);
2235+
t.i = half2float(i, &s->h2f_tables);
22452236
/* If negative value we reuse half value */
22462237
if (t.f <= 0.0f) {
22472238
s->gamma_table[i] = t;

libavcodec/exrenc.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,14 @@ typedef struct EXRContext {
8787

8888
EXRScanlineData *scanline;
8989

90-
uint16_t basetable[512];
91-
uint8_t shifttable[512];
90+
Float2HalfTables f2h_tables;
9291
} EXRContext;
9392

9493
static av_cold int encode_init(AVCodecContext *avctx)
9594
{
9695
EXRContext *s = avctx->priv_data;
9796

98-
float2half_tables(s->basetable, s->shifttable);
97+
init_float2half_tables(&s->f2h_tables);
9998

10099
switch (avctx->pix_fmt) {
101100
case AV_PIX_FMT_GBRPF32:
@@ -256,7 +255,7 @@ static int encode_scanline_rle(EXRContext *s, const AVFrame *frame)
256255
const uint32_t *src = (const uint32_t *)(frame->data[ch] + y * frame->linesize[ch]);
257256

258257
for (int x = 0; x < frame->width; x++)
259-
dst[x] = float2half(src[x], s->basetable, s->shifttable);
258+
dst[x] = float2half(src[x], &s->f2h_tables);
260259
}
261260
break;
262261
}
@@ -324,7 +323,7 @@ static int encode_scanline_zip(EXRContext *s, const AVFrame *frame)
324323
const uint32_t *src = (const uint32_t *)(frame->data[ch] + (y * s->scanline_height + l) * frame->linesize[ch]);
325324

326325
for (int x = 0; x < frame->width; x++)
327-
dst[x] = float2half(src[x], s->basetable, s->shifttable);
326+
dst[x] = float2half(src[x], &s->f2h_tables);
328327
}
329328
}
330329
break;
@@ -482,7 +481,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
482481
const uint32_t *src = (const uint32_t *)(frame->data[ch] + y * frame->linesize[ch]);
483482

484483
for (int x = 0; x < frame->width; x++)
485-
bytestream2_put_le16(pb, float2half(src[x], s->basetable, s->shifttable));
484+
bytestream2_put_le16(pb, float2half(src[x], &s->f2h_tables));
486485
}
487486
}
488487
}

libavcodec/pnm.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#ifndef AVCODEC_PNM_H
2323
#define AVCODEC_PNM_H
2424

25+
#include "libavutil/half2float.h"
2526
#include "avcodec.h"
2627

2728
typedef struct PNMContext {
@@ -34,9 +35,7 @@ typedef struct PNMContext {
3435
int half;
3536
float scale;
3637

37-
uint32_t mantissatable[3072];
38-
uint32_t exponenttable[64];
39-
uint16_t offsettable[64];
38+
Half2FloatTables h2f_tables;
4039
} PNMContext;
4140

4241
int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s);

libavcodec/pnmdec.c

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,9 @@ static int pnm_decode_frame(AVCodecContext *avctx, AVFrame *p,
313313
b = (float *)p->data[1];
314314
for (int i = 0; i < avctx->height; i++) {
315315
for (int j = 0; j < avctx->width; j++) {
316-
r[j] = av_int2float(half2float(AV_RL16(s->bytestream+0),
317-
s->mantissatable,
318-
s->exponenttable,
319-
s->offsettable)) * scale;
320-
g[j] = av_int2float(half2float(AV_RL16(s->bytestream+2),
321-
s->mantissatable,
322-
s->exponenttable,
323-
s->offsettable)) * scale;
324-
b[j] = av_int2float(half2float(AV_RL16(s->bytestream+4),
325-
s->mantissatable,
326-
s->exponenttable,
327-
s->offsettable)) * scale;
316+
r[j] = av_int2float(half2float(AV_RL16(s->bytestream+0), &s->h2f_tables)) * scale;
317+
g[j] = av_int2float(half2float(AV_RL16(s->bytestream+2), &s->h2f_tables)) * scale;
318+
b[j] = av_int2float(half2float(AV_RL16(s->bytestream+4), &s->h2f_tables)) * scale;
328319
s->bytestream += 6;
329320
}
330321

@@ -340,18 +331,9 @@ static int pnm_decode_frame(AVCodecContext *avctx, AVFrame *p,
340331
b = (float *)p->data[1];
341332
for (int i = 0; i < avctx->height; i++) {
342333
for (int j = 0; j < avctx->width; j++) {
343-
r[j] = av_int2float(half2float(AV_RB16(s->bytestream+0),
344-
s->mantissatable,
345-
s->exponenttable,
346-
s->offsettable)) * scale;
347-
g[j] = av_int2float(half2float(AV_RB16(s->bytestream+2),
348-
s->mantissatable,
349-
s->exponenttable,
350-
s->offsettable)) * scale;
351-
b[j] = av_int2float(half2float(AV_RB16(s->bytestream+4),
352-
s->mantissatable,
353-
s->exponenttable,
354-
s->offsettable)) * scale;
334+
r[j] = av_int2float(half2float(AV_RB16(s->bytestream+0), &s->h2f_tables)) * scale;
335+
g[j] = av_int2float(half2float(AV_RB16(s->bytestream+2), &s->h2f_tables)) * scale;
336+
b[j] = av_int2float(half2float(AV_RB16(s->bytestream+4), &s->h2f_tables)) * scale;
355337
s->bytestream += 6;
356338
}
357339

@@ -394,10 +376,7 @@ static int pnm_decode_frame(AVCodecContext *avctx, AVFrame *p,
394376
float *g = (float *)p->data[0];
395377
for (int i = 0; i < avctx->height; i++) {
396378
for (int j = 0; j < avctx->width; j++) {
397-
g[j] = av_int2float(half2float(AV_RL16(s->bytestream),
398-
s->mantissatable,
399-
s->exponenttable,
400-
s->offsettable)) * scale;
379+
g[j] = av_int2float(half2float(AV_RL16(s->bytestream), &s->h2f_tables)) * scale;
401380
s->bytestream += 2;
402381
}
403382
g += p->linesize[0] / 4;
@@ -406,10 +385,7 @@ static int pnm_decode_frame(AVCodecContext *avctx, AVFrame *p,
406385
float *g = (float *)p->data[0];
407386
for (int i = 0; i < avctx->height; i++) {
408387
for (int j = 0; j < avctx->width; j++) {
409-
g[j] = av_int2float(half2float(AV_RB16(s->bytestream),
410-
s->mantissatable,
411-
s->exponenttable,
412-
s->offsettable)) * scale;
388+
g[j] = av_int2float(half2float(AV_RB16(s->bytestream), &s->h2f_tables)) * scale;
413389
s->bytestream += 2;
414390
}
415391
g += p->linesize[0] / 4;
@@ -501,7 +477,7 @@ static av_cold int phm_dec_init(AVCodecContext *avctx)
501477
{
502478
PNMContext *s = avctx->priv_data;
503479

504-
half2float_table(s->mantissatable, s->exponenttable, s->offsettable);
480+
init_half2float_tables(&s->h2f_tables);
505481

506482
return 0;
507483
}

libavcodec/pnmenc.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
#include "encode.h"
3131

3232
typedef struct PHMEncContext {
33-
uint16_t basetable[512];
34-
uint8_t shifttable[512];
33+
Float2HalfTables f2h_tables;
3534
} PHMEncContext;
3635

3736
static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
@@ -169,9 +168,9 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
169168

170169
for (int i = 0; i < avctx->height; i++) {
171170
for (int j = 0; j < avctx->width; j++) {
172-
AV_WN16(bytestream + 0, float2half(av_float2int(r[j]), s->basetable, s->shifttable));
173-
AV_WN16(bytestream + 2, float2half(av_float2int(g[j]), s->basetable, s->shifttable));
174-
AV_WN16(bytestream + 4, float2half(av_float2int(b[j]), s->basetable, s->shifttable));
171+
AV_WN16(bytestream + 0, float2half(av_float2int(r[j]), &s->f2h_tables));
172+
AV_WN16(bytestream + 2, float2half(av_float2int(g[j]), &s->f2h_tables));
173+
AV_WN16(bytestream + 4, float2half(av_float2int(b[j]), &s->f2h_tables));
175174
bytestream += 6;
176175
}
177176

@@ -184,7 +183,7 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
184183

185184
for (int i = 0; i < avctx->height; i++) {
186185
for (int j = 0; j < avctx->width; j++) {
187-
AV_WN16(bytestream, float2half(av_float2int(g[j]), s->basetable, s->shifttable));
186+
AV_WN16(bytestream, float2half(av_float2int(g[j]), &s->f2h_tables));
188187
bytestream += 2;
189188
}
190189

@@ -295,7 +294,7 @@ static av_cold int phm_enc_init(AVCodecContext *avctx)
295294
{
296295
PHMEncContext *s = avctx->priv_data;
297296

298-
float2half_tables(s->basetable, s->shifttable);
297+
init_float2half_tables(&s->f2h_tables);
299298

300299
return 0;
301300
}

libavutil/float2half.h

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,50 @@
2121

2222
#include <stdint.h>
2323

24-
static void float2half_tables(uint16_t *basetable, uint8_t *shifttable)
24+
typedef struct Float2HalfTables {
25+
uint16_t basetable[512];
26+
uint8_t shifttable[512];
27+
} Float2HalfTables;
28+
29+
static void init_float2half_tables(Float2HalfTables *t)
2530
{
2631
for (int i = 0; i < 256; i++) {
2732
int e = i - 127;
2833

2934
if (e < -24) { // Very small numbers map to zero
30-
basetable[i|0x000] = 0x0000;
31-
basetable[i|0x100] = 0x8000;
32-
shifttable[i|0x000] = 24;
33-
shifttable[i|0x100] = 24;
35+
t->basetable[i|0x000] = 0x0000;
36+
t->basetable[i|0x100] = 0x8000;
37+
t->shifttable[i|0x000] = 24;
38+
t->shifttable[i|0x100] = 24;
3439
} else if (e < -14) { // Small numbers map to denorms
35-
basetable[i|0x000] = (0x0400>>(-e-14));
36-
basetable[i|0x100] = (0x0400>>(-e-14)) | 0x8000;
37-
shifttable[i|0x000] = -e-1;
38-
shifttable[i|0x100] = -e-1;
40+
t->basetable[i|0x000] = (0x0400>>(-e-14));
41+
t->basetable[i|0x100] = (0x0400>>(-e-14)) | 0x8000;
42+
t->shifttable[i|0x000] = -e-1;
43+
t->shifttable[i|0x100] = -e-1;
3944
} else if (e <= 15) { // Normal numbers just lose precision
40-
basetable[i|0x000] = ((e + 15) << 10);
41-
basetable[i|0x100] = ((e + 15) << 10) | 0x8000;
42-
shifttable[i|0x000] = 13;
43-
shifttable[i|0x100] = 13;
45+
t->basetable[i|0x000] = ((e + 15) << 10);
46+
t->basetable[i|0x100] = ((e + 15) << 10) | 0x8000;
47+
t->shifttable[i|0x000] = 13;
48+
t->shifttable[i|0x100] = 13;
4449
} else if (e < 128) { // Large numbers map to Infinity
45-
basetable[i|0x000] = 0x7C00;
46-
basetable[i|0x100] = 0xFC00;
47-
shifttable[i|0x000] = 24;
48-
shifttable[i|0x100] = 24;
50+
t->basetable[i|0x000] = 0x7C00;
51+
t->basetable[i|0x100] = 0xFC00;
52+
t->shifttable[i|0x000] = 24;
53+
t->shifttable[i|0x100] = 24;
4954
} else { // Infinity and NaN's stay Infinity and NaN's
50-
basetable[i|0x000] = 0x7C00;
51-
basetable[i|0x100] = 0xFC00;
52-
shifttable[i|0x000] = 13;
53-
shifttable[i|0x100] = 13;
55+
t->basetable[i|0x000] = 0x7C00;
56+
t->basetable[i|0x100] = 0xFC00;
57+
t->shifttable[i|0x000] = 13;
58+
t->shifttable[i|0x100] = 13;
5459
}
5560
}
5661
}
5762

58-
static uint16_t float2half(uint32_t f, uint16_t *basetable, uint8_t *shifttable)
63+
static uint16_t float2half(uint32_t f, const Float2HalfTables *t)
5964
{
6065
uint16_t h;
6166

62-
h = basetable[(f >> 23) & 0x1ff] + ((f & 0x007fffff) >> shifttable[(f >> 23) & 0x1ff]);
67+
h = t->basetable[(f >> 23) & 0x1ff] + ((f & 0x007fffff) >> t->shifttable[(f >> 23) & 0x1ff]);
6368

6469
return h;
6570
}

0 commit comments

Comments
 (0)