codecs.conf: Prefer FFmpeg AAC decoder over libfaad2
[mplayer/glamo.git] / sub_cc.c
blobc447dd0bd3c808da0ef7f7c3e10b1bacfc1adf1c
1 /*
2 * decoder for Closed Captions
4 * This decoder relies on MPlayer's OSD to display subtitles.
5 * Be warned that decoding is somewhat preliminary, though it basically works.
7 * Most notably, only the text information is decoded as of now, discarding
8 * color, background and position info (see source below).
10 * uses source from the xine closed captions decoder
12 * Copyright (C) 2002 Matteo Giani
14 * This file is part of MPlayer.
16 * MPlayer is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * MPlayer is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include "config.h"
36 #include "sub_cc.h"
38 #include "subreader.h"
40 #include "libvo/video_out.h"
41 #include "libvo/sub.h"
44 #define CC_MAX_LINE_LENGTH 64
46 static char chartbl[128];
48 static subtitle buf1,buf2;
49 static subtitle *fb,*bb;
51 static unsigned int cursor_pos=0;
53 static int initialized=0;
55 #define CC_ROLLON 1
56 #define CC_ROLLUP 2
58 static int cc_mode=CC_ROLLON;
59 static int cc_lines=4; ///< number of visible rows in CC roll-up mode, not used in CC roll-on mode
61 static void build_char_table(void)
63 int i;
64 /* first the normal ASCII codes */
65 for (i = 0; i < 128; i++)
66 chartbl[i] = (char) i;
67 /* now the special codes */
68 chartbl[0x2a] = 'á';
69 chartbl[0x5c] = 'é';
70 chartbl[0x5e] = 'í';
71 chartbl[0x5f] = 'ó';
72 chartbl[0x60] = 'ú';
73 chartbl[0x7b] = 'ç';
74 chartbl[0x7c] = '÷';
75 chartbl[0x7d] = 'Ñ';
76 chartbl[0x7e] = 'ñ';
77 chartbl[0x7f] = '¤'; /* FIXME: this should be a solid block */
80 static void clear_buffer(subtitle *buf)
82 int i;
83 buf->lines=0;
84 for (i = 0; i < SUB_MAX_TEXT; i++) {
85 free(buf->text[i]);
86 buf->text[i] = NULL;
91 /**
92 \brief scroll buffer one line up
93 \param buf buffer to scroll
95 static void scroll_buffer(subtitle* buf)
97 int i;
99 while(buf->lines > cc_lines)
101 free(buf->text[0]);
103 for(i = 0; i < buf->lines - 1; i++) buf->text[i] = buf->text[i+1];
105 buf->text[buf->lines-1] = NULL;
106 buf->lines--;
111 void subcc_init(void)
113 int i;
114 //printf("subcc_init(): initing...\n");
115 build_char_table();
116 for(i=0;i<SUB_MAX_TEXT;i++) {buf1.text[i]=buf2.text[i]=NULL;}
117 buf1.lines=buf2.lines=0;
118 fb=&buf1;
119 bb=&buf2;
121 initialized=1;
125 static void display_buffer(subtitle *buf)
127 vo_sub = buf;
128 vo_osd_changed(OSDTYPE_SUBTITLE);
132 static void append_char(char c)
134 if(!bb->lines) {bb->lines++; cursor_pos=0;}
135 if(bb->text[bb->lines - 1]==NULL)
137 bb->text[bb->lines - 1] = calloc(1, CC_MAX_LINE_LENGTH);
138 cursor_pos=0;
141 if(c=='\n')
143 if(cursor_pos>0 && bb->lines < SUB_MAX_TEXT)
145 bb->lines++;cursor_pos=0;
146 if(cc_mode==CC_ROLLUP){ //Carriage return - scroll buffer one line up
147 bb->text[bb->lines - 1]=calloc(1, CC_MAX_LINE_LENGTH);
148 scroll_buffer(bb);
152 else
154 if(cursor_pos==CC_MAX_LINE_LENGTH-1)
156 fprintf(stderr,"CC: append_char() reached CC_MAX_LINE_LENGTH!\n");
157 return;
159 bb->text[bb->lines - 1][cursor_pos++]=c;
161 //In CC roll-up mode data should be shown immediately
162 if(cc_mode==CC_ROLLUP) display_buffer(bb);
166 static void swap_buffers(void)
168 subtitle *foo;
169 foo=fb;
170 fb=bb;
171 bb=foo;
175 static void cc_decode_EIA608(unsigned short int data)
178 static unsigned short int lastcode=0x0000;
179 unsigned char c1 = data & 0x7f;
180 unsigned char c2 = (data >> 8) & 0x7f;
182 if (c1 & 0x60) { /* normal character, 0x20 <= c1 <= 0x7f */
183 append_char(chartbl[c1]);
184 if(c2 & 0x60) /*c2 might not be a normal char even if c1 is*/
185 append_char(chartbl[c2]);
187 else if (c1 & 0x10) // control code / special char
189 // int channel= (c1 & 0x08) >> 3;
190 c1&=~0x08;
191 if(data!=lastcode)
193 if(c2 & 0x40) { /*PAC, Preamble Address Code */
194 append_char('\n'); /*FIXME properly interpret PACs*/
196 else
197 switch(c1)
199 case 0x10: break; // ext attribute
200 case 0x11:
201 if((c2 & 0x30)==0x30)
203 //printf("[debug]:Special char (ignored)\n");
204 /*cc_decode_special_char()*/;
206 else if (c2 & 0x20)
208 //printf("[debug]: midrow_attr (ignored)\n");
209 /*cc_decode_midrow_attr()*/;
211 break;
212 case 0x14:
213 switch(c2)
215 case 0x00: //CC roll-on mode
216 cc_mode=CC_ROLLON;
217 break;
218 case 0x25: //CC roll-up, 2 rows
219 case 0x26: //CC roll-up, 3 rows
220 case 0x27: //CC roll-up, 4 rows
221 cc_lines=c2-0x23;
222 cc_mode=CC_ROLLUP;
223 break;
224 case 0x2C: display_buffer(NULL); //EDM
225 clear_buffer(fb); break;
226 case 0x2d: append_char('\n'); //carriage return
227 break;
228 case 0x2e: clear_buffer(bb); //ENM
229 break;
230 case 0x2f: swap_buffers(); //Swap buffers
231 display_buffer(fb);
232 clear_buffer(bb);
233 break;
235 break;
236 case 0x17:
237 if( c2>=0x21 && c2<=0x23) //TAB
239 break;
244 lastcode=data;
247 static void subcc_decode(unsigned char *inputbuffer, unsigned int inputlength)
249 /* The first number may denote a channel number. I don't have the
250 * EIA-708 standard, so it is hard to say.
251 * From what I could figure out so far, the general format seems to be:
253 * repeat
255 * 0xfe starts 2 byte sequence of unknown purpose. It might denote
256 * field #2 in line 21 of the VBI. We'll ignore it for the
257 * time being.
259 * 0xff starts 2 byte EIA-608 sequence, field #1 in line 21 of the VBI.
260 * Followed by a 3-code triplet that starts either with 0xff or
261 * 0xfe. In either case, the following triplet needs to be ignored
262 * for line 21, field 1.
264 * 0x00 is padding, followed by 2 more 0x00.
266 * 0x01 always seems to appear at the beginning, always seems to
267 * be followed by 0xf8, 8-bit number.
268 * The lower 7 bits of this 8-bit number seem to denote the
269 * number of code triplets that follow.
270 * The most significant bit denotes whether the Line 21 field 1
271 * captioning information is at odd or even triplet offsets from this
272 * beginning triplet. 1 denotes odd offsets, 0 denotes even offsets.
274 * Most captions are encoded with odd offsets, so this is what we
275 * will assume.
277 * until end of packet
279 unsigned char *current = inputbuffer;
280 unsigned int curbytes = 0;
281 unsigned char data1, data2;
282 unsigned char cc_code;
283 int odd_offset = 1;
285 while (curbytes < inputlength) {
286 int skip = 2;
288 cc_code = current[0];
290 if (inputlength - curbytes < 2) {
291 #ifdef LOG_DEBUG
292 fprintf(stderr, "Not enough data for 2-byte CC encoding\n");
293 #endif
294 break;
297 data1 = current[1];
298 data2 = current[2];
299 current++; curbytes++;
301 switch (cc_code) {
302 case 0xfe:
303 /* expect 2 byte encoding (perhaps CC3, CC4?) */
304 /* ignore for time being */
305 break;
307 case 0xff:
308 /* expect EIA-608 CC1/CC2 encoding */
309 // FIXME check parity!
310 // Parity check omitted assuming we are reading from a DVD and therefore
311 // we should encounter no "transmission errors".
312 cc_decode_EIA608(data1 | (data2 << 8));
313 skip = 5;
314 break;
316 case 0x00:
317 /* This seems to be just padding */
318 break;
320 case 0x01:
321 odd_offset = data2 & 0x80;
322 if (!odd_offset)
323 skip = 5;
324 break;
326 default:
327 //#ifdef LOG_DEBUG
328 fprintf(stderr, "Unknown CC encoding: %x\n", cc_code);
329 //#endif
330 break;
332 current += skip;
333 curbytes += skip;
338 void subcc_process_data(unsigned char *inputdata,unsigned int len)
340 if(!subcc_enabled) return;
341 if(!initialized) subcc_init();
343 subcc_decode(inputdata, len);