#include "config.h"
#include "HEVCUtilities.h"
#include <wtf/text/StringToIntegerConversion.h>
namespace WebCore {
Optional<HEVCParameterSet> parseHEVCCodecParameters(const String& codecString)
{
StringView codecView(codecString);
auto codecSplit = codecView.split('.');
auto nextElement = codecSplit.begin();
if (nextElement == codecSplit.end())
return WTF::nullopt;
HEVCParameterSet parameters;
parameters.codecName = (*nextElement).toString();
if (!equal(parameters.codecName, "hvc1") && !equal(parameters.codecName, "hev1"))
return WTF::nullopt;
if (++nextElement == codecSplit.end())
return WTF::nullopt;
auto profileSpace = *nextElement;
if (!profileSpace.length())
return WTF::nullopt;
auto firstCharacter = profileSpace[0];
bool hasProfileSpace = firstCharacter >= 'A' && firstCharacter <= 'C';
if (hasProfileSpace) {
parameters.generalProfileSpace = 1 + (firstCharacter - 'A');
profileSpace = profileSpace.substring(1);
}
bool isValidProfileIDC = false;
parameters.generalProfileIDC = toIntegralType<uint8_t>(profileSpace, &isValidProfileIDC);
if (!isValidProfileIDC)
return WTF::nullopt;
if (++nextElement == codecSplit.end())
return WTF::nullopt;
auto compatibilityFlags = *nextElement;
bool isValidCompatibilityFlags = false;
parameters.generalProfileCompatibilityFlags = toIntegralType<uint32_t>(compatibilityFlags, &isValidCompatibilityFlags, 16);
if (!isValidCompatibilityFlags)
return WTF::nullopt;
if (++nextElement == codecSplit.end())
return WTF::nullopt;
auto generalTier = *nextElement;
firstCharacter = generalTier[0];
if (firstCharacter != 'L' && firstCharacter != 'H')
return WTF::nullopt;
parameters.generalTierFlag = firstCharacter == 'H';
bool isValidGeneralLevelIDC = false;
parameters.generalLevelIDC = toIntegralType<uint8_t>(generalTier.substring(1), &isValidGeneralLevelIDC);
if (!isValidGeneralLevelIDC)
return WTF::nullopt;
parameters.constraintFlags.fill(0, 6);
for (auto& flag : parameters.constraintFlags) {
if (++nextElement == codecSplit.end())
break;
bool isValidFlag = false;
flag = toIntegralType<uint8_t>(*nextElement, &isValidFlag, 16);
if (!isValidFlag)
return WTF::nullopt;
}
return WTFMove(parameters);
}
}