0adde1ec49952b65a4727b46c2ad87dff84f2792
[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 // Try to map teletext characters into ISO-8859-1 charset
7 // Use similar looking or similar meaning characters when possible.
8
9 // G0 and G2 national option table
10 // see table 33 in ETSI EN 300 706
11 // use it with (triplet 1 bits 14-11)*(ctrl bits C12-14)
12
13 unsigned char LatinNationalOptionSubsetsLookup[16*8] =
14 {
15         1, 4, 11, 5, 3, 8, 0, 1,
16         7, 4, 11, 5, 3, 1, 0, 1,
17         1, 4, 11, 5, 3, 8, 12, 1,
18         1, 1, 1, 1, 1, 10, 1, 9,
19         1, 4, 2, 6, 1, 1, 0, 1,
20         1, 1, 1, 1, 1, 1, 1, 1, // reserved
21         1, 1, 1, 1, 1, 1, 12, 1,
22         1, 1, 1, 1, 1, 1, 1, 1, // reserved
23         1, 1, 1, 1, 3, 1, 1, 1,
24         1, 1, 1, 1, 1, 1, 1, 1, // reserved
25         1, 1, 1, 1, 1, 1, 1, 1,
26         1, 1, 1, 1, 1, 1, 1, 1, // reserved
27         1, 1, 1, 1, 1, 1, 1, 1, // reserved
28         1, 1, 1, 1, 1, 1, 1, 1, // reserved
29         1, 1, 1, 1, 1, 1, 1, 1, // reserved
30         1, 1, 1, 1, 1, 1, 1, 1  // reserved
31 };
32
33 unsigned char LatinNationalReplaceMap[128] =
34 {
35         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37         0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
38         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39         3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 6, 7, 8,
41         9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 0
43 };
44
45 // latin national option subsets
46 // see table 36 in ETSI EN 300 706
47
48 unsigned char LatinNationalOptionSubsets[13*14] = {
49         0, '#', 'u', 'c', 't', 'z', 'ý', 'í', 'r', 'é', 'á', 'e', 'ú', 's', // Slovak/Czech
50         0, '£', '$', '@', '-', '½', '-', '|', '#', '-', '¼', '#', '¾', '÷', // English
51         0, '#', 'õ', 'S', 'Ä', 'Ö', 'Z', 'Ü', 'Õ', 's', 'ä', 'ö', 'z', 'ü', // Estonian
52         0, 'é', 'ï', 'à', 'ë', 'ê', 'ù', 'î', '#', 'è', 'â', 'ô', 'û', 'ç', // French
53         0, '#', '$', '§', 'Ä', 'Ö', 'Ü', '^', '_', 'º', 'ä', 'ö', 'ü', 'ß', // German
54         0, '£', '$', 'é', 'º', 'ç', '-', '|', '#', 'ù', 'à', 'ò', 'è', 'ì', // Italian
55         0, '#', '$', 'S', 'e', 'e', 'Z', 'c', 'u', 's', 'a', 'u', 'z', 'i', // Lithuanian/Lettish
56         0, '#', 'n', 'a', 'Z', 'S', 'L', 'c', 'ó', 'e', 'z', 's', 'l', 'z', // Polish
57         0, 'ç', '$', 'i', 'á', 'é', 'í', 'ó', 'ú', '¿', 'ü', 'ñ', 'è', 'à', // Spanish/Portuguese
58         0, '#', '¤', 'T', 'Â', 'S', 'A', 'Î', 'i', 't', 'â', 's', 'a', 'î', // Rumanian
59         0, '#', 'Ë', 'C', 'C', 'Z', 'D', 'S', 'ë', 'c', 'c', 'z', 'd', 's', // Slovenian/Serbian/Croation
60         0, '#', '¤', 'É', 'Ä', 'Ö', 'Å', 'Ü', '_', 'é', 'ä', 'ö', 'å', 'ü', // Finnish/Hungarian/Swedish
61         0, 'T', 'g', 'I', 'S', 'Ö', 'Ç', 'Ü', 'G', 'i', 's', 'ö', 'ç', 'ü'  // Turkish
62 };
63
64 unsigned char MapTeletextG0Latin1Char(int Gtriplet, int NatOpts, unsigned char inchar)
65 {
66         int num = LatinNationalOptionSubsetsLookup[(Gtriplet&0xf)*(NatOpts&0x7)];
67         unsigned char c = inchar&0x7f;
68         unsigned char cc = LatinNationalReplaceMap[c];
69         if(cc)
70                 return LatinNationalOptionSubsets[num*cc];
71         else
72                 return c;
73 }
74
75 // This is a very simple en300 706 telext decoder.
76 // It can only decode a single page at a time, thus it's only used
77 // for subtitles.
78  
79 DEFINE_REF(eDVBTeletextParser);
80
81         /* we asumme error free transmission! */
82 static inline unsigned char decode_odd_parity(unsigned char *b)
83 {
84         int i;
85         unsigned char res = 0;
86         for (i=0; i<7; ++i)
87                 if (*b & (0x80 >> i))
88                         res |= 1<<i;
89         return res;
90 }
91
92 static inline unsigned char decode_hamming_84(unsigned char *b)
93 {
94         unsigned char res = 0;
95         res |= (*b << 3) & 8;
96         res |= (*b     ) & 4;
97         res |= (*b >> 3) & 2;
98         res |= (*b >> 6) & 1;
99         return res;
100 }
101
102 static inline unsigned long decode_hamming_2418(unsigned char *b)
103 {
104         unsigned long h24 = b[0] | (b[1] << 8) | (b[2] << 16);
105         
106         return
107                 ((h24 & 0x000004) >> 2) |
108                 ((h24 & 0x000070) >> 3) |
109                 ((h24 & 0x007f00) >> 4) |
110                 ((h24 & 0x7f0000) >> 5);
111 }
112
113 static int extractPTS(pts_t &pts, unsigned char *pkt)
114 {
115         pkt += 7;
116         int flags = *pkt++;
117         
118         pkt++; // header length
119         
120         if (flags & 0x80) /* PTS present? */
121         {
122                         /* damn gcc bug */
123                 pts  = ((unsigned long long)(((pkt[0] >> 1) & 7))) << 30;
124                 pts |=   pkt[1] << 22;
125                 pts |=  (pkt[2]>>1) << 15;
126                 pts |=   pkt[3] << 7;
127                 pts |=  (pkt[5]>>1);
128                 
129                 return 0;
130         } else
131                 return -1;
132 }
133
134 eDVBTeletextParser::eDVBTeletextParser(iDVBDemux *demux)
135 {
136         setStreamID(0xBD); /* as per en 300 472 */
137         
138         setPageAndMagazine(0,0);
139         
140         if (demux->createPESReader(eApp, m_pes_reader))
141                 eDebug("failed to create teletext subtitle PES reader!");
142         else
143                 m_pes_reader->connectRead(slot(*this, &eDVBTeletextParser::processData), m_read_connection);
144 }
145
146 eDVBTeletextParser::~eDVBTeletextParser()
147 {
148 }
149
150 void eDVBTeletextParser::processPESPacket(__u8 *pkt, int len)
151 {
152         unsigned char *p = pkt;
153         
154         pts_t pts;
155         int have_pts = extractPTS(pts, pkt);
156         
157         p += 4; len -= 4; /* start code, already be verified by pes parser */
158         p += 2; len -= 2; /* length, better use the argument */ 
159         
160         p += 3; len -= 3; /* pes header */
161         
162         p += 0x24; len -= 0x24; /* skip header */
163         
164 //      eDebug("data identifier: %02x", *p);
165         
166         p++; len--;
167         
168         while (len > 2)
169         {
170                 unsigned char data_unit_id = *p++;
171                 unsigned char data_unit_length = *p++;
172                 len -= 2;
173                 
174                 if (len < data_unit_length)
175                 {
176                         eDebug("data_unit_length > len");
177                         break;
178                 }
179                 
180                 if (data_unit_length != 44)
181                 {
182                         /* eDebug("illegal data unit length %d", data_unit_length); */
183                         break;
184                 }
185                 
186                 if (data_unit_id != 0x03)
187                 {
188                         /* eDebug("non subtitle data unit id %d", data_unit_id); */
189                         break;
190                 }
191                 
192                 unsigned char line_offset = *p++; len--;
193                 unsigned char framing_code = *p++; len--;
194
195                 int magazine_and_packet_address = decode_hamming_84(p++); len--;
196                 magazine_and_packet_address |= decode_hamming_84(p++)<<4; len--;
197                 
198                 unsigned char *data = p; p += 40; len -= 40;
199                 
200                 if (framing_code != 0xe4) /* no teletxt data */
201                         continue;
202                 
203                 m_M = magazine_and_packet_address & 7;
204                 m_Y = magazine_and_packet_address >> 3;
205
206 //                      eDebug("line %d, framing code: %02x, M=%02x, Y=%02x", line_offset, framing_code, m_M, m_Y);
207                 
208                 if (m_Y == 0) /* page header */
209                 {
210                         m_C = 0;
211                         
212                         m_S1 = decode_hamming_84(data + 2); /* S1 */
213                         int S2C4 = decode_hamming_84(data + 3);
214                         
215                         m_S2 = S2C4 & 7;
216                         m_C |= (S2C4 & 8) ? (1<<4) : 0;
217                         
218                         m_S3 = decode_hamming_84(data + 4);
219                         
220                         int S4C5C6 = decode_hamming_84(data + 5);
221                         
222                         m_S4 = S4C5C6 & 3;
223                         m_C |= (S4C5C6 & 0xC) << 3;
224                         
225                         m_C |= decode_hamming_84(data + 6) << 7;
226                         m_C |= decode_hamming_84(data + 7) << 11;
227                         
228                         int serial_mode = m_C & (1<<11);
229                         
230                                 /* page on the same magazine? end current page. */
231                         if ((serial_mode || m_M == m_page_M) && m_page_open)
232                         {
233                                 handlePageEnd(have_pts, pts);
234                                 m_page_open = 0;
235                         }
236                         
237                         m_X = decode_hamming_84(data+1) * 0x10 + decode_hamming_84(data);
238                         
239                         if ((m_C & (1<<6)) && (m_X != 0xFF)) /* scan for pages with subtitle bit set */
240                         {
241                                 eDVBServicePMTHandler::subtitleStream s;
242                                 s.pid = m_pid;
243                                 s.subtitling_type = 0x01; // ebu teletext subtitle
244                                 s.teletext_page_number = m_X & 0xFF;
245                                 s.teletext_magazine_number = m_M & 7;
246                                 //m_found_subtitle_pages.insert(s);
247                         }
248
249                                 /* correct page on correct magazine? open page. */
250                         if (m_M == m_page_M && m_X == m_page_X)
251                         {
252                                 handlePageStart();
253                                 m_page_open = 1;
254                                 handleLine(data + 8, 32);
255                         }
256                 } else
257                 {
258                         /* data for the selected page ? */
259                         if (m_M == m_page_M && m_page_open)
260                                 handleLine(data, 40);
261                 }
262         }
263 }
264
265 int eDVBTeletextParser::start(int pid)
266 {
267         m_page_open = 0;
268
269         if (m_pes_reader)
270         {
271                 m_pid = pid;
272                 return m_pes_reader->start(pid);
273         }
274         else
275                 return -1;
276 }
277
278 void eDVBTeletextParser::handlePageStart()
279 {
280 //      if (m_C & (1<<4)) /* erase flag set */
281
282                 /* we are always erasing the page, 
283                    even when the erase flag is not set. */
284         m_subtitle_page.clear();
285 }
286
287 void eDVBTeletextParser::handleLine(unsigned char *data, int len)
288 {
289 /* // hexdump
290         for (int i=0; i<len; ++i)
291                 eDebugNoNewLine("%02x ", decode_odd_parity(data + i));
292         eDebug(""); */
293         if (!m_Y) /* first line is page header, we don't need that. */
294         {
295                 m_double_height = -1;
296                 return;
297         }
298                 
299         if (m_double_height == m_Y)
300         {
301                 m_double_height = -1;
302                 return;
303         }
304
305         int last_was_white = 1, color = 7; /* start with whitespace. start with color=white. (that's unrelated.) */
306         
307         std::string text;
308         
309 //      eDebug("handle subtitle line: %d len", len);
310         for (int i=0; i<len; ++i)
311         {
312                 unsigned char b = decode_odd_parity(data + i);
313         
314                 if (b < 0x10) /* spacing attribute */
315                 {
316                         if (b < 8) /* colors */
317                         {
318                                 if (b != color) /* new color is split into a new string */
319                                 {
320                                         addSubtitleString(color, text);
321                                         text = "";
322                                         color = b;
323                                 }
324                         } else if (b == 0xd)
325                         {
326                                 m_double_height = m_Y + 1;
327                         } else if (b != 0xa && b != 0xb) /* box */
328                                 eDebug("[ignore %x]", b);
329                                 /* ignore other attributes */
330                 } else
331                 {
332                         //eDebugNoNewLine("%c", b);
333                                 /* no more than one whitespace, only printable chars */
334                         if (((!last_was_white) || (b != ' ')) && (b >= 0x20))
335                         {
336                                 text += b;
337                                 last_was_white = b == ' ';
338                         }
339                 }
340         }
341         //eDebug("");
342         addSubtitleString(color, text);
343 }
344
345 void eDVBTeletextParser::handlePageEnd(int have_pts, const pts_t &pts)
346 {
347 //      eDebug("handle page end");
348         addSubtitleString(-2, ""); /* end last line */ 
349         
350         m_subtitle_page.m_have_pts = have_pts;
351         m_subtitle_page.m_pts = pts;
352         m_subtitle_page.m_timeout = 90000 * 20; /* 20s */
353         if (m_page_X != 0)
354                 sendSubtitlePage();  /* send assembled subtitle page to display */
355 }
356
357 void eDVBTeletextParser::setPageAndMagazine(int page, int magazine)
358 {
359         if (page > 0)
360                 eDebug("enable teletext subtitle page %d", page);
361         else
362                 eDebug("disable teletext subtitles");
363         m_page_M = magazine&7; /* magazine to look for */
364         m_page_X = page&0xFF;  /* page number */
365 }
366
367 void eDVBTeletextParser::connectNewPage(const Slot1<void, const eDVBTeletextSubtitlePage&> &slot, ePtr<eConnection> &connection)
368 {
369         connection = new eConnection(this, m_new_subtitle_page.connect(slot));
370 }
371
372 void eDVBTeletextParser::addSubtitleString(int color, std::string string)
373 {
374 //      eDebug("add subtitle string: %s, col %d", string.c_str(), color);
375
376         int force_cell = 0;
377
378         if (string.substr(0, 2) == "- ")
379         {
380                 string = string.substr(2);
381                 force_cell = 1;
382         }
383
384         int len = string.length();
385         int idx = 0;
386
387         while (idx < len)
388         {
389                 if (string[idx] >= 0x20)
390                         string[idx] = MapTeletextG0Latin1Char(0, (m_C >> 11), string[idx]);
391                 ++idx;
392         }
393
394 //      eDebug("color %d, m_subtitle_color %d", color, m_subtitle_color);
395         gRGB rgbcol((color & 1) ? 255 : 128, (color & 2) ? 255 : 128, (color & 4) ? 255 : 128);
396         if ((color != m_subtitle_color || force_cell) && !m_subtitle_text.empty() && ((color == -2) || !string.empty()))
397         {
398 //              eDebug("add text |%s|: %d != %d || %d", m_subtitle_text.c_str(), color, m_subtitle_color, force_cell);
399                 m_subtitle_page.m_elements.push_back(eDVBTeletextSubtitlePageElement(rgbcol, m_subtitle_text));
400                 m_subtitle_text = "";
401         } else if (!m_subtitle_text.empty() && m_subtitle_text[m_subtitle_text.size()-1] != ' ')
402                 m_subtitle_text += " ";
403         
404         if (!string.empty())
405         {
406 //              eDebug("set %d as new color", color);
407                 m_subtitle_color = color;
408                 m_subtitle_text += string;
409         }
410 }
411
412 void eDVBTeletextParser::sendSubtitlePage()
413 {
414 //      eDebug("subtitle page:");
415         //for (unsigned int i = 0; i < m_subtitle_page.m_elements.size(); ++i)
416         //      eDebug("%s", m_subtitle_page.m_elements[i].m_text.c_str());
417         m_new_subtitle_page(m_subtitle_page);
418 }