23899
|
1 /*
|
|
2 * Teletext support
|
|
3 * Copyright (C) 2007 Vladimir Voroshilov <voroshil@gmail.com>
|
|
4 *
|
|
5 * This file is part of MPlayer.
|
|
6 *
|
|
7 * MPlayer is free software; you can redistribute it and/or modify
|
|
8 * it under the terms of the GNU General Public License as published by
|
|
9 * the Free Software Foundation; either version 2 of the License, or
|
|
10 * (at your option) any later version.
|
|
11 *
|
|
12 * MPlayer is distributed in the hope that it will be useful,
|
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 * GNU General Public License for more details.
|
|
16 *
|
|
17 * You should have received a copy of the GNU General Public License
|
|
18 * along with MPlayer; if not, write to the Free Software
|
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
20 *
|
|
21 *
|
|
22 * Based on Attila Otvos' teletext patch, Michael Niedermayer's
|
|
23 * proof-of-concept teletext capture utility and some parts
|
|
24 * (decode_raw_line_runin,pll_add,pll_reset) of MythTV project.
|
24099
|
25 * Code for calculating [soc:eoc] is based on aletv of Edgar Toernig.
|
23899
|
26 *
|
|
27 *
|
|
28 * Some implementation details:
|
|
29 * How to port teletext to another tvi_* driver (see tvi_v4l2.c for example):
|
|
30 *
|
|
31 * 1. Implement TVI_CONTROL_VBI_INIT (initialize driver-related vbi subsystem,
|
|
32 * start grabbing thread)
|
|
33 * input data: vbi device name.
|
|
34 * (driver should also call TV_VBI_CONTROL_START for common vbi subsystem initialization
|
|
35 * with pointer to initialized tt_stream_properties structure.
|
|
36 * After ioctl call variable will contain pointer to initialized priv_vbi_t structure.
|
|
37 *
|
|
38 * 2. After receiving next chunk of raw vbi data call TV_VBI_CONTROL_DECODE_PAGE
|
|
39 * ioctl with pointer to data buffer
|
|
40 * 3. pass all other VBI related ioctl cmds to teletext_control routine
|
|
41 *
|
|
42 * Page displaying process consist of following stages:
|
|
43 *
|
|
44 * ---grabbing stage---
|
|
45 * 0. stream/tvi_*.c: vbi_grabber(...)
|
|
46 * getting vbi data from video device
|
|
47 * ---decoding stage---
|
|
48 * 1. stream/tvi_vbi.c: decode_raw_line_runin(...) or decode_raw_line_sine(...)
|
|
49 * decode raw vbi data into sliced 45(?) bytes long packets
|
|
50 * 2. stream/tvi_vbi.c: decode_pkt0(...), decode_pkt_page(...)
|
|
51 * packets processing (header analyzing, storing complete page in cache,
|
|
52 * only raw member of tt_char is filled at this stage)
|
|
53 * 3. stream/tvi_vbi.c: decode_page(...)
|
|
54 * page decoding. filling unicode,gfx,ctl,etc members of tt_char structure
|
|
55 * with appropriate values according to teletext control chars, converting
|
|
56 * text to utf8.
|
|
57 * ---rendering stage---
|
|
58 * 4. stream/tvi_vbi.c: prepare_visible_page(...)
|
|
59 * processing page. adding number of just received by background process
|
|
60 * teletext page, adding current time,etc.
|
|
61 * 5. libvo/sub.c: vo_update_text_teletext(...)
|
|
62 * rendering displayable osd with text and graphics
|
|
63 *
|
|
64 * TODO:
|
|
65 * v4lv1,bktr support
|
|
66 * spu rendering
|
|
67 * is better quality on poor signal possible ?
|
|
68 * link support
|
|
69 * font autoscale
|
|
70 * greyscale osd
|
|
71 * slave command for dumping pages
|
|
72 * fix bcd<->dec as suggested my Michael
|
|
73 *
|
|
74 * BUGS:
|
|
75 * wrong colors in debug dump
|
|
76 * blinking when visible page was just updated
|
|
77 */
|
|
78
|
|
79 #include "config.h"
|
|
80
|
|
81 #include <stdlib.h>
|
|
82 #include <string.h>
|
|
83 #include <unistd.h>
|
|
84 #include <errno.h>
|
|
85 #include <math.h>
|
|
86 #include <stdio.h>
|
|
87
|
|
88 #include <pthread.h>
|
|
89
|
|
90 #include "tv.h"
|
|
91 #include "mp_msg.h"
|
|
92 #include "help_mp.h"
|
|
93 #include "libmpcodecs/img_format.h"
|
|
94 #include "libavutil/common.h"
|
|
95 #include "input/input.h"
|
|
96
|
|
97 //#define DEBUG_DUMP 1
|
|
98
|
|
99 /// page magazine entry structure
|
|
100 typedef struct mag_s{
|
|
101 tt_page* pt;
|
|
102 int order;
|
|
103 int lang;
|
|
104 } mag_t;
|
|
105
|
|
106 typedef struct {
|
|
107 int on; ///< teletext on/off
|
|
108 int pagenum; ///< seek page number
|
|
109 int subpagenum; ///< seek subpage
|
|
110 int curr_pagenum; ///< current page number
|
|
111 int pagenumdec; ///< set page num with dec
|
|
112
|
|
113 teletext_format tformat; ///< see teletext_format enum
|
|
114 teletext_zoom zoom; ///< see teletext_zoom enum
|
|
115 mag_t* mag; ///< pages magazine (has 8 entities)
|
|
116 /// Currently displayed page (with additional info, e.g current time)
|
|
117 tt_char display_page[VBI_ROWS*VBI_COLUMNS];
|
|
118 /// number of raw bytes between two subsequent encoded bits
|
|
119 int bpb;
|
|
120 /// clock run-in sequence will be searched in buffer in [soc:eoc] bytes range
|
|
121 int soc;
|
|
122 int eoc;
|
|
123 /// minimum number of raw vbi bytes wich can be decoded into 8 data bits
|
|
124 int bp8bl;
|
|
125 /// maximum number of raw vbi bytes wich can be decoded into 8 data bits
|
|
126 int bp8bh;
|
|
127
|
|
128 int pll_adj;
|
|
129 int pll_dir;
|
|
130 int pll_cnt;
|
|
131 int pll_err;
|
|
132 int pll_lerr;
|
|
133 int pll_fixed;
|
|
134 /// vbi stream properties (buffer size,bytes per line, etc)
|
|
135 tt_stream_props* ptsp;
|
|
136 pthread_mutex_t buffer_mutex;
|
|
137
|
|
138 tt_page** ptt_cache;
|
|
139 unsigned char* ptt_cache_first_subpage;
|
|
140 } priv_vbi_t;
|
|
141
|
|
142 static unsigned char fixParity[256];
|
|
143
|
|
144 static tt_char tt_space={0x20,7,0,0,0,0,0x20};
|
|
145 static tt_char tt_error={'?',1,0,0,0,0,'?'}; // Red '?' on black background
|
|
146 static double si[12];
|
|
147 static double co[12];
|
|
148
|
|
149 #define VBI_FORMAT(priv) (*(priv->ptsp))
|
|
150
|
|
151 #define FIXP_SH 16
|
|
152 #define ONE_FIXP (1<<FIXP_SH)
|
|
153 #define FIXP2INT(a) ((a)>>FIXP_SH)
|
|
154 #define ANY2FIXP(a) ((int)((a)*ONE_FIXP))
|
|
155
|
|
156 static const unsigned char corrHamm48[256]={
|
|
157 0x01, 0xff, 0x01, 0x01, 0xff, 0x00, 0x01, 0xff,
|
|
158 0xff, 0x02, 0x01, 0xff, 0x0a, 0xff, 0xff, 0x07,
|
|
159 0xff, 0x00, 0x01, 0xff, 0x00, 0x00, 0xff, 0x00,
|
|
160 0x06, 0xff, 0xff, 0x0b, 0xff, 0x00, 0x03, 0xff,
|
|
161 0xff, 0x0c, 0x01, 0xff, 0x04, 0xff, 0xff, 0x07,
|
|
162 0x06, 0xff, 0xff, 0x07, 0xff, 0x07, 0x07, 0x07,
|
|
163 0x06, 0xff, 0xff, 0x05, 0xff, 0x00, 0x0d, 0xff,
|
|
164 0x06, 0x06, 0x06, 0xff, 0x06, 0xff, 0xff, 0x07,
|
|
165 0xff, 0x02, 0x01, 0xff, 0x04, 0xff, 0xff, 0x09,
|
|
166 0x02, 0x02, 0xff, 0x02, 0xff, 0x02, 0x03, 0xff,
|
|
167 0x08, 0xff, 0xff, 0x05, 0xff, 0x00, 0x03, 0xff,
|
|
168 0xff, 0x02, 0x03, 0xff, 0x03, 0xff, 0x03, 0x03,
|
|
169 0x04, 0xff, 0xff, 0x05, 0x04, 0x04, 0x04, 0xff,
|
|
170 0xff, 0x02, 0x0f, 0xff, 0x04, 0xff, 0xff, 0x07,
|
|
171 0xff, 0x05, 0x05, 0x05, 0x04, 0xff, 0xff, 0x05,
|
|
172 0x06, 0xff, 0xff, 0x05, 0xff, 0x0e, 0x03, 0xff,
|
|
173 0xff, 0x0c, 0x01, 0xff, 0x0a, 0xff, 0xff, 0x09,
|
|
174 0x0a, 0xff, 0xff, 0x0b, 0x0a, 0x0a, 0x0a, 0xff,
|
|
175 0x08, 0xff, 0xff, 0x0b, 0xff, 0x00, 0x0d, 0xff,
|
|
176 0xff, 0x0b, 0x0b, 0x0b, 0x0a, 0xff, 0xff, 0x0b,
|
|
177 0x0c, 0x0c, 0xff, 0x0c, 0xff, 0x0c, 0x0d, 0xff,
|
|
178 0xff, 0x0c, 0x0f, 0xff, 0x0a, 0xff, 0xff, 0x07,
|
|
179 0xff, 0x0c, 0x0d, 0xff, 0x0d, 0xff, 0x0d, 0x0d,
|
|
180 0x06, 0xff, 0xff, 0x0b, 0xff, 0x0e, 0x0d, 0xff,
|
|
181 0x08, 0xff, 0xff, 0x09, 0xff, 0x09, 0x09, 0x09,
|
|
182 0xff, 0x02, 0x0f, 0xff, 0x0a, 0xff, 0xff, 0x09,
|
|
183 0x08, 0x08, 0x08, 0xff, 0x08, 0xff, 0xff, 0x09,
|
|
184 0x08, 0xff, 0xff, 0x0b, 0xff, 0x0e, 0x03, 0xff,
|
|
185 0xff, 0x0c, 0x0f, 0xff, 0x04, 0xff, 0xff, 0x09,
|
|
186 0x0f, 0xff, 0x0f, 0x0f, 0xff, 0x0e, 0x0f, 0xff,
|
|
187 0x08, 0xff, 0xff, 0x05, 0xff, 0x0e, 0x0d, 0xff,
|
|
188 0xff, 0x0e, 0x0f, 0xff, 0x0e, 0x0e, 0xff, 0x0e };
|
|
189
|
|
190
|
|
191 enum {
|
|
192 LAT_UNI=0,
|
|
193 RUS_UNI,
|
|
194 LANGS
|
|
195 };
|
|
196
|
|
197 // conversion table for chars 0x20-0x7F (UTF8)
|
|
198 // TODO: add another languages
|
|
199 static unsigned int lang_chars[LANGS][0x60]={
|
|
200 {
|
|
201 //Latin
|
|
202 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
|
|
203 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
|
|
204 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
|
|
205 0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
|
|
206 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,
|
|
207 0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
|
|
208 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,
|
|
209 0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
|
|
210 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
|
|
211 0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
|
|
212 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
|
|
213 0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f
|
|
214 },
|
|
215 {
|
|
216 //Russian
|
|
217 0x20,0x21,0x22,0x23,0x24,0x25,0x044b,0x27,
|
|
218 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
|
|
219 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
|
|
220 0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
|
|
221 0x042e,0x0410,0x0411,0x0426,0x0414,0x0415,0x0424,0x0413,
|
|
222 0x0425,0x0418,0x0419,0x041a,0x041b,0x041c,0x041d,0x041e,
|
|
223 0x041f,0x042f,0x0420,0x0421,0x0422,0x0423,0x0416,0x0412,
|
|
224 0x042c,0x042a,0x0417,0x0428,0x042d,0x0429,0x0427,0x042b,
|
|
225 0x044e,0x0430,0x0431,0x0446,0x0434,0x0435,0x0444,0x0433,
|
|
226 0x0445,0x0438,0x0439,0x043a,0x043b,0x043c,0x043d,0x043e,
|
|
227 0x043f,0x044f,0x0440,0x0441,0x0442,0x0443,0x0436,0x0432,
|
|
228 0x044c,0x044a,0x0437,0x0448,0x044d,0x0449,0x0447,0x044b
|
|
229 }
|
|
230 };
|
|
231
|
|
232 static int lang2id (int lang){
|
|
233 switch(lang){
|
|
234 case 0:
|
|
235 return LAT_UNI;
|
|
236 case 3: /* Stub. My teletext provider (1TV, Russia) sets this this language
|
|
237 code for russian teletext pages
|
|
238 TODO: make this configurable
|
|
239 */
|
|
240 return RUS_UNI;
|
|
241 default:
|
|
242 return LAT_UNI;
|
|
243 }
|
|
244 }
|
|
245
|
|
246 /**
|
|
247 * \brief convert chars from curent teletext codepage into MPlayer charset
|
|
248 * \param p raw teletext char to decode
|
|
249 * \param lang teletext internal language code (see lang2id)
|
|
250 * \return UTF8 char
|
|
251 *
|
|
252 * \remarks
|
|
253 * routine will analyze raw member of given tt_char structure and
|
|
254 * fill unicode member of the same struct with appropriate utf8 code.
|
|
255 */
|
|
256 static unsigned int conv2uni(unsigned int p,int lang)
|
|
257 {
|
|
258 if(p<0x80 && p>=0x20){
|
|
259 return lang_chars[lang][p-0x20];
|
|
260 }else
|
|
261 return 0x20;
|
|
262 }
|
|
263
|
|
264 static void init_vbi_consts(priv_vbi_t* priv){
|
|
265 int i,j;
|
|
266 double ang;
|
|
267 for(i=0; i<256; i++){
|
|
268 j=i&0x7F;
|
|
269 j^= j+j;
|
|
270 j^= j<<2;
|
|
271 j^= j<<4;
|
|
272 fixParity[i]= i ^ (j&0x80) ^ 0x80;
|
|
273 }
|
|
274
|
|
275 for(i=0,ang=0; i<12; i++,ang+=M_PI/priv->bpb){
|
|
276 si[i]= sin(ang);
|
|
277 co[i]= cos(ang);
|
|
278 }
|
|
279
|
|
280 priv->bpb=(priv->ptsp->sampling_rate/6937500.0)*ONE_FIXP+0.5;
|
24099
|
281 priv->soc=FFMAX(9.2e-6*priv->ptsp->sampling_rate-priv->ptsp->offset, 0);
|
|
282 priv->eoc=FFMIN(12.9e-6*priv->ptsp->sampling_rate-priv->ptsp->offset,
|
|
283 priv->ptsp->samples_per_line-43*8*priv->bpb/ONE_FIXP);
|
|
284 if (priv->eoc - priv->soc<16*priv->bpb/ONE_FIXP){ // invalid [soc:eoc]
|
|
285 priv->soc=0;
|
|
286 priv->eoc=92;
|
|
287 };
|
23899
|
288 priv->bp8bl=0.97*8*priv->bpb/ONE_FIXP; // -3% tolerance
|
|
289 priv->bp8bh=1.03*8*priv->bpb/ONE_FIXP; // +3% tolerance
|
|
290 }
|
|
291 /**
|
|
292 * \brief calculate increased/decreased by given value page number
|
|
293 * \param curr current page number in hexadecimal for
|
|
294 * \param direction decimal value (can be negative) to add to value
|
|
295 * of curr parameter
|
|
296 * \return new page number in hexadecimal form
|
|
297 *
|
|
298 * VBI page numbers are represented in special hexadecimal form, e.g.
|
|
299 * page with number 123 (as seen by user) internally has number 0x123.
|
|
300 * and equation 0x123+8 should be equal to 0x131 instead of regular 0x12b.
|
|
301 *
|
|
302 *
|
|
303 * Page numbers 0xYYY (where Y is not belongs to (0..9).
|
|
304 * Page number belongs to [0x000,0x799] or [0x100:0x899] (first 0 can be
|
|
305 * treated as '8')
|
|
306 */
|
|
307 static int steppage(int p, int direction, int skip_hidden)
|
|
308 {
|
|
309 if(skip_hidden)
|
|
310 p=(p&15)+((p>>4)&15)*10+(p>>8)*100;
|
|
311 p+=direction;
|
|
312 if(skip_hidden){
|
|
313 p=(p+800)%800;
|
|
314 p=(p%10)+((p/10)%10)*16+(p/100)*256;
|
|
315 }
|
|
316
|
|
317 return p&0x7ff;
|
|
318 }
|
|
319
|
|
320 /*
|
|
321 ------------------------------------------------------------------
|
|
322 Cache stuff
|
|
323 ------------------------------------------------------------------
|
|
324 */
|
|
325
|
|
326 /**
|
|
327 * \brief add/update entry in cache
|
|
328 * \param priv private data structure
|
|
329 * \param pg page to store in cache
|
|
330 * \param line line to update (value below 0 means update entire page)
|
|
331 */
|
|
332 static void put_to_cache(priv_vbi_t* priv,tt_page* pg,int line){
|
|
333 tt_page* pgc; //page in cache
|
|
334 int i,count;
|
|
335
|
|
336 if(line<0){
|
|
337 i=0;
|
|
338 count=VBI_ROWS*VBI_COLUMNS;
|
|
339 }else if(line<VBI_ROWS){
|
|
340 i=line*VBI_COLUMNS;
|
|
341 count=(line+1)*VBI_COLUMNS;
|
|
342 }else
|
|
343 return;
|
|
344
|
|
345 pthread_mutex_lock(&(priv->buffer_mutex));
|
|
346
|
|
347 if(!priv->ptt_cache[pg->pagenum]){
|
|
348 priv->ptt_cache[pg->pagenum]=calloc(1,sizeof(tt_page));
|
|
349 pgc=priv->ptt_cache[pg->pagenum];
|
|
350 }else{
|
|
351 pgc=priv->ptt_cache[pg->pagenum];
|
|
352 while(pgc->next_subpage && pgc->subpagenum!=pg->subpagenum)
|
|
353 pgc=pgc->next_subpage;
|
|
354
|
|
355 if(pgc->subpagenum!=pg->subpagenum){
|
|
356 pgc->next_subpage=calloc(1,sizeof(tt_page));
|
|
357 pgc=pgc->next_subpage;
|
|
358 }
|
|
359 }
|
|
360 pgc->pagenum=pg->pagenum;
|
|
361 pgc->subpagenum=pg->subpagenum;
|
|
362 pgc->lang=pg->lang;
|
|
363 pgc->flags=pg->flags;
|
|
364 //instead of copying entire page into cache, copy only undamaged
|
|
365 //symbols into cache
|
|
366 for(;i<count;i++){
|
|
367 if(!(pg->raw[i]&0x80))
|
|
368 pgc->raw[i]=pg->raw[i];
|
|
369 else
|
|
370 mp_msg(MSGT_TV,MSGL_DBG3,"char error. pg:%x, c[%d]=0x%x\n",
|
|
371 pg->pagenum,i,pg->raw[i]);
|
|
372 }
|
|
373 pgc->active=1;
|
|
374 pthread_mutex_unlock(&(priv->buffer_mutex));
|
|
375 }
|
|
376
|
|
377 /**
|
|
378 * \brief get any subpage number of given page
|
|
379 * \param priv private data structure
|
|
380 * \param pagenum page number to search subpages in
|
|
381 *
|
|
382 * \return subpage number of first found subpage which belongs to
|
|
383 * given page number
|
|
384 *
|
|
385 * \note page itself is subpage too (and usually has subpage number 0)
|
|
386 */
|
|
387 static inline int get_subpagenum_from_cache(priv_vbi_t* priv, int pagenum){
|
|
388 if (!priv->ptt_cache[pagenum])
|
|
389 return -1;
|
|
390 else
|
|
391 return priv->ptt_cache[pagenum]->subpagenum;
|
|
392 }
|
|
393
|
|
394 /**
|
|
395 * \brief get page from cache by it page and subpage number
|
|
396 * \param priv private data structure
|
|
397 * \param pagenum page number
|
|
398 * \param subpagenum subpage number
|
|
399 *
|
|
400 * \return pointer to tt_page structure if requested page is found
|
|
401 * and NULL otherwise
|
|
402 */
|
|
403 static inline tt_page* get_from_cache(priv_vbi_t* priv, int pagenum,int subpagenum){
|
|
404 tt_page* tp=priv->ptt_cache[pagenum];
|
|
405
|
|
406 while(tp && tp->subpagenum!=subpagenum)
|
|
407 tp=tp->next_subpage;
|
|
408 return tp;
|
|
409 }
|
|
410
|
|
411 /**
|
|
412 * \brief clears cache
|
|
413 * \param priv private data structure
|
|
414 *
|
|
415 * Deletes all tt_page structures from cache and frees allocated memory.
|
|
416 * Only zero-filled array of pointers remains in memory
|
|
417 */
|
|
418 static void clear_cache(priv_vbi_t* priv){
|
|
419 int i;
|
|
420 tt_page* tp;
|
|
421 for(i=0;i<VBI_MAX_PAGES;i++){
|
|
422 while(priv->ptt_cache[i]){
|
|
423 tp=priv->ptt_cache[i];
|
|
424 priv->ptt_cache[i]=tp->next_subpage;
|
|
425 free(tp);
|
|
426 }
|
|
427 }
|
|
428 }
|
|
429
|
|
430 /**
|
|
431 * \brief cache initialization
|
|
432 * \param priv private data structure
|
|
433 *
|
|
434 * \note Has to be called before any cache operations!
|
|
435 */
|
|
436 static void init_cache(priv_vbi_t* priv){
|
|
437 priv->ptt_cache=calloc(VBI_MAX_PAGES,sizeof(tt_page*));
|
|
438 }
|
|
439
|
|
440 /**
|
|
441 * \brief destroys cache
|
|
442 * \param priv private data structure
|
|
443 *
|
|
444 * Frees all memory allocated for cache (including array of pointers).
|
|
445 * It is safe to call this routine multiple times
|
|
446 */
|
|
447 static void destroy_cache(priv_vbi_t* priv){
|
|
448 if(priv->ptt_cache){
|
|
449 clear_cache(priv);
|
|
450 free(priv->ptt_cache);
|
|
451 priv->ptt_cache=NULL;
|
|
452 }
|
|
453 }
|
|
454
|
|
455 /*
|
|
456 ------------------------------------------------------------------
|
|
457 Decoder stuff
|
|
458 ------------------------------------------------------------------
|
|
459 */
|
|
460 /**
|
|
461 * \brief converts raw teletext page into useful format (1st rendering stage)
|
|
462 * \param pg page to decode
|
|
463 *
|
|
464 * Routine fills tt_char structure of each teletext_page character with proper
|
|
465 * info about foreground and background colors, character
|
|
466 * type (graphics/control/text).
|
|
467 */
|
|
468 static void decode_page(tt_char* p,int lang,unsigned char* raw)
|
|
469 {
|
|
470 int c,gfx=0,lat=0;
|
|
471 int i=0;
|
|
472 int fg_color=0;
|
|
473 int bg_color=0;
|
|
474 int row,col;
|
|
475 int separated=0;
|
|
476
|
|
477 for(row=0;row<VBI_ROWS;row++) {
|
|
478 lat=(lang==0);
|
|
479 gfx=0;
|
|
480 fg_color=7;
|
|
481 bg_color=0;
|
|
482 separated=0;
|
|
483 for(col=0;col<VBI_COLUMNS;col++){
|
|
484 i=row*VBI_COLUMNS+col;
|
|
485 c=raw[i];
|
|
486 p[i].raw=c;
|
|
487 if(c&0x80){ //damaged char
|
|
488 p[i]=tt_error;
|
|
489 continue;
|
|
490 }
|
|
491 p[i].gfx=gfx?(separated?2:1):0;
|
|
492 p[i].lng=lat?0:lang;
|
|
493 p[i].ctl=(c&0x60)==0?1:0;
|
|
494 p[i].fg=fg_color;
|
|
495 p[i].bg=bg_color;
|
|
496
|
|
497 if ((c&0x60)==0){ //control chars
|
|
498 if(c>=0x08 && c<=0x0f){
|
|
499 }else if (c<=0x17){ //colors
|
|
500 fg_color=c&0x0f;
|
|
501 gfx=c>>4;
|
|
502 }else if (c<=0x18){
|
|
503 }else if (c<=0x1a){ //Contiguous/Separated gfx
|
|
504 separated=!(c&1);
|
|
505 }else if (c<=0x1b){
|
|
506 lat=!lat;
|
|
507 }else if (c<=0x1d){
|
|
508 bg_color=(c&1)?fg_color:0;
|
|
509 p[i].bg=bg_color;
|
|
510 }
|
|
511 p[i].ctl=1;
|
|
512 p[i].unicode=p[i].gfx?0:' ';
|
|
513 continue;
|
|
514 }
|
|
515
|
|
516 if(gfx){
|
|
517 p[i].unicode=c-0x20;
|
|
518 if (p[i].unicode>0x3f) p[i].unicode-=0x20;
|
|
519 }else
|
|
520 p[i].unicode=conv2uni(c,lang2id(p[i].lng));
|
|
521
|
|
522 p[i].fg=fg_color;
|
|
523 p[i].bg=bg_color;
|
|
524 }
|
|
525 }
|
|
526 }
|
|
527
|
|
528 /**
|
|
529 * \brief prepares current page for displaying
|
|
530 * \param priv_vbi private data structure
|
|
531 *
|
|
532 * Routine adds some useful info (time and page number of page, grabbed by
|
|
533 * background thread to top line of current page). Displays "No teletext"
|
|
534 * string if no vbi data available.
|
|
535 */
|
23908
|
536 #define PRINT_HEX(dp,i,h) dp[i].unicode=((h)&0xf)>9?'A'+((h)&0xf)-10:'0'+((h)&0xf)
|
23899
|
537 static void prepare_visible_page(priv_vbi_t* priv){
|
|
538 tt_page *pg,*curr_pg;
|
|
539 unsigned char *p;
|
|
540 int i;
|
|
541
|
|
542 pthread_mutex_lock(&(priv->buffer_mutex));
|
|
543 mp_msg(MSGT_TV,MSGL_DBG3,"tvi_vbi: prepare_visible_page pg:0x%x, sub:0x%x\n",
|
|
544 priv->pagenum,priv->subpagenum);
|
|
545 if(priv->subpagenum==-1) //no page yet
|
|
546 priv->subpagenum=get_subpagenum_from_cache(priv,priv->pagenum);
|
|
547
|
|
548 pg=get_from_cache(priv,priv->pagenum,priv->subpagenum);
|
|
549 mp_dbg(MSGT_TV,MSGL_DBG3,"tvi_vbi: prepare_vibible_page2 pg:0x%x, sub:0x%x\n",
|
|
550 priv->pagenum,priv->subpagenum);
|
|
551
|
|
552 curr_pg=get_from_cache(priv,priv->curr_pagenum,
|
|
553 get_subpagenum_from_cache(priv,priv->curr_pagenum));
|
|
554 if (!pg && !curr_pg){
|
|
555 p=MSGTR_TV_NoTeletext;
|
|
556 for(i=0;i<VBI_COLUMNS && *p;i++){
|
|
557 GET_UTF8(priv->display_page[i].unicode,*p++,break;);
|
|
558 }
|
|
559 for(;i<VBI_ROWS*VBI_COLUMNS;i++)
|
|
560 priv->display_page[i]=tt_space;
|
|
561 pthread_mutex_unlock(&(priv->buffer_mutex));
|
|
562 return;
|
|
563 }
|
|
564
|
|
565 if (!pg || !pg->active){
|
|
566 for(i=0;i<VBI_ROWS*VBI_COLUMNS;i++){
|
|
567 priv->display_page[i]=tt_space;
|
|
568 }
|
|
569 }else{
|
|
570 decode_page(priv->display_page,pg->lang,pg->raw);
|
|
571 mp_msg(MSGT_TV,MSGL_DBG3,"page #%x was decoded!\n",pg->pagenum);
|
|
572 }
|
|
573
|
|
574 PRINT_HEX(priv->display_page,0,(priv->curr_pagenum&0x700)?priv->curr_pagenum>>8:8);
|
|
575 PRINT_HEX(priv->display_page,1,priv->curr_pagenum>>4);
|
|
576 PRINT_HEX(priv->display_page,2,priv->curr_pagenum);
|
|
577 priv->display_page[3].unicode=' ';
|
|
578 priv->display_page[4].unicode=' ';
|
|
579 switch(priv->pagenumdec>>12){
|
|
580 case 1:
|
|
581 priv->display_page[5].unicode='_';
|
|
582 priv->display_page[6].unicode='_';
|
|
583 PRINT_HEX(priv->display_page,7,priv->pagenumdec);
|
|
584 break;
|
|
585 case 2:
|
|
586 priv->display_page[5].unicode='_';
|
|
587 PRINT_HEX(priv->display_page,6,priv->pagenumdec>>4);
|
|
588 PRINT_HEX(priv->display_page,7,priv->pagenumdec);
|
|
589 break;
|
|
590 default:
|
|
591 PRINT_HEX(priv->display_page,5,(priv->pagenum&0x700)?priv->pagenum>>8:8);
|
|
592 PRINT_HEX(priv->display_page,6,priv->pagenum>>4);
|
|
593 PRINT_HEX(priv->display_page,7,priv->pagenum);
|
|
594 }
|
|
595 if(priv->subpagenum!=-1){
|
|
596 priv->display_page[8].unicode='.';
|
|
597 PRINT_HEX(priv->display_page,9,priv->subpagenum>>4);
|
|
598 PRINT_HEX(priv->display_page,10,priv->subpagenum);
|
|
599 }else{
|
|
600 priv->display_page[8].unicode=' ';
|
|
601 priv->display_page[9].unicode=' ';
|
|
602 priv->display_page[10].unicode=' ';
|
|
603 }
|
|
604 priv->display_page[11].unicode=' ';
|
|
605 for(i=VBI_TIME_LINEPOS;i<VBI_COLUMNS;i++){
|
|
606 priv->display_page[i].unicode=curr_pg->raw[i];
|
|
607 }
|
|
608 pthread_mutex_unlock(&(priv->buffer_mutex));
|
|
609 }
|
|
610 /*
|
|
611 ------------------------------------------------------------------
|
|
612 Renderer stuff
|
|
613 ------------------------------------------------------------------
|
|
614 */
|
|
615 #ifdef DEBUG_DUMP
|
|
616 /**
|
|
617 * \brief renders teletext page into given file
|
|
618 * \param pt page to render
|
|
619 * \param f opened file descriptor
|
|
620 * \param pagenum which page to render
|
|
621 * \param colored use colors not implementede yet)
|
|
622 *
|
|
623 * Text will be UTF8 encoded
|
|
624 */
|
|
625 static void render2text(tt_page* pt,FILE* f,int colored){
|
|
626 int i,j;
|
|
627 unsigned int u;
|
|
628 unsigned char buf[8];
|
|
629 unsigned char tmp;
|
|
630 int pos;
|
|
631 tt_char dp[VBI_ROWS*VBI_COLUMNS];
|
|
632 int color=0;
|
|
633 int bkg=0;
|
|
634 int c1,b1;
|
|
635 if(!pt)
|
|
636 return;
|
|
637 fprintf(f,"+========================================+\n");
|
|
638 fprintf(f,"| lang:%d pagenum:0x%x subpagenum:%d flags:0x%x|\n",
|
|
639 pt->lang,
|
|
640 pt->pagenum,
|
|
641 pt->subpagenum,
|
|
642 0);
|
|
643 fprintf(f,"+----------------------------------------+\n");
|
|
644
|
|
645 decode_page(dp,pt->lang,pt->raw);
|
|
646 for(i=0;i<VBI_ROWS;i++){
|
|
647 fprintf(f,"|");
|
|
648 if(colored) fprintf(f,"\033[40m");
|
|
649 for(j=0;j<VBI_COLUMNS;j++)
|
|
650 {
|
|
651 u=dp[i*VBI_COLUMNS+j].unicode;
|
23928
|
652 if(dp[i*VBI_COLUMNS+j].fg <= 7)
|
23899
|
653 c1=30+dp[i*VBI_COLUMNS+j].fg;
|
23928
|
654 else
|
23899
|
655 c1=38;
|
23928
|
656 if(dp[i*VBI_COLUMNS+j].bg <= 7)
|
23899
|
657 b1=40+dp[i*VBI_COLUMNS+j].bg;
|
23928
|
658 else
|
23899
|
659 b1=40;
|
|
660 if (b1!=bkg && colored){
|
|
661 fprintf(f,"\033[%dm",b1);
|
|
662 bkg=b1;
|
|
663 }
|
|
664 if(c1!=color && colored){
|
|
665 fprintf(f,"\033[%dm",c1);
|
|
666 color=c1;
|
|
667 }
|
|
668 if(dp[i*VBI_COLUMNS+j].gfx){
|
|
669 fprintf(f,"*");
|
|
670 }else{
|
|
671 pos=0;
|
|
672 PUT_UTF8(u,tmp,if(pos<7) buf[pos++]=tmp;);
|
|
673 buf[pos]='\0';
|
|
674 fprintf(f,"%s",buf);
|
|
675 }
|
|
676 }
|
|
677
|
|
678 if (colored) fprintf(f,"\033[0m");
|
|
679 color=-1;bkg=-1;
|
|
680 fprintf(f,"|\n");
|
|
681 }
|
|
682 #if 1
|
|
683 //for debug
|
|
684 fprintf(f,"+====================raw=================+\n");
|
|
685 for(i=0;i<VBI_ROWS;i++){
|
|
686 for(j=0;j<VBI_COLUMNS;j++)
|
|
687 fprintf(f,"%02x ",dp[i*VBI_COLUMNS+j].raw);
|
|
688 fprintf(f,"\n");
|
|
689 }
|
|
690 fprintf(f,"+====================lng=================+\n");
|
|
691 for(i=0;i<VBI_ROWS;i++){
|
|
692 for(j=0;j<VBI_COLUMNS;j++)
|
|
693 fprintf(f,"%02x ",dp[i*VBI_COLUMNS+j].lng);
|
|
694 fprintf(f,"\n");
|
|
695 }
|
|
696 #endif
|
|
697 fprintf(f,"+========================================+\n");
|
|
698 }
|
|
699
|
|
700 /**
|
|
701 * \brief dump page into pgXXX.txt file in vurrent directory
|
|
702 * \param pt page to dump
|
|
703 *
|
|
704 * \note XXX in filename is page number
|
|
705 * \note use only for debug purposes
|
|
706 */
|
|
707 static void dump_page(tt_page* pt)
|
|
708 {
|
|
709 FILE*f;
|
|
710 char name[100];
|
|
711 snprintf(name,99,"pg%x.txt",pt->pagenum);
|
|
712 f=fopen(name,"wb");
|
|
713 render2text(pt,f,1);
|
|
714 fclose(f);
|
|
715 }
|
|
716 #endif //DEBUG_DUMP
|
|
717
|
|
718
|
|
719 /**
|
|
720 * \brief checks whether page is ready and copies it into cache array if so
|
|
721 * \param priv private data structure
|
|
722 * \param magAddr page's magazine address (0-7)
|
|
723 *
|
|
724 * Routine also calls decode_page to perform 1st stage of rendering
|
|
725 */
|
|
726 static void store_in_cache(priv_vbi_t* priv, int magAddr, int line){
|
|
727 mp_msg(MSGT_TV,MSGL_DBG2,"store_in_cache(%d): pagenum:%x\n",
|
|
728 priv->mag[magAddr].order,
|
|
729 priv->mag[magAddr].pt->pagenum);
|
|
730
|
|
731 put_to_cache(priv,priv->mag[magAddr].pt,line);
|
|
732 priv->curr_pagenum=priv->mag[magAddr].pt->pagenum;
|
|
733
|
|
734 #ifdef DEBUG_DUMP
|
|
735 dump_page(get_from_cache(priv,
|
|
736 priv->mag[magAddr].pt->pagenum,
|
|
737 priv->mag[magAddr].pt->subpagenum));
|
|
738 #endif
|
|
739 }
|
|
740
|
|
741
|
|
742 /*
|
|
743 ------------------------------------------------------------------
|
|
744 Grabber stuff
|
|
745 ------------------------------------------------------------------
|
|
746 */
|
|
747 #define PLL_SAMPLES 4
|
|
748 #define PLL_ERROR 4
|
|
749 #define PLL_ADJUST 4
|
|
750
|
|
751 /**
|
|
752 * \brief adjust current phase for better signal decoding
|
|
753 * \param n count of bytes processed (?)
|
|
754 * \param err count of error bytes (?)
|
|
755 *
|
|
756 * \remarks code was got from MythTV project
|
|
757 */
|
|
758 static void pll_add(priv_vbi_t* priv,int n,int err){
|
|
759 if(priv->pll_fixed)
|
|
760 return;
|
|
761 if(err>PLL_ERROR*2/3)
|
|
762 err=PLL_ERROR*2/3;
|
|
763 priv->pll_err+=err;
|
|
764 priv->pll_cnt+=n;
|
|
765 if(priv->pll_cnt<PLL_SAMPLES)
|
|
766 return;
|
|
767 if(priv->pll_err>PLL_ERROR)
|
|
768 {
|
|
769 if(priv->pll_err>priv->pll_lerr)
|
|
770 priv->pll_dir= -priv->pll_dir;
|
|
771 priv->pll_lerr=priv->pll_err;
|
|
772 priv->pll_adj+=priv->pll_dir;
|
|
773 if (priv->pll_adj<-PLL_ADJUST || priv->pll_adj>PLL_ADJUST)
|
|
774 {
|
|
775 priv->pll_adj=0;
|
|
776 priv->pll_dir=-1;
|
|
777 priv->pll_lerr=0;
|
|
778 }
|
|
779 mp_msg(MSGT_TV,MSGL_DBG3,"vbi: pll_adj=%2d\n",priv->pll_adj);
|
|
780 }
|
|
781 priv->pll_cnt=0;
|
|
782 priv->pll_err=0;
|
|
783 }
|
|
784
|
|
785 /**
|
|
786 * \brief reset error correction
|
|
787 * \param priv private data structure
|
|
788 * \param fine_tune shift value for adjusting
|
|
789 *
|
|
790 * \remarks code was got from MythTV project
|
|
791 */
|
|
792 static void pll_reset(priv_vbi_t* priv,int fine_tune){
|
|
793 priv->pll_fixed=fine_tune >= -PLL_ADJUST && fine_tune <= PLL_ADJUST;
|
|
794
|
|
795 priv->pll_err=0;
|
|
796 priv->pll_lerr=0;
|
|
797 priv->pll_cnt=0;
|
|
798 priv->pll_dir=-1;
|
|
799 priv->pll_adj=0;
|
|
800 if(priv->pll_fixed)
|
|
801 priv->pll_adj=fine_tune;
|
|
802 if(priv->pll_fixed)
|
|
803 mp_msg(MSGT_TV,MSGL_DBG3,"pll_reset (fixed@%2d)\n",priv->pll_adj);
|
|
804 else
|
|
805 mp_msg(MSGT_TV,MSGL_DBG3,"pll_reset (auto)\n");
|
|
806
|
|
807 }
|
|
808 /**
|
|
809 * \brief decode packet 0 (teletext page header)
|
|
810 * \param priv private data structure
|
|
811 * \param data raw teletext data (with not applied hamm correction yet)
|
|
812 * \param magAddr teletext page's magazine address
|
|
813 *
|
|
814 * \remarks
|
|
815 * data buffer was shifted by 6 and now contains:
|
|
816 * 0..1 page number
|
|
817 * 2..5 sub-code
|
|
818 * 6..7 control codes
|
|
819 * 8..39 display data
|
|
820 *
|
|
821 * only first 8 bytes protected by Hamm 8/4 code
|
|
822 */
|
|
823 static int decode_pkt0(priv_vbi_t* priv,unsigned char* data,int magAddr)
|
|
824 {
|
|
825 int d[8];
|
|
826 int i,err;
|
|
827
|
|
828 if (magAddr<0 || magAddr>7)
|
|
829 return 0;
|
|
830 for(i=0;i<8;i++){
|
|
831 d[i]= corrHamm48[ data[i] ];
|
|
832 if(d[i]&0x80){
|
|
833 pll_add(priv,2,4);
|
|
834
|
|
835 if(priv->mag[magAddr].pt)
|
|
836 free(priv->mag[magAddr].pt);
|
|
837 priv->mag[magAddr].pt=NULL;
|
|
838 priv->mag[magAddr].order=0;
|
|
839 return 0;
|
|
840 }
|
|
841 }
|
|
842 if (!priv->mag[magAddr].pt)
|
|
843 priv->mag[magAddr].pt= malloc(sizeof(tt_page));
|
|
844
|
|
845 priv->mag[magAddr].lang= d[7] & 0x7;
|
|
846 priv->mag[magAddr].pt->lang=priv->mag[magAddr].lang;
|
|
847 priv->mag[magAddr].pt->subpagenum=(d[2]|(d[3]<<4)|(d[4]<<8)|(d[5]<<12))&0x3f7f;
|
|
848 priv->mag[magAddr].pt->pagenum=(magAddr<<8) | d[0] | (d[1]<<4);
|
|
849 priv->mag[magAddr].pt->flags=( d[6] | (d[7]<<4));
|
|
850
|
|
851 memset(priv->mag[magAddr].pt->raw, 0x00, VBI_COLUMNS*VBI_ROWS);
|
|
852 priv->mag[magAddr].order=0;
|
|
853
|
|
854 for(i=0;i<8;i++){
|
|
855 priv->mag[magAddr].pt->raw[i]=0x20;
|
|
856 }
|
|
857 err=0;
|
|
858 for(i=8; i<VBI_COLUMNS; i++){
|
|
859 data[i]= fixParity[data[i]];
|
|
860 priv->mag[magAddr].pt->raw[i]=data[i];
|
|
861 if(data[i]&0x80) //Error
|
|
862 err++;
|
|
863 pll_add(priv,1,err);
|
|
864 }
|
|
865
|
|
866 store_in_cache(priv,magAddr,0);
|
|
867
|
|
868 return 1;
|
|
869 }
|
|
870
|
|
871 /**
|
|
872 * \brief decode packets 1..24 (teletext page header)
|
|
873 * \param priv private data structure
|
|
874 * \param data raw teletext data
|
|
875 * \param magAddr teletext page's magazine address
|
|
876 * \param rowAddr teletext page's row number
|
|
877 *
|
|
878 * \remarks
|
|
879 * data buffer was shifted by 6 and now contains 40 bytes of display data:
|
|
880 * this type of packet is not proptected by Hamm 8/4 code
|
|
881 */
|
|
882 static void decode_pkt_page(priv_vbi_t* priv,unsigned char*data,int magAddr,int rowAddr){
|
|
883 int i,err;
|
|
884 if (!priv->mag[magAddr].pt)
|
|
885 return;
|
|
886
|
|
887 priv->mag[magAddr].order=rowAddr;
|
|
888
|
|
889 err=0;
|
|
890 for(i=0; i<VBI_COLUMNS; i++){
|
|
891 data[i]= fixParity[ data[i] ];
|
|
892 priv->mag[magAddr].pt->raw[i+rowAddr*VBI_COLUMNS]=data[i];
|
|
893 if( data[i]&0x80) //HammError
|
|
894 err++;
|
|
895 }
|
|
896 pll_add(priv,1,err);
|
|
897
|
|
898 store_in_cache(priv,magAddr,rowAddr);
|
|
899 }
|
|
900
|
|
901 /**
|
|
902 * \brief decodes raw vbi data (signal amplitudes) into sequence of bytes
|
|
903 * \param priv private data structure
|
|
904 * \param buf raw vbi data (one line of frame)
|
|
905 * \param data output buffer for decoded bytes (at least 45 bytes long)
|
|
906 *
|
|
907 * Used XawTV's algorithm. Signal phase is calculated with help of starting clock
|
|
908 * run-in sequence (min/max values and bit distance values are calculated)
|
|
909 */
|
|
910 static int decode_raw_line_runin(priv_vbi_t* priv,unsigned char* buf,unsigned char* data){
|
|
911 const int magic= 0x27; // reversed 1110010
|
|
912 int dt[256],hi[6],lo[6];
|
|
913 int i,x,r;
|
|
914 int decoded;
|
|
915 int sync;
|
|
916 unsigned char min,max;
|
|
917 int thr=0; //threshold
|
|
918
|
|
919 //stubs
|
24099
|
920 int soc=priv->soc;
|
|
921 int eoc=priv->eoc;
|
23899
|
922
|
|
923 for(i=soc;i<eoc;i++)
|
|
924 dt[i]=buf[i+priv->bpb/ONE_FIXP]-buf[i]; // amplifies the edges best.
|
|
925 /* set barrier */
|
|
926 for (i=eoc; i<eoc+16; i+=2)
|
|
927 dt[i]=100, dt[i+1]=-100;
|
|
928
|
|
929 /* find 6 rising and falling edges */
|
|
930 for (i=soc, x=0; x<6; ++x)
|
|
931 {
|
|
932 while (dt[i]<32)
|
|
933 i++;
|
|
934 hi[x]=i;
|
|
935 while (dt[i]>-32)
|
|
936 i++;
|
|
937 lo[x]=i;
|
|
938 }
|
|
939 if (i>=eoc)
|
|
940 {
|
|
941 return 0; // not enough periods found
|
|
942 }
|
|
943 i=hi[5]-hi[1]; // length of 4 periods (8 bits)
|
|
944 if (i<priv->bp8bl || i>priv->bp8bh)
|
|
945 {
|
|
946 mp_msg(MSGT_TV,MSGL_DBG3,"vbi: wrong freq %d (%d,%d)\n",
|
|
947 i,priv->bp8bl,priv->bp8bh);
|
|
948 return 0; // bad frequency
|
|
949 }
|
|
950 /* AGC and sync-reference */
|
|
951 min=255, max=0, sync=0;
|
|
952 for (i=hi[4]; i<hi[5]; ++i)
|
|
953 if (buf[i]>max)
|
|
954 max=buf[i], sync=i;
|
|
955 for (i=lo[4]; i<lo[5]; ++i)
|
|
956 if (buf[i]<min)
|
|
957 min=buf[i];
|
|
958 thr=(min+max)/2;
|
|
959
|
|
960 buf+=sync;
|
|
961 // searching for '11'
|
|
962 for(i=priv->pll_adj*priv->bpb/10;i<16*priv->bpb;i+=priv->bpb)
|
|
963 if(buf[FIXP2INT(i)]>thr && buf[FIXP2INT(i+priv->bpb)]>thr)
|
|
964 break;
|
|
965 r=0;
|
|
966 for(decoded=1; decoded<= (VBI_COLUMNS+3)<<3;decoded++){
|
|
967 r>>=1;
|
|
968 if(buf[FIXP2INT(i)]>thr) r|=0x80;
|
|
969 if(!(decoded & 0x07)){
|
|
970 data[(decoded>>3) - 1]=r;
|
|
971 r=0;
|
|
972 }
|
|
973 i+=priv->bpb;
|
|
974 }
|
|
975 if(data[0]!=magic)
|
|
976 return 0; //magic not found
|
|
977
|
|
978 //stub
|
|
979 for(i=0;i<43;i++){
|
|
980 data[i]=data[i+1];
|
|
981 }
|
|
982 mp_msg(MSGT_TV,MSGL_DBG3,"thr:%d sync:%d ",thr,sync);
|
|
983
|
|
984 return 1;
|
|
985 }
|
|
986
|
|
987 /**
|
|
988 * \brief decodes raw vbi data (signal amplitudes) into sequence of bytes
|
|
989 * \param priv private data structure
|
|
990 * \param buf raw vbi data (one line of frame)
|
|
991 * \param data output buffer for decoded bytes (at least 45 bytes long)
|
|
992 *
|
|
993 * Used Michael Niedermayer's algorithm.
|
|
994 * Signal phase is calculated using correlation between given samples data and
|
|
995 * pure sine
|
|
996 */
|
|
997 static int decode_raw_line_sine(priv_vbi_t* priv,unsigned char* buf,unsigned char* data){
|
|
998 int i,x,r,amp,xFixp;
|
|
999 int avg=0;
|
|
1000 double sin_sum=0, cos_sum=0;
|
|
1001
|
|
1002 for(x=0; x< FIXP2INT(10*priv->bpb); x++)
|
|
1003 avg+=buf[x];
|
|
1004
|
|
1005 avg/=FIXP2INT(10*priv->bpb);
|
|
1006
|
|
1007 for(x=0; x<12; x++){
|
|
1008 amp= buf[x<<1];
|
|
1009 sin_sum+= si[x]*(amp-avg);
|
|
1010 cos_sum+= co[x]*(amp-avg);
|
|
1011 }
|
|
1012 //this is always zero. Why ?
|
|
1013 xFixp= atan(sin_sum/cos_sum)*priv->bpb/M_PI;
|
|
1014
|
|
1015 //Without this line the result is full of errors
|
|
1016 //and routine is unable to find magic sequence
|
|
1017 buf+=FIXP2INT(10*priv->bpb);
|
|
1018
|
|
1019 r=0;
|
|
1020 for(x=FIXP2INT(xFixp);x<70;x=FIXP2INT(xFixp)){
|
|
1021 r=(r<<1) & 0xFFFF;
|
|
1022 if(buf[x]>avg) r|=1;
|
|
1023 xFixp+=priv->bpb;
|
|
1024 if(r==0xAAE4) break;
|
|
1025 }
|
|
1026
|
|
1027 //this is not teletext
|
|
1028 if (r!=0xaae4) return 0;
|
|
1029
|
|
1030 //Decode remaining 45-2(clock run-in)-1(framing code)=42 bytes
|
|
1031 for(i=1; i<=(42<<3); i++){
|
|
1032 r>>=1;
|
|
1033 x=FIXP2INT(xFixp);
|
|
1034 if(buf[x]> avg)
|
|
1035 r|=0x80;
|
|
1036
|
|
1037 if(!(i & 0x07)){
|
|
1038 data[(i>>3)-1]=r;
|
|
1039 r=0;
|
|
1040 }
|
|
1041 xFixp+=priv->bpb;
|
|
1042 }
|
|
1043
|
|
1044 return 1;
|
|
1045 }
|
|
1046
|
|
1047 /**
|
|
1048 * \brief decodes all vbi lines from one video frame
|
|
1049 * \param priv private data structure
|
|
1050 * \param buf buffer with raw vbi data in it
|
|
1051 *
|
|
1052 * \note buffer size have to be at least priv->ptsp->bufsize bytes
|
|
1053 */
|
|
1054 static void vbi_decode(priv_vbi_t* priv,unsigned char*buf){
|
|
1055 int magAddr;
|
|
1056 int pkt;
|
|
1057 unsigned char data[64];
|
|
1058 unsigned char* linep;
|
|
1059 int d0,d1;
|
|
1060 int i=0;
|
|
1061 mp_msg(MSGT_TV,MSGL_DBG3,"vbi: vbi_decode\n");
|
|
1062 for(linep=buf; linep<buf+priv->ptsp->bufsize; linep+=priv->ptsp->samples_per_line,i++){
|
|
1063 #if 0
|
|
1064 /*
|
|
1065 This routine is alternative implementation of raw VBI data decoding.
|
|
1066 Unfortunately, it detects only about 20% of incoming data,
|
|
1067 but Michael says that this algorithm is better, and he wants to fix it.
|
|
1068 */
|
|
1069 if(decode_raw_line_sine(priv,linep,data)<=0){
|
|
1070 #endif
|
|
1071 if(decode_raw_line_runin(priv,linep,data)<=0){
|
|
1072 continue; //this is not valid teletext line
|
|
1073 }
|
|
1074 d0= corrHamm48[ data[0] ];
|
|
1075 d1= corrHamm48[ data[1] ];
|
|
1076
|
|
1077 if(d0&0x80 || d1&0x80){
|
|
1078 pll_add(priv,2,4);
|
|
1079 mp_msg(MSGT_TV,MSGL_V,"vbi_decode(%d):HammErr after decode_raw_line\n",i);
|
|
1080
|
|
1081 continue; //hamError
|
|
1082 }
|
|
1083 magAddr=d0 & 0x7;
|
|
1084 pkt=(d0>>3)|(d1<<1);
|
|
1085 mp_msg(MSGT_TV,MSGL_DBG3,"vbi_decode(%d):%x %x (mag:%x, pkt:%d)\n",
|
|
1086 i,d0,d1,magAddr,pkt);
|
|
1087 if(!pkt){
|
|
1088 decode_pkt0(priv,data+2,magAddr); //skip MRGA
|
|
1089 }else if(pkt>0 && pkt<VBI_ROWS){
|
|
1090 if(!priv->mag[magAddr].pt) continue;
|
|
1091 decode_pkt_page(priv,data+2,magAddr,pkt);//skip MRGA
|
|
1092 } else {
|
|
1093 mp_msg(MSGT_TV,MSGL_DBG3,"unsupported packet:%d\n",pkt);
|
|
1094 }
|
|
1095 }
|
|
1096
|
|
1097 }
|
|
1098
|
|
1099 /*
|
|
1100 ---------------------------------------------------------------------------------
|
|
1101 Public routines
|
|
1102 ---------------------------------------------------------------------------------
|
|
1103 */
|
|
1104
|
|
1105 /**
|
|
1106 * \brief toggles teletext page displaying format
|
|
1107 * \param priv_vbi private data structure
|
|
1108 * \param flag new format
|
|
1109 * \return
|
|
1110 * TVI_CONTROL_TRUE is success,
|
|
1111 * TVI_CONTROL_FALSE otherwise
|
|
1112 *
|
|
1113 * flag:
|
|
1114 * 0 - opaque
|
|
1115 * 1 - transparent
|
|
1116 * 2 - opaque with black foreground color (only in bw mode)
|
|
1117 * 3 - transparent with black foreground color (only in bw mode)
|
|
1118 */
|
|
1119 static int teletext_set_format(priv_vbi_t * priv, teletext_format flag)
|
|
1120 {
|
|
1121 flag&=3;
|
|
1122
|
|
1123 mp_msg(MSGT_TV,MSGL_DBG3,"teletext_set_format_is called. mode:%d\n",flag);
|
|
1124 pthread_mutex_lock(&(priv->buffer_mutex));
|
|
1125
|
|
1126 priv->tformat=flag;
|
|
1127
|
|
1128 priv->pagenumdec=0;
|
|
1129
|
|
1130 pthread_mutex_unlock(&(priv->buffer_mutex));
|
|
1131 return TVI_CONTROL_TRUE;
|
|
1132 }
|
|
1133
|
|
1134 /**
|
|
1135 * \brief append just entered digit to editing page number
|
|
1136 * \param priv_vbi private data structure
|
|
1137 * \param dec decimal digit to append
|
|
1138 *
|
|
1139 * dec:
|
|
1140 * '0'..'9' append digit
|
|
1141 * '-' remove last digit (backspace emulation)
|
|
1142 *
|
|
1143 * This routine allows user to jump to arbitrary page.
|
|
1144 * It implements simple page number editing algorithm.
|
|
1145 *
|
|
1146 * Subsystem can be on one of two modes: normal and page number edit mode.
|
|
1147 * Zero value of priv->pagenumdec means normal mode
|
|
1148 * Non-zero value means page number edit mode and equals to packed
|
|
1149 * decimal number of already entered part of page number.
|
|
1150 *
|
|
1151 * How this works.
|
|
1152 * Let's assume that current mode is normal (pagenumdec is zero), teletext page
|
|
1153 * 100 are displayed as usual. topmost left corner of page contains page number.
|
|
1154 * Then vbi_add_dec is sequentially called (through slave
|
|
1155 * command of course) with 1,4,-,2,3 * values of dec parameter.
|
|
1156 *
|
|
1157 * +-----+------------+------------------+
|
|
1158 * | dec | pagenumdec | displayed number |
|
|
1159 * +-----+------------+------------------+
|
|
1160 * | | 0x000 | 100 |
|
|
1161 * +-----+------------+------------------+
|
|
1162 * | 1 | 0x001 | __1 |
|
|
1163 * +-----+------------+------------------+
|
|
1164 * | 4 | 0x014 | _14 |
|
|
1165 * +-----+------------+------------------+
|
|
1166 * | - | 0x001 | __1 |
|
|
1167 * +-----+------------+------------------+
|
|
1168 * | 2 | 0x012 | _12 |
|
|
1169 * +-----+------------+------------------+
|
|
1170 * | 3 | 0x123 | 123 |
|
|
1171 * +-----+------------+------------------+
|
|
1172 * | | 0x000 | 123 |
|
|
1173 * +-----+------------+------------------+
|
|
1174 *
|
|
1175 * pagenumdec will automatically receive zero value after third digit of page
|
|
1176 * number is entered and current page will be switched to another one with
|
|
1177 * entered page number.
|
|
1178 */
|
|
1179 static void vbi_add_dec(priv_vbi_t * priv, char *dec)
|
|
1180 {
|
|
1181 int count, shift;
|
|
1182 if (!dec)
|
|
1183 return;
|
|
1184 if (!priv->on)
|
|
1185 return;
|
|
1186 if ((*dec<'0' || *dec>'9') && *dec!='-')
|
|
1187 return;
|
|
1188 if (!priv->pagenumdec) //first digit cannot be '0','9' or '-'
|
|
1189 if(*dec=='-' || *dec=='0' || *dec=='9')
|
|
1190 return;
|
|
1191 pthread_mutex_lock(&(priv->buffer_mutex));
|
|
1192 count=(priv->pagenumdec>>12)&0xf;
|
|
1193 if (*dec=='-') {
|
|
1194 count--;
|
|
1195 if (count)
|
|
1196 priv->pagenumdec=((priv->pagenumdec>>4)&0xfff)|(count<<12);
|
|
1197 else
|
|
1198 priv->pagenumdec=0;
|
|
1199 } else {
|
|
1200 shift = count * 4;
|
|
1201 count++;
|
|
1202 priv->pagenumdec=
|
|
1203 (((priv->pagenumdec)<<4|(*dec-'0'))&0xfff)|(count<<12);
|
|
1204 if (count==3) {
|
|
1205 priv->pagenum=priv->pagenumdec&0x7ff;
|
|
1206 priv->subpagenum=get_subpagenum_from_cache(priv,priv->pagenum);
|
|
1207 priv->pagenumdec=0;
|
|
1208 }
|
|
1209 }
|
|
1210 pthread_mutex_unlock(&(priv->buffer_mutex));
|
|
1211 }
|
|
1212
|
|
1213
|
|
1214 /**
|
|
1215 * \brief Teletext control routine
|
|
1216 * \param priv_vbi private data structure
|
|
1217 * \param cmd command
|
|
1218 * \param arg command parameter (has to be not null)
|
|
1219 */
|
|
1220 int teletext_control(void* p, int cmd, void *arg)
|
|
1221 {
|
|
1222 int fine_tune=99;
|
|
1223 priv_vbi_t* priv=(priv_vbi_t*)p;
|
|
1224
|
|
1225 if (!priv && cmd!=TV_VBI_CONTROL_START)
|
|
1226 return TVI_CONTROL_FALSE;
|
|
1227 if (!arg && cmd!=TV_VBI_CONTROL_STOP)
|
|
1228 return TVI_CONTROL_FALSE;
|
|
1229
|
|
1230 switch (cmd) {
|
|
1231 case TV_VBI_CONTROL_RESET:
|
|
1232 {
|
|
1233 tv_param_t* tv_param=arg;
|
|
1234 pthread_mutex_lock(&(priv->buffer_mutex));
|
|
1235 priv->pagenumdec=0;
|
|
1236 clear_cache(priv);
|
|
1237 priv->pagenum=steppage(0,tv_param->tpage&0x7ff,1);
|
|
1238 priv->tformat=tv_param->tformat;
|
|
1239 priv->subpagenum=0;
|
|
1240 pll_reset(priv,fine_tune);
|
|
1241 pthread_mutex_unlock(&(priv->buffer_mutex));
|
|
1242 return TVI_CONTROL_TRUE;
|
|
1243 }
|
|
1244 case TV_VBI_CONTROL_START:
|
|
1245 {
|
|
1246 int i;
|
|
1247 tt_stream_props* ptsp=*(tt_stream_props**)arg;
|
|
1248
|
|
1249 if(!ptsp)
|
|
1250 return TVI_CONTROL_FALSE;
|
|
1251
|
|
1252 priv=calloc(1,sizeof(priv_vbi_t));
|
|
1253
|
|
1254 priv->ptsp=malloc(sizeof(tt_stream_props));
|
|
1255 memcpy(priv->ptsp,ptsp,sizeof(tt_stream_props));
|
|
1256 *(priv_vbi_t**)arg=priv;
|
|
1257
|
|
1258 priv->subpagenum=0;
|
|
1259 pthread_mutex_init(&priv->buffer_mutex, NULL);
|
|
1260 priv->pagenumdec=0;
|
|
1261 for(i=0;i<VBI_ROWS*VBI_COLUMNS;i++)
|
|
1262 priv->display_page[i]=tt_space;
|
|
1263
|
|
1264 priv->mag=calloc(8,sizeof(mag_t));
|
|
1265 init_cache(priv);
|
|
1266 init_vbi_consts(priv);
|
|
1267 pll_reset(priv,fine_tune);
|
|
1268 return TVI_CONTROL_TRUE;
|
|
1269 }
|
|
1270 case TV_VBI_CONTROL_STOP:
|
|
1271 {
|
|
1272 if(priv->mag)
|
|
1273 free(priv->mag);
|
|
1274 if(priv->ptsp)
|
|
1275 free(priv->ptsp);
|
|
1276 destroy_cache(priv);
|
|
1277 free(priv);
|
|
1278 return TVI_CONTROL_TRUE;
|
|
1279 }
|
|
1280 case TV_VBI_CONTROL_SET_MODE:
|
|
1281 priv->on=(*(int*)arg%2);
|
|
1282 return TVI_CONTROL_TRUE;
|
|
1283 case TV_VBI_CONTROL_GET_MODE:
|
|
1284 *(int*)arg=priv->on;
|
|
1285 return TVI_CONTROL_TRUE;
|
|
1286 case TV_VBI_CONTROL_SET_FORMAT:
|
|
1287 return teletext_set_format(priv, *(int *) arg);
|
|
1288 case TV_VBI_CONTROL_GET_FORMAT:
|
|
1289 pthread_mutex_lock(&(priv->buffer_mutex));
|
|
1290 *(int*)arg=priv->tformat;
|
|
1291 pthread_mutex_unlock(&(priv->buffer_mutex));
|
|
1292 return TVI_CONTROL_TRUE;
|
|
1293 case TV_VBI_CONTROL_GET_HALF_PAGE:
|
|
1294 if(!priv->on)
|
|
1295 return TVI_CONTROL_FALSE;
|
|
1296 *(int *)arg=priv->zoom;
|
|
1297 return TVI_CONTROL_TRUE;
|
|
1298 case TV_VBI_CONTROL_SET_HALF_PAGE:
|
|
1299 {
|
|
1300 int val=*(int*)arg;
|
|
1301 val%=3;
|
|
1302 if(val<0)
|
|
1303 val+=3;
|
|
1304 pthread_mutex_lock(&(priv->buffer_mutex));
|
|
1305 priv->zoom=val;
|
|
1306 pthread_mutex_unlock(&(priv->buffer_mutex));
|
|
1307 return TVI_CONTROL_TRUE;
|
|
1308 }
|
|
1309 case TV_VBI_CONTROL_SET_PAGE:
|
|
1310 {
|
|
1311 int val=*(int *) arg;
|
|
1312 if(val<100 || val>0x899)
|
|
1313 return TVI_CONTROL_FALSE;
|
|
1314 pthread_mutex_lock(&(priv->buffer_mutex));
|
|
1315 priv->pagenum=val&0x7ff;
|
|
1316 priv->subpagenum=get_subpagenum_from_cache(priv,priv->pagenum);
|
|
1317 priv->pagenumdec=0;
|
|
1318 pthread_mutex_unlock(&(priv->buffer_mutex));
|
|
1319 return TVI_CONTROL_TRUE;
|
|
1320 }
|
|
1321 case TV_VBI_CONTROL_STEP_PAGE:
|
|
1322 {
|
|
1323 int direction=*(int *) arg;
|
|
1324 pthread_mutex_lock(&(priv->buffer_mutex));
|
|
1325 priv->pagenum=steppage(priv->pagenum, direction,1);
|
|
1326 priv->subpagenum=get_subpagenum_from_cache(priv,priv->pagenum);
|
|
1327 priv->pagenumdec=0;
|
|
1328 pthread_mutex_unlock(&(priv->buffer_mutex));
|
|
1329 return TVI_CONTROL_TRUE;
|
|
1330 }
|
|
1331 case TV_VBI_CONTROL_GET_PAGE:
|
|
1332 *(int*)arg=((priv->pagenum+0x700)&0x7ff)+0x100;
|
|
1333 return TVI_CONTROL_TRUE;
|
|
1334 case TV_VBI_CONTROL_SET_SUBPAGE:
|
|
1335 pthread_mutex_lock(&(priv->buffer_mutex));
|
|
1336 priv->pagenumdec=0;
|
|
1337 priv->subpagenum=*(int*)arg;
|
|
1338 if(priv->subpagenum<0)
|
|
1339 priv->subpagenum=0;
|
|
1340 if(priv->subpagenum>=VBI_MAX_SUBPAGES)
|
|
1341 priv->subpagenum=VBI_MAX_SUBPAGES-1;
|
|
1342 pthread_mutex_unlock(&(priv->buffer_mutex));
|
|
1343 return TVI_CONTROL_TRUE;
|
|
1344 case TV_VBI_CONTROL_GET_SUBPAGE:
|
|
1345 *(int*)arg=priv->subpagenum;
|
|
1346 return TVI_CONTROL_TRUE;
|
|
1347 case TV_VBI_CONTROL_ADD_DEC:
|
|
1348 vbi_add_dec(priv, *(char **) arg);
|
|
1349 return TVI_CONTROL_TRUE;
|
|
1350 case TV_VBI_CONTROL_DECODE_PAGE:
|
|
1351 vbi_decode(priv,*(unsigned char**)arg);
|
|
1352 return TVI_CONTROL_TRUE;
|
|
1353 case TV_VBI_CONTROL_GET_VBIPAGE:
|
|
1354 if(!priv->on)
|
|
1355 return TVI_CONTROL_FALSE;
|
|
1356 prepare_visible_page(priv);
|
|
1357 *(void **)arg=priv->display_page;
|
|
1358 return TVI_CONTROL_TRUE;
|
|
1359 }
|
|
1360 return TVI_CONTROL_UNKNOWN;
|
|
1361 }
|