android nv12 nv21,android - convert NV21 to NV12 an rotate 90 degrees By libyuv? - Stack Overflow

I'm developing an Android Camera application. When I deal the frame, I met some troubles.

In Camera's onPreviewFrame(byte[] data, Camera camera) function, I set data's format is NV21 fromat, because NV21 is all Android devices supported.

When I use MediaCodec to codec the frame, the KEY_COLOR_FORMAT is COLOR_FormatYUV420SemiPlanar (NV12).

So I need convert NV21 to NV12.

And also, the frame is rotate -90 degrees, I want rotate is back,means rotate 90 degrees back.

I made it by using java:

// 1. rotate 90 degree clockwise

// 2. convert NV21 to NV12

private byte[] rotateYUV420SemiPlannerFrame(byte[] input, int width, int height) {

//from:https://github.com/upyun/android-push-sdk/blob/7d74e3c941bbede0b6f9f588b1d4e7926a5f2733/uppush/src/main/java/com/upyun/hardware/VideoEncoder.java

int frameSize = width * height;

byte[] output = new byte[frameSize * 3 / 2];

int i = 0;

for (int col = 0; col < width; col++) {

for (int row = height - 1; row >= 0; row--) {

output[i++] = input[width * row + col]; // Y

}

}

i = 0;

for (int col = 0; col < width / 2; col++) {

for (int row = height / 2 - 1; row >= 0; row--) {

int i2 = i * 2;

int fwrc2 = frameSize + width * row + col * 2;

output[frameSize + i2 + 1] = input[fwrc2]; // Cb (U)

output[frameSize + i2] = input[fwrc2 + 1]; // Cr (V)

i++;

}

}

return output;

}

The function works well , but cost long time , more than 50ms.

As I know, libyuv deals the YUV img faster, and I want to use it in my android Camera application .

In libyuv, I found three functions may help:

// Convert NV21 to I420.

LIBYUV_API

int NV21ToI420(const uint8* src_y, int src_stride_y,

const uint8* src_vu, int src_stride_vu,

uint8* dst_y, int dst_stride_y,

uint8* dst_u, int dst_stride_u,

uint8* dst_v, int dst_stride_v,

int width, int height);

// Rotate I420 frame.

LIBYUV_API

int I420Rotate(const uint8* src_y, int src_stride_y,

const uint8* src_u, int src_stride_u,

const uint8* src_v, int src_stride_v,

uint8* dst_y, int dst_stride_y,

uint8* dst_u, int dst_stride_u,

uint8* dst_v, int dst_stride_v,

int src_width, int src_height, enum RotationMode mode);

LIBYUV_API

int I420ToNV12(const uint8* src_y, int src_stride_y,

const uint8* src_u, int src_stride_u,

const uint8* src_v, int src_stride_v,

uint8* dst_y, int dst_stride_y,

uint8* dst_uv, int dst_stride_uv,

int width, int height);

Use these functions,may get work. But convert and rotate may cost more time (I guess..).

Is there any way to get my goal by using less functions? Thanks.

I also found some answers here, not what I want.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值