init
This commit is contained in:
+55
@@ -0,0 +1,55 @@
|
||||
import type { HEVCFrame, HEVCStreamInfo, DecodeResult, DecoderOptions } from "./types.js";
|
||||
/**
|
||||
* HEVC/H.265 Decoder — JavaScript wrapper for the WASM module.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const decoder = await HEVCDecoder.create();
|
||||
* const { frames, info } = decoder.decode(bitstreamBytes);
|
||||
* console.log(`${info.width}x${info.height}, ${frames.length} frames`);
|
||||
* decoder.destroy();
|
||||
* ```
|
||||
*/
|
||||
export declare class HEVCDecoder {
|
||||
private _m;
|
||||
private _api;
|
||||
private _dec;
|
||||
private constructor();
|
||||
/**
|
||||
* Create a new decoder instance. Loads the WASM module.
|
||||
*/
|
||||
static create(options?: DecoderOptions): Promise<HEVCDecoder>;
|
||||
/**
|
||||
* Decode a complete HEVC bitstream.
|
||||
* @param data Raw .265 bitstream bytes
|
||||
*/
|
||||
decode(data: Uint8Array): DecodeResult;
|
||||
/** Number of decoded frames available */
|
||||
get frameCount(): number;
|
||||
/** Get stream info (available after decode) */
|
||||
get info(): HEVCStreamInfo | null;
|
||||
private _extractFrame;
|
||||
private _extractDrainedFrame;
|
||||
private _readFrameFromPtr;
|
||||
private _extractInfo;
|
||||
/**
|
||||
* Feed a chunk of data containing one or more complete NAL units.
|
||||
* The decoder accumulates parameter sets and decodes pictures incrementally.
|
||||
* Call drain() after each feed() to retrieve output-ready frames.
|
||||
*/
|
||||
feed(data: Uint8Array): void;
|
||||
/**
|
||||
* Drain output-ready frames from the decoder (§C.5.2 bumping process).
|
||||
* Returns frames in display order, only when ready per DPB constraints.
|
||||
* Frames are valid until the next feed() or destroy() call.
|
||||
*/
|
||||
drain(): HEVCFrame[];
|
||||
/**
|
||||
* Flush all remaining frames from the DPB (call at end of stream).
|
||||
* Returns all buffered frames in display order.
|
||||
*/
|
||||
flush(): HEVCFrame[];
|
||||
/** Release decoder resources */
|
||||
destroy(): void;
|
||||
}
|
||||
//# sourceMappingURL=decoder.d.ts.map
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
export { HEVCDecoder } from "./decoder.js";
|
||||
export type { HEVCFrame, HEVCStreamInfo, DecodeResult, DecoderOptions, WorkerRequest, WorkerResponse, } from "./types.js";
|
||||
export { H264Encoder } from "./h264-encoder.js";
|
||||
export type { H264EncoderConfig, EncodedChunk } from "./h264-encoder.js";
|
||||
export { FrameRenderer } from "./renderer.js";
|
||||
export { FMP4Demuxer } from "./fmp4-demuxer.js";
|
||||
export type { DemuxedSample, VideoTrackInfo } from "./fmp4-demuxer.js";
|
||||
export { FMP4Muxer } from "./fmp4-muxer.js";
|
||||
export type { MuxerInitConfig, MuxerSample } from "./fmp4-muxer.js";
|
||||
export { MSEController } from "./mse-controller.js";
|
||||
export { TranscodePipeline } from "./transcode-pipeline.js";
|
||||
export type { TranscodePipelineConfig } from "./transcode-pipeline.js";
|
||||
export { setLogLevel } from "./log.js";
|
||||
export type { LogLevel } from "./log.js";
|
||||
export { installMSEIntercept, uninstallMSEIntercept } from "./mse-intercept.js";
|
||||
export type { MSEInterceptConfig } from "./mse-intercept.js";
|
||||
export { SegmentTranscoder } from "./segment-transcoder.js";
|
||||
export type { SegmentTranscoderConfig, TranscodedInit } from "./segment-transcoder.js";
|
||||
export { TranscodeWorkerClient } from "./transcode-worker-client.js";
|
||||
export type { TranscodeWorkerClientConfig } from "./transcode-worker-client.js";
|
||||
export { hevcMimeToH264Codec } from "./codec-mapping.js";
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
+2227
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* YUV Frame Renderer — converts decoded YUV frames to displayable video.
|
||||
*
|
||||
* Uses VideoFrame + MediaStreamTrackGenerator when available (Chrome 94+),
|
||||
* falls back to WebGL canvas rendering.
|
||||
*/
|
||||
import type { HEVCFrame } from "./types.js";
|
||||
/**
|
||||
* Renderer that converts YUV frames to a <video>-compatible MediaStream
|
||||
* or renders directly to a canvas.
|
||||
*/
|
||||
export declare class FrameRenderer {
|
||||
private _generator;
|
||||
private _writer;
|
||||
private _canvas;
|
||||
private _gl;
|
||||
private _program;
|
||||
private _texY;
|
||||
private _texCb;
|
||||
private _texCr;
|
||||
/**
|
||||
* Check if MediaStreamTrackGenerator is available (Chrome 94+).
|
||||
* When available, frames can be piped to a <video> element.
|
||||
*/
|
||||
static get supportsMediaStream(): boolean;
|
||||
/**
|
||||
* Get a MediaStream that can be assigned to a <video>.srcObject.
|
||||
* Only available when supportsMediaStream is true.
|
||||
*/
|
||||
getMediaStream(): MediaStream | null;
|
||||
/**
|
||||
* Render a decoded YUV frame.
|
||||
*
|
||||
* If MediaStreamTrackGenerator is available, creates a VideoFrame and writes it.
|
||||
* Otherwise, renders to the provided canvas via WebGL.
|
||||
*/
|
||||
renderFrame(frame: HEVCFrame, timestamp: number): Promise<void>;
|
||||
/**
|
||||
* Initialize WebGL canvas fallback.
|
||||
* Call this if MediaStreamTrackGenerator is not supported.
|
||||
*/
|
||||
initCanvas(canvas: HTMLCanvasElement | OffscreenCanvas): void;
|
||||
private _renderToVideoFrame;
|
||||
private _initWebGL;
|
||||
private _renderToWebGL;
|
||||
/** Release all resources */
|
||||
destroy(): void;
|
||||
}
|
||||
//# sourceMappingURL=renderer.d.ts.map
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
/** Decoded YUV frame — planes are copied out of WASM heap */
|
||||
export interface HEVCFrame {
|
||||
/** Luma plane (packed, no stride) */
|
||||
y: Uint16Array;
|
||||
/** Chroma Cb plane */
|
||||
cb: Uint16Array;
|
||||
/** Chroma Cr plane */
|
||||
cr: Uint16Array;
|
||||
/** Luma width (display, after conformance crop) */
|
||||
width: number;
|
||||
/** Luma height (display) */
|
||||
height: number;
|
||||
/** Chroma plane width */
|
||||
chromaWidth: number;
|
||||
/** Chroma plane height */
|
||||
chromaHeight: number;
|
||||
/** Bit depth (8 or 10) */
|
||||
bitDepth: number;
|
||||
/** Picture Order Count (display order) */
|
||||
poc: number;
|
||||
}
|
||||
/** Stream metadata — available after first decode */
|
||||
export interface HEVCStreamInfo {
|
||||
width: number;
|
||||
height: number;
|
||||
bitDepth: number;
|
||||
/** 0=mono, 1=4:2:0, 2=4:2:2, 3=4:4:4 */
|
||||
chromaFormat: number;
|
||||
/** Profile IDC (1=Main, 2=Main10) */
|
||||
profile: number;
|
||||
/** Level IDC (e.g. 93 = Level 3.1) */
|
||||
level: number;
|
||||
}
|
||||
/** Result of a decode call */
|
||||
export interface DecodeResult {
|
||||
frames: HEVCFrame[];
|
||||
info: HEVCStreamInfo | null;
|
||||
}
|
||||
/** Options for creating a decoder */
|
||||
export interface DecoderOptions {
|
||||
/** URL to the hevc-decode.js WASM glue file. Auto-resolved if omitted. */
|
||||
wasmUrl?: string;
|
||||
/** URL to the .wasm binary. Auto-resolved if omitted. */
|
||||
wasmBinaryUrl?: string;
|
||||
}
|
||||
/** Worker message types (main → worker) */
|
||||
export type WorkerRequest = {
|
||||
type: "init";
|
||||
wasmUrl: string;
|
||||
} | {
|
||||
type: "decode";
|
||||
data: ArrayBuffer;
|
||||
} | {
|
||||
type: "feed";
|
||||
data: ArrayBuffer;
|
||||
} | {
|
||||
type: "drain";
|
||||
} | {
|
||||
type: "flush";
|
||||
} | {
|
||||
type: "destroy";
|
||||
};
|
||||
/** Worker message types (worker → main) */
|
||||
export type WorkerResponse = {
|
||||
type: "ready";
|
||||
} | {
|
||||
type: "info";
|
||||
info: HEVCStreamInfo;
|
||||
} | {
|
||||
type: "frame";
|
||||
index: number;
|
||||
frame: HEVCFrame;
|
||||
} | {
|
||||
type: "done";
|
||||
frameCount: number;
|
||||
} | {
|
||||
type: "drained";
|
||||
frames: HEVCFrame[];
|
||||
} | {
|
||||
type: "flushed";
|
||||
frames: HEVCFrame[];
|
||||
} | {
|
||||
type: "error";
|
||||
message: string;
|
||||
};
|
||||
//# sourceMappingURL=types.d.ts.map
|
||||
File diff suppressed because one or more lines are too long
Binary file not shown.
+408
@@ -0,0 +1,408 @@
|
||||
// src/decoder.ts
|
||||
var HEVCDecoder = class _HEVCDecoder {
|
||||
constructor(module) {
|
||||
this._m = module;
|
||||
this._api = {
|
||||
create: module.cwrap("hevc_decoder_create", "number", []),
|
||||
destroy: module.cwrap("hevc_decoder_destroy", null, ["number"]),
|
||||
decode: module.cwrap("hevc_decoder_decode", "number", ["number", "number", "number"]),
|
||||
getFrameCount: module.cwrap("hevc_decoder_get_frame_count", "number", ["number"]),
|
||||
getFrame: module.cwrap("hevc_decoder_get_frame", "number", ["number", "number", "number"]),
|
||||
getInfo: module.cwrap("hevc_decoder_get_info", "number", ["number", "number"]),
|
||||
feed: module.cwrap("hevc_decoder_feed", "number", ["number", "number", "number"]),
|
||||
drain: module.cwrap("hevc_decoder_drain", "number", ["number", "number"]),
|
||||
getDrainedFrame: module.cwrap("hevc_decoder_get_drained_frame", "number", ["number", "number", "number"]),
|
||||
flush: module.cwrap("hevc_decoder_flush", "number", ["number"])
|
||||
};
|
||||
this._dec = this._api.create();
|
||||
if (!this._dec) throw new Error("Failed to create HEVC decoder");
|
||||
}
|
||||
/**
|
||||
* Create a new decoder instance. Loads the WASM module.
|
||||
*/
|
||||
static async create(options) {
|
||||
const factoryOpts = {};
|
||||
if (options?.wasmBinaryUrl) {
|
||||
factoryOpts.locateFile = () => options.wasmBinaryUrl;
|
||||
}
|
||||
const g = globalThis;
|
||||
if (typeof g.HEVCDecoderModule === "function") {
|
||||
const module2 = await g.HEVCDecoderModule(factoryOpts);
|
||||
return new _HEVCDecoder(module2);
|
||||
}
|
||||
const wasmUrl = options?.wasmUrl ?? "./wasm/hevc-decode.js";
|
||||
const mod = await import(
|
||||
/* @vite-ignore */
|
||||
wasmUrl
|
||||
);
|
||||
const fn = mod.default ?? mod;
|
||||
const module = await fn(factoryOpts);
|
||||
return new _HEVCDecoder(module);
|
||||
}
|
||||
/**
|
||||
* Decode a complete HEVC bitstream.
|
||||
* @param data Raw .265 bitstream bytes
|
||||
*/
|
||||
decode(data) {
|
||||
const m = this._m;
|
||||
const ptr = m._malloc(data.length);
|
||||
try {
|
||||
m.HEAPU8.set(data, ptr);
|
||||
const ret = this._api.decode(this._dec, ptr, data.length);
|
||||
if (ret !== 0) throw new Error(`Decode failed (code ${ret})`);
|
||||
const count = this._api.getFrameCount(this._dec);
|
||||
const frames = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
const frame = this._extractFrame(i);
|
||||
if (frame) frames.push(frame);
|
||||
}
|
||||
const info = this._extractInfo();
|
||||
return { frames, info };
|
||||
} finally {
|
||||
m._free(ptr);
|
||||
}
|
||||
}
|
||||
/** Number of decoded frames available */
|
||||
get frameCount() {
|
||||
return this._api.getFrameCount(this._dec);
|
||||
}
|
||||
/** Get stream info (available after decode) */
|
||||
get info() {
|
||||
return this._extractInfo();
|
||||
}
|
||||
_extractFrame(index) {
|
||||
const m = this._m;
|
||||
const framePtr = m._malloc(48);
|
||||
try {
|
||||
const ret = this._api.getFrame(this._dec, index, framePtr);
|
||||
if (ret !== 0) return null;
|
||||
return this._readFrameFromPtr(framePtr);
|
||||
} finally {
|
||||
m._free(framePtr);
|
||||
}
|
||||
}
|
||||
_extractDrainedFrame(index) {
|
||||
const m = this._m;
|
||||
const framePtr = m._malloc(48);
|
||||
try {
|
||||
const ret = this._api.getDrainedFrame(this._dec, index, framePtr);
|
||||
if (ret !== 0) return null;
|
||||
return this._readFrameFromPtr(framePtr);
|
||||
} finally {
|
||||
m._free(framePtr);
|
||||
}
|
||||
}
|
||||
_readFrameFromPtr(framePtr) {
|
||||
const m = this._m;
|
||||
const yPtr = m.getValue(framePtr, "*");
|
||||
const cbPtr = m.getValue(framePtr + 4, "*");
|
||||
const crPtr = m.getValue(framePtr + 8, "*");
|
||||
const width = m.getValue(framePtr + 12, "i32");
|
||||
const height = m.getValue(framePtr + 16, "i32");
|
||||
const strideY = m.getValue(framePtr + 20, "i32");
|
||||
const strideC = m.getValue(framePtr + 24, "i32");
|
||||
const cw = m.getValue(framePtr + 28, "i32");
|
||||
const ch = m.getValue(framePtr + 32, "i32");
|
||||
const bd = m.getValue(framePtr + 36, "i32");
|
||||
const poc = m.getValue(framePtr + 40, "i32");
|
||||
const y = copyPlane(m, yPtr, width, height, strideY);
|
||||
const cb = copyPlane(m, cbPtr, cw, ch, strideC);
|
||||
const cr = copyPlane(m, crPtr, cw, ch, strideC);
|
||||
return { y, cb, cr, width, height, chromaWidth: cw, chromaHeight: ch, bitDepth: bd, poc };
|
||||
}
|
||||
_extractInfo() {
|
||||
const m = this._m;
|
||||
const infoPtr = m._malloc(24);
|
||||
try {
|
||||
const ret = this._api.getInfo(this._dec, infoPtr);
|
||||
if (ret !== 0) return null;
|
||||
return {
|
||||
width: m.getValue(infoPtr, "i32"),
|
||||
height: m.getValue(infoPtr + 4, "i32"),
|
||||
bitDepth: m.getValue(infoPtr + 8, "i32"),
|
||||
chromaFormat: m.getValue(infoPtr + 12, "i32"),
|
||||
profile: m.getValue(infoPtr + 16, "i32"),
|
||||
level: m.getValue(infoPtr + 20, "i32")
|
||||
};
|
||||
} finally {
|
||||
m._free(infoPtr);
|
||||
}
|
||||
}
|
||||
// --- Incremental API (streaming) ---
|
||||
/**
|
||||
* Feed a chunk of data containing one or more complete NAL units.
|
||||
* The decoder accumulates parameter sets and decodes pictures incrementally.
|
||||
* Call drain() after each feed() to retrieve output-ready frames.
|
||||
*/
|
||||
feed(data) {
|
||||
const m = this._m;
|
||||
const ptr = m._malloc(data.length);
|
||||
try {
|
||||
m.HEAPU8.set(data, ptr);
|
||||
const ret = this._api.feed(this._dec, ptr, data.length);
|
||||
if (ret !== 0) throw new Error(`Feed failed (code ${ret})`);
|
||||
} finally {
|
||||
m._free(ptr);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Drain output-ready frames from the decoder (§C.5.2 bumping process).
|
||||
* Returns frames in display order, only when ready per DPB constraints.
|
||||
* Frames are valid until the next feed() or destroy() call.
|
||||
*/
|
||||
drain() {
|
||||
const m = this._m;
|
||||
const countPtr = m._malloc(4);
|
||||
try {
|
||||
const ret = this._api.drain(this._dec, countPtr);
|
||||
if (ret !== 0) return [];
|
||||
const count = m.getValue(countPtr, "i32");
|
||||
const frames = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
const frame = this._extractDrainedFrame(i);
|
||||
if (frame) frames.push(frame);
|
||||
}
|
||||
return frames;
|
||||
} finally {
|
||||
m._free(countPtr);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Flush all remaining frames from the DPB (call at end of stream).
|
||||
* Returns all buffered frames in display order.
|
||||
*/
|
||||
flush() {
|
||||
const ret = this._api.flush(this._dec);
|
||||
if (ret !== 0) return [];
|
||||
const m = this._m;
|
||||
const countPtr = m._malloc(4);
|
||||
try {
|
||||
const frames = [];
|
||||
const framePtr = m._malloc(48);
|
||||
try {
|
||||
for (let i = 0; ; i++) {
|
||||
const r = this._api.getDrainedFrame(this._dec, i, framePtr);
|
||||
if (r !== 0) break;
|
||||
frames.push(this._readFrameFromPtr(framePtr));
|
||||
}
|
||||
} finally {
|
||||
m._free(framePtr);
|
||||
}
|
||||
return frames;
|
||||
} finally {
|
||||
m._free(countPtr);
|
||||
}
|
||||
}
|
||||
/** Release decoder resources */
|
||||
destroy() {
|
||||
if (this._dec) {
|
||||
this._api.destroy(this._dec);
|
||||
this._dec = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
function copyPlane(m, ptr, width, height, stride) {
|
||||
const out = new Uint16Array(width * height);
|
||||
const base = ptr >> 1;
|
||||
for (let y = 0; y < height; y++) {
|
||||
out.set(m.HEAPU16.subarray(base + y * stride, base + y * stride + width), y * width);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// src/renderer.ts
|
||||
var FrameRenderer = class _FrameRenderer {
|
||||
constructor() {
|
||||
this._generator = null;
|
||||
this._writer = null;
|
||||
this._canvas = null;
|
||||
this._gl = null;
|
||||
this._program = null;
|
||||
this._texY = null;
|
||||
this._texCb = null;
|
||||
this._texCr = null;
|
||||
}
|
||||
/**
|
||||
* Check if MediaStreamTrackGenerator is available (Chrome 94+).
|
||||
* When available, frames can be piped to a <video> element.
|
||||
*/
|
||||
static get supportsMediaStream() {
|
||||
return typeof MediaStreamTrackGenerator !== "undefined";
|
||||
}
|
||||
/**
|
||||
* Get a MediaStream that can be assigned to a <video>.srcObject.
|
||||
* Only available when supportsMediaStream is true.
|
||||
*/
|
||||
getMediaStream() {
|
||||
if (!_FrameRenderer.supportsMediaStream) return null;
|
||||
if (!this._generator) {
|
||||
this._generator = new MediaStreamTrackGenerator({ kind: "video" });
|
||||
this._writer = this._generator.writable.getWriter();
|
||||
}
|
||||
return new MediaStream([this._generator]);
|
||||
}
|
||||
/**
|
||||
* Render a decoded YUV frame.
|
||||
*
|
||||
* If MediaStreamTrackGenerator is available, creates a VideoFrame and writes it.
|
||||
* Otherwise, renders to the provided canvas via WebGL.
|
||||
*/
|
||||
async renderFrame(frame, timestamp) {
|
||||
if (this._writer) {
|
||||
await this._renderToVideoFrame(frame, timestamp);
|
||||
} else if (this._gl) {
|
||||
this._renderToWebGL(frame);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Initialize WebGL canvas fallback.
|
||||
* Call this if MediaStreamTrackGenerator is not supported.
|
||||
*/
|
||||
initCanvas(canvas) {
|
||||
this._canvas = canvas;
|
||||
const gl = canvas.getContext("webgl");
|
||||
if (!gl) throw new Error("WebGL not supported");
|
||||
this._gl = gl;
|
||||
this._initWebGL(gl);
|
||||
}
|
||||
async _renderToVideoFrame(frame, timestamp) {
|
||||
const w = frame.width;
|
||||
const h = frame.height;
|
||||
const cw = frame.chromaWidth;
|
||||
const ch = frame.chromaHeight;
|
||||
const shift = frame.bitDepth > 8 ? frame.bitDepth - 8 : 0;
|
||||
const i420 = new Uint8Array(w * h + cw * ch * 2);
|
||||
let dst = 0;
|
||||
for (let i = 0; i < w * h; i++) {
|
||||
i420[dst++] = frame.y[i] >> shift;
|
||||
}
|
||||
for (let i = 0; i < cw * ch; i++) {
|
||||
i420[dst++] = frame.cb[i] >> shift;
|
||||
}
|
||||
for (let i = 0; i < cw * ch; i++) {
|
||||
i420[dst++] = frame.cr[i] >> shift;
|
||||
}
|
||||
const videoFrame = new VideoFrame(i420, {
|
||||
format: "I420",
|
||||
codedWidth: w,
|
||||
codedHeight: h,
|
||||
timestamp
|
||||
});
|
||||
await this._writer.write(videoFrame);
|
||||
videoFrame.close();
|
||||
}
|
||||
_initWebGL(gl) {
|
||||
const vs = gl.createShader(gl.VERTEX_SHADER);
|
||||
gl.shaderSource(vs, VERTEX_SRC);
|
||||
gl.compileShader(vs);
|
||||
const fs = gl.createShader(gl.FRAGMENT_SHADER);
|
||||
gl.shaderSource(fs, FRAGMENT_SRC);
|
||||
gl.compileShader(fs);
|
||||
this._program = gl.createProgram();
|
||||
gl.attachShader(this._program, vs);
|
||||
gl.attachShader(this._program, fs);
|
||||
gl.linkProgram(this._program);
|
||||
gl.useProgram(this._program);
|
||||
const buf = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
|
||||
gl.bufferData(
|
||||
gl.ARRAY_BUFFER,
|
||||
new Float32Array([-1, -1, 0, 1, 1, -1, 1, 1, -1, 1, 0, 0, 1, 1, 1, 0]),
|
||||
gl.STATIC_DRAW
|
||||
);
|
||||
const aPos = gl.getAttribLocation(this._program, "a_pos");
|
||||
const aTex = gl.getAttribLocation(this._program, "a_tex");
|
||||
gl.enableVertexAttribArray(aPos);
|
||||
gl.enableVertexAttribArray(aTex);
|
||||
gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 16, 0);
|
||||
gl.vertexAttribPointer(aTex, 2, gl.FLOAT, false, 16, 8);
|
||||
this._texY = createTexture(gl, 0);
|
||||
this._texCb = createTexture(gl, 1);
|
||||
this._texCr = createTexture(gl, 2);
|
||||
gl.uniform1i(gl.getUniformLocation(this._program, "u_texY"), 0);
|
||||
gl.uniform1i(gl.getUniformLocation(this._program, "u_texCb"), 1);
|
||||
gl.uniform1i(gl.getUniformLocation(this._program, "u_texCr"), 2);
|
||||
}
|
||||
_renderToWebGL(frame) {
|
||||
const gl = this._gl;
|
||||
const canvas = this._canvas;
|
||||
// ВАЖНО: в Web Worker нет HTMLCanvasElement (DOM-интерфейс главного потока) —
|
||||
// голый `instanceof HTMLCanvasElement` кинул бы ReferenceError. Для OffscreenCanvas
|
||||
// размер не трогаем (его задаёт главный поток), рисуем по текущему canvas.width/height.
|
||||
if (typeof HTMLCanvasElement !== "undefined" && canvas instanceof HTMLCanvasElement) {
|
||||
canvas.width = frame.width;
|
||||
canvas.height = frame.height;
|
||||
}
|
||||
// viewport по размеру КАНВАСА (не кадра): для OffscreenCanvas в воркере это
|
||||
// позволяет рисовать в уменьшённый буфер, GPU сам масштабирует текстуру (LINEAR).
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
const shift = frame.bitDepth > 8 ? frame.bitDepth - 8 : 0;
|
||||
uploadPlane(gl, this._texY, 0, frame.y, frame.width, frame.height, shift);
|
||||
uploadPlane(gl, this._texCb, 1, frame.cb, frame.chromaWidth, frame.chromaHeight, shift);
|
||||
uploadPlane(gl, this._texCr, 2, frame.cr, frame.chromaWidth, frame.chromaHeight, shift);
|
||||
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
||||
}
|
||||
/** Release all resources */
|
||||
destroy() {
|
||||
this._writer?.close();
|
||||
this._generator?.stop();
|
||||
this._generator = null;
|
||||
this._writer = null;
|
||||
this._gl = null;
|
||||
this._canvas = null;
|
||||
}
|
||||
};
|
||||
function createTexture(gl, unit) {
|
||||
gl.activeTexture(gl.TEXTURE0 + unit);
|
||||
const tex = gl.createTexture();
|
||||
gl.bindTexture(gl.TEXTURE_2D, tex);
|
||||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
||||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
||||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
||||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
||||
return tex;
|
||||
}
|
||||
function uploadPlane(gl, tex, unit, data, width, height, shift) {
|
||||
gl.activeTexture(gl.TEXTURE0 + unit);
|
||||
gl.bindTexture(gl.TEXTURE_2D, tex);
|
||||
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
|
||||
if (shift === 0) {
|
||||
// data — Uint16Array со значениями 0..255 (little-endian: байт[2i]=значение, байт[2i+1]=0).
|
||||
// Грузим СЫРЫЕ байты как LUMINANCE_ALPHA (2 байта/тексель): шейдер берёт .r = L = младший байт = значение.
|
||||
// Так убираем per-pixel JS-цикл 16→8 бит — главный тормоз показа на 3200×1800.
|
||||
const bytes = new Uint8Array(data.buffer, data.byteOffset, width * height * 2);
|
||||
gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE_ALPHA, width, height, 0, gl.LUMINANCE_ALPHA, gl.UNSIGNED_BYTE, bytes);
|
||||
} else {
|
||||
const u8 = new Uint8Array(width * height);
|
||||
for (let i = 0; i < width * height; i++) u8[i] = Math.min(255, data[i] >> shift);
|
||||
gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, width, height, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, u8);
|
||||
}
|
||||
}
|
||||
var VERTEX_SRC = `
|
||||
attribute vec2 a_pos;
|
||||
attribute vec2 a_tex;
|
||||
varying vec2 v_tex;
|
||||
void main() {
|
||||
gl_Position = vec4(a_pos, 0.0, 1.0);
|
||||
v_tex = a_tex;
|
||||
}
|
||||
`;
|
||||
var FRAGMENT_SRC = `
|
||||
precision mediump float;
|
||||
varying vec2 v_tex;
|
||||
uniform sampler2D u_texY;
|
||||
uniform sampler2D u_texCb;
|
||||
uniform sampler2D u_texCr;
|
||||
void main() {
|
||||
float y = texture2D(u_texY, v_tex).r;
|
||||
float cb = texture2D(u_texCb, v_tex).r - 0.5;
|
||||
float cr = texture2D(u_texCr, v_tex).r - 0.5;
|
||||
float r = y + 1.5748 * cr;
|
||||
float g = y - 0.1873 * cb - 0.4681 * cr;
|
||||
float b = y + 1.8556 * cb;
|
||||
gl_FragColor = vec4(clamp(r, 0.0, 1.0), clamp(g, 0.0, 1.0), clamp(b, 0.0, 1.0), 1.0);
|
||||
}
|
||||
`;
|
||||
|
||||
|
||||
export { HEVCDecoder, FrameRenderer };
|
||||
File diff suppressed because one or more lines are too long
+147
@@ -0,0 +1,147 @@
|
||||
// hevc-player-worker.js — HEVC-декод + рендер в OffscreenCanvas в отдельном потоке.
|
||||
// Главный поток только открывает WS и перекидывает сюда байты (transfer) — UI не блокируется.
|
||||
import { HEVCDecoder, FrameRenderer } from "./hevc-core.js";
|
||||
|
||||
// поймать тихие ошибки (в т.ч. из async renderFrame) и переслать в главный поток
|
||||
self.addEventListener("unhandledrejection", (e) => {
|
||||
self.postMessage({ type: "err", msg: "promise: " + String((e.reason && e.reason.message) || e.reason) });
|
||||
});
|
||||
|
||||
// ── строка кодека из hvcC (для информации) ──
|
||||
function hevcCodecFromHvcC(hvcC) {
|
||||
try { const p = hvcC[1] & 0x1f, t = ((hvcC[1] >> 5) & 1) ? "H" : "L", lv = hvcC[12] || 153; return `hvc1.${p}.6.${t}${lv}.B0`; }
|
||||
catch (e) { return "hvc1.1.6.L153.B0"; }
|
||||
}
|
||||
// ── VPS/SPS/PPS из hvcC → Annex-B ──
|
||||
function hvccParamSetsToAnnexB(hvcC) {
|
||||
if (hvcC.length < 23) return new Uint8Array(0);
|
||||
let o = 22; const numArrays = hvcC[o++]; const chunks = [];
|
||||
for (let a = 0; a < numArrays; a++) {
|
||||
o++;
|
||||
const num = (hvcC[o] << 8) | hvcC[o + 1]; o += 2;
|
||||
for (let n = 0; n < num; n++) { const len = (hvcC[o] << 8) | hvcC[o + 1]; o += 2; chunks.push(hvcC.subarray(o, o + len)); o += len; }
|
||||
}
|
||||
let total = 0; for (const c of chunks) total += 4 + c.length;
|
||||
const out = new Uint8Array(total); let p = 0;
|
||||
for (const c of chunks) { out[p + 3] = 1; p += 4; out.set(c, p); p += c.length; }
|
||||
return out;
|
||||
}
|
||||
// ── length-prefixed NAL → Annex-B (размер не меняется) ──
|
||||
function toAnnexB(u8) {
|
||||
const out = new Uint8Array(u8.length); out.set(u8);
|
||||
let i = 0;
|
||||
while (i + 4 <= out.length) {
|
||||
const len = ((u8[i] << 24) | (u8[i + 1] << 16) | (u8[i + 2] << 8) | u8[i + 3]) >>> 0;
|
||||
out[i] = 0; out[i + 1] = 0; out[i + 2] = 0; out[i + 3] = 1;
|
||||
if (len === 0) break; i += 4 + len;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
// ── демуксер FLV (enhanced-RTMP) → hvcC + length-prefixed HEVC-кадры ──
|
||||
class FlvHevcDemuxer {
|
||||
constructor({ onConfig, onSample }) {
|
||||
this.onConfig = onConfig; this.onSample = onSample;
|
||||
this.buf = new Uint8Array(0); this.headerDone = false; this.gotConfig = false;
|
||||
}
|
||||
push(ab) {
|
||||
const inc = new Uint8Array(ab);
|
||||
if (this.buf.length === 0) { this.buf = inc; }
|
||||
else { const m = new Uint8Array(this.buf.length + inc.length); m.set(this.buf, 0); m.set(inc, this.buf.length); this.buf = m; }
|
||||
this._parse();
|
||||
}
|
||||
_u32(o) { return ((this.buf[o] << 24) | (this.buf[o + 1] << 16) | (this.buf[o + 2] << 8) | this.buf[o + 3]) >>> 0; }
|
||||
_parse() {
|
||||
const b = this.buf, len = b.length;
|
||||
if (!this.headerDone) {
|
||||
if (len < 9) return;
|
||||
const dataOffset = this._u32(5);
|
||||
this.buf = b.slice(dataOffset); this.headerDone = true;
|
||||
return this._parse();
|
||||
}
|
||||
let o = 0;
|
||||
while (true) {
|
||||
if (o + 4 + 11 > len) break;
|
||||
const ts = o + 4;
|
||||
const tagType = b[ts] & 0x1f;
|
||||
const dataSize = (b[ts + 1] << 16) | (b[ts + 2] << 8) | b[ts + 3];
|
||||
const bodyStart = ts + 11, bodyEnd = bodyStart + dataSize;
|
||||
if (bodyEnd > len) break;
|
||||
if (tagType === 9) {
|
||||
const tsMs = ((b[ts + 7] << 24) >>> 0) | (b[ts + 4] << 16) | (b[ts + 5] << 8) | b[ts + 6];
|
||||
this._videoTag(b.subarray(bodyStart, bodyEnd), tsMs);
|
||||
}
|
||||
o = bodyEnd;
|
||||
}
|
||||
if (o > 0) this.buf = b.slice(o);
|
||||
}
|
||||
_videoTag(body, tsMs) {
|
||||
if (body.length < 5) return;
|
||||
const b0 = body[0], isEx = (b0 & 0x80) !== 0;
|
||||
if (isEx) {
|
||||
const frameType = (b0 >> 4) & 0x07, packetType = b0 & 0x0f;
|
||||
const fourcc = String.fromCharCode(body[1], body[2], body[3], body[4]);
|
||||
if (fourcc !== "hvc1" && fourcc !== "hev1") return;
|
||||
let off = 5;
|
||||
if (packetType === 1) off += 3;
|
||||
const payload = body.subarray(off);
|
||||
if (packetType === 0) this._config(payload);
|
||||
else if (packetType === 1 || packetType === 3) this._frames(payload, frameType === 1, tsMs);
|
||||
} else {
|
||||
const codecId = b0 & 0x0f, frameType = (b0 >> 4) & 0x0f;
|
||||
if (codecId !== 12 && codecId !== 7) return;
|
||||
const pkt = body[1], payload = body.subarray(5);
|
||||
if (pkt === 0) this._config(payload);
|
||||
else if (pkt === 1) this._frames(payload, frameType === 1, tsMs);
|
||||
}
|
||||
}
|
||||
_config(hvcC) {
|
||||
if (this.gotConfig || hvcC.length < 13) return;
|
||||
this.gotConfig = true;
|
||||
this.onConfig(hvcC.slice(), hevcCodecFromHvcC(hvcC));
|
||||
}
|
||||
_frames(payload, isKey, tsMs) {
|
||||
if (!this.gotConfig || payload.length === 0) return;
|
||||
this.onSample({ data: payload.slice(), type: isKey ? "key" : "delta", timestampUs: tsMs * 1000 });
|
||||
}
|
||||
}
|
||||
|
||||
// ── состояние воркера ──
|
||||
let decoder = null, renderer = null, demux = null, canvas = null;
|
||||
let decoded = 0, statTimer = null;
|
||||
|
||||
function err(e) { self.postMessage({ type: "err", msg: String((e && e.message) || e) }); }
|
||||
|
||||
function pump() {
|
||||
let fr; try { fr = decoder.drain(); } catch (e) { return; }
|
||||
for (const f of fr) { decoded++; renderer.renderFrame(f, decoded * 40000); }
|
||||
}
|
||||
|
||||
function cleanup() {
|
||||
if (statTimer) { clearInterval(statTimer); statTimer = null; }
|
||||
try { renderer && renderer.destroy(); } catch (e) {}
|
||||
try { decoder && decoder.destroy(); } catch (e) {}
|
||||
renderer = null; decoder = null; demux = null; canvas = null; decoded = 0;
|
||||
}
|
||||
|
||||
self.onmessage = async (e) => {
|
||||
const m = e.data;
|
||||
try {
|
||||
if (m.type === "init") {
|
||||
canvas = m.canvas;
|
||||
decoder = await HEVCDecoder.create({ wasmUrl: m.wasmUrl, wasmBinaryUrl: m.wasmBinaryUrl });
|
||||
renderer = new FrameRenderer();
|
||||
renderer.initCanvas(canvas);
|
||||
demux = new FlvHevcDemuxer({
|
||||
onConfig: (hvcC) => { const ps = hvccParamSetsToAnnexB(hvcC); if (ps.length) { try { decoder.feed(ps); pump(); } catch (er) { err(er); } } },
|
||||
onSample: ({ data }) => { try { decoder.feed(toAnnexB(data)); pump(); } catch (er) { err(er); } },
|
||||
});
|
||||
if (statTimer) clearInterval(statTimer);
|
||||
statTimer = setInterval(() => { self.postMessage({ type: "stat", fps: decoded }); decoded = 0; }, 1000);
|
||||
self.postMessage({ type: "ready" });
|
||||
} else if (m.type === "data") {
|
||||
if (demux) demux.push(m.buf);
|
||||
} else if (m.type === "stop") {
|
||||
cleanup();
|
||||
}
|
||||
} catch (er) { err(er); }
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
// Заглушка: dist/index.js статически импортит "mp4box" ради FMP4Demuxer,
|
||||
// который мы НЕ используем (у нас свой FLV-демукс). Import-map подменяет
|
||||
// "mp4box" на этот файл, чтобы модуль index.js загрузился без bare-specifier.
|
||||
export function createFile() {
|
||||
throw new Error("mp4box stub: FMP4Demuxer не используется в этом проекте");
|
||||
}
|
||||
export default { createFile };
|
||||
Reference in New Issue
Block a user