Merge branch 'master' of git.opendreambox.org:/git/enigma2
[enigma2.git] / lib / dvb / teletext.cpp
index 79b3a9ca10dd49f59aea5aa74e1ed3612149c4a8..40650c01864bf652847a135a1b5d0f0592539445 100644 (file)
@@ -287,7 +287,7 @@ void eDVBTeletextParser::processPESPacket(__u8 *pkt, int len)
                        if ((serial_mode || M == m_page_M) && m_page_open)
                        {
                                eDebug("Page End %d %lld", !have_pts, pts);
-                               handlePageEnd(have_pts, pts);
+                               handlePageEnd(!have_pts, pts);
                                m_page_open = 0;
                        }
 
@@ -406,8 +406,8 @@ void eDVBTeletextParser::processPESPacket(__u8 *pkt, int len)
                        int designation_code = decode_hamming_84(data++);
                        if (designation_code == 0) // 29/0
                        {
-                               m_M29_t1 = decode_hamming_2418(data++);
-                               m_M29_t2 = decode_hamming_2418(data++);
+                               m_M29_t1 = decode_hamming_2418(data);
+                               m_M29_t2 = decode_hamming_2418(data+3);
                                if ((m_M29_t1 & 0xF) == 0) // format1
                                        m_M29_0_valid = 1;
                                else
@@ -422,8 +422,8 @@ void eDVBTeletextParser::processPESPacket(__u8 *pkt, int len)
                        if (Y == 28 && designation_code == 0)   // 28/0
                        {
 #if 1
-                               m_X28_t1 = decode_hamming_2418(data++);
-                               m_X28_t2 = decode_hamming_2418(data++);
+                               m_X28_t1 = decode_hamming_2418(data);
+                               m_X28_t2 = decode_hamming_2418(data+3);
                                if ((m_X28_t1 & 0xF) == 0) // format1
                                        m_X28_0_valid = 1;
                                else
@@ -435,6 +435,15 @@ void eDVBTeletextParser::processPESPacket(__u8 *pkt, int len)
                                                int tripletX = decode_hamming_2418(data+i);
                                                if (tripletX >= 0)
                                                {
+                                                       if (i == 0)
+                                                       {
+                                                               if ((m_X28_t1 & 0xF) == 0) // format1
+                                                                       m_X28_0_valid = 1;
+                                                               m_X28_t1 = tripletX;
+                                                       }
+                                                       else if (i == 1)
+                                                               m_X28_t2 = tripletX;
+
                                                        char *c = get_bits(tripletX, 18);
                                                        int x=0;
                                                        for (; x < 18; ++x)
@@ -477,6 +486,7 @@ void eDVBTeletextParser::handlePageStart()
        }
 //     else
 //             eDebug("no erase flag set!");
+       m_current_source_line = -1;
 }
 
 void eDVBTeletextParser::handleLine(unsigned char *data, int len)
@@ -485,6 +495,9 @@ void eDVBTeletextParser::handleLine(unsigned char *data, int len)
        for (int i=0; i<len; ++i)
                eDebugNoNewLine("%02x ", decode_odd_parity(data + i));
        eDebug(""); */
+       
+       m_subtitle_page.clearLine(m_Y);
+
        if (!m_Y) /* first line is page header, we don't need that. */
        {
                m_double_height = -1;
@@ -510,24 +523,24 @@ void eDVBTeletextParser::handleLine(unsigned char *data, int len)
                nat_subset = nat_subset_2,
                second_G0_set = 0;
 
-               if (m_X28_0_valid)
-               {
-                       nat_subset = NationalOptionSubsetsLookup[((m_X28_t1 >> 14) & 0xF) | ((m_X28_t2 & 7) << 4)];
-                       nat_subset_2 = NationalOptionSubsetsLookup[(m_X28_t1 >> 7) & 0xF];
-//                     eDebug("override nat subset with X28/0... nat_subset2 is %d", nat_subset_2);
-               }
-               else if (m_M29_0_valid)
-               {
-                       nat_subset = NationalOptionSubsetsLookup[((m_M29_t1 >> 14) & 0xF) | ((m_M29_t2 & 7) << 4)];
-                       nat_subset_2 = NationalOptionSubsetsLookup[(m_M29_t1 >> 7) & 0xF];
-//                     eDebug("override nat subset with M29/0... nat_subset2 is %d", nat_subset_2);
-               }
-
-       /*eDebug("nat_opts = %d, nat_subset = %d, C121314 = %d%d%d",
-               nat_opts, nat_subset,
-               (m_C & (1<<12))?1:0,
-               (m_C & (1<<13))?1:0,
-               (m_C & (1<<14))?1:0);*/
+       if (m_X28_0_valid)
+       {
+               nat_subset = NationalOptionSubsetsLookup[(m_X28_t1 >> 7) & 0x7F];
+               nat_subset_2 = NationalOptionSubsetsLookup[((m_X28_t1 >> 14) & 0xF) | ((m_X28_t2 & 7) << 4)];
+//             eDebug("X/28/0 nat_subset %d, nat_subset2 %d", nat_subset, nat_subset_2);
+       }
+       else if (m_M29_0_valid)
+       {
+               nat_subset = NationalOptionSubsetsLookup[(m_M29_t1 >> 7) & 0x7F];
+               nat_subset_2 = NationalOptionSubsetsLookup[((m_M29_t1 >> 14) & 0xF) | ((m_M29_t2 & 7) << 4)];
+//             eDebug("M/29/0 nat_subset %d, nat_subset2 %d", nat_subset, nat_subset_2);
+       }
+/*     else
+               eDebug("nat_opts = %d, nat_subset = %d, C121314 = %d%d%d",
+                       nat_opts, nat_subset,
+                       (m_C & (1<<12))?1:0,
+                       (m_C & (1<<13))?1:0,
+                       (m_C & (1<<14))?1:0);*/
 
 //     eDebug("handle subtitle line: %d len", len);
        for (int i=0; i<len; ++i)
@@ -565,7 +578,7 @@ void eDVBTeletextParser::handleLine(unsigned char *data, int len)
                        {
                                if (b != color) /* new color is split into a new string */
                                {
-                                       addSubtitleString(color, std::string((const char*)out, outidx));
+                                       addSubtitleString(color, std::string((const char*)out, outidx), m_Y);
                                        outidx = 0;
                                        color = b;
                                }
@@ -609,13 +622,13 @@ void eDVBTeletextParser::handleLine(unsigned char *data, int len)
                }
        }
 //     eDebug("");
-       addSubtitleString(color, std::string((const char*)out, outidx));
+       addSubtitleString(color, std::string((const char*)out, outidx), m_Y);
 }
 
 void eDVBTeletextParser::handlePageEnd(int have_pts, const pts_t &pts)
 {
 //     eDebug("handle page end");
-       addSubtitleString(-2, ""); /* end last line */ 
+       addSubtitleString(-2, "", -1); /* end last line */ 
        
        m_subtitle_page.m_have_pts = have_pts;
        m_subtitle_page.m_pts = pts;
@@ -644,7 +657,7 @@ void eDVBTeletextParser::connectNewPage(const Slot1<void, const eDVBTeletextSubt
        connection = new eConnection(this, m_new_subtitle_page.connect(slot));
 }
 
-void eDVBTeletextParser::addSubtitleString(int color, std::string string)
+void eDVBTeletextParser::addSubtitleString(int color, std::string string, int source_line)
 {
 //     eDebug("(%d)add subtitle string: %s, col %d", m_Y, string.c_str(), color);
        int force_cell = 0;
@@ -656,19 +669,24 @@ void eDVBTeletextParser::addSubtitleString(int color, std::string string)
        }
 
 //     eDebug("color %d, m_subtitle_color %d", color, m_subtitle_color);
-       gRGB rgbcol((color & 1) ? 255 : 128, (color & 2) ? 255 : 128, (color & 4) ? 255 : 128);
        if ((color != m_subtitle_color || force_cell) && !m_subtitle_text.empty() && ((color == -2) || !string.empty()))
        {
+               const gRGB pal[4] = { gRGB(64, 0, 76), gRGB(180, 40, 86), gRGB(160, 170, 105), gRGB(250, 200, 140) };
 //             eDebug("add text |%s|: %d != %d || %d", m_subtitle_text.c_str(), color, m_subtitle_color, force_cell);
-               m_subtitle_page.m_elements.push_back(eDVBTeletextSubtitlePageElement(rgbcol, m_subtitle_text));
+               m_subtitle_page.m_elements.push_back(eDVBTeletextSubtitlePageElement(pal[m_subtitle_color & 3], m_subtitle_text, m_current_source_line));
+               m_current_source_line = -1;
                m_subtitle_text = "";
        } else if (!m_subtitle_text.empty() && m_subtitle_text[m_subtitle_text.size()-1] != ' ')
                m_subtitle_text += " ";
-       
+
+       if (m_current_source_line == -1)
+               m_current_source_line = source_line;
+
        if (!string.empty())
        {
 //             eDebug("set %d as new color", color);
-               m_subtitle_color = color;
+               if (color >= 0)
+                       m_subtitle_color = color;
                m_subtitle_text += string;
        }
 }
@@ -676,11 +694,10 @@ void eDVBTeletextParser::addSubtitleString(int color, std::string string)
 void eDVBTeletextParser::sendSubtitlePage()
 {
 //     eDebug("subtitle page:");
-       bool empty=true;
-       if (empty)
-               for (unsigned int i = 0; i < m_subtitle_page.m_elements.size(); ++i)
-                       if (!m_subtitle_page.m_elements[i].m_text.empty())
-                               empty=false;
-       if (!empty)
+       bool send=m_C & (1<<4);
+       for (unsigned int i = 0; i < m_subtitle_page.m_elements.size(); ++i)
+               if (!m_subtitle_page.m_elements[i].m_text.empty())
+                       send=true;
+       if (send)
                m_new_subtitle_page(m_subtitle_page);
 }