LCOV - code coverage report
Current view: top level - libavcodec - cbs_h265_syntax_template.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 661 880 75.1 %
Date: 2017-12-11 04:34:20 Functions: 32 42 76.2 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19         854 : static int FUNC(rbsp_trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
      20             : {
      21             :     int err;
      22         854 :     av_unused int one = 1, zero = 0;
      23         854 :     xu(1, rbsp_stop_one_bit, one, 1, 1);
      24        4834 :     while (byte_alignment(rw) != 0)
      25        3126 :         xu(1, rbsp_alignment_zero_bit, zero, 0, 0);
      26             : 
      27         854 :     return 0;
      28             : }
      29             : 
      30       12516 : static int FUNC(nal_unit_header)(CodedBitstreamContext *ctx, RWContext *rw,
      31             :                                  H265RawNALUnitHeader *current,
      32             :                                  int expected_nal_unit_type)
      33             : {
      34             :     int err;
      35             : 
      36       12516 :     u(1, forbidden_zero_bit, 0, 0);
      37             : 
      38       12516 :     if (expected_nal_unit_type >= 0)
      39         854 :         u(6, nal_unit_type, expected_nal_unit_type,
      40             :                             expected_nal_unit_type);
      41             :     else
      42       11662 :         u(6, nal_unit_type, 0, 63);
      43             : 
      44       12516 :     u(6, nuh_layer_id,          0, 62);
      45       12516 :     u(3, nuh_temporal_id_plus1, 1,  7);
      46             : 
      47       12516 :     return 0;
      48             : }
      49             : 
      50       11662 : static int FUNC(byte_alignment)(CodedBitstreamContext *ctx, RWContext *rw)
      51             : {
      52             :     int err;
      53       11662 :     av_unused int one = 1, zero = 0;
      54       11662 :     xu(1, alignment_bit_equal_to_one, one, 1, 1);
      55       70476 :     while (byte_alignment(rw) != 0)
      56       47152 :         xu(1, alignment_bit_equal_to_zero, zero, 0, 0);
      57             : 
      58       11662 :     return 0;
      59             : }
      60             : 
      61           0 : static int FUNC(extension_data)(CodedBitstreamContext *ctx, RWContext *rw,
      62             :                                 H265RawPSExtensionData *current)
      63             : {
      64             :     int err;
      65             :     size_t k;
      66             : #ifdef READ
      67             :     GetBitContext start;
      68             :     uint8_t bit;
      69           0 :     start = *rw;
      70           0 :     for (k = 0; cbs_h2645_read_more_rbsp_data(rw); k++)
      71           0 :         skip_bits(rw, 1);
      72           0 :     current->bit_length = k;
      73           0 :     if (k > 0) {
      74           0 :         *rw = start;
      75           0 :         allocate(current->data, (current->bit_length + 7) / 8);
      76           0 :         for (k = 0; k < current->bit_length; k++) {
      77           0 :             xu(1, extension_data, bit, 0, 1);
      78           0 :             current->data[k / 8] |= bit << (7 - k % 8);
      79             :         }
      80             :     }
      81             : #else
      82           0 :     for (k = 0; k < current->bit_length; k++)
      83           0 :         xu(1, extension_data, current->data[k / 8] >> (7 - k % 8), 0, 1);
      84             : #endif
      85           0 :     return 0;
      86             : }
      87             : 
      88         188 : static int FUNC(profile_tier_level)(CodedBitstreamContext *ctx, RWContext *rw,
      89             :                                     H265RawProfileTierLevel *current,
      90             :                                     int profile_present_flag,
      91             :                                     int max_num_sub_layers_minus1)
      92             : {
      93         188 :     av_unused unsigned int zero = 0;
      94             :     int err, i, j;
      95             : 
      96         188 :     if (profile_present_flag) {
      97         188 :         u(2, general_profile_space, 0, 0);
      98         188 :         flag(general_tier_flag);
      99         188 :         u(5, general_profile_idc, 0, 31);
     100             : 
     101        6204 :         for (j = 0; j < 32; j++)
     102        6016 :             flag(general_profile_compatibility_flag[j]);
     103             : 
     104         188 :         flag(general_progressive_source_flag);
     105         188 :         flag(general_interlaced_source_flag);
     106         188 :         flag(general_non_packed_constraint_flag);
     107         188 :         flag(general_frame_only_constraint_flag);
     108             : 
     109             : #define profile_compatible(x) (current->general_profile_idc == (x) || \
     110             :                                current->general_profile_compatibility_flag[x])
     111         376 :         if (profile_compatible(4) || profile_compatible(5) ||
     112         564 :             profile_compatible(6) || profile_compatible(7) ||
     113         564 :             profile_compatible(8) || profile_compatible(9) ||
     114         376 :             profile_compatible(10)) {
     115           0 :             flag(general_max_12bit_constraint_flag);
     116           0 :             flag(general_max_10bit_constraint_flag);
     117           0 :             flag(general_max_8bit_constraint_flag);
     118           0 :             flag(general_max_422chroma_constraint_flag);
     119           0 :             flag(general_max_420chroma_constraint_flag);
     120           0 :             flag(general_max_monochrome_constraint_flag);
     121           0 :             flag(general_intra_constraint_flag);
     122           0 :             flag(general_one_picture_only_constraint_flag);
     123           0 :             flag(general_lower_bit_rate_constraint_flag);
     124             : 
     125           0 :             if (profile_compatible(5) || profile_compatible(9) ||
     126           0 :                 profile_compatible(10)) {
     127           0 :                 flag(general_max_14bit_constraint_flag);
     128           0 :                 xu(24, general_reserved_zero_33bits, zero, 0, 0);
     129           0 :                 xu(9, general_reserved_zero_33bits, zero, 0, 0);
     130             :             } else {
     131           0 :                 xu(24, general_reserved_zero_34bits, zero, 0, 0);
     132           0 :                 xu(10, general_reserved_zero_34bits, zero, 0, 0);
     133             :             }
     134             :         } else {
     135         188 :             xu(24, general_reserved_zero_43bits, zero, 0, 0);
     136         188 :             xu(19, general_reserved_zero_43bits, zero, 0, 0);
     137             :         }
     138             : 
     139         188 :         if (profile_compatible(1) || profile_compatible(2) ||
     140           0 :             profile_compatible(3) || profile_compatible(4) ||
     141           0 :             profile_compatible(5) || profile_compatible(9)) {
     142         188 :             flag(general_inbld_flag);
     143             :         } else {
     144           0 :             xu(1, general_reserved_zero_bit, zero, 0, 0);
     145             :         }
     146             : #undef profile_compatible
     147             :     }
     148             : 
     149         188 :     u(8, general_level_idc, 0, 255);
     150             : 
     151         268 :     for (i = 0; i < max_num_sub_layers_minus1; i++) {
     152          80 :         flag(sub_layer_profile_present_flag[i]);
     153          80 :         flag(sub_layer_level_present_flag[i]);
     154             :     }
     155             : 
     156         188 :     if (max_num_sub_layers_minus1 > 0) {
     157         280 :         for (i = max_num_sub_layers_minus1; i < 8; i++) {
     158         240 :             av_unused int zero = 0;
     159         240 :             xu(2, reserved_zero_2bits, zero, 0, 0);
     160             :         }
     161             :     }
     162             : 
     163         268 :     for (i = 0; i < max_num_sub_layers_minus1; i++) {
     164          80 :         if (current->sub_layer_profile_present_flag[i])
     165           0 :             return AVERROR_PATCHWELCOME;
     166          80 :         if (current->sub_layer_level_present_flag[i])
     167           0 :             return AVERROR_PATCHWELCOME;
     168             :     }
     169             : 
     170         188 :     return 0;
     171             : }
     172             : 
     173          16 : static int FUNC(sub_layer_hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
     174             :                                           H265RawHRDParameters *hrd,
     175             :                                           int nal, int sub_layer_id)
     176             : {
     177             :     H265RawSubLayerHRDParameters *current;
     178             :     int err, i;
     179             : 
     180          16 :     if (nal)
     181           8 :         current = &hrd->nal_sub_layer_hrd_parameters[sub_layer_id];
     182             :     else
     183           8 :         current = &hrd->vcl_sub_layer_hrd_parameters[sub_layer_id];
     184             : 
     185          32 :     for (i = 0; i <= hrd->cpb_cnt_minus1[sub_layer_id]; i++) {
     186          16 :         ue(bit_rate_value_minus1[i], 0, UINT32_MAX - 1);
     187          16 :         ue(cpb_size_value_minus1[i], 0, UINT32_MAX - 1);
     188          16 :         if (hrd->sub_pic_hrd_params_present_flag) {
     189           8 :             ue(cpb_size_du_value_minus1[i], 0, UINT32_MAX - 1);
     190           8 :             ue(bit_rate_du_value_minus1[i], 0, UINT32_MAX - 1);
     191             :         }
     192          16 :         flag(cbr_flag[i]);
     193             :     }
     194             : 
     195          16 :     return 0;
     196             : }
     197             : 
     198           8 : static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
     199             :                                 H265RawHRDParameters *current, int common_inf_present_flag,
     200             :                                 int max_num_sub_layers_minus1)
     201             : {
     202             :     int err, i;
     203             : 
     204           8 :     if (common_inf_present_flag) {
     205           8 :         flag(nal_hrd_parameters_present_flag);
     206           8 :         flag(vcl_hrd_parameters_present_flag);
     207             : 
     208           8 :         if (current->nal_hrd_parameters_present_flag ||
     209           0 :             current->vcl_hrd_parameters_present_flag) {
     210           8 :             flag(sub_pic_hrd_params_present_flag);
     211           8 :             if (current->sub_pic_hrd_params_present_flag) {
     212           4 :                 u(8, tick_divisor_minus2, 0, 255);
     213           4 :                 u(5, du_cpb_removal_delay_increment_length_minus1, 0, 31);
     214           4 :                 flag(sub_pic_cpb_params_in_pic_timing_sei_flag);
     215           4 :                 u(5, dpb_output_delay_du_length_minus1, 0, 31);
     216             :             }
     217             : 
     218           8 :             u(4, bit_rate_scale, 0, 15);
     219           8 :             u(4, cpb_size_scale, 0, 15);
     220           8 :             if (current->sub_pic_hrd_params_present_flag)
     221           4 :                 u(4, cpb_size_du_scale, 0, 15);
     222             : 
     223           8 :             u(5, initial_cpb_removal_delay_length_minus1, 0, 31);
     224           8 :             u(5, au_cpb_removal_delay_length_minus1,      0, 31);
     225           8 :             u(5, dpb_output_delay_length_minus1,          0, 31);
     226             :         } else {
     227           0 :             infer(sub_pic_hrd_params_present_flag, 0);
     228             : 
     229           0 :             infer(initial_cpb_removal_delay_length_minus1, 23);
     230           0 :             infer(au_cpb_removal_delay_length_minus1,      23);
     231           0 :             infer(dpb_output_delay_length_minus1,          23);
     232             :         }
     233             :     }
     234             : 
     235          16 :     for (i = 0; i <= max_num_sub_layers_minus1; i++) {
     236           8 :         flag(fixed_pic_rate_general_flag[i]);
     237             : 
     238           8 :         if (!current->fixed_pic_rate_general_flag[i])
     239           4 :             flag(fixed_pic_rate_within_cvs_flag[i]);
     240             :         else
     241           4 :             infer(fixed_pic_rate_within_cvs_flag[i], 1);
     242             : 
     243           8 :         if (current->fixed_pic_rate_within_cvs_flag[i]) {
     244           4 :             ue(elemental_duration_in_tc_minus1[i], 0, 2047);
     245           4 :             infer(low_delay_hrd_flag[i], 0);
     246             :         } else
     247           4 :             flag(low_delay_hrd_flag[i]);
     248             : 
     249           8 :         if (!current->low_delay_hrd_flag[i])
     250           8 :             ue(cpb_cnt_minus1[i], 0, 31);
     251             :         else
     252           0 :             infer(cpb_cnt_minus1[i], 0);
     253             : 
     254           8 :         if (current->nal_hrd_parameters_present_flag)
     255           8 :             CHECK(FUNC(sub_layer_hrd_parameters)(ctx, rw, current, 0, i));
     256           8 :         if (current->vcl_hrd_parameters_present_flag)
     257           8 :             CHECK(FUNC(sub_layer_hrd_parameters)(ctx, rw, current, 1, i));
     258             :     }
     259             : 
     260           8 :     return 0;
     261             : }
     262             : 
     263          36 : static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
     264             :                                 H265RawVUI *current, const H265RawSPS *sps)
     265             : {
     266             :     int err;
     267             : 
     268          36 :     flag(aspect_ratio_info_present_flag);
     269          36 :     if (current->aspect_ratio_info_present_flag) {
     270          12 :         u(8, aspect_ratio_idc, 0, 255);
     271          12 :         if (current->aspect_ratio_idc == 255) {
     272           0 :             u(16, sar_width,  0, 65535);
     273           0 :             u(16, sar_height, 0, 65535);
     274             :         }
     275             :     } else {
     276          24 :         infer(aspect_ratio_idc, 0);
     277             :     }
     278             : 
     279          36 :     flag(overscan_info_present_flag);
     280          36 :     if (current->overscan_info_present_flag)
     281           0 :         flag(overscan_appropriate_flag);
     282             : 
     283          36 :     flag(video_signal_type_present_flag);
     284          36 :     if (current->video_signal_type_present_flag) {
     285           0 :         u(3, video_format, 0, 7);
     286           0 :         flag(video_full_range_flag);
     287           0 :         flag(colour_description_present_flag);
     288           0 :         if (current->colour_description_present_flag) {
     289           0 :             u(8, colour_primaries,         0, 255);
     290           0 :             u(8, transfer_characteristics, 0, 255);
     291           0 :             u(8, matrix_coefficients,      0, 255);
     292             :         } else {
     293           0 :             infer(colour_primaries,         2);
     294           0 :             infer(transfer_characteristics, 2);
     295           0 :             infer(matrix_coefficients,      2);
     296             :         }
     297             :     } else {
     298          36 :         infer(video_format,             5);
     299          36 :         infer(video_full_range_flag,    0);
     300          36 :         infer(colour_primaries,         2);
     301          36 :         infer(transfer_characteristics, 2);
     302          36 :         infer(matrix_coefficients,      2);
     303             :     }
     304             : 
     305          36 :     flag(chroma_loc_info_present_flag);
     306          36 :     if (current->chroma_loc_info_present_flag) {
     307           0 :         ue(chroma_sample_loc_type_top_field,    0, 5);
     308           0 :         ue(chroma_sample_loc_type_bottom_field, 0, 5);
     309             :     } else {
     310          36 :         infer(chroma_sample_loc_type_top_field,    0);
     311          36 :         infer(chroma_sample_loc_type_bottom_field, 0);
     312             :     }
     313             : 
     314          36 :     flag(neutral_chroma_indication_flag);
     315          36 :     flag(field_seq_flag);
     316          36 :     flag(frame_field_info_present_flag);
     317             : 
     318          36 :     flag(default_display_window_flag);
     319          36 :     if (current->default_display_window_flag) {
     320          12 :         ue(def_disp_win_left_offset,   0, 16384);
     321          12 :         ue(def_disp_win_right_offset,  0, 16384);
     322          12 :         ue(def_disp_win_top_offset,    0, 16384);
     323          12 :         ue(def_disp_win_bottom_offset, 0, 16384);
     324             :     }
     325             : 
     326          36 :     flag(vui_timing_info_present_flag);
     327          36 :     if (current->vui_timing_info_present_flag) {
     328           8 :         u(32, vui_num_units_in_tick, 1, UINT32_MAX);
     329           8 :         u(32, vui_time_scale,        1, UINT32_MAX);
     330           8 :         flag(vui_poc_proportional_to_timing_flag);
     331           8 :         if (current->vui_poc_proportional_to_timing_flag)
     332           4 :             ue(vui_num_ticks_poc_diff_one_minus1, 0, UINT32_MAX - 1);
     333             : 
     334           8 :         flag(vui_hrd_parameters_present_flag);
     335           8 :         if (current->vui_hrd_parameters_present_flag) {
     336           8 :             CHECK(FUNC(hrd_parameters)(ctx, rw, &current->hrd_parameters,
     337             :                                        1, sps->sps_max_sub_layers_minus1));
     338             :         }
     339             :     }
     340             : 
     341          36 :     flag(bitstream_restriction_flag);
     342          36 :     if (current->bitstream_restriction_flag) {
     343          24 :         flag(tiles_fixed_structure_flag);
     344          24 :         flag(motion_vectors_over_pic_boundaries_flag);
     345          24 :         flag(restricted_ref_pic_lists_flag);
     346          24 :         ue(min_spatial_segmentation_idc,  0, 4095);
     347          24 :         ue(max_bytes_per_pic_denom,       0, 16);
     348          24 :         ue(max_bits_per_min_cu_denom,     0, 16);
     349          24 :         ue(log2_max_mv_length_horizontal, 0, 16);
     350          24 :         ue(log2_max_mv_length_vertical,   0, 16);
     351             :     } else {
     352          12 :         infer(tiles_fixed_structure_flag,    0);
     353          12 :         infer(motion_vectors_over_pic_boundaries_flag, 1);
     354          12 :         infer(min_spatial_segmentation_idc,  0);
     355          12 :         infer(max_bytes_per_pic_denom,       2);
     356          12 :         infer(max_bits_per_min_cu_denom,     1);
     357          12 :         infer(log2_max_mv_length_horizontal, 15);
     358          12 :         infer(log2_max_mv_length_vertical,   15);
     359             :     }
     360             : 
     361          36 :     return 0;
     362             : }
     363             : 
     364          92 : static int FUNC(vps)(CodedBitstreamContext *ctx, RWContext *rw,
     365             :                      H265RawVPS *current)
     366             : {
     367             :     int err, i, j;
     368             : 
     369          92 :     HEADER("Video Parameter Set");
     370             : 
     371          92 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_VPS));
     372             : 
     373          92 :     u(4, vps_video_parameter_set_id, 0, 15);
     374             : 
     375          92 :     flag(vps_base_layer_internal_flag);
     376          92 :     flag(vps_base_layer_available_flag);
     377          92 :     u(6, vps_max_layers_minus1,     0, HEVC_MAX_LAYERS - 1);
     378          92 :     u(3, vps_max_sub_layers_minus1, 0, HEVC_MAX_SUB_LAYERS - 1);
     379          92 :     flag(vps_temporal_id_nesting_flag);
     380             : 
     381         164 :     if (current->vps_max_sub_layers_minus1 == 0 &&
     382          72 :         current->vps_temporal_id_nesting_flag != 1) {
     383           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid stream: "
     384             :                "vps_temporal_id_nesting_flag must be 1 if "
     385             :                "vps_max_sub_layers_minus1 is 0.\n");
     386           0 :         return AVERROR_INVALIDDATA;
     387             :     }
     388             : 
     389             :     {
     390          92 :         av_unused uint16_t ffff = 0xffff;
     391          92 :         xu(16, vps_reserved_0xffff_16bits, ffff, 0xffff, 0xffff);
     392             :     }
     393             : 
     394          92 :     CHECK(FUNC(profile_tier_level)(ctx, rw, &current->profile_tier_level,
     395             :                                    1, current->vps_max_sub_layers_minus1));
     396             : 
     397          92 :     flag(vps_sub_layer_ordering_info_present_flag);
     398         368 :     for (i = (current->vps_sub_layer_ordering_info_present_flag ?
     399          92 :               0 : current->vps_max_sub_layers_minus1);
     400          92 :          i <= current->vps_max_sub_layers_minus1; i++) {
     401          92 :         ue(vps_max_dec_pic_buffering_minus1[i], 0, HEVC_MAX_DPB_SIZE - 1);
     402          92 :         ue(vps_max_num_reorder_pics[i],         0, current->vps_max_dec_pic_buffering_minus1[i]);
     403          92 :         ue(vps_max_latency_increase_plus1[i],   0, UINT32_MAX - 1);
     404             :     }
     405          92 :     if (!current->vps_sub_layer_ordering_info_present_flag) {
     406          64 :         for (i = 0; i < current->vps_max_sub_layers_minus1; i++) {
     407          40 :             infer(vps_max_dec_pic_buffering_minus1[i],
     408             :                   current->vps_max_dec_pic_buffering_minus1[current->vps_max_sub_layers_minus1]);
     409          40 :             infer(vps_max_num_reorder_pics[i],
     410             :                   current->vps_max_num_reorder_pics[current->vps_max_sub_layers_minus1]);
     411          40 :             infer(vps_max_latency_increase_plus1[i],
     412             :                   current->vps_max_latency_increase_plus1[current->vps_max_sub_layers_minus1]);
     413             :         }
     414             :     }
     415             : 
     416          92 :     u(6, vps_max_layer_id,        0, HEVC_MAX_LAYERS - 1);
     417          92 :     ue(vps_num_layer_sets_minus1, 0, HEVC_MAX_LAYER_SETS - 1);
     418          92 :     for (i = 1; i <= current->vps_num_layer_sets_minus1; i++) {
     419           0 :         for (j = 0; j <= current->vps_max_layer_id; j++)
     420           0 :             flag(layer_id_included_flag[i][j]);
     421             :     }
     422         184 :     for (j = 0; j <= current->vps_max_layer_id; j++)
     423          92 :         infer(layer_id_included_flag[0][j], j == 0);
     424             : 
     425          92 :     flag(vps_timing_info_present_flag);
     426          92 :     if (current->vps_timing_info_present_flag) {
     427           4 :         u(32, vps_num_units_in_tick, 1, UINT32_MAX);
     428           4 :         u(32, vps_time_scale,        1, UINT32_MAX);
     429           4 :         flag(vps_poc_proportional_to_timing_flag);
     430           4 :         if (current->vps_poc_proportional_to_timing_flag)
     431           4 :             ue(vps_num_ticks_poc_diff_one_minus1, 0, UINT32_MAX - 1);
     432           4 :         ue(vps_num_hrd_parameters, 0, current->vps_num_layer_sets_minus1 + 1);
     433           4 :         for (i = 0; i < current->vps_num_hrd_parameters; i++) {
     434           0 :             ue(hrd_layer_set_idx[i],
     435             :                current->vps_base_layer_internal_flag ? 0 : 1,
     436             :                current->vps_num_layer_sets_minus1);
     437           0 :             if (i > 0)
     438           0 :                 flag(cprms_present_flag[i]);
     439             :             else
     440           0 :                 infer(cprms_present_flag[0], 1);
     441             : 
     442           0 :             CHECK(FUNC(hrd_parameters)(ctx, rw, &current->hrd_parameters[i],
     443             :                                        current->cprms_present_flag[i],
     444             :                                        current->vps_max_sub_layers_minus1));
     445             :         }
     446             :     }
     447             : 
     448          92 :     flag(vps_extension_flag);
     449          92 :     if (current->vps_extension_flag)
     450           0 :         CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
     451             : 
     452          92 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
     453             : 
     454          92 :     return 0;
     455             : }
     456             : 
     457        4498 : static int FUNC(st_ref_pic_set)(CodedBitstreamContext *ctx, RWContext *rw,
     458             :                                 H265RawSTRefPicSet *current, int st_rps_idx,
     459             :                                 const H265RawSPS *sps)
     460             : {
     461             :     int err, i, j;
     462             : 
     463        4498 :     if (st_rps_idx != 0)
     464        4354 :         flag(inter_ref_pic_set_prediction_flag);
     465             :     else
     466         144 :         infer(inter_ref_pic_set_prediction_flag, 0);
     467             : 
     468        4498 :     if (current->inter_ref_pic_set_prediction_flag) {
     469             :         unsigned int ref_rps_idx, num_delta_pocs;
     470             :         const H265RawSTRefPicSet *ref;
     471             :         int delta_rps, d_poc;
     472             :         int ref_delta_poc_s0[HEVC_MAX_REFS], ref_delta_poc_s1[HEVC_MAX_REFS];
     473             :         int delta_poc_s0[HEVC_MAX_REFS], delta_poc_s1[HEVC_MAX_REFS];
     474             :         uint8_t used_by_curr_pic_s0[HEVC_MAX_REFS],
     475             :                 used_by_curr_pic_s1[HEVC_MAX_REFS];
     476             : 
     477         988 :         if (st_rps_idx == sps->num_short_term_ref_pic_sets)
     478         372 :             ue(delta_idx_minus1, 0, st_rps_idx - 1);
     479             :         else
     480         616 :             infer(delta_idx_minus1, 0);
     481             : 
     482         988 :         ref_rps_idx = st_rps_idx - (current->delta_idx_minus1 + 1);
     483         988 :         ref = &sps->st_ref_pic_set[ref_rps_idx];
     484         988 :         num_delta_pocs = ref->num_negative_pics + ref->num_positive_pics;
     485             : 
     486         988 :         flag(delta_rps_sign);
     487         988 :         ue(abs_delta_rps_minus1, 0, INT16_MAX);
     488        1976 :         delta_rps = (1 - 2 * current->delta_rps_sign) *
     489         988 :             (current->abs_delta_rps_minus1 + 1);
     490             : 
     491        5368 :         for (j = 0; j <= num_delta_pocs; j++) {
     492        4380 :             flag(used_by_curr_pic_flag[j]);
     493        4380 :             if (!current->used_by_curr_pic_flag[j])
     494         994 :                 flag(use_delta_flag[j]);
     495             :             else
     496        3386 :                 infer(use_delta_flag[j], 1);
     497             :         }
     498             : 
     499             :         // Since the stored form of an RPS here is actually the delta-step
     500             :         // form used when inter_ref_pic_set_prediction_flag is not set, we
     501             :         // need to reconstruct that here in order to be able to refer to
     502             :         // the RPS later (which is required for parsing, because we don't
     503             :         // even know what syntax elements appear without it).  Therefore,
     504             :         // this code takes the delta-step form of the reference set, turns
     505             :         // it into the delta-array form, applies the prediction process of
     506             :         // 7.4.8, converts the result back to the delta-step form, and
     507             :         // stores that as the current set for future use.  Note that the
     508             :         // inferences here mean that writers using prediction will need
     509             :         // to fill in the delta-step values correctly as well - since the
     510             :         // whole RPS prediction process is somewhat overly sophisticated,
     511             :         // this hopefully forms a useful check for them to ensure their
     512             :         // predicted form actually matches what was intended rather than
     513             :         // an onerous additional requirement.
     514             : 
     515         988 :         d_poc = 0;
     516        3258 :         for (i = 0; i < ref->num_negative_pics; i++) {
     517        2270 :             d_poc -= ref->delta_poc_s0_minus1[i] + 1;
     518        2270 :             ref_delta_poc_s0[i] = d_poc;
     519             :         }
     520         988 :         d_poc = 0;
     521        2110 :         for (i = 0; i < ref->num_positive_pics; i++) {
     522        1122 :             d_poc += ref->delta_poc_s1_minus1[i] + 1;
     523        1122 :             ref_delta_poc_s1[i] = d_poc;
     524             :         }
     525             : 
     526         988 :         i = 0;
     527        2110 :         for (j = ref->num_positive_pics - 1; j >= 0; j--) {
     528        1122 :             d_poc = ref_delta_poc_s1[j] + delta_rps;
     529        1122 :             if (d_poc < 0 && current->use_delta_flag[ref->num_negative_pics + j]) {
     530         288 :                 delta_poc_s0[i] = d_poc;
     531         576 :                 used_by_curr_pic_s0[i++] =
     532         576 :                     current->used_by_curr_pic_flag[ref->num_negative_pics + j];
     533             :             }
     534             :         }
     535         988 :         if (delta_rps < 0 && current->use_delta_flag[num_delta_pocs]) {
     536         220 :             delta_poc_s0[i] = delta_rps;
     537         440 :             used_by_curr_pic_s0[i++] =
     538         220 :                 current->used_by_curr_pic_flag[num_delta_pocs];
     539             :         }
     540        3258 :         for (j = 0; j < ref->num_negative_pics; j++) {
     541        2270 :             d_poc = ref_delta_poc_s0[j] + delta_rps;
     542        2270 :             if (d_poc < 0 && current->use_delta_flag[j]) {
     543        1640 :                 delta_poc_s0[i] = d_poc;
     544        1640 :                 used_by_curr_pic_s0[i++] = current->used_by_curr_pic_flag[j];
     545             :             }
     546             :         }
     547             : 
     548         988 :         infer(num_negative_pics, i);
     549        3136 :         for (i = 0; i < current->num_negative_pics; i++) {
     550        2148 :             infer(delta_poc_s0_minus1[i],
     551             :                   -(delta_poc_s0[i] - (i == 0 ? 0 : delta_poc_s0[i - 1])) - 1);
     552        2148 :             infer(used_by_curr_pic_s0_flag[i], used_by_curr_pic_s0[i]);
     553             :         }
     554             : 
     555         988 :         i = 0;
     556        3258 :         for (j = ref->num_negative_pics - 1; j >= 0; j--) {
     557        2270 :             d_poc = ref_delta_poc_s0[j] + delta_rps;
     558        2270 :             if (d_poc > 0 && current->use_delta_flag[j]) {
     559           0 :                 delta_poc_s1[i] = d_poc;
     560           0 :                 used_by_curr_pic_s1[i++] = current->used_by_curr_pic_flag[j];
     561             :             }
     562             :         }
     563         988 :         if (delta_rps > 0 && current->use_delta_flag[num_delta_pocs]) {
     564         466 :             delta_poc_s1[i] = delta_rps;
     565         932 :             used_by_curr_pic_s1[i++] =
     566         466 :                 current->used_by_curr_pic_flag[num_delta_pocs];
     567             :         }
     568        2110 :         for (j = 0; j < ref->num_positive_pics; j++) {
     569        1122 :             d_poc = ref_delta_poc_s1[j] + delta_rps;
     570        1122 :             if (d_poc > 0 && current->use_delta_flag[ref->num_negative_pics + j]) {
     571         772 :                 delta_poc_s1[i] = d_poc;
     572        1544 :                 used_by_curr_pic_s1[i++] =
     573        1544 :                     current->used_by_curr_pic_flag[ref->num_negative_pics + j];
     574             :             }
     575             :         }
     576             : 
     577         988 :         infer(num_positive_pics, i);
     578        2226 :         for (i = 0; i < current->num_positive_pics; i++) {
     579        1238 :             infer(delta_poc_s1_minus1[i],
     580             :                   delta_poc_s1[i] - (i == 0 ? 0 : delta_poc_s1[i - 1]) - 1);
     581        1238 :             infer(used_by_curr_pic_s1_flag[i], used_by_curr_pic_s1[i]);
     582             :         }
     583             : 
     584             :     } else {
     585        3510 :         ue(num_negative_pics, 0, 15);
     586        3510 :         ue(num_positive_pics, 0, 15 - current->num_negative_pics);
     587             : 
     588       13158 :         for (i = 0; i < current->num_negative_pics; i++) {
     589        9648 :             ue(delta_poc_s0_minus1[i], 0, INT16_MAX);
     590        9648 :             flag(used_by_curr_pic_s0_flag[i]);
     591             :         }
     592             : 
     593        4320 :         for (i = 0; i < current->num_positive_pics; i++) {
     594         810 :             ue(delta_poc_s1_minus1[i], 0, INT16_MAX);
     595         810 :             flag(used_by_curr_pic_s1_flag[i]);
     596             :         }
     597             :     }
     598             : 
     599        4498 :     return 0;
     600             : }
     601             : 
     602         176 : static int FUNC(scaling_list_data)(CodedBitstreamContext *ctx, RWContext *rw,
     603             :                                    H265RawScalingList *current)
     604             : {
     605             :     int sizeId, matrixId;
     606             :     int err, n, i;
     607             : 
     608         880 :     for (sizeId = 0; sizeId < 4; sizeId++) {
     609        4224 :         for (matrixId = 0; matrixId < 6; matrixId += (sizeId == 3 ? 3 : 1)) {
     610        3520 :             flag(scaling_list_pred_mode_flag[sizeId][matrixId]);
     611        3520 :             if (!current->scaling_list_pred_mode_flag[sizeId][matrixId]) {
     612        1564 :                 ue(scaling_list_pred_matrix_id_delta[sizeId][matrixId],
     613             :                    0, sizeId == 3 ? matrixId / 3 : matrixId);
     614             :             } else {
     615        1956 :                 n = FFMIN(64, 1 << (4 + (sizeId << 1)));
     616        1956 :                 if (sizeId > 1)
     617        1188 :                     se(scaling_list_dc_coef_minus8[sizeId - 2][matrixId], -7, +247);
     618      107556 :                 for (i = 0; i < n; i++) {
     619      105600 :                     xse(scaling_list_delta_coeff,
     620             :                         current->scaling_list_delta_coeff[sizeId][matrixId][i],
     621             :                         -128, +127);
     622             :                 }
     623             :             }
     624             :         }
     625             :     }
     626             : 
     627         176 :     return 0;
     628             : }
     629             : 
     630           0 : static int FUNC(sps_range_extension)(CodedBitstreamContext *ctx, RWContext *rw,
     631             :                                      H265RawSPS *current)
     632             : {
     633             :     int err;
     634             : 
     635           0 :     flag(transform_skip_rotation_enabled_flag);
     636           0 :     flag(transform_skip_context_enabled_flag);
     637           0 :     flag(implicit_rdpcm_enabled_flag);
     638           0 :     flag(explicit_rdpcm_enabled_flag);
     639           0 :     flag(extended_precision_processing_flag);
     640           0 :     flag(intra_smoothing_disabled_flag);
     641           0 :     flag(high_precision_offsets_enabled_flag);
     642           0 :     flag(persistent_rice_adaptation_enabled_flag);
     643           0 :     flag(cabac_bypass_alignment_enabled_flag);
     644             : 
     645           0 :     return 0;
     646             : }
     647             : 
     648           0 : static int FUNC(sps_scc_extension)(CodedBitstreamContext *ctx, RWContext *rw,
     649             :                                    H265RawSPS *current)
     650             : {
     651             :     int err, comp, i;
     652             : 
     653           0 :     flag(sps_curr_pic_ref_enabled_flag);
     654             : 
     655           0 :     flag(palette_mode_enabled_flag);
     656           0 :     if (current->palette_mode_enabled_flag) {
     657           0 :         ue(palette_max_size, 0, 64);
     658           0 :         ue(delta_palette_max_predictor_size, 0, 128);
     659             : 
     660           0 :         flag(sps_palette_predictor_initializer_present_flag);
     661           0 :         if (current->sps_palette_predictor_initializer_present_flag) {
     662           0 :             ue(sps_num_palette_predictor_initializer_minus1, 0, 128);
     663           0 :             for (comp = 0; comp < (current->chroma_format_idc ? 3 : 1); comp++) {
     664           0 :                 int bit_depth = comp == 0 ? current->bit_depth_luma_minus8 + 8
     665           0 :                                           : current->bit_depth_chroma_minus8 + 8;
     666           0 :                 for (i = 0; i <= current->sps_num_palette_predictor_initializer_minus1; i++)
     667           0 :                     u(bit_depth, sps_palette_predictor_initializers[comp][i],
     668             :                       0, (1 << bit_depth) - 1);
     669             :             }
     670             :         }
     671             :     }
     672             : 
     673           0 :     u(2, motion_vector_resolution_control_idc, 0, 2);
     674           0 :     flag(intra_boundary_filtering_disable_flag);
     675             : 
     676           0 :     return 0;
     677             : }
     678             : 
     679          96 : static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
     680             :                      H265RawSPS *current)
     681             : {
     682          96 :     CodedBitstreamH265Context *h265 = ctx->priv_data;
     683             :     const H265RawVPS *vps;
     684             :     int err, i;
     685             :     unsigned int min_cb_log2_size_y, ctb_log2_size_y,
     686             :                  min_cb_size_y,   min_tb_log2_size_y;
     687             : 
     688          96 :     HEADER("Sequence Parameter Set");
     689             : 
     690          96 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_SPS));
     691             : 
     692          96 :     u(4, sps_video_parameter_set_id, 0, 15);
     693          96 :     h265->active_vps = vps = h265->vps[current->sps_video_parameter_set_id];
     694             : 
     695          96 :     u(3, sps_max_sub_layers_minus1, 0, HEVC_MAX_SUB_LAYERS - 1);
     696          96 :     flag(sps_temporal_id_nesting_flag);
     697          96 :     if (vps) {
     698          96 :         if (vps->vps_max_sub_layers_minus1 > current->sps_max_sub_layers_minus1) {
     699           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid stream: "
     700             :                    "sps_max_sub_layers_minus1 (%d) must be less than or equal to "
     701             :                    "vps_max_sub_layers_minus1 (%d).\n",
     702           0 :                    vps->vps_max_sub_layers_minus1,
     703           0 :                    current->sps_max_sub_layers_minus1);
     704           0 :             return AVERROR_INVALIDDATA;
     705             :         }
     706         172 :         if (vps->vps_temporal_id_nesting_flag &&
     707          76 :             !current->sps_temporal_id_nesting_flag) {
     708           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid stream: "
     709             :                    "sps_temporal_id_nesting_flag must be 1 if "
     710             :                    "vps_temporal_id_nesting_flag is 1.\n");
     711           0 :             return AVERROR_INVALIDDATA;
     712             :         }
     713             :     }
     714             : 
     715          96 :     CHECK(FUNC(profile_tier_level)(ctx, rw, &current->profile_tier_level,
     716             :                                    1, current->sps_max_sub_layers_minus1));
     717             : 
     718          96 :     ue(sps_seq_parameter_set_id, 0, 15);
     719             : 
     720          96 :     ue(chroma_format_idc, 0, 3);
     721          96 :     if (current->chroma_format_idc == 3)
     722           0 :         flag(separate_colour_plane_flag);
     723             :     else
     724          96 :         infer(separate_colour_plane_flag, 0);
     725             : 
     726          96 :     ue(pic_width_in_luma_samples,  1, HEVC_MAX_WIDTH);
     727          96 :     ue(pic_height_in_luma_samples, 1, HEVC_MAX_HEIGHT);
     728             : 
     729          96 :     flag(conformance_window_flag);
     730          96 :     if (current->conformance_window_flag) {
     731          72 :         ue(conf_win_left_offset,   0, current->pic_width_in_luma_samples);
     732          72 :         ue(conf_win_right_offset,  0, current->pic_width_in_luma_samples);
     733          72 :         ue(conf_win_top_offset,    0, current->pic_height_in_luma_samples);
     734          72 :         ue(conf_win_bottom_offset, 0, current->pic_height_in_luma_samples);
     735             :     } else {
     736          24 :         infer(conf_win_left_offset,   0);
     737          24 :         infer(conf_win_right_offset,  0);
     738          24 :         infer(conf_win_top_offset,    0);
     739          24 :         infer(conf_win_bottom_offset, 0);
     740             :     }
     741             : 
     742          96 :     ue(bit_depth_luma_minus8,   0, 8);
     743          96 :     ue(bit_depth_chroma_minus8, 0, 8);
     744             : 
     745          96 :     ue(log2_max_pic_order_cnt_lsb_minus4, 0, 12);
     746             : 
     747          96 :     flag(sps_sub_layer_ordering_info_present_flag);
     748         384 :     for (i = (current->sps_sub_layer_ordering_info_present_flag ?
     749          96 :               0 : current->sps_max_sub_layers_minus1);
     750          96 :          i <= current->sps_max_sub_layers_minus1; i++) {
     751          96 :         ue(sps_max_dec_pic_buffering_minus1[i], 0, HEVC_MAX_DPB_SIZE - 1);
     752          96 :         ue(sps_max_num_reorder_pics[i],         0, current->sps_max_dec_pic_buffering_minus1[i]);
     753          96 :         ue(sps_max_latency_increase_plus1[i],   0, UINT32_MAX - 1);
     754             :     }
     755          96 :     if (!current->sps_sub_layer_ordering_info_present_flag) {
     756          64 :         for (i = 0; i < current->sps_max_sub_layers_minus1; i++) {
     757          40 :             infer(sps_max_dec_pic_buffering_minus1[i],
     758             :                   current->sps_max_dec_pic_buffering_minus1[current->sps_max_sub_layers_minus1]);
     759          40 :             infer(sps_max_num_reorder_pics[i],
     760             :                   current->sps_max_num_reorder_pics[current->sps_max_sub_layers_minus1]);
     761          40 :             infer(sps_max_latency_increase_plus1[i],
     762             :                   current->sps_max_latency_increase_plus1[current->sps_max_sub_layers_minus1]);
     763             :         }
     764             :     }
     765             : 
     766          96 :     ue(log2_min_luma_coding_block_size_minus3,   0, 3);
     767          96 :     min_cb_log2_size_y = current->log2_min_luma_coding_block_size_minus3 + 3;
     768             : 
     769          96 :     ue(log2_diff_max_min_luma_coding_block_size, 0, 3);
     770          96 :     ctb_log2_size_y = min_cb_log2_size_y +
     771          96 :         current->log2_diff_max_min_luma_coding_block_size;
     772             : 
     773          96 :     min_cb_size_y = 1 << min_cb_log2_size_y;
     774         192 :     if (current->pic_width_in_luma_samples  % min_cb_size_y ||
     775          96 :         current->pic_height_in_luma_samples % min_cb_size_y) {
     776           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid dimensions: %ux%u not divisible "
     777           0 :                "by MinCbSizeY = %u.\n", current->pic_width_in_luma_samples,
     778           0 :                current->pic_height_in_luma_samples, min_cb_size_y);
     779           0 :         return AVERROR_INVALIDDATA;
     780             :     }
     781             : 
     782          96 :     ue(log2_min_luma_transform_block_size_minus2, 0, min_cb_log2_size_y - 3);
     783          96 :     min_tb_log2_size_y = current->log2_min_luma_transform_block_size_minus2 + 2;
     784             : 
     785          96 :     ue(log2_diff_max_min_luma_transform_block_size,
     786             :        0, FFMIN(ctb_log2_size_y, 5) - min_tb_log2_size_y);
     787             : 
     788          96 :     ue(max_transform_hierarchy_depth_inter,
     789             :        0, ctb_log2_size_y - min_tb_log2_size_y);
     790          96 :     ue(max_transform_hierarchy_depth_intra,
     791             :        0, ctb_log2_size_y - min_tb_log2_size_y);
     792             : 
     793          96 :     flag(scaling_list_enabled_flag);
     794          96 :     if (current->scaling_list_enabled_flag) {
     795           8 :         flag(sps_scaling_list_data_present_flag);
     796           8 :         if (current->sps_scaling_list_data_present_flag)
     797           8 :             CHECK(FUNC(scaling_list_data)(ctx, rw, &current->scaling_list));
     798             :     } else {
     799          88 :         infer(sps_scaling_list_data_present_flag, 0);
     800             :     }
     801             : 
     802          96 :     flag(amp_enabled_flag);
     803          96 :     flag(sample_adaptive_offset_enabled_flag);
     804             : 
     805          96 :     flag(pcm_enabled_flag);
     806          96 :     if (current->pcm_enabled_flag) {
     807           4 :         u(4, pcm_sample_bit_depth_luma_minus1,
     808             :           0, current->bit_depth_luma_minus8 + 8 - 1);
     809           4 :         u(4, pcm_sample_bit_depth_chroma_minus1,
     810             :           0, current->bit_depth_chroma_minus8 + 8 - 1);
     811             : 
     812           4 :         ue(log2_min_pcm_luma_coding_block_size_minus3,
     813             :            FFMIN(min_cb_log2_size_y, 5) - 3, FFMIN(ctb_log2_size_y, 5) - 3);
     814           4 :         ue(log2_diff_max_min_pcm_luma_coding_block_size,
     815             :            0, FFMIN(ctb_log2_size_y, 5) - (current->log2_min_pcm_luma_coding_block_size_minus3 + 3));
     816             : 
     817           4 :         flag(pcm_loop_filter_disabled_flag);
     818             :     }
     819             : 
     820          96 :     ue(num_short_term_ref_pic_sets, 0, HEVC_MAX_SHORT_TERM_REF_PIC_SETS);
     821         864 :     for (i = 0; i < current->num_short_term_ref_pic_sets; i++)
     822         768 :         CHECK(FUNC(st_ref_pic_set)(ctx, rw, &current->st_ref_pic_set[i], i, current));
     823             : 
     824          96 :     flag(long_term_ref_pics_present_flag);
     825          96 :     if (current->long_term_ref_pics_present_flag) {
     826          28 :         ue(num_long_term_ref_pics_sps, 0, HEVC_MAX_LONG_TERM_REF_PICS);
     827          60 :         for (i = 0; i < current->num_long_term_ref_pics_sps; i++) {
     828          32 :             u(current->log2_max_pic_order_cnt_lsb_minus4 + 4,
     829             :               lt_ref_pic_poc_lsb_sps[i],
     830             :               0, (1 << (current->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1);
     831          32 :             flag(used_by_curr_pic_lt_sps_flag[i]);
     832             :         }
     833             :     }
     834             : 
     835          96 :     flag(sps_temporal_mvp_enabled_flag);
     836          96 :     flag(strong_intra_smoothing_enabled_flag);
     837             : 
     838          96 :     flag(vui_parameters_present_flag);
     839          96 :     if (current->vui_parameters_present_flag)
     840          36 :         CHECK(FUNC(vui_parameters)(ctx, rw, &current->vui, current));
     841             : 
     842          96 :     flag(sps_extension_present_flag);
     843          96 :     if (current->sps_extension_present_flag) {
     844           0 :         flag(sps_range_extension_flag);
     845           0 :         flag(sps_multilayer_extension_flag);
     846           0 :         flag(sps_3d_extension_flag);
     847           0 :         flag(sps_scc_extension_flag);
     848           0 :         u(4, sps_extension_4bits, 0, (1 << 4) - 1);
     849             :     }
     850             : 
     851          96 :     if (current->sps_range_extension_flag)
     852           0 :         CHECK(FUNC(sps_range_extension)(ctx, rw, current));
     853          96 :     if (current->sps_multilayer_extension_flag)
     854           0 :         return AVERROR_PATCHWELCOME;
     855          96 :     if (current->sps_3d_extension_flag)
     856           0 :         return AVERROR_PATCHWELCOME;
     857          96 :     if (current->sps_scc_extension_flag)
     858           0 :         CHECK(FUNC(sps_scc_extension)(ctx, rw, current));
     859          96 :     if (current->sps_extension_4bits)
     860           0 :         CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
     861             : 
     862          96 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
     863             : 
     864          96 :     return 0;
     865             : }
     866             : 
     867           0 : static int FUNC(pps_range_extension)(CodedBitstreamContext *ctx, RWContext *rw,
     868             :                                      H265RawPPS *current)
     869             : {
     870           0 :     CodedBitstreamH265Context *h265 = ctx->priv_data;
     871           0 :     const H265RawSPS *sps = h265->active_sps;
     872             :     int err, i;
     873             : 
     874           0 :     if (current->transform_skip_enabled_flag)
     875           0 :         ue(log2_max_transform_skip_block_size_minus2, 0, 3);
     876           0 :     flag(cross_component_prediction_enabled_flag);
     877             : 
     878           0 :     flag(chroma_qp_offset_list_enabled_flag);
     879           0 :     if (current->chroma_qp_offset_list_enabled_flag) {
     880           0 :         ue(diff_cu_chroma_qp_offset_depth,
     881             :            0, sps->log2_diff_max_min_luma_coding_block_size);
     882           0 :         ue(chroma_qp_offset_list_len_minus1, 0, 5);
     883           0 :         for (i = 0; i <= current->chroma_qp_offset_list_len_minus1; i++) {
     884           0 :             se(cb_qp_offset_list[i], -12, +12);
     885           0 :             se(cr_qp_offset_list[i], -12, +12);
     886             :         }
     887             :     }
     888             : 
     889           0 :     ue(log2_sao_offset_scale_luma,   0, FFMAX(0, sps->bit_depth_luma_minus8   - 2));
     890           0 :     ue(log2_sao_offset_scale_chroma, 0, FFMAX(0, sps->bit_depth_chroma_minus8 - 2));
     891             : 
     892           0 :     return 0;
     893             : }
     894             : 
     895           0 : static int FUNC(pps_scc_extension)(CodedBitstreamContext *ctx, RWContext *rw,
     896             :                                    H265RawPPS *current)
     897             : {
     898             :     int err, comp, i;
     899             : 
     900           0 :     flag(pps_curr_pic_ref_enabled_flag);
     901             : 
     902           0 :     flag(residual_adaptive_colour_transform_enabled_flag);
     903           0 :     if (current->residual_adaptive_colour_transform_enabled_flag) {
     904           0 :         flag(pps_slice_act_qp_offsets_present_flag);
     905           0 :         se(pps_act_y_qp_offset_plus5,  -7, +17);
     906           0 :         se(pps_act_cb_qp_offset_plus5, -7, +17);
     907           0 :         se(pps_act_cr_qp_offset_plus3, -9, +15);
     908             :     } else {
     909           0 :         infer(pps_slice_act_qp_offsets_present_flag, 0);
     910           0 :         infer(pps_act_y_qp_offset_plus5,  0);
     911           0 :         infer(pps_act_cb_qp_offset_plus5, 0);
     912           0 :         infer(pps_act_cr_qp_offset_plus3, 0);
     913             :     }
     914             : 
     915           0 :     flag(pps_palette_predictor_initializer_present_flag);
     916           0 :     if (current->pps_palette_predictor_initializer_present_flag) {
     917           0 :         ue(pps_num_palette_predictor_initializer, 0, 128);
     918           0 :         if (current->pps_num_palette_predictor_initializer > 0) {
     919           0 :             flag(monochrome_palette_flag);
     920           0 :             ue(luma_bit_depth_entry_minus8, 0, 8);
     921           0 :             if (!current->monochrome_palette_flag)
     922           0 :                 ue(chroma_bit_depth_entry_minus8, 0, 8);
     923           0 :             for (comp = 0; comp < (current->monochrome_palette_flag ? 1 : 3); comp++) {
     924           0 :                 int bit_depth = comp == 0 ? current->luma_bit_depth_entry_minus8 + 8
     925           0 :                                           : current->chroma_bit_depth_entry_minus8 + 8;
     926           0 :                 for (i = 0; i < current->pps_num_palette_predictor_initializer; i++)
     927           0 :                     u(bit_depth, pps_palette_predictor_initializers[comp][i],
     928             :                       0, (1 << bit_depth) - 1);
     929             :             }
     930             :         }
     931             :     }
     932             : 
     933           0 :     return 0;
     934             : }
     935             : 
     936         546 : static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
     937             :                      H265RawPPS *current)
     938             : {
     939         546 :     CodedBitstreamH265Context *h265 = ctx->priv_data;
     940             :     const H265RawSPS *sps;
     941             :     int err, i;
     942             : 
     943         546 :     HEADER("Picture Parameter Set");
     944             : 
     945         546 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_PPS));
     946             : 
     947         546 :     ue(pps_pic_parameter_set_id, 0, 63);
     948         546 :     ue(pps_seq_parameter_set_id, 0, 15);
     949         546 :     sps = h265->sps[current->pps_seq_parameter_set_id];
     950         546 :     if (!sps) {
     951           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
     952           0 :                current->pps_seq_parameter_set_id);
     953           0 :         return AVERROR_INVALIDDATA;
     954             :     }
     955         546 :     h265->active_sps = sps;
     956             : 
     957         546 :     flag(dependent_slice_segments_enabled_flag);
     958         546 :     flag(output_flag_present_flag);
     959         546 :     u(3, num_extra_slice_header_bits, 0, 7);
     960         546 :     flag(sign_data_hiding_enabled_flag);
     961         546 :     flag(cabac_init_present_flag);
     962             : 
     963         546 :     ue(num_ref_idx_l0_default_active_minus1, 0, 14);
     964         546 :     ue(num_ref_idx_l1_default_active_minus1, 0, 14);
     965             : 
     966         546 :     se(init_qp_minus26, -(26 + 6 * sps->bit_depth_luma_minus8), +25);
     967             : 
     968         546 :     flag(constrained_intra_pred_flag);
     969         546 :     flag(transform_skip_enabled_flag);
     970         546 :     flag(cu_qp_delta_enabled_flag);
     971         546 :     if (current->cu_qp_delta_enabled_flag)
     972          12 :         ue(diff_cu_qp_delta_depth,
     973             :            0, sps->log2_diff_max_min_luma_coding_block_size);
     974             :     else
     975         534 :         infer(diff_cu_qp_delta_depth, 0);
     976             : 
     977         546 :     se(pps_cb_qp_offset, -12, +12);
     978         546 :     se(pps_cr_qp_offset, -12, +12);
     979         546 :     flag(pps_slice_chroma_qp_offsets_present_flag);
     980             : 
     981         546 :     flag(weighted_pred_flag);
     982         546 :     flag(weighted_bipred_flag);
     983             : 
     984         546 :     flag(transquant_bypass_enabled_flag);
     985         546 :     flag(tiles_enabled_flag);
     986         546 :     flag(entropy_coding_sync_enabled_flag);
     987             : 
     988         546 :     if (current->tiles_enabled_flag) {
     989         210 :         ue(num_tile_columns_minus1, 0, HEVC_MAX_TILE_COLUMNS);
     990         210 :         ue(num_tile_rows_minus1,    0, HEVC_MAX_TILE_ROWS);
     991         210 :         flag(uniform_spacing_flag);
     992         210 :         if (!current->uniform_spacing_flag) {
     993        1010 :             for (i = 0; i < current->num_tile_columns_minus1; i++)
     994         808 :                 ue(column_width_minus1[i], 0, sps->pic_width_in_luma_samples);
     995        1010 :             for (i = 0; i < current->num_tile_rows_minus1; i++)
     996         808 :                 ue(row_height_minus1[i],   0, sps->pic_height_in_luma_samples);
     997             :         }
     998         210 :         flag(loop_filter_across_tiles_enabled_flag);
     999             :     } else {
    1000         336 :         infer(num_tile_columns_minus1, 0);
    1001         336 :         infer(num_tile_rows_minus1,    0);
    1002             :     }
    1003             : 
    1004         546 :     flag(pps_loop_filter_across_slices_enabled_flag);
    1005         546 :     flag(deblocking_filter_control_present_flag);
    1006         546 :     if (current->deblocking_filter_control_present_flag) {
    1007         226 :         flag(deblocking_filter_override_enabled_flag);
    1008         226 :         flag(pps_deblocking_filter_disabled_flag);
    1009         226 :         if (!current->pps_deblocking_filter_disabled_flag) {
    1010         222 :             se(pps_beta_offset_div2, -6, +6);
    1011         222 :             se(pps_tc_offset_div2,   -6, +6);
    1012             :         } else {
    1013           4 :             infer(pps_beta_offset_div2, 0);
    1014           4 :             infer(pps_tc_offset_div2,   0);
    1015             :         }
    1016             :     } else {
    1017         320 :         infer(deblocking_filter_override_enabled_flag, 0);
    1018         320 :         infer(pps_deblocking_filter_disabled_flag,     0);
    1019         320 :         infer(pps_beta_offset_div2, 0);
    1020         320 :         infer(pps_tc_offset_div2,   0);
    1021             :     }
    1022             : 
    1023         546 :     flag(pps_scaling_list_data_present_flag);
    1024         546 :     if (current->pps_scaling_list_data_present_flag)
    1025         168 :         CHECK(FUNC(scaling_list_data)(ctx, rw, &current->scaling_list));
    1026             : 
    1027         546 :     flag(lists_modification_present_flag);
    1028             : 
    1029         546 :     ue(log2_parallel_merge_level_minus2,
    1030             :        0, (sps->log2_min_luma_coding_block_size_minus3 + 3 +
    1031             :            sps->log2_diff_max_min_luma_coding_block_size - 2));
    1032             : 
    1033         546 :     flag(slice_segment_header_extension_present_flag);
    1034             : 
    1035         546 :     flag(pps_extension_present_flag);
    1036         546 :     if (current->pps_extension_present_flag) {
    1037           0 :         flag(pps_range_extension_flag);
    1038           0 :         flag(pps_multilayer_extension_flag);
    1039           0 :         flag(pps_3d_extension_flag);
    1040           0 :         flag(pps_scc_extension_flag);
    1041           0 :         u(4, pps_extension_4bits, 0, (1 << 4) - 1);
    1042             :     }
    1043         546 :     if (current->pps_range_extension_flag)
    1044           0 :         CHECK(FUNC(pps_range_extension)(ctx, rw, current));
    1045         546 :     if (current->pps_multilayer_extension_flag)
    1046           0 :         return AVERROR_PATCHWELCOME;
    1047         546 :     if (current->pps_3d_extension_flag)
    1048           0 :         return AVERROR_PATCHWELCOME;
    1049         546 :     if (current->pps_scc_extension_flag)
    1050           0 :         CHECK(FUNC(pps_scc_extension)(ctx, rw, current));
    1051         546 :     if (current->pps_extension_4bits)
    1052           0 :         CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
    1053             : 
    1054         546 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
    1055             : 
    1056         546 :     return 0;
    1057             : }
    1058             : 
    1059         120 : static int FUNC(aud)(CodedBitstreamContext *ctx, RWContext *rw,
    1060             :                      H265RawAUD *current)
    1061             : {
    1062             :     int err;
    1063             : 
    1064         120 :     HEADER("Access Unit Delimiter");
    1065             : 
    1066         120 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_AUD));
    1067             : 
    1068         120 :     u(3, pic_type, 0, 2);
    1069             : 
    1070         120 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
    1071             : 
    1072         120 :     return 0;
    1073             : }
    1074             : 
    1075        1496 : static int FUNC(ref_pic_lists_modification)(CodedBitstreamContext *ctx, RWContext *rw,
    1076             :                                             H265RawSliceHeader *current,
    1077             :                                             unsigned int num_pic_total_curr)
    1078             : {
    1079             :     unsigned int entry_size;
    1080             :     int err, i;
    1081             : 
    1082        1496 :     entry_size = av_log2(num_pic_total_curr - 1) + 1;
    1083             : 
    1084        1496 :     flag(ref_pic_list_modification_flag_l0);
    1085        1496 :     if (current->ref_pic_list_modification_flag_l0) {
    1086        2398 :         for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++)
    1087        1672 :             u(entry_size, list_entry_l0[i], 0, num_pic_total_curr - 1);
    1088             :     }
    1089             : 
    1090        1496 :     if (current->slice_type == HEVC_SLICE_B) {
    1091        1496 :         flag(ref_pic_list_modification_flag_l1);
    1092        1496 :         if (current->ref_pic_list_modification_flag_l1) {
    1093        1436 :             for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++)
    1094        1010 :                 u(entry_size, list_entry_l1[i], 0, num_pic_total_curr - 1);
    1095             :         }
    1096             :     }
    1097             : 
    1098        1496 :     return 0;
    1099             : }
    1100             : 
    1101         510 : static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
    1102             :                                    H265RawSliceHeader *current)
    1103             : {
    1104         510 :     CodedBitstreamH265Context *h265 = ctx->priv_data;
    1105         510 :     const H265RawSPS *sps = h265->active_sps;
    1106             :     int err, i, j;
    1107        1020 :     int chroma = !sps->separate_colour_plane_flag &&
    1108         510 :                   sps->chroma_format_idc != 0;
    1109             : 
    1110         510 :     ue(luma_log2_weight_denom, 0, 7);
    1111         510 :     if (chroma)
    1112         510 :         se(delta_chroma_log2_weight_denom, -7, 7);
    1113             :     else
    1114           0 :         infer(delta_chroma_log2_weight_denom, 0);
    1115             : 
    1116        2538 :     for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
    1117             :         if (1 /* is not same POC and same layer_id */)
    1118        2028 :             flag(luma_weight_l0_flag[i]);
    1119             :         else
    1120             :             infer(luma_weight_l0_flag[i], 0);
    1121             :     }
    1122         510 :     if (chroma) {
    1123        2538 :         for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
    1124             :             if (1 /* is not same POC and same layer_id */)
    1125        2028 :                 flag(chroma_weight_l0_flag[i]);
    1126             :             else
    1127             :                 infer(chroma_weight_l0_flag[i], 0);
    1128             :         }
    1129             :     }
    1130             : 
    1131        2538 :     for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
    1132        2028 :         if (current->luma_weight_l0_flag[i]) {
    1133        1540 :             se(delta_luma_weight_l0[i], -128, +127);
    1134        1540 :             se(luma_offset_l0[i],
    1135             :                -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)),
    1136             :                ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1));
    1137             :         } else {
    1138         488 :             infer(delta_luma_weight_l0[i], 0);
    1139         488 :             infer(luma_offset_l0[i],       0);
    1140             :         }
    1141        2028 :         if (current->chroma_weight_l0_flag[i]) {
    1142        1518 :             for (j = 0; j < 2; j++) {
    1143        1012 :                 se(delta_chroma_weight_l0[i][j], -128, +127);
    1144        1012 :                 se(chroma_offset_l0[i][j],
    1145             :                    -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)),
    1146             :                    ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1));
    1147             :             }
    1148             :         } else {
    1149        4566 :             for (j = 0; j < 2; j++) {
    1150        3044 :                 infer(delta_chroma_weight_l0[i][j], 0);
    1151        3044 :                 infer(chroma_offset_l0[i][j],       0);
    1152             :             }
    1153             :         }
    1154             :     }
    1155             : 
    1156         510 :     if (current->slice_type == HEVC_SLICE_B) {
    1157           0 :         for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
    1158             :             if (1 /* RefPicList1[i] is not CurrPic, nor is it in a different layer */)
    1159           0 :                 flag(luma_weight_l1_flag[i]);
    1160             :             else
    1161             :                 infer(luma_weight_l1_flag[i], 0);
    1162             :         }
    1163           0 :         if (chroma) {
    1164           0 :             for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
    1165             :                 if (1 /* RefPicList1[i] is not CurrPic, nor is it in a different layer */)
    1166           0 :                     flag(chroma_weight_l1_flag[i]);
    1167             :                 else
    1168             :                     infer(chroma_weight_l1_flag[i], 0);
    1169             :             }
    1170             :         }
    1171             : 
    1172           0 :         for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
    1173           0 :             if (current->luma_weight_l1_flag[i]) {
    1174           0 :                 se(delta_luma_weight_l1[i], -128, +127);
    1175           0 :                 se(luma_offset_l1[i],
    1176             :                    -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)),
    1177             :                    ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1));
    1178             :             } else {
    1179           0 :                 infer(delta_luma_weight_l1[i], 0);
    1180           0 :                 infer(luma_offset_l1[i],       0);
    1181             :             }
    1182           0 :             if (current->chroma_weight_l1_flag[i]) {
    1183           0 :                 for (j = 0; j < 2; j++) {
    1184           0 :                     se(delta_chroma_weight_l1[i][j], -128, +127);
    1185           0 :                     se(chroma_offset_l1[i][j],
    1186             :                        -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)),
    1187             :                        ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1));
    1188             :                 }
    1189             :             } else {
    1190           0 :                 for (j = 0; j < 2; j++) {
    1191           0 :                     infer(delta_chroma_weight_l1[i][j], 0);
    1192           0 :                     infer(chroma_offset_l1[i][j],       0);
    1193             :                 }
    1194             :             }
    1195             :         }
    1196             :     }
    1197             : 
    1198         510 :     return 0;
    1199             : }
    1200             : 
    1201       11662 : static int FUNC(slice_segment_header)(CodedBitstreamContext *ctx, RWContext *rw,
    1202             :                                       H265RawSliceHeader *current)
    1203             : {
    1204       11662 :     CodedBitstreamH265Context *h265 = ctx->priv_data;
    1205             :     const H265RawSPS *sps;
    1206             :     const H265RawPPS *pps;
    1207             :     unsigned int min_cb_log2_size_y, ctb_log2_size_y, ctb_size_y;
    1208             :     unsigned int pic_width_in_ctbs_y, pic_height_in_ctbs_y, pic_size_in_ctbs_y;
    1209       11662 :     unsigned int num_pic_total_curr = 0;
    1210             :     int err, i;
    1211             : 
    1212       11662 :     HEADER("Slice Segment Header");
    1213             : 
    1214       11662 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, -1));
    1215             : 
    1216       11662 :     flag(first_slice_segment_in_pic_flag);
    1217             : 
    1218       11918 :     if (current->nal_unit_header.nal_unit_type >= HEVC_NAL_BLA_W_LP &&
    1219         256 :         current->nal_unit_header.nal_unit_type <= HEVC_NAL_IRAP_VCL23)
    1220         256 :         flag(no_output_of_prior_pics_flag);
    1221             : 
    1222       11662 :     ue(slice_pic_parameter_set_id, 0, 63);
    1223             : 
    1224       11662 :     pps = h265->pps[current->slice_pic_parameter_set_id];
    1225       11662 :     if (!pps) {
    1226           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
    1227           0 :                current->slice_pic_parameter_set_id);
    1228           0 :         return AVERROR_INVALIDDATA;
    1229             :     }
    1230       11662 :     h265->active_pps = pps;
    1231             : 
    1232       11662 :     sps = h265->sps[pps->pps_seq_parameter_set_id];
    1233       11662 :     if (!sps) {
    1234           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
    1235           0 :                pps->pps_seq_parameter_set_id);
    1236           0 :         return AVERROR_INVALIDDATA;
    1237             :     }
    1238       11662 :     h265->active_sps = sps;
    1239             : 
    1240       11662 :     min_cb_log2_size_y = sps->log2_min_luma_coding_block_size_minus3 + 3;
    1241       11662 :     ctb_log2_size_y = min_cb_log2_size_y + sps->log2_diff_max_min_luma_coding_block_size;
    1242       11662 :     ctb_size_y = 1 << ctb_log2_size_y;
    1243       11662 :     pic_width_in_ctbs_y =
    1244       11662 :         (sps->pic_width_in_luma_samples + ctb_size_y - 1) / ctb_size_y;
    1245       11662 :     pic_height_in_ctbs_y =
    1246       11662 :         (sps->pic_height_in_luma_samples + ctb_size_y - 1) / ctb_size_y;
    1247       11662 :     pic_size_in_ctbs_y = pic_width_in_ctbs_y * pic_height_in_ctbs_y;
    1248             : 
    1249       11662 :     if (!current->first_slice_segment_in_pic_flag) {
    1250        7464 :         unsigned int address_size = av_log2(pic_size_in_ctbs_y - 1) + 1;
    1251        7464 :         if (pps->dependent_slice_segments_enabled_flag)
    1252        4082 :             flag(dependent_slice_segment_flag);
    1253             :         else
    1254        3382 :             infer(dependent_slice_segment_flag, 0);
    1255        7464 :         u(address_size, slice_segment_address, 0, pic_size_in_ctbs_y - 1);
    1256             :     } else {
    1257        4198 :         infer(dependent_slice_segment_flag, 0);
    1258             :     }
    1259             : 
    1260       11662 :     if (!current->dependent_slice_segment_flag) {
    1261        7894 :         for (i = 0; i < pps->num_extra_slice_header_bits; i++)
    1262           0 :             flag(slice_reserved_flag[i]);
    1263             : 
    1264        7894 :         ue(slice_type, 0, 2);
    1265             : 
    1266        7894 :         if (pps->output_flag_present_flag)
    1267           0 :             flag(pic_output_flag);
    1268             : 
    1269        7894 :         if (sps->separate_colour_plane_flag)
    1270           0 :             u(2, colour_plane_id, 0, 2);
    1271             : 
    1272       15684 :         if (current->nal_unit_header.nal_unit_type != HEVC_NAL_IDR_W_RADL &&
    1273        7790 :             current->nal_unit_header.nal_unit_type != HEVC_NAL_IDR_N_LP) {
    1274             :             const H265RawSTRefPicSet *rps;
    1275             : 
    1276        7788 :             u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, slice_pic_order_cnt_lsb,
    1277             :               0, (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1);
    1278             : 
    1279        7788 :             flag(short_term_ref_pic_set_sps_flag);
    1280        7788 :             if (!current->short_term_ref_pic_set_sps_flag) {
    1281        3730 :                 CHECK(FUNC(st_ref_pic_set)(ctx, rw, &current->short_term_ref_pic_set,
    1282             :                                            sps->num_short_term_ref_pic_sets, sps));
    1283        3730 :                 rps = &current->short_term_ref_pic_set;
    1284        4058 :             } else if (sps->num_short_term_ref_pic_sets > 1) {
    1285        4058 :                 unsigned int idx_size = av_log2(sps->num_short_term_ref_pic_sets - 1) + 1;
    1286        4058 :                 u(idx_size, short_term_ref_pic_set_idx,
    1287             :                   0, sps->num_short_term_ref_pic_sets - 1);
    1288        4058 :                 rps = &sps->st_ref_pic_set[current->short_term_ref_pic_set_idx];
    1289             :             } else {
    1290           0 :                 infer(short_term_ref_pic_set_idx, 0);
    1291           0 :                 rps = &sps->st_ref_pic_set[0];
    1292             :             }
    1293             : 
    1294        7788 :             num_pic_total_curr = 0;
    1295       29156 :             for (i = 0; i < rps->num_negative_pics; i++)
    1296       21368 :                 if (rps->used_by_curr_pic_s0_flag[i])
    1297       20320 :                     ++num_pic_total_curr;
    1298       12970 :             for (i = 0; i < rps->num_positive_pics; i++)
    1299        5182 :                 if (rps->used_by_curr_pic_s1_flag[i])
    1300        4892 :                     ++num_pic_total_curr;
    1301             : 
    1302        7788 :             if (sps->long_term_ref_pics_present_flag) {
    1303             :                 unsigned int idx_size;
    1304             : 
    1305        1664 :                 if (sps->num_long_term_ref_pics_sps > 0) {
    1306         998 :                     ue(num_long_term_sps, 0, sps->num_long_term_ref_pics_sps);
    1307         998 :                     idx_size = av_log2(sps->num_long_term_ref_pics_sps - 1) + 1;
    1308             :                 } else {
    1309         666 :                     infer(num_long_term_sps, 0);
    1310         666 :                     idx_size = 0;
    1311             :                 }
    1312        1664 :                 ue(num_long_term_pics, 0, HEVC_MAX_LONG_TERM_REF_PICS);
    1313             : 
    1314       10632 :                 for (i = 0; i < current->num_long_term_sps +
    1315        7304 :                                 current->num_long_term_pics; i++) {
    1316        2820 :                     if (i < current->num_long_term_sps) {
    1317         368 :                         if (sps->num_long_term_ref_pics_sps > 1)
    1318         368 :                             u(idx_size, lt_idx_sps[i],
    1319             :                               0, sps->num_long_term_ref_pics_sps - 1);
    1320         368 :                         if (sps->used_by_curr_pic_lt_sps_flag[current->lt_idx_sps[i]])
    1321         176 :                             ++num_pic_total_curr;
    1322             :                     } else {
    1323        2452 :                         u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, poc_lsb_lt[i],
    1324             :                           0, (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1);
    1325        2452 :                         flag(used_by_curr_pic_lt_flag[i]);
    1326        2452 :                         if (current->used_by_curr_pic_lt_flag[i])
    1327         756 :                             ++num_pic_total_curr;
    1328             :                     }
    1329        2820 :                     flag(delta_poc_msb_present_flag[i]);
    1330        2820 :                     if (current->delta_poc_msb_present_flag[i])
    1331           0 :                         ue(delta_poc_msb_cycle_lt[i], 0, UINT32_MAX - 1);
    1332             :                     else
    1333        2820 :                         infer(delta_poc_msb_cycle_lt[i], 0);
    1334             :                 }
    1335             :             }
    1336             : 
    1337        7788 :             if (sps->sps_temporal_mvp_enabled_flag)
    1338        5142 :                 flag(slice_temporal_mvp_enabled_flag);
    1339             :             else
    1340        2646 :                 infer(slice_temporal_mvp_enabled_flag, 0);
    1341             : 
    1342        7788 :             if (pps->pps_curr_pic_ref_enabled_flag)
    1343           0 :                 ++num_pic_total_curr;
    1344             :         }
    1345             : 
    1346        7894 :         if (sps->sample_adaptive_offset_enabled_flag) {
    1347        4626 :             flag(slice_sao_luma_flag);
    1348        4626 :             if (!sps->separate_colour_plane_flag && sps->chroma_format_idc != 0)
    1349        4626 :                 flag(slice_sao_chroma_flag);
    1350             :             else
    1351           0 :                 infer(slice_sao_chroma_flag, 0);
    1352             :         } else {
    1353        3268 :             infer(slice_sao_luma_flag,   0);
    1354        3268 :             infer(slice_sao_chroma_flag, 0);
    1355             :         }
    1356             : 
    1357       12484 :         if (current->slice_type == HEVC_SLICE_P ||
    1358        4590 :             current->slice_type == HEVC_SLICE_B) {
    1359        7690 :             flag(num_ref_idx_active_override_flag);
    1360        7690 :             if (current->num_ref_idx_active_override_flag) {
    1361        3620 :                 ue(num_ref_idx_l0_active_minus1, 0, 14);
    1362        3620 :                 if (current->slice_type == HEVC_SLICE_B)
    1363         912 :                     ue(num_ref_idx_l1_active_minus1, 0, 14);
    1364             :                 else
    1365        2708 :                     infer(num_ref_idx_l1_active_minus1, pps->num_ref_idx_l1_default_active_minus1);
    1366             :             } else {
    1367        4070 :                 infer(num_ref_idx_l0_active_minus1, pps->num_ref_idx_l0_default_active_minus1);
    1368        4070 :                 infer(num_ref_idx_l1_active_minus1, pps->num_ref_idx_l1_default_active_minus1);
    1369             :             }
    1370             : 
    1371        7690 :             if (pps->lists_modification_present_flag && num_pic_total_curr > 1)
    1372        1496 :                 CHECK(FUNC(ref_pic_lists_modification)(ctx, rw, current,
    1373             :                                                        num_pic_total_curr));
    1374             : 
    1375        7690 :             if (current->slice_type == HEVC_SLICE_B)
    1376        4386 :                 flag(mvd_l1_zero_flag);
    1377        7690 :             if (pps->cabac_init_present_flag)
    1378        4938 :                 flag(cabac_init_flag);
    1379             :             else
    1380        2752 :                 infer(cabac_init_flag, 0);
    1381        7690 :             if (current->slice_temporal_mvp_enabled_flag) {
    1382        5054 :                 if (current->slice_type == HEVC_SLICE_B)
    1383        4386 :                     flag(collocated_from_l0_flag);
    1384             :                 else
    1385         668 :                     infer(collocated_from_l0_flag, 1);
    1386        5054 :                 if (current->collocated_from_l0_flag) {
    1387        2298 :                     if (current->num_ref_idx_l0_active_minus1 > 0)
    1388        2068 :                         ue(collocated_ref_idx, 0, current->num_ref_idx_l0_active_minus1);
    1389             :                     else
    1390         230 :                         infer(collocated_ref_idx, 0);
    1391             :                 } else {
    1392        2756 :                     if (current->num_ref_idx_l1_active_minus1 > 0)
    1393        2574 :                         ue(collocated_ref_idx, 0, current->num_ref_idx_l1_active_minus1);
    1394             :                     else
    1395         182 :                         infer(collocated_ref_idx, 0);
    1396             :                 }
    1397             :             }
    1398             : 
    1399       14870 :             if ((pps->weighted_pred_flag   && current->slice_type == HEVC_SLICE_P) ||
    1400        7180 :                 (pps->weighted_bipred_flag && current->slice_type == HEVC_SLICE_B))
    1401         510 :                 CHECK(FUNC(pred_weight_table)(ctx, rw, current));
    1402             : 
    1403        7690 :             ue(five_minus_max_num_merge_cand, 0, 4);
    1404        7690 :             if (sps->motion_vector_resolution_control_idc == 2)
    1405           0 :                 flag(use_integer_mv_flag);
    1406             :             else
    1407        7690 :                 infer(use_integer_mv_flag, sps->motion_vector_resolution_control_idc);
    1408             :         }
    1409             : 
    1410        7894 :         se(slice_qp_delta,
    1411             :            - 6 * sps->bit_depth_luma_minus8 - (pps->init_qp_minus26 + 26),
    1412             :            + 51 - (pps->init_qp_minus26 + 26));
    1413        7894 :         if (pps->pps_slice_chroma_qp_offsets_present_flag) {
    1414           0 :             se(slice_cb_qp_offset, -12, +12);
    1415           0 :             se(slice_cr_qp_offset, -12, +12);
    1416             :         } else {
    1417        7894 :             infer(slice_cb_qp_offset, 0);
    1418        7894 :             infer(slice_cr_qp_offset, 0);
    1419             :         }
    1420        7894 :         if (pps->pps_slice_act_qp_offsets_present_flag) {
    1421           0 :             se(slice_act_y_qp_offset,
    1422             :                -12 - (pps->pps_act_y_qp_offset_plus5 - 5),
    1423             :                +12 - (pps->pps_act_y_qp_offset_plus5 - 5));
    1424           0 :             se(slice_act_cb_qp_offset,
    1425             :                -12 - (pps->pps_act_cb_qp_offset_plus5 - 5),
    1426             :                +12 - (pps->pps_act_cb_qp_offset_plus5 - 5));
    1427           0 :             se(slice_act_cr_qp_offset,
    1428             :                -12 - (pps->pps_act_cr_qp_offset_plus3 - 3),
    1429             :                +12 - (pps->pps_act_cr_qp_offset_plus3 - 3));
    1430             :         } else {
    1431        7894 :             infer(slice_act_y_qp_offset,  0);
    1432        7894 :             infer(slice_act_cb_qp_offset, 0);
    1433        7894 :             infer(slice_act_cr_qp_offset, 0);
    1434             :         }
    1435        7894 :         if (pps->chroma_qp_offset_list_enabled_flag)
    1436           0 :             flag(cu_chroma_qp_offset_enabled_flag);
    1437             :         else
    1438        7894 :             infer(cu_chroma_qp_offset_enabled_flag, 0);
    1439             : 
    1440        7894 :         if (pps->deblocking_filter_override_enabled_flag)
    1441          74 :             flag(deblocking_filter_override_flag);
    1442             :         else
    1443        7820 :             infer(deblocking_filter_override_flag, 0);
    1444        7894 :         if (current->deblocking_filter_override_flag) {
    1445           2 :             flag(slice_deblocking_filter_disabled_flag);
    1446           2 :             if (!current->slice_deblocking_filter_disabled_flag) {
    1447           0 :                 se(slice_beta_offset_div2, -6, +6);
    1448           0 :                 se(slice_tc_offset_div2,   -6, +6);
    1449             :             } else {
    1450           2 :                 infer(slice_beta_offset_div2, pps->pps_beta_offset_div2);
    1451           2 :                 infer(slice_tc_offset_div2,   pps->pps_tc_offset_div2);
    1452             :             }
    1453             :         } else {
    1454        7892 :             infer(slice_deblocking_filter_disabled_flag,
    1455             :                   pps->pps_deblocking_filter_disabled_flag);
    1456        7892 :             infer(slice_beta_offset_div2, pps->pps_beta_offset_div2);
    1457        7892 :             infer(slice_tc_offset_div2,   pps->pps_tc_offset_div2);
    1458             :         }
    1459       14298 :         if (pps->pps_loop_filter_across_slices_enabled_flag &&
    1460       15368 :             (current->slice_sao_luma_flag || current->slice_sao_chroma_flag ||
    1461        4482 :              !current->slice_deblocking_filter_disabled_flag))
    1462        6402 :             flag(slice_loop_filter_across_slices_enabled_flag);
    1463             :         else
    1464        1492 :             infer(slice_loop_filter_across_slices_enabled_flag,
    1465             :                   pps->pps_loop_filter_across_slices_enabled_flag);
    1466             :     }
    1467             : 
    1468       11662 :     if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
    1469             :         unsigned int num_entry_point_offsets_limit;
    1470        3610 :         if (!pps->tiles_enabled_flag && pps->entropy_coding_sync_enabled_flag)
    1471         874 :             num_entry_point_offsets_limit = pic_height_in_ctbs_y - 1;
    1472        2736 :         else if (pps->tiles_enabled_flag && !pps->entropy_coding_sync_enabled_flag)
    1473        2736 :             num_entry_point_offsets_limit =
    1474        2736 :                 (pps->num_tile_columns_minus1 + 1) * (pps->num_tile_rows_minus1 + 1);
    1475             :         else
    1476           0 :             num_entry_point_offsets_limit =
    1477           0 :                 (pps->num_tile_columns_minus1 + 1) * pic_height_in_ctbs_y - 1;
    1478        3610 :         ue(num_entry_point_offsets, 0, num_entry_point_offsets_limit);
    1479             : 
    1480        3610 :         if (current->num_entry_point_offsets > HEVC_MAX_ENTRY_POINT_OFFSETS) {
    1481           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many entry points: "
    1482           0 :                    "%"PRIu16".\n", current->num_entry_point_offsets);
    1483           0 :             return AVERROR_PATCHWELCOME;
    1484             :         }
    1485             : 
    1486        3610 :         if (current->num_entry_point_offsets > 0) {
    1487        1538 :             ue(offset_len_minus1, 0, 31);
    1488        4650 :             for (i = 0; i < current->num_entry_point_offsets; i++)
    1489        3112 :                 u(current->offset_len_minus1 + 1, entry_point_offset_minus1[i],
    1490             :                   0, (1 << (current->offset_len_minus1 + 1)) - 1);
    1491             :         }
    1492             :     }
    1493             : 
    1494       11662 :     if (pps->slice_segment_header_extension_present_flag) {
    1495         874 :         ue(slice_segment_header_extension_length, 0, 256);
    1496        7078 :         for (i = 0; i < current->slice_segment_header_extension_length; i++)
    1497        6204 :             u(8, slice_segment_header_extension_data_byte[i], 0x00, 0xff);
    1498             :     }
    1499             : 
    1500       11662 :     CHECK(FUNC(byte_alignment)(ctx, rw));
    1501             : 
    1502       11662 :     return 0;
    1503             : }

Generated by: LCOV version 1.13