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

fix: Do not scale width by sarRatio, we already generate a pasp box with sarRatio information. #393

Merged
merged 5 commits into from
Jul 14, 2021

Conversation

brandonocasey
Copy link
Contributor

@brandonocasey brandonocasey commented Jul 13, 2021

While this doesn't give us the absolute width when we have a sarRatio, we don't want to use the absolute width. This is because we already encode the sarRatio in the pasp atom of our mp4 output. By encoding the sarRatio and an absolute width, the width is skewed even further unless the decoder looks directly at the nal units and calculates it itself. Example:

  • sarRatio is 0.5
  • width is 640 without sarRatio

How it is now with sarRatio width would get set to 320 and then when the decoder see's the pasp atom with sarRatio it will think that width is 160. Some decoders will ignore all these values on use nal units to calculate the width, but mobile Samsung Chrome on Android 11 will not. This causes a media decode error as the actual width of the video is 320.

With this change width will be set to 640 and the sarRatio pasp encoding will make the decoder set the width to 320

};

switch (data[0] & 0x1f) {
switch (event.nalUnitType) {
Copy link
Contributor Author

@brandonocasey brandonocasey Jul 13, 2021

Choose a reason for hiding this comment

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

It's annoying to try and determine the type of nal unit if it's not one that we normally parse, so I added this.

Copy link
Member

Choose a reason for hiding this comment

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

so now, for like sei_rbsp, the nalUnitType would end up being sei_rbsp but others would just pass along the hex value of it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, thinking about it again I think I will change it to nalUnitTypeCode that way type comparisons won't break down the line, since we expect it to not exist or be a string.

@@ -470,7 +470,7 @@ H264Stream = function() {
profileIdc: profileIdc,
levelIdc: levelIdc,
profileCompatibility: profileCompatibility,
width: Math.ceil((((picWidthInMbsMinus1 + 1) * 16) - frameCropLeftOffset * 2 - frameCropRightOffset * 2) * sarScale),
width: (((picWidthInMbsMinus1 + 1) * 16) - frameCropLeftOffset * 2 - frameCropRightOffset * 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.

We shouldn't need to Math.ceil if we are not scaling now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the original reason that we did scaling is because we did not encode a pasp atom with sarRatio information when mux.js was created. So We had to encode width and height as their real values at all times. I think we should have been scaling height in some scenarios too. Overall we won't have to do any of that now as the decoder will see the pasp and scale accordingly. We just have to report the values that exist.

lib/codecs/h264.js Outdated Show resolved Hide resolved
@@ -365,7 +367,7 @@ H264Stream = function() {
picHeightInMapUnitsMinus1,
frameMbsOnlyFlag,
scalingListCount,
sarRatio,
sarRatio = [1, 1],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

default to a 1 to 1 aspect ratio, ie width and height are exact values.

@brandonocasey brandonocasey merged commit 9e9982f into main Jul 14, 2021
@brandonocasey brandonocasey deleted the fix/no-width-sarRatio branch July 14, 2021 16:27
brandonocasey added a commit to videojs/http-streaming that referenced this pull request Jul 14, 2021
brandonocasey added a commit to videojs/http-streaming that referenced this pull request Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants