def _processTSPacket(self, packet): self.packetsCount = self.packetsCount + 1 packetParser = BitReader(packet) packetParser.skipBits(1) payload_unit_start_indicator = (packetParser.readBits(1) != 0) packetParser.skipBits(1) pid = packetParser.readBits(13) adaptation_field = (packetParser.readUnsignedByte() & 0x30) >> 4 if (adaptation_field > 1): length = packetParser.readUnsignedByte(); if (length > 0): packetParser.skipBytes(length) if (adaptation_field == 1 or adaptation_field == 3): if (pid == 0): self._parseProgramId(payload_unit_start_indicator, packetParser) elif (pid == self.pmtId): self._parseProgramTable(payload_unit_start_indicator, packetParser) else: track = self.tracks[pid] if(track is not None): track.appendData(payload_unit_start_indicator, packetParser)
def _processTSPacket(self, packet): self.packetsCount = self.packetsCount + 1 packetParser = BitReader(packet) packetParser.skipBits(1) payload_unit_start_indicator = (packetParser.readBits(1) != 0) packetParser.skipBits(1) pid = packetParser.readBits(13) adaptation_field = (packetParser.readUnsignedByte() & 0x30) >> 4 if (adaptation_field > 1): length = packetParser.readUnsignedByte() if (length > 0): packetParser.skipBytes(length) if (adaptation_field == 1 or adaptation_field == 3): if (pid == 0): self._parseProgramId(payload_unit_start_indicator, packetParser) elif (pid == self.pmtId): self._parseProgramTable(payload_unit_start_indicator, packetParser) else: track = self.tracks[pid] if (track is not None): track.appendData(payload_unit_start_indicator, packetParser)
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)
def _parseSEINALUnit(self, start, limit): seiParser = BitReader(self.dataBuffer[start:limit]) seiParser.skipBytes(4) while True: data = seiParser.readUnsignedByte() if (data != 0xFF): break # Parse payload size while True: data = seiParser.readUnsignedByte() if (data != 0xFF): break
def _parseSEINALUnit(self, start, limit): seiParser = BitReader(self.dataBuffer[start:limit]) seiParser.skipBytes(4) # Parse payload type payloadType = 0 while True: data = seiParser.readUnsignedByte() payloadType += data if (data != 0xFF): break; # Parse payload size payloadSize = 0 while True: data = seiParser.readUnsignedByte() payloadSize += data if (data != 0xFF): break;
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)
def _parseAUDNALUnit(self, start, limit): audParser = BitReader(self.dataBuffer[start:limit]) audParser.skipBytes(4)
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)