Skip to content

Instantly share code, notes, and snippets.

@Djuffin
Last active May 9, 2024 20:48
Show Gist options
  • Save Djuffin/9e2f98025ead49998524510cfeed8d33 to your computer and use it in GitHub Desktop.
Save Djuffin/9e2f98025ead49998524510cfeed8d33 to your computer and use it in GitHub Desktop.
Explainer for adding ability to convert YUV VideoFrames to RGB via VideoFrame.copyTo()

Allow converting VideoFrame to RGB pixel formats by calling VideoFrame.copyTo()

Problem and Motivation

Converting YUV video frames to RGB is a often required for processing them in libraries like TensorFlow.js and OpenCV.js. Currently the only possible way to achive this is rendering the frame on a canvas.

let cnv = new OffscreenCanvas(frame.visibleRect.width, frame.visibleRect.height);
let ctx = cnv.getContext('2d');
ctx.drawImage(frame, 0, 0);

let imageData = ctx.getImageData(0, 0, frame.visibleRect.width, frame.visibleRect.height, 
                                 {colorSpace: colorSpace});
let buffer = imageData.data.buffer;

This can only be done syncronously and requires extra resources (explicit canvas).

API Change Proposal

Add two new members in VideoFrameCopyToOptions dictionary:

  1. format potential values are: RGBA, RGBX, BGRA, BGRX. This field specifies the pixel format for the pixel data in copyTo() destination
  2. colorSpace potential values are: srgb, display-p3 This field specifies the output color space for the pixel data in copyTo() destination
dictionary VideoFrameCopyToOptions {
  VideoPixelFormat format; 
  PredefinedColorSpace colorSpace = "srgb"; 
  DOMRectInit rect;
  sequence<PlaneLayout> layout;
};

With format specified, VideoFrame.copyTo() will be equivalent to rendering on a canvas with a given color space and calling getImageData() afterwards.

If developers need to save the video frames with BGR channel order, they can specify BGRA or BGRX pixel format without having to write code for switching channels in the ImageData buffer.

Example

const options = { 
  format: 'RGBA',
  colorSpace: 'display-p3'
};
const bufSize = frame.allocationSize(options);

const buffer = new Uint8ClampedArray(size);
await frame.copyTo(buffer, options);
const image_data = new ImageData(buffer, frame.codedWidth, frame.codedHeight,
                                 { colorSpace: 'display-p3' });

Self-Review Questionnaire: Security and Privacy

Questions to Consider

  1. What information might this feature expose to Web sites or other parties, and for what purposes is that exposure necessary?

    no new information

  2. Do features in your specification expose the minimum amount of information necessary to enable their intended uses? yes

  3. How do the features in your specification deal with personal information, personally-identifiable information (PII), or information derived from them?

    No new personal information is handled

  4. How do the features in your specification deal with sensitive information?

    The feature doesn't deail with sensitive information

  5. Do the features in your specification introduce new state for an origin that persists across browsing sessions?

    No

  6. Do the features in your specification expose information about the underlying platform to origins?

    No

  7. Does this specification allow an origin to send data to the underlying platform?

    No

  8. Do features in this specification enable access to device sensors?

    No

  9. Do features in this specification enable new script execution/loading mechanisms?

    No

  10. Do features in this specification allow an origin to access other devices?

    No

  11. Do features in this specification allow an origin some measure of control over a user agent’s native UI?

    No

  12. What temporary identifiers do the features in this specification create or expose to the web?

    No

  13. How does this specification distinguish between behavior in first-party and third-party contexts?

    Doesn't distinguish

  14. How do the features in this specification work in the context of a browser’s Private Browsing or Incognito mode?

    No difference.

  15. Does this specification have both "Security Considerations" and "Privacy Considerations" sections?

  16. Do features in your specification enable origins to downgrade default security protections?

    No

  17. How does your feature handle non-"fully active" documents?

    N/A

  18. What should this questionnaire have asked?

    Too many questions already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment