93b7cc6e3283d98f95bbca9d43c9a07214cc091c
[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 // G0 and G2 national option table
7 // see table 33 in ETSI EN 300 706
8 // use it with (triplet 1 bits 14-11)*(ctrl bits C12-14)
9
10 unsigned char NationalOptionSubsetsLookup[16*8] =
11 {
12         1, 4, 11, 5, 3, 8, 0, 1,
13         7, 4, 11, 5, 3, 1, 0, 1,
14         1, 4, 11, 5, 3, 8, 12, 1,
15         1, 1, 1, 1, 1, 10, 1, 9,
16         1, 4, 2, 6, 1, 1, 0, 1,
17         1, 1, 1, 1, 1, 1, 1, 1, // reserved
18         1, 1, 1, 1, 1, 1, 12, 1,
19         1, 1, 1, 1, 1, 1, 1, 1, // reserved
20         1, 1, 1, 1, 3, 1, 1, 1,
21         1, 1, 1, 1, 1, 1, 1, 1, // reserved
22         1, 1, 1, 1, 1, 1, 1, 1,
23         1, 1, 1, 1, 1, 1, 1, 1, // reserved
24         1, 1, 1, 1, 1, 1, 1, 1, // reserved
25         1, 1, 1, 1, 1, 1, 1, 1, // reserved
26         1, 1, 1, 1, 1, 1, 1, 1, // reserved
27         1, 1, 1, 1, 1, 1, 1, 1  // reserved
28 };
29
30 unsigned char NationalReplaceMap[128] =
31 {
32         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34         0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36         3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 6, 7, 8,
38         9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 0
40 };
41
42 // national option subsets (UTF8)
43 // see table 36 in ETSI EN 300 706
44
45 unsigned int NationalOptionSubsets[13*14] = {
46         0, 0x0023, 0xc5af, 0xc48d, 0xc5a5, 0xc5be, 0xc3bd, 0xc3ad, 0xc599, 0xc3a9, 0xc3a1, 0xc49b, 0xc3ba, 0xc5a1, // Slovak/Czech
47         0, 0xc2a3, 0x0024, 0x0040, 0xe28690, 0xc2bd, 0xe28692, 0xe28691, 0x0023, 0x002d, 0xc2bc, 0xc781, 0xc2be, 0xc3b7, // English
48         0, 0x0023, 0xc3b5, 0xc5A0, 0xc384, 0xc396, 0xc5bd, 0xc39c, 0xc395, 0xc5a1, 0xc3a4, 0xc3b6, 0xc5be, 0xc3bc, // Estonian
49         0, 0xc3a9, 0xc3af, 0xc3a0, 0xc3ab, 0xc3aa, 0xc3b9, 0xc3ae, 0x0023, 0xc3a8, 0xc3a2, 0xc3b4, 0xc3bb, 0xc3a7, // French
50         0, 0x0023, 0x0024, 0xc2a7, 0xc384, 0xc396, 0xc39c, 0x005e, 0x005f, 0xcb9a, 0xc3a4, 0xc3b6, 0xc3bc, 0xc39f, // German
51         0, 0xc2a3, 0x0024, 0xc3a9, 0xcb9a, 0xc3a7, 0xe28692, 0xe28691, 0x0023, 0xc3b9, 0xc3a0, 0xc3b2, 0xc3a8, 0xc3ac, // Italian
52         0, 0x0023, 0x0024, 0xc5a0, 0xc497, 0xc8a9, 0xc5bd, 0xc48d, 0xc5ab, 0xc5a1, 0xc485, 0xc5b3, 0xc5be, 0xc4af/*FIXMEE*/, // Lithuanian/Lettish
53         0, 0x0023, 0xc584, 0xc485, 0xc6b5, 0xc59a, 0xc581, 0xc487, 0xc3b3, 0xc499, 0xc5bc, 0xc59b, 0xc582, 0xc5ba, // Polish
54         0, 0xc3a7, 0x0024, 0xc2a1, 0xc3a1, 0xc3a9, 0xc3ad, 0xc3b3, 0xc3ba, 0xc2bf, 0xc3bc, 0xc3b1, 0xc3a8, 0xc3a0, // Spanish/Portuguese
55         0, 0x0023, 0xc2a4, 0xc5a2, 0xc382, 0xc59e, 0xc78d, 0xc38e, 0xc4b1, 0xc5a3, 0xc3a2, 0xc59f, 0xc78e, 0xc3ae, // Rumanian
56         0, 0x0023, 0xc38b, 0xc48c, 0xc486, 0xc5bd, 0xc490, 0xc5a0, 0xc3ab, 0xc48d, 0xc487, 0xc5be, 0xc491, 0xc5a1, // Slovenian/Serbian/Croation
57         0, 0x0023, 0xc2a4, 0xc389, 0xc384, 0xc396, 0xc385, 0xc39c, 0x005f, 0xc3a9, 0xc3a4, 0xc3b6, 0xc3a5, 0xc3bc, // Finnish/Hungarian/Swedish
58         0, 0xee8080/*FIXME*/, 0xc7a7, 0xc4b0, 0xc59e, 0xc396, 0xc387, 0xc39c, 0xc7a6, 0xc4b1, 0xc59f, 0xc3b6, 0xc3a7, 0xc3bc  // Turkish
59 };
60
61 // This is a very simple en300 706 telext decoder.
62 // It can only decode a single page at a time, thus it's only used
63 // for subtitles.
64  
65 DEFINE_REF(eDVBTeletextParser);
66
67         /* we asumme error free transmission! */
68 static inline unsigned char decode_odd_parity(unsigned char *b)
69 {
70         int i;
71         unsigned char res = 0;
72         for (i=0; i<7; ++i)
73                 if (*b & (0x80 >> i))
74                         res |= 1<<i;
75         return res;
76 }
77
78 static inline unsigned char decode_hamming_84(unsigned char *b)
79 {
80         unsigned char res = 0;
81         res |= (*b << 3) & 8;
82         res |= (*b     ) & 4;
83         res |= (*b >> 3) & 2;
84         res |= (*b >> 6) & 1;
85         return res;
86 }
87
88 static inline unsigned long decode_hamming_2418(unsigned char *b)
89 {
90         unsigned long h24 = b[0] | (b[1] << 8) | (b[2] << 16);
91         
92         return
93                 ((h24 & 0x000004) >> 2) |
94                 ((h24 & 0x000070) >> 3) |
95                 ((h24 & 0x007f00) >> 4) |
96                 ((h24 & 0x7f0000) >> 5);
97 }
98
99 static int extractPTS(pts_t &pts, unsigned char *pkt)
100 {
101         pkt += 7;
102         int flags = *pkt++;
103         
104         pkt++; // header length
105         
106         if (flags & 0x80) /* PTS present? */
107         {
108                         /* damn gcc bug */
109                 pts  = ((unsigned long long)(((pkt[0] >> 1) & 7))) << 30;
110                 pts |=   pkt[1] << 22;
111                 pts |=  (pkt[2]>>1) << 15;
112                 pts |=   pkt[3] << 7;
113                 pts |=  (pkt[5]>>1);
114                 
115                 return 0;
116         } else
117                 return -1;
118 }
119
120 eDVBTeletextParser::eDVBTeletextParser(iDVBDemux *demux)
121 {
122         setStreamID(0xBD); /* as per en 300 472 */
123         
124         setPageAndMagazine(0,0);
125         
126         if (demux->createPESReader(eApp, m_pes_reader))
127                 eDebug("failed to create teletext subtitle PES reader!");
128         else
129                 m_pes_reader->connectRead(slot(*this, &eDVBTeletextParser::processData), m_read_connection);
130 }
131
132 eDVBTeletextParser::~eDVBTeletextParser()
133 {
134 }
135
136 void eDVBTeletextParser::processPESPacket(__u8 *pkt, int len)
137 {
138         unsigned char *p = pkt;
139         
140         pts_t pts;
141         int have_pts = extractPTS(pts, pkt);
142         
143         p += 4; len -= 4; /* start code, already be verified by pes parser */
144         p += 2; len -= 2; /* length, better use the argument */ 
145         
146         p += 3; len -= 3; /* pes header */
147         
148         p += 0x24; len -= 0x24; /* skip header */
149         
150 //      eDebug("data identifier: %02x", *p);
151         
152         p++; len--;
153         
154         while (len > 2)
155         {
156                 unsigned char data_unit_id = *p++;
157                 unsigned char data_unit_length = *p++;
158                 len -= 2;
159                 
160                 if (len < data_unit_length)
161                 {
162                         eDebug("data_unit_length > len");
163                         break;
164                 }
165                 
166                 if (data_unit_length != 44)
167                 {
168                         /* eDebug("illegal data unit length %d", data_unit_length); */
169                         break;
170                 }
171                 
172                 if (data_unit_id != 0x03)
173                 {
174                         /* eDebug("non subtitle data unit id %d", data_unit_id); */
175                         break;
176                 }
177                 
178                 unsigned char line_offset = *p++; len--;
179                 unsigned char framing_code = *p++; len--;
180
181                 int magazine_and_packet_address = decode_hamming_84(p++); len--;
182                 magazine_and_packet_address |= decode_hamming_84(p++)<<4; len--;
183                 
184                 unsigned char *data = p; p += 40; len -= 40;
185                 
186                 if (framing_code != 0xe4) /* no teletxt data */
187                         continue;
188                 
189                 m_M = magazine_and_packet_address & 7;
190                 m_Y = magazine_and_packet_address >> 3;
191
192 //                      eDebug("line %d, framing code: %02x, M=%02x, Y=%02x", line_offset, framing_code, m_M, m_Y);
193                 
194                 if (m_Y == 0) /* page header */
195                 {
196                         m_C = 0;
197                         
198                         m_S1 = decode_hamming_84(data + 2); /* S1 */
199                         int S2C4 = decode_hamming_84(data + 3);
200                         
201                         m_S2 = S2C4 & 7;
202                         m_C |= (S2C4 & 8) ? (1<<4) : 0;
203                         
204                         m_S3 = decode_hamming_84(data + 4);
205                         
206                         int S4C5C6 = decode_hamming_84(data + 5);
207                         
208                         m_S4 = S4C5C6 & 3;
209                         m_C |= (S4C5C6 & 0xC) << 3;
210                         
211                         m_C |= decode_hamming_84(data + 6) << 7;
212                         m_C |= decode_hamming_84(data + 7) << 11;
213                         
214                         int serial_mode = m_C & (1<<11);
215                         
216                                 /* page on the same magazine? end current page. */
217                         if ((serial_mode || m_M == m_page_M) && m_page_open)
218                         {
219                                 handlePageEnd(have_pts, pts);
220                                 m_page_open = 0;
221                         }
222                         
223                         m_X = decode_hamming_84(data+1) * 0x10 + decode_hamming_84(data);
224                         
225                         if ((m_C & (1<<6)) && (m_X != 0xFF)) /* scan for pages with subtitle bit set */
226                         {
227                                 eDVBServicePMTHandler::subtitleStream s;
228                                 s.pid = m_pid;
229                                 s.subtitling_type = 0x01; // ebu teletext subtitle
230                                 s.teletext_page_number = m_X & 0xFF;
231                                 s.teletext_magazine_number = m_M & 7;
232                                 m_found_subtitle_pages.insert(s);
233                         }
234
235                                 /* correct page on correct magazine? open page. */
236                         if (m_M == m_page_M && m_X == m_page_X)
237                         {
238                                 handlePageStart();
239                                 m_page_open = 1;
240                                 handleLine(data + 8, 32);
241                         }
242                 } else if (m_Y < 26) // directly displayable packet
243                 {
244                         /* data for the selected page ? */
245                         if (m_M == m_page_M && m_page_open)
246                                 handleLine(data, 40);
247                 }
248 /*              else
249                 {
250                         if (m_M == m_page_M && m_page_open)
251                                 eDebug("ignore packet %d, disgnation code %d", m_Y, decode_hamming_84(data));
252                 }*/
253         }
254 }
255
256 int eDVBTeletextParser::start(int pid)
257 {
258         m_page_open = 0;
259
260         if (m_pes_reader)
261         {
262                 m_pid = pid;
263                 return m_pes_reader->start(pid);
264         }
265         else
266                 return -1;
267 }
268
269 void eDVBTeletextParser::handlePageStart()
270 {
271 //      if (m_C & (1<<4)) /* erase flag set */
272
273                 /* we are always erasing the page, 
274                    even when the erase flag is not set. */
275         m_subtitle_page.clear();
276 }
277
278 void eDVBTeletextParser::handleLine(unsigned char *data, int len)
279 {
280 /* // hexdump
281         for (int i=0; i<len; ++i)
282                 eDebugNoNewLine("%02x ", decode_odd_parity(data + i));
283         eDebug(""); */
284         if (!m_Y) /* first line is page header, we don't need that. */
285         {
286                 m_double_height = -1;
287                 return;
288         }
289                 
290         if (m_double_height == m_Y)
291         {
292                 m_double_height = -1;
293                 return;
294         }
295
296         int last_was_white = 1, color = 7; /* start with whitespace. start with color=white. (that's unrelated.) */
297         
298         std::string text;
299         
300 //      eDebug("handle subtitle line: %d len", len);
301         for (int i=0; i<len; ++i)
302         {
303                 unsigned char b = decode_odd_parity(data + i);
304         
305                 if (b < 0x10) /* spacing attribute */
306                 {
307                         if (b < 8) /* colors */
308                         {
309                                 if (b != color) /* new color is split into a new string */
310                                 {
311                                         addSubtitleString(color, text);
312                                         text = "";
313                                         color = b;
314                                 }
315                         } else if (b == 0xd)
316                         {
317                                 m_double_height = m_Y + 1;
318                         } else if (b != 0xa && b != 0xb) /* box */
319                                 eDebug("[ignore %x]", b);
320                                 /* ignore other attributes */
321                 } else
322                 {
323                         //eDebugNoNewLine("%c", b);
324                                 /* no more than one whitespace, only printable chars */
325                         if (((!last_was_white) || (b != ' ')) && (b >= 0x20))
326                         {
327                                 text += b;
328                                 last_was_white = b == ' ';
329                         }
330                 }
331         }
332         //eDebug("");
333         addSubtitleString(color, text);
334 }
335
336 void eDVBTeletextParser::handlePageEnd(int have_pts, const pts_t &pts)
337 {
338 //      eDebug("handle page end");
339         addSubtitleString(-2, ""); /* end last line */ 
340         
341         m_subtitle_page.m_have_pts = have_pts;
342         m_subtitle_page.m_pts = pts;
343         m_subtitle_page.m_timeout = 90000 * 20; /* 20s */
344         if (m_page_X != 0)
345                 sendSubtitlePage();  /* send assembled subtitle page to display */
346 }
347
348 void eDVBTeletextParser::setPageAndMagazine(int page, int magazine)
349 {
350         if (page > 0)
351                 eDebug("enable teletext subtitle page %d", page);
352         else
353                 eDebug("disable teletext subtitles");
354         m_page_M = magazine&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         static unsigned char out[512];
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                 idx = 0,
377                 outidx = 0,
378                 Gtriplet = 0,
379                 nat_opts = (m_C >> 11) & 0x7,
380                 nat_subset = NationalOptionSubsetsLookup[Gtriplet*8+nat_opts];
381         while (idx < len)
382         {
383                 unsigned char c = string[idx];
384                 if (c >= 0x20)
385                 {
386                         if (NationalReplaceMap[c])
387                         {
388                                 unsigned int utf8_code =
389                                         NationalOptionSubsets[nat_subset*14+c];
390                                 if (utf8_code > 0xFFFFFF)
391                                         out[outidx++]=(utf8_code&0xFF000000)>>24;
392                                 if (utf8_code > 0xFFFF)
393                                         out[outidx++]=(utf8_code&0xFF0000)>>16;
394                                 if (utf8_code > 0xFF)
395                                         out[outidx++]=(utf8_code&0xFF00)>>8;
396                                 out[outidx++]=utf8_code&0xFF;
397                         }
398                         else
399                                 out[outidx++] = c;
400                 }
401                 ++idx;
402         }
403
404 //      eDebug("color %d, m_subtitle_color %d", color, m_subtitle_color);
405         gRGB rgbcol((color & 1) ? 255 : 128, (color & 2) ? 255 : 128, (color & 4) ? 255 : 128);
406         if ((color != m_subtitle_color || force_cell) && !m_subtitle_text.empty() && ((color == -2) || outidx))
407         {
408 //              eDebug("add text |%s|: %d != %d || %d", m_subtitle_text.c_str(), color, m_subtitle_color, force_cell);
409                 m_subtitle_page.m_elements.push_back(eDVBTeletextSubtitlePageElement(rgbcol, m_subtitle_text));
410                 m_subtitle_text = "";
411         } else if (!m_subtitle_text.empty() && m_subtitle_text[m_subtitle_text.size()-1] != ' ')
412                 m_subtitle_text += " ";
413         
414         if (outidx)
415         {
416 //              eDebug("set %d as new color", color);
417                 m_subtitle_color = color;
418                 m_subtitle_text += std::string((const char*)out, outidx);
419         }
420 }
421
422 void eDVBTeletextParser::sendSubtitlePage()
423 {
424 //      eDebug("subtitle page:");
425         //for (unsigned int i = 0; i < m_subtitle_page.m_elements.size(); ++i)
426         //      eDebug("%s", m_subtitle_page.m_elements[i].m_text.c_str());
427         m_new_subtitle_page(m_subtitle_page);
428 }