Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[media] Create IamfMimeUtil #4061

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -101,6 +101,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_
62 changes: 62 additions & 0 deletions starboard/common/string_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 {

osagie98 marked this conversation as resolved.
Show resolved Hide resolved
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);
}

} // 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
112 changes: 112 additions & 0 deletions starboard/shared/starboard/media/iamf_util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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 <sstream>
#include <string>
#include <vector>

#include "starboard/common/string.h"

namespace starboard {
namespace shared {
namespace starboard {
namespace media {

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 string should be three digits, and should be between 0
// and 255 inclusive.
int primary_profile;
std::stringstream stream(vec[1]);
stream >> primary_profile;
if (stream.fail() || vec[1].size() != 3 || primary_profile > 255) {
return;
}

// The additional profile string should be three digits, and should be between
// 0 and 255 inclusive.
stream = std::stringstream(vec[2]);
int additional_profile;
stream >> additional_profile;
if (stream.fail() || vec[2].size() != 3 || additional_profile > 255) {
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;
}
}

profile_ = primary_profile;
}

} // namespace media
} // namespace starboard
} // namespace shared
} // namespace starboard
72 changes: 72 additions & 0 deletions starboard/shared/starboard/media/iamf_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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.

#ifndef STARBOARD_SHARED_STARBOARD_MEDIA_IAMF_UTIL_H_
#define STARBOARD_SHARED_STARBOARD_MEDIA_IAMF_UTIL_H_

#include <limits>
#include <string>

#include "starboard/common/log.h"

namespace starboard {
namespace shared {
namespace starboard {
namespace media {

enum IamfSubstreamCodec {
kIamfSubstreamCodecUnknown,
kIamfSubstreamCodecOpus,
kIamfSubstreamCodecMp4a,
kIamfSubstreamCodecFlac,
kIamfSubstreamCodecIpcm
};

// These values must match the profile values defined in
// https://aomediacodec.github.io/iamf/v1.0.0-errata.html#profiles
inline constexpr int kIamfProfileSimple = 0;
inline constexpr int kIamfProfileBase = 1;
inline constexpr int kIamfProfileMax = 255;

// Parses an IAMF codecs parameter string following the convention defined in
// https://aomediacodec.github.io/iamf/v1.0.0-errata.html#codecsparameter.
// Always check is_valid() before calling the getter functions.
class IamfMimeUtil {
public:
explicit IamfMimeUtil(const std::string& mime_type);

bool is_valid() const {
return profile_ <= kIamfProfileMax &&
substream_codec_ != kIamfSubstreamCodecUnknown;
}
int profile() const {
SB_DCHECK(is_valid());
return profile_;
}
IamfSubstreamCodec substream_codec() const {
SB_DCHECK(is_valid());
return substream_codec_;
}

private:
int profile_ = std::numeric_limits<int>::max();
IamfSubstreamCodec substream_codec_ = kIamfSubstreamCodecUnknown;
};

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

#endif // STARBOARD_SHARED_STARBOARD_MEDIA_IAMF_UTIL_H_
Loading
Loading