@svta/cml-608 0.19.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/README.md +15 -0
  2. package/dist/index.d.ts +406 -20
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +1173 -15
  5. package/dist/index.js.map +1 -1
  6. package/package.json +4 -28
  7. package/NOTICE +0 -59
  8. package/dist/CaptionModes.d.ts +0 -7
  9. package/dist/CaptionModes.d.ts.map +0 -1
  10. package/dist/CaptionModes.js +0 -2
  11. package/dist/CaptionModes.js.map +0 -1
  12. package/dist/CaptionScreen.d.ts +0 -80
  13. package/dist/CaptionScreen.d.ts.map +0 -1
  14. package/dist/CaptionScreen.js +0 -220
  15. package/dist/CaptionScreen.js.map +0 -1
  16. package/dist/CaptionsLogger.d.ts +0 -49
  17. package/dist/CaptionsLogger.d.ts.map +0 -1
  18. package/dist/CaptionsLogger.js +0 -56
  19. package/dist/CaptionsLogger.js.map +0 -1
  20. package/dist/Channels.d.ts +0 -7
  21. package/dist/Channels.d.ts.map +0 -1
  22. package/dist/Channels.js +0 -2
  23. package/dist/Channels.js.map +0 -1
  24. package/dist/CmdHistory.d.ts +0 -10
  25. package/dist/CmdHistory.d.ts.map +0 -1
  26. package/dist/CmdHistory.js +0 -2
  27. package/dist/CmdHistory.js.map +0 -1
  28. package/dist/Cta608Channel.d.ts +0 -88
  29. package/dist/Cta608Channel.d.ts.map +0 -1
  30. package/dist/Cta608Channel.js +0 -262
  31. package/dist/Cta608Channel.js.map +0 -1
  32. package/dist/Cta608Parser.d.ts +0 -115
  33. package/dist/Cta608Parser.d.ts.map +0 -1
  34. package/dist/Cta608Parser.js +0 -428
  35. package/dist/Cta608Parser.js.map +0 -1
  36. package/dist/CueHandler.d.ts +0 -12
  37. package/dist/CueHandler.d.ts.map +0 -1
  38. package/dist/CueHandler.js +0 -2
  39. package/dist/CueHandler.js.map +0 -1
  40. package/dist/PACData.d.ts +0 -13
  41. package/dist/PACData.d.ts.map +0 -1
  42. package/dist/PACData.js +0 -2
  43. package/dist/PACData.js.map +0 -1
  44. package/dist/PenState.d.ts +0 -56
  45. package/dist/PenState.d.ts.map +0 -1
  46. package/dist/PenState.js +0 -95
  47. package/dist/PenState.js.map +0 -1
  48. package/dist/PenStyles.d.ts +0 -13
  49. package/dist/PenStyles.d.ts.map +0 -1
  50. package/dist/PenStyles.js +0 -2
  51. package/dist/PenStyles.js.map +0 -1
  52. package/dist/Row.d.ts +0 -75
  53. package/dist/Row.d.ts.map +0 -1
  54. package/dist/Row.js +0 -174
  55. package/dist/Row.js.map +0 -1
  56. package/dist/SccParser.d.ts +0 -49
  57. package/dist/SccParser.d.ts.map +0 -1
  58. package/dist/SccParser.js +0 -99
  59. package/dist/SccParser.js.map +0 -1
  60. package/dist/StyledUnicodeChar.d.ts +0 -54
  61. package/dist/StyledUnicodeChar.d.ts.map +0 -1
  62. package/dist/StyledUnicodeChar.js +0 -71
  63. package/dist/StyledUnicodeChar.js.map +0 -1
  64. package/dist/SupportedField.d.ts +0 -7
  65. package/dist/SupportedField.d.ts.map +0 -1
  66. package/dist/SupportedField.js +0 -2
  67. package/dist/SupportedField.js.map +0 -1
  68. package/dist/VerboseLevel.d.ts +0 -22
  69. package/dist/VerboseLevel.d.ts.map +0 -1
  70. package/dist/VerboseLevel.js +0 -17
  71. package/dist/VerboseLevel.js.map +0 -1
  72. package/dist/extractCta608Data.d.ts +0 -11
  73. package/dist/extractCta608Data.d.ts.map +0 -1
  74. package/dist/extractCta608Data.js +0 -38
  75. package/dist/extractCta608Data.js.map +0 -1
  76. package/dist/extractCta608DataFromSample.d.ts +0 -12
  77. package/dist/extractCta608DataFromSample.d.ts.map +0 -1
  78. package/dist/extractCta608DataFromSample.js +0 -35
  79. package/dist/extractCta608DataFromSample.js.map +0 -1
  80. package/dist/findCta608Nalus.d.ts +0 -12
  81. package/dist/findCta608Nalus.d.ts.map +0 -1
  82. package/dist/findCta608Nalus.js +0 -60
  83. package/dist/findCta608Nalus.js.map +0 -1
  84. package/dist/tsconfig.tsbuildinfo +0 -1
  85. package/dist/utils/NR_COLS.d.ts +0 -2
  86. package/dist/utils/NR_COLS.d.ts.map +0 -1
  87. package/dist/utils/NR_COLS.js +0 -2
  88. package/dist/utils/NR_COLS.js.map +0 -1
  89. package/dist/utils/NR_ROWS.d.ts +0 -2
  90. package/dist/utils/NR_ROWS.d.ts.map +0 -1
  91. package/dist/utils/NR_ROWS.js +0 -2
  92. package/dist/utils/NR_ROWS.js.map +0 -1
  93. package/dist/utils/backgroundColors.d.ts +0 -2
  94. package/dist/utils/backgroundColors.d.ts.map +0 -1
  95. package/dist/utils/backgroundColors.js +0 -12
  96. package/dist/utils/backgroundColors.js.map +0 -1
  97. package/dist/utils/createCmdHistory.d.ts +0 -3
  98. package/dist/utils/createCmdHistory.d.ts.map +0 -1
  99. package/dist/utils/createCmdHistory.js +0 -7
  100. package/dist/utils/createCmdHistory.js.map +0 -1
  101. package/dist/utils/getCharForByte.d.ts +0 -2
  102. package/dist/utils/getCharForByte.d.ts.map +0 -1
  103. package/dist/utils/getCharForByte.js +0 -6
  104. package/dist/utils/getCharForByte.js.map +0 -1
  105. package/dist/utils/hasCmdRepeated.d.ts +0 -3
  106. package/dist/utils/hasCmdRepeated.d.ts.map +0 -1
  107. package/dist/utils/hasCmdRepeated.js +0 -4
  108. package/dist/utils/hasCmdRepeated.js.map +0 -1
  109. package/dist/utils/index.d.ts +0 -20
  110. package/dist/utils/index.d.ts.map +0 -1
  111. package/dist/utils/index.js +0 -20
  112. package/dist/utils/index.js.map +0 -1
  113. package/dist/utils/numArrayToHexArray.d.ts +0 -2
  114. package/dist/utils/numArrayToHexArray.d.ts.map +0 -1
  115. package/dist/utils/numArrayToHexArray.js +0 -9
  116. package/dist/utils/numArrayToHexArray.js.map +0 -1
  117. package/dist/utils/rowsHighCh1.d.ts +0 -4
  118. package/dist/utils/rowsHighCh1.d.ts.map +0 -1
  119. package/dist/utils/rowsHighCh1.js +0 -10
  120. package/dist/utils/rowsHighCh1.js.map +0 -1
  121. package/dist/utils/rowsHighCh2.d.ts +0 -4
  122. package/dist/utils/rowsHighCh2.d.ts.map +0 -1
  123. package/dist/utils/rowsHighCh2.js +0 -10
  124. package/dist/utils/rowsHighCh2.js.map +0 -1
  125. package/dist/utils/rowsLowCh1.d.ts +0 -4
  126. package/dist/utils/rowsLowCh1.d.ts.map +0 -1
  127. package/dist/utils/rowsLowCh1.js +0 -12
  128. package/dist/utils/rowsLowCh1.js.map +0 -1
  129. package/dist/utils/rowsLowCh2.d.ts +0 -4
  130. package/dist/utils/rowsLowCh2.d.ts.map +0 -1
  131. package/dist/utils/rowsLowCh2.js +0 -11
  132. package/dist/utils/rowsLowCh2.js.map +0 -1
  133. package/dist/utils/seiHelpers.d.ts +0 -7
  134. package/dist/utils/seiHelpers.d.ts.map +0 -1
  135. package/dist/utils/seiHelpers.js +0 -78
  136. package/dist/utils/seiHelpers.js.map +0 -1
  137. package/dist/utils/setLastCmd.d.ts +0 -3
  138. package/dist/utils/setLastCmd.d.ts.map +0 -1
  139. package/dist/utils/setLastCmd.js +0 -5
  140. package/dist/utils/setLastCmd.js.map +0 -1
  141. package/dist/utils/specialCea608CharsCodes.d.ts +0 -7
  142. package/dist/utils/specialCea608CharsCodes.d.ts.map +0 -1
  143. package/dist/utils/specialCea608CharsCodes.js +0 -103
  144. package/dist/utils/specialCea608CharsCodes.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,16 +1,1174 @@
1
- export * from './CaptionScreen.js';
2
- export * from './CaptionsLogger.js';
3
- export * from './Cta608Channel.js';
4
- export * from './Cta608Parser.js';
5
- export * from './CueHandler.js';
6
- export * from './extractCta608Data.js';
7
- export * from './findCta608Nalus.js';
8
- export * from './PACData.js';
9
- export * from './PenState.js';
10
- export * from './PenStyles.js';
11
- export * from './Row.js';
12
- export * from './SccParser.js';
13
- export * from './StyledUnicodeChar.js';
14
- export * from './SupportedField.js';
15
- export * from './VerboseLevel.js';
1
+ //#region src/VerboseLevel.ts
2
+ /**
3
+ * Logging levels for the CTA-608 parser.
4
+ *
5
+ *
6
+ * @enum
7
+ *
8
+ * @beta
9
+ */
10
+ const VerboseLevel = {
11
+ ERROR: 0,
12
+ TEXT: 1,
13
+ WARNING: 2,
14
+ INFO: 2,
15
+ DEBUG: 3,
16
+ DATA: 3
17
+ };
18
+
19
+ //#endregion
20
+ //#region src/CaptionsLogger.ts
21
+ /**
22
+ * CaptionsLogger. To be removed in the future.
23
+ *
24
+ * @beta
25
+ */
26
+ var CaptionsLogger = class {
27
+ constructor() {
28
+ this.time = null;
29
+ this.verboseLevel = VerboseLevel.ERROR;
30
+ }
31
+ log(severity, msg) {
32
+ if (this.verboseLevel >= severity) {
33
+ const m = typeof msg === "function" ? msg() : msg;
34
+ console.log(`${this.time} [${severity}] ${m}`);
35
+ }
36
+ }
37
+ };
38
+
39
+ //#endregion
40
+ //#region src/PenState.ts
41
+ /**
42
+ * Pen state.
43
+ *
44
+ * @beta
45
+ */
46
+ var PenState = class {
47
+ constructor() {
48
+ this.foreground = "white";
49
+ this.underline = false;
50
+ this.italics = false;
51
+ this.background = "black";
52
+ this.flash = false;
53
+ }
54
+ reset() {
55
+ this.foreground = "white";
56
+ this.underline = false;
57
+ this.italics = false;
58
+ this.background = "black";
59
+ this.flash = false;
60
+ }
61
+ setStyles(styles) {
62
+ for (const style of [
63
+ "foreground",
64
+ "underline",
65
+ "italics",
66
+ "background",
67
+ "flash"
68
+ ]) if (Object.prototype.hasOwnProperty.call(styles, style)) this[style] = styles[style];
69
+ }
70
+ isDefault() {
71
+ return this.foreground === "white" && !this.underline && !this.italics && this.background === "black" && !this.flash;
72
+ }
73
+ equals(other) {
74
+ return this.foreground === other.foreground && this.underline === other.underline && this.italics === other.italics && this.background === other.background && this.flash === other.flash;
75
+ }
76
+ copy(newPenState) {
77
+ this.foreground = newPenState.foreground;
78
+ this.underline = newPenState.underline;
79
+ this.italics = newPenState.italics;
80
+ this.background = newPenState.background;
81
+ this.flash = newPenState.flash;
82
+ }
83
+ };
84
+
85
+ //#endregion
86
+ //#region src/StyledUnicodeChar.ts
87
+ /**
88
+ * Unicode character with styling and background.
89
+ *
90
+ * @beta
91
+ */
92
+ var StyledUnicodeChar = class {
93
+ constructor() {
94
+ this.uchar = " ";
95
+ this.penState = new PenState();
96
+ }
97
+ reset() {
98
+ this.uchar = " ";
99
+ this.penState.reset();
100
+ }
101
+ setChar(uchar, newPenState) {
102
+ this.uchar = uchar;
103
+ this.penState.copy(newPenState);
104
+ }
105
+ setPenState(newPenState) {
106
+ this.penState.copy(newPenState);
107
+ }
108
+ equals(other) {
109
+ return this.uchar === other.uchar && this.penState.equals(other.penState);
110
+ }
111
+ copy(newChar) {
112
+ this.uchar = newChar.uchar;
113
+ this.penState.copy(newChar.penState);
114
+ }
115
+ isEmpty() {
116
+ return this.uchar === " " && this.penState.isDefault();
117
+ }
118
+ };
119
+
120
+ //#endregion
121
+ //#region src/utils/NR_COLS.ts
122
+ const NR_COLS = 100;
123
+
124
+ //#endregion
125
+ //#region src/utils/specialCea608CharsCodes.ts
126
+ /**
127
+ * Exceptions from regular ASCII. CodePoints are mapped to UTF-16 codes
128
+ */
129
+ const specialCea608CharsCodes = {
130
+ 42: 225,
131
+ 92: 233,
132
+ 94: 237,
133
+ 95: 243,
134
+ 96: 250,
135
+ 123: 231,
136
+ 124: 247,
137
+ 125: 209,
138
+ 126: 241,
139
+ 127: 9608,
140
+ 128: 174,
141
+ 129: 176,
142
+ 130: 189,
143
+ 131: 191,
144
+ 132: 8482,
145
+ 133: 162,
146
+ 134: 163,
147
+ 135: 9834,
148
+ 136: 224,
149
+ 137: 32,
150
+ 138: 232,
151
+ 139: 226,
152
+ 140: 234,
153
+ 141: 238,
154
+ 142: 244,
155
+ 143: 251,
156
+ 144: 193,
157
+ 145: 201,
158
+ 146: 211,
159
+ 147: 218,
160
+ 148: 220,
161
+ 149: 252,
162
+ 150: 8216,
163
+ 151: 161,
164
+ 152: 42,
165
+ 153: 8217,
166
+ 154: 9473,
167
+ 155: 169,
168
+ 156: 8480,
169
+ 157: 8226,
170
+ 158: 8220,
171
+ 159: 8221,
172
+ 160: 192,
173
+ 161: 194,
174
+ 162: 199,
175
+ 163: 200,
176
+ 164: 202,
177
+ 165: 203,
178
+ 166: 235,
179
+ 167: 206,
180
+ 168: 207,
181
+ 169: 239,
182
+ 170: 212,
183
+ 171: 217,
184
+ 172: 249,
185
+ 173: 219,
186
+ 174: 171,
187
+ 175: 187,
188
+ 176: 195,
189
+ 177: 227,
190
+ 178: 205,
191
+ 179: 204,
192
+ 180: 236,
193
+ 181: 210,
194
+ 182: 242,
195
+ 183: 213,
196
+ 184: 245,
197
+ 185: 123,
198
+ 186: 125,
199
+ 187: 92,
200
+ 188: 94,
201
+ 189: 95,
202
+ 190: 124,
203
+ 191: 8764,
204
+ 192: 196,
205
+ 193: 228,
206
+ 194: 214,
207
+ 195: 246,
208
+ 196: 223,
209
+ 197: 165,
210
+ 198: 164,
211
+ 199: 9475,
212
+ 200: 197,
213
+ 201: 229,
214
+ 202: 216,
215
+ 203: 248,
216
+ 204: 9487,
217
+ 205: 9491,
218
+ 206: 9495,
219
+ 207: 9499
220
+ };
221
+
222
+ //#endregion
223
+ //#region src/utils/getCharForByte.ts
224
+ function getCharForByte(byte) {
225
+ return String.fromCharCode(specialCea608CharsCodes[byte] || byte);
226
+ }
227
+
228
+ //#endregion
229
+ //#region src/Row.ts
230
+ /**
231
+ * CTA-608 row consisting of NR_COLS instances of StyledUnicodeChar.
232
+ *
233
+ * @beta
234
+ */
235
+ var Row = class {
236
+ constructor(logger = new CaptionsLogger()) {
237
+ this.chars = [];
238
+ this.cueStartTime = null;
239
+ this.pos = 0;
240
+ this.currPenState = new PenState();
241
+ for (let i = 0; i < NR_COLS; i++) this.chars.push(new StyledUnicodeChar());
242
+ this.logger = logger;
243
+ }
244
+ equals(other) {
245
+ for (let i = 0; i < NR_COLS; i++) if (!this.chars[i].equals(other.chars[i])) return false;
246
+ return true;
247
+ }
248
+ copy(other) {
249
+ for (let i = 0; i < NR_COLS; i++) this.chars[i].copy(other.chars[i]);
250
+ }
251
+ isEmpty() {
252
+ let empty = true;
253
+ for (let i = 0; i < NR_COLS; i++) if (!this.chars[i].isEmpty()) {
254
+ empty = false;
255
+ break;
256
+ }
257
+ return empty;
258
+ }
259
+ /**
260
+ * Set the cursor to a valid column.
261
+ */
262
+ setCursor(absPos) {
263
+ if (this.pos !== absPos) this.pos = absPos;
264
+ if (this.pos < 0) {
265
+ this.logger.log(VerboseLevel.DEBUG, "Negative cursor position " + this.pos);
266
+ this.pos = 0;
267
+ } else if (this.pos > NR_COLS) {
268
+ this.logger.log(VerboseLevel.DEBUG, "Too large cursor position " + this.pos);
269
+ this.pos = NR_COLS;
270
+ }
271
+ }
272
+ /**
273
+ * Move the cursor relative to current position.
274
+ */
275
+ moveCursor(relPos) {
276
+ const newPos = this.pos + relPos;
277
+ if (relPos > 1) for (let i = this.pos + 1; i < newPos + 1; i++) this.chars[i].setPenState(this.currPenState);
278
+ this.setCursor(newPos);
279
+ }
280
+ /**
281
+ * Backspace, move one step back and clear character.
282
+ */
283
+ backSpace() {
284
+ this.moveCursor(-1);
285
+ this.chars[this.pos].setChar(" ", this.currPenState);
286
+ }
287
+ insertChar(byte) {
288
+ if (byte >= 144) this.backSpace();
289
+ const char = getCharForByte(byte);
290
+ if (this.pos >= NR_COLS) {
291
+ this.logger.log(VerboseLevel.ERROR, () => "Cannot insert " + byte.toString(16) + " (" + char + ") at position " + this.pos + ". Skipping it!");
292
+ return;
293
+ }
294
+ this.chars[this.pos].setChar(char, this.currPenState);
295
+ this.moveCursor(1);
296
+ }
297
+ clearFromPos(startPos) {
298
+ let i;
299
+ for (i = startPos; i < NR_COLS; i++) this.chars[i].reset();
300
+ }
301
+ clear() {
302
+ this.clearFromPos(0);
303
+ this.pos = 0;
304
+ this.currPenState.reset();
305
+ }
306
+ clearToEndOfRow() {
307
+ this.clearFromPos(this.pos);
308
+ }
309
+ getTextString() {
310
+ const chars = [];
311
+ let empty = true;
312
+ for (let i = 0; i < NR_COLS; i++) {
313
+ const char = this.chars[i].uchar;
314
+ if (char !== " ") empty = false;
315
+ chars.push(char);
316
+ }
317
+ if (empty) return "";
318
+ else return chars.join("");
319
+ }
320
+ setPenStyles(styles) {
321
+ this.currPenState.setStyles(styles);
322
+ this.chars[this.pos].setPenState(this.currPenState);
323
+ }
324
+ };
325
+
326
+ //#endregion
327
+ //#region src/utils/NR_ROWS.ts
328
+ const NR_ROWS = 15;
329
+
330
+ //#endregion
331
+ //#region src/CaptionScreen.ts
332
+ /**
333
+ * Keep a CTA-608 screen of 32x15 styled characters
334
+ *
335
+ * @beta
336
+ */
337
+ var CaptionScreen = class {
338
+ constructor(logger = new CaptionsLogger()) {
339
+ this.rows = [];
340
+ this.currRow = NR_ROWS - 1;
341
+ this.nrRollUpRows = null;
342
+ this.lastOutputScreen = null;
343
+ for (let i = 0; i < NR_ROWS; i++) this.rows.push(new Row(logger));
344
+ this.logger = logger;
345
+ }
346
+ reset() {
347
+ for (let i = 0; i < NR_ROWS; i++) this.rows[i].clear();
348
+ this.currRow = NR_ROWS - 1;
349
+ }
350
+ equals(other) {
351
+ let equal = true;
352
+ for (let i = 0; i < NR_ROWS; i++) if (!this.rows[i].equals(other.rows[i])) {
353
+ equal = false;
354
+ break;
355
+ }
356
+ return equal;
357
+ }
358
+ copy(other) {
359
+ for (let i = 0; i < NR_ROWS; i++) this.rows[i].copy(other.rows[i]);
360
+ }
361
+ isEmpty() {
362
+ let empty = true;
363
+ for (let i = 0; i < NR_ROWS; i++) if (!this.rows[i].isEmpty()) {
364
+ empty = false;
365
+ break;
366
+ }
367
+ return empty;
368
+ }
369
+ backSpace() {
370
+ this.rows[this.currRow].backSpace();
371
+ }
372
+ clearToEndOfRow() {
373
+ this.rows[this.currRow].clearToEndOfRow();
374
+ }
375
+ /**
376
+ * Insert a character (without styling) in the current row.
377
+ */
378
+ insertChar(char) {
379
+ this.rows[this.currRow].insertChar(char);
380
+ }
381
+ setPen(styles) {
382
+ this.rows[this.currRow].setPenStyles(styles);
383
+ }
384
+ moveCursor(relPos) {
385
+ this.rows[this.currRow].moveCursor(relPos);
386
+ }
387
+ setCursor(absPos) {
388
+ this.logger.log(VerboseLevel.INFO, "setCursor: " + absPos);
389
+ this.rows[this.currRow].setCursor(absPos);
390
+ }
391
+ setPAC(pacData) {
392
+ this.logger.log(VerboseLevel.INFO, () => "pacData = " + JSON.stringify(pacData));
393
+ let newRow = pacData.row - 1;
394
+ if (this.nrRollUpRows && newRow < this.nrRollUpRows - 1) newRow = this.nrRollUpRows - 1;
395
+ if (this.nrRollUpRows && this.currRow !== newRow) {
396
+ for (let i = 0; i < NR_ROWS; i++) this.rows[i].clear();
397
+ const topRowIndex = this.currRow + 1 - this.nrRollUpRows;
398
+ const lastOutputScreen = this.lastOutputScreen;
399
+ if (lastOutputScreen) {
400
+ const prevLineTime = lastOutputScreen.rows[topRowIndex].cueStartTime;
401
+ const time = this.logger.time;
402
+ if (prevLineTime !== null && time !== null && prevLineTime < time) for (let i = 0; i < this.nrRollUpRows; i++) this.rows[newRow - this.nrRollUpRows + i + 1].copy(lastOutputScreen.rows[topRowIndex + i]);
403
+ }
404
+ }
405
+ this.currRow = newRow;
406
+ const row = this.rows[this.currRow];
407
+ if (pacData.indent !== null) {
408
+ const indent = pacData.indent;
409
+ const prevPos = Math.max(indent - 1, 0);
410
+ row.setCursor(pacData.indent);
411
+ pacData.color = row.chars[prevPos].penState.foreground;
412
+ }
413
+ const styles = {
414
+ foreground: pacData.color,
415
+ underline: pacData.underline,
416
+ italics: pacData.italics,
417
+ background: "black",
418
+ flash: false
419
+ };
420
+ this.setPen(styles);
421
+ }
422
+ /**
423
+ * Set background/extra foreground, but first do back_space, and then insert space (backwards compatibility).
424
+ */
425
+ setBkgData(bkgData) {
426
+ this.logger.log(VerboseLevel.INFO, () => "bkgData = " + JSON.stringify(bkgData));
427
+ this.backSpace();
428
+ this.setPen(bkgData);
429
+ this.insertChar(32);
430
+ }
431
+ setRollUpRows(nrRows) {
432
+ this.nrRollUpRows = nrRows;
433
+ }
434
+ rollUp() {
435
+ if (this.nrRollUpRows === null) {
436
+ this.logger.log(VerboseLevel.DEBUG, "roll_up but nrRollUpRows not set yet");
437
+ return;
438
+ }
439
+ this.logger.log(VerboseLevel.TEXT, () => this.getDisplayText());
440
+ const topRowIndex = this.currRow + 1 - this.nrRollUpRows;
441
+ const topRow = this.rows.splice(topRowIndex, 1)[0];
442
+ topRow.clear();
443
+ this.rows.splice(this.currRow, 0, topRow);
444
+ this.logger.log(VerboseLevel.INFO, "Rolling up");
445
+ }
446
+ /**
447
+ * Get all non-empty rows with as unicode text.
448
+ */
449
+ getDisplayText(asOneRow) {
450
+ asOneRow = asOneRow || false;
451
+ const displayText = [];
452
+ let text = "";
453
+ let rowNr = -1;
454
+ for (let i = 0; i < NR_ROWS; i++) {
455
+ const rowText = this.rows[i].getTextString();
456
+ if (rowText) {
457
+ rowNr = i + 1;
458
+ if (asOneRow) displayText.push("Row " + rowNr + ": '" + rowText + "'");
459
+ else displayText.push(rowText.trim());
460
+ }
461
+ }
462
+ if (displayText.length > 0) if (asOneRow) text = "[" + displayText.join(" | ") + "]";
463
+ else text = displayText.join("\n");
464
+ return text;
465
+ }
466
+ getTextAndFormat() {
467
+ return this.rows;
468
+ }
469
+ };
470
+
471
+ //#endregion
472
+ //#region src/Cta608Channel.ts
473
+ /**
474
+ * CTA-608 Channel
475
+ *
476
+ * @beta
477
+ */
478
+ var Cta608Channel = class {
479
+ constructor(channelNumber, outputFilter, logger = new CaptionsLogger()) {
480
+ this.chNr = channelNumber;
481
+ this.outputFilter = outputFilter;
482
+ this.mode = null;
483
+ this.displayedMemory = new CaptionScreen(logger);
484
+ this.nonDisplayedMemory = new CaptionScreen(logger);
485
+ this.lastOutputScreen = new CaptionScreen(logger);
486
+ this.currRollUpRow = this.displayedMemory.rows[NR_ROWS - 1];
487
+ this.writeScreen = this.displayedMemory;
488
+ this.mode = null;
489
+ this.cueStartTime = null;
490
+ this.logger = logger;
491
+ this.logger.log(VerboseLevel.INFO, "new Cea608Channel(" + this.chNr + ")");
492
+ }
493
+ reset() {
494
+ this.mode = null;
495
+ this.displayedMemory.reset();
496
+ this.nonDisplayedMemory.reset();
497
+ this.lastOutputScreen.reset();
498
+ this.outputFilter?.reset?.();
499
+ this.currRollUpRow = this.displayedMemory.rows[NR_ROWS - 1];
500
+ this.writeScreen = this.displayedMemory;
501
+ this.mode = null;
502
+ this.cueStartTime = null;
503
+ }
504
+ getHandler() {
505
+ return this.outputFilter;
506
+ }
507
+ setHandler(outputFilter) {
508
+ this.outputFilter = outputFilter;
509
+ }
510
+ setPAC(pacData) {
511
+ this.writeScreen.setPAC(pacData);
512
+ }
513
+ setBkgData(bkgData) {
514
+ this.writeScreen.setBkgData(bkgData);
515
+ }
516
+ setMode(newMode) {
517
+ if (newMode === this.mode) return;
518
+ this.mode = newMode;
519
+ this.logger.log(VerboseLevel.INFO, () => "MODE=" + newMode);
520
+ if (this.mode === "MODE_POP-ON") this.writeScreen = this.nonDisplayedMemory;
521
+ else {
522
+ this.writeScreen = this.displayedMemory;
523
+ this.writeScreen.reset();
524
+ }
525
+ if (this.mode !== "MODE_ROLL-UP") {
526
+ this.displayedMemory.setRollUpRows(null);
527
+ this.nonDisplayedMemory.setRollUpRows(null);
528
+ }
529
+ this.mode = newMode;
530
+ }
531
+ insertChars(chars) {
532
+ for (const char of chars) this.writeScreen.insertChar(char);
533
+ const screen = this.writeScreen === this.displayedMemory ? "DISP" : "NON_DISP";
534
+ this.logger.log(VerboseLevel.INFO, () => screen + ": " + this.writeScreen.getDisplayText(true));
535
+ if (this.mode === "MODE_PAINT-ON" || this.mode === "MODE_ROLL-UP") {
536
+ this.logger.log(VerboseLevel.TEXT, () => "DISPLAYED: " + this.displayedMemory.getDisplayText(true));
537
+ this.outputDataUpdate();
538
+ }
539
+ }
540
+ ccRCL() {
541
+ this.logger.log(VerboseLevel.INFO, "RCL - Resume Caption Loading");
542
+ this.setMode("MODE_POP-ON");
543
+ }
544
+ ccBS() {
545
+ this.logger.log(VerboseLevel.INFO, "BS - BackSpace");
546
+ if (this.mode === "MODE_TEXT") return;
547
+ this.writeScreen.backSpace();
548
+ if (this.writeScreen === this.displayedMemory) this.outputDataUpdate();
549
+ }
550
+ ccAOF() {}
551
+ ccAON() {}
552
+ ccDER() {
553
+ this.logger.log(VerboseLevel.INFO, "DER- Delete to End of Row");
554
+ this.writeScreen.clearToEndOfRow();
555
+ this.outputDataUpdate();
556
+ }
557
+ ccRU(nrRows) {
558
+ this.logger.log(VerboseLevel.INFO, "RU(" + nrRows + ") - Roll Up");
559
+ this.writeScreen = this.displayedMemory;
560
+ this.setMode("MODE_ROLL-UP");
561
+ this.writeScreen.setRollUpRows(nrRows);
562
+ }
563
+ ccFON() {
564
+ this.logger.log(VerboseLevel.INFO, "FON - Flash On");
565
+ this.writeScreen.setPen({ flash: true });
566
+ }
567
+ ccRDC() {
568
+ this.logger.log(VerboseLevel.INFO, "RDC - Resume Direct Captioning");
569
+ this.setMode("MODE_PAINT-ON");
570
+ }
571
+ ccTR() {
572
+ this.logger.log(VerboseLevel.INFO, "TR");
573
+ this.setMode("MODE_TEXT");
574
+ }
575
+ ccRTD() {
576
+ this.logger.log(VerboseLevel.INFO, "RTD");
577
+ this.setMode("MODE_TEXT");
578
+ }
579
+ ccEDM() {
580
+ this.logger.log(VerboseLevel.INFO, "EDM - Erase Displayed Memory");
581
+ this.displayedMemory.reset();
582
+ this.outputDataUpdate(true);
583
+ }
584
+ ccCR() {
585
+ this.logger.log(VerboseLevel.INFO, "CR - Carriage Return");
586
+ this.writeScreen.rollUp();
587
+ this.outputDataUpdate(true);
588
+ }
589
+ ccENM() {
590
+ this.logger.log(VerboseLevel.INFO, "ENM - Erase Non-displayed Memory");
591
+ this.nonDisplayedMemory.reset();
592
+ }
593
+ ccEOC() {
594
+ this.logger.log(VerboseLevel.INFO, "EOC - End Of Caption");
595
+ if (this.mode === "MODE_POP-ON") {
596
+ const tmp = this.displayedMemory;
597
+ this.displayedMemory = this.nonDisplayedMemory;
598
+ this.nonDisplayedMemory = tmp;
599
+ this.writeScreen = this.nonDisplayedMemory;
600
+ this.logger.log(VerboseLevel.TEXT, () => "DISP: " + this.displayedMemory.getDisplayText());
601
+ }
602
+ this.outputDataUpdate(true);
603
+ }
604
+ ccTO(nrCols) {
605
+ this.logger.log(VerboseLevel.INFO, "TO(" + nrCols + ") - Tab Offset");
606
+ this.writeScreen.moveCursor(nrCols);
607
+ }
608
+ ccMIDROW(secondByte) {
609
+ const styles = { flash: false };
610
+ styles.underline = secondByte % 2 === 1;
611
+ styles.italics = secondByte >= 46;
612
+ if (!styles.italics) styles.foreground = [
613
+ "white",
614
+ "green",
615
+ "blue",
616
+ "cyan",
617
+ "red",
618
+ "yellow",
619
+ "magenta"
620
+ ][Math.floor(secondByte / 2) - 16];
621
+ else styles.foreground = "white";
622
+ this.logger.log(VerboseLevel.INFO, "MIDROW: " + JSON.stringify(styles));
623
+ this.writeScreen.setPen(styles);
624
+ }
625
+ outputDataUpdate(dispatch = false) {
626
+ const time = this.logger.time;
627
+ if (time === null) return;
628
+ if (this.outputFilter) {
629
+ if (this.cueStartTime === null && !this.displayedMemory.isEmpty()) this.cueStartTime = time;
630
+ else if (!this.displayedMemory.equals(this.lastOutputScreen)) {
631
+ this.outputFilter.newCue(this.cueStartTime, time, this.lastOutputScreen);
632
+ if (dispatch && this.outputFilter.dispatchCue) this.outputFilter.dispatchCue();
633
+ this.cueStartTime = this.displayedMemory.isEmpty() ? null : time;
634
+ }
635
+ this.lastOutputScreen.copy(this.displayedMemory);
636
+ }
637
+ }
638
+ cueSplitAtTime(t) {
639
+ if (this.outputFilter) {
640
+ if (!this.displayedMemory.isEmpty()) {
641
+ if (this.outputFilter.newCue) this.outputFilter.newCue(this.cueStartTime, t, this.displayedMemory);
642
+ this.cueStartTime = t;
643
+ }
644
+ }
645
+ }
646
+ };
647
+
648
+ //#endregion
649
+ //#region src/utils/backgroundColors.ts
650
+ const backgroundColors = [
651
+ "white",
652
+ "green",
653
+ "blue",
654
+ "cyan",
655
+ "red",
656
+ "yellow",
657
+ "magenta",
658
+ "black",
659
+ "transparent"
660
+ ];
661
+
662
+ //#endregion
663
+ //#region src/utils/createCmdHistory.ts
664
+ function createCmdHistory() {
665
+ return {
666
+ a: null,
667
+ b: null
668
+ };
669
+ }
670
+
671
+ //#endregion
672
+ //#region src/utils/hasCmdRepeated.ts
673
+ function hasCmdRepeated(a, b, cmdHistory) {
674
+ return cmdHistory.a === a && cmdHistory.b === b;
675
+ }
676
+
677
+ //#endregion
678
+ //#region src/utils/numArrayToHexArray.ts
679
+ function numArrayToHexArray(numArray) {
680
+ const hexArray = [];
681
+ for (const num of numArray) hexArray.push(num.toString(16));
682
+ return hexArray;
683
+ }
684
+
685
+ //#endregion
686
+ //#region src/utils/rowsHighCh1.ts
687
+ const rowsHighCh1 = {
688
+ 17: 2,
689
+ 18: 4,
690
+ 21: 6,
691
+ 22: 8,
692
+ 23: 10,
693
+ 19: 13,
694
+ 20: 15
695
+ };
696
+
697
+ //#endregion
698
+ //#region src/utils/rowsHighCh2.ts
699
+ const rowsHighCh2 = {
700
+ 25: 2,
701
+ 26: 4,
702
+ 29: 6,
703
+ 30: 8,
704
+ 31: 10,
705
+ 27: 13,
706
+ 28: 15
707
+ };
708
+
709
+ //#endregion
710
+ //#region src/utils/rowsLowCh1.ts
711
+ const rowsLowCh1 = {
712
+ 17: 1,
713
+ 18: 3,
714
+ 21: 5,
715
+ 22: 7,
716
+ 23: 9,
717
+ 16: 11,
718
+ 19: 12,
719
+ 20: 14
720
+ };
721
+
722
+ //#endregion
723
+ //#region src/utils/rowsLowCh2.ts
724
+ const rowsLowCh2 = {
725
+ 25: 1,
726
+ 26: 3,
727
+ 29: 5,
728
+ 30: 7,
729
+ 31: 9,
730
+ 24: 11,
731
+ 27: 12,
732
+ 28: 14
733
+ };
734
+
735
+ //#endregion
736
+ //#region src/utils/setLastCmd.ts
737
+ function setLastCmd(a, b, cmdHistory) {
738
+ cmdHistory.a = a;
739
+ cmdHistory.b = b;
740
+ }
741
+
742
+ //#endregion
743
+ //#region src/Cta608Parser.ts
744
+ /**
745
+ * CEA-608 caption parser.
746
+ *
747
+ * @beta
748
+ */
749
+ var Cta608Parser = class {
750
+ constructor(field, out1, out2) {
751
+ this.currentChannel = 0;
752
+ this.cmdHistory = createCmdHistory();
753
+ this.lastTime = null;
754
+ const logger = this.logger = new CaptionsLogger();
755
+ this.channels = [
756
+ null,
757
+ new Cta608Channel(field, out1, logger),
758
+ new Cta608Channel(field + 1, out2, logger)
759
+ ];
760
+ }
761
+ /**
762
+ * Add data for time t in forms of list of bytes (unsigned ints). The bytes are treated as pairs.
763
+ *
764
+ * @param time - The time in milliseconds
765
+ * @param byteList - The list of bytes
766
+ */
767
+ addData(time, byteList) {
768
+ this.lastTime = time;
769
+ this.logger.time = time;
770
+ for (let i = 0; i < byteList.length; i += 2) {
771
+ const a = byteList[i] & 127;
772
+ const b = byteList[i + 1] & 127;
773
+ let cmdFound = false;
774
+ let charsFound = null;
775
+ if (this.lastTime !== null) {
776
+ time = this.lastTime + .5 * i * 1001 / 3e4;
777
+ this.logger.time = time;
778
+ }
779
+ if (a === 0 && b === 0) continue;
780
+ else this.logger.log(VerboseLevel.DATA, () => "[" + numArrayToHexArray([byteList[i], byteList[i + 1]]) + "] -> (" + numArrayToHexArray([a, b]) + ")");
781
+ const cmdHistory = this.cmdHistory;
782
+ if (a >= 16 && a <= 31) {
783
+ if (hasCmdRepeated(a, b, cmdHistory)) {
784
+ setLastCmd(null, null, cmdHistory);
785
+ this.logger.log(VerboseLevel.DEBUG, () => "Repeated command (" + numArrayToHexArray([a, b]) + ") is dropped");
786
+ continue;
787
+ }
788
+ setLastCmd(a, b, this.cmdHistory);
789
+ cmdFound = this.parseCmd(a, b);
790
+ if (!cmdFound) cmdFound = this.parseMidrow(a, b);
791
+ if (!cmdFound) cmdFound = this.parsePAC(a, b);
792
+ if (!cmdFound) cmdFound = this.parseBackgroundAttributes(a, b);
793
+ } else setLastCmd(null, null, cmdHistory);
794
+ if (!cmdFound) {
795
+ charsFound = this.parseChars(a, b);
796
+ if (charsFound.length) {
797
+ const currChNr = this.currentChannel;
798
+ if (currChNr && currChNr > 0) this.channels[currChNr].insertChars(charsFound);
799
+ else this.logger.log(VerboseLevel.WARNING, "No channel found yet. TEXT-MODE?");
800
+ }
801
+ }
802
+ if (!cmdFound && !charsFound) this.logger.log(VerboseLevel.WARNING, () => "Couldn't parse cleaned data " + numArrayToHexArray([a, b]) + " orig: " + numArrayToHexArray([byteList[i], byteList[i + 1]]));
803
+ }
804
+ }
805
+ /**
806
+ * Parse Command.
807
+ *
808
+ * @param a - The first byte
809
+ * @param b - The second byte
810
+ * @returns True if a command was found
811
+ */
812
+ parseCmd(a, b) {
813
+ if (!((a === 20 || a === 28 || a === 21 || a === 29) && b >= 32 && b <= 47 || (a === 23 || a === 31) && b >= 33 && b <= 35)) return false;
814
+ const chNr = a === 20 || a === 21 || a === 23 ? 1 : 2;
815
+ const channel = this.channels[chNr];
816
+ if (a === 20 || a === 21 || a === 28 || a === 29) {
817
+ if (b === 32) channel.ccRCL();
818
+ else if (b === 33) channel.ccBS();
819
+ else if (b === 34) channel.ccAOF();
820
+ else if (b === 35) channel.ccAON();
821
+ else if (b === 36) channel.ccDER();
822
+ else if (b === 37) channel.ccRU(2);
823
+ else if (b === 38) channel.ccRU(3);
824
+ else if (b === 39) channel.ccRU(4);
825
+ else if (b === 40) channel.ccFON();
826
+ else if (b === 41) channel.ccRDC();
827
+ else if (b === 42) channel.ccTR();
828
+ else if (b === 43) channel.ccRTD();
829
+ else if (b === 44) channel.ccEDM();
830
+ else if (b === 45) channel.ccCR();
831
+ else if (b === 46) channel.ccENM();
832
+ else if (b === 47) channel.ccEOC();
833
+ } else channel.ccTO(b - 32);
834
+ this.currentChannel = chNr;
835
+ return true;
836
+ }
837
+ /**
838
+ * Parse midrow styling command
839
+ *
840
+ * @param a - The first byte
841
+ * @param b - The second byte
842
+ * @returns `true` if midrow styling command was found
843
+ */
844
+ parseMidrow(a, b) {
845
+ let chNr = 0;
846
+ if ((a === 17 || a === 25) && b >= 32 && b <= 47) {
847
+ if (a === 17) chNr = 1;
848
+ else chNr = 2;
849
+ if (chNr !== this.currentChannel) {
850
+ this.logger.log(VerboseLevel.ERROR, "Mismatch channel in midrow parsing");
851
+ return false;
852
+ }
853
+ const channel = this.channels[chNr];
854
+ if (!channel) return false;
855
+ channel.ccMIDROW(b);
856
+ this.logger.log(VerboseLevel.DEBUG, () => "MIDROW (" + numArrayToHexArray([a, b]) + ")");
857
+ return true;
858
+ }
859
+ return false;
860
+ }
861
+ /**
862
+ * Parse Preable Access Codes (Table 53).
863
+ *
864
+ * @param a - The first byte
865
+ * @param b - The second byte
866
+ * @returns A Boolean that tells if PAC found
867
+ */
868
+ parsePAC(a, b) {
869
+ let row;
870
+ if (!((a >= 17 && a <= 23 || a >= 25 && a <= 31) && b >= 64 && b <= 127 || (a === 16 || a === 24) && b >= 64 && b <= 95)) return false;
871
+ const chNr = a <= 23 ? 1 : 2;
872
+ if (b >= 64 && b <= 95) row = chNr === 1 ? rowsLowCh1[a] : rowsLowCh2[a];
873
+ else row = chNr === 1 ? rowsHighCh1[a] : rowsHighCh2[a];
874
+ const channel = this.channels[chNr];
875
+ if (!channel) return false;
876
+ channel.setPAC(this.interpretPAC(row, b));
877
+ this.currentChannel = chNr;
878
+ return true;
879
+ }
880
+ /**
881
+ * Interpret the second byte of the pac, and return the information.
882
+ *
883
+ * @param row - The row number
884
+ * @param byte - The second byte
885
+ * @returns pacData with style parameters
886
+ */
887
+ interpretPAC(row, byte) {
888
+ let pacIndex;
889
+ const pacData = {
890
+ color: null,
891
+ italics: false,
892
+ indent: null,
893
+ underline: false,
894
+ row
895
+ };
896
+ if (byte > 95) pacIndex = byte - 96;
897
+ else pacIndex = byte - 64;
898
+ pacData.underline = (pacIndex & 1) === 1;
899
+ if (pacIndex <= 13) pacData.color = [
900
+ "white",
901
+ "green",
902
+ "blue",
903
+ "cyan",
904
+ "red",
905
+ "yellow",
906
+ "magenta",
907
+ "white"
908
+ ][Math.floor(pacIndex / 2)];
909
+ else if (pacIndex <= 15) {
910
+ pacData.italics = true;
911
+ pacData.color = "white";
912
+ } else pacData.indent = Math.floor((pacIndex - 16) / 2) * 4;
913
+ return pacData;
914
+ }
915
+ /**
916
+ * Parse characters.
917
+ *
918
+ * @param a - The first byte
919
+ * @param b - The second byte
920
+ * @returns An array with 1 to 2 codes corresponding to chars, if found. null otherwise.
921
+ */
922
+ parseChars(a, b) {
923
+ let channelNr;
924
+ let charCodes = [];
925
+ let charCode1 = null;
926
+ if (a >= 25) {
927
+ channelNr = 2;
928
+ charCode1 = a - 8;
929
+ } else {
930
+ channelNr = 1;
931
+ charCode1 = a;
932
+ }
933
+ if (charCode1 >= 17 && charCode1 <= 19) {
934
+ let oneCode;
935
+ if (charCode1 === 17) oneCode = b + 80;
936
+ else if (charCode1 === 18) oneCode = b + 112;
937
+ else oneCode = b + 144;
938
+ this.logger.log(VerboseLevel.INFO, () => "Special char '" + getCharForByte(oneCode) + "' in channel " + channelNr);
939
+ charCodes = [oneCode];
940
+ } else if (a >= 32 && a <= 127) charCodes = b === 0 ? [a] : [a, b];
941
+ if (charCodes) this.logger.log(VerboseLevel.DEBUG, () => "Char codes = " + numArrayToHexArray(charCodes).join(","));
942
+ return charCodes;
943
+ }
944
+ /**
945
+ * Parse extended background attributes as well as new foreground color black.
946
+ *
947
+ * @param a - The first byte
948
+ * @param b - The second byte
949
+ * @returns True if background attributes are found
950
+ */
951
+ parseBackgroundAttributes(a, b) {
952
+ if (!((a === 16 || a === 24) && b >= 32 && b <= 47 || (a === 23 || a === 31) && b >= 45 && b <= 47)) return false;
953
+ let index;
954
+ const bkgData = {};
955
+ if (a === 16 || a === 24) {
956
+ index = Math.floor((b - 32) / 2);
957
+ bkgData.background = backgroundColors[index];
958
+ if (b % 2 === 1) bkgData.background = bkgData.background + "_semi";
959
+ } else if (b === 45) bkgData.background = "transparent";
960
+ else {
961
+ bkgData.foreground = "black";
962
+ if (b === 47) bkgData.underline = true;
963
+ }
964
+ const chNr = a <= 23 ? 1 : 2;
965
+ this.channels[chNr].setBkgData(bkgData);
966
+ return true;
967
+ }
968
+ /**
969
+ * Reset state of parser and its channels.
970
+ */
971
+ reset() {
972
+ for (let i = 0; i < Object.keys(this.channels).length; i++) {
973
+ const channel = this.channels[i];
974
+ if (channel) channel.reset();
975
+ }
976
+ setLastCmd(null, null, this.cmdHistory);
977
+ }
978
+ /**
979
+ * Trigger the generation of a cue, and the start of a new one if displayScreens are not empty.
980
+ */
981
+ cueSplitAtTime(t) {
982
+ for (const channel of this.channels) if (channel) channel.cueSplitAtTime(t);
983
+ }
984
+ };
985
+
986
+ //#endregion
987
+ //#region src/extractCta608Data.ts
988
+ /**
989
+ * Extract CTA-608 data from a DataView
990
+ *
991
+ * @param raw - The DataView to extract the data from
992
+ * @param cta608Range - The range of the CTA-608 data
993
+ * @returns The extracted CTA-608 data
994
+ *
995
+ * @beta
996
+ */
997
+ function extractCta608Data(raw, cta608Range) {
998
+ let pos = cta608Range[0];
999
+ const fieldData = [[], []];
1000
+ pos += 8;
1001
+ const ccCount = raw.getUint8(pos) & 31;
1002
+ pos += 2;
1003
+ for (let i = 0; i < ccCount; i++) {
1004
+ const byte = raw.getUint8(pos);
1005
+ const ccValid = byte & 4;
1006
+ const ccType = byte & 3;
1007
+ pos++;
1008
+ const ccData1 = raw.getUint8(pos);
1009
+ pos++;
1010
+ const ccData2 = raw.getUint8(pos);
1011
+ pos++;
1012
+ if (ccValid && (ccData1 & 127) + (ccData2 & 127) !== 0) {
1013
+ if (ccType === 0) {
1014
+ fieldData[0].push(ccData1);
1015
+ fieldData[0].push(ccData2);
1016
+ } else if (ccType === 1) {
1017
+ fieldData[1].push(ccData1);
1018
+ fieldData[1].push(ccData2);
1019
+ }
1020
+ }
1021
+ }
1022
+ return fieldData;
1023
+ }
1024
+
1025
+ //#endregion
1026
+ //#region src/findCta608Nalus.ts
1027
+ /**
1028
+ * Find CTA-608 NAL units in a video stream
1029
+ *
1030
+ * @param raw - The DataView to extract the data from
1031
+ * @param startPos - The start position of the data
1032
+ * @param size - The size of the data
1033
+ * @returns The extracted CTA-608 NAL units
1034
+ *
1035
+ * @beta
1036
+ */
1037
+ function findCta608Nalus(raw, startPos, size) {
1038
+ let nalSize = 0, cursor = startPos, nalType = 0;
1039
+ const cta608NaluRanges = [];
1040
+ const isCTA608SEI = (payloadType, payloadSize, raw$1, pos) => {
1041
+ if (payloadType !== 4 || payloadSize < 8) return null;
1042
+ const countryCode = raw$1.getUint8(pos);
1043
+ const providerCode = raw$1.getUint16(pos + 1);
1044
+ const userIdentifier = raw$1.getUint32(pos + 3);
1045
+ const userDataTypeCode = raw$1.getUint8(pos + 7);
1046
+ return countryCode == 181 && providerCode == 49 && userIdentifier == 1195456820 && userDataTypeCode == 3;
1047
+ };
1048
+ while (cursor < startPos + size) {
1049
+ nalSize = raw.getUint32(cursor);
1050
+ nalType = raw.getUint8(cursor + 4) & 31;
1051
+ if (nalType === 6) {
1052
+ let pos = cursor + 5;
1053
+ let payloadType = -1;
1054
+ while (pos < cursor + 4 + nalSize - 1) {
1055
+ payloadType = 0;
1056
+ let b = 255;
1057
+ while (b === 255) {
1058
+ b = raw.getUint8(pos);
1059
+ payloadType += b;
1060
+ pos++;
1061
+ }
1062
+ let payloadSize = 0;
1063
+ b = 255;
1064
+ while (b === 255) {
1065
+ b = raw.getUint8(pos);
1066
+ payloadSize += b;
1067
+ pos++;
1068
+ }
1069
+ if (isCTA608SEI(payloadType, payloadSize, raw, pos)) cta608NaluRanges.push([pos, payloadSize]);
1070
+ pos += payloadSize;
1071
+ }
1072
+ }
1073
+ cursor += nalSize + 4;
1074
+ }
1075
+ return cta608NaluRanges;
1076
+ }
1077
+
1078
+ //#endregion
1079
+ //#region src/SccParser.ts
1080
+ /**
1081
+ * The copyright in this software is being made available under the BSD License,
1082
+ * included below. This software may be subject to other third party and contributor
1083
+ * rights, including patent rights, and no such rights are granted under this license.
1084
+ *
1085
+ * Copyright (c) 2015-2016, DASH Industry Forum.
1086
+ * All rights reserved.
1087
+ *
1088
+ * Redistribution and use in source and binary forms, with or without modification,
1089
+ * are permitted provided that the following conditions are met:
1090
+ * 1. Redistributions of source code must retain the above copyright notice, this
1091
+ * list of conditions and the following disclaimer.
1092
+ * * Redistributions in binary form must reproduce the above copyright notice,
1093
+ * this list of conditions and the following disclaimer in the documentation and/or
1094
+ * other materials provided with the distribution.
1095
+ * 2. Neither the name of Dash Industry Forum nor the names of its
1096
+ * contributors may be used to endorse or promote products derived from this software
1097
+ * without specific prior written permission.
1098
+ *
1099
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY
1100
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1101
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1102
+ * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
1103
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1104
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1105
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
1106
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1107
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1108
+ * POSSIBILITY OF SUCH DAMAGE.
1109
+ */
1110
+ /**
1111
+ * SCC Parser
1112
+ *
1113
+ * @beta
1114
+ */
1115
+ var SccParser = class {
1116
+ constructor(processor, field = 1) {
1117
+ this.hasHeader = false;
1118
+ this.nrLinesParsed = 0;
1119
+ this.processor = processor;
1120
+ this.field = field;
1121
+ }
1122
+ parse(text) {
1123
+ const lines = text.split(/\r?\n/);
1124
+ this.nrLinesParsed = 0;
1125
+ if (lines[0] === "Scenarist_SCC V1.0") {
1126
+ this.hasHeader = true;
1127
+ this.nrLinesParsed++;
1128
+ }
1129
+ for (let l = 1; l < lines.length; l += 2) {
1130
+ if (lines[l] !== "") break;
1131
+ this.nrLinesParsed++;
1132
+ const lineData = this.parseDataLine(lines[l + 1]);
1133
+ if (lineData === null) break;
1134
+ this.nrLinesParsed++;
1135
+ this.processor.addData(lineData[0], lineData[1]);
1136
+ }
1137
+ }
1138
+ parseDataLine(line) {
1139
+ if (!line) return null;
1140
+ const lineParts = line.split(/\s+/);
1141
+ const timeData = lineParts[0];
1142
+ const ceaData = [];
1143
+ for (let i = 1; i < lineParts.length; i++) {
1144
+ const fourHexChars = lineParts[i];
1145
+ const a = parseInt(fourHexChars.substring(0, 2), 16);
1146
+ const b = parseInt(fourHexChars.substring(2, 4), 16);
1147
+ ceaData.push(a, b);
1148
+ }
1149
+ return [this.timeConverter(timeData), ceaData];
1150
+ }
1151
+ timeConverter(smpteTs) {
1152
+ const parts = smpteTs.split(":");
1153
+ if (parts.length === 3) {
1154
+ const lastParts = parts[2].split(";");
1155
+ parts[2] = lastParts[0];
1156
+ const frames = parseInt(lastParts[1], 10);
1157
+ return (30 * (60 * (60 * parseInt(parts[0], 10) + parseInt(parts[1], 10)) + parseInt(parts[2], 10)) + frames) * 1001 / 3e4;
1158
+ }
1159
+ return 0;
1160
+ }
1161
+ getHeaderStatus() {
1162
+ return this.hasHeader;
1163
+ }
1164
+ getField() {
1165
+ return this.field;
1166
+ }
1167
+ getLinesParsed() {
1168
+ return this.nrLinesParsed;
1169
+ }
1170
+ };
1171
+
1172
+ //#endregion
1173
+ export { CaptionScreen, CaptionsLogger, Cta608Channel, Cta608Parser, PenState, Row, SccParser, StyledUnicodeChar, VerboseLevel, extractCta608Data, findCta608Nalus };
16
1174
  //# sourceMappingURL=index.js.map