• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
194 
195 ALookup<C2Config::level_t, int32_t> sH263Levels = {
196     { C2Config::LEVEL_H263_10, H263Level10 },
197     { C2Config::LEVEL_H263_20, H263Level20 },
198     { C2Config::LEVEL_H263_30, H263Level30 },
199     { C2Config::LEVEL_H263_40, H263Level40 },
200     { C2Config::LEVEL_H263_45, H263Level45 },
201     { C2Config::LEVEL_H263_50, H263Level50 },
202     { C2Config::LEVEL_H263_60, H263Level60 },
203     { C2Config::LEVEL_H263_70, H263Level70 },
204 };
205 
206 ALookup<C2Config::profile_t, int32_t> sH263Profiles = {
207     { C2Config::PROFILE_H263_BASELINE,          H263ProfileBaseline },
208     { C2Config::PROFILE_H263_H320,              H263ProfileH320Coding },
209     { C2Config::PROFILE_H263_V1BC,              H263ProfileBackwardCompatible },
210     { C2Config::PROFILE_H263_ISWV2,             H263ProfileISWV2 },
211     { C2Config::PROFILE_H263_ISWV3,             H263ProfileISWV3 },
212     { C2Config::PROFILE_H263_HIGH_COMPRESSION,  H263ProfileHighCompression },
213     { C2Config::PROFILE_H263_INTERNET,          H263ProfileInternet },
214     { C2Config::PROFILE_H263_INTERLACE,         H263ProfileInterlace },
215     { C2Config::PROFILE_H263_HIGH_LATENCY,      H263ProfileHighLatency },
216 };
217 
218 ALookup<C2Config::level_t, int32_t> sHevcLevels = {
219     { C2Config::LEVEL_HEVC_MAIN_1,      HEVCMainTierLevel1 },
220     { C2Config::LEVEL_HEVC_MAIN_2,      HEVCMainTierLevel2 },
221     { C2Config::LEVEL_HEVC_MAIN_2_1,    HEVCMainTierLevel21 },
222     { C2Config::LEVEL_HEVC_MAIN_3,      HEVCMainTierLevel3 },
223     { C2Config::LEVEL_HEVC_MAIN_3_1,    HEVCMainTierLevel31 },
224     { C2Config::LEVEL_HEVC_MAIN_4,      HEVCMainTierLevel4 },
225     { C2Config::LEVEL_HEVC_MAIN_4_1,    HEVCMainTierLevel41 },
226     { C2Config::LEVEL_HEVC_MAIN_5,      HEVCMainTierLevel5 },
227     { C2Config::LEVEL_HEVC_MAIN_5_1,    HEVCMainTierLevel51 },
228     { C2Config::LEVEL_HEVC_MAIN_5_2,    HEVCMainTierLevel52 },
229     { C2Config::LEVEL_HEVC_MAIN_6,      HEVCMainTierLevel6 },
230     { C2Config::LEVEL_HEVC_MAIN_6_1,    HEVCMainTierLevel61 },
231     { C2Config::LEVEL_HEVC_MAIN_6_2,    HEVCMainTierLevel62 },
232 
233     { C2Config::LEVEL_HEVC_HIGH_4,      HEVCHighTierLevel4 },
234     { C2Config::LEVEL_HEVC_HIGH_4_1,    HEVCHighTierLevel41 },
235     { C2Config::LEVEL_HEVC_HIGH_5,      HEVCHighTierLevel5 },
236     { C2Config::LEVEL_HEVC_HIGH_5_1,    HEVCHighTierLevel51 },
237     { C2Config::LEVEL_HEVC_HIGH_5_2,    HEVCHighTierLevel52 },
238     { C2Config::LEVEL_HEVC_HIGH_6,      HEVCHighTierLevel6 },
239     { C2Config::LEVEL_HEVC_HIGH_6_1,    HEVCHighTierLevel61 },
240     { C2Config::LEVEL_HEVC_HIGH_6_2,    HEVCHighTierLevel62 },
241 
242     // map high tier levels below 4 to main tier
243     { C2Config::LEVEL_HEVC_MAIN_1,      HEVCHighTierLevel1 },
244     { C2Config::LEVEL_HEVC_MAIN_2,      HEVCHighTierLevel2 },
245     { C2Config::LEVEL_HEVC_MAIN_2_1,    HEVCHighTierLevel21 },
246     { C2Config::LEVEL_HEVC_MAIN_3,      HEVCHighTierLevel3 },
247     { C2Config::LEVEL_HEVC_MAIN_3_1,    HEVCHighTierLevel31 },
248 };
249 
250 ALookup<C2Config::profile_t, int32_t> sHevcProfiles = {
251     { C2Config::PROFILE_HEVC_MAIN, HEVCProfileMain },
252     { C2Config::PROFILE_HEVC_MAIN_10, HEVCProfileMain10 },
253     { C2Config::PROFILE_HEVC_MAIN_STILL, HEVCProfileMainStill },
254     { C2Config::PROFILE_HEVC_MAIN_INTRA, HEVCProfileMain },
255     { C2Config::PROFILE_HEVC_MAIN_10_INTRA, HEVCProfileMain10 },
256 };
257 
258 ALookup<C2Config::profile_t, int32_t> sHevcHdrProfiles = {
259     { C2Config::PROFILE_HEVC_MAIN_10, HEVCProfileMain10HDR10 },
260 };
261 
262 ALookup<C2Config::profile_t, int32_t> sHevcHdr10PlusProfiles = {
263     { C2Config::PROFILE_HEVC_MAIN_10, HEVCProfileMain10HDR10Plus },
264 };
265 
266 ALookup<C2Config::level_t, int32_t> sMpeg2Levels = {
267     { C2Config::LEVEL_MP2V_LOW,         MPEG2LevelLL },
268     { C2Config::LEVEL_MP2V_MAIN,        MPEG2LevelML },
269     { C2Config::LEVEL_MP2V_HIGH_1440,   MPEG2LevelH14 },
270     { C2Config::LEVEL_MP2V_HIGH,        MPEG2LevelHL },
271     { C2Config::LEVEL_MP2V_HIGHP,       MPEG2LevelHP },
272 };
273 
274 ALookup<C2Config::profile_t, int32_t> sMpeg2Profiles = {
275     { C2Config::PROFILE_MP2V_SIMPLE,                MPEG2ProfileSimple },
276     { C2Config::PROFILE_MP2V_MAIN,                  MPEG2ProfileMain },
277     { C2Config::PROFILE_MP2V_SNR_SCALABLE,          MPEG2ProfileSNR },
278     { C2Config::PROFILE_MP2V_SPATIALLY_SCALABLE,    MPEG2ProfileSpatial },
279     { C2Config::PROFILE_MP2V_HIGH,                  MPEG2ProfileHigh },
280     { C2Config::PROFILE_MP2V_422,                   MPEG2Profile422 },
281 };
282 
283 ALookup<C2Config::level_t, int32_t> sMpeg4Levels = {
284     { C2Config::LEVEL_MP4V_0,   MPEG4Level0 },
285     { C2Config::LEVEL_MP4V_0B,  MPEG4Level0b },
286     { C2Config::LEVEL_MP4V_1,   MPEG4Level1 },
287     { C2Config::LEVEL_MP4V_2,   MPEG4Level2 },
288     { C2Config::LEVEL_MP4V_3,   MPEG4Level3 },
289     { C2Config::LEVEL_MP4V_3B,  MPEG4Level3b },
290     { C2Config::LEVEL_MP4V_4,   MPEG4Level4 },
291     { C2Config::LEVEL_MP4V_4A,  MPEG4Level4a },
292     { C2Config::LEVEL_MP4V_5,   MPEG4Level5 },
293     { C2Config::LEVEL_MP4V_6,   MPEG4Level6 },
294 };
295 
296 ALookup<C2Config::profile_t, int32_t> sMpeg4Profiles = {
297     { C2Config::PROFILE_MP4V_SIMPLE,            MPEG4ProfileSimple },
298     { C2Config::PROFILE_MP4V_SIMPLE_SCALABLE,   MPEG4ProfileSimpleScalable },
299     { C2Config::PROFILE_MP4V_CORE,              MPEG4ProfileCore },
300     { C2Config::PROFILE_MP4V_MAIN,              MPEG4ProfileMain },
301     { C2Config::PROFILE_MP4V_NBIT,              MPEG4ProfileNbit },
302     { C2Config::PROFILE_MP4V_ARTS,              MPEG4ProfileAdvancedRealTime },
303     { C2Config::PROFILE_MP4V_CORE_SCALABLE,     MPEG4ProfileCoreScalable },
304     { C2Config::PROFILE_MP4V_ACE,               MPEG4ProfileAdvancedCoding },
305     { C2Config::PROFILE_MP4V_ADVANCED_CORE,     MPEG4ProfileAdvancedCore },
306     { C2Config::PROFILE_MP4V_ADVANCED_SIMPLE,   MPEG4ProfileAdvancedSimple },
307 };
308 
309 ALookup<C2Config::pcm_encoding_t, int32_t> sPcmEncodings = {
310     { C2Config::PCM_8, kAudioEncodingPcm8bit },
311     { C2Config::PCM_16, kAudioEncodingPcm16bit },
312     { C2Config::PCM_FLOAT, kAudioEncodingPcmFloat },
313 };
314 
315 ALookup<C2Config::level_t, int32_t> sVp9Levels = {
316     { C2Config::LEVEL_VP9_1,    VP9Level1 },
317     { C2Config::LEVEL_VP9_1_1,  VP9Level11 },
318     { C2Config::LEVEL_VP9_2,    VP9Level2 },
319     { C2Config::LEVEL_VP9_2_1,  VP9Level21 },
320     { C2Config::LEVEL_VP9_3,    VP9Level3 },
321     { C2Config::LEVEL_VP9_3_1,  VP9Level31 },
322     { C2Config::LEVEL_VP9_4,    VP9Level4 },
323     { C2Config::LEVEL_VP9_4_1,  VP9Level41 },
324     { C2Config::LEVEL_VP9_5,    VP9Level5 },
325     { C2Config::LEVEL_VP9_5_1,  VP9Level51 },
326     { C2Config::LEVEL_VP9_5_2,  VP9Level52 },
327     { C2Config::LEVEL_VP9_6,    VP9Level6 },
328     { C2Config::LEVEL_VP9_6_1,  VP9Level61 },
329     { C2Config::LEVEL_VP9_6_2,  VP9Level62 },
330 };
331 
332 ALookup<C2Config::profile_t, int32_t> sVp9Profiles = {
333     { C2Config::PROFILE_VP9_0, VP9Profile0 },
334     { C2Config::PROFILE_VP9_1, VP9Profile1 },
335     { C2Config::PROFILE_VP9_2, VP9Profile2 },
336     { C2Config::PROFILE_VP9_3, VP9Profile3 },
337     { C2Config::PROFILE_VP9_2, VP9Profile2HDR },
338     { C2Config::PROFILE_VP9_3, VP9Profile3HDR },
339     { C2Config::PROFILE_VP9_2, VP9Profile2HDR10Plus },
340     { C2Config::PROFILE_VP9_3, VP9Profile3HDR10Plus },
341 };
342 
343 ALookup<C2Config::profile_t, int32_t> sVp9HdrProfiles = {
344     { C2Config::PROFILE_VP9_2, VP9Profile2HDR },
345     { C2Config::PROFILE_VP9_3, VP9Profile3HDR },
346 };
347 
348 ALookup<C2Config::profile_t, int32_t> sVp9Hdr10PlusProfiles = {
349     { C2Config::PROFILE_VP9_2, VP9Profile2HDR10Plus },
350     { C2Config::PROFILE_VP9_3, VP9Profile3HDR10Plus },
351 };
352 
353 ALookup<C2Config::level_t, int32_t> sAv1Levels = {
354     { C2Config::LEVEL_AV1_2,    AV1Level2  },
355     { C2Config::LEVEL_AV1_2_1,  AV1Level21 },
356     { C2Config::LEVEL_AV1_2_2,  AV1Level22 },
357     { C2Config::LEVEL_AV1_2_3,  AV1Level23 },
358     { C2Config::LEVEL_AV1_3,    AV1Level3  },
359     { C2Config::LEVEL_AV1_3_1,  AV1Level31 },
360     { C2Config::LEVEL_AV1_3_2,  AV1Level32 },
361     { C2Config::LEVEL_AV1_3_3,  AV1Level33 },
362     { C2Config::LEVEL_AV1_4,    AV1Level4  },
363     { C2Config::LEVEL_AV1_4_1,  AV1Level41 },
364     { C2Config::LEVEL_AV1_4_2,  AV1Level42 },
365     { C2Config::LEVEL_AV1_4_3,  AV1Level43 },
366     { C2Config::LEVEL_AV1_5,    AV1Level5  },
367     { C2Config::LEVEL_AV1_5_1,  AV1Level51 },
368     { C2Config::LEVEL_AV1_5_2,  AV1Level52 },
369     { C2Config::LEVEL_AV1_5_3,  AV1Level53 },
370     { C2Config::LEVEL_AV1_6,    AV1Level6  },
371     { C2Config::LEVEL_AV1_6_1,  AV1Level61 },
372     { C2Config::LEVEL_AV1_6_2,  AV1Level62 },
373     { C2Config::LEVEL_AV1_6_3,  AV1Level63 },
374     { C2Config::LEVEL_AV1_7,    AV1Level7  },
375     { C2Config::LEVEL_AV1_7_1,  AV1Level71 },
376     { C2Config::LEVEL_AV1_7_2,  AV1Level72 },
377     { C2Config::LEVEL_AV1_7_3,  AV1Level73 },
378 };
379 
380 
381 ALookup<C2Config::profile_t, int32_t> sAv1Profiles = {
382     // TODO: will need to disambiguate between Main8 and Main10
383     { C2Config::PROFILE_AV1_0, AV1ProfileMain8 },
384     { C2Config::PROFILE_AV1_0, AV1ProfileMain10 },
385 };
386 
387 ALookup<C2Config::profile_t, int32_t> sAv1HdrProfiles = {
388     { C2Config::PROFILE_AV1_0, AV1ProfileMain10 },
389     { C2Config::PROFILE_AV1_0, AV1ProfileMain10HDR10 },
390 };
391 
392 ALookup<C2Config::profile_t, int32_t> sAv1Hdr10PlusProfiles = {
393     { C2Config::PROFILE_AV1_0, AV1ProfileMain10HDR10Plus },
394 };
395 
396 /**
397  * A helper that passes through vendor extension profile and level values.
398  */
399 struct ProfileLevelMapperHelper : C2Mapper::ProfileLevelMapper {
400     virtual bool simpleMap(C2Config::level_t from, int32_t *to) = 0;
401     virtual bool simpleMap(int32_t from, C2Config::level_t *to) = 0;
402     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) = 0;
403     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) = 0;
404 
405     template<typename T, typename U>
passThroughMap__anoned7743a90111::ProfileLevelMapperHelper406     bool passThroughMap(T from, U *to) {
407         // allow (and pass through) vendor extensions
408         if (from >= (T)C2_PROFILE_LEVEL_VENDOR_START && from < (T)INT32_MAX) {
409             *to = (U)from;
410             return true;
411         }
412         return simpleMap(from, to);
413     }
414 
mapLevel__anoned7743a90111::ProfileLevelMapperHelper415     virtual bool mapLevel(C2Config::level_t from, int32_t *to) {
416         return passThroughMap(from, to);
417     }
418 
mapLevel__anoned7743a90111::ProfileLevelMapperHelper419     virtual bool mapLevel(int32_t from, C2Config::level_t *to) {
420         return passThroughMap(from, to);
421     }
422 
mapProfile__anoned7743a90111::ProfileLevelMapperHelper423     virtual bool mapProfile(C2Config::profile_t from, int32_t *to) {
424         return passThroughMap(from, to);
425     }
426 
mapProfile__anoned7743a90111::ProfileLevelMapperHelper427     virtual bool mapProfile(int32_t from, C2Config::profile_t *to) {
428         return passThroughMap(from, to);
429     }
430 };
431 
432 // AAC only uses profiles, map all levels to unused or 0
433 struct AacProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anoned7743a90111::AacProfileLevelMapper434     virtual bool simpleMap(C2Config::level_t, int32_t *to) {
435         *to = 0;
436         return true;
437     }
simpleMap__anoned7743a90111::AacProfileLevelMapper438     virtual bool simpleMap(int32_t, C2Config::level_t *to) {
439         *to = C2Config::LEVEL_UNUSED;
440         return true;
441     }
simpleMap__anoned7743a90111::AacProfileLevelMapper442     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
443         return sAacProfiles.map(from, to);
444     }
simpleMap__anoned7743a90111::AacProfileLevelMapper445     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
446         return sAacProfiles.map(from, to);
447     }
448 };
449 
450 struct AvcProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anoned7743a90111::AvcProfileLevelMapper451     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
452         return sAvcLevels.map(from, to);
453     }
simpleMap__anoned7743a90111::AvcProfileLevelMapper454     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
455         return sAvcLevels.map(from, to);
456     }
simpleMap__anoned7743a90111::AvcProfileLevelMapper457     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
458         return sAvcProfiles.map(from, to);
459     }
simpleMap__anoned7743a90111::AvcProfileLevelMapper460     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
461         return sAvcProfiles.map(from, to);
462     }
463 };
464 
465 struct DolbyVisionProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anoned7743a90111::DolbyVisionProfileLevelMapper466     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
467         return sDolbyVisionLevels.map(from, to);
468     }
simpleMap__anoned7743a90111::DolbyVisionProfileLevelMapper469     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
470         return sDolbyVisionLevels.map(from, to);
471     }
simpleMap__anoned7743a90111::DolbyVisionProfileLevelMapper472     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
473         return sDolbyVisionProfiles.map(from, to);
474     }
simpleMap__anoned7743a90111::DolbyVisionProfileLevelMapper475     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
476         return sDolbyVisionProfiles.map(from, to);
477     }
478 };
479 
480 struct H263ProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anoned7743a90111::H263ProfileLevelMapper481     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
482         return sH263Levels.map(from, to);
483     }
simpleMap__anoned7743a90111::H263ProfileLevelMapper484     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
485         return sH263Levels.map(from, to);
486     }
simpleMap__anoned7743a90111::H263ProfileLevelMapper487     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
488         return sH263Profiles.map(from, to);
489     }
simpleMap__anoned7743a90111::H263ProfileLevelMapper490     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
491         return sH263Profiles.map(from, to);
492     }
493 };
494 
495 struct HevcProfileLevelMapper : ProfileLevelMapperHelper {
HevcProfileLevelMapper__anoned7743a90111::HevcProfileLevelMapper496     HevcProfileLevelMapper(bool isHdr = false, bool isHdr10Plus = false) :
497         ProfileLevelMapperHelper(),
498         mIsHdr(isHdr), mIsHdr10Plus(isHdr10Plus) {}
499 
simpleMap__anoned7743a90111::HevcProfileLevelMapper500     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
501         return sHevcLevels.map(from, to);
502     }
simpleMap__anoned7743a90111::HevcProfileLevelMapper503     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
504         return sHevcLevels.map(from, to);
505     }
simpleMap__anoned7743a90111::HevcProfileLevelMapper506     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
507         return mIsHdr10Plus ? sHevcHdr10PlusProfiles.map(from, to) :
508                      mIsHdr ? sHevcHdrProfiles.map(from, to) :
509                               sHevcProfiles.map(from, to);
510     }
simpleMap__anoned7743a90111::HevcProfileLevelMapper511     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
512         return mIsHdr10Plus ? sHevcHdr10PlusProfiles.map(from, to) :
513                      mIsHdr ? sHevcHdrProfiles.map(from, to) :
514                               sHevcProfiles.map(from, to);
515     }
516 
517 private:
518     bool mIsHdr;
519     bool mIsHdr10Plus;
520 };
521 
522 struct Mpeg2ProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anoned7743a90111::Mpeg2ProfileLevelMapper523     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
524         return sMpeg2Levels.map(from, to);
525     }
simpleMap__anoned7743a90111::Mpeg2ProfileLevelMapper526     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
527         return sMpeg2Levels.map(from, to);
528     }
simpleMap__anoned7743a90111::Mpeg2ProfileLevelMapper529     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
530         return sMpeg2Profiles.map(from, to);
531     }
simpleMap__anoned7743a90111::Mpeg2ProfileLevelMapper532     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
533         return sMpeg2Profiles.map(from, to);
534     }
535 };
536 
537 struct Mpeg4ProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anoned7743a90111::Mpeg4ProfileLevelMapper538     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
539         return sMpeg4Levels.map(from, to);
540     }
simpleMap__anoned7743a90111::Mpeg4ProfileLevelMapper541     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
542         return sMpeg4Levels.map(from, to);
543     }
simpleMap__anoned7743a90111::Mpeg4ProfileLevelMapper544     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
545         return sMpeg4Profiles.map(from, to);
546     }
simpleMap__anoned7743a90111::Mpeg4ProfileLevelMapper547     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
548         return sMpeg4Profiles.map(from, to);
549     }
550 };
551 
552 // VP8 has no profiles and levels in Codec 2.0, but we use main profile and level 0 in MediaCodec
553 // map all profiles and levels to that.
554 struct Vp8ProfileLevelMapper : ProfileLevelMapperHelper {
simpleMap__anoned7743a90111::Vp8ProfileLevelMapper555     virtual bool simpleMap(C2Config::level_t, int32_t *to) {
556         *to = VP8Level_Version0;
557         return true;
558     }
simpleMap__anoned7743a90111::Vp8ProfileLevelMapper559     virtual bool simpleMap(int32_t, C2Config::level_t *to) {
560         *to = C2Config::LEVEL_UNUSED;
561         return true;
562     }
simpleMap__anoned7743a90111::Vp8ProfileLevelMapper563     virtual bool simpleMap(C2Config::profile_t, int32_t *to) {
564         *to = VP8ProfileMain;
565         return true;
566     }
simpleMap__anoned7743a90111::Vp8ProfileLevelMapper567     virtual bool simpleMap(int32_t, C2Config::profile_t *to) {
568         *to = C2Config::PROFILE_UNUSED;
569         return true;
570     }
571 };
572 
573 struct Vp9ProfileLevelMapper : ProfileLevelMapperHelper {
Vp9ProfileLevelMapper__anoned7743a90111::Vp9ProfileLevelMapper574     Vp9ProfileLevelMapper(bool isHdr = false, bool isHdr10Plus = false) :
575         ProfileLevelMapperHelper(),
576         mIsHdr(isHdr), mIsHdr10Plus(isHdr10Plus) {}
577 
simpleMap__anoned7743a90111::Vp9ProfileLevelMapper578     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
579         return sVp9Levels.map(from, to);
580     }
simpleMap__anoned7743a90111::Vp9ProfileLevelMapper581     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
582         return sVp9Levels.map(from, to);
583     }
simpleMap__anoned7743a90111::Vp9ProfileLevelMapper584     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
585         return mIsHdr10Plus ? sVp9Hdr10PlusProfiles.map(from, to) :
586                      mIsHdr ? sVp9HdrProfiles.map(from, to) :
587                               sVp9Profiles.map(from, to);
588     }
simpleMap__anoned7743a90111::Vp9ProfileLevelMapper589     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
590         return mIsHdr10Plus ? sVp9Hdr10PlusProfiles.map(from, to) :
591                      mIsHdr ? sVp9HdrProfiles.map(from, to) :
592                               sVp9Profiles.map(from, to);
593     }
594 
595 private:
596     bool mIsHdr;
597     bool mIsHdr10Plus;
598 };
599 
600 struct Av1ProfileLevelMapper : ProfileLevelMapperHelper {
Av1ProfileLevelMapper__anoned7743a90111::Av1ProfileLevelMapper601     Av1ProfileLevelMapper(bool isHdr = false, bool isHdr10Plus = false) :
602         ProfileLevelMapperHelper(),
603         mIsHdr(isHdr), mIsHdr10Plus(isHdr10Plus) {}
604 
simpleMap__anoned7743a90111::Av1ProfileLevelMapper605     virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
606         return sAv1Levels.map(from, to);
607     }
simpleMap__anoned7743a90111::Av1ProfileLevelMapper608     virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
609         return sAv1Levels.map(from, to);
610     }
simpleMap__anoned7743a90111::Av1ProfileLevelMapper611     virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
612         return mIsHdr10Plus ? sAv1Hdr10PlusProfiles.map(from, to) :
613                      mIsHdr ? sAv1HdrProfiles.map(from, to) :
614                               sAv1Profiles.map(from, to);
615     }
simpleMap__anoned7743a90111::Av1ProfileLevelMapper616     virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
617         return mIsHdr10Plus ? sAv1Hdr10PlusProfiles.map(from, to) :
618                      mIsHdr ? sAv1HdrProfiles.map(from, to) :
619                               sAv1Profiles.map(from, to);
620     }
621 
622 private:
623     bool mIsHdr;
624     bool mIsHdr10Plus;
625 };
626 
627 } // namespace
628 
629 // static
630 std::shared_ptr<C2Mapper::ProfileLevelMapper>
GetProfileLevelMapper(std::string mediaType)631 C2Mapper::GetProfileLevelMapper(std::string mediaType) {
632     std::transform(mediaType.begin(), mediaType.begin(), mediaType.end(), ::tolower);
633     if (mediaType == MIMETYPE_AUDIO_AAC) {
634         return std::make_shared<AacProfileLevelMapper>();
635     } else if (mediaType == MIMETYPE_VIDEO_AVC) {
636         return std::make_shared<AvcProfileLevelMapper>();
637     } else if (mediaType == MIMETYPE_VIDEO_DOLBY_VISION) {
638         return std::make_shared<DolbyVisionProfileLevelMapper>();
639     } else if (mediaType == MIMETYPE_VIDEO_H263) {
640         return std::make_shared<H263ProfileLevelMapper>();
641     } else if (mediaType == MIMETYPE_VIDEO_HEVC) {
642         return std::make_shared<HevcProfileLevelMapper>();
643     } else if (mediaType == MIMETYPE_VIDEO_MPEG2) {
644         return std::make_shared<Mpeg2ProfileLevelMapper>();
645     } else if (mediaType == MIMETYPE_VIDEO_MPEG4) {
646         return std::make_shared<Mpeg4ProfileLevelMapper>();
647     } else if (mediaType == MIMETYPE_VIDEO_VP8) {
648         return std::make_shared<Vp8ProfileLevelMapper>();
649     } else if (mediaType == MIMETYPE_VIDEO_VP9) {
650         return std::make_shared<Vp9ProfileLevelMapper>();
651     } else if (mediaType == MIMETYPE_VIDEO_AV1) {
652         return std::make_shared<Av1ProfileLevelMapper>();
653     }
654     return nullptr;
655 }
656 
657 // static
658 std::shared_ptr<C2Mapper::ProfileLevelMapper>
GetHdrProfileLevelMapper(std::string mediaType,bool isHdr10Plus)659 C2Mapper::GetHdrProfileLevelMapper(std::string mediaType, bool isHdr10Plus) {
660     std::transform(mediaType.begin(), mediaType.begin(), mediaType.end(), ::tolower);
661     if (mediaType == MIMETYPE_VIDEO_HEVC) {
662         return std::make_shared<HevcProfileLevelMapper>(true, isHdr10Plus);
663     } else if (mediaType == MIMETYPE_VIDEO_VP9) {
664         return std::make_shared<Vp9ProfileLevelMapper>(true, isHdr10Plus);
665     }
666     return nullptr;
667 }
668 
669 // static
map(C2Config::bitrate_mode_t from,int32_t * to)670 bool C2Mapper::map(C2Config::bitrate_mode_t from, int32_t *to) {
671     return sBitrateModes.map(from, to);
672 }
673 
674 // static
map(int32_t from,C2Config::bitrate_mode_t * to)675 bool C2Mapper::map(int32_t from, C2Config::bitrate_mode_t *to) {
676     return sBitrateModes.map(from, to);
677 }
678 
679 // static
map(C2Config::pcm_encoding_t from,int32_t * to)680 bool C2Mapper::map(C2Config::pcm_encoding_t from, int32_t *to) {
681     return sPcmEncodings.map(from, to);
682 }
683 
684 // static
map(int32_t from,C2Config::pcm_encoding_t * to)685 bool C2Mapper::map(int32_t from, C2Config::pcm_encoding_t *to) {
686     return sPcmEncodings.map(from, to);
687 }
688 
689 // static
map(C2Color::range_t from,int32_t * to)690 bool C2Mapper::map(C2Color::range_t from, int32_t *to) {
691     bool res = true;
692     // map SDK defined values directly. For other values, use wrapping from ColorUtils.
693     if (!sColorRanges.map(from, to)) {
694         ColorAspects::Range sfRange;
695 
696         // map known constants and keep vendor extensions. all other values are mapped to 'Other'
697         if (!sColorRangesSf.map(from, &sfRange)) {
698             // use static cast and ensure it is in the extension range
699             if (from < C2Color::RANGE_VENDOR_START || from > C2Color::RANGE_OTHER) {
700                 sfRange = ColorAspects::RangeOther;
701                 res = false;
702             }
703         }
704 
705         *to = ColorUtils::wrapColorAspectsIntoColorRange(sfRange);
706     }
707     return res;
708 }
709 
710 // static
map(int32_t from,C2Color::range_t * to)711 bool C2Mapper::map(int32_t from, C2Color::range_t *to) {
712     // map SDK defined values directly. For other values, use wrapping from ColorUtils.
713     if (!sColorRanges.map(from, to)) {
714         ColorAspects::Range sfRange;
715         (void)ColorUtils::unwrapColorAspectsFromColorRange(from, &sfRange);
716 
717         // map known constants and keep vendor extensions. all other values are mapped to 'Other'
718         if (!sColorRangesSf.map(sfRange, to)) {
719             // use static cast and ensure it is in the extension range
720             *to = (C2Color::range_t)sfRange;
721             if (*to < C2Color::RANGE_VENDOR_START || *to > C2Color::RANGE_OTHER) {
722                 *to = C2Color::RANGE_OTHER;
723                 return false;
724             }
725         }
726     }
727 
728     return true;
729 }
730 
731 // static
map(C2Color::range_t from,ColorAspects::Range * to)732 bool C2Mapper::map(C2Color::range_t from, ColorAspects::Range *to) {
733     return sColorRangesSf.map(from, to);
734 }
735 
736 // static
map(ColorAspects::Range from,C2Color::range_t * to)737 bool C2Mapper::map(ColorAspects::Range from, C2Color::range_t *to) {
738     return sColorRangesSf.map(from, to);
739 }
740 
741 // static
map(C2Color::primaries_t primaries,C2Color::matrix_t matrix,int32_t * standard)742 bool C2Mapper::map(C2Color::primaries_t primaries, C2Color::matrix_t matrix, int32_t *standard) {
743     ColorAspects::Primaries sfPrimaries;
744     ColorAspects::MatrixCoeffs sfMatrix;
745     bool res = true;
746 
747     // map known constants and keep vendor extensions. all other values are mapped to 'Other'
748     if (!sColorPrimariesSf.map(primaries, &sfPrimaries)) {
749         // ensure it is in the extension range and use static cast
750         if (primaries < C2Color::PRIMARIES_VENDOR_START || primaries > C2Color::PRIMARIES_OTHER) {
751             // undefined non-extension values map to 'Other'
752             sfPrimaries = ColorAspects::PrimariesOther;
753             res = false;
754         } else {
755             sfPrimaries = (ColorAspects::Primaries)primaries;
756         }
757     }
758 
759     if (!sColorMatricesSf.map(matrix, &sfMatrix)) {
760         // use static cast and ensure it is in the extension range
761         if (matrix < C2Color::MATRIX_VENDOR_START || matrix > C2Color::MATRIX_OTHER) {
762             // undefined non-extension values map to 'Other'
763             sfMatrix = ColorAspects::MatrixOther;
764             res = false;
765         } else {
766             sfMatrix = (ColorAspects::MatrixCoeffs)matrix;
767         }
768     }
769 
770     *standard = ColorUtils::wrapColorAspectsIntoColorStandard(sfPrimaries, sfMatrix);
771 
772     return res;
773 }
774 
775 // static
map(int32_t standard,C2Color::primaries_t * primaries,C2Color::matrix_t * matrix)776 bool C2Mapper::map(int32_t standard, C2Color::primaries_t *primaries, C2Color::matrix_t *matrix) {
777     // first map to stagefright foundation aspects => these actually map nearly 1:1 to
778     // Codec 2.0 aspects
779     ColorAspects::Primaries sfPrimaries;
780     ColorAspects::MatrixCoeffs sfMatrix;
781     bool res = true;
782     (void)ColorUtils::unwrapColorAspectsFromColorStandard(standard, &sfPrimaries, &sfMatrix);
783 
784     // map known constants and keep vendor extensions. all other values are mapped to 'Other'
785     if (!sColorPrimariesSf.map(sfPrimaries, primaries)) {
786         // use static cast and ensure it is in the extension range
787         *primaries = (C2Color::primaries_t)sfPrimaries;
788         if (*primaries < C2Color::PRIMARIES_VENDOR_START || *primaries > C2Color::PRIMARIES_OTHER) {
789             *primaries = C2Color::PRIMARIES_OTHER;
790             res = false;
791         }
792     }
793 
794     if (!sColorMatricesSf.map(sfMatrix, matrix)) {
795         // use static cast and ensure it is in the extension range
796         *matrix = (C2Color::matrix_t)sfMatrix;
797         if (*matrix < C2Color::MATRIX_VENDOR_START || *matrix > C2Color::MATRIX_OTHER) {
798             *matrix = C2Color::MATRIX_OTHER;
799             res = false;
800         }
801     }
802 
803     return res;
804 }
805 
806 // static
map(C2Color::primaries_t from,ColorAspects::Primaries * to)807 bool C2Mapper::map(C2Color::primaries_t from, ColorAspects::Primaries *to) {
808     return sColorPrimariesSf.map(from, to);
809 }
810 
811 // static
map(ColorAspects::Primaries from,C2Color::primaries_t * to)812 bool C2Mapper::map(ColorAspects::Primaries from, C2Color::primaries_t *to) {
813     return sColorPrimariesSf.map(from, to);
814 }
815 
816 // static
map(C2Color::matrix_t from,ColorAspects::MatrixCoeffs * to)817 bool C2Mapper::map(C2Color::matrix_t from, ColorAspects::MatrixCoeffs *to) {
818     return sColorMatricesSf.map(from, to);
819 }
820 
821 // static
map(ColorAspects::MatrixCoeffs from,C2Color::matrix_t * to)822 bool C2Mapper::map(ColorAspects::MatrixCoeffs from, C2Color::matrix_t *to) {
823     return sColorMatricesSf.map(from, to);
824 }
825 
826 // static
map(C2Color::transfer_t from,int32_t * to)827 bool C2Mapper::map(C2Color::transfer_t from, int32_t *to) {
828     bool res = true;
829     // map SDK defined values directly. For other values, use wrapping from ColorUtils.
830     if (!sColorTransfers.map(from, to)) {
831         ColorAspects::Transfer sfTransfer;
832 
833         // map known constants and keep vendor extensions. all other values are mapped to 'Other'
834         if (!sColorTransfersSf.map(from, &sfTransfer)) {
835             // use static cast and ensure it is in the extension range
836             if (from < C2Color::TRANSFER_VENDOR_START || from > C2Color::TRANSFER_OTHER) {
837                 sfTransfer = ColorAspects::TransferOther;
838                 res = false;
839             }
840         }
841 
842         *to = ColorUtils::wrapColorAspectsIntoColorTransfer(sfTransfer);
843     }
844     return res;
845 }
846 
847 // static
map(int32_t from,C2Color::transfer_t * to)848 bool C2Mapper::map(int32_t from, C2Color::transfer_t *to) {
849     // map SDK defined values directly. For other values, use wrapping from ColorUtils.
850     if (!sColorTransfers.map(from, to)) {
851         ColorAspects::Transfer sfTransfer;
852         (void)ColorUtils::unwrapColorAspectsFromColorTransfer(from, &sfTransfer);
853 
854         // map known constants and keep vendor extensions. all other values are mapped to 'Other'
855         if (!sColorTransfersSf.map(sfTransfer, to)) {
856             // use static cast and ensure it is in the extension range
857             *to = (C2Color::transfer_t)sfTransfer;
858             if (*to < C2Color::TRANSFER_VENDOR_START || *to > C2Color::TRANSFER_OTHER) {
859                 *to = C2Color::TRANSFER_OTHER;
860                 return false;
861             }
862         }
863     }
864 
865     return true;
866 }
867 
868 // static
map(C2Color::range_t range,C2Color::primaries_t primaries,C2Color::matrix_t matrix,C2Color::transfer_t transfer,uint32_t * dataSpace)869 bool C2Mapper::map(
870         C2Color::range_t range, C2Color::primaries_t primaries,
871         C2Color::matrix_t matrix, C2Color::transfer_t transfer, uint32_t *dataSpace) {
872 #if 0
873     // pure reimplementation
874     *dataSpace = HAL_DATASPACE_UNKNOWN; // this is 0
875 
876     switch (range) {
877         case C2Color::RANGE_FULL:    *dataSpace |= HAL_DATASPACE_RANGE_FULL;    break;
878         case C2Color::RANGE_LIMITED: *dataSpace |= HAL_DATASPACE_RANGE_LIMITED; break;
879         default: break;
880     }
881 
882     switch (transfer) {
883         case C2Color::TRANSFER_LINEAR:  *dataSpace |= HAL_DATASPACE_TRANSFER_LINEAR;     break;
884         case C2Color::TRANSFER_SRGB:    *dataSpace |= HAL_DATASPACE_TRANSFER_SRGB;       break;
885         case C2Color::TRANSFER_170M:    *dataSpace |= HAL_DATASPACE_TRANSFER_SMPTE_170M; break;
886         case C2Color::TRANSFER_GAMMA22: *dataSpace |= HAL_DATASPACE_TRANSFER_GAMMA2_2;   break;
887         case C2Color::TRANSFER_GAMMA28: *dataSpace |= HAL_DATASPACE_TRANSFER_GAMMA2_8;   break;
888         case C2Color::TRANSFER_ST2084:  *dataSpace |= HAL_DATASPACE_TRANSFER_ST2084;     break;
889         case C2Color::TRANSFER_HLG:     *dataSpace |= HAL_DATASPACE_TRANSFER_HLG;        break;
890         default: break;
891     }
892 
893     switch (primaries) {
894         case C2Color::PRIMARIES_BT601_525:
895             *dataSpace |= (matrix == C2Color::MATRIX_240M
896                             || matrix == C2Color::MATRIX_BT709)
897                     ? HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED
898                     : HAL_DATASPACE_STANDARD_BT601_525;
899             break;
900         case C2Color::PRIMARIES_BT601_625:
901             *dataSpace |= (matrix == C2Color::MATRIX_240M
902                             || matrix == C2Color::MATRIX_BT709)
903                     ? HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED
904                     : HAL_DATASPACE_STANDARD_BT601_625;
905             break;
906         case C2Color::PRIMARIES_BT2020:
907             *dataSpace |= (matrix == C2Color::MATRIX_BT2020_CONSTANT
908                     ? HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE
909                     : HAL_DATASPACE_STANDARD_BT2020);
910             break;
911         case C2Color::PRIMARIES_BT470_M:
912             *dataSpace |= HAL_DATASPACE_STANDARD_BT470M;
913             break;
914         case C2Color::PRIMARIES_BT709:
915             *dataSpace |= HAL_DATASPACE_STANDARD_BT709;
916             break;
917         default: break;
918     }
919 #else
920     // for now use legacy implementation
921     ColorAspects aspects;
922     if (!sColorRangesSf.map(range, &aspects.mRange)) {
923         aspects.mRange = ColorAspects::RangeUnspecified;
924     }
925     if (!sColorPrimariesSf.map(primaries, &aspects.mPrimaries)) {
926         aspects.mPrimaries = ColorAspects::PrimariesUnspecified;
927     }
928     if (!sColorMatricesSf.map(matrix, &aspects.mMatrixCoeffs)) {
929         aspects.mMatrixCoeffs = ColorAspects::MatrixUnspecified;
930     }
931     if (!sColorTransfersSf.map(transfer, &aspects.mTransfer)) {
932         aspects.mTransfer = ColorAspects::TransferUnspecified;
933     }
934     *dataSpace = ColorUtils::getDataSpaceForColorAspects(aspects, true /* mayExpand */);
935 #endif
936     return true;
937 }
938 
939 // static
map(C2Color::transfer_t from,ColorAspects::Transfer * to)940 bool C2Mapper::map(C2Color::transfer_t from, ColorAspects::Transfer *to) {
941     return sColorTransfersSf.map(from, to);
942 }
943 
944 // static
map(ColorAspects::Transfer from,C2Color::transfer_t * to)945 bool C2Mapper::map(ColorAspects::Transfer from, C2Color::transfer_t *to) {
946     return sColorTransfersSf.map(from, to);
947 }
948