]> git.vomp.tv Git - vompclient.git/blob - demuxer.cc
Update for windows
[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 int Demuxer::PESPacket::write(UCHAR *buf, int len)
183 {
184   if (closed) return 0;
185   if (length + len > 0xFFFA) return 0;
186   memcpy(data+length+6, buf, len);
187   length += len;
188   size += len;
189   data[4] = (length >> 8);
190   data[5] = (length & 0xFF);
191   return 1;
192 }
193
194 int Demuxer::PESPacket::submit()
195 {
196   if (submitted >= size) return 0;
197   if (!closed) parseDetails();
198
199   closed = true;
200   Demuxer* dx = Demuxer::getInstance();
201   int sent;
202   if (packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
203   {
204     if (dx->video_current == -1) dx->video_current = packetType;
205     if (dx->video_current == packetType && !dx->vid_seeking)
206       sent = dx->videostream.put(data+submitted, size-submitted);
207     else
208       sent = size-submitted;
209   }
210   else if (packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
211   {
212     if (dx->audio_current == -1) dx->audio_current = packetType;
213
214     dx->avail_mpaudchan[packetType-PESTYPE_AUD0]=true;
215
216     if (dx->audio_current == packetType && !dx->aud_seeking)
217       sent = dx->audiostream.put(data+submitted, size-submitted);
218     else
219       sent = size-submitted;
220   }
221   else
222   {
223     sent = size-submitted;
224   }
225
226   submitted += sent;
227   if (submitted < size) // Stream is full.
228     return 0;
229   else
230     return 1;
231 }
232
233 void Demuxer::PESPacket::parseDetails()
234 {
235   Demuxer* dx = Demuxer::getInstance();
236   if (packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
237   {
238     // Extract audio PTS if it exists
239     if ( size >= 14 && (data[7] & 0x80) ) // PTS_DTS_flags indicate PTS
240     {
241       dx->audio_pts = pts = ( (ULLONG)(data[9] & 0x0E)  << 29 ) |
242                             ( (ULLONG)(data[10])        << 22 ) |
243                             ( (ULLONG)(data[11] & 0xFE) << 14 ) |
244                             ( (ULLONG)(data[12])        <<  7 ) |
245                             ( (ULLONG)(data[13] & 0xFE) >>  1 );
246
247       // We continue to seek on the audio if the video PTS that we
248       // are trying to match is ahead of the audio PTS by at most
249       // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
250       if (dx->aud_seeking && !dx->vid_seeking &&
251           !( (dx->video_pts_seek > dx->audio_pts &&
252               dx->video_pts_seek - dx->audio_pts < SEEK_THRESHOLD)
253               ||
254              (dx->video_pts_seek < dx->audio_pts &&
255               dx->video_pts_seek + (1LL<<33) -
256                                    dx->audio_pts < SEEK_THRESHOLD) ))
257       {
258         dx->aud_seeking = 0;
259         Log::getInstance()->log("Demuxer", Log::DEBUG,
260             "Leaving  audio sync: Audio PTS = %llu", dx->audio_pts);
261       }
262     }
263   }
264   else if (packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
265   {
266     // Extract video PTS if it exists
267     if ( size >= 14 && (data[7] & 0x80) ) // PTS_DTS_flags indicate PTS
268     {
269       dx->video_pts = pts = ( (ULLONG)(data[9] & 0x0E)  << 29 ) |
270                             ( (ULLONG)(data[10])        << 22 ) |
271                             ( (ULLONG)(data[11] & 0xFE) << 14 ) |
272                             ( (ULLONG)(data[12])        <<  7 ) |
273                             ( (ULLONG)(data[13] & 0xFE) >>  1 );
274     }
275
276     // Now, scan for a sequence header and extract information
277     UINT pos = 9; // Start searching from byte 9
278     while (pos < size - 5)
279     {
280       UINT pattern = *(UINT*)(data+pos);
281       if (pattern == DEMUXER_SEQ_HEAD)
282       {
283         seq_header = true;
284         pos += 4;
285         if (pos+6 >= size) return;
286         dx->horizontal_size = ((int)data[pos] << 4) | ((int)data[pos+1] >> 4);
287         dx->vertical_size = (((int)data[pos+1] & 0xf) << 8) | (int)data[pos+2];
288         dx->setAspectRatio((enum AspectRatio)(data[pos+3] >> 4));
289         dx->frame_rate = data[pos+3] & 0x0f;
290         if (dx->frame_rate >= 1 && dx->frame_rate <= 8)
291           dx->frame_rate = FrameRates[dx->frame_rate];
292         else
293           dx->frame_rate = 0;
294         dx->bit_rate = ((int)data[pos+4] << 10) |
295                    ((int)data[pos+5] << 2) |
296                    ((int)data[pos+6] >> 6);
297         if (dx->vid_seeking)
298         {
299           dx->vid_seeking = 0;
300           dx->video_pts_seek = dx->video_pts;
301           Log::getInstance()->log("Demuxer", Log::DEBUG,
302               "Entering audio sync: Video PTS = %llu", dx->video_pts);
303           Log::getInstance()->log("Demuxer", Log::DEBUG,
304               "Entering audio sync: Audio PTS = %llu", dx->audio_pts);
305         }
306         return;
307       }
308       else pos++;
309     }
310   }
311 }
312
313 UINT Demuxer::PESPacket::findPictureHeader()
314 {
315   if (size < 12) return 0;
316   UINT pattern = *(UINT*)(data+8);
317   UINT pos = 11;
318   while (pattern != DEMUXER_PIC_HEAD)
319   {
320     if (++pos >= size) return 0;
321     pattern = (pattern << 8) | data[pos];
322   }
323   return pos-3;
324 }
325
326 UINT Demuxer::stripAudio(UCHAR* buf, UINT len)
327 {
328   UINT read_pos = 0, write_pos = 0;
329   UINT pattern, packet_length;
330   if (len < 4) return 0;
331   pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
332   while (read_pos + 7 <= len)
333   {
334     pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
335     if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
336       read_pos++;
337     else
338     {
339       packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
340       if (read_pos + packet_length > len)
341         read_pos = len;
342       else
343       {
344         if (read_pos != write_pos)
345           memmove(buf+write_pos, buf+read_pos, packet_length);
346         read_pos += packet_length;
347         write_pos += packet_length;
348         pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
349                                         | (buf[read_pos+2]);
350       }
351     }
352   }
353   return write_pos;
354 }
355
356 bool Demuxer::scanForVideo(UCHAR* buf, UINT len)
357 {
358   UINT pos = 3;
359   UINT pattern;
360   if (len < 4) return false;
361   pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
362   while (pos < len)
363   {
364     pattern = ((pattern & 0xFFFFFF) << 8) | buf[pos++];
365     if (pattern >= (0x100|PESTYPE_VID0) && pattern <= (0x100|PESTYPE_VIDMAX))
366       return true;
367   }
368   return false;
369 }
370
371 bool* Demuxer::getmpAudioChannels()
372 {
373   return avail_mpaudchan;
374 }
375
376 int Demuxer::getselAudioChannel()
377 {
378   return audio_current;
379 }
380
381 void Demuxer::setmpAudioChannel(int aud_channel)
382 {
383   audio_current = aud_channel;
384 }