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