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.
7 * Most notably, only the text information is decoded as of now, discarding color,
8 * background and position info (see source below).
12 * uses source from the xine closed captions decoder
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;
40 static unsigned char inputbuffer
[CC_INPUTBUFFER_SIZE
];
41 static unsigned int inputlength
;
43 static void build_char_table(void)
46 /* first the normal ASCII codes */
47 for (i
= 0; i
< 128; i
++)
48 chartbl
[i
] = (char) i
;
49 /* now the special codes */
59 chartbl
[0x7f] = '¤'; /* FIXME: this should be a solid block */
62 static void clear_buffer(subtitle
*buf
)
66 for(i
=0;i
<SUB_MAX_TEXT
;i
++) if(buf
->text
[i
]) {free(buf
->text
[i
]);buf
->text
[i
]=NULL
;}
73 //printf("subcc_init(): initing...\n");
75 for(i
=0;i
<SUB_MAX_TEXT
;i
++) {buf1
.text
[i
]=buf2
.text
[i
]=NULL
;}
76 buf1
.lines
=buf2
.lines
=0;
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
);
96 bb
->lines
++;cursor_pos
=0;
100 if(cursor_pos
==CC_MAX_LINE_LENGTH
-1)
102 printf("sub_cc.c: append_char() reached CC_MAX_LINE_LENGTH!\n");
105 bb
->text
[bb
->lines
- 1][cursor_pos
++]=c
;
110 static void swap_buffers()
118 static void display_buffer(subtitle
* 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;
143 if(c2
& 0x40) { /*PAC, Preamble Address Code */
144 append_char('\n'); /*FIXME properly interpret PACs*/
149 case 0x10: break; // ext attribute
151 if((c2
& 0x30)==0x30)
153 //printf("[debug]:Special char (ignored)\n");
154 /*cc_decode_special_char()*/;
158 //printf("[debug]: midrow_attr (ignored)\n");
159 /*cc_decode_midrow_attr()*/;
165 case 0x2C: display_buffer(NULL
); //EDM
166 clear_buffer(fb
); break;
167 case 0x2d: append_char('\n'); //carriage return
169 case 0x2e: clear_buffer(bb
); //ENM
171 case 0x2f: swap_buffers(); //Swap buffers
178 if( c2
>=0x21 && c2
<=0x23) //TAB
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:
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
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
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
;
226 while (curbytes
< inputlength
) {
229 cc_code
= *(current
);
231 if (inputlength
- curbytes
< 2) {
233 fprintf(stderr
, "Not enough data for 2-byte CC encoding\n");
238 data1
= *(current
+1);
239 data2
= *(current
+ 2);
240 current
++; curbytes
++;
244 /* expect 2 byte encoding (perhaps CC3, CC4?) */
245 /* ignore for time being */
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));
259 /* This seems to be just padding */
264 odd_offset
= data2
& 0x80;
273 fprintf(stderr
, "Unknown CC encoding: %x\n", cc_code
);
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
);