@mediabunny/server 1.62.1 → 1.64.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mediabunny/server",
3
3
  "author": "Vanilagy",
4
- "version": "1.62.1",
4
+ "version": "1.64.0",
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.62.1"
38
+ "@mediabunny/prores": "^1.64.0"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "mediabunny": "^1.45.0"
@@ -26,7 +26,8 @@ export class NodeAvAudioDecoder extends CustomAudioDecoder {
26
26
  || codec === 'flac'
27
27
  || codec === 'ac3'
28
28
  || codec === 'eac3'
29
- || codec === 'dts';
29
+ || codec === 'dts'
30
+ || codec === 'truehd';
30
31
  }
31
32
 
32
33
  async init(): Promise<void> {
@@ -49,6 +50,8 @@ export class NodeAvAudioDecoder extends CustomAudioDecoder {
49
50
  codecContext.sampleRate = this.config.sampleRate;
50
51
  codecContext.channelLayout = getChannelLayout(this.config.numberOfChannels);
51
52
  codecContext.timeBase = new NodeAv.Rational(1, this.config.sampleRate);
53
+ // Without this, leading samples are discarded (like Opus pre-skip) without advancing the frame timestamp
54
+ codecContext.pktTimebase = new NodeAv.Rational(1, this.config.sampleRate);
52
55
  codecContext.codecType = NodeAv.AVMEDIA_TYPE_AUDIO;
53
56
  codecContext.codecId = codecId;
54
57
  codecContext.extraData = this.config.description
@@ -15,8 +15,9 @@ import {
15
15
  EncodedPacket,
16
16
  } from 'mediabunny';
17
17
  import * as NodeAv from 'node-av';
18
- import { CODEC_TO_CODEC_ID, getChannelLayout, getDtsChannelLayout } from './misc';
18
+ import { CODEC_TO_CODEC_ID, getChannelLayout, getDtsChannelLayout, getTrueHdChannelLayout } from './misc';
19
19
  import { assert, toUint8Array } from '../../../src/misc';
20
+ import { determineAudioPacketType } from '../../../src/codec-data';
20
21
  import { copyAudioSampleToAvFrame, AvFrameAudioSampleResource } from './audio-sample';
21
22
  import {
22
23
  AdtsHeaderTemplate,
@@ -24,6 +25,7 @@ import {
24
25
  parseAacAudioSpecificConfig,
25
26
  } from '../../../shared/aac-misc';
26
27
  import { DTS_CHANNEL_COUNTS, DTS_SAMPLE_RATES, dtsBitrateFits } from '../../../shared/dts-misc';
28
+ import { TRUEHD_CHANNEL_COUNTS, TRUEHD_SAMPLE_RATES } from '../../../shared/truehd-misc';
27
29
 
28
30
  const AAC_SAMPLE_RATES
29
31
  = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
@@ -40,8 +42,6 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
40
42
  resampler: NodeAv.SoftwareResampleContext | null = null;
41
43
  dstFrame: NodeAv.Frame | null = null;
42
44
  avCodec!: NodeAv.Codec;
43
- firstExpectedTimestamp: number | null = null;
44
- outputTimestampOffset = 0;
45
45
 
46
46
  inputParametersKey: string | null = null;
47
47
  resamplerInputSampleRate: number | null = null;
@@ -77,6 +77,9 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
77
77
  codec === 'dts' && DTS_CHANNEL_COUNTS.includes(numberOfChannels)
78
78
  && DTS_SAMPLE_RATES.includes(sampleRate)
79
79
  && dtsBitrateFits(resolveBitrate(config, codec), sampleRate, numberOfChannels)
80
+ ) || (
81
+ codec === 'truehd' && TRUEHD_CHANNEL_COUNTS.includes(numberOfChannels)
82
+ && TRUEHD_SAMPLE_RATES.includes(sampleRate)
80
83
  );
81
84
  }
82
85
 
@@ -106,15 +109,22 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
106
109
  codecContext.allocContext3(this.avCodec);
107
110
 
108
111
  let sampleFormat = NodeAv.AV_SAMPLE_FMT_FLTP;
109
- if (this.avCodec.sampleFormats && !this.avCodec.sampleFormats.includes(NodeAv.AV_SAMPLE_FMT_FLTP)) {
112
+ if (this.codec === 'truehd') {
113
+ // The first listed format is 16-bit, which would needlessly truncate what's supposed to be lossless
114
+ sampleFormat = NodeAv.AV_SAMPLE_FMT_S32P;
115
+ } else if (this.avCodec.sampleFormats && !this.avCodec.sampleFormats.includes(NodeAv.AV_SAMPLE_FMT_FLTP)) {
110
116
  // Use a format that's supported
111
117
  sampleFormat = this.avCodec.sampleFormats[0]!;
112
118
  }
113
119
 
114
120
  codecContext.sampleRate = this.config.sampleRate;
115
- codecContext.channelLayout = this.codec === 'dts'
116
- ? getDtsChannelLayout(this.config.numberOfChannels)
117
- : getChannelLayout(this.config.numberOfChannels);
121
+ if (this.codec === 'dts') {
122
+ codecContext.channelLayout = getDtsChannelLayout(this.config.numberOfChannels);
123
+ } else if (this.codec === 'truehd') {
124
+ codecContext.channelLayout = getTrueHdChannelLayout(this.config.numberOfChannels);
125
+ } else {
126
+ codecContext.channelLayout = getChannelLayout(this.config.numberOfChannels);
127
+ }
118
128
  codecContext.codecType = NodeAv.AVMEDIA_TYPE_AUDIO;
119
129
  codecContext.codecId = CODEC_TO_CODEC_ID[this.codec]!;
120
130
  codecContext.sampleFormat = sampleFormat;
@@ -126,8 +136,9 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
126
136
  codecContext.rcMaxRate = codecContext.bitRate;
127
137
  }
128
138
 
129
- // libav's DTS encoder is marked experimental, so it refuses to open at the default compliance level
130
- const options = this.codec === 'dts'
139
+ // libav's DTS and TrueHD encoders are marked experimental, so they refuse to open at the default compliance
140
+ // level
141
+ const options = this.codec === 'dts' || this.codec === 'truehd'
131
142
  ? NodeAv.Dictionary.fromObject({ strict: -2 })
132
143
  : null;
133
144
 
@@ -143,8 +154,6 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
143
154
  assert(this.codecContext);
144
155
  }
145
156
 
146
- this.firstExpectedTimestamp ??= audioSample.timestamp;
147
-
148
157
  if (audioSample._data instanceof AvFrameAudioSampleResource) {
149
158
  // Release any buffers still referenced from the previous encode before reffing the new frame, otherwise
150
159
  // av_frame_ref leaks them
@@ -261,14 +270,13 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
261
270
 
262
271
  receivePacket(ret: number) {
263
272
  assert(this.codecContext);
264
- assert(this.firstExpectedTimestamp !== null);
265
273
  NodeAv.FFmpegError.throwIfError(ret, 'Receive packet');
266
274
 
267
275
  if (!this.packet.data) {
268
276
  return;
269
277
  }
270
278
 
271
- let timestamp = Number(this.packet.pts) / this.codecContext.sampleRate;
279
+ const timestamp = Number(this.packet.pts) / this.codecContext.sampleRate;
272
280
  const duration = Number(this.packet.duration) / this.codecContext.sampleRate;
273
281
 
274
282
  let data: Uint8Array = this.packet.data;
@@ -277,11 +285,6 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
277
285
  if (this.packetEmitted) {
278
286
  metadata = {};
279
287
  } else {
280
- // To compensate for any negative timestamp things that FFmpeg might do. It does these for a reason, to
281
- // indicate encoder delay, but the notion of this is not yet supported in Mediabunny. Yes, this technically
282
- // introduces audio sync drift.
283
- this.outputTimestampOffset = Math.max(this.firstExpectedTimestamp - timestamp, 0);
284
-
285
288
  const codecString = this.config.codec;
286
289
  let description = this.codecContext.extraData
287
290
  ? toUint8Array(this.codecContext.extraData)
@@ -348,11 +351,9 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
348
351
  data = final;
349
352
  }
350
353
 
351
- timestamp += this.outputTimestampOffset;
352
-
353
354
  const packet = new EncodedPacket(
354
355
  data,
355
- 'key',
356
+ determineAudioPacketType(this.codec, data),
356
357
  timestamp,
357
358
  duration,
358
359
  );
@@ -391,8 +392,6 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
391
392
  this.codecContext.freeContext();
392
393
  this.codecContext = null;
393
394
  this.packetEmitted = false;
394
- this.firstExpectedTimestamp = null;
395
- this.outputTimestampOffset = 0;
396
395
  this.adtsHeaderTemplate = null;
397
396
 
398
397
  this.resampler?.free();
package/src/misc.ts CHANGED
@@ -26,6 +26,7 @@ export const CODEC_TO_CODEC_ID: Partial<Record<MediaCodec, NodeAv.AVCodecID>> =
26
26
  ac3: NodeAv.AV_CODEC_ID_AC3,
27
27
  eac3: NodeAv.AV_CODEC_ID_EAC3,
28
28
  dts: NodeAv.AV_CODEC_ID_DTS,
29
+ truehd: NodeAv.AV_CODEC_ID_TRUEHD,
29
30
  };
30
31
 
31
32
  let cachedHardwareContext: NodeAv.HardwareContext | null | undefined = undefined;
@@ -292,3 +293,16 @@ export const getDtsChannelLayout = (numChannels: number): NodeAv.ChannelLayout =
292
293
  default: return { nbChannels: numChannels, order: NodeAv.AV_CHANNEL_ORDER_UNSPEC, mask: 0n };
293
294
  }
294
295
  };
296
+
297
+ /** The TrueHD encoder only accepts a fixed set of layouts, so it gets its own mapping. */
298
+ export const getTrueHdChannelLayout = (numChannels: number): NodeAv.ChannelLayout => {
299
+ switch (numChannels) {
300
+ case 1: return NodeAv.AV_CHANNEL_LAYOUT_MONO;
301
+ case 2: return NodeAv.AV_CHANNEL_LAYOUT_STEREO;
302
+ case 3: return NodeAv.AV_CHANNEL_LAYOUT_SURROUND;
303
+ case 4: return NodeAv.AV_CHANNEL_LAYOUT_4POINT0;
304
+ case 5: return NodeAv.AV_CHANNEL_LAYOUT_5POINT0;
305
+ case 6: return NodeAv.AV_CHANNEL_LAYOUT_5POINT1;
306
+ default: return { nbChannels: numChannels, order: NodeAv.AV_CHANNEL_ORDER_UNSPEC, mask: 0n };
307
+ }
308
+ };