import { BufferReader } from './bufferreader' ; export class SPSParser { public reader : BufferReader ; constructor ( buffer ) { this . reader = new BufferReader ( buffer ) ; } parse ( ) { this . reader . readNext ( ) ; const profile = this . reader . readNext ( ) ; this . reader . readNext ( ) ; const level = this . reader . readNext ( ) ; this . reader . readUnsignedExpGolomb ( ) ; if ( [ 100 , 110 , 122 , 244 , 44 , 83 , 86 , 118 ] . includes ( profile ) ) { const chromaFormat = this . reader . readUnsignedExpGolomb ( ) ; if ( chromaFormat === 3 ) { this . reader . readBits ( 1 ) ; } this . reader . readUnsignedExpGolomb ( ) ; this . reader . readUnsignedExpGolomb ( ) ; this . reader . readBits ( 1 ) ; const seqScalingMatrix = this . reader . readBits ( 1 ) ; if ( seqScalingMatrix ) { for ( let k = 0 ; k < ( chromaFormat !== 3 ? 8 : 12 ) ; k ++ ) { this . reader . readBits ( 1 ) ; } } } this . reader . readUnsignedExpGolomb ( ) ; const picOrderCntType = this . reader . readUnsignedExpGolomb ( ) ; if ( picOrderCntType === 0 ) { this . reader . readUnsignedExpGolomb ( ) ; } else if ( picOrderCntType === 1 ) { let numRefFramesInPic = 0 ; this . reader . readBits ( 1 ) ; this . reader . readSignedExpGolomb ( ) ; this . reader . readSignedExpGolomb ( ) ; numRefFramesInPic = this . reader . readUnsignedExpGolomb ( ) ; for ( let i = 0 ; i < numRefFramesInPic ; i ++ ) { this . reader . readSignedExpGolomb ( ) ; } } this . reader . readUnsignedExpGolomb ( ) ; this . reader . readBits ( 1 ) ; const picWidthInMbsMinus1 = this . reader . readUnsignedExpGolomb ( ) ; const picHeightInMapUnitsMinus1 = this . reader . readUnsignedExpGolomb ( ) ; const picFrameMbsOnlyFlag = this . reader . readBits ( 1 ) ; this . reader . readBits ( 1 ) ; const frameCroppingFlag = this . reader . readBits ( 1 ) ; const frameCropLeftOffset = frameCroppingFlag ? this . reader . readUnsignedExpGolomb ( ) : 0 ; const frameCropRightOffset = frameCroppingFlag ? this . reader . readUnsignedExpGolomb ( ) : 0 ; const frameCropTopOffset = frameCroppingFlag ? this . reader . readUnsignedExpGolomb ( ) : 0 ; const frameCropBottomOffset = frameCroppingFlag ? this . reader . readUnsignedExpGolomb ( ) : 0 ; const w = ( picWidthInMbsMinus1 + 1 ) * 16 - frameCropLeftOffset * 2 - frameCropRightOffset * 2 ; const h = ( 2 - picFrameMbsOnlyFlag ) * ( picHeightInMapUnitsMinus1 + 1 ) * 16 - frameCropTopOffset * 2 - frameCropBottomOffset * 2 ; return { profile : profile , level : level / 10.0 , width : w , height : h , } ; } }