Skip to content

Commit 6dc79f1

Browse files
committed
avutil/half2float: move non-inline init code out of header
1 parent f3fb528 commit 6dc79f1

File tree

11 files changed

+166
-86
lines changed

11 files changed

+166
-86
lines changed

libavcodec/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ OBJS-$(CONFIG_EIGHTSVX_FIB_DECODER) += 8svx.o
337337
OBJS-$(CONFIG_ESCAPE124_DECODER) += escape124.o
338338
OBJS-$(CONFIG_ESCAPE130_DECODER) += escape130.o
339339
OBJS-$(CONFIG_EVRC_DECODER) += evrcdec.o acelp_vectors.o lsp.o
340-
OBJS-$(CONFIG_EXR_DECODER) += exr.o exrdsp.o
341-
OBJS-$(CONFIG_EXR_ENCODER) += exrenc.o
340+
OBJS-$(CONFIG_EXR_DECODER) += exr.o exrdsp.o half2float.o
341+
OBJS-$(CONFIG_EXR_ENCODER) += exrenc.o float2half.o
342342
OBJS-$(CONFIG_FASTAUDIO_DECODER) += fastaudio.o
343343
OBJS-$(CONFIG_FFV1_DECODER) += ffv1dec.o ffv1.o
344344
OBJS-$(CONFIG_FFV1_ENCODER) += ffv1enc.o ffv1.o
@@ -570,8 +570,8 @@ OBJS-$(CONFIG_PGMYUV_DECODER) += pnmdec.o pnm.o
570570
OBJS-$(CONFIG_PGMYUV_ENCODER) += pnmenc.o
571571
OBJS-$(CONFIG_PGSSUB_DECODER) += pgssubdec.o
572572
OBJS-$(CONFIG_PGX_DECODER) += pgxdec.o
573-
OBJS-$(CONFIG_PHM_DECODER) += pnmdec.o pnm.o
574-
OBJS-$(CONFIG_PHM_ENCODER) += pnmenc.o
573+
OBJS-$(CONFIG_PHM_DECODER) += pnmdec.o pnm.o half2float.o
574+
OBJS-$(CONFIG_PHM_ENCODER) += pnmenc.o float2half.o
575575
OBJS-$(CONFIG_PHOTOCD_DECODER) += photocd.o
576576
OBJS-$(CONFIG_PICTOR_DECODER) += pictordec.o cga_data.o
577577
OBJS-$(CONFIG_PIXLET_DECODER) += pixlet.o

libavcodec/exr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2208,7 +2208,7 @@ static av_cold int decode_init(AVCodecContext *avctx)
22082208
float one_gamma = 1.0f / s->gamma;
22092209
avpriv_trc_function trc_func = NULL;
22102210

2211-
init_half2float_tables(&s->h2f_tables);
2211+
ff_init_half2float_tables(&s->h2f_tables);
22122212

22132213
s->avctx = avctx;
22142214

libavcodec/exrenc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
9494
{
9595
EXRContext *s = avctx->priv_data;
9696

97-
init_float2half_tables(&s->f2h_tables);
97+
ff_init_float2half_tables(&s->f2h_tables);
9898

9999
switch (avctx->pix_fmt) {
100100
case AV_PIX_FMT_GBRPF32:

libavcodec/float2half.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* This file is part of FFmpeg.
3+
*
4+
* FFmpeg is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* FFmpeg is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with FFmpeg; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include "libavutil/float2half.c"

libavcodec/half2float.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* This file is part of FFmpeg.
3+
*
4+
* FFmpeg is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* FFmpeg is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with FFmpeg; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include "libavutil/half2float.c"

libavcodec/pnmdec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ static av_cold int phm_dec_init(AVCodecContext *avctx)
477477
{
478478
PNMContext *s = avctx->priv_data;
479479

480-
init_half2float_tables(&s->h2f_tables);
480+
ff_init_half2float_tables(&s->h2f_tables);
481481

482482
return 0;
483483
}

libavcodec/pnmenc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ static av_cold int phm_enc_init(AVCodecContext *avctx)
294294
{
295295
PHMEncContext *s = avctx->priv_data;
296296

297-
init_float2half_tables(&s->f2h_tables);
297+
ff_init_float2half_tables(&s->f2h_tables);
298298

299299
return 0;
300300
}

libavutil/float2half.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* This file is part of FFmpeg.
3+
*
4+
* FFmpeg is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* FFmpeg is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with FFmpeg; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include "libavutil/float2half.h"
20+
21+
void ff_init_float2half_tables(Float2HalfTables *t)
22+
{
23+
for (int i = 0; i < 256; i++) {
24+
int e = i - 127;
25+
26+
if (e < -24) { // Very small numbers map to zero
27+
t->basetable[i|0x000] = 0x0000;
28+
t->basetable[i|0x100] = 0x8000;
29+
t->shifttable[i|0x000] = 24;
30+
t->shifttable[i|0x100] = 24;
31+
} else if (e < -14) { // Small numbers map to denorms
32+
t->basetable[i|0x000] = (0x0400>>(-e-14));
33+
t->basetable[i|0x100] = (0x0400>>(-e-14)) | 0x8000;
34+
t->shifttable[i|0x000] = -e-1;
35+
t->shifttable[i|0x100] = -e-1;
36+
} else if (e <= 15) { // Normal numbers just lose precision
37+
t->basetable[i|0x000] = ((e + 15) << 10);
38+
t->basetable[i|0x100] = ((e + 15) << 10) | 0x8000;
39+
t->shifttable[i|0x000] = 13;
40+
t->shifttable[i|0x100] = 13;
41+
} else if (e < 128) { // Large numbers map to Infinity
42+
t->basetable[i|0x000] = 0x7C00;
43+
t->basetable[i|0x100] = 0xFC00;
44+
t->shifttable[i|0x000] = 24;
45+
t->shifttable[i|0x100] = 24;
46+
} else { // Infinity and NaN's stay Infinity and NaN's
47+
t->basetable[i|0x000] = 0x7C00;
48+
t->basetable[i|0x100] = 0xFC00;
49+
t->shifttable[i|0x000] = 13;
50+
t->shifttable[i|0x100] = 13;
51+
}
52+
}
53+
}

libavutil/float2half.h

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,41 +26,9 @@ typedef struct Float2HalfTables {
2626
uint8_t shifttable[512];
2727
} Float2HalfTables;
2828

29-
static void init_float2half_tables(Float2HalfTables *t)
30-
{
31-
for (int i = 0; i < 256; i++) {
32-
int e = i - 127;
33-
34-
if (e < -24) { // Very small numbers map to zero
35-
t->basetable[i|0x000] = 0x0000;
36-
t->basetable[i|0x100] = 0x8000;
37-
t->shifttable[i|0x000] = 24;
38-
t->shifttable[i|0x100] = 24;
39-
} else if (e < -14) { // Small numbers map to denorms
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;
44-
} else if (e <= 15) { // Normal numbers just lose precision
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;
49-
} else if (e < 128) { // Large numbers map to Infinity
50-
t->basetable[i|0x000] = 0x7C00;
51-
t->basetable[i|0x100] = 0xFC00;
52-
t->shifttable[i|0x000] = 24;
53-
t->shifttable[i|0x100] = 24;
54-
} else { // Infinity and NaN's stay Infinity and NaN's
55-
t->basetable[i|0x000] = 0x7C00;
56-
t->basetable[i|0x100] = 0xFC00;
57-
t->shifttable[i|0x000] = 13;
58-
t->shifttable[i|0x100] = 13;
59-
}
60-
}
61-
}
29+
void ff_init_float2half_tables(Float2HalfTables *t);
6230

63-
static uint16_t float2half(uint32_t f, const Float2HalfTables *t)
31+
static inline uint16_t float2half(uint32_t f, const Float2HalfTables *t)
6432
{
6533
uint16_t h;
6634

libavutil/half2float.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of FFmpeg.
3+
*
4+
* FFmpeg is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* FFmpeg is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with FFmpeg; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include "libavutil/half2float.h"
20+
21+
static uint32_t convertmantissa(uint32_t i)
22+
{
23+
int32_t m = i << 13; // Zero pad mantissa bits
24+
int32_t e = 0; // Zero exponent
25+
26+
while (!(m & 0x00800000)) { // While not normalized
27+
e -= 0x00800000; // Decrement exponent (1<<23)
28+
m <<= 1; // Shift mantissa
29+
}
30+
31+
m &= ~0x00800000; // Clear leading 1 bit
32+
e += 0x38800000; // Adjust bias ((127-14)<<23)
33+
34+
return m | e; // Return combined number
35+
}
36+
37+
void ff_init_half2float_tables(Half2FloatTables *t)
38+
{
39+
t->mantissatable[0] = 0;
40+
for (int i = 1; i < 1024; i++)
41+
t->mantissatable[i] = convertmantissa(i);
42+
for (int i = 1024; i < 2048; i++)
43+
t->mantissatable[i] = 0x38000000UL + ((i - 1024) << 13UL);
44+
for (int i = 2048; i < 3072; i++)
45+
t->mantissatable[i] = t->mantissatable[i - 1024] | 0x400000UL;
46+
t->mantissatable[2048] = t->mantissatable[1024];
47+
48+
t->exponenttable[0] = 0;
49+
for (int i = 1; i < 31; i++)
50+
t->exponenttable[i] = i << 23;
51+
for (int i = 33; i < 63; i++)
52+
t->exponenttable[i] = 0x80000000UL + ((i - 32) << 23UL);
53+
t->exponenttable[31]= 0x47800000UL;
54+
t->exponenttable[32]= 0x80000000UL;
55+
t->exponenttable[63]= 0xC7800000UL;
56+
57+
t->offsettable[0] = 0;
58+
for (int i = 1; i < 64; i++)
59+
t->offsettable[i] = 1024;
60+
t->offsettable[31] = 2048;
61+
t->offsettable[32] = 0;
62+
t->offsettable[63] = 2048;
63+
}

0 commit comments

Comments
 (0)