Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 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 | |
| 69 | }; |
| 70 | |
| 71 | ALookup<C2Config::profile_t, int32_t> sAvcProfiles = { |
| 72 | // treat restricted profiles as full profile if there is no equivalent - which works for |
| 73 | // decoders, but not for encoders |
| 74 | { C2Config::PROFILE_AVC_BASELINE, AVCProfileBaseline }, |
| 75 | { C2Config::PROFILE_AVC_CONSTRAINED_BASELINE, AVCProfileConstrainedBaseline }, |
| 76 | { C2Config::PROFILE_AVC_MAIN, AVCProfileMain }, |
| 77 | { C2Config::PROFILE_AVC_EXTENDED, AVCProfileExtended }, |
| 78 | { C2Config::PROFILE_AVC_HIGH, AVCProfileHigh }, |
| 79 | { C2Config::PROFILE_AVC_PROGRESSIVE_HIGH, AVCProfileHigh }, |
| 80 | { C2Config::PROFILE_AVC_CONSTRAINED_HIGH, AVCProfileConstrainedHigh }, |
| 81 | { C2Config::PROFILE_AVC_HIGH_10, AVCProfileHigh10 }, |
| 82 | { C2Config::PROFILE_AVC_PROGRESSIVE_HIGH_10, AVCProfileHigh10 }, |
| 83 | { C2Config::PROFILE_AVC_HIGH_422, AVCProfileHigh422 }, |
| 84 | { C2Config::PROFILE_AVC_HIGH_444_PREDICTIVE, AVCProfileHigh444 }, |
| 85 | { C2Config::PROFILE_AVC_HIGH_10_INTRA, AVCProfileHigh10 }, |
| 86 | { C2Config::PROFILE_AVC_HIGH_422_INTRA, AVCProfileHigh422 }, |
| 87 | { C2Config::PROFILE_AVC_HIGH_444_INTRA, AVCProfileHigh444 }, |
| 88 | { C2Config::PROFILE_AVC_CAVLC_444_INTRA, AVCProfileHigh444 }, |
| 89 | }; |
| 90 | |
| 91 | ALookup<C2Config::bitrate_mode_t, int32_t> sBitrateModes = { |
| 92 | { C2Config::BITRATE_CONST, BITRATE_MODE_CBR }, |
| 93 | { C2Config::BITRATE_VARIABLE, BITRATE_MODE_VBR }, |
| 94 | { C2Config::BITRATE_IGNORE, BITRATE_MODE_CQ }, |
| 95 | }; |
| 96 | |
| 97 | ALookup<C2Color::matrix_t, ColorAspects::MatrixCoeffs> sColorMatricesSf = { |
| 98 | { C2Color::MATRIX_UNSPECIFIED, ColorAspects::MatrixUnspecified }, |
| 99 | { C2Color::MATRIX_BT709, ColorAspects::MatrixBT709_5 }, |
| 100 | { C2Color::MATRIX_FCC47_73_682, ColorAspects::MatrixBT470_6M }, |
| 101 | { C2Color::MATRIX_BT601, ColorAspects::MatrixBT601_6 }, |
| 102 | { C2Color::MATRIX_SMPTE240M, ColorAspects::MatrixSMPTE240M }, |
| 103 | { C2Color::MATRIX_BT2020, ColorAspects::MatrixBT2020 }, |
| 104 | { C2Color::MATRIX_BT2020_CONSTANT, ColorAspects::MatrixBT2020Constant }, |
| 105 | { C2Color::MATRIX_OTHER, ColorAspects::MatrixOther }, |
| 106 | }; |
| 107 | |
| 108 | ALookup<C2Color::primaries_t, ColorAspects::Primaries> sColorPrimariesSf = { |
| 109 | { C2Color::PRIMARIES_UNSPECIFIED, ColorAspects::PrimariesUnspecified }, |
| 110 | { C2Color::PRIMARIES_BT709, ColorAspects::PrimariesBT709_5 }, |
| 111 | { C2Color::PRIMARIES_BT470_M, ColorAspects::PrimariesBT470_6M }, |
| 112 | { C2Color::PRIMARIES_BT601_625, ColorAspects::PrimariesBT601_6_625 }, |
| 113 | { C2Color::PRIMARIES_BT601_525, ColorAspects::PrimariesBT601_6_525 }, |
| 114 | { C2Color::PRIMARIES_GENERIC_FILM, ColorAspects::PrimariesGenericFilm }, |
| 115 | { C2Color::PRIMARIES_BT2020, ColorAspects::PrimariesBT2020 }, |
| 116 | // { C2Color::PRIMARIES_RP431, ColorAspects::Primaries... }, |
| 117 | // { C2Color::PRIMARIES_EG432, ColorAspects::Primaries... }, |
| 118 | // { C2Color::PRIMARIES_EBU3213, ColorAspects::Primaries... }, |
| 119 | { C2Color::PRIMARIES_OTHER, ColorAspects::PrimariesOther }, |
| 120 | }; |
| 121 | |
| 122 | ALookup<C2Color::range_t, int32_t> sColorRanges = { |
| 123 | { C2Color::RANGE_FULL, COLOR_RANGE_FULL }, |
| 124 | { C2Color::RANGE_LIMITED, COLOR_RANGE_LIMITED }, |
| 125 | }; |
| 126 | |
| 127 | ALookup<C2Color::range_t, ColorAspects::Range> sColorRangesSf = { |
| 128 | { C2Color::RANGE_UNSPECIFIED, ColorAspects::RangeUnspecified }, |
| 129 | { C2Color::RANGE_FULL, ColorAspects::RangeFull }, |
| 130 | { C2Color::RANGE_LIMITED, ColorAspects::RangeLimited }, |
| 131 | { C2Color::RANGE_OTHER, ColorAspects::RangeOther }, |
| 132 | }; |
| 133 | |
| 134 | ALookup<C2Color::transfer_t, int32_t> sColorTransfers = { |
| 135 | { C2Color::TRANSFER_LINEAR, COLOR_TRANSFER_LINEAR }, |
| 136 | { C2Color::TRANSFER_170M, COLOR_TRANSFER_SDR_VIDEO }, |
| 137 | { C2Color::TRANSFER_ST2084, COLOR_TRANSFER_ST2084 }, |
| 138 | { C2Color::TRANSFER_HLG, COLOR_TRANSFER_HLG }, |
| 139 | }; |
| 140 | |
| 141 | ALookup<C2Color::transfer_t, ColorAspects::Transfer> sColorTransfersSf = { |
| 142 | { C2Color::TRANSFER_UNSPECIFIED, ColorAspects::TransferUnspecified }, |
| 143 | { C2Color::TRANSFER_LINEAR, ColorAspects::TransferLinear }, |
| 144 | { C2Color::TRANSFER_SRGB, ColorAspects::TransferSRGB }, |
| 145 | { C2Color::TRANSFER_170M, ColorAspects::TransferSMPTE170M }, |
| 146 | { C2Color::TRANSFER_GAMMA22, ColorAspects::TransferGamma22 }, |
| 147 | { C2Color::TRANSFER_GAMMA28, ColorAspects::TransferGamma28 }, |
| 148 | { C2Color::TRANSFER_ST2084, ColorAspects::TransferST2084 }, |
| 149 | { C2Color::TRANSFER_HLG, ColorAspects::TransferHLG }, |
| 150 | { C2Color::TRANSFER_240M, ColorAspects::TransferSMPTE240M }, |
| 151 | { C2Color::TRANSFER_XVYCC, ColorAspects::TransferXvYCC }, |
| 152 | { C2Color::TRANSFER_BT1361, ColorAspects::TransferBT1361 }, |
| 153 | { C2Color::TRANSFER_ST428, ColorAspects::TransferST428 }, |
| 154 | { C2Color::TRANSFER_OTHER, ColorAspects::TransferOther }, |
| 155 | }; |
| 156 | |
| 157 | ALookup<C2Config::level_t, int32_t> sDolbyVisionLevels = { |
| 158 | { C2Config::LEVEL_DV_MAIN_HD_24, DolbyVisionLevelHd24 }, |
| 159 | { C2Config::LEVEL_DV_MAIN_HD_30, DolbyVisionLevelHd30 }, |
| 160 | { C2Config::LEVEL_DV_MAIN_FHD_24, DolbyVisionLevelFhd24 }, |
| 161 | { C2Config::LEVEL_DV_MAIN_FHD_30, DolbyVisionLevelFhd30 }, |
| 162 | { C2Config::LEVEL_DV_MAIN_FHD_60, DolbyVisionLevelFhd60 }, |
| 163 | { C2Config::LEVEL_DV_MAIN_UHD_24, DolbyVisionLevelUhd24 }, |
| 164 | { C2Config::LEVEL_DV_MAIN_UHD_30, DolbyVisionLevelUhd30 }, |
| 165 | { C2Config::LEVEL_DV_MAIN_UHD_48, DolbyVisionLevelUhd48 }, |
| 166 | { C2Config::LEVEL_DV_MAIN_UHD_60, DolbyVisionLevelUhd60 }, |
| 167 | |
| 168 | // high tiers are not yet supported on android, for now map them to main tier |
| 169 | { C2Config::LEVEL_DV_HIGH_HD_24, DolbyVisionLevelHd24 }, |
| 170 | { C2Config::LEVEL_DV_HIGH_HD_30, DolbyVisionLevelHd30 }, |
| 171 | { C2Config::LEVEL_DV_HIGH_FHD_24, DolbyVisionLevelFhd24 }, |
| 172 | { C2Config::LEVEL_DV_HIGH_FHD_30, DolbyVisionLevelFhd30 }, |
| 173 | { C2Config::LEVEL_DV_HIGH_FHD_60, DolbyVisionLevelFhd60 }, |
| 174 | { C2Config::LEVEL_DV_HIGH_UHD_24, DolbyVisionLevelUhd24 }, |
| 175 | { C2Config::LEVEL_DV_HIGH_UHD_30, DolbyVisionLevelUhd30 }, |
| 176 | { C2Config::LEVEL_DV_HIGH_UHD_48, DolbyVisionLevelUhd48 }, |
| 177 | { C2Config::LEVEL_DV_HIGH_UHD_60, DolbyVisionLevelUhd60 }, |
| 178 | }; |
| 179 | |
| 180 | ALookup<C2Config::profile_t, int32_t> sDolbyVisionProfiles = { |
| 181 | { C2Config::PROFILE_DV_AV_PER, DolbyVisionProfileDvavPer }, |
| 182 | { C2Config::PROFILE_DV_AV_PEN, DolbyVisionProfileDvavPen }, |
| 183 | { C2Config::PROFILE_DV_HE_DER, DolbyVisionProfileDvheDer }, |
| 184 | { C2Config::PROFILE_DV_HE_DEN, DolbyVisionProfileDvheDen }, |
| 185 | { C2Config::PROFILE_DV_HE_04, DolbyVisionProfileDvheDtr }, |
| 186 | { C2Config::PROFILE_DV_HE_05, DolbyVisionProfileDvheStn }, |
| 187 | { C2Config::PROFILE_DV_HE_DTH, DolbyVisionProfileDvheDth }, |
| 188 | { C2Config::PROFILE_DV_HE_07, DolbyVisionProfileDvheDtb }, |
| 189 | { C2Config::PROFILE_DV_HE_08, DolbyVisionProfileDvheSt }, |
| 190 | { C2Config::PROFILE_DV_AV_09, DolbyVisionProfileDvavSe }, |
| 191 | }; |
| 192 | |
| 193 | ALookup<C2Config::level_t, int32_t> sH263Levels = { |
| 194 | { C2Config::LEVEL_H263_10, H263Level10 }, |
| 195 | { C2Config::LEVEL_H263_20, H263Level20 }, |
| 196 | { C2Config::LEVEL_H263_30, H263Level30 }, |
| 197 | { C2Config::LEVEL_H263_40, H263Level40 }, |
| 198 | { C2Config::LEVEL_H263_45, H263Level45 }, |
| 199 | { C2Config::LEVEL_H263_50, H263Level50 }, |
| 200 | { C2Config::LEVEL_H263_60, H263Level60 }, |
| 201 | { C2Config::LEVEL_H263_70, H263Level70 }, |
| 202 | }; |
| 203 | |
| 204 | ALookup<C2Config::profile_t, int32_t> sH263Profiles = { |
| 205 | { C2Config::PROFILE_H263_BASELINE, H263ProfileBaseline }, |
| 206 | { C2Config::PROFILE_H263_H320, H263ProfileH320Coding }, |
| 207 | { C2Config::PROFILE_H263_V1BC, H263ProfileBackwardCompatible }, |
| 208 | { C2Config::PROFILE_H263_ISWV2, H263ProfileISWV2 }, |
| 209 | { C2Config::PROFILE_H263_ISWV3, H263ProfileISWV3 }, |
| 210 | { C2Config::PROFILE_H263_HIGH_COMPRESSION, H263ProfileHighCompression }, |
| 211 | { C2Config::PROFILE_H263_INTERNET, H263ProfileInternet }, |
| 212 | { C2Config::PROFILE_H263_INTERLACE, H263ProfileInterlace }, |
| 213 | { C2Config::PROFILE_H263_HIGH_LATENCY, H263ProfileHighLatency }, |
| 214 | }; |
| 215 | |
| 216 | ALookup<C2Config::level_t, int32_t> sHevcLevels = { |
| 217 | { C2Config::LEVEL_HEVC_MAIN_1, HEVCMainTierLevel1 }, |
| 218 | { C2Config::LEVEL_HEVC_MAIN_2, HEVCMainTierLevel2 }, |
| 219 | { C2Config::LEVEL_HEVC_MAIN_2_1, HEVCMainTierLevel21 }, |
| 220 | { C2Config::LEVEL_HEVC_MAIN_3, HEVCMainTierLevel3 }, |
| 221 | { C2Config::LEVEL_HEVC_MAIN_3_1, HEVCMainTierLevel31 }, |
| 222 | { C2Config::LEVEL_HEVC_MAIN_4, HEVCMainTierLevel4 }, |
| 223 | { C2Config::LEVEL_HEVC_MAIN_4_1, HEVCMainTierLevel41 }, |
| 224 | { C2Config::LEVEL_HEVC_MAIN_5, HEVCMainTierLevel5 }, |
| 225 | { C2Config::LEVEL_HEVC_MAIN_5_1, HEVCMainTierLevel51 }, |
| 226 | { C2Config::LEVEL_HEVC_MAIN_5_2, HEVCMainTierLevel52 }, |
| 227 | { C2Config::LEVEL_HEVC_MAIN_6, HEVCMainTierLevel6 }, |
| 228 | { C2Config::LEVEL_HEVC_MAIN_6_1, HEVCMainTierLevel61 }, |
| 229 | { C2Config::LEVEL_HEVC_MAIN_6_2, HEVCMainTierLevel62 }, |
| 230 | |
| 231 | { C2Config::LEVEL_HEVC_HIGH_4, HEVCHighTierLevel4 }, |
| 232 | { C2Config::LEVEL_HEVC_HIGH_4_1, HEVCHighTierLevel41 }, |
| 233 | { C2Config::LEVEL_HEVC_HIGH_5, HEVCHighTierLevel5 }, |
| 234 | { C2Config::LEVEL_HEVC_HIGH_5_1, HEVCHighTierLevel51 }, |
| 235 | { C2Config::LEVEL_HEVC_HIGH_5_2, HEVCHighTierLevel52 }, |
| 236 | { C2Config::LEVEL_HEVC_HIGH_6, HEVCHighTierLevel6 }, |
| 237 | { C2Config::LEVEL_HEVC_HIGH_6_1, HEVCHighTierLevel61 }, |
| 238 | { C2Config::LEVEL_HEVC_HIGH_6_2, HEVCHighTierLevel62 }, |
| 239 | |
| 240 | // map high tier levels below 4 to main tier |
| 241 | { C2Config::LEVEL_HEVC_MAIN_1, HEVCHighTierLevel1 }, |
| 242 | { C2Config::LEVEL_HEVC_MAIN_2, HEVCHighTierLevel2 }, |
| 243 | { C2Config::LEVEL_HEVC_MAIN_2_1, HEVCHighTierLevel21 }, |
| 244 | { C2Config::LEVEL_HEVC_MAIN_3, HEVCHighTierLevel3 }, |
| 245 | { C2Config::LEVEL_HEVC_MAIN_3_1, HEVCHighTierLevel31 }, |
| 246 | }; |
| 247 | |
| 248 | ALookup<C2Config::profile_t, int32_t> sHevcProfiles = { |
| 249 | { C2Config::PROFILE_HEVC_MAIN, HEVCProfileMain }, |
| 250 | { C2Config::PROFILE_HEVC_MAIN_10, HEVCProfileMain10 }, |
| 251 | { C2Config::PROFILE_HEVC_MAIN_STILL, HEVCProfileMainStill }, |
| 252 | { C2Config::PROFILE_HEVC_MAIN_INTRA, HEVCProfileMain }, |
| 253 | { C2Config::PROFILE_HEVC_MAIN_10_INTRA, HEVCProfileMain10 }, |
| 254 | }; |
| 255 | |
| 256 | ALookup<C2Config::level_t, int32_t> sMpeg2Levels = { |
| 257 | { C2Config::LEVEL_MP2V_LOW, MPEG2LevelLL }, |
| 258 | { C2Config::LEVEL_MP2V_MAIN, MPEG2LevelML }, |
| 259 | { C2Config::LEVEL_MP2V_HIGH_1440, MPEG2LevelH14 }, |
| 260 | { C2Config::LEVEL_MP2V_HIGH, MPEG2LevelHL }, |
| 261 | { C2Config::LEVEL_MP2V_HIGHP, MPEG2LevelHP }, |
| 262 | }; |
| 263 | |
| 264 | ALookup<C2Config::profile_t, int32_t> sMpeg2Profiles = { |
| 265 | { C2Config::PROFILE_MP2V_SIMPLE, MPEG2ProfileSimple }, |
| 266 | { C2Config::PROFILE_MP2V_MAIN, MPEG2ProfileMain }, |
| 267 | { C2Config::PROFILE_MP2V_SNR_SCALABLE, MPEG2ProfileSNR }, |
| 268 | { C2Config::PROFILE_MP2V_SPATIALLY_SCALABLE, MPEG2ProfileSpatial }, |
| 269 | { C2Config::PROFILE_MP2V_HIGH, MPEG2ProfileHigh }, |
| 270 | { C2Config::PROFILE_MP2V_422, MPEG2Profile422 }, |
| 271 | }; |
| 272 | |
| 273 | ALookup<C2Config::level_t, int32_t> sMpeg4Levels = { |
| 274 | { C2Config::LEVEL_MP4V_0, MPEG4Level0 }, |
| 275 | { C2Config::LEVEL_MP4V_0B, MPEG4Level0b }, |
| 276 | { C2Config::LEVEL_MP4V_1, MPEG4Level1 }, |
| 277 | { C2Config::LEVEL_MP4V_2, MPEG4Level2 }, |
| 278 | { C2Config::LEVEL_MP4V_3, MPEG4Level3 }, |
| 279 | { C2Config::LEVEL_MP4V_3B, MPEG4Level3b }, |
| 280 | { C2Config::LEVEL_MP4V_4, MPEG4Level4 }, |
| 281 | { C2Config::LEVEL_MP4V_4A, MPEG4Level4a }, |
| 282 | { C2Config::LEVEL_MP4V_5, MPEG4Level5 }, |
| 283 | { C2Config::LEVEL_MP4V_6, MPEG4Level6 }, |
| 284 | }; |
| 285 | |
| 286 | ALookup<C2Config::profile_t, int32_t> sMpeg4Profiles = { |
| 287 | { C2Config::PROFILE_MP4V_SIMPLE, MPEG4ProfileSimple }, |
| 288 | { C2Config::PROFILE_MP4V_SIMPLE_SCALABLE, MPEG4ProfileSimpleScalable }, |
| 289 | { C2Config::PROFILE_MP4V_CORE, MPEG4ProfileCore }, |
| 290 | { C2Config::PROFILE_MP4V_MAIN, MPEG4ProfileMain }, |
| 291 | { C2Config::PROFILE_MP4V_NBIT, MPEG4ProfileNbit }, |
| 292 | { C2Config::PROFILE_MP4V_ARTS, MPEG4ProfileAdvancedRealTime }, |
| 293 | { C2Config::PROFILE_MP4V_CORE_SCALABLE, MPEG4ProfileCoreScalable }, |
| 294 | { C2Config::PROFILE_MP4V_ACE, MPEG4ProfileAdvancedCoding }, |
| 295 | { C2Config::PROFILE_MP4V_ADVANCED_CORE, MPEG4ProfileAdvancedCore }, |
| 296 | { C2Config::PROFILE_MP4V_ADVANCED_SIMPLE, MPEG4ProfileAdvancedSimple }, |
| 297 | }; |
| 298 | |
| 299 | ALookup<C2Config::pcm_encoding_t, int32_t> sPcmEncodings = { |
| 300 | { C2Config::PCM_8, kAudioEncodingPcm8bit }, |
| 301 | { C2Config::PCM_16, kAudioEncodingPcm16bit }, |
| 302 | { C2Config::PCM_FLOAT, kAudioEncodingPcmFloat }, |
| 303 | }; |
| 304 | |
| 305 | ALookup<C2Config::level_t, int32_t> sVp9Levels = { |
| 306 | { C2Config::LEVEL_VP9_1, VP9Level1 }, |
| 307 | { C2Config::LEVEL_VP9_1_1, VP9Level11 }, |
| 308 | { C2Config::LEVEL_VP9_2, VP9Level2 }, |
| 309 | { C2Config::LEVEL_VP9_2_1, VP9Level21 }, |
| 310 | { C2Config::LEVEL_VP9_3, VP9Level3 }, |
| 311 | { C2Config::LEVEL_VP9_3_1, VP9Level31 }, |
| 312 | { C2Config::LEVEL_VP9_4, VP9Level4 }, |
| 313 | { C2Config::LEVEL_VP9_4_1, VP9Level41 }, |
| 314 | { C2Config::LEVEL_VP9_5, VP9Level5 }, |
| 315 | { C2Config::LEVEL_VP9_5_1, VP9Level51 }, |
| 316 | { C2Config::LEVEL_VP9_5_2, VP9Level52 }, |
| 317 | { C2Config::LEVEL_VP9_6, VP9Level6 }, |
| 318 | { C2Config::LEVEL_VP9_6_1, VP9Level61 }, |
| 319 | { C2Config::LEVEL_VP9_6_2, VP9Level62 }, |
| 320 | }; |
| 321 | |
| 322 | ALookup<C2Config::profile_t, int32_t> sVp9Profiles = { |
| 323 | { C2Config::PROFILE_VP9_0, VP9Profile0 }, |
| 324 | { C2Config::PROFILE_VP9_1, VP9Profile1 }, |
| 325 | { C2Config::PROFILE_VP9_2, VP9Profile2 }, |
| 326 | { C2Config::PROFILE_VP9_3, VP9Profile3 }, |
| 327 | }; |
| 328 | |
Ray Essick | 707c146 | 2018-12-05 15:21:35 -0800 | [diff] [blame] | 329 | ALookup<C2Config::level_t, int32_t> sAv1Levels = { |
| 330 | { C2Config::LEVEL_AV1_2, AV1Level2 }, |
| 331 | { C2Config::LEVEL_AV1_2_1, AV1Level21 }, |
| 332 | { C2Config::LEVEL_AV1_2_2, AV1Level22 }, |
| 333 | { C2Config::LEVEL_AV1_2_3, AV1Level23 }, |
| 334 | { C2Config::LEVEL_AV1_3, AV1Level3 }, |
| 335 | { C2Config::LEVEL_AV1_3_1, AV1Level31 }, |
| 336 | { C2Config::LEVEL_AV1_3_2, AV1Level32 }, |
| 337 | { C2Config::LEVEL_AV1_3_3, AV1Level33 }, |
| 338 | { C2Config::LEVEL_AV1_4, AV1Level4 }, |
| 339 | { C2Config::LEVEL_AV1_4_1, AV1Level41 }, |
| 340 | { C2Config::LEVEL_AV1_4_2, AV1Level42 }, |
| 341 | { C2Config::LEVEL_AV1_4_3, AV1Level43 }, |
| 342 | { C2Config::LEVEL_AV1_5, AV1Level5 }, |
| 343 | { C2Config::LEVEL_AV1_5_1, AV1Level51 }, |
| 344 | { C2Config::LEVEL_AV1_5_2, AV1Level52 }, |
| 345 | { C2Config::LEVEL_AV1_5_3, AV1Level53 }, |
| 346 | { C2Config::LEVEL_AV1_6, AV1Level6 }, |
| 347 | { C2Config::LEVEL_AV1_6_1, AV1Level61 }, |
| 348 | { C2Config::LEVEL_AV1_6_2, AV1Level62 }, |
| 349 | { C2Config::LEVEL_AV1_6_3, AV1Level63 }, |
| 350 | { C2Config::LEVEL_AV1_7, AV1Level7 }, |
| 351 | { C2Config::LEVEL_AV1_7_1, AV1Level71 }, |
| 352 | { C2Config::LEVEL_AV1_7_2, AV1Level72 }, |
| 353 | { C2Config::LEVEL_AV1_7_3, AV1Level73 }, |
| 354 | }; |
| 355 | |
| 356 | |
| 357 | ALookup<C2Config::profile_t, int32_t> sAv1Profiles = { |
| 358 | { C2Config::PROFILE_AV1_0, AV1Profile0 }, |
| 359 | { C2Config::PROFILE_AV1_1, AV1Profile1 }, |
| 360 | { C2Config::PROFILE_AV1_2, AV1Profile2 }, |
| 361 | }; |
| 362 | |
| 363 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 364 | /** |
| 365 | * A helper that passes through vendor extension profile and level values. |
| 366 | */ |
| 367 | struct ProfileLevelMapperHelper : C2Mapper::ProfileLevelMapper { |
| 368 | virtual bool simpleMap(C2Config::level_t from, int32_t *to) = 0; |
| 369 | virtual bool simpleMap(int32_t from, C2Config::level_t *to) = 0; |
| 370 | virtual bool simpleMap(C2Config::profile_t from, int32_t *to) = 0; |
| 371 | virtual bool simpleMap(int32_t from, C2Config::profile_t *to) = 0; |
| 372 | |
| 373 | template<typename T, typename U> |
| 374 | bool passThroughMap(T from, U *to) { |
| 375 | // allow (and pass through) vendor extensions |
| 376 | if (from >= (T)C2_PROFILE_LEVEL_VENDOR_START && from < (T)INT32_MAX) { |
| 377 | *to = (U)from; |
| 378 | return true; |
| 379 | } |
| 380 | return simpleMap(from, to); |
| 381 | } |
| 382 | |
| 383 | virtual bool mapLevel(C2Config::level_t from, int32_t *to) { |
| 384 | return passThroughMap(from, to); |
| 385 | } |
| 386 | |
| 387 | virtual bool mapLevel(int32_t from, C2Config::level_t *to) { |
| 388 | return passThroughMap(from, to); |
| 389 | } |
| 390 | |
| 391 | virtual bool mapProfile(C2Config::profile_t from, int32_t *to) { |
| 392 | return passThroughMap(from, to); |
| 393 | } |
| 394 | |
| 395 | virtual bool mapProfile(int32_t from, C2Config::profile_t *to) { |
| 396 | return passThroughMap(from, to); |
| 397 | } |
| 398 | }; |
| 399 | |
| 400 | // AAC only uses profiles, map all levels to unused or 0 |
| 401 | struct AacProfileLevelMapper : ProfileLevelMapperHelper { |
| 402 | virtual bool simpleMap(C2Config::level_t, int32_t *to) { |
| 403 | *to = 0; |
| 404 | return true; |
| 405 | } |
| 406 | virtual bool simpleMap(int32_t, C2Config::level_t *to) { |
| 407 | *to = C2Config::LEVEL_UNUSED; |
| 408 | return true; |
| 409 | } |
| 410 | virtual bool simpleMap(C2Config::profile_t from, int32_t *to) { |
| 411 | return sAacProfiles.map(from, to); |
| 412 | } |
| 413 | virtual bool simpleMap(int32_t from, C2Config::profile_t *to) { |
| 414 | return sAacProfiles.map(from, to); |
| 415 | } |
| 416 | }; |
| 417 | |
| 418 | struct AvcProfileLevelMapper : ProfileLevelMapperHelper { |
| 419 | virtual bool simpleMap(C2Config::level_t from, int32_t *to) { |
| 420 | return sAvcLevels.map(from, to); |
| 421 | } |
| 422 | virtual bool simpleMap(int32_t from, C2Config::level_t *to) { |
| 423 | return sAvcLevels.map(from, to); |
| 424 | } |
| 425 | virtual bool simpleMap(C2Config::profile_t from, int32_t *to) { |
| 426 | return sAvcProfiles.map(from, to); |
| 427 | } |
| 428 | virtual bool simpleMap(int32_t from, C2Config::profile_t *to) { |
| 429 | return sAvcProfiles.map(from, to); |
| 430 | } |
| 431 | }; |
| 432 | |
| 433 | struct DolbyVisionProfileLevelMapper : ProfileLevelMapperHelper { |
| 434 | virtual bool simpleMap(C2Config::level_t from, int32_t *to) { |
| 435 | return sDolbyVisionLevels.map(from, to); |
| 436 | } |
| 437 | virtual bool simpleMap(int32_t from, C2Config::level_t *to) { |
| 438 | return sDolbyVisionLevels.map(from, to); |
| 439 | } |
| 440 | virtual bool simpleMap(C2Config::profile_t from, int32_t *to) { |
| 441 | return sDolbyVisionProfiles.map(from, to); |
| 442 | } |
| 443 | virtual bool simpleMap(int32_t from, C2Config::profile_t *to) { |
| 444 | return sDolbyVisionProfiles.map(from, to); |
| 445 | } |
| 446 | }; |
| 447 | |
| 448 | struct H263ProfileLevelMapper : ProfileLevelMapperHelper { |
| 449 | virtual bool simpleMap(C2Config::level_t from, int32_t *to) { |
| 450 | return sH263Levels.map(from, to); |
| 451 | } |
| 452 | virtual bool simpleMap(int32_t from, C2Config::level_t *to) { |
| 453 | return sH263Levels.map(from, to); |
| 454 | } |
| 455 | virtual bool simpleMap(C2Config::profile_t from, int32_t *to) { |
| 456 | return sH263Profiles.map(from, to); |
| 457 | } |
| 458 | virtual bool simpleMap(int32_t from, C2Config::profile_t *to) { |
| 459 | return sH263Profiles.map(from, to); |
| 460 | } |
| 461 | }; |
| 462 | |
| 463 | struct HevcProfileLevelMapper : ProfileLevelMapperHelper { |
| 464 | virtual bool simpleMap(C2Config::level_t from, int32_t *to) { |
| 465 | return sHevcLevels.map(from, to); |
| 466 | } |
| 467 | virtual bool simpleMap(int32_t from, C2Config::level_t *to) { |
| 468 | return sHevcLevels.map(from, to); |
| 469 | } |
| 470 | virtual bool simpleMap(C2Config::profile_t from, int32_t *to) { |
| 471 | return sHevcProfiles.map(from, to); |
| 472 | } |
| 473 | virtual bool simpleMap(int32_t from, C2Config::profile_t *to) { |
| 474 | return sHevcProfiles.map(from, to); |
| 475 | } |
| 476 | }; |
| 477 | |
| 478 | struct Mpeg2ProfileLevelMapper : ProfileLevelMapperHelper { |
| 479 | virtual bool simpleMap(C2Config::level_t from, int32_t *to) { |
| 480 | return sMpeg2Levels.map(from, to); |
| 481 | } |
| 482 | virtual bool simpleMap(int32_t from, C2Config::level_t *to) { |
| 483 | return sMpeg2Levels.map(from, to); |
| 484 | } |
| 485 | virtual bool simpleMap(C2Config::profile_t from, int32_t *to) { |
| 486 | return sMpeg2Profiles.map(from, to); |
| 487 | } |
| 488 | virtual bool simpleMap(int32_t from, C2Config::profile_t *to) { |
| 489 | return sMpeg2Profiles.map(from, to); |
| 490 | } |
| 491 | }; |
| 492 | |
| 493 | struct Mpeg4ProfileLevelMapper : ProfileLevelMapperHelper { |
| 494 | virtual bool simpleMap(C2Config::level_t from, int32_t *to) { |
| 495 | return sMpeg4Levels.map(from, to); |
| 496 | } |
| 497 | virtual bool simpleMap(int32_t from, C2Config::level_t *to) { |
| 498 | return sMpeg4Levels.map(from, to); |
| 499 | } |
| 500 | virtual bool simpleMap(C2Config::profile_t from, int32_t *to) { |
| 501 | return sMpeg4Profiles.map(from, to); |
| 502 | } |
| 503 | virtual bool simpleMap(int32_t from, C2Config::profile_t *to) { |
| 504 | return sMpeg4Profiles.map(from, to); |
| 505 | } |
| 506 | }; |
| 507 | |
| 508 | // VP8 has no profiles and levels in Codec 2.0, but we use main profile and level 0 in MediaCodec |
| 509 | // map all profiles and levels to that. |
| 510 | struct Vp8ProfileLevelMapper : ProfileLevelMapperHelper { |
| 511 | virtual bool simpleMap(C2Config::level_t, int32_t *to) { |
| 512 | *to = VP8Level_Version0; |
| 513 | return true; |
| 514 | } |
| 515 | virtual bool simpleMap(int32_t, C2Config::level_t *to) { |
| 516 | *to = C2Config::LEVEL_UNUSED; |
| 517 | return true; |
| 518 | } |
| 519 | virtual bool simpleMap(C2Config::profile_t, int32_t *to) { |
| 520 | *to = VP8ProfileMain; |
| 521 | return true; |
| 522 | } |
| 523 | virtual bool simpleMap(int32_t, C2Config::profile_t *to) { |
| 524 | *to = C2Config::PROFILE_UNUSED; |
| 525 | return true; |
| 526 | } |
| 527 | }; |
| 528 | |
| 529 | struct Vp9ProfileLevelMapper : ProfileLevelMapperHelper { |
| 530 | virtual bool simpleMap(C2Config::level_t from, int32_t *to) { |
| 531 | return sVp9Levels.map(from, to); |
| 532 | } |
| 533 | virtual bool simpleMap(int32_t from, C2Config::level_t *to) { |
| 534 | return sVp9Levels.map(from, to); |
| 535 | } |
| 536 | virtual bool simpleMap(C2Config::profile_t from, int32_t *to) { |
| 537 | return sVp9Profiles.map(from, to); |
| 538 | } |
| 539 | virtual bool simpleMap(int32_t from, C2Config::profile_t *to) { |
| 540 | return sVp9Profiles.map(from, to); |
| 541 | } |
| 542 | }; |
| 543 | |
| 544 | } // namespace |
| 545 | |
| 546 | // static |
| 547 | std::shared_ptr<C2Mapper::ProfileLevelMapper> |
| 548 | C2Mapper::GetProfileLevelMapper(std::string mediaType) { |
| 549 | std::transform(mediaType.begin(), mediaType.begin(), mediaType.end(), ::tolower); |
| 550 | if (mediaType == MIMETYPE_AUDIO_AAC) { |
| 551 | return std::make_shared<AacProfileLevelMapper>(); |
| 552 | } else if (mediaType == MIMETYPE_VIDEO_AVC) { |
| 553 | return std::make_shared<AvcProfileLevelMapper>(); |
| 554 | } else if (mediaType == MIMETYPE_VIDEO_DOLBY_VISION) { |
| 555 | return std::make_shared<DolbyVisionProfileLevelMapper>(); |
| 556 | } else if (mediaType == MIMETYPE_VIDEO_H263) { |
| 557 | return std::make_shared<H263ProfileLevelMapper>(); |
| 558 | } else if (mediaType == MIMETYPE_VIDEO_HEVC) { |
| 559 | return std::make_shared<HevcProfileLevelMapper>(); |
| 560 | } else if (mediaType == MIMETYPE_VIDEO_MPEG2) { |
| 561 | return std::make_shared<Mpeg2ProfileLevelMapper>(); |
| 562 | } else if (mediaType == MIMETYPE_VIDEO_MPEG4) { |
| 563 | return std::make_shared<Mpeg4ProfileLevelMapper>(); |
| 564 | } else if (mediaType == MIMETYPE_VIDEO_VP8) { |
| 565 | return std::make_shared<Vp8ProfileLevelMapper>(); |
| 566 | } else if (mediaType == MIMETYPE_VIDEO_VP9) { |
| 567 | return std::make_shared<Vp9ProfileLevelMapper>(); |
| 568 | } |
| 569 | return nullptr; |
| 570 | } |
| 571 | |
| 572 | // static |
| 573 | bool C2Mapper::map(C2Config::bitrate_mode_t from, int32_t *to) { |
| 574 | return sBitrateModes.map(from, to); |
| 575 | } |
| 576 | |
| 577 | // static |
| 578 | bool C2Mapper::map(int32_t from, C2Config::bitrate_mode_t *to) { |
| 579 | return sBitrateModes.map(from, to); |
| 580 | } |
| 581 | |
| 582 | // static |
| 583 | bool C2Mapper::map(C2Config::pcm_encoding_t from, int32_t *to) { |
| 584 | return sPcmEncodings.map(from, to); |
| 585 | } |
| 586 | |
| 587 | // static |
| 588 | bool C2Mapper::map(int32_t from, C2Config::pcm_encoding_t *to) { |
| 589 | return sPcmEncodings.map(from, to); |
| 590 | } |
| 591 | |
| 592 | // static |
| 593 | bool C2Mapper::map(C2Color::range_t from, int32_t *to) { |
| 594 | bool res = true; |
| 595 | // map SDK defined values directly. For other values, use wrapping from ColorUtils. |
| 596 | if (!sColorRanges.map(from, to)) { |
| 597 | ColorAspects::Range sfRange; |
| 598 | |
| 599 | // map known constants and keep vendor extensions. all other values are mapped to 'Other' |
| 600 | if (!sColorRangesSf.map(from, &sfRange)) { |
| 601 | // use static cast and ensure it is in the extension range |
| 602 | if (from < C2Color::RANGE_VENDOR_START || from > C2Color::RANGE_OTHER) { |
| 603 | sfRange = ColorAspects::RangeOther; |
| 604 | res = false; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | *to = ColorUtils::wrapColorAspectsIntoColorRange(sfRange); |
| 609 | } |
| 610 | return res; |
| 611 | } |
| 612 | |
| 613 | // static |
| 614 | bool C2Mapper::map(int32_t from, C2Color::range_t *to) { |
| 615 | // map SDK defined values directly. For other values, use wrapping from ColorUtils. |
| 616 | if (!sColorRanges.map(from, to)) { |
| 617 | ColorAspects::Range sfRange; |
| 618 | (void)ColorUtils::unwrapColorAspectsFromColorRange(from, &sfRange); |
| 619 | |
| 620 | // map known constants and keep vendor extensions. all other values are mapped to 'Other' |
| 621 | if (!sColorRangesSf.map(sfRange, to)) { |
| 622 | // use static cast and ensure it is in the extension range |
| 623 | *to = (C2Color::range_t)sfRange; |
| 624 | if (*to < C2Color::RANGE_VENDOR_START || *to > C2Color::RANGE_OTHER) { |
| 625 | *to = C2Color::RANGE_OTHER; |
| 626 | return false; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | return true; |
| 632 | } |
| 633 | |
| 634 | // static |
| 635 | bool C2Mapper::map(C2Color::range_t from, ColorAspects::Range *to) { |
| 636 | return sColorRangesSf.map(from, to); |
| 637 | } |
| 638 | |
| 639 | // static |
| 640 | bool C2Mapper::map(ColorAspects::Range from, C2Color::range_t *to) { |
| 641 | return sColorRangesSf.map(from, to); |
| 642 | } |
| 643 | |
| 644 | // static |
| 645 | bool C2Mapper::map(C2Color::primaries_t primaries, C2Color::matrix_t matrix, int32_t *standard) { |
| 646 | ColorAspects::Primaries sfPrimaries; |
| 647 | ColorAspects::MatrixCoeffs sfMatrix; |
| 648 | bool res = true; |
| 649 | |
| 650 | // map known constants and keep vendor extensions. all other values are mapped to 'Other' |
| 651 | if (!sColorPrimariesSf.map(primaries, &sfPrimaries)) { |
| 652 | // ensure it is in the extension range and use static cast |
| 653 | if (primaries < C2Color::PRIMARIES_VENDOR_START || primaries > C2Color::PRIMARIES_OTHER) { |
| 654 | // undefined non-extension values map to 'Other' |
| 655 | sfPrimaries = ColorAspects::PrimariesOther; |
| 656 | res = false; |
| 657 | } else { |
| 658 | sfPrimaries = (ColorAspects::Primaries)primaries; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | if (!sColorMatricesSf.map(matrix, &sfMatrix)) { |
| 663 | // use static cast and ensure it is in the extension range |
| 664 | if (matrix < C2Color::MATRIX_VENDOR_START || matrix > C2Color::MATRIX_OTHER) { |
| 665 | // undefined non-extension values map to 'Other' |
| 666 | sfMatrix = ColorAspects::MatrixOther; |
| 667 | res = false; |
| 668 | } else { |
| 669 | sfMatrix = (ColorAspects::MatrixCoeffs)matrix; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | *standard = ColorUtils::wrapColorAspectsIntoColorStandard(sfPrimaries, sfMatrix); |
| 674 | |
| 675 | return res; |
| 676 | } |
| 677 | |
| 678 | // static |
| 679 | bool C2Mapper::map(int32_t standard, C2Color::primaries_t *primaries, C2Color::matrix_t *matrix) { |
| 680 | // first map to stagefright foundation aspects => these actually map nearly 1:1 to |
| 681 | // Codec 2.0 aspects |
| 682 | ColorAspects::Primaries sfPrimaries; |
| 683 | ColorAspects::MatrixCoeffs sfMatrix; |
| 684 | bool res = true; |
| 685 | (void)ColorUtils::unwrapColorAspectsFromColorStandard(standard, &sfPrimaries, &sfMatrix); |
| 686 | |
| 687 | // map known constants and keep vendor extensions. all other values are mapped to 'Other' |
| 688 | if (!sColorPrimariesSf.map(sfPrimaries, primaries)) { |
| 689 | // use static cast and ensure it is in the extension range |
| 690 | *primaries = (C2Color::primaries_t)sfPrimaries; |
| 691 | if (*primaries < C2Color::PRIMARIES_VENDOR_START || *primaries > C2Color::PRIMARIES_OTHER) { |
| 692 | *primaries = C2Color::PRIMARIES_OTHER; |
| 693 | res = false; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | if (!sColorMatricesSf.map(sfMatrix, matrix)) { |
| 698 | // use static cast and ensure it is in the extension range |
| 699 | *matrix = (C2Color::matrix_t)sfMatrix; |
| 700 | if (*matrix < C2Color::MATRIX_VENDOR_START || *matrix > C2Color::MATRIX_OTHER) { |
| 701 | *matrix = C2Color::MATRIX_OTHER; |
| 702 | res = false; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return res; |
| 707 | } |
| 708 | |
| 709 | // static |
| 710 | bool C2Mapper::map(C2Color::primaries_t from, ColorAspects::Primaries *to) { |
| 711 | return sColorPrimariesSf.map(from, to); |
| 712 | } |
| 713 | |
| 714 | // static |
| 715 | bool C2Mapper::map(ColorAspects::Primaries from, C2Color::primaries_t *to) { |
| 716 | return sColorPrimariesSf.map(from, to); |
| 717 | } |
| 718 | |
| 719 | // static |
| 720 | bool C2Mapper::map(C2Color::matrix_t from, ColorAspects::MatrixCoeffs *to) { |
| 721 | return sColorMatricesSf.map(from, to); |
| 722 | } |
| 723 | |
| 724 | // static |
| 725 | bool C2Mapper::map(ColorAspects::MatrixCoeffs from, C2Color::matrix_t *to) { |
| 726 | return sColorMatricesSf.map(from, to); |
| 727 | } |
| 728 | |
| 729 | // static |
| 730 | bool C2Mapper::map(C2Color::transfer_t from, int32_t *to) { |
| 731 | bool res = true; |
| 732 | // map SDK defined values directly. For other values, use wrapping from ColorUtils. |
| 733 | if (!sColorTransfers.map(from, to)) { |
| 734 | ColorAspects::Transfer sfTransfer; |
| 735 | |
| 736 | // map known constants and keep vendor extensions. all other values are mapped to 'Other' |
| 737 | if (!sColorTransfersSf.map(from, &sfTransfer)) { |
| 738 | // use static cast and ensure it is in the extension range |
| 739 | if (from < C2Color::TRANSFER_VENDOR_START || from > C2Color::TRANSFER_OTHER) { |
| 740 | sfTransfer = ColorAspects::TransferOther; |
| 741 | res = false; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | *to = ColorUtils::wrapColorAspectsIntoColorTransfer(sfTransfer); |
| 746 | } |
| 747 | return res; |
| 748 | } |
| 749 | |
| 750 | // static |
| 751 | bool C2Mapper::map(int32_t from, C2Color::transfer_t *to) { |
| 752 | // map SDK defined values directly. For other values, use wrapping from ColorUtils. |
| 753 | if (!sColorTransfers.map(from, to)) { |
| 754 | ColorAspects::Transfer sfTransfer; |
| 755 | (void)ColorUtils::unwrapColorAspectsFromColorTransfer(from, &sfTransfer); |
| 756 | |
| 757 | // map known constants and keep vendor extensions. all other values are mapped to 'Other' |
| 758 | if (!sColorTransfersSf.map(sfTransfer, to)) { |
| 759 | // use static cast and ensure it is in the extension range |
| 760 | *to = (C2Color::transfer_t)sfTransfer; |
| 761 | if (*to < C2Color::TRANSFER_VENDOR_START || *to > C2Color::TRANSFER_OTHER) { |
| 762 | *to = C2Color::TRANSFER_OTHER; |
| 763 | return false; |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | return true; |
| 769 | } |
| 770 | |
| 771 | // static |
| 772 | bool C2Mapper::map( |
| 773 | C2Color::range_t range, C2Color::primaries_t primaries, |
| 774 | C2Color::matrix_t matrix, C2Color::transfer_t transfer, uint32_t *dataSpace) { |
| 775 | #if 0 |
| 776 | // pure reimplementation |
| 777 | *dataSpace = HAL_DATASPACE_UNKNOWN; // this is 0 |
| 778 | |
| 779 | switch (range) { |
| 780 | case C2Color::RANGE_FULL: *dataSpace |= HAL_DATASPACE_RANGE_FULL; break; |
| 781 | case C2Color::RANGE_LIMITED: *dataSpace |= HAL_DATASPACE_RANGE_LIMITED; break; |
| 782 | default: break; |
| 783 | } |
| 784 | |
| 785 | switch (transfer) { |
| 786 | case C2Color::TRANSFER_LINEAR: *dataSpace |= HAL_DATASPACE_TRANSFER_LINEAR; break; |
| 787 | case C2Color::TRANSFER_SRGB: *dataSpace |= HAL_DATASPACE_TRANSFER_SRGB; break; |
| 788 | case C2Color::TRANSFER_170M: *dataSpace |= HAL_DATASPACE_TRANSFER_SMPTE_170M; break; |
| 789 | case C2Color::TRANSFER_GAMMA22: *dataSpace |= HAL_DATASPACE_TRANSFER_GAMMA2_2; break; |
| 790 | case C2Color::TRANSFER_GAMMA28: *dataSpace |= HAL_DATASPACE_TRANSFER_GAMMA2_8; break; |
| 791 | case C2Color::TRANSFER_ST2084: *dataSpace |= HAL_DATASPACE_TRANSFER_ST2084; break; |
| 792 | case C2Color::TRANSFER_HLG: *dataSpace |= HAL_DATASPACE_TRANSFER_HLG; break; |
| 793 | default: break; |
| 794 | } |
| 795 | |
| 796 | switch (primaries) { |
| 797 | case C2Color::PRIMARIES_BT601_525: |
| 798 | *dataSpace |= (matrix == C2Color::MATRIX_SMPTE240M |
| 799 | || matrix == C2Color::MATRIX_BT709) |
| 800 | ? HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED |
| 801 | : HAL_DATASPACE_STANDARD_BT601_525; |
| 802 | break; |
| 803 | case C2Color::PRIMARIES_BT601_625: |
| 804 | *dataSpace |= (matrix == C2Color::MATRIX_SMPTE240M |
| 805 | || matrix == C2Color::MATRIX_BT709) |
| 806 | ? HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED |
| 807 | : HAL_DATASPACE_STANDARD_BT601_625; |
| 808 | break; |
| 809 | case C2Color::PRIMARIES_BT2020: |
| 810 | *dataSpace |= (matrix == C2Color::MATRIX_BT2020CONSTANT |
| 811 | ? HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE |
| 812 | : HAL_DATASPACE_STANDARD_BT2020); |
| 813 | break; |
| 814 | case C2Color::PRIMARIES_BT470_M: |
| 815 | *dataSpace |= HAL_DATASPACE_STANDARD_BT470M; |
| 816 | break; |
| 817 | case C2Color::PRIMARIES_BT709: |
| 818 | *dataSpace |= HAL_DATASPACE_STANDARD_BT709; |
| 819 | break; |
| 820 | default: break; |
| 821 | } |
| 822 | #else |
| 823 | // for now use legacy implementation |
| 824 | ColorAspects aspects; |
| 825 | if (!sColorRangesSf.map(range, &aspects.mRange)) { |
| 826 | aspects.mRange = ColorAspects::RangeUnspecified; |
| 827 | } |
| 828 | if (!sColorPrimariesSf.map(primaries, &aspects.mPrimaries)) { |
| 829 | aspects.mPrimaries = ColorAspects::PrimariesUnspecified; |
| 830 | } |
| 831 | if (!sColorMatricesSf.map(matrix, &aspects.mMatrixCoeffs)) { |
| 832 | aspects.mMatrixCoeffs = ColorAspects::MatrixUnspecified; |
| 833 | } |
| 834 | if (!sColorTransfersSf.map(transfer, &aspects.mTransfer)) { |
| 835 | aspects.mTransfer = ColorAspects::TransferUnspecified; |
| 836 | } |
| 837 | *dataSpace = ColorUtils::getDataSpaceForColorAspects(aspects, true /* mayExpand */); |
| 838 | #endif |
| 839 | return true; |
| 840 | } |
| 841 | |
| 842 | // static |
| 843 | bool C2Mapper::map(C2Color::transfer_t from, ColorAspects::Transfer *to) { |
| 844 | return sColorTransfersSf.map(from, to); |
| 845 | } |
| 846 | |
| 847 | // static |
| 848 | bool C2Mapper::map(ColorAspects::Transfer from, C2Color::transfer_t *to) { |
| 849 | return sColorTransfersSf.map(from, to); |
| 850 | } |