解释此编码器如何处理 PPS 和 SPS?



我在网上找到了这段代码,有人可以解释一下PPS和SPS部分吗?

if (sps != null && pps != null)之后的一切我都明白,因为我们检查if (spsPpsBuffer.getInt() == 0x00000001)因为 NALU 以0x00000001开头,但在那之后我真的不明白以下内容:

  • 为什么ppsIndex一开始设置为 0,然后设置为spsPpsBuffer.position()

  • 为什么 SPS 缓冲区大小ppsIndex - 8

  • 为什么PPS缓冲区的大小outData.length - ppsIndex

这是代码:

@Override
public void offerEncoder(byte[] input) {
try {
ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
ByteBuffer[] outputBuffers = mediaCodec.getOutputBuffers();
int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0) {
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(input);
mediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, 0, 0);
}
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);
while (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
byte[] outData = new byte[bufferInfo.size];
outputBuffer.get(outData);
if (sps != null && pps != null) {
ByteBuffer frameBuffer = ByteBuffer.wrap(outData);
frameBuffer.putInt(bufferInfo.size - 4);
frameListener.frameReceived(outData, 0, outData.length);
} else {
ByteBuffer spsPpsBuffer = ByteBuffer.wrap(outData);
if (spsPpsBuffer.getInt() == 0x00000001) {
System.out.println("parsing sps/pps");
} else {
System.out.println("something is amiss?");
}
int ppsIndex = 0;
while(!(spsPpsBuffer.get() == 0x00 && spsPpsBuffer.get() == 0x00 && spsPpsBuffer.get() == 0x00 && spsPpsBuffer.get() == 0x01)) {
}
ppsIndex = spsPpsBuffer.position();
sps = new byte[ppsIndex - 8];
System.arraycopy(outData, 4, sps, 0, sps.length);
pps = new byte[outData.length - ppsIndex];
System.arraycopy(outData, ppsIndex, pps, 0, pps.length);
if (null != parameterSetsListener) {
parameterSetsListener.avcParametersSetsEstablished(sps, pps);
}
}
mediaCodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);
}
} catch (Throwable t) {
t.printStackTrace();
}
}

谢谢。

您可以从前面的答案中了解PPS/SPS的一般概念: H264 带多个 PPS 和 SPS

上面的代码是高度专业化的,只能处理 H.264 流的一小部分。该代码假定固定长度的 SPS(8 个字节(,并做出一些无效的假设。 除非代码是针对一个特定的编码器 - 我可能不会使用它。

这似乎是一个不错的H.264解析器:https://github.com/aizvorski/h264bitstream

最新更新