libtsfuncs is a library for mpeg PSI parsing and generation. https://georgi.unixsol.org/programs/libtsfuncs/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tsfuncs.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <netdb.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <sys/time.h>
  8. #include "tsfuncs.h"
  9. void ts_packet_init_null(uint8_t *ts_packet) {
  10. memset(ts_packet, 0xff, TS_PACKET_SIZE);
  11. ts_packet[0] = 0x47;
  12. ts_packet[1] = 0x1f;
  13. ts_packet[2] = 0xff;
  14. ts_packet[3] = 0x00;
  15. }
  16. inline int ts_packet_is_pusi(uint8_t *ts_packet) {
  17. return (ts_packet[1] &~ 0xbf) >> 6;
  18. }
  19. inline uint16_t ts_packet_get_pid(uint8_t *ts_packet) {
  20. return (ts_packet[1] &~ 0xE0) << 8 | ts_packet[2];
  21. }
  22. inline void ts_packet_set_pid(uint8_t *ts_packet, uint16_t new_pid) {
  23. ts_packet[1] = (ts_packet[1] &~ 0x1f) | (new_pid >> 8); // 111xxxxx xxxxxxxx
  24. ts_packet[2] = new_pid &~ 0xff00;
  25. }
  26. inline uint8_t ts_packet_get_cont(uint8_t *ts_packet) {
  27. return (ts_packet[3] &~ 0xF0); // 1111xxxx
  28. }
  29. inline void ts_packet_set_cont(uint8_t *ts_packet, uint8_t value) {
  30. // Mask the last 4 bits (continuity), then set the continuity
  31. ts_packet[3] = (ts_packet[3] &~ 0x0F) | (value &~ 0xF0);
  32. }
  33. inline void ts_packet_inc_cont(uint8_t *ts_packet, uint8_t increment) {
  34. ts_packet_set_cont(ts_packet, ts_packet_get_cont(ts_packet) + increment);
  35. }
  36. inline int ts_packet_is_scrambled(uint8_t *ts_packet) {
  37. return (ts_packet[3] >> 6) > 1; // 0 is not scamlbed, 1 is reserved, 2 or 3 mean scrambled
  38. }
  39. void ts_packet_set_scrambled(uint8_t *ts_packet, enum ts_scrambled_type stype) {
  40. ts_packet[3] = ts_packet[3] &~ 0xc0; // Mask top two bits (11xxxxxx)
  41. if (stype == scrambled_with_even_key)
  42. ts_packet[3] |= 2 << 6;
  43. if (stype == scrambled_with_odd_key)
  44. ts_packet[3] |= 3 << 6;
  45. if (stype == scrambled_reserved)
  46. ts_packet[3] |= 1 << 6;
  47. }
  48. uint8_t ts_packet_get_payload_offset(uint8_t *ts_packet) {
  49. if (ts_packet[0] != 0x47)
  50. return 0;
  51. uint8_t adapt_field = (ts_packet[3] &~ 0xDF) >> 5; // 11x11111
  52. uint8_t payload_field = (ts_packet[3] &~ 0xEF) >> 4; // 111x1111
  53. if (!adapt_field && !payload_field) // Not allowed
  54. return 0;
  55. if (adapt_field) {
  56. uint8_t adapt_len = ts_packet[4];
  57. if (payload_field && adapt_len > 182) // Validity checks
  58. return 0;
  59. if (!payload_field && adapt_len > 183)
  60. return 0;
  61. if (adapt_len + 1 + 4 >= 188) // adaptation field takes the whole packet
  62. return 0;
  63. return 4 + 1 + adapt_len; // ts header + adapt_field_len_byte + adapt_field_len
  64. } else {
  65. return 4; // No adaptation, data starts directly after TS header
  66. }
  67. }
  68. /*
  69. * Decode a PTS or DTS value.
  70. *
  71. * - `data` is the 5 bytes containing the encoded PTS or DTS value
  72. * - `required_guard` should be 2 for a PTS alone, 3 for a PTS before
  73. * a DTS, or 1 for a DTS after a PTS
  74. * - `value` is the PTS or DTS value as decoded
  75. *
  76. * Returns 0 if the PTS/DTS value is decoded successfully, 1 if an error occurs
  77. */
  78. int ts_decode_pts_dts(uint8_t *data, int required_guard, uint64_t *value) {
  79. uint64_t pts1,pts2,pts3;
  80. int marker;
  81. char *what;
  82. int guard = (data[0] & 0xF0) >> 4;
  83. // Rather than try to use casts to make the arithmetic come out right on both
  84. // Linux-with-gcc (old-style C rules) and Windows-with-VisualC++ (C99 rules),
  85. // it's simpler just to use intermediates that won't get cast to "int".
  86. unsigned int data0 = data[0];
  87. unsigned int data1 = data[1];
  88. unsigned int data2 = data[2];
  89. unsigned int data3 = data[3];
  90. unsigned int data4 = data[4];
  91. switch (required_guard) {
  92. case 2: what = "PTS"; break; // standalone
  93. case 3: what = "PTS"; break; // before a DTS
  94. case 1: what = "DTS"; break; // always after a PTS
  95. default: what = "???"; break;
  96. }
  97. if (guard != required_guard)
  98. {
  99. ts_LOGf("!!! decode_pts_dts(), Guard bits at start of %s data are %x, not %x\n", what, guard, required_guard);
  100. }
  101. pts1 = (data0 & 0x0E) >> 1;
  102. marker = data0 & 0x01;
  103. if (marker != 1)
  104. {
  105. ts_LOGf("!!! decode_pts_dts(), First %s marker is not 1\n",what);
  106. return 0;
  107. }
  108. pts2 = (data1 << 7) | ((data2 & 0xFE) >> 1);
  109. marker = data2 & 0x01;
  110. if (marker != 1)
  111. {
  112. ts_LOGf("!!! decode_pts_dts(), Second %s marker is not 1\n",what);
  113. return 0;
  114. }
  115. pts3 = (data3 << 7) | ((data4 & 0xFE) >> 1);
  116. marker = data4 & 0x01;
  117. if (marker != 1)
  118. {
  119. ts_LOGf("!!! decode_pts_dts(), Third %s marker is not 1\n",what);
  120. return 0;
  121. }
  122. *value = (pts1 << 30) | (pts2 << 15) | pts3;
  123. return 1;
  124. }
  125. /*
  126. * Encode a PTS or DTS.
  127. *
  128. * - `data` is the array of 5 bytes into which to encode the PTS/DTS
  129. * - `guard_bits` are the required guard bits: 2 for a PTS alone, 3 for
  130. * a PTS before a DTS, or 1 for a DTS after a PTS
  131. * - `value` is the PTS or DTS value to be encoded
  132. */
  133. void ts_encode_pts_dts(uint8_t *data, int guard_bits, uint64_t value) {
  134. int pts1,pts2,pts3;
  135. #define MAX_PTS_VALUE 0x1FFFFFFFFLL
  136. if (value > MAX_PTS_VALUE)
  137. {
  138. char *what;
  139. uint64_t temp = value;
  140. while (temp > MAX_PTS_VALUE)
  141. temp -= MAX_PTS_VALUE;
  142. switch (guard_bits)
  143. {
  144. case 2: what = "PTS alone"; break;
  145. case 3: what = "PTS before DTS"; break;
  146. case 1: what = "DTS after PTS"; break;
  147. default: what = "PTS/DTS/???"; break;
  148. }
  149. ts_LOGf("!!! value %llu for %s is more than %llu - reduced to %llu\n",value,what,MAX_PTS_VALUE,temp);
  150. value = temp;
  151. }
  152. pts1 = (int)((value >> 30) & 0x07);
  153. pts2 = (int)((value >> 15) & 0x7FFF);
  154. pts3 = (int)( value & 0x7FFF);
  155. data[0] = (guard_bits << 4) | (pts1 << 1) | 0x01;
  156. data[1] = (pts2 & 0x7F80) >> 7;
  157. data[2] = ((pts2 & 0x007F) << 1) | 0x01;
  158. data[3] = (pts3 & 0x7F80) >> 7;
  159. data[4] = ((pts3 & 0x007F) << 1) | 0x01;
  160. }
  161. // Return 0 on failure
  162. // Return payload_ofs on success
  163. int ts_packet_has_pes(uint8_t *ts_packet, uint16_t *pes_packet_len) {
  164. uint8_t payload_ofs;
  165. uint8_t *payload;
  166. if (!ts_packet_is_pusi(ts_packet))
  167. goto ERR;
  168. payload_ofs = ts_packet_get_payload_offset(ts_packet);
  169. if (!payload_ofs)
  170. goto ERR;
  171. if (payload_ofs + 6 + 2 >= 188) // 6 bytes pes header, 2 bytes pes flags
  172. goto ERR;
  173. payload = ts_packet + payload_ofs;
  174. if (payload[0] == 0x00 && payload[1] == 0x00 && payload[2] == 0x01) { // pes_start_code_prefix
  175. uint8_t stream_id = payload[3];
  176. if (pes_packet_len)
  177. *pes_packet_len = (payload[4] << 8) | payload[5];
  178. // We do not handle this streams...
  179. if (!IS_PES_STREAM_SUPPORTED(stream_id))
  180. goto ERR;
  181. return payload_ofs;
  182. }
  183. ERR:
  184. return 0;
  185. }
  186. int ts_packet_has_pts_dts(uint8_t *ts_packet, uint64_t *pts, uint64_t *dts) {
  187. *pts = NO_PTS;
  188. *dts = NO_DTS;
  189. uint8_t payload_ofs = ts_packet_has_pes(ts_packet, NULL);
  190. if (!payload_ofs)
  191. goto ERR;
  192. uint8_t *data = ts_packet + payload_ofs;
  193. uint8_t *data_end = ts_packet + 188;
  194. if ((data[6] &~ 0x3f) != 0x80) // 10xxxxxx (first two bits must be 10 eq 0x80
  195. goto ERR;
  196. if (data + 7 >= data_end) goto ERR;
  197. uint8_t pts_flag = bit_on(data[7], bit_8); // PES flags 2
  198. uint8_t dts_flag = bit_on(data[7], bit_7); // PES flags 2
  199. if (!pts_flag && dts_flag) // Invalid, can't have only DTS flag
  200. return 0;
  201. if (pts_flag && !dts_flag) {
  202. if (data + 14 >= data_end) goto ERR;
  203. if (!ts_decode_pts_dts(&data[9], 2, pts))
  204. goto ERR;
  205. } else if (pts_flag && dts_flag) {
  206. if (data + 19 >= data_end) goto ERR;
  207. if (!ts_decode_pts_dts(&data[9], 3, pts))
  208. goto ERR;
  209. if (!ts_decode_pts_dts(&data[14], 1, dts))
  210. goto ERR;
  211. }
  212. return 1;
  213. ERR:
  214. return 0;
  215. }
  216. void ts_packet_change_pts(uint8_t *ts_packet, uint64_t pts) {
  217. uint8_t payload_offset = ts_packet_get_payload_offset(ts_packet);
  218. if (!payload_offset)
  219. return;
  220. uint8_t *data = ts_packet + payload_offset;
  221. ts_encode_pts_dts(&data[9], 2, pts);
  222. }
  223. void ts_packet_change_pts_dts(uint8_t *ts_packet, uint64_t pts, uint64_t dts) {
  224. uint8_t payload_offset = ts_packet_get_payload_offset(ts_packet);
  225. if (!payload_offset)
  226. return;
  227. uint8_t *data = ts_packet + payload_offset;
  228. ts_encode_pts_dts(&data[9], 3, pts);
  229. ts_encode_pts_dts(&data[14], 1, dts);
  230. }
  231. int ts_packet_has_pcr(uint8_t *ts_packet) {
  232. if (ts_packet[0] == 0x47) { // TS packet
  233. if (bit_on(ts_packet[3], bit_6)) { // Packet have adaptation field
  234. if (ts_packet[4] > 6) { // Adaptation field length is > 6
  235. if (bit_on(ts_packet[5], bit_5)) { // The is PCR field
  236. return 1;
  237. } else {
  238. // ts_LOGf("!no PCR field\n");
  239. }
  240. } else {
  241. // ts_LOGf("!not enough adaptation len (%d), need at least 7\n", ts_packet[4]);
  242. }
  243. } else {
  244. // ts_LOGf("!no adaptation field\n");
  245. }
  246. } else {
  247. // ts_LOGf("!no ts packet start (0x%02x) need 0x47\n", ts_packet[0]);
  248. }
  249. return 0;
  250. }
  251. uint64_t ts_packet_get_pcr_ex(uint8_t *ts_packet, uint64_t *pcr_base, uint16_t *pcr_ext) {
  252. uint8_t *data = ts_packet + 6; // Offset in TS packet
  253. *pcr_base = (uint64_t)data[0] << 25;
  254. *pcr_base += (uint64_t)data[1] << 17;
  255. *pcr_base += (uint64_t)data[2] << 9;
  256. *pcr_base += (uint64_t)data[3] << 1;
  257. *pcr_base += (uint64_t)data[4] >> 7;
  258. *pcr_ext = ((uint16_t)data[4] & 0x01) << 8;
  259. *pcr_ext += (uint16_t)data[5];
  260. //ts_LOGf("get pcr_base=%10llu pcr_ext=%4u pcr=%llu\n", *pcr_base, *pcr_ext, *pcr_base * 300ll + *pcr_ext);
  261. return *pcr_base * 300ll + *pcr_ext;
  262. }
  263. uint64_t ts_packet_get_pcr(uint8_t *ts_packet) {
  264. uint64_t pcr_base;
  265. uint16_t pcr_ext;
  266. return ts_packet_get_pcr_ex(ts_packet, &pcr_base, &pcr_ext);
  267. }
  268. void ts_packet_set_pcr_ex(uint8_t *ts_packet, uint64_t pcr_base, uint16_t pcr_ext) {
  269. //ts_LOGf("set pcr_base=%10llu pcr_ext=%4u pcr=%llu\n", pcr_base, pcr_ext, pcr);
  270. // 6 is the PCR offset in ts_packet (4 bytes header, 1 byte adapt field len)
  271. ts_packet[6 + 0] = (pcr_base >> 25) & 0xFF;
  272. ts_packet[6 + 1] = (pcr_base >> 17) & 0xFF;
  273. ts_packet[6 + 2] = (pcr_base >> 9) & 0xFF;
  274. ts_packet[6 + 3] = (pcr_base >> 1) & 0xFF;
  275. ts_packet[6 + 4] = 0x7e | ((pcr_ext >> 8) & 0x01) | ((pcr_base & 0x01) <<7 ); // 0x7e == 6 reserved bits
  276. ts_packet[6 + 5] = pcr_ext & 0xFF;
  277. }
  278. void ts_packet_set_pcr(uint8_t *ts_packet, uint64_t pcr) {
  279. uint64_t pcr_base = pcr / 300;
  280. uint16_t pcr_ext = pcr % 300;
  281. ts_packet_set_pcr_ex(ts_packet, pcr_base, pcr_ext);
  282. }
  283. uint8_t *ts_packet_header_parse(uint8_t *ts_packet, struct ts_header *ts_header) {
  284. if (ts_packet[0] != 0x47) {
  285. // ts_LOGf("*** TS packet do not start with sync byte 0x47 but with 0x%02x\n", ts_packet[0]);
  286. goto return_error;
  287. }
  288. ts_header->tei = (ts_packet[1] &~ 0x7f) >> 7; // x1111111
  289. ts_header->pusi = (ts_packet[1] &~ 0xbf) >> 6; // 1x111111
  290. ts_header->prio = (ts_packet[1] &~ 0xdf) >> 5; // 11x11111
  291. ts_header->pid = (ts_packet[1] &~ 0xE0) << 8 | ts_packet[2]; // 111xxxxx xxxxxxxx
  292. ts_header->scramble = (ts_packet[3] &~ 0x3F) >> 6; // xx111111
  293. ts_header->adapt_field = (ts_packet[3] &~ 0xDF) >> 5; // 11x11111
  294. ts_header->payload_field = (ts_packet[3] &~ 0xEF) >> 4; // 111x1111
  295. ts_header->continuity = (ts_packet[3] &~ 0xF0); // 1111xxxx
  296. if (!ts_header->adapt_field) {
  297. ts_header->adapt_len = 0;
  298. ts_header->adapt_flags = 0;
  299. ts_header->payload_offset = 4;
  300. } else {
  301. ts_header->adapt_len = ts_packet[4];
  302. if (ts_header->adapt_len) {
  303. ts_header->adapt_flags = ts_packet[5];
  304. }
  305. ts_header->payload_offset = 5 + ts_header->adapt_len; // 2 bytes see above
  306. }
  307. if (!ts_header->adapt_field && !ts_header->payload_field) // Not allowed
  308. goto return_error;
  309. if (!ts_header->payload_field)
  310. return NULL;
  311. if (ts_header->payload_field && ts_header->adapt_len > 182) // Validity checks
  312. goto return_error;
  313. if (!ts_header->payload_field && ts_header->adapt_len > 183)
  314. goto return_error;
  315. if (ts_header->payload_offset > TS_MAX_PAYLOAD_SIZE) // Validity check
  316. goto return_error;
  317. ts_header->payload_size = TS_PACKET_SIZE - ts_header->payload_offset;
  318. return ts_packet + ts_header->payload_offset;
  319. return_error:
  320. memset(ts_header, 0, sizeof(struct ts_header));
  321. return NULL;
  322. }
  323. void ts_packet_header_generate(uint8_t *ts_packet, struct ts_header *ts_header) {
  324. memset(ts_packet, 0xFF, TS_PACKET_SIZE);
  325. ts_packet[0] = 0x47;
  326. ts_packet[1] = 0;
  327. ts_packet[1] = ts_header->tei << 7; // x1111111
  328. ts_packet[1] |= ts_header->pusi << 6; // 1x111111
  329. ts_packet[1] |= ts_header->prio << 5; // 11x11111
  330. ts_packet[1] |= ts_header->pid >> 8; // 111xxxxx xxxxxxxx
  331. ts_packet[2] = ts_header->pid &~ 0xff00;
  332. ts_packet[3] = 0;
  333. ts_packet[3] = ts_header->scramble << 6; // xx111111
  334. ts_packet[3] |= ts_header->adapt_field << 5; // 11x11111
  335. ts_packet[3] |= ts_header->payload_field << 4; // 111x1111
  336. ts_packet[3] |= ts_header->continuity; // 1111xxxx
  337. if (ts_header->adapt_field) {
  338. ts_packet[4] = ts_header->adapt_len;
  339. ts_packet[5] = ts_header->adapt_flags;
  340. }
  341. }
  342. void ts_packet_header_dump(struct ts_header *ts_header) {
  343. ts_LOGf("*** tei:%d pusi:%d prio:%d pid:%04x (%d) scramble:%d adapt:%d payload:%d adapt_len:%d adapt_flags:%d | pofs:%d plen:%d\n",
  344. ts_header->tei,
  345. ts_header->pusi,
  346. ts_header->prio,
  347. ts_header->pid,
  348. ts_header->pid,
  349. ts_header->scramble,
  350. ts_header->adapt_field,
  351. ts_header->payload_field,
  352. ts_header->adapt_len,
  353. ts_header->adapt_flags,
  354. ts_header->payload_offset,
  355. ts_header->payload_size
  356. );
  357. }