@mediabunny/server 1.65.2 → 1.66.1
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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"audio-decoder.d.ts","sourceRoot":"","sources":["../../../src/audio-decoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAe,kBAAkB,EAAE,aAAa,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAC3G,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;AAKlC,qBAAa,kBAAmB,SAAQ,kBAAkB;IACzD,KAAK,EAAG,MAAM,CAAC,KAAK,CAAC;IACrB,MAAM,EAAG,MAAM,CAAC,MAAM,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC,YAAY,GAAG,IAAI,CAAQ;WAGhC,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,kBAAkB,GAAG,OAAO;IAY1E,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"audio-decoder.d.ts","sourceRoot":"","sources":["../../../src/audio-decoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAe,kBAAkB,EAAE,aAAa,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAC3G,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;AAKlC,qBAAa,kBAAmB,SAAQ,kBAAkB;IACzD,KAAK,EAAG,MAAM,CAAC,KAAK,CAAC;IACrB,MAAM,EAAG,MAAM,CAAC,MAAM,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC,YAAY,GAAG,IAAI,CAAQ;WAGhC,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,kBAAkB,GAAG,OAAO;IAY1E,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAmCrB,MAAM,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAyBlD,YAAY,CAAC,GAAG,EAAE,MAAM;IAYlB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAqB5B,KAAK,IAAI,YAAY,CAAC,IAAI,CAAC;CAK3B"}
|
|
@@ -76,6 +76,7 @@ class NodeAvAudioDecoder extends mediabunny_1.CustomAudioDecoder {
|
|
|
76
76
|
}
|
|
77
77
|
const codecContext = new NodeAv.CodecContext();
|
|
78
78
|
codecContext.allocContext3(codec);
|
|
79
|
+
codecContext.threadCount = 0; // Auto
|
|
79
80
|
codecContext.sampleRate = this.config.sampleRate;
|
|
80
81
|
codecContext.channelLayout = (0, misc_1.getChannelLayout)(this.config.numberOfChannels);
|
|
81
82
|
codecContext.timeBase = new NodeAv.Rational(1, this.config.sampleRate);
|
|
@@ -22,7 +22,7 @@ export declare class NodeAvAudioEncoder extends CustomAudioEncoder {
|
|
|
22
22
|
adtsHeaderTemplate: AdtsHeaderTemplate | null;
|
|
23
23
|
static supports(codec: AudioCodec, config: AudioEncoderConfig): boolean;
|
|
24
24
|
init(): Promise<void>;
|
|
25
|
-
createCodecContext(): Promise<void>;
|
|
25
|
+
createCodecContext(firstSample: AudioSample | null): Promise<void>;
|
|
26
26
|
encode(audioSample: AudioSample): Promise<void>;
|
|
27
27
|
pullResampledFrames(): Promise<void>;
|
|
28
28
|
sendFrameAndReceivePackets(frame: NodeAv.Frame | null): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"audio-encoder.d.ts","sourceRoot":"","sources":["../../../src/audio-encoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACN,UAAU,EACV,WAAW,EACX,kBAAkB,EAClB,KAAK,YAAY,EAGjB,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;AAKlC,OAAO,EACN,kBAAkB,EAGlB,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"audio-encoder.d.ts","sourceRoot":"","sources":["../../../src/audio-encoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACN,UAAU,EACV,WAAW,EACX,kBAAkB,EAClB,KAAK,YAAY,EAGjB,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;AAKlC,OAAO,EACN,kBAAkB,EAGlB,MAAM,0BAA0B,CAAC;AAuBlC,qBAAa,kBAAmB,SAAQ,kBAAkB;IACzD,KAAK,EAAG,MAAM,CAAC,KAAK,CAAC;IACrB,MAAM,EAAG,MAAM,CAAC,MAAM,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC,YAAY,GAAG,IAAI,CAAQ;IAChD,SAAS,EAAE,MAAM,CAAC,uBAAuB,GAAG,IAAI,CAAQ;IACxD,QAAQ,EAAE,MAAM,CAAC,KAAK,GAAG,IAAI,CAAQ;IACrC,OAAO,EAAG,MAAM,CAAC,KAAK,CAAC;IAEvB,kBAAkB,EAAE,MAAM,GAAG,IAAI,CAAQ;IACzC,wBAAwB,EAAE,MAAM,GAAG,IAAI,CAAQ;IAC/C,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAQ;IACvC,aAAa,UAAS;IACtB,kBAAkB,EAAE,kBAAkB,GAAG,IAAI,CAAQ;WAErC,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,kBAAkB,GAAG,OAAO;IAkC1E,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAsBrB,kBAAkB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI;IA8DlD,MAAM,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAoF/C,mBAAmB;IAuBnB,0BAA0B,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,GAAG,IAAI;IAgB3D,aAAa,CAAC,GAAG,EAAE,MAAM;IA+FnB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAgE5B,KAAK,IAAI,YAAY,CAAC,IAAI,CAAC;CAO3B"}
|
|
@@ -54,7 +54,15 @@ const AAC_SAMPLE_RATES = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050
|
|
|
54
54
|
const OPUS_SAMPLE_RATES = [8000, 12000, 16000, 24000, 48000];
|
|
55
55
|
const MP3_SAMPLE_RATES = [8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000];
|
|
56
56
|
const AC3_SAMPLE_RATES = [32000, 44100, 48000];
|
|
57
|
+
const SUPPORTED_AAC_CHANNEL_COUNTS = [1, 2, 3, 4, 5, 6, 8];
|
|
57
58
|
const FRAME_SIZE_FALLBACK = 1024; // Just 'cause
|
|
59
|
+
const DTS_ENCODER_DELAY = 512; // In samples
|
|
60
|
+
// How many samples these encoders hold back without ever flushing them on their own
|
|
61
|
+
const UNFLUSHED_ENCODER_DELAYS = {
|
|
62
|
+
ac3: 256,
|
|
63
|
+
eac3: 256,
|
|
64
|
+
dts: DTS_ENCODER_DELAY,
|
|
65
|
+
};
|
|
58
66
|
class NodeAvAudioEncoder extends mediabunny_1.CustomAudioEncoder {
|
|
59
67
|
constructor() {
|
|
60
68
|
super(...arguments);
|
|
@@ -69,16 +77,17 @@ class NodeAvAudioEncoder extends mediabunny_1.CustomAudioEncoder {
|
|
|
69
77
|
}
|
|
70
78
|
static supports(codec, config) {
|
|
71
79
|
const { numberOfChannels, sampleRate } = config;
|
|
72
|
-
return (codec === 'aac' && numberOfChannels
|
|
73
|
-
&& AAC_SAMPLE_RATES.includes(sampleRate)) || (
|
|
80
|
+
return (codec === 'aac' && SUPPORTED_AAC_CHANNEL_COUNTS.includes(numberOfChannels)
|
|
81
|
+
&& AAC_SAMPLE_RATES.includes(sampleRate)) || (
|
|
82
|
+
// Without an explicit mapping family, libopus only does up to 8 channels
|
|
83
|
+
codec === 'opus' && numberOfChannels >= 1 && numberOfChannels <= 8
|
|
74
84
|
&& OPUS_SAMPLE_RATES.includes(sampleRate)) || (codec === 'mp3' && numberOfChannels >= 1 && numberOfChannels <= 2
|
|
75
|
-
&& MP3_SAMPLE_RATES.includes(sampleRate)) || (codec === 'vorbis' && numberOfChannels >= 1 && numberOfChannels <=
|
|
76
|
-
&& sampleRate <=
|
|
85
|
+
&& MP3_SAMPLE_RATES.includes(sampleRate)) || (codec === 'vorbis' && numberOfChannels >= 1 && numberOfChannels <= 8
|
|
86
|
+
&& sampleRate <= 50000) || (codec === 'flac' && numberOfChannels >= 1 && numberOfChannels <= 8
|
|
77
87
|
&& sampleRate <= 655350) || (codec === 'ac3' && numberOfChannels >= 1 && numberOfChannels <= 6
|
|
78
|
-
&& AC3_SAMPLE_RATES.includes(sampleRate)) || (codec === 'eac3' && numberOfChannels >= 1 && numberOfChannels <=
|
|
88
|
+
&& AC3_SAMPLE_RATES.includes(sampleRate)) || (codec === 'eac3' && numberOfChannels >= 1 && numberOfChannels <= 6
|
|
79
89
|
&& AC3_SAMPLE_RATES.includes(sampleRate)) || (codec === 'dts' && dts_misc_1.DTS_CHANNEL_COUNTS.includes(numberOfChannels)
|
|
80
|
-
&& dts_misc_1.DTS_SAMPLE_RATES.includes(sampleRate)
|
|
81
|
-
&& (0, dts_misc_1.dtsBitrateFits)(resolveBitrate(config, codec), sampleRate, numberOfChannels)) || (codec === 'truehd' && truehd_misc_1.TRUEHD_CHANNEL_COUNTS.includes(numberOfChannels)
|
|
90
|
+
&& dts_misc_1.DTS_SAMPLE_RATES.includes(sampleRate)) || (codec === 'truehd' && truehd_misc_1.TRUEHD_CHANNEL_COUNTS.includes(numberOfChannels)
|
|
82
91
|
&& truehd_misc_1.TRUEHD_SAMPLE_RATES.includes(sampleRate));
|
|
83
92
|
}
|
|
84
93
|
async init() {
|
|
@@ -93,21 +102,36 @@ class NodeAvAudioEncoder extends mediabunny_1.CustomAudioEncoder {
|
|
|
93
102
|
throw new Error(`Unable to obtain libav codec for '${this.codec}'.`);
|
|
94
103
|
}
|
|
95
104
|
this.avCodec = codec;
|
|
96
|
-
|
|
105
|
+
if (this.codec !== 'flac') {
|
|
106
|
+
// FLAC's bit depth depends on the first sample's format
|
|
107
|
+
await this.createCodecContext(null);
|
|
108
|
+
}
|
|
97
109
|
}
|
|
98
|
-
async createCodecContext() {
|
|
110
|
+
async createCodecContext(firstSample) {
|
|
99
111
|
(0, misc_2.assert)(this.codecContext === null);
|
|
100
112
|
const codecContext = new NodeAv.CodecContext();
|
|
101
113
|
codecContext.allocContext3(this.avCodec);
|
|
114
|
+
codecContext.threadCount = 0; // Auto
|
|
102
115
|
let sampleFormat = NodeAv.AV_SAMPLE_FMT_FLTP;
|
|
103
116
|
if (this.codec === 'truehd') {
|
|
104
117
|
// The first listed format is 16-bit, which would needlessly truncate what's supposed to be lossless
|
|
105
118
|
sampleFormat = NodeAv.AV_SAMPLE_FMT_S32P;
|
|
106
119
|
}
|
|
120
|
+
else if (this.codec === 'flac') {
|
|
121
|
+
(0, misc_2.assert)(firstSample);
|
|
122
|
+
if (firstSample.format.startsWith('u8') || firstSample.format.startsWith('s16')) {
|
|
123
|
+
sampleFormat = NodeAv.AV_SAMPLE_FMT_S16;
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
sampleFormat = NodeAv.AV_SAMPLE_FMT_S32;
|
|
127
|
+
codecContext.bitsPerRawSample = 24;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
107
130
|
else if (this.avCodec.sampleFormats && !this.avCodec.sampleFormats.includes(NodeAv.AV_SAMPLE_FMT_FLTP)) {
|
|
108
131
|
// Use a format that's supported
|
|
109
132
|
sampleFormat = this.avCodec.sampleFormats[0];
|
|
110
133
|
}
|
|
134
|
+
const isAacOrAc3 = this.codec === 'aac' || this.codec === 'ac3' || this.codec === 'eac3';
|
|
111
135
|
codecContext.sampleRate = this.config.sampleRate;
|
|
112
136
|
if (this.codec === 'dts') {
|
|
113
137
|
codecContext.channelLayout = (0, misc_1.getDtsChannelLayout)(this.config.numberOfChannels);
|
|
@@ -115,6 +139,12 @@ class NodeAvAudioEncoder extends mediabunny_1.CustomAudioEncoder {
|
|
|
115
139
|
else if (this.codec === 'truehd') {
|
|
116
140
|
codecContext.channelLayout = (0, misc_1.getTrueHdChannelLayout)(this.config.numberOfChannels);
|
|
117
141
|
}
|
|
142
|
+
else if (isAacOrAc3 && this.config.numberOfChannels === 3) {
|
|
143
|
+
codecContext.channelLayout = NodeAv.AV_CHANNEL_LAYOUT_SURROUND;
|
|
144
|
+
}
|
|
145
|
+
else if (isAacOrAc3 && this.config.numberOfChannels === 5) {
|
|
146
|
+
codecContext.channelLayout = NodeAv.AV_CHANNEL_LAYOUT_5POINT0_BACK;
|
|
147
|
+
}
|
|
118
148
|
else {
|
|
119
149
|
codecContext.channelLayout = (0, misc_1.getChannelLayout)(this.config.numberOfChannels);
|
|
120
150
|
}
|
|
@@ -138,7 +168,7 @@ class NodeAvAudioEncoder extends mediabunny_1.CustomAudioEncoder {
|
|
|
138
168
|
}
|
|
139
169
|
async encode(audioSample) {
|
|
140
170
|
if (this.codecContext === null) {
|
|
141
|
-
await this.createCodecContext();
|
|
171
|
+
await this.createCodecContext(audioSample);
|
|
142
172
|
(0, misc_2.assert)(this.codecContext);
|
|
143
173
|
}
|
|
144
174
|
if (audioSample._data instanceof audio_sample_1.AvFrameAudioSampleResource) {
|
|
@@ -172,9 +202,12 @@ class NodeAvAudioEncoder extends mediabunny_1.CustomAudioEncoder {
|
|
|
172
202
|
this.resampler = new NodeAv.SoftwareResampleContext();
|
|
173
203
|
this.resamplerInputSampleRate = this.frame.sampleRate;
|
|
174
204
|
// Resample straight to whatever layout the encoder was opened with, since not every codec accepts
|
|
175
|
-
// the layout that getChannelLayout hands out for a given channel count
|
|
205
|
+
// the layout that getChannelLayout hands out for a given channel count. Same-count input just takes
|
|
206
|
+
// on that layout, since remixing it between two layouts could mix or drop channels.
|
|
176
207
|
const outLayout = this.codecContext.channelLayout;
|
|
177
|
-
const inLayout =
|
|
208
|
+
const inLayout = this.frame.channels === this.codecContext.channels
|
|
209
|
+
? outLayout
|
|
210
|
+
: (0, misc_1.getChannelLayout)(this.frame.channels);
|
|
178
211
|
const ret = this.resampler.allocSetOpts2(outLayout, this.codecContext.sampleFormat, this.codecContext.sampleRate, inLayout, this.frame.format, this.frame.sampleRate);
|
|
179
212
|
NodeAv.FFmpegError.throwIfError(ret, 'allocSetOpts2');
|
|
180
213
|
const ret2 = this.resampler.init();
|
|
@@ -235,7 +268,8 @@ class NodeAvAudioEncoder extends mediabunny_1.CustomAudioEncoder {
|
|
|
235
268
|
if (!this.packet.data) {
|
|
236
269
|
return;
|
|
237
270
|
}
|
|
238
|
-
const
|
|
271
|
+
const delay = this.codec === 'dts' ? DTS_ENCODER_DELAY : 0;
|
|
272
|
+
const timestamp = (Number(this.packet.pts) - delay) / this.codecContext.sampleRate;
|
|
239
273
|
const duration = Number(this.packet.duration) / this.codecContext.sampleRate;
|
|
240
274
|
let data = this.packet.data;
|
|
241
275
|
let metadata;
|
|
@@ -313,12 +347,29 @@ class NodeAvAudioEncoder extends mediabunny_1.CustomAudioEncoder {
|
|
|
313
347
|
}
|
|
314
348
|
outer: if (this.resampler) {
|
|
315
349
|
(0, misc_2.assert)(this.resamplerInputSampleRate !== null);
|
|
350
|
+
const unflushedDelay = UNFLUSHED_ENCODER_DELAYS[this.codec];
|
|
351
|
+
if (unflushedDelay !== undefined) {
|
|
352
|
+
// Push the audio the encoder is still holding back out with silence
|
|
353
|
+
this.resampler.injectSilence(Math.ceil((unflushedDelay / this.codecContext.sampleRate) * this.resamplerInputSampleRate));
|
|
354
|
+
await this.pullResampledFrames();
|
|
355
|
+
}
|
|
316
356
|
const currentOutSamples = this.resampler.getOutSamples(0);
|
|
317
357
|
if (currentOutSamples === 0) {
|
|
318
358
|
break outer; // Clean cut-off point
|
|
319
359
|
}
|
|
320
360
|
const frameSize = this.codecContext.frameSize || FRAME_SIZE_FALLBACK;
|
|
321
361
|
(0, misc_2.assert)(currentOutSamples < frameSize); // Because if it's more, it would've already been retrieved
|
|
362
|
+
// Lossless codecs would keep silence padding as part of the audio, so they get a short last frame instead
|
|
363
|
+
if (this.codec === 'flac' || this.codec === 'truehd') {
|
|
364
|
+
(0, misc_2.assert)(this.dstFrame);
|
|
365
|
+
(0, misc_2.assert)(this.nextResamplerPts !== null);
|
|
366
|
+
const samples = await this.resampler.convert(this.dstFrame.data, frameSize, null, 0);
|
|
367
|
+
this.dstFrame.nbSamples = samples;
|
|
368
|
+
this.dstFrame.duration = BigInt(samples);
|
|
369
|
+
this.dstFrame.pts = this.nextResamplerPts;
|
|
370
|
+
await this.sendFrameAndReceivePackets(this.dstFrame);
|
|
371
|
+
break outer;
|
|
372
|
+
}
|
|
322
373
|
const inputSamplesNeeded = Math.ceil(((frameSize - currentOutSamples) / this.codecContext.sampleRate) * this.resamplerInputSampleRate);
|
|
323
374
|
this.resampler.injectSilence(inputSamplesNeeded);
|
|
324
375
|
await this.pullResampledFrames();
|
|
@@ -346,5 +397,8 @@ class NodeAvAudioEncoder extends mediabunny_1.CustomAudioEncoder {
|
|
|
346
397
|
}
|
|
347
398
|
exports.NodeAvAudioEncoder = NodeAvAudioEncoder;
|
|
348
399
|
const resolveBitrate = (config, codec) => {
|
|
349
|
-
|
|
400
|
+
const bitrate = config.bitrate ?? new mediabunny_1.Quality('medium')._toAudioBitrate(codec, config.numberOfChannels) ?? 0;
|
|
401
|
+
return codec === 'dts'
|
|
402
|
+
? (0, dts_misc_1.clampDtsBitrate)(bitrate, config.sampleRate, config.numberOfChannels)
|
|
403
|
+
: bitrate;
|
|
350
404
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"audio-sample.d.ts","sourceRoot":"","sources":["../../../src/audio-sample.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAC9D,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;AAIlC;;;;;;;;;;;GAWG;AACH,qBAAa,0BAA2B,SAAQ,mBAAmB;IAIlE;;;OAGG;IACH,IAAI,KAAK,iBAMR;gBAEW,KAAK,EAAE,MAAM,CAAC,KAAK;IAa/B,SAAS,IAAI,iBAAiB;IAU9B,aAAa,IAAI,MAAM;IAIvB,mBAAmB,IAAI,MAAM;IAI7B,iBAAiB,IAAI,MAAM;IAI3B,YAAY,IAAI,MAAM;IAItB,KAAK,IAAI,IAAI;IAKb,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU;
|
|
1
|
+
{"version":3,"file":"audio-sample.d.ts","sourceRoot":"","sources":["../../../src/audio-sample.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAC9D,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;AAIlC;;;;;;;;;;;GAWG;AACH,qBAAa,0BAA2B,SAAQ,mBAAmB;IAIlE;;;OAGG;IACH,IAAI,KAAK,iBAMR;gBAEW,KAAK,EAAE,MAAM,CAAC,KAAK;IAa/B,SAAS,IAAI,iBAAiB;IAU9B,aAAa,IAAI,MAAM;IAIvB,mBAAmB,IAAI,MAAM;IAI7B,iBAAiB,IAAI,MAAM;IAI3B,YAAY,IAAI,MAAM;IAItB,KAAK,IAAI,IAAI;IAKb,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU;CAM5C;AAED,eAAO,MAAM,wBAAwB,GAAI,QAAQ,WAAW,EAAE,OAAO,MAAM,CAAC,KAAK,SAgBhF,CAAC"}
|
|
@@ -96,15 +96,17 @@ class AvFrameAudioSampleResource extends mediabunny_1.AudioSampleResource {
|
|
|
96
96
|
return this.frame.nbSamples;
|
|
97
97
|
}
|
|
98
98
|
getTimestamp() {
|
|
99
|
-
return Number(this.frame.pts) / this.frame.timeBase.den;
|
|
99
|
+
return Number(this.frame.pts) * this.frame.timeBase.num / this.frame.timeBase.den;
|
|
100
100
|
}
|
|
101
101
|
close() {
|
|
102
102
|
this.frame.free();
|
|
103
103
|
this._frame = null;
|
|
104
104
|
}
|
|
105
105
|
getDataPlane(planeIndex) {
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
// data only holds the first 8 planes, extendedData holds all of them
|
|
107
|
+
const planes = this.frame.extendedData;
|
|
108
|
+
(0, misc_2.assert)(planes && planeIndex < planes.length);
|
|
109
|
+
return (0, misc_2.toUint8Array)(planes[planeIndex]);
|
|
108
110
|
}
|
|
109
111
|
}
|
|
110
112
|
exports.AvFrameAudioSampleResource = AvFrameAudioSampleResource;
|
|
@@ -115,9 +117,10 @@ const copyAudioSampleToAvFrame = (sample, frame) => {
|
|
|
115
117
|
frame.sampleRate = sample.sampleRate;
|
|
116
118
|
frame.channelLayout = (0, misc_1.getChannelLayout)(sample.numberOfChannels);
|
|
117
119
|
frame.allocBuffer();
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
120
|
+
const planes = frame.extendedData;
|
|
121
|
+
(0, misc_2.assert)(planes);
|
|
122
|
+
for (let i = 0; i < planes.length; i++) {
|
|
123
|
+
sample.copyTo(planes[i], { planeIndex: i });
|
|
121
124
|
}
|
|
122
125
|
};
|
|
123
126
|
exports.copyAudioSampleToAvFrame = copyAudioSampleToAvFrame;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"misc.d.ts","sourceRoot":"","sources":["../../../src/misc.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAChE,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"misc.d.ts","sourceRoot":"","sources":["../../../src/misc.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAChE,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;AAIlC,eAAO,MAAM,iBAAiB,EAAE,OAAO,CAAC,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,SAAS,CAAC,CAiB3E,CAAC;AAGF,eAAO,MAAM,kBAAkB,QAAO,MAAM,CAAC,eAAe,GAAG,IAU9D,CAAC;AAYF,eAAO,MAAM,uBAAuB,GAAU,SAAS,MAAM,CAAC,SAAS,KAAG,OAAO,CAAC,MAAM,CAAC,KAAK,GAAG,IAAI,CAapG,CAAC;AAGF,eAAO,MAAM,uBAAuB,GAAU,SAAS,MAAM,CAAC,SAAS,KAAG,OAAO,CAAC,MAAM,CAAC,KAAK,GAAG,IAAI,CAapG,CAAC;AAEF,eAAO,MAAM,iBAAiB,GAAI,WAAW,MAAM,mCAUlD,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAAI,WAAW,MAAM,qEAUpD,CAAC;AAEF,eAAO,MAAM,0BAA0B,GAAI,UAAU,MAAM,gDAW1D,CAAC;AAEF,eAAO,MAAM,4BAA4B,GAAI,UAAU,MAAM,4EAW5D,CAAC;AAEF,eAAO,MAAM,qBAAqB,GAAI,QAAQ,MAAM,+BAUnD,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAAI,QAAQ,MAAM,oEAUrD,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,aAAa,MAAM,CAAC,aAAa,YAIjE,CAAC;AAEF,eAAO,MAAM,aAAa,GAAI,mBAAmB,MAAM,CAAC,aAAa,KAAG,sBAAsB,GAAG,IAqChG,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,aAAa,sBAAsB,yBAgClE,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAAI,oBAAoB,MAAM,CAAC,cAAc,KAAG,iBAAiB,GAAG,IAanG,CAAC;AAEF,eAAO,MAAM,qBAAqB,GAAI,cAAc,iBAAiB,KAAG,MAAM,CAAC,cAa9E,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,aAAa,MAAM,KAAG,MAAM,CAAC,aAS7D,CAAC;AAKF;;;GAGG;AACH,eAAO,MAAM,mBAAmB,GAAI,aAAa,MAAM,KAAG,MAAM,CAAC,aAShE,CAAC;AAEF,0FAA0F;AAC1F,eAAO,MAAM,sBAAsB,GAAI,aAAa,MAAM,KAAG,MAAM,CAAC,aAUnE,CAAC"}
|
package/dist/modules/src/misc.js
CHANGED
|
@@ -43,6 +43,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
43
43
|
exports.getTrueHdChannelLayout = exports.getDtsChannelLayout = exports.getChannelLayout = exports.fromAudioSampleFormat = exports.toAudioSampleFormat = exports.fromPixelFormat = exports.toPixelFormat = exports.isRgbPixelFormat = exports.unmapMatrixCoefficients = exports.mapMatrixCoefficients = exports.unmapTransferCharacteristics = exports.mapTransferCharacteristics = exports.unmapColorPrimaries = exports.mapColorPrimaries = exports.getHardwareEncoderCodec = exports.getHardwareDecoderCodec = exports.getHardwareContext = exports.CODEC_TO_CODEC_ID = void 0;
|
|
44
44
|
const NodeAv = __importStar(require("node-av"));
|
|
45
45
|
const index_1 = require("./index");
|
|
46
|
+
const misc_1 = require("../../../src/misc");
|
|
46
47
|
exports.CODEC_TO_CODEC_ID = {
|
|
47
48
|
avc: NodeAv.AV_CODEC_ID_H264,
|
|
48
49
|
hevc: NodeAv.AV_CODEC_ID_HEVC,
|
|
@@ -221,6 +222,7 @@ const fromPixelFormat = (pixelFormat) => {
|
|
|
221
222
|
case 'I420P12': return NodeAv.AV_PIX_FMT_YUV420P12LE;
|
|
222
223
|
case 'I420A': return NodeAv.AV_PIX_FMT_YUVA420P;
|
|
223
224
|
case 'I420AP10': return NodeAv.AV_PIX_FMT_YUVA420P10LE;
|
|
225
|
+
case 'I420AP12': return NodeAv.AV_PIX_FMT_NONE; // FFmpeg has no 12-bit 4:2:0 format with alpha :(
|
|
224
226
|
case 'I422': return NodeAv.AV_PIX_FMT_YUV422P;
|
|
225
227
|
case 'I422P10': return NodeAv.AV_PIX_FMT_YUV422P10LE;
|
|
226
228
|
case 'I422P12': return NodeAv.AV_PIX_FMT_YUV422P12LE;
|
|
@@ -238,7 +240,7 @@ const fromPixelFormat = (pixelFormat) => {
|
|
|
238
240
|
case 'RGBX': return NodeAv.AV_PIX_FMT_RGB0;
|
|
239
241
|
case 'BGRA': return NodeAv.AV_PIX_FMT_BGRA;
|
|
240
242
|
case 'BGRX': return NodeAv.AV_PIX_FMT_BGR0;
|
|
241
|
-
default: return
|
|
243
|
+
default: return (0, misc_1.assertNever)(pixelFormat);
|
|
242
244
|
}
|
|
243
245
|
};
|
|
244
246
|
exports.fromPixelFormat = fromPixelFormat;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"video-decoder.d.ts","sourceRoot":"","sources":["../../../src/video-decoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,kBAAkB,EAAE,UAAU,EAAE,aAAa,EAAe,KAAK,YAAY,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACrH,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"video-decoder.d.ts","sourceRoot":"","sources":["../../../src/video-decoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,kBAAkB,EAAE,UAAU,EAAE,aAAa,EAAe,KAAK,YAAY,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACrH,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;AAWlC,qBAAa,kBAAmB,SAAQ,kBAAkB;IACzD,KAAK,EAAG,MAAM,CAAC,KAAK,CAAC;IACrB,MAAM,EAAG,MAAM,CAAC,MAAM,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC,YAAY,GAAG,IAAI,CAAQ;IAChD,gBAAgB,EAAG,QAAQ,CAAC;IAG5B,cAAc,EAAE;QACf,oBAAoB,EAAE,MAAM,CAAC;QAC7B,SAAS,EAAE,MAAM,CAAC;QAClB,QAAQ,EAAE,MAAM,CAAC;QACjB,gBAAgB,EAAE,OAAO,CAAC;QAC1B,eAAe,EAAE,OAAO,CAAC;KACzB,EAAE,CAAM;WAGO,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,kBAAkB,GAAG,OAAO;IAI1E,IAAI;IAOJ,gBAAgB,CAAC,MAAM,EAAE,aAAa;IAwDtC,MAAM,CAAC,MAAM,EAAE,aAAa;IAqElC,YAAY,CAAC,GAAG,EAAE,MAAM;IAsDlB,KAAK;IAwBX,KAAK,IAAI,YAAY,CAAC,IAAI,CAAC;CAK3B"}
|
|
@@ -89,6 +89,7 @@ class NodeAvVideoDecoder extends mediabunny_1.CustomVideoDecoder {
|
|
|
89
89
|
}
|
|
90
90
|
const codecContext = new NodeAv.CodecContext();
|
|
91
91
|
codecContext.allocContext3(codec);
|
|
92
|
+
codecContext.threadCount = 0; // Auto
|
|
92
93
|
this.pixelAspectRatio = (0, misc_2.simplifyRational)({
|
|
93
94
|
num: (this.config.displayAspectWidth ?? this.config.codedWidth ?? 0) * (this.config.codedHeight ?? 0),
|
|
94
95
|
den: (this.config.displayAspectHeight ?? this.config.codedHeight ?? 0) * (this.config.codedWidth ?? 0) || 1,
|
|
@@ -103,7 +104,9 @@ class NodeAvVideoDecoder extends mediabunny_1.CustomVideoDecoder {
|
|
|
103
104
|
codecContext.sampleAspectRatio = new NodeAv.Rational(this.pixelAspectRatio.num, this.pixelAspectRatio.den);
|
|
104
105
|
// Honor inferred H.264 reorder buffering even when bitstream restrictions aren't explicitly signaled
|
|
105
106
|
// See https://github.com/Vanilagy/mediabunny/issues/488
|
|
106
|
-
const options =
|
|
107
|
+
const options = this.codec === 'avc'
|
|
108
|
+
? NodeAv.Dictionary.fromObject({ strict: NodeAv.FF_COMPLIANCE_STRICT })
|
|
109
|
+
: null;
|
|
107
110
|
const ret = await codecContext.open2(codec, options);
|
|
108
111
|
NodeAv.FFmpegError.throwIfError(ret, 'Open codec context');
|
|
109
112
|
this.codecContext = codecContext;
|
|
@@ -166,6 +169,20 @@ class NodeAvVideoDecoder extends mediabunny_1.CustomVideoDecoder {
|
|
|
166
169
|
receiveFrame(ret) {
|
|
167
170
|
NodeAv.FFmpegError.throwIfError(ret, 'Receive frame');
|
|
168
171
|
this.frame.sampleAspectRatio = new NodeAv.Rational(this.pixelAspectRatio.num, this.pixelAspectRatio.den);
|
|
172
|
+
// Any config-side color space overrides take precedence
|
|
173
|
+
const colorSpace = this.config.colorSpace;
|
|
174
|
+
if (colorSpace?.primaries) {
|
|
175
|
+
this.frame.colorPrimaries = (0, misc_1.mapColorPrimaries)(colorSpace.primaries) ?? this.frame.colorPrimaries;
|
|
176
|
+
}
|
|
177
|
+
if (colorSpace?.transfer) {
|
|
178
|
+
this.frame.colorTrc = (0, misc_1.mapTransferCharacteristics)(colorSpace.transfer) ?? this.frame.colorTrc;
|
|
179
|
+
}
|
|
180
|
+
if (colorSpace?.matrix) {
|
|
181
|
+
this.frame.colorSpace = (0, misc_1.mapMatrixCoefficients)(colorSpace.matrix) ?? this.frame.colorSpace;
|
|
182
|
+
}
|
|
183
|
+
if (typeof colorSpace?.fullRange === 'boolean') {
|
|
184
|
+
this.frame.colorRange = colorSpace.fullRange ? NodeAv.AVCOL_RANGE_JPEG : NodeAv.AVCOL_RANGE_MPEG;
|
|
185
|
+
}
|
|
169
186
|
let timestamp = Number(this.frame.pts) / 1e6;
|
|
170
187
|
let duration = Number(this.frame.duration) / 1e6;
|
|
171
188
|
const preciseTimingIndex = (0, misc_2.binarySearchLessOrEqual)(this.preciseTimings, Number(this.frame.pts), x => x.microsecondTimestamp);
|
|
@@ -208,6 +225,7 @@ class NodeAvVideoDecoder extends mediabunny_1.CustomVideoDecoder {
|
|
|
208
225
|
this.receiveFrame(receiveRet);
|
|
209
226
|
}
|
|
210
227
|
this.codecContext.flushBuffers();
|
|
228
|
+
this.preciseTimings.length = 0;
|
|
211
229
|
}
|
|
212
230
|
close() {
|
|
213
231
|
this.codecContext?.freeContext();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"video-encoder.d.ts","sourceRoot":"","sources":["../../../src/video-encoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACN,kBAAkB,EAClB,KAAK,YAAY,EAEjB,UAAU,EACV,WAAW,EAGX,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;AAoClC,qBAAa,kBAAmB,SAAQ,kBAAkB;IACzD,KAAK,EAAG,MAAM,CAAC,KAAK,CAAC;IACrB,MAAM,EAAG,MAAM,CAAC,MAAM,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC,YAAY,GAAG,IAAI,CAAQ;IAChD,MAAM,EAAE,MAAM,CAAC,oBAAoB,GAAG,IAAI,CAAQ;IAClD,QAAQ,EAAE,MAAM,CAAC,KAAK,GAAG,IAAI,CAAQ;IACrC,OAAO,EAAG,MAAM,CAAC,KAAK,CAAC;IACvB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAQ;IACjC,aAAa,UAAS;IACtB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAQ;IAGhC,cAAc,EAAE;QACf,oBAAoB,EAAE,MAAM,CAAC;QAC7B,SAAS,EAAE,MAAM,CAAC;QAClB,QAAQ,EAAE,MAAM,CAAC;QACjB,gBAAgB,EAAE,OAAO,CAAC;QAC1B,eAAe,EAAE,OAAO,CAAC;KACzB,EAAE,CAAM;WAEO,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,kBAAkB,GAAG,OAAO;IAW1E,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IA6CrB,kBAAkB;
|
|
1
|
+
{"version":3,"file":"video-encoder.d.ts","sourceRoot":"","sources":["../../../src/video-encoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACN,kBAAkB,EAClB,KAAK,YAAY,EAEjB,UAAU,EACV,WAAW,EAGX,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,MAAM,MAAM,SAAS,CAAC;AAoClC,qBAAa,kBAAmB,SAAQ,kBAAkB;IACzD,KAAK,EAAG,MAAM,CAAC,KAAK,CAAC;IACrB,MAAM,EAAG,MAAM,CAAC,MAAM,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC,YAAY,GAAG,IAAI,CAAQ;IAChD,MAAM,EAAE,MAAM,CAAC,oBAAoB,GAAG,IAAI,CAAQ;IAClD,QAAQ,EAAE,MAAM,CAAC,KAAK,GAAG,IAAI,CAAQ;IACrC,OAAO,EAAG,MAAM,CAAC,KAAK,CAAC;IACvB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAQ;IACjC,aAAa,UAAS;IACtB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAQ;IAGhC,cAAc,EAAE;QACf,oBAAoB,EAAE,MAAM,CAAC;QAC7B,SAAS,EAAE,MAAM,CAAC;QAClB,QAAQ,EAAE,MAAM,CAAC;QACjB,gBAAgB,EAAE,OAAO,CAAC;QAC1B,eAAe,EAAE,OAAO,CAAC;KACzB,EAAE,CAAM;WAEO,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,kBAAkB,GAAG,OAAO;IAW1E,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IA6CrB,kBAAkB;IAgNlB,MAAM,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAAC,IAAI,CAAC;IA4JzF,aAAa,CAAC,GAAG,EAAE,MAAM;IAmQnB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IA0B5B,KAAK,IAAI,YAAY,CAAC,IAAI,CAAC;CAO3B"}
|
|
@@ -117,6 +117,7 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
117
117
|
(0, misc_2.assert)(this.codecContext === null);
|
|
118
118
|
const codecContext = new NodeAv.CodecContext();
|
|
119
119
|
codecContext.allocContext3(this.avCodec);
|
|
120
|
+
codecContext.threadCount = 0; // Auto
|
|
120
121
|
let pixelFormat = NodeAv.AV_PIX_FMT_YUV420P;
|
|
121
122
|
if (this.avCodec.pixelFormats) {
|
|
122
123
|
if (!this.avCodec.pixelFormats.includes(NodeAv.AV_PIX_FMT_YUV420P)) {
|
|
@@ -132,6 +133,11 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
132
133
|
pixelFormat = NodeAv.avcodecFindBestPixFmtOfList(this.avCodec.pixelFormats, NodeAv.AV_PIX_FMT_YUVA444P12LE);
|
|
133
134
|
}
|
|
134
135
|
}
|
|
136
|
+
else if ((this.config.codec === 'ap4h' || this.config.codec === 'ap4x')
|
|
137
|
+
&& this.avCodec.pixelFormats.includes(NodeAv.AV_PIX_FMT_YUV444P10LE)) {
|
|
138
|
+
// ProRes 4444 has full-resolution chroma, but the encoder would subsample it given 4:2:2 input
|
|
139
|
+
pixelFormat = NodeAv.AV_PIX_FMT_YUV444P10LE;
|
|
140
|
+
}
|
|
135
141
|
}
|
|
136
142
|
const pixelAspectRatio = (0, misc_2.simplifyRational)({
|
|
137
143
|
num: (this.config.displayWidth ?? this.config.width) * this.config.height,
|
|
@@ -163,6 +169,7 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
163
169
|
}
|
|
164
170
|
else if (this.avCodec.name === 'libx265') {
|
|
165
171
|
codecContext.setOption('x265-params', 'log-level=error');
|
|
172
|
+
codecContext.setOption('forced-idr', '1');
|
|
166
173
|
if (isRealtime) {
|
|
167
174
|
codecContext.setOption('tune', 'zerolatency');
|
|
168
175
|
codecContext.setOption('preset', 'ultrafast');
|
|
@@ -176,6 +183,10 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
176
183
|
else {
|
|
177
184
|
codecContext.setOption('cpu-used', '8');
|
|
178
185
|
}
|
|
186
|
+
if (this.config.alpha === 'keep') {
|
|
187
|
+
// libvpx refuses to encode VP8 transparency when alternate reference frames are enabled
|
|
188
|
+
codecContext.setOption('auto-alt-ref', '0');
|
|
189
|
+
}
|
|
179
190
|
}
|
|
180
191
|
else if (this.avCodec.name === 'libvpx-vp9') {
|
|
181
192
|
codecContext.setOption('deadline', 'realtime');
|
|
@@ -211,6 +222,12 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
211
222
|
// Mediabunny uses the quantizer index (0-255), while these encoders expect the 0-63 quantizer scale
|
|
212
223
|
mapped = Math.round(this.quantizer / 4);
|
|
213
224
|
}
|
|
225
|
+
else if (this.avCodec.name === 'libx264') {
|
|
226
|
+
// x264 makes qp 0 lossless, which switches to the High 4:4:4 Predictive profile. That's not what a
|
|
227
|
+
// quantizer of 0 means elsewhere, and a mid-stream quantizer change would swap profiles under the
|
|
228
|
+
// decoder.
|
|
229
|
+
mapped = Math.max(this.quantizer, 1);
|
|
230
|
+
}
|
|
214
231
|
else {
|
|
215
232
|
// Everything else lines up with Mediabunny directly
|
|
216
233
|
mapped = this.quantizer;
|
|
@@ -480,6 +497,7 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
480
497
|
colorSpace: null,
|
|
481
498
|
avcType: 1,
|
|
482
499
|
avcCodecInfo: null,
|
|
500
|
+
hevcType: this.config.codec.startsWith('hev1.') ? 'hev1' : 'hvc1',
|
|
483
501
|
hevcCodecInfo: null,
|
|
484
502
|
vp9CodecInfo: null,
|
|
485
503
|
av1CodecInfo: null,
|
|
@@ -540,6 +558,7 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
540
558
|
colorSpace: null,
|
|
541
559
|
avcType: null,
|
|
542
560
|
avcCodecInfo: null,
|
|
561
|
+
hevcType: null,
|
|
543
562
|
hevcCodecInfo: null,
|
|
544
563
|
vp9CodecInfo: null,
|
|
545
564
|
av1CodecInfo: null,
|
|
@@ -558,6 +577,7 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
558
577
|
colorSpace: null,
|
|
559
578
|
avcType: null,
|
|
560
579
|
avcCodecInfo: null,
|
|
580
|
+
hevcType: null,
|
|
561
581
|
hevcCodecInfo: null,
|
|
562
582
|
vp9CodecInfo,
|
|
563
583
|
av1CodecInfo: null,
|
|
@@ -576,6 +596,7 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
576
596
|
colorSpace: null,
|
|
577
597
|
avcType: null,
|
|
578
598
|
avcCodecInfo: null,
|
|
599
|
+
hevcType: null,
|
|
579
600
|
hevcCodecInfo: null,
|
|
580
601
|
vp9CodecInfo: null,
|
|
581
602
|
av1CodecInfo,
|
|
@@ -593,6 +614,7 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
593
614
|
colorSpace: null,
|
|
594
615
|
avcType: null,
|
|
595
616
|
avcCodecInfo: null,
|
|
617
|
+
hevcType: null,
|
|
596
618
|
hevcCodecInfo: null,
|
|
597
619
|
vp9CodecInfo: null,
|
|
598
620
|
av1CodecInfo: null,
|
|
@@ -652,6 +674,7 @@ class NodeAvVideoEncoder extends mediabunny_1.CustomVideoEncoder {
|
|
|
652
674
|
// codec context.
|
|
653
675
|
}
|
|
654
676
|
this.packetEmitted = false;
|
|
677
|
+
this.preciseTimings.length = 0;
|
|
655
678
|
}
|
|
656
679
|
close() {
|
|
657
680
|
this.codecContext?.freeContext();
|
|
@@ -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;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;
|
|
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;IAW9B,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,qCA6CjH,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAChC,QAAQ,WAAW,EACnB,aAAa,oCAAoC,KAC/C,OAAO,CAAC,WAAW,GAAG,IAAI,CAkI5B,CAAC"}
|
|
@@ -107,7 +107,9 @@ class AvFrameVideoSampleResource extends mediabunny_1.VideoSampleResource {
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
getSquarePixelHeight() {
|
|
110
|
-
if (this.frame.sampleAspectRatio.num > this.frame.sampleAspectRatio.den
|
|
110
|
+
if (this.frame.sampleAspectRatio.num > this.frame.sampleAspectRatio.den
|
|
111
|
+
|| this.frame.sampleAspectRatio.num === 0 // FFmpeg uses 0:1 for an unknown aspect ratio
|
|
112
|
+
) {
|
|
111
113
|
return this.frame.height;
|
|
112
114
|
}
|
|
113
115
|
else {
|
|
@@ -191,7 +193,10 @@ exports.AvFrameVideoSampleResource = AvFrameVideoSampleResource;
|
|
|
191
193
|
const copyVideoSampleToAvFrame = async (sample, frame, lastBuffer) => {
|
|
192
194
|
(0, misc_1.assert)(sample.format !== null);
|
|
193
195
|
frame.unref();
|
|
194
|
-
|
|
196
|
+
const widenFrom12To16Bit = sample.format === 'I420AP12'; // Need to widen this because of the missing pixel format
|
|
197
|
+
frame.format = widenFrom12To16Bit
|
|
198
|
+
? NodeAv.AV_PIX_FMT_YUVA420P16LE
|
|
199
|
+
: (0, misc_2.fromPixelFormat)(sample.format);
|
|
195
200
|
frame.width = sample.codedWidth;
|
|
196
201
|
frame.height = sample.codedHeight;
|
|
197
202
|
frame.sampleAspectRatio = new NodeAv.Rational(sample.pixelAspectRatio.num, sample.pixelAspectRatio.den);
|
|
@@ -211,6 +216,12 @@ const copyVideoSampleToAvFrame = async (sample, frame, lastBuffer) => {
|
|
|
211
216
|
lastBuffer = Buffer.from({ length: size });
|
|
212
217
|
}
|
|
213
218
|
await sample.copyTo(lastBuffer);
|
|
219
|
+
if (widenFrom12To16Bit) {
|
|
220
|
+
const values = new Uint16Array(lastBuffer.buffer, lastBuffer.byteOffset, lastBuffer.byteLength / 2);
|
|
221
|
+
for (let i = 0; i < values.length; i++) {
|
|
222
|
+
values[i] = (values[i] << 4) | (values[i] >> 8);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
214
225
|
frame.fromBuffer(lastBuffer);
|
|
215
226
|
return lastBuffer;
|
|
216
227
|
};
|
|
@@ -228,7 +239,6 @@ const transformVideoSample = async (sample, description) => {
|
|
|
228
239
|
srcFrame = new NodeAv.Frame();
|
|
229
240
|
srcFrame.alloc();
|
|
230
241
|
srcFrameOwned = true;
|
|
231
|
-
await (0, exports.copyVideoSampleToAvFrame)(sample, srcFrame, null);
|
|
232
242
|
}
|
|
233
243
|
// Build the filter chain. Order: square-pixel normalize -> rotate -> flip -> crop -> resize-with-fit.
|
|
234
244
|
const chain = [];
|
|
@@ -265,10 +275,32 @@ const transformVideoSample = async (sample, description) => {
|
|
|
265
275
|
const graph = new NodeAv.FilterGraph();
|
|
266
276
|
graph.alloc();
|
|
267
277
|
try {
|
|
278
|
+
if (srcFrameOwned) {
|
|
279
|
+
await (0, exports.copyVideoSampleToAvFrame)(sample, srcFrame, null);
|
|
280
|
+
}
|
|
281
|
+
if (description.alpha === 'discard') {
|
|
282
|
+
// Premultiplying the color by the alpha paints the frame onto black, after which the alpha plane can go
|
|
283
|
+
const formatName = NodeAv.avGetPixFmtName(srcFrame.format);
|
|
284
|
+
(0, misc_1.assert)(formatName);
|
|
285
|
+
const opaqueFormatName = formatName
|
|
286
|
+
.replace(/^yuva/, 'yuv')
|
|
287
|
+
.replace(/^gbrap/, 'gbrp')
|
|
288
|
+
.replace(/^ya8$/, 'gray')
|
|
289
|
+
.replace(/^ya16/, 'gray16')
|
|
290
|
+
.replace(/^(rgb|bgr)a64/, '$148')
|
|
291
|
+
.replace(/^(rgb|bgr)a/, '$10')
|
|
292
|
+
.replace(/^a(rgb|bgr)/, '0$1');
|
|
293
|
+
if (opaqueFormatName !== formatName) {
|
|
294
|
+
(0, misc_1.assert)(NodeAv.avGetPixFmtFromName(opaqueFormatName) !== NodeAv.AV_PIX_FMT_NONE);
|
|
295
|
+
chain.splice(-1, 0, 'premultiply=inplace=1', `format=${opaqueFormatName}`);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
268
298
|
const srcArgs = `video_size=${srcFrame.width}x${srcFrame.height}`
|
|
269
299
|
+ `:pix_fmt=${srcFrame.format}`
|
|
270
300
|
+ `:time_base=1/1000000`
|
|
271
|
-
+ `:pixel_aspect=${sample.pixelAspectRatio.num}/${sample.pixelAspectRatio.den}
|
|
301
|
+
+ `:pixel_aspect=${sample.pixelAspectRatio.num}/${sample.pixelAspectRatio.den}`
|
|
302
|
+
+ `:colorspace=${srcFrame.colorSpace}`
|
|
303
|
+
+ `:range=${srcFrame.colorRange}`;
|
|
272
304
|
const bufferSrc = graph.createFilter(NodeAv.Filter.getByName('buffer'), 'src', srcArgs);
|
|
273
305
|
const bufferSink = graph.createFilter(NodeAv.Filter.getByName('buffersink'), 'sink');
|
|
274
306
|
(0, misc_1.assert)(bufferSrc && bufferSink);
|
|
@@ -281,7 +313,8 @@ const transformVideoSample = async (sample, description) => {
|
|
|
281
313
|
NodeAv.FFmpegError.throwIfError(parseRet, 'FilterGraph.parsePtr');
|
|
282
314
|
const configRet = await graph.config();
|
|
283
315
|
NodeAv.FFmpegError.throwIfError(configRet, 'FilterGraph.config');
|
|
284
|
-
|
|
316
|
+
// Without KEEP_REF, the graph would take the frame's buffers away from the sample
|
|
317
|
+
const addRet = await bufferSrc.buffersrcAddFrame(srcFrame, NodeAv.AV_BUFFERSRC_FLAG_KEEP_REF);
|
|
285
318
|
NodeAv.FFmpegError.throwIfError(addRet, 'buffersrcAddFrame');
|
|
286
319
|
// Flush - we only ever push a single frame through this graph.
|
|
287
320
|
await bufferSrc.buffersrcAddFrame(null);
|
|
@@ -295,6 +328,7 @@ const transformVideoSample = async (sample, description) => {
|
|
|
295
328
|
// Baked in by the filter graph:
|
|
296
329
|
rotation: 0,
|
|
297
330
|
flip: false,
|
|
331
|
+
encodeOptions: sample.encodeOptions,
|
|
298
332
|
});
|
|
299
333
|
}
|
|
300
334
|
finally {
|