You've already forked FFmpeg
mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-11-23 21:54:53 +02:00
cbs: Make tracing more general
Turn tracing into callbacks for each syntax element, with default callbacks to match current trace_headers behaviour for debug. Move the construction of bit strings into the trace callback, which simplifies all of the read and write functions. Signed-off-by: Fei Wang <fei.w.wang@intel.com> Reviewed-by: Neal Gompa <ngompa13@gmail.com>
This commit is contained in:
committed by
Haihao Xiang
parent
6c3a5d625f
commit
abe16daea1
121
libavcodec/cbs.c
121
libavcodec/cbs.c
@@ -117,8 +117,9 @@ av_cold int ff_cbs_init(CodedBitstreamContext **ctx_ptr,
|
||||
|
||||
ctx->decompose_unit_types = NULL;
|
||||
|
||||
ctx->trace_enable = 0;
|
||||
ctx->trace_level = AV_LOG_TRACE;
|
||||
ctx->trace_enable = 0;
|
||||
ctx->trace_level = AV_LOG_TRACE;
|
||||
ctx->trace_context = ctx;
|
||||
|
||||
*ctx_ptr = ctx;
|
||||
return 0;
|
||||
@@ -496,19 +497,27 @@ void ff_cbs_trace_header(CodedBitstreamContext *ctx,
|
||||
av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
|
||||
}
|
||||
|
||||
void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
|
||||
const char *str, const int *subscripts,
|
||||
const char *bits, int64_t value)
|
||||
void ff_cbs_trace_read_log(void *trace_context,
|
||||
GetBitContext *gbc, int length,
|
||||
const char *str, const int *subscripts,
|
||||
int64_t value)
|
||||
{
|
||||
CodedBitstreamContext *ctx = trace_context;
|
||||
char name[256];
|
||||
char bits[256];
|
||||
size_t name_len, bits_len;
|
||||
int pad, subs, i, j, k, n;
|
||||
|
||||
if (!ctx->trace_enable)
|
||||
return;
|
||||
int position;
|
||||
|
||||
av_assert0(value >= INT_MIN && value <= UINT32_MAX);
|
||||
|
||||
position = get_bits_count(gbc);
|
||||
|
||||
av_assert0(length < 256);
|
||||
for (i = 0; i < length; i++)
|
||||
bits[i] = get_bits1(gbc) ? '1' : '0';
|
||||
bits[length] = 0;
|
||||
|
||||
subs = subscripts ? subscripts[0] : 0;
|
||||
n = 0;
|
||||
for (i = j = 0; str[i];) {
|
||||
@@ -535,7 +544,7 @@ void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
|
||||
av_assert0(n == subs);
|
||||
|
||||
name_len = strlen(name);
|
||||
bits_len = strlen(bits);
|
||||
bits_len = length;
|
||||
|
||||
if (name_len + bits_len > 60)
|
||||
pad = bits_len + 2;
|
||||
@@ -546,6 +555,36 @@ void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
|
||||
position, name, pad, bits, value);
|
||||
}
|
||||
|
||||
void ff_cbs_trace_write_log(void *trace_context,
|
||||
PutBitContext *pbc, int length,
|
||||
const char *str, const int *subscripts,
|
||||
int64_t value)
|
||||
{
|
||||
CodedBitstreamContext *ctx = trace_context;
|
||||
|
||||
// Ensure that the syntax element is written to the output buffer,
|
||||
// make a GetBitContext pointed at the start position, then call the
|
||||
// read log function which can read the bits back to log them.
|
||||
|
||||
GetBitContext gbc;
|
||||
int position;
|
||||
|
||||
if (length > 0) {
|
||||
PutBitContext flush;
|
||||
flush = *pbc;
|
||||
flush_put_bits(&flush);
|
||||
}
|
||||
|
||||
position = put_bits_count(pbc);
|
||||
av_assert0(position >= length);
|
||||
|
||||
init_get_bits(&gbc, pbc->buf, position);
|
||||
|
||||
skip_bits_long(&gbc, position - length);
|
||||
|
||||
ff_cbs_trace_read_log(ctx, &gbc, length, str, subscripts, value);
|
||||
}
|
||||
|
||||
static av_always_inline int cbs_read_unsigned(CodedBitstreamContext *ctx,
|
||||
GetBitContext *gbc,
|
||||
int width, const char *name,
|
||||
@@ -555,7 +594,8 @@ static av_always_inline int cbs_read_unsigned(CodedBitstreamContext *ctx,
|
||||
uint32_t range_max)
|
||||
{
|
||||
uint32_t value;
|
||||
int position;
|
||||
|
||||
CBS_TRACE_READ_START();
|
||||
|
||||
av_assert0(width > 0 && width <= 32);
|
||||
|
||||
@@ -565,21 +605,9 @@ static av_always_inline int cbs_read_unsigned(CodedBitstreamContext *ctx,
|
||||
return AVERROR_INVALIDDATA;
|
||||
}
|
||||
|
||||
if (ctx->trace_enable)
|
||||
position = get_bits_count(gbc);
|
||||
|
||||
value = get_bits_long(gbc, width);
|
||||
|
||||
if (ctx->trace_enable) {
|
||||
char bits[33];
|
||||
int i;
|
||||
for (i = 0; i < width; i++)
|
||||
bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
|
||||
bits[i] = 0;
|
||||
|
||||
ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
|
||||
bits, value);
|
||||
}
|
||||
CBS_TRACE_READ_END();
|
||||
|
||||
if (value < range_min || value > range_max) {
|
||||
av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
|
||||
@@ -613,6 +641,8 @@ int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
|
||||
const int *subscripts, uint32_t value,
|
||||
uint32_t range_min, uint32_t range_max)
|
||||
{
|
||||
CBS_TRACE_WRITE_START();
|
||||
|
||||
av_assert0(width > 0 && width <= 32);
|
||||
|
||||
if (value < range_min || value > range_max) {
|
||||
@@ -625,22 +655,13 @@ int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
|
||||
if (put_bits_left(pbc) < width)
|
||||
return AVERROR(ENOSPC);
|
||||
|
||||
if (ctx->trace_enable) {
|
||||
char bits[33];
|
||||
int i;
|
||||
for (i = 0; i < width; i++)
|
||||
bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
|
||||
bits[i] = 0;
|
||||
|
||||
ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
|
||||
name, subscripts, bits, value);
|
||||
}
|
||||
|
||||
if (width < 32)
|
||||
put_bits(pbc, width, value);
|
||||
else
|
||||
put_bits32(pbc, value);
|
||||
|
||||
CBS_TRACE_WRITE_END();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -657,7 +678,8 @@ int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
|
||||
int32_t range_min, int32_t range_max)
|
||||
{
|
||||
int32_t value;
|
||||
int position;
|
||||
|
||||
CBS_TRACE_READ_START();
|
||||
|
||||
av_assert0(width > 0 && width <= 32);
|
||||
|
||||
@@ -667,21 +689,9 @@ int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
|
||||
return AVERROR_INVALIDDATA;
|
||||
}
|
||||
|
||||
if (ctx->trace_enable)
|
||||
position = get_bits_count(gbc);
|
||||
|
||||
value = get_sbits_long(gbc, width);
|
||||
|
||||
if (ctx->trace_enable) {
|
||||
char bits[33];
|
||||
int i;
|
||||
for (i = 0; i < width; i++)
|
||||
bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
|
||||
bits[i] = 0;
|
||||
|
||||
ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
|
||||
bits, value);
|
||||
}
|
||||
CBS_TRACE_READ_END();
|
||||
|
||||
if (value < range_min || value > range_max) {
|
||||
av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
|
||||
@@ -699,6 +709,8 @@ int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc,
|
||||
const int *subscripts, int32_t value,
|
||||
int32_t range_min, int32_t range_max)
|
||||
{
|
||||
CBS_TRACE_WRITE_START();
|
||||
|
||||
av_assert0(width > 0 && width <= 32);
|
||||
|
||||
if (value < range_min || value > range_max) {
|
||||
@@ -711,22 +723,13 @@ int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc,
|
||||
if (put_bits_left(pbc) < width)
|
||||
return AVERROR(ENOSPC);
|
||||
|
||||
if (ctx->trace_enable) {
|
||||
char bits[33];
|
||||
int i;
|
||||
for (i = 0; i < width; i++)
|
||||
bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
|
||||
bits[i] = 0;
|
||||
|
||||
ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
|
||||
name, subscripts, bits, value);
|
||||
}
|
||||
|
||||
if (width < 32)
|
||||
put_sbits(pbc, width, value);
|
||||
else
|
||||
put_bits32(pbc, value);
|
||||
|
||||
CBS_TRACE_WRITE_END();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user