1
0
Fork 0

avcodec/cbs: Add specialization for ff_cbs_(read|write)_unsigned()

These functions allow not only to read and write unsigned values,
but also to check ranges and to emit trace output which can be
beautified when processing arrays (indices like "[i]" are replaced
by their actual numbers).

Yet lots of callers actually only need something simpler:
Their range is only implicitly restricted by the amount
of bits used and they are not part of arrays, hence don't
need this beautification.

This commit adds specializations for these callers;
this is very beneficial size-wise (it reduced the size
of .text by 23312 bytes here), as a call is now cheaper.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2022-07-01 11:44:36 +02:00
parent b85557b231
commit a105b11a9d
6 changed files with 97 additions and 20 deletions

View File

@ -546,10 +546,13 @@ void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
position, name, pad, bits, value);
}
int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
int width, const char *name,
const int *subscripts, uint32_t *write_to,
uint32_t range_min, uint32_t range_max)
static av_always_inline int cbs_read_unsigned(CodedBitstreamContext *ctx,
GetBitContext *gbc,
int width, const char *name,
const int *subscripts,
uint32_t *write_to,
uint32_t range_min,
uint32_t range_max)
{
uint32_t value;
int position;
@ -589,6 +592,22 @@ int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
return 0;
}
int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
int width, const char *name,
const int *subscripts, uint32_t *write_to,
uint32_t range_min, uint32_t range_max)
{
return cbs_read_unsigned(ctx, gbc, width, name, subscripts,
write_to, range_min, range_max);
}
int ff_cbs_read_simple_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
int width, const char *name, uint32_t *write_to)
{
return cbs_read_unsigned(ctx, gbc, width, name, NULL,
write_to, 0, UINT32_MAX);
}
int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
int width, const char *name,
const int *subscripts, uint32_t value,
@ -625,6 +644,13 @@ int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
return 0;
}
int ff_cbs_write_simple_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
int width, const char *name, uint32_t value)
{
return ff_cbs_write_unsigned(ctx, pbc, width, name, NULL,
value, 0, MAX_UINT_BITS(width));
}
int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
int width, const char *name,
const int *subscripts, int32_t *write_to,

View File

@ -412,9 +412,8 @@ static int cbs_av1_read_subexp(CodedBitstreamContext *ctx, GetBitContext *gbc,
}
if (len < max_len) {
err = ff_cbs_read_unsigned(ctx, gbc, range_bits,
"subexp_bits", NULL, &value,
0, MAX_UINT_BITS(range_bits));
err = ff_cbs_read_simple_unsigned(ctx, gbc, range_bits,
"subexp_bits", &value);
if (err < 0)
return err;
@ -476,10 +475,9 @@ static int cbs_av1_write_subexp(CodedBitstreamContext *ctx, PutBitContext *pbc,
return err;
if (len < max_len) {
err = ff_cbs_write_unsigned(ctx, pbc, range_bits,
"subexp_bits", NULL,
value - range_offset,
0, MAX_UINT_BITS(range_bits));
err = ff_cbs_write_simple_unsigned(ctx, pbc, range_bits,
"subexp_bits",
value - range_offset);
if (err < 0)
return err;
@ -546,8 +544,6 @@ static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
#define fb(width, name) \
xf(width, name, current->name, 0, MAX_UINT_BITS(width), 0, )
#define fc(width, name, range_min, range_max) \
xf(width, name, current->name, range_min, range_max, 0, )
#define flag(name) fb(1, name)
@ -573,6 +569,13 @@ static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
#define READWRITE read
#define RWContext GetBitContext
#define fb(width, name) do { \
uint32_t value; \
CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, \
#name, &value)); \
current->name = value; \
} while (0)
#define xf(width, name, var, range_min, range_max, subs, ...) do { \
uint32_t value; \
CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
@ -645,6 +648,7 @@ static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
#undef READ
#undef READWRITE
#undef RWContext
#undef fb
#undef xf
#undef xsu
#undef uvlc
@ -661,6 +665,11 @@ static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
#define READWRITE write
#define RWContext PutBitContext
#define fb(width, name) do { \
CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
current->name)); \
} while (0)
#define xf(width, name, var, range_min, range_max, subs, ...) do { \
CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
SUBSCRIPTS(subs, __VA_ARGS__), \
@ -723,6 +732,7 @@ static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
#undef WRITE
#undef READWRITE
#undef RWContext
#undef fb
#undef xf
#undef xsu
#undef uvlc

View File

@ -264,8 +264,6 @@ static int cbs_h265_payload_extension_present(GetBitContext *gbc, uint32_t paylo
#define u(width, name, range_min, range_max) \
xu(width, name, current->name, range_min, range_max, 0, )
#define ub(width, name) \
xu(width, name, current->name, 0, MAX_UINT_BITS(width), 0, )
#define flag(name) ub(1, name)
#define ue(name, range_min, range_max) \
xue(name, current->name, range_min, range_max, 0, )
@ -301,6 +299,12 @@ static int cbs_h265_payload_extension_present(GetBitContext *gbc, uint32_t paylo
#define READWRITE read
#define RWContext GetBitContext
#define ub(width, name) do { \
uint32_t value; \
CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, #name, \
&value)); \
current->name = value; \
} while (0)
#define xu(width, name, var, range_min, range_max, subs, ...) do { \
uint32_t value; \
CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
@ -379,6 +383,7 @@ static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
#undef READ
#undef READWRITE
#undef RWContext
#undef ub
#undef xu
#undef xi
#undef xue
@ -394,6 +399,11 @@ static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
#define READWRITE write
#define RWContext PutBitContext
#define ub(width, name) do { \
uint32_t value = current->name; \
CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
value)); \
} while (0)
#define xu(width, name, var, range_min, range_max, subs, ...) do { \
uint32_t value = var; \
CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
@ -461,6 +471,7 @@ static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
#undef WRITE
#undef READWRITE
#undef RWContext
#undef ub
#undef xu
#undef xi
#undef xue

View File

@ -163,18 +163,27 @@ void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
// Helper functions for read/write of common bitstream elements, including
// generation of trace output.
// generation of trace output. The simple functions are equivalent to
// their non-simple counterparts except that their range is unrestricted
// (i.e. only limited by the amount of bits used) and they lack
// the ability to use subscripts.
int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
int width, const char *name,
const int *subscripts, uint32_t *write_to,
uint32_t range_min, uint32_t range_max);
int ff_cbs_read_simple_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
int width, const char *name, uint32_t *write_to);
int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
int width, const char *name,
const int *subscripts, uint32_t value,
uint32_t range_min, uint32_t range_max);
int ff_cbs_write_simple_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
int width, const char *name, uint32_t value);
int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
int width, const char *name,
const int *subscripts, int32_t *write_to,

View File

@ -40,8 +40,6 @@
#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
#define ui(width, name) \
xui(width, name, current->name, 0, MAX_UINT_BITS(width), 0, )
#define uir(width, name) \
xui(width, name, current->name, 1, MAX_UINT_BITS(width), 0, )
#define uis(width, name, subs, ...) \
@ -65,6 +63,12 @@
#define READWRITE read
#define RWContext GetBitContext
#define ui(width, name) do { \
uint32_t value; \
CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, #name, \
&value)); \
current->name = value; \
} while (0)
#define xuia(width, string, var, range_min, range_max, subs, ...) do { \
uint32_t value; \
CHECK(ff_cbs_read_unsigned(ctx, rw, width, string, \
@ -95,6 +99,7 @@
#undef READ
#undef READWRITE
#undef RWContext
#undef ui
#undef xuia
#undef xsi
#undef nextbits
@ -105,6 +110,11 @@
#define READWRITE write
#define RWContext PutBitContext
#define ui(width, name) do { \
CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
current->name)); \
} while (0)
#define xuia(width, string, var, range_min, range_max, subs, ...) do { \
CHECK(ff_cbs_write_unsigned(ctx, rw, width, string, \
SUBSCRIPTS(subs, __VA_ARGS__), \
@ -134,6 +144,7 @@
#undef WRITE
#undef READWRITE
#undef RWContext
#undef ui
#undef xuia
#undef xsi
#undef nextbits

View File

@ -251,8 +251,6 @@ static int cbs_vp9_write_le(CodedBitstreamContext *ctx, PutBitContext *pbc,
#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
#define f(width, name) \
xf(width, name, current->name, 0, )
#define s(width, name) \
xs(width, name, current->name, 0, )
#define fs(width, name, subs, ...) \
@ -264,6 +262,12 @@ static int cbs_vp9_write_le(CodedBitstreamContext *ctx, PutBitContext *pbc,
#define READWRITE read
#define RWContext GetBitContext
#define f(width, name) do { \
uint32_t value; \
CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, #name, \
&value)); \
current->name = value; \
} while (0)
#define xf(width, name, var, subs, ...) do { \
uint32_t value; \
CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
@ -329,6 +333,7 @@ static int cbs_vp9_write_le(CodedBitstreamContext *ctx, PutBitContext *pbc,
#undef READ
#undef READWRITE
#undef RWContext
#undef f
#undef xf
#undef xs
#undef increment
@ -344,6 +349,10 @@ static int cbs_vp9_write_le(CodedBitstreamContext *ctx, PutBitContext *pbc,
#define READWRITE write
#define RWContext PutBitContext
#define f(width, name) do { \
CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
current->name)); \
} while (0)
#define xf(width, name, var, subs, ...) do { \
CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
SUBSCRIPTS(subs, __VA_ARGS__), \
@ -396,6 +405,7 @@ static int cbs_vp9_write_le(CodedBitstreamContext *ctx, PutBitContext *pbc,
#undef WRITE
#undef READWRITE
#undef RWContext
#undef f
#undef xf
#undef xs
#undef increment