这是解决android h264解码器延迟的一种方法吗?

btxsgosb  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(380)

我正在开发视频流应用程序客户端,它以1帧/秒的速率接收h264帧。第一帧是i帧,接下来的所有帧都是p帧。帧使用带有实时设置的openh264编码,无需下一个p帧即可解码(我使用openh264 decodeframenodelay函数在windows应用程序上按时正确解码帧)。我在android解码器端看到的是,解码器不会产生输出,除非它缓冲下一个4或5 p帧,这会导致我的应用程序在结果上有巨大且不可接受的延迟。我很确定,至少在我的情况下,没有必要再拍下一帧。我试过的:我试着给解码器输入一些毫无意义的h264 NAL,可以将延迟从3秒减少到1秒,但它并不总是有效,如果我们这样做,更多的编解码器将遇到错误并最终停止。

byte[] frame = h264Data.toByteArray();
// Now we need to give it to the Codec to decode into the surface

for (int i = 0; i < 2; i++) 
{
  // Get the input buffer from the decoder
  // Pass -1 here as here we don't have a playback time reference
  int inputIndex = m_codec.dequeueInputBuffer(-1);

  // If  the buffer index is valid use the buffer
  if (inputIndex >= 0) 
  {
    ByteBuffer buffer;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
      buffer = m_codec.getInputBuffer(inputIndex);
    } else {
      ByteBuffer[] bbuf = m_codec.getInputBuffers();
      buffer = bbuf[inputIndex];
    }
    buffer.put(frame);

    // tell the decoder to process the frame
    m_codec.queueInputBuffer(inputIndex, 0, frame.length, 0, 0);

    //Now filling the frame with effectless h264 NAL to deceive decoder
    frame = new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x0C, (byte) 0x00, (byte) 0x00, (byte) 0x00};
  }
  MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
  int outputIndex = m_codec.dequeueOutputBuffer(info, 0);
  if (outputIndex >= 0) {
    m_codec.releaseOutputBuffer(outputIndex, true);
  }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题