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 // 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)
10 unsigned char NationalOptionSubsetsLookup[16*8] =
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
30 unsigned char NationalReplaceMap[128] =
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
42 // national option subsets (UTF8)
43 // see table 36 in ETSI EN 300 706
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
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
65 DEFINE_REF(eDVBTeletextParser);
67 /* we asumme error free transmission! */
68 static inline unsigned char decode_odd_parity(unsigned char *b)
71 unsigned char res = 0;
78 static inline unsigned char decode_hamming_84(unsigned char *b)
80 unsigned char res = 0;
88 static inline unsigned long decode_hamming_2418(unsigned char *b)
90 unsigned long h24 = b[0] | (b[1] << 8) | (b[2] << 16);
93 ((h24 & 0x000004) >> 2) |
94 ((h24 & 0x000070) >> 3) |
95 ((h24 & 0x007f00) >> 4) |
96 ((h24 & 0x7f0000) >> 5);
99 static int extractPTS(pts_t &pts, unsigned char *pkt)
104 pkt++; // header length
106 if (flags & 0x80) /* PTS present? */
109 pts = ((unsigned long long)(((pkt[0] >> 1) & 7))) << 30;
111 pts |= (pkt[2]>>1) << 15;
120 eDVBTeletextParser::eDVBTeletextParser(iDVBDemux *demux)
122 setStreamID(0xBD); /* as per en 300 472 */
124 setPageAndMagazine(0,0);
126 if (demux->createPESReader(eApp, m_pes_reader))
127 eDebug("failed to create teletext subtitle PES reader!");
129 m_pes_reader->connectRead(slot(*this, &eDVBTeletextParser::processData), m_read_connection);
132 eDVBTeletextParser::~eDVBTeletextParser()
136 void eDVBTeletextParser::processPESPacket(__u8 *pkt, int len)
138 unsigned char *p = pkt;
141 int have_pts = extractPTS(pts, pkt);
143 p += 4; len -= 4; /* start code, already be verified by pes parser */
144 p += 2; len -= 2; /* length, better use the argument */
146 p += 3; len -= 3; /* pes header */
148 p += 0x24; len -= 0x24; /* skip header */
150 // eDebug("data identifier: %02x", *p);
156 unsigned char data_unit_id = *p++;
157 unsigned char data_unit_length = *p++;
160 if (len < data_unit_length)
162 eDebug("data_unit_length > len");
166 if (data_unit_length != 44)
168 /* eDebug("illegal data unit length %d", data_unit_length); */
172 if (data_unit_id != 0x03)
174 /* eDebug("non subtitle data unit id %d", data_unit_id); */
178 unsigned char line_offset = *p++; len--;
179 unsigned char framing_code = *p++; len--;
181 int magazine_and_packet_address = decode_hamming_84(p++); len--;
182 magazine_and_packet_address |= decode_hamming_84(p++)<<4; len--;
184 unsigned char *data = p; p += 40; len -= 40;
186 if (framing_code != 0xe4) /* no teletxt data */
189 m_M = magazine_and_packet_address & 7;
190 m_Y = magazine_and_packet_address >> 3;
192 // eDebug("line %d, framing code: %02x, M=%02x, Y=%02x", line_offset, framing_code, m_M, m_Y);
194 if (m_Y == 0) /* page header */
198 m_S1 = decode_hamming_84(data + 2); /* S1 */
199 int S2C4 = decode_hamming_84(data + 3);
202 m_C |= (S2C4 & 8) ? (1<<4) : 0;
204 m_S3 = decode_hamming_84(data + 4);
206 int S4C5C6 = decode_hamming_84(data + 5);
209 m_C |= (S4C5C6 & 0xC) << 3;
211 m_C |= decode_hamming_84(data + 6) << 7;
212 m_C |= decode_hamming_84(data + 7) << 11;
214 int serial_mode = m_C & (1<<11);
216 /* page on the same magazine? end current page. */
217 if ((serial_mode || m_M == m_page_M) && m_page_open)
219 handlePageEnd(have_pts, pts);
223 m_X = decode_hamming_84(data+1) * 0x10 + decode_hamming_84(data);
225 if ((m_C & (1<<6)) && (m_X != 0xFF)) /* scan for pages with subtitle bit set */
227 eDVBServicePMTHandler::subtitleStream s;
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);
235 /* correct page on correct magazine? open page. */
236 if (m_M == m_page_M && m_X == m_page_X)
240 handleLine(data + 8, 32);
242 } else if (m_Y < 26) // directly displayable packet
244 /* data for the selected page ? */
245 if (m_M == m_page_M && m_page_open)
246 handleLine(data, 40);
250 if (m_M == m_page_M && m_page_open)
251 eDebug("ignore packet %d, disgnation code %d", m_Y, decode_hamming_84(data));
256 int eDVBTeletextParser::start(int pid)
263 return m_pes_reader->start(pid);
269 void eDVBTeletextParser::handlePageStart()
271 // if (m_C & (1<<4)) /* erase flag set */
273 /* we are always erasing the page,
274 even when the erase flag is not set. */
275 m_subtitle_page.clear();
278 void eDVBTeletextParser::handleLine(unsigned char *data, int len)
281 for (int i=0; i<len; ++i)
282 eDebugNoNewLine("%02x ", decode_odd_parity(data + i));
284 if (!m_Y) /* first line is page header, we don't need that. */
286 m_double_height = -1;
290 if (m_double_height == m_Y)
292 m_double_height = -1;
296 int last_was_white = 1, color = 7; /* start with whitespace. start with color=white. (that's unrelated.) */
300 // eDebug("handle subtitle line: %d len", len);
301 for (int i=0; i<len; ++i)
303 unsigned char b = decode_odd_parity(data + i);
305 if (b < 0x10) /* spacing attribute */
307 if (b < 8) /* colors */
309 if (b != color) /* new color is split into a new string */
311 addSubtitleString(color, text);
317 m_double_height = m_Y + 1;
318 } else if (b != 0xa && b != 0xb) /* box */
319 eDebug("[ignore %x]", b);
320 /* ignore other attributes */
323 //eDebugNoNewLine("%c", b);
324 /* no more than one whitespace, only printable chars */
325 if (((!last_was_white) || (b != ' ')) && (b >= 0x20))
328 last_was_white = b == ' ';
333 addSubtitleString(color, text);
336 void eDVBTeletextParser::handlePageEnd(int have_pts, const pts_t &pts)
338 // eDebug("handle page end");
339 addSubtitleString(-2, ""); /* end last line */
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 */
345 sendSubtitlePage(); /* send assembled subtitle page to display */
348 void eDVBTeletextParser::setPageAndMagazine(int page, int magazine)
351 eDebug("enable teletext subtitle page %d", page);
353 eDebug("disable teletext subtitles");
354 m_page_M = magazine&7; /* magazine to look for */
355 m_page_X = page&0xFF; /* page number */
358 void eDVBTeletextParser::connectNewPage(const Slot1<void, const eDVBTeletextSubtitlePage&> &slot, ePtr<eConnection> &connection)
360 connection = new eConnection(this, m_new_subtitle_page.connect(slot));
363 void eDVBTeletextParser::addSubtitleString(int color, std::string string)
365 // eDebug("add subtitle string: %s, col %d", string.c_str(), color);
366 static unsigned char out[512];
369 if (string.substr(0, 2) == "- ")
371 string = string.substr(2);
375 int len = string.length(),
379 nat_opts = (m_C >> 11) & 0x7,
380 nat_subset = NationalOptionSubsetsLookup[Gtriplet*8+nat_opts];
383 unsigned char c = string[idx];
386 if (NationalReplaceMap[c])
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;
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))
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 += " ";
416 // eDebug("set %d as new color", color);
417 m_subtitle_color = color;
418 m_subtitle_text += std::string((const char*)out, outidx);
422 void eDVBTeletextParser::sendSubtitlePage()
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);