1 #include <lib/base/eerror.h>
2 #include <lib/dvb/teletext.h>
3 #include <lib/dvb/idemux.h>
4 #include <lib/gdi/gpixmap.h>
6 // Try to map teletext characters into ISO-8859-1 charset
7 // Use similar looking or similar meaning characters when possible.
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)
13 unsigned char LatinNationalOptionSubsetsLookup[16*8] =
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
33 unsigned char LatinNationalReplaceMap[128] =
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
45 // latin national option subsets
46 // see table 36 in ETSI EN 300 706
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
64 unsigned char MapTeletextG0Latin1Char(int Gtriplet, int NatOpts, unsigned char inchar)
66 int num = LatinNationalOptionSubsetsLookup[(Gtriplet&0xf)*(NatOpts&0x7)];
67 unsigned char c = inchar&0x7f;
68 unsigned char cc = LatinNationalReplaceMap[c];
70 return LatinNationalOptionSubsets[num*cc];
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
79 DEFINE_REF(eDVBTeletextParser);
81 /* we asumme error free transmission! */
82 static inline unsigned char decode_odd_parity(unsigned char *b)
85 unsigned char res = 0;
92 static inline unsigned char decode_hamming_84(unsigned char *b)
94 unsigned char res = 0;
102 static inline unsigned long decode_hamming_2418(unsigned char *b)
104 unsigned long h24 = b[0] | (b[1] << 8) | (b[2] << 16);
107 ((h24 & 0x000004) >> 2) |
108 ((h24 & 0x000070) >> 3) |
109 ((h24 & 0x007f00) >> 4) |
110 ((h24 & 0x7f0000) >> 5);
113 static int extractPTS(pts_t &pts, unsigned char *pkt)
118 pkt++; // header length
120 if (flags & 0x80) /* PTS present? */
123 pts = ((unsigned long long)(((pkt[0] >> 1) & 7))) << 30;
125 pts |= (pkt[2]>>1) << 15;
134 eDVBTeletextParser::eDVBTeletextParser(iDVBDemux *demux)
136 setStreamID(0xBD); /* as per en 300 472 */
138 setPageAndMagazine(0,0);
140 if (demux->createPESReader(eApp, m_pes_reader))
141 eDebug("failed to create teletext subtitle PES reader!");
143 m_pes_reader->connectRead(slot(*this, &eDVBTeletextParser::processData), m_read_connection);
146 eDVBTeletextParser::~eDVBTeletextParser()
150 void eDVBTeletextParser::processPESPacket(__u8 *pkt, int len)
152 unsigned char *p = pkt;
155 int have_pts = extractPTS(pts, pkt);
157 p += 4; len -= 4; /* start code, already be verified by pes parser */
158 p += 2; len -= 2; /* length, better use the argument */
160 p += 3; len -= 3; /* pes header */
162 p += 0x24; len -= 0x24; /* skip header */
164 // eDebug("data identifier: %02x", *p);
170 unsigned char data_unit_id = *p++;
171 unsigned char data_unit_length = *p++;
174 if (len < data_unit_length)
176 eDebug("data_unit_length > len");
180 if (data_unit_length != 44)
182 /* eDebug("illegal data unit length %d", data_unit_length); */
186 if (data_unit_id != 0x03)
188 /* eDebug("non subtitle data unit id %d", data_unit_id); */
192 unsigned char line_offset = *p++; len--;
193 unsigned char framing_code = *p++; len--;
195 int magazine_and_packet_address = decode_hamming_84(p++); len--;
196 magazine_and_packet_address |= decode_hamming_84(p++)<<4; len--;
198 unsigned char *data = p; p += 40; len -= 40;
200 if (framing_code != 0xe4) /* no teletxt data */
203 m_M = magazine_and_packet_address & 7;
204 m_Y = magazine_and_packet_address >> 3;
206 // eDebug("line %d, framing code: %02x, M=%02x, Y=%02x", line_offset, framing_code, m_M, m_Y);
208 if (m_Y == 0) /* page header */
212 m_S1 = decode_hamming_84(data + 2); /* S1 */
213 int S2C4 = decode_hamming_84(data + 3);
216 m_C |= (S2C4 & 8) ? (1<<4) : 0;
218 m_S3 = decode_hamming_84(data + 4);
220 int S4C5C6 = decode_hamming_84(data + 5);
223 m_C |= (S4C5C6 & 0xC) << 3;
225 m_C |= decode_hamming_84(data + 6) << 7;
226 m_C |= decode_hamming_84(data + 7) << 11;
228 int serial_mode = m_C & (1<<11);
230 /* page on the same magazine? end current page. */
231 if ((serial_mode || m_M == m_page_M) && m_page_open)
233 handlePageEnd(have_pts, pts);
237 m_X = decode_hamming_84(data+1) * 0x10 + decode_hamming_84(data);
239 if ((m_C & (1<<6)) && (m_X != 0xFF)) /* scan for pages with subtitle bit set */
241 eDVBServicePMTHandler::subtitleStream s;
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);
249 /* correct page on correct magazine? open page. */
250 if (m_M == m_page_M && m_X == m_page_X)
254 handleLine(data + 8, 32);
258 /* data for the selected page ? */
259 if (m_M == m_page_M && m_page_open)
260 handleLine(data, 40);
265 int eDVBTeletextParser::start(int pid)
272 return m_pes_reader->start(pid);
278 void eDVBTeletextParser::handlePageStart()
280 // if (m_C & (1<<4)) /* erase flag set */
282 /* we are always erasing the page,
283 even when the erase flag is not set. */
284 m_subtitle_page.clear();
287 void eDVBTeletextParser::handleLine(unsigned char *data, int len)
290 for (int i=0; i<len; ++i)
291 eDebugNoNewLine("%02x ", decode_odd_parity(data + i));
293 if (!m_Y) /* first line is page header, we don't need that. */
295 m_double_height = -1;
299 if (m_double_height == m_Y)
301 m_double_height = -1;
305 int last_was_white = 1, color = 7; /* start with whitespace. start with color=white. (that's unrelated.) */
309 // eDebug("handle subtitle line: %d len", len);
310 for (int i=0; i<len; ++i)
312 unsigned char b = decode_odd_parity(data + i);
314 if (b < 0x10) /* spacing attribute */
316 if (b < 8) /* colors */
318 if (b != color) /* new color is split into a new string */
320 addSubtitleString(color, text);
326 m_double_height = m_Y + 1;
327 } else if (b != 0xa && b != 0xb) /* box */
328 eDebug("[ignore %x]", b);
329 /* ignore other attributes */
332 //eDebugNoNewLine("%c", b);
333 /* no more than one whitespace, only printable chars */
334 if (((!last_was_white) || (b != ' ')) && (b >= 0x20))
337 last_was_white = b == ' ';
342 addSubtitleString(color, text);
345 void eDVBTeletextParser::handlePageEnd(int have_pts, const pts_t &pts)
347 // eDebug("handle page end");
348 addSubtitleString(-2, ""); /* end last line */
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 */
354 sendSubtitlePage(); /* send assembled subtitle page to display */
357 void eDVBTeletextParser::setPageAndMagazine(int page, int magazine)
360 eDebug("enable teletext subtitle page %d", page);
362 eDebug("disable teletext subtitles");
363 m_page_M = magazine&7; /* magazine to look for */
364 m_page_X = page&0xFF; /* page number */
367 void eDVBTeletextParser::connectNewPage(const Slot1<void, const eDVBTeletextSubtitlePage&> &slot, ePtr<eConnection> &connection)
369 connection = new eConnection(this, m_new_subtitle_page.connect(slot));
372 void eDVBTeletextParser::addSubtitleString(int color, std::string string)
374 // eDebug("add subtitle string: %s, col %d", string.c_str(), color);
378 if (string.substr(0, 2) == "- ")
380 string = string.substr(2);
384 int len = string.length();
389 if (string[idx] >= 0x20)
390 string[idx] = MapTeletextG0Latin1Char(0, (m_C >> 11), string[idx]);
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()))
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 += " ";
406 // eDebug("set %d as new color", color);
407 m_subtitle_color = color;
408 m_subtitle_text += string;
412 void eDVBTeletextParser::sendSubtitlePage()
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);