threefive3 3.0.1__py3-none-any.whl

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.
scte35/commands.py ADDED
@@ -0,0 +1,326 @@
1
+ """
2
+ SCTE35 Splice Commands
3
+ """
4
+
5
+ from .bitn import BitBin
6
+ from .base import SCTE35Base
7
+ from .xml import Node
8
+
9
+
10
+ class SpliceCommand(SCTE35Base):
11
+ """
12
+ Base class, not used directly.
13
+ """
14
+
15
+ def __init__(self, bites=None):
16
+ self.command_length = 0
17
+ self.command_type = None
18
+ self.name = None
19
+ self.bites = bites
20
+
21
+ def decode(self):
22
+ """
23
+ default decode method
24
+ """
25
+
26
+ def _set_len(self, start, end):
27
+ """
28
+ _set_len sets
29
+ self.command_length
30
+ """
31
+ self.command_length = (start - end) >> 3
32
+
33
+ def encode(self, nbin=None):
34
+ """
35
+ encode
36
+ """
37
+ nbin = self._chk_nbin(nbin)
38
+ return nbin.bites
39
+
40
+
41
+
42
+ class BandwidthReservation(SpliceCommand):
43
+ """
44
+ Table 12 - bandwidth_reservation()
45
+ """
46
+
47
+ def __init__(self, bites=None):
48
+ super().__init__(bites)
49
+ self.command_type = 7
50
+ self.name = "Bandwidth Reservation"
51
+
52
+ def decode(self):
53
+ """
54
+ BandwidthReservation.decode method
55
+ """
56
+
57
+ def xml(self, ns="scte35"):
58
+ """
59
+ create XML Node of type BandwidthReservation
60
+ """
61
+ return Node("BandwidthReservation", ns=ns)
62
+
63
+
64
+ class PrivateCommand(SpliceCommand):
65
+ """
66
+ Table 13 - private_command
67
+ """
68
+
69
+ def __init__(self, bites=None):
70
+ super().__init__(bites)
71
+ self.command_type = 255
72
+ self.name = "Private Command"
73
+ self.identifier = None
74
+ self.private_bytes = None
75
+
76
+ def decode(self):
77
+ """
78
+ PrivateCommand.decode method
79
+ """
80
+ self.identifier = int.from_bytes(
81
+ self.bites[:4], byteorder="big"
82
+ ) # 4 bytes = 32 bits
83
+ self.private_bytes = self.bites[4:]
84
+
85
+ def encode(self, nbin=None):
86
+ """
87
+ encode private command
88
+ """
89
+ nbin = self._chk_nbin(nbin)
90
+ self._chk_var(int, nbin.add_int, "identifier", 32) # 4 bytes = 32 bits
91
+ nbin.add_bites(self.private_bytes)
92
+ return nbin.bites
93
+
94
+ def xml(self, ns="scte35"):
95
+ """
96
+ create XML Node of type PrivateCommand
97
+ """
98
+ attrs = {"identifier": self.identifier}
99
+ pc = Node("PrivateCommand", attrs=attrs)
100
+ pc.add_child(Node("PrivateBytes", value=self.private_bytes.hex(), ns=ns))
101
+ return pc
102
+
103
+
104
+ class SpliceNull(SpliceCommand):
105
+ """
106
+ Table 8 - splice_null()
107
+ """
108
+
109
+ def __init__(self, bites=None):
110
+ super().__init__(bites)
111
+ self.command_type = 0
112
+ self.name = "Splice Null"
113
+
114
+ def xml(self, ns="scte35"):
115
+ return Node("SpliceNull", ns=ns)
116
+
117
+
118
+ class TimeSignal(SpliceCommand):
119
+ """
120
+ Table 11 - time_signal()
121
+ """
122
+
123
+ def __init__(self, bites=None):
124
+ super().__init__(bites)
125
+ self.command_type = 6
126
+ self.name = "Time Signal"
127
+ self.time_specified_flag = None
128
+ self.pts_time = None
129
+
130
+ def decode(self):
131
+ """
132
+ TimeSignal.decode method
133
+ """
134
+ bitbin = BitBin(self.bites)
135
+ start = bitbin.idx
136
+ self._splice_time(bitbin)
137
+ self._set_len(start, bitbin.idx)
138
+
139
+ def encode(self, nbin=None):
140
+ """
141
+ encode converts TimeSignal vars
142
+ to bytes
143
+ """
144
+ nbin = self._chk_nbin(nbin)
145
+ self._encode_splice_time(nbin)
146
+ return nbin.bites
147
+
148
+ def _splice_time(self, bitbin):
149
+ """
150
+ _splice_time Table 14 - splice_time()
151
+ """
152
+ self.time_specified_flag = bitbin.as_flag(1)
153
+ if self.time_specified_flag:
154
+ bitbin.forward(6)
155
+ pts_time_ticks = bitbin.as_int(33)
156
+ self.pts_time = self.as_90k(pts_time_ticks)
157
+ else:
158
+ bitbin.forward(7)
159
+
160
+ def _encode_splice_time(self, nbin):
161
+ """
162
+ _encode_splice_time Table 14 - splice_time()
163
+ """
164
+ self._chk_var(bool, nbin.add_flag, "time_specified_flag", 1)
165
+ if self.time_specified_flag:
166
+ nbin.reserve(6)
167
+ if not isinstance(self.pts_time, float):
168
+ raise ValueError(
169
+ "\033[7mA float for pts_time for the splice command is required.\033[27m"
170
+ )
171
+ self.pts_time = round(self.pts_time, 6)
172
+ nbin.add_int(int(self.as_ticks(self.pts_time)), 33)
173
+ else:
174
+ nbin.reserve(7)
175
+
176
+ def xml(self, ns="scte35"):
177
+ """
178
+ xml return TimeSignal as an xml node
179
+ """
180
+ ts = Node("TimeSignal", ns=ns)
181
+ if self.has("pts_time"):
182
+ if self.pts_time:
183
+ self.pts_time = round(self.pts_time, 6)
184
+ st = Node(
185
+ "SpliceTime",
186
+ attrs={"pts_time": self.as_ticks(self.pts_time)},
187
+ ns=ns,
188
+ )
189
+ ts.add_child(st)
190
+ return ts
191
+
192
+
193
+ class SpliceInsert(TimeSignal):
194
+ """
195
+ Table 10 - splice_insert()
196
+ """
197
+
198
+ def __init__(self, bites=None):
199
+ super().__init__(bites)
200
+ self.command_type = 5
201
+ self.name = "Splice Insert"
202
+ self.break_auto_return = None
203
+ self.break_duration = None
204
+ self.splice_event_id = None
205
+ self.splice_event_cancel_indicator = None
206
+ self.out_of_network_indicator = None
207
+ self.program_splice_flag = None
208
+ self.duration_flag = None
209
+ self.splice_immediate_flag = None
210
+ self.event_id_compliance_flag = None
211
+ self.unique_program_id = None
212
+ self.avail_num = None
213
+ self.avails_expected = None
214
+
215
+ def decode_break_duration(self, bitbin):
216
+ """
217
+ break_duration Table 15 - break_duration()
218
+ """
219
+ self.break_auto_return = bitbin.as_flag(1)
220
+ bitbin.forward(6)
221
+ break_duration_ticks = bitbin.as_int(33)
222
+ self.break_duration = self.as_90k(break_duration_ticks)
223
+
224
+ def decode(self):
225
+ """
226
+ decode SpliceInsert
227
+ """
228
+ bitbin = BitBin(self.bites)
229
+ start = bitbin.idx
230
+ self.splice_event_id = bitbin.as_int(32)
231
+ self.splice_event_cancel_indicator = bitbin.as_flag(1)
232
+ bitbin.forward(7)
233
+ if not self.splice_event_cancel_indicator:
234
+ self.out_of_network_indicator = bitbin.as_flag(1)
235
+ self.program_splice_flag = bitbin.as_flag(1)
236
+ self.duration_flag = bitbin.as_flag(1)
237
+ self.splice_immediate_flag = bitbin.as_flag(1)
238
+ self.event_id_compliance_flag = bitbin.as_flag(1)
239
+ bitbin.forward(3)
240
+ if self.program_splice_flag:
241
+ if not self.splice_immediate_flag:
242
+ self._splice_time(bitbin)
243
+ if self.duration_flag:
244
+ self.decode_break_duration(bitbin)
245
+ self.unique_program_id = bitbin.as_int(16)
246
+ self.avail_num = bitbin.as_int(8)
247
+ self.avails_expected = bitbin.as_int(8)
248
+ self._set_len(start, bitbin.idx)
249
+
250
+ def encode(self, nbin=None):
251
+ """
252
+ SpliceInsert.encode
253
+ """
254
+ nbin = self._chk_nbin(nbin)
255
+ self._chk_var(int, nbin.add_int, "splice_event_id", 32)
256
+ self._chk_var(bool, nbin.add_flag, "splice_event_cancel_indicator", 1)
257
+ nbin.forward(7)
258
+ if not self.splice_event_cancel_indicator:
259
+ self._chk_var(bool, nbin.add_flag, "out_of_network_indicator", 1)
260
+ self._chk_var(bool, nbin.add_flag, "program_splice_flag", 1)
261
+ self._chk_var(bool, nbin.add_flag, "duration_flag", 1)
262
+ self._chk_var(bool, nbin.add_flag, "splice_immediate_flag", 1)
263
+ self._chk_var(bool, nbin.add_flag, "event_id_compliance_flag", 1)
264
+ nbin.forward(3)
265
+ if self.program_splice_flag:
266
+ if not self.splice_immediate_flag:
267
+ self._encode_splice_time(nbin)
268
+ if self.duration_flag:
269
+ self.encode_break_duration(nbin)
270
+ self._chk_var(int, nbin.add_int, "unique_program_id", 16)
271
+ self._chk_var(int, nbin.add_int, "avail_num", 8)
272
+ self._chk_var(int, nbin.add_int, "avails_expected", 8)
273
+ return nbin.bites
274
+
275
+ def encode_break_duration(self, nbin):
276
+ """
277
+ SpliceInsert._encode_break(nbin) is called
278
+ if SpliceInsert.duration_flag is set
279
+ """
280
+ self._chk_var(bool, nbin.add_flag, "break_auto_return", 1)
281
+ nbin.forward(6)
282
+ nbin.add_int(self.as_ticks(self.break_duration), 33)
283
+
284
+ def xml(self, ns="scte35"):
285
+ """
286
+ xml return the SpliceInsert instance as an xml node.
287
+ """
288
+ si_attrs = {
289
+ "splice_event_id": self.splice_event_id,
290
+ "splice_event_cancel_Indicator": self.splice_event_cancel_indicator,
291
+ "splice_immediate_flag": self.splice_immediate_flag,
292
+ "event_id_compliance_flag": self.event_id_compliance_flag,
293
+ "avail_num": self.avail_num,
294
+ "avails_expected": self.avails_expected,
295
+ "out_of_network_indicator": self.out_of_network_indicator,
296
+ "unique_program_id": self.unique_program_id,
297
+ }
298
+ for k, v in si_attrs.items():
299
+ if v is None:
300
+ raise ValueError(f"\033[7mSpliceInsert.{k} needs to be set\033[27m")
301
+ si = Node("SpliceInsert", attrs=si_attrs, ns=ns)
302
+ if self.pts_time:
303
+ prgm = Node("Program", ns=ns)
304
+ st = Node(
305
+ "SpliceTime", attrs={"ptsTime": self.as_ticks(self.pts_time)}, ns=ns
306
+ )
307
+ prgm.add_child(st)
308
+ si.add_child(prgm)
309
+ if self.break_duration:
310
+ bd_attrs = {
311
+ "auto_return": self.break_auto_return,
312
+ "duration": self.as_ticks(self.break_duration),
313
+ }
314
+ bd = Node("BreakDuration", attrs=bd_attrs, ns=ns)
315
+ si.add_child(bd)
316
+ return si
317
+
318
+
319
+ # table 7
320
+ command_map = {
321
+ 0: SpliceNull,
322
+ 5: SpliceInsert,
323
+ 6: TimeSignal,
324
+ 7: BandwidthReservation,
325
+ 255: PrivateCommand,
326
+ }
scte35/crc.py ADDED
@@ -0,0 +1,49 @@
1
+ """
2
+ crc.py crc32 function for encoding.
3
+ """
4
+
5
+ POLY = 0x104C11DB7
6
+ INIT_VALUE = 0xFFFFFFFF
7
+ ZERO = 0x0
8
+ ONE = 0x1
9
+ EIGHT = 0x8
10
+ TWENTY_FOUR = 0x18
11
+ THIRTY_TWO = 0x20
12
+ TWO_FIFTY_FIVE = 0xFF
13
+ TWO_FIFTY_SIX = 0x100
14
+
15
+
16
+ def _bytecrc(crc, poly):
17
+ mask = ONE << (THIRTY_TWO - ONE)
18
+ i = EIGHT
19
+ while i:
20
+ crc = (crc << ONE, crc << ONE ^ poly)[crc & mask != ZERO]
21
+ i -= ONE
22
+ return crc & INIT_VALUE
23
+
24
+
25
+ def _mk_table():
26
+ mask = (ONE << THIRTY_TWO) - ONE
27
+ poly = POLY & mask
28
+ return [_bytecrc((i << TWENTY_FOUR), poly) for i in range(TWO_FIFTY_SIX)]
29
+
30
+
31
+ def crc32(data):
32
+ """
33
+ generate a 32 bit crc
34
+ """
35
+
36
+ table = _mk_table()
37
+ crc = INIT_VALUE
38
+ for bite in data:
39
+ crc = table[bite ^ ((crc >> TWENTY_FOUR) & TWO_FIFTY_FIVE)] ^ (
40
+ (crc << EIGHT) & (INIT_VALUE - TWO_FIFTY_FIVE)
41
+ )
42
+ return crc
43
+
44
+
45
+ def crc32hex(data):
46
+ """
47
+ crc32hex crc32 returned as hex
48
+ """
49
+ return hex(crc32(data))