在Android上解码h264 ByTestStream总是会从dequeueOutputBuffer()获取信息
正在尝试使用媒体编解码器解码已编码的数据包,但在dequeueOutputBuffer()中始终得到-1 请帮帮我,我做错了什么?我试图面对这个问题好几天都没有成功 私有视频(){在Android上解码h264 ByTestStream总是会从dequeueOutputBuffer()获取信息,android,decode,h.264,android-mediacodec,Android,Decode,H.264,Android Mediacodec,正在尝试使用媒体编解码器解码已编码的数据包,但在dequeueOutputBuffer()中始终得到-1 请帮帮我,我做错了什么?我试图面对这个问题好几天都没有成功 私有视频(){ new线程(new Runnable()){ @凌驾 公开募捐{ int n=0; MediaFormat MediaFormat=新的MediaFormat(); mediaFormat.setString(mediaFormat.KEY_MIME,“video/avc”); mediaFormat.setInte
new线程(new Runnable()){
@凌驾
公开募捐{
int n=0;
MediaFormat MediaFormat=新的MediaFormat();
mediaFormat.setString(mediaFormat.KEY_MIME,“video/avc”);
mediaFormat.setInteger(mediaFormat.KEY\u MAX\u INPUT\u SIZE,100000);
mediaFormat.setInteger(mediaFormat.KEY\u宽度,
getWidth());
mediaFormat.setInteger(mediaFormat.KEY\u高度,
getHeight());
mediaFormat.setInteger(
MediaFormat.KEY\u PUSH\u BLANK\u BUFFERS\u ON\u STOP,1);
mediaFormat.setInteger(mediaFormat.KEY\u COLOR\u格式,
MediaCodecInfo.CodecCapabilities.COLOR_FormatL2);//待办事项
字节[]csd_info={0,0,0,1,103,100,0,40,-84,52,-59,
1, -32, 17, 31, 120, 11, 80, 16, 16, 31, 0, 0, 3, 3,
-23, 0, 0, -22, 96, -108, 0, 0, 0, 1, 104, -18, 60,
-128 };
mediaFormat.setByteBuffer(“csd-0”,ByteBuffer.wrap(csd_信息));
mediaFormat.setInteger(mediaFormat.KEY\u MAX\u INPUT\u SIZE,
1920 * 1080);
setInteger(“durationUs”,63446722);
MediaCodec codec=MediaCodec.createDecoderByType(“视频/avc”);
configure(mediaFormat,mHolder.getSurface(),null,0);
codec.start();
ByteBuffer[]inputBuffers=codec.getInputBuffers();
ByteBuffer[]outputBuffers=codec.getOutputBuffers();
BufferInfo=new BufferInfo();
while(旗帜){
int-inputBufferIndex=codec.dequeueInputBuffer(-1);
如果(inputBufferIndex>=0){
而(mpackes.size()=0){
Log.d(标记“outputBufferIndex>=0”);
如果(n>2){
对数d(标记“n>2”);
//我们已经成功地编码和解码了一幅图像
// !
int length=info.size;
codec.releaseOutputBuffer(outputBufferIndex,false);
n++;
}
}
}
codec.stop();
codec.release();
编解码器=空;
}
}).start();
}
非常感谢您抽出时间。可能是因为它不喜欢接收到的输入。您是否在logcat输出中看到任何可疑的内容?
new Thread(new Runnable() {
@Override
public void run() {
int n = 0;
MediaFormat mediaFormat = new MediaFormat();
mediaFormat.setString(MediaFormat.KEY_MIME, "video/avc");
mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 100000);
mediaFormat.setInteger(MediaFormat.KEY_WIDTH,
surfaceView.getWidth());
mediaFormat.setInteger(MediaFormat.KEY_HEIGHT,
surfaceView.getHeight());
mediaFormat.setInteger(
MediaFormat.KEY_PUSH_BLANK_BUFFERS_ON_STOP, 1);
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT,
MediaCodecInfo.CodecCapabilities.COLOR_FormatL2);// TODO
byte[] csd_info = { 0, 0, 0, 1, 103, 100, 0, 40, -84, 52, -59,
1, -32, 17, 31, 120, 11, 80, 16, 16, 31, 0, 0, 3, 3,
-23, 0, 0, -22, 96, -108, 0, 0, 0, 1, 104, -18, 60,
-128 };
mediaFormat.setByteBuffer("csd-0", ByteBuffer.wrap(csd_info));
mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE,
1920 * 1080);
mediaFormat.setInteger("durationUs", 63446722);
MediaCodec codec = MediaCodec.createDecoderByType("video/avc");
codec.configure(mediaFormat, mHolder.getSurface(), null, 0);
codec.start();
ByteBuffer[] inputBuffers = codec.getInputBuffers();
ByteBuffer[] outputBuffers = codec.getOutputBuffers();
BufferInfo info = new BufferInfo();
while (flag) {
int inputBufferIndex = codec.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0) {
while (mPackets.size() <= 0) {
try {
Log.d(TAG, "nopackets");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.d(TAG, "now I have packets!");
inputBuffers[inputBufferIndex].clear();
DatagramPacket currentDatagram = mPackets.remove();
byte[] byteBuffer = (ByteBuffer.wrap(currentDatagram
.getData())).array();
inputBuffers[inputBufferIndex].put(byteBuffer, 0,
byteBuffer.length);
codec.queueInputBuffer(inputBufferIndex, 0,
byteBuffer.length, timestamp(), 0);
}
int outputBufferIndex = codec.dequeueOutputBuffer(info,-1);//always get-1
if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
outputBuffers = codec.getOutputBuffers();
Log.d(TAG, "INFO_OUTPUT_BUFFERS_CHANGED");
} else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
// Subsequent data will conform to new format.
mediaFormat = codec.getOutputFormat();
Log.d(TAG, "INFO_OUTPUT_FORMAT_CHANGED");
} else if (outputBufferIndex >= 0) {
Log.d(TAG, "outputBufferIndex>=0");
if (n > 2) {
Log.d(TAG, "n>2");
// We have successfully encoded and decoded an image
// !
int length = info.size;
codec.releaseOutputBuffer(outputBufferIndex, false);
n++;
}
}
}
codec.stop();
codec.release();
codec = null;
}
}).start();
}