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

LU-3176: optimize getImageDataAtTime #52

Merged
merged 5 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lumen5/framefusion",
"version": "1.0.1",
"version": "1.0.2",
"type": "module",
"scripts": {
"docs": "typedoc framefusion.ts",
Expand Down
31 changes: 20 additions & 11 deletions src/backends/beamcoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { DownloadVideoURL } from '../DownloadVideoURL';

const VERBOSE = false;

/**
* RGBA format need one byte for every components: r, g, b and a
*/
const RGBA_PIXEL_SIZE = 4;

const createDecoder = ({
demuxer,
streamIndex,
Expand Down Expand Up @@ -246,15 +251,22 @@ export class BeamcoderExtractor extends BaseExtractor implements Extractor {
* Get imageData for a given time in seconds
* @param targetTime
*/
async getImageDataAtTime(targetTime: number): Promise<ImageData> {
async getImageDataAtTime(targetTime: number, target?: Uint8ClampedArray): Promise<ImageData> {
stepancar marked this conversation as resolved.
Show resolved Hide resolved
const targetPts = Math.round(this._timeToPTS(targetTime));
VERBOSE && console.log('targetTime', targetTime, '-> targetPts', targetPts);
const frame = await this._getFrameAtPts(targetPts);
if (!frame) {
VERBOSE && console.log('no frame found');
return null;
}
const rawData = this._resizeFrameData(frame);

let rawData = target;

if (!target) {
rawData = new Uint8ClampedArray(frame.width * frame.height * RGBA_PIXEL_SIZE);
}

this._setFrameDataToImageData(frame, rawData);

return {
data: rawData,
Expand Down Expand Up @@ -476,25 +488,22 @@ export class BeamcoderExtractor extends BaseExtractor implements Extractor {
return packet as Packet;
}

_resizeFrameData(frame): Uint8ClampedArray {
const components = 4; // 4 components: r, g, b and a
const size = frame.width * frame.height * components;
const rawData = new Uint8ClampedArray(size); // we should probably reuse this buffer
_setFrameDataToImageData(frame: beamcoder.Frame, target: Uint8ClampedArray) {
const sourceLineSize = frame.linesize as unknown as number;
// frame.data can contain multiple "planes" in other colorspaces, but in rgba, there is just one "plane", so
// our data is in frame.data[0]
const pixels = frame.data[0] as Uint8Array;

// libav creates larger buffers because it makes their internal code simpler.
// we have to trim a part at the right of each pixel row.

for (let i = 0; i < frame.height; i++) {
const sourceStart = i * sourceLineSize;
const sourceEnd = sourceStart + frame.width * components;
const sourceData = pixels.slice(sourceStart, sourceEnd);
const targetOffset = i * frame.width * components;
rawData.set(sourceData, targetOffset);
const sourceEnd = sourceStart + frame.width * RGBA_PIXEL_SIZE;
const sourceData = pixels.subarray(sourceStart, sourceEnd);
const targetOffset = i * frame.width * RGBA_PIXEL_SIZE;
target.set(sourceData, targetOffset);
}
return rawData;
}

async dispose() {
Expand Down