1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * Contains implemenation of framebuffer conversion routines.
19  */
20 
21 #define LOG_NDEBUG 0
22 #define LOG_TAG "EmulatedCamera_Converter"
23 #include "Converters.h"
24 #include <log/log.h>
25 
26 namespace android {
27 
_YUV420SToRGB565(const uint8_t * Y,const uint8_t * U,const uint8_t * V,int dUV,uint16_t * rgb,int width,int height)28 static void _YUV420SToRGB565(const uint8_t* Y, const uint8_t* U,
29                              const uint8_t* V, int dUV, uint16_t* rgb,
30                              int width, int height) {
31   const uint8_t* U_pos = U;
32   const uint8_t* V_pos = V;
33 
34   for (int y = 0; y < height; y++) {
35     for (int x = 0; x < width; x += 2, U += dUV, V += dUV) {
36       const uint8_t nU = *U;
37       const uint8_t nV = *V;
38       *rgb = YUVToRGB565(*Y, nU, nV);
39       Y++;
40       rgb++;
41       *rgb = YUVToRGB565(*Y, nU, nV);
42       Y++;
43       rgb++;
44     }
45     if (y & 0x1) {
46       U_pos = U;
47       V_pos = V;
48     } else {
49       U = U_pos;
50       V = V_pos;
51     }
52   }
53 }
54 
_YUV420SToRGB32(const uint8_t * Y,const uint8_t * U,const uint8_t * V,int dUV,uint32_t * rgb,int width,int height)55 static void _YUV420SToRGB32(const uint8_t* Y, const uint8_t* U,
56                             const uint8_t* V, int dUV, uint32_t* rgb, int width,
57                             int height) {
58   const uint8_t* U_pos = U;
59   const uint8_t* V_pos = V;
60 
61   for (int y = 0; y < height; y++) {
62     for (int x = 0; x < width; x += 2, U += dUV, V += dUV) {
63       const uint8_t nU = *U;
64       const uint8_t nV = *V;
65       *rgb = YUVToRGB32(*Y, nU, nV);
66       Y++;
67       rgb++;
68       *rgb = YUVToRGB32(*Y, nU, nV);
69       Y++;
70       rgb++;
71     }
72     if (y & 0x1) {
73       U_pos = U;
74       V_pos = V;
75     } else {
76       U = U_pos;
77       V = V_pos;
78     }
79   }
80 }
81 
YV12ToRGB565(const void * yv12,void * rgb,int width,int height)82 void YV12ToRGB565(const void* yv12, void* rgb, int width, int height) {
83   const int pix_total = width * height;
84   const uint8_t* Y = reinterpret_cast<const uint8_t*>(yv12);
85   const uint8_t* U = Y + pix_total;
86   const uint8_t* V = U + pix_total / 4;
87   _YUV420SToRGB565(Y, U, V, 1, reinterpret_cast<uint16_t*>(rgb), width, height);
88 }
89 
YV12ToRGB32(const void * yv12,void * rgb,int width,int height)90 void YV12ToRGB32(const void* yv12, void* rgb, int width, int height) {
91   const int pix_total = width * height;
92   const uint8_t* Y = reinterpret_cast<const uint8_t*>(yv12);
93   const uint8_t* V = Y + pix_total;
94   const uint8_t* U = V + pix_total / 4;
95   _YUV420SToRGB32(Y, U, V, 1, reinterpret_cast<uint32_t*>(rgb), width, height);
96 }
97 
YU12ToRGB32(const void * yu12,void * rgb,int width,int height)98 void YU12ToRGB32(const void* yu12, void* rgb, int width, int height) {
99   const int pix_total = width * height;
100   const uint8_t* Y = reinterpret_cast<const uint8_t*>(yu12);
101   const uint8_t* U = Y + pix_total;
102   const uint8_t* V = U + pix_total / 4;
103   _YUV420SToRGB32(Y, U, V, 1, reinterpret_cast<uint32_t*>(rgb), width, height);
104 }
105 
106 /* Common converter for YUV 4:2:0 interleaved to RGB565.
107  * y, u, and v point to Y,U, and V panes, where U and V values are interleaved.
108  */
_NVXXToRGB565(const uint8_t * Y,const uint8_t * U,const uint8_t * V,uint16_t * rgb,int width,int height)109 static void _NVXXToRGB565(const uint8_t* Y, const uint8_t* U, const uint8_t* V,
110                           uint16_t* rgb, int width, int height) {
111   _YUV420SToRGB565(Y, U, V, 2, rgb, width, height);
112 }
113 
114 /* Common converter for YUV 4:2:0 interleaved to RGB32.
115  * y, u, and v point to Y,U, and V panes, where U and V values are interleaved.
116  */
_NVXXToRGB32(const uint8_t * Y,const uint8_t * U,const uint8_t * V,uint32_t * rgb,int width,int height)117 static void _NVXXToRGB32(const uint8_t* Y, const uint8_t* U, const uint8_t* V,
118                          uint32_t* rgb, int width, int height) {
119   _YUV420SToRGB32(Y, U, V, 2, rgb, width, height);
120 }
121 
NV12ToRGB565(const void * nv12,void * rgb,int width,int height)122 void NV12ToRGB565(const void* nv12, void* rgb, int width, int height) {
123   const int pix_total = width * height;
124   const uint8_t* y = reinterpret_cast<const uint8_t*>(nv12);
125   _NVXXToRGB565(y, y + pix_total, y + pix_total + 1,
126                 reinterpret_cast<uint16_t*>(rgb), width, height);
127 }
128 
NV12ToRGB32(const void * nv12,void * rgb,int width,int height)129 void NV12ToRGB32(const void* nv12, void* rgb, int width, int height) {
130   const int pix_total = width * height;
131   const uint8_t* y = reinterpret_cast<const uint8_t*>(nv12);
132   _NVXXToRGB32(y, y + pix_total, y + pix_total + 1,
133                reinterpret_cast<uint32_t*>(rgb), width, height);
134 }
135 
NV21ToRGB565(const void * nv21,void * rgb,int width,int height)136 void NV21ToRGB565(const void* nv21, void* rgb, int width, int height) {
137   const int pix_total = width * height;
138   const uint8_t* y = reinterpret_cast<const uint8_t*>(nv21);
139   _NVXXToRGB565(y, y + pix_total + 1, y + pix_total,
140                 reinterpret_cast<uint16_t*>(rgb), width, height);
141 }
142 
NV21ToRGB32(const void * nv21,void * rgb,int width,int height)143 void NV21ToRGB32(const void* nv21, void* rgb, int width, int height) {
144   const int pix_total = width * height;
145   const uint8_t* y = reinterpret_cast<const uint8_t*>(nv21);
146   _NVXXToRGB32(y, y + pix_total + 1, y + pix_total,
147                reinterpret_cast<uint32_t*>(rgb), width, height);
148 }
149 
150 }; /* namespace android */
151