change interface a bit
[enigma2.git] / lib / dvb / pesparse.cpp
1 #include <lib/base/eerror.h>
2 #include <lib/dvb/pesparse.h>
3 #include <memory.h>
4
5 ePESParser::ePESParser()
6 {
7         m_pes_position = 0;
8         m_pes_length = 0;
9         m_header[0] = 0;
10         m_header[1] = 0;
11         m_header[2] = 1;
12         setStreamID(0); /* must be overridden */
13 }
14
15 void ePESParser::setStreamID(unsigned char id)
16 {
17         m_header[3] = id;
18 }
19
20 void ePESParser::processData(const __u8 *p, int len)
21 {
22         int i;
23
24                 /* this is a state machine, handling arbitary amounts of pes-formatted data. */
25         while (len)
26         {
27                 if (m_pes_position >= 6) // length ok?
28                 {
29                         int max = m_pes_length - m_pes_position;
30                         if (max > len)
31                                 max = len;
32                         memcpy(m_pes_buffer + m_pes_position, p, max);
33                         m_pes_position += max;
34                         p += max;
35                         
36                         len -= max;
37                         
38                         if (m_pes_position == m_pes_length)
39                         {
40                                 processPESPacket(m_pes_buffer, m_pes_position);
41                                 m_pes_position = 0;
42                         }
43                 } else
44                 {
45                         if (m_pes_position < 4)
46                                 if (*p != m_header[m_pes_position])
47                                 {
48 //                                      eDebug("sync lost at %d (%02x)", m_pes_position, *p);
49                                         m_pes_position = 0;
50                                         while (m_header[m_pes_position] == *p) /* guaranteed to stop at the old m_pes_position */
51                                                 m_pes_position++;
52                                         p++;
53                                         len--;
54                                         continue;
55                                 }
56                         m_pes_buffer[m_pes_position++] = *p++; len--;
57                         if (m_pes_position == 6)
58                         {
59                                 m_pes_length = ((m_pes_buffer[4] << 8) | m_pes_buffer[5]) + 6;
60                         }
61                 }
62         }
63 }