@torrent-tv/proxy 2.107.62 → 2.107.64
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/encode/EncodeInputs.js +3 -10
- package/services/encode/OutputWaitLedger.js +3 -1
- package/services/encode/PauseRule.js +3 -7
- package/services/encode/available-share.js +2 -10
- package/services/encode/learned-median.js +3 -38
- package/services/encode/quality/HostLoad.js +5 -4
- package/services/encode/run-costs.js +5 -22
- package/services/media/SubtitleCues.js +2 -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/services/server/ViewerRequests.js +2 -1
- package/services/server/wire-outputs.js +3 -2
- package/services/storage/Diagnostics.js +1 -7
- package/services/storage/MachineBudget.js +10 -6
- package/services/storage/machine-processor.js +1 -10
- package/services/storage/memory-report.js +17 -26
- package/services/storage/piece-store/shared-piece-store.js +6 -7
- package/services/storage/processor-report.js +2 -7
- package/services/storage/returns.js +3 -2
- package/services/storage/segment-store/SegmentStore.js +2 -1
- package/services/torrent/supply-margin.js +3 -11
- package/services/torrent/torrent-pool.js +4 -3
- package/services/torrent/worker/piece-reader.js +2 -5
- package/services/torrent/worker/worker.js +12 -11
- package/utils/clamp.js +11 -0
- package/utils/median.js +66 -0
- package/utils/units.js +35 -0
|
@@ -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); }
|