• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2013 Rockchip Electronics Co., LTD.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /*
19  * @file        Rockchip_OSAL_ColorUtils.c
20  * @brief
21  * @author      xhr(xhr@rock-chips.com)
22  * @version     1.0.0
23  * @history
24  *   2018.8.15 : Create
25  */
26 
27 #include <cstdio>
28 #include <cstdlib>
29 #include <cstring>
30 #include <cerrno>
31 #include <pthread.h>
32 #include <unistd.h>
33 #ifndef OHOS
34 #include <cutils/log.h>
35 #include <cutils/atomic.h>
36 #endif
37 #include <fcntl.h>
38 #include <sys/mman.h>
39 #include "vpu.h"
40 
41 #include "Rockchip_OSAL_Log.h"
42 #include "Rockchip_OMX_Macros.h"
43 #include "Rockchip_OSAL_ColorUtils.h"
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 typedef struct {
50     OMX_U32 mColorAspects;
51     OMX_U32 mCodecAspects;
52 } MapAspects;
53 
54 static MapAspects sIsoPrimaries[] = {
55     {1, PrimariesBT709_5},
56     {2, PrimariesUnspecified},
57     {4, PrimariesBT470_6M},
58     {5, PrimariesBT601_6_625},
59     {6, PrimariesBT601_6_525}, /* main */
60     {7, PrimariesBT601_6_525},
61     // -- ITU T.832 201201 ends here
62     {8, PrimariesGenericFilm},
63     {9, PrimariesBT2020},
64     {10, PrimariesOther}, /* XYZ */
65 };
66 
67 static MapAspects sIsoTransfers[] = {
68     {1, TransferSMPTE170M}, /* main */
69     {2, TransferUnspecified},
70     {4, TransferGamma22},
71     {5, TransferGamma28},
72     {6, TransferSMPTE170M},
73     {7, TransferSMPTE240M},
74     {8, TransferLinear},
75     {9, TransferOther}, /* log 100:1 */
76     {10, TransferOther}, /* log 316:1 */
77     {11, TransferXvYCC},
78     {12, TransferBT1361},
79     {13, TransferSRGB},
80     // -- ITU T.832 201201 ends here
81     {14, TransferSMPTE170M},
82     {15, TransferSMPTE170M},
83     {16, TransferST2084},
84     {17, TransferST428},
85     {18, TransferHLG},
86 };
87 
88 static MapAspects sIsoMatrixCoeffs[] = {
89     {0, MatrixOther},
90     {1, MatrixBT709_5},
91     {2, MatrixUnspecified},
92     {4, MatrixBT470_6M},
93     {6, MatrixBT601_6}, /* main */
94     {5, MatrixBT601_6 },
95     {7, MatrixSMPTE240M },
96     {8, MatrixOther}, /* YCgCo */
97     // -- ITU T.832 201201 ends here
98     {9, MatrixBT2020},
99     {10, MatrixBT2020Constant},
100 };
101 
findCodecAspects(OMX_U32 colorAspects,OMX_U32 * codecAspects,MapAspects * mapAspects,OMX_U32 size)102 OMX_BOOL findCodecAspects(OMX_U32 colorAspects, OMX_U32 *codecAspects, MapAspects *mapAspects, OMX_U32 size)
103 {
104     OMX_BOOL ret = OMX_FALSE;
105     OMX_U32 i    = 0;
106 
107     for (i = 0; i < size; i++) {
108         if (colorAspects == mapAspects[i].mColorAspects) {
109             *codecAspects = mapAspects[i].mCodecAspects;
110             ret = OMX_TRUE;
111             break;
112         }
113     }
114 
115     return ret;
116 }
117 
findColorAspects(OMX_U32 codecAspects,OMX_U32 * colorAspects,MapAspects * mapAspects,OMX_U32 size)118 OMX_BOOL findColorAspects(OMX_U32 codecAspects, OMX_U32 *colorAspects, MapAspects *mapAspects, OMX_U32 size)
119 {
120     OMX_BOOL ret = OMX_FALSE;
121     OMX_U32 i    = 0;
122 
123     for (i = 0; i < size; i++) {
124         if (codecAspects == mapAspects[i].mCodecAspects) {
125             *colorAspects = mapAspects[i].mColorAspects;
126             ret = OMX_TRUE;
127             break;
128         }
129     }
130 
131     return ret;
132 }
133 
134 // static
convertIsoColorAspectsToCodecAspects(OMX_U32 primaries,OMX_U32 transfer,OMX_U32 coeffs,OMX_U32 fullRange,OMX_COLORASPECTS * aspects)135 void convertIsoColorAspectsToCodecAspects(OMX_U32 primaries,
136                                           OMX_U32 transfer,
137                                           OMX_U32 coeffs,
138                                           OMX_U32 fullRange,
139                                           OMX_COLORASPECTS * aspects)
140 {
141     if (!findCodecAspects(primaries, (OMX_U32 *)&aspects->mPrimaries, sIsoPrimaries, ARRAY_SIZE(sIsoPrimaries))) {
142         aspects->mPrimaries = PrimariesUnspecified;
143     }
144     if (!findCodecAspects(transfer, (OMX_U32 *)&aspects->mTransfer, sIsoTransfers, ARRAY_SIZE(sIsoTransfers))) {
145         aspects->mTransfer = TransferUnspecified;
146     }
147     if (!findCodecAspects(coeffs, (OMX_U32 *)&aspects->mMatrixCoeffs,
148         sIsoMatrixCoeffs, ARRAY_SIZE(sIsoMatrixCoeffs))) {
149         aspects->mMatrixCoeffs = MatrixUnspecified;
150     }
151     (aspects->mRange) = fullRange ? RangeFull : RangeLimited;
152 }
153 
154 // static
convertCodecAspectsToIsoColorAspects(OMX_COLORASPECTS * codecAspect,ISO_COLORASPECTS * colorAspect)155 void convertCodecAspectsToIsoColorAspects(OMX_COLORASPECTS *codecAspect, ISO_COLORASPECTS *colorAspect)
156 {
157     if (!findColorAspects((OMX_U32)codecAspect->mPrimaries, &colorAspect->mPrimaries,
158         sIsoPrimaries, ARRAY_SIZE(sIsoPrimaries))) {
159         colorAspect->mPrimaries = 2; // 2:value of mPrimaries
160     }
161     if (!findColorAspects((OMX_U32)codecAspect->mTransfer, &colorAspect->mTransfer,
162         sIsoTransfers, ARRAY_SIZE(sIsoTransfers))) {
163         colorAspect->mTransfer = 2; // 2:value of mTransfer
164     }
165     if (!findColorAspects((OMX_U32)codecAspect->mMatrixCoeffs, &colorAspect->mMatrixCoeffs,
166         sIsoMatrixCoeffs, ARRAY_SIZE(sIsoMatrixCoeffs))) {
167         colorAspect->mMatrixCoeffs = 2; // 2:value of mMatrixCoeffs
168     }
169     (colorAspect->mRange) = codecAspect->mRange == RangeFull ? 2 : 0; // 2:value
170 }
171 
colorAspectsDiffer(const OMX_COLORASPECTS * a,const OMX_COLORASPECTS * b)172 OMX_BOOL colorAspectsDiffer(const OMX_COLORASPECTS *a, const OMX_COLORASPECTS *b)
173 {
174     if (a->mRange != b->mRange
175         || a->mPrimaries != b->mPrimaries
176         || a->mTransfer != b->mTransfer
177         || a->mMatrixCoeffs != b->mMatrixCoeffs) {
178         return OMX_TRUE;
179     }
180     return OMX_FALSE;
181 }
182 
handleColorAspectsChange(const OMX_COLORASPECTS * a,const OMX_COLORASPECTS * b,OMX_COLORASPECTS * c,OMX_U32 perference)183 OMX_U32 handleColorAspectsChange(
184     const OMX_COLORASPECTS *a /* mDefaultColorAspects */,
185     const OMX_COLORASPECTS *b /* mBitstreamColorAspects */,
186     OMX_COLORASPECTS *c /* mFinalColorAspects */,
187     OMX_U32 perference)
188 {
189     if (perference == kPreferBitstream) {
190         updateFinalColorAspects(a, b, c);
191     } else if (perference == kPreferContainer) {
192         updateFinalColorAspects(b, a, c);
193     } else {
194         return OMX_ErrorUnsupportedSetting;
195     }
196     return OMX_TRUE;
197 }
198 
updateFinalColorAspects(const OMX_COLORASPECTS * otherAspects,const OMX_COLORASPECTS * preferredAspects,OMX_COLORASPECTS * mFinalColorAspects)199 void updateFinalColorAspects(
200     const OMX_COLORASPECTS *otherAspects,
201     const OMX_COLORASPECTS *preferredAspects,
202     OMX_COLORASPECTS *mFinalColorAspects)
203 {
204     OMX_COLORASPECTS newAspects;
205     OMX_COLORASPECTS *pnewAspects = &newAspects;
206     newAspects.mRange        = preferredAspects->mRange != RangeUnspecified ?
207                                preferredAspects->mRange : otherAspects->mRange;
208     newAspects.mPrimaries    = preferredAspects->mPrimaries != PrimariesUnspecified ?
209                                preferredAspects->mPrimaries : otherAspects->mPrimaries;
210     newAspects.mTransfer     = preferredAspects->mTransfer != TransferUnspecified ?
211                                preferredAspects->mTransfer : otherAspects->mTransfer;
212     newAspects.mMatrixCoeffs = preferredAspects->mMatrixCoeffs != MatrixUnspecified ?
213                                preferredAspects->mMatrixCoeffs : otherAspects->mMatrixCoeffs;
214 
215     // Check to see if need update mFinalColorAspects.
216     if (colorAspectsDiffer(mFinalColorAspects, pnewAspects)) {
217         omx_info("updateFinalColorAspects");
218         mFinalColorAspects->mRange = newAspects.mRange;
219         mFinalColorAspects->mPrimaries = newAspects.mPrimaries;
220         mFinalColorAspects->mTransfer = newAspects.mTransfer;
221         mFinalColorAspects->mMatrixCoeffs = newAspects.mMatrixCoeffs;
222     }
223 }
224 #ifdef __cplusplus
225 }
226 #endif