@mediabunny/server 1.61.3 → 1.61.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundles/mediabunny-server.cjs +77 -43
- package/dist/bundles/mediabunny-server.min.cjs +2 -2
- package/dist/bundles/mediabunny-server.min.mjs +2 -2
- package/dist/bundles/mediabunny-server.mjs +77 -43
- package/dist/modules/src/misc.d.ts +1 -0
- package/dist/modules/src/misc.d.ts.map +1 -1
- package/dist/modules/src/misc.js +7 -1
- package/dist/modules/src/video-encoder.d.ts +0 -1
- package/dist/modules/src/video-encoder.d.ts.map +1 -1
- package/dist/modules/src/video-encoder.js +56 -21
- package/dist/modules/src/video-sample.d.ts.map +1 -1
- package/dist/modules/src/video-sample.js +31 -9
- package/dist/modules/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/misc.ts +6 -0
- package/src/video-encoder.ts +64 -31
- package/src/video-sample.ts +33 -13
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mediabunny/server",
|
|
3
3
|
"author": "Vanilagy",
|
|
4
|
-
"version": "1.61.
|
|
4
|
+
"version": "1.61.5",
|
|
5
5
|
"description": "Adds full video and audio decoder and encoder support to Mediabunny for use in server-side environments (Node, Bun, Deno). Based on NodeAV.",
|
|
6
6
|
"main": "./dist/bundles/mediabunny-server.cjs",
|
|
7
7
|
"module": "./dist/bundles/mediabunny-server.mjs",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"node-av": "^6.0.0",
|
|
38
|
-
"@mediabunny/prores": "^1.61.
|
|
38
|
+
"@mediabunny/prores": "^1.61.5"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"mediabunny": "^1.45.0"
|
package/src/misc.ts
CHANGED
|
@@ -156,6 +156,12 @@ export const unmapMatrixCoefficients = (matrix: number) => {
|
|
|
156
156
|
return null;
|
|
157
157
|
};
|
|
158
158
|
|
|
159
|
+
export const isRgbPixelFormat = (pixelFormat: NodeAv.AVPixelFormat) => {
|
|
160
|
+
// NodeAV doesn't expose av_pix_fmt_desc_get, so go by name
|
|
161
|
+
const name = NodeAv.avGetPixFmtName(pixelFormat) ?? '';
|
|
162
|
+
return /rgb|bgr|gbr/.test(name);
|
|
163
|
+
};
|
|
164
|
+
|
|
159
165
|
export const toPixelFormat = (ffmpegPixelFormat: NodeAv.AVPixelFormat): VideoSamplePixelFormat | null => {
|
|
160
166
|
switch (ffmpegPixelFormat) {
|
|
161
167
|
case NodeAv.AV_PIX_FMT_YUV420P: return 'I420';
|
package/src/video-encoder.ts
CHANGED
|
@@ -19,6 +19,7 @@ import * as NodeAv from 'node-av';
|
|
|
19
19
|
import {
|
|
20
20
|
CODEC_TO_CODEC_ID,
|
|
21
21
|
getHardwareEncoderCodec,
|
|
22
|
+
isRgbPixelFormat,
|
|
22
23
|
unmapColorPrimaries,
|
|
23
24
|
unmapMatrixCoefficients,
|
|
24
25
|
unmapTransferCharacteristics,
|
|
@@ -59,7 +60,6 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
59
60
|
avCodec!: NodeAv.Codec;
|
|
60
61
|
lastBuffer: Buffer | null = null;
|
|
61
62
|
packetEmitted = false;
|
|
62
|
-
lastScalerKey: string | null = null;
|
|
63
63
|
quantizer: number | null = null;
|
|
64
64
|
|
|
65
65
|
// Bookkeeping to restore the original timing information
|
|
@@ -125,12 +125,6 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
this.avCodec = codec;
|
|
128
|
-
|
|
129
|
-
if (this.config.bitrateMode !== 'quantizer') {
|
|
130
|
-
// In quantizer mode, the codec context is instead created lazily on the first encode, once the quantizer
|
|
131
|
-
// value is known
|
|
132
|
-
await this.createCodecContext();
|
|
133
|
-
}
|
|
134
128
|
}
|
|
135
129
|
|
|
136
130
|
async createCodecContext() {
|
|
@@ -235,8 +229,12 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
235
229
|
|
|
236
230
|
// Map the quantizer from the scale used by Mediabunny to the scale expected by the specific FFmpeg encoder
|
|
237
231
|
let mapped: number;
|
|
238
|
-
if (
|
|
239
|
-
|
|
232
|
+
if (
|
|
233
|
+
this.avCodec.name === 'libaom-av1'
|
|
234
|
+
|| this.avCodec.name === 'libsvtav1'
|
|
235
|
+
|| this.avCodec.name === 'libvpx-vp9'
|
|
236
|
+
) {
|
|
237
|
+
// Mediabunny uses the quantizer index (0-255), while these encoders expect the 0-63 quantizer scale
|
|
240
238
|
mapped = Math.round(this.quantizer / 4);
|
|
241
239
|
} else {
|
|
242
240
|
// Everything else lines up with Mediabunny directly
|
|
@@ -273,6 +271,46 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
273
271
|
codecContext.setOption('profile', String(profile));
|
|
274
272
|
}
|
|
275
273
|
|
|
274
|
+
let primaries = this.frame.colorPrimaries;
|
|
275
|
+
let transfer = this.frame.colorTrc;
|
|
276
|
+
let matrix = this.frame.colorSpace;
|
|
277
|
+
let range = this.frame.colorRange;
|
|
278
|
+
|
|
279
|
+
const inputIsRgb = isRgbPixelFormat(this.frame.format as NodeAv.AVPixelFormat);
|
|
280
|
+
const outputIsRgb = isRgbPixelFormat(pixelFormat);
|
|
281
|
+
|
|
282
|
+
// If the pixel format "types" differ, modify the destination color space accordingly
|
|
283
|
+
if (inputIsRgb && !outputIsRgb) {
|
|
284
|
+
// Very common case: RGB->YUV conversion during encoding
|
|
285
|
+
// Retag sRGB as BT.709 since players handle that way better, and pick the matrix matching the primaries
|
|
286
|
+
if (primaries === NodeAv.AVCOL_PRI_UNSPECIFIED) {
|
|
287
|
+
primaries = NodeAv.AVCOL_PRI_BT709;
|
|
288
|
+
}
|
|
289
|
+
if (transfer === NodeAv.AVCOL_TRC_UNSPECIFIED || transfer === NodeAv.AVCOL_TRC_IEC61966_2_1) {
|
|
290
|
+
transfer = NodeAv.AVCOL_TRC_BT709;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (primaries === NodeAv.AVCOL_PRI_BT2020) {
|
|
294
|
+
matrix = NodeAv.AVCOL_SPC_BT2020_NCL;
|
|
295
|
+
} else if (primaries === NodeAv.AVCOL_PRI_BT470BG) {
|
|
296
|
+
matrix = NodeAv.AVCOL_SPC_BT470BG;
|
|
297
|
+
} else if (primaries === NodeAv.AVCOL_PRI_SMPTE170M) {
|
|
298
|
+
matrix = NodeAv.AVCOL_SPC_SMPTE170M;
|
|
299
|
+
} else {
|
|
300
|
+
matrix = NodeAv.AVCOL_SPC_BT709;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
range = NodeAv.AVCOL_RANGE_MPEG;
|
|
304
|
+
} else if (!inputIsRgb && outputIsRgb) {
|
|
305
|
+
matrix = NodeAv.AVCOL_SPC_RGB;
|
|
306
|
+
range = NodeAv.AVCOL_RANGE_JPEG;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
codecContext.colorPrimaries = primaries;
|
|
310
|
+
codecContext.colorTrc = transfer;
|
|
311
|
+
codecContext.colorSpace = matrix;
|
|
312
|
+
codecContext.colorRange = range;
|
|
313
|
+
|
|
276
314
|
const ret = await codecContext.open2();
|
|
277
315
|
NodeAv.FFmpegError.throwIfError(ret, 'Open codec context');
|
|
278
316
|
|
|
@@ -315,11 +353,6 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
315
353
|
this.quantizer = quantizer;
|
|
316
354
|
}
|
|
317
355
|
|
|
318
|
-
if (this.codecContext === null) {
|
|
319
|
-
await this.createCodecContext();
|
|
320
|
-
}
|
|
321
|
-
assert(this.codecContext);
|
|
322
|
-
|
|
323
356
|
if (videoSample._data instanceof AvFrameVideoSampleResource) {
|
|
324
357
|
// Release any buffers still referenced from the previous encode before reffing the new frame, otherwise
|
|
325
358
|
// av_frame_ref leaks them
|
|
@@ -334,6 +367,12 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
334
367
|
this.lastBuffer = await copyVideoSampleToAvFrame(videoSample, this.frame, this.lastBuffer);
|
|
335
368
|
}
|
|
336
369
|
|
|
370
|
+
if (this.codecContext === null) {
|
|
371
|
+
// We create it after the frame was initializied since info about the frame is needed for context init
|
|
372
|
+
await this.createCodecContext();
|
|
373
|
+
}
|
|
374
|
+
assert(this.codecContext);
|
|
375
|
+
|
|
337
376
|
let frameToEncode = this.frame;
|
|
338
377
|
|
|
339
378
|
const requiresScaler
|
|
@@ -343,23 +382,10 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
343
382
|
|
|
344
383
|
if (requiresScaler) {
|
|
345
384
|
if (!this.scaler) {
|
|
385
|
+
// Left unconfigured on purpose: getContext/initContext ignore color properties and use BT.601
|
|
346
386
|
this.scaler = new NodeAv.SoftwareScaleContext();
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
const key = `${this.frame.width}x${this.frame.height}:${this.frame.format}`;
|
|
350
|
-
const needsConfigure = key !== this.lastScalerKey;
|
|
351
|
-
|
|
352
|
-
if (needsConfigure) {
|
|
353
|
-
this.scaler.getContext(
|
|
354
|
-
this.frame.width, this.frame.height, this.frame.format as NodeAv.AVPixelFormat,
|
|
355
|
-
this.codecContext.width, this.codecContext.height, this.codecContext.pixelFormat,
|
|
356
|
-
NodeAv.SWS_FAST_BILINEAR,
|
|
357
|
-
);
|
|
358
|
-
|
|
359
|
-
this.lastScalerKey = key;
|
|
360
|
-
|
|
361
|
-
const ret = this.scaler.initContext();
|
|
362
|
-
NodeAv.FFmpegError.throwIfError(ret, 'initContext');
|
|
387
|
+
this.scaler.allocContext();
|
|
388
|
+
this.scaler.setOption('sws_flags', 'fast_bilinear');
|
|
363
389
|
}
|
|
364
390
|
|
|
365
391
|
if (!this.dstFrame) {
|
|
@@ -371,8 +397,15 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
371
397
|
this.dstFrame.allocBuffer();
|
|
372
398
|
}
|
|
373
399
|
|
|
374
|
-
await this.scaler.scaleFrame(this.dstFrame, this.frame);
|
|
375
400
|
this.dstFrame.copyProps(this.frame);
|
|
401
|
+
this.dstFrame.colorPrimaries = this.codecContext.colorPrimaries;
|
|
402
|
+
this.dstFrame.colorTrc = this.codecContext.colorTrc;
|
|
403
|
+
this.dstFrame.colorSpace = this.codecContext.colorSpace;
|
|
404
|
+
this.dstFrame.colorRange = this.codecContext.colorRange;
|
|
405
|
+
|
|
406
|
+
const ret = await this.scaler.scaleFrame(this.dstFrame, this.frame);
|
|
407
|
+
NodeAv.FFmpegError.throwIfError(ret, 'Scale frame');
|
|
408
|
+
|
|
376
409
|
frameToEncode = this.dstFrame;
|
|
377
410
|
}
|
|
378
411
|
|
package/src/video-sample.ts
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
import * as NodeAv from 'node-av';
|
|
21
21
|
import { assert, toUint8Array } from '../../../src/misc';
|
|
22
22
|
import {
|
|
23
|
+
isRgbPixelFormat,
|
|
23
24
|
toPixelFormat,
|
|
24
25
|
unmapColorPrimaries,
|
|
25
26
|
unmapTransferCharacteristics,
|
|
@@ -148,32 +149,51 @@ export class AvFrameVideoSampleResource extends VideoSampleResource {
|
|
|
148
149
|
const width = this.frame.width;
|
|
149
150
|
const height = this.frame.height;
|
|
150
151
|
|
|
151
|
-
|
|
152
|
-
const
|
|
153
|
-
|
|
152
|
+
// A separate ref so we can fill in unspecified color properties without changing the original frame
|
|
153
|
+
const srcFrame = new NodeAv.Frame();
|
|
154
|
+
srcFrame.alloc();
|
|
155
|
+
srcFrame.ref(this.frame);
|
|
154
156
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
)
|
|
157
|
+
const srcIsRgb = isRgbPixelFormat(srcFrame.format as NodeAv.AVPixelFormat);
|
|
158
|
+
if (srcFrame.colorPrimaries === NodeAv.AVCOL_PRI_UNSPECIFIED) {
|
|
159
|
+
srcFrame.colorPrimaries = NodeAv.AVCOL_PRI_BT709;
|
|
160
|
+
}
|
|
161
|
+
if (srcFrame.colorTrc === NodeAv.AVCOL_TRC_UNSPECIFIED) {
|
|
162
|
+
srcFrame.colorTrc = srcIsRgb ? NodeAv.AVCOL_TRC_IEC61966_2_1 : NodeAv.AVCOL_TRC_BT709;
|
|
163
|
+
}
|
|
164
|
+
if (srcFrame.colorSpace === NodeAv.AVCOL_SPC_UNSPECIFIED) {
|
|
165
|
+
srcFrame.colorSpace = srcIsRgb ? NodeAv.AVCOL_SPC_RGB : NodeAv.AVCOL_SPC_BT709;
|
|
166
|
+
}
|
|
167
|
+
if (srcFrame.colorRange === NodeAv.AVCOL_RANGE_UNSPECIFIED) {
|
|
168
|
+
srcFrame.colorRange = srcIsRgb ? NodeAv.AVCOL_RANGE_JPEG : NodeAv.AVCOL_RANGE_MPEG;
|
|
169
|
+
}
|
|
160
170
|
|
|
161
171
|
const dstFrame = new NodeAv.Frame();
|
|
172
|
+
dstFrame.alloc();
|
|
162
173
|
dstFrame.width = width;
|
|
163
174
|
dstFrame.height = height;
|
|
164
|
-
dstFrame.format =
|
|
165
|
-
dstFrame.alloc();
|
|
175
|
+
dstFrame.format = fromPixelFormat('RGBA');
|
|
166
176
|
dstFrame.allocBuffer();
|
|
167
177
|
|
|
168
|
-
|
|
178
|
+
dstFrame.colorPrimaries = srcFrame.colorPrimaries;
|
|
179
|
+
dstFrame.colorTrc = srcFrame.colorTrc;
|
|
180
|
+
dstFrame.colorSpace = NodeAv.AVCOL_SPC_RGB;
|
|
181
|
+
dstFrame.colorRange = NodeAv.AVCOL_RANGE_JPEG;
|
|
182
|
+
|
|
183
|
+
// Left unconfigured on purpose: getContext/initContext ignore the frames' color properties and use BT.601
|
|
184
|
+
const scaler = new NodeAv.SoftwareScaleContext();
|
|
185
|
+
scaler.allocContext();
|
|
186
|
+
scaler.setOption('sws_flags', 'bilinear');
|
|
169
187
|
|
|
170
188
|
try {
|
|
171
|
-
await scaler.scaleFrame(dstFrame, srcFrame);
|
|
189
|
+
const ret = await scaler.scaleFrame(dstFrame, srcFrame);
|
|
190
|
+
NodeAv.FFmpegError.throwIfError(ret, 'Scale frame');
|
|
172
191
|
} finally {
|
|
173
192
|
scaler.freeContext();
|
|
193
|
+
srcFrame.free();
|
|
174
194
|
}
|
|
175
195
|
|
|
176
|
-
dstFrame.sampleAspectRatio =
|
|
196
|
+
dstFrame.sampleAspectRatio = this.frame.sampleAspectRatio;
|
|
177
197
|
|
|
178
198
|
return new VideoSample(new AvFrameVideoSampleResource(dstFrame), init);
|
|
179
199
|
}
|