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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//! ISO/IEC 13818-1 (H.222.0) Transport-System Target Decoder (T-STD) buffer model.
//!
//! Implements the buffer-simulation engine that backs conformance indicators
//! 3.3 `Buffer_error`, 3.9 `Empty_buffer_error`, and 3.10 `Data_delay_error`
//! (ETSI TR 101 290 v1.4.1 Table 5.0c). Every buffer size, leak rate, and
//! threshold is a **named constant** citing its H.222.0 clause — transcribed
//! from the vendored PDF at `private/specs/itu_t_h222_0_202308_mpeg2_systems.pdf`.
//! The transcription lives at [`docs/h222_0-tstd-buffer-model.md`].
//!
//! # Architecture
//!
//! The monitor is **sans-IO** with a caller-supplied clock and no independent
//! hardware arrival timing. This model simulates the T-STD buffers using the
//! packet arrival time `t` provided in each [`crate::ConformanceMonitor::feed`]
//! call as the input clock. The caller is responsible for supplying timestamps
//! that are consistent with the transport rate; the model does **not** verify
//! that the inter-packet spacing matches any particular bitrate.
//!
//! Because arrival times are caller-supplied (not hardware), indicator 2.4
//! `PCR_accuracy_error` (±500 ns, H.222.0 §2.4.2.2) remains unimplemented — a
//! packet-index-derived arrival estimate cannot honestly resolve 500 ns.
//!
//! # References
//!
//! - ITU-T H.222.0 v9 (08/2023) | ISO/IEC 13818-1:2023 §2.4.2.1–§2.4.2.4 —
//! T-STD buffer model (symbols, sizes, leak rates)
//! - ITU-T H.222.0 §2.4.2.6 — T-STD for audio
//! - ISO/IEC 13818-4 §9.11.2 — TBsys_buffering_error
//! - ISO/IEC 13818-4 §9.1.4 — Buffer_error checks
//! - ISO/IEC 13818-9 Annex E — Extension for Real Time Interface
//! - ETSI TR 101 290 v1.4.1 Table 5.0c — indicators 3.3, 3.9, 3.10
//!
//! # Buffer-size grounding
//!
//! The eight constants below are grounded in two sources:
//! - **H.222.0 §2.4.2.4 for buffer sizes and rates**: TB_SIZE, TB_SYS_SIZE,
//! TB_SYS_LEAK_RATE, and TB_LEAK_RATE_FLOOR (the last is a modelling floor,
//! not a spec value, but bootstraps from §2.4.2.4's Rxn ≥ 2×10⁶ bps).
//! - **ETSI TR 101 290 v1.4.1 Table 5.0c for conformance thresholds**:
//! DATA_DELAY_LIMIT_SECS, TB_EMPTY_INTERVAL_SECS, TB_SYS_EMPTY_INTERVAL_SECS.
//!
//! See [`docs/h222_0-tstd-buffer-model.md`] for the full transcription.
//!
//! | Constant | Value | Source |
//! |----------|-------|--------|
//! | TB_SIZE | 512 bytes | H.222.0 §2.4.2.4, p.42 l.20: "The transport buffer size is fixed at 512 bytes." |
//! | TB_SYS_SIZE | 512 bytes | H.222.0 §2.4.2.4 (same line; TBSsys defined at §2.4.2.1 p.39 l.23) |
//! | TB_SYS_LEAK_RATE | 125 000 bytes/s | H.222.0 §2.4.2.4, p.42 l.10: "For systems data: Rxn = 1×10⁶ bits per second." Rxsys (§2.4.2.1 p.39 l.35) is TBsys's drain rate; the spec states no separate numeric value, so it is the systems-data Rxn. |
//! | TB_LEAK_RATE_FLOOR | 15 625 bytes/s | Modelling floor (not a spec value) — a conservative floor below any real DVB stream's coded rate, used only during bitrate convergence |
//! | DATA_DELAY_LIMIT_SECS | 1 s | TR 101 290 v1.4.1 Table 5.0c indicator 3.10 |
//! | TB_EMPTY_INTERVAL_SECS | 1 s | TR 101 290 v1.4.1 Table 5.0c indicator 3.9 |
//! | TB_SYS_EMPTY_INTERVAL_SECS | 1 s | TR 101 290 v1.4.1 Table 5.0c indicator 3.9 |
//!
//! # Known limitations
//!
//! - **MBn/EBn/Bn/Bsys not modelled**: these require codec-level buffer sizes
//! from descriptors (multiplex_buffer_descriptor, smoothing_buffer_descriptor,
//! etc.) and the full leak/vbv_delay methods. The current implementation
//! models only the front-end transport buffers (TBn, TBsys), which are common
//! to all stream types.
//! - **Bsys** (1536 bytes, H.222.0 §2.4.2.4 p.43 l.37) is deferred — the PSI
//! input buffer is downstream of TBsys and not currently modelled.
//! - **Still-picture differentiation not implemented**: TR 101 290 indicator 3.10
//! specifies a 60 s data-delay limit for still-picture video data versus
//! 1 s for all other data. The monitor applies only the 1 s limit because
//! detecting still-picture PIDs requires parsing the PMT's stream_type
//! (currently tracked only as a PID set, not typed). Implementing the
//! 60 s threshold is feasible but deferred pending PMT descriptor parsing.
//! - **No still-picture data-delay differentiation**: the monitor applies
//! only the 1 s limit from TR 101 290 indicator 3.10. The 60 s allowance
//! for still-picture video data is not implemented — see above.
//!
//! - **No arrival-time validation**: the model trusts the caller's timestamps.
use Duration;
// ── T-STD buffer constants ──────────────────────────────────────────────────
//
// Buffer sizes and rates: H.222.0 §2.4.2.4 ("Buffering").
// Conformance thresholds: ETSI TR 101 290 v1.4.1 Table 5.0c.
/// Size of a single elementary-stream transport buffer TBn.
///
/// H.222.0 §2.4.2.4, PDF page 42 line 20:
/// "The transport buffer size is fixed at **512 bytes**."
/// Defined at §2.4.2.1 (PDF p.39 l.24–25) as TBSn.
pub const TB_SIZE: u64 = 512;
/// Size of the system-information transport buffer TBsys.
///
/// H.222.0 §2.4.2.4 (same "fixed at 512 bytes" line as TBn).
/// Defined at §2.4.2.1 (PDF p.39 l.22–23) as TBSsys.
pub const TB_SYS_SIZE: u64 = 512;
/// Leak rate from TBsys (bytes per second).
///
/// H.222.0 §2.4.2.4, PDF page 42 line 10:
/// "For systems data: Rxn = **1×10⁶ bits per second**."
/// Rxsys (§2.4.2.1, PDF p.39 l.35) is the rate data leave TBsys;
/// the spec gives no separate numeric value for Rxsys, so it is the
/// systems-data Rxn. 1 000 000 bits/s ÷ 8 = 125 000 bytes/s.
pub const TB_SYS_LEAK_RATE: u64 = 1_000_000 / 8;
/// Floor leak rate for per-PID transport buffers TBn (bytes per second).
///
/// When the effective bitrate cannot be estimated (e.g. first packets of a
/// new PID), the TBn drain rate floors at 125 kbit/s = 15 625 bytes/s.
/// This is a **conservative modelling floor**, not a spec value — the real
/// Rxn for any video/audio PID is at least 2×10⁶ bps (§2.4.2.4).
///
/// For a 512-byte buffer draining at 15 625 bytes/s, the buffer empties
/// in ~32.8 ms — well within the 1 s empty-buffer check interval.
pub const TB_LEAK_RATE_FLOOR: u64 = 15_625;
/// Maximum data delay through the T-STD buffers.
///
/// ETSI TR 101 290 v1.4.1 Table 5.0c indicator 3.10: "Delay of data (except
/// still picture video data) through the T-STD buffers superior to 1 second".
///
/// Note: TR 101 290 allows 60 s for still-picture video data — the monitor
/// does NOT differentiate still-picture PIDs (see the "Known limitations"
/// section of the module doc). Detecting still-picture stream_type requires
/// PMT descriptor parsing which is not yet implemented.
pub const DATA_DELAY_LIMIT_SECS: u64 = 1;
/// Interval over which TBn must empty at least once.
///
/// ETSI TR 101 290 v1.4.1 Table 5.0c indicator 3.9: "Transport buffer (TBn)
/// not empty at least once per second".
pub const TB_EMPTY_INTERVAL_SECS: u64 = 1;
/// Interval over which TBsys must empty at least once.
///
/// ETSI TR 101 290 v1.4.1 Table 5.0c indicator 3.9: "transport buffer for
/// system information (TBsys) not empty at least once per second".
pub const TB_SYS_EMPTY_INTERVAL_SECS: u64 = 1;
// ── Buffer model state types ─────────────────────────────────────────────────
/// A single T-STD buffer with fixed size, tracking occupancy and drain timing.
///
/// Models the ISO/IEC 13818-1 §2.4.2.3 transport buffer: data arrives
/// instantaneously at the packet's arrival time and drains at a constant
/// leak rate. Overflow is detected when the sum of incoming data exceeds
/// the buffer capacity.
pub
/// Per-PID T-STD state: one transport buffer TBn + data-delay tracking.
pub
/// The monitors' T-STD buffer model state.
pub