File: 35.h

package info (click to toggle)
bitstream 1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,172 kB
  • sloc: ansic: 29,887; xml: 637; makefile: 101; sh: 5
file content (1537 lines) | stat: -rw-r--r-- 53,248 bytes parent folder | download
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
/*****************************************************************************
 * 35.h: SCTE 35 Digital Program Insertion Cueing Message for Cable
 *****************************************************************************
 * Copyright (C) 2015 VideoLAN
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *****************************************************************************/

/*
 * Normative references:
 *  - SCTE 35 2013 (Digital Program Insertion Cueing Message for Cable)
 */

#ifndef __BITSTREAM_SCTE_35_H__
#define __BITSTREAM_SCTE_35_H__

#include <bitstream/common.h>
#include <bitstream/mpeg/psi/psi.h>
#include <bitstream/mpeg/psi/descriptors.h>

#ifdef __cplusplus
extern "C"
{
#endif

/*****************************************************************************
 * Splice Information Table
 *****************************************************************************/
#define SCTE35_TABLE_ID                 0xfc
#define SCTE35_HEADER_SIZE              (PSI_HEADER_SIZE + 11)
#define SCTE35_HEADER2_SIZE             2

static inline void scte35_init(uint8_t *p_scte35)
{
    psi_set_tableid(p_scte35, SCTE35_TABLE_ID);
    psi_init(p_scte35, false);
    p_scte35[1] &= ~0x40; /* private indicator */
    p_scte35[3] = 0;
    p_scte35[4] = 0;
    p_scte35[9] = 0;
    p_scte35[10] = 0xff;
    p_scte35[11] = 0xf0;
}

static inline uint8_t scte35_get_protocol(const uint8_t *p_scte35)
{
    return p_scte35[3];
}

static inline void scte35_set_protocol(uint8_t *p_scte35, uint8_t i_version)
{
    p_scte35[3] = i_version;
}

static inline bool scte35_is_encrypted(const uint8_t *p_scte35)
{
    return !!(p_scte35[4] & 0x80);
}

static inline void scte35_set_encrypted(uint8_t *p_scte35, bool b_encrypted)
{
    if (b_encrypted)
        p_scte35[4] |= 0x80;
    else
        p_scte35[4] &= ~0x80;
}

static inline uint64_t scte35_get_pts_adjustment(const uint8_t *p_scte35)
{
    return ((uint64_t)(p_scte35[4] & 0x1) << 32) |
           ((uint64_t)p_scte35[5] << 24) | ((uint64_t)p_scte35[6] << 16) |
           ((uint64_t)p_scte35[7] << 8) | (uint64_t)p_scte35[8];
}

static inline void scte35_set_pts_adjustment(uint8_t *p_scte35,
                                             uint64_t i_adjustment)
{
    p_scte35[4] &= ~0x1;
    p_scte35[4] |= (i_adjustment >> 32) & 0x1;
    p_scte35[5] = (i_adjustment >> 24) & 0xff;
    p_scte35[6] = (i_adjustment >> 16) & 0xff;
    p_scte35[7] = (i_adjustment >> 8) & 0xff;
    p_scte35[8] = i_adjustment & 0xff;
}

static inline uint16_t scte35_get_command_length(const uint8_t *p_scte35)
{
    return ((p_scte35[11] & 0xf) << 8) | p_scte35[12];
}

static inline void scte35_set_command_length(uint8_t *p_scte35,
                                             uint16_t i_length)
{
    p_scte35[11] &= ~0xf;
    p_scte35[11] |= (i_length >> 8) & 0xf;
    p_scte35[12] = i_length & 0xff;
}

static inline uint8_t scte35_get_command_type(const uint8_t *p_scte35)
{
    return p_scte35[13];
}

static inline void scte35_set_command_type(uint8_t *p_scte35, uint8_t i_type)
{
    p_scte35[13] = i_type;
}

static inline uint8_t *scte35_get_command(const uint8_t *p_scte35)
{
    return (uint8_t *)p_scte35 + SCTE35_HEADER_SIZE;
}

static inline uint16_t scte35_get_desclength(const uint8_t *p_scte35)
{
    uint16_t i_command_length = scte35_get_command_length(p_scte35);
    if (i_command_length == 0xfff)
        return 0;
    const uint8_t *pi_desclength = scte35_get_command(p_scte35) +
                                   i_command_length;
    return (pi_desclength[0] << 8) | pi_desclength[1];
}

static inline void scte35_set_desclength(uint8_t *p_scte35, uint16_t i_length)
{
    uint8_t *pi_desclength = scte35_get_command(p_scte35) +
                             scte35_get_command_length(p_scte35);
    pi_desclength[0] = (i_length >> 8) & 0xff;
    pi_desclength[1] = i_length & 0xff;
}

static inline uint8_t *scte35_get_descl(const uint8_t *p_scte35)
{
    uint16_t i_command_length = scte35_get_command_length(p_scte35);
    if (i_command_length == 0xfff)
        return NULL;
    return scte35_get_command(p_scte35) + i_command_length +
           SCTE35_HEADER2_SIZE;
}

/*****************************************************************************
 * Splice Information Table - splice_time structure
 *****************************************************************************/
#define SCTE35_SPLICE_TIME_HEADER_SIZE              1
#define SCTE35_SPLICE_TIME_TIME_SIZE                4

static inline void scte35_splice_time_init(uint8_t *p_splice_time)
{
    p_splice_time[0] = 0x7f;
}

static inline bool scte35_splice_time_has_time_specified(const uint8_t *p_splice_time)
{
    return !!(p_splice_time[0] & 0x80);
}

static inline void scte35_splice_time_set_time_specified(uint8_t *p_splice_time,
        bool b_time_specified)
{
    if (b_time_specified)
        p_splice_time[0] |= 0x80;
    else
        p_splice_time[0] &= ~0x80;
}

static inline uint64_t scte35_splice_time_get_pts_time(const uint8_t *p_splice_time)
{
    return ((uint64_t)(p_splice_time[0] & 0x1) << 32) |
           ((uint64_t)p_splice_time[1] << 24) |
           ((uint64_t)p_splice_time[2] << 16) |
           ((uint64_t)p_splice_time[3] << 8) |
           (uint64_t)p_splice_time[4];
}

static inline void scte35_splice_time_set_pts_time(uint8_t *p_splice_time,
                                                   uint64_t i_pts_time)
{
    p_splice_time[0] &= ~0x1;
    p_splice_time[0] |= (i_pts_time >> 32) & 0x1;
    p_splice_time[1] = (i_pts_time >> 24) & 0xff;
    p_splice_time[2] = (i_pts_time >> 16) & 0xff;
    p_splice_time[3] = (i_pts_time >> 8) & 0xff;
    p_splice_time[4] = i_pts_time & 0xff;
}

static inline uint8_t scte35_splice_time_size(const uint8_t *p_splice_time)
{
    return SCTE35_SPLICE_TIME_HEADER_SIZE +
        (scte35_splice_time_has_time_specified(p_splice_time) ?
         SCTE35_SPLICE_TIME_TIME_SIZE : 0);
}

/*****************************************************************************
 * Splice Information Table - break_duration structure
 *****************************************************************************/
#define SCTE35_BREAK_DURATION_HEADER_SIZE              5u

static inline void scte35_break_duration_init(uint8_t *p_break_duration)
{
    p_break_duration[0] = 0xff;
}

static inline bool scte35_break_duration_has_auto_return(const uint8_t *p_break_duration)
{
    return !!(p_break_duration[0] & 0x80);
}

static inline void scte35_break_duration_set_auto_return(uint8_t *p_break_duration, bool b_auto_return)
{
    if (b_auto_return)
        p_break_duration[0] |= 0x80;
    else
        p_break_duration[0] &= ~0x80;
}

static inline uint64_t scte35_break_duration_get_duration(const uint8_t *p_break_duration)
{
    return ((uint64_t)(p_break_duration[0] & 0x1) << 32) |
           ((uint64_t)p_break_duration[1] << 24) |
           ((uint64_t)p_break_duration[2] << 16) |
           ((uint64_t)p_break_duration[3] << 8) |
           (uint64_t)p_break_duration[4];
}

static inline void scte35_break_duration_set_duration(uint8_t *p_break_duration,
                                                      uint64_t i_duration)
{
    p_break_duration[0] &= ~0x1;
    p_break_duration[0] |= (i_duration >> 32) & 0x1;
    p_break_duration[1] = (i_duration >> 24) & 0xff;
    p_break_duration[2] = (i_duration >> 16) & 0xff;
    p_break_duration[3] = (i_duration >> 8) & 0xff;
    p_break_duration[4] = i_duration & 0xff;
}

/*****************************************************************************
 * Splice Information Table - null command
 *****************************************************************************/
#define SCTE35_NULL_COMMAND                         0
#define SCTE35_NULL_HEADER_SIZE                     0

static inline void scte35_null_init(uint8_t *p_scte35)
{
    scte35_init(p_scte35);
    scte35_set_command_type(p_scte35, SCTE35_NULL_COMMAND);
    scte35_set_command_length(p_scte35, SCTE35_NULL_HEADER_SIZE);
    scte35_set_desclength(p_scte35, 0);
}

static inline bool scte35_null_validate(const uint8_t *p_scte35)
{
    return true;
}

/*****************************************************************************
 * Splice Information Table - schedule command
 *****************************************************************************/
#define SCTE35_SCHEDULE_COMMAND                     4

/* TODO Not implemented (useless) */

/*****************************************************************************
 * Splice Information Table - insert command
 *****************************************************************************/
#define SCTE35_INSERT_COMMAND                       5
#define SCTE35_INSERT_HEADER_SIZE                   5u
#define SCTE35_INSERT_HEADER2_SIZE                  1u
#define SCTE35_INSERT_COMPONENT_COUNT_SIZE          1
#define SCTE35_INSERT_COMPONENT_HEADER_SIZE         1
#define SCTE35_INSERT_FOOTER_SIZE                   4u

static inline void scte35_insert_init(uint8_t *p_scte35, uint16_t i_length)
{
    scte35_init(p_scte35);
    scte35_set_command_type(p_scte35, SCTE35_INSERT_COMMAND);
    scte35_set_command_length(p_scte35,
                              SCTE35_INSERT_HEADER_SIZE + i_length);
    scte35_set_desclength(p_scte35, 0);

    uint8_t *p_command = scte35_get_command(p_scte35);
    p_command[4] = 0xff;
}

static inline uint32_t scte35_insert_get_event_id(const uint8_t *p_scte35)
{
    const uint8_t *p_command = scte35_get_command(p_scte35);
    return ((uint32_t)p_command[0] << 24) | (p_command[1] << 16) |
           (p_command[2] << 8) | p_command[3];
}

static inline void scte35_insert_set_event_id(uint8_t *p_scte35,
                                              uint32_t i_event_id)
{
    uint8_t *p_command = scte35_get_command(p_scte35);
    p_command[0] = (i_event_id >> 24) & 0xff;
    p_command[1] = (i_event_id >> 16) & 0xff;
    p_command[2] = (i_event_id >> 8) & 0xff;
    p_command[3] = i_event_id & 0xff;
}

static inline bool scte35_insert_has_cancel(const uint8_t *p_scte35)
{
    const uint8_t *p_command = scte35_get_command(p_scte35);
    return !!(p_command[4] & 0x80);
}

static inline void scte35_insert_set_cancel(const uint8_t *p_scte35,
                                            bool b_cancel)
{
    uint8_t *p_command = scte35_get_command(p_scte35);
    if (b_cancel)
        p_command[4] |= 0x80;
    else {
        p_command[4] &= ~0x80;
        p_command[5] = 0xff;
    }
}

static inline bool scte35_insert_has_out_of_network(const uint8_t *p_scte35)
{
    const uint8_t *p_command = scte35_get_command(p_scte35);
    return !!(p_command[5] & 0x80);
}

static inline void scte35_insert_set_out_of_network(const uint8_t *p_scte35,
                                                    bool b_out_of_network)
{
    uint8_t *p_command = scte35_get_command(p_scte35);
    if (b_out_of_network)
        p_command[5] |= 0x80;
    else
        p_command[5] &= ~0x80;
}

static inline bool scte35_insert_has_program_splice(const uint8_t *p_scte35)
{
    const uint8_t *p_command = scte35_get_command(p_scte35);
    return !!(p_command[5] & 0x40);
}

static inline void scte35_insert_set_program_splice(const uint8_t *p_scte35,
                                                    bool b_program_splice)
{
    uint8_t *p_command = scte35_get_command(p_scte35);
    if (b_program_splice)
        p_command[5] |= 0x40;
    else
        p_command[5] &= ~0x40;
}

static inline bool scte35_insert_has_duration(const uint8_t *p_scte35)
{
    const uint8_t *p_command = scte35_get_command(p_scte35);
    return !!(p_command[5] & 0x20);
}

static inline void scte35_insert_set_duration(const uint8_t *p_scte35,
                                              bool b_duration)
{
    uint8_t *p_command = scte35_get_command(p_scte35);
    if (b_duration)
        p_command[5] |= 0x20;
    else
        p_command[5] &= ~0x20;
}

static inline bool scte35_insert_has_splice_immediate(const uint8_t *p_scte35)
{
    const uint8_t *p_command = scte35_get_command(p_scte35);
    return !!(p_command[5] & 0x10);
}

static inline void scte35_insert_set_splice_immediate(const uint8_t *p_scte35,
                                                      bool b_splice_immediate)
{
    uint8_t *p_command = scte35_get_command(p_scte35);
    if (b_splice_immediate)
        p_command[5] |= 0x10;
    else
        p_command[5] &= ~0x10;
}

static inline uint8_t *scte35_insert_get_splice_time(const uint8_t *p_scte35)
{
    uint8_t *p_command = scte35_get_command(p_scte35);
    return p_command + SCTE35_INSERT_HEADER_SIZE + SCTE35_INSERT_HEADER2_SIZE;
}

static inline uint8_t scte35_insert_get_component_count(const uint8_t *p_scte35)
{
    uint8_t *p_command = scte35_get_command(p_scte35);
    return p_command[SCTE35_INSERT_HEADER_SIZE + SCTE35_INSERT_HEADER2_SIZE];
}

static inline void scte35_insert_set_component_count(uint8_t *p_scte35,
                                                     uint8_t i_component_count)
{
    uint8_t *p_command = scte35_get_command(p_scte35);
    p_command[SCTE35_INSERT_HEADER_SIZE + SCTE35_INSERT_HEADER2_SIZE] =
        i_component_count;
}

static inline uint8_t scte35_insert_component_get_component_tag(const uint8_t *p_component)
{
    return p_component[0];
}

static inline void scte35_insert_component_set_component_tag(uint8_t *p_component, uint8_t i_component_tag)
{
    p_component[0] = i_component_tag;
}

static inline uint8_t *scte35_insert_component_get_splice_time(const uint8_t *p_component)
{
    return (uint8_t *)p_component + SCTE35_INSERT_COMPONENT_HEADER_SIZE;
}

static inline uint8_t *scte35_insert_get_component(const uint8_t *p_scte35,
                                                   uint8_t n)
{
    uint16_t i_section_size = psi_get_length(p_scte35) + PSI_HEADER_SIZE
                               - PSI_CRC_SIZE;
    bool b_splice_immediate = scte35_insert_has_splice_immediate(p_scte35);
    uint8_t *p_scte35_n = scte35_get_command(p_scte35) +
        SCTE35_INSERT_HEADER_SIZE + SCTE35_INSERT_HEADER2_SIZE +
        SCTE35_INSERT_COMPONENT_COUNT_SIZE;
    if (p_scte35_n - p_scte35 > i_section_size)
        return NULL;

    while (n) {
        if (p_scte35_n + SCTE35_INSERT_COMPONENT_HEADER_SIZE - p_scte35 >
                i_section_size)
            return NULL;
        p_scte35_n += SCTE35_INSERT_COMPONENT_HEADER_SIZE +
            (b_splice_immediate ?
             scte35_splice_time_size(
                 scte35_insert_component_get_splice_time(p_scte35_n)) :
             0);
        n--;
    }
    if (p_scte35_n - p_scte35 >= i_section_size) return NULL;
    return p_scte35_n;
}

static inline uint8_t *scte35_insert_get_break_duration(const uint8_t *p_scte35)
{
    if (!scte35_insert_has_program_splice(p_scte35))
        return scte35_insert_get_component(p_scte35,
                scte35_insert_get_component_count(p_scte35) + 1);

    if (scte35_insert_has_splice_immediate(p_scte35))
        return scte35_get_command(p_scte35) + SCTE35_INSERT_HEADER_SIZE +
               SCTE35_INSERT_HEADER2_SIZE;

    uint8_t *p_splice_time = scte35_insert_get_splice_time(p_scte35);
    return p_splice_time + scte35_splice_time_size(p_splice_time);
}

static inline uint8_t *scte35_insert_get_footer(const uint8_t *p_scte35)
{
    return scte35_insert_get_break_duration(p_scte35) +
        (scte35_insert_has_duration(p_scte35) ?
         SCTE35_BREAK_DURATION_HEADER_SIZE : 0);
}

static inline uint16_t scte35_insert_get_unique_program_id(const uint8_t *p_scte35)
{
    uint8_t *p_footer = scte35_insert_get_footer(p_scte35);
    return (p_footer[0] << 8) | p_footer[1];
}

static inline void scte35_insert_set_unique_program_id(uint8_t *p_scte35,
        uint16_t i_unique_program_id)
{
    uint8_t *p_footer = scte35_insert_get_footer(p_scte35);
    p_footer[0] = (i_unique_program_id >> 8) & 0xff;
    p_footer[1] = i_unique_program_id & 0xff;
}

static inline uint8_t scte35_insert_get_avail_num(const uint8_t *p_scte35)
{
    uint8_t *p_footer = scte35_insert_get_footer(p_scte35);
    return p_footer[2];
}

static inline void scte35_insert_set_avail_num(uint8_t *p_scte35,
                                               uint8_t i_avail_num)
{
    uint8_t *p_footer = scte35_insert_get_footer(p_scte35);
    p_footer[2] = i_avail_num;
}

static inline uint8_t scte35_insert_get_avails_expected(const uint8_t *p_scte35)
{
    uint8_t *p_footer = scte35_insert_get_footer(p_scte35);
    return p_footer[3];
}

static inline void scte35_insert_set_avails_expected(uint8_t *p_scte35,
                                                     uint8_t i_avails_expected)
{
    uint8_t *p_footer = scte35_insert_get_footer(p_scte35);
    p_footer[3] = i_avails_expected;
}

static inline bool scte35_insert_validate(const uint8_t *p_scte35)
{
    size_t i_length = scte35_get_command_length(p_scte35);
    if (i_length < SCTE35_INSERT_HEADER_SIZE)
        return false;

    if (scte35_insert_has_cancel(p_scte35))
        return i_length <= SCTE35_INSERT_HEADER_SIZE;
    if (i_length < SCTE35_INSERT_HEADER_SIZE + SCTE35_INSERT_HEADER2_SIZE)
        return false;

    if (scte35_insert_has_program_splice(p_scte35)) {
        if (scte35_insert_has_splice_immediate(p_scte35)) {
            if (scte35_insert_has_duration(p_scte35))
                return i_length >= SCTE35_INSERT_HEADER_SIZE +
                                   SCTE35_INSERT_HEADER2_SIZE +
                                   SCTE35_BREAK_DURATION_HEADER_SIZE +
                                   SCTE35_INSERT_FOOTER_SIZE;
            return i_length >= SCTE35_INSERT_HEADER_SIZE +
                               SCTE35_INSERT_HEADER2_SIZE +
                               SCTE35_INSERT_FOOTER_SIZE;
        }

        if (i_length < SCTE35_INSERT_HEADER_SIZE + SCTE35_INSERT_HEADER2_SIZE +
                       SCTE35_SPLICE_TIME_HEADER_SIZE ||
            i_length < SCTE35_INSERT_HEADER_SIZE + SCTE35_INSERT_HEADER2_SIZE +
                       scte35_splice_time_size(
                           scte35_insert_get_splice_time(p_scte35)))
            return false;

        if (scte35_insert_has_duration(p_scte35))
            return i_length >= SCTE35_INSERT_HEADER_SIZE +
                SCTE35_INSERT_HEADER2_SIZE +
                scte35_splice_time_size(scte35_insert_get_splice_time(p_scte35))
                + SCTE35_BREAK_DURATION_HEADER_SIZE +
                SCTE35_INSERT_FOOTER_SIZE;
        return i_length >= SCTE35_INSERT_HEADER_SIZE +
            SCTE35_INSERT_HEADER2_SIZE +
            scte35_splice_time_size(scte35_insert_get_splice_time(p_scte35))
            + SCTE35_INSERT_FOOTER_SIZE;
    }

    if (i_length < SCTE35_INSERT_HEADER_SIZE + SCTE35_INSERT_HEADER2_SIZE +
                   SCTE35_INSERT_COMPONENT_COUNT_SIZE)
        return false;

    const uint8_t *p_command = scte35_get_command(p_scte35);
    const uint8_t *p_end = scte35_insert_get_component(p_scte35,
            scte35_insert_get_component_count(p_scte35) + 1);
    if (p_end == NULL)
        return false;

    if (scte35_insert_has_duration(p_scte35))
        return i_length >= (size_t)(p_end + SCTE35_BREAK_DURATION_HEADER_SIZE +
                                   SCTE35_INSERT_FOOTER_SIZE - p_command);
    return i_length >= (size_t)(p_end + SCTE35_INSERT_FOOTER_SIZE - p_command);
}

/*****************************************************************************
 * Splice Information Table - time_signal command
 *****************************************************************************/
#define SCTE35_TIME_SIGNAL_COMMAND              6
#define SCTE35_TIME_SIGNAL_HEADER_SIZE          SCTE35_SPLICE_TIME_HEADER_SIZE
#define SCTE35_TIME_SIGNAL_TIME_SIZE            SCTE35_SPLICE_TIME_TIME_SIZE

static inline void scte35_time_signal_init(uint8_t *p_scte35, uint16_t i_length)
{
    scte35_init(p_scte35);
    scte35_set_command_type(p_scte35, SCTE35_TIME_SIGNAL_COMMAND);
    scte35_set_command_length(p_scte35,
                              SCTE35_TIME_SIGNAL_HEADER_SIZE + i_length);
    scte35_set_desclength(p_scte35, 0);
}

static inline uint8_t *scte35_time_signal_get_splice_time(const uint8_t *p_scte35)
{
    return scte35_get_command(p_scte35);
}

static inline bool scte35_time_signal_validate(const uint8_t *p_scte35)
{
    return scte35_get_command_length(p_scte35) >=
        scte35_splice_time_size(scte35_time_signal_get_splice_time(p_scte35));
}

/*****************************************************************************
 * Splice Information Table - bandwidth_reservation command
 *****************************************************************************/
#define SCTE35_BANDWIDTH_RESERVATION_COMMAND        7
#define SCTE35_BANDWIDTH_RESERVATION_HEADER_SIZE    0

static inline void scte35_bandwidth_reservation_init(uint8_t *p_scte35)
{
    scte35_init(p_scte35);
    scte35_set_command_type(p_scte35, SCTE35_BANDWIDTH_RESERVATION_COMMAND);
    scte35_set_command_length(p_scte35,
                              SCTE35_BANDWIDTH_RESERVATION_HEADER_SIZE);
    scte35_set_desclength(p_scte35, 0);
}

static inline bool scte35_bandwidth_reservation_validate(const uint8_t *p_scte35)
{
    return true;
}

/*****************************************************************************
 * Splice Information Table - private command
 *****************************************************************************/
#define SCTE35_PRIVATE_COMMAND                      0xff
#define SCTE35_PRIVATE_HEADER_SIZE                  4

static inline void scte35_private_init(uint8_t *p_scte35, uint16_t i_length)
{
    scte35_init(p_scte35);
    scte35_set_command_type(p_scte35, SCTE35_PRIVATE_COMMAND);
    scte35_set_command_length(p_scte35,
                              SCTE35_PRIVATE_HEADER_SIZE + i_length);
    scte35_set_desclength(p_scte35, 0);
}

static inline uint32_t scte35_private_get_identifier(const uint8_t *p_scte35)
{
    const uint8_t *p_command = scte35_get_command(p_scte35);
    return ((uint32_t)p_command[0] << 24) | (p_command[1] << 16) |
           (p_command[2] << 8) | p_command[3];
}

static inline void scte35_private_set_identifier(uint8_t *p_scte35,
                                                 uint32_t i_identifier)
{
    uint8_t *p_command = scte35_get_command(p_scte35);
    p_command[0] = (i_identifier >> 24) & 0xff;
    p_command[1] = (i_identifier >> 16) & 0xff;
    p_command[2] = (i_identifier >> 8) & 0xff;
    p_command[3] = i_identifier & 0xff;
}

static inline bool scte35_private_validate(const uint8_t *p_scte35)
{
    return scte35_get_command_length(p_scte35) >= SCTE35_PRIVATE_HEADER_SIZE;
}

/*****************************************************************************
 * Splice Information Table - splice descriptor
 *****************************************************************************/
#define SCTE35_SPLICE_DESC_HEADER_SIZE  6

#define SCTE35_SPLICE_DESC_IDENTIFIER   0x43554549

#define SCTE35_SPLICE_DESC_TAG_AVAIL    0x00
#define SCTE35_SPLICE_DESC_TAG_DTMF     0x01
#define SCTE35_SPLICE_DESC_TAG_SEG      0x02
#define SCTE35_SPLICE_DESC_TAG_TIME     0x03

#define scte35_splice_desc_get_tag      desc_get_tag
#define scte35_splice_desc_set_tag      desc_set_tag
#define scte35_splice_desc_get_length   desc_get_length
#define scte35_splice_desc_set_length   desc_set_length

static inline uint32_t scte35_splice_desc_get_identifier(const uint8_t *p_desc)
{
    return ((uint32_t)p_desc[2] << 24) | (p_desc[3] << 16) |
        (p_desc[4] << 8) | p_desc[5];
}

static inline void scte35_splice_desc_set_identifier(uint8_t *p_desc,
                                                     uint32_t identifier)
{
    p_desc[2] = (identifier >> 24) & 0xff;
    p_desc[3] = (identifier >> 16) & 0xff;
    p_desc[4] = (identifier >> 8) & 0xff;
    p_desc[5] = identifier & 0xff;
}

/*****************************************************************************
 * Splice Information Table - avail descriptor
 *****************************************************************************/
#define SCTE35_AVAIL_DESC_HEADER_SIZE       4

static inline void scte35_avail_desc_init(uint8_t *p_desc)
{
    scte35_splice_desc_set_tag(p_desc, SCTE35_SPLICE_DESC_TAG_AVAIL);
    scte35_splice_desc_set_length(p_desc,
                                  SCTE35_SPLICE_DESC_HEADER_SIZE -
                                  DESC_HEADER_SIZE +
                                  SCTE35_AVAIL_DESC_HEADER_SIZE);
    scte35_splice_desc_set_identifier(p_desc, SCTE35_SPLICE_DESC_IDENTIFIER);
    p_desc[10] = 0x7f;
}

static inline uint32_t
scte35_avail_desc_get_provider_avail_id(const uint8_t *p_desc)
{
    return ((uint32_t)p_desc[6] << 24) | (p_desc[7] << 16) |
        (p_desc[8] << 8) | p_desc[9];
}

static inline void scte35_avail_desc_set_provider_avail_id(uint8_t *p_desc,
                                                           uint32_t id)
{
    p_desc[6] = (id >> 24) & 0xff;
    p_desc[7] = (id >> 16) & 0xff;
    p_desc[8] = (id >> 8) & 0xff;
    p_desc[9] = id & 0xff;
}

/*****************************************************************************
 * Splice Information Table - segmentation descriptor
 *****************************************************************************/
#define SCTE35_SEG_DESC_HEADER_SIZE         5
#define SCTE35_SEG_DESC_NO_CANCEL_SIZE      6
#define SCTE35_SEG_DESC_NO_PROG_SEG_SIZE    1
#define SCTE35_SEG_DESC_COMPONENT_SIZE      6
#define SCTE35_SEG_DESC_DURATION_SIZE       5
#define SCTE35_SEG_DESC_SUB_SEG_SIZE        2

static inline void scte35_seg_desc_init(uint8_t *p_desc, uint8_t length)
{
    scte35_splice_desc_set_tag(p_desc, SCTE35_SPLICE_DESC_TAG_SEG);
    scte35_splice_desc_set_length(p_desc,
                                  SCTE35_SPLICE_DESC_HEADER_SIZE -
                                  DESC_HEADER_SIZE +
                                  SCTE35_SEG_DESC_HEADER_SIZE + length);
    scte35_splice_desc_set_identifier(p_desc, SCTE35_SPLICE_DESC_IDENTIFIER);
    p_desc[10] = 0x7f;
}

static inline uint32_t scte35_seg_desc_get_event_id(const uint8_t *p_desc)
{
    return ((uint32_t)p_desc[6] << 24) | (p_desc[7] << 16) |
        (p_desc[8] << 8) | p_desc[9];
}

static inline void scte35_seg_desc_set_event_id(uint8_t *p_desc,
                                                uint32_t event_id)
{
    p_desc[6] = (event_id >> 24) & 0xff;
    p_desc[7] = (event_id >> 16) & 0xff;
    p_desc[8] = (event_id >> 8) & 0xff;
    p_desc[9] = event_id & 0xff;
}

static inline bool scte35_seg_desc_has_cancel(const uint8_t *p_desc)
{
    return !!(p_desc[10] & 0x80);
}

static inline void scte35_seg_desc_set_cancel(uint8_t *p_desc,
                                              bool flag)
{
    p_desc[10] = (p_desc[10] & 0x7f) | (flag ? 0x80 : 0x00);
}

static inline bool scte35_seg_desc_has_program_seg(const uint8_t *p_desc)
{
    return !scte35_seg_desc_has_cancel(p_desc) &&
        !!(p_desc[11] & 0x80);
}

static inline void scte35_seg_desc_set_program_seg(uint8_t *p_desc,
                                                   bool flag)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return;
    p_desc[11] = (p_desc[11] & 0x7f) | (flag ? 0x80 : 0x00);
}

static inline bool scte35_seg_desc_has_duration(const uint8_t *p_desc)
{
    return !scte35_seg_desc_has_cancel(p_desc) &&
        !!(p_desc[11] & 0x40);
}

static inline void scte35_seg_desc_set_has_duration(uint8_t *p_desc,
                                                    bool flag)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return;
    p_desc[11] = (p_desc[11] & 0xbf) | (flag ? 0x40 : 0x00);
}

static inline bool
scte35_seg_desc_has_delivery_not_restricted(const uint8_t *p_desc)
{
    return !scte35_seg_desc_has_cancel(p_desc) &&
        !!(p_desc[11] & 0x20);
}

static inline void
scte35_seg_desc_set_delivery_not_restricted(uint8_t *p_desc,
                                            bool flag)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return;
    p_desc[11] = (p_desc[11] & 0xdf) | (flag ? 0x20 : 0x00);
    if (flag)
        p_desc[11] |= 0x1f;
}

static inline bool
scte35_seg_desc_has_web_delivery_allowed(const uint8_t *p_desc)
{
    return !scte35_seg_desc_has_delivery_not_restricted(p_desc) &&
        !!(p_desc[11] & 0x10);
}

static inline void
scte35_seg_desc_set_web_delivery_allowed(uint8_t *p_desc,
                                         bool flag)
{
    if (scte35_seg_desc_has_cancel(p_desc) ||
        scte35_seg_desc_has_delivery_not_restricted(p_desc))
        return;
    p_desc[11] = (p_desc[11] & 0xef) | (flag ? 0x10 : 0x00);
}

static inline bool
scte35_seg_desc_has_no_regional_blackout(const uint8_t *p_desc)
{
    return !scte35_seg_desc_has_delivery_not_restricted(p_desc) &&
        !!(p_desc[11] & 0x08);
}

static inline void
scte35_seg_desc_set_no_regional_blackout(uint8_t *p_desc,
                                         bool flag)
{
    if (scte35_seg_desc_has_cancel(p_desc) ||
        scte35_seg_desc_has_delivery_not_restricted(p_desc))
        return;
    p_desc[11] = (p_desc[11] & 0xf7) | (flag ? 0x08 : 0x00);
}

static inline bool
scte35_seg_desc_has_archive_allowed(const uint8_t *p_desc)
{
    return !scte35_seg_desc_has_delivery_not_restricted(p_desc) &&
        !!(p_desc[11] & 0x04);
}

static inline void
scte35_seg_desc_set_archive_allowed(uint8_t *p_desc,
                                    bool flag)
{
    if (scte35_seg_desc_has_cancel(p_desc) ||
        scte35_seg_desc_has_delivery_not_restricted(p_desc))
        return;
    p_desc[11] = (p_desc[11] & 0xfb) | (flag ? 0x04 : 0x00);
}

#define SCTE35_SEG_DESC_DEVICE_RESTRICTION_GRP0 0x00
#define SCTE35_SEG_DESC_DEVICE_RESTRICTION_GRP1 0x01
#define SCTE35_SEG_DESC_DEVICE_RESTRICTION_GRP2 0x02
#define SCTE35_SEG_DESC_DEVICE_RESTRICTION_NONE 0x03

static inline uint8_t
scte35_seg_desc_get_device_restrictions(const uint8_t *p_desc)
{
    return p_desc[11] & 0x03;
}

static inline void
scte35_seg_desc_set_device_restrictions(uint8_t *p_desc,
                                        uint8_t value)
{
    if (scte35_seg_desc_has_cancel(p_desc) ||
        scte35_seg_desc_has_delivery_not_restricted(p_desc))
        return;
    p_desc[11] = (p_desc[11] & 0xfc) | value;
}

static inline uint8_t scte35_seg_desc_get_component_count(const uint8_t *p_desc)
{
    return scte35_seg_desc_has_program_seg(p_desc) ? 0 : p_desc[12];
}

static inline void scte35_seg_desc_set_component_count(uint8_t *p_desc,
                                                       uint8_t count)
{
    if (scte35_seg_desc_has_program_seg(p_desc))
        return;
    p_desc[12] = count;
}

static inline uint8_t *scte35_seg_desc_get_component(const uint8_t *p_desc,
                                                     uint8_t i)
{
    if (i < scte35_seg_desc_get_component_count(p_desc))
        return (uint8_t *)p_desc + 13 + 6 * i;
    return NULL;
}

static inline void scte35_seg_desc_component_init(uint8_t *p_comp)
{
    p_comp[1] = 0xfe;
}

static inline uint8_t scte35_seg_desc_component_get_tag(const uint8_t *p_comp)
{
    return p_comp[0];
}

static inline void scte35_seg_desc_component_set_tag(uint8_t *p_comp,
                                                     uint8_t value)
{
    p_comp[0] = value;
}

static inline uint64_t
scte35_seg_desc_component_get_pts_off(const uint8_t *p_comp)
{
    return (((uint64_t)p_comp[1] & 0x1) << 32) | (p_comp[2] << 24) |
        (p_comp[3] << 16) | (p_comp[4] << 8) | p_comp[5];
}

static inline void
scte35_seg_desc_component_set_pts_off(uint8_t *p_comp, uint64_t value)
{
    p_comp[1] = (p_comp[1] & 0xfe) | ((value >> 32) & 0x01);
    p_comp[2] = (value >> 24) & 0xff;
    p_comp[3] = (value >> 16) & 0xff;
    p_comp[4] = (value >> 8) & 0xff;
    p_comp[5] = value & 0xff;
}

static inline uint64_t scte35_seg_desc_get_duration(const uint8_t *p_desc)
{
    if (scte35_seg_desc_has_cancel(p_desc) ||
        !scte35_seg_desc_has_duration(p_desc))
        return 0;
    const uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    return ((uint64_t)p[0] << 32) | ((uint64_t)p[1] << 24) |
        ((uint64_t)p[2] << 16) | ((uint64_t)p[3] << 8) |
        (uint64_t)p[4];
}

static inline void scte35_seg_desc_set_duration(uint8_t *p_desc,
                                                uint64_t value)
{
    if (scte35_seg_desc_has_cancel(p_desc) ||
        !scte35_seg_desc_has_duration(p_desc))
        return;
    uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    p[0] = (value >> 32) & 0xff;
    p[1] = (value >> 24) & 0xff;
    p[2] = (value >> 16) & 0xff;
    p[3] = (value >> 8) & 0xff;
    p[4] = value & 0xff;
}

#define SCTE35_SEG_DESC_UPID_TYPE_NOT_USED           0x00
#define SCTE35_SEG_DESC_UPID_TYPE_USER_DEFINED       0x01 /* deprecated */
#define SCTE35_SEG_DESC_UPID_TYPE_ISCI               0x02 /* deprecated */
#define SCTE35_SEG_DESC_UPID_TYPE_AD_ID              0x03
#define SCTE35_SEG_DESC_UPID_TYPE_UMID               0x04
#define SCTE35_SEG_DESC_UPID_TYPE_ISAN_DEPRECATED    0x05 /* deprecated */
#define SCTE35_SEG_DESC_UPID_TYPE_ISAN               0x06
#define SCTE35_SEG_DESC_UPID_TYPE_TID                0x07
#define SCTE35_SEG_DESC_UPID_TYPE_TI                 0x08
#define SCTE35_SEG_DESC_UPID_TYPE_ADI                0x09
#define SCTE35_SEG_DESC_UPID_TYPE_EIDR               0x0A
#define SCTE35_SEG_DESC_UPID_TYPE_ATSC               0x0B
#define SCTE35_SEG_DESC_UPID_TYPE_MPU                0x0C
#define SCTE35_SEG_DESC_UPID_TYPE_MID                0x0D
#define SCTE35_SEG_DESC_UPID_TYPE_ADS                0x0E
#define SCTE35_SEG_DESC_UPID_TYPE_URI                0x0F

static inline const char *scte35_seg_desc_upid_type_to_str(uint8_t upid)
{
    switch (upid) {
        case SCTE35_SEG_DESC_UPID_TYPE_NOT_USED:
            return "not_used";
        case SCTE35_SEG_DESC_UPID_TYPE_USER_DEFINED:
            return "user_defined";
        case SCTE35_SEG_DESC_UPID_TYPE_ISCI:
            return "ISCI";
        case SCTE35_SEG_DESC_UPID_TYPE_AD_ID:
            return "AD_ID";
        case SCTE35_SEG_DESC_UPID_TYPE_UMID:
            return "IMID";
        case SCTE35_SEG_DESC_UPID_TYPE_ISAN_DEPRECATED:
            return "ISAN_deprecated";
        case SCTE35_SEG_DESC_UPID_TYPE_ISAN:
            return "ISAN";
        case SCTE35_SEG_DESC_UPID_TYPE_TID:
            return "TID";
        case SCTE35_SEG_DESC_UPID_TYPE_TI:
            return "TI";
        case SCTE35_SEG_DESC_UPID_TYPE_ADI:
            return "ADI";
        case SCTE35_SEG_DESC_UPID_TYPE_EIDR:
            return "EIDR";
        case SCTE35_SEG_DESC_UPID_TYPE_ATSC:
            return "ATSC";
        case SCTE35_SEG_DESC_UPID_TYPE_MPU:
            return "MPU";
        case SCTE35_SEG_DESC_UPID_TYPE_MID:
            return "MID";
        case SCTE35_SEG_DESC_UPID_TYPE_ADS:
            return "ADS";
        case SCTE35_SEG_DESC_UPID_TYPE_URI:
            return "URI";
    }
    return "unknown";
}

static inline uint8_t scte35_seg_desc_get_upid_type(const uint8_t *p_desc)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return 0;
    const uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    return p[0];
}

static inline void scte35_seg_desc_set_upid_type(uint8_t *p_desc,
                                                 uint8_t value)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return;
    uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p[0] = value;
}

static inline uint8_t scte35_seg_desc_get_upid_length(const uint8_t *p_desc)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return 0;
    const uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    return p[1];
}

static inline void scte35_seg_desc_set_upid_length(uint8_t *p_desc,
                                                   uint8_t value)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return;
    uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p[1] = value;
}

static inline uint8_t *scte35_seg_desc_get_upid(const uint8_t *p_desc)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return NULL;
    const uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    return (uint8_t *)p + 2;
}

#define SCTE35_SEG_DESC_TYPE_ID_NOT_INDICATED            0x00
#define SCTE35_SEG_DESC_TYPE_ID_CONTENT_ID               0x01
#define SCTE35_SEG_DESC_TYPE_ID_PROG_START               0x10
#define SCTE35_SEG_DESC_TYPE_ID_PROG_END                 0x11
#define SCTE35_SEG_DESC_TYPE_ID_PROG_EARLY_TERM          0x12
#define SCTE35_SEG_DESC_TYPE_ID_PROG_BREAKAWAY           0x13
#define SCTE35_SEG_DESC_TYPE_ID_PROG_RESUMPTION          0x14
#define SCTE35_SEG_DESC_TYPE_ID_PROG_RUNOVER_PLANNED     0x15
#define SCTE35_SEG_DESC_TYPE_ID_PROG_RUNOVER_UNPLANNED   0x16
#define SCTE35_SEG_DESC_TYPE_ID_PROG_OVERLAP_START       0x17
#define SCTE35_SEG_DESC_TYPE_ID_PROG_BLACKOUT_OVERRIDE   0x18
#define SCTE35_SEG_DESC_TYPE_ID_PROG_START_IN_PROGRESS   0x19
#define SCTE35_SEG_DESC_TYPE_ID_CHAPTER_START            0x20
#define SCTE35_SEG_DESC_TYPE_ID_CHAPTER_END              0x21
#define SCTE35_SEG_DESC_TYPE_ID_BREAK_START              0x22
#define SCTE35_SEG_DESC_TYPE_ID_BREAK_END                0x23
#define SCTE35_SEG_DESC_TYPE_ID_PROVIDER_AD_START        0x30
#define SCTE35_SEG_DESC_TYPE_ID_PROVIDER_AD_END          0x31
#define SCTE35_SEG_DESC_TYPE_ID_DISTRIBUTOR_AD_START     0x32
#define SCTE35_SEG_DESC_TYPE_ID_DISTRIBUTOR_AD_END       0x33
#define SCTE35_SEG_DESC_TYPE_ID_PROVIDER_PO_START        0x34
#define SCTE35_SEG_DESC_TYPE_ID_PROVIDER_PO_END          0x35
#define SCTE35_SEG_DESC_TYPE_ID_DISTRIBUTOR_PO_START     0x36
#define SCTE35_SEG_DESC_TYPE_ID_DISTRIBUTOR_PO_END       0x37
#define SCTE35_SEG_DESC_TYPE_ID_UNSCHEDULED_EV_START     0x40
#define SCTE35_SEG_DESC_TYPE_ID_UNSCHEDULED_EV_END       0x41
#define SCTE35_SEG_DESC_TYPE_ID_NETWORK_START            0x50
#define SCTE35_SEG_DESC_TYPE_ID_NETWORK_END              0x51
/* SNPTV */
#define SCTE35_SEG_DESC_TYPE_ID_AD_SERVER                0x02

static inline const char *scte35_seg_desc_type_id_to_str(uint8_t type_id)
{
    switch (type_id) {
	case SCTE35_SEG_DESC_TYPE_ID_NOT_INDICATED:
		return "not_indicated";
	case SCTE35_SEG_DESC_TYPE_ID_CONTENT_ID:
		return "content_id";
	case SCTE35_SEG_DESC_TYPE_ID_PROG_START:
		return "prog_start";
	case SCTE35_SEG_DESC_TYPE_ID_PROG_END:
		return "prog_end";
	case SCTE35_SEG_DESC_TYPE_ID_PROG_EARLY_TERM:
		return "prog_early_term";
	case SCTE35_SEG_DESC_TYPE_ID_PROG_BREAKAWAY:
		return "prog_breakaway";
	case SCTE35_SEG_DESC_TYPE_ID_PROG_RESUMPTION:
		return "prog_resumption";
	case SCTE35_SEG_DESC_TYPE_ID_PROG_RUNOVER_PLANNED:
		return "prog_runover_planned";
	case SCTE35_SEG_DESC_TYPE_ID_PROG_RUNOVER_UNPLANNED:
		return "prog_runover_unplanned";
	case SCTE35_SEG_DESC_TYPE_ID_PROG_OVERLAP_START:
		return "prog_overlap_start";
	case SCTE35_SEG_DESC_TYPE_ID_PROG_BLACKOUT_OVERRIDE:
		return "prog_blackout_override";
	case SCTE35_SEG_DESC_TYPE_ID_PROG_START_IN_PROGRESS:
		return "prog_start_in_progress";
	case SCTE35_SEG_DESC_TYPE_ID_CHAPTER_START:
		return "chapter_start";
	case SCTE35_SEG_DESC_TYPE_ID_CHAPTER_END:
		return "chapter_end";
	case SCTE35_SEG_DESC_TYPE_ID_BREAK_START:
		return "break_start";
	case SCTE35_SEG_DESC_TYPE_ID_BREAK_END:
		return "break_end";
	case SCTE35_SEG_DESC_TYPE_ID_PROVIDER_AD_START:
		return "provider_ad_start";
	case SCTE35_SEG_DESC_TYPE_ID_PROVIDER_AD_END:
		return "provider_ad_end";
	case SCTE35_SEG_DESC_TYPE_ID_DISTRIBUTOR_AD_START:
		return "distributor_ad_start";
	case SCTE35_SEG_DESC_TYPE_ID_DISTRIBUTOR_AD_END:
		return "distributor_ad_end";
	case SCTE35_SEG_DESC_TYPE_ID_PROVIDER_PO_START:
		return "provider_po_start";
	case SCTE35_SEG_DESC_TYPE_ID_PROVIDER_PO_END:
		return "provider_po_end";
	case SCTE35_SEG_DESC_TYPE_ID_DISTRIBUTOR_PO_START:
		return "distributor_po_start";
	case SCTE35_SEG_DESC_TYPE_ID_DISTRIBUTOR_PO_END:
		return "distributor_po_end";
	case SCTE35_SEG_DESC_TYPE_ID_UNSCHEDULED_EV_START:
		return "unscheduled_ev_start";
	case SCTE35_SEG_DESC_TYPE_ID_UNSCHEDULED_EV_END:
		return "unscheduled_ev_end";
	case SCTE35_SEG_DESC_TYPE_ID_NETWORK_START:
		return "network_start";
	case SCTE35_SEG_DESC_TYPE_ID_NETWORK_END:
		return "network_end";

	case SCTE35_SEG_DESC_TYPE_ID_AD_SERVER:
		return "ad_server";
    }
    return "unknown";
}

static inline uint8_t scte35_seg_desc_get_type_id(const uint8_t *p_desc)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return 0;
    const uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p += 2;
    p += scte35_seg_desc_get_upid_length(p_desc);
    return p[0];
}

static inline void scte35_seg_desc_set_type_id(uint8_t *p_desc, uint8_t value)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return;
    uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p += 2;
    p += scte35_seg_desc_get_upid_length(p_desc);
    p[0] = value;
}

static inline uint8_t scte35_seg_desc_get_num(const uint8_t *p_desc)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return 0;
    const uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p += 2;
    p += scte35_seg_desc_get_upid_length(p_desc);
    return p[1];
}

static inline void scte35_seg_desc_set_num(uint8_t *p_desc, uint8_t value)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return;
    uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p += 2;
    p += scte35_seg_desc_get_upid_length(p_desc);
    p[1] = value;
}

static inline uint8_t scte35_seg_desc_get_expected(const uint8_t *p_desc)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return 0;
    const uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p += 2;
    p += scte35_seg_desc_get_upid_length(p_desc);
    return p[2];
}

static inline void scte35_seg_desc_set_expected(uint8_t *p_desc, uint8_t value)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return;
    uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p += 2;
    p += scte35_seg_desc_get_upid_length(p_desc);
    p[2] = value;
}

static inline bool scte35_seg_desc_has_sub_num(const uint8_t *p_desc)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return false;

    uint8_t type_id = scte35_seg_desc_get_type_id(p_desc);
    if (type_id != 0x34 && type_id != 0x36)
        return false;

    const uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p += 2;
    p += scte35_seg_desc_get_upid_length(p_desc);
    if (p + 3 <
        p_desc + scte35_splice_desc_get_length(p_desc) + DESC_HEADER_SIZE)
        return true;
    return false;
}

static inline uint8_t scte35_seg_desc_get_sub_num(const uint8_t *p_desc)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return 0;

    uint8_t type_id = scte35_seg_desc_get_type_id(p_desc);
    if (type_id != 0x34 && type_id != 0x36)
        return 0;

    const uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p += 2;
    p += scte35_seg_desc_get_upid_length(p_desc);
    if (p + 3 <
        p_desc + scte35_splice_desc_get_length(p_desc) + DESC_HEADER_SIZE)
        return p[3];
    return 0;
}

static inline void scte35_seg_desc_set_sub_num(uint8_t *p_desc, uint8_t value)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return;

    uint8_t type_id = scte35_seg_desc_get_type_id(p_desc);
    if (type_id != 0x34 && type_id != 0x36)
        return;

    uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p += 2;
    p += scte35_seg_desc_get_upid_length(p_desc);
    if (p + 3 <
        p_desc + scte35_splice_desc_get_length(p_desc) + DESC_HEADER_SIZE)
        p[3] = value;
}

static inline bool scte35_seg_desc_has_sub_expected(const uint8_t *p_desc)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return false;

    uint8_t type_id = scte35_seg_desc_get_type_id(p_desc);
    if (type_id != 0x34 && type_id != 0x36)
        return false;

    const uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p += 2;
    p += scte35_seg_desc_get_upid_length(p_desc);
    if (p + 4 <
        p_desc + scte35_splice_desc_get_length(p_desc) + DESC_HEADER_SIZE)
        return true;
    return false;
}

static inline uint8_t scte35_seg_desc_get_sub_expected(const uint8_t *p_desc)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return 0;

    uint8_t type_id = scte35_seg_desc_get_type_id(p_desc);
    if (type_id != 0x34 && type_id != 0x36)
        return 0;

    const uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p += 2;
    p += scte35_seg_desc_get_upid_length(p_desc);
    if (p + 4 <
        p_desc + scte35_splice_desc_get_length(p_desc) + DESC_HEADER_SIZE)
        return p[4];
    return 0;
}

static inline void scte35_seg_desc_set_sub_expected(uint8_t *p_desc,
                                                    uint8_t value)
{
    if (scte35_seg_desc_has_cancel(p_desc))
        return;

    uint8_t type_id = scte35_seg_desc_get_type_id(p_desc);
    if (type_id != 0x34 && type_id != 0x36)
        return;

    uint8_t *p = p_desc + 12;
    if (!scte35_seg_desc_has_program_seg(p_desc))
        p += 1 + 6 * scte35_seg_desc_get_component_count(p_desc);
    if (scte35_seg_desc_has_duration(p_desc))
        p += 5;
    p += 2;
    p += scte35_seg_desc_get_upid_length(p_desc);
    if (p + 4 <
        p_desc + scte35_splice_desc_get_length(p_desc) + DESC_HEADER_SIZE)
        p[4] = value;
}

/*****************************************************************************
 * Splice Information Table - time descriptor
 *****************************************************************************/
#define SCTE35_TIME_DESC_HEADER_SIZE        16

static inline uint64_t scte35_time_desc_get_tai_sec(const uint8_t *p_desc)
{
    return ((uint64_t)p_desc[6] << 40) | ((uint64_t)p_desc[7] << 32) |
        (p_desc[8] << 24) | (p_desc[9] << 16) | (p_desc[10] << 8) | p_desc[11];
}

static inline void scte35_time_desc_set_tai_sec(uint8_t *p_desc,
                                                uint64_t tai_sec)
{
    p_desc[6] = (tai_sec >> 40) & 0xff;
    p_desc[7] = (tai_sec >> 32) & 0xff;
    p_desc[8] = (tai_sec >> 24) & 0xff;
    p_desc[9] = (tai_sec >> 16) & 0xff;
    p_desc[10] = (tai_sec >> 8) & 0xff;
    p_desc[11] = tai_sec & 0xff;
}

static inline uint32_t scte35_time_desc_get_tai_ns(const uint8_t *p_desc)
{
    return ((uint32_t)p_desc[12] << 24) | (p_desc[13] << 16) |
        (p_desc[14] << 8) | p_desc[15];
}

static inline void scte35_time_desc_set_tai_ns(uint8_t *p_desc,
                                               uint32_t tai_ns)
{
    p_desc[12] = (tai_ns >> 24) & 0xff;
    p_desc[13] = (tai_ns >> 16) & 0xff;
    p_desc[14] = (tai_ns >> 8) & 0xff;
    p_desc[15] = tai_ns & 0xff;
}

static inline uint16_t scte35_time_desc_get_utc_off(const uint8_t *p_desc)
{
    return ((uint16_t)p_desc[16] << 8) | p_desc[17];
}

static inline void scte35_time_desc_set_utc_off(uint8_t *p_desc,
                                               uint16_t utc_off)
{
    p_desc[16] = (utc_off >> 8) & 0xff;
    p_desc[17] = utc_off & 0xff;
}

static inline void scte35_time_desc_init(uint8_t *p_desc)
{
    scte35_splice_desc_set_tag(p_desc, SCTE35_SPLICE_DESC_TAG_TIME);
    scte35_splice_desc_set_length(p_desc,
                                  SCTE35_SPLICE_DESC_HEADER_SIZE -
                                  DESC_HEADER_SIZE +
                                  SCTE35_TIME_DESC_HEADER_SIZE);
    scte35_splice_desc_set_identifier(p_desc, SCTE35_SPLICE_DESC_IDENTIFIER);
    scte35_time_desc_set_tai_sec(p_desc, 0);
    scte35_time_desc_set_tai_ns(p_desc, 0);
    scte35_time_desc_set_utc_off(p_desc, 0);
}

/*****************************************************************************
 * Splice Information Table validation
 *****************************************************************************/
static inline bool scte35_validate(const uint8_t *p_scte35)
{
    if (psi_get_syntax(p_scte35) ||
        psi_get_tableid(p_scte35) != SCTE35_TABLE_ID ||
        psi_get_length(p_scte35) <  SCTE35_HEADER2_SIZE + PSI_CRC_SIZE)
        return false;

    if (!psi_check_crc(p_scte35))
        return false;
    if (scte35_get_protocol(p_scte35))
        return false;

    uint16_t i_section_size = psi_get_length(p_scte35) + PSI_HEADER_SIZE
                               - PSI_CRC_SIZE;
    if (i_section_size < SCTE35_HEADER_SIZE)
        return false;

    uint16_t i_command_length = scte35_get_command_length(p_scte35);
    if (i_command_length != 0xfff &&
        (i_section_size < SCTE35_HEADER_SIZE + i_command_length +
                          SCTE35_HEADER2_SIZE ||
         i_section_size < SCTE35_HEADER_SIZE + i_command_length +
                          SCTE35_HEADER2_SIZE +
                          scte35_get_desclength(p_scte35)))
        return false;

    switch (scte35_get_command_type(p_scte35)) {
        case SCTE35_NULL_COMMAND:
            if (!scte35_null_validate(p_scte35))
                return false;
            break;
        case SCTE35_INSERT_COMMAND:
            if (!scte35_insert_validate(p_scte35))
                return false;
            break;
        case SCTE35_TIME_SIGNAL_COMMAND:
            if (!scte35_time_signal_validate(p_scte35))
                return false;
            break;
        case SCTE35_BANDWIDTH_RESERVATION_COMMAND:
            if (!scte35_bandwidth_reservation_validate(p_scte35))
                return false;
            break;
        case SCTE35_PRIVATE_COMMAND:
            if (!scte35_private_validate(p_scte35))
                return false;
            break;
        default:
            break;
    }

    if (i_command_length != 0xfff &&
        !descl_validate(scte35_get_descl(p_scte35),
                        scte35_get_desclength(p_scte35)))
        return false;

    return true;
}

#ifdef __cplusplus
}
#endif

#endif