]> git.sesse.net Git - vlc/blob - modules/codec/cc.h
Allows selecting CC as soon as possible.
[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 _CC_H 1
26
27 #include <vlc_bits.h>
28
29 /* CC have a maximum rate of 9600 bit/s (per field?) */
30 #define CC_MAX_DATA_SIZE (2 * 3*600)
31 typedef struct
32 {
33     /* Which channel are present */
34     bool pb_present[4];
35
36     /* */
37     bool b_reorder;
38
39     /* CC data per field
40      *  byte[x+0]: field (0/1)
41      *  byte[x+1]: cc data 1
42      *  byte[x+2]: cc data 2
43      */
44     int     i_data;
45     uint8_t p_data[CC_MAX_DATA_SIZE];
46 } cc_data_t;
47
48 static inline void cc_Init( cc_data_t *c )
49 {
50     int i;
51
52     for( i = 0; i < 4; i++ )
53         c-> pb_present[i] = false; 
54     c->i_data = 0;
55     c->b_reorder = false;
56 }
57 static inline void cc_Exit( cc_data_t *c )
58 {
59     VLC_UNUSED(c);
60     return;
61 }
62 static inline void cc_Flush( cc_data_t *c )
63 {
64     c->i_data = 0;
65 }
66
67 static inline void cc_AppendData( cc_data_t *c, int i_field, const uint8_t cc[2] )
68 {
69     if( i_field == 0 || i_field == 1 )
70     {
71         c->pb_present[2*i_field+0] =
72         c->pb_present[2*i_field+1] = true;
73     }
74
75     c->p_data[c->i_data++] = i_field;
76     c->p_data[c->i_data++] = cc[0];
77     c->p_data[c->i_data++] = cc[1];
78 }
79
80 static inline void cc_Extract( cc_data_t *c, const uint8_t *p_src, int i_src )
81 {
82     static const uint8_t p_cc_ga94[4] = { 0x47, 0x41, 0x39, 0x34 };
83     static const uint8_t p_cc_dvd[4] = { 0x43, 0x43, 0x01, 0xf8 };
84     static const uint8_t p_cc_replaytv4a[2] = { 0xbb, 0x02 };
85     static const uint8_t p_cc_replaytv4b[2] = { 0xcc, 0x02 };
86     static const uint8_t p_cc_replaytv5a[2] = { 0x99, 0x02 };
87     static const uint8_t p_cc_replaytv5b[2] = { 0xaa, 0x02 };
88     static const uint8_t p_cc_scte20[2] = { 0x03, 0x81 };
89     //static const uint8_t p_afd_start[4] = { 0x44, 0x54, 0x47, 0x31 };
90
91     if( i_src < 4 )
92         return;
93
94     if( !memcmp( p_cc_ga94, p_src, 4 ) && i_src >= 5+1+1+1 && p_src[4] == 0x03 )
95     {
96         /* Extract CC from DVB/ATSC TS */
97         /* cc_data()
98          *          u1 reserved(1)
99          *          u1 process_cc_data_flag
100          *          u1 additional_data_flag
101          *          u5 cc_count
102          *          u8 reserved(1111 1111)
103          *          for cc_count
104          *              u5 marker bit(1111 1)
105          *              u1 cc_valid
106          *              u2 cc_type
107          *              u8 cc_data_1
108          *              u8 cc_data_2
109          *          u8 marker bit(1111 1111)
110          *          if additional_data_flag
111          *              unknown
112          */
113         /* cc_type:
114          *  0x00: field 1
115          *  0x01: field 2
116          */
117         const uint8_t *cc = &p_src[5];
118         const int i_count_cc = cc[0]&0x1f;
119         int i;
120
121         if( !(cc[0]&0x40) ) // process flag
122             return;
123         if( i_src < 5 + 1+1 + i_count_cc*3 + 1)  // broken packet
124             return;
125         if( i_count_cc <= 0 )   // no cc present
126             return;
127         if( cc[2+i_count_cc * 3] != 0xff )  // marker absent
128             return;
129         cc += 2;
130
131         for( i = 0; i < i_count_cc; i++, cc += 3 )
132         {
133             int i_field = cc[0] & 0x03;
134             if( ( cc[0] & 0xfc ) != 0xfc )
135                 continue;
136             if( i_field != 0 && i_field != 1 )
137                 continue;
138             if( c->i_data + 3 > CC_MAX_DATA_SIZE )
139                 continue;
140
141             cc_AppendData( c, i_field, &cc[1] );
142         }
143         c->b_reorder = true;
144     }
145     else if( !memcmp( p_cc_dvd, p_src, 4 ) && i_src > 4+1 )
146     {
147         /* DVD CC */
148         const int b_truncate = p_src[4] & 0x01;
149         const int i_field_first = (p_src[4] & 0x80) ? 0 : 1;
150         const int i_count_cc2 = (p_src[4] >> 1) & 0xf;
151         const uint8_t *cc = &p_src[5];
152         int i;
153
154         if( i_src < 4+1+6*i_count_cc2 - ( b_truncate ? 3 : 0) )
155             return;
156         for( i = 0; i < i_count_cc2; i++ )
157         {
158             int j;
159             for( j = 0; j < 2; j++, cc += 3 )
160             {
161                 const int i_field = j == i_field_first ? 0 : 1;
162
163                 if( b_truncate && i == i_count_cc2 - 1 && j == 1 )
164                     break;
165                 if( cc[0] != 0xff && cc[0] != 0xfe )
166                     continue;
167                 if( c->i_data + 3 > CC_MAX_DATA_SIZE )
168                     continue;
169
170                 cc_AppendData( c, i_field, &cc[1] );
171             }
172         }
173         c->b_reorder = false;
174     }
175     else if( i_src >= 2+2 + 2+2 &&
176              ( ( !memcmp( p_cc_replaytv4a, &p_src[0], 2 ) && !memcmp( p_cc_replaytv4b, &p_src[4], 2 ) ) ||
177                ( !memcmp( p_cc_replaytv5a, &p_src[0], 2 ) && !memcmp( p_cc_replaytv5b, &p_src[4], 2 ) ) ) )
178     {
179         /* ReplayTV CC */
180         const uint8_t *cc = &p_src[0];
181         int i;
182         if( c->i_data + 2*3 > CC_MAX_DATA_SIZE )
183             return;
184
185         for( i = 0; i < 2; i++, cc += 4 )
186         {
187             const int i_field = i == 0 ? 1 : 0;
188
189             cc_AppendData( c, i_field, &cc[2] );
190         }
191         c->b_reorder = false;
192     }
193     else if( !memcmp( p_cc_scte20, p_src, 2 ) && i_src > 2 )
194     {
195         /* SCTE-20 CC */
196         bs_t s;
197         bs_init( &s, &p_src[2], i_src - 2 );
198         const int i_cc_count = bs_read( &s, 5 );
199         for( int i = 0; i < i_cc_count; i++ )
200         {
201             bs_skip( &s, 2 );
202             const int i_field_idx = bs_read( &s, 2 );
203             bs_skip( &s, 5 );
204             uint8_t cc[2];
205             for( int j = 0; j < 2; j++ )
206             {
207                 cc[j] = 0;
208                 for( int k = 0; k < 8; k++ )
209                     cc[j] |= bs_read( &s, 1 ) << k;
210             }
211             bs_skip( &s, 1 );
212
213             if( i_field_idx != 1 && i_field_idx != 2 )
214                 continue;
215             if( c->i_data + 2*3 > CC_MAX_DATA_SIZE )
216                 continue;
217
218             const int i_field = i_field_idx - 1;
219
220             cc_AppendData( c, i_field, &cc[0] );
221         }
222         c->b_reorder = true;
223     }
224     else
225     {
226 #if 0
227 #define V(x) ( ( x < 0x20 || x >= 0x7f ) ? '?' : x )
228         fprintf( stderr, "-------------- unknown user data " );
229         for( int i = 0; i < i_src; i++ )
230             fprintf( stderr, "%2.2x ", p_src[i] );
231         for( int i = 0; i < i_src; i++ )
232             fprintf( stderr, "%c ", V(p_src[i]) );
233         fprintf( stderr, "\n" );
234 #undef V
235 #endif
236         /* TODO DVD CC, ... */
237     }
238 }
239
240 #endif /* _CC_H */
241