• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 namespace android {
18 namespace emulation {
19 
20 template <typename T>
21 class YuvConverter {
22 public:
YuvConverter(int nWidth,int nHeight)23     YuvConverter(int nWidth, int nHeight) : nWidth(nWidth), nHeight(nHeight) {
24         pQuad = new T[nWidth * nHeight / 4];
25     }
~YuvConverter()26     ~YuvConverter() { delete[] pQuad; }
27     void PlanarToUVInterleaved(T* pFrame, int nPitch = 0) {
28         if (nPitch == 0) {
29             nPitch = nWidth;
30         }
31         T* puv = pFrame + nPitch * nHeight;
32         if (nPitch == nWidth) {
33             memcpy(pQuad, puv, nWidth * nHeight / 4 * sizeof(T));
34         } else {
35             for (int i = 0; i < nHeight / 2; i++) {
36                 memcpy(pQuad + nWidth / 2 * i, puv + nPitch / 2 * i,
37                        nWidth / 2 * sizeof(T));
38             }
39         }
40         T* pv = puv + (nPitch / 2) * (nHeight / 2);
41         for (int y = 0; y < nHeight / 2; y++) {
42             for (int x = 0; x < nWidth / 2; x++) {
43                 puv[y * nPitch + x * 2] = pQuad[y * nWidth / 2 + x];
44                 puv[y * nPitch + x * 2 + 1] = pv[y * nPitch / 2 + x];
45             }
46         }
47     }
48     void UVInterleavedToPlanar(T* pFrame, int nPitch = 0) {
49         if (nPitch == 0) {
50             nPitch = nWidth;
51         }
52         T *puv = pFrame + nPitch * nHeight, *pu = puv,
53           *pv = puv + nPitch * nHeight / 4;
54         for (int y = 0; y < nHeight / 2; y++) {
55             for (int x = 0; x < nWidth / 2; x++) {
56                 pu[y * nPitch / 2 + x] = puv[y * nPitch + x * 2];
57                 pQuad[y * nWidth / 2 + x] = puv[y * nPitch + x * 2 + 1];
58             }
59         }
60         if (nPitch == nWidth) {
61             memcpy(pv, pQuad, nWidth * nHeight / 4 * sizeof(T));
62         } else {
63             for (int i = 0; i < nHeight / 2; i++) {
64                 memcpy(pv + nPitch / 2 * i, pQuad + nWidth / 2 * i,
65                        nWidth / 2 * sizeof(T));
66             }
67         }
68     }
69 
70 private:
71     T* pQuad;
72     int nWidth, nHeight;
73 };
74 
75 }  // namespace emulation
76 }  // namespace android
77