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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 (COPYING file) for more details.
  13. *
  14. */
  15. #include <unistd.h>
  16. #include <string.h>
  17. #include <sys/uio.h>
  18. #include "data.h"
  19. #include "csa.h"
  20. #include "tables.h"
  21. #include "util.h"
  22. static unsigned long ts_pack;
  23. static int ts_pack_shown;
  24. char *get_pid_desc(struct ts *ts, uint16_t pid) {
  25. int i;
  26. uint16_t nitpid = 0x0010, pmtpid = 0xffff, pcrpid = 0xffff;
  27. if (ts->pat->initialized) {
  28. for (i=0;i<ts->pat->programs_num;i++) {
  29. struct ts_pat_program *prg = ts->pat->programs[i];
  30. if (prg->pid) {
  31. if (prg->program == 0)
  32. nitpid = prg->pid;
  33. }
  34. }
  35. }
  36. if (ts->pmt->initialized) {
  37. pmtpid = ts->pmt->ts_header.pid;
  38. pcrpid = ts->pmt->PCR_pid;
  39. for (i=0;i<ts->pmt->streams_num;i++) {
  40. struct ts_pmt_stream *stream = ts->pmt->streams[i];
  41. if (pid == stream->pid)
  42. return h222_stream_type_desc(stream->stream_type);
  43. }
  44. }
  45. switch (pid) {
  46. case 0x0000: return "PAT"; break;
  47. case 0x0001: return "CAT"; break;
  48. case 0x0011: return "SDT"; break;
  49. case 0x0012: return "EPG"; break;
  50. case 0x0014: return "TDT/TOT"; break;
  51. }
  52. if (pid == nitpid) return "NIT";
  53. else if (pid == pmtpid) return "PMT";
  54. else if (pid == pcrpid) return "PCR";
  55. else if (pid == ts->emm_pid) return "EMM";
  56. else if (pid == ts->ecm_pid) return "ECM";
  57. return "Unknown";
  58. }
  59. void show_ts_pack(struct ts *ts, uint16_t pid, char *wtf, char *extra, uint8_t *ts_packet) {
  60. char pdump[188 * 6];
  61. char cw1_dump[8 * 6];
  62. char cw2_dump[8 * 6];
  63. if (ts->debug_level >= 4) {
  64. if (ts_pack_shown)
  65. return;
  66. if (ts->debug_level >= 5)
  67. ts_hex_dump_buf(pdump, 188 * 6, ts_packet, 188, 0);
  68. int stype = ts_packet_get_scrambled(ts_packet);
  69. ts_hex_dump_buf(cw1_dump, 8 * 6, ts->key.cw , 8, 0);
  70. ts_hex_dump_buf(cw2_dump, 8 * 6, ts->key.cw + 8, 8, 0);
  71. fprintf(stderr, "@ %s %s %03x %5ld %7ld | %s %s | %s %s\n",
  72. stype == 0 ? "------" :
  73. stype == 2 ? "even 0" :
  74. stype == 3 ? "odd 1" : "??????",
  75. wtf,
  76. pid,
  77. ts_pack, ts_pack * 188,
  78. cw1_dump, cw2_dump, extra ? extra : wtf,
  79. ts->debug_level >= 5 ? pdump : "");
  80. }
  81. }
  82. static void dump_ts_pack(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  83. if (pid == 0x010) show_ts_pack(ts, pid, "nit", NULL, ts_packet);
  84. else if (pid == 0x11) show_ts_pack(ts, pid, "sdt", NULL, ts_packet);
  85. else if (pid == 0x12) show_ts_pack(ts, pid, "epg", NULL, ts_packet);
  86. else show_ts_pack(ts, pid, "---", NULL, ts_packet);
  87. }
  88. static void decode_packet(struct ts *ts, uint8_t *ts_packet) {
  89. int scramble_idx = ts_packet_get_scrambled(ts_packet);
  90. if (scramble_idx > 1) {
  91. if (ts->key.is_valid_cw) {
  92. csa_decrypt_single_packet(ts->key.csakey, ts_packet);
  93. } else {
  94. // Can't decrypt the packet just make it NULL packet
  95. if (ts->pid_filter)
  96. ts_packet_set_pid(ts_packet, 0x1fff);
  97. }
  98. }
  99. }
  100. static void decode_buffer(struct ts *ts, uint8_t *data, int data_len) {
  101. int i;
  102. int batch_sz = csa_get_batch_size(); // Tested with 32 for libdvbcsa, 70 for FFdecsa (must be multiplied by 2)
  103. int even_packets = 0;
  104. int odd_packets = 0;
  105. struct csa_batch even_pcks[batch_sz + 1];
  106. struct csa_batch odd_pcks [batch_sz + 1];
  107. uint8_t *ff_even_pcks[batch_sz * 2 + 1];
  108. uint8_t *ff_odd_pcks [batch_sz * 2 + 1];
  109. int scramble_idx_old = 0;
  110. // Prepare batch structure
  111. for (i = 0; i < batch_sz; i++) {
  112. uint8_t *ts_packet = data + (i * 188);
  113. uint16_t pid = ts_packet_get_pid(ts_packet);
  114. if (pidmap_get(&ts->pidmap, pid) && ts_packet_is_scrambled(ts_packet)) {
  115. if (ts->key.is_valid_cw) {
  116. int scramble_idx = ts_packet_get_scrambled(ts_packet);
  117. if (!scramble_idx_old)
  118. scramble_idx_old = scramble_idx;
  119. if (use_dvbcsa) {
  120. uint8_t payload_ofs = ts_packet_get_payload_offset(ts_packet);
  121. if (scramble_idx == 2) { // scramble_idx 2 == even key
  122. even_pcks[even_packets].data = ts_packet + payload_ofs;
  123. even_pcks[even_packets].len = 188 - payload_ofs;
  124. even_packets++;
  125. }
  126. if (scramble_idx == 3) { // scramble_idx 3 == odd key
  127. odd_pcks[odd_packets].data = ts_packet + payload_ofs;
  128. odd_pcks[odd_packets].len = 188 - payload_ofs;
  129. odd_packets++;
  130. }
  131. ts_packet_set_not_scrambled(ts_packet);
  132. }
  133. if (use_ffdecsa) {
  134. if (scramble_idx == 2) { // scramble_idx 2 == even key
  135. ff_even_pcks[even_packets * 2 ] = ts_packet;
  136. ff_even_pcks[even_packets * 2 + 1] = ts_packet + 188;
  137. even_packets++;
  138. }
  139. if (scramble_idx == 3) { // scramble_idx 3 == odd key
  140. ff_odd_pcks[odd_packets * 2 ] = ts_packet;
  141. ff_odd_pcks[odd_packets * 2 + 1] = ts_packet + 188;
  142. odd_packets++;
  143. }
  144. }
  145. if (scramble_idx_old != scramble_idx && !ts->camd.constant_codeword) {
  146. struct timeval tv;
  147. gettimeofday(&tv, NULL);
  148. ts_LOGf("CWC | SID 0x%04x ------------ EcmTime: %5llu ms CW_time: %5llu ms\n",
  149. ts->service_id,
  150. timeval_diff_msec(&ts->ecm_change_time, &tv),
  151. timeval_diff_msec(&ts->key.ts_keyset, &tv));
  152. }
  153. scramble_idx_old = scramble_idx;
  154. } else {
  155. if (ts->pid_filter)
  156. ts_packet_set_pid(ts_packet, 0x1fff);
  157. }
  158. }
  159. }
  160. // Decode packets
  161. if (even_packets) {
  162. if (use_dvbcsa) {
  163. even_pcks[even_packets].data = NULL; // Last one...
  164. csa_decrypt_multiple_even(ts->key.csakey, even_pcks);
  165. }
  166. if (use_ffdecsa) {
  167. ff_even_pcks[even_packets * 2] = NULL;
  168. csa_decrypt_multiple_ff(ts->key.csakey, ff_even_pcks);
  169. }
  170. }
  171. if (odd_packets) {
  172. if (use_dvbcsa) {
  173. odd_pcks[odd_packets].data = NULL; // Last one...
  174. csa_decrypt_multiple_odd(ts->key.csakey, odd_pcks);
  175. }
  176. if (use_ffdecsa) {
  177. ff_odd_pcks[odd_packets * 2] = NULL;
  178. csa_decrypt_multiple_ff(ts->key.csakey, ff_odd_pcks);
  179. }
  180. }
  181. // Fill write buffer
  182. for (i=0; i<data_len; i += 188) {
  183. uint8_t *ts_packet = data + i;
  184. if (!ts->pid_filter) {
  185. cbuf_fill(ts->write_buf, ts_packet, 188);
  186. } else {
  187. uint16_t pid = ts_packet_get_pid(ts_packet);
  188. if (pidmap_get(&ts->pidmap, pid)) // PAT or allowed PIDs
  189. cbuf_fill(ts->write_buf, ts_packet, 188);
  190. }
  191. }
  192. }
  193. void *decode_thread(void *_ts) {
  194. struct ts *ts = _ts;
  195. uint8_t *data;
  196. int data_size;
  197. int req_size = 188 * csa_get_batch_size();
  198. set_thread_name("tsdec-decode");
  199. while (!ts->decode_stop) {
  200. data = cbuf_peek(ts->decode_buf, req_size, &data_size);
  201. if (data_size < req_size) {
  202. usleep(1000);
  203. continue;
  204. }
  205. data = cbuf_get(ts->decode_buf, req_size, &data_size);
  206. if (data)
  207. decode_buffer(ts, data, data_size);
  208. }
  209. do { // Flush data
  210. data = cbuf_get(ts->decode_buf, req_size, &data_size);
  211. if (data)
  212. decode_buffer(ts, data, data_size);
  213. } while(data);
  214. return NULL;
  215. }
  216. static inline void output_write(struct ts *ts, uint8_t *data, unsigned int data_size) {
  217. if (!data)
  218. return;
  219. if (ts->no_output_on_error && !ts->camd.key->is_valid_cw)
  220. return;
  221. if (!ts->rtp_output) {
  222. write(ts->output.fd, data, data_size);
  223. } else {
  224. struct iovec iov[2];
  225. uint8_t rtp_header[12];
  226. uint32_t rtime = get_time() * 9 / 100;
  227. ts->rtp_seqnum++;
  228. rtp_header[ 0] = 0x80;
  229. rtp_header[ 1] = 33; // MPEG TS rtp payload type
  230. rtp_header[ 2] = ts->rtp_seqnum >> 8;
  231. rtp_header[ 3] = ts->rtp_seqnum & 0xff;
  232. rtp_header[ 4] = (rtime >> 24) & 0xff;
  233. rtp_header[ 5] = (rtime >> 16) & 0xff;
  234. rtp_header[ 6] = (rtime >> 8) & 0xff;
  235. rtp_header[ 7] = rtime & 0xff;
  236. rtp_header[ 8] = (ts->rtp_ssrc >> 24) & 0xff;
  237. rtp_header[ 9] = (ts->rtp_ssrc >> 16) & 0xff;
  238. rtp_header[10] = (ts->rtp_ssrc >> 8) & 0xff;
  239. rtp_header[11] = ts->rtp_ssrc & 0xff;
  240. iov[0].iov_base = rtp_header;
  241. iov[0].iov_len = sizeof(rtp_header);
  242. iov[1].iov_base = data;
  243. iov[1].iov_len = data_size;
  244. writev(ts->output.fd, iov, 2);
  245. }
  246. }
  247. void *write_thread(void *_ts) {
  248. struct ts *ts = _ts;
  249. uint8_t *data;
  250. int data_size;
  251. set_thread_name("tsdec-write");
  252. while (!ts->write_stop) {
  253. data_size = 0;
  254. data = cbuf_peek(ts->write_buf, FRAME_SIZE, &data_size);
  255. if (data_size < FRAME_SIZE) {
  256. usleep(5000);
  257. continue;
  258. }
  259. data = cbuf_get (ts->write_buf, FRAME_SIZE, &data_size);
  260. output_write(ts, data, data_size);
  261. }
  262. do { // Flush data
  263. data = cbuf_get(ts->write_buf, FRAME_SIZE, &data_size);
  264. output_write(ts, data, data_size);
  265. } while(data);
  266. return NULL;
  267. }
  268. static void detect_discontinuity(struct ts *ts, uint8_t *ts_packet) {
  269. uint16_t pid;
  270. uint8_t cur_cc, last_cc;
  271. if (!ts->ts_discont)
  272. return;
  273. pid = ts_packet_get_pid(ts_packet);
  274. cur_cc = ts_packet_get_cont(ts_packet);
  275. if (!pidmap_get(&ts->pid_seen, pid)) {
  276. if (strcmp(get_pid_desc(ts, pid), "Unknown") == 0)
  277. return;
  278. pidmap_set(&ts->pid_seen, pid);
  279. pidmap_set_val(&ts->cc, pid, cur_cc);
  280. ts_LOGf("NEW | Input PID 0x%04x appeared (%s)\n",
  281. pid, get_pid_desc(ts, pid));
  282. return;
  283. }
  284. last_cc = pidmap_get(&ts->cc, pid);
  285. if (last_cc != cur_cc && ((last_cc + 1) & 0x0f) != cur_cc)
  286. ts_LOGf("--- | TS discontinuity on PID 0x%04x expected %2d got %2d /%d/ (%s)\n",
  287. pid,
  288. ((last_cc + 1) & 0x0f), cur_cc,
  289. (cur_cc - ((last_cc + 1) & 0x0f)) & 0x0f,
  290. get_pid_desc(ts, pid));
  291. pidmap_set_val(&ts->cc, pid, cur_cc);
  292. }
  293. void process_packets(struct ts *ts, uint8_t *data, ssize_t data_len) {
  294. ssize_t i;
  295. int64_t now = get_time();
  296. for (i=0; i<data_len; i += 188) {
  297. uint8_t *ts_packet = data + i;
  298. uint16_t pid = ts_packet_get_pid(ts_packet);
  299. if (ts->pid_report)
  300. ts->pid_stats[pid]++;
  301. ts_pack_shown = 0;
  302. process_pat(ts, pid, ts_packet);
  303. process_cat(ts, pid, ts_packet);
  304. process_pmt(ts, pid, ts_packet);
  305. process_sdt(ts, pid, ts_packet);
  306. process_emm(ts, pid, ts_packet);
  307. process_ecm(ts, pid, ts_packet);
  308. detect_discontinuity(ts, ts_packet);
  309. if (!ts_pack_shown)
  310. dump_ts_pack(ts, pid, ts_packet);
  311. if (ts->emm_only)
  312. continue;
  313. // Return rewritten PAT
  314. if (pid == 0x00 && ts->pid_filter && ts->genpat->initialized) {
  315. if (!ts_packet_is_pusi(ts_packet))
  316. continue;
  317. ts_packet_set_cont(ts->genpat->section_header->packet_data, ts->genpat_cc);
  318. ts->genpat->ts_header.continuity = ts->genpat_cc;
  319. ts_packet = ts->genpat->section_header->packet_data;
  320. ts->genpat_cc = (ts->genpat_cc + 1) & 0x0f;
  321. }
  322. if (ts->threaded) {
  323. // Add to decode buffer. The decoder thread will handle it
  324. if (ts->input_buffer_time == 0) {
  325. // No input buffer, move packets to decoding buffer
  326. if (cbuf_fill(ts->decode_buf, ts_packet, 188) != 0) {
  327. ts_LOGf("Decode buffer is full, waiting...\n");
  328. cbuf_dump(ts->decode_buf);
  329. usleep(10000);
  330. }
  331. } else {
  332. // Handle input buffer
  333. struct packet_buf *p = malloc(sizeof(struct packet_buf));
  334. p->time = now + (ts->input_buffer_time * 1000); //buffer time is in ms, p->time is in us
  335. memcpy(p->data, ts_packet, 188);
  336. list_add(ts->input_buffer, p);
  337. // Move packets to decrypt buffer
  338. LNODE *lc, *lctmp;
  339. list_for_each(ts->input_buffer, lc, lctmp) {
  340. p = lc->data;
  341. if (p->time <= now) {
  342. if (cbuf_fill(ts->decode_buf, p->data, 188) != 0) {
  343. ts_LOGf("Decode buffer is full, waiting...\n");
  344. cbuf_dump(ts->decode_buf);
  345. usleep(10000);
  346. }
  347. list_del(ts->input_buffer, &lc);
  348. free(p);
  349. } else {
  350. break;
  351. }
  352. }
  353. }
  354. } else {
  355. int allowed_pid = pidmap_get(&ts->pidmap, pid);
  356. if (allowed_pid) // PAT or allowed PIDs
  357. decode_packet(ts, ts_packet);
  358. if (ts->pid_filter) {
  359. if (allowed_pid) // PAT or allowed PIDs
  360. output_write(ts, ts_packet, 188);
  361. } else {
  362. output_write(ts, ts_packet, 188);
  363. }
  364. }
  365. ts_pack++;
  366. }
  367. }
  368. void show_pid_report(struct ts *ts) {
  369. int i;
  370. if (!ts->pid_report)
  371. return;
  372. for (i = 0; i < MAX_PIDS; i++) {
  373. if (ts->pid_stats[i]) {
  374. ts_LOGf("PID | %8u packets with PID 0x%04x (%4u) %s\n",
  375. ts->pid_stats[i], i, i, get_pid_desc(ts, i));
  376. }
  377. }
  378. }