1 #include <lib/base/eerror.h>
2 #include <lib/dvb/teletext.h>
3 #include <lib/dvb/idemux.h>
4 #include <lib/gdi/gpixmap.h>
7 This is a very simple en300 706 telext decoder.
9 It can only decode a single page at a time, thus it's only used
14 DEFINE_REF(eDVBTeletextParser);
16 /* we asumme error free transmission! */
17 static inline unsigned char decode_odd_parity(unsigned char *b)
20 unsigned char res = 0;
27 static inline unsigned char decode_hamming_84(unsigned char *b)
29 unsigned char res = 0;
37 static inline unsigned long decode_hamming_2418(unsigned char *b)
39 unsigned long h24 = b[0] | (b[1] << 8) | (b[2] << 16);
42 ((h24 & 0x000004) >> 2) |
43 ((h24 & 0x000070) >> 3) |
44 ((h24 & 0x007f00) >> 4) |
45 ((h24 & 0x7f0000) >> 5);
48 static int extractPTS(pts_t &pts, unsigned char *pkt)
53 pkt++; // header length
55 if (flags & 0x80) /* PTS present? */
58 pts = ((unsigned long long)(((pkt[0] >> 1) & 7))) << 30;
60 pts |= (pkt[2]>>1) << 15;
69 eDVBTeletextParser::eDVBTeletextParser(iDVBDemux *demux)
71 setStreamID(0xBD); /* as per en 300 472 */
75 if (demux->createPESReader(eApp, m_pes_reader))
76 eDebug("failed to create PES reader!");
78 m_pes_reader->connectRead(slot(*this, &eDVBTeletextParser::processData), m_read_connection);
81 eDVBTeletextParser::~eDVBTeletextParser()
85 void eDVBTeletextParser::processPESPacket(__u8 *pkt, int len)
87 unsigned char *p = pkt;
90 int have_pts = extractPTS(pts, pkt);
92 p += 4; len -= 4; /* start code, already be verified by pes parser */
93 p += 2; len -= 2; /* length, better use the argument */
95 p += 3; len -= 3; /* pes header */
97 p += 0x24; len -= 0x24; /* skip header */
99 // eDebug("data identifier: %02x", *p);
105 unsigned char data_unit_id = *p++;
106 unsigned char data_unit_length = *p++;
109 if (len < data_unit_length)
111 eDebug("data_unit_length > len");
115 if (data_unit_length != 44)
117 /* eDebug("illegal data unit length %d", data_unit_length); */
121 unsigned char line_offset = *p++; len--;
122 unsigned char framing_code = *p++; len--;
124 int magazine_and_packet_address = decode_hamming_84(p++); len--;
125 magazine_and_packet_address |= decode_hamming_84(p++)<<4; len--;
127 unsigned char *data = p; p += 40; len -= 40;
129 if (framing_code != 0xe4) /* no teletxt data */
132 m_M = magazine_and_packet_address & 7;
133 m_Y = magazine_and_packet_address >> 3;
135 // eDebug("line %d, framing code: %02x, M=%02x, Y=%02x", line_offset, framing_code, m_M, m_Y);
137 if (m_Y == 0) /* page header */
141 m_S1 = decode_hamming_84(data + 2); /* S1 */
142 int S2C4 = decode_hamming_84(data + 3);
145 m_C |= (S2C4 & 8) ? (1<<4) : 0;
147 m_S3 = decode_hamming_84(data + 4);
149 int S4C5C6 = decode_hamming_84(data + 5);
152 m_C |= (S4C5C6 & 0xC) << 3;
154 m_C |= decode_hamming_84(data + 6) << 7;
155 m_C |= decode_hamming_84(data + 7) << 11;
157 int serial_mode = m_C & (1<<11);
159 /* page on the same magazine? end current page. */
160 if ((serial_mode || (m_M == m_page_M)) && (m_page_open))
162 handlePageEnd(have_pts, pts);
166 m_X = decode_hamming_84(data+1) * 0x10 + decode_hamming_84(data);
168 if ((m_C & (1<<6)) && (m_X != 0xFF)) /* scan for pages with subtitle bit set */
169 m_found_subtitle_pages.insert((m_M << 8) | m_X);
171 /* correct page on correct magazine? open page. */
172 if ((m_M == m_page_M) && (m_X == m_page_X))
176 handleLine(data + 8, 32);
180 /* data for the selected page? */
181 if ((m_M == m_page_M) && m_page_open)
182 handleLine(data, 40);
187 int eDVBTeletextParser::start(int pid)
192 return m_pes_reader->start(pid);
197 void eDVBTeletextParser::handlePageStart()
199 // if (m_C & (1<<4)) /* erase flag set */
201 /* we are always erasing the page,
202 even when the erase flag is not set. */
203 m_subtitle_page.clear();
206 void eDVBTeletextParser::handleLine(unsigned char *data, int len)
209 for (int i=0; i<len; ++i)
210 eDebugNoNewLine("%02x ", decode_odd_parity(data + i));
212 if (!m_Y) /* first line is page header, we don't need that. */
214 m_double_height = -1;
218 if (m_double_height == m_Y)
220 m_double_height = -1;
224 int last_was_white = 1, color = 7; /* start with whitespace. start with color=white. (that's unrelated.) */
228 // eDebug("handle subtitle line: %d len", len);
229 for (int i=0; i<len; ++i)
231 unsigned char b = decode_odd_parity(data + i);
233 if (b < 0x10) /* spacing attribute */
235 if (b < 8) /* colors */
237 if (b != color) /* new color is split into a new string */
239 addSubtitleString(color, text);
245 m_double_height = m_Y + 1;
246 } else if (b != 0xa && b != 0xb) /* box */
247 eDebug("[ignore %x]", b);
248 /* ignore other attributes */
251 eDebugNoNewLine("%c", b);
252 /* no more than one whitespace, only printable chars */
253 if (((!last_was_white) || (b != ' ')) && (b >= 0x20))
256 last_was_white = b == ' ';
261 addSubtitleString(color, text);
264 void eDVBTeletextParser::handlePageEnd(int have_pts, const pts_t &pts)
266 eDebug("handle page end");
267 addSubtitleString(-2, ""); /* end last line */
269 m_subtitle_page.m_have_pts = have_pts;
270 m_subtitle_page.m_pts = pts;
271 m_subtitle_page.m_timeout = 90000 * 20; /* 20s */
272 sendSubtitlePage(); /* send assembled subtitle page to display */
275 void eDVBTeletextParser::setPage(int page)
277 m_page_M = (page >> 8) & 7; /* magazine to look for */
278 m_page_X = page & 0xFF; /* page number */
281 void eDVBTeletextParser::connectNewPage(const Slot1<void, const eDVBTeletextSubtitlePage&> &slot, ePtr<eConnection> &connection)
283 connection = new eConnection(this, m_new_subtitle_page.connect(slot));
286 void eDVBTeletextParser::addSubtitleString(int color, std::string string)
288 // eDebug("add subtitle string: %s, col %d", string.c_str(), color);
292 if (string.substr(0, 2) == "- ")
294 string = string.substr(2);
298 // eDebug("color %d, m_subtitle_color %d", color, m_subtitle_color);
299 gRGB rgbcol((color & 1) ? 255 : 128, (color & 2) ? 255 : 128, (color & 4) ? 255 : 128);
300 if ((color != m_subtitle_color || force_cell) && !m_subtitle_text.empty() && ((color == -2) || !string.empty()))
302 // eDebug("add text |%s|: %d != %d || %d", m_subtitle_text.c_str(), color, m_subtitle_color, force_cell);
303 m_subtitle_page.m_elements.push_back(eDVBTeletextSubtitlePageElement(rgbcol, m_subtitle_text));
304 m_subtitle_text = "";
305 } else if (!m_subtitle_text.empty() && m_subtitle_text[m_subtitle_text.size()-1] != ' ')
306 m_subtitle_text += " ";
310 // eDebug("set %d as new color", color);
311 m_subtitle_color = color;
312 m_subtitle_text += string;
316 void eDVBTeletextParser::sendSubtitlePage()
318 eDebug("subtitle page:");
319 for (unsigned int i = 0; i < m_subtitle_page.m_elements.size(); ++i)
320 eDebug("%s", m_subtitle_page.m_elements[i].m_text.c_str());
321 m_new_subtitle_page(m_subtitle_page);