Skip to content

Commit

Permalink
[media] Check IAMF codec params for validity (#4244)
Browse files Browse the repository at this point in the history
1. Implements IamfMimeUtil to parse the "codecs" portion of an IAMF mime
to check it for validity. Also adds tests for IamfMimeUtil.
2. Implements SplitString() in //starboard/common/string.h. Adds tests
for SplitString().
3. Updates GetAudioCodecFromString() to use IamfMimeUtil. The Iamf test
for GetAudioCodecFromString() now has a few more cases to check for
validity.

   b/356704307
  • Loading branch information
osagie98 authored Oct 11, 2024
1 parent 3305954 commit a337e43
Show file tree
Hide file tree
Showing 10 changed files with 615 additions and 1 deletion.
1 change: 1 addition & 0 deletions starboard/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ target(gtest_target_type, "common_test") {
"memory_test.cc",
"player_test.cc",
"socket_test.cc",
"string_test.cc",
"test_main.cc",
]
deps = [
Expand Down
27 changes: 27 additions & 0 deletions starboard/common/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,33 @@ static SB_C_FORCE_INLINE int strlcat(CHAR* dst, const CHAR* src, int dst_size) {
dst_length;
}

// Splits a string on a char delimiter.
inline std::vector<std::string> SplitString(const std::string& input,
char delimiter) {
std::vector<std::string> output;
if (input.empty()) {
return output;
}

size_t start = 0;
while (start != std::string::npos) {
size_t end = input.find_first_of(delimiter, start);
std::string piece;

if (end == std::string::npos) {
piece = input.substr(start);
start = std::string::npos;
} else {
piece = input.substr(start, end - start);
start = end + 1;
}

output.emplace_back(piece);
}

return output;
}

} // namespace starboard

#endif // STARBOARD_COMMON_STRING_H_
98 changes: 98 additions & 0 deletions starboard/common/string_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <string>
#include <vector>

#include "starboard/common/string.h"

#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace {

TEST(StringTest, SplitString) {
std::string str = "The quick brown fox jumps over the lazy dog";
std::vector<std::string> output = SplitString(str, '.');
ASSERT_EQ(output.size(), 1);
ASSERT_EQ(output[0], str);

std::vector<std::string> vec = {"The", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog"};
output = SplitString(str, ' ');
ASSERT_EQ(output.size(), vec.size());
for (int i = 0; i < vec.size(); ++i) {
ASSERT_EQ(output[i], vec[i]);
}

str = "iamf.001.001.Opus";
output = SplitString(str, '.');
vec = {"iamf", "001", "001", "Opus"};
ASSERT_EQ(output.size(), vec.size());
for (int i = 0; i < vec.size(); ++i) {
ASSERT_EQ(output[i], vec[i]);
}
}

TEST(StringTest, SplitStringEmptyInput) {
std::string str;
std::vector<std::string> output = SplitString(str, '.');
ASSERT_TRUE(output.empty());
}

TEST(StringTest, SplitStringNullDelimiter) {
std::string str = "The quick brown fox jumps over the lazy dog";
std::vector<std::string> output = SplitString(str, '\0');
ASSERT_EQ(output.size(), 1);
ASSERT_EQ(output[0], str);
}

TEST(StringTest, SplitStringIrregularDelimiterPositions) {
std::string str = ".string.starts.with.delimiter";
std::vector<std::string> output = SplitString(str, '.');
std::vector<std::string> vec = {"", "string", "starts", "with", "delimiter"};
ASSERT_EQ(output.size(), vec.size());
for (int i = 0; i < vec.size(); ++i) {
ASSERT_EQ(output[i], vec[i]);
}

str = "string.ends.with.delimiter.";
output = SplitString(str, '.');
vec = {"string", "ends", "with", "delimiter", ""};
ASSERT_EQ(output.size(), vec.size());
for (int i = 0; i < vec.size(); ++i) {
ASSERT_EQ(output[i], vec[i]);
}

str = ".delimiters.on.both.sides.";
output = SplitString(str, '.');
vec = {"", "delimiters", "on", "both", "sides", ""};
ASSERT_EQ(output.size(), vec.size());
for (int i = 0; i < vec.size(); ++i) {
ASSERT_EQ(output[i], vec[i]);
}
}

TEST(StringTest, SplitStringOnlyDelimiters) {
std::string str = "....";
std::vector<std::string> output = SplitString(str, '.');
std::vector<std::string> vec = {"", "", "", "", ""};
ASSERT_EQ(output.size(), vec.size());
for (int i = 0; i < vec.size(); ++i) {
ASSERT_EQ(output[i], vec[i]);
}
}

} // namespace
} // namespace starboard
2 changes: 2 additions & 0 deletions starboard/shared/starboard/media/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ static_library("media_util") {
"//starboard/shared/starboard/media/avc_util.h",
"//starboard/shared/starboard/media/codec_util.cc",
"//starboard/shared/starboard/media/codec_util.h",
"//starboard/shared/starboard/media/iamf_util.cc",
"//starboard/shared/starboard/media/iamf_util.h",
"//starboard/shared/starboard/media/key_system_supportability_cache.cc",
"//starboard/shared/starboard/media/key_system_supportability_cache.h",
"//starboard/shared/starboard/media/media_support_internal.h",
Expand Down
3 changes: 2 additions & 1 deletion starboard/shared/starboard/media/codec_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "starboard/common/string.h"
#include "starboard/configuration.h"
#include "starboard/configuration_constants.h"
#include "starboard/shared/starboard/media/iamf_util.h"

namespace starboard {
namespace shared {
Expand Down Expand Up @@ -111,7 +112,7 @@ SbMediaAudioCodec GetAudioCodecFromString(const char* codec,
return kSbMediaAudioCodecPcm;
}
#if SB_API_VERSION >= 15
if (strcmp(codec, "iamf") == 0 || strncmp(codec, "iamf.", 5) == 0) {
if (strcmp(codec, "iamf") == 0 || IamfMimeUtil(codec).is_valid()) {
return kSbMediaAudioCodecIamf;
}
#endif // SB_API_VERSION >= 15
Expand Down
16 changes: 16 additions & 0 deletions starboard/shared/starboard/media/codec_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,22 @@ TEST(CodecUtilTest, ParsesIamfCodec) {
kSbMediaAudioCodecIamf);
EXPECT_EQ(GetAudioCodecFromString("iamf.000.000.ipcm", ""),
kSbMediaAudioCodecIamf);
EXPECT_EQ(GetAudioCodecFromString("iamf.001.000.ipcm", ""),
kSbMediaAudioCodecIamf);
EXPECT_EQ(GetAudioCodecFromString("iamf.255.255.ipcm", ""),
kSbMediaAudioCodecIamf);

// Invalid codec types
EXPECT_EQ(GetAudioCodecFromString("iamf.000.256.Opus", ""),
kSbMediaAudioCodecNone);
EXPECT_EQ(GetAudioCodecFromString("iamf.000.000.invalid", ""),
kSbMediaAudioCodecNone);
EXPECT_EQ(GetAudioCodecFromString("Iamf.000.000.fLaC", ""),
kSbMediaAudioCodecNone);
EXPECT_EQ(GetAudioCodecFromString("iamf.000.000.mp4a.40.3", ""),
kSbMediaAudioCodecNone);
EXPECT_EQ(GetAudioCodecFromString("iamf.000.0000.Opus", ""),
kSbMediaAudioCodecNone);
}
#endif // SB_API_VERSION >= 15

Expand Down
131 changes: 131 additions & 0 deletions starboard/shared/starboard/media/iamf_util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "starboard/shared/starboard/media/iamf_util.h"

#include <cctype>
#include <cstdlib>
#include <string>
#include <vector>

#include "starboard/common/string.h"

namespace starboard {
namespace shared {
namespace starboard {
namespace media {
namespace {
// Checks if |input| is a valid IAMF profile value, and stores the converted
// value in |*profile| if so.
bool StringToProfile(const std::string& input, uint32_t* profile) {
SB_DCHECK(profile);

if (input.size() != 3) {
return false;
}

if (!std::isdigit(input[0]) || !std::isdigit(input[1]) ||
!std::isdigit(input[2])) {
return false;
}

uint32_t converted_val = static_cast<uint32_t>(std::atoi(input.c_str()));
if (converted_val > kIamfProfileMax) {
return false;
}
*profile = converted_val;
return true;
}
} // namespace

IamfMimeUtil::IamfMimeUtil(const std::string& mime_type) {
// Reference: Immersive Audio Model and Formats;
// v1.0.0
// 6.3. Codecs Parameter String
// (https://aomediacodec.github.io/iamf/v1.0.0-errata.html#codecsparameter)
if (mime_type.find("iamf") != 0) {
return;
}

// 4 FOURCC string "iamf".
// +1 delimiting period.
// +3 primary_profile as 3 digit string.
// +1 delimiting period.
// +3 additional_profile as 3 digit string.
// +1 delimiting period.
// +9 The remaining string is one of "Opus", "mp4a.40.2", "fLaC", or "ipcm".
constexpr int kMaxIamfCodecIdLength = 22;
if (mime_type.size() > kMaxIamfCodecIdLength) {
return;
}

const std::vector<std::string> vec = SplitString(mime_type, '.');
// The mime type must be in 4 parts for all substreams other than AAC, which
// is 6 parts.
if (vec.size() != 4 && vec.size() != 6) {
return;
}

// The primary profile must be between 0 and 255 inclusive.
uint32_t primary_profile = 0;
if (!StringToProfile(vec[1], &primary_profile)) {
return;
}

// The additional profile must be between 0 and 255 inclusive.
uint32_t additional_profile = 0;
if (!StringToProfile(vec[2], &additional_profile)) {
return;
}

// The codec string should be one of "Opus", "mp4a", "fLaC", or "ipcm".
std::string codec = vec[3];
if ((codec != "Opus") && (codec != "mp4a") && (codec != "fLaC") &&
(codec != "ipcm")) {
return;
}

// Only IAMF codec parameter strings with "mp4a" should be greater than 4
// elements.
if (codec == "mp4a") {
if (vec.size() != 6) {
return;
}

// The fields following "mp4a" should be "40" and "2" to signal AAC-LC.
if (vec[4] != "40" || vec[5] != "2") {
return;
}
substream_codec_ = kIamfSubstreamCodecMp4a;
} else {
if (vec.size() > 4) {
return;
}
if (codec == "Opus") {
substream_codec_ = kIamfSubstreamCodecOpus;
} else if (codec == "fLaC") {
substream_codec_ = kIamfSubstreamCodecFlac;
} else {
substream_codec_ = kIamfSubstreamCodecIpcm;
}
}

primary_profile_ = primary_profile;
additional_profile_ = additional_profile;
}

} // namespace media
} // namespace starboard
} // namespace shared
} // namespace starboard
Loading

0 comments on commit a337e43

Please sign in to comment.