|
发表于 2019-9-10 14:33:50
|
显示全部楼层
楼主好。我在Android环境下用java的MediaCodec API读取YUV编码h.264,然后demsg显示要20毫秒编码一帧,达不到同时两路1080p@30fps呢?
下面是编码的代码
- public void encode(byte[] yuvFrame){
- int inputBufferIndex = codec.dequeueInputBuffer(-1);
- if (inputBufferIndex >= 0) {
- ByteBuffer inputBuffer;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- inputBuffer = codec.getInputBuffer(inputBufferIndex);
- } else {
- inputBuffer = codec.getInputBuffers()[inputBufferIndex];
- }
- pts = computePresentationTime(generateIndex);
- if (inputBuffer != null) {
- inputBuffer.clear();
- inputBuffer.put(yuvFrame, 0, yuvFrame.length);
- codec.queueInputBuffer(inputBufferIndex, 0, yuvFrame.length, pts, 0);
- }
- generateIndex += 1;
- }
- MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
- int outputBufferIndex = codec.dequeueOutputBuffer(bufferInfo, DEFAULT_TIMEOUT_US);
- while (outputBufferIndex >= 0) {
- ByteBuffer outputBuffer;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- outputBuffer = codec.getOutputBuffer(outputBufferIndex);
- } else {
- outputBuffer = codec.getOutputBuffers()[outputBufferIndex];
- }
- if (outputBuffer != null) {
- byte[] outData = new byte[bufferInfo.size];
- outputBuffer.get(outData);
- if (bufferInfo.flags == 2) {
- configByte = outData;
- } else if (bufferInfo.flags == 1) {
- byte[] keyframe = new byte[bufferInfo.size + configByte.length];
- System.arraycopy(configByte, 0, keyframe, 0, configByte.length);
- System.arraycopy(outData, 0, keyframe, configByte.length, outData.length);
- if (null != callback) {
- callback.onFrame(this, keyframe);
- }
- } else {
- if (null != callback) {
- callback.onFrame(this, outData);
- }
- }
- codec.releaseOutputBuffer(outputBufferIndex, false);
- }
- outputBufferIndex = codec.dequeueOutputBuffer(bufferInfo, DEFAULT_TIMEOUT_US);
- }
- }
- /**
- * Generates the presentation time for frame N, in microseconds.
- */
- private long computePresentationTime(long frameIndex) {
- return frameIndex * 1000000 / FPS;
- }
|
|