tsdecrypt reads and decrypts CSA encrypted incoming mpeg transport stream over UDP/RTP using code words obtained from OSCAM or similar CAM server. tsdecrypt communicates with CAM server using cs378x (camd35 over tcp) protocol or newcamd protocol. https://georgi.unixsol.org/programs/tsdecrypt/
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.

process.c 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Process packets
  3. * Copyright (C) 2011 Unix Solutions Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  17. */
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include "data.h"
  21. #include "tables.h"
  22. #include "util.h"
  23. static unsigned long ts_pack;
  24. static int ts_pack_shown;
  25. static char *get_pid_desc(struct ts *ts, uint16_t pid) {
  26. int i;
  27. uint16_t nitpid = 0x0010, pmtpid = 0xffff, pcrpid = 0xffff;
  28. if (ts->pat->initialized) {
  29. for (i=0;i<ts->pat->programs_num;i++) {
  30. struct ts_pat_program *prg = ts->pat->programs[i];
  31. if (prg->pid) {
  32. if (prg->program == 0)
  33. nitpid = prg->pid;
  34. }
  35. }
  36. }
  37. if (ts->pmt->initialized) {
  38. pmtpid = ts->pmt->ts_header.pid;
  39. pcrpid = ts->pmt->PCR_pid;
  40. for (i=0;i<ts->pmt->streams_num;i++) {
  41. struct ts_pmt_stream *stream = ts->pmt->streams[i];
  42. if (pid == stream->pid)
  43. return h222_stream_type_desc(stream->stream_type);
  44. }
  45. }
  46. switch (pid) {
  47. case 0x0000: return "PAT"; break;
  48. case 0x0001: return "CAT"; break;
  49. case 0x0011: return "SDT"; break;
  50. case 0x0012: return "EPG"; break;
  51. case 0x0014: return "TDT/TOT"; break;
  52. }
  53. if (pid == nitpid) return "NIT";
  54. else if (pid == pmtpid) return "PMT";
  55. else if (pid == pcrpid) return "PCR";
  56. else if (pid == ts->emm_pid) return "EMM";
  57. else if (pid == ts->ecm_pid) return "ECM";
  58. return "Unknown";
  59. }
  60. void show_ts_pack(struct ts *ts, uint16_t pid, char *wtf, char *extra, uint8_t *ts_packet) {
  61. char pdump[188 * 6];
  62. char cw1_dump[8 * 6];
  63. char cw2_dump[8 * 6];
  64. if (ts->debug_level >= 4) {
  65. if (ts_pack_shown)
  66. return;
  67. if (ts->debug_level >= 5)
  68. ts_hex_dump_buf(pdump, 188 * 6, ts_packet, 188, 0);
  69. int stype = ts_packet_get_scrambled(ts_packet);
  70. ts_hex_dump_buf(cw1_dump, 8 * 6, ts->key.cw , 8, 0);
  71. ts_hex_dump_buf(cw2_dump, 8 * 6, ts->key.cw + 8, 8, 0);
  72. fprintf(stderr, "@ %s %s %03x %5ld %7ld | %s %s | %s %s\n",
  73. stype == 0 ? "------" :
  74. stype == 2 ? "even 0" :
  75. stype == 3 ? "odd 1" : "??????",
  76. wtf,
  77. pid,
  78. ts_pack, ts_pack * 188,
  79. cw1_dump, cw2_dump, extra ? extra : wtf,
  80. ts->debug_level >= 5 ? pdump : "");
  81. }
  82. }
  83. static void dump_ts_pack(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  84. if (pid == 0x010) show_ts_pack(ts, pid, "nit", NULL, ts_packet);
  85. else if (pid == 0x11) show_ts_pack(ts, pid, "sdt", NULL, ts_packet);
  86. else if (pid == 0x12) show_ts_pack(ts, pid, "epg", NULL, ts_packet);
  87. else show_ts_pack(ts, pid, "---", NULL, ts_packet);
  88. }
  89. static void decode_packet(struct ts *ts, uint8_t *ts_packet) {
  90. int scramble_idx = ts_packet_get_scrambled(ts_packet);
  91. if (scramble_idx > 1) {
  92. if (ts->key.is_valid_cw) {
  93. // scramble_idx 2 == even key
  94. // scramble_idx 3 == odd key
  95. ts_packet_set_not_scrambled(ts_packet);
  96. uint8_t payload_ofs = ts_packet_get_payload_offset(ts_packet);
  97. dvbcsa_decrypt(ts->key.csakey[scramble_idx - 2], ts_packet + payload_ofs, 188 - payload_ofs);
  98. } else {
  99. // Can't decrypt the packet just make it NULL packet
  100. if (ts->pid_filter)
  101. ts_packet_set_pid(ts_packet, 0x1fff);
  102. }
  103. }
  104. }
  105. static void decode_buffer(struct ts *ts, uint8_t *data, int data_len) {
  106. int i;
  107. int batch_sz = dvbcsa_bs_batch_size(); // 32?
  108. int even_packets = 0;
  109. int odd_packets = 0;
  110. struct dvbcsa_bs_batch_s even_pcks[batch_sz + 1];
  111. struct dvbcsa_bs_batch_s odd_pcks [batch_sz + 1];
  112. // Prepare batch structure
  113. for (i = 0; i < batch_sz; i++) {
  114. uint8_t *ts_packet = data + (i * 188);
  115. int scramble_idx = ts_packet_get_scrambled(ts_packet);
  116. if (scramble_idx > 1) {
  117. if (ts->key.is_valid_cw) {
  118. uint8_t payload_ofs = ts_packet_get_payload_offset(ts_packet);
  119. if (scramble_idx == 2) { // scramble_idx 2 == even key
  120. even_pcks[even_packets].data = ts_packet + payload_ofs;
  121. even_pcks[even_packets].len = 188 - payload_ofs;
  122. even_packets++;
  123. }
  124. if (scramble_idx == 3) { // scramble_idx 3 == odd key
  125. odd_pcks[odd_packets].data = ts_packet + payload_ofs;
  126. odd_pcks[odd_packets].len = 188 - payload_ofs;
  127. odd_packets++;
  128. }
  129. ts_packet_set_not_scrambled(ts_packet);
  130. } else {
  131. if (ts->pid_filter)
  132. ts_packet_set_pid(ts_packet, 0x1fff);
  133. }
  134. }
  135. }
  136. // Decode packets
  137. if (even_packets) {
  138. even_pcks[even_packets].data = NULL; // Last one...
  139. dvbcsa_bs_decrypt(ts->key.bs_csakey[0], even_pcks, 184);
  140. }
  141. if (odd_packets) {
  142. odd_pcks[odd_packets].data = NULL; // Last one...
  143. dvbcsa_bs_decrypt(ts->key.bs_csakey[1], odd_pcks, 184);
  144. }
  145. // Fill write buffer
  146. for (i=0; i<data_len; i += 188) {
  147. uint8_t *ts_packet = data + i;
  148. if (!ts->pid_filter) {
  149. cbuf_fill(ts->write_buf, ts_packet, 188);
  150. } else {
  151. uint16_t pid = ts_packet_get_pid(ts_packet);
  152. if (pidmap_get(&ts->pidmap, pid)) // PAT or allowed PIDs
  153. cbuf_fill(ts->write_buf, ts_packet, 188);
  154. }
  155. }
  156. }
  157. void *decode_thread(void *_ts) {
  158. struct ts *ts = _ts;
  159. uint8_t *data;
  160. int data_size;
  161. int req_size = 188 * dvbcsa_bs_batch_size();
  162. set_thread_name("tsdec-decode");
  163. while (!ts->decode_stop) {
  164. data = cbuf_peek(ts->decode_buf, req_size, &data_size);
  165. if (data_size < req_size) {
  166. usleep(1000);
  167. continue;
  168. }
  169. data = cbuf_get(ts->decode_buf, req_size, &data_size);
  170. if (data)
  171. decode_buffer(ts, data, data_size);
  172. }
  173. do { // Flush data
  174. data = cbuf_get(ts->decode_buf, req_size, &data_size);
  175. if (data)
  176. decode_buffer(ts, data, data_size);
  177. } while(data);
  178. return NULL;
  179. }
  180. void *write_thread(void *_ts) {
  181. struct ts *ts = _ts;
  182. uint8_t *data;
  183. int data_size;
  184. set_thread_name("tsdec-write");
  185. while (!ts->write_stop) {
  186. data_size = 0;
  187. data = cbuf_peek(ts->write_buf, FRAME_SIZE, &data_size);
  188. if (data_size < FRAME_SIZE) {
  189. usleep(5000);
  190. continue;
  191. }
  192. data = cbuf_get (ts->write_buf, FRAME_SIZE, &data_size);
  193. if (data)
  194. write(ts->output.fd, data, data_size);
  195. }
  196. do { // Flush data
  197. data = cbuf_get(ts->write_buf, FRAME_SIZE, &data_size);
  198. if (data)
  199. write(ts->output.fd, data, data_size);
  200. } while(data);
  201. return NULL;
  202. }
  203. static void detect_discontinuity(struct ts *ts, uint8_t *ts_packet) {
  204. uint16_t pid;
  205. uint8_t cur_cc, last_cc;
  206. if (!ts->ts_discont)
  207. return;
  208. pid = ts_packet_get_pid(ts_packet);
  209. cur_cc = ts_packet_get_cont(ts_packet);
  210. if (!pidmap_get(&ts->pid_seen, pid)) {
  211. if (strcmp(get_pid_desc(ts, pid), "Unknown") == 0)
  212. return;
  213. pidmap_set(&ts->pid_seen, pid);
  214. pidmap_set_val(&ts->cc, pid, cur_cc);
  215. ts_LOGf("NEW | Input PID 0x%04x appeared (%s)\n",
  216. pid, get_pid_desc(ts, pid));
  217. return;
  218. }
  219. last_cc = pidmap_get(&ts->cc, pid);
  220. if (last_cc != cur_cc && ((last_cc + 1) & 0x0f) != cur_cc)
  221. ts_LOGf("--- | TS discontinuity on PID 0x%04x expected %2d got %2d /%d/ (%s)\n",
  222. pid,
  223. ((last_cc + 1) & 0x0f), cur_cc,
  224. (cur_cc - ((last_cc + 1) & 0x0f)) & 0x0f,
  225. get_pid_desc(ts, pid));
  226. pidmap_set_val(&ts->cc, pid, cur_cc);
  227. }
  228. void process_packets(struct ts *ts, uint8_t *data, ssize_t data_len) {
  229. ssize_t i;
  230. for (i=0; i<data_len; i += 188) {
  231. uint8_t *ts_packet = data + i;
  232. uint16_t pid = ts_packet_get_pid(ts_packet);
  233. ts_pack_shown = 0;
  234. process_pat(ts, pid, ts_packet);
  235. process_cat(ts, pid, ts_packet);
  236. process_pmt(ts, pid, ts_packet);
  237. process_sdt(ts, pid, ts_packet);
  238. process_emm(ts, pid, ts_packet);
  239. process_ecm(ts, pid, ts_packet);
  240. detect_discontinuity(ts, ts_packet);
  241. if (!ts_pack_shown)
  242. dump_ts_pack(ts, pid, ts_packet);
  243. if (ts->emm_only)
  244. continue;
  245. // Return rewritten PAT
  246. if (pid == 0x00 && ts->pid_filter && ts->genpat->initialized) {
  247. if (!ts_packet_is_pusi(ts_packet))
  248. continue;
  249. ts_packet_set_cont(ts->genpat->section_header->packet_data, ts->genpat_cc);
  250. ts->genpat->ts_header.continuity = ts->genpat_cc;
  251. ts_packet = ts->genpat->section_header->packet_data;
  252. ts->genpat_cc = (ts->genpat_cc + 1) & 0x0f;
  253. }
  254. if (ts->threaded) {
  255. // Add to decode buffer. The decoder thread will handle it
  256. if (cbuf_fill(ts->decode_buf, ts_packet, 188) != 0) {
  257. ts_LOGf("Decode buffer is full, waiting...\n");
  258. cbuf_dump(ts->decode_buf);
  259. usleep(10000);
  260. }
  261. } else {
  262. decode_packet(ts, ts_packet);
  263. if (ts->pid_filter) {
  264. if (pidmap_get(&ts->pidmap, pid)) // PAT or allowed PIDs
  265. write(ts->output.fd, ts_packet, 188);
  266. } else {
  267. write(ts->output.fd, ts_packet, 188);
  268. }
  269. }
  270. ts_pack++;
  271. }
  272. }