1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//! Encoding implementations for SCTE-35 descriptors.
use crate::descriptors::*;
use crate::encoding::{BitWriter, Encodable, EncodingResult};
impl Encodable for SpliceDescriptor {
fn encode(&self, writer: &mut BitWriter) -> EncodingResult<()> {
match self {
SpliceDescriptor::Segmentation(desc) => desc.encode(writer),
SpliceDescriptor::Avail(desc) => desc.encode(writer),
SpliceDescriptor::Dtmf(desc) => desc.encode(writer),
SpliceDescriptor::Time(desc) => desc.encode(writer),
SpliceDescriptor::Audio(desc) => desc.encode(writer),
SpliceDescriptor::Unknown { tag, length, data } => {
// splice_descriptor_tag (8 bits)
writer.write_bits(*tag as u64, 8)?;
// descriptor_length (8 bits)
writer.write_bits(*length as u64, 8)?;
// descriptor data
writer.write_bytes(data)?;
Ok(())
}
}
}
fn encoded_size(&self) -> usize {
match self {
SpliceDescriptor::Segmentation(desc) => desc.encoded_size(),
SpliceDescriptor::Avail(desc) => desc.encoded_size(),
SpliceDescriptor::Dtmf(desc) => desc.encoded_size(),
SpliceDescriptor::Time(desc) => desc.encoded_size(),
SpliceDescriptor::Audio(desc) => desc.encoded_size(),
SpliceDescriptor::Unknown { data, .. } => 2 + data.len(), // tag + length + data
}
}
}
impl Encodable for SegmentationDescriptor {
fn encode(&self, writer: &mut BitWriter) -> EncodingResult<()> {
// splice_descriptor_tag (8 bits) - 0x02 for segmentation descriptor
writer.write_bits(0x02u64, 8)?;
// descriptor_length (8 bits) - calculate from fields
let descriptor_length = self.calculate_descriptor_length();
writer.write_bits(descriptor_length as u64, 8)?;
// identifier (32 bits) - 0x43554549 ("CUEI")
writer.write_bits(0x43554549u64, 32)?;
// segmentation_event_id (32 bits)
writer.write_bits(self.segmentation_event_id as u64, 32)?;
// segmentation_event_cancel_indicator (1 bit)
writer.write_bits(self.segmentation_event_cancel_indicator as u64, 1)?;
// Reserved field - there's no segmentation_event_id_compliance_indicator field
writer.write_bits(1u64, 1)?; // Reserved bit should be 1
// reserved (6 bits) - should be all 1s
writer.write_bits(0x3F, 6)?; // 0x3F = 111111 in binary
if !self.segmentation_event_cancel_indicator {
// program_segmentation_flag (1 bit)
writer.write_bits(self.program_segmentation_flag as u64, 1)?;
// segmentation_duration_flag (1 bit)
writer.write_bits(self.segmentation_duration_flag as u64, 1)?;
// delivery_not_restricted_flag (1 bit)
writer.write_bits(self.delivery_not_restricted_flag as u64, 1)?;
if !self.delivery_not_restricted_flag {
// web_delivery_allowed_flag (1 bit)
let web_flag = self.web_delivery_allowed_flag.unwrap_or(false) as u64;
writer.write_bits(web_flag, 1)?;
// no_regional_blackout_flag (1 bit)
let blackout_flag = self.no_regional_blackout_flag.unwrap_or(false) as u64;
writer.write_bits(blackout_flag, 1)?;
// archive_allowed_flag (1 bit)
let archive_flag = self.archive_allowed_flag.unwrap_or(false) as u64;
writer.write_bits(archive_flag, 1)?;
// device_restrictions (2 bits)
let restrictions = self.device_restrictions.unwrap_or(0) as u64;
writer.write_bits(restrictions, 2)?;
} else {
// reserved (5 bits) - should be all 1s
writer.write_bits(0x1F, 5)?; // 0x1F = 11111 in binary
}
// Component loop if program_segmentation_flag == false
if !self.program_segmentation_flag {
// For now, assume no components since they're not in the struct
// component_count (8 bits)
writer.write_bits(0u64, 8)?; // This is data, not reserved bits
}
// segmentation_duration if segmentation_duration_flag == true
if self.segmentation_duration_flag {
if let Some(duration) = self.segmentation_duration {
writer.write_bits(duration & 0xFFFFFFFFFF, 40)?; // 40 bits
}
}
}
// segmentation_upid_type (8 bits)
let upid_type_value: u8 = self.segmentation_upid_type.into();
writer.write_bits(upid_type_value as u64, 8)?;
// segmentation_upid_length (8 bits)
writer.write_bits(self.segmentation_upid_length as u64, 8)?;
// segmentation_upid (variable length)
writer.write_bytes(&self.segmentation_upid)?;
// segmentation_type_id (8 bits)
writer.write_bits(self.segmentation_type_id as u64, 8)?;
// segment_num (8 bits)
writer.write_bits(self.segment_num as u64, 8)?;
// segments_expected (8 bits)
writer.write_bits(self.segments_expected as u64, 8)?;
// Sub-segment fields for specific segmentation types that support sub-segments
// 0x34 (Provider Placement Opportunity Start) does NOT have sub-segment fields
if matches!(
self.segmentation_type_id,
0x30 | 0x32 | 0x36 | 0x38 | 0x3A | 0x44 | 0x46
) {
if let Some(sub_segment_num) = self.sub_segment_num {
writer.write_bits(sub_segment_num as u64, 8)?;
}
if let Some(sub_segments_expected) = self.sub_segments_expected {
writer.write_bits(sub_segments_expected as u64, 8)?;
}
}
Ok(())
}
fn encoded_size(&self) -> usize {
let mut size = 2 + 4 + 4 + 1; // tag + length + identifier + event_id + flags
if !self.segmentation_event_cancel_indicator {
size += 1; // flags byte
// Component loop
if !self.program_segmentation_flag {
size += 1; // component_count (assuming 0 components for now)
}
// Duration
if self.segmentation_duration_flag {
size += 5; // 40 bits = 5 bytes
}
}
size += 2; // upid_type + upid_length
size += self.segmentation_upid.len(); // upid data
size += 3; // type_id + segment_num + segments_expected
// Sub-segment fields - 0x34 (Provider Placement Opportunity Start) does NOT have sub-segment fields
if matches!(
self.segmentation_type_id,
0x30 | 0x32 | 0x36 | 0x38 | 0x3A | 0x44 | 0x46
) {
size += 2; // sub_segment_num + sub_segments_expected
}
size
}
}
impl SegmentationDescriptor {
fn calculate_descriptor_length(&self) -> usize {
// Calculate length excluding tag and length field itself
self.encoded_size() - 2
}
}
// Placeholder implementations for other descriptor types
impl Encodable for AvailDescriptor {
fn encode(&self, writer: &mut BitWriter) -> EncodingResult<()> {
// splice_descriptor_tag (8 bits)
writer.write_bits(0x00u64, 8)?;
// descriptor_length (8 bits) - 4 bytes for identifier + provider_avail_id length
let length = 4 + self.provider_avail_id.len();
writer.write_bits(length as u64, 8)?;
// identifier (32 bits)
writer.write_bits(self.identifier as u64, 32)?;
// provider_avail_id (variable length)
writer.write_bytes(&self.provider_avail_id)?;
Ok(())
}
fn encoded_size(&self) -> usize {
2 + 4 + self.provider_avail_id.len() // tag + length + identifier + provider_avail_id
}
}
impl Encodable for DtmfDescriptor {
fn encode(&self, writer: &mut BitWriter) -> EncodingResult<()> {
// splice_descriptor_tag (8 bits)
writer.write_bits(0x01u64, 8)?;
// descriptor_length (8 bits)
writer.write_bits(4u64, 8)?;
// identifier (32 bits)
writer.write_bits(self.identifier as u64, 32)?;
Ok(())
}
fn encoded_size(&self) -> usize {
6 // tag + length + identifier
}
}
impl Encodable for TimeDescriptor {
fn encode(&self, writer: &mut BitWriter) -> EncodingResult<()> {
// splice_descriptor_tag (8 bits)
writer.write_bits(0x03u64, 8)?;
// descriptor_length (8 bits)
writer.write_bits(
(4 + self.tai_seconds.len() + self.tai_ns.len() + self.utc_offset.len()) as u64,
8,
)?;
// identifier (32 bits)
writer.write_bits(self.identifier as u64, 32)?;
// tai_seconds
writer.write_bytes(&self.tai_seconds)?;
// tai_ns
writer.write_bytes(&self.tai_ns)?;
// utc_offset
writer.write_bytes(&self.utc_offset)?;
Ok(())
}
fn encoded_size(&self) -> usize {
2 + 4 + self.tai_seconds.len() + self.tai_ns.len() + self.utc_offset.len()
}
}
impl Encodable for AudioDescriptor {
fn encode(&self, writer: &mut BitWriter) -> EncodingResult<()> {
// splice_descriptor_tag (8 bits)
writer.write_bits(0x04u64, 8)?;
// descriptor_length (8 bits)
writer.write_bits((4 + self.audio_components.len()) as u64, 8)?;
// identifier (32 bits)
writer.write_bits(self.identifier as u64, 32)?;
// audio_components
writer.write_bytes(&self.audio_components)?;
Ok(())
}
fn encoded_size(&self) -> usize {
2 + 4 + self.audio_components.len()
}
}