|
| 1 | +package com.pedro.common.av1 |
| 2 | + |
| 3 | +import com.pedro.common.BitBuffer |
| 4 | +import com.pedro.common.toByteArray |
| 5 | +import java.nio.ByteBuffer |
| 6 | + |
| 7 | +class Av1SequenceHeaderParser { |
| 8 | + |
| 9 | + var seqProfile = 0 |
| 10 | + var seqLevelIdx = 0 |
| 11 | + var seqTier = false |
| 12 | + var highBitDepth = false |
| 13 | + var twelveBit = false |
| 14 | + var monochrome = false |
| 15 | + var subsamplingX = false |
| 16 | + var subsamplingY = false |
| 17 | + var samplePosition = 0 |
| 18 | + var initialDisplayDelayPresentFlag = false |
| 19 | + var initialPresentationDelay = 0 |
| 20 | + |
| 21 | + |
| 22 | + fun parse(sequenceObu: ByteBuffer) { |
| 23 | + parse(sequenceObu.toByteArray()) |
| 24 | + } |
| 25 | + |
| 26 | + fun parse(sequenceObu: ByteArray) { |
| 27 | + val av1Parser = Av1Parser() |
| 28 | + val obu = av1Parser.getObus(sequenceObu).firstOrNull { |
| 29 | + av1Parser.getObuType(it.header[0]) == ObuType.SEQUENCE_HEADER |
| 30 | + } ?: throw IllegalArgumentException("sequence header obu not found") |
| 31 | + val bitBuffer = BitBuffer(ByteBuffer.wrap(obu.data)) |
| 32 | + |
| 33 | + seqProfile = bitBuffer.getInt(3) |
| 34 | + bitBuffer.skipBool() |
| 35 | + val reducedStillPictureHeader = bitBuffer.getBool() |
| 36 | + initialDisplayDelayPresentFlag = false |
| 37 | + if (reducedStillPictureHeader) { |
| 38 | + seqLevelIdx = bitBuffer.getInt(5) |
| 39 | + } else { |
| 40 | + val timingInfoPresentFlag = bitBuffer.getBool() |
| 41 | + var decoderModelInfoPresentFlag = false |
| 42 | + var bufferDelayLengthMinus1 = 0 |
| 43 | + if (timingInfoPresentFlag) { |
| 44 | + bitBuffer.skip(64) |
| 45 | + val equalPictureInterval = bitBuffer.getBool() |
| 46 | + if (equalPictureInterval) { |
| 47 | + bitBuffer.readUVLC() |
| 48 | + } |
| 49 | + decoderModelInfoPresentFlag = bitBuffer.getBool() |
| 50 | + if (decoderModelInfoPresentFlag) { |
| 51 | + bufferDelayLengthMinus1 = bitBuffer.getInt(5) |
| 52 | + bitBuffer.skip(42) //skip this |
| 53 | + } |
| 54 | + } |
| 55 | + initialDisplayDelayPresentFlag = bitBuffer.getBool() |
| 56 | + val operatingPointsCntMinus1 = bitBuffer.getInt(5) |
| 57 | + for (i in 0..operatingPointsCntMinus1) { |
| 58 | + bitBuffer.skip(12) //skip |
| 59 | + val levelIdx = bitBuffer.getInt(5) |
| 60 | + if (i == 0) seqLevelIdx = levelIdx |
| 61 | + if (levelIdx > 7) { |
| 62 | + val sTier = bitBuffer.getBool() |
| 63 | + if (i == 0) seqTier = sTier |
| 64 | + } |
| 65 | + if (decoderModelInfoPresentFlag) { |
| 66 | + val decoderModelPresentForThisOp = bitBuffer.getBool() |
| 67 | + if (decoderModelPresentForThisOp) { |
| 68 | + val n = bufferDelayLengthMinus1 + 1 |
| 69 | + bitBuffer.skip(n * 2 + 1) //skip this |
| 70 | + } |
| 71 | + } |
| 72 | + if (initialDisplayDelayPresentFlag) { |
| 73 | + val initialDisplayDelayPresentForThisOp = bitBuffer.getBool() |
| 74 | + if (initialDisplayDelayPresentForThisOp) { |
| 75 | + val initialDisplayDelayMinus1 = bitBuffer.getInt(4) |
| 76 | + if (i == 0) initialPresentationDelay = initialDisplayDelayMinus1 |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + val frameWidthBitsMinus1 = bitBuffer.getInt(4) |
| 83 | + val frameHeightBitsMinus1 = bitBuffer.getInt(4) |
| 84 | + bitBuffer.skip(frameWidthBitsMinus1 + 1 + frameHeightBitsMinus1 + 1) |
| 85 | + var frameIdNumbersPresentFlag = false |
| 86 | + if (!reducedStillPictureHeader) { |
| 87 | + frameIdNumbersPresentFlag = bitBuffer.getBool() |
| 88 | + } |
| 89 | + if (frameIdNumbersPresentFlag) bitBuffer.skip(7) |
| 90 | + bitBuffer.skip(3) |
| 91 | + if (!reducedStillPictureHeader) { |
| 92 | + bitBuffer.skip(4) |
| 93 | + val enableOrderHint = bitBuffer.getBool() |
| 94 | + if (enableOrderHint) bitBuffer.skip(2) |
| 95 | + val seqChooseScreenContentTools = bitBuffer.getBool() |
| 96 | + val seqForceScreenContentTools = seqChooseScreenContentTools || bitBuffer.getBool() |
| 97 | + if (seqForceScreenContentTools) { |
| 98 | + val seqChooseIntegerMv = bitBuffer.getBool() |
| 99 | + if (!seqChooseIntegerMv) bitBuffer.skipBool() |
| 100 | + } |
| 101 | + if (enableOrderHint) bitBuffer.skip(3) |
| 102 | + } |
| 103 | + bitBuffer.skip(3) |
| 104 | + //config color |
| 105 | + highBitDepth = bitBuffer.getBool() |
| 106 | + twelveBit = false |
| 107 | + var bitDepth = 0 |
| 108 | + if (seqProfile == 2 && highBitDepth) { |
| 109 | + twelveBit = bitBuffer.getBool() |
| 110 | + bitDepth = if (twelveBit) 12 else 10 |
| 111 | + } else if (seqProfile <= 2) { |
| 112 | + bitDepth = if (highBitDepth) 10 else 8 |
| 113 | + } |
| 114 | + monochrome = if (seqProfile == 1) { |
| 115 | + false |
| 116 | + } else { |
| 117 | + val chrome = bitBuffer.getBool() |
| 118 | + chrome |
| 119 | + } |
| 120 | + val colorDescriptionPresentFlag = bitBuffer.getBool() |
| 121 | + var colorPrimaries = 0 |
| 122 | + var transferCharacteristics = 0 |
| 123 | + var matrixCoefficients = 0 |
| 124 | + if (colorDescriptionPresentFlag) { |
| 125 | + colorPrimaries = bitBuffer.getInt(8) |
| 126 | + transferCharacteristics = bitBuffer.getInt(8) |
| 127 | + matrixCoefficients = bitBuffer.getInt(8) |
| 128 | + } |
| 129 | + samplePosition = 0 |
| 130 | + if (monochrome) { |
| 131 | + bitBuffer.getBool() |
| 132 | + subsamplingX = true |
| 133 | + subsamplingY = true |
| 134 | + } else if (colorPrimaries == 1 && transferCharacteristics == 13 && matrixCoefficients == 0) { |
| 135 | + subsamplingX = false |
| 136 | + subsamplingY = false |
| 137 | + } else { |
| 138 | + bitBuffer.skipBool() |
| 139 | + if (seqProfile == 0) { |
| 140 | + subsamplingX = true |
| 141 | + subsamplingY = true |
| 142 | + } else if (seqProfile == 1) { |
| 143 | + subsamplingX = false |
| 144 | + subsamplingY = false |
| 145 | + } else { |
| 146 | + if (bitDepth == 12) { |
| 147 | + subsamplingX = bitBuffer.getBool() |
| 148 | + subsamplingY = if (subsamplingX) { |
| 149 | + bitBuffer.getBool() |
| 150 | + } else { |
| 151 | + false |
| 152 | + } |
| 153 | + } else { |
| 154 | + subsamplingX = true |
| 155 | + subsamplingY = false |
| 156 | + } |
| 157 | + } |
| 158 | + if (subsamplingX && subsamplingY) { |
| 159 | + samplePosition = bitBuffer.getInt(2) |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments