libtsfuncs is a library for mpeg PSI parsing and generation. https://georgi.unixsol.org/programs/libtsfuncs/

nit_desc.c 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <netdb.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "tsfuncs.h"
  7. static void ts_nit_regenerate_packet_data(struct ts_nit *nit) {
  8. uint8_t *ts_packets;
  9. int num_packets;
  10. ts_nit_generate(nit, &ts_packets, &num_packets);
  11. memcpy(nit->section_header->packet_data, ts_packets, num_packets * TS_PACKET_SIZE);
  12. nit->section_header->num_packets = num_packets;
  13. free(ts_packets);
  14. }
  15. struct ts_nit *ts_nit_alloc_init(uint16_t network_id) {
  16. struct ts_nit *nit = ts_nit_alloc();
  17. nit->ts_header.pid = 0x10;
  18. nit->ts_header.pusi = 1;
  19. nit->ts_header.payload_field = 1;
  20. nit->ts_header.payload_offset = 4;
  21. nit->section_header->table_id = 0x40;
  22. nit->section_header->version_number = 1;
  23. nit->section_header->current_next_indicator = 1;
  24. nit->section_header->section_syntax_indicator = 1;
  25. nit->section_header->private_indicator = 1;
  26. nit->section_header->section_length = 9 + 4; // Empty section, +4 (16+16) for NIT table data
  27. nit->section_header->ts_id_number = network_id;
  28. nit->section_header->reserved1 = 3;
  29. nit->section_header->reserved2 = 3;
  30. nit->reserved1 = 0xf;
  31. nit->network_info_size = 0; // 16 bits
  32. nit->reserved2 = 0xf;
  33. nit->ts_loop_size = 0; // 16 bits
  34. nit->initialized = 1;
  35. ts_nit_regenerate_packet_data(nit);
  36. return nit;
  37. }
  38. int ts_nit_add_network_name_descriptor(struct ts_nit *nit, char *network_name) {
  39. if (!network_name || strlen(network_name) > 255)
  40. return 0;
  41. nit->network_info_size = strlen(network_name) + 2;
  42. uint8_t *descriptor = calloc(1, nit->network_info_size);
  43. int dpos = 0;
  44. descriptor[dpos + 0] = 0x40; // Network name descriptor
  45. descriptor[dpos + 1] = nit->network_info_size - 2; // -2 Because of two byte header
  46. dpos += 2;
  47. char *name = network_name;
  48. while (name[0]) {
  49. descriptor[dpos++] = name[0];
  50. name++;
  51. }
  52. nit->network_info = descriptor;
  53. nit->section_header->section_length += nit->network_info_size;
  54. ts_nit_regenerate_packet_data(nit);
  55. return 1;
  56. }
  57. static int ts_nit_add_stream(struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id, uint8_t *desc, uint8_t desc_size) {
  58. if (nit->streams_num == nit->streams_max - 1 || desc_size == 0) {
  59. FREE(desc);
  60. return 0;
  61. }
  62. int stream_len = 2 + 2 + 1 + 1 + desc_size;
  63. if (stream_len + nit->section_header->section_length > 4093) {
  64. ts_LOGf("NIT no space left, max 4093, current %d will become %d!\n",
  65. nit->section_header->section_length,
  66. stream_len + nit->section_header->section_length);
  67. free(desc);
  68. return 0;
  69. }
  70. nit->ts_loop_size += stream_len;
  71. nit->section_header->section_length += stream_len;
  72. struct ts_nit_stream *sinfo = calloc(1, sizeof(struct ts_nit_stream));
  73. sinfo->transport_stream_id = ts_id; // 2 bytes
  74. sinfo->original_network_id = org_net_id; // 2 bytes
  75. sinfo->reserved1 = 15; // 1 byte
  76. sinfo->descriptor_size = desc_size; // 1 byte
  77. sinfo->descriptor_data = desc; // desc_size bytes
  78. nit->streams[nit->streams_num] = sinfo;
  79. nit->streams_num++;
  80. ts_nit_regenerate_packet_data(nit);
  81. return 1;
  82. }
  83. // freq_type 0 == undefined
  84. // freq_type 1 == satellite
  85. // freq_type 2 == cable
  86. // freq_type 3 == terrestrial
  87. static int ts_nit_add_frequency_list_descriptor(struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id, uint8_t freq_type, uint32_t *freqs, uint8_t num_freqs) {
  88. uint8_t i;
  89. if (!num_freqs || num_freqs > 63)
  90. return 0;
  91. int desc_size = 2 + 1 + num_freqs * 4; // 2 for header desc header, 1 for coding type, 4 for each frequency
  92. uint8_t *desc = calloc(1, desc_size);
  93. int dpos = 0;
  94. desc[dpos + 0] = 0x62; // frequency_list_descriptor
  95. desc[dpos + 1] = desc_size - 2; // -2 Because of two byte header
  96. desc[dpos + 2] = 0xfc | freq_type; // 6 bits reserved, 2 bits freq_type
  97. dpos += 3;
  98. for(i=0;i<num_freqs;i++) {
  99. uint32_t freq = freqs[i];
  100. desc[dpos + 0] = ((freq &~ 0x00ffffff) >> 24);
  101. desc[dpos + 1] = ((freq &~ 0xff00ffff) >> 16);
  102. desc[dpos + 2] = ((freq &~ 0xffff00ff) >> 8);
  103. desc[dpos + 3] = (freq &~ 0xffffff00);
  104. dpos += 4;
  105. }
  106. return ts_nit_add_stream(nit, ts_id, org_net_id, desc, desc_size);
  107. }
  108. int ts_nit_add_frequency_list_descriptor_cable(struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id, uint32_t *freqs, uint8_t num_freqs) {
  109. return ts_nit_add_frequency_list_descriptor(nit, ts_id, org_net_id, 2, freqs, num_freqs);
  110. }
  111. int ts_nit_add_cable_delivery_descriptor(struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id, uint32_t freq, uint8_t modulation, uint32_t symbol_rate) {
  112. int desc_size = 13;
  113. uint8_t *desc = calloc(1, desc_size);
  114. desc[ 0] = 0x44; // cable_delivey_system_descriptor
  115. desc[ 1] = 11; // -2 Because of two byte header
  116. desc[ 2] = ((freq &~ 0x00ffffff) >> 24); // 32 bits, frequency
  117. desc[ 3] = ((freq &~ 0xff00ffff) >> 16);
  118. desc[ 4] = ((freq &~ 0xffff00ff) >> 8);
  119. desc[ 5] = (freq &~ 0xffffff00);
  120. desc[ 6] = 0xff; // 8 bits reserved
  121. desc[ 7] = 0xf0; // 4 bits reserved, 4 bits FEC_outer (0 == not defined)
  122. desc[ 8] = modulation; // 8 bits reserved
  123. desc[ 9] = (symbol_rate >> 20) &~ 0xffffff00; // 28 bits, symbol_rate
  124. desc[10] = (symbol_rate >> 12) &~ 0xffffff00;
  125. desc[11] = (symbol_rate >> 4 ) &~ 0xffffff00;
  126. desc[12] = (symbol_rate &~ 0xfffffff0) << 4; // 4 bits
  127. desc[12] |= 0; // 4 bits FEC_inner (0 == not defined)
  128. return ts_nit_add_stream(nit, ts_id, org_net_id, desc, desc_size);
  129. }
  130. int ts_nit_add_service_list_descriptor(struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id, uint32_t *services, uint8_t num_services) {
  131. uint8_t i;
  132. if (!num_services || num_services > 85) // 85 * 3 == 255
  133. return 0;
  134. int desc_size = 2 + num_services * 3; // 2 for header desc header, 3 for each service
  135. uint8_t *desc = calloc(1, desc_size);
  136. int dpos = 0;
  137. desc[dpos + 0] = 0x41; // service_list_descriptor
  138. desc[dpos + 1] = desc_size - 2; // -2 Because of two byte header
  139. dpos += 2;
  140. for(i=0;i<num_services;i++) {
  141. uint32_t srv = services[i];
  142. desc[dpos + 0] = (srv &~ 0xff00ffff) >> 16; // service_id (16 bits)
  143. desc[dpos + 1] = (srv &~ 0xffff00ff) >> 8;
  144. desc[dpos + 2] = (srv &~ 0xffffff00); // service_type (8 bits)
  145. dpos += 3;
  146. }
  147. return ts_nit_add_stream(nit, ts_id, org_net_id, desc, desc_size);
  148. }