Fix braindead and broken way to calculate abase, fixes regression tests on
[mplayer/glamo.git] / sub_cc.c
blob2acebf01bcca9dbc0fef96c95899a9676f90d7fc
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 initialized=0;
40 #define CC_ROLLON 1
41 #define CC_ROLLUP 2
43 static int cc_mode=CC_ROLLON;
44 static int cc_lines=4; ///< number of visible rows in CC roll-up mode, not used in CC roll-on mode
46 static void display_buffer(subtitle * buf);
48 static void build_char_table(void)
50 int i;
51 /* first the normal ASCII codes */
52 for (i = 0; i < 128; i++)
53 chartbl[i] = (char) i;
54 /* now the special codes */
55 chartbl[0x2a] = 'á';
56 chartbl[0x5c] = 'é';
57 chartbl[0x5e] = 'í';
58 chartbl[0x5f] = 'ó';
59 chartbl[0x60] = 'ú';
60 chartbl[0x7b] = 'ç';
61 chartbl[0x7c] = '÷';
62 chartbl[0x7d] = 'Ñ';
63 chartbl[0x7e] = 'ñ';
64 chartbl[0x7f] = '¤'; /* FIXME: this should be a solid block */
67 static void clear_buffer(subtitle *buf)
69 int i;
70 buf->lines=0;
71 for(i=0;i<SUB_MAX_TEXT;i++) if(buf->text[i]) {free(buf->text[i]);buf->text[i]=NULL;}
75 /**
76 \brief scroll buffer one line up
77 \param buf buffer to scroll
79 static void scroll_buffer(subtitle* buf)
81 int i;
83 while(buf->lines > cc_lines)
85 if(buf->text[0]) free(buf->text[0]);
87 for(i = 0; i < (buf->lines - 1); i++) buf->text[i] = buf->text[i+1];
89 buf->text[buf->lines-1] = NULL;
90 buf->lines--;
95 void subcc_init(void)
97 int i;
98 //printf("subcc_init(): initing...\n");
99 build_char_table();
100 for(i=0;i<SUB_MAX_TEXT;i++) {buf1.text[i]=buf2.text[i]=NULL;}
101 buf1.lines=buf2.lines=0;
102 fb=&buf1;
103 bb=&buf2;
105 initialized=1;
108 static void append_char(char c)
110 if(!bb->lines) {bb->lines++; cursor_pos=0;}
111 if(bb->text[bb->lines - 1]==NULL)
113 bb->text[bb->lines - 1]=malloc(CC_MAX_LINE_LENGTH);
114 memset(bb->text[bb->lines - 1],0,CC_MAX_LINE_LENGTH);
115 cursor_pos=0;
118 if(c=='\n')
120 if(cursor_pos>0 && bb->lines < SUB_MAX_TEXT)
122 bb->lines++;cursor_pos=0;
123 if(cc_mode==CC_ROLLUP){ //Carriage return - scroll buffer one line up
124 bb->text[bb->lines - 1]=calloc(1, CC_MAX_LINE_LENGTH);
125 scroll_buffer(bb);
129 else
131 if(cursor_pos==CC_MAX_LINE_LENGTH-1)
133 fprintf(stderr,"sub_cc.c: append_char() reached CC_MAX_LINE_LENGTH!\n");
134 return;
136 bb->text[bb->lines - 1][cursor_pos++]=c;
138 //In CC roll-up mode data should be shown immediately
139 if(cc_mode==CC_ROLLUP) display_buffer(bb);
143 static void swap_buffers(void)
145 subtitle *foo;
146 foo=fb;
147 fb=bb;
148 bb=foo;
151 static void display_buffer(subtitle * buf)
153 vo_sub=buf;
154 vo_osd_changed(OSDTYPE_SUBTITLE);
158 static void cc_decode_EIA608(unsigned short int data)
161 static unsigned short int lastcode=0x0000;
162 unsigned char c1 = data & 0x7f;
163 unsigned char c2 = (data >> 8) & 0x7f;
165 if (c1 & 0x60) { /* normal character, 0x20 <= c1 <= 0x7f */
166 append_char(chartbl[c1]);
167 if(c2 & 0x60) /*c2 might not be a normal char even if c1 is*/
168 append_char(chartbl[c2]);
170 else if (c1 & 0x10) // control code / special char
172 // int channel= (c1 & 0x08) >> 3;
173 c1&=~0x08;
174 if(data!=lastcode)
176 if(c2 & 0x40) { /*PAC, Preamble Address Code */
177 append_char('\n'); /*FIXME properly interpret PACs*/
179 else
180 switch(c1)
182 case 0x10: break; // ext attribute
183 case 0x11:
184 if((c2 & 0x30)==0x30)
186 //printf("[debug]:Special char (ignored)\n");
187 /*cc_decode_special_char()*/;
189 else if (c2 & 0x20)
191 //printf("[debug]: midrow_attr (ignored)\n");
192 /*cc_decode_midrow_attr()*/;
194 break;
195 case 0x14:
196 switch(c2)
198 case 0x00: //CC roll-on mode
199 cc_mode=CC_ROLLON;
200 break;
201 case 0x25: //CC roll-up, 2 rows
202 case 0x26: //CC roll-up, 3 rows
203 case 0x27: //CC roll-up, 4 rows
204 cc_lines=c2-0x23;
205 cc_mode=CC_ROLLUP;
206 break;
207 case 0x2C: display_buffer(NULL); //EDM
208 clear_buffer(fb); break;
209 case 0x2d: append_char('\n'); //carriage return
210 break;
211 case 0x2e: clear_buffer(bb); //ENM
212 break;
213 case 0x2f: swap_buffers(); //Swap buffers
214 display_buffer(fb);
215 clear_buffer(bb);
216 break;
218 break;
219 case 0x17:
220 if( c2>=0x21 && c2<=0x23) //TAB
222 break;
227 lastcode=data;
230 static void subcc_decode(unsigned char *inputbuffer, unsigned int inputlength)
232 /* The first number may denote a channel number. I don't have the
233 * EIA-708 standard, so it is hard to say.
234 * From what I could figure out so far, the general format seems to be:
236 * repeat
238 * 0xfe starts 2 byte sequence of unknown purpose. It might denote
239 * field #2 in line 21 of the VBI. We'll ignore it for the
240 * time being.
242 * 0xff starts 2 byte EIA-608 sequence, field #1 in line 21 of the VBI.
243 * Followed by a 3-code triplet that starts either with 0xff or
244 * 0xfe. In either case, the following triplet needs to be ignored
245 * for line 21, field 1.
247 * 0x00 is padding, followed by 2 more 0x00.
249 * 0x01 always seems to appear at the beginning, always seems to
250 * be followed by 0xf8, 8-bit number.
251 * The lower 7 bits of this 8-bit number seem to denote the
252 * number of code triplets that follow.
253 * The most significant bit denotes whether the Line 21 field 1
254 * captioning information is at odd or even triplet offsets from this
255 * beginning triplet. 1 denotes odd offsets, 0 denotes even offsets.
257 * Most captions are encoded with odd offsets, so this is what we
258 * will assume.
260 * until end of packet
262 unsigned char *current = inputbuffer;
263 unsigned int curbytes = 0;
264 unsigned char data1, data2;
265 unsigned char cc_code;
266 int odd_offset = 1;
268 while (curbytes < inputlength) {
269 int skip = 2;
271 cc_code = *(current);
273 if (inputlength - curbytes < 2) {
274 #ifdef LOG_DEBUG
275 fprintf(stderr, "Not enough data for 2-byte CC encoding\n");
276 #endif
277 break;
280 data1 = *(current+1);
281 data2 = *(current + 2);
282 current++; curbytes++;
284 switch (cc_code) {
285 case 0xfe:
286 /* expect 2 byte encoding (perhaps CC3, CC4?) */
287 /* ignore for time being */
288 skip = 2;
289 break;
291 case 0xff:
292 /* expect EIA-608 CC1/CC2 encoding */
293 // FIXME check parity!
294 // Parity check omitted assuming we are reading from a DVD and therefore
295 // we should encounter no "transmission errors".
296 cc_decode_EIA608(data1 | (data2 << 8));
297 skip = 5;
298 break;
300 case 0x00:
301 /* This seems to be just padding */
302 skip = 2;
303 break;
305 case 0x01:
306 odd_offset = data2 & 0x80;
307 if (odd_offset)
308 skip = 2;
309 else
310 skip = 5;
311 break;
313 default:
314 //#ifdef LOG_DEBUG
315 fprintf(stderr, "Unknown CC encoding: %x\n", cc_code);
316 //#endif
317 skip = 2;
318 break;
320 current += skip;
321 curbytes += skip;
326 void subcc_process_data(unsigned char *inputdata,unsigned int len)
328 if(!subcc_enabled) return;
329 if(!initialized) subcc_init();
331 subcc_decode(inputdata, len);