remove debug
[enigma2.git] / lib / dvb / teletext.cpp
1 #include <lib/base/eerror.h>
2 #include <lib/dvb/teletext.h>
3 #include <lib/dvb/idemux.h>
4 #include <lib/gdi/gpixmap.h>
5
6 /*
7  This is a very simple en300 706 telext decoder.
8  
9  It can only decode a single page at a time, thus it's only used
10  for subtitles.
11  
12  */
13
14 DEFINE_REF(eDVBTeletextParser);
15
16         /* we asumme error free transmission! */
17 static inline unsigned char decode_odd_parity(unsigned char *b)
18 {
19         int i;
20         unsigned char res = 0;
21         for (i=0; i<7; ++i)
22                 if (*b & (0x80 >> i))
23                         res |= 1<<i;
24         return res;
25 }
26
27 static inline unsigned char decode_hamming_84(unsigned char *b)
28 {
29         unsigned char res = 0;
30         res |= (*b << 3) & 8;
31         res |= (*b     ) & 4;
32         res |= (*b >> 3) & 2;
33         res |= (*b >> 6) & 1;
34         return res;
35 }
36
37 static inline unsigned long decode_hamming_2418(unsigned char *b)
38 {
39         unsigned long h24 = b[0] | (b[1] << 8) | (b[2] << 16);
40         
41         return
42                 ((h24 & 0x000004) >> 2) |
43                 ((h24 & 0x000070) >> 3) |
44                 ((h24 & 0x007f00) >> 4) |
45                 ((h24 & 0x7f0000) >> 5);
46 }
47
48 static int extractPTS(pts_t &pts, unsigned char *pkt)
49 {
50         pkt += 7;
51         int flags = *pkt++;
52         
53         pkt++; // header length
54         
55         if (flags & 0x80) /* PTS present? */
56         {
57                         /* damn gcc bug */
58                 pts  = ((unsigned long long)(((pkt[0] >> 1) & 7))) << 30;
59                 pts |=   pkt[1] << 22;
60                 pts |=  (pkt[2]>>1) << 15;
61                 pts |=   pkt[3] << 7;
62                 pts |=  (pkt[5]>>1);
63                 
64                 return 0;
65         } else
66                 return -1;
67 }
68
69 eDVBTeletextParser::eDVBTeletextParser(iDVBDemux *demux)
70 {
71         setStreamID(0xBD); /* as per en 300 472 */
72         
73         setPage(-1);
74         
75         if (demux->createPESReader(eApp, m_pes_reader))
76                 eDebug("failed to create PES reader!");
77         else
78                 m_pes_reader->connectRead(slot(*this, &eDVBTeletextParser::processData), m_read_connection);
79 }
80
81 eDVBTeletextParser::~eDVBTeletextParser()
82 {
83 }
84
85 void eDVBTeletextParser::processPESPacket(__u8 *pkt, int len)
86 {
87         unsigned char *p = pkt;
88         
89         pts_t pts;
90         int have_pts = extractPTS(pts, pkt);
91         
92         p += 4; len -= 4; /* start code, already be verified by pes parser */
93         p += 2; len -= 2; /* length, better use the argument */ 
94         
95         p += 3; len -= 3; /* pes header */
96         
97         p += 0x24; len -= 0x24; /* skip header */
98         
99 //      eDebug("data identifier: %02x", *p);
100         
101         p++; len--;
102         
103         while (len > 2)
104         {
105                 unsigned char data_unit_id = *p++;
106                 unsigned char data_unit_length = *p++;
107                 len -= 2;
108                 
109                 if (len < data_unit_length)
110                 {
111                         eDebug("data_unit_length > len");
112                         break;
113                 }
114                 
115                 if (data_unit_length != 44)
116                 {
117                         /* eDebug("illegal data unit length %d", data_unit_length); */
118                         break;
119                 }
120                 
121                 unsigned char line_offset = *p++; len--;
122                 unsigned char framing_code = *p++; len--;
123
124                 int magazine_and_packet_address = decode_hamming_84(p++); len--;
125                 magazine_and_packet_address |= decode_hamming_84(p++)<<4; len--;
126                 
127                 unsigned char *data = p; p += 40; len -= 40;
128                 
129                 if (framing_code != 0xe4) /* no teletxt data */
130                         continue;
131                 
132                 m_M = magazine_and_packet_address & 7;
133                 m_Y = magazine_and_packet_address >> 3;
134
135 //                      eDebug("line %d, framing code: %02x, M=%02x, Y=%02x", line_offset, framing_code, m_M, m_Y);
136                 
137                 if (m_Y == 0) /* page header */
138                 {
139                         m_C = 0;
140                         
141                         m_S1 = decode_hamming_84(data + 2); /* S1 */
142                         int S2C4 = decode_hamming_84(data + 3);
143                         
144                         m_S2 = S2C4 & 7;
145                         m_C |= (S2C4 & 8) ? (1<<4) : 0;
146                         
147                         m_S3 = decode_hamming_84(data + 4);
148                         
149                         int S4C5C6 = decode_hamming_84(data + 5);
150                         
151                         m_S4 = S4C5C6 & 3;
152                         m_C |= (S4C5C6 & 0xC) << 3;
153                         
154                         m_C |= decode_hamming_84(data + 6) << 7;
155                         m_C |= decode_hamming_84(data + 7) << 11;
156                         
157                         int serial_mode = m_C & (1<<11);
158                         
159                                 /* page on the same magazine? end current page. */
160                         if ((serial_mode || (m_M == m_page_M)) && (m_page_open))
161                         {
162                                 handlePageEnd(have_pts, pts);
163                                 m_page_open = 0;
164                         }
165                         
166                         m_X = decode_hamming_84(data+1) * 0x10 + decode_hamming_84(data);
167                         
168                         if ((m_C & (1<<6)) && (m_X != 0xFF)) /* scan for pages with subtitle bit set */
169                                 m_found_subtitle_pages.insert((m_M << 8) | m_X);
170                         
171                                 /* correct page on correct magazine? open page. */
172                         if ((m_M == m_page_M) && (m_X == m_page_X))
173                         {
174                                 handlePageStart();
175                                 m_page_open = 1;
176                                 handleLine(data + 8, 32);
177                         }
178                 } else
179                 {
180                         /* data for the selected page? */
181                         if ((m_M == m_page_M) && m_page_open)
182                                 handleLine(data, 40);
183                 }
184         }
185 }
186
187 int eDVBTeletextParser::start(int pid)
188 {
189         m_page_open = 0;
190
191         if (m_pes_reader)
192                 return m_pes_reader->start(pid);
193         else
194                 return -1;
195 }
196
197 void eDVBTeletextParser::handlePageStart()
198 {
199 //      if (m_C & (1<<4)) /* erase flag set */
200
201                 /* we are always erasing the page, 
202                    even when the erase flag is not set. */
203         m_subtitle_page.clear();
204 }
205
206 void eDVBTeletextParser::handleLine(unsigned char *data, int len)
207 {
208 /* // hexdump
209         for (int i=0; i<len; ++i)
210                 eDebugNoNewLine("%02x ", decode_odd_parity(data + i));
211         eDebug(""); */
212         if (!m_Y) /* first line is page header, we don't need that. */
213         {
214                 m_double_height = -1;
215                 return;
216         }
217                 
218         if (m_double_height == m_Y)
219         {
220                 m_double_height = -1;
221                 return;
222         }
223
224         int last_was_white = 1, color = 7; /* start with whitespace. start with color=white. (that's unrelated.) */
225         
226         std::string text;
227         
228 //      eDebug("handle subtitle line: %d len", len);
229         for (int i=0; i<len; ++i)
230         {
231                 unsigned char b = decode_odd_parity(data + i);
232         
233                 if (b < 0x10) /* spacing attribute */
234                 {
235                         if (b < 8) /* colors */
236                         {
237                                 if (b != color) /* new color is split into a new string */
238                                 {
239                                         addSubtitleString(color, text);
240                                         text = "";
241                                         color = b;
242                                 }
243                         } else if (b == 0xd)
244                         {
245                                 m_double_height = m_Y + 1;
246                         } else if (b != 0xa && b != 0xb) /* box */
247                                 eDebug("[ignore %x]", b);
248                                 /* ignore other attributes */
249                 } else
250                 {
251                         //eDebugNoNewLine("%c", b);
252                                 /* no more than one whitespace, only printable chars */
253                         if (((!last_was_white) || (b != ' ')) && (b >= 0x20))
254                         {
255                                 text += b;
256                                 last_was_white = b == ' ';
257                         }
258                 }
259         }
260         //eDebug("");
261         addSubtitleString(color, text);
262 }
263
264 void eDVBTeletextParser::handlePageEnd(int have_pts, const pts_t &pts)
265 {
266 //      eDebug("handle page end");
267         addSubtitleString(-2, ""); /* end last line */ 
268         
269         m_subtitle_page.m_have_pts = have_pts;
270         m_subtitle_page.m_pts = pts;
271         m_subtitle_page.m_timeout = 90000 * 20; /* 20s */
272         sendSubtitlePage();  /* send assembled subtitle page to display */
273 }
274
275 void eDVBTeletextParser::setPage(int page)
276 {
277         m_page_M = (page >> 8) & 7; /* magazine to look for */
278         m_page_X = page & 0xFF;     /* page number */
279 }
280
281 void eDVBTeletextParser::connectNewPage(const Slot1<void, const eDVBTeletextSubtitlePage&> &slot, ePtr<eConnection> &connection)
282 {
283         connection = new eConnection(this, m_new_subtitle_page.connect(slot));
284 }
285
286 void eDVBTeletextParser::addSubtitleString(int color, std::string string)
287 {
288 //      eDebug("add subtitle string: %s, col %d", string.c_str(), color);
289
290         int force_cell = 0;
291         
292         if (string.substr(0, 2) == "- ")
293         {
294                 string = string.substr(2);
295                 force_cell = 1;
296         }
297
298 //      eDebug("color %d, m_subtitle_color %d", color, m_subtitle_color);
299         gRGB rgbcol((color & 1) ? 255 : 128, (color & 2) ? 255 : 128, (color & 4) ? 255 : 128);
300         if ((color != m_subtitle_color || force_cell) && !m_subtitle_text.empty() && ((color == -2) || !string.empty()))
301         {
302 //              eDebug("add text |%s|: %d != %d || %d", m_subtitle_text.c_str(), color, m_subtitle_color, force_cell);
303                 m_subtitle_page.m_elements.push_back(eDVBTeletextSubtitlePageElement(rgbcol, m_subtitle_text));
304                 m_subtitle_text = "";
305         } else if (!m_subtitle_text.empty() && m_subtitle_text[m_subtitle_text.size()-1] != ' ')
306                 m_subtitle_text += " ";
307         
308         if (!string.empty())
309         {
310 //              eDebug("set %d as new color", color);
311                 m_subtitle_color = color;
312                 m_subtitle_text += string;
313         }
314 }
315
316 void eDVBTeletextParser::sendSubtitlePage()
317 {
318 //      eDebug("subtitle page:");
319         //for (unsigned int i = 0; i < m_subtitle_page.m_elements.size(); ++i)
320         //      eDebug("%s", m_subtitle_page.m_elements[i].m_text.c_str());
321         m_new_subtitle_page(m_subtitle_page);
322 }