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

Conversation

osagie98
Copy link
Contributor

b/356704307

@osagie98 osagie98 marked this pull request as ready for review September 9, 2024 22:22
@osagie98 osagie98 added the cp-25.lts.1+ Cherry Pick to the 25.lts.1+ branch label Sep 9, 2024
return false;
}

constexpr int kMaxIamfCodecIdLength =
Copy link
Contributor

Choose a reason for hiding this comment

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

Although addition is a cheap operation, should we consider directly put the total number here, and then comment how it is calculated?

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


std::stringstream stream(input);
std::string token;
while (std::getline(stream, token, delimiter)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious if this is the most efficient way to split strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if it's the most efficient, but I found it most readable. I replaced it with an implementation similar to Chromium's SplitString(), already in use in media code, so I think we can be confident it's efficient enough.

@osagie98 osagie98 changed the title Explicity check for supported IAMF codecs parameter strings Explicitly check for supported IAMF codecs parameter strings Sep 10, 2024
}

std::vector<std::string> vec = SplitString(mime_type, '.');
if (vec.size() != 4 && vec.size() != 6) {
Copy link
Contributor

@borongc borongc Sep 12, 2024

Choose a reason for hiding this comment

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

Maybe we should specify why 4 and 6 here as a comment. Same for other places.

If the format of IAMF is always iamf.<primary_profile>.<additional_profile>.<audio_format>, then we can consider make SplitString() as a special case, such as std::vector<std::string> SplitString(std::string& input, char delimiter, int num_fields=-1), so if num_fields=4, this is used to parse iamf MIME strings.

So we can get rid of checking vec.size() != 6 here, and check vec.size() != 4 || vec[0] != "iamf".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the comment. Seems that adding a special case is pretty specific to this use case, I'm not sure if it'd see any use outside of this function to justify adding it in. Happy to discuss it.

// The primary profile string should be three digits, and should be between 0
// and 255 inclusive.
int primary_profile;
std::stringstream stream(vec[1]);
Copy link
Contributor

Choose a reason for hiding this comment

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

vec[1] is std::string, should we consider to use std::stoi() to convert string to integer here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, std::isdigit() can check if each character is a digit or not. Then, make this a helper function, so we don't need the similar codes in all other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue with std::stoi() is that it throws an exception if it can't convert the field to an int. std::isdigit() will help with that but I wonder if it's much more efficient than using stream.

}

// The codec string should be one of "Opus", "mp4a", "fLaC", or "ipcm".
std::string codec = vec[3];
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe consider to refactor the following code to switch-case, so it is more readable.

For example:

switch(codec) {
  case "Opus":
  case "fLaC":
  case "ipcm":
    return true;
  default:
    // handle "mp4a.40.2" and check its format here
    // return true if it meets
}

return false;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately switch statements can't work with strings, only values resolving to int.

kSbMediaAudioCodecIamf);
}

TEST(CodecUtilTest, IsIamfMimeType) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider to add cases where the length of resulting string vector is not 4, like iamf.xxx.Opus, iamf.1xx.2yy.

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

@@ -106,12 +107,100 @@ SbMediaAudioCodec GetAudioCodecFromString(const char* codec,
if (is_wav && strcmp(codec, "1") == 0) {
return kSbMediaAudioCodecPcm;
}
if (strcmp(codec, "iamf") == 0 || strncmp(codec, "iamf.", 5) == 0) {
if (strcmp(codec, "iamf") == 0 || IsIamfMimeType(codec)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't check the validity of the codec string in this function.

The best way to check the validity of the iamf codec string I can think of is to check it in SbMediaIsAudioSupported(). We can add an iamf_util, and check the validity of codec string while deciding if the sub codec can be supported.

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

@@ -101,6 +101,37 @@ static SB_C_FORCE_INLINE int strlcat(CHAR* dst, const CHAR* src, int dst_size) {
dst_length;
}

// Splits a string on a char delimiter. Returns an empty vector if the delimiter
// is not found
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider double checking if returning an empty vector when the delimiter is not found is a common practice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It now returns the full string if non empty, like the Chromium method does.

@@ -101,6 +101,37 @@ static SB_C_FORCE_INLINE int strlcat(CHAR* dst, const CHAR* src, int dst_size) {
dst_length;
}

// Splits a string on a char delimiter. Returns an empty vector if the delimiter
// is not found
inline std::vector<std::string> SplitString(std::string& input,
Copy link
Contributor

Choose a reason for hiding this comment

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

const std::string&

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

@osagie98 osagie98 changed the title Explicitly check for supported IAMF codecs parameter strings [media] Create IamfMimeUtil Oct 1, 2024
@osagie98 osagie98 changed the base branch from main to 25.lts.1+ October 1, 2024 17:09
@osagie98 osagie98 changed the base branch from 25.lts.1+ to main October 1, 2024 17:09
@@ -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.

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.

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.

Comment on lines 33 to 35
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


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

Comment on lines 94 to 106
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.

The test will be implemented in a separate PR
youtube#4188
@osagie98 osagie98 changed the base branch from main to 25.lts.1+ October 3, 2024 18:44
osagie98 added a commit that referenced this pull request Oct 8, 2024
Adds a test for GetAudioCodecFromString() to parse the codecs parameter
of an IAMF MIME.

`GetAudioCodecFromString()` will more strictly parse for valid values of
the IAMF codec param in a follow up
#4061.

b/356704307
@osagie98 osagie98 changed the base branch from 25.lts.1+ to main October 9, 2024 18:34
@osagie98 osagie98 marked this pull request as draft October 9, 2024 19:22
@osagie98
Copy link
Contributor Author

osagie98 commented Oct 9, 2024

This PR will be close. The 25.lts PR is here: #4244

@osagie98 osagie98 closed this Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cp-25.lts.1+ Cherry Pick to the 25.lts.1+ branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants