|
1 | 1 | package com.frank.live.util; |
2 | 2 |
|
| 3 | +import android.util.Log; |
| 4 | + |
3 | 5 | /** |
4 | 6 | * Tool of transforming YUV format |
5 | 7 | * Created by frank on 2018/7/1. |
6 | 8 | */ |
7 | 9 |
|
8 | 10 | public class YUVUtil { |
9 | 11 |
|
10 | | - public static byte[] ARGBtoYUV420SemiPlanar(int[] input, int width, int height) { |
| 12 | + public static byte[] ARGBtoYUV420SemiPlanar(int[] input, int width, int height) { |
11 | 13 |
|
12 | | - final int frameSize = width * height; |
13 | | - byte[] yuv420sp = new byte[width * height * 3 / 2]; |
14 | | - int yIndex = 0; |
15 | | - int uvIndex = frameSize; |
| 14 | + final int frameSize = width * height; |
| 15 | + byte[] yuv420sp = new byte[width * height * 3 / 2]; |
| 16 | + int yIndex = 0; |
| 17 | + int uvIndex = frameSize; |
16 | 18 |
|
17 | | - int a, R, G, B, Y, U, V; |
18 | | - int index = 0; |
19 | | - for (int j = 0; j < height; j++) { |
20 | | - for (int i = 0; i < width; i++) { |
| 19 | + int a, R, G, B, Y, U, V; |
| 20 | + int index = 0; |
| 21 | + for (int j = 0; j < height; j++) { |
| 22 | + for (int i = 0; i < width; i++) { |
21 | 23 |
|
22 | | - a = (input[index] & 0xff000000) >> 24; |
23 | | - R = (input[index] & 0xff0000) >> 16; |
24 | | - G = (input[index] & 0xff00) >> 8; |
25 | | - B = (input[index] & 0xff); |
| 24 | + a = (input[index] & 0xff000000) >> 24; |
| 25 | + R = (input[index] & 0xff0000) >> 16; |
| 26 | + G = (input[index] & 0xff00) >> 8; |
| 27 | + B = (input[index] & 0xff); |
26 | 28 |
|
27 | | - // RGB to YUV algorithm |
28 | | - Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16; |
29 | | - U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128; |
30 | | - V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128; |
| 29 | + // RGB to YUV algorithm |
| 30 | + Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16; |
| 31 | + U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128; |
| 32 | + V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128; |
31 | 33 |
|
32 | | - // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2 |
33 | | - // meaning for every 4 Y pixels there are 1 V and 1 U. |
34 | | - yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : (Math.min(Y, 255))); |
35 | | - if (j % 2 == 0 && index % 2 == 0) { |
36 | | - yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : (Math.min(V, 255))); |
37 | | - yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : (Math.min(U, 255))); |
38 | | - } |
| 34 | + // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2 |
| 35 | + // meaning for every 4 Y pixels there are 1 V and 1 U. |
| 36 | + yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : (Math.min(Y, 255))); |
| 37 | + if (j % 2 == 0 && index % 2 == 0) { |
| 38 | + yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : (Math.min(V, 255))); |
| 39 | + yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : (Math.min(U, 255))); |
| 40 | + } |
39 | 41 |
|
40 | | - index++; |
41 | | - } |
| 42 | + index++; |
| 43 | + } |
| 44 | + } |
| 45 | + return yuv420sp; |
42 | 46 | } |
43 | | - return yuv420sp; |
44 | | - } |
45 | 47 |
|
46 | | - public static void YUV420pRotate90(byte[] dstData, byte[] data, int width, int height) { |
47 | | - int n = 0; |
48 | | - int wh = width * height; |
49 | | - //y |
50 | | - for (int j = 0; j < width; j++) { |
51 | | - for(int i = height - 1; i >= 0; i--) { |
52 | | - dstData[n++] = data[width * i + j]; |
53 | | - } |
54 | | - } |
55 | | - //u |
56 | | - for (int i = 0; i < width / 2; i++) { |
57 | | - for (int j = 1; j <= height / 2; j++) { |
58 | | - dstData[n++] = data[wh + ((height/2 - j) * (width / 2) + i)]; |
59 | | - } |
| 48 | + public static void YUV420pRotate90(byte[] dst, byte[] src, int width, int height) { |
| 49 | + int n = 0; |
| 50 | + int wh = width * height; |
| 51 | + int halfWidth = width / 2; |
| 52 | + int halfHeight = height / 2; |
| 53 | + // y |
| 54 | + for (int j = 0; j < width; j++) { |
| 55 | + for (int i = height - 1; i >= 0; i--) { |
| 56 | + dst[n++] = src[width * i + j]; |
| 57 | + } |
| 58 | + } |
| 59 | + // u |
| 60 | + for (int i = 0; i < halfWidth; i++) { |
| 61 | + for (int j = 1; j <= halfHeight; j++) { |
| 62 | + dst[n++] = src[wh + ((halfHeight - j) * halfWidth + i)]; |
| 63 | + } |
| 64 | + } |
| 65 | + // v |
| 66 | + for (int i = 0; i < halfWidth; i++) { |
| 67 | + for (int j = 1; j <= halfHeight; j++) { |
| 68 | + dst[n++] = src[wh + wh / 4 + ((halfHeight - j) * halfWidth + i)]; |
| 69 | + } |
| 70 | + } |
60 | 71 | } |
61 | | - //v |
62 | | - for(int i = 0; i < width / 2; i++) { |
63 | | - for(int j = 1; j <= height / 2; j++) { |
64 | | - dstData[n++] = data[wh + wh / 4 + ((height / 2 - j) * (width / 2) + i)]; |
65 | | - } |
| 72 | + |
| 73 | + public static void YUV420pRotate180(byte[] dst, byte[] src, int width, int height) { |
| 74 | + int n = 0; |
| 75 | + int halfWidth = width / 2; |
| 76 | + int halfHeight = height / 2; |
| 77 | + // y |
| 78 | + for (int j = height - 1; j >= 0; j--) { |
| 79 | + for (int i = width; i > 0; i--) { |
| 80 | + dst[n++] = src[width * j + i - 1]; |
| 81 | + } |
| 82 | + } |
| 83 | + // u |
| 84 | + int offset = width * height; |
| 85 | + for (int j = halfHeight - 1; j >= 0; j--) { |
| 86 | + for (int i = halfWidth; i > 0; i--) { |
| 87 | + dst[n++] = src[offset + halfWidth * j + i - 1]; |
| 88 | + } |
| 89 | + } |
| 90 | + // v |
| 91 | + offset += halfWidth * halfHeight; |
| 92 | + for (int j = halfHeight - 1; j >= 0; j--) { |
| 93 | + for (int i = halfWidth; i > 0; i--) { |
| 94 | + dst[n++] = src[offset + halfWidth * j + i - 1]; |
| 95 | + } |
| 96 | + } |
66 | 97 | } |
67 | | - } |
68 | 98 |
|
69 | 99 | } |
0 commit comments