Exemple #1
0
    def _parseSliceNALUnit(self, start, limit):
        sliceParser = BitReader(self.dataBuffer[start:limit])
        sliceParser.skipBytes(4)
        sliceParser.readUnsignedExpGolombCodedInt()
        sliceType = sliceParser.readUnsignedExpGolombCodedInt()

        #print "{} Frame - Time: {}".format(self._getSliceTypeName(sliceType), self.timeUs)
        self._addNewFrame(sliceType, self.timeUs)
Exemple #2
0
    def _parseSliceNALUnit(self, start, limit):
        sliceParser = BitReader(self.dataBuffer[start:limit])
        sliceParser.skipBytes(4)
        sliceParser.readUnsignedExpGolombCodedInt()
        sliceType = sliceParser.readUnsignedExpGolombCodedInt()

        #print "{} Frame - Time: {}".format(self._getSliceTypeName(sliceType), self.timeUs)
        self._addNewFrame(sliceType, self.timeUs)
Exemple #3
0
    def _parseSPSNALUnit(self, start, limit):
        spsParser = BitReader(self.dataBuffer[start:limit])
        spsParser.skipBytes(4)

        self.profileId = spsParser.readBits(8)
        self.levelId = spsParser.readBits(8)
        spsParser.skipBytes(1)

        spsParser.readUnsignedExpGolombCodedInt()

        chromaFormatIdc = 1  # default is 4:2:0
        if (self.profileId == 100 or self.profileId == 110
                or self.profileId == 122 or self.profileId == 244
                or self.profileId == 44 or self.profileId == 83
                or self.profileId == 86 or self.profileId == 118
                or self.profileId == 128 or self.profileId == 138):
            chromaFormatIdc = spsParser.readUnsignedExpGolombCodedInt()
            if (chromaFormatIdc == 3):
                spsParser.skipBits(1)

            spsParser.readUnsignedExpGolombCodedInt()
            # bit_depth_luma_minus8
            spsParser.readUnsignedExpGolombCodedInt()
            # bit_depth_chroma_minus8
            spsParser.skipBits(1)
            # qpprime_y_zero_transform_bypass_flag
            seqScalingMatrixPresentFlag = spsParser.readBit()

            if (seqScalingMatrixPresentFlag == 1):
                limit = 12
                if (chromaFormatIdc != 3):
                    limit = 8

                for i in range(0, limit):
                    seqScalingListPresentFlag = spsParser.readBit()
                    if (seqScalingListPresentFlag == 1):
                        if (i < 6):
                            self._skipScalingList(spsParser, 16)
                        else:
                            self._skipScalingList(spsParser, 64)

        spsParser.readUnsignedExpGolombCodedInt()
        # log2_max_frame_num_minus4
        picOrderCntType = spsParser.readUnsignedExpGolombCodedInt()
        if (picOrderCntType == 0):
            spsParser.readUnsignedExpGolombCodedInt()
            # log2_max_pic_order_cnt_lsb_minus4
        elif (picOrderCntType == 1):
            spsParser.skipBits(1)
            # delta_pic_order_always_zero_flag
            spsParser.readSignedExpGolombCodedInt()
            # offset_for_non_ref_pic
            spsParser.readSignedExpGolombCodedInt()
            # offset_for_top_to_bottom_field

            numRefFramesInPicOrderCntCycle = spsParser.readUnsignedExpGolombCodedInt(
            )

            for i in range(0, numRefFramesInPicOrderCntCycle):
                spsParser.readSignedExpGolombCodedInt()
                #offset_for_ref_frame[i]

        self.numRefFrames = spsParser.readUnsignedExpGolombCodedInt()
        # max_num_ref_frames
        spsParser.skipBits(1)
        # gaps_in_frame_num_value_allowed_flag

        picWidthInMbs = spsParser.readUnsignedExpGolombCodedInt() + 1
        picHeightInMapUnits = spsParser.readUnsignedExpGolombCodedInt() + 1
        frameMbsOnlyFlag = spsParser.readBit()

        frameHeightInMbs = picHeightInMapUnits
        if (frameMbsOnlyFlag == 0):
            frameHeightInMbs += picHeightInMapUnits
            spsParser.skipBits(1)  # mb_adaptive_frame_field_flag

        spsParser.skipBits(1)
        # direct_8x8_inference_flag
        self.frameWidth = picWidthInMbs * 16
        self.frameHeight = frameHeightInMbs * 16

        frameCroppingFlag = spsParser.readBit()

        if (frameCroppingFlag == 1):
            frameCropLeftOffset = spsParser.readUnsignedExpGolombCodedInt()
            frameCropRightOffset = spsParser.readUnsignedExpGolombCodedInt()
            frameCropTopOffset = spsParser.readUnsignedExpGolombCodedInt()
            frameCropBottomOffset = spsParser.readUnsignedExpGolombCodedInt()
            cropUnitX, cropUnitY = 0, 0

            if (chromaFormatIdc == 0):
                cropUnitX = 1
                cropUnitY = 2 - (1 if (frameMbsOnlyFlag == 1) else 0)
            else:
                subWidthC = (1 if (chromaFormatIdc == 3) else 2)
                subHeightC = (2 if (chromaFormatIdc == 1) else 1)
                cropUnitX = subWidthC
                cropUnitY = subHeightC * (2 - (1 if
                                               (frameMbsOnlyFlag == 1) else 0))

            self.frameWidth -= (frameCropLeftOffset +
                                frameCropRightOffset) * cropUnitX
            self.frameHeight -= (frameCropTopOffset +
                                 frameCropBottomOffset) * cropUnitY

        vui_parameters_present_flag = spsParser.readBit()
        if (vui_parameters_present_flag == 1):
            aspect_ratio_info_present_flag = spsParser.readBit()
            if (aspect_ratio_info_present_flag == 1):
                aspect_ratio_idc = spsParser.readUnsignedByte()
                if (aspect_ratio_idc == self.H264_ASPECT_RATIO_EXTENDED_SAR):
                    self.aspectRatioNum = spsParser.readBits(16)
                    self.aspectRatioDen = spsParser.readBits(16)
                else:
                    self.aspectRatioNum = self.H264_ASPECT_RATIO_PIXEL_ASPECT[
                        aspect_ratio_idc][0]
                    self.aspectRatioDen = self.H264_ASPECT_RATIO_PIXEL_ASPECT[
                        aspect_ratio_idc][1]

        if (self.aspectRatioNum != 0):
            self.displayAspectRatio = Fraction(
                self.frameWidth * self.aspectRatioNum,
                self.frameHeight * self.aspectRatioDen)
Exemple #4
0
    def _parseSPSNALUnit(self, start, limit):
        spsParser = BitReader(self.dataBuffer[start:limit])
        spsParser.skipBytes(4)

        self.profileId = spsParser.readBits(8)
        self.levelId = spsParser.readBits(8)
        spsParser.skipBytes(1)

        spsParser.readUnsignedExpGolombCodedInt()

        chromaFormatIdc = 1 # default is 4:2:0
        if(self.profileId == 100 or self.profileId == 110 or self.profileId == 122
                or self.profileId == 244 or self.profileId == 44 or self.profileId == 83
                or self.profileId == 86 or self.profileId == 118 or self.profileId == 128
                or self.profileId == 138):
            chromaFormatIdc = spsParser.readUnsignedExpGolombCodedInt()
            if(chromaFormatIdc == 3):
                spsParser.skipBits(1)

            spsParser.readUnsignedExpGolombCodedInt(); # bit_depth_luma_minus8
            spsParser.readUnsignedExpGolombCodedInt(); # bit_depth_chroma_minus8
            spsParser.skipBits(1); # qpprime_y_zero_transform_bypass_flag
            seqScalingMatrixPresentFlag = spsParser.readBit();

            if(seqScalingMatrixPresentFlag == 1):
                limit = 12
                if(chromaFormatIdc != 3):
                    limit = 8

                for i in range(0, limit):
                    seqScalingListPresentFlag = spsParser.readBit();
                    if(seqScalingListPresentFlag == 1):
                        if(i < 6):
                            self._skipScalingList(spsParser, 16)
                        else:
                            self._skipScalingList(spsParser, 64)


        spsParser.readUnsignedExpGolombCodedInt(); # log2_max_frame_num_minus4
        picOrderCntType = spsParser.readUnsignedExpGolombCodedInt();
        if(picOrderCntType == 0):
            spsParser.readUnsignedExpGolombCodedInt(); # log2_max_pic_order_cnt_lsb_minus4
        elif (picOrderCntType == 1):
            spsParser.skipBits(1); # delta_pic_order_always_zero_flag
            spsParser.readSignedExpGolombCodedInt(); # offset_for_non_ref_pic
            spsParser.readSignedExpGolombCodedInt(); # offset_for_top_to_bottom_field

            numRefFramesInPicOrderCntCycle = spsParser.readUnsignedExpGolombCodedInt();

            for i in range(0, numRefFramesInPicOrderCntCycle):
                spsParser.readSignedExpGolombCodedInt(); #offset_for_ref_frame[i]


        self.numRefFrames = spsParser.readUnsignedExpGolombCodedInt(); # max_num_ref_frames
        spsParser.skipBits(1); # gaps_in_frame_num_value_allowed_flag

        picWidthInMbs = spsParser.readUnsignedExpGolombCodedInt() + 1;
        picHeightInMapUnits = spsParser.readUnsignedExpGolombCodedInt() + 1;
        frameMbsOnlyFlag = spsParser.readBit();

        frameHeightInMbs = picHeightInMapUnits
        if(frameMbsOnlyFlag == 0):
            frameHeightInMbs += picHeightInMapUnits
            spsParser.skipBits(1) # mb_adaptive_frame_field_flag


        spsParser.skipBits(1); # direct_8x8_inference_flag
        self.frameWidth = picWidthInMbs * 16;
        self.frameHeight = frameHeightInMbs * 16;

        frameCroppingFlag = spsParser.readBit();

        if (frameCroppingFlag == 1):
            frameCropLeftOffset = spsParser.readUnsignedExpGolombCodedInt();
            frameCropRightOffset = spsParser.readUnsignedExpGolombCodedInt();
            frameCropTopOffset = spsParser.readUnsignedExpGolombCodedInt();
            frameCropBottomOffset = spsParser.readUnsignedExpGolombCodedInt();
            cropUnitX, cropUnitY = 0, 0

            if (chromaFormatIdc == 0):
                cropUnitX = 1;
                cropUnitY = 2 - (1 if (frameMbsOnlyFlag == 1) else 0);
            else:
                subWidthC =  (1 if (chromaFormatIdc == 3) else 2);
                subHeightC = (2 if (chromaFormatIdc == 1) else 1);
                cropUnitX = subWidthC;
                cropUnitY = subHeightC * (2 - (1 if (frameMbsOnlyFlag == 1) else 0));

            self.frameWidth -= (frameCropLeftOffset + frameCropRightOffset) * cropUnitX;
            self.frameHeight -= (frameCropTopOffset + frameCropBottomOffset) * cropUnitY;

        vui_parameters_present_flag = spsParser.readBit()
        if (vui_parameters_present_flag == 1):
            aspect_ratio_info_present_flag = spsParser.readBit()
            if (aspect_ratio_info_present_flag == 1):
                aspect_ratio_idc = spsParser.readUnsignedByte()
                if (aspect_ratio_idc == self.H264_ASPECT_RATIO_EXTENDED_SAR):
                    self.aspectRatioNum = spsParser.readBits(16)
                    self.aspectRatioDen = spsParser.readBits(16)
                else:
                    self.aspectRatioNum = self.H264_ASPECT_RATIO_PIXEL_ASPECT[aspect_ratio_idc][0]
                    self.aspectRatioDen = self.H264_ASPECT_RATIO_PIXEL_ASPECT[aspect_ratio_idc][1]


        if(self.aspectRatioNum != 0):
            self.displayAspectRatio = Fraction(self.frameWidth * self.aspectRatioNum,
                self.frameHeight * self.aspectRatioDen)