|
this.mediaCodec = MediaCodec.createEncoderByType("video/avc");
MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", this.width, this.height);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 300000);
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 20);
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar);
mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
this.mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
this.mediaCodec.start();
编码线程
EncoderThread = new Thread(new Runnable() {
@Override
public void run() {
isRuning = true;
byte[] input = null;
while (isRuning) {
if (dataListIn != null)
input = dataListIn.poll();
if (input != null) {
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);
// inputBuffer.put(yuv420Byte);
// mediaCodec.queueInputBuffer(inputBufferIndex, 0, yuv420Byte.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 (dataListOut != null)
dataListOut.add(outData);
mediaCodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);
}
} catch (Throwable t) {
t.printStackTrace();
}
} else {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
mediaCodec.stop();
mediaCodec.release();
}
});
EncoderThread.start();
[size=13.63636302947998px]以上代码 实现的对应解码...在 真机测试 尺寸在800*600 就出现解码 绿屏
[size=13.63636302947998px]另外板子只支持 COLOR_FormatYUV420SemiPlanar 是社 固件有关系么?
[size=13.63636302947998px]请大大 指点指点...这个 视频编码.解码
|
|