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, 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
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, 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
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 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);
193 if (Y == 0) /* page header */
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),
199 // S3 = decode_hamming_84(data + 4),
200 S4C5C6 = decode_hamming_84(data + 5),
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);
208 /* page on the same magazine? end current page. */
209 if ((serial_mode || M == m_page_M) && m_page_open)
211 handlePageEnd(have_pts, pts);
215 if ((C & (1<<6)) && (X != 0xFF)) /* scan for pages with subtitle bit set */
217 eDVBServicePMTHandler::subtitleStream s;
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);
225 /* correct page on correct magazine? open page. */
226 if (M == m_page_M && X == m_page_X)
229 m_subtitle_page.m_C = C;
230 m_subtitle_page.m_Y = Y;
232 handleLine(data + 8, 32);
234 } else if (Y < 26) // directly displayable packet
236 /* data for the selected page ? */
237 if (M == m_page_M && m_page_open)
239 m_subtitle_page.m_Y = Y;
240 handleLine(data, 40);
245 if (M == m_page_M && m_page_open)
246 eDebug("ignore packet %d, designation code %d", Y, decode_hamming_84(data));
251 int eDVBTeletextParser::start(int pid)
258 return m_pes_reader->start(pid);
264 void eDVBTeletextParser::handlePageStart()
266 // if (m_C & (1<<4)) /* erase flag set */
268 /* we are always erasing the page,
269 even when the erase flag is not set. */
270 m_subtitle_page.clear();
273 void eDVBTeletextParser::handleLine(unsigned char *data, int len)
276 for (int i=0; i<len; ++i)
277 eDebugNoNewLine("%02x ", decode_odd_parity(data + i));
279 if (!m_subtitle_page.m_Y) /* first line is page header, we don't need that. */
281 m_double_height = -1;
285 if (m_double_height == m_subtitle_page.m_Y)
287 m_double_height = -1;
291 int last_was_white = 1, color = 7; /* start with whitespace. start with color=white. (that's unrelated.) */
295 // eDebug("handle subtitle line: %d len", len);
296 for (int i=0; i<len; ++i)
298 unsigned char b = decode_odd_parity(data + i);
300 if (b < 0x10) /* spacing attribute */
302 if (b < 8) /* colors */
304 if (b != color) /* new color is split into a new string */
306 addSubtitleString(color, text);
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 */
318 //eDebugNoNewLine("%c", b);
319 /* no more than one whitespace, only printable chars */
320 if (((!last_was_white) || (b != ' ')) && (b >= 0x20))
323 last_was_white = b == ' ';
328 addSubtitleString(color, text);
331 void eDVBTeletextParser::handlePageEnd(int have_pts, const pts_t &pts)
333 // eDebug("handle page end");
334 addSubtitleString(-2, ""); /* end last line */
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 */
340 sendSubtitlePage(); /* send assembled subtitle page to display */
343 void eDVBTeletextParser::setPageAndMagazine(int page, int magazine)
346 eDebug("enable teletext subtitle page %x%02x", magazine, page);
348 eDebug("disable teletext subtitles");
349 m_page_M = magazine&7; /* magazine to look for */
350 m_page_X = page&0xFF; /* page number */
353 void eDVBTeletextParser::connectNewPage(const Slot1<void, const eDVBTeletextSubtitlePage&> &slot, ePtr<eConnection> &connection)
355 connection = new eConnection(this, m_new_subtitle_page.connect(slot));
358 void eDVBTeletextParser::addSubtitleString(int color, std::string string)
360 // eDebug("add subtitle string: %s, col %d", string.c_str(), color);
361 static unsigned char out[512];
364 if (string.substr(0, 2) == "- ")
366 string = string.substr(2);
370 int len = string.length(),
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); */
386 unsigned char c = string[idx];
389 unsigned char offs = NationalReplaceMap[c];
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;
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))
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 += " ";
420 // eDebug("set %d as new color", color);
421 m_subtitle_color = color;
422 m_subtitle_text += std::string((const char*)out, outidx);
426 void eDVBTeletextParser::sendSubtitlePage()
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);