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

nit_desc.c 6.3KB

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