Class NalUnitUtil


  • public final class NalUnitUtil
    extends Object
    Utility methods for handling H.264/AVC and H.265/HEVC NAL units.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  NalUnitUtil.H265SpsData
      Holds data parsed from a H.265 sequence parameter set NAL unit.
      static class  NalUnitUtil.PpsData
      Holds data parsed from a picture parameter set NAL unit.
      static class  NalUnitUtil.SpsData
      Holds data parsed from a H.264 sequence parameter set NAL unit.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static float[] ASPECT_RATIO_IDC_VALUES
      Aspect ratios indexed by aspect_ratio_idc, in H.264 and H.265 SPSs.
      static int EXTENDED_SAR
      Value for aspect_ratio_idc indicating an extended aspect ratio, in H.264 and H.265 SPSs.
      static byte[] NAL_START_CODE
      Four initial bytes that must prefix NAL units for decoding.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void clearPrefixFlags​(boolean[] prefixFlags)
      Clears prefix flags, as used by findNalUnit(byte[], int, int, boolean[]).
      static void discardToSps​(ByteBuffer data)
      Discards data from the buffer up to the first SPS, where data.position() is interpreted as the length of the buffer.
      static int findNalUnit​(byte[] data, int startOffset, int endOffset, boolean[] prefixFlags)
      Finds the first NAL unit in data.
      static int getH265NalUnitType​(byte[] data, int offset)
      Returns the type of the H.265 NAL unit in data that starts at offset.
      static int getNalUnitType​(byte[] data, int offset)
      Returns the type of the NAL unit in data that starts at offset.
      static boolean isNalUnitSei​(String mimeType, byte nalUnitHeaderFirstByte)
      Returns whether the NAL unit with the specified header contains supplemental enhancement information.
      static NalUnitUtil.H265SpsData parseH265SpsNalUnit​(byte[] nalData, int nalOffset, int nalLimit)
      Parses a H.265 SPS NAL unit using the syntax defined in ITU-T Recommendation H.265 (2019) subsection 7.3.2.2.1.
      static NalUnitUtil.H265SpsData parseH265SpsNalUnitPayload​(byte[] nalData, int nalOffset, int nalLimit)
      Parses a H.265 SPS NAL unit payload (excluding the NAL unit header) using the syntax defined in ITU-T Recommendation H.265 (2019) subsection 7.3.2.2.1.
      static NalUnitUtil.PpsData parsePpsNalUnit​(byte[] nalData, int nalOffset, int nalLimit)
      Parses a PPS NAL unit using the syntax defined in ITU-T Recommendation H.264 (2013) subsection 7.3.2.2.
      static NalUnitUtil.PpsData parsePpsNalUnitPayload​(byte[] nalData, int nalOffset, int nalLimit)
      Parses a PPS NAL unit payload (excluding the NAL unit header) using the syntax defined in ITU-T Recommendation H.264 (2013) subsection 7.3.2.2.
      static NalUnitUtil.SpsData parseSpsNalUnit​(byte[] nalData, int nalOffset, int nalLimit)
      Parses a SPS NAL unit using the syntax defined in ITU-T Recommendation H.264 (2013) subsection 7.3.2.1.1.
      static NalUnitUtil.SpsData parseSpsNalUnitPayload​(byte[] nalData, int nalOffset, int nalLimit)
      Parses a SPS NAL unit payload (excluding the NAL unit header) using the syntax defined in ITU-T Recommendation H.264 (2013) subsection 7.3.2.1.1.
      static int unescapeStream​(byte[] data, int limit)
      Unescapes data up to the specified limit, replacing occurrences of [0, 0, 3] with [0, 0].
    • Field Detail

      • NAL_START_CODE

        public static final byte[] NAL_START_CODE
        Four initial bytes that must prefix NAL units for decoding.
      • EXTENDED_SAR

        public static final int EXTENDED_SAR
        Value for aspect_ratio_idc indicating an extended aspect ratio, in H.264 and H.265 SPSs.
        See Also:
        Constant Field Values
      • ASPECT_RATIO_IDC_VALUES

        public static final float[] ASPECT_RATIO_IDC_VALUES
        Aspect ratios indexed by aspect_ratio_idc, in H.264 and H.265 SPSs.
    • Method Detail

      • unescapeStream

        public static int unescapeStream​(byte[] data,
                                         int limit)
        Unescapes data up to the specified limit, replacing occurrences of [0, 0, 3] with [0, 0]. The unescaped data is returned in-place, with the return value indicating its length.

        Executions of this method are mutually exclusive, so it should not be called with very large buffers.

        Parameters:
        data - The data to unescape.
        limit - The limit (exclusive) of the data to unescape.
        Returns:
        The length of the unescaped data.
      • discardToSps

        public static void discardToSps​(ByteBuffer data)
        Discards data from the buffer up to the first SPS, where data.position() is interpreted as the length of the buffer.

        When the method returns, data.position() will contain the new length of the buffer. If the buffer is not empty it is guaranteed to start with an SPS.

        Parameters:
        data - Buffer containing start code delimited NAL units.
      • isNalUnitSei

        public static boolean isNalUnitSei​(@Nullable
                                           String mimeType,
                                           byte nalUnitHeaderFirstByte)
        Returns whether the NAL unit with the specified header contains supplemental enhancement information.
        Parameters:
        mimeType - The sample MIME type, or null if unknown.
        nalUnitHeaderFirstByte - The first byte of nal_unit().
        Returns:
        Whether the NAL unit with the specified header is an SEI NAL unit. False is returned if the MimeType is null.
      • getNalUnitType

        public static int getNalUnitType​(byte[] data,
                                         int offset)
        Returns the type of the NAL unit in data that starts at offset.
        Parameters:
        data - The data to search.
        offset - The start offset of a NAL unit. Must lie between -3 (inclusive) and data.length - 3 (exclusive).
        Returns:
        The type of the unit.
      • getH265NalUnitType

        public static int getH265NalUnitType​(byte[] data,
                                             int offset)
        Returns the type of the H.265 NAL unit in data that starts at offset.
        Parameters:
        data - The data to search.
        offset - The start offset of a NAL unit. Must lie between -3 (inclusive) and data.length - 3 (exclusive).
        Returns:
        The type of the unit.
      • parseSpsNalUnit

        public static NalUnitUtil.SpsData parseSpsNalUnit​(byte[] nalData,
                                                          int nalOffset,
                                                          int nalLimit)
        Parses a SPS NAL unit using the syntax defined in ITU-T Recommendation H.264 (2013) subsection 7.3.2.1.1.
        Parameters:
        nalData - A buffer containing escaped SPS data.
        nalOffset - The offset of the NAL unit header in nalData.
        nalLimit - The limit of the NAL unit in nalData.
        Returns:
        A parsed representation of the SPS data.
      • parseSpsNalUnitPayload

        public static NalUnitUtil.SpsData parseSpsNalUnitPayload​(byte[] nalData,
                                                                 int nalOffset,
                                                                 int nalLimit)
        Parses a SPS NAL unit payload (excluding the NAL unit header) using the syntax defined in ITU-T Recommendation H.264 (2013) subsection 7.3.2.1.1.
        Parameters:
        nalData - A buffer containing escaped SPS data.
        nalOffset - The offset of the NAL unit payload in nalData.
        nalLimit - The limit of the NAL unit in nalData.
        Returns:
        A parsed representation of the SPS data.
      • parseH265SpsNalUnit

        public static NalUnitUtil.H265SpsData parseH265SpsNalUnit​(byte[] nalData,
                                                                  int nalOffset,
                                                                  int nalLimit)
        Parses a H.265 SPS NAL unit using the syntax defined in ITU-T Recommendation H.265 (2019) subsection 7.3.2.2.1.
        Parameters:
        nalData - A buffer containing escaped SPS data.
        nalOffset - The offset of the NAL unit header in nalData.
        nalLimit - The limit of the NAL unit in nalData.
        Returns:
        A parsed representation of the SPS data.
      • parseH265SpsNalUnitPayload

        public static NalUnitUtil.H265SpsData parseH265SpsNalUnitPayload​(byte[] nalData,
                                                                         int nalOffset,
                                                                         int nalLimit)
        Parses a H.265 SPS NAL unit payload (excluding the NAL unit header) using the syntax defined in ITU-T Recommendation H.265 (2019) subsection 7.3.2.2.1.
        Parameters:
        nalData - A buffer containing escaped SPS data.
        nalOffset - The offset of the NAL unit payload in nalData.
        nalLimit - The limit of the NAL unit in nalData.
        Returns:
        A parsed representation of the SPS data.
      • parsePpsNalUnit

        public static NalUnitUtil.PpsData parsePpsNalUnit​(byte[] nalData,
                                                          int nalOffset,
                                                          int nalLimit)
        Parses a PPS NAL unit using the syntax defined in ITU-T Recommendation H.264 (2013) subsection 7.3.2.2.
        Parameters:
        nalData - A buffer containing escaped PPS data.
        nalOffset - The offset of the NAL unit header in nalData.
        nalLimit - The limit of the NAL unit in nalData.
        Returns:
        A parsed representation of the PPS data.
      • parsePpsNalUnitPayload

        public static NalUnitUtil.PpsData parsePpsNalUnitPayload​(byte[] nalData,
                                                                 int nalOffset,
                                                                 int nalLimit)
        Parses a PPS NAL unit payload (excluding the NAL unit header) using the syntax defined in ITU-T Recommendation H.264 (2013) subsection 7.3.2.2.
        Parameters:
        nalData - A buffer containing escaped PPS data.
        nalOffset - The offset of the NAL unit payload in nalData.
        nalLimit - The limit of the NAL unit in nalData.
        Returns:
        A parsed representation of the PPS data.
      • findNalUnit

        public static int findNalUnit​(byte[] data,
                                      int startOffset,
                                      int endOffset,
                                      boolean[] prefixFlags)
        Finds the first NAL unit in data.

        If prefixFlags is null then the first three bytes of a NAL unit must be entirely contained within the part of the array being searched in order for it to be found.

        When prefixFlags is non-null, this method supports finding NAL units whose first four bytes span data arrays passed to successive calls. To use this feature, pass the same prefixFlags parameter to successive calls. State maintained in this parameter enables the detection of such NAL units. Note that when using this feature, the return value may be 3, 2 or 1 less than startOffset, to indicate a NAL unit starting 3, 2 or 1 bytes before the first byte in the current array.

        Parameters:
        data - The data to search.
        startOffset - The offset (inclusive) in the data to start the search.
        endOffset - The offset (exclusive) in the data to end the search.
        prefixFlags - A boolean array whose first three elements are used to store the state required to detect NAL units where the NAL unit prefix spans array boundaries. The array must be at least 3 elements long.
        Returns:
        The offset of the NAL unit, or endOffset if a NAL unit was not found.
      • clearPrefixFlags

        public static void clearPrefixFlags​(boolean[] prefixFlags)
        Clears prefix flags, as used by findNalUnit(byte[], int, int, boolean[]).
        Parameters:
        prefixFlags - The flags to clear.