MCPcopy Create free account
hub / github.com/Vanilagy/webcodecs-polyfill / ComputeLayoutAndAllocationSize

Function ComputeLayoutAndAllocationSize

src/video_frame.ts:743–850  ·  view source on GitHub ↗
(
	parsedRect: DOMRectInit,
	format: VideoPixelFormat,
	layout?: PlaneLayout[],
)

Source from the content-addressed store, hash-verified

741};
742
743const ComputeLayoutAndAllocationSize = (
744 parsedRect: DOMRectInit,
745 format: VideoPixelFormat,
746 layout?: PlaneLayout[],
747): CombinedBufferLayout => {
748 const info = getPixelFormatInfo(format);
749 if (!info) throw new TypeError('Unknown format');
750
751 // 1. Let numPlanes be the number of planes as defined by format.
752 const numPlanes = info.planes.length;
753
754 // 2. If layout is not undefined and its length does not equal numPlanes, throw a TypeError.
755 if (layout !== undefined && layout.length !== numPlanes) {
756 throw new TypeError(`Layout must have ${numPlanes} planes`);
757 }
758
759 // 3. Let minAllocationSize be 0.
760 let minAllocationSize = 0;
761
762 // 4. Let computedLayouts be a new list.
763 const computedLayouts: ComputedPlaneLayout[] = [];
764
765 // 5. Let endOffsets be a new list.
766 const endOffsets: number[] = [];
767
768 // 6. Let planeIndex be 0.
769 // 7. While planeIndex < numPlanes:
770 for (let planeIndex = 0; planeIndex < numPlanes; planeIndex++) {
771 const plane = info.planes[planeIndex];
772 const sampleBytes = plane.bytesPerSample;
773 const sampleWidth = plane.sampleWidth;
774 const sampleHeight = plane.sampleHeight;
775
776 // Let computedLayout be a new computed plane layout.
777 const computedLayout: ComputedPlaneLayout = {
778 destinationOffset: 0,
779 destinationStride: 0,
780 sourceTop: 0,
781 sourceHeight: 0,
782 sourceLeftBytes: 0,
783 sourceWidthBytes: 0,
784 };
785
786 // Set computedLayout’s sourceTop...
787 computedLayout.sourceTop = Math.ceil(Math.trunc(parsedRect.y || 0) / sampleHeight);
788 // Set computedLayout’s sourceHeight...
789 computedLayout.sourceHeight = Math.ceil(Math.trunc(parsedRect.height || 0) / sampleHeight);
790 // Set computedLayout’s sourceLeftBytes...
791 computedLayout.sourceLeftBytes = Math.floor(Math.trunc(parsedRect.x || 0) / sampleWidth) * sampleBytes;
792 // Set computedLayout’s sourceWidthBytes...
793 computedLayout.sourceWidthBytes = Math.floor(Math.trunc(parsedRect.width || 0) / sampleWidth) * sampleBytes;
794
795 // If layout is not undefined:
796 if (layout !== undefined) {
797 const planeLayout = layout[planeIndex];
798 // If planeLayout.stride is less than computedLayout’s sourceWidthBytes, return a TypeError.
799 if (planeLayout.stride < computedLayout.sourceWidthBytes) {
800 throw new TypeError(`Stride for plane ${planeIndex} is too small`);

Callers 2

constructorMethod · 0.85

Calls 1

getPixelFormatInfoFunction · 0.85

Tested by

no test coverage detected