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

Method encodeInternal

src/video_encoder.ts:447–570  ·  view source on GitHub ↗
(clonedFrame: VideoFrame, options?: VideoEncoderEncodeOptions)

Source from the content-addressed store, hash-verified

445 lastBuffer: Buffer | null = null;
446
447 private async encodeInternal(clonedFrame: VideoFrame, options?: VideoEncoderEncodeOptions) {
448 const currentConfigId = this.currentConfigId;
449
450 using lock = this.mutex.lock();
451 await lock.ready; // Unconditional await here to queue a microtask
452
453 if (this.currentConfigId !== currentConfigId) {
454 clonedFrame.close();
455 return;
456 }
457
458 // Spec 6.5: Decrement [[encodeQueueSize]] and run the Schedule Dequeue Event algorithm.
459 this.decrementQueue();
460
461 try {
462 assert(this.codecContext);
463
464 if (clonedFrame instanceof VideoFramePolyfill && clonedFrame.data instanceof Frame) {
465 const otherFrame = clonedFrame.data;
466 this.frame.ref(otherFrame);
467 } else {
468 // Copy frame data from VideoFrame to FFmpeg Frame
469 this.frame.format = fromPixelFormat(clonedFrame.format as string);
470 this.frame.width = clonedFrame.codedWidth;
471 this.frame.height = clonedFrame.codedHeight;
472 this.frame.pts = BigInt(clonedFrame.timestamp);
473 this.frame.duration = BigInt(clonedFrame.duration ?? 0);
474 this.frame.colorPrimaries
475 = mapColorPrimaries(clonedFrame.colorSpace.primaries ?? 'unknown') ?? AVCOL_PRI_UNSPECIFIED;
476 this.frame.colorSpace
477 = mapMatrixCoefficients(clonedFrame.colorSpace.matrix ?? 'unknown') ?? AVCOL_SPC_UNSPECIFIED;
478 this.frame.colorTrc
479 = mapTransferCharacteristics(clonedFrame.colorSpace.transfer ?? 'unknown') ?? AVCOL_TRC_UNSPECIFIED;
480 this.frame.colorRange = clonedFrame.colorSpace.fullRange === false
481 ? AVCOL_RANGE_MPEG
482 : clonedFrame.colorSpace.fullRange === true
483 ? AVCOL_RANGE_JPEG
484 : AVCOL_RANGE_UNSPECIFIED;
485 this.frame.keyFrame = options?.keyFrame ? 1 : 0;
486
487 const size = clonedFrame.allocationSize();
488 if (!this.lastBuffer || this.lastBuffer.byteLength !== size) {
489 this.lastBuffer = Buffer.from({ length: size });
490 }
491
492 await clonedFrame.copyTo(this.lastBuffer);
493 this.frame.fromBuffer(this.lastBuffer);
494 }
495 } finally {
496 clonedFrame.close();
497 }
498
499 if (!this.colorSpaceSet) {
500 this.codecContext.colorPrimaries = this.frame.colorPrimaries;
501 this.codecContext.colorTrc = this.frame.colorTrc;
502 this.codecContext.colorSpace = this.frame.colorSpace;
503 this.codecContext.colorRange = this.frame.colorRange;
504

Callers 1

encodeMethod · 0.95

Calls 11

decrementQueueMethod · 0.95
outputChunkMethod · 0.95
assertFunction · 0.85
fromPixelFormatFunction · 0.85
mapColorPrimariesFunction · 0.85
mapMatrixCoefficientsFunction · 0.85
lockMethod · 0.80
closeMethod · 0.45
allocationSizeMethod · 0.45
copyToMethod · 0.45

Tested by

no test coverage detected