Skip to content

🎥 Make videos programmatically with React

`@remotion/media`: estimate opaque frames as 4:2:0 rather than RGBA (#10212)

Alec Watts732acce05eac
diff --git a/packages/media/src/test/get-allocation-size.test.ts b/packages/media/src/test/get-allocation-size.test.ts
new file mode 100644
index 0000000000..5f341d46bc
--- /dev/null
+++ b/packages/media/src/test/get-allocation-size.test.ts
@@ -0,0 +1,61 @@
+import {expect, test} from 'vitest';
+import {getAllocationSize} from '../video-extraction/get-allocation-size';
+import type {VideoSampleWithoutDuration} from '../video-extraction/keyframe-bank';
+
+const makeSample = ({
+ format,
+ codedWidth,
+ codedHeight,
+ allocationSize,
+}: {
+ format: string | null;
+ codedWidth: number;
+ codedHeight: number;
+ allocationSize?: number;
+}) =>
+ ({
+ format,
+ codedWidth,
+ codedHeight,
+ allocationSize: () => allocationSize ?? 0,
+ }) as unknown as VideoSampleWithoutDuration;
+
+test('a readable sample reports its own allocation size', () => {
+ const sample = makeSample({
+ format: 'I420',
+ codedWidth: 1920,
+ codedHeight: 1080,
+ allocationSize: 3110400,
+ });
+
+ expect(getAllocationSize(sample)).toBe(3110400);
+});
+
+test('an opaque 4K frame is estimated as 4:2:0, not RGBA', () => {
+ const sample = makeSample({
+ format: null,
+ codedWidth: 3840,
+ codedHeight: 2160,
+ });
+
+ // 24.9 MB, which is exactly P010 (10-bit 4:2:0), rather than the 33.2 MB
+ // that 4 bytes per pixel would charge.
+ expect(getAllocationSize(sample)).toBe(3840 * 2160 * 3);
+ expect(getAllocationSize(sample)).toBeLessThan(3840 * 2160 * 4);
+});
+
+test('the estimate still bounds real 4:2:0 layouts from above', () => {
+ const width = 3840;
+ const height = 2160;
+ const sample = makeSample({
+ format: null,
+ codedWidth: width,
+ codedHeight: height,
+ });
+
+ const nv12EightBit = width * height * 1.5;
+ const p010TenBit = width * height * 3;
+
+ expect(getAllocationSize(sample)).toBeGreaterThanOrEqual(nv12EightBit);
+ expect(getAllocationSize(sample)).toBeGreaterThanOrEqual(p010TenBit);
+});
diff --git a/packages/media/src/video-extraction/get-allocation-size.ts b/packages/media/src/video-extraction/get-allocation-size.ts
index 7d15655cd7..8bc2b21bbf 100644
--- a/packages/media/src/video-extraction/get-allocation-size.ts
+++ b/packages/media/src/video-extraction/get-allocation-size.ts
@@ -1,8 +1,26 @@
import type {VideoSampleWithoutDuration} from './keyframe-bank';
+// A sample with a `null` format is opaque -- typically a hardware-decoded frame
+// we cannot read back -- so `allocationSize()` is unavailable and the footprint
+// has to be estimated from the dimensions.
+//
+// Opaque decoder output is 4:2:0, not RGBA: NV12 for 8-bit at 1.5 bytes per
+// pixel, P010 for 10-bit at 3. Charging 4 bytes per pixel therefore overstates
+// every such frame, by 1.3x for 10-bit and 2.7x for 8-bit, and the cache evicts
+// (and re-decodes) frames it still had room for. At 4K that is 33.2 MB per
+// frame against a 12.4-24.9 MB reality, so a 1 GB budget holds ~30 frames where
+// it could hold ~40.
+//
+// 3 bytes per pixel is exact for 10-bit and still a comfortable upper bound for
+// 8-bit, so the estimate stays conservative while giving back a quarter of the
+// cache.
+const BYTES_PER_PIXEL_FOR_OPAQUE_FRAME = 3;
+
export const getAllocationSize = (sample: VideoSampleWithoutDuration) => {
if (sample.format === null) {
- return sample.codedHeight * sample.codedWidth * 4;
+ return (
+ sample.codedHeight * sample.codedWidth * BYTES_PER_PIXEL_FOR_OPAQUE_FRAME
+ );
}
return sample.allocationSize();