1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-04 22:03:09 +02:00

avcodec/put_bits: Add and use put_bits63()

When using a 64bit PutBitContext (i.e. on x64), put_bits_no_assert()
can naturally write up to 63 bits. So one can avoid treating the
cases <32bits, 32 bits and <63 bits differently.

As it turns out, no user actually wants to write 64 bit at once
(maybe except testprograms).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt
2025-03-08 17:35:32 +01:00
parent 512e597932
commit ede2b391cc
6 changed files with 30 additions and 15 deletions

View File

@ -334,12 +334,12 @@ static inline void put_ue_coef(PutBitContext *pb, const AVDOVIRpuDataHeader *hdr
switch (hdr->coef_data_type) { switch (hdr->coef_data_type) {
case RPU_COEFF_FIXED: case RPU_COEFF_FIXED:
set_ue_golomb(pb, coef >> hdr->coef_log2_denom); set_ue_golomb(pb, coef >> hdr->coef_log2_denom);
put_bits64(pb, hdr->coef_log2_denom, put_bits63(pb, hdr->coef_log2_denom,
coef & ((1LL << hdr->coef_log2_denom) - 1)); coef & ((1LL << hdr->coef_log2_denom) - 1));
break; break;
case RPU_COEFF_FLOAT: case RPU_COEFF_FLOAT:
fpart.f32 = coef / (float) (1LL << hdr->coef_log2_denom); fpart.f32 = coef / (float) (1LL << hdr->coef_log2_denom);
put_bits64(pb, hdr->coef_log2_denom, fpart.u32); put_bits63(pb, hdr->coef_log2_denom, fpart.u32);
break; break;
} }
} }
@ -352,12 +352,12 @@ static inline void put_se_coef(PutBitContext *pb, const AVDOVIRpuDataHeader *hdr
switch (hdr->coef_data_type) { switch (hdr->coef_data_type) {
case RPU_COEFF_FIXED: case RPU_COEFF_FIXED:
set_se_golomb(pb, coef >> hdr->coef_log2_denom); set_se_golomb(pb, coef >> hdr->coef_log2_denom);
put_bits64(pb, hdr->coef_log2_denom, put_bits63(pb, hdr->coef_log2_denom,
coef & ((1LL << hdr->coef_log2_denom) - 1)); coef & ((1LL << hdr->coef_log2_denom) - 1));
break; break;
case RPU_COEFF_FLOAT: case RPU_COEFF_FLOAT:
fpart.f32 = coef / (float) (1LL << hdr->coef_log2_denom); fpart.f32 = coef / (float) (1LL << hdr->coef_log2_denom);
put_bits64(pb, hdr->coef_log2_denom, fpart.u32); put_bits63(pb, hdr->coef_log2_denom, fpart.u32);
break; break;
} }
} }

View File

@ -39,14 +39,14 @@
typedef uint64_t BitBuf; typedef uint64_t BitBuf;
#define AV_WBBUF AV_WB64 #define AV_WBBUF AV_WB64
#define AV_WLBUF AV_WL64 #define AV_WLBUF AV_WL64
#define BUF_BITS 64
#else #else
typedef uint32_t BitBuf; typedef uint32_t BitBuf;
#define AV_WBBUF AV_WB32 #define AV_WBBUF AV_WB32
#define AV_WLBUF AV_WL32 #define AV_WLBUF AV_WL32
#define BUF_BITS 32
#endif #endif
static const int BUF_BITS = 8 * sizeof(BitBuf);
typedef struct PutBitContext { typedef struct PutBitContext {
BitBuf bit_buf; BitBuf bit_buf;
int bit_left; int bit_left;
@ -329,12 +329,15 @@ static void av_unused put_bits32(PutBitContext *s, uint32_t value)
} }
/** /**
* Write up to 64 bits into a bitstream. * Write up to 63 bits into a bitstream.
*/ */
static inline void put_bits64(PutBitContext *s, int n, uint64_t value) static inline void put_bits63(PutBitContext *s, int n, uint64_t value)
{ {
av_assert2((n == 64) || (n < 64 && value < (UINT64_C(1) << n))); av_assert2(n < 64U && value < (UINT64_C(1) << n));
#if BUF_BITS >= 64
put_bits_no_assert(s, n, value);
#else
if (n < 32) if (n < 32)
put_bits(s, n, value); put_bits(s, n, value);
else if (n == 32) else if (n == 32)
@ -349,6 +352,19 @@ static inline void put_bits64(PutBitContext *s, int n, uint64_t value)
put_bits(s, n - 32, hi); put_bits(s, n - 32, hi);
put_bits32(s, lo); put_bits32(s, lo);
#endif #endif
}
#endif
}
/**
* Write up to 64 bits into a bitstream.
*/
static inline void put_bits64(PutBitContext *s, int n, uint64_t value)
{
av_assert2((n == 64) || (n < 64 && value < (UINT64_C(1) << n)));
if (n < 64) {
put_bits63(s, n, value);
} else { } else {
uint32_t lo = value & 0xffffffff; uint32_t lo = value & 0xffffffff;
uint32_t hi = value >> 32; uint32_t hi = value >> 32;
@ -359,7 +375,6 @@ static inline void put_bits64(PutBitContext *s, int n, uint64_t value)
put_bits32(s, hi); put_bits32(s, hi);
put_bits32(s, lo); put_bits32(s, lo);
#endif #endif
} }
} }
@ -367,7 +382,7 @@ static inline void put_sbits63(PutBitContext *pb, int n, int64_t value)
{ {
av_assert2(n >= 0 && n < 64); av_assert2(n >= 0 && n < 64);
put_bits64(pb, n, (uint64_t)(value) & (~(UINT64_MAX << n))); put_bits63(pb, n, (uint64_t)(value) & (~(UINT64_MAX << n)));
} }
/** /**

View File

@ -62,7 +62,7 @@ static inline void set_ue_golomb_long(PutBitContext *pb, uint32_t i)
put_bits(pb, ff_ue_golomb_len[i], i + 1); put_bits(pb, ff_ue_golomb_len[i], i + 1);
else { else {
int e = av_log2(i + 1); int e = av_log2(i + 1);
put_bits64(pb, 2 * e + 1, i + 1); put_bits63(pb, 2 * e + 1, i + 1);
} }
} }

View File

@ -201,7 +201,7 @@ static av_always_inline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val)
pbits |= 0x1; pbits |= 0x1;
} }
put_bits64(pb, bits*2 + 1, (pbits << 1) | 1); put_bits63(pb, 2 * bits + 1, (pbits << 1) | 1);
} }
static av_always_inline int count_vc2_ue_uint(uint32_t val) static av_always_inline int count_vc2_ue_uint(uint32_t val)

View File

@ -319,7 +319,7 @@ static int update_extradata(AVCodecParameters *codecpar)
return ret; return ret;
put_bits32(&pb, get_bits_long(&gb, 32)); // min/max blocksize put_bits32(&pb, get_bits_long(&gb, 32)); // min/max blocksize
put_bits64(&pb, 48, get_bits64(&gb, 48)); // min/max framesize put_bits63(&pb, 48, get_bits64(&gb, 48)); // min/max framesize
put_bits(&pb, 20, get_bits(&gb, 20)); // samplerate put_bits(&pb, 20, get_bits(&gb, 20)); // samplerate
skip_bits(&gb, 3); skip_bits(&gb, 3);
put_bits(&pb, 3, codecpar->ch_layout.nb_channels - 1); put_bits(&pb, 3, codecpar->ch_layout.nb_channels - 1);

View File

@ -59,7 +59,7 @@ static int update_extradata(IAMFCodecConfig *codec_config)
return ret; return ret;
put_bits32(&pb, get_bits_long(&gb, 32)); // min/max blocksize put_bits32(&pb, get_bits_long(&gb, 32)); // min/max blocksize
put_bits64(&pb, 48, get_bits64(&gb, 48)); // min/max framesize put_bits63(&pb, 48, get_bits64(&gb, 48)); // min/max framesize
put_bits(&pb, 20, get_bits(&gb, 20)); // samplerate put_bits(&pb, 20, get_bits(&gb, 20)); // samplerate
skip_bits(&gb, 3); skip_bits(&gb, 3);
put_bits(&pb, 3, 1); // set channels to stereo put_bits(&pb, 3, 1); // set channels to stereo