[FFmpeg-devel] [PATCH v5 2/8] cbs: Make tracing more general

2023-09-11 Thread fei . w . wang-at-intel . com
From: Mark Thompson 

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 
---
 libavcodec/cbs.c   | 121 +--
 libavcodec/cbs.h   |  88 +-
 libavcodec/cbs_av1.c   | 206 +
 libavcodec/cbs_bsf.c   |   5 +
 libavcodec/cbs_h2645.c | 134 +
 libavcodec/cbs_internal.h  |  85 +-
 libavcodec/cbs_vp9.c   | 108 +
 libavcodec/trace_headers_bsf.c |   2 +
 8 files changed, 373 insertions(+), 376 deletions(-)

diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
index 3ec8285e21..daf7f66427 100644
--- a/libavcodec/cbs.c
+++ b/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, 
Put

Re: [FFmpeg-devel] [PATCH v5 2/8] cbs: Make tracing more general

2023-09-19 Thread Neal Gompa
On Mon, Sep 11, 2023 at 3:53 AM  wrote:
>
> From: Mark Thompson 
>
> 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 
> ---
>  libavcodec/cbs.c   | 121 +--
>  libavcodec/cbs.h   |  88 +-
>  libavcodec/cbs_av1.c   | 206 +
>  libavcodec/cbs_bsf.c   |   5 +
>  libavcodec/cbs_h2645.c | 134 +
>  libavcodec/cbs_internal.h  |  85 +-
>  libavcodec/cbs_vp9.c   | 108 +
>  libavcodec/trace_headers_bsf.c |   2 +
>  8 files changed, 373 insertions(+), 376 deletions(-)
>
> diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
> index 3ec8285e21..daf7f66427 100644
> --- a/libavcodec/cbs.c
> +++ b/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, pos