[MPlayer-dev-eng] [PATCH] Teletext support try3 (1/5, core)

Vladimir Voroshilov voroshil at gmail.com
Sun Jul 15 13:01:13 CEST 2007


Hi, All

Here is current version of teletext patch (w/o zvbi).
Attached patch contains driver-independent core functionality
(analyzing raw vbi data, packets, decoding pages, etc)

1. Good cache implementation is needed

2. There are two routines for converting raw data into bytes.

decode_raw_line_sine - extracted from Michael Nidermauer's code. It
can't detect any data. Perhaps error somewhere in code.

decode_raw_line_runin - got from MythTV project source, based on
detecting min/max values on clock run-in sequence.

-- 
Regards,
Vladimir Voroshilov     mailto:voroshil at gmail.com
JID: voroshil at gmail.com, voroshil at jabber.ru
ICQ: 95587719
-------------- next part --------------
Index: stream/tvi_vbi.c
===================================================================
--- stream/tvi_vbi.c	(revision 0)
+++ stream/tvi_vbi.c	(revision 0)
@@ -0,0 +1,1451 @@
+/*
+ *     Teletext support
+ *
+ *
+ *     (C) Vladimir Voroshilov <voroshil at gmail.com> 2007.
+ *
+ *     Based on Attila Otvos's teletext patch, Michael Niedermayer's
+ *     proof-of-concept teletext capture utility and some parts
+ *     (decode_raw_line_runin,pll_add,pll_reset) of MythTV project.
+ *
+ *     This program 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.
+ *
+ *     This program 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 this program; if not, write to the Free Software
+ *     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *    
+ *     ------------------------------------------------------------------------
+ *
+ *    Some implementation details:
+ *    How to port teletext to another tvi_* driver (see tvi_v4l2.c for example):
+ *
+ *    1. Implement TVI_CONTROL_VBI_INIT (initialize driver-related vbi subsystem,
+ *       start grabbing thread)
+ *       input data: vbi device name.
+ *       (driver should also call TV_VBI_CONTROL_START for common vbi subsystem initialization
+ *       with pointer to initialized tt_stream_properties structure.
+ *       After ioctl call variable will contain pointer to initialized priv_vbi_t structure.
+ *
+ *    2. After receiving next chunk of raw vbi data call TV_VBI_CONTROL_DECODE_PAGE
+ *       ioctl with pointer to data buffer
+ *    3. pass all other VBI related ioctl cmds to teletext_control routine
+ *
+ *    Page displaying process consist of following stages:
+ *
+ *    ---grabbing stage---
+ *    0. stream/tvi_*.c: vbi_grabber(...)
+ *         getting vbi data from video device
+ *    ---decoding stage---
+ *    1. stream/tvi_vbi.c: decode_raw_line_runin(...) or decode_raw_line_sine(...)
+ *         decode raw vbi data into sliced 45(?) bytes long packets
+ *    2. stream/tvi_vbi.c: decode_pkt0(...), decode_pkt_page(...)
+ *         packets processing (header analyzing, storing complete page in cache,
+ *         only raw member of tt_char is filled at this stage)
+ *    3. stream/tvi_vbi.c: decode_page(...)
+ *         page decoding. filling unicode,gfx,ctl,etc members of tt_char structure
+ *         with apropriate values according to teletext control chars, converting
+ *       text to utf8.
+ *    ---rendering stage---
+ *    4. stream/tvi_vbi.c: prepare_visible_page(...)
+ *         processing page. adding number of just received by background process
+ *         teletext page, adding current time,etc.
+ *    5. libvo/sub.c: vo_update_text_teletext(...)
+ *         rendering displayable osd with text and graphics
+ *
+ * TODO:
+ *  v4lv1,bktr support
+ *  spu rendering
+ *  is better quality on poor signal possible ?
+ *  link support
+ *  optional libzvbi support
+ *  font autoscale
+ *  greyscale osd
+ *  slace command for dumping pages
+ *
+ *  BUGS:
+ *  wrong colors in debug dump
+ *  blinking when visible page was just updated
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <math.h>
+#include <stdio.h>
+
+#include <pthread.h>
+#include <iconv.h>
+
+#include "tv.h"
+#include "mp_msg.h"
+#include "libmpcodecs/img_format.h"
+#include "input/input.h"
+
+#define DEBUG_DUMP 1
+
+/// page magazine entry structure
+typedef struct _MAG{
+  tt_page *pt;
+  int order;
+  int lang;
+} MAG;
+
+
+typedef struct {
+    int            on;            ///< teletext on/off
+    int            pgno;          ///< seek page number
+    int            subno;         ///< seek subpage
+    int            curr_pgno;     ///< current page number
+    int            pagenumdec;    ///< set page num with dec
+
+    int            tformat;       ///< 0:text, 1:spu
+    int            tmode;         ///< 0:, 1:bw, 2:gray, 3:color
+    int            half;          ///< 0:half mode off, 1:top half page, 2:bottom half page
+    MAG*           mag;           ///< pages magazine (has 8 entities)
+    iconv_t        icd;           ///< opened iconv  descriptor
+    char*          input_section; ///< saved section name (for bining keys)
+    tt_page        display_page;  ///< currently displayed page (with additional info, e.g current time)
+
+    int bpb;   /// number of raw bytes between two subsequent encoded bits
+    /// clock run-in sequence will be searched in buffer in [soc:eoc] bytes range
+    int soc;
+    int eoc;
+    int bp8bl; /// minimum number of raw vbi bytes wich can be decoded into 8 data bits
+    int bp8bh; /// maximum number of raw vbi bytes wich can be decoded into 8 data bits
+
+    int pll_adj;
+    int pll_dir;
+    int pll_cnt;
+    int pll_err;
+    int pll_lerr;
+    int pll_fixed;
+
+    tt_stream_props* ptsp;  ///< vbi stream properties (buffer size,bytes per line, etc)
+    pthread_mutex_t buffer_mutex;
+} priv_vbi_t;
+
+
+
+#define VBI_TEXT_CHARSET    "UTF-8"
+
+char* tv_param_tformat="text";      ///< format: text,bw,gray,color
+int tv_param_tpage=100;             ///< page number
+
+static unsigned char fixParity[256];
+static tt_page** ptt_cache;
+static unsigned char* ptt_cache_first_subpage;
+
+static tt_char tt_space={0x20,0,0,0,0,0,0x20};
+static double si[12];
+static double co[12];
+
+#define VBI_FORMAT(priv) (*(priv->ptsp))
+
+#define FIXP_SH 16
+#define ONE_FIXP (1<<FIXP_SH)
+#define FIXP2INT(a) ((a)>>FIXP_SH)
+#define ANY2FIXP(a) ((int)((a)*ONE_FIXP))
+
+static unsigned char corrHamm48[256]={
+  0x01, 0xff, 0x01, 0x01, 0xff, 0x00, 0x01, 0xff,  0xff, 0x02, 0x01, 0xff, 0x0a, 0xff, 0xff, 0x07, 
+  0xff, 0x00, 0x01, 0xff, 0x00, 0x00, 0xff, 0x00,  0x06, 0xff, 0xff, 0x0b, 0xff, 0x00, 0x03, 0xff, 
+  0xff, 0x0c, 0x01, 0xff, 0x04, 0xff, 0xff, 0x07,  0x06, 0xff, 0xff, 0x07, 0xff, 0x07, 0x07, 0x07, 
+  0x06, 0xff, 0xff, 0x05, 0xff, 0x00, 0x0d, 0xff,  0x06, 0x06, 0x06, 0xff, 0x06, 0xff, 0xff, 0x07, 
+  0xff, 0x02, 0x01, 0xff, 0x04, 0xff, 0xff, 0x09,  0x02, 0x02, 0xff, 0x02, 0xff, 0x02, 0x03, 0xff, 
+  0x08, 0xff, 0xff, 0x05, 0xff, 0x00, 0x03, 0xff,  0xff, 0x02, 0x03, 0xff, 0x03, 0xff, 0x03, 0x03, 
+  0x04, 0xff, 0xff, 0x05, 0x04, 0x04, 0x04, 0xff,  0xff, 0x02, 0x0f, 0xff, 0x04, 0xff, 0xff, 0x07, 
+  0xff, 0x05, 0x05, 0x05, 0x04, 0xff, 0xff, 0x05,  0x06, 0xff, 0xff, 0x05, 0xff, 0x0e, 0x03, 0xff, 
+  0xff, 0x0c, 0x01, 0xff, 0x0a, 0xff, 0xff, 0x09,  0x0a, 0xff, 0xff, 0x0b, 0x0a, 0x0a, 0x0a, 0xff, 
+  0x08, 0xff, 0xff, 0x0b, 0xff, 0x00, 0x0d, 0xff,  0xff, 0x0b, 0x0b, 0x0b, 0x0a, 0xff, 0xff, 0x0b, 
+  0x0c, 0x0c, 0xff, 0x0c, 0xff, 0x0c, 0x0d, 0xff,  0xff, 0x0c, 0x0f, 0xff, 0x0a, 0xff, 0xff, 0x07, 
+  0xff, 0x0c, 0x0d, 0xff, 0x0d, 0xff, 0x0d, 0x0d,  0x06, 0xff, 0xff, 0x0b, 0xff, 0x0e, 0x0d, 0xff, 
+  0x08, 0xff, 0xff, 0x09, 0xff, 0x09, 0x09, 0x09,  0xff, 0x02, 0x0f, 0xff, 0x0a, 0xff, 0xff, 0x09, 
+  0x08, 0x08, 0x08, 0xff, 0x08, 0xff, 0xff, 0x09,  0x08, 0xff, 0xff, 0x0b, 0xff, 0x0e, 0x03, 0xff, 
+  0xff, 0x0c, 0x0f, 0xff, 0x04, 0xff, 0xff, 0x09,  0x0f, 0xff, 0x0f, 0x0f, 0xff, 0x0e, 0x0f, 0xff, 
+  0x08, 0xff, 0xff, 0x05, 0xff, 0x0e, 0x0d, 0xff,  0xff, 0x0e, 0x0f, 0xff, 0x0e, 0x0e, 0xff, 0x0e };
+
+
+#define LAT_UNI 0
+#define RUS_UNI 1
+
+#define LANGS 2
+
+// conversion table for chars 0x20-0x7F (UCS-2LE)
+// TODO: add another languages
+unsigned int lang_chars[LANGS][0x60]={
+ {
+  //Latin
+  0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
+  0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
+  0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
+  0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
+  0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,
+  0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
+  0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,
+  0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
+  0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
+  0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
+  0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
+  0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f
+ },
+ {
+  //Russian
+  0x20,0x21,0x22,0x23,0x24,0x25,0x044b,0x27,
+  0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
+  0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
+  0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
+  0x042e,0x0410,0x0411,0x0426,0x0414,0x0415,0x0424,0x0413,
+  0x0425,0x0418,0x0419,0x041a,0x041b,0x041c,0x041d,0x041e,
+  0x041f,0x042f,0x0420,0x0421,0x0422,0x0423,0x0416,0x0412,
+  0x042c,0x042a,0x0417,0x0428,0x042d,0x0429,0x0427,0x042b,
+  0x044e,0x0430,0x0431,0x0446,0x0434,0x0435,0x0444,0x0433,
+  0x0445,0x0438,0x0439,0x043a,0x043b,0x043c,0x043d,0x043e,
+  0x043f,0x044f,0x0440,0x0441,0x0442,0x0443,0x0436,0x0432,
+  0x044c,0x044a,0x0437,0x0448,0x044d,0x0449,0x0447,0x044b
+ }
+};
+
+int lang2id (int lang){
+  switch(lang){
+  case 3: //Stub. My teletext provider (1TV, Russia) sets this this language code for russian teletext pages
+          //TODO: make this configurable
+      return RUS_UNI;
+  default:
+      return LAT_UNI;
+  }
+}
+
+/**
+ * \brief convert chars from curent teletext codepage into MPlayer charset
+ * \param icd opened iconv descriptor
+ * \param p tt_char structure to recode
+ * \param lang teletext internal language code (see lang2id)
+ *
+ * \remarks
+ * routine will analyze raw member of given tt_char structure and
+ * fill unicode member of the same struct with apropriate utf8 code.
+ *
+ */
+void conv2uni(iconv_t icd,tt_char*p,int lang)
+{
+    unsigned int u1,u2;
+    char* ibuf,*obuf;
+    int inum,onum;
+
+    if (lang>=LANGS) //make sure that we will not get out of bounds error
+        lang=LAT_UNI;//fall back to latin
+
+    if(p[0].raw<0x80 && p[0].raw>=0x20){
+        u1=lang_chars[lang][p[0].raw-0x20];
+        u2=0;
+        inum=2;
+        onum=4;
+        ibuf=(char*)&u1;
+        obuf=(char*)&u2;
+        if(iconv(icd,&ibuf,&inum,&obuf,&onum)<0)
+            u2=0x20;
+    }else
+        u2=0x20;
+    p[0].unicode=u2;
+}
+
+/**
+ * \brief convert 123 --> 0x123
+ * \param dec decimal value
+ * \return apropriate hexadecimal value
+ *
+ */
+static inline int vbi_dec2bcd(int dec){
+    unsigned char c1,c2,c3;
+    int ret;
+    c1=dec%10;
+    c2=(dec/10)%10;
+    c3=(dec/100)%10;
+    ret=(c3<<8)|(c2<<4)|c1;
+    mp_msg(MSGT_TV,MSGL_DBG3,"vbi_dec2bcd: %d -> 0x%03x\n",dec,ret);
+    return ret;
+}
+
+/**
+ * \brief convert 0x123 --> 123
+ * \param bcd hexadecimal value
+ * \return apropriate decimal value
+ *
+ */
+static inline int vbi_bcd2dec(int bcd){
+    unsigned char c1,c2,c3;
+    int ret;
+ 
+    c1=(bcd>>8)&0xf;
+    c2=(bcd>>4)&0xf;
+    c3=(bcd)&0xf;
+    
+    if(c1>9 || c2>9 || c3>9)
+        return 0;
+
+    ret=(c1*100+c2*10+c3);
+    mp_msg(MSGT_TV,MSGL_DBG3,"vbi_bcd2dec:0x%03x -> %d \n",bcd,ret);
+    return ret;
+}
+
+/**
+ * \brief calculate increased/decreased by given value page number
+ * \param curr  current page number in hexadecimal for
+ * \param direction decimal value (can be negative) to add to value or curr parameter
+ * \return new page number in hexadecimal form
+ *
+ * VBI page numbers are represented in special hexadecimal form, e.g.
+ * page with number 123 (as seen by user) internally has number 0x123.
+ * and equation 0x123+8 should be equal to 0x131 instead of regular 0x12b.
+ *
+ *
+ * Page numbers 0xYYY (where Y is not belongs to (0..9) and pages below 0x100 and
+ * higher 0x799 are reserved for internal use.
+ *
+ */
+static int steppage(int curr, int direction)
+{
+    int newpage = vbi_dec2bcd(vbi_bcd2dec(curr) + direction);
+    if (newpage < 0x0)
+        newpage = 0x799;
+    if (newpage > 0x799)
+        newpage = 0x0;
+    mp_msg(MSGT_TV,MSGL_DBG3,"steppage is called: curr:0x%03x, direction: %d, newpage: 0x%03x\n",curr,direction,newpage);
+    return newpage;
+}
+
+/*
+------------------------------------------------------------------
+   Cache stuff
+------------------------------------------------------------------
+*/
+void put_to_cache(priv_vbi_t* priv,tt_page* pg,int pgno,int subno){
+    int cache_idx=pgno+VBI_MAX_PAGES*subno;
+    if(!pg){
+        if(ptt_cache[cache_idx])
+            ptt_cache[cache_idx]->active=0;
+        return;
+    }
+    pthread_mutex_lock(&(priv->buffer_mutex));
+    if(ptt_cache_first_subpage[pgno]>subno){
+        ptt_cache_first_subpage[pgno]=subno;
+    }
+
+    if(!ptt_cache[cache_idx]){
+        ptt_cache[cache_idx]=(tt_page*)malloc(sizeof(tt_page));
+        memset(ptt_cache[cache_idx],0,sizeof(tt_page));
+    }
+    memcpy(ptt_cache[cache_idx],pg,sizeof(tt_page));
+    ptt_cache[cache_idx]->active=1;
+    pthread_mutex_unlock(&(priv->buffer_mutex));
+}
+
+tt_page* get_from_cache(priv_vbi_t* priv,int pgno,int subno){
+    if(!ptt_cache)
+        return NULL;
+
+    return ptt_cache[pgno+VBI_MAX_PAGES*subno];
+}
+
+void clear_cache(priv_vbi_t* priv){
+    int i;
+    for(i=0;i<VBI_MAX_PAGES*VBI_MAX_SUBPAGES;i++)
+        if(ptt_cache[i])
+            ptt_cache[i]->active=0;
+    memset(ptt_cache_first_subpage,VBI_MAX_SUBPAGES,VBI_MAX_PAGES);
+}
+void init_cache(void){
+    ptt_cache=(tt_page**)malloc(VBI_MAX_PAGES*VBI_MAX_SUBPAGES*sizeof(tt_page*));
+    memset(ptt_cache,0,VBI_MAX_PAGES*VBI_MAX_SUBPAGES*sizeof(tt_page*));
+    ptt_cache_first_subpage=malloc(VBI_MAX_PAGES);
+    memset(ptt_cache_first_subpage,VBI_MAX_SUBPAGES,VBI_MAX_PAGES);
+}
+void destroy_cache(void){
+    int i;
+
+    if(ptt_cache){
+        for(i=0;i<VBI_MAX_PAGES*VBI_MAX_SUBPAGES;i++)
+            if(ptt_cache[i])
+                free(ptt_cache[i]);
+        free(ptt_cache);
+        ptt_cache=NULL;
+    }
+    if(ptt_cache_first_subpage)
+        free(ptt_cache_first_subpage);
+    ptt_cache_first_subpage=NULL;
+}
+
+/*
+------------------------------------------------------------------
+   Renderer stuff
+------------------------------------------------------------------
+*/
+#ifdef DEBUG_DUMP
+/**
+ * \brief renders teletext page into given file
+ * \param f opened file descriptor
+ * \param pgno which page to render
+ * \param colored use colors not implementede yet)
+ *
+ * Text will be UCS-2LE encoded
+ *
+ */
+void render2ascii(priv_vbi_t* priv,FILE* f,int pgno,int colored){
+    int i,j;
+    unsigned int u;
+    int color=0;
+    int bkg=0;
+    int c1,b1;
+    tt_page* pt=get_from_cache(priv,pgno,0);
+    if(!pt)
+        return;
+    fprintf(f,"+========================================+\n");
+    fprintf(f,"| lang:%d pgno:0x%x subno:%d flags:0x%x    |\n",
+    pt->lang,
+    pt->pgno,
+    pt->subno,
+    0);
+    fprintf(f,"+----------------------------------------+\n");
+
+    for(i=0;i<VBI_ROWS;i++){
+        fprintf(f,"|");
+        if(colored) fprintf(f,"\033[40m");
+        for(j=0;j<VBI_COLUMNS;j++)
+        {
+             u=pt->text[i*VBI_COLUMNS+j].unicode;
+              switch(pt->text[i*VBI_COLUMNS+j].fg){
+              case 0 ... 7:
+                c1=30+pt->text[i*VBI_COLUMNS+j].fg;
+                break;
+              default:
+                c1=38;
+                  break;
+              }
+              switch(pt->text[i*VBI_COLUMNS+j].bg){
+              case 0 ... 7:
+                  b1=40+pt->text[i*VBI_COLUMNS+j].bg;
+                break;
+              default:
+                b1=40;
+              }
+            if (b1!=bkg  && colored){
+                fprintf(f,"\033[%dm",b1);
+                bkg=b1;
+            }
+            if(c1!=color && colored){
+                fprintf(f,"\033[%dm",c1);
+                color=c1;
+            }
+	    if(pt->text[i*VBI_COLUMNS+j].gfx){
+	        fprintf(f,"*");
+	    }else{
+            fprintf(f,"%c",u&0xff);
+	    if((u>>8)&0xff) fprintf(f,"%c",(u>>8)&0xff);
+	    if((u>>16)&0xff) fprintf(f,"%c",(u>>16)&0xff);
+	    if((u>>24)&0xff) fprintf(f,"%c",(u>>24)&0xff);
+	    }
+        }
+        
+        if (colored) fprintf(f,"\033[0m");
+        color=-1;bkg=-1;
+        fprintf(f,"|\n");
+    }
+    fprintf(f,"+========================================+\n");
+}
+
+/**
+ * \brief dump page into pgXXX.txt file in vurrent directory
+ * \param pgno with page to dump
+ *
+ * \note XXX in filename is page number
+ * \note use only for debug purposes
+ *
+ */
+void dump_page(priv_vbi_t* priv,int pgno)
+{
+    FILE*f;
+    char name[100];
+    snprintf(name,99,"pg%x.txt",pgno);
+    f=fopen(name,"wb");
+    render2ascii(priv,f,pgno,1);//dump colored ascii page (UCS-2LE encoded)
+    fclose(f);
+}
+#endif //DEBUG_DUMP
+
+/*
+------------------------------------------------------------------
+   Decoder stuff
+------------------------------------------------------------------
+*/
+/**
+ * \brief converts raw teletext page into usefull format (1st rendering stage)
+ * \param priv private data structure
+ * \param pgno which page to decode
+ *
+ * Routine fills tt_char structure of each teletext_page character with proper info about
+ * foreground and background colors, character type (graphics/control/text).
+ *
+ */
+void decode_page(priv_vbi_t* priv,tt_page* pg,int pgno,int subno)
+{
+    int c,gfx=0,lat=0;
+    int i=0;
+    int color=0;
+    int bkg_color=0;
+    int row,col;
+    int separated=0;
+    int lang;
+    tt_char *p;
+
+    if(!pg)
+        return;
+
+    p=pg->text;
+    lang=pg->lang;
+
+    for(row=0;row<VBI_ROWS;row++)   {
+        lat=lang==0?1:0;
+        gfx=0;
+        color=7;
+        bkg_color=0;
+        separated=0;
+        for(col=0;col<VBI_COLUMNS;col++){
+            i=row*VBI_COLUMNS+col;
+            c=p[i].raw;
+            p[i].gfx=gfx?(separated?2:1):0;
+            p[i].lng=lat?1:0;
+            p[i].ctl=(c&0x60)==0?1:0;
+            p[i].fg=color;
+            p[i].bg=bkg_color;
+    
+#ifdef DEEP_DEBUG
+        if(pgno==0x770) fprintf(stderr,"(%02x %c) ",c,p[i].gfx?'1':'0');
+#endif
+            if ((c&0x60)==0){ //control chars
+                switch (c){
+                case 0x00://Alpha Black
+                case 0x01://Alpha Red          
+                case 0x02://Alpha Green
+                case 0x03://Alpha Yellow
+                case 0x04://Alpha Blue
+                case 0x05://Alpha Magenta
+                case 0x06://Alpha Cyan
+                case 0x07://Alpha White - Assumed at Row start
+                    color=c;
+                    gfx=0;
+                    break;
+                case 0x08://Flash (NYI)
+                case 0x09://Steady (NYI) - Assumed at Row start
+                case 0x0a://End Box (NYI) - Assumed at Row start
+                case 0x0b://Start Box (NYI)
+                case 0x0c://Normal Height (NYI) - Assumed at Row start
+                case 0x0d://Double Height (NYI)
+                case 0x0e://S0 (NYI)
+                case 0x0f://S1 (NYI)
+		    break;
+                case 0x10://Graphics black
+                case 0x11://Graphics Red
+                case 0x12://Graphics Green
+                case 0x13://Graphics Yellow
+                case 0x14://Graphics Blue
+                case 0x15://Graphics Magenta
+                case 0x16://Graphics Cyan
+                case 0x17://Graphics White - Assumed at Row start
+                    color=c&0xf;
+                    gfx=1;
+                    break;      
+                case 0x18://Conceal display
+                    break;
+                case 0x19://Contiguous Gfx - Assumed at Row start
+                    separated=0;
+                    break;      
+                case 0x1a://Separate Gfx
+                    separated=1;
+                    break;      
+                case 0x1b: //ESC (Used as lang switch) - for backward compatibility
+                    lat=!lat;
+                    break;
+                case 0x1c: //Black background - Assumed at Row start
+                    bkg_color=0;
+		    p[i].bg=0;
+                    break;
+                case 0x1d: //New background
+                    bkg_color=color;
+		    p[i].bg=color;
+                    break;  
+                case 0x1e: //Hold graphics
+                case 0x1f: //Release gaphics - Assumed at Row start
+                    break;  
+                }
+		p[i].ctl=1;
+		if(p[i].gfx)
+                    p[i].unicode=0;
+		else
+                    p[i].unicode=' ';
+                continue;
+            }
+        
+            if(gfx){
+                p[i].unicode=p[i].raw;
+                p[i].unicode-=0x20;
+                if (p[i].unicode>0x3f) p[i].unicode-=0x20;
+            }else if(lat) {
+                conv2uni(priv->icd,p+i,LAT_UNI);
+            }else
+                conv2uni(priv->icd,p+i,lang2id(lang));
+    
+            p[i].fg=color;
+            p[i].bg=bkg_color;
+        }
+#ifdef DEEP_DEBUG
+        if(pgno==0x770) fprintf(stderr,"\n");
+#endif
+    }
+    mp_msg(MSGT_TV,MSGL_DBG3,"page #%x was decoded!\n",pgno);
+}
+    
+void prepare_visible_page(priv_vbi_t* priv_vbi){
+    tt_page *pg,*curr_pg;
+    int i;
+
+    pthread_mutex_lock(&(priv_vbi->buffer_mutex));
+
+    pg=get_from_cache(priv_vbi,priv_vbi->pgno,priv_vbi->subno);
+    if(!pg && priv_vbi->subno==0)
+    {
+        priv_vbi->subno=ptt_cache_first_subpage[priv_vbi->pgno];
+        if (priv_vbi->subno!=VBI_MAX_SUBPAGES)
+            pg=get_from_cache(priv_vbi,priv_vbi->pgno,priv_vbi->subno);
+            if(!pg)
+            priv_vbi->subno=0;
+    }
+    curr_pg=get_from_cache(priv_vbi,priv_vbi->curr_pgno,0);
+    if(!curr_pg && ptt_cache_first_subpage[priv_vbi->curr_pgno]<VBI_MAX_SUBPAGES){
+        curr_pg=get_from_cache(priv_vbi,priv_vbi->curr_pgno,ptt_cache_first_subpage[priv_vbi->curr_pgno]);
+    }
+    if (!pg && !curr_pg){
+        for(i=0;i<VBI_ROWS*VBI_COLUMNS;i++){
+            priv_vbi->display_page.text[i]=pg->text[i];
+        }
+        priv_vbi->display_page.text[0].unicode='N';
+        priv_vbi->display_page.text[1].unicode='o';
+        priv_vbi->display_page.text[2].unicode=' ';
+        priv_vbi->display_page.text[3].unicode='t';
+        priv_vbi->display_page.text[4].unicode='e';
+        priv_vbi->display_page.text[5].unicode='l';
+        priv_vbi->display_page.text[6].unicode='e';
+        priv_vbi->display_page.text[7].unicode='t';
+        priv_vbi->display_page.text[8].unicode='e';
+        priv_vbi->display_page.text[9].unicode='x';
+        priv_vbi->display_page.text[10].unicode='t';
+    }else{
+        if (!pg || !pg->active){
+            for(i=0;i<VBI_ROWS*VBI_COLUMNS;i++){
+                priv_vbi->display_page.text[i]=tt_space;
+            }
+        }else{
+            for(i=0;i<VBI_ROWS*VBI_COLUMNS;i++){
+                priv_vbi->display_page.text[i]=pg->text[i];
+            }
+        }
+        priv_vbi->display_page.text[0]=curr_pg->text[8];
+        priv_vbi->display_page.text[1]=curr_pg->text[9];
+        priv_vbi->display_page.text[2]=curr_pg->text[10];
+	if((priv_vbi->pgno>>8)&0xf)
+            priv_vbi->display_page.text[5].unicode='0'+((priv_vbi->pgno>>8)&0xf);
+        else
+            priv_vbi->display_page.text[5].unicode='8';
+        priv_vbi->display_page.text[6].unicode='0'+((priv_vbi->pgno>>4)&0xf);
+        priv_vbi->display_page.text[7].unicode='0'+((priv_vbi->pgno)&0xf);
+        priv_vbi->display_page.text[8].unicode='.';
+        priv_vbi->display_page.text[9].unicode='0'+((priv_vbi->subno>>4)&0xf);
+        priv_vbi->display_page.text[10].unicode='0'+((priv_vbi->subno)&0xf);
+        priv_vbi->display_page.text[11].unicode=' ';
+
+        for(i=VBI_TIME_LINEPOS;i<VBI_COLUMNS;i++){
+            priv_vbi->display_page.text[i]=curr_pg->text[i];
+        }
+
+        switch(priv_vbi->pagenumdec>>12){
+            case 1:
+                priv_vbi->display_page.text[5].unicode='_';
+                priv_vbi->display_page.text[6].unicode='_';
+                priv_vbi->display_page.text[7].unicode=0x30+((priv_vbi->pagenumdec)&0xf);
+                break;
+            case 2:
+                priv_vbi->display_page.text[5].unicode='_';
+                priv_vbi->display_page.text[6].unicode=0x30+((priv_vbi->pagenumdec>>4)&0xf);
+                priv_vbi->display_page.text[7].unicode=0x30+((priv_vbi->pagenumdec)&0xf);
+            break;
+        }
+        priv_vbi->display_page.active=1;
+    }
+    pthread_mutex_unlock(&(priv_vbi->buffer_mutex));
+}
+/**
+ * \brief checks whether page is ready and copies it into cache array if so
+ * \param priv private data structure
+ * \param magAddr page's magazine address (0-7)
+ *
+ * Routine also calls decode_page to perform 1st stage of rendering
+ *
+ */
+
+void store_in_cache(priv_vbi_t* priv, int magAddr){
+    int pgno=priv->mag[magAddr].pt->pgno;
+    int subno=priv->mag[magAddr].pt->subno;
+
+    if(magAddr<0 || magAddr>7)
+        return;
+    if (priv->mag[magAddr].order!=23){ //is last row ?
+        put_to_cache(priv,NULL,pgno,subno);
+        mp_msg(MSGT_TV,MSGL_DBG3,"store_in_cache(%d): pgno:%x\n",priv->mag[magAddr].order,pgno);
+        return;
+    }
+
+    decode_page(priv,priv->mag[magAddr].pt,pgno,subno);
+    put_to_cache(priv,priv->mag[magAddr].pt,pgno,subno);
+#ifdef DEBUG_DUMP
+    dump_page(priv,pgno);
+#endif
+    priv->curr_pgno=pgno;
+}
+/*
+------------------------------------------------------------------
+  Grabber stuff
+------------------------------------------------------------------
+*/
+
+
+#define PLL_SAMPLES 4
+#define PLL_ERROR   4
+#define PLL_ADJUST  4
+
+/**
+ * \brief adjust current phase for better signal decoding
+ * \param n count of bytes processed (?)
+ * \param err count of error bytes (?)
+ *
+ * \remarks code was got from MythTV project
+ *
+ */
+static void pll_add(priv_vbi_t* priv,int n,int err){
+    if(priv->pll_fixed)
+        return;
+    if(err>PLL_ERROR*2/3)
+        err = PLL_ERROR*2/3;
+    priv->pll_err+=err;
+    priv->pll_cnt +=n;
+    if(priv->pll_cnt < PLL_SAMPLES)
+        return;
+    if(priv->pll_err>PLL_ERROR)
+    {
+        if(priv->pll_err>priv->pll_lerr)
+	    priv->pll_dir= -priv->pll_dir;
+        priv->pll_lerr=priv->pll_err;
+        priv->pll_adj+=priv->pll_dir;
+        if (priv->pll_adj < -PLL_ADJUST || priv->pll_adj > PLL_ADJUST)
+        {
+            priv->pll_adj=0;
+	    priv->pll_dir=-1;
+	    priv->pll_lerr=0;
+        }
+        mp_msg(MSGT_TV,MSGL_DBG3,"vbi: pll_adj=%2d\n",priv->pll_adj);
+    }
+    priv->pll_cnt=0;
+    priv->pll_err=0;
+}
+
+/**
+ * \brief reset error correction
+ * \param priv private data structure
+ * \param fine_tune shift value for adjusting
+ *
+ * \remarks code was got from MythTV project
+ *
+ */
+static void pll_reset(priv_vbi_t* priv,int fine_tune){
+    priv->pll_fixed = fine_tune >= -PLL_ADJUST && fine_tune <= PLL_ADJUST;
+
+    priv->pll_err=0;
+    priv->pll_lerr=0;
+    priv->pll_cnt=0;
+    priv->pll_dir=-1;
+    priv->pll_adj=0;
+    if(priv->pll_fixed)
+        priv->pll_adj=fine_tune;
+    if(priv->pll_fixed)
+        mp_msg(MSGT_TV,MSGL_DBG3,"pll_reset (fixed@%2d)\n",priv->pll_adj);
+    else
+        mp_msg(MSGT_TV,MSGL_DBG3,"pll_reset (auto)\n");
+
+}
+/**
+ * \brief decode packet 0 (teletext page header)
+ * \param priv private data structure 
+ * \param data raw teletext data (with not applied hamm correction yet)
+ * \param magAddr teletext page's magazine address
+ *
+ * \remarks 
+ * data buffer was shifted by 6 and now contains:
+ *  0..1 page number
+ *  2..5 sub-code
+ *  6..7 control codes
+ *  8..39 display data
+ *
+ *  only first 8 bytes protected by Hamm 8/4 code
+ */
+int decode_pkt0(priv_vbi_t* priv,unsigned char* data,int magAddr)
+{
+    int d[8];
+    int i,err;
+
+    if (magAddr<0 || magAddr>7)
+        return 0;
+    for(i=0;i<8;i++){
+        d[i]= corrHamm48[ data[i] ];
+        if(d[i]&0x80){
+	    pll_add(priv,2,4);
+
+            if(priv->mag[magAddr].pt)
+                  free(priv->mag[magAddr].pt);
+            priv->mag[magAddr].pt=NULL;
+            priv->mag[magAddr].order=0;
+            return 0;
+        }
+    }
+    if (!priv->mag[magAddr].pt)
+        priv->mag[magAddr].pt= malloc(sizeof(tt_page));
+
+    priv->mag[magAddr].lang= d[7] & 0x7;
+    priv->mag[magAddr].pt->lang=priv->mag[magAddr].lang;
+    priv->mag[magAddr].pt->subno=( d[2] | (d[3]<<4) | (d[4]<<8) | (d[5]<<12) ) & 0x3F7F;
+    priv->mag[magAddr].pt->pgno=(magAddr<<8) | d[0] | (d[1]<<4);
+    priv->mag[magAddr].pt->flags=( d[6] | (d[7]<<4));
+
+    memset(priv->mag[magAddr].pt->text, 0x00, VBI_COLUMNS*VBI_ROWS*sizeof(tt_char));
+    priv->mag[magAddr].order=0;
+
+    for(i=0;i<8;i++){
+        priv->mag[magAddr].pt->text[i].raw=0x20;
+    }
+    err=0;
+    for(i=8; i<VBI_COLUMNS; i++){
+        data[i]= fixParity[data[i]];
+        if(data[i]&0x80){ //Error
+            priv->mag[magAddr].pt->text[i].raw='?';
+	    err++;
+        }else
+            priv->mag[magAddr].pt->text[i].raw=data[i];
+        pll_add(priv,1,err);
+    }
+    return 1;
+}
+
+/**
+ * \brief decode packets 1..24 (teletext page header)
+ * \param priv private data structure 
+ * \param data raw teletext data
+ * \param magAddr teletext page's magazine address
+ * \param rowAddr teletext page's row number
+ *
+ * 
+ * \remarks 
+ * data buffer was shifted by 6 and now contains 40 bytes of display data:
+ * this type of packet is not proptected by Hamm 8/4 code
+ *
+ */
+void decode_pkt_page(priv_vbi_t* priv,unsigned char*data,int magAddr,int rowAddr){
+    int i,err;
+    if (!priv->mag[magAddr].pt)
+        return;
+
+    priv->mag[magAddr].order=rowAddr;
+
+    err=0;
+    for(i=0; i<VBI_COLUMNS; i++){
+        data[i]= fixParity[ data[i] ];
+        if( data[i]&0x80){ //HammError
+	    err++;
+            priv->mag[magAddr].pt->text[i+rowAddr*VBI_COLUMNS].raw='?';
+        }else
+            priv->mag[magAddr].pt->text[i+rowAddr*VBI_COLUMNS].raw=data[i];
+    }
+    pll_add(priv,1,err);
+
+    store_in_cache(priv,magAddr);
+}
+
+/**
+ * \brief decode packet 30 (teletext page header)
+ * \param priv private data structure 
+ * \param data raw teletext data
+ * \param magAddr teletext page's magazine address
+ *
+ * \remarks 
+ * data buffer was shifted by 6 and now contains:
+ *   0      designation code
+ *   1..6   initial teletext page
+ *   7..8   network id
+ *   9      time offset
+ *   10..12 date
+ *   13..15 time
+ *   16..19 tv programme label
+ *   20..39 status display
+ *
+ * Stub. Not Yet Implemented
+ *
+ */
+int decode_pkt30(priv_vbi_t* priv,unsigned char* data,int magAddr){
+
+    return 0;
+}
+
+
+/**
+ * \brief decodes raw vbi data (signal amplitudes) into sequence of bytes
+ * \param priv private data structure
+ * \param buf raw vbi data (one line of frame)
+ * \param data output buffer for decoded bytes (at least 45 bytes long)
+ *
+ * Used XawTV's algorithm. Signal phase is calculated with help of starting clock
+ * run-in sequence (min/max values and bit distance values are calculated)
+ *
+ */
+int decode_raw_line_runin(priv_vbi_t* priv,unsigned char* buf,unsigned char* data){
+    const int magic= 0x27; // reversed 1110010
+    int dt[256],hi[6],lo[6];
+    int i,x,r;
+    int decoded;
+    int sync;
+    unsigned char min,max;
+    int thr=0; //threshold
+    
+    //stubs
+    int soc=0;
+    int eoc=92;
+    
+    for(i=soc;i<eoc;i++)
+        dt[i] = buf[i+priv->bpb/ONE_FIXP] - buf[i];    // amplifies the edges best.
+    /* set barrier */
+    for (i = eoc; i < eoc+16; i += 2)
+        dt[i] = 100, dt[i+1] = -100;
+
+    /* find 6 rising and falling edges */
+    for (i = soc, x = 0; x< 6; ++x)
+    {
+        while (dt[i] < 32)
+            i++;
+        hi[x] = i;
+        while (dt[i] > -32)
+            i++;
+        lo[x] = i;
+    }
+    if (i >= eoc)
+    {
+        return 0;      // not enough periods found
+    }
+    i = hi[5] - hi[1]; // length of 4 periods (8 bits)
+    if (i < priv->bp8bl || i > priv->bp8bh)
+    {
+        mp_msg(MSGT_TV,MSGL_DBG3,"vbi: wrong freq %d (%d,%d)\n",i,priv->bp8bl,priv->bp8bh);
+        return 0;      // bad frequency
+    }
+    /* AGC and sync-reference */
+    min = 255, max = 0, sync = 0;
+    for (i = hi[4]; i < hi[5]; ++i)
+        if (buf[i] > max)
+            max = buf[i], sync = i;
+    for (i = lo[4]; i < lo[5]; ++i)
+        if (buf[i] < min)
+            min = buf[i];
+    thr = (min + max) / 2;
+
+    buf += sync;
+    for(i=priv->pll_adj*priv->bpb/10;i<16*priv->bpb;i+=priv->bpb) // searching for '11'
+        if(buf[FIXP2INT(i)]>thr && buf[FIXP2INT(i+priv->bpb)]>thr)
+            break;
+    r=0;
+    for(decoded=1; decoded<= (VBI_COLUMNS+3)<<3;decoded++){
+        r>>=1;
+        if(buf[FIXP2INT(i)] > thr) r|=0x80;
+        if(!(decoded & 0x07)){
+            data[(decoded>>3) - 1]=r;
+            r=0;
+        }
+        i+=priv->bpb;
+    }
+    if(data[0]!=magic)
+        return 0; //magic not found
+
+    //stub
+    for(i=0;i<43;i++){
+        data[i]=data[i+1];
+    }
+    mp_msg(MSGT_TV,MSGL_DBG3,"thr:%d sync:%d ",thr,sync);
+
+    return 1;
+}
+
+/**
+ * \brief decodes raw vbi data (signal amplitudes) into sequence of bytes
+ * \param priv private data structure
+ * \param buf raw vbi data (one line of frame)
+ * \param data output buffer for decoded bytes (at least 45 bytes long)
+ *
+ * Used Michael Niedermayer's algorithm.
+ * Signal phase is calculated using corellation between given samples data and
+ * pure sine
+ *
+ */
+int decode_raw_line_sine(priv_vbi_t* priv,unsigned char* buf,unsigned char* data){
+    int i,x,r,amp,xFixp;
+    int avg=0;
+    double sin_sum=0, cos_sum=0;
+
+    for(x=0; x< FIXP2INT(10*priv->bpb); x++)
+      avg+=buf[x];
+
+    avg/=FIXP2INT(10*priv->bpb);
+
+    for(x=0; x<12; x++){
+      amp= buf[x<<1];
+      sin_sum+= si[x]*(amp-avg);
+      cos_sum+= co[x]*(amp-avg);
+    }
+    //this is always zero. Why ?
+    xFixp= atan(sin_sum/cos_sum)*priv->bpb/M_PI;
+
+    //Without this line the result is full of errors
+    //and routine is unable to find magic sequence
+    buf+=FIXP2INT(10*priv->bpb);
+
+    r=0;
+    for(x=FIXP2INT(xFixp);x<70;x=FIXP2INT(xFixp)){
+      r=(r<<1) & 0xFFFF;
+      if(buf[x] > avg) r|=1;
+      xFixp+=priv->bpb;
+      if(r==0xAAE4) break;
+    }
+
+    //this is not teletest
+    if (r!=0xaae4) return 0;
+
+    //Decode Remainin 45-2(clock run-in)-1(framing code)=42 bytes
+    for(i=1; i<=(42<<3); i++){
+      r>>=1;
+      x=FIXP2INT(xFixp);
+      if(buf[x]> avg)
+          r|=0x80;
+
+      if(!(i & 0x07)){
+          data[(i>>3)-1]=r;
+          r=0;
+      }
+      xFixp+=priv->bpb;
+    }
+
+    return 1;
+}
+
+/**
+ * \brief decodes all vbi lines from one video frame
+ * \param priv private data structure
+ * \param buf buffer with raw vbi data in it
+ *
+ * \note buffer size have to be at least priv->ptsp->bufsize bytes
+ *
+ */
+static void vbi_decode(priv_vbi_t* priv,unsigned char*buf){
+    int magAddr;
+    int pkt;
+    unsigned char data[64];
+    unsigned char* linep;
+    int d0,d1;
+    int i=0;
+    for(linep=buf; linep<buf+priv->ptsp->bufsize; linep+=priv->ptsp->samples_per_line,i++){
+//        if(decode_raw_line_sine(priv,linep,data)<=0){
+        if(decode_raw_line_runin(priv,linep,data)<=0){
+             continue; //this is not valid teletext line
+        }
+        d0= corrHamm48[ data[0] ];
+        d1= corrHamm48[ data[1] ];
+
+        if(d0&0x80 || d1&0x80){
+	   pll_add(priv,2,4);
+           mp_msg(MSGT_TV,MSGL_V,"vbi_decode(%d):HammErr after decode_raw_line\n",i);
+
+           continue; //hamError
+        }
+        magAddr = d0 & 0x7;
+        pkt = (d0>>3) | (d1<<1);
+        mp_msg(MSGT_TV,MSGL_DBG3,"vbi_decode(%d):%x %x (mag:%x, pkt:%d)\n",i,d0,d1,magAddr,pkt);
+        switch(pkt){
+        case 0:
+            decode_pkt0(priv,data+2,magAddr); //skip MRGA
+            break;
+        case 1 ... VBI_ROWS-1:
+            if(!priv->mag[magAddr].pt) continue;
+            decode_pkt_page(priv,data+2,magAddr,pkt);//skip MRGA
+            break;
+        case 30:
+            decode_pkt30(priv,data+2,magAddr);//skip MRGA
+            break;
+        default:
+            mp_msg(MSGT_TV,MSGL_DBG3,"unsupported packet:%d\n",pkt);
+            break;
+        }
+    }
+
+}
+
+/*
+---------------------------------------------------------------------------------
+    Public routines
+---------------------------------------------------------------------------------
+*/
+
+/**
+ * \brief toggles teletext page displaying mode
+ * \param priv_vbi private data structure
+ * \param flag new mode
+ * \return 
+ *   TVI_CONTROL_TRUE is success,
+ *   TVI_CONTROL_FALSE otherwise
+ *
+ * flag:
+ * 0 - off
+ * 1 - on & opaque
+ * 2 - on & transparent
+ * 3 - on & transparent  with black foreground color (only in bw mode)
+ *
+ */
+static int teletext_set_mode(priv_vbi_t * priv_vbi, int flag)
+{
+    char* tmp;
+    if (flag<0 || flag>4)
+        return TVI_CONTROL_FALSE;
+        
+    mp_msg(MSGT_TV,MSGL_DBG3,"teletext_set_mode_is called. mode:%d\n",flag);
+    pthread_mutex_lock(&(priv_vbi->buffer_mutex));
+
+    priv_vbi->tmode = flag;
+
+    priv_vbi->pagenumdec = 0;
+    priv_vbi->on=flag?1:0;
+
+    if(priv_vbi->on!=0 && priv_vbi->input_section==NULL) {
+        if (NULL==(tmp=mp_input_get_section())) tmp="default";
+        priv_vbi->input_section=strdup(tmp);
+        mp_input_set_section("teletext");
+    }
+    if(priv_vbi->on==0 && priv_vbi->input_section!=NULL) {
+        mp_input_set_section(priv_vbi->input_section);
+        free(priv_vbi->input_section);
+        priv_vbi->input_section=NULL;
+    }
+
+    pthread_mutex_unlock(&(priv_vbi->buffer_mutex));
+    return TVI_CONTROL_TRUE;
+}
+
+/**
+ * \brief append just entered digit to editing page number
+ * \param priv_vbi private data structure
+ * \param dec decimal digit to append
+ *
+ *  dec: 
+ *   '0'..'9' append digit
+ *    '-' remove last digit (backspace emulation)
+ *
+ * This routine allows user to jump to arbitrary page.
+ * It implements simple page number editing algorithm.
+ *
+ * Subsystem can be on one of two modes: normal and page number edit mode.
+ * Zero value of priv_vbi->pagenumdec means normal mode
+ * Non-zero value means page number edit mode and equals to packed
+ * decimal number of already entered part of page number.
+ *
+ * How this works.
+ * Let's assume that current mode is normal (pagenumdec is zero), teletext page 
+ * 100 are displayed as usual. topmost left corner of page contains page number.
+ * Then vbi_add_dec is sequentally called (through slave 
+ * command of course) with 1,4,-,2,3 * values of dec parameter.
+ *
+ * +-----+------------+------------------+
+ * | dec | pagenumxec | displayed number |
+ * +-----+------------+------------------+
+ * |     | 0x000      | 100              | 
+ * +-----+------------+------------------+
+ * | 1   | 0x001      | __1              |
+ * +-----+------------+------------------+
+ * | 4   | 0x014      | _14              |
+ * +-----+------------+------------------+
+ * | -   | 0x001      | __1              |
+ * +-----+------------+------------------+
+ * | 2   | 0x012      | _12              |
+ * +-----+------------+------------------+
+ * | 3   | 0x123      | 123              |
+ * +-----+------------+------------------+
+ * |     | 0x000      | 123              | 
+ * +-----+------------+------------------+
+ *
+ * pagenumdec will automatically receive zero value after third digit of page number
+ * is entered and current page will be switched to another one with entered page number.
+ *
+ */
+static void vbi_add_dec(priv_vbi_t * priv_vbi, char *dec)
+{
+    int count, shift;
+    if (!dec)
+        return;
+    if (!priv_vbi->on)
+        return;
+    if ((*dec < '0' || *dec > '9') && *dec != '-')
+        return;
+    pthread_mutex_lock(&(priv_vbi->buffer_mutex));
+    count = (priv_vbi->pagenumdec >> 12) & 0xf;
+    if (*dec == '-') {
+        count--;
+        if (count)
+            priv_vbi->pagenumdec = ((priv_vbi->pagenumdec >> 4) & 0xfff) | (count << 12);
+        else
+            priv_vbi->pagenumdec = 0;
+    } else {
+        shift = count * 4;
+        count++;
+        priv_vbi->pagenumdec =
+            (((priv_vbi->pagenumdec) << 4 | (*dec -'0')) & 0xfff) | (count << 12);
+        if (count == 3) {
+            priv_vbi->pgno = priv_vbi->pagenumdec & 0x7ff; // 0x800 -> 0x000
+	    
+            priv_vbi->subno = 0;
+            priv_vbi->pagenumdec = 0;
+        }
+    }
+    pthread_mutex_unlock(&(priv_vbi->buffer_mutex));
+}
+
+
+/**
+ * \brief Teletext control routine
+ * \param priv_vbi private data structure
+ * \param cmd command 
+ * \param arg command parameter (has to be not null)
+ *
+ */
+int teletext_control(void* p, int cmd, void *arg)
+{
+    priv_vbi_t* priv_vbi=(priv_vbi_t*)p;
+    int fine_tune=99;
+
+    if (!priv_vbi && cmd!=TV_VBI_CONTROL_START)
+        return TVI_CONTROL_FALSE;
+    if (!arg){
+        switch(cmd){
+        case TV_VBI_CONTROL_RESET:
+        case TV_VBI_CONTROL_STOP:
+            break;
+        default:
+            return TVI_CONTROL_FALSE;
+        }
+    }
+    
+    switch (cmd) {
+    case TV_VBI_CONTROL_RESET:
+    {
+        pthread_mutex_lock(&(priv_vbi->buffer_mutex));
+        priv_vbi->pagenumdec = 0;
+        clear_cache(priv_vbi);
+        priv_vbi->pgno=steppage(0,tv_param_tpage);
+        priv_vbi->subno=0;
+	pll_reset(priv_vbi,fine_tune);
+        pthread_mutex_unlock(&(priv_vbi->buffer_mutex));
+        return TVI_CONTROL_TRUE;
+    }
+    case TV_VBI_CONTROL_START:
+    {
+        int i,j;
+        double ang;
+        tt_stream_props* ptsp=*(tt_stream_props**)arg;
+
+        if(!ptsp)
+            return TVI_CONTROL_FALSE;
+
+        priv_vbi=malloc(sizeof(priv_vbi_t));
+        memset(priv_vbi,0,sizeof(priv_vbi_t));
+        priv_vbi->ptsp=malloc(sizeof(tt_stream_props));
+        memcpy(priv_vbi->ptsp,ptsp,sizeof(tt_stream_props));
+        *(priv_vbi_t**)arg=priv_vbi;
+        
+        priv_vbi->subno = 0;
+        pthread_mutex_init(&priv_vbi->buffer_mutex, NULL);
+        priv_vbi->pagenumdec = 0;
+        memset(&(priv_vbi->display_page),0,sizeof(tt_page));
+        for(i=VBI_COLUMNS;i<VBI_ROWS*VBI_COLUMNS;i++) priv_vbi->display_page.text[i].unicode=0x20;
+
+        for(i=0; i<256; i++){
+            j=i&0x7F;                 
+            j^= j+j;                      
+            j^= j<<2;                     
+            j^= j<<4;                     
+            fixParity[i]= i ^ (j&0x80) ^ 0x80;  
+        }
+
+        for(i=0,ang=0; i<12; i++,ang+=M_PI/priv_vbi->bpb){
+            si[i]= sin(ang);
+            co[i]= cos(ang);
+        }
+        
+        init_cache();
+
+        priv_vbi->mag=malloc(8*sizeof(MAG));
+        memset(priv_vbi->mag,0,8*sizeof(MAG));
+
+        priv_vbi->bpb =  (priv_vbi->ptsp->sampling_rate /6937500.0) * ONE_FIXP+ 0.5;
+//FIXME: STUBS!!!!
+        priv_vbi->soc=0;
+        priv_vbi->eoc=92;
+//END STUBS!!!
+        priv_vbi->bp8bl = 0.97 * 8*priv_vbi->bpb/ONE_FIXP; // -3% tolerance
+        priv_vbi->bp8bh = 1.03 * 8*priv_vbi->bpb/ONE_FIXP; // +3% tolerance
+        priv_vbi->icd=iconv_open(VBI_TEXT_CHARSET,"UCS-2LE");
+        pll_reset(priv_vbi,fine_tune);
+        return TVI_CONTROL_TRUE;
+    }
+    case TV_VBI_CONTROL_STOP:
+    {
+        if(priv_vbi->icd>0){
+            iconv_close(priv_vbi->icd);
+            priv_vbi->icd=0;
+        }
+        if(priv_vbi->mag)
+            free(priv_vbi->mag);
+        if(priv_vbi->ptsp)
+            free(priv_vbi->ptsp);
+        destroy_cache();
+        free(priv_vbi);
+        return TVI_CONTROL_TRUE;
+    }
+    case TV_VBI_CONTROL_GET_FORMAT:
+        pthread_mutex_lock(&(priv_vbi->buffer_mutex));
+        *(int*)arg=priv_vbi->tformat;
+        pthread_mutex_unlock(&(priv_vbi->buffer_mutex));
+        return TVI_CONTROL_TRUE;
+    case TV_VBI_CONTROL_SET_MODE:
+        return teletext_set_mode(priv_vbi, *(int *) arg);
+    case TV_VBI_CONTROL_GET_MODE:
+        pthread_mutex_lock(&(priv_vbi->buffer_mutex));
+        *(int*)arg=priv_vbi->tmode;
+        pthread_mutex_unlock(&(priv_vbi->buffer_mutex));
+        return TVI_CONTROL_TRUE;
+    case TV_VBI_CONTROL_STEP_MODE:
+    {
+        int val;
+        pthread_mutex_lock(&(priv_vbi->buffer_mutex));
+        val=(priv_vbi->tmode+*(int*)arg)%5;
+        pthread_mutex_unlock(&(priv_vbi->buffer_mutex));
+        if (val<0)
+            val+=4;
+        return teletext_set_mode(priv_vbi,val);
+    }
+    case TV_VBI_CONTROL_GET_HALF_PAGE:
+        if(!priv_vbi->on)
+            return TVI_CONTROL_FALSE;
+        *(int *) arg = priv_vbi->half;
+        return TVI_CONTROL_TRUE;
+    case TV_VBI_CONTROL_SET_HALF_PAGE:
+    {
+        int val=*(int*)arg;
+        if(val<0 || val>2)
+           return TVI_CONTROL_FALSE;
+        pthread_mutex_lock(&(priv_vbi->buffer_mutex));
+        priv_vbi->half = val;
+        pthread_mutex_unlock(&(priv_vbi->buffer_mutex));
+        return TVI_CONTROL_TRUE;
+    }
+    case TV_VBI_CONTROL_STEP_HALF_PAGE:
+    {
+        int val;
+        val=priv_vbi->half+((*(int*)arg)%3);
+        
+        if (val<0)
+            val+=3;
+        if (val>2)
+            val-=3;
+        pthread_mutex_lock(&(priv_vbi->buffer_mutex));
+        priv_vbi->half = val;
+        pthread_mutex_unlock(&(priv_vbi->buffer_mutex));
+        return TVI_CONTROL_TRUE;
+    }
+    case TV_VBI_CONTROL_SET_PAGE:
+    {
+        int val=*(int *) arg;
+        if(val<0 || val>0x799)
+            return TVI_CONTROL_FALSE;
+        pthread_mutex_lock(&(priv_vbi->buffer_mutex));
+        priv_vbi->pgno = steppage(0, val);
+        priv_vbi->subno = 0;
+        priv_vbi->pagenumdec = 0;
+        pthread_mutex_unlock(&(priv_vbi->buffer_mutex));
+        return TVI_CONTROL_TRUE;
+    }
+    case TV_VBI_CONTROL_STEP_PAGE:
+    {
+        int direction=*(int *) arg;
+        pthread_mutex_lock(&(priv_vbi->buffer_mutex));
+        priv_vbi->pgno=steppage(priv_vbi->pgno, direction);
+        priv_vbi->subno=0;
+        priv_vbi->pagenumdec = 0;
+        pthread_mutex_unlock(&(priv_vbi->buffer_mutex));
+        return TVI_CONTROL_TRUE;
+    }
+    case TV_VBI_CONTROL_GET_PAGE:
+        *(int*) arg = priv_vbi->pgno;
+        return TVI_CONTROL_TRUE;
+    case TV_VBI_CONTROL_SET_SUBPAGE:
+        pthread_mutex_lock(&(priv_vbi->buffer_mutex));
+        priv_vbi->pagenumdec = 0;
+        priv_vbi->subno = *(int *) arg;
+        pthread_mutex_unlock(&(priv_vbi->buffer_mutex));
+        return TVI_CONTROL_TRUE;
+    case TV_VBI_CONTROL_STEP_SUBPAGE:
+        pthread_mutex_lock(&(priv_vbi->buffer_mutex));
+        priv_vbi->pagenumdec = 0;
+        priv_vbi->subno +=*(int*)arg;
+        if(priv_vbi->subno<0)
+            priv_vbi->subno=0;
+        if(priv_vbi->subno>=VBI_MAX_SUBPAGES)
+            priv_vbi->subno=VBI_MAX_SUBPAGES-1;
+        pthread_mutex_unlock(&(priv_vbi->buffer_mutex));
+        return TVI_CONTROL_TRUE;
+    case TV_VBI_CONTROL_GET_SUBPAGE:
+        *(int*) arg = priv_vbi->subno;
+        return TVI_CONTROL_TRUE;
+    case TV_VBI_CONTROL_ADD_DEC:
+        vbi_add_dec(priv_vbi, *(char **) arg);
+        return TVI_CONTROL_TRUE;
+    case TV_VBI_CONTROL_DECODE_PAGE:
+        vbi_decode(priv_vbi,*(unsigned char**)arg);
+        return TVI_CONTROL_TRUE;
+    case TV_VBI_CONTROL_GET_VBIPAGE:
+        if(!priv_vbi->on)
+            return TVI_CONTROL_FALSE;
+        prepare_visible_page(priv_vbi);
+        *(void **)arg=&(priv_vbi->display_page);
+        return TVI_CONTROL_TRUE;
+    }
+    return TVI_CONTROL_UNKNOWN;
+}


More information about the MPlayer-dev-eng mailing list