ignore all non diseqc 1.2 lnbs for satpos dependent tuners
[enigma2.git] / lib / dvb / scan.cpp
index 8b895dc9f8a42a59ea959604e162a033bb2f0a30..379ab8e9a01b56924486e70cc370d4c9b1f2c57a 100644 (file)
 #include <lib/base/estring.h>
 #include <errno.h>
 
 #include <lib/base/estring.h>
 #include <errno.h>
 
-static bool scan_debug;
-#define SCAN_eDebug(x...) do { if (scan_debug) eDebug(x); } while(0)
-#define SCAN_eDebugNoNewLine(x...) do { if (scan_debug) eDebugNoNewLine(x); } while(0)
+#define SCAN_eDebug(x...) do { if (m_scan_debug) eDebug(x); } while(0)
+#define SCAN_eDebugNoNewLine(x...) do { if (m_scan_debug) eDebugNoNewLine(x); } while(0)
 
 DEFINE_REF(eDVBScan);
 
 eDVBScan::eDVBScan(iDVBChannel *channel, bool usePAT, bool debug)
        :m_channel(channel), m_channel_state(iDVBChannel::state_idle)
        ,m_ready(0), m_ready_all(usePAT ? (readySDT|readyPAT) : readySDT)
 
 DEFINE_REF(eDVBScan);
 
 eDVBScan::eDVBScan(iDVBChannel *channel, bool usePAT, bool debug)
        :m_channel(channel), m_channel_state(iDVBChannel::state_idle)
        ,m_ready(0), m_ready_all(usePAT ? (readySDT|readyPAT) : readySDT)
-       ,m_flags(0), m_usePAT(usePAT)
+       ,m_pmt_running(false), m_abort_current_pmt(false), m_flags(0)
+       ,m_usePAT(usePAT), m_scan_debug(debug)
 {
 {
-       scan_debug=debug;
        if (m_channel->getDemux(m_demux))
                SCAN_eDebug("scan: failed to allocate demux!");
        m_channel->connectStateChange(slot(*this, &eDVBScan::stateChange), m_stateChanged_connection);
        if (m_channel->getDemux(m_demux))
                SCAN_eDebug("scan: failed to allocate demux!");
        m_channel->connectStateChange(slot(*this, &eDVBScan::stateChange), m_stateChanged_connection);
@@ -41,6 +40,8 @@ int eDVBScan::isValidONIDTSID(int orbital_position, eOriginalNetworkID onid, eTr
        case 0:
        case 0x1111:
                return 0;
        case 0:
        case 0x1111:
                return 0;
+       case 0x13E:  // workaround for 11258H and 11470V on hotbird with same ONID/TSID (0x13E/0x578)
+               return orbital_position != 130 || tsid != 0x578;
        case 1:
                return orbital_position == 192;
        case 0x00B1:
        case 1:
                return orbital_position == 192;
        case 0x00B1:
@@ -84,10 +85,12 @@ RESULT eDVBScan::nextChannel()
 {
        ePtr<iDVBFrontend> fe;
 
 {
        ePtr<iDVBFrontend> fe;
 
-       m_SDT = 0; m_BAT = 0; m_NIT = 0;
+       m_SDT = 0; m_PAT = 0; m_BAT = 0; m_NIT = 0, m_PMT = 0;
 
        m_ready = 0;
 
 
        m_ready = 0;
 
+       m_pat_tsid = eTransportStreamID();
+
                /* check what we need */
        m_ready_all = readySDT;
        
                /* check what we need */
        m_ready_all = readySDT;
        
@@ -120,25 +123,13 @@ RESULT eDVBScan::nextChannel()
                return -ENOTSUP;
        }
 
                return -ENOTSUP;
        }
 
-       int fetype;
-       fe->getFrontendType(fetype);
-       if ( fetype == iDVBFrontend::feSatellite)
-       {
-               eDVBFrontendParametersSatellite p;
-               m_ch_current->getDVBS(p);
-               m_chid_current = eDVBChannelID(p.orbital_position << 16, -1, -1);
-       }
-       else
-               m_chid_current = eDVBChannelID();
+       m_chid_current = eDVBChannelID();
 
        m_channel_state = iDVBChannel::state_idle;
 
        m_channel_state = iDVBChannel::state_idle;
+
        if (fe->tune(*m_ch_current))
        if (fe->tune(*m_ch_current))
-       {
                return nextChannel();
                return nextChannel();
-               m_event(evtFail);
-               return -EINVAL;
-       }
-               
+
        m_event(evtUpdate);
        return 0;
 }
        m_event(evtUpdate);
        return 0;
 }
@@ -153,20 +144,59 @@ RESULT eDVBScan::startFilter()
        if (m_ready_all & readyPAT)
                startSDT = m_ready & readyPAT;
 
        if (m_ready_all & readyPAT)
                startSDT = m_ready & readyPAT;
 
+       // m_ch_current is not set, when eDVBScan is just used for a SDT update
+       if (!m_ch_current)
+       {
+               unsigned int channelFlags;
+               m_channel->getCurrentFrontendParameters(m_ch_current);
+               m_ch_current->getFlags(channelFlags);
+               if (channelFlags & iDVBFrontendParameters::flagOnlyFree)
+                       m_flags |= scanOnlyFree;
+       }
+
        m_SDT = 0;
        if (startSDT && (m_ready_all & readySDT))
        {
        m_SDT = 0;
        if (startSDT && (m_ready_all & readySDT))
        {
-               m_SDT = new eTable<ServiceDescriptionSection>();
+               m_SDT = new eTable<ServiceDescriptionSection>(m_scan_debug);
+               int tsid=-1;
                if (m_ready & readyPAT && m_ready & validPAT)
                {
                        std::vector<ProgramAssociationSection*>::const_iterator i =
                                m_PAT->getSections().begin();
                        assert(i != m_PAT->getSections().end());
                if (m_ready & readyPAT && m_ready & validPAT)
                {
                        std::vector<ProgramAssociationSection*>::const_iterator i =
                                m_PAT->getSections().begin();
                        assert(i != m_PAT->getSections().end());
-                       int tsid = (*i)->getTableIdExtension(); // in PAT this is the transport stream id
-                       if (m_SDT->start(m_demux, eDVBSDTSpec(tsid, true)))
+                       tsid = (*i)->getTableIdExtension(); // in PAT this is the transport stream id
+                       m_pat_tsid = eTransportStreamID(tsid);
+                       for (; i != m_PAT->getSections().end(); ++i)
+                       {
+                               const ProgramAssociationSection &pat = **i;
+                               ProgramAssociationConstIterator program = pat.getPrograms()->begin();
+                               for (; program != pat.getPrograms()->end(); ++program)
+                                       m_pmts_to_read.insert(std::pair<unsigned short, service>((*program)->getProgramNumber(), service((*program)->getProgramMapPid())));
+                       }
+                       m_PMT = new eTable<ProgramMapSection>(m_scan_debug);
+                       CONNECT(m_PMT->tableReady, eDVBScan::PMTready);
+                       PMTready(-2);
+                       // KabelBW HACK ... on 618Mhz and 626Mhz the transport stream id in PAT and SDT is different
+
+                       {
+                               int type;
+                               m_ch_current->getSystem(type);
+                               if (type == iDVBFrontend::feCable)
+                               {
+                                       eDVBFrontendParametersCable parm;
+                                       m_ch_current->getDVBC(parm);
+                                       if ((tsid == 0x00d7 && abs(parm.frequency-618000) < 2000) ||
+                                               (tsid == 0x00d8 && abs(parm.frequency-626000) < 2000))
+                                               tsid = -1;
+                               }
+                       }
+               }
+               if (tsid == -1)
+               {
+                       if (m_SDT->start(m_demux, eDVBSDTSpec()))
                                return -1;
                }
                                return -1;
                }
-               else if (m_SDT->start(m_demux, eDVBSDTSpec()))
+               else if (m_SDT->start(m_demux, eDVBSDTSpec(tsid, true)))
                        return -1;
                CONNECT(m_SDT->tableReady, eDVBScan::SDTready);
        }
                        return -1;
                CONNECT(m_SDT->tableReady, eDVBScan::SDTready);
        }
@@ -176,8 +206,8 @@ RESULT eDVBScan::startFilter()
                m_PAT = 0;
                if (m_ready_all & readyPAT)
                {
                m_PAT = 0;
                if (m_ready_all & readyPAT)
                {
-                       m_PAT = new eTable<ProgramAssociationSection>();
-                       if (m_PAT->start(m_demux, eDVBPATSpec()))
+                       m_PAT = new eTable<ProgramAssociationSection>(m_scan_debug);
+                       if (m_PAT->start(m_demux, eDVBPATSpec(4000)))
                                return -1;
                        CONNECT(m_PAT->tableReady, eDVBScan::PATready);
                }
                                return -1;
                        CONNECT(m_PAT->tableReady, eDVBScan::PATready);
                }
@@ -185,7 +215,7 @@ RESULT eDVBScan::startFilter()
                m_NIT = 0;
                if (m_ready_all & readyNIT)
                {
                m_NIT = 0;
                if (m_ready_all & readyNIT)
                {
-                       m_NIT = new eTable<NetworkInformationSection>();
+                       m_NIT = new eTable<NetworkInformationSection>(m_scan_debug);
                        if (m_NIT->start(m_demux, eDVBNITSpec()))
                                return -1;
                        CONNECT(m_NIT->tableReady, eDVBScan::NITready);
                        if (m_NIT->start(m_demux, eDVBNITSpec()))
                                return -1;
                        CONNECT(m_NIT->tableReady, eDVBScan::NITready);
@@ -194,19 +224,18 @@ RESULT eDVBScan::startFilter()
                m_BAT = 0;
                if (m_ready_all & readyBAT)
                {
                m_BAT = 0;
                if (m_ready_all & readyBAT)
                {
-                       m_BAT = new eTable<BouquetAssociationSection>();
+                       m_BAT = new eTable<BouquetAssociationSection>(m_scan_debug);
                        if (m_BAT->start(m_demux, eDVBBATSpec()))
                                return -1;
                        CONNECT(m_BAT->tableReady, eDVBScan::BATready);
                }
        }
                        if (m_BAT->start(m_demux, eDVBBATSpec()))
                                return -1;
                        CONNECT(m_BAT->tableReady, eDVBScan::BATready);
                }
        }
-
        return 0;
 }
 
 void eDVBScan::SDTready(int err)
 {
        return 0;
 }
 
 void eDVBScan::SDTready(int err)
 {
-       SCAN_eDebug("got sdt");
+       SCAN_eDebug("got sdt %d", err);
        m_ready |= readySDT;
        if (!err)
                m_ready |= validSDT;
        m_ready |= readySDT;
        if (!err)
                m_ready |= validSDT;
@@ -240,6 +269,125 @@ void eDVBScan::PATready(int err)
        startFilter(); // for starting the SDT filter
 }
 
        startFilter(); // for starting the SDT filter
 }
 
+void eDVBScan::PMTready(int err)
+{
+       SCAN_eDebug("got pmt %d", err);
+       if (!err)
+       {
+               bool scrambled = false;
+               bool have_audio = false;
+               bool have_video = false;
+               unsigned short pcrpid = 0xFFFF;
+               std::vector<ProgramMapSection*>::const_iterator i;
+               for (i = m_PMT->getSections().begin(); i != m_PMT->getSections().end(); ++i)
+               {
+                       const ProgramMapSection &pmt = **i;
+                       if (pcrpid == 0xFFFF)
+                               pcrpid = pmt.getPcrPid();
+                       else
+                               SCAN_eDebug("already have a pcrpid %04x %04x", pcrpid, pmt.getPcrPid());
+                       ElementaryStreamInfoConstIterator es;
+                       for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
+                       {
+                               int isaudio = 0, isvideo = 0, is_scrambled = 0;
+                               switch ((*es)->getType())
+                               {
+                               case 0x1b: // AVC Video Stream (MPEG4 H264)
+                               case 0x01: // MPEG 1 video
+                               case 0x02: // MPEG 2 video
+                                       isvideo = 1;
+                                       //break; fall through !!!
+                               case 0x03: // MPEG 1 audio
+                               case 0x04: // MPEG 2 audio:
+                                       if (!isvideo)
+                                               isaudio = 1;
+                                       //break; fall through !!!
+                               case 0x06: // PES Private
+                               case 0x81: // user private
+                                               /* PES private can contain AC-3, DTS or lots of other stuff.
+                                                  check descriptors to get the exact type. */
+                                       for (DescriptorConstIterator desc = (*es)->getDescriptors()->begin();
+                                                       desc != (*es)->getDescriptors()->end(); ++desc)
+                                       {
+                                               uint8_t tag = (*desc)->getTag();
+                                               if (!isaudio && !isvideo)
+                                               {
+                                                       switch (tag)
+                                                       {
+                                                       case DTS_DESCRIPTOR:
+                                                       case AAC_DESCRIPTOR:
+                                                       case AC3_DESCRIPTOR:
+                                                               isaudio = 1;
+                                                               break;
+                                                       case REGISTRATION_DESCRIPTOR: /* some services don't have a separate AC3 descriptor */
+                                                       {
+                                                                       /* libdvbsi++ doesn't yet support this descriptor type, so work around. */
+                                                               if ((*desc)->getLength() != 4)
+                                                                       break;
+                                                               unsigned char descr[6];
+                                                               (*desc)->writeToBuffer(descr);
+                                                               int format_identifier = (descr[2] << 24) | (descr[3] << 16) | (descr[4] << 8) | (descr[5]);
+                                                               switch (format_identifier)
+                                                               {
+                                                               case 0x41432d33:
+                                                                       isaudio = 1;
+                                                               default:
+                                                                       break;
+                                                               }
+                                                               break;
+                                                       }
+                                                       }
+                                               }
+                                               if (tag == CA_DESCRIPTOR)
+                                                       is_scrambled = 1;
+                                       }
+                                       break;
+                               }
+                               if (isaudio)
+                                       have_audio = true;
+                               else if (isvideo)
+                                       have_video = true;
+                               else
+                                       continue;
+                               if (is_scrambled)
+                                       scrambled = true;
+                       }
+                       for (DescriptorConstIterator desc = pmt.getDescriptors()->begin();
+                               desc != pmt.getDescriptors()->end(); ++desc)
+                       {
+                               if ((*desc)->getTag() == CA_DESCRIPTOR)
+                                       scrambled = true;
+                       }
+               }
+               m_pmt_in_progress->second.scrambled = scrambled;
+               if ( have_video )
+                       m_pmt_in_progress->second.serviceType = 1;
+               else if ( have_audio )
+                       m_pmt_in_progress->second.serviceType = 2;
+               else
+                       m_pmt_in_progress->second.serviceType = 100;
+       }
+       if (err == -1) // timeout or removed by sdt
+               m_pmts_to_read.erase(m_pmt_in_progress++);
+       else if (m_pmt_running)
+               ++m_pmt_in_progress;
+       else
+       {
+               m_pmt_in_progress = m_pmts_to_read.begin();
+               m_pmt_running = true;
+       }
+
+       if (m_pmt_in_progress != m_pmts_to_read.end())
+               m_PMT->start(m_demux, eDVBPMTSpec(m_pmt_in_progress->second.pmtPid, m_pmt_in_progress->first, 4000));
+       else
+       {
+               m_PMT = 0;
+               m_pmt_running = false;
+               channelDone();
+       }
+}
+
+
 void eDVBScan::addKnownGoodChannel(const eDVBChannelID &chid, iDVBFrontendParameters *feparm)
 {
                /* add it to the list of known channels. */
 void eDVBScan::addKnownGoodChannel(const eDVBChannelID &chid, iDVBFrontendParameters *feparm)
 {
                /* add it to the list of known channels. */
@@ -260,7 +408,7 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
        {
                eDVBFrontendParametersSatellite parm;
                feparm->getDVBS(parm);
        {
                eDVBFrontendParametersSatellite parm;
                feparm->getDVBS(parm);
-               eDebug("try to add %d %d %d %d %d %d",
+               SCAN_eDebug("try to add %d %d %d %d %d %d",
                        parm.orbital_position, parm.frequency, parm.symbol_rate, parm.polarisation, parm.fec, parm.modulation);
                break;
        }
                        parm.orbital_position, parm.frequency, parm.symbol_rate, parm.polarisation, parm.fec, parm.modulation);
                break;
        }
@@ -268,7 +416,7 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
        {
                eDVBFrontendParametersCable parm;
                feparm->getDVBC(parm);
        {
                eDVBFrontendParametersCable parm;
                feparm->getDVBC(parm);
-               eDebug("try to add %d %d %d %d",
+               SCAN_eDebug("try to add %d %d %d %d",
                        parm.frequency, parm.symbol_rate, parm.modulation, parm.fec_inner);
                break;
        }
                        parm.frequency, parm.symbol_rate, parm.modulation, parm.fec_inner);
                break;
        }
@@ -276,7 +424,7 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
        {
                eDVBFrontendParametersTerrestrial parm;
                feparm->getDVBT(parm);
        {
                eDVBFrontendParametersTerrestrial parm;
                feparm->getDVBT(parm);
-               eDebug("try to add %d %d %d %d %d %d %d %d",
+               SCAN_eDebug("try to add %d %d %d %d %d %d %d %d",
                        parm.frequency, parm.modulation, parm.transmission_mode, parm.hierarchy,
                        parm.guard_interval, parm.code_rate_LP, parm.code_rate_HP, parm.bandwidth);
                break;
                        parm.frequency, parm.modulation, parm.transmission_mode, parm.hierarchy,
                        parm.guard_interval, parm.code_rate_LP, parm.code_rate_HP, parm.bandwidth);
                break;
@@ -292,11 +440,11 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
                        if (!found_count)
                        {
                                *i = feparm;  // update
                        if (!found_count)
                        {
                                *i = feparm;  // update
-                               eDebug("update");
+                               SCAN_eDebug("update");
                        }
                        else
                        {
                        }
                        else
                        {
-                               eDebug("remove dupe");
+                               SCAN_eDebug("remove dupe");
                                m_ch_toScan.erase(i++);
                                continue;
                        }
                                m_ch_toScan.erase(i++);
                                continue;
                        }
@@ -307,7 +455,7 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
 
        if (found_count > 0)
        {
 
        if (found_count > 0)
        {
-               eDebug("already in todo list");
+               SCAN_eDebug("already in todo list");
                return;
        }
 
                return;
        }
 
@@ -315,7 +463,7 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
        for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_scanned.begin()); i != m_ch_scanned.end(); ++i)
                if (sameChannel(*i, feparm))
                {
        for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_scanned.begin()); i != m_ch_scanned.end(); ++i)
                if (sameChannel(*i, feparm))
                {
-                       eDebug("successfully scanned");
+                       SCAN_eDebug("successfully scanned");
                        return;
                }
 
                        return;
                }
 
@@ -323,18 +471,18 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
        for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_unavailable.begin()); i != m_ch_unavailable.end(); ++i)
                if (sameChannel(*i, feparm, true))
                {
        for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_unavailable.begin()); i != m_ch_unavailable.end(); ++i)
                if (sameChannel(*i, feparm, true))
                {
-                       eDebug("scanned but not available");
+                       SCAN_eDebug("scanned but not available");
                        return;
                }
 
                /* ... on the current channel */
        if (sameChannel(m_ch_current, feparm))
        {
                        return;
                }
 
                /* ... on the current channel */
        if (sameChannel(m_ch_current, feparm))
        {
-               eDebug("is current");
+               SCAN_eDebug("is current");
                return;
        }
 
                return;
        }
 
-       eDebug("really add");
+       SCAN_eDebug("really add");
                /* otherwise, add it to the todo list. */
        m_ch_toScan.push_front(feparm); // better.. then the rotor not turning wild from east to west :)
 }
                /* otherwise, add it to the todo list. */
        m_ch_toScan.push_front(feparm); // better.. then the rotor not turning wild from east to west :)
 }
@@ -351,14 +499,10 @@ int eDVBScan::sameChannel(iDVBFrontendParameters *ch1, iDVBFrontendParameters *c
 
 void eDVBScan::channelDone()
 {
 
 void eDVBScan::channelDone()
 {
-       if (m_ready & validSDT)
+       if (m_ready & validSDT && (!(m_flags & scanOnlyFree) || !m_pmt_running))
        {
                unsigned long hash = 0;
 
        {
                unsigned long hash = 0;
 
-               // m_ch_current is not set, when eDVBScan is just used for a SDT update
-               if (!m_ch_current)
-                       m_channel->getCurrentFrontendParameters(m_ch_current);
-
                m_ch_current->getHash(hash);
                
                eDVBNamespace dvbnamespace = buildNamespace(
                m_ch_current->getHash(hash);
                
                eDVBNamespace dvbnamespace = buildNamespace(
@@ -376,12 +520,13 @@ void eDVBScan::channelDone()
        if (m_ready & validNIT)
        {
                int system;
        if (m_ready & validNIT)
        {
                int system;
+               std::list<ePtr<iDVBFrontendParameters> > m_ch_toScan_backup;
                m_ch_current->getSystem(system);
                SCAN_eDebug("dumping NIT");
                if (m_flags & clearToScanOnFirstNIT)
                {
                m_ch_current->getSystem(system);
                SCAN_eDebug("dumping NIT");
                if (m_flags & clearToScanOnFirstNIT)
                {
+                       m_ch_toScan_backup = m_ch_toScan;
                        m_ch_toScan.clear();
                        m_ch_toScan.clear();
-                       m_flags &= ~clearToScanOnFirstNIT;
                }
                std::vector<NetworkInformationSection*>::const_iterator i;
                for (i = m_NIT->getSections().begin(); i != m_NIT->getSections().end(); ++i)
                }
                std::vector<NetworkInformationSection*>::const_iterator i;
                for (i = m_NIT->getSections().begin(); i != m_NIT->getSections().end(); ++i)
@@ -452,19 +597,29 @@ void eDVBScan::channelDone()
                                                ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
                                                eDVBFrontendParametersSatellite sat;
                                                sat.set(d);
                                                ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
                                                eDVBFrontendParametersSatellite sat;
                                                sat.set(d);
+
+                                               eDVBFrontendParametersSatellite p;
+                                               m_ch_current->getDVBS(p);
+
+                                               if ( abs(p.orbital_position - sat.orbital_position) < 5 )
+                                                       sat.orbital_position = p.orbital_position;
+
+                                               if ( abs(abs(3600 - p.orbital_position) - sat.orbital_position) < 5 )
+                                               {
+                                                       SCAN_eDebug("found transponder with incorrect west/east flag ... correct this");
+                                                       sat.orbital_position = p.orbital_position;
+                                               }
+
                                                feparm->setDVBS(sat);
                                                feparm->setDVBS(sat);
-                                               unsigned long hash=0;
-                                               feparm->getHash(hash);
-                                               
-                                               eDVBNamespace ns = buildNamespace(onid, tsid, hash);
-                                               
-                                               if ( m_chid_current.dvbnamespace.get() != -1 &&
-                                                       ((ns.get() ^ m_chid_current.dvbnamespace.get()) & 0xFFFF0000))
+
+                                               if ( p.orbital_position != sat.orbital_position)
                                                        SCAN_eDebug("dropping this transponder, it's on another satellite.");
                                                else
                                                {
                                                        SCAN_eDebug("dropping this transponder, it's on another satellite.");
                                                else
                                                {
+                                                       unsigned long hash=0;
+                                                       feparm->getHash(hash);
                                                        addChannelToScan(
                                                        addChannelToScan(
-                                                                       eDVBChannelID(ns, tsid, onid),
+                                                                       eDVBChannelID(buildNamespace(onid, tsid, hash), tsid, onid),
                                                                        feparm);
                                                }
                                                break;
                                                                        feparm);
                                                }
                                                break;
@@ -474,14 +629,38 @@ void eDVBScan::channelDone()
                                                break;
                                        }
                                }
                                                break;
                                        }
                                }
-                               
                        }
                        }
+                       
                }
                }
+
+                       /* a pitfall is to have the clearToScanOnFirstNIT-flag set, and having channels which have
+                          no or invalid NIT. this code will not erase the toScan list unless at least one valid entry
+                          has been found.
+
+                          This is not a perfect solution, as the channel could contain a partial NIT. Life's bad.
+                       */
+               if (m_flags & clearToScanOnFirstNIT)
+               {
+                       if (m_ch_toScan.empty())
+                       {
+                               eWarning("clearToScanOnFirstNIT was set, but NIT is invalid. Refusing to stop scan.");
+                               m_ch_toScan = m_ch_toScan_backup;
+                       } else
+                               m_flags &= ~clearToScanOnFirstNIT;
+               }
                m_ready &= ~validNIT;
        }
                m_ready &= ~validNIT;
        }
-       
-       if ((m_ready  & m_ready_all) != m_ready_all)
+
+       if (m_pmt_running || (m_ready & m_ready_all) != m_ready_all)
+       {
+               if (m_abort_current_pmt)
+               {
+                       m_abort_current_pmt = false;
+                       PMTready(-1);
+               }
                return;
                return;
+       }
+
        SCAN_eDebug("channel done!");
        
                /* if we had services on this channel, we declare
        SCAN_eDebug("channel done!");
        
                /* if we had services on this channel, we declare
@@ -505,26 +684,127 @@ void eDVBScan::channelDone()
                   here, and not before.
                */
 
                   here, and not before.
                */
 
-       if (!m_chid_current)
-               eWarning("SCAN: the current channel's ID was not corrected - not adding channel.");
-       else
+       for (m_pmt_in_progress = m_pmts_to_read.begin(); m_pmt_in_progress != m_pmts_to_read.end();)
        {
        {
-               addKnownGoodChannel(m_chid_current, m_ch_current);
-       
-               m_ch_scanned.push_back(m_ch_current);
+               int type;
+               eServiceReferenceDVB ref;
+               ePtr<eDVBService> service = new eDVBService;
+
+               if (!m_chid_current)
+               {
+                       unsigned long hash = 0;
+
+                       m_ch_current->getHash(hash);
+
+                       m_chid_current = eDVBChannelID(
+                               buildNamespace(eOriginalNetworkID(0), m_pat_tsid, hash),
+                               m_pat_tsid, eOriginalNetworkID(0));
+               }
 
 
-               for (std::list<ePtr<iDVBFrontendParameters> >::iterator i(m_ch_toScan.begin()); i != m_ch_toScan.end();)
+               if (m_pmt_in_progress->second.serviceType == 1)
+                       SCAN_eDebug("SID %04x is VIDEO", m_pmt_in_progress->first);
+               else if (m_pmt_in_progress->second.serviceType == 2)
+                       SCAN_eDebug("SID %04x is AUDIO", m_pmt_in_progress->first);
+               else
+                       SCAN_eDebug("SID %04x is DATA", m_pmt_in_progress->first);
+
+               ref.set(m_chid_current);
+               ref.setServiceID(m_pmt_in_progress->first);
+               ref.setServiceType(m_pmt_in_progress->second.serviceType);
+
+               if (!m_ch_current->getSystem(type))
                {
                {
-                       if (sameChannel(*i, m_ch_current))
+                       char sname[255];
+                       char pname[255];
+                       memset(pname, 0, sizeof(pname));
+                       memset(sname, 0, sizeof(sname));
+                       switch(type)
                        {
                        {
-                               eDebug("remove dupe 2");
-                               m_ch_toScan.erase(i++);
-                               continue;
+                               case iDVBFrontend::feSatellite:
+                               {
+                                       eDVBFrontendParametersSatellite parm;
+                                       m_ch_current->getDVBS(parm);
+                                       snprintf(sname, 255, "%d%c SID 0x%02x",
+                                                       parm.frequency/1000,
+                                                       parm.polarisation ? 'V' : 'H',
+                                                       m_pmt_in_progress->first);
+                                       snprintf(pname, 255, "%s %s %d%c %d.%d°%c",
+                                               parm.system ? "DVB-S2" : "DVB-S",
+                                               parm.modulation == 1 ? "QPSK" : "8PSK",
+                                               parm.frequency/1000,
+                                               parm.polarisation ? 'V' : 'H',
+                                               parm.orbital_position/10,
+                                               parm.orbital_position%10,
+                                               parm.orbital_position > 0 ? 'E' : 'W');
+                                       break;
+                               }
+                               case iDVBFrontend::feTerrestrial:
+                               {
+                                       ePtr<iDVBFrontend> fe;
+                                       eDVBFrontendParametersTerrestrial parm;
+                                       m_ch_current->getDVBT(parm);
+                                       snprintf(sname, 255, "%d SID 0x%02x",
+                                               parm.frequency/1000,
+                                               m_pmt_in_progress->first);
+                                       if (!m_channel->getFrontend(fe))
+                                       {
+                                               ePyObject tp_dict = PyDict_New();
+                                               fe->getTransponderData(tp_dict, false);
+                                               m_corrected_frequencys[m_chid_current] =
+                                                       PyInt_AsLong(PyDict_GetItemString(tp_dict, "frequency"));
+                                               Py_DECREF(tp_dict);
+                                       }
+                                       break;
+                               }
+                               case iDVBFrontend::feCable:
+                               {
+                                       eDVBFrontendParametersCable parm;
+                                       m_ch_current->getDVBC(parm);
+                                       snprintf(sname, 255, "%d SID 0x%02x",
+                                               parm.frequency/1000,
+                                               m_pmt_in_progress->first);
+                                       break;
+                               }
+                       }
+                       SCAN_eDebug("name '%s', provider_name '%s'", sname, pname);
+                       service->m_service_name = convertDVBUTF8(sname);
+                       service->genSortName();
+                       service->m_provider_name = convertDVBUTF8(pname);
+               }
+
+               if (!(m_flags & scanOnlyFree) || !m_pmt_in_progress->second.scrambled) {
+                       SCAN_eDebug("add not scrambled!");
+                       std::pair<std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator, bool> i =
+                               m_new_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(ref, service));
+                       if (i.second)
+                       {
+                               m_last_service = i.first;
+                               m_event(evtNewService);
                        }
                        }
-                       ++i;
                }
                }
+               else
+                       SCAN_eDebug("dont add... is scrambled!");
+               m_pmts_to_read.erase(m_pmt_in_progress++);
        }
 
        }
 
+       if (!m_chid_current)
+               eWarning("SCAN: the current channel's ID was not corrected - not adding channel.");
+       else
+               addKnownGoodChannel(m_chid_current, m_ch_current);
+       
+       m_ch_scanned.push_back(m_ch_current);
+       
+       for (std::list<ePtr<iDVBFrontendParameters> >::iterator i(m_ch_toScan.begin()); i != m_ch_toScan.end();)
+       {
+               if (sameChannel(*i, m_ch_current))
+               {
+                       SCAN_eDebug("remove dupe 2");
+                       m_ch_toScan.erase(i++);
+                       continue;
+               }
+               ++i;
+       }
+       
        nextChannel();
 }
 
        nextChannel();
 }
 
@@ -567,52 +847,62 @@ void eDVBScan::insertInto(iDVBChannelList *db, bool dontRemoveOldFlags)
                std::list<ePtr<iDVBFrontendParameters> >::iterator it(m_ch_scanned.begin());
                for (;it != m_ch_scanned.end(); ++it)
                {
                std::list<ePtr<iDVBFrontendParameters> >::iterator it(m_ch_scanned.begin());
                for (;it != m_ch_scanned.end(); ++it)
                {
-                       int system;
-                       (*it)->getSystem(system);
-                       switch(system)
+                       if (m_flags & scanDontRemoveUnscanned)
+                               db->removeServices(&(*(*it)));
+                       else
                        {
                        {
-                               case iDVBFrontend::feSatellite:
-                               {
-                                       eDVBFrontendParametersSatellite sat_parm;
-                                       (*it)->getDVBS(sat_parm);
-                                       scanned_sat_positions.insert(sat_parm.orbital_position);
-                                       break;
-                               }
-                               case iDVBFrontend::feTerrestrial:
+                               int system;
+                               (*it)->getSystem(system);
+                               switch(system)
                                {
                                {
-                                       clearTerrestrial=true;
-                                       break;
-                               }
-                               case iDVBFrontend::feCable:
-                               {
-                                       clearCable=true;
-                                       break;
+                                       case iDVBFrontend::feSatellite:
+                                       {
+                                               eDVBFrontendParametersSatellite sat_parm;
+                                               (*it)->getDVBS(sat_parm);
+                                               scanned_sat_positions.insert(sat_parm.orbital_position);
+                                               break;
+                                       }
+                                       case iDVBFrontend::feTerrestrial:
+                                       {
+                                               clearTerrestrial=true;
+                                               break;
+                                       }
+                                       case iDVBFrontend::feCable:
+                                       {
+                                               clearCable=true;
+                                               break;
+                                       }
                                }
                        }
                }
 
                for (it=m_ch_unavailable.begin();it != m_ch_unavailable.end(); ++it)
                {
                                }
                        }
                }
 
                for (it=m_ch_unavailable.begin();it != m_ch_unavailable.end(); ++it)
                {
-                       int system;
-                       (*it)->getSystem(system);
-                       switch(system)
+                       if (m_flags & scanDontRemoveUnscanned)
+                               db->removeServices(&(*(*it)));
+                       else
                        {
                        {
-                               case iDVBFrontend::feSatellite:
-                               {
-                                       eDVBFrontendParametersSatellite sat_parm;
-                                       (*it)->getDVBS(sat_parm);
-                                       scanned_sat_positions.insert(sat_parm.orbital_position);
-                                       break;
-                               }
-                               case iDVBFrontend::feTerrestrial:
+                               int system;
+                               (*it)->getSystem(system);
+                               switch(system)
                                {
                                {
-                                       clearTerrestrial=true;
-                                       break;
-                               }
-                               case iDVBFrontend::feCable:
-                               {
-                                       clearCable=true;
-                                       break;
+                                       case iDVBFrontend::feSatellite:
+                                       {
+                                               eDVBFrontendParametersSatellite sat_parm;
+                                               (*it)->getDVBS(sat_parm);
+                                               scanned_sat_positions.insert(sat_parm.orbital_position);
+                                               break;
+                                       }
+                                       case iDVBFrontend::feTerrestrial:
+                                       {
+                                               clearTerrestrial=true;
+                                               break;
+                                       }
+                                       case iDVBFrontend::feCable:
+                                       {
+                                               clearCable=true;
+                                               break;
+                                       }
                                }
                        }
                }
                                }
                        }
                }
@@ -641,7 +931,33 @@ void eDVBScan::insertInto(iDVBChannelList *db, bool dontRemoveOldFlags)
 
        for (std::map<eDVBChannelID, ePtr<iDVBFrontendParameters> >::const_iterator 
                        ch(m_new_channels.begin()); ch != m_new_channels.end(); ++ch)
 
        for (std::map<eDVBChannelID, ePtr<iDVBFrontendParameters> >::const_iterator 
                        ch(m_new_channels.begin()); ch != m_new_channels.end(); ++ch)
+       {
+               int system;
+               ch->second->getSystem(system);
+               if (system == iDVBFrontend::feTerrestrial)
+               {
+                       std::map<eDVBChannelID, unsigned int>::iterator it = m_corrected_frequencys.find(ch->first);
+                       if (it != m_corrected_frequencys.end())
+                       {
+                               eDVBFrontendParameters *p = (eDVBFrontendParameters*)&(*ch->second);
+                               eDVBFrontendParametersTerrestrial parm;
+                               p->getDVBT(parm);
+                               eDebug("corrected freq for tsid %04x, onid %04x, ns %08x is %d, old was %d",
+                                       ch->first.transport_stream_id.get(), ch->first.original_network_id.get(),
+                                       ch->first.dvbnamespace.get(), it->second, parm.frequency);
+                               parm.frequency = it->second;
+                               p->setDVBT(parm);
+                               m_corrected_frequencys.erase(it);
+                       }
+               }
+               if (m_flags & scanOnlyFree)
+               {
+                       eDVBFrontendParameters *ptr = (eDVBFrontendParameters*)&(*ch->second);
+                       ptr->setFlags(iDVBFrontendParameters::flagOnlyFree);
+               }
                db->addChannelToList(ch->first, ch->second);
                db->addChannelToList(ch->first, ch->second);
+       }
+
        for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::const_iterator
                service(m_new_services.begin()); service != m_new_services.end(); ++service)
        {
        for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::const_iterator
                service(m_new_services.begin()); service != m_new_services.end(); ++service)
        {
@@ -664,7 +980,8 @@ void eDVBScan::insertInto(iDVBChannelList *db, bool dontRemoveOldFlags)
                else
                {
                        db->addService(service->first, service->second);
                else
                {
                        db->addService(service->first, service->second);
-                       service->second->m_flags |= eDVBService::dxNewFound;
+                       if (!(m_flags & scanRemoveServices))
+                               service->second->m_flags |= eDVBService::dxNewFound;
                }
        }
 }
                }
        }
 }
@@ -680,61 +997,88 @@ RESULT eDVBScan::processSDT(eDVBNamespace dvbnamespace, const ServiceDescription
 
        for (ServiceDescriptionConstIterator s(services.begin()); s != services.end(); ++s)
        {
 
        for (ServiceDescriptionConstIterator s(services.begin()); s != services.end(); ++s)
        {
-               SCAN_eDebugNoNewLine("SID %04x: ", (*s)->getServiceId());
-
-               eServiceReferenceDVB ref;
-               ePtr<eDVBService> service = new eDVBService;
-               
-               ref.set(chid);
-               ref.setServiceID((*s)->getServiceId());
+               unsigned short service_id = (*s)->getServiceId();
+               SCAN_eDebugNoNewLine("SID %04x: ", service_id);
+               bool add = true;
 
 
-               for (DescriptorConstIterator desc = (*s)->getDescriptors()->begin();
-                               desc != (*s)->getDescriptors()->end(); ++desc)
-                       if ((*desc)->getTag() == SERVICE_DESCRIPTOR)
-                               ref.setServiceType(((ServiceDescriptor&)**desc).getServiceType());
-               
-               for (DescriptorConstIterator desc = (*s)->getDescriptors()->begin();
-                               desc != (*s)->getDescriptors()->end(); ++desc)
+               if (m_flags & scanOnlyFree)
                {
                {
-                       switch ((*desc)->getTag())
-                       {
-                       case SERVICE_DESCRIPTOR:
+                       std::map<unsigned short, service>::iterator it =
+                               m_pmts_to_read.find(service_id);
+                       if (it != m_pmts_to_read.end())
                        {
                        {
-                               ServiceDescriptor &d = (ServiceDescriptor&)**desc;
-                               service->m_service_name = convertDVBUTF8(d.getServiceName());
-                               service->genSortName();
-
-                               service->m_provider_name = convertDVBUTF8(d.getServiceProviderName());
-                               SCAN_eDebug("name '%s', provider_name '%s'", service->m_service_name.c_str(), service->m_provider_name.c_str());
-                               break;
-                       }
-                       case CA_IDENTIFIER_DESCRIPTOR:
-                       {
-                               CaIdentifierDescriptor &d = (CaIdentifierDescriptor&)**desc;
-                               const CaSystemIdList &caids = *d.getCaSystemIds();
-                               SCAN_eDebugNoNewLine("CA ");
-                               for (CaSystemIdList::const_iterator i(caids.begin()); i != caids.end(); ++i)
+                               if (it->second.scrambled)
                                {
                                {
-                                       SCAN_eDebugNoNewLine("%04x ", *i);
-                                       service->m_ca.push_front(*i);
+                                       SCAN_eDebug("is scrambled!");
+                                       add = false;
                                }
                                }
-                               SCAN_eDebug("");
-                               break;
+                               else
+                                       SCAN_eDebug("is free");
                        }
                        }
-                       default:
-                               SCAN_eDebug("descr<%x>", (*desc)->getTag());
-                               break;
+                       else {
+                               SCAN_eDebug("not found in PAT.. so we assume it is scrambled!!");
+                               add = false;
                        }
                }
                        }
                }
-               
-               std::pair<std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator, bool> i = m_new_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(ref, service));
-               
-               if (i.second)
+
+               if (add)
                {
                {
-                       m_last_service = i.first;
-                       m_event(evtNewService);
+                       eServiceReferenceDVB ref;
+                       ePtr<eDVBService> service = new eDVBService;
+
+                       ref.set(chid);
+                       ref.setServiceID(service_id);
+
+                       for (DescriptorConstIterator desc = (*s)->getDescriptors()->begin();
+                                       desc != (*s)->getDescriptors()->end(); ++desc)
+                       {
+                               switch ((*desc)->getTag())
+                               {
+                               case SERVICE_DESCRIPTOR:
+                               {
+                                       ServiceDescriptor &d = (ServiceDescriptor&)**desc;
+                                       ref.setServiceType(d.getServiceType());
+                                       service->m_service_name = convertDVBUTF8(d.getServiceName());
+                                       service->genSortName();
+
+                                       service->m_provider_name = convertDVBUTF8(d.getServiceProviderName());
+                                       SCAN_eDebug("name '%s', provider_name '%s'", service->m_service_name.c_str(), service->m_provider_name.c_str());
+                                       break;
+                               }
+                               case CA_IDENTIFIER_DESCRIPTOR:
+                               {
+                                       CaIdentifierDescriptor &d = (CaIdentifierDescriptor&)**desc;
+                                       const CaSystemIdList &caids = *d.getCaSystemIds();
+                                       SCAN_eDebugNoNewLine("CA ");
+                                       for (CaSystemIdList::const_iterator i(caids.begin()); i != caids.end(); ++i)
+                                       {
+                                               SCAN_eDebugNoNewLine("%04x ", *i);
+                                               service->m_ca.push_front(*i);
+                                       }
+                                       SCAN_eDebug("");
+                                       break;
+                               }
+                               default:
+                                       SCAN_eDebug("descr<%x>", (*desc)->getTag());
+                                       break;
+                               }
+                       }
+
+                       std::pair<std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator, bool> i =
+                               m_new_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(ref, service));
+
+                       if (i.second)
+                       {
+                               m_last_service = i.first;
+                               m_event(evtNewService);
+                       }
                }
                }
+               if (m_pmt_running && m_pmt_in_progress->first == service_id)
+                       m_abort_current_pmt = true;
+               else
+                       m_pmts_to_read.erase(service_id);
        }
        }
+
        return 0;
 }
 
        return 0;
 }
 
@@ -758,3 +1102,22 @@ void eDVBScan::getLastServiceName(std::string &last_service_name)
        else
                last_service_name = m_last_service->second->m_service_name;
 }
        else
                last_service_name = m_last_service->second->m_service_name;
 }
+
+RESULT eDVBScan::getFrontend(ePtr<iDVBFrontend> &fe)
+{
+       if (m_channel)
+               return m_channel->getFrontend(fe);
+       fe = 0;
+       return -1;
+}
+
+RESULT eDVBScan::getCurrentTransponder(ePtr<iDVBFrontendParameters> &tp)
+{
+       if (m_ch_current)
+       {
+               tp = m_ch_current;
+               return 0;
+       }
+       tp = 0;
+       return -1;
+}