[FFmpeg-devel] [PATCH 3/6] cbs: Add support for array subscripts in trace output

Mark Thompson sw at jkqxz.net
Tue May 1 02:26:02 EEST 2018


This makes the trace output for arrays significantly nicer.
---
 libavcodec/cbs.c                       |  44 ++++++--
 libavcodec/cbs_h2645.c                 |  83 ++++++++-------
 libavcodec/cbs_h264_syntax_template.c  | 101 +++++++++----------
 libavcodec/cbs_h265_syntax_template.c  | 179 +++++++++++++++++----------------
 libavcodec/cbs_internal.h              |  10 +-
 libavcodec/cbs_mpeg2.c                 |  20 ++--
 libavcodec/cbs_mpeg2_syntax_template.c |  25 ++---
 7 files changed, 262 insertions(+), 200 deletions(-)

diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
index 0d6ffe8824..7091c9abf5 100644
--- a/libavcodec/cbs.c
+++ b/libavcodec/cbs.c
@@ -357,17 +357,43 @@ void ff_cbs_trace_header(CodedBitstreamContext *ctx,
 }
 
 void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
-                                 const char *name, const char *bits,
-                                 int64_t value)
+                                 const char *str, const int *subscripts,
+                                 const char *bits, int64_t value)
 {
+    char name[256];
     size_t name_len, bits_len;
-    int pad;
+    int pad, subs, i, j, k, n;
 
     if (!ctx->trace_enable)
         return;
 
     av_assert0(value >= INT_MIN && value <= UINT32_MAX);
 
+    subs = subscripts ? subscripts[0] : 0;
+    n = 0;
+    for (i = j = 0; str[i];) {
+        if (str[i] == '[') {
+            if (n < subs) {
+                ++n;
+                k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
+                av_assert0(k > 0 && j + k < sizeof(name));
+                j += k;
+                for (++i; str[i] && str[i] != ']'; i++);
+                av_assert0(str[i] == ']');
+            } else {
+                while (str[i] && str[i] != ']')
+                    name[j++] = str[i++];
+                av_assert0(str[i] == ']');
+            }
+        } else {
+            av_assert0(j + 1 < sizeof(name));
+            name[j++] = str[i++];
+        }
+    }
+    av_assert0(j + 1 < sizeof(name));
+    name[j] = 0;
+    av_assert0(n == subs);
+
     name_len = strlen(name);
     bits_len = strlen(bits);
 
@@ -381,7 +407,8 @@ void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
 }
 
 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
-                         int width, const char *name, uint32_t *write_to,
+                         int width, const char *name,
+                         const int *subscripts, uint32_t *write_to,
                          uint32_t range_min, uint32_t range_max)
 {
     uint32_t value;
@@ -407,7 +434,8 @@ int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
             bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
         bits[i] = 0;
 
-        ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
+        ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
+                                    bits, value);
     }
 
     if (value < range_min || value > range_max) {
@@ -422,7 +450,8 @@ int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
 }
 
 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
-                          int width, const char *name, uint32_t value,
+                          int width, const char *name,
+                          const int *subscripts, uint32_t value,
                           uint32_t range_min, uint32_t range_max)
 {
     av_assert0(width > 0 && width <= 32);
@@ -444,7 +473,8 @@ int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
             bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
         bits[i] = 0;
 
-        ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
+        ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
+                                    name, subscripts, bits, value);
     }
 
     if (width < 32)
diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index c243dc7da4..62bd0e5f29 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -32,7 +32,8 @@
 
 
 static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
-                              const char *name, uint32_t *write_to,
+                              const char *name, const int *subscripts,
+                              uint32_t *write_to,
                               uint32_t range_min, uint32_t range_max)
 {
     uint32_t value;
@@ -68,7 +69,8 @@ static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
     --value;
 
     if (ctx->trace_enable)
-        ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
+        ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
+                                    bits, value);
 
     if (value < range_min || value > range_max) {
         av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
@@ -82,7 +84,8 @@ static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
 }
 
 static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
-                              const char *name, int32_t *write_to,
+                              const char *name, const int *subscripts,
+                              int32_t *write_to,
                               int32_t range_min, int32_t range_max)
 {
     int32_t value;
@@ -122,7 +125,8 @@ static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
         value = v / 2;
 
     if (ctx->trace_enable)
-        ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
+        ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
+                                    bits, value);
 
     if (value < range_min || value > range_max) {
         av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
@@ -136,7 +140,8 @@ static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
 }
 
 static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
-                               const char *name, uint32_t value,
+                               const char *name, const int *subscripts,
+                               uint32_t value,
                                uint32_t range_min, uint32_t range_max)
 {
     int len;
@@ -164,7 +169,8 @@ static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
             bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
         bits[len + len + 1] = 0;
 
-        ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
+        ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
+                                    name, subscripts, bits, value);
     }
 
     put_bits(pbc, len, 0);
@@ -177,7 +183,8 @@ static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
 }
 
 static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
-                               const char *name, int32_t value,
+                               const char *name, const int *subscripts,
+                               int32_t value,
                                int32_t range_min, int32_t range_max)
 {
     int len;
@@ -213,7 +220,8 @@ static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
             bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
         bits[len + len + 1] = 0;
 
-        ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
+        ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
+                                    name, subscripts, bits, value);
     }
 
     put_bits(pbc, len, 0);
@@ -239,9 +247,28 @@ static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
 #define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
 #define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
 
+#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
+
+#define u(width, name, range_min, range_max) \
+        xu(width, name, current->name, range_min, range_max, 0)
+#define flag(name) u(1, name, 0, 1)
+#define ue(name, range_min, range_max) \
+        xue(name, current->name, range_min, range_max, 0)
+#define se(name, range_min, range_max) \
+        xse(name, current->name, range_min, range_max, 0)
+
+#define us(width, name, range_min, range_max, subs, ...) \
+        xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
+#define flags(name, subs, ...) \
+        xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
+#define ues(name, range_min, range_max, subs, ...) \
+        xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
+#define ses(name, range_min, range_max, subs, ...) \
+        xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
+
 #define fixed(width, name, value) do { \
         av_unused uint32_t fixed_value = value; \
-        xu(width, name, fixed_value, value, value); \
+        xu(width, name, fixed_value, value, value, 0); \
     } while (0)
 
 
@@ -249,34 +276,29 @@ static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
 #define READWRITE read
 #define RWContext GetBitContext
 
-#define xu(width, name, var, range_min, range_max) do { \
+#define xu(width, name, var, range_min, range_max, subs, ...) do { \
         uint32_t value = range_min; \
         CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
+                                   SUBSCRIPTS(subs, __VA_ARGS__), \
                                    &value, range_min, range_max)); \
         var = value; \
     } while (0)
-#define xue(name, var, range_min, range_max) do { \
+#define xue(name, var, range_min, range_max, subs, ...) do { \
         uint32_t value = range_min; \
         CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
+                                 SUBSCRIPTS(subs, __VA_ARGS__), \
                                  &value, range_min, range_max)); \
         var = value; \
     } while (0)
-#define xse(name, var, range_min, range_max) do { \
+#define xse(name, var, range_min, range_max, subs, ...) do { \
         int32_t value = range_min; \
         CHECK(cbs_read_se_golomb(ctx, rw, #name, \
+                                 SUBSCRIPTS(subs, __VA_ARGS__), \
                                  &value, range_min, range_max)); \
         var = value; \
     } while (0)
 
 
-#define u(width, name, range_min, range_max) \
-        xu(width, name, current->name, range_min, range_max)
-#define flag(name) u(1, name, 0, 1)
-#define ue(name, range_min, range_max) \
-        xue(name, current->name, range_min, range_max)
-#define se(name, range_min, range_max) \
-        xse(name, current->name, range_min, range_max)
-
 #define infer(name, value) do { \
         current->name = value; \
     } while (0)
@@ -316,10 +338,6 @@ static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
 #undef xu
 #undef xue
 #undef xse
-#undef u
-#undef flag
-#undef ue
-#undef se
 #undef infer
 #undef more_rbsp_data
 #undef byte_alignment
@@ -330,30 +348,25 @@ static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
 #define READWRITE write
 #define RWContext PutBitContext
 
-#define xu(width, name, var, range_min, range_max) do { \
+#define xu(width, name, var, range_min, range_max, subs, ...) do { \
         uint32_t value = var; \
         CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
+                                    SUBSCRIPTS(subs, __VA_ARGS__), \
                                     value, range_min, range_max)); \
     } while (0)
-#define xue(name, var, range_min, range_max) do { \
+#define xue(name, var, range_min, range_max, subs, ...) do { \
         uint32_t value = var; \
         CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
+                                  SUBSCRIPTS(subs, __VA_ARGS__), \
                                   value, range_min, range_max)); \
     } while (0)
-#define xse(name, var, range_min, range_max) do { \
+#define xse(name, var, range_min, range_max, subs, ...) do { \
         int32_t value = var; \
         CHECK(cbs_write_se_golomb(ctx, rw, #name, \
+                                  SUBSCRIPTS(subs, __VA_ARGS__), \
                                   value, range_min, range_max)); \
     } while (0)
 
-#define u(width, name, range_min, range_max) \
-        xu(width, name, current->name, range_min, range_max)
-#define flag(name) u(1, name, 0, 1)
-#define ue(name, range_min, range_max) \
-        xue(name, current->name, range_min, range_max)
-#define se(name, range_min, range_max) \
-        xse(name, current->name, range_min, range_max)
-
 #define infer(name, value) do { \
         if (current->name != (value)) { \
             av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
diff --git a/libavcodec/cbs_h264_syntax_template.c b/libavcodec/cbs_h264_syntax_template.c
index 7c507b9a13..c3327f6e90 100644
--- a/libavcodec/cbs_h264_syntax_template.c
+++ b/libavcodec/cbs_h264_syntax_template.c
@@ -76,7 +76,7 @@ static int FUNC(scaling_list)(CodedBitstreamContext *ctx, RWContext *rw,
 
     scale = 8;
     for (i = 0; i < size_of_scaling_list; i++) {
-        xse(delta_scale, current->delta_scale[i], -128, +127);
+        ses(delta_scale[i], -128, +127, 1, i);
         scale = (scale + current->delta_scale[i] + 256) % 256;
         if (scale == 0)
             break;
@@ -95,9 +95,9 @@ static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
     u(4, cpb_size_scale, 0, 15);
 
     for (i = 0; i <= current->cpb_cnt_minus1; i++) {
-        ue(bit_rate_value_minus1[i], 0, UINT32_MAX - 1);
-        ue(cpb_size_value_minus1[i], 0, UINT32_MAX - 1);
-        flag(cbr_flag[i]);
+        ues(bit_rate_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
+        ues(cpb_size_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
+        flags(cbr_flag[i], 1, i);
     }
 
     u(5, initial_cpb_removal_delay_length_minus1, 0, 31);
@@ -256,7 +256,7 @@ static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
         flag(seq_scaling_matrix_present_flag);
         if (current->seq_scaling_matrix_present_flag) {
             for (i = 0; i < ((current->chroma_format_idc != 3) ? 8 : 12); i++) {
-                flag(seq_scaling_list_present_flag[i]);
+                flags(seq_scaling_list_present_flag[i], 1, i);
                 if (current->seq_scaling_list_present_flag[i]) {
                     if (i < 6)
                         CHECK(FUNC(scaling_list)(ctx, rw,
@@ -289,7 +289,7 @@ static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
         ue(num_ref_frames_in_pic_order_cnt_cycle, 0, 255);
 
         for (i = 0; i < current->num_ref_frames_in_pic_order_cnt_cycle; i++)
-            se(offset_for_ref_frame[i], INT32_MIN + 1, INT32_MAX);
+            ses(offset_for_ref_frame[i], INT32_MIN + 1, INT32_MAX, 1, i);
     }
 
     ue(max_num_ref_frames, 0, H264_MAX_DPB_FRAMES);
@@ -390,12 +390,13 @@ static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
 
         if (current->slice_group_map_type == 0) {
             for (iGroup = 0; iGroup <= current->num_slice_groups_minus1; iGroup++)
-                ue(run_length_minus1[iGroup], 0, pic_size - 1);
+                ues(run_length_minus1[iGroup], 0, pic_size - 1, 1, iGroup);
 
         } else if (current->slice_group_map_type == 2) {
             for (iGroup = 0; iGroup < current->num_slice_groups_minus1; iGroup++) {
-                ue(top_left[iGroup],     0,                         pic_size - 1);
-                ue(bottom_right[iGroup], current->top_left[iGroup], pic_size - 1);
+                ues(top_left[iGroup],       0, pic_size - 1, 1, iGroup);
+                ues(bottom_right[iGroup],
+                    current->top_left[iGroup], pic_size - 1, 1, iGroup);
             }
         } else if (current->slice_group_map_type == 3 ||
                    current->slice_group_map_type == 4 ||
@@ -408,8 +409,8 @@ static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
             allocate(current->slice_group_id,
                      current->pic_size_in_map_units_minus1 + 1);
             for (i = 0; i <= current->pic_size_in_map_units_minus1; i++)
-                u(av_log2(2 * current->num_slice_groups_minus1 + 1),
-                  slice_group_id[i], 0, current->num_slice_groups_minus1);
+                us(av_log2(2 * current->num_slice_groups_minus1 + 1),
+                   slice_group_id[i], 0, current->num_slice_groups_minus1, 1, i);
         }
     }
 
@@ -435,7 +436,7 @@ static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
         if (current->pic_scaling_matrix_present_flag) {
             for (i = 0; i < 6 + (((sps->chroma_format_idc != 3) ? 2 : 6) *
                                  current->transform_8x8_mode_flag); i++) {
-                flag(pic_scaling_list_present_flag[i]);
+                flags(pic_scaling_list_present_flag[i], 1, i);
                 if (current->pic_scaling_list_present_flag[i]) {
                     if (i < 6)
                         CHECK(FUNC(scaling_list)(ctx, rw,
@@ -483,10 +484,10 @@ static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
             length = sps->vui.nal_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
             xu(length, initial_cpb_removal_delay[SchedSelIdx],
                current->nal.initial_cpb_removal_delay[i],
-               1, MAX_UINT_BITS(length));
+               1, MAX_UINT_BITS(length), 1, i);
             xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
                current->nal.initial_cpb_removal_delay_offset[i],
-               0, MAX_UINT_BITS(length));
+               0, MAX_UINT_BITS(length), 1, i);
         }
     }
 
@@ -495,10 +496,10 @@ static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
             length = sps->vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
             xu(length, initial_cpb_removal_delay[SchedSelIdx],
                current->vcl.initial_cpb_removal_delay[i],
-               1, MAX_UINT_BITS(length));
+               1, MAX_UINT_BITS(length), 1, i);
             xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
                current->vcl.initial_cpb_removal_delay_offset[i],
-               0, MAX_UINT_BITS(length));
+               0, MAX_UINT_BITS(length), 1, i);
         }
     }
 
@@ -616,7 +617,7 @@ static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
             return AVERROR_INVALIDDATA;
 
         for (i = 0; i < num_clock_ts[current->pic_struct]; i++) {
-            flag(clock_timestamp_flag[i]);
+            flags(clock_timestamp_flag[i], 1, i);
             if (current->clock_timestamp_flag[i])
                 CHECK(FUNC(sei_pic_timestamp)(ctx, rw, &current->timestamp[i]));
         }
@@ -652,7 +653,7 @@ static int FUNC(sei_user_data_registered)(CodedBitstreamContext *ctx, RWContext
 
     allocate(current->data, current->data_length);
     for (j = 0; j < current->data_length; j++)
-        xu(8, itu_t_t35_payload_byte, current->data[j], 0x00, 0xff);
+        xu(8, itu_t_t35_payload_byte[i], current->data[j], 0x00, 0xff, 1, i + j);
 
     return 0;
 }
@@ -674,15 +675,13 @@ static int FUNC(sei_user_data_unregistered)(CodedBitstreamContext *ctx, RWContex
     *payload_size = 16 + current->data_length;
 #endif
 
-    for (i = 0; i < 16; i++) {
-        xu(8, uuid_iso_iec_11578,
-           current->uuid_iso_iec_11578[i], 0x00, 0xff);
-    }
+    for (i = 0; i < 16; i++)
+        us(8, uuid_iso_iec_11578[i], 0x00, 0xff, 1, i);
 
     allocate(current->data, current->data_length);
 
     for (i = 0; i < current->data_length; i++)
-        xu(8, user_data_payload_byte, current->data[i], 0x00, 0xff);
+        xu(8, user_data_payload_byte[i], current->data[i], 0x00, 0xff, 1, i);
 
     return 0;
 }
@@ -764,7 +763,7 @@ static int FUNC(sei_payload)(CodedBitstreamContext *ctx, RWContext *rw,
         {
             allocate(current->payload.other.data, current->payload_size);
             for (i = 0; i < current->payload_size; i++)
-                xu(8, payload_byte, current->payload.other.data[i], 0, 255);
+                xu(8, payload_byte, current->payload.other.data[i], 0, 255, 1, i);
         }
     }
 
@@ -811,14 +810,14 @@ static int FUNC(sei)(CodedBitstreamContext *ctx, RWContext *rw,
             fixed(8, ff_byte, 0xff);
             payload_type += 255;
         }
-        xu(8, last_payload_type_byte, tmp, 0, 254);
+        xu(8, last_payload_type_byte, tmp, 0, 254, 0);
         payload_type += tmp;
 
         while (show_bits(rw, 8) == 0xff) {
             fixed(8, ff_byte, 0xff);
             payload_size += 255;
         }
-        xu(8, last_payload_size_byte, tmp, 0, 254);
+        xu(8, last_payload_size_byte, tmp, 0, 254, 0);
         payload_size += tmp;
 
         current->payload[k].payload_type = payload_type;
@@ -854,14 +853,14 @@ static int FUNC(sei)(CodedBitstreamContext *ctx, RWContext *rw,
                 fixed(8, ff_byte, 0xff);
                 tmp -= 255;
             }
-            xu(8, last_payload_type_byte, tmp, 0, 254);
+            xu(8, last_payload_type_byte, tmp, 0, 254, 0);
 
             tmp = current->payload[k].payload_size;
             while (tmp >= 255) {
                 fixed(8, ff_byte, 0xff);
                 tmp -= 255;
             }
-            xu(8, last_payload_size_byte, tmp, 0, 254);
+            xu(8, last_payload_size_byte, tmp, 0, 254, 0);
 
             CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
         }
@@ -903,7 +902,7 @@ static int FUNC(ref_pic_list_modification)(CodedBitstreamContext *ctx, RWContext
         if (current->ref_pic_list_modification_flag_l0) {
             for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
                 xue(modification_of_pic_nums_idc,
-                    current->rplm_l0[i].modification_of_pic_nums_idc, 0, 3);
+                    current->rplm_l0[i].modification_of_pic_nums_idc, 0, 3, 0);
 
                 mopn = current->rplm_l0[i].modification_of_pic_nums_idc;
                 if (mopn == 3)
@@ -913,11 +912,11 @@ static int FUNC(ref_pic_list_modification)(CodedBitstreamContext *ctx, RWContext
                     xue(abs_diff_pic_num_minus1,
                         current->rplm_l0[i].abs_diff_pic_num_minus1,
                         0, (1 + current->field_pic_flag) *
-                        (1 << (sps->log2_max_frame_num_minus4 + 4)));
+                        (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
                 else if (mopn == 2)
                     xue(long_term_pic_num,
                         current->rplm_l0[i].long_term_pic_num,
-                        0, sps->max_num_ref_frames - 1);
+                        0, sps->max_num_ref_frames - 1, 0);
             }
         }
     }
@@ -927,7 +926,7 @@ static int FUNC(ref_pic_list_modification)(CodedBitstreamContext *ctx, RWContext
         if (current->ref_pic_list_modification_flag_l1) {
             for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
                 xue(modification_of_pic_nums_idc,
-                    current->rplm_l1[i].modification_of_pic_nums_idc, 0, 3);
+                    current->rplm_l1[i].modification_of_pic_nums_idc, 0, 3, 0);
 
                 mopn = current->rplm_l1[i].modification_of_pic_nums_idc;
                 if (mopn == 3)
@@ -937,11 +936,11 @@ static int FUNC(ref_pic_list_modification)(CodedBitstreamContext *ctx, RWContext
                     xue(abs_diff_pic_num_minus1,
                         current->rplm_l1[i].abs_diff_pic_num_minus1,
                         0, (1 + current->field_pic_flag) *
-                        (1 << (sps->log2_max_frame_num_minus4 + 4)));
+                        (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
                 else if (mopn == 2)
                     xue(long_term_pic_num,
                         current->rplm_l1[i].long_term_pic_num,
-                        0, sps->max_num_ref_frames - 1);
+                        0, sps->max_num_ref_frames - 1, 0);
             }
         }
     }
@@ -964,17 +963,17 @@ static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
         ue(chroma_log2_weight_denom, 0, 7);
 
     for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
-        flag(luma_weight_l0_flag[i]);
+        flags(luma_weight_l0_flag[i], 1, i);
         if (current->luma_weight_l0_flag[i]) {
-            se(luma_weight_l0[i], -128, +127);
-            se(luma_offset_l0[i], -128, +127);
+            ses(luma_weight_l0[i], -128, +127, 1, i);
+            ses(luma_offset_l0[i], -128, +127, 1, i);
         }
         if (chroma) {
-            flag(chroma_weight_l0_flag[i]);
+            flags(chroma_weight_l0_flag[i], 1, i);
             if (current->chroma_weight_l0_flag[i]) {
                 for (j = 0; j < 2; j++) {
-                    se(chroma_weight_l0[i][j], -128, +127);
-                    se(chroma_offset_l0[i][j], -128, +127);
+                    ses(chroma_weight_l0[i][j], -128, +127, 2, i, j);
+                    ses(chroma_offset_l0[i][j], -128, +127, 2, i, j);
                 }
             }
         }
@@ -982,17 +981,17 @@ static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
 
     if (current->slice_type % 5 == 1) {
         for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
-            flag(luma_weight_l1_flag[i]);
+            flags(luma_weight_l1_flag[i], 1, i);
             if (current->luma_weight_l1_flag[i]) {
-                se(luma_weight_l1[i], -128, +127);
-                se(luma_offset_l1[i], -128, +127);
+                ses(luma_weight_l1[i], -128, +127, 1, i);
+                ses(luma_offset_l1[i], -128, +127, 1, i);
             }
             if (chroma) {
-                flag(chroma_weight_l1_flag[i]);
+                flags(chroma_weight_l1_flag[i], 1, i);
                 if (current->chroma_weight_l1_flag[i]) {
                     for (j = 0; j < 2; j++) {
-                        se(chroma_weight_l1[i][j], -128, +127);
-                        se(chroma_offset_l1[i][j], -128, +127);
+                        ses(chroma_weight_l1[i][j], -128, +127, 2, i, j);
+                        ses(chroma_offset_l1[i][j], -128, +127, 2, i, j);
                     }
                 }
             }
@@ -1019,7 +1018,7 @@ static int FUNC(dec_ref_pic_marking)(CodedBitstreamContext *ctx, RWContext *rw,
             for (i = 0; i < H264_MAX_MMCO_COUNT; i++) {
                 xue(memory_management_control_operation,
                     current->mmco[i].memory_management_control_operation,
-                    0, 6);
+                    0, 6, 0);
 
                 mmco = current->mmco[i].memory_management_control_operation;
                 if (mmco == 0)
@@ -1028,19 +1027,19 @@ static int FUNC(dec_ref_pic_marking)(CodedBitstreamContext *ctx, RWContext *rw,
                 if (mmco == 1 || mmco == 3)
                     xue(difference_of_pic_nums_minus1,
                         current->mmco[i].difference_of_pic_nums_minus1,
-                        0, INT32_MAX);
+                        0, INT32_MAX, 0);
                 if (mmco == 2)
                     xue(long_term_pic_num,
                         current->mmco[i].long_term_pic_num,
-                        0, sps->max_num_ref_frames - 1);
+                        0, sps->max_num_ref_frames - 1, 0);
                 if (mmco == 3 || mmco == 6)
                     xue(long_term_frame_idx,
                         current->mmco[i].long_term_frame_idx,
-                        0, sps->max_num_ref_frames - 1);
+                        0, sps->max_num_ref_frames - 1, 0);
                 if (mmco == 4)
                     xue(max_long_term_frame_idx_plus1,
                         current->mmco[i].max_long_term_frame_idx_plus1,
-                        0, sps->max_num_ref_frames);
+                        0, sps->max_num_ref_frames, 0);
             }
             if (i == H264_MAX_MMCO_COUNT) {
                 av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many "
diff --git a/libavcodec/cbs_h265_syntax_template.c b/libavcodec/cbs_h265_syntax_template.c
index 58a79f3014..9f13061f38 100644
--- a/libavcodec/cbs_h265_syntax_template.c
+++ b/libavcodec/cbs_h265_syntax_template.c
@@ -74,13 +74,13 @@ static int FUNC(extension_data)(CodedBitstreamContext *ctx, RWContext *rw,
         *rw = start;
         allocate(current->data, (current->bit_length + 7) / 8);
         for (k = 0; k < current->bit_length; k++) {
-            xu(1, extension_data, bit, 0, 1);
+            xu(1, extension_data, bit, 0, 1, 0);
             current->data[k / 8] |= bit << (7 - k % 8);
         }
     }
 #else
     for (k = 0; k < current->bit_length; k++)
-        xu(1, extension_data, current->data[k / 8] >> (7 - k % 8), 0, 1);
+        xu(1, extension_data, current->data[k / 8] >> (7 - k % 8), 0, 1, 0);
 #endif
     return 0;
 }
@@ -98,7 +98,7 @@ static int FUNC(profile_tier_level)(CodedBitstreamContext *ctx, RWContext *rw,
         u(5, general_profile_idc, 0, 31);
 
         for (j = 0; j < 32; j++)
-            flag(general_profile_compatibility_flag[j]);
+            flags(general_profile_compatibility_flag[j], 1, j);
 
         flag(general_progressive_source_flag);
         flag(general_interlaced_source_flag);
@@ -148,8 +148,8 @@ static int FUNC(profile_tier_level)(CodedBitstreamContext *ctx, RWContext *rw,
     u(8, general_level_idc, 0, 255);
 
     for (i = 0; i < max_num_sub_layers_minus1; i++) {
-        flag(sub_layer_profile_present_flag[i]);
-        flag(sub_layer_level_present_flag[i]);
+        flags(sub_layer_profile_present_flag[i], 1, i);
+        flags(sub_layer_level_present_flag[i],   1, i);
     }
 
     if (max_num_sub_layers_minus1 > 0) {
@@ -180,13 +180,13 @@ static int FUNC(sub_layer_hrd_parameters)(CodedBitstreamContext *ctx, RWContext
         current = &hrd->vcl_sub_layer_hrd_parameters[sub_layer_id];
 
     for (i = 0; i <= hrd->cpb_cnt_minus1[sub_layer_id]; i++) {
-        ue(bit_rate_value_minus1[i], 0, UINT32_MAX - 1);
-        ue(cpb_size_value_minus1[i], 0, UINT32_MAX - 1);
+        ues(bit_rate_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
+        ues(cpb_size_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
         if (hrd->sub_pic_hrd_params_present_flag) {
-            ue(cpb_size_du_value_minus1[i], 0, UINT32_MAX - 1);
-            ue(bit_rate_du_value_minus1[i], 0, UINT32_MAX - 1);
+            ues(cpb_size_du_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
+            ues(bit_rate_du_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
         }
-        flag(cbr_flag[i]);
+        flags(cbr_flag[i], 1, i);
     }
 
     return 0;
@@ -230,21 +230,21 @@ static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
     }
 
     for (i = 0; i <= max_num_sub_layers_minus1; i++) {
-        flag(fixed_pic_rate_general_flag[i]);
+        flags(fixed_pic_rate_general_flag[i], 1, i);
 
         if (!current->fixed_pic_rate_general_flag[i])
-            flag(fixed_pic_rate_within_cvs_flag[i]);
+            flags(fixed_pic_rate_within_cvs_flag[i], 1, i);
         else
             infer(fixed_pic_rate_within_cvs_flag[i], 1);
 
         if (current->fixed_pic_rate_within_cvs_flag[i]) {
-            ue(elemental_duration_in_tc_minus1[i], 0, 2047);
+            ues(elemental_duration_in_tc_minus1[i], 0, 2047, 1, i);
             infer(low_delay_hrd_flag[i], 0);
         } else
-            flag(low_delay_hrd_flag[i]);
+            flags(low_delay_hrd_flag[i], 1, i);
 
         if (!current->low_delay_hrd_flag[i])
-            ue(cpb_cnt_minus1[i], 0, 31);
+            ues(cpb_cnt_minus1[i], 0, 31, 1, i);
         else
             infer(cpb_cnt_minus1[i], 0);
 
@@ -392,9 +392,12 @@ static int FUNC(vps)(CodedBitstreamContext *ctx, RWContext *rw,
     for (i = (current->vps_sub_layer_ordering_info_present_flag ?
               0 : current->vps_max_sub_layers_minus1);
          i <= current->vps_max_sub_layers_minus1; i++) {
-        ue(vps_max_dec_pic_buffering_minus1[i], 0, HEVC_MAX_DPB_SIZE - 1);
-        ue(vps_max_num_reorder_pics[i],         0, current->vps_max_dec_pic_buffering_minus1[i]);
-        ue(vps_max_latency_increase_plus1[i],   0, UINT32_MAX - 1);
+        ues(vps_max_dec_pic_buffering_minus1[i],
+            0, HEVC_MAX_DPB_SIZE - 1,                        1, i);
+        ues(vps_max_num_reorder_pics[i],
+            0, current->vps_max_dec_pic_buffering_minus1[i], 1, i);
+        ues(vps_max_latency_increase_plus1[i],
+            0, UINT32_MAX - 1,                               1, i);
     }
     if (!current->vps_sub_layer_ordering_info_present_flag) {
         for (i = 0; i < current->vps_max_sub_layers_minus1; i++) {
@@ -411,7 +414,7 @@ static int FUNC(vps)(CodedBitstreamContext *ctx, RWContext *rw,
     ue(vps_num_layer_sets_minus1, 0, HEVC_MAX_LAYER_SETS - 1);
     for (i = 1; i <= current->vps_num_layer_sets_minus1; i++) {
         for (j = 0; j <= current->vps_max_layer_id; j++)
-            flag(layer_id_included_flag[i][j]);
+            flags(layer_id_included_flag[i][j], 2, i, j);
     }
     for (j = 0; j <= current->vps_max_layer_id; j++)
         infer(layer_id_included_flag[0][j], j == 0);
@@ -425,11 +428,11 @@ static int FUNC(vps)(CodedBitstreamContext *ctx, RWContext *rw,
             ue(vps_num_ticks_poc_diff_one_minus1, 0, UINT32_MAX - 1);
         ue(vps_num_hrd_parameters, 0, current->vps_num_layer_sets_minus1 + 1);
         for (i = 0; i < current->vps_num_hrd_parameters; i++) {
-            ue(hrd_layer_set_idx[i],
-               current->vps_base_layer_internal_flag ? 0 : 1,
-               current->vps_num_layer_sets_minus1);
+            ues(hrd_layer_set_idx[i],
+                current->vps_base_layer_internal_flag ? 0 : 1,
+                current->vps_num_layer_sets_minus1, 1, i);
             if (i > 0)
-                flag(cprms_present_flag[i]);
+                flags(cprms_present_flag[i], 1, i);
             else
                 infer(cprms_present_flag[0], 1);
 
@@ -483,9 +486,9 @@ static int FUNC(st_ref_pic_set)(CodedBitstreamContext *ctx, RWContext *rw,
             (current->abs_delta_rps_minus1 + 1);
 
         for (j = 0; j <= num_delta_pocs; j++) {
-            flag(used_by_curr_pic_flag[j]);
+            flags(used_by_curr_pic_flag[j], 1, j);
             if (!current->used_by_curr_pic_flag[j])
-                flag(use_delta_flag[j]);
+                flags(use_delta_flag[j], 1, j);
             else
                 infer(use_delta_flag[j], 1);
         }
@@ -580,13 +583,13 @@ static int FUNC(st_ref_pic_set)(CodedBitstreamContext *ctx, RWContext *rw,
         ue(num_positive_pics, 0, 15 - current->num_negative_pics);
 
         for (i = 0; i < current->num_negative_pics; i++) {
-            ue(delta_poc_s0_minus1[i], 0, INT16_MAX);
-            flag(used_by_curr_pic_s0_flag[i]);
+            ues(delta_poc_s0_minus1[i], 0, INT16_MAX, 1, i);
+            flags(used_by_curr_pic_s0_flag[i],        1, i);
         }
 
         for (i = 0; i < current->num_positive_pics; i++) {
-            ue(delta_poc_s1_minus1[i], 0, INT16_MAX);
-            flag(used_by_curr_pic_s1_flag[i]);
+            ues(delta_poc_s1_minus1[i], 0, INT16_MAX, 1, i);
+            flags(used_by_curr_pic_s1_flag[i],        1, i);
         }
     }
 
@@ -601,18 +604,21 @@ static int FUNC(scaling_list_data)(CodedBitstreamContext *ctx, RWContext *rw,
 
     for (sizeId = 0; sizeId < 4; sizeId++) {
         for (matrixId = 0; matrixId < 6; matrixId += (sizeId == 3 ? 3 : 1)) {
-            flag(scaling_list_pred_mode_flag[sizeId][matrixId]);
+            flags(scaling_list_pred_mode_flag[sizeId][matrixId],
+                  2, sizeId, matrixId);
             if (!current->scaling_list_pred_mode_flag[sizeId][matrixId]) {
-                ue(scaling_list_pred_matrix_id_delta[sizeId][matrixId],
-                   0, sizeId == 3 ? matrixId / 3 : matrixId);
+                ues(scaling_list_pred_matrix_id_delta[sizeId][matrixId],
+                    0, sizeId == 3 ? matrixId / 3 : matrixId,
+                    2, sizeId, matrixId);
             } else {
                 n = FFMIN(64, 1 << (4 + (sizeId << 1)));
-                if (sizeId > 1)
-                    se(scaling_list_dc_coef_minus8[sizeId - 2][matrixId], -7, +247);
+                if (sizeId > 1) {
+                    ses(scaling_list_dc_coef_minus8[sizeId - 2][matrixId], -7, +247,
+                        2, sizeId - 2, matrixId);
+                }
                 for (i = 0; i < n; i++) {
-                    xse(scaling_list_delta_coeff,
-                        current->scaling_list_delta_coeff[sizeId][matrixId][i],
-                        -128, +127);
+                    ses(scaling_list_delta_coeff[sizeId][matrixId][i],
+                        -128, +127, 3, sizeId, matrixId, i);
                 }
             }
         }
@@ -658,8 +664,8 @@ static int FUNC(sps_scc_extension)(CodedBitstreamContext *ctx, RWContext *rw,
                 int bit_depth = comp == 0 ? current->bit_depth_luma_minus8 + 8
                                           : current->bit_depth_chroma_minus8 + 8;
                 for (i = 0; i <= current->sps_num_palette_predictor_initializer_minus1; i++)
-                    u(bit_depth, sps_palette_predictor_initializers[comp][i],
-                      0, MAX_UINT_BITS(bit_depth));
+                    us(bit_depth, sps_palette_predictor_initializers[comp][i],
+                       0, MAX_UINT_BITS(bit_depth), 2, comp, i);
             }
         }
     }
@@ -742,9 +748,12 @@ static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
     for (i = (current->sps_sub_layer_ordering_info_present_flag ?
               0 : current->sps_max_sub_layers_minus1);
          i <= current->sps_max_sub_layers_minus1; i++) {
-        ue(sps_max_dec_pic_buffering_minus1[i], 0, HEVC_MAX_DPB_SIZE - 1);
-        ue(sps_max_num_reorder_pics[i],         0, current->sps_max_dec_pic_buffering_minus1[i]);
-        ue(sps_max_latency_increase_plus1[i],   0, UINT32_MAX - 1);
+        ues(sps_max_dec_pic_buffering_minus1[i],
+            0, HEVC_MAX_DPB_SIZE - 1,                        1, i);
+        ues(sps_max_num_reorder_pics[i],
+            0, current->sps_max_dec_pic_buffering_minus1[i], 1, i);
+        ues(sps_max_latency_increase_plus1[i],
+            0, UINT32_MAX - 1,                               1, i);
     }
     if (!current->sps_sub_layer_ordering_info_present_flag) {
         for (i = 0; i < current->sps_max_sub_layers_minus1; i++) {
@@ -819,10 +828,10 @@ static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
     if (current->long_term_ref_pics_present_flag) {
         ue(num_long_term_ref_pics_sps, 0, HEVC_MAX_LONG_TERM_REF_PICS);
         for (i = 0; i < current->num_long_term_ref_pics_sps; i++) {
-            u(current->log2_max_pic_order_cnt_lsb_minus4 + 4,
-              lt_ref_pic_poc_lsb_sps[i],
-              0, MAX_UINT_BITS(current->log2_max_pic_order_cnt_lsb_minus4 + 4));
-            flag(used_by_curr_pic_lt_sps_flag[i]);
+            us(current->log2_max_pic_order_cnt_lsb_minus4 + 4,
+               lt_ref_pic_poc_lsb_sps[i],
+               0, MAX_UINT_BITS(current->log2_max_pic_order_cnt_lsb_minus4 + 4), 1, i);
+            flags(used_by_curr_pic_lt_sps_flag[i], 1, i);
         }
     }
 
@@ -875,8 +884,8 @@ static int FUNC(pps_range_extension)(CodedBitstreamContext *ctx, RWContext *rw,
            0, sps->log2_diff_max_min_luma_coding_block_size);
         ue(chroma_qp_offset_list_len_minus1, 0, 5);
         for (i = 0; i <= current->chroma_qp_offset_list_len_minus1; i++) {
-            se(cb_qp_offset_list[i], -12, +12);
-            se(cr_qp_offset_list[i], -12, +12);
+            ses(cb_qp_offset_list[i], -12, +12, 1, i);
+            ses(cr_qp_offset_list[i], -12, +12, 1, i);
         }
     }
 
@@ -918,8 +927,8 @@ static int FUNC(pps_scc_extension)(CodedBitstreamContext *ctx, RWContext *rw,
                 int bit_depth = comp == 0 ? current->luma_bit_depth_entry_minus8 + 8
                                           : current->chroma_bit_depth_entry_minus8 + 8;
                 for (i = 0; i < current->pps_num_palette_predictor_initializer; i++)
-                    u(bit_depth, pps_palette_predictor_initializers[comp][i],
-                      0, MAX_UINT_BITS(bit_depth));
+                    us(bit_depth, pps_palette_predictor_initializers[comp][i],
+                       0, MAX_UINT_BITS(bit_depth), 2, comp, i);
             }
         }
     }
@@ -985,9 +994,9 @@ static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
         flag(uniform_spacing_flag);
         if (!current->uniform_spacing_flag) {
             for (i = 0; i < current->num_tile_columns_minus1; i++)
-                ue(column_width_minus1[i], 0, sps->pic_width_in_luma_samples);
+                ues(column_width_minus1[i], 0, sps->pic_width_in_luma_samples,  1, i);
             for (i = 0; i < current->num_tile_rows_minus1; i++)
-                ue(row_height_minus1[i],   0, sps->pic_height_in_luma_samples);
+                ues(row_height_minus1[i],   0, sps->pic_height_in_luma_samples, 1, i);
         }
         flag(loop_filter_across_tiles_enabled_flag);
     } else {
@@ -1078,14 +1087,14 @@ static int FUNC(ref_pic_lists_modification)(CodedBitstreamContext *ctx, RWContex
     flag(ref_pic_list_modification_flag_l0);
     if (current->ref_pic_list_modification_flag_l0) {
         for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++)
-            u(entry_size, list_entry_l0[i], 0, num_pic_total_curr - 1);
+            us(entry_size, list_entry_l0[i], 0, num_pic_total_curr - 1, 1, i);
     }
 
     if (current->slice_type == HEVC_SLICE_B) {
         flag(ref_pic_list_modification_flag_l1);
         if (current->ref_pic_list_modification_flag_l1) {
             for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++)
-                u(entry_size, list_entry_l1[i], 0, num_pic_total_curr - 1);
+                us(entry_size, list_entry_l1[i], 0, num_pic_total_curr - 1, 1, i);
         }
     }
 
@@ -1109,14 +1118,14 @@ static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
 
     for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
         if (1 /* is not same POC and same layer_id */)
-            flag(luma_weight_l0_flag[i]);
+            flags(luma_weight_l0_flag[i], 1, i);
         else
             infer(luma_weight_l0_flag[i], 0);
     }
     if (chroma) {
         for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
             if (1 /* is not same POC and same layer_id */)
-                flag(chroma_weight_l0_flag[i]);
+                flags(chroma_weight_l0_flag[i], 1, i);
             else
                 infer(chroma_weight_l0_flag[i], 0);
         }
@@ -1124,20 +1133,20 @@ static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
 
     for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
         if (current->luma_weight_l0_flag[i]) {
-            se(delta_luma_weight_l0[i], -128, +127);
-            se(luma_offset_l0[i],
-               -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)),
-               ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1));
+            ses(delta_luma_weight_l0[i], -128, +127, 1, i);
+            ses(luma_offset_l0[i],
+                -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)),
+                ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1), 1, i);
         } else {
             infer(delta_luma_weight_l0[i], 0);
             infer(luma_offset_l0[i],       0);
         }
         if (current->chroma_weight_l0_flag[i]) {
             for (j = 0; j < 2; j++) {
-                se(delta_chroma_weight_l0[i][j], -128, +127);
-                se(chroma_offset_l0[i][j],
-                   -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)),
-                   ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1));
+                ses(delta_chroma_weight_l0[i][j], -128, +127, 2, i, j);
+                ses(chroma_offset_l0[i][j],
+                    -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)),
+                    ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1), 2, i, j);
             }
         } else {
             for (j = 0; j < 2; j++) {
@@ -1150,14 +1159,14 @@ static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
     if (current->slice_type == HEVC_SLICE_B) {
         for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
             if (1 /* RefPicList1[i] is not CurrPic, nor is it in a different layer */)
-                flag(luma_weight_l1_flag[i]);
+                flags(luma_weight_l1_flag[i], 1, i);
             else
                 infer(luma_weight_l1_flag[i], 0);
         }
         if (chroma) {
             for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
                 if (1 /* RefPicList1[i] is not CurrPic, nor is it in a different layer */)
-                    flag(chroma_weight_l1_flag[i]);
+                    flags(chroma_weight_l1_flag[i], 1, i);
                 else
                     infer(chroma_weight_l1_flag[i], 0);
             }
@@ -1165,20 +1174,20 @@ static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
 
         for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
             if (current->luma_weight_l1_flag[i]) {
-                se(delta_luma_weight_l1[i], -128, +127);
-                se(luma_offset_l1[i],
-                   -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)),
-                   ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1));
+                ses(delta_luma_weight_l1[i], -128, +127, 1, i);
+                ses(luma_offset_l1[i],
+                    -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)),
+                    ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1), 1, i);
             } else {
                 infer(delta_luma_weight_l1[i], 0);
                 infer(luma_offset_l1[i],       0);
             }
             if (current->chroma_weight_l1_flag[i]) {
                 for (j = 0; j < 2; j++) {
-                    se(delta_chroma_weight_l1[i][j], -128, +127);
-                    se(chroma_offset_l1[i][j],
-                       -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)),
-                       ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1));
+                    ses(delta_chroma_weight_l1[i][j], -128, +127, 2, i, j);
+                    ses(chroma_offset_l1[i][j],
+                        -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)),
+                        ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1), 2, i, j);
                 }
             } else {
                 for (j = 0; j < 2; j++) {
@@ -1253,7 +1262,7 @@ static int FUNC(slice_segment_header)(CodedBitstreamContext *ctx, RWContext *rw,
 
     if (!current->dependent_slice_segment_flag) {
         for (i = 0; i < pps->num_extra_slice_header_bits; i++)
-            flag(slice_reserved_flag[i]);
+            flags(slice_reserved_flag[i], 1, i);
 
         ue(slice_type, 0, 2);
 
@@ -1309,20 +1318,20 @@ static int FUNC(slice_segment_header)(CodedBitstreamContext *ctx, RWContext *rw,
                                 current->num_long_term_pics; i++) {
                     if (i < current->num_long_term_sps) {
                         if (sps->num_long_term_ref_pics_sps > 1)
-                            u(idx_size, lt_idx_sps[i],
-                              0, sps->num_long_term_ref_pics_sps - 1);
+                            us(idx_size, lt_idx_sps[i],
+                               0, sps->num_long_term_ref_pics_sps - 1, 1, i);
                         if (sps->used_by_curr_pic_lt_sps_flag[current->lt_idx_sps[i]])
                             ++num_pic_total_curr;
                     } else {
-                        u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, poc_lsb_lt[i],
-                          0, MAX_UINT_BITS(sps->log2_max_pic_order_cnt_lsb_minus4 + 4));
-                        flag(used_by_curr_pic_lt_flag[i]);
+                        us(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, poc_lsb_lt[i],
+                           0, MAX_UINT_BITS(sps->log2_max_pic_order_cnt_lsb_minus4 + 4), 1, i);
+                        flags(used_by_curr_pic_lt_flag[i], 1, i);
                         if (current->used_by_curr_pic_lt_flag[i])
                             ++num_pic_total_curr;
                     }
-                    flag(delta_poc_msb_present_flag[i]);
+                    flags(delta_poc_msb_present_flag[i], 1, i);
                     if (current->delta_poc_msb_present_flag[i])
-                        ue(delta_poc_msb_cycle_lt[i], 0, UINT32_MAX - 1);
+                        ues(delta_poc_msb_cycle_lt[i], 0, UINT32_MAX - 1, 1, i);
                     else
                         infer(delta_poc_msb_cycle_lt[i], 0);
                 }
@@ -1480,15 +1489,15 @@ static int FUNC(slice_segment_header)(CodedBitstreamContext *ctx, RWContext *rw,
         if (current->num_entry_point_offsets > 0) {
             ue(offset_len_minus1, 0, 31);
             for (i = 0; i < current->num_entry_point_offsets; i++)
-                u(current->offset_len_minus1 + 1, entry_point_offset_minus1[i],
-                  0, MAX_UINT_BITS(current->offset_len_minus1 + 1));
+                us(current->offset_len_minus1 + 1, entry_point_offset_minus1[i],
+                   0, MAX_UINT_BITS(current->offset_len_minus1 + 1), 1, i);
         }
     }
 
     if (pps->slice_segment_header_extension_present_flag) {
         ue(slice_segment_header_extension_length, 0, 256);
         for (i = 0; i < current->slice_segment_header_extension_length; i++)
-            u(8, slice_segment_header_extension_data_byte[i], 0x00, 0xff);
+            us(8, slice_segment_header_extension_data_byte[i], 0x00, 0xff, 1, i);
     }
 
     CHECK(FUNC(byte_alignment)(ctx, rw));
diff --git a/libavcodec/cbs_internal.h b/libavcodec/cbs_internal.h
index be540e2a44..3280e7602d 100644
--- a/libavcodec/cbs_internal.h
+++ b/libavcodec/cbs_internal.h
@@ -63,8 +63,8 @@ typedef struct CodedBitstreamType {
 void ff_cbs_trace_header(CodedBitstreamContext *ctx,
                          const char *name);
 
-void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx,
-                                 int position, const char *name,
+void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
+                                 const char *name, const int *subscripts,
                                  const char *bitstring, int64_t value);
 
 
@@ -72,11 +72,13 @@ void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx,
 // generation of trace output.
 
 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
-                         int width, const char *name, uint32_t *write_to,
+                         int width, const char *name,
+                         const int *subscripts, uint32_t *write_to,
                          uint32_t range_min, uint32_t range_max);
 
 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
-                          int width, const char *name, uint32_t value,
+                          int width, const char *name,
+                          const int *subscripts, uint32_t value,
                           uint32_t range_min, uint32_t range_max);
 
 // The largest value representable in N bits, suitable for use as
diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 989daf0a4d..1a1455b9c8 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -38,24 +38,29 @@
 #define FUNC_MPEG2(rw, name) FUNC_NAME(rw, mpeg2, name)
 #define FUNC(name) FUNC_MPEG2(READWRITE, name)
 
+#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
+
 
 #define READ
 #define READWRITE read
 #define RWContext GetBitContext
 
-#define xui(width, name, var) do { \
+#define xui(width, name, var, subs, ...) do { \
         uint32_t value = 0; \
         CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
+                                   SUBSCRIPTS(subs, __VA_ARGS__), \
                                    &value, 0, (1 << width) - 1)); \
         var = value; \
     } while (0)
 
 #define ui(width, name) \
-        xui(width, name, current->name)
+        xui(width, name, current->name, 0)
+#define uis(width, name, subs, ...) \
+        xui(width, name, current->name, subs, __VA_ARGS__)
 
 #define marker_bit() do { \
         av_unused uint32_t one; \
-        CHECK(ff_cbs_read_unsigned(ctx, rw, 1, "marker_bit", &one, 1, 1)); \
+        CHECK(ff_cbs_read_unsigned(ctx, rw, 1, "marker_bit", NULL, &one, 1, 1)); \
     } while (0)
 
 #define nextbits(width, compare, var) \
@@ -77,16 +82,19 @@
 #define READWRITE write
 #define RWContext PutBitContext
 
-#define xui(width, name, var) do { \
+#define xui(width, name, var, subs, ...) do { \
         CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
+                                    SUBSCRIPTS(subs, __VA_ARGS__), \
                                     var, 0, (1 << width) - 1)); \
     } while (0)
 
 #define ui(width, name) \
-        xui(width, name, current->name)
+        xui(width, name, current->name, 0)
+#define uis(width, name, subs, ...) \
+        xui(width, name, current->name, subs, __VA_ARGS__)
 
 #define marker_bit() do { \
-        CHECK(ff_cbs_write_unsigned(ctx, rw, 1, "marker_bit", 1, 1, 1)); \
+        CHECK(ff_cbs_write_unsigned(ctx, rw, 1, "marker_bit", NULL, 1, 1, 1)); \
     } while (0)
 
 #define nextbits(width, compare, var) (var)
diff --git a/libavcodec/cbs_mpeg2_syntax_template.c b/libavcodec/cbs_mpeg2_syntax_template.c
index 51c32cb58d..8b5d35437e 100644
--- a/libavcodec/cbs_mpeg2_syntax_template.c
+++ b/libavcodec/cbs_mpeg2_syntax_template.c
@@ -44,13 +44,13 @@ static int FUNC(sequence_header)(CodedBitstreamContext *ctx, RWContext *rw,
     ui(1, load_intra_quantiser_matrix);
     if (current->load_intra_quantiser_matrix) {
         for (i = 0; i < 64; i++)
-            ui(8, intra_quantiser_matrix[i]);
+            uis(8, intra_quantiser_matrix[i], 1, i);
     }
 
     ui(1, load_non_intra_quantiser_matrix);
     if (current->load_non_intra_quantiser_matrix) {
         for (i = 0; i < 64; i++)
-            ui(8, non_intra_quantiser_matrix[i]);
+            uis(8, non_intra_quantiser_matrix[i], 1, i);
     }
 
     return 0;
@@ -79,7 +79,7 @@ static int FUNC(user_data)(CodedBitstreamContext *ctx, RWContext *rw,
 #endif
 
     for (k = 0; k < current->user_data_length; k++)
-        xui(8, user_data, current->user_data[k]);
+        xui(8, user_data, current->user_data[k], 0);
 
     return 0;
 }
@@ -250,25 +250,25 @@ static int FUNC(quant_matrix_extension)(CodedBitstreamContext *ctx, RWContext *r
     ui(1, load_intra_quantiser_matrix);
     if (current->load_intra_quantiser_matrix) {
         for (i = 0; i < 64; i++)
-            ui(8, intra_quantiser_matrix[i]);
+            uis(8, intra_quantiser_matrix[i], 1, i);
     }
 
     ui(1, load_non_intra_quantiser_matrix);
     if (current->load_non_intra_quantiser_matrix) {
         for (i = 0; i < 64; i++)
-            ui(8, non_intra_quantiser_matrix[i]);
+            uis(8, non_intra_quantiser_matrix[i], 1, i);
     }
 
     ui(1, load_chroma_intra_quantiser_matrix);
     if (current->load_chroma_intra_quantiser_matrix) {
         for (i = 0; i < 64; i++)
-            ui(8, intra_quantiser_matrix[i]);
+            uis(8, intra_quantiser_matrix[i], 1, i);
     }
 
     ui(1, load_chroma_non_intra_quantiser_matrix);
     if (current->load_chroma_non_intra_quantiser_matrix) {
         for (i = 0; i < 64; i++)
-            ui(8, chroma_non_intra_quantiser_matrix[i]);
+            uis(8, chroma_non_intra_quantiser_matrix[i], 1, i);
     }
 
     return 0;
@@ -366,15 +366,16 @@ static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw,
                 if (!current->extra_information)
                     return AVERROR(ENOMEM);
                 for (k = 0; k < current->extra_information_length; k++) {
-                    xui(1, extra_bit_slice, bit);
-                    xui(8, extra_information_slice,
-                        current->extra_information[k]);
+                    xui(1, extra_bit_slice, bit, 0);
+                    xui(8, extra_information_slice[k],
+                        current->extra_information[k], 1, k);
                 }
             }
 #else
             for (k = 0; k < current->extra_information_length; k++) {
-                xui(1, extra_bit_slice, 1);
-                xui(8, extra_information_slice, current->extra_information[k]);
+                xui(1, extra_bit_slice, 1, 0);
+                xui(8, extra_information_slice[k],
+                    current->extra_information[k], 1, k);
             }
 #endif
         }
-- 
2.16.3



More information about the ffmpeg-devel mailing list