synced with r22428
[mplayer/greg.git] / sub_cc.c
blobc2580d0a2964ceccddd9926f9cc49074a4c3932b
1 /*
2 * sub_cc.c - Decoder for Closed Captions
4 * This decoder relies on MPlayer's OSD to display subtitles.
5 * Be warned that the decoding is somewhat preliminary, though it basically works.
6 *
7 * Most notably, only the text information is decoded as of now, discarding color,
8 * background and position info (see source below).
9 *
10 * by Matteo Giani
12 * uses source from the xine closed captions decoder
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
20 #include "config.h"
21 #include "sub_cc.h"
23 #include "subreader.h"
25 #include "libvo/video_out.h"
26 #include "libvo/sub.h"
29 #define CC_MAX_LINE_LENGTH 64
31 static char chartbl[128];
33 static subtitle buf1,buf2;
34 static subtitle *fb,*bb;
36 static unsigned int cursor_pos=0;
38 static int inited=0;
40 static void build_char_table(void)
42 int i;
43 /* first the normal ASCII codes */
44 for (i = 0; i < 128; i++)
45 chartbl[i] = (char) i;
46 /* now the special codes */
47 chartbl[0x2a] = 'á';
48 chartbl[0x5c] = 'é';
49 chartbl[0x5e] = 'í';
50 chartbl[0x5f] = 'ó';
51 chartbl[0x60] = 'ú';
52 chartbl[0x7b] = 'ç';
53 chartbl[0x7c] = '÷';
54 chartbl[0x7d] = 'Ñ';
55 chartbl[0x7e] = 'ñ';
56 chartbl[0x7f] = '¤'; /* FIXME: this should be a solid block */
59 static void clear_buffer(subtitle *buf)
61 int i;
62 buf->lines=0;
63 for(i=0;i<SUB_MAX_TEXT;i++) if(buf->text[i]) {free(buf->text[i]);buf->text[i]=NULL;}
67 void subcc_init(void)
69 int i;
70 //printf("subcc_init(): initing...\n");
71 build_char_table();
72 for(i=0;i<SUB_MAX_TEXT;i++) {buf1.text[i]=buf2.text[i]=NULL;}
73 buf1.lines=buf2.lines=0;
74 fb=&buf1;
75 bb=&buf2;
77 inited=1;
80 static void append_char(char c)
82 if(!bb->lines) {bb->lines++; cursor_pos=0;}
83 if(bb->text[bb->lines - 1]==NULL)
85 bb->text[bb->lines - 1]=malloc(CC_MAX_LINE_LENGTH);
86 memset(bb->text[bb->lines - 1],0,CC_MAX_LINE_LENGTH);
87 cursor_pos=0;
90 if(c=='\n')
92 if(cursor_pos>0 && bb->lines < SUB_MAX_TEXT)
93 bb->lines++;cursor_pos=0;
95 else
97 if(cursor_pos==CC_MAX_LINE_LENGTH-1)
99 fprintf(stderr,"sub_cc.c: append_char() reached CC_MAX_LINE_LENGTH!\n");
100 return;
102 bb->text[bb->lines - 1][cursor_pos++]=c;
107 static void swap_buffers(void)
109 subtitle *foo;
110 foo=fb;
111 fb=bb;
112 bb=foo;
115 static void display_buffer(subtitle * buf)
117 vo_sub=buf;
118 vo_osd_changed(OSDTYPE_SUBTITLE);
122 static void cc_decode_EIA608(unsigned short int data)
125 static unsigned short int lastcode=0x0000;
126 unsigned char c1 = data & 0x7f;
127 unsigned char c2 = (data >> 8) & 0x7f;
129 if (c1 & 0x60) { /* normal character, 0x20 <= c1 <= 0x7f */
130 append_char(chartbl[c1]);
131 if(c2 & 0x60) /*c2 might not be a normal char even if c1 is*/
132 append_char(chartbl[c2]);
134 else if (c1 & 0x10) // control code / special char
136 // int channel= (c1 & 0x08) >> 3;
137 c1&=~0x08;
138 if(data!=lastcode)
140 if(c2 & 0x40) { /*PAC, Preamble Address Code */
141 append_char('\n'); /*FIXME properly interpret PACs*/
143 else
144 switch(c1)
146 case 0x10: break; // ext attribute
147 case 0x11:
148 if((c2 & 0x30)==0x30)
150 //printf("[debug]:Special char (ignored)\n");
151 /*cc_decode_special_char()*/;
153 else if (c2 & 0x20)
155 //printf("[debug]: midrow_attr (ignored)\n");
156 /*cc_decode_midrow_attr()*/;
158 break;
159 case 0x14:
160 switch(c2)
162 case 0x2C: display_buffer(NULL); //EDM
163 clear_buffer(fb); break;
164 case 0x2d: append_char('\n'); //carriage return
165 break;
166 case 0x2e: clear_buffer(bb); //ENM
167 break;
168 case 0x2f: swap_buffers(); //Swap buffers
169 display_buffer(fb);
170 clear_buffer(bb);
171 break;
173 break;
174 case 0x17:
175 if( c2>=0x21 && c2<=0x23) //TAB
177 break;
182 lastcode=data;
185 static void subcc_decode(unsigned char *inputbuffer, unsigned int inputlength)
187 /* The first number may denote a channel number. I don't have the
188 * EIA-708 standard, so it is hard to say.
189 * From what I could figure out so far, the general format seems to be:
191 * repeat
193 * 0xfe starts 2 byte sequence of unknown purpose. It might denote
194 * field #2 in line 21 of the VBI. We'll ignore it for the
195 * time being.
197 * 0xff starts 2 byte EIA-608 sequence, field #1 in line 21 of the VBI.
198 * Followed by a 3-code triplet that starts either with 0xff or
199 * 0xfe. In either case, the following triplet needs to be ignored
200 * for line 21, field 1.
202 * 0x00 is padding, followed by 2 more 0x00.
204 * 0x01 always seems to appear at the beginning, always seems to
205 * be followed by 0xf8, 8-bit number.
206 * The lower 7 bits of this 8-bit number seem to denote the
207 * number of code triplets that follow.
208 * The most significant bit denotes whether the Line 21 field 1
209 * captioning information is at odd or even triplet offsets from this
210 * beginning triplet. 1 denotes odd offsets, 0 denotes even offsets.
212 * Most captions are encoded with odd offsets, so this is what we
213 * will assume.
215 * until end of packet
217 unsigned char *current = inputbuffer;
218 unsigned int curbytes = 0;
219 unsigned char data1, data2;
220 unsigned char cc_code;
221 int odd_offset = 1;
223 while (curbytes < inputlength) {
224 int skip = 2;
226 cc_code = *(current);
228 if (inputlength - curbytes < 2) {
229 #ifdef LOG_DEBUG
230 fprintf(stderr, "Not enough data for 2-byte CC encoding\n");
231 #endif
232 break;
235 data1 = *(current+1);
236 data2 = *(current + 2);
237 current++; curbytes++;
239 switch (cc_code) {
240 case 0xfe:
241 /* expect 2 byte encoding (perhaps CC3, CC4?) */
242 /* ignore for time being */
243 skip = 2;
244 break;
246 case 0xff:
247 /* expect EIA-608 CC1/CC2 encoding */
248 // FIXME check parity!
249 // Parity check omitted assuming we are reading from a DVD and therefore
250 // we should encounter no "transmission errors".
251 cc_decode_EIA608(data1 | (data2 << 8));
252 skip = 5;
253 break;
255 case 0x00:
256 /* This seems to be just padding */
257 skip = 2;
258 break;
260 case 0x01:
261 odd_offset = data2 & 0x80;
262 if (odd_offset)
263 skip = 2;
264 else
265 skip = 5;
266 break;
268 default:
269 //#ifdef LOG_DEBUG
270 fprintf(stderr, "Unknown CC encoding: %x\n", cc_code);
271 //#endif
272 skip = 2;
273 break;
275 current += skip;
276 curbytes += skip;
281 void subcc_process_data(unsigned char *inputdata,unsigned int len)
283 if(!subcc_enabled) return;
284 if(!inited) subcc_init();
286 subcc_decode(inputdata, len);