]> git.sesse.net Git - vlc/blobdiff - modules/codec/cc.h
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / codec / cc.h
index 145f3c909dc6e93db6f4bcc493058f9f62b1aadb..f582c36eb2c179c841298086ecdaed04e5bc05b7 100644 (file)
@@ -22,7 +22,9 @@
  *****************************************************************************/
 
 #ifndef _CC_H
-#define _C_H 1
+#define _CC_H 1
+
+#include <vlc_bits.h>
 
 /* CC have a maximum rate of 9600 bit/s (per field?) */
 #define CC_MAX_DATA_SIZE (2 * 3*600)
@@ -43,18 +45,6 @@ typedef struct
     uint8_t p_data[CC_MAX_DATA_SIZE];
 } cc_data_t;
 
-static inline int cc_Channel( int i_field, const uint8_t p_data[2] )
-{
-    const uint8_t d = p_data[0] & 0x7f;
-    if( i_field != 0 && i_field != 1 )
-        return -1;
-    if( d == 0x14 )
-        return 2*i_field + 0;
-    else if( d == 0x1c )
-        return 2*i_field + 1;
-    /* unknown(middle of a command)  or not cc channel */
-    return -1;
-}
 static inline void cc_Init( cc_data_t *c )
 {
     int i;
@@ -73,7 +63,21 @@ static inline void cc_Flush( cc_data_t *c )
 {
     c->i_data = 0;
 }
-static inline void cc_Extract( cc_data_t *c, const uint8_t *p_src, int i_src )
+
+static inline void cc_AppendData( cc_data_t *c, int i_field, const uint8_t cc[2] )
+{
+    if( i_field == 0 || i_field == 1 )
+    {
+        c->pb_present[2*i_field+0] =
+        c->pb_present[2*i_field+1] = true;
+    }
+
+    c->p_data[c->i_data++] = i_field;
+    c->p_data[c->i_data++] = cc[0];
+    c->p_data[c->i_data++] = cc[1];
+}
+
+static inline void cc_Extract( cc_data_t *c, bool b_top_field_first, const uint8_t *p_src, int i_src )
 {
     static const uint8_t p_cc_ga94[4] = { 0x47, 0x41, 0x39, 0x34 };
     static const uint8_t p_cc_dvd[4] = { 0x43, 0x43, 0x01, 0xf8 };
@@ -81,6 +85,8 @@ static inline void cc_Extract( cc_data_t *c, const uint8_t *p_src, int i_src )
     static const uint8_t p_cc_replaytv4b[2] = { 0xcc, 0x02 };
     static const uint8_t p_cc_replaytv5a[2] = { 0x99, 0x02 };
     static const uint8_t p_cc_replaytv5b[2] = { 0xaa, 0x02 };
+    static const uint8_t p_cc_scte20[2] = { 0x03, 0x81 };
+    static const uint8_t p_cc_scte20_old[2] = { 0x03, 0x01 };
     //static const uint8_t p_afd_start[4] = { 0x44, 0x54, 0x47, 0x31 };
 
     if( i_src < 4 )
@@ -126,7 +132,6 @@ static inline void cc_Extract( cc_data_t *c, const uint8_t *p_src, int i_src )
         for( i = 0; i < i_count_cc; i++, cc += 3 )
         {
             int i_field = cc[0] & 0x03;
-            int i_channel;
             if( ( cc[0] & 0xfc ) != 0xfc )
                 continue;
             if( i_field != 0 && i_field != 1 )
@@ -134,13 +139,7 @@ static inline void cc_Extract( cc_data_t *c, const uint8_t *p_src, int i_src )
             if( c->i_data + 3 > CC_MAX_DATA_SIZE )
                 continue;
 
-            i_channel = cc_Channel( i_field, &cc[1] );
-            if( i_channel >= 0 && i_channel < 4 )
-                c->pb_present[i_channel] = true;
-
-            c->p_data[c->i_data++] = i_field;
-            c->p_data[c->i_data++] = cc[1];
-            c->p_data[c->i_data++] = cc[2];
+            cc_AppendData( c, i_field, &cc[1] );
         }
         c->b_reorder = true;
     }
@@ -161,7 +160,6 @@ static inline void cc_Extract( cc_data_t *c, const uint8_t *p_src, int i_src )
             for( j = 0; j < 2; j++, cc += 3 )
             {
                 const int i_field = j == i_field_first ? 0 : 1;
-                int i_channel;
 
                 if( b_truncate && i == i_count_cc2 - 1 && j == 1 )
                     break;
@@ -170,12 +168,7 @@ static inline void cc_Extract( cc_data_t *c, const uint8_t *p_src, int i_src )
                 if( c->i_data + 3 > CC_MAX_DATA_SIZE )
                     continue;
 
-                i_channel = cc_Channel( i_field, &cc[1] );
-                if( i_channel >= 0 && i_channel < 4 )
-                    c->pb_present[i_channel] = true;
-                c->p_data[c->i_data++] = i_field;
-                c->p_data[c->i_data++] = cc[1];
-                c->p_data[c->i_data++] = cc[2];
+                cc_AppendData( c, i_field, &cc[1] );
             }
         }
         c->b_reorder = false;
@@ -193,22 +186,56 @@ static inline void cc_Extract( cc_data_t *c, const uint8_t *p_src, int i_src )
         for( i = 0; i < 2; i++, cc += 4 )
         {
             const int i_field = i == 0 ? 1 : 0;
-            int i_channel = cc_Channel( i_field, &cc[2] );
-            if( i_channel >= 0 && i_channel < 4 )
-                c->pb_present[i_channel] = true;
-            c->p_data[c->i_data++] = i_field;
-            c->p_data[c->i_data++] = cc[2];
-            c->p_data[c->i_data++] = cc[3];
+
+            cc_AppendData( c, i_field, &cc[2] );
         }
         c->b_reorder = false;
     }
+    else if( ( !memcmp( p_cc_scte20, p_src, 2 ) ||
+               !memcmp( p_cc_scte20_old, p_src, 2 ) ) && i_src > 2 )
+    {
+        /* SCTE-20 CC */
+        bs_t s;
+        bs_init( &s, &p_src[2], i_src - 2 );
+        const int i_cc_count = bs_read( &s, 5 );
+        for( int i = 0; i < i_cc_count; i++ )
+        {
+            bs_skip( &s, 2 );
+            const int i_field_idx = bs_read( &s, 2 );
+            bs_skip( &s, 5 );
+            uint8_t cc[2];
+            for( int j = 0; j < 2; j++ )
+            {
+                cc[j] = 0;
+                for( int k = 0; k < 8; k++ )
+                    cc[j] |= bs_read( &s, 1 ) << k;
+            }
+            bs_skip( &s, 1 );
+
+            if( i_field_idx == 0 )
+                continue;
+            if( c->i_data + 2*3 > CC_MAX_DATA_SIZE )
+                continue;
+
+            /* 1,2,3 -> 0,1,0. I.E. repeated field 3 is merged with field 1 */
+            int i_field = ((i_field_idx - 1) & 1);
+            if (!b_top_field_first)
+                i_field ^= 1;
+
+            cc_AppendData( c, i_field, &cc[0] );
+        }
+        c->b_reorder = true;
+    }
     else
     {
 #if 0
 #define V(x) ( ( x < 0x20 || x >= 0x7f ) ? '?' : x )
-        fprintf( stderr, "-------------- unknown user data %2.2x %2.2x %2.2x %2.2x %c%c%c%c\n",
-                 p_src[0], p_src[1], p_src[2], p_src[3],
-                 V(p_src[0]), V(p_src[1]), V(p_src[2]), V(p_src[3]) );
+        fprintf( stderr, "-------------- unknown user data " );
+        for( int i = 0; i < i_src; i++ )
+            fprintf( stderr, "%2.2x ", p_src[i] );
+        for( int i = 0; i < i_src; i++ )
+            fprintf( stderr, "%c ", V(p_src[i]) );
+        fprintf( stderr, "\n" );
 #undef V
 #endif
         /* TODO DVD CC, ... */