cafd891076cda55a640c6afe63569c48a566b8fc
[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, 9,
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, 0xc5a3, 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(-1, -1);
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                 int M = magazine_and_packet_address & 7,
190                         Y = magazine_and_packet_address >> 3;
191 //                      eDebug("line %d, framing code: %02x, M=%02x, Y=%02x", line_offset, framing_code, m_M, m_Y);
192                 
193                 if (Y == 0) /* page header */
194                 {
195                         int X = decode_hamming_84(data + 1) * 0x10 + decode_hamming_84(data),
196 //                              S1 = decode_hamming_84(data + 2),
197                                 S2C4 = decode_hamming_84(data + 3),
198 //                              S2 = S2C4 & 7,
199 //                              S3 = decode_hamming_84(data + 4),
200                                 S4C5C6 = decode_hamming_84(data + 5),
201 //                              S4 = S4C5C6 & 3,
202                                 C = ((S2C4 & 8) ? (1<<4) : 0) |
203                                         ((S4C5C6 & 0xC) << 3) |
204                                         (decode_hamming_84(data + 6) << 7) |
205                                         (decode_hamming_84(data + 7) << 11),
206                                 serial_mode = C & (1<<11);
207
208                                 /* page on the same magazine? end current page. */
209                         if ((serial_mode || M == m_page_M) && m_page_open)
210                         {
211                                 handlePageEnd(have_pts, pts);
212                                 m_page_open = 0;
213                         }
214
215                         if ((C & (1<<6)) && (X != 0xFF) && !(C & (1<<5))) /* scan for pages with subtitle bit set */
216                         {
217                                 eDVBServicePMTHandler::subtitleStream s;
218                                 s.pid = m_pid;
219                                 s.subtitling_type = 0x01; // ebu teletext subtitle
220                                 s.teletext_page_number = X & 0xFF;
221                                 s.teletext_magazine_number = M & 7;
222                                 m_found_subtitle_pages.insert(s);
223                         }
224
225                                 /* correct page on correct magazine? open page. */
226                         if (M == m_page_M && X == m_page_X)
227                         {
228                                 handlePageStart();
229                                 m_subtitle_page.m_C = C;
230                                 m_subtitle_page.m_Y = Y; 
231                                 m_page_open = 1;
232                                 m_box_open = 0;
233                                 handleLine(data + 8, 32);
234                         }
235                 } else if (Y < 26) // directly displayable packet
236                 {
237                         /* data for the selected page ? */
238                         if (M == m_page_M && m_page_open)
239                         {
240                                 m_subtitle_page.m_Y = Y;
241                                 m_box_open = 0;
242                                 handleLine(data, 40);
243                         }
244                 } else if (Y > 29)
245                 {
246                 }
247                 else if (m_page_open)
248                 {
249                         if (M == m_page_M)
250                                 eDebug("ignore packet X/%d/%d", Y, decode_hamming_84(data));
251                         else
252                                 eDebug("ignore packet M/%d/%d", Y, decode_hamming_84(data));
253                 }
254         }
255 }
256
257 int eDVBTeletextParser::start(int pid)
258 {
259         m_page_open = 0;
260
261         if (m_pes_reader)
262         {
263                 m_pid = pid;
264                 return m_pes_reader->start(pid);
265         }
266         else
267                 return -1;
268 }
269
270 void eDVBTeletextParser::handlePageStart()
271 {
272 //      if (m_C & (1<<4)) /* erase flag set */
273
274                 /* we are always erasing the page, 
275                    even when the erase flag is not set. */
276         m_subtitle_page.clear();
277 }
278
279 void eDVBTeletextParser::handleLine(unsigned char *data, int len)
280 {
281 /* // hexdump
282         for (int i=0; i<len; ++i)
283                 eDebugNoNewLine("%02x ", decode_odd_parity(data + i));
284         eDebug(""); */
285         if (!m_subtitle_page.m_Y) /* first line is page header, we don't need that. */
286         {
287                 m_double_height = -1;
288                 return;
289         }
290                 
291         if (m_double_height == m_subtitle_page.m_Y)
292         {
293                 m_double_height = -1;
294                 return;
295         }
296
297         int last_was_white = 1, color = 1; /* start with whitespace. start with color=white. (that's unrelated.) */
298
299         static unsigned char out[128];
300
301         int outidx = 0,
302                 Gtriplet = 0,
303                 nat_opts = (m_subtitle_page.m_C & (1<<14) ? 1 : 0) |
304                                         (m_subtitle_page.m_C & (1<<13) ? 2 : 0) |
305                                         (m_subtitle_page.m_C & (1<<12) ? 4 : 0),
306                 nat_subset = NationalOptionSubsetsLookup[Gtriplet*8+nat_opts];
307 /*      eDebug("nat_opts = %d, nat_subset = %d, C121314 = %d%d%d, m_C %08x",
308                 nat_opts, nat_subset,
309                 (m_subtitle_page.m_C & (1<<12))?1:0,
310                 (m_subtitle_page.m_C & (1<<13))?1:0,
311                 (m_subtitle_page.m_C & (1<<14))?1:0,
312                 m_subtitle_page.m_C); */
313
314 //      eDebug("handle subtitle line: %d len", len);
315         for (int i=0; i<len; ++i)
316         {
317                 unsigned char b = decode_odd_parity(data + i);
318         
319                 if (b < 0x10) /* spacing attribute */
320                 {
321                         if (b < 8) /* colors */
322                         {
323                                 if (b != color) /* new color is split into a new string */
324                                 {
325                                         addSubtitleString(color, std::string((const char*)out, outidx));
326                                         outidx = 0;
327                                         color = b;
328                                 }
329                         }
330                         else if (b == 0xd)
331                                 m_double_height = m_subtitle_page.m_Y + 1;
332                         else if (b == 0xa)  // close box
333                                 m_box_open=0;
334                         else if (b == 0xb)  // open box
335                                 ++m_box_open;
336                         else
337                                 eDebug("[ignore %x]", b);
338                                 /* ignore other attributes */
339                 } else if (m_box_open>1)
340                 {
341                         //eDebugNoNewLine("%c", b);
342                                 /* no more than one whitespace, only printable chars */
343                         if (((!last_was_white) || (b != ' ')) && (b >= 0x20))
344                         {
345                                 unsigned char offs = NationalReplaceMap[b];
346                                 if (offs)
347                                 {
348                                         unsigned int utf8_code =
349                                                 NationalOptionSubsets[nat_subset*14+offs];
350                                         if (utf8_code > 0xFFFFFF)
351                                                 out[outidx++]=(utf8_code&0xFF000000)>>24;
352                                         if (utf8_code > 0xFFFF)
353                                                 out[outidx++]=(utf8_code&0xFF0000)>>16;
354                                         if (utf8_code > 0xFF)
355                                                 out[outidx++]=(utf8_code&0xFF00)>>8;
356                                         out[outidx++]=utf8_code&0xFF;
357                                 }
358                                 else
359                                         out[outidx++] = b;
360                                 last_was_white = b == ' ';
361                         }
362                 }
363         }
364         //eDebug("");
365         addSubtitleString(color, std::string((const char*)out, outidx));
366 }
367
368 void eDVBTeletextParser::handlePageEnd(int have_pts, const pts_t &pts)
369 {
370 //      eDebug("handle page end");
371         addSubtitleString(-2, ""); /* end last line */ 
372         
373         m_subtitle_page.m_have_pts = have_pts;
374         m_subtitle_page.m_pts = pts;
375         m_subtitle_page.m_timeout = 90000 * 20; /* 20s */
376         if (m_page_X != 0)
377                 sendSubtitlePage();  /* send assembled subtitle page to display */
378 }
379
380 void eDVBTeletextParser::setPageAndMagazine(int page, int magazine)
381 {
382         if (page > 0)
383                 eDebug("enable teletext subtitle page %x%02x", magazine, page);
384         else
385                 eDebug("disable teletext subtitles");
386         m_page_M = magazine; /* magazine to look for */
387         if (magazine != -1)
388                 m_page_M &= 7;
389         m_page_X = page;  /* page number */
390         if (page != -1)
391                 m_page_X &= 0xFF;
392 }
393
394 void eDVBTeletextParser::connectNewPage(const Slot1<void, const eDVBTeletextSubtitlePage&> &slot, ePtr<eConnection> &connection)
395 {
396         connection = new eConnection(this, m_new_subtitle_page.connect(slot));
397 }
398
399 void eDVBTeletextParser::addSubtitleString(int color, std::string string)
400 {
401 //      eDebug("add subtitle string: %s, col %d", string.c_str(), color);
402         int force_cell = 0;
403
404         if (string.substr(0, 2) == "- ")
405         {
406                 string = string.substr(2);
407                 force_cell = 1;
408         }
409
410 //      eDebug("color %d, m_subtitle_color %d", color, m_subtitle_color);
411         gRGB rgbcol((color & 1) ? 255 : 128, (color & 2) ? 255 : 128, (color & 4) ? 255 : 128);
412         if ((color != m_subtitle_color || force_cell) && !m_subtitle_text.empty() && ((color == -2) || !string.empty()))
413         {
414 //              eDebug("add text |%s|: %d != %d || %d", m_subtitle_text.c_str(), color, m_subtitle_color, force_cell);
415                 m_subtitle_page.m_elements.push_back(eDVBTeletextSubtitlePageElement(rgbcol, m_subtitle_text));
416                 m_subtitle_text = "";
417         } else if (!m_subtitle_text.empty() && m_subtitle_text[m_subtitle_text.size()-1] != ' ')
418                 m_subtitle_text += " ";
419         
420         if (!string.empty())
421         {
422 //              eDebug("set %d as new color", color);
423                 m_subtitle_color = color;
424                 m_subtitle_text += string;
425         }
426 }
427
428 void eDVBTeletextParser::sendSubtitlePage()
429 {
430 //      eDebug("subtitle page:");
431         bool empty=true;
432         if (empty)
433                 for (unsigned int i = 0; i < m_subtitle_page.m_elements.size(); ++i)
434                         if (!m_subtitle_page.m_elements[i].m_text.empty())
435                                 empty=false;
436         if (!empty)
437                 m_new_subtitle_page(m_subtitle_page);
438 }