cc.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * cc.h
00003  *****************************************************************************
00004  * Copyright (C) 2007 Laurent Aimar
00005  * $Id: f582c36eb2c179c841298086ecdaed04e5bc05b7 $
00006  *
00007  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00022  *****************************************************************************/
00023 
00024 #ifndef _CC_H
00025 #define _CC_H 1
00026 
00027 #include <vlc_bits.h>
00028 
00029 /* CC have a maximum rate of 9600 bit/s (per field?) */
00030 #define CC_MAX_DATA_SIZE (2 * 3*600)
00031 typedef struct
00032 {
00033     /* Which channel are present */
00034     bool pb_present[4];
00035 
00036     /* */
00037     bool b_reorder;
00038 
00039     /* CC data per field
00040      *  byte[x+0]: field (0/1)
00041      *  byte[x+1]: cc data 1
00042      *  byte[x+2]: cc data 2
00043      */
00044     int     i_data;
00045     uint8_t p_data[CC_MAX_DATA_SIZE];
00046 } cc_data_t;
00047 
00048 static inline void cc_Init( cc_data_t *c )
00049 {
00050     int i;
00051 
00052     for( i = 0; i < 4; i++ )
00053         c-> pb_present[i] = false; 
00054     c->i_data = 0;
00055     c->b_reorder = false;
00056 }
00057 static inline void cc_Exit( cc_data_t *c )
00058 {
00059     VLC_UNUSED(c);
00060     return;
00061 }
00062 static inline void cc_Flush( cc_data_t *c )
00063 {
00064     c->i_data = 0;
00065 }
00066 
00067 static inline void cc_AppendData( cc_data_t *c, int i_field, const uint8_t cc[2] )
00068 {
00069     if( i_field == 0 || i_field == 1 )
00070     {
00071         c->pb_present[2*i_field+0] =
00072         c->pb_present[2*i_field+1] = true;
00073     }
00074 
00075     c->p_data[c->i_data++] = i_field;
00076     c->p_data[c->i_data++] = cc[0];
00077     c->p_data[c->i_data++] = cc[1];
00078 }
00079 
00080 static inline void cc_Extract( cc_data_t *c, bool b_top_field_first, const uint8_t *p_src, int i_src )
00081 {
00082     static const uint8_t p_cc_ga94[4] = { 0x47, 0x41, 0x39, 0x34 };
00083     static const uint8_t p_cc_dvd[4] = { 0x43, 0x43, 0x01, 0xf8 };
00084     static const uint8_t p_cc_replaytv4a[2] = { 0xbb, 0x02 };
00085     static const uint8_t p_cc_replaytv4b[2] = { 0xcc, 0x02 };
00086     static const uint8_t p_cc_replaytv5a[2] = { 0x99, 0x02 };
00087     static const uint8_t p_cc_replaytv5b[2] = { 0xaa, 0x02 };
00088     static const uint8_t p_cc_scte20[2] = { 0x03, 0x81 };
00089     static const uint8_t p_cc_scte20_old[2] = { 0x03, 0x01 };
00090     //static const uint8_t p_afd_start[4] = { 0x44, 0x54, 0x47, 0x31 };
00091 
00092     if( i_src < 4 )
00093         return;
00094 
00095     if( !memcmp( p_cc_ga94, p_src, 4 ) && i_src >= 5+1+1+1 && p_src[4] == 0x03 )
00096     {
00097         /* Extract CC from DVB/ATSC TS */
00098         /* cc_data()
00099          *          u1 reserved(1)
00100          *          u1 process_cc_data_flag
00101          *          u1 additional_data_flag
00102          *          u5 cc_count
00103          *          u8 reserved(1111 1111)
00104          *          for cc_count
00105          *              u5 marker bit(1111 1)
00106          *              u1 cc_valid
00107          *              u2 cc_type
00108          *              u8 cc_data_1
00109          *              u8 cc_data_2
00110          *          u8 marker bit(1111 1111)
00111          *          if additional_data_flag
00112          *              unknown
00113          */
00114         /* cc_type:
00115          *  0x00: field 1
00116          *  0x01: field 2
00117          */
00118         const uint8_t *cc = &p_src[5];
00119         const int i_count_cc = cc[0]&0x1f;
00120         int i;
00121 
00122         if( !(cc[0]&0x40) ) // process flag
00123             return;
00124         if( i_src < 5 + 1+1 + i_count_cc*3 + 1)  // broken packet
00125             return;
00126         if( i_count_cc <= 0 )   // no cc present
00127             return;
00128         if( cc[2+i_count_cc * 3] != 0xff )  // marker absent
00129             return;
00130         cc += 2;
00131 
00132         for( i = 0; i < i_count_cc; i++, cc += 3 )
00133         {
00134             int i_field = cc[0] & 0x03;
00135             if( ( cc[0] & 0xfc ) != 0xfc )
00136                 continue;
00137             if( i_field != 0 && i_field != 1 )
00138                 continue;
00139             if( c->i_data + 3 > CC_MAX_DATA_SIZE )
00140                 continue;
00141 
00142             cc_AppendData( c, i_field, &cc[1] );
00143         }
00144         c->b_reorder = true;
00145     }
00146     else if( !memcmp( p_cc_dvd, p_src, 4 ) && i_src > 4+1 )
00147     {
00148         /* DVD CC */
00149         const int b_truncate = p_src[4] & 0x01;
00150         const int i_field_first = (p_src[4] & 0x80) ? 0 : 1;
00151         const int i_count_cc2 = (p_src[4] >> 1) & 0xf;
00152         const uint8_t *cc = &p_src[5];
00153         int i;
00154 
00155         if( i_src < 4+1+6*i_count_cc2 - ( b_truncate ? 3 : 0) )
00156             return;
00157         for( i = 0; i < i_count_cc2; i++ )
00158         {
00159             int j;
00160             for( j = 0; j < 2; j++, cc += 3 )
00161             {
00162                 const int i_field = j == i_field_first ? 0 : 1;
00163 
00164                 if( b_truncate && i == i_count_cc2 - 1 && j == 1 )
00165                     break;
00166                 if( cc[0] != 0xff && cc[0] != 0xfe )
00167                     continue;
00168                 if( c->i_data + 3 > CC_MAX_DATA_SIZE )
00169                     continue;
00170 
00171                 cc_AppendData( c, i_field, &cc[1] );
00172             }
00173         }
00174         c->b_reorder = false;
00175     }
00176     else if( i_src >= 2+2 + 2+2 &&
00177              ( ( !memcmp( p_cc_replaytv4a, &p_src[0], 2 ) && !memcmp( p_cc_replaytv4b, &p_src[4], 2 ) ) ||
00178                ( !memcmp( p_cc_replaytv5a, &p_src[0], 2 ) && !memcmp( p_cc_replaytv5b, &p_src[4], 2 ) ) ) )
00179     {
00180         /* ReplayTV CC */
00181         const uint8_t *cc = &p_src[0];
00182         int i;
00183         if( c->i_data + 2*3 > CC_MAX_DATA_SIZE )
00184             return;
00185 
00186         for( i = 0; i < 2; i++, cc += 4 )
00187         {
00188             const int i_field = i == 0 ? 1 : 0;
00189 
00190             cc_AppendData( c, i_field, &cc[2] );
00191         }
00192         c->b_reorder = false;
00193     }
00194     else if( ( !memcmp( p_cc_scte20, p_src, 2 ) ||
00195                !memcmp( p_cc_scte20_old, p_src, 2 ) ) && i_src > 2 )
00196     {
00197         /* SCTE-20 CC */
00198         bs_t s;
00199         bs_init( &s, &p_src[2], i_src - 2 );
00200         const int i_cc_count = bs_read( &s, 5 );
00201         for( int i = 0; i < i_cc_count; i++ )
00202         {
00203             bs_skip( &s, 2 );
00204             const int i_field_idx = bs_read( &s, 2 );
00205             bs_skip( &s, 5 );
00206             uint8_t cc[2];
00207             for( int j = 0; j < 2; j++ )
00208             {
00209                 cc[j] = 0;
00210                 for( int k = 0; k < 8; k++ )
00211                     cc[j] |= bs_read( &s, 1 ) << k;
00212             }
00213             bs_skip( &s, 1 );
00214 
00215             if( i_field_idx == 0 )
00216                 continue;
00217             if( c->i_data + 2*3 > CC_MAX_DATA_SIZE )
00218                 continue;
00219 
00220             /* 1,2,3 -> 0,1,0. I.E. repeated field 3 is merged with field 1 */
00221             int i_field = ((i_field_idx - 1) & 1);
00222             if (!b_top_field_first)
00223                 i_field ^= 1;
00224 
00225             cc_AppendData( c, i_field, &cc[0] );
00226         }
00227         c->b_reorder = true;
00228     }
00229     else
00230     {
00231 #if 0
00232 #define V(x) ( ( x < 0x20 || x >= 0x7f ) ? '?' : x )
00233         fprintf( stderr, "-------------- unknown user data " );
00234         for( int i = 0; i < i_src; i++ )
00235             fprintf( stderr, "%2.2x ", p_src[i] );
00236         for( int i = 0; i < i_src; i++ )
00237             fprintf( stderr, "%c ", V(p_src[i]) );
00238         fprintf( stderr, "\n" );
00239 #undef V
00240 #endif
00241         /* TODO DVD CC, ... */
00242     }
00243 }
00244 
00245 #endif /* _CC_H */
00246 

Generated on Tue May 25 08:04:54 2010 for VLC by  doxygen 1.5.6