libcdio
[mplayer.git] / sub_cc.c
blob9f263aba668ce650d1ac970c7eb7d25106f79235
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 "sub_cc.h"
22 #include "subreader.h"
24 #include "libvo/video_out.h"
25 #include "libvo/sub.h"
28 #define CC_INPUTBUFFER_SIZE 256
30 #define CC_MAX_LINE_LENGTH 64
32 static char chartbl[128];
34 static subtitle buf1,buf2;
35 static subtitle *fb,*bb;
37 static unsigned int cursor_pos=0;
39 static int inited=0;
40 static unsigned char inputbuffer[CC_INPUTBUFFER_SIZE];
41 static unsigned int inputlength;
43 static void build_char_table(void)
45 int i;
46 /* first the normal ASCII codes */
47 for (i = 0; i < 128; i++)
48 chartbl[i] = (char) i;
49 /* now the special codes */
50 chartbl[0x2a] = 'á';
51 chartbl[0x5c] = 'é';
52 chartbl[0x5e] = 'í';
53 chartbl[0x5f] = 'ó';
54 chartbl[0x60] = 'ú';
55 chartbl[0x7b] = 'ç';
56 chartbl[0x7c] = '÷';
57 chartbl[0x7d] = 'Ñ';
58 chartbl[0x7e] = 'ñ';
59 chartbl[0x7f] = '¤'; /* FIXME: this should be a solid block */
62 static void clear_buffer(subtitle *buf)
64 int i;
65 buf->lines=0;
66 for(i=0;i<SUB_MAX_TEXT;i++) if(buf->text[i]) {free(buf->text[i]);buf->text[i]=NULL;}
70 void subcc_init()
72 int i;
73 //printf("subcc_init(): initing...\n");
74 build_char_table();
75 for(i=0;i<SUB_MAX_TEXT;i++) {buf1.text[i]=buf2.text[i]=NULL;}
76 buf1.lines=buf2.lines=0;
77 fb=&buf1;
78 bb=&buf2;
80 inited=1;
83 static void append_char(char c)
85 if(!bb->lines) {bb->lines++; cursor_pos=0;}
86 if(bb->text[bb->lines - 1]==NULL)
88 bb->text[bb->lines - 1]=malloc(CC_MAX_LINE_LENGTH);
89 memset(bb->text[bb->lines - 1],0,CC_MAX_LINE_LENGTH);
90 cursor_pos=0;
93 if(c=='\n')
95 if(cursor_pos>0)
96 bb->lines++;cursor_pos=0;
98 else
100 if(cursor_pos==CC_MAX_LINE_LENGTH-1)
102 printf("sub_cc.c: append_char() reached CC_MAX_LINE_LENGTH!\n");
103 return;
105 bb->text[bb->lines - 1][cursor_pos++]=c;
110 static void swap_buffers()
112 subtitle *foo;
113 foo=fb;
114 fb=bb;
115 bb=foo;
118 static void display_buffer(subtitle * buf)
120 vo_sub=buf;
121 vo_osd_changed(OSDTYPE_SUBTITLE);
125 static void cc_decode_EIA608(unsigned short int data)
128 static unsigned short int lastcode=0x0000;
129 unsigned char c1 = data & 0x7f;
130 unsigned char c2 = (data >> 8) & 0x7f;
132 if (c1 & 0x60) { /* normal character, 0x20 <= c1 <= 0x7f */
133 append_char(chartbl[c1]);
134 if(c2 & 0x60) /*c2 might not be a normal char even if c1 is*/
135 append_char(chartbl[c2]);
137 else if (c1 & 0x10) // control code / special char
139 // int channel= (c1 & 0x08) >> 3;
140 c1&=~0x08;
141 if(data!=lastcode)
143 if(c2 & 0x40) { /*PAC, Preamble Address Code */
144 append_char('\n'); /*FIXME properly interpret PACs*/
146 else
147 switch(c1)
149 case 0x10: break; // ext attribute
150 case 0x11:
151 if((c2 & 0x30)==0x30)
153 //printf("[debug]:Special char (ignored)\n");
154 /*cc_decode_special_char()*/;
156 else if (c2 & 0x20)
158 //printf("[debug]: midrow_attr (ignored)\n");
159 /*cc_decode_midrow_attr()*/;
161 break;
162 case 0x14:
163 switch(c2)
165 case 0x2C: display_buffer(NULL); //EDM
166 clear_buffer(fb); break;
167 case 0x2d: append_char('\n'); //carriage return
168 break;
169 case 0x2e: clear_buffer(bb); //ENM
170 break;
171 case 0x2f: swap_buffers(); //Swap buffers
172 display_buffer(fb);
173 clear_buffer(bb);
174 break;
176 break;
177 case 0x17:
178 if( c2>=0x21 && c2<=0x23) //TAB
180 break;
185 lastcode=data;
188 static void subcc_decode()
190 /* The first number may denote a channel number. I don't have the
191 * EIA-708 standard, so it is hard to say.
192 * From what I could figure out so far, the general format seems to be:
194 * repeat
196 * 0xfe starts 2 byte sequence of unknown purpose. It might denote
197 * field #2 in line 21 of the VBI. We'll ignore it for the
198 * time being.
200 * 0xff starts 2 byte EIA-608 sequence, field #1 in line 21 of the VBI.
201 * Followed by a 3-code triplet that starts either with 0xff or
202 * 0xfe. In either case, the following triplet needs to be ignored
203 * for line 21, field 1.
205 * 0x00 is padding, followed by 2 more 0x00.
207 * 0x01 always seems to appear at the beginning, always seems to
208 * be followed by 0xf8, 8-bit number.
209 * The lower 7 bits of this 8-bit number seem to denote the
210 * number of code triplets that follow.
211 * The most significant bit denotes whether the Line 21 field 1
212 * captioning information is at odd or even triplet offsets from this
213 * beginning triplet. 1 denotes odd offsets, 0 denotes even offsets.
215 * Most captions are encoded with odd offsets, so this is what we
216 * will assume.
218 * until end of packet
220 unsigned char *current = inputbuffer;
221 unsigned int curbytes = 0;
222 unsigned char data1, data2;
223 unsigned char cc_code;
224 int odd_offset = 1;
226 while (curbytes < inputlength) {
227 int skip = 2;
229 cc_code = *(current);
231 if (inputlength - curbytes < 2) {
232 #ifdef LOG_DEBUG
233 fprintf(stderr, "Not enough data for 2-byte CC encoding\n");
234 #endif
235 break;
238 data1 = *(current+1);
239 data2 = *(current + 2);
240 current++; curbytes++;
242 switch (cc_code) {
243 case 0xfe:
244 /* expect 2 byte encoding (perhaps CC3, CC4?) */
245 /* ignore for time being */
246 skip = 2;
247 break;
249 case 0xff:
250 /* expect EIA-608 CC1/CC2 encoding */
251 // FIXME check parity!
252 // Parity check omitted assuming we are reading from a DVD and therefore
253 // we should encounter no "transmission errors".
254 cc_decode_EIA608(data1 | (data2 << 8));
255 skip = 5;
256 break;
258 case 0x00:
259 /* This seems to be just padding */
260 skip = 2;
261 break;
263 case 0x01:
264 odd_offset = data2 & 0x80;
265 if (odd_offset)
266 skip = 2;
267 else
268 skip = 5;
269 break;
271 default:
272 //#ifdef LOG_DEBUG
273 fprintf(stderr, "Unknown CC encoding: %x\n", cc_code);
274 //#endif
275 skip = 2;
276 break;
278 current += skip;
279 curbytes += skip;
284 void subcc_process_data(unsigned char *inputdata,unsigned int len)
286 if(!subcc_enabled) return;
287 if(!inited) subcc_init();
289 memcpy(inputbuffer,inputdata,len);
290 inputlength=len;
291 subcc_decode();