@mediabunny/server 1.65.2 → 1.66.0
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 +707 -701
- package/dist/bundles/mediabunny-server.min.cjs +2 -3
- package/dist/bundles/mediabunny-server.min.mjs +2 -3
- package/dist/bundles/mediabunny-server.mjs +700 -694
- package/dist/modules/src/audio-decoder.d.ts.map +1 -1
- package/dist/modules/src/audio-decoder.js +1 -0
- package/dist/modules/src/audio-encoder.d.ts +1 -1
- package/dist/modules/src/audio-encoder.d.ts.map +1 -1
- package/dist/modules/src/audio-encoder.js +68 -14
- package/dist/modules/src/audio-sample.d.ts.map +1 -1
- package/dist/modules/src/audio-sample.js +9 -6
- package/dist/modules/src/misc.d.ts.map +1 -1
- package/dist/modules/src/misc.js +3 -1
- package/dist/modules/src/video-decoder.d.ts.map +1 -1
- package/dist/modules/src/video-decoder.js +19 -1
- package/dist/modules/src/video-encoder.d.ts.map +1 -1
- package/dist/modules/src/video-encoder.js +23 -0
- package/dist/modules/src/video-sample.d.ts.map +1 -1
- package/dist/modules/src/video-sample.js +39 -5
- package/dist/modules/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/audio-decoder.ts +1 -0
- package/src/audio-encoder.ts +74 -14
- package/src/audio-sample.ts +10 -6
- package/src/misc.ts +3 -1
- package/src/video-decoder.ts +27 -2
- package/src/video-encoder.ts +24 -0
- package/src/video-sample.ts +47 -6
package/src/audio-encoder.ts
CHANGED
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
buildAdtsHeaderTemplate,
|
|
25
25
|
parseAacAudioSpecificConfig,
|
|
26
26
|
} from '../../../shared/aac-misc';
|
|
27
|
-
import { DTS_CHANNEL_COUNTS, DTS_SAMPLE_RATES
|
|
27
|
+
import { clampDtsBitrate, DTS_CHANNEL_COUNTS, DTS_SAMPLE_RATES } from '../../../shared/dts-misc';
|
|
28
28
|
import { TRUEHD_CHANNEL_COUNTS, TRUEHD_SAMPLE_RATES } from '../../../shared/truehd-misc';
|
|
29
29
|
|
|
30
30
|
const AAC_SAMPLE_RATES
|
|
@@ -33,8 +33,19 @@ const OPUS_SAMPLE_RATES = [8000, 12000, 16000, 24000, 48000];
|
|
|
33
33
|
const MP3_SAMPLE_RATES = [8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000];
|
|
34
34
|
const AC3_SAMPLE_RATES = [32000, 44100, 48000];
|
|
35
35
|
|
|
36
|
+
const SUPPORTED_AAC_CHANNEL_COUNTS = [1, 2, 3, 4, 5, 6, 8];
|
|
37
|
+
|
|
36
38
|
const FRAME_SIZE_FALLBACK = 1024; // Just 'cause
|
|
37
39
|
|
|
40
|
+
const DTS_ENCODER_DELAY = 512; // In samples
|
|
41
|
+
|
|
42
|
+
// How many samples these encoders hold back without ever flushing them on their own
|
|
43
|
+
const UNFLUSHED_ENCODER_DELAYS: Partial<Record<AudioCodec, number>> = {
|
|
44
|
+
ac3: 256,
|
|
45
|
+
eac3: 256,
|
|
46
|
+
dts: DTS_ENCODER_DELAY,
|
|
47
|
+
};
|
|
48
|
+
|
|
38
49
|
export class NodeAvAudioEncoder extends CustomAudioEncoder {
|
|
39
50
|
frame!: NodeAv.Frame;
|
|
40
51
|
packet!: NodeAv.Packet;
|
|
@@ -53,17 +64,18 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
|
|
|
53
64
|
const { numberOfChannels, sampleRate } = config;
|
|
54
65
|
|
|
55
66
|
return (
|
|
56
|
-
codec === 'aac' && numberOfChannels
|
|
67
|
+
codec === 'aac' && SUPPORTED_AAC_CHANNEL_COUNTS.includes(numberOfChannels)
|
|
57
68
|
&& AAC_SAMPLE_RATES.includes(sampleRate)
|
|
58
69
|
) || (
|
|
59
|
-
|
|
70
|
+
// Without an explicit mapping family, libopus only does up to 8 channels
|
|
71
|
+
codec === 'opus' && numberOfChannels >= 1 && numberOfChannels <= 8
|
|
60
72
|
&& OPUS_SAMPLE_RATES.includes(sampleRate)
|
|
61
73
|
) || (
|
|
62
74
|
codec === 'mp3' && numberOfChannels >= 1 && numberOfChannels <= 2
|
|
63
75
|
&& MP3_SAMPLE_RATES.includes(sampleRate)
|
|
64
76
|
) || (
|
|
65
|
-
codec === 'vorbis' && numberOfChannels >= 1 && numberOfChannels <=
|
|
66
|
-
&& sampleRate <=
|
|
77
|
+
codec === 'vorbis' && numberOfChannels >= 1 && numberOfChannels <= 8
|
|
78
|
+
&& sampleRate <= 50000
|
|
67
79
|
) || (
|
|
68
80
|
codec === 'flac' && numberOfChannels >= 1 && numberOfChannels <= 8
|
|
69
81
|
&& sampleRate <= 655350
|
|
@@ -71,12 +83,11 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
|
|
|
71
83
|
codec === 'ac3' && numberOfChannels >= 1 && numberOfChannels <= 6
|
|
72
84
|
&& AC3_SAMPLE_RATES.includes(sampleRate)
|
|
73
85
|
) || (
|
|
74
|
-
codec === 'eac3' && numberOfChannels >= 1 && numberOfChannels <=
|
|
86
|
+
codec === 'eac3' && numberOfChannels >= 1 && numberOfChannels <= 6
|
|
75
87
|
&& AC3_SAMPLE_RATES.includes(sampleRate)
|
|
76
88
|
) || (
|
|
77
89
|
codec === 'dts' && DTS_CHANNEL_COUNTS.includes(numberOfChannels)
|
|
78
90
|
&& DTS_SAMPLE_RATES.includes(sampleRate)
|
|
79
|
-
&& dtsBitrateFits(resolveBitrate(config, codec), sampleRate, numberOfChannels)
|
|
80
91
|
) || (
|
|
81
92
|
codec === 'truehd' && TRUEHD_CHANNEL_COUNTS.includes(numberOfChannels)
|
|
82
93
|
&& TRUEHD_SAMPLE_RATES.includes(sampleRate)
|
|
@@ -99,29 +110,48 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
|
|
|
99
110
|
|
|
100
111
|
this.avCodec = codec;
|
|
101
112
|
|
|
102
|
-
|
|
113
|
+
if (this.codec !== 'flac') {
|
|
114
|
+
// FLAC's bit depth depends on the first sample's format
|
|
115
|
+
await this.createCodecContext(null);
|
|
116
|
+
}
|
|
103
117
|
}
|
|
104
118
|
|
|
105
|
-
async createCodecContext() {
|
|
119
|
+
async createCodecContext(firstSample: AudioSample | null) {
|
|
106
120
|
assert(this.codecContext === null);
|
|
107
121
|
|
|
108
122
|
const codecContext = new NodeAv.CodecContext();
|
|
109
123
|
codecContext.allocContext3(this.avCodec);
|
|
124
|
+
codecContext.threadCount = 0; // Auto
|
|
110
125
|
|
|
111
126
|
let sampleFormat = NodeAv.AV_SAMPLE_FMT_FLTP;
|
|
112
127
|
if (this.codec === 'truehd') {
|
|
113
128
|
// The first listed format is 16-bit, which would needlessly truncate what's supposed to be lossless
|
|
114
129
|
sampleFormat = NodeAv.AV_SAMPLE_FMT_S32P;
|
|
130
|
+
} else if (this.codec === 'flac') {
|
|
131
|
+
assert(firstSample);
|
|
132
|
+
|
|
133
|
+
if (firstSample.format.startsWith('u8') || firstSample.format.startsWith('s16')) {
|
|
134
|
+
sampleFormat = NodeAv.AV_SAMPLE_FMT_S16;
|
|
135
|
+
} else {
|
|
136
|
+
sampleFormat = NodeAv.AV_SAMPLE_FMT_S32;
|
|
137
|
+
codecContext.bitsPerRawSample = 24;
|
|
138
|
+
}
|
|
115
139
|
} else if (this.avCodec.sampleFormats && !this.avCodec.sampleFormats.includes(NodeAv.AV_SAMPLE_FMT_FLTP)) {
|
|
116
140
|
// Use a format that's supported
|
|
117
141
|
sampleFormat = this.avCodec.sampleFormats[0]!;
|
|
118
142
|
}
|
|
119
143
|
|
|
144
|
+
const isAacOrAc3 = this.codec === 'aac' || this.codec === 'ac3' || this.codec === 'eac3';
|
|
145
|
+
|
|
120
146
|
codecContext.sampleRate = this.config.sampleRate;
|
|
121
147
|
if (this.codec === 'dts') {
|
|
122
148
|
codecContext.channelLayout = getDtsChannelLayout(this.config.numberOfChannels);
|
|
123
149
|
} else if (this.codec === 'truehd') {
|
|
124
150
|
codecContext.channelLayout = getTrueHdChannelLayout(this.config.numberOfChannels);
|
|
151
|
+
} else if (isAacOrAc3 && this.config.numberOfChannels === 3) {
|
|
152
|
+
codecContext.channelLayout = NodeAv.AV_CHANNEL_LAYOUT_SURROUND;
|
|
153
|
+
} else if (isAacOrAc3 && this.config.numberOfChannels === 5) {
|
|
154
|
+
codecContext.channelLayout = NodeAv.AV_CHANNEL_LAYOUT_5POINT0_BACK;
|
|
125
155
|
} else {
|
|
126
156
|
codecContext.channelLayout = getChannelLayout(this.config.numberOfChannels);
|
|
127
157
|
}
|
|
@@ -150,7 +180,7 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
|
|
|
150
180
|
|
|
151
181
|
async encode(audioSample: AudioSample): Promise<void> {
|
|
152
182
|
if (this.codecContext === null) {
|
|
153
|
-
await this.createCodecContext();
|
|
183
|
+
await this.createCodecContext(audioSample);
|
|
154
184
|
assert(this.codecContext);
|
|
155
185
|
}
|
|
156
186
|
|
|
@@ -192,9 +222,12 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
|
|
|
192
222
|
this.resamplerInputSampleRate = this.frame.sampleRate;
|
|
193
223
|
|
|
194
224
|
// Resample straight to whatever layout the encoder was opened with, since not every codec accepts
|
|
195
|
-
// the layout that getChannelLayout hands out for a given channel count
|
|
225
|
+
// the layout that getChannelLayout hands out for a given channel count. Same-count input just takes
|
|
226
|
+
// on that layout, since remixing it between two layouts could mix or drop channels.
|
|
196
227
|
const outLayout = this.codecContext.channelLayout;
|
|
197
|
-
const inLayout =
|
|
228
|
+
const inLayout = this.frame.channels === this.codecContext.channels
|
|
229
|
+
? outLayout
|
|
230
|
+
: getChannelLayout(this.frame.channels);
|
|
198
231
|
|
|
199
232
|
const ret = this.resampler.allocSetOpts2(
|
|
200
233
|
outLayout, this.codecContext.sampleFormat, this.codecContext.sampleRate,
|
|
@@ -276,7 +309,8 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
|
|
|
276
309
|
return;
|
|
277
310
|
}
|
|
278
311
|
|
|
279
|
-
const
|
|
312
|
+
const delay = this.codec === 'dts' ? DTS_ENCODER_DELAY : 0;
|
|
313
|
+
const timestamp = (Number(this.packet.pts) - delay) / this.codecContext.sampleRate;
|
|
280
314
|
const duration = Number(this.packet.duration) / this.codecContext.sampleRate;
|
|
281
315
|
|
|
282
316
|
let data: Uint8Array = this.packet.data;
|
|
@@ -371,6 +405,15 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
|
|
|
371
405
|
if (this.resampler) {
|
|
372
406
|
assert(this.resamplerInputSampleRate !== null);
|
|
373
407
|
|
|
408
|
+
const unflushedDelay = UNFLUSHED_ENCODER_DELAYS[this.codec];
|
|
409
|
+
if (unflushedDelay !== undefined) {
|
|
410
|
+
// Push the audio the encoder is still holding back out with silence
|
|
411
|
+
this.resampler.injectSilence(
|
|
412
|
+
Math.ceil((unflushedDelay / this.codecContext.sampleRate) * this.resamplerInputSampleRate),
|
|
413
|
+
);
|
|
414
|
+
await this.pullResampledFrames();
|
|
415
|
+
}
|
|
416
|
+
|
|
374
417
|
const currentOutSamples = this.resampler.getOutSamples(0);
|
|
375
418
|
if (currentOutSamples === 0) {
|
|
376
419
|
break outer; // Clean cut-off point
|
|
@@ -379,6 +422,20 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
|
|
|
379
422
|
const frameSize = this.codecContext.frameSize || FRAME_SIZE_FALLBACK;
|
|
380
423
|
assert(currentOutSamples < frameSize); // Because if it's more, it would've already been retrieved
|
|
381
424
|
|
|
425
|
+
// Lossless codecs would keep silence padding as part of the audio, so they get a short last frame instead
|
|
426
|
+
if (this.codec === 'flac' || this.codec === 'truehd') {
|
|
427
|
+
assert(this.dstFrame);
|
|
428
|
+
assert(this.nextResamplerPts !== null);
|
|
429
|
+
|
|
430
|
+
const samples = await this.resampler.convert(this.dstFrame.data, frameSize, null, 0);
|
|
431
|
+
this.dstFrame.nbSamples = samples;
|
|
432
|
+
this.dstFrame.duration = BigInt(samples);
|
|
433
|
+
this.dstFrame.pts = this.nextResamplerPts;
|
|
434
|
+
await this.sendFrameAndReceivePackets(this.dstFrame);
|
|
435
|
+
|
|
436
|
+
break outer;
|
|
437
|
+
}
|
|
438
|
+
|
|
382
439
|
const inputSamplesNeeded = Math.ceil(
|
|
383
440
|
((frameSize - currentOutSamples) / this.codecContext.sampleRate) * this.resamplerInputSampleRate,
|
|
384
441
|
);
|
|
@@ -413,5 +470,8 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
|
|
|
413
470
|
}
|
|
414
471
|
|
|
415
472
|
const resolveBitrate = (config: AudioEncoderConfig, codec: AudioCodec) => {
|
|
416
|
-
|
|
473
|
+
const bitrate = config.bitrate ?? new Quality('medium')._toAudioBitrate(codec, config.numberOfChannels) ?? 0;
|
|
474
|
+
return codec === 'dts'
|
|
475
|
+
? clampDtsBitrate(bitrate, config.sampleRate, config.numberOfChannels)
|
|
476
|
+
: bitrate;
|
|
417
477
|
};
|
package/src/audio-sample.ts
CHANGED
|
@@ -75,7 +75,7 @@ export class AvFrameAudioSampleResource extends AudioSampleResource {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
getTimestamp(): number {
|
|
78
|
-
return Number(this.frame.pts) / this.frame.timeBase.den;
|
|
78
|
+
return Number(this.frame.pts) * this.frame.timeBase.num / this.frame.timeBase.den;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
close(): void {
|
|
@@ -84,8 +84,10 @@ export class AvFrameAudioSampleResource extends AudioSampleResource {
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
getDataPlane(planeIndex: number): Uint8Array {
|
|
87
|
-
|
|
88
|
-
|
|
87
|
+
// data only holds the first 8 planes, extendedData holds all of them
|
|
88
|
+
const planes = this.frame.extendedData;
|
|
89
|
+
assert(planes && planeIndex < planes.length);
|
|
90
|
+
return toUint8Array(planes[planeIndex]!);
|
|
89
91
|
}
|
|
90
92
|
}
|
|
91
93
|
|
|
@@ -98,9 +100,11 @@ export const copyAudioSampleToAvFrame = (sample: AudioSample, frame: NodeAv.Fram
|
|
|
98
100
|
frame.channelLayout = getChannelLayout(sample.numberOfChannels);
|
|
99
101
|
|
|
100
102
|
frame.allocBuffer();
|
|
101
|
-
assert(frame.data);
|
|
102
103
|
|
|
103
|
-
|
|
104
|
-
|
|
104
|
+
const planes = frame.extendedData;
|
|
105
|
+
assert(planes);
|
|
106
|
+
|
|
107
|
+
for (let i = 0; i < planes.length; i++) {
|
|
108
|
+
sample.copyTo(planes[i]!, { planeIndex: i });
|
|
105
109
|
}
|
|
106
110
|
};
|
package/src/misc.ts
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
import { VideoSamplePixelFormat, MediaCodec } from 'mediabunny';
|
|
10
10
|
import * as NodeAv from 'node-av';
|
|
11
11
|
import { _serverOptions } from './index';
|
|
12
|
+
import { assertNever } from '../../../src/misc';
|
|
12
13
|
|
|
13
14
|
export const CODEC_TO_CODEC_ID: Partial<Record<MediaCodec, NodeAv.AVCodecID>> = {
|
|
14
15
|
avc: NodeAv.AV_CODEC_ID_H264,
|
|
@@ -209,6 +210,7 @@ export const fromPixelFormat = (pixelFormat: VideoSamplePixelFormat) => {
|
|
|
209
210
|
case 'I420P12': return NodeAv.AV_PIX_FMT_YUV420P12LE;
|
|
210
211
|
case 'I420A': return NodeAv.AV_PIX_FMT_YUVA420P;
|
|
211
212
|
case 'I420AP10': return NodeAv.AV_PIX_FMT_YUVA420P10LE;
|
|
213
|
+
case 'I420AP12': return NodeAv.AV_PIX_FMT_NONE; // FFmpeg has no 12-bit 4:2:0 format with alpha :(
|
|
212
214
|
|
|
213
215
|
case 'I422': return NodeAv.AV_PIX_FMT_YUV422P;
|
|
214
216
|
case 'I422P10': return NodeAv.AV_PIX_FMT_YUV422P10LE;
|
|
@@ -231,7 +233,7 @@ export const fromPixelFormat = (pixelFormat: VideoSamplePixelFormat) => {
|
|
|
231
233
|
case 'BGRA': return NodeAv.AV_PIX_FMT_BGRA;
|
|
232
234
|
case 'BGRX': return NodeAv.AV_PIX_FMT_BGR0;
|
|
233
235
|
|
|
234
|
-
default: return
|
|
236
|
+
default: return assertNever(pixelFormat);
|
|
235
237
|
}
|
|
236
238
|
};
|
|
237
239
|
|
package/src/video-decoder.ts
CHANGED
|
@@ -8,7 +8,13 @@
|
|
|
8
8
|
|
|
9
9
|
import { CustomVideoDecoder, VideoCodec, EncodedPacket, VideoSample, type MaybePromise, Rational } from 'mediabunny';
|
|
10
10
|
import * as NodeAv from 'node-av';
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
CODEC_TO_CODEC_ID,
|
|
13
|
+
getHardwareDecoderCodec,
|
|
14
|
+
mapColorPrimaries,
|
|
15
|
+
mapMatrixCoefficients,
|
|
16
|
+
mapTransferCharacteristics,
|
|
17
|
+
} from './misc';
|
|
12
18
|
import { assert, binarySearchLessOrEqual, simplifyRational, toUint8Array } from '../../../src/misc';
|
|
13
19
|
import { AvFrameVideoSampleResource } from './video-sample';
|
|
14
20
|
|
|
@@ -67,6 +73,7 @@ export class NodeAvVideoDecoder extends CustomVideoDecoder {
|
|
|
67
73
|
|
|
68
74
|
const codecContext = new NodeAv.CodecContext();
|
|
69
75
|
codecContext.allocContext3(codec);
|
|
76
|
+
codecContext.threadCount = 0; // Auto
|
|
70
77
|
|
|
71
78
|
this.pixelAspectRatio = simplifyRational({
|
|
72
79
|
num: (this.config.displayAspectWidth ?? this.config.codedWidth ?? 0) * (this.config.codedHeight ?? 0),
|
|
@@ -84,7 +91,9 @@ export class NodeAvVideoDecoder extends CustomVideoDecoder {
|
|
|
84
91
|
|
|
85
92
|
// Honor inferred H.264 reorder buffering even when bitstream restrictions aren't explicitly signaled
|
|
86
93
|
// See https://github.com/Vanilagy/mediabunny/issues/488
|
|
87
|
-
const options =
|
|
94
|
+
const options = this.codec === 'avc'
|
|
95
|
+
? NodeAv.Dictionary.fromObject({ strict: NodeAv.FF_COMPLIANCE_STRICT })
|
|
96
|
+
: null;
|
|
88
97
|
|
|
89
98
|
const ret = await codecContext.open2(codec, options);
|
|
90
99
|
NodeAv.FFmpegError.throwIfError(ret, 'Open codec context');
|
|
@@ -166,6 +175,21 @@ export class NodeAvVideoDecoder extends CustomVideoDecoder {
|
|
|
166
175
|
|
|
167
176
|
this.frame.sampleAspectRatio = new NodeAv.Rational(this.pixelAspectRatio.num, this.pixelAspectRatio.den);
|
|
168
177
|
|
|
178
|
+
// Any config-side color space overrides take precedence
|
|
179
|
+
const colorSpace = this.config.colorSpace;
|
|
180
|
+
if (colorSpace?.primaries) {
|
|
181
|
+
this.frame.colorPrimaries = mapColorPrimaries(colorSpace.primaries) ?? this.frame.colorPrimaries;
|
|
182
|
+
}
|
|
183
|
+
if (colorSpace?.transfer) {
|
|
184
|
+
this.frame.colorTrc = mapTransferCharacteristics(colorSpace.transfer) ?? this.frame.colorTrc;
|
|
185
|
+
}
|
|
186
|
+
if (colorSpace?.matrix) {
|
|
187
|
+
this.frame.colorSpace = mapMatrixCoefficients(colorSpace.matrix) ?? this.frame.colorSpace;
|
|
188
|
+
}
|
|
189
|
+
if (typeof colorSpace?.fullRange === 'boolean') {
|
|
190
|
+
this.frame.colorRange = colorSpace.fullRange ? NodeAv.AVCOL_RANGE_JPEG : NodeAv.AVCOL_RANGE_MPEG;
|
|
191
|
+
}
|
|
192
|
+
|
|
169
193
|
let timestamp = Number(this.frame.pts) / 1e6;
|
|
170
194
|
let duration = Number(this.frame.duration) / 1e6;
|
|
171
195
|
|
|
@@ -221,6 +245,7 @@ export class NodeAvVideoDecoder extends CustomVideoDecoder {
|
|
|
221
245
|
}
|
|
222
246
|
|
|
223
247
|
this.codecContext.flushBuffers();
|
|
248
|
+
this.preciseTimings.length = 0;
|
|
224
249
|
}
|
|
225
250
|
|
|
226
251
|
close(): MaybePromise<void> {
|
package/src/video-encoder.ts
CHANGED
|
@@ -132,6 +132,7 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
132
132
|
|
|
133
133
|
const codecContext = new NodeAv.CodecContext();
|
|
134
134
|
codecContext.allocContext3(this.avCodec);
|
|
135
|
+
codecContext.threadCount = 0; // Auto
|
|
135
136
|
|
|
136
137
|
let pixelFormat = NodeAv.AV_PIX_FMT_YUV420P;
|
|
137
138
|
|
|
@@ -151,6 +152,12 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
151
152
|
NodeAv.AV_PIX_FMT_YUVA444P12LE,
|
|
152
153
|
);
|
|
153
154
|
}
|
|
155
|
+
} else if (
|
|
156
|
+
(this.config.codec === 'ap4h' || this.config.codec === 'ap4x')
|
|
157
|
+
&& this.avCodec.pixelFormats.includes(NodeAv.AV_PIX_FMT_YUV444P10LE)
|
|
158
|
+
) {
|
|
159
|
+
// ProRes 4444 has full-resolution chroma, but the encoder would subsample it given 4:2:2 input
|
|
160
|
+
pixelFormat = NodeAv.AV_PIX_FMT_YUV444P10LE;
|
|
154
161
|
}
|
|
155
162
|
}
|
|
156
163
|
|
|
@@ -189,6 +196,7 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
189
196
|
}
|
|
190
197
|
} else if (this.avCodec.name === 'libx265') {
|
|
191
198
|
codecContext.setOption('x265-params', 'log-level=error');
|
|
199
|
+
codecContext.setOption('forced-idr', '1');
|
|
192
200
|
|
|
193
201
|
if (isRealtime) {
|
|
194
202
|
codecContext.setOption('tune', 'zerolatency');
|
|
@@ -201,6 +209,11 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
201
209
|
} else {
|
|
202
210
|
codecContext.setOption('cpu-used', '8');
|
|
203
211
|
}
|
|
212
|
+
|
|
213
|
+
if (this.config.alpha === 'keep') {
|
|
214
|
+
// libvpx refuses to encode VP8 transparency when alternate reference frames are enabled
|
|
215
|
+
codecContext.setOption('auto-alt-ref', '0');
|
|
216
|
+
}
|
|
204
217
|
} else if (this.avCodec.name === 'libvpx-vp9') {
|
|
205
218
|
codecContext.setOption('deadline', 'realtime');
|
|
206
219
|
|
|
@@ -236,6 +249,11 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
236
249
|
) {
|
|
237
250
|
// Mediabunny uses the quantizer index (0-255), while these encoders expect the 0-63 quantizer scale
|
|
238
251
|
mapped = Math.round(this.quantizer / 4);
|
|
252
|
+
} else if (this.avCodec.name === 'libx264') {
|
|
253
|
+
// x264 makes qp 0 lossless, which switches to the High 4:4:4 Predictive profile. That's not what a
|
|
254
|
+
// quantizer of 0 means elsewhere, and a mid-stream quantizer change would swap profiles under the
|
|
255
|
+
// decoder.
|
|
256
|
+
mapped = Math.max(this.quantizer, 1);
|
|
239
257
|
} else {
|
|
240
258
|
// Everything else lines up with Mediabunny directly
|
|
241
259
|
mapped = this.quantizer;
|
|
@@ -546,6 +564,7 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
546
564
|
colorSpace: null,
|
|
547
565
|
avcType: 1,
|
|
548
566
|
avcCodecInfo: null,
|
|
567
|
+
hevcType: this.config.codec.startsWith('hev1.') ? 'hev1' : 'hvc1',
|
|
549
568
|
hevcCodecInfo: null,
|
|
550
569
|
vp9CodecInfo: null,
|
|
551
570
|
av1CodecInfo: null,
|
|
@@ -622,6 +641,7 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
622
641
|
colorSpace: null,
|
|
623
642
|
avcType: null,
|
|
624
643
|
avcCodecInfo: null,
|
|
644
|
+
hevcType: null,
|
|
625
645
|
hevcCodecInfo: null,
|
|
626
646
|
vp9CodecInfo: null,
|
|
627
647
|
av1CodecInfo: null,
|
|
@@ -640,6 +660,7 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
640
660
|
colorSpace: null,
|
|
641
661
|
avcType: null,
|
|
642
662
|
avcCodecInfo: null,
|
|
663
|
+
hevcType: null,
|
|
643
664
|
hevcCodecInfo: null,
|
|
644
665
|
vp9CodecInfo,
|
|
645
666
|
av1CodecInfo: null,
|
|
@@ -658,6 +679,7 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
658
679
|
colorSpace: null,
|
|
659
680
|
avcType: null,
|
|
660
681
|
avcCodecInfo: null,
|
|
682
|
+
hevcType: null,
|
|
661
683
|
hevcCodecInfo: null,
|
|
662
684
|
vp9CodecInfo: null,
|
|
663
685
|
av1CodecInfo,
|
|
@@ -674,6 +696,7 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
674
696
|
colorSpace: null,
|
|
675
697
|
avcType: null,
|
|
676
698
|
avcCodecInfo: null,
|
|
699
|
+
hevcType: null,
|
|
677
700
|
hevcCodecInfo: null,
|
|
678
701
|
vp9CodecInfo: null,
|
|
679
702
|
av1CodecInfo: null,
|
|
@@ -750,6 +773,7 @@ export class NodeAvVideoEncoder extends CustomVideoEncoder {
|
|
|
750
773
|
}
|
|
751
774
|
|
|
752
775
|
this.packetEmitted = false;
|
|
776
|
+
this.preciseTimings.length = 0;
|
|
753
777
|
}
|
|
754
778
|
|
|
755
779
|
close(): MaybePromise<void> {
|
package/src/video-sample.ts
CHANGED
|
@@ -105,7 +105,10 @@ export class AvFrameVideoSampleResource extends VideoSampleResource {
|
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
getSquarePixelHeight(): number {
|
|
108
|
-
if (
|
|
108
|
+
if (
|
|
109
|
+
this.frame.sampleAspectRatio.num > this.frame.sampleAspectRatio.den
|
|
110
|
+
|| this.frame.sampleAspectRatio.num === 0 // FFmpeg uses 0:1 for an unknown aspect ratio
|
|
111
|
+
) {
|
|
109
112
|
return this.frame.height;
|
|
110
113
|
} else {
|
|
111
114
|
return Math.round(this.frame.height * this.frame.sampleAspectRatio.den / this.frame.sampleAspectRatio.num);
|
|
@@ -204,7 +207,11 @@ export const copyVideoSampleToAvFrame = async (sample: VideoSample, frame: NodeA
|
|
|
204
207
|
|
|
205
208
|
frame.unref();
|
|
206
209
|
|
|
207
|
-
|
|
210
|
+
const widenFrom12To16Bit = sample.format === 'I420AP12'; // Need to widen this because of the missing pixel format
|
|
211
|
+
|
|
212
|
+
frame.format = widenFrom12To16Bit
|
|
213
|
+
? NodeAv.AV_PIX_FMT_YUVA420P16LE
|
|
214
|
+
: fromPixelFormat(sample.format);
|
|
208
215
|
frame.width = sample.codedWidth;
|
|
209
216
|
frame.height = sample.codedHeight;
|
|
210
217
|
frame.sampleAspectRatio = new NodeAv.Rational(
|
|
@@ -229,6 +236,14 @@ export const copyVideoSampleToAvFrame = async (sample: VideoSample, frame: NodeA
|
|
|
229
236
|
}
|
|
230
237
|
|
|
231
238
|
await sample.copyTo(lastBuffer);
|
|
239
|
+
|
|
240
|
+
if (widenFrom12To16Bit) {
|
|
241
|
+
const values = new Uint16Array(lastBuffer.buffer, lastBuffer.byteOffset, lastBuffer.byteLength / 2);
|
|
242
|
+
for (let i = 0; i < values.length; i++) {
|
|
243
|
+
values[i] = (values[i]! << 4) | (values[i]! >> 8);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
232
247
|
frame.fromBuffer(lastBuffer);
|
|
233
248
|
|
|
234
249
|
return lastBuffer;
|
|
@@ -251,8 +266,6 @@ export const transformVideoSample = async (
|
|
|
251
266
|
srcFrame = new NodeAv.Frame();
|
|
252
267
|
srcFrame.alloc();
|
|
253
268
|
srcFrameOwned = true;
|
|
254
|
-
|
|
255
|
-
await copyVideoSampleToAvFrame(sample, srcFrame, null);
|
|
256
269
|
}
|
|
257
270
|
|
|
258
271
|
// Build the filter chain. Order: square-pixel normalize -> rotate -> flip -> crop -> resize-with-fit.
|
|
@@ -294,10 +307,36 @@ export const transformVideoSample = async (
|
|
|
294
307
|
graph.alloc();
|
|
295
308
|
|
|
296
309
|
try {
|
|
310
|
+
if (srcFrameOwned) {
|
|
311
|
+
await copyVideoSampleToAvFrame(sample, srcFrame, null);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (description.alpha === 'discard') {
|
|
315
|
+
// Premultiplying the color by the alpha paints the frame onto black, after which the alpha plane can go
|
|
316
|
+
const formatName = NodeAv.avGetPixFmtName(srcFrame.format as NodeAv.AVPixelFormat);
|
|
317
|
+
assert(formatName);
|
|
318
|
+
|
|
319
|
+
const opaqueFormatName = formatName
|
|
320
|
+
.replace(/^yuva/, 'yuv')
|
|
321
|
+
.replace(/^gbrap/, 'gbrp')
|
|
322
|
+
.replace(/^ya8$/, 'gray')
|
|
323
|
+
.replace(/^ya16/, 'gray16')
|
|
324
|
+
.replace(/^(rgb|bgr)a64/, '$148')
|
|
325
|
+
.replace(/^(rgb|bgr)a/, '$10')
|
|
326
|
+
.replace(/^a(rgb|bgr)/, '0$1');
|
|
327
|
+
|
|
328
|
+
if (opaqueFormatName !== formatName) {
|
|
329
|
+
assert(NodeAv.avGetPixFmtFromName(opaqueFormatName) !== NodeAv.AV_PIX_FMT_NONE);
|
|
330
|
+
chain.splice(-1, 0, 'premultiply=inplace=1', `format=${opaqueFormatName}`);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
297
334
|
const srcArgs = `video_size=${srcFrame.width}x${srcFrame.height}`
|
|
298
335
|
+ `:pix_fmt=${srcFrame.format}`
|
|
299
336
|
+ `:time_base=1/1000000`
|
|
300
|
-
+ `:pixel_aspect=${sample.pixelAspectRatio.num}/${sample.pixelAspectRatio.den}
|
|
337
|
+
+ `:pixel_aspect=${sample.pixelAspectRatio.num}/${sample.pixelAspectRatio.den}`
|
|
338
|
+
+ `:colorspace=${srcFrame.colorSpace}`
|
|
339
|
+
+ `:range=${srcFrame.colorRange}`;
|
|
301
340
|
|
|
302
341
|
const bufferSrc = graph.createFilter(NodeAv.Filter.getByName('buffer')!, 'src', srcArgs);
|
|
303
342
|
const bufferSink = graph.createFilter(NodeAv.Filter.getByName('buffersink')!, 'sink');
|
|
@@ -315,7 +354,8 @@ export const transformVideoSample = async (
|
|
|
315
354
|
const configRet = await graph.config();
|
|
316
355
|
NodeAv.FFmpegError.throwIfError(configRet, 'FilterGraph.config');
|
|
317
356
|
|
|
318
|
-
|
|
357
|
+
// Without KEEP_REF, the graph would take the frame's buffers away from the sample
|
|
358
|
+
const addRet = await bufferSrc.buffersrcAddFrame(srcFrame, NodeAv.AV_BUFFERSRC_FLAG_KEEP_REF);
|
|
319
359
|
NodeAv.FFmpegError.throwIfError(addRet, 'buffersrcAddFrame');
|
|
320
360
|
|
|
321
361
|
// Flush - we only ever push a single frame through this graph.
|
|
@@ -333,6 +373,7 @@ export const transformVideoSample = async (
|
|
|
333
373
|
// Baked in by the filter graph:
|
|
334
374
|
rotation: 0,
|
|
335
375
|
flip: false,
|
|
376
|
+
encodeOptions: sample.encodeOptions,
|
|
336
377
|
});
|
|
337
378
|
} finally {
|
|
338
379
|
graph.free();
|