Android MediaCodec YV12转YUV420 编码后花屏、四重影

a250575 2015-01-21 02:30:45
摄像头预览的图片格式设置YV12,编码成H264时要转成YUV420的,编码完成后再解码播放,就有花屏、重影的现象,现在不知道是什么原因了,求助!!!!
图标左侧是预览的,没有问题,右侧是经过编码、解码后的


设置图片预览格式
parameters.setPictureFormat(ImageFormat.YV12);

编码
// add h264 file for test
public int encode(byte[] input, byte[] output, RandomAccessFile raf){
int pos = 0;
swapYV12toI420(input, yuv420, width, height);
// yuv420 = swapYV12toI420(input, width, height);
try {
ByteBuffer[] inputBuffers = mediaencodec.getInputBuffers();
ByteBuffer[] outputBuffers = mediaencodec.getOutputBuffers();
int inputBufferIndex = mediaencodec.dequeueInputBuffer(-1);
//Log.i("EncodeAndDecode", "encode inputBufferIndex= "+inputBufferIndex);
++einputcount;
Log.i("EncodeAndDecode", "encode inputcount= "+einputcount+" input size="+yuv420.length);
if (inputBufferIndex >= 0)
{
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(yuv420);
mediaencodec.queueInputBuffer(inputBufferIndex, 0, yuv420.length, 0, 0);
}

MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = mediaencodec.dequeueOutputBuffer(bufferInfo,0);
//Log.i("EncodeAndDecode", "encode outputBufferIndex= "+outputBufferIndex);

// add h264 file for test
// raf.write(new byte[]{0x00,0x00,0x00,0x01});
while (outputBufferIndex >= 0)
{
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
byte[] outData = new byte[bufferInfo.size];
outputBuffer.get(outData);
++eoutputcount;
Log.i("EncodeAndDecode", "encode outputcount= "+eoutputcount+" outData size="+outData.length);

// add h264 file for test
// raf.write(outData);

decode(outData);
mediaencodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mediaencodec.dequeueOutputBuffer(bufferInfo, 0);
}

} catch (Throwable t) {
t.printStackTrace();
}

return pos;

}

格式转换
private void swapYV12toI420(byte[] yv12bytes, byte[] i420bytes, int width, int height)
{
System.arraycopy(yv12bytes, 0, i420bytes, 0, width*height);
System.arraycopy(yv12bytes, width*height+width*height/4, i420bytes, width*height, width*height/4);
System.arraycopy(yv12bytes, width*height, i420bytes, width*height+width*height/4, width*height/4);
}
...全文
1676 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lucky_zf 2016-08-23
  • 打赏
  • 举报
回复
?什么情况 什么情况
lishengnan1234 2016-05-30
  • 打赏
  • 举报
回复
楼主仗义点啊,人人为我,我为人人啊
  • 打赏
  • 举报
回复
楼主能参考下你的configure么? 我dequebuffer 时总是返回-1 yuv420序列.
西街恶人 2015-11-22
  • 打赏
  • 举报
回复
楼主怎么解决的哈。。。。
毛同学 2015-05-20
  • 打赏
  • 举报
回复
请问 这个怎么解决的? 多谢
windfury_plus 2015-02-11
  • 打赏
  • 举报
回复
引用 4 楼 crazyman2010 的回复:
如果Y对了,UV错了那只是颜色不对,他这个应该是stride没处理好
可能是用错了方法?nv21和yv12的转换互用好像能造成这个效果……唉都忘了
M_O_ 2015-02-11
  • 打赏
  • 举报
回复
引用 3 楼 inquisitive_plus 的回复:
花屏、重影现象是Y转对了,U转错了?
如果Y对了,UV错了那只是颜色不对,他这个应该是stride没处理好
windfury_plus 2015-02-11
  • 打赏
  • 举报
回复
花屏、重影现象是Y转对了,U转错了?
qq852014098 2015-02-11
  • 打赏
  • 举报
回复
请问你是怎么解决的。我也遇到了这个问题了
a250575 2015-01-26
  • 打赏
  • 举报
回复
已经解决了。

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧