mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-10-03 22:03:49 +02:00
Add @mediabunny/dts extension package
This commit is contained in:
@@ -0,0 +1,321 @@
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include <emscripten.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "libavcodec/avcodec.h"
|
||||
#include "libavutil/opt.h"
|
||||
#include "libavutil/channel_layout.h"
|
||||
|
||||
typedef struct {
|
||||
AVCodecContext *codec_ctx;
|
||||
AVPacket *packet;
|
||||
AVFrame *frame;
|
||||
} DecoderContext;
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
DecoderContext *init_decoder() {
|
||||
const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_DTS);
|
||||
if (!codec) return NULL;
|
||||
|
||||
AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
|
||||
if (!codec_ctx) return NULL;
|
||||
|
||||
if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
|
||||
avcodec_free_context(&codec_ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
AVPacket *packet = av_packet_alloc();
|
||||
if (!packet) {
|
||||
avcodec_free_context(&codec_ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
AVFrame *frame = av_frame_alloc();
|
||||
if (!frame) {
|
||||
av_packet_free(&packet);
|
||||
avcodec_free_context(&codec_ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DecoderContext *ctx = malloc(sizeof(DecoderContext));
|
||||
if (!ctx) {
|
||||
av_frame_free(&frame);
|
||||
av_packet_free(&packet);
|
||||
avcodec_free_context(&codec_ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ctx->codec_ctx = codec_ctx;
|
||||
ctx->packet = packet;
|
||||
ctx->frame = frame;
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
uint8_t *configure_decode_packet(DecoderContext *ctx, int size) {
|
||||
if (av_new_packet(ctx->packet, size) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ctx->packet->data;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int decode_packet(DecoderContext *ctx, int64_t pts) {
|
||||
ctx->packet->pts = pts;
|
||||
int ret = avcodec_send_packet(ctx->codec_ctx, ctx->packet);
|
||||
av_packet_unref(ctx->packet);
|
||||
if (ret < 0) return ret;
|
||||
|
||||
ret = avcodec_receive_frame(ctx->codec_ctx, ctx->frame);
|
||||
if (ret < 0) return ret;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int get_decoded_format(DecoderContext *ctx) {
|
||||
return ctx->frame->format;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
uint8_t *get_decoded_plane_ptr(DecoderContext *ctx, int plane) {
|
||||
return ctx->frame->data[plane];
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int get_decoded_channels(DecoderContext *ctx) {
|
||||
return ctx->frame->ch_layout.nb_channels;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int get_decoded_sample_rate(DecoderContext *ctx) {
|
||||
return ctx->frame->sample_rate;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int get_decoded_sample_count(DecoderContext *ctx) {
|
||||
return ctx->frame->nb_samples;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int64_t get_decoded_pts(DecoderContext *ctx) {
|
||||
return ctx->frame->pts;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
void flush_decoder(DecoderContext *ctx) {
|
||||
avcodec_send_packet(ctx->codec_ctx, NULL);
|
||||
while (avcodec_receive_frame(ctx->codec_ctx, ctx->frame) == 0) {}
|
||||
avcodec_flush_buffers(ctx->codec_ctx);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
void close_decoder(DecoderContext *ctx) {
|
||||
av_frame_free(&ctx->frame);
|
||||
av_packet_free(&ctx->packet);
|
||||
avcodec_free_context(&ctx->codec_ctx);
|
||||
free(ctx);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
AVCodecContext *codec_ctx;
|
||||
AVPacket *packet;
|
||||
AVFrame *frame;
|
||||
float *input_buffer;
|
||||
int input_buffer_size;
|
||||
int64_t encoded_pts;
|
||||
int encoded_duration;
|
||||
} EncoderContext;
|
||||
|
||||
/**
|
||||
* DTS insists on the side-based surround layouts and rejects the back-based ones that av_channel_layout_default hands
|
||||
* out for 4, 5 and 6 channels.
|
||||
*/
|
||||
static int set_dts_channel_layout(AVChannelLayout *layout, int channels) {
|
||||
switch (channels) {
|
||||
case 1: {
|
||||
AVChannelLayout mono = AV_CHANNEL_LAYOUT_MONO;
|
||||
return av_channel_layout_copy(layout, &mono);
|
||||
}
|
||||
case 2: {
|
||||
AVChannelLayout stereo = AV_CHANNEL_LAYOUT_STEREO;
|
||||
return av_channel_layout_copy(layout, &stereo);
|
||||
}
|
||||
case 4: {
|
||||
AVChannelLayout quad_side = AV_CHANNEL_LAYOUT_2_2;
|
||||
return av_channel_layout_copy(layout, &quad_side);
|
||||
}
|
||||
case 5: {
|
||||
AVChannelLayout five_zero = AV_CHANNEL_LAYOUT_5POINT0;
|
||||
return av_channel_layout_copy(layout, &five_zero);
|
||||
}
|
||||
case 6: {
|
||||
AVChannelLayout five_one = AV_CHANNEL_LAYOUT_5POINT1;
|
||||
return av_channel_layout_copy(layout, &five_one);
|
||||
}
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
EncoderContext *init_encoder(int channels, int sample_rate, int bitrate) {
|
||||
const AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_DTS);
|
||||
if (!codec) return NULL;
|
||||
|
||||
AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
|
||||
if (!codec_ctx) return NULL;
|
||||
|
||||
codec_ctx->sample_fmt = AV_SAMPLE_FMT_S32;
|
||||
codec_ctx->sample_rate = sample_rate;
|
||||
codec_ctx->bit_rate = bitrate;
|
||||
codec_ctx->time_base = (AVRational){1, sample_rate};
|
||||
|
||||
// FFmpeg marks its DTS encoder experimental, so it refuses to open at the default compliance level
|
||||
codec_ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
|
||||
|
||||
if (set_dts_channel_layout(&codec_ctx->ch_layout, channels) < 0) {
|
||||
avcodec_free_context(&codec_ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
|
||||
avcodec_free_context(&codec_ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
AVPacket *packet = av_packet_alloc();
|
||||
if (!packet) {
|
||||
avcodec_free_context(&codec_ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
AVFrame *frame = av_frame_alloc();
|
||||
if (!frame) {
|
||||
av_packet_free(&packet);
|
||||
avcodec_free_context(&codec_ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// The frame has a fixed format, so let's create it now:
|
||||
frame->format = AV_SAMPLE_FMT_S32;
|
||||
frame->sample_rate = sample_rate;
|
||||
frame->nb_samples = codec_ctx->frame_size;
|
||||
av_channel_layout_copy(&frame->ch_layout, &codec_ctx->ch_layout);
|
||||
|
||||
if (av_frame_get_buffer(frame, 0) < 0) {
|
||||
av_frame_free(&frame);
|
||||
av_packet_free(&packet);
|
||||
avcodec_free_context(&codec_ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
EncoderContext *ctx = malloc(sizeof(EncoderContext));
|
||||
if (!ctx) {
|
||||
av_frame_free(&frame);
|
||||
av_packet_free(&packet);
|
||||
avcodec_free_context(&codec_ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ctx->codec_ctx = codec_ctx;
|
||||
ctx->packet = packet;
|
||||
ctx->frame = frame;
|
||||
ctx->input_buffer = NULL;
|
||||
ctx->input_buffer_size = 0;
|
||||
ctx->encoded_pts = 0;
|
||||
ctx->encoded_duration = 0;
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int get_encoder_frame_size(EncoderContext *ctx) {
|
||||
return ctx->codec_ctx->frame_size;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
float *get_encode_input_ptr(EncoderContext *ctx, int size) {
|
||||
if (ctx->input_buffer_size < size) {
|
||||
free(ctx->input_buffer);
|
||||
ctx->input_buffer = malloc(size);
|
||||
if (!ctx->input_buffer) {
|
||||
ctx->input_buffer_size = 0;
|
||||
return NULL;
|
||||
}
|
||||
ctx->input_buffer_size = size;
|
||||
}
|
||||
return ctx->input_buffer;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int encode_frame(EncoderContext *ctx, int64_t pts) {
|
||||
int channels = ctx->codec_ctx->ch_layout.nb_channels;
|
||||
int frame_size = ctx->frame->nb_samples;
|
||||
|
||||
ctx->frame->pts = pts;
|
||||
|
||||
// DTS encodes from s32, which is a packed format, so the samples stay interleaved and all land in data[0]
|
||||
float *input = ctx->input_buffer;
|
||||
int32_t *output = (int32_t *)ctx->frame->data[0];
|
||||
for (int i = 0; i < frame_size * channels; i++) {
|
||||
float sample = input[i];
|
||||
if (sample > 1.0f) sample = 1.0f;
|
||||
if (sample < -1.0f) sample = -1.0f;
|
||||
output[i] = (int32_t)(sample * 2147483647.0f);
|
||||
}
|
||||
|
||||
int ret = avcodec_send_frame(ctx->codec_ctx, ctx->frame);
|
||||
if (ret < 0) return ret;
|
||||
|
||||
ret = avcodec_receive_packet(ctx->codec_ctx, ctx->packet);
|
||||
if (ret < 0) return ret;
|
||||
|
||||
ctx->encoded_pts = ctx->packet->pts;
|
||||
ctx->encoded_duration = ctx->packet->duration;
|
||||
|
||||
return ctx->packet->size;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
void flush_encoder(EncoderContext *ctx) {
|
||||
avcodec_send_frame(ctx->codec_ctx, NULL);
|
||||
while (avcodec_receive_packet(ctx->codec_ctx, ctx->packet) == 0) {
|
||||
av_packet_unref(ctx->packet);
|
||||
}
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
uint8_t *get_encoded_data(EncoderContext *ctx) {
|
||||
return ctx->packet->data;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int64_t get_encoded_pts(EncoderContext *ctx) {
|
||||
return ctx->encoded_pts;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int get_encoded_duration(EncoderContext *ctx) {
|
||||
return ctx->encoded_duration;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
void close_encoder(EncoderContext *ctx) {
|
||||
free(ctx->input_buffer);
|
||||
av_frame_free(&ctx->frame);
|
||||
av_packet_free(&ctx->packet);
|
||||
avcodec_free_context(&ctx->codec_ctx);
|
||||
free(ctx);
|
||||
}
|
||||
@@ -0,0 +1,306 @@
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import createModule from '../build/dts';
|
||||
import type { WorkerCommand, WorkerResponse, WorkerResponseData } from './shared';
|
||||
|
||||
type ExtendedEmscriptenModule = EmscriptenModule & {
|
||||
cwrap: typeof cwrap;
|
||||
};
|
||||
|
||||
let module: ExtendedEmscriptenModule;
|
||||
let modulePromise: Promise<ExtendedEmscriptenModule> | null = null;
|
||||
|
||||
let initDecoderFn: () => number;
|
||||
let configureDecodePacket: (ctx: number, size: number) => number;
|
||||
let decodePacket: (ctx: number, pts: bigint) => number;
|
||||
let getDecodedFormat: (ctx: number) => number;
|
||||
let getDecodedPlanePtr: (ctx: number, plane: number) => number;
|
||||
let getDecodedChannels: (ctx: number) => number;
|
||||
let getDecodedSampleRate: (ctx: number) => number;
|
||||
let getDecodedSampleCount: (ctx: number) => number;
|
||||
let getDecodedPts: (ctx: number) => bigint;
|
||||
let flushDecoderFn: (ctx: number) => void;
|
||||
let closeDecoderFn: (ctx: number) => void;
|
||||
|
||||
let initEncoderFn: (channels: number, sampleRate: number, bitrate: number) => number;
|
||||
let getEncoderFrameSize: (ctx: number) => number;
|
||||
let getEncodeInputPtr: (ctx: number, size: number) => number;
|
||||
let encodeFrameFn: (ctx: number, pts: bigint) => number;
|
||||
let flushEncoderFn: (ctx: number) => void;
|
||||
let getEncodedData: (ctx: number) => number;
|
||||
let getEncodedPts: (ctx: number) => bigint;
|
||||
let getEncodedDuration: (ctx: number) => number;
|
||||
let closeEncoderFn: (ctx: number) => void;
|
||||
|
||||
const ensureModule = async () => {
|
||||
if (!module) {
|
||||
if (modulePromise) {
|
||||
// If we don't do this we can have a race condition
|
||||
return modulePromise;
|
||||
}
|
||||
|
||||
modulePromise = createModule() as Promise<ExtendedEmscriptenModule>;
|
||||
module = await modulePromise;
|
||||
modulePromise = null;
|
||||
|
||||
initDecoderFn = module.cwrap('init_decoder', 'number', []);
|
||||
configureDecodePacket = module.cwrap('configure_decode_packet', 'number', ['number', 'number']);
|
||||
decodePacket = module.cwrap('decode_packet', 'number', ['number', 'number']) as unknown as typeof decodePacket;
|
||||
getDecodedFormat = module.cwrap('get_decoded_format', 'number', ['number']);
|
||||
getDecodedPlanePtr = module.cwrap('get_decoded_plane_ptr', 'number', ['number', 'number']);
|
||||
getDecodedChannels = module.cwrap('get_decoded_channels', 'number', ['number']);
|
||||
getDecodedSampleRate = module.cwrap('get_decoded_sample_rate', 'number', ['number']);
|
||||
getDecodedSampleCount = module.cwrap('get_decoded_sample_count', 'number', ['number']);
|
||||
getDecodedPts = module.cwrap('get_decoded_pts', 'number', ['number']) as unknown as typeof getDecodedPts;
|
||||
flushDecoderFn = module.cwrap('flush_decoder', null, ['number']);
|
||||
closeDecoderFn = module.cwrap('close_decoder', null, ['number']);
|
||||
|
||||
initEncoderFn = module.cwrap('init_encoder', 'number', ['number', 'number', 'number']);
|
||||
getEncoderFrameSize = module.cwrap('get_encoder_frame_size', 'number', ['number']);
|
||||
getEncodeInputPtr = module.cwrap('get_encode_input_ptr', 'number', ['number', 'number']);
|
||||
encodeFrameFn = module.cwrap('encode_frame', 'number', ['number', 'number']) as unknown as typeof encodeFrameFn;
|
||||
flushEncoderFn = module.cwrap('flush_encoder', null, ['number']);
|
||||
getEncodedData = module.cwrap('get_encoded_data', 'number', ['number']);
|
||||
getEncodedPts = module.cwrap('get_encoded_pts', 'number', ['number']) as unknown as typeof getEncodedPts;
|
||||
getEncodedDuration = module.cwrap('get_encoded_duration', 'number', ['number']);
|
||||
closeEncoderFn = module.cwrap('close_encoder', null, ['number']);
|
||||
}
|
||||
};
|
||||
|
||||
const initDecoder = async () => {
|
||||
await ensureModule();
|
||||
|
||||
const ctx = initDecoderFn();
|
||||
if (ctx === 0) {
|
||||
throw new Error('Failed to initialize DTS decoder.');
|
||||
}
|
||||
|
||||
return { ctx, frameSize: 0 };
|
||||
};
|
||||
|
||||
// Keys are AVSampleFormat enum values
|
||||
const AV_FORMAT_MAP: Record<number, { format: AudioSampleFormat; bytesPerSample: number; planar: boolean }> = {
|
||||
0: { format: 'u8', bytesPerSample: 1, planar: false },
|
||||
1: { format: 's16', bytesPerSample: 2, planar: false },
|
||||
2: { format: 's32', bytesPerSample: 4, planar: false },
|
||||
3: { format: 'f32', bytesPerSample: 4, planar: false },
|
||||
5: { format: 'u8-planar', bytesPerSample: 1, planar: true },
|
||||
6: { format: 's16-planar', bytesPerSample: 2, planar: true },
|
||||
7: { format: 's32-planar', bytesPerSample: 4, planar: true },
|
||||
8: { format: 'f32-planar', bytesPerSample: 4, planar: true },
|
||||
};
|
||||
|
||||
const decode = (ctx: number, encodedData: ArrayBuffer, timestamp: number) => {
|
||||
const bytes = new Uint8Array(encodedData);
|
||||
|
||||
const dataPtr = configureDecodePacket(ctx, bytes.length);
|
||||
if (dataPtr === 0) {
|
||||
throw new Error('Failed to configure decode packet.');
|
||||
}
|
||||
|
||||
module.HEAPU8.set(bytes, dataPtr);
|
||||
|
||||
const ret = decodePacket(ctx, BigInt(timestamp));
|
||||
if (ret < 0) {
|
||||
throw new Error(`Decode failed with error code ${ret}.`);
|
||||
}
|
||||
|
||||
const avFormat = getDecodedFormat(ctx);
|
||||
const info = AV_FORMAT_MAP[avFormat];
|
||||
if (!info) {
|
||||
throw new Error(`Unsupported AVSampleFormat: ${avFormat}`);
|
||||
}
|
||||
|
||||
const channels = getDecodedChannels(ctx);
|
||||
const sampleRate = getDecodedSampleRate(ctx);
|
||||
const sampleCount = getDecodedSampleCount(ctx);
|
||||
const pts = Number(getDecodedPts(ctx));
|
||||
|
||||
let pcmData: ArrayBuffer;
|
||||
if (info.planar) {
|
||||
const planeSize = sampleCount * info.bytesPerSample;
|
||||
const buffer = new Uint8Array(planeSize * channels);
|
||||
|
||||
for (let ch = 0; ch < channels; ch++) {
|
||||
const ptr = getDecodedPlanePtr(ctx, ch);
|
||||
buffer.set(module.HEAPU8.subarray(ptr, ptr + planeSize), ch * planeSize);
|
||||
}
|
||||
|
||||
pcmData = buffer.buffer;
|
||||
} else {
|
||||
const totalSize = sampleCount * channels * info.bytesPerSample;
|
||||
const ptr = getDecodedPlanePtr(ctx, 0);
|
||||
pcmData = module.HEAPU8.slice(ptr, ptr + totalSize).buffer;
|
||||
}
|
||||
|
||||
return { pcmData, format: info.format, channels, sampleRate, sampleCount, pts };
|
||||
};
|
||||
|
||||
const initEncoder = async (
|
||||
numberOfChannels: number,
|
||||
sampleRate: number,
|
||||
bitrate: number,
|
||||
) => {
|
||||
await ensureModule();
|
||||
|
||||
const ctx = initEncoderFn(numberOfChannels, sampleRate, bitrate);
|
||||
if (ctx === 0) {
|
||||
throw new Error('Failed to initialize DTS encoder.');
|
||||
}
|
||||
|
||||
return { ctx, frameSize: getEncoderFrameSize(ctx) };
|
||||
};
|
||||
|
||||
const encode = (ctx: number, audioData: ArrayBuffer, timestamp: number) => {
|
||||
const audioBytes = new Uint8Array(audioData);
|
||||
|
||||
const inputPtr = getEncodeInputPtr(ctx, audioBytes.length);
|
||||
if (inputPtr === 0) {
|
||||
throw new Error('Failed to allocate encoder input buffer.');
|
||||
}
|
||||
module.HEAPU8.set(audioBytes, inputPtr);
|
||||
|
||||
const bytesWritten = encodeFrameFn(ctx, BigInt(timestamp));
|
||||
if (bytesWritten < 0) {
|
||||
throw new Error(`Encode failed with error code ${bytesWritten}.`);
|
||||
}
|
||||
|
||||
const ptr = getEncodedData(ctx);
|
||||
const encodedData = module.HEAPU8.slice(ptr, ptr + bytesWritten).buffer;
|
||||
const pts = Number(getEncodedPts(ctx));
|
||||
const duration = getEncodedDuration(ctx);
|
||||
|
||||
return { encodedData, pts, duration };
|
||||
};
|
||||
|
||||
const flushEncoder = (ctx: number) => {
|
||||
flushEncoderFn(ctx);
|
||||
};
|
||||
|
||||
const onMessage = (data: { id: number; command: WorkerCommand }) => {
|
||||
const { id, command } = data;
|
||||
|
||||
const handleCommand = async (): Promise<void> => {
|
||||
try {
|
||||
let result: WorkerResponseData;
|
||||
const transferables: Transferable[] = [];
|
||||
|
||||
switch (command.type) {
|
||||
case 'init-decoder': {
|
||||
const { ctx, frameSize } = await initDecoder();
|
||||
result = { type: command.type, ctx, frameSize };
|
||||
}; break;
|
||||
|
||||
case 'decode': {
|
||||
const decoded = decode(command.data.ctx, command.data.encodedData, command.data.timestamp);
|
||||
result = {
|
||||
type: command.type,
|
||||
pcmData: decoded.pcmData,
|
||||
format: decoded.format,
|
||||
channels: decoded.channels,
|
||||
sampleRate: decoded.sampleRate,
|
||||
sampleCount: decoded.sampleCount,
|
||||
pts: decoded.pts,
|
||||
};
|
||||
transferables.push(decoded.pcmData);
|
||||
}; break;
|
||||
|
||||
case 'flush-decoder': {
|
||||
flushDecoderFn(command.data.ctx);
|
||||
result = { type: command.type };
|
||||
}; break;
|
||||
|
||||
case 'close-decoder': {
|
||||
closeDecoderFn(command.data.ctx);
|
||||
result = { type: command.type };
|
||||
}; break;
|
||||
|
||||
case 'init-encoder': {
|
||||
const { ctx, frameSize } = await initEncoder(
|
||||
command.data.numberOfChannels,
|
||||
command.data.sampleRate,
|
||||
command.data.bitrate,
|
||||
);
|
||||
result = { type: command.type, ctx, frameSize };
|
||||
}; break;
|
||||
|
||||
case 'encode': {
|
||||
const encoded = encode(
|
||||
command.data.ctx,
|
||||
command.data.audioData,
|
||||
command.data.timestamp,
|
||||
);
|
||||
result = {
|
||||
type: command.type,
|
||||
encodedData: encoded.encodedData,
|
||||
pts: encoded.pts,
|
||||
duration: encoded.duration,
|
||||
};
|
||||
transferables.push(encoded.encodedData);
|
||||
}; break;
|
||||
|
||||
case 'flush-encoder': {
|
||||
flushEncoder(command.data.ctx);
|
||||
result = { type: command.type };
|
||||
}; break;
|
||||
|
||||
case 'close-encoder': {
|
||||
closeEncoderFn(command.data.ctx);
|
||||
result = { type: command.type };
|
||||
}; break;
|
||||
}
|
||||
|
||||
const response: WorkerResponse = {
|
||||
id,
|
||||
success: true,
|
||||
data: result,
|
||||
};
|
||||
sendMessage(response, transferables);
|
||||
} catch (error: unknown) {
|
||||
const response: WorkerResponse = {
|
||||
id,
|
||||
success: false,
|
||||
error,
|
||||
};
|
||||
sendMessage(response);
|
||||
}
|
||||
};
|
||||
|
||||
void handleCommand();
|
||||
};
|
||||
|
||||
const sendMessage = (data: unknown, transferables?: Transferable[]) => {
|
||||
if (parentPort) {
|
||||
parentPort.postMessage(data, transferables ?? []);
|
||||
} else {
|
||||
self.postMessage(data, { transfer: transferables ?? [] });
|
||||
}
|
||||
};
|
||||
|
||||
let parentPort: {
|
||||
postMessage: (data: unknown, transferables?: Transferable[]) => void;
|
||||
on: (event: string, listener: (data: never) => void) => void;
|
||||
} | null = null;
|
||||
|
||||
if (typeof self === 'undefined') {
|
||||
const workerModule = 'worker_threads';
|
||||
// eslint-disable-next-line @stylistic/max-len
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-member-access
|
||||
parentPort = require(workerModule).parentPort;
|
||||
}
|
||||
|
||||
if (parentPort) {
|
||||
parentPort.on('message', onMessage);
|
||||
} else {
|
||||
self.addEventListener('message', event => onMessage(event.data as { id: number; command: WorkerCommand }));
|
||||
}
|
||||
|
||||
// Prevents the worker for being randomly closed by Firefox
|
||||
// https://github.com/Vanilagy/mediabunny/issues/435
|
||||
setInterval(() => {}, 1000);
|
||||
@@ -0,0 +1,80 @@
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import {
|
||||
CustomAudioDecoder,
|
||||
AudioCodec,
|
||||
AudioSample,
|
||||
EncodedPacket,
|
||||
registerDecoder,
|
||||
} from 'mediabunny';
|
||||
import { sendCommand, refWorker, unrefWorker } from './worker-client';
|
||||
|
||||
class DtsDecoder extends CustomAudioDecoder {
|
||||
private ctx = 0;
|
||||
|
||||
static override supports(codec: AudioCodec): boolean {
|
||||
return codec === 'dts';
|
||||
}
|
||||
|
||||
async init() {
|
||||
await refWorker();
|
||||
|
||||
const result = await sendCommand({
|
||||
type: 'init-decoder',
|
||||
data: {},
|
||||
});
|
||||
this.ctx = result.ctx;
|
||||
}
|
||||
|
||||
async decode(packet: EncodedPacket) {
|
||||
const encodedData = packet.data.slice().buffer;
|
||||
const timestamp = Math.round(packet.timestamp * this.config.sampleRate);
|
||||
|
||||
const result = await sendCommand({
|
||||
type: 'decode',
|
||||
data: { ctx: this.ctx, encodedData, timestamp },
|
||||
}, [encodedData]);
|
||||
|
||||
const sample = new AudioSample({
|
||||
data: result.pcmData,
|
||||
format: result.format,
|
||||
numberOfChannels: result.channels,
|
||||
sampleRate: result.sampleRate,
|
||||
timestamp: result.pts / result.sampleRate,
|
||||
});
|
||||
this.onSample(sample);
|
||||
}
|
||||
|
||||
async flush() {
|
||||
await sendCommand({ type: 'flush-decoder', data: { ctx: this.ctx } });
|
||||
}
|
||||
|
||||
async close() {
|
||||
void sendCommand({ type: 'close-decoder', data: { ctx: this.ctx } });
|
||||
await unrefWorker();
|
||||
}
|
||||
}
|
||||
|
||||
let registered = false;
|
||||
|
||||
/**
|
||||
* Registers a DTS audio decoder, which Mediabunny will then use automatically when applicable. Make sure to call this
|
||||
* function before starting any decoding task.
|
||||
*
|
||||
* @group \@mediabunny/dts
|
||||
* @public
|
||||
*/
|
||||
export const registerDtsDecoder = () => {
|
||||
if (registered) {
|
||||
return;
|
||||
}
|
||||
registered = true;
|
||||
|
||||
registerDecoder(DtsDecoder);
|
||||
};
|
||||
@@ -0,0 +1,197 @@
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import {
|
||||
CustomAudioEncoder,
|
||||
AudioCodec,
|
||||
AudioSample,
|
||||
EncodedPacket,
|
||||
registerEncoder,
|
||||
} from 'mediabunny';
|
||||
import { sendCommand, refWorker, unrefWorker } from './worker-client';
|
||||
import { assert } from './shared';
|
||||
import { DTS_CHANNEL_COUNTS, DTS_SAMPLE_RATES, dtsBitrateFits } from '../../../shared/dts-misc';
|
||||
|
||||
class DtsEncoder extends CustomAudioEncoder {
|
||||
private ctx = 0;
|
||||
private encoderFrameSize = 0;
|
||||
private sampleRate = 0;
|
||||
private numberOfChannels = 0;
|
||||
private chunkMetadata: EncodedAudioChunkMetadata = {};
|
||||
|
||||
// Accumulate interleaved f32 samples until we have a full frame
|
||||
private pendingBuffer = new Float32Array(2 ** 16);
|
||||
private pendingFrames = 0;
|
||||
private nextSampleTimestampInSamples: number | null = null;
|
||||
private nextPacketTimestampInSamples: number | null = null;
|
||||
|
||||
static override supports(codec: AudioCodec, config: AudioEncoderConfig): boolean {
|
||||
return codec === 'dts'
|
||||
&& DTS_CHANNEL_COUNTS.includes(config.numberOfChannels)
|
||||
&& DTS_SAMPLE_RATES.includes(config.sampleRate)
|
||||
&& config.bitrate !== undefined
|
||||
&& dtsBitrateFits(config.bitrate, config.sampleRate, config.numberOfChannels);
|
||||
}
|
||||
|
||||
async init() {
|
||||
await refWorker();
|
||||
|
||||
assert(this.config.bitrate !== undefined);
|
||||
this.sampleRate = this.config.sampleRate;
|
||||
this.numberOfChannels = this.config.numberOfChannels;
|
||||
|
||||
const result = await sendCommand({
|
||||
type: 'init-encoder',
|
||||
data: {
|
||||
numberOfChannels: this.config.numberOfChannels,
|
||||
sampleRate: this.config.sampleRate,
|
||||
bitrate: this.config.bitrate,
|
||||
},
|
||||
});
|
||||
|
||||
this.ctx = result.ctx;
|
||||
this.encoderFrameSize = result.frameSize;
|
||||
|
||||
this.resetInternalState();
|
||||
}
|
||||
|
||||
private resetInternalState() {
|
||||
this.pendingFrames = 0;
|
||||
this.nextSampleTimestampInSamples = null;
|
||||
this.nextPacketTimestampInSamples = null;
|
||||
|
||||
this.chunkMetadata = {
|
||||
decoderConfig: {
|
||||
codec: 'dtsc',
|
||||
numberOfChannels: this.config.numberOfChannels,
|
||||
sampleRate: this.config.sampleRate,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async encode(audioSample: AudioSample) {
|
||||
if (this.nextSampleTimestampInSamples === null) {
|
||||
this.nextSampleTimestampInSamples = Math.round(audioSample.timestamp * this.sampleRate);
|
||||
this.nextPacketTimestampInSamples = this.nextSampleTimestampInSamples;
|
||||
}
|
||||
|
||||
const channels = this.numberOfChannels;
|
||||
const incomingFrames = audioSample.numberOfFrames;
|
||||
|
||||
// Extract interleaved f32 data
|
||||
const totalBytes = audioSample.allocationSize({ format: 'f32', planeIndex: 0 });
|
||||
const audioBytes = new Uint8Array(totalBytes);
|
||||
audioSample.copyTo(audioBytes, { format: 'f32', planeIndex: 0 });
|
||||
const incomingData = new Float32Array(audioBytes.buffer);
|
||||
|
||||
const requiredSamples = (this.pendingFrames + incomingFrames) * channels;
|
||||
if (requiredSamples > this.pendingBuffer.length) {
|
||||
let newSize = this.pendingBuffer.length;
|
||||
while (newSize < requiredSamples) {
|
||||
newSize *= 2;
|
||||
}
|
||||
const newBuffer = new Float32Array(newSize);
|
||||
newBuffer.set(this.pendingBuffer.subarray(0, this.pendingFrames * channels));
|
||||
this.pendingBuffer = newBuffer;
|
||||
}
|
||||
this.pendingBuffer.set(incomingData, this.pendingFrames * channels);
|
||||
this.pendingFrames += incomingFrames;
|
||||
|
||||
while (this.pendingFrames >= this.encoderFrameSize) {
|
||||
await this.encodeOneFrame();
|
||||
}
|
||||
}
|
||||
|
||||
async flush() {
|
||||
// Pad remaining samples with silence to fill a full frame
|
||||
if (this.pendingFrames > 0) {
|
||||
const channels = this.numberOfChannels;
|
||||
const frameSize = this.encoderFrameSize;
|
||||
const usedSamples = this.pendingFrames * channels;
|
||||
const frameSamples = frameSize * channels;
|
||||
|
||||
this.pendingBuffer.fill(0, usedSamples, frameSamples);
|
||||
this.pendingFrames = frameSize;
|
||||
|
||||
await this.encodeOneFrame();
|
||||
}
|
||||
|
||||
await sendCommand({ type: 'flush-encoder', data: { ctx: this.ctx } });
|
||||
|
||||
this.resetInternalState();
|
||||
}
|
||||
|
||||
close() {
|
||||
void sendCommand({ type: 'close-encoder', data: { ctx: this.ctx } });
|
||||
void unrefWorker();
|
||||
}
|
||||
|
||||
private async encodeOneFrame() {
|
||||
assert(this.nextSampleTimestampInSamples !== null);
|
||||
assert(this.nextPacketTimestampInSamples !== null);
|
||||
|
||||
const channels = this.numberOfChannels;
|
||||
const frameSize = this.encoderFrameSize;
|
||||
const frameSamples = frameSize * channels;
|
||||
|
||||
const frameData = this.pendingBuffer.slice(0, frameSamples);
|
||||
|
||||
// Shift remaining using copyWithin
|
||||
this.pendingFrames -= frameSize;
|
||||
if (this.pendingFrames > 0) {
|
||||
this.pendingBuffer.copyWithin(0, frameSamples, frameSamples + this.pendingFrames * channels);
|
||||
}
|
||||
|
||||
const audioData = frameData.buffer;
|
||||
const result = await sendCommand({
|
||||
type: 'encode',
|
||||
data: {
|
||||
ctx: this.ctx,
|
||||
audioData,
|
||||
timestamp: this.nextSampleTimestampInSamples,
|
||||
},
|
||||
}, [audioData]);
|
||||
|
||||
this.nextSampleTimestampInSamples += frameSize;
|
||||
|
||||
// We always get exactly one packet because we encode the correct frame size
|
||||
const packet = new EncodedPacket(
|
||||
new Uint8Array(result.encodedData),
|
||||
'key',
|
||||
this.nextPacketTimestampInSamples / this.sampleRate,
|
||||
result.duration / this.sampleRate,
|
||||
);
|
||||
|
||||
this.nextPacketTimestampInSamples += result.duration;
|
||||
|
||||
this.onPacket(
|
||||
packet,
|
||||
this.chunkMetadata,
|
||||
);
|
||||
|
||||
this.chunkMetadata = {};
|
||||
}
|
||||
}
|
||||
|
||||
let registered = false;
|
||||
|
||||
/**
|
||||
* Registers a DTS audio encoder, which Mediabunny will then use automatically when applicable. Make sure to call this
|
||||
* function before starting any encoding task.
|
||||
*
|
||||
* @group \@mediabunny/dts
|
||||
* @public
|
||||
*/
|
||||
export const registerDtsEncoder = () => {
|
||||
if (registered) {
|
||||
return;
|
||||
}
|
||||
registered = true;
|
||||
|
||||
registerEncoder(DtsEncoder);
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { Logging } from 'mediabunny';
|
||||
|
||||
const DTS_LOADED_SYMBOL = Symbol.for('@mediabunny/dts loaded');
|
||||
if ((globalThis as Record<symbol, unknown>)[DTS_LOADED_SYMBOL]) {
|
||||
Logging._error(
|
||||
'[WARNING]\n@mediabunny/dts was loaded twice.'
|
||||
+ ' This will likely cause the encoder/decoder not to work correctly.'
|
||||
+ ' Check if multiple dependencies are importing different versions of @mediabunny/dts,'
|
||||
+ ' or if something is being bundled incorrectly.',
|
||||
);
|
||||
}
|
||||
(globalThis as Record<symbol, unknown>)[DTS_LOADED_SYMBOL] = true;
|
||||
|
||||
export { registerDtsDecoder } from './decoder';
|
||||
export { registerDtsEncoder } from './encoder';
|
||||
@@ -0,0 +1,100 @@
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
export type WorkerCommand = {
|
||||
type: 'init-decoder';
|
||||
data: Record<string, never>;
|
||||
} | {
|
||||
type: 'decode';
|
||||
data: {
|
||||
ctx: number;
|
||||
encodedData: ArrayBuffer;
|
||||
timestamp: number;
|
||||
};
|
||||
} | {
|
||||
type: 'flush-decoder';
|
||||
data: {
|
||||
ctx: number;
|
||||
};
|
||||
} | {
|
||||
type: 'close-decoder';
|
||||
data: {
|
||||
ctx: number;
|
||||
};
|
||||
} | {
|
||||
type: 'init-encoder';
|
||||
data: {
|
||||
numberOfChannels: number;
|
||||
sampleRate: number;
|
||||
bitrate: number;
|
||||
};
|
||||
} | {
|
||||
type: 'encode';
|
||||
data: {
|
||||
ctx: number;
|
||||
audioData: ArrayBuffer;
|
||||
timestamp: number;
|
||||
};
|
||||
} | {
|
||||
type: 'flush-encoder';
|
||||
data: {
|
||||
ctx: number;
|
||||
};
|
||||
} | {
|
||||
type: 'close-encoder';
|
||||
data: {
|
||||
ctx: number;
|
||||
};
|
||||
};
|
||||
|
||||
export type WorkerResponseData = {
|
||||
type: 'init-decoder';
|
||||
ctx: number;
|
||||
frameSize: number;
|
||||
} | {
|
||||
type: 'decode';
|
||||
pcmData: ArrayBuffer;
|
||||
format: AudioSampleFormat;
|
||||
channels: number;
|
||||
sampleRate: number;
|
||||
sampleCount: number;
|
||||
pts: number;
|
||||
} | {
|
||||
type: 'flush-decoder';
|
||||
} | {
|
||||
type: 'close-decoder';
|
||||
} | {
|
||||
type: 'init-encoder';
|
||||
ctx: number;
|
||||
frameSize: number;
|
||||
} | {
|
||||
type: 'encode';
|
||||
encodedData: ArrayBuffer;
|
||||
pts: number;
|
||||
duration: number;
|
||||
} | {
|
||||
type: 'flush-encoder';
|
||||
} | {
|
||||
type: 'close-encoder';
|
||||
};
|
||||
|
||||
export type WorkerResponse = {
|
||||
id: number;
|
||||
} & ({
|
||||
success: true;
|
||||
data: WorkerResponseData;
|
||||
} | {
|
||||
success: false;
|
||||
error: unknown;
|
||||
});
|
||||
|
||||
export function assert(x: unknown): asserts x {
|
||||
if (!x) {
|
||||
throw new Error('Assertion failed.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/*!
|
||||
* Copyright (c) 2026-present, Vanilagy and contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { assert, type WorkerCommand, type WorkerResponse, type WorkerResponseData } from './shared';
|
||||
// @ts-expect-error An esbuild plugin handles this, TypeScript doesn't need to understand
|
||||
import createWorker from './codec.worker';
|
||||
|
||||
type ExtendedWorker = Worker & {
|
||||
ref?: () => void;
|
||||
unref?: () => void;
|
||||
};
|
||||
|
||||
let workerPromise: Promise<ExtendedWorker> | null;
|
||||
let nextMessageId = 0;
|
||||
const pendingMessages = new Map<number, {
|
||||
resolve: (value: WorkerResponseData) => void;
|
||||
reject: (reason?: unknown) => void;
|
||||
}>();
|
||||
|
||||
let refCount = 0;
|
||||
let keepAliveInterval: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
export const refWorker = async () => {
|
||||
refCount++;
|
||||
if (refCount === 1) {
|
||||
keepAliveInterval = setInterval(() => {}, 2 ** 31 - 1);
|
||||
const worker = await ensureWorker();
|
||||
worker.ref?.();
|
||||
}
|
||||
};
|
||||
|
||||
export const unrefWorker = async () => {
|
||||
refCount--;
|
||||
if (refCount === 0) {
|
||||
if (keepAliveInterval !== null) {
|
||||
clearInterval(keepAliveInterval);
|
||||
keepAliveInterval = null;
|
||||
}
|
||||
|
||||
const worker = await workerPromise;
|
||||
if (worker) {
|
||||
if (worker.unref) {
|
||||
worker.unref(); // If we don't do this, then the Node process never terminates by itself
|
||||
// Keep the worker around tho
|
||||
} else if (typeof window === 'undefined') {
|
||||
// Non-browser environment without unref - terminate instead
|
||||
worker.terminate();
|
||||
workerPromise = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const sendCommand = async <T extends string>(
|
||||
command: WorkerCommand & { type: T },
|
||||
transferables?: Transferable[],
|
||||
) => {
|
||||
const worker = await ensureWorker();
|
||||
|
||||
return new Promise<WorkerResponseData & { type: T }>((resolve, reject) => {
|
||||
const id = nextMessageId++;
|
||||
pendingMessages.set(id, {
|
||||
resolve: resolve as (value: WorkerResponseData) => void,
|
||||
reject,
|
||||
});
|
||||
|
||||
if (transferables) {
|
||||
worker.postMessage({ id, command }, transferables);
|
||||
} else {
|
||||
worker.postMessage({ id, command });
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const ensureWorker = () => {
|
||||
return workerPromise ??= (async () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
||||
const worker = (await createWorker()) as ExtendedWorker;
|
||||
worker.unref?.(); // Start unreffed
|
||||
|
||||
const onMessage = (data: WorkerResponse) => {
|
||||
const pending = pendingMessages.get(data.id);
|
||||
assert(pending !== undefined);
|
||||
|
||||
pendingMessages.delete(data.id);
|
||||
if (data.success) {
|
||||
pending.resolve(data.data);
|
||||
} else {
|
||||
pending.reject(data.error);
|
||||
}
|
||||
};
|
||||
|
||||
if (worker.addEventListener) {
|
||||
worker.addEventListener('message', event => onMessage(event.data as WorkerResponse));
|
||||
} else {
|
||||
const nodeWorker = worker as unknown as {
|
||||
on: (event: string, listener: (data: never) => void) => void;
|
||||
};
|
||||
nodeWorker.on('message', onMessage);
|
||||
}
|
||||
|
||||
return worker;
|
||||
})();
|
||||
};
|
||||
Reference in New Issue
Block a user