Closed Bug 1934976 Opened 1 year ago Closed 1 year ago

Fix I420 rescaling on Mac

Categories

(Core :: Audio/Video: Web Codecs, defect)

All
macOS
defect

Tracking

()

RESOLVED FIXED
139 Branch
Tracking Status
firefox139 --- fixed

People

(Reporter: chunmin, Assigned: chunmin)

References

(Blocks 1 open bug)

Details

Attachments

(1 file, 20 external files, 2 obsolete files)

4.73 KB, text/html
Details

The I420-related WPT in video-encoder-rescaling.https.any.js, which was introduced in Bug 1921623, currently fails on Mac. We need to fix it.

Assignee: nobody → cchang

Here is a visual test page. It takes a photo (via WebRTC video stream), encodes it with a different size, and then decodes it. Currently, the image will duplicate in the destination canvas.

Here is a backup test page hosted on github.

From the test page here, it demonstrates that encoding a resized image in RGBX format is okay, while in I420 is not. In addition, this test page shows that drawing a videoframe, which creates from a video element, into a canvas with different size is okay. Hence, the problem must reside in the Mac encoder rather than rendering. I am going to fix this shortly.

I guess the CVPixelBufferRef used for the Mac's encoder input should contain the resized data. A resizing approach for I420 was implemented in bug 1921623 for the Windows encoder, where I420 data is scaled if needed before being passed to the underlying encoder.

Refactoring the resizing logic into a separate function that returns a scaled/resized buffer and calling it accordingly should work for both Windows and Mac scenarios.

(In reply to C.M.Chang[:chunmin] from comment #3)

I guess the CVPixelBufferRef used for the Mac's encoder input should contain the resized data. A resizing approach for I420 was implemented in bug 1921623 for the Windows encoder, where I420 data is scaled if needed before being passed to the underlying encoder.

It turns out to be a wrong guess. It looks like the mac's encoder can automatically resize the image if needed.
The real problem is that the pixel format for our YUV input is wrong. The pixel format is converted from mConfig.mSourcePixelFormat, while mConfig.mSourcePixelFormat is set to BGRA by default and never being updated.

Changing the pixel type we used in CreateCVPixelBuffer and BuildSourceImageBufferAttributes to YUV420 fixed the problem (If these two formats don't match each other, kVTPixelTransferNotSupportedErr will be thown).

However, since the input format is unknown when the WebCodecs's encoder is initialized (that's why we have a default guess), setting a nullptr as the sourceImageBufferAttributes to VTCompressionSessionCreate is a way to go. The drawback is that it could increase the chance that we'll have to copy image data, according to the developer document. The alternative is to re-initialize the encoder if the input format changes or differs from the expected one.

I am going to go with the more straightforward approach first, and then see if we need to go the alternative one.

Depends on D241513

Depends on D241514

Depends on D241518

Attachment #9471888 - Attachment description: WIP: Bug 1934976 - Remove duplicate check → Bug 1934976 - Remove duplicate check
Attachment #9471889 - Attachment description: WIP: Bug 1934976 - Add an error log → Bug 1934976 - Add an error log
Attachment #9471890 - Attachment description: WIP: Bug 1934976 - Ensure number of YUV planes is valid → Bug 1934976 - Ensure number of YUV planes is valid
Attachment #9471891 - Attachment description: WIP: Bug 1934976 - Set no pixel format in AppleVTEncoder's initialization → Bug 1934976 - Allow flexible input formats for AppleVTEncoder
Attachment #9471892 - Attachment description: WIP: Bug 1934976 - Set pixel-format by input image's format → Bug 1934976 - Set encoder format by the given input's one
Attachment #9471893 - Attachment description: WIP: Bug 1934976 - Correct color-range in the converted pixel format → Bug 1934976 - Correct color-range in the converted pixel format
Attachment #9471916 - Attachment description: WIP: Bug 1934976 - Adjust WPT → Bug 1934976 - Adjust WPT

This patch restores pixel format initialization during the configuration
stage, which was removed in D241516, and introduces a mechanism to
reinitialize the format during encoding if the format changes.

Previously, to allow flexibility in pixel formats for encoding requests,
the encoder was initialized without a preset pixel format. This also
eliminated the creation of a pixel buffer pool for source frames,
reducing unnecessary image copies.

To strike a balance, this patch reintroduces pixel format initialization
while still allowing updates when the input image format differs from
the expected format. This ensures better performance while maintaining
flexibility.

Depends on D241537

This patch automatically selects the encoder pixel format based on the
intended use case.

  • For real-time applications, the format defaults to YUV420, as it's the
    most commonly used format for video conferencing.
  • For other use cases, the format is set to BGRA, which is the preferred
    format in Gecko.

Depends on D241898

Depends on D241899

mInited is never being set to true in the first place [1], so this
patch sets it to true once initialization succeeds. Additionally,
assertions that guarantees the initialization are added for the
operations called only after successful initialization.

[1] https://hg.mozilla.org/mozilla-central/rev/b86195e2307a

Depends on D242244

Depends on D242245

Previously, the mInited is used to ensure the success of
initialization since having mSession doesn't guarantee that. The
initialization continues after creating mSession. However, as long as
mSession is torn down safely when initialization fails, it can be a
singal of initialization success, and a replacement of mInit.

Depends on D242246

Depends on D242247

Attachment #9471892 - Attachment description: Bug 1934976 - Set encoder format by the given input's one → Bug 1934976 - Set buffer pixel format by the given input's one

Comment on attachment 9472564 [details]
Bug 1934976 - Reintroduce format initialization with dynamic reconfiguration

Revision D241898 was moved to bug 1955153. Setting attachment 9472564 [details] to obsolete.

Attachment #9472564 - Attachment is obsolete: true

Comment on attachment 9472565 [details]
Bug 1934976 - Default pixel format selection based on use case

Revision D241899 was moved to bug 1955153. Setting attachment 9472565 [details] to obsolete.

Attachment #9472565 - Attachment is obsolete: true
Attachment #9471893 - Attachment description: Bug 1934976 - Correct color-range in the converted pixel format → Bug 1934976 - Accurate pixel format and color range mapping

Depends on D243033

Attachment #9472565 - Attachment is obsolete: false
Attachment #9471891 - Attachment description: Bug 1934976 - Allow flexible input formats for AppleVTEncoder → Bug 1934976 - Handle multiple input formats in AppleVTEncoder reliably
Attachment #9474539 - Attachment is obsolete: true
Pushed by cchang@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/c9eb32edfb6c Remove duplicate check r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/f3ff68ce51d7 Add an error log r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/ebb683c5f76a Ensure number of YUV planes is valid r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/c775f915768b Ensure thread-safety and session existence for Reconfigure r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/234305a93fb8 Set mInited properly r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/6cdb9725cb0b Add a method to invalidate session if needed r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/d518c76574b6 Replace the mInited by mSession entirely r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/f09899753b4c Move session initialization to a function r=media-playback-reviewers,alwu https://hg.mozilla.org/integration/autoland/rev/dd315e8f4c0d Set sourceImageBufferAttributes by given pixel format r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/422f9fd66a21 Get NumberOfPlanes by OSType instead of ImageBitmapFormat r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/12b77d56d968 Make MapPixelFormat return Result r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/2727becd865d Set buffer pixel format by the given input's one r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/270ae400212f Accurate pixel format and color range mapping r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/f6e96227dec9 Adjust WPT r=aosmond,padenot https://hg.mozilla.org/integration/autoland/rev/6d13925d71d1 Remove unused member veriable in EncoderConfig r=media-playback-reviewers,alwu https://hg.mozilla.org/integration/autoland/rev/4e8cca45d1f8 Sort include headers in EncoderConfig.h r=media-playback-reviewers,alwu,karlt https://hg.mozilla.org/integration/autoland/rev/19a3a04306fb Add color-range info to EncoderConfig r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/4ce8e9c60315 Correct typo in EncoderConfig::ToString r=media-playback-reviewers,padenot https://hg.mozilla.org/integration/autoland/rev/f1d77d732ffa Default pixel format selection based on use case r=media-playback-reviewers,alwu https://hg.mozilla.org/integration/autoland/rev/116962bc2aa2 Handle multiple input formats in AppleVTEncoder reliably r=media-playback-reviewers,padenot
QA Whiteboard: [qa-triage-done-c140/b139]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: