fix displaying teletext subtitle characters > 0x80
[vuplus_dvbapp] / 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         setPage(-1);
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                                 m_found_subtitle_pages.insert((m_M << 8) | m_X);
241                         
242                                 /* correct page on correct magazine? open page. */
243                         if ((m_M == m_page_M) && (m_X == m_page_X))
244                         {
245                                 handlePageStart();
246                                 m_page_open = 1;
247                                 handleLine(data + 8, 32);
248                         }
249                 } else
250                 {
251                         /* data for the selected page? */
252                         if ((m_M == m_page_M) && m_page_open)
253                                 handleLine(data, 40);
254                 }
255         }
256 }
257
258 int eDVBTeletextParser::start(int pid)
259 {
260         m_page_open = 0;
261
262         if (m_pes_reader)
263                 return m_pes_reader->start(pid);
264         else
265                 return -1;
266 }
267
268 void eDVBTeletextParser::handlePageStart()
269 {
270 //      if (m_C & (1<<4)) /* erase flag set */
271
272                 /* we are always erasing the page, 
273                    even when the erase flag is not set. */
274         m_subtitle_page.clear();
275 }
276
277 void eDVBTeletextParser::handleLine(unsigned char *data, int len)
278 {
279 /* // hexdump
280         for (int i=0; i<len; ++i)
281                 eDebugNoNewLine("%02x ", decode_odd_parity(data + i));
282         eDebug(""); */
283         if (!m_Y) /* first line is page header, we don't need that. */
284         {
285                 m_double_height = -1;
286                 return;
287         }
288                 
289         if (m_double_height == m_Y)
290         {
291                 m_double_height = -1;
292                 return;
293         }
294
295         int last_was_white = 1, color = 7; /* start with whitespace. start with color=white. (that's unrelated.) */
296         
297         std::string text;
298         
299 //      eDebug("handle subtitle line: %d len", len);
300         for (int i=0; i<len; ++i)
301         {
302                 unsigned char b = decode_odd_parity(data + i);
303         
304                 if (b < 0x10) /* spacing attribute */
305                 {
306                         if (b < 8) /* colors */
307                         {
308                                 if (b != color) /* new color is split into a new string */
309                                 {
310                                         addSubtitleString(color, text);
311                                         text = "";
312                                         color = b;
313                                 }
314                         } else if (b == 0xd)
315                         {
316                                 m_double_height = m_Y + 1;
317                         } else if (b != 0xa && b != 0xb) /* box */
318                                 eDebug("[ignore %x]", b);
319                                 /* ignore other attributes */
320                 } else
321                 {
322                         //eDebugNoNewLine("%c", b);
323                                 /* no more than one whitespace, only printable chars */
324                         if (((!last_was_white) || (b != ' ')) && (b >= 0x20))
325                         {
326                                 text += b;
327                                 last_was_white = b == ' ';
328                         }
329                 }
330         }
331         //eDebug("");
332         addSubtitleString(color, text);
333 }
334
335 void eDVBTeletextParser::handlePageEnd(int have_pts, const pts_t &pts)
336 {
337 //      eDebug("handle page end");
338         addSubtitleString(-2, ""); /* end last line */ 
339         
340         m_subtitle_page.m_have_pts = have_pts;
341         m_subtitle_page.m_pts = pts;
342         m_subtitle_page.m_timeout = 90000 * 20; /* 20s */
343         if (m_page_number != -1)
344                 sendSubtitlePage();  /* send assembled subtitle page to display */
345 }
346
347 void eDVBTeletextParser::setPage(int page)
348 {
349         if (page > 0)
350                 eDebug("enable teletext subtitle page %d", page);
351         else
352                 eDebug("disable teletext subtitles");
353         m_page_number = page;
354         m_page_M = (page >> 8) & 7; /* magazine to look for */
355         m_page_X = page & 0xFF;     /* page number */
356 }
357
358 void eDVBTeletextParser::connectNewPage(const Slot1<void, const eDVBTeletextSubtitlePage&> &slot, ePtr<eConnection> &connection)
359 {
360         connection = new eConnection(this, m_new_subtitle_page.connect(slot));
361 }
362
363 void eDVBTeletextParser::addSubtitleString(int color, std::string string)
364 {
365 //      eDebug("add subtitle string: %s, col %d", string.c_str(), color);
366
367         int force_cell = 0;
368
369         if (string.substr(0, 2) == "- ")
370         {
371                 string = string.substr(2);
372                 force_cell = 1;
373         }
374
375         int len = string.length();
376         int idx = 0;
377
378         while (idx < len)
379         {
380                 if (string[idx] >= 0x20)
381                         string[idx] = MapTeletextG0Latin1Char(0, (m_C >> 11), string[idx]);
382                 ++idx;
383         }
384
385 //      eDebug("color %d, m_subtitle_color %d", color, m_subtitle_color);
386         gRGB rgbcol((color & 1) ? 255 : 128, (color & 2) ? 255 : 128, (color & 4) ? 255 : 128);
387         if ((color != m_subtitle_color || force_cell) && !m_subtitle_text.empty() && ((color == -2) || !string.empty()))
388         {
389 //              eDebug("add text |%s|: %d != %d || %d", m_subtitle_text.c_str(), color, m_subtitle_color, force_cell);
390                 m_subtitle_page.m_elements.push_back(eDVBTeletextSubtitlePageElement(rgbcol, m_subtitle_text));
391                 m_subtitle_text = "";
392         } else if (!m_subtitle_text.empty() && m_subtitle_text[m_subtitle_text.size()-1] != ' ')
393                 m_subtitle_text += " ";
394         
395         if (!string.empty())
396         {
397 //              eDebug("set %d as new color", color);
398                 m_subtitle_color = color;
399                 m_subtitle_text += string;
400         }
401 }
402
403 void eDVBTeletextParser::sendSubtitlePage()
404 {
405 //      eDebug("subtitle page:");
406         //for (unsigned int i = 0; i < m_subtitle_page.m_elements.size(); ++i)
407         //      eDebug("%s", m_subtitle_page.m_elements[i].m_text.c_str());
408         m_new_subtitle_page(m_subtitle_page);
409 }