Changed code that used pts=0 in demux packets to indicate "not known".
[mplayer.git] / sub_cc.c
blob2e3123f7bfe2dbc1f9832240e6d5818b38c4d102
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_INPUTBUFFER_SIZE 256
31 #define CC_MAX_LINE_LENGTH 64
33 static char chartbl[128];
35 static subtitle buf1,buf2;
36 static subtitle *fb,*bb;
38 static unsigned int cursor_pos=0;
40 static int inited=0;
41 static unsigned char inputbuffer[CC_INPUTBUFFER_SIZE];
42 static unsigned int inputlength;
44 static void build_char_table(void)
46 int i;
47 /* first the normal ASCII codes */
48 for (i = 0; i < 128; i++)
49 chartbl[i] = (char) i;
50 /* now the special codes */
51 chartbl[0x2a] = 'á';
52 chartbl[0x5c] = 'é';
53 chartbl[0x5e] = 'í';
54 chartbl[0x5f] = 'ó';
55 chartbl[0x60] = 'ú';
56 chartbl[0x7b] = 'ç';
57 chartbl[0x7c] = '÷';
58 chartbl[0x7d] = 'Ñ';
59 chartbl[0x7e] = 'ñ';
60 chartbl[0x7f] = '¤'; /* FIXME: this should be a solid block */
63 static void clear_buffer(subtitle *buf)
65 int i;
66 buf->lines=0;
67 for(i=0;i<SUB_MAX_TEXT;i++) if(buf->text[i]) {free(buf->text[i]);buf->text[i]=NULL;}
71 void subcc_init(void)
73 int i;
74 //printf("subcc_init(): initing...\n");
75 build_char_table();
76 for(i=0;i<SUB_MAX_TEXT;i++) {buf1.text[i]=buf2.text[i]=NULL;}
77 buf1.lines=buf2.lines=0;
78 fb=&buf1;
79 bb=&buf2;
81 inited=1;
84 static void append_char(char c)
86 if(!bb->lines) {bb->lines++; cursor_pos=0;}
87 if(bb->text[bb->lines - 1]==NULL)
89 bb->text[bb->lines - 1]=malloc(CC_MAX_LINE_LENGTH);
90 memset(bb->text[bb->lines - 1],0,CC_MAX_LINE_LENGTH);
91 cursor_pos=0;
94 if(c=='\n')
96 if(cursor_pos>0)
97 bb->lines++;cursor_pos=0;
99 else
101 if(cursor_pos==CC_MAX_LINE_LENGTH-1)
103 fprintf(stderr,"sub_cc.c: append_char() reached CC_MAX_LINE_LENGTH!\n");
104 return;
106 bb->text[bb->lines - 1][cursor_pos++]=c;
111 static void swap_buffers(void)
113 subtitle *foo;
114 foo=fb;
115 fb=bb;
116 bb=foo;
119 static void display_buffer(subtitle * buf)
121 vo_sub=buf;
122 vo_osd_changed(OSDTYPE_SUBTITLE);
126 static void cc_decode_EIA608(unsigned short int data)
129 static unsigned short int lastcode=0x0000;
130 unsigned char c1 = data & 0x7f;
131 unsigned char c2 = (data >> 8) & 0x7f;
133 if (c1 & 0x60) { /* normal character, 0x20 <= c1 <= 0x7f */
134 append_char(chartbl[c1]);
135 if(c2 & 0x60) /*c2 might not be a normal char even if c1 is*/
136 append_char(chartbl[c2]);
138 else if (c1 & 0x10) // control code / special char
140 // int channel= (c1 & 0x08) >> 3;
141 c1&=~0x08;
142 if(data!=lastcode)
144 if(c2 & 0x40) { /*PAC, Preamble Address Code */
145 append_char('\n'); /*FIXME properly interpret PACs*/
147 else
148 switch(c1)
150 case 0x10: break; // ext attribute
151 case 0x11:
152 if((c2 & 0x30)==0x30)
154 //printf("[debug]:Special char (ignored)\n");
155 /*cc_decode_special_char()*/;
157 else if (c2 & 0x20)
159 //printf("[debug]: midrow_attr (ignored)\n");
160 /*cc_decode_midrow_attr()*/;
162 break;
163 case 0x14:
164 switch(c2)
166 case 0x2C: display_buffer(NULL); //EDM
167 clear_buffer(fb); break;
168 case 0x2d: append_char('\n'); //carriage return
169 break;
170 case 0x2e: clear_buffer(bb); //ENM
171 break;
172 case 0x2f: swap_buffers(); //Swap buffers
173 display_buffer(fb);
174 clear_buffer(bb);
175 break;
177 break;
178 case 0x17:
179 if( c2>=0x21 && c2<=0x23) //TAB
181 break;
186 lastcode=data;
189 static void subcc_decode(void)
191 /* The first number may denote a channel number. I don't have the
192 * EIA-708 standard, so it is hard to say.
193 * From what I could figure out so far, the general format seems to be:
195 * repeat
197 * 0xfe starts 2 byte sequence of unknown purpose. It might denote
198 * field #2 in line 21 of the VBI. We'll ignore it for the
199 * time being.
201 * 0xff starts 2 byte EIA-608 sequence, field #1 in line 21 of the VBI.
202 * Followed by a 3-code triplet that starts either with 0xff or
203 * 0xfe. In either case, the following triplet needs to be ignored
204 * for line 21, field 1.
206 * 0x00 is padding, followed by 2 more 0x00.
208 * 0x01 always seems to appear at the beginning, always seems to
209 * be followed by 0xf8, 8-bit number.
210 * The lower 7 bits of this 8-bit number seem to denote the
211 * number of code triplets that follow.
212 * The most significant bit denotes whether the Line 21 field 1
213 * captioning information is at odd or even triplet offsets from this
214 * beginning triplet. 1 denotes odd offsets, 0 denotes even offsets.
216 * Most captions are encoded with odd offsets, so this is what we
217 * will assume.
219 * until end of packet
221 unsigned char *current = inputbuffer;
222 unsigned int curbytes = 0;
223 unsigned char data1, data2;
224 unsigned char cc_code;
225 int odd_offset = 1;
227 while (curbytes < inputlength) {
228 int skip = 2;
230 cc_code = *(current);
232 if (inputlength - curbytes < 2) {
233 #ifdef LOG_DEBUG
234 fprintf(stderr, "Not enough data for 2-byte CC encoding\n");
235 #endif
236 break;
239 data1 = *(current+1);
240 data2 = *(current + 2);
241 current++; curbytes++;
243 switch (cc_code) {
244 case 0xfe:
245 /* expect 2 byte encoding (perhaps CC3, CC4?) */
246 /* ignore for time being */
247 skip = 2;
248 break;
250 case 0xff:
251 /* expect EIA-608 CC1/CC2 encoding */
252 // FIXME check parity!
253 // Parity check omitted assuming we are reading from a DVD and therefore
254 // we should encounter no "transmission errors".
255 cc_decode_EIA608(data1 | (data2 << 8));
256 skip = 5;
257 break;
259 case 0x00:
260 /* This seems to be just padding */
261 skip = 2;
262 break;
264 case 0x01:
265 odd_offset = data2 & 0x80;
266 if (odd_offset)
267 skip = 2;
268 else
269 skip = 5;
270 break;
272 default:
273 //#ifdef LOG_DEBUG
274 fprintf(stderr, "Unknown CC encoding: %x\n", cc_code);
275 //#endif
276 skip = 2;
277 break;
279 current += skip;
280 curbytes += skip;
285 void subcc_process_data(unsigned char *inputdata,unsigned int len)
287 if(!subcc_enabled) return;
288 if(!inited) subcc_init();
290 memcpy(inputbuffer,inputdata,len);
291 inputlength=len;
292 subcc_decode();