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