]> git.sesse.net Git - vlc/blob - modules/codec/cc.h
Closed captions decoder.
[vlc] / modules / codec / cc.h
1 /*****************************************************************************
2  * cc.h
3  *****************************************************************************
4  * Copyright (C) 2007 Laurent Aimar
5  * $Id: csa.h 20134 2007-05-16 14:30:27Z jpsaman $
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     /* CC data per field
35      *  byte[x+0]: field (0/1)
36      *  byte[x+1]: cc data 1
37      *  byte[x+2]: cc data 2
38      */
39     int     i_data;
40     uint8_t p_data[CC_MAX_DATA_SIZE];
41 } cc_data_t;
42
43 static inline int cc_Channel( int i_field, const uint8_t p_data[2] )
44 {
45     const uint8_t d = p_data[0] & 0x7f;
46     if( i_field != 0 && i_field != 1 )
47         return -1;
48     if( d == 0x14 )
49         return 2*i_field + 0;
50     else if( d == 0x1c )
51         return 2*i_field + 1;
52     /* unknown(middle of a command)  or not cc channel */
53     return -1;
54 }
55 static inline void cc_Init( cc_data_t *c )
56 {
57     int i;
58
59     for( i = 0; i < 4; i++ )
60         c-> pb_present[i] = VLC_FALSE; 
61     c->i_data = 0;
62 }
63 static inline void cc_Exit( cc_data_t *c )
64 {
65     return;
66 }
67 static inline void cc_Flush( cc_data_t *c )
68 {
69     c->i_data = 0;
70 }
71 static inline void cc_Extract( cc_data_t *c, const uint8_t *p_src, int i_src )
72 {
73     static const uint8_t p_cc_ga94[4] = { 0x47, 0x41, 0x39, 0x34 };
74     //static const uint8_t p_afd_start[4] = { 0x44, 0x54, 0x47, 0x31 };
75
76     if( i_src < 4 )
77         return;
78
79     /* cc_data()
80      *          u1 reserved(1)
81      *          u1 process_cc_data_flag
82      *          u1 additional_data_flag
83      *          u5 cc_count
84      *          u8 reserved(1111 1111)
85      *          for cc_count
86      *              u5 marker bit(1111 1)
87      *              u1 cc_valid
88      *              u2 cc_type
89      *              u8 cc_data_1
90      *              u8 cc_data_2
91      *          u8 marker bit(1111 1111)
92      *          if additional_data_flag
93      *              unknown
94      */
95     /* cc_type:
96      *  0x00: field 1
97      *  0x01: field 2
98      */
99     if( !memcmp( p_cc_ga94, p_src, 4 ) && i_src >= 5+1+1+1 && p_src[4] == 0x03 )
100     {
101         /* Extract CC from DVB/ATSC TS */
102         const uint8_t *cc = &p_src[5];
103         const int i_count_cc = cc[0]&0x1f;
104         int i;
105
106         if( !(cc[0]&0x40) ) // process flag
107             return;
108         if( i_src < 5 + 1+1 + i_count_cc*3 + 1)  // broken packet
109             return;
110         if( i_count_cc <= 0 )   // no cc present
111             return;
112         if( cc[2+i_count_cc * 3] != 0xff )  // marker absent
113             return;
114         cc += 2;
115
116         for( i = 0; i < i_count_cc; i++, cc += 3 )
117         {
118             int i_field = cc[0] & 0x03;
119             int i_channel;
120             if( ( cc[0] & 0xfc ) != 0xfc )
121                 continue;
122             if( i_field != 0 && i_field != 1 )
123                 continue;
124             if( c->i_data + 3 > CC_MAX_DATA_SIZE )
125                 continue;
126
127             i_channel = cc_Channel( i_field, &cc[1] );
128             if( i_channel >= 0 && i_channel < 4 )
129                 c->pb_present[i_channel] = VLC_TRUE;
130
131             c->p_data[c->i_data++] = i_field;
132             c->p_data[c->i_data++] = cc[1];
133             c->p_data[c->i_data++] = cc[2];
134         }
135     }
136     else
137     {
138         /* TODO DVD CC, ... */
139     }
140 }
141
142 #endif /* _CC_H */
143