1

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.

Community
  • 1
  • 1
I'm SuperMan
  • 653
  • 7
  • 20

0 Answers0