]> git.vomp.tv Git - vompserver.git/blob - remux/tsremux.c
Initial import
[vompserver.git] / remux / tsremux.c
1 #include "tsremux.h"
2
3 // from VDR's remux.c
4 #define MAXNONUSEFULDATA (10*1024*1024)
5 #define SC_PICTURE 0x00  // "picture header"
6 #define VIDEO_STREAM_S   0xE0
7
8 cTSRemux::cTSRemux(bool Sync) {
9         memset(m_PROTECTION1, 0x20, PROTECTIONSIZE);
10         m_ResultCount = 0;
11         m_ResultDelivered = 0;
12         m_Synced = false;
13         m_Skipped = 0;
14         m_Sync = Sync;
15 }
16
17 cTSRemux::~cTSRemux(void) {
18 }
19
20 uchar *cTSRemux::Process(const uchar *Data, int &Count, int &Result) {
21   // Remove any previously delivered data from the result buffer:
22   if (m_ResultDelivered) {
23     if (m_ResultDelivered < m_ResultCount)
24       memmove(m_ResultBuffer, m_ResultBuffer + m_ResultDelivered, m_ResultCount
25                                         - m_ResultDelivered);
26     m_ResultCount -= m_ResultDelivered;
27     m_ResultDelivered = 0;
28         }
29
30   int used = 0;
31
32         // Make sure we are looking at a TS packet:
33   while (Count > TS_SIZE) {
34                 if (Data[0] == 0x47 && Data[TS_SIZE] == 0x47)
35                         break;
36     Data++;
37     Count--;
38     used++;
39   }
40   if (used)
41     esyslog("ERROR: skipped %d byte to sync on TS packet", used);
42
43         // Convert incoming TS data
44   for (int i = 0; i < Count; i += TS_SIZE) {
45     if (Count - i < TS_SIZE)
46       break;
47     if (Data[i] != 0x47)
48       break;
49     int pid = get_pid((uint8_t*)(Data + i + 1));
50     if (Data[i + 3] & 0x10) // got payload
51                         PutTSPacket(pid, Data + i);
52     /*if      (pid == m_VPid)               m_VRemux->ConvertTSPacket(Data + i);
53          else if (pid == m_APid1)              m_ARemux1->ConvertTSPacket(Data + i);
54          else if (pid == m_APid2 && m_ARemux2) m_ARemux2->ConvertTSPacket(Data + i);
55          else if (pid == m_DPid1 && m_DRemux1) m_DRemux1->ConvertTSPacket(Data + i);
56          else if (pid == m_DPid2 && m_DRemux2) m_DRemux2->ConvertTSPacket(Data + i);*/
57     used += TS_SIZE;
58     if (m_ResultCount > (int)sizeof(m_ResultBuffer) / 2)
59       break;
60
61 #if 0
62     if (memcmp(m_PROTECTION1, m_PROTECTION1+(PROTECTIONSIZE/2), PROTECTIONSIZE/2)) {
63       int ow_from = -1; // counted from _end_ of protection buffer
64       int ow_to = -1; // counted from _end_ of protection buffer
65       for (int pi=0; pi<PROTECTIONSIZE; pi++) {
66         if (ow_from < 0 && m_PROTECTION1[PROTECTIONSIZE-1-pi] != 0x20) {
67           ow_from = pi;
68         }
69         if (ow_to < 0 && m_PROTECTION1[pi] != 0x20) {
70           ow_to = PROTECTIONSIZE-1-pi;
71         }
72       }
73       fprintf(stderr, "OVERWRITE detected !!! from -%d to -%d\n", ow_from, ow_to);
74       fprintf(stderr, " well, hopefully one signal 11 fewer ;-)\n");
75       memset(m_PROTECTION1, 0x20, PROTECTIONSIZE);
76     }
77 #endif
78
79   }
80   Count = used;
81
82         // When we don't need to sync, we don't need to sync :-)
83   if (!m_Sync) {
84     Result = m_ResultDelivered = m_ResultCount;
85     return Result ? m_ResultBuffer : NULL;
86   }
87
88         // Check if we're getting anywhere here:
89
90   if (!m_Synced && m_Skipped >= 0) {
91      if (m_Skipped > MAXNONUSEFULDATA) {
92         esyslog("ERROR: no useful data seen within %d byte of video stream", m_Skipped);
93         m_Skipped = -1;
94         //if (exitOnFailure)
95            //cThread::EmergencyExit(true);
96         }
97      else
98         m_Skipped += Count;
99      }
100
101   // Check for frame borders:
102
103   if (m_ResultCount >= MINVIDEODATA) {
104      for (int i = 0; i < m_ResultCount; i++) {
105          if (m_ResultBuffer[i] == 0 && m_ResultBuffer[i + 1] == 0 && m_ResultBuffer[i + 2] == 1) {
106             switch (m_ResultBuffer[i + 3]) {
107               case VIDEO_STREAM_S ... VIDEO_STREAM_E:
108                    {
109                      uchar pt = NO_PICTURE;
110                      int l = ScanVideoPacket(m_ResultBuffer, m_ResultCount, i, pt);
111                      if (l < 0)
112                         return NULL; // no useful data found, wait for more
113                      if (pt != NO_PICTURE) {
114                         if (pt < I_FRAME || B_FRAME < pt)
115                            esyslog("ERROR: unknown picture type '%d'", pt);
116                         else if (!m_Synced) {
117                            if (pt == I_FRAME) {
118                               m_ResultDelivered = i; // will drop everything before this position
119                               SetBrokenLink(m_ResultBuffer + i, l);
120                               m_Synced = true;
121                               }
122                            else {
123                               m_ResultDelivered = i + l; // will drop everything before and including this packet
124                               return NULL;
125                               }
126                            }
127                         }
128                      if (m_Synced) {
129                         Result = l;
130                         uchar *p = m_ResultBuffer + m_ResultDelivered;
131                         m_ResultDelivered += l;
132                         return p;
133                         }
134                      else {
135                         m_ResultDelivered = i + l; // will drop everything before and including this packet
136                         return NULL;
137                         }
138                    }
139                    break;
140               case PRIVATE_STREAM1:
141               case AUDIO_STREAM_S ... AUDIO_STREAM_E:
142                    {
143                      int l = GetPacketLength(m_ResultBuffer, m_ResultCount, i);
144                      if (l < 0)
145                         return NULL; // no useful data found, wait for more
146
147                      /* This shouldn't be happening - why does it for some channels? */
148                      if ( l > m_ResultCount )
149                          return NULL;
150                      if (m_Synced) {
151                         Result = l;
152                         uchar *p = m_ResultBuffer + m_ResultDelivered;
153                         m_ResultDelivered += l;
154                         return p;
155                         }
156                      else {
157                         m_ResultDelivered = i + l; // will drop everything before and including this packet
158                         return NULL;
159                         }
160                    }
161                    break;
162               }
163             }
164          }
165      }
166   return NULL; // no useful data found, wait for more
167 }
168
169 int cTSRemux::ScanVideoPacket(const uchar *Data, int Count, int Offset, uchar &PictureType) {
170   // Scans the video packet starting at Offset and returns its length.
171   // If the return value is -1 the packet was not completely in the buffer.
172
173   int Length = GetPacketLength(Data, Count, Offset);
174   if (Length > 0 && Offset + Length <= Count) {
175      int i = Offset + 8; // the minimum length of the video packet header
176      i += Data[i] + 1;   // possible additional header bytes
177      for (; i < Offset + Length; i++) {
178          if (Data[i] == 0 && Data[i + 1] == 0 && Data[i + 2] == 1) {
179             switch (Data[i + 3]) {
180               case SC_PICTURE: PictureType = (Data[i + 5] >> 3) & 0x07;
181                                return Length;
182               }
183             }
184          }
185      PictureType = NO_PICTURE;
186      return Length;
187      }
188   return -1;
189 }
190
191 int cTSRemux::GetPacketLength(const uchar *Data, int Count, int Offset) {
192   // Returns the entire length of the packet starting at offset, or -1 in case of error.
193   return (Offset + 5 < Count) ? (Data[Offset + 4] << 8) + Data[Offset + 5] + 6 : -1;
194 }
195
196 void cTSRemux::SetBrokenLink(uchar *Data, int Length) {
197   if (Length > 9 && Data[0] == 0 && Data[1] == 0 && Data[2] == 1 && (Data[3] & VIDEO_STREAM_S) == VIDEO_STREAM_S) {
198      for (int i = Data[8] + 9; i < Length - 7; i++) { // +9 to skip video packet header
199          if (Data[i] == 0 && Data[i + 1] == 0 && Data[i + 2] == 1 && Data[i + 3] == 0xB8) {
200             if (!(Data[i + 7] & 0x40)) // set flag only if GOP is not closed
201                Data[i + 7] |= 0x20;
202             return;
203             }
204          }
205      dsyslog("SetBrokenLink: no GOP header found in video packet");
206      }
207   else
208      dsyslog("SetBrokenLink: no video packet in frame");
209 }