add pes parser
authorFelix Domke <tmbinc@elitedvb.net>
Fri, 12 May 2006 15:17:24 +0000 (15:17 +0000)
committerFelix Domke <tmbinc@elitedvb.net>
Fri, 12 May 2006 15:17:24 +0000 (15:17 +0000)
lib/dvb/Makefile.am
lib/dvb/pesparse.cpp [new file with mode: 0644]
lib/dvb/pesparse.h [new file with mode: 0644]

index a255a61..49ed05f 100644 (file)
@@ -5,4 +5,5 @@ noinst_LIBRARIES = libenigma_dvb.a
 
 libenigma_dvb_a_SOURCES = dvb.cpp demux.cpp frontend.cpp esection.cpp db.cpp \
        sec.cpp scan.cpp crc32.cpp pmt.cpp decoder.cpp eit.cpp rotor_calc.cpp \
-       epgcache.cpp dvbtime.cpp metaparser.cpp volume.cpp tstools.cpp pvrparse.cpp
+       epgcache.cpp dvbtime.cpp metaparser.cpp volume.cpp tstools.cpp pvrparse.cpp \
+       pesparse.cpp
diff --git a/lib/dvb/pesparse.cpp b/lib/dvb/pesparse.cpp
new file mode 100644 (file)
index 0000000..7e05c96
--- /dev/null
@@ -0,0 +1,57 @@
+#include <lib/dvb/pesparse.h>
+#include <memory.h>
+
+ePESParser::ePESParser()
+{
+       m_pes_position = 0;
+       m_pes_length = 0;
+       m_header[0] = 0;
+       m_header[1] = 0;
+       m_header[2] = 1;
+       setStreamID(0); /* must be overridden */
+}
+
+void ePESParser::setStreamID(unsigned char id)
+{
+       m_header[3] = id;
+}
+
+void ePESParser::processData(unsigned char *p, int len)
+{
+               /* this is a state machine, handling arbitary amounts of pes-formatted data. */
+       while (len)
+       {
+               if (m_pes_position >= 6) // length ok?
+               {
+                       int max = m_pes_length - m_pes_position;
+                       if (max > len)
+                               max = len;
+                       memcpy(m_pes_buffer + m_pes_position, p, max);
+                       m_pes_position += max;
+                       p += max;
+                       
+                       len -= max;
+                       
+                       if (m_pes_position == m_pes_length)
+                       {
+                               processPESPacket(m_pes_buffer, m_pes_position);
+                               m_pes_position = 0;
+                       }
+               } else
+               {
+                       if (m_pes_position < 4)
+                               if (*p != "\x00\x00\x01\xbd"[m_pes_position])
+                               {
+                                       m_pes_position = 0;
+                                       p++;
+                                       len--;
+                                       continue;
+                               }
+                       m_pes_buffer[m_pes_position++] = *p++; len--;
+                       if (m_pes_position == 6)
+                       {
+                               m_pes_length = ((m_pes_buffer[4] << 8) | m_pes_buffer[5]) + 6;
+                       }
+               }
+       }
+}
diff --git a/lib/dvb/pesparse.h b/lib/dvb/pesparse.h
new file mode 100644 (file)
index 0000000..f2f59f9
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef __lib_dvb_pesparse_h
+#define __lib_dvb_pesparse_h
+
+class ePESParser
+{
+public:
+       ePESParser();
+       void setStreamID(unsigned char id);
+       void processData(unsigned char *data, int len);
+       virtual void processPESPacket(unsigned char *pkt, int len) = 0;
+private:
+       unsigned char m_pes_buffer[65536];
+       int m_pes_position, m_pes_length;
+       unsigned char m_header[4];
+};
+
+#endif