@mediabunny/server 1.61.4 → 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
|
@@ -64,7 +64,6 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
64
64
|
this.dstFrame = null;
|
|
65
65
|
this.lastBuffer = null;
|
|
66
66
|
this.packetEmitted = false;
|
|
67
|
-
this.lastScalerKey = null;
|
|
68
67
|
this.quantizer = null;
|
|
69
68
|
// Bookkeeping to restore the original timing information
|
|
70
69
|
this.preciseTimings = [];
|
|
@@ -113,11 +112,6 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
113
112
|
throw new Error(`Unable to obtain libav codec for '${this.codec}'.`);
|
|
114
113
|
}
|
|
115
114
|
this.avCodec = codec;
|
|
116
|
-
if (this.config.bitrateMode !== 'quantizer') {
|
|
117
|
-
// In quantizer mode, the codec context is instead created lazily on the first encode, once the quantizer
|
|
118
|
-
// value is known
|
|
119
|
-
await this.createCodecContext();
|
|
120
|
-
}
|
|
121
115
|
}
|
|
122
116
|
async createCodecContext() {
|
|
123
117
|
(0, misc_2.assert)(this.codecContext === null);
|
|
@@ -211,8 +205,10 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
211
205
|
(0, misc_2.assert)(this.quantizer !== null);
|
|
212
206
|
// Map the quantizer from the scale used by Mediabunny to the scale expected by the specific FFmpeg encoder
|
|
213
207
|
let mapped;
|
|
214
|
-
if (this.avCodec.name === 'libaom-av1'
|
|
215
|
-
|
|
208
|
+
if (this.avCodec.name === 'libaom-av1'
|
|
209
|
+
|| this.avCodec.name === 'libsvtav1'
|
|
210
|
+
|| this.avCodec.name === 'libvpx-vp9') {
|
|
211
|
+
// Mediabunny uses the quantizer index (0-255), while these encoders expect the 0-63 quantizer scale
|
|
216
212
|
mapped = Math.round(this.quantizer / 4);
|
|
217
213
|
}
|
|
218
214
|
else {
|
|
@@ -250,6 +246,44 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
250
246
|
(0, misc_2.assert)(profile !== undefined);
|
|
251
247
|
codecContext.setOption('profile', String(profile));
|
|
252
248
|
}
|
|
249
|
+
let primaries = this.frame.colorPrimaries;
|
|
250
|
+
let transfer = this.frame.colorTrc;
|
|
251
|
+
let matrix = this.frame.colorSpace;
|
|
252
|
+
let range = this.frame.colorRange;
|
|
253
|
+
const inputIsRgb = (0, misc_1.isRgbPixelFormat)(this.frame.format);
|
|
254
|
+
const outputIsRgb = (0, misc_1.isRgbPixelFormat)(pixelFormat);
|
|
255
|
+
// If the pixel format "types" differ, modify the destination color space accordingly
|
|
256
|
+
if (inputIsRgb && !outputIsRgb) {
|
|
257
|
+
// Very common case: RGB->YUV conversion during encoding
|
|
258
|
+
// Retag sRGB as BT.709 since players handle that way better, and pick the matrix matching the primaries
|
|
259
|
+
if (primaries === NodeAv.AVCOL_PRI_UNSPECIFIED) {
|
|
260
|
+
primaries = NodeAv.AVCOL_PRI_BT709;
|
|
261
|
+
}
|
|
262
|
+
if (transfer === NodeAv.AVCOL_TRC_UNSPECIFIED || transfer === NodeAv.AVCOL_TRC_IEC61966_2_1) {
|
|
263
|
+
transfer = NodeAv.AVCOL_TRC_BT709;
|
|
264
|
+
}
|
|
265
|
+
if (primaries === NodeAv.AVCOL_PRI_BT2020) {
|
|
266
|
+
matrix = NodeAv.AVCOL_SPC_BT2020_NCL;
|
|
267
|
+
}
|
|
268
|
+
else if (primaries === NodeAv.AVCOL_PRI_BT470BG) {
|
|
269
|
+
matrix = NodeAv.AVCOL_SPC_BT470BG;
|
|
270
|
+
}
|
|
271
|
+
else if (primaries === NodeAv.AVCOL_PRI_SMPTE170M) {
|
|
272
|
+
matrix = NodeAv.AVCOL_SPC_SMPTE170M;
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
matrix = NodeAv.AVCOL_SPC_BT709;
|
|
276
|
+
}
|
|
277
|
+
range = NodeAv.AVCOL_RANGE_MPEG;
|
|
278
|
+
}
|
|
279
|
+
else if (!inputIsRgb && outputIsRgb) {
|
|
280
|
+
matrix = NodeAv.AVCOL_SPC_RGB;
|
|
281
|
+
range = NodeAv.AVCOL_RANGE_JPEG;
|
|
282
|
+
}
|
|
283
|
+
codecContext.colorPrimaries = primaries;
|
|
284
|
+
codecContext.colorTrc = transfer;
|
|
285
|
+
codecContext.colorSpace = matrix;
|
|
286
|
+
codecContext.colorRange = range;
|
|
253
287
|
const ret = await codecContext.open2();
|
|
254
288
|
NodeAv.FFmpegError.throwIfError(ret, 'Open codec context');
|
|
255
289
|
this.codecContext = codecContext;
|
|
@@ -287,10 +321,6 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
287
321
|
}
|
|
288
322
|
this.quantizer = quantizer;
|
|
289
323
|
}
|
|
290
|
-
if (this.codecContext === null) {
|
|
291
|
-
await this.createCodecContext();
|
|
292
|
-
}
|
|
293
|
-
(0, misc_2.assert)(this.codecContext);
|
|
294
324
|
if (videoSample._data instanceof video_sample_1.AvFrameVideoSampleResource) {
|
|
295
325
|
// Release any buffers still referenced from the previous encode before reffing the new frame, otherwise
|
|
296
326
|
// av_frame_ref leaks them
|
|
@@ -304,21 +334,21 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
304
334
|
}
|
|
305
335
|
this.lastBuffer = await (0, video_sample_1.copyVideoSampleToAvFrame)(videoSample, this.frame, this.lastBuffer);
|
|
306
336
|
}
|
|
337
|
+
if (this.codecContext === null) {
|
|
338
|
+
// We create it after the frame was initializied since info about the frame is needed for context init
|
|
339
|
+
await this.createCodecContext();
|
|
340
|
+
}
|
|
341
|
+
(0, misc_2.assert)(this.codecContext);
|
|
307
342
|
let frameToEncode = this.frame;
|
|
308
343
|
const requiresScaler = this.codecContext.pixelFormat !== this.frame.format
|
|
309
344
|
|| this.codecContext.width !== this.frame.width
|
|
310
345
|
|| this.codecContext.height !== this.frame.height;
|
|
311
346
|
if (requiresScaler) {
|
|
312
347
|
if (!this.scaler) {
|
|
348
|
+
// Left unconfigured on purpose: getContext/initContext ignore color properties and use BT.601
|
|
313
349
|
this.scaler = new NodeAv.SoftwareScaleContext();
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
const needsConfigure = key !== this.lastScalerKey;
|
|
317
|
-
if (needsConfigure) {
|
|
318
|
-
this.scaler.getContext(this.frame.width, this.frame.height, this.frame.format, this.codecContext.width, this.codecContext.height, this.codecContext.pixelFormat, NodeAv.SWS_FAST_BILINEAR);
|
|
319
|
-
this.lastScalerKey = key;
|
|
320
|
-
const ret = this.scaler.initContext();
|
|
321
|
-
NodeAv.FFmpegError.throwIfError(ret, 'initContext');
|
|
350
|
+
this.scaler.allocContext();
|
|
351
|
+
this.scaler.setOption('sws_flags', 'fast_bilinear');
|
|
322
352
|
}
|
|
323
353
|
if (!this.dstFrame) {
|
|
324
354
|
this.dstFrame = new NodeAv.Frame();
|
|
@@ -328,8 +358,13 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
328
358
|
this.dstFrame.format = this.codecContext.pixelFormat;
|
|
329
359
|
this.dstFrame.allocBuffer();
|
|
330
360
|
}
|
|
331
|
-
await this.scaler.scaleFrame(this.dstFrame, this.frame);
|
|
332
361
|
this.dstFrame.copyProps(this.frame);
|
|
362
|
+
this.dstFrame.colorPrimaries = this.codecContext.colorPrimaries;
|
|
363
|
+
this.dstFrame.colorTrc = this.codecContext.colorTrc;
|
|
364
|
+
this.dstFrame.colorSpace = this.codecContext.colorSpace;
|
|
365
|
+
this.dstFrame.colorRange = this.codecContext.colorRange;
|
|
366
|
+
const ret = await this.scaler.scaleFrame(this.dstFrame, this.frame);
|
|
367
|
+
NodeAv.FFmpegError.throwIfError(ret, 'Scale frame');
|
|
333
368
|
frameToEncode = this.dstFrame;
|
|
334
369
|
}
|
|
335
370
|
frameToEncode.pts = BigInt(videoSample.microsecondTimestamp);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"video-sample.d.ts","sourceRoot":"","sources":["../../../src/video-sample.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACN,KAAK,YAAY,EACjB,sBAAsB,EACtB,mBAAmB,EACnB,qBAAqB,EACrB,WAAW,EACX,eAAe,EACf,WAAW,EACX,cAAc,EACd,oCAAoC,EACpC,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"video-sample.d.ts","sourceRoot":"","sources":["../../../src/video-sample.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACN,KAAK,YAAY,EACjB,sBAAsB,EACtB,mBAAmB,EACnB,qBAAqB,EACrB,WAAW,EACX,eAAe,EACf,WAAW,EACX,cAAc,EACd,oCAAoC,EACpC,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;AAsBlC;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,0BAA2B,SAAQ,mBAAmB;IAIlE;;;OAGG;IACH,IAAI,KAAK,iBAMR;gBAEW,KAAK,EAAE,MAAM,CAAC,KAAK;IAa/B,SAAS,IAAI,sBAAsB,GAAG,IAAI;IAI1C,aAAa,IAAI,MAAM;IAIvB,cAAc,IAAI,MAAM;IAIxB,mBAAmB,IAAI,MAAM;IAQ7B,oBAAoB,IAAI,MAAM;IAQ9B,aAAa,IAAI,qBAAqB;IActC,KAAK,IAAI,IAAI;IAKb,aAAa,IAAI,YAAY,CAAC,cAAc,EAAE,CAAC;IASzC,WAAW,CAChB,IAAI,EAAE,WAAW,CAAC,eAAe,EAAE,WAAW,CAAC,EAG/C,UAAU,EAAE,oBAAoB,GAC9B,OAAO,CAAC,WAAW,CAAC;CAoDvB;AAED,eAAO,MAAM,wBAAwB,GAAU,QAAQ,WAAW,EAAE,OAAO,MAAM,CAAC,KAAK,EAAE,YAAY,MAAM,GAAG,IAAI,qCA+BjH,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAChC,QAAQ,WAAW,EACnB,aAAa,oCAAoC,KAC/C,OAAO,CAAC,WAAW,GAAG,IAAI,CAwG5B,CAAC"}
|
|
@@ -144,24 +144,46 @@ class AvFrameVideoSampleResource extends mediabunny_1.VideoSampleResource {
|
|
|
144
144
|
colorSpace) {
|
|
145
145
|
const width = this.frame.width;
|
|
146
146
|
const height = this.frame.height;
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
147
|
+
// A separate ref so we can fill in unspecified color properties without changing the original frame
|
|
148
|
+
const srcFrame = new NodeAv.Frame();
|
|
149
|
+
srcFrame.alloc();
|
|
150
|
+
srcFrame.ref(this.frame);
|
|
151
|
+
const srcIsRgb = (0, misc_2.isRgbPixelFormat)(srcFrame.format);
|
|
152
|
+
if (srcFrame.colorPrimaries === NodeAv.AVCOL_PRI_UNSPECIFIED) {
|
|
153
|
+
srcFrame.colorPrimaries = NodeAv.AVCOL_PRI_BT709;
|
|
154
|
+
}
|
|
155
|
+
if (srcFrame.colorTrc === NodeAv.AVCOL_TRC_UNSPECIFIED) {
|
|
156
|
+
srcFrame.colorTrc = srcIsRgb ? NodeAv.AVCOL_TRC_IEC61966_2_1 : NodeAv.AVCOL_TRC_BT709;
|
|
157
|
+
}
|
|
158
|
+
if (srcFrame.colorSpace === NodeAv.AVCOL_SPC_UNSPECIFIED) {
|
|
159
|
+
srcFrame.colorSpace = srcIsRgb ? NodeAv.AVCOL_SPC_RGB : NodeAv.AVCOL_SPC_BT709;
|
|
160
|
+
}
|
|
161
|
+
if (srcFrame.colorRange === NodeAv.AVCOL_RANGE_UNSPECIFIED) {
|
|
162
|
+
srcFrame.colorRange = srcIsRgb ? NodeAv.AVCOL_RANGE_JPEG : NodeAv.AVCOL_RANGE_MPEG;
|
|
163
|
+
}
|
|
151
164
|
const dstFrame = new NodeAv.Frame();
|
|
165
|
+
dstFrame.alloc();
|
|
152
166
|
dstFrame.width = width;
|
|
153
167
|
dstFrame.height = height;
|
|
154
|
-
dstFrame.format =
|
|
155
|
-
dstFrame.alloc();
|
|
168
|
+
dstFrame.format = (0, misc_2.fromPixelFormat)('RGBA');
|
|
156
169
|
dstFrame.allocBuffer();
|
|
157
|
-
|
|
170
|
+
dstFrame.colorPrimaries = srcFrame.colorPrimaries;
|
|
171
|
+
dstFrame.colorTrc = srcFrame.colorTrc;
|
|
172
|
+
dstFrame.colorSpace = NodeAv.AVCOL_SPC_RGB;
|
|
173
|
+
dstFrame.colorRange = NodeAv.AVCOL_RANGE_JPEG;
|
|
174
|
+
// Left unconfigured on purpose: getContext/initContext ignore the frames' color properties and use BT.601
|
|
175
|
+
const scaler = new NodeAv.SoftwareScaleContext();
|
|
176
|
+
scaler.allocContext();
|
|
177
|
+
scaler.setOption('sws_flags', 'bilinear');
|
|
158
178
|
try {
|
|
159
|
-
await scaler.scaleFrame(dstFrame, srcFrame);
|
|
179
|
+
const ret = await scaler.scaleFrame(dstFrame, srcFrame);
|
|
180
|
+
NodeAv.FFmpegError.throwIfError(ret, 'Scale frame');
|
|
160
181
|
}
|
|
161
182
|
finally {
|
|
162
183
|
scaler.freeContext();
|
|
184
|
+
srcFrame.free();
|
|
163
185
|
}
|
|
164
|
-
dstFrame.sampleAspectRatio =
|
|
186
|
+
dstFrame.sampleAspectRatio = this.frame.sampleAspectRatio;
|
|
165
187
|
return new mediabunny_1.VideoSample(new AvFrameVideoSampleResource(dstFrame), init);
|
|
166
188
|
}
|
|
167
189
|
}
|