dvb_gen_si.c 105 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * dvb_gen_si.c: Generate SI tables and descriptors
 *****************************************************************************
 * Copyright (c) 2011 Unix Solutions Ltd.
5
 * Copyright (c) 2015 VideoLAN
6 7 8
 *
 * Authors:
 *   Georgi Chorbadzhiyski <gf@unixsol.org>
9
 *   Christophe Massiot <massiot@via.ecp.fr>
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
 *
 * 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.
 *****************************************************************************/

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
38
#include <errno.h>
39 40 41 42 43

#include <bitstream/mpeg/ts.h>
#include <bitstream/mpeg/psi.h>
#include <bitstream/dvb/si.h>
#include <bitstream/dvb/si_print.h>
44 45
#include <bitstream/scte/35.h>
#include <bitstream/scte/35_print.h>
46 47 48 49 50 51 52

uint16_t tsid     = 10000;
uint16_t sid      = 20000;
uint16_t event_id = 30000;
uint16_t onid     = 40000;

uint16_t pmt_pid  = 100;
53
uint16_t scte35_pid  = 200;
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 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754

time_t ts_0 = 1234567890;
time_t ts_1 = 1;
time_t ts_2 = 999999999;
time_t ts_3 = 0x7fffffff;

uint8_t  cc = 0;

/* =========================================================================
 * MPEG defined descriptors
 * ========================================================================= */

/* MPEG Descriptor 0x02: Video stream descriptor */
static void build_desc02_1(uint8_t *desc) {
    desc02_init(desc);
    desc02_set_multiple_frame_rate (desc, true);
    desc02_set_frame_rate_code     (desc, 3); // 25.00
    desc02_set_mpeg1_only_flag     (desc, true);
    desc02_set_constraint_parameter(desc, true);
    desc02_set_still_picture       (desc, true);
}

static void build_desc02_2(uint8_t *desc) {
    desc02_init(desc);
    desc02_set_multiple_frame_rate (desc, true);
    desc02_set_frame_rate_code     (desc, 4); // 29.97
    desc02_set_mpeg1_only_flag     (desc, false); // Allow more flags
    desc02_set_constraint_parameter(desc, true);
    desc02_set_still_picture       (desc, true);
    desc02_set_profile             (desc, 4); // Main
    desc02_set_level               (desc, 8); // Main
    desc02_set_chroma_format       (desc, 2); // 4:2:2
    desc02_set_frame_rate_extension(desc, false);
}

/* MPEG Descriptor 0x03: Audio stream descriptor */
static void build_desc03(uint8_t *desc) {
    desc03_init(desc);
    desc03_set_free_format_flag(desc, true);
    desc03_set_ID(desc, true);
    desc03_set_layer(desc, 2); // Layer II
    desc03_set_vbr_flag(desc, true);
}

/* MPEG Descriptor 0x04: Hierarchy descriptor */
static void build_desc04(uint8_t *desc) {
    desc04_init(desc);
    desc04_set_type(desc, 1);
    desc04_set_layer_index(desc, 6);
    desc04_set_embedded_layer_index(desc, 7);
    desc04_set_channel(desc, 8);
}

/* MPEG Descriptor 0x05: Registration descriptor */
static void build_desc05(uint8_t *desc) {
    desc05_init(desc);
    desc05_set_identifier(desc, (uint8_t *)"TEST");
}

/* MPEG Descriptor 0x06: Data stream alignment descriptor */
static void build_desc06(uint8_t *desc) {
    desc06_init(desc);
    desc06_set_alignment(desc, 2); // Video access unit
}

/* MPEG Descriptor 0x07: Target Background Grid descriptor */
static void build_desc07(uint8_t *desc) {
    desc07_init(desc);
    desc07_set_horizontal_size(desc, 720);
    desc07_set_vertical_size(desc, 576);
    desc07_set_aspect_ratio_info(desc, 7);
}

/* MPEG Descriptor 0x08: Video Window descriptor */
static void build_desc08(uint8_t *desc) {
    desc08_init(desc);
    desc08_set_horizontal_offset(desc, 1024);
    desc08_set_vertical_offset(desc, 768);
    desc08_set_window_priority(desc, 15);
}

/* MPEG Descriptor 0x09: Conditional access descriptor */
static void build_desc09(uint8_t *desc) {
    desc09_init(desc);
    desc09_set_sysid(desc, 0xb000);
    desc09_set_pid(desc, 1234);
}

/* MPEG Descriptor 0x0A: ISO-639 language descriptor */
static void build_desc0a(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *lang_n;

    desc0a_init(desc);
    desc_set_length(desc, 255);

    lang_n = desc0a_get_language(desc, k++);
    desc0an_set_code(lang_n, (uint8_t *)"eng");
    desc0an_set_audiotype(lang_n, 0);

    lang_n = desc0a_get_language(desc, k++);
    desc0an_set_code(lang_n, (uint8_t *)"eng");
    desc0an_set_audiotype(lang_n, 1);

    lang_n = desc0a_get_language(desc, k++);
    desc0an_set_code(lang_n, (uint8_t *)"eng");
    desc0an_set_audiotype(lang_n, 2);

    lang_n = desc0a_get_language(desc, k++);
    desc0an_set_code(lang_n, (uint8_t *)"eng");
    desc0an_set_audiotype(lang_n, 3);

    lang_n = desc0a_get_language(desc, k++);
    desc0an_set_code(lang_n, (uint8_t *)"bul");
    desc0an_set_audiotype(lang_n, 4);

    lang_n = desc0a_get_language(desc, k++);
    desc0an_set_code(lang_n, (uint8_t *)"bul");
    desc0an_set_audiotype(lang_n, 0);

    lang_n = desc0a_get_language(desc, k);
    desc_set_length(desc, lang_n - desc - DESC_HEADER_SIZE);
}

/* MPEG Descriptor 0x0B: System clock descriptor */
static void build_desc0b(uint8_t *desc) {
    desc0b_init(desc);
    desc0b_set_external_clock_reference_indicator(desc, true);
    desc0b_set_clock_accuracy_integer(desc, 60);
    desc0b_set_clock_accuracy_exponent(desc, 4);
}

/* MPEG Descriptor 0x0C: Multiplex buffer utilization descriptor */
static void build_desc0c(uint8_t *desc) {
    desc0c_init(desc);
    desc0c_set_bound_valid(desc, true);
    desc0c_set_LTW_offset_lower_bound(desc, 1234);
    desc0c_set_LTW_offset_upper_bound(desc, 5678);
}

/* MPEG Descriptor 0x0D: Copyright descriptor */
static void build_desc0d(uint8_t *desc) {
    desc0d_init(desc);
    desc0d_set_copyright_identifier(desc, 0x12345678);
    desc0d_set_additional_copyright_info(desc, (uint8_t *)"ABCD", 4);
}

/* MPEG Descriptor 0x0E: Maximum bitrate descriptor */
static void build_desc0e(uint8_t *desc) {
    desc0e_init(desc);
    desc0e_set_max_bitrate(desc, 123456);
}

/* MPEG Descriptor 0x0F: Private data indicator descriptor */
static void build_desc0f(uint8_t *desc) {
    desc0f_init(desc);
    desc0f_set_private_indicator(desc, 0xaabbccdd);
}

/* MPEG Descriptor 0x10: Smoothing buffer descriptor */
static void build_desc10(uint8_t *desc) {
    desc10_init(desc);
    desc10_set_sb_leak_rate(desc, 112233);
    desc10_set_sb_size(desc, 4112233);
}

/* MPEG Descriptor 0x11: STD descriptor */
static void build_desc11(uint8_t *desc) {
    desc11_init(desc);
    desc11_set_leak_valid_flag(desc, true);
}

/* MPEG Descriptor 0x12: IBP descriptor */
static void build_desc12(uint8_t *desc) {
    desc12_init(desc);
    desc12_set_closed_gop_flag(desc, true);
    desc12_set_identical_gop_flag(desc, false);
    desc12_set_max_gop_length(desc, 5000);
}

/* MPEG Descriptor 0x1b: MPEG4 Video descriptor */
static void build_desc1b(uint8_t *desc) {
    desc1b_init(desc);
    desc1b_set_mpeg4_visual_profile_and_level(desc, 0x12);
}

/* MPEG Descriptor 0x1c: MPEG4 Audio descriptor */
static void build_desc1c(uint8_t *desc) {
    desc1c_init(desc);
    desc1c_set_mpeg4_audio_profile_and_level(desc, 0x13);
}

/* MPEG Descriptor 0x1d: IOD descriptor */
static void build_desc1d(uint8_t *desc) {
    desc1d_init(desc);
    desc1d_set_scope(desc, 0x01);
    desc1d_set_iod_label(desc, 0x23);
    desc1d_set_iod(desc, 0x45);
}

/* MPEG Descriptor 0x1e: SL descriptor */
static void build_desc1e(uint8_t *desc) {
    desc1e_init(desc);
    desc1e_set_es_id(desc, 0x1234);
}

/* MPEG Descriptor 0x1f: FMC descriptor */
static void build_desc1f(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *entry_n;

    desc1f_init(desc);
    desc_set_length(desc, 255);

    entry_n = desc1f_get_entry(desc, k++);
    desc1fn_set_es_id(entry_n, 0x1122);
    desc1fn_set_flexmux_channel(entry_n, 0x33);

    entry_n = desc1f_get_entry(desc, k++);
    desc1fn_set_es_id(entry_n, 0x4455);
    desc1fn_set_flexmux_channel(entry_n, 0x66);

    entry_n = desc1f_get_entry(desc, k++);
    desc1fn_set_es_id(entry_n, 0x7788);
    desc1fn_set_flexmux_channel(entry_n, 0x99);

    entry_n = desc1f_get_entry(desc, k);
    desc_set_length(desc, entry_n - desc - DESC_HEADER_SIZE);
}

/* MPEG Descriptor 0x20: External ES_ID descriptor */
static void build_desc20(uint8_t *desc) {
    desc20_init(desc);
    desc20_set_external_es_id(desc, 0x1234);
}

/* MPEG Descriptor 0x21: MuxCode descriptor */
static void build_desc21(uint8_t *desc) {
    uint8_t n = 0, k = 0, r;
    uint8_t *entry_n, *entry_k;

    desc21_init(desc);
    desc_set_length(desc, 255);

    entry_n = desc21_get_entry(desc, n++);
    desc21n_set_muxcode(entry_n, 1);
    desc21n_set_version(entry_n, 1);
    desc21n_set_length(entry_n, DESC21_ENTRY_HEADER_SIZE - 1);
    desc21n_set_substruct_count(entry_n, 0);

    entry_n = desc21_get_entry(desc, n++);
    desc21n_set_muxcode(entry_n, 2);
    desc21n_set_version(entry_n, 2);
    desc21n_set_length(entry_n, 255);
    desc21n_set_substruct_count(entry_n, 255);
    {
        k = 0;
        entry_k = desc21n_get_substruct(entry_n, k++);
        desc21k_set_repetition_count(entry_k, 1);
        desc21k_set_slot_count(entry_k, 0);

        entry_k = desc21n_get_substruct(entry_n, k++);
        desc21k_set_repetition_count(entry_k, 5);
        desc21k_set_slot_count(entry_k, 5);
        for (r=0; r<desc21k_get_slot_count(entry_k); r++) {
            desc21k_set_flex_mux_channel(entry_k, r, r + 10);
            desc21k_set_number_of_bytes(entry_k,  r, r + 20);
        }

        entry_k = desc21n_get_substruct(entry_n, k++);
        desc21k_set_repetition_count(entry_k, 3);
        desc21k_set_slot_count(entry_k, 1);
        for (r=0; r<desc21k_get_slot_count(entry_k); r++) {
            desc21k_set_flex_mux_channel(entry_k, r, r + 30);
            desc21k_set_number_of_bytes(entry_k,  r, r + 40);
        }

        entry_k = desc21n_get_substruct(entry_n, k);
        desc21n_set_length(entry_n, entry_k - entry_n - 1);
        desc21n_set_substruct_count(entry_n, k);
    }

    entry_n = desc21_get_entry(desc, n++);
    desc21n_set_muxcode(entry_n, 3);
    desc21n_set_version(entry_n, 3);
    desc21n_set_length(entry_n, DESC21_ENTRY_HEADER_SIZE - 1);
    desc21n_set_substruct_count(entry_n, 0);

    entry_n = desc21_get_entry(desc, n++);
    desc21n_set_muxcode(entry_n, 4);
    desc21n_set_version(entry_n, 4);
    desc21n_set_length(entry_n, 255);
    desc21n_set_substruct_count(entry_n, 255);
    {
        k = 0;
        entry_k = desc21n_get_substruct(entry_n, k++);
        desc21k_set_repetition_count(entry_k, 3);
        desc21k_set_slot_count(entry_k, 2);
        for (r=0; r<desc21k_get_slot_count(entry_k); r++) {
            desc21k_set_flex_mux_channel(entry_k, r, r + 50);
            desc21k_set_number_of_bytes(entry_k,  r, r + 60);
        }

        entry_k = desc21n_get_substruct(entry_n, k++);
        desc21k_set_repetition_count(entry_k, 4);
        desc21k_set_slot_count(entry_k, 4);
        for (r=0; r<desc21k_get_slot_count(entry_k); r++) {
            desc21k_set_flex_mux_channel(entry_k, r, r + 70);
            desc21k_set_number_of_bytes(entry_k,  r, r + 80);
        }

        entry_k = desc21n_get_substruct(entry_n, k);
        desc21n_set_length(entry_n, entry_k - entry_n - 1);
        desc21n_set_substruct_count(entry_n, k);
    }

    entry_n = desc21_get_entry(desc, n);
    desc_set_length(desc, entry_n - desc - DESC_HEADER_SIZE);
}

/* MPEG Descriptor 0x22: FmxBufferSize descriptor */
static void build_desc22(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *entry_n;

    desc22_init(desc);
    desc22_set_default_buffer_size(desc, 0x112233);

    desc_set_length(desc, 255);
    entry_n = desc22_get_entry(desc, k++);
    desc22n_set_flexmux_channel(entry_n, 0x11);
    desc22n_set_flexmux_buffer_size(entry_n, 0x223344);

    entry_n = desc22_get_entry(desc, k++);
    desc22n_set_flexmux_channel(entry_n, 0x55);
    desc22n_set_flexmux_buffer_size(entry_n, 0x667788);

    entry_n = desc22_get_entry(desc, k++);
    desc22n_set_flexmux_channel(entry_n, 0x99);
    desc22n_set_flexmux_buffer_size(entry_n, 0xaabbcc);

    entry_n = desc22_get_entry(desc, k);
    desc_set_length(desc, entry_n - desc - DESC_HEADER_SIZE);
}

/* MPEG Descriptor 0x23: MultiplexBuffer descriptor */
static void build_desc23(uint8_t *desc) {
    desc23_init(desc);
    desc23_set_mb_buffer_size(desc, 112233);
    desc23_set_tb_leak_rate(desc, 445566);
}

/* MPEG Descriptor 0x24: Content labeling descriptor */
static void build_desc24_1(uint8_t *desc) {
    desc24_init(desc);

    desc24_set_metadata_application_format(desc, 0xabcd);
    desc24_set_content_reference_id_record_flag(desc, false);
    desc24_set_content_time_base_indicator(desc, 0);

    desc24_set_length(desc);
}

static void build_desc24_2(uint8_t *desc) {
    desc24_init(desc);

    desc24_set_metadata_application_format(desc, 0xfff0);

    desc24_set_content_reference_id_record_flag(desc, false);

    desc24_set_content_time_base_indicator(desc, 3);
    desc24_set_time_base_association(desc, 3, (uint8_t *)"ABC");

    desc24_set_length(desc);
}

static void build_desc24_3(uint8_t *desc) {
    desc24_init(desc);

    desc24_set_metadata_application_format(desc, 0xffff);
    // Needs desc24_set_metadata_application_format(desc, 0xffff);
    desc24_set_metadata_application_format_identifier(desc, 0xaabbccdd);

    desc24_set_content_reference_id_record_flag(desc, true);
    // Needs desc24_set_content_reference_id_record_flag(desc, true);
    desc24_set_content_reference_id_record(desc, 3, (uint8_t *)"abc");

    desc24_set_content_time_base_indicator(desc, 2);
    // Needs desc24_set_content_time_base_indicator(desc, 1..2);
    desc24_set_content_time_base_value (desc, 0x1F0F0F0F0); // 8337289456
    desc24_set_metadata_time_base_value(desc, 0x1F1F1F1F1); // 8354132465

    // Needs desc24_set_content_time_base_indicator(desc, 2);
    desc24_set_content_id(desc, 100);

    // Needs desc24_set_content_time_base_indicator(desc, 3..7);
    desc24_set_time_base_association(desc, 3, (uint8_t *)"ABC");

    desc24_set_length(desc);
}

/* MPEG Descriptor 0x25: Metadata pointer descriptor */
static void build_desc25(uint8_t *desc) {
    desc25_init(desc);

    desc25_set_metadata_application_format(desc, 0xffff);
    // Needs desc25_set_metadata_application_format(desc, 0xffff);
    desc25_set_metadata_application_format_identifier(desc, 0x00112233);

    desc25_set_metadata_format(desc, 0xff);
    // Needs desc25_set_metadata_format(desc, 0xff);
    desc25_set_metadata_format_identifier(desc, 0xdeadbeaf);

    desc25_set_metadata_service_id(desc, 0x88);

    desc25_set_metadata_locator_record_flag(desc, true);
    // Needs desc25_set_metadata_locator_record_flag(desc, true);
    desc25_set_metadata_locator_record(desc, 3, (uint8_t *)"abc");

    desc25_set_mpeg_carriage_flags(desc, 1);
    // Needs desc25_set_mpeg_carriage_flags(desc, 0..2);
    desc25_set_program_number(desc, 1000);
    // Needs desc25_set_mpeg_carriage_flags(desc, 1);
    desc25_set_ts_location(desc, 2000);
    desc25_set_ts_id(desc, 3000);

    desc25_set_length(desc);
}

/* MPEG Descriptor 0x26: Metadata descriptor */
static void build_desc26_1(uint8_t *desc) {
    desc26_init(desc);

    desc26_set_metadata_application_format(desc, 0xfff0);
    desc26_set_metadata_format(desc, 0xf0);
    desc26_set_metadata_service_id(desc, 0x88);
    desc26_set_dsm_cc_flag(desc, false);
    desc26_set_decoder_config_flags(desc, 0);

    desc26_set_length(desc);
}

static void build_desc26_2(uint8_t *desc) {
    desc26_init(desc);

    desc26_set_metadata_application_format(desc, 0xffff);
    // Needs desc26_set_metadata_application_format(desc, 0xffff);
    desc26_set_metadata_application_format_identifier(desc, 0x00112233);

    desc26_set_metadata_format(desc, 0xff);
    // Needs desc26_set_metadata_format(desc, 0xff);
    desc26_set_metadata_format_identifier(desc, 0xdeadbeaf);

    desc26_set_metadata_service_id(desc, 0x88);

    desc26_set_dsm_cc_flag(desc, true);
    // Needs desc26_set_dsm_cc_flag(desc, true);
    desc26_set_service_identification_record(desc, 3, (uint8_t *)"abc");

    desc26_set_decoder_config_flags(desc, 3);
    // Needs desc26_set_decoder_config_flags(desc, 1);
    desc26_set_decoder_config(desc, 3, (uint8_t *)"123");

    // Needs desc26_set_decoder_config_flags(desc, 3);
    desc26_set_dec_config_identification_record(desc, 3, (uint8_t *)"ABC");

    // Needs desc26_set_mpeg_carriage_flags(desc, 4);
    desc26_set_decoder_config_metadata_service_id(desc, 100);

    desc26_set_length(desc);
}

/* MPEG Descriptor 0x27: Metadata STD descriptor */
static void build_desc27(uint8_t *desc) {
    desc27_init(desc);
    desc27_set_input_leak_rate(desc, 12345);
    desc27_set_buffer_size(desc, 23456);
    desc27_set_output_leak_rate(desc, 34567);
}

/* MPEG Descriptor 0x28: AVC video descriptor */
static void build_desc28(uint8_t *desc) {
    desc28_init(desc);
    desc28_set_profile_idc(desc, 0x12);
    desc28_set_constraint_set0_flag(desc, true);
    desc28_set_constraint_set1_flag(desc, true);
    desc28_set_constraint_set2_flag(desc, false);
    desc28_set_avc_compatible_flags(desc, 0x0a);
    desc28_set_level_idc(desc, 0x34);
    desc28_set_avc_still_present(desc, false);
    desc28_set_avc_24_hour_picture_flag(desc, false);
}

/* MPEG Descriptor 0x2a: AVC timing and HRD descriptor */
static void build_desc2a(uint8_t *desc) {
    desc2a_init(desc);
    desc2a_set_hrd_management_valid_flag(desc, false);
    desc2a_set_picture_and_timing_info_present(desc, true);
    desc2a_set_90khz_flag(desc, false); // depends on picture_and_timing_info
    desc2a_set_N(desc, 12345678); // depends on 90khz == false
    desc2a_set_K(desc, 34567890); // depends on 90khz == false
    desc2a_set_num_units_in_tick(desc, 456789); // depends on picture_and_timing_info
    desc2a_set_fixed_frame_rate_flag(desc, true);
    desc2a_set_temporal_poc_flag(desc, false);
    desc2a_set_picture_to_display_conversion_flag(desc, true);
}

/* MPEG Descriptor 0x2b: MPEG-2 AAC audio descriptor */
static void build_desc2b(uint8_t *desc) {
    desc2b_init(desc);
    desc2b_set_aac_profile(desc, 0x12);
    desc2b_set_aac_channel_config(desc, 0x05);
    desc2b_set_aac_additional_info(desc, 0x00);
}

/* MPEG Descriptor 0x2c: FlexMuxTiming descriptor */
static void build_desc2c(uint8_t *desc) {
    desc2c_init(desc);
    desc2c_set_fcr_es_id(desc, 0x1234);
    desc2c_set_fcr_resolution(desc, 123456789);
    desc2c_set_fcr_length(desc, 55);
    desc2c_set_fmx_rate_length(desc, 32);
}

/* =========================================================================
 * DVB defined descriptors
 * ========================================================================= */

/* DVB  Descriptor 0x40: Network name descriptor */
static void build_desc40(uint8_t *desc) {
    char *network_name = "Test Network Name";
    desc40_init(desc);
    desc40_set_networkname(desc, (uint8_t *)network_name, strlen(network_name));
}

/* DVB  Descriptor 0x41: Service list descriptor */
static void build_desc41(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *service_n;

    desc41_init(desc);
    desc_set_length(desc, 255);

    service_n = desc41_get_service(desc, k++);
    desc41n_set_sid(service_n, sid);
    desc41n_set_type(service_n, 1);

    service_n = desc41_get_service(desc, k++);
    desc41n_set_sid(service_n, sid + 100);
    desc41n_set_type(service_n, 2);

    service_n = desc41_get_service(desc, k++);
    desc41n_set_sid(service_n, sid + 200);
    desc41n_set_type(service_n, 1);

    service_n = desc41_get_service(desc, k++);
    desc41n_set_sid(service_n, sid + 300);
    desc41n_set_type(service_n, 2);

    service_n = desc41_get_service(desc, k);
    desc_set_length(desc, service_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x43: Satellite delivery system descriptor */
static void build_desc43(uint8_t *desc) {
    desc43_init(desc);
    desc43_set_frequency_bcd  (desc, 0x1175725); // 11,75725 GHz
    desc43_set_position_bcd   (desc, 0x0192); // 19.2
    desc43_set_east           (desc, true);
    desc43_set_polarization   (desc, 1); // 0-H, 1-V, 2-L, 3-R
    desc43_set_rolloff        (desc, 0); // Must be 0 if dvbs2 is false
    desc43_set_dvbs2          (desc, false);
    desc43_set_modulation     (desc, 1); // QPSK
//    desc43_set_rolloff        (desc, 1); // 0,25
//    desc43_set_dvbs2          (desc, true);
//    desc43_set_modulation     (desc, 2); // 8PSK
    desc43_set_symbolrate_bcd (desc, 0x274500); // 27,450
    desc43_set_fecinner       (desc, 5); // 7/8
}

/* DVB  Descriptor 0x44: Cable delivery system descriptor */
static void build_desc44(uint8_t *desc) {
    desc44_init(desc);
    desc44_set_frequency_bcd  (desc, 0x3120000); // 312,0000 Mhz
    desc44_set_fecouter       (desc, 0); // 0 - not defined
    desc44_set_modulation     (desc, 3); // 64-QAM
    desc44_set_symbolrate_bcd (desc, 0x274500); // 27,450
    desc44_set_fecinner       (desc, 6); // 8/9
}

/* DVB  Descriptor 0x45: VBI data descriptor */
static void build_desc45(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *data_n;

    desc45_init(desc);
    desc_set_length(desc, 255);

    data_n = desc45_get_data(desc, k++);
    desc45n_set_service_id (data_n, 0x01);   // EBU teletext
    desc45n_set_data_length(data_n, 8);      // How much bytes are after this
    {
        uint8_t j, max_j = desc45n_get_data_length(data_n);
        for (j = 0; j < max_j; j++) {
            desc45n_set_field_parity(data_n, j, (j % 2) == 0);
            desc45n_set_line_offset (data_n, j, j);
        }
    }

    data_n = desc45_get_data(desc, k++);
    desc45n_set_service_id (data_n, 0x03);   // Reserved
    desc45n_set_data_length(data_n, 4);      // How much bytes are after this
    desc45n_set_byte(data_n, 0, 0x12);
    desc45n_set_byte(data_n, 1, 0x34);
    desc45n_set_byte(data_n, 2, 0x56);
    desc45n_set_byte(data_n, 3, 0x78);

    data_n = desc45_get_data(desc, k++);
    desc45n_set_service_id (data_n, 0x04);   // VPS
    desc45n_set_data_length(data_n, 1);      // How much bytes are after this
    {
        uint8_t j, max_j = desc45n_get_data_length(data_n);
        for (j = 0; j < max_j; j++) {
            desc45n_set_field_parity(data_n, j, (j % 2) == 2);
            desc45n_set_line_offset (data_n, j, j + 10);
        }
    }

    data_n = desc45_get_data(desc, k++);
    desc45n_set_service_id (data_n, 0x05);   // WSS
    desc45n_set_data_length(data_n, 4);      // How much bytes are after this
    {
        uint8_t j, max_j = desc45n_get_data_length(data_n);
        for (j = 0; j < max_j; j++) {
            desc45n_set_field_parity(data_n, j, (j % 2) == 1);
            desc45n_set_line_offset (data_n, j, j + 20);
        }
    }

    data_n = desc45_get_data(desc, k++);
    desc45n_set_service_id (data_n, 0x06);   // Closed Captioning
    desc45n_set_data_length(data_n, 0);      // How much bytes are after this

    data_n = desc45_get_data(desc, k);
    desc_set_length(desc, data_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x46: VBI teletext descriptor */
static void build_desc46(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *lang_n;

    desc46_init(desc);
    desc_set_length(desc, 255);

    lang_n = desc46_get_language(desc, k++);
    desc46n_set_code(lang_n, (uint8_t *)"eng");
    desc46n_set_teletexttype(lang_n, 1);
    desc46n_set_teletextmagazine(lang_n, 3);
    desc46n_set_teletextpage(lang_n, 255);

    lang_n = desc46_get_language(desc, k++);
    desc46n_set_code(lang_n, (uint8_t *)"bul");
    desc46n_set_teletexttype(lang_n, 2);
    desc46n_set_teletextmagazine(lang_n, 2);
    desc46n_set_teletextpage(lang_n, 127);

    lang_n = desc46_get_language(desc, k++);
    desc46n_set_code(lang_n, (uint8_t *)"fre");
    desc46n_set_teletexttype(lang_n, 3);
    desc46n_set_teletextmagazine(lang_n, 1);
    desc46n_set_teletextpage(lang_n, 64);

    lang_n = desc46_get_language(desc, k);
    desc_set_length(desc, lang_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x47: Bouquet name descriptor */
static void build_desc47(uint8_t *desc) {
    char *bouquet_name = "Test Bouquet Name";
    desc47_init(desc);
    desc47_set_bouquetname(desc, (uint8_t *)bouquet_name, strlen(bouquet_name));
}

/* DVB  Descriptor 0x48: Service descriptor */
static void build_desc48(uint8_t *desc) {
    uint8_t service_type = 0x01; // digital tv
    char *provider_name = "Test Provider Name";
    char *service_name = "Test Service Name";
    desc48_init(desc);
    desc48_set_type(desc, service_type);
    desc48_set_provider(desc, (uint8_t *)provider_name, strlen(provider_name));
    desc48_set_service(desc, (uint8_t *)service_name, strlen(service_name));
    desc48_set_length(desc);
}

/* DVB  Descriptor 0x49: Country availability descriptor */
static void build_desc49(uint8_t *desc, bool b_available) {
    uint8_t k = 0;
    uint8_t *code_n;

    desc49_init(desc);
    desc49_set_country_availability_flag(desc, b_available);

    desc_set_length(desc, 255);

    code_n = desc49_get_code(desc, k++);
    desc49n_set_code(code_n, (uint8_t *)"GBR");

    code_n = desc49_get_code(desc, k++);
    desc49n_set_code(code_n, (uint8_t *)"FRA");

    code_n = desc49_get_code(desc, k++);
    desc49n_set_code(code_n, (uint8_t *)"BUL");

    code_n = desc49_get_code(desc, k);
    desc_set_length(desc, code_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x4a: Linkage descriptor */
static void build_desc4a_std(uint8_t *desc) {
    desc4a_init(desc);
    desc4a_set_tsid(desc, tsid + 500);
    desc4a_set_onid(desc, onid + 500);
    desc4a_set_sid (desc, 0x0000);
    desc4a_set_linkage(desc, 0x04); // new ts
}

static void build_desc4a_mobile(uint8_t *desc) {
    desc4a_init(desc);
    desc4a_set_tsid(desc, tsid + 600);
    desc4a_set_onid(desc, onid + 600);
    desc4a_set_sid (desc, sid + 600);
    // mobile handover
    desc4a_set_linkage(desc, DESC4A_LINKAGE_MOBILE);
    desc4a_set_mobile_handover_type(desc, 1);
    desc4a_set_mobile_origin_type(desc, false);
    desc4a_set_mobile_nid(desc, onid + 1000);
    desc4a_set_mobile_initial_sid(desc, sid + 1000);
    desc4a_set_length(desc);
}

static void build_desc4a_event(uint8_t *desc) {
    desc4a_init(desc);
    desc4a_set_tsid(desc, tsid + 700);
    desc4a_set_onid(desc, onid + 700);
    desc4a_set_sid (desc, sid + 700);
    // event linkage
    desc4a_set_linkage(desc, DESC4A_LINKAGE_EVENT);
    desc4a_set_event_target_event_id(desc, event_id + 1000);
    desc4a_set_event_target_listed(desc, true);
    desc4a_set_event_simulcast(desc, true);
    desc4a_set_length(desc);
}

static void build_desc4a_extended_event(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *ext_n;

    desc4a_init(desc);
    desc4a_set_tsid(desc, tsid + 800);
    desc4a_set_onid(desc, onid + 800);
    desc4a_set_sid (desc, sid + 800);
    // extended event linkage
    desc4a_set_linkage(desc, DESC4A_LINKAGE_EXT_EVENT);

    desc_set_length(desc, 255);

    ext_n = desc4a_get_ext_event(desc, k++);
    desc4an_set_ext_event_target_event_id(ext_n, event_id + 1000);
    desc4an_set_ext_event_target_listed  (ext_n, false);
    desc4an_set_ext_event_simulcast      (ext_n, true);
    desc4an_set_ext_event_link_type      (ext_n, 1); // HD
    desc4an_set_ext_event_target_id_type (ext_n, 3); // user defined id
    desc4an_set_ext_event_onid_id_flag   (ext_n, true);
    desc4an_set_ext_event_service_id_flag(ext_n, true);
    desc4an_set_ext_event_user_defined_id(ext_n, 7878);
    desc4an_set_ext_event_target_tsid    (ext_n, tsid + 1000);  // !!!
    desc4an_set_ext_event_target_onid    (ext_n, onid + 1000);  // !!!
    desc4an_set_ext_event_service_id     (ext_n, sid + 1000);   // !!!

    ext_n = desc4a_get_ext_event(desc, k++);
    desc4an_set_ext_event_target_event_id(ext_n, event_id + 2000);
    desc4an_set_ext_event_target_listed  (ext_n, true);
    desc4an_set_ext_event_simulcast      (ext_n, true);
    desc4an_set_ext_event_link_type      (ext_n, 0); // SD
    desc4an_set_ext_event_target_id_type (ext_n, 0); // onid_id_flag, target_service_id_flag
    desc4an_set_ext_event_onid_id_flag   (ext_n, true);
    desc4an_set_ext_event_service_id_flag(ext_n, true);
    desc4an_set_ext_event_user_defined_id(ext_n, 8787);         // !!!
    desc4an_set_ext_event_target_tsid    (ext_n, tsid + 2000);  // !!!
    desc4an_set_ext_event_target_onid    (ext_n, onid + 2000);
    desc4an_set_ext_event_service_id     (ext_n, sid + 2000);

    ext_n = desc4a_get_ext_event(desc, k++);
    desc4an_set_ext_event_target_event_id(ext_n, event_id + 3000);
    desc4an_set_ext_event_target_listed  (ext_n, true);
    desc4an_set_ext_event_simulcast      (ext_n, true);
    desc4an_set_ext_event_link_type      (ext_n, 2); // 3D
    desc4an_set_ext_event_target_id_type (ext_n, 1); // target_tsid, onid_id_flag, target_service_id_flag
    desc4an_set_ext_event_onid_id_flag   (ext_n, true);
    desc4an_set_ext_event_service_id_flag(ext_n, true);
    desc4an_set_ext_event_user_defined_id(ext_n, 8787);         // !!!
    desc4an_set_ext_event_target_tsid    (ext_n, tsid + 3000);
    desc4an_set_ext_event_target_onid    (ext_n, onid + 3000);
    desc4an_set_ext_event_service_id     (ext_n, sid + 3000);

    ext_n = desc4a_get_ext_event(desc, k++);
    desc4an_set_ext_event_target_event_id(ext_n, event_id + 4000);
    desc4an_set_ext_event_target_listed  (ext_n, false);
    desc4an_set_ext_event_simulcast      (ext_n, false);
    desc4an_set_ext_event_link_type      (ext_n, 0); // SD
    desc4an_set_ext_event_target_id_type (ext_n, 0); // onid_id_flag, target_service_id_flag
    desc4an_set_ext_event_onid_id_flag   (ext_n, false);
    desc4an_set_ext_event_service_id_flag(ext_n, false);
    desc4an_set_ext_event_user_defined_id(ext_n, 8787);         // !!!
    desc4an_set_ext_event_target_tsid    (ext_n, tsid + 4000);  // !!!
    desc4an_set_ext_event_target_onid    (ext_n, onid + 4000);  // !!!
    desc4an_set_ext_event_service_id     (ext_n, sid + 4000);   // !!!

    ext_n = desc4a_get_ext_event(desc, k);
    desc_set_length(desc, ext_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x4b: NVOD_reference_descriptor */
static void build_desc4b(uint8_t *desc, bool b_available) {
    uint8_t k = 0;
    uint8_t *ref_n;

    desc4b_init(desc);
    desc_set_length(desc, 255);

    ref_n = desc4b_get_reference(desc, k++);
    desc4bn_set_tsid(ref_n, tsid);
    desc4bn_set_onid(ref_n, onid);
    desc4bn_set_sid (ref_n, sid);

    ref_n = desc4b_get_reference(desc, k++);
    desc4bn_set_tsid(ref_n, tsid + 100);
    desc4bn_set_onid(ref_n, onid + 100);
    desc4bn_set_sid (ref_n, sid  + 100);

    ref_n = desc4b_get_reference(desc, k++);
    desc4bn_set_tsid(ref_n, tsid + 200);
    desc4bn_set_onid(ref_n, onid + 200);
    desc4bn_set_sid (ref_n, sid  + 200);

    ref_n = desc4b_get_reference(desc, k);
    desc_set_length(desc, ref_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x4c: time_shifted_service_descriptor */
static void build_desc4c(uint8_t *desc) {
    desc4c_init(desc);
    desc4c_set_reference_sid(desc, sid + 1000);
}

/* DVB  Descriptor 0x4d: Short event descriptor */
static void build_desc4d(uint8_t *desc) {
    char *event_name = "Major TV event";
    char *text = "The event of the century!";
    desc4d_init(desc);
    desc4d_set_lang(desc, (uint8_t *)"eng");
    desc4d_set_event_name(desc, (uint8_t *)event_name, strlen(event_name));
    desc4d_set_text(desc, (uint8_t *)text, strlen(text));
    desc4d_set_length(desc);
}

/* DVB  Descriptor 0x4e: Extended event descriptor */
static void build_desc4e(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *item_n;
    char *item1_desc = "Director";
    char *item1_text = "Famous director";
    char *item2_desc = "Year";
    char *item2_text = "2011";
    char *item3_desc = "Rating";
    char *item3_text = "***++";
    char *text = "Wow, what an event!";

    desc4e_init(desc);
    desc_set_length(desc, 255);

    desc4e_set_desc_number(desc, 0);
    desc4e_set_last_desc_number(desc, 0);
    desc4e_set_lang(desc, (uint8_t *)"eng");
    desc4e_set_items_length(desc, 0);

    {
        desc4e_set_items_length(desc, 255);
        uint8_t *first_item = desc4e_get_item(desc, 0);

        item_n = desc4e_get_item(desc, k++);
        desc4en_set_item_description(item_n, (uint8_t *)item1_desc, strlen(item1_desc));
        desc4en_set_item_text(item_n, (uint8_t *)item1_text, strlen(item1_text));

        item_n = desc4e_get_item(desc, k++);
        desc4en_set_item_description(item_n, (uint8_t *)item2_desc, strlen(item2_desc));
        desc4en_set_item_text(item_n, (uint8_t *)item2_text, strlen(item2_text));

        item_n = desc4e_get_item(desc, k++);
        desc4en_set_item_description(item_n, (uint8_t *)item3_desc, strlen(item3_desc));
        desc4en_set_item_text(item_n, (uint8_t *)item3_text, strlen(item3_text));

        item_n = desc4e_get_item(desc, k);
        desc4e_set_items_length(desc, item_n - first_item);
    }

    desc4e_set_text(desc, (uint8_t *)text, strlen(text));
    desc4e_set_length(desc);
}

/* DVB  Descriptor 0x4f: time_shifted_event_descriptor */
static void build_desc4f(uint8_t *desc) {
    desc4f_init(desc);
    desc4f_set_reference_sid(desc, sid + 2000);
    desc4f_set_reference_event_id(desc, event_id + 2000);
}

/* DVB  Descriptor 0x50: Component descriptor */
static void build_desc50(uint8_t *desc) {
    char *text = "Stereo";
    desc50_init(desc);
    desc50_set_stream_content(desc, 0x02);
    desc50_set_component_type(desc, 0x03);
    desc50_set_component_tag(desc, 46);
    desc50_set_language(desc, (uint8_t *)"eng");
    desc50_set_text(desc, (uint8_t *)text, strlen(text)); // Not required
}

/* DVB  Descriptor 0x51: Mosaic descriptor */
static void build_desc51(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *cell_n;

    desc51_init(desc);
    desc51_set_mosaic_entry_point(desc, 1);
    desc51_set_horizontal_cells(desc, 2); // +1 == 3
    desc51_set_vertical_cells(desc, 2);   // +1 == 3 (3x3 == 9 cells)

    desc_set_length(desc, 255);
    cell_n = desc51_get_logical_cell(desc, k++);
    {
        desc51n_set_logical_cell_id(cell_n, 0);
        desc51n_set_logical_cell_presentation_info(cell_n, 1);
        desc51n_set_elementary_cell_field_length(cell_n, 3);
        {
            desc51n_set_elementary_cell_id(cell_n, 0, 0);
            desc51n_set_elementary_cell_id(cell_n, 1, 1);
            desc51n_set_elementary_cell_id(cell_n, 2, 2);
        }

        desc51n_set_cell_linkage_info(cell_n, 1); // bouquet_id
        desc51n_set_bouquet_id(cell_n, onid + 100);
    }

    cell_n = desc51_get_logical_cell(desc, k++);
    {
        desc51n_set_logical_cell_id(cell_n, 1);
        desc51n_set_logical_cell_presentation_info(cell_n, 2);
        desc51n_set_elementary_cell_field_length(cell_n, 3);
        {
            desc51n_set_elementary_cell_id(cell_n, 0, 3);
            desc51n_set_elementary_cell_id(cell_n, 1, 4);
            desc51n_set_elementary_cell_id(cell_n, 2, 5);
        }
        desc51n_set_cell_linkage_info(cell_n, 2); // 2 or 3, onid, tsid, sid
        desc51n_set_onid(cell_n, onid + 500);
        desc51n_set_tsid(cell_n, tsid + 500);
        desc51n_set_sid(cell_n, sid + 500);
    }

    cell_n = desc51_get_logical_cell(desc, k++);
    {
        desc51n_set_logical_cell_id(cell_n, 2);
        desc51n_set_logical_cell_presentation_info(cell_n, 3);
        desc51n_set_elementary_cell_field_length(cell_n, 3);
        {
            desc51n_set_elementary_cell_id(cell_n, 0, 6);
            desc51n_set_elementary_cell_id(cell_n, 1, 7);
            desc51n_set_elementary_cell_id(cell_n, 2, 8);
        }
        desc51n_set_cell_linkage_info(cell_n, 4); // onid, tsid, sid, event_id
        desc51n_set_onid(cell_n, onid + 1000);
        desc51n_set_tsid(cell_n, tsid + 1000);
        desc51n_set_sid(cell_n, sid + 1000);
        desc51n_set_event_id(cell_n, event_id + 1000);
    }

    cell_n = desc51_get_logical_cell(desc, k);
    desc_set_length(desc, cell_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x52: Stream identifier descriptor */
static void build_desc52(uint8_t *desc) {
    desc52_init(desc);
    desc52_set_component_tag(desc, 46);
}

/* DVB  Descriptor 0x53: CA_identifier_descriptor */
static void build_desc53(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *cas_n;

    desc53_init(desc);
    desc_set_length(desc, 255);

    cas_n = desc53_get_ca(desc, k++);
    desc53n_set_ca_sysid(cas_n, 0xaabb);

    cas_n = desc53_get_ca(desc, k++);
    desc53n_set_ca_sysid(cas_n, 0xccdd);

    cas_n = desc53_get_ca(desc, k++);
    desc53n_set_ca_sysid(cas_n, 0xeeff);

    cas_n = desc53_get_ca(desc, k);
    desc_set_length(desc, cas_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x54: Content descriptor */
static void build_desc54(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *content_n;

    desc54_init(desc);
    desc_set_length(desc, 255);

    content_n = desc54_get_content(desc, k++);
    desc54n_set_content_l1(content_n, 2);
    desc54n_set_content_l2(content_n, 4);
    desc54n_set_user(content_n, 78);

    content_n = desc54_get_content(desc, k++);
    desc54n_set_content_l1(content_n, 6);
    desc54n_set_content_l2(content_n, 8);
    desc54n_set_user(content_n, 177);

    content_n = desc54_get_content(desc, k);
    desc_set_length(desc, content_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x55: Parental rating descriptor */
static void build_desc55(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *rating_n;

    desc55_init(desc);
    desc_set_length(desc, 255);

    rating_n = desc55_get_rating(desc, k++);
    desc55n_set_country_code(rating_n, (uint8_t *)"USA");
    desc55n_set_rating(rating_n, 0);

    rating_n = desc55_get_rating(desc, k++);
    desc55n_set_country_code(rating_n, (uint8_t *)"CHI");
    desc55n_set_rating(rating_n, 15);

    rating_n = desc55_get_rating(desc, k++);
    desc55n_set_country_code(rating_n, (uint8_t *)"FRA");
    desc55n_set_rating(rating_n, 12);

    rating_n = desc55_get_rating(desc, k++);
    desc55n_set_country_code(rating_n, (uint8_t *)"BUL");
    desc55n_set_rating(rating_n, 24);

    rating_n = desc55_get_rating(desc, k);
    desc_set_length(desc, rating_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x56: Teletext descriptor */
static void build_desc56(uint8_t *desc) {
    // Desc 46 is the same as desc 56, only
    // the descriptor tag is different
    build_desc46(desc);
    desc56_init(desc);
}

/* DVB  Descriptor 0x57: telephone_descriptor */
static void build_desc57(uint8_t *desc) {
    char *country_prefix = "+";
    char *international_area_code = "359";
    char *operator_code = "2";
    char *national_area_code = "";
    char *core_number = "9868620";

    desc57_init(desc);

    desc57_set_foreign_availability(desc, true);
    desc57_set_connection_type(desc, 3);

    desc57_set_country_prefix(desc, country_prefix, strlen(country_prefix));
    desc57_set_international_area_code(desc, international_area_code, strlen(international_area_code));
    desc57_set_operator_code(desc, operator_code, strlen(operator_code));
    desc57_set_national_area_code(desc, national_area_code, strlen(national_area_code));
    desc57_set_core_number(desc, core_number, strlen(core_number));
    desc57_set_length(desc);
}

/* DVB  Descriptor 0x58: Local time offset descriptor */
static void build_desc58(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *lto_n;

    desc58_init(desc);
    desc_set_length(desc, 255);

    lto_n = desc58_get_lto(desc, k++);
    desc58n_set_country_code(lto_n, (uint8_t *)"BUL");
    desc58n_set_country_region_id(lto_n, 2);
    desc58n_set_lto_polarity(lto_n, 1);
    desc58n_set_lt_offset(lto_n, 0x0200);
    desc58n_set_time_of_change(lto_n, dvb_time_encode_UTC(ts_0));
    desc58n_set_next_offset(lto_n, 0x0300);

    lto_n = desc58_get_lto(desc, k++);
    desc58n_set_country_code(lto_n, (uint8_t *)"USA");
    desc58n_set_country_region_id(lto_n, 9);
    desc58n_set_lto_polarity(lto_n, 0);
    desc58n_set_lt_offset(lto_n, 0x1400);
    desc58n_set_time_of_change(lto_n, dvb_time_encode_UTC(ts_2));
    desc58n_set_next_offset(lto_n, 0x1830);

    lto_n = desc58_get_lto(desc, k);
    desc_set_length(desc, lto_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x59: Subtitling descriptor */
static void build_desc59(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *lang_n;

    desc59_init(desc);
    desc_set_length(desc, 255);

    lang_n = desc59_get_language(desc, k++);
    desc59n_set_code(lang_n, (uint8_t *)"eng");
    desc59n_set_subtitlingtype(lang_n, 1);
    desc59n_set_compositionpage(lang_n, 2);
    desc59n_set_ancillarypage(lang_n, 3);

    lang_n = desc59_get_language(desc, k++);
    desc59n_set_code(lang_n, (uint8_t *)"bul");
    desc59n_set_subtitlingtype(lang_n, 2);
    desc59n_set_compositionpage(lang_n, 3);
    desc59n_set_ancillarypage(lang_n, 4);

    lang_n = desc59_get_language(desc, k++);
    desc59n_set_code(lang_n, (uint8_t *)"fre");
    desc59n_set_subtitlingtype(lang_n, 5);
    desc59n_set_compositionpage(lang_n, 6);
    desc59n_set_ancillarypage(lang_n, 7);

    lang_n = desc59_get_language(desc, k);
    desc_set_length(desc, lang_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x5a: Terrestrial delivery system descriptor */
static void build_desc5a(uint8_t *desc) {
    desc5a_init(desc);
    desc5a_set_frequency      (desc, 1234567890); // Hz * 10
    desc5a_set_bandwidth      (desc, 2); // 6 Mhz
    desc5a_set_priority       (desc, false); // HP, hierarchy must 0
    desc5a_set_timeslicing    (desc, true);
    desc5a_set_mpefec         (desc, true);
    desc5a_set_constellation  (desc, 2); // 64-QAM
    desc5a_set_hierarchy      (desc, 0xe);
    desc5a_set_coderatehp     (desc, 2); // 3/4
    desc5a_set_coderatelp     (desc, 3); // 5/6
    desc5a_set_guard          (desc, 1); // 1/16
    desc5a_set_transmission   (desc, 2); // 4k mode
    desc5a_set_otherfrequency (desc, true);
}

/* DVB  Descriptor 0x5b: multilingual_network_name_descriptor */
static void build_desc5b(uint8_t *desc) {
    char *network_name = "M Network";
    uint8_t k = 0;
    uint8_t *data_n;

    desc5b_init(desc);
    desc_set_length(desc, 255);

    data_n = desc5b_get_data(desc, k++);
    desc5bn_set_code(data_n, (uint8_t *)"eng");
    desc5bn_set_networkname(data_n, (uint8_t *)network_name, strlen(network_name));

    data_n = desc5b_get_data(desc, k++);
    desc5bn_set_code(data_n, (uint8_t *)"fre");
    desc5bn_set_networkname(data_n, (uint8_t *)network_name, strlen(network_name));

    data_n = desc5b_get_data(desc, k++);
    desc5bn_set_code(data_n, (uint8_t *)"bul");
    desc5bn_set_networkname(data_n, (uint8_t *)network_name, strlen(network_name));

    data_n = desc5b_get_data(desc, k);
    desc_set_length(desc, data_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x5c: Multilingual bouquet name descriptor */
static void build_desc5c(uint8_t *desc) {
    char *bouquet_name = "M Bouquet";
    uint8_t k = 0;
    uint8_t *data_n;

    desc5c_init(desc);
    desc_set_length(desc, 255);

    data_n = desc5c_get_data(desc, k++);
    desc5cn_set_code(data_n, (uint8_t *)"eng");
    desc5cn_set_bouquetname(data_n, (uint8_t *)bouquet_name, strlen(bouquet_name));

    data_n = desc5c_get_data(desc, k++);
    desc5cn_set_code(data_n, (uint8_t *)"fre");
    desc5cn_set_bouquetname(data_n, (uint8_t *)bouquet_name, strlen(bouquet_name));

    data_n = desc5c_get_data(desc, k++);
    desc5cn_set_code(data_n, (uint8_t *)"bul");
    desc5cn_set_bouquetname(data_n, (uint8_t *)bouquet_name, strlen(bouquet_name));

    data_n = desc5c_get_data(desc, k);
    desc_set_length(desc, data_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x5d: Multilingual service name descriptor */
static void build_desc5d(uint8_t *desc) {
    char *provider_name = "M Provider";
    char *service_name = "M Service";
    uint8_t k = 0;
    uint8_t *data_n;

    desc5d_init(desc);
    desc_set_length(desc, 255);

    data_n = desc5d_get_data(desc, k++);
    desc5dn_set_code(data_n, (uint8_t *)"eng");
    desc5dn_set_provider_name(data_n, (uint8_t *)provider_name, strlen(provider_name));
    desc5dn_set_service_name(data_n, (uint8_t *)service_name, strlen(service_name));

    data_n = desc5d_get_data(desc, k++);
    desc5dn_set_code(data_n, (uint8_t *)"fre");
    desc5dn_set_provider_name(data_n, (uint8_t *)provider_name, strlen(provider_name));
    desc5dn_set_service_name(data_n, (uint8_t *)service_name, strlen(service_name));

    data_n = desc5d_get_data(desc, k++);
    desc5dn_set_code(data_n, (uint8_t *)"bul");
    desc5dn_set_provider_name(data_n, (uint8_t *)provider_name, strlen(provider_name));
    desc5dn_set_service_name(data_n, (uint8_t *)service_name, strlen(service_name));


    data_n = desc5d_get_data(desc, k);
    desc_set_length(desc, data_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x5e: Multilingual component descriptor */
static void build_desc5e(uint8_t *desc) {
    char *text = "Stereo";
    uint8_t k = 0;
    uint8_t *data_n;

    desc5e_init(desc);
    desc5e_set_component_tag(desc, 46);
    desc_set_length(desc, 255);

    data_n = desc5e_get_data(desc, k++);
    desc5en_set_code(data_n, (uint8_t *)"eng");
    desc5en_set_text(data_n, (uint8_t *)text, strlen(text));

    data_n = desc5e_get_data(desc, k++);
    desc5en_set_code(data_n, (uint8_t *)"fre");
    desc5en_set_text(data_n, (uint8_t *)text, strlen(text));

    data_n = desc5e_get_data(desc, k++);
    desc5en_set_code(data_n, (uint8_t *)"bul");
    desc5en_set_text(data_n, (uint8_t *)text, strlen(text));

    data_n = desc5e_get_data(desc, k);
    desc_set_length(desc, data_n - desc - DESC_HEADER_SIZE);
}


/* DVB  Descriptor 0x5f: Private data specifier descriptor */
static void build_desc5f(uint8_t *desc) {
    desc5f_init(desc);
    desc5f_set_specifier(desc, 0xaabbccdd);
}

/* DVB  Descriptor 0x60: Service move descriptor */
static void build_desc60(uint8_t *desc) {
    desc60_init(desc);
    desc60_set_new_onid(desc, 10000);
    desc60_set_new_tsid(desc, 15000);
    desc60_set_new_service_id(desc, 20000);
}

/* DVB  Descriptor 0x61: Short smoothing buffer descriptor */
static void build_desc61(uint8_t *desc) {
    desc61_init(desc);
    desc61_set_sb_size(desc, 1); // 1536 bytes buffer size
    desc61_set_sb_leak_rate(desc, 10); // 0.5 Mbit/s
}

/* DVB  Descriptor 0x62: Frequency list descriptor */
static void build_desc62(uint8_t *desc) {
    uint8_t k = 0;
    uint8_t *freq_n;

    desc62_init(desc);
    desc62_set_coding_type(desc, 2); // Cable
    desc_set_length(desc, 255);

    freq_n = desc62_get_frequency(desc, k++);
    desc62n_set_freq(freq_n, 0x3120000); // 312,0000 Mhz

    freq_n = desc62_get_frequency(desc, k++);
    desc62n_set_freq(freq_n, 0x3180000); // 318,0000 Mhz

    freq_n = desc62_get_frequency(desc, k++);
    desc62n_set_freq(freq_n, 0x3240000); // 324,0000 Mhz

    freq_n = desc62_get_frequency(desc, k++);
    desc62n_set_freq(freq_n, 0x3300000); // 330,0000 Mhz

    freq_n = desc62_get_frequency(desc, k++);
    desc62n_set_freq(freq_n, 0x3360000); // 336,0000 Mhz

    freq_n = desc62_get_frequency(desc, k);
    desc_set_length(desc, freq_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x63: Partial transport stream descriptor */
static void build_desc63(uint8_t *desc) {
    desc63_init(desc);
    desc63_set_peak_rate(desc, 5000);
    desc63_set_min_overall_smoothing_rate(desc, 7000);
    desc63_set_max_overall_smoothing_buffer(desc, 1000);
}

/* DVB  Descriptor 0x64: Data broadcast descriptor */
static void build_desc64(uint8_t *desc) {
    char *selector_byte = "BY\"TE";
    char *text = "Some text";
    desc64_init(desc);
    desc64_set_broadcast_id(desc, 0x1122);
    desc64_set_component_tag(desc, 78);
    desc64_set_selector_byte(desc, (uint8_t *)selector_byte, strlen(selector_byte));
    desc64_set_lang(desc, (uint8_t *)"eng");
    desc64_set_text(desc, (uint8_t *)text, strlen(text));
    desc64_set_length(desc);
}

/* DVB  Descriptor 0x65: Scrambling descriptor */
static void build_desc65(uint8_t *desc) {
    desc65_init(desc);
    desc65_set_scrambling_mode(desc, 0x01);
}

/* DVB  Descriptor 0x66: Data broadcast id descriptor */
static void build_desc66(uint8_t *desc) {
    char *selector_byte = "UNIX";
    desc66_init(desc);
    desc66_set_broadcast_id(desc, 0x1122);
    desc66_set_selector_byte(desc, (uint8_t *)selector_byte, strlen(selector_byte));
}

/* DVB  Descriptor 0x67: Transport stream descriptor */
static void build_desc67(uint8_t *desc, char *bytes) {
    desc67_init(desc);
    desc67_set_bytes(desc, (uint8_t *)bytes, strlen(bytes));
}

/* DVB  Descriptor 0x68: DSNG_descriptor */
static void build_desc68(uint8_t *desc) {
    char *dsng_bytes = "1234,SNG_Headquarter,SNG_Provider";
    desc68_init(desc);
    desc68_set_bytes(desc, (uint8_t *)dsng_bytes, strlen(dsng_bytes));
}

/* DVB  Descriptor 0x69: PDC_descriptor */
static void build_desc69(uint8_t *desc) {
    desc69_init(desc);
//    desc69_set_pil(desc, 0x5d805);
    desc69_set_day(desc, 11);
    desc69_set_month(desc, 11);
    desc69_set_hour(desc, 0);
    desc69_set_minute(desc, 5);
}

/* DVB  Descriptor 0x6a: AC-3 descriptor */
static void build_desc6a(uint8_t *desc) {
    desc6a_init(desc);

    desc6a_set_component_type_flag  (desc, true);
    desc6a_set_component_type       (desc, 10);

    desc6a_set_bsid_flag            (desc, true);
    desc6a_set_bsid                 (desc, 20);

    desc6a_set_mainid_flag          (desc, true);
    desc6a_set_mainid               (desc, 30);

    desc6a_set_asvc_flag            (desc, true);
    desc6a_set_asvc                 (desc, 40);

    desc6a_set_length(desc);
}

/* DVB  Descriptor 0x6b: Ancillary data descriptor */
static void build_desc6b(uint8_t *desc) {
    desc6b_init(desc);
    desc6b_set_dvd_video_ancillary_data_flag    (desc, true);
    desc6b_set_extended_ancillary_data_flag     (desc, false);
    desc6b_set_announcement_switching_data_flag (desc, true);
    desc6b_set_dab_ancillary_data_flag          (desc, false);
    desc6b_set_scale_factor_error_check_flag    (desc, true);
    desc6b_set_mpeg4_ancillary_data_flag        (desc, false);
    desc6b_set_rds_via_uecp_flag                (desc, true);
}

/* DVB  Descriptor 0x6c: Cell list descriptor */
static void build_desc6c(uint8_t *desc) {
    uint8_t n = 0, k;
    uint8_t *cell_n, *subcell_k;

    desc6c_init(desc);
    desc_set_length(desc, 255);

    cell_n = desc6c_get_cell(desc, n++);
    desc6cn_set_cell_id(cell_n, 1234);
    desc6cn_set_cell_latitude(cell_n, 4567);
    desc6cn_set_cell_longtitude(cell_n, 5678);
    desc6cn_set_cell_extend_of_latitude(cell_n, 123);
    desc6cn_set_cell_extend_of_longtitude(cell_n, 345);
    desc6cn_set_subcell_info_loop_length(cell_n, 0);

    cell_n = desc6c_get_cell(desc, n++);
    desc6cn_set_cell_id(cell_n, 4456);
    desc6cn_set_cell_latitude(cell_n, 5567);
    desc6cn_set_cell_longtitude(cell_n, 6678);
    desc6cn_set_cell_extend_of_latitude(cell_n, 1234);
    desc6cn_set_cell_extend_of_longtitude(cell_n, 1234);
    desc6cn_set_subcell_info_loop_length(cell_n, 0);
    {
        k = 0;
        desc6cn_set_subcell_info_loop_length(cell_n, 255);

        subcell_k = desc6cn_get_subcell(cell_n, k++);
        desc6ck_set_cell_id_extension(subcell_k, 0);
        desc6ck_set_subcell_latitude(subcell_k, 0x1122);
        desc6ck_set_subcell_longtitude(subcell_k, 0x3344);
        desc6ck_set_subcell_extend_of_latitude(subcell_k, 0xf567);
        desc6ck_set_subcell_extend_of_longtitude(subcell_k, 0xf89a);

        subcell_k = desc6cn_get_subcell(cell_n, k++);
        desc6ck_set_cell_id_extension(subcell_k, 1);
        desc6ck_set_subcell_latitude(subcell_k, 0x2233);
        desc6ck_set_subcell_longtitude(subcell_k, 0x4455);
        desc6ck_set_subcell_extend_of_latitude(subcell_k, 0xf678);
        desc6ck_set_subcell_extend_of_longtitude(subcell_k, 0xf9ab);

        subcell_k = desc6cn_get_subcell(cell_n, k++);
        desc6ck_set_cell_id_extension(subcell_k, 2);
        desc6ck_set_subcell_latitude(subcell_k, 0x2233);
        desc6ck_set_subcell_longtitude(subcell_k, 0x4455);
        desc6ck_set_subcell_extend_of_latitude(subcell_k, 0xf678);
        desc6ck_set_subcell_extend_of_longtitude(subcell_k, 0xf9ab);

        subcell_k = desc6cn_get_subcell(cell_n, k);
        desc6cn_set_subcell_info_loop_length(cell_n, subcell_k - cell_n - DESC6C_DATA_SIZE);
    }

    cell_n = desc6c_get_cell(desc, n++);
    desc6cn_set_cell_id(cell_n, 0xffff);
    desc6cn_set_cell_latitude(cell_n, 0xeeee);
    desc6cn_set_cell_longtitude(cell_n, 0xdddd);
    desc6cn_set_cell_extend_of_latitude(cell_n, 0xf789);
    desc6cn_set_cell_extend_of_longtitude(cell_n, 0xfabc);
    desc6cn_set_subcell_info_loop_length(cell_n, 0);
    {
        k = 0;
        desc6cn_set_subcell_info_loop_length(cell_n, 255);

        subcell_k = desc6cn_get_subcell(cell_n, k++);
        desc6ck_set_cell_id_extension(subcell_k, 0x00);
        desc6ck_set_subcell_latitude(subcell_k, 0x1122);
        desc6ck_set_subcell_longtitude(subcell_k, 0x3344);
        desc6ck_set_subcell_extend_of_latitude(subcell_k, 0xf567);
        desc6ck_set_subcell_extend_of_longtitude(subcell_k, 0xf89a);

        subcell_k = desc6cn_get_subcell(cell_n, k);
        desc6cn_set_subcell_info_loop_length(cell_n, subcell_k - cell_n - DESC6C_DATA_SIZE);
    }

    cell_n = desc6c_get_cell(desc, n);
    desc_set_length(desc, cell_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x6d: Cell frequency link descriptor */
static void build_desc6d(uint8_t *desc) {
    uint8_t n = 0, k;
    uint8_t *cell_n, *subcell_k;

    desc6d_init(desc);
    desc_set_length(desc, 255);

    cell_n = desc6d_get_cell(desc, n++);
    desc6dn_set_cell_id(cell_n, 1234);
    desc6dn_set_frequency(cell_n, 4567);
    desc6dn_set_subcell_info_loop_length(cell_n, 0);

    cell_n = desc6d_get_cell(desc, n++);
    desc6dn_set_cell_id(cell_n, 4456);
    desc6dn_set_frequency(cell_n, 5567);
    desc6dn_set_subcell_info_loop_length(cell_n, 0);
    {
        k = 0;
        desc6dn_set_subcell_info_loop_length(cell_n, 255);

        subcell_k = desc6dn_get_subcell(cell_n, k++);
        desc6dk_set_cell_id_extension(subcell_k, 0);
        desc6dk_set_transponder_frequency(subcell_k, 1122);

        subcell_k = desc6dn_get_subcell(cell_n, k++);
        desc6dk_set_cell_id_extension(subcell_k, 1);
        desc6dk_set_transponder_frequency(subcell_k, 2233);

        subcell_k = desc6dn_get_subcell(cell_n, k++);
        desc6dk_set_cell_id_extension(subcell_k, 2);
        desc6dk_set_transponder_frequency(subcell_k, 3344);

        subcell_k = desc6dn_get_subcell(cell_n, k);
        desc6dn_set_subcell_info_loop_length(cell_n, subcell_k - cell_n - DESC6D_DATA_SIZE);
    }

    cell_n = desc6d_get_cell(desc, n++);
    desc6dn_set_cell_id(cell_n, 7890);
    desc6dn_set_frequency(cell_n, 56789);
    desc6dn_set_subcell_info_loop_length(cell_n, 0);
    {
        k = 0;
        desc6dn_set_subcell_info_loop_length(cell_n, 255);

        subcell_k = desc6dn_get_subcell(cell_n, k++);
        desc6dk_set_cell_id_extension(subcell_k, 0);
        desc6dk_set_transponder_frequency(subcell_k, 889911);

        subcell_k = desc6dn_get_subcell(cell_n, k);
        desc6dn_set_subcell_info_loop_length(cell_n, subcell_k - cell_n - DESC6D_DATA_SIZE);
    }

    cell_n = desc6d_get_cell(desc, n);
    desc_set_length(desc, cell_n - desc - DESC_HEADER_SIZE);
}

/* DVB  Descriptor 0x6e: Announcement support descriptor */
static void build_desc6e(uint8_t *desc) {
    desc6e_init(desc);

    desc6e_set_emergency_alarm_flag         (desc, true);
    desc6e_set_road_traffic_flash_flag      (desc, false);
    desc6e_set_public_transport_flash_flag  (desc, true);
    desc6e_set_warning_message_flag         (desc, true);
    desc6e_set_news_flash_flag              (desc, true);
    desc6e_set_weather_flash_flag           (desc, true);
    desc6e_set_event_announcement_flag      (desc, false);
    desc6e_set_personal_call_flag           (desc, false);

    {
        uint8_t n = 0;
        uint8_t *ann_n;
        desc_set_length(desc, 255);

        ann_n = desc6e_get_announcement(desc, n++);
        desc6en_set_announcement_type(ann_n, 0); // Emergency alarm
        desc6en_set_reference_type(ann_n, 0);

        ann_n = desc6e_get_announcement(desc, n++);
        desc6en_set_announcement_type(ann_n, 3); // Warning alarm
        desc6en_set_reference_type(ann_n, 1);

        // The following are valid only if
        //  reference_type == 0x01
        //  || reference_type == 0x02
        //  || reference_type == 0x03
        //
        desc6en_set_onid(ann_n, 0x1122);
        desc6en_set_tsid(ann_n, 0x3344);
        desc6en_set_service_id(ann_n, 0x5566);
        desc6en_set_component_tag(ann_n, 0x77);

        ann_n = desc6e_get_announcement(desc, n++);
        desc6en_set_announcement_type(ann_n, 5); // Weather flash
        desc6en_set_reference_type(ann_n, 0);

        ann_n = desc6e_get_announcement(desc, n++);
        desc6en_set_announcement_type(ann_n, 2); // Public Transport flash
        desc6en_set_reference_type(ann_n, 3);

        // The following are valid only if
        //  reference_type == 0x01
        //  || reference_type == 0x02
        //  || reference_type == 0x03
        //
        desc6en_set_onid(ann_n, 0x6677);
        desc6en_set_tsid(ann_n, 0x8899);
        desc6en_set_service_id(ann_n, 0x4455);
        desc6en_set_component_tag(ann_n, 0x88);

        ann_n = desc6e_get_announcement(desc, n++);
        desc6en_set_announcement_type(ann_n, 4); // News flash
        desc6en_set_reference_type(ann_n, 0);

        ann_n = desc6e_get_announcement(desc, n);
        desc_set_length(desc, ann_n - desc - DESC_HEADER_SIZE);
    }
}

/* ---  Descriptor 0x6f: application_signalling_descriptor */
/* ---  Descriptor 0x70: adaptation_field_data_descriptor */
/* ---  Descriptor 0x71: service_identifier_descriptor */
/* ---  Descriptor 0x72: service_availability_descriptor */
/* ---  Descriptor 0x73: default_authority_descriptor */
/* ---  Descriptor 0x74: related_content_descriptor */
/* ---  Descriptor 0x75: TVA_id_descriptor */
/* ---  Descriptor 0x76: content_identifier_descriptor */
/* ---  Descriptor 0x77: time_slice_fec_identifier_descriptor */
/* ---  Descriptor 0x78: ECM_repetition_rate_descriptor */
/* ---  Descriptor 0x79: S2_satellite_delivery_system_descriptor */

/* DVB  Descriptor 0x7a: Enhanced AC-3 descriptor */
static void build_desc7a(uint8_t *desc) {
    desc7a_init(desc);

    desc7a_set_component_type_flag  (desc, true);
    desc7a_set_component_type       (desc, 10);

    desc7a_set_bsid_flag            (desc, true);
    desc7a_set_bsid                 (desc, 20);

    desc7a_set_mainid_flag          (desc, true);
    desc7a_set_mainid               (desc, 30);

    desc7a_set_asvc_flag            (desc, true);
    desc7a_set_asvc                 (desc, 40);

    desc7a_set_mixinfoexists_flag   (desc, true);

    desc7a_set_substream1_flag      (desc, true);
    desc7a_set_substream1           (desc, 50);

    desc7a_set_substream2_flag      (desc, true);
    desc7a_set_substream2           (desc, 60);

    desc7a_set_substream3_flag      (desc, true);
    desc7a_set_substream3           (desc, 70);

    desc7a_set_length(desc);
}

/* DVB  Descriptor 0x7b: DTS descriptor */
static void build_desc7b(uint8_t *desc) {
    desc7b_init(desc);
    desc7b_set_sample_rate_code(desc, 8);
    desc7b_set_bit_rate_code(desc, 11);
    desc7b_set_nblks(desc, 5);
    desc7b_set_fsize(desc, 95);
    desc7b_set_surround_mode(desc, 3);
    desc7b_set_lfe_flag(desc, true);
    desc7b_set_extended_surround_flag(desc, 2);
}

/* DVB  Descriptor 0x7c: AAC descriptor */
static void build_desc7c(uint8_t *desc) {
    desc7c_init(desc);
    desc7c_set_profile_and_level(desc, 0x14);
    desc7c_set_aac_type_flag(desc, true);
    desc7c_set_aac_type(desc, 0x1f);
}

/* ---  Descriptor 0x7d: XAIT location descriptor */
/* ---  Descriptor 0x7e: FTA_content_management_descriptor */
/* ---  Descriptor 0x7f: extension descriptor */

static void output_psi_section(uint8_t *section, uint16_t pid, uint8_t *cc) {
    uint16_t section_length = psi_get_length(section) + PSI_HEADER_SIZE;
    uint16_t section_offset = 0;
    do {
        uint8_t ts[TS_SIZE];
        uint8_t ts_offset = 0;
        memset(ts, 0xff, TS_SIZE);

        psi_split_section(ts, &ts_offset, section, &section_offset);

        ts_set_pid(ts, pid);
        ts_set_cc(ts, *cc);
        (*cc)++;
        *cc &= 0xf;

        if (section_offset == section_length)
            psi_split_end(ts, &ts_offset);

1755 1756
        if (write(fileno(stdout), ts, TS_SIZE) < 0)
            perror("write");
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
    } while (section_offset < section_length);
}

/* =========================================================================
 * SI Tables
 * ========================================================================= */

/* MPEG Program Allocation Table (PAT) */
static void generate_pat(void) {
    // Generate empty PAT
    uint8_t *pat = psi_allocate();
    uint8_t *pat_n;
    uint8_t j = 0;

    // Generate empty PAT
    pat_init(pat);
    pat_set_length(pat, 0);
    pat_set_tsid(pat, tsid);
    psi_set_version(pat, 0);
    psi_set_current(pat);
    psi_set_section(pat, 0);
    psi_set_lastsection(pat, 0);
    psi_set_crc(pat);
    output_psi_section(pat, PAT_PID, &cc);

    // Increase PAT version
    psi_set_version(pat, 1);
    psi_set_current(pat);
    psi_set_crc(pat);
    output_psi_section(pat, PAT_PID, &cc);

    // Add couple of programs to PAT
    psi_set_version(pat, 2);
    psi_set_current(pat);
    psi_set_length(pat, PSI_MAX_SIZE);

    pat_n = pat_get_program(pat, j++);
    patn_init(pat_n);
    patn_set_program(pat_n, 0);
    patn_set_pid(pat_n, NIT_PID);

    pat_n = pat_get_program(pat, j++);
    patn_init(pat_n);
    patn_set_program(pat_n, sid);
    patn_set_pid(pat_n, pmt_pid);

    pat_n = pat_get_program(pat, j++);
    patn_init(pat_n);
    patn_set_program(pat_n, sid + 100);
    patn_set_pid(pat_n, pmt_pid + 100);

    pat_n = pat_get_program(pat, j++);
    patn_init(pat_n);
    patn_set_program(pat_n, sid + 200);
    patn_set_pid(pat_n, pmt_pid + 200);

    pat_n = pat_get_program(pat, j++);
    patn_init(pat_n);
    patn_set_program(pat_n, sid + 300);
    patn_set_pid(pat_n, pmt_pid + 300);

    // Set correct PAT length
    pat_n = pat_get_program(pat, j); // Get offset of the end of last program
    pat_set_length(pat, pat_n - pat - PAT_HEADER_SIZE);
    psi_set_crc(pat);

    output_psi_section(pat, PAT_PID, &cc);

    free(pat);
}

/* MPEG Conditional Access Table (CAT) */
static void generate_cat(void) {
    // Generate empty cat
    uint8_t *cat = psi_allocate();
    uint8_t *desc;
    uint8_t desc_loop[DESCS_HEADER_SIZE + DESCS_MAX_SIZE];
    uint8_t desc_counter;

    cat_init(cat);
    cat_set_length(cat, 0);
    psi_set_version(cat, 0);
    psi_set_current(cat);
    psi_set_crc(cat);
    output_psi_section(cat, CAT_PID, &cc);

    // Increase CAT version
    psi_set_version(cat, 1);
    psi_set_current(cat);
    psi_set_crc(cat);
    output_psi_section(cat, CAT_PID, &cc);

    // Add couple of descriptors to CAT
    psi_set_version(cat, 2);
    psi_set_current(cat);
    psi_set_length(cat, PSI_MAX_SIZE);

    descs_set_length(desc_loop, DESCS_MAX_SIZE);
    desc_counter = 0;

    desc = descs_get_desc(desc_loop, desc_counter++);
    build_desc09(desc);

    desc = descs_get_desc(desc_loop, desc_counter++);
    build_desc09(desc);

    desc = descs_get_desc(desc_loop, desc_counter++);
    build_desc09(desc);

    // Finish descriptor generation
    desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
    descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
    cat_set_desclength(cat, descs_get_length(desc_loop));
    // Put descriptor loop into CAT
    memcpy(cat_get_descl(cat), desc_loop + DESCS_HEADER_SIZE,
        descs_get_length(desc_loop));

    psi_set_crc(cat);
    output_psi_section(cat, CAT_PID, &cc);

    free(cat);
}

/* MPEG Conditional Access Table (TSDT) */
static void generate_tsdt(void) {
    // Generate empty tsdt
    uint8_t *tsdt = psi_allocate();
    uint8_t *desc;
    uint8_t desc_loop[DESCS_HEADER_SIZE + DESCS_MAX_SIZE];
    uint8_t desc_counter;

    tsdt_init(tsdt);
    tsdt_set_length(tsdt, 0);
    psi_set_version(tsdt, 0);
    psi_set_current(tsdt);
    psi_set_crc(tsdt);
    output_psi_section(tsdt, TSDT_PID, &cc);

    // Add couple of descriptors to TSDT
    psi_set_version(tsdt, 1);
    psi_set_current(tsdt);
    psi_set_length(tsdt, PSI_MAX_SIZE);

    descs_set_length(desc_loop, DESCS_MAX_SIZE);
    desc_counter = 0;

    desc = descs_get_desc(desc_loop, desc_counter++);
    build_desc67(desc, "DVB");

    desc = descs_get_desc(desc_loop, desc_counter++);
    build_desc67(desc, "CONT");

    desc = descs_get_desc(desc_loop, desc_counter++);
    build_desc68(desc);

    // Finish descriptor generation
    desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
    descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
    tsdt_set_desclength(tsdt, descs_get_length(desc_loop));
    // Put descriptor loop into TSDT
    memcpy(tsdt_get_descl(tsdt), desc_loop + DESCS_HEADER_SIZE,
        descs_get_length(desc_loop));

    psi_set_crc(tsdt);
    output_psi_section(tsdt, TSDT_PID, &cc);

    free(tsdt);
}

/* DVB  Network Information Table (NIT) */
static void generate_nit(void) {
    uint8_t *nit = psi_allocate();
    uint8_t *nit_tsloop;
    uint8_t *desc_loop, *desc;
    uint8_t desc_counter;

    // Generate empty nit
    nit_init(nit, true);
    psi_set_version(nit, 0);
    psi_set_current(nit);
    nit_set_nid(nit, onid);
    nit_set_length(nit, 2);

    nit_set_desclength(nit, 0);

    nit_tsloop = nit_get_header2(nit);
    nith_init(nit_tsloop);
    nith_set_tslength(nit_tsloop, 0);

    psi_set_crc(nit);

    output_psi_section(nit, NIT_PID, &cc);

    // Add descriptors and transport stream loop
    nit_init(nit, true);
    psi_set_version(nit, 1);
    psi_set_current(nit);
    nit_set_nid(nit, onid);
    nit_set_length(nit, PSI_MAX_SIZE);
    nit_set_desclength(nit, 0);

    {
        // Add descriptors to network descriptors
        desc_counter = 0;
        desc_loop = nit_get_descs(nit);

        nit_set_desclength(nit, DESCS_MAX_SIZE);
        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc40(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc43(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc44(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc5a(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc5b(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc62(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc6c(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc6d(desc);

        // Finish descriptor generation
        desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
        nit_set_desclength(nit, desc - desc_loop - DESCS_HEADER_SIZE);
        nit_set_length(nit, 2 + nit_get_desclength(nit));
    }

    // Process transport stream loop
    nit_tsloop = nit_get_header2(nit);
    nith_init(nit_tsloop);
    nit_set_length(nit, PSI_MAX_SIZE); // This needed so nit_get_ts works
    {
        uint8_t *nit_n;
        uint8_t nit_n_counter = 0;

        nit_n = nit_get_ts(nit, nit_n_counter++);
        nitn_init(nit_n);
        nitn_set_tsid(nit_n, tsid);
        nitn_set_onid(nit_n, onid);
        nitn_set_desclength(nit_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = nitn_get_descs(nit_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc41(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc4a_std(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        nit_n = nit_get_ts(nit, nit_n_counter++);
        nitn_init(nit_n);
        nitn_set_tsid(nit_n, tsid + 100);
        nitn_set_onid(nit_n, onid + 100);
        nitn_set_desclength(nit_n, 0);

        nit_n = nit_get_ts(nit, nit_n_counter++);
        nitn_init(nit_n);
        nitn_set_tsid(nit_n, tsid + 200);
        nitn_set_onid(nit_n, onid + 200);
        nitn_set_onid(nit_n, onid + 200);
        nitn_set_desclength(nit_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = nitn_get_descs(nit_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc41(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc4a_mobile(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        nit_n = nit_get_ts(nit, nit_n_counter++);
        nitn_init(nit_n);
        nitn_set_tsid(nit_n, tsid + 300);
        nitn_set_onid(nit_n, onid + 300);
        nitn_set_desclength(nit_n, 0);

        // Set transport_stream_loop length
        nit_n = nit_get_ts(nit, nit_n_counter); // Get last ts
        nith_set_tslength(nit_tsloop, nit_n - nit_tsloop - NIT_HEADER2_SIZE);
    }
    // Set NIT length
    nit_set_length(nit, 2 + nit_get_desclength(nit) + nith_get_tslength(nit_tsloop));
    psi_set_crc(nit);

    output_psi_section(nit, NIT_PID, &cc);

    free(nit);
}

/* DVB  Bouquet Association Table (BAT) */
static void generate_bat(void) {
    uint8_t *bat = psi_allocate();
    uint8_t *bat_tsloop;
    uint8_t *desc_loop, *desc;
    uint8_t desc_counter;

    // Generate empty BAT
    bat_init(bat);
    psi_set_version(bat, 0);
    psi_set_current(bat);
    bat_set_bouquet_id(bat, onid);
    bat_set_length(bat, 2);

    bat_set_desclength(bat, 0);

    bat_tsloop = bat_get_header2(bat);
    bath_init(bat_tsloop);
    bath_set_tslength(bat_tsloop, 0);

    psi_set_crc(bat);

    output_psi_section(bat, BAT_PID, &cc);

    // Add descriptors and transport stream loop
    bat_init(bat);
    psi_set_version(bat, 1);
    psi_set_current(bat);
    bat_set_bouquet_id(bat, onid);
    bat_set_length(bat, PSI_MAX_SIZE);
    bat_set_desclength(bat, 0);

    {
        // Add descriptors to bouquet descriptors
        bat_set_desclength(bat, DESCS_MAX_SIZE);

        desc_counter = 0;
        desc_loop = bat_get_descs(bat);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc47(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc5c(desc);

        // Finish descriptor generation
        desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
        bat_set_desclength(bat, desc - desc_loop - DESCS_HEADER_SIZE);
        bat_set_length(bat, 2 + bat_get_desclength(bat));
    }

    // Process transport stream loop
    bat_tsloop = bat_get_header2(bat);
    bath_init(bat_tsloop);
    bat_set_length(bat, PSI_MAX_SIZE); // This needed so bat_get_ts works
    {
        uint8_t *bat_n;
        uint8_t bat_n_counter = 0;

        bat_n = bat_get_ts(bat, bat_n_counter++);
        batn_init(bat_n);
        batn_set_tsid(bat_n, tsid);
        batn_set_onid(bat_n, onid);
        batn_set_desclength(bat_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = batn_get_descs(bat_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc41(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        bat_n = bat_get_ts(bat, bat_n_counter++);
        batn_init(bat_n);
        batn_set_tsid(bat_n, tsid + 100);
        batn_set_onid(bat_n, onid + 100);
        batn_set_desclength(bat_n, 0);

        bat_n = bat_get_ts(bat, bat_n_counter++);
        batn_init(bat_n);
        batn_set_tsid(bat_n, tsid + 200);
        batn_set_onid(bat_n, onid + 200);
        batn_set_desclength(bat_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = batn_get_descs(bat_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc41(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        bat_n = bat_get_ts(bat, bat_n_counter++);
        batn_init(bat_n);
        batn_set_tsid(bat_n, tsid + 300);
        batn_set_onid(bat_n, onid + 300);
        batn_set_desclength(bat_n, 0);

        // Set transport_stream_loop length
        bat_n = bat_get_ts(bat, bat_n_counter); // Get last ts
        bath_set_tslength(bat_tsloop, bat_n - bat_tsloop - BAT_HEADER2_SIZE);
    }
    // Set BAT length
    bat_set_length(bat, 2 + bat_get_desclength(bat) + bath_get_tslength(bat_tsloop));
    psi_set_crc(bat);

    output_psi_section(bat, BAT_PID, &cc);

    free(bat);
}

/* DVB  Service Definition Table (SDT) */
static void generate_sdt(void) {
    uint8_t *sdt = psi_allocate();
    uint8_t *desc_loop, *desc;
    uint8_t desc_counter;

    // Generate empty SDT
    sdt_init(sdt, true);
    psi_set_version(sdt, 0);
    psi_set_current(sdt);
    sdt_set_tsid(sdt, tsid);
    sdt_set_onid(sdt, onid);
    sdt_set_length(sdt, 0);
    psi_set_crc(sdt);
    output_psi_section(sdt, SDT_PID, &cc);

    // Add service descriptions
    sdt_init(sdt, true);
    psi_set_version(sdt, 1);
    psi_set_current(sdt);
    sdt_set_tsid(sdt, tsid);
    sdt_set_onid(sdt, onid);

    // Process transport stream loop
    sdt_set_length(sdt, PSI_MAX_SIZE); // This needed so sdt_get_ts works
    {
        uint8_t *sdt_n;
        uint8_t sdt_n_counter = 0;

        sdt_n = sdt_get_service(sdt, sdt_n_counter++);
        sdtn_init(sdt_n);
        sdtn_set_sid(sdt_n, sid);
        sdtn_set_running(sdt_n, 2);
        sdtn_set_desclength(sdt_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = sdtn_get_descs(sdt_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc48(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc49(desc, true);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc51(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc57(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc5d(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc5f(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        sdt_n = sdt_get_service(sdt, sdt_n_counter++);
        sdtn_init(sdt_n);
        sdtn_set_sid(sdt_n, sid + 100);
        sdtn_set_eitschedule(sdt_n);
        sdtn_set_running(sdt_n, 1);
        sdtn_set_desclength(sdt_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = sdtn_get_descs(sdt_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc48(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc49(desc, false);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc5f(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc6e(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        sdt_n = sdt_get_service(sdt, sdt_n_counter++);
        sdtn_init(sdt_n);
        sdtn_set_sid(sdt_n, sid + 200);
        sdtn_set_eitschedule(sdt_n);
        sdtn_set_eitpresent(sdt_n);
        sdtn_set_running(sdt_n, 3);
        sdtn_set_desclength(sdt_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = sdtn_get_descs(sdt_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc48(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc4c(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc5f(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc64(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc66(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        sdt_n = sdt_get_service(sdt, sdt_n_counter++);
        sdtn_init(sdt_n);
        sdtn_set_sid(sdt_n, sid + 300);
        sdtn_set_eitschedule(sdt_n);
        sdtn_set_eitpresent(sdt_n);
        sdtn_set_running(sdt_n, 2);
        sdtn_set_ca(sdt_n);
        sdtn_set_desclength(sdt_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = sdtn_get_descs(sdt_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc48(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc4b(desc, false);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc5f(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        // Set transport_stream_loop length
        sdt_n = sdt_get_service(sdt, sdt_n_counter); // Get last service
        sdt_set_length(sdt, sdt_n - sdt_get_service(sdt, 0));
    }
    psi_set_crc(sdt);
    output_psi_section(sdt, SDT_PID, &cc);

    free(sdt);
}

/* DVB  Event Information Table (EIT) */
static void generate_eit(void) {
    uint8_t *eit = psi_private_allocate();
    uint8_t *desc_loop, *desc;
    uint8_t desc_counter;

    // Generate empty EIT
    eit_init(eit, true);
    psi_set_version(eit, 0);
    psi_set_current(eit);

    eit_set_length(eit, 0);
2369
    eit_set_sid(eit, sid);
2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393
    eit_set_tsid(eit, tsid);
    eit_set_onid(eit, onid);
    eit_set_segment_last_sec_number(eit, 0);
    eit_set_last_table_id(eit, 0);
    psi_set_crc(eit);
    output_psi_section(eit, EIT_PID, &cc);

    // Add service descriptions
    eit_init(eit, true);
    psi_set_version(eit, 1);
    psi_set_current(eit);
    eit_set_tsid(eit, tsid);
    eit_set_onid(eit, onid);

    // Process transport stream loop
    eit_set_length(eit, PSI_MAX_SIZE); // This needed so eit_get_ts works
    {
        uint8_t *eit_n;
        uint8_t eit_n_counter = 0;

        eit_n = eit_get_event(eit, eit_n_counter++);
        eitn_set_event_id(eit_n, event_id);
        eitn_set_start_time(eit_n, dvb_time_encode_UTC(ts_0));
        eitn_set_duration_bcd(eit_n, dvb_time_encode_duration(86399));
2394
        eitn_set_running(eit_n, 2);
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413
        eitn_set_desclength(eit_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = eitn_get_descs(eit_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc4d(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        eit_n = eit_get_event(eit, eit_n_counter++);
        eitn_set_event_id(eit_n, event_id + 100);
        eitn_set_start_time(eit_n, dvb_time_encode_UTC(ts_1));
        eitn_set_duration_bcd(eit_n, dvb_time_encode_duration(3600));
2414
        eitn_set_running(eit_n, 1);
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
        eitn_set_desclength(eit_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = eitn_get_descs(eit_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc4a_event(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc4e(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc4d(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc61(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        eit_n = eit_get_event(eit, eit_n_counter++);
        eitn_set_event_id(eit_n, event_id + 200);
        eitn_set_start_time(eit_n, dvb_time_encode_UTC(ts_2));
        eitn_set_duration_bcd(eit_n, dvb_time_encode_duration(7200));
2443
        eitn_set_running(eit_n, 0);
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
        eitn_set_desclength(eit_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = eitn_get_descs(eit_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc4a_extended_event(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc54(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc53(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc55(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc4d(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc4f(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        eit_n = eit_get_event(eit, eit_n_counter++);
        eitn_set_event_id(eit_n, event_id + 300);
        eitn_set_start_time(eit_n, dvb_time_encode_UTC(ts_2));
        eitn_set_duration_bcd(eit_n, dvb_time_encode_duration(7200));
2478 2479
        eitn_set_running(eit_n, 4);
        eitn_set_ca(eit_n);
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608
        eitn_set_desclength(eit_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = eitn_get_descs(eit_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc54(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc4d(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc4e(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc55(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc69(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        // Set transport_stream_loop length
        eit_n = eit_get_event(eit, eit_n_counter); // Get last service
        eit_set_length(eit, eit_n - eit_get_event(eit, 0));
    }
    psi_set_crc(eit);
    output_psi_section(eit, EIT_PID, &cc);

    free(eit);
}

/* DVB  Time and Date Table (TDT) */
static void generate_tdt(void) {
    uint8_t *tdt = psi_allocate();

    tdt_init(tdt);
    tdt_set_utc(tdt, dvb_time_encode_UTC(ts_0));
    if (dvb_time_decode_UTC(tdt_get_utc(tdt)) != ts_0)
        fprintf(stderr, "check tdt_get_set_utc!!!\n");
    output_psi_section(tdt, TDT_PID, &cc);

    tdt_set_utc(tdt, dvb_time_encode_UTC(ts_1));
    if (dvb_time_decode_UTC(tdt_get_utc(tdt)) != ts_1)
        fprintf(stderr, "check tdt_get_set_utc!!!\n");
    output_psi_section(tdt, TDT_PID, &cc);

    tdt_set_utc(tdt, dvb_time_encode_UTC(ts_2));
    if (dvb_time_decode_UTC(tdt_get_utc(tdt)) != ts_2)
        fprintf(stderr, "check tdt_get_set_utc!!!\n");
    output_psi_section(tdt, TDT_PID, &cc);

    tdt_set_utc(tdt, dvb_time_encode_UTC(ts_3));
    if (dvb_time_decode_UTC(tdt_get_utc(tdt)) != ts_3)
        fprintf(stderr, "check tdt_get_set_utc!!!\n");
    output_psi_section(tdt, TDT_PID, &cc);

    free(tdt);
}

/* DVB  Time Offset Table (TOT) */
static void generate_tot(void) {
    uint8_t *tot = psi_allocate();
    uint8_t *desc_loop, *desc;
    uint8_t desc_counter;

    tot_init(tot);
    tot_set_length(tot, 0);
    tot_set_desclength(tot, 0);

    tot_set_utc(tot, dvb_time_encode_UTC(ts_0));
    if (dvb_time_decode_UTC(tot_get_utc(tot)) != ts_0)
        fprintf(stderr, "check tot_get_set_utc!!!\n");
    psi_set_crc(tot);
    output_psi_section(tot, TOT_PID, &cc);

    tot_set_utc(tot, dvb_time_encode_UTC(ts_1));
    if (dvb_time_decode_UTC(tot_get_utc(tot)) != ts_1)
        fprintf(stderr, "check tot_get_set_utc!!!\n");
    psi_set_crc(tot);
    output_psi_section(tot, TOT_PID, &cc);

    /* Change time */
    tot_set_utc(tot, dvb_time_encode_UTC(ts_2));

    /* Add some descriptors */
    desc_loop = tot_get_descs(tot); // First descriptor
    tot_set_desclength(tot, DESCS_MAX_SIZE);
    desc_counter = 0;

    desc = descs_get_desc(desc_loop, desc_counter++);
    build_desc58(desc);

    // Finish descriptor generation
    desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
    tot_set_desclength(tot, desc - desc_loop - DESCS_HEADER_SIZE);
    tot_set_length(tot, tot_get_desclength(tot) + DESCS_HEADER_SIZE);
    psi_set_crc(tot);

    output_psi_section(tot, TOT_PID, &cc);

    free(tot);
}

/* DVB  Running Status Table (RST) */
static void generate_rst(void) {
    uint8_t *rst = psi_allocate();
    uint8_t *rst_n;
    uint8_t j = 0;

    // Generate empty rst
    rst_init(rst);
    rst_set_length(rst, 0);
    output_psi_section(rst, RST_PID, &cc);

    // Add couple of statuses to rst
    rst_set_length(rst, PSI_MAX_SIZE);

    rst_n = rst_get_status(rst, j++);
    rstn_init(rst_n);
    rstn_set_tsid(rst_n, tsid);
    rstn_set_onid(rst_n, onid);
    rstn_set_service_id(rst_n, sid);
    rstn_set_event_id(rst_n, event_id);
2609
    rstn_set_running(rst_n, 1);
2610 2611 2612 2613 2614 2615 2616

    rst_n = rst_get_status(rst, j++);
    rstn_init(rst_n);
    rstn_set_tsid(rst_n, tsid + 100);
    rstn_set_onid(rst_n, onid + 100);
    rstn_set_service_id(rst_n, sid + 100);
    rstn_set_event_id(rst_n, event_id + 100);
2617
    rstn_set_running(rst_n, 2);
2618 2619 2620 2621 2622 2623 2624

    rst_n = rst_get_status(rst, j++);
    rstn_init(rst_n);
    rstn_set_tsid(rst_n, tsid + 200);
    rstn_set_onid(rst_n, onid + 200);
    rstn_set_service_id(rst_n, sid + 200);
    rstn_set_event_id(rst_n, event_id + 200);
2625
    rstn_set_running(rst_n, 3);
2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968

    // Set correct rst length
    rst_n = rst_get_status(rst, j); // Get offset of the end of last status
    rst_set_length(rst, rst_n - rst - RST_HEADER_SIZE);

    output_psi_section(rst, RST_PID, &cc);

    free(rst);
}

/* MPEG Program Map Table (PMT) */
static void generate_pmt(void) {
    uint8_t *pmt = psi_allocate();
    uint8_t *pmt_n;
    uint8_t pmt_n_counter;
    uint8_t *desc_loop, *desc;
    uint8_t desc_counter;

    // Generate empty PMT
    pmt_init(pmt);
    psi_set_version(pmt, 0);
    psi_set_current(pmt);

    pmt_set_length(pmt, 0);
    pmt_set_program(pmt, sid);
    pmt_set_pcrpid(pmt, pmt_pid + 10);
    pmt_set_desclength(pmt, 0);

    psi_set_crc(pmt);
    output_psi_section(pmt, pmt_pid, &cc);

    // Add elementary streams
    pmt_init(pmt);
    psi_set_version(pmt, 1);
    psi_set_current(pmt);
    pmt_set_pcrpid(pmt, pmt_pid + 10);

    {
        // Add descriptors to program descriptors
        pmt_set_length(pmt, PSI_MAX_SIZE);
        pmt_set_desclength(pmt, DESCS_MAX_SIZE);

        desc_counter = 0;
        desc_loop = pmt_get_descs(pmt);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc0b(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc0c(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc0d(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc0e(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc0f(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc10(desc);

        desc = descs_get_desc(desc_loop, desc_counter++);
        build_desc12(desc);

        // Finish descriptor generation
        desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
        pmt_set_desclength(pmt, desc - desc_loop - DESCS_HEADER_SIZE);
        pmt_set_length(pmt, pmt_get_desclength(pmt));
    }

    {
        pmt_set_length(pmt, PSI_MAX_SIZE); // This needed so pmt_get_es works

        // Process elementary streams
        pmt_n_counter = 0;

        pmt_n = pmt_get_es(pmt, pmt_n_counter++);
        pmtn_init(pmt_n);
        pmtn_set_streamtype(pmt_n, 0x02); // MPEG2 Video
        pmtn_set_pid(pmt_n, pmt_pid + 20);
        pmtn_set_desclength(pmt_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = pmtn_get_descs(pmt_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc02_1(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc02_2(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc04(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc05(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc06(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc07(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc08(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc11(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc1b(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        pmt_n = pmt_get_es(pmt, pmt_n_counter++);
        pmtn_init(pmt_n);
        pmtn_set_streamtype(pmt_n, 0x04); // MPEG2 audio
        pmtn_set_pid(pmt_n, pmt_pid + 21);
        pmtn_set_desclength(pmt_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = pmtn_get_descs(pmt_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc03(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc0a(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc52(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc1c(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc50(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc5e(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc6a(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc7a(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc7b(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        pmt_n = pmt_get_es(pmt, pmt_n_counter++);
        pmtn_init(pmt_n);
        pmtn_set_streamtype(pmt_n, 0x06); // PES Private Data (desc46 - VBI teletext)
        pmtn_set_pid(pmt_n, pmt_pid + 22);
        pmtn_set_desclength(pmt_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = pmtn_get_descs(pmt_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc46(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc45(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        pmt_n = pmt_get_es(pmt, pmt_n_counter++);
        pmtn_init(pmt_n);
        pmtn_set_streamtype(pmt_n, 0x06); // PES Private Data (desc56 - Teletext)
        pmtn_set_pid(pmt_n, pmt_pid + 23);
        pmtn_set_desclength(pmt_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = pmtn_get_descs(pmt_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc56(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc60(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc65(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        pmt_n = pmt_get_es(pmt, pmt_n_counter++);
        pmtn_init(pmt_n);
        pmtn_set_streamtype(pmt_n, 0x06); // PES Private Data (desc59 - Subtitles)
        pmtn_set_pid(pmt_n, pmt_pid + 24);
        pmtn_set_desclength(pmt_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = pmtn_get_descs(pmt_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc59(desc);

            // Finish descriptor generation
            desc = descs_get_desc(desc_loop, desc_counter); // Get next descriptor pos
            descs_set_length(desc_loop, desc - desc_loop - DESCS_HEADER_SIZE);
        }

        pmt_n = pmt_get_es(pmt, pmt_n_counter++);
        pmtn_init(pmt_n);
        pmtn_set_streamtype(pmt_n, 0x06);
        pmtn_set_pid(pmt_n, pmt_pid + 25);
        pmtn_set_desclength(pmt_n, 0);
        {
            // Add descriptors to transport_stream_n
            desc_counter = 0;
            desc_loop = pmtn_get_descs(pmt_n);
            descs_set_length(desc_loop, DESCS_MAX_SIZE); // This is needed so descs_get_desc(x, n) works

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc1e(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc1d(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc1f(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc20(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc21(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);
            build_desc22(desc);

            desc = descs_get_desc(desc_loop, desc_counter++);