#include "config.h"
#include "HEVCUtilities.h"
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/text/StringHash.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 parameters;
}
static String codecStringForDoViCodecType(const String& codec)
{
using MapType = HashMap<String, String>;
static NeverDestroyed<MapType> types = std::initializer_list<MapType::KeyValuePairType>({
{ "dvhe", "hev1" },
{ "dvh1", "hvc1" },
{ "dvav", "avc3" },
{ "dva1", "avc1" }
});
auto findResults = types.get().find(codec);
if (findResults == types.get().end())
return nullString();
return findResults->value;
}
static Optional<unsigned short> profileIDForAlphabeticDoViProfile(const String& profile)
{
using MapType = HashMap<String, unsigned short>;
static NeverDestroyed<MapType> map = std::initializer_list<MapType::KeyValuePairType>({
{ "dvhe.dtr", 4 },
{ "dvhe.stn", 5 },
{ "dvhe.dtb", 7 },
{ "dvhe.st", 8 },
{ "dvav.se", 9 }
});
auto findResults = map.get().find(profile);
if (findResults == map.get().end())
return WTF::nullopt;
return findResults->value;
}
static bool isValidDoViProfileID(unsigned short profileID)
{
switch (profileID) {
case 4:
case 5:
case 7:
case 8:
case 9:
return true;
default:
return false;
}
}
static Optional<unsigned short> maximumLevelIDForDoViProfileID(unsigned short profileID)
{
switch (profileID) {
case 4: return 9;
case 5: return 13;
case 7: return 9;
case 8: return 13;
case 9: return 5;
default: return WTF::nullopt;
}
}
static bool isValidProfileIDForCodecName(unsigned short profileID, const String& codecName)
{
if (profileID == 9)
return codecName == "avc1" || codecName == "avc3";
return codecName == "hvc1" || codecName == "hev1";
}
Optional<DoViParameterSet> parseDoViCodecParameters(const String& codecString)
{
StringView codecView(codecString);
auto codecSplit = codecView.split('.');
auto nextElement = codecSplit.begin();
if (nextElement == codecSplit.end())
return WTF::nullopt;
DoViParameterSet parameters;
parameters.codecName = codecStringForDoViCodecType((*nextElement).toString());
if (!parameters.codecName)
return WTF::nullopt;
if (++nextElement == codecSplit.end())
return WTF::nullopt;
auto profileID = *nextElement;
if (!profileID.length())
return WTF::nullopt;
bool isIntegral = false;
auto firstCharacter = profileID[0];
if (firstCharacter == '0') {
parameters.bitstreamProfileID = toIntegralType<uint8_t>(profileID, &isIntegral, 10);
if (!isIntegral)
return WTF::nullopt;
} else {
auto alphanumericProfileString = codecView.left(5 + profileID.length()).toString();
auto profileID = profileIDForAlphabeticDoViProfile(alphanumericProfileString);
if (!profileID)
return WTF::nullopt;
parameters.bitstreamProfileID = profileID.value();
}
if (!isValidDoViProfileID(parameters.bitstreamProfileID))
return WTF::nullopt;
if (!isValidProfileIDForCodecName(parameters.bitstreamProfileID, parameters.codecName))
return WTF::nullopt;
if (++nextElement == codecSplit.end())
return WTF::nullopt;
auto levelID = *nextElement;
if (!levelID.length())
return WTF::nullopt;
parameters.bitstreamLevelID = toIntegralType<uint8_t>(levelID, &isIntegral, 10);
if (!isIntegral)
return WTF::nullopt;
auto maximumLevelID = maximumLevelIDForDoViProfileID(parameters.bitstreamProfileID);
if (!maximumLevelID || parameters.bitstreamLevelID > maximumLevelID.value())
return WTF::nullopt;
return parameters;
}
}