input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / codec / cc.h
blobfb80d176f1e46480da7331c31151c2dd97eae8a4
1 /*****************************************************************************
2 * cc.h
3 *****************************************************************************
4 * Copyright (C) 2007 Laurent Aimar
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifndef VLC_CC_H_
25 #define VLC_CC_H_
27 #include <vlc_bits.h>
29 #define CC_PKT_BYTE0(field) (0xFC | (0x03 & field))
31 /* CC have a maximum rate of 9600 bit/s (per field?) */
32 #define CC_MAX_DATA_SIZE (2 * 3*600)
33 enum cc_payload_type_e
35 CC_PAYLOAD_NONE,
36 CC_PAYLOAD_RAW,
37 CC_PAYLOAD_GA94,
38 CC_PAYLOAD_DVD,
39 CC_PAYLOAD_REPLAYTV,
40 CC_PAYLOAD_SCTE20,
42 typedef struct
44 /* Which channel are present */
45 uint64_t i_708channels;
46 uint8_t i_608channels;
48 /* */
49 bool b_reorder;
51 /* */
52 enum cc_payload_type_e i_payload_type;
53 int i_payload_other_count;
55 /* CC data per field
56 * byte[x+0]: field (0/1)
57 * byte[x+1]: cc data 1
58 * byte[x+2]: cc data 2
60 size_t i_data;
61 uint8_t p_data[CC_MAX_DATA_SIZE];
62 } cc_data_t;
64 static inline void cc_Init( cc_data_t *c )
66 c->i_608channels = 0;
67 c->i_708channels = 0;
68 c->i_data = 0;
69 c->b_reorder = false;
70 c->i_payload_type = CC_PAYLOAD_NONE;
71 c->i_payload_other_count = 0;
73 static inline void cc_Exit( cc_data_t *c )
75 VLC_UNUSED(c);
76 return;
78 static inline void cc_Flush( cc_data_t *c )
80 c->i_data = 0;
83 static inline void cc_AppendData( cc_data_t *c, uint8_t cc_preamble, const uint8_t cc[2] )
85 uint8_t i_field = cc_preamble & 0x03;
86 if( i_field == 0 || i_field == 1 )
87 c->i_608channels |= (3 << (2 * i_field));
88 else
89 c->i_708channels |= 1;
91 c->p_data[c->i_data++] = cc_preamble;
92 c->p_data[c->i_data++] = cc[0];
93 c->p_data[c->i_data++] = cc[1];
96 static inline void cc_Extract( cc_data_t *c, enum cc_payload_type_e i_payload_type,
97 bool b_top_field_first, const uint8_t *p_src, int i_src )
99 if( c->i_payload_type != CC_PAYLOAD_NONE && c->i_payload_type != i_payload_type )
101 c->i_payload_other_count++;
102 if( c->i_payload_other_count < 50 )
103 return;
105 c->i_payload_type = i_payload_type;
106 c->i_payload_other_count = 0;
108 if( i_payload_type == CC_PAYLOAD_RAW )
110 for( int i = 0; i + 2 < i_src; i += 3 )
112 if( c->i_data + 3 > CC_MAX_DATA_SIZE )
113 break;
115 const uint8_t *cc = &p_src[i];
116 cc_AppendData( c, cc[0], &cc[1] );
118 c->b_reorder = true;
120 else if( i_payload_type == CC_PAYLOAD_GA94 )
122 /* cc_data()
123 * u1 reserved(1)
124 * u1 process_cc_data_flag
125 * u1 additional_data_flag
126 * u5 cc_count
127 * u8 reserved(1111 1111)
128 * for cc_count
129 * u5 marker bit(1111 1)
130 * u1 cc_valid
131 * u2 cc_type
132 * u8 cc_data_1
133 * u8 cc_data_2
134 * u8 marker bit(1111 1111)
135 * if additional_data_flag
136 * unknown
138 /* cc_type:
139 * 0x00: field 1
140 * 0x01: field 2
142 const uint8_t *cc = &p_src[0];
143 const int i_count_cc = cc[0]&0x1f;
144 int i;
146 if( !(cc[0]&0x40) ) // process flag
147 return;
148 if( i_src < 1+1 + i_count_cc*3 + 1) // broken packet
149 return;
150 if( i_count_cc <= 0 ) // no cc present
151 return;
152 if( cc[2+i_count_cc * 3] != 0xff ) // marker absent
153 return;
154 cc += 2;
156 for( i = 0; i < i_count_cc; i++, cc += 3 )
158 if( c->i_data + 3 > CC_MAX_DATA_SIZE )
159 break;
161 cc_AppendData( c, cc[0], &cc[1] );
163 c->b_reorder = true;
165 else if( i_payload_type == CC_PAYLOAD_DVD )
167 /* user_data
168 * (u32 stripped earlier)
169 * u32 (0x43 0x43 0x01 0xf8)
170 * u1 caption_odd_field_first (CC1/CC2)
171 * u1 caption_filler
172 * u5 cc_block_count (== cc_count / 2)
173 * u1 caption_extra_field_added (because odd cc_count)
174 * for cc_block_count * 2 + caption_extra_field_added
175 * u7 cc_filler_1
176 * u1 cc_field_is_odd
177 * u8 cc_data_1
178 * u8 cc_data_2
180 const int b_truncate = p_src[4] & 0x01;
181 const int i_field_first = (p_src[4] & 0x80) ? 0 : 1;
182 const int i_count_cc2 = (p_src[4] >> 1) & 0xf;
183 const uint8_t *cc = &p_src[5];
184 int i;
186 if( i_src < 4+1+6*i_count_cc2 - ( b_truncate ? 3 : 0) )
187 return;
188 for( i = 0; i < i_count_cc2; i++ )
190 int j;
191 for( j = 0; j < 2; j++, cc += 3 )
193 const int i_field = j == i_field_first ? 0 : 1;
195 if( b_truncate && i == i_count_cc2 - 1 && j == 1 )
196 break;
197 if( (cc[0] & 0xfe) != 0xfe )
198 continue;
199 if( c->i_data + 3 > CC_MAX_DATA_SIZE )
200 continue;
202 cc_AppendData( c, CC_PKT_BYTE0(i_field), &cc[1] );
205 c->b_reorder = false;
207 else if( i_payload_type == CC_PAYLOAD_REPLAYTV )
209 const uint8_t *cc = &p_src[0];
210 for( int i_cc_count = i_src >> 2; i_cc_count > 0;
211 i_cc_count--, cc += 4 )
213 if( c->i_data + 3 > CC_MAX_DATA_SIZE )
214 return;
215 uint8_t i_field = (cc[0] & 0x02) >> 1;
216 cc_AppendData( c, CC_PKT_BYTE0(i_field), &cc[2] );
218 c->b_reorder = false;
220 else /* CC_PAYLOAD_SCTE20 */
222 /* user_data(2)
223 * (u32 stripped earlier)
224 * u16 p_cc_scte20
225 * u5 cc_count
226 * for cc_count
227 * u2 cc_priority
228 * u2 cc_field_num
229 * u5 cc_line_offset
230 * u8 cc_data_1[1:8]
231 * u8 cc_data_2[1:8]
232 * u1 marker bit
233 * un additional_realtimevideodata
234 * un reserved
236 bs_t s;
237 bs_init( &s, &p_src[2], i_src - 2 );
238 const int i_cc_count = bs_read( &s, 5 );
239 for( int i = 0; i < i_cc_count; i++ )
241 bs_skip( &s, 2 );
242 const int i_field_idx = bs_read( &s, 2 );
243 bs_skip( &s, 5 );
244 uint8_t cc[2];
245 for( int j = 0; j < 2; j++ )
247 cc[j] = 0;
248 for( int k = 0; k < 8; k++ )
249 cc[j] |= bs_read( &s, 1 ) << k;
251 bs_skip( &s, 1 );
253 if( i_field_idx == 0 )
254 continue;
255 if( c->i_data + 2*3 > CC_MAX_DATA_SIZE )
256 continue;
258 /* 1,2,3 -> 0,1,0. I.E. repeated field 3 is merged with field 1 */
259 int i_field = ((i_field_idx - 1) & 1);
260 if (!b_top_field_first)
261 i_field ^= 1;
263 cc_AppendData( c, CC_PKT_BYTE0(i_field), &cc[0] );
265 c->b_reorder = true;
270 static inline void cc_ProbeAndExtract( cc_data_t *c, bool b_top_field_first, const uint8_t *p_src, int i_src )
272 static const uint8_t p_cc_ga94[4] = { 0x47, 0x41, 0x39, 0x34 };
273 static const uint8_t p_cc_dvd[4] = { 0x43, 0x43, 0x01, 0xf8 }; /* ascii 'CC', type_code, cc_block_size */
274 static const uint8_t p_cc_replaytv4a[2] = { 0xbb, 0x02 };/* RTV4K, BB02xxxxCC02 */
275 static const uint8_t p_cc_replaytv4b[2] = { 0xcc, 0x02 };/* see DVR-ClosedCaption in samples */
276 static const uint8_t p_cc_replaytv5a[2] = { 0x99, 0x02 };/* RTV5K, 9902xxxxAA02 */
277 static const uint8_t p_cc_replaytv5b[2] = { 0xaa, 0x02 };/* see DVR-ClosedCaption in samples */
278 static const uint8_t p_cc_scte20[2] = { 0x03, 0x81 }; /* user_data_type_code, SCTE 20 */
279 static const uint8_t p_cc_scte20_old[2] = { 0x03, 0x01 };/* user_data_type_code, old, Note 1 */
281 if( i_src < 4 )
282 return;
284 enum cc_payload_type_e i_payload_type;
285 if( !memcmp( p_cc_ga94, p_src, 4 ) && i_src >= 5+1+1+1 && p_src[4] == 0x03 )
287 /* CC from DVB/ATSC TS */
288 i_payload_type = CC_PAYLOAD_GA94;
289 i_src -= 5;
290 p_src += 5;
292 else if( !memcmp( p_cc_dvd, p_src, 4 ) && i_src > 4+1 )
294 i_payload_type = CC_PAYLOAD_DVD;
296 else if( i_src >= 2+2 + 2+2 &&
297 ( ( !memcmp( p_cc_replaytv4a, &p_src[0], 2 ) && !memcmp( p_cc_replaytv4b, &p_src[4], 2 ) ) ||
298 ( !memcmp( p_cc_replaytv5a, &p_src[0], 2 ) && !memcmp( p_cc_replaytv5b, &p_src[4], 2 ) ) ) )
300 i_payload_type = CC_PAYLOAD_REPLAYTV;
302 else if( ( !memcmp( p_cc_scte20, p_src, 2 ) ||
303 !memcmp( p_cc_scte20_old, p_src, 2 ) ) && i_src > 2 )
305 i_payload_type = CC_PAYLOAD_SCTE20;
307 else if (p_src[0] == 0x03 && p_src[1] == i_src - 2) /* DIRECTV */
309 i_payload_type = CC_PAYLOAD_GA94;
310 i_src -= 2;
311 p_src += 2;
313 else
315 #if 0
316 #define V(x) ( ( x < 0x20 || x >= 0x7f ) ? '?' : x )
317 fprintf( stderr, "-------------- unknown user data " );
318 for( int i = 0; i < i_src; i++ )
319 fprintf( stderr, "%2.2x ", p_src[i] );
320 for( int i = 0; i < i_src; i++ )
321 fprintf( stderr, "%c ", V(p_src[i]) );
322 fprintf( stderr, "\n" );
323 #undef V
324 #endif
325 return;
328 cc_Extract( c, i_payload_type, b_top_field_first, p_src, i_src );
331 #endif /* _CC_H */