]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
Maximize use of libdvbcsa's batch processing.
authorjoeuser <joeusercz@goglemail.co>
Wed, 5 Dec 2018 21:25:29 +0000 (22:25 +0100)
committerJaroslav Kysela <perex@perex.cz>
Thu, 6 Dec 2018 15:07:22 +0000 (16:07 +0100)
src/descrambler/descrambler.c
src/descrambler/tvhcsa.c
src/descrambler/tvhcsa.h
src/webui/static/app/tvheadend.js

index 33034c123d42063f093b63aa775c98efcd8c56a7..63b70b2051b38d86aa4021dfa5aedae0e01330cd 100644 (file)
@@ -689,7 +689,7 @@ descrambler_keys ( th_descrambler_t *td, int type, uint16_t pid,
     snprintf(pidname, sizeof(pidname), "[%d]", pid);
   ktype = descrambler_keytype2str(type);
 
-  if (tvhcsa_set_type(&tk->key_csa, type) < 0) {
+  if (tvhcsa_set_type(&tk->key_csa, (mpegts_service_t *)t, type) < 0) {
     if (tk->key_type_overwritten)
       goto end;
     if (type == DESCRAMBLER_CSA_CBC && tk->key_csa.csa_type == DESCRAMBLER_DES_NCB) {
@@ -706,7 +706,7 @@ descrambler_keys ( th_descrambler_t *td, int type, uint16_t pid,
             ktype, ((mpegts_service_t *)t)->s_dvb_svcname);
     tvhcsa_destroy(&tk->key_csa);
     tvhcsa_init(&tk->key_csa);
-    if (tvhcsa_set_type(&tk->key_csa, type) < 0)
+    if (tvhcsa_set_type(&tk->key_csa, (mpegts_service_t *)t, type) < 0)
       goto end;
     tk->key_valid = 0;
   }
index d2c00955559bd3ac28450a90a2575071cd3ce4a3..7c71e08abd92a8dee616998da22b4e4aa7605e8f 100644 (file)
@@ -73,6 +73,8 @@ tvhcsa_csa_cbc_flush
   ( tvhcsa_t *csa, struct mpegts_service *s )
 {
 #if ENABLE_DVBCSA
+  tvhtrace(LS_CSA, "%p: CSA flush - descramble packets for service \"%s\" MAX=%d even=%d odd=%d fill=%d",
+           csa,((mpegts_service_t *)s)->s_dvb_svcname, csa->csa_cluster_size,csa->csa_fill_even,csa->csa_fill_odd,csa->csa_fill);
 
   if(csa->csa_fill_even) {
     csa->csa_tsbbatch_even[csa->csa_fill_even].data = NULL;
@@ -100,7 +102,7 @@ tvhcsa_csa_cbc_descramble
 {
   const uint8_t *tsb_end = tsb + tsb_len;
 
-  assert(csa->csa_fill >= 0 && csa->csa_fill < csa->csa_cluster_size);
+  assert(csa->csa_fill >= 0 && csa->csa_fill < csa->csa_fill_size);
 
 #if ENABLE_DVBCSA
   uint8_t *pkt;
@@ -121,8 +123,12 @@ tvhcsa_csa_cbc_descramble
      pkt[3] &= 0x3f;           // consider it decrypted now
      if(pkt[3] & 0x20) {       // incomplete packet
        offset = 4 + pkt[4] + 1;
-       if (offset > 188-8)     // invalid offset (residue handling?)
+       if (offset > 188-8){    // invalid offset (residue handling?)
+         if (tvhlog_limit(&csa->tvhcsa_loglimit, 10))
+           tvhwarn(LS_CSA, "invalid payload offset in packet for service \"%s\" (offset=%ld pkt[3]=0x%02x pkt[4]=0x%02x)",
+                           ((mpegts_service_t *)s)->s_dvb_svcname, offset, pkt[3], pkt[4]);
          break;                        // no more processing
+       }
        len = 188 - offset;
      } else {
        len = 184;
@@ -132,14 +138,18 @@ tvhcsa_csa_cbc_descramble
        csa->csa_tsbbatch_even[csa->csa_fill_even].data = pkt + offset;
        csa->csa_tsbbatch_even[csa->csa_fill_even].len = len;
        csa->csa_fill_even++;
+       if(csa->csa_fill_even == csa->csa_cluster_size)
+         tvhcsa_csa_cbc_flush(csa, s);
      } else {
        csa->csa_tsbbatch_odd[csa->csa_fill_odd].data = pkt + offset;
        csa->csa_tsbbatch_odd[csa->csa_fill_odd].len = len;
        csa->csa_fill_odd++;
+       if(csa->csa_fill_odd == csa->csa_cluster_size)
+         tvhcsa_csa_cbc_flush(csa, s);
      }
    } while(0);
 
-   if(csa->csa_fill == csa->csa_cluster_size)
+   if(csa->csa_fill == csa->csa_fill_size )
      tvhcsa_csa_cbc_flush(csa, s);
 
   }
@@ -150,7 +160,7 @@ tvhcsa_csa_cbc_descramble
 }
 
 int
-tvhcsa_set_type( tvhcsa_t *csa, int type )
+tvhcsa_set_type( tvhcsa_t *csa, struct mpegts_service *s, int type )
 {
   if (csa->csa_type == type)
     return 0;
@@ -166,10 +176,11 @@ tvhcsa_set_type( tvhcsa_t *csa, int type )
 #else
     csa->csa_cluster_size  = 0;
 #endif
-    /* Note: the optimized routines might read memory after last TS packet */
-    /*       allocate safe memory and fill it with zeros */
-    csa->csa_tsbcluster    = malloc((csa->csa_cluster_size + 1) * 188);
-    memset(csa->csa_tsbcluster + csa->csa_cluster_size * 188, 0, 188);
+    csa->csa_fill_size  = 3 * csa->csa_cluster_size;
+    tvhtrace(LS_CSA, "%p: service \"%s\" using CSA batch size = %d for decryption",
+             csa, ((mpegts_service_t *)s)->s_dvb_svcname, csa->csa_cluster_size );
+
+    csa->csa_tsbcluster    = malloc(csa->csa_fill_size * 188);
 #if ENABLE_DVBCSA
     csa->csa_tsbbatch_even = malloc((csa->csa_cluster_size + 1) *
                                     sizeof(struct dvbcsa_bs_batch_s));
index 7f85764ba47ced9255467332813e9c5819fc367b..855de5acc2b40ae04c29591fc2571603fc5899f9 100644 (file)
@@ -27,6 +27,7 @@ struct elementary_stream;
 #if ENABLE_DVBCSA
 #include <dvbcsa/dvbcsa.h>
 #endif
+#include "tvhlog.h"
 
 typedef struct tvhcsa
 {
@@ -45,6 +46,7 @@ typedef struct tvhcsa
   int      csa_cluster_size;
   uint8_t *csa_tsbcluster;
   int      csa_fill;
+  int      csa_fill_size;
 
 #if ENABLE_DVBCSA
   struct dvbcsa_bs_batch_s *csa_tsbbatch_even;
@@ -56,12 +58,13 @@ typedef struct tvhcsa
   struct dvbcsa_bs_key_s *csa_key_odd;
 #endif
   void *csa_priv;
+  tvhlog_limit_t tvhcsa_loglimit;
 
 } tvhcsa_t;
 
 #if ENABLE_TVHCSA
 
-int  tvhcsa_set_type( tvhcsa_t *csa, int type );
+int  tvhcsa_set_type( tvhcsa_t *csa, struct mpegts_service *s, int type );
 
 void tvhcsa_set_key_even( tvhcsa_t *csa, const uint8_t *even );
 void tvhcsa_set_key_odd ( tvhcsa_t *csa, const uint8_t *odd );
@@ -71,7 +74,7 @@ void tvhcsa_destroy ( tvhcsa_t *csa );
 
 #else
 
-static inline int tvhcsa_set_type( tvhcsa_t *csa, int type ) { return -1; }
+static inline int tvhcsa_set_type( tvhcsa_t *csa, struct mpegts_service *s, int type ) { return -1; }
 
 static inline void tvhcsa_set_key_even( tvhcsa_t *csa, const uint8_t *even ) { };
 static inline void tvhcsa_set_key_odd ( tvhcsa_t *csa, const uint8_t *odd ) { };
index a7ec8c77f84251708c6bc18372806c09d30dc35e..2893622042ec289ec6dad57c18ac9de0cbb5e0e1 100644 (file)
@@ -1165,7 +1165,7 @@ function accessUpdate(o) {
 
         cp.add(tsdvr);
 
-        /* CSA */
+        /* CAs */
         if (tvheadend.capabilities.indexOf('caclient') !== -1)
             tvheadend.caclient(cp, 6);