<commit_before>package org.jcodec.codecs.h264.io.model;

import static org.jcodec.codecs.h264.decode.CAVLCReader.moreRBSPData;
import static org.jcodec.codecs.h264.decode.CAVLCReader.readBool;
import static org.jcodec.codecs.h264.decode.CAVLCReader.readNBit;
import static org.jcodec.codecs.h264.decode.CAVLCReader.readSE;
import static org.jcodec.codecs.h264.decode.CAVLCReader.readU;
import static org.jcodec.codecs.h264.decode.CAVLCReader.readUE;
import static org.jcodec.codecs.h264.io.write.CAVLCWriter.writeBool;
import static org.jcodec.codecs.h264.io.write.CAVLCWriter.writeNBit;
import static org.jcodec.codecs.h264.io.write.CAVLCWriter.writeSE;
import static org.jcodec.codecs.h264.io.write.CAVLCWriter.writeTrailingBits;
import static org.jcodec.codecs.h264.io.write.CAVLCWriter.writeU;
import static org.jcodec.codecs.h264.io.write.CAVLCWriter.writeUE;

import java.nio.ByteBuffer;
import java.util.Arrays;

import org.jcodec.common.io.BitReader;
import org.jcodec.common.io.BitWriter;

/**
 * This class is part of JCodec ( www.jcodec.org ) This software is distributed
 * under FreeBSD License
 * 
 * Picture Parameter Set entity of H264 bitstream
 * 
 * capable to serialize / deserialize with CAVLC bitstream
 * 
 * @author The JCodec project
 * 
 */
public class PictureParameterSet {

    public static class PPSExt {
        public boolean transform_8x8_mode_flag;
        public ScalingMatrix scalindMatrix;
        public int second_chroma_qp_index_offset;
        public boolean[] pic_scaling_list_present_flag;
        public boolean isTransform_8x8_mode_flag() {
            return transform_8x8_mode_flag;
        }
        public ScalingMatrix getScalindMatrix() {
            return scalindMatrix;
        }
        public int getSecond_chroma_qp_index_offset() {
            return second_chroma_qp_index_offset;
        }
        public boolean[] getPic_scaling_list_present_flag() {
            return pic_scaling_list_present_flag;
        }
    }

    public boolean entropy_coding_mode_flag;
    public int num_ref_idx_active_minus1[] = new int[2];
    public int slice_group_change_rate_minus1;
    public int pic_parameter_set_id;
    public int seq_parameter_set_id;
    public boolean pic_order_present_flag;
    public int num_slice_groups_minus1;
    public int slice_group_map_type;
    public boolean weighted_pred_flag;
    public int weighted_bipred_idc;
    public int pic_init_qp_minus26;
    public int pic_init_qs_minus26;
    public int chroma_qp_index_offset;
    public boolean deblocking_filter_control_present_flag;
    public boolean constrained_intra_pred_flag;
    public boolean redundant_pic_cnt_present_flag;
    public int[] top_left;
    public int[] bottom_right;
    public int[] run_length_minus1;
    public boolean slice_group_change_direction_flag;
    public int[] slice_group_id;
    public PPSExt extended;

    public static PictureParameterSet read(ByteBuffer is) {
        BitReader in = new BitReader(is);
        PictureParameterSet pps = new PictureParameterSet();

        pps.pic_parameter_set_id = readUE(in, "PPS: pic_parameter_set_id");
        pps.seq_parameter_set_id = readUE(in, "PPS: seq_parameter_set_id");
        pps.entropy_coding_mode_flag = readBool(in, "PPS: entropy_coding_mode_flag");
        pps.pic_order_present_flag = readBool(in, "PPS: pic_order_present_flag");
        pps.num_slice_groups_minus1 = readUE(in, "PPS: num_slice_groups_minus1");
        if (pps.num_slice_groups_minus1 > 0) {
            pps.slice_group_map_type = readUE(in, "PPS: slice_group_map_type");
            pps.top_left = new int[pps.num_slice_groups_minus1 + 1];
            pps.bottom_right = new int[pps.num_slice_groups_minus1 + 1];
            pps.run_length_minus1 = new int[pps.num_slice_groups_minus1 + 1];
            if (pps.slice_group_map_type == 0)
                for (int iGroup = 0; iGroup <= pps.num_slice_groups_minus1; iGroup++)
                    pps.run_length_minus1[iGroup] = readUE(in, "PPS: run_length_minus1");
            else if (pps.slice_group_map_type == 2)
                for (int iGroup = 0; iGroup < pps.num_slice_groups_minus1; iGroup++) {
                    pps.top_left[iGroup] = readUE(in, "PPS: top_left");
                    pps.bottom_right[iGroup] = readUE(in, "PPS: bottom_right");
                }
            else if (pps.slice_group_map_type == 3 || pps.slice_group_map_type == 4 || pps.slice_group_map_type == 5) {
                pps.slice_group_change_direction_flag = readBool(in, "PPS: slice_group_change_direction_flag");
                pps.slice_group_change_rate_minus1 = readUE(in, "PPS: slice_group_change_rate_minus1");
            } else if (pps.slice_group_map_type == 6) {
                int NumberBitsPerSliceGroupId;
                if (pps.num_slice_groups_minus1 + 1 > 4)
                    NumberBitsPerSliceGroupId = 3;
                else if (pps.num_slice_groups_minus1 + 1 > 2)
                    NumberBitsPerSliceGroupId = 2;
                else
                    NumberBitsPerSliceGroupId = 1;
                int pic_size_in_map_units_minus1 = readUE(in, "PPS: pic_size_in_map_units_minus1");
                pps.slice_group_id = new int[pic_size_in_map_units_minus1 + 1];
                for (int i = 0; i <= pic_size_in_map_units_minus1; i++) {
                    pps.slice_group_id[i] = readU(in, NumberBitsPerSliceGroupId, "PPS: slice_group_id [" + i + "]f");
                }
            }
        }
        pps.num_ref_idx_active_minus1 = new int[] {readUE(in, "PPS: num_ref_idx_l0_active_minus1"), readUE(in, "PPS: num_ref_idx_l1_active_minus1")};
        pps.weighted_pred_flag = readBool(in, "PPS: weighted_pred_flag");
        pps.weighted_bipred_idc = readNBit(in, 2, "PPS: weighted_bipred_idc");
        pps.pic_init_qp_minus26 = readSE(in, "PPS: pic_init_qp_minus26");
        pps.pic_init_qs_minus26 = readSE(in, "PPS: pic_init_qs_minus26");
        pps.chroma_qp_index_offset = readSE(in, "PPS: chroma_qp_index_offset");
        pps.deblocking_filter_control_present_flag = readBool(in, "PPS: deblocking_filter_control_present_flag");
        pps.constrained_intra_pred_flag = readBool(in, "PPS: constrained_intra_pred_flag");
        pps.redundant_pic_cnt_present_flag = readBool(in, "PPS: redundant_pic_cnt_present_flag");
        if (moreRBSPData(in)) {
            pps.extended = new PictureParameterSet.PPSExt();
            pps.extended.transform_8x8_mode_flag = readBool(in, "PPS: transform_8x8_mode_flag");
            boolean pic_scaling_matrix_present_flag = readBool(in, "PPS: pic_scaling_matrix_present_flag");
            if (pic_scaling_matrix_present_flag) {
                for (int i = 0; i < 6 + 2 * (pps.extended.transform_8x8_mode_flag ? 1 : 0); i++) {
                    boolean pic_scaling_list_present_flag = readBool(in, "PPS: pic_scaling_list_present_flag");
                    if (pic_scaling_list_present_flag) {
                        pps.extended.scalindMatrix.ScalingList4x4 = new ScalingList[8];
                        pps.extended.scalindMatrix.ScalingList8x8 = new ScalingList[8];
                        if (i < 6) {<commit_msg>Fix nullPoinerException in scalindMatrix  object<commit_after>package org.jcodec.codecs.h264.io.model;

import static org.jcodec.codecs.h264.decode.CAVLCReader.moreRBSPData;
import static org.jcodec.codecs.h264.decode.CAVLCReader.readBool;
import static org.jcodec.codecs.h264.decode.CAVLCReader.readNBit;
import static org.jcodec.codecs.h264.decode.CAVLCReader.readSE;
import static org.jcodec.codecs.h264.decode.CAVLCReader.readU;
import static org.jcodec.codecs.h264.decode.CAVLCReader.readUE;
import static org.jcodec.codecs.h264.io.write.CAVLCWriter.writeBool;
import static org.jcodec.codecs.h264.io.write.CAVLCWriter.writeNBit;
import static org.jcodec.codecs.h264.io.write.CAVLCWriter.writeSE;
import static org.jcodec.codecs.h264.io.write.CAVLCWriter.writeTrailingBits;
import static org.jcodec.codecs.h264.io.write.CAVLCWriter.writeU;
import static org.jcodec.codecs.h264.io.write.CAVLCWriter.writeUE;

import java.nio.ByteBuffer;
import java.util.Arrays;

import org.jcodec.common.io.BitReader;
import org.jcodec.common.io.BitWriter;

/**
 * This class is part of JCodec ( www.jcodec.org ) This software is distributed
 * under FreeBSD License
 * 
 * Picture Parameter Set entity of H264 bitstream
 * 
 * capable to serialize / deserialize with CAVLC bitstream
 * 
 * @author The JCodec project
 * 
 */
public class PictureParameterSet {

    public static class PPSExt {
        public boolean transform_8x8_mode_flag;
        public ScalingMatrix scalindMatrix;
        public int second_chroma_qp_index_offset;
        public boolean[] pic_scaling_list_present_flag;
        public boolean isTransform_8x8_mode_flag() {
            return transform_8x8_mode_flag;
        }
        public ScalingMatrix getScalindMatrix() {
            return scalindMatrix;
        }
        public int getSecond_chroma_qp_index_offset() {
            return second_chroma_qp_index_offset;
        }
        public boolean[] getPic_scaling_list_present_flag() {
            return pic_scaling_list_present_flag;
        }
    }

    public boolean entropy_coding_mode_flag;
    public int num_ref_idx_active_minus1[] = new int[2];
    public int slice_group_change_rate_minus1;
    public int pic_parameter_set_id;
    public int seq_parameter_set_id;
    public boolean pic_order_present_flag;
    public int num_slice_groups_minus1;
    public int slice_group_map_type;
    public boolean weighted_pred_flag;
    public int weighted_bipred_idc;
    public int pic_init_qp_minus26;
    public int pic_init_qs_minus26;
    public int chroma_qp_index_offset;
    public boolean deblocking_filter_control_present_flag;
    public boolean constrained_intra_pred_flag;
    public boolean redundant_pic_cnt_present_flag;
    public int[] top_left;
    public int[] bottom_right;
    public int[] run_length_minus1;
    public boolean slice_group_change_direction_flag;
    public int[] slice_group_id;
    public PPSExt extended;

    public static PictureParameterSet read(ByteBuffer is) {
        BitReader in = new BitReader(is);
        PictureParameterSet pps = new PictureParameterSet();

        pps.pic_parameter_set_id = readUE(in, "PPS: pic_parameter_set_id");
        pps.seq_parameter_set_id = readUE(in, "PPS: seq_parameter_set_id");
        pps.entropy_coding_mode_flag = readBool(in, "PPS: entropy_coding_mode_flag");
        pps.pic_order_present_flag = readBool(in, "PPS: pic_order_present_flag");
        pps.num_slice_groups_minus1 = readUE(in, "PPS: num_slice_groups_minus1");
        if (pps.num_slice_groups_minus1 > 0) {
            pps.slice_group_map_type = readUE(in, "PPS: slice_group_map_type");
            pps.top_left = new int[pps.num_slice_groups_minus1 + 1];
            pps.bottom_right = new int[pps.num_slice_groups_minus1 + 1];
            pps.run_length_minus1 = new int[pps.num_slice_groups_minus1 + 1];
            if (pps.slice_group_map_type == 0)
                for (int iGroup = 0; iGroup <= pps.num_slice_groups_minus1; iGroup++)
                    pps.run_length_minus1[iGroup] = readUE(in, "PPS: run_length_minus1");
            else if (pps.slice_group_map_type == 2)
                for (int iGroup = 0; iGroup < pps.num_slice_groups_minus1; iGroup++) {
                    pps.top_left[iGroup] = readUE(in, "PPS: top_left");
                    pps.bottom_right[iGroup] = readUE(in, "PPS: bottom_right");
                }
            else if (pps.slice_group_map_type == 3 || pps.slice_group_map_type == 4 || pps.slice_group_map_type == 5) {
                pps.slice_group_change_direction_flag = readBool(in, "PPS: slice_group_change_direction_flag");
                pps.slice_group_change_rate_minus1 = readUE(in, "PPS: slice_group_change_rate_minus1");
            } else if (pps.slice_group_map_type == 6) {
                int NumberBitsPerSliceGroupId;
                if (pps.num_slice_groups_minus1 + 1 > 4)
                    NumberBitsPerSliceGroupId = 3;
                else if (pps.num_slice_groups_minus1 + 1 > 2)
                    NumberBitsPerSliceGroupId = 2;
                else
                    NumberBitsPerSliceGroupId = 1;
                int pic_size_in_map_units_minus1 = readUE(in, "PPS: pic_size_in_map_units_minus1");
                pps.slice_group_id = new int[pic_size_in_map_units_minus1 + 1];
                for (int i = 0; i <= pic_size_in_map_units_minus1; i++) {
                    pps.slice_group_id[i] = readU(in, NumberBitsPerSliceGroupId, "PPS: slice_group_id [" + i + "]f");
                }
            }
        }
        pps.num_ref_idx_active_minus1 = new int[] {readUE(in, "PPS: num_ref_idx_l0_active_minus1"), readUE(in, "PPS: num_ref_idx_l1_active_minus1")};
        pps.weighted_pred_flag = readBool(in, "PPS: weighted_pred_flag");
        pps.weighted_bipred_idc = readNBit(in, 2, "PPS: weighted_bipred_idc");
        pps.pic_init_qp_minus26 = readSE(in, "PPS: pic_init_qp_minus26");
        pps.pic_init_qs_minus26 = readSE(in, "PPS: pic_init_qs_minus26");
        pps.chroma_qp_index_offset = readSE(in, "PPS: chroma_qp_index_offset");
        pps.deblocking_filter_control_present_flag = readBool(in, "PPS: deblocking_filter_control_present_flag");
        pps.constrained_intra_pred_flag = readBool(in, "PPS: constrained_intra_pred_flag");
        pps.redundant_pic_cnt_present_flag = readBool(in, "PPS: redundant_pic_cnt_present_flag");
        if (moreRBSPData(in)) {
            pps.extended = new PictureParameterSet.PPSExt();
            pps.extended.transform_8x8_mode_flag = readBool(in, "PPS: transform_8x8_mode_flag");
            boolean pic_scaling_matrix_present_flag = readBool(in, "PPS: pic_scaling_matrix_present_flag");
            if (pic_scaling_matrix_present_flag) {
                for (int i = 0; i < 6 + 2 * (pps.extended.transform_8x8_mode_flag ? 1 : 0); i++) {
                    boolean pic_scaling_list_present_flag = readBool(in, "PPS: pic_scaling_list_present_flag");
                    if (pic_scaling_list_present_flag) {
                        pps.extended.scalindMatrix = new ScalingMatrix();
                        pps.extended.scalindMatrix.ScalingList4x4 = new ScalingList[8];
                        pps.extended.scalindMatrix.ScalingList8x8 = new ScalingList[8];
                        if (i < 6) {
{'fraction_non_alphanumeric': 0.08793498645551157, 'fraction_numerical': 0.017920400083350697, 'mean_word_length': 3.123138602520046, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6988765', 'n_tokens_mistral': 5050, 'n_tokens_neox': 4711, 'n_words': 839}