@torrent-tv/proxy 2.107.63 → 2.107.65
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 +1 -1
- package/services/media/container/ElementaryAddresses.js +46 -0
- package/services/media/container/ac3-frame.js +5 -6
- package/services/media/container/avc-picture-order.js +2 -2
- package/services/media/container/bitstream.js +180 -0
- package/services/media/container/dts-frame.js +29 -13
- package/services/media/container/h264-configuration.js +4 -34
- package/services/media/container/h264-elementary-index.js +24 -51
- package/services/media/container/hevc-configuration.js +9 -25
- package/services/media/container/hevc-elementary-index.js +7 -11
- package/services/media/container/hevc-picture-order.js +3 -18
- package/services/media/container/latm-frame.js +11 -54
- package/services/media/container/mpeg-elementary-index.js +12 -25
- package/services/media/container/mpeg-video-configuration.js +12 -14
- package/services/media/container/mpeg4-picture-timing.js +4 -4
- package/services/media/tracks/audio-bound.js +10 -52
package/package.json
CHANGED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the bytes of one elementary stream came from, and the PES stamps that arrived with them. A transport
|
|
3
|
+
* stream is walked once and its packets discarded; this keeps just enough to give every access unit or audio
|
|
4
|
+
* frame the exact source ranges of its bytes and the last stamp at or before its first byte.
|
|
5
|
+
*/
|
|
6
|
+
export class ElementaryAddresses {
|
|
7
|
+
#mappings = [];
|
|
8
|
+
#stamps = [];
|
|
9
|
+
#incomplete;
|
|
10
|
+
|
|
11
|
+
/** @param {string} incomplete - The error message when some byte of a range has no source address. */
|
|
12
|
+
constructor(incomplete) {
|
|
13
|
+
this.#incomplete = incomplete;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Bytes `from` up to `from + length` of the elementary stream sit at `sourceStart` of the file. */
|
|
17
|
+
map(from, length, sourceStart) {
|
|
18
|
+
this.#mappings.push({ from, to: from + length, sourceStart });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A stamp that arrived with the byte at elementary position `position`. */
|
|
22
|
+
stamp(position, pts, dts = pts) {
|
|
23
|
+
this.#stamps.push({ position, pts, dts });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** @returns {{ position: number, pts: number, dts: number } | null} the last stamp at or before `position`, consumed with the ones before it */
|
|
27
|
+
stampAt(position) {
|
|
28
|
+
let latest = null;
|
|
29
|
+
while (this.#stamps[0]?.position <= position) latest = this.#stamps.shift();
|
|
30
|
+
return latest;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @returns {Array<[number, number]>} the inclusive source ranges of elementary bytes `from` up to `to` */
|
|
34
|
+
ranges(from, to) {
|
|
35
|
+
const ranges = this.#mappings.filter(mapping => mapping.from < to && mapping.to > from).map(mapping => [
|
|
36
|
+
mapping.sourceStart + Math.max(from, mapping.from) - mapping.from,
|
|
37
|
+
mapping.sourceStart + Math.min(to, mapping.to) - mapping.from - 1]);
|
|
38
|
+
if (ranges.reduce((sum, [a, b]) => sum + b - a + 1, 0) !== to - from) throw new Error(this.#incomplete);
|
|
39
|
+
return ranges;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Forget the addresses of every byte before `end`: nothing will ask for them again. */
|
|
43
|
+
release(end) {
|
|
44
|
+
this.#mappings = this.#mappings.filter(mapping => mapping.to > end);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -1,14 +1,13 @@
|
|
|
1
|
+
import { Bits } from "./bitstream.js";
|
|
2
|
+
|
|
1
3
|
/** ATSC A/52 sync information gives exact frame length and sample count. */
|
|
2
4
|
export function ac3Frame(bytes) {
|
|
3
5
|
if (bytes.length < 7 || bytes[0] !== 0x0b || bytes[1] !== 0x77) throw new Error("AC-3 frame synchronization is invalid.");
|
|
4
6
|
const bitstreamId = bytes[5] >> 3;
|
|
5
7
|
if (bitstreamId > 16) throw new Error("AC-3 bitstream version is invalid.");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
for (let bit = 0; bit < count; bit++, position++) value = (value << 1) | ((bytes[position >> 3] >> (7 - (position & 7))) & 1);
|
|
10
|
-
return value;
|
|
11
|
-
};
|
|
8
|
+
const bits = new Bits(bytes, "AC-3 frame header");
|
|
9
|
+
bits.skip(16);
|
|
10
|
+
const read = count => bits.uint(count);
|
|
12
11
|
let sampleRate, size, channelMode, lfe, samples, codecId;
|
|
13
12
|
const frequencies = [48000, 44100, 32000];
|
|
14
13
|
if (bitstreamId <= 10) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Bits,
|
|
1
|
+
import { Bits, rbsp } from "./bitstream.js";
|
|
2
2
|
|
|
3
3
|
/** AVC frame picture-order counts, retaining reference and frame-number wraps. */
|
|
4
4
|
export class AvcPictureOrder {
|
|
@@ -15,7 +15,7 @@ export class AvcPictureOrder {
|
|
|
15
15
|
(params.type === 1 && !Array.isArray(params.referenceOffsets))) {
|
|
16
16
|
throw new Error("AVC missing timestamps require supported frame picture-order declarations.");
|
|
17
17
|
}
|
|
18
|
-
const bits = new Bits(
|
|
18
|
+
const bits = new Bits(rbsp(nal, 1), "AVC slice header");
|
|
19
19
|
bits.ue(); bits.ue(); bits.ue();
|
|
20
20
|
const frameNumber = bits.uint(params.frameNumberBits);
|
|
21
21
|
const idr = (nal[0] & 31) === 5;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file How this proxy reads the bits of a video or audio elementary stream:
|
|
3
|
+
* one bounded MSB-first reader, one emulation-prevention rule, one AAC
|
|
4
|
+
* sampling-frequency table and one AudioSpecificConfig parser. Every parser of
|
|
5
|
+
* a bitstream (AVC, HEVC, MPEG video, DTS, AC-3, AAC/LATM) reads through this
|
|
6
|
+
* file, so a stream is refused or accepted the same way wherever it is read.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** ISO/IEC 14496-3 Table 1.18: samplingFrequencyIndex to Hz. 0xF is an explicit 24-bit rate. */
|
|
10
|
+
export const AAC_SAMPLE_RATES = Object.freeze([96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350]);
|
|
11
|
+
|
|
12
|
+
/** ISO/IEC 14496-3 Table 1.19: channelConfiguration to channels; 0 is a program configuration element. */
|
|
13
|
+
const AAC_CHANNELS = Object.freeze([null, 1, 2, 3, 4, 5, 6, 8]);
|
|
14
|
+
|
|
15
|
+
/** A bounded MSB-first bit reader. A read past the end throws under `label`. */
|
|
16
|
+
export class Bits {
|
|
17
|
+
at = 0;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {Buffer | Uint8Array} bytes
|
|
21
|
+
* @param {string} label - Names what is read in every error ("HEVC SPS" gives "HEVC SPS bits are truncated.").
|
|
22
|
+
*/
|
|
23
|
+
constructor(bytes, label) {
|
|
24
|
+
this.bytes = bytes;
|
|
25
|
+
this.label = label;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** @param {number} count - 0 to 32 bits. @returns {number} the unsigned value */
|
|
29
|
+
uint(count) {
|
|
30
|
+
if (!(count >= 0 && count <= 32) || this.at + count > this.bytes.length * 8) throw new Error(`${this.label} bits are truncated.`);
|
|
31
|
+
let value = 0;
|
|
32
|
+
for (let index = 0; index < count; index++, this.at++) value = value * 2 + ((this.bytes[this.at >> 3] >> (7 - (this.at & 7))) & 1);
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** @param {number} count - Any number of bits to pass over. */
|
|
37
|
+
skip(count) {
|
|
38
|
+
if (!(count >= 0) || this.at + count > this.bytes.length * 8) throw new Error(`${this.label} bits are truncated.`);
|
|
39
|
+
this.at += count;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @returns {number} an unsigned Exp-Golomb value */
|
|
43
|
+
ue() {
|
|
44
|
+
let zeroes = 0;
|
|
45
|
+
while (!this.uint(1)) if (++zeroes > 31) throw new Error(`${this.label} Exp-Golomb value is invalid.`);
|
|
46
|
+
return 2 ** zeroes - 1 + this.uint(zeroes);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @returns {number} a signed Exp-Golomb value */
|
|
50
|
+
se() {
|
|
51
|
+
const code = this.ue();
|
|
52
|
+
return code & 1 ? (code + 1) / 2 : -code / 2;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The bits from `from` to `to` as bytes, MSB first and zero padded; the read position is left where it was.
|
|
57
|
+
* @returns {Buffer}
|
|
58
|
+
*/
|
|
59
|
+
copy(from, to) {
|
|
60
|
+
const reader = new Bits(this.bytes, this.label);
|
|
61
|
+
reader.skip(from);
|
|
62
|
+
const bytes = Buffer.alloc(Math.ceil((to - from) / 8));
|
|
63
|
+
for (let done = 0; done < to - from; done += 8) {
|
|
64
|
+
const width = Math.min(8, to - from - done);
|
|
65
|
+
bytes[done >> 3] = reader.uint(width) << (8 - width);
|
|
66
|
+
}
|
|
67
|
+
return bytes;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The emulation-prevention rule, one byte at a time: a 0x03 after two zero bytes is dropped whatever follows
|
|
73
|
+
* it. H.264 §7.4.1 forbids 0x000003 before a byte above 0x03, but encoders write it (LostFilm's LostCoder)
|
|
74
|
+
* and decoders drop it (ffmpeg's ff_h2645_extract_rbsp), so refusing it refuses files every player plays.
|
|
75
|
+
*/
|
|
76
|
+
export class EmulationPrevention {
|
|
77
|
+
#zeroes = 0;
|
|
78
|
+
|
|
79
|
+
/** @param {number} byte @returns {boolean} whether the byte belongs to the RBSP (false: it is the 0x03 to drop) */
|
|
80
|
+
keeps(byte) {
|
|
81
|
+
if (this.#zeroes >= 2 && byte === 3) {
|
|
82
|
+
this.#zeroes = 0;
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
this.#zeroes = byte === 0 ? this.#zeroes + 1 : 0;
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The raw byte sequence payload of a NAL unit: emulation prevention bytes removed, never throws.
|
|
92
|
+
* @param {Buffer} nal
|
|
93
|
+
* @param {number} start - Where the payload begins: 1 after an AVC header, 2 after an HEVC header.
|
|
94
|
+
* @returns {Buffer} a new buffer; `nal` is left alone
|
|
95
|
+
*/
|
|
96
|
+
export function rbsp(nal, start) {
|
|
97
|
+
const bytes = Buffer.allocUnsafe(Math.max(0, nal.length - start));
|
|
98
|
+
const rule = new EmulationPrevention();
|
|
99
|
+
let length = 0;
|
|
100
|
+
for (let at = start; at < nal.length; at++) if (rule.keeps(nal[at])) bytes[length++] = nal[at];
|
|
101
|
+
return bytes.subarray(0, length);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Read an AudioSpecificConfig (ISO/IEC 14496-3 §1.6.2.1) from the current position, as the one reading of it:
|
|
106
|
+
* explicit SBR and parametric stereo, and the program configuration element of channelConfiguration 0.
|
|
107
|
+
* Given a `Bits` it reads from where that stands, as a LATM stream does, and truncation throws under its label.
|
|
108
|
+
* Given bytes it answers null when they are no readable configuration.
|
|
109
|
+
*
|
|
110
|
+
* `objectType` is the type as first declared (5 SBR, 29 PS, else the codec's own) and `audioObjectType` the
|
|
111
|
+
* codec underneath; `sampleRate` is the output rate and `coreSampleRate` the one a frame lasts by. A
|
|
112
|
+
* configuration this proxy cannot describe (a codec other than AAC Main/LC/SSR/LTP, no rate, a channel
|
|
113
|
+
* configuration above 7) answers `supported: false` with only what was read before it was told so.
|
|
114
|
+
*
|
|
115
|
+
* @param {Bits | Buffer} source
|
|
116
|
+
* @returns {null | { supported: boolean, objectType: number, audioObjectType: number, sampleRate: number | undefined,
|
|
117
|
+
* coreSampleRate: number | undefined, channelConfiguration: number, channels: number | null,
|
|
118
|
+
* frameLength: number | null, extensionVersion: number }}
|
|
119
|
+
*/
|
|
120
|
+
export function parseAudioSpecificConfig(source) {
|
|
121
|
+
if (source instanceof Bits) return readConfig(source);
|
|
122
|
+
if (!Buffer.isBuffer(source) || source.length < 2) return null;
|
|
123
|
+
try { return readConfig(new Bits(source, "AudioSpecificConfig")); }
|
|
124
|
+
catch { return null; }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function readConfig(bits) {
|
|
128
|
+
const start = bits.at;
|
|
129
|
+
const objectType = audioObjectType(bits);
|
|
130
|
+
let type = objectType;
|
|
131
|
+
const coreSampleRate = audioRate(bits);
|
|
132
|
+
const channelConfiguration = bits.uint(4);
|
|
133
|
+
let sampleRate = coreSampleRate;
|
|
134
|
+
if (type === 5 || type === 29) {
|
|
135
|
+
sampleRate = audioRate(bits);
|
|
136
|
+
type = audioObjectType(bits);
|
|
137
|
+
}
|
|
138
|
+
const result = { supported: false, objectType, audioObjectType: type, sampleRate, coreSampleRate, channelConfiguration,
|
|
139
|
+
channels: AAC_CHANNELS[channelConfiguration] ?? null, frameLength: null, extensionVersion: 0 };
|
|
140
|
+
if (![1, 2, 3, 4].includes(type) || !(coreSampleRate > 0) || !(sampleRate > 0) || channelConfiguration > 7) return result;
|
|
141
|
+
const shortFrame = bits.uint(1);
|
|
142
|
+
if (bits.uint(1)) bits.uint(14);
|
|
143
|
+
const extension = bits.uint(1);
|
|
144
|
+
const channels = channelConfiguration ? result.channels : programChannels(bits, start, coreSampleRate);
|
|
145
|
+
if (extension) result.extensionVersion = bits.uint(1);
|
|
146
|
+
return { ...result, supported: true, frameLength: shortFrame ? 960 : 1024,
|
|
147
|
+
channels: objectType === 29 && channels === 1 ? 2 : channels };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function audioObjectType(bits) {
|
|
151
|
+
const type = bits.uint(5);
|
|
152
|
+
return type === 31 ? 32 + bits.uint(6) : type;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function audioRate(bits) {
|
|
156
|
+
const index = bits.uint(4);
|
|
157
|
+
return index === 15 ? bits.uint(24) : AAC_SAMPLE_RATES[index];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The program configuration element declares every single, paired and LFE channel. */
|
|
161
|
+
function programChannels(bits, start, sampleRate) {
|
|
162
|
+
bits.skip(6);
|
|
163
|
+
if (AAC_SAMPLE_RATES[bits.uint(4)] !== sampleRate) throw new Error("AAC program sample rate differs from its decoder declaration.");
|
|
164
|
+
const front = bits.uint(4), side = bits.uint(4), back = bits.uint(4), lfe = bits.uint(2),
|
|
165
|
+
associated = bits.uint(3), coupled = bits.uint(4);
|
|
166
|
+
if (bits.uint(1)) bits.skip(4);
|
|
167
|
+
if (bits.uint(1)) bits.skip(4);
|
|
168
|
+
if (bits.uint(1)) bits.skip(3);
|
|
169
|
+
let channels = lfe;
|
|
170
|
+
for (let index = 0; index < front + side + back; index++) {
|
|
171
|
+
channels += bits.uint(1) ? 2 : 1;
|
|
172
|
+
bits.skip(4);
|
|
173
|
+
}
|
|
174
|
+
bits.skip(4 * (lfe + associated));
|
|
175
|
+
bits.skip(5 * coupled);
|
|
176
|
+
bits.skip((8 - ((bits.at - start) % 8)) % 8);
|
|
177
|
+
bits.skip(bits.uint(8) * 8);
|
|
178
|
+
if (!channels) throw new Error("AAC program declares no output channels.");
|
|
179
|
+
return channels;
|
|
180
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Bits } from "./bitstream.js";
|
|
2
|
+
|
|
1
3
|
/** DTS core headers declare physical frame length and PCM block duration. */
|
|
2
4
|
export function dtsFrame(bytes) {
|
|
3
5
|
if (bytes.length < 24) throw new Error("DTS core header is truncated.");
|
|
@@ -7,19 +9,9 @@ export function dtsFrame(bytes) {
|
|
|
7
9
|
if (![0x7ffe8001, 0xfe7f0180, 0x1fffe800, 0xff1f00e8].includes(sync)) {
|
|
8
10
|
throw new Error("DTS core synchronization is invalid.");
|
|
9
11
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
let byte = physical >> 3;
|
|
14
|
-
if (little) byte ^= 1;
|
|
15
|
-
if (byte >= bytes.length) throw new Error("DTS core header is truncated.");
|
|
16
|
-
return (bytes[byte] >> (7 - (physical & 7))) & 1;
|
|
17
|
-
};
|
|
18
|
-
const read = count => {
|
|
19
|
-
let value = 0;
|
|
20
|
-
for (let at = 0; at < count; at++) value = value * 2 + bit(position++);
|
|
21
|
-
return value;
|
|
22
|
-
};
|
|
12
|
+
const bits = new Bits(logicalBytes(bytes.subarray(0, 24), packed, little), "DTS core header");
|
|
13
|
+
bits.skip(32);
|
|
14
|
+
const read = count => bits.uint(count);
|
|
23
15
|
read(1);
|
|
24
16
|
if (read(5) !== 31) throw new Error("DTS deficit sample count is invalid.");
|
|
25
17
|
const crc = read(1);
|
|
@@ -49,3 +41,27 @@ export function dtsFrame(bytes) {
|
|
|
49
41
|
return { size: packed ? Math.ceil(logicalSize * 8 / 14) * 2 : logicalSize,
|
|
50
42
|
sampleRate, channels: channels + Number(lfe > 0), duration: blocks * 32 / sampleRate, codecId: "dts" };
|
|
51
43
|
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The header as a plain big-endian bit string: bytes swapped when the stream is little-endian, and the
|
|
47
|
+
* 14-bit words of a packed stream (two padding bits in each 16) joined end to end.
|
|
48
|
+
*/
|
|
49
|
+
function logicalBytes(bytes, packed, little) {
|
|
50
|
+
const ordered = Buffer.from(bytes.subarray(0, bytes.length - (bytes.length & 1)));
|
|
51
|
+
if (little) ordered.swap16();
|
|
52
|
+
if (!packed) return ordered;
|
|
53
|
+
const words = new Bits(ordered, "DTS core header");
|
|
54
|
+
const joined = [];
|
|
55
|
+
let held = 0, count = 0;
|
|
56
|
+
while (words.at < ordered.length * 8) {
|
|
57
|
+
words.skip(2);
|
|
58
|
+
held = held * 16384 + words.uint(14);
|
|
59
|
+
count += 14;
|
|
60
|
+
while (count >= 8) {
|
|
61
|
+
count -= 8;
|
|
62
|
+
joined.push(Math.floor(held / 2 ** count));
|
|
63
|
+
held %= 2 ** count;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return Buffer.from(joined);
|
|
67
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Bits, rbsp } from "./bitstream.js";
|
|
2
|
+
|
|
1
3
|
/** H.264 SPS facts required to bound decode reordering and describe frames. */
|
|
2
4
|
export function h264Configuration(avcc) {
|
|
3
5
|
if (!Buffer.isBuffer(avcc) || avcc.length < 7 || avcc[0] !== 1) throw new Error("AVC decoder configuration is invalid.");
|
|
@@ -21,7 +23,7 @@ export function h264Configuration(avcc) {
|
|
|
21
23
|
|
|
22
24
|
function readSps(nal, includeOrder = false) {
|
|
23
25
|
if ((nal[0] & 31) !== 7 || nal[0] & 128) throw new Error("AVC configuration entry is not an SPS.");
|
|
24
|
-
const bits = new Bits(
|
|
26
|
+
const bits = new Bits(rbsp(nal, 1), "AVC SPS");
|
|
25
27
|
const profile = bits.uint(8), constraints = bits.uint(8), level = bits.uint(8);
|
|
26
28
|
bits.ue();
|
|
27
29
|
let chroma = 1, separate = 0, depth = 8;
|
|
@@ -117,27 +119,11 @@ function readSps(nal, includeOrder = false) {
|
|
|
117
119
|
|
|
118
120
|
export function avcOrderParameters(sps, pps) {
|
|
119
121
|
const facts = readSps(sps, true);
|
|
120
|
-
const bits = new Bits(
|
|
122
|
+
const bits = new Bits(rbsp(pps, 1), "AVC PPS");
|
|
121
123
|
bits.ue(); bits.ue(); bits.uint(1);
|
|
122
124
|
return { ...facts.pictureOrder, bottomFieldOrderPresent: bits.uint(1) === 1 };
|
|
123
125
|
}
|
|
124
126
|
|
|
125
|
-
/**
|
|
126
|
-
* Remove every emulation prevention byte: a 0x03 after two zero bytes, whatever follows it. H.264 §7.4.1
|
|
127
|
-
* forbids 0x000003 before a byte above 0x03, but encoders write it (LostFilm's LostCoder) and decoders drop
|
|
128
|
-
* it (ffmpeg's ff_h2645_extract_rbsp), so refusing it refuses files every player plays.
|
|
129
|
-
*/
|
|
130
|
-
export function unescapeNal(nal) {
|
|
131
|
-
const bytes = [];
|
|
132
|
-
for (let at = 1, zeroes = 0; at < nal.length; at++) {
|
|
133
|
-
const byte = nal[at];
|
|
134
|
-
if (zeroes >= 2 && byte === 3) { zeroes = 0; continue; }
|
|
135
|
-
bytes.push(byte);
|
|
136
|
-
zeroes = byte === 0 ? zeroes + 1 : 0;
|
|
137
|
-
}
|
|
138
|
-
return Buffer.from(bytes);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
127
|
/** Build the decoder declaration from the complete in-band SPS and PPS units. */
|
|
142
128
|
export function avcConfigurationFromUnits(sps, pps) {
|
|
143
129
|
if (!Buffer.isBuffer(sps) || !Buffer.isBuffer(pps) || sps.length < 4 || !pps.length ||
|
|
@@ -158,19 +144,3 @@ function hrd(bits) {
|
|
|
158
144
|
bits.uint(5);
|
|
159
145
|
return removal + output;
|
|
160
146
|
}
|
|
161
|
-
|
|
162
|
-
export class Bits {
|
|
163
|
-
constructor(bytes, label = "AVC") { this.bytes = bytes; this.at = 0; this.label = label; }
|
|
164
|
-
uint(count) {
|
|
165
|
-
if (count < 0 || count > 32 || this.at + count > this.bytes.length * 8) throw new Error(`${this.label} SPS bits are truncated.`);
|
|
166
|
-
let value = 0;
|
|
167
|
-
for (let index = 0; index < count; index++, this.at++) value = value * 2 + ((this.bytes[this.at >> 3] >> (7 - (this.at & 7))) & 1);
|
|
168
|
-
return value;
|
|
169
|
-
}
|
|
170
|
-
ue() {
|
|
171
|
-
let zeroes = 0;
|
|
172
|
-
while (!this.uint(1)) if (++zeroes > 31) throw new Error(`${this.label} Exp-Golomb value is invalid.`);
|
|
173
|
-
return 2 ** zeroes - 1 + this.uint(zeroes);
|
|
174
|
-
}
|
|
175
|
-
se() { const code = this.ue(); return code & 1 ? (code + 1) / 2 : -code / 2; }
|
|
176
|
-
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { avcConfigurationFromUnits, avcOrderParameters } from "./h264-configuration.js";
|
|
2
2
|
import { AvcPictureOrder } from "./avc-picture-order.js";
|
|
3
3
|
import { CadencedPictureOrder } from "./CadencedPictureOrder.js";
|
|
4
|
+
import { Bits, EmulationPrevention, rbsp } from "./bitstream.js";
|
|
5
|
+
import { ElementaryAddresses } from "./ElementaryAddresses.js";
|
|
4
6
|
|
|
5
7
|
/** Incremental AVC access units, retaining addresses rather than frame payloads. */
|
|
6
8
|
export class H264ElementaryIndex {
|
|
@@ -9,8 +11,7 @@ export class H264ElementaryIndex {
|
|
|
9
11
|
#position = 0;
|
|
10
12
|
#zeroes = 0;
|
|
11
13
|
#nal = null;
|
|
12
|
-
#
|
|
13
|
-
#stamps = [];
|
|
14
|
+
#addresses = new ElementaryAddresses("AVC access-unit addresses are incomplete.");
|
|
14
15
|
#sets = new Map();
|
|
15
16
|
#frame = null;
|
|
16
17
|
#prefix = 0;
|
|
@@ -28,8 +29,8 @@ export class H264ElementaryIndex {
|
|
|
28
29
|
|
|
29
30
|
push(bytes, sourceStart, stamp) {
|
|
30
31
|
const from = this.#position;
|
|
31
|
-
this.#
|
|
32
|
-
if (stamp?.pts !== null && stamp?.pts !== undefined) this.#
|
|
32
|
+
this.#addresses.map(from, bytes.length, sourceStart);
|
|
33
|
+
if (stamp?.pts !== null && stamp?.pts !== undefined) this.#addresses.stamp(from, stamp.pts, stamp.dts ?? stamp.pts);
|
|
33
34
|
for (const byte of bytes) {
|
|
34
35
|
if (byte === 1 && this.#zeroes >= 2) {
|
|
35
36
|
const start = this.#position - this.#zeroes;
|
|
@@ -80,7 +81,7 @@ export class H264ElementaryIndex {
|
|
|
80
81
|
const bytes = Buffer.from(nal.bytes.slice(0, size));
|
|
81
82
|
if (nal.sei) {
|
|
82
83
|
const timing = nal.sei.finish();
|
|
83
|
-
if (timing && this.#track.picStructPresent) this.#picStruct =
|
|
84
|
+
if (timing && this.#track.picStructPresent) this.#picStruct = pictureStructure(timing, this.#track.seiDelayBits ?? 0);
|
|
84
85
|
}
|
|
85
86
|
if ([7, 8].includes(nal.type)) {
|
|
86
87
|
const previous = this.#sets.get(nal.type);
|
|
@@ -100,15 +101,14 @@ export class H264ElementaryIndex {
|
|
|
100
101
|
return;
|
|
101
102
|
}
|
|
102
103
|
if (![1, 5].includes(nal.type)) return;
|
|
103
|
-
const firstMacroblock =
|
|
104
|
+
const firstMacroblock = firstMacroblockOf(bytes);
|
|
104
105
|
if (this.#frame && firstMacroblock !== 0) {
|
|
105
106
|
this.#frame.keyframe ||= nal.type === 5;
|
|
106
107
|
return;
|
|
107
108
|
}
|
|
108
109
|
if (!this.#track.codecPrivateB64) throw new Error("AVC picture precedes its decoder configuration.");
|
|
109
110
|
const start = this.#prefix ?? nal.start;
|
|
110
|
-
let stamp =
|
|
111
|
-
while (this.#stamps[0]?.position <= nal.start) stamp = this.#stamps.shift();
|
|
111
|
+
let stamp = this.#addresses.stampAt(nal.start);
|
|
112
112
|
let pictureOrder = null;
|
|
113
113
|
if (this.#track.frameOnly && !this.#track.picStructPresent) {
|
|
114
114
|
try { pictureOrder = this.#order?.read(bytes); }
|
|
@@ -140,10 +140,7 @@ export class H264ElementaryIndex {
|
|
|
140
140
|
#close(end, duration) {
|
|
141
141
|
if (!(duration > 0)) throw new Error("AVC final access-unit duration is unavailable.");
|
|
142
142
|
const { start, picStruct: _picStruct, pictureOrder, orderReset, ...timing } = this.#frame;
|
|
143
|
-
const ranges = this.#
|
|
144
|
-
range.sourceStart + Math.max(start, range.from) - range.from,
|
|
145
|
-
range.sourceStart + Math.min(end, range.to) - range.from - 1]);
|
|
146
|
-
if (ranges.reduce((sum, [a, b]) => sum + b - a + 1, 0) !== end - start) throw new Error("AVC access-unit addresses are incomplete.");
|
|
143
|
+
const ranges = this.#addresses.ranges(start, end);
|
|
147
144
|
const packet = { ...timing, duration, ranges };
|
|
148
145
|
if (this.#cadenced) this.#cadenced.push(packet, pictureOrder, orderReset);
|
|
149
146
|
else this.#index.append(this.#track.trackNumber, packet);
|
|
@@ -154,7 +151,7 @@ export class H264ElementaryIndex {
|
|
|
154
151
|
this.#recent.splice(this.#recent.indexOf(earliest), 1);
|
|
155
152
|
this.#index.coverThrough(this.#track.trackNumber, Math.max(0, earliest + duration));
|
|
156
153
|
}
|
|
157
|
-
this.#
|
|
154
|
+
this.#addresses.release(end);
|
|
158
155
|
}
|
|
159
156
|
}
|
|
160
157
|
|
|
@@ -166,18 +163,10 @@ class SeiTiming {
|
|
|
166
163
|
#remaining = 0;
|
|
167
164
|
#bytes = [];
|
|
168
165
|
#timing = null;
|
|
169
|
-
#
|
|
170
|
-
#escaped = false;
|
|
166
|
+
#escapes = new EmulationPrevention();
|
|
171
167
|
|
|
172
168
|
push(byte) {
|
|
173
|
-
if (this.#
|
|
174
|
-
this.#zeroes = 0;
|
|
175
|
-
this.#escaped = true;
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
if (this.#escaped && byte > 3) throw new Error("AVC SEI escape byte is invalid.");
|
|
179
|
-
this.#escaped = false;
|
|
180
|
-
this.#zeroes = byte === 0 ? this.#zeroes + 1 : 0;
|
|
169
|
+
if (!this.#escapes.keeps(byte)) return;
|
|
181
170
|
if (this.#phase === "type") {
|
|
182
171
|
this.#type += byte;
|
|
183
172
|
if (byte !== 255) this.#phase = "size";
|
|
@@ -203,43 +192,27 @@ class SeiTiming {
|
|
|
203
192
|
}
|
|
204
193
|
|
|
205
194
|
finish() {
|
|
206
|
-
if (
|
|
195
|
+
if (!(this.#phase === "size" && this.#type === 128 && this.#size === 0)) {
|
|
207
196
|
throw new Error("AVC SEI payload or trailing bits are incomplete.");
|
|
208
197
|
}
|
|
209
198
|
return this.#timing;
|
|
210
199
|
}
|
|
211
200
|
}
|
|
212
201
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
202
|
+
/** The pic_struct of a picture timing SEI payload, which follows `offset` bits of HRD delays. */
|
|
203
|
+
function pictureStructure(payload, offset) {
|
|
204
|
+
if (offset + 4 > payload.length * 8) throw new Error("AVC SEI picture timing is truncated.");
|
|
205
|
+
const bits = new Bits(payload, "AVC SEI picture timing");
|
|
206
|
+
bits.skip(offset);
|
|
207
|
+
const value = bits.uint(4);
|
|
217
208
|
if (value > 8) throw new Error("AVC SEI picture structure is reserved.");
|
|
218
209
|
return value;
|
|
219
210
|
}
|
|
220
211
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
if (at + 1 >= bytes.length || bytes[at + 1] > 3) throw new Error("AVC slice escape byte is invalid.");
|
|
227
|
-
zeroes = 0;
|
|
228
|
-
continue;
|
|
229
|
-
}
|
|
230
|
-
raw.push(byte);
|
|
231
|
-
zeroes = byte === 0 ? zeroes + 1 : 0;
|
|
212
|
+
/** first_mb_in_slice of an AVC slice NAL unit. */
|
|
213
|
+
function firstMacroblockOf(nal) {
|
|
214
|
+
try { return new Bits(rbsp(nal, 1), "AVC slice header").ue(); }
|
|
215
|
+
catch (error) {
|
|
216
|
+
throw new Error(error.message.includes("Exp-Golomb") ? "AVC first macroblock is invalid." : "AVC slice header is truncated.");
|
|
232
217
|
}
|
|
233
|
-
bytes = Buffer.from(raw);
|
|
234
|
-
let position = 0, zeroes = 0;
|
|
235
|
-
const bit = () => {
|
|
236
|
-
if (position >= bytes.length * 8) throw new Error("AVC slice header is truncated.");
|
|
237
|
-
const value = (bytes[position >> 3] >> (7 - (position & 7))) & 1;
|
|
238
|
-
position++;
|
|
239
|
-
return value;
|
|
240
|
-
};
|
|
241
|
-
while (!bit()) if (++zeroes > 31) throw new Error("AVC first macroblock is invalid.");
|
|
242
|
-
let value = 0;
|
|
243
|
-
for (let at = 0; at < zeroes; at++) value = value * 2 + bit();
|
|
244
|
-
return 2 ** zeroes - 1 + value;
|
|
245
218
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Bits } from "./
|
|
1
|
+
import { Bits, rbsp } from "./bitstream.js";
|
|
2
2
|
|
|
3
3
|
/** HEVC SPS ordering limits are declared for every temporal sublayer (H.265 7.3.2.2). */
|
|
4
4
|
export function hevcConfiguration(hvcc) {
|
|
@@ -29,27 +29,18 @@ export function hevcConfiguration(hvcc) {
|
|
|
29
29
|
export function hevcSequence(nal) {
|
|
30
30
|
if ((nal[0] & 128) || ((nal[0] >> 1) & 63) !== 33 || !(nal[1] & 7)) throw new Error("HEVC SPS NAL header is invalid.");
|
|
31
31
|
if ((nal[0] & 1) || (nal[1] >> 3)) throw new Error("HEVC multilayer SPS requires layer-specific configuration.");
|
|
32
|
-
const
|
|
33
|
-
for (let at = 2, zeroes = 0; at < nal.length; at++) {
|
|
34
|
-
const byte = nal[at];
|
|
35
|
-
if (zeroes >= 2 && byte === 3) {
|
|
36
|
-
if (at + 1 >= nal.length || nal[at + 1] > 3) throw new Error("HEVC escape byte is invalid.");
|
|
37
|
-
zeroes = 0; continue;
|
|
38
|
-
}
|
|
39
|
-
bytes.push(byte); zeroes = byte === 0 ? zeroes + 1 : 0;
|
|
40
|
-
}
|
|
41
|
-
const bits = new Bits(Buffer.from(bytes), "HEVC");
|
|
32
|
+
const bits = new Bits(rbsp(nal, 2), "HEVC SPS");
|
|
42
33
|
bits.uint(4);
|
|
43
34
|
const layers = bits.uint(3);
|
|
44
35
|
if (layers > 6) throw new Error("HEVC temporal sublayer count is invalid.");
|
|
45
36
|
bits.uint(1);
|
|
46
|
-
skip(
|
|
37
|
+
bits.skip(96);
|
|
47
38
|
const profiles = [], levels = [];
|
|
48
39
|
for (let layer = 0; layer < layers; layer++) { profiles.push(bits.uint(1)); levels.push(bits.uint(1)); }
|
|
49
|
-
if (layers) skip(
|
|
40
|
+
if (layers) bits.skip((8 - layers) * 2);
|
|
50
41
|
for (let layer = 0; layer < layers; layer++) {
|
|
51
|
-
if (profiles[layer]) skip(
|
|
52
|
-
if (levels[layer]) skip(
|
|
42
|
+
if (profiles[layer]) bits.skip(88);
|
|
43
|
+
if (levels[layer]) bits.skip(8);
|
|
53
44
|
}
|
|
54
45
|
const sequenceId = bits.ue(), chroma = bits.ue();
|
|
55
46
|
if (sequenceId > 15 || chroma > 3) throw new Error("HEVC SPS identity or chroma format is invalid.");
|
|
@@ -76,8 +67,6 @@ export function hevcSequence(nal) {
|
|
|
76
67
|
...sequenceTiming(bits, orderBits) };
|
|
77
68
|
}
|
|
78
69
|
|
|
79
|
-
function skip(bits, count) { for (let left = count; left > 0; left -= 32) bits.uint(Math.min(32, left)); }
|
|
80
|
-
|
|
81
70
|
function sequenceTiming(bits, orderBits) {
|
|
82
71
|
for (let field = 0; field < 6; field++) bits.ue();
|
|
83
72
|
if (bits.uint(1) && bits.uint(1)) {
|
|
@@ -123,7 +112,7 @@ function sequenceTiming(bits, orderBits) {
|
|
|
123
112
|
if (!bits.uint(1)) return {};
|
|
124
113
|
if (bits.uint(1) && bits.uint(8) === 255) { bits.uint(16); bits.uint(16); }
|
|
125
114
|
if (bits.uint(1)) bits.uint(1);
|
|
126
|
-
if (bits.uint(1)) { bits.uint(4); if (bits.uint(1)) skip(
|
|
115
|
+
if (bits.uint(1)) { bits.uint(4); if (bits.uint(1)) bits.skip(24); }
|
|
127
116
|
if (bits.uint(1)) { bits.ue(); bits.ue(); }
|
|
128
117
|
bits.uint(1);
|
|
129
118
|
const fieldSequence = bits.uint(1), frameFieldInfo = bits.uint(1);
|
|
@@ -140,15 +129,10 @@ function sequenceTiming(bits, orderBits) {
|
|
|
140
129
|
/** Construct hvcC from complete in-band parameter sets without keeping picture bytes. */
|
|
141
130
|
export function hevcConfigurationFromUnits(vps, sps, pps) {
|
|
142
131
|
const facts = hevcSequence(sps);
|
|
143
|
-
const raw =
|
|
144
|
-
for (let at = 2, zeroes = 0; at < sps.length; at++) {
|
|
145
|
-
const byte = sps[at];
|
|
146
|
-
if (zeroes >= 2 && byte === 3) { zeroes = 0; continue; }
|
|
147
|
-
raw.push(byte); zeroes = byte === 0 ? zeroes + 1 : 0;
|
|
148
|
-
}
|
|
132
|
+
const raw = rbsp(sps, 2);
|
|
149
133
|
const header = Buffer.alloc(23);
|
|
150
134
|
header[0] = 1;
|
|
151
|
-
|
|
135
|
+
raw.copy(header, 1, 1, 13);
|
|
152
136
|
header[13] = 0xf0; header[15] = 0xfc;
|
|
153
137
|
header[16] = 0xfc | facts.chromaFormat;
|
|
154
138
|
header[17] = 0xf8 | (facts.bitDepth - 8); header[18] = 0xf8 | (facts.chromaBitDepth - 8);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { hevcConfigurationFromUnits } from "./hevc-configuration.js";
|
|
2
2
|
import { HevcPictureOrder } from "./hevc-picture-order.js";
|
|
3
3
|
import { CadencedPictureOrder } from "./CadencedPictureOrder.js";
|
|
4
|
+
import { ElementaryAddresses } from "./ElementaryAddresses.js";
|
|
4
5
|
|
|
5
6
|
/** HEVC access units keep exact source addresses and the PES presentation/decode clocks. */
|
|
6
7
|
export class HevcElementaryIndex {
|
|
@@ -9,8 +10,7 @@ export class HevcElementaryIndex {
|
|
|
9
10
|
#position = 0;
|
|
10
11
|
#zeroes = 0;
|
|
11
12
|
#nal = null;
|
|
12
|
-
#
|
|
13
|
-
#stamps = [];
|
|
13
|
+
#addresses = new ElementaryAddresses("HEVC access-unit addresses are incomplete.");
|
|
14
14
|
#sets = new Map();
|
|
15
15
|
#frame = null;
|
|
16
16
|
#prefix = 0;
|
|
@@ -26,8 +26,8 @@ export class HevcElementaryIndex {
|
|
|
26
26
|
|
|
27
27
|
push(bytes, sourceStart, stamp) {
|
|
28
28
|
const from = this.#position;
|
|
29
|
-
this.#
|
|
30
|
-
if (stamp?.pts !== null && stamp?.pts !== undefined) this.#
|
|
29
|
+
this.#addresses.map(from, bytes.length, sourceStart);
|
|
30
|
+
if (stamp?.pts !== null && stamp?.pts !== undefined) this.#addresses.stamp(from, stamp.pts, stamp.dts ?? stamp.pts);
|
|
31
31
|
for (const byte of bytes) {
|
|
32
32
|
if (byte === 1 && this.#zeroes >= 2) {
|
|
33
33
|
const start = this.#position - this.#zeroes;
|
|
@@ -84,8 +84,7 @@ export class HevcElementaryIndex {
|
|
|
84
84
|
}
|
|
85
85
|
if (!this.#track.codecPrivateB64) throw new Error("HEVC picture precedes its decoder configuration.");
|
|
86
86
|
const start = this.#prefix ?? nal.start;
|
|
87
|
-
let stamp =
|
|
88
|
-
while (this.#stamps[0]?.position <= nal.start) stamp = this.#stamps.shift();
|
|
87
|
+
let stamp = this.#addresses.stampAt(nal.start);
|
|
89
88
|
let pictureOrder = null;
|
|
90
89
|
try { pictureOrder = this.#order?.read(bytes); }
|
|
91
90
|
catch (error) { if (!stamp) throw error; }
|
|
@@ -115,10 +114,7 @@ export class HevcElementaryIndex {
|
|
|
115
114
|
|
|
116
115
|
#close(end, duration) {
|
|
117
116
|
const { start, pictureOrder, orderReset, ...timing } = this.#frame;
|
|
118
|
-
const ranges = this.#
|
|
119
|
-
range.sourceStart + Math.max(start, range.from) - range.from,
|
|
120
|
-
range.sourceStart + Math.min(end, range.to) - range.from - 1]);
|
|
121
|
-
if (ranges.reduce((sum, [a, b]) => sum + b - a + 1, 0) !== end - start) throw new Error("HEVC access-unit addresses are incomplete.");
|
|
117
|
+
const ranges = this.#addresses.ranges(start, end);
|
|
122
118
|
if (this.#cadenced) this.#cadenced.push({ ...timing, duration, ranges }, pictureOrder, orderReset);
|
|
123
119
|
else this.#index.append(this.#track.trackNumber, { ...timing, duration, ranges });
|
|
124
120
|
if (!this.#cadenced) this.#recent.push(timing.pts);
|
|
@@ -127,6 +123,6 @@ export class HevcElementaryIndex {
|
|
|
127
123
|
this.#recent.splice(this.#recent.indexOf(earliest), 1);
|
|
128
124
|
this.#index.coverThrough(this.#track.trackNumber, Math.max(0, earliest + duration));
|
|
129
125
|
}
|
|
130
|
-
this.#
|
|
126
|
+
this.#addresses.release(end);
|
|
131
127
|
}
|
|
132
128
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Bits } from "./
|
|
1
|
+
import { Bits, rbsp } from "./bitstream.js";
|
|
2
2
|
|
|
3
3
|
/** HEVC first-slice picture order, using the previous reference TemporalId 0 picture. */
|
|
4
4
|
export class HevcPictureOrder {
|
|
@@ -14,7 +14,7 @@ export class HevcPictureOrder {
|
|
|
14
14
|
throw new Error("HEVC picture-order declarations are invalid.");
|
|
15
15
|
}
|
|
16
16
|
this.#sequence = sequence;
|
|
17
|
-
const bits = new Bits(rbsp(pps), "HEVC");
|
|
17
|
+
const bits = new Bits(rbsp(pps, 2), "HEVC SPS");
|
|
18
18
|
const id = bits.ue(), sequenceId = bits.ue();
|
|
19
19
|
if (id > 63 || sequenceId > 15) throw new Error("HEVC PPS identity is invalid.");
|
|
20
20
|
const dependentSlices = bits.uint(1), outputFlag = bits.uint(1), extraBits = bits.uint(3);
|
|
@@ -24,7 +24,7 @@ export class HevcPictureOrder {
|
|
|
24
24
|
read(nal) {
|
|
25
25
|
const type = (nal[0] >> 1) & 63, temporalId = (nal[1] & 7) - 1;
|
|
26
26
|
if (type > 31 || temporalId < 0 || (nal[0] & 129) || (nal[1] >> 3)) throw new Error("HEVC slice identity is invalid.");
|
|
27
|
-
const bits = new Bits(rbsp(nal), "HEVC");
|
|
27
|
+
const bits = new Bits(rbsp(nal, 2), "HEVC SPS");
|
|
28
28
|
if (!bits.uint(1)) throw new Error("HEVC picture order requires its first slice.");
|
|
29
29
|
const irap = type >= 16 && type <= 23;
|
|
30
30
|
if (irap) bits.uint(1);
|
|
@@ -49,18 +49,3 @@ export class HevcPictureOrder {
|
|
|
49
49
|
return { count: msb + lsb, reset };
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
function rbsp(nal) {
|
|
54
|
-
const bytes = [];
|
|
55
|
-
for (let at = 2, zeroes = 0; at < nal.length; at++) {
|
|
56
|
-
const byte = nal[at];
|
|
57
|
-
if (zeroes >= 2 && byte === 3) {
|
|
58
|
-
if (at + 1 >= nal.length || nal[at + 1] > 3) throw new Error("HEVC escape byte is invalid.");
|
|
59
|
-
zeroes = 0;
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
bytes.push(byte);
|
|
63
|
-
zeroes = byte === 0 ? zeroes + 1 : 0;
|
|
64
|
-
}
|
|
65
|
-
return Buffer.from(bytes);
|
|
66
|
-
}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { Bits } from "./
|
|
2
|
-
|
|
3
|
-
const SAMPLE_RATES = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
|
|
1
|
+
import { Bits, parseAudioSpecificConfig } from "./bitstream.js";
|
|
4
2
|
|
|
5
3
|
/** LOAS frames retain their original addresses; payload bit positions describe raw AAC. */
|
|
6
4
|
export class LatmFrames {
|
|
@@ -10,7 +8,7 @@ export class LatmFrames {
|
|
|
10
8
|
if (bytes.length < 3 || bytes[0] !== 0x56 || (bytes[1] & 0xe0) !== 0xe0) throw new Error("AAC LOAS synchronization is invalid.");
|
|
11
9
|
const size = 3 + ((bytes[1] & 31) << 8) + bytes[2];
|
|
12
10
|
if (bytes.length < size) return { size };
|
|
13
|
-
const bits = new Bits(bytes.subarray(3, size), "AAC LATM");
|
|
11
|
+
const bits = new Bits(bytes.subarray(3, size), "AAC LATM SPS");
|
|
14
12
|
if (!bits.uint(1)) {
|
|
15
13
|
const version = bits.uint(1), versionA = version ? bits.uint(1) : 0;
|
|
16
14
|
if (versionA) throw new Error("AAC LATM version A requires its own payload declarations.");
|
|
@@ -18,11 +16,11 @@ export class LatmFrames {
|
|
|
18
16
|
const sameTime = bits.uint(1), subframes = bits.uint(6), programs = bits.uint(4), layers = bits.uint(3);
|
|
19
17
|
if (!sameTime || subframes || programs || layers) throw new Error("AAC LATM multiplexed layers require separate packet declarations.");
|
|
20
18
|
const ascLength = version ? latmValue(bits) : null, ascStart = bits.at;
|
|
21
|
-
const configuration =
|
|
19
|
+
const configuration = audioConfiguration(bits);
|
|
22
20
|
let ascEnd = bits.at;
|
|
23
21
|
if (ascLength !== null) {
|
|
24
22
|
if (ascEnd - ascStart > ascLength) throw new Error("AAC LATM decoder configuration exceeds its bit length.");
|
|
25
|
-
skip(
|
|
23
|
+
bits.skip(ascLength - (ascEnd - ascStart));
|
|
26
24
|
ascEnd = bits.at;
|
|
27
25
|
}
|
|
28
26
|
const lengthType = bits.uint(3);
|
|
@@ -36,11 +34,7 @@ export class LatmFrames {
|
|
|
36
34
|
}
|
|
37
35
|
}
|
|
38
36
|
if (bits.uint(1)) bits.uint(8);
|
|
39
|
-
const privateBytes =
|
|
40
|
-
for (let bit = ascStart; bit < ascEnd; bit++) {
|
|
41
|
-
const value = (bits.bytes[bit >> 3] >> (7 - (bit & 7))) & 1;
|
|
42
|
-
privateBytes[(bit - ascStart) >> 3] |= value << (7 - ((bit - ascStart) & 7));
|
|
43
|
-
}
|
|
37
|
+
const privateBytes = bits.copy(ascStart, ascEnd);
|
|
44
38
|
this.#configuration = { ...configuration, codecPrivateB64: privateBytes.toString("base64") };
|
|
45
39
|
}
|
|
46
40
|
if (!this.#configuration) throw new Error("AAC LATM payload precedes its stream configuration.");
|
|
@@ -51,49 +45,12 @@ export class LatmFrames {
|
|
|
51
45
|
}
|
|
52
46
|
}
|
|
53
47
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (type === 5 || parametricStereo) { sampleRate = audioRate(bits); type = audioType(bits); }
|
|
61
|
-
if (![1, 2, 3, 4].includes(type) || !coreRate || !sampleRate || channels > 7) {
|
|
62
|
-
throw new Error("AAC LATM requires a supported declared audio configuration.");
|
|
63
|
-
}
|
|
64
|
-
const shortFrame = bits.uint(1);
|
|
65
|
-
if (bits.uint(1)) bits.uint(14);
|
|
66
|
-
const extension = bits.uint(1);
|
|
67
|
-
channels = channels ? channels === 7 ? 8 : channels : programChannels(bits, start, coreRate);
|
|
68
|
-
if (extension && bits.uint(1)) throw new Error("AAC LATM extension version requires its own decoder declaration.");
|
|
69
|
-
return { sampleRate, channels: parametricStereo && channels === 1 ? 2 : channels,
|
|
70
|
-
duration: (shortFrame ? 960 : 1024) / coreRate };
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function audioType(bits) { const type = bits.uint(5); return type === 31 ? 32 + bits.uint(6) : type; }
|
|
74
|
-
function audioRate(bits) { const index = bits.uint(4); return index === 15 ? bits.uint(24) : SAMPLE_RATES[index]; }
|
|
75
|
-
|
|
76
|
-
/** Program configuration declares every single, paired and LFE channel. */
|
|
77
|
-
function programChannels(bits, start, sampleRate) {
|
|
78
|
-
bits.uint(4); bits.uint(2);
|
|
79
|
-
if (SAMPLE_RATES[bits.uint(4)] !== sampleRate) throw new Error("AAC program sample rate differs from its decoder declaration.");
|
|
80
|
-
const front = bits.uint(4), side = bits.uint(4), back = bits.uint(4), lfe = bits.uint(2),
|
|
81
|
-
associated = bits.uint(3), coupled = bits.uint(4);
|
|
82
|
-
if (bits.uint(1)) bits.uint(4);
|
|
83
|
-
if (bits.uint(1)) bits.uint(4);
|
|
84
|
-
if (bits.uint(1)) bits.uint(3);
|
|
85
|
-
let channels = lfe;
|
|
86
|
-
for (let index = 0; index < front + side + back; index++) {
|
|
87
|
-
channels += bits.uint(1) ? 2 : 1;
|
|
88
|
-
bits.uint(4);
|
|
89
|
-
}
|
|
90
|
-
skip(bits, 4 * (lfe + associated));
|
|
91
|
-
skip(bits, 5 * coupled);
|
|
92
|
-
skip(bits, (8 - ((bits.at - start) % 8)) % 8);
|
|
93
|
-
skip(bits, bits.uint(8) * 8);
|
|
94
|
-
if (!channels) throw new Error("AAC program declares no output channels.");
|
|
95
|
-
return channels;
|
|
48
|
+
/** The AudioSpecificConfig of a LATM stream, or a refusal of one this proxy cannot describe. */
|
|
49
|
+
function audioConfiguration(bits) {
|
|
50
|
+
const config = parseAudioSpecificConfig(bits);
|
|
51
|
+
if (!config.supported) throw new Error("AAC LATM requires a supported declared audio configuration.");
|
|
52
|
+
if (config.extensionVersion) throw new Error("AAC LATM extension version requires its own decoder declaration.");
|
|
53
|
+
return { sampleRate: config.sampleRate, channels: config.channels, duration: config.frameLength / config.coreSampleRate };
|
|
96
54
|
}
|
|
97
55
|
|
|
98
56
|
function latmValue(bits) { return bits.uint((bits.uint(2) + 1) * 8); }
|
|
99
|
-
function skip(bits, count) { for (let left = count; left > 0; left -= 32) bits.uint(Math.min(left, 32)); }
|
|
@@ -6,6 +6,8 @@ import { LatmFrames } from "./latm-frame.js";
|
|
|
6
6
|
import { ac3Frame } from "./ac3-frame.js";
|
|
7
7
|
import { dtsFrame } from "./dts-frame.js";
|
|
8
8
|
import { MpegAudioReservoir } from "./mpeg-audio-reservoir.js";
|
|
9
|
+
import { AAC_SAMPLE_RATES } from "./bitstream.js";
|
|
10
|
+
import { ElementaryAddresses } from "./ElementaryAddresses.js";
|
|
9
11
|
|
|
10
12
|
/** Preserve elementary-frame addresses while transport packets are discarded. */
|
|
11
13
|
export class MpegElementaryIndex {
|
|
@@ -23,7 +25,7 @@ export class MpegElementaryIndex {
|
|
|
23
25
|
this.index.refuse(track.trackNumber, `elementary-index-unavailable:${track.codecId || "unknown"}`);
|
|
24
26
|
continue;
|
|
25
27
|
}
|
|
26
|
-
this.#states.set(track.trackNumber, { track, offset: 0,
|
|
28
|
+
this.#states.set(track.trackNumber, { track, offset: 0, addresses: new ElementaryAddresses("MPEG elementary bytes have missing addresses."), carry: Buffer.alloc(0),
|
|
27
29
|
latm: track.codecId === "aac_latm" ? new LatmFrames() : null,
|
|
28
30
|
nalIndex: track.codecId === "h264" ? new H264ElementaryIndex(track, this.index)
|
|
29
31
|
: track.codecId === "hevc" ? new HevcElementaryIndex(track, this.index) : null,
|
|
@@ -39,7 +41,7 @@ export class MpegElementaryIndex {
|
|
|
39
41
|
const state = this.#states.get(id);
|
|
40
42
|
if (!state || bytes.length === 0) return;
|
|
41
43
|
const position = state.offset;
|
|
42
|
-
if (!state.nalIndex) state.
|
|
44
|
+
if (!state.nalIndex) state.addresses.map(position, bytes.length, sourceStart);
|
|
43
45
|
let normalized = null;
|
|
44
46
|
if (pts !== null) {
|
|
45
47
|
const cycle = 2 ** 33 / 90000;
|
|
@@ -56,7 +58,7 @@ export class MpegElementaryIndex {
|
|
|
56
58
|
if (presentation < state.lastTimestamp - cycle / 2) presentation += cycle;
|
|
57
59
|
else if (presentation > state.lastTimestamp + cycle / 2) presentation -= cycle;
|
|
58
60
|
normalized = { position, pts: presentation, dts: state.lastTimestamp };
|
|
59
|
-
if (!state.nalIndex) state.
|
|
61
|
+
if (!state.nalIndex) state.addresses.stamp(position, normalized.pts, normalized.dts);
|
|
60
62
|
}
|
|
61
63
|
state.offset += bytes.length;
|
|
62
64
|
if (state.nalIndex) state.nalIndex.push(bytes, sourceStart, normalized);
|
|
@@ -109,7 +111,7 @@ export class MpegElementaryIndex {
|
|
|
109
111
|
if (type < 1 || type > 3) throw new Error("MPEG picture type is invalid.");
|
|
110
112
|
const start = state.pendingStart ?? offset;
|
|
111
113
|
if (state.picture) this.#closePicture(state, start);
|
|
112
|
-
const stamp = stampAt(
|
|
114
|
+
const stamp = state.addresses.stampAt(offset);
|
|
113
115
|
if (state.requiresStamp && !stamp) throw new Error("MPEG repeated frames require an explicit following timestamp.");
|
|
114
116
|
state.requiresStamp = false;
|
|
115
117
|
// `temporal_reference` counts the pictures of one group of pictures in
|
|
@@ -142,9 +144,9 @@ export class MpegElementaryIndex {
|
|
|
142
144
|
#closePicture(state, end) {
|
|
143
145
|
const { start, ...packet } = state.picture;
|
|
144
146
|
if (!(end > start)) throw new Error("MPEG picture has no bytes.");
|
|
145
|
-
this.index.append(state.track.trackNumber, { ...packet, ranges: addresses(
|
|
147
|
+
this.index.append(state.track.trackNumber, { ...packet, ranges: state.addresses.ranges(start, end) });
|
|
146
148
|
state.shownUntil = Math.max(state.shownUntil ?? -Infinity, packet.pts + packet.duration);
|
|
147
|
-
state.
|
|
149
|
+
state.addresses.release(end);
|
|
148
150
|
}
|
|
149
151
|
|
|
150
152
|
#audio(state, chunk) {
|
|
@@ -154,7 +156,7 @@ export class MpegElementaryIndex {
|
|
|
154
156
|
: ["ac3", "eac3"].includes(state.track.codecId) ? ac3Frame(state.audioBytes)
|
|
155
157
|
: state.track.codecId === "dts" ? dtsFrame(state.audioBytes) : mpegAudioFrame(state.audioBytes);
|
|
156
158
|
if (state.audioBytes.length < frame.size) break;
|
|
157
|
-
const stamp = stampAt(state
|
|
159
|
+
const stamp = state.addresses.stampAt(state.audioStart);
|
|
158
160
|
if (stamp) state.audioPts = stamp.pts;
|
|
159
161
|
if (state.audioPts === null) throw new Error("MPEG audio frame has no presentation timestamp.");
|
|
160
162
|
if (frame.codecPrivateB64) {
|
|
@@ -166,39 +168,24 @@ export class MpegElementaryIndex {
|
|
|
166
168
|
this.index.append(state.track.trackNumber, { pts: state.audioPts, dts: state.audioPts,
|
|
167
169
|
...(reservoir ? { decodeFromIndex: reservoir.decodeFromIndex } : {}),
|
|
168
170
|
...(frame.bitOffset === undefined ? {} : { bitOffset: frame.bitOffset, bitLength: frame.bitLength }),
|
|
169
|
-
duration: frame.duration, keyframe: true, ranges: addresses(state
|
|
171
|
+
duration: frame.duration, keyframe: true, ranges: state.addresses.ranges(state.audioStart + (frame.headerBytes ?? 0), state.audioStart + frame.size) });
|
|
170
172
|
reservoir?.commit();
|
|
171
173
|
state.packetCount++;
|
|
172
174
|
state.audioPts += frame.duration;
|
|
173
175
|
this.index.coverThrough(state.track.trackNumber, Math.max(0, state.audioPts));
|
|
174
176
|
state.audioStart += frame.size;
|
|
175
177
|
state.audioBytes = state.audioBytes.subarray(frame.size);
|
|
176
|
-
state.
|
|
178
|
+
state.addresses.release(state.audioStart);
|
|
177
179
|
}
|
|
178
180
|
state.audioBytes = Buffer.from(state.audioBytes);
|
|
179
181
|
}
|
|
180
182
|
}
|
|
181
183
|
|
|
182
|
-
function stampAt(state, position) {
|
|
183
|
-
let latest = null;
|
|
184
|
-
while (state.stamps[0]?.position <= position) latest = state.stamps.shift();
|
|
185
|
-
return latest;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
function addresses(state, from, to) {
|
|
189
|
-
const ranges = state.mappings.filter(mapping => mapping.from < to && mapping.to > from).map(mapping => [
|
|
190
|
-
mapping.sourceStart + Math.max(from, mapping.from) - mapping.from,
|
|
191
|
-
mapping.sourceStart + Math.min(to, mapping.to) - mapping.from - 1
|
|
192
|
-
]);
|
|
193
|
-
if (ranges.reduce((sum, [a, b]) => sum + b - a + 1, 0) !== to - from) throw new Error("MPEG elementary bytes have missing addresses.");
|
|
194
|
-
return ranges;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
184
|
function adtsFrame(bytes) {
|
|
198
185
|
if (bytes[0] !== 0xff || (bytes[1] & 0xf6) !== 0xf0) throw new Error("AAC ADTS header is invalid.");
|
|
199
186
|
const objectType = (bytes[2] >> 6) + 1;
|
|
200
187
|
const frequencyIndex = (bytes[2] >> 2) & 15;
|
|
201
|
-
const sampleRate = [
|
|
188
|
+
const sampleRate = AAC_SAMPLE_RATES[frequencyIndex];
|
|
202
189
|
const channelConfiguration = ((bytes[2] & 1) << 2) | (bytes[3] >> 6);
|
|
203
190
|
if (!sampleRate || !channelConfiguration) throw new Error("AAC ADTS requires a declared sample rate and channel configuration.");
|
|
204
191
|
if (bytes[6] & 3) throw new Error("AAC ADTS multiple raw blocks require block-specific addresses.");
|
|
@@ -1,10 +1,20 @@
|
|
|
1
|
+
import { Bits } from "./bitstream.js";
|
|
2
|
+
|
|
1
3
|
const RATES = [null, 24000 / 1001, 24, 25, 30000 / 1001, 30, 50, 60000 / 1001, 60];
|
|
2
4
|
|
|
3
5
|
/** Read sequence declarations from a bounded elementary-stream prefix. */
|
|
4
6
|
export function mpegVideoConfiguration(bytes) {
|
|
7
|
+
try { return sequenceDeclarations(bytes); }
|
|
8
|
+
catch (error) {
|
|
9
|
+
if (/ bits are truncated\.$/.test(error.message)) throw new Error("MPEG video sequence header is truncated.");
|
|
10
|
+
throw error;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function sequenceDeclarations(bytes) {
|
|
5
15
|
const sequence = bytes.indexOf(Buffer.from([0, 0, 1, 0xb3]));
|
|
6
16
|
if (sequence < 0) return null;
|
|
7
|
-
const
|
|
17
|
+
const stream = new Bits(bytes.subarray(sequence + 4), "MPEG video sequence header"), bits = count => stream.uint(count);
|
|
8
18
|
let width = bits(12), height = bits(12);
|
|
9
19
|
const aspect = bits(4), rate = bits(4);
|
|
10
20
|
if (!width || !height || !aspect || !RATES[rate]) throw new Error("MPEG video sequence dimensions or rate are invalid.");
|
|
@@ -22,7 +32,7 @@ export function mpegVideoConfiguration(bytes) {
|
|
|
22
32
|
if (bytes[at] !== 0 || bytes[at + 1] !== 0 || bytes[at + 2] !== 1) continue;
|
|
23
33
|
if (bytes[at + 3] === 0 || bytes[at + 3] === 0xb8) break;
|
|
24
34
|
if (bytes[at + 3] !== 0xb5 || bytes[at + 4] >> 4 !== 1) continue;
|
|
25
|
-
const
|
|
35
|
+
const part = new Bits(bytes.subarray(at + 4), "MPEG video sequence header"), extension = count => part.uint(count);
|
|
26
36
|
extension(4); extension(8); progressiveSequence = extension(1) === 1;
|
|
27
37
|
if (extension(2) === 0) throw new Error("MPEG video chroma format is invalid.");
|
|
28
38
|
width += extension(2) * 4096;
|
|
@@ -36,15 +46,3 @@ export function mpegVideoConfiguration(bytes) {
|
|
|
36
46
|
}
|
|
37
47
|
return { width, height, fps, codecId, bitDepth: 8, progressiveSequence };
|
|
38
48
|
}
|
|
39
|
-
|
|
40
|
-
function reader(bytes) {
|
|
41
|
-
let offset = 0;
|
|
42
|
-
return count => {
|
|
43
|
-
if (offset + count > bytes.length * 8) throw new Error("MPEG video sequence header is truncated.");
|
|
44
|
-
let value = 0;
|
|
45
|
-
for (let index = 0; index < count; index++, offset++) {
|
|
46
|
-
value = value * 2 + ((bytes[offset >> 3] >> (7 - (offset & 7))) & 1);
|
|
47
|
-
}
|
|
48
|
-
return value;
|
|
49
|
-
};
|
|
50
|
-
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Bits } from "./
|
|
1
|
+
import { Bits } from "./bitstream.js";
|
|
2
2
|
import { RetainedBytes } from "./RetainedBytes.js";
|
|
3
3
|
|
|
4
4
|
/** MPEG-4 Visual VOL/VOP clocks, committed only after packet admission. */
|
|
@@ -46,7 +46,7 @@ export class Mpeg4PictureTiming {
|
|
|
46
46
|
const code = bytes[at + 3];
|
|
47
47
|
if (code >= 0x20 && code <= 0x2f) state.resolution = resolutionOf(bytes.subarray(at + 4));
|
|
48
48
|
if (code === 0xb3) {
|
|
49
|
-
const bits = new Bits(bytes.subarray(at + 4), "MPEG-4 Visual");
|
|
49
|
+
const bits = new Bits(bytes.subarray(at + 4), "MPEG-4 Visual SPS");
|
|
50
50
|
const hours = bits.uint(5), minutes = bits.uint(6);
|
|
51
51
|
marker(bits);
|
|
52
52
|
const seconds = bits.uint(6);
|
|
@@ -59,7 +59,7 @@ export class Mpeg4PictureTiming {
|
|
|
59
59
|
throw new Error("Multiple MPEG-4 pictures require a packed-stream declaration.");
|
|
60
60
|
}
|
|
61
61
|
if (!(state.resolution > 0)) throw new Error("MPEG-4 picture precedes its VOL clock declaration.");
|
|
62
|
-
const bits = new Bits(bytes.subarray(at + 4), "MPEG-4 Visual");
|
|
62
|
+
const bits = new Bits(bytes.subarray(at + 4), "MPEG-4 Visual SPS");
|
|
63
63
|
const type = bits.uint(2);
|
|
64
64
|
let seconds = 0;
|
|
65
65
|
while (bits.uint(1)) seconds++;
|
|
@@ -85,7 +85,7 @@ export class Mpeg4PictureTiming {
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
function resolutionOf(bytes) {
|
|
88
|
-
const bits = new Bits(bytes, "MPEG-4 Visual");
|
|
88
|
+
const bits = new Bits(bytes, "MPEG-4 Visual SPS");
|
|
89
89
|
bits.uint(1); bits.uint(8);
|
|
90
90
|
if (bits.uint(1)) { bits.uint(4); bits.uint(3); }
|
|
91
91
|
if (bits.uint(4) === 15) { bits.uint(8); bits.uint(8); }
|
|
@@ -32,13 +32,7 @@
|
|
|
32
32
|
* PURE: plain values in, plain values out. No container, no torrent.
|
|
33
33
|
*/
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
const AAC_SAMPLING_FREQUENCIES = Object.freeze([
|
|
37
|
-
96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350
|
|
38
|
-
]);
|
|
39
|
-
|
|
40
|
-
/** ISO/IEC 14496-3 Table 1.19: channelConfiguration to channels; 0 is a PCE. */
|
|
41
|
-
const AAC_CHANNELS = Object.freeze([null, 1, 2, 3, 4, 5, 6, 8]);
|
|
35
|
+
import { parseAudioSpecificConfig as readAudioSpecificConfig } from "../container/bitstream.js";
|
|
42
36
|
|
|
43
37
|
/** The AAC-LC object type, and the one frame length the bound is stated for. */
|
|
44
38
|
const AAC_LC = 2;
|
|
@@ -62,29 +56,6 @@ const AC3_SAMPLES_PER_FRAME = 1536;
|
|
|
62
56
|
* @property {number | null} sampleRate - Hz.
|
|
63
57
|
*/
|
|
64
58
|
|
|
65
|
-
/**
|
|
66
|
-
* A minimal MSB-first bit reader over a buffer.
|
|
67
|
-
*
|
|
68
|
-
* @param {Buffer} bytes
|
|
69
|
-
*/
|
|
70
|
-
function bitReader(bytes) {
|
|
71
|
-
let position = 0;
|
|
72
|
-
return {
|
|
73
|
-
read(count) {
|
|
74
|
-
let value = 0;
|
|
75
|
-
for (let i = 0; i < count; i += 1) {
|
|
76
|
-
const byte = bytes[position >> 3];
|
|
77
|
-
if (byte === undefined) {
|
|
78
|
-
throw new RangeError("AudioSpecificConfig ended early");
|
|
79
|
-
}
|
|
80
|
-
value = (value * 2) + ((byte >> (7 - (position & 7))) & 1);
|
|
81
|
-
position += 1;
|
|
82
|
-
}
|
|
83
|
-
return value;
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
59
|
/**
|
|
89
60
|
* The fields of an AudioSpecificConfig the bound is computed from (ISO/IEC
|
|
90
61
|
* 14496-3 §1.6.2.1 and the GASpecificConfig that follows it for AAC-LC).
|
|
@@ -94,30 +65,17 @@ function bitReader(bytes) {
|
|
|
94
65
|
* Null when the bytes are not a readable config.
|
|
95
66
|
*/
|
|
96
67
|
export function parseAudioSpecificConfig(bytes) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
try {
|
|
101
|
-
const bits = bitReader(bytes);
|
|
102
|
-
let objectType = bits.read(5);
|
|
103
|
-
if (objectType === 31) {
|
|
104
|
-
objectType = 32 + bits.read(6);
|
|
105
|
-
}
|
|
106
|
-
const frequencyIndex = bits.read(4);
|
|
107
|
-
const sampleRate = frequencyIndex === 0xf
|
|
108
|
-
? bits.read(24)
|
|
109
|
-
: (AAC_SAMPLING_FREQUENCIES[frequencyIndex] ?? null);
|
|
110
|
-
const channelConfiguration = bits.read(4);
|
|
111
|
-
const channels = AAC_CHANNELS[channelConfiguration] ?? null;
|
|
112
|
-
// GASpecificConfig begins with frameLengthFlag for the object types that
|
|
113
|
-
// carry one; for any other the frame length is not read here.
|
|
114
|
-
const frameLength = objectType === AAC_LC
|
|
115
|
-
? (bits.read(1) === 1 ? 960 : AAC_LC_FRAME_LENGTH)
|
|
116
|
-
: null;
|
|
117
|
-
return { objectType, sampleRate: sampleRate > 0 ? sampleRate : null, channels, frameLength };
|
|
118
|
-
} catch {
|
|
68
|
+
const config = readAudioSpecificConfig(bytes);
|
|
69
|
+
if (!config) {
|
|
119
70
|
return null;
|
|
120
71
|
}
|
|
72
|
+
return {
|
|
73
|
+
objectType: config.objectType,
|
|
74
|
+
sampleRate: config.sampleRate > 0 ? config.sampleRate : null,
|
|
75
|
+
channels: config.channels,
|
|
76
|
+
// Only the AAC-LC bound is stated for a frame length, so only AAC-LC reports one.
|
|
77
|
+
frameLength: config.objectType === AAC_LC ? config.frameLength : null
|
|
78
|
+
};
|
|
121
79
|
}
|
|
122
80
|
|
|
123
81
|
/**
|