在 Xperia Z1 上使用 MediaCodec 编码相机预览帧时视频质量不一致 [英] Inconsistent video quality when encoding camera preview frames with MediaCodec on Xperia Z1

查看:26
本文介绍了在 Xperia Z1 上使用 MediaCodec 编码相机预览帧时视频质量不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编码来自相机预览的 NV21 帧.出于某种原因,在其他设备上运行良好的架构在带有 Android 4.3Sony Xperia Z1 上运行不正常.它发回不正确(低)质量的编码帧.

I am encoding NV21 frames coming from camera preview. For some reason schema which is working fine on other devices works incorrectly on Sony Xperia Z1 with Android 4.3. It sends back encoded frames with incorrect (low) quality.

MediaCodec 的格式是 COLOR_FormatYUV420SemiPlanarNV12 (我将 NV21 转换为 NV12 通过交换 U 和 V 分量).MediaCodec 发回给我的输出缓冲区的尺寸非常小,与分辨率 (1280x720) 和比特率 (1000000) 不相符,我正在使用.前几帧质量很好,但随后显着下降:

MediaCodec's format is COLOR_FormatYUV420SemiPlanar which is NV12 (I convert NV21 to NV12 by swapping U and V components). Output buffers sent back to me by MediaCodec are with very low size which don't correspond to resolution (1280x720) and bitrate (1000000) I am using. First few frames are coming with a good quality, but then it's dropping significantly:

int encoderStatus = mMediaCodec.dequeueOutputBuffer(mBufferInfo, TIMEOUT_USEC);
// a few encoderStatus checks skipped
ByteBuffer encodedData = outputBuffers[encoderStatus];
Log.i(Constants.TAG, "Buffer size " + mBufferInfo.size);

这给了我 Xperia Z1 上的以下日志:

Which gives me the following log on Xperia Z1:

Buffer size 26
Buffer size 52172
Buffer size 23650
Buffer size 14394
Buffer size 3591
Buffer size 1849
Buffer size 3908

...

Buffer size 1043
Buffer size 248
Buffer size 836
Buffer size 518
Buffer size 1112

来自 Sony Xperia ZR 的示例日志,它可以正常工作:

Example log from Sony Xperia ZR on which this works properly:

Buffer size 21
Buffer size 51048
Buffer size 21063
Buffer size 24228
Buffer size 28040
...
Buffer size 44959
Buffer size 44972
Buffer size 44957
Buffer size 45004
Buffer size 44999
Buffer size 44957

如有任何建议,我们将不胜感激.

Any advice would be appreciated.

推荐答案

您确定以正确的单位(微秒)传递时间戳 - 并且它设置了合理的帧速率?

Are you sure you pass timestamps in the right unit (microseconds) - and that it has set a sensible framerate?

  • 某些编码器可能会忽略时间戳,仅根据帧率设置每帧固定的比特率预算.

  • Some encoders might ignore the timestamps and only set a fixed bitrate budget per frame, based on the framerate.

其他人可能会尝试根据时间戳计算每帧允许使用多少位.

Others might try to calculate how many bits they're allowed to use per frame based on the timestamp.

如果给出时间戳,例如相反,这可能会导致编码器将编码帧的大小减小到零.

If the timestamps are given e.g. in milliseconds instead, this could lead to the encoder reducing the size of encoded frames down towards zero.

这篇关于在 Xperia Z1 上使用 MediaCodec 编码相机预览帧时视频质量不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆