LCOV - code coverage report
Current view: top level - libavcodec - cbs_h265_syntax_template.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 704 958 73.5 %
Date: 2018-05-20 11:54:08 Functions: 36 50 72.0 %

          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        1192 : static int FUNC(rbsp_trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
      20             : {
      21             :     int err;
      22             : 
      23        1192 :     fixed(1, rbsp_stop_one_bit, 1);
      24        7876 :     while (byte_alignment(rw) != 0)
      25        5492 :         fixed(1, rbsp_alignment_zero_bit, 0);
      26             : 
      27        1192 :     return 0;
      28             : }
      29             : 
      30       12854 : 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       12854 :     u(1, forbidden_zero_bit, 0, 0);
      37             : 
      38       12854 :     if (expected_nal_unit_type >= 0)
      39        1192 :         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       12854 :     u(6, nuh_layer_id,          0, 62);
      45       12854 :     u(3, nuh_temporal_id_plus1, 1,  7);
      46             : 
      47       12854 :     return 0;
      48             : }
      49             : 
      50       11662 : static int FUNC(byte_alignment)(CodedBitstreamContext *ctx, RWContext *rw)
      51             : {
      52             :     int err;
      53             : 
      54       11662 :     fixed(1, alignment_bit_equal_to_one, 1);
      55       70476 :     while (byte_alignment(rw) != 0)
      56       47152 :         fixed(1, alignment_bit_equal_to_zero, 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, 0);
      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, 0);
      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             :     int err, i, j;
      94             : 
      95         188 :     if (profile_present_flag) {
      96         188 :         u(2, general_profile_space, 0, 0);
      97         188 :         flag(general_tier_flag);
      98         188 :         u(5, general_profile_idc, 0, 31);
      99             : 
     100        6204 :         for (j = 0; j < 32; j++)
     101        6016 :             flags(general_profile_compatibility_flag[j], 1, j);
     102             : 
     103         188 :         flag(general_progressive_source_flag);
     104         188 :         flag(general_interlaced_source_flag);
     105         188 :         flag(general_non_packed_constraint_flag);
     106         188 :         flag(general_frame_only_constraint_flag);
     107             : 
     108             : #define profile_compatible(x) (current->general_profile_idc == (x) || \
     109             :                                current->general_profile_compatibility_flag[x])
     110         376 :         if (profile_compatible(4) || profile_compatible(5) ||
     111         564 :             profile_compatible(6) || profile_compatible(7) ||
     112         564 :             profile_compatible(8) || profile_compatible(9) ||
     113         376 :             profile_compatible(10)) {
     114           0 :             flag(general_max_12bit_constraint_flag);
     115           0 :             flag(general_max_10bit_constraint_flag);
     116           0 :             flag(general_max_8bit_constraint_flag);
     117           0 :             flag(general_max_422chroma_constraint_flag);
     118           0 :             flag(general_max_420chroma_constraint_flag);
     119           0 :             flag(general_max_monochrome_constraint_flag);
     120           0 :             flag(general_intra_constraint_flag);
     121           0 :             flag(general_one_picture_only_constraint_flag);
     122           0 :             flag(general_lower_bit_rate_constraint_flag);
     123             : 
     124           0 :             if (profile_compatible(5) || profile_compatible(9) ||
     125           0 :                 profile_compatible(10)) {
     126           0 :                 flag(general_max_14bit_constraint_flag);
     127           0 :                 fixed(24, general_reserved_zero_33bits, 0);
     128           0 :                 fixed( 9, general_reserved_zero_33bits, 0);
     129             :             } else {
     130           0 :                 fixed(24, general_reserved_zero_34bits, 0);
     131           0 :                 fixed(10, general_reserved_zero_34bits, 0);
     132             :             }
     133             :         } else {
     134         188 :             fixed(24, general_reserved_zero_43bits, 0);
     135         188 :             fixed(19, general_reserved_zero_43bits, 0);
     136             :         }
     137             : 
     138         188 :         if (profile_compatible(1) || profile_compatible(2) ||
     139           0 :             profile_compatible(3) || profile_compatible(4) ||
     140           0 :             profile_compatible(5) || profile_compatible(9)) {
     141         188 :             flag(general_inbld_flag);
     142             :         } else {
     143           0 :             fixed(1, general_reserved_zero_bit, 0);
     144             :         }
     145             : #undef profile_compatible
     146             :     }
     147             : 
     148         188 :     u(8, general_level_idc, 0, 255);
     149             : 
     150         268 :     for (i = 0; i < max_num_sub_layers_minus1; i++) {
     151          80 :         flags(sub_layer_profile_present_flag[i], 1, i);
     152          80 :         flags(sub_layer_level_present_flag[i],   1, i);
     153             :     }
     154             : 
     155         188 :     if (max_num_sub_layers_minus1 > 0) {
     156         280 :         for (i = max_num_sub_layers_minus1; i < 8; i++)
     157         240 :             fixed(2, reserved_zero_2bits, 0);
     158             :     }
     159             : 
     160         268 :     for (i = 0; i < max_num_sub_layers_minus1; i++) {
     161          80 :         if (current->sub_layer_profile_present_flag[i])
     162           0 :             return AVERROR_PATCHWELCOME;
     163          80 :         if (current->sub_layer_level_present_flag[i])
     164           0 :             return AVERROR_PATCHWELCOME;
     165             :     }
     166             : 
     167         188 :     return 0;
     168             : }
     169             : 
     170          16 : static int FUNC(sub_layer_hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
     171             :                                           H265RawHRDParameters *hrd,
     172             :                                           int nal, int sub_layer_id)
     173             : {
     174             :     H265RawSubLayerHRDParameters *current;
     175             :     int err, i;
     176             : 
     177          16 :     if (nal)
     178           8 :         current = &hrd->nal_sub_layer_hrd_parameters[sub_layer_id];
     179             :     else
     180           8 :         current = &hrd->vcl_sub_layer_hrd_parameters[sub_layer_id];
     181             : 
     182          32 :     for (i = 0; i <= hrd->cpb_cnt_minus1[sub_layer_id]; i++) {
     183          16 :         ues(bit_rate_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
     184          16 :         ues(cpb_size_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
     185          16 :         if (hrd->sub_pic_hrd_params_present_flag) {
     186           8 :             ues(cpb_size_du_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
     187           8 :             ues(bit_rate_du_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
     188             :         }
     189          16 :         flags(cbr_flag[i], 1, i);
     190             :     }
     191             : 
     192          16 :     return 0;
     193             : }
     194             : 
     195           8 : static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
     196             :                                 H265RawHRDParameters *current, int common_inf_present_flag,
     197             :                                 int max_num_sub_layers_minus1)
     198             : {
     199             :     int err, i;
     200             : 
     201           8 :     if (common_inf_present_flag) {
     202           8 :         flag(nal_hrd_parameters_present_flag);
     203           8 :         flag(vcl_hrd_parameters_present_flag);
     204             : 
     205           8 :         if (current->nal_hrd_parameters_present_flag ||
     206           0 :             current->vcl_hrd_parameters_present_flag) {
     207           8 :             flag(sub_pic_hrd_params_present_flag);
     208           8 :             if (current->sub_pic_hrd_params_present_flag) {
     209           4 :                 u(8, tick_divisor_minus2, 0, 255);
     210           4 :                 u(5, du_cpb_removal_delay_increment_length_minus1, 0, 31);
     211           4 :                 flag(sub_pic_cpb_params_in_pic_timing_sei_flag);
     212           4 :                 u(5, dpb_output_delay_du_length_minus1, 0, 31);
     213             :             }
     214             : 
     215           8 :             u(4, bit_rate_scale, 0, 15);
     216           8 :             u(4, cpb_size_scale, 0, 15);
     217           8 :             if (current->sub_pic_hrd_params_present_flag)
     218           4 :                 u(4, cpb_size_du_scale, 0, 15);
     219             : 
     220           8 :             u(5, initial_cpb_removal_delay_length_minus1, 0, 31);
     221           8 :             u(5, au_cpb_removal_delay_length_minus1,      0, 31);
     222           8 :             u(5, dpb_output_delay_length_minus1,          0, 31);
     223             :         } else {
     224           0 :             infer(sub_pic_hrd_params_present_flag, 0);
     225             : 
     226           0 :             infer(initial_cpb_removal_delay_length_minus1, 23);
     227           0 :             infer(au_cpb_removal_delay_length_minus1,      23);
     228           0 :             infer(dpb_output_delay_length_minus1,          23);
     229             :         }
     230             :     }
     231             : 
     232          16 :     for (i = 0; i <= max_num_sub_layers_minus1; i++) {
     233           8 :         flags(fixed_pic_rate_general_flag[i], 1, i);
     234             : 
     235           8 :         if (!current->fixed_pic_rate_general_flag[i])
     236           4 :             flags(fixed_pic_rate_within_cvs_flag[i], 1, i);
     237             :         else
     238           4 :             infer(fixed_pic_rate_within_cvs_flag[i], 1);
     239             : 
     240           8 :         if (current->fixed_pic_rate_within_cvs_flag[i]) {
     241           4 :             ues(elemental_duration_in_tc_minus1[i], 0, 2047, 1, i);
     242           4 :             infer(low_delay_hrd_flag[i], 0);
     243             :         } else
     244           4 :             flags(low_delay_hrd_flag[i], 1, i);
     245             : 
     246           8 :         if (!current->low_delay_hrd_flag[i])
     247           8 :             ues(cpb_cnt_minus1[i], 0, 31, 1, i);
     248             :         else
     249           0 :             infer(cpb_cnt_minus1[i], 0);
     250             : 
     251           8 :         if (current->nal_hrd_parameters_present_flag)
     252           8 :             CHECK(FUNC(sub_layer_hrd_parameters)(ctx, rw, current, 0, i));
     253           8 :         if (current->vcl_hrd_parameters_present_flag)
     254           8 :             CHECK(FUNC(sub_layer_hrd_parameters)(ctx, rw, current, 1, i));
     255             :     }
     256             : 
     257           8 :     return 0;
     258             : }
     259             : 
     260          36 : static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
     261             :                                 H265RawVUI *current, const H265RawSPS *sps)
     262             : {
     263             :     int err;
     264             : 
     265          36 :     flag(aspect_ratio_info_present_flag);
     266          36 :     if (current->aspect_ratio_info_present_flag) {
     267          12 :         u(8, aspect_ratio_idc, 0, 255);
     268          12 :         if (current->aspect_ratio_idc == 255) {
     269           0 :             u(16, sar_width,  0, 65535);
     270           0 :             u(16, sar_height, 0, 65535);
     271             :         }
     272             :     } else {
     273          24 :         infer(aspect_ratio_idc, 0);
     274             :     }
     275             : 
     276          36 :     flag(overscan_info_present_flag);
     277          36 :     if (current->overscan_info_present_flag)
     278           0 :         flag(overscan_appropriate_flag);
     279             : 
     280          36 :     flag(video_signal_type_present_flag);
     281          36 :     if (current->video_signal_type_present_flag) {
     282           0 :         u(3, video_format, 0, 7);
     283           0 :         flag(video_full_range_flag);
     284           0 :         flag(colour_description_present_flag);
     285           0 :         if (current->colour_description_present_flag) {
     286           0 :             u(8, colour_primaries,         0, 255);
     287           0 :             u(8, transfer_characteristics, 0, 255);
     288           0 :             u(8, matrix_coefficients,      0, 255);
     289             :         } else {
     290           0 :             infer(colour_primaries,         2);
     291           0 :             infer(transfer_characteristics, 2);
     292           0 :             infer(matrix_coefficients,      2);
     293             :         }
     294             :     } else {
     295          36 :         infer(video_format,             5);
     296          36 :         infer(video_full_range_flag,    0);
     297          36 :         infer(colour_primaries,         2);
     298          36 :         infer(transfer_characteristics, 2);
     299          36 :         infer(matrix_coefficients,      2);
     300             :     }
     301             : 
     302          36 :     flag(chroma_loc_info_present_flag);
     303          36 :     if (current->chroma_loc_info_present_flag) {
     304           0 :         ue(chroma_sample_loc_type_top_field,    0, 5);
     305           0 :         ue(chroma_sample_loc_type_bottom_field, 0, 5);
     306             :     } else {
     307          36 :         infer(chroma_sample_loc_type_top_field,    0);
     308          36 :         infer(chroma_sample_loc_type_bottom_field, 0);
     309             :     }
     310             : 
     311          36 :     flag(neutral_chroma_indication_flag);
     312          36 :     flag(field_seq_flag);
     313          36 :     flag(frame_field_info_present_flag);
     314             : 
     315          36 :     flag(default_display_window_flag);
     316          36 :     if (current->default_display_window_flag) {
     317          12 :         ue(def_disp_win_left_offset,   0, 16384);
     318          12 :         ue(def_disp_win_right_offset,  0, 16384);
     319          12 :         ue(def_disp_win_top_offset,    0, 16384);
     320          12 :         ue(def_disp_win_bottom_offset, 0, 16384);
     321             :     }
     322             : 
     323          36 :     flag(vui_timing_info_present_flag);
     324          36 :     if (current->vui_timing_info_present_flag) {
     325           8 :         u(32, vui_num_units_in_tick, 1, UINT32_MAX);
     326           8 :         u(32, vui_time_scale,        1, UINT32_MAX);
     327           8 :         flag(vui_poc_proportional_to_timing_flag);
     328           8 :         if (current->vui_poc_proportional_to_timing_flag)
     329           4 :             ue(vui_num_ticks_poc_diff_one_minus1, 0, UINT32_MAX - 1);
     330             : 
     331           8 :         flag(vui_hrd_parameters_present_flag);
     332           8 :         if (current->vui_hrd_parameters_present_flag) {
     333           8 :             CHECK(FUNC(hrd_parameters)(ctx, rw, &current->hrd_parameters,
     334             :                                        1, sps->sps_max_sub_layers_minus1));
     335             :         }
     336             :     }
     337             : 
     338          36 :     flag(bitstream_restriction_flag);
     339          36 :     if (current->bitstream_restriction_flag) {
     340          24 :         flag(tiles_fixed_structure_flag);
     341          24 :         flag(motion_vectors_over_pic_boundaries_flag);
     342          24 :         flag(restricted_ref_pic_lists_flag);
     343          24 :         ue(min_spatial_segmentation_idc,  0, 4095);
     344          24 :         ue(max_bytes_per_pic_denom,       0, 16);
     345          24 :         ue(max_bits_per_min_cu_denom,     0, 16);
     346          24 :         ue(log2_max_mv_length_horizontal, 0, 16);
     347          24 :         ue(log2_max_mv_length_vertical,   0, 16);
     348             :     } else {
     349          12 :         infer(tiles_fixed_structure_flag,    0);
     350          12 :         infer(motion_vectors_over_pic_boundaries_flag, 1);
     351          12 :         infer(min_spatial_segmentation_idc,  0);
     352          12 :         infer(max_bytes_per_pic_denom,       2);
     353          12 :         infer(max_bits_per_min_cu_denom,     1);
     354          12 :         infer(log2_max_mv_length_horizontal, 15);
     355          12 :         infer(log2_max_mv_length_vertical,   15);
     356             :     }
     357             : 
     358          36 :     return 0;
     359             : }
     360             : 
     361          92 : static int FUNC(vps)(CodedBitstreamContext *ctx, RWContext *rw,
     362             :                      H265RawVPS *current)
     363             : {
     364             :     int err, i, j;
     365             : 
     366          92 :     HEADER("Video Parameter Set");
     367             : 
     368          92 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_VPS));
     369             : 
     370          92 :     u(4, vps_video_parameter_set_id, 0, 15);
     371             : 
     372          92 :     flag(vps_base_layer_internal_flag);
     373          92 :     flag(vps_base_layer_available_flag);
     374          92 :     u(6, vps_max_layers_minus1,     0, HEVC_MAX_LAYERS - 1);
     375          92 :     u(3, vps_max_sub_layers_minus1, 0, HEVC_MAX_SUB_LAYERS - 1);
     376          92 :     flag(vps_temporal_id_nesting_flag);
     377             : 
     378         164 :     if (current->vps_max_sub_layers_minus1 == 0 &&
     379          72 :         current->vps_temporal_id_nesting_flag != 1) {
     380           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid stream: "
     381             :                "vps_temporal_id_nesting_flag must be 1 if "
     382             :                "vps_max_sub_layers_minus1 is 0.\n");
     383           0 :         return AVERROR_INVALIDDATA;
     384             :     }
     385             : 
     386          92 :     fixed(16, vps_reserved_0xffff_16bits, 0xffff);
     387             : 
     388          92 :     CHECK(FUNC(profile_tier_level)(ctx, rw, &current->profile_tier_level,
     389             :                                    1, current->vps_max_sub_layers_minus1));
     390             : 
     391          92 :     flag(vps_sub_layer_ordering_info_present_flag);
     392         368 :     for (i = (current->vps_sub_layer_ordering_info_present_flag ?
     393          92 :               0 : current->vps_max_sub_layers_minus1);
     394          92 :          i <= current->vps_max_sub_layers_minus1; i++) {
     395          92 :         ues(vps_max_dec_pic_buffering_minus1[i],
     396             :             0, HEVC_MAX_DPB_SIZE - 1,                        1, i);
     397          92 :         ues(vps_max_num_reorder_pics[i],
     398             :             0, current->vps_max_dec_pic_buffering_minus1[i], 1, i);
     399          92 :         ues(vps_max_latency_increase_plus1[i],
     400             :             0, UINT32_MAX - 1,                               1, i);
     401             :     }
     402          92 :     if (!current->vps_sub_layer_ordering_info_present_flag) {
     403          64 :         for (i = 0; i < current->vps_max_sub_layers_minus1; i++) {
     404          40 :             infer(vps_max_dec_pic_buffering_minus1[i],
     405             :                   current->vps_max_dec_pic_buffering_minus1[current->vps_max_sub_layers_minus1]);
     406          40 :             infer(vps_max_num_reorder_pics[i],
     407             :                   current->vps_max_num_reorder_pics[current->vps_max_sub_layers_minus1]);
     408          40 :             infer(vps_max_latency_increase_plus1[i],
     409             :                   current->vps_max_latency_increase_plus1[current->vps_max_sub_layers_minus1]);
     410             :         }
     411             :     }
     412             : 
     413          92 :     u(6, vps_max_layer_id,        0, HEVC_MAX_LAYERS - 1);
     414          92 :     ue(vps_num_layer_sets_minus1, 0, HEVC_MAX_LAYER_SETS - 1);
     415          92 :     for (i = 1; i <= current->vps_num_layer_sets_minus1; i++) {
     416           0 :         for (j = 0; j <= current->vps_max_layer_id; j++)
     417           0 :             flags(layer_id_included_flag[i][j], 2, i, j);
     418             :     }
     419         184 :     for (j = 0; j <= current->vps_max_layer_id; j++)
     420          92 :         infer(layer_id_included_flag[0][j], j == 0);
     421             : 
     422          92 :     flag(vps_timing_info_present_flag);
     423          92 :     if (current->vps_timing_info_present_flag) {
     424           4 :         u(32, vps_num_units_in_tick, 1, UINT32_MAX);
     425           4 :         u(32, vps_time_scale,        1, UINT32_MAX);
     426           4 :         flag(vps_poc_proportional_to_timing_flag);
     427           4 :         if (current->vps_poc_proportional_to_timing_flag)
     428           4 :             ue(vps_num_ticks_poc_diff_one_minus1, 0, UINT32_MAX - 1);
     429           4 :         ue(vps_num_hrd_parameters, 0, current->vps_num_layer_sets_minus1 + 1);
     430           4 :         for (i = 0; i < current->vps_num_hrd_parameters; i++) {
     431           0 :             ues(hrd_layer_set_idx[i],
     432             :                 current->vps_base_layer_internal_flag ? 0 : 1,
     433             :                 current->vps_num_layer_sets_minus1, 1, i);
     434           0 :             if (i > 0)
     435           0 :                 flags(cprms_present_flag[i], 1, i);
     436             :             else
     437           0 :                 infer(cprms_present_flag[0], 1);
     438             : 
     439           0 :             CHECK(FUNC(hrd_parameters)(ctx, rw, &current->hrd_parameters[i],
     440             :                                        current->cprms_present_flag[i],
     441             :                                        current->vps_max_sub_layers_minus1));
     442             :         }
     443             :     }
     444             : 
     445          92 :     flag(vps_extension_flag);
     446          92 :     if (current->vps_extension_flag)
     447           0 :         CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
     448             : 
     449          92 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
     450             : 
     451          92 :     return 0;
     452             : }
     453             : 
     454        4498 : static int FUNC(st_ref_pic_set)(CodedBitstreamContext *ctx, RWContext *rw,
     455             :                                 H265RawSTRefPicSet *current, int st_rps_idx,
     456             :                                 const H265RawSPS *sps)
     457             : {
     458             :     int err, i, j;
     459             : 
     460        4498 :     if (st_rps_idx != 0)
     461        4354 :         flag(inter_ref_pic_set_prediction_flag);
     462             :     else
     463         144 :         infer(inter_ref_pic_set_prediction_flag, 0);
     464             : 
     465        4498 :     if (current->inter_ref_pic_set_prediction_flag) {
     466             :         unsigned int ref_rps_idx, num_delta_pocs;
     467             :         const H265RawSTRefPicSet *ref;
     468             :         int delta_rps, d_poc;
     469             :         int ref_delta_poc_s0[HEVC_MAX_REFS], ref_delta_poc_s1[HEVC_MAX_REFS];
     470             :         int delta_poc_s0[HEVC_MAX_REFS], delta_poc_s1[HEVC_MAX_REFS];
     471             :         uint8_t used_by_curr_pic_s0[HEVC_MAX_REFS],
     472             :                 used_by_curr_pic_s1[HEVC_MAX_REFS];
     473             : 
     474         988 :         if (st_rps_idx == sps->num_short_term_ref_pic_sets)
     475         372 :             ue(delta_idx_minus1, 0, st_rps_idx - 1);
     476             :         else
     477         616 :             infer(delta_idx_minus1, 0);
     478             : 
     479         988 :         ref_rps_idx = st_rps_idx - (current->delta_idx_minus1 + 1);
     480         988 :         ref = &sps->st_ref_pic_set[ref_rps_idx];
     481         988 :         num_delta_pocs = ref->num_negative_pics + ref->num_positive_pics;
     482             : 
     483         988 :         flag(delta_rps_sign);
     484         988 :         ue(abs_delta_rps_minus1, 0, INT16_MAX);
     485        1976 :         delta_rps = (1 - 2 * current->delta_rps_sign) *
     486         988 :             (current->abs_delta_rps_minus1 + 1);
     487             : 
     488        5368 :         for (j = 0; j <= num_delta_pocs; j++) {
     489        4380 :             flags(used_by_curr_pic_flag[j], 1, j);
     490        4380 :             if (!current->used_by_curr_pic_flag[j])
     491         994 :                 flags(use_delta_flag[j], 1, j);
     492             :             else
     493        3386 :                 infer(use_delta_flag[j], 1);
     494             :         }
     495             : 
     496             :         // Since the stored form of an RPS here is actually the delta-step
     497             :         // form used when inter_ref_pic_set_prediction_flag is not set, we
     498             :         // need to reconstruct that here in order to be able to refer to
     499             :         // the RPS later (which is required for parsing, because we don't
     500             :         // even know what syntax elements appear without it).  Therefore,
     501             :         // this code takes the delta-step form of the reference set, turns
     502             :         // it into the delta-array form, applies the prediction process of
     503             :         // 7.4.8, converts the result back to the delta-step form, and
     504             :         // stores that as the current set for future use.  Note that the
     505             :         // inferences here mean that writers using prediction will need
     506             :         // to fill in the delta-step values correctly as well - since the
     507             :         // whole RPS prediction process is somewhat overly sophisticated,
     508             :         // this hopefully forms a useful check for them to ensure their
     509             :         // predicted form actually matches what was intended rather than
     510             :         // an onerous additional requirement.
     511             : 
     512         988 :         d_poc = 0;
     513        3258 :         for (i = 0; i < ref->num_negative_pics; i++) {
     514        2270 :             d_poc -= ref->delta_poc_s0_minus1[i] + 1;
     515        2270 :             ref_delta_poc_s0[i] = d_poc;
     516             :         }
     517         988 :         d_poc = 0;
     518        2110 :         for (i = 0; i < ref->num_positive_pics; i++) {
     519        1122 :             d_poc += ref->delta_poc_s1_minus1[i] + 1;
     520        1122 :             ref_delta_poc_s1[i] = d_poc;
     521             :         }
     522             : 
     523         988 :         i = 0;
     524        2110 :         for (j = ref->num_positive_pics - 1; j >= 0; j--) {
     525        1122 :             d_poc = ref_delta_poc_s1[j] + delta_rps;
     526        1122 :             if (d_poc < 0 && current->use_delta_flag[ref->num_negative_pics + j]) {
     527         288 :                 delta_poc_s0[i] = d_poc;
     528         576 :                 used_by_curr_pic_s0[i++] =
     529         576 :                     current->used_by_curr_pic_flag[ref->num_negative_pics + j];
     530             :             }
     531             :         }
     532         988 :         if (delta_rps < 0 && current->use_delta_flag[num_delta_pocs]) {
     533         220 :             delta_poc_s0[i] = delta_rps;
     534         440 :             used_by_curr_pic_s0[i++] =
     535         220 :                 current->used_by_curr_pic_flag[num_delta_pocs];
     536             :         }
     537        3258 :         for (j = 0; j < ref->num_negative_pics; j++) {
     538        2270 :             d_poc = ref_delta_poc_s0[j] + delta_rps;
     539        2270 :             if (d_poc < 0 && current->use_delta_flag[j]) {
     540        1640 :                 delta_poc_s0[i] = d_poc;
     541        1640 :                 used_by_curr_pic_s0[i++] = current->used_by_curr_pic_flag[j];
     542             :             }
     543             :         }
     544             : 
     545         988 :         infer(num_negative_pics, i);
     546        3136 :         for (i = 0; i < current->num_negative_pics; i++) {
     547        2148 :             infer(delta_poc_s0_minus1[i],
     548             :                   -(delta_poc_s0[i] - (i == 0 ? 0 : delta_poc_s0[i - 1])) - 1);
     549        2148 :             infer(used_by_curr_pic_s0_flag[i], used_by_curr_pic_s0[i]);
     550             :         }
     551             : 
     552         988 :         i = 0;
     553        3258 :         for (j = ref->num_negative_pics - 1; j >= 0; j--) {
     554        2270 :             d_poc = ref_delta_poc_s0[j] + delta_rps;
     555        2270 :             if (d_poc > 0 && current->use_delta_flag[j]) {
     556           0 :                 delta_poc_s1[i] = d_poc;
     557           0 :                 used_by_curr_pic_s1[i++] = current->used_by_curr_pic_flag[j];
     558             :             }
     559             :         }
     560         988 :         if (delta_rps > 0 && current->use_delta_flag[num_delta_pocs]) {
     561         466 :             delta_poc_s1[i] = delta_rps;
     562         932 :             used_by_curr_pic_s1[i++] =
     563         466 :                 current->used_by_curr_pic_flag[num_delta_pocs];
     564             :         }
     565        2110 :         for (j = 0; j < ref->num_positive_pics; j++) {
     566        1122 :             d_poc = ref_delta_poc_s1[j] + delta_rps;
     567        1122 :             if (d_poc > 0 && current->use_delta_flag[ref->num_negative_pics + j]) {
     568         772 :                 delta_poc_s1[i] = d_poc;
     569        1544 :                 used_by_curr_pic_s1[i++] =
     570        1544 :                     current->used_by_curr_pic_flag[ref->num_negative_pics + j];
     571             :             }
     572             :         }
     573             : 
     574         988 :         infer(num_positive_pics, i);
     575        2226 :         for (i = 0; i < current->num_positive_pics; i++) {
     576        1238 :             infer(delta_poc_s1_minus1[i],
     577             :                   delta_poc_s1[i] - (i == 0 ? 0 : delta_poc_s1[i - 1]) - 1);
     578        1238 :             infer(used_by_curr_pic_s1_flag[i], used_by_curr_pic_s1[i]);
     579             :         }
     580             : 
     581             :     } else {
     582        3510 :         ue(num_negative_pics, 0, 15);
     583        3510 :         ue(num_positive_pics, 0, 15 - current->num_negative_pics);
     584             : 
     585       13158 :         for (i = 0; i < current->num_negative_pics; i++) {
     586        9648 :             ues(delta_poc_s0_minus1[i], 0, INT16_MAX, 1, i);
     587        9648 :             flags(used_by_curr_pic_s0_flag[i],        1, i);
     588             :         }
     589             : 
     590        4320 :         for (i = 0; i < current->num_positive_pics; i++) {
     591         810 :             ues(delta_poc_s1_minus1[i], 0, INT16_MAX, 1, i);
     592         810 :             flags(used_by_curr_pic_s1_flag[i],        1, i);
     593             :         }
     594             :     }
     595             : 
     596        4498 :     return 0;
     597             : }
     598             : 
     599         176 : static int FUNC(scaling_list_data)(CodedBitstreamContext *ctx, RWContext *rw,
     600             :                                    H265RawScalingList *current)
     601             : {
     602             :     int sizeId, matrixId;
     603             :     int err, n, i;
     604             : 
     605         880 :     for (sizeId = 0; sizeId < 4; sizeId++) {
     606        4224 :         for (matrixId = 0; matrixId < 6; matrixId += (sizeId == 3 ? 3 : 1)) {
     607        3520 :             flags(scaling_list_pred_mode_flag[sizeId][matrixId],
     608             :                   2, sizeId, matrixId);
     609        3520 :             if (!current->scaling_list_pred_mode_flag[sizeId][matrixId]) {
     610        1564 :                 ues(scaling_list_pred_matrix_id_delta[sizeId][matrixId],
     611             :                     0, sizeId == 3 ? matrixId / 3 : matrixId,
     612             :                     2, sizeId, matrixId);
     613             :             } else {
     614        1956 :                 n = FFMIN(64, 1 << (4 + (sizeId << 1)));
     615        1956 :                 if (sizeId > 1) {
     616        1188 :                     ses(scaling_list_dc_coef_minus8[sizeId - 2][matrixId], -7, +247,
     617             :                         2, sizeId - 2, matrixId);
     618             :                 }
     619      107556 :                 for (i = 0; i < n; i++) {
     620      105600 :                     ses(scaling_list_delta_coeff[sizeId][matrixId][i],
     621             :                         -128, +127, 3, sizeId, matrixId, i);
     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 :                     us(bit_depth, sps_palette_predictor_initializers[comp][i],
     668             :                        0, MAX_UINT_BITS(bit_depth), 2, comp, i);
     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 :         ues(sps_max_dec_pic_buffering_minus1[i],
     752             :             0, HEVC_MAX_DPB_SIZE - 1,                        1, i);
     753          96 :         ues(sps_max_num_reorder_pics[i],
     754             :             0, current->sps_max_dec_pic_buffering_minus1[i], 1, i);
     755          96 :         ues(sps_max_latency_increase_plus1[i],
     756             :             0, UINT32_MAX - 1,                               1, i);
     757             :     }
     758          96 :     if (!current->sps_sub_layer_ordering_info_present_flag) {
     759          64 :         for (i = 0; i < current->sps_max_sub_layers_minus1; i++) {
     760          40 :             infer(sps_max_dec_pic_buffering_minus1[i],
     761             :                   current->sps_max_dec_pic_buffering_minus1[current->sps_max_sub_layers_minus1]);
     762          40 :             infer(sps_max_num_reorder_pics[i],
     763             :                   current->sps_max_num_reorder_pics[current->sps_max_sub_layers_minus1]);
     764          40 :             infer(sps_max_latency_increase_plus1[i],
     765             :                   current->sps_max_latency_increase_plus1[current->sps_max_sub_layers_minus1]);
     766             :         }
     767             :     }
     768             : 
     769          96 :     ue(log2_min_luma_coding_block_size_minus3,   0, 3);
     770          96 :     min_cb_log2_size_y = current->log2_min_luma_coding_block_size_minus3 + 3;
     771             : 
     772          96 :     ue(log2_diff_max_min_luma_coding_block_size, 0, 3);
     773          96 :     ctb_log2_size_y = min_cb_log2_size_y +
     774          96 :         current->log2_diff_max_min_luma_coding_block_size;
     775             : 
     776          96 :     min_cb_size_y = 1 << min_cb_log2_size_y;
     777         192 :     if (current->pic_width_in_luma_samples  % min_cb_size_y ||
     778          96 :         current->pic_height_in_luma_samples % min_cb_size_y) {
     779           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid dimensions: %ux%u not divisible "
     780           0 :                "by MinCbSizeY = %u.\n", current->pic_width_in_luma_samples,
     781           0 :                current->pic_height_in_luma_samples, min_cb_size_y);
     782           0 :         return AVERROR_INVALIDDATA;
     783             :     }
     784             : 
     785          96 :     ue(log2_min_luma_transform_block_size_minus2, 0, min_cb_log2_size_y - 3);
     786          96 :     min_tb_log2_size_y = current->log2_min_luma_transform_block_size_minus2 + 2;
     787             : 
     788          96 :     ue(log2_diff_max_min_luma_transform_block_size,
     789             :        0, FFMIN(ctb_log2_size_y, 5) - min_tb_log2_size_y);
     790             : 
     791          96 :     ue(max_transform_hierarchy_depth_inter,
     792             :        0, ctb_log2_size_y - min_tb_log2_size_y);
     793          96 :     ue(max_transform_hierarchy_depth_intra,
     794             :        0, ctb_log2_size_y - min_tb_log2_size_y);
     795             : 
     796          96 :     flag(scaling_list_enabled_flag);
     797          96 :     if (current->scaling_list_enabled_flag) {
     798           8 :         flag(sps_scaling_list_data_present_flag);
     799           8 :         if (current->sps_scaling_list_data_present_flag)
     800           8 :             CHECK(FUNC(scaling_list_data)(ctx, rw, &current->scaling_list));
     801             :     } else {
     802          88 :         infer(sps_scaling_list_data_present_flag, 0);
     803             :     }
     804             : 
     805          96 :     flag(amp_enabled_flag);
     806          96 :     flag(sample_adaptive_offset_enabled_flag);
     807             : 
     808          96 :     flag(pcm_enabled_flag);
     809          96 :     if (current->pcm_enabled_flag) {
     810           4 :         u(4, pcm_sample_bit_depth_luma_minus1,
     811             :           0, current->bit_depth_luma_minus8 + 8 - 1);
     812           4 :         u(4, pcm_sample_bit_depth_chroma_minus1,
     813             :           0, current->bit_depth_chroma_minus8 + 8 - 1);
     814             : 
     815           4 :         ue(log2_min_pcm_luma_coding_block_size_minus3,
     816             :            FFMIN(min_cb_log2_size_y, 5) - 3, FFMIN(ctb_log2_size_y, 5) - 3);
     817           4 :         ue(log2_diff_max_min_pcm_luma_coding_block_size,
     818             :            0, FFMIN(ctb_log2_size_y, 5) - (current->log2_min_pcm_luma_coding_block_size_minus3 + 3));
     819             : 
     820           4 :         flag(pcm_loop_filter_disabled_flag);
     821             :     }
     822             : 
     823          96 :     ue(num_short_term_ref_pic_sets, 0, HEVC_MAX_SHORT_TERM_REF_PIC_SETS);
     824         864 :     for (i = 0; i < current->num_short_term_ref_pic_sets; i++)
     825         768 :         CHECK(FUNC(st_ref_pic_set)(ctx, rw, &current->st_ref_pic_set[i], i, current));
     826             : 
     827          96 :     flag(long_term_ref_pics_present_flag);
     828          96 :     if (current->long_term_ref_pics_present_flag) {
     829          28 :         ue(num_long_term_ref_pics_sps, 0, HEVC_MAX_LONG_TERM_REF_PICS);
     830          60 :         for (i = 0; i < current->num_long_term_ref_pics_sps; i++) {
     831          32 :             us(current->log2_max_pic_order_cnt_lsb_minus4 + 4,
     832             :                lt_ref_pic_poc_lsb_sps[i],
     833             :                0, MAX_UINT_BITS(current->log2_max_pic_order_cnt_lsb_minus4 + 4), 1, i);
     834          32 :             flags(used_by_curr_pic_lt_sps_flag[i], 1, i);
     835             :         }
     836             :     }
     837             : 
     838          96 :     flag(sps_temporal_mvp_enabled_flag);
     839          96 :     flag(strong_intra_smoothing_enabled_flag);
     840             : 
     841          96 :     flag(vui_parameters_present_flag);
     842          96 :     if (current->vui_parameters_present_flag)
     843          36 :         CHECK(FUNC(vui_parameters)(ctx, rw, &current->vui, current));
     844             : 
     845          96 :     flag(sps_extension_present_flag);
     846          96 :     if (current->sps_extension_present_flag) {
     847           0 :         flag(sps_range_extension_flag);
     848           0 :         flag(sps_multilayer_extension_flag);
     849           0 :         flag(sps_3d_extension_flag);
     850           0 :         flag(sps_scc_extension_flag);
     851           0 :         u(4, sps_extension_4bits, 0, MAX_UINT_BITS(4));
     852             :     }
     853             : 
     854          96 :     if (current->sps_range_extension_flag)
     855           0 :         CHECK(FUNC(sps_range_extension)(ctx, rw, current));
     856          96 :     if (current->sps_multilayer_extension_flag)
     857           0 :         return AVERROR_PATCHWELCOME;
     858          96 :     if (current->sps_3d_extension_flag)
     859           0 :         return AVERROR_PATCHWELCOME;
     860          96 :     if (current->sps_scc_extension_flag)
     861           0 :         CHECK(FUNC(sps_scc_extension)(ctx, rw, current));
     862          96 :     if (current->sps_extension_4bits)
     863           0 :         CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
     864             : 
     865          96 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
     866             : 
     867          96 :     return 0;
     868             : }
     869             : 
     870           0 : static int FUNC(pps_range_extension)(CodedBitstreamContext *ctx, RWContext *rw,
     871             :                                      H265RawPPS *current)
     872             : {
     873           0 :     CodedBitstreamH265Context *h265 = ctx->priv_data;
     874           0 :     const H265RawSPS *sps = h265->active_sps;
     875             :     int err, i;
     876             : 
     877           0 :     if (current->transform_skip_enabled_flag)
     878           0 :         ue(log2_max_transform_skip_block_size_minus2, 0, 3);
     879           0 :     flag(cross_component_prediction_enabled_flag);
     880             : 
     881           0 :     flag(chroma_qp_offset_list_enabled_flag);
     882           0 :     if (current->chroma_qp_offset_list_enabled_flag) {
     883           0 :         ue(diff_cu_chroma_qp_offset_depth,
     884             :            0, sps->log2_diff_max_min_luma_coding_block_size);
     885           0 :         ue(chroma_qp_offset_list_len_minus1, 0, 5);
     886           0 :         for (i = 0; i <= current->chroma_qp_offset_list_len_minus1; i++) {
     887           0 :             ses(cb_qp_offset_list[i], -12, +12, 1, i);
     888           0 :             ses(cr_qp_offset_list[i], -12, +12, 1, i);
     889             :         }
     890             :     }
     891             : 
     892           0 :     ue(log2_sao_offset_scale_luma,   0, FFMAX(0, sps->bit_depth_luma_minus8   - 2));
     893           0 :     ue(log2_sao_offset_scale_chroma, 0, FFMAX(0, sps->bit_depth_chroma_minus8 - 2));
     894             : 
     895           0 :     return 0;
     896             : }
     897             : 
     898           0 : static int FUNC(pps_scc_extension)(CodedBitstreamContext *ctx, RWContext *rw,
     899             :                                    H265RawPPS *current)
     900             : {
     901             :     int err, comp, i;
     902             : 
     903           0 :     flag(pps_curr_pic_ref_enabled_flag);
     904             : 
     905           0 :     flag(residual_adaptive_colour_transform_enabled_flag);
     906           0 :     if (current->residual_adaptive_colour_transform_enabled_flag) {
     907           0 :         flag(pps_slice_act_qp_offsets_present_flag);
     908           0 :         se(pps_act_y_qp_offset_plus5,  -7, +17);
     909           0 :         se(pps_act_cb_qp_offset_plus5, -7, +17);
     910           0 :         se(pps_act_cr_qp_offset_plus3, -9, +15);
     911             :     } else {
     912           0 :         infer(pps_slice_act_qp_offsets_present_flag, 0);
     913           0 :         infer(pps_act_y_qp_offset_plus5,  0);
     914           0 :         infer(pps_act_cb_qp_offset_plus5, 0);
     915           0 :         infer(pps_act_cr_qp_offset_plus3, 0);
     916             :     }
     917             : 
     918           0 :     flag(pps_palette_predictor_initializer_present_flag);
     919           0 :     if (current->pps_palette_predictor_initializer_present_flag) {
     920           0 :         ue(pps_num_palette_predictor_initializer, 0, 128);
     921           0 :         if (current->pps_num_palette_predictor_initializer > 0) {
     922           0 :             flag(monochrome_palette_flag);
     923           0 :             ue(luma_bit_depth_entry_minus8, 0, 8);
     924           0 :             if (!current->monochrome_palette_flag)
     925           0 :                 ue(chroma_bit_depth_entry_minus8, 0, 8);
     926           0 :             for (comp = 0; comp < (current->monochrome_palette_flag ? 1 : 3); comp++) {
     927           0 :                 int bit_depth = comp == 0 ? current->luma_bit_depth_entry_minus8 + 8
     928           0 :                                           : current->chroma_bit_depth_entry_minus8 + 8;
     929           0 :                 for (i = 0; i < current->pps_num_palette_predictor_initializer; i++)
     930           0 :                     us(bit_depth, pps_palette_predictor_initializers[comp][i],
     931             :                        0, MAX_UINT_BITS(bit_depth), 2, comp, i);
     932             :             }
     933             :         }
     934             :     }
     935             : 
     936           0 :     return 0;
     937             : }
     938             : 
     939         546 : static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
     940             :                      H265RawPPS *current)
     941             : {
     942         546 :     CodedBitstreamH265Context *h265 = ctx->priv_data;
     943             :     const H265RawSPS *sps;
     944             :     int err, i;
     945             : 
     946         546 :     HEADER("Picture Parameter Set");
     947             : 
     948         546 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_PPS));
     949             : 
     950         546 :     ue(pps_pic_parameter_set_id, 0, 63);
     951         546 :     ue(pps_seq_parameter_set_id, 0, 15);
     952         546 :     sps = h265->sps[current->pps_seq_parameter_set_id];
     953         546 :     if (!sps) {
     954           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
     955           0 :                current->pps_seq_parameter_set_id);
     956           0 :         return AVERROR_INVALIDDATA;
     957             :     }
     958         546 :     h265->active_sps = sps;
     959             : 
     960         546 :     flag(dependent_slice_segments_enabled_flag);
     961         546 :     flag(output_flag_present_flag);
     962         546 :     u(3, num_extra_slice_header_bits, 0, 7);
     963         546 :     flag(sign_data_hiding_enabled_flag);
     964         546 :     flag(cabac_init_present_flag);
     965             : 
     966         546 :     ue(num_ref_idx_l0_default_active_minus1, 0, 14);
     967         546 :     ue(num_ref_idx_l1_default_active_minus1, 0, 14);
     968             : 
     969         546 :     se(init_qp_minus26, -(26 + 6 * sps->bit_depth_luma_minus8), +25);
     970             : 
     971         546 :     flag(constrained_intra_pred_flag);
     972         546 :     flag(transform_skip_enabled_flag);
     973         546 :     flag(cu_qp_delta_enabled_flag);
     974         546 :     if (current->cu_qp_delta_enabled_flag)
     975          12 :         ue(diff_cu_qp_delta_depth,
     976             :            0, sps->log2_diff_max_min_luma_coding_block_size);
     977             :     else
     978         534 :         infer(diff_cu_qp_delta_depth, 0);
     979             : 
     980         546 :     se(pps_cb_qp_offset, -12, +12);
     981         546 :     se(pps_cr_qp_offset, -12, +12);
     982         546 :     flag(pps_slice_chroma_qp_offsets_present_flag);
     983             : 
     984         546 :     flag(weighted_pred_flag);
     985         546 :     flag(weighted_bipred_flag);
     986             : 
     987         546 :     flag(transquant_bypass_enabled_flag);
     988         546 :     flag(tiles_enabled_flag);
     989         546 :     flag(entropy_coding_sync_enabled_flag);
     990             : 
     991         546 :     if (current->tiles_enabled_flag) {
     992         210 :         ue(num_tile_columns_minus1, 0, HEVC_MAX_TILE_COLUMNS);
     993         210 :         ue(num_tile_rows_minus1,    0, HEVC_MAX_TILE_ROWS);
     994         210 :         flag(uniform_spacing_flag);
     995         210 :         if (!current->uniform_spacing_flag) {
     996        1010 :             for (i = 0; i < current->num_tile_columns_minus1; i++)
     997         808 :                 ues(column_width_minus1[i], 0, sps->pic_width_in_luma_samples,  1, i);
     998        1010 :             for (i = 0; i < current->num_tile_rows_minus1; i++)
     999         808 :                 ues(row_height_minus1[i],   0, sps->pic_height_in_luma_samples, 1, i);
    1000             :         }
    1001         210 :         flag(loop_filter_across_tiles_enabled_flag);
    1002             :     } else {
    1003         336 :         infer(num_tile_columns_minus1, 0);
    1004         336 :         infer(num_tile_rows_minus1,    0);
    1005             :     }
    1006             : 
    1007         546 :     flag(pps_loop_filter_across_slices_enabled_flag);
    1008         546 :     flag(deblocking_filter_control_present_flag);
    1009         546 :     if (current->deblocking_filter_control_present_flag) {
    1010         226 :         flag(deblocking_filter_override_enabled_flag);
    1011         226 :         flag(pps_deblocking_filter_disabled_flag);
    1012         226 :         if (!current->pps_deblocking_filter_disabled_flag) {
    1013         222 :             se(pps_beta_offset_div2, -6, +6);
    1014         222 :             se(pps_tc_offset_div2,   -6, +6);
    1015             :         } else {
    1016           4 :             infer(pps_beta_offset_div2, 0);
    1017           4 :             infer(pps_tc_offset_div2,   0);
    1018             :         }
    1019             :     } else {
    1020         320 :         infer(deblocking_filter_override_enabled_flag, 0);
    1021         320 :         infer(pps_deblocking_filter_disabled_flag,     0);
    1022         320 :         infer(pps_beta_offset_div2, 0);
    1023         320 :         infer(pps_tc_offset_div2,   0);
    1024             :     }
    1025             : 
    1026         546 :     flag(pps_scaling_list_data_present_flag);
    1027         546 :     if (current->pps_scaling_list_data_present_flag)
    1028         168 :         CHECK(FUNC(scaling_list_data)(ctx, rw, &current->scaling_list));
    1029             : 
    1030         546 :     flag(lists_modification_present_flag);
    1031             : 
    1032         546 :     ue(log2_parallel_merge_level_minus2,
    1033             :        0, (sps->log2_min_luma_coding_block_size_minus3 + 3 +
    1034             :            sps->log2_diff_max_min_luma_coding_block_size - 2));
    1035             : 
    1036         546 :     flag(slice_segment_header_extension_present_flag);
    1037             : 
    1038         546 :     flag(pps_extension_present_flag);
    1039         546 :     if (current->pps_extension_present_flag) {
    1040           0 :         flag(pps_range_extension_flag);
    1041           0 :         flag(pps_multilayer_extension_flag);
    1042           0 :         flag(pps_3d_extension_flag);
    1043           0 :         flag(pps_scc_extension_flag);
    1044           0 :         u(4, pps_extension_4bits, 0, MAX_UINT_BITS(4));
    1045             :     }
    1046         546 :     if (current->pps_range_extension_flag)
    1047           0 :         CHECK(FUNC(pps_range_extension)(ctx, rw, current));
    1048         546 :     if (current->pps_multilayer_extension_flag)
    1049           0 :         return AVERROR_PATCHWELCOME;
    1050         546 :     if (current->pps_3d_extension_flag)
    1051           0 :         return AVERROR_PATCHWELCOME;
    1052         546 :     if (current->pps_scc_extension_flag)
    1053           0 :         CHECK(FUNC(pps_scc_extension)(ctx, rw, current));
    1054         546 :     if (current->pps_extension_4bits)
    1055           0 :         CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
    1056             : 
    1057         546 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
    1058             : 
    1059         546 :     return 0;
    1060             : }
    1061             : 
    1062         120 : static int FUNC(aud)(CodedBitstreamContext *ctx, RWContext *rw,
    1063             :                      H265RawAUD *current)
    1064             : {
    1065             :     int err;
    1066             : 
    1067         120 :     HEADER("Access Unit Delimiter");
    1068             : 
    1069         120 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_AUD));
    1070             : 
    1071         120 :     u(3, pic_type, 0, 2);
    1072             : 
    1073         120 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
    1074             : 
    1075         120 :     return 0;
    1076             : }
    1077             : 
    1078        1496 : static int FUNC(ref_pic_lists_modification)(CodedBitstreamContext *ctx, RWContext *rw,
    1079             :                                             H265RawSliceHeader *current,
    1080             :                                             unsigned int num_pic_total_curr)
    1081             : {
    1082             :     unsigned int entry_size;
    1083             :     int err, i;
    1084             : 
    1085        1496 :     entry_size = av_log2(num_pic_total_curr - 1) + 1;
    1086             : 
    1087        1496 :     flag(ref_pic_list_modification_flag_l0);
    1088        1496 :     if (current->ref_pic_list_modification_flag_l0) {
    1089        2398 :         for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++)
    1090        1672 :             us(entry_size, list_entry_l0[i], 0, num_pic_total_curr - 1, 1, i);
    1091             :     }
    1092             : 
    1093        1496 :     if (current->slice_type == HEVC_SLICE_B) {
    1094        1496 :         flag(ref_pic_list_modification_flag_l1);
    1095        1496 :         if (current->ref_pic_list_modification_flag_l1) {
    1096        1436 :             for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++)
    1097        1010 :                 us(entry_size, list_entry_l1[i], 0, num_pic_total_curr - 1, 1, i);
    1098             :         }
    1099             :     }
    1100             : 
    1101        1496 :     return 0;
    1102             : }
    1103             : 
    1104         510 : static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
    1105             :                                    H265RawSliceHeader *current)
    1106             : {
    1107         510 :     CodedBitstreamH265Context *h265 = ctx->priv_data;
    1108         510 :     const H265RawSPS *sps = h265->active_sps;
    1109             :     int err, i, j;
    1110        1020 :     int chroma = !sps->separate_colour_plane_flag &&
    1111         510 :                   sps->chroma_format_idc != 0;
    1112             : 
    1113         510 :     ue(luma_log2_weight_denom, 0, 7);
    1114         510 :     if (chroma)
    1115         510 :         se(delta_chroma_log2_weight_denom, -7, 7);
    1116             :     else
    1117           0 :         infer(delta_chroma_log2_weight_denom, 0);
    1118             : 
    1119        2538 :     for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
    1120             :         if (1 /* is not same POC and same layer_id */)
    1121        2028 :             flags(luma_weight_l0_flag[i], 1, i);
    1122             :         else
    1123             :             infer(luma_weight_l0_flag[i], 0);
    1124             :     }
    1125         510 :     if (chroma) {
    1126        2538 :         for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
    1127             :             if (1 /* is not same POC and same layer_id */)
    1128        2028 :                 flags(chroma_weight_l0_flag[i], 1, i);
    1129             :             else
    1130             :                 infer(chroma_weight_l0_flag[i], 0);
    1131             :         }
    1132             :     }
    1133             : 
    1134        2538 :     for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
    1135        2028 :         if (current->luma_weight_l0_flag[i]) {
    1136        1540 :             ses(delta_luma_weight_l0[i], -128, +127, 1, i);
    1137        1540 :             ses(luma_offset_l0[i],
    1138             :                 -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)),
    1139             :                 ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1), 1, i);
    1140             :         } else {
    1141         488 :             infer(delta_luma_weight_l0[i], 0);
    1142         488 :             infer(luma_offset_l0[i],       0);
    1143             :         }
    1144        2028 :         if (current->chroma_weight_l0_flag[i]) {
    1145        1518 :             for (j = 0; j < 2; j++) {
    1146        1012 :                 ses(delta_chroma_weight_l0[i][j], -128, +127, 2, i, j);
    1147        1012 :                 ses(chroma_offset_l0[i][j],
    1148             :                     -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)),
    1149             :                     ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1), 2, i, j);
    1150             :             }
    1151             :         } else {
    1152        4566 :             for (j = 0; j < 2; j++) {
    1153        3044 :                 infer(delta_chroma_weight_l0[i][j], 0);
    1154        3044 :                 infer(chroma_offset_l0[i][j],       0);
    1155             :             }
    1156             :         }
    1157             :     }
    1158             : 
    1159         510 :     if (current->slice_type == HEVC_SLICE_B) {
    1160           0 :         for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
    1161             :             if (1 /* RefPicList1[i] is not CurrPic, nor is it in a different layer */)
    1162           0 :                 flags(luma_weight_l1_flag[i], 1, i);
    1163             :             else
    1164             :                 infer(luma_weight_l1_flag[i], 0);
    1165             :         }
    1166           0 :         if (chroma) {
    1167           0 :             for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
    1168             :                 if (1 /* RefPicList1[i] is not CurrPic, nor is it in a different layer */)
    1169           0 :                     flags(chroma_weight_l1_flag[i], 1, i);
    1170             :                 else
    1171             :                     infer(chroma_weight_l1_flag[i], 0);
    1172             :             }
    1173             :         }
    1174             : 
    1175           0 :         for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
    1176           0 :             if (current->luma_weight_l1_flag[i]) {
    1177           0 :                 ses(delta_luma_weight_l1[i], -128, +127, 1, i);
    1178           0 :                 ses(luma_offset_l1[i],
    1179             :                     -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)),
    1180             :                     ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1), 1, i);
    1181             :             } else {
    1182           0 :                 infer(delta_luma_weight_l1[i], 0);
    1183           0 :                 infer(luma_offset_l1[i],       0);
    1184             :             }
    1185           0 :             if (current->chroma_weight_l1_flag[i]) {
    1186           0 :                 for (j = 0; j < 2; j++) {
    1187           0 :                     ses(delta_chroma_weight_l1[i][j], -128, +127, 2, i, j);
    1188           0 :                     ses(chroma_offset_l1[i][j],
    1189             :                         -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)),
    1190             :                         ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1), 2, i, j);
    1191             :                 }
    1192             :             } else {
    1193           0 :                 for (j = 0; j < 2; j++) {
    1194           0 :                     infer(delta_chroma_weight_l1[i][j], 0);
    1195           0 :                     infer(chroma_offset_l1[i][j],       0);
    1196             :                 }
    1197             :             }
    1198             :         }
    1199             :     }
    1200             : 
    1201         510 :     return 0;
    1202             : }
    1203             : 
    1204       11662 : static int FUNC(slice_segment_header)(CodedBitstreamContext *ctx, RWContext *rw,
    1205             :                                       H265RawSliceHeader *current)
    1206             : {
    1207       11662 :     CodedBitstreamH265Context *h265 = ctx->priv_data;
    1208             :     const H265RawSPS *sps;
    1209             :     const H265RawPPS *pps;
    1210             :     unsigned int min_cb_log2_size_y, ctb_log2_size_y, ctb_size_y;
    1211             :     unsigned int pic_width_in_ctbs_y, pic_height_in_ctbs_y, pic_size_in_ctbs_y;
    1212       11662 :     unsigned int num_pic_total_curr = 0;
    1213             :     int err, i;
    1214             : 
    1215       11662 :     HEADER("Slice Segment Header");
    1216             : 
    1217       11662 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, -1));
    1218             : 
    1219       11662 :     flag(first_slice_segment_in_pic_flag);
    1220             : 
    1221       11918 :     if (current->nal_unit_header.nal_unit_type >= HEVC_NAL_BLA_W_LP &&
    1222         256 :         current->nal_unit_header.nal_unit_type <= HEVC_NAL_IRAP_VCL23)
    1223         256 :         flag(no_output_of_prior_pics_flag);
    1224             : 
    1225       11662 :     ue(slice_pic_parameter_set_id, 0, 63);
    1226             : 
    1227       11662 :     pps = h265->pps[current->slice_pic_parameter_set_id];
    1228       11662 :     if (!pps) {
    1229           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
    1230           0 :                current->slice_pic_parameter_set_id);
    1231           0 :         return AVERROR_INVALIDDATA;
    1232             :     }
    1233       11662 :     h265->active_pps = pps;
    1234             : 
    1235       11662 :     sps = h265->sps[pps->pps_seq_parameter_set_id];
    1236       11662 :     if (!sps) {
    1237           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
    1238           0 :                pps->pps_seq_parameter_set_id);
    1239           0 :         return AVERROR_INVALIDDATA;
    1240             :     }
    1241       11662 :     h265->active_sps = sps;
    1242             : 
    1243       11662 :     min_cb_log2_size_y = sps->log2_min_luma_coding_block_size_minus3 + 3;
    1244       11662 :     ctb_log2_size_y = min_cb_log2_size_y + sps->log2_diff_max_min_luma_coding_block_size;
    1245       11662 :     ctb_size_y = 1 << ctb_log2_size_y;
    1246       11662 :     pic_width_in_ctbs_y =
    1247       11662 :         (sps->pic_width_in_luma_samples + ctb_size_y - 1) / ctb_size_y;
    1248       11662 :     pic_height_in_ctbs_y =
    1249       11662 :         (sps->pic_height_in_luma_samples + ctb_size_y - 1) / ctb_size_y;
    1250       11662 :     pic_size_in_ctbs_y = pic_width_in_ctbs_y * pic_height_in_ctbs_y;
    1251             : 
    1252       11662 :     if (!current->first_slice_segment_in_pic_flag) {
    1253        7464 :         unsigned int address_size = av_log2(pic_size_in_ctbs_y - 1) + 1;
    1254        7464 :         if (pps->dependent_slice_segments_enabled_flag)
    1255        4082 :             flag(dependent_slice_segment_flag);
    1256             :         else
    1257        3382 :             infer(dependent_slice_segment_flag, 0);
    1258        7464 :         u(address_size, slice_segment_address, 0, pic_size_in_ctbs_y - 1);
    1259             :     } else {
    1260        4198 :         infer(dependent_slice_segment_flag, 0);
    1261             :     }
    1262             : 
    1263       11662 :     if (!current->dependent_slice_segment_flag) {
    1264        7894 :         for (i = 0; i < pps->num_extra_slice_header_bits; i++)
    1265           0 :             flags(slice_reserved_flag[i], 1, i);
    1266             : 
    1267        7894 :         ue(slice_type, 0, 2);
    1268             : 
    1269        7894 :         if (pps->output_flag_present_flag)
    1270           0 :             flag(pic_output_flag);
    1271             : 
    1272        7894 :         if (sps->separate_colour_plane_flag)
    1273           0 :             u(2, colour_plane_id, 0, 2);
    1274             : 
    1275       15684 :         if (current->nal_unit_header.nal_unit_type != HEVC_NAL_IDR_W_RADL &&
    1276        7790 :             current->nal_unit_header.nal_unit_type != HEVC_NAL_IDR_N_LP) {
    1277             :             const H265RawSTRefPicSet *rps;
    1278             : 
    1279        7788 :             u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, slice_pic_order_cnt_lsb,
    1280             :               0, MAX_UINT_BITS(sps->log2_max_pic_order_cnt_lsb_minus4 + 4));
    1281             : 
    1282        7788 :             flag(short_term_ref_pic_set_sps_flag);
    1283        7788 :             if (!current->short_term_ref_pic_set_sps_flag) {
    1284        3730 :                 CHECK(FUNC(st_ref_pic_set)(ctx, rw, &current->short_term_ref_pic_set,
    1285             :                                            sps->num_short_term_ref_pic_sets, sps));
    1286        3730 :                 rps = &current->short_term_ref_pic_set;
    1287        4058 :             } else if (sps->num_short_term_ref_pic_sets > 1) {
    1288        4058 :                 unsigned int idx_size = av_log2(sps->num_short_term_ref_pic_sets - 1) + 1;
    1289        4058 :                 u(idx_size, short_term_ref_pic_set_idx,
    1290             :                   0, sps->num_short_term_ref_pic_sets - 1);
    1291        4058 :                 rps = &sps->st_ref_pic_set[current->short_term_ref_pic_set_idx];
    1292             :             } else {
    1293           0 :                 infer(short_term_ref_pic_set_idx, 0);
    1294           0 :                 rps = &sps->st_ref_pic_set[0];
    1295             :             }
    1296             : 
    1297        7788 :             num_pic_total_curr = 0;
    1298       29156 :             for (i = 0; i < rps->num_negative_pics; i++)
    1299       21368 :                 if (rps->used_by_curr_pic_s0_flag[i])
    1300       20320 :                     ++num_pic_total_curr;
    1301       12970 :             for (i = 0; i < rps->num_positive_pics; i++)
    1302        5182 :                 if (rps->used_by_curr_pic_s1_flag[i])
    1303        4892 :                     ++num_pic_total_curr;
    1304             : 
    1305        7788 :             if (sps->long_term_ref_pics_present_flag) {
    1306             :                 unsigned int idx_size;
    1307             : 
    1308        1664 :                 if (sps->num_long_term_ref_pics_sps > 0) {
    1309         998 :                     ue(num_long_term_sps, 0, sps->num_long_term_ref_pics_sps);
    1310         998 :                     idx_size = av_log2(sps->num_long_term_ref_pics_sps - 1) + 1;
    1311             :                 } else {
    1312         666 :                     infer(num_long_term_sps, 0);
    1313         666 :                     idx_size = 0;
    1314             :                 }
    1315        1664 :                 ue(num_long_term_pics, 0, HEVC_MAX_LONG_TERM_REF_PICS);
    1316             : 
    1317       10632 :                 for (i = 0; i < current->num_long_term_sps +
    1318        7304 :                                 current->num_long_term_pics; i++) {
    1319        2820 :                     if (i < current->num_long_term_sps) {
    1320         368 :                         if (sps->num_long_term_ref_pics_sps > 1)
    1321         368 :                             us(idx_size, lt_idx_sps[i],
    1322             :                                0, sps->num_long_term_ref_pics_sps - 1, 1, i);
    1323         368 :                         if (sps->used_by_curr_pic_lt_sps_flag[current->lt_idx_sps[i]])
    1324         176 :                             ++num_pic_total_curr;
    1325             :                     } else {
    1326        2452 :                         us(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, poc_lsb_lt[i],
    1327             :                            0, MAX_UINT_BITS(sps->log2_max_pic_order_cnt_lsb_minus4 + 4), 1, i);
    1328        2452 :                         flags(used_by_curr_pic_lt_flag[i], 1, i);
    1329        2452 :                         if (current->used_by_curr_pic_lt_flag[i])
    1330         756 :                             ++num_pic_total_curr;
    1331             :                     }
    1332        2820 :                     flags(delta_poc_msb_present_flag[i], 1, i);
    1333        2820 :                     if (current->delta_poc_msb_present_flag[i])
    1334           0 :                         ues(delta_poc_msb_cycle_lt[i], 0, UINT32_MAX - 1, 1, i);
    1335             :                     else
    1336        2820 :                         infer(delta_poc_msb_cycle_lt[i], 0);
    1337             :                 }
    1338             :             }
    1339             : 
    1340        7788 :             if (sps->sps_temporal_mvp_enabled_flag)
    1341        5142 :                 flag(slice_temporal_mvp_enabled_flag);
    1342             :             else
    1343        2646 :                 infer(slice_temporal_mvp_enabled_flag, 0);
    1344             : 
    1345        7788 :             if (pps->pps_curr_pic_ref_enabled_flag)
    1346           0 :                 ++num_pic_total_curr;
    1347             :         }
    1348             : 
    1349        7894 :         if (sps->sample_adaptive_offset_enabled_flag) {
    1350        4626 :             flag(slice_sao_luma_flag);
    1351        4626 :             if (!sps->separate_colour_plane_flag && sps->chroma_format_idc != 0)
    1352        4626 :                 flag(slice_sao_chroma_flag);
    1353             :             else
    1354           0 :                 infer(slice_sao_chroma_flag, 0);
    1355             :         } else {
    1356        3268 :             infer(slice_sao_luma_flag,   0);
    1357        3268 :             infer(slice_sao_chroma_flag, 0);
    1358             :         }
    1359             : 
    1360       12484 :         if (current->slice_type == HEVC_SLICE_P ||
    1361        4590 :             current->slice_type == HEVC_SLICE_B) {
    1362        7690 :             flag(num_ref_idx_active_override_flag);
    1363        7690 :             if (current->num_ref_idx_active_override_flag) {
    1364        3620 :                 ue(num_ref_idx_l0_active_minus1, 0, 14);
    1365        3620 :                 if (current->slice_type == HEVC_SLICE_B)
    1366         912 :                     ue(num_ref_idx_l1_active_minus1, 0, 14);
    1367             :                 else
    1368        2708 :                     infer(num_ref_idx_l1_active_minus1, pps->num_ref_idx_l1_default_active_minus1);
    1369             :             } else {
    1370        4070 :                 infer(num_ref_idx_l0_active_minus1, pps->num_ref_idx_l0_default_active_minus1);
    1371        4070 :                 infer(num_ref_idx_l1_active_minus1, pps->num_ref_idx_l1_default_active_minus1);
    1372             :             }
    1373             : 
    1374        7690 :             if (pps->lists_modification_present_flag && num_pic_total_curr > 1)
    1375        1496 :                 CHECK(FUNC(ref_pic_lists_modification)(ctx, rw, current,
    1376             :                                                        num_pic_total_curr));
    1377             : 
    1378        7690 :             if (current->slice_type == HEVC_SLICE_B)
    1379        4386 :                 flag(mvd_l1_zero_flag);
    1380        7690 :             if (pps->cabac_init_present_flag)
    1381        4938 :                 flag(cabac_init_flag);
    1382             :             else
    1383        2752 :                 infer(cabac_init_flag, 0);
    1384        7690 :             if (current->slice_temporal_mvp_enabled_flag) {
    1385        5054 :                 if (current->slice_type == HEVC_SLICE_B)
    1386        4386 :                     flag(collocated_from_l0_flag);
    1387             :                 else
    1388         668 :                     infer(collocated_from_l0_flag, 1);
    1389        5054 :                 if (current->collocated_from_l0_flag) {
    1390        2298 :                     if (current->num_ref_idx_l0_active_minus1 > 0)
    1391        2068 :                         ue(collocated_ref_idx, 0, current->num_ref_idx_l0_active_minus1);
    1392             :                     else
    1393         230 :                         infer(collocated_ref_idx, 0);
    1394             :                 } else {
    1395        2756 :                     if (current->num_ref_idx_l1_active_minus1 > 0)
    1396        2574 :                         ue(collocated_ref_idx, 0, current->num_ref_idx_l1_active_minus1);
    1397             :                     else
    1398         182 :                         infer(collocated_ref_idx, 0);
    1399             :                 }
    1400             :             }
    1401             : 
    1402       14870 :             if ((pps->weighted_pred_flag   && current->slice_type == HEVC_SLICE_P) ||
    1403        7180 :                 (pps->weighted_bipred_flag && current->slice_type == HEVC_SLICE_B))
    1404         510 :                 CHECK(FUNC(pred_weight_table)(ctx, rw, current));
    1405             : 
    1406        7690 :             ue(five_minus_max_num_merge_cand, 0, 4);
    1407        7690 :             if (sps->motion_vector_resolution_control_idc == 2)
    1408           0 :                 flag(use_integer_mv_flag);
    1409             :             else
    1410        7690 :                 infer(use_integer_mv_flag, sps->motion_vector_resolution_control_idc);
    1411             :         }
    1412             : 
    1413        7894 :         se(slice_qp_delta,
    1414             :            - 6 * sps->bit_depth_luma_minus8 - (pps->init_qp_minus26 + 26),
    1415             :            + 51 - (pps->init_qp_minus26 + 26));
    1416        7894 :         if (pps->pps_slice_chroma_qp_offsets_present_flag) {
    1417           0 :             se(slice_cb_qp_offset, -12, +12);
    1418           0 :             se(slice_cr_qp_offset, -12, +12);
    1419             :         } else {
    1420        7894 :             infer(slice_cb_qp_offset, 0);
    1421        7894 :             infer(slice_cr_qp_offset, 0);
    1422             :         }
    1423        7894 :         if (pps->pps_slice_act_qp_offsets_present_flag) {
    1424           0 :             se(slice_act_y_qp_offset,
    1425             :                -12 - (pps->pps_act_y_qp_offset_plus5 - 5),
    1426             :                +12 - (pps->pps_act_y_qp_offset_plus5 - 5));
    1427           0 :             se(slice_act_cb_qp_offset,
    1428             :                -12 - (pps->pps_act_cb_qp_offset_plus5 - 5),
    1429             :                +12 - (pps->pps_act_cb_qp_offset_plus5 - 5));
    1430           0 :             se(slice_act_cr_qp_offset,
    1431             :                -12 - (pps->pps_act_cr_qp_offset_plus3 - 3),
    1432             :                +12 - (pps->pps_act_cr_qp_offset_plus3 - 3));
    1433             :         } else {
    1434        7894 :             infer(slice_act_y_qp_offset,  0);
    1435        7894 :             infer(slice_act_cb_qp_offset, 0);
    1436        7894 :             infer(slice_act_cr_qp_offset, 0);
    1437             :         }
    1438        7894 :         if (pps->chroma_qp_offset_list_enabled_flag)
    1439           0 :             flag(cu_chroma_qp_offset_enabled_flag);
    1440             :         else
    1441        7894 :             infer(cu_chroma_qp_offset_enabled_flag, 0);
    1442             : 
    1443        7894 :         if (pps->deblocking_filter_override_enabled_flag)
    1444          74 :             flag(deblocking_filter_override_flag);
    1445             :         else
    1446        7820 :             infer(deblocking_filter_override_flag, 0);
    1447        7894 :         if (current->deblocking_filter_override_flag) {
    1448           2 :             flag(slice_deblocking_filter_disabled_flag);
    1449           2 :             if (!current->slice_deblocking_filter_disabled_flag) {
    1450           0 :                 se(slice_beta_offset_div2, -6, +6);
    1451           0 :                 se(slice_tc_offset_div2,   -6, +6);
    1452             :             } else {
    1453           2 :                 infer(slice_beta_offset_div2, pps->pps_beta_offset_div2);
    1454           2 :                 infer(slice_tc_offset_div2,   pps->pps_tc_offset_div2);
    1455             :             }
    1456             :         } else {
    1457        7892 :             infer(slice_deblocking_filter_disabled_flag,
    1458             :                   pps->pps_deblocking_filter_disabled_flag);
    1459        7892 :             infer(slice_beta_offset_div2, pps->pps_beta_offset_div2);
    1460        7892 :             infer(slice_tc_offset_div2,   pps->pps_tc_offset_div2);
    1461             :         }
    1462       14298 :         if (pps->pps_loop_filter_across_slices_enabled_flag &&
    1463       15368 :             (current->slice_sao_luma_flag || current->slice_sao_chroma_flag ||
    1464        4482 :              !current->slice_deblocking_filter_disabled_flag))
    1465        6402 :             flag(slice_loop_filter_across_slices_enabled_flag);
    1466             :         else
    1467        1492 :             infer(slice_loop_filter_across_slices_enabled_flag,
    1468             :                   pps->pps_loop_filter_across_slices_enabled_flag);
    1469             :     }
    1470             : 
    1471       11662 :     if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
    1472             :         unsigned int num_entry_point_offsets_limit;
    1473        3610 :         if (!pps->tiles_enabled_flag && pps->entropy_coding_sync_enabled_flag)
    1474         874 :             num_entry_point_offsets_limit = pic_height_in_ctbs_y - 1;
    1475        2736 :         else if (pps->tiles_enabled_flag && !pps->entropy_coding_sync_enabled_flag)
    1476        2736 :             num_entry_point_offsets_limit =
    1477        2736 :                 (pps->num_tile_columns_minus1 + 1) * (pps->num_tile_rows_minus1 + 1);
    1478             :         else
    1479           0 :             num_entry_point_offsets_limit =
    1480           0 :                 (pps->num_tile_columns_minus1 + 1) * pic_height_in_ctbs_y - 1;
    1481        3610 :         ue(num_entry_point_offsets, 0, num_entry_point_offsets_limit);
    1482             : 
    1483        3610 :         if (current->num_entry_point_offsets > HEVC_MAX_ENTRY_POINT_OFFSETS) {
    1484           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many entry points: "
    1485           0 :                    "%"PRIu16".\n", current->num_entry_point_offsets);
    1486           0 :             return AVERROR_PATCHWELCOME;
    1487             :         }
    1488             : 
    1489        3610 :         if (current->num_entry_point_offsets > 0) {
    1490        1538 :             ue(offset_len_minus1, 0, 31);
    1491        4650 :             for (i = 0; i < current->num_entry_point_offsets; i++)
    1492        3112 :                 us(current->offset_len_minus1 + 1, entry_point_offset_minus1[i],
    1493             :                    0, MAX_UINT_BITS(current->offset_len_minus1 + 1), 1, i);
    1494             :         }
    1495             :     }
    1496             : 
    1497       11662 :     if (pps->slice_segment_header_extension_present_flag) {
    1498         874 :         ue(slice_segment_header_extension_length, 0, 256);
    1499        7078 :         for (i = 0; i < current->slice_segment_header_extension_length; i++)
    1500        6204 :             us(8, slice_segment_header_extension_data_byte[i], 0x00, 0xff, 1, i);
    1501             :     }
    1502             : 
    1503       11662 :     CHECK(FUNC(byte_alignment)(ctx, rw));
    1504             : 
    1505       11662 :     return 0;
    1506             : }
    1507             : 
    1508           0 : static int FUNC(sei_mastering_display)(CodedBitstreamContext *ctx, RWContext *rw,
    1509             :                                        H265RawSEIMasteringDisplayColourVolume *current)
    1510             : {
    1511             :     int err, c;
    1512             : 
    1513           0 :     for (c = 0; c < 3; c++) {
    1514           0 :         us(16, display_primaries_x[c], 0, 50000, 1, c);
    1515           0 :         us(16, display_primaries_y[c], 0, 50000, 1, c);
    1516             :     }
    1517             : 
    1518           0 :     u(16, white_point_x, 0, 50000);
    1519           0 :     u(16, white_point_y, 0, 50000);
    1520             : 
    1521           0 :     u(32, max_display_mastering_luminance,
    1522             :       1, MAX_UINT_BITS(32));
    1523           0 :     u(32, min_display_mastering_luminance,
    1524             :       0, current->max_display_mastering_luminance - 1);
    1525             : 
    1526           0 :     return 0;
    1527             : }
    1528             : 
    1529           0 : static int FUNC(sei_content_light_level)(CodedBitstreamContext *ctx, RWContext *rw,
    1530             :                                          H265RawSEIContentLightLevelInfo *current)
    1531             : {
    1532             :     int err;
    1533             : 
    1534           0 :     u(16, max_content_light_level, 0, MAX_UINT_BITS(16));
    1535           0 :     u(16, max_pic_average_light_level, 0, MAX_UINT_BITS(16));
    1536             : 
    1537           0 :     return 0;
    1538             : }
    1539             : 
    1540         338 : static int FUNC(sei_payload)(CodedBitstreamContext *ctx, RWContext *rw,
    1541             :                              H265RawSEIPayload *current)
    1542             : {
    1543             :     int err, i;
    1544             :     int start_position, end_position;
    1545             : 
    1546             : #ifdef READ
    1547         169 :     start_position = get_bits_count(rw);
    1548             : #else
    1549         169 :     start_position = put_bits_count(rw);
    1550             : #endif
    1551             : 
    1552         338 :     switch (current->payload_type) {
    1553           0 :     case HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO:
    1554           0 :         CHECK(FUNC(sei_mastering_display)
    1555             :               (ctx, rw, &current->payload.mastering_display));
    1556             : 
    1557           0 :         break;
    1558             : 
    1559           0 :     case HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
    1560           0 :         CHECK(FUNC(sei_content_light_level)
    1561             :               (ctx, rw, &current->payload.content_light_level));
    1562             : 
    1563           0 :         break;
    1564             : 
    1565         338 :     default:
    1566             :         {
    1567             : #ifdef READ
    1568         169 :             current->payload.other.data_length = current->payload_size;
    1569             : #endif
    1570         338 :             allocate(current->payload.other.data, current->payload.other.data_length);
    1571             : 
    1572        2512 :             for (i = 0; i < current->payload_size; i++)
    1573        2174 :                 xu(8, payload_byte[i], current->payload.other.data[i], 0, 255,
    1574             :                    1, i);
    1575             :         }
    1576             :     }
    1577             : 
    1578         338 :     if (byte_alignment(rw)) {
    1579           0 :         fixed(1, bit_equal_to_one, 1);
    1580           0 :         while (byte_alignment(rw))
    1581           0 :             fixed(1, bit_equal_to_zero, 0);
    1582             :     }
    1583             : 
    1584             : #ifdef READ
    1585         169 :     end_position = get_bits_count(rw);
    1586         169 :     if (end_position < start_position + 8 * current->payload_size) {
    1587           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Incorrect SEI payload length: "
    1588             :                "header %"PRIu32" bits, actually %d bits.\n",
    1589           0 :                8 * current->payload_size,
    1590             :                end_position - start_position);
    1591           0 :         return AVERROR_INVALIDDATA;
    1592             :     }
    1593             : #else
    1594         169 :     end_position = put_bits_count(rw);
    1595         169 :     current->payload_size = (end_position - start_position) >> 3;
    1596             : #endif
    1597             : 
    1598         338 :     return 0;
    1599             : }
    1600             : 
    1601         338 : static int FUNC(sei)(CodedBitstreamContext *ctx, RWContext *rw,
    1602             :                      H265RawSEI *current)
    1603             : {
    1604             :     int err, k;
    1605             : 
    1606         338 :     HEADER("Supplemental Enhancement Information");
    1607             : 
    1608         338 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
    1609             :                                 HEVC_NAL_SEI_PREFIX));
    1610             : 
    1611             : #ifdef READ
    1612         169 :     for (k = 0; k < H265_MAX_SEI_PAYLOADS; k++) {
    1613         169 :         uint32_t payload_type = 0;
    1614         169 :         uint32_t payload_size = 0;
    1615             :         uint32_t tmp;
    1616             : 
    1617         338 :         while (show_bits(rw, 8) == 0xff) {
    1618           0 :             fixed(8, ff_byte, 0xff);
    1619           0 :             payload_type += 255;
    1620             :         }
    1621         169 :         xu(8, last_payload_type_byte, tmp, 0, 254, 0);
    1622         169 :         payload_type += tmp;
    1623             : 
    1624         338 :         while (show_bits(rw, 8) == 0xff) {
    1625           0 :             fixed(8, ff_byte, 0xff);
    1626           0 :             payload_size += 255;
    1627             :         }
    1628         169 :         xu(8, last_payload_size_byte, tmp, 0, 254, 0);
    1629         169 :         payload_size += tmp;
    1630             : 
    1631         169 :         current->payload[k].payload_type = payload_type;
    1632         169 :         current->payload[k].payload_size = payload_size;
    1633             : 
    1634         169 :         CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
    1635             : 
    1636         169 :         if (!cbs_h2645_read_more_rbsp_data(rw))
    1637         169 :             break;
    1638             :     }
    1639         169 :     if (k >= H265_MAX_SEI_PAYLOADS) {
    1640           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
    1641             :                "SEI message: found %d.\n", k);
    1642           0 :         return AVERROR_INVALIDDATA;
    1643             :     }
    1644         169 :     current->payload_count = k + 1;
    1645             : #else
    1646         676 :     for (k = 0; k < current->payload_count; k++) {
    1647             :         PutBitContext start_state;
    1648             :         uint32_t tmp;
    1649             :         int need_size, i;
    1650             : 
    1651             :         // Somewhat clumsy: we write the payload twice when
    1652             :         // we don't know the size in advance.  This will mess
    1653             :         // with trace output, but is otherwise harmless.
    1654         169 :         start_state = *rw;
    1655         169 :         need_size = !current->payload[k].payload_size;
    1656         338 :         for (i = 0; i < 1 + need_size; i++) {
    1657         169 :             *rw = start_state;
    1658             : 
    1659         169 :             tmp = current->payload[k].payload_type;
    1660         338 :             while (tmp >= 255) {
    1661           0 :                 fixed(8, ff_byte, 0xff);
    1662           0 :                 tmp -= 255;
    1663             :             }
    1664         169 :             xu(8, last_payload_type_byte, tmp, 0, 254, 0);
    1665             : 
    1666         169 :             tmp = current->payload[k].payload_size;
    1667         338 :             while (tmp >= 255) {
    1668           0 :                 fixed(8, ff_byte, 0xff);
    1669           0 :                 tmp -= 255;
    1670             :             }
    1671         169 :             xu(8, last_payload_size_byte, tmp, 0, 254, 0);
    1672             : 
    1673         169 :             CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
    1674             :         }
    1675             :     }
    1676             : #endif
    1677             : 
    1678         338 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
    1679             : 
    1680         338 :     return 0;
    1681             : }

Generated by: LCOV version 1.13