2002-07-25 03:37:28 +00:00
|
|
|
|
/*
|
2009-03-15 12:45:16 +00:00
|
|
|
|
* decoder for Closed Captions
|
2002-07-25 03:37:28 +00:00
|
|
|
|
*
|
|
|
|
|
* This decoder relies on MPlayer's OSD to display subtitles.
|
2009-05-12 02:58:29 +00:00
|
|
|
|
* Be warned that decoding is somewhat preliminary, though it basically works.
|
2009-05-12 02:45:52 +00:00
|
|
|
|
*
|
2009-05-12 02:58:29 +00:00
|
|
|
|
* Most notably, only the text information is decoded as of now, discarding
|
|
|
|
|
* color, background and position info (see source below).
|
2009-05-12 02:45:52 +00:00
|
|
|
|
*
|
2002-07-25 03:37:28 +00:00
|
|
|
|
* uses source from the xine closed captions decoder
|
2010-01-30 23:24:23 +00:00
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2002 Matteo Giani
|
|
|
|
|
*
|
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
|
*
|
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2002-07-25 03:37:28 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2002-11-06 23:54:29 +00:00
|
|
|
|
#include <string.h>
|
2002-07-25 03:37:28 +00:00
|
|
|
|
|
2006-02-16 10:08:03 +00:00
|
|
|
|
#include "config.h"
|
2002-07-25 16:39:22 +00:00
|
|
|
|
#include "sub_cc.h"
|
|
|
|
|
|
2002-07-25 03:37:28 +00:00
|
|
|
|
#include "subreader.h"
|
|
|
|
|
|
|
|
|
|
#include "libvo/video_out.h"
|
|
|
|
|
#include "libvo/sub.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define CC_MAX_LINE_LENGTH 64
|
|
|
|
|
|
|
|
|
|
static char chartbl[128];
|
|
|
|
|
|
2002-07-25 16:39:22 +00:00
|
|
|
|
static subtitle buf1,buf2;
|
|
|
|
|
static subtitle *fb,*bb;
|
2002-07-25 03:37:28 +00:00
|
|
|
|
|
|
|
|
|
static unsigned int cursor_pos=0;
|
|
|
|
|
|
2008-02-14 14:23:55 +00:00
|
|
|
|
static int initialized=0;
|
2002-07-25 03:37:28 +00:00
|
|
|
|
|
2007-10-27 04:46:01 +00:00
|
|
|
|
#define CC_ROLLON 1
|
|
|
|
|
#define CC_ROLLUP 2
|
|
|
|
|
|
|
|
|
|
static int cc_mode=CC_ROLLON;
|
|
|
|
|
static int cc_lines=4; ///< number of visible rows in CC roll-up mode, not used in CC roll-on mode
|
|
|
|
|
|
2002-07-25 03:37:28 +00:00
|
|
|
|
static void build_char_table(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
/* first the normal ASCII codes */
|
|
|
|
|
for (i = 0; i < 128; i++)
|
|
|
|
|
chartbl[i] = (char) i;
|
|
|
|
|
/* now the special codes */
|
|
|
|
|
chartbl[0x2a] = '<EFBFBD>';
|
|
|
|
|
chartbl[0x5c] = '<EFBFBD>';
|
|
|
|
|
chartbl[0x5e] = '<EFBFBD>';
|
|
|
|
|
chartbl[0x5f] = '<EFBFBD>';
|
|
|
|
|
chartbl[0x60] = '<EFBFBD>';
|
|
|
|
|
chartbl[0x7b] = '<EFBFBD>';
|
|
|
|
|
chartbl[0x7c] = '<EFBFBD>';
|
|
|
|
|
chartbl[0x7d] = '<EFBFBD>';
|
|
|
|
|
chartbl[0x7e] = '<EFBFBD>';
|
|
|
|
|
chartbl[0x7f] = '<EFBFBD>'; /* FIXME: this should be a solid block */
|
|
|
|
|
}
|
|
|
|
|
|
2002-07-25 16:39:22 +00:00
|
|
|
|
static void clear_buffer(subtitle *buf)
|
2002-07-25 03:37:28 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
buf->lines=0;
|
2010-11-07 12:47:40 +00:00
|
|
|
|
for (i = 0; i < SUB_MAX_TEXT; i++) {
|
|
|
|
|
free(buf->text[i]);
|
|
|
|
|
buf->text[i] = NULL;
|
|
|
|
|
}
|
2002-07-25 03:37:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-10-27 04:46:01 +00:00
|
|
|
|
/**
|
|
|
|
|
\brief scroll buffer one line up
|
|
|
|
|
\param buf buffer to scroll
|
|
|
|
|
*/
|
|
|
|
|
static void scroll_buffer(subtitle* buf)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
while(buf->lines > cc_lines)
|
|
|
|
|
{
|
2010-11-07 12:47:40 +00:00
|
|
|
|
free(buf->text[0]);
|
2007-10-27 04:46:01 +00:00
|
|
|
|
|
|
|
|
|
for(i = 0; i < (buf->lines - 1); i++) buf->text[i] = buf->text[i+1];
|
|
|
|
|
|
|
|
|
|
buf->text[buf->lines-1] = NULL;
|
|
|
|
|
buf->lines--;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-05-12 02:45:52 +00:00
|
|
|
|
|
|
|
|
|
|
2006-02-09 14:08:03 +00:00
|
|
|
|
void subcc_init(void)
|
2002-07-25 03:37:28 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
//printf("subcc_init(): initing...\n");
|
|
|
|
|
build_char_table();
|
|
|
|
|
for(i=0;i<SUB_MAX_TEXT;i++) {buf1.text[i]=buf2.text[i]=NULL;}
|
|
|
|
|
buf1.lines=buf2.lines=0;
|
|
|
|
|
fb=&buf1;
|
|
|
|
|
bb=&buf2;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
|
2008-02-14 14:23:55 +00:00
|
|
|
|
initialized=1;
|
2002-07-25 03:37:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-01 18:29:35 +00:00
|
|
|
|
|
|
|
|
|
static void display_buffer(subtitle *buf)
|
|
|
|
|
{
|
|
|
|
|
vo_sub = buf;
|
|
|
|
|
vo_osd_changed(OSDTYPE_SUBTITLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-07-25 03:37:28 +00:00
|
|
|
|
static void append_char(char c)
|
|
|
|
|
{
|
|
|
|
|
if(!bb->lines) {bb->lines++; cursor_pos=0;}
|
2009-05-12 02:45:52 +00:00
|
|
|
|
if(bb->text[bb->lines - 1]==NULL)
|
2002-07-25 03:37:28 +00:00
|
|
|
|
{
|
|
|
|
|
bb->text[bb->lines - 1]=malloc(CC_MAX_LINE_LENGTH);
|
|
|
|
|
memset(bb->text[bb->lines - 1],0,CC_MAX_LINE_LENGTH);
|
|
|
|
|
cursor_pos=0;
|
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
|
2002-07-25 03:37:28 +00:00
|
|
|
|
if(c=='\n')
|
|
|
|
|
{
|
2006-11-25 22:18:40 +00:00
|
|
|
|
if(cursor_pos>0 && bb->lines < SUB_MAX_TEXT)
|
2007-10-27 04:46:01 +00:00
|
|
|
|
{
|
2002-07-25 03:37:28 +00:00
|
|
|
|
bb->lines++;cursor_pos=0;
|
2007-10-27 04:46:01 +00:00
|
|
|
|
if(cc_mode==CC_ROLLUP){ //Carriage return - scroll buffer one line up
|
|
|
|
|
bb->text[bb->lines - 1]=calloc(1, CC_MAX_LINE_LENGTH);
|
|
|
|
|
scroll_buffer(bb);
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-07-25 03:37:28 +00:00
|
|
|
|
}
|
2009-05-12 02:45:52 +00:00
|
|
|
|
else
|
2002-07-25 03:37:28 +00:00
|
|
|
|
{
|
|
|
|
|
if(cursor_pos==CC_MAX_LINE_LENGTH-1)
|
|
|
|
|
{
|
2009-03-15 12:45:16 +00:00
|
|
|
|
fprintf(stderr,"CC: append_char() reached CC_MAX_LINE_LENGTH!\n");
|
2002-07-25 03:37:28 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
bb->text[bb->lines - 1][cursor_pos++]=c;
|
|
|
|
|
}
|
2007-10-27 04:46:01 +00:00
|
|
|
|
//In CC roll-up mode data should be shown immediately
|
|
|
|
|
if(cc_mode==CC_ROLLUP) display_buffer(bb);
|
2002-07-25 03:37:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-02-09 14:08:03 +00:00
|
|
|
|
static void swap_buffers(void)
|
2002-07-25 03:37:28 +00:00
|
|
|
|
{
|
|
|
|
|
subtitle *foo;
|
|
|
|
|
foo=fb;
|
|
|
|
|
fb=bb;
|
|
|
|
|
bb=foo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void cc_decode_EIA608(unsigned short int data)
|
|
|
|
|
{
|
2009-05-12 02:45:52 +00:00
|
|
|
|
|
2009-07-06 23:26:13 +00:00
|
|
|
|
static unsigned short int lastcode=0x0000;
|
2002-07-25 03:37:28 +00:00
|
|
|
|
unsigned char c1 = data & 0x7f;
|
|
|
|
|
unsigned char c2 = (data >> 8) & 0x7f;
|
|
|
|
|
|
|
|
|
|
if (c1 & 0x60) { /* normal character, 0x20 <= c1 <= 0x7f */
|
|
|
|
|
append_char(chartbl[c1]);
|
|
|
|
|
if(c2 & 0x60) /*c2 might not be a normal char even if c1 is*/
|
|
|
|
|
append_char(chartbl[c2]);
|
|
|
|
|
}
|
|
|
|
|
else if (c1 & 0x10) // control code / special char
|
|
|
|
|
{
|
2002-09-22 02:33:28 +00:00
|
|
|
|
// int channel= (c1 & 0x08) >> 3;
|
2002-07-25 03:37:28 +00:00
|
|
|
|
c1&=~0x08;
|
|
|
|
|
if(data!=lastcode)
|
|
|
|
|
{
|
|
|
|
|
if(c2 & 0x40) { /*PAC, Preamble Address Code */
|
|
|
|
|
append_char('\n'); /*FIXME properly interpret PACs*/
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
switch(c1)
|
|
|
|
|
{
|
|
|
|
|
case 0x10: break; // ext attribute
|
2009-05-12 02:45:52 +00:00
|
|
|
|
case 0x11:
|
|
|
|
|
if((c2 & 0x30)==0x30)
|
2002-07-25 03:37:28 +00:00
|
|
|
|
{
|
|
|
|
|
//printf("[debug]:Special char (ignored)\n");
|
|
|
|
|
/*cc_decode_special_char()*/;
|
|
|
|
|
}
|
2009-05-12 02:45:52 +00:00
|
|
|
|
else if (c2 & 0x20)
|
2002-07-25 03:37:28 +00:00
|
|
|
|
{
|
|
|
|
|
//printf("[debug]: midrow_attr (ignored)\n");
|
|
|
|
|
/*cc_decode_midrow_attr()*/;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 0x14:
|
|
|
|
|
switch(c2)
|
|
|
|
|
{
|
2007-10-27 04:46:01 +00:00
|
|
|
|
case 0x00: //CC roll-on mode
|
|
|
|
|
cc_mode=CC_ROLLON;
|
|
|
|
|
break;
|
|
|
|
|
case 0x25: //CC roll-up, 2 rows
|
|
|
|
|
case 0x26: //CC roll-up, 3 rows
|
|
|
|
|
case 0x27: //CC roll-up, 4 rows
|
|
|
|
|
cc_lines=c2-0x23;
|
|
|
|
|
cc_mode=CC_ROLLUP;
|
|
|
|
|
break;
|
2002-07-25 03:37:28 +00:00
|
|
|
|
case 0x2C: display_buffer(NULL); //EDM
|
|
|
|
|
clear_buffer(fb); break;
|
|
|
|
|
case 0x2d: append_char('\n'); //carriage return
|
|
|
|
|
break;
|
|
|
|
|
case 0x2e: clear_buffer(bb); //ENM
|
|
|
|
|
break;
|
|
|
|
|
case 0x2f: swap_buffers(); //Swap buffers
|
|
|
|
|
display_buffer(fb);
|
|
|
|
|
clear_buffer(bb);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 0x17:
|
|
|
|
|
if( c2>=0x21 && c2<=0x23) //TAB
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-05-12 02:45:52 +00:00
|
|
|
|
}
|
|
|
|
|
lastcode=data;
|
2002-07-25 03:37:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-11-25 22:06:00 +00:00
|
|
|
|
static void subcc_decode(unsigned char *inputbuffer, unsigned int inputlength)
|
2002-07-25 03:37:28 +00:00
|
|
|
|
{
|
|
|
|
|
/* The first number may denote a channel number. I don't have the
|
|
|
|
|
* EIA-708 standard, so it is hard to say.
|
|
|
|
|
* From what I could figure out so far, the general format seems to be:
|
|
|
|
|
*
|
|
|
|
|
* repeat
|
|
|
|
|
*
|
|
|
|
|
* 0xfe starts 2 byte sequence of unknown purpose. It might denote
|
|
|
|
|
* field #2 in line 21 of the VBI. We'll ignore it for the
|
|
|
|
|
* time being.
|
|
|
|
|
*
|
|
|
|
|
* 0xff starts 2 byte EIA-608 sequence, field #1 in line 21 of the VBI.
|
|
|
|
|
* Followed by a 3-code triplet that starts either with 0xff or
|
|
|
|
|
* 0xfe. In either case, the following triplet needs to be ignored
|
|
|
|
|
* for line 21, field 1.
|
|
|
|
|
*
|
|
|
|
|
* 0x00 is padding, followed by 2 more 0x00.
|
|
|
|
|
*
|
|
|
|
|
* 0x01 always seems to appear at the beginning, always seems to
|
2009-05-12 02:45:52 +00:00
|
|
|
|
* be followed by 0xf8, 8-bit number.
|
2002-07-25 03:37:28 +00:00
|
|
|
|
* The lower 7 bits of this 8-bit number seem to denote the
|
|
|
|
|
* number of code triplets that follow.
|
2009-05-12 02:45:52 +00:00
|
|
|
|
* The most significant bit denotes whether the Line 21 field 1
|
2002-07-25 03:37:28 +00:00
|
|
|
|
* captioning information is at odd or even triplet offsets from this
|
|
|
|
|
* beginning triplet. 1 denotes odd offsets, 0 denotes even offsets.
|
2009-05-12 02:45:52 +00:00
|
|
|
|
*
|
2002-07-25 03:37:28 +00:00
|
|
|
|
* Most captions are encoded with odd offsets, so this is what we
|
|
|
|
|
* will assume.
|
|
|
|
|
*
|
|
|
|
|
* until end of packet
|
|
|
|
|
*/
|
|
|
|
|
unsigned char *current = inputbuffer;
|
|
|
|
|
unsigned int curbytes = 0;
|
|
|
|
|
unsigned char data1, data2;
|
|
|
|
|
unsigned char cc_code;
|
|
|
|
|
int odd_offset = 1;
|
|
|
|
|
|
|
|
|
|
while (curbytes < inputlength) {
|
|
|
|
|
int skip = 2;
|
|
|
|
|
|
|
|
|
|
cc_code = *(current);
|
|
|
|
|
|
|
|
|
|
if (inputlength - curbytes < 2) {
|
|
|
|
|
#ifdef LOG_DEBUG
|
|
|
|
|
fprintf(stderr, "Not enough data for 2-byte CC encoding\n");
|
|
|
|
|
#endif
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-05-12 02:45:52 +00:00
|
|
|
|
|
2002-07-25 03:37:28 +00:00
|
|
|
|
data1 = *(current+1);
|
|
|
|
|
data2 = *(current + 2);
|
|
|
|
|
current++; curbytes++;
|
2009-05-12 02:45:52 +00:00
|
|
|
|
|
2002-07-25 03:37:28 +00:00
|
|
|
|
switch (cc_code) {
|
|
|
|
|
case 0xfe:
|
|
|
|
|
/* expect 2 byte encoding (perhaps CC3, CC4?) */
|
|
|
|
|
/* ignore for time being */
|
|
|
|
|
break;
|
2009-05-12 02:45:52 +00:00
|
|
|
|
|
2002-07-25 03:37:28 +00:00
|
|
|
|
case 0xff:
|
|
|
|
|
/* expect EIA-608 CC1/CC2 encoding */
|
|
|
|
|
// FIXME check parity!
|
|
|
|
|
// Parity check omitted assuming we are reading from a DVD and therefore
|
|
|
|
|
// we should encounter no "transmission errors".
|
|
|
|
|
cc_decode_EIA608(data1 | (data2 << 8));
|
|
|
|
|
skip = 5;
|
|
|
|
|
break;
|
2009-05-12 02:45:52 +00:00
|
|
|
|
|
2002-07-25 03:37:28 +00:00
|
|
|
|
case 0x00:
|
|
|
|
|
/* This seems to be just padding */
|
|
|
|
|
break;
|
2009-05-12 02:45:52 +00:00
|
|
|
|
|
2002-07-25 03:37:28 +00:00
|
|
|
|
case 0x01:
|
|
|
|
|
odd_offset = data2 & 0x80;
|
2010-07-11 17:10:18 +00:00
|
|
|
|
if (!odd_offset)
|
2002-07-25 03:37:28 +00:00
|
|
|
|
skip = 5;
|
|
|
|
|
break;
|
2009-05-12 02:45:52 +00:00
|
|
|
|
|
2002-07-25 03:37:28 +00:00
|
|
|
|
default:
|
|
|
|
|
//#ifdef LOG_DEBUG
|
|
|
|
|
fprintf(stderr, "Unknown CC encoding: %x\n", cc_code);
|
|
|
|
|
//#endif
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
current += skip;
|
|
|
|
|
curbytes += skip;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void subcc_process_data(unsigned char *inputdata,unsigned int len)
|
|
|
|
|
{
|
|
|
|
|
if(!subcc_enabled) return;
|
2008-02-14 14:23:55 +00:00
|
|
|
|
if(!initialized) subcc_init();
|
2009-07-06 23:26:13 +00:00
|
|
|
|
|
2006-11-25 22:06:00 +00:00
|
|
|
|
subcc_decode(inputdata, len);
|
2002-07-25 03:37:28 +00:00
|
|
|
|
}
|