]> git.sesse.net Git - vlc/blob - modules/codec/cc.h
update module LIST file.
[vlc] / modules / codec / cc.h
1 /*****************************************************************************
2  * cc.h
3  *****************************************************************************
4  * Copyright (C) 2007 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifndef _CC_H
25 #define _C_H 1
26
27 /* CC have a maximum rate of 9600 bit/s (per field?) */
28 #define CC_MAX_DATA_SIZE (2 * 3*600)
29 typedef struct
30 {
31     /* Which channel are present */
32     vlc_bool_t pb_present[4];
33
34     /* */
35     vlc_bool_t b_reorder;
36
37     /* CC data per field
38      *  byte[x+0]: field (0/1)
39      *  byte[x+1]: cc data 1
40      *  byte[x+2]: cc data 2
41      */
42     int     i_data;
43     uint8_t p_data[CC_MAX_DATA_SIZE];
44 } cc_data_t;
45
46 static inline int cc_Channel( int i_field, const uint8_t p_data[2] )
47 {
48     const uint8_t d = p_data[0] & 0x7f;
49     if( i_field != 0 && i_field != 1 )
50         return -1;
51     if( d == 0x14 )
52         return 2*i_field + 0;
53     else if( d == 0x1c )
54         return 2*i_field + 1;
55     /* unknown(middle of a command)  or not cc channel */
56     return -1;
57 }
58 static inline void cc_Init( cc_data_t *c )
59 {
60     int i;
61
62     for( i = 0; i < 4; i++ )
63         c-> pb_present[i] = VLC_FALSE; 
64     c->i_data = 0;
65     c->b_reorder = VLC_FALSE;
66 }
67 static inline void cc_Exit( cc_data_t *c )
68 {
69     VLC_UNUSED(c);
70     return;
71 }
72 static inline void cc_Flush( cc_data_t *c )
73 {
74     c->i_data = 0;
75 }
76 static inline void cc_Extract( cc_data_t *c, const uint8_t *p_src, int i_src )
77 {
78     static const uint8_t p_cc_ga94[4] = { 0x47, 0x41, 0x39, 0x34 };
79     static const uint8_t p_cc_dvd[4] = { 0x43, 0x43, 0x01, 0xf8 };
80     static const uint8_t p_cc_replaytv4a[2] = { 0xbb, 0x02 };
81     static const uint8_t p_cc_replaytv4b[2] = { 0xcc, 0x02 };
82     static const uint8_t p_cc_replaytv5a[2] = { 0x99, 0x02 };
83     static const uint8_t p_cc_replaytv5b[2] = { 0xaa, 0x02 };
84     //static const uint8_t p_afd_start[4] = { 0x44, 0x54, 0x47, 0x31 };
85
86     if( i_src < 4 )
87         return;
88
89     if( !memcmp( p_cc_ga94, p_src, 4 ) && i_src >= 5+1+1+1 && p_src[4] == 0x03 )
90     {
91         /* Extract CC from DVB/ATSC TS */
92         /* cc_data()
93          *          u1 reserved(1)
94          *          u1 process_cc_data_flag
95          *          u1 additional_data_flag
96          *          u5 cc_count
97          *          u8 reserved(1111 1111)
98          *          for cc_count
99          *              u5 marker bit(1111 1)
100          *              u1 cc_valid
101          *              u2 cc_type
102          *              u8 cc_data_1
103          *              u8 cc_data_2
104          *          u8 marker bit(1111 1111)
105          *          if additional_data_flag
106          *              unknown
107          */
108         /* cc_type:
109          *  0x00: field 1
110          *  0x01: field 2
111          */
112         const uint8_t *cc = &p_src[5];
113         const int i_count_cc = cc[0]&0x1f;
114         int i;
115
116         if( !(cc[0]&0x40) ) // process flag
117             return;
118         if( i_src < 5 + 1+1 + i_count_cc*3 + 1)  // broken packet
119             return;
120         if( i_count_cc <= 0 )   // no cc present
121             return;
122         if( cc[2+i_count_cc * 3] != 0xff )  // marker absent
123             return;
124         cc += 2;
125
126         for( i = 0; i < i_count_cc; i++, cc += 3 )
127         {
128             int i_field = cc[0] & 0x03;
129             int i_channel;
130             if( ( cc[0] & 0xfc ) != 0xfc )
131                 continue;
132             if( i_field != 0 && i_field != 1 )
133                 continue;
134             if( c->i_data + 3 > CC_MAX_DATA_SIZE )
135                 continue;
136
137             i_channel = cc_Channel( i_field, &cc[1] );
138             if( i_channel >= 0 && i_channel < 4 )
139                 c->pb_present[i_channel] = VLC_TRUE;
140
141             c->p_data[c->i_data++] = i_field;
142             c->p_data[c->i_data++] = cc[1];
143             c->p_data[c->i_data++] = cc[2];
144         }
145         c->b_reorder = VLC_TRUE;
146     }
147     else if( !memcmp( p_cc_dvd, p_src, 4 ) && i_src > 4+1 )
148     {
149         /* DVD CC */
150         const int b_truncate = p_src[4] & 0x01;
151         const int i_field_first = (p_src[4] & 0x80) ? 0 : 1;
152         const int i_count_cc2 = (p_src[4] >> 1) & 0xf;
153         const uint8_t *cc = &p_src[5];
154         int i;
155
156         if( i_src < 4+1+6*i_count_cc2 - ( b_truncate ? 3 : 0) )
157             return;
158         for( i = 0; i < i_count_cc2; i++ )
159         {
160             int j;
161             for( j = 0; j < 2; j++, cc += 3 )
162             {
163                 const int i_field = j == i_field_first ? 0 : 1;
164                 int i_channel;
165
166                 if( b_truncate && i == i_count_cc2 - 1 && j == 1 )
167                     break;
168                 if( cc[0] != 0xff && cc[0] != 0xfe )
169                     continue;
170                 if( c->i_data + 3 > CC_MAX_DATA_SIZE )
171                     continue;
172
173                 i_channel = cc_Channel( i_field, &cc[1] );
174                 if( i_channel >= 0 && i_channel < 4 )
175                     c->pb_present[i_channel] = VLC_TRUE;
176                 c->p_data[c->i_data++] = i_field;
177                 c->p_data[c->i_data++] = cc[1];
178                 c->p_data[c->i_data++] = cc[2];
179             }
180         }
181         c->b_reorder = VLC_FALSE;
182     }
183     else if( i_src >= 2+2 + 2+2 &&
184              ( ( !memcmp( p_cc_replaytv4a, &p_src[0], 2 ) && !memcmp( p_cc_replaytv4b, &p_src[4], 2 ) ) ||
185                ( !memcmp( p_cc_replaytv5a, &p_src[0], 2 ) && !memcmp( p_cc_replaytv5b, &p_src[4], 2 ) ) ) )
186     {
187         /* ReplayTV CC */
188         const uint8_t *cc = &p_src[0];
189         int i;
190         if( c->i_data + 2*3 > CC_MAX_DATA_SIZE )
191             return;
192
193         for( i = 0; i < 2; i++, cc += 4 )
194         {
195             const int i_field = i == 0 ? 1 : 0;
196             int i_channel = cc_Channel( i_field, &cc[2] );
197             if( i_channel >= 0 && i_channel < 4 )
198                 c->pb_present[i_channel] = VLC_TRUE;
199             c->p_data[c->i_data++] = i_field;
200             c->p_data[c->i_data++] = cc[2];
201             c->p_data[c->i_data++] = cc[3];
202         }
203         c->b_reorder = VLC_FALSE;
204     }
205     else
206     {
207 #if 0
208 #define V(x) ( ( x < 0x20 || x >= 0x7f ) ? '?' : x )
209         fprintf( stderr, "-------------- unknown user data %2.2x %2.2x %2.2x %2.2x %c%c%c%c\n",
210                  p_src[0], p_src[1], p_src[2], p_src[3],
211                  V(p_src[0]), V(p_src[1]), V(p_src[2]), V(p_src[3]) );
212 #undef V
213 #endif
214         /* TODO DVD CC, ... */
215     }
216 }
217
218 #endif /* _CC_H */
219