1 /*
2  * Copyright 2018 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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "Codec2Mapper"
19 #include <utils/Log.h>
20 
21 #include <media/stagefright/MediaCodecConstants.h>
22 #include <media/stagefright/SurfaceUtils.h>
23 #include <media/stagefright/foundation/ALookup.h>
24 #include <media/stagefright/foundation/ColorUtils.h>
25 #include <media/stagefright/foundation/MediaDefs.h>
26 
27 #include <stdint.h>  // for INT32_MAX
28 
29 #include "Codec2Mapper.h"
30 
31 using namespace android;
32 
33 namespace {
34 
35 ALookup<C2Config::profile_t, int32_t> sAacProfiles = {
36     { C2Config::PROFILE_AAC_LC,         AACObjectLC },
37     { C2Config::PROFILE_AAC_MAIN,       AACObjectMain },
38     { C2Config::PROFILE_AAC_SSR,        AACObjectSSR },
39     { C2Config::PROFILE_AAC_LTP,        AACObjectLTP },
40     { C2Config::PROFILE_AAC_HE,         AACObjectHE },
41     { C2Config::PROFILE_AAC_SCALABLE,   AACObjectScalable },
42     { C2Config::PROFILE_AAC_ER_LC,      AACObjectERLC },
43     { C2Config::PROFILE_AAC_ER_SCALABLE, AACObjectERScalable },
44     { C2Config::PROFILE_AAC_LD,         AACObjectLD },
45     { C2Config::PROFILE_AAC_HE_PS,      AACObjectHE_PS },
46     { C2Config::PROFILE_AAC_ELD,        AACObjectELD },
47     { C2Config::PROFILE_AAC_XHE,        AACObjectXHE },
48 };
49 
50 ALookup<C2Config::level_t, int32_t> sAvcLevels = {
51     { C2Config::LEVEL_AVC_1,    AVCLevel1 },
52     { C2Config::LEVEL_AVC_1B,   AVCLevel1b },
53     { C2Config::LEVEL_AVC_1_1,  AVCLevel11 },
54     { C2Config::LEVEL_AVC_1_2,  AVCLevel12 },
55     { C2Config::LEVEL_AVC_1_3,  AVCLevel13 },
56     { C2Config::LEVEL_AVC_2,    AVCLevel2 },
57     { C2Config::LEVEL_AVC_2_1,  AVCLevel21 },
58     { C2Config::LEVEL_AVC_2_2,  AVCLevel22 },
59     { C2Config::LEVEL_AVC_3,    AVCLevel3 },
60     { C2Config::LEVEL_AVC_3_1,  AVCLevel31 },
61     { C2Config::LEVEL_AVC_3_2,  AVCLevel32 },
62     { C2Config::LEVEL_AVC_4,    AVCLevel4 },
63     { C2Config::LEVEL_AVC_4_1,  AVCLevel41 },
64     { C2Config::LEVEL_AVC_4_2,  AVCLevel42 },
65     { C2Config::LEVEL_AVC_5,    AVCLevel5 },
66     { C2Config::LEVEL_AVC_5_1,  AVCLevel51 },
67     { C2Config::LEVEL_AVC_5_2,  AVCLevel52 },
68     { C2Config::LEVEL_AVC_6,    AVCLevel6 },
69     { C2Config::LEVEL_AVC_6_1,  AVCLevel61 },
70     { C2Config::LEVEL_AVC_6_2,  AVCLevel62 },
71 };
72 
73 ALookup<C2Config::profile_t, int32_t> sAvcProfiles = {
74     // treat restricted profiles as full profile if there is no equivalent - which works for
75     // decoders, but not for encoders
76     { C2Config::PROFILE_AVC_BASELINE,               AVCProfileBaseline },
77     { C2Config::PROFILE_AVC_CONSTRAINED_BASELINE,   AVCProfileConstrainedBaseline },
78     { C2Config::PROFILE_AVC_MAIN,                   AVCProfileMain },
79     { C2Config::PROFILE_AVC_EXTENDED,               AVCProfileExtended },
80     { C2Config::PROFILE_AVC_HIGH,                   AVCProfileHigh },
81     { C2Config::PROFILE_AVC_PROGRESSIVE_HIGH,       AVCProfileHigh },
82     { C2Config::PROFILE_AVC_CONSTRAINED_HIGH,       AVCProfileConstrainedHigh },
83     { C2Config::PROFILE_AVC_HIGH_10,                AVCProfileHigh10 },
84     { C2Config::PROFILE_AVC_PROGRESSIVE_HIGH_10,    AVCProfileHigh10 },
85     { C2Config::PROFILE_AVC_HIGH_422,               AVCProfileHigh422 },
86     { C2Config::PROFILE_AVC_HIGH_444_PREDICTIVE,    AVCProfileHigh444 },
87     { C2Config::PROFILE_AVC_HIGH_10_INTRA,          AVCProfileHigh10 },
88     { C2Config::PROFILE_AVC_HIGH_422_INTRA,         AVCProfileHigh422 },
89     { C2Config::PROFILE_AVC_HIGH_444_INTRA,         AVCProfileHigh444 },
90     { C2Config::PROFILE_AVC_CAVLC_444_INTRA,        AVCProfileHigh444 },
91 };
92 
93 ALookup<C2Config::bitrate_mode_t, int32_t> sBitrateModes = {
94     { C2Config::BITRATE_CONST,      BITRATE_MODE_CBR },
95     { C2Config::BITRATE_VARIABLE,   BITRATE_MODE_VBR },
96     { C2Config::BITRATE_IGNORE,     BITRATE_MODE_CQ },
97 };
98 
99 ALookup<C2Color::matrix_t, ColorAspects::MatrixCoeffs> sColorMatricesSf = {
100     { C2Color::MATRIX_UNSPECIFIED,     ColorAspects::MatrixUnspecified },
101     { C2Color::MATRIX_BT709,           ColorAspects::MatrixBT709_5 },
102     { C2Color::MATRIX_FCC47_73_682,    ColorAspects::MatrixBT470_6M },
103     { C2Color::MATRIX_BT601,           ColorAspects::MatrixBT601_6 },
104     { C2Color::MATRIX_240M,       ColorAspects::MatrixSMPTE240M },
105     { C2Color::MATRIX_BT2020,          ColorAspects::MatrixBT2020 },
106     { C2Color::MATRIX_BT2020_CONSTANT, ColorAspects::MatrixBT2020Constant },
107     { C2Color::MATRIX_OTHER,           ColorAspects::MatrixOther },
108 };
109 
110 ALookup<C2Color::primaries_t, ColorAspects::Primaries> sColorPrimariesSf = {
111     { C2Color::PRIMARIES_UNSPECIFIED,  ColorAspects::PrimariesUnspecified },
112     { C2Color::PRIMARIES_BT709,        ColorAspects::PrimariesBT709_5 },
113     { C2Color::PRIMARIES_BT470_M,      ColorAspects::PrimariesBT470_6M },
114     { C2Color::PRIMARIES_BT601_625,    ColorAspects::PrimariesBT601_6_625 },
115     { C2Color::PRIMARIES_BT601_525,    ColorAspects::PrimariesBT601_6_525 },
116     { C2Color::PRIMARIES_GENERIC_FILM, ColorAspects::PrimariesGenericFilm },
117     { C2Color::PRIMARIES_BT2020,       ColorAspects::PrimariesBT2020 },
118 //    { C2Color::PRIMARIES_RP431,        ColorAspects::Primaries... },
119 //    { C2Color::PRIMARIES_EG432,        ColorAspects::Primaries... },
120 //    { C2Color::PRIMARIES_EBU3213,      ColorAspects::Primaries... },
121     { C2Color::PRIMARIES_OTHER,        ColorAspects::PrimariesOther },
122 };
123 
124 ALookup<C2Color::range_t, int32_t> sColorRanges = {
125     { C2Color::RANGE_FULL,    COLOR_RANGE_FULL },
126     { C2Color::RANGE_LIMITED, COLOR_RANGE_LIMITED },
127 };
128 
129 ALookup<C2Color::range_t, ColorAspects::Range> sColorRangesSf = {
130     { C2Color::RANGE_UNSPECIFIED, ColorAspects::RangeUnspecified },
131     { C2Color::RANGE_FULL,        ColorAspects::RangeFull },
132     { C2Color::RANGE_LIMITED,     ColorAspects::RangeLimited },
133     { C2Color::RANGE_OTHER,       ColorAspects::RangeOther },
134 };
135 
136 ALookup<C2Color::transfer_t, int32_t> sColorTransfers = {
137     { C2Color::TRANSFER_LINEAR, COLOR_TRANSFER_LINEAR },
138     { C2Color::TRANSFER_170M,   COLOR_TRANSFER_SDR_VIDEO },
139     { C2Color::TRANSFER_ST2084, COLOR_TRANSFER_ST2084 },
140     { C2Color::TRANSFER_HLG,    COLOR_TRANSFER_HLG },
141 };
142 
143 ALookup<C2Color::transfer_t, ColorAspects::Transfer> sColorTransfersSf = {
144     { C2Color::TRANSFER_UNSPECIFIED, ColorAspects::TransferUnspecified },
145     { C2Color::TRANSFER_LINEAR,      ColorAspects::TransferLinear },
146     { C2Color::TRANSFER_SRGB,        ColorAspects::TransferSRGB },
147     { C2Color::TRANSFER_170M,        ColorAspects::TransferSMPTE170M },
148     { C2Color::TRANSFER_GAMMA22,     ColorAspects::TransferGamma22 },
149     { C2Color::TRANSFER_GAMMA28,     ColorAspects::TransferGamma28 },
150     { C2Color::TRANSFER_ST2084,      ColorAspects::TransferST2084 },
151     { C2Color::TRANSFER_HLG,         ColorAspects::TransferHLG },
152     { C2Color::TRANSFER_240M,        ColorAspects::TransferSMPTE240M },
153     { C2Color::TRANSFER_XVYCC,       ColorAspects::TransferXvYCC },
154     { C2Color::TRANSFER_BT1361,      ColorAspects::TransferBT1361 },
155     { C2Color::TRANSFER_ST428,       ColorAspects::TransferST428 },
156     { C2Color::TRANSFER_OTHER,       ColorAspects::TransferOther },
157 };
158 
159 ALookup<C2Config::level_t, int32_t> sDolbyVisionLevels = {
160     { C2Config::LEVEL_DV_MAIN_HD_24,  DolbyVisionLevelHd24 },
161     { C2Config::LEVEL_DV_MAIN_HD_30,  DolbyVisionLevelHd30 },
162     { C2Config::LEVEL_DV_MAIN_FHD_24, DolbyVisionLevelFhd24 },
163     { C2Config::LEVEL_DV_MAIN_FHD_30, DolbyVisionLevelFhd30 },
164     { C2Config::LEVEL_DV_MAIN_FHD_60, DolbyVisionLevelFhd60 },
165     { C2Config::LEVEL_DV_MAIN_UHD_24, DolbyVisionLevelUhd24 },
166     { C2Config::LEVEL_DV_MAIN_UHD_30, DolbyVisionLevelUhd30 },
167     { C2Config::LEVEL_DV_MAIN_UHD_48, DolbyVisionLevelUhd48 },
168     { C2Config::LEVEL_DV_MAIN_UHD_60, DolbyVisionLevelUhd60 },
169 
170     // high tiers are not yet supported on android, for now map them to main tier
171     { C2Config::LEVEL_DV_HIGH_HD_24,  DolbyVisionLevelHd24 },
172     { C2Config::LEVEL_DV_HIGH_HD_30,  DolbyVisionLevelHd30 },
173     { C2Config::LEVEL_DV_HIGH_FHD_24, DolbyVisionLevelFhd24 },
174     { C2Config::LEVEL_DV_HIGH_FHD_30, DolbyVisionLevelFhd30 },
175     { C2Config::LEVEL_DV_HIGH_FHD_60, DolbyVisionLevelFhd60 },
176     { C2Config::LEVEL_DV_HIGH_UHD_24, DolbyVisionLevelUhd24 },
177     { C2Config::LEVEL_DV_HIGH_UHD_30, DolbyVisionLevelUhd30 },
178     { C2Config::LEVEL_DV_HIGH_UHD_48, DolbyVisionLevelUhd48 },
179     { C2Config::LEVEL_DV_HIGH_UHD_60, DolbyVisionLevelUhd60 },
180 };
181 
182 ALookup<C2Config::profile_t, int32_t> sDolbyVisionProfiles = {
183     { C2Config::PROFILE_DV_AV_PER, DolbyVisionProfileDvavPer },
184     { C2Config::PROFILE_DV_AV_PEN, DolbyVisionProfileDvavPen },
185     { C2Config::PROFILE_DV_HE_DER, DolbyVisionProfileDvheDer },
186     { C2Config::PROFILE_DV_HE_DEN, DolbyVisionProfileDvheDen },
187     { C2Config::PROFILE_DV_HE_04, DolbyVisionProfileDvheDtr },
188     { C2Config::PROFILE_DV_HE_05, DolbyVisionProfileDvheStn },
189     { C2Config::PROFILE_DV_HE_DTH, DolbyVisionProfileDvheDth },
190     { C2Config::PROFILE_DV_HE_07, DolbyVisionProfileDvheDtb },
191     { C2Config::PROFILE_DV_HE_08, DolbyVisionProfileDvheSt },
192     { C2Config::PROFILE_DV_AV_09, DolbyVisionProfileDvavSe },
193     { C2Config::PROFILE_DV_AV1_10, DolbyVisionProfileDvav110 },
194 };
195 
196 ALookup<C2Config::level_t, int32_t> sH263Levels = {
197     { C2Config::LEVEL_H263_10, H263Level10 },
198     { C2Config::LEVEL_H263_20, H263Level20 },
199     { C2Config::LEVEL_H263_30, H263Level30 },
200     { C2Config::LEVEL_H263_40, H263Level40 },
201     { C2Config::LEVEL_H263_45, H263Level45 },
202     { C2Config::LEVEL_H263_50, H263Level50 },
203     { C2Config::LEVEL_H263_60, H263Level60 },
204     { C2Config::LEVEL_H263_70, H263Level70 },
205 };
206 
207 ALookup<C2Config::profile_t, int32_t> sH263Profiles = {
208     { C2Config::PROFILE_H263_BASELINE,          H263ProfileBaseline },
209     { C2Config::PROFILE_H263_H320,              H263ProfileH320Coding },
210     { C2Config::PROFILE_H263_V1BC,              H263ProfileBackwardCompatible },
211     { C2Config::PROFILE_H263_ISWV2,             H263ProfileISWV2 },
212     { C2Config::PROFILE_H263_ISWV3,             H263ProfileISWV3 },
213     { C2Config::PROFILE_H263_HIGH_COMPRESSION,  H263ProfileHighCompression },
214     { C2Config::PROFILE_H263_INTERNET,          H263ProfileInternet },
215     { C2Config::PROFILE_H263_INTERLACE,         H263ProfileInterlace },
216     { C2Config::PROFILE_H263_HIGH_LATENCY,      H263ProfileHighLatency },
217 };
218 
219 ALookup<C2Config::level_t, int32_t> sHevcLevels = {
220     { C2Config::LEVEL_HEVC_MAIN_1,      HEVCMainTierLevel1 },
221     { C2Config::LEVEL_HEVC_MAIN_2,      HEVCMainTierLevel2 },
222     { C2Config::LEVEL_HEVC_MAIN_2_1,    HEVCMainTierLevel21 },
223     { C2Config::LEVEL_HEVC_MAIN_3,      HEVCMainTierLevel3 },
224     { C2Config::LEVEL_HEVC_MAIN_3_1,    HEVCMainTierLevel31 },
225     { C2Config::LEVEL_HEVC_MAIN_4,      HEVCMainTierLevel4 },
226     { C2Config::LEVEL_HEVC_MAIN_4_1,    HEVCMainTierLevel41 },
227     { C2Config::LEVEL_HEVC_MAIN_5,      HEVCMainTierLevel5 },
228     { C2Config::LEVEL_HEVC_MAIN_5_1,    HEVCMainTierLevel51 },
229     { C2Config::LEVEL_HEVC_MAIN_5_2,    HEVCMainTierLevel52 },
230     { C2Config::LEVEL_HEVC_MAIN_6,      HEVCMainTierLevel6 },
231     { C2Config::LEVEL_HEVC_MAIN_6_1,    HEVCMainTierLevel61 },
232     { C2Config::LEVEL_HEVC_MAIN_6_2,    HEVCMainTierLevel62 },
233 
234     { C2Config::LEVEL_HEVC_HIGH_4,      HEVCHighTierLevel4 },
235     { C2Config::LEVEL_HEVC_HIGH_4_1,    HEVCHighTierLevel41 },
236     { C2Config::LEVEL_HEVC_HIGH_5,      HEVCHighTierLevel5 },
237     { C2Config::LEVEL_HEVC_HIGH_5_1,    HEVCHighTierLevel51 },
238     { C2Config::LEVEL_HEVC_HIGH_5_2,    HEVCHighTierLevel52 },
239     { C2Config::LEVEL_HEVC_HIGH_6,      HEVCHighTierLevel6 },
240     { C2Config::LEVEL_HEVC_HIGH_6_1,    HEVCHighTierLevel61 },
241     { C2Config::LEVEL_HEVC_HIGH_6_2,    HEVCHighTierLevel62 },
242 
243     // map high tier levels below 4 to main tier
244     { C2Config::LEVEL_HEVC_MAIN_1,      HEVCHighTierLevel1 },
245     { C2Config::LEVEL_HEVC_MAIN_2,      HEVCHighTierLevel2 },
246     { C2Config::LEVEL_HEVC_MAIN_2_1,    HEVCHighTierLevel21 },
247     { C2Config::LEVEL_HEVC_MAIN_3,      HEVCHighTierLevel3 },
248     { C2Config::LEVEL_HEVC_MAIN_3_1,    HEVCHighTierLevel31 },
249 };
250 
251 ALookup<C2Config::profile_t, int32_t> sHevcProfiles = {
252     { C2Config::PROFILE_HEVC_MAIN, HEVCProfileMain },
253     { C2Config::PROFILE_HEVC_MAIN_10, HEVCProfileMain10 },
254     { C2Config::PROFILE_HEVC_MAIN_STILL, HEVCProfileMainStill },
255     { C2Config::PROFILE_HEVC_MAIN_INTRA, HEVCProfileMain },
256     { C2Config::PROFILE_HEVC_MAIN_10_INTRA, HEVCProfileMain10 },
257 };
258 
259 ALookup<C2Config::profile_t, int32_t> sHevcHdrProfiles = {
260     { C2Config::PROFILE_HEVC_MAIN_10, HEVCProfileMain10HDR10 },
261 };
262 
263 ALookup<C2Config::profile_t, int32_t> sHevcHdr10PlusProfiles = {
264     { C2Config::PROFILE_HEVC_MAIN_10, HEVCProfileMain10HDR10Plus },
265 };
266 
267 ALookup<C2Config::level_t, int32_t> sMpeg2Levels = {
268     { C2Config::LEVEL_MP2V_LOW,         MPEG2LevelLL },
269     { C2Config::LEVEL_MP2V_MAIN,        MPEG2LevelML },
270     { C2Config::LEVEL_MP2V_HIGH_1440,   MPEG2LevelH14 },
271     { C2Config::LEVEL_MP2V_HIGH,        MPEG2LevelHL },
272     { C2Config::LEVEL_MP2V_HIGHP,       MPEG2LevelHP },
273 };
274 
275 ALookup<C2Config::profile_t, int32_t> sMpeg2Profiles = {
276     { C2Config::PROFILE_MP2V_SIMPLE,                MPEG2ProfileSimple },
277     { C2Config::PROFILE_MP2V_MAIN,                  MPEG2ProfileMain },
278     { C2Config::PROFILE_MP2V_SNR_SCALABLE,          MPEG2ProfileSNR },
279     { C2Config::PROFILE_MP2V_SPATIALLY_SCALABLE,    MPEG2ProfileSpatial },
280     { C2Config::PROFILE_MP2V_HIGH,                  MPEG2ProfileHigh },
281     { C2Config::PROFILE_MP2V_422,                   MPEG2Profile422 },
282 };
283 
284 ALookup<C2Config::level_t, int32_t> sMpeg4Levels = {
285     { C2Config::LEVEL_MP4V_0,   MPEG4Level0 },
286     { C2Config::LEVEL_MP4V_0B,  MPEG4Level0b },
287     { C2Config::LEVEL_MP4V_1,   MPEG4Level1 },
288     { C2Config::LEVEL_MP4V_2,   MPEG4Level2 },
289     { C2Config::LEVEL_MP4V_3,   MPEG4Level3 },
290     { C2Config::LEVEL_MP4V_3B,  MPEG4Level3b },
291     { C2Config::LEVEL_MP4V_4,   MPEG4Level4 },
292     { C2Config::LEVEL_MP4V_4A,  MPEG4Level4a },
293     { C2Config::LEVEL_MP4V_5,   MPEG4Level5 },
294     { C2Config::LEVEL_MP4V_6,   MPEG4Level6 },
295 };
296 
297 ALookup<C2Config::profile_t, int32_t> sMpeg4Profiles = {
298     { C2Config::PROFILE_MP4V_SIMPLE,            MPEG4ProfileSimple },
299     { C2Config::PROFILE_MP4V_SIMPLE_SCALABLE,   MPEG4ProfileSimpleScalable },
300     { C2Config::PROFILE_MP4V_CORE,              MPEG4ProfileCore },
301     { C2Config::PROFILE_MP4V_MAIN,              MPEG4ProfileMain },
302     { C2Config::PROFILE_MP4V_NBIT,              MPEG4ProfileNbit },
303     { C2Config::PROFILE_MP4V_ARTS,              MPEG4ProfileAdvancedRealTime },
304     { C2Config::PROFILE_MP4V_CORE_SCALABLE,     MPEG4ProfileCoreScalable },
305     { C2Config::PROFILE_MP4V_ACE,               MPEG4ProfileAdvancedCoding },
306     { C2Config::PROFILE_MP4V_ADVANCED_CORE,     MPEG4ProfileAdvancedCore },
307     { C2Config::PROFILE_MP4V_ADVANCED_SIMPLE,   MPEG4ProfileAdvancedSimple },
308 };
309 
310 ALookup<C2Config::pcm_encoding_t, int32_t> sPcmEncodings = {
311     { C2Config::PCM_8, kAudioEncodingPcm8bit },
312     { C2Config::PCM_16, kAudioEncodingPcm16bit },
313     { C2Config::PCM_FLOAT, kAudioEncodingPcmFloat },
314 };
315 
316 ALookup<C2Config::level_t, int32_t> sVp9Levels = {
317     { C2Config::LEVEL_VP9_1,    VP9Level1 },
318     { C2Config::LEVEL_VP9_1_1,  VP9Level11 },
319     { C2Config::LEVEL_VP9_2,    VP9Level2 },
320     { C2Config::LEVEL_VP9_2_1,  VP9Level21 },
321     { C2Config::LEVEL_VP9_3,    VP9Level3 },
322     { C2Config::LEVEL_VP9_3_1,  VP9Level31 },
323     { C2Config::LEVEL_VP9_4,    VP9Level4 },
324     { C2Config::LEVEL_VP9_4_1,  VP9Level41 },
325     { C2Config::LEVEL_VP9_5,    VP9Level5 },
326     { C2Config::LEVEL_VP9_5_1,  VP9Level51 },
327     { C2Config::LEVEL_VP9_5_2,  VP9Level52 },
328     { C2Config::LEVEL_VP9_6,    VP9Level6 },
329     { C2Config::LEVEL_VP9_6_1,  VP9Level61 },
330     { C2Config::LEVEL_VP9_6_2,  VP9Level62 },
331 };
332 
333 ALookup<C2Config::profile_t, int32_t> sVp9Profiles = {
334     { C2Config::PROFILE_VP9_0, VP9Profile0 },
335     { C2Config::PROFILE_VP9_1, VP9Profile1 },
336     { C2Config::PROFILE_VP9_2, VP9Profile2 },
337     { C2Config::PROFILE_VP9_3, VP9Profile3 },
338     { C2Config::PROFILE_VP9_2, VP9Profile2HDR },
339     { C2Config::PROFILE_VP9_3, VP9Profile3HDR },
340     { C2Config::PROFILE_VP9_2, VP9Profile2HDR10Plus },
341     { C2Config::PROFILE_VP9_3, VP9Profile3HDR10Plus },
342 };
343 
344 ALookup<C2Config::profile_t, int32_t> sVp9HdrProfiles = {
345     { C2Config::PROFILE_VP9_2, VP9Profile2HDR },
346     { C2Config::PROFILE_VP9_3, VP9Profile3HDR },
347 };
348 
349 ALookup<C2Config::profile_t, int32_t> sVp9Hdr10PlusProfiles = {
350     { C2Config::PROFILE_VP9_2, VP9Profile2HDR10Plus },
351     { C2Config::PROFILE_VP9_3, VP9Profile3HDR10Plus },
352 };
353 
354 ALookup<C2Config::level_t, int32_t> sAv1Levels = {
355     { C2Config::LEVEL_AV1_2,    AV1Level2  },
356     { C2Config::LEVEL_AV1_2_1,  AV1Level21 },
357     { C2Config::LEVEL_AV1_2_2,  AV1Level22 },
358     { C2Config::LEVEL_AV1_2_3,  AV1Level23 },
359     { C2Config::LEVEL_AV1_3,    AV1Level3  },
360     { C2Config::LEVEL_AV1_3_1,  AV1Level31 },
361     { C2Config::LEVEL_AV1_3_2,  AV1Level32 },
362     { C2Config::LEVEL_AV1_3_3,  AV1Level33 },
363     { C2Config::LEVEL_AV1_4,    AV1Level4  },
364     { C2Config::LEVEL_AV1_4_1,  AV1Level41 },
365     { C2Config::LEVEL_AV1_4_2,  AV1Level42 },
366     { C2Config::LEVEL_AV1_4_3,  AV1Level43 },
367     { C2Config::LEVEL_AV1_5,    AV1Level5  },
368     { C2Config::LEVEL_AV1_5_1,  AV1Level51 },
369     { C2Config::LEVEL_AV1_5_2,  AV1Level52 },
370     { C2Config::LEVEL_AV1_5_3,  AV1Level53 },
371     { C2Config::LEVEL_AV1_6,    AV1Level6  },
372     { C2Config::LEVEL_AV1_6_1,  AV1Level61 },
373     { C2Config::LEVEL_AV1_6_2,  AV1Level62 },
374     { C2Config::LEVEL_AV1_6_3,  AV1Level63 },
375     { C2Config::LEVEL_AV1_7,    AV1Level7  },
376     { C2Config::LEVEL_AV1_7_1,  AV1Level71 },
377     { C2Config::LEVEL_AV1_7_2,  AV1Level72 },
378     { C2Config::LEVEL_AV1_7_3,  AV1Level73 },
379 };
380 
381 
382 ALookup<C2Config::profile_t, int32_t> sAv1Profiles = {
383     // TODO: will need to disambiguate between Main8 and Main10
384     { C2Config::PROFILE_AV1_0, AV1ProfileMain8 },
385     { C2Config::PROFILE_AV1_0, AV1ProfileMain10 },
386     { C2Config::PROFILE_AV1_0, AV1ProfileMain10HDR10 },
387     { C2Config::PROFILE_AV1_0, AV1ProfileMain10HDR10Plus },
388 };
389 
390 ALookup<C2Config::profile_t, int32_t> sAv1HdrProfiles = {
391     { C2Config::PROFILE_AV1_0, AV1ProfileMain10HDR10 },
392 };
393 
394 ALookup<C2Config::profile_t, int32_t> sAv1Hdr10PlusProfiles = {
395     { C2Config::PROFILE_AV1_0, AV1ProfileMain10HDR10Plus },
396 };
397 
398 /**
399  * A helper that passes through vendor extension profile and level values.
400  */
401 struct ProfileLevelMapperHelper : C2Mapper::ProfileLevelMapper {
402     virtual bool simpleMap(C2Config::level_t from, int32_t *to) = 0;
403     virtual bool simpleMap(int32_t from, C2Config::level_t *to) = 0;
404     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) = 0;
405     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) = 0;
406 
407     template<typename T, typename U>
passThroughMap__anon5ab1e7200111::ProfileLevelMapperHelper408     bool passThroughMap(T from, U *to) {
409         // allow (and pass through) vendor extensions
410         if (from >= (T)C2_PROFILE_LEVEL_VENDOR_START && from < (T)INT32_MAX) {
411             *to = (U)from;
412             return true;
413         }
414         return simpleMap(from, to);
415     }
416 
mapLevel__anon5ab1e7200111::ProfileLevelMapperHelper417     virtual bool mapLevel(C2Config::level_t from, int32_t *to) {
418         return passThroughMap(from, to);
419     }
420 
mapLevel__anon5ab1e7200111::ProfileLevelMapperHelper421     virtual bool mapLevel(int32_t from, C2Config::level_t *to) {
422         return passThroughMap(from, to);
423     }
424 
mapProfile__anon5ab1e7200111::ProfileLevelMapperHelper425     virtual bool mapProfile(C2Config::profile_t from, int32_t *to) {
426         return passThroughMap(from, to);
427     }
428 
mapProfile__anon5ab1e7200111::ProfileLevelMapperHelper429     virtual bool mapProfile(int32_t from, C2Config::profile_t *to) {
430         return passThroughMap(from, to);
431     }
432 };
433 
434 // AAC only uses profiles, map all levels to unused or 0
435 struct AacProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anon5ab1e7200111::AacProfileLevelMapper436     virtual bool simpleMap(C2Config::level_t, int32_t *to) {
437         *to = 0;
438         return true;
439     }
simpleMap__anon5ab1e7200111::AacProfileLevelMapper440     virtual bool simpleMap(int32_t, C2Config::level_t *to) {
441         *to = C2Config::LEVEL_UNUSED;
442         return true;
443     }
simpleMap__anon5ab1e7200111::AacProfileLevelMapper444     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
445         return sAacProfiles.map(from, to);
446     }
simpleMap__anon5ab1e7200111::AacProfileLevelMapper447     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
448         return sAacProfiles.map(from, to);
449     }
450 };
451 
452 struct AvcProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anon5ab1e7200111::AvcProfileLevelMapper453     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
454         return sAvcLevels.map(from, to);
455     }
simpleMap__anon5ab1e7200111::AvcProfileLevelMapper456     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
457         return sAvcLevels.map(from, to);
458     }
simpleMap__anon5ab1e7200111::AvcProfileLevelMapper459     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
460         return sAvcProfiles.map(from, to);
461     }
simpleMap__anon5ab1e7200111::AvcProfileLevelMapper462     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
463         return sAvcProfiles.map(from, to);
464     }
465 };
466 
467 struct DolbyVisionProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anon5ab1e7200111::DolbyVisionProfileLevelMapper468     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
469         return sDolbyVisionLevels.map(from, to);
470     }
simpleMap__anon5ab1e7200111::DolbyVisionProfileLevelMapper471     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
472         return sDolbyVisionLevels.map(from, to);
473     }
simpleMap__anon5ab1e7200111::DolbyVisionProfileLevelMapper474     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
475         return sDolbyVisionProfiles.map(from, to);
476     }
simpleMap__anon5ab1e7200111::DolbyVisionProfileLevelMapper477     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
478         return sDolbyVisionProfiles.map(from, to);
479     }
480 };
481 
482 struct H263ProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anon5ab1e7200111::H263ProfileLevelMapper483     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
484         return sH263Levels.map(from, to);
485     }
simpleMap__anon5ab1e7200111::H263ProfileLevelMapper486     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
487         return sH263Levels.map(from, to);
488     }
simpleMap__anon5ab1e7200111::H263ProfileLevelMapper489     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
490         return sH263Profiles.map(from, to);
491     }
simpleMap__anon5ab1e7200111::H263ProfileLevelMapper492     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
493         return sH263Profiles.map(from, to);
494     }
495 };
496 
497 struct HevcProfileLevelMapper : ProfileLevelMapperHelper {
HevcProfileLevelMapper__anon5ab1e7200111::HevcProfileLevelMapper498     HevcProfileLevelMapper(bool isHdr = false, bool isHdr10Plus = false) :
499         ProfileLevelMapperHelper(),
500         mIsHdr(isHdr), mIsHdr10Plus(isHdr10Plus) {}
501 
simpleMap__anon5ab1e7200111::HevcProfileLevelMapper502     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
503         return sHevcLevels.map(from, to);
504     }
simpleMap__anon5ab1e7200111::HevcProfileLevelMapper505     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
506         return sHevcLevels.map(from, to);
507     }
simpleMap__anon5ab1e7200111::HevcProfileLevelMapper508     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
509         return mIsHdr10Plus ? sHevcHdr10PlusProfiles.map(from, to) :
510                      mIsHdr ? sHevcHdrProfiles.map(from, to) :
511                               sHevcProfiles.map(from, to);
512     }
simpleMap__anon5ab1e7200111::HevcProfileLevelMapper513     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
514         return mIsHdr10Plus ? sHevcHdr10PlusProfiles.map(from, to) :
515                      mIsHdr ? sHevcHdrProfiles.map(from, to) :
516                               sHevcProfiles.map(from, to);
517     }
518 
519 private:
520     bool mIsHdr;
521     bool mIsHdr10Plus;
522 };
523 
524 struct Mpeg2ProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anon5ab1e7200111::Mpeg2ProfileLevelMapper525     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
526         return sMpeg2Levels.map(from, to);
527     }
simpleMap__anon5ab1e7200111::Mpeg2ProfileLevelMapper528     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
529         return sMpeg2Levels.map(from, to);
530     }
simpleMap__anon5ab1e7200111::Mpeg2ProfileLevelMapper531     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
532         return sMpeg2Profiles.map(from, to);
533     }
simpleMap__anon5ab1e7200111::Mpeg2ProfileLevelMapper534     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
535         return sMpeg2Profiles.map(from, to);
536     }
537 };
538 
539 struct Mpeg4ProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anon5ab1e7200111::Mpeg4ProfileLevelMapper540     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
541         return sMpeg4Levels.map(from, to);
542     }
simpleMap__anon5ab1e7200111::Mpeg4ProfileLevelMapper543     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
544         return sMpeg4Levels.map(from, to);
545     }
simpleMap__anon5ab1e7200111::Mpeg4ProfileLevelMapper546     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
547         return sMpeg4Profiles.map(from, to);
548     }
simpleMap__anon5ab1e7200111::Mpeg4ProfileLevelMapper549     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
550         return sMpeg4Profiles.map(from, to);
551     }
552 };
553 
554 // VP8 has no profiles and levels in Codec 2.0, but we use main profile and level 0 in MediaCodec
555 // map all profiles and levels to that.
556 struct Vp8ProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anon5ab1e7200111::Vp8ProfileLevelMapper557     virtual bool simpleMap(C2Config::level_t, int32_t *to) {
558         *to = VP8Level_Version0;
559         return true;
560     }
simpleMap__anon5ab1e7200111::Vp8ProfileLevelMapper561     virtual bool simpleMap(int32_t, C2Config::level_t *to) {
562         *to = C2Config::LEVEL_UNUSED;
563         return true;
564     }
simpleMap__anon5ab1e7200111::Vp8ProfileLevelMapper565     virtual bool simpleMap(C2Config::profile_t, int32_t *to) {
566         *to = VP8ProfileMain;
567         return true;
568     }
simpleMap__anon5ab1e7200111::Vp8ProfileLevelMapper569     virtual bool simpleMap(int32_t, C2Config::profile_t *to) {
570         *to = C2Config::PROFILE_UNUSED;
571         return true;
572     }
573 };
574 
575 struct Vp9ProfileLevelMapper : ProfileLevelMapperHelper {
Vp9ProfileLevelMapper__anon5ab1e7200111::Vp9ProfileLevelMapper576     Vp9ProfileLevelMapper(bool isHdr = false, bool isHdr10Plus = false) :
577         ProfileLevelMapperHelper(),
578         mIsHdr(isHdr), mIsHdr10Plus(isHdr10Plus) {}
579 
simpleMap__anon5ab1e7200111::Vp9ProfileLevelMapper580     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
581         return sVp9Levels.map(from, to);
582     }
simpleMap__anon5ab1e7200111::Vp9ProfileLevelMapper583     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
584         return sVp9Levels.map(from, to);
585     }
simpleMap__anon5ab1e7200111::Vp9ProfileLevelMapper586     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
587         return mIsHdr10Plus ? sVp9Hdr10PlusProfiles.map(from, to) :
588                      mIsHdr ? sVp9HdrProfiles.map(from, to) :
589                               sVp9Profiles.map(from, to);
590     }
simpleMap__anon5ab1e7200111::Vp9ProfileLevelMapper591     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
592         return mIsHdr10Plus ? sVp9Hdr10PlusProfiles.map(from, to) :
593                      mIsHdr ? sVp9HdrProfiles.map(from, to) :
594                               sVp9Profiles.map(from, to);
595     }
596 
597 private:
598     bool mIsHdr;
599     bool mIsHdr10Plus;
600 };
601 
602 struct Av1ProfileLevelMapper : ProfileLevelMapperHelper {
Av1ProfileLevelMapper__anon5ab1e7200111::Av1ProfileLevelMapper603     Av1ProfileLevelMapper(bool isHdr = false, bool isHdr10Plus = false) :
604         ProfileLevelMapperHelper(),
605         mIsHdr(isHdr), mIsHdr10Plus(isHdr10Plus) {}
606 
simpleMap__anon5ab1e7200111::Av1ProfileLevelMapper607     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
608         return sAv1Levels.map(from, to);
609     }
simpleMap__anon5ab1e7200111::Av1ProfileLevelMapper610     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
611         return sAv1Levels.map(from, to);
612     }
simpleMap__anon5ab1e7200111::Av1ProfileLevelMapper613     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
614         return mIsHdr10Plus ? sAv1Hdr10PlusProfiles.map(from, to) :
615                      mIsHdr ? sAv1HdrProfiles.map(from, to) :
616                               sAv1Profiles.map(from, to);
617     }
simpleMap__anon5ab1e7200111::Av1ProfileLevelMapper618     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
619         return mIsHdr10Plus ? sAv1Hdr10PlusProfiles.map(from, to) :
620                      mIsHdr ? sAv1HdrProfiles.map(from, to) :
621                               sAv1Profiles.map(from, to);
622     }
623 
624 private:
625     bool mIsHdr;
626     bool mIsHdr10Plus;
627 };
628 
629 } // namespace
630 
631 // static
632 std::shared_ptr<C2Mapper::ProfileLevelMapper>
GetProfileLevelMapper(std::string mediaType)633 C2Mapper::GetProfileLevelMapper(std::string mediaType) {
634     std::transform(mediaType.begin(), mediaType.end(), mediaType.begin(), ::tolower);
635     if (mediaType == MIMETYPE_AUDIO_AAC) {
636         return std::make_shared<AacProfileLevelMapper>();
637     } else if (mediaType == MIMETYPE_VIDEO_AVC) {
638         return std::make_shared<AvcProfileLevelMapper>();
639     } else if (mediaType == MIMETYPE_VIDEO_DOLBY_VISION) {
640         return std::make_shared<DolbyVisionProfileLevelMapper>();
641     } else if (mediaType == MIMETYPE_VIDEO_H263) {
642         return std::make_shared<H263ProfileLevelMapper>();
643     } else if (mediaType == MIMETYPE_VIDEO_HEVC) {
644         return std::make_shared<HevcProfileLevelMapper>();
645     } else if (mediaType == MIMETYPE_VIDEO_MPEG2) {
646         return std::make_shared<Mpeg2ProfileLevelMapper>();
647     } else if (mediaType == MIMETYPE_VIDEO_MPEG4) {
648         return std::make_shared<Mpeg4ProfileLevelMapper>();
649     } else if (mediaType == MIMETYPE_VIDEO_VP8) {
650         return std::make_shared<Vp8ProfileLevelMapper>();
651     } else if (mediaType == MIMETYPE_VIDEO_VP9) {
652         return std::make_shared<Vp9ProfileLevelMapper>();
653     } else if (mediaType == MIMETYPE_VIDEO_AV1) {
654         return std::make_shared<Av1ProfileLevelMapper>();
655     }
656     return nullptr;
657 }
658 
659 // static
660 std::shared_ptr<C2Mapper::ProfileLevelMapper>
GetHdrProfileLevelMapper(std::string mediaType,bool isHdr10Plus)661 C2Mapper::GetHdrProfileLevelMapper(std::string mediaType, bool isHdr10Plus) {
662     std::transform(mediaType.begin(), mediaType.end(), mediaType.begin(), ::tolower);
663     if (mediaType == MIMETYPE_VIDEO_HEVC) {
664         return std::make_shared<HevcProfileLevelMapper>(true, isHdr10Plus);
665     } else if (mediaType == MIMETYPE_VIDEO_VP9) {
666         return std::make_shared<Vp9ProfileLevelMapper>(true, isHdr10Plus);
667     } else if (mediaType == MIMETYPE_VIDEO_AV1) {
668         return std::make_shared<Av1ProfileLevelMapper>(true, isHdr10Plus);
669     }
670     return nullptr;
671 }
672 
673 // static
map(C2Config::bitrate_mode_t from,int32_t * to)674 bool C2Mapper::map(C2Config::bitrate_mode_t from, int32_t *to) {
675     return sBitrateModes.map(from, to);
676 }
677 
678 // static
map(int32_t from,C2Config::bitrate_mode_t * to)679 bool C2Mapper::map(int32_t from, C2Config::bitrate_mode_t *to) {
680     return sBitrateModes.map(from, to);
681 }
682 
683 // static
map(C2Config::pcm_encoding_t from,int32_t * to)684 bool C2Mapper::map(C2Config::pcm_encoding_t from, int32_t *to) {
685     return sPcmEncodings.map(from, to);
686 }
687 
688 // static
map(int32_t from,C2Config::pcm_encoding_t * to)689 bool C2Mapper::map(int32_t from, C2Config::pcm_encoding_t *to) {
690     return sPcmEncodings.map(from, to);
691 }
692 
693 // static
map(C2Color::range_t from,int32_t * to)694 bool C2Mapper::map(C2Color::range_t from, int32_t *to) {
695     bool res = true;
696     // map SDK defined values directly. For other values, use wrapping from ColorUtils.
697     if (!sColorRanges.map(from, to)) {
698         ColorAspects::Range sfRange;
699 
700         // map known constants and keep vendor extensions. all other values are mapped to 'Other'
701         if (!sColorRangesSf.map(from, &sfRange)) {
702             // use static cast and ensure it is in the extension range
703             if (from < C2Color::RANGE_VENDOR_START || from > C2Color::RANGE_OTHER) {
704                 sfRange = ColorAspects::RangeOther;
705                 res = false;
706             }
707         }
708 
709         *to = ColorUtils::wrapColorAspectsIntoColorRange(sfRange);
710     }
711     return res;
712 }
713 
714 // static
map(int32_t from,C2Color::range_t * to)715 bool C2Mapper::map(int32_t from, C2Color::range_t *to) {
716     // map SDK defined values directly. For other values, use wrapping from ColorUtils.
717     if (!sColorRanges.map(from, to)) {
718         ColorAspects::Range sfRange;
719         (void)ColorUtils::unwrapColorAspectsFromColorRange(from, &sfRange);
720 
721         // map known constants and keep vendor extensions. all other values are mapped to 'Other'
722         if (!sColorRangesSf.map(sfRange, to)) {
723             // use static cast and ensure it is in the extension range
724             *to = (C2Color::range_t)sfRange;
725             if (*to < C2Color::RANGE_VENDOR_START || *to > C2Color::RANGE_OTHER) {
726                 *to = C2Color::RANGE_OTHER;
727                 return false;
728             }
729         }
730     }
731 
732     return true;
733 }
734 
735 // static
map(C2Color::range_t from,ColorAspects::Range * to)736 bool C2Mapper::map(C2Color::range_t from, ColorAspects::Range *to) {
737     return sColorRangesSf.map(from, to);
738 }
739 
740 // static
map(ColorAspects::Range from,C2Color::range_t * to)741 bool C2Mapper::map(ColorAspects::Range from, C2Color::range_t *to) {
742     return sColorRangesSf.map(from, to);
743 }
744 
745 // static
map(C2Color::primaries_t primaries,C2Color::matrix_t matrix,int32_t * standard)746 bool C2Mapper::map(C2Color::primaries_t primaries, C2Color::matrix_t matrix, int32_t *standard) {
747     ColorAspects::Primaries sfPrimaries;
748     ColorAspects::MatrixCoeffs sfMatrix;
749     bool res = true;
750 
751     // map known constants and keep vendor extensions. all other values are mapped to 'Other'
752     if (!sColorPrimariesSf.map(primaries, &sfPrimaries)) {
753         // ensure it is in the extension range and use static cast
754         if (primaries < C2Color::PRIMARIES_VENDOR_START || primaries > C2Color::PRIMARIES_OTHER) {
755             // undefined non-extension values map to 'Other'
756             sfPrimaries = ColorAspects::PrimariesOther;
757             res = false;
758         } else {
759             sfPrimaries = (ColorAspects::Primaries)primaries;
760         }
761     }
762 
763     if (!sColorMatricesSf.map(matrix, &sfMatrix)) {
764         // use static cast and ensure it is in the extension range
765         if (matrix < C2Color::MATRIX_VENDOR_START || matrix > C2Color::MATRIX_OTHER) {
766             // undefined non-extension values map to 'Other'
767             sfMatrix = ColorAspects::MatrixOther;
768             res = false;
769         } else {
770             sfMatrix = (ColorAspects::MatrixCoeffs)matrix;
771         }
772     }
773 
774     *standard = ColorUtils::wrapColorAspectsIntoColorStandard(sfPrimaries, sfMatrix);
775 
776     return res;
777 }
778 
779 // static
map(int32_t standard,C2Color::primaries_t * primaries,C2Color::matrix_t * matrix)780 bool C2Mapper::map(int32_t standard, C2Color::primaries_t *primaries, C2Color::matrix_t *matrix) {
781     // first map to stagefright foundation aspects => these actually map nearly 1:1 to
782     // Codec 2.0 aspects
783     ColorAspects::Primaries sfPrimaries;
784     ColorAspects::MatrixCoeffs sfMatrix;
785     bool res = true;
786     (void)ColorUtils::unwrapColorAspectsFromColorStandard(standard, &sfPrimaries, &sfMatrix);
787 
788     // map known constants and keep vendor extensions. all other values are mapped to 'Other'
789     if (!sColorPrimariesSf.map(sfPrimaries, primaries)) {
790         // use static cast and ensure it is in the extension range
791         *primaries = (C2Color::primaries_t)sfPrimaries;
792         if (*primaries < C2Color::PRIMARIES_VENDOR_START || *primaries > C2Color::PRIMARIES_OTHER) {
793             *primaries = C2Color::PRIMARIES_OTHER;
794             res = false;
795         }
796     }
797 
798     if (!sColorMatricesSf.map(sfMatrix, matrix)) {
799         // use static cast and ensure it is in the extension range
800         *matrix = (C2Color::matrix_t)sfMatrix;
801         if (*matrix < C2Color::MATRIX_VENDOR_START || *matrix > C2Color::MATRIX_OTHER) {
802             *matrix = C2Color::MATRIX_OTHER;
803             res = false;
804         }
805     }
806 
807     return res;
808 }
809 
810 // static
map(C2Color::primaries_t from,ColorAspects::Primaries * to)811 bool C2Mapper::map(C2Color::primaries_t from, ColorAspects::Primaries *to) {
812     return sColorPrimariesSf.map(from, to);
813 }
814 
815 // static
map(ColorAspects::Primaries from,C2Color::primaries_t * to)816 bool C2Mapper::map(ColorAspects::Primaries from, C2Color::primaries_t *to) {
817     return sColorPrimariesSf.map(from, to);
818 }
819 
820 // static
map(C2Color::matrix_t from,ColorAspects::MatrixCoeffs * to)821 bool C2Mapper::map(C2Color::matrix_t from, ColorAspects::MatrixCoeffs *to) {
822     return sColorMatricesSf.map(from, to);
823 }
824 
825 // static
map(ColorAspects::MatrixCoeffs from,C2Color::matrix_t * to)826 bool C2Mapper::map(ColorAspects::MatrixCoeffs from, C2Color::matrix_t *to) {
827     return sColorMatricesSf.map(from, to);
828 }
829 
830 // static
map(C2Color::transfer_t from,int32_t * to)831 bool C2Mapper::map(C2Color::transfer_t from, int32_t *to) {
832     bool res = true;
833     // map SDK defined values directly. For other values, use wrapping from ColorUtils.
834     if (!sColorTransfers.map(from, to)) {
835         ColorAspects::Transfer sfTransfer;
836 
837         // map known constants and keep vendor extensions. all other values are mapped to 'Other'
838         if (!sColorTransfersSf.map(from, &sfTransfer)) {
839             // use static cast and ensure it is in the extension range
840             if (from < C2Color::TRANSFER_VENDOR_START || from > C2Color::TRANSFER_OTHER) {
841                 sfTransfer = ColorAspects::TransferOther;
842                 res = false;
843             }
844         }
845 
846         *to = ColorUtils::wrapColorAspectsIntoColorTransfer(sfTransfer);
847     }
848     return res;
849 }
850 
851 // static
map(int32_t from,C2Color::transfer_t * to)852 bool C2Mapper::map(int32_t from, C2Color::transfer_t *to) {
853     // map SDK defined values directly. For other values, use wrapping from ColorUtils.
854     if (!sColorTransfers.map(from, to)) {
855         ColorAspects::Transfer sfTransfer;
856         (void)ColorUtils::unwrapColorAspectsFromColorTransfer(from, &sfTransfer);
857 
858         // map known constants and keep vendor extensions. all other values are mapped to 'Other'
859         if (!sColorTransfersSf.map(sfTransfer, to)) {
860             // use static cast and ensure it is in the extension range
861             *to = (C2Color::transfer_t)sfTransfer;
862             if (*to < C2Color::TRANSFER_VENDOR_START || *to > C2Color::TRANSFER_OTHER) {
863                 *to = C2Color::TRANSFER_OTHER;
864                 return false;
865             }
866         }
867     }
868 
869     return true;
870 }
871 
872 // static
map(C2Color::range_t range,C2Color::primaries_t primaries,C2Color::matrix_t matrix,C2Color::transfer_t transfer,uint32_t * dataSpace)873 bool C2Mapper::map(
874         C2Color::range_t range, C2Color::primaries_t primaries,
875         C2Color::matrix_t matrix, C2Color::transfer_t transfer, uint32_t *dataSpace) {
876 #if 0
877     // pure reimplementation
878     *dataSpace = HAL_DATASPACE_UNKNOWN; // this is 0
879 
880     switch (range) {
881         case C2Color::RANGE_FULL:    *dataSpace |= HAL_DATASPACE_RANGE_FULL;    break;
882         case C2Color::RANGE_LIMITED: *dataSpace |= HAL_DATASPACE_RANGE_LIMITED; break;
883         default: break;
884     }
885 
886     switch (transfer) {
887         case C2Color::TRANSFER_LINEAR:  *dataSpace |= HAL_DATASPACE_TRANSFER_LINEAR;     break;
888         case C2Color::TRANSFER_SRGB:    *dataSpace |= HAL_DATASPACE_TRANSFER_SRGB;       break;
889         case C2Color::TRANSFER_170M:    *dataSpace |= HAL_DATASPACE_TRANSFER_SMPTE_170M; break;
890         case C2Color::TRANSFER_GAMMA22: *dataSpace |= HAL_DATASPACE_TRANSFER_GAMMA2_2;   break;
891         case C2Color::TRANSFER_GAMMA28: *dataSpace |= HAL_DATASPACE_TRANSFER_GAMMA2_8;   break;
892         case C2Color::TRANSFER_ST2084:  *dataSpace |= HAL_DATASPACE_TRANSFER_ST2084;     break;
893         case C2Color::TRANSFER_HLG:     *dataSpace |= HAL_DATASPACE_TRANSFER_HLG;        break;
894         default: break;
895     }
896 
897     switch (primaries) {
898         case C2Color::PRIMARIES_BT601_525:
899             *dataSpace |= (matrix == C2Color::MATRIX_240M
900                             || matrix == C2Color::MATRIX_BT709)
901                     ? HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED
902                     : HAL_DATASPACE_STANDARD_BT601_525;
903             break;
904         case C2Color::PRIMARIES_BT601_625:
905             *dataSpace |= (matrix == C2Color::MATRIX_240M
906                             || matrix == C2Color::MATRIX_BT709)
907                     ? HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED
908                     : HAL_DATASPACE_STANDARD_BT601_625;
909             break;
910         case C2Color::PRIMARIES_BT2020:
911             *dataSpace |= (matrix == C2Color::MATRIX_BT2020_CONSTANT
912                     ? HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE
913                     : HAL_DATASPACE_STANDARD_BT2020);
914             break;
915         case C2Color::PRIMARIES_BT470_M:
916             *dataSpace |= HAL_DATASPACE_STANDARD_BT470M;
917             break;
918         case C2Color::PRIMARIES_BT709:
919             *dataSpace |= HAL_DATASPACE_STANDARD_BT709;
920             break;
921         default: break;
922     }
923 #else
924     // for now use legacy implementation
925     ColorAspects aspects;
926     if (!sColorRangesSf.map(range, &aspects.mRange)) {
927         aspects.mRange = ColorAspects::RangeUnspecified;
928     }
929     if (!sColorPrimariesSf.map(primaries, &aspects.mPrimaries)) {
930         aspects.mPrimaries = ColorAspects::PrimariesUnspecified;
931     }
932     if (!sColorMatricesSf.map(matrix, &aspects.mMatrixCoeffs)) {
933         aspects.mMatrixCoeffs = ColorAspects::MatrixUnspecified;
934     }
935     if (!sColorTransfersSf.map(transfer, &aspects.mTransfer)) {
936         aspects.mTransfer = ColorAspects::TransferUnspecified;
937     }
938     *dataSpace = ColorUtils::getDataSpaceForColorAspects(aspects, true /* mayExpand */);
939 #endif
940     return true;
941 }
942 
943 // static
map(C2Color::transfer_t from,ColorAspects::Transfer * to)944 bool C2Mapper::map(C2Color::transfer_t from, ColorAspects::Transfer *to) {
945     return sColorTransfersSf.map(from, to);
946 }
947 
948 // static
map(ColorAspects::Transfer from,C2Color::transfer_t * to)949 bool C2Mapper::map(ColorAspects::Transfer from, C2Color::transfer_t *to) {
950     return sColorTransfersSf.map(from, to);
951 }
952 
953 // static
mapPixelFormatFrameworkToCodec(int32_t frameworkValue,uint32_t * c2Value)954 bool C2Mapper::mapPixelFormatFrameworkToCodec(
955         int32_t frameworkValue, uint32_t *c2Value) {
956     switch (frameworkValue) {
957         case COLOR_FormatSurface:
958             *c2Value = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
959             return true;
960         case COLOR_FormatYUV420Flexible:
961             *c2Value = HAL_PIXEL_FORMAT_YCBCR_420_888;
962             return true;
963         case COLOR_FormatYUV420Planar:
964         case COLOR_FormatYUV420SemiPlanar:
965         case COLOR_FormatYUV420PackedPlanar:
966         case COLOR_FormatYUV420PackedSemiPlanar:
967             *c2Value = HAL_PIXEL_FORMAT_YV12;
968             return true;
969         default:
970             // TODO: support some sort of passthrough
971             return false;
972     }
973 }
974 
975 // static
mapPixelFormatCodecToFramework(uint32_t c2Value,int32_t * frameworkValue)976 bool C2Mapper::mapPixelFormatCodecToFramework(
977         uint32_t c2Value, int32_t *frameworkValue) {
978     switch (c2Value) {
979         case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
980             *frameworkValue = COLOR_FormatSurface;
981             return true;
982         case HAL_PIXEL_FORMAT_YV12:
983         case HAL_PIXEL_FORMAT_YCBCR_420_888:
984             *frameworkValue = COLOR_FormatYUV420Flexible;
985             return true;
986         default:
987             return false;
988     }
989 }
990