audio: improve decoder open failure handling
[mplayer.git] / sub / sub_cc.c
blob8919ce84f4f0236ae1ec0d52e09207111d82ee48
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 "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 int subcc_enabled = 0;
63 static void build_char_table(void)
65 int i;
66 /* first the normal ASCII codes */
67 for (i = 0; i < 128; i++)
68 chartbl[i] = (char) i;
69 /* now the special codes */
70 chartbl[0x2a] = 'á';
71 chartbl[0x5c] = 'é';
72 chartbl[0x5e] = 'í';
73 chartbl[0x5f] = 'ó';
74 chartbl[0x60] = 'ú';
75 chartbl[0x7b] = 'ç';
76 chartbl[0x7c] = '÷';
77 chartbl[0x7d] = 'Ñ';
78 chartbl[0x7e] = 'ñ';
79 chartbl[0x7f] = '¤'; /* FIXME: this should be a solid block */
82 static void clear_buffer(subtitle *buf)
84 int i;
85 buf->lines=0;
86 for (i = 0; i < SUB_MAX_TEXT; i++) {
87 free(buf->text[i]);
88 buf->text[i] = NULL;
93 /**
94 \brief scroll buffer one line up
95 \param buf buffer to scroll
97 static void scroll_buffer(subtitle* buf)
99 int i;
101 while(buf->lines > cc_lines)
103 free(buf->text[0]);
105 for(i = 0; i < buf->lines - 1; i++) buf->text[i] = buf->text[i+1];
107 buf->text[buf->lines-1] = NULL;
108 buf->lines--;
112 static int channel;
114 void subcc_init(void)
116 int i;
117 //printf("subcc_init(): initing...\n");
118 build_char_table();
119 for(i=0;i<SUB_MAX_TEXT;i++) {buf1.text[i]=buf2.text[i]=NULL;}
120 buf1.lines=buf2.lines=0;
121 fb=&buf1;
122 bb=&buf2;
123 channel = -1;
125 initialized=1;
129 static void display_buffer(subtitle *buf)
131 vo_sub = buf;
132 vo_osd_changed(OSDTYPE_SUBTITLE);
136 static void append_char(char c)
138 if(!bb->lines) {bb->lines++; cursor_pos=0;}
139 if(bb->text[bb->lines - 1]==NULL)
141 bb->text[bb->lines - 1] = calloc(1, CC_MAX_LINE_LENGTH);
142 cursor_pos=0;
145 if(c=='\n')
147 if(cursor_pos>0 && bb->lines < SUB_MAX_TEXT)
149 bb->lines++;cursor_pos=0;
150 if(cc_mode==CC_ROLLUP){ //Carriage return - scroll buffer one line up
151 bb->text[bb->lines - 1]=calloc(1, CC_MAX_LINE_LENGTH);
152 scroll_buffer(bb);
156 else
158 if(cursor_pos==CC_MAX_LINE_LENGTH-1)
160 fprintf(stderr,"CC: append_char() reached CC_MAX_LINE_LENGTH!\n");
161 return;
163 bb->text[bb->lines - 1][cursor_pos++]=c;
165 //In CC roll-up mode data should be shown immediately
166 if(cc_mode==CC_ROLLUP) display_buffer(bb);
170 static void swap_buffers(void)
172 subtitle *foo;
173 foo=fb;
174 fb=bb;
175 bb=foo;
178 static int selected_channel(void)
180 return subcc_enabled - 1;
183 static void cc_decode_EIA608(unsigned short int data)
186 static unsigned short int lastcode=0x0000;
187 uint8_t c1 = data & 0x7f;
188 uint8_t c2 = (data >> 8) & 0x7f;
190 if (c1 & 0x60) { /* normal character, 0x20 <= c1 <= 0x7f */
191 if (channel != (selected_channel() & 1))
192 return;
193 append_char(chartbl[c1]);
194 if(c2 & 0x60) /*c2 might not be a normal char even if c1 is*/
195 append_char(chartbl[c2]);
197 else if (c1 & 0x10) // control code / special char
199 channel = (c1 & 0x08) >> 3;
200 if (channel != (selected_channel() & 1))
201 return;
202 c1&=~0x08;
203 if(data!=lastcode)
205 if(c2 & 0x40) { /*PAC, Preamble Address Code */
206 append_char('\n'); /*FIXME properly interpret PACs*/
208 else
209 switch(c1)
211 case 0x10: break; // ext attribute
212 case 0x11:
213 if((c2 & 0x30)==0x30)
215 //printf("[debug]:Special char (ignored)\n");
216 /*cc_decode_special_char()*/;
218 else if (c2 & 0x20)
220 //printf("[debug]: midrow_attr (ignored)\n");
221 /*cc_decode_midrow_attr()*/;
223 break;
224 case 0x14:
225 switch(c2)
227 case 0x00: //CC roll-on mode
228 cc_mode=CC_ROLLON;
229 break;
230 case 0x25: //CC roll-up, 2 rows
231 case 0x26: //CC roll-up, 3 rows
232 case 0x27: //CC roll-up, 4 rows
233 cc_lines=c2-0x23;
234 cc_mode=CC_ROLLUP;
235 break;
236 case 0x2C: display_buffer(NULL); //EDM
237 clear_buffer(fb); break;
238 case 0x2d: append_char('\n'); //carriage return
239 break;
240 case 0x2e: clear_buffer(bb); //ENM
241 break;
242 case 0x2f: swap_buffers(); //Swap buffers
243 display_buffer(fb);
244 clear_buffer(bb);
245 break;
247 break;
248 case 0x17:
249 if( c2>=0x21 && c2<=0x23) //TAB
251 break;
256 lastcode=data;
259 static void subcc_decode(const uint8_t *inputbuffer, unsigned int inputlength)
261 /* The first number may denote a channel number. I don't have the
262 * EIA-708 standard, so it is hard to say.
263 * From what I could figure out so far, the general format seems to be:
265 * repeat
267 * 0xfe starts 2 byte sequence of unknown purpose. It might denote
268 * field #2 in line 21 of the VBI.
269 * Treating it identical of 0xff fixes
270 * http://samples.mplayerhq.hu/MPEG-VOB/ClosedCaptions/Starship_Troopers.vob
272 * 0xff starts 2 byte EIA-608 sequence, field #1 in line 21 of the VBI.
273 * Followed by a 3-code triplet that starts either with 0xff or
274 * 0xfe. In either case, the following triplet needs to be ignored
275 * for line 21, field 1.
277 * 0x00 is padding, followed by 2 more 0x00.
279 * 0x01 always seems to appear at the beginning, always seems to
280 * be followed by 0xf8, 8-bit number.
281 * The lower 7 bits of this 8-bit number seem to denote the
282 * number of code triplets that follow.
283 * The most significant bit denotes whether the Line 21 field 1
284 * captioning information is at odd or even triplet offsets from this
285 * beginning triplet. 1 denotes odd offsets, 0 denotes even offsets.
287 * Most captions are encoded with odd offsets, so this is what we
288 * will assume.
290 * until end of packet
292 const uint8_t *current = inputbuffer;
293 unsigned int curbytes = 0;
294 uint8_t data1, data2;
295 uint8_t cc_code;
296 int odd_offset = 1;
298 while (curbytes < inputlength) {
299 cc_code = current[0];
301 if (inputlength - curbytes < 2) {
302 #ifdef LOG_DEBUG
303 fprintf(stderr, "Not enough data for 2-byte CC encoding\n");
304 #endif
305 break;
308 data1 = current[1];
309 data2 = current[2];
310 current += 3; curbytes += 3;
312 switch (cc_code) {
313 case 0xfe:
314 case 0xff:
315 odd_offset ^= 1;
316 if (odd_offset != selected_channel() >> 1)
317 break;
318 /* expect EIA-608 CC1/CC2 encoding */
319 // FIXME check parity!
320 // Parity check omitted assuming we are reading from a DVD and therefore
321 // we should encounter no "transmission errors".
322 cc_decode_EIA608(data1 | (data2 << 8));
323 break;
325 case 0x00:
326 /* This seems to be just padding */
327 break;
329 case 0x01:
330 odd_offset = data2 >> 7;
331 break;
333 default:
334 //#ifdef LOG_DEBUG
335 fprintf(stderr, "Unknown CC encoding: %x\n", cc_code);
336 //#endif
337 break;
343 void subcc_process_data(const uint8_t *inputdata, unsigned int len)
345 if(!subcc_enabled) return;
346 if(!initialized) subcc_init();
348 subcc_decode(inputdata, len);