LCOV - code coverage report
Current view: top level - libavcodec - cbs_h264_syntax_template.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 419 675 62.1 %
Date: 2017-12-12 11:08:38 Functions: 30 44 68.2 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19        3372 : static int FUNC(rbsp_trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
      20             : {
      21             :     int err;
      22        3372 :     av_unused int one = 1, zero = 0;
      23        3372 :     xu(1, rbsp_stop_one_bit, one, 1, 1);
      24       20972 :     while (byte_alignment(rw) != 0)
      25       14228 :         xu(1, rbsp_alignment_zero_bit, zero, 0, 0);
      26             : 
      27        3372 :     return 0;
      28             : }
      29             : 
      30       13188 : static int FUNC(nal_unit_header)(CodedBitstreamContext *ctx, RWContext *rw,
      31             :                                  H264RawNALUnitHeader *current,
      32             :                                  uint32_t valid_type_mask)
      33             : {
      34             :     int err;
      35             : 
      36       13188 :     u(1, forbidden_zero_bit, 0, 0);
      37       13188 :     u(2, nal_ref_idc,        0, 3);
      38       13188 :     u(5, nal_unit_type,      0, 31);
      39             : 
      40       13188 :     if (!(1 << current->nal_unit_type & valid_type_mask)) {
      41           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid NAL unit type %d.\n",
      42           0 :                current->nal_unit_type);
      43           0 :         return AVERROR_INVALIDDATA;
      44             :     }
      45             : 
      46       26376 :     if (current->nal_unit_type == 14 ||
      47       26376 :         current->nal_unit_type == 20 ||
      48       13188 :         current->nal_unit_type == 21) {
      49           0 :         if (current->nal_unit_type != 21)
      50           0 :             flag(svc_extension_flag);
      51             :         else
      52           0 :             flag(avc_3d_extension_flag);
      53             : 
      54           0 :         if (current->svc_extension_flag) {
      55           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "SVC not supported.\n");
      56           0 :             return AVERROR_PATCHWELCOME;
      57             : 
      58           0 :         } else if (current->avc_3d_extension_flag) {
      59           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "3DAVC not supported.\n");
      60           0 :             return AVERROR_PATCHWELCOME;
      61             : 
      62             :         } else {
      63           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "MVC not supported.\n");
      64           0 :             return AVERROR_PATCHWELCOME;
      65             :         }
      66             :     }
      67             : 
      68       13188 :     return 0;
      69             : }
      70             : 
      71           0 : static int FUNC(scaling_list)(CodedBitstreamContext *ctx, RWContext *rw,
      72             :                               H264RawScalingList *current,
      73             :                               int size_of_scaling_list)
      74             : {
      75             :     int err, i, scale;
      76             : 
      77           0 :     scale = 8;
      78           0 :     for (i = 0; i < size_of_scaling_list; i++) {
      79           0 :         xse(delta_scale, current->delta_scale[i], -128, +127);
      80           0 :         scale = (scale + current->delta_scale[i] + 256) % 256;
      81           0 :         if (scale == 0)
      82           0 :             break;
      83             :     }
      84             : 
      85           0 :     return 0;
      86             : }
      87             : 
      88           8 : static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
      89             :                                 H264RawHRD *current)
      90             : {
      91             :     int err, i;
      92             : 
      93           8 :     ue(cpb_cnt_minus1, 0, 31);
      94           8 :     u(4, bit_rate_scale, 0, 15);
      95           8 :     u(4, cpb_size_scale, 0, 15);
      96             : 
      97          16 :     for (i = 0; i <= current->cpb_cnt_minus1; i++) {
      98           8 :         ue(bit_rate_value_minus1[i], 0, UINT32_MAX - 1);
      99           8 :         ue(cpb_size_value_minus1[i], 0, UINT32_MAX - 1);
     100           8 :         flag(cbr_flag[i]);
     101             :     }
     102             : 
     103           8 :     u(5, initial_cpb_removal_delay_length_minus1, 0, 31);
     104           8 :     u(5, cpb_removal_delay_length_minus1,         0, 31);
     105           8 :     u(5, dpb_output_delay_length_minus1,          0, 31);
     106           8 :     u(5, time_offset_length,                      0, 31);
     107             : 
     108           8 :     return 0;
     109             : }
     110             : 
     111           4 : static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
     112             :                                 H264RawVUI *current, H264RawSPS *sps)
     113             : {
     114             :     int err;
     115             : 
     116           4 :     flag(aspect_ratio_info_present_flag);
     117           4 :     if (current->aspect_ratio_info_present_flag) {
     118           0 :         u(8, aspect_ratio_idc, 0, 255);
     119           0 :         if (current->aspect_ratio_idc == 255) {
     120           0 :             u(16, sar_width,  0, 65535);
     121           0 :             u(16, sar_height, 0, 65535);
     122             :         }
     123             :     } else {
     124           4 :         infer(aspect_ratio_idc, 0);
     125             :     }
     126             : 
     127           4 :     flag(overscan_info_present_flag);
     128           4 :     if (current->overscan_info_present_flag)
     129           0 :         flag(overscan_appropriate_flag);
     130             : 
     131           4 :     flag(video_signal_type_present_flag);
     132           4 :     if (current->video_signal_type_present_flag) {
     133           0 :         u(3, video_format, 0, 7);
     134           0 :         flag(video_full_range_flag);
     135           0 :         flag(colour_description_present_flag);
     136           0 :         if (current->colour_description_present_flag) {
     137           0 :             u(8, colour_primaries,         0, 255);
     138           0 :             u(8, transfer_characteristics, 0, 255);
     139           0 :             u(8, matrix_coefficients,      0, 255);
     140             :         }
     141             :     } else {
     142           4 :         infer(video_format,             5);
     143           4 :         infer(video_full_range_flag,    0);
     144           4 :         infer(colour_primaries,         2);
     145           4 :         infer(transfer_characteristics, 2);
     146           4 :         infer(matrix_coefficients,      2);
     147             :     }
     148             : 
     149           4 :     flag(chroma_loc_info_present_flag);
     150           4 :     if (current->chroma_loc_info_present_flag) {
     151           0 :         ue(chroma_sample_loc_type_top_field,    0, 5);
     152           0 :         ue(chroma_sample_loc_type_bottom_field, 0, 5);
     153             :     } else {
     154           4 :         infer(chroma_sample_loc_type_top_field,    0);
     155           4 :         infer(chroma_sample_loc_type_bottom_field, 0);
     156             :     }
     157             : 
     158           4 :     flag(timing_info_present_flag);
     159           4 :     if (current->timing_info_present_flag) {
     160           4 :         u(32, num_units_in_tick, 1, UINT32_MAX);
     161           4 :         u(32, time_scale,        1, UINT32_MAX);
     162           4 :         flag(fixed_frame_rate_flag);
     163             :     } else {
     164           0 :         infer(fixed_frame_rate_flag, 0);
     165             :     }
     166             : 
     167           4 :     flag(nal_hrd_parameters_present_flag);
     168           4 :     if (current->nal_hrd_parameters_present_flag)
     169           4 :         CHECK(FUNC(hrd_parameters)(ctx, rw, &current->nal_hrd_parameters));
     170             : 
     171           4 :     flag(vcl_hrd_parameters_present_flag);
     172           4 :     if (current->vcl_hrd_parameters_present_flag)
     173           4 :         CHECK(FUNC(hrd_parameters)(ctx, rw, &current->vcl_hrd_parameters));
     174             : 
     175           4 :     if (current->nal_hrd_parameters_present_flag ||
     176           0 :         current->vcl_hrd_parameters_present_flag)
     177           4 :         flag(low_delay_hrd_flag);
     178             :     else
     179           0 :         infer(low_delay_hrd_flag, 1 - current->fixed_frame_rate_flag);
     180             : 
     181           4 :     flag(pic_struct_present_flag);
     182             : 
     183           4 :     flag(bitstream_restriction_flag);
     184           4 :     if (current->bitstream_restriction_flag) {
     185           4 :         flag(motion_vectors_over_pic_boundaries_flag);
     186           4 :         ue(max_bytes_per_pic_denom, 0, 16);
     187           4 :         ue(max_bits_per_mb_denom,   0, 16);
     188           4 :         ue(log2_max_mv_length_horizontal, 0, 16);
     189           4 :         ue(log2_max_mv_length_vertical,   0, 16);
     190           4 :         ue(max_num_reorder_frames,  0, H264_MAX_DPB_FRAMES);
     191           4 :         ue(max_dec_frame_buffering, 0, H264_MAX_DPB_FRAMES);
     192             :     } else {
     193           0 :         infer(motion_vectors_over_pic_boundaries_flag, 1);
     194           0 :         infer(max_bytes_per_pic_denom, 2);
     195           0 :         infer(max_bits_per_mb_denom,   1);
     196           0 :         infer(log2_max_mv_length_horizontal, 16);
     197           0 :         infer(log2_max_mv_length_vertical,   16);
     198             : 
     199           0 :         if ((sps->profile_idc ==  44 || sps->profile_idc ==  86 ||
     200           0 :              sps->profile_idc == 110 || sps->profile_idc == 110 ||
     201           0 :              sps->profile_idc == 122 || sps->profile_idc == 244) &&
     202           0 :             sps->constraint_set3_flag) {
     203           0 :             infer(max_num_reorder_frames,  0);
     204           0 :             infer(max_dec_frame_buffering, 0);
     205             :         } else {
     206           0 :             infer(max_num_reorder_frames,  H264_MAX_DPB_FRAMES);
     207           0 :             infer(max_dec_frame_buffering, H264_MAX_DPB_FRAMES);
     208             :         }
     209             :     }
     210             : 
     211           4 :     return 0;
     212             : }
     213             : 
     214          76 : static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
     215             :                      H264RawSPS *current)
     216             : {
     217             :     int err, i;
     218             : 
     219          76 :     HEADER("Sequence Parameter Set");
     220             : 
     221          76 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
     222             :                                 1 << H264_NAL_SPS));
     223             : 
     224          76 :     u(8, profile_idc, 0, 255);
     225             : 
     226          76 :     flag(constraint_set0_flag);
     227          76 :     flag(constraint_set1_flag);
     228          76 :     flag(constraint_set2_flag);
     229          76 :     flag(constraint_set3_flag);
     230          76 :     flag(constraint_set4_flag);
     231          76 :     flag(constraint_set5_flag);
     232             : 
     233          76 :     u(2, reserved_zero_2bits,  0, 0);
     234             : 
     235          76 :     u(8, level_idc, 0, 255);
     236             : 
     237          76 :     ue(seq_parameter_set_id, 0, 31);
     238             : 
     239         152 :     if (current->profile_idc == 100 || current->profile_idc == 110 ||
     240         228 :         current->profile_idc == 122 || current->profile_idc == 244 ||
     241         228 :         current->profile_idc ==  44 || current->profile_idc ==  83 ||
     242         228 :         current->profile_idc ==  86 || current->profile_idc == 118 ||
     243         152 :         current->profile_idc == 128 || current->profile_idc == 138) {
     244           0 :         ue(chroma_format_idc, 0, 3);
     245             : 
     246           0 :         if (current->chroma_format_idc == 3)
     247           0 :             flag(separate_colour_plane_flag);
     248             :         else
     249           0 :             infer(separate_colour_plane_flag, 0);
     250             : 
     251           0 :         ue(bit_depth_luma_minus8,   0, 6);
     252           0 :         ue(bit_depth_chroma_minus8, 0, 6);
     253             : 
     254           0 :         flag(qpprime_y_zero_transform_bypass_flag);
     255             : 
     256           0 :         flag(seq_scaling_matrix_present_flag);
     257           0 :         if (current->seq_scaling_matrix_present_flag) {
     258           0 :             for (i = 0; i < ((current->chroma_format_idc != 3) ? 8 : 12); i++) {
     259           0 :                 flag(seq_scaling_list_present_flag[i]);
     260           0 :                 if (current->seq_scaling_list_present_flag[i]) {
     261           0 :                     if (i < 6)
     262           0 :                         CHECK(FUNC(scaling_list)(ctx, rw,
     263             :                                                  &current->scaling_list_4x4[i],
     264             :                                                  16));
     265             :                     else
     266           0 :                         CHECK(FUNC(scaling_list)(ctx, rw,
     267             :                                                  &current->scaling_list_8x8[i - 6],
     268             :                                                  64));
     269             :                 }
     270             :             }
     271             :         }
     272             :     } else {
     273          76 :         infer(chroma_format_idc, current->profile_idc == 183 ? 0 : 1);
     274             : 
     275          76 :         infer(separate_colour_plane_flag, 0);
     276          76 :         infer(bit_depth_luma_minus8,      0);
     277          76 :         infer(bit_depth_chroma_minus8,    0);
     278             :     }
     279             : 
     280          76 :     ue(log2_max_frame_num_minus4, 0, 12);
     281          76 :     ue(pic_order_cnt_type, 0, 2);
     282             : 
     283          76 :     if (current->pic_order_cnt_type == 0) {
     284          42 :         ue(log2_max_pic_order_cnt_lsb_minus4, 0, 12);
     285          34 :     } else if (current->pic_order_cnt_type == 1) {
     286          16 :         flag(delta_pic_order_always_zero_flag);
     287          16 :         se(offset_for_non_ref_pic,         INT32_MIN + 1, INT32_MAX);
     288          16 :         se(offset_for_top_to_bottom_field, INT32_MIN + 1, INT32_MAX);
     289          16 :         ue(num_ref_frames_in_pic_order_cnt_cycle, 0, 255);
     290             : 
     291          32 :         for (i = 0; i < current->num_ref_frames_in_pic_order_cnt_cycle; i++)
     292          16 :             se(offset_for_ref_frame[i], INT32_MIN + 1, INT32_MAX);
     293             :     }
     294             : 
     295          76 :     ue(max_num_ref_frames, 0, H264_MAX_DPB_FRAMES);
     296          76 :     flag(gaps_in_frame_num_allowed_flag);
     297             : 
     298          76 :     ue(pic_width_in_mbs_minus1,        0, H264_MAX_MB_WIDTH);
     299          76 :     ue(pic_height_in_map_units_minus1, 0, H264_MAX_MB_HEIGHT);
     300             : 
     301          76 :     flag(frame_mbs_only_flag);
     302          76 :     if (!current->frame_mbs_only_flag)
     303          22 :         flag(mb_adaptive_frame_field_flag);
     304             : 
     305          76 :     flag(direct_8x8_inference_flag);
     306             : 
     307          76 :     flag(frame_cropping_flag);
     308          76 :     if (current->frame_cropping_flag) {
     309           4 :         ue(frame_crop_left_offset,   0, H264_MAX_WIDTH);
     310           4 :         ue(frame_crop_right_offset,  0, H264_MAX_WIDTH);
     311           4 :         ue(frame_crop_top_offset,    0, H264_MAX_HEIGHT);
     312           4 :         ue(frame_crop_bottom_offset, 0, H264_MAX_HEIGHT);
     313             :     }
     314             : 
     315          76 :     flag(vui_parameters_present_flag);
     316          76 :     if (current->vui_parameters_present_flag)
     317           4 :         CHECK(FUNC(vui_parameters)(ctx, rw, &current->vui, current));
     318             : 
     319          76 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
     320             : 
     321          76 :     return 0;
     322             : }
     323             : 
     324           0 : static int FUNC(sps_extension)(CodedBitstreamContext *ctx, RWContext *rw,
     325             :                                H264RawSPSExtension *current)
     326             : {
     327             :     int err;
     328             : 
     329           0 :     HEADER("Sequence Parameter Set Extension");
     330             : 
     331           0 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
     332             :                                 1 << H264_NAL_SPS_EXT));
     333             : 
     334           0 :     ue(seq_parameter_set_id, 0, 31);
     335             : 
     336           0 :     ue(aux_format_idc, 0, 3);
     337             : 
     338           0 :     if (current->aux_format_idc != 0) {
     339             :         int bits;
     340             : 
     341           0 :         ue(bit_depth_aux_minus8, 0, 4);
     342           0 :         flag(alpha_incr_flag);
     343             : 
     344           0 :         bits = current->bit_depth_aux_minus8 + 9;
     345           0 :         u(bits, alpha_opaque_value,      0, (1 << bits) - 1);
     346           0 :         u(bits, alpha_transparent_value, 0, (1 << bits) - 1);
     347             :     }
     348             : 
     349           0 :     flag(additional_extension_flag);
     350             : 
     351           0 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
     352             : 
     353           0 :     return 0;
     354             : }
     355             : 
     356        2360 : static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
     357             :                      H264RawPPS *current)
     358             : {
     359        2360 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
     360             :     const H264RawSPS *sps;
     361             :     int err, i;
     362             : 
     363        2360 :     HEADER("Picture Parameter Set");
     364             : 
     365        2360 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
     366             :                                 1 << H264_NAL_PPS));
     367             : 
     368        2360 :     ue(pic_parameter_set_id, 0, 255);
     369        2360 :     ue(seq_parameter_set_id, 0, 31);
     370             : 
     371        2360 :     sps = h264->sps[current->seq_parameter_set_id];
     372        2360 :     if (!sps) {
     373           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
     374           0 :                current->seq_parameter_set_id);
     375           0 :         return AVERROR_INVALIDDATA;
     376             :     }
     377             : 
     378        2360 :     flag(entropy_coding_mode_flag);
     379        2360 :     flag(bottom_field_pic_order_in_frame_present_flag);
     380             : 
     381        2360 :     ue(num_slice_groups_minus1, 0, 7);
     382        2360 :     if (current->num_slice_groups_minus1 > 0) {
     383             :         unsigned int pic_size;
     384             :         int iGroup;
     385             : 
     386          56 :         pic_size = (sps->pic_width_in_mbs_minus1 + 1) *
     387          28 :                    (sps->pic_height_in_map_units_minus1 + 1);
     388             : 
     389          28 :         ue(slice_group_map_type, 0, 6);
     390             : 
     391          28 :         if (current->slice_group_map_type == 0) {
     392          36 :             for (iGroup = 0; iGroup <= current->num_slice_groups_minus1; iGroup++)
     393          32 :                 ue(run_length_minus1[iGroup], 0, pic_size - 1);
     394             : 
     395          24 :         } else if (current->slice_group_map_type == 2) {
     396          12 :             for (iGroup = 0; iGroup < current->num_slice_groups_minus1; iGroup++) {
     397           8 :                 ue(top_left[iGroup],     0,                         pic_size - 1);
     398           8 :                 ue(bottom_right[iGroup], current->top_left[iGroup], pic_size - 1);
     399             :             }
     400          36 :         } else if (current->slice_group_map_type == 3 ||
     401          28 :                    current->slice_group_map_type == 4 ||
     402          12 :                    current->slice_group_map_type == 5) {
     403          12 :             flag(slice_group_change_direction_flag);
     404          12 :             ue(slice_group_change_rate_minus1, 0, pic_size - 1);
     405           8 :         } else if (current->slice_group_map_type == 6) {
     406           4 :             ue(pic_size_in_map_units_minus1, pic_size - 1, pic_size - 1);
     407         400 :             for (i = 0; i <= current->pic_size_in_map_units_minus1; i++)
     408         396 :                 u(av_log2(2 * current->num_slice_groups_minus1 + 1),
     409             :                   slice_group_id[i], 0, current->num_slice_groups_minus1);
     410             :         }
     411             :     }
     412             : 
     413        2360 :     ue(num_ref_idx_l0_default_active_minus1, 0, 31);
     414        2360 :     ue(num_ref_idx_l1_default_active_minus1, 0, 31);
     415             : 
     416        2360 :     flag(weighted_pred_flag);
     417        2360 :     u(2, weighted_bipred_idc, 0, 2);
     418             : 
     419        2360 :     se(pic_init_qp_minus26, -26 - 6 * sps->bit_depth_luma_minus8, +25);
     420        2360 :     se(pic_init_qs_minus26, -26, +25);
     421        2360 :     se(chroma_qp_index_offset, -12, +12);
     422             : 
     423        2360 :     flag(deblocking_filter_control_present_flag);
     424        2360 :     flag(constrained_intra_pred_flag);
     425        2360 :     flag(redundant_pic_cnt_present_flag);
     426             : 
     427        2360 :     if (more_rbsp_data(current->more_rbsp_data))
     428             :     {
     429           0 :         flag(transform_8x8_mode_flag);
     430             : 
     431           0 :         flag(pic_scaling_matrix_present_flag);
     432           0 :         if (current->pic_scaling_matrix_present_flag) {
     433           0 :             for (i = 0; i < 6 + (((sps->chroma_format_idc != 3) ? 2 : 6) *
     434           0 :                                  current->transform_8x8_mode_flag); i++) {
     435           0 :                 flag(pic_scaling_list_present_flag[i]);
     436           0 :                 if (current->pic_scaling_list_present_flag[i]) {
     437           0 :                     if (i < 6)
     438           0 :                         CHECK(FUNC(scaling_list)(ctx, rw,
     439             :                                                  &current->scaling_list_4x4[i],
     440             :                                                  16));
     441             :                     else
     442           0 :                         CHECK(FUNC(scaling_list)(ctx, rw,
     443             :                                                  &current->scaling_list_8x8[i - 6],
     444             :                                                  64));
     445             :                 }
     446             :             }
     447             :         }
     448             : 
     449           0 :         se(second_chroma_qp_index_offset, -12, +12);
     450             :     } else {
     451        2360 :         infer(transform_8x8_mode_flag, 0);
     452        2360 :         infer(pic_scaling_matrix_present_flag, 0);
     453        2360 :         infer(second_chroma_qp_index_offset, current->chroma_qp_index_offset);
     454             :     }
     455             : 
     456        2360 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
     457             : 
     458        2360 :     return 0;
     459             : }
     460             : 
     461           2 : static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
     462             :                                       H264RawSEIBufferingPeriod *current)
     463             : {
     464           2 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
     465             :     const H264RawSPS *sps;
     466             :     int err, i, length;
     467             : 
     468           2 :     ue(seq_parameter_set_id, 0, 31);
     469             : 
     470           2 :     sps = h264->sps[current->seq_parameter_set_id];
     471           2 :     if (!sps) {
     472           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
     473           0 :                current->seq_parameter_set_id);
     474           0 :         return AVERROR_INVALIDDATA;
     475             :     }
     476           2 :     h264->active_sps = sps;
     477             : 
     478           2 :     if (sps->vui.nal_hrd_parameters_present_flag) {
     479           4 :         for (i = 0; i <= sps->vui.nal_hrd_parameters.cpb_cnt_minus1; i++) {
     480           2 :             length = sps->vui.nal_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
     481           2 :             xu(length, initial_cpb_removal_delay[SchedSelIdx],
     482             :                current->nal.initial_cpb_removal_delay[i],
     483             :                0, (1 << (uint64_t)length) - 1);
     484           2 :             xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
     485             :                current->nal.initial_cpb_removal_delay_offset[i],
     486             :                0, (1 << (uint64_t)length) - 1);
     487             :         }
     488             :     }
     489             : 
     490           2 :     if (sps->vui.vcl_hrd_parameters_present_flag) {
     491           4 :         for (i = 0; i <= sps->vui.vcl_hrd_parameters.cpb_cnt_minus1; i++) {
     492           2 :             length = sps->vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
     493           2 :             xu(length, initial_cpb_removal_delay[SchedSelIdx],
     494             :                current->vcl.initial_cpb_removal_delay[i],
     495             :                0, (1 << (uint64_t)length) - 1);
     496           2 :             xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
     497             :                current->vcl.initial_cpb_removal_delay_offset[i],
     498             :                0, (1 << (uint64_t)length) - 1);
     499             :         }
     500             :     }
     501             : 
     502           2 :     return 0;
     503             : }
     504             : 
     505           0 : static int FUNC(sei_pic_timestamp)(CodedBitstreamContext *ctx, RWContext *rw,
     506             :                                    H264RawSEIPicTimestamp *current)
     507             : {
     508           0 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
     509             :     const H264RawSPS *sps;
     510             :     uint8_t time_offset_length;
     511             :     int err;
     512             : 
     513           0 :     u(2, ct_type, 0, 2);
     514           0 :     flag(nuit_field_based_flag);
     515           0 :     u(5, counting_type, 0, 6);
     516           0 :     flag(full_timestamp_flag);
     517           0 :     flag(discontinuity_flag);
     518           0 :     flag(cnt_dropped_flag);
     519           0 :     u(8, n_frames, 0, 255);
     520           0 :     if (current->full_timestamp_flag) {
     521           0 :             u(6, seconds_value, 0, 59);
     522           0 :             u(6, minutes_value, 0, 59);
     523           0 :             u(5, hours_value,   0, 23);
     524             :     } else {
     525           0 :         flag(seconds_flag);
     526           0 :         if (current->seconds_flag) {
     527           0 :             u(6, seconds_value, 0, 59);
     528           0 :             flag(minutes_flag);
     529           0 :             if (current->minutes_flag) {
     530           0 :                 u(6, minutes_value, 0, 59);
     531           0 :                 flag(hours_flag);
     532           0 :                 if (current->hours_flag)
     533           0 :                     u(5, hours_value, 0, 23);
     534             :             }
     535             :         }
     536             :     }
     537             : 
     538           0 :     sps = h264->active_sps;
     539           0 :     if (sps->vui.nal_hrd_parameters_present_flag)
     540           0 :         time_offset_length = sps->vui.nal_hrd_parameters.time_offset_length;
     541           0 :     else if (sps->vui.vcl_hrd_parameters_present_flag)
     542           0 :         time_offset_length = sps->vui.vcl_hrd_parameters.time_offset_length;
     543             :     else
     544           0 :         time_offset_length = 24;
     545             : 
     546           0 :     if (time_offset_length > 0)
     547           0 :         u(time_offset_length, time_offset,
     548             :           0, (1 << (uint64_t)time_offset_length) - 1);
     549             :     else
     550           0 :         infer(time_offset, 0);
     551             : 
     552           0 :     return 0;
     553             : }
     554             : 
     555         556 : static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
     556             :                                 H264RawSEIPicTiming *current)
     557             : {
     558         556 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
     559             :     const H264RawSPS *sps;
     560             :     int err;
     561             : 
     562         556 :     sps = h264->active_sps;
     563         556 :     if (!sps) {
     564           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR,
     565             :                "No active SPS for pic_timing.\n");
     566           0 :         return AVERROR_INVALIDDATA;
     567             :     }
     568             : 
     569         556 :     if (sps->vui.nal_hrd_parameters_present_flag ||
     570           0 :         sps->vui.vcl_hrd_parameters_present_flag) {
     571             :         const H264RawHRD *hrd;
     572             : 
     573         556 :         if (sps->vui.nal_hrd_parameters_present_flag)
     574         556 :             hrd = &sps->vui.nal_hrd_parameters;
     575           0 :         else if (sps->vui.vcl_hrd_parameters_present_flag)
     576           0 :             hrd = &sps->vui.vcl_hrd_parameters;
     577             :         else {
     578           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR,
     579             :                    "No HRD parameters for pic_timing.\n");
     580           0 :             return AVERROR_INVALIDDATA;
     581             :         }
     582             : 
     583         556 :         u(hrd->cpb_removal_delay_length_minus1 + 1, cpb_removal_delay,
     584             :           0, (1 << (uint64_t)hrd->cpb_removal_delay_length_minus1) + 1);
     585         556 :         u(hrd->dpb_output_delay_length_minus1 + 1, dpb_output_delay,
     586             :           0, (1 << (uint64_t)hrd->dpb_output_delay_length_minus1) + 1);
     587             :     }
     588             : 
     589         556 :     if (sps->vui.pic_struct_present_flag) {
     590             :         static const int num_clock_ts[9] = {
     591             :             1, 1, 1, 2, 2, 3, 3, 2, 3
     592             :         };
     593             :         int i;
     594             : 
     595         556 :         u(4, pic_struct, 0, 8);
     596         556 :         if (current->pic_struct > 8)
     597           0 :             return AVERROR_INVALIDDATA;
     598             : 
     599        1944 :         for (i = 0; i < num_clock_ts[current->pic_struct]; i++) {
     600        1388 :             flag(clock_timestamp_flag[i]);
     601        1388 :             if (current->clock_timestamp_flag[i])
     602           0 :                 CHECK(FUNC(sei_pic_timestamp)(ctx, rw, &current->timestamp[i]));
     603             :         }
     604             :     }
     605             : 
     606         556 :     return 0;
     607             : }
     608             : 
     609           0 : static int FUNC(sei_user_data_registered)(CodedBitstreamContext *ctx, RWContext *rw,
     610             :                                           H264RawSEIUserDataRegistered *current,
     611             :                                           uint32_t *payload_size)
     612             : {
     613             :     int err, i, j;
     614             : 
     615           0 :     u(8, itu_t_t35_country_code, 0x00, 0xff);
     616           0 :     if (current->itu_t_t35_country_code != 0xff)
     617           0 :         i = 1;
     618             :     else {
     619           0 :         u(8, itu_t_t35_country_code_extension_byte, 0x00, 0xff);
     620           0 :         i = 2;
     621             :     }
     622             : 
     623             : #ifdef READ
     624           0 :     if (*payload_size < i) {
     625           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR,
     626             :                "Invalid SEI user data registered payload.\n");
     627           0 :         return AVERROR_INVALIDDATA;
     628             :     }
     629           0 :     current->data_length = *payload_size - i;
     630             : #else
     631           0 :     *payload_size = i + current->data_length;
     632             : #endif
     633             : 
     634           0 :     allocate(current->data, current->data_length);
     635           0 :     for (j = 0; j < current->data_length; j++)
     636           0 :         xu(8, itu_t_t35_payload_byte, current->data[j], 0x00, 0xff);
     637             : 
     638           0 :     return 0;
     639             : }
     640             : 
     641           0 : static int FUNC(sei_user_data_unregistered)(CodedBitstreamContext *ctx, RWContext *rw,
     642             :                                             H264RawSEIUserDataUnregistered *current,
     643             :                                             uint32_t *payload_size)
     644             : {
     645             :     int err, i;
     646             : 
     647             : #ifdef READ
     648           0 :     if (*payload_size < 16) {
     649           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR,
     650             :                "Invalid SEI user data unregistered payload.\n");
     651           0 :         return AVERROR_INVALIDDATA;
     652             :     }
     653           0 :     current->data_length = *payload_size - 16;
     654             : #else
     655           0 :     *payload_size = 16 + current->data_length;
     656             : #endif
     657             : 
     658           0 :     for (i = 0; i < 16; i++) {
     659           0 :         xu(8, uuid_iso_iec_11578,
     660             :            current->uuid_iso_iec_11578[i], 0x00, 0xff);
     661             :     }
     662             : 
     663           0 :     allocate(current->data, current->data_length);
     664             : 
     665           0 :     for (i = 0; i < current->data_length; i++)
     666           0 :         xu(8, user_data_payload_byte, current->data[i], 0x00, 0xff);
     667             : 
     668           0 :     return 0;
     669             : }
     670             : 
     671           0 : static int FUNC(sei_recovery_point)(CodedBitstreamContext *ctx, RWContext *rw,
     672             :                                     H264RawSEIRecoveryPoint *current)
     673             : {
     674             :     int err;
     675             : 
     676           0 :     ue(recovery_frame_cnt, 0, 65535);
     677           0 :     flag(exact_match_flag);
     678           0 :     flag(broken_link_flag);
     679           0 :     u(2, changing_slice_group_idc, 0, 2);
     680             : 
     681           0 :     return 0;
     682             : }
     683             : 
     684           0 : static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *rw,
     685             :                                          H264RawSEIDisplayOrientation *current)
     686             : {
     687             :     int err;
     688             : 
     689           0 :     flag(display_orientation_cancel_flag);
     690           0 :     if (!current->display_orientation_cancel_flag) {
     691           0 :         flag(hor_flip);
     692           0 :         flag(ver_flip);
     693           0 :         u(16, anticlockwise_rotation, 0, 65535);
     694           0 :         ue(display_orientation_repetition_period, 0, 16384);
     695           0 :         flag(display_orientation_extension_flag);
     696             :     }
     697             : 
     698           0 :     return 0;
     699             : }
     700             : 
     701         558 : static int FUNC(sei_payload)(CodedBitstreamContext *ctx, RWContext *rw,
     702             :                              H264RawSEIPayload *current)
     703             : {
     704             :     int err, i;
     705             :     int start_position, end_position;
     706             : 
     707             : #ifdef READ
     708         279 :     start_position = get_bits_count(rw);
     709             : #else
     710         279 :     start_position = put_bits_count(rw);
     711             : #endif
     712             : 
     713         558 :     switch (current->payload_type) {
     714           2 :     case H264_SEI_TYPE_BUFFERING_PERIOD:
     715           2 :         CHECK(FUNC(sei_buffering_period)
     716             :               (ctx, rw, &current->payload.buffering_period));
     717           2 :         break;
     718         556 :     case H264_SEI_TYPE_PIC_TIMING:
     719         556 :         CHECK(FUNC(sei_pic_timing)
     720             :               (ctx, rw, &current->payload.pic_timing));
     721         556 :         break;
     722           0 :     case H264_SEI_TYPE_FILLER_PAYLOAD:
     723             :         {
     724           0 :             av_unused int ff_byte = 0xff;
     725           0 :             for (i = 0; i  < current->payload_size; i++)
     726           0 :                 xu(8, ff_byte, ff_byte, 0xff, 0xff);
     727             :         }
     728           0 :         break;
     729           0 :     case H264_SEI_TYPE_USER_DATA_REGISTERED:
     730           0 :         CHECK(FUNC(sei_user_data_registered)
     731             :               (ctx, rw, &current->payload.user_data_registered, &current->payload_size));
     732           0 :         break;
     733           0 :     case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
     734           0 :         CHECK(FUNC(sei_user_data_unregistered)
     735             :               (ctx, rw, &current->payload.user_data_unregistered, &current->payload_size));
     736           0 :         break;
     737           0 :     case H264_SEI_TYPE_RECOVERY_POINT:
     738           0 :         CHECK(FUNC(sei_recovery_point)
     739             :               (ctx, rw, &current->payload.recovery_point));
     740           0 :         break;
     741           0 :     case H264_SEI_TYPE_DISPLAY_ORIENTATION:
     742           0 :         CHECK(FUNC(sei_display_orientation)
     743             :               (ctx, rw, &current->payload.display_orientation));
     744           0 :         break;
     745           0 :     default:
     746             :         {
     747           0 :             allocate(current->payload.other.data, current->payload_size);
     748           0 :             for (i = 0; i < current->payload_size; i++)
     749           0 :                 xu(8, payload_byte, current->payload.other.data[i], 0, 255);
     750             :         }
     751             :     }
     752             : 
     753         558 :     if (byte_alignment(rw)) {
     754         558 :         av_unused int one = 1, zero = 0;
     755         558 :         xu(1, bit_equal_to_one, one, 1, 1);
     756        1408 :         while (byte_alignment(rw))
     757         292 :             xu(1, bit_equal_to_zero, zero, 0, 0);
     758             :     }
     759             : 
     760             : #ifdef READ
     761         279 :     end_position = get_bits_count(rw);
     762         279 :     if (end_position < start_position + 8 * current->payload_size) {
     763           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Incorrect SEI payload length: "
     764             :                "header %"PRIu32" bits, actually %d bits.\n",
     765           0 :                8 * current->payload_size,
     766             :                end_position - start_position);
     767           0 :         return AVERROR_INVALIDDATA;
     768             :     }
     769             : #else
     770         279 :     end_position = put_bits_count(rw);
     771         279 :     current->payload_size = (end_position - start_position) / 8;
     772             : #endif
     773             : 
     774         558 :     return 0;
     775             : }
     776             : 
     777         556 : static int FUNC(sei)(CodedBitstreamContext *ctx, RWContext *rw,
     778             :                      H264RawSEI *current)
     779             : {
     780             :     int err, k;
     781             : 
     782         556 :     HEADER("Supplemental Enhancement Information");
     783             : 
     784         556 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
     785             :                                 1 << H264_NAL_SEI));
     786             : 
     787             : #ifdef READ
     788         279 :     for (k = 0; k < H264_MAX_SEI_PAYLOADS; k++) {
     789         279 :         uint32_t payload_type = 0;
     790         279 :         uint32_t payload_size = 0;
     791             :         uint32_t tmp;
     792             : 
     793         558 :         while (show_bits(rw, 8) == 0xff) {
     794           0 :             xu(8, ff_byte, tmp, 0xff, 0xff);
     795           0 :             payload_type += 255;
     796             :         }
     797         279 :         xu(8, last_payload_type_byte, tmp, 0, 254);
     798         279 :         payload_type += tmp;
     799             : 
     800         558 :         while (show_bits(rw, 8) == 0xff) {
     801           0 :             xu(8, ff_byte, tmp, 0xff, 0xff);
     802           0 :             payload_size += 255;
     803             :         }
     804         279 :         xu(8, last_payload_size_byte, tmp, 0, 254);
     805         279 :         payload_size += tmp;
     806             : 
     807         279 :         current->payload[k].payload_type = payload_type;
     808         279 :         current->payload[k].payload_size = payload_size;
     809             : 
     810         279 :         CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
     811             : 
     812         279 :         if (!cbs_h2645_read_more_rbsp_data(rw))
     813         278 :             break;
     814             :     }
     815         278 :     if (k >= H264_MAX_SEI_PAYLOADS) {
     816           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
     817             :                "SEI message: found %d.\n", k);
     818           0 :         return AVERROR_INVALIDDATA;
     819             :     }
     820         278 :     current->payload_count = k + 1;
     821             : #else
     822        1114 :     for (k = 0; k < current->payload_count; k++) {
     823             :         PutBitContext start_state;
     824             :         uint32_t tmp;
     825             :         int need_size, i;
     826             : 
     827             :         // Somewhat clumsy: we write the payload twice when
     828             :         // we don't know the size in advance.  This will mess
     829             :         // with trace output, but is otherwise harmless.
     830         279 :         start_state = *rw;
     831         279 :         need_size = !current->payload[k].payload_size;
     832         558 :         for (i = 0; i < 1 + need_size; i++) {
     833         279 :             *rw = start_state;
     834             : 
     835         279 :             tmp = current->payload[k].payload_type;
     836         558 :             while (tmp >= 255) {
     837           0 :                 xu(8, ff_byte, 0xff, 0xff, 0xff);
     838           0 :                 tmp -= 255;
     839             :             }
     840         279 :             xu(8, last_payload_type_byte, tmp, 0, 254);
     841             : 
     842         279 :             tmp = current->payload[k].payload_size;
     843         558 :             while (tmp >= 255) {
     844           0 :                 xu(8, ff_byte, 0xff, 0xff, 0xff);
     845           0 :                 tmp -= 255;
     846             :             }
     847         279 :             xu(8, last_payload_size_byte, tmp, 0, 254);
     848             : 
     849         279 :             CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
     850             :         }
     851             :     }
     852             : #endif
     853             : 
     854         556 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
     855             : 
     856         556 :     return 0;
     857             : }
     858             : 
     859         380 : static int FUNC(aud)(CodedBitstreamContext *ctx, RWContext *rw,
     860             :                      H264RawAUD *current)
     861             : {
     862             :     int err;
     863             : 
     864         380 :     HEADER("Access Unit Delimiter");
     865             : 
     866         380 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
     867             :                                 1 << H264_NAL_AUD));
     868             : 
     869         380 :     u(3, primary_pic_type, 0, 7);
     870             : 
     871         380 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
     872             : 
     873         380 :     return 0;
     874             : }
     875             : 
     876        9816 : static int FUNC(ref_pic_list_modification)(CodedBitstreamContext *ctx, RWContext *rw,
     877             :                                            H264RawSliceHeader *current)
     878             : {
     879        9816 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
     880        9816 :     const H264RawSPS *sps = h264->active_sps;
     881             :     int err, i, mopn;
     882             : 
     883       19072 :     if (current->slice_type % 5 != 2 &&
     884        9256 :         current->slice_type % 5 != 4) {
     885        9256 :         flag(ref_pic_list_modification_flag_l0);
     886        9256 :         if (current->ref_pic_list_modification_flag_l0) {
     887        3034 :             for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
     888        3034 :                 xue(modification_of_pic_nums_idc,
     889             :                     current->rplm_l0[i].modification_of_pic_nums_idc, 0, 3);
     890             : 
     891        3034 :                 mopn = current->rplm_l0[i].modification_of_pic_nums_idc;
     892        3034 :                 if (mopn == 3)
     893         972 :                     break;
     894             : 
     895        2062 :                 if (mopn == 0 || mopn == 1)
     896        2048 :                     xue(abs_diff_pic_num_minus1,
     897             :                         current->rplm_l0[i].abs_diff_pic_num_minus1,
     898             :                         0, (1 + current->field_pic_flag) *
     899             :                         (1 << (sps->log2_max_frame_num_minus4 + 4)));
     900          14 :                 else if (mopn == 2)
     901          14 :                     xue(long_term_pic_num,
     902             :                         current->rplm_l0[i].long_term_pic_num,
     903             :                         0, sps->max_num_ref_frames - 1);
     904             :             }
     905             :         }
     906             :     }
     907             : 
     908        9816 :     if (current->slice_type % 5 == 1) {
     909        2132 :         flag(ref_pic_list_modification_flag_l1);
     910        2132 :         if (current->ref_pic_list_modification_flag_l1) {
     911           0 :             for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
     912           0 :                 xue(modification_of_pic_nums_idc,
     913             :                     current->rplm_l1[i].modification_of_pic_nums_idc, 0, 3);
     914             : 
     915           0 :                 mopn = current->rplm_l1[i].modification_of_pic_nums_idc;
     916           0 :                 if (mopn == 3)
     917           0 :                     break;
     918             : 
     919           0 :                 if (mopn == 0 || mopn == 1)
     920           0 :                     xue(abs_diff_pic_num_minus1,
     921             :                         current->rplm_l1[i].abs_diff_pic_num_minus1,
     922             :                         0, (1 + current->field_pic_flag) *
     923             :                         (1 << (sps->log2_max_frame_num_minus4 + 4)));
     924           0 :                 else if (mopn == 2)
     925           0 :                     xue(long_term_pic_num,
     926             :                         current->rplm_l1[i].long_term_pic_num,
     927             :                         0, sps->max_num_ref_frames - 1);
     928             :             }
     929             :         }
     930             :     }
     931             : 
     932        9816 :     return 0;
     933             : }
     934             : 
     935         168 : static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
     936             :                                    H264RawSliceHeader *current)
     937             : {
     938         168 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
     939         168 :     const H264RawSPS *sps = h264->active_sps;
     940             :     int chroma;
     941             :     int err, i, j;
     942             : 
     943         168 :     ue(luma_log2_weight_denom, 0, 7);
     944             : 
     945         168 :     chroma = !sps->separate_colour_plane_flag && sps->chroma_format_idc != 0;
     946         168 :     if (chroma)
     947         168 :         ue(chroma_log2_weight_denom, 0, 7);
     948             : 
     949         988 :     for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
     950         820 :         flag(luma_weight_l0_flag[i]);
     951         820 :         if (current->luma_weight_l0_flag[i]) {
     952         436 :             se(luma_weight_l0[i], -128, +127);
     953         436 :             se(luma_offset_l0[i], -128, +127);
     954             :         }
     955         820 :         if (chroma) {
     956         820 :             flag(chroma_weight_l0_flag[i]);
     957         820 :             if (current->chroma_weight_l0_flag[i]) {
     958        1248 :                 for (j = 0; j < 2; j++) {
     959         832 :                     se(chroma_weight_l0[i][j], -128, +127);
     960         832 :                     se(chroma_offset_l0[i][j], -128, +127);
     961             :                 }
     962             :             }
     963             :         }
     964             :     }
     965             : 
     966         168 :     if (current->slice_type % 5 == 1) {
     967           0 :         for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
     968           0 :             flag(luma_weight_l1_flag[i]);
     969           0 :             if (current->luma_weight_l1_flag[i]) {
     970           0 :                 se(luma_weight_l1[i], -128, +127);
     971           0 :                 se(luma_offset_l1[i], -128, +127);
     972             :             }
     973           0 :             if (chroma) {
     974           0 :                 flag(chroma_weight_l1_flag[i]);
     975           0 :                 if (current->chroma_weight_l1_flag[i]) {
     976           0 :                     for (j = 0; j < 2; j++) {
     977           0 :                         se(chroma_weight_l1[i][j], -128, +127);
     978           0 :                         se(chroma_offset_l1[i][j], -128, +127);
     979             :                     }
     980             :                 }
     981             :             }
     982             :         }
     983             :     }
     984             : 
     985         168 :     return 0;
     986             : }
     987             : 
     988        7680 : static int FUNC(dec_ref_pic_marking)(CodedBitstreamContext *ctx, RWContext *rw,
     989             :                                      H264RawSliceHeader *current, int idr_pic_flag)
     990             : {
     991        7680 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
     992        7680 :     const H264RawSPS *sps = h264->active_sps;
     993             :     int err, i;
     994             :     uint32_t mmco;
     995             : 
     996        7680 :     if (idr_pic_flag) {
     997         114 :         flag(no_output_of_prior_pics_flag);
     998         114 :         flag(long_term_reference_flag);
     999             :     } else {
    1000        7566 :         flag(adaptive_ref_pic_marking_mode_flag);
    1001        7566 :         if (current->adaptive_ref_pic_marking_mode_flag) {
    1002         826 :             for (i = 0; i < H264_MAX_MMCO_COUNT; i++) {
    1003         826 :                 xue(memory_management_control_operation,
    1004             :                     current->mmco[i].memory_management_control_operation,
    1005             :                     0, 6);
    1006             : 
    1007         826 :                 mmco = current->mmco[i].memory_management_control_operation;
    1008         826 :                 if (mmco == 0)
    1009         412 :                     break;
    1010             : 
    1011         414 :                 if (mmco == 1 || mmco == 3)
    1012         332 :                     xue(difference_of_pic_nums_minus1,
    1013             :                         current->mmco[i].difference_of_pic_nums_minus1,
    1014             :                         0, INT32_MAX);
    1015         414 :                 if (mmco == 2)
    1016           0 :                     xue(long_term_pic_num,
    1017             :                         current->mmco[i].long_term_pic_num,
    1018             :                         0, sps->max_num_ref_frames - 1);
    1019         414 :                 if (mmco == 3 || mmco == 6)
    1020          56 :                     xue(long_term_frame_idx,
    1021             :                         current->mmco[i].long_term_frame_idx,
    1022             :                         0, sps->max_num_ref_frames - 1);
    1023         414 :                 if (mmco == 4)
    1024           4 :                     xue(max_long_term_frame_idx_plus1,
    1025             :                         current->mmco[i].max_long_term_frame_idx_plus1,
    1026             :                         0, sps->max_num_ref_frames);
    1027             :             }
    1028         412 :             if (i == H264_MAX_MMCO_COUNT) {
    1029           0 :                 av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many "
    1030             :                        "memory management control operations.\n");
    1031           0 :                 return AVERROR_INVALIDDATA;
    1032             :             }
    1033             :         }
    1034             :     }
    1035             : 
    1036        7680 :     return 0;
    1037             : }
    1038             : 
    1039        9816 : static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw,
    1040             :                               H264RawSliceHeader *current)
    1041             : {
    1042        9816 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
    1043             :     const H264RawSPS *sps;
    1044             :     const H264RawPPS *pps;
    1045             :     int err;
    1046             :     int idr_pic_flag;
    1047             :     int slice_type_i, slice_type_p, slice_type_b;
    1048             :     int slice_type_si, slice_type_sp;
    1049             : 
    1050        9816 :     HEADER("Slice Header");
    1051             : 
    1052        9816 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
    1053             :                                 1 << H264_NAL_SLICE     |
    1054             :                                 1 << H264_NAL_IDR_SLICE |
    1055             :                                 1 << H264_NAL_AUXILIARY_SLICE));
    1056             : 
    1057        9816 :     if (current->nal_unit_header.nal_unit_type == H264_NAL_AUXILIARY_SLICE) {
    1058           0 :         if (!h264->last_slice_nal_unit_type) {
    1059           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Auxiliary slice "
    1060             :                    "is not decodable without the main picture "
    1061             :                    "in the same access unit.\n");
    1062           0 :             return AVERROR_INVALIDDATA;
    1063             :         }
    1064             :     } else {
    1065        9816 :         h264->last_slice_nal_unit_type =
    1066        9816 :             current->nal_unit_header.nal_unit_type;
    1067             :     }
    1068        9816 :     idr_pic_flag = h264->last_slice_nal_unit_type == H264_NAL_IDR_SLICE;
    1069             : 
    1070        9816 :     ue(first_mb_in_slice, 0, H264_MAX_MB_PIC_SIZE - 1);
    1071        9816 :     ue(slice_type, 0, 9);
    1072             : 
    1073        9816 :     slice_type_i  = current->slice_type % 5 == 2;
    1074        9816 :     slice_type_p  = current->slice_type % 5 == 0;
    1075        9816 :     slice_type_b  = current->slice_type % 5 == 1;
    1076        9816 :     slice_type_si = current->slice_type % 5 == 4;
    1077        9816 :     slice_type_sp = current->slice_type % 5 == 3;
    1078             : 
    1079        9816 :     if (idr_pic_flag && !(slice_type_i || slice_type_si)) {
    1080           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid slice type %d "
    1081           0 :                "for IDR picture.\n", current->slice_type);
    1082           0 :         return AVERROR_INVALIDDATA;
    1083             :     }
    1084             : 
    1085        9816 :     ue(pic_parameter_set_id, 0, 255);
    1086             : 
    1087        9816 :     pps = h264->pps[current->pic_parameter_set_id];
    1088        9816 :     if (!pps) {
    1089           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
    1090           0 :                current->pic_parameter_set_id);
    1091           0 :         return AVERROR_INVALIDDATA;
    1092             :     }
    1093        9816 :     h264->active_pps = pps;
    1094             : 
    1095        9816 :     sps = h264->sps[pps->seq_parameter_set_id];
    1096        9816 :     if (!sps) {
    1097           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
    1098           0 :                pps->seq_parameter_set_id);
    1099           0 :         return AVERROR_INVALIDDATA;
    1100             :     }
    1101        9816 :     h264->active_sps = sps;
    1102             : 
    1103        9816 :     if (sps->separate_colour_plane_flag)
    1104           0 :         u(2, colour_plane_id, 0, 2);
    1105             : 
    1106        9816 :     u(sps->log2_max_frame_num_minus4 + 4, frame_num,
    1107             :       0, (1 << (sps->log2_max_frame_num_minus4 + 4)) - 1);
    1108             : 
    1109        9816 :     if (!sps->frame_mbs_only_flag) {
    1110         702 :         flag(field_pic_flag);
    1111         702 :         if (current->field_pic_flag)
    1112         508 :             flag(bottom_field_flag);
    1113             :         else
    1114         194 :             infer(bottom_field_flag, 0);
    1115             :     } else {
    1116        9114 :         infer(field_pic_flag,    0);
    1117        9114 :         infer(bottom_field_flag, 0);
    1118             :     }
    1119             : 
    1120        9816 :     if (idr_pic_flag)
    1121         114 :         ue(idr_pic_id, 0, 65535);
    1122             : 
    1123        9816 :     if (sps->pic_order_cnt_type == 0) {
    1124        4972 :         u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, pic_order_cnt_lsb,
    1125             :           0, (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1);
    1126        5152 :         if (pps->bottom_field_pic_order_in_frame_present_flag &&
    1127         180 :             !current->field_pic_flag)
    1128         180 :             se(delta_pic_order_cnt_bottom, INT32_MIN + 1, INT32_MAX);
    1129             : 
    1130        4844 :     } else if (sps->pic_order_cnt_type == 1) {
    1131        4562 :         if (!sps->delta_pic_order_always_zero_flag) {
    1132         846 :             se(delta_pic_order_cnt[0], INT32_MIN + 1, INT32_MAX);
    1133         846 :             if (pps->bottom_field_pic_order_in_frame_present_flag &&
    1134           0 :                 !current->field_pic_flag)
    1135           0 :                 se(delta_pic_order_cnt[1], INT32_MIN + 1, INT32_MAX);
    1136             :             else
    1137         846 :                 infer(delta_pic_order_cnt[1], 0);
    1138             :         } else {
    1139        3716 :             infer(delta_pic_order_cnt[0], 0);
    1140        3716 :             infer(delta_pic_order_cnt[1], 0);
    1141             :         }
    1142             :     }
    1143             : 
    1144        9816 :     if (pps->redundant_pic_cnt_present_flag)
    1145           0 :         ue(redundant_pic_cnt, 0, 127);
    1146             : 
    1147        9816 :     if (slice_type_b)
    1148        2132 :         flag(direct_spatial_mv_pred_flag);
    1149             : 
    1150        9816 :     if (slice_type_p || slice_type_sp || slice_type_b) {
    1151        9256 :         flag(num_ref_idx_active_override_flag);
    1152        9256 :         if (current->num_ref_idx_active_override_flag) {
    1153        4360 :             ue(num_ref_idx_l0_active_minus1, 0, 31);
    1154        4360 :             if (slice_type_b)
    1155        2016 :                 ue(num_ref_idx_l1_active_minus1, 0, 31);
    1156             :         } else {
    1157        4896 :             infer(num_ref_idx_l0_active_minus1,
    1158             :                   pps->num_ref_idx_l0_default_active_minus1);
    1159        4896 :             infer(num_ref_idx_l1_active_minus1,
    1160             :                   pps->num_ref_idx_l1_default_active_minus1);
    1161             :         }
    1162             :     }
    1163             : 
    1164       19632 :     if (current->nal_unit_header.nal_unit_type == 20 ||
    1165        9816 :         current->nal_unit_header.nal_unit_type == 21) {
    1166           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "MVC / 3DAVC not supported.\n");
    1167           0 :         return AVERROR_PATCHWELCOME;
    1168             :     } else {
    1169        9816 :         CHECK(FUNC(ref_pic_list_modification)(ctx, rw, current));
    1170             :     }
    1171             : 
    1172       19464 :     if ((pps->weighted_pred_flag && (slice_type_p || slice_type_sp)) ||
    1173        9648 :         (pps->weighted_bipred_idc == 1 && slice_type_b)) {
    1174         168 :         CHECK(FUNC(pred_weight_table)(ctx, rw, current));
    1175             :     }
    1176             : 
    1177        9816 :     if (current->nal_unit_header.nal_ref_idc != 0) {
    1178        7680 :         CHECK(FUNC(dec_ref_pic_marking)(ctx, rw, current, idr_pic_flag));
    1179             :     }
    1180             : 
    1181        9816 :     if (pps->entropy_coding_mode_flag &&
    1182        2240 :         !slice_type_i && !slice_type_si) {
    1183        2240 :         ue(cabac_init_idc, 0, 2);
    1184             :     }
    1185             : 
    1186        9816 :     se(slice_qp_delta, - 51 - 6 * sps->bit_depth_luma_minus8,
    1187             :                        + 51 + 6 * sps->bit_depth_luma_minus8);
    1188        9816 :     if (slice_type_sp || slice_type_si) {
    1189          78 :         if (slice_type_sp)
    1190          78 :             flag(sp_for_switch_flag);
    1191          78 :         se(slice_qs_delta, -51, +51);
    1192             :     }
    1193             : 
    1194        9816 :     if (pps->deblocking_filter_control_present_flag) {
    1195        5818 :         ue(disable_deblocking_filter_idc, 0, 2);
    1196        5818 :         if (current->disable_deblocking_filter_idc != 1) {
    1197        4116 :             se(slice_alpha_c0_offset_div2, -6, +6);
    1198        4116 :             se(slice_beta_offset_div2,     -6, +6);
    1199             :         } else {
    1200        1702 :             infer(slice_alpha_c0_offset_div2, 0);
    1201        1702 :             infer(slice_beta_offset_div2,     0);
    1202             :         }
    1203             :     } else {
    1204        3998 :         infer(disable_deblocking_filter_idc, 0);
    1205        3998 :         infer(slice_alpha_c0_offset_div2,    0);
    1206        3998 :         infer(slice_beta_offset_div2,        0);
    1207             :     }
    1208             : 
    1209       13054 :     if (pps->num_slice_groups_minus1 > 0 &&
    1210        4566 :         pps->slice_group_map_type >= 3 &&
    1211        1328 :         pps->slice_group_map_type <= 5) {
    1212             :         unsigned int pic_size, max, bits;
    1213             : 
    1214        1256 :         pic_size = (sps->pic_width_in_mbs_minus1 + 1) *
    1215         628 :                    (sps->pic_height_in_map_units_minus1 + 1);
    1216        1256 :         max = (pic_size + pps->slice_group_change_rate_minus1) /
    1217         628 :               (pps->slice_group_change_rate_minus1 + 1);
    1218         628 :         bits = av_log2(2 * max - 1);
    1219             : 
    1220         628 :         u(bits, slice_group_change_cycle, 0, max);
    1221             :     }
    1222             : 
    1223        9816 :     if (pps->entropy_coding_mode_flag) {
    1224        2400 :         av_unused int one = 1;
    1225       12532 :         while (byte_alignment(rw))
    1226        7732 :             xu(1, cabac_alignment_one_bit, one, 1, 1);
    1227             :     }
    1228             : 
    1229        9816 :     return 0;
    1230             : }

Generated by: LCOV version 1.13