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