fix some refcounting bugs
[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, unsigned char id_mask)
16 {
17         m_header[3] = id;
18         m_stream_id_mask = id_mask;
19 }
20
21 void ePESParser::processData(const __u8 *p, int len)
22 {
23                 /* this is a state machine, handling arbitary amounts of pes-formatted data. */
24         while (len)
25         {
26                 if (m_pes_position >= 6) // length ok?
27                 {
28                         int max = m_pes_length - m_pes_position;
29                         if (max > len)
30                                 max = len;
31                         memcpy(m_pes_buffer + m_pes_position, p, max);
32                         m_pes_position += max;
33                         p += max;
34                         
35                         len -= max;
36                         
37                         if (m_pes_position == m_pes_length)
38                         {
39                                 processPESPacket(m_pes_buffer, m_pes_position);
40                                 m_pes_position = 0;
41                         }
42                 } else
43                 {
44                         if (m_pes_position < 4)
45                         {
46                                 unsigned char ch = *p;
47                                 if (m_pes_position == 3)
48                                         ch &= m_stream_id_mask;
49                                 if (ch != m_header[m_pes_position])
50                                 {
51 //                                      eDebug("sync lost at %d (%02x)", m_pes_position, *p);
52                                         m_pes_position = 0;
53                                         while (m_header[m_pes_position] == ch) /* guaranteed to stop at the old m_pes_position */
54                                                 m_pes_position++;
55                                         p++;
56                                         len--;
57                                         continue;
58                                 }
59                         }
60                         m_pes_buffer[m_pes_position++] = *p++; len--;
61                         if (m_pes_position == 6)
62                         {
63                                 m_pes_length = ((m_pes_buffer[4] << 8) | m_pes_buffer[5]) + 6;
64                         }
65                 }
66         }
67 }