]> git.vomp.tv Git - vompclient.git/blob - demuxer.cc
New TS demuxer
[vompclient.git] / demuxer.cc
1 /*
2     Copyright 2005-2006 Mark Calderbank
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "demuxer.h"
22 #ifndef WIN32
23 #include <endian.h>
24 #else
25 #define __LITTLE_ENDIAN 1234
26 #define __BIG_ENDIAN  4321
27 #define __BYTE_ORDER __LITTLE_ENDIAN
28 #endif
29
30 #if __BYTE_ORDER == __BIG_ENDIAN
31 #define DEMUXER_SEQ_HEAD 0x000001B3
32 #else
33 #define DEMUXER_SEQ_HEAD 0xB3010000
34 #endif
35
36 #define DEMUXER_PIC_HEAD 0x00000101
37
38 #define SEEK_THRESHOLD 150000 // About 1.5 seconds
39
40 const int Demuxer::FrameRates[9] = { 0, 23, 24, 25, 29, 30, 50, 59, 60 };
41 const ULLONG Demuxer::PESPacket::PTS_INVALID = (1LL << 33);
42
43 Demuxer* Demuxer::instance = NULL;
44
45 Demuxer::Demuxer()
46 {
47   if (instance) return;
48   instance = this;
49   initted = false;
50   callback = NULL;
51   arcnt = 0;
52   vid_seeking = aud_seeking = false;
53   video_pts = audio_pts = 0;
54 }
55
56 Demuxer::~Demuxer()
57 {
58   shutdown();
59   instance = NULL;
60 }
61
62 Demuxer* Demuxer::getInstance()
63 {
64   return instance;
65 }
66
67 int Demuxer::init(Callback* tcallback, DrainTarget* audio, DrainTarget* video, ULONG demuxMemoryV, ULONG demuxMemoryA)
68 {
69   if (!initted)
70   {
71     if ( !videostream.init(video, demuxMemoryV) ||
72          !audiostream.init(audio, demuxMemoryA))
73     {
74       Log::getInstance()->log("Demuxer", Log::CRIT,
75                               "Failed to initialize demuxer");
76       shutdown();
77       return 0;
78     }
79   }
80
81   reset();
82   initted = true;
83   callback = tcallback;
84   return 1;
85 }
86
87 void Demuxer::reset()
88 {
89   flush();
90   video_current = audio_current = -1;
91   horizontal_size = vertical_size = 0;
92   aspect_ratio = (enum AspectRatio) 0;
93   frame_rate = bit_rate = 0;
94
95   for (int i = 0; i <= (PESTYPE_AUDMAX - PESTYPE_AUD0); i++)
96   {
97     avail_mpaudchan[i] = false;
98   }
99 }
100
101 int Demuxer::shutdown()
102 {
103   videostream.shutdown();
104   audiostream.shutdown();
105   initted = false;
106   return 1;
107 }
108
109 void Demuxer::flush()
110 {
111   videostream.flush();
112   audiostream.flush();
113 }
114
115 void Demuxer::flushAudio()
116 {
117   audiostream.flush();
118 }
119
120 void Demuxer::seek()
121 {
122   vid_seeking = aud_seeking = true;
123   video_pts = audio_pts = 0;
124 }
125
126 void Demuxer::setAudioStream(int id)
127 {
128   audio_current = id;
129 }
130
131 void Demuxer::setVideoStream(int id)
132 {
133   video_current = id;
134 }
135
136 void Demuxer::setAspectRatio(enum AspectRatio ar)
137 {
138   if (aspect_ratio != ar)
139   {
140     Log::getInstance()->log("Demux", Log::DEBUG,
141                             "Aspect ratio difference signalled");
142     if (++arcnt > 3) // avoid changing aspect ratio if glitch in signal
143     {
144       arcnt = 0;
145       aspect_ratio = ar;
146       if (callback) callback->call(this);
147     }
148   }
149   else
150     arcnt = 0;
151 }
152
153 bool Demuxer::writeAudio()
154 {
155   return audiostream.drain();
156 }
157
158 bool Demuxer::writeVideo()
159 {
160   return videostream.drain();
161 }
162
163 Demuxer::PESPacket::PESPacket()
164 {
165   data[0] = 0x00;
166   data[1] = 0x00;
167   data[2] = 0x01;
168   init(0);
169 }
170
171 void Demuxer::PESPacket::init(UCHAR type)
172 {
173   length = submitted = 0;
174   size = 6; closed = false;
175   data[3] = type;
176   data[4] = data[5] = 0;
177   packetType = type;
178   pts = PTS_INVALID;
179   seq_header = false;
180 }
181
182 void Demuxer::PESPacket::truncate()
183 {
184   init(packetType);
185 }
186
187 int Demuxer::PESPacket::write(UCHAR *buf, int len)
188 {
189   if (closed) return 0;
190   if (length + len > 0xFFFA) return 0;
191   memcpy(data+length+6, buf, len);
192   length += len;
193   size += len;
194   data[4] = (length >> 8);
195   data[5] = (length & 0xFF);
196   return 1;
197 }
198
199 int Demuxer::PESPacket::submit()
200 {
201   if (submitted >= size) return 0;
202   if (!closed) parseDetails();
203
204   closed = true;
205   Demuxer* dx = Demuxer::getInstance();
206   int sent;
207   if (packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
208   {
209     if (dx->video_current == -1) dx->video_current = packetType;
210     if (dx->video_current == packetType && !dx->vid_seeking)
211       sent = dx->videostream.put(data+submitted, size-submitted);
212     else
213       sent = size-submitted;
214   }
215   else if (packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
216   {
217     if (dx->audio_current == -1) dx->audio_current = packetType;
218
219     dx->avail_mpaudchan[packetType-PESTYPE_AUD0]=true;
220
221     if (dx->audio_current == packetType && !dx->aud_seeking)
222       sent = dx->audiostream.put(data+submitted, size-submitted);
223     else
224       sent = size-submitted;
225   }
226   else
227   {
228     sent = size-submitted;
229   }
230
231   submitted += sent;
232   if (submitted < size) // Stream is full.
233     return 0;
234   else
235     return 1;
236 }
237
238 void Demuxer::PESPacket::parseDetails()
239 {
240   Demuxer* dx = Demuxer::getInstance();
241   if (packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
242   {
243     // Extract audio PTS if it exists
244     if ( size >= 14 && (data[7] & 0x80) ) // PTS_DTS_flags indicate PTS
245     {
246       dx->audio_pts = pts = ( (ULLONG)(data[9] & 0x0E)  << 29 ) |
247                             ( (ULLONG)(data[10])        << 22 ) |
248                             ( (ULLONG)(data[11] & 0xFE) << 14 ) |
249                             ( (ULLONG)(data[12])        <<  7 ) |
250                             ( (ULLONG)(data[13] & 0xFE) >>  1 );
251
252       // We continue to seek on the audio if the video PTS that we
253       // are trying to match is ahead of the audio PTS by at most
254       // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
255       if (dx->aud_seeking && !dx->vid_seeking &&
256           !( (dx->video_pts_seek > dx->audio_pts &&
257               dx->video_pts_seek - dx->audio_pts < SEEK_THRESHOLD)
258               ||
259              (dx->video_pts_seek < dx->audio_pts &&
260               dx->video_pts_seek + (1LL<<33) -
261                                    dx->audio_pts < SEEK_THRESHOLD) ))
262       {
263         dx->aud_seeking = 0;
264         Log::getInstance()->log("Demuxer", Log::DEBUG,
265             "Leaving  audio sync: Audio PTS = %llu", dx->audio_pts);
266       }
267     }
268   }
269   else if (packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
270   {
271     // Extract video PTS if it exists
272     if ( size >= 14 && (data[7] & 0x80) ) // PTS_DTS_flags indicate PTS
273     {
274       dx->video_pts = pts = ( (ULLONG)(data[9] & 0x0E)  << 29 ) |
275                             ( (ULLONG)(data[10])        << 22 ) |
276                             ( (ULLONG)(data[11] & 0xFE) << 14 ) |
277                             ( (ULLONG)(data[12])        <<  7 ) |
278                             ( (ULLONG)(data[13] & 0xFE) >>  1 );
279     }
280
281     // Now, scan for a sequence header and extract information
282     UINT pos = 9; // Start searching from byte 9
283     while (pos < size - 5)
284     {
285       UINT pattern = *(UINT*)(data+pos);
286       if (pattern == DEMUXER_SEQ_HEAD)
287       {
288         seq_header = true;
289         pos += 4;
290         if (pos+6 >= size) return;
291         dx->horizontal_size = ((int)data[pos] << 4) | ((int)data[pos+1] >> 4);
292         dx->vertical_size = (((int)data[pos+1] & 0xf) << 8) | (int)data[pos+2];
293         dx->setAspectRatio((enum AspectRatio)(data[pos+3] >> 4));
294         dx->frame_rate = data[pos+3] & 0x0f;
295         if (dx->frame_rate >= 1 && dx->frame_rate <= 8)
296           dx->frame_rate = FrameRates[dx->frame_rate];
297         else
298           dx->frame_rate = 0;
299         dx->bit_rate = ((int)data[pos+4] << 10) |
300                    ((int)data[pos+5] << 2) |
301                    ((int)data[pos+6] >> 6);
302         if (dx->vid_seeking)
303         {
304           dx->vid_seeking = 0;
305           dx->video_pts_seek = dx->video_pts;
306           Log::getInstance()->log("Demuxer", Log::DEBUG,
307               "Entering audio sync: Video PTS = %llu", dx->video_pts);
308           Log::getInstance()->log("Demuxer", Log::DEBUG,
309               "Entering audio sync: Audio PTS = %llu", dx->audio_pts);
310         }
311         return;
312       }
313       else pos++;
314     }
315   }
316 }
317
318 UINT Demuxer::PESPacket::findPictureHeader()
319 {
320   if (size < 12) return 0;
321   UINT pattern = *(UINT*)(data+8);
322   UINT pos = 11;
323   while (pattern != DEMUXER_PIC_HEAD)
324   {
325     if (++pos >= size) return 0;
326     pattern = (pattern << 8) | data[pos];
327   }
328   return pos-3;
329 }
330
331 UINT Demuxer::stripAudio(UCHAR* buf, UINT len)
332 {
333   UINT read_pos = 0, write_pos = 0;
334   UINT pattern, packet_length;
335   if (len < 4) return 0;
336   pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
337   while (read_pos + 7 <= len)
338   {
339     pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
340     if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
341       read_pos++;
342     else
343     {
344       packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
345       if (read_pos + packet_length > len)
346         read_pos = len;
347       else
348       {
349         if (read_pos != write_pos)
350           memmove(buf+write_pos, buf+read_pos, packet_length);
351         read_pos += packet_length;
352         write_pos += packet_length;
353         pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
354                                         | (buf[read_pos+2]);
355       }
356     }
357   }
358   return write_pos;
359 }
360
361 bool Demuxer::scanForVideo(UCHAR* buf, UINT len)
362 {
363   UINT pos = 3;
364   UINT pattern;
365   if (len < 4) return false;
366   pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
367   while (pos < len)
368   {
369     pattern = ((pattern & 0xFFFFFF) << 8) | buf[pos++];
370     if (pattern >= (0x100|PESTYPE_VID0) && pattern <= (0x100|PESTYPE_VIDMAX))
371       return true;
372   }
373   return false;
374 }
375
376 bool* Demuxer::getmpAudioChannels()
377 {
378   return avail_mpaudchan;
379 }
380
381 int Demuxer::getselAudioChannel()
382 {
383   return audio_current;
384 }
385
386 void Demuxer::setmpAudioChannel(int aud_channel)
387 {
388   audio_current = aud_channel;
389 }