some more small steps on the painful way to an ULE decoder ;)
tho [Tue, 31 Jul 2007 15:04:33 +0000 (15:04 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@368 270642c3-0616-0410-b53a-bc976706d245

Examples/DVBAdapter/ULEdec.cc
Examples/DVBAdapter/ULEdec.hh
Packets/MPEGDVBBundle/SNDUPacket.cc
Packets/MPEGDVBBundle/SNDUPacket.hh

index a732b96..5b3e2f8 100644 (file)
@@ -25,6 +25,7 @@
 #include <string>
 #include <iostream>
 #include <iomanip>
+#include <algorithm>
 #include <sys/ioctl.h>
 #include <linux/sockios.h>
 #include <linux/dvb/dmx.h> 
@@ -99,7 +100,8 @@ ULEdec::ULEdec()
     senf::Scheduler::instance().add(
         dvrHandle, senf::membind(&ULEdec::handleEvent, this));
     
-    this->receiver_state = 1; // Idle
+    this->receiver_state = Idle;
+    this->priv_sndu_type_1 = false;
 }
 
 void ULEdec::handleEvent(senf::FileHandle, senf::Scheduler::EventId event)
@@ -123,56 +125,95 @@ void ULEdec::handleEvent(senf::FileHandle, senf::Scheduler::EventId event)
     
 void ULEdec::handleTSPacket(senf::TransportPacket ts_packet)
 {
-    unsigned char payload_pointer;
-
-    senf::PacketData &ts_payload (ts_packet.next().data());
-    BOOST_ASSERT( ts_payload.size() == 184 );
+    senf::PacketData & payloadData (ts_packet.next().data());
+    iterator payload_start = payloadData.begin();
+    iterator payload_end = payloadData.end();
     
-    switch (this->receiver_state) {
-    case 1: { // Idle State
-        // resync on PUSI
-        if (ts_packet->pusi() == 0) 
-            return; // skip packet
-        
-        // Synchronize continuity counter
-        this->priv_tscc = ts_packet->continuity_counter();
-
+    hexdump(payload_start, payload_end, std::cout);
+    
+    // Synchronize continuity counter
+    this->priv_tscc = ts_packet->continuity_counter();
+    
+    if (ts_packet->pusi() == 0) {
+        switch (this->receiver_state) {
+        case Idle:
+            return;
+        case Reassembly:
+            readContSNDUPacket( payload_start, payload_end );
+        }
+    } else {
         // a PUSI value of 1 indicates the presence of a Payload Pointer.
-        payload_pointer = ts_payload[0];
+        unsigned char payload_pointer = *payload_start++;
         if (payload_pointer>181) {
             std::cerr << "invalid payload_pointer\n";
             return;
         }
-        payload_pointer++;
-        
-        bool dbit = false;
-        senf::Packet::size_type sndu_length;
-        while (payload_pointer < 184) {
-            sndu_length = ts_payload[payload_pointer] << 8 | ts_payload[payload_pointer+1];
-            if (sndu_length & 0x8000) {
-                sndu_length &= 0x7FFF;
-                dbit = true;
-            }
-            this->snduPacket = senf::SNDUPacket::create(sndu_length+2);
-            this->snduPacket->d_bit() = dbit;
-            this->snduPacket->length() = sndu_length;
+        switch (this->receiver_state) {
+        case Idle:
+            payload_start += payload_pointer;
+            readNewSNDUPacket( payload_start, payload_end );
+            this->snduPacket.dump(std::cout);
+            break;
+        case Reassembly:
+            readContSNDUPacket( payload_start, payload_end );
+        }       
+    }
 
-            return;
-            
-            payload_pointer += 2;
-            
-            
-        }
-        
-        
-        
-        // 
-        break;
+}
+
+
+ULEdec::iterator ULEdec::readContSNDUPacket(iterator i_start, iterator i_end)
+{
+    if (priv_sndu_type_1) {
+        this->snduPacket->type() |= *i_start++;
     }
-    case 2: { // Reassembly State
-        break;
+    i_start = readRawSNDUPacketData(i_start, i_end);
+    
+    return i_start;
+}
+
+
+ULEdec::iterator ULEdec::readNewSNDUPacket(iterator i_start, iterator i_end)
+{ 
+    bool dbit = false;
+    senf::Packet::size_type sndu_length;
+    sndu_length = *i_start++ << 8 | *i_start++;
+    if (sndu_length & 0x8000) {
+        sndu_length &= 0x7FFF;
+        dbit = true;
     }
+    this->snduPacket = senf::SNDUPacket::create(sndu_length+4);
+    this->snduPacket->d_bit() = dbit;
+    this->snduPacket->length() = sndu_length;
+    this->snduPacketData_iter = this->snduPacket.data().begin() + 2;
+    
+    switch (std::distance(i_start, i_end)) {
+    case 1:
+        this->priv_sndu_type_1 = true;;
+        this->snduPacket->type() = *i_start++;
+        this->snduPacketData_iter++;
+    case 0:
+        break;
+        
+    default: 
+        this->snduPacket->type() = *i_start++ | *i_start++;
+        this->snduPacketData_iter += 2;
+        i_start = readRawSNDUPacketData(i_start, i_end);
     }
+    
+    return i_start;
+}
+
+
+ULEdec::iterator ULEdec::readRawSNDUPacketData(iterator i_start, iterator i_end)
+{
+    unsigned char how_much = std::min(
+            std::distance( this->snduPacketData_iter, this->snduPacket.data().end() ),
+            std::distance( i_start, i_end ) );
+    copy_n(i_start, how_much, this->snduPacketData_iter);
+    i_start += how_much;
+    this->snduPacketData_iter += how_much;
+    return i_start;
 }
 
 
index bfd177f..9ec78b0 100644 (file)
@@ -47,11 +47,22 @@ public:
     ULEdec();
 
 private:
+    typedef senf::PacketData::iterator iterator;
+    
+    enum ReciverState {
+        Idle,
+        Reassembly
+    };
+    
     senf::DVBDemuxPESHandle demuxHandle;
     senf::DVBDvrHandle dvrHandle;
+    
     senf::SNDUPacket snduPacket;
-    unsigned char receiver_state;
+    
+    ReciverState receiver_state;
     unsigned char priv_tscc;
+    bool priv_sndu_type_1;
+    iterator snduPacketData_iter;
 
     static const unsigned BLOCK_SIZE = 16;
     
@@ -61,6 +72,11 @@ private:
     void handleEvent(senf::FileHandle, senf::Scheduler::EventId event);
     
     void handleTSPacket(senf::TransportPacket tsPacket);
+    
+    iterator readNewSNDUPacket(iterator i_start, iterator i_end);
+    iterator readContSNDUPacket(iterator i_start, iterator i_end);
+    iterator readRawSNDUPacketData(iterator i_start, iterator i_end);
+    
 };
 
 \f
index c5c9dc6..d2f4546 100644 (file)
@@ -49,7 +49,10 @@ prefix_ senf::PacketParserBase::size_type senf::Parse_SNDUPacket::bytes()
 
 prefix_ void senf::SNDUPacketType::dump(packet p, std::ostream & os)
 {
-    os << "SNDUPacket:\n";
+    os << "SNDUPacket:\n"
+       << "  d_bit: " << p->d_bit() << "\n"
+       << "  length: " << unsigned(p->length()) << "\n"
+       << "  crc: " << unsigned(p->crc()) << "\n";
 }
 
 prefix_ senf::PacketParserBase::size_type senf::SNDUPacketType::initSize()
index bb219ca..79833c2 100644 (file)
@@ -49,7 +49,7 @@ namespace senf {
     {
 #       ifndef DOXYGEN
         
-        SENF_PACKET_PARSER_INIT(Parse_SNDUPacket);
+        SENF_PACKET_PARSER_NO_INIT(Parse_SNDUPacket);
 
 #       endif
         
@@ -73,6 +73,11 @@ namespace senf {
             return parse<Parse_UInt32>( data().size()-4 ); 
         }
         
+        void init() const {
+            defaultInit();
+            d_bit() = false;
+        }
+        
         PacketParserBase::size_type bytes() const;
         
         static const size_type init_bytes = 2+2+4; // D-Bit + 15 bits length + 16 bits type field + 32 bits crc
@@ -110,8 +115,8 @@ namespace senf {
 
     /*!
      \def ULE_END_INDICATOR 
-         ULE End Indicator; indicates to the Receiver that there are no 
-         further SNDUs present within the current TS Packet.
+         ULE End Indicator; indicates to the receiver that there are no 
+         further SNDUs present within the current TS packet.
     */
 #   define ULE_END_INDICATOR 0xffff
 }