Repository navigation
Description
(Full credit to @sandersdan for the following analysis and design. I'm just turning his doc into a bug.)
The VideoFrame.planes API allows reading back individual planes, but it has drawbacks:
- The implementation may map and unmap the underlying frame for each plane, which can be slow when all planes are read.
- It’s overly complicated to read all planes (partners have filed bugs showing its easy to get wrong)
- Compared to its complexity, it doesn’t actually offer much control.
This issue proposes a simpler API for copying all planes at once.
Concepts
Region
A region is a {top, left, width, height} dictionary. VideoFrame should be updated to use this structure:
- Add
codedRegion, which is always{top: 0, left: 0, width: codedWidth, height: codedHeight}.- We could deprecate
codedWidthandcodedHeight, but this only seems to complicate constructingVideoFrames.
- We could deprecate
- Add
visibleRegion, replacing thecropparameters.
The default region is visibleRegion.
elementSize
The number of bytes in a sample. Each Plane has an elementSize attribute.
Stride
The number of bytes from the start of one row to the start of the next row, must be at least elementSize * region.width.
The default stride is exactly elementSize * region.width.
Layout
A layout is a list of {offset, stride} dictionaries, one for each plane. offset is the location in a buffer that a plane starts.
The default offset is tightly packed (each plane immediately follows the previous), and either all offsets should be provided or none should be.
API
// Returns the size in bytes of a buffer that can hold a copy of |region|.
VideoFrame.allocationSize({region});
Plane.allocationSize({region, stride});
// Copies all planes into an output buffer |dst|.
VideoFrame.readInto(dst, {region, layout}); // returns |layout|
Plane.readInto(dst, {region, stride}); // returns |stride|
// Returns a new frame containing a copy of a region. Holding the new frame for an
// extended period of time will not stall decoding.
VideoFrame.copy({region});
Changes to the planes API
Plane- Add codedRegion, visibleRegion.
- Add elementSize.
- Rename columns to codedWidth and rows to codedHeight.
- Deprecate length. Apps should call allocationSize().
- Deprecate stride. It’s not relevant anymore.
PlaneInit- Deprecate rows.
- Add offset.
Examples
Copy visibleRegion to a new buffer (packed)
let buf = new ArrayBuffer(frame.allocationSize());
let layout = frame.readInto(buf);
// For 1080p I420, |layout| = [{offset: 0, stride: 1920},
// {offset: 2073600, stride: 960},
// {offset: 2592000, stride: 960}]
Copy codedRegion to a WASM frame pool
let region = frame.codedRegion;
let layout = wasmApp.allocateYUV(region.width, region.height);
frame.readInto(wasmApp.memory, {region, layout});
Activity
[-]Simplifying and optimizing VideoFrame[/-][+]Simplifying and optimizing VideoFrame read-back [/+][-]Simplifying and optimizing VideoFrame read-back [/-][+]Simplifying and optimizing VideoFrame readback [/+]dalecurtis commentedon Apr 28, 2021
One of the key aspects of the current
Planes.readInto()is that it's synchronous. Since frames are already being delivered asynchronously via decoding callbacks and theMediaStreamTrackProcessor, it would be unfortunate to add another hop in order to access information that may be readily available without an async hop.I think this API needs to have a
readIntoSync()method and potentially areadableattribute that indicates when readInto can be completed synchronously. E.g., the frame is a simple cpu memory backed frame or otherwise synchronously mappable. A texture backed frame would not be available for synchronous readback.Without a
readableattributereadIntoSync()would need to return null or otherwise fail, so I defer to the common spec idioms for whether we'd want an attribute versus just returning null.sandersdan commentedon Apr 28, 2021
Clarification: since the proposal @chcunningham posted, we determined that the default implementation of
readInto()should be async so that we can do async texture readback in the implementation. Synchronous texture readback is strongly discouraged by Chrome's GPU experts.That leaves a gap where some frames can be read synchronously but there is no way to express that without adding new methods. It's not yet clear how important the difference is, but the proposal to close the gap is as described by @dalecurtis above; add a fallible
readIntoSync()and areadableattribute to signal whenreadIntoSync()is expected to work.I'll also throw in my own bike shed, I have a preference to call the attribute
readableSyncas justreadableis rather generic.dalecurtis commentedon Apr 28, 2021
Additional bike shed colors:
chcunningham commentedon May 4, 2021
I opened some design discussion comments in the PR, but I'm resolving those in favor of focusing the discussion here where it's more visible/readable. Three points for discussion:
We need asynchronous readback (as Dan notes above). However, we may also want synchronous readback where feasible (with a naming bikeshed above for the method and capability signaling).
Both sync and async methods under consideration in point 1 live on VideoFrame. Do we still have a need for a readInto() method (currently sync) on planes? Planes.readInto() affords the power to read just one plane when desired. A bit niche, and can be solved via other mechanisms using videoFrame.readInto().
example of another mechanism:
If the outcome of point 2 is to remove planes.readInto(), should we also remove videoFrame.planes entirely? The remaining Planes attributes were mainly provided to assist in calling planes.readInto().
chcunningham commentedon May 12, 2021
Triage note: marked 'breaking' as, as we are proposing a whole new readback API and discussing removal of old paths.
chcunningham commentedon May 13, 2021
Another consideration: naming bikeshed method to get the pixel data
frame.readInto(dest)chunk.copyTo(dest). Decided againstcopy*In*to(), as described in this thread. So maybe this should be simplyframe.readTo(dest)readprefix is useful vs just calling thiscopy.. folks seem to lean keepingreadas part of history (video frame "read back") and hint that this isn't always a simple copy from cpu memory (e.g. may need copy and unswizzle from gpu / texture etc). Personally, I like 'read' prefix ok.In conclusion, I propose we call it
videoFrame.readTo(dest).dalecurtis commentedon May 13, 2021
I think the difference is subtle enough that if every other interface will have
copyTo()instead, then calling this onecopyTo()sounds better to me in terms of consistency.chcunningham commentedon May 13, 2021
I'm also fine with that.
padenot commentedon May 19, 2021
Access to each plane individually is also needed for WebGPU/WebGL interrop, although we don't really know the preferred API shape.
copyTois fine to me if we want uniformity, yes.chcunningham commentedon Jun 11, 2021
Fixed by #208