[FFmpeg-devel,1/3] avcodec/cbs_h2645: add macros to read and write fields with no custom range of values

Submitted by James Almer on April 17, 2019, 2:56 a.m.

Details

Message ID 20190417025635.9700-1-jamrial@gmail.com
State New
Headers show

Commit Message

James Almer April 17, 2019, 2:56 a.m.
Signed-off-by: James Almer <jamrial@gmail.com>
---
Better macro names welcome. I used the same convention as in cbs_av1.

fate-cbs passes, but i'm sure a bunch of these are not tested by it,
so help double checking i didn't screw up is welcome.

 libavcodec/cbs_h2645.c                |  10 +-
 libavcodec/cbs_h264_syntax_template.c |  60 ++++++------
 libavcodec/cbs_h265_syntax_template.c | 126 +++++++++++---------------
 3 files changed, 90 insertions(+), 106 deletions(-)

Comments

James Almer April 22, 2019, 5:21 p.m.
On 4/16/2019 11:56 PM, James Almer wrote:
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
> Better macro names welcome. I used the same convention as in cbs_av1.
> 
> fate-cbs passes, but i'm sure a bunch of these are not tested by it,
> so help double checking i didn't screw up is welcome.
> 
>  libavcodec/cbs_h2645.c                |  10 +-
>  libavcodec/cbs_h264_syntax_template.c |  60 ++++++------
>  libavcodec/cbs_h265_syntax_template.c | 126 +++++++++++---------------
>  3 files changed, 90 insertions(+), 106 deletions(-)

Ping for set.
Mark Thompson April 28, 2019, 10:59 p.m.
On 17/04/2019 03:56, James Almer wrote:
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
> Better macro names welcome. I used the same convention as in cbs_av1.
> 
> fate-cbs passes, but i'm sure a bunch of these are not tested by it,
> so help double checking i didn't screw up is welcome.
> 
>  libavcodec/cbs_h2645.c                |  10 +-
>  libavcodec/cbs_h264_syntax_template.c |  60 ++++++------
>  libavcodec/cbs_h265_syntax_template.c | 126 +++++++++++---------------
>  3 files changed, 90 insertions(+), 106 deletions(-)

The cleanup to things with nontrivial length expressions is nice :)

LGTM.

Thanks,

- Mark
James Almer April 28, 2019, 11:34 p.m.
On 4/28/2019 7:59 PM, Mark Thompson wrote:
> On 17/04/2019 03:56, James Almer wrote:
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>> Better macro names welcome. I used the same convention as in cbs_av1.
>>
>> fate-cbs passes, but i'm sure a bunch of these are not tested by it,
>> so help double checking i didn't screw up is welcome.
>>
>>  libavcodec/cbs_h2645.c                |  10 +-
>>  libavcodec/cbs_h264_syntax_template.c |  60 ++++++------
>>  libavcodec/cbs_h265_syntax_template.c | 126 +++++++++++---------------
>>  3 files changed, 90 insertions(+), 106 deletions(-)
> 
> The cleanup to things with nontrivial length expressions is nice :)
> 
> LGTM.
> 
> Thanks,
> 
> - Mark

Pushed, thanks!

Patch hide | download patch | download mbox

diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index a205293b3c..319202fc48 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -252,22 +252,30 @@  static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
 
 #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 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)
 #define i(width, name, range_min, range_max) \
         xi(width, name, current->name, range_min, range_max, 0)
+#define ib(width, name) \
+        xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), 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 ubs(width, name, subs, ...) \
+        xu(width, name, current->name, 0, MAX_UINT_BITS(width), 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 is(width, name, range_min, range_max, subs, ...) \
         xi(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
+#define ibs(width, name, subs, ...) \
+        xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), subs, __VA_ARGS__)
 #define ses(name, range_min, range_max, subs, ...) \
         xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
 
diff --git a/libavcodec/cbs_h264_syntax_template.c b/libavcodec/cbs_h264_syntax_template.c
index 07b4cddb5e..cc9bd07590 100644
--- a/libavcodec/cbs_h264_syntax_template.c
+++ b/libavcodec/cbs_h264_syntax_template.c
@@ -34,8 +34,8 @@  static int FUNC(nal_unit_header)(CodedBitstreamContext *ctx, RWContext *rw,
     int err;
 
     u(1, forbidden_zero_bit, 0, 0);
-    u(2, nal_ref_idc,        0, 3);
-    u(5, nal_unit_type,      0, 31);
+    ub(2, nal_ref_idc);
+    ub(5, nal_unit_type);
 
     if (!(1 << current->nal_unit_type & valid_type_mask)) {
         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid NAL unit type %d.\n",
@@ -91,8 +91,8 @@  static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
     int err, i;
 
     ue(cpb_cnt_minus1, 0, 31);
-    u(4, bit_rate_scale, 0, 15);
-    u(4, cpb_size_scale, 0, 15);
+    ub(4, bit_rate_scale);
+    ub(4, cpb_size_scale);
 
     for (i = 0; i <= current->cpb_cnt_minus1; i++) {
         ues(bit_rate_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
@@ -100,10 +100,10 @@  static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
         flags(cbr_flag[i], 1, i);
     }
 
-    u(5, initial_cpb_removal_delay_length_minus1, 0, 31);
-    u(5, cpb_removal_delay_length_minus1,         0, 31);
-    u(5, dpb_output_delay_length_minus1,          0, 31);
-    u(5, time_offset_length,                      0, 31);
+    ub(5, initial_cpb_removal_delay_length_minus1);
+    ub(5, cpb_removal_delay_length_minus1);
+    ub(5, dpb_output_delay_length_minus1);
+    ub(5, time_offset_length);
 
     return 0;
 }
@@ -115,10 +115,10 @@  static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
 
     flag(aspect_ratio_info_present_flag);
     if (current->aspect_ratio_info_present_flag) {
-        u(8, aspect_ratio_idc, 0, 255);
+        ub(8, aspect_ratio_idc);
         if (current->aspect_ratio_idc == 255) {
-            u(16, sar_width,  0, 65535);
-            u(16, sar_height, 0, 65535);
+            ub(16, sar_width);
+            ub(16, sar_height);
         }
     } else {
         infer(aspect_ratio_idc, 0);
@@ -130,13 +130,13 @@  static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
 
     flag(video_signal_type_present_flag);
     if (current->video_signal_type_present_flag) {
-        u(3, video_format, 0, 7);
+        ub(3, video_format);
         flag(video_full_range_flag);
         flag(colour_description_present_flag);
         if (current->colour_description_present_flag) {
-            u(8, colour_primaries,         0, 255);
-            u(8, transfer_characteristics, 0, 255);
-            u(8, matrix_coefficients,      0, 255);
+            ub(8, colour_primaries);
+            ub(8, transfer_characteristics);
+            ub(8, matrix_coefficients);
         }
     } else {
         infer(video_format,             5);
@@ -263,7 +263,7 @@  static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
                                 1 << H264_NAL_SPS));
 
-    u(8, profile_idc, 0, 255);
+    ub(8, profile_idc);
 
     flag(constraint_set0_flag);
     flag(constraint_set1_flag);
@@ -274,7 +274,7 @@  static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
 
     u(2, reserved_zero_2bits,  0, 0);
 
-    u(8, level_idc, 0, 255);
+    ub(8, level_idc);
 
     ue(seq_parameter_set_id, 0, 31);
 
@@ -386,8 +386,8 @@  static int FUNC(sps_extension)(CodedBitstreamContext *ctx, RWContext *rw,
         flag(alpha_incr_flag);
 
         bits = current->bit_depth_aux_minus8 + 9;
-        u(bits, alpha_opaque_value,      0, MAX_UINT_BITS(bits));
-        u(bits, alpha_transparent_value, 0, MAX_UINT_BITS(bits));
+        ub(bits, alpha_opaque_value);
+        ub(bits, alpha_transparent_value);
     }
 
     flag(additional_extension_flag);
@@ -565,7 +565,7 @@  static int FUNC(sei_pic_timestamp)(CodedBitstreamContext *ctx, RWContext *rw,
     flag(full_timestamp_flag);
     flag(discontinuity_flag);
     flag(cnt_dropped_flag);
-    u(8, n_frames, 0, 255);
+    ub(8, n_frames);
     if (current->full_timestamp_flag) {
             u(6, seconds_value, 0, 59);
             u(6, minutes_value, 0, 59);
@@ -592,9 +592,7 @@  static int FUNC(sei_pic_timestamp)(CodedBitstreamContext *ctx, RWContext *rw,
         time_offset_length = 24;
 
     if (time_offset_length > 0)
-        i(time_offset_length, time_offset,
-          MIN_INT_BITS(time_offset_length),
-          MAX_INT_BITS(time_offset_length));
+        ib(time_offset_length, time_offset);
     else
         infer(time_offset, 0);
 
@@ -647,10 +645,8 @@  static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
             return AVERROR_INVALIDDATA;
         }
 
-        u(hrd->cpb_removal_delay_length_minus1 + 1, cpb_removal_delay,
-          0, MAX_UINT_BITS(hrd->cpb_removal_delay_length_minus1 + 1));
-        u(hrd->dpb_output_delay_length_minus1 + 1, dpb_output_delay,
-          0, MAX_UINT_BITS(hrd->dpb_output_delay_length_minus1 + 1));
+        ub(hrd->cpb_removal_delay_length_minus1 + 1, cpb_removal_delay);
+        ub(hrd->dpb_output_delay_length_minus1 + 1, dpb_output_delay);
     }
 
     if (sps->vui.pic_struct_present_flag) {
@@ -790,7 +786,7 @@  static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *
     if (!current->display_orientation_cancel_flag) {
         flag(hor_flip);
         flag(ver_flip);
-        u(16, anticlockwise_rotation, 0, 65535);
+        ub(16, anticlockwise_rotation);
         ue(display_orientation_repetition_period, 0, 16384);
         flag(display_orientation_extension_flag);
     }
@@ -996,7 +992,7 @@  static int FUNC(aud)(CodedBitstreamContext *ctx, RWContext *rw,
     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
                                 1 << H264_NAL_AUD));
 
-    u(3, primary_pic_type, 0, 7);
+    ub(3, primary_pic_type);
 
     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
 
@@ -1232,8 +1228,7 @@  static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw,
     if (sps->separate_colour_plane_flag)
         u(2, colour_plane_id, 0, 2);
 
-    u(sps->log2_max_frame_num_minus4 + 4, frame_num,
-      0, MAX_UINT_BITS(sps->log2_max_frame_num_minus4 + 4));
+    ub(sps->log2_max_frame_num_minus4 + 4, frame_num);
 
     if (!sps->frame_mbs_only_flag) {
         flag(field_pic_flag);
@@ -1250,8 +1245,7 @@  static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw,
         ue(idr_pic_id, 0, 65535);
 
     if (sps->pic_order_cnt_type == 0) {
-        u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, pic_order_cnt_lsb,
-          0, MAX_UINT_BITS(sps->log2_max_pic_order_cnt_lsb_minus4 + 4));
+        ub(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, pic_order_cnt_lsb);
         if (pps->bottom_field_pic_order_in_frame_present_flag &&
             !current->field_pic_flag)
             se(delta_pic_order_cnt_bottom, INT32_MIN + 1, INT32_MAX);
diff --git a/libavcodec/cbs_h265_syntax_template.c b/libavcodec/cbs_h265_syntax_template.c
index bbd363a389..87d1332f48 100644
--- a/libavcodec/cbs_h265_syntax_template.c
+++ b/libavcodec/cbs_h265_syntax_template.c
@@ -39,7 +39,7 @@  static int FUNC(nal_unit_header)(CodedBitstreamContext *ctx, RWContext *rw,
         u(6, nal_unit_type, expected_nal_unit_type,
                             expected_nal_unit_type);
     else
-        u(6, nal_unit_type, 0, 63);
+        ub(6, nal_unit_type);
 
     u(6, nuh_layer_id,          0, 62);
     u(3, nuh_temporal_id_plus1, 1,  7);
@@ -95,7 +95,7 @@  static int FUNC(profile_tier_level)(CodedBitstreamContext *ctx, RWContext *rw,
     if (profile_present_flag) {
         u(2, general_profile_space, 0, 0);
         flag(general_tier_flag);
-        u(5, general_profile_idc, 0, 31);
+        ub(5, general_profile_idc);
 
         for (j = 0; j < 32; j++)
             flags(general_profile_compatibility_flag[j], 1, j);
@@ -150,7 +150,7 @@  static int FUNC(profile_tier_level)(CodedBitstreamContext *ctx, RWContext *rw,
 #undef profile_compatible
     }
 
-    u(8, general_level_idc, 0, 255);
+    ub(8, general_level_idc);
 
     for (i = 0; i < max_num_sub_layers_minus1; i++) {
         flags(sub_layer_profile_present_flag[i], 1, i);
@@ -265,20 +265,20 @@  static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
             current->vcl_hrd_parameters_present_flag) {
             flag(sub_pic_hrd_params_present_flag);
             if (current->sub_pic_hrd_params_present_flag) {
-                u(8, tick_divisor_minus2, 0, 255);
-                u(5, du_cpb_removal_delay_increment_length_minus1, 0, 31);
+                ub(8, tick_divisor_minus2);
+                ub(5, du_cpb_removal_delay_increment_length_minus1);
                 flag(sub_pic_cpb_params_in_pic_timing_sei_flag);
-                u(5, dpb_output_delay_du_length_minus1, 0, 31);
+                ub(5, dpb_output_delay_du_length_minus1);
             }
 
-            u(4, bit_rate_scale, 0, 15);
-            u(4, cpb_size_scale, 0, 15);
+            ub(4, bit_rate_scale);
+            ub(4, cpb_size_scale);
             if (current->sub_pic_hrd_params_present_flag)
-                u(4, cpb_size_du_scale, 0, 15);
+                ub(4, cpb_size_du_scale);
 
-            u(5, initial_cpb_removal_delay_length_minus1, 0, 31);
-            u(5, au_cpb_removal_delay_length_minus1,      0, 31);
-            u(5, dpb_output_delay_length_minus1,          0, 31);
+            ub(5, initial_cpb_removal_delay_length_minus1);
+            ub(5, au_cpb_removal_delay_length_minus1);
+            ub(5, dpb_output_delay_length_minus1);
         } else {
             infer(sub_pic_hrd_params_present_flag, 0);
 
@@ -323,10 +323,10 @@  static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
 
     flag(aspect_ratio_info_present_flag);
     if (current->aspect_ratio_info_present_flag) {
-        u(8, aspect_ratio_idc, 0, 255);
+        ub(8, aspect_ratio_idc);
         if (current->aspect_ratio_idc == 255) {
-            u(16, sar_width,  0, 65535);
-            u(16, sar_height, 0, 65535);
+            ub(16, sar_width);
+            ub(16, sar_height);
         }
     } else {
         infer(aspect_ratio_idc, 0);
@@ -338,13 +338,13 @@  static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
 
     flag(video_signal_type_present_flag);
     if (current->video_signal_type_present_flag) {
-        u(3, video_format, 0, 7);
+        ub(3, video_format);
         flag(video_full_range_flag);
         flag(colour_description_present_flag);
         if (current->colour_description_present_flag) {
-            u(8, colour_primaries,         0, 255);
-            u(8, transfer_characteristics, 0, 255);
-            u(8, matrix_coefficients,      0, 255);
+            ub(8, colour_primaries);
+            ub(8, transfer_characteristics);
+            ub(8, matrix_coefficients);
         } else {
             infer(colour_primaries,         2);
             infer(transfer_characteristics, 2);
@@ -426,7 +426,7 @@  static int FUNC(vps)(CodedBitstreamContext *ctx, RWContext *rw,
 
     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_VPS));
 
-    u(4, vps_video_parameter_set_id, 0, 15);
+    ub(4, vps_video_parameter_set_id);
 
     flag(vps_base_layer_internal_flag);
     flag(vps_base_layer_available_flag);
@@ -723,8 +723,7 @@  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++)
-                    us(bit_depth, sps_palette_predictor_initializers[comp][i],
-                       0, MAX_UINT_BITS(bit_depth), 2, comp, i);
+                    ubs(bit_depth, sps_palette_predictor_initializers[comp][i], 2, comp, i);
             }
         }
     }
@@ -748,7 +747,7 @@  static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
 
     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_SPS));
 
-    u(4, sps_video_parameter_set_id, 0, 15);
+    ub(4, sps_video_parameter_set_id);
     h265->active_vps = vps = h265->vps[current->sps_video_parameter_set_id];
 
     u(3, sps_max_sub_layers_minus1, 0, HEVC_MAX_SUB_LAYERS - 1);
@@ -887,9 +886,8 @@  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++) {
-            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);
+            ubs(current->log2_max_pic_order_cnt_lsb_minus4 + 4,
+                lt_ref_pic_poc_lsb_sps[i], 1, i);
             flags(used_by_curr_pic_lt_sps_flag[i], 1, i);
         }
     }
@@ -907,7 +905,7 @@  static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
         flag(sps_multilayer_extension_flag);
         flag(sps_3d_extension_flag);
         flag(sps_scc_extension_flag);
-        u(4, sps_extension_4bits, 0, MAX_UINT_BITS(4));
+        ub(4, sps_extension_4bits);
     }
 
     if (current->sps_range_extension_flag)
@@ -986,8 +984,7 @@  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++)
-                    us(bit_depth, pps_palette_predictor_initializers[comp][i],
-                       0, MAX_UINT_BITS(bit_depth), 2, comp, i);
+                    ubs(bit_depth, pps_palette_predictor_initializers[comp][i], 2, comp, i);
             }
         }
     }
@@ -1018,7 +1015,7 @@  static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
 
     flag(dependent_slice_segments_enabled_flag);
     flag(output_flag_present_flag);
-    u(3, num_extra_slice_header_bits, 0, 7);
+    ub(3, num_extra_slice_header_bits);
     flag(sign_data_hiding_enabled_flag);
     flag(cabac_init_present_flag);
 
@@ -1100,7 +1097,7 @@  static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
         flag(pps_multilayer_extension_flag);
         flag(pps_3d_extension_flag);
         flag(pps_scc_extension_flag);
-        u(4, pps_extension_4bits, 0, MAX_UINT_BITS(4));
+        ub(4, pps_extension_4bits);
     }
     if (current->pps_range_extension_flag)
         CHECK(FUNC(pps_range_extension)(ctx, rw, current));
@@ -1335,8 +1332,7 @@  static int FUNC(slice_segment_header)(CodedBitstreamContext *ctx, RWContext *rw,
             current->nal_unit_header.nal_unit_type != HEVC_NAL_IDR_N_LP) {
             const H265RawSTRefPicSet *rps;
 
-            u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, slice_pic_order_cnt_lsb,
-              0, MAX_UINT_BITS(sps->log2_max_pic_order_cnt_lsb_minus4 + 4));
+            ub(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, slice_pic_order_cnt_lsb);
 
             flag(short_term_ref_pic_set_sps_flag);
             if (!current->short_term_ref_pic_set_sps_flag) {
@@ -1382,8 +1378,7 @@  static int FUNC(slice_segment_header)(CodedBitstreamContext *ctx, RWContext *rw,
                         if (sps->used_by_curr_pic_lt_sps_flag[current->lt_idx_sps[i]])
                             ++num_pic_total_curr;
                     } else {
-                        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);
+                        ubs(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, poc_lsb_lt[i], 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;
@@ -1548,8 +1543,7 @@  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++)
-                us(current->offset_len_minus1 + 1, entry_point_offset_minus1[i],
-                   0, MAX_UINT_BITS(current->offset_len_minus1 + 1), 1, i);
+                ubs(current->offset_len_minus1 + 1, entry_point_offset_minus1[i], 1, i);
         }
     }
 
@@ -1610,9 +1604,9 @@  static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
         infer(irap_cpb_params_present_flag, 0);
     if (current->irap_cpb_params_present_flag) {
         length = hrd->au_cpb_removal_delay_length_minus1 + 1;
-        u(length, cpb_delay_offset, 0, MAX_UINT_BITS(length));
+        ub(length, cpb_delay_offset);
         length = hrd->dpb_output_delay_length_minus1 + 1;
-        u(length, dpb_delay_offset, 0, MAX_UINT_BITS(length));
+        ub(length, dpb_delay_offset);
     } else {
         infer(cpb_delay_offset, 0);
         infer(dpb_delay_offset, 0);
@@ -1621,23 +1615,19 @@  static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
     flag(concatenation_flag);
 
     length = hrd->au_cpb_removal_delay_length_minus1 + 1;
-    u(length, au_cpb_removal_delay_delta_minus1, 0, MAX_UINT_BITS(length));
+    ub(length, au_cpb_removal_delay_delta_minus1);
 
     if (hrd->nal_hrd_parameters_present_flag) {
         for (i = 0; i <= hrd->cpb_cnt_minus1[0]; i++) {
             length = hrd->initial_cpb_removal_delay_length_minus1 + 1;
 
-            us(length, nal_initial_cpb_removal_delay[i],
-               0, MAX_UINT_BITS(length), 1, i);
-            us(length, nal_initial_cpb_removal_offset[i],
-               0, MAX_UINT_BITS(length), 1, i);
+            ubs(length, nal_initial_cpb_removal_delay[i], 1, i);
+            ubs(length, nal_initial_cpb_removal_offset[i], 1, i);
 
             if (hrd->sub_pic_hrd_params_present_flag ||
                 current->irap_cpb_params_present_flag) {
-                us(length, nal_initial_alt_cpb_removal_delay[i],
-                   0, MAX_UINT_BITS(length), 1, i);
-                us(length, nal_initial_alt_cpb_removal_offset[i],
-                   0, MAX_UINT_BITS(length), 1, i);
+                ubs(length, nal_initial_alt_cpb_removal_delay[i], 1, i);
+                ubs(length, nal_initial_alt_cpb_removal_offset[i], 1, i);
             }
         }
     }
@@ -1645,17 +1635,13 @@  static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
         for (i = 0; i <= hrd->cpb_cnt_minus1[0]; i++) {
             length = hrd->initial_cpb_removal_delay_length_minus1 + 1;
 
-            us(length, vcl_initial_cpb_removal_delay[i],
-               0, MAX_UINT_BITS(length), 1, i);
-            us(length, vcl_initial_cpb_removal_offset[i],
-               0, MAX_UINT_BITS(length), 1, i);
+            ubs(length, vcl_initial_cpb_removal_delay[i], 1, i);
+            ubs(length, vcl_initial_cpb_removal_offset[i], 1, i);
 
             if (hrd->sub_pic_hrd_params_present_flag ||
                 current->irap_cpb_params_present_flag) {
-                us(length, vcl_initial_alt_cpb_removal_delay[i],
-                   0, MAX_UINT_BITS(length), 1, i);
-                us(length, vcl_initial_alt_cpb_removal_offset[i],
-                   0, MAX_UINT_BITS(length), 1, i);
+                ubs(length, vcl_initial_alt_cpb_removal_delay[i], 1, i);
+                ubs(length, vcl_initial_alt_cpb_removal_offset[i], 1, i);
             }
         }
     }
@@ -1720,14 +1706,14 @@  static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
     if (hrd && (hrd->nal_hrd_parameters_present_flag ||
                 hrd->vcl_hrd_parameters_present_flag)) {
         length = hrd->au_cpb_removal_delay_length_minus1 + 1;
-        u(length, au_cpb_removal_delay_minus1, 0, MAX_UINT_BITS(length));
+        ub(length, au_cpb_removal_delay_minus1);
 
         length = hrd->dpb_output_delay_length_minus1 + 1;
-        u(length, pic_dpb_output_delay, 0, MAX_UINT_BITS(length));
+        ub(length, pic_dpb_output_delay);
 
         if (hrd->sub_pic_hrd_params_present_flag) {
             length = hrd->dpb_output_delay_du_length_minus1 + 1;
-            u(length, pic_dpb_output_du_delay, 0, MAX_UINT_BITS(length));
+            ub(length, pic_dpb_output_du_delay);
         }
 
         if (hrd->sub_pic_hrd_params_present_flag &&
@@ -1738,16 +1724,14 @@  static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
 
             length = hrd->du_cpb_removal_delay_increment_length_minus1 + 1;
             if (current->du_common_cpb_removal_delay_flag)
-                u(length, du_common_cpb_removal_delay_increment_minus1,
-                  0, MAX_UINT_BITS(length));
+                ub(length, du_common_cpb_removal_delay_increment_minus1);
 
             for (i = 0; i <= current->num_decoding_units_minus1; i++) {
                 ues(num_nalus_in_du_minus1[i],
                     0, HEVC_MAX_SLICE_SEGMENTS, 1, i);
                 if (!current->du_common_cpb_removal_delay_flag &&
                     i < current->num_decoding_units_minus1)
-                    us(length, du_cpb_removal_delay_increment_minus1[i],
-                      0, MAX_UINT_BITS(length), 1, i);
+                    ubs(length, du_cpb_removal_delay_increment_minus1[i], 1, i);
             }
         }
     }
@@ -1871,7 +1855,7 @@  static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *
     if (!current->display_orientation_cancel_flag) {
         flag(hor_flip);
         flag(ver_flip);
-        u(16, anticlockwise_rotation, 0, 65535);
+        ub(16, anticlockwise_rotation);
         flag(display_orientation_persistence_flag);
     }
 
@@ -1964,7 +1948,7 @@  static int FUNC(sei_time_code)(CodedBitstreamContext *ctx, RWContext *rw,
             flags(discontinuity_flag[i],     1, i);
             flags(cnt_dropped_flag[i],       1, i);
 
-            us(9, n_frames[i], 0, MAX_UINT_BITS(9), 1, i);
+            ubs(9, n_frames[i], 1, i);
 
             if (current->full_timestamp_flag[i]) {
                 us(6, seconds_value[i], 0, 59, 1, i);
@@ -1984,11 +1968,9 @@  static int FUNC(sei_time_code)(CodedBitstreamContext *ctx, RWContext *rw,
                 }
             }
 
-            us(5, time_offset_length[i], 0, 31, 1, i);
+            ubs(5, time_offset_length[i], 1, i);
             if (current->time_offset_length[i] > 0)
-                is(current->time_offset_length[i], time_offset_value[i],
-                   MIN_INT_BITS(current->time_offset_length[i]),
-                   MAX_INT_BITS(current->time_offset_length[i]), 1, i);
+                ibs(current->time_offset_length[i], time_offset_value[i], 1, i);
             else
                 infer(time_offset_value[i], 0);
         }
@@ -2027,8 +2009,8 @@  static int FUNC(sei_content_light_level)(CodedBitstreamContext *ctx, RWContext *
 
     HEADER("Content Light Level");
 
-    u(16, max_content_light_level, 0, MAX_UINT_BITS(16));
-    u(16, max_pic_average_light_level, 0, MAX_UINT_BITS(16));
+    ub(16, max_content_light_level);
+    ub(16, max_pic_average_light_level);
 
     return 0;
 }
@@ -2041,7 +2023,7 @@  static int FUNC(sei_alternative_transfer_characteristics)(CodedBitstreamContext
 
     HEADER("Alternative Transfer Characteristics");
 
-    u(8, preferred_transfer_characteristics, 0, 255);
+    ub(8, preferred_transfer_characteristics);
 
     return 0;
 }