1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
/*
* Copyright (C) 2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "VP9Utilities.h"
#include <wtf/NeverDestroyed.h>
#include <wtf/text/IntegerToStringConversion.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/text/StringToIntegerConversion.h>
namespace WebCore {
static bool isValidVPProfile(uint8_t profile)
{
return profile <= 3;
}
static bool isValidVPLevel(uint8_t level)
{
constexpr uint8_t validLevels[] = {
VPConfigurationLevel::Level_1,
VPConfigurationLevel::Level_1_1,
VPConfigurationLevel::Level_2,
VPConfigurationLevel::Level_2_1,
VPConfigurationLevel::Level_3,
VPConfigurationLevel::Level_3_1,
VPConfigurationLevel::Level_4,
VPConfigurationLevel::Level_4_1,
VPConfigurationLevel::Level_5,
VPConfigurationLevel::Level_5_1,
VPConfigurationLevel::Level_5_2,
VPConfigurationLevel::Level_6,
VPConfigurationLevel::Level_6_1,
VPConfigurationLevel::Level_6_2,
};
ASSERT(std::is_sorted(std::begin(validLevels), std::end(validLevels)));
return std::binary_search(std::begin(validLevels), std::end(validLevels), level);
}
static bool isValidBitDepth(uint8_t bitDepth)
{
return bitDepth == 8
|| bitDepth == 10
|| bitDepth == 12;
}
static bool isValidRange(uint8_t range)
{
return range == VPConfigurationRange::VideoRange
|| range == VPConfigurationRange::FullRange;
}
static bool isValidChromaSubsampling(uint8_t subsampling)
{
return subsampling >= VPConfigurationChromaSubsampling::Subsampling_420_Vertical
&& subsampling <= VPConfigurationChromaSubsampling::Subsampling_444;
}
static bool isValidVPColorPrimaries(uint8_t colorPrimaries)
{
constexpr uint8_t validColorPrimaries[] = {
VPConfigurationColorPrimaries::BT_709_6,
VPConfigurationColorPrimaries::Unspecified,
VPConfigurationColorPrimaries::BT_470_6_M,
VPConfigurationColorPrimaries::BT_470_7_BG,
VPConfigurationColorPrimaries::BT_601_7,
VPConfigurationColorPrimaries::SMPTE_ST_240,
VPConfigurationColorPrimaries::Film,
VPConfigurationColorPrimaries::BT_2020_Nonconstant_Luminance,
VPConfigurationColorPrimaries::SMPTE_ST_428_1,
VPConfigurationColorPrimaries::SMPTE_RP_431_2,
VPConfigurationColorPrimaries::SMPTE_EG_432_1,
VPConfigurationColorPrimaries::EBU_Tech_3213_E,
};
ASSERT(std::is_sorted(std::begin(validColorPrimaries), std::end(validColorPrimaries)));
return std::binary_search(std::begin(validColorPrimaries), std::end(validColorPrimaries), colorPrimaries);
}
static bool isValidVPTransferCharacteristics(uint8_t transferCharacteristics)
{
constexpr uint8_t validTransferCharacteristics[] = {
VPConfigurationTransferCharacteristics::BT_709_6,
VPConfigurationTransferCharacteristics::Unspecified,
VPConfigurationTransferCharacteristics::BT_470_6_M,
VPConfigurationTransferCharacteristics::BT_470_7_BG,
VPConfigurationTransferCharacteristics::BT_601_7,
VPConfigurationTransferCharacteristics::SMPTE_ST_240,
VPConfigurationTransferCharacteristics::Linear,
VPConfigurationTransferCharacteristics::Logrithmic,
VPConfigurationTransferCharacteristics::Logrithmic_Sqrt,
VPConfigurationTransferCharacteristics::IEC_61966_2_4,
VPConfigurationTransferCharacteristics::BT_1361_0,
VPConfigurationTransferCharacteristics::IEC_61966_2_1,
VPConfigurationTransferCharacteristics::BT_2020_10bit,
VPConfigurationTransferCharacteristics::BT_2020_12bit,
VPConfigurationTransferCharacteristics::SMPTE_ST_2084,
VPConfigurationTransferCharacteristics::SMPTE_ST_428_1,
VPConfigurationTransferCharacteristics::BT_2100_HLG,
};
ASSERT(std::is_sorted(std::begin(validTransferCharacteristics), std::end(validTransferCharacteristics)));
return std::binary_search(std::begin(validTransferCharacteristics), std::end(validTransferCharacteristics), transferCharacteristics);
}
static bool isValidVPMatrixCoefficients(uint8_t matrixCoefficients)
{
constexpr uint8_t validMatrixCoefficients[] = {
VPConfigurationMatrixCoefficients::Identity,
VPConfigurationMatrixCoefficients::BT_709_6,
VPConfigurationMatrixCoefficients::Unspecified,
VPConfigurationMatrixCoefficients::FCC,
VPConfigurationMatrixCoefficients::BT_470_7_BG,
VPConfigurationMatrixCoefficients::BT_601_7,
VPConfigurationMatrixCoefficients::SMPTE_ST_240,
VPConfigurationMatrixCoefficients::YCgCo,
VPConfigurationMatrixCoefficients::BT_2020_Nonconstant_Luminance,
VPConfigurationMatrixCoefficients::BT_2020_Constant_Luminance,
VPConfigurationMatrixCoefficients::SMPTE_ST_2085,
VPConfigurationMatrixCoefficients::Chromacity_Constant_Luminance,
VPConfigurationMatrixCoefficients::Chromacity_Nonconstant_Luminance,
VPConfigurationMatrixCoefficients::BT_2100_ICC,
};
ASSERT(std::is_sorted(std::begin(validMatrixCoefficients), std::end(validMatrixCoefficients)));
return std::binary_search(std::begin(validMatrixCoefficients), std::end(validMatrixCoefficients), matrixCoefficients);
}
std::optional<VPCodecConfigurationRecord> parseVPCodecParameters(StringView codecView)
{
auto codecSplit = codecView.split('.');
auto nextElement = codecSplit.begin();
if (nextElement == codecSplit.end())
return std::nullopt;
VPCodecConfigurationRecord configuration;
configuration.codecName = (*nextElement).toString();
++nextElement;
// Support the legacy identifiers (with no parameters) for VP8 and VP9.
if (configuration.codecName == "vp8"_s || configuration.codecName == "vp9"_s) {
if (nextElement == codecSplit.end())
return configuration;
auto codecString = codecView.toStringWithoutCopying();
if (codecString == "vp8.0"_s || codecString == "vp9.0"_s)
return configuration;
}
// The format of the 'vp09' codec string is specified in the webm GitHub repo:
// <https://github.com/webmproject/vp9-dash/blob/master/VPCodecISOMediaFileFormatBinding.md#codecs-parameter-string>
// "sample entry 4CC, profile, level, and bitDepth are all mandatory fields. If any of these fields are empty, or not
// within their allowed range, the processing device SHALL treat it as an error."
// Codec identifier: legal values are 'vp08' or 'vp09'.
if (configuration.codecName != "vp08"_s && configuration.codecName != "vp09"_s)
return std::nullopt;
if (nextElement == codecSplit.end())
return std::nullopt;
// First element: profile. Legal values are 0-3.
auto profile = parseInteger<uint8_t>(*nextElement);
if (!profile || !isValidVPProfile(*profile))
return std::nullopt;
configuration.profile = *profile;
if (++nextElement == codecSplit.end())
return std::nullopt;
// Second element: level. Legal values are enumerated in validVPLevels above.
auto level = parseInteger<uint8_t>(*nextElement);
if (!level || !isValidVPLevel(*level))
return std::nullopt;
configuration.level = *level;
if (++nextElement == codecSplit.end())
return std::nullopt;
// Third element: bitDepth. Legal values are 8, 10, and 12.
auto bitDepth = parseInteger<uint8_t>(*nextElement);
if (!bitDepth || !isValidBitDepth(*bitDepth))
return std::nullopt;
configuration.bitDepth = *bitDepth;
// "colorPrimaries, transferCharacteristics, matrixCoefficients, videoFullRangeFlag, and chromaSubsampling are OPTIONAL,
// mutually inclusive (all or none) fields. If not specified then the processing device MUST use the values listed in the
// table below as defaults when deciding if the device is able to decode and potentially render the video."
if (++nextElement == codecSplit.end())
return configuration;
// Fourth element: chromaSubsampling. Legal values are 0-3.
auto chromaSubsampling = parseInteger<uint8_t>(*nextElement);
if (!chromaSubsampling || !isValidChromaSubsampling(*chromaSubsampling))
return std::nullopt;
configuration.chromaSubsampling = *chromaSubsampling;
if (++nextElement == codecSplit.end())
return std::nullopt;
// Fifth element: colorPrimaries. Legal values are defined by ISO/IEC 23001-8:2016, superseded
// by ISO/IEC 23091-2:2019.
auto colorPrimaries = parseInteger<uint8_t>(*nextElement);
if (!colorPrimaries || !isValidVPColorPrimaries(*colorPrimaries))
return std::nullopt;
configuration.colorPrimaries = *colorPrimaries;
if (++nextElement == codecSplit.end())
return std::nullopt;
// Sixth element: transferCharacteristics. Legal values are defined by ISO/IEC 23001-8:2016, superseded
// by ISO/IEC 23091-2:2019.
auto transferCharacteristics = parseInteger<uint8_t>(*nextElement);
if (!transferCharacteristics || !isValidVPTransferCharacteristics(*transferCharacteristics))
return std::nullopt;
configuration.transferCharacteristics = *transferCharacteristics;
if (++nextElement == codecSplit.end())
return std::nullopt;
// Seventh element: matrixCoefficients. Legal values are defined by ISO/IEC 23001-8:2016, superseded
// by ISO/IEC 23091-2:2019.
auto matrixCoefficients = parseInteger<uint8_t>(*nextElement);
if (!matrixCoefficients || !isValidVPMatrixCoefficients(*matrixCoefficients))
return std::nullopt;
configuration.matrixCoefficients = *matrixCoefficients;
// "If matrixCoefficients is 0 (RGB), then chroma subsampling MUST be 3 (4:4:4)."
if (!configuration.matrixCoefficients && configuration.chromaSubsampling != 3)
return std::nullopt;
if (++nextElement == codecSplit.end())
return std::nullopt;
// Eighth element: videoFullRangeFlag. Legal values are 0 and 1.
auto videoFullRangeFlag = parseInteger<uint8_t>(*nextElement);
if (!videoFullRangeFlag || !isValidRange(*videoFullRangeFlag))
return std::nullopt;
configuration.videoFullRangeFlag = *videoFullRangeFlag;
if (++nextElement != codecSplit.end())
return std::nullopt;
return configuration;
}
String createVPCodecParametersString(const VPCodecConfigurationRecord& configuration)
{
// The format of the 'vp09' codec string is specified in the webm GitHub repo:
// <https://github.com/webmproject/vp9-dash/blob/master/VPCodecISOMediaFileFormatBinding.md#codecs-parameter-string>
//
// The codecs parameter string for the VP codec family is as follows:
// <sample entry 4CC>.<profile>.<level>.<bitDepth>.<chromaSubsampling>.
// <colourPrimaries>.<transferCharacteristics>.<matrixCoefficients>.
// <videoFullRangeFlag>
// All parameter values are expressed as double-digit decimals.
// sample entry 4CC, profile, level, and bitDepth are all mandatory fields.
StringBuilder resultBuilder;
resultBuilder.append(configuration.codecName);
if (configuration.profile > 10
|| !isValidVPProfile(configuration.profile)
|| !isValidVPLevel(configuration.level)
|| !isValidBitDepth(configuration.bitDepth)
|| !isValidChromaSubsampling(configuration.chromaSubsampling)
|| !isValidVPColorPrimaries(configuration.colorPrimaries)
|| !isValidVPTransferCharacteristics(configuration.transferCharacteristics)
|| !isValidVPMatrixCoefficients(configuration.matrixCoefficients)
|| !isValidRange(configuration.videoFullRangeFlag))
return resultBuilder.toString();
resultBuilder.append(".0");
resultBuilder.append(numberToStringUnsigned<String>(configuration.profile));
resultBuilder.append('.');
resultBuilder.append(numberToStringUnsigned<String>(configuration.level));
resultBuilder.append('.');
if (configuration.transferCharacteristics < 10)
resultBuilder.append('0');
resultBuilder.append(numberToStringUnsigned<String>(configuration.bitDepth));
static NeverDestroyed<VPCodecConfigurationRecord> defaultConfiguration { };
// colourPrimaries, transferCharacteristics, matrixCoefficients, videoFullRangeFlag, and chromaSubsampling
// are OPTIONAL, mutually inclusive (all or none) fields.
if (configuration.chromaSubsampling == defaultConfiguration->chromaSubsampling
&& configuration.videoFullRangeFlag == defaultConfiguration->videoFullRangeFlag
&& configuration.colorPrimaries == defaultConfiguration->colorPrimaries
&& configuration.transferCharacteristics == defaultConfiguration->transferCharacteristics
&& configuration.matrixCoefficients == defaultConfiguration->matrixCoefficients)
return resultBuilder.toString();
resultBuilder.append(".0");
resultBuilder.append(numberToStringUnsigned<String>(configuration.chromaSubsampling));
resultBuilder.append('.');
if (configuration.colorPrimaries < 10)
resultBuilder.append('0');
resultBuilder.append(numberToStringUnsigned<String>(configuration.colorPrimaries));
resultBuilder.append('.');
if (configuration.transferCharacteristics < 10)
resultBuilder.append('0');
resultBuilder.append(numberToStringUnsigned<String>(configuration.transferCharacteristics));
resultBuilder.append('.');
if (configuration.matrixCoefficients < 10)
resultBuilder.append('0');
resultBuilder.append(numberToStringUnsigned<String>(configuration.matrixCoefficients));
resultBuilder.append(".0");
resultBuilder.append(numberToStringUnsigned<String>(configuration.videoFullRangeFlag));
return resultBuilder.toString();
}
}
|