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

How to handle varying pixel formats #83

Open
youennf opened this issue Apr 21, 2022 · 3 comments
Open

How to handle varying pixel formats #83

youennf opened this issue Apr 21, 2022 · 3 comments

Comments

@youennf
Copy link
Contributor

youennf commented Apr 21, 2022

A transform exposes video frames that can be of various pixel formats (https://w3c.github.io/webcodecs/#enumdef-videopixelformat).
Depending on the OS and/or camera, this might be I420 or NV12 right now for cameras.
This might probably be the same for video element exported tracks, RGBA might be used for canvas capture tracks maybe.

It seems this can lead us to interop issue, especially for camera tracks, where applications will expect a given format and will break whenever their assumption is wrong.
I see a few options:

  • Let the web app deal with it: they can implement their own conversion in JS (computationally expensive though)
  • Let the web app easily convert video frames to another format.
  • Let the web app prescribe the pixel format it wants as API when creating the transform.
  • Let the UAs consistently select pixel formats (specs recommend or require to use a particular pixel formats, on a source type maybe)
@youennf
Copy link
Contributor Author

youennf commented Apr 21, 2022

Ditto for other characteristics such as color space (fullRange or not fullRange e.g.).

@dontcallmedom
Copy link
Member

this was also discussed a bit under webmachinelearning/webnn#226 (comment)

@tidoust
Copy link
Member

tidoust commented Feb 2, 2023

Regarding conversion by the web app, a relatively easy and efficient way of converting to RGBA is through WebGPU (well, "relatively easy" provided you're familiar with a few WebGPU concepts, and "efficient" when the underlying data of the VideoFrame is on the GPU). The external texture sampler returns pixels in RGBA (or BGRA) with the specified color space, regardless of the pixel format of the external texture.

Here is a code example of a transformer function that converts a VideoFrame to RGBA with WebGPU. Most lines are "boilerplate" code to use WebGPU. The actual conversion is done by the fragment shader and does not require specific knowledge of pixel formats and conversion formulas:

fn frag_main(@location(0) uv : vec2<f32>) -> @location(0) vec4<f32> {
  return textureSampleBaseClampToEdge(myTexture, mySampler, uv);
}

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

No branches or pull requests

3 participants