]> git.vomp.tv Git - vompclient-marten.git/blob - teletextdecodervbiebu.h
Fix bug in demuxer widescreen signaling
[vompclient-marten.git] / teletextdecodervbiebu.h
1 /*
2     Copyright 2008 Marten Richter
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20 /* Portions from vdr osdteletext plugin "txtrender.c": */
21 /***************************************************************************
22  *                                                                         *
23  *   txtrender.h - Teletext display abstraction and teletext code          *
24  *                 renderer                                                *
25  *                                                                         *
26  *   This program is free software; you can redistribute it and/or modify  *
27  *   it under the terms of the GNU General Public License as published by  *
28  *   the Free Software Foundation; either version 2 of the License, or     *
29  *   (at your option) any later version.                                   *
30  *                                                                         *
31  *   Changelog:                                                            *
32  *     2005-03    initial version (c) Udo Richter                          *
33  *                                                                         *
34  ***************************************************************************/
35
36 #ifndef TXTDECVBIEBU_H
37 #define TXTDECVBIEBU_H
38
39 #include "draintarget.h"
40 #include "log.h"
41
42 /* from osdteletext begin */
43
44 // Teletext character sets
45 enum enumCharsets {
46     CHARSET_LATIN_G0          = 0x0000, // native latin (partially todo)
47     CHARSET_LATIN_G0_CZ_SK    = 0x0100, // Czech/Slovak (todo)
48     CHARSET_LATIN_G0_EN       = 0x0200, // English
49     CHARSET_LATIN_G0_EE       = 0x0300, // Estonian (todo)
50     CHARSET_LATIN_G0_FR       = 0x0400, // French
51     CHARSET_LATIN_G0_DE       = 0x0500, // German
52     CHARSET_LATIN_G0_IT       = 0x0600, // Italian
53     CHARSET_LATIN_G0_LV_LT    = 0x0700, // Lettish/Lithuanian (todo)
54     CHARSET_LATIN_G0_PL       = 0x0800, // Polish (todo)
55     CHARSET_LATIN_G0_PT_ES    = 0x0900, // Portugese/Spanish
56     CHARSET_LATIN_G0_RO       = 0x0A00, // Romanian (todo)
57     CHARSET_LATIN_G0_SR_HR_SL = 0x0B00, // Serbian/Croatian/Slovenian (todo)
58     CHARSET_LATIN_G0_SV_FI    = 0x0C00, // Swedish/Finnish
59     CHARSET_LATIN_G0_TR       = 0x0D00, // Turkish (todo)
60     CHARSET_LATIN_G2          = 0x0E00, // Latin G2 supplementary set (todo)
61     CHARSET_CYRILLIC_G0_SR_HR = 0x0F00, // Serbian/Croatian (todo)
62     CHARSET_CYRILLIC_G0_RU_BG = 0x1000, // Russian/Bulgarian (todo)
63     CHARSET_CYRILLIC_G0_UK    = 0x1100, // Ukrainian (todo)
64     CHARSET_CYRILLIC_G2       = 0x1200, // Cyrillic G2 Supplementary (todo)
65     CHARSET_GREEK_G0          = 0x1300, // Greek G0 (todo)
66     CHARSET_GREEK_G2          = 0x1400, // Greeek G2 (todo)
67     CHARSET_ARABIC_G0         = 0x1500, // Arabic G0 (todo)
68     CHARSET_ARABIC_G2         = 0x1600, // Arabic G2 (todo)
69     CHARSET_HEBREW_G0         = 0x1700, // Hebrew G0 (todo)
70     CHARSET_GRAPHICS_G1       = 0x1800, // G1 graphics set
71     CHARSET_GRAPHICS_G1_SEP   = 0x1900, // G1 graphics set, separated
72     CHARSET_GRAPHICS_G3       = 0x1A00, // G3 graphics set (todo)
73     CHARSET_INVALID           = 0x1F00  // no charset defined
74 };
75
76 // Macro to get the lowest non-0 bit position from a bit mask
77 // Should evaluate to const on a const mask
78 #define LowestSet2Bit(mask) ((mask)&0x0001?0:1)
79 #define LowestSet4Bit(mask) ((mask)&0x0003?LowestSet2Bit(mask):LowestSet2Bit((mask)>>2)+2)
80 #define LowestSet8Bit(mask) ((mask)&0x000f?LowestSet4Bit(mask):LowestSet4Bit((mask)>>4)+4)
81 #define LowestSet16Bit(mask) ((mask)&0x00ff?LowestSet8Bit(mask):LowestSet8Bit((mask)>>8)+8)
82 #define LowestSet32Bit(mask) ((mask)&0xffff?LowestSet16Bit(mask):LowestSet16Bit((mask)>>16)+16)
83
84
85 // Character modifcation double height:
86 enum enumDblHeight {
87     dblh_Normal=0x00000000, // normal height
88     dblh_Top   =0x04000000, // upper half character
89     dblh_Bottom=0x08000000  // lower half character
90 };
91 // Character modifcation double width:
92 enum enumDblWidth {
93     dblw_Normal=0x00000000, // normal width
94     dblw_Left  =0x10000000, // left half character
95     dblw_Right =0x20000000  // right half character
96 };
97
98 // Teletext colors
99 enum enumTeletextColor {
100     // level 1:
101     ttcBlack=0,
102     ttcRed=1,
103     ttcGreen=2,
104     ttcYellow=3,
105     ttcBlue=4,
106     ttcMagenta=5,
107     ttcCyan=6,
108     ttcWhite=7,
109     // level 2.5:
110     ttcTransparent=8,
111     ttcHalfRed=9,
112     ttcHalfGreen=10,
113     ttcHalfYellow=11,
114     ttcHalfBlue=12,
115     ttcHalfMagenta=13,
116     ttcHalfCyan=14,
117     ttcGrey=15,
118     // unnamed, level 2.5:
119     ttcColor16=16, ttcColor17=17, ttcColor18=18, ttcColor19=19,
120     ttcColor20=20, ttcColor21=21, ttcColor22=22, ttcColor23=23,
121     ttcColor24=24, ttcColor25=25, ttcColor26=26, ttcColor27=27,
122     ttcColor28=28, ttcColor29=29, ttcColor30=30, ttcColor31=31,
123     
124     ttcFirst=0, ttcLast=31
125 };
126 inline enumTeletextColor& operator++(enumTeletextColor& c) { return c=enumTeletextColor(int(c)+1); }
127 inline enumTeletextColor operator++(enumTeletextColor& c, int) { enumTeletextColor tmp(c); ++c; return tmp; }
128     
129 class cTeletextChar {
130     // Wrapper class that represents a teletext character,
131     // including colors and effects. Should optimize back
132     // to 4 byte unsigned int on compile.
133     
134 protected:
135     unsigned int c;
136
137     static const unsigned int CHAR             = 0x000000FF;
138     // character code
139     static const unsigned int CHARSET          = 0x00001F00;
140     // character set code, see below
141     static const unsigned int BOXOUT           = 0x00004000;
142     // 'boxed' mode hidden area
143     static const unsigned int DIRTY            = 0x00008000;
144     // 'dirty' bit - internal marker only
145     static const unsigned int FGCOLOR          = 0x001F0000;
146     // 5-bit foreground color code, 3 bit used for now
147     static const unsigned int BGCOLOR          = 0x03E00000;
148     // 5-bit background color code, 3 bit used for now
149     static const unsigned int DBLHEIGHT        = 0x0C000000;
150     // show double height
151     static const unsigned int DBLWIDTH         = 0x30000000;
152     // show double width (todo)
153     static const unsigned int CONCEAL          = 0x40000000;
154     // character concealed
155     static const unsigned int BLINK            = 0x80000000;
156     // blinking character
157
158     cTeletextChar(unsigned int cc) { c=cc; }
159
160 public:
161     cTeletextChar() { c=0; }
162     
163     // inline helper functions:
164     // For each parameter encoded into the 32-bit int, there is
165     // a Get...() to read, a Set...() to write, and a To...() to
166     // return a modified copy
167     
168     inline unsigned char GetChar() 
169         { return c&CHAR; }
170     inline void SetChar(unsigned char chr)
171         { c=(c&~CHAR)|chr; }
172     inline cTeletextChar ToChar(unsigned char chr)
173         { return cTeletextChar((c&~CHAR)|chr); }
174         
175     inline enumCharsets GetCharset() 
176         { return (enumCharsets)(c&CHARSET); }
177     inline void SetCharset(enumCharsets charset) 
178         { c=(c&~CHARSET)|charset; }
179     inline cTeletextChar ToCharset(enumCharsets charset) 
180         { return cTeletextChar((c&~CHARSET)|charset); }
181     
182     inline enumTeletextColor GetFGColor() 
183         { return (enumTeletextColor)((c&FGCOLOR) >> LowestSet32Bit(FGCOLOR)); }
184     inline void SetFGColor(enumTeletextColor fgc) 
185         { c=(c&~FGCOLOR) | (fgc << LowestSet32Bit(FGCOLOR)); }
186     inline cTeletextChar ToFGColor(enumTeletextColor fgc) 
187         { return cTeletextChar((c&~FGCOLOR) | (fgc << LowestSet32Bit(FGCOLOR))); }
188     
189     inline enumTeletextColor GetBGColor() 
190         { return (enumTeletextColor)((c&BGCOLOR) >> LowestSet32Bit(BGCOLOR)); }
191     inline void SetBGColor(enumTeletextColor bgc) 
192         { c=(c&~BGCOLOR) | (bgc << LowestSet32Bit(BGCOLOR)); }
193     inline cTeletextChar ToBGColor(enumTeletextColor bgc) 
194         { return cTeletextChar((c&~BGCOLOR) | (bgc << LowestSet32Bit(BGCOLOR))); }
195     
196     inline bool GetBoxedOut() 
197         { return c&BOXOUT; }
198     inline void SetBoxedOut(bool BoxedOut) 
199         { c=(BoxedOut)?(c|BOXOUT):(c&~BOXOUT); }
200     inline cTeletextChar ToBoxedOut(bool BoxedOut) 
201         { return cTeletextChar((BoxedOut)?(c|BOXOUT):(c&~BOXOUT)); }
202     
203     inline bool GetDirty() 
204         { return c&DIRTY; }
205     inline void SetDirty(bool Dirty) 
206         { c=(Dirty)?(c|DIRTY):(c&~DIRTY); }
207     inline cTeletextChar ToDirty(bool Dirty) 
208         { return cTeletextChar((Dirty)?(c|DIRTY):(c&~DIRTY)); }
209     
210     inline enumDblHeight GetDblHeight() 
211         { return (enumDblHeight)(c&DBLHEIGHT); }
212     inline void SetDblHeight(enumDblHeight dh) 
213         { c=(c&~(DBLHEIGHT)) | dh; }
214     inline cTeletextChar ToDblHeight(enumDblHeight dh) 
215         { return cTeletextChar((c&~(DBLHEIGHT)) | dh); }
216     
217     inline enumDblWidth GetDblWidth() 
218         { return (enumDblWidth)(c&DBLWIDTH); }
219     inline void SetDblWidth(enumDblWidth dw) 
220         { c=(c&~(DBLWIDTH)) | dw; }
221     inline cTeletextChar ToDblWidth(enumDblWidth dw) 
222         { return cTeletextChar((c&~(DBLWIDTH)) | dw); }
223     
224     inline bool GetConceal() 
225         { return c&CONCEAL; }
226     inline void SetConceal(bool Conceal) 
227         { c=(Conceal)?(c|CONCEAL):(c&~CONCEAL); }
228     inline cTeletextChar ToConceal(bool Conceal) 
229         { return cTeletextChar((Conceal)?(c|CONCEAL):(c&~CONCEAL)); }
230     
231     inline bool GetBlink() 
232         { return c&BLINK; }
233     inline void SetBlink(bool Blink) 
234         { c=(Blink)?(c|BLINK):(c&~BLINK); }
235     inline cTeletextChar ToBlink(bool Blink) 
236         { return cTeletextChar((Blink)?(c|BLINK):(c&~BLINK)); }
237     inline unsigned int getGlyphIndex() {
238         return c & (CHAR | CHARSET | DBLHEIGHT | DBLWIDTH);
239     };
240
241     inline void setInternal(unsigned int cc) {c=cc;};
242     inline unsigned int getInternal() {return c;};
243         
244     bool operator==(cTeletextChar &chr) { return c==chr.c; }
245     bool operator!=(cTeletextChar &chr) { return c!=chr.c; }
246 };
247 /* from osdteletext end*/
248
249 class VTeletextView;
250
251 /* Decoder of teletext matrial present in Data stream for VBI reinsertion more or less according to EBU specs*/ 
252 class TeletextDecoderVBIEBU: public DrainTarget {
253 public:
254     TeletextDecoderVBIEBU();
255     virtual ~TeletextDecoderVBIEBU();
256
257     virtual long long SetStartOffset(long long curreftime, bool *rsync);
258     virtual void ResetTimeOffsets();
259
260     void ResetDecoder();
261     void setKeyinDigits(char digits[3],bool inKeying);
262     void setPage(unsigned int newpage);
263     int getPage() {return selectedpage;};
264     void setRecordigMode(bool isrecord) {isrecording=isrecord;};
265     int *getSubtitlePages() {return record_pages;};
266
267
268     virtual void PrepareMediaSample(const MediaPacketList& mplist, UINT samplepos);
269     virtual UINT DeliverMediaSample(UCHAR* buffer, UINT *samplepos);
270
271     void registerTeletextView(VTeletextView* view) {txtview=view;};
272     void unRegisterTeletextView(VTeletextView* view) {if (txtview==view) txtview=NULL;};
273     VTeletextView* getTeletxtView() {return txtview;};
274
275     cTeletextChar getChar(int x, int y) {
276         // Read character content from page
277         if (x<0 || x>=40 || y<0 || y>=25) {
278             Log::getInstance()->log("TeletextDecoderVBIEBU", Log::DEBUG, "Warning: out of bounds read access to teletext page");
279             return cTeletextChar();
280         }
281         return Page[x][y].ToDirty(false);
282     }
283     bool isDirty(int x, int y) {
284         if (x<0 || x>=40 || y<0 || y>=25) {
285             Log::getInstance()->log("TeletextDecoderVBIEBU", Log::DEBUG, "Warning: out of bounds dirty access to teletext page");
286             return false;
287         }
288         return Page[x][y].GetDirty();
289     }
290     void setChar(int x, int y, cTeletextChar c) {
291         // Set character at given location
292         
293         if (x<0 || x>=40 || y<0 || y>=25) {
294             Log::getInstance()->log("TeletextDecoderVBIEBU", Log::DEBUG, "Warning: out of bounds write access to teletext page");
295             return;
296         }
297         if (getChar(x,y) != c) {
298             Page[x][y]=c.ToDirty(true);
299             dirty=true; 
300         }           
301     }
302
303
304 protected:
305     void DecodeTeletext(const UCHAR* buffer, unsigned int field);
306     void RenderTeletextCode(bool renderfirstlineonly);
307     void CleanPage();
308
309     int selectedpage;
310     int lang;
311     int flags;
312     UCHAR curpage[25][40]; //Line DataCache
313     cTeletextChar Page[40][25];
314     char keyindigits[3];
315
316
317     // Font Code pages
318     int FirstG0CodePage;  // 7-bit number, lower 3 bits ignored
319     int SecondG0CodePage; // 7-bit number
320  
321     bool ourpage;
322     bool dirty;
323     bool gotcha;
324     bool inkeying;
325     bool isrecording;
326     int record_pages[10];//Only 10 Pages per record;
327     unsigned int firstlineupdate;
328     VTeletextView *txtview;
329 private:
330     MediaPacket mediapacket;
331     
332     
333
334 };
335
336 #endif