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 16 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_
53 changes: 53 additions & 0 deletions starboard/common/string_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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], "The quick brown fox jumps over the lazy dog");

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]);
}

str = "";
output = SplitString(str, '.');
EXPECT_TRUE(output.empty());
}

} // 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
12 changes: 12 additions & 0 deletions starboard/shared/starboard/media/codec_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@ TEST(CodecUtilTest, DoesNotParse1AsPcmForNonWavSubtypes) {
EXPECT_EQ(GetAudioCodecFromString("1", "webm"), kSbMediaAudioCodecNone);
}

TEST(CodecUtilTest, ParsesIamfCodec) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to double check if this makes use of IamfMimeUtil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, IamfMimeUtil is intended to be used in SbMediaIsAudioSupported() implementations.

EXPECT_EQ(GetAudioCodecFromString("iamf", ""), kSbMediaAudioCodecIamf);
EXPECT_EQ(GetAudioCodecFromString("iamf.000.000.Opus", ""),
kSbMediaAudioCodecIamf);
EXPECT_EQ(GetAudioCodecFromString("iamf.000.000.mp4a.40.2", ""),
kSbMediaAudioCodecIamf);
EXPECT_EQ(GetAudioCodecFromString("iamf.000.000.fLaC", ""),
kSbMediaAudioCodecIamf);
EXPECT_EQ(GetAudioCodecFromString("iamf.000.000.ipcm", ""),
kSbMediaAudioCodecIamf);
}

} // namespace
} // namespace media
} // namespace starboard
Expand Down
127 changes: 127 additions & 0 deletions starboard/shared/starboard/media/iamf_util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// 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") == std::string::npos) {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably redundant to the check in line 57? And if we do want to check, we should check that the return value is 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


// 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;
}

if (vec[0] != "iamf") {
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.size() != 4 || ((codec != "Opus") && (codec != "mp4a") &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check codec.size() != 4 is redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

(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.
stream = std::stringstream(vec[4]);
int object_type_indication;
stream >> object_type_indication;
if (stream.fail() || vec[4].size() != 2 || object_type_indication != 40) {
return;
}

stream = std::stringstream(vec[5]);
int audio_object_type;
stream >> audio_object_type;
if (stream.fail() || vec[5].size() != 1 || audio_object_type != 2) {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we just check vec[4] == '40' && vec[5] == '2'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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

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

enum IamfSubstreamCodec {
kIamfSubstreamCodecOpus,
kIamfSubstreamCodecMp4a,
kIamfSubstreamCodecFlac,
kIamfSubstreamCodecIpcm,
kIamfSubstreamCodecUnknown
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless this is a map of spec defined somewhere, consider moving kIamfSubstreamCodecUnknown to the first, so when the value is zero initialized, it's initialized to kIamfSubstreamCodecUnknown.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

};

// 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 { return profile_; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add DCHECK(is_valid()), and the same to below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

IamfSubstreamCodec substream_codec() const { 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