LCOV - code coverage report
Current view: top level - libavcodec - cbs_h264_syntax_template.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 493 720 68.5 %
Date: 2018-05-20 11:54:08 Functions: 40 50 80.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        3400 : static int FUNC(rbsp_trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
      20             : {
      21             :     int err;
      22             : 
      23        3400 :     fixed(1, rbsp_stop_one_bit, 1);
      24       21170 :     while (byte_alignment(rw) != 0)
      25       14370 :         fixed(1, rbsp_alignment_zero_bit, 0);
      26             : 
      27        3400 :     return 0;
      28             : }
      29             : 
      30       13226 : 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       13226 :     u(1, forbidden_zero_bit, 0, 0);
      37       13226 :     u(2, nal_ref_idc,        0, 3);
      38       13226 :     u(5, nal_unit_type,      0, 31);
      39             : 
      40       13226 :     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       26452 :     if (current->nal_unit_type == 14 ||
      47       26452 :         current->nal_unit_type == 20 ||
      48       13226 :         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       13226 :     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 :         ses(delta_scale[i], -128, +127, 1, i);
      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          12 : static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
      89             :                                 H264RawHRD *current)
      90             : {
      91             :     int err, i;
      92             : 
      93          12 :     ue(cpb_cnt_minus1, 0, 31);
      94          12 :     u(4, bit_rate_scale, 0, 15);
      95          12 :     u(4, cpb_size_scale, 0, 15);
      96             : 
      97          24 :     for (i = 0; i <= current->cpb_cnt_minus1; i++) {
      98          12 :         ues(bit_rate_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
      99          12 :         ues(cpb_size_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
     100          12 :         flags(cbr_flag[i], 1, i);
     101             :     }
     102             : 
     103          12 :     u(5, initial_cpb_removal_delay_length_minus1, 0, 31);
     104          12 :     u(5, cpb_removal_delay_length_minus1,         0, 31);
     105          12 :     u(5, dpb_output_delay_length_minus1,          0, 31);
     106          12 :     u(5, time_offset_length,                      0, 31);
     107             : 
     108          12 :     return 0;
     109             : }
     110             : 
     111           8 : static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
     112             :                                 H264RawVUI *current, H264RawSPS *sps)
     113             : {
     114             :     int err;
     115             : 
     116           8 :     flag(aspect_ratio_info_present_flag);
     117           8 :     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           8 :         infer(aspect_ratio_idc, 0);
     125             :     }
     126             : 
     127           8 :     flag(overscan_info_present_flag);
     128           8 :     if (current->overscan_info_present_flag)
     129           0 :         flag(overscan_appropriate_flag);
     130             : 
     131           8 :     flag(video_signal_type_present_flag);
     132           8 :     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           8 :         infer(video_format,             5);
     143           8 :         infer(video_full_range_flag,    0);
     144           8 :         infer(colour_primaries,         2);
     145           8 :         infer(transfer_characteristics, 2);
     146           8 :         infer(matrix_coefficients,      2);
     147             :     }
     148             : 
     149           8 :     flag(chroma_loc_info_present_flag);
     150           8 :     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           8 :         infer(chroma_sample_loc_type_top_field,    0);
     155           8 :         infer(chroma_sample_loc_type_bottom_field, 0);
     156             :     }
     157             : 
     158           8 :     flag(timing_info_present_flag);
     159           8 :     if (current->timing_info_present_flag) {
     160           8 :         u(32, num_units_in_tick, 1, UINT32_MAX);
     161           8 :         u(32, time_scale,        1, UINT32_MAX);
     162           8 :         flag(fixed_frame_rate_flag);
     163             :     } else {
     164           0 :         infer(fixed_frame_rate_flag, 0);
     165             :     }
     166             : 
     167           8 :     flag(nal_hrd_parameters_present_flag);
     168           8 :     if (current->nal_hrd_parameters_present_flag)
     169           8 :         CHECK(FUNC(hrd_parameters)(ctx, rw, &current->nal_hrd_parameters));
     170             : 
     171           8 :     flag(vcl_hrd_parameters_present_flag);
     172           8 :     if (current->vcl_hrd_parameters_present_flag)
     173           4 :         CHECK(FUNC(hrd_parameters)(ctx, rw, &current->vcl_hrd_parameters));
     174             : 
     175           8 :     if (current->nal_hrd_parameters_present_flag ||
     176           0 :         current->vcl_hrd_parameters_present_flag)
     177           8 :         flag(low_delay_hrd_flag);
     178             :     else
     179           0 :         infer(low_delay_hrd_flag, 1 - current->fixed_frame_rate_flag);
     180             : 
     181           8 :     flag(pic_struct_present_flag);
     182             : 
     183           8 :     flag(bitstream_restriction_flag);
     184           8 :     if (current->bitstream_restriction_flag) {
     185           8 :         flag(motion_vectors_over_pic_boundaries_flag);
     186           8 :         ue(max_bytes_per_pic_denom, 0, 16);
     187           8 :         ue(max_bits_per_mb_denom,   0, 16);
     188           8 :         ue(log2_max_mv_length_horizontal, 0, 16);
     189           8 :         ue(log2_max_mv_length_vertical,   0, 16);
     190           8 :         ue(max_num_reorder_frames,  0, H264_MAX_DPB_FRAMES);
     191           8 :         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           8 :     return 0;
     212             : }
     213             : 
     214          80 : static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
     215             :                      H264RawSPS *current)
     216             : {
     217             :     int err, i;
     218             : 
     219          80 :     HEADER("Sequence Parameter Set");
     220             : 
     221          80 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
     222             :                                 1 << H264_NAL_SPS));
     223             : 
     224          80 :     u(8, profile_idc, 0, 255);
     225             : 
     226          80 :     flag(constraint_set0_flag);
     227          80 :     flag(constraint_set1_flag);
     228          80 :     flag(constraint_set2_flag);
     229          80 :     flag(constraint_set3_flag);
     230          80 :     flag(constraint_set4_flag);
     231          80 :     flag(constraint_set5_flag);
     232             : 
     233          80 :     u(2, reserved_zero_2bits,  0, 0);
     234             : 
     235          80 :     u(8, level_idc, 0, 255);
     236             : 
     237          80 :     ue(seq_parameter_set_id, 0, 31);
     238             : 
     239         160 :     if (current->profile_idc == 100 || current->profile_idc == 110 ||
     240         240 :         current->profile_idc == 122 || current->profile_idc == 244 ||
     241         240 :         current->profile_idc ==  44 || current->profile_idc ==  83 ||
     242         240 :         current->profile_idc ==  86 || current->profile_idc == 118 ||
     243         160 :         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 :                 flags(seq_scaling_list_present_flag[i], 1, 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          80 :         infer(chroma_format_idc, current->profile_idc == 183 ? 0 : 1);
     274             : 
     275          80 :         infer(separate_colour_plane_flag, 0);
     276          80 :         infer(bit_depth_luma_minus8,      0);
     277          80 :         infer(bit_depth_chroma_minus8,    0);
     278             :     }
     279             : 
     280          80 :     ue(log2_max_frame_num_minus4, 0, 12);
     281          80 :     ue(pic_order_cnt_type, 0, 2);
     282             : 
     283          80 :     if (current->pic_order_cnt_type == 0) {
     284          46 :         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 :             ses(offset_for_ref_frame[i], INT32_MIN + 1, INT32_MAX, 1, i);
     293             :     }
     294             : 
     295          80 :     ue(max_num_ref_frames, 0, H264_MAX_DPB_FRAMES);
     296          80 :     flag(gaps_in_frame_num_allowed_flag);
     297             : 
     298          80 :     ue(pic_width_in_mbs_minus1,        0, H264_MAX_MB_WIDTH);
     299          80 :     ue(pic_height_in_map_units_minus1, 0, H264_MAX_MB_HEIGHT);
     300             : 
     301          80 :     flag(frame_mbs_only_flag);
     302          80 :     if (!current->frame_mbs_only_flag)
     303          22 :         flag(mb_adaptive_frame_field_flag);
     304             : 
     305          80 :     flag(direct_8x8_inference_flag);
     306             : 
     307          80 :     flag(frame_cropping_flag);
     308          80 :     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          80 :     flag(vui_parameters_present_flag);
     316          80 :     if (current->vui_parameters_present_flag)
     317           8 :         CHECK(FUNC(vui_parameters)(ctx, rw, &current->vui, current));
     318             : 
     319          80 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
     320             : 
     321          80 :     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, MAX_UINT_BITS(bits));
     346           0 :         u(bits, alpha_transparent_value, 0, MAX_UINT_BITS(bits));
     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        2364 : static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
     357             :                      H264RawPPS *current)
     358             : {
     359        2364 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
     360             :     const H264RawSPS *sps;
     361             :     int err, i;
     362             : 
     363        2364 :     HEADER("Picture Parameter Set");
     364             : 
     365        2364 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
     366             :                                 1 << H264_NAL_PPS));
     367             : 
     368        2364 :     ue(pic_parameter_set_id, 0, 255);
     369        2364 :     ue(seq_parameter_set_id, 0, 31);
     370             : 
     371        2364 :     sps = h264->sps[current->seq_parameter_set_id];
     372        2364 :     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        2364 :     flag(entropy_coding_mode_flag);
     379        2364 :     flag(bottom_field_pic_order_in_frame_present_flag);
     380             : 
     381        2364 :     ue(num_slice_groups_minus1, 0, 7);
     382        2364 :     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 :                 ues(run_length_minus1[iGroup], 0, pic_size - 1, 1, iGroup);
     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 :                 ues(top_left[iGroup],       0, pic_size - 1, 1, iGroup);
     398           8 :                 ues(bottom_right[iGroup],
     399             :                     current->top_left[iGroup], pic_size - 1, 1, iGroup);
     400             :             }
     401          36 :         } else if (current->slice_group_map_type == 3 ||
     402          28 :                    current->slice_group_map_type == 4 ||
     403          12 :                    current->slice_group_map_type == 5) {
     404          12 :             flag(slice_group_change_direction_flag);
     405          12 :             ue(slice_group_change_rate_minus1, 0, pic_size - 1);
     406           8 :         } else if (current->slice_group_map_type == 6) {
     407           4 :             ue(pic_size_in_map_units_minus1, pic_size - 1, pic_size - 1);
     408             : 
     409           4 :             allocate(current->slice_group_id,
     410             :                      current->pic_size_in_map_units_minus1 + 1);
     411         400 :             for (i = 0; i <= current->pic_size_in_map_units_minus1; i++)
     412         396 :                 us(av_log2(2 * current->num_slice_groups_minus1 + 1),
     413             :                    slice_group_id[i], 0, current->num_slice_groups_minus1, 1, i);
     414             :         }
     415             :     }
     416             : 
     417        2364 :     ue(num_ref_idx_l0_default_active_minus1, 0, 31);
     418        2364 :     ue(num_ref_idx_l1_default_active_minus1, 0, 31);
     419             : 
     420        2364 :     flag(weighted_pred_flag);
     421        2364 :     u(2, weighted_bipred_idc, 0, 2);
     422             : 
     423        2364 :     se(pic_init_qp_minus26, -26 - 6 * sps->bit_depth_luma_minus8, +25);
     424        2364 :     se(pic_init_qs_minus26, -26, +25);
     425        2364 :     se(chroma_qp_index_offset, -12, +12);
     426             : 
     427        2364 :     flag(deblocking_filter_control_present_flag);
     428        2364 :     flag(constrained_intra_pred_flag);
     429        2364 :     flag(redundant_pic_cnt_present_flag);
     430             : 
     431        2364 :     if (more_rbsp_data(current->more_rbsp_data))
     432             :     {
     433           0 :         flag(transform_8x8_mode_flag);
     434             : 
     435           0 :         flag(pic_scaling_matrix_present_flag);
     436           0 :         if (current->pic_scaling_matrix_present_flag) {
     437           0 :             for (i = 0; i < 6 + (((sps->chroma_format_idc != 3) ? 2 : 6) *
     438           0 :                                  current->transform_8x8_mode_flag); i++) {
     439           0 :                 flags(pic_scaling_list_present_flag[i], 1, i);
     440           0 :                 if (current->pic_scaling_list_present_flag[i]) {
     441           0 :                     if (i < 6)
     442           0 :                         CHECK(FUNC(scaling_list)(ctx, rw,
     443             :                                                  &current->scaling_list_4x4[i],
     444             :                                                  16));
     445             :                     else
     446           0 :                         CHECK(FUNC(scaling_list)(ctx, rw,
     447             :                                                  &current->scaling_list_8x8[i - 6],
     448             :                                                  64));
     449             :                 }
     450             :             }
     451             :         }
     452             : 
     453           0 :         se(second_chroma_qp_index_offset, -12, +12);
     454             :     } else {
     455        2364 :         infer(transform_8x8_mode_flag, 0);
     456        2364 :         infer(pic_scaling_matrix_present_flag, 0);
     457        2364 :         infer(second_chroma_qp_index_offset, current->chroma_qp_index_offset);
     458             :     }
     459             : 
     460        2364 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
     461             : 
     462        2364 :     return 0;
     463             : }
     464             : 
     465           4 : static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
     466             :                                       H264RawSEIBufferingPeriod *current)
     467             : {
     468           4 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
     469             :     const H264RawSPS *sps;
     470             :     int err, i, length;
     471             : 
     472           4 :     ue(seq_parameter_set_id, 0, 31);
     473             : 
     474           4 :     sps = h264->sps[current->seq_parameter_set_id];
     475           4 :     if (!sps) {
     476           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
     477           0 :                current->seq_parameter_set_id);
     478           0 :         return AVERROR_INVALIDDATA;
     479             :     }
     480           4 :     h264->active_sps = sps;
     481             : 
     482           4 :     if (sps->vui.nal_hrd_parameters_present_flag) {
     483           8 :         for (i = 0; i <= sps->vui.nal_hrd_parameters.cpb_cnt_minus1; i++) {
     484           4 :             length = sps->vui.nal_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
     485           4 :             xu(length, initial_cpb_removal_delay[SchedSelIdx],
     486             :                current->nal.initial_cpb_removal_delay[i],
     487             :                1, MAX_UINT_BITS(length), 1, i);
     488           4 :             xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
     489             :                current->nal.initial_cpb_removal_delay_offset[i],
     490             :                0, MAX_UINT_BITS(length), 1, i);
     491             :         }
     492             :     }
     493             : 
     494           4 :     if (sps->vui.vcl_hrd_parameters_present_flag) {
     495           4 :         for (i = 0; i <= sps->vui.vcl_hrd_parameters.cpb_cnt_minus1; i++) {
     496           2 :             length = sps->vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
     497           2 :             xu(length, initial_cpb_removal_delay[SchedSelIdx],
     498             :                current->vcl.initial_cpb_removal_delay[i],
     499             :                1, MAX_UINT_BITS(length), 1, i);
     500           2 :             xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
     501             :                current->vcl.initial_cpb_removal_delay_offset[i],
     502             :                0, MAX_UINT_BITS(length), 1, i);
     503             :         }
     504             :     }
     505             : 
     506           4 :     return 0;
     507             : }
     508             : 
     509           0 : static int FUNC(sei_pic_timestamp)(CodedBitstreamContext *ctx, RWContext *rw,
     510             :                                    H264RawSEIPicTimestamp *current)
     511             : {
     512           0 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
     513             :     const H264RawSPS *sps;
     514             :     uint8_t time_offset_length;
     515             :     int err;
     516             : 
     517           0 :     u(2, ct_type, 0, 2);
     518           0 :     flag(nuit_field_based_flag);
     519           0 :     u(5, counting_type, 0, 6);
     520           0 :     flag(full_timestamp_flag);
     521           0 :     flag(discontinuity_flag);
     522           0 :     flag(cnt_dropped_flag);
     523           0 :     u(8, n_frames, 0, 255);
     524           0 :     if (current->full_timestamp_flag) {
     525           0 :             u(6, seconds_value, 0, 59);
     526           0 :             u(6, minutes_value, 0, 59);
     527           0 :             u(5, hours_value,   0, 23);
     528             :     } else {
     529           0 :         flag(seconds_flag);
     530           0 :         if (current->seconds_flag) {
     531           0 :             u(6, seconds_value, 0, 59);
     532           0 :             flag(minutes_flag);
     533           0 :             if (current->minutes_flag) {
     534           0 :                 u(6, minutes_value, 0, 59);
     535           0 :                 flag(hours_flag);
     536           0 :                 if (current->hours_flag)
     537           0 :                     u(5, hours_value, 0, 23);
     538             :             }
     539             :         }
     540             :     }
     541             : 
     542           0 :     sps = h264->active_sps;
     543           0 :     if (sps->vui.nal_hrd_parameters_present_flag)
     544           0 :         time_offset_length = sps->vui.nal_hrd_parameters.time_offset_length;
     545           0 :     else if (sps->vui.vcl_hrd_parameters_present_flag)
     546           0 :         time_offset_length = sps->vui.vcl_hrd_parameters.time_offset_length;
     547             :     else
     548           0 :         time_offset_length = 24;
     549             : 
     550           0 :     if (time_offset_length > 0)
     551           0 :         u(time_offset_length, time_offset,
     552             :           0, MAX_UINT_BITS(time_offset_length));
     553             :     else
     554           0 :         infer(time_offset, 0);
     555             : 
     556           0 :     return 0;
     557             : }
     558             : 
     559         566 : static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
     560             :                                 H264RawSEIPicTiming *current)
     561             : {
     562         566 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
     563             :     const H264RawSPS *sps;
     564             :     int err;
     565             : 
     566         566 :     sps = h264->active_sps;
     567         566 :     if (!sps) {
     568             :         // If there is exactly one possible SPS but it is not yet active
     569             :         // then just assume that it should be the active one.
     570           0 :         int i, k = -1;
     571           0 :         for (i = 0; i < H264_MAX_SPS_COUNT; i++) {
     572           0 :             if (h264->sps[i]) {
     573           0 :                 if (k >= 0) {
     574           0 :                     k = -1;
     575           0 :                     break;
     576             :                 }
     577           0 :                 k = i;
     578             :             }
     579             :         }
     580           0 :         if (k >= 0)
     581           0 :             sps = h264->sps[k];
     582             :     }
     583         566 :     if (!sps) {
     584           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR,
     585             :                "No active SPS for pic_timing.\n");
     586           0 :         return AVERROR_INVALIDDATA;
     587             :     }
     588             : 
     589         566 :     if (sps->vui.nal_hrd_parameters_present_flag ||
     590           0 :         sps->vui.vcl_hrd_parameters_present_flag) {
     591             :         const H264RawHRD *hrd;
     592             : 
     593         566 :         if (sps->vui.nal_hrd_parameters_present_flag)
     594         566 :             hrd = &sps->vui.nal_hrd_parameters;
     595           0 :         else if (sps->vui.vcl_hrd_parameters_present_flag)
     596           0 :             hrd = &sps->vui.vcl_hrd_parameters;
     597             :         else {
     598           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR,
     599             :                    "No HRD parameters for pic_timing.\n");
     600           0 :             return AVERROR_INVALIDDATA;
     601             :         }
     602             : 
     603         566 :         u(hrd->cpb_removal_delay_length_minus1 + 1, cpb_removal_delay,
     604             :           0, MAX_UINT_BITS(hrd->cpb_removal_delay_length_minus1 + 1));
     605         566 :         u(hrd->dpb_output_delay_length_minus1 + 1, dpb_output_delay,
     606             :           0, MAX_UINT_BITS(hrd->dpb_output_delay_length_minus1 + 1));
     607             :     }
     608             : 
     609         566 :     if (sps->vui.pic_struct_present_flag) {
     610             :         static const int num_clock_ts[9] = {
     611             :             1, 1, 1, 2, 2, 3, 3, 2, 3
     612             :         };
     613             :         int i;
     614             : 
     615         556 :         u(4, pic_struct, 0, 8);
     616         556 :         if (current->pic_struct > 8)
     617           0 :             return AVERROR_INVALIDDATA;
     618             : 
     619        1944 :         for (i = 0; i < num_clock_ts[current->pic_struct]; i++) {
     620        1388 :             flags(clock_timestamp_flag[i], 1, i);
     621        1388 :             if (current->clock_timestamp_flag[i])
     622           0 :                 CHECK(FUNC(sei_pic_timestamp)(ctx, rw, &current->timestamp[i]));
     623             :         }
     624             :     }
     625             : 
     626         566 :     return 0;
     627             : }
     628             : 
     629          10 : static int FUNC(sei_pan_scan_rect)(CodedBitstreamContext *ctx, RWContext *rw,
     630             :                                    H264RawSEIPanScanRect *current)
     631             : {
     632             :     int err, i;
     633             : 
     634          10 :     ue(pan_scan_rect_id, 0, UINT32_MAX - 1);
     635          10 :     flag(pan_scan_rect_cancel_flag);
     636             : 
     637          10 :     if (!current->pan_scan_rect_cancel_flag) {
     638          10 :         ue(pan_scan_cnt_minus1, 0, 2);
     639             : 
     640          20 :         for (i = 0; i <= current->pan_scan_cnt_minus1; i++) {
     641          10 :             ses(pan_scan_rect_left_offset[i],   INT32_MIN + 1, INT32_MAX, 1, i);
     642          10 :             ses(pan_scan_rect_right_offset[i],  INT32_MIN + 1, INT32_MAX, 1, i);
     643          10 :             ses(pan_scan_rect_top_offset[i],    INT32_MIN + 1, INT32_MAX, 1, i);
     644          10 :             ses(pan_scan_rect_bottom_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
     645             :         }
     646             : 
     647          10 :         ue(pan_scan_rect_repetition_period, 0, 16384);
     648             :     }
     649             : 
     650          10 :     return 0;
     651             : }
     652             : 
     653          10 : static int FUNC(sei_user_data_registered)(CodedBitstreamContext *ctx, RWContext *rw,
     654             :                                           H264RawSEIUserDataRegistered *current,
     655             :                                           uint32_t *payload_size)
     656             : {
     657             :     int err, i, j;
     658             : 
     659          10 :     u(8, itu_t_t35_country_code, 0x00, 0xff);
     660          10 :     if (current->itu_t_t35_country_code != 0xff)
     661          10 :         i = 1;
     662             :     else {
     663           0 :         u(8, itu_t_t35_country_code_extension_byte, 0x00, 0xff);
     664           0 :         i = 2;
     665             :     }
     666             : 
     667             : #ifdef READ
     668           5 :     if (*payload_size < i) {
     669           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR,
     670             :                "Invalid SEI user data registered payload.\n");
     671           0 :         return AVERROR_INVALIDDATA;
     672             :     }
     673           5 :     current->data_length = *payload_size - i;
     674             : #else
     675           5 :     *payload_size = i + current->data_length;
     676             : #endif
     677             : 
     678          10 :     allocate(current->data, current->data_length);
     679         176 :     for (j = 0; j < current->data_length; j++)
     680         166 :         xu(8, itu_t_t35_payload_byte[i], current->data[j], 0x00, 0xff, 1, i + j);
     681             : 
     682          10 :     return 0;
     683             : }
     684             : 
     685           0 : static int FUNC(sei_user_data_unregistered)(CodedBitstreamContext *ctx, RWContext *rw,
     686             :                                             H264RawSEIUserDataUnregistered *current,
     687             :                                             uint32_t *payload_size)
     688             : {
     689             :     int err, i;
     690             : 
     691             : #ifdef READ
     692           0 :     if (*payload_size < 16) {
     693           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR,
     694             :                "Invalid SEI user data unregistered payload.\n");
     695           0 :         return AVERROR_INVALIDDATA;
     696             :     }
     697           0 :     current->data_length = *payload_size - 16;
     698             : #else
     699           0 :     *payload_size = 16 + current->data_length;
     700             : #endif
     701             : 
     702           0 :     for (i = 0; i < 16; i++)
     703           0 :         us(8, uuid_iso_iec_11578[i], 0x00, 0xff, 1, i);
     704             : 
     705           0 :     allocate(current->data, current->data_length);
     706             : 
     707           0 :     for (i = 0; i < current->data_length; i++)
     708           0 :         xu(8, user_data_payload_byte[i], current->data[i], 0x00, 0xff, 1, i);
     709             : 
     710           0 :     return 0;
     711             : }
     712             : 
     713           2 : static int FUNC(sei_recovery_point)(CodedBitstreamContext *ctx, RWContext *rw,
     714             :                                     H264RawSEIRecoveryPoint *current)
     715             : {
     716             :     int err;
     717             : 
     718           2 :     ue(recovery_frame_cnt, 0, 65535);
     719           2 :     flag(exact_match_flag);
     720           2 :     flag(broken_link_flag);
     721           2 :     u(2, changing_slice_group_idc, 0, 2);
     722             : 
     723           2 :     return 0;
     724             : }
     725             : 
     726           2 : static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *rw,
     727             :                                          H264RawSEIDisplayOrientation *current)
     728             : {
     729             :     int err;
     730             : 
     731           2 :     flag(display_orientation_cancel_flag);
     732           2 :     if (!current->display_orientation_cancel_flag) {
     733           2 :         flag(hor_flip);
     734           2 :         flag(ver_flip);
     735           2 :         u(16, anticlockwise_rotation, 0, 65535);
     736           2 :         ue(display_orientation_repetition_period, 0, 16384);
     737           2 :         flag(display_orientation_extension_flag);
     738             :     }
     739             : 
     740           2 :     return 0;
     741             : }
     742             : 
     743           2 : static int FUNC(sei_mastering_display_colour_volume)(CodedBitstreamContext *ctx, RWContext *rw,
     744             :                                                      H264RawSEIMasteringDisplayColourVolume *current)
     745             : {
     746             :     int err, c;
     747             : 
     748           8 :     for (c = 0; c < 3; c++) {
     749           6 :         us(16, display_primaries_x[c], 0, 50000, 1, c);
     750           6 :         us(16, display_primaries_y[c], 0, 50000, 1, c);
     751             :     }
     752             : 
     753           2 :     u(16, white_point_x, 0, 50000);
     754           2 :     u(16, white_point_y, 0, 50000);
     755             : 
     756           2 :     u(32, max_display_mastering_luminance, 1, MAX_UINT_BITS(32));
     757           2 :     u(32, min_display_mastering_luminance, 0, current->max_display_mastering_luminance - 1);
     758             : 
     759           2 :     return 0;
     760             : }
     761             : 
     762         607 : static int FUNC(sei_payload)(CodedBitstreamContext *ctx, RWContext *rw,
     763             :                              H264RawSEIPayload *current)
     764             : {
     765             :     int err, i;
     766             :     int start_position, end_position;
     767             : 
     768             : #ifdef READ
     769         303 :     start_position = get_bits_count(rw);
     770             : #else
     771         304 :     start_position = put_bits_count(rw);
     772             : #endif
     773             : 
     774         607 :     switch (current->payload_type) {
     775           4 :     case H264_SEI_TYPE_BUFFERING_PERIOD:
     776           4 :         CHECK(FUNC(sei_buffering_period)
     777             :               (ctx, rw, &current->payload.buffering_period));
     778           4 :         break;
     779         566 :     case H264_SEI_TYPE_PIC_TIMING:
     780         566 :         CHECK(FUNC(sei_pic_timing)
     781             :               (ctx, rw, &current->payload.pic_timing));
     782         566 :         break;
     783          10 :     case H264_SEI_TYPE_PAN_SCAN_RECT:
     784          10 :         CHECK(FUNC(sei_pan_scan_rect)
     785             :               (ctx, rw, &current->payload.pan_scan_rect));
     786          10 :         break;
     787           0 :     case H264_SEI_TYPE_FILLER_PAYLOAD:
     788             :         {
     789           0 :             for (i = 0; i  < current->payload_size; i++)
     790           0 :                 fixed(8, ff_byte, 0xff);
     791             :         }
     792           0 :         break;
     793          10 :     case H264_SEI_TYPE_USER_DATA_REGISTERED:
     794          10 :         CHECK(FUNC(sei_user_data_registered)
     795             :               (ctx, rw, &current->payload.user_data_registered, &current->payload_size));
     796          10 :         break;
     797           0 :     case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
     798           0 :         CHECK(FUNC(sei_user_data_unregistered)
     799             :               (ctx, rw, &current->payload.user_data_unregistered, &current->payload_size));
     800           0 :         break;
     801           2 :     case H264_SEI_TYPE_RECOVERY_POINT:
     802           2 :         CHECK(FUNC(sei_recovery_point)
     803             :               (ctx, rw, &current->payload.recovery_point));
     804           2 :         break;
     805           2 :     case H264_SEI_TYPE_DISPLAY_ORIENTATION:
     806           2 :         CHECK(FUNC(sei_display_orientation)
     807             :               (ctx, rw, &current->payload.display_orientation));
     808           2 :         break;
     809           2 :     case H264_SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
     810           2 :         CHECK(FUNC(sei_mastering_display_colour_volume)
     811             :               (ctx, rw, &current->payload.mastering_display_colour_volume));
     812           2 :         break;
     813          11 :     default:
     814             :         {
     815             : #ifdef READ
     816           5 :             current->payload.other.data_length = current->payload_size;
     817             : #endif
     818          11 :             allocate(current->payload.other.data, current->payload.other.data_length);
     819        3499 :             for (i = 0; i < current->payload.other.data_length; i++)
     820        3488 :                 xu(8, payload_byte[i], current->payload.other.data[i], 0, 255, 1, i);
     821             :         }
     822             :     }
     823             : 
     824         607 :     if (byte_alignment(rw)) {
     825         564 :         fixed(1, bit_equal_to_one, 1);
     826        1436 :         while (byte_alignment(rw))
     827         308 :             fixed(1, bit_equal_to_zero, 0);
     828             :     }
     829             : 
     830             : #ifdef READ
     831         303 :     end_position = get_bits_count(rw);
     832         303 :     if (end_position < start_position + 8 * current->payload_size) {
     833           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Incorrect SEI payload length: "
     834             :                "header %"PRIu32" bits, actually %d bits.\n",
     835           0 :                8 * current->payload_size,
     836             :                end_position - start_position);
     837           0 :         return AVERROR_INVALIDDATA;
     838             :     }
     839             : #else
     840         304 :     end_position = put_bits_count(rw);
     841         304 :     current->payload_size = (end_position - start_position) / 8;
     842             : #endif
     843             : 
     844         607 :     return 0;
     845             : }
     846             : 
     847         566 : static int FUNC(sei)(CodedBitstreamContext *ctx, RWContext *rw,
     848             :                      H264RawSEI *current)
     849             : {
     850             :     int err, k;
     851             : 
     852         566 :     HEADER("Supplemental Enhancement Information");
     853             : 
     854         566 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
     855             :                                 1 << H264_NAL_SEI));
     856             : 
     857             : #ifdef READ
     858         303 :     for (k = 0; k < H264_MAX_SEI_PAYLOADS; k++) {
     859         303 :         uint32_t payload_type = 0;
     860         303 :         uint32_t payload_size = 0;
     861             :         uint32_t tmp;
     862             : 
     863         626 :         while (show_bits(rw, 8) == 0xff) {
     864          20 :             fixed(8, ff_byte, 0xff);
     865          20 :             payload_type += 255;
     866             :         }
     867         303 :         xu(8, last_payload_type_byte, tmp, 0, 254, 0);
     868         303 :         payload_type += tmp;
     869             : 
     870         611 :         while (show_bits(rw, 8) == 0xff) {
     871           5 :             fixed(8, ff_byte, 0xff);
     872           5 :             payload_size += 255;
     873             :         }
     874         303 :         xu(8, last_payload_size_byte, tmp, 0, 254, 0);
     875         303 :         payload_size += tmp;
     876             : 
     877         303 :         current->payload[k].payload_type = payload_type;
     878         303 :         current->payload[k].payload_size = payload_size;
     879             : 
     880         303 :         CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
     881             : 
     882         303 :         if (!cbs_h2645_read_more_rbsp_data(rw))
     883         283 :             break;
     884             :     }
     885         283 :     if (k >= H264_MAX_SEI_PAYLOADS) {
     886           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
     887             :                "SEI message: found %d.\n", k);
     888           0 :         return AVERROR_INVALIDDATA;
     889             :     }
     890         283 :     current->payload_count = k + 1;
     891             : #else
     892        1172 :     for (k = 0; k < current->payload_count; k++) {
     893             :         PutBitContext start_state;
     894             :         uint32_t tmp;
     895             :         int need_size, i;
     896             : 
     897             :         // Somewhat clumsy: we write the payload twice when
     898             :         // we don't know the size in advance.  This will mess
     899             :         // with trace output, but is otherwise harmless.
     900         303 :         start_state = *rw;
     901         303 :         need_size = !current->payload[k].payload_size;
     902         607 :         for (i = 0; i < 1 + need_size; i++) {
     903         304 :             *rw = start_state;
     904             : 
     905         304 :             tmp = current->payload[k].payload_type;
     906         632 :             while (tmp >= 255) {
     907          24 :                 fixed(8, ff_byte, 0xff);
     908          24 :                 tmp -= 255;
     909             :             }
     910         304 :             xu(8, last_payload_type_byte, tmp, 0, 254, 0);
     911             : 
     912         304 :             tmp = current->payload[k].payload_size;
     913         613 :             while (tmp >= 255) {
     914           5 :                 fixed(8, ff_byte, 0xff);
     915           5 :                 tmp -= 255;
     916             :             }
     917         304 :             xu(8, last_payload_size_byte, tmp, 0, 254, 0);
     918             : 
     919         304 :             CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
     920             :         }
     921             :     }
     922             : #endif
     923             : 
     924         566 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
     925             : 
     926         566 :     return 0;
     927             : }
     928             : 
     929         390 : static int FUNC(aud)(CodedBitstreamContext *ctx, RWContext *rw,
     930             :                      H264RawAUD *current)
     931             : {
     932             :     int err;
     933             : 
     934         390 :     HEADER("Access Unit Delimiter");
     935             : 
     936         390 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
     937             :                                 1 << H264_NAL_AUD));
     938             : 
     939         390 :     u(3, primary_pic_type, 0, 7);
     940             : 
     941         390 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
     942             : 
     943         390 :     return 0;
     944             : }
     945             : 
     946        9826 : static int FUNC(ref_pic_list_modification)(CodedBitstreamContext *ctx, RWContext *rw,
     947             :                                            H264RawSliceHeader *current)
     948             : {
     949        9826 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
     950        9826 :     const H264RawSPS *sps = h264->active_sps;
     951             :     int err, i, mopn;
     952             : 
     953       19088 :     if (current->slice_type % 5 != 2 &&
     954        9262 :         current->slice_type % 5 != 4) {
     955        9262 :         flag(ref_pic_list_modification_flag_l0);
     956        9262 :         if (current->ref_pic_list_modification_flag_l0) {
     957        3034 :             for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
     958        3034 :                 xue(modification_of_pic_nums_idc,
     959             :                     current->rplm_l0[i].modification_of_pic_nums_idc, 0, 3, 0);
     960             : 
     961        3034 :                 mopn = current->rplm_l0[i].modification_of_pic_nums_idc;
     962        3034 :                 if (mopn == 3)
     963         972 :                     break;
     964             : 
     965        2062 :                 if (mopn == 0 || mopn == 1)
     966        2048 :                     xue(abs_diff_pic_num_minus1,
     967             :                         current->rplm_l0[i].abs_diff_pic_num_minus1,
     968             :                         0, (1 + current->field_pic_flag) *
     969             :                         (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
     970          14 :                 else if (mopn == 2)
     971          14 :                     xue(long_term_pic_num,
     972             :                         current->rplm_l0[i].long_term_pic_num,
     973             :                         0, sps->max_num_ref_frames - 1, 0);
     974             :             }
     975             :         }
     976             :     }
     977             : 
     978        9826 :     if (current->slice_type % 5 == 1) {
     979        2136 :         flag(ref_pic_list_modification_flag_l1);
     980        2136 :         if (current->ref_pic_list_modification_flag_l1) {
     981           0 :             for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
     982           0 :                 xue(modification_of_pic_nums_idc,
     983             :                     current->rplm_l1[i].modification_of_pic_nums_idc, 0, 3, 0);
     984             : 
     985           0 :                 mopn = current->rplm_l1[i].modification_of_pic_nums_idc;
     986           0 :                 if (mopn == 3)
     987           0 :                     break;
     988             : 
     989           0 :                 if (mopn == 0 || mopn == 1)
     990           0 :                     xue(abs_diff_pic_num_minus1,
     991             :                         current->rplm_l1[i].abs_diff_pic_num_minus1,
     992             :                         0, (1 + current->field_pic_flag) *
     993             :                         (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
     994           0 :                 else if (mopn == 2)
     995           0 :                     xue(long_term_pic_num,
     996             :                         current->rplm_l1[i].long_term_pic_num,
     997             :                         0, sps->max_num_ref_frames - 1, 0);
     998             :             }
     999             :         }
    1000             :     }
    1001             : 
    1002        9826 :     return 0;
    1003             : }
    1004             : 
    1005         168 : static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
    1006             :                                    H264RawSliceHeader *current)
    1007             : {
    1008         168 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
    1009         168 :     const H264RawSPS *sps = h264->active_sps;
    1010             :     int chroma;
    1011             :     int err, i, j;
    1012             : 
    1013         168 :     ue(luma_log2_weight_denom, 0, 7);
    1014             : 
    1015         168 :     chroma = !sps->separate_colour_plane_flag && sps->chroma_format_idc != 0;
    1016         168 :     if (chroma)
    1017         168 :         ue(chroma_log2_weight_denom, 0, 7);
    1018             : 
    1019         988 :     for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
    1020         820 :         flags(luma_weight_l0_flag[i], 1, i);
    1021         820 :         if (current->luma_weight_l0_flag[i]) {
    1022         436 :             ses(luma_weight_l0[i], -128, +127, 1, i);
    1023         436 :             ses(luma_offset_l0[i], -128, +127, 1, i);
    1024             :         }
    1025         820 :         if (chroma) {
    1026         820 :             flags(chroma_weight_l0_flag[i], 1, i);
    1027         820 :             if (current->chroma_weight_l0_flag[i]) {
    1028        1248 :                 for (j = 0; j < 2; j++) {
    1029         832 :                     ses(chroma_weight_l0[i][j], -128, +127, 2, i, j);
    1030         832 :                     ses(chroma_offset_l0[i][j], -128, +127, 2, i, j);
    1031             :                 }
    1032             :             }
    1033             :         }
    1034             :     }
    1035             : 
    1036         168 :     if (current->slice_type % 5 == 1) {
    1037           0 :         for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
    1038           0 :             flags(luma_weight_l1_flag[i], 1, i);
    1039           0 :             if (current->luma_weight_l1_flag[i]) {
    1040           0 :                 ses(luma_weight_l1[i], -128, +127, 1, i);
    1041           0 :                 ses(luma_offset_l1[i], -128, +127, 1, i);
    1042             :             }
    1043           0 :             if (chroma) {
    1044           0 :                 flags(chroma_weight_l1_flag[i], 1, i);
    1045           0 :                 if (current->chroma_weight_l1_flag[i]) {
    1046           0 :                     for (j = 0; j < 2; j++) {
    1047           0 :                         ses(chroma_weight_l1[i][j], -128, +127, 2, i, j);
    1048           0 :                         ses(chroma_offset_l1[i][j], -128, +127, 2, i, j);
    1049             :                     }
    1050             :                 }
    1051             :             }
    1052             :         }
    1053             :     }
    1054             : 
    1055         168 :     return 0;
    1056             : }
    1057             : 
    1058        7686 : static int FUNC(dec_ref_pic_marking)(CodedBitstreamContext *ctx, RWContext *rw,
    1059             :                                      H264RawSliceHeader *current, int idr_pic_flag)
    1060             : {
    1061        7686 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
    1062        7686 :     const H264RawSPS *sps = h264->active_sps;
    1063             :     int err, i;
    1064             :     uint32_t mmco;
    1065             : 
    1066        7686 :     if (idr_pic_flag) {
    1067         116 :         flag(no_output_of_prior_pics_flag);
    1068         116 :         flag(long_term_reference_flag);
    1069             :     } else {
    1070        7570 :         flag(adaptive_ref_pic_marking_mode_flag);
    1071        7570 :         if (current->adaptive_ref_pic_marking_mode_flag) {
    1072         826 :             for (i = 0; i < H264_MAX_MMCO_COUNT; i++) {
    1073         826 :                 xue(memory_management_control_operation,
    1074             :                     current->mmco[i].memory_management_control_operation,
    1075             :                     0, 6, 0);
    1076             : 
    1077         826 :                 mmco = current->mmco[i].memory_management_control_operation;
    1078         826 :                 if (mmco == 0)
    1079         412 :                     break;
    1080             : 
    1081         414 :                 if (mmco == 1 || mmco == 3)
    1082         332 :                     xue(difference_of_pic_nums_minus1,
    1083             :                         current->mmco[i].difference_of_pic_nums_minus1,
    1084             :                         0, INT32_MAX, 0);
    1085         414 :                 if (mmco == 2)
    1086           0 :                     xue(long_term_pic_num,
    1087             :                         current->mmco[i].long_term_pic_num,
    1088             :                         0, sps->max_num_ref_frames - 1, 0);
    1089         414 :                 if (mmco == 3 || mmco == 6)
    1090          56 :                     xue(long_term_frame_idx,
    1091             :                         current->mmco[i].long_term_frame_idx,
    1092             :                         0, sps->max_num_ref_frames - 1, 0);
    1093         414 :                 if (mmco == 4)
    1094           4 :                     xue(max_long_term_frame_idx_plus1,
    1095             :                         current->mmco[i].max_long_term_frame_idx_plus1,
    1096             :                         0, sps->max_num_ref_frames, 0);
    1097             :             }
    1098         412 :             if (i == H264_MAX_MMCO_COUNT) {
    1099           0 :                 av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many "
    1100             :                        "memory management control operations.\n");
    1101           0 :                 return AVERROR_INVALIDDATA;
    1102             :             }
    1103             :         }
    1104             :     }
    1105             : 
    1106        7686 :     return 0;
    1107             : }
    1108             : 
    1109        9826 : static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw,
    1110             :                               H264RawSliceHeader *current)
    1111             : {
    1112        9826 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
    1113             :     const H264RawSPS *sps;
    1114             :     const H264RawPPS *pps;
    1115             :     int err;
    1116             :     int idr_pic_flag;
    1117             :     int slice_type_i, slice_type_p, slice_type_b;
    1118             :     int slice_type_si, slice_type_sp;
    1119             : 
    1120        9826 :     HEADER("Slice Header");
    1121             : 
    1122        9826 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
    1123             :                                 1 << H264_NAL_SLICE     |
    1124             :                                 1 << H264_NAL_IDR_SLICE |
    1125             :                                 1 << H264_NAL_AUXILIARY_SLICE));
    1126             : 
    1127        9826 :     if (current->nal_unit_header.nal_unit_type == H264_NAL_AUXILIARY_SLICE) {
    1128           0 :         if (!h264->last_slice_nal_unit_type) {
    1129           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Auxiliary slice "
    1130             :                    "is not decodable without the main picture "
    1131             :                    "in the same access unit.\n");
    1132           0 :             return AVERROR_INVALIDDATA;
    1133             :         }
    1134             :     } else {
    1135        9826 :         h264->last_slice_nal_unit_type =
    1136        9826 :             current->nal_unit_header.nal_unit_type;
    1137             :     }
    1138        9826 :     idr_pic_flag = h264->last_slice_nal_unit_type == H264_NAL_IDR_SLICE;
    1139             : 
    1140        9826 :     ue(first_mb_in_slice, 0, H264_MAX_MB_PIC_SIZE - 1);
    1141        9826 :     ue(slice_type, 0, 9);
    1142             : 
    1143        9826 :     slice_type_i  = current->slice_type % 5 == 2;
    1144        9826 :     slice_type_p  = current->slice_type % 5 == 0;
    1145        9826 :     slice_type_b  = current->slice_type % 5 == 1;
    1146        9826 :     slice_type_si = current->slice_type % 5 == 4;
    1147        9826 :     slice_type_sp = current->slice_type % 5 == 3;
    1148             : 
    1149        9826 :     if (idr_pic_flag && !(slice_type_i || slice_type_si)) {
    1150           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid slice type %d "
    1151           0 :                "for IDR picture.\n", current->slice_type);
    1152           0 :         return AVERROR_INVALIDDATA;
    1153             :     }
    1154             : 
    1155        9826 :     ue(pic_parameter_set_id, 0, 255);
    1156             : 
    1157        9826 :     pps = h264->pps[current->pic_parameter_set_id];
    1158        9826 :     if (!pps) {
    1159           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
    1160           0 :                current->pic_parameter_set_id);
    1161           0 :         return AVERROR_INVALIDDATA;
    1162             :     }
    1163        9826 :     h264->active_pps = pps;
    1164             : 
    1165        9826 :     sps = h264->sps[pps->seq_parameter_set_id];
    1166        9826 :     if (!sps) {
    1167           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
    1168           0 :                pps->seq_parameter_set_id);
    1169           0 :         return AVERROR_INVALIDDATA;
    1170             :     }
    1171        9826 :     h264->active_sps = sps;
    1172             : 
    1173        9826 :     if (sps->separate_colour_plane_flag)
    1174           0 :         u(2, colour_plane_id, 0, 2);
    1175             : 
    1176        9826 :     u(sps->log2_max_frame_num_minus4 + 4, frame_num,
    1177             :       0, MAX_UINT_BITS(sps->log2_max_frame_num_minus4 + 4));
    1178             : 
    1179        9826 :     if (!sps->frame_mbs_only_flag) {
    1180         702 :         flag(field_pic_flag);
    1181         702 :         if (current->field_pic_flag)
    1182         508 :             flag(bottom_field_flag);
    1183             :         else
    1184         194 :             infer(bottom_field_flag, 0);
    1185             :     } else {
    1186        9124 :         infer(field_pic_flag,    0);
    1187        9124 :         infer(bottom_field_flag, 0);
    1188             :     }
    1189             : 
    1190        9826 :     if (idr_pic_flag)
    1191         116 :         ue(idr_pic_id, 0, 65535);
    1192             : 
    1193        9826 :     if (sps->pic_order_cnt_type == 0) {
    1194        4982 :         u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, pic_order_cnt_lsb,
    1195             :           0, MAX_UINT_BITS(sps->log2_max_pic_order_cnt_lsb_minus4 + 4));
    1196        5162 :         if (pps->bottom_field_pic_order_in_frame_present_flag &&
    1197         180 :             !current->field_pic_flag)
    1198         180 :             se(delta_pic_order_cnt_bottom, INT32_MIN + 1, INT32_MAX);
    1199             : 
    1200        4844 :     } else if (sps->pic_order_cnt_type == 1) {
    1201        4562 :         if (!sps->delta_pic_order_always_zero_flag) {
    1202         846 :             se(delta_pic_order_cnt[0], INT32_MIN + 1, INT32_MAX);
    1203         846 :             if (pps->bottom_field_pic_order_in_frame_present_flag &&
    1204           0 :                 !current->field_pic_flag)
    1205           0 :                 se(delta_pic_order_cnt[1], INT32_MIN + 1, INT32_MAX);
    1206             :             else
    1207         846 :                 infer(delta_pic_order_cnt[1], 0);
    1208             :         } else {
    1209        3716 :             infer(delta_pic_order_cnt[0], 0);
    1210        3716 :             infer(delta_pic_order_cnt[1], 0);
    1211             :         }
    1212             :     }
    1213             : 
    1214        9826 :     if (pps->redundant_pic_cnt_present_flag)
    1215           0 :         ue(redundant_pic_cnt, 0, 127);
    1216             : 
    1217        9826 :     if (slice_type_b)
    1218        2136 :         flag(direct_spatial_mv_pred_flag);
    1219             : 
    1220        9826 :     if (slice_type_p || slice_type_sp || slice_type_b) {
    1221        9262 :         flag(num_ref_idx_active_override_flag);
    1222        9262 :         if (current->num_ref_idx_active_override_flag) {
    1223        4360 :             ue(num_ref_idx_l0_active_minus1, 0, 31);
    1224        4360 :             if (slice_type_b)
    1225        2016 :                 ue(num_ref_idx_l1_active_minus1, 0, 31);
    1226             :         } else {
    1227        4902 :             infer(num_ref_idx_l0_active_minus1,
    1228             :                   pps->num_ref_idx_l0_default_active_minus1);
    1229        4902 :             infer(num_ref_idx_l1_active_minus1,
    1230             :                   pps->num_ref_idx_l1_default_active_minus1);
    1231             :         }
    1232             :     }
    1233             : 
    1234       19652 :     if (current->nal_unit_header.nal_unit_type == 20 ||
    1235        9826 :         current->nal_unit_header.nal_unit_type == 21) {
    1236           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "MVC / 3DAVC not supported.\n");
    1237           0 :         return AVERROR_PATCHWELCOME;
    1238             :     } else {
    1239        9826 :         CHECK(FUNC(ref_pic_list_modification)(ctx, rw, current));
    1240             :     }
    1241             : 
    1242       19484 :     if ((pps->weighted_pred_flag && (slice_type_p || slice_type_sp)) ||
    1243        9658 :         (pps->weighted_bipred_idc == 1 && slice_type_b)) {
    1244         168 :         CHECK(FUNC(pred_weight_table)(ctx, rw, current));
    1245             :     }
    1246             : 
    1247        9826 :     if (current->nal_unit_header.nal_ref_idc != 0) {
    1248        7686 :         CHECK(FUNC(dec_ref_pic_marking)(ctx, rw, current, idr_pic_flag));
    1249             :     }
    1250             : 
    1251        9826 :     if (pps->entropy_coding_mode_flag &&
    1252        2240 :         !slice_type_i && !slice_type_si) {
    1253        2240 :         ue(cabac_init_idc, 0, 2);
    1254             :     }
    1255             : 
    1256        9826 :     se(slice_qp_delta, - 51 - 6 * sps->bit_depth_luma_minus8,
    1257             :                        + 51 + 6 * sps->bit_depth_luma_minus8);
    1258        9826 :     if (slice_type_sp || slice_type_si) {
    1259          78 :         if (slice_type_sp)
    1260          78 :             flag(sp_for_switch_flag);
    1261          78 :         se(slice_qs_delta, -51, +51);
    1262             :     }
    1263             : 
    1264        9826 :     if (pps->deblocking_filter_control_present_flag) {
    1265        5818 :         ue(disable_deblocking_filter_idc, 0, 2);
    1266        5818 :         if (current->disable_deblocking_filter_idc != 1) {
    1267        4116 :             se(slice_alpha_c0_offset_div2, -6, +6);
    1268        4116 :             se(slice_beta_offset_div2,     -6, +6);
    1269             :         } else {
    1270        1702 :             infer(slice_alpha_c0_offset_div2, 0);
    1271        1702 :             infer(slice_beta_offset_div2,     0);
    1272             :         }
    1273             :     } else {
    1274        4008 :         infer(disable_deblocking_filter_idc, 0);
    1275        4008 :         infer(slice_alpha_c0_offset_div2,    0);
    1276        4008 :         infer(slice_beta_offset_div2,        0);
    1277             :     }
    1278             : 
    1279       13064 :     if (pps->num_slice_groups_minus1 > 0 &&
    1280        4566 :         pps->slice_group_map_type >= 3 &&
    1281        1328 :         pps->slice_group_map_type <= 5) {
    1282             :         unsigned int pic_size, max, bits;
    1283             : 
    1284        1256 :         pic_size = (sps->pic_width_in_mbs_minus1 + 1) *
    1285         628 :                    (sps->pic_height_in_map_units_minus1 + 1);
    1286        1256 :         max = (pic_size + pps->slice_group_change_rate_minus1) /
    1287         628 :               (pps->slice_group_change_rate_minus1 + 1);
    1288         628 :         bits = av_log2(2 * max - 1);
    1289             : 
    1290         628 :         u(bits, slice_group_change_cycle, 0, max);
    1291             :     }
    1292             : 
    1293        9826 :     if (pps->entropy_coding_mode_flag) {
    1294       12532 :         while (byte_alignment(rw))
    1295        7732 :             fixed(1, cabac_alignment_one_bit, 1);
    1296             :     }
    1297             : 
    1298        9826 :     return 0;
    1299             : }
    1300             : 
    1301           0 : static int FUNC(filler)(CodedBitstreamContext *ctx, RWContext *rw,
    1302             :                         H264RawFiller *current)
    1303             : {
    1304             :     int err;
    1305             : 
    1306           0 :     HEADER("Filler Data");
    1307             : 
    1308           0 :     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
    1309             :                                 1 << H264_NAL_FILLER_DATA));
    1310             : 
    1311             : #ifdef READ
    1312           0 :     while (show_bits(rw, 8) == 0xff) {
    1313           0 :         fixed(8, ff_byte, 0xff);
    1314           0 :         ++current->filler_size;
    1315             :     }
    1316             : #else
    1317             :     {
    1318             :         uint32_t i;
    1319           0 :         for (i = 0; i < current->filler_size; i++)
    1320           0 :             fixed(8, ff_byte, 0xff);
    1321             :     }
    1322             : #endif
    1323             : 
    1324           0 :     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
    1325             : 
    1326           0 :     return 0;
    1327             : }

Generated by: LCOV version 1.13