关于视频预览硬解的问题
0
发表于2015-8-17
手机看帖
15
7298
hi,各位,最近在弄dji摄像头预览的视频数据处理工作,经过一段时间的网上调研,准备采用硬解码的方式,
以下是我的代码,有没有人能帮我看一下问题出在哪呢。
//解码器初始化
MediaCodec mediaCodec = MediaCodec.createDecoderByType("video/avc");
MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", 640, 360);
if(mediaCodec == null)
{
Log.e("","mediaCodec == null");
return;
}
mediaCodec.configure(mediaFormat, null, null, 0);
mediaCodec.start();
//视频数据回调接口,我的解码在这里面处理的,这个代码参考的英文网站http://forum.dji.com/forum.php?m ... mp;page=1#pid137903
mReceivedVideoDataCallBack = new DJIReceivedVideoDataCallBack(){
private int packetLength = 0;
private ByteBuffer accessUnitBuffer = ByteBuffer.allocate( 50000 ); //数据流缓冲区,存放一帧完整的数据流
@Override
public void onResult(byte[] videoBuffer, int size)
{
// TODO Auto-generated method stub
for(int i = 0; i < size-5; i++)
{
//查找H264数据流中的结束符,将当前帧数据送到mediaCodec解码
if(videoBuffer== 0x00 && videoBuffer[i+1]== 0x00
&& videoBuffer[i+2]== 0x00 && videoBuffer[i+3]== 0x01
&& videoBuffer[i+4]== 0x09)
{
handler.sendMessage(handler.obtainMessage(SHOWTOAST, "packetLength = "+ packetLength));
ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
ByteBuffer[] outputBuffers = mediaCodec.getOutputBuffers();
int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1);
handler.sendMessage(handler.obtainMessage(SHOWTOAST, "inputBufferIndex = "+ inputBufferIndex));
if (inputBufferIndex >= 0) {
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(accessUnitBuffer);
mediaCodec.queueInputBuffer(inputBufferIndex, 0, packetLength, presentationTime, 0);
presentationTime += 100;
packetLength = 0;
accessUnitBuffer.clear();
accessUnitBuffer.rewind();
}
//解码后的数据存入到dataListOut中,为后面写入文件准备
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);
handler.sendMessage(handler.obtainMessage(SHOWTOAST, "outputBufferIndex = "+ outputBufferIndex));
//现在问题就在这,outputBufferIndex 这个数值得到的是-1,没有解码数据,有没有大神能帮我找一下原因啊
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);
}
//解码后的一帧数据写入文件
handler.sendMessage(handler.obtainMessage(SHOWTOAST, "dataListOut.size() = "+ dataListOut.size()));
if(dataListOut.size()!=0)
{
try {
FileOutputStream fout = new FileOutputStream(createNewFileName());
int offset = 0;
for(int j = 0; j< dataListOut.size();j++)
{
fout.write(dataListOut.get(j),offset,dataListOut.get(j).length);
offset += dataListOut.get(j).length;
}
fout.close();
handler.sendMessage(handler.obtainMessage(SHOWTOAST, "write file succes"));
dataListOut.clear();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else
{
accessUnitBuffer.put(videoBuffer);
packetLength++;
}
}
// mDjiGLSurfaceView.setDataToDecoder(videoBuffer, size);
}
};
然后还有一个问题,这样解码后得到的一帧的图像数据应该是YUV格式的吧,我将YUV格式的文件存为.raw后缀名的文件可以吗,用什么软件可以查看这种格式的图片
|
|
|
|