@mediabunny/server 1.63.0 → 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.63.0",
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.63.0"
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> {
@@ -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];
@@ -75,6 +77,9 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
75
77
  codec === 'dts' && DTS_CHANNEL_COUNTS.includes(numberOfChannels)
76
78
  && DTS_SAMPLE_RATES.includes(sampleRate)
77
79
  && dtsBitrateFits(resolveBitrate(config, codec), sampleRate, numberOfChannels)
80
+ ) || (
81
+ codec === 'truehd' && TRUEHD_CHANNEL_COUNTS.includes(numberOfChannels)
82
+ && TRUEHD_SAMPLE_RATES.includes(sampleRate)
78
83
  );
79
84
  }
80
85
 
@@ -104,15 +109,22 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
104
109
  codecContext.allocContext3(this.avCodec);
105
110
 
106
111
  let sampleFormat = NodeAv.AV_SAMPLE_FMT_FLTP;
107
- 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)) {
108
116
  // Use a format that's supported
109
117
  sampleFormat = this.avCodec.sampleFormats[0]!;
110
118
  }
111
119
 
112
120
  codecContext.sampleRate = this.config.sampleRate;
113
- codecContext.channelLayout = this.codec === 'dts'
114
- ? getDtsChannelLayout(this.config.numberOfChannels)
115
- : 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
+ }
116
128
  codecContext.codecType = NodeAv.AVMEDIA_TYPE_AUDIO;
117
129
  codecContext.codecId = CODEC_TO_CODEC_ID[this.codec]!;
118
130
  codecContext.sampleFormat = sampleFormat;
@@ -124,8 +136,9 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
124
136
  codecContext.rcMaxRate = codecContext.bitRate;
125
137
  }
126
138
 
127
- // libav's DTS encoder is marked experimental, so it refuses to open at the default compliance level
128
- 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'
129
142
  ? NodeAv.Dictionary.fromObject({ strict: -2 })
130
143
  : null;
131
144
 
@@ -340,7 +353,7 @@ export class NodeAvAudioEncoder extends CustomAudioEncoder {
340
353
 
341
354
  const packet = new EncodedPacket(
342
355
  data,
343
- 'key',
356
+ determineAudioPacketType(this.codec, data),
344
357
  timestamp,
345
358
  duration,
346
359
  );
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
+ };